FFmpeg
libtwolame.c
Go to the documentation of this file.
1 /*
2  * Interface to libtwolame for mp2 encoding
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * Interface to libtwolame for mp2 encoding.
25  */
26 
27 #include <twolame.h>
28 
30 #include "libavutil/common.h"
31 #include "libavutil/opt.h"
32 
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "encode.h"
36 #include "mpegaudio.h"
37 
38 typedef struct TWOLAMEContext {
39  AVClass *class;
40  int mode;
41  int psymodel;
42  int energy;
44  int copyright;
45  int original;
46  int verbosity;
47 
48  twolame_options *glopts;
49  int64_t next_pts;
51 
53 {
54  TWOLAMEContext *s = avctx->priv_data;
55  twolame_close(&s->glopts);
56  return 0;
57 }
58 
60 {
61  TWOLAMEContext *s = avctx->priv_data;
62  int ret;
63 
64  avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
65  avctx->initial_padding = 512 - 32 + 1;
66 
67  s->glopts = twolame_init();
68  if (!s->glopts)
69  return AVERROR(ENOMEM);
70 
71  twolame_set_verbosity(s->glopts, s->verbosity);
72  twolame_set_mode(s->glopts, s->mode);
73  twolame_set_psymodel(s->glopts, s->psymodel);
74  twolame_set_energy_levels(s->glopts, s->energy);
75  twolame_set_error_protection(s->glopts, s->error_protection);
76  twolame_set_copyright(s->glopts, s->copyright);
77  twolame_set_original(s->glopts, s->original);
78 
79  twolame_set_num_channels(s->glopts, avctx->ch_layout.nb_channels);
80  twolame_set_in_samplerate(s->glopts, avctx->sample_rate);
81  twolame_set_out_samplerate(s->glopts, avctx->sample_rate);
82 
83  if (!avctx->bit_rate) {
84  if ((s->mode == TWOLAME_AUTO_MODE && avctx->ch_layout.nb_channels == 1) || s->mode == TWOLAME_MONO)
85  avctx->bit_rate = avctx->sample_rate < 28000 ? 80000 : 192000;
86  else
87  avctx->bit_rate = avctx->sample_rate < 28000 ? 160000 : 384000;
88  }
89 
90  if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
91  twolame_set_VBR(s->glopts, TRUE);
92  twolame_set_VBR_level(s->glopts,
93  avctx->global_quality / (float) FF_QP2LAMBDA);
94  av_log(avctx, AV_LOG_WARNING,
95  "VBR in MP2 is a hack, use another codec that supports it.\n");
96  } else {
97  twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
98  }
99 
100  ret = twolame_init_params(s->glopts);
101  if (ret) {
102  twolame_encode_close(avctx);
103  return AVERROR_UNKNOWN;
104  }
105 
106  return 0;
107 }
108 
109 static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
110  const AVFrame *frame, int *got_packet_ptr)
111 {
112  TWOLAMEContext *s = avctx->priv_data;
113  int ret;
114 
115  if ((ret = ff_alloc_packet(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
116  return ret;
117 
118  if (frame) {
119  switch (avctx->sample_fmt) {
120  case AV_SAMPLE_FMT_FLT:
121  ret = twolame_encode_buffer_float32_interleaved(s->glopts,
122  (const float *)frame->data[0],
123  frame->nb_samples,
124  avpkt->data,
125  avpkt->size);
126  break;
127  case AV_SAMPLE_FMT_FLTP:
128  ret = twolame_encode_buffer_float32(s->glopts,
129  (const float *)frame->data[0],
130  (const float *)frame->data[1],
131  frame->nb_samples,
132  avpkt->data, avpkt->size);
133  break;
134  case AV_SAMPLE_FMT_S16:
135  ret = twolame_encode_buffer_interleaved(s->glopts,
136  (const short int *)frame->data[0],
137  frame->nb_samples,
138  avpkt->data, avpkt->size);
139  break;
140  case AV_SAMPLE_FMT_S16P:
141  ret = twolame_encode_buffer(s->glopts,
142  (const short int *)frame->data[0],
143  (const short int *)frame->data[1],
144  frame->nb_samples,
145  avpkt->data, avpkt->size);
146  break;
147  default:
148  av_log(avctx, AV_LOG_ERROR,
149  "Unsupported sample format %d.\n", avctx->sample_fmt);
150  return AVERROR_BUG;
151  }
152  } else {
153  ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
154  }
155 
156  if (!ret) // no bytes written
157  return 0;
158  if (ret < 0) // twolame error
159  return AVERROR_UNKNOWN;
160 
161  if (frame) {
163  if (frame->pts != AV_NOPTS_VALUE)
164  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
165  } else {
166  avpkt->pts = s->next_pts;
167  }
168  // this is for setting pts for flushed packet(s).
169  if (avpkt->pts != AV_NOPTS_VALUE)
170  s->next_pts = avpkt->pts + avpkt->duration;
171 
172  av_shrink_packet(avpkt, ret);
173  *got_packet_ptr = 1;
174  return 0;
175 }
176 
177 #define OFFSET(x) offsetof(TWOLAMEContext, x)
178 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
179 static const AVOption options[] = {
180  { "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, .unit = "mode"},
181  { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, .unit = "mode" },
182  { "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, .unit = "mode" },
183  { "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, .unit = "mode" },
184  { "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, .unit = "mode" },
185  { "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, .unit = "mode" },
186  { "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
187  { "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
188  { "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
189  { "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
190  { "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
191  { "verbosity", "set library optput level (0-10)", OFFSET(verbosity), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, AE},
192  { NULL },
193 };
194 
195 static const AVClass twolame_class = {
196  .class_name = "libtwolame encoder",
197  .item_name = av_default_item_name,
198  .option = options,
199  .version = LIBAVUTIL_VERSION_INT,
200 };
201 
203  { "b", "0" },
204  { NULL },
205 };
206 
207 static const int twolame_samplerates[] = {
208  16000, 22050, 24000, 32000, 44100, 48000, 0
209 };
210 
212  .p.name = "libtwolame",
213  CODEC_LONG_NAME("libtwolame MP2 (MPEG audio layer 2)"),
214  .p.type = AVMEDIA_TYPE_AUDIO,
215  .p.id = AV_CODEC_ID_MP2,
216  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
217  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
218  .priv_data_size = sizeof(TWOLAMEContext),
221  .close = twolame_encode_close,
222  .defaults = twolame_defaults,
223  .p.priv_class = &twolame_class,
224  .p.sample_fmts = (const enum AVSampleFormat[]) {
230  },
231  .p.ch_layouts = (const AVChannelLayout[]) {
234  { 0 },
235  },
236  .p.supported_samplerates = twolame_samplerates,
237  .p.wrapper_name = "libtwolame",
238 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1077
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
twolame_encode_close
static av_cold int twolame_encode_close(AVCodecContext *avctx)
Definition: libtwolame.c:52
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:224
TWOLAMEContext::error_protection
int error_protection
Definition: libtwolame.c:43
ff_libtwolame_encoder
const FFCodec ff_libtwolame_encoder
Definition: libtwolame.c:211
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:487
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
encode.h
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:396
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
TWOLAMEContext::psymodel
int psymodel
Definition: libtwolame.c:41
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:113
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1122
TWOLAMEContext::copyright
int copyright
Definition: libtwolame.c:44
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
twolame_encode_init
static av_cold int twolame_encode_init(AVCodecContext *avctx)
Definition: libtwolame.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1239
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:440
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ff_samples_to_time_base
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: encode.h:90
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
twolame_encode_frame
static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libtwolame.c:109
twolame_class
static const AVClass twolame_class
Definition: libtwolame.c:195
TWOLAMEContext::energy
int energy
Definition: libtwolame.c:42
TWOLAMEContext::original
int original
Definition: libtwolame.c:45
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
TWOLAMEContext::mode
int mode
Definition: libtwolame.c:40
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
options
static const AVOption options[]
Definition: libtwolame.c:179
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:455
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
TWOLAMEContext
Definition: libtwolame.c:38
OFFSET
#define OFFSET(x)
Definition: libtwolame.c:177
twolame_defaults
static const FFCodecDefault twolame_defaults[]
Definition: libtwolame.c:202
mpegaudio.h
avcodec.h
TWOLAMEContext::glopts
twolame_options * glopts
Definition: libtwolame.c:48
TWOLAMEContext::next_pts
int64_t next_pts
Definition: libtwolame.c:49
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AE
#define AE
Definition: libtwolame.c:178
AVCodecContext
main external API structure.
Definition: avcodec.h:445
twolame_samplerates
static const int twolame_samplerates[]
Definition: libtwolame.c:207
channel_layout.h
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MPA_MAX_CODED_FRAME_SIZE
#define MPA_MAX_CODED_FRAME_SIZE
Definition: mpegaudio.h:40
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:61
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
TWOLAMEContext::verbosity
int verbosity
Definition: libtwolame.c:46