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