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