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