]> git.sesse.net Git - ffmpeg/blob - libavcodec/mp3lameaudio.c
zero copy packet handling for DV1394 by Max Krasnyansky
[ffmpeg] / libavcodec / mp3lameaudio.c
1 /*
2  * Interface to libmp3lame for mp3 encoding
3  * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
4  *
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.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
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
18  */
19
20 #include "avcodec.h"
21 #include "mpegaudio.h"
22 #include <lame/lame.h>
23
24 typedef struct Mp3AudioContext {
25         lame_global_flags *gfp;
26         int stereo;
27 } Mp3AudioContext;
28
29
30 static int MP3lame_encode_init(AVCodecContext *avctx)
31 {
32         Mp3AudioContext *s = avctx->priv_data;
33
34         if (avctx->channels > 2)
35                 return -1;
36
37         s->stereo = avctx->channels > 1 ? 1 : 0;
38
39         if ((s->gfp = lame_init()) == NULL)
40                 goto err;
41         lame_set_in_samplerate(s->gfp, avctx->sample_rate);
42         lame_set_out_samplerate(s->gfp, avctx->sample_rate);
43         lame_set_num_channels(s->gfp, avctx->channels);
44         /* lame 3.91 dies on quality != 5 */
45         lame_set_quality(s->gfp, 5);
46         /* lame 3.91 doesn't work in mono */
47         lame_set_mode(s->gfp, JOINT_STEREO);
48         lame_set_brate(s->gfp, avctx->bit_rate/1000);
49         if (lame_init_params(s->gfp) < 0)
50                 goto err_close;
51
52         avctx->frame_size = MPA_FRAME_SIZE;
53     
54         avctx->coded_frame= avcodec_alloc_frame();
55         avctx->coded_frame->key_frame= 1;
56
57         return 0;
58
59 err_close:
60         lame_close(s->gfp);
61 err:
62         return -1;
63 }
64
65 int MP3lame_encode_frame(AVCodecContext *avctx,
66                      unsigned char *frame, int buf_size, void *data)
67 {
68         Mp3AudioContext *s = avctx->priv_data;
69         int num;
70
71         /* lame 3.91 dies on '1-channel interleaved' data */
72         if (s->stereo) {
73                 num = lame_encode_buffer_interleaved(s->gfp, data,
74                         MPA_FRAME_SIZE, frame, buf_size);
75         } else {
76                 num = lame_encode_buffer(s->gfp, data, data, MPA_FRAME_SIZE,
77                         frame, buf_size);
78         }
79
80         return num;
81 }
82
83 int MP3lame_encode_close(AVCodecContext *avctx)
84 {
85         Mp3AudioContext *s = avctx->priv_data;
86     
87         av_freep(&avctx->coded_frame);
88
89         lame_close(s->gfp);
90         return 0;
91 }
92
93
94 AVCodec mp3lame_encoder = {
95     "mp3",
96     CODEC_TYPE_AUDIO,
97     CODEC_ID_MP3LAME,
98     sizeof(Mp3AudioContext),
99     MP3lame_encode_init,
100     MP3lame_encode_frame,
101     MP3lame_encode_close
102 };