LCOV - code coverage report
Current view: top level - lib_dec - dec_tcx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 599 650 92.2 %
Date: 2025-05-23 08:37:30 Functions: 11 11 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 <assert.h>
      38             : #include "prot.h"
      39             : #include "ivas_prot.h"
      40             : #include <stdint.h>
      41             : #include "options.h"
      42             : #ifdef DEBUGGING
      43             : #include "debug.h"
      44             : #endif
      45             : #include <math.h>
      46             : #include "stat_com.h"
      47             : #include "cnst.h"
      48             : #include "wmc_auto.h"
      49             : #include "ivas_rom_com.h"
      50             : #ifdef DEBUG_PLOT
      51             : #include "deb_out.h"
      52             : #endif
      53             : 
      54             : 
      55             : /*-----------------------------------------------------------------*
      56             :  * decoder_tcx()
      57             :  *
      58             :  *
      59             :  *-----------------------------------------------------------------*/
      60             : 
      61      707661 : void decoder_tcx(
      62             :     Decoder_State *st,                  /* i/o: coder memory state                     */
      63             :     int16_t prm[],                      /* i  : parameters                             */
      64             :     float A[],                          /* i  : coefficients NxAz[M+1]                 */
      65             :     Word16 Aind[],                      /* i  : frame-independent coefficients Az[M+1] */
      66             :     float synth[],                      /* i/o: synthesis @internal_FS                 */
      67             :     float synthFB[],                    /* i/o: synthesis @output_FS                   */
      68             :     const int16_t bfi,                  /* i  : Bad frame indicator                    */
      69             :     const int16_t frame_cnt,            /* i  : frame counter in the super frame       */
      70             :     const int16_t sba_dirac_stereo_flag /* i  : signal stereo output for SBA DirAC     */
      71             : )
      72             : {
      73             :     float x[N_MAX];
      74             :     float gainlpc2[FDNS_NPTS];
      75             :     float gain_tcx;
      76             :     int16_t fUseTns, L_frame_glob, L_frameTCX_glob;
      77             :     STnsData tnsData;
      78             :     int16_t tcx_offset, tcx_offsetFB, L_frame, L_frameTCX;
      79             :     int16_t left_rect, L_spec, tmp_concealment_method, nf_seed;
      80             :     const int16_t *prm_sqQ;
      81             :     float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
      82             : 
      83      707661 :     L_spec = st->hTcxCfg->tcx_coded_lines;
      84      707661 :     L_frame_glob = st->L_frame;
      85      707661 :     L_frameTCX_glob = st->hTcxDec->L_frameTCX;
      86      707661 :     if ( st->core == TCX_10_CORE )
      87             :     {
      88       12408 :         L_spec /= 2;
      89       12408 :         L_frame_glob /= 2;
      90       12408 :         L_frameTCX_glob /= 2;
      91             :     }
      92             : 
      93      707661 :     tmp_concealment_method = 0;
      94      707661 :     nf_seed = 0;
      95      707661 :     fUseTns = 0; /* flag that is set if TNS data is present */
      96      707661 :     prm_sqQ = NULL;
      97             : 
      98      707661 :     set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
      99             : 
     100      707661 :     init_tcx_info( st, L_frame_glob, L_frameTCX_glob, frame_cnt, bfi, &tcx_offset, &tcx_offsetFB, &L_frame, &L_frameTCX, &left_rect, &L_spec );
     101             : 
     102      707661 :     decoder_tcx_invQ( st, prm, A, Aind, L_spec, L_frame, L_frameTCX, &x[0], &gainlpc2[0], &xn_buf[0], &fUseTns, &tnsData, &gain_tcx, &prm_sqQ, &nf_seed, bfi, frame_cnt );
     103             : 
     104      707661 :     decoder_tcx_noisefilling( st, NULL, A, L_frameTCX_glob, L_spec, L_frame, L_frameTCX, &x[0], &gainlpc2[0], &tmp_concealment_method, gain_tcx, prm_sqQ, nf_seed, bfi, 0, frame_cnt );
     105             : 
     106      707661 :     decoder_tcx_noiseshaping_igf( st, L_spec, L_frame, L_frameTCX, left_rect, &x[0], &gainlpc2[0], &tmp_concealment_method, bfi );
     107             : 
     108      707661 :     decoder_tcx_tns( st, L_frame_glob, L_spec, L_frame, L_frameTCX, &x[0], fUseTns, &tnsData, bfi, frame_cnt, 0 );
     109             : 
     110      707661 :     decoder_tcx_imdct( st, L_frame_glob, L_frameTCX_glob, L_spec, tcx_offset, tcx_offsetFB, L_frame, L_frameTCX, left_rect, &x[0], &xn_buf[0], MDCT_IV,
     111             :                        fUseTns, &synth[0], &synthFB[0], bfi, frame_cnt, sba_dirac_stereo_flag );
     112             : 
     113      707661 :     return;
     114             : }
     115             : 
     116             : 
     117             : /*-------------------------------------------------------------------*
     118             :  * decoder_tcx_post()
     119             :  *
     120             :  *
     121             :  *-------------------------------------------------------------------*/
     122             : 
     123     2805831 : void decoder_tcx_post(
     124             :     Decoder_State *st,     /* i/o: decoder memory state                 */
     125             :     float *synth,          /* i/o: synthesis                            */
     126             :     float *synthFB,        /* i/o: FB synthesis                         */
     127             :     float *A,              /* i  : A(z) filter coefficients             */
     128             :     const int16_t bfi,     /* i  : Bad frame indicator                  */
     129             :     const int16_t MCT_flag /* i  : hMCT handle allocated (1) or not (0) */
     130             : )
     131             : {
     132             :     int16_t i;
     133     2805831 :     float level_syn, gainCNG = 0.0f, step;
     134             :     float xn_buf[L_FRAME_MAX];
     135     2805831 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
     136             : 
     137             :     /* TCX output */
     138     2805831 :     mvr2r( synth, xn_buf, st->L_frame );
     139             : 
     140             :     /* first TCX frame after ACELP; overwrite ltp initialization done during acelp PLC */
     141     2805831 :     if ( !bfi && st->prev_bfi && !st->last_core )
     142             :     {
     143         441 :         hTcxDec->tcxltp_last_gain_unmodified = 0.0f;
     144             :     }
     145             : 
     146     2805831 :     if ( st->hTcxLtpDec != NULL && st->element_mode > EVS_MONO && ( st->clas_dec == UNVOICED_CLAS || st->clas_dec == INACTIVE_CLAS ) )
     147             :     {
     148             :         /* deactivate TCX LTP for non-voiced frames */
     149     1470900 :         st->hTcxLtpDec->tcxltp = 0;
     150     1470900 :         st->hTcxLtpDec->tcxltp_gain = 0.0f;
     151             :     }
     152             : 
     153     2805831 :     if ( bfi && !st->use_partial_copy )
     154             :     {
     155             :         /* run lpc gain compensation not for waveform adjustment */
     156       37497 :         if ( !st->enablePlcWaveadjust || ( st->hPlcInfo != NULL && st->hPlcInfo->concealment_method == TCX_TONAL ) )
     157             :         {
     158       37497 :             float gainHelperFB = hTcxDec->gainHelper;
     159       37497 :             float stepCompensateFB = hTcxDec->stepCompensate * st->L_frame / hTcxDec->L_frameTCX;
     160             : 
     161    27164217 :             for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
     162             :             {
     163    27126720 :                 synthFB[i] *= gainHelperFB;
     164    27126720 :                 gainHelperFB -= stepCompensateFB;
     165             :             }
     166             :         }
     167             : 
     168    17876025 :         for ( i = 0; i < st->L_frame; i++ )
     169             :         {
     170    17838528 :             xn_buf[i] *= hTcxDec->gainHelper;
     171    17838528 :             hTcxDec->gainHelper -= hTcxDec->stepCompensate;
     172             :         }
     173             :     }
     174             : 
     175             :     /* PLC: [TCX: Fade-out]
     176             :      * PLC: estimate and update CNG energy */
     177     2805831 :     level_syn = (float) sqrt( ( dotp( synthFB, synthFB, hTcxDec->L_frameTCX ) ) / hTcxDec->L_frameTCX );
     178             : 
     179             :     /* PLC: [TCX: Fade-out]
     180             :      * PLC: update or retrieve the background level */
     181     2805831 :     if ( bfi == 0 && st->tcxonly && ( st->element_mode != IVAS_CPE_MDCT || MCT_flag ) && st->clas_dec == UNVOICED_CLAS )
     182             :     {
     183     1086396 :         minimumStatistics( hTcxDec->NoiseLevelMemory_bfi, &hTcxDec->NoiseLevelIndex_bfi, &hTcxDec->CurrLevelIndex_bfi, &hTcxDec->CngLevelBackgroundTrace_bfi, &hTcxDec->LastFrameLevel_bfi, level_syn, PLC_MIN_CNG_LEV, PLC_MIN_STAT_BUFF_SIZE );
     184             :     }
     185             : 
     186             :     /* PLC: [TCX: Fade-out]
     187             :      * PLC: fade-out in time domain */
     188     2805831 :     if ( bfi )
     189             :     {
     190             :         float conceal_eof_gainFB;
     191             : 
     192       37497 :         if ( st->tcxonly )
     193             :         {
     194       31779 :             gainCNG = hTcxDec->CngLevelBackgroundTrace_bfi / ( level_syn + 0.01f );
     195             : 
     196       31779 :             if ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag )
     197             :             {
     198       10923 :                 if ( st->nbLostCmpt > MDCT_ST_PLC_FADEOUT_MAX_CONC_FRAME + MDCT_ST_PLC_FADEOUT_TO_ZERO_LEN )
     199             :                 {
     200           0 :                     gainCNG = 0.f;
     201             :                 }
     202       10923 :                 else if ( st->nbLostCmpt > MDCT_ST_PLC_FADEOUT_MAX_CONC_FRAME )
     203             :                 {
     204           0 :                     gainCNG *= 1.f - (float) ( st->nbLostCmpt - MDCT_ST_PLC_FADEOUT_MAX_CONC_FRAME ) / MDCT_ST_PLC_FADEOUT_TO_ZERO_LEN;
     205             :                 }
     206             :             }
     207             :         }
     208             :         else
     209             :         {
     210        5718 :             gainCNG = st->cngTDLevel / ( level_syn + 0.01f );
     211             :         }
     212       37497 :         if ( st->nbLostCmpt == 1 )
     213             :         {
     214       22410 :             hTcxDec->conceal_eof_gain = 1.0f;
     215             :         }
     216             : 
     217       37497 :         step = ( hTcxDec->conceal_eof_gain - ( hTcxDec->conceal_eof_gain * hTcxDec->damping + gainCNG * ( 1 - hTcxDec->damping ) ) ) / st->L_frame;
     218             :         {
     219       37497 :             float stepFB = step * st->L_frame / hTcxDec->L_frameTCX;
     220       37497 :             conceal_eof_gainFB = hTcxDec->conceal_eof_gain;
     221             : 
     222    27164217 :             for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
     223             :             {
     224    27126720 :                 synthFB[i] *= conceal_eof_gainFB;
     225    27126720 :                 conceal_eof_gainFB -= stepFB;
     226             :             }
     227             :         }
     228             : 
     229    17876025 :         for ( i = 0; i < st->L_frame; i++ )
     230             :         {
     231    17838528 :             xn_buf[i] *= hTcxDec->conceal_eof_gain;
     232    17838528 :             hTcxDec->conceal_eof_gain -= step;
     233             :         }
     234             : 
     235             :         /* run lpc gain compensation not for waveform adjustment */
     236       37497 :         if ( st->hPlcInfo != NULL )
     237             :         {
     238           0 :             if ( ( !st->enablePlcWaveadjust || st->hPlcInfo->concealment_method == TCX_TONAL ) && !st->use_partial_copy )
     239             :             {
     240           0 :                 st->hPlcInfo->recovery_gain = conceal_eof_gainFB * st->last_concealed_gain_syn_deemph;
     241             :             }
     242             :             else
     243             :             {
     244           0 :                 st->hPlcInfo->recovery_gain = conceal_eof_gainFB;
     245             :             }
     246           0 :             st->hPlcInfo->step_concealgain = step * st->L_frame / hTcxDec->L_frameTCX;
     247             :         }
     248             :     }
     249             : 
     250             : 
     251             :     /*-----------------------------------------------------------*
     252             :      * Memory update                                             *
     253             :      *-----------------------------------------------------------*/
     254             : 
     255             :     /* Update synth, exc and old_Aq */
     256     2805831 :     tcx_decoder_memory_update( st, xn_buf, synth, A );
     257             : 
     258             : #ifdef DEBUG_MODE_TCX
     259             :     {
     260             :         int16_t tmp[320];
     261             :         for ( i = 0; i < L_EXC_MEM; i++ )
     262             :         {
     263             :             tmp[i] = (int16_t) ( st->old_exc[L_EXC_MEM_DEC - L_EXC_MEM + i] + 0.5f );
     264             :         }
     265             :         dbgwrite( tmp, 2, L_EXC_MEM, 1, "res/exc_old.dec.pcm" );
     266             :     }
     267             : #endif
     268             : 
     269             :     /* PLC: [TCX: Memory update] */
     270     2805831 :     st->old_pitch_buf[0] = st->old_pitch_buf[st->nb_subfr];
     271     2805831 :     st->old_pitch_buf[1] = st->old_pitch_buf[st->nb_subfr + 1];
     272     2805831 :     mvr2r( &st->old_pitch_buf[st->nb_subfr + 2], &st->old_pitch_buf[2], st->nb_subfr );
     273     2805831 :     set_f( &st->old_pitch_buf[st->nb_subfr + 2], st->old_fpitch, st->nb_subfr );
     274     2805831 :     st->bfi_pitch = st->old_fpitch;
     275     2805831 :     st->bfi_pitch_frame = st->L_frame;
     276             : 
     277     2805831 :     st->mem_pitch_gain[2 * st->nb_subfr + 1] = st->mem_pitch_gain[st->nb_subfr + 1];
     278     2805831 :     st->mem_pitch_gain[2 * st->nb_subfr] = st->mem_pitch_gain[st->nb_subfr];
     279             : 
     280    15801264 :     for ( i = 0; i < st->nb_subfr; i++ )
     281             :     {
     282    12995433 :         st->mem_pitch_gain[2 * st->nb_subfr - 1 - i] = st->mem_pitch_gain[st->nb_subfr - 1 - i];
     283    12995433 :         st->mem_pitch_gain[st->nb_subfr - 1 - i] = hTcxDec->tcxltp_last_gain_unmodified;
     284             :     }
     285             : 
     286     2805831 :     return;
     287             : }
     288             : 
     289             : 
     290             : /*-------------------------------------------------------------------*
     291             :  * IMDCT()
     292             :  *
     293             :  *
     294             :  *-------------------------------------------------------------------*/
     295             : 
     296     5286246 : void IMDCT(
     297             :     float *x,
     298             :     float *old_syn_overl,
     299             :     float *syn_Overl_TDAC,
     300             :     float *xn_buf,
     301             :     const float *tcx_aldo_window_1_trunc,
     302             :     const float *tcx_aldo_window_2,
     303             :     const float *tcx_mdct_window_half,
     304             :     const float *tcx_mdct_window_minimum,
     305             :     const float *tcx_mdct_window_trans,
     306             :     const int16_t tcx_mdct_window_half_length,
     307             :     const int16_t tcx_mdct_window_min_length,
     308             :     int16_t index,
     309             :     const uint16_t kernel_type, /* i  : TCX transform kernel type               */
     310             :     const int16_t left_rect,
     311             :     const int16_t tcx_offset,
     312             :     const int16_t overlap,
     313             :     const int16_t L_frame,
     314             :     const int16_t L_frameTCX,
     315             :     const int16_t L_spec_TCX5,
     316             :     const int16_t L_frame_glob,
     317             :     const int16_t frame_cnt,
     318             :     const int16_t bfi,
     319             :     float *old_out,
     320             :     const int16_t FB_flag,
     321             :     Decoder_State *st,
     322             :     const int16_t fullbandScale,
     323             :     float *acelp_zir )
     324             : {
     325             :     int16_t i, nz, aldo, w, L_win, L_ola;
     326             :     float win[( L_FRAME_PLUS + L_MDCT_OVLP_MAX ) / 2];
     327     5286246 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
     328     5286246 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
     329             : 
     330     5286246 :     aldo = 0;
     331             : 
     332             :     /* number of zero for ALDO windows*/
     333     5286246 :     nz = NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) * L_frame / L_frameTCX;
     334             : 
     335     5286246 :     if ( st->element_mode != EVS_MONO && frame_cnt == 0 && !bfi && st->prev_bfi && ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE ) && !hTcxCfg->last_aldo )
     336             :     {
     337         312 :         v_multc( old_syn_overl, hTcxDec->conceal_eof_gain * st->last_concealed_gain_syn_deemph, old_syn_overl, overlap );
     338             :     }
     339             : 
     340     5286246 :     if ( ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && st->tcxonly )
     341             :     {
     342             :         /* Mode decision in PLC
     343             : 
     344             :         last OL   curr OL   left TCX-10  right TCX-10
     345             :         -------------------------------------------------------------
     346             :             0      0         2x TCX-5* 1x  TCX-10
     347             :             0      2         1x TCX-10 1x  TCX-10
     348             :             0      3         1x TCX-10 1x  TCX-10
     349             :             2      0         2x TCX-5  1x  TCX-10
     350             :             2      2         2x TCX-5  2x  TCX-5
     351             :             2      3         2x TCX-5  2x  TCX-5
     352             :             3      0         2x TCX-5  1x  TCX-10
     353             :             3      2         2x TCX-5  2x  TCX-5
     354             :             3      3         2x TCX-5  2x  TCX-5
     355             :         */
     356             : 
     357      199344 :         if ( ( !bfi && hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) || ( bfi && ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) && ( hTcxCfg->tcx_curr_overlap_mode != FULL_OVERLAP ) ) )
     358             :         {
     359             :             /* minimum or half overlap, two transforms, grouping into one window */
     360       57096 :             L_win = L_frame >> 1;
     361       57096 :             L_ola = ( hTcxCfg->tcx_last_overlap_mode == MIN_OVERLAP ) ? tcx_mdct_window_min_length : tcx_mdct_window_half_length;
     362             : 
     363       57096 :             set_f( win, 0, ( L_FRAME_PLUS + L_MDCT_OVLP_MAX ) / 2 );
     364       57096 :             set_zero( xn_buf, tcx_offset + ( L_ola >> 1 ) ); /* zero left end of buffer */
     365             : 
     366      171288 :             for ( w = 0; w < 2; w++ )
     367             :             {
     368      114192 :                 if ( kernel_type == MDST_IV || ( kernel_type & w ) )
     369             :                 {
     370          24 :                     TCX_MDST_Inverse( x + w * L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
     371             :                 }
     372      114168 :                 else if ( kernel_type != 0 && w == 0 ) /* type 1 or 2 */
     373             :                 {
     374         102 :                     TCX_MDXT_Inverse( x + w * L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, kernel_type );
     375             :                 }
     376             :                 else
     377             :                 {
     378      114066 :                     TCX_MDCT_Inverse( x + w * L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
     379             :                 }
     380             : 
     381      114192 :                 tcx_windowing_synthesis_current_frame( win, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, ( w > 0 ) ? 0 : left_rect, ( w > 0 ) || ( w == 0 && index == 2 ) ? MIN_OVERLAP : hTcxCfg->tcx_last_overlap_mode, acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_win, tcx_offset < 0 ? -tcx_offset : 0, ( w > 0 ) || ( frame_cnt > 0 ) ? 1 : st->last_core, ( w > 0 ) || ( frame_cnt > 0 ) ? 0 : st->last_is_cng, fullbandScale );
     382             : 
     383      114192 :                 if ( w > 0 )
     384             :                 {
     385       57096 :                     tcx_windowing_synthesis_past_frame( xn_buf + tcx_offset - ( L_ola >> 1 ) + w * L_win, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, MIN_OVERLAP );
     386             :                 }
     387             : 
     388             :                 /* add part of current sub-window overlapping with previous window */
     389      114192 :                 v_add( win, xn_buf + tcx_offset - ( L_ola >> 1 ) + w * L_win, xn_buf + tcx_offset - ( L_ola >> 1 ) + w * L_win, L_ola );
     390             : 
     391             :                 /* copy new sub-window region not overlapping with previous window */
     392      114192 :                 mvr2r( win + L_ola, xn_buf + tcx_offset + ( L_ola >> 1 ) + w * L_win, L_win );
     393             :             }
     394             : 
     395             :             /* To assure that no garbage values are passed to overlap */
     396       57096 :             set_zero( xn_buf + L_frame + tcx_offset + ( L_ola >> 1 ), overlap - tcx_offset - ( L_ola >> 1 ) );
     397             : 
     398       57096 :             if ( st->prev_bfi && frame_cnt == 0 && st->last_core != st->last_core_bfi && st->last_core_bfi == ACELP_CORE )
     399             :             {
     400           0 :                 tcx_windowing_synthesis_past_frame( old_syn_overl, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, hTcxCfg->tcx_last_overlap_mode );
     401           0 :                 v_add( xn_buf, old_syn_overl, xn_buf, overlap );
     402             :             }
     403             :         }
     404      142248 :         else if ( !bfi && ( frame_cnt == 0 ) && ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) )
     405             : 
     406             :         {
     407             :             /* special overlap attempt, two transforms, grouping into one window */
     408       46254 :             L_win = L_frame >> 1;
     409       46254 :             L_ola = tcx_mdct_window_min_length;
     410             : 
     411       46254 :             set_f( win, 0, ( L_FRAME_PLUS + L_MDCT_OVLP_MAX ) / 2 );
     412             : 
     413             :             /* 1st TCX-5 window, special MDCT with minimum overlap on right side */
     414       46254 :             if ( kernel_type == MDST_IV )
     415             :             {
     416           0 :                 TCX_MDST_Inverse( x, win + L_win, 0, L_win - ( L_ola >> 1 ), L_ola, st->element_mode );
     417             :             }
     418       46254 :             else if ( kernel_type != MDCT_IV ) /* type 1 or 2 */
     419             :             {
     420          12 :                 TCX_MDXT_Inverse( x, win + L_win, 0, L_win - ( L_ola >> 1 ), L_ola, kernel_type );
     421             :             }
     422             :             else
     423             :             {
     424       46242 :                 TCX_MDCT_Inverse( x, win + L_win, 0, L_win - ( L_ola >> 1 ), L_ola, st->element_mode );
     425             :             }
     426             : 
     427       46254 :             set_zero( xn_buf, ( overlap >> 1 ) );
     428             : 
     429             :             /* copy new sub-window region not overlapping with previous window */
     430       46254 :             mvr2r( win + L_win, xn_buf + ( overlap >> 1 ), L_win + ( L_ola >> 1 ) );
     431             : 
     432             :             /* 2nd TCX-5 window, regular MDCT with minimum overlap on both sides */
     433       46254 :             if ( kernel_type & 1 )
     434             :             {
     435          12 :                 TCX_MDST_Inverse( x + L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
     436             :             }
     437             :             else
     438             :             {
     439       46242 :                 TCX_MDCT_Inverse( x + L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
     440             :             }
     441             : 
     442       46254 :             tcx_windowing_synthesis_current_frame( win, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, 0,
     443       46254 :                                                    /* left_rect */ MIN_OVERLAP, /* left_mode */ acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_win, tcx_offset < 0 ? -tcx_offset : 0, 1, /* st->last_mode_bfi */ 0, /* st->last_is_cng */ fullbandScale );
     444             : 
     445       46254 :             tcx_windowing_synthesis_past_frame( xn_buf + ( overlap >> 1 ) + L_win - ( L_ola >> 1 ), tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, 2 );
     446             : 
     447             :             /* add part of current sub-window overlapping with previous window */
     448       46254 :             v_add( win, xn_buf + ( overlap >> 1 ) + L_win - ( L_ola >> 1 ), xn_buf + ( overlap >> 1 ) + L_win - ( L_ola >> 1 ), L_ola );
     449             : 
     450             :             /* copy new sub-window region not overlapping with previous window */
     451       46254 :             mvr2r( win + L_ola, xn_buf + ( overlap >> 1 ) + L_win + ( L_ola >> 1 ), L_win );
     452             : 
     453             :             /* extra folding-out on left side of win, for perfect reconstruction */
     454       46254 :             if ( kernel_type >= MDCT_II )
     455             :             {
     456           0 :                 for ( w = ( overlap >> 1 ); w < overlap; w++ )
     457             :                 {
     458           0 :                     xn_buf[overlap - 1 - w] = xn_buf[w];
     459             :                 }
     460             :             }
     461             :             else
     462             :             {
     463     7168404 :                 for ( w = ( overlap >> 1 ); w < overlap; w++ )
     464             :                 {
     465     7122150 :                     xn_buf[overlap - 1 - w] = -1.0f * xn_buf[w];
     466             :                 }
     467             :             }
     468       46254 :             tcx_windowing_synthesis_current_frame( xn_buf, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, left_rect, 0, /* left_mode */ acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, 2 * L_win, tcx_offset < 0 ? -tcx_offset : 0, st->last_core_bfi, st->last_is_cng, fullbandScale );
     469             :         }
     470             :         else
     471             :         {
     472             :             /* default, i.e. maximum overlap, single transform, no grouping */
     473       95994 :             if ( kernel_type == MDST_IV )
     474             :             {
     475          24 :                 TCX_MDST_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
     476             :             }
     477       95970 :             else if ( kernel_type != MDCT_IV ) /* type 1 or 2 */
     478             :             {
     479          78 :                 TCX_MDXT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, kernel_type );
     480             :             }
     481             :             else
     482             :             {
     483       95892 :                 TCX_MDCT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
     484             :             }
     485             : 
     486       95994 :             tcx_windowing_synthesis_current_frame( xn_buf, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, left_rect, !bfi && ( frame_cnt > 0 ) && ( index == 0 ) && ( st->last_core != ACELP_CORE ) ? MIN_OVERLAP : index, acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_frame_glob >> 1, tcx_offset < 0 ? -tcx_offset : 0, ( frame_cnt > 0 /*|| (st->last_con_tcx )*/ ) ? 1 : st->last_core_bfi, ( frame_cnt > 0 ) ? 0 : st->last_is_cng, fullbandScale );
     487             : 
     488             :         } /* tcx_last_overlap_mode != FULL_OVERLAP */
     489             :     }
     490             :     else
     491             :     {
     492             :         /* frame is TCX-20 or not TCX-only */
     493     5086902 :         assert( frame_cnt == 0 );
     494     5086902 :         if ( hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     495             :         {
     496             :             float tmp[L_FRAME_PLUS];
     497     5029200 :             if ( kernel_type != MDCT_IV ) /* inverse transform */
     498             :             {
     499        4740 :                 if ( kernel_type == MDST_IV )
     500             :                 {
     501        1788 :                     edst( x, xn_buf + overlap / 2 + nz, L_frame, st->element_mode );
     502             :                 }
     503             :                 else /* type 1 or 2 */
     504             :                 {
     505        2952 :                     edxt( x, xn_buf + overlap / 2 + nz, L_frame, kernel_type, TRUE );
     506             :                 }
     507        4740 :                 v_multc( xn_buf + overlap / 2 + nz, (float) sqrt( (float) L_frame / NORM_MDCT_FACTOR ), tmp, L_frame );
     508        4740 :                 window_ola_ext( tmp, xn_buf, old_out, L_frame, hTcxCfg->tcx_last_overlap_mode, hTcxCfg->tcx_curr_overlap_mode, kernel_type );
     509             :             }
     510             :             else
     511             :             {
     512     5024460 :                 edct( x, xn_buf + overlap / 2 + nz, L_frame, st->element_mode );
     513             : 
     514     5024460 :                 v_multc( xn_buf + overlap / 2 + nz, (float) sqrt( (float) L_frame / NORM_MDCT_FACTOR ), tmp, L_frame );
     515     5024460 :                 window_ola( tmp, xn_buf, old_out, L_frame, hTcxCfg->tcx_last_overlap_mode, hTcxCfg->tcx_curr_overlap_mode, 0, 0, NULL );
     516             :             }
     517     5029200 :             aldo = 1;
     518             :         }
     519             :         else
     520             :         {
     521       57702 :             int16_t acelp_mem_len = tcx_offset < 0 ? -tcx_offset : 0;
     522             : 
     523       57702 :             if ( kernel_type == MDST_IV )
     524             :             {
     525           0 :                 TCX_MDST_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
     526             :             }
     527       57702 :             else if ( kernel_type != MDCT_IV ) /* type 1 or 2 */
     528             :             {
     529           0 :                 TCX_MDXT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, kernel_type );
     530             :             }
     531             :             else
     532             :             {
     533       57702 :                 TCX_MDCT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
     534             :             }
     535             : 
     536             :             /*-----------------------------------------------------------*
     537             :              * Windowing, overlap and add                                *
     538             :              *-----------------------------------------------------------*/
     539             : 
     540       57702 :             if ( st->element_mode == IVAS_CPE_MDCT && ( st->last_core_brate <= SID_2k40 || st->last_core == ACELP_CORE ) && !fullbandScale )
     541             :             {
     542             :                 float buf[L_FRAME_MAX / 4];
     543             :                 float window_buf[L_FRAME_MAX / 4];
     544             :                 float r[M + 1];
     545        4059 :                 const int16_t analysis_len = L_frame_glob / 4;
     546             : 
     547             :                 /* get the first 5 ms of non-aliased TCX syntesis */
     548        4059 :                 mvr2r( xn_buf + overlap / 2 + 2 * acelp_mem_len, &buf[0], analysis_len );
     549             : 
     550             :                 /* get LPC from that signal part to use for acelp zir smoothing */
     551        4059 :                 ham_cos_window( &window_buf[0], analysis_len / 2, analysis_len / 2 );
     552        4059 :                 autocorr( &buf[0], &r[0], M, analysis_len, &window_buf[0], 0, 0, 0 );
     553        4059 :                 lag_wind( r, M, L_frame_glob * FRAMES_PER_SEC, LAGW_STRONG );
     554        4059 :                 lev_dur( &st->old_Aq_12_8[0], &r[0], M, NULL );
     555             :             }
     556             : 
     557             :             /* Window current frame */
     558       57702 :             tcx_windowing_synthesis_current_frame( xn_buf, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, left_rect, hTcxCfg->tcx_last_overlap_mode, acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_frame_glob >> 1, acelp_mem_len, st->last_core_bfi, st->last_is_cng, fullbandScale );
     559             :         }
     560             :     } /* TCX-20 and TCX-only */
     561             : 
     562             :     /* Window and overlap-add past frame if past frame is TCX */
     563     5286246 :     if ( ( frame_cnt != 0 ) || ( st->last_core_bfi > ACELP_CORE ) )
     564             :     {
     565     5179488 :         if ( ( ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && ( st->tcxonly ) ) ||
     566     5004276 :              ( hTcxCfg->tcx_last_overlap_mode == TRANSITION_OVERLAP ) )
     567             :         {
     568      176928 :             if ( !bfi && ( frame_cnt > 0 ) && ( index == 0 ) && ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( st->last_core != ACELP_CORE ) )
     569             :             {
     570       46254 :                 index = MIN_OVERLAP; /* use minimum overlap between the two TCX-10 windows */
     571             :             }
     572             : 
     573      176928 :             if ( hTcxCfg->last_aldo )
     574             :             {
     575    18759312 :                 for ( i = 0; i < overlap - tcx_mdct_window_min_length; i++ )
     576             :                 {
     577    18686592 :                     xn_buf[i + overlap / 2 - tcx_offset] += old_out[i + nz];
     578             :                 }
     579             : 
     580             :                 /* fade truncated ALDO window */
     581       72720 :                 if ( L_frameTCX == hTcxDec->L_frameTCX >> 1 && st->element_mode == IVAS_CPE_MDCT && frame_cnt == 0 && hTcxCfg->tcx_last_overlap_mode == FULL_OVERLAP )
     582             :                 {
     583     2002686 :                     for ( ; i < overlap; i++ ) /* perfectly reconstructing ALDO shortening */
     584             :                     {
     585     1956816 :                         xn_buf[i + overlap / 2 - tcx_offset] += old_out[i + nz];
     586             :                     }
     587     2002686 :                     for ( i = 0; i < tcx_mdct_window_min_length; i++ )
     588             :                     {
     589     1956816 :                         xn_buf[i + overlap / 2 - tcx_offset + overlap] += old_out[i + nz + overlap] * tcx_mdct_window_minimum[tcx_mdct_window_min_length - 1 - i];
     590             :                     }
     591             :                 }
     592             :                 else
     593             :                 {
     594     1184466 :                     for ( ; i < overlap; i++ )
     595             :                     {
     596     1157616 :                         xn_buf[i + overlap / 2 - tcx_offset] += old_out[i + nz] * tcx_mdct_window_minimum[overlap - 1 - i];
     597             :                     }
     598             :                 }
     599             :             }
     600             :             else
     601             :             {
     602      104208 :                 tcx_windowing_synthesis_past_frame( old_syn_overl, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, ( index == 0 || hTcxCfg->tcx_last_overlap_mode == MIN_OVERLAP ) ? hTcxCfg->tcx_last_overlap_mode : index );
     603             : 
     604      104208 :                 if ( bfi )
     605             :                 {
     606      144972 :                     for ( i = 0; i < tcx_mdct_window_half_length; i++ )
     607             :                     {
     608      143640 :                         xn_buf[i + overlap / 2 - tcx_offset] += old_syn_overl[i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i];
     609             :                     }
     610             :                 }
     611      102876 :                 else if ( !left_rect )
     612             :                 {
     613    31173888 :                     for ( i = 0; i < overlap; i++ )
     614             :                     {
     615    31071012 :                         xn_buf[i] += old_syn_overl[i];
     616             :                     }
     617             :                 }
     618             :                 else
     619             :                 {
     620           0 :                     for ( i = 0; i < overlap; i++ )
     621             :                     {
     622           0 :                         xn_buf[i + ( overlap >> 1 )] += old_syn_overl[i];
     623             :                     }
     624             :                 }
     625             :             }
     626             :         }
     627             :     }
     628             : 
     629     5286246 :     if ( !aldo && ( ( ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && frame_cnt > 0 ) || L_frameTCX != ( hTcxDec->L_frameTCX >> 1 ) ) )
     630             :     {
     631             :         /* Compute windowed synthesis in case of switching to ALDO windows in next frame */
     632      157374 :         mvr2r( xn_buf + L_frame - nz, old_out, nz + overlap );
     633      157374 :         set_zero( old_out + nz + overlap, nz );
     634             : 
     635      157374 :         tcx_windowing_synthesis_past_frame( old_out + nz, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, hTcxCfg->tcx_curr_overlap_mode );
     636             : 
     637             :         /* If current overlap mode = FULL_OVERLAP -> ALDO_WINDOW */
     638      157374 :         if ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
     639             :         {
     640      122118 :             if ( kernel_type & 1 )
     641             :             {
     642        4566 :                 for ( i = 0; i < nz; i++ )
     643             :                 {
     644        4536 :                     old_out[nz + overlap + i] = -1.0f * xn_buf[L_frame - 1 - i] * tcx_aldo_window_1_trunc[-1 - i];
     645             :                 }
     646             :             }
     647             :             else
     648             :             {
     649    22364742 :                 for ( i = 0; i < nz; i++ )
     650             :                 {
     651    22242654 :                     old_out[nz + overlap + i] = xn_buf[L_frame - 1 - i] * tcx_aldo_window_1_trunc[-1 - i];
     652             :                 }
     653             :             }
     654      122118 :             aldo = 1;
     655             :         }
     656             :     }
     657             : 
     658     5286246 :     if ( FB_flag )
     659             :     {
     660     2643123 :         hTcxCfg->last_aldo = aldo;
     661             :     }
     662             : 
     663             :     /* Smoothing between the ACELP PLC and TCX Transition frame. Using the shape of the half overlap window for the crossfading. */
     664     5286246 :     if ( left_rect && ( frame_cnt == 0 ) && ( st->last_core_bfi == ACELP_CORE ) && st->prev_bfi )
     665             :     {
     666         762 :         if ( FB_flag )
     667             :         {
     668       56721 :             for ( i = 0; i < tcx_mdct_window_half_length; i++ )
     669             :             {
     670       56340 :                 xn_buf[i + overlap / 2 - tcx_offset] *= tcx_mdct_window_half[i];
     671       56340 :                 xn_buf[i + overlap / 2 - tcx_offset] += hTcxDec->syn_OverlFB[i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i];
     672             :             }
     673             :         }
     674             :         else
     675             :         {
     676       30801 :             for ( i = 0; i < tcx_mdct_window_half_length; i++ )
     677             :             {
     678       30420 :                 xn_buf[i + overlap / 2 - tcx_offset] *= tcx_mdct_window_half[i];
     679       30420 :                 xn_buf[i + overlap / 2 - tcx_offset] += hTcxDec->syn_Overl[i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i];
     680             :             }
     681             :         }
     682             :     }
     683             : 
     684     5286246 :     return;
     685             : }
     686             : 
     687             : 
     688             : /*-------------------------------------------------------------------*
     689             :  * decoder_tcx_invQ
     690             :  *
     691             :  * TCX: inverse quantization
     692             :  *-------------------------------------------------------------------*/
     693             : 
     694     2653764 : void decoder_tcx_invQ(
     695             :     Decoder_State *st, /* i/o: coder memory state                      */
     696             :     int16_t prm[],     /* i  : parameters                              */
     697             :     float A[],         /* i  : coefficients NxAz[M+1]                  */
     698             :     Word16 Aind[],     /* i  : frame-independent coefficients Az[M+1]  */
     699             :     const int16_t L_spec,
     700             :     const int16_t L_frame,
     701             :     const int16_t L_frameTCX,
     702             :     float x[],
     703             :     float gainlpc2[],
     704             :     float xn_buf[],
     705             :     int16_t *fUseTns, /* o  : flag that is set if TNS data is present */
     706             :     STnsData *tnsData,
     707             :     float *gain_tcx,
     708             :     const int16_t **prm_sqQ1,
     709             :     int16_t *nf_seed,
     710             :     const int16_t bfi,      /* i  : Bad frame indicator                     */
     711             :     const int16_t frame_cnt /* i  : frame counter in the super frame        */
     712             : )
     713             : {
     714             :     int16_t i, index;
     715             :     int16_t start_zeroing;
     716             :     float Ap[M + 2];
     717             : 
     718             :     int16_t noiseFillingSize;
     719             :     int16_t tnsSize; /* number of tns parameters put into prm   */
     720             : 
     721             :     float gamma1;
     722             :     float gamma;
     723     2653764 :     float gainCompensate = 1.f;
     724             :     float h1[L_FRAME_MAX / 4 + 1];
     725             :     float mem[M];
     726             :     float tmp2;
     727             :     int16_t arith_bits, signaling_bits;
     728             :     const int16_t *prm_ltp, *prm_tns, *prm_hm, *prm_sqQ, *prm_target;
     729     2653764 :     TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
     730     2653764 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
     731     2653764 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
     732             : 
     733     2653764 :     tnsSize = 0;
     734             : 
     735             :     /* just to suppress MSVC warnigs */
     736     2653764 :     prm_target = NULL;
     737     2653764 :     prm_ltp = NULL;
     738     2653764 :     prm_tns = NULL;
     739     2653764 :     prm_sqQ = NULL;
     740             : 
     741             :     /*-----------------------------------------------------------------*
     742             :      * Initializations
     743             :      *-----------------------------------------------------------------*/
     744             : 
     745             :     /* Init lengths */
     746     2653764 :     gamma1 = st->gamma;
     747             : 
     748     2653764 :     if ( hTcxDec->enableTcxLpc )
     749             :     {
     750       52128 :         gamma1 = 1.0f;
     751             :     }
     752             : 
     753     2653764 :     noiseFillingSize = L_spec;
     754     2653764 :     if ( st->igf )
     755             :     {
     756     1814607 :         noiseFillingSize = st->hIGFDec->infoIGFStartLine;
     757             :     }
     758             : 
     759             :     /*-----------------------------------------------------------*
     760             :      * Read TCX parameters                                       *
     761             :      *-----------------------------------------------------------*/
     762             : 
     763     2653764 :     index = 0;
     764             : 
     765     2653764 :     if ( !bfi )
     766             :     {
     767     2619240 :         index = prm[0];
     768     2619240 :         prm_ltp = &prm[1 + NOISE_FILL_RANGES];
     769     2619240 :         prm_tns = prm_ltp + LTPSIZE;
     770             : 
     771             :         /* read noise level (fac_ns) */
     772     2619240 :         st->hTcxDec->noise_filling_index[frame_cnt] = prm[1];
     773             :     }
     774             : 
     775             :     /* read TNS data */
     776     2653764 :     if ( !bfi && hTcxCfg->fIsTNSAllowed )
     777             :     {
     778     2398068 :         *fUseTns = DecodeTnsData( hTcxCfg->pCurrentTnsConfig, prm_tns, &tnsSize, tnsData );
     779             :     }
     780             :     else
     781             :     {
     782      255696 :         *fUseTns = 0;
     783             :     }
     784             : 
     785             :     /*-----------------------------------------------------------*
     786             :      * Spectrum data                                             *
     787             :      *-----------------------------------------------------------*/
     788             : 
     789     2653764 :     if ( !bfi )
     790             :     {
     791     2619240 :         prm_hm = prm_tns + tnsSize;
     792     2619240 :         prm_sqQ = prm_hm + NPRM_CTX_HM;
     793     2619240 :         *prm_sqQ1 = prm_sqQ;
     794             : 
     795             :         /*-----------------------------------------------------------*
     796             :          * Context HM                                                *
     797             :          *-----------------------------------------------------------*/
     798             : 
     799     2619240 :         if ( hTcxCfg->ctx_hm && ( ( st->last_core_from_bs != ACELP_CORE ) || ( frame_cnt > 0 ) ) )
     800             :         {
     801      531105 :             st->last_ctx_hm_enabled = prm_hm[0];
     802             : 
     803   448581345 :             for ( i = 0; i < L_spec; i++ )
     804             :             {
     805             :                 /* no context harmonic model, copy MDCT coefficients to x */
     806   448050240 :                 x[i] = (float) prm_sqQ[i];
     807             :             }
     808             :         }
     809             :         else /* hTcxCfg->ctx_hm == 0 */
     810             :         {
     811     2088135 :             if ( hTcxDec->tcx_lpc_shaped_ari ) /* low rates: envelope based arithmetic coder */
     812             :             {
     813       50958 :                 prm_target = prm_sqQ;
     814       50958 :                 prm_sqQ = prm_target + 1;
     815             : 
     816       50958 :                 tcx_arith_decode_envelope( st, x, L_frame, L_spec, Aind, *prm_target, prm_sqQ, st->last_core_from_bs != ACELP_CORE, prm_hm, /* HM parameter area */ hTcxDec->tcx_hm_LtpPitchLag, &arith_bits, &signaling_bits, ( st->bwidth > WB ) ? 1 : 0 );
     817             : 
     818       50958 :                 hTcxDec->resQBits[frame_cnt] = *prm_target - arith_bits;
     819             : 
     820             :                 /* Noise filling seed */
     821    10665966 :                 for ( i = 0; i < noiseFillingSize; ++i )
     822             :                 {
     823    10615008 :                     *nf_seed += (int16_t) ( abs( (int16_t) x[i] ) * i * 2 );
     824             :                 }
     825             :             }
     826             :             else /* TCX-only: context based arithmetic coder */
     827             :             {
     828  1643480697 :                 for ( i = 0; i < L_spec; i++ )
     829             :                 {
     830  1641443520 :                     x[i] = (float) prm_sqQ[i];
     831             :                 }
     832             : 
     833    80324217 :                 for ( i = L_spec; i < L_frameTCX; i++ )
     834             :                 {
     835    78287040 :                     x[i] = 0.0f;
     836             :                 }
     837             :             }
     838             : 
     839             :         } /* else of if hTcxCfg->ctx_hm */
     840             : 
     841     2619240 :         start_zeroing = ( st->last_core != st->last_core_from_bs ) ? min( L_spec, L_frame ) : L_spec;
     842             : 
     843     2619240 :         if ( frame_cnt == 0 && st->last_core == ACELP_CORE && st->last_core_from_bs != ACELP_CORE )
     844             :         {
     845         114 :             int16_t L_spec_con = L_spec + ( st->hTcxCfg->tcx_coded_lines >> 2 );
     846             : 
     847       50514 :             for ( i = start_zeroing; i < L_spec_con; i++ )
     848             :             {
     849       50400 :                 x[i] = 0.0f;
     850             :             }
     851             : 
     852         114 :             start_zeroing = L_spec_con;
     853             :         }
     854             : 
     855   133370664 :         for ( i = start_zeroing; i < max( L_frame, L_frameTCX ); i++ )
     856             :         {
     857   130751424 :             x[i] = 0.0f;
     858             :         }
     859             : 
     860             :         /*-----------------------------------------------------------*
     861             :          * adaptive low frequency deemphasis.                        *
     862             :          *-----------------------------------------------------------*/
     863             : 
     864     2619240 :         if ( st->element_mode != IVAS_CPE_MDCT )
     865             :         {
     866      698367 :             weight_a( A, Ap, gamma1, M );
     867      698367 :             lpc2mdct( Ap, M, gainlpc2, FDNS_NPTS, 0 );
     868             :         }
     869             : 
     870             :         /* initialize LF deemphasis factors in xn_buf */
     871     2619240 :         if ( !st->tcxonly || ( hTcxCfg->resq && hTcxDec->tcx_lpc_shaped_ari ) )
     872             :         {
     873   340587876 :             for ( i = 0; i < max( L_spec, L_frameTCX ); i++ )
     874             :             {
     875   340211520 :                 xn_buf[i] = 1.0f;
     876             :             }
     877             :         }
     878             : 
     879     2619240 :         if ( !st->tcxonly )
     880             :         {
     881      376356 :             AdaptLowFreqDeemph( x, hTcxDec->tcx_lpc_shaped_ari, gainlpc2, L_frame, xn_buf /* LF deemphasis factors */ );
     882             :         }
     883             :     }
     884             : 
     885     2653764 :     hTcxDec->damping = 0.f;
     886             : 
     887     2653764 :     if ( bfi == 0 )
     888             :     {
     889             :         /*-----------------------------------------------------------*
     890             :          * Compute global gain                                       *
     891             :          *-----------------------------------------------------------*/
     892             : 
     893     2619240 :         if ( st->element_mode > EVS_MONO )
     894             :         {
     895             :             float ener;
     896             : 
     897             :             /*To be in sync with encoder gain computating*/
     898     2617302 :             ener = (float) sqrt( (float) L_spec / (float) NORM_MDCT_FACTOR );
     899     2617302 :             *gain_tcx = (float) pow( 10.0f, ( (float) index ) / 28.0f ) / ener;
     900             :         }
     901             :         else
     902             :         {
     903        1938 :             *gain_tcx = (float) pow( 10.0f, index / 28.0f ) * (float) sqrt( (float) NORM_MDCT_FACTOR / (float) L_spec );
     904             :         }
     905             : 
     906     2619240 :         hTcxDec->old_gaintcx_bfi = *gain_tcx;
     907             : 
     908     2619240 :         hTcxDec->cummulative_damping_tcx = 1.0f;
     909             :     }
     910             :     else /* bfi = 1 */
     911             :     {
     912             :         /* PLC: [TCX: Fade-out]
     913             :          * derivation of damping factor */
     914       34524 :         if ( st->use_partial_copy )
     915             :         {
     916           0 :             if ( st->rf_frame_type == RF_TCXFD )
     917             :             {
     918           0 :                 *gain_tcx = (float) pow( 10.0f, (int16_t) hTcxDec->old_gaintcx_bfi / 28.0f ) * (float) sqrt( (float) NORM_MDCT_FACTOR / (float) L_spec );
     919           0 :                 hTcxDec->old_gaintcx_bfi = *gain_tcx;
     920             :             }
     921             :             else
     922             :             {
     923           0 :                 *gain_tcx = hTcxDec->old_gaintcx_bfi;
     924             :             }
     925             : 
     926           0 :             hTcxDec->damping = 1;
     927             :         }
     928             :         else
     929             :         {
     930       34524 :             *gain_tcx = hTcxDec->old_gaintcx_bfi;
     931       34524 :             hTcxDec->damping = Damping_fact( st->coder_type, st->nbLostCmpt, st->last_good, st->stab_fac, &( st->lp_gainp ), st->last_core );
     932             :         }
     933             : 
     934       34524 :         hTcxDec->cummulative_damping_tcx *= hTcxDec->damping;
     935             :     }
     936             : 
     937     2653764 :     if ( bfi )
     938             :     {
     939       34524 :         if ( hTcxDec->envWeighted )
     940             :         {
     941           0 :             gamma = st->gamma;
     942             :         }
     943             :         else
     944             :         {
     945       34524 :             gamma = gamma1;
     946             :         }
     947             : 
     948             :         /* PLC: [TCX: Fade-out]
     949             :          * PLC: invert LPC weighting in case of PLC */
     950             : 
     951       34524 :         if ( hTcxDec->enableTcxLpc )
     952             :         {
     953        1170 :             gamma = hTcxDec->cummulative_damping_tcx * ( st->gamma - 1 ) + 1;
     954             :         }
     955             :         else
     956             :         {
     957       33354 :             gamma = hTcxDec->cummulative_damping_tcx * ( gamma1 - 1 ) + 1;
     958             :         }
     959             : 
     960       34524 :         if ( st->element_mode != IVAS_CPE_MDCT )
     961             :         {
     962        9294 :             weight_a( A, Ap, gamma, M );
     963        9294 :             lpc2mdct( Ap, M, gainlpc2, FDNS_NPTS, 0 );
     964             :         }
     965             :     }
     966             : 
     967     2653764 :     if ( st->element_mode != IVAS_CPE_MDCT )
     968             :     {
     969      707661 :         tmp2 = 0;
     970      707661 :         set_zero( h1, L_SUBFR + 1 );
     971      707661 :         set_zero( mem, M );
     972      707661 :         h1[0] = 1.0f;
     973      707661 :         syn_filt( Ap, M, h1, h1, L_SUBFR, mem, 0 );    /* impulse response of LPC     */
     974      707661 :         deemph( h1, st->preemph_fac, L_SUBFR, &tmp2 ); /* impulse response of deemph  */
     975             :     }
     976             : 
     977             :     /* impulse response level = gain introduced by synthesis+deemphasis */
     978     2653764 :     if ( !bfi )
     979             :     {
     980     2619240 :         if ( st->element_mode == IVAS_CPE_MDCT )
     981             :         {
     982     1920873 :             st->last_gain_syn_deemph = 0.0;
     983             :         }
     984             :         else
     985             :         {
     986      698367 :             st->last_gain_syn_deemph = (float) sqrt( dotp( h1, h1, L_SUBFR ) );
     987             :         }
     988             : 
     989             :         /*for avoiding compiler warnings*/
     990     2619240 :         hTcxDec->gainHelper = 1.f;
     991     2619240 :         hTcxDec->stepCompensate = 0.f;
     992             :     }
     993       34524 :     else if ( st->core == TCX_20_CORE || frame_cnt == 1 )
     994             :     {
     995       34047 :         if ( st->element_mode == IVAS_CPE_MDCT )
     996             :         {
     997       24846 :             gainCompensate = 1.0f;
     998             :         }
     999             :         else
    1000             :         {
    1001        9201 :             gainCompensate = st->last_gain_syn_deemph / (float) sqrt( dotp( h1, h1, L_SUBFR ) );
    1002             :         }
    1003             : 
    1004       34047 :         if ( st->nbLostCmpt == 1 )
    1005             :         {
    1006       20484 :             hTcxDec->stepCompensate = ( 1.f - gainCompensate ) / st->L_frame;
    1007       20484 :             hTcxDec->gainHelper = 1.f;
    1008             :         }
    1009             :         else
    1010             :         {
    1011       13563 :             hTcxDec->stepCompensate = ( st->last_concealed_gain_syn_deemph - gainCompensate ) / st->L_frame;
    1012       13563 :             hTcxDec->gainHelper = st->last_concealed_gain_syn_deemph;
    1013             :         }
    1014       34047 :         st->last_concealed_gain_syn_deemph = gainCompensate;
    1015             :     }
    1016             : 
    1017             :     /*-----------------------------------------------------------*
    1018             :      * Residual inv. Q.                                          *
    1019             :      *-----------------------------------------------------------*/
    1020             : 
    1021     2653764 :     if ( !bfi && hTcxCfg->resq )
    1022             :     {
    1023     1144770 :         if ( hTcxDec->tcx_lpc_shaped_ari )
    1024             :         {
    1025             :             /* envelope based arithmetic coder */
    1026             :             const int16_t *prm_resq;
    1027       50958 :             prm_resq = prm_sqQ + *prm_target /* = targetBits */ - hTcxDec->resQBits[frame_cnt];
    1028       50958 :             i = tcx_ari_res_invQ_spec( x, L_spec, prm_resq, hTcxDec->resQBits[frame_cnt], 0, hTcxCfg->sq_rounding, xn_buf /* LF deemphasis factors */ );
    1029             :         }
    1030             :         else
    1031             :         {
    1032             :             /* context based arithmetic coder */
    1033     1093812 :             i = tcx_res_invQ_gain( gain_tcx, &prm_sqQ[L_spec], hTcxDec->resQBits[frame_cnt] );
    1034     1093812 :             tcx_res_invQ_spec( x, L_spec, &prm_sqQ[L_spec], hTcxDec->resQBits[frame_cnt], i, hTcxCfg->sq_rounding, st->tcxonly ? NULL : xn_buf /* LF deemphasis factors */ );
    1035             :         }
    1036             :     }
    1037             : 
    1038     2653764 :     if ( !bfi && st->tcxonly && st->element_mode != IVAS_CPE_MDCT )
    1039             :     {
    1040      322011 :         if ( hTcxLtpDec->tcxltp && ( hTcxLtpDec->tcxltp_gain > 0.0f ) && !( *fUseTns ) )
    1041             :         {
    1042       10887 :             PsychAdaptLowFreqDeemph( x, gainlpc2, NULL );
    1043             :         }
    1044             :     }
    1045             : 
    1046     2653764 :     if ( !bfi && !st->tcxonly )
    1047             :     {
    1048             :         /* Replication of ACELP formant enhancement for low rates */
    1049      376356 :         if ( st->total_brate < ACELP_13k20 )
    1050             :         {
    1051       97050 :             if ( st->element_mode == IVAS_CPE_MDCT )
    1052             :             {
    1053           0 :                 assert( !"Not adapted to warped scale" );
    1054             :             }
    1055       97050 :             tcxFormantEnhancement( xn_buf, gainlpc2, x, L_frame );
    1056             :         }
    1057             :     }
    1058             : 
    1059             :     /*-----------------------------------------------------------*
    1060             :      * Add gain to the lpc gains                                 *
    1061             :      *-----------------------------------------------------------*/
    1062             : 
    1063     2653764 :     if ( st->VAD == 0 )
    1064             :     {
    1065     1801581 :         *gain_tcx *= hTcxCfg->na_scale;
    1066             :     }
    1067             : 
    1068     2653764 :     if ( st->element_mode != IVAS_CPE_MDCT )
    1069             :     {
    1070      707661 :         v_multc( gainlpc2, *gain_tcx, gainlpc2, FDNS_NPTS );
    1071             :     }
    1072             : 
    1073     2653764 :     return;
    1074             : }
    1075             : 
    1076             : 
    1077             : /*-------------------------------------------------------------------*
    1078             :  *  decoder_tcx_noisefilling()
    1079             :  *
    1080             :  *  TCX: core noise filling, IGF updates
    1081             :  *-------------------------------------------------------------------*/
    1082             : 
    1083     2653764 : void decoder_tcx_noisefilling(
    1084             :     Decoder_State *st, /* i/o: coder memory state          */
    1085             :     float concealment_noise[L_FRAME48k],
    1086             :     const float A[], /* i  :  coefficients NxAz[M+1]     */
    1087             :     const int16_t L_frameTCX_glob,
    1088             :     const int16_t L_spec,
    1089             :     const int16_t L_frame,
    1090             :     const int16_t L_frameTCX,
    1091             :     float x[],
    1092             :     float gainlpc2[],
    1093             :     int16_t *temp_concealment_method,
    1094             :     const float gain_tcx,
    1095             :     const int16_t *prm_sqQ,
    1096             :     int16_t nf_seed,
    1097             :     const int16_t bfi, /* i  :  Bad frame indicator            */
    1098             :     const int16_t MCT_flag,
    1099             :     const int16_t frame_cnt /* i  : frame counter in the super frame*/
    1100             : )
    1101             : {
    1102             :     int16_t i;
    1103             :     int16_t firstLine;
    1104             :     float fac_ns;
    1105             :     int16_t noiseFillingSize;
    1106     2653764 :     int16_t noiseTransWidth = MIN_NOISE_FILLING_HOLE;
    1107             :     float tmp2;
    1108             :     int32_t sum_word32;
    1109             :     int16_t infoIGFStartLine;
    1110             :     float f, noiseTiltFactor, smooth_gain;
    1111             :     float E_2ndlast, E_last;
    1112     2653764 :     TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
    1113     2653764 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
    1114     2653764 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1115     2653764 :     int32_t total_brate = ( st->element_mode == IVAS_CPE_MDCT ? st->element_brate : st->total_brate );
    1116             : 
    1117             :     /*-----------------------------------------------------------------*
    1118             :      * Initializations
    1119             :      *-----------------------------------------------------------------*/
    1120             : 
    1121             :     /* Init lengths */
    1122     2653764 :     infoIGFStartLine = get_igf_startline( st, L_frame, L_frameTCX );
    1123             : 
    1124     2653764 :     noiseFillingSize = L_spec;
    1125     2653764 :     if ( st->igf )
    1126             :     {
    1127     1814607 :         noiseFillingSize = st->hIGFDec->infoIGFStartLine;
    1128             :     }
    1129             : 
    1130     2653764 :     fac_ns = (float) hTcxDec->noise_filling_index[frame_cnt] * 0.75f / ( 1 << NBITS_NOISE_FILL_LEVEL );
    1131             : 
    1132             :     /*-----------------------------------------------------------*
    1133             :      * Noise filling.                                            *
    1134             :      *-----------------------------------------------------------*/
    1135             : 
    1136     2653764 :     if ( !bfi && ( fac_ns > 0.0f ) )
    1137             :     {
    1138     2490309 :         firstLine = tcxGetNoiseFillingTilt( A, L_frame, ( total_brate >= ACELP_13k20 && !st->rf_flag ), &noiseTiltFactor );
    1139             : 
    1140     2490309 :         if ( st->tcxonly )
    1141             :         {
    1142     2115129 :             if ( st->element_mode == IVAS_CPE_MDCT )
    1143             :             {
    1144     1846692 :                 if ( frame_cnt == 0 )
    1145             :                 {
    1146     1804923 :                     mvr2r( hTcxDec->ltpGainMemory, &hTcxDec->ltpGainMemory[1], N_LTP_GAIN_MEMS - 1 );
    1147     1804923 :                     hTcxDec->ltpGainMemory[0] = hTcxLtpDec->tcxltp_gain;
    1148             :                 }
    1149             : 
    1150     1846692 :                 smooth_gain = dotp( hTcxDec->ltpGainMemory, nf_tw_smoothing_coeffs, N_LTP_GAIN_MEMS );
    1151     1846692 :                 noiseTransWidth = HOLE_SIZE_FROM_LTP( max( smooth_gain, ( hTcxCfg->ctx_hm && st->last_core != ACELP_CORE ) ? 0.3125f * st->last_ctx_hm_enabled : 0 ) );
    1152             :             }
    1153             :             else
    1154             :             {
    1155      268437 :                 noiseTransWidth = HOLE_SIZE_FROM_LTP( max( hTcxLtpDec->tcxltp_gain, ( hTcxCfg->ctx_hm && st->last_core != ACELP_CORE ) ? 0.3125f * st->last_ctx_hm_enabled : 0 ) );
    1156             :             }
    1157             : 
    1158     2115129 :             if ( L_frame == st->L_frame >> 1 )
    1159             :             {
    1160       84369 :                 noiseTransWidth = 3; /* minimum transition fading for noise filling in TCX-10 */
    1161             :             }
    1162             :         }
    1163             : 
    1164     2490309 :         if ( !hTcxDec->tcx_lpc_shaped_ari )
    1165             :         {
    1166  1998247089 :             for ( i = 0; i < L_spec; i++ )
    1167             :             {
    1168  1995807120 :                 nf_seed += (int16_t) ( abs( prm_sqQ[i] ) * i * 2 );
    1169             :             }
    1170             :         }
    1171     2490309 :         tcx_noise_filling( x, nf_seed, firstLine, noiseFillingSize, noiseTransWidth, L_frame, noiseTiltFactor, fac_ns, st->igf ? st->hIGFDec->infoTCXNoise : NULL, st->element_mode );
    1172             : 
    1173     2490309 :         st->seed_tcx_plc = nf_seed;
    1174             :     }
    1175             : 
    1176     2653764 :     if ( st->enablePlcWaveadjust )
    1177             :     {
    1178           0 :         if ( bfi )
    1179             :         {
    1180           0 :             if ( st->nbLostCmpt == 1 )
    1181             :             {
    1182           0 :                 st->hPlcInfo->concealment_method = TCX_NONTONAL;
    1183             : 
    1184             :                 /* tonal/non-tonal decision */
    1185           0 :                 if ( st->hPlcInfo->Transient[0] == 1 && st->hPlcInfo->Transient[1] == 1 && st->hPlcInfo->Transient[2] == 1 )
    1186             :                 {
    1187           0 :                     sum_word32 = 0;
    1188             : 
    1189           0 :                     for ( i = 9; i >= 0; i-- )
    1190             :                     {
    1191           0 :                         sum_word32 += st->hPlcInfo->TCX_Tonality[i];
    1192             :                     }
    1193             : 
    1194           0 :                     if ( sum_word32 >= 6 )
    1195             :                     {
    1196           0 :                         st->hPlcInfo->concealment_method = TCX_TONAL;
    1197             :                     }
    1198             :                 }
    1199             : 
    1200           0 :                 if ( st->tonal_mdct_plc_active )
    1201             :                 {
    1202           0 :                     st->hPlcInfo->concealment_method = TCX_TONAL;
    1203             :                 }
    1204             :             }
    1205             : 
    1206           0 :             if ( L_frameTCX > hTcxDec->L_frameTCX )
    1207             :             {
    1208           0 :                 st->hPlcInfo->concealment_method = TCX_TONAL;
    1209             :             }
    1210             : 
    1211           0 :             *temp_concealment_method = st->hPlcInfo->concealment_method;
    1212             : 
    1213           0 :             if ( st->core == TCX_10_CORE )
    1214             :             {
    1215           0 :                 *temp_concealment_method = TCX_TONAL;
    1216             :             }
    1217             :         }
    1218             : 
    1219             :         /* get the starting location of the subframe in the frame */
    1220           0 :         if ( st->core == TCX_10_CORE )
    1221             :         {
    1222           0 :             st->hPlcInfo->subframe = frame_cnt * L_frameTCX_glob;
    1223             :         }
    1224             :     }
    1225             : 
    1226             :     /* PLC: [TCX: Tonal Concealment] */
    1227             :     /* PLC: [TCX: Fade-out]
    1228             :      * PLC: Fade out to white noise */
    1229     2653764 :     if ( st->hTonalMDCTConc != NULL )
    1230             :     {
    1231     2653764 :         if ( !bfi && st->element_mode != IVAS_CPE_MDCT )
    1232             :         {
    1233      698367 :             TonalMDCTConceal_SaveFreqSignal( st->hTonalMDCTConc, x, L_frameTCX, L_frame, gainlpc2, infoIGFStartLine );
    1234             :         }
    1235     1955397 :         else if ( bfi )
    1236             :         {
    1237             : 
    1238       34524 :             if ( !st->enablePlcWaveadjust || ( *temp_concealment_method == TCX_TONAL ) )
    1239             :             {
    1240             :                 /* set f to 1 to not fade out */
    1241             :                 /* set f to 0 to immediately switch to white noise */
    1242       34524 :                 if ( st->tcxonly && ( st->element_mode != IVAS_CPE_MDCT || MCT_flag ) )
    1243             :                 {
    1244       17733 :                     f = 1.0f;
    1245             :                 }
    1246             :                 else
    1247             :                 {
    1248       16791 :                     f = hTcxDec->cummulative_damping_tcx;
    1249             :                 }
    1250             : 
    1251       34524 :                 if ( ( frame_cnt == 0 ) && ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && ( st->tcxonly ) && ( !st->tonal_mdct_plc_active ) && ( st->nbLostCmpt == 1 ) && ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) && ( hTcxCfg->tcx_curr_overlap_mode != FULL_OVERLAP ) )
    1252             :                 {
    1253         156 :                     E_2ndlast = E_last = EPSILON;
    1254         156 :                     if ( st->element_mode > EVS_MONO )
    1255             :                     {
    1256       25356 :                         for ( i = 0; i < L_frameTCX; i = i + 2 )
    1257             :                         {
    1258       25200 :                             E_2ndlast += st->hTonalMDCTConc->lastBlockData.spectralData[i] * st->hTonalMDCTConc->lastBlockData.spectralData[i];
    1259       25200 :                             E_last += st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] * st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
    1260             :                         }
    1261             :                     }
    1262             :                     else
    1263             :                     {
    1264           0 :                         for ( i = 0; i < infoIGFStartLine; i = i + 2 )
    1265             :                         {
    1266           0 :                             E_2ndlast += st->hTonalMDCTConc->lastBlockData.spectralData[i] * st->hTonalMDCTConc->lastBlockData.spectralData[i];
    1267           0 :                             E_last += st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] * st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
    1268             :                         }
    1269             :                     }
    1270         156 :                     tmp2 = E_2ndlast / E_last;
    1271             : 
    1272             :                     /* replace higher energy TCX5 frame by lower one to avoid energy fluctuation */
    1273         156 :                     if ( tmp2 > 2 )
    1274             :                     {
    1275          15 :                         if ( st->element_mode > EVS_MONO )
    1276             :                         {
    1277        3615 :                             for ( i = 0; i < L_frameTCX; i = i + 2 )
    1278             :                             {
    1279        3600 :                                 st->hTonalMDCTConc->lastBlockData.spectralData[i] = st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
    1280             :                             }
    1281             :                         }
    1282             :                         else
    1283             :                         {
    1284           0 :                             for ( i = 0; i < infoIGFStartLine; i = i + 2 )
    1285             :                             {
    1286           0 :                                 st->hTonalMDCTConc->lastBlockData.spectralData[i] = st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
    1287             :                             }
    1288             :                         }
    1289             :                     }
    1290         141 :                     else if ( tmp2 < 0.5 )
    1291             :                     {
    1292          66 :                         if ( st->element_mode > EVS_MONO )
    1293             :                         {
    1294       10146 :                             for ( i = 0; i < L_frameTCX; i = i + 2 )
    1295             :                             {
    1296       10080 :                                 st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] = st->hTonalMDCTConc->lastBlockData.spectralData[i];
    1297             :                             }
    1298             :                         }
    1299             :                         else
    1300             :                         {
    1301           0 :                             for ( i = 0; i < infoIGFStartLine; i = i + 2 )
    1302             :                             {
    1303           0 :                                 st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] = st->hTonalMDCTConc->lastBlockData.spectralData[i];
    1304             :                             }
    1305             :                         }
    1306             :                     }
    1307             :                 }
    1308             : 
    1309       34524 :                 noiseTiltFactor = 1.0f;
    1310       34524 :                 tcxGetNoiseFillingTilt( A, L_frame, ( total_brate >= ACELP_13k20 && !st->rf_flag ), &noiseTiltFactor );
    1311             : 
    1312       34524 :                 if ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag )
    1313             :                 {
    1314       11073 :                     TonalMDCTConceal_InsertNoise( st->hTonalMDCTConc, x, st->tonal_mdct_plc_active, &st->seed_tcx_plc, noiseTiltFactor, f, concealment_noise, hTcxDec->CngLevelBackgroundTrace_bfi, infoIGFStartLine );
    1315             :                 }
    1316             :                 else
    1317             :                 {
    1318       23451 :                     TonalMDCTConceal_InsertNoise( st->hTonalMDCTConc, x, st->tonal_mdct_plc_active, &st->seed_tcx_plc, noiseTiltFactor, f, NULL, hTcxDec->CngLevelBackgroundTrace_bfi, infoIGFStartLine );
    1319             :                 }
    1320             :             }
    1321             :         }
    1322             :     }
    1323             : 
    1324             :     /*------------------------- SPLIT 2 noise filling ------------------------*/
    1325     2653764 :     if ( !bfi )
    1326             :     {
    1327     2619240 :         if ( st->element_mode == IVAS_CPE_MDCT )
    1328             :         {
    1329     1920873 :             v_multc( x, gain_tcx, x, L_spec );
    1330             :         }
    1331             :     }
    1332             : 
    1333     2653764 :     if ( L_spec < L_frame )
    1334             :     {
    1335           0 :         set_zero( x + L_spec, L_frame - L_spec );
    1336             :     }
    1337     2653764 :     else if ( L_spec > L_frameTCX )
    1338             :     {
    1339      208038 :         set_zero( x + L_frameTCX, L_spec - L_frameTCX );
    1340             :     }
    1341             : 
    1342     2653764 :     if ( bfi && ( !st->enablePlcWaveadjust || ( *temp_concealment_method == TCX_TONAL ) ) && st->igf && ( frame_cnt == 0 ) && ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && ( st->tcxonly ) && ( !st->tonal_mdct_plc_active ) && ( st->nbLostCmpt == 1 ) && ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) && ( hTcxCfg->tcx_curr_overlap_mode != FULL_OVERLAP ) )
    1343             :     {
    1344          75 :         IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, IGF_GRID_LB_SHORT );
    1345             :         /* also replace flat spectrum for the second TCX10 sub frame */
    1346          75 :         IGFDecUpdateInfo( st->hIGFDec, 1, IGF_GRID_LB_SHORT );
    1347          75 :         IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, IGF_GRID_LB_SHORT );
    1348          75 :         IGFDecUpdateInfo( st->hIGFDec, 0, IGF_GRID_LB_SHORT );
    1349          75 :         mvs2s( st->hIGFDec->igfData.igf_curr_subframe[0][0], st->hIGFDec->igfData.igf_curr_subframe[1][0], IGF_MAX_SFB );
    1350             :     }
    1351     2653689 :     else if ( bfi && st->igf && ( frame_cnt == 0 ) && ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) )
    1352             :     {
    1353             :         /* copy second to first subframe */
    1354          75 :         IGFDecReplicateTCX10State( st->hIGFDec );
    1355             :     }
    1356             : 
    1357     2653764 :     if ( st->element_mode != EVS_MONO )
    1358             :     {
    1359     2651826 :         if ( bfi )
    1360             :         {
    1361       34524 :             nf_seed = st->seed_tcx_plc;
    1362             :         }
    1363     2617302 :         else if ( nf_seed == 0 )
    1364             :         {
    1365      134637 :             nf_seed = *st->hIGFDec->igfData.igfInfo.nfSeed;
    1366             :         }
    1367             :     }
    1368             : 
    1369     2653764 :     if ( st->igf )
    1370             :     {
    1371     1814607 :         *st->hIGFDec->igfData.igfInfo.nfSeed = (int16_t) ( nf_seed * 31821L + 13849L );
    1372             :     }
    1373             : 
    1374     2653764 :     return;
    1375             : }
    1376             : 
    1377             : 
    1378             : /*-------------------------------------------------------------------*
    1379             :  * decoder_tcx_noiseshaping_igf()
    1380             :  *
    1381             :  * TCX: FDNS and IGF application
    1382             :  *-------------------------------------------------------------------*/
    1383             : 
    1384     2653764 : void decoder_tcx_noiseshaping_igf(
    1385             :     Decoder_State *st, /* i/o: coder memory state          */
    1386             :     const int16_t L_spec,
    1387             :     const int16_t L_frame,
    1388             :     const int16_t L_frameTCX,
    1389             :     const int16_t left_rect,
    1390             :     float x[],
    1391             :     const float gainlpc2[],
    1392             :     int16_t *temp_concealment_method,
    1393             :     const int16_t bfi /* i  : Bad frame indicator                 */
    1394             : )
    1395             : {
    1396     2653764 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
    1397             : 
    1398             :     /*-----------------------------------------------------------*
    1399             :      * Noise shaping in frequency domain (1/Wz)                  *
    1400             :      *-----------------------------------------------------------*/
    1401             : 
    1402     2653764 :     if ( st->igf && ( !bfi || ( st->element_mode == IVAS_CPE_MDCT && st->prev_bfi ) ) )
    1403             :     {
    1404     1802649 :         if ( ( L_frame == st->L_frame >> 1 ) && ( st->tcxonly ) )
    1405             :         {
    1406       69504 :             IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, IGF_GRID_LB_SHORT );
    1407             :         }
    1408             :         else
    1409             :         {
    1410     1733145 :             IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM );
    1411             :         }
    1412             :     }
    1413             :     /* LPC gains already available */
    1414             : 
    1415     2653764 :     if ( st->element_mode != IVAS_CPE_MDCT && ( !st->enablePlcWaveadjust || !bfi || ( *temp_concealment_method == TCX_TONAL ) ) )
    1416             :     {
    1417      707661 :         mdct_noiseShaping( x, L_frame, gainlpc2, FDNS_NPTS );
    1418             : 
    1419      707661 :         if ( !bfi )
    1420             :         {
    1421      698367 :             v_multc( x + L_frame, gainlpc2[FDNS_NPTS - 1], x + L_frame, L_spec - L_frame );
    1422             :         }
    1423             : 
    1424      707661 :         set_zero( x + L_spec, L_frameTCX - L_spec );
    1425             :     }
    1426             : 
    1427             :     /* PLC: [TCX: Tonal Concealment] */
    1428     2653764 :     if ( bfi && st->tonal_mdct_plc_active && st->element_mode != IVAS_CPE_MDCT )
    1429             :     {
    1430         834 :         TonalMDCTConceal_Apply( st->hTonalMDCTConc, x, st->hTcxCfg->psychParamsCurrent );
    1431             :     }
    1432             : 
    1433     2653764 :     if ( st->hTonalMDCTConc != NULL && st->element_mode != IVAS_CPE_MDCT )
    1434             :     {
    1435      707661 :         TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, L_frameTCX, ( hTcxDec->tcxltp_last_gain_unmodified > 0 ) ? st->old_fpitch : 0, bfi, bfi && st->tonal_mdct_plc_active );
    1436             :     }
    1437             : 
    1438     2653764 :     if ( st->enablePlcWaveadjust )
    1439             :     {
    1440             :         /* spectrum concealment */
    1441           0 :         if ( bfi && *temp_concealment_method == TCX_NONTONAL )
    1442             :         {
    1443           0 :             concealment_decode( st->core, x, st->hPlcInfo );
    1444             :         }
    1445             : 
    1446             :         /* update spectrum buffer, tonality flag, etc. */
    1447           0 :         concealment_update( bfi, st->core, st->tonality_flag, x, st->hPlcInfo );
    1448             :     }
    1449             : 
    1450             :     /*-----------------------------------------------------------*
    1451             :      * IGF                                                       *
    1452             :      *-----------------------------------------------------------*/
    1453             : 
    1454     2653764 :     if ( st->igf && !( ( L_frame == st->L_frame >> 1 ) && st->tcxonly ) )
    1455             :     {
    1456     1744815 :         if ( st->element_mode != IVAS_CPE_MDCT )
    1457             :         {
    1458      665154 :             IGFDecApplyMono( st->hIGFDec, x, ( st->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, bfi, st->element_mode );
    1459             :         }
    1460             :     }
    1461             : 
    1462     2653764 :     if ( st->igf && ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly ) )
    1463             :     {
    1464       69792 :         if ( st->element_mode != IVAS_CPE_MDCT )
    1465             :         {
    1466       12036 :             IGFDecApplyMono( st->hIGFDec, x, IGF_GRID_LB_SHORT, bfi, st->element_mode );
    1467             :         }
    1468             :     }
    1469             : 
    1470     2653764 :     return;
    1471             : }
    1472             : 
    1473             : 
    1474             : /*-------------------------------------------------------------------*
    1475             :  *  decoder_tcx_tns()
    1476             :  *
    1477             :  *  TCX: TNS application
    1478             :  *-------------------------------------------------------------------*/
    1479             : 
    1480     4577397 : void decoder_tcx_tns(
    1481             :     Decoder_State *st,          /* i/o: coder memory state                      */
    1482             :     const int16_t L_frame_glob, /* i  : frame length                            */
    1483             :     const int16_t L_spec,
    1484             :     const int16_t L_frame,
    1485             :     const int16_t L_frameTCX,
    1486             :     float x[N_MAX],
    1487             :     const int16_t fUseTns, /* i  : flag that is set if TNS data is present */
    1488             :     STnsData *tnsData,
    1489             :     const int16_t bfi,       /* i  : Bad frame indicator                     */
    1490             :     const int16_t frame_cnt, /* i  : frame counter in the super frame        */
    1491             :     const int16_t whitenedDomain )
    1492             : {
    1493             :     int16_t index, isTCX5, L;
    1494     4577397 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1495             : 
    1496     4577397 :     index = hTcxCfg->tcx_last_overlap_mode; /* backup last TCX overlap mode */
    1497             : 
    1498     4577397 :     isTCX5 = 0;
    1499     4577397 :     L = L_frameTCX;
    1500             : 
    1501     4577397 :     if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly )
    1502             :     {
    1503      186936 :         if ( frame_cnt && !bfi && st->last_core != ACELP_CORE )
    1504             :         {
    1505             :             /* fix sub-window overlap */
    1506       92607 :             hTcxCfg->tcx_last_overlap_mode = hTcxCfg->tcx_curr_overlap_mode;
    1507             :         }
    1508             : 
    1509      186936 :         if ( ( hTcxCfg->fIsTNSAllowed && fUseTns != 0 && bfi != 1 ) || ( L_spec > L_frameTCX ) )
    1510             :         {
    1511      110367 :             L = L_spec;
    1512             :         }
    1513             : 
    1514      186936 :         if ( ( ( !bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) ||
    1515      135411 :                              ( ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( frame_cnt == 0 ) && ( index == 0 ) ) ) ) ||
    1516        1722 :              ( ( bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) &&
    1517         630 :                             !( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) ) ) )
    1518             :         {
    1519       96651 :             isTCX5 = 1;
    1520             : 
    1521       96651 :             tcx5SpectrumDeinterleaving( L >> 1, x );
    1522             :         }
    1523             :     }
    1524             : 
    1525             : #ifdef DEBUG_PLOT
    1526             :     if ( !whitenedDomain )
    1527             :         sendDebout( "tnsSpec2", L_frameTCX, 1, "aftInvertGrouping", MTV_FLOAT, x );
    1528             : #endif
    1529             : 
    1530             :     /*-----------------------------------------------------------*
    1531             :      * Temporal Noise Shaping Synthesis                          *
    1532             :      *-----------------------------------------------------------*/
    1533             : 
    1534     4577397 :     if ( hTcxCfg->fIsTNSAllowed && fUseTns != 0 && bfi != 1 && tnsData->tnsOnWhitenedSpectra == whitenedDomain )
    1535             :     {
    1536             :         /* Apply TNS to get the reconstructed signal */
    1537      267114 :         SetTnsConfig( hTcxCfg, L_frame_glob == st->L_frame, ( st->last_core == ACELP_CORE ) && ( frame_cnt == 0 ) );
    1538             : 
    1539      267114 :         if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && isTCX5 )
    1540             :         {
    1541       39408 :             tcx5TnsGrouping( L >> 1, hTcxCfg->tnsConfig[0][0].iFilterBorders[0] >> 1, x );
    1542             :         }
    1543             : 
    1544      267114 :         ApplyTnsFilter( hTcxCfg->pCurrentTnsConfig, tnsData, x, 0 );
    1545             : #ifdef DEBUG_PLOT
    1546             :         sendDebout( "tnsSpec2", L_frameTCX, 1, "aftTNS", MTV_FLOAT, x );
    1547             : #endif
    1548             : 
    1549      267114 :         if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && isTCX5 )
    1550             :         {
    1551       39408 :             if ( st->element_mode == EVS_MONO || L_spec < L_frameTCX ) /* todo: this is temporary to maintain EVS BE, this is a bug and should be fixed also for EVS (see issue 13) */
    1552             :             {
    1553        3534 :                 tcx5TnsUngrouping( L_frameTCX >> 1, hTcxCfg->tnsConfig[0][0].iFilterBorders[0] >> 1, x, DEC );
    1554             :             }
    1555             :             else
    1556             :             {
    1557       35874 :                 tcx5TnsUngrouping( L >> 1, hTcxCfg->tnsConfig[0][0].iFilterBorders[0] >> 1, x, DEC );
    1558             :             }
    1559             :         }
    1560             :     }
    1561             : 
    1562     4577397 :     if ( whitenedDomain && isTCX5 )
    1563             :     {
    1564       44976 :         tcx5SpectrumInterleaving( L >> 1, x );
    1565             :     }
    1566             : 
    1567             : #ifdef DEBUG_PLOT
    1568             :     if ( !whitenedDomain )
    1569             :         sendDebout( "tnsSpec1", L_frameTCX, 1, "aftTNS", MTV_FLOAT, x );
    1570             : #endif
    1571             :     /* restore index */
    1572     4577397 :     if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && frame_cnt && !bfi && st->last_core != ACELP_CORE )
    1573             :     {
    1574             :         /* restore sub-window overlap */
    1575       92607 :         hTcxCfg->tcx_last_overlap_mode = index;
    1576             :     }
    1577             : 
    1578     4577397 :     return;
    1579             : }
    1580             : 
    1581             : 
    1582             : /*-------------------------------------------------------------------*
    1583             :  *  decoder_tcx_imdct()
    1584             :  *
    1585             :  *  TCX: ITF and IMDCT
    1586             :  *-------------------------------------------------------------------*/
    1587             : 
    1588     2642529 : void decoder_tcx_imdct(
    1589             :     Decoder_State *st,          /* i/o: coder memory state                      */
    1590             :     const int16_t L_frame_glob, /* i  : frame length                            */
    1591             :     const int16_t L_frameTCX_glob,
    1592             :     const int16_t L_spec,
    1593             :     const int16_t tcx_offset,
    1594             :     const int16_t tcx_offsetFB,
    1595             :     const int16_t L_frame,
    1596             :     const int16_t L_frameTCX,
    1597             :     const int16_t left_rect,
    1598             :     float x[N_MAX],
    1599             :     float xn_buf[],
    1600             :     const uint16_t kernelType, /* i  : TCX transform kernel type               */
    1601             :     const int16_t fUseTns,     /* i  : flag that is set if TNS data is present */
    1602             :     float synth[],             /* i/o: synth[-M..L_frame]                      */
    1603             :     float synthFB[],
    1604             :     const int16_t bfi,                  /* i  :  Bad frame indicator                    */
    1605             :     const int16_t frame_cnt,            /* i  : frame counter in the super frame        */
    1606             :     const int16_t sba_dirac_stereo_flag /* i  : signal stereo output for SBA DirAC      */
    1607             : )
    1608             : {
    1609             :     int16_t j, L, overlap, curr_order, startLine, endLine, isTCX5;
    1610             :     int16_t overlapFB;
    1611             :     float x_tmp[L_FRAME_PLUS];
    1612             :     float xn_bufFB[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
    1613             :     float acelp_zir[L_FRAME_MAX / 2];
    1614             :     float x_itf[N_MAX_TCX - IGF_START_MN];
    1615             :     float A_itf[ITF_MAX_FILTER_ORDER + 1];
    1616             :     float predictionGain;
    1617             :     int16_t index, proc;
    1618     2642529 :     TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
    1619     2642529 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
    1620     2642529 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1621             : 
    1622             :     /*-----------------------------------------------------------------*
    1623             :      * Initializations
    1624             :      *-----------------------------------------------------------------*/
    1625             : 
    1626             :     /* Init lengths */
    1627     2642529 :     overlap = hTcxCfg->tcx_mdct_window_length;
    1628     2642529 :     overlapFB = hTcxCfg->tcx_mdct_window_lengthFB;
    1629             : 
    1630     2642529 :     index = hTcxCfg->tcx_last_overlap_mode; /* backup last TCX overlap mode */
    1631     2642529 :     if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && frame_cnt && !bfi && st->last_core != ACELP_CORE )
    1632             :     {
    1633             :         /* fix sub-window overlap */
    1634       49359 :         hTcxCfg->tcx_last_overlap_mode = hTcxCfg->tcx_curr_overlap_mode;
    1635             :     }
    1636             : 
    1637     2642529 :     if ( st->igf )
    1638             :     {
    1639     1810467 :         proc = st->hIGFDec->flatteningTrigger;
    1640             : 
    1641     1810467 :         if ( proc && fUseTns != 0 )
    1642             :         {
    1643      179613 :             proc = 0;
    1644             :         }
    1645             : 
    1646     1810467 :         if ( proc )
    1647             :         {
    1648     1565487 :             startLine = st->hIGFDec->infoIGFStartLine;
    1649     1565487 :             endLine = st->hIGFDec->infoIGFStopLine;
    1650     1565487 :             curr_order = 0;
    1651     1565487 :             predictionGain = 0;
    1652     1565487 :             L = L_frameTCX;
    1653     1565487 :             isTCX5 = 0;
    1654             : 
    1655             :             /* interleave again for ITF */
    1656     1565487 :             if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly )
    1657             :             {
    1658       28926 :                 if ( ( hTcxCfg->fIsTNSAllowed && fUseTns != 0 && bfi != 1 ) || ( L_spec > L_frameTCX ) )
    1659             :                 {
    1660        4188 :                     L = L_spec;
    1661             :                 }
    1662             : 
    1663       28926 :                 if ( ( ( !bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) ||
    1664       23940 :                                      ( ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( frame_cnt == 0 ) && ( index == 0 ) ) ) ) ||
    1665         297 :                      ( ( bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) &&
    1666         174 :                                     !( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) ) ) )
    1667             :                 {
    1668        6933 :                     isTCX5 = 1;
    1669             : 
    1670        6933 :                     tcx5SpectrumInterleaving( L >> 1, x );
    1671             :                 }
    1672             :             }
    1673             : 
    1674   460498287 :             for ( j = startLine; j < endLine; j++ )
    1675             :             {
    1676   458932800 :                 if ( st->hIGFDec->flag_sparse[j - IGF_START_MN] == 2 )
    1677             :                 {
    1678       55248 :                     x_itf[j - IGF_START_MN] = x[j];
    1679       55248 :                     x[j] = st->hIGFDec->virtualSpec[j - IGF_START_MN];
    1680             :                 }
    1681             :             }
    1682             : 
    1683     1565487 :             ITF_Detect( x + IGF_START_MN, startLine, endLine, 8 /*maxOrder*/, A_itf, &predictionGain, &curr_order );
    1684             : 
    1685     1565487 :             ITF_Apply( x, startLine, endLine, A_itf, curr_order );
    1686             : 
    1687   460498287 :             for ( j = startLine; j < endLine; j++ )
    1688             :             {
    1689   458932800 :                 if ( st->hIGFDec->flag_sparse[j - IGF_START_MN] == 2 )
    1690             :                 {
    1691       55248 :                     x[j] = x_itf[j - IGF_START_MN];
    1692             :                 }
    1693             :             }
    1694             : 
    1695             :             /* deinterleave */
    1696     1565487 :             if ( isTCX5 )
    1697             :             {
    1698        6933 :                 tcx5SpectrumDeinterleaving( L >> 1, x );
    1699             :             }
    1700             :         }
    1701             :     }
    1702             : 
    1703             :     /*-----------------------------------------------------------*
    1704             :      * Prepare OLA buffer after waveadjustment.                  *
    1705             :      * Compute inverse MDCT of x[].                              *
    1706             :      *-----------------------------------------------------------*/
    1707             : 
    1708             : #ifdef DEBUG_PLOT
    1709             :     sendDebout( "tnsSpec", L_frameTCX, 1, "befIMDCT", MTV_FLOAT, x );
    1710             : #endif
    1711             : 
    1712     2642529 :     if ( st->element_mode == IVAS_CPE_MDCT )
    1713             :     {
    1714     1934868 :         set_f( x_tmp, 0.0f, L_FRAME_PLUS );
    1715     1934868 :         mvr2r( x, x_tmp, min( L_FRAME48k, max( L_spec, max( L_frame, L_frameTCX ) ) ) );
    1716     1934868 :         mvr2r( x, xn_bufFB, min( L_FRAME48k, max( L_spec, max( L_frame, L_frameTCX ) ) ) );
    1717             :     }
    1718      707661 :     else if ( st->element_mode == EVS_MONO )
    1719             :     {
    1720        1938 :         mvr2r( x, xn_bufFB, max( L_spec, max( L_frame, L_frameTCX ) ) );
    1721             :     }
    1722             :     else
    1723             :     {
    1724      705723 :         mvr2r( x, x_tmp, max( L_spec, max( L_frame, L_frameTCX ) ) );
    1725      705723 :         mvr2r( x, xn_bufFB, max( L_spec, max( L_frame, L_frameTCX ) ) );
    1726             :     }
    1727             : 
    1728     2642529 :     if ( st->igf )
    1729             :     {
    1730     1810467 :         set_zero( xn_bufFB + st->hIGFDec->infoIGFStartLine, L_frameTCX - st->hIGFDec->infoIGFStartLine );
    1731             :     }
    1732             : 
    1733     2642529 :     if ( st->element_mode != IVAS_CPE_DFT && !sba_dirac_stereo_flag )
    1734             :     {
    1735     2592576 :         IMDCT( xn_bufFB, hTcxDec->syn_Overl, hTcxDec->syn_Overl_TDAC, xn_buf, hTcxCfg->tcx_aldo_window_1_trunc, hTcxCfg->tcx_aldo_window_2,
    1736     2592576 :                hTcxCfg->tcx_mdct_window_half, hTcxCfg->tcx_mdct_window_minimum, hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_half_length, hTcxCfg->tcx_mdct_window_min_length, index,
    1737     2592576 :                kernelType, left_rect, tcx_offset, overlap, L_frame, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frame_glob, frame_cnt, bfi, st->hHQ_core->old_outLB, 0, st, 0, acelp_zir );
    1738             :     }
    1739             : 
    1740             :     /* Generate additional comfort noise to mask potential coding artefacts */
    1741     2642529 :     if ( st->flag_cna && st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT && !st->cna_dirac_flag )
    1742             :     {
    1743       77970 :         generate_masking_noise_mdct( x, st->hFdCngDec->hFdCngCom );
    1744             :     }
    1745             : 
    1746     2642529 :     if ( st->element_mode == IVAS_CPE_DFT || sba_dirac_stereo_flag )
    1747             :     {
    1748       49953 :         mvr2r( x, xn_bufFB, max( L_spec, max( L_frame, L_frameTCX ) ) );
    1749             : 
    1750       49953 :         IMDCT( xn_bufFB, hTcxDec->syn_Overl, hTcxDec->syn_Overl_TDAC, xn_buf, hTcxCfg->tcx_aldo_window_1_trunc, hTcxCfg->tcx_aldo_window_2, hTcxCfg->tcx_mdct_window_half, hTcxCfg->tcx_mdct_window_minimum, hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_half_length, hTcxCfg->tcx_mdct_window_min_length, index,
    1751       49953 :                kernelType, left_rect, tcx_offset, overlap, L_frame, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frame_glob, frame_cnt, bfi, st->hHQ_core->old_outLB, 0, st, 0, acelp_zir );
    1752             :     }
    1753             : 
    1754     2642529 :     if ( st->element_mode != EVS_MONO )
    1755             :     {
    1756     2640591 :         IMDCT( x_tmp, hTcxDec->syn_OverlFB, hTcxDec->syn_Overl_TDACFB, xn_bufFB, hTcxCfg->tcx_aldo_window_1_FB_trunc, hTcxCfg->tcx_aldo_window_2_FB,
    1757     2640591 :                hTcxCfg->tcx_mdct_window_halfFB, hTcxCfg->tcx_mdct_window_minimumFB, hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_half_lengthFB, hTcxCfg->tcx_mdct_window_min_lengthFB, index,
    1758     2640591 :                kernelType, left_rect, tcx_offsetFB, overlapFB, L_frameTCX, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frameTCX_glob, frame_cnt, bfi, st->hHQ_core->old_out, 1, st, FSCALE_DENOM * L_frameTCX_glob / L_frame_glob, acelp_zir );
    1759             :     }
    1760             :     else
    1761             :     {
    1762        1938 :         IMDCT( x, hTcxDec->syn_OverlFB, hTcxDec->syn_Overl_TDACFB, xn_bufFB, hTcxCfg->tcx_aldo_window_1_FB_trunc, hTcxCfg->tcx_aldo_window_2_FB, hTcxCfg->tcx_mdct_window_halfFB, hTcxCfg->tcx_mdct_window_minimumFB, hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_half_lengthFB, hTcxCfg->tcx_mdct_window_min_lengthFB, index,
    1763        1938 :                kernelType, left_rect, tcx_offsetFB, overlapFB, L_frameTCX, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frameTCX_glob, frame_cnt, bfi, st->hHQ_core->old_out, 1, st, FSCALE_DENOM * L_frameTCX_glob / L_frame_glob, acelp_zir );
    1764             :     }
    1765             : 
    1766             :     /* PLC: [TCX: Tonal Concealment] */
    1767     2642529 :     if ( !bfi )
    1768             :     {
    1769     2608005 :         st->second_last_tns_active = st->last_tns_active;
    1770     2608005 :         st->last_tns_active = hTcxCfg->fIsTNSAllowed & fUseTns;
    1771     2608005 :         hTcxDec->tcxltp_third_last_pitch = hTcxDec->tcxltp_second_last_pitch;
    1772     2608005 :         hTcxDec->tcxltp_second_last_pitch = st->old_fpitch;
    1773     2608005 :         st->old_fpitch = hTcxLtpDec->tcxltp_pitch_int + hTcxLtpDec->tcxltp_pitch_fr / (float) st->pit_res_max;
    1774             : 
    1775     2608005 :         if ( st->element_mode == IVAS_CPE_MDCT )
    1776             :         {
    1777     1909638 :             st->old_fpitch *= (float) L_frame_glob / (float) L_FRAME;
    1778             :         }
    1779             : 
    1780     2608005 :         if ( st->element_mode > EVS_MONO )
    1781             :         {
    1782     2606067 :             st->old_fpitchFB = st->old_fpitch * (float) L_frameTCX_glob / (float) L_frame_glob;
    1783             :         }
    1784             :         else
    1785             :         {
    1786        1938 :             st->old_fpitchFB = st->old_fpitch * (float) L_frameTCX / (float) L_frame;
    1787             :         }
    1788             :     }
    1789             : 
    1790             :     /* Update old_syn_overl */
    1791     2642529 :     if ( !hTcxCfg->last_aldo )
    1792             :     {
    1793       67464 :         mvr2r( xn_buf + L_frame, hTcxDec->syn_Overl, overlap );
    1794       67464 :         mvr2r( xn_bufFB + L_frameTCX, hTcxDec->syn_OverlFB, overlapFB );
    1795             :     }
    1796             : 
    1797             :     /* Output */
    1798     2642529 :     mvr2r( xn_buf + ( overlap >> 1 ) - tcx_offset, synth, L_frame_glob );
    1799     2642529 :     mvr2r( xn_bufFB + ( overlapFB >> 1 ) - tcx_offsetFB, synthFB, L_frameTCX_glob );
    1800             : 
    1801     2642529 :     return;
    1802             : }
    1803             : 
    1804             : 
    1805             : /*-------------------------------------------------------------------*
    1806             :  *  init_tcx_info()
    1807             :  *
    1808             :  *  Initialize lengths for TCX processing, update IGF subframe info
    1809             :  *-------------------------------------------------------------------*/
    1810             : 
    1811     7798755 : void init_tcx_info(
    1812             :     Decoder_State *st,             /* i/o: coder memory state                                               */
    1813             :     const int16_t L_frame_glob,    /* i  : global frame length                                              */
    1814             :     const int16_t L_frameTCX_glob, /* i  : FB global frame length                                           */
    1815             :     const int16_t frame_cnt,       /* i  : frame counter in the super_frame                                 */
    1816             :     const int16_t bfi,             /* i  : bad frame indicator                                              */
    1817             :     int16_t *tcx_offset,           /* o  : folding point offset relative to the end of the previous frame   */
    1818             :     int16_t *tcx_offsetFB,         /* o  : FB folding point offset relative to the end of the previous frame*/
    1819             :     int16_t *L_frame,              /* o  : frame length                                                     */
    1820             :     int16_t *L_frameTCX,           /* o  : TCX frame length                                                 */
    1821             :     int16_t *left_rect,            /* o  : left part is rectangular                                         */
    1822             :     int16_t *L_spec                /* o  : spectrum length                                                  */
    1823             : )
    1824             : {
    1825     7798755 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
    1826     7798755 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1827             : 
    1828             :     /* Init lengths */
    1829     7798755 :     *tcx_offset = hTcxCfg->tcx_offset;
    1830     7798755 :     *tcx_offsetFB = hTcxCfg->tcx_offsetFB;
    1831             : 
    1832     7798755 :     if ( bfi )
    1833             :     {
    1834             :         /* PLC: [TCX: Memory update]
    1835             :          * PLC: Init buffers */
    1836             : 
    1837      102798 :         assert( st->L_frame_past > 0 );
    1838      102798 :         *L_frame = st->L_frame_past;
    1839      102798 :         *L_frameTCX = st->L_frameTCX_past;
    1840             : 
    1841      102798 :         *left_rect = hTcxDec->prev_widow_left_rect;
    1842             : 
    1843      102798 :         if ( *left_rect )
    1844             :         {
    1845        1434 :             *tcx_offset = hTcxCfg->lfacNext;
    1846        1434 :             *tcx_offsetFB = hTcxCfg->lfacNextFB;
    1847        1434 :             *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
    1848             :         }
    1849             :     }
    1850             :     else
    1851             :     {
    1852     7695957 :         if ( frame_cnt == 0 && st->last_core == ACELP_CORE )
    1853             :         {
    1854       40251 :             if ( !st->prev_bfi )
    1855             :             {
    1856       39303 :                 hTcxCfg->last_aldo = 0;
    1857             :             }
    1858             : 
    1859             :             /* if past frame is ACELP */
    1860       40251 :             *L_frame = L_frame_glob + *tcx_offset;
    1861       40251 :             *L_frameTCX = L_frameTCX_glob + *tcx_offsetFB;
    1862       40251 :             if ( st->last_core == st->last_core_from_bs )
    1863             :             {
    1864             :                 /* case: last frame was lost and concealed as CNG */
    1865             :                 /* using the longer transition spec length causes reading of uninitialized data */
    1866       39777 :                 *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
    1867             :             }
    1868             : 
    1869       40251 :             assert( hTcxCfg->lfacNext <= 0 );
    1870       40251 :             *L_frame -= hTcxCfg->lfacNext;
    1871       40251 :             *L_frameTCX -= hTcxCfg->lfacNextFB;
    1872       40251 :             *tcx_offset = hTcxCfg->lfacNext;
    1873       40251 :             *tcx_offsetFB = hTcxCfg->lfacNextFB;
    1874             : 
    1875       40251 :             *left_rect = 1;
    1876       40251 :             hTcxDec->prev_widow_left_rect = 1;
    1877             :         }
    1878             :         else
    1879             :         {
    1880     7655706 :             *L_frame = L_frame_glob;
    1881     7655706 :             *L_frameTCX = L_frameTCX_glob;
    1882     7655706 :             *left_rect = 0;
    1883     7655706 :             hTcxDec->prev_widow_left_rect = 0;
    1884             :         }
    1885             : 
    1886     7695957 :         st->L_frame_past = *L_frame;
    1887     7695957 :         st->L_frameTCX_past = *L_frameTCX;
    1888             :     }
    1889             : 
    1890     7798755 :     if ( st->igf )
    1891             :     {
    1892     5178117 :         if ( ( *L_frame == st->L_frame >> 1 ) && ( st->tcxonly ) )
    1893             :         {
    1894      234000 :             IGFDecUpdateInfo( st->hIGFDec, frame_cnt, IGF_GRID_LB_SHORT );
    1895             :         }
    1896             :         else
    1897             :         {
    1898     4944117 :             IGFDecUpdateInfo( st->hIGFDec, frame_cnt, ( st->last_core == ACELP_CORE || ( *left_rect && st->bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM );
    1899             :         }
    1900             :     }
    1901             : 
    1902     7798755 :     return;
    1903             : }
    1904             : 
    1905             : 
    1906             : /*-------------------------------------------------------------------*
    1907             :  *  decoder_tcx_IGF_mono()
    1908             :  *
    1909             :  *  TCX: apply mono IGF
    1910             :  *-------------------------------------------------------------------*/
    1911             : 
    1912      602523 : void decoder_tcx_IGF_mono(
    1913             :     Decoder_State *st,       /* i/o: coder memory state                 */
    1914             :     float x[],               /* o  : de-quatized coefficients           */
    1915             :     const int16_t L_frame,   /* i  : frame length                       */
    1916             :     const int16_t left_rect, /* i  : left part is rectangular           */
    1917             :     const int16_t bfi,       /* i  : bad frame indicator                */
    1918             :     const int16_t frame_cnt  /* i  : frame counter in the super_frame   */
    1919             : )
    1920             : {
    1921             :     int16_t igfGridIdx;
    1922             : 
    1923     1174626 :     IGFDecUpdateInfo( st->hIGFDec, frame_cnt, ( L_frame == st->L_frame >> 1 ) && ( st->tcxonly ) ? IGF_GRID_LB_SHORT : ( st->last_core == ACELP_CORE || left_rect ) ? IGF_GRID_LB_TRAN
    1924      572103 :                                                                                                                                                                     : IGF_GRID_LB_NORM );
    1925             : 
    1926      602523 :     if ( st->igf )
    1927             :     {
    1928      602523 :         igfGridIdx = IGF_GRID_LB_SHORT;
    1929             :         /*st->hIGFDec.igfData.igfInfo.nfSeed = (int16_t)(*nf_seed * 31821L + 13849L);*/
    1930      602523 :         if ( !( L_frame == st->L_frame >> 1 ) && st->tcxonly )
    1931             :         {
    1932      572103 :             igfGridIdx = ( st->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM;
    1933             :         }
    1934             : 
    1935      602523 :         IGFDecApplyMono( st->hIGFDec, x, igfGridIdx, bfi, st->element_mode );
    1936             :     }
    1937             : 
    1938      602523 :     return;
    1939             : }
    1940             : 
    1941             : 
    1942             : /*-------------------------------------------------------------------*
    1943             :  * decoder_tcx_IGF_stereo()
    1944             :  *
    1945             :  * TCX: apply stereo IGF processing
    1946             :  *-------------------------------------------------------------------*/
    1947             : 
    1948      267447 : void decoder_tcx_IGF_stereo(
    1949             :     Decoder_State **sts,                     /* i/o: coder memory states                    */
    1950             :     STEREO_MDCT_DEC_DATA_HANDLE hStereoMdct, /* i/o: MDCT stereo structure                  */
    1951             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                       */
    1952             :     float *x[CPE_CHANNELS][NB_DIV],          /* o  : de-quatized coefficients               */
    1953             :     const int16_t L_frame,                   /* i  : frame length                           */
    1954             :     const int16_t left_rect,                 /* i  : left part is rectangular               */
    1955             :     const int16_t k,                         /* i  : Subframe index                         */
    1956             :     const int16_t bfi,                       /* i  : bad frame indicator                    */
    1957             :     const int16_t MCT_flag                   /* i  : hMCT handle allocated (1) or not (0)   */
    1958             : )
    1959             : {
    1960             :     int16_t coreMsMask[N_MAX];
    1961             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
    1962             :     int16_t core, sfb, igfGridIdx;
    1963             : 
    1964      267447 :     core = sts[0]->core;
    1965             : 
    1966             :     /* assumptions: stereo filling was already done on the flattened spectra
    1967             :      *               IGF region is always coded M/S, never L/R (to be done in the encoder)
    1968             :      *               for residual bands with stereo filling infoTcxNoise is set to zero
    1969             :      *               both channels have the same IGF configuration
    1970             :      */
    1971             : 
    1972             : #ifdef DEBUGGING
    1973             :     /* sanity checks: check if both channels have the same configuration...*/
    1974             :     assert( ( sts[0]->core == sts[1]->core ) );
    1975             :     if ( sts[0]->last_core_from_bs == ACELP_CORE || sts[1]->last_core_from_bs == ACELP_CORE )
    1976             :     {
    1977             :         assert( ( sts[0]->last_core_from_bs == sts[1]->last_core_from_bs ) );
    1978             :     }
    1979             : #endif
    1980             : 
    1981             :     /* initialization */
    1982      267447 :     sfbConf = ( core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
    1983             : 
    1984      267447 :     if ( sts[0]->last_core == ACELP_CORE )
    1985             :     {
    1986          33 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
    1987             :     }
    1988             : 
    1989             :     /* create line wise ms mask for the core bands */
    1990      267447 :     set_s( coreMsMask, 0, N_MAX );
    1991             : 
    1992    11350140 :     for ( sfb = 0; sfb < sfbConf->sfbCnt; sfb++ )
    1993             :     {
    1994    11082693 :         set_s( &coreMsMask[sfbConf->sfbOffset[sfb]], ms_mask[k][sfb], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
    1995             :     }
    1996             : 
    1997      521226 :     IGFDecUpdateInfo( sts[0]->hIGFDec, k, ( L_frame == sts[0]->L_frame >> 1 ) && ( sts[0]->tcxonly ) ? IGF_GRID_LB_SHORT : ( sts[0]->last_core == ACELP_CORE || left_rect ) ? IGF_GRID_LB_TRAN
    1998      253779 :                                                                                                                                                                             : IGF_GRID_LB_NORM );
    1999             : 
    2000      521226 :     IGFDecUpdateInfo( sts[1]->hIGFDec, k, ( L_frame == sts[1]->L_frame >> 1 ) && ( sts[1]->tcxonly ) ? IGF_GRID_LB_SHORT : ( sts[1]->last_core == ACELP_CORE || left_rect ) ? IGF_GRID_LB_TRAN
    2001      253779 :                                                                                                                                                                             : IGF_GRID_LB_NORM );
    2002             : 
    2003      267447 :     if ( sts[0]->igf )
    2004             :     {
    2005      267447 :         igfGridIdx = IGF_GRID_LB_SHORT;
    2006      267447 :         if ( !( L_frame == sts[0]->L_frame >> 1 ) && ( sts[0]->tcxonly ) )
    2007             :         {
    2008      253779 :             igfGridIdx = ( sts[0]->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM;
    2009             :         }
    2010             : 
    2011      267447 :         IGFDecApplyStereo( sts[0]->hIGFDec, sts[1]->hIGFDec, x[0][k], x[1][k], igfGridIdx, coreMsMask, hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS, bfi, MCT_flag );
    2012             :     }
    2013             : 
    2014      267447 :     return;
    2015             : }

Generated by: LCOV version 1.14