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