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 {
398 int output_samples = 0;
399 int flush_needed = 0;
401
402 s->cur_out[0] =
s->out[0];
403 s->cur_out[1] =
s->out[1];
404 s->remaining_out_size =
s->out_size;
405
406 /* check if we need to flush the resampler */
408 if (buf) {
411 flush_needed = (
s->packet.mode ==
OPUS_MODE_CELT) || (cur_samplerate !=
s->silk_samplerate);
412 } else {
413 flush_needed = !!
s->delayed_samples;
414 }
415 }
416
417 if (!buf && !flush_needed)
418 return 0;
419
420 /* use dummy output buffers if the channel is not mapped to anything */
421 if (!
s->cur_out[0] ||
422 (
s->output_channels == 2 && !
s->cur_out[1])) {
424 s->remaining_out_size);
428 s->cur_out[0] =
s->out_dummy;
430 s->cur_out[1] =
s->out_dummy;
431 }
432
433 /* flush the resampler if necessary */
434 if (flush_needed) {
439 }
441 output_samples +=
s->delayed_samples;
442 s->delayed_samples = 0;
443
444 if (!buf)
446 }
447
448 /* decode all the frames in the packet */
449 for (
i = 0;
i <
s->packet.frame_count;
i++) {
450 int size =
s->packet.frame_size[
i];
452
457
458 for (j = 0; j <
s->output_channels; j++)
459 memset(
s->cur_out[j], 0,
s->packet.frame_duration *
sizeof(
float));
461 }
463
464 for (j = 0; j <
s->output_channels; j++)
466 s->remaining_out_size -=
samples *
sizeof(
float);
467 }
468
470 s->cur_out[0] =
s->cur_out[1] =
NULL;
471 s->remaining_out_size = 0;
472
473 return output_samples;
474 }
475
477 int *got_frame_ptr,
AVPacket *avpkt)
478 {
480 const uint8_t *buf = avpkt->
data;
481 int buf_size = avpkt->
size;
482 int coded_samples = 0;
486
487 /* calculate the number of delayed samples */
488 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
494 s->delayed_samples + fifo_samples);
495 }
496
497 /* decode the header of the first sub-packet to find out the sample count */
498 if (buf) {
504 }
505 coded_samples +=
pkt->frame_count *
pkt->frame_duration;
507 }
508
510
511 /* no input or buffered data => nothing to do */
512 if (!
frame->nb_samples) {
513 *got_frame_ptr = 0;
514 return 0;
515 }
516
517 /* setup the data buffers */
521 frame->nb_samples = 0;
522
526 c->streams[
map->stream_idx].out[
map->channel_idx] = (
float*)
frame->extended_data[
i];
527 }
528
529 /* read the data from the sync buffers */
530 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
532 float **
out =
s->out;
534
535 float sync_dummy[32];
537
544
548
551 else
555 else
557
559 }
560
561 /* decode each sub-packet */
562 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
564
570 }
571 if (coded_samples !=
s->packet.frame_count *
s->packet.frame_duration) {
573 "Mismatching coded sample count in substream %d.\n",
i);
575 }
576
578 }
579
583 s->decoded_samples =
ret;
585
586 if (!buf)
587 continue;
588
589 buf +=
s->packet.packet_size;
590 buf_size -=
s->packet.packet_size;
591 }
592
593 /* buffer the extra samples */
594 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
597 if (buffer_samples) {
598 float *buf[2] = {
s->out[0] ?
s->out[0] : (
float*)
frame->extended_data[0],
599 s->out[1] ?
s->out[1] : (
float*)
frame->extended_data[0] };
605 }
606 }
607
610
611 /* handle copied channels */
613 memcpy(
frame->extended_data[
i],
614 frame->extended_data[
map->copy_idx],
616 }
else if (
map->silence) {
617 memset(
frame->extended_data[
i], 0,
frame->linesize[0]);
618 }
619
621 c->fdsp->vector_fmul_scalar((
float*)
frame->extended_data[
i],
622 (
float*)
frame->extended_data[
i],
624 }
625 }
626
629
631 }
632
634 {
636
637 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
639
640 memset(&
s->packet, 0,
sizeof(
s->packet));
641 s->delayed_samples = 0;
642
645
647
650 }
651 }
652
654 {
656
657 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
659
662
664 s->out_dummy_allocated_size = 0;
665
669 }
670
672
674
677
678 return 0;
679 }
680
682 {
685
688
692
693 /* find out the channel configuration */
698 c->gain =
ff_exp10(
c->p.gain_i / (20.0 * 256));
699
700 /* allocate and init each independent decoder */
701 c->streams =
av_calloc(
c->p.nb_streams,
sizeof(*
c->streams));
705 }
706
707 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
710
711 s->output_channels = (
i <
c->p.nb_stereo_streams) ? 2 : 1;
712
714
715 for (
int j = 0; j <
s->output_channels; j++) {
716 s->silk_output[j] =
s->silk_buf[j];
717 s->celt_output[j] =
s->celt_buf[j];
718 s->redundancy_output[j] =
s->redundancy_buf[j];
719 }
720
722
726
735
739
743
745 s->output_channels, 1024);
748
750 s->output_channels, 32);
753 }
754
755 return 0;
756 }
757
758 #define OFFSET(x) offsetof(OpusContext, x)
759 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
761 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
AD },
763 };
764
770 };
771
785 };