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

Generated by: LCOV version 1.14