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 
453  /* decode the header of the first sub-packet to find out the sample count */
454  if (buf) {
455  OpusPacket *pkt = &c->streams[0].packet;
456  ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
457  if (ret < 0) {
458  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
459  return ret;
460  }
461  coded_samples += pkt->frame_count * pkt->frame_duration;
463  }
464 
465  frame->nb_samples = coded_samples + c->streams[0].delayed_samples;
466 
467  /* no input or buffered data => nothing to do */
468  if (!frame->nb_samples) {
469  *got_frame_ptr = 0;
470  return 0;
471  }
472 
473  /* setup the data buffers */
474  ret = ff_get_buffer(avctx, frame, 0);
475  if (ret < 0)
476  return ret;
477  frame->nb_samples = 0;
478 
479  for (i = 0; i < avctx->channels; i++) {
480  ChannelMap *map = &c->channel_maps[i];
481  if (!map->copy)
482  c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
483  }
484 
485  for (i = 0; i < c->nb_streams; i++)
486  c->streams[i].out_size = frame->linesize[0];
487 
488  /* decode each sub-packet */
489  for (i = 0; i < c->nb_streams; i++) {
490  OpusStreamContext *s = &c->streams[i];
491 
492  if (i && buf) {
493  ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
494  if (ret < 0) {
495  av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
496  return ret;
497  }
498  if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
499  av_log(avctx, AV_LOG_ERROR,
500  "Mismatching coded sample count in substream %d.\n", i);
501  return AVERROR_INVALIDDATA;
502  }
503 
505  }
506 
507  ret = opus_decode_subpacket(&c->streams[i], buf,
508  s->packet.data_size, coded_samples);
509  if (ret < 0)
510  return ret;
511  if (decoded_samples && ret != decoded_samples) {
512  av_log(avctx, AV_LOG_ERROR, "Different numbers of decoded samples "
513  "in a multi-channel stream\n");
514  return AVERROR_INVALIDDATA;
515  }
516  decoded_samples = ret;
517  buf += s->packet.packet_size;
518  buf_size -= s->packet.packet_size;
519  }
520 
521  for (i = 0; i < avctx->channels; i++) {
522  ChannelMap *map = &c->channel_maps[i];
523 
524  /* handle copied channels */
525  if (map->copy) {
526  memcpy(frame->extended_data[i],
527  frame->extended_data[map->copy_idx],
528  frame->linesize[0]);
529  } else if (map->silence) {
530  memset(frame->extended_data[i], 0, frame->linesize[0]);
531  }
532 
533  if (c->gain_i) {
534  c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
535  (float*)frame->extended_data[i],
536  c->gain, FFALIGN(decoded_samples, 8));
537  }
538  }
539 
540  frame->nb_samples = decoded_samples;
541  *got_frame_ptr = !!decoded_samples;
542 
543  return avpkt->size;
544 }
545 
547 {
548  OpusContext *c = ctx->priv_data;
549  int i;
550 
551  for (i = 0; i < c->nb_streams; i++) {
552  OpusStreamContext *s = &c->streams[i];
553 
554  memset(&s->packet, 0, sizeof(s->packet));
555  s->delayed_samples = 0;
556 
557  if (s->celt_delay)
559  swr_close(s->swr);
560 
561  ff_silk_flush(s->silk);
562  ff_celt_flush(s->celt);
563  }
564 }
565 
567 {
568  OpusContext *c = avctx->priv_data;
569  int i;
570 
571  for (i = 0; i < c->nb_streams; i++) {
572  OpusStreamContext *s = &c->streams[i];
573 
574  ff_silk_free(&s->silk);
575  ff_celt_free(&s->celt);
576 
577  av_freep(&s->out_dummy);
579 
581  swr_free(&s->swr);
582  }
583 
584  av_freep(&c->streams);
585  c->nb_streams = 0;
586 
587  av_freep(&c->channel_maps);
588  av_freep(&c->fdsp);
589 
590  return 0;
591 }
592 
594 {
595  OpusContext *c = avctx->priv_data;
596  int ret, i, j;
597 
599  avctx->sample_rate = 48000;
600 
602  if (!c->fdsp)
603  return AVERROR(ENOMEM);
604 
605  /* find out the channel configuration */
606  ret = ff_opus_parse_extradata(avctx, c);
607  if (ret < 0)
608  return ret;
609 
610  /* allocate and init each independent decoder */
611  c->streams = av_mallocz_array(c->nb_streams, sizeof(*c->streams));
612  if (!c->streams) {
613  c->nb_streams = 0;
614  ret = AVERROR(ENOMEM);
615  goto fail;
616  }
617 
618  for (i = 0; i < c->nb_streams; i++) {
619  OpusStreamContext *s = &c->streams[i];
620  uint64_t layout;
621 
622  s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
623 
624  s->avctx = avctx;
625 
626  for (j = 0; j < s->output_channels; j++) {
627  s->silk_output[j] = s->silk_buf[j];
628  s->celt_output[j] = s->celt_buf[j];
629  s->redundancy_output[j] = s->redundancy_buf[j];
630  }
631 
632  s->fdsp = c->fdsp;
633 
634  s->swr =swr_alloc();
635  if (!s->swr)
636  goto fail;
637 
639  av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
640  av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
641  av_opt_set_int(s->swr, "in_channel_layout", layout, 0);
642  av_opt_set_int(s->swr, "out_channel_layout", layout, 0);
643  av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
644  av_opt_set_int(s->swr, "filter_size", 16, 0);
645 
646  ret = ff_silk_init(avctx, &s->silk, s->output_channels);
647  if (ret < 0)
648  goto fail;
649 
650  ret = ff_celt_init(avctx, &s->celt, s->output_channels);
651  if (ret < 0)
652  goto fail;
653 
655  s->output_channels, 1024);
656  if (!s->celt_delay) {
657  ret = AVERROR(ENOMEM);
658  goto fail;
659  }
660  }
661 
662  return 0;
663 fail:
664  opus_decode_close(avctx);
665  return ret;
666 }
667 
669  .name = "opus",
670  .long_name = NULL_IF_CONFIG_SMALL("Opus"),
671  .type = AVMEDIA_TYPE_AUDIO,
672  .id = AV_CODEC_ID_OPUS,
673  .priv_data_size = sizeof(OpusContext),
675  .close = opus_decode_close,
678  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
679 };