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 int reservoir;
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 #if FF_API_LAME_GLOBAL_OPTS
00072 s->reservoir = avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR;
00073 #endif
00074 lame_set_disable_reservoir(s->gfp, !s->reservoir);
00075 if (lame_init_params(s->gfp) < 0)
00076 goto err_close;
00077
00078 avctx->frame_size = lame_get_framesize(s->gfp);
00079 avctx->coded_frame = avcodec_alloc_frame();
00080 avctx->coded_frame->key_frame = 1;
00081
00082 return 0;
00083
00084 err_close:
00085 lame_close(s->gfp);
00086 err:
00087 return -1;
00088 }
00089
00090 static const int sSampleRates[] = {
00091 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
00092 };
00093
00094 static const int sBitRates[2][3][15] = {
00095 {
00096 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
00097 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 },
00098 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 }
00099 },
00100 {
00101 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256 },
00102 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 },
00103 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }
00104 },
00105 };
00106
00107 static const int sSamplesPerFrame[2][3] = {
00108 { 384, 1152, 1152 },
00109 { 384, 1152, 576 }
00110 };
00111
00112 static const int sBitsPerSlot[3] = { 32, 8, 8 };
00113
00114 static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
00115 {
00116 uint32_t header = AV_RB32(data);
00117 int layerID = 3 - ((header >> 17) & 0x03);
00118 int bitRateID = ((header >> 12) & 0x0f);
00119 int sampleRateID = ((header >> 10) & 0x03);
00120 int bitsPerSlot = sBitsPerSlot[layerID];
00121 int isPadded = ((header >> 9) & 0x01);
00122 static int const mode_tab[4] = { 2, 3, 1, 0 };
00123 int mode = mode_tab[(header >> 19) & 0x03];
00124 int mpeg_id = mode > 0;
00125 int temp0, temp1, bitRate;
00126
00127 if (((header >> 21) & 0x7ff) != 0x7ff || mode == 3 || layerID == 3 ||
00128 sampleRateID == 3) {
00129 return -1;
00130 }
00131
00132 if (!samplesPerFrame)
00133 samplesPerFrame = &temp0;
00134 if (!sampleRate)
00135 sampleRate = &temp1;
00136
00137
00138
00139 *sampleRate = sSampleRates[sampleRateID] >> mode;
00140 bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
00141 *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
00142
00143
00144
00145
00146 return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
00147 }
00148
00149 static int MP3lame_encode_frame(AVCodecContext *avctx, unsigned char *frame,
00150 int buf_size, void *data)
00151 {
00152 Mp3AudioContext *s = avctx->priv_data;
00153 int len;
00154 int lame_result;
00155
00156
00157
00158 if (data) {
00159 if (s->stereo) {
00160 lame_result = lame_encode_buffer_interleaved(s->gfp, data,
00161 avctx->frame_size,
00162 s->buffer + s->buffer_index,
00163 BUFFER_SIZE - s->buffer_index);
00164 } else {
00165 lame_result = lame_encode_buffer(s->gfp, data, data,
00166 avctx->frame_size, s->buffer +
00167 s->buffer_index, BUFFER_SIZE -
00168 s->buffer_index);
00169 }
00170 } else {
00171 lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
00172 BUFFER_SIZE - s->buffer_index);
00173 }
00174
00175 if (lame_result < 0) {
00176 if (lame_result == -1) {
00177
00178 av_log(avctx, AV_LOG_ERROR,
00179 "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
00180 s->buffer_index, BUFFER_SIZE - s->buffer_index);
00181 }
00182 return -1;
00183 }
00184
00185 s->buffer_index += lame_result;
00186
00187 if (s->buffer_index < 4)
00188 return 0;
00189
00190 len = mp3len(s->buffer, NULL, NULL);
00191
00192
00193 if (len <= s->buffer_index) {
00194 memcpy(frame, s->buffer, len);
00195 s->buffer_index -= len;
00196
00197 memmove(s->buffer, s->buffer + len, s->buffer_index);
00198
00199
00200
00201
00202 return len;
00203 } else
00204 return 0;
00205 }
00206
00207 static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
00208 {
00209 Mp3AudioContext *s = avctx->priv_data;
00210
00211 av_freep(&avctx->coded_frame);
00212
00213 lame_close(s->gfp);
00214 return 0;
00215 }
00216
00217 #define OFFSET(x) offsetof(Mp3AudioContext, x)
00218 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00219 static const AVOption options[] = {
00220 { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { 1 }, 0, 1, AE },
00221 { NULL },
00222 };
00223
00224 static const AVClass libmp3lame_class = {
00225 .class_name = "libmp3lame encoder",
00226 .item_name = av_default_item_name,
00227 .option = options,
00228 .version = LIBAVUTIL_VERSION_INT,
00229 };
00230
00231 AVCodec ff_libmp3lame_encoder = {
00232 .name = "libmp3lame",
00233 .type = AVMEDIA_TYPE_AUDIO,
00234 .id = CODEC_ID_MP3,
00235 .priv_data_size = sizeof(Mp3AudioContext),
00236 .init = MP3lame_encode_init,
00237 .encode = MP3lame_encode_frame,
00238 .close = MP3lame_encode_close,
00239 .capabilities = CODEC_CAP_DELAY,
00240 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
00241 AV_SAMPLE_FMT_NONE },
00242 .supported_samplerates = sSampleRates,
00243 .long_name = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
00244 .priv_class = &libmp3lame_class,
00245 };