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