]> git.sesse.net Git - ffmpeg/blob - libavcodec/mp3lameaudio.c
Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine...
[ffmpeg] / libavcodec / mp3lameaudio.c
1 /*
2  * Interface to libmp3lame for mp3 encoding
3  * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "avcodec.h"
21 #include <math.h>
22 #include "mpegaudio.h"
23 #include <lame/lame.h>
24
25 typedef struct Mp3AudioContext {
26         lame_global_flags *gfp;
27         int first_frame;
28         int stereo;
29 } Mp3AudioContext;
30
31
32 static int MP3lame_encode_init(AVCodecContext *avctx)
33 {
34         Mp3AudioContext *s = avctx->priv_data;
35
36         if (avctx->channels > 2)
37                 return -1;
38
39         s->first_frame = 1;
40         s->stereo = avctx->channels > 1 ? 1 : 0;
41
42         if ((s->gfp = lame_init()) == NULL)
43                 goto err;
44         lame_set_in_samplerate(s->gfp, avctx->sample_rate);
45         lame_set_out_samplerate(s->gfp, avctx->sample_rate);
46         lame_set_num_channels(s->gfp, avctx->channels);
47         /* lame 3.91 dies on quality != 5 */
48         lame_set_quality(s->gfp, 5);
49         /* lame 3.91 doesn't work in mono */
50         lame_set_mode(s->gfp, JOINT_STEREO);
51         lame_set_brate(s->gfp, avctx->bit_rate/1000);
52         if (lame_init_params(s->gfp) < 0)
53                 goto err_close;
54
55         avctx->frame_size = MPA_FRAME_SIZE;
56         avctx->key_frame = 1;
57
58         return 0;
59
60 err_close:
61         lame_close(s->gfp);
62 err:
63         return -1;
64 }
65
66 int MP3lame_encode_frame(AVCodecContext *avctx,
67                      unsigned char *frame, int buf_size, void *data)
68 {
69         Mp3AudioContext *s = avctx->priv_data;
70         int num;
71
72         /* lame 3.91 dies on '1-channel interleaved' data */
73         if (s->stereo) {
74                 num = lame_encode_buffer_interleaved(s->gfp, data,
75                         MPA_FRAME_SIZE, frame, buf_size);
76         } else {
77                 num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
78                         frame, buf_size);
79         }
80
81         /* lame 3.91 outputs the first frame as garbage */
82         if (s->first_frame)
83                 s->first_frame = num = 0;
84         return num;
85 }
86
87 int MP3lame_encode_close(AVCodecContext *avctx)
88 {
89         Mp3AudioContext *s = avctx->priv_data;
90
91         lame_close(s->gfp);
92         return 0;
93 }
94
95
96 AVCodec mp3lame_encoder = {
97     "mp3",
98     CODEC_TYPE_AUDIO,
99     CODEC_ID_MP3LAME,
100     sizeof(Mp3AudioContext),
101     MP3lame_encode_init,
102     MP3lame_encode_frame,
103     MP3lame_encode_close
104 };