LCOV - code coverage report
Current view: top level - lib_dec - acelp_core_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 460 486 94.7 %
Date: 2025-07-09 08:36:12 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include <assert.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "ivas_cnst.h"
      47             : #include "ivas_prot.h"
      48             : #include "ivas_rom_com.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*-------------------------------------------------------------------*
      52             :  * acelp_core_dec()
      53             :  *
      54             :  * ACELP core decoder
      55             :  *-------------------------------------------------------------------*/
      56             : 
      57      549852 : ivas_error acelp_core_dec(
      58             :     Decoder_State *st,                   /* i/o: decoder state structure                                                 */
      59             :     float output[],                      /* o  : synthesis @internal Fs                                                  */
      60             :     float synth[],                       /* o  : synthesis                                                               */
      61             :     float save_hb_synth[],               /* o  : HB synthesis                                                            */
      62             :     float bwe_exc_extended[],            /* i/o: bandwidth extended excitation                                           */
      63             :     float *voice_factors,                /* o  : voicing factors                                                         */
      64             :     float old_syn_12k8_16k[],            /* o  : intermediate ACELP synthesis at 12.8kHz or 16kHz to be used by SWB BWE  */
      65             :     const int16_t sharpFlag,             /* i  : formant sharpening flag                                                 */
      66             :     float pitch_buf[NB_SUBFR16k],        /* o  : floating pitch for each subframe                                        */
      67             :     int16_t *unbits,                     /* o  : number of unused bits                                                   */
      68             :     int16_t *sid_bw,                     /* o  : 0-NB/WB, 1-SWB SID                                                      */
      69             :     STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i/o: TD stereo decoder handle                                                */
      70             :     const float tdm_lsfQ_PCh[M],         /* i  : Q LSFs for primary channel                                              */
      71             :     const int16_t use_cldfb_for_dft,     /* i  : flag to use of CLDFB for DFT Stereo                                     */
      72             :     const int16_t last_element_mode,     /* i  : last element mode                                                       */
      73             :     const int32_t last_element_brate,    /* i  : last element bitrate                                                    */
      74             :     const int16_t flag_sec_CNA,          /* i  : CNA flag for secondary channel                                          */
      75             :     const int16_t nchan_out,             /* i  : number of output channels                                               */
      76             :     STEREO_CNG_DEC_HANDLE hStereoCng,    /* i  : stereo CNG handle                                                       */
      77             :     const int16_t read_sid_info          /* i  : read SID info flag                                                      */
      78             : )
      79             : {
      80             :     float old_exc[L_EXC_DEC], *exc;                                              /* excitation signal buffer              */
      81             :     float syn_tmp[L_FRAME16k + L_SUBFR], *syn;                                   /* synthesis signal buffer               */
      82             :     int16_t output_frame;                                                        /* frame length at output sampling freq. */
      83             :     float lsf_new[M];                                                            /* LSFs at the end of the frame          */
      84             :     float lsp_new[M];                                                            /* LSPs at the end of the frame          */
      85             :     float lsp_mid[M];                                                            /* LSPs in the middle of the frame       */
      86             :     float Aq[NB_SUBFR16k * ( M + 1 )];                                           /* A(q) quantized for the 4 subframes    */
      87             :     float old_exc2[L_FRAME16k + L_EXC_MEM], *exc2;                               /* total excitation buffer               */
      88             :     float mem_tmp[M];                                                            /* temporary synthesis filter memory     */
      89             :     float enr_q;                                                                 /* E information for FER protection      */
      90             :     float tmp_noise;                                                             /* Long term temporary noise energy      */
      91             :     float Es_pred;                                                               /* predicted scaled innov. energy        */
      92             :     float FEC_pitch;                                                             /* FEC pitch                             */
      93             :     float old_bwe_exc[( ( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2 )]; /* excitation buffer                     */
      94             :     float *bwe_exc;                                                              /* Excitation for SWB TBE                */
      95             :     int16_t i, int_fs;
      96             :     int16_t tc_subfr;
      97             :     int16_t allow_cn_step;
      98             :     float temp_buf[L_FRAME16k + L_SYN_MEM];
      99             :     int16_t last_pulse_pos;
     100             :     int16_t T0_tmp;
     101             :     int16_t do_WI;
     102             :     float dct_buffer[DCT_L_POST];
     103             :     float exc_buffer[DCT_L_POST];
     104             :     float dct_exc_tmp[L_FRAME16k];
     105             :     float bpf_error_signal[L_FRAME16k];
     106             :     int16_t nb_bits; /* number of bits                       */
     107             :     int16_t indice;  /* parameter indices to write           */
     108             :     float gain_buf[NB_SUBFR16k];
     109             :     float q_env[20];
     110             :     float exc3[L_FRAME16k];
     111             :     float syn1_tmp[L_FRAME16k + 2], *syn1;
     112             :     float *realBuffer[CLDFB_NO_COL_MAX], *imagBuffer[CLDFB_NO_COL_MAX];
     113             :     float realBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
     114             :     float imagBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
     115             :     int16_t LSF_Q_prediction; /* LSF prediction mode                  */
     116             :     float tmpF;
     117             :     int16_t uc_two_stage_flag;
     118             :     int16_t tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag;
     119             :     float *old_exc_s; /* Start of last excitation frame       */
     120             :     float *p_tdm_Pri_pitch_buf;
     121             :     int16_t local_element_mode;
     122             :     ivas_error error;
     123             : 
     124      549852 :     error = IVAS_ERR_OK;
     125             : 
     126      549852 :     if ( st->element_mode == IVAS_CPE_MDCT && nchan_out == 1 && st->idchan == 1 && last_element_brate <= IVAS_SID_5k2 )
     127             :     {
     128             :         /* In MDCT-Stereo DTX with mono output, we can skip CNG for the second channel, except for the first inactive frame following an active period */
     129        3240 :         return error;
     130             :     }
     131             : 
     132      546612 :     push_wmops( "acelp_core_dec" );
     133             : 
     134      546612 :     output_frame = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
     135             : 
     136             :     /*----------------------------------------------------------------*
     137             :      * stereo SID and CNG frames processing
     138             :      *----------------------------------------------------------------*/
     139             : 
     140      546612 :     if ( st->core_brate <= SID_2k40 && st->element_mode == IVAS_CPE_DFT && nchan_out == 2 )
     141             :     {
     142       16305 :         if ( st->cng_type == FD_CNG )
     143             :         {
     144       12585 :             configureFdCngDec( st->hFdCngDec, st->bwidth, ACELP_14k25, st->L_frame, st->last_L_frame, st->element_mode );
     145             : 
     146             :             /* Only run parameter decoding in SID frames */
     147       12585 :             if ( st->core_brate == SID_2k40 )
     148             :             {
     149        1623 :                 FdCng_decodeSID( st );
     150             :             }
     151             : 
     152      314625 :             for ( i = 0; i < NPART; i++ )
     153             :             {
     154      302040 :                 st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] = STEREO_DFT_FD_FILT * st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] + ( 1 - STEREO_DFT_FD_FILT ) * st->hFdCngDec->hFdCngCom->sidNoiseEst[i];
     155             :             }
     156             : 
     157       12585 :             ApplyFdCng( NULL, NULL, NULL, NULL, st, 0, 0 );
     158             :         }
     159             :         else
     160             :         {
     161        3720 :             configureFdCngDec( st->hFdCngDec, st->bwidth, ACELP_14k25, st->L_frame, st->last_L_frame, st->element_mode );
     162             : 
     163             :             /* decode CNG parameters */
     164        3720 :             CNG_dec( st, last_element_mode, Aq, lsp_new, lsf_new, &allow_cn_step, sid_bw, q_env );
     165             : 
     166             :             /* comfort noise generation */
     167        3720 :             CNG_exc( st->core_brate, st->L_frame, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, NULL, NULL, &st->lp_ener, st->last_core_brate, &st->first_CNG, &( st->hTdCngDec->cng_ener_seed ), NULL, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, sid_bw, &st->hTdCngDec->cng_ener_seed1, NULL, st->Opt_AMR_WB, st->element_mode );
     168             : 
     169        3720 :             mvr2r( Aq, st->Aq_cng, M + 1 );
     170             : 
     171             :             /* update old LSP and LSF vector */
     172        3720 :             mvr2r( lsf_new, st->lsf_old, M );
     173        3720 :             mvr2r( lsp_new, st->lsp_old, M );
     174             :         }
     175             : 
     176       16305 :         set_f( output, 0, output_frame ); /* output and synth are not used in DFT domain CNG generation and the decoder output is unaffected if they are left uninitalized */
     177       16305 :         set_f( synth, 0, output_frame );  /* They are however read in a few places which causes errors in the valgrind tests. Simplest solution from a code perspective was to set them to zero. */
     178             : 
     179             :         /* CN generation done in DFT domain */
     180       16305 :         pop_wmops();
     181       16305 :         return error;
     182             :     }
     183             : 
     184             :     /*----------------------------------------------------------------*
     185             :      * Active frames processing
     186             :      *----------------------------------------------------------------*/
     187             : 
     188             :     /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
     189     9015219 :     for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
     190             :     {
     191     8484912 :         set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
     192     8484912 :         set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
     193     8484912 :         realBuffer[i] = realBufferTmp[i];
     194     8484912 :         imagBuffer[i] = imagBufferTmp[i];
     195             :     }
     196             : 
     197             :     /*----------------------------------------------------------------*
     198             :      * Initialization
     199             :      *----------------------------------------------------------------*/
     200             : 
     201      530307 :     LSF_Q_prediction = -1;
     202      530307 :     set_f( syn_tmp, 0, L_SUBFR );
     203      530307 :     syn = syn_tmp + L_SUBFR;
     204      530307 :     syn1_tmp[0] = 0;
     205      530307 :     syn1_tmp[1] = 0;
     206      530307 :     syn1 = syn1_tmp + 2;
     207      530307 :     st->bpf_off = 0;
     208             : 
     209      530307 :     if ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE || ( st->element_mode == IVAS_CPE_DFT && st->last_core_brate <= SID_2k40 ) || ( st->element_mode == IVAS_CPE_MDCT && st->last_core_brate <= SID_2k40 ) )
     210             :     {
     211             :         /* in case of HQ->ACELP switching, do not apply BPF */
     212       58890 :         st->bpf_off = 1;
     213             : 
     214       58890 :         if ( st->hPFstat != NULL )
     215             :         {
     216             :             /* in case of core switching, reset post-filter memories */
     217       37152 :             st->hPFstat->on = 0;
     218             :         }
     219             : 
     220       58890 :         if ( st->hGSCDec != NULL )
     221             :         {
     222             :             /* reset the GSC pre echo energy threshold in case of switching */
     223       37152 :             st->hGSCDec->Last_frame_ener = (float) MAX_32;
     224             :         }
     225             :     }
     226             : 
     227      530307 :     if ( st->hGSCDec != NULL && st->prev_bfi > 0 )
     228             :     {
     229             :         /* reset the GSC pre echo energy threshold in case of FEC */
     230       12186 :         st->hGSCDec->Last_frame_ener = (float) MAX_32;
     231             :     }
     232             : 
     233      530307 :     if ( st->hFdCngDec != NULL && ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && ( st->last_core_brate == SID_2k40 || st->last_core_brate == FRAME_NO_DATA ) )
     234             :     {
     235       14871 :         set_zero( st->hFdCngDec->hFdCngCom->olapBufferSynth2, FFTLEN );
     236       14871 :         set_zero( hStereoCng->olapBufferSynth22, FFTLEN );
     237             :     }
     238             : 
     239      530307 :     st->clas_dec = st->last_good;
     240      530307 :     enr_q = 0.0f;
     241      530307 :     Es_pred = 0.0f;
     242      530307 :     tmp_noise = 0.0f;
     243             : 
     244      530307 :     mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
     245      530307 :     exc = old_exc + L_EXC_MEM_DEC;
     246      530307 :     if ( st->hWIDec != NULL )
     247             :     {
     248           0 :         mvr2r( st->hWIDec->old_exc2, old_exc2, L_EXC_MEM );
     249             :     }
     250             :     else
     251             :     {
     252      530307 :         set_f( old_exc2, 0, L_EXC_MEM );
     253             :     }
     254      530307 :     exc2 = old_exc2 + L_EXC_MEM;
     255      530307 :     if ( st->hBWE_TD != NULL )
     256             :     {
     257      508287 :         mvr2r( st->hBWE_TD->old_bwe_exc, old_bwe_exc, PIT16k_MAX * 2 );
     258      508287 :         bwe_exc = old_bwe_exc + PIT16k_MAX * 2;
     259             :     }
     260             :     else
     261             :     {
     262       22020 :         bwe_exc = NULL;
     263             :     }
     264             : 
     265      530307 :     last_pulse_pos = 0;
     266      530307 :     do_WI = 0;
     267      530307 :     st->GSC_noisy_speech = 0;
     268      530307 :     st->relax_prev_lsf_interp = 0;
     269      530307 :     set_zero( gain_buf, NB_SUBFR16k );
     270             : 
     271      530307 :     if ( st->L_frame == L_FRAME )
     272             :     {
     273      271005 :         st->gamma = GAMMA1;
     274      271005 :         st->preemph_fac = PREEMPH_FAC;
     275      271005 :         int_fs = INT_FS_12k8;
     276             :     }
     277             :     else
     278             :     {
     279      259302 :         st->gamma = GAMMA16k;
     280      259302 :         st->preemph_fac = PREEMPH_FAC_16k;
     281      259302 :         int_fs = INT_FS_16k;
     282             :     }
     283             : 
     284             :     /* reset post-filter in case of switching */
     285      530307 :     if ( st->hPFstat != NULL && st->hPFstat->on == 0 )
     286             :     {
     287       77535 :         st->hPFstat->reset = 1;
     288             :     }
     289             : 
     290             :     /* TD stereo parameters */
     291      530307 :     if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
     292             :     {
     293       11340 :         tdm_lp_reuse_flag = hStereoTD->tdm_lp_reuse_flag;
     294       11340 :         tdm_low_rate_mode = hStereoTD->tdm_low_rate_mode;
     295       11340 :         tdm_Pitch_reuse_flag = hStereoTD->tdm_Pitch_reuse_flag;
     296       11340 :         p_tdm_Pri_pitch_buf = hStereoTD->tdm_Pri_pitch_buf;
     297             :     }
     298             :     else
     299             :     {
     300      518967 :         tdm_lp_reuse_flag = 0;
     301      518967 :         tdm_low_rate_mode = 0;
     302      518967 :         if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     303             :         {
     304       23772 :             tdm_low_rate_mode = 1;
     305             :         }
     306      518967 :         tdm_Pitch_reuse_flag = 0;
     307      518967 :         p_tdm_Pri_pitch_buf = NULL;
     308             :     }
     309             : 
     310             :     /*----------------------------------------------------------------*
     311             :      * Updates in case of internal sampling rate switching
     312             :      *----------------------------------------------------------------*/
     313             : 
     314      530307 :     if ( st->last_L_frame != st->L_frame && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
     315             :     {
     316             :         int16_t dec;
     317             : 
     318        2208 :         if ( st->hPFstat->on != 0 )
     319             :         {
     320             :             int16_t mem_syn_r_size_old, mem_syn_r_size_new;
     321             : 
     322        1998 :             mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
     323        1998 :             mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
     324        1998 :             lerp( st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
     325        1998 :             lerp( st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
     326             :         }
     327             : 
     328             :         /* convert quantized LSP vector */
     329        2208 :         st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
     330             : 
     331             :         /* convert old quantized LSF vector */
     332        2208 :         lsp2lsf( st->lsp_old, st->lsf_old, M, int_fs );
     333             : 
     334             :         /* FEC - update adaptive LSF mean vector */
     335        2208 :         mvr2r( st->lsf_old, st->lsfoldbfi1, M );
     336        2208 :         mvr2r( st->lsf_old, st->lsfoldbfi0, M );
     337        2208 :         mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
     338             : 
     339             :         /* Reset LPC mem */
     340        2208 :         if ( st->sr_core == INT_FS_16k )
     341             :         {
     342        1341 :             mvr2r( GEWB2_Ave, st->mem_AR, M );
     343             :         }
     344             :         else
     345             :         {
     346         867 :             mvr2r( GEWB_Ave, st->mem_AR, M );
     347             :         }
     348        2208 :         set_zero( st->mem_MA, M );
     349             : 
     350             :         /* update synthesis filter memories */
     351        2208 :         dec = DEC;
     352        2208 :         if ( st->element_mode != EVS_MONO )
     353             :         {
     354        2208 :             dec = DEC_IVAS;
     355             :         }
     356        2208 :         synth_mem_updt2( st->L_frame, st->last_L_frame, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, dec );
     357        2208 :         mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
     358        2208 :         mvr2r( st->mem_syn2, st->mem_syn1, M );
     359        2208 :         mvr2r( st->mem_syn2, st->mem_syn3, M );
     360             :     }
     361             : 
     362             :     /* update buffer of old subframe pitch values */
     363      530307 :     if ( st->last_L_frame != st->L_frame )
     364             :     {
     365        6120 :         if ( st->L_frame == L_FRAME )
     366             :         {
     367        3720 :             if ( st->last_L_frame == L_FRAME32k )
     368             :             {
     369         936 :                 tmpF = (float) 12800 / (float) 32000;
     370             :             }
     371        2784 :             else if ( st->last_L_frame == 512 )
     372             :             {
     373        1002 :                 tmpF = (float) 12800 / (float) 25600;
     374             :             }
     375             :             else /* st->last_L_frame == L_FRAME16k */
     376             :             {
     377        1782 :                 tmpF = (float) 12800 / (float) 16000;
     378             :             }
     379             : 
     380       18600 :             for ( i = NB_SUBFR16k - NB_SUBFR; i < NB_SUBFR16k; i++ )
     381             :             {
     382       14880 :                 st->old_pitch_buf[i - 1] = tmpF * st->old_pitch_buf[i];
     383             :             }
     384             : 
     385       18600 :             for ( i = 2 * NB_SUBFR16k - NB_SUBFR; i < 2 * NB_SUBFR16k; i++ )
     386             :             {
     387       14880 :                 st->old_pitch_buf[i - 2] = tmpF * st->old_pitch_buf[i];
     388             :             }
     389             :         }
     390             :         else
     391             :         {
     392        2400 :             if ( st->last_L_frame == L_FRAME32k )
     393             :             {
     394         120 :                 tmpF = (float) 16000 / (float) 32000;
     395             :             }
     396        2280 :             else if ( st->last_L_frame == 512 )
     397             :             {
     398         807 :                 tmpF = (float) 16000 / (float) 25600;
     399             :             }
     400             :             else /* st->last_L_frame == L_FRAME12k8 */
     401             :             {
     402        1473 :                 tmpF = (float) 16000 / (float) 12800;
     403             :             }
     404             : 
     405       12000 :             for ( i = 2 * NB_SUBFR - 1; i >= NB_SUBFR; i-- )
     406             :             {
     407        9600 :                 st->old_pitch_buf[i + 2] = tmpF * st->old_pitch_buf[i];
     408             :             }
     409        2400 :             st->old_pitch_buf[NB_SUBFR + 1] = st->old_pitch_buf[NB_SUBFR + 2];
     410             : 
     411       12000 :             for ( i = NB_SUBFR - 1; i >= 0; i-- )
     412             :             {
     413        9600 :                 st->old_pitch_buf[i + 1] = tmpF * st->old_pitch_buf[i];
     414             :             }
     415        2400 :             st->old_pitch_buf[0] = st->old_pitch_buf[1];
     416             :         }
     417             :     }
     418             : 
     419      530307 :     if ( st->bfi_pitch_frame != st->L_frame )
     420             :     {
     421        9813 :         if ( st->L_frame == L_FRAME )
     422             :         {
     423        3660 :             if ( st->bfi_pitch_frame == L_FRAME32k )
     424             :             {
     425         936 :                 tmpF = (float) 12800 / (float) 32000;
     426             :             }
     427        2724 :             else if ( st->bfi_pitch_frame == 512 )
     428             :             {
     429        1008 :                 tmpF = (float) 12800 / (float) 25600;
     430             :             }
     431             :             else /* st->bfi_pitch_frame == L_FRAME16k */
     432             :             {
     433        1716 :                 tmpF = (float) 12800 / (float) 16000;
     434             :             }
     435        3660 :             st->bfi_pitch *= tmpF;
     436        3660 :             st->bfi_pitch_frame = L_FRAME;
     437             :         }
     438             :         else
     439             :         {
     440        6153 :             if ( st->bfi_pitch_frame == L_FRAME32k )
     441             :             {
     442         120 :                 tmpF = (float) 16000 / (float) 32000;
     443             :             }
     444        6033 :             else if ( st->bfi_pitch_frame == 512 )
     445             :             {
     446         813 :                 tmpF = (float) 16000 / (float) 25600;
     447             :             }
     448             :             else /* st->bfi_pitch_frame == L_FRAME12k8 */
     449             :             {
     450        5220 :                 tmpF = (float) 16000 / (float) 12800;
     451             :             }
     452        6153 :             st->bfi_pitch *= tmpF;
     453        6153 :             st->bfi_pitch_frame = L_FRAME16k;
     454             :         }
     455             :     }
     456             : 
     457      530307 :     if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
     458             :     {
     459           0 :         st->rate_switching_reset = 1;
     460             :     }
     461             : 
     462             :     /*----------------------------------------------------------------------*
     463             :      * GOOD frame
     464             :      *----------------------------------------------------------------------*/
     465             : 
     466      530307 :     if ( !st->bfi )
     467             :     {
     468             : 
     469             :         /*----------------------------------------------------------------*
     470             :          * Decoding of TC subframe classification
     471             :          *----------------------------------------------------------------*/
     472             : 
     473      518217 :         tc_subfr = -1;
     474      518217 :         if ( st->coder_type == TRANSITION )
     475             :         {
     476       41832 :             tc_subfr = tc_classif( st );
     477             :         }
     478             : 
     479             :         /*----------------------------------------------------------------*
     480             :          * Decoding of GSC IVAS mode
     481             :          *----------------------------------------------------------------*/
     482             : 
     483      518217 :         if ( st->element_mode > EVS_MONO && st->idchan == 0 && !( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && !tdm_low_rate_mode )
     484             :         {
     485      422340 :             if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
     486             :             {
     487       45192 :                 st->GSC_IVAS_mode = get_next_indice( st, 2 );
     488             :             }
     489             :         }
     490             : 
     491             :         /*----------------------------------------------------------------*
     492             :          * Decoding of inactive CNG frames
     493             :          *----------------------------------------------------------------*/
     494             : 
     495      518217 :         if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
     496             :         {
     497             :             /* decode CNG parameters */
     498       58263 :             if ( st->cng_type == LP_CNG )
     499             :             {
     500        3450 :                 CNG_dec( st, last_element_mode, Aq, lsp_new, lsf_new, &allow_cn_step, sid_bw, q_env );
     501             : 
     502             :                 /* comfort noise generation */
     503        3450 :                 local_element_mode = st->element_mode;
     504        3450 :                 if ( ( nchan_out == 1 && st->element_mode == IVAS_CPE_DFT ) || ( st->masa_sid_format == 1 ) )
     505             :                 {
     506        3450 :                     local_element_mode = IVAS_SCE; /* For DFT Stereo mono decoding, run CNG_exc as in SCE */
     507             :                 }
     508        3450 :                 CNG_exc( st->core_brate, st->L_frame, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, exc, exc2, &st->lp_ener, st->last_core_brate, &st->first_CNG, &( st->hTdCngDec->cng_ener_seed ), bwe_exc, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, sid_bw, &st->hTdCngDec->cng_ener_seed1, exc3, st->Opt_AMR_WB, local_element_mode );
     509        3450 :                 mvr2r( Aq, st->Aq_cng, M + 1 );
     510             :             }
     511             :             else
     512             :             {
     513       54813 :                 if ( st->core_brate == SID_2k40 && st->element_mode != IVAS_CPE_MDCT )
     514             :                 {
     515        3855 :                     FdCng_decodeSID( st );
     516        3855 :                     *sid_bw = 0;
     517             :                 }
     518             : 
     519       54813 :                 if ( st->element_mode == IVAS_CPE_DFT )
     520             :                 {
     521       11049 :                     assert( nchan_out == 1 );
     522             : 
     523      276225 :                     for ( i = 0; i < NPART; i++ )
     524             :                     {
     525      265176 :                         st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] = STEREO_DFT_FD_FILT * st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] + ( 1 - STEREO_DFT_FD_FILT ) * st->hFdCngDec->hFdCngCom->sidNoiseEst[i];
     526             :                     }
     527       11049 :                     ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
     528             :                 }
     529             : 
     530       54813 :                 if ( !read_sid_info )
     531             :                 {
     532             :                     float noise_lvl_highest;
     533             : 
     534        9387 :                     noise_lvl_highest = st->hFdCngDec->hFdCngCom->cngNoiseLevel[st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand - 1];
     535      195813 :                     for ( int16_t b = st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand; b < st->hFdCngDec->hFdCngCom->stopBand; b++ )
     536             :                     {
     537      186426 :                         st->hFdCngDec->hFdCngCom->cngNoiseLevel[b] = noise_lvl_highest;
     538             :                     }
     539             :                 }
     540             : 
     541       54813 :                 generate_comfort_noise_dec( NULL, NULL, st, nchan_out );
     542             : 
     543       54813 :                 FdCng_exc( st->hFdCngDec->hFdCngCom, &st->CNG_mode, st->L_frame, st->lsp_old, st->first_CNG, st->lspCNG, Aq, lsp_new, lsf_new, exc, exc2, bwe_exc );
     544             : 
     545       54813 :                 mvr2r( exc2, exc3, st->L_frame );
     546             :             }
     547             : 
     548             :             /* update past excitation signals for LD music post-filter */
     549       58263 :             if ( st->hMusicPF != NULL )
     550             :             {
     551       36525 :                 mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
     552       36525 :                 mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
     553             : 
     554             :                 /* Update music post processing values */
     555             :                 /* Filter energies update */
     556    23412525 :                 for ( i = 0; i < DCT_L_POST; i++ )
     557             :                 {
     558    23376000 :                     st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
     559             :                 }
     560             :             }
     561             : 
     562             : #ifdef DEBUG_MODE_ACELP
     563             :             dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc", st->idchan, st->id_element, DEC ) );
     564             : #endif
     565             : 
     566             :             /* synthesis at 12.8kHz sampling rate */
     567       58263 :             syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
     568       58263 :             syn_12k8( st->L_frame, Aq, exc3, syn1, st->mem_syn3, 1 );
     569             : 
     570             :             /* reset the decoder */
     571       58263 :             CNG_reset_dec( st, pitch_buf, voice_factors );
     572             : 
     573             :             /* update st->mem_syn1 for ACELP core switching */
     574       58263 :             mvr2r( st->mem_syn3, st->mem_syn1, M );
     575             : 
     576             :             /* update old synthesis for classification */
     577       58263 :             mvr2r( syn1 + st->L_frame - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
     578             : 
     579             :             /* save and delay synthesis to be used by SWB BWE */
     580       58263 :             if ( st->hBWE_FD != NULL )
     581             :             {
     582       36618 :                 save_old_syn( st->L_frame, syn1, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     583             :             }
     584             :         }
     585             : 
     586             :         /*----------------------------------------------------------------*
     587             :          * Decoding of all other frames
     588             :          *----------------------------------------------------------------*/
     589             : 
     590             :         else
     591             :         {
     592             :             /*-----------------------------------------------------------------*
     593             :              * Configure ACELP bit allocation
     594             :              *-----------------------------------------------------------------*/
     595             : 
     596      459954 :             nb_bits = 0;
     597      459954 :             st->acelp_cfg.FEC_mode = 0;
     598      459954 :             uc_two_stage_flag = 0;
     599             : 
     600      459954 :             if ( !st->nelp_mode_dec && !st->ppp_mode_dec )
     601             :             {
     602             :                 int16_t tc_subfr_tmp;
     603             : 
     604      459954 :                 tc_subfr_tmp = tc_subfr;
     605      459954 :                 if ( tc_subfr_tmp < L_SUBFR )
     606             :                 {
     607      438033 :                     tc_subfr_tmp = 0;
     608             :                 }
     609             : 
     610      459954 :                 if ( tc_subfr == TC_0_192 )
     611             :                 {
     612         645 :                     nb_bits = -1;
     613             :                 }
     614             : 
     615      459954 :                 config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, st->GSC_noisy_speech, &( st->acelp_cfg ), st->next_bit_pos, st->coder_type, st->inactive_coder_type_flag, tc_subfr_tmp, 1, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     616             : 
     617      459954 :                 if ( st->coder_type == TRANSITION && tc_subfr < L_SUBFR && st->L_frame == L_FRAME )
     618             :                 {
     619       11733 :                     config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, -1, &( st->acelp_cfg ), st->next_bit_pos, TRANSITION, -1, tc_subfr, 2, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     620             :                 }
     621             :             }
     622             : 
     623             :             /*-----------------------------------------------------------------*
     624             :              * After CNG period, use the most up-to-date LSPs
     625             :              *-----------------------------------------------------------------*/
     626             : 
     627      459954 :             if ( st->hTdCngDec != NULL && ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_2k40 ) )
     628             :             {
     629        1860 :                 mvr2r( st->lspCNG, st->lsp_old, M );
     630        1860 :                 lsp2lsf( st->lspCNG, st->lsf_old, M, int_fs );
     631             :             }
     632             : 
     633             :             /*-----------------------------------------------------------------*
     634             :              * Reset higher ACELP pre-quantizer in case of switching
     635             :              *-----------------------------------------------------------------*/
     636             : 
     637      459954 :             if ( !st->use_acelp_preq )
     638             :             {
     639      419466 :                 st->mem_preemp_preQ = 0.0f;
     640      419466 :                 st->last_nq_preQ = 0;
     641      419466 :                 st->last_code_preq = 0;
     642             :             }
     643      459954 :             st->use_acelp_preq = 0;
     644             : 
     645             :             /*-----------------------------------------------------------------*
     646             :              * LSF de-quantization and interpolation
     647             :              *-----------------------------------------------------------------*/
     648             : 
     649      459954 :             if ( !tdm_lp_reuse_flag )
     650             :             {
     651      459528 :                 lsf_dec( st, tc_subfr, Aq, &LSF_Q_prediction, lsf_new, lsp_new, lsp_mid, tdm_low_rate_mode, tdm_lsfQ_PCh );
     652             :             }
     653             :             else
     654             :             {
     655             :                 const float *pt_interp_2;
     656             :                 int16_t beta_index;
     657             : 
     658         426 :                 beta_index = get_next_indice( st, TDM_IC_LSF_PRED_BITS );
     659         426 :                 tdm_SCh_lsf_reuse( DEC, st->element_brate, lsf_new, lsp_new, tdm_lsfQ_PCh, NULL, &beta_index );
     660             : 
     661         426 :                 if ( st->rate_switching_reset )
     662             :                 {
     663             :                     /* extrapolation in case of unstable LSF convert */
     664           0 :                     mvr2r( lsp_new, st->lsp_old, M );
     665           0 :                     mvr2r( lsf_new, st->lsf_old, M );
     666             :                 }
     667             : 
     668         426 :                 pt_interp_2 = interpol_frac_12k8;
     669         426 :                 if ( tdm_low_rate_mode == 1 && st->coder_type > UNVOICED )
     670             :                 {
     671           0 :                     pt_interp_2 = interpol_frac2;
     672             :                 }
     673             : 
     674         426 :                 if ( st->active_cnt == 1 )
     675             :                 {
     676           0 :                     mvr2r( lsp_new, st->lsp_old, M );
     677           0 :                     lsp2lsf( lsp_new, st->lsf_old, M, st->sr_core );
     678             :                 }
     679             : 
     680             :                 /* LSP interpolation and conversion of LSPs to A(z) */
     681         426 :                 int_lsp( st->L_frame, st->lsp_old, lsp_new, Aq, M, pt_interp_2, 0 );
     682             :                 /* Check LSF stability (distance between old LSFs and current LSFs) */
     683         426 :                 st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
     684             :             }
     685             : 
     686      459954 :             if ( st->last_core == HQ_CORE && st->element_mode > EVS_MONO )
     687             :             {
     688             :                 /* Prepare ACB memory from last HQ frame */
     689         663 :                 old_exc_s = st->old_exc + L_EXC_MEM_DEC - st->L_frame;
     690         663 :                 tmpF = *old_exc_s;
     691         663 :                 st->mem_deemph = old_exc_s[st->L_frame - 1];
     692         663 :                 preemph( old_exc_s, st->preemph_fac, st->L_frame, &tmpF );
     693         663 :                 mvr2r( old_exc_s + st->L_frame - M, st->mem_syn2, M );
     694         663 :                 residu( Aq, M, old_exc_s, old_exc + L_EXC_MEM_DEC - st->L_frame, st->L_frame );
     695             :             }
     696             : 
     697      459954 :             if ( st->last_core != ACELP_CORE && st->element_mode > EVS_MONO )
     698             :             {
     699             :                 /* Prepare ACB memory of old_bwe_exc */
     700       20487 :                 if ( st->L_frame == L_FRAME )
     701             :                 {
     702       10965 :                     lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * HIBND_ACB_L_FAC, L_EXC_MEM_DEC );
     703             :                 }
     704             :                 else
     705             :                 {
     706        9522 :                     lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * 2, L_EXC_MEM_DEC );
     707             :                 }
     708             :             }
     709             : 
     710             :             /*-----------------------------------------------------------------*
     711             :              * FEC - first good frame after lost frame(s) (possibility to correct the ACB)
     712             :              *-----------------------------------------------------------------*/
     713             : 
     714      459954 :             if ( st->acelp_cfg.FEC_mode > 0 )
     715             :             {
     716      357147 :                 last_pulse_pos = 0;
     717             : 
     718             :                 /* decode the last glottal pulse position */
     719      357147 :                 T0_tmp = FEC_pos_dec( st, &last_pulse_pos, &enr_q, nb_bits );
     720             : 
     721      357147 :                 if ( st->last_core != HQ_CORE || ( st->last_core == HQ_CORE && st->last_con_tcx ) )
     722             :                 {
     723      356388 :                     if ( st->clas_dec == SIN_ONSET && last_pulse_pos != 0 && st->prev_bfi == 1 )
     724             :                     {
     725           9 :                         FEC_SinOnset( old_exc + L_EXC_MEM_DEC - L_EXC_MEM, last_pulse_pos, T0_tmp, enr_q, Aq, st->L_frame );
     726             :                     }
     727      356379 :                     else if ( ( st->coder_type == GENERIC || st->coder_type == VOICED ) && last_pulse_pos != 0 && st->old_bfi_cnt == 1 && st->hWIDec != NULL )
     728             :                     {
     729           0 :                         do_WI = FEC_enhACB( st->L_frame, st->last_L_frame, old_exc + L_EXC_MEM_DEC - L_EXC_MEM, T0_tmp, last_pulse_pos, st->bfi_pitch );
     730             :                     }
     731             :                 }
     732             :             }
     733             : 
     734             :             /*------------------------------------------------------------*
     735             :              * In case of first frame after an erasure and transition from voiced to unvoiced or inactive
     736             :              * redo the LPC interpolation
     737             :              *------------------------------------------------------------*/
     738             : 
     739      459954 :             if ( st->stab_fac == 0 && st->old_bfi_cnt > 0 && st->clas_dec != VOICED_CLAS && st->clas_dec != ONSET && st->relax_prev_lsf_interp == 0 && !( st->element_mode == IVAS_CPE_TD && st->idchan == 1 ) )
     740             :             {
     741         495 :                 int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, 2 );
     742             :             }
     743             : 
     744             :             /*---------------------------------------------------------------*
     745             :              * Decoding of the scaled predicted innovation energy
     746             :              *---------------------------------------------------------------*/
     747             : 
     748      459954 :             if ( nb_bits > 0 )
     749             :             {
     750      382383 :                 indice = get_next_indice( st, nb_bits );
     751      382383 :                 Es_pred_dec( &Es_pred, indice, nb_bits, uc_two_stage_flag );
     752             :             }
     753             : 
     754             :             /*------------------------------------------------------------*
     755             :              * Decode excitation according to coding type
     756             :              *------------------------------------------------------------*/
     757             : 
     758      459954 :             if ( tdm_low_rate_mode ) /* tdm stereo low rate mode */
     759             :             {
     760       22509 :                 if ( st->coder_type <= UNVOICED )
     761             :                 {
     762       22509 :                     tdm_low_rate_dec( st, dct_exc_tmp, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, lsf_new );
     763             :                 }
     764             :                 else /* GENERIC */
     765             :                 {
     766           0 :                     decod_gen_2sbfr( st, sharpFlag, Aq, pitch_buf, voice_factors, exc, exc2, bwe_exc, gain_buf, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf );
     767             : 
     768           0 :                     if ( st->element_mode == IVAS_CPE_TD )
     769             :                     {
     770           0 :                         tmp_noise = st->lp_gainc;
     771             :                     }
     772             :                 }
     773             :             }
     774      437445 :             else if ( st->nelp_mode_dec )
     775             :             {
     776             :                 /* SC-VBR - NELP frames */
     777           0 :                 decod_nelp( st, &tmp_noise, pitch_buf, exc, exc2, voice_factors, bwe_exc, st->bfi, gain_buf );
     778             :             }
     779      437445 :             else if ( st->coder_type == UNVOICED )
     780             :             {
     781             :                 /* UNVOICED frames */
     782        6000 :                 decod_unvoiced( st, Aq, Es_pred, uc_two_stage_flag, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, gain_buf );
     783             :             }
     784      431445 :             else if ( st->ppp_mode_dec )
     785             :             {
     786             :                 /* SC-VBR - PPP frames */
     787           0 :                 if ( ( error = decod_ppp( st, Aq, pitch_buf, exc, exc2, voice_factors, bwe_exc, gain_buf, st->bfi ) ) != IVAS_ERR_OK )
     788             :                 {
     789           0 :                     return error;
     790             :                 }
     791             :             }
     792      431445 :             else if ( st->coder_type == TRANSITION )
     793             :             {
     794       41832 :                 decod_tran( st, st->L_frame, tc_subfr, Aq, Es_pred, pitch_buf, voice_factors, exc, exc2, bwe_exc, unbits, sharpFlag, gain_buf );
     795             :             }
     796      389613 :             else if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
     797             :             {
     798             :                 /* AUDIO and INACTIVE frames (coded by GSC technology) */
     799       45210 :                 decod_audio( st, dct_exc_tmp, Aq, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, lsf_new, gain_buf, tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf );
     800             :             }
     801             :             else
     802             :             {
     803             :                 /* GENERIC, VOICED and INACTIVE frames (coded by AVQ technology) */
     804      344403 :                 if ( ( error = decod_gen_voic( st, st->L_frame, sharpFlag, Aq, Es_pred, do_WI, pitch_buf, voice_factors, exc, exc2, bwe_exc, unbits, gain_buf, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf ) ) != IVAS_ERR_OK )
     805             :                 {
     806           0 :                     return error;
     807             :                 }
     808             : 
     809      344403 :                 if ( st->element_mode == IVAS_CPE_TD )
     810             :                 {
     811       20067 :                     tmp_noise = st->lp_gainc;
     812             :                 }
     813             :             }
     814             : 
     815             : #ifdef DEBUG_MODE_ACELP
     816             :             dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc", st->idchan, st->id_element, DEC ) );
     817             : #endif
     818             : 
     819             :             /* synthesis for ACELP core switching and SWB BWE */
     820      459954 :             syn_12k8( st->L_frame, Aq, exc, temp_buf, st->mem_syn1, 1 );
     821             : 
     822             :             /* save and delay synthesis to be used by SWB BWE */
     823      459954 :             if ( st->hBWE_FD != NULL )
     824             :             {
     825      459600 :                 save_old_syn( st->L_frame, temp_buf, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     826             :             }
     827             : 
     828             :             /*-----------------------------------------------------------------*
     829             :              * Apply energy matching when switching to inactive frames
     830             :              *-----------------------------------------------------------------*/
     831             : 
     832      459954 :             inact_switch_ematch( exc2, dct_exc_tmp, st->hGSCDec->lt_ener_per_band, st->coder_type, st->inactive_coder_type_flag, st->L_frame, st->bfi, st->last_core, st->last_codec_mode, tdm_low_rate_mode, st->element_mode );
     833             : 
     834             :             /*------------------------------------------------------------*
     835             :              * Decode information and modify the excitation signal of stationary unvoiced frames
     836             :              *------------------------------------------------------------*/
     837             : 
     838      459954 :             if ( !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->nelp_mode_dec != 1 && !( st->element_mode == IVAS_SCE && tdm_low_rate_mode ) )
     839             :             {
     840      426570 :                 stat_noise_uv_dec( st, lsp_new, lsp_mid, Aq, exc2, uc_two_stage_flag );
     841             :             }
     842             : 
     843             :             /*------------------------------------------------------------*
     844             :              * Save filter memory in case the synthesis is redone after scaling
     845             :              * Synthesis at 12k8 Hz sampling rate
     846             :              *------------------------------------------------------------*/
     847             : 
     848             :             /* update past excitation signals for LD music post-filter */
     849      459954 :             if ( st->hMusicPF != NULL )
     850             :             {
     851      459954 :                 mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
     852      459954 :                 mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
     853      459954 :                 mvr2r( st->hMusicPF->dct_post_old_exc, exc_buffer, DCT_L_POST - OFFSET2 );
     854             :             }
     855             : 
     856      459954 :             if ( ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) || ( st->GSC_IVAS_mode >= 1 && st->L_frame == L_FRAME ) )
     857       31194 :             {
     858       31194 :                 int16_t last_coder_type = st->last_coder_type;
     859             : 
     860       31194 :                 if ( ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) || ( st->GSC_IVAS_mode >= 1 && st->GSC_noisy_speech == 0 ) )
     861             :                 {
     862       14319 :                     last_coder_type = AUDIO;
     863             :                 }
     864             : 
     865             :                 /* Extrapolation of the last future part, windowing and high resolution DCT transform */
     866       31194 :                 Prep_music_postP( exc_buffer, dct_buffer, st->hMusicPF->filt_lfE, st->last_core, pitch_buf, st->hMusicPF->LDm_enh_lp_gbin );
     867             : 
     868             :                 /* LD music post-filter */
     869       31194 :                 LD_music_post_filter( st->hMusicPF, dct_buffer, dct_buffer, st->core_brate, AUDIO, last_coder_type );
     870             : 
     871             :                 /* Inverse DCT transform, retrieval of the aligned excitation, re-synthesis */
     872       31194 :                 mvr2r( st->mem_syn2, mem_tmp, M );
     873       31194 :                 Post_music_postP( dct_buffer, exc_buffer, exc2, st->mem_syn2, st->mem_syn2, Aq, syn );
     874             :             }
     875             :             else
     876             :             {
     877             :                 /* Core synthesis at 12.8kHz or 16kHz */
     878      428760 :                 mvr2r( st->mem_syn2, mem_tmp, M );
     879      428760 :                 syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
     880             : 
     881      428760 :                 if ( st->hMusicPF != NULL )
     882             :                 {
     883   274835160 :                     for ( i = 0; i < DCT_L_POST; i++ )
     884             :                     {
     885   274406400 :                         st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
     886             :                     }
     887             :                 }
     888             :             }
     889             : 
     890             :             /*------------------------------------------------------------*
     891             :              * FEC - Estimate the classification information
     892             :              *------------------------------------------------------------*/
     893             : 
     894      459954 :             FEC_clas_estim( syn, pitch_buf, st->L_frame, st->coder_type, st->codec_mode, st->mem_syn_clas_estim, &st->clas_dec, &st->lp_ener_bfi, st->Opt_AMR_WB, &st->decision_hyst, NULL, NULL, NULL, NULL, NULL, NULL, temp_buf, 0, 0, 0, 0, 0, 0, st->last_core_brate, st->acelp_cfg.FEC_mode );
     895             : 
     896             :             /*------------------------------------------------------------*
     897             :              * FEC - Estimate pitch
     898             :              *------------------------------------------------------------*/
     899             : 
     900      459954 :             FEC_pitch_estim( st->Opt_AMR_WB, st->last_core, st->L_frame, st->clas_dec, st->last_good, pitch_buf, st->old_pitch_buf, &st->bfi_pitch, &st->bfi_pitch_frame, &st->upd_cnt, st->coder_type );
     901             : 
     902             :             /*------------------------------------------------------------*
     903             :              * FEC - Smooth the speech energy evolution when recovering after a BAD frame
     904             :              * (smoothing is performed in the excitation domain and signal is resynthesized after)
     905             :              *------------------------------------------------------------*/
     906             : 
     907      459954 :             FEC_scale_syn( st->L_frame, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, st->coder_type, LSF_Q_prediction, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
     908             : 
     909             :             /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
     910      459954 :             if ( ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->total_brate <= ACELP_7k20 ) || ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
     911             :             {
     912         333 :                 fer_energy( st->L_frame, st->clas_dec, syn, pitch_buf[( ( st->L_frame ) >> 6 ) - 1], &st->enr_old, st->L_frame );
     913             :             }
     914             :         }
     915             : 
     916             :     } /* End of GOOD FRAME */
     917             : 
     918             :     /*----------------------------------------------------------------*
     919             :      * BAD frame
     920             :      *----------------------------------------------------------------*/
     921             : 
     922             :     else
     923             :     {
     924             :         /* SC-VBR */
     925       12090 :         if ( st->last_nelp_mode_dec == 1 )
     926             :         {
     927           0 :             st->nelp_mode_dec = 1;
     928             :         }
     929             : 
     930             :         /* long burst frame erasures */
     931       12090 :         if ( st->nbLostCmpt > 5 && st->clas_dec >= VOICED_CLAS && st->clas_dec < INACTIVE_CLAS )
     932             :         {
     933          57 :             st->last_good = VOICED_TRANSITION;
     934             :         }
     935             : 
     936             :         /* LSF estimation and A(z) calculation */
     937       12090 :         lsf_dec_bfi( MODE1, lsf_new, st->lsf_old, st->lsf_adaptive_mean, NULL, st->mem_MA, st->mem_AR, st->stab_fac, st->last_coder_type, st->L_frame, st->last_good, st->nbLostCmpt, 0, NULL, NULL, NULL, st->hGSCDec->Last_GSC_pit_band_idx, st->Opt_AMR_WB, st->bwidth );
     938             : 
     939       12090 :         FEC_lsf2lsp_interp( st, st->L_frame, Aq, lsf_new, lsp_new );
     940             : 
     941       12090 :         if ( st->nelp_mode_dec == 1 )
     942             :         {
     943             :             /* SC-VBR */
     944           0 :             decod_nelp( st, &tmp_noise, pitch_buf, exc, exc2, voice_factors, bwe_exc, st->bfi, gain_buf );
     945           0 :             FEC_pitch = pitch_buf[3];
     946             :         }
     947             :         else
     948             :         {
     949             :             /* calculation of excitation signal */
     950       12090 :             FEC_exc_estim( st, st->L_frame, exc, exc2, dct_exc_tmp, pitch_buf, voice_factors, &FEC_pitch, bwe_exc, lsf_new, &tmp_noise );
     951             : 
     952       12090 :             tmp_noise = st->lp_gainc;
     953             : 
     954             :             /* SC-VBR */
     955       12090 :             st->prev_gain_pit_dec = st->lp_gainp;
     956             :         }
     957             : 
     958             :         /* synthesis for ACELP core switching and SWB BWE */
     959       12090 :         syn_12k8( st->L_frame, Aq, exc, temp_buf, st->mem_syn1, 1 );
     960             : 
     961             :         /* save and delay synthesis to be used by SWB BWE */
     962       12090 :         if ( st->hBWE_FD != NULL )
     963             :         {
     964       12069 :             save_old_syn( st->L_frame, temp_buf, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     965             :         }
     966             : 
     967             :         /* Apply energy matching when switching to inactive frames */
     968       12090 :         inact_switch_ematch( exc2, dct_exc_tmp, st->hGSCDec->lt_ener_per_band, st->coder_type, st->inactive_coder_type_flag, st->L_frame, st->bfi, st->last_core, st->last_codec_mode, tdm_low_rate_mode, st->element_mode );
     969             :         /* update past excitation signals for LD music post-filter */
     970       12090 :         if ( st->hMusicPF != NULL )
     971             :         {
     972       12090 :             mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
     973       12090 :             mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
     974             : 
     975             :             /* Update music post processing values */
     976             :             /* Filter energies update */
     977     7749690 :             for ( i = 0; i < DCT_L_POST; i++ )
     978             :             {
     979     7737600 :                 st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
     980             :             }
     981             :             /* Update circular buffer, keep last energy difference unchanged */
     982      483600 :             for ( i = 1; i < MAX_LT; i++ )
     983             :             {
     984      471510 :                 st->hMusicPF->LDm_lt_diff_etot[i - 1] = st->hMusicPF->LDm_lt_diff_etot[i];
     985             :             }
     986             :         }
     987             : 
     988             :         /* synthesis at 12k8 Hz sampling rate */
     989       12090 :         if ( ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
     990             :         {
     991           9 :             mvr2r( st->mem_syn2, mem_tmp, M );
     992             :         }
     993       12090 :         syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
     994             : 
     995             :         /* update buffer for classifier */
     996       12090 :         if ( st->hWIDec != NULL )
     997             :         {
     998           0 :             mvr2r( exc2 + st->L_frame - L_EXC_MEM, st->hWIDec->old_exc2, L_EXC_MEM );
     999           0 :             mvr2r( syn + st->L_frame - L_EXC_MEM, st->hWIDec->old_syn2, L_EXC_MEM );
    1000             :         }
    1001       12090 :         mvr2r( syn + st->L_frame - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
    1002             : 
    1003             :         /*------------------------------------------------------------*
    1004             :          * FEC - Smooth the speech energy evolution when recovering after a BAD frame
    1005             :          * (smoothing is performed in the excitation domain and signal is resynthesized after)
    1006             :          *------------------------------------------------------------*/
    1007             : 
    1008       12090 :         if ( ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
    1009             :         {
    1010           9 :             FEC_scale_syn( st->L_frame, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, st->coder_type, LSF_Q_prediction, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
    1011             :         }
    1012             : 
    1013             :         /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
    1014       12090 :         fer_energy( st->L_frame, st->last_good, syn, FEC_pitch, &st->enr_old, st->L_frame );
    1015             : 
    1016       12090 :         if ( st->nelp_mode_dec != 1 )
    1017             :         {
    1018             :             /* modify the excitation signal of stationary unvoiced frames */
    1019       12090 :             stat_noise_uv_mod( st->coder_type, 0, st->lsp_old, lsp_new, lsp_new, Aq, exc2, 1, &st->ge_sm, &st->uv_count, &st->act_count, st->lspold_s, &st->noimix_seed, &st->min_alpha, &st->exc_pe, st->core_brate, st->bwidth );
    1020             :         }
    1021             :     }
    1022             : 
    1023      530307 :     if ( st->hBWE_TD != NULL )
    1024             :     {
    1025      508287 :         if ( st->L_frame == L_FRAME )
    1026             :         {
    1027      270630 :             mvr2r( Aq + 2 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
    1028             :         }
    1029             :         else
    1030             :         {
    1031      237657 :             mvr2r( Aq + 3 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
    1032             :         }
    1033             :     }
    1034             : 
    1035             :     /*--------------------------------------------------------*
    1036             :      * Apply NB postfilter in case of 8kHz output
    1037             :      *--------------------------------------------------------*/
    1038             : 
    1039      530307 :     if ( st->last_bwidth == NB && st->hPFstat != NULL )
    1040             :     {
    1041        7419 :         if ( st->bwidth == NB )
    1042             :         {
    1043           0 :             st->hPFstat->on = 1;
    1044           0 :             nb_post_filt( st->L_frame, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, st->coder_type, st->BER_detect, 0 );
    1045             :         }
    1046             :         else
    1047             :         {
    1048        7419 :             st->hPFstat->on = 0;
    1049        7419 :             nb_post_filt( st->L_frame, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, AUDIO, st->BER_detect, 0 );
    1050             :         }
    1051             :     }
    1052             :     else
    1053             :     {
    1054      522888 :         st->psf_lp_noise = st->lp_noise;
    1055             :     }
    1056             : 
    1057             :     /*------------------------------------------------------------------*
    1058             :      * Perform fixed deemphasis through 1/(1 - g*z^-1)
    1059             :      *-----------------------------------------------------------------*/
    1060             : 
    1061             :     /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
    1062      530307 :     mvr2r( syn + st->L_frame - L_SYN_MEM, st->mem_syn_r, L_SYN_MEM );
    1063      530307 :     deemph( syn, st->preemph_fac, st->L_frame, &( st->mem_deemph ) );
    1064             : 
    1065      530307 :     AGC_dec( syn, st->agc_mem2, st->L_frame );
    1066             : 
    1067      530307 :     if ( st->hTcxDec != NULL )
    1068             :     {
    1069      518967 :         mvr2r( syn + st->L_frame / 2, st->hTcxDec->old_syn_Overl, st->L_frame / 2 );
    1070             :     }
    1071      530307 :     mvr2r( syn + st->L_frame - M - 1, st->syn, M + 1 );
    1072             : 
    1073             :     /*------------------------------------------------------------------*
    1074             :      * Formant post-filter
    1075             :      *-----------------------------------------------------------------*/
    1076             : 
    1077      530307 :     if ( st->hPFstat != NULL && st->last_bwidth >= WB && ( st->core_brate > ACELP_24k40 || st->element_mode > EVS_MONO ) && st->core_brate <= ACELP_32k )
    1078             :     {
    1079      470790 :         mvr2r( syn, temp_buf + L_SYN_MEM, L_FRAME16k );
    1080             : 
    1081      470790 :         st->hPFstat->on = 1;
    1082      470790 :         formant_post_filt( st->hPFstat, temp_buf + L_SYN_MEM, Aq, syn, st->L_frame, L_SUBFR, st->lp_noise, st->total_brate, 0 );
    1083             :     }
    1084       59517 :     else if ( st->hPFstat != NULL && st->last_bwidth >= WB )
    1085             :     {
    1086       30360 :         if ( st->hPFstat->on )
    1087             :         {
    1088        3696 :             mvr2r( st->hPFstat->mem_pf_in + L_SYN_MEM - M, temp_buf, M );
    1089        3696 :             mvr2r( syn, temp_buf + M, L_SUBFR );
    1090        3696 :             residu( Aq, M, temp_buf + M, temp_buf + M + L_SUBFR, L_SUBFR );
    1091        3696 :             syn_filt( Aq, M, temp_buf + M + L_SUBFR, temp_buf, L_SUBFR, st->hPFstat->mem_stp + L_SYN_MEM - M, 0 );
    1092        3696 :             scale_st( syn, temp_buf, &st->hPFstat->gain_prec, L_SUBFR, -1 );
    1093        3696 :             mvr2r( temp_buf, syn, L_SUBFR / 2 );
    1094        3696 :             blend_subfr2( temp_buf + L_SUBFR / 2, syn + L_SUBFR / 2, syn + L_SUBFR / 2 );
    1095             :         }
    1096       30360 :         st->hPFstat->on = 0;
    1097             :     }
    1098             : 
    1099             :     /*----------------------------------------------------------------*
    1100             :      * Comfort noise addition
    1101             :      *----------------------------------------------------------------*/
    1102             : 
    1103      530307 :     if ( ( st->hFdCngDec != NULL || st->idchan == 1 ) && st->element_mode != IVAS_CPE_MDCT )
    1104             :     {
    1105      508569 :         if ( st->element_mode == IVAS_CPE_TD || st->flag_cna || ( st->cng_type == FD_CNG && st->total_brate <= ACELP_32k ) || ( st->cng_type == LP_CNG && st->core_brate <= SID_2k40 ) )
    1106             :         {
    1107             :             /*VAD only for non inactive frame*/
    1108      443757 :             st->VAD = st->VAD && ( st->coder_type != INACTIVE );
    1109             : 
    1110      443757 :             if ( st->idchan == 0 && ( st->flag_cna || ( st->cng_type == FD_CNG && st->total_brate <= ACELP_32k ) || ( st->cng_type == LP_CNG && st->core_brate <= SID_2k40 ) ) )
    1111             :             {
    1112             :                 /*Noisy speech detector*/
    1113      427941 :                 noisy_speech_detection( st->hFdCngDec, st->VAD, syn );
    1114             : 
    1115      427941 :                 st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
    1116             :             }
    1117             : 
    1118      443757 :             if ( st->idchan == 0 )
    1119             :             {
    1120      432417 :                 st->lp_noise = st->hFdCngDec->lp_noise;
    1121             :             }
    1122             : 
    1123      443757 :             if ( st->element_mode != IVAS_CPE_TD && !st->cng_ism_flag )
    1124             :             {
    1125             :                 /*Noise estimate*/
    1126      405780 :                 ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
    1127             :             }
    1128             : 
    1129      443757 :             if ( !st->cna_dirac_flag )
    1130             :             {
    1131             :                 /* CNA: Generate additional comfort noise to mask potential coding artefacts */
    1132      435225 :                 if ( st->flag_cna && !( st->coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->GSC_noisy_speech ) ) )
    1133             :                 {
    1134      235170 :                     if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
    1135             :                     {
    1136        2724 :                         if ( hStereoCng->flag_cna_fade )
    1137             :                         {
    1138          36 :                             generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 1, hStereoCng, nchan_out );
    1139          36 :                             hStereoCng->flag_cna_fade = 0;
    1140             :                         }
    1141             :                         else
    1142             :                         {
    1143        2688 :                             if ( st->element_mode != last_element_mode && st->idchan == 0 )
    1144             :                             {
    1145             :                                 /* Clear memory for secondary channel CNA */
    1146           6 :                                 set_f( hStereoCng->olapBufferSynth22, 0.0f, st->hFdCngDec->hFdCngCom->frameSize / 2 );
    1147             :                             }
    1148             : 
    1149        2688 :                             generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 0, hStereoCng, nchan_out );
    1150             :                         }
    1151             :                     }
    1152      232446 :                     else if ( st->element_mode != IVAS_CPE_DFT )
    1153             :                     {
    1154      122481 :                         if ( st->idchan == 0 )
    1155             :                         {
    1156      122148 :                             if ( st->element_mode != last_element_mode )
    1157             :                             {
    1158           0 :                                 set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.0f, st->hFdCngDec->hFdCngCom->fftlen );
    1159             :                             }
    1160      122148 :                             generate_masking_noise( syn, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, 0, 0, 0, st->element_mode, hStereoCng, nchan_out );
    1161             :                         }
    1162             :                     }
    1163             :                 }
    1164      200055 :                 else if ( st->flag_cna && st->coder_type == AUDIO && ( ( st->last_core == ACELP_CORE && !( st->last_coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->Last_GSC_noisy_speech_flag ) ) ) || st->last_core == TCX_20_CORE ) )
    1165             :                 {
    1166        4956 :                     if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
    1167             :                     {
    1168           0 :                         generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 1, hStereoCng, nchan_out );
    1169           0 :                         hStereoCng->flag_cna_fade = 1;
    1170             :                     }
    1171             :                     else
    1172             :                     {
    1173        4956 :                         v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * st->hFdCngDec->hFdCngCom->frameSize / 4, (float) ( st->hFdCngDec->hFdCngCom->fftlen / 2 ), temp_buf, st->hFdCngDec->hFdCngCom->frameSize / 2 );
    1174        4956 :                         v_add( temp_buf, syn, syn, st->hFdCngDec->hFdCngCom->frameSize / 2 );
    1175             :                     }
    1176             :                 }
    1177             :                 else
    1178             :                 {
    1179      195099 :                     if ( hStereoCng != NULL )
    1180             :                     {
    1181       23526 :                         hStereoCng->flag_cna_fade = 1;
    1182       23526 :                         hStereoCng->enableSecCNA = 0;
    1183             :                     }
    1184             :                 }
    1185             : 
    1186      435225 :                 if ( st->element_mode == IVAS_CPE_TD )
    1187             :                 {
    1188             :                     /*Noise estimate*/
    1189       22191 :                     if ( st->idchan == 0 && ( nchan_out == 2 || ( st->core_brate != FRAME_NO_DATA && st->core_brate != SID_2k40 ) ) )
    1190             :                     {
    1191       10851 :                         ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
    1192             :                     }
    1193             :                 }
    1194             :             }
    1195             :         }
    1196             : 
    1197      508569 :         if ( !st->cna_dirac_flag )
    1198             :         {
    1199      489513 :             if ( st->flag_cna == 0 && st->L_frame == L_FRAME16k && st->last_flag_cna == 1 && ( ( st->last_core == ACELP_CORE && !( st->last_coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->Last_GSC_noisy_speech_flag ) ) ) || st->last_core == AMR_WB_CORE ) )
    1200             :             {
    1201         315 :                 v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * st->L_frame / 4, 256.f, temp_buf, st->L_frame / 2 );
    1202         315 :                 v_add( temp_buf, syn, syn, st->L_frame / 2 );
    1203             :             }
    1204             : 
    1205      489513 :             if ( st->flag_cna == 0 || ( st->coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->GSC_noisy_speech ) ) )
    1206             :             {
    1207      254343 :                 if ( st->idchan == 0 )
    1208             :                 {
    1209      244716 :                     set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.f, st->hFdCngDec->hFdCngCom->fftlen );
    1210             :                 }
    1211      254343 :                 if ( hStereoCng != NULL && st->idchan == 0 )
    1212             :                 {
    1213       15429 :                     set_f( hStereoCng->olapBufferSynth22, 0.f, st->hFdCngDec->hFdCngCom->fftlen );
    1214             :                 }
    1215             :             }
    1216             :         }
    1217             :     }
    1218             : 
    1219             : #ifdef DEBUG_MODE_ACELP
    1220             :     dbgwrite( syn, sizeof( float ), st->L_frame, 1, fname( debug_dir, "syn.intFs", st->idchan, st->id_element, DEC ) );
    1221             : #endif
    1222             : 
    1223             :     /*----------------------------------------------------------------*
    1224             :      * Resample to the output sampling rate (8/16/32/48 kHz)
    1225             :      * Bass post-filter
    1226             :      *----------------------------------------------------------------*/
    1227             : 
    1228             :     /* check if the CLDFB works on the right sample rate */
    1229      530307 :     if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
    1230             :     {
    1231       10188 :         resampleCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
    1232       10188 :         resampleCldfb( st->cldfbBPF, st->L_frame * FRAMES_PER_SEC );
    1233             : 
    1234       10188 :         if ( st->ini_frame > 0 )
    1235             :         {
    1236        6342 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1237             :         }
    1238             :     }
    1239             : 
    1240             :     /* analyze pitch coherence for bass post-filter */
    1241      530307 :     bpf_pitch_coherence( st, pitch_buf );
    1242             : 
    1243      530307 :     if ( !( st->element_mode == IVAS_CPE_MDCT && st->bpf_off ) )
    1244             :     {
    1245      508569 :         if ( st->L_frame != st->last_L_frame && st->last_codec_mode != MODE2 )
    1246             :         {
    1247        5388 :             if ( st->L_frame == L_FRAME )
    1248             :             {
    1249        3714 :                 retro_interp5_4( st->hBPF->pst_old_syn );
    1250             :             }
    1251        1674 :             else if ( st->L_frame == L_FRAME16k )
    1252             :             {
    1253        1674 :                 retro_interp4_5( syn, st->hBPF->pst_old_syn );
    1254             :             }
    1255             :         }
    1256             : 
    1257             :         /* bass post-filter */
    1258      508569 :         bass_psfilter( st->hBPF, st->Opt_AMR_WB, syn, st->L_frame, pitch_buf, st->bpf_off, st->stab_fac, &st->stab_fac_smooth, st->coder_type, bpf_error_signal );
    1259             :     }
    1260             : 
    1261      530307 :     if ( st->element_mode != IVAS_CPE_DFT || use_cldfb_for_dft )
    1262      458421 :     {
    1263             :         float realBufferSave[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    1264             :         float imagBufferSave[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    1265             :         float *pRealSave[CLDFB_NO_COL_MAX], *pImagSave[CLDFB_NO_COL_MAX];
    1266     7793157 :         for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1267             :         {
    1268     7334736 :             pRealSave[i] = realBufferSave[i];
    1269     7334736 :             pImagSave[i] = imagBufferSave[i];
    1270             :         }
    1271      458421 :         if ( st->p_bpf_noise_buf )
    1272             :         {
    1273      419976 :             mvr2r( bpf_error_signal, st->p_bpf_noise_buf, st->L_frame );
    1274             :         }
    1275             : 
    1276             :         /* analysis of the synthesis at internal sampling rate */
    1277      458421 :         cldfbAnalysis( syn, realBuffer, imagBuffer, -1, st->cldfbAna );
    1278             : 
    1279             :         /* analysis and add the BPF error signal */
    1280      458421 :         addBassPostFilter( bpf_error_signal, st->bpf_off ? 0 : -1, realBuffer, imagBuffer, st->cldfbBPF );
    1281             : 
    1282             :         /* set output mask for upsampling */
    1283      458421 :         if ( st->bwidth == NB )
    1284             :         {
    1285             :             /* set NB mask for upsampling */
    1286           0 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - 10;
    1287             :         }
    1288      458421 :         else if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
    1289             :         {
    1290             :             /* in case of BW switching, re-init to default */
    1291       45957 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1292             :         }
    1293             : 
    1294      458421 :         if ( !st->cng_sba_flag || st->element_mode == IVAS_CPE_MDCT )
    1295             :         {
    1296             :             /*WB/SWB-FD_CNG*/
    1297      424578 :             if ( ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && ( st->cng_type == FD_CNG ) && ( st->hFdCngDec->hFdCngCom->numCoreBands < st->cldfbSyn->no_channels ) )
    1298             :             {
    1299       44895 :                 generate_comfort_noise_dec_hf( realBuffer, imagBuffer, st->hFdCngDec->hFdCngCom, st->cng_ism_flag );
    1300             : 
    1301       44895 :                 if ( st->hFdCngDec->hFdCngCom->regularStopBand < st->cldfbSyn->no_channels )
    1302             :                 {
    1303       33960 :                     st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->hFdCngDec->hFdCngCom->regularStopBand;
    1304             :                 }
    1305             :                 else
    1306             :                 {
    1307       10935 :                     st->cldfbSyn->bandsToZero = 0;
    1308             :                 }
    1309             :             }
    1310             :         }
    1311             : 
    1312      458421 :         if ( save_hb_synth != NULL )
    1313             :         {
    1314             :             /* save and then zero-out lowband */
    1315       16653 :             for ( int16_t j = 0; j < CLDFB_NO_CHANNELS_MAX; j++ )
    1316             :             {
    1317      278460 :                 for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1318             :                 {
    1319      262080 :                     realBufferSave[i][j] = realBuffer[i][j];
    1320      262080 :                     imagBufferSave[i][j] = imagBuffer[i][j];
    1321      262080 :                     if ( j < st->hFdCngDec->hFdCngCom->numCoreBands && i < st->hFdCngDec->hFdCngCom->numSlots )
    1322             :                     {
    1323       87360 :                         realBuffer[i][j] = 0.0f;
    1324       87360 :                         imagBuffer[i][j] = 0.0f;
    1325             :                     }
    1326             :                 }
    1327             :             }
    1328             : 
    1329         273 :             cldfbSynthesis( realBuffer, imagBuffer, save_hb_synth, -1, st->cldfbSynHB );
    1330             : 
    1331             :             /* restore lowband */
    1332       16653 :             for ( int16_t j = 0; j < CLDFB_NO_CHANNELS_MAX; j++ )
    1333             :             {
    1334      278460 :                 for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1335             :                 {
    1336      262080 :                     realBuffer[i][j] = realBufferSave[i][j];
    1337      262080 :                     imagBuffer[i][j] = imagBufferSave[i][j];
    1338             :                 }
    1339             :             }
    1340             : 
    1341         273 :             cldfbSynthesis( pRealSave, pImagSave, synth, -1, st->cldfbSyn );
    1342             :         }
    1343             :         else
    1344             :         {
    1345             :             /* synthesis of the combined signal */
    1346      458148 :             cldfbSynthesis( realBuffer, imagBuffer, synth, -1, st->cldfbSyn );
    1347             :         }
    1348             : 
    1349             :         /* save synthesis - needed in case of core switching */
    1350      458421 :         mvr2r( synth, st->previoussynth, output_frame );
    1351             : 
    1352             : #ifdef DEBUG_MODE_ACELP
    1353             :         dbgwrite( synth, sizeof( float ), output_frame, 1, fname( debug_dir, "output.Fs", st->idchan, st->id_element, DEC ) );
    1354             : #endif
    1355             :     }
    1356             :     else
    1357             :     {
    1358       71886 :         int16_t nSamples = NS2SA( st->L_frame * FRAMES_PER_SEC, FRAME_SIZE_NS /*DELAY_CLDFB_NS*/ ); /* IVAS-64: optimization is likely possible here (don't resample the whole frame) */
    1359             : 
    1360             :         /* analysis of the synthesis at internal sampling rate - needed for DFT stereo -> TD stereo switching */
    1361       71886 :         cldfbAnalysis( syn + st->L_frame - nSamples, realBuffer, imagBuffer, nSamples, st->cldfbAna );
    1362             : 
    1363             :         /* analysis and add the BPF error signal - needed for DFT stereo -> TD stereo switching */
    1364       71886 :         addBassPostFilter( bpf_error_signal + st->L_frame - nSamples, st->bpf_off ? 0 : nSamples, realBuffer, imagBuffer, st->cldfbBPF );
    1365             : 
    1366             :         /* synthesis of the combined signal - needed for DFT stereo -> TD stereo switching */
    1367       71886 :         cldfbSynthesis( realBuffer, imagBuffer, synth /*dummy*/, NS2SA( st->output_Fs, FRAME_SIZE_NS /*DELAY_CLDFB_NS*/ ), st->cldfbSyn );
    1368             : 
    1369       71886 :         if ( st->p_bpf_noise_buf )
    1370             :         {
    1371       70032 :             mvr2r( bpf_error_signal, st->p_bpf_noise_buf, st->L_frame );
    1372             :         }
    1373             : 
    1374             : #ifdef DEBUG_MODE_ACELP
    1375             :         dbgwrite( synth, sizeof( float ), output_frame, 1, fname( debug_dir, "output.Fs", st->idchan, st->id_element, DEC ) );
    1376             : #endif
    1377       71886 :         set_f( synth, 0.0f, output_frame );
    1378             :     }
    1379             : 
    1380             :     /* Copy output signal */
    1381      530307 :     if ( st->element_mode > EVS_MONO )
    1382             :     {
    1383      526077 :         mvr2r( syn, output, st->L_frame );
    1384             :     }
    1385             : 
    1386             :     /*-----------------------------------------------------------------*
    1387             :      * Bandwidth extension 6kHz-7kHz
    1388             :      *-----------------------------------------------------------------*/
    1389             : 
    1390      530307 :     if ( st->hBWE_zero != NULL )
    1391             :     {
    1392      508569 :         if ( ( st->L_frame == L_FRAME && st->bwidth != NB && output_frame >= L_FRAME16k &&
    1393      270999 :                ( st->extl == -1 || st->extl == SWB_CNG || ( st->extl == WB_BWE && st->extl_brate == 0 && st->coder_type != AUDIO ) ) ) )
    1394             :         {
    1395       46926 :             hf_synth( st->hBWE_zero, st->core_brate, output_frame, Aq, exc2, syn, synth );
    1396             :         }
    1397             :         else
    1398             :         {
    1399      461643 :             hf_synth_reset( st->hBWE_zero );
    1400             :         }
    1401             :     }
    1402             : 
    1403             :     /*-----------------------------------------------------------------*
    1404             :      * Populate parameters for SWB TBE
    1405             :      *-----------------------------------------------------------------*/
    1406             : 
    1407      530307 :     if ( st->hBWE_TD != NULL )
    1408             :     {
    1409      508287 :         if ( ( !st->bfi && st->prev_bfi ) || ( st->last_vbr_hw_BWE_disable_dec == 1 && st->vbr_hw_BWE_disable_dec == 0 ) || ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->last_extl != SWB_TBE && st->last_extl != WB_TBE && st->last_extl != FB_TBE ) || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && !st->tdm_LRTD_flag ) )
    1410             :         {
    1411       28971 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
    1412       28971 :             set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
    1413             :         }
    1414             : 
    1415      508287 :         if ( !st->ppp_mode_dec && ( st->idchan == 0 || st->element_mode != IVAS_CPE_TD || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) ) )
    1416             :         {
    1417      508287 :             non_linearity( bwe_exc, bwe_exc_extended, st->hBWE_TD->old_bwe_exc_extended, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale, st->coder_type, voice_factors, st->L_frame );
    1418             :         }
    1419             : 
    1420      508287 :         if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
    1421             :         {
    1422       36618 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
    1423             :         }
    1424             :     }
    1425             : 
    1426             :     /*----------------------------------------------------------------------*
    1427             :      * Updates
    1428             :      *----------------------------------------------------------------------*/
    1429             : 
    1430      530307 :     updt_dec( st, old_exc, pitch_buf, Es_pred, Aq, lsf_new, lsp_new, voice_factors, old_bwe_exc, gain_buf );
    1431             : 
    1432      530307 :     if ( st->core_brate > SID_2k40 && st->hTdCngDec != NULL && st->hFdCngDec != NULL )
    1433             :     {
    1434             :         /* update CNG parameters in active frames */
    1435      313326 :         cng_params_upd( lsp_new, exc, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
    1436             : 
    1437             :         /* Set 16k LSP flag for CNG buffer */
    1438      313326 :         st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
    1439             :     }
    1440             : 
    1441      530307 :     pop_wmops();
    1442      530307 :     return error;
    1443             : }

Generated by: LCOV version 1.14