LCOV - code coverage report
Current view: top level - lib_dec - swb_tbe_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 705 929 75.9 %
Date: 2025-05-23 08:37:30 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "rom_dec.h"
      47             : #include "wmc_auto.h"
      48             : #include "ivas_prot.h"
      49             : 
      50             : /*-----------------------------------------------------------------*
      51             :  * Local function prototypes
      52             :  *-----------------------------------------------------------------*/
      53             : 
      54             : static void dequantizeSHBparams( Decoder_State *st, const int16_t extl, int32_t extl_brate, float *Q_lsf, float *Q_subgain, float *Q_framegrain, int16_t *uv_flag, float *Q_shb_ener_sf, float *Q_shb_res_gshape, float *Q_mixFactors, int16_t *MSFlag );
      55             : static void Dequant_lower_LSF( const int16_t lsf_idx[], float lsf_q[] );
      56             : static void Map_higher_LSF( float lsf_q[], const float m, const float grid_in[] );
      57             : static void Dequant_mirror_point( const float lsf_q[], const int16_t m_idx, float *m );
      58             : 
      59             : 
      60             : /*-------------------------------------------------------------------*
      61             :  * ResetSHBbuffer_Dec()
      62             :  *
      63             :  *
      64             :  *-------------------------------------------------------------------*/
      65             : 
      66       16431 : void ResetSHBbuffer_Dec(
      67             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
      68             :     const int16_t extl         /* i  : BWE extension layer     */
      69             : )
      70             : {
      71             :     int16_t i;
      72             :     float f;
      73             :     float inc;
      74             : 
      75       16431 :     if ( extl != WB_TBE )
      76             :     {
      77       16431 :         f = 1.0f / 22.0f;
      78       16431 :         inc = 1.0f / 22.0f;
      79             :     }
      80             :     else
      81             :     {
      82           0 :         f = 1.0f / 6.0f;
      83           0 :         inc = 1.0f / 6.0f;
      84             :     }
      85             : 
      86             :     /* states for the filters used in generating SHB excitation from WB excitation*/
      87       16431 :     set_f( hBWE_TD->mem_csfilt, 0, 2 );
      88             : 
      89             :     /* states for the filters used in generating SHB signal from SHB excitation*/
      90       16431 :     set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD );
      91       16431 :     set_f( hBWE_TD->state_lpc_syn, 0, LPC_SHB_ORDER );
      92             : 
      93       16431 :     if ( extl == FB_TBE )
      94             :     {
      95           0 :         set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
      96           0 :         hBWE_TD->fb_tbe_demph = 0;
      97           0 :         fb_tbe_reset_synth( hBWE_TD->fbbwe_hpf_mem, &hBWE_TD->prev_fbbwe_ratio );
      98             :     }
      99             : 
     100             :     /* states for the filters used in generating SHB signal from SHB excitation in wideband*/
     101       16431 :     set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     102       16431 :     set_f( hBWE_TD->mem_genSHBexc_filt_down_wb2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     103       16431 :     set_f( hBWE_TD->mem_genSHBexc_filt_down_wb3, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     104             : 
     105       16431 :     set_f( hBWE_TD->state_lsyn_filt_shb, 0, 2 * ALLPASSSECTIONS_STEEP );
     106       16431 :     set_f( hBWE_TD->state_lsyn_filt_dwn_shb, 0, 2 * ALLPASSSECTIONS_STEEP );
     107       16431 :     set_f( hBWE_TD->mem_resamp_HB, 0, INTERP_3_1_MEM_LEN );
     108             : 
     109             :     /* States for the local synthesis filters */
     110       16431 :     set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
     111             : 
     112             :     /* States for FEC */
     113       16431 :     if ( extl != WB_TBE )
     114             :     {
     115      180741 :         for ( i = 0; i < LPC_SHB_ORDER; i++ )
     116             :         {
     117      164310 :             hBWE_TD->lsp_prevfrm[i] = f;
     118      164310 :             f += inc;
     119             :         }
     120             :     }
     121             :     else
     122             :     {
     123           0 :         for ( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     124             :         {
     125           0 :             hBWE_TD->lsp_prevfrm[i] = f;
     126           0 :             f += inc;
     127             :         }
     128           0 :         for ( ; i < LPC_SHB_ORDER; i++ )
     129             :         {
     130           0 :             hBWE_TD->lsp_prevfrm[i] = 0.0f;
     131             :         }
     132             :     }
     133             : 
     134       16431 :     hBWE_TD->GainFrame_prevfrm = 0.0f;
     135       16431 :     hBWE_TD->GainAttn = 1.0;
     136       16431 :     hBWE_TD->tbe_demph = 0.0f;
     137       16431 :     hBWE_TD->tbe_premph = 0.0f;
     138       16431 :     set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
     139       16431 :     hBWE_TD->gain_prec_swb = 1.0f;
     140       16431 :     set_f( hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
     141       16431 :     hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
     142       16431 :     hBWE_TD->prev_mix_factor = 1.0f;
     143             : 
     144       16431 :     set_f( hBWE_TD->old_core_synth, 0, L_FRAME16k );
     145       16431 :     set_f( hBWE_TD->old_tbe_synth, 0, L_SHB_TRANSITION_LENGTH );
     146       16431 :     hBWE_TD->tilt_swb_fec = 0.0f;
     147             : 
     148       16431 :     return;
     149             : }
     150             : 
     151             : /*-------------------------------------------------------------------*
     152             :  * wb_tbe_dec()
     153             :  *
     154             :  * WB TBE decoder, 6 - 8 kHz band decoding module
     155             :  *-------------------------------------------------------------------*/
     156             : 
     157       20967 : void wb_tbe_dec(
     158             :     Decoder_State *st,             /* i/o: decoder state structure         */
     159             :     const float *bwe_exc_extended, /* i  : bandwidth extended excitation   */
     160             :     const float voice_factors[],   /* i  : voicing factors                 */
     161             :     float *synth                   /* o  : WB synthesis/final synthesis    */
     162             : )
     163             : {
     164             :     int16_t i;
     165             :     TD_BWE_DEC_HANDLE hBWE_TD;
     166             :     float shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
     167             :     float exc4kWhtnd[L_FRAME16k / 4];
     168             :     float lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR], GainFrame;
     169             :     float error[L_FRAME16k];
     170             :     float upsampled_synth[L_FRAME48k];
     171             :     float prev_pow, curr_pow, scale;
     172             :     float temp, curr_frame_pow;
     173             :     int16_t j;
     174             :     float vf_modified[NB_SUBFR16k];
     175       20967 :     int16_t uv_flag = 0;
     176             : 
     177       20967 :     hBWE_TD = st->hBWE_TD;
     178             : 
     179       20967 :     if ( st->bws_cnt == 0 )
     180             :     {
     181             : 
     182       20727 :         if ( !st->bfi )
     183             :         {
     184       20577 :             if ( st->use_partial_copy )
     185             :             {
     186           0 :                 if ( st->last_extl != WB_TBE )
     187             :                 {
     188           0 :                     hBWE_TD->GainFrame_prevfrm = 0;
     189           0 :                     hBWE_TD->lsp_prevfrm[0] = 0.1f;
     190           0 :                     for ( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
     191             :                     {
     192           0 :                         hBWE_TD->lsp_prevfrm[i] = hBWE_TD->lsp_prevfrm[i - i] + 0.1f;
     193             :                     }
     194             :                 }
     195             : 
     196           0 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     197           0 :                 set_f( GainShape, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR / 2 );
     198             : 
     199           0 :                 if ( st->rf_frame_type == RF_NELP )
     200             :                 {
     201             :                     /* Frame gain */
     202             :                     /* only four LSBs are valid */
     203           0 :                     st->rf_indx_tbeGainFr &= 0xF;
     204           0 :                     mvr2r( SHBCB_FrameGain16 + st->rf_indx_tbeGainFr, &GainFrame, 1 );
     205           0 :                     if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE && !st->prev_use_partial_copy && st->prev_coder_type == UNVOICED && GainFrame != hBWE_TD->GainFrame_prevfrm && st->last_extl == WB_TBE )
     206             :                     {
     207           0 :                         GainFrame = 0.2f * GainFrame + 0.8f * hBWE_TD->GainFrame_prevfrm;
     208             :                     }
     209             :                 }
     210             :                 else
     211             :                 {
     212             :                     /* Frame gain */
     213           0 :                     temp = 0.0f;
     214           0 :                     switch ( st->rf_indx_tbeGainFr )
     215             :                     {
     216           0 :                         case 0:
     217           0 :                             GainFrame = 0.5f;
     218           0 :                             if ( hBWE_TD->GainFrame_prevfrm <= 1.25 )
     219             :                             {
     220           0 :                                 temp = 0.8f;
     221             :                             }
     222           0 :                             break;
     223           0 :                         case 1:
     224           0 :                             GainFrame = 2.0f;
     225           0 :                             if ( hBWE_TD->GainFrame_prevfrm > 1.25 && hBWE_TD->GainFrame_prevfrm <= 3 )
     226             :                             {
     227           0 :                                 temp = 0.8f;
     228             :                             }
     229           0 :                             break;
     230           0 :                         case 2:
     231           0 :                             GainFrame = 4.0f;
     232           0 :                             if ( hBWE_TD->GainFrame_prevfrm > 3 && hBWE_TD->GainFrame_prevfrm <= 6 )
     233             :                             {
     234           0 :                                 temp = 0.8f;
     235             :                             }
     236           0 :                             break;
     237           0 :                         case 3:
     238           0 :                             GainFrame = 8.0f;
     239           0 :                             if ( hBWE_TD->GainFrame_prevfrm > 6 && hBWE_TD->GainFrame_prevfrm <= 16 )
     240             :                             {
     241           0 :                                 temp = 0.8f;
     242             :                             }
     243           0 :                             break;
     244           0 :                         default:
     245           0 :                             GainFrame = 1.0f;
     246           0 :                             IVAS_ERROR( IVAS_ERR_INTERNAL, "RF WB-TBE gain bits not supported." );
     247           0 :                             break;
     248             :                     }
     249             : 
     250           0 :                     if ( st->last_extl == WB_TBE )
     251             :                     {
     252           0 :                         GainFrame = ( 1 - temp ) * GainFrame + temp * ( hBWE_TD->GainFrame_prevfrm );
     253             :                     }
     254             : 
     255           0 :                     if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE )
     256             :                     {
     257           0 :                         if ( !st->prev_use_partial_copy && st->last_coder_type == VOICED && st->rf_frame_type == RF_GENPRED && st->prev_tilt_code_dec < 0.046f && st->prev_tilt_code_dec > 0.006f )
     258             :                         {
     259           0 :                             GainFrame *= 0.3f;
     260             :                         }
     261             :                     }
     262             :                 }
     263             :             }
     264             :             else
     265             :             {
     266             :                 /* de-quantization */
     267       20577 :                 dequantizeSHBparams( st, st->extl, st->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0, &j /*temp*/ );
     268             :             }
     269             :         }
     270             :         else
     271             :         {
     272         150 :             if ( st->extl_brate == WB_TBE_0k35 )
     273             :             {
     274           0 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     275             :             }
     276             :             else
     277             :             {
     278         150 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_wb, LPC_SHB_ORDER_WB );
     279             :             }
     280         150 :             set_f( GainShape, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR / 2 );
     281         150 :             hBWE_TD->GainAttn *= 0.85f;
     282         150 :             if ( st->codec_mode == MODE1 )
     283             :             {
     284         150 :                 GainFrame = hBWE_TD->GainAttn * hBWE_TD->GainFrame_prevfrm;
     285             :             }
     286             :             else
     287             :             {
     288           0 :                 GainFrame = hBWE_TD->GainFrame_prevfrm;
     289             :             }
     290             :         }
     291             : 
     292       20727 :         if ( st->extl_brate == WB_TBE_0k35 )
     293             :         {
     294             :             /* convert LSPs back into LP coeffs */
     295        7317 :             lsp2a( lpc_wb, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     296        7317 :             set_f( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0.0f, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
     297             :         }
     298             :         else
     299             :         {
     300             :             /* convert LSPs back into LP coeffs */
     301       13410 :             lsp2a( lpc_wb, lsf_wb, LPC_SHB_ORDER_WB );
     302             :         }
     303       20727 :         lpc_wb[0] = 1.0f;
     304       20727 :         mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
     305       20727 :         if ( st->coder_type == VOICED )
     306             :         {
     307       14136 :             for ( i = 1; i < NB_SUBFR; i++ )
     308             :             {
     309       10602 :                 vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
     310             :             }
     311             : 
     312        3534 :             if ( st->L_frame != L_FRAME )
     313             :             {
     314           0 :                 vf_modified[4] = 0.8f * voice_factors[4] + 0.2f * voice_factors[3];
     315             :             }
     316             :         }
     317             : 
     318       20727 :         if ( st->use_partial_copy && st->nelp_mode_dec )
     319             :         {
     320           0 :             set_f( vf_modified, 0.0f, NB_SUBFR16k );
     321             :         }
     322             : 
     323             :         /* From low band excitation, generate highband excitation */
     324       20727 :         mvr2r( hBWE_TD->state_syn_shbexc, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     325             : 
     326       20727 :         GenShapedWBExcitation( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt, hBWE_TD->mem_genSHBexc_filt_down_shb, hBWE_TD->mem_genSHBexc_filt_down_wb2, hBWE_TD->mem_genSHBexc_filt_down_wb3, hBWE_TD->state_lpc_syn, st->coder_type, bwe_exc_extended, hBWE_TD->bwe_seed, vf_modified, uv_flag, st->igf );
     327             : 
     328       20727 :         prev_pow = sum2_f( shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     329       20727 :         curr_pow = sum2_f( shaped_wb_excitation + L_SHB_LAHEAD / 4, L_SHB_LAHEAD / 4 );
     330             : 
     331       20727 :         if ( st->element_mode > EVS_MONO )
     332             :         {
     333             :             /* prevent too low values of energy */
     334       20727 :             prev_pow = max( 0.00001f, prev_pow );
     335       20727 :             curr_pow = max( 0.00001f, curr_pow );
     336             :         }
     337             : 
     338       20727 :         if ( voice_factors[0] > 0.75f )
     339             :         {
     340         738 :             curr_pow *= 0.25;
     341             :         }
     342             : 
     343       20727 :         if ( prev_pow == 0 )
     344             :         {
     345           0 :             scale = 0;
     346             :         }
     347             :         else
     348             :         {
     349       20727 :             scale = (float) sqrt( curr_pow / prev_pow );
     350             :         }
     351             : 
     352      103635 :         for ( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
     353             :         {
     354       82908 :             shaped_wb_excitation[i] *= scale;
     355             :         }
     356       20727 :         scale = (float) sqrt( scale );
     357             : 
     358       20727 :         shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] *= scale;
     359             : 
     360             :         /* Update SHB excitation */
     361       20727 :         mvr2r( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD / 4 );
     362             : 
     363             :         /* Adjust the subframe and frame gain of the synthesized shb signal */
     364             :         /* Scale the shaped excitation */
     365       20727 :         ScaleShapedSHB( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap, GainShape, GainFrame, window_wb, subwin_wb );
     366             : 
     367       20727 :         curr_frame_pow = sum2_f( shaped_wb_excitation, L_FRAME16k / 4 ) + 0.001f;
     368       20727 :         curr_frame_pow = min( curr_frame_pow, FLT_MAX );
     369             : 
     370       20727 :         if ( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
     371             :         {
     372         144 :             if ( curr_frame_pow > 2.0f * hBWE_TD->prev_wb_bwe_frame_pow )
     373             :             {
     374         102 :                 scale = root_a_over_b( hBWE_TD->prev_wb_bwe_frame_pow, curr_frame_pow );
     375         102 :                 temp = (float) pow( scale, 0.125f );
     376             :             }
     377             :             else
     378             :             {
     379          42 :                 scale = 1.0f;
     380          42 :                 temp = 1.0f;
     381             :             }
     382             : 
     383        1296 :             for ( j = 0; j < 8; j++ )
     384             :             {
     385        1152 :                 GainShape[2 * j] *= scale;
     386        1152 :                 GainShape[2 * j + 1] *= scale;
     387       12672 :                 for ( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
     388             :                 {
     389       11520 :                     shaped_wb_excitation[i + j * L_FRAME16k / ( 4 * 8 )] *= scale;
     390             :                 }
     391        1152 :                 scale /= temp;
     392             :             }
     393             :         }
     394             : 
     395       20727 :         hBWE_TD->prev_wb_bwe_frame_pow = curr_frame_pow;
     396             : 
     397             :         /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
     398       20727 :         GenWBSynth( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb, hBWE_TD->state_lsyn_filt_dwn_shb );
     399             : 
     400       20727 :         mvr2r( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth, L_SHB_TRANSITION_LENGTH );
     401             : 
     402     6653367 :         for ( i = 0; i < L_FRAME16k; i++ )
     403             :         {
     404     6632640 :             synth[i] = 0.65f * error[i];
     405             :         }
     406             : 
     407       20727 :         if ( st->hBWE_FD != NULL )
     408             :         {
     409       20727 :             st->hBWE_FD->last_wb_bwe_ener = 0.0f;
     410     6653367 :             for ( i = 0; i < L_FRAME16k; i++ )
     411             :             {
     412     6632640 :                 st->hBWE_FD->last_wb_bwe_ener += synth[i] * synth[i];
     413             :             }
     414       20727 :             st->hBWE_FD->last_wb_bwe_ener = (float) sqrt( st->hBWE_FD->last_wb_bwe_ener / L_FRAME16k );
     415             :         }
     416             : 
     417       20727 :         if ( st->output_Fs == 32000 ) /* 32kHz sampling rate, but only WB output - interpolate */
     418             :         {
     419        1551 :             Interpolate_allpass_steep( synth, hBWE_TD->mem_resamp_HB, L_FRAME16k, upsampled_synth );
     420        1551 :             mvr2r( upsampled_synth, synth, L_FRAME32k );
     421             :         }
     422       19176 :         else if ( st->output_Fs == 48000 )
     423             :         {
     424        9882 :             interpolate_3_over_1_allpass( synth, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB );
     425        9882 :             mvr2r( upsampled_synth, synth, L_FRAME48k );
     426             :         }
     427             :     }
     428             :     else
     429             :     {
     430        1680 :         for ( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     431             :         {
     432        1440 :             lsf_wb[i] = ( i + 1 ) / 6.f;
     433             :         }
     434         240 :         GainFrame = 0;
     435             : 
     436         240 :         hBWE_TD->prev_wb_bwe_frame_pow = 0.001f;
     437             :     }
     438             : 
     439             :     /* Update previous frame parameters for FEC */
     440       20967 :     if ( st->extl_brate == WB_TBE_0k35 )
     441             :     {
     442        7341 :         mvr2r( lsf_wb, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER_LBR_WB );
     443             :     }
     444             :     else
     445             :     {
     446       13626 :         mvr2r( lsf_wb, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER_WB );
     447             :     }
     448       20967 :     hBWE_TD->GainFrame_prevfrm = GainFrame;
     449             : 
     450       20967 :     if ( !st->bfi )
     451             :     {
     452       20817 :         hBWE_TD->GainAttn = 1.0f;
     453             :     }
     454             : 
     455       20967 :     return;
     456             : }
     457             : 
     458             : /*-------------------------------------------------------------------*
     459             :  * swb_tbe_dec()
     460             :  *
     461             :  * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
     462             :  *-------------------------------------------------------------------*/
     463             : 
     464      345429 : void swb_tbe_dec(
     465             :     Decoder_State *st,                    /* i/o: decoder state structure         */
     466             :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure          */
     467             :     const float *bwe_exc_extended,        /* i  : bandwidth extended excitation   */
     468             :     const float voice_factors[],          /* i  : voicing factors                 */
     469             :     const float old_syn_12k8_16k[],       /* i  : low band synthesis              */
     470             :     float *White_exc16k,                  /* o  : shaped white excitation for the FB TBE */
     471             :     float *synth,                         /* o  : SHB synthesis/final synthesis   */
     472             :     float *pitch_buf )
     473             : {
     474             :     int16_t i, j;
     475             :     int16_t stemp;
     476             :     TD_BWE_DEC_HANDLE hBWE_TD;
     477             :     float shaped_shb_excitation[L_FRAME16k + L_SHB_LAHEAD];
     478             :     float lsf_shb[LPC_SHB_ORDER], lpc_shb[LPC_SHB_ORDER + 1], GainShape[NUM_SHB_SUBFR], GainFrame;
     479             :     float error[L_FRAME32k];
     480             :     float ener;
     481             :     int16_t is_fractive;
     482             :     float prev_pow, curr_pow, scale;
     483             :     float curr_frame_pow, temp;
     484             :     float GainShapeTemp[NUM_SHB_SUBFR / 4], GainGrad0[3], GainGrad1[3], GainGradFEC[4];
     485             :     float vf_modified[NB_SUBFR16k];
     486             :     float f, inc;
     487             :     float GainFrame_prevfrm;
     488             :     float tilt_swb_fec;
     489             :     float prev_ener_ratio;
     490             :     float lsp_shb_1[LPC_SHB_ORDER], lsp_shb_2[LPC_SHB_ORDER], lsp_temp[LPC_SHB_ORDER];
     491             :     float lpc_shb_sf[4 * ( LPC_SHB_ORDER + 1 )];
     492             :     const float *ptr_lsp_interp_coef;
     493             :     float shb_ener_sf;
     494             :     float shb_res_gshape[NB_SUBFR16k];
     495             :     float mixFactors;
     496             :     int16_t vind;
     497             :     float shb_res_dummy[L_FRAME16k];
     498             :     float shaped_shb_excitationTemp[L_FRAME16k];
     499             :     float ener_tmp[NUM_SHB_SUBGAINS];
     500             :     float GainShape_tmp[NUM_SHB_SUBGAINS];
     501             :     float pitch;
     502             :     int16_t l_subframe;
     503             :     float formant_fac;
     504             :     float synth_scale;
     505             :     float lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
     506             :     float refl[M];
     507             :     float tilt_para;
     508             :     float *nlExc16k, *mixExc16k;
     509             :     int16_t MSFlag;
     510             :     float feedback;
     511             :     float GainShape_tilt;
     512             : 
     513      345429 :     hBWE_TD = st->hBWE_TD;
     514             : 
     515             :     /* initializations */
     516      345429 :     GainFrame = 0.0f;
     517      345429 :     mixFactors = 0.0f;
     518      345429 :     shb_ener_sf = 0.0f;
     519      345429 :     set_f( shaped_shb_excitationTemp, 0.0f, L_FRAME16k );
     520      345429 :     if ( st->hTdCngDec != NULL )
     521             :     {
     522      244428 :         st->hTdCngDec->shb_dtx_count = 0;
     523             :     }
     524      345429 :     is_fractive = 0;
     525             : 
     526      345429 :     if ( hStereoICBWE != NULL )
     527             :     {
     528       49284 :         nlExc16k = hStereoICBWE->nlExc16k;
     529       49284 :         mixExc16k = hStereoICBWE->mixExc16k;
     530       49284 :         MSFlag = hStereoICBWE->MSFlag;
     531             :     }
     532             :     else
     533             :     {
     534      296145 :         nlExc16k = NULL;
     535      296145 :         mixExc16k = NULL;
     536      296145 :         MSFlag = 0;
     537             :     }
     538             : 
     539             :     /* find tilt */
     540      345429 :     calc_tilt_bwe( old_syn_12k8_16k, &tilt_swb_fec, L_FRAME );
     541             : 
     542      345429 :     if ( st->bfi && st->clas_dec != UNVOICED_CLAS )
     543             :     {
     544        6849 :         tilt_swb_fec = hBWE_TD->tilt_swb_fec;
     545             :     }
     546             : 
     547             :     /* WB/SWB bandwidth switching */
     548      345429 :     if ( ( st->tilt_wb > 5 && st->clas_dec == UNVOICED_CLAS ) || st->tilt_wb > 10 )
     549             :     {
     550       26604 :         if ( ( st->prev_fractive == 0 && st->prev_enerLH < 2.0f * st->enerLH && st->prev_enerLH > 0.5f * st->enerLH && st->prev_enerLL < 2.0f * st->enerLL && st->prev_enerLL > 0.5f * st->enerLL ) || ( st->prev_fractive == 1 && st->prev_enerLH > 3.0f * st->enerLH ) || ( st->enerLL > 1.5f * st->enerLH && st->tilt_wb < 10.0f ) )
     551             :         {
     552         129 :             is_fractive = 0;
     553             :         }
     554             :         else
     555             :         {
     556       26475 :             is_fractive = 1;
     557             :         }
     558             :     }
     559             : 
     560             :     /* WB/SWB bandwidth switching */
     561      345429 :     if ( st->bws_cnt > 0 )
     562             :     {
     563         312 :         f = 1.0f / 22.0f;
     564         312 :         inc = 1.0f / 22.0f;
     565             : 
     566         312 :         if ( is_fractive == 1 )
     567             :         {
     568           0 :             mvr2r( lsf_tab, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER );
     569             :         }
     570             :         else
     571             :         {
     572        3432 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
     573             :             {
     574        3120 :                 hBWE_TD->lsp_prevfrm[i] = f;
     575        3120 :                 f += inc;
     576             :             }
     577             :         }
     578         312 :         if ( ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE && !( st->prev_enerLH < 2.0f * st->enerLH && st->prev_enerLH > 0.5f * st->enerLH ) ) || st->last_core != ACELP_CORE || ( st->last_core == ACELP_CORE && labs( st->last_core_brate - st->core_brate ) > 3600 ) || ( is_fractive ^ st->prev_fractive ) == 1 )
     579             :         {
     580         312 :             set_f( GainShape, 0.3536f, NUM_SHB_SUBFR );
     581             :         }
     582             :         else
     583             :         {
     584           0 :             hBWE_TD->prev_GainShape = ( hBWE_TD->prev_GainShape > 0.3536f ) ? 0.353f : hBWE_TD->prev_GainShape;
     585           0 :             set_f( GainShape, hBWE_TD->prev_GainShape, NUM_SHB_SUBFR );
     586             :         }
     587             : 
     588         312 :         mvr2r( hBWE_TD->lsp_prevfrm, lsf_shb, LPC_SHB_ORDER );
     589         312 :         set_f( shb_res_gshape, 0.2f, NB_SUBFR16k );
     590             :     }
     591             :     else
     592             :     {
     593      345117 :         if ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE )
     594             :         {
     595       21684 :             f = 1.0f / 22.0f;
     596       21684 :             inc = 1.0f / 22.0f;
     597      238524 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
     598             :             {
     599      216840 :                 hBWE_TD->lsp_prevfrm[i] = f;
     600      216840 :                 f += inc;
     601             :             }
     602             :         }
     603             : 
     604      345117 :         if ( !st->bfi )
     605             :         {
     606      336285 :             if ( st->use_partial_copy )
     607             :             {
     608           0 :                 if ( st->last_extl != SWB_TBE )
     609             :                 {
     610           0 :                     hBWE_TD->GainFrame_prevfrm = 0;
     611           0 :                     f = 1.0f / 22.0f;
     612           0 :                     inc = 1.0f / 22.0f;
     613           0 :                     for ( i = 0; i < LPC_SHB_ORDER; i++ )
     614             :                     {
     615           0 :                         hBWE_TD->lsp_prevfrm[i] = f;
     616           0 :                         f += inc;
     617             :                     }
     618             :                 }
     619           0 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_shb, LPC_SHB_ORDER );
     620           0 :                 set_f( GainShape, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR );
     621             : 
     622           0 :                 if ( st->rf_frame_type == RF_NELP )
     623             :                 {
     624             :                     /* Frame gain */
     625           0 :                     GainFrame = usdequant( st->rf_indx_tbeGainFr, SHB_GAIN_QLOW, SHB_GAIN_QDELTA );
     626           0 :                     GainFrame = (float) pow( 10.0, GainFrame );
     627             : 
     628           0 :                     if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE && !st->prev_use_partial_copy && st->prev_coder_type == UNVOICED && GainFrame != hBWE_TD->GainFrame_prevfrm && st->next_coder_type != GENERIC && st->last_extl == SWB_TBE )
     629             :                     {
     630           0 :                         GainFrame = 0.2f * GainFrame + 0.8f * hBWE_TD->GainFrame_prevfrm;
     631             :                     }
     632             :                 }
     633             :                 else
     634             :                 {
     635           0 :                     temp = 0.0f;
     636             :                     /* Frame gain */
     637           0 :                     switch ( st->rf_indx_tbeGainFr )
     638             :                     {
     639           0 :                         case 0:
     640           0 :                             GainFrame = 0.5f;
     641           0 :                             if ( hBWE_TD->GainFrame_prevfrm <= 1.25 )
     642             :                             {
     643           0 :                                 temp = 0.8f;
     644             :                             }
     645           0 :                             break;
     646           0 :                         case 1:
     647           0 :                             GainFrame = 2.0f;
     648           0 :                             if ( hBWE_TD->GainFrame_prevfrm > 1.25 && hBWE_TD->GainFrame_prevfrm <= 3 )
     649             :                             {
     650           0 :                                 temp = 0.8f;
     651             :                             }
     652           0 :                             break;
     653           0 :                         case 2:
     654           0 :                             GainFrame = 4.0f;
     655           0 :                             if ( hBWE_TD->GainFrame_prevfrm > 3 && hBWE_TD->GainFrame_prevfrm <= 6 )
     656             :                             {
     657           0 :                                 temp = 0.8f;
     658             :                             }
     659           0 :                             break;
     660           0 :                         case 3:
     661           0 :                             GainFrame = 8.0f;
     662           0 :                             if ( hBWE_TD->GainFrame_prevfrm > 6 && hBWE_TD->GainFrame_prevfrm <= 16 )
     663             :                             {
     664           0 :                                 temp = 0.8f;
     665             :                             }
     666           0 :                             break;
     667           0 :                         default:
     668           0 :                             IVAS_ERROR( IVAS_ERR_INTERNAL, "RF SWB-TBE gain bits not supported." );
     669             :                     }
     670             : 
     671           0 :                     if ( st->last_extl == SWB_TBE )
     672             :                     {
     673           0 :                         GainFrame = ( 1 - temp ) * GainFrame + temp * ( hBWE_TD->GainFrame_prevfrm );
     674             :                     }
     675             : 
     676           0 :                     if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE )
     677             :                     {
     678           0 :                         if ( !st->prev_use_partial_copy && st->last_coder_type == VOICED && st->rf_frame_type == RF_GENPRED && GainFrame > 8.0f && GainFrame < 11.67f )
     679             :                         {
     680           0 :                             GainFrame *= 0.3f;
     681             :                         }
     682             :                     }
     683             :                 }
     684             :             }
     685             :             else
     686             :             {
     687             :                 /* de-quantization */
     688      336285 :                 dequantizeSHBparams( st, st->extl, st->extl_brate, lsf_shb, GainShape, &GainFrame, &stemp, &shb_ener_sf, shb_res_gshape, &mixFactors, &MSFlag );
     689      336285 :                 if ( hStereoICBWE != NULL )
     690             :                 {
     691       48252 :                     hStereoICBWE->MSFlag = MSFlag;
     692             :                 }
     693             :             }
     694             :         }
     695             :         else
     696             :         {
     697        8832 :             mvr2r( hBWE_TD->lsp_prevfrm, lsf_shb, LPC_SHB_ORDER );
     698             : 
     699        8832 :             if ( st->codec_mode == MODE1 && st->element_mode == EVS_MONO )
     700             :             {
     701             :                 /* the previous frame gainshape gradient and the gainshape gradient pattern for the current frame */
     702           0 :                 for ( j = 0; j < 3; j++ )
     703             :                 {
     704           0 :                     GainGrad0[j] = hBWE_TD->GainShape_Delay[j + 1] - hBWE_TD->GainShape_Delay[j];
     705           0 :                     GainGrad1[j] = hBWE_TD->GainShape_Delay[j + 5] - hBWE_TD->GainShape_Delay[j + 4];
     706           0 :                     GainGradFEC[j + 1] = GainGrad0[j] * 0.4f + GainGrad1[j] * 0.6f;
     707             :                 }
     708             : 
     709             :                 /* gradient for the first gainshape */
     710           0 :                 if ( ( GainGrad1[2] > 2 * GainGrad1[1] && GainGrad1[1] > 2 * GainGrad1[0] ) ||
     711           0 :                      ( GainGrad1[2] < 2 * GainGrad1[1] && GainGrad1[1] < 2 * GainGrad1[0] ) )
     712             :                 {
     713           0 :                     GainGradFEC[0] = GainGrad1[1] * 0.1f + GainGrad1[2] * 0.9f;
     714             :                 }
     715             :                 else
     716             :                 {
     717           0 :                     GainGradFEC[0] = GainGrad1[0] * 0.2f + GainGrad1[1] * 0.3f + GainGrad1[2] * 0.5f;
     718             :                 }
     719             : 
     720             :                 /* get the first gainshape template */
     721           0 :                 if ( ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS ) && GainGradFEC[0] > 0 )
     722             :                 {
     723           0 :                     GainShapeTemp[0] = hBWE_TD->GainShape_Delay[7] + GainGradFEC[0];
     724             :                 }
     725           0 :                 else if ( GainGradFEC[0] > 0 )
     726             :                 {
     727           0 :                     GainShapeTemp[0] = hBWE_TD->GainShape_Delay[7] + GainGradFEC[0] * 0.5f;
     728             :                 }
     729             :                 else
     730             :                 {
     731           0 :                     GainShapeTemp[0] = hBWE_TD->GainShape_Delay[7];
     732             :                 }
     733             : 
     734             :                 /*Get the second the third and the fourth gainshape template*/
     735           0 :                 if ( ( GainGrad1[2] > 10.0f * GainGrad1[1] ) && GainGrad1[1] > 0 )
     736             :                 {
     737           0 :                     for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
     738             :                     {
     739           0 :                         GainShapeTemp[i] = GainShapeTemp[i - 1] + GainGradFEC[i] * 0.8f;
     740           0 :                         GainShapeTemp[i] = max( GainShapeTemp[i], 0.01f );
     741             :                     }
     742             :                 }
     743           0 :                 else if ( ( GainGrad1[2] > 10.0f * GainGrad1[1] ) && GainGrad1[1] < 0 )
     744             :                 {
     745           0 :                     for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
     746             :                     {
     747           0 :                         GainShapeTemp[i] = GainShapeTemp[i - 1] + GainGradFEC[i] * 0.2f;
     748           0 :                         GainShapeTemp[i] = max( GainShapeTemp[i], 0.01f );
     749             :                     }
     750             :                 }
     751             :                 else
     752             :                 {
     753           0 :                     for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
     754             :                     {
     755           0 :                         GainShapeTemp[i] = GainShapeTemp[i - 1] + GainGradFEC[i];
     756           0 :                         GainShapeTemp[i] = max( GainShapeTemp[i], 0.01f );
     757             :                     }
     758             :                 }
     759             : 
     760             :                 /* Get the gainshape and gain frame for the current frame*/
     761           0 :                 if ( ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS ) && st->nbLostCmpt == 1 )
     762             :                 {
     763           0 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     764             :                     {
     765           0 :                         for ( j = 0; j < 4; j++ )
     766             :                         {
     767           0 :                             GainShape[i * 4 + j] = GainShapeTemp[i] * 1.2f;
     768             :                         }
     769             :                     }
     770           0 :                     hBWE_TD->GainAttn *= 0.95f;
     771             :                 }
     772           0 :                 else if ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS )
     773             :                 {
     774           0 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     775             :                     {
     776           0 :                         for ( j = 0; j < 4; j++ )
     777             :                         {
     778           0 :                             GainShape[i * 4 + j] = GainShapeTemp[i];
     779             :                         }
     780             :                     }
     781           0 :                     hBWE_TD->GainAttn *= 0.95f;
     782             :                 }
     783           0 :                 else if ( st->nbLostCmpt > 1 )
     784             :                 {
     785           0 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     786             :                     {
     787           0 :                         for ( j = 0; j < 4; j++ )
     788             :                         {
     789           0 :                             GainShape[i * 4 + j] = GainShapeTemp[i] * 0.5f;
     790             :                         }
     791             :                     }
     792           0 :                     hBWE_TD->GainAttn *= 0.5f;
     793             :                 }
     794             :                 else
     795             :                 {
     796           0 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     797             :                     {
     798           0 :                         for ( j = 0; j < 4; j++ )
     799             :                         {
     800           0 :                             GainShape[i * 4 + j] = GainShapeTemp[i];
     801             :                         }
     802             :                     }
     803           0 :                     hBWE_TD->GainAttn *= 0.85f;
     804             :                 }
     805             : 
     806           0 :                 GainFrame = hBWE_TD->GainAttn * hBWE_TD->GainFrame_prevfrm;
     807             :             }
     808             :             else
     809             :             {
     810       44160 :                 for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     811             :                 {
     812      176640 :                     for ( j = 0; j < 4; j++ )
     813             :                     {
     814      141312 :                         GainShape[i * 4 + j] = st->cummulative_damping * hBWE_TD->GainShape_Delay[4 + i];
     815             :                     }
     816             :                 }
     817             : 
     818        8832 :                 if ( tilt_swb_fec > 8 )
     819             :                 {
     820         267 :                     if ( st->nbLostCmpt == 1 )
     821             :                     {
     822         213 :                         GainFrame = (float) ( 0.6 * st->cummulative_damping * hBWE_TD->GainFrame_prevfrm );
     823             :                     }
     824          54 :                     else if ( st->nbLostCmpt == 2 )
     825             :                     {
     826          54 :                         GainFrame = (float) ( 0.35 * st->cummulative_damping * hBWE_TD->GainFrame_prevfrm );
     827             :                     }
     828             :                     else
     829             :                     {
     830           0 :                         GainFrame = (float) ( 0.2 * st->cummulative_damping * hBWE_TD->GainFrame_prevfrm );
     831             :                     }
     832             :                 }
     833             :                 else
     834             :                 {
     835        8565 :                     GainFrame = hBWE_TD->GainFrame_prevfrm; /* gain locking */
     836             :                 }
     837             :             }
     838             : 
     839        8832 :             if ( st->extl_brate >= SWB_TBE_2k8 )
     840             :             {
     841        2883 :                 if ( st->codec_mode == MODE1 && st->element_mode == EVS_MONO )
     842             :                 {
     843           0 :                     scale = (float) ( hBWE_TD->prev1_shb_ener_sf / sqrt( ( hBWE_TD->prev2_shb_ener_sf * hBWE_TD->prev3_shb_ener_sf ) + 0.0001 ) );
     844           0 :                     scale = hBWE_TD->prev_res_shb_gshape * min( scale, 1.0f );
     845             : 
     846           0 :                     if ( hBWE_TD->prev2_shb_ener_sf > 2.0f * hBWE_TD->prev1_shb_ener_sf || hBWE_TD->prev3_shb_ener_sf > 2.0f * hBWE_TD->prev2_shb_ener_sf )
     847             :                     {
     848           0 :                         shb_ener_sf = 0.5f * scale * hBWE_TD->prev1_shb_ener_sf;
     849           0 :                         if ( st->nbLostCmpt > 1 )
     850             :                         {
     851           0 :                             shb_ener_sf *= 0.5f;
     852             :                         }
     853             :                     }
     854             :                     else
     855             :                     {
     856           0 :                         shb_ener_sf = scale * scale * hBWE_TD->prev1_shb_ener_sf;
     857             :                     }
     858             :                 }
     859             :                 else
     860             :                 {
     861        2883 :                     if ( hBWE_TD->prev2_shb_ener_sf > 2.0f * hBWE_TD->prev1_shb_ener_sf || hBWE_TD->prev3_shb_ener_sf > 2.0f * hBWE_TD->prev2_shb_ener_sf )
     862             :                     {
     863         156 :                         shb_ener_sf = 0.5f * st->cummulative_damping * hBWE_TD->prev1_shb_ener_sf;
     864             :                     }
     865             :                     else
     866             :                     {
     867        2727 :                         shb_ener_sf = st->cummulative_damping * hBWE_TD->prev1_shb_ener_sf;
     868             :                     }
     869             :                 }
     870             :             }
     871             : 
     872        8832 :             shb_ener_sf = max( shb_ener_sf, 1.0f );
     873        8832 :             mixFactors = hBWE_TD->prev_mixFactors;
     874             : 
     875        8832 :             if ( st->codec_mode == MODE2 )
     876             :             {
     877           0 :                 set_f( shb_res_gshape, 1.0f, NB_SUBFR16k );
     878             :             }
     879             :             else
     880             :             {
     881        8832 :                 set_f( shb_res_gshape, 0.2f, NB_SUBFR16k );
     882             :             }
     883             :         }
     884             :     }
     885             : 
     886             :     /* get the gainshape delay */
     887      345429 :     mvr2r( &hBWE_TD->GainShape_Delay[4], &hBWE_TD->GainShape_Delay[0], NUM_SHB_SUBFR / 4 );
     888     1727145 :     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     889             :     {
     890     1381716 :         hBWE_TD->GainShape_Delay[i + 4] = GainShape[i * 4];
     891             :     }
     892             : 
     893      345429 :     mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
     894      345429 :     if ( st->coder_type == VOICED || mean( voice_factors, 4 ) > 0.4f )
     895             :     {
     896      308376 :         for ( i = 1; i < NB_SUBFR; i++ )
     897             :         {
     898      231282 :             vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
     899             :         }
     900             : 
     901       77094 :         if ( st->L_frame != L_FRAME )
     902             :         {
     903       38481 :             vf_modified[4] = 0.8f * voice_factors[4] + 0.2f * voice_factors[3];
     904             :         }
     905             :     }
     906             : 
     907      345429 :     if ( st->use_partial_copy && st->nelp_mode_dec )
     908             :     {
     909           0 :         set_f( vf_modified, 0.0f, NB_SUBFR16k );
     910             :     }
     911             : 
     912             :     /* SHB LSF from current frame; and convert to LSP for interpolation */
     913      345429 :     lsf2lsp( lsf_shb, lsp_shb_2, LPC_SHB_ORDER, 1 );
     914             : 
     915      345429 :     if ( st->last_extl == SWB_TBE || st->last_extl == FB_TBE )
     916             :     {
     917             :         /* SHB LSP values from prev. frame for interpolation */
     918      323433 :         mvr2r( hBWE_TD->swb_lsp_prev_interp, lsp_shb_1, LPC_SHB_ORDER );
     919             :     }
     920             :     else
     921             :     {
     922             :         /* Use current frame's LSPs; in effect no interpolation */
     923       21996 :         mvr2r( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
     924             :     }
     925             : 
     926      345429 :     if ( st->bws_cnt == 0 && st->bws_cnt1 == 0 && st->prev_use_partial_copy == 0 && st->use_partial_copy == 0 )
     927             :     {
     928      344235 :         lsf_diff[0] = lsf_diff[LPC_SHB_ORDER - 1] = 0.5f;
     929     3098115 :         for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
     930             :         {
     931     2753880 :             lsf_diff[i] = lsf_shb[i] - lsf_shb[i - 1];
     932             :         }
     933             : 
     934      344235 :         a2rc( hBWE_TD->cur_sub_Aq + 1, refl, M );
     935             : 
     936      344235 :         tilt_para = 6.6956f * ( 1.0f + refl[0] ) * ( 1.0f + refl[0] ) - 3.8714f * ( 1.0f + refl[0] ) + 1.3041f;
     937      344235 :         if ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE )
     938             :         {
     939      192132 :             for ( i = 1; i < LPC_SHB_ORDER - 1; i++ )
     940             :             {
     941      170784 :                 hBWE_TD->prev_lsf_diff[i - 1] = 0.5f * lsf_diff[i];
     942             :             }
     943             :         }
     944             : 
     945      344235 :         if ( st->extl_brate <= FB_TBE_1k8 )
     946             :         {
     947      266178 :             if ( !( hBWE_TD->prev_tilt_para > 5.0f && ( st->coder_type == TRANSITION || tilt_para < 1.0f ) ) && !( ( hBWE_TD->prev_tilt_para < 3.0f && st->prev_coder_type >= VOICED ) && tilt_para > 5.0f ) )
     948             :             {
     949     2334582 :                 for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
     950             :                 {
     951     2075184 :                     if ( lsf_diff[i] < 0 || hBWE_TD->prev_lsf_diff[i - 1] <= 0 ) /* safety check in case of bit errors */
     952             :                     {
     953           0 :                         w[i] = 0;
     954           0 :                         st->BER_detect = 1;
     955             :                     }
     956             :                     else
     957             :                     {
     958     2075184 :                         w[i] = ( lsf_diff[i] < hBWE_TD->prev_lsf_diff[i - 1] ) ? min( max( 0.8f * lsf_diff[i] / hBWE_TD->prev_lsf_diff[i - 1], 0.5f ), 1.0f ) : min( max( 0.8f * hBWE_TD->prev_lsf_diff[i - 1] / lsf_diff[i], 0.5f ), 1.0f );
     959             :                     }
     960             :                 }
     961      259398 :                 w[0] = w[1];
     962      259398 :                 w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
     963             : 
     964     2853378 :                 for ( i = 0; i < LPC_SHB_ORDER; i++ )
     965             :                 {
     966     2593980 :                     lsp_temp[i] = lsp_shb_1[i] * ( 1.0f - w[i] ) + lsp_shb_2[i] * w[i];
     967             :                 }
     968             :             }
     969             :             else
     970             :             {
     971        6780 :                 mvr2r( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
     972             :             }
     973             : 
     974             :             /* convert from lsp to lsf */
     975      266178 :             lsp2lsf( lsp_temp, lsf_shb, LPC_SHB_ORDER, 1 );
     976             :         }
     977             : 
     978      344235 :         mvr2r( lsf_diff + 1, hBWE_TD->prev_lsf_diff, LPC_SHB_ORDER - 2 );
     979      344235 :         hBWE_TD->prev_tilt_para = tilt_para;
     980             :     }
     981             :     else
     982             :     {
     983        1194 :         mvr2r( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
     984             :     }
     985             : 
     986      345429 :     if ( st->extl_brate >= SWB_TBE_2k8 )
     987             :     {
     988             :         /* SHB LSP interpolation */
     989       78204 :         ptr_lsp_interp_coef = interpol_frac_shb;
     990      391020 :         for ( j = 0; j < 4; j++ )
     991             :         {
     992     3440976 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
     993             :             {
     994     3128160 :                 lsp_temp[i] = lsp_shb_1[i] * ( *ptr_lsp_interp_coef ) + lsp_shb_2[i] * ( *( ptr_lsp_interp_coef + 1 ) );
     995             :             }
     996      312816 :             ptr_lsp_interp_coef += 2;
     997             : 
     998             :             /* convert from lsp to lsf */
     999      312816 :             lsp2lsf( lsp_temp, lsp_temp, LPC_SHB_ORDER, 1 );
    1000             : 
    1001             :             /* convert lsf to lpc for SHB synthesis */
    1002      312816 :             lsp2a( lpc_shb_sf + j * ( LPC_SHB_ORDER + 1 ), lsp_temp, LPC_SHB_ORDER );
    1003      312816 :             lpc_shb_sf[j * ( LPC_SHB_ORDER + 1 )] = 1.0f;
    1004             :         }
    1005             :     }
    1006             : 
    1007             :     /* Save the SWB LSP values from current frame for interpolation */
    1008      345429 :     mvr2r( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp, LPC_SHB_ORDER );
    1009             : 
    1010             :     /* save the shb_ener and mixFactor values */
    1011      345429 :     hBWE_TD->prev3_shb_ener_sf = hBWE_TD->prev2_shb_ener_sf;
    1012      345429 :     hBWE_TD->prev2_shb_ener_sf = hBWE_TD->prev1_shb_ener_sf;
    1013      345429 :     hBWE_TD->prev1_shb_ener_sf = shb_ener_sf;
    1014      345429 :     hBWE_TD->prev_res_shb_gshape = shb_res_gshape[4];
    1015      345429 :     hBWE_TD->prev_mixFactors = mixFactors;
    1016             : 
    1017             :     /* SWB CNG/DTX - update memories */
    1018      345429 :     if ( st->hTdCngDec != NULL )
    1019             :     {
    1020      244428 :         mvr2r( st->hTdCngDec->lsp_shb_prev, st->hTdCngDec->lsp_shb_prev_prev, LPC_SHB_ORDER );
    1021      244428 :         mvr2r( lsf_shb, st->hTdCngDec->lsp_shb_prev, LPC_SHB_ORDER );
    1022             :     }
    1023             : 
    1024             :     /* convert LSPs back into LP coeffs */
    1025      345429 :     lsp2a( lpc_shb, lsf_shb, LPC_SHB_ORDER );
    1026      345429 :     lpc_shb[0] = 1.0;
    1027      345429 :     if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
    1028             :     {
    1029       12417 :         vind = (int16_t) ( mixFactors * ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
    1030             :     }
    1031             :     else
    1032             :     {
    1033      333012 :         vind = (int16_t) ( mixFactors * ( 1 << NUM_BITS_SHB_VF ) );
    1034             :     }
    1035             : 
    1036             :     /* Determine formant PF strength */
    1037      345429 :     formant_fac = swb_formant_fac( lpc_shb[1], &hBWE_TD->tilt_mem );
    1038      345429 :     if ( st->total_brate > ACELP_32k )
    1039             :     {
    1040      157950 :         for ( j = 0; j < 4; j++ )
    1041             :         {
    1042      126360 :             mvr2r( lpc_shb, &lpc_shb_sf[j * ( LPC_SHB_ORDER + 1 )], LPC_SHB_ORDER + 1 );
    1043             :         }
    1044             :     }
    1045             : 
    1046             :     /* From low band excitation, generate highband excitation */
    1047      345429 :     mvr2r( hBWE_TD->state_syn_shbexc, shaped_shb_excitation, L_SHB_LAHEAD );
    1048      345429 :     GenShapedSHBExcitation( shaped_shb_excitation + L_SHB_LAHEAD, lpc_shb, White_exc16k, hBWE_TD->mem_csfilt, hBWE_TD->mem_genSHBexc_filt_down_shb, hBWE_TD->state_lpc_syn, st->coder_type, bwe_exc_extended, hBWE_TD->bwe_seed, vf_modified, st->extl, &( hBWE_TD->tbe_demph ), &( hBWE_TD->tbe_premph ), lpc_shb_sf, &shb_ener_sf, shb_res_gshape, shb_res_dummy, &vind, formant_fac, hBWE_TD->fb_state_lpc_syn, &( hBWE_TD->fb_tbe_demph ), st->total_brate, st->prev_bfi, st->element_mode, st->flag_ACELP16k, nlExc16k, mixExc16k, st->extl_brate, MSFlag,
    1049             :                             NULL, &( hBWE_TD->prev_pow_exc16kWhtnd ), &( hBWE_TD->prev_mix_factor ), NULL, NULL );
    1050             : 
    1051             :     /* fill-in missing SHB excitation */
    1052      345429 :     if ( ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT ) && st->last_core_brate <= SID_2k40 )
    1053             :     {
    1054         507 :         mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, shaped_shb_excitation, L_SHB_LAHEAD );
    1055             :     }
    1056             : 
    1057      345429 :     if ( hStereoICBWE != NULL )
    1058             :     {
    1059       49284 :         mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef, L_FRAME16k );
    1060             :     }
    1061             : 
    1062      345429 :     if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
    1063             :     {
    1064     1665060 :         for ( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    1065             :         {
    1066             :             /* TD BWE post-processing */
    1067     1332048 :             PostShortTerm( &shaped_shb_excitation[L_SHB_LAHEAD + i], lpc_shb, &shaped_shb_excitationTemp[i], hBWE_TD->mem_stp_swb, hBWE_TD->ptr_mem_stp_swb, &( hBWE_TD->gain_prec_swb ), hBWE_TD->mem_zero_swb, formant_fac );
    1068             :         }
    1069             : 
    1070      333012 :         mvr2r( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k );
    1071             : 
    1072      333012 :         prev_pow = sum2_f( shaped_shb_excitation, L_SHB_LAHEAD + 10 );
    1073      333012 :         curr_pow = sum2_f( shaped_shb_excitation + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
    1074             : 
    1075      333012 :         if ( st->element_mode > EVS_MONO )
    1076             :         {
    1077             :             /* prevent too low values of energy */
    1078      328995 :             prev_pow = max( 0.00001f, prev_pow );
    1079      328995 :             curr_pow = max( 0.00001f, curr_pow );
    1080             :         }
    1081             : 
    1082      333012 :         if ( voice_factors[0] > 0.75f )
    1083             :         {
    1084       10080 :             curr_pow *= 0.25;
    1085             :         }
    1086             : 
    1087      333012 :         if ( prev_pow == 0 )
    1088             :         {
    1089           0 :             scale = 0;
    1090             :         }
    1091             :         else
    1092             :         {
    1093      333012 :             scale = (float) sqrt( curr_pow / prev_pow );
    1094             :         }
    1095             : 
    1096     6993252 :         for ( i = 0; i < L_SHB_LAHEAD; i++ )
    1097             :         {
    1098     6660240 :             shaped_shb_excitation[i] *= scale;
    1099             :         }
    1100     3663132 :         for ( ; i < L_SHB_LAHEAD + 10; i++ )
    1101             :         {
    1102     3330120 :             temp = ( i - 19 ) / 10.0f;
    1103     3330120 :             shaped_shb_excitation[i] *= ( temp * 1.0f + ( 1.0f - temp ) * scale );
    1104             :         }
    1105             :     }
    1106             :     else
    1107             :     {
    1108             :         /* reset the PF memories if the PF is not running */
    1109       12417 :         set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
    1110       12417 :         hBWE_TD->gain_prec_swb = 1.0f;
    1111       12417 :         set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
    1112             :     }
    1113             : 
    1114             :     /* Update SHB excitation */
    1115      345429 :     mvr2r( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD );
    1116      345429 :     l_subframe = L_FRAME16k / NUM_SHB_SUBGAINS;
    1117      345429 :     ener = EPSILON;
    1118     1727145 :     for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1119             :     {
    1120     1381716 :         ener_tmp[i] = EPSILON;
    1121   111918996 :         for ( j = 0; j < l_subframe; j++ )
    1122             :         {
    1123   110537280 :             ener_tmp[i] += shaped_shb_excitation[i * l_subframe + j] * shaped_shb_excitation[i * l_subframe + j] * 0.0125f;
    1124             :         }
    1125     1381716 :         ener_tmp[i] = (float) sqrt( ener_tmp[i] );
    1126     1381716 :         ener += ener_tmp[i];
    1127             :     }
    1128      345429 :     ener /= NUM_SHB_SUBGAINS;
    1129             : 
    1130             :     /* WB/SWB bandwidth switching */
    1131      345429 :     if ( st->bws_cnt > 0 )
    1132             :     {
    1133         312 :         ener *= 0.35f;
    1134             : 
    1135         312 :         if ( st->tilt_swb > 8 )
    1136             :         {
    1137           0 :             st->prev_fractive = 1;
    1138             :         }
    1139             : 
    1140         312 :         if ( is_fractive == 0 )
    1141             :         {
    1142         312 :             if ( st->tilt_wb > 1.0 )
    1143             :             {
    1144          96 :                 st->tilt_wb = 1.0f;
    1145             :             }
    1146         216 :             else if ( st->tilt_wb < 0.5 )
    1147             :             {
    1148         204 :                 st->tilt_wb = 0.5f;
    1149             :             }
    1150             : 
    1151         312 :             if ( st->prev_fractive == 1 && st->tilt_wb > 0.5 )
    1152             :             {
    1153           6 :                 st->tilt_wb = 0.5f;
    1154             :             }
    1155             :         }
    1156             :         else
    1157             :         {
    1158           0 :             if ( st->tilt_wb > 4 )
    1159             :             {
    1160           0 :                 if ( st->prev_fractive == 0 )
    1161             :                 {
    1162           0 :                     st->tilt_wb = 4;
    1163             :                 }
    1164             :                 else
    1165             :                 {
    1166           0 :                     st->tilt_wb = 8;
    1167             :                 }
    1168             :             }
    1169             :             else
    1170             :             {
    1171           0 :                 st->tilt_wb *= 2;
    1172             :             }
    1173             :         }
    1174             : 
    1175         312 :         if ( ener != 0 )
    1176             :         {
    1177         312 :             if ( ener * st->tilt_wb > st->enerLH )
    1178             :             {
    1179         312 :                 st->tilt_wb = 0.5f * st->enerLH / ener;
    1180             :             }
    1181           0 :             else if ( ener * st->tilt_wb < 0.05f * st->enerLH && is_fractive == 1 )
    1182             :             {
    1183           0 :                 st->tilt_wb = 0.25f * st->enerLH / ener;
    1184             :             }
    1185             : 
    1186         312 :             GainFrame_prevfrm = st->prev_ener_shb / ener;
    1187             :         }
    1188             :         else
    1189             :         {
    1190           0 :             GainFrame_prevfrm = 0;
    1191             :         }
    1192             : 
    1193         312 :         if ( is_fractive == 1 )
    1194             :         {
    1195           0 :             GainFrame = 8.0f * st->tilt_wb;
    1196             :         }
    1197             :         else
    1198             :         {
    1199         312 :             GainFrame = 2.0f * st->tilt_wb;
    1200             :         }
    1201             : 
    1202         312 :         if ( ( is_fractive & st->prev_fractive ) == 1 && GainFrame > GainFrame_prevfrm )
    1203             :         {
    1204           0 :             GainFrame = 0.2f * GainFrame + 0.8f * GainFrame_prevfrm;
    1205             :         }
    1206             :         else
    1207             :         {
    1208         312 :             if ( ( st->prev_enerLH < 2.0f * st->enerLH && st->prev_enerLH > 0.5f * st->enerLH ) && ( st->prev_enerLL < 2.0f * st->enerLL && st->prev_enerLL > 0.5f * st->enerLL ) && ( is_fractive ^ st->prev_fractive ) == 0 )
    1209             :             {
    1210           0 :                 GainFrame = 0.5f * GainFrame + 0.5f * GainFrame_prevfrm;
    1211             :             }
    1212             :             else
    1213             :             {
    1214         312 :                 if ( is_fractive == 0 && st->prev_fractive == 1 )
    1215             :                 {
    1216           6 :                     GainFrame = ( 1.0f - 0.1f * GainFrame ) * GainFrame + 0.1f * GainFrame * GainFrame_prevfrm;
    1217             :                 }
    1218             :                 else
    1219             :                 {
    1220         306 :                     GainFrame = 0.5f * GainFrame + 0.5f * GainFrame_prevfrm;
    1221             :                 }
    1222             :             }
    1223             :         }
    1224             : 
    1225         312 :         GainFrame *= ( (float) N_WS2N_FRAMES - (float) st->bws_cnt ) / (float) N_WS2N_FRAMES;
    1226             :     }
    1227             :     else
    1228             :     {
    1229      345117 :         if ( st->bws_cnt1 > 0 )
    1230             :         {
    1231         882 :             GainFrame *= (float) st->bws_cnt1 / (float) N_WS2N_FRAMES;
    1232             :         }
    1233             : 
    1234      345117 :         if ( st->nbLostCmpt == 1 )
    1235             :         {
    1236        5361 :             prev_ener_ratio = st->prev_ener_shb / ener;
    1237             : 
    1238        5361 :             if ( st->clas_dec != UNVOICED_CLAS && st->clas_dec != UNVOICED_TRANSITION && hBWE_TD->tilt_swb_fec < 8.0 &&
    1239        3753 :                  ( ( st->enerLL > 0.5f * st->prev_enerLL && st->enerLL < 2.0f * st->prev_enerLL ) || ( st->enerLH > 0.5f * st->prev_enerLH && st->enerLH < 2.0f * st->prev_enerLH ) ) )
    1240             :             {
    1241           0 :                 if ( prev_ener_ratio > 4.0f * GainFrame )
    1242             :                 {
    1243           0 :                     GainFrame = 0.4f * prev_ener_ratio + 0.6f * GainFrame;
    1244             :                 }
    1245           0 :                 else if ( prev_ener_ratio > 2.0f * GainFrame )
    1246             :                 {
    1247           0 :                     GainFrame = 0.8f * prev_ener_ratio + 0.2f * GainFrame;
    1248             :                 }
    1249             :                 else
    1250             :                 {
    1251           0 :                     GainFrame = 0.2f * prev_ener_ratio + 0.8f * GainFrame;
    1252             :                 }
    1253             : 
    1254           0 :                 if ( tilt_swb_fec > hBWE_TD->tilt_swb_fec )
    1255             :                 {
    1256           0 :                     GainFrame *= hBWE_TD->tilt_swb_fec > 0 ? ( min( 5.0f, tilt_swb_fec / hBWE_TD->tilt_swb_fec ) ) : 1.0f;
    1257             :                 }
    1258             :             }
    1259        5361 :             else if ( ( st->clas_dec != UNVOICED_CLAS || hBWE_TD->tilt_swb_fec > 8.0 ) && prev_ener_ratio > 4.0f * GainFrame && ( st->enerLL > 0.5f * st->prev_enerLL || st->enerLH > 0.5f * st->prev_enerLH ) )
    1260             :             {
    1261           0 :                 GainFrame = 0.2f * prev_ener_ratio + 0.8f * GainFrame;
    1262             :             }
    1263             :         }
    1264      339756 :         else if ( st->nbLostCmpt > 1 )
    1265             :         {
    1266        3471 :             prev_ener_ratio = st->prev_ener_shb / ener;
    1267        3471 :             if ( ( prev_ener_ratio > 4.0 * GainFrame ) && ( ( st->codec_mode == MODE1 && st->enerLL > st->prev_enerLL && st->enerLH > st->prev_enerLH ) || st->codec_mode == MODE2 ) )
    1268             :             {
    1269           0 :                 if ( tilt_swb_fec > 10.0f && hBWE_TD->tilt_swb_fec > 10.0f )
    1270             :                 {
    1271           0 :                     GainFrame = min( ( prev_ener_ratio * 0.8f + GainFrame * 0.2f ), 4.0f * GainFrame );
    1272             :                 }
    1273             :                 else
    1274             :                 {
    1275           0 :                     GainFrame = min( ( prev_ener_ratio * 0.5f + GainFrame * 0.5f ), 4.0f * GainFrame );
    1276             :                 }
    1277             :             }
    1278        3471 :             else if ( ( prev_ener_ratio > GainFrame ) && ( ( st->codec_mode == MODE1 && st->enerLL > st->prev_enerLL && st->enerLH > st->prev_enerLH ) || st->codec_mode == MODE2 ) )
    1279             :             {
    1280           0 :                 if ( tilt_swb_fec > 10.0f && hBWE_TD->tilt_swb_fec > 10.0f )
    1281             :                 {
    1282           0 :                     GainFrame = 0.5f * prev_ener_ratio + 0.5f * GainFrame;
    1283             :                 }
    1284             :                 else
    1285             :                 {
    1286           0 :                     GainFrame = 0.2f * prev_ener_ratio + 0.8f * GainFrame;
    1287             :                 }
    1288             :             }
    1289             :         }
    1290             :     }
    1291             : 
    1292      345429 :     st->prev_fractive = is_fractive;
    1293             : 
    1294             :     /* Adjust the subframe and frame gain of the synthesized shb signal */
    1295             :     /* Scale the shaped excitation */
    1296      345429 :     if ( st->L_frame == L_FRAME )
    1297             :     {
    1298      168834 :         pitch = 0.25f * sum_f( pitch_buf, 4 );
    1299             :     }
    1300             :     else
    1301             :     {
    1302      176595 :         pitch = 0.2f * sum_f( pitch_buf, 5 );
    1303             :     }
    1304             : 
    1305      345429 :     if ( ( ( st->extl_brate >= SWB_TBE_2k8 && st->prev_coder_type == st->coder_type && st->coder_type != UNVOICED ) || ( st->extl_brate < SWB_TBE_2k8 && ( st->prev_coder_type == st->coder_type || ( st->prev_coder_type == VOICED && st->coder_type == GENERIC ) || ( st->prev_coder_type == GENERIC && st->coder_type == VOICED ) ) ) ) && pitch > 70 && st->extl < FB_TBE && st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
    1306             :     {
    1307      489855 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1308             :         {
    1309      391884 :             GainShape_tmp[i] = GainShape[i * 4];
    1310             :         }
    1311             : 
    1312      489855 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1313             :         {
    1314      391884 :             if ( ener_tmp[i] * GainShape_tmp[i] > hBWE_TD->prev_ener * hBWE_TD->prev_GainShape )
    1315             :             {
    1316      229236 :                 GainShape_tmp[i] = 0.5f * ( hBWE_TD->prev_ener * hBWE_TD->prev_GainShape / ener_tmp[i] + GainShape_tmp[i] );
    1317             :             }
    1318      391884 :             hBWE_TD->prev_ener = ener_tmp[i];
    1319      391884 :             hBWE_TD->prev_GainShape = GainShape_tmp[i];
    1320             :         }
    1321             : 
    1322     1665507 :         for ( i = 0; i < NUM_SHB_SUBFR; i++ )
    1323             :         {
    1324     1567536 :             GainShape[i] = GainShape_tmp[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    1325             :         }
    1326             :     }
    1327             : 
    1328             :     /* Gain shape smoothing after quantization */
    1329      345429 :     if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
    1330             :     {
    1331       62085 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1332             :         {
    1333       49668 :             GainShape_tmp[i] = GainShape[i * NUM_SHB_SUBGAINS];
    1334             :         }
    1335             : 
    1336       12417 :         lls_interp_n( GainShape_tmp, NUM_SHB_SUBGAINS, &GainShape_tilt, &temp, 1 );
    1337             : 
    1338       12417 :         if ( vind >= 6 && fabs( GainShape_tilt ) < 0.12f )
    1339             :         {
    1340        7413 :             feedback = 0.3f;
    1341       37065 :             for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1342             :             {
    1343       29652 :                 GainShape[i] = ( 1 - feedback ) * GainShape[i * NUM_SHB_SUBGAINS] + feedback * GainShape_tmp[i];
    1344             :             }
    1345             : 
    1346      118608 :             for ( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
    1347             :             {
    1348      111195 :                 GainShape[i] = GainShape[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    1349             :             }
    1350             :         }
    1351             :     }
    1352             : 
    1353             :     /* fil-in missing memory */
    1354      345429 :     if ( ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT ) && st->last_core_brate <= SID_2k40 )
    1355             :     {
    1356       10647 :         for ( i = 0; i < L_SHB_LAHEAD; i++ )
    1357             :         {
    1358       10140 :             hBWE_TD->syn_overlap[i] = shaped_shb_excitation[i] * subwin_shb[L_SHB_LAHEAD - i];
    1359       10140 :             hBWE_TD->syn_overlap[i] *= window_shb[L_SHB_LAHEAD - 1 - i] * GainFrame;
    1360             :         }
    1361             :     }
    1362             : 
    1363      345429 :     ScaleShapedSHB( SHB_OVERLAP_LEN, shaped_shb_excitation, hBWE_TD->syn_overlap, GainShape, GainFrame, window_shb, subwin_shb );
    1364             : 
    1365      345429 :     if ( hStereoICBWE != NULL )
    1366             :     {
    1367       49284 :         mvr2r( lpc_shb, hStereoICBWE->lpSHBRef, LPC_SHB_ORDER + 1 );
    1368       49284 :         mvr2r( GainShape, hStereoICBWE->gshapeRef, NUM_SHB_SUBFR );
    1369       49284 :         hStereoICBWE->gFrameRef = GainFrame;
    1370             : 
    1371       49284 :         mvr2r( shaped_shb_excitation, hStereoICBWE->shbSynthRef, L_FRAME16k );
    1372             :     }
    1373             : 
    1374      345429 :     curr_frame_pow = sum2_f( shaped_shb_excitation, L_FRAME16k ) + 0.001f;
    1375      345429 :     curr_frame_pow = min( curr_frame_pow, FLT_MAX );
    1376             : 
    1377      345429 :     if ( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
    1378             :     {
    1379        5388 :         if ( ( curr_frame_pow > 2.0f * hBWE_TD->prev_swb_bwe_frame_pow ) &&
    1380        3096 :              ( curr_frame_pow < 30.0f * hBWE_TD->prev_swb_bwe_frame_pow ) &&
    1381        2226 :              st->prev_coder_type == UNVOICED )
    1382             :         {
    1383         117 :             scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1384         117 :             if ( curr_frame_pow == 0 )
    1385             :             {
    1386           0 :                 scale = 0;
    1387             :             }
    1388         117 :             temp = (float) pow( scale, 0.125f );
    1389             :         }
    1390             :         else
    1391             :         {
    1392        5271 :             scale = 1.0f;
    1393        5271 :             temp = 1.0f;
    1394             :         }
    1395             : 
    1396       48492 :         for ( j = 0; j < 8; j++ )
    1397             :         {
    1398       43104 :             GainShape[2 * j] *= scale;
    1399       43104 :             GainShape[2 * j + 1] *= scale;
    1400     1767264 :             for ( i = 0; i < L_FRAME16k / 8; i++ )
    1401             :             {
    1402     1724160 :                 shaped_shb_excitation[i + j * L_FRAME16k / 8] *= scale;
    1403             :             }
    1404             : 
    1405       43104 :             scale /= temp;
    1406             :         }
    1407             :     }
    1408             : 
    1409             :     /* adjust the FEC frame energy */
    1410      345429 :     if ( st->bfi )
    1411             :     {
    1412        8904 :         scale = 1.0f;
    1413        8904 :         temp = 1.0f;
    1414        8904 :         if ( st->nbLostCmpt == 1 )
    1415             :         {
    1416        5415 :             if ( curr_frame_pow > hBWE_TD->prev_swb_bwe_frame_pow && st->prev_coder_type != UNVOICED && st->last_good != UNVOICED_CLAS )
    1417             :             {
    1418         378 :                 scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1419         378 :                 if ( curr_frame_pow == 0 )
    1420             :                 {
    1421           0 :                     scale = 0;
    1422             :                 }
    1423         378 :                 temp = (float) pow( scale, 0.125f );
    1424             :             }
    1425        5037 :             else if ( curr_frame_pow < 0.5f * hBWE_TD->prev_swb_bwe_frame_pow && st->nbLostCmpt == 1 && ( st->enerLL > 0.5 * st->prev_enerLL || st->enerLH > 0.5 * st->prev_enerLH ) && ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS || hBWE_TD->tilt_swb_fec > 5.0f ) )
    1426             :             {
    1427           0 :                 scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1428           0 :                 if ( curr_frame_pow == 0 )
    1429             :                 {
    1430           0 :                     scale = 0;
    1431             :                 }
    1432           0 :                 temp = (float) pow( scale, 0.125f );
    1433             :             }
    1434             :         }
    1435        3489 :         else if ( st->nbLostCmpt > 1 )
    1436             :         {
    1437        3489 :             if ( curr_frame_pow > hBWE_TD->prev_swb_bwe_frame_pow )
    1438             :             {
    1439        1062 :                 scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1440        1062 :                 if ( curr_frame_pow == 0 )
    1441             :                 {
    1442           0 :                     scale = 0;
    1443             :                 }
    1444        1062 :                 temp = (float) pow( scale, 0.125f );
    1445             :             }
    1446        2427 :             else if ( curr_frame_pow < 0.5f * hBWE_TD->prev_swb_bwe_frame_pow && ( st->enerLL > 0.5 * st->prev_enerLL || st->enerLH > 0.5 * st->prev_enerLH ) && ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS || hBWE_TD->tilt_swb_fec > 5.0f ) )
    1447             :             {
    1448           0 :                 scale = (float) min( 2.0f, sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow ) );
    1449           0 :                 if ( curr_frame_pow == 0 )
    1450             :                 {
    1451           0 :                     scale = 0;
    1452             :                 }
    1453           0 :                 temp = (float) pow( scale, 0.125f );
    1454             :             }
    1455             :         }
    1456             : 
    1457       80136 :         for ( j = 0; j < 8; j++ )
    1458             :         {
    1459       71232 :             GainShape[2 * j] *= scale;
    1460       71232 :             GainShape[2 * j + 1] *= scale;
    1461     2920512 :             for ( i = 0; i < L_FRAME16k / 8; i++ )
    1462             :             {
    1463     2849280 :                 shaped_shb_excitation[i + j * L_FRAME16k / 8] *= scale;
    1464             :             }
    1465             : 
    1466       71232 :             scale /= temp;
    1467             :         }
    1468             :     }
    1469             : 
    1470      345429 :     hBWE_TD->prev_swb_bwe_frame_pow = curr_frame_pow;
    1471             : 
    1472      345429 :     st->prev_ener_shb = EPSILON;
    1473   110882709 :     for ( i = 0; i < L_FRAME16k; i++ )
    1474             :     {
    1475   110537280 :         st->prev_ener_shb += shaped_shb_excitation[i] * shaped_shb_excitation[i];
    1476             :     }
    1477      345429 :     st->prev_ener_shb = (float) sqrt( st->prev_ener_shb / L_FRAME16k );
    1478             : 
    1479      345429 :     if ( st->hBWE_FD != NULL )
    1480             :     {
    1481     5181435 :         for ( i = 0; i < SWB_FENV; i++ )
    1482             :         {
    1483     4836006 :             st->hBWE_FD->prev_SWB_fenv[i] = (float) sqrt( curr_frame_pow / L_FRAME16k );
    1484             :         }
    1485             :     }
    1486             : 
    1487             :     /* generate 32kHz SHB synthesis from 12.8(16)kHz signal */
    1488      345429 :     GenSHBSynth( shaped_shb_excitation, error, hBWE_TD->genSHBsynth_Hilbert_Mem, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local, st->L_frame, &( hBWE_TD->syn_dm_phase ) );
    1489             : 
    1490      345429 :     mvr2r( error + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth, L_SHB_TRANSITION_LENGTH );
    1491             : 
    1492             :     /* resample SHB synthesis (if needed) and scale down */
    1493      345429 :     synth_scale = ( st->codec_mode == MODE1 ) ? 0.9f : 1.f;
    1494             : 
    1495      345429 :     if ( st->output_Fs == 48000 )
    1496             :     {
    1497      247407 :         if ( st->extl == FB_TBE )
    1498             :         {
    1499    36400437 :             for ( i = 0; i < L_FRAME16k; i++ )
    1500             :             {
    1501    36287040 :                 White_exc16k[i] *= GainFrame * GainShape[NUM_SHB_SUBFR * i / L_FRAME16k];
    1502             :             }
    1503             :         }
    1504             : 
    1505   158587887 :         for ( i = 0; i < L_FRAME32k; i++ )
    1506             :         {
    1507   158340480 :             error[i] *= synth_scale;
    1508             :         }
    1509             : 
    1510      247407 :         interpolate_3_over_2_allpass( error, L_FRAME32k, synth, hBWE_TD->int_3_over_2_tbemem_dec );
    1511             :     }
    1512       98022 :     else if ( st->output_Fs == 32000 )
    1513             :     {
    1514    54503589 :         for ( i = 0; i < L_FRAME32k; i++ )
    1515             :         {
    1516    54418560 :             synth[i] = synth_scale * error[i];
    1517             :         }
    1518             :     }
    1519       12993 :     else if ( st->output_Fs == 16000 )
    1520             :     {
    1521     8328513 :         for ( i = 0; i < L_FRAME32k; i++ )
    1522             :         {
    1523     8315520 :             error[i] *= synth_scale;
    1524             :         }
    1525             : 
    1526       12993 :         Decimate_allpass_steep( error, hBWE_TD->mem_resamp_HB_32k, L_FRAME32k, synth );
    1527             :     }
    1528             : 
    1529             :     /* Update previous frame parameters for FEC */
    1530      345429 :     mvr2r( lsf_shb, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER );
    1531      345429 :     if ( st->codec_mode == MODE1 )
    1532             :     {
    1533      343617 :         hBWE_TD->GainFrame_prevfrm = GainFrame;
    1534      343617 :         hBWE_TD->tilt_swb_fec = tilt_swb_fec;
    1535             : 
    1536      343617 :         if ( !st->bfi )
    1537             :         {
    1538      334713 :             hBWE_TD->GainAttn = 1.0f;
    1539             :         }
    1540             :     }
    1541             :     else
    1542             :     {
    1543        1812 :         if ( !st->bfi )
    1544             :         {
    1545        1812 :             hBWE_TD->tilt_swb_fec = tilt_swb_fec;
    1546        1812 :             hBWE_TD->GainFrame_prevfrm = GainFrame; /* gain locking on lost frame */
    1547        1812 :             hBWE_TD->GainAttn = 1.0f;
    1548             :         }
    1549             :     }
    1550      345429 :     hBWE_TD->prev_ener = ener_tmp[NUM_SHB_SUBGAINS - 1];
    1551      345429 :     hBWE_TD->prev_GainShape = GainShape[NUM_SHB_SUBFR - 1];
    1552             : 
    1553      345429 :     return;
    1554             : }
    1555             : 
    1556             : /*-------------------------------------------------------------------*
    1557             :  * Dequant_lower_LSF()
    1558             :  *
    1559             :  * Dequantized the lower LSFs
    1560             :  *-------------------------------------------------------------------*/
    1561             : 
    1562      230955 : static void Dequant_lower_LSF(
    1563             :     const int16_t lsf_idx[], /* i  : LSF indices */
    1564             :     float lsf_q[]            /* o  : Quantized LSFs */
    1565             : )
    1566             : {
    1567             :     int16_t i;
    1568             : 
    1569      230955 :     lsf_q[0] = lsf_q_cb[0][lsf_idx[0]];
    1570     1154775 :     for ( i = 1; i < NUM_Q_LSF; i++ )
    1571             :     {
    1572      923820 :         lsf_q[i] = lsf_q_cb[i][lsf_idx[i]] + lsf_q[i - 1];
    1573             :     }
    1574             : 
    1575      230955 :     return;
    1576             : }
    1577             : 
    1578             : /*-------------------------------------------------------------------*
    1579             :  * Map_higher_LSF()
    1580             :  *
    1581             :  * Map the higher LSFs from the lower LSFs
    1582             :  *-------------------------------------------------------------------*/
    1583             : 
    1584      230955 : static void Map_higher_LSF(
    1585             :     float lsf_q[],        /* i/o: Quantized lower LSFs       */
    1586             :     const float m,        /* i  : Mirroring point            */
    1587             :     const float grid_in[] /* i  : Input LSF smoohthing grid  */
    1588             : )
    1589             : {
    1590             :     float lsf_map[NUM_MAP_LSF];
    1591             :     float grid[NUM_MAP_LSF];
    1592             :     float last_q_lsf;
    1593             :     float lsf_smooth[NUM_MAP_LSF];
    1594             :     float offset;
    1595             :     int16_t i;
    1596             :     float scale;
    1597             : 
    1598     1385730 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1599             :     {
    1600     1154775 :         lsf_map[i] = 2 * m - lsf_q[NUM_MAP_LSF - 1 - i];
    1601             :     }
    1602             : 
    1603      230955 :     if ( m > MAX_LSF / 2 )
    1604             :     {
    1605       45801 :         offset = lsf_map[0];
    1606       45801 :         scale = ( MAX_LSF - m ) / m;
    1607      274806 :         for ( i = 0; i < NUM_MAP_LSF; i++ )
    1608             :         {
    1609      229005 :             lsf_map[i] = ( lsf_map[i] - offset ) * scale + offset;
    1610             :         }
    1611             :     }
    1612             : 
    1613      230955 :     last_q_lsf = lsf_q[NUM_Q_LSF - 1];
    1614      230955 :     scale = MAX_LSF - last_q_lsf;
    1615             : 
    1616     1385730 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1617             :     {
    1618     1154775 :         grid[i] = grid_in[i] * scale + last_q_lsf;
    1619             :     }
    1620             : 
    1621     1385730 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1622             :     {
    1623     1154775 :         lsf_smooth[i] = ( 1 - grid_smoothing[i] ) * lsf_map[i] + grid_smoothing[i] * grid[i];
    1624             :     }
    1625             : 
    1626     1385730 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1627             :     {
    1628     1154775 :         lsf_q[NUM_Q_LSF + i] = lsf_smooth[i];
    1629             :     }
    1630             : 
    1631      230955 :     return;
    1632             : }
    1633             : 
    1634             : /*-------------------------------------------------------------------*
    1635             :  * Map_higher_LSF()
    1636             :  *
    1637             :  * Map the higher LSFs from the lower LSFs
    1638             :  *-------------------------------------------------------------------*/
    1639             : 
    1640      230955 : static void Dequant_mirror_point(
    1641             :     const float lsf_q[], /* i/o: Quantized lower LSFs   */
    1642             :     const int16_t m_idx, /* i  : Mirror point index     */
    1643             :     float *m             /* i  : Mirroring point        */
    1644             : )
    1645             : {
    1646      230955 :     *m = mirror_point_q_cb[m_idx] + lsf_q[NUM_Q_LSF - 1];
    1647             : 
    1648      230955 :     return;
    1649             : }
    1650             : 
    1651             : /*-------------------------------------------------------------------*
    1652             :  * dequantizeSHBparams()
    1653             :  *
    1654             :  * Dequantize super highband spectral envolope, temporal gains and frame gain
    1655             :  *-------------------------------------------------------------------*/
    1656             : 
    1657      356862 : static void dequantizeSHBparams(
    1658             :     Decoder_State *st,   /* i/o: decoder state structure                 */
    1659             :     const int16_t extl,  /* i  : extension layer                         */
    1660             :     int32_t extl_brate,  /* i  : extensiuon layer bitrate                */
    1661             :     float *Q_lsf,        /* o  : SHB LSF from de-quantization            */
    1662             :     float *Q_subgain,    /* o  : SHB subframe gains from de-quantization */
    1663             :     float *Q_framegrain, /* o  : SHB frame gain from de-quantization     */
    1664             :     int16_t *uv_flag,    /* o  : unvoiced flag                           */
    1665             :     float *Q_shb_ener_sf,
    1666             :     float *Q_shb_res_gshape,
    1667             :     float *Q_mixFactors,
    1668             :     int16_t *MSFlag )
    1669             : {
    1670             :     int16_t i, j, idxLSF, idxSubGain, idxFrameGain;
    1671             :     TD_BWE_DEC_HANDLE hBWE_TD;
    1672             :     float Q_combined_gains[NUM_SHB_SUBFR / 4];
    1673             :     float lsf_q[LPC_SHB_ORDER];
    1674             :     int16_t lsf_idx[NUM_Q_LSF];
    1675             :     int16_t m_idx, grid_idx;
    1676             :     float m;
    1677             :     int16_t idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
    1678             : 
    1679             :     UWord32 Idx_lvq;
    1680             :     int16_t Idx, Idx_pred;
    1681             :     int16_t num_bits_lvq;
    1682             :     float out[LATTICE_DIM];
    1683             :     const float *cb_stage;
    1684             :     int16_t predictor_bits;
    1685      356862 :     int16_t nbits = NUM_BITS_SHB_MSLVQ;
    1686             : 
    1687      356862 :     hBWE_TD = st->hBWE_TD;
    1688             : 
    1689             :     /* LSFs */
    1690      356862 :     if ( extl == WB_TBE )
    1691             :     {
    1692       20577 :         if ( extl_brate == WB_TBE_0k35 )
    1693             :         {
    1694        7317 :             if ( st->codec_mode == MODE2 )
    1695             :             {
    1696           0 :                 idxFrameGain = hBWE_TD->gFrame_WB;
    1697           0 :                 idxLSF = hBWE_TD->lsf_WB;
    1698             :             }
    1699             :             else
    1700             :             {
    1701        7317 :                 idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FrameGain_LBR_WB );
    1702        7317 :                 idxLSF = get_next_indice( st, NUM_BITS_LBR_WB_LSF );
    1703             :             }
    1704             : 
    1705        7317 :             mvr2r( lbr_wb_bwe_lsfvq_cbook_2bit + idxLSF * LPC_SHB_ORDER_LBR_WB, Q_lsf, LPC_SHB_ORDER_LBR_WB );
    1706        7317 :             set_f( Q_subgain, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR / 2 );
    1707        7317 :             mvr2r( SHBCB_FrameGain16 + idxFrameGain, Q_framegrain, 1 );
    1708             :         }
    1709             :         else
    1710             :         {
    1711             :             /* read the information about UNVOICED frame */
    1712       13260 :             *uv_flag = get_next_indice( st, 1 );
    1713             : 
    1714       13260 :             idxSubGain = get_next_indice( st, NUM_BITS_SHB_SUBGAINS );
    1715       13260 :             idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FrameGain );
    1716       13260 :             idxLSF = get_next_indice( st, NUM_BITS_WB_LSF );
    1717             : 
    1718       13260 :             mvr2r( wb_bwe_lsfvq_cbook_8bit + idxLSF * LPC_SHB_ORDER_WB, Q_lsf, LPC_SHB_ORDER_WB );
    1719       13260 :             mvr2r( HBCB_SubGain5bit + idxSubGain * NUM_SHB_SUBFR / 4, Q_combined_gains, NUM_SHB_SUBFR / 4 );
    1720             : 
    1721       66300 :             for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    1722             :             {
    1723       53040 :                 Q_combined_gains[i] = (float) pow( 10.0f, Q_combined_gains[i] / 20.0f );
    1724             :             }
    1725             : 
    1726       66300 :             for ( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
    1727             :             {
    1728       53040 :                 Q_subgain[i] = Q_combined_gains[i / 2];
    1729       53040 :                 Q_subgain[i + 1] = Q_combined_gains[i / 2];
    1730             :             }
    1731             : 
    1732             :             /* frame gain */
    1733       13260 :             mvr2r( SHBCB_FrameGain64 + idxFrameGain, Q_framegrain, 1 );
    1734             :         }
    1735             :     }
    1736             :     else
    1737             :     {
    1738      336285 :         if ( st->codec_mode == MODE2 )
    1739             :         {
    1740        1812 :             idxSubGain = hBWE_TD->idxSubGains;
    1741        1812 :             idxFrameGain = hBWE_TD->idxFrameGain;
    1742             :         }
    1743             :         else
    1744             :         {
    1745      334473 :             idxSubGain = get_next_indice( st, NUM_BITS_SHB_SUBGAINS );
    1746      334473 :             if ( st->extl_brate == SWB_TBE_1k75 )
    1747             :             {
    1748        9384 :                 idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FRAMEGAIN_1k75 );
    1749             :             }
    1750             :             else
    1751             :             {
    1752      325089 :                 idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FRAMEGAIN );
    1753             :             }
    1754             :         }
    1755             : 
    1756             :         /* Multi Source Flag */
    1757      336285 :         if ( st->element_mode >= IVAS_CPE_DFT && !( st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) )
    1758             :         {
    1759       66903 :             *MSFlag = get_next_indice( st, STEREO_ICBWE_MSFLAG_BITS );
    1760             :         }
    1761             :         else
    1762             :         {
    1763      269382 :             *MSFlag = 0;
    1764             :         }
    1765             : 
    1766      336285 :         if ( st->extl_brate >= SWB_TBE_2k8 )
    1767             :         {
    1768       75321 :             if ( st->codec_mode == MODE2 )
    1769             :             {
    1770        1812 :                 idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
    1771             :             }
    1772             :             else
    1773             :             {
    1774       73509 :                 idx_shb_fr_gain = get_next_indice( st, NUM_BITS_SHB_ENER_SF );
    1775             :             }
    1776       75321 :             *Q_shb_ener_sf = usdequant( idx_shb_fr_gain, 0, 0.042f );
    1777       75321 :             *Q_shb_ener_sf = (float) pow( 10.0, *Q_shb_ener_sf );
    1778             : 
    1779      451926 :             for ( i = 0; i < 5; i++ )
    1780             :             {
    1781      376605 :                 if ( st->codec_mode == MODE2 )
    1782             :                 {
    1783        9060 :                     idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
    1784             :                 }
    1785             :                 else
    1786             :                 {
    1787      367545 :                     idx_res_gs[i] = get_next_indice( st, NUM_BITS_SHB_RES_GS );
    1788             :                 }
    1789      376605 :                 Q_shb_res_gshape[i] = usdequant( idx_res_gs[i], 0.125f, 0.125f );
    1790             :             }
    1791             : 
    1792       75321 :             if ( st->codec_mode == MODE2 )
    1793             :             {
    1794        1812 :                 idx_mixFac = hBWE_TD->idx_mixFac;
    1795             :             }
    1796             :             else
    1797             :             {
    1798       73509 :                 idx_mixFac = get_next_indice( st, NUM_BITS_SHB_VF );
    1799             :             }
    1800       75321 :             *Q_mixFactors = usdequant( idx_mixFac, 0.125f, 0.125f );
    1801             :         }
    1802             :         else
    1803             :         {
    1804      260964 :             *Q_shb_ener_sf = 0;
    1805      260964 :             if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
    1806             :             {
    1807       11934 :                 idx_mixFac = get_next_indice( st, NUM_BITS_SHB_VF );
    1808       11934 :                 *Q_mixFactors = usdequant( idx_mixFac, 0.0f, 1.0f / ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
    1809             :             }
    1810             :             else
    1811             :             {
    1812      249030 :                 *Q_mixFactors = 0;
    1813             :             }
    1814      260964 :             set_f( Q_shb_res_gshape, 0, 5 );
    1815             :         }
    1816             : 
    1817      336285 :         if ( ( st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 ) && st->codec_mode == MODE1 )
    1818             :         {
    1819       95946 :             set_s( lsf_idx, 0, 5 );
    1820       95946 :             lsf_idx[0] = get_next_indice( st, 8 );
    1821       95946 :             grid_idx = 0;
    1822       95946 :             m_idx = 0;
    1823             : 
    1824       95946 :             mvr2r( swb_tbe_lsfvq_cbook_8b + lsf_idx[0] * LPC_SHB_ORDER, Q_lsf, LPC_SHB_ORDER );
    1825             :         }
    1826      240339 :         else if ( st->rf_flag == 0 && !( ( st->element_mode == EVS_MONO && st->total_brate == ACELP_9k60 ) || ( st->element_mode == EVS_MONO && ( st->total_brate == 0 ) && ( ( st->last_total_brate == ACELP_9k60 ) || ( st->last_total_brate == ACELP_13k20 && st->rf_flag_last ) ) ) ) )
    1827             :         {
    1828             :             /* LSFs */
    1829      240339 :             if ( st->extl_brate == SWB_TBE_1k75 )
    1830             :             {
    1831             :                 /* read multi-stage LVQ quantizer */
    1832        9384 :                 if ( nbits >= 19 )
    1833             :                 {
    1834        9384 :                     cb_stage = cb_LSF_BWE[0];
    1835             :                 }
    1836             :                 else
    1837             :                 {
    1838           0 :                     cb_stage = cb_LSF_BWE[1];
    1839             :                 }
    1840             : 
    1841        9384 :                 set_zero( lsf_q, LPC_SHB_ORDER );
    1842             : 
    1843             :                 /* VQ part */
    1844        9384 :                 num_bits_lvq = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3];
    1845        9384 :                 Idx = get_next_indice( st, num_bits_lvq );
    1846        9384 :                 v_add( lsf_q, cb_stage + Idx * 6, lsf_q, 6 );
    1847             : 
    1848             :                 /* MSLVQ part */
    1849        9384 :                 num_bits_lvq = nbits - num_bits_lvq - config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
    1850        9384 :                 predictor_bits = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
    1851        9384 :                 Idx_pred = 0;
    1852             : 
    1853        9384 :                 if ( num_bits_lvq == 16 )
    1854             :                 {
    1855             :                     /* MSLVQ part */
    1856           0 :                     Idx_lvq = get_next_indice( st, num_bits_lvq - 1 ) + ( ( get_next_indice( st, 1 ) ) << 15 );
    1857           0 :                     deindex_lvq_SHB( Idx_lvq, out, num_bits_lvq, 0 );
    1858             :                 }
    1859             :                 else
    1860             :                 {
    1861             :                     /* MSLVQ part */
    1862        9384 :                     Idx_lvq = get_next_indice( st, num_bits_lvq );
    1863        9384 :                     deindex_lvq_SHB( Idx_lvq, out, num_bits_lvq, ( nbits < 19 ) );
    1864             :                 }
    1865             : 
    1866             :                 /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
    1867        9384 :                 v_add( lsf_q, out, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
    1868             : 
    1869             :                 /* predict last 2 components */
    1870        9384 :                 if ( predictor_bits == 0 )
    1871             :                 {
    1872           0 :                     lsf_q[LATTICE_DIM] = dotp( lsf_q, LastCoefPred_0bit, LATTICE_DIM );
    1873           0 :                     lsf_q[LATTICE_DIM + 1] = dotp( lsf_q, &LastCoefPred_0bit[LATTICE_DIM + 1], LATTICE_DIM );
    1874             :                 }
    1875             :                 else
    1876             :                 {
    1877        9384 :                     Idx_pred = get_next_indice( st, 1 );
    1878             : 
    1879        9384 :                     lsf_q[LATTICE_DIM] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * Idx_pred], LATTICE_DIM );
    1880        9384 :                     lsf_q[LATTICE_DIM + 1] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * Idx_pred + LATTICE_DIM + 1], LATTICE_DIM );
    1881             :                 }
    1882             : 
    1883        9384 :                 if ( nbits < NUM_BITS_SHB_MSLVQ )
    1884             :                 {
    1885           0 :                     Idx_pred = get_next_indice( st, NUM_BITS_SHB_MSLVQ - nbits );
    1886             :                 }
    1887             : 
    1888        9384 :                 v_add( SHB_LSF_mean, lsf_q, lsf_q, LPC_SHB_ORDER );
    1889        9384 :                 v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
    1890             :             }
    1891             :             else
    1892             :             {
    1893      230955 :                 if ( extl_brate == SWB_TBE_1k6 || extl_brate == FB_TBE_1k8 || extl_brate == SWB_TBE_2k8 || extl_brate == FB_TBE_3k0 )
    1894             :                 {
    1895     1385730 :                     for ( i = 0; i < NUM_Q_LSF; i++ )
    1896             :                     {
    1897     1154775 :                         if ( st->codec_mode == MODE2 )
    1898             :                         {
    1899        9060 :                             lsf_idx[i] = hBWE_TD->lsf_idx[i];
    1900             :                         }
    1901             :                         else
    1902             :                         {
    1903     1145715 :                             lsf_idx[i] = get_next_indice( st, lsf_q_num_bits[i] );
    1904             :                         }
    1905             :                     }
    1906             :                 }
    1907             : 
    1908      230955 :                 Dequant_lower_LSF( lsf_idx, lsf_q );
    1909             : 
    1910      230955 :                 if ( st->codec_mode == MODE2 )
    1911             :                 {
    1912        1812 :                     m_idx = hBWE_TD->m_idx;
    1913        1812 :                     grid_idx = hBWE_TD->grid_idx;
    1914             :                 }
    1915             :                 else
    1916             :                 {
    1917      229143 :                     m_idx = get_next_indice( st, MIRROR_POINT_BITS );
    1918      229143 :                     grid_idx = get_next_indice( st, NUM_LSF_GRID_BITS );
    1919             :                 }
    1920             : 
    1921      230955 :                 Dequant_mirror_point( lsf_q, m_idx, &m );
    1922             : 
    1923             :                 /* safety check in case of bit errors */
    1924      230955 :                 if ( m > MAX_LSF )
    1925             :                 {
    1926           0 :                     st->BER_detect = 1;
    1927           0 :                     m = MAX_LSF;
    1928             :                 }
    1929             : 
    1930      230955 :                 Map_higher_LSF( lsf_q, m, lsf_grid[grid_idx] );
    1931             :             }
    1932     2643729 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
    1933             :             {
    1934             :                 /* safety check in case of bit errors */
    1935     2403390 :                 if ( lsf_q[LPC_SHB_ORDER - 1 - i] > MAX_LSF )
    1936             :                 {
    1937           0 :                     st->BER_detect = 1;
    1938           0 :                     lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF;
    1939             :                 }
    1940     2403390 :                 Q_lsf[i] = 0.5f - lsf_q[LPC_SHB_ORDER - 1 - i];
    1941             :             }
    1942             :         }
    1943             :         else
    1944             :         {
    1945           0 :             set_s( lsf_idx, 0, 5 );
    1946           0 :             mvs2s( hBWE_TD->lsf_idx, lsf_idx, 5 );
    1947           0 :             grid_idx = 0;
    1948           0 :             m_idx = 0;
    1949           0 :             mvr2r( swb_tbe_lsfvq_cbook_8b + lsf_idx[0] * LPC_SHB_ORDER, Q_lsf, LPC_SHB_ORDER );
    1950             :         }
    1951             : 
    1952      336285 :         space_lsfs( Q_lsf, LPC_SHB_ORDER );
    1953             : 
    1954             :         /* Dequantize subgain indices */
    1955      336285 :         j = idxSubGain * NUM_SHB_SUBGAINS;
    1956             : 
    1957     1681425 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1958             :         {
    1959     1345140 :             Q_subgain[i] = (float) pow( 10.0, SHBCB_SubGain5bit[j++] );
    1960             :         }
    1961             : 
    1962     5716845 :         for ( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    1963             :         {
    1964     5380560 :             Q_subgain[i] = Q_subgain[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    1965             :         }
    1966             : 
    1967             :         /* Frame gain */
    1968      336285 :         if ( st->extl_brate == SWB_TBE_1k75 )
    1969             :         {
    1970        9384 :             *Q_framegrain = usdequant( idxFrameGain, SHB_GAIN_QLOW_1k75, SHB_GAIN_QDELTA_1k75 );
    1971             :         }
    1972             :         else
    1973             :         {
    1974      326901 :             *Q_framegrain = usdequant( idxFrameGain, SHB_GAIN_QLOW, SHB_GAIN_QDELTA );
    1975             :         }
    1976             : 
    1977      336285 :         *Q_framegrain = (float) pow( 10.0, *Q_framegrain );
    1978             :     }
    1979             : 
    1980      356862 :     return;
    1981             : }
    1982             : 
    1983             : 
    1984             : /*-------------------------------------------------------------------*
    1985             :  * fb_tbe_dec()
    1986             :  *
    1987             :  * FB TBE decoder, 14(resp. 15.5) - 20 kHz band decoding module
    1988             :  *-------------------------------------------------------------------*/
    1989             : 
    1990      129792 : void fb_tbe_dec(
    1991             :     Decoder_State *st,         /* i/o: decoder state structure         */
    1992             :     const float fb_exc[],      /* i  : FB excitation from the SWB part */
    1993             :     float *hb_synth,           /* o  : high-band synthesis             */
    1994             :     float *fb_synth_ref,       /* o  : high-band synthesis 16-20 kHz   */
    1995             :     const int16_t output_frame /* i  : output frame length             */
    1996             : )
    1997             : {
    1998             :     int16_t i;
    1999             :     TD_BWE_DEC_HANDLE hBWE_TD;
    2000      129792 :     float ratio = 0, fb_exc_energy = 0;
    2001             :     float fb_synth[L_FRAME48k];
    2002             : 
    2003      129792 :     hBWE_TD = st->hBWE_TD;
    2004             : 
    2005      129792 :     if ( output_frame == L_FRAME48k )
    2006             :     {
    2007             :         /* decode FB slope information */
    2008      113397 :         if ( st->extl == FB_TBE && !st->bfi )
    2009             :         {
    2010      110682 :             if ( st->codec_mode == MODE2 )
    2011             :             {
    2012           0 :                 i = hBWE_TD->idxGain;
    2013             :             }
    2014             :             else
    2015             :             {
    2016      110682 :                 i = get_next_indice( st, NUM_BITS_FB_FRAMEGAIN_TBE );
    2017             :             }
    2018      110682 :             ratio = (float) ( 1 << i );
    2019             :         }
    2020        2715 :         else if ( st->extl == FB_TBE && st->bfi )
    2021             :         {
    2022        2715 :             ratio = hBWE_TD->prev_fbbwe_ratio;
    2023             :         }
    2024             :     }
    2025             :     else
    2026             :     {
    2027             :         /* update the position in the bitstream - needed in IVAS */
    2028       16395 :         if ( !st->bfi )
    2029             :         {
    2030       15933 :             st->next_bit_pos += NUM_BITS_FB_FRAMEGAIN;
    2031             :         }
    2032             : 
    2033       16395 :         return;
    2034             :     }
    2035             : 
    2036      113397 :     fb_exc_energy = sum2_f( fb_exc, L_FRAME16k );
    2037             : 
    2038             :     /* FB TBE synthesis */
    2039      113397 :     synthesise_fb_high_band( fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio ), hBWE_TD->fbbwe_hpf_mem );
    2040             : 
    2041      113397 :     if ( st->element_mode >= IVAS_CPE_DFT && st->idchan == 0 )
    2042             :     {
    2043       11928 :         mvr2r( fb_synth, fb_synth_ref, L_FRAME48k );
    2044             :     }
    2045             : 
    2046             :     /* add the fb_synth component to the hb_synth component */
    2047      113397 :     v_add( hb_synth, fb_synth, hb_synth, L_FRAME48k );
    2048             : 
    2049      113397 :     return;
    2050             : }
    2051             : 
    2052             : 
    2053             : /*---------------------------------------------------------------------*
    2054             :  * tbe_read_bitstream()
    2055             :  *
    2056             :  * Read TBE bitstream and populate the parameters for TD-BWE decoder.
    2057             :  *---------------------------------------------------------------------*/
    2058             : 
    2059        1812 : void tbe_read_bitstream(
    2060             :     Decoder_State *st /* i/o: decoder state structure         */
    2061             : )
    2062             : {
    2063             :     int16_t i;
    2064             :     TD_BWE_DEC_HANDLE hBWE_TD;
    2065             : 
    2066        1812 :     hBWE_TD = st->hBWE_TD;
    2067             : 
    2068        1812 :     if ( ( st->rf_flag || st->total_brate == ACELP_9k60 ) && st->bwidth == WB )
    2069             :     {
    2070             :         /* WB LSF */
    2071           0 :         hBWE_TD->lsf_WB = get_next_indice( st, NUM_BITS_LBR_WB_LSF );
    2072             : 
    2073             :         /* WB frame gain */
    2074           0 :         hBWE_TD->gFrame_WB = get_next_indice( st, NUM_BITS_SHB_FrameGain_LBR_WB );
    2075             :     }
    2076        1812 :     else if ( st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_32k && ( st->bwidth == SWB || st->bwidth == FB ) )
    2077             :     {
    2078        1812 :         if ( st->rf_flag == 0 && st->total_brate > ACELP_9k60 )
    2079             :         {
    2080       10872 :             for ( i = 0; i < NUM_Q_LSF; i++ )
    2081             :             {
    2082        9060 :                 hBWE_TD->lsf_idx[i] = get_next_indice( st, lsf_q_num_bits[i] );
    2083             :             }
    2084        1812 :             hBWE_TD->m_idx = get_next_indice( st, MIRROR_POINT_BITS );
    2085        1812 :             hBWE_TD->grid_idx = get_next_indice( st, NUM_LSF_GRID_BITS );
    2086             :         }
    2087             :         else
    2088             :         {
    2089           0 :             hBWE_TD->lsf_idx[0] = get_next_indice( st, 8 );
    2090           0 :             hBWE_TD->m_idx = 0;
    2091           0 :             hBWE_TD->grid_idx = 0;
    2092             :         }
    2093             : 
    2094             :         /* shape gains */
    2095        1812 :         hBWE_TD->idxSubGains = get_next_indice( st, NUM_BITS_SHB_SUBGAINS );
    2096             : 
    2097             :         /* frame gain */
    2098        1812 :         hBWE_TD->idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FRAMEGAIN );
    2099             : 
    2100        1812 :         if ( st->total_brate >= ACELP_24k40 )
    2101             :         {
    2102             :             /* sub frame energy*/
    2103        1812 :             hBWE_TD->idx_shb_fr_gain = get_next_indice( st, NUM_BITS_SHB_ENER_SF );
    2104             : 
    2105             :             /* gain shapes residual */
    2106       10872 :             for ( i = 0; i < NB_SUBFR16k; i++ )
    2107             :             {
    2108        9060 :                 hBWE_TD->idx_res_gs[i] = get_next_indice( st, NUM_BITS_SHB_RES_GS );
    2109             :             }
    2110             : 
    2111             :             /* voicing factor */
    2112        1812 :             hBWE_TD->idx_mixFac = get_next_indice( st, NUM_BITS_SHB_VF );
    2113             :         }
    2114             : 
    2115        1812 :         if ( st->tec_tfa == 1 )
    2116             :         {
    2117        1812 :             st->tec_flag = get_next_indice( st, BITS_TEC );
    2118        1812 :             st->tfa_flag = get_next_indice( st, BITS_TFA );
    2119             : 
    2120        1812 :             if ( st->tfa_flag && st->tec_flag )
    2121             :             {
    2122          12 :                 st->tec_flag = 2;
    2123          12 :                 st->tfa_flag = 0;
    2124             :             }
    2125             :         }
    2126             :         else
    2127             :         {
    2128           0 :             st->tec_flag = 0;
    2129           0 :             st->tfa_flag = 0;
    2130             :         }
    2131             :     }
    2132             : 
    2133        1812 :     if ( st->bwidth == FB )
    2134             :     {
    2135           0 :         hBWE_TD->idxGain = get_next_indice( st, 4 );
    2136             :     }
    2137             : 
    2138        1812 :     return;
    2139             : }
    2140             : 
    2141             : 
    2142             : /*---------------------------------------------------------------------*
    2143             :  * GenTransition()
    2144             :  *
    2145             :  * Generate a highband transition signal from the gain shape overlap
    2146             :  * buffer to fill the gap caused by the delay alignment buffer when
    2147             :  * switching from TBE to IGF
    2148             :  *---------------------------------------------------------------------*/
    2149             : 
    2150       19272 : void GenTransition(
    2151             :     TD_BWE_DEC_HANDLE hBWE_TD,  /* i/o: TD BWE data handle                   */
    2152             :     float *outputHB,            /* o  : synthesized HB transitions signal    */
    2153             :     const int32_t output_Fs,    /* i  : output sampling rate                 */
    2154             :     const int16_t element_mode, /* i  : element mode                         */
    2155             :     const int16_t L_frame,      /* i  : ACELP frame length                   */
    2156             :     const int16_t rf_flag,      /* i  : RF flag                              */
    2157             :     const int32_t total_brate   /* i  : total bitrate                        */
    2158             : )
    2159             : {
    2160             :     int16_t i, length;
    2161             :     float syn_overlap_32k[2 * SHB_OVERLAP_LEN];
    2162             : 
    2163             :     /* set targeted length of transition signal */
    2164       19272 :     length = 2 * NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
    2165             : 
    2166             :     /* upsample overlap snippet */
    2167       19272 :     Interpolate_allpass_steep( hBWE_TD->syn_overlap, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local, SHB_OVERLAP_LEN, syn_overlap_32k );
    2168             : 
    2169             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    2170       19272 :     if ( ( element_mode == EVS_MONO && ( rf_flag || total_brate == ACELP_9k60 ) ) || ( element_mode > EVS_MONO && L_frame == L_FRAME ) )
    2171             :     {
    2172        6984 :         flip_and_downmix_generic( syn_overlap_32k, syn_overlap_32k, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem, hBWE_TD->genSHBsynth_Hilbert_Mem + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ), &( hBWE_TD->syn_dm_phase ) );
    2173             :     }
    2174             :     else
    2175             :     {
    2176      503808 :         for ( i = 0; i < 2 * SHB_OVERLAP_LEN; i++ )
    2177             :         {
    2178      491520 :             syn_overlap_32k[i] = ( ( i % 2 ) == 0 ) ? ( -syn_overlap_32k[i] ) : ( syn_overlap_32k[i] );
    2179             :         }
    2180             :     }
    2181             : 
    2182             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    2183      790152 :     for ( i = 0; i < 2 * L_SHB_LAHEAD; i++ )
    2184             :     {
    2185      770880 :         outputHB[i] = window_shb_32k[i] * hBWE_TD->old_tbe_synth[L_SHB_TRANSITION_LENGTH - 1 - i] + window_shb_32k[2 * L_SHB_LAHEAD - 1 - i] * syn_overlap_32k[i];
    2186             :     }
    2187             : 
    2188             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    2189     3154038 :     for ( ; i < length; i++ )
    2190             :     {
    2191     3134766 :         outputHB[i] = hBWE_TD->old_tbe_synth[L_SHB_TRANSITION_LENGTH - 1 - i];
    2192             :     }
    2193             : 
    2194       19272 :     if ( output_Fs == 48000 )
    2195             :     {
    2196       14643 :         interpolate_3_over_2_allpass( outputHB, length, outputHB, hBWE_TD->int_3_over_2_tbemem_dec );
    2197             :     }
    2198        4629 :     else if ( output_Fs == 16000 )
    2199             :     {
    2200         408 :         Decimate_allpass_steep( outputHB, hBWE_TD->mem_resamp_HB_32k, L_FRAME32k, outputHB );
    2201             :     }
    2202             : 
    2203       19272 :     return;
    2204             : }
    2205             : 
    2206             : /*---------------------------------------------------------------------*
    2207             :  * GenTransition_WB()
    2208             :  *
    2209             :  *
    2210             :  *---------------------------------------------------------------------*/
    2211             : 
    2212         360 : void GenTransition_WB(
    2213             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                  */
    2214             :     float *outputHB,           /* o  : synthesized HB transitions signal   */
    2215             :     const int32_t output_Fs    /* i  : output sampling rate                */
    2216             : )
    2217             : {
    2218             :     int16_t i, length;
    2219             :     float speech_buf_16k1[SHB_OVERLAP_LEN], speech_buf_16k2[2 * SHB_OVERLAP_LEN];
    2220             :     float upsampled_synth[L_FRAME48k];
    2221             : 
    2222             :     /* set targeted length of transition signal */
    2223         360 :     length = 2 * NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
    2224             : 
    2225             :     /* upsample overlap snippet */
    2226         360 :     Interpolate_allpass_steep( hBWE_TD->syn_overlap, hBWE_TD->state_lsyn_filt_shb, SHB_OVERLAP_LEN / 2, speech_buf_16k1 );
    2227         360 :     Interpolate_allpass_steep( speech_buf_16k1, hBWE_TD->state_lsyn_filt_dwn_shb, SHB_OVERLAP_LEN, speech_buf_16k2 );
    2228             : 
    2229             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    2230        7560 :     for ( i = 0; i < SHB_OVERLAP_LEN; i++ )
    2231             :     {
    2232        7200 :         speech_buf_16k2[i] = ( ( i % 2 ) == 0 ) ? ( -speech_buf_16k2[i] ) : ( speech_buf_16k2[i] );
    2233             :     }
    2234             : 
    2235             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    2236        7560 :     for ( i = 0; i < L_SHB_LAHEAD; i++ )
    2237             :     {
    2238        7200 :         outputHB[i] = window_shb[i] * hBWE_TD->old_tbe_synth[L_SHB_TRANSITION_LENGTH - 1 - i] + window_shb[L_SHB_LAHEAD - 1 - i] * speech_buf_16k2[i];
    2239        7200 :         outputHB[i] *= 0.65f;
    2240             :     }
    2241             : 
    2242             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    2243       40668 :     for ( ; i < length; i++ )
    2244             :     {
    2245       40308 :         outputHB[i] = hBWE_TD->old_tbe_synth[L_SHB_TRANSITION_LENGTH - 1 - i];
    2246       40308 :         outputHB[i] *= 0.65f;
    2247             :     }
    2248             : 
    2249             :     /* upsampling if necessary */
    2250         360 :     if ( output_Fs == 32000 )
    2251             :     {
    2252          30 :         Interpolate_allpass_steep( outputHB, hBWE_TD->mem_resamp_HB, L_FRAME16k, upsampled_synth );
    2253          30 :         mvr2r( upsampled_synth, outputHB, L_FRAME32k );
    2254             :     }
    2255         330 :     else if ( output_Fs == 48000 )
    2256             :     {
    2257         126 :         interpolate_3_over_1_allpass( outputHB, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB );
    2258         126 :         mvr2r( upsampled_synth, outputHB, L_FRAME48k );
    2259             :     }
    2260             : 
    2261         360 :     return;
    2262             : }
    2263             : 
    2264             : 
    2265             : /*---------------------------------------------------------------------*
    2266             :  * void TBEreset_dec()
    2267             :  *
    2268             :  *
    2269             :  *---------------------------------------------------------------------*/
    2270             : 
    2271      732774 : void TBEreset_dec(
    2272             :     Decoder_State *st /* i/o: decoder state structure     */
    2273             : )
    2274             : {
    2275             :     TD_BWE_DEC_HANDLE hBWE_TD;
    2276             : 
    2277      732774 :     hBWE_TD = st->hBWE_TD;
    2278             : 
    2279      732774 :     if ( st->last_core != ACELP_CORE )
    2280             :     {
    2281      726621 :         set_f( hBWE_TD->old_bwe_exc, 0.0f, PIT16k_MAX * 2 );
    2282      726621 :         hBWE_TD->bwe_non_lin_prev_scale = 0.f;
    2283             :     }
    2284      732774 :     if ( st->bwidth == WB )
    2285             :     {
    2286       29328 :         wb_tbe_extras_reset( hBWE_TD->mem_genSHBexc_filt_down_wb2, hBWE_TD->mem_genSHBexc_filt_down_wb3 );
    2287       29328 :         wb_tbe_extras_reset_synth( hBWE_TD->state_lsyn_filt_shb, hBWE_TD->state_lsyn_filt_dwn_shb, hBWE_TD->mem_resamp_HB );
    2288       29328 :         set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0, 7 );
    2289       29328 :         set_f( hBWE_TD->state_lpc_syn, 0, 10 );
    2290       29328 :         set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
    2291       29328 :         set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
    2292       29328 :         set_f( hBWE_TD->mem_csfilt, 0, 2 );
    2293             :     }
    2294      703446 :     else if ( st->bwidth == SWB || st->bwidth == FB )
    2295             :     {
    2296      703446 :         swb_tbe_reset( hBWE_TD->mem_csfilt, hBWE_TD->mem_genSHBexc_filt_down_shb, hBWE_TD->state_lpc_syn, hBWE_TD->syn_overlap, hBWE_TD->state_syn_shbexc, &( hBWE_TD->tbe_demph ), &( hBWE_TD->tbe_premph ), hBWE_TD->mem_stp_swb, &( hBWE_TD->gain_prec_swb ) );
    2297             : 
    2298      703446 :         set_f( hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
    2299      703446 :         set_f( hBWE_TD->int_3_over_2_tbemem_dec, 0.f, INTERP_3_2_MEM_LEN );
    2300      703446 :         set_f( hBWE_TD->mem_resamp_HB_32k, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    2301      703446 :         hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
    2302      703446 :         hBWE_TD->prev_mix_factor = 1.0f;
    2303             : 
    2304      703446 :         swb_tbe_reset_synth( hBWE_TD->genSHBsynth_Hilbert_Mem, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local );
    2305             : 
    2306      703446 :         if ( st->bwidth == FB )
    2307             :         {
    2308      490602 :             if ( st->hBWE_FD != NULL )
    2309             :             {
    2310      490602 :                 st->hBWE_FD->prev_fb_ener_adjust = 0.0f;
    2311             :             }
    2312      490602 :             set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
    2313      490602 :             hBWE_TD->fb_tbe_demph = 0;
    2314      490602 :             fb_tbe_reset_synth( hBWE_TD->fbbwe_hpf_mem, &hBWE_TD->prev_fbbwe_ratio );
    2315             :         }
    2316             :     }
    2317             : 
    2318      732774 :     return;
    2319             : }
    2320             : 
    2321             : /*-------------------------------------------------------------------*
    2322             :  * td_bwe_dec_init()
    2323             :  *
    2324             :  * Initialize TD BWE state structure at the decoder
    2325             :  *-------------------------------------------------------------------*/
    2326             : 
    2327       11040 : void td_bwe_dec_init(
    2328             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
    2329             :     const int16_t extl,        /* i  : BWE extension layer     */
    2330             :     const int32_t output_Fs    /* i  : output sampling rate    */
    2331             : )
    2332             : {
    2333             :     int16_t i;
    2334             : 
    2335             :     /* init. SHB buffers */;
    2336       11040 :     set_f( hBWE_TD->old_bwe_exc, 0.0f, ( PIT16k_MAX * 2 ) );
    2337       11040 :     hBWE_TD->bwe_seed[0] = 23; /* 1; */
    2338       11040 :     hBWE_TD->bwe_seed[1] = 59; /* 10000; */
    2339       11040 :     set_f( hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
    2340       11040 :     hBWE_TD->bwe_non_lin_prev_scale = 0;
    2341             : 
    2342       11040 :     set_f( hBWE_TD->genSHBsynth_Hilbert_Mem, 0.0f, HILBERT_MEM_SIZE );
    2343       11040 :     set_f( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
    2344             : 
    2345       11040 :     hBWE_TD->syn_dm_phase = 0;
    2346       11040 :     hBWE_TD->prev_fbbwe_ratio = 1.0f;
    2347       11040 :     hBWE_TD->prev_wb_bwe_frame_pow = 0.001f;
    2348       11040 :     hBWE_TD->prev_swb_bwe_frame_pow = 0.001f;
    2349             : 
    2350             :     /* reset SHB buffers */
    2351       11040 :     ResetSHBbuffer_Dec( hBWE_TD, extl );
    2352             : 
    2353       11040 :     if ( output_Fs == 48000 )
    2354             :     {
    2355        8418 :         set_f( hBWE_TD->fbbwe_hpf_mem[0], 0, 4 );
    2356        8418 :         set_f( hBWE_TD->fbbwe_hpf_mem[1], 0, 4 );
    2357        8418 :         set_f( hBWE_TD->fbbwe_hpf_mem[2], 0, 4 );
    2358        8418 :         set_f( hBWE_TD->fbbwe_hpf_mem[3], 0, 4 );
    2359             :     }
    2360             : 
    2361       11040 :     set_f( hBWE_TD->mem_resamp_HB, 0, INTERP_3_1_MEM_LEN );
    2362       11040 :     set_f( hBWE_TD->mem_resamp_HB_32k, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    2363             : 
    2364       11040 :     hBWE_TD->tilt_mem = 0.0f;
    2365       11040 :     set_f( hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
    2366       11040 :     hBWE_TD->prev_tilt_para = 0.0f;
    2367       11040 :     set_f( hBWE_TD->cur_sub_Aq, 0.0f, M + 1 );
    2368       11040 :     set_f( hBWE_TD->int_3_over_2_tbemem_dec, 0.0f, INTERP_3_2_MEM_LEN );
    2369             : 
    2370             :     /* TD BWE post-processing */
    2371       11040 :     hBWE_TD->ptr_mem_stp_swb = hBWE_TD->mem_stp_swb + LPC_SHB_ORDER - 1;
    2372       11040 :     set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
    2373             : 
    2374      121440 :     for ( i = 0; i < LPC_SHB_ORDER; i++ )
    2375             :     {
    2376      110400 :         hBWE_TD->swb_lsp_prev_interp[i] = (float) cos( (float) i * EVS_PI / (float) 10.0f );
    2377             :     }
    2378             : 
    2379       11040 :     hBWE_TD->prev1_shb_ener_sf = 1.0f;
    2380       11040 :     hBWE_TD->prev2_shb_ener_sf = 1.0f;
    2381       11040 :     hBWE_TD->prev3_shb_ener_sf = 1.0f;
    2382       11040 :     hBWE_TD->prev_res_shb_gshape = 0.125f;
    2383       11040 :     hBWE_TD->prev_mixFactors = 0.5f;
    2384       11040 :     hBWE_TD->prev_GainShape = 0.0f;
    2385       11040 :     set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
    2386       11040 :     hBWE_TD->fb_tbe_demph = 0.0f;
    2387             : 
    2388       11040 :     set_f( hBWE_TD->old_hb_synth, 0, L_FRAME48k );
    2389             : 
    2390       11040 :     hBWE_TD->GainFrame_prevfrm = 0.0f;
    2391             : 
    2392       11040 :     hBWE_TD->prev_ener = 0.0f;
    2393             : 
    2394       11040 :     return;
    2395             : }

Generated by: LCOV version 1.14