FFmpeg  2.1.1
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/opt.h"
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "mpegaudio.h"
33 
34 typedef struct TWOLAMEContext {
35  AVClass *class;
36  int mode;
37  int psymodel;
38  int energy;
40  int copyright;
41  int original;
42 
43  twolame_options *glopts;
44  int64_t next_pts;
46 
48 {
49  TWOLAMEContext *s = avctx->priv_data;
50  twolame_close(&s->glopts);
51  return 0;
52 }
53 
55 {
56  TWOLAMEContext *s = avctx->priv_data;
57  int ret;
58 
59  avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
60 
61  s->glopts = twolame_init();
62  if (!s->glopts)
63  return AVERROR(ENOMEM);
64 
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);
72 
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");
80  } else {
81  twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
82  }
83 
84  if ((ret = twolame_init_params(s->glopts)))
85  goto error;
86 
87  return 0;
88 error:
89  twolame_encode_close(avctx);
90  return ret;
91 }
92 
93 static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
94  const AVFrame *frame, int *got_packet_ptr)
95 {
96  TWOLAMEContext *s = avctx->priv_data;
97  int ret;
98 
99  if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
100  return ret;
101 
102  if (frame) {
103  switch (avctx->sample_fmt) {
104  case AV_SAMPLE_FMT_FLT:
105  ret = twolame_encode_buffer_float32_interleaved(s->glopts,
106  frame->data[0],
107  frame->nb_samples,
108  avpkt->data, avpkt->size);
109  break;
110  case AV_SAMPLE_FMT_FLTP:
111  ret = twolame_encode_buffer_float32(s->glopts,
112  frame->data[0], frame->data[1],
113  frame->nb_samples,
114  avpkt->data, avpkt->size);
115  break;
116  case AV_SAMPLE_FMT_S16:
117  ret = twolame_encode_buffer_interleaved(s->glopts,
118  frame->data[0],
119  frame->nb_samples,
120  avpkt->data, avpkt->size);
121  break;
122  case AV_SAMPLE_FMT_S16P:
123  ret = twolame_encode_buffer(s->glopts,
124  frame->data[0], frame->data[1],
125  frame->nb_samples,
126  avpkt->data, avpkt->size);
127  break;
128  default:
129  return AVERROR_BUG;
130  }
131  } else {
132  ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
133  }
134 
135  if (ret > 0) {
136  avpkt->duration = ff_samples_to_time_base(avctx, avctx->frame_size);
137  if (frame) {
138  if (frame->pts != AV_NOPTS_VALUE)
139  avpkt->pts = frame->pts;
140  } else {
141  avpkt->pts = s->next_pts;
142  }
143  if (avpkt->pts != AV_NOPTS_VALUE)
144  s->next_pts = avpkt->pts + avpkt->duration;
145 
146  avpkt->size = ret;
147  *got_packet_ptr = 1;
148  return 0;
149  }
150 
151  return ret;
152 }
153 
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},
168  { NULL },
169 };
170 
171 static const AVClass libtwolame_class = {
172  .class_name = "libtwolame encoder",
173  .item_name = av_default_item_name,
174  .option = options,
175  .version = LIBAVUTIL_VERSION_INT,
176 };
177 
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),
185  .encode2 = twolame_encode_frame,
187  .capabilities = CODEC_CAP_DELAY,
188  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
193  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
195  0 },
196  .supported_samplerates = (const int[]){ 16000, 22050, 24000, 32000, 44100, 48000, 0 },
197  .priv_class = &libtwolame_class,
198 };
#define MPA_MAX_CODED_FRAME_SIZE
Definition: mpegaudio.h:39
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1500
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static av_cold int twolame_encode_init(AVCodecContext *avctx)
Definition: libtwolame.c:54
AVOption.
Definition: opt.h:253
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define AE
Definition: libtwolame.c:155
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
AVCodec ff_libtwolame_encoder
Definition: libtwolame.c:178
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1881
mode
Definition: f_perms.c:27
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:55
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:182
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
const OptionDef options[]
Definition: ffserver.c:4682
signed 16 bits
Definition: samplefmt.h:52
static AVFrame * frame
Definition: demuxing.c:51
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:769
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:692
Libavcodec external API header.
int bit_rate
the average bitrate
Definition: avcodec.h:1204
ret
Definition: avfilter.c:961
#define TRUE
Definition: windows2linux.h:33
twolame_options * glopts
Definition: libtwolame.c:43
static const AVClass libtwolame_class
Definition: libtwolame.c:171
int error_protection
Definition: libtwolame.c:39
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1893
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
int sample_rate
samples per second
Definition: avcodec.h:1873
#define OFFSET(x)
Definition: libtwolame.c:154
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
Describe the class of an AVClass context structure.
Definition: log.h:50
uint8_t * data
Definition: avcodec.h:1063
void * priv_data
Definition: avcodec.h:1182
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1220
int64_t next_pts
Definition: libtwolame.c:44
common internal api header.
static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libtwolame.c:93
static av_cold int twolame_encode_close(AVCodecContext *avctx)
Definition: libtwolame.c:47
mpeg audio declarations for both encoder and decoder.
#define AVERROR_BUG
int channels
number of audio channels
Definition: avcodec.h:1874
#define AVERROR(e)
float, planar
Definition: samplefmt.h:60
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
signed 16 bits, planar
Definition: samplefmt.h:58
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:181
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1040
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:150
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278