2012-08-01 17:33:34 +03:00
|
|
|
/*
|
|
|
|
* Interface to libtwolame for mp2 encoding
|
|
|
|
* Copyright (c) 2012 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Interface to libtwolame for mp2 encoding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <twolame.h>
|
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-08-01 17:33:34 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-08-01 19:33:34 +03:00
|
|
|
|
2012-08-01 17:33:34 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "mpegaudio.h"
|
|
|
|
|
|
|
|
typedef struct TWOLAMEContext {
|
2012-08-01 19:33:34 +03:00
|
|
|
AVClass *class;
|
|
|
|
int mode;
|
|
|
|
int psymodel;
|
|
|
|
int energy;
|
|
|
|
int error_protection;
|
|
|
|
int copyright;
|
|
|
|
int original;
|
|
|
|
int verbosity;
|
2012-08-01 17:33:34 +03:00
|
|
|
|
|
|
|
twolame_options *glopts;
|
2012-08-01 19:33:34 +03:00
|
|
|
int64_t next_pts;
|
2012-08-01 17:33:34 +03:00
|
|
|
} TWOLAMEContext;
|
|
|
|
|
|
|
|
static av_cold int twolame_encode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TWOLAMEContext *s = avctx->priv_data;
|
|
|
|
twolame_close(&s->glopts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int twolame_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TWOLAMEContext *s = avctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
|
2014-08-23 15:40:50 +03:00
|
|
|
avctx->initial_padding = 512 - 32 + 1;
|
2012-08-01 17:33:34 +03:00
|
|
|
|
|
|
|
s->glopts = twolame_init();
|
|
|
|
if (!s->glopts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
twolame_set_verbosity(s->glopts, s->verbosity);
|
2012-08-01 17:33:34 +03:00
|
|
|
twolame_set_mode(s->glopts, s->mode);
|
|
|
|
twolame_set_psymodel(s->glopts, s->psymodel);
|
|
|
|
twolame_set_energy_levels(s->glopts, s->energy);
|
|
|
|
twolame_set_error_protection(s->glopts, s->error_protection);
|
2013-05-27 00:57:28 +03:00
|
|
|
twolame_set_copyright(s->glopts, s->copyright);
|
|
|
|
twolame_set_original(s->glopts, s->original);
|
2012-08-01 17:33:34 +03:00
|
|
|
|
|
|
|
twolame_set_num_channels(s->glopts, avctx->channels);
|
|
|
|
twolame_set_in_samplerate(s->glopts, avctx->sample_rate);
|
|
|
|
twolame_set_out_samplerate(s->glopts, avctx->sample_rate);
|
2014-06-21 17:57:23 +03:00
|
|
|
|
|
|
|
if (!avctx->bit_rate)
|
|
|
|
avctx->bit_rate = avctx->sample_rate < 28000 ? 160000 : 384000;
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
|
2012-08-01 17:33:34 +03:00
|
|
|
twolame_set_VBR(s->glopts, TRUE);
|
2012-08-01 19:33:34 +03:00
|
|
|
twolame_set_VBR_level(s->glopts,
|
|
|
|
avctx->global_quality / (float) FF_QP2LAMBDA);
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"VBR in MP2 is a hack, use another codec that supports it.\n");
|
2012-08-01 17:33:34 +03:00
|
|
|
} else {
|
|
|
|
twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
ret = twolame_init_params(s->glopts);
|
|
|
|
if (ret) {
|
|
|
|
twolame_encode_close(avctx);
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
2012-08-01 17:33:34 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
|
|
|
const AVFrame *frame, int *got_packet_ptr)
|
|
|
|
{
|
|
|
|
TWOLAMEContext *s = avctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
2015-07-27 19:36:16 +02:00
|
|
|
if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE, 0)) < 0)
|
2012-08-01 17:33:34 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
switch (avctx->sample_fmt) {
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
|
|
|
ret = twolame_encode_buffer_float32_interleaved(s->glopts,
|
2012-08-01 19:33:34 +03:00
|
|
|
(const float *)frame->data[0],
|
|
|
|
frame->nb_samples,
|
|
|
|
avpkt->data,
|
|
|
|
avpkt->size);
|
2012-08-01 17:33:34 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
ret = twolame_encode_buffer_float32(s->glopts,
|
2012-08-01 19:33:34 +03:00
|
|
|
(const float *)frame->data[0],
|
|
|
|
(const float *)frame->data[1],
|
|
|
|
frame->nb_samples,
|
|
|
|
avpkt->data, avpkt->size);
|
2012-08-01 17:33:34 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
|
|
|
ret = twolame_encode_buffer_interleaved(s->glopts,
|
2012-08-01 19:33:34 +03:00
|
|
|
(const short int *)frame->data[0],
|
|
|
|
frame->nb_samples,
|
|
|
|
avpkt->data, avpkt->size);
|
2012-08-01 17:33:34 +03:00
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16P:
|
|
|
|
ret = twolame_encode_buffer(s->glopts,
|
2012-08-01 19:33:34 +03:00
|
|
|
(const short int *)frame->data[0],
|
|
|
|
(const short int *)frame->data[1],
|
2012-08-01 17:33:34 +03:00
|
|
|
frame->nb_samples,
|
|
|
|
avpkt->data, avpkt->size);
|
|
|
|
break;
|
|
|
|
default:
|
2012-08-01 19:33:34 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Unsupported sample format %d.\n", avctx->sample_fmt);
|
2012-08-01 17:33:34 +03:00
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
if (!ret) // no bytes written
|
2012-08-01 17:33:34 +03:00
|
|
|
return 0;
|
2012-08-01 19:33:34 +03:00
|
|
|
if (ret < 0) // twolame error
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
|
|
|
|
if (frame) {
|
2014-10-13 15:22:41 +03:00
|
|
|
avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
|
2012-08-01 19:33:34 +03:00
|
|
|
if (frame->pts != AV_NOPTS_VALUE)
|
2014-08-23 15:40:50 +03:00
|
|
|
avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
|
2012-08-01 19:33:34 +03:00
|
|
|
} else {
|
|
|
|
avpkt->pts = s->next_pts;
|
2012-08-01 17:33:34 +03:00
|
|
|
}
|
2012-08-01 19:33:34 +03:00
|
|
|
// this is for setting pts for flushed packet(s).
|
|
|
|
if (avpkt->pts != AV_NOPTS_VALUE)
|
|
|
|
s->next_pts = avpkt->pts + avpkt->duration;
|
2012-08-01 17:33:34 +03:00
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
av_shrink_packet(avpkt, ret);
|
|
|
|
*got_packet_ptr = 1;
|
|
|
|
return 0;
|
2012-08-01 17:33:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(TWOLAMEContext, x)
|
|
|
|
#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2012-08-01 19:33:34 +03:00
|
|
|
{ "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, "mode"},
|
|
|
|
{ "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, "mode" },
|
|
|
|
{ "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, "mode" },
|
|
|
|
{ "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, "mode" },
|
|
|
|
{ "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, "mode" },
|
|
|
|
{ "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, "mode" },
|
|
|
|
{ "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
|
|
|
|
{ "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
|
2012-09-05 15:26:01 +03:00
|
|
|
{ "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
|
2012-08-01 19:33:34 +03:00
|
|
|
{ "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
|
|
|
|
{ "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
|
|
|
|
{ "verbosity", "set library optput level (0-10)", OFFSET(verbosity), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, AE},
|
2012-08-01 17:33:34 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
static const AVClass twolame_class = {
|
2012-08-01 17:33:34 +03:00
|
|
|
.class_name = "libtwolame encoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2012-08-01 19:33:34 +03:00
|
|
|
static const AVCodecDefault twolame_defaults[] = {
|
2014-06-21 17:57:23 +03:00
|
|
|
{ "b", "0" },
|
2012-08-01 19:33:34 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int twolame_samplerates[] = {
|
|
|
|
16000, 22050, 24000, 32000, 44100, 48000, 0
|
|
|
|
};
|
|
|
|
|
2012-08-01 17:33:34 +03:00
|
|
|
AVCodec ff_libtwolame_encoder = {
|
2012-08-01 19:33:34 +03:00
|
|
|
.name = "libtwolame",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libtwolame MP2 (MPEG audio layer 2)"),
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.id = AV_CODEC_ID_MP2,
|
|
|
|
.priv_data_size = sizeof(TWOLAMEContext),
|
|
|
|
.init = twolame_encode_init,
|
|
|
|
.encode2 = twolame_encode_frame,
|
|
|
|
.close = twolame_encode_close,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DELAY,
|
2012-08-01 19:33:34 +03:00
|
|
|
.defaults = twolame_defaults,
|
|
|
|
.priv_class = &twolame_class,
|
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) {
|
|
|
|
AV_SAMPLE_FMT_FLT,
|
|
|
|
AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
AV_SAMPLE_FMT_S16P,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
},
|
|
|
|
.channel_layouts = (const uint64_t[]) {
|
|
|
|
AV_CH_LAYOUT_MONO,
|
|
|
|
AV_CH_LAYOUT_STEREO,
|
|
|
|
0 },
|
|
|
|
.supported_samplerates = twolame_samplerates,
|
2012-08-01 17:33:34 +03:00
|
|
|
};
|