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 "libavcodec/avcodec.h"
52 #include "libavcodec/decode.h"
53 
54 #include "opus.h"
55 #include "tab.h"
56 #include "celt.h"
57 #include "parse.h"
58 #include "rc.h"
59 #include "silk.h"
60 
61 static const uint16_t silk_frame_duration_ms[16] = {
62  10, 20, 40, 60,
63  10, 20, 40, 60,
64  10, 20, 40, 60,
65  10, 20,
66  10, 20,
67 };
68 
69 /* number of samples of silence to feed to the resampler
70  * at the beginning */
71 static const int silk_resample_delay[] = {
72  4, 8, 11, 11, 11
73 };
74 
75 typedef struct OpusStreamContext {
78 
79  /* number of decoded samples for this stream */
81  /* current output buffers for this stream */
82  float *out[2];
83  int out_size;
84  /* Buffer with samples from this stream for synchronizing
85  * the streams when they have different resampling delays */
87 
93 
94  float silk_buf[2][960];
95  float *silk_output[2];
96  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
97  float *celt_output[2];
98 
99  DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
100  float *redundancy_output[2];
101 
102  /* buffers for the next samples to be decoded */
103  float *cur_out[2];
105 
106  float *out_dummy;
108 
112  /* number of samples we still want to get from the resampler */
114 
116 
119 
120 typedef struct OpusContext {
122 
125 
127  float gain;
128 
130 } OpusContext;
131 
133 {
134  if (config < 4)
135  return 8000;
136  else if (config < 8)
137  return 12000;
138  return 16000;
139 }
140 
141 static void opus_fade(float *out,
142  const float *in1, const float *in2,
143  const float *window, int len)
144 {
145  int i;
146  for (i = 0; i < len; i++)
147  out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
148 }
149 
150 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
151 {
152  int celt_size = av_audio_fifo_size(s->celt_delay);
153  int ret, i;
154  ret = swr_convert(s->swr,
155  (uint8_t**)s->cur_out, nb_samples,
156  NULL, 0);
157  if (ret < 0)
158  return ret;
159  else if (ret != nb_samples) {
160  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
161  ret);
162  return AVERROR_BUG;
163  }
164 
165  if (celt_size) {
166  if (celt_size != nb_samples) {
167  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
168  return AVERROR_BUG;
169  }
170  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
171  for (i = 0; i < s->output_channels; i++) {
172  s->fdsp->vector_fmac_scalar(s->cur_out[i],
173  s->celt_output[i], 1.0,
174  nb_samples);
175  }
176  }
177 
178  if (s->redundancy_idx) {
179  for (i = 0; i < s->output_channels; i++)
180  opus_fade(s->cur_out[i], s->cur_out[i],
181  s->redundancy_output[i] + 120 + s->redundancy_idx,
182  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
183  s->redundancy_idx = 0;
184  }
185 
186  s->cur_out[0] += nb_samples;
187  s->cur_out[1] += nb_samples;
188  s->remaining_out_size -= nb_samples * sizeof(float);
189 
190  return 0;
191 }
192 
194 {
195  static const float delay[16] = { 0.0 };
196  const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
197  int ret;
198 
199  av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
200  ret = swr_init(s->swr);
201  if (ret < 0) {
202  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
203  return ret;
204  }
205 
206  ret = swr_convert(s->swr,
207  NULL, 0,
208  delayptr, silk_resample_delay[s->packet.bandwidth]);
209  if (ret < 0) {
210  av_log(s->avctx, AV_LOG_ERROR,
211  "Error feeding initial silence to the resampler.\n");
212  return ret;
213  }
214 
215  return 0;
216 }
217 
218 static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
219 {
220  int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
221  if (ret < 0)
222  goto fail;
223  ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
224 
225  ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
226  s->redundancy_output,
227  s->packet.stereo + 1, 240,
228  0, ff_celt_band_end[s->packet.bandwidth]);
229  if (ret < 0)
230  goto fail;
231 
232  return 0;
233 fail:
234  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
235  return ret;
236 }
237 
238 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
239 {
240  int samples = s->packet.frame_duration;
241  int redundancy = 0;
242  int redundancy_size, redundancy_pos;
243  int ret, i, consumed;
244  int delayed_samples = s->delayed_samples;
245 
246  ret = ff_opus_rc_dec_init(&s->rc, data, size);
247  if (ret < 0)
248  return ret;
249 
250  /* decode the silk frame */
251  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
252  if (!swr_is_initialized(s->swr)) {
254  if (ret < 0)
255  return ret;
256  }
257 
258  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
259  FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
260  s->packet.stereo + 1,
261  silk_frame_duration_ms[s->packet.config]);
262  if (samples < 0) {
263  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
264  return samples;
265  }
266  samples = swr_convert(s->swr,
267  (uint8_t**)s->cur_out, s->packet.frame_duration,
268  (const uint8_t**)s->silk_output, samples);
269  if (samples < 0) {
270  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
271  return samples;
272  }
273  av_assert2((samples & 7) == 0);
274  s->delayed_samples += s->packet.frame_duration - samples;
275  } else
276  ff_silk_flush(s->silk);
277 
278  // decode redundancy information
279  consumed = opus_rc_tell(&s->rc);
280  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
281  redundancy = ff_opus_rc_dec_log(&s->rc, 12);
282  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
283  redundancy = 1;
284 
285  if (redundancy) {
286  redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
287 
288  if (s->packet.mode == OPUS_MODE_HYBRID)
289  redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
290  else
291  redundancy_size = size - (consumed + 7) / 8;
292  size -= redundancy_size;
293  if (size < 0) {
294  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
295  return AVERROR_INVALIDDATA;
296  }
297 
298  if (redundancy_pos) {
299  ret = opus_decode_redundancy(s, data + size, redundancy_size);
300  if (ret < 0)
301  return ret;
302  ff_celt_flush(s->celt);
303  }
304  }
305 
306  /* decode the CELT frame */
307  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
308  float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
309  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
310  out_tmp : s->celt_output;
311  int celt_output_samples = samples;
312  int delay_samples = av_audio_fifo_size(s->celt_delay);
313 
314  if (delay_samples) {
315  if (s->packet.mode == OPUS_MODE_HYBRID) {
316  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
317 
318  for (i = 0; i < s->output_channels; i++) {
319  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
320  delay_samples);
321  out_tmp[i] += delay_samples;
322  }
323  celt_output_samples -= delay_samples;
324  } else {
325  av_log(s->avctx, AV_LOG_WARNING,
326  "Spurious CELT delay samples present.\n");
327  av_audio_fifo_reset(s->celt_delay);
328  if (s->avctx->err_recognition & AV_EF_EXPLODE)
329  return AVERROR_BUG;
330  }
331  }
332 
334 
335  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
336  s->packet.stereo + 1,
337  s->packet.frame_duration,
338  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
339  ff_celt_band_end[s->packet.bandwidth]);
340  if (ret < 0)
341  return ret;
342 
343  if (s->packet.mode == OPUS_MODE_HYBRID) {
344  int celt_delay = s->packet.frame_duration - celt_output_samples;
345  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
346  s->celt_output[1] + celt_output_samples };
347 
348  for (i = 0; i < s->output_channels; i++) {
349  s->fdsp->vector_fmac_scalar(out_tmp[i],
350  s->celt_output[i], 1.0,
351  celt_output_samples);
352  }
353 
354  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
355  if (ret < 0)
356  return ret;
357  }
358  } else
359  ff_celt_flush(s->celt);
360 
361  if (s->redundancy_idx) {
362  for (i = 0; i < s->output_channels; i++)
363  opus_fade(s->cur_out[i], s->cur_out[i],
364  s->redundancy_output[i] + 120 + s->redundancy_idx,
365  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
366  s->redundancy_idx = 0;
367  }
368  if (redundancy) {
369  if (!redundancy_pos) {
370  ff_celt_flush(s->celt);
371  ret = opus_decode_redundancy(s, data + size, redundancy_size);
372  if (ret < 0)
373  return ret;
374 
375  for (i = 0; i < s->output_channels; i++) {
376  opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
377  s->cur_out[i] + samples - 120 + delayed_samples,
378  s->redundancy_output[i] + 120,
380  if (delayed_samples)
381  s->redundancy_idx = 120 - delayed_samples;
382  }
383  } else {
384  for (i = 0; i < s->output_channels; i++) {
385  memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
386  opus_fade(s->cur_out[i] + 120 + delayed_samples,
387  s->redundancy_output[i] + 120,
388  s->cur_out[i] + 120 + delayed_samples,
389  ff_celt_window2, 120);
390  }
391  }
392  }
393 
394  return samples;
395 }
396 
397 static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
398 {
399  int output_samples = 0;
400  int flush_needed = 0;
401  int i, j, ret;
402 
403  s->cur_out[0] = s->out[0];
404  s->cur_out[1] = s->out[1];
405  s->remaining_out_size = s->out_size;
406 
407  /* check if we need to flush the resampler */
408  if (swr_is_initialized(s->swr)) {
409  if (buf) {
410  int64_t cur_samplerate;
411  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
412  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
413  } else {
414  flush_needed = !!s->delayed_samples;
415  }
416  }
417 
418  if (!buf && !flush_needed)
419  return 0;
420 
421  /* use dummy output buffers if the channel is not mapped to anything */
422  if (!s->cur_out[0] ||
423  (s->output_channels == 2 && !s->cur_out[1])) {
424  av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
425  s->remaining_out_size);
426  if (!s->out_dummy)
427  return AVERROR(ENOMEM);
428  if (!s->cur_out[0])
429  s->cur_out[0] = s->out_dummy;
430  if (!s->cur_out[1])
431  s->cur_out[1] = s->out_dummy;
432  }
433 
434  /* flush the resampler if necessary */
435  if (flush_needed) {
436  ret = opus_flush_resample(s, s->delayed_samples);
437  if (ret < 0) {
438  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
439  return ret;
440  }
441  swr_close(s->swr);
442  output_samples += s->delayed_samples;
443  s->delayed_samples = 0;
444 
445  if (!buf)
446  goto finish;
447  }
448 
449  /* decode all the frames in the packet */
450  for (i = 0; i < s->packet.frame_count; i++) {
451  int size = s->packet.frame_size[i];
452  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
453 
454  if (samples < 0) {
455  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
456  if (s->avctx->err_recognition & AV_EF_EXPLODE)
457  return samples;
458 
459  for (j = 0; j < s->output_channels; j++)
460  memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
461  samples = s->packet.frame_duration;
462  }
463  output_samples += samples;
464 
465  for (j = 0; j < s->output_channels; j++)
466  s->cur_out[j] += samples;
467  s->remaining_out_size -= samples * sizeof(float);
468  }
469 
470 finish:
471  s->cur_out[0] = s->cur_out[1] = NULL;
472  s->remaining_out_size = 0;
473 
474  return output_samples;
475 }
476 
478  int *got_frame_ptr, AVPacket *avpkt)
479 {
481  const uint8_t *buf = avpkt->data;
482  int buf_size = avpkt->size;
483  int coded_samples = 0;
484  int decoded_samples = INT_MAX;
485  int delayed_samples = 0;
486  int i, ret;
487 
488  /* calculate the number of delayed samples */
489  for (int i = 0; i < c->p.nb_streams; i++) {
490  OpusStreamContext *s = &c->streams[i];
491  s->out[0] =
492  s->out[1] = NULL;
493  int fifo_samples = av_audio_fifo_size(s->sync_buffer);
495  s->delayed_samples + fifo_samples);
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);
582  if (ret < 0)
583  return ret;
584  s->decoded_samples = ret;
586 
587  if (!buf)
588  continue;
589 
590  buf += s->packet.packet_size;
591  buf_size -= s->packet.packet_size;
592  }
593 
594  /* buffer the extra samples */
595  for (int i = 0; i < c->p.nb_streams; i++) {
596  OpusStreamContext *s = &c->streams[i];
597  int buffer_samples = s->decoded_samples - decoded_samples;
598  if (buffer_samples) {
599  float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
600  s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
601  buf[0] += decoded_samples;
602  buf[1] += decoded_samples;
603  ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
604  if (ret < 0)
605  return ret;
606  }
607  }
608 
609  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
610  ChannelMap *map = &c->p.channel_maps[i];
611 
612  /* handle copied channels */
613  if (map->copy) {
614  memcpy(frame->extended_data[i],
615  frame->extended_data[map->copy_idx],
616  frame->linesize[0]);
617  } else if (map->silence) {
618  memset(frame->extended_data[i], 0, frame->linesize[0]);
619  }
620 
621  if (c->p.gain_i && decoded_samples > 0) {
622  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
623  (float*)frame->extended_data[i],
624  c->gain, FFALIGN(decoded_samples, 8));
625  }
626  }
627 
628  frame->nb_samples = decoded_samples;
629  *got_frame_ptr = !!decoded_samples;
630 
631  return avpkt->size;
632 }
633 
635 {
637 
638  for (int i = 0; i < c->p.nb_streams; i++) {
639  OpusStreamContext *s = &c->streams[i];
640 
641  memset(&s->packet, 0, sizeof(s->packet));
642  s->delayed_samples = 0;
643 
644  av_audio_fifo_reset(s->celt_delay);
645  swr_close(s->swr);
646 
647  av_audio_fifo_reset(s->sync_buffer);
648 
649  ff_silk_flush(s->silk);
650  ff_celt_flush(s->celt);
651  }
652 }
653 
655 {
657 
658  for (int i = 0; i < c->p.nb_streams; i++) {
659  OpusStreamContext *s = &c->streams[i];
660 
661  ff_silk_free(&s->silk);
662  ff_celt_free(&s->celt);
663 
664  av_freep(&s->out_dummy);
665  s->out_dummy_allocated_size = 0;
666 
667  av_audio_fifo_free(s->sync_buffer);
668  av_audio_fifo_free(s->celt_delay);
669  swr_free(&s->swr);
670  }
671 
672  av_freep(&c->streams);
673 
674  c->p.nb_streams = 0;
675 
676  av_freep(&c->p.channel_maps);
677  av_freep(&c->fdsp);
678 
679  return 0;
680 }
681 
683 {
685  int ret;
686 
688  avctx->sample_rate = 48000;
689 
690  c->fdsp = avpriv_float_dsp_alloc(0);
691  if (!c->fdsp)
692  return AVERROR(ENOMEM);
693 
694  /* find out the channel configuration */
696  if (ret < 0)
697  return ret;
698  if (c->p.gain_i)
699  c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
700 
701  /* allocate and init each independent decoder */
702  c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
703  if (!c->streams) {
704  c->p.nb_streams = 0;
705  return AVERROR(ENOMEM);
706  }
707 
708  for (int i = 0; i < c->p.nb_streams; i++) {
709  OpusStreamContext *s = &c->streams[i];
711 
712  s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
713 
714  s->avctx = avctx;
715 
716  for (int j = 0; j < s->output_channels; j++) {
717  s->silk_output[j] = s->silk_buf[j];
718  s->celt_output[j] = s->celt_buf[j];
719  s->redundancy_output[j] = s->redundancy_buf[j];
720  }
721 
722  s->fdsp = c->fdsp;
723 
724  s->swr =swr_alloc();
725  if (!s->swr)
726  return AVERROR(ENOMEM);
727 
728  layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
730  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
731  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
732  av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
733  av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
734  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
735  av_opt_set_int(s->swr, "filter_size", 16, 0);
736 
737  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
738  if (ret < 0)
739  return ret;
740 
741  ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
742  if (ret < 0)
743  return ret;
744 
745  s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
746  s->output_channels, 1024);
747  if (!s->celt_delay)
748  return AVERROR(ENOMEM);
749 
750  s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
751  s->output_channels, 32);
752  if (!s->sync_buffer)
753  return AVERROR(ENOMEM);
754  }
755 
756  return 0;
757 }
758 
759 #define OFFSET(x) offsetof(OpusContext, x)
760 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
761 static const AVOption opus_options[] = {
762  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
763  { NULL },
764 };
765 
766 static const AVClass opus_class = {
767  .class_name = "Opus Decoder",
768  .item_name = av_default_item_name,
769  .option = opus_options,
770  .version = LIBAVUTIL_VERSION_INT,
771 };
772 
774  .p.name = "opus",
775  CODEC_LONG_NAME("Opus"),
776  .p.priv_class = &opus_class,
777  .p.type = AVMEDIA_TYPE_AUDIO,
778  .p.id = AV_CODEC_ID_OPUS,
779  .priv_data_size = sizeof(OpusContext),
783  .flush = opus_decode_flush,
785  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
786 };
OpusStreamContext::decoded_samples
int decoded_samples
Definition: dec.c:80
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:71
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:682
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:76
opus_fade
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition: dec.c:141
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
opus_decode_subpacket
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf)
Definition: dec.c:397
out
static FILE * out
Definition: movenc.c:55
OpusStreamContext::delayed_samples
int delayed_samples
Definition: dec.c:113
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:1040
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
av_cold
#define av_cold
Definition: attributes.h: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:634
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
AVPacket::data
uint8_t * data
Definition: packet.h:603
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:759
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
OpusStreamContext::rc
OpusRangeCoder rc
Definition: dec.c:88
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:110
OpusContext::p
OpusParseContext p
Definition: dec.c:129
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:197
OpusStreamContext::swr
SwrContext * swr
Definition: dec.c:109
window
static SDL_Window * window
Definition: ffplay.c:364
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:1055
OpusStreamContext::out_size
int out_size
Definition: dec.c:83
OpusContext::gain
float gain
Definition: dec.c:127
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:721
OpusStreamContext::celt
CeltFrame * celt
Definition: dec.c:91
OpusStreamContext::redundancy_buf
float redundancy_buf[2][960]
Definition: dec.c:99
ff_silk_free
void ff_silk_free(SilkContext **ps)
Definition: silk.c:866
OpusStreamContext::celt_output
float * celt_output[2]
Definition: dec.c:97
opus_decode_frame
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition: dec.c:238
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:725
OpusStreamContext::out
float * out[2]
Definition: dec.c:82
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
parse.h
OpusStreamContext::silk_output
float * silk_output[2]
Definition: dec.c:95
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:156
float
float
Definition: af_crystalizer.c:122
OpusStreamContext
Definition: dec.c:75
OpusContext::apply_phase_inv
int apply_phase_inv
Definition: dec.c:124
OpusStreamContext::remaining_out_size
int remaining_out_size
Definition: dec.c:104
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
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:286
OpusStreamContext::fdsp
AVFloatDSPContext * fdsp
Definition: dec.c:92
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:117
OpusContext::av_class
AVClass * av_class
Definition: dec.c:121
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
decode.h
OpusStreamContext::sync_buffer
AVAudioFifo * sync_buffer
Definition: dec.c:86
ff_celt_init
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition: dec_celt.c:547
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
ff_celt_flush
void ff_celt_flush(CeltFrame *f)
Definition: dec_celt.c:499
opus_options
static const AVOption opus_options[]
Definition: dec.c:761
SilkContext
Definition: silk.c:53
OPUS_BANDWIDTH_WIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
Definition: opus.h:52
OPUS_MODE_CELT
@ OPUS_MODE_CELT
Definition: opus.h:44
fail
#define fail
Definition: test.h:478
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
opus_class
static const AVClass opus_class
Definition: dec.c:766
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:111
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:242
OpusStreamContext::packet
OpusPacket packet
Definition: dec.c:115
OpusParseContext
Definition: parse.h:63
attributes.h
OpusStreamContext::out_dummy_allocated_size
int out_dummy_allocated_size
Definition: dec.c:107
av_opt_get_int
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:1263
OpusStreamContext::redundancy_rc
OpusRangeCoder redundancy_rc
Definition: dec.c:89
OpusContext::fdsp
AVFloatDSPContext * fdsp
Definition: dec.c:126
ff_celt_free
void ff_celt_free(CeltFrame **f)
Definition: dec_celt.c:530
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:870
float_dsp.h
ff_silk_init
int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
Definition: silk.c:879
ff_silk_flush
void ff_silk_flush(SilkContext *s)
Definition: silk.c:871
opus_init_resample
static int opus_init_resample(OpusStreamContext *s)
Definition: dec.c:193
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:1771
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:579
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:604
ff_opus_decoder
const FFCodec ff_opus_decoder
Definition: dec.c:773
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:990
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1047
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:90
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:137
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:521
AVFloatDSPContext
Definition: float_dsp.h:24
frame.h
OpusContext::streams
struct OpusStreamContext * streams
Definition: dec.c:123
AD
#define AD
Definition: dec.c:760
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
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:218
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:443
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:786
channel_layout.h
OpusStreamContext::silk_buf
float silk_buf[2][960]
Definition: dec.c:94
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:61
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:106
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:150
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:470
AVPacket
This structure stores compressed data.
Definition: packet.h:580
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:152
OpusStreamContext::celt_buf
float celt_buf[2][960]
Definition: dec.c:96
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:103
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
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
get_silk_samplerate
static int get_silk_samplerate(int config)
Definition: dec.c:132
OpusStreamContext::output_channels
int output_channels
Definition: dec.c:77
opus_decode_close
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: dec.c:654
OpusStreamContext::redundancy_output
float * redundancy_output[2]
Definition: dec.c:100
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:85
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1342
opus_decode_packet
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: dec.c:477
CeltFrame
Definition: celt.h:98
OpusContext
Definition: dec.c:120
ff_celt_window2
const float ff_celt_window2[120]
Definition: tab.c:1200