00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "avcodec.h"
00029 #include "mpegaudio.h"
00030 #include <lame/lame.h>
00031
00032 #define BUFFER_SIZE (7200 + 2*MPA_FRAME_SIZE + MPA_FRAME_SIZE/4)
00033 typedef struct Mp3AudioContext {
00034 lame_global_flags *gfp;
00035 int stereo;
00036 uint8_t buffer[BUFFER_SIZE];
00037 int buffer_index;
00038 struct {
00039 int *left;
00040 int *right;
00041 } s32_data;
00042 } Mp3AudioContext;
00043
00044 static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
00045 {
00046 Mp3AudioContext *s = avctx->priv_data;
00047
00048 if (avctx->channels > 2)
00049 return -1;
00050
00051 s->stereo = avctx->channels > 1 ? 1 : 0;
00052
00053 if ((s->gfp = lame_init()) == NULL)
00054 goto err;
00055 lame_set_in_samplerate(s->gfp, avctx->sample_rate);
00056 lame_set_out_samplerate(s->gfp, avctx->sample_rate);
00057 lame_set_num_channels(s->gfp, avctx->channels);
00058 if(avctx->compression_level == FF_COMPRESSION_DEFAULT) {
00059 lame_set_quality(s->gfp, 5);
00060 } else {
00061 lame_set_quality(s->gfp, avctx->compression_level);
00062 }
00063 lame_set_mode(s->gfp, s->stereo ? JOINT_STEREO : MONO);
00064 lame_set_brate(s->gfp, avctx->bit_rate/1000);
00065 if(avctx->flags & CODEC_FLAG_QSCALE) {
00066 lame_set_brate(s->gfp, 0);
00067 lame_set_VBR(s->gfp, vbr_default);
00068 lame_set_VBR_quality(s->gfp, avctx->global_quality/(float)FF_QP2LAMBDA);
00069 }
00070 lame_set_bWriteVbrTag(s->gfp,0);
00071 lame_set_disable_reservoir(s->gfp, avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR ? 0 : 1);
00072 if (lame_init_params(s->gfp) < 0)
00073 goto err_close;
00074
00075 avctx->frame_size = lame_get_framesize(s->gfp);
00076
00077 if(!(avctx->coded_frame= avcodec_alloc_frame())) {
00078 lame_close(s->gfp);
00079
00080 return AVERROR(ENOMEM);
00081 }
00082 avctx->coded_frame->key_frame= 1;
00083
00084 if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt && s->stereo) {
00085 int nelem = 2 * avctx->frame_size;
00086
00087 if(! (s->s32_data.left = av_malloc(nelem * sizeof(int)))) {
00088 av_freep(&avctx->coded_frame);
00089 lame_close(s->gfp);
00090
00091 return AVERROR(ENOMEM);
00092 }
00093
00094 s->s32_data.right = s->s32_data.left + avctx->frame_size;
00095 }
00096
00097 return 0;
00098
00099 err_close:
00100 lame_close(s->gfp);
00101 err:
00102 return -1;
00103 }
00104
00105 static const int sSampleRates[] = {
00106 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
00107 };
00108
00109 static const int sBitRates[2][3][15] = {
00110 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
00111 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
00112 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
00113 },
00114 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
00115 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
00116 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
00117 },
00118 };
00119
00120 static const int sSamplesPerFrame[2][3] =
00121 {
00122 { 384, 1152, 1152 },
00123 { 384, 1152, 576 }
00124 };
00125
00126 static const int sBitsPerSlot[3] = {
00127 32,
00128 8,
00129 8
00130 };
00131
00132 static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
00133 {
00134 uint32_t header = AV_RB32(data);
00135 int layerID = 3 - ((header >> 17) & 0x03);
00136 int bitRateID = ((header >> 12) & 0x0f);
00137 int sampleRateID = ((header >> 10) & 0x03);
00138 int bitsPerSlot = sBitsPerSlot[layerID];
00139 int isPadded = ((header >> 9) & 0x01);
00140 static int const mode_tab[4]= {2,3,1,0};
00141 int mode= mode_tab[(header >> 19) & 0x03];
00142 int mpeg_id= mode>0;
00143 int temp0, temp1, bitRate;
00144
00145 if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) {
00146 return -1;
00147 }
00148
00149 if(!samplesPerFrame) samplesPerFrame= &temp0;
00150 if(!sampleRate ) sampleRate = &temp1;
00151
00152
00153
00154 *sampleRate = sSampleRates[sampleRateID]>>mode;
00155 bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
00156 *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
00157
00158
00159 return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
00160 }
00161
00162 static int MP3lame_encode_frame(AVCodecContext *avctx,
00163 unsigned char *frame, int buf_size, void *data)
00164 {
00165 Mp3AudioContext *s = avctx->priv_data;
00166 int len;
00167 int lame_result;
00168
00169
00170
00171 if(!data){
00172 lame_result= lame_encode_flush(
00173 s->gfp,
00174 s->buffer + s->buffer_index,
00175 BUFFER_SIZE - s->buffer_index
00176 );
00177 #if 2147483647 == INT_MAX
00178 }else if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt){
00179 if (s->stereo) {
00180 int32_t *rp = data;
00181 int32_t *mp = rp + 2*avctx->frame_size;
00182 int *wpl = s->s32_data.left;
00183 int *wpr = s->s32_data.right;
00184
00185 while (rp < mp) {
00186 *wpl++ = *rp++;
00187 *wpr++ = *rp++;
00188 }
00189
00190 lame_result = lame_encode_buffer_int(
00191 s->gfp,
00192 s->s32_data.left,
00193 s->s32_data.right,
00194 avctx->frame_size,
00195 s->buffer + s->buffer_index,
00196 BUFFER_SIZE - s->buffer_index
00197 );
00198 } else {
00199 lame_result = lame_encode_buffer_int(
00200 s->gfp,
00201 data,
00202 data,
00203 avctx->frame_size,
00204 s->buffer + s->buffer_index,
00205 BUFFER_SIZE - s->buffer_index
00206 );
00207 }
00208 #endif
00209 }else{
00210 if (s->stereo) {
00211 lame_result = lame_encode_buffer_interleaved(
00212 s->gfp,
00213 data,
00214 avctx->frame_size,
00215 s->buffer + s->buffer_index,
00216 BUFFER_SIZE - s->buffer_index
00217 );
00218 } else {
00219 lame_result = lame_encode_buffer(
00220 s->gfp,
00221 data,
00222 data,
00223 avctx->frame_size,
00224 s->buffer + s->buffer_index,
00225 BUFFER_SIZE - s->buffer_index
00226 );
00227 }
00228 }
00229
00230 if(lame_result < 0){
00231 if(lame_result==-1) {
00232
00233 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);
00234 }
00235 return -1;
00236 }
00237
00238 s->buffer_index += lame_result;
00239
00240 if(s->buffer_index<4)
00241 return 0;
00242
00243 len= mp3len(s->buffer, NULL, NULL);
00244
00245 if(len <= s->buffer_index){
00246 memcpy(frame, s->buffer, len);
00247 s->buffer_index -= len;
00248
00249 memmove(s->buffer, s->buffer+len, s->buffer_index);
00250
00251
00252
00253
00254 return len;
00255 }else
00256 return 0;
00257 }
00258
00259 static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
00260 {
00261 Mp3AudioContext *s = avctx->priv_data;
00262
00263 av_freep(&s->s32_data.left);
00264 av_freep(&avctx->coded_frame);
00265
00266 lame_close(s->gfp);
00267 return 0;
00268 }
00269
00270
00271 AVCodec ff_libmp3lame_encoder = {
00272 "libmp3lame",
00273 AVMEDIA_TYPE_AUDIO,
00274 CODEC_ID_MP3,
00275 sizeof(Mp3AudioContext),
00276 MP3lame_encode_init,
00277 MP3lame_encode_frame,
00278 MP3lame_encode_close,
00279 .capabilities= CODEC_CAP_DELAY,
00280 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,
00281 #if 2147483647 == INT_MAX
00282 AV_SAMPLE_FMT_S32,
00283 #endif
00284 AV_SAMPLE_FMT_NONE},
00285 .supported_samplerates= sSampleRates,
00286 .long_name= NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
00287 };