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 
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 
32 #include "avcodec.h"
33 #include "internal.h"
34 #include "mpegaudio.h"
35 
36 typedef struct TWOLAMEContext {
37  AVClass *class;
38  int mode;
39  int psymodel;
40  int energy;
42  int copyright;
43  int original;
44  int verbosity;
45 
46  twolame_options *glopts;
47  int64_t next_pts;
49 
51 {
52  TWOLAMEContext *s = avctx->priv_data;
53  twolame_close(&s->glopts);
54  return 0;
55 }
56 
58 {
59  TWOLAMEContext *s = avctx->priv_data;
60  int ret;
61 
62  avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
63  avctx->initial_padding = 512 - 32 + 1;
64 
65  s->glopts = twolame_init();
66  if (!s->glopts)
67  return AVERROR(ENOMEM);
68 
69  twolame_set_verbosity(s->glopts, s->verbosity);
70  twolame_set_mode(s->glopts, s->mode);
71  twolame_set_psymodel(s->glopts, s->psymodel);
72  twolame_set_energy_levels(s->glopts, s->energy);
73  twolame_set_error_protection(s->glopts, s->error_protection);
74  twolame_set_copyright(s->glopts, s->copyright);
75  twolame_set_original(s->glopts, s->original);
76 
77  twolame_set_num_channels(s->glopts, avctx->channels);
78  twolame_set_in_samplerate(s->glopts, avctx->sample_rate);
79  twolame_set_out_samplerate(s->glopts, avctx->sample_rate);
80 
81  if (!avctx->bit_rate) {
82  if ((s->mode == TWOLAME_AUTO_MODE && avctx->channels == 1) || s->mode == TWOLAME_MONO)
83  avctx->bit_rate = avctx->sample_rate < 28000 ? 80000 : 192000;
84  else
85  avctx->bit_rate = avctx->sample_rate < 28000 ? 160000 : 384000;
86  }
87 
88  if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
89  twolame_set_VBR(s->glopts, TRUE);
90  twolame_set_VBR_level(s->glopts,
91  avctx->global_quality / (float) FF_QP2LAMBDA);
92  av_log(avctx, AV_LOG_WARNING,
93  "VBR in MP2 is a hack, use another codec that supports it.\n");
94  } else {
95  twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
96  }
97 
98  ret = twolame_init_params(s->glopts);
99  if (ret) {
100  twolame_encode_close(avctx);
101  return AVERROR_UNKNOWN;
102  }
103 
104  return 0;
105 }
106 
107 static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
108  const AVFrame *frame, int *got_packet_ptr)
109 {
110  TWOLAMEContext *s = avctx->priv_data;
111  int ret;
112 
113  if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE, 0)) < 0)
114  return ret;
115 
116  if (frame) {
117  switch (avctx->sample_fmt) {
118  case AV_SAMPLE_FMT_FLT:
119  ret = twolame_encode_buffer_float32_interleaved(s->glopts,
120  (const float *)frame->data[0],
121  frame->nb_samples,
122  avpkt->data,
123  avpkt->size);
124  break;
125  case AV_SAMPLE_FMT_FLTP:
126  ret = twolame_encode_buffer_float32(s->glopts,
127  (const float *)frame->data[0],
128  (const float *)frame->data[1],
129  frame->nb_samples,
130  avpkt->data, avpkt->size);
131  break;
132  case AV_SAMPLE_FMT_S16:
133  ret = twolame_encode_buffer_interleaved(s->glopts,
134  (const short int *)frame->data[0],
135  frame->nb_samples,
136  avpkt->data, avpkt->size);
137  break;
138  case AV_SAMPLE_FMT_S16P:
139  ret = twolame_encode_buffer(s->glopts,
140  (const short int *)frame->data[0],
141  (const short int *)frame->data[1],
142  frame->nb_samples,
143  avpkt->data, avpkt->size);
144  break;
145  default:
146  av_log(avctx, AV_LOG_ERROR,
147  "Unsupported sample format %d.\n", avctx->sample_fmt);
148  return AVERROR_BUG;
149  }
150  } else {
151  ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
152  }
153 
154  if (!ret) // no bytes written
155  return 0;
156  if (ret < 0) // twolame error
157  return AVERROR_UNKNOWN;
158 
159  if (frame) {
160  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
161  if (frame->pts != AV_NOPTS_VALUE)
162  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
163  } else {
164  avpkt->pts = s->next_pts;
165  }
166  // this is for setting pts for flushed packet(s).
167  if (avpkt->pts != AV_NOPTS_VALUE)
168  s->next_pts = avpkt->pts + avpkt->duration;
169 
170  av_shrink_packet(avpkt, ret);
171  *got_packet_ptr = 1;
172  return 0;
173 }
174 
175 #define OFFSET(x) offsetof(TWOLAMEContext, x)
176 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
177 static const AVOption options[] = {
178  { "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, "mode"},
179  { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, "mode" },
180  { "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, "mode" },
181  { "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, "mode" },
182  { "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, "mode" },
183  { "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, "mode" },
184  { "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
185  { "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
186  { "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
187  { "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
188  { "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
189  { "verbosity", "set library optput level (0-10)", OFFSET(verbosity), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, AE},
190  { NULL },
191 };
192 
193 static const AVClass twolame_class = {
194  .class_name = "libtwolame encoder",
195  .item_name = av_default_item_name,
196  .option = options,
197  .version = LIBAVUTIL_VERSION_INT,
198 };
199 
201  { "b", "0" },
202  { NULL },
203 };
204 
205 static const int twolame_samplerates[] = {
206  16000, 22050, 24000, 32000, 44100, 48000, 0
207 };
208 
210  .name = "libtwolame",
211  .long_name = NULL_IF_CONFIG_SMALL("libtwolame MP2 (MPEG audio layer 2)"),
212  .type = AVMEDIA_TYPE_AUDIO,
213  .id = AV_CODEC_ID_MP2,
214  .priv_data_size = sizeof(TWOLAMEContext),
216  .encode2 = twolame_encode_frame,
217  .close = twolame_encode_close,
218  .capabilities = AV_CODEC_CAP_DELAY,
220  .priv_class = &twolame_class,
221  .sample_fmts = (const enum AVSampleFormat[]) {
227  },
228  .channel_layouts = (const uint64_t[]) {
231  0 },
232  .supported_samplerates = twolame_samplerates,
233  .wrapper_name = "libtwolame",
234 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1118
AVCodec
AVCodec.
Definition: codec.h:197
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
twolame_encode_close
static av_cold int twolame_encode_close(AVCodecContext *avctx)
Definition: libtwolame.c:50
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:187
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
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
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1098
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:953
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:252
TWOLAMEContext::error_protection
int error_protection
Definition: libtwolame.c:41
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
AVOption
AVOption.
Definition: opt.h:248
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:383
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
TWOLAMEContext::psymodel
int psymodel
Definition: libtwolame.c:39
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:109
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1807
TWOLAMEContext::copyright
int copyright
Definition: libtwolame.c:42
defaults
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:581
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
twolame_encode_init
static av_cold int twolame_encode_init(AVCodecContext *avctx)
Definition: libtwolame.c:57
ff_samples_to_time_base
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:262
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:567
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:421
AVCodecDefault
Definition: internal.h:207
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:551
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
twolame_encode_frame
static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libtwolame.c:107
twolame_class
static const AVClass twolame_class
Definition: libtwolame.c:193
TWOLAMEContext::energy
int energy
Definition: libtwolame.c:40
TWOLAMEContext::original
int original
Definition: libtwolame.c:43
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
TWOLAMEContext::mode
int mode
Definition: libtwolame.c:38
ff_libtwolame_encoder
const AVCodec ff_libtwolame_encoder
Definition: libtwolame.c:209
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1106
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
options
static const AVOption options[]
Definition: libtwolame.c:177
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1099
twolame_defaults
static const AVCodecDefault twolame_defaults[]
Definition: libtwolame.c:200
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:358
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
TWOLAMEContext
Definition: libtwolame.c:36
OFFSET
#define OFFSET(x)
Definition: libtwolame.c:175
mpegaudio.h
avcodec.h
TWOLAMEContext::glopts
twolame_options * glopts
Definition: libtwolame.c:46
TWOLAMEContext::next_pts
int64_t next_pts
Definition: libtwolame.c:47
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:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AE
#define AE
Definition: libtwolame.c:176
AVCodecContext
main external API structure.
Definition: avcodec.h:501
twolame_samplerates
static const int twolame_samplerates[]
Definition: libtwolame.c:205
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
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:77
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:528
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
TWOLAMEContext::verbosity
int verbosity
Definition: libtwolame.c:44