FFmpeg
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 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
43  int apply_phase_inv;
44 #endif
46 
47 typedef struct LibopusEncContext {
48  AVClass *class;
49  OpusMSEncoder *enc;
56 
57 static const uint8_t opus_coupled_streams[8] = {
58  0, 1, 1, 2, 2, 2, 2, 3
59 };
60 
61 /* Opus internal to Vorbis channel order mapping written in the header */
62 static const uint8_t opus_vorbis_channel_map[8][8] = {
63  { 0 },
64  { 0, 1 },
65  { 0, 2, 1 },
66  { 0, 1, 2, 3 },
67  { 0, 4, 1, 2, 3 },
68  { 0, 4, 1, 2, 3, 5 },
69  { 0, 4, 1, 2, 3, 5, 6 },
70  { 0, 6, 1, 2, 3, 4, 5, 7 },
71 };
72 
73 /* libavcodec to libopus channel order mapping, passed to libopus */
75  { 0 },
76  { 0, 1 },
77  { 0, 1, 2 },
78  { 0, 1, 2, 3 },
79  { 0, 1, 3, 4, 2 },
80  { 0, 1, 4, 5, 2, 3 },
81  { 0, 1, 5, 6, 2, 4, 3 },
82  { 0, 1, 6, 7, 4, 5, 2, 3 },
83 };
84 
85 static void libopus_write_header(AVCodecContext *avctx, int stream_count,
86  int coupled_stream_count,
87  int mapping_family,
88  const uint8_t *channel_mapping)
89 {
90  uint8_t *p = avctx->extradata;
91  int channels = avctx->channels;
92 
93  bytestream_put_buffer(&p, "OpusHead", 8);
94  bytestream_put_byte(&p, 1); /* Version */
95  bytestream_put_byte(&p, channels);
96  bytestream_put_le16(&p, avctx->initial_padding); /* Lookahead samples at 48kHz */
97  bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
98  bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
99 
100  /* Channel mapping */
101  bytestream_put_byte(&p, mapping_family);
102  if (mapping_family != 0) {
103  bytestream_put_byte(&p, stream_count);
104  bytestream_put_byte(&p, coupled_stream_count);
105  bytestream_put_buffer(&p, channel_mapping, channels);
106  }
107 }
108 
109 static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
111 {
112  int ret;
113 
114  if (avctx->global_quality) {
115  av_log(avctx, AV_LOG_ERROR,
116  "Quality-based encoding not supported, "
117  "please specify a bitrate and VBR setting.\n");
118  return AVERROR(EINVAL);
119  }
120 
121  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
122  if (ret != OPUS_OK) {
123  av_log(avctx, AV_LOG_ERROR,
124  "Failed to set bitrate: %s\n", opus_strerror(ret));
125  return ret;
126  }
127 
128  ret = opus_multistream_encoder_ctl(enc,
129  OPUS_SET_COMPLEXITY(opts->complexity));
130  if (ret != OPUS_OK)
131  av_log(avctx, AV_LOG_WARNING,
132  "Unable to set complexity: %s\n", opus_strerror(ret));
133 
134  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
135  if (ret != OPUS_OK)
136  av_log(avctx, AV_LOG_WARNING,
137  "Unable to set VBR: %s\n", opus_strerror(ret));
138 
139  ret = opus_multistream_encoder_ctl(enc,
140  OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
141  if (ret != OPUS_OK)
142  av_log(avctx, AV_LOG_WARNING,
143  "Unable to set constrained VBR: %s\n", opus_strerror(ret));
144 
145  ret = opus_multistream_encoder_ctl(enc,
146  OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
147  if (ret != OPUS_OK)
148  av_log(avctx, AV_LOG_WARNING,
149  "Unable to set expected packet loss percentage: %s\n",
150  opus_strerror(ret));
151 
152  if (avctx->cutoff) {
153  ret = opus_multistream_encoder_ctl(enc,
154  OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
155  if (ret != OPUS_OK)
156  av_log(avctx, AV_LOG_WARNING,
157  "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
158  }
159 
160 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
161  ret = opus_multistream_encoder_ctl(enc,
162  OPUS_SET_PHASE_INVERSION_DISABLED(!opts->apply_phase_inv));
163  if (ret != OPUS_OK)
164  av_log(avctx, AV_LOG_WARNING,
165  "Unable to set phase inversion: %s\n",
166  opus_strerror(ret));
167 #endif
168  return OPUS_OK;
169 }
170 
172  int max_channels) {
173  if (avctx->channels > max_channels) {
174  av_log(avctx, AV_LOG_ERROR, "Opus mapping family undefined for %d channels.\n",
175  avctx->channels);
176  return AVERROR(EINVAL);
177  }
178 
179  return 0;
180 }
181 
182 static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family) {
184 
185  if (!avctx->channel_layout) {
186  av_log(avctx, AV_LOG_WARNING,
187  "No channel layout specified. Opus encoder will use Vorbis "
188  "channel layout for %d channels.\n", avctx->channels);
189  } else if (avctx->channel_layout != ff_vorbis_channel_layouts[avctx->channels - 1]) {
190  char name[32];
192  avctx->channel_layout);
193  av_log(avctx, AV_LOG_ERROR,
194  "Invalid channel layout %s for specified mapping family %d.\n",
195  name, mapping_family);
196 
197  return AVERROR(EINVAL);
198  }
199 
200  return 0;
201 }
202 
204  AVCodecContext *avctx,
205  int mapping_family,
206  const uint8_t ** channel_map_result)
207 {
208  const uint8_t * channel_map = NULL;
209  int ret;
210 
211  switch (mapping_family) {
212  case -1:
213  ret = libopus_check_max_channels(avctx, 8);
214  if (ret == 0) {
215  ret = libopus_check_vorbis_layout(avctx, mapping_family);
216  /* Channels do not need to be reordered. */
217  }
218 
219  break;
220  case 0:
221  ret = libopus_check_max_channels(avctx, 2);
222  if (ret == 0) {
223  ret = libopus_check_vorbis_layout(avctx, mapping_family);
224  }
225  break;
226  case 1:
227  /* Opus expects channels to be in Vorbis order. */
228  ret = libopus_check_max_channels(avctx, 8);
229  if (ret == 0) {
230  ret = libopus_check_vorbis_layout(avctx, mapping_family);
231  channel_map = ff_vorbis_channel_layout_offsets[avctx->channels - 1];
232  }
233  break;
234  case 255:
235  ret = libopus_check_max_channels(avctx, 254);
236  break;
237  default:
238  av_log(avctx, AV_LOG_WARNING,
239  "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
240  mapping_family);
241  ret = 0;
242  }
243 
244  *channel_map_result = channel_map;
245  return ret;
246 }
247 
249 {
250  LibopusEncContext *opus = avctx->priv_data;
251  OpusMSEncoder *enc;
252  uint8_t libopus_channel_mapping[255];
253  int ret = OPUS_OK;
254  int av_ret;
255  int coupled_stream_count, header_size, frame_size;
256  int mapping_family;
257 
258  frame_size = opus->opts.frame_duration * 48000 / 1000;
259  switch (frame_size) {
260  case 120:
261  case 240:
262  if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
263  av_log(avctx, AV_LOG_WARNING,
264  "LPC mode cannot be used with a frame duration of less "
265  "than 10ms. Enabling restricted low-delay mode.\n"
266  "Use a longer frame duration if this is not what you want.\n");
267  /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
268  * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
269  opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
270  case 480:
271  case 960:
272  case 1920:
273  case 2880:
274 #ifdef OPUS_FRAMESIZE_120_MS
275  case 3840:
276  case 4800:
277  case 5760:
278 #endif
279  opus->opts.packet_size =
280  avctx->frame_size = frame_size * avctx->sample_rate / 48000;
281  break;
282  default:
283  av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
284  "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
285 #ifdef OPUS_FRAMESIZE_120_MS
286  ", 60, 80, 100 or 120.\n",
287 #else
288  " or 60.\n",
289 #endif
290  opus->opts.frame_duration);
291  return AVERROR(EINVAL);
292  }
293 
294  if (avctx->compression_level < 0 || avctx->compression_level > 10) {
295  av_log(avctx, AV_LOG_WARNING,
296  "Compression level must be in the range 0 to 10. "
297  "Defaulting to 10.\n");
298  opus->opts.complexity = 10;
299  } else {
300  opus->opts.complexity = avctx->compression_level;
301  }
302 
303  if (avctx->cutoff) {
304  switch (avctx->cutoff) {
305  case 4000:
307  break;
308  case 6000:
310  break;
311  case 8000:
313  break;
314  case 12000:
316  break;
317  case 20000:
319  break;
320  default:
321  av_log(avctx, AV_LOG_WARNING,
322  "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
323  "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
324  avctx->cutoff);
325  avctx->cutoff = 0;
326  }
327  }
328 
329  /* Channels may need to be reordered to match opus mapping. */
331  &opus->encoder_channel_map);
332  if (av_ret) {
333  return av_ret;
334  }
335 
336  if (opus->opts.mapping_family == -1) {
337  /* By default, use mapping family 1 for the header but use the older
338  * libopus multistream API to avoid surround masking. */
339 
340  /* Set the mapping family so that the value is correct in the header */
341  mapping_family = avctx->channels > 2 ? 1 : 0;
342  coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
343  opus->stream_count = avctx->channels - coupled_stream_count;
344  memcpy(libopus_channel_mapping,
345  opus_vorbis_channel_map[avctx->channels - 1],
346  avctx->channels * sizeof(*libopus_channel_mapping));
347 
348  enc = opus_multistream_encoder_create(
349  avctx->sample_rate, avctx->channels, opus->stream_count,
350  coupled_stream_count,
352  opus->opts.application, &ret);
353  } else {
354  /* Use the newer multistream API. The encoder will set the channel
355  * mapping and coupled stream counts to its internal defaults and will
356  * use surround masking analysis to save bits. */
357  mapping_family = opus->opts.mapping_family;
358  enc = opus_multistream_surround_encoder_create(
359  avctx->sample_rate, avctx->channels, mapping_family,
360  &opus->stream_count, &coupled_stream_count, libopus_channel_mapping,
361  opus->opts.application, &ret);
362  }
363 
364  if (ret != OPUS_OK) {
365  av_log(avctx, AV_LOG_ERROR,
366  "Failed to create encoder: %s\n", opus_strerror(ret));
368  }
369 
370  if (!avctx->bit_rate) {
371  /* Sane default copied from opusenc */
372  avctx->bit_rate = 64000 * opus->stream_count +
373  32000 * coupled_stream_count;
374  av_log(avctx, AV_LOG_WARNING,
375  "No bit rate set. Defaulting to %"PRId64" bps.\n", avctx->bit_rate);
376  }
377 
378  if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
379  av_log(avctx, AV_LOG_ERROR, "The bit rate %"PRId64" bps is unsupported. "
380  "Please choose a value between 500 and %d.\n", avctx->bit_rate,
381  256000 * avctx->channels);
382  ret = AVERROR(EINVAL);
383  goto fail;
384  }
385 
386  ret = libopus_configure_encoder(avctx, enc, &opus->opts);
387  if (ret != OPUS_OK) {
389  goto fail;
390  }
391 
392  /* Header includes channel mapping table if and only if mapping family is NOT 0 */
393  header_size = 19 + (mapping_family == 0 ? 0 : 2 + avctx->channels);
394  avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
395  if (!avctx->extradata) {
396  av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
397  ret = AVERROR(ENOMEM);
398  goto fail;
399  }
400  avctx->extradata_size = header_size;
401 
402  opus->samples = av_mallocz_array(frame_size, avctx->channels *
404  if (!opus->samples) {
405  av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
406  ret = AVERROR(ENOMEM);
407  goto fail;
408  }
409 
410  ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
411  if (ret != OPUS_OK)
412  av_log(avctx, AV_LOG_WARNING,
413  "Unable to get number of lookahead samples: %s\n",
414  opus_strerror(ret));
415 
416  libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
417  mapping_family, libopus_channel_mapping);
418 
419  ff_af_queue_init(avctx, &opus->afq);
420 
421  opus->enc = enc;
422 
423  return 0;
424 
425 fail:
426  opus_multistream_encoder_destroy(enc);
427  av_freep(&avctx->extradata);
428  return ret;
429 }
430 
432  uint8_t *dst, const uint8_t *src, const uint8_t *channel_map,
433  int nb_channels, int nb_samples, int bytes_per_sample) {
434  int sample, channel;
435  for (sample = 0; sample < nb_samples; ++sample) {
436  for (channel = 0; channel < nb_channels; ++channel) {
437  const size_t src_pos = bytes_per_sample * (nb_channels * sample + channel);
438  const size_t dst_pos = bytes_per_sample * (nb_channels * sample + channel_map[channel]);
439 
440  memcpy(&dst[dst_pos], &src[src_pos], bytes_per_sample);
441  }
442  }
443 }
444 
445 static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
446  const AVFrame *frame, int *got_packet_ptr)
447 {
448  LibopusEncContext *opus = avctx->priv_data;
449  const int bytes_per_sample = av_get_bytes_per_sample(avctx->sample_fmt);
450  const int sample_size = avctx->channels * bytes_per_sample;
451  uint8_t *audio;
452  int ret;
453  int discard_padding;
454 
455  if (frame) {
456  ret = ff_af_queue_add(&opus->afq, frame);
457  if (ret < 0)
458  return ret;
459  if (opus->encoder_channel_map != NULL) {
460  audio = opus->samples;
462  audio, frame->data[0], opus->encoder_channel_map,
463  avctx->channels, frame->nb_samples, bytes_per_sample);
464  } else if (frame->nb_samples < opus->opts.packet_size) {
465  audio = opus->samples;
466  memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
467  } else
468  audio = frame->data[0];
469  } else {
470  if (!opus->afq.remaining_samples || (!opus->afq.frame_alloc && !opus->afq.frame_count))
471  return 0;
472  audio = opus->samples;
473  memset(audio, 0, opus->opts.packet_size * sample_size);
474  }
475 
476  /* Maximum packet size taken from opusenc in opus-tools. 120ms packets
477  * consist of 6 frames in one packet. The maximum frame size is 1275
478  * bytes along with the largest possible packet header of 7 bytes. */
479  if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count, 0)) < 0)
480  return ret;
481 
482  if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
483  ret = opus_multistream_encode_float(opus->enc, (float *)audio,
484  opus->opts.packet_size,
485  avpkt->data, avpkt->size);
486  else
487  ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
488  opus->opts.packet_size,
489  avpkt->data, avpkt->size);
490 
491  if (ret < 0) {
492  av_log(avctx, AV_LOG_ERROR,
493  "Error encoding frame: %s\n", opus_strerror(ret));
495  }
496 
497  av_shrink_packet(avpkt, ret);
498 
499  ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
500  &avpkt->pts, &avpkt->duration);
501 
502  discard_padding = opus->opts.packet_size - avpkt->duration;
503  // Check if subtraction resulted in an overflow
504  if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0)) {
505  av_packet_unref(avpkt);
506  return AVERROR(EINVAL);
507  }
508  if (discard_padding > 0) {
509  uint8_t* side_data = av_packet_new_side_data(avpkt,
511  10);
512  if(!side_data) {
513  av_packet_unref(avpkt);
514  return AVERROR(ENOMEM);
515  }
516  AV_WL32(side_data + 4, discard_padding);
517  }
518 
519  *got_packet_ptr = 1;
520 
521  return 0;
522 }
523 
525 {
526  LibopusEncContext *opus = avctx->priv_data;
527 
528  opus_multistream_encoder_destroy(opus->enc);
529 
530  ff_af_queue_close(&opus->afq);
531 
532  av_freep(&opus->samples);
533  av_freep(&avctx->extradata);
534 
535  return 0;
536 }
537 
538 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
539 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
540 static const AVOption libopus_options[] = {
541  { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
542  { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
543  { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
544  { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
545  { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0, FLAGS },
546  { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
547  { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
548  { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
549  { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
550  { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
551  { "mapping_family", "Channel Mapping Family", OFFSET(mapping_family), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, FLAGS, "mapping_family" },
552 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
553  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
554 #endif
555  { NULL },
556 };
557 
558 static const AVClass libopus_class = {
559  .class_name = "libopus",
560  .item_name = av_default_item_name,
561  .option = libopus_options,
562  .version = LIBAVUTIL_VERSION_INT,
563 };
564 
566  { "b", "0" },
567  { "compression_level", "10" },
568  { NULL },
569 };
570 
571 static const int libopus_sample_rates[] = {
572  48000, 24000, 16000, 12000, 8000, 0,
573 };
574 
576  .name = "libopus",
577  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
578  .type = AVMEDIA_TYPE_AUDIO,
579  .id = AV_CODEC_ID_OPUS,
580  .priv_data_size = sizeof(LibopusEncContext),
582  .encode2 = libopus_encode,
583  .close = libopus_encode_close,
585  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
588  .supported_samplerates = libopus_sample_rates,
589  .priv_class = &libopus_class,
590  .defaults = libopus_defaults,
591  .wrapper_name = "libopus",
592 };
libopus_class
static const AVClass libopus_class
Definition: libopusenc.c:558
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
AVCodec
AVCodec.
Definition: codec.h:190
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
libopus.h
LibopusEncOpts::application
int application
Definition: libopusenc.c:35
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
libopus_encode
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libopusenc.c:445
LibopusEncContext::opts
LibopusEncOpts opts
Definition: libopusenc.c:52
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
OPUS_BANDWIDTH_NARROWBAND
@ OPUS_BANDWIDTH_NARROWBAND
Definition: opus.h:71
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1186
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:716
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
av_get_channel_layout_string
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
Definition: channel_layout.c:211
LibopusEncContext::enc
OpusMSEncoder * enc
Definition: libopusenc.c:49
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
LibopusEncOpts::complexity
int complexity
Definition: libopusenc.c:37
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
AVOption
AVOption.
Definition: opt.h:246
LibopusEncOpts::packet_size
int packet_size
Definition: libopusenc.c:39
opus.h
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:373
OPUS_BANDWIDTH_FULLBAND
@ OPUS_BANDWIDTH_FULLBAND
Definition: opus.h:75
LibopusEncOpts::frame_duration
float frame_duration
Definition: libopusenc.c:38
opus_vorbis_channel_map
static const uint8_t opus_vorbis_channel_map[8][8]
Definition: libopusenc.c:62
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
libopus_encode_init
static av_cold int libopus_encode_init(AVCodecContext *avctx)
Definition: libopusenc.c:248
fail
#define fail()
Definition: checkasm.h:123
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:2060
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:628
libopus_encode_close
static av_cold int libopus_encode_close(AVCodecContext *avctx)
Definition: libopusenc.c:524
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:592
frame_size
int frame_size
Definition: mxfenc.c:2137
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AudioFrameQueue::remaining_samples
int remaining_samples
Definition: audio_frame_queue.h:35
libopus_write_header
static void libopus_write_header(AVCodecContext *avctx, int stream_count, int coupled_stream_count, int mapping_family, const uint8_t *channel_mapping)
Definition: libopusenc.c:85
AudioFrameQueue
Definition: audio_frame_queue.h:32
channels
channels
Definition: aptx.h:33
ff_opus_error_to_averror
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
FLAGS
#define FLAGS
Definition: libopusenc.c:539
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:332
OPUS_BANDWIDTH_WIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
Definition: opus.h:73
AVCodecDefault
Definition: internal.h:201
ff_libopus_encoder
AVCodec ff_libopus_encoder
Definition: libopusenc.c:575
LibopusEncOpts::mapping_family
int mapping_family
Definition: libopusenc.c:41
opts
AVDictionary * opts
Definition: movenc.c:50
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
LibopusEncContext
Definition: libopusenc.c:47
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
libopus_configure_encoder
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc, LibopusEncOpts *opts)
Definition: libopusenc.c:109
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
LibopusEncContext::samples
uint8_t * samples
Definition: libopusenc.c:51
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
src
#define src
Definition: vp8dsp.c:254
libopus_options
static const AVOption libopus_options[]
Definition: libopusenc.c:540
ff_vorbis_channel_layout_offsets
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
OPUS_BANDWIDTH_SUPERWIDEBAND
@ OPUS_BANDWIDTH_SUPERWIDEBAND
Definition: opus.h:74
LibopusEncContext::afq
AudioFrameQueue afq
Definition: libopusenc.c:53
AVPacket::size
int size
Definition: packet.h:356
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:188
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
sample
#define sample
Definition: flacdsp_template.c:44
LibopusEncOpts
Definition: libopusenc.c:33
libopus_defaults
static const AVCodecDefault libopus_defaults[]
Definition: libopusenc.c:565
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:470
LibopusEncContext::encoder_channel_map
const uint8_t * encoder_channel_map
Definition: libopusenc.c:54
OFFSET
#define OFFSET(x)
Definition: libopusenc.c:538
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:226
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:348
vorbis.h
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
AVCodecContext::cutoff
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1230
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint8_t
uint8_t
Definition: audio_convert.c:194
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:197
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:156
avcodec.h
AudioFrameQueue::frame_count
unsigned frame_count
Definition: audio_frame_queue.h:37
libopus_check_max_channels
static int libopus_check_max_channels(AVCodecContext *avctx, int max_channels)
Definition: libopusenc.c:171
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
LibopusEncContext::stream_count
int stream_count
Definition: libopusenc.c:50
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:215
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen_template.c:38
libopus_check_vorbis_layout
static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family)
Definition: libopusenc.c:182
AVCodecContext
main external API structure.
Definition: avcodec.h:526
LibopusEncOpts::vbr
int vbr
Definition: libopusenc.c:34
AudioFrameQueue::frame_alloc
unsigned frame_alloc
Definition: audio_frame_queue.h:38
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
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:75
OPUS_BANDWIDTH_MEDIUMBAND
@ OPUS_BANDWIDTH_MEDIUMBAND
Definition: opus.h:72
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:240
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
LibopusEncOpts::packet_loss
int packet_loss
Definition: libopusenc.c:36
ff_vorbis_channel_layouts
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
LibopusEncOpts::max_bandwidth
int max_bandwidth
Definition: libopusenc.c:40
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:80
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:32
libopus_validate_layout_and_get_channel_map
static int libopus_validate_layout_and_get_channel_map(AVCodecContext *avctx, int mapping_family, const uint8_t **channel_map_result)
Definition: libopusenc.c:203
libavcodec_libopus_channel_map
static const uint8_t libavcodec_libopus_channel_map[8][8]
Definition: libopusenc.c:74
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232
libopus_copy_samples_with_channel_map
static void libopus_copy_samples_with_channel_map(uint8_t *dst, const uint8_t *src, const uint8_t *channel_map, int nb_channels, int nb_samples, int bytes_per_sample)
Definition: libopusenc.c:431
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
channel
channel
Definition: ebur128.h:39
opus_coupled_streams
static const uint8_t opus_coupled_streams[8]
Definition: libopusenc.c:57
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:598
libopus_sample_rates
static const int libopus_sample_rates[]
Definition: libopusenc.c:571
nb_channels
int nb_channels
Definition: channel_layout.c:76