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