Alexandre Lision | 744f742 | 2013-09-25 11:39:37 -0400 | [diff] [blame] | 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, opus_int16 *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], opus_int16 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] = (opus_int16)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] = (opus_int16)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] = (opus_int16)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 | opus_int16 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 | opus_int16 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 | if (frame_size-packet_frame_size!=0) |
| 748 | { |
| 749 | ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL); |
| 750 | if (ret<0) |
| 751 | { |
| 752 | st->last_packet_duration = duration_copy; |
| 753 | return ret; |
| 754 | } |
| 755 | celt_assert(ret==frame_size-packet_frame_size); |
| 756 | } |
| 757 | /* Complete with FEC */ |
| 758 | st->mode = packet_mode; |
| 759 | st->bandwidth = packet_bandwidth; |
| 760 | st->frame_size = packet_frame_size; |
| 761 | st->stream_channels = packet_stream_channels; |
| 762 | ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size), |
| 763 | packet_frame_size, 1); |
| 764 | if (ret<0) |
| 765 | return ret; |
| 766 | st->last_packet_duration = frame_size; |
| 767 | return frame_size; |
| 768 | } |
| 769 | tot_offset = 0; |
| 770 | if (count < 0) |
| 771 | return count; |
| 772 | |
| 773 | tot_offset += offset; |
| 774 | |
| 775 | if (count*packet_frame_size > frame_size) |
| 776 | return OPUS_BUFFER_TOO_SMALL; |
| 777 | |
| 778 | /* Update the state as the last step to avoid updating it on an invalid packet */ |
| 779 | st->mode = packet_mode; |
| 780 | st->bandwidth = packet_bandwidth; |
| 781 | st->frame_size = packet_frame_size; |
| 782 | st->stream_channels = packet_stream_channels; |
| 783 | |
| 784 | nb_samples=0; |
| 785 | for (i=0;i<count;i++) |
| 786 | { |
| 787 | int ret; |
| 788 | ret = opus_decode_frame(st, data, size[i], pcm, frame_size-nb_samples, decode_fec); |
| 789 | if (ret<0) |
| 790 | return ret; |
| 791 | data += size[i]; |
| 792 | tot_offset += size[i]; |
| 793 | pcm += ret*st->channels; |
| 794 | nb_samples += ret; |
| 795 | } |
| 796 | if (packet_offset != NULL) |
| 797 | *packet_offset = tot_offset; |
| 798 | st->last_packet_duration = nb_samples; |
| 799 | return nb_samples; |
| 800 | } |
| 801 | |
| 802 | #ifdef FIXED_POINT |
| 803 | |
| 804 | int opus_decode(OpusDecoder *st, const unsigned char *data, |
| 805 | opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
| 806 | { |
| 807 | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL); |
| 808 | } |
| 809 | |
| 810 | #ifndef DISABLE_FLOAT_API |
| 811 | int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
| 812 | opus_int32 len, float *pcm, int frame_size, int decode_fec) |
| 813 | { |
| 814 | VARDECL(opus_int16, out); |
| 815 | int ret, i; |
| 816 | ALLOC_STACK; |
| 817 | |
| 818 | ALLOC(out, frame_size*st->channels, opus_int16); |
| 819 | |
| 820 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL); |
| 821 | if (ret > 0) |
| 822 | { |
| 823 | for (i=0;i<ret*st->channels;i++) |
| 824 | pcm[i] = (1.f/32768.f)*(out[i]); |
| 825 | } |
| 826 | RESTORE_STACK; |
| 827 | return ret; |
| 828 | } |
| 829 | #endif |
| 830 | |
| 831 | |
| 832 | #else |
| 833 | int opus_decode(OpusDecoder *st, const unsigned char *data, |
| 834 | opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) |
| 835 | { |
| 836 | VARDECL(float, out); |
| 837 | int ret, i; |
| 838 | ALLOC_STACK; |
| 839 | |
| 840 | if(frame_size<0) |
| 841 | { |
| 842 | RESTORE_STACK; |
| 843 | return OPUS_BAD_ARG; |
| 844 | } |
| 845 | |
| 846 | ALLOC(out, frame_size*st->channels, float); |
| 847 | |
| 848 | ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL); |
| 849 | if (ret > 0) |
| 850 | { |
| 851 | for (i=0;i<ret*st->channels;i++) |
| 852 | pcm[i] = FLOAT2INT16(out[i]); |
| 853 | } |
| 854 | RESTORE_STACK; |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | int opus_decode_float(OpusDecoder *st, const unsigned char *data, |
| 859 | opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) |
| 860 | { |
| 861 | return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL); |
| 862 | } |
| 863 | |
| 864 | #endif |
| 865 | |
| 866 | int opus_decoder_ctl(OpusDecoder *st, int request, ...) |
| 867 | { |
| 868 | int ret = OPUS_OK; |
| 869 | va_list ap; |
| 870 | void *silk_dec; |
| 871 | CELTDecoder *celt_dec; |
| 872 | |
| 873 | silk_dec = (char*)st+st->silk_dec_offset; |
| 874 | celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); |
| 875 | |
| 876 | |
| 877 | va_start(ap, request); |
| 878 | |
| 879 | switch (request) |
| 880 | { |
| 881 | case OPUS_GET_BANDWIDTH_REQUEST: |
| 882 | { |
| 883 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 884 | *value = st->bandwidth; |
| 885 | } |
| 886 | break; |
| 887 | case OPUS_GET_FINAL_RANGE_REQUEST: |
| 888 | { |
| 889 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 890 | *value = st->rangeFinal; |
| 891 | } |
| 892 | break; |
| 893 | case OPUS_RESET_STATE: |
| 894 | { |
| 895 | OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START, |
| 896 | sizeof(OpusDecoder)- |
| 897 | ((char*)&st->OPUS_DECODER_RESET_START - (char*)st)); |
| 898 | |
| 899 | celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); |
| 900 | silk_InitDecoder( silk_dec ); |
| 901 | st->stream_channels = st->channels; |
| 902 | st->frame_size = st->Fs/400; |
| 903 | } |
| 904 | break; |
| 905 | case OPUS_GET_SAMPLE_RATE_REQUEST: |
| 906 | { |
| 907 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 908 | if (value==NULL) |
| 909 | { |
| 910 | ret = OPUS_BAD_ARG; |
| 911 | break; |
| 912 | } |
| 913 | *value = st->Fs; |
| 914 | } |
| 915 | break; |
| 916 | case OPUS_GET_PITCH_REQUEST: |
| 917 | { |
| 918 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 919 | if (value==NULL) |
| 920 | { |
| 921 | ret = OPUS_BAD_ARG; |
| 922 | break; |
| 923 | } |
| 924 | if (st->prev_mode == MODE_CELT_ONLY) |
| 925 | celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); |
| 926 | else |
| 927 | *value = st->DecControl.prevPitchLag; |
| 928 | } |
| 929 | break; |
| 930 | case OPUS_GET_GAIN_REQUEST: |
| 931 | { |
| 932 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 933 | if (value==NULL) |
| 934 | { |
| 935 | ret = OPUS_BAD_ARG; |
| 936 | break; |
| 937 | } |
| 938 | *value = st->decode_gain; |
| 939 | } |
| 940 | break; |
| 941 | case OPUS_SET_GAIN_REQUEST: |
| 942 | { |
| 943 | opus_int32 value = va_arg(ap, opus_int32); |
| 944 | if (value<-32768 || value>32767) |
| 945 | { |
| 946 | ret = OPUS_BAD_ARG; |
| 947 | break; |
| 948 | } |
| 949 | st->decode_gain = value; |
| 950 | } |
| 951 | break; |
| 952 | case OPUS_GET_LAST_PACKET_DURATION_REQUEST: |
| 953 | { |
| 954 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 955 | *value = st->last_packet_duration; |
| 956 | } |
| 957 | break; |
| 958 | default: |
| 959 | /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ |
| 960 | ret = OPUS_UNIMPLEMENTED; |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | va_end(ap); |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | void opus_decoder_destroy(OpusDecoder *st) |
| 969 | { |
| 970 | opus_free(st); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | int opus_packet_get_bandwidth(const unsigned char *data) |
| 975 | { |
| 976 | int bandwidth; |
| 977 | if (data[0]&0x80) |
| 978 | { |
| 979 | bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3); |
| 980 | if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) |
| 981 | bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
| 982 | } else if ((data[0]&0x60) == 0x60) |
| 983 | { |
| 984 | bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND : |
| 985 | OPUS_BANDWIDTH_SUPERWIDEBAND; |
| 986 | } else { |
| 987 | bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3); |
| 988 | } |
| 989 | return bandwidth; |
| 990 | } |
| 991 | |
| 992 | int opus_packet_get_samples_per_frame(const unsigned char *data, |
| 993 | opus_int32 Fs) |
| 994 | { |
| 995 | int audiosize; |
| 996 | if (data[0]&0x80) |
| 997 | { |
| 998 | audiosize = ((data[0]>>3)&0x3); |
| 999 | audiosize = (Fs<<audiosize)/400; |
| 1000 | } else if ((data[0]&0x60) == 0x60) |
| 1001 | { |
| 1002 | audiosize = (data[0]&0x08) ? Fs/50 : Fs/100; |
| 1003 | } else { |
| 1004 | audiosize = ((data[0]>>3)&0x3); |
| 1005 | if (audiosize == 3) |
| 1006 | audiosize = Fs*60/1000; |
| 1007 | else |
| 1008 | audiosize = (Fs<<audiosize)/100; |
| 1009 | } |
| 1010 | return audiosize; |
| 1011 | } |
| 1012 | |
| 1013 | int opus_packet_get_nb_channels(const unsigned char *data) |
| 1014 | { |
| 1015 | return (data[0]&0x4) ? 2 : 1; |
| 1016 | } |
| 1017 | |
| 1018 | int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) |
| 1019 | { |
| 1020 | int count; |
| 1021 | if (len<1) |
| 1022 | return OPUS_BAD_ARG; |
| 1023 | count = packet[0]&0x3; |
| 1024 | if (count==0) |
| 1025 | return 1; |
| 1026 | else if (count!=3) |
| 1027 | return 2; |
| 1028 | else if (len<2) |
| 1029 | return OPUS_INVALID_PACKET; |
| 1030 | else |
| 1031 | return packet[1]&0x3F; |
| 1032 | } |
| 1033 | |
| 1034 | int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, |
| 1035 | opus_int32 Fs) |
| 1036 | { |
| 1037 | int samples; |
| 1038 | int count = opus_packet_get_nb_frames(packet, len); |
| 1039 | |
| 1040 | if (count<0) |
| 1041 | return count; |
| 1042 | |
| 1043 | samples = count*opus_packet_get_samples_per_frame(packet, Fs); |
| 1044 | /* Can't have more than 120 ms */ |
| 1045 | if (samples*25 > Fs*3) |
| 1046 | return OPUS_INVALID_PACKET; |
| 1047 | else |
| 1048 | return samples; |
| 1049 | } |
| 1050 | |
| 1051 | int opus_decoder_get_nb_samples(const OpusDecoder *dec, |
| 1052 | const unsigned char packet[], opus_int32 len) |
| 1053 | { |
| 1054 | return opus_packet_get_nb_samples(packet, len, dec->Fs); |
| 1055 | } |