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 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 #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 /* libavcodec to libopus channel order mapping, passed to libopus */
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  if (avctx->global_quality) {
111  av_log(avctx, AV_LOG_ERROR,
112  "Quality-based encoding not supported, "
113  "please specify a bitrate and VBR setting.\n");
114  return AVERROR(EINVAL);
115  }
116 
117  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
118  if (ret != OPUS_OK) {
119  av_log(avctx, AV_LOG_ERROR,
120  "Failed to set bitrate: %s\n", opus_strerror(ret));
121  return ret;
122  }
123 
124  ret = opus_multistream_encoder_ctl(enc,
125  OPUS_SET_COMPLEXITY(opts->complexity));
126  if (ret != OPUS_OK)
127  av_log(avctx, AV_LOG_WARNING,
128  "Unable to set complexity: %s\n", opus_strerror(ret));
129 
130  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
131  if (ret != OPUS_OK)
132  av_log(avctx, AV_LOG_WARNING,
133  "Unable to set VBR: %s\n", opus_strerror(ret));
134 
135  ret = opus_multistream_encoder_ctl(enc,
136  OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
137  if (ret != OPUS_OK)
138  av_log(avctx, AV_LOG_WARNING,
139  "Unable to set constrained VBR: %s\n", opus_strerror(ret));
140 
141  ret = opus_multistream_encoder_ctl(enc,
142  OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
143  if (ret != OPUS_OK)
144  av_log(avctx, AV_LOG_WARNING,
145  "Unable to set expected packet loss percentage: %s\n",
146  opus_strerror(ret));
147 
148  if (avctx->cutoff) {
149  ret = opus_multistream_encoder_ctl(enc,
150  OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
151  if (ret != OPUS_OK)
152  av_log(avctx, AV_LOG_WARNING,
153  "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
154  }
155 
156  return OPUS_OK;
157 }
158 
160 {
161  LibopusEncContext *opus = avctx->priv_data;
162  const uint8_t *channel_mapping;
163  OpusMSEncoder *enc;
164  int ret = OPUS_OK;
165  int coupled_stream_count, header_size, frame_size;
166 
167  coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
168  opus->stream_count = avctx->channels - coupled_stream_count;
169  channel_mapping = libavcodec_libopus_channel_map[avctx->channels - 1];
170 
171  /* FIXME: Opus can handle up to 255 channels. However, the mapping for
172  * anything greater than 8 is undefined. */
173  if (avctx->channels > 8)
174  av_log(avctx, AV_LOG_WARNING,
175  "Channel layout undefined for %d channels.\n", avctx->channels);
176 
177  if (!avctx->bit_rate) {
178  /* Sane default copied from opusenc */
179  avctx->bit_rate = 64000 * opus->stream_count +
180  32000 * coupled_stream_count;
181  av_log(avctx, AV_LOG_WARNING,
182  "No bit rate set. Defaulting to %d bps.\n", avctx->bit_rate);
183  }
184 
185  if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
186  av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
187  "Please choose a value between 500 and %d.\n", avctx->bit_rate,
188  256000 * avctx->channels);
189  return AVERROR(EINVAL);
190  }
191 
192  frame_size = opus->opts.frame_duration * 48000 / 1000;
193  switch (frame_size) {
194  case 120:
195  case 240:
196  if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
197  av_log(avctx, AV_LOG_WARNING,
198  "LPC mode cannot be used with a frame duration of less "
199  "than 10ms. Enabling restricted low-delay mode.\n"
200  "Use a longer frame duration if this is not what you want.\n");
201  /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
202  * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
203  opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
204  case 480:
205  case 960:
206  case 1920:
207  case 2880:
208  opus->opts.packet_size =
209  avctx->frame_size = frame_size * avctx->sample_rate / 48000;
210  break;
211  default:
212  av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
213  "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
214  opus->opts.frame_duration);
215  return AVERROR(EINVAL);
216  }
217 
218  if (avctx->compression_level < 0 || avctx->compression_level > 10) {
219  av_log(avctx, AV_LOG_WARNING,
220  "Compression level must be in the range 0 to 10. "
221  "Defaulting to 10.\n");
222  opus->opts.complexity = 10;
223  } else {
224  opus->opts.complexity = avctx->compression_level;
225  }
226 
227  if (avctx->cutoff) {
228  switch (avctx->cutoff) {
229  case 4000:
230  opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
231  break;
232  case 6000:
233  opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
234  break;
235  case 8000:
236  opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
237  break;
238  case 12000:
239  opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
240  break;
241  case 20000:
242  opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
243  break;
244  default:
245  av_log(avctx, AV_LOG_WARNING,
246  "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
247  "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
248  avctx->cutoff);
249  avctx->cutoff = 0;
250  }
251  }
252 
253  enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
254  opus->stream_count,
255  coupled_stream_count,
256  channel_mapping,
257  opus->opts.application, &ret);
258  if (ret != OPUS_OK) {
259  av_log(avctx, AV_LOG_ERROR,
260  "Failed to create encoder: %s\n", opus_strerror(ret));
261  return ff_opus_error_to_averror(ret);
262  }
263 
264  ret = libopus_configure_encoder(avctx, enc, &opus->opts);
265  if (ret != OPUS_OK) {
266  ret = ff_opus_error_to_averror(ret);
267  goto fail;
268  }
269 
270  header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
271  avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
272  if (!avctx->extradata) {
273  av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
274  ret = AVERROR(ENOMEM);
275  goto fail;
276  }
277  avctx->extradata_size = header_size;
278 
279  opus->samples = av_mallocz(frame_size * avctx->channels *
281  if (!opus->samples) {
282  av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
283  ret = AVERROR(ENOMEM);
284  goto fail;
285  }
286 
287  ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->delay));
288  if (ret != OPUS_OK)
289  av_log(avctx, AV_LOG_WARNING,
290  "Unable to get number of lookahead samples: %s\n",
291  opus_strerror(ret));
292 
293  libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
294  opus_vorbis_channel_map[avctx->channels - 1]);
295 
296  ff_af_queue_init(avctx, &opus->afq);
297 
298  opus->enc = enc;
299 
300  return 0;
301 
302 fail:
303  opus_multistream_encoder_destroy(enc);
304  av_freep(&avctx->extradata);
305  return ret;
306 }
307 
308 static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
309  const AVFrame *frame, int *got_packet_ptr)
310 {
311  LibopusEncContext *opus = avctx->priv_data;
312  const int sample_size = avctx->channels *
314  uint8_t *audio;
315  int ret;
316 
317  if (frame) {
318  ff_af_queue_add(&opus->afq, frame);
319  if (frame->nb_samples < opus->opts.packet_size) {
320  audio = opus->samples;
321  memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
322  } else
323  audio = frame->data[0];
324  } else {
325  if (!opus->afq.remaining_samples)
326  return 0;
327  audio = opus->samples;
328  memset(audio, 0, opus->opts.packet_size * sample_size);
329  }
330 
331  /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
332  * consist of 3 frames in one packet. The maximum frame size is 1275
333  * bytes along with the largest possible packet header of 7 bytes. */
334  if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 3 + 7) * opus->stream_count)) < 0)
335  return ret;
336 
337  if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
338  ret = opus_multistream_encode_float(opus->enc, (float *)audio,
339  opus->opts.packet_size,
340  avpkt->data, avpkt->size);
341  else
342  ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
343  opus->opts.packet_size,
344  avpkt->data, avpkt->size);
345 
346  if (ret < 0) {
347  av_log(avctx, AV_LOG_ERROR,
348  "Error encoding frame: %s\n", opus_strerror(ret));
349  return ff_opus_error_to_averror(ret);
350  }
351 
352  av_shrink_packet(avpkt, ret);
353 
354  ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
355  &avpkt->pts, &avpkt->duration);
356 
357  *got_packet_ptr = 1;
358 
359  return 0;
360 }
361 
363 {
364  LibopusEncContext *opus = avctx->priv_data;
365 
366  opus_multistream_encoder_destroy(opus->enc);
367 
368  ff_af_queue_close(&opus->afq);
369 
370  av_freep(&opus->samples);
371  av_freep(&avctx->extradata);
372 
373  return 0;
374 }
375 
376 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
377 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
378 static const AVOption libopus_options[] = {
379  { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
380  { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
381  { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
382  { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
383  { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 10.0 }, 2.5, 60.0, FLAGS },
384  { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
385  { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
386  { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
387  { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
388  { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
389  { NULL },
390 };
391 
392 static const AVClass libopus_class = {
393  .class_name = "libopus",
394  .item_name = av_default_item_name,
395  .option = libopus_options,
396  .version = LIBAVUTIL_VERSION_INT,
397 };
398 
400  { "b", "0" },
401  { "compression_level", "10" },
402  { NULL },
403 };
404 
405 static const int libopus_sample_rates[] = {
406  48000, 24000, 16000, 12000, 8000, 0,
407 };
408 
410  .name = "libopus",
411  .type = AVMEDIA_TYPE_AUDIO,
412  .id = AV_CODEC_ID_OPUS,
413  .priv_data_size = sizeof(LibopusEncContext),
415  .encode2 = libopus_encode,
418  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
421  .channel_layouts = ff_vorbis_channel_layouts,
422  .supported_samplerates = libopus_sample_rates,
423  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
424  .priv_class = &libopus_class,
425  .defaults = libopus_defaults,
426 };
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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc, LibopusEncOpts *opts)
Definition: libopusenc.c:105
AVOption.
Definition: opt.h:251
av_default_item_name
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:97
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
#define OFFSET(x)
Definition: libopusenc.c:376
static int av_cold libopus_encode_close(AVCodecContext *avctx)
Definition: libopusenc.c:362
uint8_t * samples
Definition: libopusenc.c:47
signed 16 bits
Definition: samplefmt.h:52
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:55
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
AVOptions.
static const AVClass libopus_class
Definition: libopusenc.c:392
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint8_t * data
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
OpusMSEncoder * enc
Definition: libopusenc.c:45
frame
Definition: stft.m:14
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
static int av_cold libopus_encode_init(AVCodecContext *avctx)
Definition: libopusenc.c:159
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static const uint8_t libavcodec_libopus_channel_map[8][8]
Definition: libopusenc.c:69
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
external API header
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...
int bit_rate
the average bitrate
ret
Definition: avfilter.c:821
struct LibopusEncContext LibopusEncContext
static const int libopus_sample_rates[]
Definition: libopusenc.c:405
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
int frame_size
Number of samples per channel in an audio frame.
NULL
Definition: eval.c:55
int sample_rate
samples per second
main external API structure.
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libopusenc.c:308
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
AVCodec ff_libopus_encoder
Definition: libopusenc.c:409
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
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:73
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
int global_quality
Global quality for codecs which cannot change it per frame.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
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
static const AVCodecDefault libopus_defaults[]
Definition: libopusenc.c:399
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:337
int cutoff
Audio cutoff bandwidth (0 means "automatic")
void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
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
float frame_duration
Definition: libopusenc.c:38
static const AVOption libopus_options[]
Definition: libopusenc.c:378
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
#define FLAGS
Definition: libopusenc.c:377
This structure stores compressed data.
int delay
Codec delay.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
struct LibopusEncOpts LibopusEncOpts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static void libopus_write_header(AVCodecContext *avctx, int stream_count, int coupled_stream_count, const uint8_t *channel_mapping)
Definition: libopusenc.c:80