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
46
48
58
60 10, 20, 40, 60,
61 10, 20, 40, 60,
62 10, 20, 40, 60,
63 10, 20,
64 10, 20,
65 };
66
67 /* number of samples of silence to feed to the resampler
68 * at the beginning */
70 4, 8, 11, 11, 11
71 };
72
76
77 /* number of decoded samples for this stream */
79 /* current output buffers for this stream */
82 /* Buffer with samples from this stream for synchronizing
83 * the streams when they have different resampling delays */
85
91
96
99
100 /* buffers for the next samples to be decoded */
103
106
110 /* number of samples we still want to get from the resampler */
112
114
117
120
123
126
129
131 {
133 return 8000;
135 return 12000;
136 return 16000;
137 }
138
140 const float *in1, const float *in2,
142 {
146 }
147
149 {
153 (uint8_t**)
s->cur_out, nb_samples,
157 else if (
ret != nb_samples) {
161 }
162
163 if (celt_size) {
164 if (celt_size != nb_samples) {
167 }
169 for (
i = 0;
i <
s->output_channels;
i++) {
170 s->fdsp->vector_fmac_scalar(
s->cur_out[
i],
171 s->celt_output[
i], 1.0,
172 nb_samples);
173 }
174 }
175
176 if (
s->redundancy_idx) {
177 for (
i = 0;
i <
s->output_channels;
i++)
179 s->redundancy_output[
i] + 120 +
s->redundancy_idx,
181 s->redundancy_idx = 0;
182 }
183
184 s->cur_out[0] += nb_samples;
185 s->cur_out[1] += nb_samples;
186 s->remaining_out_size -= nb_samples *
sizeof(
float);
187
188 return 0;
189 }
190
192 {
193 static const float delay[16] = { 0.0 };
194 const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
196
202 }
203
209 "Error feeding initial silence to the resampler.\n");
211 }
212
213 return 0;
214 }
215
217 {
222
224 s->redundancy_output,
225 s->packet.stereo + 1, 240,
229
230 return 0;
234 }
235
237 {
238 int samples =
s->packet.frame_duration;
239 int redundancy = 0;
240 int redundancy_size, redundancy_pos;
241 int ret,
i, consumed;
243
247
248 /* decode the silk frame */
254 }
255
258 s->packet.stereo + 1,
263 }
265 (uint8_t**)
s->cur_out,
s->packet.frame_duration,
266 (
const uint8_t**)
s->silk_output,
samples);
270 }
272 s->delayed_samples +=
s->packet.frame_duration -
samples;
273 } else
275
276 // decode redundancy information
281 redundancy = 1;
282
283 if (redundancy) {
285
288 else
289 redundancy_size =
size - (consumed + 7) / 8;
290 size -= redundancy_size;
294 }
295
296 if (redundancy_pos) {
301 }
302 }
303
304 /* decode the CELT frame */
306 float *out_tmp[2] = {
s->cur_out[0],
s->cur_out[1] };
308 out_tmp :
s->celt_output;
309 int celt_output_samples =
samples;
311
312 if (delay_samples) {
315
316 for (
i = 0;
i <
s->output_channels;
i++) {
317 s->fdsp->vector_fmac_scalar(out_tmp[
i],
s->celt_output[
i], 1.0,
318 delay_samples);
319 out_tmp[
i] += delay_samples;
320 }
321 celt_output_samples -= delay_samples;
322 } else {
324 "Spurious CELT delay samples present.\n");
328 }
329 }
330
332
334 s->packet.stereo + 1,
335 s->packet.frame_duration,
340
342 int celt_delay =
s->packet.frame_duration - celt_output_samples;
343 void *delaybuf[2] = {
s->celt_output[0] + celt_output_samples,
344 s->celt_output[1] + celt_output_samples };
345
346 for (
i = 0;
i <
s->output_channels;
i++) {
347 s->fdsp->vector_fmac_scalar(out_tmp[
i],
348 s->celt_output[
i], 1.0,
349 celt_output_samples);
350 }
351
355 }
356 } else
358
359 if (
s->redundancy_idx) {
360 for (
i = 0;
i <
s->output_channels;
i++)
362 s->redundancy_output[
i] + 120 +
s->redundancy_idx,
364 s->redundancy_idx = 0;
365 }
366 if (redundancy) {
367 if (!redundancy_pos) {
372
373 for (
i = 0;
i <
s->output_channels;
i++) {
376 s->redundancy_output[
i] + 120,
380 }
381 } else {
382 for (
i = 0;
i <
s->output_channels;
i++) {
385 s->redundancy_output[
i] + 120,
388 }
389 }
390 }
391
393 }
394
396 const uint8_t *buf, int buf_size,
397 int nb_samples)
398 {
399 int output_samples = 0;
400 int flush_needed = 0;
402
403 s->cur_out[0] =
s->out[0];
404 s->cur_out[1] =
s->out[1];
405 s->remaining_out_size =
s->out_size;
406
407 /* check if we need to flush the resampler */
409 if (buf) {
410 int64_t cur_samplerate;
412 flush_needed = (
s->packet.mode ==
OPUS_MODE_CELT) || (cur_samplerate !=
s->silk_samplerate);
413 } else {
414 flush_needed = !!
s->delayed_samples;
415 }
416 }
417
418 if (!buf && !flush_needed)
419 return 0;
420
421 /* use dummy output buffers if the channel is not mapped to anything */
422 if (!
s->cur_out[0] ||
423 (
s->output_channels == 2 && !
s->cur_out[1])) {
425 s->remaining_out_size);
429 s->cur_out[0] =
s->out_dummy;
431 s->cur_out[1] =
s->out_dummy;
432 }
433
434 /* flush the resampler if necessary */
435 if (flush_needed) {
440 }
442 output_samples +=
s->delayed_samples;
443 s->delayed_samples = 0;
444
445 if (!buf)
447 }
448
449 /* decode all the frames in the packet */
450 for (
i = 0;
i <
s->packet.frame_count;
i++) {
451 int size =
s->packet.frame_size[
i];
453
458
459 for (j = 0; j <
s->output_channels; j++)
460 memset(
s->cur_out[j], 0,
s->packet.frame_duration *
sizeof(
float));
462 }
464
465 for (j = 0; j <
s->output_channels; j++)
467 s->remaining_out_size -=
samples *
sizeof(
float);
468 }
469
471 s->cur_out[0] =
s->cur_out[1] =
NULL;
472 s->remaining_out_size = 0;
473
474 return output_samples;
475 }
476
478 int *got_frame_ptr,
AVPacket *avpkt)
479 {
481 const uint8_t *buf = avpkt->
data;
482 int buf_size = avpkt->
size;
483 int coded_samples = 0;
487
488 /* calculate the number of delayed samples */
489 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
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
581 coded_samples);
584 s->decoded_samples =
ret;
586
587 buf +=
s->packet.packet_size;
588 buf_size -=
s->packet.packet_size;
589 }
590
591 /* buffer the extra samples */
592 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
595 if (buffer_samples) {
596 float *buf[2] = {
s->out[0] ?
s->out[0] : (
float*)
frame->extended_data[0],
597 s->out[1] ?
s->out[1] : (
float*)
frame->extended_data[0] };
603 }
604 }
605
608
609 /* handle copied channels */
611 memcpy(
frame->extended_data[
i],
612 frame->extended_data[
map->copy_idx],
614 }
else if (
map->silence) {
615 memset(
frame->extended_data[
i], 0,
frame->linesize[0]);
616 }
617
619 c->fdsp->vector_fmul_scalar((
float*)
frame->extended_data[
i],
620 (
float*)
frame->extended_data[
i],
622 }
623 }
624
627
629 }
630
632 {
634
635 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
637
638 memset(&
s->packet, 0,
sizeof(
s->packet));
639 s->delayed_samples = 0;
640
643
645
648 }
649 }
650
652 {
654
655 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
657
660
662 s->out_dummy_allocated_size = 0;
663
667 }
668
670
672
675
676 return 0;
677 }
678
680 {
683
686
690
691 /* find out the channel configuration */
696 c->gain =
ff_exp10(
c->p.gain_i / (20.0 * 256));
697
698 /* allocate and init each independent decoder */
699 c->streams =
av_calloc(
c->p.nb_streams,
sizeof(*
c->streams));
703 }
704
705 for (
int i = 0;
i <
c->p.nb_streams;
i++) {
708
709 s->output_channels = (
i <
c->p.nb_stereo_streams) ? 2 : 1;
710
712
713 for (
int j = 0; j <
s->output_channels; j++) {
714 s->silk_output[j] =
s->silk_buf[j];
715 s->celt_output[j] =
s->celt_buf[j];
716 s->redundancy_output[j] =
s->redundancy_buf[j];
717 }
718
720
724
733
737
741
743 s->output_channels, 1024);
746
748 s->output_channels, 32);
751 }
752
753 return 0;
754 }
755
756 #define OFFSET(x) offsetof(OpusContext, x)
757 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
759 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
AD },
761 };
762
768 };
769
783 };