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  int nb_samples)
368 {
369  int output_samples = 0;
370  int flush_needed = 0;
371  int i, j, ret;
372 
373  /* check if we need to flush the resampler */
374  if (swr_is_initialized(s->swr)) {
375  if (buf) {
376  int64_t cur_samplerate;
377  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
378  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
379  } else {
380  flush_needed = !!s->delayed_samples;
381  }
382  }
383 
384  if (!buf && !flush_needed)
385  return 0;
386 
387  /* use dummy output buffers if the channel is not mapped to anything */
388  if (!s->out[0] ||
389  (s->output_channels == 2 && !s->out[1])) {
391  if (!s->out_dummy)
392  return AVERROR(ENOMEM);
393  if (!s->out[0])
394  s->out[0] = s->out_dummy;
395  if (!s->out[1])
396  s->out[1] = s->out_dummy;
397  }
398 
399  /* flush the resampler if necessary */
400  if (flush_needed) {
402  if (ret < 0) {
403  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
404  return ret;
405  }
406  swr_close(s->swr);
407  output_samples += s->delayed_samples;
408  s->delayed_samples = 0;
409 
410  if (!buf)
411  goto finish;
412  }
413 
414  /* decode all the frames in the packet */
415  for (i = 0; i < s->packet.frame_count; i++) {
416  int size = s->packet.frame_size[i];
417  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
418 
419  if (samples < 0) {
420  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
422  return samples;
423 
424  for (j = 0; j < s->output_channels; j++)
425  memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
426  samples = s->packet.frame_duration;
427  }
428  output_samples += samples;
429 
430  for (j = 0; j < s->output_channels; j++)
431  s->out[j] += samples;
432  s->out_size -= samples * sizeof(float);
433  }
434 
435 finish:
436  s->out[0] = s->out[1] = NULL;
437  s->out_size = 0;
438 
439  return output_samples;
440 }
441 
442 static int opus_decode_packet(AVCodecContext *avctx, void *data,
443  int *got_frame_ptr, AVPacket *avpkt)
444 {
445  OpusContext *c = avctx->priv_data;
446  AVFrame *frame = data;
447  const uint8_t *buf = avpkt->data;
448  int buf_size = avpkt->size;
449  int coded_samples = 0;
450  int decoded_samples = 0;
451  int i, ret;
452  int delayed_samples = 0;
453 
454  for (i = 0; i < c->nb_streams; i++) {
455  OpusStreamContext *s = &c->streams[i];
456  s->out[0] =
457  s->out[1] = NULL;
458  delayed_samples = FFMAX(delayed_samples, s->delayed_samples);
459  }
460 
461  /* decode the header of the first sub-packet to find out the sample count */
462  if (buf) {
463  OpusPacket *pkt = &c->streams[0].packet;
464  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
465  if (ret < 0) {
466  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
467  return ret;
468  }
469  coded_samples += pkt->frame_count * pkt->frame_duration;
471  }
472 
473  frame->nb_samples = coded_samples + delayed_samples;
474 
475  /* no input or buffered data => nothing to do */
476  if (!frame->nb_samples) {
477  *got_frame_ptr = 0;
478  return 0;
479  }
480 
481  /* setup the data buffers */
482  ret = ff_get_buffer(avctx, frame, 0);
483  if (ret < 0)
484  return ret;
485  frame->nb_samples = 0;
486 
487  for (i = 0; i < avctx->channels; i++) {
488  ChannelMap *map = &c->channel_maps[i];
489  if (!map->copy)
490  c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
491  }
492 
493  for (i = 0; i < c->nb_streams; i++)
494  c->streams[i].out_size = frame->linesize[0];
495 
496  /* decode each sub-packet */
497  for (i = 0; i < c->nb_streams; i++) {
498  OpusStreamContext *s = &c->streams[i];
499 
500  if (i && buf) {
501  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
502  if (ret < 0) {
503  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
504  return ret;
505  }
506  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
507  av_log(avctx, AV_LOG_ERROR,
508  "Mismatching coded sample count in substream %d.\n", i);
509  return AVERROR_INVALIDDATA;
510  }
511 
513  }
514 
515  ret = opus_decode_subpacket(&c->streams[i], buf,
516  s->packet.data_size, coded_samples);
517  if (ret < 0)
518  return ret;
519  if (decoded_samples && ret != decoded_samples) {
520  av_log(avctx, AV_LOG_ERROR, "Different numbers of decoded samples "
521  "in a multi-channel stream\n");
522  return AVERROR_INVALIDDATA;
523  }
524  decoded_samples = ret;
525  buf += s->packet.packet_size;
526  buf_size -= s->packet.packet_size;
527  }
528 
529  for (i = 0; i < avctx->channels; i++) {
530  ChannelMap *map = &c->channel_maps[i];
531 
532  /* handle copied channels */
533  if (map->copy) {
534  memcpy(frame->extended_data[i],
535  frame->extended_data[map->copy_idx],
536  frame->linesize[0]);
537  } else if (map->silence) {
538  memset(frame->extended_data[i], 0, frame->linesize[0]);
539  }
540 
541  if (c->gain_i) {
542  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
543  (float*)frame->extended_data[i],
544  c->gain, FFALIGN(decoded_samples, 8));
545  }
546  }
547 
548  frame->nb_samples = decoded_samples;
549  *got_frame_ptr = !!decoded_samples;
550 
551  return avpkt->size;
552 }
553 
555 {
556  OpusContext *c = ctx->priv_data;
557  int i;
558 
559  for (i = 0; i < c->nb_streams; i++) {
560  OpusStreamContext *s = &c->streams[i];
561 
562  memset(&s->packet, 0, sizeof(s->packet));
563  s->delayed_samples = 0;
564 
565  if (s->celt_delay)
567  swr_close(s->swr);
568 
569  ff_silk_flush(s->silk);
570  ff_celt_flush(s->celt);
571  }
572 }
573 
575 {
576  OpusContext *c = avctx->priv_data;
577  int i;
578 
579  for (i = 0; i < c->nb_streams; i++) {
580  OpusStreamContext *s = &c->streams[i];
581 
582  ff_silk_free(&s->silk);
583  ff_celt_free(&s->celt);
584 
585  av_freep(&s->out_dummy);
587 
589  swr_free(&s->swr);
590  }
591 
592  av_freep(&c->streams);
593  c->nb_streams = 0;
594 
595  av_freep(&c->channel_maps);
596  av_freep(&c->fdsp);
597 
598  return 0;
599 }
600 
602 {
603  OpusContext *c = avctx->priv_data;
604  int ret, i, j;
605 
607  avctx->sample_rate = 48000;
608 
610  if (!c->fdsp)
611  return AVERROR(ENOMEM);
612 
613  /* find out the channel configuration */
614  ret = ff_opus_parse_extradata(avctx, c);
615  if (ret < 0)
616  return ret;
617 
618  /* allocate and init each independent decoder */
619  c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
620  if (!c->streams) {
621  c->nb_streams = 0;
622  ret = AVERROR(ENOMEM);
623  goto fail;
624  }
625 
626  for (i = 0; i < c->nb_streams; i++) {
627  OpusStreamContext *s = &c->streams[i];
628  uint64_t layout;
629 
630  s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
631 
632  s->avctx = avctx;
633 
634  for (j = 0; j < s->output_channels; j++) {
635  s->silk_output[j] = s->silk_buf[j];
636  s->celt_output[j] = s->celt_buf[j];
637  s->redundancy_output[j] = s->redundancy_buf[j];
638  }
639 
640  s->fdsp = c->fdsp;
641 
642  s->swr =swr_alloc();
643  if (!s->swr)
644  goto fail;
645 
647  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
648  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
649  av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
650  av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
651  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
652  av_opt_set_int(s->swr, "filter_size", 16, 0);
653 
654  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
655  if (ret < 0)
656  goto fail;
657 
658  ret = ff_celt_init(avctx, &s->celt, s->output_channels);
659  if (ret < 0)
660  goto fail;
661 
663  s->output_channels, 1024);
664  if (!s->celt_delay) {
665  ret = AVERROR(ENOMEM);
666  goto fail;
667  }
668  }
669 
670  return 0;
671 fail:
672  opus_decode_close(avctx);
673  return ret;
674 }
675 
677  .name = "opus",
678  .long_name = NULL_IF_CONFIG_SMALL("Opus"),
679  .type = AVMEDIA_TYPE_AUDIO,
680  .id = AV_CODEC_ID_OPUS,
681  .priv_data_size = sizeof(OpusContext),
683  .close = opus_decode_close,
686  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
687 };
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:88
static const uint8_t celt_band_end[]
Definition: opusdec.c:65
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: opusdec.c:574
void ff_celt_flush(CeltContext *s)
Definition: opus_celt.c:2146
float, planar
Definition: samplefmt.h:70
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#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:60
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
Definition: swresample.c:150
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:139
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:177
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:260
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:200
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:181
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:179
int size
Definition: avcodec.h:1163
RawBitsContext rb
Definition: opus.h:96
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
Definition: opus.h:224
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:3181
int16_t gain_i
Definition: opus.h:180
Macro definitions for various function/variable attributes.
#define FFALIGN(x, a)
Definition: common.h:71
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
unsigned int cacheval
Definition: opus.h:91
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition: opusdec.c:111
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
uint8_t
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:148
#define av_cold
Definition: attributes.h:74
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
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
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
void ff_celt_free(CeltContext **s)
Definition: opus_celt.c:2173
uint8_t * data
Definition: avcodec.h:1162
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 av_log(a,...)
const float ff_celt_window2[120]
Definition: opus_celt.c:466
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: opusdec.c:601
AVFloatDSPContext * fdsp
Definition: opus.h:130
ChannelMap * channel_maps
Definition: opus.h:183
libswresample public header
int nb_streams
Definition: opus.h:176
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2623
#define 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:824
#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:175
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:491
AVFloatDSPContext * fdsp
Definition: opus.h:179
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
#define FFMAX(a, b)
Definition: common.h:64
Libavcodec external API header.
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:186
int ff_celt_init(AVCodecContext *avctx, CeltContext **s, int output_channels)
Definition: opus_celt.c:2188
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:2612
int frame_size[MAX_FRAMES]
frame sizes
Definition: opus.h:116
#define FFMIN(a, b)
Definition: common.h:66
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:117
ret
Definition: avfilter.c:974
float celt_buf[2][960]
Definition: opus.h:134
SwrContext * swr
Definition: opus.h:147
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
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:514
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:823
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
OpusPacket packet
Definition: opus.h:153
int sample_rate
samples per second
Definition: avcodec.h:1985
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:441
AVCodec ff_opus_decoder
Definition: opusdec.c:676
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:1567
main external API structure.
Definition: avcodec.h:1241
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:139
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
GetBitContext gb
Definition: opus.h:95
void * buf
Definition: avisynth_c.h:553
#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
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:143
static int opus_decode_subpacket(OpusStreamContext *s, const uint8_t *buf, int buf_size, int nb_samples)
Definition: opusdec.c:365
int stereo
whether this packet is mono or stereo
Definition: opus.h:110
AVCodecContext * avctx
Definition: opus.h:123
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:686
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:113
static int opus_decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: opusdec.c:442
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:159
static int opus_init_resample(OpusStreamContext *s)
Definition: opusdec.c:154
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
unsigned int range
Definition: opus.h:97
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:513
static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
Definition: opus.h:186
static void finish(void)
Definition: ffhash.c:62
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
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:293
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:1976
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:1283
Audio FIFO Buffer.
int len
int channels
number of audio channels
Definition: avcodec.h:1986
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:245
enum OpusBandwidth bandwidth
bandwidth
Definition: opus.h:119
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition: opusdec.c:554
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
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
AVAudioFifo * celt_delay
Definition: opus.h:148
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:289
#define av_freep(p)
int silk_samplerate
Definition: opus.h:149
const uint8_t * position
Definition: opus.h:88
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
#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:682
This structure stores compressed data.
Definition: avcodec.h:1139
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:154