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