FFmpeg
libopusenc.c
Go to the documentation of this file.
1 /*
2  * Opus encoder using libopus
3  * Copyright (c) 2012 Nathan Caldwell
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <opus.h>
23 #include <opus_multistream.h>
24 
25 #include "libavutil/opt.h"
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "internal.h"
29 #include "libopus.h"
30 #include "vorbis.h"
31 #include "audio_frame_queue.h"
32 
33 typedef struct LibopusEncOpts {
34  int vbr;
37  int fec;
43 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
44  int apply_phase_inv;
45 #endif
47 
48 typedef struct LibopusEncContext {
49  AVClass *class;
50  OpusMSEncoder *enc;
57 
58 static const uint8_t opus_coupled_streams[8] = {
59  0, 1, 1, 2, 2, 2, 2, 3
60 };
61 
62 /* Opus internal to Vorbis channel order mapping written in the header */
63 static const uint8_t opus_vorbis_channel_map[8][8] = {
64  { 0 },
65  { 0, 1 },
66  { 0, 2, 1 },
67  { 0, 1, 2, 3 },
68  { 0, 4, 1, 2, 3 },
69  { 0, 4, 1, 2, 3, 5 },
70  { 0, 4, 1, 2, 3, 5, 6 },
71  { 0, 6, 1, 2, 3, 4, 5, 7 },
72 };
73 
74 /* libavcodec to libopus channel order mapping, passed to libopus */
76  { 0 },
77  { 0, 1 },
78  { 0, 1, 2 },
79  { 0, 1, 2, 3 },
80  { 0, 1, 3, 4, 2 },
81  { 0, 1, 4, 5, 2, 3 },
82  { 0, 1, 5, 6, 2, 4, 3 },
83  { 0, 1, 6, 7, 4, 5, 2, 3 },
84 };
85 
86 static void libopus_write_header(AVCodecContext *avctx, int stream_count,
87  int coupled_stream_count,
88  int mapping_family,
89  const uint8_t *channel_mapping)
90 {
91  uint8_t *p = avctx->extradata;
92  int channels = avctx->channels;
93 
94  bytestream_put_buffer(&p, "OpusHead", 8);
95  bytestream_put_byte(&p, 1); /* Version */
96  bytestream_put_byte(&p, channels);
97  bytestream_put_le16(&p, avctx->initial_padding); /* Lookahead samples at 48kHz */
98  bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
99  bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
100 
101  /* Channel mapping */
102  bytestream_put_byte(&p, mapping_family);
103  if (mapping_family != 0) {
104  bytestream_put_byte(&p, stream_count);
105  bytestream_put_byte(&p, coupled_stream_count);
106  bytestream_put_buffer(&p, channel_mapping, channels);
107  }
108 }
109 
110 static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
112 {
113  int ret;
114 
115  if (avctx->global_quality) {
116  av_log(avctx, AV_LOG_ERROR,
117  "Quality-based encoding not supported, "
118  "please specify a bitrate and VBR setting.\n");
119  return AVERROR(EINVAL);
120  }
121 
122  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
123  if (ret != OPUS_OK) {
124  av_log(avctx, AV_LOG_ERROR,
125  "Failed to set bitrate: %s\n", opus_strerror(ret));
126  return ret;
127  }
128 
129  ret = opus_multistream_encoder_ctl(enc,
130  OPUS_SET_COMPLEXITY(opts->complexity));
131  if (ret != OPUS_OK)
132  av_log(avctx, AV_LOG_WARNING,
133  "Unable to set complexity: %s\n", opus_strerror(ret));
134 
135  ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
136  if (ret != OPUS_OK)
137  av_log(avctx, AV_LOG_WARNING,
138  "Unable to set VBR: %s\n", opus_strerror(ret));
139 
140  ret = opus_multistream_encoder_ctl(enc,
141  OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
142  if (ret != OPUS_OK)
143  av_log(avctx, AV_LOG_WARNING,
144  "Unable to set constrained VBR: %s\n", opus_strerror(ret));
145 
146  ret = opus_multistream_encoder_ctl(enc,
147  OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
148  if (ret != OPUS_OK)
149  av_log(avctx, AV_LOG_WARNING,
150  "Unable to set expected packet loss percentage: %s\n",
151  opus_strerror(ret));
152 
153  ret = opus_multistream_encoder_ctl(enc,
154  OPUS_SET_INBAND_FEC(opts->fec));
155  if (ret != OPUS_OK)
156  av_log(avctx, AV_LOG_WARNING,
157  "Unable to set inband FEC: %s\n",
158  opus_strerror(ret));
159 
160  if (avctx->cutoff) {
161  ret = opus_multistream_encoder_ctl(enc,
162  OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
163  if (ret != OPUS_OK)
164  av_log(avctx, AV_LOG_WARNING,
165  "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
166  }
167 
168 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
169  ret = opus_multistream_encoder_ctl(enc,
170  OPUS_SET_PHASE_INVERSION_DISABLED(!opts->apply_phase_inv));
171  if (ret != OPUS_OK)
172  av_log(avctx, AV_LOG_WARNING,
173  "Unable to set phase inversion: %s\n",
174  opus_strerror(ret));
175 #endif
176  return OPUS_OK;
177 }
178 
180  int max_channels) {
181  if (avctx->channels > max_channels) {
182  av_log(avctx, AV_LOG_ERROR, "Opus mapping family undefined for %d channels.\n",
183  avctx->channels);
184  return AVERROR(EINVAL);
185  }
186 
187  return 0;
188 }
189 
192 
193  if (!avctx->channel_layout) {
194  av_log(avctx, AV_LOG_WARNING,
195  "No channel layout specified. Opus encoder will use Vorbis "
196  "channel layout for %d channels.\n", avctx->channels);
197  } else if (avctx->channel_layout != ff_vorbis_channel_layouts[avctx->channels - 1]) {
198  char name[32];
199  av_get_channel_layout_string(name, sizeof(name), avctx->channels,
200  avctx->channel_layout);
201  av_log(avctx, AV_LOG_ERROR,
202  "Invalid channel layout %s for specified mapping family %d.\n",
203  name, mapping_family);
204 
205  return AVERROR(EINVAL);
206  }
207 
208  return 0;
209 }
210 
212  AVCodecContext *avctx,
213  int mapping_family,
214  const uint8_t ** channel_map_result)
215 {
216  const uint8_t * channel_map = NULL;
217  int ret;
218 
219  switch (mapping_family) {
220  case -1:
221  ret = libopus_check_max_channels(avctx, 8);
222  if (ret == 0) {
223  ret = libopus_check_vorbis_layout(avctx, mapping_family);
224  /* Channels do not need to be reordered. */
225  }
226 
227  break;
228  case 0:
229  ret = libopus_check_max_channels(avctx, 2);
230  if (ret == 0) {
231  ret = libopus_check_vorbis_layout(avctx, mapping_family);
232  }
233  break;
234  case 1:
235  /* Opus expects channels to be in Vorbis order. */
236  ret = libopus_check_max_channels(avctx, 8);
237  if (ret == 0) {
238  ret = libopus_check_vorbis_layout(avctx, mapping_family);
239  channel_map = ff_vorbis_channel_layout_offsets[avctx->channels - 1];
240  }
241  break;
242  case 255:
243  ret = libopus_check_max_channels(avctx, 254);
244  break;
245  default:
246  av_log(avctx, AV_LOG_WARNING,
247  "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
248  mapping_family);
249  ret = 0;
250  }
251 
252  *channel_map_result = channel_map;
253  return ret;
254 }
255 
257 {
258  LibopusEncContext *opus = avctx->priv_data;
259  OpusMSEncoder *enc;
260  uint8_t libopus_channel_mapping[255];
261  int ret = OPUS_OK;
262  int av_ret;
263  int coupled_stream_count, header_size, frame_size;
264  int mapping_family;
265 
266  frame_size = opus->opts.frame_duration * 48000 / 1000;
267  switch (frame_size) {
268  case 120:
269  case 240:
270  if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
271  av_log(avctx, AV_LOG_WARNING,
272  "LPC mode cannot be used with a frame duration of less "
273  "than 10ms. Enabling restricted low-delay mode.\n"
274  "Use a longer frame duration if this is not what you want.\n");
275  /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
276  * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
277  opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
278  case 480:
279  case 960:
280  case 1920:
281  case 2880:
282 #ifdef OPUS_FRAMESIZE_120_MS
283  case 3840:
284  case 4800:
285  case 5760:
286 #endif
287  opus->opts.packet_size =
288  avctx->frame_size = frame_size * avctx->sample_rate / 48000;
289  break;
290  default:
291  av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
292  "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
293 #ifdef OPUS_FRAMESIZE_120_MS
294  ", 60, 80, 100 or 120.\n",
295 #else
296  " or 60.\n",
297 #endif
298  opus->opts.frame_duration);
299  return AVERROR(EINVAL);
300  }
301 
302  if (avctx->compression_level < 0 || avctx->compression_level > 10) {
303  av_log(avctx, AV_LOG_WARNING,
304  "Compression level must be in the range 0 to 10. "
305  "Defaulting to 10.\n");
306  opus->opts.complexity = 10;
307  } else {
308  opus->opts.complexity = avctx->compression_level;
309  }
310 
311  if (avctx->cutoff) {
312  switch (avctx->cutoff) {
313  case 4000:
315  break;
316  case 6000:
318  break;
319  case 8000:
321  break;
322  case 12000:
324  break;
325  case 20000:
327  break;
328  default:
329  av_log(avctx, AV_LOG_WARNING,
330  "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
331  "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
332  avctx->cutoff);
333  avctx->cutoff = 0;
334  }
335  }
336 
337  /* Channels may need to be reordered to match opus mapping. */
339  &opus->encoder_channel_map);
340  if (av_ret) {
341  return av_ret;
342  }
343 
344  if (opus->opts.mapping_family == -1) {
345  /* By default, use mapping family 1 for the header but use the older
346  * libopus multistream API to avoid surround masking. */
347 
348  /* Set the mapping family so that the value is correct in the header */
349  mapping_family = avctx->channels > 2 ? 1 : 0;
350  coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
351  opus->stream_count = avctx->channels - coupled_stream_count;
352  memcpy(libopus_channel_mapping,
353  opus_vorbis_channel_map[avctx->channels - 1],
354  avctx->channels * sizeof(*libopus_channel_mapping));
355 
356  enc = opus_multistream_encoder_create(
357  avctx->sample_rate, avctx->channels, opus->stream_count,
358  coupled_stream_count,
360  opus->opts.application, &ret);
361  } else {
362  /* Use the newer multistream API. The encoder will set the channel
363  * mapping and coupled stream counts to its internal defaults and will
364  * use surround masking analysis to save bits. */
365  mapping_family = opus->opts.mapping_family;
366  enc = opus_multistream_surround_encoder_create(
367  avctx->sample_rate, avctx->channels, mapping_family,
368  &opus->stream_count, &coupled_stream_count, libopus_channel_mapping,
369  opus->opts.application, &ret);
370  }
371 
372  if (ret != OPUS_OK) {
373  av_log(avctx, AV_LOG_ERROR,
374  "Failed to create encoder: %s\n", opus_strerror(ret));
375  return ff_opus_error_to_averror(ret);
376  }
377 
378  if (!avctx->bit_rate) {
379  /* Sane default copied from opusenc */
380  avctx->bit_rate = 64000 * opus->stream_count +
381  32000 * coupled_stream_count;
382  av_log(avctx, AV_LOG_WARNING,
383  "No bit rate set. Defaulting to %"PRId64" bps.\n", avctx->bit_rate);
384  }
385 
386  if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
387  av_log(avctx, AV_LOG_ERROR, "The bit rate %"PRId64" bps is unsupported. "
388  "Please choose a value between 500 and %d.\n", avctx->bit_rate,
389  256000 * avctx->channels);
390  ret = AVERROR(EINVAL);
391  goto fail;
392  }
393 
394  ret = libopus_configure_encoder(avctx, enc, &opus->opts);
395  if (ret != OPUS_OK) {
396  ret = ff_opus_error_to_averror(ret);
397  goto fail;
398  }
399 
400  /* Header includes channel mapping table if and only if mapping family is NOT 0 */
401  header_size = 19 + (mapping_family == 0 ? 0 : 2 + avctx->channels);
402  avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
403  if (!avctx->extradata) {
404  av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
405  ret = AVERROR(ENOMEM);
406  goto fail;
407  }
408  avctx->extradata_size = header_size;
409 
410  opus->samples = av_mallocz_array(frame_size, avctx->channels *
412  if (!opus->samples) {
413  av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
414  ret = AVERROR(ENOMEM);
415  goto fail;
416  }
417 
418  ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
419  if (ret != OPUS_OK)
420  av_log(avctx, AV_LOG_WARNING,
421  "Unable to get number of lookahead samples: %s\n",
422  opus_strerror(ret));
423 
424  libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
425  mapping_family, libopus_channel_mapping);
426 
427  ff_af_queue_init(avctx, &opus->afq);
428 
429  opus->enc = enc;
430 
431  return 0;
432 
433 fail:
434  opus_multistream_encoder_destroy(enc);
435  av_freep(&avctx->extradata);
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_packet2(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count, 0)) < 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));
502  return ff_opus_error_to_averror(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  av_freep(&avctx->extradata);
542 
543  return 0;
544 }
545 
546 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
547 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
548 static const AVOption libopus_options[] = {
549  { "application", "Intended application type", OFFSET(application), AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
550  { "voip", "Favor improved speech intelligibility", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0, FLAGS, "application" },
551  { "audio", "Favor faithfulness to the input", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0, FLAGS, "application" },
552  { "lowdelay", "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
553  { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0, FLAGS },
554  { "packet_loss", "Expected packet loss percentage", OFFSET(packet_loss), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, FLAGS },
555  { "fec", "Enable inband FEC. Expected packet loss must be non-zero", OFFSET(fec), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
556  { "vbr", "Variable bit rate mode", OFFSET(vbr), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 2, FLAGS, "vbr" },
557  { "off", "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
558  { "on", "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
559  { "constrained", "Use constrained VBR", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
560  { "mapping_family", "Channel Mapping Family", OFFSET(mapping_family), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, FLAGS, "mapping_family" },
561 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
562  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
563 #endif
564  { NULL },
565 };
566 
567 static const AVClass libopus_class = {
568  .class_name = "libopus",
569  .item_name = av_default_item_name,
570  .option = libopus_options,
571  .version = LIBAVUTIL_VERSION_INT,
572 };
573 
575  { "b", "0" },
576  { "compression_level", "10" },
577  { NULL },
578 };
579 
580 static const int libopus_sample_rates[] = {
581  48000, 24000, 16000, 12000, 8000, 0,
582 };
583 
585  .name = "libopus",
586  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
587  .type = AVMEDIA_TYPE_AUDIO,
588  .id = AV_CODEC_ID_OPUS,
589  .priv_data_size = sizeof(LibopusEncContext),
591  .encode2 = libopus_encode,
592  .close = libopus_encode_close,
594  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
597  .supported_samplerates = libopus_sample_rates,
598  .priv_class = &libopus_class,
599  .defaults = libopus_defaults,
600  .wrapper_name = "libopus",
601 };
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc, LibopusEncOpts *opts)
Definition: libopusenc.c:110
AVOption.
Definition: opt.h:248
static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family)
Definition: libopusenc.c:190
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int libopus_validate_layout_and_get_channel_map(AVCodecContext *avctx, int mapping_family, const uint8_t **channel_map_result)
Definition: libopusenc.c:211
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:86
#define OFFSET(x)
Definition: libopusenc.c:546
int size
Definition: packet.h:364
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
uint8_t * samples
Definition: libopusenc.c:52
#define sample
AVCodec.
Definition: codec.h:190
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
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:75
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOptions.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:381
static const AVClass libopus_class
Definition: libopusenc.c:567
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
const uint8_t * encoder_channel_map
Definition: libopusenc.c:55
uint8_t * data
Definition: packet.h:363
channels
Definition: aptx.h:33
#define av_log(a,...)
OpusMSEncoder * enc
Definition: libopusenc.c:50
#define src
Definition: vp8dsp.c:254
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static int libopus_check_max_channels(AVCodecContext *avctx, int max_channels)
Definition: libopusenc.c:179
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
static const uint8_t libavcodec_libopus_channel_map[8][8]
Definition: libopusenc.c:75
int initial_padding
Audio only.
Definition: avcodec.h:2060
const char * name
Name of the codec implementation.
Definition: codec.h:197
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define fail()
Definition: checkasm.h:123
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
static const uint8_t opus_coupled_streams[8]
Definition: libopusenc.c:58
AVDictionary * opts
Definition: movenc.c:50
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:80
static const int libopus_sample_rates[]
Definition: libopusenc.c:580
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
#define FF_ARRAY_ELEMS(a)
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.
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
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
static av_cold int libopus_encode_init(AVCodecContext *avctx)
Definition: libopusenc.c:256
int frame_size
Definition: mxfenc.c:2166
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int compression_level
Definition: avcodec.h:598
int sample_rate
samples per second
Definition: avcodec.h:1186
main external API structure.
Definition: avcodec.h:526
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libopusenc.c:453
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
AVCodec ff_libopus_encoder
Definition: libopusenc.c:584
int extradata_size
Definition: avcodec.h:628
Describe the class of an AVClass context structure.
Definition: log.h:67
Recommmends skipping the specified number of samples.
Definition: packet.h:156
int mapping_family
Definition: libopusenc.c:42
static av_cold int libopus_encode_close(AVCodecContext *avctx)
Definition: libopusenc.c:532
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:592
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
static const uint8_t opus_vorbis_channel_map[8][8]
Definition: libopusenc.c:63
LibopusEncOpts opts
Definition: libopusenc.c:53
common internal api header.
AudioFrameQueue afq
Definition: libopusenc.c:54
signed 16 bits
Definition: samplefmt.h:61
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
static const AVCodecDefault libopus_defaults[]
Definition: libopusenc.c:574
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
void * priv_data
Definition: avcodec.h:553
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1230
int channels
number of audio channels
Definition: avcodec.h:1187
float frame_duration
Definition: libopusenc.c:39
static const AVOption libopus_options[]
Definition: libopusenc.c:548
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:836
#define av_freep(p)
int nb_channels
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:332
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
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
#define FLAGS
Definition: libopusenc.c:547
This structure stores compressed data.
Definition: packet.h:340
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:374
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
const char * name
Definition: opengl_enc.c:102