FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
dec.c
Go to the documentation of this file.
1 /*
2  * Opus decoder
3  * Copyright (c) 2012 Andrew D'Addesio
4  * Copyright (c) 2013-2014 Mozilla Corporation
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Opus decoder
26  * @author Andrew D'Addesio, Anton Khirnov
27  *
28  * Codec homepage: http://opus-codec.org/
29  * Specification: http://tools.ietf.org/html/rfc6716
30  * Ogg Opus specification: https://tools.ietf.org/html/draft-ietf-codec-oggopus-03
31  *
32  * Ogg-contained .opus files can be produced with opus-tools:
33  * http://git.xiph.org/?p=opus-tools.git
34  */
35 
36 #include <stdint.h>
37 
38 #include "libavutil/attributes.h"
39 #include "libavutil/audio_fifo.h"
41 #include "libavutil/ffmath.h"
42 #include "libavutil/float_dsp.h"
43 #include "libavutil/frame.h"
44 #include "libavutil/mem.h"
45 #include "libavutil/mem_internal.h"
46 #include "libavutil/opt.h"
47 
49 
50 #include "avcodec.h"
51 #include "codec_internal.h"
52 #include "decode.h"
53 #include "opus.h"
54 #include "tab.h"
55 #include "celt.h"
56 #include "parse.h"
57 #include "rc.h"
58 #include "silk.h"
59 
60 static const uint16_t silk_frame_duration_ms[16] = {
61  10, 20, 40, 60,
62  10, 20, 40, 60,
63  10, 20, 40, 60,
64  10, 20,
65  10, 20,
66 };
67 
68 /* number of samples of silence to feed to the resampler
69  * at the beginning */
70 static const int silk_resample_delay[] = {
71  4, 8, 11, 11, 11
72 };
73 
74 typedef struct OpusStreamContext {
77 
78  /* number of decoded samples for this stream */
80  /* current output buffers for this stream */
81  float *out[2];
82  int out_size;
83  /* Buffer with samples from this stream for synchronizing
84  * the streams when they have different resampling delays */
86 
92 
93  float silk_buf[2][960];
94  float *silk_output[2];
95  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
96  float *celt_output[2];
97 
98  DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
99  float *redundancy_output[2];
100 
101  /* buffers for the next samples to be decoded */
102  float *cur_out[2];
104 
105  float *out_dummy;
107 
111  /* number of samples we still want to get from the resampler */
113 
115 
118 
119 typedef struct OpusContext {
121 
124 
126  float gain;
127 
129 } OpusContext;
130 
132 {
133  if (config < 4)
134  return 8000;
135  else if (config < 8)
136  return 12000;
137  return 16000;
138 }
139 
140 static void opus_fade(float *out,
141  const float *in1, const float *in2,
142  const float *window, int len)
143 {
144  int i;
145  for (i = 0; i < len; i++)
146  out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
147 }
148 
149 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
150 {
151  int celt_size = av_audio_fifo_size(s->celt_delay);
152  int ret, i;
153  ret = swr_convert(s->swr,
154  (uint8_t**)s->cur_out, nb_samples,
155  NULL, 0);
156  if (ret < 0)
157  return ret;
158  else if (ret != nb_samples) {
159  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
160  ret);
161  return AVERROR_BUG;
162  }
163 
164  if (celt_size) {
165  if (celt_size != nb_samples) {
166  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
167  return AVERROR_BUG;
168  }
169  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
170  for (i = 0; i < s->output_channels; i++) {
171  s->fdsp->vector_fmac_scalar(s->cur_out[i],
172  s->celt_output[i], 1.0,
173  nb_samples);
174  }
175  }
176 
177  if (s->redundancy_idx) {
178  for (i = 0; i < s->output_channels; i++)
179  opus_fade(s->cur_out[i], s->cur_out[i],
180  s->redundancy_output[i] + 120 + s->redundancy_idx,
181  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
182  s->redundancy_idx = 0;
183  }
184 
185  s->cur_out[0] += nb_samples;
186  s->cur_out[1] += nb_samples;
187  s->remaining_out_size -= nb_samples * sizeof(float);
188 
189  return 0;
190 }
191 
193 {
194  static const float delay[16] = { 0.0 };
195  const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
196  int ret;
197 
198  av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
199  ret = swr_init(s->swr);
200  if (ret < 0) {
201  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
202  return ret;
203  }
204 
205  ret = swr_convert(s->swr,
206  NULL, 0,
207  delayptr, silk_resample_delay[s->packet.bandwidth]);
208  if (ret < 0) {
209  av_log(s->avctx, AV_LOG_ERROR,
210  "Error feeding initial silence to the resampler.\n");
211  return ret;
212  }
213 
214  return 0;
215 }
216 
217 static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
218 {
219  int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
220  if (ret < 0)
221  goto fail;
222  ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
223 
224  ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
225  s->redundancy_output,
226  s->packet.stereo + 1, 240,
227  0, ff_celt_band_end[s->packet.bandwidth]);
228  if (ret < 0)
229  goto fail;
230 
231  return 0;
232 fail:
233  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
234  return ret;
235 }
236 
237 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
238 {
239  int samples = s->packet.frame_duration;
240  int redundancy = 0;
241  int redundancy_size, redundancy_pos;
242  int ret, i, consumed;
243  int delayed_samples = s->delayed_samples;
244 
245  ret = ff_opus_rc_dec_init(&s->rc, data, size);
246  if (ret < 0)
247  return ret;
248 
249  /* decode the silk frame */
250  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
251  if (!swr_is_initialized(s->swr)) {
253  if (ret < 0)
254  return ret;
255  }
256 
257  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
258  FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
259  s->packet.stereo + 1,
260  silk_frame_duration_ms[s->packet.config]);
261  if (samples < 0) {
262  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
263  return samples;
264  }
265  samples = swr_convert(s->swr,
266  (uint8_t**)s->cur_out, s->packet.frame_duration,
267  (const uint8_t**)s->silk_output, samples);
268  if (samples < 0) {
269  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
270  return samples;
271  }
272  av_assert2((samples & 7) == 0);
273  s->delayed_samples += s->packet.frame_duration - samples;
274  } else
275  ff_silk_flush(s->silk);
276 
277  // decode redundancy information
278  consumed = opus_rc_tell(&s->rc);
279  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
280  redundancy = ff_opus_rc_dec_log(&s->rc, 12);
281  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
282  redundancy = 1;
283 
284  if (redundancy) {
285  redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
286 
287  if (s->packet.mode == OPUS_MODE_HYBRID)
288  redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
289  else
290  redundancy_size = size - (consumed + 7) / 8;
291  size -= redundancy_size;
292  if (size < 0) {
293  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
294  return AVERROR_INVALIDDATA;
295  }
296 
297  if (redundancy_pos) {
298  ret = opus_decode_redundancy(s, data + size, redundancy_size);
299  if (ret < 0)
300  return ret;
301  ff_celt_flush(s->celt);
302  }
303  }
304 
305  /* decode the CELT frame */
306  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
307  float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
308  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
309  out_tmp : s->celt_output;
310  int celt_output_samples = samples;
311  int delay_samples = av_audio_fifo_size(s->celt_delay);
312 
313  if (delay_samples) {
314  if (s->packet.mode == OPUS_MODE_HYBRID) {
315  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
316 
317  for (i = 0; i < s->output_channels; i++) {
318  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
319  delay_samples);
320  out_tmp[i] += delay_samples;
321  }
322  celt_output_samples -= delay_samples;
323  } else {
324  av_log(s->avctx, AV_LOG_WARNING,
325  "Spurious CELT delay samples present.\n");
326  av_audio_fifo_reset(s->celt_delay);
327  if (s->avctx->err_recognition & AV_EF_EXPLODE)
328  return AVERROR_BUG;
329  }
330  }
331 
333 
334  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
335  s->packet.stereo + 1,
336  s->packet.frame_duration,
337  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
338  ff_celt_band_end[s->packet.bandwidth]);
339  if (ret < 0)
340  return ret;
341 
342  if (s->packet.mode == OPUS_MODE_HYBRID) {
343  int celt_delay = s->packet.frame_duration - celt_output_samples;
344  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
345  s->celt_output[1] + celt_output_samples };
346 
347  for (i = 0; i < s->output_channels; i++) {
348  s->fdsp->vector_fmac_scalar(out_tmp[i],
349  s->celt_output[i], 1.0,
350  celt_output_samples);
351  }
352 
353  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
354  if (ret < 0)
355  return ret;
356  }
357  } else
358  ff_celt_flush(s->celt);
359 
360  if (s->redundancy_idx) {
361  for (i = 0; i < s->output_channels; i++)
362  opus_fade(s->cur_out[i], s->cur_out[i],
363  s->redundancy_output[i] + 120 + s->redundancy_idx,
364  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
365  s->redundancy_idx = 0;
366  }
367  if (redundancy) {
368  if (!redundancy_pos) {
369  ff_celt_flush(s->celt);
370  ret = opus_decode_redundancy(s, data + size, redundancy_size);
371  if (ret < 0)
372  return ret;
373 
374  for (i = 0; i < s->output_channels; i++) {
375  opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
376  s->cur_out[i] + samples - 120 + delayed_samples,
377  s->redundancy_output[i] + 120,
379  if (delayed_samples)
380  s->redundancy_idx = 120 - delayed_samples;
381  }
382  } else {
383  for (i = 0; i < s->output_channels; i++) {
384  memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
385  opus_fade(s->cur_out[i] + 120 + delayed_samples,
386  s->redundancy_output[i] + 120,
387  s->cur_out[i] + 120 + delayed_samples,
388  ff_celt_window2, 120);
389  }
390  }
391  }
392 
393  return samples;
394 }
395 
396 static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
397 {
398  int output_samples = 0;
399  int flush_needed = 0;
400  int i, j, ret;
401 
402  s->cur_out[0] = s->out[0];
403  s->cur_out[1] = s->out[1];
404  s->remaining_out_size = s->out_size;
405 
406  /* check if we need to flush the resampler */
407  if (swr_is_initialized(s->swr)) {
408  if (buf) {
409  int64_t cur_samplerate;
410  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
411  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
412  } else {
413  flush_needed = !!s->delayed_samples;
414  }
415  }
416 
417  if (!buf && !flush_needed)
418  return 0;
419 
420  /* use dummy output buffers if the channel is not mapped to anything */
421  if (!s->cur_out[0] ||
422  (s->output_channels == 2 && !s->cur_out[1])) {
423  av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
424  s->remaining_out_size);
425  if (!s->out_dummy)
426  return AVERROR(ENOMEM);
427  if (!s->cur_out[0])
428  s->cur_out[0] = s->out_dummy;
429  if (!s->cur_out[1])
430  s->cur_out[1] = s->out_dummy;
431  }
432 
433  /* flush the resampler if necessary */
434  if (flush_needed) {
435  ret = opus_flush_resample(s, s->delayed_samples);
436  if (ret < 0) {
437  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
438  return ret;
439  }
440  swr_close(s->swr);
441  output_samples += s->delayed_samples;
442  s->delayed_samples = 0;
443 
444  if (!buf)
445  goto finish;
446  }
447 
448  /* decode all the frames in the packet */
449  for (i = 0; i < s->packet.frame_count; i++) {
450  int size = s->packet.frame_size[i];
451  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
452 
453  if (samples < 0) {
454  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
455  if (s->avctx->err_recognition & AV_EF_EXPLODE)
456  return samples;
457 
458  for (j = 0; j < s->output_channels; j++)
459  memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
460  samples = s->packet.frame_duration;
461  }
462  output_samples += samples;
463 
464  for (j = 0; j < s->output_channels; j++)
465  s->cur_out[j] += samples;
466  s->remaining_out_size -= samples * sizeof(float);
467  }
468 
469 finish:
470  s->cur_out[0] = s->cur_out[1] = NULL;
471  s->remaining_out_size = 0;
472 
473  return output_samples;
474 }
475 
477  int *got_frame_ptr, AVPacket *avpkt)
478 {
480  const uint8_t *buf = avpkt->data;
481  int buf_size = avpkt->size;
482  int coded_samples = 0;
483  int decoded_samples = INT_MAX;
484  int delayed_samples = 0;
485  int i, ret;
486 
487  /* calculate the number of delayed samples */
488  for (int i = 0; i < c->p.nb_streams; i++) {
489  OpusStreamContext *s = &c->streams[i];
490  s->out[0] =
491  s->out[1] = NULL;
492  int fifo_samples = av_audio_fifo_size(s->sync_buffer);
494  s->delayed_samples + fifo_samples);
495  }
496 
497  /* decode the header of the first sub-packet to find out the sample count */
498  if (buf) {
499  OpusPacket *pkt = &c->streams[0].packet;
500  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
501  if (ret < 0) {
502  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
503  return ret;
504  }
505  coded_samples += pkt->frame_count * pkt->frame_duration;
506  c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
507  }
508 
509  frame->nb_samples = coded_samples + delayed_samples;
510 
511  /* no input or buffered data => nothing to do */
512  if (!frame->nb_samples) {
513  *got_frame_ptr = 0;
514  return 0;
515  }
516 
517  /* setup the data buffers */
518  ret = ff_get_buffer(avctx, frame, 0);
519  if (ret < 0)
520  return ret;
521  frame->nb_samples = 0;
522 
523  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
524  ChannelMap *map = &c->p.channel_maps[i];
525  if (!map->copy)
526  c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
527  }
528 
529  /* read the data from the sync buffers */
530  for (int i = 0; i < c->p.nb_streams; i++) {
531  OpusStreamContext *s = &c->streams[i];
532  float **out = s->out;
533  int sync_size = av_audio_fifo_size(s->sync_buffer);
534 
535  float sync_dummy[32];
536  int out_dummy = (!out[0]) | ((!out[1]) << 1);
537 
538  if (!out[0])
539  out[0] = sync_dummy;
540  if (!out[1])
541  out[1] = sync_dummy;
542  if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
543  return AVERROR_BUG;
544 
545  ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
546  if (ret < 0)
547  return ret;
548 
549  if (out_dummy & 1)
550  out[0] = NULL;
551  else
552  out[0] += ret;
553  if (out_dummy & 2)
554  out[1] = NULL;
555  else
556  out[1] += ret;
557 
558  s->out_size = frame->linesize[0] - ret * sizeof(float);
559  }
560 
561  /* decode each sub-packet */
562  for (int i = 0; i < c->p.nb_streams; i++) {
563  OpusStreamContext *s = &c->streams[i];
564 
565  if (i && buf) {
566  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
567  if (ret < 0) {
568  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
569  return ret;
570  }
571  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
573  "Mismatching coded sample count in substream %d.\n", i);
574  return AVERROR_INVALIDDATA;
575  }
576 
577  s->silk_samplerate = get_silk_samplerate(s->packet.config);
578  }
579 
580  ret = opus_decode_subpacket(&c->streams[i], buf);
581  if (ret < 0)
582  return ret;
583  s->decoded_samples = ret;
585 
586  if (!buf)
587  continue;
588 
589  buf += s->packet.packet_size;
590  buf_size -= s->packet.packet_size;
591  }
592 
593  /* buffer the extra samples */
594  for (int i = 0; i < c->p.nb_streams; i++) {
595  OpusStreamContext *s = &c->streams[i];
596  int buffer_samples = s->decoded_samples - decoded_samples;
597  if (buffer_samples) {
598  float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
599  s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
600  buf[0] += decoded_samples;
601  buf[1] += decoded_samples;
602  ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
603  if (ret < 0)
604  return ret;
605  }
606  }
607 
608  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
609  ChannelMap *map = &c->p.channel_maps[i];
610 
611  /* handle copied channels */
612  if (map->copy) {
613  memcpy(frame->extended_data[i],
614  frame->extended_data[map->copy_idx],
615  frame->linesize[0]);
616  } else if (map->silence) {
617  memset(frame->extended_data[i], 0, frame->linesize[0]);
618  }
619 
620  if (c->p.gain_i && decoded_samples > 0) {
621  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
622  (float*)frame->extended_data[i],
623  c->gain, FFALIGN(decoded_samples, 8));
624  }
625  }
626 
627  frame->nb_samples = decoded_samples;
628  *got_frame_ptr = !!decoded_samples;
629 
630  return avpkt->size;
631 }
632 
634 {
636 
637  for (int i = 0; i < c->p.nb_streams; i++) {
638  OpusStreamContext *s = &c->streams[i];
639 
640  memset(&s->packet, 0, sizeof(s->packet));
641  s->delayed_samples = 0;
642 
643  av_audio_fifo_reset(s->celt_delay);
644  swr_close(s->swr);
645 
646  av_audio_fifo_reset(s->sync_buffer);
647 
648  ff_silk_flush(s->silk);
649  ff_celt_flush(s->celt);
650  }
651 }
652 
654 {
656 
657  for (int i = 0; i < c->p.nb_streams; i++) {
658  OpusStreamContext *s = &c->streams[i];
659 
660  ff_silk_free(&s->silk);
661  ff_celt_free(&s->celt);
662 
663  av_freep(&s->out_dummy);
664  s->out_dummy_allocated_size = 0;
665 
666  av_audio_fifo_free(s->sync_buffer);
667  av_audio_fifo_free(s->celt_delay);
668  swr_free(&s->swr);
669  }
670 
671  av_freep(&c->streams);
672 
673  c->p.nb_streams = 0;
674 
675  av_freep(&c->p.channel_maps);
676  av_freep(&c->fdsp);
677 
678  return 0;
679 }
680 
682 {
684  int ret;
685 
687  avctx->sample_rate = 48000;
688 
689  c->fdsp = avpriv_float_dsp_alloc(0);
690  if (!c->fdsp)
691  return AVERROR(ENOMEM);
692 
693  /* find out the channel configuration */
695  if (ret < 0)
696  return ret;
697  if (c->p.gain_i)
698  c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
699 
700  /* allocate and init each independent decoder */
701  c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
702  if (!c->streams) {
703  c->p.nb_streams = 0;
704  return AVERROR(ENOMEM);
705  }
706 
707  for (int i = 0; i < c->p.nb_streams; i++) {
708  OpusStreamContext *s = &c->streams[i];
710 
711  s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
712 
713  s->avctx = avctx;
714 
715  for (int j = 0; j < s->output_channels; j++) {
716  s->silk_output[j] = s->silk_buf[j];
717  s->celt_output[j] = s->celt_buf[j];
718  s->redundancy_output[j] = s->redundancy_buf[j];
719  }
720 
721  s->fdsp = c->fdsp;
722 
723  s->swr =swr_alloc();
724  if (!s->swr)
725  return AVERROR(ENOMEM);
726 
727  layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
729  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
730  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
731  av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
732  av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
733  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
734  av_opt_set_int(s->swr, "filter_size", 16, 0);
735 
736  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
737  if (ret < 0)
738  return ret;
739 
740  ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
741  if (ret < 0)
742  return ret;
743 
744  s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
745  s->output_channels, 1024);
746  if (!s->celt_delay)
747  return AVERROR(ENOMEM);
748 
749  s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
750  s->output_channels, 32);
751  if (!s->sync_buffer)
752  return AVERROR(ENOMEM);
753  }
754 
755  return 0;
756 }
757 
758 #define OFFSET(x) offsetof(OpusContext, x)
759 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
760 static const AVOption opus_options[] = {
761  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
762  { NULL },
763 };
764 
765 static const AVClass opus_class = {
766  .class_name = "Opus Decoder",
767  .item_name = av_default_item_name,
768  .option = opus_options,
769  .version = LIBAVUTIL_VERSION_INT,
770 };
771 
773  .p.name = "opus",
774  CODEC_LONG_NAME("Opus"),
775  .p.priv_class = &opus_class,
776  .p.type = AVMEDIA_TYPE_AUDIO,
777  .p.id = AV_CODEC_ID_OPUS,
778  .priv_data_size = sizeof(OpusContext),
782  .flush = opus_decode_flush,
784  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
785 };
OpusStreamContext::decoded_samples
int decoded_samples
Definition: dec.c:79
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:48
silk_resample_delay
static const int silk_resample_delay[]
Definition: dec.c:70
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
opus_decode_init
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: dec.c:681
ff_exp10
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
OpusStreamContext::avctx
AVCodecContext * avctx
Definition: dec.c:75
opus_fade
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition: dec.c:140
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_celt_decode_frame
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, int coded_channels, int frame_size, int startband, int endband)
Definition: dec_celt.c:323
mem_internal.h
opus_decode_subpacket
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
Definition: dec.c:396
out
FILE * out
Definition: movenc.c:55
OpusStreamContext::delayed_samples
int delayed_samples
Definition: dec.c:112
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:395
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1024
opus_rc_tell
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame,...
Definition: rc.h:62
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:119
int64_t
long long int64_t
Definition: coverity.c:34
opus_decode_flush
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition: dec.c:633
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:421
AVPacket::data
uint8_t * data
Definition: packet.h:552
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:149
opus.h
FFCodec
Definition: codec_internal.h:127
OFFSET
#define OFFSET(x)
Definition: dec.c:758
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
OpusStreamContext::rc
OpusRangeCoder rc
Definition: dec.c:87
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
rc.h
tf_sess_config.config
config
Definition: tf_sess_config.py:33
OpusStreamContext::celt_delay
AVAudioFifo * celt_delay
Definition: dec.c:109
OpusContext::p
OpusParseContext p
Definition: dec.c:128
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:135
OpusStreamContext::swr
SwrContext * swr
Definition: dec.c:108
window
static SDL_Window * window
Definition: ffplay.c:361
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:37
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
finish
static void finish(void)
Definition: movenc.c:374
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
fail
#define fail()
Definition: checkasm.h:199
OpusStreamContext::out_size
int out_size
Definition: dec.c:82
OpusContext::gain
float gain
Definition: dec.c:126
ff_opus_rc_dec_uint
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition: rc.c:182
swr_is_initialized
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
Definition: swresample.c:715
OpusStreamContext::celt
CeltFrame * celt
Definition: dec.c:90
OpusStreamContext::redundancy_buf
float redundancy_buf[2][960]
Definition: dec.c:98
ff_silk_free
void ff_silk_free(SilkContext **ps)
Definition: silk.c:870
OpusStreamContext::celt_output
float * celt_output[2]
Definition: dec.c:96
opus_decode_frame
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition: dec.c:237
swr_convert
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
Definition: swresample.c:719
OpusStreamContext::out
float * out[2]
Definition: dec.c:81
pkt
AVPacket * pkt
Definition: movenc.c:60
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:90
parse.h
OpusStreamContext::silk_output
float * silk_output[2]
Definition: dec.c:94
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:140
float
float
Definition: af_crystalizer.c:122
OpusStreamContext
Definition: dec.c:74
OpusContext::apply_phase_inv
int apply_phase_inv
Definition: dec.c:123
OpusStreamContext::remaining_out_size
int remaining_out_size
Definition: dec.c:103
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:341
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_opus_parse_extradata
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s)
Definition: parse.c:296
OpusStreamContext::fdsp
AVFloatDSPContext * fdsp
Definition: dec.c:91
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
swr_alloc
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:148
OpusStreamContext::redundancy_idx
int redundancy_idx
Definition: dec.c:116
OpusContext::av_class
AVClass * av_class
Definition: dec.c:120
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
OpusStreamContext::sync_buffer
AVAudioFifo * sync_buffer
Definition: dec.c:85
ff_celt_init
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition: dec_celt.c:545
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
ff_celt_flush
void ff_celt_flush(CeltFrame *f)
Definition: dec_celt.c:497
opus_options
static const AVOption opus_options[]
Definition: dec.c:760
SilkContext
Definition: silk.c:51
OPUS_BANDWIDTH_WIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
Definition: opus.h:52
OPUS_MODE_CELT
@ OPUS_MODE_CELT
Definition: opus.h:44
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
opus_class
static const AVClass opus_class
Definition: dec.c:765
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
av_audio_fifo_alloc
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:62
OpusStreamContext::silk_samplerate
int silk_samplerate
Definition: dec.c:110
ff_opus_rc_dec_log
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition: rc.c:114
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:240
OpusStreamContext::packet
OpusPacket packet
Definition: dec.c:114
OpusParseContext
Definition: parse.h:63
OpusStreamContext::out_dummy_allocated_size
int out_dummy_allocated_size
Definition: dec.c:106
av_opt_get_int
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:1273
OpusStreamContext::redundancy_rc
OpusRangeCoder redundancy_rc
Definition: dec.c:88
OpusContext::fdsp
AVFloatDSPContext * fdsp
Definition: dec.c:125
ff_celt_free
void ff_celt_free(CeltFrame **f)
Definition: dec_celt.c:528
swresample.h
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_opt_set_int
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:880
float_dsp.h
ff_silk_init
int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
Definition: silk.c:883
ff_silk_flush
void ff_silk_flush(SilkContext *s)
Definition: silk.c:875
opus_init_resample
static int opus_init_resample(OpusStreamContext *s)
Definition: dec.c:192
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:91
silk.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1635
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:553
ff_opus_decoder
const FFCodec ff_opus_decoder
Definition: dec.c:772
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
av_opt_set_chlayout
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition: opt.c:1000
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1031
av_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:175
size
int size
Definition: twinvq_data.h:10344
OpusRangeCoder
Definition: rc.h:41
OpusStreamContext::silk
SilkContext * silk
Definition: dec.c:89
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:121
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:510
AVFloatDSPContext
Definition: float_dsp.h:24
frame.h
OpusContext::streams
struct OpusStreamContext * streams
Definition: dec.c:122
AD
#define AD
Definition: dec.c:759
attributes.h
av_audio_fifo_size
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:222
layout
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 layout
Definition: filter_design.txt:18
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:68
tab.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_opus_rc_dec_raw_init
void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
Definition: rc.c:352
OPUS_MODE_HYBRID
@ OPUS_MODE_HYBRID
Definition: opus.h:43
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
opus_decode_redundancy
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition: dec.c:217
audio_fifo.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
len
int len
Definition: vorbis_enc_data.h:426
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
celt.h
avcodec.h
ff_opus_rc_dec_init
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Definition: rc.c:338
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
AVCodecContext
main external API structure.
Definition: avcodec.h:431
ff_silk_decode_superframe
int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, float *output[2], enum OpusBandwidth bandwidth, int coded_channels, int duration_ms)
Decode the LP layer of one Opus frame (which may correspond to several SILK frames).
Definition: silk.c:792
channel_layout.h
OpusStreamContext::silk_buf
float silk_buf[2][960]
Definition: dec.c:93
OPUS_MODE_SILK
@ OPUS_MODE_SILK
Definition: opus.h:42
OpusPacket
Definition: parse.h:31
silk_frame_duration_ms
static const uint16_t silk_frame_duration_ms[16]
Definition: dec.c:60
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
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ffmath.h
ChannelMap
Definition: parse.h:48
mem.h
OpusStreamContext::out_dummy
float * out_dummy
Definition: dec.c:105
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:394
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
opus_flush_resample
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition: dec.c:149
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
AVPacket
This structure stores compressed data.
Definition: packet.h:529
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
av_fast_malloc
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:557
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
swr_close
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
Definition: swresample.c:136
OpusStreamContext::celt_buf
float celt_buf[2][960]
Definition: dec.c:95
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
OpusStreamContext::cur_out
float * cur_out[2]
Definition: dec.c:102
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_celt_band_end
const uint8_t ff_celt_band_end[]
Definition: tab.c:29
get_silk_samplerate
static int get_silk_samplerate(int config)
Definition: dec.c:131
OpusStreamContext::output_channels
int output_channels
Definition: dec.c:76
opus_decode_close
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: dec.c:653
OpusStreamContext::redundancy_output
float * redundancy_output[2]
Definition: dec.c:99
av_audio_fifo_reset
void av_audio_fifo_reset(AVAudioFifo *af)
Reset the AVAudioFifo buffer.
Definition: audio_fifo.c:212
ff_opus_parse_packet
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition: parse.c:95
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1292
opus_decode_packet
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: dec.c:476
CeltFrame
Definition: celt.h:98
OpusContext
Definition: dec.c:119
ff_celt_window2
const float ff_celt_window2[120]
Definition: tab.c:1130