2 * Interface to libmp3lame for mp3 encoding
3 * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Interface to libmp3lame for mp3 encoding.
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/log.h"
29 #include "libavutil/opt.h"
32 #include "mpegaudio.h"
33 #include "mpegaudiodecheader.h"
34 #include <lame/lame.h>
36 #define BUFFER_SIZE (7200 + 2 * MPA_FRAME_SIZE + MPA_FRAME_SIZE / 4)
37 typedef struct Mp3AudioContext {
39 lame_global_flags *gfp;
40 uint8_t buffer[BUFFER_SIZE];
46 static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
48 Mp3AudioContext *s = avctx->priv_data;
50 av_freep(&avctx->coded_frame);
56 static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
58 Mp3AudioContext *s = avctx->priv_data;
61 if (avctx->channels > 2)
62 return AVERROR(EINVAL);
64 if ((s->gfp = lame_init()) == NULL)
65 return AVERROR(ENOMEM);
66 lame_set_in_samplerate(s->gfp, avctx->sample_rate);
67 lame_set_out_samplerate(s->gfp, avctx->sample_rate);
68 lame_set_num_channels(s->gfp, avctx->channels);
69 if (avctx->compression_level == FF_COMPRESSION_DEFAULT) {
70 lame_set_quality(s->gfp, 5);
72 lame_set_quality(s->gfp, avctx->compression_level);
74 lame_set_mode(s->gfp, avctx->channels > 1 ? JOINT_STEREO : MONO);
75 if (avctx->flags & CODEC_FLAG_QSCALE) {
76 lame_set_VBR(s->gfp, vbr_default);
77 lame_set_VBR_quality(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
80 lame_set_brate(s->gfp, avctx->bit_rate / 1000);
82 lame_set_bWriteVbrTag(s->gfp,0);
83 lame_set_disable_reservoir(s->gfp, !s->reservoir);
84 if (lame_init_params(s->gfp) < 0) {
89 avctx->frame_size = lame_get_framesize(s->gfp);
90 avctx->coded_frame = avcodec_alloc_frame();
91 if (!avctx->coded_frame) {
92 ret = AVERROR(ENOMEM);
98 MP3lame_encode_close(avctx);
102 static const int sSampleRates[] = {
103 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
106 static int MP3lame_encode_frame(AVCodecContext *avctx, unsigned char *frame,
107 int buf_size, void *data)
109 Mp3AudioContext *s = avctx->priv_data;
115 if (avctx->channels > 1) {
116 lame_result = lame_encode_buffer_interleaved(s->gfp, data,
118 s->buffer + s->buffer_index,
119 BUFFER_SIZE - s->buffer_index);
121 lame_result = lame_encode_buffer(s->gfp, data, data,
122 avctx->frame_size, s->buffer +
123 s->buffer_index, BUFFER_SIZE -
127 lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
128 BUFFER_SIZE - s->buffer_index);
131 if (lame_result < 0) {
132 if (lame_result == -1) {
133 av_log(avctx, AV_LOG_ERROR,
134 "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
135 s->buffer_index, BUFFER_SIZE - s->buffer_index);
140 s->buffer_index += lame_result;
142 if (s->buffer_index < 4)
145 if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
146 av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
149 len = hdr.frame_size;
150 av_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
152 if (len <= s->buffer_index) {
153 memcpy(frame, s->buffer, len);
154 s->buffer_index -= len;
156 memmove(s->buffer, s->buffer + len, s->buffer_index);
162 #define OFFSET(x) offsetof(Mp3AudioContext, x)
163 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
164 static const AVOption options[] = {
165 { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { 1 }, 0, 1, AE },
169 static const AVClass libmp3lame_class = {
170 .class_name = "libmp3lame encoder",
171 .item_name = av_default_item_name,
173 .version = LIBAVUTIL_VERSION_INT,
176 static const AVCodecDefault libmp3lame_defaults[] = {
181 AVCodec ff_libmp3lame_encoder = {
182 .name = "libmp3lame",
183 .type = AVMEDIA_TYPE_AUDIO,
185 .priv_data_size = sizeof(Mp3AudioContext),
186 .init = MP3lame_encode_init,
187 .encode = MP3lame_encode_frame,
188 .close = MP3lame_encode_close,
189 .capabilities = CODEC_CAP_DELAY,
190 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
191 AV_SAMPLE_FMT_NONE },
192 .supported_samplerates = sSampleRates,
193 .long_name = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
194 .priv_class = &libmp3lame_class,
195 .defaults = libmp3lame_defaults,