2 * Interface to libtwolame for mp2 encoding
3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Interface to libtwolame for mp2 encoding.
29 #include "libavutil/opt.h"
32 #include "mpegaudio.h"
34 typedef struct TWOLAMEContext {
43 twolame_options *glopts;
47 static av_cold int twolame_encode_close(AVCodecContext *avctx)
49 TWOLAMEContext *s = avctx->priv_data;
50 twolame_close(&s->glopts);
54 static av_cold int twolame_encode_init(AVCodecContext *avctx)
56 TWOLAMEContext *s = avctx->priv_data;
59 avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
61 s->glopts = twolame_init();
63 return AVERROR(ENOMEM);
65 twolame_set_verbosity(s->glopts, 0);
66 twolame_set_mode(s->glopts, s->mode);
67 twolame_set_psymodel(s->glopts, s->psymodel);
68 twolame_set_energy_levels(s->glopts, s->energy);
69 twolame_set_error_protection(s->glopts, s->error_protection);
70 twolame_set_copyright(s->glopts, s->copyright);
71 twolame_set_original(s->glopts, s->original);
73 twolame_set_num_channels(s->glopts, avctx->channels);
74 twolame_set_in_samplerate(s->glopts, avctx->sample_rate);
75 twolame_set_out_samplerate(s->glopts, avctx->sample_rate);
76 if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
77 twolame_set_VBR(s->glopts, TRUE);
78 twolame_set_VBR_level(s->glopts, avctx->global_quality);
79 av_log(avctx, AV_LOG_WARNING, "VBR mode is experimental!\n");
81 twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
84 if ((ret = twolame_init_params(s->glopts)))
89 twolame_encode_close(avctx);
93 static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
94 const AVFrame *frame, int *got_packet_ptr)
96 TWOLAMEContext *s = avctx->priv_data;
99 if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
103 switch (avctx->sample_fmt) {
104 case AV_SAMPLE_FMT_FLT:
105 ret = twolame_encode_buffer_float32_interleaved(s->glopts,
108 avpkt->data, avpkt->size);
110 case AV_SAMPLE_FMT_FLTP:
111 ret = twolame_encode_buffer_float32(s->glopts,
112 frame->data[0], frame->data[1],
114 avpkt->data, avpkt->size);
116 case AV_SAMPLE_FMT_S16:
117 ret = twolame_encode_buffer_interleaved(s->glopts,
120 avpkt->data, avpkt->size);
122 case AV_SAMPLE_FMT_S16P:
123 ret = twolame_encode_buffer(s->glopts,
124 frame->data[0], frame->data[1],
126 avpkt->data, avpkt->size);
132 ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
136 avpkt->duration = ff_samples_to_time_base(avctx, avctx->frame_size);
138 if (frame->pts != AV_NOPTS_VALUE)
139 avpkt->pts = frame->pts;
141 avpkt->pts = s->next_pts;
143 if (avpkt->pts != AV_NOPTS_VALUE)
144 s->next_pts = avpkt->pts + avpkt->duration;
154 #define OFFSET(x) offsetof(TWOLAMEContext, x)
155 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
156 static const AVOption options[] = {
157 { "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, "mode"},
158 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, "mode" },
159 { "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, "mode" },
160 { "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, "mode" },
161 { "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, "mode" },
162 { "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, "mode" },
163 { "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
164 { "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
165 { "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
166 { "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
167 { "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
171 static const AVClass libtwolame_class = {
172 .class_name = "libtwolame encoder",
173 .item_name = av_default_item_name,
175 .version = LIBAVUTIL_VERSION_INT,
178 AVCodec ff_libtwolame_encoder = {
179 .name = "libtwolame",
180 .long_name = NULL_IF_CONFIG_SMALL("libtwolame MP2 (MPEG audio layer 2)"),
181 .type = AVMEDIA_TYPE_AUDIO,
182 .id = AV_CODEC_ID_MP2,
183 .priv_data_size = sizeof(TWOLAMEContext),
184 .init = twolame_encode_init,
185 .encode2 = twolame_encode_frame,
186 .close = twolame_encode_close,
187 .capabilities = CODEC_CAP_DELAY,
188 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
192 AV_SAMPLE_FMT_NONE },
193 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
196 .supported_samplerates = (const int[]){ 16000, 22050, 24000, 32000, 44100, 48000, 0 },
197 .priv_class = &libtwolame_class,