Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
| 2 | Copyright (c) 2007-2010 Xiph.Org Foundation |
| 3 | Copyright (c) 2008 Gregory Maxwell |
| 4 | Written by Jean-Marc Valin and Gregory Maxwell */ |
| 5 | /* |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions |
| 8 | are met: |
| 9 | |
| 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | |
| 13 | - Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the |
| 15 | documentation and/or other materials provided with the distribution. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 21 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include "config.h" |
| 32 | #endif |
| 33 | |
Jean-Marc Valin | 1ecb7ea | 2012-11-08 11:25:20 -0500 | [diff] [blame] | 34 | #define CELT_DECODER_C |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 35 | |
| 36 | #include "os_support.h" |
| 37 | #include "mdct.h" |
| 38 | #include <math.h> |
| 39 | #include "celt.h" |
| 40 | #include "pitch.h" |
| 41 | #include "bands.h" |
| 42 | #include "modes.h" |
| 43 | #include "entcode.h" |
| 44 | #include "quant_bands.h" |
| 45 | #include "rate.h" |
| 46 | #include "stack_alloc.h" |
| 47 | #include "mathops.h" |
| 48 | #include "float_cast.h" |
| 49 | #include <stdarg.h> |
| 50 | #include "celt_lpc.h" |
| 51 | #include "vq.h" |
| 52 | |
| 53 | /**********************************************************************/ |
| 54 | /* */ |
| 55 | /* DECODER */ |
| 56 | /* */ |
| 57 | /**********************************************************************/ |
| 58 | #define DECODE_BUFFER_SIZE 2048 |
| 59 | |
| 60 | /** Decoder state |
| 61 | @brief Decoder state |
| 62 | */ |
| 63 | struct OpusCustomDecoder { |
| 64 | const OpusCustomMode *mode; |
| 65 | int overlap; |
| 66 | int channels; |
| 67 | int stream_channels; |
| 68 | |
| 69 | int downsample; |
| 70 | int start, end; |
| 71 | int signalling; |
| 72 | |
| 73 | /* Everything beyond this point gets cleared on a reset */ |
| 74 | #define DECODER_RESET_START rng |
| 75 | |
| 76 | opus_uint32 rng; |
| 77 | int error; |
| 78 | int last_pitch_index; |
| 79 | int loss_count; |
| 80 | int postfilter_period; |
| 81 | int postfilter_period_old; |
| 82 | opus_val16 postfilter_gain; |
| 83 | opus_val16 postfilter_gain_old; |
| 84 | int postfilter_tapset; |
| 85 | int postfilter_tapset_old; |
| 86 | |
| 87 | celt_sig preemph_memD[2]; |
| 88 | |
| 89 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ |
| 90 | /* opus_val16 lpc[], Size = channels*LPC_ORDER */ |
| 91 | /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */ |
| 92 | /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */ |
| 93 | /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */ |
| 94 | /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */ |
| 95 | }; |
| 96 | |
| 97 | int celt_decoder_get_size(int channels) |
| 98 | { |
| 99 | const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); |
| 100 | return opus_custom_decoder_get_size(mode, channels); |
| 101 | } |
| 102 | |
| 103 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels) |
| 104 | { |
| 105 | int size = sizeof(struct CELTDecoder) |
| 106 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) |
| 107 | + channels*LPC_ORDER*sizeof(opus_val16) |
| 108 | + 4*2*mode->nbEBands*sizeof(opus_val16); |
| 109 | return size; |
| 110 | } |
| 111 | |
| 112 | #ifdef CUSTOM_MODES |
| 113 | CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error) |
| 114 | { |
| 115 | int ret; |
| 116 | CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); |
| 117 | ret = opus_custom_decoder_init(st, mode, channels); |
| 118 | if (ret != OPUS_OK) |
| 119 | { |
| 120 | opus_custom_decoder_destroy(st); |
| 121 | st = NULL; |
| 122 | } |
| 123 | if (error) |
| 124 | *error = ret; |
| 125 | return st; |
| 126 | } |
| 127 | #endif /* CUSTOM_MODES */ |
| 128 | |
| 129 | int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels) |
| 130 | { |
| 131 | int ret; |
| 132 | ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); |
| 133 | if (ret != OPUS_OK) |
| 134 | return ret; |
| 135 | st->downsample = resampling_factor(sampling_rate); |
| 136 | if (st->downsample==0) |
| 137 | return OPUS_BAD_ARG; |
| 138 | else |
| 139 | return OPUS_OK; |
| 140 | } |
| 141 | |
| 142 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels) |
| 143 | { |
| 144 | if (channels < 0 || channels > 2) |
| 145 | return OPUS_BAD_ARG; |
| 146 | |
| 147 | if (st==NULL) |
| 148 | return OPUS_ALLOC_FAIL; |
| 149 | |
| 150 | OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels)); |
| 151 | |
| 152 | st->mode = mode; |
| 153 | st->overlap = mode->overlap; |
| 154 | st->stream_channels = st->channels = channels; |
| 155 | |
| 156 | st->downsample = 1; |
| 157 | st->start = 0; |
| 158 | st->end = st->mode->effEBands; |
| 159 | st->signalling = 1; |
| 160 | |
| 161 | st->loss_count = 0; |
| 162 | |
| 163 | opus_custom_decoder_ctl(st, OPUS_RESET_STATE); |
| 164 | |
| 165 | return OPUS_OK; |
| 166 | } |
| 167 | |
| 168 | #ifdef CUSTOM_MODES |
| 169 | void opus_custom_decoder_destroy(CELTDecoder *st) |
| 170 | { |
| 171 | opus_free(st); |
| 172 | } |
| 173 | #endif /* CUSTOM_MODES */ |
| 174 | |
| 175 | static inline opus_val16 SIG2WORD16(celt_sig x) |
| 176 | { |
| 177 | #ifdef FIXED_POINT |
| 178 | x = PSHR32(x, SIG_SHIFT); |
| 179 | x = MAX32(x, -32768); |
| 180 | x = MIN32(x, 32767); |
| 181 | return EXTRACT16(x); |
| 182 | #else |
| 183 | return (opus_val16)x; |
| 184 | #endif |
| 185 | } |
| 186 | |
| 187 | #ifndef RESYNTH |
| 188 | static |
| 189 | #endif |
| 190 | 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) |
| 191 | { |
| 192 | int c; |
| 193 | int Nd; |
| 194 | opus_val16 coef0, coef1; |
| 195 | |
| 196 | coef0 = coef[0]; |
| 197 | coef1 = coef[1]; |
| 198 | Nd = N/downsample; |
| 199 | c=0; do { |
| 200 | int j; |
| 201 | celt_sig * OPUS_RESTRICT x; |
| 202 | opus_val16 * OPUS_RESTRICT y; |
| 203 | celt_sig m = mem[c]; |
| 204 | x =in[c]; |
| 205 | y = pcm+c; |
| 206 | /* Shortcut for the standard (non-custom modes) case */ |
| 207 | if (coef1 == 0) |
| 208 | { |
| 209 | for (j=0;j<N;j++) |
| 210 | { |
| 211 | celt_sig tmp = x[j] + m; |
| 212 | m = MULT16_32_Q15(coef0, tmp); |
| 213 | scratch[j] = tmp; |
| 214 | } |
| 215 | } else { |
| 216 | opus_val16 coef3 = coef[3]; |
| 217 | for (j=0;j<N;j++) |
| 218 | { |
| 219 | celt_sig tmp = x[j] + m; |
| 220 | m = MULT16_32_Q15(coef0, tmp) |
| 221 | - MULT16_32_Q15(coef1, x[j]); |
| 222 | tmp = SHL32(MULT16_32_Q15(coef3, tmp), 2); |
| 223 | scratch[j] = tmp; |
| 224 | } |
| 225 | } |
| 226 | mem[c] = m; |
| 227 | |
| 228 | /* Perform down-sampling */ |
| 229 | for (j=0;j<Nd;j++) |
| 230 | y[j*C] = SCALEOUT(SIG2WORD16(scratch[j*downsample])); |
| 231 | } while (++c<C); |
| 232 | } |
| 233 | |
| 234 | /** Compute the IMDCT and apply window for all sub-frames and |
| 235 | all channels in a frame */ |
| 236 | #ifndef RESYNTH |
| 237 | static |
| 238 | #endif |
| 239 | void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, |
| 240 | celt_sig * OPUS_RESTRICT out_mem[], int C, int LM) |
| 241 | { |
| 242 | int b, c; |
| 243 | int B; |
| 244 | int N; |
| 245 | int shift; |
| 246 | const int overlap = OVERLAP(mode); |
| 247 | |
| 248 | if (shortBlocks) |
| 249 | { |
| 250 | B = shortBlocks; |
| 251 | N = mode->shortMdctSize; |
| 252 | shift = mode->maxLM; |
| 253 | } else { |
| 254 | B = 1; |
| 255 | N = mode->shortMdctSize<<LM; |
| 256 | shift = mode->maxLM-LM; |
| 257 | } |
| 258 | c=0; do { |
| 259 | /* IMDCT on the interleaved the sub-frames, overlap-add is performed by the IMDCT */ |
| 260 | for (b=0;b<B;b++) |
| 261 | clt_mdct_backward(&mode->mdct, &X[b+c*N*B], out_mem[c]+N*b, mode->window, overlap, shift, B); |
| 262 | } while (++c<C); |
| 263 | } |
| 264 | |
| 265 | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) |
| 266 | { |
| 267 | int i, curr, tf_select; |
| 268 | int tf_select_rsv; |
| 269 | int tf_changed; |
| 270 | int logp; |
| 271 | opus_uint32 budget; |
| 272 | opus_uint32 tell; |
| 273 | |
| 274 | budget = dec->storage*8; |
| 275 | tell = ec_tell(dec); |
| 276 | logp = isTransient ? 2 : 4; |
| 277 | tf_select_rsv = LM>0 && tell+logp+1<=budget; |
| 278 | budget -= tf_select_rsv; |
| 279 | tf_changed = curr = 0; |
| 280 | for (i=start;i<end;i++) |
| 281 | { |
| 282 | if (tell+logp<=budget) |
| 283 | { |
| 284 | curr ^= ec_dec_bit_logp(dec, logp); |
| 285 | tell = ec_tell(dec); |
| 286 | tf_changed |= curr; |
| 287 | } |
| 288 | tf_res[i] = curr; |
| 289 | logp = isTransient ? 4 : 5; |
| 290 | } |
| 291 | tf_select = 0; |
| 292 | if (tf_select_rsv && |
| 293 | tf_select_table[LM][4*isTransient+0+tf_changed] != |
| 294 | tf_select_table[LM][4*isTransient+2+tf_changed]) |
| 295 | { |
| 296 | tf_select = ec_dec_bit_logp(dec, 1); |
| 297 | } |
| 298 | for (i=start;i<end;i++) |
| 299 | { |
| 300 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | |
| 305 | static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM) |
| 306 | { |
| 307 | int c; |
| 308 | int pitch_index; |
| 309 | opus_val16 fade = Q15ONE; |
| 310 | int i, len; |
| 311 | const int C = st->channels; |
| 312 | int offset; |
| 313 | celt_sig *out_mem[2]; |
| 314 | celt_sig *decode_mem[2]; |
| 315 | opus_val16 *lpc; |
| 316 | opus_val32 *out_syn[2]; |
| 317 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; |
| 318 | const OpusCustomMode *mode; |
| 319 | int nbEBands; |
| 320 | int overlap; |
| 321 | const opus_int16 *eBands; |
| 322 | VARDECL(celt_sig, scratch); |
| 323 | SAVE_STACK; |
| 324 | |
| 325 | mode = st->mode; |
| 326 | nbEBands = mode->nbEBands; |
| 327 | overlap = mode->overlap; |
| 328 | eBands = mode->eBands; |
| 329 | |
| 330 | c=0; do { |
| 331 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 332 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 333 | } while (++c<C); |
| 334 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
| 335 | oldBandE = lpc+C*LPC_ORDER; |
| 336 | oldLogE = oldBandE + 2*nbEBands; |
| 337 | oldLogE2 = oldLogE + 2*nbEBands; |
| 338 | backgroundLogE = oldLogE2 + 2*nbEBands; |
| 339 | |
| 340 | c=0; do { |
| 341 | out_syn[c] = out_mem[c]+MAX_PERIOD-N; |
| 342 | } while (++c<C); |
| 343 | |
| 344 | len = N+overlap; |
| 345 | |
| 346 | if (st->loss_count >= 5 || st->start!=0) |
| 347 | { |
| 348 | /* Noise-based PLC/CNG */ |
| 349 | VARDECL(celt_sig, freq); |
| 350 | VARDECL(celt_norm, X); |
| 351 | VARDECL(celt_ener, bandE); |
| 352 | opus_uint32 seed; |
| 353 | int effEnd; |
| 354 | |
| 355 | effEnd = st->end; |
| 356 | if (effEnd > mode->effEBands) |
| 357 | effEnd = mode->effEBands; |
| 358 | |
| 359 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 360 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 361 | ALLOC(bandE, nbEBands*C, celt_ener); |
| 362 | |
| 363 | if (st->loss_count >= 5) |
| 364 | log2Amp(mode, st->start, st->end, bandE, backgroundLogE, C); |
| 365 | else { |
| 366 | /* Energy decay */ |
| 367 | opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT); |
| 368 | c=0; do |
| 369 | { |
| 370 | for (i=st->start;i<st->end;i++) |
| 371 | oldBandE[c*nbEBands+i] -= decay; |
| 372 | } while (++c<C); |
| 373 | log2Amp(mode, st->start, st->end, bandE, oldBandE, C); |
| 374 | } |
| 375 | seed = st->rng; |
| 376 | for (c=0;c<C;c++) |
| 377 | { |
| 378 | for (i=st->start;i<mode->effEBands;i++) |
| 379 | { |
| 380 | int j; |
| 381 | int boffs; |
| 382 | int blen; |
| 383 | boffs = N*c+(eBands[i]<<LM); |
| 384 | blen = (eBands[i+1]-eBands[i])<<LM; |
| 385 | for (j=0;j<blen;j++) |
| 386 | { |
| 387 | seed = celt_lcg_rand(seed); |
| 388 | X[boffs+j] = (celt_norm)((opus_int32)seed>>20); |
| 389 | } |
| 390 | renormalise_vector(X+boffs, blen, Q15ONE); |
| 391 | } |
| 392 | } |
| 393 | st->rng = seed; |
| 394 | |
| 395 | denormalise_bands(mode, X, freq, bandE, st->start, mode->effEBands, C, 1<<LM); |
| 396 | |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 397 | c=0; do { |
| 398 | int bound = eBands[effEnd]<<LM; |
| 399 | if (st->downsample!=1) |
| 400 | bound = IMIN(bound, N/st->downsample); |
| 401 | for (i=bound;i<N;i++) |
| 402 | freq[c*N+i] = 0; |
| 403 | } while (++c<C); |
| 404 | c=0; do { |
| 405 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap); |
| 406 | } while (++c<C); |
| 407 | compute_inv_mdcts(mode, 0, freq, out_syn, C, LM); |
| 408 | } else { |
| 409 | /* Pitch-based PLC */ |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 410 | VARDECL(opus_val32, etmp); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 411 | |
| 412 | if (st->loss_count == 0) |
| 413 | { |
| 414 | opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1]; |
| 415 | /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this |
| 416 | search by using only part of the decode buffer */ |
| 417 | int poffset = 720; |
| 418 | pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C); |
| 419 | /* Max pitch is 100 samples (480 Hz) */ |
| 420 | pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset, |
| 421 | poffset-100, &pitch_index); |
| 422 | pitch_index = poffset-pitch_index; |
| 423 | st->last_pitch_index = pitch_index; |
| 424 | } else { |
| 425 | pitch_index = st->last_pitch_index; |
| 426 | fade = QCONST16(.8f,15); |
| 427 | } |
| 428 | |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 429 | ALLOC(etmp, overlap, opus_val32); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 430 | c=0; do { |
| 431 | opus_val16 exc[MAX_PERIOD]; |
| 432 | opus_val32 ac[LPC_ORDER+1]; |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 433 | opus_val16 decay; |
| 434 | opus_val16 attenuation; |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 435 | opus_val32 S1=0; |
| 436 | opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 437 | opus_val32 *e = out_syn[c]; |
| 438 | |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 439 | |
| 440 | offset = MAX_PERIOD-pitch_index; |
| 441 | for (i=0;i<MAX_PERIOD;i++) |
| 442 | exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT); |
| 443 | |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 444 | /* Compute LPC coefficients for the last MAX_PERIOD samples before the loss so we can |
| 445 | work in the excitation-filter domain */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 446 | if (st->loss_count == 0) |
| 447 | { |
| 448 | _celt_autocorr(exc, ac, mode->window, overlap, |
| 449 | LPC_ORDER, MAX_PERIOD); |
| 450 | |
| 451 | /* Noise floor -40 dB */ |
| 452 | #ifdef FIXED_POINT |
| 453 | ac[0] += SHR32(ac[0],13); |
| 454 | #else |
| 455 | ac[0] *= 1.0001f; |
| 456 | #endif |
| 457 | /* Lag windowing */ |
| 458 | for (i=1;i<=LPC_ORDER;i++) |
| 459 | { |
| 460 | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ |
| 461 | #ifdef FIXED_POINT |
| 462 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
| 463 | #else |
| 464 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); |
| 465 | #endif |
| 466 | } |
| 467 | |
| 468 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); |
| 469 | } |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 470 | /* Samples just before the beginning of exc */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 471 | for (i=0;i<LPC_ORDER;i++) |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 472 | mem[i] = ROUND16(out_mem[c][-1-i], SIG_SHIFT); |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 473 | /* Compute the excitation for MAX_PERIOD samples before the loss */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 474 | celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem); |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 475 | |
| 476 | /* Check if the waveform is decaying (and if so how fast) |
| 477 | We do this to avoid adding energy when concealing in a segment |
| 478 | with decaying energy */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 479 | { |
| 480 | opus_val32 E1=1, E2=1; |
| 481 | int period; |
| 482 | if (pitch_index <= MAX_PERIOD/2) |
| 483 | period = pitch_index; |
| 484 | else |
| 485 | period = MAX_PERIOD/2; |
| 486 | for (i=0;i<period;i++) |
| 487 | { |
| 488 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); |
| 489 | E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8); |
| 490 | } |
| 491 | if (E1 > E2) |
| 492 | E1 = E2; |
| 493 | decay = celt_sqrt(frac_div32(SHR32(E1,1),E2)); |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 494 | attenuation = decay; |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 495 | } |
| 496 | |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 497 | /* Move memory one frame to the left */ |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 498 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap); |
| 499 | |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 500 | /* Extrapolate excitation with the right period, taking decay into account */ |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 501 | for (i=0;i<len;i++) |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 502 | { |
| 503 | opus_val16 tmp; |
| 504 | if (offset+i >= MAX_PERIOD) |
| 505 | { |
| 506 | offset -= pitch_index; |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 507 | attenuation = MULT16_16_Q15(attenuation, decay); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 508 | } |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 509 | e[i] = SHL32(EXTEND32(MULT16_16_Q15(attenuation, exc[offset+i])), SIG_SHIFT); |
| 510 | tmp = ROUND16(out_mem[c][-N+offset+i],SIG_SHIFT); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 511 | S1 += SHR32(MULT16_16(tmp,tmp),8); |
| 512 | } |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 513 | |
| 514 | /* Last samples correctly decoded so we can have a continuous signal */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 515 | for (i=0;i<LPC_ORDER;i++) |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 516 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-N-1-i], SIG_SHIFT); |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 517 | /* Apply the fading if not the first loss */ |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 518 | for (i=0;i<len;i++) |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 519 | e[i] = MULT16_32_Q15(fade, e[i]); |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 520 | /* Synthesis filter -- back in the signal domain */ |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 521 | celt_iir(e, lpc+c*LPC_ORDER, e, len, LPC_ORDER, mem); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 522 | |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 523 | /* Check if the synthesis energy is higher than expected, which can |
| 524 | happen with the signal changes during our window. If so, attenuate. */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 525 | { |
| 526 | opus_val32 S2=0; |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 527 | for (i=0;i<len;i++) |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 528 | { |
| 529 | opus_val16 tmp = ROUND16(e[i],SIG_SHIFT); |
| 530 | S2 += SHR32(MULT16_16(tmp,tmp),8); |
| 531 | } |
| 532 | /* This checks for an "explosion" in the synthesis */ |
| 533 | #ifdef FIXED_POINT |
| 534 | if (!(S1 > SHR32(S2,2))) |
| 535 | #else |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 536 | /* Float test is written this way to catch NaNs at the same time */ |
| 537 | if (!(S1 > 0.2f*S2)) |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 538 | #endif |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 539 | { |
| 540 | for (i=0;i<len;i++) |
| 541 | e[i] = 0; |
| 542 | } else if (S1 < S2) |
| 543 | { |
| 544 | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); |
| 545 | for (i=0;i<overlap;i++) |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 546 | { |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 547 | opus_val16 tmp_g = Q15ONE - MULT16_16_Q15(mode->window[i], Q15ONE-ratio); |
| 548 | e[i] = MULT16_32_Q15(tmp_g, e[i]); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 549 | } |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 550 | for (i=overlap;i<len;i++) |
| 551 | e[i] = MULT16_32_Q15(ratio, e[i]); |
| 552 | } |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 553 | } |
| 554 | |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 555 | /* Apply pre-filter to the MDCT overlap for the next frame because the |
| 556 | post-filter will be re-applied in the decoder after the MDCT overlap */ |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 557 | comb_filter(etmp, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, |
| 558 | -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 559 | NULL, 0); |
| 560 | |
Jean-Marc Valin | 0094c88 | 2012-11-29 17:59:50 -0500 | [diff] [blame^] | 561 | /* Simulate TDAC on the concealed audio so that it blends with the |
| 562 | MDCT of next frames. */ |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 563 | for (i=0;i<overlap/2;i++) |
| 564 | { |
| 565 | opus_val32 tmp; |
Jean-Marc Valin | e0491e7 | 2012-11-29 16:49:46 -0500 | [diff] [blame] | 566 | tmp = MULT16_32_Q15(mode->window[i], etmp[overlap-1-i]) + |
| 567 | MULT16_32_Q15(mode->window[overlap-i-1], etmp[i ]); |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 568 | out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(mode->window[overlap-i-1], tmp); |
| 569 | out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(mode->window[i], tmp); |
| 570 | } |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 571 | } while (++c<C); |
| 572 | } |
| 573 | |
| 574 | ALLOC(scratch, N, celt_sig); |
| 575 | deemphasis(out_syn, pcm, N, C, st->downsample, mode->preemph, st->preemph_memD, scratch); |
| 576 | |
| 577 | st->loss_count++; |
| 578 | |
| 579 | RESTORE_STACK; |
| 580 | } |
| 581 | |
| 582 | 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) |
| 583 | { |
| 584 | int c, i, N; |
| 585 | int spread_decision; |
| 586 | opus_int32 bits; |
| 587 | ec_dec _dec; |
| 588 | VARDECL(celt_sig, freq); |
| 589 | VARDECL(celt_norm, X); |
| 590 | VARDECL(celt_ener, bandE); |
| 591 | VARDECL(int, fine_quant); |
| 592 | VARDECL(int, pulses); |
| 593 | VARDECL(int, cap); |
| 594 | VARDECL(int, offsets); |
| 595 | VARDECL(int, fine_priority); |
| 596 | VARDECL(int, tf_res); |
| 597 | VARDECL(unsigned char, collapse_masks); |
| 598 | celt_sig *out_mem[2]; |
| 599 | celt_sig *decode_mem[2]; |
| 600 | celt_sig *out_syn[2]; |
| 601 | opus_val16 *lpc; |
| 602 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; |
| 603 | |
| 604 | int shortBlocks; |
| 605 | int isTransient; |
| 606 | int intra_ener; |
| 607 | const int CC = st->channels; |
| 608 | int LM, M; |
| 609 | int effEnd; |
| 610 | int codedBands; |
| 611 | int alloc_trim; |
| 612 | int postfilter_pitch; |
| 613 | opus_val16 postfilter_gain; |
| 614 | int intensity=0; |
| 615 | int dual_stereo=0; |
| 616 | opus_int32 total_bits; |
| 617 | opus_int32 balance; |
| 618 | opus_int32 tell; |
| 619 | int dynalloc_logp; |
| 620 | int postfilter_tapset; |
| 621 | int anti_collapse_rsv; |
| 622 | int anti_collapse_on=0; |
| 623 | int silence; |
| 624 | int C = st->stream_channels; |
| 625 | const OpusCustomMode *mode; |
| 626 | int nbEBands; |
| 627 | int overlap; |
| 628 | const opus_int16 *eBands; |
| 629 | ALLOC_STACK; |
| 630 | |
| 631 | mode = st->mode; |
| 632 | nbEBands = mode->nbEBands; |
| 633 | overlap = mode->overlap; |
| 634 | eBands = mode->eBands; |
| 635 | frame_size *= st->downsample; |
| 636 | |
| 637 | c=0; do { |
| 638 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+overlap); |
| 639 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 640 | } while (++c<CC); |
| 641 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+overlap)*CC); |
| 642 | oldBandE = lpc+CC*LPC_ORDER; |
| 643 | oldLogE = oldBandE + 2*nbEBands; |
| 644 | oldLogE2 = oldLogE + 2*nbEBands; |
| 645 | backgroundLogE = oldLogE2 + 2*nbEBands; |
| 646 | |
| 647 | #ifdef CUSTOM_MODES |
| 648 | if (st->signalling && data!=NULL) |
| 649 | { |
| 650 | int data0=data[0]; |
| 651 | /* Convert "standard mode" to Opus header */ |
| 652 | if (mode->Fs==48000 && mode->shortMdctSize==120) |
| 653 | { |
| 654 | data0 = fromOpus(data0); |
| 655 | if (data0<0) |
| 656 | return OPUS_INVALID_PACKET; |
| 657 | } |
| 658 | st->end = IMAX(1, mode->effEBands-2*(data0>>5)); |
| 659 | LM = (data0>>3)&0x3; |
| 660 | C = 1 + ((data0>>2)&0x1); |
| 661 | data++; |
| 662 | len--; |
| 663 | if (LM>mode->maxLM) |
| 664 | return OPUS_INVALID_PACKET; |
| 665 | if (frame_size < mode->shortMdctSize<<LM) |
| 666 | return OPUS_BUFFER_TOO_SMALL; |
| 667 | else |
| 668 | frame_size = mode->shortMdctSize<<LM; |
| 669 | } else { |
| 670 | #else |
| 671 | { |
| 672 | #endif |
| 673 | for (LM=0;LM<=mode->maxLM;LM++) |
| 674 | if (mode->shortMdctSize<<LM==frame_size) |
| 675 | break; |
| 676 | if (LM>mode->maxLM) |
| 677 | return OPUS_BAD_ARG; |
| 678 | } |
| 679 | M=1<<LM; |
| 680 | |
| 681 | if (len<0 || len>1275 || pcm==NULL) |
| 682 | return OPUS_BAD_ARG; |
| 683 | |
| 684 | N = M*mode->shortMdctSize; |
| 685 | |
| 686 | effEnd = st->end; |
| 687 | if (effEnd > mode->effEBands) |
| 688 | effEnd = mode->effEBands; |
| 689 | |
| 690 | if (data == NULL || len<=1) |
| 691 | { |
| 692 | celt_decode_lost(st, pcm, N, LM); |
| 693 | RESTORE_STACK; |
| 694 | return frame_size/st->downsample; |
| 695 | } |
| 696 | |
| 697 | if (dec == NULL) |
| 698 | { |
| 699 | ec_dec_init(&_dec,(unsigned char*)data,len); |
| 700 | dec = &_dec; |
| 701 | } |
| 702 | |
| 703 | if (C==1) |
| 704 | { |
| 705 | for (i=0;i<nbEBands;i++) |
| 706 | oldBandE[i]=MAX16(oldBandE[i],oldBandE[nbEBands+i]); |
| 707 | } |
| 708 | |
| 709 | total_bits = len*8; |
| 710 | tell = ec_tell(dec); |
| 711 | |
| 712 | if (tell >= total_bits) |
| 713 | silence = 1; |
| 714 | else if (tell==1) |
| 715 | silence = ec_dec_bit_logp(dec, 15); |
| 716 | else |
| 717 | silence = 0; |
| 718 | if (silence) |
| 719 | { |
| 720 | /* Pretend we've read all the remaining bits */ |
| 721 | tell = len*8; |
| 722 | dec->nbits_total+=tell-ec_tell(dec); |
| 723 | } |
| 724 | |
| 725 | postfilter_gain = 0; |
| 726 | postfilter_pitch = 0; |
| 727 | postfilter_tapset = 0; |
| 728 | if (st->start==0 && tell+16 <= total_bits) |
| 729 | { |
| 730 | if(ec_dec_bit_logp(dec, 1)) |
| 731 | { |
| 732 | int qg, octave; |
| 733 | octave = ec_dec_uint(dec, 6); |
| 734 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; |
| 735 | qg = ec_dec_bits(dec, 3); |
| 736 | if (ec_tell(dec)+2<=total_bits) |
| 737 | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); |
| 738 | postfilter_gain = QCONST16(.09375f,15)*(qg+1); |
| 739 | } |
| 740 | tell = ec_tell(dec); |
| 741 | } |
| 742 | |
| 743 | if (LM > 0 && tell+3 <= total_bits) |
| 744 | { |
| 745 | isTransient = ec_dec_bit_logp(dec, 3); |
| 746 | tell = ec_tell(dec); |
| 747 | } |
| 748 | else |
| 749 | isTransient = 0; |
| 750 | |
| 751 | if (isTransient) |
| 752 | shortBlocks = M; |
| 753 | else |
| 754 | shortBlocks = 0; |
| 755 | |
| 756 | /* Decode the global flags (first symbols in the stream) */ |
| 757 | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; |
| 758 | /* Get band energies */ |
| 759 | unquant_coarse_energy(mode, st->start, st->end, oldBandE, |
| 760 | intra_ener, dec, C, LM); |
| 761 | |
| 762 | ALLOC(tf_res, nbEBands, int); |
| 763 | tf_decode(st->start, st->end, isTransient, tf_res, LM, dec); |
| 764 | |
| 765 | tell = ec_tell(dec); |
| 766 | spread_decision = SPREAD_NORMAL; |
| 767 | if (tell+4 <= total_bits) |
| 768 | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); |
| 769 | |
| 770 | ALLOC(cap, nbEBands, int); |
| 771 | |
| 772 | init_caps(mode,cap,LM,C); |
| 773 | |
| 774 | ALLOC(offsets, nbEBands, int); |
| 775 | |
| 776 | dynalloc_logp = 6; |
| 777 | total_bits<<=BITRES; |
| 778 | tell = ec_tell_frac(dec); |
| 779 | for (i=st->start;i<st->end;i++) |
| 780 | { |
| 781 | int width, quanta; |
| 782 | int dynalloc_loop_logp; |
| 783 | int boost; |
| 784 | width = C*(eBands[i+1]-eBands[i])<<LM; |
| 785 | /* quanta is 6 bits, but no more than 1 bit/sample |
| 786 | and no less than 1/8 bit/sample */ |
| 787 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
| 788 | dynalloc_loop_logp = dynalloc_logp; |
| 789 | boost = 0; |
| 790 | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) |
| 791 | { |
| 792 | int flag; |
| 793 | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); |
| 794 | tell = ec_tell_frac(dec); |
| 795 | if (!flag) |
| 796 | break; |
| 797 | boost += quanta; |
| 798 | total_bits -= quanta; |
| 799 | dynalloc_loop_logp = 1; |
| 800 | } |
| 801 | offsets[i] = boost; |
| 802 | /* Making dynalloc more likely */ |
| 803 | if (boost>0) |
| 804 | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
| 805 | } |
| 806 | |
| 807 | ALLOC(fine_quant, nbEBands, int); |
| 808 | alloc_trim = tell+(6<<BITRES) <= total_bits ? |
| 809 | ec_dec_icdf(dec, trim_icdf, 7) : 5; |
| 810 | |
| 811 | bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1; |
| 812 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
| 813 | bits -= anti_collapse_rsv; |
| 814 | |
| 815 | ALLOC(pulses, nbEBands, int); |
| 816 | ALLOC(fine_priority, nbEBands, int); |
| 817 | |
| 818 | codedBands = compute_allocation(mode, st->start, st->end, offsets, cap, |
| 819 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, |
| 820 | fine_quant, fine_priority, C, LM, dec, 0, 0); |
| 821 | |
| 822 | unquant_fine_energy(mode, st->start, st->end, oldBandE, fine_quant, dec, C); |
| 823 | |
| 824 | /* Decode fixed codebook */ |
| 825 | ALLOC(collapse_masks, C*nbEBands, unsigned char); |
| 826 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 827 | |
| 828 | quant_all_bands(0, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, |
| 829 | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, |
| 830 | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng); |
| 831 | |
| 832 | if (anti_collapse_rsv > 0) |
| 833 | { |
| 834 | anti_collapse_on = ec_dec_bits(dec, 1); |
| 835 | } |
| 836 | |
| 837 | unquant_energy_finalise(mode, st->start, st->end, oldBandE, |
| 838 | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); |
| 839 | |
| 840 | if (anti_collapse_on) |
| 841 | anti_collapse(mode, X, collapse_masks, LM, C, N, |
| 842 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
| 843 | |
| 844 | ALLOC(bandE, nbEBands*C, celt_ener); |
| 845 | |
| 846 | log2Amp(mode, st->start, st->end, bandE, oldBandE, C); |
| 847 | |
| 848 | if (silence) |
| 849 | { |
| 850 | for (i=0;i<C*nbEBands;i++) |
| 851 | { |
| 852 | bandE[i] = 0; |
| 853 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
| 854 | } |
| 855 | } |
| 856 | ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 857 | /* Synthesis */ |
| 858 | denormalise_bands(mode, X, freq, bandE, st->start, effEnd, C, M); |
| 859 | |
| 860 | c=0; do { |
| 861 | OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap); |
| 862 | } while (++c<CC); |
| 863 | |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 864 | c=0; do { |
| 865 | int bound = M*eBands[effEnd]; |
| 866 | if (st->downsample!=1) |
| 867 | bound = IMIN(bound, N/st->downsample); |
| 868 | for (i=bound;i<N;i++) |
| 869 | freq[c*N+i] = 0; |
| 870 | } while (++c<C); |
| 871 | |
| 872 | c=0; do { |
| 873 | out_syn[c] = out_mem[c]+MAX_PERIOD-N; |
| 874 | } while (++c<CC); |
| 875 | |
| 876 | if (CC==2&&C==1) |
| 877 | { |
| 878 | for (i=0;i<N;i++) |
| 879 | freq[N+i] = freq[i]; |
| 880 | } |
| 881 | if (CC==1&&C==2) |
| 882 | { |
| 883 | for (i=0;i<N;i++) |
| 884 | freq[i] = HALF32(ADD32(freq[i],freq[N+i])); |
| 885 | } |
| 886 | |
| 887 | /* Compute inverse MDCTs */ |
| 888 | compute_inv_mdcts(mode, shortBlocks, freq, out_syn, CC, LM); |
| 889 | |
| 890 | c=0; do { |
| 891 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); |
| 892 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); |
| 893 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, mode->shortMdctSize, |
| 894 | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, |
| 895 | mode->window, overlap); |
| 896 | if (LM!=0) |
| 897 | comb_filter(out_syn[c]+mode->shortMdctSize, out_syn[c]+mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-mode->shortMdctSize, |
| 898 | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, |
| 899 | mode->window, overlap); |
| 900 | |
| 901 | } while (++c<CC); |
| 902 | st->postfilter_period_old = st->postfilter_period; |
| 903 | st->postfilter_gain_old = st->postfilter_gain; |
| 904 | st->postfilter_tapset_old = st->postfilter_tapset; |
| 905 | st->postfilter_period = postfilter_pitch; |
| 906 | st->postfilter_gain = postfilter_gain; |
| 907 | st->postfilter_tapset = postfilter_tapset; |
| 908 | if (LM!=0) |
| 909 | { |
| 910 | st->postfilter_period_old = st->postfilter_period; |
| 911 | st->postfilter_gain_old = st->postfilter_gain; |
| 912 | st->postfilter_tapset_old = st->postfilter_tapset; |
| 913 | } |
| 914 | |
| 915 | if (C==1) { |
| 916 | for (i=0;i<nbEBands;i++) |
| 917 | oldBandE[nbEBands+i]=oldBandE[i]; |
| 918 | } |
| 919 | |
| 920 | /* In case start or end were to change */ |
| 921 | if (!isTransient) |
| 922 | { |
| 923 | for (i=0;i<2*nbEBands;i++) |
| 924 | oldLogE2[i] = oldLogE[i]; |
| 925 | for (i=0;i<2*nbEBands;i++) |
| 926 | oldLogE[i] = oldBandE[i]; |
| 927 | for (i=0;i<2*nbEBands;i++) |
| 928 | backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]); |
| 929 | } else { |
| 930 | for (i=0;i<2*nbEBands;i++) |
| 931 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
| 932 | } |
| 933 | c=0; do |
| 934 | { |
| 935 | for (i=0;i<st->start;i++) |
| 936 | { |
| 937 | oldBandE[c*nbEBands+i]=0; |
| 938 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); |
| 939 | } |
| 940 | for (i=st->end;i<nbEBands;i++) |
| 941 | { |
| 942 | oldBandE[c*nbEBands+i]=0; |
| 943 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); |
| 944 | } |
| 945 | } while (++c<2); |
| 946 | st->rng = dec->rng; |
| 947 | |
| 948 | /* We reuse freq[] as scratch space for the de-emphasis */ |
| 949 | deemphasis(out_syn, pcm, N, CC, st->downsample, mode->preemph, st->preemph_memD, freq); |
| 950 | st->loss_count = 0; |
| 951 | RESTORE_STACK; |
| 952 | if (ec_tell(dec) > 8*len) |
| 953 | return OPUS_INTERNAL_ERROR; |
| 954 | if(ec_get_error(dec)) |
| 955 | st->error = 1; |
| 956 | return frame_size/st->downsample; |
| 957 | } |
| 958 | |
| 959 | |
| 960 | #ifdef CUSTOM_MODES |
| 961 | |
| 962 | #ifdef FIXED_POINT |
| 963 | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) |
| 964 | { |
| 965 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 966 | } |
| 967 | |
| 968 | #ifndef DISABLE_FLOAT_API |
| 969 | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) |
| 970 | { |
| 971 | int j, ret, C, N; |
| 972 | VARDECL(opus_int16, out); |
| 973 | ALLOC_STACK; |
| 974 | |
| 975 | if (pcm==NULL) |
| 976 | return OPUS_BAD_ARG; |
| 977 | |
| 978 | C = st->channels; |
| 979 | N = frame_size; |
| 980 | |
| 981 | ALLOC(out, C*N, opus_int16); |
| 982 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); |
| 983 | if (ret>0) |
| 984 | for (j=0;j<C*ret;j++) |
| 985 | pcm[j]=out[j]*(1.f/32768.f); |
| 986 | |
| 987 | RESTORE_STACK; |
| 988 | return ret; |
| 989 | } |
| 990 | #endif /* DISABLE_FLOAT_API */ |
| 991 | |
| 992 | #else |
| 993 | |
| 994 | int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size) |
| 995 | { |
| 996 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 997 | } |
| 998 | |
| 999 | int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size) |
| 1000 | { |
| 1001 | int j, ret, C, N; |
| 1002 | VARDECL(celt_sig, out); |
| 1003 | ALLOC_STACK; |
| 1004 | |
| 1005 | if (pcm==NULL) |
| 1006 | return OPUS_BAD_ARG; |
| 1007 | |
| 1008 | C = st->channels; |
| 1009 | N = frame_size; |
| 1010 | ALLOC(out, C*N, celt_sig); |
| 1011 | |
| 1012 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); |
| 1013 | |
| 1014 | if (ret>0) |
| 1015 | for (j=0;j<C*ret;j++) |
| 1016 | pcm[j] = FLOAT2INT16 (out[j]); |
| 1017 | |
| 1018 | RESTORE_STACK; |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | #endif |
| 1023 | #endif /* CUSTOM_MODES */ |
| 1024 | |
| 1025 | int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...) |
| 1026 | { |
| 1027 | va_list ap; |
| 1028 | |
| 1029 | va_start(ap, request); |
| 1030 | switch (request) |
| 1031 | { |
| 1032 | case CELT_SET_START_BAND_REQUEST: |
| 1033 | { |
| 1034 | opus_int32 value = va_arg(ap, opus_int32); |
| 1035 | if (value<0 || value>=st->mode->nbEBands) |
| 1036 | goto bad_arg; |
| 1037 | st->start = value; |
| 1038 | } |
| 1039 | break; |
| 1040 | case CELT_SET_END_BAND_REQUEST: |
| 1041 | { |
| 1042 | opus_int32 value = va_arg(ap, opus_int32); |
| 1043 | if (value<1 || value>st->mode->nbEBands) |
| 1044 | goto bad_arg; |
| 1045 | st->end = value; |
| 1046 | } |
| 1047 | break; |
| 1048 | case CELT_SET_CHANNELS_REQUEST: |
| 1049 | { |
| 1050 | opus_int32 value = va_arg(ap, opus_int32); |
| 1051 | if (value<1 || value>2) |
| 1052 | goto bad_arg; |
| 1053 | st->stream_channels = value; |
| 1054 | } |
| 1055 | break; |
| 1056 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: |
| 1057 | { |
| 1058 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 1059 | if (value==NULL) |
| 1060 | goto bad_arg; |
| 1061 | *value=st->error; |
| 1062 | st->error = 0; |
| 1063 | } |
| 1064 | break; |
| 1065 | case OPUS_GET_LOOKAHEAD_REQUEST: |
| 1066 | { |
| 1067 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 1068 | if (value==NULL) |
| 1069 | goto bad_arg; |
| 1070 | *value = st->overlap/st->downsample; |
| 1071 | } |
| 1072 | break; |
| 1073 | case OPUS_RESET_STATE: |
| 1074 | { |
| 1075 | int i; |
| 1076 | opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2; |
| 1077 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels); |
| 1078 | oldBandE = lpc+st->channels*LPC_ORDER; |
| 1079 | oldLogE = oldBandE + 2*st->mode->nbEBands; |
| 1080 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; |
| 1081 | OPUS_CLEAR((char*)&st->DECODER_RESET_START, |
| 1082 | opus_custom_decoder_get_size(st->mode, st->channels)- |
| 1083 | ((char*)&st->DECODER_RESET_START - (char*)st)); |
| 1084 | for (i=0;i<2*st->mode->nbEBands;i++) |
| 1085 | oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); |
| 1086 | } |
| 1087 | break; |
| 1088 | case OPUS_GET_PITCH_REQUEST: |
| 1089 | { |
| 1090 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 1091 | if (value==NULL) |
| 1092 | goto bad_arg; |
| 1093 | *value = st->postfilter_period; |
| 1094 | } |
| 1095 | break; |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 1096 | case CELT_GET_MODE_REQUEST: |
| 1097 | { |
| 1098 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1099 | if (value==0) |
| 1100 | goto bad_arg; |
| 1101 | *value=st->mode; |
| 1102 | } |
| 1103 | break; |
| 1104 | case CELT_SET_SIGNALLING_REQUEST: |
| 1105 | { |
| 1106 | opus_int32 value = va_arg(ap, opus_int32); |
| 1107 | st->signalling = value; |
| 1108 | } |
| 1109 | break; |
| 1110 | case OPUS_GET_FINAL_RANGE_REQUEST: |
| 1111 | { |
| 1112 | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
| 1113 | if (value==0) |
| 1114 | goto bad_arg; |
| 1115 | *value=st->rng; |
| 1116 | } |
| 1117 | break; |
Jean-Marc Valin | 6906210 | 2012-11-08 09:42:27 -0500 | [diff] [blame] | 1118 | default: |
| 1119 | goto bad_request; |
| 1120 | } |
| 1121 | va_end(ap); |
| 1122 | return OPUS_OK; |
| 1123 | bad_arg: |
| 1124 | va_end(ap); |
| 1125 | return OPUS_BAD_ARG; |
| 1126 | bad_request: |
| 1127 | va_end(ap); |
| 1128 | return OPUS_UNIMPLEMENTED; |
| 1129 | } |