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