Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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->delay = 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  if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
81  twolame_set_VBR(s->glopts, TRUE);
82  twolame_set_VBR_level(s->glopts,
83  avctx->global_quality / (float) FF_QP2LAMBDA);
84  av_log(avctx, AV_LOG_WARNING,
85  "VBR in MP2 is a hack, use another codec that supports it.\n");
86  } else {
87  twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
88  }
89 
90  ret = twolame_init_params(s->glopts);
91  if (ret) {
92  twolame_encode_close(avctx);
93  return AVERROR_UNKNOWN;
94  }
95 
96  return 0;
97 }
98 
99 static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
100  const AVFrame *frame, int *got_packet_ptr)
101 {
102  TWOLAMEContext *s = avctx->priv_data;
103  int ret;
104 
105  if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
106  return ret;
107 
108  if (frame) {
109  switch (avctx->sample_fmt) {
110  case AV_SAMPLE_FMT_FLT:
111  ret = twolame_encode_buffer_float32_interleaved(s->glopts,
112  (const float *)frame->data[0],
113  frame->nb_samples,
114  avpkt->data,
115  avpkt->size);
116  break;
117  case AV_SAMPLE_FMT_FLTP:
118  ret = twolame_encode_buffer_float32(s->glopts,
119  (const float *)frame->data[0],
120  (const float *)frame->data[1],
121  frame->nb_samples,
122  avpkt->data, avpkt->size);
123  break;
124  case AV_SAMPLE_FMT_S16:
125  ret = twolame_encode_buffer_interleaved(s->glopts,
126  (const short int *)frame->data[0],
127  frame->nb_samples,
128  avpkt->data, avpkt->size);
129  break;
130  case AV_SAMPLE_FMT_S16P:
131  ret = twolame_encode_buffer(s->glopts,
132  (const short int *)frame->data[0],
133  (const short int *)frame->data[1],
134  frame->nb_samples,
135  avpkt->data, avpkt->size);
136  break;
137  default:
138  av_log(avctx, AV_LOG_ERROR,
139  "Unsupported sample format %d.\n", avctx->sample_fmt);
140  return AVERROR_BUG;
141  }
142  } else {
143  ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
144  }
145 
146  if (!ret) // no bytes written
147  return 0;
148  if (ret < 0) // twolame error
149  return AVERROR_UNKNOWN;
150 
151  if (frame) {
152  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
153  if (frame->pts != AV_NOPTS_VALUE)
154  avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
155  } else {
156  avpkt->pts = s->next_pts;
157  }
158  // this is for setting pts for flushed packet(s).
159  if (avpkt->pts != AV_NOPTS_VALUE)
160  s->next_pts = avpkt->pts + avpkt->duration;
161 
162  av_shrink_packet(avpkt, ret);
163  *got_packet_ptr = 1;
164  return 0;
165 }
166 
167 #define OFFSET(x) offsetof(TWOLAMEContext, x)
168 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
169 static const AVOption options[] = {
170  { "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, "mode"},
171  { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, "mode" },
172  { "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, "mode" },
173  { "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, "mode" },
174  { "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, "mode" },
175  { "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, "mode" },
176  { "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
177  { "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
178  { "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
179  { "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
180  { "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
181  { "verbosity", "set library optput level (0-10)", OFFSET(verbosity), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, AE},
182  { NULL },
183 };
184 
185 static const AVClass twolame_class = {
186  .class_name = "libtwolame encoder",
187  .item_name = av_default_item_name,
188  .option = options,
189  .version = LIBAVUTIL_VERSION_INT,
190 };
191 
193  { "b", "384000" },
194  { NULL },
195 };
196 
197 static const int twolame_samplerates[] = {
198  16000, 22050, 24000, 32000, 44100, 48000, 0
199 };
200 
202  .name = "libtwolame",
203  .long_name = NULL_IF_CONFIG_SMALL("libtwolame MP2 (MPEG audio layer 2)"),
204  .type = AVMEDIA_TYPE_AUDIO,
205  .id = AV_CODEC_ID_MP2,
206  .priv_data_size = sizeof(TWOLAMEContext),
208  .encode2 = twolame_encode_frame,
210  .capabilities = CODEC_CAP_DELAY,
211  .defaults = twolame_defaults,
212  .priv_class = &twolame_class,
213  .sample_fmts = (const enum AVSampleFormat[]) {
219  },
220  .channel_layouts = (const uint64_t[]) {
223  0 },
224  .supported_samplerates = twolame_samplerates,
225 };
#define MPA_MAX_CODED_FRAME_SIZE
Definition: mpegaudio.h:39
float, planar
Definition: samplefmt.h:72
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static av_cold int twolame_encode_init(AVCodecContext *avctx)
Definition: libtwolame.c:57
AVOption.
Definition: opt.h:234
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
int size
Definition: avcodec.h:974
#define AE
Definition: libtwolame.c:168
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:2812
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:38
AVCodec ff_libtwolame_encoder
Definition: libtwolame.c:201
static const AVCodecDefault twolame_defaults[]
Definition: libtwolame.c:192
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
#define av_cold
Definition: attributes.h:66
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
uint8_t * data
Definition: avcodec.h:973
static const int twolame_samplerates[]
Definition: libtwolame.c:197
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static const AVCodecDefault defaults[]
Definition: libspeexenc.c:345
#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:713
#define AVERROR(e)
Definition: error.h:43
sample_fmts
Definition: avconv_filter.c:68
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:611
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
static const AVOption options[]
Definition: libtwolame.c:169
int bit_rate
the average bitrate
Definition: avcodec.h:1114
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1257
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
twolame_options * glopts
Definition: libtwolame.c:46
int error_protection
Definition: libtwolame.c:41
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1827
NULL
Definition: eval.c:55
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int sample_rate
samples per second
Definition: avcodec.h:1807
av_default_item_name
Definition: dnxhdenc.c:52
#define OFFSET(x)
Definition: libtwolame.c:167
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
Describe the class of an AVClass context structure.
Definition: log.h:33
static const AVClass twolame_class
Definition: libtwolame.c:185
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1130
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int64_t next_pts
Definition: libtwolame.c:47
common internal api header.
common internal and external API header
static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libtwolame.c:99
static av_cold int twolame_encode_close(AVCodecContext *avctx)
Definition: libtwolame.c:50
signed 16 bits
Definition: samplefmt.h:64
mpeg audio declarations for both encoder and decoder.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int channels
number of audio channels
Definition: avcodec.h:1808
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:207
signed 16 bits, planar
Definition: samplefmt.h:70
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:151
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:950
int delay
Codec delay.
Definition: avcodec.h:1212
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228