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