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