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
47
49
59
61 10, 20, 40, 60,
62 10, 20, 40, 60,
63 10, 20, 40, 60,
64 10, 20,
65 10, 20,
66 };
67
68 /* number of samples of silence to feed to the resampler
69 * at the beginning */
71 4, 8, 11, 11, 11
72 };
73
77
78 /* number of decoded samples for this stream */
80 /* current output buffers for this stream */
83 /* Buffer with samples from this stream for synchronizing
84 * the streams when they have different resampling delays */
86
92
97
100
101 /* buffers for the next samples to be decoded */
104
107
111 /* number of samples we still want to get from the resampler */
113
115
118
121
124
127
130
132 {
134 return 8000;
136 return 12000;
137 return 16000;
138 }
139
141 const float *in1, const float *in2,
143 {
147 }
148
150 {
154 (uint8_t**)
s->cur_out, nb_samples,
158 else if (
ret != nb_samples) {
162 }
163
164 if (celt_size) {
165 if (celt_size != nb_samples) {
168 }
170 for (
i = 0;
i <
s->output_channels;
i++) {
171 s->fdsp->vector_fmac_scalar(
s->cur_out[
i],
172 s->celt_output[
i], 1.0,
173 nb_samples);
174 }
175 }
176
177 if (
s->redundancy_idx) {
178 for (
i = 0;
i <
s->output_channels;
i++)
180 s->redundancy_output[
i] + 120 +
s->redundancy_idx,
182 s->redundancy_idx = 0;
183 }
184
185 s->cur_out[0] += nb_samples;
186 s->cur_out[1] += nb_samples;
187 s->remaining_out_size -= nb_samples *
sizeof(
float);
188
189 return 0;
190 }
191
193 {
194 static const float delay[16] = { 0.0 };
195 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
197
203 }
204
210 "Error feeding initial silence to the resampler.\n");
212 }
213
214 return 0;
215 }
216
218 {
223
225 s->redundancy_output,
226 s->packet.stereo + 1, 240,
230
231 return 0;
235 }
236
238 {
239 int samples =
s->packet.frame_duration;
240 int redundancy = 0;
241 int redundancy_size, redundancy_pos;
242 int ret,
i, consumed;
244
248
249 /* decode the silk frame */
255 }
256
259 s->packet.stereo + 1,
264 }
266 (uint8_t**)
s->cur_out,
s->packet.frame_duration,
267 (
const uint8_t**)
s->silk_output,
samples);
271 }
273 s->delayed_samples +=
s->packet.frame_duration -
samples;
274 } else
276
277 // decode redundancy information
282 redundancy = 1;
283
284 if (redundancy) {
286
289 else
290 redundancy_size =
size - (consumed + 7) / 8;
291 size -= redundancy_size;
295 }
296
297 if (redundancy_pos) {
302 }
303 }
304
305 /* decode the CELT frame */
307 float *out_tmp[2] = {
s->cur_out[0],
s->cur_out[1] };
309 out_tmp :
s->celt_output;
310 int celt_output_samples =
samples;
312
313 if (delay_samples) {
316
317 for (
i = 0;
i <
s->output_channels;
i++) {
318 s->fdsp->vector_fmac_scalar(out_tmp[
i],
s->celt_output[
i], 1.0,
319 delay_samples);
320 out_tmp[
i] += delay_samples;
321 }
322 celt_output_samples -= delay_samples;
323 } else {
325 "Spurious CELT delay samples present.\n");
329 }
330 }
331
333
335 s->packet.stereo + 1,
336 s->packet.frame_duration,
341
343 int celt_delay =
s->packet.frame_duration - celt_output_samples;
344 void *delaybuf[2] = {
s->celt_output[0] + celt_output_samples,
345 s->celt_output[1] + celt_output_samples };
346
347 for (
i = 0;
i <
s->output_channels;
i++) {
348 s->fdsp->vector_fmac_scalar(out_tmp[
i],
349 s->celt_output[
i], 1.0,
350 celt_output_samples);
351 }
352
356 }
357 } else
359
360 if (
s->redundancy_idx) {
361 for (
i = 0;
i <
s->output_channels;
i++)
363 s->redundancy_output[
i] + 120 +
s->redundancy_idx,
365 s->redundancy_idx = 0;
366 }
367 if (redundancy) {
368 if (!redundancy_pos) {
373
374 for (
i = 0;
i <
s->output_channels;
i++) {
377 s->redundancy_output[
i] + 120,
381 }
382 } else {
383 for (
i = 0;
i <
s->output_channels;
i++) {
386 s->redundancy_output[
i] + 120,
389 }
390 }
391 }
392
394 }
395
397 const uint8_t *buf, int buf_size,
398 int nb_samples)
399 {
400 int output_samples = 0;
401 int flush_needed = 0;
403
404 s->cur_out[0] =
s->out[0];
405 s->cur_out[1] =
s->out[1];
406 s->remaining_out_size =
s->out_size;
407
408 /* check if we need to flush the resampler */
410 if (buf) {
413 flush_needed = (
s->packet.mode ==
OPUS_MODE_CELT) || (cur_samplerate !=
s->silk_samplerate);
414 } else {
415 flush_needed = !!
s->delayed_samples;
416 }
417 }
418
419 if (!buf && !flush_needed)
420 return 0;
421
422 /* use dummy output buffers if the channel is not mapped to anything */
423 if (!
s->cur_out[0] ||
424 (
s->output_channels == 2 && !
s->cur_out[1])) {
426 s->remaining_out_size);
430 s->cur_out[0] =
s->out_dummy;
432 s->cur_out[1] =
s->out_dummy;
433 }
434
435 /* flush the resampler if necessary */
436 if (flush_needed) {
441 }
443 output_samples +=
s->delayed_samples;
444 s->delayed_samples = 0;
445
446 if (!buf)
448 }
449
450 /* decode all the frames in the packet */
451 for (
i = 0;
i <
s->packet.frame_count;
i++) {
452 int size =
s->packet.frame_size[
i];
454
459
460 for (j = 0; j <
s->output_channels; j++)
461 memset(
s->cur_out[j], 0,
s->packet.frame_duration *
sizeof(
float));
463 }
465
466 for (j = 0; j <
s->output_channels; j++)
468 s->remaining_out_size -=
samples *
sizeof(
float);
469 }
470
472 s->cur_out[0] =
s->cur_out[1] =
NULL;
473 s->remaining_out_size = 0;
474
475 return output_samples;
476 }
477
479 int *got_frame_ptr,
AVPacket *avpkt)
480 {
482 const uint8_t *buf = avpkt->
data;
483 int buf_size = avpkt->
size;
484 int coded_samples = 0;
488
489 /* calculate the number of delayed samples */
490 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
496 }
497
498 /* decode the header of the first sub-packet to find out the sample count */
499 if (buf) {
505 }
506 coded_samples +=
pkt->frame_count *
pkt->frame_duration;
508 }
509
511
512 /* no input or buffered data => nothing to do */
513 if (!
frame->nb_samples) {
514 *got_frame_ptr = 0;
515 return 0;
516 }
517
518 /* setup the data buffers */
522 frame->nb_samples = 0;
523
527 c->streams[
map->stream_idx].out[
map->channel_idx] = (
float*)
frame->extended_data[
i];
528 }
529
530 /* read the data from the sync buffers */
531 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
533 float **
out =
s->out;
535
536 float sync_dummy[32];
538
545
549
552 else
556 else
558
560 }
561
562 /* decode each sub-packet */
563 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
565
571 }
572 if (coded_samples !=
s->packet.frame_count *
s->packet.frame_duration) {
574 "Mismatching coded sample count in substream %d.\n",
i);
576 }
577
579 }
580
582 coded_samples);
585 s->decoded_samples =
ret;
587
588 buf +=
s->packet.packet_size;
589 buf_size -=
s->packet.packet_size;
590 }
591
592 /* buffer the extra samples */
593 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
596 if (buffer_samples) {
597 float *buf[2] = {
s->out[0] ?
s->out[0] : (
float*)
frame->extended_data[0],
598 s->out[1] ?
s->out[1] : (
float*)
frame->extended_data[0] };
604 }
605 }
606
609
610 /* handle copied channels */
612 memcpy(
frame->extended_data[
i],
613 frame->extended_data[
map->copy_idx],
615 }
else if (
map->silence) {
616 memset(
frame->extended_data[
i], 0,
frame->linesize[0]);
617 }
618
620 c->fdsp->vector_fmul_scalar((
float*)
frame->extended_data[
i],
621 (
float*)
frame->extended_data[
i],
623 }
624 }
625
628
630 }
631
633 {
635
636 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
638
639 memset(&
s->packet, 0,
sizeof(
s->packet));
640 s->delayed_samples = 0;
641
644
646
649 }
650 }
651
653 {
655
656 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
658
661
663 s->out_dummy_allocated_size = 0;
664
668 }
669
671
673
676
677 return 0;
678 }
679
681 {
684
687
691
692 /* find out the channel configuration */
697 c->gain =
ff_exp10(
c->p.gain_i / (20.0 * 256));
698
699 /* allocate and init each independent decoder */
700 c->streams =
av_calloc(
c->p.nb_streams,
sizeof(*
c->streams));
704 }
705
706 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
709
710 s->output_channels = (
i <
c->p.nb_stereo_streams) ? 2 : 1;
711
713
714 for (
int j = 0; j <
s->output_channels; j++) {
715 s->silk_output[j] =
s->silk_buf[j];
716 s->celt_output[j] =
s->celt_buf[j];
717 s->redundancy_output[j] =
s->redundancy_buf[j];
718 }
719
721
725
734
738
742
744 s->output_channels, 1024);
747
749 s->output_channels, 32);
752 }
753
754 return 0;
755 }
756
757 #define OFFSET(x) offsetof(OpusContext, x)
758 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
760 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
AD },
762 };
763
769 };
770
784 };