Home | History | Annotate | Download | only in celt
      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_C
     35 
     36 #include "os_support.h"
     37 #include "mdct.h"
     38 #include <math.h>
     39 #include "celt.h"
     40 #include "pitch.h"
     41 #include "bands.h"
     42 #include "modes.h"
     43 #include "entcode.h"
     44 #include "quant_bands.h"
     45 #include "rate.h"
     46 #include "stack_alloc.h"
     47 #include "mathops.h"
     48 #include "float_cast.h"
     49 #include <stdarg.h>
     50 #include "celt_lpc.h"
     51 #include "vq.h"
     52 
     53 #ifndef OPUS_VERSION
     54 #define OPUS_VERSION "unknown"
     55 #endif
     56 
     57 #ifdef CUSTOM_MODES
     58 #define OPUS_CUSTOM_NOSTATIC
     59 #else
     60 #define OPUS_CUSTOM_NOSTATIC static inline
     61 #endif
     62 
     63 static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
     64 /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
     65 static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
     66 
     67 static const unsigned char tapset_icdf[3]={2,1,0};
     68 
     69 #ifdef CUSTOM_MODES
     70 static const unsigned char toOpusTable[20] = {
     71       0xE0, 0xE8, 0xF0, 0xF8,
     72       0xC0, 0xC8, 0xD0, 0xD8,
     73       0xA0, 0xA8, 0xB0, 0xB8,
     74       0x00, 0x00, 0x00, 0x00,
     75       0x80, 0x88, 0x90, 0x98,
     76 };
     77 
     78 static const unsigned char fromOpusTable[16] = {
     79       0x80, 0x88, 0x90, 0x98,
     80       0x40, 0x48, 0x50, 0x58,
     81       0x20, 0x28, 0x30, 0x38,
     82       0x00, 0x08, 0x10, 0x18
     83 };
     84 
     85 static inline int toOpus(unsigned char c)
     86 {
     87    int ret=0;
     88    if (c<0xA0)
     89       ret = toOpusTable[c>>3];
     90    if (ret == 0)
     91       return -1;
     92    else
     93       return ret|(c&0x7);
     94 }
     95 
     96 static inline int fromOpus(unsigned char c)
     97 {
     98    if (c<0x80)
     99       return -1;
    100    else
    101       return fromOpusTable[(c>>3)-16] | (c&0x7);
    102 }
    103 #endif /* CUSTOM_MODES */
    104 
    105 #define COMBFILTER_MAXPERIOD 1024
    106 #define COMBFILTER_MINPERIOD 15
    107 
    108 static int resampling_factor(opus_int32 rate)
    109 {
    110    int ret;
    111    switch (rate)
    112    {
    113    case 48000:
    114       ret = 1;
    115       break;
    116    case 24000:
    117       ret = 2;
    118       break;
    119    case 16000:
    120       ret = 3;
    121       break;
    122    case 12000:
    123       ret = 4;
    124       break;
    125    case 8000:
    126       ret = 6;
    127       break;
    128    default:
    129 #ifndef CUSTOM_MODES
    130       celt_assert(0);
    131 #endif
    132       ret = 0;
    133       break;
    134    }
    135    return ret;
    136 }
    137 
    138 /** Encoder state
    139  @brief Encoder state
    140  */
    141 struct OpusCustomEncoder {
    142    const OpusCustomMode *mode;     /**< Mode used by the encoder */
    143    int overlap;
    144    int channels;
    145    int stream_channels;
    146 
    147    int force_intra;
    148    int clip;
    149    int disable_pf;
    150    int complexity;
    151    int upsample;
    152    int start, end;
    153 
    154    opus_int32 bitrate;
    155    int vbr;
    156    int signalling;
    157    int constrained_vbr;      /* If zero, VBR can do whatever it likes with the rate */
    158    int loss_rate;
    159    int lsb_depth;
    160 
    161    /* Everything beyond this point gets cleared on a reset */
    162 #define ENCODER_RESET_START rng
    163 
    164    opus_uint32 rng;
    165    int spread_decision;
    166    opus_val32 delayedIntra;
    167    int tonal_average;
    168    int lastCodedBands;
    169    int hf_average;
    170    int tapset_decision;
    171 
    172    int prefilter_period;
    173    opus_val16 prefilter_gain;
    174    int prefilter_tapset;
    175 #ifdef RESYNTH
    176    int prefilter_period_old;
    177    opus_val16 prefilter_gain_old;
    178    int prefilter_tapset_old;
    179 #endif
    180    int consec_transient;
    181 
    182    opus_val32 preemph_memE[2];
    183    opus_val32 preemph_memD[2];
    184 
    185    /* VBR-related parameters */
    186    opus_int32 vbr_reservoir;
    187    opus_int32 vbr_drift;
    188    opus_int32 vbr_offset;
    189    opus_int32 vbr_count;
    190 
    191 #ifdef RESYNTH
    192    celt_sig syn_mem[2][2*MAX_PERIOD];
    193 #endif
    194 
    195    celt_sig in_mem[1]; /* Size = channels*mode->overlap */
    196    /* celt_sig prefilter_mem[],  Size = channels*COMBFILTER_MAXPERIOD */
    197    /* opus_val16 oldBandE[],     Size = channels*mode->nbEBands */
    198    /* opus_val16 oldLogE[],      Size = channels*mode->nbEBands */
    199    /* opus_val16 oldLogE2[],     Size = channels*mode->nbEBands */
    200 #ifdef RESYNTH
    201    /* opus_val16 overlap_mem[],  Size = channels*overlap */
    202 #endif
    203 };
    204 
    205 int celt_encoder_get_size(int channels)
    206 {
    207    CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
    208    return opus_custom_encoder_get_size(mode, channels);
    209 }
    210 
    211 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
    212 {
    213    int size = sizeof(struct CELTEncoder)
    214          + (channels*mode->overlap-1)*sizeof(celt_sig)    /* celt_sig in_mem[channels*mode->overlap]; */
    215          + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
    216          + 3*channels*mode->nbEBands*sizeof(opus_val16);  /* opus_val16 oldBandE[channels*mode->nbEBands]; */
    217                                                           /* opus_val16 oldLogE[channels*mode->nbEBands]; */
    218                                                           /* opus_val16 oldLogE2[channels*mode->nbEBands]; */
    219 #ifdef RESYNTH
    220    size += channels*mode->overlap*sizeof(celt_sig);       /* celt_sig overlap_mem[channels*mode->nbEBands]; */
    221 #endif
    222    return size;
    223 }
    224 
    225 #ifdef CUSTOM_MODES
    226 CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
    227 {
    228    int ret;
    229    CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
    230    /* init will handle the NULL case */
    231    ret = opus_custom_encoder_init(st, mode, channels);
    232    if (ret != OPUS_OK)
    233    {
    234       opus_custom_encoder_destroy(st);
    235       st = NULL;
    236    }
    237    if (error)
    238       *error = ret;
    239    return st;
    240 }
    241 #endif /* CUSTOM_MODES */
    242 
    243 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
    244 {
    245    int ret;
    246    ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
    247    if (ret != OPUS_OK)
    248       return ret;
    249    st->upsample = resampling_factor(sampling_rate);
    250    return OPUS_OK;
    251 }
    252 
    253 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
    254 {
    255    if (channels < 0 || channels > 2)
    256       return OPUS_BAD_ARG;
    257 
    258    if (st==NULL || mode==NULL)
    259       return OPUS_ALLOC_FAIL;
    260 
    261    OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
    262 
    263    st->mode = mode;
    264    st->overlap = mode->overlap;
    265    st->stream_channels = st->channels = channels;
    266 
    267    st->upsample = 1;
    268    st->start = 0;
    269    st->end = st->mode->effEBands;
    270    st->signalling = 1;
    271 
    272    st->constrained_vbr = 1;
    273    st->clip = 1;
    274 
    275    st->bitrate = OPUS_BITRATE_MAX;
    276    st->vbr = 0;
    277    st->force_intra  = 0;
    278    st->complexity = 5;
    279    st->lsb_depth=24;
    280 
    281    opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
    282 
    283    return OPUS_OK;
    284 }
    285 
    286 #ifdef CUSTOM_MODES
    287 void opus_custom_encoder_destroy(CELTEncoder *st)
    288 {
    289    opus_free(st);
    290 }
    291 #endif /* CUSTOM_MODES */
    292 
    293 static inline opus_val16 SIG2WORD16(celt_sig x)
    294 {
    295 #ifdef FIXED_POINT
    296    x = PSHR32(x, SIG_SHIFT);
    297    x = MAX32(x, -32768);
    298    x = MIN32(x, 32767);
    299    return EXTRACT16(x);
    300 #else
    301    return (opus_val16)x;
    302 #endif
    303 }
    304 
    305 static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
    306                               int overlap)
    307 {
    308    int i;
    309    VARDECL(opus_val16, tmp);
    310    opus_val32 mem0=0,mem1=0;
    311    int is_transient = 0;
    312    int block;
    313    int N;
    314    VARDECL(opus_val16, bins);
    315    SAVE_STACK;
    316    ALLOC(tmp, len, opus_val16);
    317 
    318    block = overlap/2;
    319    N=len/block;
    320    ALLOC(bins, N, opus_val16);
    321    if (C==1)
    322    {
    323       for (i=0;i<len;i++)
    324          tmp[i] = SHR32(in[i],SIG_SHIFT);
    325    } else {
    326       for (i=0;i<len;i++)
    327          tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
    328    }
    329 
    330    /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
    331    for (i=0;i<len;i++)
    332    {
    333       opus_val32 x,y;
    334       x = tmp[i];
    335       y = ADD32(mem0, x);
    336 #ifdef FIXED_POINT
    337       mem0 = mem1 + y - SHL32(x,1);
    338       mem1 = x - SHR32(y,1);
    339 #else
    340       mem0 = mem1 + y - 2*x;
    341       mem1 = x - .5f*y;
    342 #endif
    343       tmp[i] = EXTRACT16(SHR32(y,2));
    344    }
    345    /* First few samples are bad because we don't propagate the memory */
    346    for (i=0;i<12;i++)
    347       tmp[i] = 0;
    348 
    349    for (i=0;i<N;i++)
    350    {
    351       int j;
    352       opus_val16 max_abs=0;
    353       for (j=0;j<block;j++)
    354          max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
    355       bins[i] = max_abs;
    356    }
    357    for (i=0;i<N;i++)
    358    {
    359       int j;
    360       int conseq=0;
    361       opus_val16 t1, t2, t3;
    362 
    363       t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
    364       t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
    365       t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
    366       for (j=0;j<i;j++)
    367       {
    368          if (bins[j] < t1)
    369             conseq++;
    370          if (bins[j] < t2)
    371             conseq++;
    372          else
    373             conseq = 0;
    374       }
    375       if (conseq>=3)
    376          is_transient=1;
    377       conseq = 0;
    378       for (j=i+1;j<N;j++)
    379       {
    380          if (bins[j] < t3)
    381             conseq++;
    382          else
    383             conseq = 0;
    384       }
    385       if (conseq>=7)
    386          is_transient=1;
    387    }
    388    RESTORE_STACK;
    389 #ifdef FUZZING
    390    is_transient = rand()&0x1;
    391 #endif
    392    return is_transient;
    393 }
    394 
    395 /** Apply window and compute the MDCT for all sub-frames and
    396     all channels in a frame */
    397 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in, celt_sig * OPUS_RESTRICT out, int C, int LM)
    398 {
    399    if (C==1 && !shortBlocks)
    400    {
    401       const int overlap = OVERLAP(mode);
    402       clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
    403    } else {
    404       const int overlap = OVERLAP(mode);
    405       int N = mode->shortMdctSize<<LM;
    406       int B = 1;
    407       int b, c;
    408       if (shortBlocks)
    409       {
    410          N = mode->shortMdctSize;
    411          B = shortBlocks;
    412       }
    413       c=0; do {
    414          for (b=0;b<B;b++)
    415          {
    416             /* Interleaving the sub-frames while doing the MDCTs */
    417             clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
    418          }
    419       } while (++c<C);
    420    }
    421 }
    422 
    423 /** Compute the IMDCT and apply window for all sub-frames and
    424     all channels in a frame */
    425 static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
    426       celt_sig * OPUS_RESTRICT out_mem[],
    427       celt_sig * OPUS_RESTRICT overlap_mem[], int C, int LM)
    428 {
    429    int c;
    430    const int N = mode->shortMdctSize<<LM;
    431    const int overlap = OVERLAP(mode);
    432    VARDECL(opus_val32, x);
    433    SAVE_STACK;
    434 
    435    ALLOC(x, N+overlap, opus_val32);
    436    c=0; do {
    437       int j;
    438       int b;
    439       int N2 = N;
    440       int B = 1;
    441 
    442       if (shortBlocks)
    443       {
    444          N2 = mode->shortMdctSize;
    445          B = shortBlocks;
    446       }
    447       /* Prevents problems from the imdct doing the overlap-add */
    448       OPUS_CLEAR(x, overlap);
    449 
    450       for (b=0;b<B;b++)
    451       {
    452          /* IMDCT on the interleaved the sub-frames */
    453          clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
    454       }
    455 
    456       for (j=0;j<overlap;j++)
    457          out_mem[c][j] = x[j] + overlap_mem[c][j];
    458       for (;j<N;j++)
    459          out_mem[c][j] = x[j];
    460       for (j=0;j<overlap;j++)
    461          overlap_mem[c][j] = x[N+j];
    462    } while (++c<C);
    463    RESTORE_STACK;
    464 }
    465 
    466 static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem)
    467 {
    468    int c;
    469    int count=0;
    470    c=0; do {
    471       int j;
    472       celt_sig * OPUS_RESTRICT x;
    473       opus_val16  * OPUS_RESTRICT y;
    474       celt_sig m = mem[c];
    475       x =in[c];
    476       y = pcm+c;
    477       for (j=0;j<N;j++)
    478       {
    479          celt_sig tmp = *x + m;
    480          m = MULT16_32_Q15(coef[0], tmp)
    481            - MULT16_32_Q15(coef[1], *x);
    482          tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
    483          x++;
    484          /* Technically the store could be moved outside of the if because
    485             the stores we don't want will just be overwritten */
    486          if (count==0)
    487             *y = SCALEOUT(SIG2WORD16(tmp));
    488          if (++count==downsample)
    489          {
    490             y+=C;
    491             count=0;
    492          }
    493       }
    494       mem[c] = m;
    495    } while (++c<C);
    496 }
    497 
    498 static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
    499       opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
    500       const opus_val16 *window, int overlap)
    501 {
    502    int i;
    503    /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
    504    opus_val16 g00, g01, g02, g10, g11, g12;
    505    static const opus_val16 gains[3][3] = {
    506          {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
    507          {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
    508          {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
    509    g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
    510    g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
    511    g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
    512    g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
    513    g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
    514    g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
    515    for (i=0;i<overlap;i++)
    516    {
    517       opus_val16 f;
    518       f = MULT16_16_Q15(window[i],window[i]);
    519       y[i] = x[i]
    520                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
    521                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
    522                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
    523                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
    524                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
    525                + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
    526                + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
    527                + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
    528                + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
    529                + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
    530 
    531    }
    532    for (i=overlap;i<N;i++)
    533       y[i] = x[i]
    534                + MULT16_32_Q15(g10,x[i-T1])
    535                + MULT16_32_Q15(g11,x[i-T1-1])
    536                + MULT16_32_Q15(g11,x[i-T1+1])
    537                + MULT16_32_Q15(g12,x[i-T1-2])
    538                + MULT16_32_Q15(g12,x[i-T1+2]);
    539 }
    540 
    541 static const signed char tf_select_table[4][8] = {
    542       {0, -1, 0, -1,    0,-1, 0,-1},
    543       {0, -1, 0, -2,    1, 0, 1,-1},
    544       {0, -2, 0, -3,    2, 0, 1,-1},
    545       {0, -2, 0, -3,    3, 0, 1,-1},
    546 };
    547 
    548 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width)
    549 {
    550    int i, j;
    551    static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)};
    552    opus_val32 L1;
    553    opus_val16 bias;
    554    L1=0;
    555    for (i=0;i<1<<LM;i++)
    556    {
    557       opus_val32 L2 = 0;
    558       for (j=0;j<N>>LM;j++)
    559          L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]);
    560       L1 += celt_sqrt(L2);
    561    }
    562    L1 = MULT16_32_Q15(sqrtM_1[LM], L1);
    563    if (width==1)
    564       bias = QCONST16(.12f,15)*LM;
    565    else if (width==2)
    566       bias = QCONST16(.05f,15)*LM;
    567    else
    568       bias = QCONST16(.02f,15)*LM;
    569    L1 = MAC16_32_Q15(L1, bias, L1);
    570    return L1;
    571 }
    572 
    573 static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
    574       int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
    575       int start, int *tf_sum)
    576 {
    577    int i;
    578    VARDECL(int, metric);
    579    int cost0;
    580    int cost1;
    581    VARDECL(int, path0);
    582    VARDECL(int, path1);
    583    VARDECL(celt_norm, tmp);
    584    int lambda;
    585    int tf_select=0;
    586    SAVE_STACK;
    587 
    588    if (nbCompressedBytes<15*C || start!=0)
    589    {
    590       *tf_sum = 0;
    591       for (i=0;i<len;i++)
    592          tf_res[i] = isTransient;
    593       return 0;
    594    }
    595    if (nbCompressedBytes<40)
    596       lambda = 12;
    597    else if (nbCompressedBytes<60)
    598       lambda = 6;
    599    else if (nbCompressedBytes<100)
    600       lambda = 4;
    601    else
    602       lambda = 3;
    603 
    604    ALLOC(metric, len, int);
    605    ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
    606    ALLOC(path0, len, int);
    607    ALLOC(path1, len, int);
    608 
    609    *tf_sum = 0;
    610    for (i=0;i<len;i++)
    611    {
    612       int j, k, N;
    613       opus_val32 L1, best_L1;
    614       int best_level=0;
    615       N = (m->eBands[i+1]-m->eBands[i])<<LM;
    616       for (j=0;j<N;j++)
    617          tmp[j] = X[j+(m->eBands[i]<<LM)];
    618       /* Just add the right channel if we're in stereo */
    619       if (C==2)
    620          for (j=0;j<N;j++)
    621             tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));
    622       L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM);
    623       best_L1 = L1;
    624       /*printf ("%f ", L1);*/
    625       for (k=0;k<LM;k++)
    626       {
    627          int B;
    628 
    629          if (isTransient)
    630             B = (LM-k-1);
    631          else
    632             B = k+1;
    633 
    634          if (isTransient)
    635             haar1(tmp, N>>(LM-k), 1<<(LM-k));
    636          else
    637             haar1(tmp, N>>k, 1<<k);
    638 
    639          L1 = l1_metric(tmp, N, B, N>>LM);
    640 
    641          if (L1 < best_L1)
    642          {
    643             best_L1 = L1;
    644             best_level = k+1;
    645          }
    646       }
    647       /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
    648       if (isTransient)
    649          metric[i] = best_level;
    650       else
    651          metric[i] = -best_level;
    652       *tf_sum += metric[i];
    653    }
    654    /*printf("\n");*/
    655    /* NOTE: Future optimized implementations could detect extreme transients and set
    656       tf_select = 1 but so far we have not found a reliable way of making this useful */
    657    tf_select = 0;
    658 
    659    cost0 = 0;
    660    cost1 = isTransient ? 0 : lambda;
    661    /* Viterbi forward pass */
    662    for (i=1;i<len;i++)
    663    {
    664       int curr0, curr1;
    665       int from0, from1;
    666 
    667       from0 = cost0;
    668       from1 = cost1 + lambda;
    669       if (from0 < from1)
    670       {
    671          curr0 = from0;
    672          path0[i]= 0;
    673       } else {
    674          curr0 = from1;
    675          path0[i]= 1;
    676       }
    677 
    678       from0 = cost0 + lambda;
    679       from1 = cost1;
    680       if (from0 < from1)
    681       {
    682          curr1 = from0;
    683          path1[i]= 0;
    684       } else {
    685          curr1 = from1;
    686          path1[i]= 1;
    687       }
    688       cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
    689       cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
    690    }
    691    tf_res[len-1] = cost0 < cost1 ? 0 : 1;
    692    /* Viterbi backward pass to check the decisions */
    693    for (i=len-2;i>=0;i--)
    694    {
    695       if (tf_res[i+1] == 1)
    696          tf_res[i] = path1[i+1];
    697       else
    698          tf_res[i] = path0[i+1];
    699    }
    700    RESTORE_STACK;
    701 #ifdef FUZZING
    702    tf_select = rand()&0x1;
    703    tf_res[0] = rand()&0x1;
    704    for (i=1;i<len;i++)
    705       tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
    706 #endif
    707    return tf_select;
    708 }
    709 
    710 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
    711 {
    712    int curr, i;
    713    int tf_select_rsv;
    714    int tf_changed;
    715    int logp;
    716    opus_uint32 budget;
    717    opus_uint32 tell;
    718    budget = enc->storage*8;
    719    tell = ec_tell(enc);
    720    logp = isTransient ? 2 : 4;
    721    /* Reserve space to code the tf_select decision. */
    722    tf_select_rsv = LM>0 && tell+logp+1 <= budget;
    723    budget -= tf_select_rsv;
    724    curr = tf_changed = 0;
    725    for (i=start;i<end;i++)
    726    {
    727       if (tell+logp<=budget)
    728       {
    729          ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
    730          tell = ec_tell(enc);
    731          curr = tf_res[i];
    732          tf_changed |= curr;
    733       }
    734       else
    735          tf_res[i] = curr;
    736       logp = isTransient ? 4 : 5;
    737    }
    738    /* Only code tf_select if it would actually make a difference. */
    739    if (tf_select_rsv &&
    740          tf_select_table[LM][4*isTransient+0+tf_changed]!=
    741          tf_select_table[LM][4*isTransient+2+tf_changed])
    742       ec_enc_bit_logp(enc, tf_select, 1);
    743    else
    744       tf_select = 0;
    745    for (i=start;i<end;i++)
    746       tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
    747    /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
    748 }
    749 
    750 static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
    751 {
    752    int i, curr, tf_select;
    753    int tf_select_rsv;
    754    int tf_changed;
    755    int logp;
    756    opus_uint32 budget;
    757    opus_uint32 tell;
    758 
    759    budget = dec->storage*8;
    760    tell = ec_tell(dec);
    761    logp = isTransient ? 2 : 4;
    762    tf_select_rsv = LM>0 && tell+logp+1<=budget;
    763    budget -= tf_select_rsv;
    764    tf_changed = curr = 0;
    765    for (i=start;i<end;i++)
    766    {
    767       if (tell+logp<=budget)
    768       {
    769          curr ^= ec_dec_bit_logp(dec, logp);
    770          tell = ec_tell(dec);
    771          tf_changed |= curr;
    772       }
    773       tf_res[i] = curr;
    774       logp = isTransient ? 4 : 5;
    775    }
    776    tf_select = 0;
    777    if (tf_select_rsv &&
    778      tf_select_table[LM][4*isTransient+0+tf_changed] !=
    779      tf_select_table[LM][4*isTransient+2+tf_changed])
    780    {
    781       tf_select = ec_dec_bit_logp(dec, 1);
    782    }
    783    for (i=start;i<end;i++)
    784    {
    785       tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
    786    }
    787 }
    788 
    789 static void init_caps(const CELTMode *m,int *cap,int LM,int C)
    790 {
    791    int i;
    792    for (i=0;i<m->nbEBands;i++)
    793    {
    794       int N;
    795       N=(m->eBands[i+1]-m->eBands[i])<<LM;
    796       cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
    797    }
    798 }
    799 
    800 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
    801       const opus_val16 *bandLogE, int end, int LM, int C, int N0)
    802 {
    803    int i;
    804    opus_val32 diff=0;
    805    int c;
    806    int trim_index = 5;
    807    if (C==2)
    808    {
    809       opus_val16 sum = 0; /* Q10 */
    810       /* Compute inter-channel correlation for low frequencies */
    811       for (i=0;i<8;i++)
    812       {
    813          int j;
    814          opus_val32 partial = 0;
    815          for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
    816             partial = MAC16_16(partial, X[j], X[N0+j]);
    817          sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
    818       }
    819       sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
    820       /*printf ("%f\n", sum);*/
    821       if (sum > QCONST16(.995f,10))
    822          trim_index-=4;
    823       else if (sum > QCONST16(.92f,10))
    824          trim_index-=3;
    825       else if (sum > QCONST16(.85f,10))
    826          trim_index-=2;
    827       else if (sum > QCONST16(.8f,10))
    828          trim_index-=1;
    829    }
    830 
    831    /* Estimate spectral tilt */
    832    c=0; do {
    833       for (i=0;i<end-1;i++)
    834       {
    835          diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
    836       }
    837    } while (++c<C);
    838    /* We divide by two here to avoid making the tilt larger for stereo as a
    839       result of a bug in the loop above */
    840    diff /= 2*C*(end-1);
    841    /*printf("%f\n", diff);*/
    842    if (diff > QCONST16(2.f, DB_SHIFT))
    843       trim_index--;
    844    if (diff > QCONST16(8.f, DB_SHIFT))
    845       trim_index--;
    846    if (diff < -QCONST16(4.f, DB_SHIFT))
    847       trim_index++;
    848    if (diff < -QCONST16(10.f, DB_SHIFT))
    849       trim_index++;
    850 
    851    if (trim_index<0)
    852       trim_index = 0;
    853    if (trim_index>10)
    854       trim_index = 10;
    855 #ifdef FUZZING
    856    trim_index = rand()%11;
    857 #endif
    858    return trim_index;
    859 }
    860 
    861 static int stereo_analysis(const CELTMode *m, const celt_norm *X,
    862       int LM, int N0)
    863 {
    864    int i;
    865    int thetas;
    866    opus_val32 sumLR = EPSILON, sumMS = EPSILON;
    867 
    868    /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
    869    for (i=0;i<13;i++)
    870    {
    871       int j;
    872       for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
    873       {
    874          opus_val32 L, R, M, S;
    875          /* We cast to 32-bit first because of the -32768 case */
    876          L = EXTEND32(X[j]);
    877          R = EXTEND32(X[N0+j]);
    878          M = ADD32(L, R);
    879          S = SUB32(L, R);
    880          sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
    881          sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
    882       }
    883    }
    884    sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
    885    thetas = 13;
    886    /* We don't need thetas for lower bands with LM<=1 */
    887    if (LM<=1)
    888       thetas -= 8;
    889    return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
    890          > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
    891 }
    892 
    893 int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
    894 {
    895    int i, c, N;
    896    opus_int32 bits;
    897    ec_enc _enc;
    898    VARDECL(celt_sig, in);
    899    VARDECL(celt_sig, freq);
    900    VARDECL(celt_norm, X);
    901    VARDECL(celt_ener, bandE);
    902    VARDECL(opus_val16, bandLogE);
    903    VARDECL(int, fine_quant);
    904    VARDECL(opus_val16, error);
    905    VARDECL(int, pulses);
    906    VARDECL(int, cap);
    907    VARDECL(int, offsets);
    908    VARDECL(int, fine_priority);
    909    VARDECL(int, tf_res);
    910    VARDECL(unsigned char, collapse_masks);
    911    celt_sig *prefilter_mem;
    912    opus_val16 *oldBandE, *oldLogE, *oldLogE2;
    913    int shortBlocks=0;
    914    int isTransient=0;
    915    const int CC = st->channels;
    916    const int C = st->stream_channels;
    917    int LM, M;
    918    int tf_select;
    919    int nbFilledBytes, nbAvailableBytes;
    920    int effEnd;
    921    int codedBands;
    922    int tf_sum;
    923    int alloc_trim;
    924    int pitch_index=COMBFILTER_MINPERIOD;
    925    opus_val16 gain1 = 0;
    926    int intensity=0;
    927    int dual_stereo=0;
    928    int effectiveBytes;
    929    opus_val16 pf_threshold;
    930    int dynalloc_logp;
    931    opus_int32 vbr_rate;
    932    opus_int32 total_bits;
    933    opus_int32 total_boost;
    934    opus_int32 balance;
    935    opus_int32 tell;
    936    int prefilter_tapset=0;
    937    int pf_on;
    938    int anti_collapse_rsv;
    939    int anti_collapse_on=0;
    940    int silence=0;
    941    ALLOC_STACK;
    942 
    943    if (nbCompressedBytes<2 || pcm==NULL)
    944      return OPUS_BAD_ARG;
    945 
    946    frame_size *= st->upsample;
    947    for (LM=0;LM<=st->mode->maxLM;LM++)
    948       if (st->mode->shortMdctSize<<LM==frame_size)
    949          break;
    950    if (LM>st->mode->maxLM)
    951       return OPUS_BAD_ARG;
    952    M=1<<LM;
    953    N = M*st->mode->shortMdctSize;
    954 
    955    prefilter_mem = st->in_mem+CC*(st->overlap);
    956    oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD));
    957    oldLogE = oldBandE + CC*st->mode->nbEBands;
    958    oldLogE2 = oldLogE + CC*st->mode->nbEBands;
    959 
    960    if (enc==NULL)
    961    {
    962       tell=1;
    963       nbFilledBytes=0;
    964    } else {
    965       tell=ec_tell(enc);
    966       nbFilledBytes=(tell+4)>>3;
    967    }
    968 
    969 #ifdef CUSTOM_MODES
    970    if (st->signalling && enc==NULL)
    971    {
    972       int tmp = (st->mode->effEBands-st->end)>>1;
    973       st->end = IMAX(1, st->mode->effEBands-tmp);
    974       compressed[0] = tmp<<5;
    975       compressed[0] |= LM<<3;
    976       compressed[0] |= (C==2)<<2;
    977       /* Convert "standard mode" to Opus header */
    978       if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
    979       {
    980          int c0 = toOpus(compressed[0]);
    981          if (c0<0)
    982             return OPUS_BAD_ARG;
    983          compressed[0] = c0;
    984       }
    985       compressed++;
    986       nbCompressedBytes--;
    987    }
    988 #else
    989    celt_assert(st->signalling==0);
    990 #endif
    991 
    992    /* Can't produce more than 1275 output bytes */
    993    nbCompressedBytes = IMIN(nbCompressedBytes,1275);
    994    nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
    995 
    996    if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
    997    {
    998       opus_int32 den=st->mode->Fs>>BITRES;
    999       vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
   1000 #ifdef CUSTOM_MODES
   1001       if (st->signalling)
   1002          vbr_rate -= 8<<BITRES;
   1003 #endif
   1004       effectiveBytes = vbr_rate>>(3+BITRES);
   1005    } else {
   1006       opus_int32 tmp;
   1007       vbr_rate = 0;
   1008       tmp = st->bitrate*frame_size;
   1009       if (tell>1)
   1010          tmp += tell;
   1011       if (st->bitrate!=OPUS_BITRATE_MAX)
   1012          nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
   1013                (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
   1014       effectiveBytes = nbCompressedBytes;
   1015    }
   1016 
   1017    if (enc==NULL)
   1018    {
   1019       ec_enc_init(&_enc, compressed, nbCompressedBytes);
   1020       enc = &_enc;
   1021    }
   1022 
   1023    if (vbr_rate>0)
   1024    {
   1025       /* Computes the max bit-rate allowed in VBR mode to avoid violating the
   1026           target rate and buffering.
   1027          We must do this up front so that bust-prevention logic triggers
   1028           correctly if we don't have enough bits. */
   1029       if (st->constrained_vbr)
   1030       {
   1031          opus_int32 vbr_bound;
   1032          opus_int32 max_allowed;
   1033          /* We could use any multiple of vbr_rate as bound (depending on the
   1034              delay).
   1035             This is clamped to ensure we use at least two bytes if the encoder
   1036              was entirely empty, but to allow 0 in hybrid mode. */
   1037          vbr_bound = vbr_rate;
   1038          max_allowed = IMIN(IMAX(tell==1?2:0,
   1039                (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
   1040                nbAvailableBytes);
   1041          if(max_allowed < nbAvailableBytes)
   1042          {
   1043             nbCompressedBytes = nbFilledBytes+max_allowed;
   1044             nbAvailableBytes = max_allowed;
   1045             ec_enc_shrink(enc, nbCompressedBytes);
   1046          }
   1047       }
   1048    }
   1049    total_bits = nbCompressedBytes*8;
   1050 
   1051    effEnd = st->end;
   1052    if (effEnd > st->mode->effEBands)
   1053       effEnd = st->mode->effEBands;
   1054 
   1055    ALLOC(in, CC*(N+st->overlap), celt_sig);
   1056 
   1057    /* Find pitch period and gain */
   1058    {
   1059       VARDECL(celt_sig, _pre);
   1060       celt_sig *pre[2];
   1061       SAVE_STACK;
   1062       ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
   1063 
   1064       pre[0] = _pre;
   1065       pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
   1066 
   1067       silence = 1;
   1068       c=0; do {
   1069          int count = 0;
   1070          const opus_val16 * OPUS_RESTRICT pcmp = pcm+c;
   1071          celt_sig * OPUS_RESTRICT inp = in+c*(N+st->overlap)+st->overlap;
   1072 
   1073          for (i=0;i<N;i++)
   1074          {
   1075             celt_sig x, tmp;
   1076 
   1077             x = SCALEIN(*pcmp);
   1078 #ifndef FIXED_POINT
   1079             if (!(x==x))
   1080                x = 0;
   1081             if (st->clip)
   1082                x = MAX32(-65536.f, MIN32(65536.f,x));
   1083 #endif
   1084             if (++count==st->upsample)
   1085             {
   1086                count=0;
   1087                pcmp+=CC;
   1088             } else {
   1089                x = 0;
   1090             }
   1091             /* Apply pre-emphasis */
   1092             tmp = MULT16_16(st->mode->preemph[2], x);
   1093             *inp = tmp + st->preemph_memE[c];
   1094             st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
   1095                                    - MULT16_32_Q15(st->mode->preemph[0], tmp);
   1096             silence = silence && *inp == 0;
   1097             inp++;
   1098          }
   1099          OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
   1100          OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
   1101       } while (++c<CC);
   1102 
   1103 #ifdef FUZZING
   1104       if ((rand()&0x3F)==0)
   1105          silence = 1;
   1106 #endif
   1107       if (tell==1)
   1108          ec_enc_bit_logp(enc, silence, 15);
   1109       else
   1110          silence=0;
   1111       if (silence)
   1112       {
   1113          /*In VBR mode there is no need to send more than the minimum. */
   1114          if (vbr_rate>0)
   1115          {
   1116             effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
   1117             total_bits=nbCompressedBytes*8;
   1118             nbAvailableBytes=2;
   1119             ec_enc_shrink(enc, nbCompressedBytes);
   1120          }
   1121          /* Pretend we've filled all the remaining bits with zeros
   1122             (that's what the initialiser did anyway) */
   1123          tell = nbCompressedBytes*8;
   1124          enc->nbits_total+=tell-ec_tell(enc);
   1125       }
   1126       if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
   1127       {
   1128          VARDECL(opus_val16, pitch_buf);
   1129          ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
   1130 
   1131          pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
   1132          pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
   1133                COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
   1134          pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
   1135 
   1136          gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
   1137                N, &pitch_index, st->prefilter_period, st->prefilter_gain);
   1138          if (pitch_index > COMBFILTER_MAXPERIOD-2)
   1139             pitch_index = COMBFILTER_MAXPERIOD-2;
   1140          gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
   1141          if (st->loss_rate>2)
   1142             gain1 = HALF32(gain1);
   1143          if (st->loss_rate>4)
   1144             gain1 = HALF32(gain1);
   1145          if (st->loss_rate>8)
   1146             gain1 = 0;
   1147          prefilter_tapset = st->tapset_decision;
   1148       } else {
   1149          gain1 = 0;
   1150       }
   1151 
   1152       /* Gain threshold for enabling the prefilter/postfilter */
   1153       pf_threshold = QCONST16(.2f,15);
   1154 
   1155       /* Adjusting the threshold based on rate and continuity */
   1156       if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
   1157          pf_threshold += QCONST16(.2f,15);
   1158       if (nbAvailableBytes<25)
   1159          pf_threshold += QCONST16(.1f,15);
   1160       if (nbAvailableBytes<35)
   1161          pf_threshold += QCONST16(.1f,15);
   1162       if (st->prefilter_gain > QCONST16(.4f,15))
   1163          pf_threshold -= QCONST16(.1f,15);
   1164       if (st->prefilter_gain > QCONST16(.55f,15))
   1165          pf_threshold -= QCONST16(.1f,15);
   1166 
   1167       /* Hard threshold at 0.2 */
   1168       pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
   1169       if (gain1<pf_threshold)
   1170       {
   1171          if(st->start==0 && tell+16<=total_bits)
   1172             ec_enc_bit_logp(enc, 0, 1);
   1173          gain1 = 0;
   1174          pf_on = 0;
   1175       } else {
   1176          /*This block is not gated by a total bits check only because
   1177            of the nbAvailableBytes check above.*/
   1178          int qg;
   1179          int octave;
   1180 
   1181          if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
   1182             gain1=st->prefilter_gain;
   1183 
   1184 #ifdef FIXED_POINT
   1185          qg = ((gain1+1536)>>10)/3-1;
   1186 #else
   1187          qg = (int)floor(.5f+gain1*32/3)-1;
   1188 #endif
   1189          qg = IMAX(0, IMIN(7, qg));
   1190          ec_enc_bit_logp(enc, 1, 1);
   1191          pitch_index += 1;
   1192          octave = EC_ILOG(pitch_index)-5;
   1193          ec_enc_uint(enc, octave, 6);
   1194          ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
   1195          pitch_index -= 1;
   1196          ec_enc_bits(enc, qg, 3);
   1197          if (ec_tell(enc)+2<=total_bits)
   1198             ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
   1199          else
   1200            prefilter_tapset = 0;
   1201          gain1 = QCONST16(0.09375f,15)*(qg+1);
   1202          pf_on = 1;
   1203       }
   1204       /*printf("%d %f\n", pitch_index, gain1);*/
   1205 
   1206       c=0; do {
   1207          int offset = st->mode->shortMdctSize-st->mode->overlap;
   1208          st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
   1209          OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
   1210          if (offset)
   1211             comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
   1212                   st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
   1213                   st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
   1214 
   1215          comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
   1216                st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
   1217                st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
   1218          OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
   1219 
   1220          if (N>COMBFILTER_MAXPERIOD)
   1221          {
   1222             OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
   1223          } else {
   1224             OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
   1225             OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
   1226          }
   1227       } while (++c<CC);
   1228 
   1229       RESTORE_STACK;
   1230    }
   1231 
   1232    isTransient = 0;
   1233    shortBlocks = 0;
   1234    if (LM>0 && ec_tell(enc)+3<=total_bits)
   1235    {
   1236       if (st->complexity > 1)
   1237       {
   1238          isTransient = transient_analysis(in, N+st->overlap, CC,
   1239                   st->overlap);
   1240          if (isTransient)
   1241             shortBlocks = M;
   1242       }
   1243       ec_enc_bit_logp(enc, isTransient, 3);
   1244    }
   1245 
   1246    ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
   1247    ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
   1248    ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
   1249    /* Compute MDCTs */
   1250    compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
   1251 
   1252    if (CC==2&&C==1)
   1253    {
   1254       for (i=0;i<N;i++)
   1255          freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
   1256    }
   1257    if (st->upsample != 1)
   1258    {
   1259       c=0; do
   1260       {
   1261          int bound = N/st->upsample;
   1262          for (i=0;i<bound;i++)
   1263             freq[c*N+i] *= st->upsample;
   1264          for (;i<N;i++)
   1265             freq[c*N+i] = 0;
   1266       } while (++c<C);
   1267    }
   1268    ALLOC(X, C*N, celt_norm);         /**< Interleaved normalised MDCTs */
   1269 
   1270    compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
   1271 
   1272    amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
   1273 
   1274    /* Band normalisation */
   1275    normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
   1276 
   1277    ALLOC(tf_res, st->mode->nbEBands, int);
   1278    tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, st->start, &tf_sum);
   1279    for (i=effEnd;i<st->end;i++)
   1280       tf_res[i] = tf_res[effEnd-1];
   1281 
   1282    ALLOC(error, C*st->mode->nbEBands, opus_val16);
   1283    quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
   1284          oldBandE, total_bits, error, enc,
   1285          C, LM, nbAvailableBytes, st->force_intra,
   1286          &st->delayedIntra, st->complexity >= 4, st->loss_rate);
   1287 
   1288    tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
   1289 
   1290    if (ec_tell(enc)+4<=total_bits)
   1291    {
   1292       if (shortBlocks || st->complexity < 3
   1293           || nbAvailableBytes < 10*C || st->start!=0)
   1294       {
   1295          if (st->complexity == 0)
   1296             st->spread_decision = SPREAD_NONE;
   1297          else
   1298             st->spread_decision = SPREAD_NORMAL;
   1299       } else {
   1300          st->spread_decision = spreading_decision(st->mode, X,
   1301                &st->tonal_average, st->spread_decision, &st->hf_average,
   1302                &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
   1303       }
   1304       ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
   1305    }
   1306 
   1307    ALLOC(cap, st->mode->nbEBands, int);
   1308    ALLOC(offsets, st->mode->nbEBands, int);
   1309 
   1310    init_caps(st->mode,cap,LM,C);
   1311    for (i=0;i<st->mode->nbEBands;i++)
   1312       offsets[i] = 0;
   1313    /* Dynamic allocation code */
   1314    /* Make sure that dynamic allocation can't make us bust the budget */
   1315    if (effectiveBytes > 50 && LM>=1)
   1316    {
   1317       int t1, t2;
   1318       if (LM <= 1)
   1319       {
   1320          t1 = 3;
   1321          t2 = 5;
   1322       } else {
   1323          t1 = 2;
   1324          t2 = 4;
   1325       }
   1326       for (i=st->start+1;i<st->end-1;i++)
   1327       {
   1328          opus_val32 d2;
   1329          d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
   1330          if (C==2)
   1331             d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
   1332                   bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
   1333 #ifdef FUZZING
   1334          if((rand()&0xF)==0)
   1335          {
   1336             offsets[i] += 1;
   1337             if((rand()&0x3)==0)
   1338                offsets[i] += 1+(rand()&0x3);
   1339          }
   1340 #else
   1341          if (d2 > SHL16(t1,DB_SHIFT))
   1342             offsets[i] += 1;
   1343          if (d2 > SHL16(t2,DB_SHIFT))
   1344             offsets[i] += 1;
   1345 #endif
   1346       }
   1347    }
   1348    dynalloc_logp = 6;
   1349    total_bits<<=BITRES;
   1350    total_boost = 0;
   1351    tell = ec_tell_frac(enc);
   1352    for (i=st->start;i<st->end;i++)
   1353    {
   1354       int width, quanta;
   1355       int dynalloc_loop_logp;
   1356       int boost;
   1357       int j;
   1358       width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
   1359       /* quanta is 6 bits, but no more than 1 bit/sample
   1360          and no less than 1/8 bit/sample */
   1361       quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
   1362       dynalloc_loop_logp = dynalloc_logp;
   1363       boost = 0;
   1364       for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
   1365             && boost < cap[i]; j++)
   1366       {
   1367          int flag;
   1368          flag = j<offsets[i];
   1369          ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
   1370          tell = ec_tell_frac(enc);
   1371          if (!flag)
   1372             break;
   1373          boost += quanta;
   1374          total_boost += quanta;
   1375          dynalloc_loop_logp = 1;
   1376       }
   1377       /* Making dynalloc more likely */
   1378       if (j)
   1379          dynalloc_logp = IMAX(2, dynalloc_logp-1);
   1380       offsets[i] = boost;
   1381    }
   1382    alloc_trim = 5;
   1383    if (tell+(6<<BITRES) <= total_bits - total_boost)
   1384    {
   1385       alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
   1386             st->end, LM, C, N);
   1387       ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
   1388       tell = ec_tell_frac(enc);
   1389    }
   1390 
   1391    /* Variable bitrate */
   1392    if (vbr_rate>0)
   1393    {
   1394      opus_val16 alpha;
   1395      opus_int32 delta;
   1396      /* The target rate in 8th bits per frame */
   1397      opus_int32 target;
   1398      opus_int32 min_allowed;
   1399      int lm_diff = st->mode->maxLM - LM;
   1400 
   1401      /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
   1402         The CELT allocator will just not be able to use more than that anyway. */
   1403      nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
   1404      target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES);
   1405 
   1406      /* Shortblocks get a large boost in bitrate, but since they
   1407         are uncommon long blocks are not greatly affected */
   1408      if (shortBlocks || tf_sum < -2*(st->end-st->start))
   1409         target = 7*target/4;
   1410      else if (tf_sum < -(st->end-st->start))
   1411         target = 3*target/2;
   1412      else if (M > 1)
   1413         target-=(target+14)/28;
   1414 
   1415      /* The current offset is removed from the target and the space used
   1416         so far is added*/
   1417      target=target+tell;
   1418 
   1419      /* In VBR mode the frame size must not be reduced so much that it would
   1420          result in the encoder running out of bits.
   1421         The margin of 2 bytes ensures that none of the bust-prevention logic
   1422          in the decoder will have triggered so far. */
   1423      min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
   1424 
   1425      nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
   1426      nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
   1427      nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
   1428 
   1429      /* By how much did we "miss" the target on that frame */
   1430      delta = target - vbr_rate;
   1431 
   1432      target=nbAvailableBytes<<(BITRES+3);
   1433 
   1434      /*If the frame is silent we don't adjust our drift, otherwise
   1435        the encoder will shoot to very high rates after hitting a
   1436        span of silence, but we do allow the bitres to refill.
   1437        This means that we'll undershoot our target in CVBR/VBR modes
   1438        on files with lots of silence. */
   1439      if(silence)
   1440      {
   1441        nbAvailableBytes = 2;
   1442        target = 2*8<<BITRES;
   1443        delta = 0;
   1444      }
   1445 
   1446      if (st->vbr_count < 970)
   1447      {
   1448         st->vbr_count++;
   1449         alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
   1450      } else
   1451         alpha = QCONST16(.001f,15);
   1452      /* How many bits have we used in excess of what we're allowed */
   1453      if (st->constrained_vbr)
   1454         st->vbr_reservoir += target - vbr_rate;
   1455      /*printf ("%d\n", st->vbr_reservoir);*/
   1456 
   1457      /* Compute the offset we need to apply in order to reach the target */
   1458      st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
   1459      st->vbr_offset = -st->vbr_drift;
   1460      /*printf ("%d\n", st->vbr_drift);*/
   1461 
   1462      if (st->constrained_vbr && st->vbr_reservoir < 0)
   1463      {
   1464         /* We're under the min value -- increase rate */
   1465         int adjust = (-st->vbr_reservoir)/(8<<BITRES);
   1466         /* Unless we're just coding silence */
   1467         nbAvailableBytes += silence?0:adjust;
   1468         st->vbr_reservoir = 0;
   1469         /*printf ("+%d\n", adjust);*/
   1470      }
   1471      nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
   1472      /* This moves the raw bits to take into account the new compressed size */
   1473      ec_enc_shrink(enc, nbCompressedBytes);
   1474    }
   1475    if (C==2)
   1476    {
   1477       int effectiveRate;
   1478 
   1479       /* Always use MS for 2.5 ms frames until we can do a better analysis */
   1480       if (LM!=0)
   1481          dual_stereo = stereo_analysis(st->mode, X, LM, N);
   1482 
   1483       /* Account for coarse energy */
   1484       effectiveRate = (8*effectiveBytes - 80)>>LM;
   1485 
   1486       /* effectiveRate in kb/s */
   1487       effectiveRate = 2*effectiveRate/5;
   1488       if (effectiveRate<35)
   1489          intensity = 8;
   1490       else if (effectiveRate<50)
   1491          intensity = 12;
   1492       else if (effectiveRate<68)
   1493          intensity = 16;
   1494       else if (effectiveRate<84)
   1495          intensity = 18;
   1496       else if (effectiveRate<102)
   1497          intensity = 19;
   1498       else if (effectiveRate<130)
   1499          intensity = 20;
   1500       else
   1501          intensity = 100;
   1502       intensity = IMIN(st->end,IMAX(st->start, intensity));
   1503    }
   1504 
   1505    /* Bit allocation */
   1506    ALLOC(fine_quant, st->mode->nbEBands, int);
   1507    ALLOC(pulses, st->mode->nbEBands, int);
   1508    ALLOC(fine_priority, st->mode->nbEBands, int);
   1509 
   1510    /* bits =           packet size                    - where we are - safety*/
   1511    bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
   1512    anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
   1513    bits -= anti_collapse_rsv;
   1514    codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
   1515          alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
   1516          fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
   1517    st->lastCodedBands = codedBands;
   1518 
   1519    quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
   1520 
   1521 #ifdef MEASURE_NORM_MSE
   1522    float X0[3000];
   1523    float bandE0[60];
   1524    c=0; do
   1525       for (i=0;i<N;i++)
   1526          X0[i+c*N] = X[i+c*N];
   1527    while (++c<C);
   1528    for (i=0;i<C*st->mode->nbEBands;i++)
   1529       bandE0[i] = bandE[i];
   1530 #endif
   1531 
   1532    /* Residual quantisation */
   1533    ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
   1534    quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
   1535          bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
   1536          nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
   1537 
   1538    if (anti_collapse_rsv > 0)
   1539    {
   1540       anti_collapse_on = st->consec_transient<2;
   1541 #ifdef FUZZING
   1542       anti_collapse_on = rand()&0x1;
   1543 #endif
   1544       ec_enc_bits(enc, anti_collapse_on, 1);
   1545    }
   1546    quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
   1547 
   1548    if (silence)
   1549    {
   1550       for (i=0;i<C*st->mode->nbEBands;i++)
   1551          oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
   1552    }
   1553 
   1554 #ifdef RESYNTH
   1555    /* Re-synthesis of the coded audio if required */
   1556    {
   1557       celt_sig *out_mem[2];
   1558       celt_sig *overlap_mem[2];
   1559 
   1560       log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
   1561       if (silence)
   1562       {
   1563          for (i=0;i<C*st->mode->nbEBands;i++)
   1564             bandE[i] = 0;
   1565       }
   1566 
   1567 #ifdef MEASURE_NORM_MSE
   1568       measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
   1569 #endif
   1570       if (anti_collapse_on)
   1571       {
   1572          anti_collapse(st->mode, X, collapse_masks, LM, C, N,
   1573                st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
   1574       }
   1575 
   1576       /* Synthesis */
   1577       denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
   1578 
   1579       OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
   1580       if (CC==2)
   1581          OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
   1582 
   1583       c=0; do
   1584          for (i=0;i<M*st->mode->eBands[st->start];i++)
   1585             freq[c*N+i] = 0;
   1586       while (++c<C);
   1587       c=0; do
   1588          for (i=M*st->mode->eBands[st->end];i<N;i++)
   1589             freq[c*N+i] = 0;
   1590       while (++c<C);
   1591 
   1592       if (CC==2&&C==1)
   1593       {
   1594          for (i=0;i<N;i++)
   1595             freq[N+i] = freq[i];
   1596       }
   1597 
   1598       out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
   1599       if (CC==2)
   1600          out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
   1601 
   1602       overlap_mem[0] = (celt_sig*)(oldLogE2 + CC*st->mode->nbEBands);
   1603       if (CC==2)
   1604          overlap_mem[1] = overlap_mem[0] + st->overlap;
   1605 
   1606       compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
   1607 
   1608       c=0; do {
   1609          st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
   1610          st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
   1611          comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
   1612                st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
   1613                st->mode->window, st->overlap);
   1614          if (LM!=0)
   1615             comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
   1616                   st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
   1617                   st->mode->window, st->mode->overlap);
   1618       } while (++c<CC);
   1619 
   1620       deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
   1621       st->prefilter_period_old = st->prefilter_period;
   1622       st->prefilter_gain_old = st->prefilter_gain;
   1623       st->prefilter_tapset_old = st->prefilter_tapset;
   1624    }
   1625 #endif
   1626 
   1627    st->prefilter_period = pitch_index;
   1628    st->prefilter_gain = gain1;
   1629    st->prefilter_tapset = prefilter_tapset;
   1630 #ifdef RESYNTH
   1631    if (LM!=0)
   1632    {
   1633       st->prefilter_period_old = st->prefilter_period;
   1634       st->prefilter_gain_old = st->prefilter_gain;
   1635       st->prefilter_tapset_old = st->prefilter_tapset;
   1636    }
   1637 #endif
   1638 
   1639    if (CC==2&&C==1) {
   1640       for (i=0;i<st->mode->nbEBands;i++)
   1641          oldBandE[st->mode->nbEBands+i]=oldBandE[i];
   1642    }
   1643 
   1644    if (!isTransient)
   1645    {
   1646       for (i=0;i<CC*st->mode->nbEBands;i++)
   1647          oldLogE2[i] = oldLogE[i];
   1648       for (i=0;i<CC*st->mode->nbEBands;i++)
   1649          oldLogE[i] = oldBandE[i];
   1650    } else {
   1651       for (i=0;i<CC*st->mode->nbEBands;i++)
   1652          oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
   1653    }
   1654    /* In case start or end were to change */
   1655    c=0; do
   1656    {
   1657       for (i=0;i<st->start;i++)
   1658       {
   1659          oldBandE[c*st->mode->nbEBands+i]=0;
   1660          oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
   1661       }
   1662       for (i=st->end;i<st->mode->nbEBands;i++)
   1663       {
   1664          oldBandE[c*st->mode->nbEBands+i]=0;
   1665          oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
   1666       }
   1667    } while (++c<CC);
   1668 
   1669    if (isTransient)
   1670       st->consec_transient++;
   1671    else
   1672       st->consec_transient=0;
   1673    st->rng = enc->rng;
   1674 
   1675    /* If there's any room left (can only happen for very high rates),
   1676       it's already filled with zeros */
   1677    ec_enc_done(enc);
   1678 
   1679 #ifdef CUSTOM_MODES
   1680    if (st->signalling)
   1681       nbCompressedBytes++;
   1682 #endif
   1683 
   1684    RESTORE_STACK;
   1685    if (ec_get_error(enc))
   1686       return OPUS_INTERNAL_ERROR;
   1687    else
   1688       return nbCompressedBytes;
   1689 }
   1690 
   1691 
   1692 #ifdef CUSTOM_MODES
   1693 
   1694 #ifdef FIXED_POINT
   1695 int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
   1696 {
   1697    return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
   1698 }
   1699 
   1700 #ifndef DISABLE_FLOAT_API
   1701 int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
   1702 {
   1703    int j, ret, C, N;
   1704    VARDECL(opus_int16, in);
   1705    ALLOC_STACK;
   1706 
   1707    if (pcm==NULL)
   1708       return OPUS_BAD_ARG;
   1709 
   1710    C = st->channels;
   1711    N = frame_size;
   1712    ALLOC(in, C*N, opus_int16);
   1713 
   1714    for (j=0;j<C*N;j++)
   1715      in[j] = FLOAT2INT16(pcm[j]);
   1716 
   1717    ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
   1718 #ifdef RESYNTH
   1719    for (j=0;j<C*N;j++)
   1720       ((float*)pcm)[j]=in[j]*(1.f/32768.f);
   1721 #endif
   1722    RESTORE_STACK;
   1723    return ret;
   1724 }
   1725 #endif /* DISABLE_FLOAT_API */
   1726 #else
   1727 
   1728 int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
   1729 {
   1730    int j, ret, C, N;
   1731    VARDECL(celt_sig, in);
   1732    ALLOC_STACK;
   1733 
   1734    if (pcm==NULL)
   1735       return OPUS_BAD_ARG;
   1736 
   1737    C=st->channels;
   1738    N=frame_size;
   1739    ALLOC(in, C*N, celt_sig);
   1740    for (j=0;j<C*N;j++) {
   1741      in[j] = SCALEOUT(pcm[j]);
   1742    }
   1743 
   1744    ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
   1745 #ifdef RESYNTH
   1746    for (j=0;j<C*N;j++)
   1747       ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
   1748 #endif
   1749    RESTORE_STACK;
   1750    return ret;
   1751 }
   1752 
   1753 int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
   1754 {
   1755    return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
   1756 }
   1757 
   1758 #endif
   1759 
   1760 #endif /* CUSTOM_MODES */
   1761 
   1762 int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
   1763 {
   1764    va_list ap;
   1765 
   1766    va_start(ap, request);
   1767    switch (request)
   1768    {
   1769       case OPUS_SET_COMPLEXITY_REQUEST:
   1770       {
   1771          int value = va_arg(ap, opus_int32);
   1772          if (value<0 || value>10)
   1773             goto bad_arg;
   1774          st->complexity = value;
   1775       }
   1776       break;
   1777       case CELT_SET_START_BAND_REQUEST:
   1778       {
   1779          opus_int32 value = va_arg(ap, opus_int32);
   1780          if (value<0 || value>=st->mode->nbEBands)
   1781             goto bad_arg;
   1782          st->start = value;
   1783       }
   1784       break;
   1785       case CELT_SET_END_BAND_REQUEST:
   1786       {
   1787          opus_int32 value = va_arg(ap, opus_int32);
   1788          if (value<1 || value>st->mode->nbEBands)
   1789             goto bad_arg;
   1790          st->end = value;
   1791       }
   1792       break;
   1793       case CELT_SET_PREDICTION_REQUEST:
   1794       {
   1795          int value = va_arg(ap, opus_int32);
   1796          if (value<0 || value>2)
   1797             goto bad_arg;
   1798          st->disable_pf = value<=1;
   1799          st->force_intra = value==0;
   1800       }
   1801       break;
   1802       case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
   1803       {
   1804          int value = va_arg(ap, opus_int32);
   1805          if (value<0 || value>100)
   1806             goto bad_arg;
   1807          st->loss_rate = value;
   1808       }
   1809       break;
   1810       case OPUS_SET_VBR_CONSTRAINT_REQUEST:
   1811       {
   1812          opus_int32 value = va_arg(ap, opus_int32);
   1813          st->constrained_vbr = value;
   1814       }
   1815       break;
   1816       case OPUS_SET_VBR_REQUEST:
   1817       {
   1818          opus_int32 value = va_arg(ap, opus_int32);
   1819          st->vbr = value;
   1820       }
   1821       break;
   1822       case OPUS_SET_BITRATE_REQUEST:
   1823       {
   1824          opus_int32 value = va_arg(ap, opus_int32);
   1825          if (value<=500 && value!=OPUS_BITRATE_MAX)
   1826             goto bad_arg;
   1827          value = IMIN(value, 260000*st->channels);
   1828          st->bitrate = value;
   1829       }
   1830       break;
   1831       case CELT_SET_CHANNELS_REQUEST:
   1832       {
   1833          opus_int32 value = va_arg(ap, opus_int32);
   1834          if (value<1 || value>2)
   1835             goto bad_arg;
   1836          st->stream_channels = value;
   1837       }
   1838       break;
   1839       case OPUS_SET_LSB_DEPTH_REQUEST:
   1840       {
   1841           opus_int32 value = va_arg(ap, opus_int32);
   1842           if (value<8 || value>24)
   1843              goto bad_arg;
   1844           st->lsb_depth=value;
   1845       }
   1846       break;
   1847       case OPUS_GET_LSB_DEPTH_REQUEST:
   1848       {
   1849           opus_int32 *value = va_arg(ap, opus_int32*);
   1850           *value=st->lsb_depth;
   1851       }
   1852       break;
   1853       case OPUS_RESET_STATE:
   1854       {
   1855          int i;
   1856          opus_val16 *oldBandE, *oldLogE, *oldLogE2;
   1857          oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTER_MAXPERIOD));
   1858          oldLogE = oldBandE + st->channels*st->mode->nbEBands;
   1859          oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
   1860          OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
   1861                opus_custom_encoder_get_size(st->mode, st->channels)-
   1862                ((char*)&st->ENCODER_RESET_START - (char*)st));
   1863          for (i=0;i<st->channels*st->mode->nbEBands;i++)
   1864             oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
   1865          st->vbr_offset = 0;
   1866          st->delayedIntra = 1;
   1867          st->spread_decision = SPREAD_NORMAL;
   1868          st->tonal_average = 256;
   1869          st->hf_average = 0;
   1870          st->tapset_decision = 0;
   1871       }
   1872       break;
   1873 #ifdef CUSTOM_MODES
   1874       case CELT_SET_INPUT_CLIPPING_REQUEST:
   1875       {
   1876          opus_int32 value = va_arg(ap, opus_int32);
   1877          st->clip = value;
   1878       }
   1879       break;
   1880 #endif
   1881       case CELT_SET_SIGNALLING_REQUEST:
   1882       {
   1883          opus_int32 value = va_arg(ap, opus_int32);
   1884          st->signalling = value;
   1885       }
   1886       break;
   1887       case CELT_GET_MODE_REQUEST:
   1888       {
   1889          const CELTMode ** value = va_arg(ap, const CELTMode**);
   1890          if (value==0)
   1891             goto bad_arg;
   1892          *value=st->mode;
   1893       }
   1894       break;
   1895       case OPUS_GET_FINAL_RANGE_REQUEST:
   1896       {
   1897          opus_uint32 * value = va_arg(ap, opus_uint32 *);
   1898          if (value==0)
   1899             goto bad_arg;
   1900          *value=st->rng;
   1901       }
   1902       break;
   1903       default:
   1904          goto bad_request;
   1905    }
   1906    va_end(ap);
   1907    return OPUS_OK;
   1908 bad_arg:
   1909    va_end(ap);
   1910    return OPUS_BAD_ARG;
   1911 bad_request:
   1912    va_end(ap);
   1913    return OPUS_UNIMPLEMENTED;
   1914 }
   1915 
   1916 /**********************************************************************/
   1917 /*                                                                    */
   1918 /*                             DECODER                                */
   1919 /*                                                                    */
   1920 /**********************************************************************/
   1921 #define DECODE_BUFFER_SIZE 2048
   1922 
   1923 /** Decoder state
   1924  @brief Decoder state
   1925  */
   1926 struct OpusCustomDecoder {
   1927    const OpusCustomMode *mode;
   1928    int overlap;
   1929    int channels;
   1930    int stream_channels;
   1931 
   1932    int downsample;
   1933    int start, end;
   1934    int signalling;
   1935 
   1936    /* Everything beyond this point gets cleared on a reset */
   1937 #define DECODER_RESET_START rng
   1938 
   1939    opus_uint32 rng;
   1940    int error;
   1941    int last_pitch_index;
   1942    int loss_count;
   1943    int postfilter_period;
   1944    int postfilter_period_old;
   1945    opus_val16 postfilter_gain;
   1946    opus_val16 postfilter_gain_old;
   1947    int postfilter_tapset;
   1948    int postfilter_tapset_old;
   1949 
   1950    celt_sig preemph_memD[2];
   1951 
   1952    celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
   1953    /* opus_val16 lpc[],  Size = channels*LPC_ORDER */
   1954    /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
   1955    /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
   1956    /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
   1957    /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
   1958 };
   1959 
   1960 int celt_decoder_get_size(int channels)
   1961 {
   1962    const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
   1963    return opus_custom_decoder_get_size(mode, channels);
   1964 }
   1965 
   1966 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
   1967 {
   1968    int size = sizeof(struct CELTDecoder)
   1969             + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
   1970             + channels*LPC_ORDER*sizeof(opus_val16)
   1971             + 4*2*mode->nbEBands*sizeof(opus_val16);
   1972    return size;
   1973 }
   1974 
   1975 #ifdef CUSTOM_MODES
   1976 CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
   1977 {
   1978    int ret;
   1979    CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
   1980    ret = opus_custom_decoder_init(st, mode, channels);
   1981    if (ret != OPUS_OK)
   1982    {
   1983       opus_custom_decoder_destroy(st);
   1984       st = NULL;
   1985    }
   1986    if (error)
   1987       *error = ret;
   1988    return st;
   1989 }
   1990 #endif /* CUSTOM_MODES */
   1991 
   1992 int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
   1993 {
   1994    int ret;
   1995    ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
   1996    if (ret != OPUS_OK)
   1997       return ret;
   1998    st->downsample = resampling_factor(sampling_rate);
   1999    if (st->downsample==0)
   2000       return OPUS_BAD_ARG;
   2001    else
   2002       return OPUS_OK;
   2003 }
   2004 
   2005 OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
   2006 {
   2007    if (channels < 0 || channels > 2)
   2008       return OPUS_BAD_ARG;
   2009 
   2010    if (st==NULL)
   2011       return OPUS_ALLOC_FAIL;
   2012 
   2013    OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
   2014 
   2015    st->mode = mode;
   2016    st->overlap = mode->overlap;
   2017    st->stream_channels = st->channels = channels;
   2018 
   2019    st->downsample = 1;
   2020    st->start = 0;
   2021    st->end = st->mode->effEBands;
   2022    st->signalling = 1;
   2023 
   2024    st->loss_count = 0;
   2025 
   2026    opus_custom_decoder_ctl(st, OPUS_RESET_STATE);
   2027 
   2028    return OPUS_OK;
   2029 }
   2030 
   2031 #ifdef CUSTOM_MODES
   2032 void opus_custom_decoder_destroy(CELTDecoder *st)
   2033 {
   2034    opus_free(st);
   2035 }
   2036 #endif /* CUSTOM_MODES */
   2037 
   2038 static void celt_decode_lost(CELTDecoder * OPUS_RESTRICT st, opus_val16 * OPUS_RESTRICT pcm, int N, int LM)
   2039 {
   2040    int c;
   2041    int pitch_index;
   2042    opus_val16 fade = Q15ONE;
   2043    int i, len;
   2044    const int C = st->channels;
   2045    int offset;
   2046    celt_sig *out_mem[2];
   2047    celt_sig *decode_mem[2];
   2048    celt_sig *overlap_mem[2];
   2049    opus_val16 *lpc;
   2050    opus_val32 *out_syn[2];
   2051    opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
   2052    const OpusCustomMode *mode;
   2053    int nbEBands;
   2054    int overlap;
   2055    const opus_int16 *eBands;
   2056    SAVE_STACK;
   2057 
   2058    mode = st->mode;
   2059    nbEBands = mode->nbEBands;
   2060    overlap = mode->overlap;
   2061    eBands = mode->eBands;
   2062 
   2063    c=0; do {
   2064       decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
   2065       out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
   2066       overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
   2067    } while (++c<C);
   2068    lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
   2069    oldBandE = lpc+C*LPC_ORDER;
   2070    oldLogE = oldBandE + 2*nbEBands;
   2071    oldLogE2 = oldLogE + 2*nbEBands;
   2072    backgroundLogE = oldLogE2  + 2*nbEBands;
   2073 
   2074    c=0; do {
   2075       out_syn[c] = out_mem[c]+MAX_PERIOD-N;
   2076    } while (++c<C);
   2077 
   2078    len = N+overlap;
   2079 
   2080    if (st->loss_count >= 5 || st->start!=0)
   2081    {
   2082       /* Noise-based PLC/CNG */
   2083       VARDECL(celt_sig, freq);
   2084       VARDECL(celt_norm, X);
   2085       VARDECL(celt_ener, bandE);
   2086       opus_uint32 seed;
   2087       int effEnd;
   2088 
   2089       effEnd = st->end;
   2090       if (effEnd > mode->effEBands)
   2091          effEnd = mode->effEBands;
   2092 
   2093       ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
   2094       ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
   2095       ALLOC(bandE, nbEBands*C, celt_ener);
   2096 
   2097       if (st->loss_count >= 5)
   2098          log2Amp(mode, st->start, st->end, bandE, backgroundLogE, C);
   2099       else {
   2100          /* Energy decay */
   2101          opus_val16 decay = st->loss_count==0 ? QCONST16(1.5f, DB_SHIFT) : QCONST16(.5f, DB_SHIFT);
   2102          c=0; do
   2103          {
   2104             for (i=st->start;i<st->end;i++)
   2105                oldBandE[c*nbEBands+i] -= decay;
   2106          } while (++c<C);
   2107          log2Amp(mode, st->start, st->end, bandE, oldBandE, C);
   2108       }
   2109       seed = st->rng;
   2110       for (c=0;c<C;c++)
   2111       {
   2112          for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
   2113             X[c*N+i] = 0;
   2114          for (i=st->start;i<mode->effEBands;i++)
   2115          {
   2116             int j;
   2117             int boffs;
   2118             int blen;
   2119             boffs = N*c+(eBands[i]<<LM);
   2120             blen = (eBands[i+1]-eBands[i])<<LM;
   2121             for (j=0;j<blen;j++)
   2122             {
   2123                seed = celt_lcg_rand(seed);
   2124                X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
   2125             }
   2126             renormalise_vector(X+boffs, blen, Q15ONE);
   2127          }
   2128          for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
   2129             X[c*N+i] = 0;
   2130       }
   2131       st->rng = seed;
   2132 
   2133       denormalise_bands(mode, X, freq, bandE, mode->effEBands, C, 1<<LM);
   2134 
   2135       c=0; do
   2136          for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
   2137             freq[c*N+i] = 0;
   2138       while (++c<C);
   2139       c=0; do {
   2140          int bound = eBands[effEnd]<<LM;
   2141          if (st->downsample!=1)
   2142             bound = IMIN(bound, N/st->downsample);
   2143          for (i=bound;i<N;i++)
   2144             freq[c*N+i] = 0;
   2145       } while (++c<C);
   2146       c=0; do {
   2147          OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap);
   2148       } while (++c<C);
   2149       compute_inv_mdcts(mode, 0, freq, out_syn, overlap_mem, C, LM);
   2150    } else {
   2151       /* Pitch-based PLC */
   2152       VARDECL(opus_val32, etmp);
   2153 
   2154       if (st->loss_count == 0)
   2155       {
   2156          opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
   2157          /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
   2158          search by using only part of the decode buffer */
   2159          int poffset = 720;
   2160          pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
   2161          /* Max pitch is 100 samples (480 Hz) */
   2162          pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
   2163                poffset-100, &pitch_index);
   2164          pitch_index = poffset-pitch_index;
   2165          st->last_pitch_index = pitch_index;
   2166       } else {
   2167          pitch_index = st->last_pitch_index;
   2168          fade = QCONST16(.8f,15);
   2169       }
   2170 
   2171       ALLOC(etmp, overlap, opus_val32);
   2172       c=0; do {
   2173          opus_val16 exc[MAX_PERIOD];
   2174          opus_val32 ac[LPC_ORDER+1];
   2175          opus_val16 decay;
   2176          opus_val16 attenuation;
   2177          opus_val32 S1=0;
   2178          opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
   2179          opus_val32 *e = out_syn[c];
   2180 
   2181 
   2182          offset = MAX_PERIOD-pitch_index;
   2183          for (i=0;i<MAX_PERIOD;i++)
   2184             exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
   2185 
   2186          /* Compute LPC coefficients for the last MAX_PERIOD samples before the loss so we can
   2187             work in the excitation-filter domain */
   2188          if (st->loss_count == 0)
   2189          {
   2190             _celt_autocorr(exc, ac, mode->window, overlap,
   2191                   LPC_ORDER, MAX_PERIOD);
   2192 
   2193             /* Noise floor -40 dB */
   2194 #ifdef FIXED_POINT
   2195             ac[0] += SHR32(ac[0],13);
   2196 #else
   2197             ac[0] *= 1.0001f;
   2198 #endif
   2199             /* Lag windowing */
   2200             for (i=1;i<=LPC_ORDER;i++)
   2201             {
   2202                /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
   2203 #ifdef FIXED_POINT
   2204                ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
   2205 #else
   2206                ac[i] -= ac[i]*(.008f*i)*(.008f*i);
   2207 #endif
   2208             }
   2209 
   2210             _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
   2211          }
   2212          /* Samples just before the beginning of exc  */
   2213          for (i=0;i<LPC_ORDER;i++)
   2214             mem[i] = ROUND16(out_mem[c][-1-i], SIG_SHIFT);
   2215          /* Compute the excitation for MAX_PERIOD samples before the loss */
   2216          celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
   2217 
   2218          /* Check if the waveform is decaying (and if so how fast)
   2219             We do this to avoid adding energy when concealing in a segment
   2220             with decaying energy */
   2221          {
   2222             opus_val32 E1=1, E2=1;
   2223             int period;
   2224             if (pitch_index <= MAX_PERIOD/2)
   2225                period = pitch_index;
   2226             else
   2227                period = MAX_PERIOD/2;
   2228             for (i=0;i<period;i++)
   2229             {
   2230                E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
   2231                E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
   2232             }
   2233             if (E1 > E2)
   2234                E1 = E2;
   2235             decay = celt_sqrt(frac_div32(SHR32(E1,1),E2));
   2236             attenuation = decay;
   2237          }
   2238 
   2239          /* Move memory one frame to the left */
   2240          OPUS_MOVE(decode_mem[c], decode_mem[c]+N, DECODE_BUFFER_SIZE-N+overlap);
   2241 
   2242          /* Extrapolate excitation with the right period, taking decay into account */
   2243          for (i=0;i<len;i++)
   2244          {
   2245             opus_val16 tmp;
   2246             if (offset+i >= MAX_PERIOD)
   2247             {
   2248                offset -= pitch_index;
   2249                attenuation = MULT16_16_Q15(attenuation, decay);
   2250             }
   2251             e[i] = SHL32(EXTEND32(MULT16_16_Q15(attenuation, exc[offset+i])), SIG_SHIFT);
   2252             /* Compute the energy of the previously decoded signal whose
   2253                excitation we're copying */
   2254             tmp = ROUND16(out_mem[c][-N+offset+i],SIG_SHIFT);
   2255             S1 += SHR32(MULT16_16(tmp,tmp),8);
   2256          }
   2257 
   2258          /* Copy the last decoded samples (prior to the overlap region) to
   2259             synthesis filter memory so we can have a continuous signal. */
   2260          for (i=0;i<LPC_ORDER;i++)
   2261             mem[i] = ROUND16(out_mem[c][MAX_PERIOD-N-1-i], SIG_SHIFT);
   2262          /* Apply the fading if not the first loss */
   2263          for (i=0;i<len;i++)
   2264             e[i] = MULT16_32_Q15(fade, e[i]);
   2265          /* Synthesis filter -- back in the signal domain */
   2266          celt_iir(e, lpc+c*LPC_ORDER, e, len, LPC_ORDER, mem);
   2267 
   2268          /* Check if the synthesis energy is higher than expected, which can
   2269             happen with the signal changes during our window. If so, attenuate. */
   2270          {
   2271             opus_val32 S2=0;
   2272             for (i=0;i<len;i++)
   2273             {
   2274                opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
   2275                S2 += SHR32(MULT16_16(tmp,tmp),8);
   2276             }
   2277             /* This checks for an "explosion" in the synthesis */
   2278 #ifdef FIXED_POINT
   2279             if (!(S1 > SHR32(S2,2)))
   2280 #else
   2281             /* Float test is written this way to catch NaNs at the same time */
   2282             if (!(S1 > 0.2f*S2))
   2283 #endif
   2284             {
   2285                for (i=0;i<len;i++)
   2286                   e[i] = 0;
   2287             } else if (S1 < S2)
   2288             {
   2289                opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
   2290                for (i=0;i<overlap;i++)
   2291                {
   2292                   opus_val16 tmp_g = Q15ONE - MULT16_16_Q15(mode->window[i], Q15ONE-ratio);
   2293                   e[i] = MULT16_32_Q15(tmp_g, e[i]);
   2294                }
   2295                for (i=overlap;i<len;i++)
   2296                   e[i] = MULT16_32_Q15(ratio, e[i]);
   2297             }
   2298          }
   2299 
   2300          /* Apply pre-filter to the MDCT overlap for the next frame because the
   2301             post-filter will be re-applied in the decoder after the MDCT overlap */
   2302          comb_filter(etmp, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
   2303                -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
   2304                NULL, 0);
   2305 
   2306          /* Simulate TDAC on the concealed audio so that it blends with the
   2307             MDCT of next frames. */
   2308          for (i=0;i<overlap/2;i++)
   2309          {
   2310             opus_val32 tmp;
   2311             tmp = MULT16_32_Q15(mode->window[i],           etmp[overlap-1-i]) +
   2312                   MULT16_32_Q15(mode->window[overlap-i-1], etmp[i          ]);
   2313             out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(mode->window[overlap-i-1], tmp);
   2314             out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(mode->window[i], tmp);
   2315          }
   2316       } while (++c<C);
   2317    }
   2318 
   2319    deemphasis(out_syn, pcm, N, C, st->downsample, mode->preemph, st->preemph_memD);
   2320 
   2321    st->loss_count++;
   2322 
   2323    RESTORE_STACK;
   2324 }
   2325 
   2326 int celt_decode_with_ec(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec)
   2327 {
   2328    int c, i, N;
   2329    int spread_decision;
   2330    opus_int32 bits;
   2331    ec_dec _dec;
   2332    VARDECL(celt_sig, freq);
   2333    VARDECL(celt_norm, X);
   2334    VARDECL(celt_ener, bandE);
   2335    VARDECL(int, fine_quant);
   2336    VARDECL(int, pulses);
   2337    VARDECL(int, cap);
   2338    VARDECL(int, offsets);
   2339    VARDECL(int, fine_priority);
   2340    VARDECL(int, tf_res);
   2341    VARDECL(unsigned char, collapse_masks);
   2342    celt_sig *out_mem[2];
   2343    celt_sig *decode_mem[2];
   2344    celt_sig *overlap_mem[2];
   2345    celt_sig *out_syn[2];
   2346    opus_val16 *lpc;
   2347    opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
   2348 
   2349    int shortBlocks;
   2350    int isTransient;
   2351    int intra_ener;
   2352    const int CC = st->channels;
   2353    int LM, M;
   2354    int effEnd;
   2355    int codedBands;
   2356    int alloc_trim;
   2357    int postfilter_pitch;
   2358    opus_val16 postfilter_gain;
   2359    int intensity=0;
   2360    int dual_stereo=0;
   2361    opus_int32 total_bits;
   2362    opus_int32 balance;
   2363    opus_int32 tell;
   2364    int dynalloc_logp;
   2365    int postfilter_tapset;
   2366    int anti_collapse_rsv;
   2367    int anti_collapse_on=0;
   2368    int silence;
   2369    int C = st->stream_channels;
   2370    ALLOC_STACK;
   2371 
   2372    frame_size *= st->downsample;
   2373 
   2374    c=0; do {
   2375       decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
   2376       out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
   2377       overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
   2378    } while (++c<CC);
   2379    lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
   2380    oldBandE = lpc+CC*LPC_ORDER;
   2381    oldLogE = oldBandE + 2*st->mode->nbEBands;
   2382    oldLogE2 = oldLogE + 2*st->mode->nbEBands;
   2383    backgroundLogE = oldLogE2  + 2*st->mode->nbEBands;
   2384 
   2385 #ifdef CUSTOM_MODES
   2386    if (st->signalling && data!=NULL)
   2387    {
   2388       int data0=data[0];
   2389       /* Convert "standard mode" to Opus header */
   2390       if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
   2391       {
   2392          data0 = fromOpus(data0);
   2393          if (data0<0)
   2394             return OPUS_INVALID_PACKET;
   2395       }
   2396       st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
   2397       LM = (data0>>3)&0x3;
   2398       C = 1 + ((data0>>2)&0x1);
   2399       data++;
   2400       len--;
   2401       if (LM>st->mode->maxLM)
   2402          return OPUS_INVALID_PACKET;
   2403       if (frame_size < st->mode->shortMdctSize<<LM)
   2404          return OPUS_BUFFER_TOO_SMALL;
   2405       else
   2406          frame_size = st->mode->shortMdctSize<<LM;
   2407    } else {
   2408 #else
   2409    {
   2410 #endif
   2411       for (LM=0;LM<=st->mode->maxLM;LM++)
   2412          if (st->mode->shortMdctSize<<LM==frame_size)
   2413             break;
   2414       if (LM>st->mode->maxLM)
   2415          return OPUS_BAD_ARG;
   2416    }
   2417    M=1<<LM;
   2418 
   2419    if (len<0 || len>1275 || pcm==NULL)
   2420       return OPUS_BAD_ARG;
   2421 
   2422    N = M*st->mode->shortMdctSize;
   2423 
   2424    effEnd = st->end;
   2425    if (effEnd > st->mode->effEBands)
   2426       effEnd = st->mode->effEBands;
   2427 
   2428    if (data == NULL || len<=1)
   2429    {
   2430       celt_decode_lost(st, pcm, N, LM);
   2431       RESTORE_STACK;
   2432       return frame_size/st->downsample;
   2433    }
   2434 
   2435    ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
   2436    ALLOC(X, C*N, celt_norm);   /**< Interleaved normalised MDCTs */
   2437    ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
   2438    c=0; do
   2439       for (i=0;i<M*st->mode->eBands[st->start];i++)
   2440          X[c*N+i] = 0;
   2441    while (++c<C);
   2442    c=0; do
   2443       for (i=M*st->mode->eBands[effEnd];i<N;i++)
   2444          X[c*N+i] = 0;
   2445    while (++c<C);
   2446 
   2447    if (dec == NULL)
   2448    {
   2449       ec_dec_init(&_dec,(unsigned char*)data,len);
   2450       dec = &_dec;
   2451    }
   2452 
   2453    if (C==1)
   2454    {
   2455       for (i=0;i<st->mode->nbEBands;i++)
   2456          oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
   2457    }
   2458 
   2459    total_bits = len*8;
   2460    tell = ec_tell(dec);
   2461 
   2462    if (tell >= total_bits)
   2463       silence = 1;
   2464    else if (tell==1)
   2465       silence = ec_dec_bit_logp(dec, 15);
   2466    else
   2467       silence = 0;
   2468    if (silence)
   2469    {
   2470       /* Pretend we've read all the remaining bits */
   2471       tell = len*8;
   2472       dec->nbits_total+=tell-ec_tell(dec);
   2473    }
   2474 
   2475    postfilter_gain = 0;
   2476    postfilter_pitch = 0;
   2477    postfilter_tapset = 0;
   2478    if (st->start==0 && tell+16 <= total_bits)
   2479    {
   2480       if(ec_dec_bit_logp(dec, 1))
   2481       {
   2482          int qg, octave;
   2483          octave = ec_dec_uint(dec, 6);
   2484          postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
   2485          qg = ec_dec_bits(dec, 3);
   2486          if (ec_tell(dec)+2<=total_bits)
   2487             postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
   2488          postfilter_gain = QCONST16(.09375f,15)*(qg+1);
   2489       }
   2490       tell = ec_tell(dec);
   2491    }
   2492 
   2493    if (LM > 0 && tell+3 <= total_bits)
   2494    {
   2495       isTransient = ec_dec_bit_logp(dec, 3);
   2496       tell = ec_tell(dec);
   2497    }
   2498    else
   2499       isTransient = 0;
   2500 
   2501    if (isTransient)
   2502       shortBlocks = M;
   2503    else
   2504       shortBlocks = 0;
   2505 
   2506    /* Decode the global flags (first symbols in the stream) */
   2507    intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
   2508    /* Get band energies */
   2509    unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
   2510          intra_ener, dec, C, LM);
   2511 
   2512    ALLOC(tf_res, st->mode->nbEBands, int);
   2513    tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
   2514 
   2515    tell = ec_tell(dec);
   2516    spread_decision = SPREAD_NORMAL;
   2517    if (tell+4 <= total_bits)
   2518       spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
   2519 
   2520    ALLOC(pulses, st->mode->nbEBands, int);
   2521    ALLOC(cap, st->mode->nbEBands, int);
   2522    ALLOC(offsets, st->mode->nbEBands, int);
   2523    ALLOC(fine_priority, st->mode->nbEBands, int);
   2524 
   2525    init_caps(st->mode,cap,LM,C);
   2526 
   2527    dynalloc_logp = 6;
   2528    total_bits<<=BITRES;
   2529    tell = ec_tell_frac(dec);
   2530    for (i=st->start;i<st->end;i++)
   2531    {
   2532       int width, quanta;
   2533       int dynalloc_loop_logp;
   2534       int boost;
   2535       width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
   2536       /* quanta is 6 bits, but no more than 1 bit/sample
   2537          and no less than 1/8 bit/sample */
   2538       quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
   2539       dynalloc_loop_logp = dynalloc_logp;
   2540       boost = 0;
   2541       while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
   2542       {
   2543          int flag;
   2544          flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
   2545          tell = ec_tell_frac(dec);
   2546          if (!flag)
   2547             break;
   2548          boost += quanta;
   2549          total_bits -= quanta;
   2550          dynalloc_loop_logp = 1;
   2551       }
   2552       offsets[i] = boost;
   2553       /* Making dynalloc more likely */
   2554       if (boost>0)
   2555          dynalloc_logp = IMAX(2, dynalloc_logp-1);
   2556    }
   2557 
   2558    ALLOC(fine_quant, st->mode->nbEBands, int);
   2559    alloc_trim = tell+(6<<BITRES) <= total_bits ?
   2560          ec_dec_icdf(dec, trim_icdf, 7) : 5;
   2561 
   2562    bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
   2563    anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
   2564    bits -= anti_collapse_rsv;
   2565    codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
   2566          alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
   2567          fine_quant, fine_priority, C, LM, dec, 0, 0);
   2568 
   2569    unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
   2570 
   2571    /* Decode fixed codebook */
   2572    ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
   2573    quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
   2574          NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
   2575          len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
   2576 
   2577    if (anti_collapse_rsv > 0)
   2578    {
   2579       anti_collapse_on = ec_dec_bits(dec, 1);
   2580    }
   2581 
   2582    unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
   2583          fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
   2584 
   2585    if (anti_collapse_on)
   2586       anti_collapse(st->mode, X, collapse_masks, LM, C, N,
   2587             st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
   2588 
   2589    log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
   2590 
   2591    if (silence)
   2592    {
   2593       for (i=0;i<C*st->mode->nbEBands;i++)
   2594       {
   2595          bandE[i] = 0;
   2596          oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
   2597       }
   2598    }
   2599    /* Synthesis */
   2600    denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
   2601 
   2602    OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
   2603    if (CC==2)
   2604       OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
   2605 
   2606    c=0; do
   2607       for (i=0;i<M*st->mode->eBands[st->start];i++)
   2608          freq[c*N+i] = 0;
   2609    while (++c<C);
   2610    c=0; do {
   2611       int bound = M*st->mode->eBands[effEnd];
   2612       if (st->downsample!=1)
   2613          bound = IMIN(bound, N/st->downsample);
   2614       for (i=bound;i<N;i++)
   2615          freq[c*N+i] = 0;
   2616    } while (++c<C);
   2617 
   2618    out_syn[0] = out_mem[0]+MAX_PERIOD-N;
   2619    if (CC==2)
   2620       out_syn[1] = out_mem[1]+MAX_PERIOD-N;
   2621 
   2622    if (CC==2&&C==1)
   2623    {
   2624       for (i=0;i<N;i++)
   2625          freq[N+i] = freq[i];
   2626    }
   2627    if (CC==1&&C==2)
   2628    {
   2629       for (i=0;i<N;i++)
   2630          freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
   2631    }
   2632 
   2633    /* Compute inverse MDCTs */
   2634    compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
   2635 
   2636    c=0; do {
   2637       st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
   2638       st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
   2639       comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
   2640             st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
   2641             st->mode->window, st->overlap);
   2642       if (LM!=0)
   2643          comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
   2644                st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
   2645                st->mode->window, st->mode->overlap);
   2646 
   2647    } while (++c<CC);
   2648    st->postfilter_period_old = st->postfilter_period;
   2649    st->postfilter_gain_old = st->postfilter_gain;
   2650    st->postfilter_tapset_old = st->postfilter_tapset;
   2651    st->postfilter_period = postfilter_pitch;
   2652    st->postfilter_gain = postfilter_gain;
   2653    st->postfilter_tapset = postfilter_tapset;
   2654    if (LM!=0)
   2655    {
   2656       st->postfilter_period_old = st->postfilter_period;
   2657       st->postfilter_gain_old = st->postfilter_gain;
   2658       st->postfilter_tapset_old = st->postfilter_tapset;
   2659    }
   2660 
   2661    if (C==1) {
   2662       for (i=0;i<st->mode->nbEBands;i++)
   2663          oldBandE[st->mode->nbEBands+i]=oldBandE[i];
   2664    }
   2665 
   2666    /* In case start or end were to change */
   2667    if (!isTransient)
   2668    {
   2669       for (i=0;i<2*st->mode->nbEBands;i++)
   2670          oldLogE2[i] = oldLogE[i];
   2671       for (i=0;i<2*st->mode->nbEBands;i++)
   2672          oldLogE[i] = oldBandE[i];
   2673       for (i=0;i<2*st->mode->nbEBands;i++)
   2674          backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
   2675    } else {
   2676       for (i=0;i<2*st->mode->nbEBands;i++)
   2677          oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
   2678    }
   2679    c=0; do
   2680    {
   2681       for (i=0;i<st->start;i++)
   2682       {
   2683          oldBandE[c*st->mode->nbEBands+i]=0;
   2684          oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
   2685       }
   2686       for (i=st->end;i<st->mode->nbEBands;i++)
   2687       {
   2688          oldBandE[c*st->mode->nbEBands+i]=0;
   2689          oldLogE[c*st->mode->nbEBands+i]=oldLogE2[c*st->mode->nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
   2690       }
   2691    } while (++c<2);
   2692    st->rng = dec->rng;
   2693 
   2694    deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
   2695    st->loss_count = 0;
   2696    RESTORE_STACK;
   2697    if (ec_tell(dec) > 8*len)
   2698       return OPUS_INTERNAL_ERROR;
   2699    if(ec_get_error(dec))
   2700       st->error = 1;
   2701    return frame_size/st->downsample;
   2702 }
   2703 
   2704 
   2705 #ifdef CUSTOM_MODES
   2706 
   2707 #ifdef FIXED_POINT
   2708 int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
   2709 {
   2710    return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
   2711 }
   2712 
   2713 #ifndef DISABLE_FLOAT_API
   2714 int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
   2715 {
   2716    int j, ret, C, N;
   2717    VARDECL(opus_int16, out);
   2718    ALLOC_STACK;
   2719 
   2720    if (pcm==NULL)
   2721       return OPUS_BAD_ARG;
   2722 
   2723    C = st->channels;
   2724    N = frame_size;
   2725 
   2726    ALLOC(out, C*N, opus_int16);
   2727    ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
   2728    if (ret>0)
   2729       for (j=0;j<C*ret;j++)
   2730          pcm[j]=out[j]*(1.f/32768.f);
   2731 
   2732    RESTORE_STACK;
   2733    return ret;
   2734 }
   2735 #endif /* DISABLE_FLOAT_API */
   2736 
   2737 #else
   2738 
   2739 int opus_custom_decode_float(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, float * OPUS_RESTRICT pcm, int frame_size)
   2740 {
   2741    return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
   2742 }
   2743 
   2744 int opus_custom_decode(CELTDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_int16 * OPUS_RESTRICT pcm, int frame_size)
   2745 {
   2746    int j, ret, C, N;
   2747    VARDECL(celt_sig, out);
   2748    ALLOC_STACK;
   2749 
   2750    if (pcm==NULL)
   2751       return OPUS_BAD_ARG;
   2752 
   2753    C = st->channels;
   2754    N = frame_size;
   2755    ALLOC(out, C*N, celt_sig);
   2756 
   2757    ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
   2758 
   2759    if (ret>0)
   2760       for (j=0;j<C*ret;j++)
   2761          pcm[j] = FLOAT2INT16 (out[j]);
   2762 
   2763    RESTORE_STACK;
   2764    return ret;
   2765 }
   2766 
   2767 #endif
   2768 #endif /* CUSTOM_MODES */
   2769 
   2770 int opus_custom_decoder_ctl(CELTDecoder * OPUS_RESTRICT st, int request, ...)
   2771 {
   2772    va_list ap;
   2773 
   2774    va_start(ap, request);
   2775    switch (request)
   2776    {
   2777       case CELT_SET_START_BAND_REQUEST:
   2778       {
   2779          opus_int32 value = va_arg(ap, opus_int32);
   2780          if (value<0 || value>=st->mode->nbEBands)
   2781             goto bad_arg;
   2782          st->start = value;
   2783       }
   2784       break;
   2785       case CELT_SET_END_BAND_REQUEST:
   2786       {
   2787          opus_int32 value = va_arg(ap, opus_int32);
   2788          if (value<1 || value>st->mode->nbEBands)
   2789             goto bad_arg;
   2790          st->end = value;
   2791       }
   2792       break;
   2793       case CELT_SET_CHANNELS_REQUEST:
   2794       {
   2795          opus_int32 value = va_arg(ap, opus_int32);
   2796          if (value<1 || value>2)
   2797             goto bad_arg;
   2798          st->stream_channels = value;
   2799       }
   2800       break;
   2801       case CELT_GET_AND_CLEAR_ERROR_REQUEST:
   2802       {
   2803          opus_int32 *value = va_arg(ap, opus_int32*);
   2804          if (value==NULL)
   2805             goto bad_arg;
   2806          *value=st->error;
   2807          st->error = 0;
   2808       }
   2809       break;
   2810       case OPUS_GET_LOOKAHEAD_REQUEST:
   2811       {
   2812          opus_int32 *value = va_arg(ap, opus_int32*);
   2813          if (value==NULL)
   2814             goto bad_arg;
   2815          *value = st->overlap/st->downsample;
   2816       }
   2817       break;
   2818       case OPUS_RESET_STATE:
   2819       {
   2820          int i;
   2821          opus_val16 *lpc, *oldBandE, *oldLogE, *oldLogE2;
   2822          lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*st->channels);
   2823          oldBandE = lpc+st->channels*LPC_ORDER;
   2824          oldLogE = oldBandE + 2*st->mode->nbEBands;
   2825          oldLogE2 = oldLogE + 2*st->mode->nbEBands;
   2826          OPUS_CLEAR((char*)&st->DECODER_RESET_START,
   2827                opus_custom_decoder_get_size(st->mode, st->channels)-
   2828                ((char*)&st->DECODER_RESET_START - (char*)st));
   2829          for (i=0;i<2*st->mode->nbEBands;i++)
   2830             oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
   2831       }
   2832       break;
   2833       case OPUS_GET_PITCH_REQUEST:
   2834       {
   2835          opus_int32 *value = va_arg(ap, opus_int32*);
   2836          if (value==NULL)
   2837             goto bad_arg;
   2838          *value = st->postfilter_period;
   2839       }
   2840       break;
   2841       case CELT_GET_MODE_REQUEST:
   2842       {
   2843          const CELTMode ** value = va_arg(ap, const CELTMode**);
   2844          if (value==0)
   2845             goto bad_arg;
   2846          *value=st->mode;
   2847       }
   2848       break;
   2849       case CELT_SET_SIGNALLING_REQUEST:
   2850       {
   2851          opus_int32 value = va_arg(ap, opus_int32);
   2852          st->signalling = value;
   2853       }
   2854       break;
   2855       case OPUS_GET_FINAL_RANGE_REQUEST:
   2856       {
   2857          opus_uint32 * value = va_arg(ap, opus_uint32 *);
   2858          if (value==0)
   2859             goto bad_arg;
   2860          *value=st->rng;
   2861       }
   2862       break;
   2863       default:
   2864          goto bad_request;
   2865    }
   2866    va_end(ap);
   2867    return OPUS_OK;
   2868 bad_arg:
   2869    va_end(ap);
   2870    return OPUS_BAD_ARG;
   2871 bad_request:
   2872       va_end(ap);
   2873   return OPUS_UNIMPLEMENTED;
   2874 }
   2875 
   2876 
   2877 
   2878 const char *opus_strerror(int error)
   2879 {
   2880    static const char * const error_strings[8] = {
   2881       "success",
   2882       "invalid argument",
   2883       "buffer too small",
   2884       "internal error",
   2885       "corrupted stream",
   2886       "request not implemented",
   2887       "invalid state",
   2888       "memory allocation failed"
   2889    };
   2890    if (error > 0 || error < -7)
   2891       return "unknown error";
   2892    else
   2893       return error_strings[-error];
   2894 }
   2895 
   2896 const char *opus_get_version_string(void)
   2897 {
   2898     return "libopus " OPUS_VERSION
   2899 #ifdef FIXED_POINT
   2900           "-fixed"
   2901 #endif
   2902 #ifdef FUZZING
   2903           "-fuzzing"
   2904 #endif
   2905           ;
   2906 }
   2907