FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
51 static const uint16_t silk_frame_duration_ms[16] = {
52  10, 20, 40, 60,
53  10, 20, 40, 60,
54  10, 20, 40, 60,
55  10, 20,
56  10, 20,
57 };
58 
59 /* number of samples of silence to feed to the resampler
60  * at the beginning */
61 static const int silk_resample_delay[] = {
62  4, 8, 11, 11, 11
63 };
64 
65 static const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 };
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 /**
77  * Range decoder
78  */
79 static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
80 {
81  int ret = init_get_bits8(&rc->gb, data, size);
82  if (ret < 0)
83  return ret;
84 
85  rc->range = 128;
86  rc->value = 127 - get_bits(&rc->gb, 7);
87  rc->total_read_bits = 9;
89 
90  return 0;
91 }
92 
93 static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend,
94  unsigned int bytes)
95 {
96  rc->rb.position = rightend;
97  rc->rb.bytes = bytes;
98  rc->rb.cachelen = 0;
99  rc->rb.cacheval = 0;
100 }
101 
102 static void opus_fade(float *out,
103  const float *in1, const float *in2,
104  const float *window, int len)
105 {
106  int i;
107  for (i = 0; i < len; i++)
108  out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
109 }
110 
111 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
112 {
113  int celt_size = av_audio_fifo_size(s->celt_delay);
114  int ret, i;
115  ret = swr_convert(s->swr,
116  (uint8_t**)s->out, nb_samples,
117  NULL, 0);
118  if (ret < 0)
119  return ret;
120  else if (ret != nb_samples) {
121  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
122  ret);
123  return AVERROR_BUG;
124  }
125 
126  if (celt_size) {
127  if (celt_size != nb_samples) {
128  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
129  return AVERROR_BUG;
130  }
131  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
132  for (i = 0; i < s->output_channels; i++) {
133  s->fdsp->vector_fmac_scalar(s->out[i],
134  s->celt_output[i], 1.0,
135  nb_samples);
136  }
137  }
138 
139  if (s->redundancy_idx) {
140  for (i = 0; i < s->output_channels; i++)
141  opus_fade(s->out[i], s->out[i],
142  s->redundancy_output[i] + 120 + s->redundancy_idx,
144  s->redundancy_idx = 0;
145  }
146 
147  s->out[0] += nb_samples;
148  s->out[1] += nb_samples;
149  s->out_size -= nb_samples * sizeof(float);
150 
151  return 0;
152 }
153 
155 {
156  static const float delay[16] = { 0.0 };
157  const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
158  int ret;
159 
160  av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
161  ret = swr_init(s->swr);
162  if (ret < 0) {
163  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
164  return ret;
165  }
166 
167  ret = swr_convert(s->swr,
168  NULL, 0,
169  delayptr, silk_resample_delay[s->packet.bandwidth]);
170  if (ret < 0) {
172  "Error feeding initial silence to the resampler.\n");
173  return ret;
174  }
175 
176  return 0;
177 }
178 
180 {
181  int ret;
182  enum OpusBandwidth bw = s->packet.bandwidth;
183 
184  if (s->packet.mode == OPUS_MODE_SILK &&
187 
188  ret = opus_rc_init(&s->redundancy_rc, data, size);
189  if (ret < 0)
190  goto fail;
191  opus_raw_init(&s->redundancy_rc, data + size, size);
192 
195  s->packet.stereo + 1, 240,
197  if (ret < 0)
198  goto fail;
199 
200  return 0;
201 fail:
202  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
203  return ret;
204 }
205 
207 {
208  int samples = s->packet.frame_duration;
209  int redundancy = 0;
210  int redundancy_size, redundancy_pos;
211  int ret, i, consumed;
212  int delayed_samples = s->delayed_samples;
213 
214  ret = opus_rc_init(&s->rc, data, size);
215  if (ret < 0)
216  return ret;
217 
218  /* decode the silk frame */
219  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
220  if (!swr_is_initialized(s->swr)) {
221  ret = opus_init_resample(s);
222  if (ret < 0)
223  return ret;
224  }
225 
226  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
228  s->packet.stereo + 1,
230  if (samples < 0) {
231  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
232  return samples;
233  }
234  samples = swr_convert(s->swr,
235  (uint8_t**)s->out, s->packet.frame_duration,
236  (const uint8_t**)s->silk_output, samples);
237  if (samples < 0) {
238  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
239  return samples;
240  }
241  av_assert2((samples & 7) == 0);
242  s->delayed_samples += s->packet.frame_duration - samples;
243  } else
244  ff_silk_flush(s->silk);
245 
246  // decode redundancy information
247  consumed = opus_rc_tell(&s->rc);
248  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
249  redundancy = opus_rc_p2model(&s->rc, 12);
250  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
251  redundancy = 1;
252 
253  if (redundancy) {
254  redundancy_pos = opus_rc_p2model(&s->rc, 1);
255 
256  if (s->packet.mode == OPUS_MODE_HYBRID)
257  redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2;
258  else
259  redundancy_size = size - (consumed + 7) / 8;
260  size -= redundancy_size;
261  if (size < 0) {
262  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
263  return AVERROR_INVALIDDATA;
264  }
265 
266  if (redundancy_pos) {
267  ret = opus_decode_redundancy(s, data + size, redundancy_size);
268  if (ret < 0)
269  return ret;
270  ff_celt_flush(s->celt);
271  }
272  }
273 
274  /* decode the CELT frame */
275  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
276  float *out_tmp[2] = { s->out[0], s->out[1] };
277  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
278  out_tmp : s->celt_output;
279  int celt_output_samples = samples;
280  int delay_samples = av_audio_fifo_size(s->celt_delay);
281 
282  if (delay_samples) {
283  if (s->packet.mode == OPUS_MODE_HYBRID) {
284  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
285 
286  for (i = 0; i < s->output_channels; i++) {
287  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
288  delay_samples);
289  out_tmp[i] += delay_samples;
290  }
291  celt_output_samples -= delay_samples;
292  } else {
294  "Spurious CELT delay samples present.\n");
295  av_audio_fifo_drain(s->celt_delay, delay_samples);
297  return AVERROR_BUG;
298  }
299  }
300 
301  opus_raw_init(&s->rc, data + size, size);
302 
303  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
304  s->packet.stereo + 1,
306  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
308  if (ret < 0)
309  return ret;
310 
311  if (s->packet.mode == OPUS_MODE_HYBRID) {
312  int celt_delay = s->packet.frame_duration - celt_output_samples;
313  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
314  s->celt_output[1] + celt_output_samples };
315 
316  for (i = 0; i < s->output_channels; i++) {
317  s->fdsp->vector_fmac_scalar(out_tmp[i],
318  s->celt_output[i], 1.0,
319  celt_output_samples);
320  }
321 
322  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
323  if (ret < 0)
324  return ret;
325  }
326  } else
327  ff_celt_flush(s->celt);
328 
329  if (s->redundancy_idx) {
330  for (i = 0; i < s->output_channels; i++)
331  opus_fade(s->out[i], s->out[i],
332  s->redundancy_output[i] + 120 + s->redundancy_idx,
334  s->redundancy_idx = 0;
335  }
336  if (redundancy) {
337  if (!redundancy_pos) {
338  ff_celt_flush(s->celt);
339  ret = opus_decode_redundancy(s, data + size, redundancy_size);
340  if (ret < 0)
341  return ret;
342 
343  for (i = 0; i < s->output_channels; i++) {
344  opus_fade(s->out[i] + samples - 120 + delayed_samples,
345  s->out[i] + samples - 120 + delayed_samples,
346  s->redundancy_output[i] + 120,
347  ff_celt_window2, 120 - delayed_samples);
348  if (delayed_samples)
349  s->redundancy_idx = 120 - delayed_samples;
350  }
351  } else {
352  for (i = 0; i < s->output_channels; i++) {
353  memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
354  opus_fade(s->out[i] + 120 + delayed_samples,
355  s->redundancy_output[i] + 120,
356  s->out[i] + 120 + delayed_samples,
357  ff_celt_window2, 120);
358  }
359  }
360  }
361 
362  return samples;
363 }
364 
366  const uint8_t *buf, int buf_size,
367  float **out, int out_size,
368  int nb_samples)
369 {
370  int output_samples = 0;
371  int flush_needed = 0;
372  int i, j, ret;
373 
374  s->out[0] = out[0];
375  s->out[1] = out[1];
376  s->out_size = out_size;
377 
378  /* check if we need to flush the resampler */
379  if (swr_is_initialized(s->swr)) {
380  if (buf) {
381  int64_t cur_samplerate;
382  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
383  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
384  } else {
385  flush_needed = !!s->delayed_samples;
386  }
387  }
388 
389  if (!buf && !flush_needed)
390  return 0;
391 
392  /* use dummy output buffers if the channel is not mapped to anything */
393  if (!s->out[0] ||
394  (s->output_channels == 2 && !s->out[1])) {
396  if (!s->out_dummy)
397  return AVERROR(ENOMEM);
398  if (!s->out[0])
399  s->out[0] = s->out_dummy;
400  if (!s->out[1])
401  s->out[1] = s->out_dummy;
402  }
403 
404  /* flush the resampler if necessary */
405  if (flush_needed) {
407  if (ret < 0) {
408  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
409  return ret;
410  }
411  swr_close(s->swr);
412  output_samples += s->delayed_samples;
413  s->delayed_samples = 0;
414 
415  if (!buf)
416  goto finish;
417  }
418 
419  /* decode all the frames in the packet */
420  for (i = 0; i < s->packet.frame_count; i++) {
421  int size = s->packet.frame_size[i];
422  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
423 
424  if (samples < 0) {
425  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
427  return samples;
428 
429  for (j = 0; j < s->output_channels; j++)
430  memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
431  samples = s->packet.frame_duration;
432  }
433  output_samples += samples;
434 
435  for (j = 0; j < s->output_channels; j++)
436  s->out[j] += samples;
437  s->out_size -= samples * sizeof(float);
438  }
439 
440 finish:
441  s->out[0] = s->out[1] = NULL;
442  s->out_size = 0;
443 
444  return output_samples;
445 }
446 
447 static int opus_decode_packet(AVCodecContext *avctx, void *data,
448  int *got_frame_ptr, AVPacket *avpkt)
449 {
450  OpusContext *c = avctx->priv_data;
451  AVFrame *frame = data;
452  const uint8_t *buf = avpkt->data;
453  int buf_size = avpkt->size;
454  int coded_samples = 0;
455  int decoded_samples = INT_MAX;
456  int delayed_samples = 0;
457  int i, ret;
458 
459  /* calculate the number of delayed samples */
460  for (i = 0; i < c->nb_streams; i++) {
461  OpusStreamContext *s = &c->streams[i];
462  s->out[0] =
463  s->out[1] = NULL;
464  delayed_samples = FFMAX(delayed_samples,
466  }
467 
468  /* decode the header of the first sub-packet to find out the sample count */
469  if (buf) {
470  OpusPacket *pkt = &c->streams[0].packet;
471  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
472  if (ret < 0) {
473  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
474  return ret;
475  }
476  coded_samples += pkt->frame_count * pkt->frame_duration;
478  }
479 
480  frame->nb_samples = coded_samples + delayed_samples;
481 
482  /* no input or buffered data => nothing to do */
483  if (!frame->nb_samples) {
484  *got_frame_ptr = 0;
485  return 0;
486  }
487 
488  /* setup the data buffers */
489  ret = ff_get_buffer(avctx, frame, 0);
490  if (ret < 0)
491  return ret;
492  frame->nb_samples = 0;
493 
494  memset(c->out, 0, c->nb_streams * 2 * sizeof(*c->out));
495  for (i = 0; i < avctx->channels; i++) {
496  ChannelMap *map = &c->channel_maps[i];
497  if (!map->copy)
498  c->out[2 * map->stream_idx + map->channel_idx] = (float*)frame->extended_data[i];
499  }
500 
501  /* read the data from the sync buffers */
502  for (i = 0; i < c->nb_streams; i++) {
503  float **out = c->out + 2 * i;
504  int sync_size = av_audio_fifo_size(c->sync_buffers[i]);
505 
506  float sync_dummy[32];
507  int out_dummy = (!out[0]) | ((!out[1]) << 1);
508 
509  if (!out[0])
510  out[0] = sync_dummy;
511  if (!out[1])
512  out[1] = sync_dummy;
513  if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
514  return AVERROR_BUG;
515 
516  ret = av_audio_fifo_read(c->sync_buffers[i], (void**)out, sync_size);
517  if (ret < 0)
518  return ret;
519 
520  if (out_dummy & 1)
521  out[0] = NULL;
522  else
523  out[0] += ret;
524  if (out_dummy & 2)
525  out[1] = NULL;
526  else
527  out[1] += ret;
528 
529  c->out_size[i] = frame->linesize[0] - ret * sizeof(float);
530  }
531 
532  /* decode each sub-packet */
533  for (i = 0; i < c->nb_streams; i++) {
534  OpusStreamContext *s = &c->streams[i];
535 
536  if (i && buf) {
537  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
538  if (ret < 0) {
539  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
540  return ret;
541  }
542  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
543  av_log(avctx, AV_LOG_ERROR,
544  "Mismatching coded sample count in substream %d.\n", i);
545  return AVERROR_INVALIDDATA;
546  }
547 
549  }
550 
551  ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
552  c->out + 2 * i, c->out_size[i], coded_samples);
553  if (ret < 0)
554  return ret;
555  c->decoded_samples[i] = ret;
556  decoded_samples = FFMIN(decoded_samples, ret);
557 
558  buf += s->packet.packet_size;
559  buf_size -= s->packet.packet_size;
560  }
561 
562  /* buffer the extra samples */
563  for (i = 0; i < c->nb_streams; i++) {
564  int buffer_samples = c->decoded_samples[i] - decoded_samples;
565  if (buffer_samples) {
566  float *buf[2] = { c->out[2 * i + 0] ? c->out[2 * i + 0] : (float*)frame->extended_data[0],
567  c->out[2 * i + 1] ? c->out[2 * i + 1] : (float*)frame->extended_data[0] };
568  buf[0] += decoded_samples;
569  buf[1] += decoded_samples;
570  ret = av_audio_fifo_write(c->sync_buffers[i], (void**)buf, buffer_samples);
571  if (ret < 0)
572  return ret;
573  }
574  }
575 
576  for (i = 0; i < avctx->channels; i++) {
577  ChannelMap *map = &c->channel_maps[i];
578 
579  /* handle copied channels */
580  if (map->copy) {
581  memcpy(frame->extended_data[i],
582  frame->extended_data[map->copy_idx],
583  frame->linesize[0]);
584  } else if (map->silence) {
585  memset(frame->extended_data[i], 0, frame->linesize[0]);
586  }
587 
588  if (c->gain_i && decoded_samples > 0) {
589  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
590  (float*)frame->extended_data[i],
591  c->gain, FFALIGN(decoded_samples, 8));
592  }
593  }
594 
595  frame->nb_samples = decoded_samples;
596  *got_frame_ptr = !!decoded_samples;
597 
598  return avpkt->size;
599 }
600 
602 {
603  OpusContext *c = ctx->priv_data;
604  int i;
605 
606  for (i = 0; i < c->nb_streams; i++) {
607  OpusStreamContext *s = &c->streams[i];
608 
609  memset(&s->packet, 0, sizeof(s->packet));
610  s->delayed_samples = 0;
611 
612  if (s->celt_delay)
614  swr_close(s->swr);
615 
617 
618  ff_silk_flush(s->silk);
619  ff_celt_flush(s->celt);
620  }
621 }
622 
624 {
625  OpusContext *c = avctx->priv_data;
626  int i;
627 
628  for (i = 0; i < c->nb_streams; i++) {
629  OpusStreamContext *s = &c->streams[i];
630 
631  ff_silk_free(&s->silk);
632  ff_celt_free(&s->celt);
633 
634  av_freep(&s->out_dummy);
636 
638  swr_free(&s->swr);
639  }
640 
641  av_freep(&c->streams);
642 
643  if (c->sync_buffers) {
644  for (i = 0; i < c->nb_streams; i++)
646  }
647  av_freep(&c->sync_buffers);
649  av_freep(&c->out);
650  av_freep(&c->out_size);
651 
652  c->nb_streams = 0;
653 
654  av_freep(&c->channel_maps);
655  av_freep(&c->fdsp);
656 
657  return 0;
658 }
659 
661 {
662  OpusContext *c = avctx->priv_data;
663  int ret, i, j;
664 
666  avctx->sample_rate = 48000;
667 
669  if (!c->fdsp)
670  return AVERROR(ENOMEM);
671 
672  /* find out the channel configuration */
673  ret = ff_opus_parse_extradata(avctx, c);
674  if (ret < 0) {
675  av_freep(&c->channel_maps);
676  av_freep(&c->fdsp);
677  return ret;
678  }
679 
680  /* allocate and init each independent decoder */
681  c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
682  c->out = av_mallocz_array(c->nb_streams, 2 * sizeof(*c->out));
683  c->out_size = av_mallocz_array(c->nb_streams, sizeof(*c->out_size));
686  if (!c->streams || !c->sync_buffers || !c->decoded_samples || !c->out || !c->out_size) {
687  c->nb_streams = 0;
688  ret = AVERROR(ENOMEM);
689  goto fail;
690  }
691 
692  for (i = 0; i < c->nb_streams; i++) {
693  OpusStreamContext *s = &c->streams[i];
694  uint64_t layout;
695 
696  s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
697 
698  s->avctx = avctx;
699 
700  for (j = 0; j < s->output_channels; j++) {
701  s->silk_output[j] = s->silk_buf[j];
702  s->celt_output[j] = s->celt_buf[j];
703  s->redundancy_output[j] = s->redundancy_buf[j];
704  }
705 
706  s->fdsp = c->fdsp;
707 
708  s->swr =swr_alloc();
709  if (!s->swr)
710  goto fail;
711 
713  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
714  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
715  av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
716  av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
717  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
718  av_opt_set_int(s->swr, "filter_size", 16, 0);
719 
720  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
721  if (ret < 0)
722  goto fail;
723 
724  ret = ff_celt_init(avctx, &s->celt, s->output_channels);
725  if (ret < 0)
726  goto fail;
727 
729  s->output_channels, 1024);
730  if (!s->celt_delay) {
731  ret = AVERROR(ENOMEM);
732  goto fail;
733  }
734 
736  s->output_channels, 32);
737  if (!c->sync_buffers[i]) {
738  ret = AVERROR(ENOMEM);
739  goto fail;
740  }
741  }
742 
743  return 0;
744 fail:
745  opus_decode_close(avctx);
746  return ret;
747 }
748 
750  .name = "opus",
751  .long_name = NULL_IF_CONFIG_SMALL("Opus"),
752  .type = AVMEDIA_TYPE_AUDIO,
753  .id = AV_CODEC_ID_OPUS,
754  .priv_data_size = sizeof(OpusContext),
756  .close = opus_decode_close,
759  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
760 };
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:89
static const uint8_t celt_band_end[]
Definition: opusdec.c:65
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: opusdec.c:623
void ff_celt_flush(CeltContext *s)
Definition: opus_celt.c:2147
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
Definition: swresample.c:151
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:181
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVAudioFifo ** sync_buffers
Definition: opus.h:182
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
static const uint16_t silk_frame_duration_ms[16]
Definition: opusdec.c:51
int frame_count
frame count
Definition: opus.h:114
int nb_stereo_streams
Definition: opus.h:187
float redundancy_buf[2][960]
Definition: opus.h:137
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, unsigned int bytes)
Definition: opusdec.c:93
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static FFServerConfig config
Definition: ffserver.c:202
int output_channels
Definition: opus.h:124
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
int delayed_samples
Definition: opus.h:151
float gain
Definition: opus.h:191
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:179
int size
Definition: avcodec.h:1602
RawBitsContext rb
Definition: opus.h:96
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
Definition: opus.h:234
int out_size
Definition: movenc.c:55
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:3600
int16_t gain_i
Definition: opus.h:190
Macro definitions for various function/variable attributes.
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
Definition: float_dsp.h:54
#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: avcodec.h:984
unsigned int cacheval
Definition: opus.h:91
int * decoded_samples
Definition: opus.h:184
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition: opusdec.c:111
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2446
uint8_t
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:149
#define av_cold
Definition: attributes.h:82
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOptions.
unsigned int total_read_bits
Definition: opus.h:99
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:206
int copy
Definition: opus.h:166
SilkContext * silk
Definition: opus.h:128
static AVFrame * frame
void ff_celt_free(CeltContext **s)
Definition: opus_celt.c:2174
static void finish(void)
Definition: movenc.c:344
uint8_t * data
Definition: avcodec.h:1601
bitstream reader API header.
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition: opusdec.c:102
ptrdiff_t size
Definition: opengl_enc.c:101
float * silk_output[2]
Definition: opus.h:133
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
const float ff_celt_window2[120]
Definition: opus_celt.c:467
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: opusdec.c:660
AVFloatDSPContext * fdsp
Definition: opus.h:130
ChannelMap * channel_maps
Definition: opus.h:193
libswresample public header
int nb_streams
Definition: opus.h:186
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
unsigned int value
Definition: opus.h:98
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:539
AVFloatDSPContext * fdsp
Definition: opus.h:189
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
#define FFMAX(a, b)
Definition: common.h:94
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
#define fail()
Definition: checkasm.h:83
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:499
static SDL_Window * window
Definition: ffplay.c:361
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels)
Definition: opus_celt.c:2189
audio channel layout utility functions
float * out[2]
Definition: opus.h:141
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2964
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:116
#define FFMIN(a, b)
Definition: common.h:96
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:117
float celt_buf[2][960]
Definition: opus.h:134
SwrContext * swr
Definition: opus.h:147
AVFormatContext * ctx
Definition: movenc.c:48
int out_dummy_allocated_size
Definition: opus.h:145
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:69
float silk_buf[2][960]
Definition: opus.h:132
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2975
#define FF_ARRAY_ELEMS(a)
int silence
Definition: opus.h:171
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:853
static int get_silk_samplerate(int config)
Definition: opusdec.c:67
unsigned int bytes
Definition: opus.h:89
float * out_dummy
Definition: opus.h:144
unsigned int cachelen
Definition: opus.h:90
Libavcodec external API header.
OpusPacket packet
Definition: opus.h:153
int sample_rate
samples per second
Definition: avcodec.h:2438
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:437
AVCodec ff_opus_decoder
Definition: opusdec.c:749
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:1567
main external API structure.
Definition: avcodec.h:1676
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:1575
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:140
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
GetBitContext gb
Definition: opus.h:95
void * buf
Definition: avisynth_c.h:690
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int config
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:112
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:1562
enum OpusMode mode
mode
Definition: opus.h:118
int copy_idx
Definition: opus.h:168
const VDPAUPixFmtMap * map
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:119
int stereo
whether this packet is mono or stereo
Definition: opus.h:110
AVCodecContext * avctx
Definition: opus.h:123
float ** out
Definition: opus.h:178
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:707
int data_size
size of the useful data – packet size - padding
Definition: opus.h:108
int channel_idx
Definition: opus.h:161
CeltContext * celt
Definition: opus.h:129
int redundancy_idx
Definition: opus.h:155
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
static int opus_decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: opusdec.c:447
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
static int opus_init_resample(OpusStreamContext *s)
Definition: opusdec.c:154
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:365
unsigned int range
Definition: opus.h:97
static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
Definition: opus.h:196
float * celt_output[2]
Definition: opus.h:135
common internal api header.
OpusRangeCoder rc
Definition: opus.h:126
int stream_idx
Definition: opus.h:160
int * out_size
Definition: opus.h:179
OpusBandwidth
Definition: opus.h:79
static double c[64]
static const int silk_resample_delay[]
Definition: opusdec.c:61
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:1498
static av_always_inline unsigned int opus_rc_unimodel(OpusRangeCoder *rc, unsigned int size)
CELT: read a uniform distribution.
Definition: opus.h:303
int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, float **output, int coded_channels, int frame_size, int startband, int endband)
Definition: opus_celt.c:1977
OpusStreamContext * streams
Definition: opus.h:175
int packet_size
packet size
Definition: opus.h:107
OpusRangeCoder redundancy_rc
Definition: opus.h:127
void * priv_data
Definition: avcodec.h:1718
Audio FIFO Buffer.
int len
int channels
number of audio channels
Definition: avcodec.h:2439
int frame_offset[MAX_FRAMES]
frame offsets
Definition: opus.h:115
static av_always_inline unsigned int opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame...
Definition: opus.h:255
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:119
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition: opusdec.c:601
static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Range decoder.
Definition: opusdec.c:79
float * redundancy_output[2]
Definition: opus.h:138
uint64_t layout
AVAudioFifo * celt_delay
Definition: opus.h:148
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:290
FILE * out
Definition: movenc.c:54
#define av_freep(p)
int silk_samplerate
Definition: opus.h:149
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
const uint8_t * position
Definition: opus.h:88
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:231
#define AV_CH_LAYOUT_MONO
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
Definition: swresample.c:703
This structure stores compressed data.
Definition: avcodec.h:1578
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:155