OLD | NEW |
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 |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include "entdec.h" | 39 #include "entdec.h" |
40 #include "modes.h" | 40 #include "modes.h" |
41 #include "API.h" | 41 #include "API.h" |
42 #include "stack_alloc.h" | 42 #include "stack_alloc.h" |
43 #include "float_cast.h" | 43 #include "float_cast.h" |
44 #include "opus_private.h" | 44 #include "opus_private.h" |
45 #include "os_support.h" | 45 #include "os_support.h" |
46 #include "structs.h" | 46 #include "structs.h" |
47 #include "define.h" | 47 #include "define.h" |
48 #include "mathops.h" | 48 #include "mathops.h" |
| 49 #include "cpu_support.h" |
49 | 50 |
50 struct OpusDecoder { | 51 struct OpusDecoder { |
51 int celt_dec_offset; | 52 int celt_dec_offset; |
52 int silk_dec_offset; | 53 int silk_dec_offset; |
53 int channels; | 54 int channels; |
54 opus_int32 Fs; /** Sampling rate (at the API level) */ | 55 opus_int32 Fs; /** Sampling rate (at the API level) */ |
55 silk_DecControlStruct DecControl; | 56 silk_DecControlStruct DecControl; |
56 int decode_gain; | 57 int decode_gain; |
57 | 58 |
58 /* Everything beyond this point gets cleared on a reset */ | 59 /* Everything beyond this point gets cleared on a reset */ |
59 #define OPUS_DECODER_RESET_START stream_channels | 60 #define OPUS_DECODER_RESET_START stream_channels |
60 int stream_channels; | 61 int stream_channels; |
61 | 62 |
62 int bandwidth; | 63 int bandwidth; |
63 int mode; | 64 int mode; |
64 int prev_mode; | 65 int prev_mode; |
65 int frame_size; | 66 int frame_size; |
66 int prev_redundancy; | 67 int prev_redundancy; |
67 int last_packet_duration; | 68 int last_packet_duration; |
| 69 #ifndef FIXED_POINT |
| 70 opus_val16 softclip_mem[2]; |
| 71 #endif |
68 | 72 |
69 opus_uint32 rangeFinal; | 73 opus_uint32 rangeFinal; |
| 74 int arch; |
70 }; | 75 }; |
71 | 76 |
72 #ifdef FIXED_POINT | 77 #ifdef FIXED_POINT |
73 static inline opus_int16 SAT16(opus_int32 x) { | 78 static inline opus_int16 SAT16(opus_int32 x) { |
74 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x; | 79 return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x; |
75 } | 80 } |
76 #endif | 81 #endif |
77 | 82 |
78 | 83 |
79 int opus_decoder_get_size(int channels) | 84 int opus_decoder_get_size(int channels) |
(...skipping 29 matching lines...) Expand all Loading... |
109 silkDecSizeBytes = align(silkDecSizeBytes); | 114 silkDecSizeBytes = align(silkDecSizeBytes); |
110 st->silk_dec_offset = align(sizeof(OpusDecoder)); | 115 st->silk_dec_offset = align(sizeof(OpusDecoder)); |
111 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; | 116 st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; |
112 silk_dec = (char*)st+st->silk_dec_offset; | 117 silk_dec = (char*)st+st->silk_dec_offset; |
113 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); | 118 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
114 st->stream_channels = st->channels = channels; | 119 st->stream_channels = st->channels = channels; |
115 | 120 |
116 st->Fs = Fs; | 121 st->Fs = Fs; |
117 st->DecControl.API_sampleRate = st->Fs; | 122 st->DecControl.API_sampleRate = st->Fs; |
118 st->DecControl.nChannelsAPI = st->channels; | 123 st->DecControl.nChannelsAPI = st->channels; |
| 124 st->arch = opus_select_arch(); |
119 | 125 |
120 /* Reset decoder */ | 126 /* Reset decoder */ |
121 ret = silk_InitDecoder( silk_dec ); | 127 ret = silk_InitDecoder( silk_dec ); |
122 if(ret)return OPUS_INTERNAL_ERROR; | 128 if(ret)return OPUS_INTERNAL_ERROR; |
123 | 129 |
124 /* Initialize CELT decoder */ | 130 /* Initialize CELT decoder */ |
125 ret = celt_decoder_init(celt_dec, Fs, channels); | 131 ret = celt_decoder_init(celt_dec, Fs, channels); |
126 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; | 132 if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; |
127 | 133 |
128 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); | 134 celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 200 } |
195 | 201 |
196 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, | 202 static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, |
197 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) | 203 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
198 { | 204 { |
199 void *silk_dec; | 205 void *silk_dec; |
200 CELTDecoder *celt_dec; | 206 CELTDecoder *celt_dec; |
201 int i, silk_ret=0, celt_ret=0; | 207 int i, silk_ret=0, celt_ret=0; |
202 ec_dec dec; | 208 ec_dec dec; |
203 opus_int32 silk_frame_size; | 209 opus_int32 silk_frame_size; |
| 210 int pcm_silk_size; |
204 VARDECL(opus_int16, pcm_silk); | 211 VARDECL(opus_int16, pcm_silk); |
205 VARDECL(opus_val16, pcm_transition); | 212 int pcm_transition_silk_size; |
| 213 VARDECL(opus_val16, pcm_transition_silk); |
| 214 int pcm_transition_celt_size; |
| 215 VARDECL(opus_val16, pcm_transition_celt); |
| 216 opus_val16 *pcm_transition; |
| 217 int redundant_audio_size; |
206 VARDECL(opus_val16, redundant_audio); | 218 VARDECL(opus_val16, redundant_audio); |
207 | 219 |
208 int audiosize; | 220 int audiosize; |
209 int mode; | 221 int mode; |
210 int transition=0; | 222 int transition=0; |
211 int start_band; | 223 int start_band; |
212 int redundancy=0; | 224 int redundancy=0; |
213 int redundancy_bytes = 0; | 225 int redundancy_bytes = 0; |
214 int celt_to_silk=0; | 226 int celt_to_silk=0; |
215 int c; | 227 int c; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 return audiosize; | 267 return audiosize; |
256 } else { | 268 } else { |
257 mode = st->prev_mode; | 269 mode = st->prev_mode; |
258 } | 270 } |
259 } | 271 } |
260 | 272 |
261 /* For CELT/hybrid PLC of more than 20 ms, opus_decode_native() will do | 273 /* For CELT/hybrid PLC of more than 20 ms, opus_decode_native() will do |
262 multiple calls */ | 274 multiple calls */ |
263 if (data==NULL && mode != MODE_SILK_ONLY) | 275 if (data==NULL && mode != MODE_SILK_ONLY) |
264 frame_size = IMIN(frame_size, F20); | 276 frame_size = IMIN(frame_size, F20); |
265 ALLOC(pcm_transition, F5*st->channels, opus_val16); | |
266 | 277 |
| 278 pcm_transition_silk_size = 0; |
| 279 pcm_transition_celt_size = 0; |
267 if (data!=NULL && st->prev_mode > 0 && ( | 280 if (data!=NULL && st->prev_mode > 0 && ( |
268 (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_r
edundancy) | 281 (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_r
edundancy) |
269 || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) | 282 || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) |
270 ) | 283 ) |
271 { | 284 { |
272 transition = 1; | 285 transition = 1; |
| 286 /* Decide where to allocate the stack memory for pcm_transition */ |
273 if (mode == MODE_CELT_ONLY) | 287 if (mode == MODE_CELT_ONLY) |
274 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); | 288 pcm_transition_celt_size = F5*st->channels; |
| 289 else |
| 290 pcm_transition_silk_size = F5*st->channels; |
| 291 } |
| 292 ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16); |
| 293 if (transition && mode == MODE_CELT_ONLY) |
| 294 { |
| 295 pcm_transition = pcm_transition_celt; |
| 296 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); |
275 } | 297 } |
276 if (audiosize > frame_size) | 298 if (audiosize > frame_size) |
277 { | 299 { |
278 /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosiz
e, frame_size, mode);*/ | 300 /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosiz
e, frame_size, mode);*/ |
279 RESTORE_STACK; | 301 RESTORE_STACK; |
280 return OPUS_BAD_ARG; | 302 return OPUS_BAD_ARG; |
281 } else { | 303 } else { |
282 frame_size = audiosize; | 304 frame_size = audiosize; |
283 } | 305 } |
284 | 306 |
285 ALLOC(pcm_silk, IMAX(F10, frame_size)*st->channels, opus_int16); | 307 /* Don't allocate any memory when in CELT-only mode */ |
286 ALLOC(redundant_audio, F5*st->channels, opus_val16); | 308 pcm_silk_size = (mode != MODE_CELT_ONLY) ? IMAX(F10, frame_size)*st->channels
: 0; |
| 309 ALLOC(pcm_silk, pcm_silk_size, opus_int16); |
287 | 310 |
288 /* SILK processing */ | 311 /* SILK processing */ |
289 if (mode != MODE_CELT_ONLY) | 312 if (mode != MODE_CELT_ONLY) |
290 { | 313 { |
291 int lost_flag, decoded_samples; | 314 int lost_flag, decoded_samples; |
292 opus_int16 *pcm_ptr = pcm_silk; | 315 opus_int16 *pcm_ptr = pcm_silk; |
293 | 316 |
294 if (st->prev_mode==MODE_CELT_ONLY) | 317 if (st->prev_mode==MODE_CELT_ONLY) |
295 silk_InitDecoder( silk_dec ); | 318 silk_InitDecoder( silk_dec ); |
296 | 319 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 break; | 413 break; |
391 case OPUS_BANDWIDTH_FULLBAND: | 414 case OPUS_BANDWIDTH_FULLBAND: |
392 endband = 21; | 415 endband = 21; |
393 break; | 416 break; |
394 } | 417 } |
395 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)); | 418 celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)); |
396 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)); | 419 celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)); |
397 } | 420 } |
398 | 421 |
399 if (redundancy) | 422 if (redundancy) |
| 423 { |
400 transition = 0; | 424 transition = 0; |
| 425 pcm_transition_silk_size=0; |
| 426 } |
| 427 |
| 428 ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16); |
401 | 429 |
402 if (transition && mode != MODE_CELT_ONLY) | 430 if (transition && mode != MODE_CELT_ONLY) |
| 431 { |
| 432 pcm_transition = pcm_transition_silk; |
403 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); | 433 opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); |
| 434 } |
| 435 |
| 436 /* Only allocation memory for redundancy if/when needed */ |
| 437 redundant_audio_size = redundancy ? F5*st->channels : 0; |
| 438 ALLOC(redundant_audio, redundant_audio_size, opus_val16); |
404 | 439 |
405 /* 5 ms redundant frame for CELT->SILK*/ | 440 /* 5 ms redundant frame for CELT->SILK*/ |
406 if (redundancy && celt_to_silk) | 441 if (redundancy && celt_to_silk) |
407 { | 442 { |
408 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); | 443 celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); |
409 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, | 444 celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, |
410 redundant_audio, F5, NULL); | 445 redundant_audio, F5, NULL); |
411 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); | 446 celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); |
412 } | 447 } |
413 | 448 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 } | 542 } |
508 } | 543 } |
509 | 544 |
510 if (len <= 1) | 545 if (len <= 1) |
511 st->rangeFinal = 0; | 546 st->rangeFinal = 0; |
512 else | 547 else |
513 st->rangeFinal = dec.rng ^ redundant_rng; | 548 st->rangeFinal = dec.rng ^ redundant_rng; |
514 | 549 |
515 st->prev_mode = mode; | 550 st->prev_mode = mode; |
516 st->prev_redundancy = redundancy && !celt_to_silk; | 551 st->prev_redundancy = redundancy && !celt_to_silk; |
| 552 |
| 553 if (celt_ret>=0) |
| 554 { |
| 555 if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) |
| 556 OPUS_PRINT_INT(audiosize); |
| 557 } |
| 558 |
517 RESTORE_STACK; | 559 RESTORE_STACK; |
518 return celt_ret < 0 ? celt_ret : audiosize; | 560 return celt_ret < 0 ? celt_ret : audiosize; |
519 | 561 |
520 } | 562 } |
521 | 563 |
522 static int parse_size(const unsigned char *data, opus_int32 len, short *size) | 564 static int parse_size(const unsigned char *data, opus_int32 len, opus_int16 *siz
e) |
523 { | 565 { |
524 if (len<1) | 566 if (len<1) |
525 { | 567 { |
526 *size = -1; | 568 *size = -1; |
527 return -1; | 569 return -1; |
528 } else if (data[0]<252) | 570 } else if (data[0]<252) |
529 { | 571 { |
530 *size = data[0]; | 572 *size = data[0]; |
531 return 1; | 573 return 1; |
532 } else if (len<2) | 574 } else if (len<2) |
533 { | 575 { |
534 *size = -1; | 576 *size = -1; |
535 return -1; | 577 return -1; |
536 } else { | 578 } else { |
537 *size = 4*data[1] + data[0]; | 579 *size = 4*data[1] + data[0]; |
538 return 2; | 580 return 2; |
539 } | 581 } |
540 } | 582 } |
541 | 583 |
542 static int opus_packet_parse_impl(const unsigned char *data, opus_int32 len, | 584 static int opus_packet_parse_impl(const unsigned char *data, opus_int32 len, |
543 int self_delimited, unsigned char *out_toc, | 585 int self_delimited, unsigned char *out_toc, |
544 const unsigned char *frames[48], short size[48], int *payload_offset) | 586 const unsigned char *frames[48], opus_int16 size[48], int *payload_offset) |
545 { | 587 { |
546 int i, bytes; | 588 int i, bytes; |
547 int count; | 589 int count; |
548 int cbr; | 590 int cbr; |
549 unsigned char ch, toc; | 591 unsigned char ch, toc; |
550 int framesize; | 592 int framesize; |
551 opus_int32 last_size; | 593 opus_int32 last_size; |
552 const unsigned char *data0 = data; | 594 const unsigned char *data0 = data; |
553 | 595 |
554 if (size==NULL) | 596 if (size==NULL) |
(...skipping 14 matching lines...) Expand all Loading... |
569 /* Two CBR frames */ | 611 /* Two CBR frames */ |
570 case 1: | 612 case 1: |
571 count=2; | 613 count=2; |
572 cbr = 1; | 614 cbr = 1; |
573 if (!self_delimited) | 615 if (!self_delimited) |
574 { | 616 { |
575 if (len&0x1) | 617 if (len&0x1) |
576 return OPUS_INVALID_PACKET; | 618 return OPUS_INVALID_PACKET; |
577 last_size = len/2; | 619 last_size = len/2; |
578 /* If last_size doesn't fit in size[0], we'll catch it later */ | 620 /* If last_size doesn't fit in size[0], we'll catch it later */ |
579 size[0] = (short)last_size; | 621 size[0] = (opus_int16)last_size; |
580 } | 622 } |
581 break; | 623 break; |
582 /* Two VBR frames */ | 624 /* Two VBR frames */ |
583 case 2: | 625 case 2: |
584 count = 2; | 626 count = 2; |
585 bytes = parse_size(data, len, size); | 627 bytes = parse_size(data, len, size); |
586 len -= bytes; | 628 len -= bytes; |
587 if (size[0]<0 || size[0] > len) | 629 if (size[0]<0 || size[0] > len) |
588 return OPUS_INVALID_PACKET; | 630 return OPUS_INVALID_PACKET; |
589 data += bytes; | 631 data += bytes; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 } | 672 } |
631 if (last_size<0) | 673 if (last_size<0) |
632 return OPUS_INVALID_PACKET; | 674 return OPUS_INVALID_PACKET; |
633 } else if (!self_delimited) | 675 } else if (!self_delimited) |
634 { | 676 { |
635 /* CBR case */ | 677 /* CBR case */ |
636 last_size = len/count; | 678 last_size = len/count; |
637 if (last_size*count!=len) | 679 if (last_size*count!=len) |
638 return OPUS_INVALID_PACKET; | 680 return OPUS_INVALID_PACKET; |
639 for (i=0;i<count-1;i++) | 681 for (i=0;i<count-1;i++) |
640 size[i] = (short)last_size; | 682 size[i] = (opus_int16)last_size; |
641 } | 683 } |
642 break; | 684 break; |
643 } | 685 } |
644 /* Self-delimited framing has an extra size for the last frame. */ | 686 /* Self-delimited framing has an extra size for the last frame. */ |
645 if (self_delimited) | 687 if (self_delimited) |
646 { | 688 { |
647 bytes = parse_size(data, len, size+count-1); | 689 bytes = parse_size(data, len, size+count-1); |
648 len -= bytes; | 690 len -= bytes; |
649 if (size[count-1]<0 || size[count-1] > len) | 691 if (size[count-1]<0 || size[count-1] > len) |
650 return OPUS_INVALID_PACKET; | 692 return OPUS_INVALID_PACKET; |
651 data += bytes; | 693 data += bytes; |
652 /* For CBR packets, apply the size to all the frames. */ | 694 /* For CBR packets, apply the size to all the frames. */ |
653 if (cbr) | 695 if (cbr) |
654 { | 696 { |
655 if (size[count-1]*count > len) | 697 if (size[count-1]*count > len) |
656 return OPUS_INVALID_PACKET; | 698 return OPUS_INVALID_PACKET; |
657 for (i=0;i<count-1;i++) | 699 for (i=0;i<count-1;i++) |
658 size[i] = size[count-1]; | 700 size[i] = size[count-1]; |
659 } else if(size[count-1] > last_size) | 701 } else if(size[count-1] > last_size) |
660 return OPUS_INVALID_PACKET; | 702 return OPUS_INVALID_PACKET; |
661 } else | 703 } else |
662 { | 704 { |
663 /* Because it's not encoded explicitly, it's possible the size of the | 705 /* Because it's not encoded explicitly, it's possible the size of the |
664 last packet (or all the packets, for the CBR case) is larger than | 706 last packet (or all the packets, for the CBR case) is larger than |
665 1275. Reject them here.*/ | 707 1275. Reject them here.*/ |
666 if (last_size > 1275) | 708 if (last_size > 1275) |
667 return OPUS_INVALID_PACKET; | 709 return OPUS_INVALID_PACKET; |
668 size[count-1] = (short)last_size; | 710 size[count-1] = (opus_int16)last_size; |
669 } | 711 } |
670 | 712 |
671 if (frames) | 713 if (frames) |
672 { | 714 { |
673 for (i=0;i<count;i++) | 715 for (i=0;i<count;i++) |
674 { | 716 { |
675 frames[i] = data; | 717 frames[i] = data; |
676 data += size[i]; | 718 data += size[i]; |
677 } | 719 } |
678 } | 720 } |
679 | 721 |
680 if (out_toc) | 722 if (out_toc) |
681 *out_toc = toc; | 723 *out_toc = toc; |
682 | 724 |
683 if (payload_offset) | 725 if (payload_offset) |
684 *payload_offset = data-data0; | 726 *payload_offset = (int)(data-data0); |
685 | 727 |
686 return count; | 728 return count; |
687 } | 729 } |
688 | 730 |
689 int opus_packet_parse(const unsigned char *data, opus_int32 len, | 731 int opus_packet_parse(const unsigned char *data, opus_int32 len, |
690 unsigned char *out_toc, const unsigned char *frames[48], | 732 unsigned char *out_toc, const unsigned char *frames[48], |
691 short size[48], int *payload_offset) | 733 opus_int16 size[48], int *payload_offset) |
692 { | 734 { |
693 return opus_packet_parse_impl(data, len, 0, out_toc, | 735 return opus_packet_parse_impl(data, len, 0, out_toc, |
694 frames, size, payload_offset); | 736 frames, size, payload_offset); |
695 } | 737 } |
696 | 738 |
697 int opus_decode_native(OpusDecoder *st, const unsigned char *data, | 739 int opus_decode_native(OpusDecoder *st, const unsigned char *data, |
698 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec, | 740 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec, |
699 int self_delimited, int *packet_offset) | 741 int self_delimited, int *packet_offset, int soft_clip) |
700 { | 742 { |
701 int i, nb_samples; | 743 int i, nb_samples; |
702 int count, offset; | 744 int count, offset; |
703 unsigned char toc; | 745 unsigned char toc; |
704 int tot_offset; | 746 int tot_offset; |
705 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; | 747 int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; |
706 /* 48 x 2.5 ms = 120 ms */ | 748 /* 48 x 2.5 ms = 120 ms */ |
707 short size[48]; | 749 opus_int16 size[48]; |
708 if (decode_fec<0 || decode_fec>1) | 750 if (decode_fec<0 || decode_fec>1) |
709 return OPUS_BAD_ARG; | 751 return OPUS_BAD_ARG; |
710 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ | 752 /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ |
711 if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) | 753 if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) |
712 return OPUS_BAD_ARG; | 754 return OPUS_BAD_ARG; |
713 if (len==0 || data==NULL) | 755 if (len==0 || data==NULL) |
714 { | 756 { |
715 int pcm_count=0; | 757 int pcm_count=0; |
716 do { | 758 do { |
717 int ret; | 759 int ret; |
718 ret = opus_decode_frame(st, NULL, 0, pcm, frame_size-pcm_count, 0); | 760 ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_
size-pcm_count, 0); |
719 if (ret<0) | 761 if (ret<0) |
720 return ret; | 762 return ret; |
721 pcm += st->channels*ret; | |
722 pcm_count += ret; | 763 pcm_count += ret; |
723 } while (pcm_count < frame_size); | 764 } while (pcm_count < frame_size); |
| 765 celt_assert(pcm_count == frame_size); |
| 766 if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels)) |
| 767 OPUS_PRINT_INT(pcm_count); |
724 st->last_packet_duration = pcm_count; | 768 st->last_packet_duration = pcm_count; |
725 return pcm_count; | 769 return pcm_count; |
726 } else if (len<0) | 770 } else if (len<0) |
727 return OPUS_BAD_ARG; | 771 return OPUS_BAD_ARG; |
728 | 772 |
729 packet_mode = opus_packet_get_mode(data); | 773 packet_mode = opus_packet_get_mode(data); |
730 packet_bandwidth = opus_packet_get_bandwidth(data); | 774 packet_bandwidth = opus_packet_get_bandwidth(data); |
731 packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); | 775 packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); |
732 packet_stream_channels = opus_packet_get_nb_channels(data); | 776 packet_stream_channels = opus_packet_get_nb_channels(data); |
733 | 777 |
734 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &
offset); | 778 count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, size, &
offset); |
735 | 779 |
736 data += offset; | 780 data += offset; |
737 | 781 |
738 if (decode_fec) | 782 if (decode_fec) |
739 { | 783 { |
740 int duration_copy; | 784 int duration_copy; |
741 int ret; | 785 int ret; |
742 /* If no FEC can be present, run the PLC (recursive call) */ | 786 /* If no FEC can be present, run the PLC (recursive call) */ |
743 if (frame_size <= packet_frame_size || packet_mode == MODE_CELT_ONLY || st
->mode == MODE_CELT_ONLY) | 787 if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st-
>mode == MODE_CELT_ONLY) |
744 return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL); | 788 return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, sof
t_clip); |
745 /* Otherwise, run the PLC on everything except the size for which we might
have FEC */ | 789 /* Otherwise, run the PLC on everything except the size for which we might
have FEC */ |
746 duration_copy = st->last_packet_duration; | 790 duration_copy = st->last_packet_duration; |
747 ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0
, 0, NULL); | 791 if (frame_size-packet_frame_size!=0) |
748 if (ret<0) | |
749 { | 792 { |
750 st->last_packet_duration = duration_copy; | 793 ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size
, 0, 0, NULL, soft_clip); |
751 return ret; | 794 if (ret<0) |
| 795 { |
| 796 st->last_packet_duration = duration_copy; |
| 797 return ret; |
| 798 } |
| 799 celt_assert(ret==frame_size-packet_frame_size); |
752 } | 800 } |
753 /* Complete with FEC */ | 801 /* Complete with FEC */ |
754 st->mode = packet_mode; | 802 st->mode = packet_mode; |
755 st->bandwidth = packet_bandwidth; | 803 st->bandwidth = packet_bandwidth; |
756 st->frame_size = packet_frame_size; | 804 st->frame_size = packet_frame_size; |
757 st->stream_channels = packet_stream_channels; | 805 st->stream_channels = packet_stream_channels; |
758 ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-pa
cket_frame_size), | 806 ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-pa
cket_frame_size), |
759 packet_frame_size, 1); | 807 packet_frame_size, 1); |
760 if (ret<0) | 808 if (ret<0) |
761 return ret; | 809 return ret; |
762 st->last_packet_duration = frame_size; | 810 else { |
763 return frame_size; | 811 if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) |
| 812 OPUS_PRINT_INT(frame_size); |
| 813 st->last_packet_duration = frame_size; |
| 814 return frame_size; |
| 815 } |
764 } | 816 } |
765 tot_offset = 0; | 817 tot_offset = 0; |
766 if (count < 0) | 818 if (count < 0) |
767 return count; | 819 return count; |
768 | 820 |
769 tot_offset += offset; | 821 tot_offset += offset; |
770 | 822 |
771 if (count*packet_frame_size > frame_size) | 823 if (count*packet_frame_size > frame_size) |
772 return OPUS_BUFFER_TOO_SMALL; | 824 return OPUS_BUFFER_TOO_SMALL; |
773 | 825 |
774 /* Update the state as the last step to avoid updating it on an invalid packe
t */ | 826 /* Update the state as the last step to avoid updating it on an invalid packe
t */ |
775 st->mode = packet_mode; | 827 st->mode = packet_mode; |
776 st->bandwidth = packet_bandwidth; | 828 st->bandwidth = packet_bandwidth; |
777 st->frame_size = packet_frame_size; | 829 st->frame_size = packet_frame_size; |
778 st->stream_channels = packet_stream_channels; | 830 st->stream_channels = packet_stream_channels; |
779 | 831 |
780 nb_samples=0; | 832 nb_samples=0; |
781 for (i=0;i<count;i++) | 833 for (i=0;i<count;i++) |
782 { | 834 { |
783 int ret; | 835 int ret; |
784 ret = opus_decode_frame(st, data, size[i], pcm, frame_size-nb_samples, dec
ode_fec); | 836 ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, fr
ame_size-nb_samples, 0); |
785 if (ret<0) | 837 if (ret<0) |
786 return ret; | 838 return ret; |
| 839 celt_assert(ret==packet_frame_size); |
787 data += size[i]; | 840 data += size[i]; |
788 tot_offset += size[i]; | 841 tot_offset += size[i]; |
789 pcm += ret*st->channels; | |
790 nb_samples += ret; | 842 nb_samples += ret; |
791 } | 843 } |
792 if (packet_offset != NULL) | 844 if (packet_offset != NULL) |
793 *packet_offset = tot_offset; | 845 *packet_offset = tot_offset; |
794 st->last_packet_duration = nb_samples; | 846 st->last_packet_duration = nb_samples; |
| 847 if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) |
| 848 OPUS_PRINT_INT(nb_samples); |
| 849 #ifndef FIXED_POINT |
| 850 if (soft_clip) |
| 851 opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem); |
| 852 else |
| 853 st->softclip_mem[0]=st->softclip_mem[1]=0; |
| 854 #endif |
795 return nb_samples; | 855 return nb_samples; |
796 } | 856 } |
797 | 857 |
798 #ifdef FIXED_POINT | 858 #ifdef FIXED_POINT |
799 | 859 |
800 int opus_decode(OpusDecoder *st, const unsigned char *data, | 860 int opus_decode(OpusDecoder *st, const unsigned char *data, |
801 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) | 861 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
802 { | 862 { |
803 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL
); | 863 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL
, 0); |
804 } | 864 } |
805 | 865 |
806 #ifndef DISABLE_FLOAT_API | 866 #ifndef DISABLE_FLOAT_API |
807 int opus_decode_float(OpusDecoder *st, const unsigned char *data, | 867 int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
808 opus_int32 len, float *pcm, int frame_size, int decode_fec) | 868 opus_int32 len, float *pcm, int frame_size, int decode_fec) |
809 { | 869 { |
810 VARDECL(opus_int16, out); | 870 VARDECL(opus_int16, out); |
811 int ret, i; | 871 int ret, i; |
812 ALLOC_STACK; | 872 ALLOC_STACK; |
813 | 873 |
814 ALLOC(out, frame_size*st->channels, opus_int16); | 874 ALLOC(out, frame_size*st->channels, opus_int16); |
815 | 875 |
816 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL)
; | 876 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL,
0); |
817 if (ret > 0) | 877 if (ret > 0) |
818 { | 878 { |
819 for (i=0;i<ret*st->channels;i++) | 879 for (i=0;i<ret*st->channels;i++) |
820 pcm[i] = (1.f/32768.f)*(out[i]); | 880 pcm[i] = (1.f/32768.f)*(out[i]); |
821 } | 881 } |
822 RESTORE_STACK; | 882 RESTORE_STACK; |
823 return ret; | 883 return ret; |
824 } | 884 } |
825 #endif | 885 #endif |
826 | 886 |
827 | 887 |
828 #else | 888 #else |
829 int opus_decode(OpusDecoder *st, const unsigned char *data, | 889 int opus_decode(OpusDecoder *st, const unsigned char *data, |
830 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) | 890 opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) |
831 { | 891 { |
832 VARDECL(float, out); | 892 VARDECL(float, out); |
833 int ret, i; | 893 int ret, i; |
834 ALLOC_STACK; | 894 ALLOC_STACK; |
835 | 895 |
836 if(frame_size<0) | 896 if(frame_size<0) |
837 { | 897 { |
838 RESTORE_STACK; | 898 RESTORE_STACK; |
839 return OPUS_BAD_ARG; | 899 return OPUS_BAD_ARG; |
840 } | 900 } |
841 | 901 |
842 ALLOC(out, frame_size*st->channels, float); | 902 ALLOC(out, frame_size*st->channels, float); |
843 | 903 |
844 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL)
; | 904 ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL,
1); |
845 if (ret > 0) | 905 if (ret > 0) |
846 { | 906 { |
847 for (i=0;i<ret*st->channels;i++) | 907 for (i=0;i<ret*st->channels;i++) |
848 pcm[i] = FLOAT2INT16(out[i]); | 908 pcm[i] = FLOAT2INT16(out[i]); |
849 } | 909 } |
850 RESTORE_STACK; | 910 RESTORE_STACK; |
851 return ret; | 911 return ret; |
852 } | 912 } |
853 | 913 |
854 int opus_decode_float(OpusDecoder *st, const unsigned char *data, | 914 int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
855 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) | 915 opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
856 { | 916 { |
857 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL
); | 917 return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL
, 0); |
858 } | 918 } |
859 | 919 |
860 #endif | 920 #endif |
861 | 921 |
862 int opus_decoder_ctl(OpusDecoder *st, int request, ...) | 922 int opus_decoder_ctl(OpusDecoder *st, int request, ...) |
863 { | 923 { |
864 int ret = OPUS_OK; | 924 int ret = OPUS_OK; |
865 va_list ap; | 925 va_list ap; |
866 void *silk_dec; | 926 void *silk_dec; |
867 CELTDecoder *celt_dec; | 927 CELTDecoder *celt_dec; |
868 | 928 |
869 silk_dec = (char*)st+st->silk_dec_offset; | 929 silk_dec = (char*)st+st->silk_dec_offset; |
870 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); | 930 celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
871 | 931 |
872 | 932 |
873 va_start(ap, request); | 933 va_start(ap, request); |
874 | 934 |
875 switch (request) | 935 switch (request) |
876 { | 936 { |
877 case OPUS_GET_BANDWIDTH_REQUEST: | 937 case OPUS_GET_BANDWIDTH_REQUEST: |
878 { | 938 { |
879 opus_int32 *value = va_arg(ap, opus_int32*); | 939 opus_int32 *value = va_arg(ap, opus_int32*); |
| 940 if (!value) |
| 941 { |
| 942 goto bad_arg; |
| 943 } |
880 *value = st->bandwidth; | 944 *value = st->bandwidth; |
881 } | 945 } |
882 break; | 946 break; |
883 case OPUS_GET_FINAL_RANGE_REQUEST: | 947 case OPUS_GET_FINAL_RANGE_REQUEST: |
884 { | 948 { |
885 opus_uint32 *value = va_arg(ap, opus_uint32*); | 949 opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 950 if (!value) |
| 951 { |
| 952 goto bad_arg; |
| 953 } |
886 *value = st->rangeFinal; | 954 *value = st->rangeFinal; |
887 } | 955 } |
888 break; | 956 break; |
889 case OPUS_RESET_STATE: | 957 case OPUS_RESET_STATE: |
890 { | 958 { |
891 OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START, | 959 OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START, |
892 sizeof(OpusDecoder)- | 960 sizeof(OpusDecoder)- |
893 ((char*)&st->OPUS_DECODER_RESET_START - (char*)st)); | 961 ((char*)&st->OPUS_DECODER_RESET_START - (char*)st)); |
894 | 962 |
895 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); | 963 celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); |
896 silk_InitDecoder( silk_dec ); | 964 silk_InitDecoder( silk_dec ); |
897 st->stream_channels = st->channels; | 965 st->stream_channels = st->channels; |
898 st->frame_size = st->Fs/400; | 966 st->frame_size = st->Fs/400; |
899 } | 967 } |
900 break; | 968 break; |
901 case OPUS_GET_SAMPLE_RATE_REQUEST: | 969 case OPUS_GET_SAMPLE_RATE_REQUEST: |
902 { | 970 { |
903 opus_int32 *value = va_arg(ap, opus_int32*); | 971 opus_int32 *value = va_arg(ap, opus_int32*); |
904 if (value==NULL) | 972 if (!value) |
905 { | 973 { |
906 ret = OPUS_BAD_ARG; | 974 goto bad_arg; |
907 break; | |
908 } | 975 } |
909 *value = st->Fs; | 976 *value = st->Fs; |
910 } | 977 } |
911 break; | 978 break; |
912 case OPUS_GET_PITCH_REQUEST: | 979 case OPUS_GET_PITCH_REQUEST: |
913 { | 980 { |
914 opus_int32 *value = va_arg(ap, opus_int32*); | 981 opus_int32 *value = va_arg(ap, opus_int32*); |
915 if (value==NULL) | 982 if (!value) |
916 { | 983 { |
917 ret = OPUS_BAD_ARG; | 984 goto bad_arg; |
918 break; | |
919 } | 985 } |
920 if (st->prev_mode == MODE_CELT_ONLY) | 986 if (st->prev_mode == MODE_CELT_ONLY) |
921 celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); | 987 celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); |
922 else | 988 else |
923 *value = st->DecControl.prevPitchLag; | 989 *value = st->DecControl.prevPitchLag; |
924 } | 990 } |
925 break; | 991 break; |
926 case OPUS_GET_GAIN_REQUEST: | 992 case OPUS_GET_GAIN_REQUEST: |
927 { | 993 { |
928 opus_int32 *value = va_arg(ap, opus_int32*); | 994 opus_int32 *value = va_arg(ap, opus_int32*); |
929 if (value==NULL) | 995 if (!value) |
930 { | 996 { |
931 ret = OPUS_BAD_ARG; | 997 goto bad_arg; |
932 break; | |
933 } | 998 } |
934 *value = st->decode_gain; | 999 *value = st->decode_gain; |
935 } | 1000 } |
936 break; | 1001 break; |
937 case OPUS_SET_GAIN_REQUEST: | 1002 case OPUS_SET_GAIN_REQUEST: |
938 { | 1003 { |
939 opus_int32 value = va_arg(ap, opus_int32); | 1004 opus_int32 value = va_arg(ap, opus_int32); |
940 if (value<-32768 || value>32767) | 1005 if (value<-32768 || value>32767) |
941 { | 1006 { |
942 ret = OPUS_BAD_ARG; | 1007 goto bad_arg; |
943 break; | |
944 } | 1008 } |
945 st->decode_gain = value; | 1009 st->decode_gain = value; |
946 } | 1010 } |
947 break; | 1011 break; |
948 case OPUS_GET_LAST_PACKET_DURATION_REQUEST: | 1012 case OPUS_GET_LAST_PACKET_DURATION_REQUEST: |
949 { | 1013 { |
950 opus_uint32 *value = va_arg(ap, opus_uint32*); | 1014 opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 1015 if (!value) |
| 1016 { |
| 1017 goto bad_arg; |
| 1018 } |
951 *value = st->last_packet_duration; | 1019 *value = st->last_packet_duration; |
952 } | 1020 } |
953 break; | 1021 break; |
954 default: | 1022 default: |
955 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ | 1023 /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ |
956 ret = OPUS_UNIMPLEMENTED; | 1024 ret = OPUS_UNIMPLEMENTED; |
957 break; | 1025 break; |
958 } | 1026 } |
959 | 1027 |
960 va_end(ap); | 1028 va_end(ap); |
961 return ret; | 1029 return ret; |
| 1030 bad_arg: |
| 1031 va_end(ap); |
| 1032 return OPUS_BAD_ARG; |
962 } | 1033 } |
963 | 1034 |
964 void opus_decoder_destroy(OpusDecoder *st) | 1035 void opus_decoder_destroy(OpusDecoder *st) |
965 { | 1036 { |
966 opus_free(st); | 1037 opus_free(st); |
967 } | 1038 } |
968 | 1039 |
969 | 1040 |
970 int opus_packet_get_bandwidth(const unsigned char *data) | 1041 int opus_packet_get_bandwidth(const unsigned char *data) |
971 { | 1042 { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 return OPUS_INVALID_PACKET; | 1113 return OPUS_INVALID_PACKET; |
1043 else | 1114 else |
1044 return samples; | 1115 return samples; |
1045 } | 1116 } |
1046 | 1117 |
1047 int opus_decoder_get_nb_samples(const OpusDecoder *dec, | 1118 int opus_decoder_get_nb_samples(const OpusDecoder *dec, |
1048 const unsigned char packet[], opus_int32 len) | 1119 const unsigned char packet[], opus_int32 len) |
1049 { | 1120 { |
1050 return opus_packet_get_nb_samples(packet, len, dec->Fs); | 1121 return opus_packet_get_nb_samples(packet, len, dec->Fs); |
1051 } | 1122 } |
OLD | NEW |