summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/opus_decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/opus_decoder.c')
-rw-r--r--lib/rbcodec/codecs/libopus/opus_decoder.c999
1 files changed, 999 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libopus/opus_decoder.c b/lib/rbcodec/codecs/libopus/opus_decoder.c
new file mode 100644
index 0000000000..7103b183b8
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/opus_decoder.c
@@ -0,0 +1,999 @@
1/* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "opus_config.h"
30#endif
31
32#ifndef OPUS_BUILD
33#error "OPUS_BUILD _MUST_ be defined to build Opus and you probably want a decent config.h, see README for more details."
34#endif
35
36#include <stdarg.h>
37#include "celt.h"
38#include "opus.h"
39#include "entdec.h"
40#include "modes.h"
41#include "API.h"
42#include "stack_alloc.h"
43#include "float_cast.h"
44#include "opus_private.h"
45#include "os_support.h"
46#include "structs.h"
47#include "define.h"
48#include "mathops.h"
49
50struct OpusDecoder {
51 int celt_dec_offset;
52 int silk_dec_offset;
53 int channels;
54 opus_int32 Fs; /** Sampling rate (at the API level) */
55 silk_DecControlStruct DecControl;
56 int decode_gain;
57
58 /* Everything beyond this point gets cleared on a reset */
59#define OPUS_DECODER_RESET_START stream_channels
60 int stream_channels;
61
62 int bandwidth;
63 int mode;
64 int prev_mode;
65 int frame_size;
66 int prev_redundancy;
67
68 opus_uint32 rangeFinal;
69};
70
71#ifdef FIXED_POINT
72static inline opus_int16 SAT16(opus_int32 x) {
73 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
74}
75#endif
76
77
78int opus_decoder_get_size(int channels)
79{
80 int silkDecSizeBytes, celtDecSizeBytes;
81 int ret;
82 if (channels<1 || channels > 2)
83 return 0;
84 ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
85 if(ret)
86 return 0;
87 silkDecSizeBytes = align(silkDecSizeBytes);
88 celtDecSizeBytes = celt_decoder_get_size(channels);
89 return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes;
90}
91
92int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
93{
94 void *silk_dec;
95 CELTDecoder *celt_dec;
96 int ret, silkDecSizeBytes;
97
98 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
99 || (channels!=1&&channels!=2))
100 return OPUS_BAD_ARG;
101
102 OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
103 /* Initialize SILK encoder */
104 ret = silk_Get_Decoder_Size(&silkDecSizeBytes);
105 if (ret)
106 return OPUS_INTERNAL_ERROR;
107
108 silkDecSizeBytes = align(silkDecSizeBytes);
109 st->silk_dec_offset = align(sizeof(OpusDecoder));
110 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
111 silk_dec = (char*)st+st->silk_dec_offset;
112 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
113 st->stream_channels = st->channels = channels;
114
115 st->Fs = Fs;
116 st->DecControl.API_sampleRate = st->Fs;
117 st->DecControl.nChannelsAPI = st->channels;
118
119 /* Reset decoder */
120 ret = silk_InitDecoder( silk_dec );
121 if(ret)return OPUS_INTERNAL_ERROR;
122
123 /* Initialize CELT decoder */
124 ret = celt_decoder_init(celt_dec, Fs, channels);
125 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
126
127 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
128
129 st->prev_mode = 0;
130 st->frame_size = Fs/400;
131 return OPUS_OK;
132}
133
134OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error)
135{
136 int ret;
137 OpusDecoder *st;
138 if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)
139 || (channels!=1&&channels!=2))
140 {
141 if (error)
142 *error = OPUS_BAD_ARG;
143 return NULL;
144 }
145 st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels));
146 if (st == NULL)
147 {
148 if (error)
149 *error = OPUS_ALLOC_FAIL;
150 return NULL;
151 }
152 ret = opus_decoder_init(st, Fs, channels);
153 if (error)
154 *error = ret;
155 if (ret != OPUS_OK)
156 {
157 opus_free(st);
158 st = NULL;
159 }
160 return st;
161}
162
163static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2,
164 opus_val16 *out, int overlap, int channels,
165 const opus_val16 *window, opus_int32 Fs)
166{
167 int i, c;
168 int inc = 48000/Fs;
169 for (c=0;c<channels;c++)
170 {
171 for (i=0;i<overlap;i++)
172 {
173 opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]);
174 out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]),
175 Q15ONE-w, in1[i*channels+c]), 15);
176 }
177 }
178}
179
180static int opus_packet_get_mode(const unsigned char *data)
181{
182 int mode;
183 if (data[0]&0x80)
184 {
185 mode = MODE_CELT_ONLY;
186 } else if ((data[0]&0x60) == 0x60)
187 {
188 mode = MODE_HYBRID;
189 } else {
190 mode = MODE_SILK_ONLY;
191 }
192 return mode;
193}
194
195static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
196 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
197{
198 void *silk_dec;
199 CELTDecoder *celt_dec;
200 int i, silk_ret=0, celt_ret=0;
201 ec_dec dec;
202 opus_int32 silk_frame_size;
203 VARDECL(opus_int16, pcm_silk);
204 VARDECL(opus_val16, pcm_transition);
205 VARDECL(opus_val16, redundant_audio);
206
207 int audiosize;
208 int mode;
209 int transition=0;
210 int start_band;
211 int redundancy=0;
212 int redundancy_bytes = 0;
213 int celt_to_silk=0;
214 int c;
215 int F2_5, F5, F10, F20;
216 const opus_val16 *window;
217 opus_uint32 redundant_rng = 0;
218 ALLOC_STACK;
219
220 silk_dec = (char*)st+st->silk_dec_offset;
221 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
222 F20 = st->Fs/50;
223 F10 = F20>>1;
224 F5 = F10>>1;
225 F2_5 = F5>>1;
226 if (frame_size < F2_5)
227 {
228 RESTORE_STACK;
229 return OPUS_BUFFER_TOO_SMALL;
230 }
231 /* Limit frame_size to avoid excessive stack allocations. */
232 frame_size = IMIN(frame_size, st->Fs/25*3);
233 /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */
234 if (len<=1)
235 {
236 data = NULL;
237 /* In that case, don't conceal more than what the ToC says */
238 frame_size = IMIN(frame_size, st->frame_size);
239 }
240 if (data != NULL)
241 {
242 audiosize = st->frame_size;
243 mode = st->mode;
244 ec_dec_init(&dec,(unsigned char*)data,len);
245 } else {
246 audiosize = frame_size;
247
248 if (st->prev_mode == 0)
249 {
250 /* If we haven't got any packet yet, all we can do is return zeros */
251 for (i=0;i<audiosize*st->channels;i++)
252 pcm[i] = 0;
253 RESTORE_STACK;
254 return audiosize;
255 } else {
256 mode = st->prev_mode;
257 }
258 }
259
260 /* For CELT/hybrid PLC of more than 20 ms, do multiple calls */
261 if (data==NULL && frame_size > F20 && mode != MODE_SILK_ONLY)
262 {
263 int nb_samples = 0;
264 do {
265 int ret = opus_decode_frame(st, NULL, 0, pcm, F20, 0);
266 if (ret != F20)
267 {
268 RESTORE_STACK;
269 return OPUS_INTERNAL_ERROR;
270 }
271 pcm += F20*st->channels;
272 nb_samples += F20;
273 } while (nb_samples < frame_size);
274 RESTORE_STACK;
275 return frame_size;
276 }
277 ALLOC(pcm_transition, F5*st->channels, opus_val16);
278
279 if (data!=NULL && st->prev_mode > 0 && (
280 (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy)
281 || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
282 )
283 {
284 transition = 1;
285 if (mode == MODE_CELT_ONLY)
286 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
287 }
288 if (audiosize > frame_size)
289 {
290 /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
291 RESTORE_STACK;
292 return OPUS_BAD_ARG;
293 } else {
294 frame_size = audiosize;
295 }
296
297 ALLOC(pcm_silk, IMAX(F10, frame_size)*st->channels, opus_int16);
298 ALLOC(redundant_audio, F5*st->channels, opus_val16);
299
300 /* SILK processing */
301 if (mode != MODE_CELT_ONLY)
302 {
303 int lost_flag, decoded_samples;
304 opus_int16 *pcm_ptr = pcm_silk;
305
306 if (st->prev_mode==MODE_CELT_ONLY)
307 silk_InitDecoder( silk_dec );
308
309 /* The SILK PLC cannot produce frames of less than 10 ms */
310 st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs);
311
312 if (data != NULL)
313 {
314 st->DecControl.nChannelsInternal = st->stream_channels;
315 if( mode == MODE_SILK_ONLY ) {
316 if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) {
317 st->DecControl.internalSampleRate = 8000;
318 } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) {
319 st->DecControl.internalSampleRate = 12000;
320 } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) {
321 st->DecControl.internalSampleRate = 16000;
322 } else {
323 st->DecControl.internalSampleRate = 16000;
324 silk_assert( 0 );
325 }
326 } else {
327 /* Hybrid mode */
328 st->DecControl.internalSampleRate = 16000;
329 }
330 }
331
332 lost_flag = data == NULL ? 1 : 2 * decode_fec;
333 decoded_samples = 0;
334 do {
335 /* Call SILK decoder */
336 int first_frame = decoded_samples == 0;
337 silk_ret = silk_Decode( silk_dec, &st->DecControl,
338 lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size );
339 if( silk_ret ) {
340 if (lost_flag) {
341 /* PLC failure should not be fatal */
342 silk_frame_size = frame_size;
343 for (i=0;i<frame_size*st->channels;i++)
344 pcm_ptr[i] = 0;
345 } else {
346 RESTORE_STACK;
347 return OPUS_INVALID_PACKET;
348 }
349 }
350 pcm_ptr += silk_frame_size * st->channels;
351 decoded_samples += silk_frame_size;
352 } while( decoded_samples < frame_size );
353 }
354
355 start_band = 0;
356 if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
357 && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
358 {
359 /* Check if we have a redundant 0-8 kHz band */
360 if (mode == MODE_HYBRID)
361 redundancy = ec_dec_bit_logp(&dec, 12);
362 else
363 redundancy = 1;
364 if (redundancy)
365 {
366 celt_to_silk = ec_dec_bit_logp(&dec, 1);
367 /* redundancy_bytes will be at least two, in the non-hybrid
368 case due to the ec_tell() check above */
369 redundancy_bytes = mode==MODE_HYBRID ?
370 (opus_int32)ec_dec_uint(&dec, 256)+2 :
371 len-((ec_tell(&dec)+7)>>3);
372 len -= redundancy_bytes;
373 /* This is a sanity check. It should never happen for a valid
374 packet, so the exact behaviour is not normative. */
375 if (len*8 < ec_tell(&dec))
376 {
377 len = 0;
378 redundancy_bytes = 0;
379 redundancy = 0;
380 }
381 /* Shrink decoder because of raw bits */
382 dec.storage -= redundancy_bytes;
383 }
384 }
385 if (mode != MODE_CELT_ONLY)
386 start_band = 17;
387
388 {
389 int endband=21;
390
391 switch(st->bandwidth)
392 {
393 case OPUS_BANDWIDTH_NARROWBAND:
394 endband = 13;
395 break;
396 case OPUS_BANDWIDTH_MEDIUMBAND:
397 case OPUS_BANDWIDTH_WIDEBAND:
398 endband = 17;
399 break;
400 case OPUS_BANDWIDTH_SUPERWIDEBAND:
401 endband = 19;
402 break;
403 case OPUS_BANDWIDTH_FULLBAND:
404 endband = 21;
405 break;
406 }
407 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
408 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
409 }
410
411 if (redundancy)
412 transition = 0;
413
414 if (transition && mode != MODE_CELT_ONLY)
415 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
416
417 /* 5 ms redundant frame for CELT->SILK*/
418 if (redundancy && celt_to_silk)
419 {
420 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
421 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
422 redundant_audio, F5, NULL);
423 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
424 }
425
426 /* MUST be after PLC */
427 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band));
428
429 if (mode != MODE_SILK_ONLY)
430 {
431 int celt_frame_size = IMIN(F20, frame_size);
432 /* Make sure to discard any previous CELT state */
433 if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy)
434 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
435 /* Decode CELT */
436 celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data,
437 len, pcm, celt_frame_size, &dec);
438 } else {
439 unsigned char silence[2] = {0xFF, 0xFF};
440 for (i=0;i<frame_size*st->channels;i++)
441 pcm[i] = 0;
442 /* For hybrid -> SILK transitions, we let the CELT MDCT
443 do a fade-out by decoding a silence frame */
444 if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) )
445 {
446 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
447 celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL);
448 }
449 }
450
451 if (mode != MODE_CELT_ONLY)
452 {
453#ifdef FIXED_POINT
454 for (i=0;i<frame_size*st->channels;i++)
455 pcm[i] = SAT16(pcm[i] + pcm_silk[i]);
456#else
457 for (i=0;i<frame_size*st->channels;i++)
458 pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]);
459#endif
460 }
461
462 {
463 const CELTMode *celt_mode;
464 celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode));
465 window = celt_mode->window;
466 }
467
468 /* 5 ms redundant frame for SILK->CELT */
469 if (redundancy && !celt_to_silk)
470 {
471 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
472 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
473
474 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL);
475 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
476 smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5,
477 pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs);
478 }
479 if (redundancy && celt_to_silk)
480 {
481 for (c=0;c<st->channels;c++)
482 {
483 for (i=0;i<F2_5;i++)
484 pcm[st->channels*i+c] = redundant_audio[st->channels*i+c];
485 }
486 smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5,
487 pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs);
488 }
489 if (transition)
490 {
491 if (audiosize >= F5)
492 {
493 for (i=0;i<st->channels*F2_5;i++)
494 pcm[i] = pcm_transition[i];
495 smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5,
496 pcm+st->channels*F2_5, F2_5,
497 st->channels, window, st->Fs);
498 } else {
499 /* Not enough time to do a clean transition, but we do it anyway
500 This will not preserve amplitude perfectly and may introduce
501 a bit of temporal aliasing, but it shouldn't be too bad and
502 that's pretty much the best we can do. In any case, generating this
503 transition it pretty silly in the first place */
504 smooth_fade(pcm_transition, pcm,
505 pcm, F2_5,
506 st->channels, window, st->Fs);
507 }
508 }
509
510 if(st->decode_gain)
511 {
512 opus_val32 gain;
513 gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain));
514 for (i=0;i<frame_size*st->channels;i++)
515 {
516 opus_val32 x;
517 x = MULT16_32_P16(pcm[i],gain);
518 pcm[i] = SATURATE(x, 32767);
519 }
520 }
521
522 if (len <= 1)
523 st->rangeFinal = 0;
524 else
525 st->rangeFinal = dec.rng ^ redundant_rng;
526
527 st->prev_mode = mode;
528 st->prev_redundancy = redundancy && !celt_to_silk;
529 RESTORE_STACK;
530 return celt_ret < 0 ? celt_ret : audiosize;
531
532}
533
534static int parse_size(const unsigned char *data, opus_int32 len, short *size)
535{
536 if (len<1)
537 {
538 *size = -1;
539 return -1;
540 } else if (data[0]<252)
541 {
542 *size = data[0];
543 return 1;
544 } else if (len<2)
545 {
546 *size = -1;
547 return -1;
548 } else {
549 *size = 4*data[1] + data[0];
550 return 2;
551 }
552}
553
554static int opus_packet_parse_impl(const unsigned char *data, opus_int32 len,
555 int self_delimited, unsigned char *out_toc,
556 const unsigned char *frames[48], short size[48], int *payload_offset)
557{
558 int i, bytes;
559 int count;
560 int cbr;
561 unsigned char ch, toc;
562 int framesize;
563 int last_size;
564 const unsigned char *data0 = data;
565
566 if (size==NULL)
567 return OPUS_BAD_ARG;
568
569 framesize = opus_packet_get_samples_per_frame(data, 48000);
570
571 cbr = 0;
572 toc = *data++;
573 len--;
574 last_size = len;
575 switch (toc&0x3)
576 {
577 /* One frame */
578 case 0:
579 count=1;
580 break;
581 /* Two CBR frames */
582 case 1:
583 count=2;
584 cbr = 1;
585 if (!self_delimited)
586 {
587 if (len&0x1)
588 return OPUS_INVALID_PACKET;
589 size[0] = last_size = len/2;
590 }
591 break;
592 /* Two VBR frames */
593 case 2:
594 count = 2;
595 bytes = parse_size(data, len, size);
596 len -= bytes;
597 if (size[0]<0 || size[0] > len)
598 return OPUS_INVALID_PACKET;
599 data += bytes;
600 last_size = len-size[0];
601 break;
602 /* Multiple CBR/VBR frames (from 0 to 120 ms) */
603 default: /*case 3:*/
604 if (len<1)
605 return OPUS_INVALID_PACKET;
606 /* Number of frames encoded in bits 0 to 5 */
607 ch = *data++;
608 count = ch&0x3F;
609 if (count <= 0 || framesize*count > 5760)
610 return OPUS_INVALID_PACKET;
611 len--;
612 /* Padding flag is bit 6 */
613 if (ch&0x40)
614 {
615 int padding=0;
616 int p;
617 do {
618 if (len<=0)
619 return OPUS_INVALID_PACKET;
620 p = *data++;
621 len--;
622 padding += p==255 ? 254: p;
623 } while (p==255);
624 len -= padding;
625 }
626 if (len<0)
627 return OPUS_INVALID_PACKET;
628 /* VBR flag is bit 7 */
629 cbr = !(ch&0x80);
630 if (!cbr)
631 {
632 /* VBR case */
633 last_size = len;
634 for (i=0;i<count-1;i++)
635 {
636 bytes = parse_size(data, len, size+i);
637 len -= bytes;
638 if (size[i]<0 || size[i] > len)
639 return OPUS_INVALID_PACKET;
640 data += bytes;
641 last_size -= bytes+size[i];
642 }
643 if (last_size<0)
644 return OPUS_INVALID_PACKET;
645 } else if (!self_delimited)
646 {
647 /* CBR case */
648 last_size = len/count;
649 if (last_size*count!=len)
650 return OPUS_INVALID_PACKET;
651 for (i=0;i<count-1;i++)
652 size[i] = last_size;
653 }
654 break;
655 }
656 /* Self-delimited framing has an extra size for the last frame. */
657 if (self_delimited)
658 {
659 bytes = parse_size(data, len, size+count-1);
660 len -= bytes;
661 if (size[count-1]<0 || size[count-1] > len)
662 return OPUS_INVALID_PACKET;
663 data += bytes;
664 /* For CBR packets, apply the size to all the frames. */
665 if (cbr)
666 {
667 if (size[count-1]*count > len)
668 return OPUS_INVALID_PACKET;
669 for (i=0;i<count-1;i++)
670 size[i] = size[count-1];
671 } else if(size[count-1] > last_size)
672 return OPUS_INVALID_PACKET;
673 } else
674 {
675 /* Because it's not encoded explicitly, it's possible the size of the
676 last packet (or all the packets, for the CBR case) is larger than
677 1275. Reject them here.*/
678 if (last_size > 1275)
679 return OPUS_INVALID_PACKET;
680 size[count-1] = last_size;
681 }
682
683 if (frames)
684 {
685 for (i=0;i<count;i++)
686 {
687 frames[i] = data;
688 data += size[i];
689 }
690 }
691
692 if (out_toc)
693 *out_toc = toc;
694
695 if (payload_offset)
696 *payload_offset = data-data0;
697
698 return count;
699}
700
701int opus_packet_parse(const unsigned char *data, opus_int32 len,
702 unsigned char *out_toc, const unsigned char *frames[48],
703 short size[48], int *payload_offset)
704{
705 return opus_packet_parse_impl(data, len, 0, out_toc,
706 frames, size, payload_offset);
707}
708
709int opus_decode_native(OpusDecoder *st, const unsigned char *data,
710 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec,
711 int self_delimited, int *packet_offset)
712{
713 int i, nb_samples;
714 int count, offset;
715 unsigned char toc;
716 int tot_offset;
717 /* 48 x 2.5 ms = 120 ms */
718 short size[48];
719 if (decode_fec<0 || decode_fec>1)
720 return OPUS_BAD_ARG;
721 if (len==0 || data==NULL)
722 return opus_decode_frame(st, NULL, 0, pcm, frame_size, 0);
723 else if (len<0)
724 return OPUS_BAD_ARG;
725
726 tot_offset = 0;
727 st->mode = opus_packet_get_mode(data);
728 st->bandwidth = opus_packet_get_bandwidth(data);
729 st->frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
730 st->stream_channels = opus_packet_get_nb_channels(data);
731
732 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &offset);
733 if (count < 0)
734 return count;
735
736 data += offset;
737 tot_offset += offset;
738
739 if (count*st->frame_size > frame_size)
740 return OPUS_BUFFER_TOO_SMALL;
741 nb_samples=0;
742 for (i=0;i<count;i++)
743 {
744 int ret;
745 ret = opus_decode_frame(st, data, size[i], pcm, frame_size-nb_samples, decode_fec);
746 if (ret<0)
747 return ret;
748 data += size[i];
749 tot_offset += size[i];
750 pcm += ret*st->channels;
751 nb_samples += ret;
752 }
753 if (packet_offset != NULL)
754 *packet_offset = tot_offset;
755 return nb_samples;
756}
757
758#ifdef FIXED_POINT
759
760int opus_decode(OpusDecoder *st, const unsigned char *data,
761 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
762{
763 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL);
764}
765
766#ifndef DISABLE_FLOAT_API
767int opus_decode_float(OpusDecoder *st, const unsigned char *data,
768 opus_int32 len, float *pcm, int frame_size, int decode_fec)
769{
770 VARDECL(opus_int16, out);
771 int ret, i;
772 ALLOC_STACK;
773
774 ALLOC(out, frame_size*st->channels, opus_int16);
775
776 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL);
777 if (ret > 0)
778 {
779 for (i=0;i<ret*st->channels;i++)
780 pcm[i] = (1.f/32768.f)*(out[i]);
781 }
782 RESTORE_STACK;
783 return ret;
784}
785#endif
786
787
788#else
789int opus_decode(OpusDecoder *st, const unsigned char *data,
790 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
791{
792 VARDECL(float, out);
793 int ret, i;
794 ALLOC_STACK;
795
796 if(frame_size<0)
797 {
798 RESTORE_STACK;
799 return OPUS_BAD_ARG;
800 }
801
802 ALLOC(out, frame_size*st->channels, float);
803
804 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL);
805 if (ret > 0)
806 {
807 for (i=0;i<ret*st->channels;i++)
808 pcm[i] = FLOAT2INT16(out[i]);
809 }
810 RESTORE_STACK;
811 return ret;
812}
813
814int opus_decode_float(OpusDecoder *st, const unsigned char *data,
815 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
816{
817 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL);
818}
819
820#endif
821
822int opus_decoder_ctl(OpusDecoder *st, int request, ...)
823{
824 int ret = OPUS_OK;
825 va_list ap;
826 void *silk_dec;
827 CELTDecoder *celt_dec;
828
829 silk_dec = (char*)st+st->silk_dec_offset;
830 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
831
832
833 va_start(ap, request);
834
835 switch (request)
836 {
837 case OPUS_GET_BANDWIDTH_REQUEST:
838 {
839 opus_int32 *value = va_arg(ap, opus_int32*);
840 *value = st->bandwidth;
841 }
842 break;
843 case OPUS_GET_FINAL_RANGE_REQUEST:
844 {
845 opus_uint32 *value = va_arg(ap, opus_uint32*);
846 *value = st->rangeFinal;
847 }
848 break;
849 case OPUS_RESET_STATE:
850 {
851 OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START,
852 sizeof(OpusDecoder)-
853 ((char*)&st->OPUS_DECODER_RESET_START - (char*)st));
854
855 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE);
856 silk_InitDecoder( silk_dec );
857 st->stream_channels = st->channels;
858 st->frame_size = st->Fs/400;
859 }
860 break;
861 case OPUS_GET_SAMPLE_RATE_REQUEST:
862 {
863 opus_int32 *value = va_arg(ap, opus_int32*);
864 if (value==NULL)
865 {
866 ret = OPUS_BAD_ARG;
867 break;
868 }
869 *value = st->Fs;
870 }
871 break;
872 case OPUS_GET_PITCH_REQUEST:
873 {
874 opus_int32 *value = va_arg(ap, opus_int32*);
875 if (value==NULL)
876 {
877 ret = OPUS_BAD_ARG;
878 break;
879 }
880 if (st->prev_mode == MODE_CELT_ONLY)
881 celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value));
882 else
883 *value = st->DecControl.prevPitchLag;
884 }
885 break;
886 case OPUS_GET_GAIN_REQUEST:
887 {
888 opus_int32 *value = va_arg(ap, opus_int32*);
889 if (value==NULL)
890 {
891 ret = OPUS_BAD_ARG;
892 break;
893 }
894 *value = st->decode_gain;
895 }
896 break;
897 case OPUS_SET_GAIN_REQUEST:
898 {
899 opus_int32 value = va_arg(ap, opus_int32);
900 if (value<-32768 || value>32767)
901 {
902 ret = OPUS_BAD_ARG;
903 break;
904 }
905 st->decode_gain = value;
906 }
907 break;
908 default:
909 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
910 ret = OPUS_UNIMPLEMENTED;
911 break;
912 }
913
914 va_end(ap);
915 return ret;
916}
917
918void opus_decoder_destroy(OpusDecoder *st)
919{
920 opus_free(st);
921}
922
923
924int opus_packet_get_bandwidth(const unsigned char *data)
925{
926 int bandwidth;
927 if (data[0]&0x80)
928 {
929 bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3);
930 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
931 bandwidth = OPUS_BANDWIDTH_NARROWBAND;
932 } else if ((data[0]&0x60) == 0x60)
933 {
934 bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND :
935 OPUS_BANDWIDTH_SUPERWIDEBAND;
936 } else {
937 bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3);
938 }
939 return bandwidth;
940}
941
942int opus_packet_get_samples_per_frame(const unsigned char *data,
943 opus_int32 Fs)
944{
945 int audiosize;
946 if (data[0]&0x80)
947 {
948 audiosize = ((data[0]>>3)&0x3);
949 audiosize = (Fs<<audiosize)/400;
950 } else if ((data[0]&0x60) == 0x60)
951 {
952 audiosize = (data[0]&0x08) ? Fs/50 : Fs/100;
953 } else {
954 audiosize = ((data[0]>>3)&0x3);
955 if (audiosize == 3)
956 audiosize = Fs*60/1000;
957 else
958 audiosize = (Fs<<audiosize)/100;
959 }
960 return audiosize;
961}
962
963int opus_packet_get_nb_channels(const unsigned char *data)
964{
965 return (data[0]&0x4) ? 2 : 1;
966}
967
968int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len)
969{
970 int count;
971 if (len<1)
972 return OPUS_BAD_ARG;
973 count = packet[0]&0x3;
974 if (count==0)
975 return 1;
976 else if (count!=3)
977 return 2;
978 else if (len<2)
979 return OPUS_INVALID_PACKET;
980 else
981 return packet[1]&0x3F;
982}
983
984int opus_decoder_get_nb_samples(const OpusDecoder *dec,
985 const unsigned char packet[], opus_int32 len)
986{
987 int samples;
988 int count = opus_packet_get_nb_frames(packet, len);
989
990 if (count<0)
991 return count;
992
993 samples = count*opus_packet_get_samples_per_frame(packet, dec->Fs);
994 /* Can't have more than 120 ms */
995 if (samples*25 > dec->Fs*3)
996 return OPUS_INVALID_PACKET;
997 else
998 return samples;
999}