annotate src/opus-1.3/celt/celt_decoder.c @ 156:1bf23f5aebc4

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