Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
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 "celp_filters.h"
47 #include "fft.h"
48 #include "get_bits.h"
49 #include "internal.h"
50 #include "mathops.h"
51 #include "opus.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 const uint8_t celt_band_end[] = { 13, 17, 17, 19, 21 };
68 
69 static int get_silk_samplerate(int config)
70 {
71  if (config < 4)
72  return 8000;
73  else if (config < 8)
74  return 12000;
75  return 16000;
76 }
77 
81 static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
82 {
83  int ret = init_get_bits8(&rc->gb, data, size);
84  if (ret < 0)
85  return ret;
86 
87  rc->range = 128;
88  rc->value = 127 - get_bits(&rc->gb, 7);
89  rc->total_read_bits = 9;
91 
92  return 0;
93 }
94 
95 static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend,
96  unsigned int bytes)
97 {
98  rc->rb.position = rightend;
99  rc->rb.bytes = bytes;
100  rc->rb.cachelen = 0;
101  rc->rb.cacheval = 0;
102 }
103 
104 static void opus_fade(float *out,
105  const float *in1, const float *in2,
106  const float *window, int len)
107 {
108  int i;
109  for (i = 0; i < len; i++)
110  out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
111 }
112 
113 static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
114 {
115  int celt_size = av_audio_fifo_size(s->celt_delay);
116  int ret, i;
117 
118  ret = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size, nb_samples,
119  NULL, 0, 0);
120  if (ret < 0)
121  return ret;
122  else if (ret != nb_samples) {
123  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
124  ret);
125  return AVERROR_BUG;
126  }
127 
128  if (celt_size) {
129  if (celt_size != nb_samples) {
130  av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
131  return AVERROR_BUG;
132  }
133  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
134  for (i = 0; i < s->output_channels; i++) {
135  s->fdsp->vector_fmac_scalar(s->out[i],
136  s->celt_output[i], 1.0,
137  nb_samples);
138  }
139  }
140 
141  if (s->redundancy_idx) {
142  for (i = 0; i < s->output_channels; i++)
143  opus_fade(s->out[i], s->out[i],
144  s->redundancy_output[i] + 120 + s->redundancy_idx,
146  s->redundancy_idx = 0;
147  }
148 
149  s->out[0] += nb_samples;
150  s->out[1] += nb_samples;
151  s->out_size -= nb_samples * sizeof(float);
152 
153  return 0;
154 }
155 
157 {
158  float delay[16] = { 0.0 };
159  uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
160  int ret;
161 
162  av_opt_set_int(s->avr, "in_sample_rate", s->silk_samplerate, 0);
163  ret = avresample_open(s->avr);
164  if (ret < 0) {
165  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
166  return ret;
167  }
168 
169  ret = avresample_convert(s->avr, NULL, 0, 0, delayptr, sizeof(delay),
171  if (ret < 0) {
173  "Error feeding initial silence to the resampler.\n");
174  return ret;
175  }
176 
177  return 0;
178 }
179 
181 {
182  int ret;
183  enum OpusBandwidth bw = s->packet.bandwidth;
184 
185  if (s->packet.mode == OPUS_MODE_SILK &&
188 
189  ret = opus_rc_init(&s->redundancy_rc, data, size);
190  if (ret < 0)
191  goto fail;
192  opus_raw_init(&s->redundancy_rc, data + size, size);
193 
196  s->packet.stereo + 1, 240,
198  if (ret < 0)
199  goto fail;
200 
201  return 0;
202 fail:
203  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
204  return ret;
205 }
206 
208 {
209  int samples = s->packet.frame_duration;
210  int redundancy = 0;
211  int redundancy_size, redundancy_pos;
212  int ret, i, consumed;
213  int delayed_samples = s->delayed_samples;
214 
215  ret = opus_rc_init(&s->rc, data, size);
216  if (ret < 0)
217  return ret;
218 
219  /* decode the silk frame */
220  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
221  if (!avresample_is_open(s->avr)) {
222  ret = opus_init_resample(s);
223  if (ret < 0)
224  return ret;
225  }
226 
227  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
229  s->packet.stereo + 1,
231  if (samples < 0) {
232  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
233  return samples;
234  }
235 
236  samples = avresample_convert(s->avr, (uint8_t**)s->out, s->out_size,
238  (uint8_t**)s->silk_output,
239  sizeof(s->silk_buf[0]),
240  samples);
241  if (samples < 0) {
242  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
243  return samples;
244  }
245  s->delayed_samples += s->packet.frame_duration - samples;
246  } else
247  ff_silk_flush(s->silk);
248 
249  // decode redundancy information
250  consumed = opus_rc_tell(&s->rc);
251  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
252  redundancy = opus_rc_p2model(&s->rc, 12);
253  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
254  redundancy = 1;
255 
256  if (redundancy) {
257  redundancy_pos = opus_rc_p2model(&s->rc, 1);
258 
259  if (s->packet.mode == OPUS_MODE_HYBRID)
260  redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2;
261  else
262  redundancy_size = size - (consumed + 7) / 8;
263  size -= redundancy_size;
264  if (size < 0) {
265  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
266  return AVERROR_INVALIDDATA;
267  }
268 
269  if (redundancy_pos) {
270  ret = opus_decode_redundancy(s, data + size, redundancy_size);
271  if (ret < 0)
272  return ret;
273  ff_celt_flush(s->celt);
274  }
275  }
276 
277  /* decode the CELT frame */
278  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
279  float *out_tmp[2] = { s->out[0], s->out[1] };
280  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
281  out_tmp : s->celt_output;
282  int celt_output_samples = samples;
283  int delay_samples = av_audio_fifo_size(s->celt_delay);
284 
285  if (delay_samples) {
286  if (s->packet.mode == OPUS_MODE_HYBRID) {
287  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
288 
289  for (i = 0; i < s->output_channels; i++) {
290  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
291  delay_samples);
292  out_tmp[i] += delay_samples;
293  }
294  celt_output_samples -= delay_samples;
295  } else {
297  "Spurious CELT delay samples present.\n");
298  av_audio_fifo_drain(s->celt_delay, delay_samples);
300  return AVERROR_BUG;
301  }
302  }
303 
304  opus_raw_init(&s->rc, data + size, size);
305 
306  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
307  s->packet.stereo + 1,
309  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
311  if (ret < 0)
312  return ret;
313 
314  if (s->packet.mode == OPUS_MODE_HYBRID) {
315  int celt_delay = s->packet.frame_duration - celt_output_samples;
316  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
317  s->celt_output[1] + celt_output_samples };
318 
319  for (i = 0; i < s->output_channels; i++) {
320  s->fdsp->vector_fmac_scalar(out_tmp[i],
321  s->celt_output[i], 1.0,
322  celt_output_samples);
323  }
324 
325  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
326  if (ret < 0)
327  return ret;
328  }
329  } else
330  ff_celt_flush(s->celt);
331 
332  if (s->redundancy_idx) {
333  for (i = 0; i < s->output_channels; i++)
334  opus_fade(s->out[i], s->out[i],
335  s->redundancy_output[i] + 120 + s->redundancy_idx,
337  s->redundancy_idx = 0;
338  }
339  if (redundancy) {
340  if (!redundancy_pos) {
341  ff_celt_flush(s->celt);
342  ret = opus_decode_redundancy(s, data + size, redundancy_size);
343  if (ret < 0)
344  return ret;
345 
346  for (i = 0; i < s->output_channels; i++) {
347  opus_fade(s->out[i] + samples - 120 + delayed_samples,
348  s->out[i] + samples - 120 + delayed_samples,
349  s->redundancy_output[i] + 120,
350  ff_celt_window2, 120 - delayed_samples);
351  if (delayed_samples)
352  s->redundancy_idx = 120 - delayed_samples;
353  }
354  } else {
355  for (i = 0; i < s->output_channels; i++) {
356  memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
357  opus_fade(s->out[i] + 120 + delayed_samples,
358  s->redundancy_output[i] + 120,
359  s->out[i] + 120 + delayed_samples,
360  ff_celt_window2, 120);
361  }
362  }
363  }
364 
365  return samples;
366 }
367 
369  const uint8_t *buf, int buf_size,
370  float **out, int out_size,
371  int nb_samples)
372 {
373  int output_samples = 0;
374  int flush_needed = 0;
375  int i, j, ret;
376 
377  s->out[0] = out[0];
378  s->out[1] = out[1];
379  s->out_size = out_size;
380 
381  /* check if we need to flush the resampler */
382  if (avresample_is_open(s->avr)) {
383  if (buf) {
384  int64_t cur_samplerate;
385  av_opt_get_int(s->avr, "in_sample_rate", 0, &cur_samplerate);
386  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
387  } else {
388  flush_needed = !!s->delayed_samples;
389  }
390  }
391 
392  if (!buf && !flush_needed)
393  return 0;
394 
395  /* use dummy output buffers if the channel is not mapped to anything */
396  if (!s->out[0] ||
397  (s->output_channels == 2 && !s->out[1])) {
399  if (!s->out_dummy)
400  return AVERROR(ENOMEM);
401  if (!s->out[0])
402  s->out[0] = s->out_dummy;
403  if (!s->out[1])
404  s->out[1] = s->out_dummy;
405  }
406 
407  /* flush the resampler if necessary */
408  if (flush_needed) {
410  if (ret < 0) {
411  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
412  return ret;
413  }
414  avresample_close(s->avr);
415  output_samples += s->delayed_samples;
416  s->delayed_samples = 0;
417 
418  if (!buf)
419  goto finish;
420  }
421 
422  /* decode all the frames in the packet */
423  for (i = 0; i < s->packet.frame_count; i++) {
424  int size = s->packet.frame_size[i];
425  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
426 
427  if (samples < 0) {
428  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
430  return samples;
431 
432  for (j = 0; j < s->output_channels; j++)
433  memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
434  samples = s->packet.frame_duration;
435  }
436  output_samples += samples;
437 
438  for (j = 0; j < s->output_channels; j++)
439  s->out[j] += samples;
440  s->out_size -= samples * sizeof(float);
441  }
442 
443 finish:
444  s->out[0] = s->out[1] = NULL;
445  s->out_size = 0;
446 
447  return output_samples;
448 }
449 
450 static int opus_decode_packet(AVCodecContext *avctx, void *data,
451  int *got_frame_ptr, AVPacket *avpkt)
452 {
453  OpusContext *c = avctx->priv_data;
454  AVFrame *frame = data;
455  const uint8_t *buf = avpkt->data;
456  int buf_size = avpkt->size;
457  int coded_samples = 0;
458  int decoded_samples = INT_MAX;
459  int delayed_samples = 0;
460  int i, ret;
461 
462  /* calculate the number of delayed samples */
463  for (i = 0; i < c->nb_streams; i++) {
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  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
492  return ret;
493  }
494  frame->nb_samples = 0;
495 
496  memset(c->out, 0, c->nb_streams * 2 * sizeof(*c->out));
497  for (i = 0; i < avctx->channels; i++) {
498  ChannelMap *map = &c->channel_maps[i];
499  if (!map->copy)
500  c->out[2 * map->stream_idx + map->channel_idx] = (float*)frame->extended_data[i];
501  }
502 
503  /* read the data from the sync buffers */
504  for (i = 0; i < c->nb_streams; i++) {
505  float **out = c->out + 2 * i;
506  int sync_size = av_audio_fifo_size(c->sync_buffers[i]);
507 
508  float sync_dummy[32];
509  int out_dummy = (!out[0]) | ((!out[1]) << 1);
510 
511  if (!out[0])
512  out[0] = sync_dummy;
513  if (!out[1])
514  out[1] = sync_dummy;
515  if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
516  return AVERROR_BUG;
517 
518  ret = av_audio_fifo_read(c->sync_buffers[i], (void**)out, sync_size);
519  if (ret < 0)
520  return ret;
521 
522  if (out_dummy & 1)
523  out[0] = NULL;
524  else
525  out[0] += ret;
526  if (out_dummy & 2)
527  out[1] = NULL;
528  else
529  out[1] += ret;
530 
531  c->out_size[i] = frame->linesize[0] - ret * sizeof(float);
532  }
533 
534  /* decode each sub-packet */
535  for (i = 0; i < c->nb_streams; i++) {
536  OpusStreamContext *s = &c->streams[i];
537 
538  if (i && buf) {
539  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
540  if (ret < 0) {
541  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
542  return ret;
543  }
544  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
545  av_log(avctx, AV_LOG_ERROR,
546  "Mismatching coded sample count in substream %d.\n", i);
547  return AVERROR_INVALIDDATA;
548  }
549 
551  }
552 
553  ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
554  c->out + 2 * i, c->out_size[i], coded_samples);
555  if (ret < 0)
556  return ret;
557  c->decoded_samples[i] = ret;
558  decoded_samples = FFMIN(decoded_samples, ret);
559 
560  buf += s->packet.packet_size;
561  buf_size -= s->packet.packet_size;
562  }
563 
564  /* buffer the extra samples */
565  for (i = 0; i < c->nb_streams; i++) {
566  int buffer_samples = c->decoded_samples[i] - decoded_samples;
567  if (buffer_samples) {
568  float *buf[2] = { c->out[2 * i + 0] ? c->out[2 * i + 0] : (float*)frame->extended_data[0],
569  c->out[2 * i + 1] ? c->out[2 * i + 1] : (float*)frame->extended_data[0] };
570  buf[0] += decoded_samples;
571  buf[1] += decoded_samples;
572  ret = av_audio_fifo_write(c->sync_buffers[i], (void**)buf, buffer_samples);
573  if (ret < 0)
574  return ret;
575  }
576  }
577 
578  for (i = 0; i < avctx->channels; i++) {
579  ChannelMap *map = &c->channel_maps[i];
580 
581  /* handle copied channels */
582  if (map->copy) {
583  memcpy(frame->extended_data[i],
584  frame->extended_data[map->copy_idx],
585  frame->linesize[0]);
586  } else if (map->silence) {
587  memset(frame->extended_data[i], 0, frame->linesize[0]);
588  }
589 
590  if (c->gain_i) {
591  c->fdsp.vector_fmul_scalar((float*)frame->extended_data[i],
592  (float*)frame->extended_data[i],
593  c->gain, FFALIGN(decoded_samples, 8));
594  }
595  }
596 
597  frame->nb_samples = decoded_samples;
598  *got_frame_ptr = !!decoded_samples;
599 
600  return avpkt->size;
601 }
602 
604 {
605  OpusContext *c = ctx->priv_data;
606  int i;
607 
608  for (i = 0; i < c->nb_streams; i++) {
609  OpusStreamContext *s = &c->streams[i];
610 
611  memset(&s->packet, 0, sizeof(s->packet));
612  s->delayed_samples = 0;
613 
614  if (s->celt_delay)
616  avresample_close(s->avr);
617 
619 
620  ff_silk_flush(s->silk);
621  ff_celt_flush(s->celt);
622  }
623 }
624 
626 {
627  OpusContext *c = avctx->priv_data;
628  int i;
629 
630  for (i = 0; i < c->nb_streams; i++) {
631  OpusStreamContext *s = &c->streams[i];
632 
633  ff_silk_free(&s->silk);
634  ff_celt_free(&s->celt);
635 
636  av_freep(&s->out_dummy);
638 
640  avresample_free(&s->avr);
641  }
642 
643  av_freep(&c->streams);
644 
645  if (c->sync_buffers) {
646  for (i = 0; i < c->nb_streams; i++)
648  }
649  av_freep(&c->sync_buffers);
651  av_freep(&c->out);
652  av_freep(&c->out_size);
653 
654  c->nb_streams = 0;
655 
656  av_freep(&c->channel_maps);
657 
658  return 0;
659 }
660 
662 {
663  OpusContext *c = avctx->priv_data;
664  int ret, i, j;
665 
667  avctx->sample_rate = 48000;
668 
669  avpriv_float_dsp_init(&c->fdsp, 0);
670 
671  /* find out the channel configuration */
672  ret = ff_opus_parse_extradata(avctx, c);
673  if (ret < 0)
674  return ret;
675 
676  /* allocate and init each independent decoder */
677  c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
678  c->out = av_mallocz_array(c->nb_streams, 2 * sizeof(*c->out));
679  c->out_size = av_mallocz_array(c->nb_streams, sizeof(*c->out_size));
682  if (!c->streams || !c->sync_buffers || !c->decoded_samples || !c->out || !c->out_size) {
683  c->nb_streams = 0;
684  ret = AVERROR(ENOMEM);
685  goto fail;
686  }
687 
688  for (i = 0; i < c->nb_streams; i++) {
689  OpusStreamContext *s = &c->streams[i];
690  uint64_t layout;
691 
692  s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
693 
694  s->avctx = avctx;
695 
696  for (j = 0; j < s->output_channels; j++) {
697  s->silk_output[j] = s->silk_buf[j];
698  s->celt_output[j] = s->celt_buf[j];
699  s->redundancy_output[j] = s->redundancy_buf[j];
700  }
701 
702  s->fdsp = &c->fdsp;
703 
705  if (!s->avr)
706  goto fail;
707 
709  av_opt_set_int(s->avr, "in_sample_fmt", avctx->sample_fmt, 0);
710  av_opt_set_int(s->avr, "out_sample_fmt", avctx->sample_fmt, 0);
711  av_opt_set_int(s->avr, "in_channel_layout", layout, 0);
712  av_opt_set_int(s->avr, "out_channel_layout", layout, 0);
713  av_opt_set_int(s->avr, "out_sample_rate", avctx->sample_rate, 0);
714 
715  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
716  if (ret < 0)
717  goto fail;
718 
719  ret = ff_celt_init(avctx, &s->celt, s->output_channels);
720  if (ret < 0)
721  goto fail;
722 
724  s->output_channels, 1024);
725  if (!s->celt_delay) {
726  ret = AVERROR(ENOMEM);
727  goto fail;
728  }
729 
731  s->output_channels, 32);
732  if (!c->sync_buffers[i]) {
733  ret = AVERROR(ENOMEM);
734  goto fail;
735  }
736  }
737 
738  return 0;
739 fail:
740  opus_decode_close(avctx);
741  return ret;
742 }
743 
745  .name = "opus",
746  .long_name = NULL_IF_CONFIG_SMALL("Opus"),
747  .type = AVMEDIA_TYPE_AUDIO,
748  .id = AV_CODEC_ID_OPUS,
749  .priv_data_size = sizeof(OpusContext),
754  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
755 };
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:67
static av_cold int opus_decode_close(AVCodecContext *avctx)
Definition: opusdec.c:625
AVAudioResampleContext * avr
Definition: opus.h:138
void ff_celt_flush(CeltContext *s)
Definition: opus_celt.c:2146
float, planar
Definition: samplefmt.h:72
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:60
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:139
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVAudioFifo ** sync_buffers
Definition: opus.h:173
static const uint16_t silk_frame_duration_ms[16]
Definition: opusdec.c:53
int frame_count
configuration: tells the audio mode, bandwidth, and frame duration
Definition: opus.h:105
int nb_stereo_streams
Definition: opus.h:178
float redundancy_buf[2][960]
Definition: opus.h:128
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void opus_raw_init(OpusRangeCoder *rc, const uint8_t *rightend, unsigned int bytes)
Definition: opusdec.c:95
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int output_channels
Definition: opus.h:115
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
int delayed_samples
Definition: opus.h:142
float gain
Definition: opus.h:182
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:180
int size
Definition: avcodec.h:974
RawBitsContext rb
Definition: opus.h:87
static av_always_inline unsigned int opus_rc_p2model(OpusRangeCoder *rc, unsigned int bits)
Definition: opus.h:225
#define FF_ARRAY_ELEMS(a)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
#define AV_CH_LAYOUT_STEREO
AVCodec.
Definition: avcodec.h:2812
int16_t gain_i
Definition: opus.h:181
void avresample_free(AVAudioResampleContext **avr)
Free AVAudioResampleContext and associated AVOption values.
Definition: utils.c:278
Macro definitions for various function/variable attributes.
#define FFALIGN(x, a)
Definition: common.h:62
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
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
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
unsigned int cacheval
Definition: opus.h:82
int * decoded_samples
Definition: opus.h:175
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
Definition: opusdec.c:113
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
unsigned int total_read_bits
Definition: opus.h:90
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
Definition: opusdec.c:207
int copy
Definition: opus.h:157
SilkContext * silk
Definition: opus.h:119
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
void ff_celt_free(CeltContext **s)
Definition: opus_celt.c:2173
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
static void opus_fade(float *out, const float *in1, const float *in2, const float *window, int len)
Definition: opusdec.c:104
float * silk_output[2]
Definition: opus.h:124
void avresample_close(AVAudioResampleContext *avr)
Close AVAudioResampleContext.
Definition: utils.c:262
const float ff_celt_window2[120]
Definition: opus_celt.c:466
static av_cold int opus_decode_init(AVCodecContext *avctx)
Definition: opusdec.c:661
AVFloatDSPContext * fdsp
Definition: opus.h:121
ChannelMap * channel_maps
Definition: opus.h:184
int nb_streams
Definition: opus.h:177
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:713
#define AVERROR(e)
Definition: error.h:43
unsigned int value
Definition: opus.h:89
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:268
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
#define FFMAX(a, b)
Definition: common.h:55
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:2187
audio channel layout utility functions
float * out[2]
Definition: opus.h:132
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2422
int frame_size[MAX_FRAMES]
frame offsets
Definition: opus.h:107
external API header
#define FFMIN(a, b)
Definition: common.h:57
int frame_duration
frame sizes
Definition: opus.h:108
float celt_buf[2][960]
Definition: opus.h:125
int out_dummy_allocated_size
Definition: opus.h:136
float silk_buf[2][960]
Definition: opus.h:123
#define AV_EF_EXPLODE
Definition: avcodec.h:2433
int silence
Definition: opus.h:162
int av_opt_get_int(void *obj, const char *name, int search_flags, int64_t *out_val)
Definition: opt.c:393
static int get_silk_samplerate(int config)
Definition: opusdec.c:69
NULL
Definition: eval.c:55
unsigned int bytes
Definition: opus.h:80
float * out_dummy
Definition: opus.h:135
unsigned int cachelen
Definition: opus.h:81
Libavcodec external API header.
OpusPacket packet
Definition: opus.h:144
int sample_rate
samples per second
Definition: avcodec.h:1807
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:408
AVCodec ff_opus_decoder
Definition: opusdec.c:744
void ff_silk_flush(SilkContext *s)
Definition: opus_silk.c:1567
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_silk_init(AVCodecContext *avctx, SilkContext **ps, int output_channels)
Definition: opus_silk.c:1575
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
GetBitContext gb
Definition: opus.h:86
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
int config
vbr flag
Definition: opus.h:103
void ff_silk_free(SilkContext **ps)
Definition: opus_silk.c:1562
enum OpusMode mode
frame duration, in samples @ 48kHz
Definition: opus.h:109
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
int copy_idx
Definition: opus.h:159
int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t **input, int in_plane_size, int in_samples)
Convert input samples and write them to the output FIFO.
Definition: utils.c:330
int stereo
packet code: specifies the frame layout
Definition: opus.h:101
AVCodecContext * avctx
Definition: opus.h:114
float ** out
Definition: opus.h:169
int data_size
packet size
Definition: opus.h:99
int channel_idx
Definition: opus.h:152
CeltContext * celt
Definition: opus.h:120
int redundancy_idx
Definition: opus.h:146
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
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:450
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:156
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:368
unsigned int range
Definition: opus.h:88
AVAudioResampleContext * avresample_alloc_context(void)
Allocate AVAudioResampleContext and set options.
Definition: options.c:96
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:388
static av_always_inline void opus_rc_normalize(OpusRangeCoder *rc)
Definition: opus.h:187
float * celt_output[2]
Definition: opus.h:126
common internal api header.
OpusRangeCoder rc
Definition: opus.h:117
int stream_idx
Definition: opus.h:151
AVFloatDSPContext fdsp
Definition: opus.h:180
int * out_size
Definition: opus.h:170
OpusBandwidth
Definition: opus.h:70
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1797
static const int silk_resample_delay[]
Definition: opusdec.c:63
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:294
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
int avresample_is_open(AVAudioResampleContext *avr)
Check whether an AVAudioResampleContext is open or closed.
Definition: utils.c:257
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
OpusStreamContext * streams
Definition: opus.h:166
int packet_size
Definition: opus.h:98
OpusRangeCoder redundancy_rc
Definition: opus.h:118
void * priv_data
Definition: avcodec.h:1092
Audio FIFO Buffer.
int len
int channels
number of audio channels
Definition: avcodec.h:1808
int frame_offset[MAX_FRAMES]
frame count
Definition: opus.h:106
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:246
enum OpusBandwidth bandwidth
mode
Definition: opus.h:110
static av_cold void opus_decode_flush(AVCodecContext *ctx)
Definition: opusdec.c:603
static int opus_rc_init(OpusRangeCoder *rc, const uint8_t *data, int size)
Range decoder.
Definition: opusdec.c:81
float * redundancy_output[2]
Definition: opus.h:129
uint64_t layout
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:205
AVAudioFifo * celt_delay
Definition: opus.h:139
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:289
int silk_samplerate
Definition: opus.h:140
const uint8_t * position
Definition: opus.h:79
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:950
int avresample_open(AVAudioResampleContext *avr)
Initialize AVAudioResampleContext.
Definition: utils.c:36
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179