]> git.sesse.net Git - ffmpeg/blob - libavcodec/libmp3lame.c
Add stereo rematrixing support to the AC-3 encoders.
[ffmpeg] / libavcodec / libmp3lame.c
1 /*
2  * Interface to libmp3lame for mp3 encoding
3  * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Interface to libmp3lame for mp3 encoding.
25  */
26
27 #include "avcodec.h"
28 #include "mpegaudio.h"
29 #include <lame/lame.h>
30
31 #define BUFFER_SIZE (7200 + 2*MPA_FRAME_SIZE + MPA_FRAME_SIZE/4)
32 typedef struct Mp3AudioContext {
33     lame_global_flags *gfp;
34     int stereo;
35     uint8_t buffer[BUFFER_SIZE];
36     int buffer_index;
37 } Mp3AudioContext;
38
39 static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
40 {
41     Mp3AudioContext *s = avctx->priv_data;
42
43     if (avctx->channels > 2)
44         return -1;
45
46     s->stereo = avctx->channels > 1 ? 1 : 0;
47
48     if ((s->gfp = lame_init()) == NULL)
49         goto err;
50     lame_set_in_samplerate(s->gfp, avctx->sample_rate);
51     lame_set_out_samplerate(s->gfp, avctx->sample_rate);
52     lame_set_num_channels(s->gfp, avctx->channels);
53     if(avctx->compression_level == FF_COMPRESSION_DEFAULT) {
54         lame_set_quality(s->gfp, 5);
55     } else {
56         lame_set_quality(s->gfp, avctx->compression_level);
57     }
58     lame_set_mode(s->gfp, s->stereo ? JOINT_STEREO : MONO);
59     lame_set_brate(s->gfp, avctx->bit_rate/1000);
60     if(avctx->flags & CODEC_FLAG_QSCALE) {
61         lame_set_brate(s->gfp, 0);
62         lame_set_VBR(s->gfp, vbr_default);
63         lame_set_VBR_quality(s->gfp, avctx->global_quality/(float)FF_QP2LAMBDA);
64     }
65     lame_set_bWriteVbrTag(s->gfp,0);
66     lame_set_disable_reservoir(s->gfp, avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR ? 0 : 1);
67     if (lame_init_params(s->gfp) < 0)
68         goto err_close;
69
70     avctx->frame_size = lame_get_framesize(s->gfp);
71
72     avctx->coded_frame= avcodec_alloc_frame();
73     avctx->coded_frame->key_frame= 1;
74
75     return 0;
76
77 err_close:
78     lame_close(s->gfp);
79 err:
80     return -1;
81 }
82
83 static const int sSampleRates[] = {
84     44100, 48000,  32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
85 };
86
87 static const int sBitRates[2][3][15] = {
88     {   {  0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
89         {  0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
90         {  0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
91     },
92     {   {  0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
93         {  0,  8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
94         {  0,  8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
95     },
96 };
97
98 static const int sSamplesPerFrame[2][3] =
99 {
100     {  384,     1152,    1152 },
101     {  384,     1152,     576 }
102 };
103
104 static const int sBitsPerSlot[3] = {
105     32,
106     8,
107     8
108 };
109
110 static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
111 {
112     uint32_t header = AV_RB32(data);
113     int layerID = 3 - ((header >> 17) & 0x03);
114     int bitRateID = ((header >> 12) & 0x0f);
115     int sampleRateID = ((header >> 10) & 0x03);
116     int bitsPerSlot = sBitsPerSlot[layerID];
117     int isPadded = ((header >> 9) & 0x01);
118     static int const mode_tab[4]= {2,3,1,0};
119     int mode= mode_tab[(header >> 19) & 0x03];
120     int mpeg_id= mode>0;
121     int temp0, temp1, bitRate;
122
123     if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) {
124         return -1;
125     }
126
127     if(!samplesPerFrame) samplesPerFrame= &temp0;
128     if(!sampleRate     ) sampleRate     = &temp1;
129
130 //    *isMono = ((header >>  6) & 0x03) == 0x03;
131
132     *sampleRate = sSampleRates[sampleRateID]>>mode;
133     bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
134     *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
135 //av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
136
137     return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
138 }
139
140 static int MP3lame_encode_frame(AVCodecContext *avctx,
141                                 unsigned char *frame, int buf_size, void *data)
142 {
143     Mp3AudioContext *s = avctx->priv_data;
144     int len;
145     int lame_result;
146
147     /* lame 3.91 dies on '1-channel interleaved' data */
148
149     if(data){
150         if (s->stereo) {
151             lame_result = lame_encode_buffer_interleaved(
152                 s->gfp,
153                 data,
154                 avctx->frame_size,
155                 s->buffer + s->buffer_index,
156                 BUFFER_SIZE - s->buffer_index
157                 );
158         } else {
159             lame_result = lame_encode_buffer(
160                 s->gfp,
161                 data,
162                 data,
163                 avctx->frame_size,
164                 s->buffer + s->buffer_index,
165                 BUFFER_SIZE - s->buffer_index
166                 );
167         }
168     }else{
169         lame_result= lame_encode_flush(
170                 s->gfp,
171                 s->buffer + s->buffer_index,
172                 BUFFER_SIZE - s->buffer_index
173                 );
174     }
175
176     if(lame_result < 0){
177         if(lame_result==-1) {
178             /* output buffer too small */
179             av_log(avctx, AV_LOG_ERROR, "lame: output buffer too small (buffer index: %d, free bytes: %d)\n", s->buffer_index, BUFFER_SIZE - s->buffer_index);
180         }
181         return -1;
182     }
183
184     s->buffer_index += lame_result;
185
186     if(s->buffer_index<4)
187         return 0;
188
189     len= mp3len(s->buffer, NULL, NULL);
190 //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index);
191     if(len <= s->buffer_index){
192         memcpy(frame, s->buffer, len);
193         s->buffer_index -= len;
194
195         memmove(s->buffer, s->buffer+len, s->buffer_index);
196             //FIXME fix the audio codec API, so we do not need the memcpy()
197 /*for(i=0; i<len; i++){
198     av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
199 }*/
200         return len;
201     }else
202         return 0;
203 }
204
205 static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
206 {
207     Mp3AudioContext *s = avctx->priv_data;
208
209     av_freep(&avctx->coded_frame);
210
211     lame_close(s->gfp);
212     return 0;
213 }
214
215
216 AVCodec libmp3lame_encoder = {
217     "libmp3lame",
218     AVMEDIA_TYPE_AUDIO,
219     CODEC_ID_MP3,
220     sizeof(Mp3AudioContext),
221     MP3lame_encode_init,
222     MP3lame_encode_frame,
223     MP3lame_encode_close,
224     .capabilities= CODEC_CAP_DELAY,
225     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
226     .supported_samplerates= sSampleRates,
227     .long_name= NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
228 };