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