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