LCOV - code coverage report
Current view: top level - lib_dec - swb_tbe_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 848 929 91.3 %
Date: 2025-09-14 08:49:17 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      168337 : 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      168337 :     if ( extl != WB_TBE )
      76             :     {
      77      168337 :         f = 1.0f / 22.0f;
      78      168337 :         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      168337 :     set_f( hBWE_TD->mem_csfilt, 0, 2 );
      88             : 
      89             :     /* states for the filters used in generating SHB signal from SHB excitation*/
      90      168337 :     set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD );
      91      168337 :     set_f( hBWE_TD->state_lpc_syn, 0, LPC_SHB_ORDER );
      92             : 
      93      168337 :     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      168337 :     set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     102      168337 :     set_f( hBWE_TD->mem_genSHBexc_filt_down_wb2, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     103      168337 :     set_f( hBWE_TD->mem_genSHBexc_filt_down_wb3, 0.0f, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     104             : 
     105      168337 :     set_f( hBWE_TD->state_lsyn_filt_shb, 0, 2 * ALLPASSSECTIONS_STEEP );
     106      168337 :     set_f( hBWE_TD->state_lsyn_filt_dwn_shb, 0, 2 * ALLPASSSECTIONS_STEEP );
     107      168337 :     set_f( hBWE_TD->mem_resamp_HB, 0, INTERP_3_1_MEM_LEN );
     108             : 
     109             :     /* States for the local synthesis filters */
     110      168337 :     set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
     111             : 
     112             :     /* States for FEC */
     113      168337 :     if ( extl != WB_TBE )
     114             :     {
     115     1851707 :         for ( i = 0; i < LPC_SHB_ORDER; i++ )
     116             :         {
     117     1683370 :             hBWE_TD->lsp_prevfrm[i] = f;
     118     1683370 :             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      168337 :     hBWE_TD->GainFrame_prevfrm = 0.0f;
     135      168337 :     hBWE_TD->GainAttn = 1.0;
     136      168337 :     hBWE_TD->tbe_demph = 0.0f;
     137      168337 :     hBWE_TD->tbe_premph = 0.0f;
     138      168337 :     set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
     139      168337 :     hBWE_TD->gain_prec_swb = 1.0f;
     140      168337 :     set_f( hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
     141      168337 :     hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
     142      168337 :     hBWE_TD->prev_mix_factor = 1.0f;
     143             : 
     144      168337 :     set_f( hBWE_TD->old_core_synth, 0, L_FRAME16k );
     145      168337 :     set_f( hBWE_TD->old_tbe_synth, 0, L_SHB_TRANSITION_LENGTH );
     146      168337 :     hBWE_TD->tilt_swb_fec = 0.0f;
     147             : 
     148      168337 :     return;
     149             : }
     150             : 
     151             : /*-------------------------------------------------------------------*
     152             :  * wb_tbe_dec()
     153             :  *
     154             :  * WB TBE decoder, 6 - 8 kHz band decoding module
     155             :  *-------------------------------------------------------------------*/
     156             : 
     157      331277 : 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      331277 :     int16_t uv_flag = 0;
     176             : 
     177      331277 :     hBWE_TD = st->hBWE_TD;
     178             : 
     179      331277 :     if ( st->bws_cnt == 0 )
     180             :     {
     181             : 
     182      330458 :         if ( !st->bfi )
     183             :         {
     184      306061 :             if ( st->use_partial_copy )
     185             :             {
     186          92 :                 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          92 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     197          92 :                 set_f( GainShape, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR / 2 );
     198             : 
     199          92 :                 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          92 :                     temp = 0.0f;
     214          92 :                     switch ( st->rf_indx_tbeGainFr )
     215             :                     {
     216          52 :                         case 0:
     217          52 :                             GainFrame = 0.5f;
     218          52 :                             if ( hBWE_TD->GainFrame_prevfrm <= 1.25 )
     219             :                             {
     220          40 :                                 temp = 0.8f;
     221             :                             }
     222          52 :                             break;
     223          40 :                         case 1:
     224          40 :                             GainFrame = 2.0f;
     225          40 :                             if ( hBWE_TD->GainFrame_prevfrm > 1.25 && hBWE_TD->GainFrame_prevfrm <= 3 )
     226             :                             {
     227          14 :                                 temp = 0.8f;
     228             :                             }
     229          40 :                             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          92 :                     if ( st->last_extl == WB_TBE )
     251             :                     {
     252          92 :                         GainFrame = ( 1 - temp ) * GainFrame + temp * ( hBWE_TD->GainFrame_prevfrm );
     253             :                     }
     254             : 
     255          92 :                     if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE )
     256             :                     {
     257          92 :                         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      305969 :                 dequantizeSHBparams( st, st->extl, st->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0, &j /*temp*/ );
     268             :             }
     269             :         }
     270             :         else
     271             :         {
     272       24397 :             if ( st->extl_brate == WB_TBE_0k35 )
     273             :             {
     274        4341 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     275             :             }
     276             :             else
     277             :             {
     278       20056 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_wb, LPC_SHB_ORDER_WB );
     279             :             }
     280       24397 :             set_f( GainShape, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR / 2 );
     281       24397 :             hBWE_TD->GainAttn *= 0.85f;
     282       24397 :             if ( st->codec_mode == MODE1 )
     283             :             {
     284       24243 :                 GainFrame = hBWE_TD->GainAttn * hBWE_TD->GainFrame_prevfrm;
     285             :             }
     286             :             else
     287             :             {
     288         154 :                 GainFrame = hBWE_TD->GainFrame_prevfrm;
     289             :             }
     290             :         }
     291             : 
     292      330458 :         if ( st->extl_brate == WB_TBE_0k35 )
     293             :         {
     294             :             /* convert LSPs back into LP coeffs */
     295       71932 :             lsp2a( lpc_wb, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     296       71932 :             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      258526 :             lsp2a( lpc_wb, lsf_wb, LPC_SHB_ORDER_WB );
     302             :         }
     303      330458 :         lpc_wb[0] = 1.0f;
     304      330458 :         mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
     305      330458 :         if ( st->coder_type == VOICED )
     306             :         {
     307      154268 :             for ( i = 1; i < NB_SUBFR; i++ )
     308             :             {
     309      115701 :                 vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
     310             :             }
     311             : 
     312       38567 :             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      330458 :         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      330458 :         mvr2r( hBWE_TD->state_syn_shbexc, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     325             : 
     326      330458 :         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      330458 :         prev_pow = sum2_f( shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     329      330458 :         curr_pow = sum2_f( shaped_wb_excitation + L_SHB_LAHEAD / 4, L_SHB_LAHEAD / 4 );
     330             : 
     331      330458 :         if ( st->element_mode > EVS_MONO )
     332             :         {
     333             :             /* prevent too low values of energy */
     334      325198 :             prev_pow = max( 0.00001f, prev_pow );
     335      325198 :             curr_pow = max( 0.00001f, curr_pow );
     336             :         }
     337             : 
     338      330458 :         if ( voice_factors[0] > 0.75f )
     339             :         {
     340        5930 :             curr_pow *= 0.25;
     341             :         }
     342             : 
     343      330458 :         if ( prev_pow == 0 )
     344             :         {
     345         237 :             scale = 0;
     346             :         }
     347             :         else
     348             :         {
     349      330221 :             scale = (float) sqrt( curr_pow / prev_pow );
     350             :         }
     351             : 
     352     1652290 :         for ( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
     353             :         {
     354     1321832 :             shaped_wb_excitation[i] *= scale;
     355             :         }
     356      330458 :         scale = (float) sqrt( scale );
     357             : 
     358      330458 :         shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] *= scale;
     359             : 
     360             :         /* Update SHB excitation */
     361      330458 :         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      330458 :         ScaleShapedSHB( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap, GainShape, GainFrame, window_wb, subwin_wb );
     366             : 
     367      330458 :         curr_frame_pow = sum2_f( shaped_wb_excitation, L_FRAME16k / 4 ) + 0.001f;
     368      330458 :         curr_frame_pow = min( curr_frame_pow, FLT_MAX );
     369             : 
     370      330458 :         if ( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
     371             :         {
     372       10139 :             if ( curr_frame_pow > 2.0f * hBWE_TD->prev_wb_bwe_frame_pow )
     373             :             {
     374        8246 :                 scale = root_a_over_b( hBWE_TD->prev_wb_bwe_frame_pow, curr_frame_pow );
     375        8246 :                 temp = (float) pow( scale, 0.125f );
     376             :             }
     377             :             else
     378             :             {
     379        1893 :                 scale = 1.0f;
     380        1893 :                 temp = 1.0f;
     381             :             }
     382             : 
     383       91251 :             for ( j = 0; j < 8; j++ )
     384             :             {
     385       81112 :                 GainShape[2 * j] *= scale;
     386       81112 :                 GainShape[2 * j + 1] *= scale;
     387      892232 :                 for ( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
     388             :                 {
     389      811120 :                     shaped_wb_excitation[i + j * L_FRAME16k / ( 4 * 8 )] *= scale;
     390             :                 }
     391       81112 :                 scale /= temp;
     392             :             }
     393             :         }
     394             : 
     395      330458 :         hBWE_TD->prev_wb_bwe_frame_pow = curr_frame_pow;
     396             : 
     397             :         /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
     398      330458 :         GenWBSynth( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb, hBWE_TD->state_lsyn_filt_dwn_shb );
     399             : 
     400      330458 :         mvr2r( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth, L_SHB_TRANSITION_LENGTH );
     401             : 
     402   106077018 :         for ( i = 0; i < L_FRAME16k; i++ )
     403             :         {
     404   105746560 :             synth[i] = 0.65f * error[i];
     405             :         }
     406             : 
     407      330458 :         if ( st->hBWE_FD != NULL )
     408             :         {
     409      330458 :             st->hBWE_FD->last_wb_bwe_ener = 0.0f;
     410   106077018 :             for ( i = 0; i < L_FRAME16k; i++ )
     411             :             {
     412   105746560 :                 st->hBWE_FD->last_wb_bwe_ener += synth[i] * synth[i];
     413             :             }
     414      330458 :             st->hBWE_FD->last_wb_bwe_ener = (float) sqrt( st->hBWE_FD->last_wb_bwe_ener / L_FRAME16k );
     415             :         }
     416             : 
     417      330458 :         if ( st->output_Fs == 32000 ) /* 32kHz sampling rate, but only WB output - interpolate */
     418             :         {
     419       11864 :             Interpolate_allpass_steep( synth, hBWE_TD->mem_resamp_HB, L_FRAME16k, upsampled_synth );
     420       11864 :             mvr2r( upsampled_synth, synth, L_FRAME32k );
     421             :         }
     422      318594 :         else if ( st->output_Fs == 48000 )
     423             :         {
     424       33013 :             interpolate_3_over_1_allpass( synth, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB );
     425       33013 :             mvr2r( upsampled_synth, synth, L_FRAME48k );
     426             :         }
     427             :     }
     428             :     else
     429             :     {
     430        5733 :         for ( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     431             :         {
     432        4914 :             lsf_wb[i] = ( i + 1 ) / 6.f;
     433             :         }
     434         819 :         GainFrame = 0;
     435             : 
     436         819 :         hBWE_TD->prev_wb_bwe_frame_pow = 0.001f;
     437             :     }
     438             : 
     439             :     /* Update previous frame parameters for FEC */
     440      331277 :     if ( st->extl_brate == WB_TBE_0k35 )
     441             :     {
     442       72520 :         mvr2r( lsf_wb, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER_LBR_WB );
     443             :     }
     444             :     else
     445             :     {
     446      258757 :         mvr2r( lsf_wb, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER_WB );
     447             :     }
     448      331277 :     hBWE_TD->GainFrame_prevfrm = GainFrame;
     449             : 
     450      331277 :     if ( !st->bfi )
     451             :     {
     452      306880 :         hBWE_TD->GainAttn = 1.0f;
     453             :     }
     454             : 
     455      331277 :     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     1975507 : 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     1975507 :     hBWE_TD = st->hBWE_TD;
     514             : 
     515             :     /* initializations */
     516     1975507 :     GainFrame = 0.0f;
     517     1975507 :     mixFactors = 0.0f;
     518     1975507 :     shb_ener_sf = 0.0f;
     519     1975507 :     set_f( shaped_shb_excitationTemp, 0.0f, L_FRAME16k );
     520     1975507 :     if ( st->hTdCngDec != NULL )
     521             :     {
     522     1646834 :         st->hTdCngDec->shb_dtx_count = 0;
     523             :     }
     524     1975507 :     is_fractive = 0;
     525             : 
     526     1975507 :     if ( hStereoICBWE != NULL )
     527             :     {
     528      263225 :         nlExc16k = hStereoICBWE->nlExc16k;
     529      263225 :         mixExc16k = hStereoICBWE->mixExc16k;
     530      263225 :         MSFlag = hStereoICBWE->MSFlag;
     531             :     }
     532             :     else
     533             :     {
     534     1712282 :         nlExc16k = NULL;
     535     1712282 :         mixExc16k = NULL;
     536     1712282 :         MSFlag = 0;
     537             :     }
     538             : 
     539             :     /* find tilt */
     540     1975507 :     calc_tilt_bwe( old_syn_12k8_16k, &tilt_swb_fec, L_FRAME );
     541             : 
     542     1975507 :     if ( st->bfi && st->clas_dec != UNVOICED_CLAS )
     543             :     {
     544       89911 :         tilt_swb_fec = hBWE_TD->tilt_swb_fec;
     545             :     }
     546             : 
     547             :     /* WB/SWB bandwidth switching */
     548     1975507 :     if ( ( st->tilt_wb > 5 && st->clas_dec == UNVOICED_CLAS ) || st->tilt_wb > 10 )
     549             :     {
     550      169433 :         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         695 :             is_fractive = 0;
     553             :         }
     554             :         else
     555             :         {
     556      168738 :             is_fractive = 1;
     557             :         }
     558             :     }
     559             : 
     560             :     /* WB/SWB bandwidth switching */
     561     1975507 :     if ( st->bws_cnt > 0 )
     562             :     {
     563        1933 :         f = 1.0f / 22.0f;
     564        1933 :         inc = 1.0f / 22.0f;
     565             : 
     566        1933 :         if ( is_fractive == 1 )
     567             :         {
     568           3 :             mvr2r( lsf_tab, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER );
     569             :         }
     570             :         else
     571             :         {
     572       21230 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
     573             :             {
     574       19300 :                 hBWE_TD->lsp_prevfrm[i] = f;
     575       19300 :                 f += inc;
     576             :             }
     577             :         }
     578        1933 :         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        1859 :             set_f( GainShape, 0.3536f, NUM_SHB_SUBFR );
     581             :         }
     582             :         else
     583             :         {
     584          74 :             hBWE_TD->prev_GainShape = ( hBWE_TD->prev_GainShape > 0.3536f ) ? 0.353f : hBWE_TD->prev_GainShape;
     585          74 :             set_f( GainShape, hBWE_TD->prev_GainShape, NUM_SHB_SUBFR );
     586             :         }
     587             : 
     588        1933 :         mvr2r( hBWE_TD->lsp_prevfrm, lsf_shb, LPC_SHB_ORDER );
     589        1933 :         set_f( shb_res_gshape, 0.2f, NB_SUBFR16k );
     590             :     }
     591             :     else
     592             :     {
     593     1973574 :         if ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE )
     594             :         {
     595      136132 :             f = 1.0f / 22.0f;
     596      136132 :             inc = 1.0f / 22.0f;
     597     1497452 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
     598             :             {
     599     1361320 :                 hBWE_TD->lsp_prevfrm[i] = f;
     600     1361320 :                 f += inc;
     601             :             }
     602             :         }
     603             : 
     604     1973574 :         if ( !st->bfi )
     605             :         {
     606     1832875 :             if ( st->use_partial_copy )
     607             :             {
     608          80 :                 if ( st->last_extl != SWB_TBE )
     609             :                 {
     610           8 :                     hBWE_TD->GainFrame_prevfrm = 0;
     611           8 :                     f = 1.0f / 22.0f;
     612           8 :                     inc = 1.0f / 22.0f;
     613          88 :                     for ( i = 0; i < LPC_SHB_ORDER; i++ )
     614             :                     {
     615          80 :                         hBWE_TD->lsp_prevfrm[i] = f;
     616          80 :                         f += inc;
     617             :                     }
     618             :                 }
     619          80 :                 mvr2r( hBWE_TD->lsp_prevfrm, lsf_shb, LPC_SHB_ORDER );
     620          80 :                 set_f( GainShape, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR );
     621             : 
     622          80 :                 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          80 :                     temp = 0.0f;
     636             :                     /* Frame gain */
     637          80 :                     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          50 :                         case 1:
     647          50 :                             GainFrame = 2.0f;
     648          50 :                             if ( hBWE_TD->GainFrame_prevfrm > 1.25 && hBWE_TD->GainFrame_prevfrm <= 3 )
     649             :                             {
     650          38 :                                 temp = 0.8f;
     651             :                             }
     652          50 :                             break;
     653          24 :                         case 2:
     654          24 :                             GainFrame = 4.0f;
     655          24 :                             if ( hBWE_TD->GainFrame_prevfrm > 3 && hBWE_TD->GainFrame_prevfrm <= 6 )
     656             :                             {
     657          12 :                                 temp = 0.8f;
     658             :                             }
     659          24 :                             break;
     660           6 :                         case 3:
     661           6 :                             GainFrame = 8.0f;
     662           6 :                             if ( hBWE_TD->GainFrame_prevfrm > 6 && hBWE_TD->GainFrame_prevfrm <= 16 )
     663             :                             {
     664           0 :                                 temp = 0.8f;
     665             :                             }
     666           6 :                             break;
     667           0 :                         default:
     668           0 :                             IVAS_ERROR( IVAS_ERR_INTERNAL, "RF SWB-TBE gain bits not supported." );
     669             :                     }
     670             : 
     671          80 :                     if ( st->last_extl == SWB_TBE )
     672             :                     {
     673          72 :                         GainFrame = ( 1 - temp ) * GainFrame + temp * ( hBWE_TD->GainFrame_prevfrm );
     674             :                     }
     675             : 
     676          80 :                     if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE )
     677             :                     {
     678          72 :                         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     1832795 :                 dequantizeSHBparams( st, st->extl, st->extl_brate, lsf_shb, GainShape, &GainFrame, &stemp, &shb_ener_sf, shb_res_gshape, &mixFactors, &MSFlag );
     689     1832795 :                 if ( hStereoICBWE != NULL )
     690             :                 {
     691      255662 :                     hStereoICBWE->MSFlag = MSFlag;
     692             :                 }
     693             :             }
     694             :         }
     695             :         else
     696             :         {
     697      140699 :             mvr2r( hBWE_TD->lsp_prevfrm, lsf_shb, LPC_SHB_ORDER );
     698             : 
     699      140699 :             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        1272 :                 for ( j = 0; j < 3; j++ )
     703             :                 {
     704         954 :                     GainGrad0[j] = hBWE_TD->GainShape_Delay[j + 1] - hBWE_TD->GainShape_Delay[j];
     705         954 :                     GainGrad1[j] = hBWE_TD->GainShape_Delay[j + 5] - hBWE_TD->GainShape_Delay[j + 4];
     706         954 :                     GainGradFEC[j + 1] = GainGrad0[j] * 0.4f + GainGrad1[j] * 0.6f;
     707             :                 }
     708             : 
     709             :                 /* gradient for the first gainshape */
     710         318 :                 if ( ( GainGrad1[2] > 2 * GainGrad1[1] && GainGrad1[1] > 2 * GainGrad1[0] ) ||
     711         283 :                      ( GainGrad1[2] < 2 * GainGrad1[1] && GainGrad1[1] < 2 * GainGrad1[0] ) )
     712             :                 {
     713         102 :                     GainGradFEC[0] = GainGrad1[1] * 0.1f + GainGrad1[2] * 0.9f;
     714             :                 }
     715             :                 else
     716             :                 {
     717         216 :                     GainGradFEC[0] = GainGrad1[0] * 0.2f + GainGrad1[1] * 0.3f + GainGrad1[2] * 0.5f;
     718             :                 }
     719             : 
     720             :                 /* get the first gainshape template */
     721         318 :                 if ( ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS ) && GainGradFEC[0] > 0 )
     722             :                 {
     723          15 :                     GainShapeTemp[0] = hBWE_TD->GainShape_Delay[7] + GainGradFEC[0];
     724             :                 }
     725         303 :                 else if ( GainGradFEC[0] > 0 )
     726             :                 {
     727         130 :                     GainShapeTemp[0] = hBWE_TD->GainShape_Delay[7] + GainGradFEC[0] * 0.5f;
     728             :                 }
     729             :                 else
     730             :                 {
     731         173 :                     GainShapeTemp[0] = hBWE_TD->GainShape_Delay[7];
     732             :                 }
     733             : 
     734             :                 /*Get the second the third and the fourth gainshape template*/
     735         318 :                 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         318 :                 else if ( ( GainGrad1[2] > 10.0f * GainGrad1[1] ) && GainGrad1[1] < 0 )
     744             :                 {
     745         292 :                     for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
     746             :                     {
     747         219 :                         GainShapeTemp[i] = GainShapeTemp[i - 1] + GainGradFEC[i] * 0.2f;
     748         219 :                         GainShapeTemp[i] = max( GainShapeTemp[i], 0.01f );
     749             :                     }
     750             :                 }
     751             :                 else
     752             :                 {
     753         980 :                     for ( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
     754             :                     {
     755         735 :                         GainShapeTemp[i] = GainShapeTemp[i - 1] + GainGradFEC[i];
     756         735 :                         GainShapeTemp[i] = max( GainShapeTemp[i], 0.01f );
     757             :                     }
     758             :                 }
     759             : 
     760             :                 /* Get the gainshape and gain frame for the current frame*/
     761         318 :                 if ( ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS ) && st->nbLostCmpt == 1 )
     762             :                 {
     763         120 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     764             :                     {
     765         480 :                         for ( j = 0; j < 4; j++ )
     766             :                         {
     767         384 :                             GainShape[i * 4 + j] = GainShapeTemp[i] * 1.2f;
     768             :                         }
     769             :                     }
     770          24 :                     hBWE_TD->GainAttn *= 0.95f;
     771             :                 }
     772         294 :                 else if ( st->prev_coder_type == UNVOICED || st->last_good == UNVOICED_CLAS )
     773             :                 {
     774          50 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     775             :                     {
     776         200 :                         for ( j = 0; j < 4; j++ )
     777             :                         {
     778         160 :                             GainShape[i * 4 + j] = GainShapeTemp[i];
     779             :                         }
     780             :                     }
     781          10 :                     hBWE_TD->GainAttn *= 0.95f;
     782             :                 }
     783         284 :                 else if ( st->nbLostCmpt > 1 )
     784             :                 {
     785         860 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     786             :                     {
     787        3440 :                         for ( j = 0; j < 4; j++ )
     788             :                         {
     789        2752 :                             GainShape[i * 4 + j] = GainShapeTemp[i] * 0.5f;
     790             :                         }
     791             :                     }
     792         172 :                     hBWE_TD->GainAttn *= 0.5f;
     793             :                 }
     794             :                 else
     795             :                 {
     796         560 :                     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     797             :                     {
     798        2240 :                         for ( j = 0; j < 4; j++ )
     799             :                         {
     800        1792 :                             GainShape[i * 4 + j] = GainShapeTemp[i];
     801             :                         }
     802             :                     }
     803         112 :                     hBWE_TD->GainAttn *= 0.85f;
     804             :                 }
     805             : 
     806         318 :                 GainFrame = hBWE_TD->GainAttn * hBWE_TD->GainFrame_prevfrm;
     807             :             }
     808             :             else
     809             :             {
     810      701905 :                 for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     811             :                 {
     812     2807620 :                     for ( j = 0; j < 4; j++ )
     813             :                     {
     814     2246096 :                         GainShape[i * 4 + j] = st->cummulative_damping * hBWE_TD->GainShape_Delay[4 + i];
     815             :                     }
     816             :                 }
     817             : 
     818      140381 :                 if ( tilt_swb_fec > 8 )
     819             :                 {
     820        4289 :                     if ( st->nbLostCmpt == 1 )
     821             :                     {
     822        3341 :                         GainFrame = (float) ( 0.6 * st->cummulative_damping * hBWE_TD->GainFrame_prevfrm );
     823             :                     }
     824         948 :                     else if ( st->nbLostCmpt == 2 )
     825             :                     {
     826         847 :                         GainFrame = (float) ( 0.35 * st->cummulative_damping * hBWE_TD->GainFrame_prevfrm );
     827             :                     }
     828             :                     else
     829             :                     {
     830         101 :                         GainFrame = (float) ( 0.2 * st->cummulative_damping * hBWE_TD->GainFrame_prevfrm );
     831             :                     }
     832             :                 }
     833             :                 else
     834             :                 {
     835      136092 :                     GainFrame = hBWE_TD->GainFrame_prevfrm; /* gain locking */
     836             :                 }
     837             :             }
     838             : 
     839      140699 :             if ( st->extl_brate >= SWB_TBE_2k8 )
     840             :             {
     841       42729 :                 if ( st->codec_mode == MODE1 && st->element_mode == EVS_MONO )
     842             :                 {
     843         152 :                     scale = (float) ( hBWE_TD->prev1_shb_ener_sf / sqrt( ( hBWE_TD->prev2_shb_ener_sf * hBWE_TD->prev3_shb_ener_sf ) + 0.0001 ) );
     844         152 :                     scale = hBWE_TD->prev_res_shb_gshape * min( scale, 1.0f );
     845             : 
     846         152 :                     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          60 :                         shb_ener_sf = 0.5f * scale * hBWE_TD->prev1_shb_ener_sf;
     849          60 :                         if ( st->nbLostCmpt > 1 )
     850             :                         {
     851          48 :                             shb_ener_sf *= 0.5f;
     852             :                         }
     853             :                     }
     854             :                     else
     855             :                     {
     856          92 :                         shb_ener_sf = scale * scale * hBWE_TD->prev1_shb_ener_sf;
     857             :                     }
     858             :                 }
     859             :                 else
     860             :                 {
     861       42577 :                     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        2496 :                         shb_ener_sf = 0.5f * st->cummulative_damping * hBWE_TD->prev1_shb_ener_sf;
     864             :                     }
     865             :                     else
     866             :                     {
     867       40081 :                         shb_ener_sf = st->cummulative_damping * hBWE_TD->prev1_shb_ener_sf;
     868             :                     }
     869             :                 }
     870             :             }
     871             : 
     872      140699 :             shb_ener_sf = max( shb_ener_sf, 1.0f );
     873      140699 :             mixFactors = hBWE_TD->prev_mixFactors;
     874             : 
     875      140699 :             if ( st->codec_mode == MODE2 )
     876             :             {
     877         322 :                 set_f( shb_res_gshape, 1.0f, NB_SUBFR16k );
     878             :             }
     879             :             else
     880             :             {
     881      140377 :                 set_f( shb_res_gshape, 0.2f, NB_SUBFR16k );
     882             :             }
     883             :         }
     884             :     }
     885             : 
     886             :     /* get the gainshape delay */
     887     1975507 :     mvr2r( &hBWE_TD->GainShape_Delay[4], &hBWE_TD->GainShape_Delay[0], NUM_SHB_SUBFR / 4 );
     888     9877535 :     for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
     889             :     {
     890     7902028 :         hBWE_TD->GainShape_Delay[i + 4] = GainShape[i * 4];
     891             :     }
     892             : 
     893     1975507 :     mvr2r( voice_factors, vf_modified, NB_SUBFR16k );
     894     1975507 :     if ( st->coder_type == VOICED || mean( voice_factors, 4 ) > 0.4f )
     895             :     {
     896     1771576 :         for ( i = 1; i < NB_SUBFR; i++ )
     897             :         {
     898     1328682 :             vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i - 1];
     899             :         }
     900             : 
     901      442894 :         if ( st->L_frame != L_FRAME )
     902             :         {
     903      251890 :             vf_modified[4] = 0.8f * voice_factors[4] + 0.2f * voice_factors[3];
     904             :         }
     905             :     }
     906             : 
     907     1975507 :     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     1975507 :     lsf2lsp( lsf_shb, lsp_shb_2, LPC_SHB_ORDER, 1 );
     914             : 
     915     1975507 :     if ( st->last_extl == SWB_TBE || st->last_extl == FB_TBE )
     916             :     {
     917             :         /* SHB LSP values from prev. frame for interpolation */
     918     1837442 :         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      138065 :         mvr2r( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
     924             :     }
     925             : 
     926     1975507 :     if ( st->bws_cnt == 0 && st->bws_cnt1 == 0 && st->prev_use_partial_copy == 0 && st->use_partial_copy == 0 )
     927             :     {
     928     1967454 :         lsf_diff[0] = lsf_diff[LPC_SHB_ORDER - 1] = 0.5f;
     929    17707086 :         for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
     930             :         {
     931    15739632 :             lsf_diff[i] = lsf_shb[i] - lsf_shb[i - 1];
     932             :         }
     933             : 
     934     1967454 :         a2rc( hBWE_TD->cur_sub_Aq + 1, refl, M );
     935             : 
     936     1967454 :         tilt_para = 6.6956f * ( 1.0f + refl[0] ) * ( 1.0f + refl[0] ) - 3.8714f * ( 1.0f + refl[0] ) + 1.3041f;
     937     1967454 :         if ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE )
     938             :         {
     939     1209627 :             for ( i = 1; i < LPC_SHB_ORDER - 1; i++ )
     940             :             {
     941     1075224 :                 hBWE_TD->prev_lsf_diff[i - 1] = 0.5f * lsf_diff[i];
     942             :             }
     943             :         }
     944             : 
     945     1967454 :         if ( st->extl_brate <= FB_TBE_1k8 )
     946             :         {
     947     1519773 :             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    13273263 :                 for ( i = 1; i < ( LPC_SHB_ORDER - 1 ); i++ )
     950             :                 {
     951    11798456 :                     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    11798456 :                         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     1474807 :                 w[0] = w[1];
     962     1474807 :                 w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
     963             : 
     964    16222877 :                 for ( i = 0; i < LPC_SHB_ORDER; i++ )
     965             :                 {
     966    14748070 :                     lsp_temp[i] = lsp_shb_1[i] * ( 1.0f - w[i] ) + lsp_shb_2[i] * w[i];
     967             :                 }
     968             :             }
     969             :             else
     970             :             {
     971       44966 :                 mvr2r( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
     972             :             }
     973             : 
     974             :             /* convert from lsp to lsf */
     975     1519773 :             lsp2lsf( lsp_temp, lsf_shb, LPC_SHB_ORDER, 1 );
     976             :         }
     977             : 
     978     1967454 :         mvr2r( lsf_diff + 1, hBWE_TD->prev_lsf_diff, LPC_SHB_ORDER - 2 );
     979     1967454 :         hBWE_TD->prev_tilt_para = tilt_para;
     980             :     }
     981             :     else
     982             :     {
     983        8053 :         mvr2r( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
     984             :     }
     985             : 
     986     1975507 :     if ( st->extl_brate >= SWB_TBE_2k8 )
     987             :     {
     988             :         /* SHB LSP interpolation */
     989      447959 :         ptr_lsp_interp_coef = interpol_frac_shb;
     990     2239795 :         for ( j = 0; j < 4; j++ )
     991             :         {
     992    19710196 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
     993             :             {
     994    17918360 :                 lsp_temp[i] = lsp_shb_1[i] * ( *ptr_lsp_interp_coef ) + lsp_shb_2[i] * ( *( ptr_lsp_interp_coef + 1 ) );
     995             :             }
     996     1791836 :             ptr_lsp_interp_coef += 2;
     997             : 
     998             :             /* convert from lsp to lsf */
     999     1791836 :             lsp2lsf( lsp_temp, lsp_temp, LPC_SHB_ORDER, 1 );
    1000             : 
    1001             :             /* convert lsf to lpc for SHB synthesis */
    1002     1791836 :             lsp2a( lpc_shb_sf + j * ( LPC_SHB_ORDER + 1 ), lsp_temp, LPC_SHB_ORDER );
    1003     1791836 :             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     1975507 :     mvr2r( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp, LPC_SHB_ORDER );
    1009             : 
    1010             :     /* save the shb_ener and mixFactor values */
    1011     1975507 :     hBWE_TD->prev3_shb_ener_sf = hBWE_TD->prev2_shb_ener_sf;
    1012     1975507 :     hBWE_TD->prev2_shb_ener_sf = hBWE_TD->prev1_shb_ener_sf;
    1013     1975507 :     hBWE_TD->prev1_shb_ener_sf = shb_ener_sf;
    1014     1975507 :     hBWE_TD->prev_res_shb_gshape = shb_res_gshape[4];
    1015     1975507 :     hBWE_TD->prev_mixFactors = mixFactors;
    1016             : 
    1017             :     /* SWB CNG/DTX - update memories */
    1018     1975507 :     if ( st->hTdCngDec != NULL )
    1019             :     {
    1020     1646834 :         mvr2r( st->hTdCngDec->lsp_shb_prev, st->hTdCngDec->lsp_shb_prev_prev, LPC_SHB_ORDER );
    1021     1646834 :         mvr2r( lsf_shb, st->hTdCngDec->lsp_shb_prev, LPC_SHB_ORDER );
    1022             :     }
    1023             : 
    1024             :     /* convert LSPs back into LP coeffs */
    1025     1975507 :     lsp2a( lpc_shb, lsf_shb, LPC_SHB_ORDER );
    1026     1975507 :     lpc_shb[0] = 1.0;
    1027     1975507 :     if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
    1028             :     {
    1029       28510 :         vind = (int16_t) ( mixFactors * ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
    1030             :     }
    1031             :     else
    1032             :     {
    1033     1946997 :         vind = (int16_t) ( mixFactors * ( 1 << NUM_BITS_SHB_VF ) );
    1034             :     }
    1035             : 
    1036             :     /* Determine formant PF strength */
    1037     1975507 :     formant_fac = swb_formant_fac( lpc_shb[1], &hBWE_TD->tilt_mem );
    1038     1975507 :     if ( st->total_brate > ACELP_32k )
    1039             :     {
    1040      697995 :         for ( j = 0; j < 4; j++ )
    1041             :         {
    1042      558396 :             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     1975507 :     mvr2r( hBWE_TD->state_syn_shbexc, shaped_shb_excitation, L_SHB_LAHEAD );
    1048     1975507 :     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     1975507 :     if ( ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT ) && st->last_core_brate <= SID_2k40 )
    1053             :     {
    1054        1117 :         mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, shaped_shb_excitation, L_SHB_LAHEAD );
    1055             :     }
    1056             : 
    1057     1975507 :     if ( hStereoICBWE != NULL )
    1058             :     {
    1059      263225 :         mvr2r( shaped_shb_excitation + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef, L_FRAME16k );
    1060             :     }
    1061             : 
    1062     1975507 :     if ( st->extl_brate != SWB_TBE_1k10 && st->extl_brate != SWB_TBE_1k75 )
    1063             :     {
    1064     9734985 :         for ( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    1065             :         {
    1066             :             /* TD BWE post-processing */
    1067     7787988 :             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     1946997 :         mvr2r( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k );
    1071             : 
    1072     1946997 :         prev_pow = sum2_f( shaped_shb_excitation, L_SHB_LAHEAD + 10 );
    1073     1946997 :         curr_pow = sum2_f( shaped_shb_excitation + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
    1074             : 
    1075     1946997 :         if ( st->element_mode > EVS_MONO )
    1076             :         {
    1077             :             /* prevent too low values of energy */
    1078     1919509 :             prev_pow = max( 0.00001f, prev_pow );
    1079     1919509 :             curr_pow = max( 0.00001f, curr_pow );
    1080             :         }
    1081             : 
    1082     1946997 :         if ( voice_factors[0] > 0.75f )
    1083             :         {
    1084       76375 :             curr_pow *= 0.25;
    1085             :         }
    1086             : 
    1087     1946997 :         if ( prev_pow == 0 )
    1088             :         {
    1089           0 :             scale = 0;
    1090             :         }
    1091             :         else
    1092             :         {
    1093     1946997 :             scale = (float) sqrt( curr_pow / prev_pow );
    1094             :         }
    1095             : 
    1096    40886937 :         for ( i = 0; i < L_SHB_LAHEAD; i++ )
    1097             :         {
    1098    38939940 :             shaped_shb_excitation[i] *= scale;
    1099             :         }
    1100    21416967 :         for ( ; i < L_SHB_LAHEAD + 10; i++ )
    1101             :         {
    1102    19469970 :             temp = ( i - 19 ) / 10.0f;
    1103    19469970 :             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       28510 :         set_f( hBWE_TD->mem_stp_swb, 0, LPC_SHB_ORDER );
    1110       28510 :         hBWE_TD->gain_prec_swb = 1.0f;
    1111       28510 :         set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
    1112             :     }
    1113             : 
    1114             :     /* Update SHB excitation */
    1115     1975507 :     mvr2r( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc, L_SHB_LAHEAD );
    1116     1975507 :     l_subframe = L_FRAME16k / NUM_SHB_SUBGAINS;
    1117     1975507 :     ener = EPSILON;
    1118     9877535 :     for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1119             :     {
    1120     7902028 :         ener_tmp[i] = EPSILON;
    1121   640064268 :         for ( j = 0; j < l_subframe; j++ )
    1122             :         {
    1123   632162240 :             ener_tmp[i] += shaped_shb_excitation[i * l_subframe + j] * shaped_shb_excitation[i * l_subframe + j] * 0.0125f;
    1124             :         }
    1125     7902028 :         ener_tmp[i] = (float) sqrt( ener_tmp[i] );
    1126     7902028 :         ener += ener_tmp[i];
    1127             :     }
    1128     1975507 :     ener /= NUM_SHB_SUBGAINS;
    1129             : 
    1130             :     /* WB/SWB bandwidth switching */
    1131     1975507 :     if ( st->bws_cnt > 0 )
    1132             :     {
    1133        1933 :         ener *= 0.35f;
    1134             : 
    1135        1933 :         if ( st->tilt_swb > 8 )
    1136             :         {
    1137          25 :             st->prev_fractive = 1;
    1138             :         }
    1139             : 
    1140        1933 :         if ( is_fractive == 0 )
    1141             :         {
    1142        1930 :             if ( st->tilt_wb > 1.0 )
    1143             :             {
    1144        1006 :                 st->tilt_wb = 1.0f;
    1145             :             }
    1146         924 :             else if ( st->tilt_wb < 0.5 )
    1147             :             {
    1148         876 :                 st->tilt_wb = 0.5f;
    1149             :             }
    1150             : 
    1151        1930 :             if ( st->prev_fractive == 1 && st->tilt_wb > 0.5 )
    1152             :             {
    1153         196 :                 st->tilt_wb = 0.5f;
    1154             :             }
    1155             :         }
    1156             :         else
    1157             :         {
    1158           3 :             if ( st->tilt_wb > 4 )
    1159             :             {
    1160           3 :                 if ( st->prev_fractive == 0 )
    1161             :                 {
    1162           2 :                     st->tilt_wb = 4;
    1163             :                 }
    1164             :                 else
    1165             :                 {
    1166           1 :                     st->tilt_wb = 8;
    1167             :                 }
    1168             :             }
    1169             :             else
    1170             :             {
    1171           0 :                 st->tilt_wb *= 2;
    1172             :             }
    1173             :         }
    1174             : 
    1175        1933 :         if ( ener != 0 )
    1176             :         {
    1177        1933 :             if ( ener * st->tilt_wb > st->enerLH )
    1178             :             {
    1179        1785 :                 st->tilt_wb = 0.5f * st->enerLH / ener;
    1180             :             }
    1181         148 :             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        1933 :             GainFrame_prevfrm = st->prev_ener_shb / ener;
    1187             :         }
    1188             :         else
    1189             :         {
    1190           0 :             GainFrame_prevfrm = 0;
    1191             :         }
    1192             : 
    1193        1933 :         if ( is_fractive == 1 )
    1194             :         {
    1195           3 :             GainFrame = 8.0f * st->tilt_wb;
    1196             :         }
    1197             :         else
    1198             :         {
    1199        1930 :             GainFrame = 2.0f * st->tilt_wb;
    1200             :         }
    1201             : 
    1202        1933 :         if ( ( is_fractive & st->prev_fractive ) == 1 && GainFrame > GainFrame_prevfrm )
    1203             :         {
    1204           1 :             GainFrame = 0.2f * GainFrame + 0.8f * GainFrame_prevfrm;
    1205             :         }
    1206             :         else
    1207             :         {
    1208        1932 :             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          81 :                 GainFrame = 0.5f * GainFrame + 0.5f * GainFrame_prevfrm;
    1211             :             }
    1212             :             else
    1213             :             {
    1214        1851 :                 if ( is_fractive == 0 && st->prev_fractive == 1 )
    1215             :                 {
    1216         203 :                     GainFrame = ( 1.0f - 0.1f * GainFrame ) * GainFrame + 0.1f * GainFrame * GainFrame_prevfrm;
    1217             :                 }
    1218             :                 else
    1219             :                 {
    1220        1648 :                     GainFrame = 0.5f * GainFrame + 0.5f * GainFrame_prevfrm;
    1221             :                 }
    1222             :             }
    1223             :         }
    1224             : 
    1225        1933 :         GainFrame *= ( (float) N_WS2N_FRAMES - (float) st->bws_cnt ) / (float) N_WS2N_FRAMES;
    1226             :     }
    1227             :     else
    1228             :     {
    1229     1973574 :         if ( st->bws_cnt1 > 0 )
    1230             :         {
    1231        6000 :             GainFrame *= (float) st->bws_cnt1 / (float) N_WS2N_FRAMES;
    1232             :         }
    1233             : 
    1234     1973574 :         if ( st->nbLostCmpt == 1 )
    1235             :         {
    1236       58508 :             prev_ener_ratio = st->prev_ener_shb / ener;
    1237             : 
    1238       58508 :             if ( st->clas_dec != UNVOICED_CLAS && st->clas_dec != UNVOICED_TRANSITION && hBWE_TD->tilt_swb_fec < 8.0 &&
    1239       39287 :                  ( ( 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         172 :                 if ( prev_ener_ratio > 4.0f * GainFrame )
    1242             :                 {
    1243           4 :                     GainFrame = 0.4f * prev_ener_ratio + 0.6f * GainFrame;
    1244             :                 }
    1245         168 :                 else if ( prev_ener_ratio > 2.0f * GainFrame )
    1246             :                 {
    1247          34 :                     GainFrame = 0.8f * prev_ener_ratio + 0.2f * GainFrame;
    1248             :                 }
    1249             :                 else
    1250             :                 {
    1251         134 :                     GainFrame = 0.2f * prev_ener_ratio + 0.8f * GainFrame;
    1252             :                 }
    1253             : 
    1254         172 :                 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       58336 :             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     1915066 :         else if ( st->nbLostCmpt > 1 )
    1265             :         {
    1266       82191 :             prev_ener_ratio = st->prev_ener_shb / ener;
    1267       82191 :             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       82191 :             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     1975507 :     st->prev_fractive = is_fractive;
    1293             : 
    1294             :     /* Adjust the subframe and frame gain of the synthesized shb signal */
    1295             :     /* Scale the shaped excitation */
    1296     1975507 :     if ( st->L_frame == L_FRAME )
    1297             :     {
    1298      926941 :         pitch = 0.25f * sum_f( pitch_buf, 4 );
    1299             :     }
    1300             :     else
    1301             :     {
    1302     1048566 :         pitch = 0.2f * sum_f( pitch_buf, 5 );
    1303             :     }
    1304             : 
    1305     1975507 :     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     3641310 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1308             :         {
    1309     2913048 :             GainShape_tmp[i] = GainShape[i * 4];
    1310             :         }
    1311             : 
    1312     3641310 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1313             :         {
    1314     2913048 :             if ( ener_tmp[i] * GainShape_tmp[i] > hBWE_TD->prev_ener * hBWE_TD->prev_GainShape )
    1315             :             {
    1316     1719984 :                 GainShape_tmp[i] = 0.5f * ( hBWE_TD->prev_ener * hBWE_TD->prev_GainShape / ener_tmp[i] + GainShape_tmp[i] );
    1317             :             }
    1318     2913048 :             hBWE_TD->prev_ener = ener_tmp[i];
    1319     2913048 :             hBWE_TD->prev_GainShape = GainShape_tmp[i];
    1320             :         }
    1321             : 
    1322    12380454 :         for ( i = 0; i < NUM_SHB_SUBFR; i++ )
    1323             :         {
    1324    11652192 :             GainShape[i] = GainShape_tmp[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    1325             :         }
    1326             :     }
    1327             : 
    1328             :     /* Gain shape smoothing after quantization */
    1329     1975507 :     if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
    1330             :     {
    1331      142550 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1332             :         {
    1333      114040 :             GainShape_tmp[i] = GainShape[i * NUM_SHB_SUBGAINS];
    1334             :         }
    1335             : 
    1336       28510 :         lls_interp_n( GainShape_tmp, NUM_SHB_SUBGAINS, &GainShape_tilt, &temp, 1 );
    1337             : 
    1338       28510 :         if ( vind >= 6 && fabs( GainShape_tilt ) < 0.12f )
    1339             :         {
    1340       17020 :             feedback = 0.3f;
    1341       85100 :             for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1342             :             {
    1343       68080 :                 GainShape[i] = ( 1 - feedback ) * GainShape[i * NUM_SHB_SUBGAINS] + feedback * GainShape_tmp[i];
    1344             :             }
    1345             : 
    1346      272320 :             for ( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
    1347             :             {
    1348      255300 :                 GainShape[i] = GainShape[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    1349             :             }
    1350             :         }
    1351             :     }
    1352             : 
    1353             :     /* fil-in missing memory */
    1354     1975507 :     if ( ( st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_DFT ) && st->last_core_brate <= SID_2k40 )
    1355             :     {
    1356       23457 :         for ( i = 0; i < L_SHB_LAHEAD; i++ )
    1357             :         {
    1358       22340 :             hBWE_TD->syn_overlap[i] = shaped_shb_excitation[i] * subwin_shb[L_SHB_LAHEAD - i];
    1359       22340 :             hBWE_TD->syn_overlap[i] *= window_shb[L_SHB_LAHEAD - 1 - i] * GainFrame;
    1360             :         }
    1361             :     }
    1362             : 
    1363     1975507 :     ScaleShapedSHB( SHB_OVERLAP_LEN, shaped_shb_excitation, hBWE_TD->syn_overlap, GainShape, GainFrame, window_shb, subwin_shb );
    1364             : 
    1365     1975507 :     if ( hStereoICBWE != NULL )
    1366             :     {
    1367      263225 :         mvr2r( lpc_shb, hStereoICBWE->lpSHBRef, LPC_SHB_ORDER + 1 );
    1368      263225 :         mvr2r( GainShape, hStereoICBWE->gshapeRef, NUM_SHB_SUBFR );
    1369      263225 :         hStereoICBWE->gFrameRef = GainFrame;
    1370             : 
    1371      263225 :         mvr2r( shaped_shb_excitation, hStereoICBWE->shbSynthRef, L_FRAME16k );
    1372             :     }
    1373             : 
    1374     1975507 :     curr_frame_pow = sum2_f( shaped_shb_excitation, L_FRAME16k ) + 0.001f;
    1375     1975507 :     curr_frame_pow = min( curr_frame_pow, FLT_MAX );
    1376             : 
    1377     1975507 :     if ( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
    1378             :     {
    1379       59914 :         if ( ( curr_frame_pow > 2.0f * hBWE_TD->prev_swb_bwe_frame_pow ) &&
    1380       34646 :              ( curr_frame_pow < 30.0f * hBWE_TD->prev_swb_bwe_frame_pow ) &&
    1381       23650 :              st->prev_coder_type == UNVOICED )
    1382             :         {
    1383        1305 :             scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1384        1305 :             if ( curr_frame_pow == 0 )
    1385             :             {
    1386           0 :                 scale = 0;
    1387             :             }
    1388        1305 :             temp = (float) pow( scale, 0.125f );
    1389             :         }
    1390             :         else
    1391             :         {
    1392       58609 :             scale = 1.0f;
    1393       58609 :             temp = 1.0f;
    1394             :         }
    1395             : 
    1396      539226 :         for ( j = 0; j < 8; j++ )
    1397             :         {
    1398      479312 :             GainShape[2 * j] *= scale;
    1399      479312 :             GainShape[2 * j + 1] *= scale;
    1400    19651792 :             for ( i = 0; i < L_FRAME16k / 8; i++ )
    1401             :             {
    1402    19172480 :                 shaped_shb_excitation[i + j * L_FRAME16k / 8] *= scale;
    1403             :             }
    1404             : 
    1405      479312 :             scale /= temp;
    1406             :         }
    1407             :     }
    1408             : 
    1409             :     /* adjust the FEC frame energy */
    1410     1975507 :     if ( st->bfi )
    1411             :     {
    1412      141648 :         scale = 1.0f;
    1413      141648 :         temp = 1.0f;
    1414      141648 :         if ( st->nbLostCmpt == 1 )
    1415             :         {
    1416       59140 :             if ( curr_frame_pow > hBWE_TD->prev_swb_bwe_frame_pow && st->prev_coder_type != UNVOICED && st->last_good != UNVOICED_CLAS )
    1417             :             {
    1418        6114 :                 scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1419        6114 :                 if ( curr_frame_pow == 0 )
    1420             :                 {
    1421           0 :                     scale = 0;
    1422             :                 }
    1423        6114 :                 temp = (float) pow( scale, 0.125f );
    1424             :             }
    1425       53026 :             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          15 :                 scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1428          15 :                 if ( curr_frame_pow == 0 )
    1429             :                 {
    1430           0 :                     scale = 0;
    1431             :                 }
    1432          15 :                 temp = (float) pow( scale, 0.125f );
    1433             :             }
    1434             :         }
    1435       82508 :         else if ( st->nbLostCmpt > 1 )
    1436             :         {
    1437       82508 :             if ( curr_frame_pow > hBWE_TD->prev_swb_bwe_frame_pow )
    1438             :             {
    1439       20663 :                 scale = (float) sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow );
    1440       20663 :                 if ( curr_frame_pow == 0 )
    1441             :                 {
    1442           0 :                     scale = 0;
    1443             :                 }
    1444       20663 :                 temp = (float) pow( scale, 0.125f );
    1445             :             }
    1446       61845 :             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           6 :                 scale = (float) min( 2.0f, sqrt( hBWE_TD->prev_swb_bwe_frame_pow / curr_frame_pow ) );
    1449           6 :                 if ( curr_frame_pow == 0 )
    1450             :                 {
    1451           0 :                     scale = 0;
    1452             :                 }
    1453           6 :                 temp = (float) pow( scale, 0.125f );
    1454             :             }
    1455             :         }
    1456             : 
    1457     1274832 :         for ( j = 0; j < 8; j++ )
    1458             :         {
    1459     1133184 :             GainShape[2 * j] *= scale;
    1460     1133184 :             GainShape[2 * j + 1] *= scale;
    1461    46460544 :             for ( i = 0; i < L_FRAME16k / 8; i++ )
    1462             :             {
    1463    45327360 :                 shaped_shb_excitation[i + j * L_FRAME16k / 8] *= scale;
    1464             :             }
    1465             : 
    1466     1133184 :             scale /= temp;
    1467             :         }
    1468             :     }
    1469             : 
    1470     1975507 :     hBWE_TD->prev_swb_bwe_frame_pow = curr_frame_pow;
    1471             : 
    1472     1975507 :     st->prev_ener_shb = EPSILON;
    1473   634137747 :     for ( i = 0; i < L_FRAME16k; i++ )
    1474             :     {
    1475   632162240 :         st->prev_ener_shb += shaped_shb_excitation[i] * shaped_shb_excitation[i];
    1476             :     }
    1477     1975507 :     st->prev_ener_shb = (float) sqrt( st->prev_ener_shb / L_FRAME16k );
    1478             : 
    1479     1975507 :     if ( st->hBWE_FD != NULL )
    1480             :     {
    1481    29632605 :         for ( i = 0; i < SWB_FENV; i++ )
    1482             :         {
    1483    27657098 :             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     1975507 :     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     1975507 :     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     1975507 :     synth_scale = ( st->codec_mode == MODE1 ) ? 0.9f : 1.f;
    1494             : 
    1495     1975507 :     if ( st->output_Fs == 48000 )
    1496             :     {
    1497     1176202 :         if ( st->extl == FB_TBE )
    1498             :         {
    1499   179175780 :             for ( i = 0; i < L_FRAME16k; i++ )
    1500             :             {
    1501   178617600 :                 White_exc16k[i] *= GainFrame * GainShape[NUM_SHB_SUBFR * i / L_FRAME16k];
    1502             :             }
    1503             :         }
    1504             : 
    1505   753945482 :         for ( i = 0; i < L_FRAME32k; i++ )
    1506             :         {
    1507   752769280 :             error[i] *= synth_scale;
    1508             :         }
    1509             : 
    1510     1176202 :         interpolate_3_over_2_allpass( error, L_FRAME32k, synth, hBWE_TD->int_3_over_2_tbemem_dec );
    1511             :     }
    1512      799305 :     else if ( st->output_Fs == 32000 )
    1513             :     {
    1514   480025670 :         for ( i = 0; i < L_FRAME32k; i++ )
    1515             :         {
    1516   479276800 :             synth[i] = synth_scale * error[i];
    1517             :         }
    1518             :     }
    1519       50435 :     else if ( st->output_Fs == 16000 )
    1520             :     {
    1521    32328835 :         for ( i = 0; i < L_FRAME32k; i++ )
    1522             :         {
    1523    32278400 :             error[i] *= synth_scale;
    1524             :         }
    1525             : 
    1526       50435 :         Decimate_allpass_steep( error, hBWE_TD->mem_resamp_HB_32k, L_FRAME32k, synth );
    1527             :     }
    1528             : 
    1529             :     /* Update previous frame parameters for FEC */
    1530     1975507 :     mvr2r( lsf_shb, hBWE_TD->lsp_prevfrm, LPC_SHB_ORDER );
    1531     1975507 :     if ( st->codec_mode == MODE1 )
    1532             :     {
    1533     1962934 :         hBWE_TD->GainFrame_prevfrm = GainFrame;
    1534     1962934 :         hBWE_TD->tilt_swb_fec = tilt_swb_fec;
    1535             : 
    1536     1962934 :         if ( !st->bfi )
    1537             :         {
    1538     1821608 :             hBWE_TD->GainAttn = 1.0f;
    1539             :         }
    1540             :     }
    1541             :     else
    1542             :     {
    1543       12573 :         if ( !st->bfi )
    1544             :         {
    1545       12251 :             hBWE_TD->tilt_swb_fec = tilt_swb_fec;
    1546       12251 :             hBWE_TD->GainFrame_prevfrm = GainFrame; /* gain locking on lost frame */
    1547       12251 :             hBWE_TD->GainAttn = 1.0f;
    1548             :         }
    1549             :     }
    1550     1975507 :     hBWE_TD->prev_ener = ener_tmp[NUM_SHB_SUBGAINS - 1];
    1551     1975507 :     hBWE_TD->prev_GainShape = GainShape[NUM_SHB_SUBFR - 1];
    1552             : 
    1553     1975507 :     return;
    1554             : }
    1555             : 
    1556             : /*-------------------------------------------------------------------*
    1557             :  * Dequant_lower_LSF()
    1558             :  *
    1559             :  * Dequantized the lower LSFs
    1560             :  *-------------------------------------------------------------------*/
    1561             : 
    1562     1279943 : 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     1279943 :     lsf_q[0] = lsf_q_cb[0][lsf_idx[0]];
    1570     6399715 :     for ( i = 1; i < NUM_Q_LSF; i++ )
    1571             :     {
    1572     5119772 :         lsf_q[i] = lsf_q_cb[i][lsf_idx[i]] + lsf_q[i - 1];
    1573             :     }
    1574             : 
    1575     1279943 :     return;
    1576             : }
    1577             : 
    1578             : /*-------------------------------------------------------------------*
    1579             :  * Map_higher_LSF()
    1580             :  *
    1581             :  * Map the higher LSFs from the lower LSFs
    1582             :  *-------------------------------------------------------------------*/
    1583             : 
    1584     1279943 : 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     7679658 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1599             :     {
    1600     6399715 :         lsf_map[i] = 2 * m - lsf_q[NUM_MAP_LSF - 1 - i];
    1601             :     }
    1602             : 
    1603     1279943 :     if ( m > MAX_LSF / 2 )
    1604             :     {
    1605      316277 :         offset = lsf_map[0];
    1606      316277 :         scale = ( MAX_LSF - m ) / m;
    1607     1897662 :         for ( i = 0; i < NUM_MAP_LSF; i++ )
    1608             :         {
    1609     1581385 :             lsf_map[i] = ( lsf_map[i] - offset ) * scale + offset;
    1610             :         }
    1611             :     }
    1612             : 
    1613     1279943 :     last_q_lsf = lsf_q[NUM_Q_LSF - 1];
    1614     1279943 :     scale = MAX_LSF - last_q_lsf;
    1615             : 
    1616     7679658 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1617             :     {
    1618     6399715 :         grid[i] = grid_in[i] * scale + last_q_lsf;
    1619             :     }
    1620             : 
    1621     7679658 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1622             :     {
    1623     6399715 :         lsf_smooth[i] = ( 1 - grid_smoothing[i] ) * lsf_map[i] + grid_smoothing[i] * grid[i];
    1624             :     }
    1625             : 
    1626     7679658 :     for ( i = 0; i < NUM_MAP_LSF; i++ )
    1627             :     {
    1628     6399715 :         lsf_q[NUM_Q_LSF + i] = lsf_smooth[i];
    1629             :     }
    1630             : 
    1631     1279943 :     return;
    1632             : }
    1633             : 
    1634             : /*-------------------------------------------------------------------*
    1635             :  * Map_higher_LSF()
    1636             :  *
    1637             :  * Map the higher LSFs from the lower LSFs
    1638             :  *-------------------------------------------------------------------*/
    1639             : 
    1640     1279943 : 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     1279943 :     *m = mirror_point_q_cb[m_idx] + lsf_q[NUM_Q_LSF - 1];
    1647             : 
    1648     1279943 :     return;
    1649             : }
    1650             : 
    1651             : /*-------------------------------------------------------------------*
    1652             :  * dequantizeSHBparams()
    1653             :  *
    1654             :  * Dequantize super highband spectral envolope, temporal gains and frame gain
    1655             :  *-------------------------------------------------------------------*/
    1656             : 
    1657     2138764 : 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     2138764 :     int16_t nbits = NUM_BITS_SHB_MSLVQ;
    1686             : 
    1687     2138764 :     hBWE_TD = st->hBWE_TD;
    1688             : 
    1689             :     /* LSFs */
    1690     2138764 :     if ( extl == WB_TBE )
    1691             :     {
    1692      305969 :         if ( extl_brate == WB_TBE_0k35 )
    1693             :         {
    1694       67499 :             if ( st->codec_mode == MODE2 )
    1695             :             {
    1696        2872 :                 idxFrameGain = hBWE_TD->gFrame_WB;
    1697        2872 :                 idxLSF = hBWE_TD->lsf_WB;
    1698             :             }
    1699             :             else
    1700             :             {
    1701       64627 :                 idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FrameGain_LBR_WB );
    1702       64627 :                 idxLSF = get_next_indice( st, NUM_BITS_LBR_WB_LSF );
    1703             :             }
    1704             : 
    1705       67499 :             mvr2r( lbr_wb_bwe_lsfvq_cbook_2bit + idxLSF * LPC_SHB_ORDER_LBR_WB, Q_lsf, LPC_SHB_ORDER_LBR_WB );
    1706       67499 :             set_f( Q_subgain, RECIP_ROOT_EIGHT, NUM_SHB_SUBFR / 2 );
    1707       67499 :             mvr2r( SHBCB_FrameGain16 + idxFrameGain, Q_framegrain, 1 );
    1708             :         }
    1709             :         else
    1710             :         {
    1711             :             /* read the information about UNVOICED frame */
    1712      238470 :             *uv_flag = get_next_indice( st, 1 );
    1713             : 
    1714      238470 :             idxSubGain = get_next_indice( st, NUM_BITS_SHB_SUBGAINS );
    1715      238470 :             idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FrameGain );
    1716      238470 :             idxLSF = get_next_indice( st, NUM_BITS_WB_LSF );
    1717             : 
    1718      238470 :             mvr2r( wb_bwe_lsfvq_cbook_8bit + idxLSF * LPC_SHB_ORDER_WB, Q_lsf, LPC_SHB_ORDER_WB );
    1719      238470 :             mvr2r( HBCB_SubGain5bit + idxSubGain * NUM_SHB_SUBFR / 4, Q_combined_gains, NUM_SHB_SUBFR / 4 );
    1720             : 
    1721     1192350 :             for ( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    1722             :             {
    1723      953880 :                 Q_combined_gains[i] = (float) pow( 10.0f, Q_combined_gains[i] / 20.0f );
    1724             :             }
    1725             : 
    1726     1192350 :             for ( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
    1727             :             {
    1728      953880 :                 Q_subgain[i] = Q_combined_gains[i / 2];
    1729      953880 :                 Q_subgain[i + 1] = Q_combined_gains[i / 2];
    1730             :             }
    1731             : 
    1732             :             /* frame gain */
    1733      238470 :             mvr2r( SHBCB_FrameGain64 + idxFrameGain, Q_framegrain, 1 );
    1734             :         }
    1735             :     }
    1736             :     else
    1737             :     {
    1738     1832795 :         if ( st->codec_mode == MODE2 )
    1739             :         {
    1740       12171 :             idxSubGain = hBWE_TD->idxSubGains;
    1741       12171 :             idxFrameGain = hBWE_TD->idxFrameGain;
    1742             :         }
    1743             :         else
    1744             :         {
    1745     1820624 :             idxSubGain = get_next_indice( st, NUM_BITS_SHB_SUBGAINS );
    1746     1820624 :             if ( st->extl_brate == SWB_TBE_1k75 )
    1747             :             {
    1748       23365 :                 idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FRAMEGAIN_1k75 );
    1749             :             }
    1750             :             else
    1751             :             {
    1752     1797259 :                 idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FRAMEGAIN );
    1753             :             }
    1754             :         }
    1755             : 
    1756             :         /* Multi Source Flag */
    1757     1832795 :         if ( st->element_mode >= IVAS_CPE_DFT && !( st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) )
    1758             :         {
    1759      376561 :             *MSFlag = get_next_indice( st, STEREO_ICBWE_MSFLAG_BITS );
    1760             :         }
    1761             :         else
    1762             :         {
    1763     1456234 :             *MSFlag = 0;
    1764             :         }
    1765             : 
    1766     1832795 :         if ( st->extl_brate >= SWB_TBE_2k8 )
    1767             :         {
    1768      405230 :             if ( st->codec_mode == MODE2 )
    1769             :             {
    1770        7684 :                 idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
    1771             :             }
    1772             :             else
    1773             :             {
    1774      397546 :                 idx_shb_fr_gain = get_next_indice( st, NUM_BITS_SHB_ENER_SF );
    1775             :             }
    1776      405230 :             *Q_shb_ener_sf = usdequant( idx_shb_fr_gain, 0, 0.042f );
    1777      405230 :             *Q_shb_ener_sf = (float) pow( 10.0, *Q_shb_ener_sf );
    1778             : 
    1779     2431380 :             for ( i = 0; i < 5; i++ )
    1780             :             {
    1781     2026150 :                 if ( st->codec_mode == MODE2 )
    1782             :                 {
    1783       38420 :                     idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
    1784             :                 }
    1785             :                 else
    1786             :                 {
    1787     1987730 :                     idx_res_gs[i] = get_next_indice( st, NUM_BITS_SHB_RES_GS );
    1788             :                 }
    1789     2026150 :                 Q_shb_res_gshape[i] = usdequant( idx_res_gs[i], 0.125f, 0.125f );
    1790             :             }
    1791             : 
    1792      405230 :             if ( st->codec_mode == MODE2 )
    1793             :             {
    1794        7684 :                 idx_mixFac = hBWE_TD->idx_mixFac;
    1795             :             }
    1796             :             else
    1797             :             {
    1798      397546 :                 idx_mixFac = get_next_indice( st, NUM_BITS_SHB_VF );
    1799             :             }
    1800      405230 :             *Q_mixFactors = usdequant( idx_mixFac, 0.125f, 0.125f );
    1801             :         }
    1802             :         else
    1803             :         {
    1804     1427565 :             *Q_shb_ener_sf = 0;
    1805     1427565 :             if ( st->extl_brate == SWB_TBE_1k10 || st->extl_brate == SWB_TBE_1k75 )
    1806             :             {
    1807       26970 :                 idx_mixFac = get_next_indice( st, NUM_BITS_SHB_VF );
    1808       26970 :                 *Q_mixFactors = usdequant( idx_mixFac, 0.0f, 1.0f / ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) );
    1809             :             }
    1810             :             else
    1811             :             {
    1812     1400595 :                 *Q_mixFactors = 0;
    1813             :             }
    1814     1427565 :             set_f( Q_shb_res_gshape, 0, 5 );
    1815             :         }
    1816             : 
    1817     1832795 :         if ( ( st->extl_brate == SWB_TBE_0k95 || st->extl_brate == SWB_TBE_1k10 ) && st->codec_mode == MODE1 )
    1818             :         {
    1819      527536 :             set_s( lsf_idx, 0, 5 );
    1820      527536 :             lsf_idx[0] = get_next_indice( st, 8 );
    1821      527536 :             grid_idx = 0;
    1822      527536 :             m_idx = 0;
    1823             : 
    1824      527536 :             mvr2r( swb_tbe_lsfvq_cbook_8b + lsf_idx[0] * LPC_SHB_ORDER, Q_lsf, LPC_SHB_ORDER );
    1825             :         }
    1826     1305259 :         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     1303308 :             if ( st->extl_brate == SWB_TBE_1k75 )
    1830             :             {
    1831             :                 /* read multi-stage LVQ quantizer */
    1832       23365 :                 if ( nbits >= 19 )
    1833             :                 {
    1834       23365 :                     cb_stage = cb_LSF_BWE[0];
    1835             :                 }
    1836             :                 else
    1837             :                 {
    1838           0 :                     cb_stage = cb_LSF_BWE[1];
    1839             :                 }
    1840             : 
    1841       23365 :                 set_zero( lsf_q, LPC_SHB_ORDER );
    1842             : 
    1843             :                 /* VQ part */
    1844       23365 :                 num_bits_lvq = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3];
    1845       23365 :                 Idx = get_next_indice( st, num_bits_lvq );
    1846       23365 :                 v_add( lsf_q, cb_stage + Idx * 6, lsf_q, 6 );
    1847             : 
    1848             :                 /* MSLVQ part */
    1849       23365 :                 num_bits_lvq = nbits - num_bits_lvq - config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
    1850       23365 :                 predictor_bits = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
    1851       23365 :                 Idx_pred = 0;
    1852             : 
    1853       23365 :                 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       23365 :                     Idx_lvq = get_next_indice( st, num_bits_lvq );
    1863       23365 :                     deindex_lvq_SHB( Idx_lvq, out, num_bits_lvq, ( nbits < 19 ) );
    1864             :                 }
    1865             : 
    1866             :                 /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
    1867       23365 :                 v_add( lsf_q, out, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
    1868             : 
    1869             :                 /* predict last 2 components */
    1870       23365 :                 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       23365 :                     Idx_pred = get_next_indice( st, 1 );
    1878             : 
    1879       23365 :                     lsf_q[LATTICE_DIM] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * Idx_pred], LATTICE_DIM );
    1880       23365 :                     lsf_q[LATTICE_DIM + 1] = dotp( lsf_q, &LastCoefPred_1bit[2 * ( LATTICE_DIM + 1 ) * Idx_pred + LATTICE_DIM + 1], LATTICE_DIM );
    1881             :                 }
    1882             : 
    1883       23365 :                 if ( nbits < NUM_BITS_SHB_MSLVQ )
    1884             :                 {
    1885           0 :                     Idx_pred = get_next_indice( st, NUM_BITS_SHB_MSLVQ - nbits );
    1886             :                 }
    1887             : 
    1888       23365 :                 v_add( SHB_LSF_mean, lsf_q, lsf_q, LPC_SHB_ORDER );
    1889       23365 :                 v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
    1890             :             }
    1891             :             else
    1892             :             {
    1893     1279943 :                 if ( extl_brate == SWB_TBE_1k6 || extl_brate == FB_TBE_1k8 || extl_brate == SWB_TBE_2k8 || extl_brate == FB_TBE_3k0 )
    1894             :                 {
    1895     7679658 :                     for ( i = 0; i < NUM_Q_LSF; i++ )
    1896             :                     {
    1897     6399715 :                         if ( st->codec_mode == MODE2 )
    1898             :                         {
    1899       51100 :                             lsf_idx[i] = hBWE_TD->lsf_idx[i];
    1900             :                         }
    1901             :                         else
    1902             :                         {
    1903     6348615 :                             lsf_idx[i] = get_next_indice( st, lsf_q_num_bits[i] );
    1904             :                         }
    1905             :                     }
    1906             :                 }
    1907             : 
    1908     1279943 :                 Dequant_lower_LSF( lsf_idx, lsf_q );
    1909             : 
    1910     1279943 :                 if ( st->codec_mode == MODE2 )
    1911             :                 {
    1912       10220 :                     m_idx = hBWE_TD->m_idx;
    1913       10220 :                     grid_idx = hBWE_TD->grid_idx;
    1914             :                 }
    1915             :                 else
    1916             :                 {
    1917     1269723 :                     m_idx = get_next_indice( st, MIRROR_POINT_BITS );
    1918     1269723 :                     grid_idx = get_next_indice( st, NUM_LSF_GRID_BITS );
    1919             :                 }
    1920             : 
    1921     1279943 :                 Dequant_mirror_point( lsf_q, m_idx, &m );
    1922             : 
    1923             :                 /* safety check in case of bit errors */
    1924     1279943 :                 if ( m > MAX_LSF )
    1925             :                 {
    1926           0 :                     st->BER_detect = 1;
    1927           0 :                     m = MAX_LSF;
    1928             :                 }
    1929             : 
    1930     1279943 :                 Map_higher_LSF( lsf_q, m, lsf_grid[grid_idx] );
    1931             :             }
    1932    14336388 :             for ( i = 0; i < LPC_SHB_ORDER; i++ )
    1933             :             {
    1934             :                 /* safety check in case of bit errors */
    1935    13033080 :                 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    13033080 :                 Q_lsf[i] = 0.5f - lsf_q[LPC_SHB_ORDER - 1 - i];
    1941             :             }
    1942             :         }
    1943             :         else
    1944             :         {
    1945        1951 :             set_s( lsf_idx, 0, 5 );
    1946        1951 :             mvs2s( hBWE_TD->lsf_idx, lsf_idx, 5 );
    1947        1951 :             grid_idx = 0;
    1948        1951 :             m_idx = 0;
    1949        1951 :             mvr2r( swb_tbe_lsfvq_cbook_8b + lsf_idx[0] * LPC_SHB_ORDER, Q_lsf, LPC_SHB_ORDER );
    1950             :         }
    1951             : 
    1952     1832795 :         space_lsfs( Q_lsf, LPC_SHB_ORDER );
    1953             : 
    1954             :         /* Dequantize subgain indices */
    1955     1832795 :         j = idxSubGain * NUM_SHB_SUBGAINS;
    1956             : 
    1957     9163975 :         for ( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    1958             :         {
    1959     7331180 :             Q_subgain[i] = (float) pow( 10.0, SHBCB_SubGain5bit[j++] );
    1960             :         }
    1961             : 
    1962    31157515 :         for ( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    1963             :         {
    1964    29324720 :             Q_subgain[i] = Q_subgain[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    1965             :         }
    1966             : 
    1967             :         /* Frame gain */
    1968     1832795 :         if ( st->extl_brate == SWB_TBE_1k75 )
    1969             :         {
    1970       23365 :             *Q_framegrain = usdequant( idxFrameGain, SHB_GAIN_QLOW_1k75, SHB_GAIN_QDELTA_1k75 );
    1971             :         }
    1972             :         else
    1973             :         {
    1974     1809430 :             *Q_framegrain = usdequant( idxFrameGain, SHB_GAIN_QLOW, SHB_GAIN_QDELTA );
    1975             :         }
    1976             : 
    1977     1832795 :         *Q_framegrain = (float) pow( 10.0, *Q_framegrain );
    1978             :     }
    1979             : 
    1980     2138764 :     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      600488 : 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      600488 :     float ratio = 0, fb_exc_energy = 0;
    2001             :     float fb_synth[L_FRAME48k];
    2002             : 
    2003      600488 :     hBWE_TD = st->hBWE_TD;
    2004             : 
    2005      600488 :     if ( output_frame == L_FRAME48k )
    2006             :     {
    2007             :         /* decode FB slope information */
    2008      558180 :         if ( st->extl == FB_TBE && !st->bfi )
    2009             :         {
    2010      511713 :             if ( st->codec_mode == MODE2 )
    2011             :             {
    2012         452 :                 i = hBWE_TD->idxGain;
    2013             :             }
    2014             :             else
    2015             :             {
    2016      511261 :                 i = get_next_indice( st, NUM_BITS_FB_FRAMEGAIN_TBE );
    2017             :             }
    2018      511713 :             ratio = (float) ( 1 << i );
    2019             :         }
    2020       46467 :         else if ( st->extl == FB_TBE && st->bfi )
    2021             :         {
    2022       46467 :             ratio = hBWE_TD->prev_fbbwe_ratio;
    2023             :         }
    2024             :     }
    2025             :     else
    2026             :     {
    2027             :         /* update the position in the bitstream - needed in IVAS */
    2028       42308 :         if ( !st->bfi )
    2029             :         {
    2030       40931 :             st->next_bit_pos += NUM_BITS_FB_FRAMEGAIN;
    2031             :         }
    2032             : 
    2033       42308 :         return;
    2034             :     }
    2035             : 
    2036      558180 :     fb_exc_energy = sum2_f( fb_exc, L_FRAME16k );
    2037             : 
    2038             :     /* FB TBE synthesis */
    2039      558180 :     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      558180 :     if ( st->element_mode >= IVAS_CPE_DFT && st->idchan == 0 )
    2042             :     {
    2043       71740 :         mvr2r( fb_synth, fb_synth_ref, L_FRAME48k );
    2044             :     }
    2045             : 
    2046             :     /* add the fb_synth component to the hb_synth component */
    2047      558180 :     v_add( hb_synth, fb_synth, hb_synth, L_FRAME48k );
    2048             : 
    2049      558180 :     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       18908 : 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       18908 :     hBWE_TD = st->hBWE_TD;
    2067             : 
    2068       18908 :     if ( ( st->rf_flag || st->total_brate == ACELP_9k60 ) && st->bwidth == WB )
    2069             :     {
    2070             :         /* WB LSF */
    2071        2964 :         hBWE_TD->lsf_WB = get_next_indice( st, NUM_BITS_LBR_WB_LSF );
    2072             : 
    2073             :         /* WB frame gain */
    2074        2964 :         hBWE_TD->gFrame_WB = get_next_indice( st, NUM_BITS_SHB_FrameGain_LBR_WB );
    2075             :     }
    2076       15944 :     else if ( st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_32k && ( st->bwidth == SWB || st->bwidth == FB ) )
    2077             :     {
    2078       12251 :         if ( st->rf_flag == 0 && st->total_brate > ACELP_9k60 )
    2079             :         {
    2080       61320 :             for ( i = 0; i < NUM_Q_LSF; i++ )
    2081             :             {
    2082       51100 :                 hBWE_TD->lsf_idx[i] = get_next_indice( st, lsf_q_num_bits[i] );
    2083             :             }
    2084       10220 :             hBWE_TD->m_idx = get_next_indice( st, MIRROR_POINT_BITS );
    2085       10220 :             hBWE_TD->grid_idx = get_next_indice( st, NUM_LSF_GRID_BITS );
    2086             :         }
    2087             :         else
    2088             :         {
    2089        2031 :             hBWE_TD->lsf_idx[0] = get_next_indice( st, 8 );
    2090        2031 :             hBWE_TD->m_idx = 0;
    2091        2031 :             hBWE_TD->grid_idx = 0;
    2092             :         }
    2093             : 
    2094             :         /* shape gains */
    2095       12251 :         hBWE_TD->idxSubGains = get_next_indice( st, NUM_BITS_SHB_SUBGAINS );
    2096             : 
    2097             :         /* frame gain */
    2098       12251 :         hBWE_TD->idxFrameGain = get_next_indice( st, NUM_BITS_SHB_FRAMEGAIN );
    2099             : 
    2100       12251 :         if ( st->total_brate >= ACELP_24k40 )
    2101             :         {
    2102             :             /* sub frame energy*/
    2103        7684 :             hBWE_TD->idx_shb_fr_gain = get_next_indice( st, NUM_BITS_SHB_ENER_SF );
    2104             : 
    2105             :             /* gain shapes residual */
    2106       46104 :             for ( i = 0; i < NB_SUBFR16k; i++ )
    2107             :             {
    2108       38420 :                 hBWE_TD->idx_res_gs[i] = get_next_indice( st, NUM_BITS_SHB_RES_GS );
    2109             :             }
    2110             : 
    2111             :             /* voicing factor */
    2112        7684 :             hBWE_TD->idx_mixFac = get_next_indice( st, NUM_BITS_SHB_VF );
    2113             :         }
    2114             : 
    2115       12251 :         if ( st->tec_tfa == 1 )
    2116             :         {
    2117        9768 :             st->tec_flag = get_next_indice( st, BITS_TEC );
    2118        9768 :             st->tfa_flag = get_next_indice( st, BITS_TFA );
    2119             : 
    2120        9768 :             if ( st->tfa_flag && st->tec_flag )
    2121             :             {
    2122          57 :                 st->tec_flag = 2;
    2123          57 :                 st->tfa_flag = 0;
    2124             :             }
    2125             :         }
    2126             :         else
    2127             :         {
    2128        2483 :             st->tec_flag = 0;
    2129        2483 :             st->tfa_flag = 0;
    2130             :         }
    2131             :     }
    2132             : 
    2133       18908 :     if ( st->bwidth == FB )
    2134             :     {
    2135         452 :         hBWE_TD->idxGain = get_next_indice( st, 4 );
    2136             :     }
    2137             : 
    2138       18908 :     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      138948 : 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      138948 :     length = 2 * NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
    2165             : 
    2166             :     /* upsample overlap snippet */
    2167      138948 :     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      138948 :     if ( ( element_mode == EVS_MONO && ( rf_flag || total_brate == ACELP_9k60 ) ) || ( element_mode > EVS_MONO && L_frame == L_FRAME ) )
    2171             :     {
    2172       43576 :         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     3910252 :         for ( i = 0; i < 2 * SHB_OVERLAP_LEN; i++ )
    2177             :         {
    2178     3814880 :             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     5696868 :     for ( i = 0; i < 2 * L_SHB_LAHEAD; i++ )
    2184             :     {
    2185     5557920 :         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    21483210 :     for ( ; i < length; i++ )
    2190             :     {
    2191    21344262 :         outputHB[i] = hBWE_TD->old_tbe_synth[L_SHB_TRANSITION_LENGTH - 1 - i];
    2192             :     }
    2193             : 
    2194      138948 :     if ( output_Fs == 48000 )
    2195             :     {
    2196       87416 :         interpolate_3_over_2_allpass( outputHB, length, outputHB, hBWE_TD->int_3_over_2_tbemem_dec );
    2197             :     }
    2198       51532 :     else if ( output_Fs == 16000 )
    2199             :     {
    2200        1769 :         Decimate_allpass_steep( outputHB, hBWE_TD->mem_resamp_HB_32k, L_FRAME32k, outputHB );
    2201             :     }
    2202             : 
    2203      138948 :     return;
    2204             : }
    2205             : 
    2206             : /*---------------------------------------------------------------------*
    2207             :  * GenTransition_WB()
    2208             :  *
    2209             :  *
    2210             :  *---------------------------------------------------------------------*/
    2211             : 
    2212       11154 : 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       11154 :     length = 2 * NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
    2224             : 
    2225             :     /* upsample overlap snippet */
    2226       11154 :     Interpolate_allpass_steep( hBWE_TD->syn_overlap, hBWE_TD->state_lsyn_filt_shb, SHB_OVERLAP_LEN / 2, speech_buf_16k1 );
    2227       11154 :     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      234234 :     for ( i = 0; i < SHB_OVERLAP_LEN; i++ )
    2231             :     {
    2232      223080 :         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      234234 :     for ( i = 0; i < L_SHB_LAHEAD; i++ )
    2237             :     {
    2238      223080 :         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      223080 :         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      690356 :     for ( ; i < length; i++ )
    2244             :     {
    2245      679202 :         outputHB[i] = hBWE_TD->old_tbe_synth[L_SHB_TRANSITION_LENGTH - 1 - i];
    2246      679202 :         outputHB[i] *= 0.65f;
    2247             :     }
    2248             : 
    2249             :     /* upsampling if necessary */
    2250       11154 :     if ( output_Fs == 32000 )
    2251             :     {
    2252          97 :         Interpolate_allpass_steep( outputHB, hBWE_TD->mem_resamp_HB, L_FRAME16k, upsampled_synth );
    2253          97 :         mvr2r( upsampled_synth, outputHB, L_FRAME32k );
    2254             :     }
    2255       11057 :     else if ( output_Fs == 48000 )
    2256             :     {
    2257         471 :         interpolate_3_over_1_allpass( outputHB, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB );
    2258         471 :         mvr2r( upsampled_synth, outputHB, L_FRAME48k );
    2259             :     }
    2260             : 
    2261       11154 :     return;
    2262             : }
    2263             : 
    2264             : 
    2265             : /*---------------------------------------------------------------------*
    2266             :  * void TBEreset_dec()
    2267             :  *
    2268             :  *
    2269             :  *---------------------------------------------------------------------*/
    2270             : 
    2271     6729970 : void TBEreset_dec(
    2272             :     Decoder_State *st /* i/o: decoder state structure     */
    2273             : )
    2274             : {
    2275             :     TD_BWE_DEC_HANDLE hBWE_TD;
    2276             : 
    2277     6729970 :     hBWE_TD = st->hBWE_TD;
    2278             : 
    2279     6729970 :     if ( st->last_core != ACELP_CORE )
    2280             :     {
    2281     6695314 :         set_f( hBWE_TD->old_bwe_exc, 0.0f, PIT16k_MAX * 2 );
    2282     6695314 :         hBWE_TD->bwe_non_lin_prev_scale = 0.f;
    2283             :     }
    2284     6729970 :     if ( st->bwidth == WB )
    2285             :     {
    2286     1382239 :         wb_tbe_extras_reset( hBWE_TD->mem_genSHBexc_filt_down_wb2, hBWE_TD->mem_genSHBexc_filt_down_wb3 );
    2287     1382239 :         wb_tbe_extras_reset_synth( hBWE_TD->state_lsyn_filt_shb, hBWE_TD->state_lsyn_filt_dwn_shb, hBWE_TD->mem_resamp_HB );
    2288     1382239 :         set_f( hBWE_TD->mem_genSHBexc_filt_down_shb, 0, 7 );
    2289     1382239 :         set_f( hBWE_TD->state_lpc_syn, 0, 10 );
    2290     1382239 :         set_f( hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
    2291     1382239 :         set_f( hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
    2292     1382239 :         set_f( hBWE_TD->mem_csfilt, 0, 2 );
    2293             :     }
    2294     5347731 :     else if ( st->bwidth == SWB || st->bwidth == FB )
    2295             :     {
    2296     5347731 :         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     5347731 :         set_f( hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
    2299     5347731 :         set_f( hBWE_TD->int_3_over_2_tbemem_dec, 0.f, INTERP_3_2_MEM_LEN );
    2300     5347731 :         set_f( hBWE_TD->mem_resamp_HB_32k, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    2301     5347731 :         hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
    2302     5347731 :         hBWE_TD->prev_mix_factor = 1.0f;
    2303             : 
    2304     5347731 :         swb_tbe_reset_synth( hBWE_TD->genSHBsynth_Hilbert_Mem, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local );
    2305             : 
    2306     5347731 :         if ( st->bwidth == FB )
    2307             :         {
    2308     3072594 :             if ( st->hBWE_FD != NULL )
    2309             :             {
    2310     3072594 :                 st->hBWE_FD->prev_fb_ener_adjust = 0.0f;
    2311             :             }
    2312     3072594 :             set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
    2313     3072594 :             hBWE_TD->fb_tbe_demph = 0;
    2314     3072594 :             fb_tbe_reset_synth( hBWE_TD->fbbwe_hpf_mem, &hBWE_TD->prev_fbbwe_ratio );
    2315             :         }
    2316             :     }
    2317             : 
    2318     6729970 :     return;
    2319             : }
    2320             : 
    2321             : /*-------------------------------------------------------------------*
    2322             :  * td_bwe_dec_init()
    2323             :  *
    2324             :  * Initialize TD BWE state structure at the decoder
    2325             :  *-------------------------------------------------------------------*/
    2326             : 
    2327      157457 : 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      157457 :     set_f( hBWE_TD->old_bwe_exc, 0.0f, ( PIT16k_MAX * 2 ) );
    2337      157457 :     hBWE_TD->bwe_seed[0] = 23; /* 1; */
    2338      157457 :     hBWE_TD->bwe_seed[1] = 59; /* 10000; */
    2339      157457 :     set_f( hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
    2340      157457 :     hBWE_TD->bwe_non_lin_prev_scale = 0;
    2341             : 
    2342      157457 :     set_f( hBWE_TD->genSHBsynth_Hilbert_Mem, 0.0f, HILBERT_MEM_SIZE );
    2343      157457 :     set_f( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local, 0.0f, 2 * ALLPASSSECTIONS_STEEP );
    2344             : 
    2345      157457 :     hBWE_TD->syn_dm_phase = 0;
    2346      157457 :     hBWE_TD->prev_fbbwe_ratio = 1.0f;
    2347      157457 :     hBWE_TD->prev_wb_bwe_frame_pow = 0.001f;
    2348      157457 :     hBWE_TD->prev_swb_bwe_frame_pow = 0.001f;
    2349             : 
    2350             :     /* reset SHB buffers */
    2351      157457 :     ResetSHBbuffer_Dec( hBWE_TD, extl );
    2352             : 
    2353      157457 :     if ( output_Fs == 48000 )
    2354             :     {
    2355       82422 :         set_f( hBWE_TD->fbbwe_hpf_mem[0], 0, 4 );
    2356       82422 :         set_f( hBWE_TD->fbbwe_hpf_mem[1], 0, 4 );
    2357       82422 :         set_f( hBWE_TD->fbbwe_hpf_mem[2], 0, 4 );
    2358       82422 :         set_f( hBWE_TD->fbbwe_hpf_mem[3], 0, 4 );
    2359             :     }
    2360             : 
    2361      157457 :     set_f( hBWE_TD->mem_resamp_HB, 0, INTERP_3_1_MEM_LEN );
    2362      157457 :     set_f( hBWE_TD->mem_resamp_HB_32k, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    2363             : 
    2364      157457 :     hBWE_TD->tilt_mem = 0.0f;
    2365      157457 :     set_f( hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
    2366      157457 :     hBWE_TD->prev_tilt_para = 0.0f;
    2367      157457 :     set_f( hBWE_TD->cur_sub_Aq, 0.0f, M + 1 );
    2368      157457 :     set_f( hBWE_TD->int_3_over_2_tbemem_dec, 0.0f, INTERP_3_2_MEM_LEN );
    2369             : 
    2370             :     /* TD BWE post-processing */
    2371      157457 :     hBWE_TD->ptr_mem_stp_swb = hBWE_TD->mem_stp_swb + LPC_SHB_ORDER - 1;
    2372      157457 :     set_f( hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
    2373             : 
    2374     1732027 :     for ( i = 0; i < LPC_SHB_ORDER; i++ )
    2375             :     {
    2376     1574570 :         hBWE_TD->swb_lsp_prev_interp[i] = (float) cos( (float) i * EVS_PI / (float) 10.0f );
    2377             :     }
    2378             : 
    2379      157457 :     hBWE_TD->prev1_shb_ener_sf = 1.0f;
    2380      157457 :     hBWE_TD->prev2_shb_ener_sf = 1.0f;
    2381      157457 :     hBWE_TD->prev3_shb_ener_sf = 1.0f;
    2382      157457 :     hBWE_TD->prev_res_shb_gshape = 0.125f;
    2383      157457 :     hBWE_TD->prev_mixFactors = 0.5f;
    2384      157457 :     hBWE_TD->prev_GainShape = 0.0f;
    2385      157457 :     set_f( hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
    2386      157457 :     hBWE_TD->fb_tbe_demph = 0.0f;
    2387             : 
    2388      157457 :     set_f( hBWE_TD->old_hb_synth, 0, L_FRAME48k );
    2389             : 
    2390      157457 :     hBWE_TD->GainFrame_prevfrm = 0.0f;
    2391             : 
    2392      157457 :     hBWE_TD->prev_ener = 0.0f;
    2393             : 
    2394      157457 :     return;
    2395             : }

Generated by: LCOV version 1.14