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