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