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