Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: src/opus_decoder.c

Issue 107243004: Updating Opus to release 1.1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/opus
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/opus.c ('k') | src/opus_demo.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited 1 /* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */ 2 Written by Jean-Marc Valin and Koen Vos */
3 /* 3 /*
4 Redistribution and use in source and binary forms, with or without 4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions 5 modification, are permitted provided that the following conditions
6 are met: 6 are met:
7 7
8 - Redistributions of source code must retain the above copyright 8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer. 9 notice, this list of conditions and the following disclaimer.
10 10
11 - Redistributions in binary form must reproduce the above copyright 11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the 12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution. 13 documentation and/or other materials provided with the distribution.
14 14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 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. 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28 #ifdef HAVE_CONFIG_H 28 #ifdef HAVE_CONFIG_H
29 #include "config.h" 29 # include "config.h"
30 #endif 30 #endif
31 31
32 #ifndef OPUS_BUILD 32 #ifndef OPUS_BUILD
33 #error "OPUS_BUILD _MUST_ be defined to build Opus. This probably means you need other defines as well, as in a config.h. See the included build files for detai ls." 33 # error "OPUS_BUILD _MUST_ be defined to build Opus. This probably means you nee d other defines as well, as in a config.h. See the included build files for deta ils."
34 #endif
35
36 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
37 # pragma message "You appear to be compiling without optimization, if so opus wi ll be very slow."
34 #endif 38 #endif
35 39
36 #include <stdarg.h> 40 #include <stdarg.h>
37 #include "celt.h" 41 #include "celt.h"
38 #include "opus.h" 42 #include "opus.h"
39 #include "entdec.h" 43 #include "entdec.h"
40 #include "modes.h" 44 #include "modes.h"
41 #include "API.h" 45 #include "API.h"
42 #include "stack_alloc.h" 46 #include "stack_alloc.h"
43 #include "float_cast.h" 47 #include "float_cast.h"
(...skipping 20 matching lines...) Expand all
64 int mode; 68 int mode;
65 int prev_mode; 69 int prev_mode;
66 int frame_size; 70 int frame_size;
67 int prev_redundancy; 71 int prev_redundancy;
68 int last_packet_duration; 72 int last_packet_duration;
69 #ifndef FIXED_POINT 73 #ifndef FIXED_POINT
70 opus_val16 softclip_mem[2]; 74 opus_val16 softclip_mem[2];
71 #endif 75 #endif
72 76
73 opus_uint32 rangeFinal; 77 opus_uint32 rangeFinal;
74 int arch;
75 }; 78 };
76 79
77 #ifdef FIXED_POINT 80 #ifdef FIXED_POINT
78 static inline opus_int16 SAT16(opus_int32 x) { 81 static OPUS_INLINE opus_int16 SAT16(opus_int32 x) {
79 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x; 82 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x;
80 } 83 }
81 #endif 84 #endif
82 85
83 86
84 int opus_decoder_get_size(int channels) 87 int opus_decoder_get_size(int channels)
85 { 88 {
86 int silkDecSizeBytes, celtDecSizeBytes; 89 int silkDecSizeBytes, celtDecSizeBytes;
87 int ret; 90 int ret;
88 if (channels<1 || channels > 2) 91 if (channels<1 || channels > 2)
(...skipping 25 matching lines...) Expand all
114 silkDecSizeBytes = align(silkDecSizeBytes); 117 silkDecSizeBytes = align(silkDecSizeBytes);
115 st->silk_dec_offset = align(sizeof(OpusDecoder)); 118 st->silk_dec_offset = align(sizeof(OpusDecoder));
116 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; 119 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes;
117 silk_dec = (char*)st+st->silk_dec_offset; 120 silk_dec = (char*)st+st->silk_dec_offset;
118 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); 121 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset);
119 st->stream_channels = st->channels = channels; 122 st->stream_channels = st->channels = channels;
120 123
121 st->Fs = Fs; 124 st->Fs = Fs;
122 st->DecControl.API_sampleRate = st->Fs; 125 st->DecControl.API_sampleRate = st->Fs;
123 st->DecControl.nChannelsAPI = st->channels; 126 st->DecControl.nChannelsAPI = st->channels;
124 st->arch = opus_select_arch();
125 127
126 /* Reset decoder */ 128 /* Reset decoder */
127 ret = silk_InitDecoder( silk_dec ); 129 ret = silk_InitDecoder( silk_dec );
128 if(ret)return OPUS_INTERNAL_ERROR; 130 if(ret)return OPUS_INTERNAL_ERROR;
129 131
130 /* Initialize CELT decoder */ 132 /* Initialize CELT decoder */
131 ret = celt_decoder_init(celt_dec, Fs, channels); 133 ret = celt_decoder_init(celt_dec, Fs, channels);
132 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; 134 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR;
133 135
134 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); 136 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0));
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 /* In that case, don't conceal more than what the ToC says */ 252 /* In that case, don't conceal more than what the ToC says */
251 frame_size = IMIN(frame_size, st->frame_size); 253 frame_size = IMIN(frame_size, st->frame_size);
252 } 254 }
253 if (data != NULL) 255 if (data != NULL)
254 { 256 {
255 audiosize = st->frame_size; 257 audiosize = st->frame_size;
256 mode = st->mode; 258 mode = st->mode;
257 ec_dec_init(&dec,(unsigned char*)data,len); 259 ec_dec_init(&dec,(unsigned char*)data,len);
258 } else { 260 } else {
259 audiosize = frame_size; 261 audiosize = frame_size;
262 mode = st->prev_mode;
260 263
261 if (st->prev_mode == 0) 264 if (mode == 0)
262 { 265 {
263 /* If we haven't got any packet yet, all we can do is return zeros */ 266 /* If we haven't got any packet yet, all we can do is return zeros */
264 for (i=0;i<audiosize*st->channels;i++) 267 for (i=0;i<audiosize*st->channels;i++)
265 pcm[i] = 0; 268 pcm[i] = 0;
266 RESTORE_STACK; 269 RESTORE_STACK;
267 return audiosize; 270 return audiosize;
268 } else { 271 }
269 mode = st->prev_mode; 272
273 /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT),
274 10, or 20 (e.g. 12.5 or 30 ms). */
275 if (audiosize > F20)
276 {
277 do {
278 int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0);
279 if (ret<0)
280 {
281 RESTORE_STACK;
282 return ret;
283 }
284 pcm += ret*st->channels;
285 audiosize -= ret;
286 } while (audiosize > 0);
287 RESTORE_STACK;
288 return frame_size;
289 } else if (audiosize < F20)
290 {
291 if (audiosize > F10)
292 audiosize = F10;
293 else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10)
294 audiosize = F5;
270 } 295 }
271 } 296 }
272 297
273 /* For CELT/hybrid PLC of more than 20 ms, opus_decode_native() will do 298 pcm_transition_silk_size = ALLOC_NONE;
274 multiple calls */ 299 pcm_transition_celt_size = ALLOC_NONE;
275 if (data==NULL && mode != MODE_SILK_ONLY)
276 frame_size = IMIN(frame_size, F20);
277
278 pcm_transition_silk_size = 0;
279 pcm_transition_celt_size = 0;
280 if (data!=NULL && st->prev_mode > 0 && ( 300 if (data!=NULL && st->prev_mode > 0 && (
281 (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_r edundancy) 301 (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_r edundancy)
282 || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) 302 || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) )
283 ) 303 )
284 { 304 {
285 transition = 1; 305 transition = 1;
286 /* Decide where to allocate the stack memory for pcm_transition */ 306 /* Decide where to allocate the stack memory for pcm_transition */
287 if (mode == MODE_CELT_ONLY) 307 if (mode == MODE_CELT_ONLY)
288 pcm_transition_celt_size = F5*st->channels; 308 pcm_transition_celt_size = F5*st->channels;
289 else 309 else
290 pcm_transition_silk_size = F5*st->channels; 310 pcm_transition_silk_size = F5*st->channels;
291 } 311 }
292 ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16); 312 ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16);
293 if (transition && mode == MODE_CELT_ONLY) 313 if (transition && mode == MODE_CELT_ONLY)
294 { 314 {
295 pcm_transition = pcm_transition_celt; 315 pcm_transition = pcm_transition_celt;
296 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); 316 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
297 } 317 }
298 if (audiosize > frame_size) 318 if (audiosize > frame_size)
299 { 319 {
300 /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosiz e, frame_size, mode);*/ 320 /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosiz e, frame_size, mode);*/
301 RESTORE_STACK; 321 RESTORE_STACK;
302 return OPUS_BAD_ARG; 322 return OPUS_BAD_ARG;
303 } else { 323 } else {
304 frame_size = audiosize; 324 frame_size = audiosize;
305 } 325 }
306 326
307 /* Don't allocate any memory when in CELT-only mode */ 327 /* Don't allocate any memory when in CELT-only mode */
308 pcm_silk_size = (mode != MODE_CELT_ONLY) ? IMAX(F10, frame_size)*st->channels : 0; 328 pcm_silk_size = (mode != MODE_CELT_ONLY) ? IMAX(F10, frame_size)*st->channels : ALLOC_NONE;
309 ALLOC(pcm_silk, pcm_silk_size, opus_int16); 329 ALLOC(pcm_silk, pcm_silk_size, opus_int16);
310 330
311 /* SILK processing */ 331 /* SILK processing */
312 if (mode != MODE_CELT_ONLY) 332 if (mode != MODE_CELT_ONLY)
313 { 333 {
314 int lost_flag, decoded_samples; 334 int lost_flag, decoded_samples;
315 opus_int16 *pcm_ptr = pcm_silk; 335 opus_int16 *pcm_ptr = pcm_silk;
316 336
317 if (st->prev_mode==MODE_CELT_ONLY) 337 if (st->prev_mode==MODE_CELT_ONLY)
318 silk_InitDecoder( silk_dec ); 338 silk_InitDecoder( silk_dec );
(...skipping 29 matching lines...) Expand all
348 silk_ret = silk_Decode( silk_dec, &st->DecControl, 368 silk_ret = silk_Decode( silk_dec, &st->DecControl,
349 lost_flag, first_frame, &dec, pcm_ptr, &silk_fra me_size ); 369 lost_flag, first_frame, &dec, pcm_ptr, &silk_fra me_size );
350 if( silk_ret ) { 370 if( silk_ret ) {
351 if (lost_flag) { 371 if (lost_flag) {
352 /* PLC failure should not be fatal */ 372 /* PLC failure should not be fatal */
353 silk_frame_size = frame_size; 373 silk_frame_size = frame_size;
354 for (i=0;i<frame_size*st->channels;i++) 374 for (i=0;i<frame_size*st->channels;i++)
355 pcm_ptr[i] = 0; 375 pcm_ptr[i] = 0;
356 } else { 376 } else {
357 RESTORE_STACK; 377 RESTORE_STACK;
358 return OPUS_INVALID_PACKET; 378 return OPUS_INTERNAL_ERROR;
359 } 379 }
360 } 380 }
361 pcm_ptr += silk_frame_size * st->channels; 381 pcm_ptr += silk_frame_size * st->channels;
362 decoded_samples += silk_frame_size; 382 decoded_samples += silk_frame_size;
363 } while( decoded_samples < frame_size ); 383 } while( decoded_samples < frame_size );
364 } 384 }
365 385
366 start_band = 0; 386 start_band = 0;
367 if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL 387 if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL
368 && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len) 388 && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len)
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 endband = 21; 435 endband = 21;
416 break; 436 break;
417 } 437 }
418 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)); 438 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband));
419 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)); 439 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels));
420 } 440 }
421 441
422 if (redundancy) 442 if (redundancy)
423 { 443 {
424 transition = 0; 444 transition = 0;
425 pcm_transition_silk_size=0; 445 pcm_transition_silk_size=ALLOC_NONE;
426 } 446 }
427 447
428 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16); 448 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16);
429 449
430 if (transition && mode != MODE_CELT_ONLY) 450 if (transition && mode != MODE_CELT_ONLY)
431 { 451 {
432 pcm_transition = pcm_transition_silk; 452 pcm_transition = pcm_transition_silk;
433 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); 453 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0);
434 } 454 }
435 455
436 /* Only allocation memory for redundancy if/when needed */ 456 /* Only allocation memory for redundancy if/when needed */
437 redundant_audio_size = redundancy ? F5*st->channels : 0; 457 redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE;
438 ALLOC(redundant_audio, redundant_audio_size, opus_val16); 458 ALLOC(redundant_audio, redundant_audio_size, opus_val16);
439 459
440 /* 5 ms redundant frame for CELT->SILK*/ 460 /* 5 ms redundant frame for CELT->SILK*/
441 if (redundancy && celt_to_silk) 461 if (redundancy && celt_to_silk)
442 { 462 {
443 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 463 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0));
444 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, 464 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes,
445 redundant_audio, F5, NULL); 465 redundant_audio, F5, NULL);
446 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); 466 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng));
447 } 467 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 { 574 {
555 if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) 575 if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels))
556 OPUS_PRINT_INT(audiosize); 576 OPUS_PRINT_INT(audiosize);
557 } 577 }
558 578
559 RESTORE_STACK; 579 RESTORE_STACK;
560 return celt_ret < 0 ? celt_ret : audiosize; 580 return celt_ret < 0 ? celt_ret : audiosize;
561 581
562 } 582 }
563 583
564 static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *siz e)
565 {
566 if (len<1)
567 {
568 *size = -1;
569 return -1;
570 } else if (data[0]<252)
571 {
572 *size = data[0];
573 return 1;
574 } else if (len<2)
575 {
576 *size = -1;
577 return -1;
578 } else {
579 *size = 4*data[1] + data[0];
580 return 2;
581 }
582 }
583
584 static int opus_packet_parse_impl(const unsigned char *data, opus_int32 len,
585 int self_delimited, unsigned char *out_toc,
586 const unsigned char *frames[48], opus_int16 size[48], int *payload_offset)
587 {
588 int i, bytes;
589 int count;
590 int cbr;
591 unsigned char ch, toc;
592 int framesize;
593 opus_int32 last_size;
594 const unsigned char *data0 = data;
595
596 if (size==NULL)
597 return OPUS_BAD_ARG;
598
599 framesize = opus_packet_get_samples_per_frame(data, 48000);
600
601 cbr = 0;
602 toc = *data++;
603 len--;
604 last_size = len;
605 switch (toc&0x3)
606 {
607 /* One frame */
608 case 0:
609 count=1;
610 break;
611 /* Two CBR frames */
612 case 1:
613 count=2;
614 cbr = 1;
615 if (!self_delimited)
616 {
617 if (len&0x1)
618 return OPUS_INVALID_PACKET;
619 last_size = len/2;
620 /* If last_size doesn't fit in size[0], we'll catch it later */
621 size[0] = (opus_int16)last_size;
622 }
623 break;
624 /* Two VBR frames */
625 case 2:
626 count = 2;
627 bytes = parse_size(data, len, size);
628 len -= bytes;
629 if (size[0]<0 || size[0] > len)
630 return OPUS_INVALID_PACKET;
631 data += bytes;
632 last_size = len-size[0];
633 break;
634 /* Multiple CBR/VBR frames (from 0 to 120 ms) */
635 default: /*case 3:*/
636 if (len<1)
637 return OPUS_INVALID_PACKET;
638 /* Number of frames encoded in bits 0 to 5 */
639 ch = *data++;
640 count = ch&0x3F;
641 if (count <= 0 || framesize*count > 5760)
642 return OPUS_INVALID_PACKET;
643 len--;
644 /* Padding flag is bit 6 */
645 if (ch&0x40)
646 {
647 int p;
648 do {
649 if (len<=0)
650 return OPUS_INVALID_PACKET;
651 p = *data++;
652 len--;
653 len -= p==255 ? 254: p;
654 } while (p==255);
655 }
656 if (len<0)
657 return OPUS_INVALID_PACKET;
658 /* VBR flag is bit 7 */
659 cbr = !(ch&0x80);
660 if (!cbr)
661 {
662 /* VBR case */
663 last_size = len;
664 for (i=0;i<count-1;i++)
665 {
666 bytes = parse_size(data, len, size+i);
667 len -= bytes;
668 if (size[i]<0 || size[i] > len)
669 return OPUS_INVALID_PACKET;
670 data += bytes;
671 last_size -= bytes+size[i];
672 }
673 if (last_size<0)
674 return OPUS_INVALID_PACKET;
675 } else if (!self_delimited)
676 {
677 /* CBR case */
678 last_size = len/count;
679 if (last_size*count!=len)
680 return OPUS_INVALID_PACKET;
681 for (i=0;i<count-1;i++)
682 size[i] = (opus_int16)last_size;
683 }
684 break;
685 }
686 /* Self-delimited framing has an extra size for the last frame. */
687 if (self_delimited)
688 {
689 bytes = parse_size(data, len, size+count-1);
690 len -= bytes;
691 if (size[count-1]<0 || size[count-1] > len)
692 return OPUS_INVALID_PACKET;
693 data += bytes;
694 /* For CBR packets, apply the size to all the frames. */
695 if (cbr)
696 {
697 if (size[count-1]*count > len)
698 return OPUS_INVALID_PACKET;
699 for (i=0;i<count-1;i++)
700 size[i] = size[count-1];
701 } else if(size[count-1] > last_size)
702 return OPUS_INVALID_PACKET;
703 } else
704 {
705 /* Because it's not encoded explicitly, it's possible the size of the
706 last packet (or all the packets, for the CBR case) is larger than
707 1275. Reject them here.*/
708 if (last_size > 1275)
709 return OPUS_INVALID_PACKET;
710 size[count-1] = (opus_int16)last_size;
711 }
712
713 if (frames)
714 {
715 for (i=0;i<count;i++)
716 {
717 frames[i] = data;
718 data += size[i];
719 }
720 }
721
722 if (out_toc)
723 *out_toc = toc;
724
725 if (payload_offset)
726 *payload_offset = (int)(data-data0);
727
728 return count;
729 }
730
731 int opus_packet_parse(const unsigned char *data, opus_int32 len,
732 unsigned char *out_toc, const unsigned char *frames[48],
733 opus_int16 size[48], int *payload_offset)
734 {
735 return opus_packet_parse_impl(data, len, 0, out_toc,
736 frames, size, payload_offset);
737 }
738
739 int opus_decode_native(OpusDecoder *st, const unsigned char *data, 584 int opus_decode_native(OpusDecoder *st, const unsigned char *data,
740 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec, 585 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec,
741 int self_delimited, int *packet_offset, int soft_clip) 586 int self_delimited, opus_int32 *packet_offset, int soft_clip)
742 { 587 {
743 int i, nb_samples; 588 int i, nb_samples;
744 int count, offset; 589 int count, offset;
745 unsigned char toc; 590 unsigned char toc;
746 int tot_offset;
747 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; 591 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels;
748 /* 48 x 2.5 ms = 120 ms */ 592 /* 48 x 2.5 ms = 120 ms */
749 opus_int16 size[48]; 593 opus_int16 size[48];
750 if (decode_fec<0 || decode_fec>1) 594 if (decode_fec<0 || decode_fec>1)
751 return OPUS_BAD_ARG; 595 return OPUS_BAD_ARG;
752 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ 596 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */
753 if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) 597 if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0)
754 return OPUS_BAD_ARG; 598 return OPUS_BAD_ARG;
755 if (len==0 || data==NULL) 599 if (len==0 || data==NULL)
756 { 600 {
(...skipping 11 matching lines...) Expand all
768 st->last_packet_duration = pcm_count; 612 st->last_packet_duration = pcm_count;
769 return pcm_count; 613 return pcm_count;
770 } else if (len<0) 614 } else if (len<0)
771 return OPUS_BAD_ARG; 615 return OPUS_BAD_ARG;
772 616
773 packet_mode = opus_packet_get_mode(data); 617 packet_mode = opus_packet_get_mode(data);
774 packet_bandwidth = opus_packet_get_bandwidth(data); 618 packet_bandwidth = opus_packet_get_bandwidth(data);
775 packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); 619 packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs);
776 packet_stream_channels = opus_packet_get_nb_channels(data); 620 packet_stream_channels = opus_packet_get_nb_channels(data);
777 621
778 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, & offset); 622 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL,
623 size, &offset, packet_offset);
624 if (count<0)
625 return count;
779 626
780 data += offset; 627 data += offset;
781 628
782 if (decode_fec) 629 if (decode_fec)
783 { 630 {
784 int duration_copy; 631 int duration_copy;
785 int ret; 632 int ret;
786 /* If no FEC can be present, run the PLC (recursive call) */ 633 /* If no FEC can be present, run the PLC (recursive call) */
787 if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st- >mode == MODE_CELT_ONLY) 634 if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st- >mode == MODE_CELT_ONLY)
788 return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, sof t_clip); 635 return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, sof t_clip);
(...skipping 18 matching lines...) Expand all
807 packet_frame_size, 1); 654 packet_frame_size, 1);
808 if (ret<0) 655 if (ret<0)
809 return ret; 656 return ret;
810 else { 657 else {
811 if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) 658 if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels))
812 OPUS_PRINT_INT(frame_size); 659 OPUS_PRINT_INT(frame_size);
813 st->last_packet_duration = frame_size; 660 st->last_packet_duration = frame_size;
814 return frame_size; 661 return frame_size;
815 } 662 }
816 } 663 }
817 tot_offset = 0;
818 if (count < 0)
819 return count;
820
821 tot_offset += offset;
822 664
823 if (count*packet_frame_size > frame_size) 665 if (count*packet_frame_size > frame_size)
824 return OPUS_BUFFER_TOO_SMALL; 666 return OPUS_BUFFER_TOO_SMALL;
825 667
826 /* Update the state as the last step to avoid updating it on an invalid packe t */ 668 /* Update the state as the last step to avoid updating it on an invalid packe t */
827 st->mode = packet_mode; 669 st->mode = packet_mode;
828 st->bandwidth = packet_bandwidth; 670 st->bandwidth = packet_bandwidth;
829 st->frame_size = packet_frame_size; 671 st->frame_size = packet_frame_size;
830 st->stream_channels = packet_stream_channels; 672 st->stream_channels = packet_stream_channels;
831 673
832 nb_samples=0; 674 nb_samples=0;
833 for (i=0;i<count;i++) 675 for (i=0;i<count;i++)
834 { 676 {
835 int ret; 677 int ret;
836 ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, fr ame_size-nb_samples, 0); 678 ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, fr ame_size-nb_samples, 0);
837 if (ret<0) 679 if (ret<0)
838 return ret; 680 return ret;
839 celt_assert(ret==packet_frame_size); 681 celt_assert(ret==packet_frame_size);
840 data += size[i]; 682 data += size[i];
841 tot_offset += size[i];
842 nb_samples += ret; 683 nb_samples += ret;
843 } 684 }
844 if (packet_offset != NULL)
845 *packet_offset = tot_offset;
846 st->last_packet_duration = nb_samples; 685 st->last_packet_duration = nb_samples;
847 if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) 686 if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels))
848 OPUS_PRINT_INT(nb_samples); 687 OPUS_PRINT_INT(nb_samples);
849 #ifndef FIXED_POINT 688 #ifndef FIXED_POINT
850 if (soft_clip) 689 if (soft_clip)
851 opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem); 690 opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem);
852 else 691 else
853 st->softclip_mem[0]=st->softclip_mem[1]=0; 692 st->softclip_mem[0]=st->softclip_mem[1]=0;
854 #endif 693 #endif
855 return nb_samples; 694 return nb_samples;
856 } 695 }
857 696
858 #ifdef FIXED_POINT 697 #ifdef FIXED_POINT
859 698
860 int opus_decode(OpusDecoder *st, const unsigned char *data, 699 int opus_decode(OpusDecoder *st, const unsigned char *data,
861 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) 700 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
862 { 701 {
702 if(frame_size<=0)
703 return OPUS_BAD_ARG;
863 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL , 0); 704 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL , 0);
864 } 705 }
865 706
866 #ifndef DISABLE_FLOAT_API 707 #ifndef DISABLE_FLOAT_API
867 int opus_decode_float(OpusDecoder *st, const unsigned char *data, 708 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
868 opus_int32 len, float *pcm, int frame_size, int decode_fec) 709 opus_int32 len, float *pcm, int frame_size, int decode_fec)
869 { 710 {
870 VARDECL(opus_int16, out); 711 VARDECL(opus_int16, out);
871 int ret, i; 712 int ret, i;
872 ALLOC_STACK; 713 ALLOC_STACK;
873 714
715 if(frame_size<=0)
716 {
717 RESTORE_STACK;
718 return OPUS_BAD_ARG;
719 }
874 ALLOC(out, frame_size*st->channels, opus_int16); 720 ALLOC(out, frame_size*st->channels, opus_int16);
875 721
876 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0); 722 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0);
877 if (ret > 0) 723 if (ret > 0)
878 { 724 {
879 for (i=0;i<ret*st->channels;i++) 725 for (i=0;i<ret*st->channels;i++)
880 pcm[i] = (1.f/32768.f)*(out[i]); 726 pcm[i] = (1.f/32768.f)*(out[i]);
881 } 727 }
882 RESTORE_STACK; 728 RESTORE_STACK;
883 return ret; 729 return ret;
884 } 730 }
885 #endif 731 #endif
886 732
887 733
888 #else 734 #else
889 int opus_decode(OpusDecoder *st, const unsigned char *data, 735 int opus_decode(OpusDecoder *st, const unsigned char *data,
890 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) 736 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
891 { 737 {
892 VARDECL(float, out); 738 VARDECL(float, out);
893 int ret, i; 739 int ret, i;
894 ALLOC_STACK; 740 ALLOC_STACK;
895 741
896 if(frame_size<0) 742 if(frame_size<=0)
897 { 743 {
898 RESTORE_STACK; 744 RESTORE_STACK;
899 return OPUS_BAD_ARG; 745 return OPUS_BAD_ARG;
900 } 746 }
901 747
902 ALLOC(out, frame_size*st->channels, float); 748 ALLOC(out, frame_size*st->channels, float);
903 749
904 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1); 750 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1);
905 if (ret > 0) 751 if (ret > 0)
906 { 752 {
907 for (i=0;i<ret*st->channels;i++) 753 for (i=0;i<ret*st->channels;i++)
908 pcm[i] = FLOAT2INT16(out[i]); 754 pcm[i] = FLOAT2INT16(out[i]);
909 } 755 }
910 RESTORE_STACK; 756 RESTORE_STACK;
911 return ret; 757 return ret;
912 } 758 }
913 759
914 int opus_decode_float(OpusDecoder *st, const unsigned char *data, 760 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
915 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) 761 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
916 { 762 {
763 if(frame_size<=0)
764 return OPUS_BAD_ARG;
917 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL , 0); 765 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL , 0);
918 } 766 }
919 767
920 #endif 768 #endif
921 769
922 int opus_decoder_ctl(OpusDecoder *st, int request, ...) 770 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
923 { 771 {
924 int ret = OPUS_OK; 772 int ret = OPUS_OK;
925 va_list ap; 773 va_list ap;
926 void *silk_dec; 774 void *silk_dec;
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1113 return OPUS_INVALID_PACKET; 961 return OPUS_INVALID_PACKET;
1114 else 962 else
1115 return samples; 963 return samples;
1116 } 964 }
1117 965
1118 int opus_decoder_get_nb_samples(const OpusDecoder *dec, 966 int opus_decoder_get_nb_samples(const OpusDecoder *dec,
1119 const unsigned char packet[], opus_int32 len) 967 const unsigned char packet[], opus_int32 len)
1120 { 968 {
1121 return opus_packet_get_nb_samples(packet, len, dec->Fs); 969 return opus_packet_get_nb_samples(packet, len, dec->Fs);
1122 } 970 }
OLDNEW
« no previous file with comments | « src/opus.c ('k') | src/opus_demo.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698