00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <lame/lame.h>
00028
00029 #include "libavutil/channel_layout.h"
00030 #include "libavutil/common.h"
00031 #include "libavutil/float_dsp.h"
00032 #include "libavutil/intreadwrite.h"
00033 #include "libavutil/log.h"
00034 #include "libavutil/opt.h"
00035 #include "avcodec.h"
00036 #include "audio_frame_queue.h"
00037 #include "internal.h"
00038 #include "mpegaudio.h"
00039 #include "mpegaudiodecheader.h"
00040
00041 #define BUFFER_SIZE (7200 + 2 * MPA_FRAME_SIZE + MPA_FRAME_SIZE / 4+1000) // FIXME: Buffer size to small? Adding 1000 to make up for it.
00042
00043 typedef struct LAMEContext {
00044 AVClass *class;
00045 AVCodecContext *avctx;
00046 lame_global_flags *gfp;
00047 uint8_t *buffer;
00048 int buffer_index;
00049 int buffer_size;
00050 int reservoir;
00051 float *samples_flt[2];
00052 AudioFrameQueue afq;
00053 AVFloatDSPContext fdsp;
00054 } LAMEContext;
00055
00056
00057 static int realloc_buffer(LAMEContext *s)
00058 {
00059 if (!s->buffer || s->buffer_size - s->buffer_index < BUFFER_SIZE) {
00060 uint8_t *tmp;
00061 int new_size = s->buffer_index + 2 * BUFFER_SIZE;
00062
00063 av_dlog(s->avctx, "resizing output buffer: %d -> %d\n", s->buffer_size,
00064 new_size);
00065 tmp = av_realloc(s->buffer, new_size);
00066 if (!tmp) {
00067 av_freep(&s->buffer);
00068 s->buffer_size = s->buffer_index = 0;
00069 return AVERROR(ENOMEM);
00070 }
00071 s->buffer = tmp;
00072 s->buffer_size = new_size;
00073 }
00074 return 0;
00075 }
00076
00077 static av_cold int mp3lame_encode_close(AVCodecContext *avctx)
00078 {
00079 LAMEContext *s = avctx->priv_data;
00080
00081 #if FF_API_OLD_ENCODE_AUDIO
00082 av_freep(&avctx->coded_frame);
00083 #endif
00084 av_freep(&s->samples_flt[0]);
00085 av_freep(&s->samples_flt[1]);
00086 av_freep(&s->buffer);
00087
00088 ff_af_queue_close(&s->afq);
00089
00090 lame_close(s->gfp);
00091 return 0;
00092 }
00093
00094 static av_cold int mp3lame_encode_init(AVCodecContext *avctx)
00095 {
00096 LAMEContext *s = avctx->priv_data;
00097 int ret;
00098
00099 s->avctx = avctx;
00100
00101
00102 if ((s->gfp = lame_init()) == NULL)
00103 return AVERROR(ENOMEM);
00104
00105
00106 lame_set_num_channels(s->gfp, avctx->channels);
00107 lame_set_mode(s->gfp, avctx->channels > 1 ? JOINT_STEREO : MONO);
00108
00109
00110 lame_set_in_samplerate (s->gfp, avctx->sample_rate);
00111 lame_set_out_samplerate(s->gfp, avctx->sample_rate);
00112
00113
00114 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
00115 lame_set_quality(s->gfp, 5);
00116 else
00117 lame_set_quality(s->gfp, avctx->compression_level);
00118
00119
00120 if (avctx->flags & CODEC_FLAG_QSCALE) {
00121 lame_set_VBR(s->gfp, vbr_default);
00122 lame_set_VBR_quality(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
00123 } else {
00124 if (avctx->bit_rate)
00125 lame_set_brate(s->gfp, avctx->bit_rate / 1000);
00126 }
00127
00128
00129 lame_set_bWriteVbrTag(s->gfp,0);
00130
00131
00132 lame_set_disable_reservoir(s->gfp, !s->reservoir);
00133
00134
00135 if (lame_init_params(s->gfp) < 0) {
00136 ret = -1;
00137 goto error;
00138 }
00139
00140
00141 avctx->delay = lame_get_encoder_delay(s->gfp) + 528 + 1;
00142 ff_af_queue_init(avctx, &s->afq);
00143
00144 avctx->frame_size = lame_get_framesize(s->gfp);
00145
00146 #if FF_API_OLD_ENCODE_AUDIO
00147 avctx->coded_frame = avcodec_alloc_frame();
00148 if (!avctx->coded_frame) {
00149 ret = AVERROR(ENOMEM);
00150 goto error;
00151 }
00152 #endif
00153
00154
00155 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
00156 int ch;
00157 for (ch = 0; ch < avctx->channels; ch++) {
00158 s->samples_flt[ch] = av_malloc(avctx->frame_size *
00159 sizeof(*s->samples_flt[ch]));
00160 if (!s->samples_flt[ch]) {
00161 ret = AVERROR(ENOMEM);
00162 goto error;
00163 }
00164 }
00165 }
00166
00167 ret = realloc_buffer(s);
00168 if (ret < 0)
00169 goto error;
00170
00171 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
00172
00173 return 0;
00174 error:
00175 mp3lame_encode_close(avctx);
00176 return ret;
00177 }
00178
00179 #define ENCODE_BUFFER(func, buf_type, buf_name) do { \
00180 lame_result = func(s->gfp, \
00181 (const buf_type *)buf_name[0], \
00182 (const buf_type *)buf_name[1], frame->nb_samples, \
00183 s->buffer + s->buffer_index, \
00184 s->buffer_size - s->buffer_index); \
00185 } while (0)
00186
00187 static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00188 const AVFrame *frame, int *got_packet_ptr)
00189 {
00190 LAMEContext *s = avctx->priv_data;
00191 MPADecodeHeader hdr;
00192 int len, ret, ch;
00193 int lame_result;
00194
00195 if (frame) {
00196 switch (avctx->sample_fmt) {
00197 case AV_SAMPLE_FMT_S16P:
00198 ENCODE_BUFFER(lame_encode_buffer, int16_t, frame->data);
00199 break;
00200 case AV_SAMPLE_FMT_S32P:
00201 ENCODE_BUFFER(lame_encode_buffer_int, int32_t, frame->data);
00202 break;
00203 case AV_SAMPLE_FMT_FLTP:
00204 if (frame->linesize[0] < 4 * FFALIGN(frame->nb_samples, 8)) {
00205 av_log(avctx, AV_LOG_ERROR, "inadequate AVFrame plane padding\n");
00206 return AVERROR(EINVAL);
00207 }
00208 for (ch = 0; ch < avctx->channels; ch++) {
00209 s->fdsp.vector_fmul_scalar(s->samples_flt[ch],
00210 (const float *)frame->data[ch],
00211 32768.0f,
00212 FFALIGN(frame->nb_samples, 8));
00213 }
00214 ENCODE_BUFFER(lame_encode_buffer_float, float, s->samples_flt);
00215 break;
00216 default:
00217 return AVERROR_BUG;
00218 }
00219 } else {
00220 lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
00221 BUFFER_SIZE - s->buffer_index);
00222 }
00223 if (lame_result < 0) {
00224 if (lame_result == -1) {
00225 av_log(avctx, AV_LOG_ERROR,
00226 "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
00227 s->buffer_index, s->buffer_size - s->buffer_index);
00228 }
00229 return -1;
00230 }
00231 s->buffer_index += lame_result;
00232 ret = realloc_buffer(s);
00233 if (ret < 0) {
00234 av_log(avctx, AV_LOG_ERROR, "error reallocating output buffer\n");
00235 return ret;
00236 }
00237
00238
00239 if (frame) {
00240 if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
00241 return ret;
00242 }
00243
00244
00245
00246
00247 if (s->buffer_index < 4)
00248 return 0;
00249 if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
00250 av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
00251 return -1;
00252 }
00253 len = hdr.frame_size;
00254 av_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
00255 s->buffer_index);
00256 if (len <= s->buffer_index) {
00257 if ((ret = ff_alloc_packet2(avctx, avpkt, len)))
00258 return ret;
00259 memcpy(avpkt->data, s->buffer, len);
00260 s->buffer_index -= len;
00261 memmove(s->buffer, s->buffer + len, s->buffer_index);
00262
00263
00264 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
00265 &avpkt->duration);
00266
00267 avpkt->size = len;
00268 *got_packet_ptr = 1;
00269 }
00270 return 0;
00271 }
00272
00273 #define OFFSET(x) offsetof(LAMEContext, x)
00274 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00275 static const AVOption options[] = {
00276 { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
00277 { NULL },
00278 };
00279
00280 static const AVClass libmp3lame_class = {
00281 .class_name = "libmp3lame encoder",
00282 .item_name = av_default_item_name,
00283 .option = options,
00284 .version = LIBAVUTIL_VERSION_INT,
00285 };
00286
00287 static const AVCodecDefault libmp3lame_defaults[] = {
00288 { "b", "0" },
00289 { NULL },
00290 };
00291
00292 static const int libmp3lame_sample_rates[] = {
00293 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
00294 };
00295
00296 AVCodec ff_libmp3lame_encoder = {
00297 .name = "libmp3lame",
00298 .type = AVMEDIA_TYPE_AUDIO,
00299 .id = AV_CODEC_ID_MP3,
00300 .priv_data_size = sizeof(LAMEContext),
00301 .init = mp3lame_encode_init,
00302 .encode2 = mp3lame_encode_frame,
00303 .close = mp3lame_encode_close,
00304 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
00305 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
00306 AV_SAMPLE_FMT_FLTP,
00307 AV_SAMPLE_FMT_S16P,
00308 AV_SAMPLE_FMT_NONE },
00309 .supported_samplerates = libmp3lame_sample_rates,
00310 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
00311 AV_CH_LAYOUT_STEREO,
00312 0 },
00313 .long_name = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
00314 .priv_class = &libmp3lame_class,
00315 .defaults = libmp3lame_defaults,
00316 };