* .mov files with uncompressed audio can't be correctly processed
[libav.git] / libavcodec / mp3lameaudio.c
CommitLineData
0716b577
J
1/*
2 * Interface to libmp3lame for mp3 encoding
3 * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
4 *
ff4ec49e
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
0716b577 9 *
ff4ec49e 10 * This library is distributed in the hope that it will be useful,
0716b577 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
0716b577 14 *
ff4ec49e
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0716b577 18 */
983e3246
MN
19
20/**
21 * @file mp3lameaudio.c
22 * Interface to libmp3lame for mp3 encoding.
23 */
0716b577
J
24
25#include "avcodec.h"
0716b577
J
26#include "mpegaudio.h"
27#include <lame/lame.h>
28
29typedef struct Mp3AudioContext {
30 lame_global_flags *gfp;
0716b577
J
31 int stereo;
32} Mp3AudioContext;
33
34
35static int MP3lame_encode_init(AVCodecContext *avctx)
36{
37 Mp3AudioContext *s = avctx->priv_data;
38
39 if (avctx->channels > 2)
40 return -1;
41
0716b577
J
42 s->stereo = avctx->channels > 1 ? 1 : 0;
43
44 if ((s->gfp = lame_init()) == NULL)
45 goto err;
46 lame_set_in_samplerate(s->gfp, avctx->sample_rate);
e2263827 47 lame_set_out_samplerate(s->gfp, avctx->sample_rate);
0716b577
J
48 lame_set_num_channels(s->gfp, avctx->channels);
49 /* lame 3.91 dies on quality != 5 */
50 lame_set_quality(s->gfp, 5);
51 /* lame 3.91 doesn't work in mono */
52 lame_set_mode(s->gfp, JOINT_STEREO);
53 lame_set_brate(s->gfp, avctx->bit_rate/1000);
c276af04 54 lame_set_bWriteVbrTag(s->gfp,0);
0716b577
J
55 if (lame_init_params(s->gfp) < 0)
56 goto err_close;
57
58 avctx->frame_size = MPA_FRAME_SIZE;
befaced5
MN
59
60 avctx->coded_frame= avcodec_alloc_frame();
61 avctx->coded_frame->key_frame= 1;
0716b577
J
62
63 return 0;
64
65err_close:
66 lame_close(s->gfp);
67err:
68 return -1;
69}
70
71int MP3lame_encode_frame(AVCodecContext *avctx,
72 unsigned char *frame, int buf_size, void *data)
73{
74 Mp3AudioContext *s = avctx->priv_data;
c276af04
MN
75 int num, i;
76//av_log(avctx, AV_LOG_DEBUG, "%X %d %X\n", (int)frame, buf_size, (int)data);
77// if(data==NULL)
78// return lame_encode_flush(s->gfp, frame, buf_size);
0716b577
J
79
80 /* lame 3.91 dies on '1-channel interleaved' data */
81 if (s->stereo) {
82 num = lame_encode_buffer_interleaved(s->gfp, data,
83 MPA_FRAME_SIZE, frame, buf_size);
84 } else {
85 num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
86 frame, buf_size);
c276af04
MN
87
88/*av_log(avctx, AV_LOG_DEBUG, "in:%d out:%d\n", MPA_FRAME_SIZE, num);
89for(i=0; i<num; i++){
90 av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
91}*/
0716b577
J
92 }
93
0716b577
J
94 return num;
95}
96
97int MP3lame_encode_close(AVCodecContext *avctx)
98{
99 Mp3AudioContext *s = avctx->priv_data;
befaced5
MN
100
101 av_freep(&avctx->coded_frame);
0716b577
J
102
103 lame_close(s->gfp);
104 return 0;
105}
106
107
108AVCodec mp3lame_encoder = {
109 "mp3",
110 CODEC_TYPE_AUDIO,
80783dc2 111 CODEC_ID_MP3,
0716b577
J
112 sizeof(Mp3AudioContext),
113 MP3lame_encode_init,
114 MP3lame_encode_frame,
115 MP3lame_encode_close
116};