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 "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 
78 /**
79  * Range decoder
80  */
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  ret = swr_convert(s->swr,
118  (uint8_t**)s->out, nb_samples,
119  NULL, 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  static const float delay[16] = { 0.0 };
159  const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
160  int ret;
161 
162  av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
163  ret = swr_init(s->swr);
164  if (ret < 0) {
165  av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
166  return ret;
167  }
168 
169  ret = swr_convert(s->swr,
170  NULL, 0,
171  delayptr, silk_resample_delay[s->packet.bandwidth]);
172  if (ret < 0) {
174  "Error feeding initial silence to the resampler.\n");
175  return ret;
176  }
177 
178  return 0;
179 }
180 
182 {
183  int ret;
184  enum OpusBandwidth bw = s->packet.bandwidth;
185 
186  if (s->packet.mode == OPUS_MODE_SILK &&
189 
190  ret = opus_rc_init(&s->redundancy_rc, data, size);
191  if (ret < 0)
192  goto fail;
193  opus_raw_init(&s->redundancy_rc, data + size, size);
194 
197  s->packet.stereo + 1, 240,
199  if (ret < 0)
200  goto fail;
201 
202  return 0;
203 fail:
204  av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
205  return ret;
206 }
207 
209 {
210  int samples = s->packet.frame_duration;
211  int redundancy = 0;
212  int redundancy_size, redundancy_pos;
213  int ret, i, consumed;
214  int delayed_samples = s->delayed_samples;
215 
216  ret = opus_rc_init(&s->rc, data, size);
217  if (ret < 0)
218  return ret;
219 
220  /* decode the silk frame */
221  if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
222  if (!swr_is_initialized(s->swr)) {
223  ret = opus_init_resample(s);
224  if (ret < 0)
225  return ret;
226  }
227 
228  samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
230  s->packet.stereo + 1,
232  if (samples < 0) {
233  av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
234  return samples;
235  }
236  samples = swr_convert(s->swr,
237  (uint8_t**)s->out, s->packet.frame_duration,
238  (const uint8_t**)s->silk_output, samples);
239  if (samples < 0) {
240  av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
241  return samples;
242  }
243  av_assert2((samples & 7) == 0);
244  s->delayed_samples += s->packet.frame_duration - samples;
245  } else
246  ff_silk_flush(s->silk);
247 
248  // decode redundancy information
249  consumed = opus_rc_tell(&s->rc);
250  if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
251  redundancy = opus_rc_p2model(&s->rc, 12);
252  else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
253  redundancy = 1;
254 
255  if (redundancy) {
256  redundancy_pos = opus_rc_p2model(&s->rc, 1);
257 
258  if (s->packet.mode == OPUS_MODE_HYBRID)
259  redundancy_size = opus_rc_unimodel(&s->rc, 256) + 2;
260  else
261  redundancy_size = size - (consumed + 7) / 8;
262  size -= redundancy_size;
263  if (size < 0) {
264  av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
265  return AVERROR_INVALIDDATA;
266  }
267 
268  if (redundancy_pos) {
269  ret = opus_decode_redundancy(s, data + size, redundancy_size);
270  if (ret < 0)
271  return ret;
272  ff_celt_flush(s->celt);
273  }
274  }
275 
276  /* decode the CELT frame */
277  if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
278  float *out_tmp[2] = { s->out[0], s->out[1] };
279  float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
280  out_tmp : s->celt_output;
281  int celt_output_samples = samples;
282  int delay_samples = av_audio_fifo_size(s->celt_delay);
283 
284  if (delay_samples) {
285  if (s->packet.mode == OPUS_MODE_HYBRID) {
286  av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
287 
288  for (i = 0; i < s->output_channels; i++) {
289  s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
290  delay_samples);
291  out_tmp[i] += delay_samples;
292  }
293  celt_output_samples -= delay_samples;
294  } else {
296  "Spurious CELT delay samples present.\n");
297  av_audio_fifo_drain(s->celt_delay, delay_samples);
299  return AVERROR_BUG;
300  }
301  }
302 
303  opus_raw_init(&s->rc, data + size, size);
304 
305  ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
306  s->packet.stereo + 1,
308  (s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
310  if (ret < 0)
311  return ret;
312 
313  if (s->packet.mode == OPUS_MODE_HYBRID) {
314  int celt_delay = s->packet.frame_duration - celt_output_samples;
315  void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
316  s->celt_output[1] + celt_output_samples };
317 
318  for (i = 0; i < s->output_channels; i++) {
319  s->fdsp->vector_fmac_scalar(out_tmp[i],
320  s->celt_output[i], 1.0,
321  celt_output_samples);
322  }
323 
324  ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
325  if (ret < 0)
326  return ret;
327  }
328  } else
329  ff_celt_flush(s->celt);
330 
331  if (s->redundancy_idx) {
332  for (i = 0; i < s->output_channels; i++)
333  opus_fade(s->out[i], s->out[i],
334  s->redundancy_output[i] + 120 + s->redundancy_idx,
336  s->redundancy_idx = 0;
337  }
338  if (redundancy) {
339  if (!redundancy_pos) {
340  ff_celt_flush(s->celt);
341  ret = opus_decode_redundancy(s, data + size, redundancy_size);
342  if (ret < 0)
343  return ret;
344 
345  for (i = 0; i < s->output_channels; i++) {
346  opus_fade(s->out[i] + samples - 120 + delayed_samples,
347  s->out[i] + samples - 120 + delayed_samples,
348  s->redundancy_output[i] + 120,
349  ff_celt_window2, 120 - delayed_samples);
350  if (delayed_samples)
351  s->redundancy_idx = 120 - delayed_samples;
352  }
353  } else {
354  for (i = 0; i < s->output_channels; i++) {
355  memcpy(s->out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
356  opus_fade(s->out[i] + 120 + delayed_samples,
357  s->redundancy_output[i] + 120,
358  s->out[i] + 120 + delayed_samples,
359  ff_celt_window2, 120);
360  }
361  }
362  }
363 
364  return samples;
365 }
366 
368  const uint8_t *buf, int buf_size,
369  int nb_samples)
370 {
371  int output_samples = 0;
372  int flush_needed = 0;
373  int i, j, ret;
374 
375  /* check if we need to flush the resampler */
376  if (swr_is_initialized(s->swr)) {
377  if (buf) {
378  int64_t cur_samplerate;
379  av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
380  flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
381  } else {
382  flush_needed = !!s->delayed_samples;
383  }
384  }
385 
386  if (!buf && !flush_needed)
387  return 0;
388 
389  /* use dummy output buffers if the channel is not mapped to anything */
390  if (!s->out[0] ||
391  (s->output_channels == 2 && !s->out[1])) {
393  if (!s->out_dummy)
394  return AVERROR(ENOMEM);
395  if (!s->out[0])
396  s->out[0] = s->out_dummy;
397  if (!s->out[1])
398  s->out[1] = s->out_dummy;
399  }
400 
401  /* flush the resampler if necessary */
402  if (flush_needed) {
404  if (ret < 0) {
405  av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
406  return ret;
407  }
408  swr_close(s->swr);
409  output_samples += s->delayed_samples;
410  s->delayed_samples = 0;
411 
412  if (!buf)
413  goto finish;
414  }
415 
416  /* decode all the frames in the packet */
417  for (i = 0; i < s->packet.frame_count; i++) {
418  int size = s->packet.frame_size[i];
419  int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
420 
421  if (samples < 0) {
422  av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
424  return samples;
425 
426  for (j = 0; j < s->output_channels; j++)
427  memset(s->out[j], 0, s->packet.frame_duration * sizeof(float));
428  samples = s->packet.frame_duration;
429  }
430  output_samples += samples;
431 
432  for (j = 0; j < s->output_channels; j++)
433  s->out[j] += samples;
434  s->out_size -= samples * sizeof(float);
435  }
436 
437 finish:
438  s->out[0] = s->out[1] = NULL;
439  s->out_size = 0;
440 
441  return output_samples;
442 }
443 
444 static int opus_decode_packet(AVCodecContext *avctx, void *data,
445  int *got_frame_ptr, AVPacket *avpkt)
446 {
447  OpusContext *c = avctx->priv_data;
448  AVFrame *frame = data;
449  const uint8_t *buf = avpkt->data;
450  int buf_size = avpkt->size;
451  int coded_samples = 0;
452  int decoded_samples = 0;
453  int i, ret;
454 
455  /* decode the header of the first sub-packet to find out the sample count */
456  if (buf) {
457  OpusPacket *pkt = &c->streams[0].packet;
458  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
459  if (ret < 0) {
460  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
461  return ret;
462  }
463  coded_samples += pkt->frame_count * pkt->frame_duration;
465  }
466 
467  frame->nb_samples = coded_samples + c->streams[0].delayed_samples;
468 
469  /* no input or buffered data => nothing to do */
470  if (!frame->nb_samples) {
471  *got_frame_ptr = 0;
472  return 0;
473  }
474 
475  /* setup the data buffers */
476  ret = ff_get_buffer(avctx, frame, 0);
477  if (ret < 0) {
478  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
479  return ret;
480  }
481  frame->nb_samples = 0;
482 
483  for (i = 0; i < avctx->channels; i++) {
484  ChannelMap *map = &c->channel_maps[i];
485  if (!map->copy)
486  c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
487  }
488 
489  for (i = 0; i < c->nb_streams; i++)
490  c->streams[i].out_size = frame->linesize[0];
491 
492  /* decode each sub-packet */
493  for (i = 0; i < c->nb_streams; i++) {
494  OpusStreamContext *s = &c->streams[i];
495 
496  if (i && buf) {
497  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
498  if (ret < 0) {
499  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
500  return ret;
501  }
502  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
503  av_log(avctx, AV_LOG_ERROR,
504  "Mismatching coded sample count in substream %d.\n", i);
505  return AVERROR_INVALIDDATA;
506  }
507 
509  }
510 
511  ret = opus_decode_subpacket(&c->streams[i], buf,
512  s->packet.data_size, coded_samples);
513  if (ret < 0)
514  return ret;
515  if (decoded_samples && ret != decoded_samples) {
516  av_log(avctx, AV_LOG_ERROR, "Different numbers of decoded samples "
517  "in a multi-channel stream\n");
518  return AVERROR_INVALIDDATA;
519  }
520  decoded_samples = ret;
521  buf += s->packet.packet_size;
522  buf_size -= s->packet.packet_size;
523  }
524 
525  for (i = 0; i < avctx->channels; i++) {
526  ChannelMap *map = &c->channel_maps[i];
527 
528  /* handle copied channels */
529  if (map->copy) {
530  memcpy(frame->extended_data[i],
531  frame->extended_data[map->copy_idx],
532  frame->linesize[0]);
533  } else if (map->silence) {
534  memset(frame->extended_data[i], 0, frame->linesize[0]);
535  }
536 
537  if (c->gain_i) {
538  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
539  (float*)frame->extended_data[i],
540  c->gain, FFALIGN(decoded_samples, 8));
541  }
542  }
543 
544  frame->nb_samples = decoded_samples;
545  *got_frame_ptr = !!decoded_samples;
546 
547  return avpkt->size;
548 }
549 
551 {
552  OpusContext *c = ctx->priv_data;
553  int i;
554 
555  for (i = 0; i < c->nb_streams; i++) {
556  OpusStreamContext *s = &c->streams[i];
557 
558  memset(&s->packet, 0, sizeof(s->packet));
559  s->delayed_samples = 0;
560 
561  if (s->celt_delay)
563  swr_close(s->swr);
564 
565  ff_silk_flush(s->silk);
566  ff_celt_flush(s->celt);
567  }
568 }
569 
571 {
572  OpusContext *c = avctx->priv_data;
573  int i;
574 
575  for (i = 0; i < c->nb_streams; i++) {
576  OpusStreamContext *s = &c->streams[i];
577 
578  ff_silk_free(&s->silk);
579  ff_celt_free(&s->celt);
580 
581  av_freep(&s->out_dummy);
583 
585  swr_free(&s->swr);
586  }
587 
588  av_freep(&c->streams);
589  c->nb_streams = 0;
590 
591  av_freep(&c->channel_maps);
592  av_freep(&c->fdsp);
593 
594  return 0;
595 }
596 
598 {
599  OpusContext *c = avctx->priv_data;
600  int ret, i, j;
601 
603  avctx->sample_rate = 48000;
604 
606  if (!c->fdsp)
607  return AVERROR(ENOMEM);
608 
609  /* find out the channel configuration */
610  ret = ff_opus_parse_extradata(avctx, c);
611  if (ret < 0)
612  return ret;
613 
614  /* allocate and init each independent decoder */
615  c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
616  if (!c->streams) {
617  c->nb_streams = 0;
618  ret = AVERROR(ENOMEM);
619  goto fail;
620  }
621 
622  for (i = 0; i < c->nb_streams; i++) {
623  OpusStreamContext *s = &c->streams[i];
624  uint64_t layout;
625 
626  s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
627 
628  s->avctx = avctx;
629 
630  for (j = 0; j < s->output_channels; j++) {
631  s->silk_output[j] = s->silk_buf[j];
632  s->celt_output[j] = s->celt_buf[j];
633  s->redundancy_output[j] = s->redundancy_buf[j];
634  }
635 
636  s->fdsp = c->fdsp;
637 
638  s->swr =swr_alloc();
639  if (!s->swr)
640  goto fail;
641 
643  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
644  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
645  av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
646  av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
647  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
648  av_opt_set_int(s->swr, "filter_size", 16, 0);
649 
650  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
651  if (ret < 0)
652  goto fail;
653 
654  ret = ff_celt_init(avctx, &s->celt, s->output_channels);
655  if (ret < 0)
656  goto fail;
657 
659  s->output_channels, 1024);
660  if (!s->celt_delay) {
661  ret = AVERROR(ENOMEM);
662  goto fail;
663  }
664  }
665 
666  return 0;
667 fail:
668  opus_decode_close(avctx);
669  return ret;
670 }
671 
673  .name = "opus",
674  .long_name = NULL_IF_CONFIG_SMALL("Opus"),
675  .type = AVMEDIA_TYPE_AUDIO,
676  .id = AV_CODEC_ID_OPUS,
677  .priv_data_size = sizeof(OpusContext),
682  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
683 };