1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/celt/celt_decoder.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1195 @@ 1.4 +/* Copyright (c) 2007-2008 CSIRO 1.5 + Copyright (c) 2007-2010 Xiph.Org Foundation 1.6 + Copyright (c) 2008 Gregory Maxwell 1.7 + Written by Jean-Marc Valin and Gregory Maxwell */ 1.8 +/* 1.9 + Redistribution and use in source and binary forms, with or without 1.10 + modification, are permitted provided that the following conditions 1.11 + are met: 1.12 + 1.13 + - Redistributions of source code must retain the above copyright 1.14 + notice, this list of conditions and the following disclaimer. 1.15 + 1.16 + - Redistributions in binary form must reproduce the above copyright 1.17 + notice, this list of conditions and the following disclaimer in the 1.18 + documentation and/or other materials provided with the distribution. 1.19 + 1.20 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.24 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.25 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.26 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.27 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.28 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.29 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.30 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 +*/ 1.32 + 1.33 +#ifdef HAVE_CONFIG_H 1.34 +#include "config.h" 1.35 +#endif 1.36 + 1.37 +#define CELT_DECODER_C 1.38 + 1.39 +#include "cpu_support.h" 1.40 +#include "os_support.h" 1.41 +#include "mdct.h" 1.42 +#include <math.h> 1.43 +#include "celt.h" 1.44 +#include "pitch.h" 1.45 +#include "bands.h" 1.46 +#include "modes.h" 1.47 +#include "entcode.h" 1.48 +#include "quant_bands.h" 1.49 +#include "rate.h" 1.50 +#include "stack_alloc.h" 1.51 +#include "mathops.h" 1.52 +#include "float_cast.h" 1.53 +#include <stdarg.h> 1.54 +#include "celt_lpc.h" 1.55 +#include "vq.h" 1.56 + 1.57 +/**********************************************************************/ 1.58 +/* */ 1.59 +/* DECODER */ 1.60 +/* */ 1.61 +/**********************************************************************/ 1.62 +#define DECODE_BUFFER_SIZE 2048 1.63 + 1.64 +/** Decoder state 1.65 + @brief Decoder state 1.66 + */ 1.67 +struct OpusCustomDecoder { 1.68 + const OpusCustomMode *mode; 1.69 + int overlap; 1.70 + int channels; 1.71 + int stream_channels; 1.72 + 1.73 + int downsample; 1.74 + int start, end; 1.75 + int signalling; 1.76 + int arch; 1.77 + 1.78 + /* Everything beyond this point gets cleared on a reset */ 1.79 +#define DECODER_RESET_START rng 1.80 + 1.81 + opus_uint32 rng; 1.82 + int error; 1.83 + int last_pitch_index; 1.84 + int loss_count; 1.85 + int postfilter_period; 1.86 + int postfilter_period_old; 1.87 + opus_val16 postfilter_gain; 1.88 + opus_val16 postfilter_gain_old; 1.89 + int postfilter_tapset; 1.90 + int postfilter_tapset_old; 1.91 + 1.92 + celt_sig preemph_memD[2]; 1.93 + 1.94 + celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ 1.95 + /* opus_val16 lpc[], Size = channels*LPC_ORDER */ 1.96 + /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */ 1.97 + /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */ 1.98 + /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */ 1.99 + /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */ 1.100 +}; 1.101 + 1.102 +int celt_decoder_get_size(int channels) 1.103 +{ 1.104 + const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); 1.105 + return opus_custom_decoder_get_size(mode, channels); 1.106 +} 1.107 + 1.108 +OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels) 1.109 +{ 1.110 + int size = sizeof(struct CELTDecoder) 1.111 + + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) 1.112 + + channels*LPC_ORDER*sizeof(opus_val16) 1.113 + + 4*2*mode->nbEBands*sizeof(opus_val16); 1.114 + return size; 1.115 +} 1.116 + 1.117 +#ifdef CUSTOM_MODES 1.118 +CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error) 1.119 +{ 1.120 + int ret; 1.121 + CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); 1.122 + ret = opus_custom_decoder_init(st, mode, channels); 1.123 + if (ret != OPUS_OK) 1.124 + { 1.125 + opus_custom_decoder_destroy(st); 1.126 + st = NULL; 1.127 + } 1.128 + if (error) 1.129 + *error = ret; 1.130 + return st; 1.131 +} 1.132 +#endif /* CUSTOM_MODES */ 1.133 + 1.134 +int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels) 1.135 +{ 1.136 + int ret; 1.137 + ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); 1.138 + if (ret != OPUS_OK) 1.139 + return ret; 1.140 + st->downsample = resampling_factor(sampling_rate); 1.141 + if (st->downsample==0) 1.142 + return OPUS_BAD_ARG; 1.143 + else 1.144 + return OPUS_OK; 1.145 +} 1.146 + 1.147 +OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels) 1.148 +{ 1.149 + if (channels < 0 || channels > 2) 1.150 + return OPUS_BAD_ARG; 1.151 + 1.152 + if (st==NULL) 1.153 + return OPUS_ALLOC_FAIL; 1.154 + 1.155 + OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels)); 1.156 + 1.157 + st->mode = mode; 1.158 + st->overlap = mode->overlap; 1.159 + st->stream_channels = st->channels = channels; 1.160 + 1.161 + st->downsample = 1; 1.162 + st->start = 0; 1.163 + st->end = st->mode->effEBands; 1.164 + st->signalling = 1; 1.165 + st->arch = opus_select_arch(); 1.166 + 1.167 + st->loss_count = 0; 1.168 + 1.169 + opus_custom_decoder_ctl(st, OPUS_RESET_STATE); 1.170 + 1.171 + return OPUS_OK; 1.172 +} 1.173 + 1.174 +#ifdef CUSTOM_MODES 1.175 +void opus_custom_decoder_destroy(CELTDecoder *st) 1.176 +{ 1.177 + opus_free(st); 1.178 +} 1.179 +#endif /* CUSTOM_MODES */ 1.180 + 1.181 +static OPUS_INLINE opus_val16 SIG2WORD16(celt_sig x) 1.182 +{ 1.183 +#ifdef FIXED_POINT 1.184 + x = PSHR32(x, SIG_SHIFT); 1.185 + x = MAX32(x, -32768); 1.186 + x = MIN32(x, 32767); 1.187 + return EXTRACT16(x); 1.188 +#else 1.189 + return (opus_val16)x; 1.190 +#endif 1.191 +} 1.192 + 1.193 +#ifndef RESYNTH 1.194 +static 1.195 +#endif 1.196 +void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, celt_sig * OPUS_RESTRICT scratch) 1.197 +{ 1.198 + int c; 1.199 + int Nd; 1.200 + int apply_downsampling=0; 1.201 + opus_val16 coef0; 1.202 + 1.203 + coef0 = coef[0]; 1.204 + Nd = N/downsample; 1.205 + c=0; do { 1.206 + int j; 1.207 + celt_sig * OPUS_RESTRICT x; 1.208 + opus_val16 * OPUS_RESTRICT y; 1.209 + celt_sig m = mem[c]; 1.210 + x =in[c]; 1.211 + y = pcm+c; 1.212 +#ifdef CUSTOM_MODES 1.213 + if (coef[1] != 0) 1.214 + { 1.215 + opus_val16 coef1 = coef[1]; 1.216 + opus_val16 coef3 = coef[3]; 1.217 + for (j=0;j<N;j++) 1.218 + { 1.219 + celt_sig tmp = x[j] + m + VERY_SMALL; 1.220 + m = MULT16_32_Q15(coef0, tmp) 1.221 + - MULT16_32_Q15(coef1, x[j]); 1.222 + tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); 1.223 + scratch[j] = tmp; 1.224 + } 1.225 + apply_downsampling=1; 1.226 + } else 1.227 +#endif 1.228 + if (downsample>1) 1.229 + { 1.230 + /* Shortcut for the standard (non-custom modes) case */ 1.231 + for (j=0;j<N;j++) 1.232 + { 1.233 + celt_sig tmp = x[j] + m + VERY_SMALL; 1.234 + m = MULT16_32_Q15(coef0, tmp); 1.235 + scratch[j] = tmp; 1.236 + } 1.237 + apply_downsampling=1; 1.238 + } else { 1.239 + /* Shortcut for the standard (non-custom modes) case */ 1.240 + for (j=0;j<N;j++) 1.241 + { 1.242 + celt_sig tmp = x[j] + m + VERY_SMALL; 1.243 + m = MULT16_32_Q15(coef0, tmp); 1.244 + y[j*C] = SCALEOUT(SIG2WORD16(tmp)); 1.245 + } 1.246 + } 1.247 + mem[c] = m; 1.248 + 1.249 + if (apply_downsampling) 1.250 + { 1.251 + /* Perform down-sampling */ 1.252 + for (j=0;j<Nd;j++) 1.253 + y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample])); 1.254 + } 1.255 + } while (++c<C); 1.256 +} 1.257 + 1.258 +/** Compute the IMDCT and apply window for all sub-frames and 1.259 + all channels in a frame */ 1.260 +#ifndef RESYNTH 1.261 +static 1.262 +#endif 1.263 +void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, 1.264 + celt_sig * OPUS_RESTRICT out_mem[], int C, int LM) 1.265 +{ 1.266 + int b, c; 1.267 + int B; 1.268 + int N; 1.269 + int shift; 1.270 + const int overlap = OVERLAP(mode); 1.271 + 1.272 + if (shortBlocks) 1.273 + { 1.274 + B = shortBlocks; 1.275 + N = mode->shortMdctSize; 1.276 + shift = mode->maxLM; 1.277 + } else { 1.278 + B = 1; 1.279 + N = mode->shortMdctSize<<LM; 1.280 + shift = mode->maxLM-LM; 1.281 + } 1.282 + c=0; do { 1.283 + /* IMDCT on the interleaved the sub-frames, overlap-add is performed by the IMDCT */ 1.284 + for (b=0;b<B;b++) 1.285 + clt_mdct_backward(&mode->mdct, &X[b+c*N*B], out_mem[c]+N*b, mode->window, overlap, shift, B); 1.286 + } while (++c<C); 1.287 +} 1.288 + 1.289 +static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) 1.290 +{ 1.291 + int i, curr, tf_select; 1.292 + int tf_select_rsv; 1.293 + int tf_changed; 1.294 + int logp; 1.295 + opus_uint32 budget; 1.296 + opus_uint32 tell; 1.297 + 1.298 + budget = dec->storage*8; 1.299 + tell = ec_tell(dec); 1.300 + logp = isTransient ? 2 : 4; 1.301 + tf_select_rsv = LM>0 && tell+logp+1<=budget; 1.302 + budget -= tf_select_rsv; 1.303 + tf_changed = curr = 0; 1.304 + for (i=start;i<end;i++) 1.305 + { 1.306 + if (tell+logp<=budget) 1.307 + { 1.308 + curr ^= ec_dec_bit_logp(dec, logp); 1.309 + tell = ec_tell(dec); 1.310 + tf_changed |= curr; 1.311 + } 1.312 + tf_res[i] = curr; 1.313 + logp = isTransient ? 4 : 5; 1.314 + } 1.315 + tf_select = 0; 1.316 + if (tf_select_rsv && 1.317 + tf_select_table[LM][4*isTransient+0+tf_changed] != 1.318 + tf_select_table[LM][4*isTransient+2+tf_changed]) 1.319 + { 1.320 + tf_select = ec_dec_bit_logp(dec, 1); 1.321 + } 1.322 + for (i=start;i<end;i++) 1.323 + { 1.324 + tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; 1.325 + } 1.326 +} 1.327 + 1.328 +/* The maximum pitch lag to allow in the pitch-based PLC. It's possible to save 1.329 + CPU time in the PLC pitch search by making this smaller than MAX_PERIOD. The 1.330 + current value corresponds to a pitch of 66.67 Hz. */ 1.331 +#define PLC_PITCH_LAG_MAX (720) 1.332 +/* The minimum pitch lag to allow in the pitch-based PLC. This corresponds to a 1.333 + pitch of 480 Hz. */ 1.334 +#define PLC_PITCH_LAG_MIN (100) 1.335 + 1.336 +static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM) 1.337 +{ 1.338 + int c; 1.339 + int i; 1.340 + const int C = st->channels; 1.341 + celt_sig *decode_mem[2]; 1.342 + celt_sig *out_syn[2]; 1.343 + opus_val16 *lpc; 1.344 + opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; 1.345 + const OpusCustomMode *mode; 1.346 + int nbEBands; 1.347 + int overlap; 1.348 + int start; 1.349 + int downsample; 1.350 + int loss_count; 1.351 + int noise_based; 1.352 + const opus_int16 *eBands; 1.353 + VARDECL(celt_sig, scratch); 1.354 + SAVE_STACK; 1.355 + 1.356 + mode = st->mode; 1.357 + nbEBands = mode->nbEBands; 1.358 + overlap = mode->overlap; 1.359 + eBands = mode->eBands; 1.360 + 1.361 + c=0; do { 1.362 + decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap); 1.363 + out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N; 1.364 + } while (++c<C); 1.365 + lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*C); 1.366 + oldBandE = lpc+C*LPC_ORDER; 1.367 + oldLogE = oldBandE + 2*nbEBands; 1.368 + oldLogE2 = oldLogE + 2*nbEBands; 1.369 + backgroundLogE = oldLogE2 + 2*nbEBands; 1.370 + 1.371 + loss_count = st->loss_count; 1.372 + start = st->start; 1.373 + downsample = st->downsample; 1.374 + noise_based = loss_count >= 5 || start != 0; 1.375 + ALLOC(scratch, noise_based?N*C:N, celt_sig); 1.376 + if (noise_based) 1.377 + { 1.378 + /* Noise-based PLC/CNG */ 1.379 + celt_sig *freq; 1.380 + VARDECL(celt_norm, X); 1.381 + opus_uint32 seed; 1.382 + opus_val16 *plcLogE; 1.383 + int end; 1.384 + int effEnd; 1.385 + 1.386 + end = st->end; 1.387 + effEnd = IMAX(start, IMIN(end, mode->effEBands)); 1.388 + 1.389 + /* Share the interleaved signal MDCT coefficient buffer with the 1.390 + deemphasis scratch buffer. */ 1.391 + freq = scratch; 1.392 + ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ 1.393 + 1.394 + if (loss_count >= 5) 1.395 + plcLogE = backgroundLogE; 1.396 + else { 1.397 + /* Energy decay */ 1.398 + opus_val16 decay = loss_count==0 ? 1.399 + QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT); 1.400 + c=0; do 1.401 + { 1.402 + for (i=start;i<end;i++) 1.403 + oldBandE[c*nbEBands+i] -= decay; 1.404 + } while (++c<C); 1.405 + plcLogE = oldBandE; 1.406 + } 1.407 + seed = st->rng; 1.408 + for (c=0;c<C;c++) 1.409 + { 1.410 + for (i=start;i<effEnd;i++) 1.411 + { 1.412 + int j; 1.413 + int boffs; 1.414 + int blen; 1.415 + boffs = N*c+(eBands[i]<<LM); 1.416 + blen = (eBands[i+1]-eBands[i])<<LM; 1.417 + for (j=0;j<blen;j++) 1.418 + { 1.419 + seed = celt_lcg_rand(seed); 1.420 + X[boffs+j] = (celt_norm)((opus_int32)seed>>20); 1.421 + } 1.422 + renormalise_vector(X+boffs, blen, Q15ONE); 1.423 + } 1.424 + } 1.425 + st->rng = seed; 1.426 + 1.427 + denormalise_bands(mode, X, freq, plcLogE, start, effEnd, C, 1<<LM); 1.428 + 1.429 + c=0; do { 1.430 + int bound = eBands[effEnd]<<LM; 1.431 + if (downsample!=1) 1.432 + bound = IMIN(bound, N/downsample); 1.433 + for (i=bound;i<N;i++) 1.434 + freq[c*N+i] = 0; 1.435 + } while (++c<C); 1.436 + c=0; do { 1.437 + OPUS_MOVE(decode_mem[c], decode_mem[c]+N, 1.438 + DECODE_BUFFER_SIZE-N+(overlap>>1)); 1.439 + } while (++c<C); 1.440 + compute_inv_mdcts(mode, 0, freq, out_syn, C, LM); 1.441 + } else { 1.442 + /* Pitch-based PLC */ 1.443 + const opus_val16 *window; 1.444 + opus_val16 fade = Q15ONE; 1.445 + int pitch_index; 1.446 + VARDECL(opus_val32, etmp); 1.447 + VARDECL(opus_val16, exc); 1.448 + 1.449 + if (loss_count == 0) 1.450 + { 1.451 + VARDECL( opus_val16, lp_pitch_buf ); 1.452 + ALLOC( lp_pitch_buf, DECODE_BUFFER_SIZE>>1, opus_val16 ); 1.453 + pitch_downsample(decode_mem, lp_pitch_buf, 1.454 + DECODE_BUFFER_SIZE, C, st->arch); 1.455 + pitch_search(lp_pitch_buf+(PLC_PITCH_LAG_MAX>>1), lp_pitch_buf, 1.456 + DECODE_BUFFER_SIZE-PLC_PITCH_LAG_MAX, 1.457 + PLC_PITCH_LAG_MAX-PLC_PITCH_LAG_MIN, &pitch_index, st->arch); 1.458 + pitch_index = PLC_PITCH_LAG_MAX-pitch_index; 1.459 + st->last_pitch_index = pitch_index; 1.460 + } else { 1.461 + pitch_index = st->last_pitch_index; 1.462 + fade = QCONST16(.8f,15); 1.463 + } 1.464 + 1.465 + ALLOC(etmp, overlap, opus_val32); 1.466 + ALLOC(exc, MAX_PERIOD, opus_val16); 1.467 + window = mode->window; 1.468 + c=0; do { 1.469 + opus_val16 decay; 1.470 + opus_val16 attenuation; 1.471 + opus_val32 S1=0; 1.472 + celt_sig *buf; 1.473 + int extrapolation_offset; 1.474 + int extrapolation_len; 1.475 + int exc_length; 1.476 + int j; 1.477 + 1.478 + buf = decode_mem[c]; 1.479 + for (i=0;i<MAX_PERIOD;i++) { 1.480 + exc[i] = ROUND16(buf[DECODE_BUFFER_SIZE-MAX_PERIOD+i], SIG_SHIFT); 1.481 + } 1.482 + 1.483 + if (loss_count == 0) 1.484 + { 1.485 + opus_val32 ac[LPC_ORDER+1]; 1.486 + /* Compute LPC coefficients for the last MAX_PERIOD samples before 1.487 + the first loss so we can work in the excitation-filter domain. */ 1.488 + _celt_autocorr(exc, ac, window, overlap, 1.489 + LPC_ORDER, MAX_PERIOD, st->arch); 1.490 + /* Add a noise floor of -40 dB. */ 1.491 +#ifdef FIXED_POINT 1.492 + ac[0] += SHR32(ac[0],13); 1.493 +#else 1.494 + ac[0] *= 1.0001f; 1.495 +#endif 1.496 + /* Use lag windowing to stabilize the Levinson-Durbin recursion. */ 1.497 + for (i=1;i<=LPC_ORDER;i++) 1.498 + { 1.499 + /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ 1.500 +#ifdef FIXED_POINT 1.501 + ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); 1.502 +#else 1.503 + ac[i] -= ac[i]*(0.008f*0.008f)*i*i; 1.504 +#endif 1.505 + } 1.506 + _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); 1.507 + } 1.508 + /* We want the excitation for 2 pitch periods in order to look for a 1.509 + decaying signal, but we can't get more than MAX_PERIOD. */ 1.510 + exc_length = IMIN(2*pitch_index, MAX_PERIOD); 1.511 + /* Initialize the LPC history with the samples just before the start 1.512 + of the region for which we're computing the excitation. */ 1.513 + { 1.514 + opus_val16 lpc_mem[LPC_ORDER]; 1.515 + for (i=0;i<LPC_ORDER;i++) 1.516 + { 1.517 + lpc_mem[i] = 1.518 + ROUND16(buf[DECODE_BUFFER_SIZE-exc_length-1-i], SIG_SHIFT); 1.519 + } 1.520 + /* Compute the excitation for exc_length samples before the loss. */ 1.521 + celt_fir(exc+MAX_PERIOD-exc_length, lpc+c*LPC_ORDER, 1.522 + exc+MAX_PERIOD-exc_length, exc_length, LPC_ORDER, lpc_mem); 1.523 + } 1.524 + 1.525 + /* Check if the waveform is decaying, and if so how fast. 1.526 + We do this to avoid adding energy when concealing in a segment 1.527 + with decaying energy. */ 1.528 + { 1.529 + opus_val32 E1=1, E2=1; 1.530 + int decay_length; 1.531 +#ifdef FIXED_POINT 1.532 + int shift = IMAX(0,2*celt_zlog2(celt_maxabs16(&exc[MAX_PERIOD-exc_length], exc_length))-20); 1.533 +#endif 1.534 + decay_length = exc_length>>1; 1.535 + for (i=0;i<decay_length;i++) 1.536 + { 1.537 + opus_val16 e; 1.538 + e = exc[MAX_PERIOD-decay_length+i]; 1.539 + E1 += SHR32(MULT16_16(e, e), shift); 1.540 + e = exc[MAX_PERIOD-2*decay_length+i]; 1.541 + E2 += SHR32(MULT16_16(e, e), shift); 1.542 + } 1.543 + E1 = MIN32(E1, E2); 1.544 + decay = celt_sqrt(frac_div32(SHR32(E1, 1), E2)); 1.545 + } 1.546 + 1.547 + /* Move the decoder memory one frame to the left to give us room to 1.548 + add the data for the new frame. We ignore the overlap that extends 1.549 + past the end of the buffer, because we aren't going to use it. */ 1.550 + OPUS_MOVE(buf, buf+N, DECODE_BUFFER_SIZE-N); 1.551 + 1.552 + /* Extrapolate from the end of the excitation with a period of 1.553 + "pitch_index", scaling down each period by an additional factor of 1.554 + "decay". */ 1.555 + extrapolation_offset = MAX_PERIOD-pitch_index; 1.556 + /* We need to extrapolate enough samples to cover a complete MDCT 1.557 + window (including overlap/2 samples on both sides). */ 1.558 + extrapolation_len = N+overlap; 1.559 + /* We also apply fading if this is not the first loss. */ 1.560 + attenuation = MULT16_16_Q15(fade, decay); 1.561 + for (i=j=0;i<extrapolation_len;i++,j++) 1.562 + { 1.563 + opus_val16 tmp; 1.564 + if (j >= pitch_index) { 1.565 + j -= pitch_index; 1.566 + attenuation = MULT16_16_Q15(attenuation, decay); 1.567 + } 1.568 + buf[DECODE_BUFFER_SIZE-N+i] = 1.569 + SHL32(EXTEND32(MULT16_16_Q15(attenuation, 1.570 + exc[extrapolation_offset+j])), SIG_SHIFT); 1.571 + /* Compute the energy of the previously decoded signal whose 1.572 + excitation we're copying. */ 1.573 + tmp = ROUND16( 1.574 + buf[DECODE_BUFFER_SIZE-MAX_PERIOD-N+extrapolation_offset+j], 1.575 + SIG_SHIFT); 1.576 + S1 += SHR32(MULT16_16(tmp, tmp), 8); 1.577 + } 1.578 + 1.579 + { 1.580 + opus_val16 lpc_mem[LPC_ORDER]; 1.581 + /* Copy the last decoded samples (prior to the overlap region) to 1.582 + synthesis filter memory so we can have a continuous signal. */ 1.583 + for (i=0;i<LPC_ORDER;i++) 1.584 + lpc_mem[i] = ROUND16(buf[DECODE_BUFFER_SIZE-N-1-i], SIG_SHIFT); 1.585 + /* Apply the synthesis filter to convert the excitation back into 1.586 + the signal domain. */ 1.587 + celt_iir(buf+DECODE_BUFFER_SIZE-N, lpc+c*LPC_ORDER, 1.588 + buf+DECODE_BUFFER_SIZE-N, extrapolation_len, LPC_ORDER, 1.589 + lpc_mem); 1.590 + } 1.591 + 1.592 + /* Check if the synthesis energy is higher than expected, which can 1.593 + happen with the signal changes during our window. If so, 1.594 + attenuate. */ 1.595 + { 1.596 + opus_val32 S2=0; 1.597 + for (i=0;i<extrapolation_len;i++) 1.598 + { 1.599 + opus_val16 tmp = ROUND16(buf[DECODE_BUFFER_SIZE-N+i], SIG_SHIFT); 1.600 + S2 += SHR32(MULT16_16(tmp, tmp), 8); 1.601 + } 1.602 + /* This checks for an "explosion" in the synthesis. */ 1.603 +#ifdef FIXED_POINT 1.604 + if (!(S1 > SHR32(S2,2))) 1.605 +#else 1.606 + /* The float test is written this way to catch NaNs in the output 1.607 + of the IIR filter at the same time. */ 1.608 + if (!(S1 > 0.2f*S2)) 1.609 +#endif 1.610 + { 1.611 + for (i=0;i<extrapolation_len;i++) 1.612 + buf[DECODE_BUFFER_SIZE-N+i] = 0; 1.613 + } else if (S1 < S2) 1.614 + { 1.615 + opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); 1.616 + for (i=0;i<overlap;i++) 1.617 + { 1.618 + opus_val16 tmp_g = Q15ONE 1.619 + - MULT16_16_Q15(window[i], Q15ONE-ratio); 1.620 + buf[DECODE_BUFFER_SIZE-N+i] = 1.621 + MULT16_32_Q15(tmp_g, buf[DECODE_BUFFER_SIZE-N+i]); 1.622 + } 1.623 + for (i=overlap;i<extrapolation_len;i++) 1.624 + { 1.625 + buf[DECODE_BUFFER_SIZE-N+i] = 1.626 + MULT16_32_Q15(ratio, buf[DECODE_BUFFER_SIZE-N+i]); 1.627 + } 1.628 + } 1.629 + } 1.630 + 1.631 + /* Apply the pre-filter to the MDCT overlap for the next frame because 1.632 + the post-filter will be re-applied in the decoder after the MDCT 1.633 + overlap. */ 1.634 + comb_filter(etmp, buf+DECODE_BUFFER_SIZE, 1.635 + st->postfilter_period, st->postfilter_period, overlap, 1.636 + -st->postfilter_gain, -st->postfilter_gain, 1.637 + st->postfilter_tapset, st->postfilter_tapset, NULL, 0); 1.638 + 1.639 + /* Simulate TDAC on the concealed audio so that it blends with the 1.640 + MDCT of the next frame. */ 1.641 + for (i=0;i<overlap/2;i++) 1.642 + { 1.643 + buf[DECODE_BUFFER_SIZE+i] = 1.644 + MULT16_32_Q15(window[i], etmp[overlap-1-i]) 1.645 + + MULT16_32_Q15(window[overlap-i-1], etmp[i]); 1.646 + } 1.647 + } while (++c<C); 1.648 + } 1.649 + 1.650 + deemphasis(out_syn, pcm, N, C, downsample, 1.651 + mode->preemph, st->preemph_memD, scratch); 1.652 + 1.653 + st->loss_count = loss_count+1; 1.654 + 1.655 + RESTORE_STACK; 1.656 +} 1.657 + 1.658 +int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec) 1.659 +{ 1.660 + int c, i, N; 1.661 + int spread_decision; 1.662 + opus_int32 bits; 1.663 + ec_dec _dec; 1.664 + VARDECL(celt_sig, freq); 1.665 + VARDECL(celt_norm, X); 1.666 + VARDECL(int, fine_quant); 1.667 + VARDECL(int, pulses); 1.668 + VARDECL(int, cap); 1.669 + VARDECL(int, offsets); 1.670 + VARDECL(int, fine_priority); 1.671 + VARDECL(int, tf_res); 1.672 + VARDECL(unsigned char, collapse_masks); 1.673 + celt_sig *decode_mem[2]; 1.674 + celt_sig *out_syn[2]; 1.675 + opus_val16 *lpc; 1.676 + opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; 1.677 + 1.678 + int shortBlocks; 1.679 + int isTransient; 1.680 + int intra_ener; 1.681 + const int CC = st->channels; 1.682 + int LM, M; 1.683 + int effEnd; 1.684 + int codedBands; 1.685 + int alloc_trim; 1.686 + int postfilter_pitch; 1.687 + opus_val16 postfilter_gain; 1.688 + int intensity=0; 1.689 + int dual_stereo=0; 1.690 + opus_int32 total_bits; 1.691 + opus_int32 balance; 1.692 + opus_int32 tell; 1.693 + int dynalloc_logp; 1.694 + int postfilter_tapset; 1.695 + int anti_collapse_rsv; 1.696 + int anti_collapse_on=0; 1.697 + int silence; 1.698 + int C = st->stream_channels; 1.699 + const OpusCustomMode *mode; 1.700 + int nbEBands; 1.701 + int overlap; 1.702 + const opus_int16 *eBands; 1.703 + ALLOC_STACK; 1.704 + 1.705 + mode = st->mode; 1.706 + nbEBands = mode->nbEBands; 1.707 + overlap = mode->overlap; 1.708 + eBands = mode->eBands; 1.709 + frame_size *= st->downsample; 1.710 + 1.711 + c=0; do { 1.712 + decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap); 1.713 + } while (++c<CC); 1.714 + lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC); 1.715 + oldBandE = lpc+CC*LPC_ORDER; 1.716 + oldLogE = oldBandE + 2*nbEBands; 1.717 + oldLogE2 = oldLogE + 2*nbEBands; 1.718 + backgroundLogE = oldLogE2 + 2*nbEBands; 1.719 + 1.720 +#ifdef CUSTOM_MODES 1.721 + if (st->signalling && data!=NULL) 1.722 + { 1.723 + int data0=data[0]; 1.724 + /* Convert "standard mode" to Opus header */ 1.725 + if (mode->Fs==48000 && mode->shortMdctSize==120) 1.726 + { 1.727 + data0 = fromOpus(data0); 1.728 + if (data0<0) 1.729 + return OPUS_INVALID_PACKET; 1.730 + } 1.731 + st->end = IMAX(1, mode->effEBands-2*(data0>>5)); 1.732 + LM = (data0>>3)&0x3; 1.733 + C = 1 + ((data0>>2)&0x1); 1.734 + data++; 1.735 + len--; 1.736 + if (LM>mode->maxLM) 1.737 + return OPUS_INVALID_PACKET; 1.738 + if (frame_size < mode->shortMdctSize<<LM) 1.739 + return OPUS_BUFFER_TOO_SMALL; 1.740 + else 1.741 + frame_size = mode->shortMdctSize<<LM; 1.742 + } else { 1.743 +#else 1.744 + { 1.745 +#endif 1.746 + for (LM=0;LM<=mode->maxLM;LM++) 1.747 + if (mode->shortMdctSize<<LM==frame_size) 1.748 + break; 1.749 + if (LM>mode->maxLM) 1.750 + return OPUS_BAD_ARG; 1.751 + } 1.752 + M=1<<LM; 1.753 + 1.754 + if (len<0 || len>1275 || pcm==NULL) 1.755 + return OPUS_BAD_ARG; 1.756 + 1.757 + N = M*mode->shortMdctSize; 1.758 + 1.759 + effEnd = st->end; 1.760 + if (effEnd > mode->effEBands) 1.761 + effEnd = mode->effEBands; 1.762 + 1.763 + if (data == NULL || len<=1) 1.764 + { 1.765 + celt_decode_lost(st, pcm, N, LM); 1.766 + RESTORE_STACK; 1.767 + return frame_size/st->downsample; 1.768 + } 1.769 + 1.770 + if (dec == NULL) 1.771 + { 1.772 + ec_dec_init(&_dec,(unsigned char*)data,len); 1.773 + dec = &_dec; 1.774 + } 1.775 + 1.776 + if (C==1) 1.777 + { 1.778 + for (i=0;i<nbEBands;i++) 1.779 + oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]); 1.780 + } 1.781 + 1.782 + total_bits = len*8; 1.783 + tell = ec_tell(dec); 1.784 + 1.785 + if (tell >= total_bits) 1.786 + silence = 1; 1.787 + else if (tell==1) 1.788 + silence = ec_dec_bit_logp(dec, 15); 1.789 + else 1.790 + silence = 0; 1.791 + if (silence) 1.792 + { 1.793 + /* Pretend we've read all the remaining bits */ 1.794 + tell = len*8; 1.795 + dec->nbits_total+=tell-ec_tell(dec); 1.796 + } 1.797 + 1.798 + postfilter_gain = 0; 1.799 + postfilter_pitch = 0; 1.800 + postfilter_tapset = 0; 1.801 + if (st->start==0 && tell+16 <= total_bits) 1.802 + { 1.803 + if(ec_dec_bit_logp(dec, 1)) 1.804 + { 1.805 + int qg, octave; 1.806 + octave = ec_dec_uint(dec, 6); 1.807 + postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; 1.808 + qg = ec_dec_bits(dec, 3); 1.809 + if (ec_tell(dec)+2<=total_bits) 1.810 + postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); 1.811 + postfilter_gain = QCONST16(.09375f,15)*(qg+1); 1.812 + } 1.813 + tell = ec_tell(dec); 1.814 + } 1.815 + 1.816 + if (LM > 0 && tell+3 <= total_bits) 1.817 + { 1.818 + isTransient = ec_dec_bit_logp(dec, 3); 1.819 + tell = ec_tell(dec); 1.820 + } 1.821 + else 1.822 + isTransient = 0; 1.823 + 1.824 + if (isTransient) 1.825 + shortBlocks = M; 1.826 + else 1.827 + shortBlocks = 0; 1.828 + 1.829 + /* Decode the global flags (first symbols in the stream) */ 1.830 + intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; 1.831 + /* Get band energies */ 1.832 + unquant_coarse_energy(mode, st->start, st->end, oldBandE, 1.833 + intra_ener, dec, C, LM); 1.834 + 1.835 + ALLOC(tf_res, nbEBands, int); 1.836 + tf_decode(st->start, st->end, isTransient, tf_res, LM, dec); 1.837 + 1.838 + tell = ec_tell(dec); 1.839 + spread_decision = SPREAD_NORMAL; 1.840 + if (tell+4 <= total_bits) 1.841 + spread_decision = ec_dec_icdf(dec, spread_icdf, 5); 1.842 + 1.843 + ALLOC(cap, nbEBands, int); 1.844 + 1.845 + init_caps(mode,cap,LM,C); 1.846 + 1.847 + ALLOC(offsets, nbEBands, int); 1.848 + 1.849 + dynalloc_logp = 6; 1.850 + total_bits<<=BITRES; 1.851 + tell = ec_tell_frac(dec); 1.852 + for (i=st->start;i<st->end;i++) 1.853 + { 1.854 + int width, quanta; 1.855 + int dynalloc_loop_logp; 1.856 + int boost; 1.857 + width = C*(eBands[i+1]-eBands[i])<<LM; 1.858 + /* quanta is 6 bits, but no more than 1 bit/sample 1.859 + and no less than 1/8 bit/sample */ 1.860 + quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); 1.861 + dynalloc_loop_logp = dynalloc_logp; 1.862 + boost = 0; 1.863 + while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) 1.864 + { 1.865 + int flag; 1.866 + flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); 1.867 + tell = ec_tell_frac(dec); 1.868 + if (!flag) 1.869 + break; 1.870 + boost += quanta; 1.871 + total_bits -= quanta; 1.872 + dynalloc_loop_logp = 1; 1.873 + } 1.874 + offsets[i] = boost; 1.875 + /* Making dynalloc more likely */ 1.876 + if (boost>0) 1.877 + dynalloc_logp = IMAX(2, dynalloc_logp-1); 1.878 + } 1.879 + 1.880 + ALLOC(fine_quant, nbEBands, int); 1.881 + alloc_trim = tell+(6<<BITRES) <= total_bits ? 1.882 + ec_dec_icdf(dec, trim_icdf, 7) : 5; 1.883 + 1.884 + bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1; 1.885 + anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; 1.886 + bits -= anti_collapse_rsv; 1.887 + 1.888 + ALLOC(pulses, nbEBands, int); 1.889 + ALLOC(fine_priority, nbEBands, int); 1.890 + 1.891 + codedBands = compute_allocation(mode, st->start, st->end, offsets, cap, 1.892 + alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, 1.893 + fine_quant, fine_priority, C, LM, dec, 0, 0, 0); 1.894 + 1.895 + unquant_fine_energy(mode, st->start, st->end, oldBandE, fine_quant, dec, C); 1.896 + 1.897 + /* Decode fixed codebook */ 1.898 + ALLOC(collapse_masks, C*nbEBands, unsigned char); 1.899 + ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ 1.900 + 1.901 + quant_all_bands(0, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, 1.902 + NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, 1.903 + len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng); 1.904 + 1.905 + if (anti_collapse_rsv > 0) 1.906 + { 1.907 + anti_collapse_on = ec_dec_bits(dec, 1); 1.908 + } 1.909 + 1.910 + unquant_energy_finalise(mode, st->start, st->end, oldBandE, 1.911 + fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); 1.912 + 1.913 + if (anti_collapse_on) 1.914 + anti_collapse(mode, X, collapse_masks, LM, C, N, 1.915 + st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); 1.916 + 1.917 + ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */ 1.918 + 1.919 + if (silence) 1.920 + { 1.921 + for (i=0;i<C*nbEBands;i++) 1.922 + oldBandE[i] = -QCONST16(28.f,DB_SHIFT); 1.923 + for (i=0;i<C*N;i++) 1.924 + freq[i] = 0; 1.925 + } else { 1.926 + /* Synthesis */ 1.927 + denormalise_bands(mode, X, freq, oldBandE, st->start, effEnd, C, M); 1.928 + } 1.929 + c=0; do { 1.930 + OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap/2); 1.931 + } while (++c<CC); 1.932 + 1.933 + c=0; do { 1.934 + int bound = M*eBands[effEnd]; 1.935 + if (st->downsample!=1) 1.936 + bound = IMIN(bound, N/st->downsample); 1.937 + for (i=bound;i<N;i++) 1.938 + freq[c*N+i] = 0; 1.939 + } while (++c<C); 1.940 + 1.941 + c=0; do { 1.942 + out_syn[c] = decode_mem[c]+DECODE_BUFFER_SIZE-N; 1.943 + } while (++c<CC); 1.944 + 1.945 + if (CC==2&&C==1) 1.946 + { 1.947 + for (i=0;i<N;i++) 1.948 + freq[N+i] = freq[i]; 1.949 + } 1.950 + if (CC==1&&C==2) 1.951 + { 1.952 + for (i=0;i<N;i++) 1.953 + freq[i] = HALF32(ADD32(freq[i],freq[N+i])); 1.954 + } 1.955 + 1.956 + /* Compute inverse MDCTs */ 1.957 + compute_inv_mdcts(mode, shortBlocks, freq, out_syn, CC, LM); 1.958 + 1.959 + c=0; do { 1.960 + st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); 1.961 + st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); 1.962 + comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, 1.963 + st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, 1.964 + mode->window, overlap); 1.965 + if (LM!=0) 1.966 + comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, 1.967 + st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, 1.968 + mode->window, overlap); 1.969 + 1.970 + } while (++c<CC); 1.971 + st->postfilter_period_old = st->postfilter_period; 1.972 + st->postfilter_gain_old = st->postfilter_gain; 1.973 + st->postfilter_tapset_old = st->postfilter_tapset; 1.974 + st->postfilter_period = postfilter_pitch; 1.975 + st->postfilter_gain = postfilter_gain; 1.976 + st->postfilter_tapset = postfilter_tapset; 1.977 + if (LM!=0) 1.978 + { 1.979 + st->postfilter_period_old = st->postfilter_period; 1.980 + st->postfilter_gain_old = st->postfilter_gain; 1.981 + st->postfilter_tapset_old = st->postfilter_tapset; 1.982 + } 1.983 + 1.984 + if (C==1) { 1.985 + for (i=0;i<nbEBands;i++) 1.986 + oldBandE[nbEBands+i]=oldBandE[i]; 1.987 + } 1.988 + 1.989 + /* In case start or end were to change */ 1.990 + if (!isTransient) 1.991 + { 1.992 + for (i=0;i<2*nbEBands;i++) 1.993 + oldLogE2[i] = oldLogE[i]; 1.994 + for (i=0;i<2*nbEBands;i++) 1.995 + oldLogE[i] = oldBandE[i]; 1.996 + for (i=0;i<2*nbEBands;i++) 1.997 + backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]); 1.998 + } else { 1.999 + for (i=0;i<2*nbEBands;i++) 1.1000 + oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); 1.1001 + } 1.1002 + c=0; do 1.1003 + { 1.1004 + for (i=0;i<st->start;i++) 1.1005 + { 1.1006 + oldBandE[c*nbEBands+i]=0; 1.1007 + oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); 1.1008 + } 1.1009 + for (i=st->end;i<nbEBands;i++) 1.1010 + { 1.1011 + oldBandE[c*nbEBands+i]=0; 1.1012 + oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); 1.1013 + } 1.1014 + } while (++c<2); 1.1015 + st->rng = dec->rng; 1.1016 + 1.1017 + /* We reuse freq[] as scratch space for the de-emphasis */ 1.1018 + deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, freq); 1.1019 + st->loss_count = 0; 1.1020 + RESTORE_STACK; 1.1021 + if (ec_tell(dec) > 8*len) 1.1022 + return OPUS_INTERNAL_ERROR; 1.1023 + if(ec_get_error(dec)) 1.1024 + st->error = 1; 1.1025 + return frame_size/st->downsample; 1.1026 +} 1.1027 + 1.1028 + 1.1029 +#ifdef CUSTOM_MODES 1.1030 + 1.1031 +#ifdef FIXED_POINT 1.1032 +int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) 1.1033 +{ 1.1034 + return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); 1.1035 +} 1.1036 + 1.1037 +#ifndef DISABLE_FLOAT_API 1.1038 +int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) 1.1039 +{ 1.1040 + int j, ret, C, N; 1.1041 + VARDECL(opus_int16, out); 1.1042 + ALLOC_STACK; 1.1043 + 1.1044 + if (pcm==NULL) 1.1045 + return OPUS_BAD_ARG; 1.1046 + 1.1047 + C = st->channels; 1.1048 + N = frame_size; 1.1049 + 1.1050 + ALLOC(out, C*N, opus_int16); 1.1051 + ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); 1.1052 + if (ret>0) 1.1053 + for (j=0;j<C*ret;j++) 1.1054 + pcm[j]=out[j]*(1.f/32768.f); 1.1055 + 1.1056 + RESTORE_STACK; 1.1057 + return ret; 1.1058 +} 1.1059 +#endif /* DISABLE_FLOAT_API */ 1.1060 + 1.1061 +#else 1.1062 + 1.1063 +int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) 1.1064 +{ 1.1065 + return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); 1.1066 +} 1.1067 + 1.1068 +int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) 1.1069 +{ 1.1070 + int j, ret, C, N; 1.1071 + VARDECL(celt_sig, out); 1.1072 + ALLOC_STACK; 1.1073 + 1.1074 + if (pcm==NULL) 1.1075 + return OPUS_BAD_ARG; 1.1076 + 1.1077 + C = st->channels; 1.1078 + N = frame_size; 1.1079 + ALLOC(out, C*N, celt_sig); 1.1080 + 1.1081 + ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); 1.1082 + 1.1083 + if (ret>0) 1.1084 + for (j=0;j<C*ret;j++) 1.1085 + pcm[j] = FLOAT2INT16 (out[j]); 1.1086 + 1.1087 + RESTORE_STACK; 1.1088 + return ret; 1.1089 +} 1.1090 + 1.1091 +#endif 1.1092 +#endif /* CUSTOM_MODES */ 1.1093 + 1.1094 +int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...) 1.1095 +{ 1.1096 + va_list ap; 1.1097 + 1.1098 + va_start(ap, request); 1.1099 + switch (request) 1.1100 + { 1.1101 + case CELT_SET_START_BAND_REQUEST: 1.1102 + { 1.1103 + opus_int32 value = va_arg(ap, opus_int32); 1.1104 + if (value<0 || value>=st->mode->nbEBands) 1.1105 + goto bad_arg; 1.1106 + st->start = value; 1.1107 + } 1.1108 + break; 1.1109 + case CELT_SET_END_BAND_REQUEST: 1.1110 + { 1.1111 + opus_int32 value = va_arg(ap, opus_int32); 1.1112 + if (value<1 || value>st->mode->nbEBands) 1.1113 + goto bad_arg; 1.1114 + st->end = value; 1.1115 + } 1.1116 + break; 1.1117 + case CELT_SET_CHANNELS_REQUEST: 1.1118 + { 1.1119 + opus_int32 value = va_arg(ap, opus_int32); 1.1120 + if (value<1 || value>2) 1.1121 + goto bad_arg; 1.1122 + st->stream_channels = value; 1.1123 + } 1.1124 + break; 1.1125 + case CELT_GET_AND_CLEAR_ERROR_REQUEST: 1.1126 + { 1.1127 + opus_int32 *value = va_arg(ap, opus_int32*); 1.1128 + if (value==NULL) 1.1129 + goto bad_arg; 1.1130 + *value=st->error; 1.1131 + st->error = 0; 1.1132 + } 1.1133 + break; 1.1134 + case OPUS_GET_LOOKAHEAD_REQUEST: 1.1135 + { 1.1136 + opus_int32 *value = va_arg(ap, opus_int32*); 1.1137 + if (value==NULL) 1.1138 + goto bad_arg; 1.1139 + *value = st->overlap/st->downsample; 1.1140 + } 1.1141 + break; 1.1142 + case OPUS_RESET_STATE: 1.1143 + { 1.1144 + int i; 1.1145 + opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2; 1.1146 + lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels); 1.1147 + oldBandE = lpc+st->channels*LPC_ORDER; 1.1148 + oldLogE = oldBandE + 2*st->mode->nbEBands; 1.1149 + oldLogE2 = oldLogE + 2*st->mode->nbEBands; 1.1150 + OPUS_CLEAR((char*)&st->DECODER_RESET_START, 1.1151 + opus_custom_decoder_get_size(st->mode, st->channels)- 1.1152 + ((char*)&st->DECODER_RESET_START - (char*)st)); 1.1153 + for (i=0;i<2*st->mode->nbEBands;i++) 1.1154 + oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); 1.1155 + } 1.1156 + break; 1.1157 + case OPUS_GET_PITCH_REQUEST: 1.1158 + { 1.1159 + opus_int32 *value = va_arg(ap, opus_int32*); 1.1160 + if (value==NULL) 1.1161 + goto bad_arg; 1.1162 + *value = st->postfilter_period; 1.1163 + } 1.1164 + break; 1.1165 + case CELT_GET_MODE_REQUEST: 1.1166 + { 1.1167 + const CELTMode ** value = va_arg(ap, const CELTMode**); 1.1168 + if (value==0) 1.1169 + goto bad_arg; 1.1170 + *value=st->mode; 1.1171 + } 1.1172 + break; 1.1173 + case CELT_SET_SIGNALLING_REQUEST: 1.1174 + { 1.1175 + opus_int32 value = va_arg(ap, opus_int32); 1.1176 + st->signalling = value; 1.1177 + } 1.1178 + break; 1.1179 + case OPUS_GET_FINAL_RANGE_REQUEST: 1.1180 + { 1.1181 + opus_uint32 * value = va_arg(ap, opus_uint32 *); 1.1182 + if (value==0) 1.1183 + goto bad_arg; 1.1184 + *value=st->rng; 1.1185 + } 1.1186 + break; 1.1187 + default: 1.1188 + goto bad_request; 1.1189 + } 1.1190 + va_end(ap); 1.1191 + return OPUS_OK; 1.1192 +bad_arg: 1.1193 + va_end(ap); 1.1194 + return OPUS_BAD_ARG; 1.1195 +bad_request: 1.1196 + va_end(ap); 1.1197 + return OPUS_UNIMPLEMENTED; 1.1198 +}