FFmpeg
opusdec.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_internal.h"
45 #include "libavutil/opt.h"
46 
48 
49 #include "avcodec.h"
50 #include "codec_internal.h"
51 #include "decode.h"
52 #include "opus.h"
53 #include "opustab.h"
54 #include "opus_celt.h"
55 #include "opus_parse.h"
56 #include "opus_rc.h"
57 #include "opus_silk.h"
58 
59 static const uint16_t silk_frame_duration_ms[16] = {
60  10, 20, 40, 60,
61  10, 20, 40, 60,
62  10, 20, 40, 60,
63  10, 20,
64  10, 20,
65 };
66 
67 /* number of samples of silence to feed to the resampler
68  * at the beginning */
69 static const int silk_resample_delay[] = {
70  4, 8, 11, 11, 11
71 };
72 
73 typedef struct OpusStreamContext {
76 
77  /* number of decoded samples for this stream */
79  /* current output buffers for this stream */
80  float *out[2];
81  int out_size;
82  /* Buffer with samples from this stream for synchronizing
83  * the streams when they have different resampling delays */
85 
91 
92  float silk_buf[2][960];
93  float *silk_output[2];
94  DECLARE_ALIGNED(32, float, celt_buf)[2][960];
95  float *celt_output[2];
96 
97  DECLARE_ALIGNED(32, float, redundancy_buf)[2][960];
98  float *redundancy_output[2];
99 
100  /* buffers for the next samples to be decoded */
101  float *cur_out[2];
103 
104  float *out_dummy;
106 
110  /* number of samples we still want to get from the resampler */
112 
114 
117 
118 typedef struct OpusContext {
120 
123 
125  float gain;
126 
128 } OpusContext;
129 
131 {
132  if (config < 4)
133  return 8000;
134  else if (config < 8)
135  return 12000;
136  return 16000;
137 }
138 
139 static void opus_fade(float *out,
140  const float *in1, const float *in2,
141  const float *window, int len)
142 {
143  int i;
144  for (i = 0; i < len; i++)
145  out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
146 }
147 
148 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
149 {
150  int celt_size = av_audio_fifo_size(s->celt_delay);
151  int ret, i;
152  ret = swr_convert(s->swr,
153  (uint8_t**)s->cur_out, nb_samples,
154  NULL, 0);
155  if (ret < 0)
156  return ret;
157  else if (ret != nb_samples) {
158  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
159  ret);
160  return AVERROR_BUG;
161  }
162 
163  if (celt_size) {
164  if (celt_size != nb_samples) {
165  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
166  return AVERROR_BUG;
167  }
168  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
169  for (i = 0; i < s->output_channels; i++) {
170  s->fdsp->vector_fmac_scalar(s->cur_out[i],
171  s->celt_output[i], 1.0,
172  nb_samples);
173  }
174  }
175 
176  if (s->redundancy_idx) {
177  for (i = 0; i < s->output_channels; i++)
178  opus_fade(s->cur_out[i], s->cur_out[i],
179  s->redundancy_output[i] + 120 + s->redundancy_idx,
180  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
181  s->redundancy_idx = 0;
182  }
183 
184  s->cur_out[0] += nb_samples;
185  s->cur_out[1] += nb_samples;
186  s->remaining_out_size -= nb_samples * sizeof(float);
187 
188  return 0;
189 }
190 
192 {
193  static const float delay[16] = { 0.0 };
194  const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
195  int ret;
196 
197  av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
198  ret = swr_init(s->swr);
199  if (ret < 0) {
200  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
201  return ret;
202  }
203 
204  ret = swr_convert(s->swr,
205  NULL, 0,
206  delayptr, silk_resample_delay[s->packet.bandwidth]);
207  if (ret < 0) {
208  av_log(s->avctx, AV_LOG_ERROR,
209  "Error feeding initial silence to the resampler.\n");
210  return ret;
211  }
212 
213  return 0;
214 }
215 
216 static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
217 {
218  int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
219  if (ret < 0)
220  goto fail;
221  ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
222 
223  ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
224  s->redundancy_output,
225  s->packet.stereo + 1, 240,
226  0, ff_celt_band_end[s->packet.bandwidth]);
227  if (ret < 0)
228  goto fail;
229 
230  return 0;
231 fail:
232  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
233  return ret;
234 }
235 
236 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
237 {
238  int samples = s->packet.frame_duration;
239  int redundancy = 0;
240  int redundancy_size, redundancy_pos;
241  int ret, i, consumed;
242  int delayed_samples = s->delayed_samples;
243 
244  ret = ff_opus_rc_dec_init(&s->rc, data, size);
245  if (ret < 0)
246  return ret;
247 
248  /* decode the silk frame */
249  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
250  if (!swr_is_initialized(s->swr)) {
252  if (ret < 0)
253  return ret;
254  }
255 
256  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
257  FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
258  s->packet.stereo + 1,
259  silk_frame_duration_ms[s->packet.config]);
260  if (samples < 0) {
261  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
262  return samples;
263  }
264  samples = swr_convert(s->swr,
265  (uint8_t**)s->cur_out, s->packet.frame_duration,
266  (const uint8_t**)s->silk_output, samples);
267  if (samples < 0) {
268  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
269  return samples;
270  }
271  av_assert2((samples & 7) == 0);
272  s->delayed_samples += s->packet.frame_duration - samples;
273  } else
274  ff_silk_flush(s->silk);
275 
276  // decode redundancy information
277  consumed = opus_rc_tell(&s->rc);
278  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
279  redundancy = ff_opus_rc_dec_log(&s->rc, 12);
280  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
281  redundancy = 1;
282 
283  if (redundancy) {
284  redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
285 
286  if (s->packet.mode == OPUS_MODE_HYBRID)
287  redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
288  else
289  redundancy_size = size - (consumed + 7) / 8;
290  size -= redundancy_size;
291  if (size < 0) {
292  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
293  return AVERROR_INVALIDDATA;
294  }
295 
296  if (redundancy_pos) {
297  ret = opus_decode_redundancy(s, data + size, redundancy_size);
298  if (ret < 0)
299  return ret;
300  ff_celt_flush(s->celt);
301  }
302  }
303 
304  /* decode the CELT frame */
305  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
306  float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
307  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
308  out_tmp : s->celt_output;
309  int celt_output_samples = samples;
310  int delay_samples = av_audio_fifo_size(s->celt_delay);
311 
312  if (delay_samples) {
313  if (s->packet.mode == OPUS_MODE_HYBRID) {
314  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
315 
316  for (i = 0; i < s->output_channels; i++) {
317  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
318  delay_samples);
319  out_tmp[i] += delay_samples;
320  }
321  celt_output_samples -= delay_samples;
322  } else {
323  av_log(s->avctx, AV_LOG_WARNING,
324  "Spurious CELT delay samples present.\n");
325  av_audio_fifo_drain(s->celt_delay, delay_samples);
326  if (s->avctx->err_recognition & AV_EF_EXPLODE)
327  return AVERROR_BUG;
328  }
329  }
330 
332 
333  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
334  s->packet.stereo + 1,
335  s->packet.frame_duration,
336  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
337  ff_celt_band_end[s->packet.bandwidth]);
338  if (ret < 0)
339  return ret;
340 
341  if (s->packet.mode == OPUS_MODE_HYBRID) {
342  int celt_delay = s->packet.frame_duration - celt_output_samples;
343  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
344  s->celt_output[1] + celt_output_samples };
345 
346  for (i = 0; i < s->output_channels; i++) {
347  s->fdsp->vector_fmac_scalar(out_tmp[i],
348  s->celt_output[i], 1.0,
349  celt_output_samples);
350  }
351 
352  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
353  if (ret < 0)
354  return ret;
355  }
356  } else
357  ff_celt_flush(s->celt);
358 
359  if (s->redundancy_idx) {
360  for (i = 0; i < s->output_channels; i++)
361  opus_fade(s->cur_out[i], s->cur_out[i],
362  s->redundancy_output[i] + 120 + s->redundancy_idx,
363  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
364  s->redundancy_idx = 0;
365  }
366  if (redundancy) {
367  if (!redundancy_pos) {
368  ff_celt_flush(s->celt);
369  ret = opus_decode_redundancy(s, data + size, redundancy_size);
370  if (ret < 0)
371  return ret;
372 
373  for (i = 0; i < s->output_channels; i++) {
374  opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
375  s->cur_out[i] + samples - 120 + delayed_samples,
376  s->redundancy_output[i] + 120,
378  if (delayed_samples)
379  s->redundancy_idx = 120 - delayed_samples;
380  }
381  } else {
382  for (i = 0; i < s->output_channels; i++) {
383  memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
384  opus_fade(s->cur_out[i] + 120 + delayed_samples,
385  s->redundancy_output[i] + 120,
386  s->cur_out[i] + 120 + delayed_samples,
387  ff_celt_window2, 120);
388  }
389  }
390  }
391 
392  return samples;
393 }
394 
396  const uint8_t *buf, int buf_size,
397  int nb_samples)
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;
494  s->delayed_samples + av_audio_fifo_size(s->sync_buffer));
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, s->packet.data_size,
581  coded_samples);
582  if (ret < 0)
583  return ret;
584  s->decoded_samples = ret;
586 
587  buf += s->packet.packet_size;
588  buf_size -= s->packet.packet_size;
589  }
590 
591  /* buffer the extra samples */
592  for (int i = 0; i < c->p.nb_streams; i++) {
593  OpusStreamContext *s = &c->streams[i];
594  int buffer_samples = s->decoded_samples - decoded_samples;
595  if (buffer_samples) {
596  float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
597  s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
598  buf[0] += decoded_samples;
599  buf[1] += decoded_samples;
600  ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
601  if (ret < 0)
602  return ret;
603  }
604  }
605 
606  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
607  ChannelMap *map = &c->p.channel_maps[i];
608 
609  /* handle copied channels */
610  if (map->copy) {
611  memcpy(frame->extended_data[i],
612  frame->extended_data[map->copy_idx],
613  frame->linesize[0]);
614  } else if (map->silence) {
615  memset(frame->extended_data[i], 0, frame->linesize[0]);
616  }
617 
618  if (c->p.gain_i && decoded_samples > 0) {
619  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
620  (float*)frame->extended_data[i],
621  c->gain, FFALIGN(decoded_samples, 8));
622  }
623  }
624 
626  *got_frame_ptr = !!decoded_samples;
627 
628  return avpkt->size;
629 }
630 
632 {
634 
635  for (int i = 0; i < c->p.nb_streams; i++) {
636  OpusStreamContext *s = &c->streams[i];
637 
638  memset(&s->packet, 0, sizeof(s->packet));
639  s->delayed_samples = 0;
640 
641  av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
642  swr_close(s->swr);
643 
644  av_audio_fifo_drain(s->sync_buffer, av_audio_fifo_size(s->sync_buffer));
645 
646  ff_silk_flush(s->silk);
647  ff_celt_flush(s->celt);
648  }
649 }
650 
652 {
654 
655  for (int i = 0; i < c->p.nb_streams; i++) {
656  OpusStreamContext *s = &c->streams[i];
657 
658  ff_silk_free(&s->silk);
659  ff_celt_free(&s->celt);
660 
661  av_freep(&s->out_dummy);
662  s->out_dummy_allocated_size = 0;
663 
664  av_audio_fifo_free(s->sync_buffer);
665  av_audio_fifo_free(s->celt_delay);
666  swr_free(&s->swr);
667  }
668 
669  av_freep(&c->streams);
670 
671  c->p.nb_streams = 0;
672 
673  av_freep(&c->p.channel_maps);
674  av_freep(&c->fdsp);
675 
676  return 0;
677 }
678 
680 {
682  int ret;
683 
685  avctx->sample_rate = 48000;
686 
687  c->fdsp = avpriv_float_dsp_alloc(0);
688  if (!c->fdsp)
689  return AVERROR(ENOMEM);
690 
691  /* find out the channel configuration */
693  if (ret < 0)
694  return ret;
695  if (c->p.gain_i)
696  c->gain = ff_exp10(c->p.gain_i / (20.0 * 256));
697 
698  /* allocate and init each independent decoder */
699  c->streams = av_calloc(c->p.nb_streams, sizeof(*c->streams));
700  if (!c->streams) {
701  c->p.nb_streams = 0;
702  return AVERROR(ENOMEM);
703  }
704 
705  for (int i = 0; i < c->p.nb_streams; i++) {
706  OpusStreamContext *s = &c->streams[i];
708 
709  s->output_channels = (i < c->p.nb_stereo_streams) ? 2 : 1;
710 
711  s->avctx = avctx;
712 
713  for (int j = 0; j < s->output_channels; j++) {
714  s->silk_output[j] = s->silk_buf[j];
715  s->celt_output[j] = s->celt_buf[j];
716  s->redundancy_output[j] = s->redundancy_buf[j];
717  }
718 
719  s->fdsp = c->fdsp;
720 
721  s->swr =swr_alloc();
722  if (!s->swr)
723  return AVERROR(ENOMEM);
724 
725  layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
727  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
728  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
729  av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
730  av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
731  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
732  av_opt_set_int(s->swr, "filter_size", 16, 0);
733 
734  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
735  if (ret < 0)
736  return ret;
737 
738  ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
739  if (ret < 0)
740  return ret;
741 
742  s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
743  s->output_channels, 1024);
744  if (!s->celt_delay)
745  return AVERROR(ENOMEM);
746 
747  s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
748  s->output_channels, 32);
749  if (!s->sync_buffer)
750  return AVERROR(ENOMEM);
751  }
752 
753  return 0;
754 }
755 
756 #define OFFSET(x) offsetof(OpusContext, x)
757 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
758 static const AVOption opus_options[] = {
759  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
760  { NULL },
761 };
762 
763 static const AVClass opus_class = {
764  .class_name = "Opus Decoder",
765  .item_name = av_default_item_name,
766  .option = opus_options,
767  .version = LIBAVUTIL_VERSION_INT,
768 };
769 
771  .p.name = "opus",
772  CODEC_LONG_NAME("Opus"),
773  .p.priv_class = &opus_class,
774  .p.type = AVMEDIA_TYPE_AUDIO,
775  .p.id = AV_CODEC_ID_OPUS,
776  .priv_data_size = sizeof(OpusContext),
778  .close = opus_decode_close,
780  .flush = opus_decode_flush,
782  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
783 };
OpusStreamContext::decoded_samples
int decoded_samples
Definition: opusdec.c:78
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:48
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:186
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: opusdec.c:74
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
mem_internal.h
out
FILE * out
Definition: movenc.c:54
OpusStreamContext::delayed_samples
int delayed_samples
Definition: opusdec.c:111
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:383
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
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
ff_opus_decoder
const FFCodec ff_opus_decoder
Definition: opusdec.c:770
opus_decode_packet
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: opusdec.c:477
opus_decode_frame
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:236
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVPacket::data
uint8_t * data
Definition: packet.h:491
AVOption
AVOption.
Definition: opt.h:251
data
const char data[16]
Definition: mxf.c:148
opus_decode_close
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: opusdec.c:651
opus.h
FFCodec
Definition: codec_internal.h:127
OFFSET
#define OFFSET(x)
Definition: opusdec.c:756
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
OpusStreamContext::rc
OpusRangeCoder rc
Definition: opusdec.c:86
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
tf_sess_config.config
config
Definition: tf_sess_config.py:33
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: opus_rc.h:60
ff_celt_flush
void ff_celt_flush(CeltFrame *f)
Definition: opusdec_celt.c:494
OpusStreamContext::celt_delay
AVAudioFifo * celt_delay
Definition: opusdec.c:108
silk_frame_duration_ms
static const uint16_t silk_frame_duration_ms[16]
Definition: opusdec.c:59
opus_parse.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: opusdec_celt.c:322
OpusContext::p
OpusParseContext p
Definition: opusdec.c:127
opus_options
static const AVOption opus_options[]
Definition: opusdec.c:758
OpusStreamContext::swr
SwrContext * swr
Definition: opusdec.c:107
window
static SDL_Window * window
Definition: ffplay.c:360
ff_silk_flush
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:874
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:342
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
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:138
OpusStreamContext::out_size
int out_size
Definition: opusdec.c:81
OpusContext::gain
float gain
Definition: opusdec.c:125
swr_is_initialized
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
Definition: swresample.c:832
OpusStreamContext::celt
CeltFrame * celt
Definition: opusdec.c:89
OpusStreamContext::redundancy_buf
float redundancy_buf[2][960]
Definition: opusdec.c:97
opus_fade
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition: opusdec.c:139
OpusStreamContext::celt_output
float * celt_output[2]
Definition: opusdec.c:95
opus_decode_redundancy
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:216
OpusStreamContext::out
float * out[2]
Definition: opusdec.c:80
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
OpusStreamContext::silk_output
float * silk_output[2]
Definition: opusdec.c:93
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:193
float
float
Definition: af_crystalizer.c:121
OpusStreamContext
Definition: opusdec.c:73
ff_celt_band_end
const uint8_t ff_celt_band_end[]
Definition: opustab.c:29
OpusContext::apply_phase_inv
int apply_phase_inv
Definition: opusdec.c:122
OpusStreamContext::remaining_out_size
int remaining_out_size
Definition: opusdec.c:102
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
ff_opus_rc_dec_uint
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
Definition: opus_rc.c:182
s
#define s(width, name)
Definition: cbs_vp9.c:198
OpusStreamContext::fdsp
AVFloatDSPContext * fdsp
Definition: opusdec.c:90
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
swr_alloc
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:169
OpusStreamContext::redundancy_idx
int redundancy_idx
Definition: opusdec.c:115
OpusContext::av_class
AVClass * av_class
Definition: opusdec.c:119
opus_flush_resample
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition: opusdec.c:148
ctx
AVFormatContext * ctx
Definition: movenc.c:48
decode.h
OpusStreamContext::sync_buffer
AVAudioFifo * sync_buffer
Definition: opusdec.c:84
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
SilkContext
Definition: opus_silk.c:50
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
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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: opusdec.c:109
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: opusdec.c:113
OpusParseContext
Definition: opus_parse.h:63
OpusStreamContext::out_dummy_allocated_size
int out_dummy_allocated_size
Definition: opusdec.c:105
av_opt_get_int
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:978
OpusStreamContext::redundancy_rc
OpusRangeCoder redundancy_rc
Definition: opusdec.c:87
OpusContext::fdsp
AVFloatDSPContext * fdsp
Definition: opusdec.c:124
ff_silk_init
int ff_silk_init(void *logctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:882
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:624
opus_class
static const AVClass opus_class
Definition: opusdec.c:763
float_dsp.h
opus_decode_init
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: opusdec.c:679
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
opustab.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
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:492
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.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:786
get_silk_samplerate
static int get_silk_samplerate(int config)
Definition: opusdec.c:130
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
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: opus_rc.h:39
OpusStreamContext::silk
SilkContext * silk
Definition: opusdec.c:88
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:174
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:502
swr_convert
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t **out_arg, int out_count, const uint8_t **in_arg, int in_count)
Convert audio.
Definition: swresample.c:836
AVFloatDSPContext
Definition: float_dsp.h:24
ff_celt_window2
const float ff_celt_window2[120]
Definition: opustab.c:1136
ff_opus_rc_dec_init
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Definition: opus_rc.c:338
frame.h
OpusContext::streams
struct OpusStreamContext * streams
Definition: opusdec.c:121
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
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_silk_free
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:869
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
opus_celt.h
ff_opus_rc_dec_raw_init
void ff_opus_rc_dec_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, uint32_t bytes)
Definition: opus_rc.c:352
OPUS_MODE_HYBRID
@ OPUS_MODE_HYBRID
Definition: opus.h:43
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
opus_decode_flush
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition: opusdec.c:631
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:262
avcodec.h
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:71
opus_decode_subpacket
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf, int buf_size, int nb_samples)
Definition: opusdec.c:395
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AD
#define AD
Definition: opusdec.c:757
channel_layout.h
OpusStreamContext::silk_buf
float silk_buf[2][960]
Definition: opusdec.c:92
OPUS_MODE_SILK
@ OPUS_MODE_SILK
Definition: opus.h:42
OpusPacket
Definition: opus_parse.h:31
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
opus_init_resample
static int opus_init_resample(OpusStreamContext *s)
Definition: opusdec.c:191
ffmath.h
ChannelMap
Definition: opus_parse.h:48
silk_resample_delay
static const int silk_resample_delay[]
Definition: opusdec.c:69
OpusStreamContext::out_dummy
float * out_dummy
Definition: opusdec.c:104
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:382
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
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:555
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
ff_opus_parse_extradata
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s)
Definition: opus_parse.c:296
ff_celt_free
void ff_celt_free(CeltFrame **f)
Definition: opusdec_celt.c:525
swr_close
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
Definition: swresample.c:189
OpusStreamContext::celt_buf
float celt_buf[2][960]
Definition: opusdec.c:94
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
OpusStreamContext::cur_out
float * cur_out[2]
Definition: opusdec.c:101
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_opus_rc_dec_log
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
Definition: opus_rc.c:114
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: opus_silk.c:791
opus_rc.h
OpusStreamContext::output_channels
int output_channels
Definition: opusdec.c:75
OpusStreamContext::redundancy_output
float * redundancy_output[2]
Definition: opusdec.c:98
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1143
opus_silk.h
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: opus_parse.c:95
ff_celt_init
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition: opusdec_celt.c:542
CeltFrame
Definition: opus_celt.h:97
OpusContext
Definition: opusdec.c:118