Libav
libopusenc.c
Go to the documentation of this file.
1 /*
2  * Opus encoder using libopus
3  * Copyright (c) 2012 Nathan Caldwell
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 
22 #include <opus.h>
23 #include <opus_multistream.h>
24 
25 #include "libavutil/opt.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "libopus.h"
30 #include "vorbis.h"
31 #include "audio_frame_queue.h"
32 
33 typedef struct LibopusEncOpts {
34  int vbr;
42 
43 typedef struct LibopusEncContext {
44  AVClass *class;
45  OpusMSEncoder *enc;
51 
52 static const uint8_t opus_coupled_streams[8] = {
53  0, 1, 1, 2, 2, 2, 2, 3
54 };
55 
56 /* Opus internal to Vorbis channel order mapping written in the header */
57 static const uint8_t opus_vorbis_channel_map[8][8] = {
58  { 0 },
59  { 0, 1 },
60  { 0, 2, 1 },
61  { 0, 1, 2, 3 },
62  { 0, 4, 1, 2, 3 },
63  { 0, 4, 1, 2, 3, 5 },
64  { 0, 4, 1, 2, 3, 5, 6 },
65  { 0, 6, 1, 2, 3, 4, 5, 7 },
66 };
67 
68 /* libav to libopus channel order mapping, passed to libopus */
69 static const uint8_t libav_libopus_channel_map[8][8] = {
70  { 0 },
71  { 0, 1 },
72  { 0, 1, 2 },
73  { 0, 1, 2, 3 },
74  { 0, 1, 3, 4, 2 },
75  { 0, 1, 4, 5, 2, 3 },
76  { 0, 1, 5, 6, 2, 4, 3 },
77  { 0, 1, 6, 7, 4, 5, 2, 3 },
78 };
79 
80 static void libopus_write_header(AVCodecContext *avctx, int stream_count,
81  int coupled_stream_count,
82  const uint8_t *channel_mapping)
83 {
84  uint8_t *p = avctx->extradata;
85  int channels = avctx->channels;
86 
87  bytestream_put_buffer(&p, "OpusHead", 8);
88  bytestream_put_byte(&p, 1); /* Version */
89  bytestream_put_byte(&p, channels);
90  bytestream_put_le16(&p, avctx->delay); /* Lookahead samples at 48kHz */
91  bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
92  bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
93 
94  /* Channel mapping */
95  if (channels > 2) {
96  bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
97  bytestream_put_byte(&p, stream_count);
98  bytestream_put_byte(&p, coupled_stream_count);
99  bytestream_put_buffer(&p, channel_mapping, channels);
100  } else {
101  bytestream_put_byte(&p, 0);
102  }
103 }
104 
105 static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
106  LibopusEncOpts *opts)
107 {
108  int ret;
109 
110  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
111  if (ret != OPUS_OK) {
112  av_log(avctx, AV_LOG_ERROR,
113  "Failed to set bitrate: %s\n", opus_strerror(ret));
114  return ret;
115  }
116 
117  ret = opus_multistream_encoder_ctl(enc,
118  OPUS_SET_COMPLEXITY(opts->complexity));
119  if (ret != OPUS_OK)
120  av_log(avctx, AV_LOG_WARNING,
121  "Unable to set complexity: %s\n", opus_strerror(ret));
122 
123  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
124  if (ret != OPUS_OK)
125  av_log(avctx, AV_LOG_WARNING,
126  "Unable to set VBR: %s\n", opus_strerror(ret));
127 
128  ret = opus_multistream_encoder_ctl(enc,
129  OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
130  if (ret != OPUS_OK)
131  av_log(avctx, AV_LOG_WARNING,
132  "Unable to set constrained VBR: %s\n", opus_strerror(ret));
133 
134  ret = opus_multistream_encoder_ctl(enc,
135  OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
136  if (ret != OPUS_OK)
137  av_log(avctx, AV_LOG_WARNING,
138  "Unable to set expected packet loss percentage: %s\n",
139  opus_strerror(ret));
140 
141  if (avctx->cutoff) {
142  ret = opus_multistream_encoder_ctl(enc,
143  OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
144  if (ret != OPUS_OK)
145  av_log(avctx, AV_LOG_WARNING,
146  "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
147  }
148 
149  return OPUS_OK;
150 }
151 
153 {
154  LibopusEncContext *opus = avctx->priv_data;
155  const uint8_t *channel_mapping;
156  OpusMSEncoder *enc;
157  int ret = OPUS_OK;
158  int coupled_stream_count, header_size, frame_size;
159 
160  coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
161  opus->stream_count = avctx->channels - coupled_stream_count;
162  channel_mapping = libav_libopus_channel_map[avctx->channels - 1];
163 
164  /* FIXME: Opus can handle up to 255 channels. However, the mapping for
165  * anything greater than 8 is undefined. */
166  if (avctx->channels > 8) {
167  av_log(avctx, AV_LOG_ERROR,
168  "Channel layout undefined for %d channels.\n", avctx->channels);
169  return AVERROR_PATCHWELCOME;
170  }
171  if (!avctx->bit_rate) {
172  /* Sane default copied from opusenc */
173  avctx->bit_rate = 64000 * opus->stream_count +
174  32000 * coupled_stream_count;
175  av_log(avctx, AV_LOG_WARNING,
176  "No bit rate set. Defaulting to %d bps.\n", avctx->bit_rate);
177  }
178 
179  if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
180  av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
181  "Please choose a value between 500 and %d.\n", avctx->bit_rate,
182  256000 * avctx->channels);
183  return AVERROR(EINVAL);
184  }
185 
186  frame_size = opus->opts.frame_duration * 48000 / 1000;
187  switch (frame_size) {
188  case 120:
189  case 240:
190  if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
191  av_log(avctx, AV_LOG_WARNING,
192  "LPC mode cannot be used with a frame duration of less "
193  "than 10ms. Enabling restricted low-delay mode.\n"
194  "Use a longer frame duration if this is not what you want.\n");
195  /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
196  * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
197  opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
198  case 480:
199  case 960:
200  case 1920:
201  case 2880:
202  opus->opts.packet_size =
203  avctx->frame_size = frame_size * avctx->sample_rate / 48000;
204  break;
205  default:
206  av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
207  "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
208  opus->opts.frame_duration);
209  return AVERROR(EINVAL);
210  }
211 
212  if (avctx->compression_level < 0 || avctx->compression_level > 10) {
213  av_log(avctx, AV_LOG_WARNING,
214  "Compression level must be in the range 0 to 10. "
215  "Defaulting to 10.\n");
216  opus->opts.complexity = 10;
217  } else {
218  opus->opts.complexity = avctx->compression_level;
219  }
220 
221  if (avctx->cutoff) {
222  switch (avctx->cutoff) {
223  case 4000:
225  break;
226  case 6000:
228  break;
229  case 8000:
231  break;
232  case 12000:
234  break;
235  case 20000:
237  break;
238  default:
239  av_log(avctx, AV_LOG_WARNING,
240  "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
241  "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
242  avctx->cutoff);
243  avctx->cutoff = 0;
244  }
245  }
246 
247  enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
248  opus->stream_count,
249  coupled_stream_count,
250  channel_mapping,
251  opus->opts.application, &ret);
252  if (ret != OPUS_OK) {
253  av_log(avctx, AV_LOG_ERROR,
254  "Failed to create encoder: %s\n", opus_strerror(ret));
255  return ff_opus_error_to_averror(ret);
256  }
257 
258  ret = libopus_configure_encoder(avctx, enc, &opus->opts);
259  if (ret != OPUS_OK) {
260  ret = ff_opus_error_to_averror(ret);
261  goto fail;
262  }
263 
264  header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
265  avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
266  if (!avctx->extradata) {
267  av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
268  ret = AVERROR(ENOMEM);
269  goto fail;
270  }
271  avctx->extradata_size = header_size;
272 
273  opus->samples = av_mallocz(frame_size * avctx->channels *
275  if (!opus->samples) {
276  av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
277  ret = AVERROR(ENOMEM);
278  goto fail;
279  }
280 
281  ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->delay));
282  if (ret != OPUS_OK)
283  av_log(avctx, AV_LOG_WARNING,
284  "Unable to get number of lookahead samples: %s\n",
285  opus_strerror(ret));
286 
287  libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
288  opus_vorbis_channel_map[avctx->channels - 1]);
289 
290  ff_af_queue_init(avctx, &opus->afq);
291 
292  opus->enc = enc;
293 
294  return 0;
295 
296 fail:
297  opus_multistream_encoder_destroy(enc);
298  av_freep(&avctx->extradata);
299  return ret;
300 }
301 
302 static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
303  const AVFrame *frame, int *got_packet_ptr)
304 {
305  LibopusEncContext *opus = avctx->priv_data;
306  const int sample_size = avctx->channels *
308  uint8_t *audio;
309  int ret;
310 
311  if (frame) {
312  ff_af_queue_add(&opus->afq, frame);
313  if (frame->nb_samples < opus->opts.packet_size) {
314  audio = opus->samples;
315  memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
316  } else
317  audio = frame->data[0];
318  } else {
319  if (!opus->afq.remaining_samples)
320  return 0;
321  audio = opus->samples;
322  memset(audio, 0, opus->opts.packet_size * sample_size);
323  }
324 
325  /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
326  * consist of 3 frames in one packet. The maximum frame size is 1275
327  * bytes along with the largest possible packet header of 7 bytes. */
328  if (ret = ff_alloc_packet(avpkt, (1275 * 3 + 7) * opus->stream_count)) {
329  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
330  return ret;
331  }
332 
333  if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
334  ret = opus_multistream_encode_float(opus->enc, (float *)audio,
335  opus->opts.packet_size,
336  avpkt->data, avpkt->size);
337  else
338  ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
339  opus->opts.packet_size,
340  avpkt->data, avpkt->size);
341 
342  if (ret < 0) {
343  av_log(avctx, AV_LOG_ERROR,
344  "Error encoding frame: %s\n", opus_strerror(ret));
345  return ff_opus_error_to_averror(ret);
346  }
347 
348  av_shrink_packet(avpkt, ret);
349 
350  ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
351  &avpkt->pts, &avpkt->duration);
352 
353  *got_packet_ptr = 1;
354 
355  return 0;
356 }
357 
359 {
360  LibopusEncContext *opus = avctx->priv_data;
361 
362  opus_multistream_encoder_destroy(opus->enc);
363 
364  ff_af_queue_close(&opus->afq);
365 
366  av_freep(&opus->samples);
367  av_freep(&avctx->extradata);
368 
369  return 0;
370 }
371 
372 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
373 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
374 static const AVOption libopus_options[] = {
375  { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
376  { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
377  { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
378  { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
379  { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 60.0, FLAGS },
380  { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
381  { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
382  { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
383  { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
384  { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
385  { NULL },
386 };
387 
388 static const AVClass libopus_class = {
389  .class_name = "libopus",
390  .item_name = av_default_item_name,
391  .option = libopus_options,
392  .version = LIBAVUTIL_VERSION_INT,
393 };
394 
396  { "b", "0" },
397  { "compression_level", "10" },
398  { NULL },
399 };
400 
401 static const int libopus_sample_rates[] = {
402  48000, 24000, 16000, 12000, 8000, 0,
403 };
404 
406  .name = "libopus",
407  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
408  .type = AVMEDIA_TYPE_AUDIO,
409  .id = AV_CODEC_ID_OPUS,
410  .priv_data_size = sizeof(LibopusEncContext),
412  .encode2 = libopus_encode,
415  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
418  .channel_layouts = ff_vorbis_channel_layouts,
419  .supported_samplerates = libopus_sample_rates,
420  .priv_class = &libopus_class,
421  .defaults = libopus_defaults,
422 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc, LibopusEncOpts *opts)
Definition: libopusenc.c:105
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
#define OFFSET(x)
Definition: libopusenc.c:372
static int av_cold libopus_encode_close(AVCodecContext *avctx)
Definition: libopusenc.c:358
int size
Definition: avcodec.h:974
uint8_t * samples
Definition: libopusenc.c:47
AVCodec.
Definition: avcodec.h:2812
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
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
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
static const AVClass libopus_class
Definition: libopusenc.c:388
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
uint8_t * data
Definition: avcodec.h:973
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
OpusMSEncoder * enc
Definition: libopusenc.c:45
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
static int av_cold libopus_encode_init(AVCodecContext *avctx)
Definition: libopusenc.c:152
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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 CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:718
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
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 uint8_t libav_libopus_channel_map[8][8]
Definition: libopusenc.c:69
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
static const uint8_t opus_coupled_streams[8]
Definition: libopusenc.c:52
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
int bit_rate
the average bitrate
Definition: avcodec.h:1114
static const int libopus_sample_rates[]
Definition: libopusenc.c:401
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
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
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
int compression_level
Definition: avcodec.h:1136
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
main external API structure.
Definition: avcodec.h:1050
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libopusenc.c:302
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
AVCodec ff_libopus_encoder
Definition: libopusenc.c:405
int extradata_size
Definition: avcodec.h:1165
Describe the class of an AVClass context structure.
Definition: log.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:95
static const uint8_t opus_vorbis_channel_map[8][8]
Definition: libopusenc.c:57
LibopusEncOpts opts
Definition: libopusenc.c:48
common internal api header.
AudioFrameQueue afq
Definition: libopusenc.c:49
signed 16 bits
Definition: samplefmt.h:64
static const AVCodecDefault libopus_defaults[]
Definition: libopusenc.c:395
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:365
void * priv_data
Definition: avcodec.h:1092
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1851
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int *duration)
Remove frame(s) from the queue.
int channels
number of audio channels
Definition: avcodec.h:1808
float frame_duration
Definition: libopusenc.c:38
static const AVOption libopus_options[]
Definition: libopusenc.c:374
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
#define FLAGS
Definition: libopusenc.c:373
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
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
static void libopus_write_header(AVCodecContext *avctx, int stream_count, int coupled_stream_count, const uint8_t *channel_mapping)
Definition: libopusenc.c:80