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/opt.h"
42 
44 
45 #include "avcodec.h"
46 #include "codec_internal.h"
47 #include "decode.h"
48 #include "opus.h"
49 #include "opustab.h"
50 #include "opus_celt.h"
51 
52 static const uint16_t silk_frame_duration_ms[16] = {
53  10, 20, 40, 60,
54  10, 20, 40, 60,
55  10, 20, 40, 60,
56  10, 20,
57  10, 20,
58 };
59 
60 /* number of samples of silence to feed to the resampler
61  * at the beginning */
62 static const int silk_resample_delay[] = {
63  4, 8, 11, 11, 11
64 };
65 
66 static int get_silk_samplerate(int config)
67 {
68  if (config < 4)
69  return 8000;
70  else if (config < 8)
71  return 12000;
72  return 16000;
73 }
74 
75 static void opus_fade(float *out,
76  const float *in1, const float *in2,
77  const float *window, int len)
78 {
79  int i;
80  for (i = 0; i < len; i++)
81  out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
82 }
83 
84 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
85 {
86  int celt_size = av_audio_fifo_size(s->celt_delay);
87  int ret, i;
88  ret = swr_convert(s->swr,
89  (uint8_t**)s->cur_out, nb_samples,
90  NULL, 0);
91  if (ret < 0)
92  return ret;
93  else if (ret != nb_samples) {
94  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
95  ret);
96  return AVERROR_BUG;
97  }
98 
99  if (celt_size) {
100  if (celt_size != nb_samples) {
101  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
102  return AVERROR_BUG;
103  }
104  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
105  for (i = 0; i < s->output_channels; i++) {
106  s->fdsp->vector_fmac_scalar(s->cur_out[i],
107  s->celt_output[i], 1.0,
108  nb_samples);
109  }
110  }
111 
112  if (s->redundancy_idx) {
113  for (i = 0; i < s->output_channels; i++)
114  opus_fade(s->cur_out[i], s->cur_out[i],
115  s->redundancy_output[i] + 120 + s->redundancy_idx,
116  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
117  s->redundancy_idx = 0;
118  }
119 
120  s->cur_out[0] += nb_samples;
121  s->cur_out[1] += nb_samples;
122  s->remaining_out_size -= nb_samples * sizeof(float);
123 
124  return 0;
125 }
126 
128 {
129  static const float delay[16] = { 0.0 };
130  const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
131  int ret;
132 
133  av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
134  ret = swr_init(s->swr);
135  if (ret < 0) {
136  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
137  return ret;
138  }
139 
140  ret = swr_convert(s->swr,
141  NULL, 0,
142  delayptr, silk_resample_delay[s->packet.bandwidth]);
143  if (ret < 0) {
144  av_log(s->avctx, AV_LOG_ERROR,
145  "Error feeding initial silence to the resampler.\n");
146  return ret;
147  }
148 
149  return 0;
150 }
151 
152 static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
153 {
154  int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
155  if (ret < 0)
156  goto fail;
157  ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
158 
159  ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
160  s->redundancy_output,
161  s->packet.stereo + 1, 240,
162  0, ff_celt_band_end[s->packet.bandwidth]);
163  if (ret < 0)
164  goto fail;
165 
166  return 0;
167 fail:
168  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
169  return ret;
170 }
171 
172 static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
173 {
174  int samples = s->packet.frame_duration;
175  int redundancy = 0;
176  int redundancy_size, redundancy_pos;
177  int ret, i, consumed;
178  int delayed_samples = s->delayed_samples;
179 
180  ret = ff_opus_rc_dec_init(&s->rc, data, size);
181  if (ret < 0)
182  return ret;
183 
184  /* decode the silk frame */
185  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
186  if (!swr_is_initialized(s->swr)) {
188  if (ret < 0)
189  return ret;
190  }
191 
192  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
193  FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
194  s->packet.stereo + 1,
195  silk_frame_duration_ms[s->packet.config]);
196  if (samples < 0) {
197  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
198  return samples;
199  }
200  samples = swr_convert(s->swr,
201  (uint8_t**)s->cur_out, s->packet.frame_duration,
202  (const uint8_t**)s->silk_output, samples);
203  if (samples < 0) {
204  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
205  return samples;
206  }
207  av_assert2((samples & 7) == 0);
208  s->delayed_samples += s->packet.frame_duration - samples;
209  } else
210  ff_silk_flush(s->silk);
211 
212  // decode redundancy information
213  consumed = opus_rc_tell(&s->rc);
214  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
215  redundancy = ff_opus_rc_dec_log(&s->rc, 12);
216  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
217  redundancy = 1;
218 
219  if (redundancy) {
220  redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
221 
222  if (s->packet.mode == OPUS_MODE_HYBRID)
223  redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
224  else
225  redundancy_size = size - (consumed + 7) / 8;
226  size -= redundancy_size;
227  if (size < 0) {
228  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
229  return AVERROR_INVALIDDATA;
230  }
231 
232  if (redundancy_pos) {
233  ret = opus_decode_redundancy(s, data + size, redundancy_size);
234  if (ret < 0)
235  return ret;
236  ff_celt_flush(s->celt);
237  }
238  }
239 
240  /* decode the CELT frame */
241  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
242  float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
243  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
244  out_tmp : s->celt_output;
245  int celt_output_samples = samples;
246  int delay_samples = av_audio_fifo_size(s->celt_delay);
247 
248  if (delay_samples) {
249  if (s->packet.mode == OPUS_MODE_HYBRID) {
250  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
251 
252  for (i = 0; i < s->output_channels; i++) {
253  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
254  delay_samples);
255  out_tmp[i] += delay_samples;
256  }
257  celt_output_samples -= delay_samples;
258  } else {
259  av_log(s->avctx, AV_LOG_WARNING,
260  "Spurious CELT delay samples present.\n");
261  av_audio_fifo_drain(s->celt_delay, delay_samples);
262  if (s->avctx->err_recognition & AV_EF_EXPLODE)
263  return AVERROR_BUG;
264  }
265  }
266 
268 
269  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
270  s->packet.stereo + 1,
271  s->packet.frame_duration,
272  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
273  ff_celt_band_end[s->packet.bandwidth]);
274  if (ret < 0)
275  return ret;
276 
277  if (s->packet.mode == OPUS_MODE_HYBRID) {
278  int celt_delay = s->packet.frame_duration - celt_output_samples;
279  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
280  s->celt_output[1] + celt_output_samples };
281 
282  for (i = 0; i < s->output_channels; i++) {
283  s->fdsp->vector_fmac_scalar(out_tmp[i],
284  s->celt_output[i], 1.0,
285  celt_output_samples);
286  }
287 
288  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
289  if (ret < 0)
290  return ret;
291  }
292  } else
293  ff_celt_flush(s->celt);
294 
295  if (s->redundancy_idx) {
296  for (i = 0; i < s->output_channels; i++)
297  opus_fade(s->cur_out[i], s->cur_out[i],
298  s->redundancy_output[i] + 120 + s->redundancy_idx,
299  ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
300  s->redundancy_idx = 0;
301  }
302  if (redundancy) {
303  if (!redundancy_pos) {
304  ff_celt_flush(s->celt);
305  ret = opus_decode_redundancy(s, data + size, redundancy_size);
306  if (ret < 0)
307  return ret;
308 
309  for (i = 0; i < s->output_channels; i++) {
310  opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
311  s->cur_out[i] + samples - 120 + delayed_samples,
312  s->redundancy_output[i] + 120,
313  ff_celt_window2, 120 - delayed_samples);
314  if (delayed_samples)
315  s->redundancy_idx = 120 - delayed_samples;
316  }
317  } else {
318  for (i = 0; i < s->output_channels; i++) {
319  memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
320  opus_fade(s->cur_out[i] + 120 + delayed_samples,
321  s->redundancy_output[i] + 120,
322  s->cur_out[i] + 120 + delayed_samples,
323  ff_celt_window2, 120);
324  }
325  }
326  }
327 
328  return samples;
329 }
330 
332  const uint8_t *buf, int buf_size,
333  int nb_samples)
334 {
335  int output_samples = 0;
336  int flush_needed = 0;
337  int i, j, ret;
338 
339  s->cur_out[0] = s->out[0];
340  s->cur_out[1] = s->out[1];
341  s->remaining_out_size = s->out_size;
342 
343  /* check if we need to flush the resampler */
344  if (swr_is_initialized(s->swr)) {
345  if (buf) {
346  int64_t cur_samplerate;
347  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
348  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
349  } else {
350  flush_needed = !!s->delayed_samples;
351  }
352  }
353 
354  if (!buf && !flush_needed)
355  return 0;
356 
357  /* use dummy output buffers if the channel is not mapped to anything */
358  if (!s->cur_out[0] ||
359  (s->output_channels == 2 && !s->cur_out[1])) {
360  av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
361  s->remaining_out_size);
362  if (!s->out_dummy)
363  return AVERROR(ENOMEM);
364  if (!s->cur_out[0])
365  s->cur_out[0] = s->out_dummy;
366  if (!s->cur_out[1])
367  s->cur_out[1] = s->out_dummy;
368  }
369 
370  /* flush the resampler if necessary */
371  if (flush_needed) {
372  ret = opus_flush_resample(s, s->delayed_samples);
373  if (ret < 0) {
374  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
375  return ret;
376  }
377  swr_close(s->swr);
378  output_samples += s->delayed_samples;
379  s->delayed_samples = 0;
380 
381  if (!buf)
382  goto finish;
383  }
384 
385  /* decode all the frames in the packet */
386  for (i = 0; i < s->packet.frame_count; i++) {
387  int size = s->packet.frame_size[i];
388  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
389 
390  if (samples < 0) {
391  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
392  if (s->avctx->err_recognition & AV_EF_EXPLODE)
393  return samples;
394 
395  for (j = 0; j < s->output_channels; j++)
396  memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
397  samples = s->packet.frame_duration;
398  }
399  output_samples += samples;
400 
401  for (j = 0; j < s->output_channels; j++)
402  s->cur_out[j] += samples;
403  s->remaining_out_size -= samples * sizeof(float);
404  }
405 
406 finish:
407  s->cur_out[0] = s->cur_out[1] = NULL;
408  s->remaining_out_size = 0;
409 
410  return output_samples;
411 }
412 
414  int *got_frame_ptr, AVPacket *avpkt)
415 {
416  OpusContext *c = avctx->priv_data;
417  const uint8_t *buf = avpkt->data;
418  int buf_size = avpkt->size;
419  int coded_samples = 0;
420  int decoded_samples = INT_MAX;
421  int delayed_samples = 0;
422  int i, ret;
423 
424  /* calculate the number of delayed samples */
425  for (i = 0; i < c->nb_streams; i++) {
426  OpusStreamContext *s = &c->streams[i];
427  s->out[0] =
428  s->out[1] = NULL;
429  delayed_samples = FFMAX(delayed_samples,
430  s->delayed_samples + av_audio_fifo_size(s->sync_buffer));
431  }
432 
433  /* decode the header of the first sub-packet to find out the sample count */
434  if (buf) {
435  OpusPacket *pkt = &c->streams[0].packet;
436  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
437  if (ret < 0) {
438  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
439  return ret;
440  }
441  coded_samples += pkt->frame_count * pkt->frame_duration;
442  c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
443  }
444 
445  frame->nb_samples = coded_samples + delayed_samples;
446 
447  /* no input or buffered data => nothing to do */
448  if (!frame->nb_samples) {
449  *got_frame_ptr = 0;
450  return 0;
451  }
452 
453  /* setup the data buffers */
454  ret = ff_get_buffer(avctx, frame, 0);
455  if (ret < 0)
456  return ret;
457  frame->nb_samples = 0;
458 
459  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
460  ChannelMap *map = &c->channel_maps[i];
461  if (!map->copy)
462  c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
463  }
464 
465  /* read the data from the sync buffers */
466  for (i = 0; i < c->nb_streams; i++) {
467  OpusStreamContext *s = &c->streams[i];
468  float **out = s->out;
469  int sync_size = av_audio_fifo_size(s->sync_buffer);
470 
471  float sync_dummy[32];
472  int out_dummy = (!out[0]) | ((!out[1]) << 1);
473 
474  if (!out[0])
475  out[0] = sync_dummy;
476  if (!out[1])
477  out[1] = sync_dummy;
478  if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
479  return AVERROR_BUG;
480 
481  ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
482  if (ret < 0)
483  return ret;
484 
485  if (out_dummy & 1)
486  out[0] = NULL;
487  else
488  out[0] += ret;
489  if (out_dummy & 2)
490  out[1] = NULL;
491  else
492  out[1] += ret;
493 
494  s->out_size = frame->linesize[0] - ret * sizeof(float);
495  }
496 
497  /* decode each sub-packet */
498  for (i = 0; i < c->nb_streams; i++) {
499  OpusStreamContext *s = &c->streams[i];
500 
501  if (i && buf) {
502  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
503  if (ret < 0) {
504  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
505  return ret;
506  }
507  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
508  av_log(avctx, AV_LOG_ERROR,
509  "Mismatching coded sample count in substream %d.\n", i);
510  return AVERROR_INVALIDDATA;
511  }
512 
513  s->silk_samplerate = get_silk_samplerate(s->packet.config);
514  }
515 
516  ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
517  coded_samples);
518  if (ret < 0)
519  return ret;
520  s->decoded_samples = ret;
521  decoded_samples = FFMIN(decoded_samples, ret);
522 
523  buf += s->packet.packet_size;
524  buf_size -= s->packet.packet_size;
525  }
526 
527  /* buffer the extra samples */
528  for (i = 0; i < c->nb_streams; i++) {
529  OpusStreamContext *s = &c->streams[i];
530  int buffer_samples = s->decoded_samples - decoded_samples;
531  if (buffer_samples) {
532  float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
533  s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
534  buf[0] += decoded_samples;
535  buf[1] += decoded_samples;
536  ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
537  if (ret < 0)
538  return ret;
539  }
540  }
541 
542  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
543  ChannelMap *map = &c->channel_maps[i];
544 
545  /* handle copied channels */
546  if (map->copy) {
547  memcpy(frame->extended_data[i],
548  frame->extended_data[map->copy_idx],
549  frame->linesize[0]);
550  } else if (map->silence) {
551  memset(frame->extended_data[i], 0, frame->linesize[0]);
552  }
553 
554  if (c->gain_i && decoded_samples > 0) {
555  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
556  (float*)frame->extended_data[i],
557  c->gain, FFALIGN(decoded_samples, 8));
558  }
559  }
560 
561  frame->nb_samples = decoded_samples;
562  *got_frame_ptr = !!decoded_samples;
563 
564  return avpkt->size;
565 }
566 
568 {
570  int i;
571 
572  for (i = 0; i < c->nb_streams; i++) {
573  OpusStreamContext *s = &c->streams[i];
574 
575  memset(&s->packet, 0, sizeof(s->packet));
576  s->delayed_samples = 0;
577 
578  av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
579  swr_close(s->swr);
580 
581  av_audio_fifo_drain(s->sync_buffer, av_audio_fifo_size(s->sync_buffer));
582 
583  ff_silk_flush(s->silk);
584  ff_celt_flush(s->celt);
585  }
586 }
587 
589 {
590  OpusContext *c = avctx->priv_data;
591  int i;
592 
593  for (i = 0; i < c->nb_streams; i++) {
594  OpusStreamContext *s = &c->streams[i];
595 
596  ff_silk_free(&s->silk);
597  ff_celt_free(&s->celt);
598 
599  av_freep(&s->out_dummy);
600  s->out_dummy_allocated_size = 0;
601 
602  av_audio_fifo_free(s->sync_buffer);
603  av_audio_fifo_free(s->celt_delay);
604  swr_free(&s->swr);
605  }
606 
607  av_freep(&c->streams);
608 
609  c->nb_streams = 0;
610 
611  av_freep(&c->channel_maps);
612  av_freep(&c->fdsp);
613 
614  return 0;
615 }
616 
618 {
619  OpusContext *c = avctx->priv_data;
620  int ret, i, j;
621 
623  avctx->sample_rate = 48000;
624 
625  c->fdsp = avpriv_float_dsp_alloc(0);
626  if (!c->fdsp)
627  return AVERROR(ENOMEM);
628 
629  /* find out the channel configuration */
630  ret = ff_opus_parse_extradata(avctx, c);
631  if (ret < 0)
632  return ret;
633 
634  /* allocate and init each independent decoder */
635  c->streams = av_calloc(c->nb_streams, sizeof(*c->streams));
636  if (!c->streams) {
637  c->nb_streams = 0;
638  return AVERROR(ENOMEM);
639  }
640 
641  for (i = 0; i < c->nb_streams; i++) {
642  OpusStreamContext *s = &c->streams[i];
644 
645  s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
646 
647  s->avctx = avctx;
648 
649  for (j = 0; j < s->output_channels; j++) {
650  s->silk_output[j] = s->silk_buf[j];
651  s->celt_output[j] = s->celt_buf[j];
652  s->redundancy_output[j] = s->redundancy_buf[j];
653  }
654 
655  s->fdsp = c->fdsp;
656 
657  s->swr =swr_alloc();
658  if (!s->swr)
659  return AVERROR(ENOMEM);
660 
661  layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
663  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
664  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
665  av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
666  av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
667  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
668  av_opt_set_int(s->swr, "filter_size", 16, 0);
669 
670  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
671  if (ret < 0)
672  return ret;
673 
674  ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
675  if (ret < 0)
676  return ret;
677 
678  s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
679  s->output_channels, 1024);
680  if (!s->celt_delay)
681  return AVERROR(ENOMEM);
682 
683  s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
684  s->output_channels, 32);
685  if (!s->sync_buffer)
686  return AVERROR(ENOMEM);
687  }
688 
689  return 0;
690 }
691 
692 #define OFFSET(x) offsetof(OpusContext, x)
693 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
694 static const AVOption opus_options[] = {
695  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
696  { NULL },
697 };
698 
699 static const AVClass opus_class = {
700  .class_name = "Opus Decoder",
701  .item_name = av_default_item_name,
702  .option = opus_options,
703  .version = LIBAVUTIL_VERSION_INT,
704 };
705 
707  .p.name = "opus",
708  CODEC_LONG_NAME("Opus"),
709  .p.priv_class = &opus_class,
710  .p.type = AVMEDIA_TYPE_AUDIO,
711  .p.id = AV_CODEC_ID_OPUS,
712  .priv_data_size = sizeof(OpusContext),
714  .close = opus_decode_close,
716  .flush = opus_decode_flush,
718  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
719 };
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
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
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
out
FILE * out
Definition: movenc.c:54
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1007
ff_opus_parse_extradata
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:293
ff_celt_flush
void ff_celt_flush(CeltFrame *f)
Definition: opus_celt.c:492
ff_opus_decoder
const FFCodec ff_opus_decoder
Definition: opusdec.c:706
opus_decode_packet
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: opusdec.c:413
opus_decode_frame
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:172
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
data
const char data[16]
Definition: mxf.c:146
opus_decode_close
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: opusdec.c:588
opus.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:356
FFCodec
Definition: codec_internal.h:119
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.c:92
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:357
OFFSET
#define OFFSET(x)
Definition: opusdec.c:692
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:303
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:61
init
static int init
Definition: av_tx.c:47
silk_frame_duration_ms
static const uint16_t silk_frame_duration_ms[16]
Definition: opusdec.c:52
opus_options
static const AVOption opus_options[]
Definition: opusdec.c:694
window
static SDL_Window * window
Definition: ffplay.c:365
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
finish
static void finish(void)
Definition: movenc.c:342
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2059
av_audio_fifo_drain
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:194
fail
#define fail()
Definition: checkasm.h:133
swr_is_initialized
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
Definition: swresample.c:812
opus_fade
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition: opusdec.c:75
opus_decode_redundancy
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:152
ff_celt_init
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition: opus_celt.c:540
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
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:191
float
float
Definition: af_crystalizer.c:122
OpusStreamContext
Definition: opus.h:101
ff_celt_band_end
const uint8_t ff_celt_band_end[]
Definition: opustab.c:29
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
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:256
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:119
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
swr_alloc
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:167
ff_silk_init
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:877
opus_flush_resample
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition: opusdec.c:84
ctx
AVFormatContext * ctx
Definition: movenc.c:48
decode.h
ff_silk_flush
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:869
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
OPUS_BANDWIDTH_WIDEBAND
@ OPUS_BANDWIDTH_WIDEBAND
Definition: opus.h:74
OPUS_MODE_CELT
@ OPUS_MODE_CELT
Definition: opus.h:66
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:786
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
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
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
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:699
opus_decode_init
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: opusdec.c:617
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:109
opustab.h
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1450
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:375
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:293
codec_internal.h
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:66
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1023
size
int size
Definition: twinvq_data.h:10344
ff_celt_free
void ff_celt_free(CeltFrame **f)
Definition: opus_celt.c:523
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:173
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:494
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:816
ff_celt_window2
const float ff_celt_window2[120]
Definition: opustab.c:1138
ff_opus_rc_dec_init
int ff_opus_rc_dec_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Definition: opus_rc.c:338
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:221
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_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:174
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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:65
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
opus_decode_flush
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition: opusdec.c:567
audio_fifo.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
len
int len
Definition: vorbis_enc_data.h:426
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
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
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
ff_celt_decode_frame
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, int channels, int frame_size, int start_band, int end_band)
Definition: opus_celt.c:320
opus_decode_subpacket
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf, int buf_size, int nb_samples)
Definition: opusdec.c:331
AVCodecContext
main external API structure.
Definition: avcodec.h:398
AD
#define AD
Definition: opusdec.c:693
channel_layout.h
OPUS_MODE_SILK
@ OPUS_MODE_SILK
Definition: opus.h:64
OpusPacket
Definition: opus.h:85
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:82
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:127
ChannelMap
Definition: opus.h:147
silk_resample_delay
static const int silk_resample_delay[]
Definition: opusdec.c:62
map
const VDPAUPixFmtMap * map
Definition: hwcontext_vdpau.c:71
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
AVPacket
This structure stores compressed data.
Definition: packet.h:351
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:565
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:187
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
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_free
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:864
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1244
OpusContext
Definition: opus.h:162