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
42
44
50
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 */
62 4, 8, 11, 11, 11
63 };
64
66
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 */
80 {
82 if (ret < 0)
84
89
90 return 0;
91 }
92
94 unsigned int bytes)
95 {
100 }
101
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
112 {
118 if (ret < 0)
120 else if (ret != nb_samples) {
122 ret);
124 }
125
126 if (celt_size) {
127 if (celt_size != nb_samples) {
130 }
135 nb_samples);
136 }
137 }
138
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 };
159
162 if (ret < 0) {
165 }
166
170 if (ret < 0) {
172 "Error feeding initial silence to the resampler.\n");
174 }
175
176 return 0;
177 }
178
180 {
183
187
189 if (ret < 0)
190 goto fail;
192
197 if (ret < 0)
198 goto fail;
199
200 return 0;
201 fail:
204 }
205
207 {
209 int redundancy = 0;
210 int redundancy_size, redundancy_pos;
211 int ret, i, consumed;
213
215 if (ret < 0)
217
218 /* decode the silk frame */
222 if (ret < 0)
224 }
225
230 if (samples < 0) {
232 return samples;
233 }
237 if (samples < 0) {
239 return samples;
240 }
243 } else
245
246 // decode redundancy information
251 redundancy = 1;
252
253 if (redundancy) {
255
258 else
259 redundancy_size = size - (consumed + 7) / 8;
260 size -= redundancy_size;
261 if (size < 0) {
264 }
265
266 if (redundancy_pos) {
268 if (ret < 0)
271 }
272 }
273
274 /* decode the CELT frame */
276 float *out_tmp[2] = { s->
out[0], s->
out[1] };
279 int celt_output_samples = samples;
281
282 if (delay_samples) {
285
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");
298 }
299 }
300
302
308 if (ret < 0)
310
313 void *delaybuf[2] = { s->
celt_output[0] + celt_output_samples,
315
319 celt_output_samples);
320 }
321
323 if (ret < 0)
325 }
326 } else
328
335 }
336 if (redundancy) {
337 if (!redundancy_pos) {
340 if (ret < 0)
342
345 s->
out[i] + samples - 120 + delayed_samples,
348 if (delayed_samples)
350 }
351 } else {
356 s->
out[i] + 120 + delayed_samples,
358 }
359 }
360 }
361
362 return samples;
363 }
364
367 int nb_samples)
368 {
369 int output_samples = 0;
370 int flush_needed = 0;
372
373 /* check if we need to flush the resampler */
375 if (buf) {
376 int64_t cur_samplerate;
379 } else {
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 */
397 }
398
399 /* flush the resampler if necessary */
400 if (flush_needed) {
402 if (ret < 0) {
405 }
409
410 if (!buf)
412 }
413
414 /* decode all the frames in the packet */
418
419 if (samples < 0) {
422 return samples;
423
427 }
428 output_samples += samples;
429
431 s->
out[j] += samples;
432 s->
out_size -= samples *
sizeof(
float);
433 }
434
438
439 return output_samples;
440 }
441
443 int *got_frame_ptr,
AVPacket *avpkt)
444 {
448 int buf_size = avpkt->
size;
449 int coded_samples = 0;
450 int decoded_samples = 0;
452
453 /* decode the header of the first sub-packet to find out the sample count */
454 if (buf) {
457 if (ret < 0) {
460 }
463 }
464
466
467 /* no input or buffered data => nothing to do */
469 *got_frame_ptr = 0;
470 return 0;
471 }
472
473 /* setup the data buffers */
475 if (ret < 0)
478
479 for (i = 0; i < avctx->
channels; i++) {
483 }
484
487
488 /* decode each sub-packet */
491
492 if (i && buf) {
494 if (ret < 0) {
497 }
500 "Mismatching coded sample count in substream %d.\n", i);
502 }
503
505 }
506
509 if (ret < 0)
511 if (decoded_samples && ret != decoded_samples) {
513 "in a multi-channel stream\n");
515 }
516 decoded_samples =
ret;
519 }
520
521 for (i = 0; i < avctx->
channels; i++) {
523
524 /* handle copied channels */
531 }
532
537 }
538 }
539
541 *got_frame_ptr = !!decoded_samples;
542
544 }
545
547 {
549 int i;
550
553
556
560
563 }
564 }
565
567 {
569 int i;
570
573
576
579
582 }
583
586
589
590 return 0;
591 }
592
594 {
597
600
604
605 /* find out the channel configuration */
607 if (ret < 0)
609
610 /* allocate and init each independent decoder */
615 goto fail;
616 }
617
621
623
625
630 }
631
633
636 goto fail;
637
645
647 if (ret < 0)
648 goto fail;
649
651 if (ret < 0)
652 goto fail;
653
658 goto fail;
659 }
660 }
661
662 return 0;
663 fail:
666 }
667
679 };