FFmpeg
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_drain(s->celt_delay, delay_samples);
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 
397  const uint8_t *buf, int buf_size,
398  int nb_samples)
399 {
400  int output_samples = 0;
401  int flush_needed = 0;
402  int i, j, ret;
403 
404  s->cur_out[0] = s->out[0];
405  s->cur_out[1] = s->out[1];
406  s->remaining_out_size = s->out_size;
407 
408  /* check if we need to flush the resampler */
409  if (swr_is_initialized(s->swr)) {
410  if (buf) {
411  int64_t cur_samplerate;
412  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
413  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
414  } else {
415  flush_needed = !!s->delayed_samples;
416  }
417  }
418 
419  if (!buf && !flush_needed)
420  return 0;
421 
422  /* use dummy output buffers if the channel is not mapped to anything */
423  if (!s->cur_out[0] ||
424  (s->output_channels == 2 && !s->cur_out[1])) {
425  av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
426  s->remaining_out_size);
427  if (!s->out_dummy)
428  return AVERROR(ENOMEM);
429  if (!s->cur_out[0])
430  s->cur_out[0] = s->out_dummy;
431  if (!s->cur_out[1])
432  s->cur_out[1] = s->out_dummy;
433  }
434 
435  /* flush the resampler if necessary */
436  if (flush_needed) {
437  ret = opus_flush_resample(s, s->delayed_samples);
438  if (ret < 0) {
439  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
440  return ret;
441  }
442  swr_close(s->swr);
443  output_samples += s->delayed_samples;
444  s->delayed_samples = 0;
445 
446  if (!buf)
447  goto finish;
448  }
449 
450  /* decode all the frames in the packet */
451  for (i = 0; i < s->packet.frame_count; i++) {
452  int size = s->packet.frame_size[i];
453  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
454 
455  if (samples < 0) {
456  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
457  if (s->avctx->err_recognition & AV_EF_EXPLODE)
458  return samples;
459 
460  for (j = 0; j < s->output_channels; j++)
461  memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
462  samples = s->packet.frame_duration;
463  }
464  output_samples += samples;
465 
466  for (j = 0; j < s->output_channels; j++)
467  s->cur_out[j] += samples;
468  s->remaining_out_size -= samples * sizeof(float);
469  }
470 
471 finish:
472  s->cur_out[0] = s->cur_out[1] = NULL;
473  s->remaining_out_size = 0;
474 
475  return output_samples;
476 }
477 
479  int *got_frame_ptr, AVPacket *avpkt)
480 {
482  const uint8_t *buf = avpkt->data;
483  int buf_size = avpkt->size;
484  int coded_samples = 0;
485  int decoded_samples = INT_MAX;
486  int delayed_samples = 0;
487  int i, ret;
488 
489  /* calculate the number of delayed samples */
490  for (int i = 0; i < c->p.nb_streams; i++) {
491  OpusStreamContext *s = &c->streams[i];
492  s->out[0] =
493  s->out[1] = NULL;
495  s->delayed_samples + av_audio_fifo_size(s->sync_buffer));
496  }
497 
498  /* decode the header of the first sub-packet to find out the sample count */
499  if (buf) {
500  OpusPacket *pkt = &c->streams[0].packet;
501  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->p.nb_streams > 1);
502  if (ret < 0) {
503  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
504  return ret;
505  }
506  coded_samples += pkt->frame_count * pkt->frame_duration;
507  c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
508  }
509 
510  frame->nb_samples = coded_samples + delayed_samples;
511 
512  /* no input or buffered data => nothing to do */
513  if (!frame->nb_samples) {
514  *got_frame_ptr = 0;
515  return 0;
516  }
517 
518  /* setup the data buffers */
519  ret = ff_get_buffer(avctx, frame, 0);
520  if (ret < 0)
521  return ret;
522  frame->nb_samples = 0;
523 
524  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
525  ChannelMap *map = &c->p.channel_maps[i];
526  if (!map->copy)
527  c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
528  }
529 
530  /* read the data from the sync buffers */
531  for (int i = 0; i < c->p.nb_streams; i++) {
532  OpusStreamContext *s = &c->streams[i];
533  float **out = s->out;
534  int sync_size = av_audio_fifo_size(s->sync_buffer);
535 
536  float sync_dummy[32];
537  int out_dummy = (!out[0]) | ((!out[1]) << 1);
538 
539  if (!out[0])
540  out[0] = sync_dummy;
541  if (!out[1])
542  out[1] = sync_dummy;
543  if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
544  return AVERROR_BUG;
545 
546  ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
547  if (ret < 0)
548  return ret;
549 
550  if (out_dummy & 1)
551  out[0] = NULL;
552  else
553  out[0] += ret;
554  if (out_dummy & 2)
555  out[1] = NULL;
556  else
557  out[1] += ret;
558 
559  s->out_size = frame->linesize[0] - ret * sizeof(float);
560  }
561 
562  /* decode each sub-packet */
563  for (int i = 0; i < c->p.nb_streams; i++) {
564  OpusStreamContext *s = &c->streams[i];
565 
566  if (i && buf) {
567  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->p.nb_streams - 1);
568  if (ret < 0) {
569  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
570  return ret;
571  }
572  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
574  "Mismatching coded sample count in substream %d.\n", i);
575  return AVERROR_INVALIDDATA;
576  }
577 
578  s->silk_samplerate = get_silk_samplerate(s->packet.config);
579  }
580 
581  ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
582  coded_samples);
583  if (ret < 0)
584  return ret;
585  s->decoded_samples = ret;
587 
588  buf += s->packet.packet_size;
589  buf_size -= s->packet.packet_size;
590  }
591 
592  /* buffer the extra samples */
593  for (int i = 0; i < c->p.nb_streams; i++) {
594  OpusStreamContext *s = &c->streams[i];
595  int buffer_samples = s->decoded_samples - decoded_samples;
596  if (buffer_samples) {
597  float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
598  s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
599  buf[0] += decoded_samples;
600  buf[1] += decoded_samples;
601  ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
602  if (ret < 0)
603  return ret;
604  }
605  }
606 
607  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
608  ChannelMap *map = &c->p.channel_maps[i];
609 
610  /* handle copied channels */
611  if (map->copy) {
612  memcpy(frame->extended_data[i],
613  frame->extended_data[map->copy_idx],
614  frame->linesize[0]);
615  } else if (map->silence) {
616  memset(frame->extended_data[i], 0, frame->linesize[0]);
617  }
618 
619  if (c->p.gain_i && decoded_samples > 0) {
620  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
621  (float*)frame->extended_data[i],
622  c->gain, FFALIGN(decoded_samples, 8));
623  }
624  }
625 
626  frame->nb_samples = decoded_samples;
627  *got_frame_ptr = !!decoded_samples;
628 
629  return avpkt->size;
630 }
631 
633 {
635 
636  for (int i = 0; i < c->p.nb_streams; i++) {
637  OpusStreamContext *s = &c->streams[i];
638 
639  memset(&s->packet, 0, sizeof(s->packet));
640  s->delayed_samples = 0;
641 
642  av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
643  swr_close(s->swr);
644 
645  av_audio_fifo_drain(s->sync_buffer, av_audio_fifo_size(s->sync_buffer));
646 
647  ff_silk_flush(s->silk);
648  ff_celt_flush(s->celt);
649  }
650 }
651 
653 {
655 
656  for (int i = 0; i < c->p.nb_streams; i++) {
657  OpusStreamContext *s = &c->streams[i];
658 
659  ff_silk_free(&s->silk);
660  ff_celt_free(&s->celt);
661 
662  av_freep(&s->out_dummy);
663  s->out_dummy_allocated_size = 0;
664 
665  av_audio_fifo_free(s->sync_buffer);
666  av_audio_fifo_free(s->celt_delay);
667  swr_free(&s->swr);
668  }
669 
670  av_freep(&c->streams);
671 
672  c->p.nb_streams = 0;
673 
674  av_freep(&c->p.channel_maps);
675  av_freep(&c->fdsp);
676 
677  return 0;
678 }
679 
681 {
683  int ret;
684 
686  avctx->sample_rate = 48000;
687 
688  c->fdsp = avpriv_float_dsp_alloc(0);
689  if (!c->fdsp)
690  return AVERROR(ENOMEM);
691 
692  /* find out the channel configuration */
694  if (ret < 0)
695  return ret;
696  if (c->p.gain_i)
697  c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
698 
699  /* allocate and init each independent decoder */
700  c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
701  if (!c->streams) {
702  c->p.nb_streams = 0;
703  return AVERROR(ENOMEM);
704  }
705 
706  for (int i = 0; i < c->p.nb_streams; i++) {
707  OpusStreamContext *s = &c->streams[i];
709 
710  s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
711 
712  s->avctx = avctx;
713 
714  for (int j = 0; j < s->output_channels; j++) {
715  s->silk_output[j] = s->silk_buf[j];
716  s->celt_output[j] = s->celt_buf[j];
717  s->redundancy_output[j] = s->redundancy_buf[j];
718  }
719 
720  s->fdsp = c->fdsp;
721 
722  s->swr =swr_alloc();
723  if (!s->swr)
724  return AVERROR(ENOMEM);
725 
726  layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
728  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
729  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
730  av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
731  av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
732  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
733  av_opt_set_int(s->swr, "filter_size", 16, 0);
734 
735  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
736  if (ret < 0)
737  return ret;
738 
739  ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
740  if (ret < 0)
741  return ret;
742 
743  s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
744  s->output_channels, 1024);
745  if (!s->celt_delay)
746  return AVERROR(ENOMEM);
747 
748  s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
749  s->output_channels, 32);
750  if (!s->sync_buffer)
751  return AVERROR(ENOMEM);
752  }
753 
754  return 0;
755 }
756 
757 #define OFFSET(x) offsetof(OpusContext, x)
758 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
759 static const AVOption opus_options[] = {
760  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
761  { NULL },
762 };
763 
764 static const AVClass opus_class = {
765  .class_name = "Opus Decoder",
766  .item_name = av_default_item_name,
767  .option = opus_options,
768  .version = LIBAVUTIL_VERSION_INT,
769 };
770 
772  .p.name = "opus",
773  CODEC_LONG_NAME("Opus"),
774  .p.priv_class = &opus_class,
775  .p.type = AVMEDIA_TYPE_AUDIO,
776  .p.id = AV_CODEC_ID_OPUS,
777  .priv_data_size = sizeof(OpusContext),
779  .close = opus_decode_close,
781  .flush = opus_decode_flush,
783  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
784 };
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:215
opus_decode_init
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: dec.c:680
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:43
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
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:393
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
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:632
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
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:757
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:327
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
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:373
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:195
fail
#define fail()
Definition: checkasm.h:189
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:209
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:311
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:202
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:296
ff_celt_flush
void ff_celt_flush(CeltFrame *f)
Definition: dec_celt.c:497
opus_options
static const AVOption opus_options[]
Definition: dec.c:759
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:764
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
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:237
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
opus_decode_subpacket
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf, int buf_size, int nb_samples)
Definition: dec.c:396
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:106
silk.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
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:540
ff_opus_decoder
const FFCodec ff_opus_decoder
Definition: dec.c:771
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:109
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
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:1063
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:506
AVFloatDSPContext
Definition: float_dsp.h:24
frame.h
OpusContext::streams
struct OpusStreamContext * streams
Definition: dec.c:122
AD
#define AD
Definition: dec.c:758
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:67
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:194
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:80
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:451
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:392
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:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
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:34
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:146
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:652
OpusStreamContext::redundancy_output
float * redundancy_output[2]
Definition: dec.c:99
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:1328
opus_decode_packet
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: dec.c:478
CeltFrame
Definition: celt.h:98
OpusContext
Definition: dec.c:119
ff_celt_window2
const float ff_celt_window2[120]
Definition: tab.c:1136