1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/src/opus_decoder.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,970 @@ 1.4 +/* Copyright (c) 2010 Xiph.Org Foundation, Skype Limited 1.5 + Written by Jean-Marc Valin and Koen Vos */ 1.6 +/* 1.7 + Redistribution and use in source and binary forms, with or without 1.8 + modification, are permitted provided that the following conditions 1.9 + are met: 1.10 + 1.11 + - Redistributions of source code must retain the above copyright 1.12 + notice, this list of conditions and the following disclaimer. 1.13 + 1.14 + - Redistributions in binary form must reproduce the above copyright 1.15 + notice, this list of conditions and the following disclaimer in the 1.16 + documentation and/or other materials provided with the distribution. 1.17 + 1.18 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.19 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.20 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.21 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.22 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.23 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.24 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.25 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.26 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.27 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.28 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 +*/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +# include "config.h" 1.33 +#endif 1.34 + 1.35 +#ifndef OPUS_BUILD 1.36 +# 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." 1.37 +#endif 1.38 + 1.39 +#if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__) 1.40 +# pragma message "You appear to be compiling without optimization, if so opus will be very slow." 1.41 +#endif 1.42 + 1.43 +#include <stdarg.h> 1.44 +#include "celt.h" 1.45 +#include "opus.h" 1.46 +#include "entdec.h" 1.47 +#include "modes.h" 1.48 +#include "API.h" 1.49 +#include "stack_alloc.h" 1.50 +#include "float_cast.h" 1.51 +#include "opus_private.h" 1.52 +#include "os_support.h" 1.53 +#include "structs.h" 1.54 +#include "define.h" 1.55 +#include "mathops.h" 1.56 +#include "cpu_support.h" 1.57 + 1.58 +struct OpusDecoder { 1.59 + int celt_dec_offset; 1.60 + int silk_dec_offset; 1.61 + int channels; 1.62 + opus_int32 Fs; /** Sampling rate (at the API level) */ 1.63 + silk_DecControlStruct DecControl; 1.64 + int decode_gain; 1.65 + 1.66 + /* Everything beyond this point gets cleared on a reset */ 1.67 +#define OPUS_DECODER_RESET_START stream_channels 1.68 + int stream_channels; 1.69 + 1.70 + int bandwidth; 1.71 + int mode; 1.72 + int prev_mode; 1.73 + int frame_size; 1.74 + int prev_redundancy; 1.75 + int last_packet_duration; 1.76 +#ifndef FIXED_POINT 1.77 + opus_val16 softclip_mem[2]; 1.78 +#endif 1.79 + 1.80 + opus_uint32 rangeFinal; 1.81 +}; 1.82 + 1.83 +#ifdef FIXED_POINT 1.84 +static OPUS_INLINE opus_int16 SAT16(opus_int32 x) { 1.85 + return x > 32767 ? 32767 : x < -32768 ? -32768 : (opus_int16)x; 1.86 +} 1.87 +#endif 1.88 + 1.89 + 1.90 +int opus_decoder_get_size(int channels) 1.91 +{ 1.92 + int silkDecSizeBytes, celtDecSizeBytes; 1.93 + int ret; 1.94 + if (channels<1 || channels > 2) 1.95 + return 0; 1.96 + ret = silk_Get_Decoder_Size( &silkDecSizeBytes ); 1.97 + if(ret) 1.98 + return 0; 1.99 + silkDecSizeBytes = align(silkDecSizeBytes); 1.100 + celtDecSizeBytes = celt_decoder_get_size(channels); 1.101 + return align(sizeof(OpusDecoder))+silkDecSizeBytes+celtDecSizeBytes; 1.102 +} 1.103 + 1.104 +int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels) 1.105 +{ 1.106 + void *silk_dec; 1.107 + CELTDecoder *celt_dec; 1.108 + int ret, silkDecSizeBytes; 1.109 + 1.110 + if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000) 1.111 + || (channels!=1&&channels!=2)) 1.112 + return OPUS_BAD_ARG; 1.113 + 1.114 + OPUS_CLEAR((char*)st, opus_decoder_get_size(channels)); 1.115 + /* Initialize SILK encoder */ 1.116 + ret = silk_Get_Decoder_Size(&silkDecSizeBytes); 1.117 + if (ret) 1.118 + return OPUS_INTERNAL_ERROR; 1.119 + 1.120 + silkDecSizeBytes = align(silkDecSizeBytes); 1.121 + st->silk_dec_offset = align(sizeof(OpusDecoder)); 1.122 + st->celt_dec_offset = st->silk_dec_offset+silkDecSizeBytes; 1.123 + silk_dec = (char*)st+st->silk_dec_offset; 1.124 + celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); 1.125 + st->stream_channels = st->channels = channels; 1.126 + 1.127 + st->Fs = Fs; 1.128 + st->DecControl.API_sampleRate = st->Fs; 1.129 + st->DecControl.nChannelsAPI = st->channels; 1.130 + 1.131 + /* Reset decoder */ 1.132 + ret = silk_InitDecoder( silk_dec ); 1.133 + if(ret)return OPUS_INTERNAL_ERROR; 1.134 + 1.135 + /* Initialize CELT decoder */ 1.136 + ret = celt_decoder_init(celt_dec, Fs, channels); 1.137 + if(ret!=OPUS_OK)return OPUS_INTERNAL_ERROR; 1.138 + 1.139 + celt_decoder_ctl(celt_dec, CELT_SET_SIGNALLING(0)); 1.140 + 1.141 + st->prev_mode = 0; 1.142 + st->frame_size = Fs/400; 1.143 + return OPUS_OK; 1.144 +} 1.145 + 1.146 +OpusDecoder *opus_decoder_create(opus_int32 Fs, int channels, int *error) 1.147 +{ 1.148 + int ret; 1.149 + OpusDecoder *st; 1.150 + if ((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000) 1.151 + || (channels!=1&&channels!=2)) 1.152 + { 1.153 + if (error) 1.154 + *error = OPUS_BAD_ARG; 1.155 + return NULL; 1.156 + } 1.157 + st = (OpusDecoder *)opus_alloc(opus_decoder_get_size(channels)); 1.158 + if (st == NULL) 1.159 + { 1.160 + if (error) 1.161 + *error = OPUS_ALLOC_FAIL; 1.162 + return NULL; 1.163 + } 1.164 + ret = opus_decoder_init(st, Fs, channels); 1.165 + if (error) 1.166 + *error = ret; 1.167 + if (ret != OPUS_OK) 1.168 + { 1.169 + opus_free(st); 1.170 + st = NULL; 1.171 + } 1.172 + return st; 1.173 +} 1.174 + 1.175 +static void smooth_fade(const opus_val16 *in1, const opus_val16 *in2, 1.176 + opus_val16 *out, int overlap, int channels, 1.177 + const opus_val16 *window, opus_int32 Fs) 1.178 +{ 1.179 + int i, c; 1.180 + int inc = 48000/Fs; 1.181 + for (c=0;c<channels;c++) 1.182 + { 1.183 + for (i=0;i<overlap;i++) 1.184 + { 1.185 + opus_val16 w = MULT16_16_Q15(window[i*inc], window[i*inc]); 1.186 + out[i*channels+c] = SHR32(MAC16_16(MULT16_16(w,in2[i*channels+c]), 1.187 + Q15ONE-w, in1[i*channels+c]), 15); 1.188 + } 1.189 + } 1.190 +} 1.191 + 1.192 +static int opus_packet_get_mode(const unsigned char *data) 1.193 +{ 1.194 + int mode; 1.195 + if (data[0]&0x80) 1.196 + { 1.197 + mode = MODE_CELT_ONLY; 1.198 + } else if ((data[0]&0x60) == 0x60) 1.199 + { 1.200 + mode = MODE_HYBRID; 1.201 + } else { 1.202 + mode = MODE_SILK_ONLY; 1.203 + } 1.204 + return mode; 1.205 +} 1.206 + 1.207 +static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, 1.208 + opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) 1.209 +{ 1.210 + void *silk_dec; 1.211 + CELTDecoder *celt_dec; 1.212 + int i, silk_ret=0, celt_ret=0; 1.213 + ec_dec dec; 1.214 + opus_int32 silk_frame_size; 1.215 + int pcm_silk_size; 1.216 + VARDECL(opus_int16, pcm_silk); 1.217 + int pcm_transition_silk_size; 1.218 + VARDECL(opus_val16, pcm_transition_silk); 1.219 + int pcm_transition_celt_size; 1.220 + VARDECL(opus_val16, pcm_transition_celt); 1.221 + opus_val16 *pcm_transition; 1.222 + int redundant_audio_size; 1.223 + VARDECL(opus_val16, redundant_audio); 1.224 + 1.225 + int audiosize; 1.226 + int mode; 1.227 + int transition=0; 1.228 + int start_band; 1.229 + int redundancy=0; 1.230 + int redundancy_bytes = 0; 1.231 + int celt_to_silk=0; 1.232 + int c; 1.233 + int F2_5, F5, F10, F20; 1.234 + const opus_val16 *window; 1.235 + opus_uint32 redundant_rng = 0; 1.236 + ALLOC_STACK; 1.237 + 1.238 + silk_dec = (char*)st+st->silk_dec_offset; 1.239 + celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); 1.240 + F20 = st->Fs/50; 1.241 + F10 = F20>>1; 1.242 + F5 = F10>>1; 1.243 + F2_5 = F5>>1; 1.244 + if (frame_size < F2_5) 1.245 + { 1.246 + RESTORE_STACK; 1.247 + return OPUS_BUFFER_TOO_SMALL; 1.248 + } 1.249 + /* Limit frame_size to avoid excessive stack allocations. */ 1.250 + frame_size = IMIN(frame_size, st->Fs/25*3); 1.251 + /* Payloads of 1 (2 including ToC) or 0 trigger the PLC/DTX */ 1.252 + if (len<=1) 1.253 + { 1.254 + data = NULL; 1.255 + /* In that case, don't conceal more than what the ToC says */ 1.256 + frame_size = IMIN(frame_size, st->frame_size); 1.257 + } 1.258 + if (data != NULL) 1.259 + { 1.260 + audiosize = st->frame_size; 1.261 + mode = st->mode; 1.262 + ec_dec_init(&dec,(unsigned char*)data,len); 1.263 + } else { 1.264 + audiosize = frame_size; 1.265 + mode = st->prev_mode; 1.266 + 1.267 + if (mode == 0) 1.268 + { 1.269 + /* If we haven't got any packet yet, all we can do is return zeros */ 1.270 + for (i=0;i<audiosize*st->channels;i++) 1.271 + pcm[i] = 0; 1.272 + RESTORE_STACK; 1.273 + return audiosize; 1.274 + } 1.275 + 1.276 + /* Avoids trying to run the PLC on sizes other than 2.5 (CELT), 5 (CELT), 1.277 + 10, or 20 (e.g. 12.5 or 30 ms). */ 1.278 + if (audiosize > F20) 1.279 + { 1.280 + do { 1.281 + int ret = opus_decode_frame(st, NULL, 0, pcm, IMIN(audiosize, F20), 0); 1.282 + if (ret<0) 1.283 + { 1.284 + RESTORE_STACK; 1.285 + return ret; 1.286 + } 1.287 + pcm += ret*st->channels; 1.288 + audiosize -= ret; 1.289 + } while (audiosize > 0); 1.290 + RESTORE_STACK; 1.291 + return frame_size; 1.292 + } else if (audiosize < F20) 1.293 + { 1.294 + if (audiosize > F10) 1.295 + audiosize = F10; 1.296 + else if (mode != MODE_SILK_ONLY && audiosize > F5 && audiosize < F10) 1.297 + audiosize = F5; 1.298 + } 1.299 + } 1.300 + 1.301 + pcm_transition_silk_size = ALLOC_NONE; 1.302 + pcm_transition_celt_size = ALLOC_NONE; 1.303 + if (data!=NULL && st->prev_mode > 0 && ( 1.304 + (mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY && !st->prev_redundancy) 1.305 + || (mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ) 1.306 + ) 1.307 + { 1.308 + transition = 1; 1.309 + /* Decide where to allocate the stack memory for pcm_transition */ 1.310 + if (mode == MODE_CELT_ONLY) 1.311 + pcm_transition_celt_size = F5*st->channels; 1.312 + else 1.313 + pcm_transition_silk_size = F5*st->channels; 1.314 + } 1.315 + ALLOC(pcm_transition_celt, pcm_transition_celt_size, opus_val16); 1.316 + if (transition && mode == MODE_CELT_ONLY) 1.317 + { 1.318 + pcm_transition = pcm_transition_celt; 1.319 + opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); 1.320 + } 1.321 + if (audiosize > frame_size) 1.322 + { 1.323 + /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/ 1.324 + RESTORE_STACK; 1.325 + return OPUS_BAD_ARG; 1.326 + } else { 1.327 + frame_size = audiosize; 1.328 + } 1.329 + 1.330 + /* Don't allocate any memory when in CELT-only mode */ 1.331 + pcm_silk_size = (mode != MODE_CELT_ONLY) ? IMAX(F10, frame_size)*st->channels : ALLOC_NONE; 1.332 + ALLOC(pcm_silk, pcm_silk_size, opus_int16); 1.333 + 1.334 + /* SILK processing */ 1.335 + if (mode != MODE_CELT_ONLY) 1.336 + { 1.337 + int lost_flag, decoded_samples; 1.338 + opus_int16 *pcm_ptr = pcm_silk; 1.339 + 1.340 + if (st->prev_mode==MODE_CELT_ONLY) 1.341 + silk_InitDecoder( silk_dec ); 1.342 + 1.343 + /* The SILK PLC cannot produce frames of less than 10 ms */ 1.344 + st->DecControl.payloadSize_ms = IMAX(10, 1000 * audiosize / st->Fs); 1.345 + 1.346 + if (data != NULL) 1.347 + { 1.348 + st->DecControl.nChannelsInternal = st->stream_channels; 1.349 + if( mode == MODE_SILK_ONLY ) { 1.350 + if( st->bandwidth == OPUS_BANDWIDTH_NARROWBAND ) { 1.351 + st->DecControl.internalSampleRate = 8000; 1.352 + } else if( st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) { 1.353 + st->DecControl.internalSampleRate = 12000; 1.354 + } else if( st->bandwidth == OPUS_BANDWIDTH_WIDEBAND ) { 1.355 + st->DecControl.internalSampleRate = 16000; 1.356 + } else { 1.357 + st->DecControl.internalSampleRate = 16000; 1.358 + silk_assert( 0 ); 1.359 + } 1.360 + } else { 1.361 + /* Hybrid mode */ 1.362 + st->DecControl.internalSampleRate = 16000; 1.363 + } 1.364 + } 1.365 + 1.366 + lost_flag = data == NULL ? 1 : 2 * decode_fec; 1.367 + decoded_samples = 0; 1.368 + do { 1.369 + /* Call SILK decoder */ 1.370 + int first_frame = decoded_samples == 0; 1.371 + silk_ret = silk_Decode( silk_dec, &st->DecControl, 1.372 + lost_flag, first_frame, &dec, pcm_ptr, &silk_frame_size ); 1.373 + if( silk_ret ) { 1.374 + if (lost_flag) { 1.375 + /* PLC failure should not be fatal */ 1.376 + silk_frame_size = frame_size; 1.377 + for (i=0;i<frame_size*st->channels;i++) 1.378 + pcm_ptr[i] = 0; 1.379 + } else { 1.380 + RESTORE_STACK; 1.381 + return OPUS_INTERNAL_ERROR; 1.382 + } 1.383 + } 1.384 + pcm_ptr += silk_frame_size * st->channels; 1.385 + decoded_samples += silk_frame_size; 1.386 + } while( decoded_samples < frame_size ); 1.387 + } 1.388 + 1.389 + start_band = 0; 1.390 + if (!decode_fec && mode != MODE_CELT_ONLY && data != NULL 1.391 + && ec_tell(&dec)+17+20*(st->mode == MODE_HYBRID) <= 8*len) 1.392 + { 1.393 + /* Check if we have a redundant 0-8 kHz band */ 1.394 + if (mode == MODE_HYBRID) 1.395 + redundancy = ec_dec_bit_logp(&dec, 12); 1.396 + else 1.397 + redundancy = 1; 1.398 + if (redundancy) 1.399 + { 1.400 + celt_to_silk = ec_dec_bit_logp(&dec, 1); 1.401 + /* redundancy_bytes will be at least two, in the non-hybrid 1.402 + case due to the ec_tell() check above */ 1.403 + redundancy_bytes = mode==MODE_HYBRID ? 1.404 + (opus_int32)ec_dec_uint(&dec, 256)+2 : 1.405 + len-((ec_tell(&dec)+7)>>3); 1.406 + len -= redundancy_bytes; 1.407 + /* This is a sanity check. It should never happen for a valid 1.408 + packet, so the exact behaviour is not normative. */ 1.409 + if (len*8 < ec_tell(&dec)) 1.410 + { 1.411 + len = 0; 1.412 + redundancy_bytes = 0; 1.413 + redundancy = 0; 1.414 + } 1.415 + /* Shrink decoder because of raw bits */ 1.416 + dec.storage -= redundancy_bytes; 1.417 + } 1.418 + } 1.419 + if (mode != MODE_CELT_ONLY) 1.420 + start_band = 17; 1.421 + 1.422 + { 1.423 + int endband=21; 1.424 + 1.425 + switch(st->bandwidth) 1.426 + { 1.427 + case OPUS_BANDWIDTH_NARROWBAND: 1.428 + endband = 13; 1.429 + break; 1.430 + case OPUS_BANDWIDTH_MEDIUMBAND: 1.431 + case OPUS_BANDWIDTH_WIDEBAND: 1.432 + endband = 17; 1.433 + break; 1.434 + case OPUS_BANDWIDTH_SUPERWIDEBAND: 1.435 + endband = 19; 1.436 + break; 1.437 + case OPUS_BANDWIDTH_FULLBAND: 1.438 + endband = 21; 1.439 + break; 1.440 + } 1.441 + celt_decoder_ctl(celt_dec, CELT_SET_END_BAND(endband)); 1.442 + celt_decoder_ctl(celt_dec, CELT_SET_CHANNELS(st->stream_channels)); 1.443 + } 1.444 + 1.445 + if (redundancy) 1.446 + { 1.447 + transition = 0; 1.448 + pcm_transition_silk_size=ALLOC_NONE; 1.449 + } 1.450 + 1.451 + ALLOC(pcm_transition_silk, pcm_transition_silk_size, opus_val16); 1.452 + 1.453 + if (transition && mode != MODE_CELT_ONLY) 1.454 + { 1.455 + pcm_transition = pcm_transition_silk; 1.456 + opus_decode_frame(st, NULL, 0, pcm_transition, IMIN(F5, audiosize), 0); 1.457 + } 1.458 + 1.459 + /* Only allocation memory for redundancy if/when needed */ 1.460 + redundant_audio_size = redundancy ? F5*st->channels : ALLOC_NONE; 1.461 + ALLOC(redundant_audio, redundant_audio_size, opus_val16); 1.462 + 1.463 + /* 5 ms redundant frame for CELT->SILK*/ 1.464 + if (redundancy && celt_to_silk) 1.465 + { 1.466 + celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 1.467 + celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, 1.468 + redundant_audio, F5, NULL); 1.469 + celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); 1.470 + } 1.471 + 1.472 + /* MUST be after PLC */ 1.473 + celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(start_band)); 1.474 + 1.475 + if (mode != MODE_SILK_ONLY) 1.476 + { 1.477 + int celt_frame_size = IMIN(F20, frame_size); 1.478 + /* Make sure to discard any previous CELT state */ 1.479 + if (mode != st->prev_mode && st->prev_mode > 0 && !st->prev_redundancy) 1.480 + celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); 1.481 + /* Decode CELT */ 1.482 + celt_ret = celt_decode_with_ec(celt_dec, decode_fec ? NULL : data, 1.483 + len, pcm, celt_frame_size, &dec); 1.484 + } else { 1.485 + unsigned char silence[2] = {0xFF, 0xFF}; 1.486 + for (i=0;i<frame_size*st->channels;i++) 1.487 + pcm[i] = 0; 1.488 + /* For hybrid -> SILK transitions, we let the CELT MDCT 1.489 + do a fade-out by decoding a silence frame */ 1.490 + if (st->prev_mode == MODE_HYBRID && !(redundancy && celt_to_silk && st->prev_redundancy) ) 1.491 + { 1.492 + celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 1.493 + celt_decode_with_ec(celt_dec, silence, 2, pcm, F2_5, NULL); 1.494 + } 1.495 + } 1.496 + 1.497 + if (mode != MODE_CELT_ONLY) 1.498 + { 1.499 +#ifdef FIXED_POINT 1.500 + for (i=0;i<frame_size*st->channels;i++) 1.501 + pcm[i] = SAT16(pcm[i] + pcm_silk[i]); 1.502 +#else 1.503 + for (i=0;i<frame_size*st->channels;i++) 1.504 + pcm[i] = pcm[i] + (opus_val16)((1.f/32768.f)*pcm_silk[i]); 1.505 +#endif 1.506 + } 1.507 + 1.508 + { 1.509 + const CELTMode *celt_mode; 1.510 + celt_decoder_ctl(celt_dec, CELT_GET_MODE(&celt_mode)); 1.511 + window = celt_mode->window; 1.512 + } 1.513 + 1.514 + /* 5 ms redundant frame for SILK->CELT */ 1.515 + if (redundancy && !celt_to_silk) 1.516 + { 1.517 + celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); 1.518 + celt_decoder_ctl(celt_dec, CELT_SET_START_BAND(0)); 1.519 + 1.520 + celt_decode_with_ec(celt_dec, data+len, redundancy_bytes, redundant_audio, F5, NULL); 1.521 + celt_decoder_ctl(celt_dec, OPUS_GET_FINAL_RANGE(&redundant_rng)); 1.522 + smooth_fade(pcm+st->channels*(frame_size-F2_5), redundant_audio+st->channels*F2_5, 1.523 + pcm+st->channels*(frame_size-F2_5), F2_5, st->channels, window, st->Fs); 1.524 + } 1.525 + if (redundancy && celt_to_silk) 1.526 + { 1.527 + for (c=0;c<st->channels;c++) 1.528 + { 1.529 + for (i=0;i<F2_5;i++) 1.530 + pcm[st->channels*i+c] = redundant_audio[st->channels*i+c]; 1.531 + } 1.532 + smooth_fade(redundant_audio+st->channels*F2_5, pcm+st->channels*F2_5, 1.533 + pcm+st->channels*F2_5, F2_5, st->channels, window, st->Fs); 1.534 + } 1.535 + if (transition) 1.536 + { 1.537 + if (audiosize >= F5) 1.538 + { 1.539 + for (i=0;i<st->channels*F2_5;i++) 1.540 + pcm[i] = pcm_transition[i]; 1.541 + smooth_fade(pcm_transition+st->channels*F2_5, pcm+st->channels*F2_5, 1.542 + pcm+st->channels*F2_5, F2_5, 1.543 + st->channels, window, st->Fs); 1.544 + } else { 1.545 + /* Not enough time to do a clean transition, but we do it anyway 1.546 + This will not preserve amplitude perfectly and may introduce 1.547 + a bit of temporal aliasing, but it shouldn't be too bad and 1.548 + that's pretty much the best we can do. In any case, generating this 1.549 + transition it pretty silly in the first place */ 1.550 + smooth_fade(pcm_transition, pcm, 1.551 + pcm, F2_5, 1.552 + st->channels, window, st->Fs); 1.553 + } 1.554 + } 1.555 + 1.556 + if(st->decode_gain) 1.557 + { 1.558 + opus_val32 gain; 1.559 + gain = celt_exp2(MULT16_16_P15(QCONST16(6.48814081e-4f, 25), st->decode_gain)); 1.560 + for (i=0;i<frame_size*st->channels;i++) 1.561 + { 1.562 + opus_val32 x; 1.563 + x = MULT16_32_P16(pcm[i],gain); 1.564 + pcm[i] = SATURATE(x, 32767); 1.565 + } 1.566 + } 1.567 + 1.568 + if (len <= 1) 1.569 + st->rangeFinal = 0; 1.570 + else 1.571 + st->rangeFinal = dec.rng ^ redundant_rng; 1.572 + 1.573 + st->prev_mode = mode; 1.574 + st->prev_redundancy = redundancy && !celt_to_silk; 1.575 + 1.576 + if (celt_ret>=0) 1.577 + { 1.578 + if (OPUS_CHECK_ARRAY(pcm, audiosize*st->channels)) 1.579 + OPUS_PRINT_INT(audiosize); 1.580 + } 1.581 + 1.582 + RESTORE_STACK; 1.583 + return celt_ret < 0 ? celt_ret : audiosize; 1.584 + 1.585 +} 1.586 + 1.587 +int opus_decode_native(OpusDecoder *st, const unsigned char *data, 1.588 + opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec, 1.589 + int self_delimited, opus_int32 *packet_offset, int soft_clip) 1.590 +{ 1.591 + int i, nb_samples; 1.592 + int count, offset; 1.593 + unsigned char toc; 1.594 + int packet_frame_size, packet_bandwidth, packet_mode, packet_stream_channels; 1.595 + /* 48 x 2.5 ms = 120 ms */ 1.596 + opus_int16 size[48]; 1.597 + if (decode_fec<0 || decode_fec>1) 1.598 + return OPUS_BAD_ARG; 1.599 + /* For FEC/PLC, frame_size has to be to have a multiple of 2.5 ms */ 1.600 + if ((decode_fec || len==0 || data==NULL) && frame_size%(st->Fs/400)!=0) 1.601 + return OPUS_BAD_ARG; 1.602 + if (len==0 || data==NULL) 1.603 + { 1.604 + int pcm_count=0; 1.605 + do { 1.606 + int ret; 1.607 + ret = opus_decode_frame(st, NULL, 0, pcm+pcm_count*st->channels, frame_size-pcm_count, 0); 1.608 + if (ret<0) 1.609 + return ret; 1.610 + pcm_count += ret; 1.611 + } while (pcm_count < frame_size); 1.612 + celt_assert(pcm_count == frame_size); 1.613 + if (OPUS_CHECK_ARRAY(pcm, pcm_count*st->channels)) 1.614 + OPUS_PRINT_INT(pcm_count); 1.615 + st->last_packet_duration = pcm_count; 1.616 + return pcm_count; 1.617 + } else if (len<0) 1.618 + return OPUS_BAD_ARG; 1.619 + 1.620 + packet_mode = opus_packet_get_mode(data); 1.621 + packet_bandwidth = opus_packet_get_bandwidth(data); 1.622 + packet_frame_size = opus_packet_get_samples_per_frame(data, st->Fs); 1.623 + packet_stream_channels = opus_packet_get_nb_channels(data); 1.624 + 1.625 + count = opus_packet_parse_impl(data, len, self_delimited, &toc, NULL, 1.626 + size, &offset, packet_offset); 1.627 + if (count<0) 1.628 + return count; 1.629 + 1.630 + data += offset; 1.631 + 1.632 + if (decode_fec) 1.633 + { 1.634 + int duration_copy; 1.635 + int ret; 1.636 + /* If no FEC can be present, run the PLC (recursive call) */ 1.637 + if (frame_size < packet_frame_size || packet_mode == MODE_CELT_ONLY || st->mode == MODE_CELT_ONLY) 1.638 + return opus_decode_native(st, NULL, 0, pcm, frame_size, 0, 0, NULL, soft_clip); 1.639 + /* Otherwise, run the PLC on everything except the size for which we might have FEC */ 1.640 + duration_copy = st->last_packet_duration; 1.641 + if (frame_size-packet_frame_size!=0) 1.642 + { 1.643 + ret = opus_decode_native(st, NULL, 0, pcm, frame_size-packet_frame_size, 0, 0, NULL, soft_clip); 1.644 + if (ret<0) 1.645 + { 1.646 + st->last_packet_duration = duration_copy; 1.647 + return ret; 1.648 + } 1.649 + celt_assert(ret==frame_size-packet_frame_size); 1.650 + } 1.651 + /* Complete with FEC */ 1.652 + st->mode = packet_mode; 1.653 + st->bandwidth = packet_bandwidth; 1.654 + st->frame_size = packet_frame_size; 1.655 + st->stream_channels = packet_stream_channels; 1.656 + ret = opus_decode_frame(st, data, size[0], pcm+st->channels*(frame_size-packet_frame_size), 1.657 + packet_frame_size, 1); 1.658 + if (ret<0) 1.659 + return ret; 1.660 + else { 1.661 + if (OPUS_CHECK_ARRAY(pcm, frame_size*st->channels)) 1.662 + OPUS_PRINT_INT(frame_size); 1.663 + st->last_packet_duration = frame_size; 1.664 + return frame_size; 1.665 + } 1.666 + } 1.667 + 1.668 + if (count*packet_frame_size > frame_size) 1.669 + return OPUS_BUFFER_TOO_SMALL; 1.670 + 1.671 + /* Update the state as the last step to avoid updating it on an invalid packet */ 1.672 + st->mode = packet_mode; 1.673 + st->bandwidth = packet_bandwidth; 1.674 + st->frame_size = packet_frame_size; 1.675 + st->stream_channels = packet_stream_channels; 1.676 + 1.677 + nb_samples=0; 1.678 + for (i=0;i<count;i++) 1.679 + { 1.680 + int ret; 1.681 + ret = opus_decode_frame(st, data, size[i], pcm+nb_samples*st->channels, frame_size-nb_samples, 0); 1.682 + if (ret<0) 1.683 + return ret; 1.684 + celt_assert(ret==packet_frame_size); 1.685 + data += size[i]; 1.686 + nb_samples += ret; 1.687 + } 1.688 + st->last_packet_duration = nb_samples; 1.689 + if (OPUS_CHECK_ARRAY(pcm, nb_samples*st->channels)) 1.690 + OPUS_PRINT_INT(nb_samples); 1.691 +#ifndef FIXED_POINT 1.692 + if (soft_clip) 1.693 + opus_pcm_soft_clip(pcm, nb_samples, st->channels, st->softclip_mem); 1.694 + else 1.695 + st->softclip_mem[0]=st->softclip_mem[1]=0; 1.696 +#endif 1.697 + return nb_samples; 1.698 +} 1.699 + 1.700 +#ifdef FIXED_POINT 1.701 + 1.702 +int opus_decode(OpusDecoder *st, const unsigned char *data, 1.703 + opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) 1.704 +{ 1.705 + if(frame_size<=0) 1.706 + return OPUS_BAD_ARG; 1.707 + return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0); 1.708 +} 1.709 + 1.710 +#ifndef DISABLE_FLOAT_API 1.711 +int opus_decode_float(OpusDecoder *st, const unsigned char *data, 1.712 + opus_int32 len, float *pcm, int frame_size, int decode_fec) 1.713 +{ 1.714 + VARDECL(opus_int16, out); 1.715 + int ret, i; 1.716 + ALLOC_STACK; 1.717 + 1.718 + if(frame_size<=0) 1.719 + { 1.720 + RESTORE_STACK; 1.721 + return OPUS_BAD_ARG; 1.722 + } 1.723 + ALLOC(out, frame_size*st->channels, opus_int16); 1.724 + 1.725 + ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0); 1.726 + if (ret > 0) 1.727 + { 1.728 + for (i=0;i<ret*st->channels;i++) 1.729 + pcm[i] = (1.f/32768.f)*(out[i]); 1.730 + } 1.731 + RESTORE_STACK; 1.732 + return ret; 1.733 +} 1.734 +#endif 1.735 + 1.736 + 1.737 +#else 1.738 +int opus_decode(OpusDecoder *st, const unsigned char *data, 1.739 + opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec) 1.740 +{ 1.741 + VARDECL(float, out); 1.742 + int ret, i; 1.743 + ALLOC_STACK; 1.744 + 1.745 + if(frame_size<=0) 1.746 + { 1.747 + RESTORE_STACK; 1.748 + return OPUS_BAD_ARG; 1.749 + } 1.750 + 1.751 + ALLOC(out, frame_size*st->channels, float); 1.752 + 1.753 + ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1); 1.754 + if (ret > 0) 1.755 + { 1.756 + for (i=0;i<ret*st->channels;i++) 1.757 + pcm[i] = FLOAT2INT16(out[i]); 1.758 + } 1.759 + RESTORE_STACK; 1.760 + return ret; 1.761 +} 1.762 + 1.763 +int opus_decode_float(OpusDecoder *st, const unsigned char *data, 1.764 + opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec) 1.765 +{ 1.766 + if(frame_size<=0) 1.767 + return OPUS_BAD_ARG; 1.768 + return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0); 1.769 +} 1.770 + 1.771 +#endif 1.772 + 1.773 +int opus_decoder_ctl(OpusDecoder *st, int request, ...) 1.774 +{ 1.775 + int ret = OPUS_OK; 1.776 + va_list ap; 1.777 + void *silk_dec; 1.778 + CELTDecoder *celt_dec; 1.779 + 1.780 + silk_dec = (char*)st+st->silk_dec_offset; 1.781 + celt_dec = (CELTDecoder*)((char*)st+st->celt_dec_offset); 1.782 + 1.783 + 1.784 + va_start(ap, request); 1.785 + 1.786 + switch (request) 1.787 + { 1.788 + case OPUS_GET_BANDWIDTH_REQUEST: 1.789 + { 1.790 + opus_int32 *value = va_arg(ap, opus_int32*); 1.791 + if (!value) 1.792 + { 1.793 + goto bad_arg; 1.794 + } 1.795 + *value = st->bandwidth; 1.796 + } 1.797 + break; 1.798 + case OPUS_GET_FINAL_RANGE_REQUEST: 1.799 + { 1.800 + opus_uint32 *value = va_arg(ap, opus_uint32*); 1.801 + if (!value) 1.802 + { 1.803 + goto bad_arg; 1.804 + } 1.805 + *value = st->rangeFinal; 1.806 + } 1.807 + break; 1.808 + case OPUS_RESET_STATE: 1.809 + { 1.810 + OPUS_CLEAR((char*)&st->OPUS_DECODER_RESET_START, 1.811 + sizeof(OpusDecoder)- 1.812 + ((char*)&st->OPUS_DECODER_RESET_START - (char*)st)); 1.813 + 1.814 + celt_decoder_ctl(celt_dec, OPUS_RESET_STATE); 1.815 + silk_InitDecoder( silk_dec ); 1.816 + st->stream_channels = st->channels; 1.817 + st->frame_size = st->Fs/400; 1.818 + } 1.819 + break; 1.820 + case OPUS_GET_SAMPLE_RATE_REQUEST: 1.821 + { 1.822 + opus_int32 *value = va_arg(ap, opus_int32*); 1.823 + if (!value) 1.824 + { 1.825 + goto bad_arg; 1.826 + } 1.827 + *value = st->Fs; 1.828 + } 1.829 + break; 1.830 + case OPUS_GET_PITCH_REQUEST: 1.831 + { 1.832 + opus_int32 *value = va_arg(ap, opus_int32*); 1.833 + if (!value) 1.834 + { 1.835 + goto bad_arg; 1.836 + } 1.837 + if (st->prev_mode == MODE_CELT_ONLY) 1.838 + celt_decoder_ctl(celt_dec, OPUS_GET_PITCH(value)); 1.839 + else 1.840 + *value = st->DecControl.prevPitchLag; 1.841 + } 1.842 + break; 1.843 + case OPUS_GET_GAIN_REQUEST: 1.844 + { 1.845 + opus_int32 *value = va_arg(ap, opus_int32*); 1.846 + if (!value) 1.847 + { 1.848 + goto bad_arg; 1.849 + } 1.850 + *value = st->decode_gain; 1.851 + } 1.852 + break; 1.853 + case OPUS_SET_GAIN_REQUEST: 1.854 + { 1.855 + opus_int32 value = va_arg(ap, opus_int32); 1.856 + if (value<-32768 || value>32767) 1.857 + { 1.858 + goto bad_arg; 1.859 + } 1.860 + st->decode_gain = value; 1.861 + } 1.862 + break; 1.863 + case OPUS_GET_LAST_PACKET_DURATION_REQUEST: 1.864 + { 1.865 + opus_uint32 *value = va_arg(ap, opus_uint32*); 1.866 + if (!value) 1.867 + { 1.868 + goto bad_arg; 1.869 + } 1.870 + *value = st->last_packet_duration; 1.871 + } 1.872 + break; 1.873 + default: 1.874 + /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/ 1.875 + ret = OPUS_UNIMPLEMENTED; 1.876 + break; 1.877 + } 1.878 + 1.879 + va_end(ap); 1.880 + return ret; 1.881 +bad_arg: 1.882 + va_end(ap); 1.883 + return OPUS_BAD_ARG; 1.884 +} 1.885 + 1.886 +void opus_decoder_destroy(OpusDecoder *st) 1.887 +{ 1.888 + opus_free(st); 1.889 +} 1.890 + 1.891 + 1.892 +int opus_packet_get_bandwidth(const unsigned char *data) 1.893 +{ 1.894 + int bandwidth; 1.895 + if (data[0]&0x80) 1.896 + { 1.897 + bandwidth = OPUS_BANDWIDTH_MEDIUMBAND + ((data[0]>>5)&0x3); 1.898 + if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 1.899 + bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1.900 + } else if ((data[0]&0x60) == 0x60) 1.901 + { 1.902 + bandwidth = (data[0]&0x10) ? OPUS_BANDWIDTH_FULLBAND : 1.903 + OPUS_BANDWIDTH_SUPERWIDEBAND; 1.904 + } else { 1.905 + bandwidth = OPUS_BANDWIDTH_NARROWBAND + ((data[0]>>5)&0x3); 1.906 + } 1.907 + return bandwidth; 1.908 +} 1.909 + 1.910 +int opus_packet_get_samples_per_frame(const unsigned char *data, 1.911 + opus_int32 Fs) 1.912 +{ 1.913 + int audiosize; 1.914 + if (data[0]&0x80) 1.915 + { 1.916 + audiosize = ((data[0]>>3)&0x3); 1.917 + audiosize = (Fs<<audiosize)/400; 1.918 + } else if ((data[0]&0x60) == 0x60) 1.919 + { 1.920 + audiosize = (data[0]&0x08) ? Fs/50 : Fs/100; 1.921 + } else { 1.922 + audiosize = ((data[0]>>3)&0x3); 1.923 + if (audiosize == 3) 1.924 + audiosize = Fs*60/1000; 1.925 + else 1.926 + audiosize = (Fs<<audiosize)/100; 1.927 + } 1.928 + return audiosize; 1.929 +} 1.930 + 1.931 +int opus_packet_get_nb_channels(const unsigned char *data) 1.932 +{ 1.933 + return (data[0]&0x4) ? 2 : 1; 1.934 +} 1.935 + 1.936 +int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) 1.937 +{ 1.938 + int count; 1.939 + if (len<1) 1.940 + return OPUS_BAD_ARG; 1.941 + count = packet[0]&0x3; 1.942 + if (count==0) 1.943 + return 1; 1.944 + else if (count!=3) 1.945 + return 2; 1.946 + else if (len<2) 1.947 + return OPUS_INVALID_PACKET; 1.948 + else 1.949 + return packet[1]&0x3F; 1.950 +} 1.951 + 1.952 +int opus_packet_get_nb_samples(const unsigned char packet[], opus_int32 len, 1.953 + opus_int32 Fs) 1.954 +{ 1.955 + int samples; 1.956 + int count = opus_packet_get_nb_frames(packet, len); 1.957 + 1.958 + if (count<0) 1.959 + return count; 1.960 + 1.961 + samples = count*opus_packet_get_samples_per_frame(packet, Fs); 1.962 + /* Can't have more than 120 ms */ 1.963 + if (samples*25 > Fs*3) 1.964 + return OPUS_INVALID_PACKET; 1.965 + else 1.966 + return samples; 1.967 +} 1.968 + 1.969 +int opus_decoder_get_nb_samples(const OpusDecoder *dec, 1.970 + const unsigned char packet[], opus_int32 len) 1.971 +{ 1.972 + return opus_packet_get_nb_samples(packet, len, dec->Fs); 1.973 +}