LCOV - code coverage report
Current view: top level - media/libopus/celt - celt_decoder.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 508 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 12 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13