LCOV - code coverage report
Current view: top level - lib_dec - swb_tbe_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ da9cc8ead0679b4682d329fdff98cf1616159273 Lines: 2311 3642 63.5 %
Date: 2025-10-13 22:24:20 Functions: 25 29 86.2 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : 
       6             : #include <stdlib.h>
       7             : #include "options.h"
       8             : #include "rom_com.h"
       9             : #include "prot_fx.h"
      10             : #include "rom_dec.h"
      11             : #include "stl.h"
      12             : 
      13             : #include "ivas_prot_fx.h"
      14             : 
      15             : /*-----------------------------------------------------------------*
      16             :  * Local functions
      17             :  *-----------------------------------------------------------------*/
      18             : 
      19             : static void dequantizeSHBparams_fx_9_1( Decoder_State *st_fx, const Word16 extl, Word32 extl_brate, Word16 *Q_lsf, Word16 *Q_subgain, Word32 *Q_framegrain, Word16 *uv_flag, Word32 *Q_shb_ener_sf_32, Word16 *Q_shb_res_gshape, Word16 *Q_mixFactors );
      20             : 
      21             : static void find_max_mem_dec( Decoder_State *st_fx, Word16 *n_mem, Word16 *n_mem2, Word16 *n_mem3 );
      22             : static void rescale_genSHB_mem_dec( Decoder_State *st_fx, Word16 sf );
      23             : static void find_max_mem_wb( Decoder_State *st_fx, Word16 *n_mem );
      24             : static void rescale_genWB_mem( Decoder_State *st_fx, Word16 sf );
      25             : static void Dequant_lower_LSF_fx( const Word16 lsf_idx[], Word16 lsf_q[] );
      26             : static void Map_higher_LSF_fx( Word16 lsf_q[], const Word16 m, const Word16 grid_in[] );
      27             : static void Dequant_mirror_point_fx( const Word16 lsf_q[], const Word16 m_idx, Word16 *m );
      28             : static Word16 dotp_loc( const Word16 x[], const Word32 y[], const Word16 n );
      29             : static void find_max_mem_dec_m3( Decoder_State *st, Word16 *n_mem3 );
      30             : 
      31             : /* gain shape concealment code */
      32             : static void gradientGainShape( Decoder_State *st_fx, Word16 *GainShape, Word32 *GainFrame );
      33             : 
      34             : /*-------------------------------------------------------------------*
      35             :  * find_max_mem_dec()
      36             :  *
      37             :  * Find norm and max in TBE memories and past buffers
      38             :  *-------------------------------------------------------------------*/
      39      106081 : static void find_max_mem_dec(
      40             :     Decoder_State *st_fx,
      41             :     Word16 *n_mem,
      42             :     Word16 *n_mem2,
      43             :     Word16 *n_mem3 )
      44             : {
      45             :     Word16 i;
      46             :     Word16 n_mem_32;
      47      106081 :     Word16 max = 0;
      48      106081 :     move16();
      49      106081 :     Word32 Lmax = 0;
      50      106081 :     move32();
      51      106081 :     Word16 tempQ15, max2 = 0;
      52      106081 :     move16();
      53             :     Word16 max3;
      54             :     Word32 tempQ32, Lmax3;
      55             :     TD_BWE_DEC_HANDLE hBWE_TD;
      56      106081 :     hBWE_TD = st_fx->hBWE_TD;
      57             : 
      58             :     /* old BWE exc max */
      59      106081 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
      60             :     {
      61       17589 :         FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
      62             :         {
      63       16236 :             tempQ15 = abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] );
      64       16236 :             max = s_max( max, tempQ15 );
      65             :         }
      66             :     }
      67             : 
      68             :     /* decimate all-pass steep memory */
      69      848648 :     FOR( i = 0; i < 7; i++ )
      70             :     {
      71      742567 :         tempQ15 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
      72      742567 :         max = s_max( max, tempQ15 );
      73             :     }
      74             : 
      75             :     /*  -- keep norm of state_lpc_syn_fx, state_syn_shbexc_fx,
      76             :            and mem_stp_swb_fx separately for 24.4 and 32kbps ----*/
      77             :     /* findMaxMem2() inside tbe com */
      78     1166891 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
      79             :     {
      80     1060810 :         tempQ15 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
      81     1060810 :         max2 = s_max( max2, tempQ15 );
      82             :     }
      83             : 
      84             :     /* findMaxMem2() inside tbe com */
      85     2227701 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
      86             :     {
      87     2121620 :         tempQ15 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
      88     2121620 :         max2 = s_max( max2, tempQ15 );
      89             :     }
      90             : 
      91             :     /* findMaxMem2() inside tbe com */
      92     1166891 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
      93             :     {
      94     1060810 :         tempQ15 = abs_s( hBWE_TD->mem_stp_swb_fx[i] );
      95     1060810 :         max2 = s_max( max2, tempQ15 );
      96             :     }
      97             : 
      98             :     /* for total_brate > 16.4kbps, use n_mem2; else account for the max2 for n_mem calculation */
      99      106081 :     *n_mem2 = norm_s( max2 );
     100      106081 :     move16();
     101      106081 :     if ( max2 == 0 )
     102             :     {
     103       85012 :         *n_mem2 = 15;
     104       85012 :         move16();
     105             :     }
     106             : 
     107      106081 :     if ( LT_32( st_fx->total_brate, ACELP_24k40 ) )
     108             :     {
     109       82335 :         max = s_max( max, max2 );
     110             :     }
     111             : 
     112             :     /* de-emph and pre-emph memory */
     113      106081 :     tempQ15 = abs_s( hBWE_TD->tbe_demph_fx );
     114      106081 :     max = s_max( max, tempQ15 );
     115             : 
     116      106081 :     tempQ15 = abs_s( hBWE_TD->tbe_premph_fx );
     117      106081 :     max = s_max( max, tempQ15 );
     118             : 
     119      106081 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
     120             :     {
     121      447359 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     122             :         {
     123      406690 :             tempQ15 = abs_s( hBWE_TD->fb_state_lpc_syn_fx[i] );
     124      406690 :             max = s_max( max, tempQ15 );
     125             :         }
     126             :         /* FB de-emph memory */
     127       40669 :         tempQ15 = abs_s( hBWE_TD->fb_tbe_demph_fx );
     128       40669 :         max = s_max( max, tempQ15 );
     129             :     }
     130             :     /* estimate the norm for 16-bit memories  */
     131      106081 :     *n_mem = norm_s( max );
     132      106081 :     move16();
     133      106081 :     if ( max == 0 )
     134             :     {
     135        9065 :         *n_mem = 15;
     136        9065 :         move16();
     137             :     }
     138             : 
     139             :     /* estimate the norm for 32-bit memories */
     140      106081 :     Lmax = L_abs( hBWE_TD->mem_csfilt_fx[0] ); /* only element [0] is used in env. shaping */
     141             : 
     142      106081 :     n_mem_32 = norm_l( Lmax );
     143      106081 :     if ( Lmax == 0 )
     144             :     {
     145       35708 :         n_mem_32 = 31;
     146       35708 :         move16();
     147             :     }
     148             : 
     149      106081 :     tempQ15 = sub( s_min( *n_mem, n_mem_32 ), 1 );
     150      106081 :     *n_mem = s_max( tempQ15, 0 );
     151      106081 :     move16();
     152             : 
     153             :     /* --------------------------------------------------------------*/
     154             :     /* Find headroom for synthesis stage associated with these memories:
     155             :           1. st_fx->syn_overlap_fx
     156             :           2. st_fx->genSHBsynth_state_lsyn_filt_shb_local
     157             :           3. st_fx->genSHBsynth_Hilbert_Mem_fx (32-bit) */
     158      106081 :     max3 = 0;
     159      106081 :     move16();
     160             :     /* find max in prev overlapSyn */
     161     2227701 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
     162             :     {
     163     2121620 :         tempQ15 = abs_s( hBWE_TD->syn_overlap_fx[i] );
     164     2121620 :         max3 = s_max( max3, tempQ15 );
     165             :     }
     166             :     /* find max in prev genSHBsynth_state_lsyn_filt_shb_local_fx */
     167      742567 :     FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
     168             :     {
     169      636486 :         tempQ15 = abs_s( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i] );
     170      636486 :         max3 = s_max( max3, tempQ15 );
     171             :     }
     172             :     /* find max in prev int_3_over_2_tbemem_dec_fx */
     173      106081 :     IF( EQ_32( st_fx->output_Fs, 48000 ) )
     174             :     {
     175     1173120 :         FOR( i = 0; i < INTERP_3_2_MEM_LEN; i++ )
     176             :         {
     177     1099800 :             tempQ15 = abs_s( hBWE_TD->int_3_over_2_tbemem_dec_fx[i] );
     178     1099800 :             max3 = s_max( max3, tempQ15 );
     179             :         }
     180             :     }
     181      106081 :     IF( EQ_32( st_fx->output_Fs, 16000 ) )
     182             :     {
     183       34880 :         FOR( i = 0; i < ( 2 * ALLPASSSECTIONS_STEEP + 1 ); i++ )
     184             :         {
     185       30520 :             tempQ15 = abs_s( hBWE_TD->mem_resamp_HB_32k_fx[i] );
     186       30520 :             max3 = s_max( max3, tempQ15 );
     187             :         }
     188             :     }
     189             :     /* estimate the norm for 16-bit memories  */
     190      106081 :     *n_mem3 = norm_s( max3 );
     191      106081 :     move16();
     192      106081 :     if ( max3 == 0 )
     193             :     {
     194        9005 :         *n_mem3 = 15;
     195        9005 :         move16();
     196             :     }
     197             : 
     198      106081 :     Lmax3 = 0;
     199      106081 :     move32();
     200      106081 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     201             :     {
     202             :         /* find max in prev genSHBsynth_Hilbert_Mem_fx */
     203     1098900 :         FOR( i = 0; i < HILBERT_MEM_SIZE; i++ )
     204             :         {
     205     1048950 :             tempQ32 = L_abs( hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i] );
     206     1048950 :             Lmax3 = L_max( Lmax3, tempQ32 );
     207             :         }
     208             :     }
     209             : 
     210             :     /* estimate the norm for 32-bit memories  */
     211      106081 :     n_mem_32 = norm_l( Lmax3 );
     212      106081 :     if ( Lmax3 == 0 )
     213             :     {
     214       60753 :         n_mem_32 = 31;
     215       60753 :         move16();
     216             :     }
     217             : 
     218      106081 :     tempQ15 = sub( s_min( *n_mem3, n_mem_32 ), 2 ); /* very important leave at least 2 bit head room
     219             :                                                        because of the Hilber transform and Q14 coeffs */
     220      106081 :     *n_mem3 = s_max( tempQ15, 0 );
     221      106081 :     move16();
     222             :     /* --------------------------------------------------------------*/
     223      106081 : }
     224             : 
     225             : /*-------------------------------------------------------------------*
     226             :  * rescale_genSHB_mem_dec()
     227             :  *
     228             :  * Rescale genSHB memories
     229             :  *-------------------------------------------------------------------*/
     230         683 : static void rescale_genSHB_mem_dec(
     231             :     Decoder_State *st_fx,
     232             :     Word16 sf )
     233             : {
     234             :     Word16 i;
     235             :     TD_BWE_DEC_HANDLE hBWE_TD;
     236         683 :     hBWE_TD = st_fx->hBWE_TD;
     237             : 
     238         683 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
     239             :     {
     240        8879 :         FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     241             :         {
     242        8196 :             hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
     243        8196 :             move16();
     244             :         }
     245             :     }
     246             : 
     247        5464 :     FOR( i = 0; i < 7; i++ )
     248             :     {
     249        4781 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
     250        4781 :         move16();
     251             :     }
     252             : 
     253             :     /*  -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
     254         683 :     IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
     255             :     {
     256        4070 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     257             :         {
     258        3700 :             hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
     259        3700 :             move16();
     260             :         }
     261             : 
     262        7770 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
     263             :         {
     264        7400 :             hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
     265        7400 :             move16();
     266             :         }
     267             :     }
     268             : 
     269         683 :     if ( EQ_16( st_fx->extl, FB_TBE ) )
     270             :     {
     271             :     }
     272         683 :     hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
     273         683 :     move32();
     274             : 
     275         683 :     hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
     276         683 :     move16();
     277         683 :     hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
     278         683 :     move16();
     279         683 : }
     280             : 
     281        7134 : void find_max_mem_wb( Decoder_State *st_fx, Word16 *n_mem )
     282             : {
     283             :     Word16 i;
     284        7134 :     Word16 max = 0;
     285        7134 :     move16();
     286        7134 :     Word32 Lmax = 0;
     287        7134 :     move32();
     288             :     Word16 n_mem_32;
     289             :     TD_BWE_DEC_HANDLE hBWE_TD;
     290        7134 :     hBWE_TD = st_fx->hBWE_TD;
     291             : 
     292        7134 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
     293             :     {
     294           0 :         FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     295             :         {
     296           0 :             max = s_max( max, abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] ) );
     297             :         }
     298             :     }
     299       57072 :     FOR( i = 0; i < 7; i++ )
     300             :     {
     301             : 
     302       49938 :         if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] ), max ) )
     303       18549 :             max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
     304             :     }
     305             : 
     306       57072 :     FOR( i = 0; i < 7; i++ )
     307             :     {
     308       49938 :         if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] ), max ) )
     309        8224 :             max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] );
     310             :     }
     311             : 
     312       57072 :     FOR( i = 0; i < 7; i++ )
     313             :     {
     314             : 
     315       49938 :         if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] ), max ) )
     316        2187 :             max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] );
     317             :     }
     318             : 
     319       78474 :     FOR( i = 0; i < 10; i++ )
     320             :     {
     321             : 
     322       71340 :         if ( GT_16( abs_s( hBWE_TD->state_lpc_syn_fx[i] ), max ) )
     323        2427 :             max = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
     324             :     }
     325             : 
     326       42804 :     FOR( i = 0; i < 5; i++ )
     327             :     {
     328             : 
     329       35670 :         if ( GT_16( abs_s( hBWE_TD->state_syn_shbexc_fx[i] ), max ) )
     330         526 :             max = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
     331             :     }
     332             : 
     333        7134 :     IF( max == 0 )
     334             :     {
     335         149 :         *n_mem = 15;
     336         149 :         move16();
     337             :     }
     338             :     ELSE
     339             :     {
     340        6985 :         *n_mem = norm_s( max );
     341        6985 :         move16();
     342             :     }
     343             : 
     344             : 
     345       21402 :     FOR( i = 0; i < 2; i++ )
     346             :     {
     347             : 
     348       14268 :         if ( GT_32( L_abs( hBWE_TD->mem_csfilt_fx[i] ), Lmax ) )
     349        6798 :             Lmax = L_abs( hBWE_TD->mem_csfilt_fx[i] );
     350             :     }
     351             : 
     352        7134 :     IF( Lmax == 0 )
     353             :     {
     354         336 :         n_mem_32 = 31;
     355         336 :         move16();
     356             :     }
     357             :     ELSE
     358             :     {
     359        6798 :         n_mem_32 = norm_l( Lmax );
     360             :     }
     361             : 
     362        7134 :     *n_mem = sub( s_min( *n_mem, n_mem_32 ), 1 );
     363        7134 :     move16();
     364        7134 :     *n_mem = s_max( *n_mem, 0 );
     365        7134 :     move16();
     366        7134 : }
     367             : 
     368        7134 : void rescale_genWB_mem( Decoder_State *st_fx, Word16 sf )
     369             : {
     370             :     Word16 i;
     371             :     TD_BWE_DEC_HANDLE hBWE_TD;
     372        7134 :     hBWE_TD = st_fx->hBWE_TD;
     373             : 
     374        7134 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
     375             :     {
     376           0 :         FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     377             :         {
     378           0 :             hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
     379           0 :             move16();
     380             :         }
     381             :     }
     382             : 
     383       78474 :     FOR( i = 0; i < 10; i++ )
     384             :     {
     385       71340 :         hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
     386       71340 :         move16();
     387             :     }
     388             : 
     389       42804 :     FOR( i = 0; i < 5; i++ )
     390             :     {
     391       35670 :         hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
     392       35670 :         move16();
     393             :     }
     394             : 
     395       57072 :     FOR( i = 0; i < 7; i++ )
     396             :     {
     397       49938 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
     398       49938 :         move16();
     399             :     }
     400             : 
     401       57072 :     FOR( i = 0; i < 7; i++ )
     402             :     {
     403       49938 :         hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i], sf );
     404       49938 :         move16();
     405             :     }
     406             : 
     407       57072 :     FOR( i = 0; i < 7; i++ )
     408             :     {
     409       49938 :         hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i], sf );
     410       49938 :         move16();
     411             :     }
     412             : 
     413       21402 :     FOR( i = 0; i < 2; i++ )
     414             :     {
     415       14268 :         hBWE_TD->mem_csfilt_fx[i] = L_shl( hBWE_TD->mem_csfilt_fx[i], sf );
     416       14268 :         move32();
     417             :     }
     418             : 
     419        7134 :     return;
     420             : }
     421             : 
     422             : 
     423        3257 : static void InitSWBdecBuffer_fx(
     424             :     TD_BWE_DEC_HANDLE hBWE_TD /* TD BWE data handle */ )
     425             : {
     426        3257 :     set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
     427        3257 :     hBWE_TD->bwe_seed[0] = 23;
     428        3257 :     move16();
     429        3257 :     hBWE_TD->bwe_seed[1] = 59;
     430        3257 :     move16();
     431             : 
     432        3257 :     set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
     433        3257 :     hBWE_TD->q_old_bwe_exc_extended_fx = Q15;
     434        3257 :     move16();
     435        3257 :     hBWE_TD->bwe_non_lin_prev_scale_fx = 0;
     436        3257 :     move16();
     437             : 
     438        3257 :     set32_fx( hBWE_TD->genSHBsynth_Hilbert_Mem_fx, 0, HILBERT_MEM_SIZE );
     439        3257 :     set16_fx( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, 0, 2 * ALLPASSSECTIONS_STEEP );    /* Interp all pass memory */
     440        3257 :     set32_fx( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP ); /* Interp all pass memory */
     441             : 
     442        3257 :     hBWE_TD->syn_dm_phase = 0;
     443        3257 :     move16();
     444        3257 :     hBWE_TD->prev_fbbwe_ratio_fx = 32767 /*1.0f Q15*/;
     445        3257 :     move16();
     446             : 
     447             :     /* these are fd-bwe constants */
     448        3257 :     hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
     449        3257 :     move32();
     450        3257 :     hBWE_TD->prev_swb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
     451        3257 :     move32();
     452        3257 :     hBWE_TD->prev_ener_fx_Q = 31;
     453        3257 :     move16();
     454        3257 :     hBWE_TD->prev_Qx = 0;
     455        3257 :     move16();
     456        3257 :     hBWE_TD->prev_frame_pow_exp = 0;
     457        3257 :     move16();
     458        3257 :     hBWE_TD->prev_Q_bwe_syn = 0;
     459        3257 :     move16();
     460        3257 :     hBWE_TD->prev_Q_bwe_syn2 = 0;
     461        3257 :     move16();
     462        3257 :     hBWE_TD->prev_hb_synth_fx_exp = 31;
     463        3257 :     move16();
     464             : 
     465        3257 :     return;
     466             : }
     467             : 
     468             : 
     469        5089 : void ResetSHBbuffer_Dec_fx(
     470             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
     471             :     const Word16 extl          /* i  : BWE extension layer     */
     472             : )
     473             : {
     474             :     Word16 i;
     475             :     Word16 f;
     476             :     Word16 inc;
     477             : 
     478        5089 :     IF( NE_16( extl, WB_TBE ) )
     479             :     {
     480        5089 :         f = 1489;
     481        5089 :         move16(); /* Q15 */
     482        5089 :         inc = 1489;
     483        5089 :         move16(); /* Q15 */
     484             :     }
     485             :     ELSE
     486             :     {
     487           0 :         f = 5461;
     488           0 :         move16(); /* Q15 */
     489           0 :         inc = 5461;
     490           0 :         move16(); /* Q15 */
     491             :     }
     492             : 
     493             :     /* states for the filters used in generating SHB excitation from WB excitation*/
     494        5089 :     set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
     495             : 
     496             :     /* states for the filters used in generating SHB signal from SHB excitation*/
     497        5089 :     set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
     498        5089 :     set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     499             : 
     500        5089 :     IF( EQ_16( extl, FB_TBE ) )
     501             :     {
     502           0 :         set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     503           0 :         hBWE_TD->fb_tbe_demph_fx = 0;
     504           0 :         move16();
     505           0 :         fb_tbe_reset_synth_fx( hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, &hBWE_TD->prev_fbbwe_ratio_fx );
     506             :     }
     507             :     /* states for the filters used in generating SHB signal from SHB excitation in wideband*/
     508        5089 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     509        5089 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     510        5089 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     511        5089 :     set16_fx( hBWE_TD->state_lsyn_filt_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
     512        5089 :     set32_fx( hBWE_TD->state_lsyn_filt_shb_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP );
     513        5089 :     set16_fx( hBWE_TD->state_lsyn_filt_dwn_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
     514        5089 :     set32_fx( hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP );
     515        5089 :     set16_fx( hBWE_TD->state_32and48k_WB_upsample_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
     516             : 
     517             :     /* States for the local synthesis filters */
     518        5089 :     set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
     519        5089 :     set32_fx( hBWE_TD->syn_overlap_fx_32, 0, L_SHB_LAHEAD );
     520             : 
     521             :     /* States for FEC */
     522             : 
     523        5089 :     IF( NE_16( extl, WB_TBE ) )
     524             :     {
     525       55979 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     526             :         {
     527       50890 :             hBWE_TD->lsp_prevfrm_fx[i] = f;
     528       50890 :             move16(); /*Q15*/
     529       50890 :             f = add( f, inc );
     530       50890 :             move16();
     531             :         }
     532             :     }
     533             :     ELSE
     534             :     {
     535           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     536             :         {
     537           0 :             hBWE_TD->lsp_prevfrm_fx[i] = f;
     538           0 :             move16(); /*Q15*/
     539           0 :             f = add( f, inc );
     540           0 :             move16();
     541             :         }
     542           0 :         FOR( ; i < LPC_SHB_ORDER; i++ )
     543             :         {
     544           0 :             hBWE_TD->lsp_prevfrm_fx[i] = 0;
     545           0 :             move16();
     546             :         }
     547             :     }
     548        5089 :     hBWE_TD->GainFrame_prevfrm_fx = 0;
     549        5089 :     move16(); /*Q18*/
     550        5089 :     hBWE_TD->GainAttn_fx = 32767;
     551        5089 :     move16(); /*Q15*/
     552        5089 :     hBWE_TD->tbe_demph_fx = 0;
     553        5089 :     move16();
     554        5089 :     hBWE_TD->tbe_premph_fx = 0;
     555        5089 :     move16();
     556        5089 :     set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
     557        5089 :     hBWE_TD->gain_prec_swb_fx = 16384; /*Q14 =1*/
     558        5089 :     move16();
     559        5089 :     set16_fx( hBWE_TD->GainShape_Delay_fx, 0, NUM_SHB_SUBFR / 2 );
     560        5089 :     hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /* Q0 1.f */
     561        5089 :     move32();
     562        5089 :     hBWE_TD->prev_mix_factor_fx = 32767; /*Q15 1.f*/
     563        5089 :     move16();
     564             : 
     565        5089 :     set16_fx( hBWE_TD->old_core_synth_fx, 0, L_FRAME16k );
     566        5089 :     set16_fx( hBWE_TD->old_tbe_synth_fx, 0, L_SHB_TRANSITION_LENGTH );
     567        5089 :     set32_fx( hBWE_TD->old_tbe_synth_fx_32, 0, L_SHB_TRANSITION_LENGTH );
     568        5089 :     hBWE_TD->tilt_swb_fec_fx = 0;
     569        5089 :     move16();
     570             : 
     571        5089 :     return;
     572             : }
     573             : 
     574             : 
     575             : /*==========================================================================*/
     576             : /* FUNCTION      : void wb_tbe_dec_fx ()                                    */
     577             : /*--------------------------------------------------------------------------*/
     578             : /* PURPOSE       : WB TBE decoder, 6 - 8 kHz band decoding module           */
     579             : /*--------------------------------------------------------------------------*/
     580             : /* INPUT ARGUMENTS  :                                                       */
     581             : /* _Word16 coder_type             i  : coding type                          */
     582             : /* _Word32 *bwe_exc_extended      i  : bandwidth extended exciatation 2*Q_exc*/
     583             : /* _Word16 Q_exc                  i  : Q format                             */
     584             : /* _Word16 voice_factors[]        i  : voicing factors          Q15         */
     585             : /*--------------------------------------------------------------------------*/
     586             : /* OUTPUT ARGUMENTS :                                                       */
     587             : /* _Word16 *synth               o  : WB synthesis/final synthesis  Q_synth  */
     588             : /*--------------------------------------------------------------------------*/
     589             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
     590             : /* Decoder_State *st_fx,       i/o: decoder state structure              */
     591             : /*--------------------------------------------------------------------------*/
     592             : /* RETURN ARGUMENTS :                                                       */
     593             : /*           _ None                                                         */
     594             : /*--------------------------------------------------------------------------*/
     595             : /* CALLED FROM :                                                            */
     596             : /*==========================================================================*/
     597        7134 : void ivas_wb_tbe_dec_fx(
     598             :     Decoder_State *st_fx,     /* i/o: decoder state structure         */
     599             :     const Word16 coder_type,  /* i  : coding type                 Q0     */
     600             :     Word32 *bwe_exc_extended, /* i  : bandwidth extended exciatation 2*Q_exc*/
     601             :     const Word16 Q_exc,
     602             :     const Word16 voice_factors[], /* i  : voicing factors              Q15   */
     603             :     Word16 *synth,                /* o  : WB synthesis/final synthesis  Q_synth  */
     604             :     Word16 *Q_synth )
     605             : {
     606             :     Word16 i;
     607             :     Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
     608             :     Word16 shaped_wb_excitation_frac[L_FRAME16k / 4];
     609             :     Word16 bwe_exc_extended_16[L_FRAME32k + 40];
     610             :     Word16 exc4kWhtnd[L_FRAME16k / 4];
     611             :     Word16 lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR];
     612             :     Word32 GainFrame;
     613             :     Word16 error[L_FRAME16k];
     614             :     Word16 synth_frac[L_FRAME16k];
     615             :     Word16 upsampled_synth[L_FRAME48k];
     616             :     Word32 tmp_synL[L_FRAME48k], upsampled_synth_32fx[L_FRAME48k];
     617             :     Word32 prev_pow, curr_pow, curr_frame_pow;
     618             :     Word16 curr_frame_pow_exp;
     619             :     Word16 temp, scale, n;
     620             :     Word16 j;
     621             : 
     622             :     Word16 Q_bwe_exc, Q_bwe_exc_ext, Qx;
     623             :     Word16 n_mem, cnt;
     624        7134 :     Word16 max = 0;
     625        7134 :     move16();
     626        7134 :     Word32 L_tmp, Lacc, Lscale, Lmax = 0;
     627        7134 :     move32();
     628             :     Word16 tmp, exp, sc;
     629             :     Word16 vf_modified[NB_SUBFR16k];
     630        7134 :     Word16 uv_flag = 0;
     631        7134 :     move16();
     632        7134 :     Word16 dummy = 0;
     633        7134 :     move16();
     634             :     Word32 dummy2[HILBERT_MEM_SIZE];
     635             :     Word16 f, inc;
     636             :     Word64 W_tmp;
     637             :     TD_BWE_DEC_HANDLE hBWE_TD;
     638        7134 :     hBWE_TD = st_fx->hBWE_TD;
     639        7134 :     set32_fx( &dummy2[0], 0, HILBERT_MEM_SIZE );
     640        7134 :     IF( st_fx->bws_cnt == 0 )
     641             :     {
     642             :         /* Initialization */
     643        7134 :         set16_fx( GainShape, 11469 /*0.35f Q15*/, NUM_SHB_SUBFR );
     644        7134 :         GainFrame = 1;
     645        7134 :         move32();
     646             : 
     647        7134 :         IF( !st_fx->bfi )
     648             :         {
     649        7101 :             IF( EQ_16( st_fx->use_partial_copy, 1 ) )
     650             :             {
     651           0 :                 IF( NE_16( st_fx->last_extl, WB_TBE ) )
     652             :                 {
     653           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
     654           0 :                     move32();
     655           0 :                     hBWE_TD->lsp_prevfrm_fx[0] = 3277 /*0.1f Q15*/;
     656           0 :                     move16();
     657           0 :                     FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
     658             :                     {
     659           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = add( hBWE_TD->lsp_prevfrm_fx[i - i], 3277 /*0.1f Q15*/ );
     660           0 :                         move16();
     661             :                     }
     662             :                 }
     663           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     664           0 :                 set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
     665             : 
     666           0 :                 IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
     667             :                 {
     668             :                     /* Frame gain */
     669           0 :                     st_fx->rf_indx_tbeGainFr = s_and( st_fx->rf_indx_tbeGainFr, 0xF ); /* only four LSBs are valid */
     670           0 :                     move16();
     671           0 :                     Copy32( SHBCB_FrameGain16_fx + st_fx->rf_indx_tbeGainFr, &GainFrame, 1 );
     672           0 :                     test();
     673           0 :                     test();
     674           0 :                     test();
     675           0 :                     test();
     676           0 :                     test();
     677           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && EQ_16( st_fx->last_extl, WB_TBE ) )
     678             :                     {
     679             :                         /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
     680           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx /*Q18*/, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame /*Q18*/, 6553 /*0.2f in Q15*/ ) /*Q18+Q15 - 15*/ ); /*Q18*/
     681             :                     }
     682             :                 }
     683             :                 ELSE
     684             :                 {
     685           0 :                     temp = 0;
     686           0 :                     move16();
     687             :                     /* Frame gain */
     688           0 :                     SWITCH( st_fx->rf_indx_tbeGainFr )
     689             :                     {
     690           0 :                         case 0:
     691           0 :                             GainFrame = 131072; /* 0.5f in Q18 */
     692           0 :                             move32();
     693           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
     694             :                             {
     695           0 :                                 temp = 26214 /*0.8 Q15*/;
     696           0 :                                 move16();
     697             :                             }
     698           0 :                             BREAK;
     699           0 :                         case 1:
     700           0 :                             GainFrame = 524288; /* 2.0f in Q18 */
     701           0 :                             move32();
     702           0 :                             test();
     703           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
     704             :                             {
     705           0 :                                 temp = 26214 /*0.8 Q15*/;
     706           0 :                                 move16();
     707             :                             }
     708           0 :                             BREAK;
     709           0 :                         case 2:
     710           0 :                             GainFrame = 1048576; /* 4.0f in Q18 */
     711           0 :                             move32();
     712           0 :                             test();
     713           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
     714             :                             {
     715           0 :                                 temp = 26214 /*0.8 Q15*/;
     716           0 :                                 move16();
     717             :                             }
     718           0 :                             BREAK;
     719           0 :                         case 3:
     720           0 :                             GainFrame = 2097152; /* 8.0f in Q18 */
     721           0 :                             move32();
     722           0 :                             test();
     723           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16 Q18*/ ) )
     724             :                             {
     725           0 :                                 temp = 26214 /*0.8 Q15*/;
     726           0 :                                 move16();
     727             :                             }
     728           0 :                             BREAK;
     729           0 :                         default:
     730           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
     731             :                     }
     732           0 :                     IF( EQ_16( st_fx->last_extl, WB_TBE ) )
     733             :                     {
     734             : 
     735           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
     736             :                     }
     737           0 :                     test();
     738           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
     739             :                     {
     740           0 :                         test();
     741           0 :                         test();
     742           0 :                         test();
     743           0 :                         test();
     744           0 :                         IF( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && LT_16( st_fx->prev_tilt_code_dec_fx, 1497 ) && GT_16( st_fx->prev_tilt_code_dec_fx, 200 ) )
     745             :                         {
     746           0 :                             GainFrame = Mult_32_16( GainFrame, 9830 /*0.3f in Q15*/ ); /*Q18*/
     747             :                         }
     748             :                     }
     749             :                 }
     750             :             }
     751             :             ELSE
     752             :             {
     753             :                 /* de-quantization */
     754             :                 Word16 ignore;
     755        7101 :                 ivas_dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0, &ignore );
     756             :             }
     757             :         }
     758             :         ELSE
     759             :         {
     760          33 :             IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
     761             :             {
     762           2 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     763             :             }
     764             :             ELSE
     765             :             {
     766          31 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_WB );
     767             :             }
     768          33 :             set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
     769             : 
     770          33 :             hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27853 /*0.85f in Q15*/ ); /*Q15*/
     771          33 :             move16();
     772             : 
     773          33 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
     774             :             {
     775          33 :                 GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx /*Q18*/, hBWE_TD->GainAttn_fx /*Q15*/ ); /*Q15+Q18-15*/
     776             :             }
     777             :             ELSE
     778             :             {
     779           0 :                 GainFrame = hBWE_TD->GainFrame_prevfrm_fx; /*Q18*/
     780           0 :                 move32();
     781             :             }
     782             :         }
     783             : 
     784        7134 :         IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
     785             :         {
     786             :             /* convert LSPs back into LP coeffs */
     787        2480 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
     788        2480 :             set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
     789       17360 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     790             :             {
     791       14880 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
     792       14880 :                 move16();
     793             :             }
     794       12400 :             FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
     795             :             {
     796        9920 :                 lpc_wb[i] = negate( lpc_wb[i] );
     797        9920 :                 move16();
     798             :             }
     799        2480 :             lpc_wb[0] = 4096; /*1.0f in Q12*/
     800        2480 :             move16();
     801             :         }
     802             :         ELSE
     803             :         {
     804             :             /* convert LSPs back into LP coeffs */
     805        4654 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
     806       32578 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     807             :             {
     808       27924 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
     809       27924 :                 move16();
     810             :             }
     811       32578 :             FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
     812             :             {
     813       27924 :                 lpc_wb[i] = negate( lpc_wb[i] );
     814       27924 :                 move16();
     815             :             }
     816        4654 :             lpc_wb[0] = 4096; /*1.0f in Q12*/
     817        4654 :             move16();
     818             :         }
     819             : 
     820        7134 :         Copy( voice_factors, vf_modified, NB_SUBFR16k );
     821        7134 :         IF( EQ_16( coder_type, VOICED ) )
     822             :         {
     823        4852 :             FOR( i = 1; i < NB_SUBFR; i++ )
     824             :             {
     825        3639 :                 vf_modified[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6554, voice_factors[i - 1] ) ); /* Q15 */
     826        3639 :                 move16();
     827             :             }
     828        1213 :             IF( NE_16( st_fx->L_frame, L_FRAME ) )
     829             :             {
     830           0 :                 vf_modified[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6554, voice_factors[3] ) ); /* Q15 */
     831           0 :                 move16();
     832             :             }
     833             :         }
     834             : 
     835             :         /* From low band excitation, generate highband excitation */
     836        7134 :         Lmax = 0;
     837        7134 :         move32();
     838     4658502 :         FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
     839             :         {
     840     4651368 :             Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
     841             :         }
     842        7134 :         IF( Lmax == 0 )
     843             :         {
     844           0 :             Q_bwe_exc = 31;
     845           0 :             move16();
     846             :         }
     847             :         ELSE
     848             :         {
     849        7134 :             Q_bwe_exc = norm_l( Lmax );
     850             :         }
     851        7134 :         Q_bwe_exc = sub( Q_bwe_exc, 3 );
     852        7134 :         Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
     853             : 
     854        7134 :         find_max_mem_wb( st_fx, &n_mem );
     855             : 
     856        7134 :         if ( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
     857             :         {
     858          67 :             Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
     859             :         }
     860             : 
     861        7134 :         test();
     862        7134 :         if ( uv_flag && GT_16( Q_bwe_exc, 20 ) )
     863             :         {
     864         182 :             Q_bwe_exc = 20;
     865         182 :             move16(); /* restrict this to 21 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
     866             :         }
     867             : 
     868        7134 :         W_tmp = 0;
     869        7134 :         move64();
     870        7134 :         IF( st_fx->element_mode > EVS_MONO )
     871             :         {
     872        7134 :             tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
     873        7134 :             W_tmp = L_shl( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
     874             :         }
     875       42804 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
     876             :         {
     877       35670 :             W_tmp = W_mac_16_16( W_tmp, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /*Q(2*(st_fx->prev_Q_bwe_exc-16 + 1))*/
     878             :         }
     879        7134 :         exp = W_norm( W_tmp );
     880        7134 :         prev_pow = W_extract_h( W_shl( W_tmp, exp ) );
     881        7134 :         exp = sub( add( add( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 1 ), exp ), 32 );
     882             : 
     883        7134 :         rescale_genWB_mem( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
     884             : 
     885        7134 :         sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
     886     4658502 :         FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
     887             :         {
     888     4651368 :             bwe_exc_extended_16[cnt] = round_fx( L_shl( bwe_exc_extended[cnt], sc ) );
     889     4651368 :             move16();
     890             :         }
     891             : 
     892        7134 :         Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     893             : 
     894        7134 :         Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
     895             : 
     896        7134 :         GenShapedWBExcitation_ivas_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
     897        7134 :                                        hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx,
     898        7134 :                                        hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, hBWE_TD->state_lpc_syn_fx, coder_type,
     899        7134 :                                        bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, vf_modified, uv_flag, st_fx->igf );
     900             : 
     901        7134 :         curr_pow = 0;
     902        7134 :         move32();
     903        7134 :         IF( st_fx->element_mode > EVS_MONO )
     904             :         {
     905        7134 :             tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
     906        7134 :             curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
     907             :         }
     908       42804 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
     909             :         {
     910       35670 :             curr_pow = L_mac0_sat( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
     911             :         }
     912             : 
     913        7134 :         if ( GT_16( voice_factors[0], 24576 ) )
     914             :         {
     915         264 :             curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
     916             :         }
     917             : 
     918        7134 :         Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow,
     919             :                                    exp, &exp );
     920             : 
     921       35670 :         FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
     922             :         {
     923       28536 :             L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] );             /* Q(16-exp+Q_bwe_exc_ext) */
     924       28536 :             shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
     925       28536 :             move16();
     926             :         }
     927        7134 :         Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
     928        7134 :         L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] );     /* Q(16-exp+Q_bwe_exc_ext) */
     929        7134 :         shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
     930        7134 :         move16();
     931             : 
     932             :         /* Update SHB excitation */
     933        7134 :         Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
     934             : 
     935             : 
     936             :         /* Adjust the subframe and frame gain of the synthesized shb signal */
     937             :         /* Scale the shaped excitation */
     938        7134 :         ScaleShapedWB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame,
     939             :                           window_wb_fx, subwin_wb_fx,
     940        7134 :                           Q_bwe_exc_ext, st_fx->L_frame, 0, &dummy, dummy, dummy2 );
     941             : 
     942        7134 :         max = 0;
     943        7134 :         move16();
     944      577854 :         FOR( i = 0; i < L_FRAME16k / 4; i++ )
     945             :         {
     946      570720 :             max = s_max( max, abs_s( shaped_wb_excitation[i] ) );
     947             :         }
     948             : 
     949        7134 :         IF( max == 0 )
     950             :         {
     951         110 :             curr_frame_pow = 1;
     952         110 :             move16();
     953         110 :             n = 0;
     954         110 :             move16();
     955             :         }
     956             :         ELSE
     957             :         {
     958        7024 :             n = norm_s( max );
     959      568944 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
     960             :             {
     961      561920 :                 shaped_wb_excitation_frac[i] = shl_sat( shaped_wb_excitation[i], n ); /*Q14*/
     962      561920 :                 move16();
     963             :             }
     964        7024 :             n = sub( 14, n );
     965        7024 :             curr_frame_pow = 1;
     966        7024 :             move32();
     967      568944 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
     968             :             {
     969      561920 :                 L_tmp = L_mult_sat( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i] ); /*Q29*/
     970      561920 :                 curr_frame_pow = L_add_sat( curr_frame_pow, L_shr( L_tmp, 7 ) );                  /*Q22*/
     971             :             }
     972             :         }
     973        7134 :         curr_frame_pow_exp = add( n, n );
     974             : 
     975        7134 :         IF( GT_16( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) )
     976             :         {
     977        6330 :             curr_frame_pow = L_shr( curr_frame_pow, sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) );
     978        6330 :             curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
     979        6330 :             move16();
     980             :         }
     981             :         ELSE
     982             :         {
     983         804 :             hBWE_TD->prev_wb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_wb_bwe_frame_pow_fx, sub( curr_frame_pow_exp, hBWE_TD->prev_frame_pow_exp ) );
     984         804 :             move32();
     985             :         }
     986             : 
     987        7134 :         test();
     988        7134 :         test();
     989        7134 :         IF( !st_fx->bfi && ( st_fx->prev_bfi || st_fx->prev_use_partial_copy ) )
     990             :         {
     991          31 :             IF( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_wb_bwe_frame_pow_fx ) )
     992             :             {
     993          26 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_wb_bwe_frame_pow_fx, 22, curr_frame_pow, 22, &exp );
     994          26 :                 scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
     995             : 
     996          26 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
     997          26 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
     998          26 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
     999          26 :                 temp = round_fx( L_shl( L_tmp, exp ) ); /* Q15 */
    1000             :             }
    1001             :             ELSE
    1002             :             {
    1003           5 :                 scale = temp = 32767;
    1004           5 :                 move16(); /* Q15 */
    1005           5 :                 move16();
    1006             :             }
    1007             : 
    1008         279 :             FOR( j = 0; j < 8; j++ )
    1009             :             {
    1010         248 :                 GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
    1011         248 :                 GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
    1012        2728 :                 FOR( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
    1013             :                 {
    1014        2480 :                     shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )] = mult_r( shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )], scale );
    1015        2480 :                     move16();
    1016             :                 }
    1017         248 :                 IF( temp > 0 )
    1018             :                 {
    1019         248 :                     IF( LT_16( scale, temp ) )
    1020             :                     {
    1021         208 :                         scale = div_s( scale, temp );
    1022             :                     }
    1023             :                     ELSE
    1024             :                     {
    1025          40 :                         scale = 32767;
    1026          40 :                         move16();
    1027             :                     }
    1028             :                 }
    1029             :                 ELSE
    1030             :                 {
    1031           0 :                     scale = 0;
    1032           0 :                     move16();
    1033             :                 }
    1034             :             }
    1035             :         }
    1036             : 
    1037        7134 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = curr_frame_pow;
    1038        7134 :         move32();
    1039        7134 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    1040        7134 :         move16();
    1041             : 
    1042             :         /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
    1043        7134 :         max = 0;
    1044        7134 :         move16();
    1045      613524 :         FOR( cnt = 0; cnt < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; cnt++ )
    1046             :         {
    1047      606390 :             if ( GT_16( abs_s( shaped_wb_excitation[cnt] ), max ) )
    1048             :             {
    1049       47074 :                 max = abs_s( shaped_wb_excitation[cnt] );
    1050             :             }
    1051             :         }
    1052        7134 :         Qx = norm_s( max );
    1053        7134 :         if ( max == 0 )
    1054             :         {
    1055           0 :             Qx = 15;
    1056           0 :             move16();
    1057             :         }
    1058             : 
    1059        7134 :         Qx = sub( Qx, 1 ); /* 1 bit space for saturation */
    1060             : 
    1061        7134 :         max = 0;
    1062        7134 :         move16();
    1063       49938 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1064             :         {
    1065       42804 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] ), max ) )
    1066       15734 :                 max = abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] );
    1067             :         }
    1068             : 
    1069       49938 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1070             :         {
    1071       42804 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] ), max ) )
    1072        4135 :                 max = abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] );
    1073             :         }
    1074             : 
    1075        7134 :         IF( EQ_32( st_fx->output_Fs, 32000 ) )
    1076             :         {
    1077        4837 :             FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1078             :             {
    1079        4146 :                 max = s_max( max, abs_s( hBWE_TD->state_32and48k_WB_upsample_fx[i] ) );
    1080             :             }
    1081             :         }
    1082        7134 :         IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1083             :         {
    1084       46368 :             FOR( i = 0; i < INTERP_3_1_MEM_LEN; i++ )
    1085             :             {
    1086       43056 :                 max = s_max( max, abs_s( hBWE_TD->mem_resamp_HB_fx[i] ) );
    1087             :             }
    1088             :         }
    1089        7134 :         n_mem = 15;
    1090        7134 :         move16();
    1091        7134 :         if ( max != 0 )
    1092             :         {
    1093        6763 :             n_mem = norm_s( max );
    1094             :         }
    1095        7134 :         n_mem = s_max( n_mem, 0 );
    1096             : 
    1097        7134 :         if ( GT_16( sub( Qx, hBWE_TD->prev_Qx ), n_mem ) )
    1098             :         {
    1099           3 :             Qx = add( hBWE_TD->prev_Qx, n_mem );
    1100             :         }
    1101             : 
    1102      613524 :         FOR( i = 0; i < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; i++ )
    1103             :         {
    1104      606390 :             shaped_wb_excitation[i] = shl( shaped_wb_excitation[i], Qx );
    1105      606390 :             move16();
    1106             :         }
    1107             : 
    1108       49938 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1109             :         {
    1110       42804 :             hBWE_TD->state_lsyn_filt_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1111       42804 :             move16();
    1112             :         }
    1113             : 
    1114       49938 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1115             :         {
    1116       42804 :             hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1117       42804 :             move16();
    1118             :         }
    1119             : 
    1120        7134 :         GenWBSynth_fx( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx );
    1121             : 
    1122        7134 :         Copy( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
    1123             : 
    1124     2290014 :         FOR( i = 0; i < L_FRAME16k; i++ )
    1125             :         {
    1126     2282880 :             synth[i] = mult_r( error[i], 21299 /*0.65f in Q15*/ );
    1127     2282880 :             move16();
    1128             :         }
    1129             : 
    1130        7134 :         IF( st_fx->hBWE_FD != NULL )
    1131             :         {
    1132        7134 :             max = 0;
    1133        7134 :             move16();
    1134     2290014 :             FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1135             :             {
    1136     2282880 :                 max = s_max( max, abs_s( synth[cnt] ) );
    1137             :             }
    1138             : 
    1139        7134 :             IF( max == 0 )
    1140             :             {
    1141          85 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = 0;
    1142          85 :                 move16();
    1143             :             }
    1144             :             ELSE
    1145             :             {
    1146        7049 :                 n = norm_s( max );
    1147     2262729 :                 FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1148             :                 {
    1149     2255680 :                     synth_frac[cnt] = shl( synth[cnt], n ); /*Q14*/
    1150     2255680 :                     move16();
    1151             :                 }
    1152        7049 :                 n = sub( sub( 14, n ), Qx );
    1153             : 
    1154        7049 :                 Lacc = 0;
    1155        7049 :                 move32();
    1156     2262729 :                 FOR( i = 0; i < L_FRAME16k; i++ )
    1157             :                 {
    1158     2255680 :                     L_tmp = L_mult( synth_frac[i], synth_frac[i] ); /* Q29 */
    1159     2255680 :                     Lacc = L_add( Lacc, L_shr( L_tmp, 7 ) );        /* Q22 */
    1160             :                 }
    1161             : 
    1162        7049 :                 L_tmp = Mult_32_16( Lacc, 102 ); /* Q22 */
    1163        7049 :                 exp = norm_l( L_tmp );
    1164        7049 :                 tmp = round_fx_sat( L_shl( L_tmp, exp ) );
    1165        7049 :                 exp = sub( add( exp, 22 ), 30 );
    1166        7049 :                 tmp = div_s( 16384, tmp );
    1167        7049 :                 L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                                                       /* Q(31-exp) */
    1168        7049 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = round_fx_sat( L_shl_sat( L_tmp, add( exp, sub( n, 12 ) ) ) ); /*  Q3 */
    1169        7049 :                 move16();
    1170             :             }
    1171             :         }
    1172             : 
    1173        7134 :         IF( EQ_32( st_fx->output_Fs, 32000 ) ) /* 32kHz sampling rate, but only WB output - interpolate */
    1174             :         {
    1175         691 :             Scale_sig( hBWE_TD->state_32and48k_WB_upsample_fx, 2 * ALLPASSSECTIONS_STEEP, sub( Qx, hBWE_TD->prev_Qx ) );
    1176         691 :             Interpolate_allpass_steep_fx( synth, hBWE_TD->state_32and48k_WB_upsample_fx, L_FRAME16k, upsampled_synth );
    1177         691 :             Copy( upsampled_synth, synth, L_FRAME32k );
    1178             :         }
    1179        6443 :         ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1180             :         {
    1181        3312 :             Copy_Scale_sig_16_32_no_sat( synth, tmp_synL, L_FRAME48k, sub( Q11, Qx ) );
    1182        3312 :             interpolate_3_over_1_allpass_fx32( tmp_synL, L_FRAME16k, upsampled_synth_32fx, hBWE_TD->mem_resamp_HB_fx_32 );
    1183        3312 :             Copy_Scale_sig_32_16( hBWE_TD->mem_resamp_HB_fx_32, hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( Qx, Q11 ) );
    1184        3312 :             Copy_Scale_sig_32_16( upsampled_synth_32fx, synth, L_FRAME48k, sub( Qx, Q11 ) );
    1185             :         }
    1186             :     }
    1187             :     ELSE
    1188             :     {
    1189           0 :         f = 5461;
    1190           0 :         move16(); /* Q15 */
    1191           0 :         inc = 5461;
    1192           0 :         move16(); /* Q15 */
    1193           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1194             :         {
    1195           0 :             lsf_wb[i] = f;
    1196           0 :             move16(); /*Q15*/
    1197           0 :             f = add_sat( f, inc );
    1198           0 :             move16();
    1199             :         }
    1200           0 :         GainFrame = 0; /* Q18 */
    1201           0 :         move32();
    1202           0 :         Qx = 0;
    1203           0 :         move16();
    1204           0 :         Q_bwe_exc = 31;
    1205           0 :         move16();
    1206           0 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
    1207           0 :         move32();
    1208           0 :         hBWE_TD->prev_frame_pow_exp = 0;
    1209           0 :         move16();
    1210             :     }
    1211             : 
    1212             :     /* Update previous frame parameters for FEC */
    1213        7134 :     IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1214             :     {
    1215        2480 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_LBR_WB );
    1216             :     }
    1217             :     ELSE
    1218             :     {
    1219        4654 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_WB );
    1220             :     }
    1221        7134 :     hBWE_TD->GainFrame_prevfrm_fx = GainFrame; /* Q18 */
    1222        7134 :     move32();
    1223             : 
    1224        7134 :     if ( !st_fx->bfi )
    1225             :     {
    1226        7101 :         hBWE_TD->GainAttn_fx = 32767;
    1227        7101 :         move16();
    1228             :     }
    1229             : 
    1230        7134 :     *Q_synth = Qx;
    1231        7134 :     move16();
    1232             : 
    1233        7134 :     st_fx->prev_Q_bwe_exc = Q_bwe_exc;
    1234        7134 :     move16();
    1235        7134 :     hBWE_TD->prev_Qx = Qx;
    1236        7134 :     move16();
    1237             : 
    1238        7134 :     return;
    1239             : }
    1240             : 
    1241             : 
    1242           0 : void wb_tbe_dec_fx(
    1243             :     Decoder_State *st_fx,     /* i/o: decoder state structure         */
    1244             :     const Word16 coder_type,  /* i  : coding type                     */
    1245             :     Word32 *bwe_exc_extended, /* i  : bandwidth extended exciatation 2*Q_exc*/
    1246             :     const Word16 Q_exc,
    1247             :     const Word16 voice_factors[], /* i  : voicing factors                 */
    1248             :     Word16 *synth,                /* o  : WB synthesis/final synthesis    */
    1249             :     Word16 *Q_synth )
    1250             : {
    1251             :     Word16 i;
    1252             :     Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
    1253             :     Word16 shaped_wb_excitation_frac[L_FRAME16k / 4];
    1254             :     Word16 bwe_exc_extended_16[L_FRAME32k + 40];
    1255             :     Word16 exc4kWhtnd[L_FRAME16k / 4];
    1256             :     Word16 lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR];
    1257             :     Word32 GainFrame;
    1258             :     Word16 error[L_FRAME16k];
    1259             :     Word16 synth_frac[L_FRAME16k];
    1260             :     Word16 upsampled_synth[L_FRAME48k];
    1261             :     Word32 prev_pow, curr_pow, curr_frame_pow;
    1262             :     Word16 curr_frame_pow_exp;
    1263             :     Word16 temp, scale, n;
    1264             :     Word16 j;
    1265             : 
    1266             :     Word16 Q_bwe_exc, Q_bwe_exc_ext, Qx;
    1267             :     Word16 n_mem, cnt;
    1268           0 :     Word16 max = 0;
    1269           0 :     move16();
    1270           0 :     Word32 L_tmp, Lacc, Lscale, Lmax = 0;
    1271           0 :     move32();
    1272             :     Word16 tmp, exp, sc;
    1273             :     Word16 vf_modified[NB_SUBFR16k];
    1274           0 :     Word16 uv_flag = 0;
    1275           0 :     move16();
    1276           0 :     Word16 dummy = 0;
    1277           0 :     move16();
    1278             :     Word32 dummy2[HILBERT_MEM_SIZE];
    1279             :     Word16 f, inc;
    1280             :     TD_BWE_DEC_HANDLE hBWE_TD;
    1281           0 :     hBWE_TD = st_fx->hBWE_TD;
    1282           0 :     set32_fx( &dummy2[0], 0, HILBERT_MEM_SIZE );
    1283           0 :     IF( st_fx->bws_cnt == 0 )
    1284             :     {
    1285             :         /* Initialization */
    1286           0 :         set16_fx( GainShape, 11469 /*0.35f Q15*/, NUM_SHB_SUBFR );
    1287           0 :         GainFrame = 1;
    1288           0 :         move32();
    1289             : 
    1290           0 :         IF( !st_fx->bfi )
    1291             :         {
    1292           0 :             IF( EQ_16( st_fx->use_partial_copy, 1 ) )
    1293             :             {
    1294           0 :                 IF( NE_16( st_fx->last_extl, WB_TBE ) )
    1295             :                 {
    1296           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
    1297           0 :                     move32();
    1298           0 :                     hBWE_TD->lsp_prevfrm_fx[0] = 3277 /*0.1f Q15*/;
    1299           0 :                     move16();
    1300           0 :                     FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
    1301             :                     {
    1302           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = add( hBWE_TD->lsp_prevfrm_fx[i - i], 3277 /*0.1f Q15*/ ); /*Q15*/
    1303           0 :                         move16();
    1304             :                     }
    1305             :                 }
    1306           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
    1307           0 :                 set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    1308             : 
    1309           0 :                 IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
    1310             :                 {
    1311             :                     /* Frame gain */
    1312           0 :                     st_fx->rf_indx_tbeGainFr = s_and( st_fx->rf_indx_tbeGainFr, 0xF ); /* only four LSBs are valid */
    1313           0 :                     move16();
    1314           0 :                     Copy32( SHBCB_FrameGain16_fx + st_fx->rf_indx_tbeGainFr, &GainFrame, 1 );
    1315           0 :                     test();
    1316           0 :                     test();
    1317           0 :                     test();
    1318           0 :                     test();
    1319           0 :                     test();
    1320           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && EQ_16( st_fx->last_extl, WB_TBE ) )
    1321             :                     {
    1322             :                         /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
    1323           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6553 ) ); /*Q18*/
    1324             :                     }
    1325             :                 }
    1326             :                 ELSE
    1327             :                 {
    1328           0 :                     temp = 0;
    1329           0 :                     move16();
    1330             :                     /* Frame gain */
    1331           0 :                     SWITCH( st_fx->rf_indx_tbeGainFr )
    1332             :                     {
    1333           0 :                         case 0:
    1334           0 :                             GainFrame = 131072; /* 0.5f in Q18 */
    1335           0 :                             move32();
    1336           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
    1337             :                             {
    1338           0 :                                 temp = 26214 /*0.8 Q15*/;
    1339           0 :                                 move16();
    1340             :                             }
    1341           0 :                             BREAK;
    1342           0 :                         case 1:
    1343           0 :                             GainFrame = 524288; /* 2.0f in Q18 */
    1344           0 :                             move32();
    1345           0 :                             test();
    1346           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
    1347             :                             {
    1348           0 :                                 temp = 26214 /*0.8 Q15*/;
    1349           0 :                                 move16();
    1350             :                             }
    1351           0 :                             BREAK;
    1352           0 :                         case 2:
    1353           0 :                             GainFrame = 1048576; /* 4.0f in Q18 */
    1354           0 :                             move32();
    1355           0 :                             test();
    1356           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
    1357             :                             {
    1358           0 :                                 temp = 26214 /*0.8 Q15*/;
    1359           0 :                                 move16();
    1360             :                             }
    1361           0 :                             BREAK;
    1362           0 :                         case 3:
    1363           0 :                             GainFrame = 2097152; /* 8.0f in Q18 */
    1364           0 :                             move32();
    1365           0 :                             test();
    1366           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16 Q18*/ ) )
    1367             :                             {
    1368           0 :                                 temp = 26214 /*0.8 Q15*/;
    1369           0 :                                 move16();
    1370             :                             }
    1371           0 :                             BREAK;
    1372           0 :                         default:
    1373           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
    1374             :                     }
    1375           0 :                     IF( EQ_16( st_fx->last_extl, WB_TBE ) )
    1376             :                     {
    1377             : 
    1378           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
    1379             :                     }
    1380           0 :                     test();
    1381           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
    1382             :                     {
    1383           0 :                         test();
    1384           0 :                         test();
    1385           0 :                         test();
    1386           0 :                         test();
    1387           0 :                         IF( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && LT_16( st_fx->prev_tilt_code_dec_fx, 1497 ) && GT_16( st_fx->prev_tilt_code_dec_fx, 200 ) )
    1388             :                         {
    1389           0 :                             GainFrame = Mult_32_16( GainFrame, 9830 ); /*Q18*/
    1390             :                         }
    1391             :                     }
    1392             :                 }
    1393             :             }
    1394             :             ELSE
    1395             :             {
    1396             :                 /* de-quantization */
    1397           0 :                 dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0 );
    1398             :             }
    1399             :         }
    1400             :         ELSE
    1401             :         {
    1402           0 :             IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1403             :             {
    1404           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
    1405             :             }
    1406             :             ELSE
    1407             :             {
    1408           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_WB ); /*Q15*/
    1409             :             }
    1410           0 :             set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    1411             : 
    1412           0 :             hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27853 );
    1413           0 :             move16();
    1414             : 
    1415           0 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    1416             :             {
    1417           0 :                 GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, hBWE_TD->GainAttn_fx ); /*Q18*/
    1418             :             }
    1419             :             ELSE
    1420             :             {
    1421           0 :                 GainFrame = hBWE_TD->GainFrame_prevfrm_fx; /*Q18*/
    1422           0 :                 move32();
    1423             :             }
    1424             :         }
    1425             : 
    1426           0 :         IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1427             :         {
    1428             :             /* convert LSPs back into LP coeffs */
    1429           0 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
    1430           0 :             set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB );
    1431           0 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1432             :             {
    1433           0 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
    1434           0 :                 move16();
    1435             :             }
    1436           0 :             FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
    1437             :             {
    1438           0 :                 lpc_wb[i] = negate( lpc_wb[i] );
    1439           0 :                 move16();
    1440             :             }
    1441           0 :             lpc_wb[0] = 4096;
    1442           0 :             move16();
    1443             :         }
    1444             :         ELSE
    1445             :         {
    1446             :             /* convert LSPs back into LP coeffs */
    1447           0 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
    1448           0 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1449             :             {
    1450           0 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
    1451           0 :                 move16();
    1452             :             }
    1453           0 :             FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
    1454             :             {
    1455           0 :                 lpc_wb[i] = negate( lpc_wb[i] );
    1456           0 :                 move16();
    1457             :             }
    1458           0 :             lpc_wb[0] = 4096;
    1459           0 :             move16();
    1460             :         }
    1461             : 
    1462           0 :         Copy( voice_factors, vf_modified, NB_SUBFR16k );
    1463           0 :         IF( EQ_16( coder_type, VOICED ) )
    1464             :         {
    1465           0 :             FOR( i = 1; i < NB_SUBFR; i++ )
    1466             :             {
    1467           0 :                 vf_modified[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6554, voice_factors[i - 1] ) ); /* Q15 */
    1468           0 :                 move16();
    1469             :             }
    1470           0 :             IF( NE_16( st_fx->L_frame, L_FRAME ) )
    1471             :             {
    1472           0 :                 vf_modified[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6554, voice_factors[3] ) ); /* Q15 */
    1473           0 :                 move16();
    1474             :             }
    1475             :         }
    1476             : 
    1477             :         /* From low band excitation, generate highband excitation */
    1478           0 :         Lmax = 0;
    1479           0 :         move32();
    1480           0 :         FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    1481             :         {
    1482           0 :             Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
    1483             :         }
    1484           0 :         IF( Lmax == 0 )
    1485             :         {
    1486           0 :             Q_bwe_exc = 31;
    1487           0 :             move16();
    1488             :         }
    1489             :         ELSE
    1490             :         {
    1491           0 :             Q_bwe_exc = norm_l( Lmax );
    1492             :         }
    1493           0 :         Q_bwe_exc = sub( Q_bwe_exc, 1 );
    1494           0 :         Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
    1495             : 
    1496           0 :         find_max_mem_wb( st_fx, &n_mem );
    1497             : 
    1498           0 :         if ( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
    1499             :         {
    1500           0 :             Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
    1501             :         }
    1502             : 
    1503           0 :         test();
    1504           0 :         if ( uv_flag && GT_16( Q_bwe_exc, 20 ) )
    1505             :         {
    1506           0 :             Q_bwe_exc = 20;
    1507           0 :             move16(); /* restrict this to 21 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
    1508             :         }
    1509             : 
    1510           0 :         prev_pow = 0;
    1511           0 :         move32();
    1512           0 :         IF( st_fx->element_mode > EVS_MONO )
    1513             :         {
    1514           0 :             tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
    1515           0 :             prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
    1516             :         }
    1517           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1518             :         {
    1519           0 :             prev_pow = L_mac0_sat( prev_pow, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
    1520             :         }
    1521             : 
    1522           0 :         rescale_genWB_mem( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
    1523             : 
    1524           0 :         Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
    1525           0 :         sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
    1526           0 :         FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    1527             :         {
    1528           0 :             bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
    1529             :         }
    1530           0 :         Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
    1531             : 
    1532           0 :         Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
    1533             : 
    1534           0 :         Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
    1535             : 
    1536           0 :         GenShapedWBExcitation_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
    1537           0 :                                   hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx,
    1538           0 :                                   hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, hBWE_TD->state_lpc_syn_fx, coder_type,
    1539           0 :                                   bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, vf_modified, uv_flag, st_fx->igf );
    1540             : 
    1541           0 :         curr_pow = 0;
    1542           0 :         move32();
    1543           0 :         IF( st_fx->element_mode > EVS_MONO )
    1544             :         {
    1545           0 :             tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
    1546           0 :             curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
    1547             :         }
    1548           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1549             :         {
    1550           0 :             curr_pow = L_mac0_sat( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
    1551             :         }
    1552             : 
    1553           0 :         if ( GT_16( voice_factors[0], 24576 ) )
    1554             :         {
    1555           0 :             curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
    1556             :         }
    1557             : 
    1558           0 :         Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow,
    1559           0 :                                    shl_r( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), &exp );
    1560             : 
    1561           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
    1562             :         {
    1563           0 :             L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] );             /* Q(16-exp+Q_bwe_exc_ext) */
    1564           0 :             shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1565           0 :             move16();
    1566             :         }
    1567           0 :         Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
    1568           0 :         L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] );     /* Q(16-exp+Q_bwe_exc_ext) */
    1569           0 :         shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1570             : 
    1571             :         /* Update SHB excitation */
    1572           0 :         Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
    1573             : 
    1574             : 
    1575             :         /* Adjust the subframe and frame gain of the synthesized shb signal */
    1576             :         /* Scale the shaped excitation */
    1577           0 :         ScaleShapedWB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame,
    1578             :                           window_wb_fx, subwin_wb_fx,
    1579           0 :                           Q_bwe_exc_ext, st_fx->L_frame, 0, &dummy, dummy, dummy2 );
    1580             : 
    1581           0 :         max = 0;
    1582           0 :         move16();
    1583           0 :         FOR( i = 0; i < L_FRAME16k / 4; i++ )
    1584             :         {
    1585           0 :             max = s_max( max, shaped_wb_excitation[i] ); /*Q0*/
    1586             :         }
    1587             : 
    1588           0 :         IF( max == 0 )
    1589             :         {
    1590           0 :             curr_frame_pow = 1;
    1591           0 :             move32();
    1592           0 :             n = 0;
    1593           0 :             move16();
    1594             :         }
    1595             :         ELSE
    1596             :         {
    1597           0 :             n = norm_s( max );
    1598           0 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
    1599             :             {
    1600           0 :                 shaped_wb_excitation_frac[i] = shl_sat( shaped_wb_excitation[i], n ); /*Q14*/
    1601           0 :                 move16();
    1602             :             }
    1603           0 :             n = sub( 14, n );
    1604           0 :             curr_frame_pow = 1;
    1605           0 :             move32();
    1606           0 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
    1607             :             {
    1608           0 :                 L_tmp = L_mult_sat( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i] ); /*Q29*/
    1609           0 :                 curr_frame_pow = L_add_sat( curr_frame_pow, L_shr( L_tmp, 7 ) );                  /*Q22*/
    1610             :             }
    1611             :         }
    1612           0 :         curr_frame_pow_exp = add( n, n );
    1613             : 
    1614           0 :         IF( GT_16( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) )
    1615             :         {
    1616           0 :             curr_frame_pow = L_shr( curr_frame_pow, sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) );
    1617           0 :             curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
    1618           0 :             move16();
    1619             :         }
    1620             :         ELSE
    1621             :         {
    1622           0 :             hBWE_TD->prev_wb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_wb_bwe_frame_pow_fx, sub( curr_frame_pow_exp, hBWE_TD->prev_frame_pow_exp ) );
    1623           0 :             move32();
    1624             :         }
    1625             : 
    1626           0 :         test();
    1627           0 :         IF( !st_fx->bfi && st_fx->prev_bfi )
    1628             :         {
    1629           0 :             IF( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_wb_bwe_frame_pow_fx ) )
    1630             :             {
    1631           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_wb_bwe_frame_pow_fx, 22, curr_frame_pow, 22, &exp );
    1632           0 :                 scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    1633             : 
    1634           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    1635           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    1636           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    1637           0 :                 temp = round_fx( L_shl( L_tmp, exp ) ); /* Q15 */
    1638             :             }
    1639             :             ELSE
    1640             :             {
    1641           0 :                 scale = temp = 32767;
    1642           0 :                 move16(); /* Q15 */
    1643           0 :                 move16();
    1644             :             }
    1645             : 
    1646           0 :             FOR( j = 0; j < 8; j++ )
    1647             :             {
    1648           0 :                 GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
    1649           0 :                 GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
    1650           0 :                 FOR( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
    1651             :                 {
    1652           0 :                     shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )] = mult_r( shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )], scale );
    1653             :                 }
    1654           0 :                 IF( temp > 0 )
    1655             :                 {
    1656           0 :                     IF( LT_16( scale, temp ) )
    1657             :                     {
    1658           0 :                         scale = div_s( scale, temp );
    1659             :                     }
    1660             :                     ELSE
    1661             :                     {
    1662           0 :                         scale = 32767;
    1663           0 :                         move16();
    1664             :                     }
    1665             :                 }
    1666             :                 ELSE
    1667             :                 {
    1668           0 :                     scale = 0;
    1669           0 :                     move16();
    1670             :                 }
    1671             :             }
    1672             :         }
    1673             : 
    1674           0 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = curr_frame_pow;
    1675           0 :         move32();
    1676           0 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    1677           0 :         move16();
    1678             : 
    1679             :         /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
    1680           0 :         max = 0;
    1681           0 :         move16();
    1682           0 :         FOR( cnt = 0; cnt < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; cnt++ )
    1683             :         {
    1684           0 :             if ( GT_16( abs_s( shaped_wb_excitation[cnt] ), max ) )
    1685             :             {
    1686           0 :                 max = abs_s( shaped_wb_excitation[cnt] );
    1687             :             }
    1688             :         }
    1689           0 :         Qx = norm_s( max );
    1690           0 :         if ( max == 0 )
    1691             :         {
    1692           0 :             Qx = 15;
    1693           0 :             move16();
    1694             :         }
    1695             : 
    1696           0 :         Qx = sub( Qx, 1 ); /* 1 bit space for saturation */
    1697             : 
    1698           0 :         max = 0;
    1699           0 :         move16();
    1700           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1701             :         {
    1702           0 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] ), max ) )
    1703             :             {
    1704           0 :                 max = abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] );
    1705             :             }
    1706             :         }
    1707             : 
    1708           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1709             :         {
    1710           0 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] ), max ) )
    1711             :             {
    1712           0 :                 max = abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] );
    1713             :             }
    1714             :         }
    1715             : 
    1716           0 :         IF( EQ_32( st_fx->output_Fs, 32000 ) )
    1717             :         {
    1718           0 :             FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1719             :             {
    1720           0 :                 max = s_max( max, abs_s( hBWE_TD->state_32and48k_WB_upsample_fx[i] ) );
    1721             :             }
    1722             :         }
    1723           0 :         IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1724             :         {
    1725           0 :             FOR( i = 0; i < INTERP_3_1_MEM_LEN; i++ )
    1726             :             {
    1727           0 :                 max = s_max( max, abs_s( hBWE_TD->mem_resamp_HB_fx[i] ) );
    1728             :             }
    1729             :         }
    1730           0 :         n_mem = 15;
    1731           0 :         move16();
    1732           0 :         if ( max != 0 )
    1733             :         {
    1734           0 :             n_mem = norm_s( max );
    1735             :         }
    1736           0 :         n_mem = s_max( n_mem, 0 );
    1737             : 
    1738           0 :         if ( GT_16( sub( Qx, hBWE_TD->prev_Qx ), n_mem ) )
    1739             :         {
    1740           0 :             Qx = add( hBWE_TD->prev_Qx, n_mem );
    1741             :         }
    1742             : 
    1743           0 :         FOR( i = 0; i < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; i++ )
    1744             :         {
    1745           0 :             shaped_wb_excitation[i] = shl( shaped_wb_excitation[i], Qx );
    1746           0 :             move16();
    1747             :         }
    1748             : 
    1749           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1750             :         {
    1751           0 :             hBWE_TD->state_lsyn_filt_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1752           0 :             move16();
    1753             :         }
    1754             : 
    1755           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1756             :         {
    1757           0 :             hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1758           0 :             move16();
    1759             :         }
    1760             : 
    1761           0 :         GenWBSynth_fx( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx );
    1762             : 
    1763           0 :         Copy( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
    1764             : 
    1765           0 :         FOR( i = 0; i < L_FRAME16k; i++ )
    1766             :         {
    1767           0 :             synth[i] = mult_r( error[i], 21299 );
    1768           0 :             move16();
    1769             :         }
    1770             : 
    1771           0 :         IF( st_fx->hBWE_FD != NULL )
    1772             :         {
    1773           0 :             max = 0;
    1774           0 :             move16();
    1775           0 :             FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1776             :             {
    1777           0 :                 max = s_max( max, abs_s( synth[cnt] ) );
    1778             :             }
    1779             : 
    1780           0 :             IF( max == 0 )
    1781             :             {
    1782           0 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = 0;
    1783           0 :                 move16();
    1784             :             }
    1785             :             ELSE
    1786             :             {
    1787           0 :                 n = norm_s( max );
    1788           0 :                 FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1789             :                 {
    1790           0 :                     synth_frac[cnt] = shl( synth[cnt], n ); /*Q14*/
    1791           0 :                     move16();
    1792             :                 }
    1793           0 :                 n = sub( sub( 14, n ), Qx );
    1794             : 
    1795           0 :                 Lacc = 0;
    1796           0 :                 move32();
    1797           0 :                 FOR( i = 0; i < L_FRAME16k; i++ )
    1798             :                 {
    1799           0 :                     L_tmp = L_mult( synth_frac[i], synth_frac[i] ); /* Q29 */
    1800           0 :                     Lacc = L_add( Lacc, L_shr( L_tmp, 7 ) );        /* Q22 */
    1801             :                 }
    1802             : 
    1803           0 :                 L_tmp = Mult_32_16( Lacc, 102 ); /* Q22 */
    1804           0 :                 exp = norm_l( L_tmp );
    1805           0 :                 tmp = round_fx_sat( L_shl( L_tmp, exp ) );
    1806           0 :                 exp = sub( add( exp, 22 ), 30 );
    1807           0 :                 tmp = div_s( 16384, tmp );
    1808           0 :                 L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                                                       /* Q(31-exp) */
    1809           0 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = round_fx_sat( L_shl_sat( L_tmp, add( exp, sub( n, 12 ) ) ) ); /*  Q3 */
    1810           0 :                 move16();
    1811             :             }
    1812             :         }
    1813             : 
    1814           0 :         IF( EQ_32( st_fx->output_Fs, 32000 ) ) /* 32kHz sampling rate, but only WB output - interpolate */
    1815             :         {
    1816           0 :             Scale_sig( hBWE_TD->state_32and48k_WB_upsample_fx, 2 * ALLPASSSECTIONS_STEEP, sub( Qx, hBWE_TD->prev_Qx ) );
    1817           0 :             Interpolate_allpass_steep_fx( synth, hBWE_TD->state_32and48k_WB_upsample_fx, L_FRAME16k, upsampled_synth );
    1818           0 :             Copy( upsampled_synth, synth, L_FRAME32k );
    1819             :         }
    1820           0 :         ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1821             :         {
    1822           0 :             Scale_sig( hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( Qx, hBWE_TD->prev_Qx ) );
    1823           0 :             interpolate_3_over_1_allpass_fx( synth, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
    1824           0 :             Copy( upsampled_synth, synth, L_FRAME48k );
    1825             :         }
    1826             :     }
    1827             :     ELSE
    1828             :     {
    1829           0 :         f = 5461;
    1830           0 :         move16(); /* Q15 */
    1831           0 :         inc = 5461;
    1832           0 :         move16(); /* Q15 */
    1833           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1834             :         {
    1835           0 :             lsf_wb[i] = f;
    1836           0 :             move16(); /*Q15*/
    1837           0 :             f = add_sat( f, inc );
    1838           0 :             move16();
    1839             :         }
    1840           0 :         GainFrame = 0; /* Q18 */
    1841           0 :         move32();
    1842           0 :         Qx = 0;
    1843           0 :         move16();
    1844           0 :         Q_bwe_exc = 31;
    1845           0 :         move16();
    1846           0 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
    1847           0 :         move32();
    1848           0 :         hBWE_TD->prev_frame_pow_exp = 0;
    1849           0 :         move16();
    1850             :     }
    1851             : 
    1852             :     /* Update previous frame parameters for FEC */
    1853           0 :     IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1854             :     {
    1855           0 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
    1856             :     }
    1857             :     ELSE
    1858             :     {
    1859           0 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_WB ); /*Q15*/
    1860             :     }
    1861           0 :     hBWE_TD->GainFrame_prevfrm_fx = GainFrame; /* Q18 */
    1862           0 :     move32();
    1863             : 
    1864           0 :     IF( !st_fx->bfi )
    1865             :     {
    1866           0 :         hBWE_TD->GainAttn_fx = 32767; /*Q15*/
    1867           0 :         move16();
    1868             :     }
    1869             : 
    1870           0 :     *Q_synth = Qx;
    1871           0 :     move16();
    1872             : 
    1873           0 :     st_fx->prev_Q_bwe_exc = Q_bwe_exc;
    1874           0 :     move16();
    1875           0 :     hBWE_TD->prev_Qx = Qx;
    1876           0 :     move16();
    1877             : 
    1878           0 :     return;
    1879             : }
    1880             : 
    1881             : 
    1882             : /*======================================================================================*/
    1883             : /* FUNCTION      : void swb_tbe_dec_fx ()                                               */
    1884             : /*--------------------------------------------------------------------------------------*/
    1885             : /* PURPOSE       : SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module */
    1886             : /*--------------------------------------------------------------------------------------*/
    1887             : /* INPUT ARGUMENTS  :                                                                   */
    1888             : /*  _(Word16) coder_type         : coding type                                          */
    1889             : /*  _(Word16*) bwe_exc_extended  :bandwidth extended exciatation  Q0                    */
    1890             : /*  _(Word16[]) voice_factors    :voicing factors          Q15                          */
    1891             : /*  _(Word16*) Q_white_exc       :Q Format of White Exc                                 */
    1892             : /*  _(Word16*) Q_synth           :Q Format of Synthesis                                 */
    1893             : /*--------------------------------------------------------------------------------------*/
    1894             : /* OUTPUT ARGUMENTS :                                                                   */
    1895             : /*   _(Word16*)synth            : SHB synthesis/final synthesis          Q_white_exc    */
    1896             : /*   _(Word16*)White_exc16k     : shaped white excitation for the FB TBE  Q_synth       */
    1897             : /*--------------------------------------------------------------------------------------*/
    1898             : /* INPUT/OUTPUT ARGUMENTS :                                                             */
    1899             : /*   _ Decoder_State *st_fx:      : Decoder state structure                          */
    1900             : /*--------------------------------------------------------------------------------------*/
    1901             : /* RETURN ARGUMENTS :                                                                   */
    1902             : /*           _ None                                                                     */
    1903             : /*--------------------------------------------------------------------------------------*/
    1904             : /* CALLED FROM : RX                                                                     */
    1905             : /*======================================================================================*/
    1906        1353 : void swb_tbe_dec_fx(
    1907             :     Decoder_State *st_fx,     /* i/o: decoder state structure                 */
    1908             :     const Word16 coder_type,  /* i  : coding type                             */
    1909             :     Word32 *bwe_exc_extended, /* i  : bandwidth extended excitation  2*Q_exc  */
    1910             :     Word16 Q_exc,
    1911             :     const Word16 voice_factors[],    /* i  : voicing factors                         */
    1912             :     const Word16 old_syn_12k8_16k[], /* i  : low band synthesis                      */
    1913             :     Word16 *White_exc16k,            /* o  : shaped white excitation for the FB TBE  */
    1914             :     Word16 *Q_white_exc,
    1915             :     Word16 *synth, /* o  : SHB synthesis/final synthesis           */
    1916             :     Word16 *Q_synth,
    1917             :     Word16 *pitch_buf /* i  : pitch buffer Q6                         */
    1918             : )
    1919             : {
    1920             :     Word16 i;
    1921             :     Word16 shaped_shb_excitation[L_FRAME16k + L_SHB_LAHEAD];
    1922             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    1923             :     Word16 lsf_shb[LPC_SHB_ORDER], lpc_shb[LPC_SHB_ORDER + 1], GainShape[NUM_SHB_SUBFR];
    1924             :     Word32 GainFrame;
    1925             :     Word16 error[L_FRAME32k];
    1926             :     Word32 L_ener;
    1927             :     Word16 ener;
    1928             :     Word16 is_fractive;
    1929             :     Word32 prev_pow, curr_pow, Lscale;
    1930             :     Word16 scale;
    1931             :     Word16 exp, tmp;
    1932             :     Word16 j, cnt;
    1933             :     Word16 n_mem, n_mem2, Qx, sc;
    1934             :     Word16 n_mem3;
    1935             :     Word32 Lmax, L_tmp;
    1936             :     Word16 frac;
    1937             : 
    1938             :     Word32 L_tmp1, L_tmp2;
    1939             :     Word16 expa, expb;
    1940             :     Word16 fraca, fracb;
    1941             :     Word16 GainShape_tmp[NUM_SHB_SUBGAINS];
    1942             :     Word16 Q_bwe_exc;
    1943             :     Word16 Q_shb;
    1944             :     Word16 vf_modified[NB_SUBFR16k];
    1945             :     Word16 stemp;
    1946             : 
    1947             :     Word16 tilt_swb_fec;
    1948             :     Word16 Q_bwe_exc_fb;
    1949             : 
    1950             :     Word16 lsp_shb_1[LPC_SHB_ORDER], lsp_shb_2[LPC_SHB_ORDER], lsp_temp[LPC_SHB_ORDER];
    1951             :     Word16 lpc_shb_sf[4 * ( LPC_SHB_ORDER + 1 )];
    1952             :     const Word16 *ptr_lsp_interp_coef;
    1953             :     Word32 shb_ener_sf_32;
    1954             :     Word16 shb_res_gshape[NB_SUBFR16k];
    1955             :     Word16 mixFactors;
    1956             :     Word16 vind;
    1957             :     Word16 shb_res_dummy[L_FRAME16k];
    1958             :     Word16 shaped_shb_excitationTemp[L_FRAME16k];
    1959             :     Word32 ener_tmp[NUM_SHB_SUBGAINS];
    1960             :     Word16 pitch_fx;
    1961             :     Word16 l_subframe_fx;
    1962             :     Word16 formant_fac;
    1963             :     Word16 lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
    1964             :     Word16 refl[M];
    1965             :     Word16 tilt_para;
    1966             :     Word16 tmp1, tmp2;
    1967             :     Word16 f_fx, inc_fx;
    1968             :     Word32 GainFrame_prevfrm_fx;
    1969             : 
    1970             :     Word16 synth_scale_fx;
    1971             :     Word16 mean_vf;
    1972             :     Word16 exp_ener, inv_ener;
    1973        1353 :     Word32 prev_ener_ratio_fx = 0; /* initialize just to avoid compiler warning */
    1974        1353 :     move32();
    1975             :     Word16 max, n, temp, shaped_shb_excitation_frac[L_FRAME16k + L_SHB_LAHEAD];
    1976             :     Word32 curr_frame_pow;
    1977             :     Word16 curr_frame_pow_exp;
    1978             :     Word32 L_prev_ener_shb;
    1979             :     TD_BWE_DEC_HANDLE hBWE_TD;
    1980        1353 :     hBWE_TD = st_fx->hBWE_TD;
    1981             : 
    1982             :     /* initializations */
    1983        1353 :     GainFrame = L_deposit_l( 0 );
    1984        1353 :     mixFactors = 0;
    1985        1353 :     move16();
    1986        1353 :     shb_ener_sf_32 = L_deposit_l( 0 );
    1987        1353 :     set16_fx( shaped_shb_excitationTemp, 0, L_FRAME16k );
    1988        1353 :     st_fx->hTdCngDec->shb_dtx_count_fx = 0;
    1989        1353 :     move16();
    1990        1353 :     is_fractive = 0;
    1991        1353 :     move16();
    1992        1353 :     set16_fx( shb_res_gshape, 1638 /*0.1f Q14*/, NB_SUBFR16k ); /* Q14 */
    1993        1353 :     Q_shb = 0;                                                  /* high band target Q factor set to zero */
    1994        1353 :     move16();
    1995        1353 :     L_tmp = calc_tilt_bwe_fx( old_syn_12k8_16k, st_fx->Q_syn2, st_fx->L_frame );
    1996        1353 :     tilt_swb_fec = round_fx_sat( L_shl_sat( L_tmp, 3 ) );
    1997             :     /* i: old_syn_12k8_16k in st_fx->Q_syn2 */
    1998             :     /* o: tilt_swb_fec in Q11 */
    1999        1353 :     test();
    2000        1353 :     if ( st_fx->bfi && st_fx->clas_dec != UNVOICED_CLAS )
    2001             :     {
    2002           0 :         tilt_swb_fec = hBWE_TD->tilt_swb_fec_fx;
    2003           0 :         move16();
    2004             :     }
    2005             : 
    2006             :     /* WB/SWB bandwidth switching */
    2007        1353 :     test();
    2008        1353 :     test();
    2009        1353 :     IF( ( GT_16( st_fx->tilt_wb_fx, 10240 ) && ( st_fx->clas_dec == UNVOICED_CLAS ) ) || GT_16( st_fx->tilt_wb_fx, 20480 ) )
    2010             :     {
    2011          61 :         test();
    2012          61 :         test();
    2013          61 :         test();
    2014          61 :         test();
    2015          61 :         test();
    2016          61 :         test();
    2017          61 :         test();
    2018          61 :         test();
    2019          61 :         IF( ( ( st_fx->prev_fractive == 0 ) &&
    2020             :               ( LT_32( st_fx->prev_enerLH_fx, L_shl( st_fx->enerLH_fx, 1 ) ) && GT_32( st_fx->prev_enerLH_fx, L_shr( st_fx->enerLH_fx, 1 ) ) && LT_32( st_fx->prev_enerLL_fx, L_shl( st_fx->enerLL_fx, 1 ) ) && GT_32( st_fx->prev_enerLL_fx, L_shr( st_fx->enerLL_fx, 1 ) ) ) ) ||
    2021             :             ( EQ_16( st_fx->prev_fractive, 1 ) &&
    2022             :               GT_32( L_shr( st_fx->prev_enerLH_fx, 2 ), Mult_32_16( st_fx->enerLH_fx, 24576 ) ) ) /* 24576 in Q13*/
    2023             :             || ( GT_32( L_shr( st_fx->enerLL_fx, 1 ), Mult_32_16( st_fx->enerLH_fx, 24576 ) ) &&  /*24576 = 1.5 in Q14*/
    2024             :                  LT_16( st_fx->tilt_wb_fx, 20480 ) )                                              /* 20480 = 10 in Q11*/
    2025             :         )
    2026             :         {
    2027          36 :             is_fractive = 0;
    2028          36 :             move16();
    2029             :         }
    2030             :         ELSE
    2031             :         {
    2032          25 :             is_fractive = 1;
    2033          25 :             move16();
    2034             :         }
    2035             :     }
    2036             : 
    2037             :     /* WB/SWB bandwidth switching */
    2038        1353 :     IF( st_fx->bws_cnt > 0 )
    2039             :     {
    2040           0 :         f_fx = 1489; /*Q15*/
    2041           0 :         move16();
    2042           0 :         inc_fx = 1489; /*Q15*/
    2043           0 :         move16();
    2044           0 :         IF( EQ_16( is_fractive, 1 ) )
    2045             :         {
    2046           0 :             Copy( lsf_tab_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    2047             :         }
    2048             :         ELSE
    2049             :         {
    2050           0 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2051             :             {
    2052           0 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
    2053           0 :                 move16();
    2054           0 :                 f_fx = add( f_fx, inc_fx );
    2055             :             }
    2056             :         }
    2057           0 :         test();
    2058           0 :         test();
    2059           0 :         test();
    2060           0 :         test();
    2061           0 :         test();
    2062           0 :         test();
    2063           0 :         test();
    2064           0 :         IF( ( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) &&
    2065             :               !( ( L_sub( L_shr( st_fx->prev_enerLH_fx, 1 ), st_fx->enerLH_fx ) < 0 ) && L_sub( st_fx->prev_enerLH_fx, ( L_shr( st_fx->enerLH_fx, 1 ) > 0 ) ) ) ) ||
    2066             :             ( sub( st_fx->last_core, ACELP_CORE ) != 0 ) || ( ( sub( st_fx->last_core, ACELP_CORE ) == 0 ) && ( L_sub( L_abs( L_sub( st_fx->last_core_brate, st_fx->core_brate ) ), 3600 ) > 0 ) ) || ( sub( s_xor( is_fractive, st_fx->prev_fractive ), 1 ) == 0 ) )
    2067             :         {
    2068           0 :             set16_fx( GainShape, 11587, NUM_SHB_SUBFR );
    2069             :         }
    2070             :         ELSE
    2071             :         {
    2072           0 :             if ( GT_16( hBWE_TD->prev_GainShape_fx, 11587 ) )
    2073             :             {
    2074           0 :                 hBWE_TD->prev_GainShape_fx = 11587;
    2075           0 :                 move16();
    2076             :             }
    2077           0 :             set16_fx( GainShape, hBWE_TD->prev_GainShape_fx, NUM_SHB_SUBFR );
    2078             :         }
    2079             : 
    2080           0 :         Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
    2081           0 :         set16_fx( shb_res_gshape, 3277 /*0.2f Q14*/, NB_SUBFR16k ); /* Q14 */
    2082             :     }
    2083             :     ELSE /* No bandwidth switching */
    2084             :     {
    2085        1353 :         test();
    2086        1353 :         IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) )
    2087             :         {
    2088          49 :             f_fx = 1489; /*Q15*/
    2089          49 :             move16();
    2090          49 :             inc_fx = 1489; /*Q15*/
    2091          49 :             move16();
    2092         539 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2093             :             {
    2094         490 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
    2095         490 :                 move16();
    2096         490 :                 f_fx = add( f_fx, inc_fx );
    2097             :             }
    2098             :         }
    2099             : 
    2100        1353 :         IF( !st_fx->bfi )
    2101             :         {
    2102        1353 :             IF( st_fx->use_partial_copy )
    2103             :             {
    2104           0 :                 IF( NE_16( st_fx->last_extl, SWB_TBE ) )
    2105             :                 {
    2106           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
    2107           0 :                     move16();
    2108           0 :                     f_fx = 1489 /*0.045454f Q15*/;
    2109           0 :                     move16();
    2110           0 :                     inc_fx = 1489 /*0.045454f Q15*/;
    2111           0 :                     move16();
    2112           0 :                     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2113             :                     {
    2114           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
    2115           0 :                         move16();
    2116           0 :                         f_fx = add( f_fx, inc_fx );
    2117             :                     }
    2118             :                 }
    2119           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
    2120           0 :                 set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR );
    2121             : 
    2122           0 :                 IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
    2123             :                 {
    2124             :                     /* Frame gain */
    2125           0 :                     GainFrame = L_mac( SHB_GAIN_QLOW_FX, st_fx->rf_indx_tbeGainFr, SHB_GAIN_QDELTA_FX );
    2126           0 :                     move32();                                       /*Q18*/
    2127           0 :                     L_tmp = Mult_32_16( GainFrame, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    2128             : 
    2129           0 :                     frac = L_Extract_lc( L_tmp, &exp );
    2130           0 :                     L_tmp = Pow2( 30, frac );
    2131           0 :                     GainFrame = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    2132           0 :                     test();
    2133           0 :                     test();
    2134           0 :                     test();
    2135           0 :                     test();
    2136           0 :                     test();
    2137           0 :                     test();
    2138           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && NE_16( st_fx->next_coder_type, GENERIC ) && EQ_16( st_fx->last_extl, SWB_TBE ) )
    2139             :                     {
    2140             :                         /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
    2141           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6553 ) );
    2142             :                     }
    2143             :                 }
    2144             :                 ELSE
    2145             :                 {
    2146           0 :                     temp = 0;
    2147           0 :                     move16();
    2148             :                     /* Frame gain */
    2149           0 :                     SWITCH( st_fx->rf_indx_tbeGainFr )
    2150             :                     {
    2151           0 :                         case 0:
    2152           0 :                             GainFrame = 131072; /* 0.5f in Q18 */
    2153           0 :                             move32();
    2154           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
    2155             :                             {
    2156           0 :                                 temp = 26214 /*0.8 Q15*/;
    2157           0 :                                 move16();
    2158             :                             }
    2159           0 :                             BREAK;
    2160           0 :                         case 1:
    2161           0 :                             GainFrame = 524288; /* 2.0f in Q18 */
    2162           0 :                             move32();
    2163           0 :                             test();
    2164           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
    2165             :                             {
    2166           0 :                                 temp = 26214 /*0.8 Q15*/;
    2167           0 :                                 move16();
    2168             :                             }
    2169           0 :                             BREAK;
    2170           0 :                         case 2:
    2171           0 :                             GainFrame = 1048576; /* 4.0f in Q18 */
    2172           0 :                             move32();
    2173           0 :                             test();
    2174           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
    2175             :                             {
    2176           0 :                                 temp = 26214 /*0.8 Q15*/;
    2177           0 :                                 move16();
    2178             :                             }
    2179           0 :                             BREAK;
    2180           0 :                         case 3:
    2181           0 :                             GainFrame = 2097152; /* 8.0f in Q18 */
    2182           0 :                             move32();
    2183           0 :                             test();
    2184           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16Q18*/ ) )
    2185             :                             {
    2186           0 :                                 temp = 26214 /*0.8 Q15*/;
    2187           0 :                                 move16();
    2188             :                             }
    2189           0 :                             BREAK;
    2190           0 :                         default:
    2191           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
    2192             :                     }
    2193           0 :                     IF( EQ_16( st_fx->last_extl, SWB_TBE ) )
    2194             :                     {
    2195           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
    2196             :                     }
    2197           0 :                     test();
    2198           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
    2199             :                     {
    2200           0 :                         test();
    2201           0 :                         test();
    2202           0 :                         test();
    2203           0 :                         test();
    2204           0 :                         if ( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && GT_32( GainFrame, 2097152 ) && LT_32( GainFrame, 3059606 ) )
    2205             :                         {
    2206           0 :                             GainFrame = Mult_32_16( GainFrame, 9830 );
    2207             :                         }
    2208             :                     }
    2209             :                 }
    2210             :             }
    2211             :             ELSE
    2212             :             {
    2213             : 
    2214             :                 /* de-quantization */
    2215        1353 :                 dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_shb, GainShape, &GainFrame, &stemp,
    2216             :                                             &shb_ener_sf_32, shb_res_gshape, &mixFactors );
    2217        1353 :                 Q_shb = 0;
    2218        1353 :                 move16();
    2219             :                 /* o: shb_ener_sf_32 in (2*Q_shb) */
    2220             :                 /* o: shb_res_gshape in Q14 */
    2221             :                 /* o: GainShape Q15 */
    2222             :                 /* o: GainFrame Q18 */
    2223             :             }
    2224             :         }
    2225             :         ELSE /* FER concealment of TBE parameters */
    2226             :         {
    2227           0 :             Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
    2228             : 
    2229             :             /* Gain shape concealment */
    2230           0 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    2231             :             {
    2232             :                 /* Gradient based GS estimation */
    2233           0 :                 gradientGainShape( st_fx, GainShape, &GainFrame );
    2234             :                 /* o: GainShape[16] in Q15 */
    2235             :                 /* o: GainFrame in Q18 */
    2236             :             }
    2237             :             ELSE
    2238             :             {
    2239           0 :                 FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    2240             :                 {
    2241           0 :                     FOR( j = 0; j < 4; j++ )
    2242             :                     {
    2243           0 :                         GainShape[add( i * 4, j )] = mult_r( st_fx->cummulative_damping, hBWE_TD->GainShape_Delay_fx[4 + i] );
    2244           0 :                         move16();
    2245             :                     }
    2246             :                 }
    2247           0 :                 IF( GT_16( tilt_swb_fec, ( 8 << 11 ) ) ) /* tilt_swb_fec in Q11 */
    2248             :                 {
    2249           0 :                     IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
    2250             :                     {
    2251           0 :                         GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 19661 /*0.6f Q15*/ );
    2252             :                     }
    2253           0 :                     ELSE IF( EQ_16( st_fx->nbLostCmpt, 2 ) )
    2254             :                     {
    2255           0 :                         GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 11469 /*0.35f Q15*/ );
    2256             :                     }
    2257             :                     ELSE
    2258             :                     {
    2259           0 :                         GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 6554 /*0.2f Q15*/ );
    2260             :                     }
    2261           0 :                     GainFrame = Mult_32_16( GainFrame, st_fx->cummulative_damping );
    2262             :                 }
    2263             :                 ELSE
    2264             :                 {
    2265           0 :                     GainFrame = hBWE_TD->GainFrame_prevfrm_fx;
    2266           0 :                     move16(); /* gain locking */
    2267             :                 }
    2268             :             }
    2269             : 
    2270             :             /* FER concealment for 24.4kbps and 32kbps */
    2271           0 :             test();
    2272           0 :             IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    2273             :             {
    2274           0 :                 IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    2275             :                 {
    2276             :                     /*scale = st->prev1_shb_ener_sf/root_a(st->prev2_shb_ener_sf * st->prev3_shb_ener_sf); */
    2277           0 :                     L_tmp = L_mult( extract_l( hBWE_TD->prev2_shb_ener_sf_fx ), extract_l( hBWE_TD->prev3_shb_ener_sf_fx ) ); /*Q1*/
    2278           0 :                     tmp = round_fx( root_a_fx( L_tmp, 1, &exp ) );                                                            /* Q = 15-exp */
    2279           0 :                     tmp1 = extract_l( hBWE_TD->prev1_shb_ener_sf_fx );                                                        /*Q0*/
    2280           0 :                     i = sub( norm_s( tmp1 ), 1 );
    2281           0 :                     tmp1 = shl( tmp1, i ); /* Qi */
    2282           0 :                     IF( tmp == 0 )
    2283             :                     {
    2284           0 :                         tmp = 32767 /*1.0f Q15*/;
    2285           0 :                         move16(); /*Q15*/
    2286             :                     }
    2287             :                     ELSE
    2288             :                     {
    2289           0 :                         scale = div_s( tmp1, tmp ); /* Q15 - Q(15-exp) + Qi  = Qexp+i */
    2290           0 :                         scale = s_max( scale, 0 );
    2291             :                         /*scale = st->prev_res_shb_gshape * min(scale, 1.0f); */
    2292           0 :                         tmp = shl_sat( scale, sub( sub( 15, exp ), i ) ); /*Q15*/
    2293             :                     }
    2294           0 :                     scale = mult_r( hBWE_TD->prev_res_shb_gshape_fx, tmp ); /* Q14 */
    2295             : 
    2296           0 :                     test();
    2297           0 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    2298             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    2299             :                     {
    2300             :                         /* shb_ener_sf_32 = 0.5f * scale * st_fx->prev1_shb_ener_sf_fx; */
    2301           0 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, scale );
    2302             : 
    2303           0 :                         if ( GT_16( st_fx->nbLostCmpt, 1 ) )
    2304             :                         {
    2305             :                             /* shb_ener_sf_32 *= 0.5f; */
    2306           0 :                             shb_ener_sf_32 = L_shr( shb_ener_sf_32, 1 );
    2307             :                         }
    2308             :                     }
    2309             :                     ELSE
    2310             :                     {
    2311             :                         /* shb_ener_sf = scale * scale * st_fx->prev1_shb_ener_sf_fx; */
    2312           0 :                         L_tmp = L_mult( scale, scale ); /* Q29 */
    2313           0 :                         shb_ener_sf_32 = L_shl( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, round_fx( L_tmp ) ), 2 );
    2314             :                     }
    2315             :                 }
    2316             :                 ELSE
    2317             :                 {
    2318           0 :                     test();
    2319           0 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    2320             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    2321             :                     {
    2322             :                         /* shb_ener_sf_32 = 0.5f * st->cummulative_damping * st_fx->prev1_shb_ener_sf_fx; */
    2323           0 :                         shb_ener_sf_32 = L_shr( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st_fx->cummulative_damping ), 1 );
    2324             :                     }
    2325             :                     ELSE
    2326             :                     {
    2327           0 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st_fx->cummulative_damping );
    2328             :                     }
    2329             :                 }
    2330             :             }
    2331             : 
    2332           0 :             shb_ener_sf_32 = L_max( shb_ener_sf_32, 1l /*1.0f Q0*/ );
    2333           0 :             mixFactors = hBWE_TD->prev_mixFactors_fx;
    2334           0 :             move16();
    2335             : 
    2336           0 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    2337             :             {
    2338           0 :                 set16_fx( shb_res_gshape, 3277 /*0.2f Q14*/, 5 ); /* Q14 */
    2339             :             }
    2340             :             ELSE
    2341             :             {
    2342           0 :                 set16_fx( shb_res_gshape, 16384 /*1.0f Q14*/, 5 ); /* Q14 */
    2343             :             }
    2344             :         }
    2345             :     }
    2346             : 
    2347             :     /* get the gainshape delay */
    2348        1353 :     Copy( &hBWE_TD->GainShape_Delay_fx[4], &hBWE_TD->GainShape_Delay_fx[0], NUM_SHB_SUBFR / 4 );
    2349        1353 :     test();
    2350        1353 :     IF( ( st_fx->rf_flag != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
    2351             :     {
    2352           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    2353             :         {
    2354           0 :             hBWE_TD->GainShape_Delay_fx[i + 4] = s_min( s_max( GainShape[i * 4], 3277 /*0.1f Q15*/ ), 16384 /*0.5f Q15*/ );
    2355           0 :             move16();
    2356             :         }
    2357             :     }
    2358             :     ELSE
    2359             :     {
    2360        6765 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    2361             :         {
    2362        5412 :             hBWE_TD->GainShape_Delay_fx[i + 4] = GainShape[i * 4];
    2363        5412 :             move16();
    2364             :         }
    2365             :     }
    2366             : 
    2367             :     /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
    2368             :     /* mean(voice_factors[i], 4); */
    2369        1353 :     L_tmp = L_mult( voice_factors[0], 8192 );
    2370        1353 :     L_tmp = L_mac( L_tmp, voice_factors[1], 8192 );
    2371        1353 :     L_tmp = L_mac( L_tmp, voice_factors[2], 8192 );
    2372        1353 :     mean_vf = mac_r( L_tmp, voice_factors[3], 8192 );
    2373             : 
    2374        1353 :     Copy( voice_factors, vf_modified, NB_SUBFR16k );
    2375             : 
    2376        1353 :     test();
    2377        1353 :     IF( EQ_16( coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
    2378             :     {
    2379        2076 :         FOR( i = 1; i < NB_SUBFR; i++ )
    2380             :         {
    2381        1557 :             L_tmp = L_mult( voice_factors[i], 26214 /*0.8f Q15*/ );
    2382        1557 :             vf_modified[i] = mac_r( L_tmp, voice_factors[i - 1], 6554 /*0.2f Q15*/ );
    2383        1557 :             move16();
    2384             :         }
    2385         519 :         IF( NE_16( st_fx->L_frame, L_FRAME ) )
    2386             :         {
    2387         274 :             L_tmp = L_mult( voice_factors[4], 26214 /*0.8f Q15*/ );
    2388         274 :             vf_modified[4] = mac_r( L_tmp, voice_factors[3], 6554 /*0.2f Q15*/ );
    2389         274 :             move16();
    2390             :         }
    2391             :     }
    2392             : 
    2393             :     /* convert quantized LSFs to LSPs for interpolation */
    2394        1353 :     E_LPC_lsf_lsp_conversion( lsf_shb, lsp_shb_2, LPC_SHB_ORDER );
    2395             : 
    2396        1353 :     test();
    2397        1353 :     IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
    2398             :     {
    2399             :         /* SHB LSP values from prev. frame for interpolation */
    2400        1304 :         Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1, LPC_SHB_ORDER );
    2401             :     }
    2402             :     ELSE
    2403             :     {
    2404             :         /* Use current frame's LSPs; in effect no interpolation */
    2405          49 :         Copy( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
    2406             :     }
    2407             : 
    2408        1353 :     test();
    2409        1353 :     test();
    2410        1353 :     test();
    2411        1353 :     IF( ( st_fx->bws_cnt == 0 ) && ( st_fx->bws_cnt1 == 0 ) && ( st_fx->prev_use_partial_copy == 0 ) && ( st_fx->use_partial_copy == 0 ) )
    2412             :     {
    2413        1353 :         lsf_diff[0] = 16384;
    2414        1353 :         move16(); /*Q15*/
    2415        1353 :         lsf_diff[LPC_SHB_ORDER - 1] = 16384;
    2416        1353 :         move16(); /*Q15*/
    2417       12177 :         FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2418             :         {
    2419       10824 :             lsf_diff[i] = sub( lsf_shb[i], lsf_shb[i - 1] );
    2420       10824 :             move16();
    2421             :         }
    2422             : 
    2423        1353 :         a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl, M );
    2424        1353 :         tmp = add( 16384, shr( refl[0], 1 ) ); /*Q14*/
    2425        1353 :         tmp1 = mult( 27425, tmp );
    2426        1353 :         tmp1 = mult( tmp1, tmp );                   /*Q10*/
    2427        1353 :         tmp2 = shr( mult( 31715, tmp ), 2 );        /*Q10*/
    2428        1353 :         tilt_para = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
    2429             : 
    2430        1353 :         test();
    2431        1353 :         IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) )
    2432             :         {
    2433         441 :             FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2434             :             {
    2435         392 :                 hBWE_TD->prev_lsf_diff_fx[i - 1] = shr( lsf_diff[i], 1 );
    2436         392 :                 move16();
    2437             :             }
    2438             :         }
    2439             : 
    2440        1353 :         IF( LE_32( st_fx->total_brate, ACELP_16k40 ) )
    2441             :         {
    2442         741 :             test();
    2443         741 :             test();
    2444         741 :             test();
    2445         741 :             test();
    2446         741 :             test();
    2447         741 :             IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( coder_type, TRANSITION ) || LT_16( tilt_para, 1024 ) ) ) &&
    2448             :                 !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( st_fx->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para, 5120 ) ) )
    2449             :             {
    2450        6489 :                 FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2451             :                 {
    2452        5768 :                     IF( LT_16( lsf_diff[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
    2453             :                     {
    2454        2491 :                         tmp = mult( 26214, lsf_diff[i] );
    2455             : 
    2456        2491 :                         test();
    2457        2491 :                         IF( ( hBWE_TD->prev_lsf_diff_fx[i - 1] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    2458             :                         {
    2459           0 :                             st_fx->BER_detect = 1;
    2460           0 :                             move16();
    2461           0 :                             tmp = 0;
    2462           0 :                             move16();
    2463             :                         }
    2464             :                         ELSE
    2465             :                         {
    2466        2491 :                             tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    2467             :                         }
    2468             : 
    2469        2491 :                         tmp = s_max( tmp, 16384 );
    2470        2491 :                         w[i] = s_min( tmp, 32767 );
    2471        2491 :                         move16();
    2472             :                     }
    2473             :                     ELSE
    2474             :                     {
    2475        3277 :                         tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    2476             : 
    2477        3277 :                         test();
    2478        3277 :                         IF( ( lsf_diff[i] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    2479             :                         {
    2480           0 :                             st_fx->BER_detect = 1;
    2481           0 :                             move16();
    2482           0 :                             tmp = 0;
    2483           0 :                             move16();
    2484             :                         }
    2485             :                         ELSE
    2486             :                         {
    2487        3277 :                             tmp = div_s( tmp, lsf_diff[i] );
    2488             :                         }
    2489             : 
    2490        3277 :                         tmp = s_max( tmp, 16384 );
    2491        3277 :                         w[i] = s_min( tmp, 32767 );
    2492        3277 :                         move16();
    2493             :                     }
    2494             :                 }
    2495         721 :                 w[0] = w[1];
    2496         721 :                 move16();
    2497         721 :                 w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
    2498         721 :                 move16();
    2499             : 
    2500        7931 :                 FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2501             :                 {
    2502        7210 :                     tmp1 = mult( lsp_shb_1[i], sub( 32767, w[i] ) );
    2503        7210 :                     tmp2 = mult( lsp_shb_2[i], w[i] );
    2504        7210 :                     lsp_temp[i] = add( tmp1, tmp2 );
    2505        7210 :                     move16();
    2506             :                 }
    2507             :             }
    2508             :             ELSE
    2509             :             {
    2510          20 :                 Copy( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
    2511             :             }
    2512             :         }
    2513             : 
    2514        1353 :         Copy( lsf_diff + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
    2515        1353 :         hBWE_TD->prev_tilt_para_fx = tilt_para;
    2516        1353 :         move16();
    2517             :     }
    2518             :     ELSE
    2519             :     {
    2520           0 :         Copy( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
    2521             :     }
    2522             : 
    2523        1353 :     test();
    2524        1353 :     IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    2525             :     {
    2526             :         /* ---------- SHB LSP interpolation ---------- */
    2527         612 :         ptr_lsp_interp_coef = interpol_frac_shb; /*Q15*/
    2528        3060 :         FOR( j = 0; j < 4; j++ )
    2529             :         {
    2530       26928 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2531             :             {
    2532             :                 /*lsp_temp_fx[i] =  lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx) */
    2533             :                 /*                + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1));*/
    2534       24480 :                 L_tmp = L_mult( lsp_shb_1[i], ( *ptr_lsp_interp_coef ) );
    2535       24480 :                 lsp_temp[i] = mac_r( L_tmp, lsp_shb_2[i], ( *( ptr_lsp_interp_coef + 1 ) ) );
    2536       24480 :                 move16();
    2537             :             }
    2538        2448 :             ptr_lsp_interp_coef += 2;
    2539             : 
    2540             :             /* convert from lsp to lsf */
    2541             :             /*old code: lsp2lsf_fx(lsp_temp, lsp_temp, LPC_SHB_ORDER, INT_FS_FX); */ /* input lsp_temp_fx in Q15*/
    2542             : 
    2543        2448 :             tmp = i_mult( j, LPC_SHB_ORDER + 1 );
    2544             :             /* convert LSPs to LP coefficients */
    2545        2448 :             E_LPC_f_lsp_a_conversion( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
    2546             :             /* Bring the LPCs to Q12 */
    2547        2448 :             Copy_Scale_sig( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf[tmp] ), 2 ) );
    2548             :         }
    2549             :     }
    2550             :     /*ELSE*/
    2551             :     {
    2552             :         /* for 13.2 and 16.4kbps */
    2553        1353 :         E_LPC_f_lsp_a_conversion( lsp_temp, lpc_shb, LPC_SHB_ORDER );
    2554        1353 :         Copy_Scale_sig( lpc_shb, lpc_shb, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb[0] ), 2 ) ); /* Q12 */
    2555             :     }
    2556             : 
    2557             :     /* Save the SWB LSP values from current frame for interpolation */
    2558        1353 :     Copy( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER );
    2559             :     /* lsp_shb_2_fx in Q15 */
    2560             : 
    2561             :     /* save the shb_ener Q18, prev_resgainshape Q14,  and mixFactor Q15 values */
    2562        1353 :     hBWE_TD->prev3_shb_ener_sf_fx = hBWE_TD->prev2_shb_ener_sf_fx;
    2563        1353 :     move32();
    2564        1353 :     hBWE_TD->prev2_shb_ener_sf_fx = hBWE_TD->prev1_shb_ener_sf_fx;
    2565        1353 :     move32();
    2566        1353 :     hBWE_TD->prev1_shb_ener_sf_fx = shb_ener_sf_32;
    2567        1353 :     move32();
    2568        1353 :     hBWE_TD->prev_res_shb_gshape_fx = shb_res_gshape[4];
    2569        1353 :     move16();
    2570        1353 :     hBWE_TD->prev_mixFactors_fx = mixFactors;
    2571        1353 :     move16();
    2572             : 
    2573             :     /* SWB CNG/DTX - update memories */
    2574        1353 :     if ( st_fx->hTdCngDec != NULL )
    2575             :     {
    2576        1353 :         Copy( st_fx->hTdCngDec->lsp_shb_prev_fx, st_fx->hTdCngDec->lsp_shb_prev_prev_fx, LPC_SHB_ORDER ); /* Q15 */
    2577        1353 :         Copy( lsf_shb, st_fx->hTdCngDec->lsp_shb_prev_fx, LPC_SHB_ORDER );                                /* Q15 */
    2578             :     }
    2579             : 
    2580             :     /* vind = (short)(mixFactors*8.0f); */
    2581        1353 :     vind = shl( mixFactors, 3 - 15 ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */
    2582             :     /* i: mixFactors in Q15 */
    2583             :     /* o: vind in Q0        */
    2584             : 
    2585             :     /* Determine formant PF strength */
    2586        1353 :     formant_fac = swb_formant_fac_fx( lpc_shb[1], &hBWE_TD->tilt_mem_fx );
    2587             :     /* o: formant_fac in Q15 */
    2588             : 
    2589             :     /* -------- start of  memory rescaling  -------- */
    2590             :     /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
    2591        1353 :     Lmax = 0;
    2592        1353 :     move32();
    2593      867273 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    2594             :     {
    2595      865920 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
    2596             :     }
    2597        1353 :     Q_bwe_exc = norm_l( Lmax );
    2598        1353 :     if ( Lmax == 0 )
    2599             :     {
    2600           0 :         Q_bwe_exc = 31;
    2601           0 :         move16();
    2602             :     }
    2603        1353 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
    2604             : 
    2605             :     /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
    2606        1353 :     find_max_mem_dec( st_fx, &n_mem, &n_mem2, &n_mem3 ); /* for >=24.4, use n_mem2 lpc_syn, shb_20sample, and mem_stp_swb_fx memory */
    2607             : 
    2608        1353 :     tmp = add( st_fx->prev_Q_bwe_exc, n_mem );
    2609        1353 :     if ( GT_16( Q_bwe_exc, tmp ) )
    2610             :     {
    2611          80 :         Q_bwe_exc = tmp;
    2612          80 :         move16();
    2613             :     }
    2614             : 
    2615             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    2616        1353 :     sc = sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc );
    2617        1353 :     IF( sc != 0 )
    2618             :     {
    2619         683 :         rescale_genSHB_mem_dec( st_fx, sc );
    2620             :     }
    2621             : 
    2622             :     /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm  */
    2623        1353 :     Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
    2624        1353 :     sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
    2625             : 
    2626      867273 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    2627             :     {
    2628      865920 :         bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
    2629             :     }
    2630        1353 :     Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
    2631             : 
    2632             :     /* state_syn_shbexc_fx is kept at (st_fx->prev_Q_bwe_syn) for 24.4/32kbps or is kept at Q_bwe_exc for 13.2/16.4kbps */
    2633        1353 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation, L_SHB_LAHEAD );
    2634             : 
    2635             :     /* save the previous Q factor (32-bit) of the buffer */
    2636        1353 :     st_fx->prev_Q_bwe_exc = Q_bwe_exc;
    2637        1353 :     move16();
    2638             : 
    2639        1353 :     Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
    2640             : 
    2641             :     /* -------- end of rescaling memories -------- */
    2642        1353 :     Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
    2643        1353 :     move16();
    2644             : 
    2645        1353 :     IF( GT_32( st_fx->total_brate, ACELP_32k ) )
    2646             :     {
    2647           0 :         FOR( j = 0; j < 4; j++ )
    2648             :         {
    2649           0 :             Copy( lpc_shb, &lpc_shb_sf[i_mult( j, LPC_SHB_ORDER + 1 )], LPC_SHB_ORDER + 1 );
    2650             :         }
    2651             :     }
    2652             : 
    2653             :     /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the low band ACELP core excitation */
    2654        1353 :     GenShapedSHBExcitation_fx( shaped_shb_excitation + L_SHB_LAHEAD, lpc_shb, White_exc16k,
    2655        1353 :                                hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    2656        1353 :                                coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified, st_fx->extl,
    2657             :                                &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf, shb_ener_sf_32,
    2658        1353 :                                shb_res_gshape, shb_res_dummy, &vind, formant_fac, hBWE_TD->fb_state_lpc_syn_fx,
    2659        1353 :                                &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st_fx->total_brate, st_fx->prev_bfi );
    2660             : 
    2661        1353 :     *Q_white_exc = Q_bwe_exc_fb;
    2662        1353 :     move16();
    2663        1353 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
    2664             :     {
    2665           0 :         hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
    2666           0 :         move16();
    2667             :     }
    2668             :     ELSE
    2669             :     {
    2670             :         /*Indirectly a memory reset of FB memories for next frame such that rescaling of memories would lead to 0 due to such high prev. Q value.
    2671             :         51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
    2672        1353 :         hBWE_TD->prev_Q_bwe_exc_fb = 51;
    2673        1353 :         move16();
    2674             :     }
    2675             :     /* rescale the TBE post proc memory */
    2676       14883 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2677             :     {
    2678       13530 :         hBWE_TD->mem_stp_swb_fx[i] = shl( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn ) );
    2679       13530 :         move16();
    2680             :     }
    2681             : 
    2682        6765 :     FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    2683             :     {
    2684             :         /* TD BWE post-processing */
    2685        5412 :         PostShortTerm_fx( &shaped_shb_excitation[L_SHB_LAHEAD + i], lpc_shb, &shaped_shb_excitationTemp[i], hBWE_TD->mem_stp_swb_fx,
    2686        5412 :                           hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac );
    2687             :     }
    2688        1353 :     Copy( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k ); /* Q_bwe_exc */
    2689             : 
    2690        1353 :     tmp = sub( shl( Q_bwe_exc, 1 ), 31 + 16 );
    2691        1353 :     prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    2692        1353 :     curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    2693       41943 :     FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
    2694             :     {
    2695       40590 :         prev_pow = L_mac0_sat( prev_pow, shaped_shb_excitation[i], shaped_shb_excitation[i] );                                         /*2*Q_bwe_exc*/
    2696       40590 :         curr_pow = L_mac0_sat( curr_pow, shaped_shb_excitation[i + L_SHB_LAHEAD + 10], shaped_shb_excitation[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
    2697             :     }
    2698             : 
    2699        1353 :     if ( GT_16( voice_factors[0], 24576 /*0.75f Q15*/ ) )
    2700             :     {
    2701          48 :         curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc) */
    2702             :     }
    2703             : 
    2704        1353 :     Lscale = root_a_over_b_fx( curr_pow, shl( Q_bwe_exc, 1 ), prev_pow, shl( Q_bwe_exc, 1 ), &exp );
    2705             : 
    2706       28413 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    2707             :     {
    2708       27060 :         L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] ); /* Q_bwe_exc + (31-exp) - 15 */
    2709             : #ifdef FIX_2602_NONBE_SAT_IN_SWB_TBE_SCALE
    2710       27060 :         shaped_shb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    2711             : #else
    2712             :         shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) );     /* Q_bwe_exc */
    2713             : #endif
    2714       27060 :         move16();
    2715             :     }
    2716        1353 :     IF( exp < 0 )
    2717             :     {
    2718         138 :         Lscale = L_shl( Lscale, exp );
    2719         138 :         exp = 0;
    2720         138 :         move16();
    2721             :     }
    2722             :     /*
    2723             :        code for EVS and IVAS are basically identical with the exception of i_mult_sat() which has precision issues
    2724             :        thus is was replaced for IVAS and kept for EVS, in order to keep EVS BE to test sequences and legacy implementations
    2725             :     */
    2726        1353 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
    2727             :     {
    2728       14883 :         FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    2729             :         {
    2730       13530 :             temp = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ );        /* Q15 */
    2731       13530 :             L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp ); /* Q31-exp */
    2732       13530 :             temp = sub( 32767 /*1.0f Q15*/, temp );
    2733       13530 :             Lscale = L_add( Mult_32_16( Lscale, temp ), L_tmp1 );
    2734       13530 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] ); /* Q_bwe_exc + (31-exp) - 15 */
    2735             : #ifdef FIX_2602_NONBE_SAT_IN_SWB_TBE_SCALE
    2736       13530 :             shaped_shb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    2737             : #else
    2738             :             shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
    2739             : #endif
    2740       13530 :             move16();
    2741             :         }
    2742             :     }
    2743             :     ELSE
    2744             :     {
    2745           0 :         FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    2746             :         {
    2747           0 :             temp = round_fx_sat( L_shl( L_mult( 0x6666 /* 0.1 in Q12 */, shl( sub( i, 19 ), 11 ) ), 1 ) );
    2748           0 :             L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp ); /* Q31-exp */
    2749           0 :             temp = sub( 32767 /*1.0f Q15*/, temp );
    2750           0 :             Lscale = L_add( Mult_32_16( Lscale, temp ), L_tmp1 );
    2751           0 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] ); /* Q_bwe_exc + (31-exp) - 15 */
    2752             : #ifdef FIX_2602_NONBE_SAT_IN_SWB_TBE_SCALE
    2753           0 :             shaped_shb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    2754             : #else
    2755             :             shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
    2756             : #endif
    2757           0 :             move16();
    2758             :         }
    2759             :     }
    2760             : 
    2761             :     /* Update SHB excitation */
    2762        1353 :     Copy( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
    2763             : 
    2764        1353 :     l_subframe_fx = L_FRAME16k / NUM_SHB_SUBGAINS;
    2765        1353 :     L_ener = 0;
    2766        1353 :     move32();
    2767        6765 :     FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    2768             :     {
    2769        5412 :         L_tmp = 0;
    2770        5412 :         move32();
    2771        5412 :         ener_tmp[i] = 0;
    2772        5412 :         move32();
    2773             :         {
    2774        5412 :             Word64 tmp64 = 0;
    2775        5412 :             move64();
    2776      438372 :             FOR( j = 0; j < l_subframe_fx; j++ )
    2777             :             {
    2778      432960 :                 tmp64 = W_mac0_16_16( tmp64, shaped_shb_excitation[add( i_mult( i, l_subframe_fx ), j )], shaped_shb_excitation[add( i_mult( i, l_subframe_fx ), j )] ); /* 2*Q_bwe_exc */
    2779             :             }
    2780        5412 :             L_tmp = W_sat_l( tmp64 );
    2781             :         }
    2782             : 
    2783        5412 :         L_tmp = Mult_32_16( L_tmp, 410 /*0.0125 Q15*/ ); /* 2*Q_bwe_exc: ener_tmp_fx in (2*Q_bwe_exc) */
    2784        5412 :         IF( L_tmp != 0 )
    2785             :         {
    2786        5407 :             exp = norm_l( L_tmp );
    2787        5407 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    2788        5407 :             exp = sub( exp, sub( 30, i_mult( 2, Q_bwe_exc ) ) );
    2789             : 
    2790        5407 :             tmp = div_s( 16384, tmp );
    2791        5407 :             L_tmp = L_deposit_h( tmp );
    2792        5407 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    2793        5407 :             ener_tmp[i] = L_shl_sat( L_tmp, sub( add( exp, shl( Q_bwe_exc, 1 ) ), 31 ) ); /*2 * Q_bwe_exc: Q31 -exp +exp +2 * Q_bwe_exc -31  */
    2794        5407 :             move32();
    2795        5407 :             L_ener = L_add_sat( L_ener, L_shr( ener_tmp[i], 2 ) ); /* 2*Q_bwe_exc */
    2796             :         }
    2797             :     }
    2798        1353 :     ener = s_max( 1, round_fx_sat( L_shl_sat( L_ener, sub( 19, shl( Q_bwe_exc, 1 ) ) ) ) ); /* Q3: 2*Q_bwe_exc+19-2*Q_bwe_exc-16 */
    2799             :     /* WB/SWB bandwidth switching */
    2800        1353 :     IF( st_fx->bws_cnt > 0 )
    2801             :     {
    2802           0 :         ener = mult( ener, 11587 );
    2803             :         /*bandwidth switching should be updated*/
    2804           0 :         if ( GT_16( st_fx->tilt_swb_fx, 16384 ) )
    2805             :         {
    2806           0 :             st_fx->prev_fractive = 1;
    2807           0 :             move16();
    2808             :         }
    2809             : 
    2810           0 :         IF( is_fractive == 0 )
    2811             :         {
    2812           0 :             IF( GT_16( st_fx->tilt_wb_fx, 2048 ) ) /*assuming st_fx->tilt_wb_fx in Q11*/
    2813             :             {
    2814           0 :                 st_fx->tilt_wb_fx = 2048;
    2815           0 :                 move16();
    2816             :             }
    2817           0 :             ELSE IF( LT_16( st_fx->tilt_wb_fx, 1024 ) )
    2818             :             {
    2819           0 :                 st_fx->tilt_wb_fx = 1024;
    2820           0 :                 move16();
    2821             :             }
    2822           0 :             test();
    2823           0 :             if ( EQ_16( st_fx->prev_fractive, 1 ) && GT_16( st_fx->tilt_wb_fx, 1024 ) )
    2824             :             {
    2825           0 :                 st_fx->tilt_wb_fx = 1024;
    2826           0 :                 move16();
    2827             :             }
    2828             :         }
    2829             :         ELSE
    2830             :         {
    2831           0 :             IF( GT_16( st_fx->tilt_wb_fx, 8192 ) )
    2832             :             {
    2833           0 :                 IF( st_fx->prev_fractive == 0 )
    2834             :                 {
    2835           0 :                     st_fx->tilt_wb_fx = 8192;
    2836           0 :                     move16();
    2837             :                 }
    2838             :                 ELSE
    2839             :                 {
    2840           0 :                     st_fx->tilt_wb_fx = 16384;
    2841           0 :                     move16();
    2842             :                 }
    2843             :             }
    2844             :             ELSE
    2845             :             {
    2846           0 :                 st_fx->tilt_wb_fx = shl( st_fx->tilt_wb_fx, 2 );
    2847           0 :                 move16();
    2848             :             }
    2849             :         }
    2850             : 
    2851           0 :         IF( ener != 0 )
    2852             :         {
    2853           0 :             L_tmp = L_shl( L_mult0( ener, st_fx->tilt_wb_fx ), sub( st_fx->Q_syn2, 14 ) ); /* 3+11 +st_fx->Q_syn2 -14 = st_fx->Q_syn2*/
    2854           0 :             exp_ener = norm_s( ener );
    2855           0 :             tmp = shl( ener, exp_ener );    /*Q(3+exp)*/
    2856           0 :             inv_ener = div_s( 16384, tmp ); /*Q(15+14-3-exp) = 26- exp*/
    2857             : 
    2858           0 :             test();
    2859           0 :             IF( GT_32( L_tmp, st_fx->enerLH_fx ) ) /*st_fx->Q_syn2*/
    2860             :             {
    2861           0 :                 st_fx->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st_fx->enerLH_fx, inv_ener ), sub( sub( st_fx->Q_syn2, exp_ener ), 16 ) ) ); /*Q11*/
    2862           0 :                 move16();
    2863             :                 /*st_fx->Q_syn2 -1 + 26- exp_ener -15 -(st_fx->Q_syn2 -exp_ener -16 ) -16    +1 -1 = (11) *0.5*/
    2864             :             }
    2865           0 :             ELSE IF( LT_32( L_tmp, Mult_32_16( st_fx->enerLH_fx, 1638 ) ) && EQ_16( is_fractive, 1 ) )
    2866             :             {
    2867           0 :                 st_fx->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st_fx->enerLH_fx, inv_ener ), sub( sub( st_fx->Q_syn2, exp_ener ), 15 ) ) ); /*Q11*/
    2868           0 :                 move16();
    2869             :                 /*st_fx->Q_syn2 -1 + 26- exp_ener -15 -(st_fx->Q_syn2 -exp_ener -15 ) -16 = (11) 0.25*/
    2870             :             }
    2871           0 :             L_tmp = L_mult0( st_fx->prev_ener_shb_fx, inv_ener );          /*Q(1+15+14-3-exp_ener) = 27 -exp_ener*/
    2872           0 :             GainFrame_prevfrm_fx = L_shr_sat( L_tmp, sub( 9, exp_ener ) ); /*27 -exp_ener -(9-exp_ener )= Q18*/
    2873             :         }
    2874             :         ELSE
    2875             :         {
    2876           0 :             GainFrame_prevfrm_fx = 0;
    2877           0 :             move32();
    2878             :         }
    2879             : 
    2880           0 :         IF( EQ_16( is_fractive, 1 ) )
    2881             :         {
    2882           0 :             GainFrame = L_shl( L_deposit_l( st_fx->tilt_wb_fx ), 10 );
    2883             :         }
    2884             :         ELSE
    2885             :         {
    2886           0 :             GainFrame = L_shl( L_deposit_l( st_fx->tilt_wb_fx ), 8 );
    2887             :         }
    2888             : 
    2889           0 :         test();
    2890           0 :         IF( EQ_16( s_and( is_fractive, st_fx->prev_fractive ), 1 ) && GT_32( GainFrame, GainFrame_prevfrm_fx ) )
    2891             :         {
    2892           0 :             GainFrame = L_add( Mult_32_16( GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) ); /* 18 +15 -15 = 18*/
    2893             :         }
    2894             :         ELSE
    2895             :         {
    2896           0 :             test();
    2897           0 :             test();
    2898           0 :             test();
    2899           0 :             test();
    2900           0 :             IF( ( LT_32( L_shr( st_fx->prev_enerLH_fx, 1 ), st_fx->enerLH_fx ) && GT_32( st_fx->prev_enerLH_fx, L_shr( st_fx->enerLH_fx, 1 ) ) ) && ( LT_32( L_shr( st_fx->prev_enerLL_fx, 1 ), st_fx->enerLL_fx ) && GT_32( st_fx->prev_enerLL_fx, L_shr( st_fx->enerLL_fx, 1 ) ) ) && ( s_xor( is_fractive, st_fx->prev_fractive ) == 0 ) )
    2901             :             {
    2902           0 :                 GainFrame = L_add( L_shr( GainFrame, 1 ), L_shr( GainFrame_prevfrm_fx, 1 ) );
    2903             :             }
    2904             :             ELSE
    2905             :             {
    2906           0 :                 test();
    2907           0 :                 IF( ( is_fractive == 0 ) && EQ_16( st_fx->prev_fractive, 1 ) )
    2908             :                 {
    2909           0 :                     L_tmp1 = L_shl( Mult_32_16( GainFrame, 3277 ), 13 );                                             /* 31 */
    2910           0 :                     L_tmp = L_sub( 2147483647, L_tmp1 );                                                             /* 31 */
    2911           0 :                     GainFrame = L_add( Mult_32_32( GainFrame, L_tmp ), Mult_32_32( GainFrame_prevfrm_fx, L_tmp1 ) ); /* 18 */
    2912             :                 }
    2913             :                 ELSE
    2914             :                 {
    2915           0 :                     GainFrame = L_add( L_shr( GainFrame, 1 ), L_shr( L_min( GainFrame_prevfrm_fx, GainFrame ), 1 ) ); /* 18 */
    2916             :                 }
    2917             :             }
    2918             :         }
    2919             : 
    2920           0 :         GainFrame = Mult_32_16( GainFrame, i_mult( sub( N_WS2N_FRAMES, st_fx->bws_cnt ), 819 ) ); /*Q18*/
    2921             :     }
    2922             :     ELSE
    2923             :     {
    2924        1353 :         if ( st_fx->bws_cnt1 > 0 )
    2925             :         {
    2926           0 :             GainFrame = Mult_32_16( GainFrame, i_mult( st_fx->bws_cnt1, 819 ) ); /*Q18*/
    2927             :         }
    2928        1353 :         IF( GE_16( st_fx->nbLostCmpt, 1 ) )
    2929             :         {
    2930           0 :             ener = s_max( 1, ener );
    2931           0 :             exp_ener = norm_s( ener );
    2932           0 :             tmp = shl( ener, exp_ener );                                                                        /*Q(3+exp)*/
    2933           0 :             inv_ener = div_s( 16384, tmp );                                                                     /*Q(15+14-3-exp)*/
    2934           0 :             prev_ener_ratio_fx = L_shr_sat( L_mult0( st_fx->prev_ener_shb_fx, inv_ener ), sub( 9, exp_ener ) ); /*Q: 1+26-exp-9+exp = 18 */
    2935             :         }
    2936             : 
    2937        1353 :         IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
    2938             :         {
    2939           0 :             test();
    2940           0 :             test();
    2941           0 :             test();
    2942           0 :             test();
    2943           0 :             test();
    2944           0 :             test();
    2945           0 :             test();
    2946           0 :             test();
    2947           0 :             test();
    2948           0 :             test();
    2949           0 :             IF( ( st_fx->clas_dec != UNVOICED_CLAS ) && NE_16( st_fx->clas_dec, UNVOICED_TRANSITION ) && LT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) &&
    2950             :                 ( ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) && LT_32( L_shr( st_fx->enerLL_fx, 1 ), st_fx->prev_enerLL_fx ) ) || ( GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) && LT_32( L_shr( st_fx->enerLH_fx, 1 ), st_fx->prev_enerLH_fx ) ) ) )
    2951             :             {
    2952           0 :                 IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) ) /*18*/
    2953             :                 {
    2954           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 13107 ), Mult_32_16( GainFrame, 19661 ) ); /*18*/
    2955             :                 }
    2956           0 :                 ELSE IF( GT_32( L_shr( prev_ener_ratio_fx, 1 ), GainFrame ) )
    2957             :                 {
    2958           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) );
    2959             :                 }
    2960             :                 ELSE
    2961             :                 {
    2962           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
    2963             :                 }
    2964             : 
    2965           0 :                 test();
    2966           0 :                 IF( GT_16( tilt_swb_fec, hBWE_TD->tilt_swb_fec_fx ) && ( hBWE_TD->tilt_swb_fec_fx > 0 ) )
    2967             :                 {
    2968           0 :                     exp = norm_s( hBWE_TD->tilt_swb_fec_fx );
    2969           0 :                     tmp = shl( hBWE_TD->tilt_swb_fec_fx, exp );                                   /*Q(11+exp)*/
    2970           0 :                     tmp = div_s( 16384, tmp );                                                    /*Q(15+14-11-exp)*/
    2971           0 :                     tmp = extract_h( L_shl( L_mult0( tmp, st_fx->tilt_wb_fx ), sub( exp, 1 ) ) ); /*18 -exp +11  + exp -1 -16  =12;          */
    2972           0 :                     GainFrame = L_shl( Mult_32_16( GainFrame, s_min( tmp, 20480 ) ), 3 );         /*Q18 = 18 +12 -15 +3 */
    2973             :                 }
    2974             :             }
    2975           0 :             ELSE IF( ( ( st_fx->clas_dec != UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) ) && GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) &&
    2976             :                      ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) )
    2977             :             {
    2978           0 :                 GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
    2979             :             }
    2980             :         }
    2981        1353 :         ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
    2982             :         {
    2983           0 :             test();
    2984           0 :             test();
    2985           0 :             test();
    2986           0 :             test();
    2987           0 :             test();
    2988           0 :             test();
    2989           0 :             test();
    2990           0 :             test();
    2991           0 :             IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) && ( ( EQ_16( st_fx->codec_mode, MODE1 ) && GT_32( st_fx->enerLL_fx, st_fx->prev_enerLL_fx ) && GT_32( st_fx->enerLH_fx, st_fx->prev_enerLH_fx ) ) || EQ_16( st_fx->codec_mode, MODE2 ) ) )
    2992             :             {
    2993           0 :                 test();
    2994           0 :                 IF( GT_16( tilt_swb_fec, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
    2995             :                 {
    2996           0 :                     GainFrame = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) ), L_shl( Mult_32_16( GainFrame, 16384 ), 3 ) ); /*Q18*/
    2997             :                 }
    2998             :                 ELSE
    2999             :                 {
    3000           0 :                     GainFrame = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame, 16384 ) ), L_shl( Mult_32_16( GainFrame, 16384 ), 3 ) ); /*Q18*/
    3001             :                 }
    3002             :             }
    3003           0 :             ELSE IF( GT_32( prev_ener_ratio_fx, GainFrame ) && ( ( EQ_16( st_fx->codec_mode, MODE1 ) && GT_32( st_fx->enerLL_fx, st_fx->prev_enerLL_fx ) && GT_32( st_fx->enerLH_fx, st_fx->prev_enerLH_fx ) ) || EQ_16( st_fx->codec_mode, MODE2 ) ) )
    3004             :             {
    3005           0 :                 test();
    3006           0 :                 IF( GT_16( tilt_swb_fec, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
    3007             :                 {
    3008           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame, 16384 ) );
    3009             :                 }
    3010             :                 ELSE
    3011             :                 {
    3012           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
    3013             :                 }
    3014             :             }
    3015             :         }
    3016             :     }
    3017        1353 :     st_fx->prev_fractive = is_fractive;
    3018        1353 :     move16();
    3019             : 
    3020             :     /* Adjust the subframe and frame gain of the synthesized shb signal */
    3021        1353 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    3022             :     {
    3023             :         /* pitch = 0.25f*sum_s(pitch_buf, 4); */
    3024         741 :         L_tmp = L_mult( pitch_buf[0], 8192 );
    3025        2964 :         FOR( i = 1; i < NB_SUBFR; i++ )
    3026             :         {
    3027        2223 :             L_tmp = L_mac( L_tmp, pitch_buf[i], 8192 ); /* pitch_buf in Q6 x 0.25 in Q15 */
    3028             :         }
    3029         741 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    3030             :     }
    3031             :     ELSE
    3032             :     {
    3033             :         /* pitch_fx = 0.2f*sum_s(pitch_buf, 5); */
    3034         612 :         L_tmp = L_mult( pitch_buf[0], 6554 );
    3035        3060 :         FOR( i = 1; i < NB_SUBFR16k; i++ )
    3036             :         {
    3037        2448 :             L_tmp = L_mac( L_tmp, pitch_buf[i], 6554 ); /* pitch_buf in Q6 x 0.2 in Q15 */
    3038             :         }
    3039         612 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    3040             :     }
    3041             : 
    3042        1353 :     test();
    3043        1353 :     test();
    3044        1353 :     test();
    3045        1353 :     test();
    3046        1353 :     test();
    3047        1353 :     test();
    3048        1353 :     test();
    3049        1353 :     test();
    3050        1353 :     test();
    3051        1353 :     test();
    3052        1353 :     IF( ( ( GE_32( st_fx->total_brate, ACELP_24k40 ) && EQ_16( st_fx->prev_coder_type, coder_type ) && NE_16( coder_type, UNVOICED ) ) || ( LE_32( st_fx->total_brate, ACELP_16k40 ) && ( EQ_16( st_fx->prev_coder_type, coder_type ) || ( EQ_16( st_fx->prev_coder_type, VOICED ) && EQ_16( coder_type, GENERIC ) ) || ( EQ_16( st_fx->prev_coder_type, GENERIC ) && EQ_16( coder_type, VOICED ) ) ) ) ) && GT_16( pitch_fx, 4480 /*70 in Q6*/ ) && LT_16( st_fx->extl, FB_TBE ) )
    3053             :     {
    3054        2640 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3055             :         {
    3056        2112 :             GainShape_tmp[i] = GainShape[i * 4]; /* Q15 */
    3057        2112 :             move16();
    3058             :         }
    3059             : 
    3060        2640 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3061             :         {
    3062             :             /* if( ener_tmp_fx[i]*GainShape_tmp_fx[i] > st_fx->prev_ener_fx*st_fx->prev_GainShape_fx ) */
    3063        2112 :             L_tmp1 = Mult_32_16( ener_tmp[i], GainShape_tmp[i] );                     /* (2*Q_bwe_exc) */
    3064        2112 :             L_tmp2 = Mult_32_16( hBWE_TD->prev_ener_fx, hBWE_TD->prev_GainShape_fx ); /* (2*st_fx->prev_ener_fx_Q) */
    3065        2112 :             tmp = sub( shl( Q_bwe_exc, 1 ), shl( hBWE_TD->prev_ener_fx_Q, 1 ) );
    3066        2112 :             L_tmp2 = L_shl_sat( L_tmp2, tmp ); /* new Q = (2*Q_bwe_exc) */
    3067        2112 :             IF( GT_32( L_tmp1, L_tmp2 ) )
    3068             :             {
    3069             :                 /*GainShape_tmp_fx[i] = 0.5f*(L_tmp2/ener_tmp_fx[i] + GainShape_tmp_fx[i]);*/
    3070             :                 /* tmp = L_tmp2/ener_tmp_fx[i]*/
    3071        1201 :                 L_tmp = L_tmp2;
    3072        1201 :                 move32();
    3073        1201 :                 if ( L_tmp2 < 0 )
    3074             :                 {
    3075           0 :                     L_tmp = L_negate( L_tmp2 );
    3076             :                 }
    3077             : 
    3078        1201 :                 expb = norm_l( L_tmp );
    3079        1201 :                 fracb = round_fx_sat( L_shl_sat( L_tmp, expb ) );
    3080        1201 :                 expb = sub( 30, expb ); /* - (2*Q_bwe_exc_ext);                    */
    3081             : 
    3082        1201 :                 expa = norm_l( ener_tmp[i] );
    3083        1201 :                 fraca = extract_h( L_shl( ener_tmp[i], expa ) );
    3084        1201 :                 expa = sub( 30, expa );
    3085             : 
    3086        1201 :                 scale = shr( sub( fraca, fracb ), 15 );
    3087        1201 :                 fracb = shl( fracb, scale );
    3088        1201 :                 expb = sub( expb, scale );
    3089             : 
    3090        1201 :                 tmp = div_s( fracb, fraca );
    3091        1201 :                 exp = sub( sub( expb, expa ), 1 );
    3092        1201 :                 tmp = shl( tmp, exp );
    3093        1201 :                 GainShape_tmp[i] = add( tmp, shr( GainShape_tmp[i], 1 ) ); /* Q15 */
    3094        1201 :                 move16();
    3095             :             }
    3096             : 
    3097        2112 :             hBWE_TD->prev_ener_fx = ener_tmp[i];
    3098        2112 :             move32();
    3099        2112 :             hBWE_TD->prev_GainShape_fx = GainShape_tmp[i];
    3100        2112 :             move16();
    3101        2112 :             hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    3102        2112 :             move16();
    3103             :         }
    3104        8976 :         FOR( i = 0; i < NUM_SHB_SUBFR; i++ )
    3105             :         {
    3106        8448 :             Word16 idx = 0;
    3107        8448 :             move16();
    3108        8448 :             IF( i != 0 )
    3109             :             {
    3110        7920 :                 idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
    3111             :             }
    3112        8448 :             GainShape[i] = GainShape_tmp[idx];
    3113        8448 :             move16();
    3114             :         }
    3115             :     }
    3116             :     ELSE
    3117             :     {
    3118         825 :         hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    3119         825 :         move16();
    3120             :     }
    3121             : 
    3122             : 
    3123             :     /* Back up the Q_bwe_exc associated with shaped_shb_excitation for the next frame*/
    3124        1353 :     hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
    3125        1353 :     move16();
    3126             : 
    3127             :     /* Scale the shaped excitation */
    3128        1353 :     ScaleShapedSHB_fx( SHB_OVERLAP_LEN,
    3129             :                        shaped_shb_excitation, /* i/o: Q_bwe_exc */
    3130        1353 :                        hBWE_TD->syn_overlap_fx,
    3131             :                        GainShape, /* Q15  */
    3132             :                        GainFrame, /* Q18  */
    3133             :                        window_shb_fx,
    3134             :                        subwin_shb_fx,
    3135        1353 :                        &Q_bwe_exc, &Qx, n_mem3, hBWE_TD->prev_Q_bwe_syn2 );
    3136             : 
    3137        1353 :     max = 0;
    3138        1353 :     move16();
    3139      434313 :     FOR( i = 0; i < L_FRAME16k; i++ )
    3140             :     {
    3141      432960 :         max = s_max( max, shaped_shb_excitation[i] ); /* Q0 */
    3142             :     }
    3143             : 
    3144        1353 :     IF( max == 0 )
    3145             :     {
    3146           0 :         curr_frame_pow = 0;
    3147           0 :         move16();
    3148           0 :         n = 0;
    3149           0 :         move16();
    3150             :     }
    3151             :     ELSE
    3152             :     {
    3153        1353 :         n = norm_s( max );
    3154        1353 :         max = 0;
    3155        1353 :         move16();
    3156      434313 :         FOR( i = 0; i < L_FRAME16k; i++ )
    3157             :         {
    3158      432960 :             shaped_shb_excitation_frac[i] = shl_sat( shaped_shb_excitation[i], n ); /*Q_bwe_exc+n*/
    3159      432960 :             move16();
    3160             :         }
    3161             : 
    3162        1353 :         curr_frame_pow = 0;
    3163        1353 :         move32();
    3164      434313 :         FOR( i = 0; i < L_FRAME16k; i++ )
    3165             :         {
    3166      432960 :             L_tmp = L_mult0( shaped_shb_excitation_frac[i], shaped_shb_excitation_frac[i] ); /*2*(Q_bwe_exc+n)*/
    3167      432960 :             curr_frame_pow = L_add( curr_frame_pow, L_shr( L_tmp, 9 ) );                     /*2*(Q_bwe_exc+n)-9*/
    3168             :         }
    3169             :     }
    3170        1353 :     curr_frame_pow_exp = sub( shl( add( Q_bwe_exc, n ), 1 ), 9 );
    3171             : 
    3172             : 
    3173        1353 :     tmp = sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp );
    3174        1353 :     IF( tmp > 0 ) /* shifting prev */
    3175             :     {
    3176           8 :         IF( GT_16( tmp, 32 ) )
    3177             :         {
    3178           0 :             hBWE_TD->prev_frame_pow_exp = add( curr_frame_pow_exp, 32 );
    3179           0 :             move16();
    3180           0 :             tmp = 32;
    3181           0 :             move16();
    3182             :         }
    3183           8 :         hBWE_TD->prev_swb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, tmp );
    3184           8 :         move32();
    3185           8 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    3186           8 :         move16();
    3187             :     }
    3188             :     ELSE /* shifting curr */
    3189             :     {
    3190        1345 :         IF( LT_16( tmp, -32 ) )
    3191             :         {
    3192           0 :             curr_frame_pow_exp = sub( hBWE_TD->prev_frame_pow_exp, 32 );
    3193           0 :             tmp = -32;
    3194           0 :             move16();
    3195             :         }
    3196        1345 :         curr_frame_pow = L_shr( curr_frame_pow, -tmp );
    3197        1345 :         curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
    3198        1345 :         move16();
    3199             :     }
    3200        1353 :     test();
    3201        1353 :     IF( !st_fx->bfi && st_fx->prev_bfi )
    3202             :     {
    3203           0 :         L_tmp = L_shr( curr_frame_pow, 4 );
    3204           0 :         L_tmp = Mult_32_16( L_tmp, 17476 );
    3205             : 
    3206           0 :         test();
    3207           0 :         test();
    3208           0 :         IF( ( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_swb_bwe_frame_pow_fx ) ) &&
    3209             :             ( GT_32( hBWE_TD->prev_swb_bwe_frame_pow_fx, L_tmp ) ) && EQ_16( st_fx->prev_coder_type, UNVOICED ) )
    3210             :         {
    3211           0 :             L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3212           0 :             scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    3213             : 
    3214           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3215           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3216           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3217           0 :             temp = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    3218             :         }
    3219             :         ELSE
    3220             :         {
    3221           0 :             scale = temp = 32767;
    3222           0 :             move16(); /*Q15*/
    3223             :         }
    3224           0 :         FOR( j = 0; j < 8; j++ )
    3225             :         {
    3226           0 :             GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
    3227           0 :             move16();
    3228           0 :             GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
    3229           0 :             move16();
    3230           0 :             FOR( i = 0; i < L_FRAME16k / 8; i++ )
    3231             :             {
    3232           0 :                 shaped_shb_excitation[add( i, j * ( L_FRAME16k / 8 ) )] = mult_r( shaped_shb_excitation[add( i, j * ( L_FRAME16k / 8 ) )], scale );
    3233           0 :                 move16();
    3234             :             }
    3235             : 
    3236           0 :             IF( temp > 0 )
    3237             :             {
    3238             :                 /* scale <= temp, due to scale = sqrt( st->prev_swb_bwe_frame_pow_fx/curr_frame_pow ), temp = sqrt( scale, 1.f/8.f )
    3239             :                    and curr_frame_pow > st->prev_swb_bwe_frame_pow_fx -> scale <= 1.0, sqrt(scale, 1.f/8.f) >= scale  */
    3240           0 :                 IF( LT_16( scale, temp ) )
    3241             :                 {
    3242           0 :                     scale = div_s( scale, temp );
    3243             :                 }
    3244             :                 ELSE
    3245             :                 {
    3246           0 :                     scale = 32767;
    3247           0 :                     move16();
    3248             :                 }
    3249             :             }
    3250             :             ELSE
    3251             :             {
    3252           0 :                 scale = 0;
    3253           0 :                 move16();
    3254             :             }
    3255             :         }
    3256             :     }
    3257             : 
    3258             :     /* adjust the FEC frame energy */
    3259        1353 :     IF( st_fx->bfi )
    3260             :     {
    3261           0 :         scale = temp = 4096;
    3262           0 :         move16(); /*Q12*/
    3263             : 
    3264           0 :         IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
    3265             :         {
    3266           0 :             test();
    3267           0 :             test();
    3268           0 :             test();
    3269           0 :             test();
    3270           0 :             test();
    3271           0 :             test();
    3272           0 :             test();
    3273           0 :             test();
    3274           0 :             IF( GT_32( curr_frame_pow, hBWE_TD->prev_swb_bwe_frame_pow_fx ) &&
    3275             :                 NE_16( st_fx->prev_coder_type, UNVOICED ) &&
    3276             :                 ( st_fx->last_good != UNVOICED_CLAS ) )
    3277             :             {
    3278           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp ); /*31 - exp*/
    3279           0 :                 scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );                                                                            /*Q12*/
    3280           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3281           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3282           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3283           0 :                 temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3284             :             }
    3285           0 :             ELSE IF( LT_32( curr_frame_pow, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) && EQ_16( st_fx->nbLostCmpt, 1 ) &&
    3286             :                      ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) &&
    3287             :                      ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    3288             :             {
    3289           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3290           0 :                 scale = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3291           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3292           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3293           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3294           0 :                 temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3295             :             }
    3296             :         }
    3297           0 :         ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
    3298             :         {
    3299           0 :             test();
    3300           0 :             test();
    3301           0 :             test();
    3302           0 :             test();
    3303           0 :             test();
    3304           0 :             IF( GT_32( curr_frame_pow, hBWE_TD->prev_swb_bwe_frame_pow_fx ) )
    3305             :             {
    3306           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3307           0 :                 scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3308           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3309           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3310           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3311           0 :                 temp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3312             :             }
    3313           0 :             ELSE IF( LT_32( curr_frame_pow, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) &&
    3314             :                      ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) &&
    3315             :                      ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    3316             :             {
    3317           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3318           0 :                 L_tmp = L_min( L_tmp, L_shl_sat( 2, sub( 31, exp ) ) ); /*31 - exp*/
    3319           0 :                 scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );      /*Q12*/
    3320           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3321           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3322           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3323           0 :                 temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3324             :             }
    3325             :         }
    3326           0 :         FOR( j = 0; j < 8; j++ )
    3327             :         {
    3328           0 :             GainShape[2 * j] = shl_sat( mult_r( GainShape[2 * j], scale ), 3 );
    3329           0 :             move16(); /* 15 +12 +3-15 =15*/
    3330           0 :             GainShape[add( 2 * j, 1 )] = shl_sat( mult_r( GainShape[add( 2 * j, 1 )], scale ), 3 );
    3331           0 :             move16();
    3332           0 :             FOR( i = 0; i < 40; i++ )
    3333             :             {
    3334           0 :                 shaped_shb_excitation[add( i, j * 40 )] = shl_sat( mult_r( shaped_shb_excitation[add( i, j * 40 )], scale ), 3 );
    3335           0 :                 move16(); /* Q_bwe_exc +12+3 -15  =Q_bwe_exc*/
    3336             :             }
    3337             : 
    3338           0 :             IF( temp > 0 )
    3339             :             {
    3340           0 :                 IF( LT_16( scale, temp ) )
    3341             :                 {
    3342           0 :                     scale = shr( div_s( scale, temp ), 3 );
    3343             :                 }
    3344             :                 ELSE
    3345             :                 {
    3346           0 :                     tmp1 = sub( norm_s( scale ), 1 );
    3347           0 :                     tmp2 = norm_s( temp );
    3348           0 :                     scale = div_s( shl( scale, tmp1 ), shl( temp, tmp2 ) );
    3349           0 :                     scale = shr( scale, add( sub( tmp1, tmp2 ), 3 ) );
    3350             :                 }
    3351             :             }
    3352             :             ELSE
    3353             :             {
    3354           0 :                 scale = 0;
    3355           0 :                 move16();
    3356             :             }
    3357             :         }
    3358             :     }
    3359             : 
    3360        1353 :     hBWE_TD->prev_swb_bwe_frame_pow_fx = curr_frame_pow;
    3361        1353 :     move32();
    3362        1353 :     hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    3363        1353 :     move16();
    3364             : 
    3365             :     {
    3366        1353 :         Word64 prev_ener_shb64 = 0;
    3367        1353 :         move64();
    3368      434313 :         FOR( i = 0; i < L_FRAME16k; i++ )
    3369             :         {
    3370      432960 :             prev_ener_shb64 = W_mac0_16_16( prev_ener_shb64, shaped_shb_excitation[i], shaped_shb_excitation[i] ); /* Q0 */
    3371             :         }
    3372        1353 :         L_prev_ener_shb = W_sat_l( prev_ener_shb64 );
    3373             :     }
    3374             : 
    3375             :     /* st->prev_ener_shb = sqrt(st->prev_ener_shb/L_FRAME16k) */
    3376        1353 :     L_prev_ener_shb = Mult_32_16( L_prev_ener_shb, 26214 ); /* 2*Q_bwe_exc_mod+8; 26214=(1/L_FRAME16k) in Q23 */
    3377        1353 :     st_fx->prev_ener_shb_fx = 0;
    3378        1353 :     move16();
    3379             : 
    3380        1353 :     IF( L_prev_ener_shb != 0 )
    3381             :     {
    3382        1353 :         exp = norm_l( L_prev_ener_shb );
    3383        1353 :         tmp = extract_h( L_shl( L_prev_ener_shb, exp ) );
    3384        1353 :         exp = sub( exp, sub( 30, ( add( i_mult( 2, Q_bwe_exc ), 8 ) ) ) );
    3385             : 
    3386        1353 :         tmp = div_s( 16384, tmp );
    3387        1353 :         L_tmp = L_deposit_h( tmp );
    3388        1353 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    3389        1353 :         st_fx->prev_ener_shb_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    3390        1353 :         move16();
    3391             :     }
    3392             :     /* st->prev_SWB_fenv[i] = sqrt(curr_frame_pow/L_FRAME16k); */
    3393        1353 :     L_tmp = Mult_32_16( curr_frame_pow, 26214 ); /* curr_frame_pow_exp+8; 26214=(1/L_FRAME16k) in Q23 */
    3394        1353 :     tmp = 0;
    3395        1353 :     move16();
    3396        1353 :     IF( L_tmp != 0 )
    3397             :     {
    3398        1318 :         exp = norm_l( L_tmp );
    3399        1318 :         tmp = extract_h( L_shl( L_tmp, exp ) );
    3400        1318 :         exp = sub( exp, sub( 30, add( curr_frame_pow_exp, 8 ) ) );
    3401             : 
    3402        1318 :         tmp = div_s( 16384, tmp );
    3403        1318 :         L_tmp = L_deposit_h( tmp );
    3404        1318 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    3405        1318 :         tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    3406             :     }
    3407        1353 :     set16_fx( st_fx->hBWE_FD->prev_SWB_fenv_fx, tmp, SWB_FENV ); /* Q1 */
    3408             : 
    3409             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    3410        1353 :     sc = sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn2 );
    3411        1353 :     IF( sc != 0 )
    3412             :     {
    3413        5999 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    3414             :         {
    3415        5142 :             hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i] = shl( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i], sc );
    3416        5142 :             move16();
    3417             :         }
    3418             : 
    3419         857 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    3420             :         {
    3421        9878 :             FOR( i = 0; i < HILBERT_MEM_SIZE; i++ )
    3422             :             {
    3423        9429 :                 hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i] = L_shl( hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i], sc );
    3424        9429 :                 move32();
    3425             :             }
    3426             :         }
    3427         857 :         IF( EQ_32( st_fx->output_Fs, 48000 ) )
    3428             :         {
    3429         408 :             Scale_sig( hBWE_TD->int_3_over_2_tbemem_dec_fx, INTERP_3_2_MEM_LEN, sc );
    3430             :         }
    3431         857 :         IF( EQ_32( st_fx->output_Fs, 16000 ) )
    3432             :         {
    3433           0 :             Scale_sig( hBWE_TD->mem_resamp_HB_32k_fx, 2 * ALLPASSSECTIONS_STEEP + 1, sc );
    3434             :         }
    3435             :     }
    3436             :     /* i: shaped_shb_excitation[320] in (Q_bwe_exc)            */
    3437             :     /* i/o: st_fx->genSHBsynth_Hilbert_Mem_fx in (Q_bwe_exc)   */
    3438             :     /* i/o: st_fx->genSHBsynth_state_lsyn_filt_shb_local_fx in (Q_bwe_exc)   */
    3439             :     /* o: error in (Qx)                                 */
    3440        1353 :     GenSHBSynth_fx( shaped_shb_excitation, error, hBWE_TD->genSHBsynth_Hilbert_Mem_fx,
    3441        1353 :                     hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, st_fx->L_frame, &( hBWE_TD->syn_dm_phase ) );
    3442             : 
    3443        1353 :     Copy( error + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
    3444             : 
    3445             :     /* resample SHB synthesis (if needed) and scale down */
    3446        1353 :     synth_scale_fx = 32767;
    3447        1353 :     move16(); /* 1.0 in Q15 */
    3448        1353 :     if ( EQ_16( st_fx->codec_mode, MODE1 ) )
    3449             :     {
    3450         741 :         synth_scale_fx = 29491;
    3451         741 :         move16(); /* 0.9 in Q15 */
    3452             :     }
    3453             : 
    3454        1353 :     IF( EQ_32( st_fx->output_Fs, 48000 ) )
    3455             :     {
    3456         612 :         IF( EQ_32( st_fx->extl, FB_TBE ) )
    3457             :         {
    3458           0 :             tmp = norm_l( GainFrame );
    3459           0 :             if ( GainFrame == 0 )
    3460             :             {
    3461           0 :                 tmp = 31;
    3462           0 :                 move16();
    3463             :             }
    3464           0 :             L_tmp = L_shl( GainFrame, tmp ); /* 18 + tmp */
    3465             : 
    3466           0 :             tmp1 = 0;
    3467           0 :             move16();
    3468           0 :             FOR( i = 0; i < L_FRAME16k; i++ )
    3469             :             {
    3470           0 :                 Word16 idx = 0;
    3471           0 :                 IF( i != 0 )
    3472             :                 {
    3473           0 :                     idx = idiv1616( i_mult( NUM_SHB_SUBFR, i ), L_FRAME16k );
    3474             :                 }
    3475           0 :                 L_tmp1 = Mult_32_16( L_tmp, GainShape[idx] );                        /* Q : 18 + tmp +15 -15*/
    3476           0 :                 White_exc16k[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
    3477           0 :                 move16();
    3478           0 :                 tmp1 = s_max( tmp1, abs_s( White_exc16k[i] ) );
    3479             :             }
    3480             : 
    3481           0 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
    3482           0 :             move16();
    3483           0 :             tmp = norm_s( tmp1 );
    3484           0 :             if ( tmp1 == 0 )
    3485             :             {
    3486           0 :                 tmp = 15;
    3487           0 :                 move16();
    3488             :             }
    3489             : 
    3490           0 :             FOR( i = 0; i < L_FRAME16k; i++ )
    3491             :             {
    3492           0 :                 White_exc16k[i] = shl( White_exc16k[i], sub( tmp, 1 ) );
    3493           0 :                 move16();
    3494             :             }
    3495           0 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
    3496           0 :             move16();
    3497             :         }
    3498             : 
    3499         612 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    3500             :         {
    3501           0 :             FOR( i = 0; i < L_FRAME32k; i++ )
    3502             :             {
    3503           0 :                 error[i] = mult_r( error[i], synth_scale_fx );
    3504           0 :                 move16();
    3505             :             }
    3506             :         }
    3507             : 
    3508         612 :         interpolate_3_over_2_allpass_fx( error, L_FRAME32k, synth, hBWE_TD->int_3_over_2_tbemem_dec_fx, allpass_poles_3_ov_2 );
    3509             :     }
    3510         741 :     ELSE IF( EQ_32( st_fx->output_Fs, 32000 ) )
    3511             :     {
    3512         741 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    3513             :         {
    3514      474981 :             FOR( i = 0; i < L_FRAME32k; i++ )
    3515             :             {
    3516      474240 :                 synth[i] = mult_r( synth_scale_fx, error[i] );
    3517      474240 :                 move16(); /*Qx*/
    3518             :             }
    3519             :         }
    3520             :         ELSE
    3521             :         {
    3522           0 :             Copy( error, synth, L_FRAME32k );
    3523             :         }
    3524             :     }
    3525           0 :     ELSE IF( EQ_32( st_fx->output_Fs, 16000 ) )
    3526             :     {
    3527           0 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    3528             :         {
    3529           0 :             FOR( i = 0; i < L_FRAME32k; i++ )
    3530             :             {
    3531           0 :                 error[i] = mult_r( error[i], synth_scale_fx );
    3532           0 :                 move16();
    3533             :             }
    3534             :         }
    3535             : 
    3536           0 :         Decimate_allpass_steep_fx( error, hBWE_TD->mem_resamp_HB_32k_fx, L_FRAME32k, synth );
    3537             :     }
    3538             : 
    3539             :     /* Update previous frame parameters for FEC */
    3540        1353 :     Copy( lsf_shb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    3541        1353 :     IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    3542             :     {
    3543         741 :         hBWE_TD->GainFrame_prevfrm_fx = GainFrame;
    3544         741 :         move16(); /*Q18*/
    3545         741 :         hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec;
    3546         741 :         move16();
    3547             : 
    3548         741 :         if ( !st_fx->bfi )
    3549             :         {
    3550         741 :             hBWE_TD->GainAttn_fx = 32767; /*Q15*/
    3551         741 :             move16();
    3552             :         }
    3553             :     }
    3554             :     ELSE
    3555             :     {
    3556         612 :         IF( !st_fx->bfi )
    3557             :         {
    3558         612 :             hBWE_TD->GainFrame_prevfrm_fx = GainFrame;
    3559         612 :             move16(); /*Q18*/
    3560         612 :             hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec;
    3561         612 :             move16();
    3562         612 :             hBWE_TD->GainAttn_fx = 32767; /*Q15*/
    3563         612 :             move16();
    3564             :         }
    3565             :     }
    3566             : 
    3567        1353 :     hBWE_TD->prev_ener_fx = ener_tmp[NUM_SHB_SUBGAINS - 1];
    3568        1353 :     move32();
    3569        1353 :     hBWE_TD->prev_GainShape_fx = GainShape[NUM_SHB_SUBFR - 1];
    3570        1353 :     move32();
    3571        1353 :     *Q_synth = Q_bwe_exc;
    3572        1353 :     move16();
    3573        1353 :     hBWE_TD->prev_Q_bwe_syn2 = Q_bwe_exc;
    3574        1353 :     move16();
    3575        1353 :     hBWE_TD->prev_Qx = Q_bwe_exc;
    3576        1353 :     move16();
    3577             : 
    3578        1353 :     return;
    3579             : }
    3580             : 
    3581           0 : static void gradientGainShape(
    3582             :     Decoder_State *st_fx,
    3583             :     Word16 *GainShape,
    3584             :     Word32 *GainFrame )
    3585             : {
    3586             :     Word16 i, j, tmp;
    3587             :     Word16 GainShapeTemp[NUM_SHB_SUBFR / 4];
    3588             :     Word16 GainGrad0[3];
    3589             :     Word16 GainGrad1[3];
    3590             :     Word16 GainGradFEC[4];
    3591             :     TD_BWE_DEC_HANDLE hBWE_TD;
    3592           0 :     hBWE_TD = st_fx->hBWE_TD;
    3593             : 
    3594             :     /* the previous frame gainshape gradient and the gainshape gradient pattern for the current frame */
    3595           0 :     FOR( j = 0; j < 3; j++ )
    3596             :     {
    3597           0 :         GainGrad0[j] = sub( shr( hBWE_TD->GainShape_Delay_fx[j + 1], 1 ), shr( hBWE_TD->GainShape_Delay_fx[j], 1 ) );
    3598           0 :         move16(); /* Q14 */
    3599           0 :         GainGrad1[j] = sub( shr( hBWE_TD->GainShape_Delay_fx[j + 5], 1 ), shr( hBWE_TD->GainShape_Delay_fx[j + 4], 1 ) );
    3600           0 :         move16(); /* Q14 */
    3601           0 :         GainGradFEC[j + 1] = add( mult_r( GainGrad0[j], 13107 ), mult_r( GainGrad1[j], 19660 ) );
    3602           0 :         move16(); /* Q14 */
    3603             :     }
    3604             : 
    3605             :     /* gradient for the first gainshape */
    3606           0 :     test();
    3607           0 :     test();
    3608           0 :     test();
    3609           0 :     IF( ( ( GT_16( shr( GainGrad1[2], 1 ), GainGrad1[1] ) ) && ( GT_16( shr( GainGrad1[1], 1 ), GainGrad1[0] ) ) ) ||
    3610             :         ( ( LT_16( shr( GainGrad1[2], 1 ), GainGrad1[1] ) ) && ( LT_16( shr( GainGrad1[1], 1 ), GainGrad1[0] ) ) ) )
    3611             :     {
    3612           0 :         GainGradFEC[0] = add( mult_r( GainGrad1[1], 3277 ), mult_r( GainGrad1[2], 29490 ) );
    3613           0 :         move16(); /* Q14 */
    3614             :     }
    3615             :     ELSE
    3616             :     {
    3617           0 :         GainGradFEC[0] = add( mult_r( GainGrad1[0], 6553 ), mult_r( GainGrad1[1], 9830 ) );
    3618           0 :         move16();
    3619           0 :         GainGradFEC[0] = add( GainGradFEC[0], mult_r( GainGrad1[2], 16384 ) );
    3620           0 :         move16(); /* Q14 */
    3621             :     }
    3622             : 
    3623             :     /* get the first gainshape template */
    3624           0 :     test();
    3625           0 :     test();
    3626           0 :     IF( ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) ) && ( GainGradFEC[0] > 0 ) )
    3627             :     {
    3628           0 :         GainShapeTemp[0] = add( shr( hBWE_TD->GainShape_Delay_fx[7], 1 ), GainGradFEC[0] );
    3629           0 :         move16();
    3630             :     }
    3631           0 :     ELSE IF( GainGradFEC[0] > 0 )
    3632             :     {
    3633           0 :         GainShapeTemp[0] = add( shr( hBWE_TD->GainShape_Delay_fx[7], 1 ), mult_r( GainGradFEC[0], 16384 ) );
    3634           0 :         move16(); /* Q14 */
    3635             :     }
    3636             :     ELSE
    3637             :     {
    3638           0 :         GainShapeTemp[0] = shr( hBWE_TD->GainShape_Delay_fx[7], 1 );
    3639           0 :         move16(); /* Q14 */
    3640             :     }
    3641             : 
    3642             :     /*Get the second the third and the fourth gainshape template*/
    3643             : 
    3644           0 :     tmp = shr( GainGrad1[2], 3 ); /* GainGrad1[2]/8 */
    3645           0 :     tmp = mult_r( tmp, 26214 );   /* 0.8 in Q15 tmp*(8/10) */
    3646             : 
    3647           0 :     test();
    3648           0 :     IF( ( GT_16( tmp, GainGrad1[1] ) ) && ( GainGrad1[1] > 0 ) )
    3649             :     {
    3650           0 :         FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    3651             :         {
    3652           0 :             GainShapeTemp[i] = add( GainShapeTemp[i - 1], mult_r( GainGradFEC[i], 26214 ) );
    3653           0 :             move16(); /* GainShapeTemp[i-1] + 0.8* GainShapeTemp[i] */
    3654           0 :             GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
    3655           0 :             move16();
    3656             :         }
    3657             :     }
    3658             :     ELSE
    3659             :     {
    3660           0 :         test();
    3661           0 :         IF( ( GT_16( tmp, GainGrad1[1] ) ) && ( GainGrad1[1] < 0 ) )
    3662             :         {
    3663           0 :             FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    3664             :             {
    3665           0 :                 GainShapeTemp[i] = add( GainShapeTemp[i - 1], mult_r( GainGradFEC[i], 6553 ) );
    3666           0 :                 move16(); /* GainShapeTemp[i-1] + 0.8* GainShapeTemp[i] */
    3667           0 :                 GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
    3668           0 :                 move16(); /* Q14 */
    3669             :             }
    3670             :         }
    3671             :         ELSE
    3672             :         {
    3673           0 :             FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    3674             :             {
    3675           0 :                 GainShapeTemp[i] = add( GainShapeTemp[i - 1], GainGradFEC[i] );
    3676           0 :                 move16();
    3677           0 :                 GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
    3678           0 :                 move16();
    3679             :             }
    3680             :         }
    3681             :     }
    3682             : 
    3683             :     /* Get the gainshape and gain frame for the current frame*/
    3684           0 :     test();
    3685           0 :     test();
    3686           0 :     test();
    3687           0 :     IF( ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) ) && EQ_16( st_fx->nbLostCmpt, 1 ) )
    3688             :     {
    3689           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3690             :         {
    3691           0 :             FOR( j = 0; j < 4; j++ )
    3692             :             {
    3693           0 :                 tmp = mult_r( GainShapeTemp[i], 19660 ); /* GainShapeTemp[i]*0.6 */
    3694             : 
    3695           0 :                 IF( GT_16( 8192, tmp ) )
    3696             :                 {
    3697           0 :                     GainShape[i * 4 + j] = shl( tmp, 2 );
    3698           0 :                     move16(); /* (GainShapeTemp[i]*0.6)>>1 */
    3699             :                 }
    3700             :                 ELSE
    3701             :                 {
    3702           0 :                     GainShape[i * 4 + j] = 32767;
    3703           0 :                     move16(); /* Clipping here to avoid the a huge change of the code due to gain shape change */
    3704             :                 }
    3705             :             }
    3706             :         }
    3707           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 31129 );
    3708           0 :         move16();
    3709             :     }
    3710           0 :     ELSE IF( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) )
    3711             :     {
    3712           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3713             :         {
    3714           0 :             FOR( j = 0; j < 4; j++ )
    3715             :             {
    3716           0 :                 IF( LT_16( GainShapeTemp[i], 16384 ) )
    3717             :                 {
    3718           0 :                     GainShape[i * 4 + j] = shl( GainShapeTemp[i], 1 );
    3719           0 :                     move16();
    3720             :                 }
    3721             :                 ELSE
    3722             :                 {
    3723           0 :                     GainShape[i * 4 + j] = 32767;
    3724           0 :                     move16();
    3725             :                 }
    3726             :             }
    3727             :         }
    3728           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 31129 );
    3729           0 :         move16();
    3730             :     }
    3731           0 :     ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
    3732             :     {
    3733           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3734             :         {
    3735           0 :             FOR( j = 0; j < 4; j++ )
    3736             :             {
    3737           0 :                 GainShape[add( i * 4, j )] = GainShapeTemp[i];
    3738           0 :                 move16();
    3739             :             }
    3740             :         }
    3741           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 16384 );
    3742           0 :         move16();
    3743             :     }
    3744             :     ELSE
    3745             :     {
    3746           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3747             :         {
    3748           0 :             FOR( j = 0; j < 4; j++ )
    3749             :             {
    3750           0 :                 IF( LT_16( GainShapeTemp[i], 16384 ) )
    3751             :                 {
    3752           0 :                     GainShape[i * 4 + j] = shl( GainShapeTemp[i], 1 );
    3753           0 :                     move16();
    3754             :                 }
    3755             :                 ELSE
    3756             :                 {
    3757           0 :                     GainShape[i * 4 + j] = 32767;
    3758           0 :                     move16();
    3759             :                 }
    3760             :             }
    3761             :         }
    3762           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27852 );
    3763           0 :         move16();
    3764             :     }
    3765             : 
    3766           0 :     *GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, hBWE_TD->GainAttn_fx ); /* Q18 */
    3767           0 :     move32();
    3768           0 : }
    3769             : 
    3770             : /*-------------------------------------------------------------------*
    3771             :  * Dequant_lower_LSF()
    3772             :  *
    3773             :  * Dequantized the lower LSFs
    3774             :  *-------------------------------------------------------------------*/
    3775             : 
    3776             : 
    3777       73885 : static void Dequant_lower_LSF_fx(
    3778             :     const Word16 lsf_idx[], /* i  : LSF indices */
    3779             :     Word16 lsf_q[]          /* o  : Quantized LSFs Q15*/
    3780             : )
    3781             : {
    3782             :     Word16 i;
    3783             : 
    3784       73885 :     lsf_q[0] = lsf_q_cb_fx[0][lsf_idx[0]];
    3785       73885 :     move16();
    3786      369425 :     FOR( i = 1; i < NUM_Q_LSF; i++ )
    3787             :     {
    3788      295540 :         lsf_q[i] = add( lsf_q_cb_fx[i][lsf_idx[i]], lsf_q[i - 1] ); /*Q15*/
    3789      295540 :         move16();
    3790             :     }
    3791             : 
    3792       73885 :     return;
    3793             : }
    3794             : 
    3795             : /*-------------------------------------------------------------------*
    3796             :  * Map_higher_LSF()
    3797             :  *
    3798             :  * Map the higher LSFs from the lower LSFs
    3799             :  *-------------------------------------------------------------------*/
    3800             : 
    3801       73885 : static void Map_higher_LSF_fx(
    3802             :     Word16 lsf_q[],        /* i/o : Quantized lower LSFs               Q15*/
    3803             :     const Word16 m,        /* i   : Mirroring point                    Q15*/
    3804             :     const Word16 grid_in[] /* i   : Input LSF smoohthing grid   Q15*/
    3805             : )
    3806             : {
    3807             :     Word16 lsf_map[NUM_MAP_LSF];
    3808             :     Word16 grid[NUM_MAP_LSF];
    3809             :     Word16 last_q_lsf;
    3810             :     Word16 lsf_smooth[NUM_MAP_LSF];
    3811             :     Word16 offset;
    3812             :     Word16 i;
    3813             :     Word16 scale;
    3814             : 
    3815      443310 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3816             :     {
    3817      369425 :         lsf_map[i] = sub( shl( m, 1 ), lsf_q[NUM_MAP_LSF - 1 - i] ); /*Q15*/
    3818      369425 :         move16();
    3819             :     }
    3820             : 
    3821       73885 :     IF( GT_16( m, MAX_LSF_FX_BY_2 ) )
    3822             :     {
    3823       14981 :         offset = lsf_map[0]; /*Q15*/
    3824       14981 :         move16();
    3825       14981 :         scale = div_s( sub( MAX_LSF_FX, m ), m );
    3826       89886 :         FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3827             :         {
    3828       74905 :             lsf_map[i] = add( mult_r( sub( lsf_map[i], offset ), scale ), offset ); /*Q15*/
    3829       74905 :             move16();
    3830             :         }
    3831             :     }
    3832             : 
    3833       73885 :     last_q_lsf = lsf_q[NUM_Q_LSF - 1]; /*Q15*/
    3834       73885 :     move16();
    3835       73885 :     scale = sub( MAX_LSF_FX, last_q_lsf );
    3836             : 
    3837      443310 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3838             :     {
    3839      369425 :         grid[i] = add( mult_r( grid_in[i], scale ), last_q_lsf ); /*Q15*/
    3840      369425 :         move16();
    3841             :     }
    3842             : 
    3843      443310 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3844             :     {
    3845      369425 :         lsf_smooth[i] = sub( mult_r( grid_smoothing_fx[i], grid[i] ) /*Q15*/,
    3846      369425 :                              mult_r( lsf_map[i], add( grid_smoothing_fx[i], -32768 /*-1.0f Q15*/ /*Q15*/ ) ) ); /*Q15*/
    3847      369425 :         move16();
    3848             :     }
    3849             : 
    3850      443310 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3851             :     {
    3852      369425 :         lsf_q[NUM_Q_LSF + i] = lsf_smooth[i]; /*Q15*/
    3853      369425 :         move16();
    3854             :     }
    3855             : 
    3856       73885 :     return;
    3857             : }
    3858             : 
    3859             : 
    3860       73885 : static void Dequant_mirror_point_fx(
    3861             :     const Word16 lsf_q[], /* i/o : Quantized lower LSFs Q15*/
    3862             :     const Word16 m_idx,   /* i   : Mirror point index   Q0 */
    3863             :     Word16 *m             /* i   : Mirroring point      Q15*/
    3864             : )
    3865             : {
    3866       73885 :     *m = add( mirror_point_q_cb_fx[m_idx], lsf_q[NUM_Q_LSF - 1] ); /*Q15*/
    3867       73885 :     move16();
    3868             : 
    3869       73885 :     return;
    3870             : }
    3871             : 
    3872             : 
    3873        6394 : static Word16 dotp_loc(
    3874             :     const Word16 x[], /* i  : vector x[]                   Qx */
    3875             :     const Word32 y[], /* i  : vector y[]                   Qy */
    3876             :     const Word16 n    /* i  : vector length                 */
    3877             : )
    3878             : {
    3879             :     Word16 i;
    3880             :     Word32 suma;
    3881        6394 :     Word16 guarded_bits = find_guarded_bits_fx( n );
    3882        6394 :     suma = L_shr( Mpy_32_16_1( y[0], x[0] ), guarded_bits ); /*Qx + Qy - guarded_bits*/
    3883             : 
    3884       51152 :     FOR( i = 1; i < n; i++ )
    3885             :     {
    3886       44758 :         suma = L_add( suma, L_shr( Mpy_32_16_1( y[i], x[i] ), guarded_bits ) ); /*Qx + Qy - guarded_bits*/
    3887             :     }
    3888        6394 :     suma = L_shl_sat( suma, guarded_bits ); /*Qx + Qy*/
    3889             : 
    3890        6394 :     return extract_h( suma ); /*Qx + Qy - 16*/
    3891             : }
    3892             : 
    3893      109686 : void ivas_dequantizeSHBparams_fx_9_1(
    3894             :     Decoder_State *st_fx,
    3895             :     const Word16 extl,        /* i  : extension layer                             */
    3896             :     Word32 extl_brate,        /* i  : extensiuon layer bitrate                    */
    3897             :     Word16 *Q_lsf,            /* o  : SHB LSF from de-quantization             Q15*/
    3898             :     Word16 *Q_subgain,        /* o  : SHB subframe gains from de-quantization  Q15*/
    3899             :     Word32 *Q_framegrain,     /* o  : SHB frame gain from de-quantization      Q18*/
    3900             :     Word16 *uv_flag,          /* o  : unvoiced flag*/
    3901             :     Word32 *Q_shb_ener_sf,    /* o  : Q15                                         */
    3902             :     Word16 *Q_shb_res_gshape, /* o  : Q14                                         */
    3903             :     Word16 *Q_mixFactors,     /* o  : Q15                                         */
    3904             :     Word16 *MSFlag )
    3905             : {
    3906             :     Word16 i, j, idxLSF, idxSubGain, idxFrameGain;
    3907             :     Word16 Q_combined_gains[NUM_SHB_SUBFR / 4];
    3908             :     Word16 lsf_q[LPC_SHB_ORDER];
    3909             :     Word16 lsf_idx[NUM_Q_LSF];
    3910             :     Word16 m_idx, grid_idx;
    3911             :     Word16 m;
    3912             :     Word32 L_tmp;
    3913             :     Word16 tmp, frac, exp;
    3914             :     Word16 idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
    3915             :     Word16 temp_shb_ener_sf_fx;
    3916             :     TD_BWE_DEC_HANDLE hBWE_TD;
    3917             : 
    3918             :     UWord32 Idx_lvq;
    3919             :     Word16 Idx, Idx_pred;
    3920             :     Word16 num_bits_lvq;
    3921             :     Word16 out[LATTICE_DIM];
    3922             :     const Word16 *cb_stage;
    3923             :     Word16 predictor_bits;
    3924      109686 :     Word16 nbits = NUM_BITS_SHB_MSLVQ;
    3925      109686 :     move16();
    3926      109686 :     hBWE_TD = st_fx->hBWE_TD;
    3927             : 
    3928             :     /* LSFs */
    3929             : 
    3930      109686 :     IF( EQ_16( extl, WB_TBE ) )
    3931             :     {
    3932        7101 :         IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
    3933             :         {
    3934        2478 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    3935             :             {
    3936           0 :                 idxFrameGain = hBWE_TD->gFrame_WB;
    3937           0 :                 move16();
    3938           0 :                 idxLSF = hBWE_TD->lsf_WB;
    3939           0 :                 move16();
    3940             :             }
    3941             :             ELSE
    3942             :             {
    3943        2478 :                 idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain_LBR_WB );
    3944        2478 :                 idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_LBR_WB_LSF );
    3945             :             }
    3946             : 
    3947        2478 :             Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_LBR_WB ), Q_lsf, LPC_SHB_ORDER_LBR_WB );
    3948        2478 :             set16_fx( Q_subgain, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    3949        2478 :             Copy32( SHBCB_FrameGain16_fx + idxFrameGain, Q_framegrain, 1 );
    3950             :         }
    3951             :         ELSE
    3952             :         {
    3953        4623 :             *uv_flag = (Word16) get_next_indice_fx( st_fx, 1 );
    3954        4623 :             move16();
    3955        4623 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    3956        4623 :             idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain );
    3957        4623 :             idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_WB_LSF );
    3958        4623 :             Copy( wb_bwe_lsfvq_cbook_8bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_WB ), Q_lsf, LPC_SHB_ORDER_WB );
    3959        4623 :             Copy( HBCB_SubGain5bit_fx + i_mult( idxSubGain, NUM_SHB_SUBFR / 4 ), Q_combined_gains, NUM_SHB_SUBFR / 4 );
    3960             : 
    3961       23115 :             FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3962             :             {
    3963             : 
    3964       18492 :                 L_tmp = L_mult( Q_combined_gains[i], 21771 ); /*Q26  0.166096 in Q17 */
    3965       18492 :                 L_tmp = L_shr( L_tmp, 10 );
    3966       18492 :                 frac = L_Extract_lc( L_tmp, &exp );
    3967       18492 :                 tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    3968             :                 /* output of Pow2() will be: */
    3969             :                 /* 16384 < Pow2() <= 32767 */
    3970       18492 :                 Q_combined_gains[i] = shl( tmp, add( exp, 1 ) ); /* Q15 */
    3971       18492 :                 move16();
    3972             :             }
    3973             : 
    3974       23115 :             FOR( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
    3975             :             {
    3976       18492 :                 Q_subgain[i] = Q_combined_gains[i / 2]; // Q15
    3977       18492 :                 move16();
    3978       18492 :                 Q_subgain[i + 1] = Q_combined_gains[i / 2]; // Q15
    3979       18492 :                 move16();
    3980             :             }
    3981             : 
    3982             :             /* frame gain */
    3983        4623 :             Copy32( SHBCB_FrameGain64_fx + idxFrameGain, Q_framegrain, 1 );
    3984             :         }
    3985             :     }
    3986             :     ELSE /* SWB TBE DEC */
    3987             :     {
    3988      102585 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    3989             :         {
    3990           0 :             idxSubGain = hBWE_TD->idxSubGains;
    3991           0 :             move16();
    3992           0 :             idxFrameGain = hBWE_TD->idxFrameGain;
    3993           0 :             move16();
    3994             :         }
    3995             :         ELSE
    3996             :         {
    3997      102585 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    3998      102585 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3999             :             {
    4000        3197 :                 idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN_1k75 );
    4001             :             }
    4002             :             ELSE
    4003             :             {
    4004       99388 :                 idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
    4005             :             }
    4006             :         }
    4007             : 
    4008             :         /* Multi Source Flag */
    4009      102585 :         test();
    4010      102585 :         test();
    4011      102585 :         IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && !( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) && st_fx->tdm_LRTD_flag ) )
    4012             :         {
    4013       22598 :             *MSFlag = (Word16) get_next_indice_fx( st_fx, STEREO_ICBWE_MSFLAG_BITS );
    4014       22598 :             move16();
    4015             :         }
    4016             :         ELSE
    4017             :         {
    4018       79987 :             *MSFlag = 0;
    4019       79987 :             move16();
    4020             :         }
    4021             : 
    4022      102585 :         IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) )
    4023             :         {
    4024       22050 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4025             :             {
    4026           0 :                 idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
    4027           0 :                 move16();
    4028             :             }
    4029             :             ELSE
    4030             :             {
    4031       22050 :                 idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
    4032             :             }
    4033       22050 :             temp_shb_ener_sf_fx = usdequant_fx( idx_shb_fr_gain, 0, 86 ); /* 86 = 0.042f in Q11 = Qin-1 */
    4034             :             /* o: temp_shb_ener_sf_fx in Q12 */
    4035             : 
    4036             :             /* *Q_shb_ener_sf = Pow(10.0, temp_shb_ener_sf_fx );        */
    4037             :             /*                = pow(2, 3.321928*temp_shb_ener_sf_fx)    */
    4038       22050 :             L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
    4039       22050 :             L_tmp = L_shl( L_tmp, -10 );                  /* bring L_tmp from Q26 to Q16 */
    4040       22050 :             frac = L_Extract_lc( L_tmp, &exp );           /* Extract exponent */
    4041       22050 :             L_tmp = Pow2( 14, frac );
    4042       22050 :             *Q_shb_ener_sf = L_shl( L_tmp, sub( exp, 14 ) /*exp - 14 + 0 */ ); /* In Q_ener: 2*Q_shb+1, Q_shb = 0; */
    4043       22050 :             move32();
    4044             : 
    4045      132300 :             FOR( i = 0; i < 5; i++ )
    4046             :             {
    4047      110250 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4048             :                 {
    4049           0 :                     idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
    4050           0 :                     move16();
    4051             :                 }
    4052             :                 ELSE
    4053             :                 {
    4054      110250 :                     idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
    4055      110250 :                     move16();
    4056             :                 }
    4057      110250 :                 Q_shb_res_gshape[i] = usdequant_fx( idx_res_gs[i],
    4058             :                                                     2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
    4059             :                                                     1024 /*0.125f Q13*/  /*1024 = 0.125 in Q13 */
    4060             :                 );
    4061      110250 :                 move16();
    4062             :                 /* o: Q_shb_res_gshape in Q14 */
    4063             :             }
    4064             : 
    4065       22050 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4066             :             {
    4067           0 :                 idx_mixFac = hBWE_TD->idx_mixFac;
    4068           0 :                 move16();
    4069             :             }
    4070             :             ELSE
    4071             :             {
    4072       22050 :                 idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4073             :             }
    4074       22050 :             *Q_mixFactors = usdequant_fx( idx_mixFac, 4096 /* 0.125 in Q15 */, 2048 /* 0.125 in Q14 */ );
    4075       22050 :             move16();
    4076             :             /* o: Q_mixFactors in Q15 */
    4077             :         }
    4078             :         ELSE
    4079             :         {
    4080       80535 :             *Q_shb_ener_sf = L_deposit_l( 0 );
    4081       80535 :             move32();
    4082       80535 :             test();
    4083       80535 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4084             :             {
    4085        4103 :                 idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4086        4103 :                 *Q_mixFactors = usdequant_fx( idx_mixFac, 0 /* 0.0f in Q15*/, 2341 /*1.0f / ((1 << NUM_BITS_SHB_VF) - 1) in Q14*/ );
    4087        4103 :                 move16();
    4088             :             }
    4089             :             ELSE
    4090             :             {
    4091       76432 :                 *Q_mixFactors = 0;
    4092       76432 :                 move16();
    4093             :             }
    4094       80535 :             set16_fx( Q_shb_res_gshape, 0, 5 );
    4095             :         }
    4096             : 
    4097             :         /* LSFs */
    4098      102585 :         test();
    4099      102585 :         test();
    4100      102585 :         test();
    4101      102585 :         test();
    4102      102585 :         test();
    4103      102585 :         test();
    4104      102585 :         test();
    4105      102585 :         test();
    4106      102585 :         test();
    4107      102585 :         IF( ( EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) ) && EQ_16( st_fx->codec_mode, MODE1 ) )
    4108             :         {
    4109       26856 :             set16_fx( lsf_idx, 0, 5 );
    4110       26856 :             lsf_idx[0] = (Word16) get_next_indice_fx( st_fx, 8 );
    4111       26856 :             move16();
    4112       26856 :             grid_idx = 0;
    4113       26856 :             move16();
    4114       26856 :             m_idx = 0;
    4115       26856 :             move16();
    4116             : 
    4117       26856 :             Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
    4118             :         }
    4119       75729 :         ELSE IF( ( st_fx->rf_flag == 0 ) && !( ( ( st_fx->element_mode == EVS_MONO ) && EQ_32( st_fx->total_brate, ACELP_9k60 ) ) || ( ( st_fx->element_mode == EVS_MONO ) && ( ( st_fx->total_brate == 0 ) ) && ( ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) ) || ( EQ_32( st_fx->last_total_brate, ACELP_13k20 ) && st_fx->rf_flag_last ) ) ) ) )
    4120             :         {
    4121       75729 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4122             :             {
    4123             :                 /* read multi-stage LVQ quantizer */
    4124        3197 :                 IF( GE_16( nbits, 19 ) )
    4125             :                 {
    4126        3197 :                     cb_stage = cb_LSF_BWE_fx[0];
    4127        3197 :                     move16();
    4128             :                 }
    4129             :                 ELSE
    4130             :                 {
    4131           0 :                     cb_stage = cb_LSF_BWE_fx[1];
    4132           0 :                     move16();
    4133             :                 }
    4134        3197 :                 set16_fx( lsf_q, 0, LPC_SHB_ORDER );
    4135             : 
    4136             :                 /* VQ part */
    4137        3197 :                 num_bits_lvq = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3];
    4138        3197 :                 Idx = get_next_indice_fx( st_fx, num_bits_lvq );
    4139        3197 :                 v_add_16( lsf_q, cb_stage + i_mult( Idx, 6 ), lsf_q, 6 );
    4140             : 
    4141             :                 /* MSLVQ part */
    4142        3197 :                 num_bits_lvq = sub( sub( nbits, num_bits_lvq ), config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2] );
    4143        3197 :                 predictor_bits = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
    4144        3197 :                 Idx_pred = 0;
    4145        3197 :                 move16();
    4146             : 
    4147        3197 :                 IF( EQ_16( num_bits_lvq, 16 ) )
    4148             :                 {
    4149             :                     /* MSLVQ part */
    4150           0 :                     Idx_lvq = L_add( get_next_indice_fx( st_fx, sub( num_bits_lvq, 1 ) ), L_shl( get_next_indice_fx( st_fx, 1 ), 15 ) );
    4151           0 :                     deindex_lvq_SHB_fx( Idx_lvq, out, num_bits_lvq, 0 );
    4152             :                 }
    4153             :                 ELSE
    4154             :                 {
    4155             :                     /* MSLVQ part */
    4156        3197 :                     Idx_lvq = get_next_indice_fx( st_fx, num_bits_lvq );
    4157        3197 :                     deindex_lvq_SHB_fx( Idx_lvq, out, num_bits_lvq, extract_l( LT_16( nbits, 19 ) ) );
    4158             :                 }
    4159             : 
    4160             :                 /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
    4161        3197 :                 v_add_16( lsf_q, out, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
    4162             : 
    4163             :                 /* predict last 2 components */
    4164        3197 :                 IF( predictor_bits == 0 )
    4165             :                 {
    4166           0 :                     lsf_q[LATTICE_DIM] = dotp_loc( lsf_q, LastCoefPred_0bit_fx, LATTICE_DIM );
    4167           0 :                     move16();
    4168           0 :                     lsf_q[LATTICE_DIM + 1] = dotp_loc( lsf_q, &LastCoefPred_0bit_fx[LATTICE_DIM + 1], LATTICE_DIM );
    4169           0 :                     move16();
    4170             :                 }
    4171             :                 ELSE
    4172             :                 {
    4173        3197 :                     Idx_pred = (Word16) get_next_indice_fx( st_fx, 1 );
    4174        3197 :                     lsf_q[LATTICE_DIM] = dotp_loc( lsf_q, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * Idx_pred], LATTICE_DIM );
    4175        3197 :                     move16();
    4176        3197 :                     lsf_q[LATTICE_DIM + 1] = dotp_loc( lsf_q, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * Idx_pred + LATTICE_DIM + 1], LATTICE_DIM );
    4177        3197 :                     move16();
    4178             :                 }
    4179             : 
    4180        3197 :                 IF( LT_16( nbits, NUM_BITS_SHB_MSLVQ ) )
    4181             :                 {
    4182           0 :                     Idx_pred = (Word16) get_next_indice_fx( st_fx, sub( NUM_BITS_SHB_MSLVQ, nbits ) );
    4183             :                 }
    4184             : 
    4185        3197 :                 v_add_16( SHB_LSF_mean_fx, lsf_q, lsf_q, LPC_SHB_ORDER );
    4186        3197 :                 v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
    4187             :             }
    4188             :             ELSE
    4189             :             {
    4190             :                 /* LSFs */
    4191       72532 :                 test();
    4192       72532 :                 test();
    4193       72532 :                 test();
    4194       72532 :                 IF( EQ_32( extl_brate, SWB_TBE_1k6 ) || EQ_32( extl_brate, FB_TBE_1k8 ) || EQ_32( extl_brate, SWB_TBE_2k8 ) || EQ_32( extl_brate, FB_TBE_3k0 ) )
    4195             :                 {
    4196       72532 :                     IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4197             :                     {
    4198           0 :                         FOR( i = 0; i < NUM_Q_LSF; i++ )
    4199             :                         {
    4200           0 :                             lsf_idx[i] = hBWE_TD->lsf_idx[i];
    4201           0 :                             move16();
    4202             :                         }
    4203             :                     }
    4204             :                     ELSE
    4205             :                     {
    4206      435192 :                         FOR( i = 0; i < NUM_Q_LSF; i++ )
    4207             :                         {
    4208      362660 :                             lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
    4209      362660 :                             move16();
    4210             :                         }
    4211             :                     }
    4212             :                 }
    4213       72532 :                 Dequant_lower_LSF_fx( lsf_idx, lsf_q );
    4214             : 
    4215       72532 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4216             :                 {
    4217           0 :                     m_idx = hBWE_TD->m_idx;
    4218           0 :                     move16();
    4219             :                 }
    4220             :                 ELSE
    4221             :                 {
    4222       72532 :                     m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
    4223             :                 }
    4224             : 
    4225       72532 :                 Dequant_mirror_point_fx( lsf_q, m_idx, &m );
    4226             : 
    4227             :                 /* safety check in case of bit errors */
    4228       72532 :                 IF( GT_16( m, MAX_LSF_FX ) )
    4229             :                 {
    4230           0 :                     st_fx->BER_detect = 1;
    4231           0 :                     move16();
    4232           0 :                     m = MAX_LSF_FX - 1;
    4233           0 :                     move16();
    4234             :                 }
    4235             : 
    4236       72532 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4237             :                 {
    4238           0 :                     grid_idx = hBWE_TD->grid_idx;
    4239           0 :                     move16();
    4240             :                 }
    4241             :                 ELSE
    4242             :                 {
    4243       72532 :                     grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
    4244             :                 }
    4245             : 
    4246       72532 :                 Map_higher_LSF_fx( lsf_q, m, lsf_grid_fx[grid_idx] );
    4247             :             }
    4248      833019 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    4249             :             {
    4250             :                 /* safety check in case of bit errors */
    4251      757290 :                 IF( GT_16( lsf_q[LPC_SHB_ORDER - 1 - i], MAX_LSF_FX ) )
    4252             :                 {
    4253           0 :                     st_fx->BER_detect = 1;
    4254           0 :                     move16();
    4255           0 :                     lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF_FX - 1;
    4256           0 :                     move16();
    4257             :                 }
    4258      757290 :                 Q_lsf[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
    4259      757290 :                 move16();
    4260             :             }
    4261             :         }
    4262             :         ELSE
    4263             :         {
    4264           0 :             set16_fx( lsf_idx, 0, 5 );
    4265           0 :             Copy( hBWE_TD->lsf_idx, lsf_idx, 5 );
    4266           0 :             grid_idx = 0;
    4267           0 :             move16();
    4268           0 :             m_idx = 0;
    4269           0 :             move16();
    4270           0 :             Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
    4271             :         }
    4272             : 
    4273      102585 :         space_lsfs_fx( Q_lsf, LPC_SHB_ORDER );
    4274             : 
    4275             :         /* Dequantize subgain indices */
    4276      102585 :         j = i_mult( idxSubGain, NUM_SHB_SUBGAINS );
    4277      102585 :         move16();
    4278      512925 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4279             :         {
    4280             :             /*    Q_subgain[i] = (float) pow(10.0, SHBCB_SubGain5bit[j++]); */
    4281             : 
    4282      410340 :             L_tmp = L_mult( SHBCB_SubGain5bit_fx[j++], 27213 ); /*Q28  3.321928 in Q13 */
    4283      410340 :             L_tmp = L_shr( L_tmp, 12 );
    4284      410340 :             frac = L_Extract_lc( L_tmp, &exp );
    4285      410340 :             tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    4286             :             /* output of Pow2() will be: */
    4287             :             /* 16384 < Pow2() <= 32767 */
    4288      410340 :             Q_subgain[i] = shl( tmp, add( exp, 1 ) ); /*Q15*/
    4289      410340 :             move16();
    4290             :         }
    4291             : 
    4292     1743945 :         FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    4293             :         {
    4294     1641360 :             Word16 idx = 0;
    4295     1641360 :             move16();
    4296     1641360 :             IF( i != 0 )
    4297             :             {
    4298     1538775 :                 idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
    4299             :             }
    4300     1641360 :             Q_subgain[i] = Q_subgain[idx];
    4301     1641360 :             move16();
    4302             :         }
    4303             : 
    4304             :         /* Frame gain */
    4305      102585 :         IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4306             :         {
    4307        3197 :             *Q_framegrain = L_mac( SHB_GAIN_QLOW_1k75_FX, idxFrameGain, SHB_GAIN_QDELTA_1k75_FX );
    4308        3197 :             move32();
    4309             :         }
    4310             :         ELSE
    4311             :         {
    4312       99388 :             *Q_framegrain = L_mac( SHB_GAIN_QLOW_FX, idxFrameGain, SHB_GAIN_QDELTA_FX );
    4313       99388 :             move32();
    4314             :         }                                                   /*Q18*/
    4315      102585 :         L_tmp = Mult_32_16( *Q_framegrain, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    4316      102585 :         frac = L_Extract_lc( L_tmp, &exp );
    4317      102585 :         L_tmp = Pow2( 30, frac );
    4318      102585 :         *Q_framegrain = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    4319      102585 :         move32();
    4320             :     }
    4321             : 
    4322      109686 :     return;
    4323             : }
    4324             : 
    4325             : 
    4326             : /*==========================================================================*/
    4327             : /* FUNCTION      : static void dequantizeSHBparams_fx_9_1 ()        */
    4328             : /*--------------------------------------------------------------------------*/
    4329             : /* PURPOSE       : Dequantize super highband spectral envolope        */
    4330             : /*          temporal gains and frame gain              */
    4331             : /*--------------------------------------------------------------------------*/
    4332             : /* INPUT ARGUMENTS  :                            */
    4333             : /* _Word16 extl               i  : extension layer                */
    4334             : /* _Word32 extl_brate         i  : extensiuon layer bitrate          */
    4335             : /*--------------------------------------------------------------------------*/
    4336             : /* OUTPUT ARGUMENTS :                            */
    4337             : /* _Word16 *Q_lsf,              o  : SHB LSF from de-quantization       Q15 */
    4338             : /* _Word16 *Q_subgain,      o  : SHB subframe gains from de-quantization Q15*/
    4339             : /* _Word32 *Q_framegrain      o  : SHB frame gain from de-quantization Q18  */
    4340             : /*--------------------------------------------------------------------------*/
    4341             : /* INPUT/OUTPUT ARGUMENTS :                          */
    4342             : /*--------------------------------------------------------------------------*/
    4343             : /* RETURN ARGUMENTS :                            */
    4344             : /*           _ None                          */
    4345             : /*--------------------------------------------------------------------------*/
    4346             : /* CALLED FROM :                               */
    4347             : /*==========================================================================*/
    4348             : 
    4349        1353 : static void dequantizeSHBparams_fx_9_1(
    4350             :     Decoder_State *st_fx,
    4351             :     const Word16 extl,        /* i  : extension layer                             */
    4352             :     Word32 extl_brate,        /* i  : extensiuon layer bitrate                    */
    4353             :     Word16 *Q_lsf,            /* o  : SHB LSF from de-quantization             Q15*/
    4354             :     Word16 *Q_subgain,        /* o  : SHB subframe gains from de-quantization  Q15*/
    4355             :     Word32 *Q_framegrain,     /* o  : SHB frame gain from de-quantization      Q18*/
    4356             :     Word16 *uv_flag,          /* o  : unvoiced flag*/
    4357             :     Word32 *Q_shb_ener_sf,    /* o  :                                          Q15*/
    4358             :     Word16 *Q_shb_res_gshape, /* o  :                                          Q14*/
    4359             :     Word16 *Q_mixFactors      /* o  :                                          Q15*/
    4360             : )
    4361             : {
    4362             :     Word16 i, j, idxLSF, idxSubGain, idxFrameGain;
    4363             :     Word16 Q_combined_gains[NUM_SHB_SUBFR / 4];
    4364             :     Word16 lsf_q[LPC_SHB_ORDER];
    4365             :     Word16 lsf_idx[NUM_Q_LSF];
    4366             :     Word16 m_idx, grid_idx;
    4367             :     Word16 m;
    4368             :     Word32 L_tmp;
    4369             :     Word16 tmp, frac, exp;
    4370             :     Word16 idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
    4371             :     Word16 temp_shb_ener_sf_fx;
    4372             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4373        1353 :     hBWE_TD = st_fx->hBWE_TD;
    4374             : 
    4375             :     /* LSFs */
    4376             : 
    4377        1353 :     IF( EQ_16( extl, WB_TBE ) )
    4378             :     {
    4379           0 :         IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
    4380             :         {
    4381           0 :             idxFrameGain = hBWE_TD->gFrame_WB;
    4382           0 :             move16();
    4383           0 :             idxLSF = hBWE_TD->lsf_WB;
    4384           0 :             move16();
    4385             : 
    4386           0 :             Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_LBR_WB ), Q_lsf, LPC_SHB_ORDER_LBR_WB );
    4387           0 :             set16_fx( Q_subgain, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    4388           0 :             Copy32( SHBCB_FrameGain16_fx + idxFrameGain, Q_framegrain, 1 );
    4389             :         }
    4390             :         ELSE
    4391             :         {
    4392           0 :             *uv_flag = (Word16) get_next_indice_fx( st_fx, 1 );
    4393           0 :             move16();
    4394           0 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    4395           0 :             idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain );
    4396           0 :             idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_WB_LSF );
    4397           0 :             Copy( wb_bwe_lsfvq_cbook_8bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_WB ), Q_lsf, LPC_SHB_ORDER_WB );
    4398           0 :             Copy( HBCB_SubGain5bit_fx + i_mult( idxSubGain, NUM_SHB_SUBFR / 4 ), Q_combined_gains, NUM_SHB_SUBFR / 4 );
    4399             : 
    4400           0 :             FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    4401             :             {
    4402             : 
    4403           0 :                 L_tmp = L_mult( Q_combined_gains[i], 21771 ); /*Q26  0.166096 in Q17 */
    4404           0 :                 L_tmp = L_shr( L_tmp, 10 );
    4405           0 :                 frac = L_Extract_lc( L_tmp, &exp );
    4406           0 :                 tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    4407             :                 /* output of Pow2() will be: */
    4408             :                 /* 16384 < Pow2() <= 32767 */
    4409           0 :                 Q_combined_gains[i] = shl( tmp, add( exp, 1 ) ); /* Q15 */
    4410           0 :                 move16();
    4411             :             }
    4412             : 
    4413           0 :             FOR( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
    4414             :             {
    4415           0 :                 Q_subgain[i] = Q_combined_gains[i / 2];
    4416           0 :                 move16();
    4417           0 :                 Q_subgain[i + 1] = Q_combined_gains[i / 2];
    4418           0 :                 move16();
    4419             :             }
    4420             : 
    4421             :             /* frame gain */
    4422           0 :             Copy32( SHBCB_FrameGain64_fx + idxFrameGain, Q_framegrain, 1 );
    4423             :         }
    4424             :     }
    4425             :     ELSE /* SWB TBE DEC */
    4426             :     {
    4427        1353 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4428             :         {
    4429         612 :             idxSubGain = hBWE_TD->idxSubGains;
    4430         612 :             move16();
    4431         612 :             idxFrameGain = hBWE_TD->idxFrameGain;
    4432         612 :             move16();
    4433             :         }
    4434             :         ELSE
    4435             :         {
    4436         741 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    4437         741 :             idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
    4438             :         }
    4439             : 
    4440        1353 :         test();
    4441        1353 :         IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    4442             :         {
    4443         612 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4444             :             {
    4445         612 :                 idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
    4446         612 :                 move16();
    4447             :             }
    4448             :             ELSE
    4449             :             {
    4450           0 :                 idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
    4451             :             }
    4452         612 :             temp_shb_ener_sf_fx = usdequant_fx( idx_shb_fr_gain, 0, 86 ); /* 86 = 0.042f in Q11 = Qin-1 */
    4453             :             /* o: temp_shb_ener_sf_fx in Q12 */
    4454             : 
    4455             :             /* *Q_shb_ener_sf = Pow(10.0, temp_shb_ener_sf_fx );        */
    4456             :             /*                = pow(2, 3.321928*temp_shb_ener_sf_fx)    */
    4457         612 :             L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
    4458         612 :             L_tmp = L_shl( L_tmp, -10 );                  /* bring L_tmp from Q26 to Q16 */
    4459         612 :             frac = L_Extract_lc( L_tmp, &exp );           /* Extract exponent */
    4460         612 :             L_tmp = Pow2( 14, frac );
    4461         612 :             *Q_shb_ener_sf = L_shl( L_tmp, sub( exp, 14 ) /* exp - 14 + 0 */ ); /* In Q_ener: 2*Q_shb+1, Q_shb = 0; */
    4462         612 :             move32();
    4463             : 
    4464        3672 :             FOR( i = 0; i < 5; i++ )
    4465             :             {
    4466        3060 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4467             :                 {
    4468        3060 :                     idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
    4469        3060 :                     move16();
    4470             :                 }
    4471             :                 ELSE
    4472             :                 {
    4473           0 :                     idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
    4474           0 :                     move16();
    4475             :                 }
    4476        3060 :                 Q_shb_res_gshape[i] = usdequant_fx( idx_res_gs[i],
    4477             :                                                     2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
    4478             :                                                     1024 /*0.125f Q13*/  /*1024 = 0.125 in Q13 */
    4479             :                 );
    4480        3060 :                 move16();
    4481             :                 /* o: Q_shb_res_gshape in Q14 */
    4482             :             }
    4483             : 
    4484         612 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4485             :             {
    4486         612 :                 idx_mixFac = hBWE_TD->idx_mixFac;
    4487         612 :                 move16();
    4488             :             }
    4489             :             ELSE
    4490             :             {
    4491           0 :                 idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4492             :             }
    4493         612 :             *Q_mixFactors = usdequant_fx( idx_mixFac, 4096 /* 0.125 in Q15 */, 2048 /* 0.125 in Q14 */ );
    4494         612 :             move16();
    4495             :             /* o: Q_mixFactors in Q15 */
    4496             :         }
    4497             :         ELSE
    4498             :         {
    4499         741 :             *Q_shb_ener_sf = L_deposit_l( 0 );
    4500         741 :             move32();
    4501         741 :             *Q_mixFactors = 0;
    4502         741 :             move16();
    4503         741 :             set16_fx( Q_shb_res_gshape, 0, 5 );
    4504             :         }
    4505             : 
    4506             :         /* LSFs */
    4507             : 
    4508             : 
    4509        1353 :         test();
    4510        1353 :         test();
    4511        1353 :         test();
    4512        1353 :         test();
    4513        1353 :         test();
    4514        1353 :         IF( ( ( st_fx->rf_flag == 0 ) ) && !( ( EQ_32( st_fx->total_brate, ACELP_9k60 ) ) || ( ( ( st_fx->total_brate == 0 ) ) && ( ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) ) || ( EQ_32( st_fx->last_total_brate, ACELP_13k20 ) && EQ_16( st_fx->rf_flag_last, 1 ) ) ) ) ) )
    4515             : 
    4516             :         {
    4517             :             /* LSFs */
    4518        1353 :             test();
    4519        1353 :             test();
    4520        1353 :             test();
    4521        1353 :             IF( EQ_32( extl_brate, SWB_TBE_1k6 ) || EQ_32( extl_brate, FB_TBE_1k8 ) || EQ_32( extl_brate, SWB_TBE_2k8 ) || EQ_32( extl_brate, FB_TBE_3k0 ) )
    4522             :             {
    4523        1353 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4524             :                 {
    4525        3672 :                     FOR( i = 0; i < NUM_Q_LSF; i++ )
    4526             :                     {
    4527        3060 :                         lsf_idx[i] = hBWE_TD->lsf_idx[i];
    4528        3060 :                         move16();
    4529             :                     }
    4530             :                 }
    4531             :                 ELSE
    4532             :                 {
    4533        4446 :                     FOR( i = 0; i < NUM_Q_LSF; i++ )
    4534             :                     {
    4535        3705 :                         lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
    4536        3705 :                         move16();
    4537             :                     }
    4538             :                 }
    4539             :             }
    4540        1353 :             Dequant_lower_LSF_fx( lsf_idx, lsf_q );
    4541             : 
    4542        1353 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4543             :             {
    4544         612 :                 m_idx = hBWE_TD->m_idx;
    4545         612 :                 move16();
    4546             :             }
    4547             :             ELSE
    4548             :             {
    4549         741 :                 m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
    4550             :             }
    4551             : 
    4552        1353 :             Dequant_mirror_point_fx( lsf_q, m_idx, &m );
    4553             : 
    4554             :             /* safety check in case of bit errors */
    4555        1353 :             IF( GT_16( m, MAX_LSF_FX ) )
    4556             :             {
    4557           0 :                 st_fx->BER_detect = 1;
    4558           0 :                 move16();
    4559           0 :                 m = MAX_LSF_FX - 1;
    4560           0 :                 move16();
    4561             :             }
    4562             : 
    4563        1353 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4564             :             {
    4565         612 :                 grid_idx = hBWE_TD->grid_idx;
    4566         612 :                 move16();
    4567             :             }
    4568             :             ELSE
    4569             :             {
    4570         741 :                 grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
    4571             :             }
    4572             : 
    4573        1353 :             Map_higher_LSF_fx( lsf_q, m, lsf_grid_fx[grid_idx] );
    4574             : 
    4575       14883 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    4576             :             {
    4577             :                 /* safety check in case of bit errors */
    4578       13530 :                 IF( GT_16( lsf_q[LPC_SHB_ORDER - 1 - i], MAX_LSF_FX ) )
    4579             :                 {
    4580           0 :                     st_fx->BER_detect = 1;
    4581           0 :                     move16();
    4582           0 :                     lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF_FX - 1;
    4583           0 :                     move16();
    4584             :                 }
    4585       13530 :                 Q_lsf[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
    4586       13530 :                 move16();
    4587             :             }
    4588             :         }
    4589             :         ELSE
    4590             :         {
    4591           0 :             set16_fx( lsf_idx, 0, 5 );
    4592           0 :             Copy( hBWE_TD->lsf_idx, lsf_idx, 5 );
    4593           0 :             grid_idx = 0;
    4594           0 :             move16();
    4595           0 :             m_idx = 0;
    4596           0 :             move16();
    4597           0 :             Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
    4598             :         }
    4599             : 
    4600        1353 :         space_lsfs_fx( Q_lsf, LPC_SHB_ORDER );
    4601             : 
    4602             :         /* Dequantize subgain indices */
    4603        1353 :         j = i_mult( idxSubGain, NUM_SHB_SUBGAINS );
    4604        1353 :         move16();
    4605        6765 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4606             :         {
    4607             :             /*    Q_subgain[i] = (float) pow(10.0, SHBCB_SubGain5bit[j++]); */
    4608             : 
    4609        5412 :             L_tmp = L_mult( SHBCB_SubGain5bit_fx[j], 27213 ); /*Q28  3.321928 in Q13 */
    4610        5412 :             j = add( j, 1 );
    4611        5412 :             L_tmp = L_shr( L_tmp, 12 );
    4612        5412 :             frac = L_Extract_lc( L_tmp, &exp );
    4613        5412 :             tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    4614             :             /* output of Pow2() will be: */
    4615             :             /* 16384 < Pow2() <= 32767 */
    4616        5412 :             Q_subgain[i] = shl( tmp, add( exp, 1 ) ); /*Q15*/
    4617        5412 :             move16();
    4618             :         }
    4619             : 
    4620       23001 :         FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    4621             :         {
    4622       21648 :             Word16 idx = 0;
    4623       21648 :             move16();
    4624       21648 :             IF( i != 0 )
    4625             :             {
    4626       20295 :                 idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
    4627             :             }
    4628       21648 :             Q_subgain[i] = Q_subgain[idx];
    4629       21648 :             move16();
    4630             :         }
    4631             : 
    4632             :         /* Frame gain */
    4633        1353 :         *Q_framegrain = L_mac( SHB_GAIN_QLOW_FX, idxFrameGain, SHB_GAIN_QDELTA_FX ); /*Q18*/
    4634        1353 :         move32();
    4635        1353 :         L_tmp = Mult_32_16( *Q_framegrain, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    4636        1353 :         frac = L_Extract_lc( L_tmp, &exp );
    4637        1353 :         L_tmp = Pow2( 30, frac );
    4638        1353 :         *Q_framegrain = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    4639        1353 :         move32();
    4640             :     }
    4641             : 
    4642        1353 :     return;
    4643             : }
    4644             : 
    4645             : /*-------------------------------------------------------------------*
    4646             :  * fb_tbe_dec()
    4647             :  *
    4648             :  * FB TBE decoder, 14(resp. 15.5) - 20 kHz band decoding module
    4649             :  *-------------------------------------------------------------------*/
    4650           0 : void fb_tbe_dec_fx(
    4651             :     Decoder_State *st,     /* i/o: encoder state structure                                */
    4652             :     const Word16 fb_exc[], /* i  : FB excitation from the SWB part      Q_fb_exc          */
    4653             :     Word16 Q_fb_exc,
    4654             :     Word16 *hb_synth, /* o  : high-band synthesis                    Q(15 - hb_synth_exp) */
    4655             :     Word16 hb_synth_exp )
    4656             : 
    4657             : {
    4658             :     Word16 i;
    4659           0 :     Word16 ratio = 0;
    4660           0 :     move16();
    4661           0 :     Word32 fb_exc_energy = 0;
    4662           0 :     move32();
    4663             :     Word16 fb_synth[L_FRAME48k];
    4664             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4665           0 :     hBWE_TD = st->hBWE_TD;
    4666             : 
    4667             :     /* decode FB slope information */
    4668           0 :     test();
    4669           0 :     test();
    4670           0 :     IF( EQ_16( st->extl, FB_TBE ) && !st->bfi )
    4671             :     {
    4672           0 :         IF( EQ_16( st->codec_mode, MODE2 ) )
    4673             :         {
    4674           0 :             i = hBWE_TD->idxGain;
    4675           0 :             move16();
    4676             :         }
    4677             :         ELSE
    4678             :         {
    4679           0 :             i = (Word16) get_next_indice_fx( st, 4 );
    4680             :         }
    4681           0 :         ratio = shl_sat( 1, i );
    4682             :     }
    4683           0 :     ELSE IF( EQ_16( st->extl, FB_TBE ) && st->bfi )
    4684             :     {
    4685           0 :         ratio = hBWE_TD->prev_fbbwe_ratio_fx;
    4686           0 :         move16();
    4687             :     }
    4688           0 :     fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k );
    4689             : 
    4690             :     /* FB TBE synthesis */
    4691           0 :     synthesise_fb_high_band_fx( fb_exc, Q_fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio_fx ), hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, hb_synth_exp,
    4692           0 :                                 st->element_mode );
    4693             : 
    4694             :     /* add the fb_synth component to the hb_synth component */
    4695             :     /*  v_add_fx( hb_synth, fb_synth, hb_synth, L_FRAME48k );*/
    4696           0 :     FOR( i = 0; i < L_FRAME48k; i++ )
    4697             :     {
    4698           0 :         hb_synth[i] = add_sat( hb_synth[i], fb_synth[i] );
    4699           0 :         move16();
    4700             :     }
    4701           0 :     return;
    4702             : }
    4703             : 
    4704       40669 : void fb_tbe_dec_ivas_fx(
    4705             :     Decoder_State *st,     /* i/o: encoder state structure                 */
    4706             :     const Word16 fb_exc[], /* i  : FB excitation from the SWB part      Q_fb_exc   */
    4707             :     Word16 Q_fb_exc,
    4708             :     Word32 *hb_synth, /* o  : high-band synthesis                    Q(15 - hb_synth_exp) */
    4709             :     Word16 hb_synth_exp,
    4710             :     Word16 *fb_synth_ref, /*Q_fb_synth_ref*/
    4711             :     Word16 Q_fb_synth_ref,
    4712             :     Word16 output_frame )
    4713             : 
    4714             : {
    4715             :     Word16 i;
    4716       40669 :     Word16 ratio = 0;
    4717       40669 :     move16();
    4718       40669 :     Word32 fb_exc_energy = 0;
    4719       40669 :     move32();
    4720             :     Word16 fb_synth[L_FRAME48k];
    4721             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4722       40669 :     hBWE_TD = st->hBWE_TD;
    4723             : 
    4724             :     /* decode FB slope information */
    4725       40669 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    4726             :     {
    4727       35133 :         test();
    4728       35133 :         test();
    4729       35133 :         IF( EQ_16( st->extl, FB_TBE ) && !st->bfi )
    4730             :         {
    4731       34385 :             IF( EQ_16( st->codec_mode, MODE2 ) )
    4732             :             {
    4733           0 :                 i = hBWE_TD->idxGain;
    4734           0 :                 move16();
    4735             :             }
    4736             :             ELSE
    4737             :             {
    4738       34385 :                 i = (Word16) get_next_indice_fx( st, 4 );
    4739             :             }
    4740       34385 :             ratio = shl_sat( 1, i );
    4741             :         }
    4742         748 :         ELSE IF( EQ_16( st->extl, FB_TBE ) && st->bfi )
    4743             :         {
    4744         748 :             ratio = hBWE_TD->prev_fbbwe_ratio_fx;
    4745         748 :             move16();
    4746             :         }
    4747             :     }
    4748             :     ELSE
    4749             :     {
    4750        5536 :         IF( !st->bfi )
    4751             :         {
    4752        5430 :             st->next_bit_pos = add( st->next_bit_pos, NUM_BITS_FB_FRAMEGAIN );
    4753        5430 :             move16();
    4754             :         }
    4755             : 
    4756        5536 :         return;
    4757             :     }
    4758       35133 :     fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k );
    4759             : 
    4760             :     /* FB TBE synthesis */
    4761       35133 :     synthesise_fb_high_band_fx( fb_exc, Q_fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio_fx ), hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, hb_synth_exp,
    4762       35133 :                                 st->element_mode );
    4763             : 
    4764       35133 :     test();
    4765       35133 :     IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && ( st->idchan == 0 ) )
    4766             :     {
    4767        3958 :         Copy_Scale_sig( fb_synth, fb_synth_ref, L_FRAME48k, sub( Q_fb_synth_ref, add( hb_synth_exp, 16 ) ) ); // scaling is required
    4768             :     }
    4769             :     /* add the fb_synth component to the hb_synth component */
    4770             :     /*  v_add_fx( hb_synth, fb_synth, hb_synth, L_FRAME48k );*/
    4771    33762813 :     FOR( i = 0; i < L_FRAME48k; i++ )
    4772             :     {
    4773             :         // hb_synth[i] = L_add( hb_synth[i], L_deposit_l(fb_synth[i]));
    4774    33727680 :         hb_synth[i] = L_add( hb_synth[i], L_shl( fb_synth[i], Q11 ) ); // hb_synth_exp
    4775    33727680 :         move16();
    4776             :     }
    4777       35133 :     return;
    4778             : }
    4779             : 
    4780         612 : void tbe_read_bitstream_fx(
    4781             :     Decoder_State *st_fx )
    4782             : {
    4783             :     Word16 i;
    4784             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4785         612 :     hBWE_TD = st_fx->hBWE_TD;
    4786             : 
    4787         612 :     test();
    4788         612 :     test();
    4789         612 :     test();
    4790         612 :     test();
    4791         612 :     test();
    4792         612 :     IF( ( EQ_16( st_fx->rf_flag, 1 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) ) && EQ_16( st_fx->bwidth, WB ) )
    4793             :     {
    4794             :         /* WB LSF */
    4795           0 :         hBWE_TD->lsf_WB = (Word16) get_next_indice_fx( st_fx, NUM_BITS_LBR_WB_LSF );
    4796           0 :         move16();
    4797             : 
    4798             :         /* WB frame gain */
    4799           0 :         hBWE_TD->gFrame_WB = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain_LBR_WB );
    4800           0 :         move16();
    4801             :     }
    4802         612 :     ELSE IF( ( GE_32( st_fx->total_brate, ACELP_9k60 ) ) && ( LE_32( st_fx->total_brate, ACELP_32k ) ) &&
    4803             :              ( ( EQ_16( st_fx->bwidth, SWB ) ) || ( EQ_16( st_fx->bwidth, FB ) ) ) )
    4804             :     {
    4805         612 :         test();
    4806         612 :         IF( ( ( st_fx->rf_flag == 0 ) ) && ( GT_32( st_fx->total_brate, ACELP_9k60 ) ) )
    4807             :         {
    4808        3672 :             FOR( i = 0; i < NUM_Q_LSF; i++ )
    4809             :             {
    4810        3060 :                 hBWE_TD->lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
    4811        3060 :                 move16();
    4812             :             }
    4813             : 
    4814         612 :             hBWE_TD->m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
    4815         612 :             move16();
    4816         612 :             hBWE_TD->grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
    4817         612 :             move16();
    4818             :         }
    4819             :         ELSE
    4820             :         {
    4821           0 :             hBWE_TD->lsf_idx[0] = (Word16) get_next_indice_fx( st_fx, 8 );
    4822           0 :             move16();
    4823           0 :             hBWE_TD->m_idx = 0;
    4824           0 :             move16();
    4825           0 :             hBWE_TD->grid_idx = 0;
    4826           0 :             move16();
    4827             :         }
    4828             : 
    4829             :         /* shape gains */
    4830         612 :         hBWE_TD->idxSubGains = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    4831         612 :         move16();
    4832             : 
    4833             :         /* frame gain */
    4834         612 :         hBWE_TD->idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
    4835         612 :         move16();
    4836             : 
    4837         612 :         IF( GE_32( st_fx->total_brate, ACELP_24k40 ) )
    4838             :         {
    4839             :             /* sub frame energy*/
    4840         612 :             hBWE_TD->idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
    4841         612 :             move16();
    4842             : 
    4843             :             /* gain shapes residual */
    4844        3672 :             FOR( i = 0; i < NB_SUBFR16k; i++ )
    4845             :             {
    4846        3060 :                 hBWE_TD->idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
    4847        3060 :                 move16();
    4848             :             }
    4849             : 
    4850             :             /* voicing factor */
    4851         612 :             hBWE_TD->idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4852         612 :             move16();
    4853             :         }
    4854             : 
    4855         612 :         IF( EQ_16( st_fx->tec_tfa, 1 ) )
    4856             :         {
    4857         612 :             st_fx->tec_flag = (Word16) get_next_indice_fx( st_fx, BITS_TEC );
    4858         612 :             move16();
    4859         612 :             st_fx->tfa_flag = (Word16) get_next_indice_fx( st_fx, BITS_TFA );
    4860         612 :             move16();
    4861         612 :             test();
    4862         612 :             IF( st_fx->tfa_flag && st_fx->tec_flag )
    4863             :             {
    4864           4 :                 st_fx->tec_flag = 2;
    4865           4 :                 move16();
    4866           4 :                 st_fx->tfa_flag = 0;
    4867           4 :                 move16();
    4868             :             }
    4869             :         }
    4870             :         ELSE
    4871             :         {
    4872           0 :             st_fx->tec_flag = 0;
    4873           0 :             move16();
    4874           0 :             st_fx->tfa_flag = 0;
    4875           0 :             move16();
    4876             :         }
    4877             :     }
    4878             : 
    4879         612 :     IF( EQ_16( st_fx->bwidth, FB ) )
    4880             :     {
    4881           0 :         hBWE_TD->idxGain = (Word16) get_next_indice_fx( st_fx, 4 );
    4882           0 :         move16();
    4883             :     }
    4884         612 : }
    4885             : 
    4886             : 
    4887             : /*---------------------------------------------------------------------*
    4888             :  * GenTransition()
    4889             :  *
    4890             :  * Generate a highband transition signal from the gain shape overlap
    4891             :  * buffer to fill the gap caused by the delay alignment buffer when
    4892             :  * switching from TBE to IGF
    4893             :  *---------------------------------------------------------------------*/
    4894             : 
    4895          23 : void GenTransition_fx(
    4896             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                                        */
    4897             :     Word16 *output_HB,         /* o  : synthesized HB transitions signal  st_fx->prev_Q_bwe_syn2 */
    4898             :     const Word32 output_Fs,    /* i  : output sampling rate                                      */
    4899             :     Word16 rf_flag,            /* i  : RF flag                                                   */
    4900             :     Word32 total_bitrate       /* i  : total bitrate                                             */
    4901             : )
    4902             : {
    4903             :     Word16 i, length;
    4904             :     Word16 syn_overlap_32k[L_FRAME32k];
    4905             :     Word32 L_tmp;
    4906          23 :     Word16 ol_len = 2 * SHB_OVERLAP_LEN;
    4907             : 
    4908             :     /* set targeted length of transition signal */
    4909          23 :     length = shl( NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ), 1 );
    4910             : 
    4911             :     /* upsample overlap snippet */
    4912          23 :     Interpolate_allpass_steep_fx( hBWE_TD->syn_overlap_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, SHB_OVERLAP_LEN, syn_overlap_32k );
    4913             : 
    4914             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    4915          23 :     test();
    4916          23 :     IF( ( rf_flag != 0 ) || EQ_32( total_bitrate, ACELP_9k60 ) )
    4917             :     {
    4918           0 :         flip_and_downmix_generic_fx( syn_overlap_32k, syn_overlap_32k, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem_fx,
    4919           0 :                                      hBWE_TD->genSHBsynth_Hilbert_Mem_fx + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ),
    4920             :                                      &( hBWE_TD->syn_dm_phase ) );
    4921             :     }
    4922             :     ELSE
    4923             :     {
    4924         483 :         FOR( i = 0; i < ol_len; i = i + 2 )
    4925             :         {
    4926         460 :             syn_overlap_32k[i] = negate( syn_overlap_32k[i] );
    4927         460 :             move16();
    4928         460 :             syn_overlap_32k[i + 1] = syn_overlap_32k[i + 1];
    4929         460 :             move16();
    4930             :         }
    4931             :     }
    4932             : 
    4933             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    4934         943 :     FOR( i = 0; i < ol_len; i++ )
    4935             :     {
    4936         920 :         L_tmp = L_mult( window_shb_32k_fx[i], hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i] );
    4937         920 :         output_HB[i] = mac_r( L_tmp, window_shb_32k_fx[2 * L_SHB_LAHEAD - 1 - i], syn_overlap_32k[i] );
    4938         920 :         move16();
    4939             :     }
    4940             : 
    4941             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    4942        4209 :     FOR( ; i < length; i++ )
    4943             :     {
    4944        4186 :         output_HB[i] = hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i];
    4945        4186 :         move16();
    4946             :     }
    4947             : 
    4948          23 :     IF( EQ_32( output_Fs, 48000 ) )
    4949             :     {
    4950          23 :         interpolate_3_over_2_allpass_fx( output_HB, length, output_HB, hBWE_TD->int_3_over_2_tbemem_dec_fx, allpass_poles_3_ov_2 );
    4951             :     }
    4952           0 :     ELSE IF( EQ_32( output_Fs, 16000 ) )
    4953             :     {
    4954           0 :         Decimate_allpass_steep_fx( output_HB, hBWE_TD->mem_resamp_HB_32k_fx, L_FRAME32k, output_HB );
    4955             :     }
    4956             : 
    4957          23 :     return;
    4958             : }
    4959             : 
    4960             : /* IVAS 32-bit variant */
    4961        5973 : void GenTransition_fx32(
    4962             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                      */
    4963             :     Word32 *outputHB_fx,       /* o  : synthesized HB transitions signal : Q11 */
    4964             :     const Word32 output_Fs,    /* i  : output sampling rate              : Q0  */
    4965             :     const Word16 L_frame,      /* i  : ACELP frame length                : Q0  */
    4966             :     const Word16 prev_Qx )
    4967             : {
    4968             :     Word16 i, length;
    4969             : 
    4970             :     Word32 syn_overlap_32k_fx[2 * SHB_OVERLAP_LEN];
    4971             : 
    4972             :     /* set targeted length of transition signal */
    4973        5973 :     length = i_mult( 2, NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ) );
    4974             : 
    4975             :     /* upsample overlap snippet */
    4976        5973 :     Interpolate_allpass_steep_fx32( hBWE_TD->syn_overlap_fx_32, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, SHB_OVERLAP_LEN, syn_overlap_32k_fx );
    4977             : 
    4978             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    4979        5973 :     test();
    4980        5973 :     IF( EQ_16( L_frame, L_FRAME ) )
    4981             :     {
    4982        2010 :         flip_and_downmix_generic_fx32( syn_overlap_32k_fx, syn_overlap_32k_fx, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ), &( hBWE_TD->syn_dm_phase ) );
    4983             :     }
    4984             :     ELSE
    4985             :     {
    4986      162483 :         FOR( i = 0; i < 2 * SHB_OVERLAP_LEN; i++ )
    4987             :         {
    4988      158520 :             IF( i % 2 == 0 )
    4989             :             {
    4990       79260 :                 syn_overlap_32k_fx[i] = L_negate( syn_overlap_32k_fx[i] );
    4991             :             }
    4992             :             ELSE
    4993             :             {
    4994       79260 :                 syn_overlap_32k_fx[i] = syn_overlap_32k_fx[i];
    4995             :             }
    4996      158520 :             move32();
    4997             :         }
    4998             :     }
    4999             : 
    5000             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    5001      244893 :     FOR( i = 0; i < 2 * L_SHB_LAHEAD; i++ )
    5002             :     {
    5003      238920 :         outputHB_fx[i] = L_add_sat( Mpy_32_16_1( hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i], window_shb_32k_fx[i] ), Mpy_32_16_1( syn_overlap_32k_fx[i], window_shb_32k_fx[2 * L_SHB_LAHEAD - 1 - i] ) );
    5004      238920 :         move32();
    5005             :     }
    5006             : 
    5007             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    5008      973031 :     FOR( ; i < length; i++ )
    5009             :     {
    5010      967058 :         outputHB_fx[i] = L_shl( hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i], sub( Q11, prev_Qx ) );
    5011      967058 :         move32();
    5012             :     }
    5013             : 
    5014        5973 :     IF( EQ_32( output_Fs, 48000 ) )
    5015             :     {
    5016        4467 :         interpolate_3_over_2_allpass_fx32( outputHB_fx, length, outputHB_fx, hBWE_TD->int_3_over_2_tbemem_dec_fx_32 );
    5017             :     }
    5018        1506 :     ELSE IF( EQ_32( output_Fs, 16000 ) )
    5019             :     {
    5020         116 :         Decimate_allpass_steep_fx32( outputHB_fx, hBWE_TD->mem_resamp_HB_32k_fx_32, L_FRAME32k, outputHB_fx );
    5021             :     }
    5022             : 
    5023        5973 :     return;
    5024             : }
    5025             : 
    5026             : 
    5027             : /*---------------------------------------------------------------------*
    5028             :  * GenTransition_WB()
    5029             :  *
    5030             :  *---------------------------------------------------------------------*/
    5031             : 
    5032           0 : void GenTransition_WB_fx(
    5033             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                   */
    5034             :     Word16 *output,            /* o  : synthesized transitions signal       */
    5035             :     const Word32 output_Fs     /* i  : output sampling rate                 */
    5036             : )
    5037             : {
    5038             :     Word16 i, length;
    5039             :     Word32 L_tmp;
    5040             :     Word16 speech_buf_16k1[L_FRAME16k], speech_buf_16k2[L_FRAME16k];
    5041             :     Word16 upsampled_synth[L_FRAME48k];
    5042             :     Word16 input_scaled[SHB_OVERLAP_LEN / 2];
    5043             : 
    5044             :     /* set targeted length of transition signal */
    5045           0 :     length = shl( NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ), 1 );
    5046             : 
    5047             :     /* upsample overlap snippet */
    5048           0 :     Copy_Scale_sig( hBWE_TD->syn_overlap_fx, input_scaled, SHB_OVERLAP_LEN / 2, hBWE_TD->prev_Qx );
    5049           0 :     Interpolate_allpass_steep_fx( input_scaled, hBWE_TD->state_lsyn_filt_shb_fx, SHB_OVERLAP_LEN / 2, speech_buf_16k1 );
    5050           0 :     Interpolate_allpass_steep_fx( speech_buf_16k1, hBWE_TD->state_lsyn_filt_dwn_shb_fx, SHB_OVERLAP_LEN, speech_buf_16k2 );
    5051             : 
    5052             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    5053           0 :     FOR( i = 0; i < SHB_OVERLAP_LEN; i += 2 )
    5054             :     {
    5055           0 :         speech_buf_16k2[i] = negate( speech_buf_16k2[i] );
    5056           0 :         speech_buf_16k2[i + 1] = speech_buf_16k2[i + 1];
    5057             :     }
    5058             : 
    5059             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    5060           0 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5061             :     {
    5062           0 :         L_tmp = L_mult( window_shb_fx[i], hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i] );
    5063           0 :         output[i] = mac_r( L_tmp, window_shb_fx[L_SHB_LAHEAD - 1 - i], speech_buf_16k2[i] );
    5064           0 :         move16();
    5065           0 :         output[i] = mult_r( output[i], 21299 );
    5066           0 :         move16();
    5067             :     }
    5068             : 
    5069             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    5070           0 :     FOR( ; i < length; i++ )
    5071             :     {
    5072           0 :         output[i] = mult_r( hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i], 21299 );
    5073           0 :         move16();
    5074             :     }
    5075             : 
    5076             :     /* upsampling if necessary */
    5077           0 :     IF( EQ_32( output_Fs, 32000 ) )
    5078             :     {
    5079           0 :         Interpolate_allpass_steep_fx( output, hBWE_TD->mem_resamp_HB_fx, L_FRAME16k, upsampled_synth );
    5080           0 :         Copy( upsampled_synth, output, L_FRAME32k );
    5081             :     }
    5082           0 :     ELSE IF( EQ_32( output_Fs, 48000 ) )
    5083             :     {
    5084           0 :         interpolate_3_over_1_allpass_fx( output, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
    5085           0 :         Copy( upsampled_synth, output, L_FRAME48k );
    5086             :     }
    5087             : 
    5088           0 :     return;
    5089             : }
    5090             : 
    5091             : 
    5092             : /* IVAS 32-bit variant */
    5093         106 : void GenTransition_WB_fx32(
    5094             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                       */
    5095             :     Word32 *outputHB_fx,       /* o  : synthesized HB transitions signal  : Q11 */
    5096             :     const Word32 output_Fs     /* i  : output sampling rate                     */
    5097             : )
    5098             : {
    5099             :     Word16 i, length;
    5100             :     Word32 speech_buf_16k1_fx[SHB_OVERLAP_LEN], speech_buf_16k2_fx[2 * SHB_OVERLAP_LEN];
    5101             :     Word32 upsampled_synth_fx[L_FRAME48k];
    5102             : 
    5103             :     /* set targeted length of transition signal */
    5104         106 :     length = i_mult( 2, NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ) );
    5105             : 
    5106             :     /* upsample overlap snippet */
    5107         106 :     Interpolate_allpass_steep_fx32( hBWE_TD->syn_overlap_fx_32, hBWE_TD->state_lsyn_filt_shb_fx_32, SHB_OVERLAP_LEN / 2, speech_buf_16k1_fx );
    5108         106 :     Interpolate_allpass_steep_fx32( speech_buf_16k1_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, SHB_OVERLAP_LEN, speech_buf_16k2_fx );
    5109             : 
    5110             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    5111        2226 :     FOR( i = 0; i < SHB_OVERLAP_LEN; i++ )
    5112             :     {
    5113        2120 :         IF( i % 2 == 0 )
    5114             :         {
    5115        1060 :             speech_buf_16k2_fx[i] = L_negate( speech_buf_16k2_fx[i] );
    5116        1060 :             move32();
    5117             :         }
    5118             :         ELSE
    5119             :         {
    5120        1060 :             speech_buf_16k2_fx[i] = speech_buf_16k2_fx[i];
    5121        1060 :             move32();
    5122             :         }
    5123             :     }
    5124             : 
    5125             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    5126        2226 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5127             :     {
    5128        2120 :         outputHB_fx[i] = L_add( Mpy_32_16_1( hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i], window_shb_fx[i] ), Mpy_32_16_1( speech_buf_16k2_fx[i], window_shb_fx[L_SHB_LAHEAD - 1 - i] ) );
    5129        2120 :         move32();
    5130        2120 :         outputHB_fx[i] = Mpy_32_16_1( outputHB_fx[i], 21299 );
    5131        2120 :         move32();
    5132             :     }
    5133             : 
    5134             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    5135       12564 :     FOR( ; i < length; i++ )
    5136             :     {
    5137       12458 :         outputHB_fx[i] = hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i];
    5138       12458 :         move32();
    5139       12458 :         outputHB_fx[i] = Mpy_32_16_1( outputHB_fx[i], 21299 );
    5140       12458 :         move32();
    5141             :     }
    5142             : 
    5143             :     /* upsampling if necessary */
    5144         106 :     IF( EQ_32( output_Fs, 32000 ) )
    5145             :     {
    5146           7 :         Interpolate_allpass_steep_fx32( outputHB_fx, hBWE_TD->mem_resamp_HB_fx_32, L_FRAME16k, upsampled_synth_fx );
    5147           7 :         Copy32( upsampled_synth_fx, outputHB_fx, L_FRAME32k );
    5148             :     }
    5149          99 :     ELSE IF( EQ_32( output_Fs, 48000 ) )
    5150             :     {
    5151          42 :         interpolate_3_over_1_allpass_fx32( outputHB_fx, L_FRAME16k, upsampled_synth_fx, hBWE_TD->mem_resamp_HB_fx_32 );
    5152          42 :         Copy32( upsampled_synth_fx, outputHB_fx, L_FRAME48k );
    5153             :     }
    5154             : 
    5155         106 :     return;
    5156             : }
    5157             : 
    5158             : 
    5159             : /*---------------------------------------------------------------------*
    5160             :  * TBEreset_dec()
    5161             :  *
    5162             :  *---------------------------------------------------------------------*/
    5163             : 
    5164      229840 : void TBEreset_dec_fx(
    5165             :     Decoder_State *st_fx /* i/o: decoder state structure                 */
    5166             : )
    5167             : {
    5168             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5169      229840 :     hBWE_TD = st_fx->hBWE_TD;
    5170             : 
    5171      229840 :     IF( NE_16( st_fx->last_core, ACELP_CORE ) )
    5172             :     {
    5173      228016 :         set16_fx( hBWE_TD->old_bwe_exc_fx, 0, PIT16k_MAX * 2 );
    5174      228016 :         hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
    5175      228016 :         move32();
    5176      228016 :         st_fx->prev_Q_bwe_exc = 31;
    5177      228016 :         move16();
    5178             :     }
    5179             : 
    5180      229840 :     test();
    5181      229840 :     IF( EQ_16( st_fx->bwidth, WB ) )
    5182             :     {
    5183       10361 :         wb_tbe_extras_reset_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx );
    5184       10361 :         wb_tbe_extras_reset_synth_fx( hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx, hBWE_TD->state_32and48k_WB_upsample_fx, hBWE_TD->mem_resamp_HB_fx );
    5185             : 
    5186       10361 :         set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 7 );
    5187       10361 :         set16_fx( hBWE_TD->state_lpc_syn_fx, 0, 10 );
    5188       10361 :         set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD / 4 );
    5189       10361 :         set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
    5190       10361 :         set32_fx( hBWE_TD->syn_overlap_fx_32, 0, L_SHB_LAHEAD );
    5191       10361 :         set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
    5192             :     }
    5193      219479 :     ELSE IF( EQ_16( st_fx->bwidth, SWB ) || EQ_16( st_fx->bwidth, FB ) )
    5194             :     {
    5195      219479 :         swb_tbe_reset_fx( hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    5196      219479 :                           hBWE_TD->syn_overlap_fx, hBWE_TD->state_syn_shbexc_fx, &( hBWE_TD->tbe_demph_fx ),
    5197      219479 :                           &( hBWE_TD->tbe_premph_fx ), hBWE_TD->mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ) );
    5198             : 
    5199      219479 :         swb_tbe_reset_synth_fx( hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32 );
    5200             : 
    5201      219479 :         set16_fx( hBWE_TD->GainShape_Delay_fx, 0, NUM_SHB_SUBFR / 2 );
    5202      219479 :         set16_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx, 0, INTERP_3_2_MEM_LEN );
    5203      219479 :         set32_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx_32, 0, INTERP_3_2_MEM_LEN );
    5204      219479 :         set16_fx( hBWE_TD->mem_resamp_HB_32k_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5205      219479 :         set32_fx( hBWE_TD->mem_resamp_HB_32k_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5206      219479 :         hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /* Q0 1.f */
    5207      219479 :         move32();
    5208      219479 :         hBWE_TD->prev_mix_factor_fx = 32767; /* Q15 1.f */
    5209      219479 :         move16();
    5210             : 
    5211      219479 :         IF( EQ_16( st_fx->bwidth, FB ) )
    5212             :         {
    5213      156031 :             if ( st_fx->hBWE_FD != NULL )
    5214             :             {
    5215      156031 :                 st_fx->hBWE_FD->prev_fb_ener_adjust_fx = 0;
    5216      156031 :                 move16();
    5217             :             }
    5218      156031 :             set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
    5219      156031 :             hBWE_TD->fb_tbe_demph_fx = 0;
    5220      156031 :             move16();
    5221      156031 :             fb_tbe_reset_synth_fx( hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, &hBWE_TD->prev_fbbwe_ratio_fx );
    5222             :         }
    5223             :     }
    5224             : 
    5225      229840 :     return;
    5226             : }
    5227             : 
    5228             : 
    5229             : /*-------------------------------------------------------------------*
    5230             :  * td_bwe_dec_init()
    5231             :  *
    5232             :  * Initialize TD BWE state structure at the decoder
    5233             :  *-------------------------------------------------------------------*/
    5234             : 
    5235        3257 : void td_bwe_dec_init_fx(
    5236             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
    5237             :     const Word16 extl,         /* i  :  BWE extension layer    */
    5238             :     const Word32 output_Fs     /* i  : output sampling rate    */
    5239             : )
    5240             : {
    5241             :     Word16 i;
    5242             : 
    5243             :     /* init. SHB buffers */;
    5244        3257 :     InitSWBdecBuffer_fx( hBWE_TD );
    5245             : 
    5246             :     /* reset SHB buffers */
    5247        3257 :     ResetSHBbuffer_Dec_fx( hBWE_TD, extl );
    5248             : 
    5249        3257 :     IF( EQ_32( output_Fs, 48000 ) )
    5250             :     {
    5251        2379 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[0], 0, 4 );
    5252        2379 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[1], 0, 4 );
    5253        2379 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[2], 0, 4 );
    5254        2379 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[3], 0, 4 );
    5255        2379 :         set16_fx( hBWE_TD->fbbwe_hpf_mem_fx_Q, 0, 4 );
    5256             :     }
    5257             : 
    5258        3257 :     set16_fx( hBWE_TD->mem_resamp_HB_fx, 0, INTERP_3_1_MEM_LEN );
    5259        3257 :     set32_fx( hBWE_TD->mem_resamp_HB_fx_32, 0, INTERP_3_1_MEM_LEN );
    5260        3257 :     set16_fx( hBWE_TD->mem_resamp_HB_32k_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5261        3257 :     set32_fx( hBWE_TD->mem_resamp_HB_32k_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5262             : 
    5263        3257 :     hBWE_TD->tilt_mem_fx = 0;
    5264        3257 :     move16();
    5265        3257 :     set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384 /*0.5f in Q15*/, LPC_SHB_ORDER - 2 );
    5266        3257 :     hBWE_TD->prev_tilt_para_fx = 0;
    5267        3257 :     move16();
    5268        3257 :     set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
    5269        3257 :     set16_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx, 0, INTERP_3_2_MEM_LEN );
    5270        3257 :     set32_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx_32, 0, INTERP_3_2_MEM_LEN );
    5271             : 
    5272             :     /* TD BWE post-processing */
    5273        3257 :     hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
    5274        3257 :     set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
    5275             : 
    5276       35827 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5277             :     {
    5278       32570 :         hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
    5279       32570 :         move16();
    5280             :     }
    5281             : 
    5282        3257 :     hBWE_TD->prev1_shb_ener_sf_fx = 32767; /* Q15*/
    5283        3257 :     move16();
    5284        3257 :     hBWE_TD->prev2_shb_ener_sf_fx = 32767; /* Q15*/
    5285        3257 :     move16();
    5286        3257 :     hBWE_TD->prev3_shb_ener_sf_fx = 32767; /* Q15*/
    5287        3257 :     move16();
    5288        3257 :     hBWE_TD->prev_res_shb_gshape_fx = 8192; /* 0.125 in Q14*/
    5289        3257 :     move16();
    5290        3257 :     hBWE_TD->prev_mixFactors_fx = 16384; /* 0.5 in Q15*/
    5291        3257 :     move16();
    5292        3257 :     hBWE_TD->prev_GainShape_fx = 0;
    5293        3257 :     move16();
    5294        3257 :     hBWE_TD->prev_Q_bwe_exc_fb = 51;
    5295        3257 :     move16();
    5296        3257 :     set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
    5297        3257 :     hBWE_TD->fb_tbe_demph_fx = 0;
    5298        3257 :     move16();
    5299             : 
    5300        3257 :     set16_fx( hBWE_TD->old_hb_synth_fx, 0, L_FRAME48k );
    5301             : 
    5302        3257 :     hBWE_TD->prev_ener_fx = L_deposit_l( 0 );
    5303        3257 :     move32();
    5304             : 
    5305        3257 :     set16_fx( hBWE_TD->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
    5306             : 
    5307        3257 :     return;
    5308             : }
    5309             : 
    5310             : /*-------------------------------------------------------------------*
    5311             :  * ResetSHBbuffer_Dec()
    5312             :  *
    5313             :  *
    5314             :  *-------------------------------------------------------------------*/
    5315             : 
    5316      104728 : static void calc_tilt_bwe_fx_loc(
    5317             :     const Word32 *sp_fx, /* i  : input signal   : Q11       */
    5318             :     Word32 *tilt_fx,     /* o  : signal tilt    : tilt_fx_q */
    5319             :     Word16 *tilt_fx_q,   /* o  : signal tilt                */
    5320             :     const Word16 N       /* i  : signal length  : Q0        */
    5321             : )
    5322             : {
    5323             :     Word16 i;
    5324             :     Word64 r0_fx, r1_fx;
    5325             : 
    5326      104728 :     r0_fx = EPSILON_FX_SMALL;
    5327      104728 :     move64();
    5328    26915096 :     FOR( i = 0; i < N; i++ )
    5329             :     {
    5330    26810368 :         r0_fx = W_add( r0_fx, W_shr( W_mult_32_32( sp_fx[i], sp_fx[i] ), 1 ) ); /*Q11*2 - 1*/
    5331             :     }
    5332      104728 :     r1_fx = W_deposit32_l( abs( L_sub( sp_fx[1], sp_fx[0] ) ) ); /*Q11*/
    5333    26705640 :     FOR( i = 2; i < N; i++ )
    5334             :     {
    5335    26600912 :         IF( W_mult_32_32( L_sub( sp_fx[i], sp_fx[i - 1] ) /*Q11*/, L_sub( sp_fx[i - 1], sp_fx[i - 2] ) /*Q11*/ ) /*Q11 * 2 - 1*/ < 0 )
    5336             :         {
    5337     6612735 :             r1_fx = W_add( r1_fx, W_deposit32_l( abs( L_sub( sp_fx[i], sp_fx[i - 1] ) ) ) ); /*Q11*/
    5338             :         }
    5339             :     }
    5340      104728 :     Word16 headroom_left_r0 = W_norm( r0_fx );
    5341      104728 :     Word16 headroom_left_r1 = W_norm( r1_fx );
    5342      104728 :     Word16 r0_q = 0, r1_q = 0;
    5343      104728 :     move16();
    5344      104728 :     move16();
    5345      104728 :     IF( LT_16( headroom_left_r0, 32 ) )
    5346             :     {
    5347      104569 :         r0_fx = W_shr( r0_fx, sub( 32, headroom_left_r0 ) );
    5348      104569 :         r0_q = sub( 31, sub( ( 2 * OUTPUT_Q ), sub( 32, headroom_left_r0 ) ) );
    5349             :     }
    5350             :     ELSE
    5351             :     {
    5352         159 :         r0_q = 31 - ( 2 * OUTPUT_Q );
    5353         159 :         move16();
    5354             :     }
    5355             : 
    5356      104728 :     IF( LT_16( headroom_left_r1, 32 ) )
    5357             :     {
    5358         681 :         r1_fx = W_shr( r1_fx, sub( 32, headroom_left_r1 ) );
    5359         681 :         r1_q = sub( OUTPUT_Q, sub( 32, headroom_left_r1 ) );
    5360             :     }
    5361             :     ELSE
    5362             :     {
    5363      104047 :         r1_q = OUTPUT_Q;
    5364      104047 :         move16();
    5365             :     }
    5366      104728 :     Word32 temp_r0_inv = ISqrt32( W_extract_l( r0_fx ), &r0_q );
    5367      104728 :     Word32 res = Mpy_32_32( W_extract_l( r1_fx ), temp_r0_inv );
    5368             :     // Word16 res_q = r1_q + ( r0_q < 0 ? ( 31 + ( -1 * r0_q ) ) : r0_q ) - 31;
    5369             :     Word16 res_q;
    5370      104728 :     IF( r0_q < 0 )
    5371             :     {
    5372      104656 :         res_q = add( r1_q, sub( add( 31, -r0_q ), 31 ) );
    5373             :     }
    5374             :     ELSE
    5375             :     {
    5376          72 :         res_q = add( r1_q, sub( r0_q, 31 ) );
    5377             :     }
    5378      104728 :     Word16 norm_res = norm_l( res );
    5379      104728 :     IF( norm_res > 0 )
    5380             :     {
    5381      104047 :         *tilt_fx = L_shl_sat( res, norm_res );
    5382      104047 :         move32();
    5383      104047 :         *tilt_fx_q = add( res_q, norm_res );
    5384      104047 :         move16();
    5385             :     }
    5386             :     ELSE
    5387             :     {
    5388         681 :         *tilt_fx = res;
    5389         681 :         move32();
    5390         681 :         *tilt_fx_q = res_q;
    5391         681 :         move16();
    5392             :     }
    5393      104728 :     return;
    5394             : }
    5395             : 
    5396             : /*-------------------------------------------------------------------*
    5397             :  * swb_tbe_dec()
    5398             :  *
    5399             :  * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
    5400             :  *-------------------------------------------------------------------*/
    5401       46084 : static void rescale_genSHB_mem_dec_ivas(
    5402             :     Decoder_State *st_fx,
    5403             :     Word16 sf /*Q0*/
    5404             : )
    5405             : {
    5406             :     Word16 i;
    5407             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5408       46084 :     hBWE_TD = st_fx->hBWE_TD;
    5409             : 
    5410      368672 :     FOR( i = 0; i < 7; i++ )
    5411             :     {
    5412      322588 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
    5413      322588 :         move16();
    5414             :     }
    5415             : 
    5416             :     /*  -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
    5417       46084 :     IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
    5418             :     {
    5419      407077 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5420             :         {
    5421      370070 :             hBWE_TD->state_lpc_syn_fx[i] = shl_sat( hBWE_TD->state_lpc_syn_fx[i], sf );
    5422      370070 :             move16();
    5423             :         }
    5424             : 
    5425      777147 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5426             :         {
    5427      740140 :             hBWE_TD->state_syn_shbexc_fx[i] = shl_sat( hBWE_TD->state_syn_shbexc_fx[i], sf );
    5428      740140 :             move16();
    5429             :         }
    5430             :     }
    5431             : 
    5432       46084 :     hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
    5433       46084 :     move32();
    5434             : 
    5435       46084 :     hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
    5436       46084 :     move16();
    5437       46084 :     hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
    5438       46084 :     move16();
    5439             : 
    5440       46084 :     return;
    5441             : }
    5442             : 
    5443      104728 : static void find_max_mem_dec_m3(
    5444             :     Decoder_State *st,
    5445             :     Word16 *n_mem3 )
    5446             : {
    5447             :     Word16 i;
    5448             :     // Word16 n_mem_32;
    5449             :     Word16 tempQ15;
    5450             :     Word16 max3;
    5451             :     // Word32 tempQ32, Lmax3;
    5452             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5453      104728 :     hBWE_TD = st->hBWE_TD;
    5454             : 
    5455             :     /* --------------------------------------------------------------*/
    5456             :     /* Find headroom for synthesis stage associated with these memories:
    5457             :           1. st->syn_overlap_fx*/
    5458      104728 :     max3 = 0;
    5459      104728 :     move16();
    5460             :     /* find max in prev overlapSyn */
    5461     2199288 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5462             :     {
    5463     2094560 :         tempQ15 = abs_s( hBWE_TD->syn_overlap_fx[i] );
    5464     2094560 :         max3 = s_max( max3, tempQ15 );
    5465             :     }
    5466             :     /* find max in prev genSHBsynth_state_lsyn_filt_shb_local_fx */
    5467             : 
    5468             :     /* estimate the norm for 16-bit memories  */
    5469      104728 :     *n_mem3 = norm_s( max3 );
    5470      104728 :     move16();
    5471      104728 :     if ( max3 == 0 )
    5472             :     {
    5473        8788 :         *n_mem3 = 15;
    5474        8788 :         move16();
    5475             :     }
    5476      104728 : }
    5477             : 
    5478             : /*-------------------------------------------------------------------*
    5479             :  * ivas_swb_tbe_dec_fx()
    5480             :  *
    5481             :  * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
    5482             :  *-------------------------------------------------------------------*/
    5483      104728 : void ivas_swb_tbe_dec_fx(
    5484             :     Decoder_State *st,                    /* i/o: decoder state structure                           */
    5485             :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure                            */
    5486             :     const Word32 *bwe_exc_extended_fx,    /* i  : bandwidth extended excitation : Q_exc             */
    5487             :     Word16 Q_exc,
    5488             :     const Word16 voice_factors_fx[],    /* i  : voicing factors  : Q15                              */
    5489             :     const Word32 old_syn_12k8_16k_fx[], /* i  : low band synthesis : old_syn_fx                     */
    5490             :     Word16 *White_exc16k_fx,            /* o  : shaped white excitation for the FB TBE : Q_white_exc*/
    5491             :     Word32 *synth_fx,                   /* o  : SHB synthesis/final synthesis  : Qx                 */
    5492             :     Word16 *pitch_buf_fx,               /* i  : Q6                                                  */
    5493             :     Word16 *Q_white_exc )
    5494             : {
    5495             :     Word16 i, j, cnt, n;
    5496             :     Word16 stemp;
    5497             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5498             :     Word32 shaped_shb_excitation_fx_32[L_FRAME16k + L_SHB_LAHEAD];
    5499             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    5500             :     Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
    5501             :     Word16 lsf_shb_fx[LPC_SHB_ORDER], lpc_shb_fx[LPC_SHB_ORDER + 1], GainShape_fx[NUM_SHB_SUBFR]; // Q12,Q12,Q15
    5502             :     Word32 GainFrame_fx;                                                                          // Q18
    5503             :     Word32 error_fx[L_FRAME32k];
    5504             :     Word16 ener_fx;
    5505             :     Word32 L_ener;
    5506             :     Word16 is_fractive;
    5507             :     Word32 prev_pow_fx, curr_pow_fx, Lscale;
    5508             :     Word16 scale_fx;
    5509             :     Word16 max_val, temp_fx, shaped_shb_excitation_frac[L_FRAME16k + L_SHB_LAHEAD];
    5510             :     Word32 curr_frame_pow_fx;
    5511             :     Word16 curr_frame_pow_exp;
    5512             :     Word32 L_prev_ener_shb;
    5513             :     Word16 vf_modified_fx[NB_SUBFR16k];
    5514             :     Word16 f_fx, inc_fx;
    5515             :     Word32 GainFrame_prevfrm_fx;
    5516             :     Word32 tilt_swb_fec_32_fx;
    5517             :     Word16 tilt_swb_fec_fx_q;
    5518             :     Word16 tilt_swb_fec_fx;
    5519      104728 :     Word32 prev_ener_ratio_fx = 0; /* initialize just to avoid compiler warning */
    5520             :     Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
    5521             :     Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
    5522             :     const Word16 *ptr_lsp_interp_coef_fx;
    5523             :     Word32 shb_ener_sf_32;
    5524             :     Word16 shb_res_gshape_fx[NB_SUBFR16k];
    5525             :     Word16 mixFactors_fx;
    5526             :     Word16 vind;
    5527             :     Word16 shb_res_dummy_fx[L_FRAME16k];
    5528             :     Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
    5529             :     Word32 ener_tmp_fx[NUM_SHB_SUBGAINS];
    5530             :     Word16 GainShape_tmp_fx[NUM_SHB_SUBGAINS];
    5531             :     Word16 pitch_fx;
    5532             :     Word16 l_subframe;
    5533             :     Word16 formant_fac_fx;
    5534             :     Word16 synth_scale_fx;
    5535             :     Word16 lsf_diff_fx[LPC_SHB_ORDER], w_fx[LPC_SHB_ORDER];
    5536             :     Word16 refl_fx[M];
    5537             :     Word16 tilt_para_fx;
    5538             :     Word16 *nlExc16k_fx, *mixExc16k_fx;
    5539             :     Word16 MSFlag;
    5540             :     Word16 feedback_fx;
    5541             :     Word16 GainShape_tilt_fx;
    5542             : 
    5543             :     // scaling
    5544             :     Word16 exp, tmp;
    5545             :     Word16 tmp1, tmp2;
    5546             :     Word16 mean_vf;
    5547             :     Word32 Lmax, L_tmp;
    5548             :     Word16 frac;
    5549             :     Word32 L_tmp1, L_tmp2;
    5550             :     Word16 Q_bwe_exc;
    5551             :     Word16 Q_bwe_exc_fb;
    5552             :     Word16 Q_shb;
    5553             :     Word16 n_mem, n_mem2, n_mem3, Qx, sc;
    5554             :     Word16 expa, expb;
    5555             :     Word16 fraca, fracb;
    5556             :     Word16 exp_ener, inv_ener;
    5557             : 
    5558      104728 :     hBWE_TD = st->hBWE_TD;
    5559             : 
    5560             :     /* initializations */
    5561      104728 :     GainFrame_fx = 0;
    5562      104728 :     move32();
    5563      104728 :     mixFactors_fx = 0;
    5564      104728 :     move16();
    5565      104728 :     shb_ener_sf_32 = 0;
    5566      104728 :     move32();
    5567      104728 :     set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
    5568      104728 :     if ( st->hTdCngDec != NULL )
    5569             :     {
    5570       76292 :         st->hTdCngDec->shb_dtx_count_fx = 0;
    5571       76292 :         move16();
    5572             :     }
    5573      104728 :     is_fractive = 0;
    5574      104728 :     move16();
    5575             : 
    5576      104728 :     IF( hStereoICBWE != NULL )
    5577             :     {
    5578       16532 :         nlExc16k_fx = hStereoICBWE->nlExc16k_fx;
    5579       16532 :         mixExc16k_fx = hStereoICBWE->mixExc16k_fx;
    5580       16532 :         MSFlag = hStereoICBWE->MSFlag;
    5581       16532 :         move16();
    5582             :     }
    5583             :     ELSE
    5584             :     {
    5585       88196 :         nlExc16k_fx = NULL;
    5586       88196 :         mixExc16k_fx = NULL;
    5587       88196 :         MSFlag = 0;
    5588       88196 :         move16();
    5589             :     }
    5590             : 
    5591             :     /* find tilt */
    5592      104728 :     calc_tilt_bwe_fx_loc( old_syn_12k8_16k_fx, &tilt_swb_fec_32_fx, &tilt_swb_fec_fx_q, L_FRAME );
    5593      104728 :     tilt_swb_fec_fx = round_fx_sat( L_shl_sat( tilt_swb_fec_32_fx, sub( 11 + 16, tilt_swb_fec_fx_q ) ) );
    5594      104728 :     test();
    5595      104728 :     if ( st->bfi && st->clas_dec != UNVOICED_CLAS )
    5596             :     {
    5597        1717 :         tilt_swb_fec_fx = hBWE_TD->tilt_swb_fec_fx;
    5598        1717 :         move16();
    5599             :     }
    5600             : 
    5601             :     /* WB/SWB bandwidth switching */
    5602      104728 :     test();
    5603      104728 :     test();
    5604      104728 :     IF( ( GT_16( st->tilt_wb_fx, 10240 /*5 in Q11*/ ) && ( EQ_16( st->clas_dec, UNVOICED_CLAS ) ) ) || GT_16( st->tilt_wb_fx, 20480 /*10 in Q11*/ ) )
    5605             :     {
    5606        7380 :         test();
    5607        7380 :         test();
    5608        7380 :         test();
    5609        7380 :         test();
    5610        7380 :         test();
    5611        7380 :         test();
    5612        7380 :         test();
    5613        7380 :         test();
    5614        7380 :         IF( ( ( st->prev_fractive == 0 ) &&
    5615             :               ( LT_32( st->prev_enerLH_fx, L_shl( st->enerLH_fx, 1 ) ) && GT_32( st->prev_enerLH_fx, L_shr( st->enerLH_fx, 1 ) ) && LT_32( st->prev_enerLL_fx, L_shl( st->enerLL_fx, 1 ) ) && GT_32( st->prev_enerLL_fx, L_shr( st->enerLL_fx, 1 ) ) ) ) ||
    5616             :             ( EQ_16( st->prev_fractive, 1 ) &&
    5617             :               GT_32( L_shr( st->prev_enerLH_fx, 2 ), Mult_32_16( st->enerLH_fx, 24576 ) ) ) /* 24576 in Q13*/
    5618             :             || ( GT_32( L_shr( st->enerLL_fx, 1 ), Mult_32_16( st->enerLH_fx, 24576 ) ) &&  /*24576 = 1.5 in Q14*/
    5619             :                  LT_16( st->tilt_wb_fx, 20480 ) )                                           /* 20480 = 10 in Q11*/
    5620             :         )
    5621             :         {
    5622           0 :             is_fractive = 0;
    5623             :         }
    5624             :         ELSE
    5625             :         {
    5626        7380 :             is_fractive = 1;
    5627             :         }
    5628        7380 :         move16();
    5629             :     }
    5630             : 
    5631             :     /* WB/SWB bandwidth switching */
    5632      104728 :     IF( st->bws_cnt > 0 )
    5633             :     {
    5634          12 :         f_fx = 1489; /*1.0f / 22.0f in Q15*/
    5635          12 :         move16();
    5636          12 :         inc_fx = 1489; /*1.0f / 22.0f in Q15*/
    5637          12 :         move16();
    5638             : 
    5639          12 :         IF( EQ_16( is_fractive, 1 ) )
    5640             :         {
    5641           0 :             Copy( lsf_tab_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    5642             :         }
    5643             :         ELSE
    5644             :         {
    5645         132 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5646             :             {
    5647         120 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
    5648         120 :                 move16();
    5649         120 :                 f_fx = add( f_fx, inc_fx ); /*Q15*/
    5650             :             }
    5651             :         }
    5652          12 :         test();
    5653          12 :         test();
    5654          12 :         test();
    5655          12 :         test();
    5656          12 :         test();
    5657          12 :         test();
    5658          12 :         test();
    5659          12 :         IF( ( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) && !( ( L_sub( L_shr( st->prev_enerLH_fx, 1 ), st->enerLH_fx ) < 0 ) && L_sub( st->prev_enerLH_fx, ( L_shr( st->enerLH_fx, 1 ) > 0 ) ) ) ) || st->last_core != ACELP_CORE || ( ( st->last_core == ACELP_CORE ) && GT_32( abs( L_sub( st->last_core_brate, st->core_brate ) ), 3600 ) ) || EQ_16( s_xor( is_fractive, st->prev_fractive ), 1 ) )
    5660             :         {
    5661          12 :             set16_fx( GainShape_fx, 11587, NUM_SHB_SUBFR ); /*0.3536f in Q15*/
    5662             :         }
    5663             :         ELSE
    5664             :         {
    5665           0 :             if ( GT_16( hBWE_TD->prev_GainShape_fx, 11587 ) ) /*0.3536f in Q15*/
    5666             :             {
    5667           0 :                 hBWE_TD->prev_GainShape_fx = 11587; /*0.3536f in Q15*/
    5668           0 :                 move16();
    5669             :             }
    5670           0 :             set16_fx( GainShape_fx, hBWE_TD->prev_GainShape_fx, NUM_SHB_SUBFR );
    5671             :         }
    5672             : 
    5673          12 :         Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
    5674          12 :         set16_fx( shb_res_gshape_fx, 3277 /*0.2f Q14*/, NB_SUBFR16k ); /* Q14 */
    5675             :     }
    5676             :     ELSE
    5677             :     {
    5678      104716 :         test();
    5679      104716 :         IF( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) )
    5680             :         {
    5681        6620 :             f_fx = 1489; /*Q15*/
    5682        6620 :             move16();
    5683        6620 :             inc_fx = 1489; /*Q15*/
    5684        6620 :             move16();
    5685       72820 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5686             :             {
    5687       66200 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
    5688       66200 :                 move16();
    5689       66200 :                 f_fx = add( f_fx, inc_fx );
    5690             :             }
    5691             :         }
    5692             : 
    5693      104716 :         IF( !st->bfi )
    5694             :         {
    5695      102585 :             IF( st->use_partial_copy )
    5696             :             {
    5697           0 :                 IF( NE_16( st->last_extl, SWB_TBE ) )
    5698             :                 {
    5699           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
    5700           0 :                     move32();
    5701           0 :                     f_fx = 1489 /*0.045454f Q15*/;
    5702           0 :                     move16();
    5703           0 :                     inc_fx = 1489 /*0.045454f Q15*/;
    5704           0 :                     move16();
    5705           0 :                     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5706             :                     {
    5707           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
    5708           0 :                         move16();
    5709           0 :                         f_fx = add( f_fx, inc_fx ); /*Q15*/
    5710             :                     }
    5711             :                 }
    5712           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
    5713           0 :                 set16_fx( GainShape_fx, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR );
    5714             : 
    5715           0 :                 IF( EQ_16( st->rf_frame_type, RF_NELP ) )
    5716             :                 {
    5717             :                     /* Frame gain */
    5718             : 
    5719           0 :                     GainFrame_fx = L_mac( SHB_GAIN_QLOW_FX, st->rf_indx_tbeGainFr, SHB_GAIN_QDELTA_FX );
    5720           0 :                     L_tmp = Mult_32_16( GainFrame_fx, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    5721             : 
    5722           0 :                     frac = L_Extract_lc( L_tmp, &exp );
    5723           0 :                     L_tmp = Pow2( 30, frac );
    5724           0 :                     GainFrame_fx = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    5725             : 
    5726           0 :                     test();
    5727           0 :                     test();
    5728           0 :                     test();
    5729           0 :                     test();
    5730           0 :                     test();
    5731           0 :                     test();
    5732           0 :                     IF( ( st->core == ACELP_CORE ) && ( st->last_core == ACELP_CORE ) && !st->prev_use_partial_copy && EQ_16( st->prev_coder_type, UNVOICED ) && NE_32( GainFrame_fx, hBWE_TD->GainFrame_prevfrm_fx ) && NE_16( st->next_coder_type, GENERIC ) && EQ_16( st->last_extl, SWB_TBE ) )
    5733             :                     {
    5734           0 :                         GainFrame_fx = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame_fx, 6553 /*0.2f in Q15*/ ) );
    5735             :                     }
    5736             :                 }
    5737             :                 ELSE
    5738             :                 {
    5739           0 :                     temp_fx = 0;
    5740           0 :                     move16();
    5741             :                     /* Frame gain */
    5742           0 :                     SWITCH( st->rf_indx_tbeGainFr )
    5743             :                     {
    5744           0 :                         case 0:
    5745           0 :                             GainFrame_fx = 131072; /* 0.5f in Q18 */
    5746           0 :                             move32();
    5747           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
    5748             :                             {
    5749           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5750           0 :                                 move16();
    5751             :                             }
    5752           0 :                             BREAK;
    5753           0 :                         case 1:
    5754           0 :                             GainFrame_fx = 524288; /* 2.0f in Q18 */
    5755           0 :                             move32();
    5756           0 :                             test();
    5757           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
    5758             :                             {
    5759           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5760           0 :                                 move16();
    5761             :                             }
    5762           0 :                             BREAK;
    5763           0 :                         case 2:
    5764           0 :                             GainFrame_fx = 1048576; /* 4.0f in Q18 */
    5765           0 :                             move32();
    5766           0 :                             test();
    5767           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
    5768             :                             {
    5769           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5770           0 :                                 move16();
    5771             :                             }
    5772           0 :                             BREAK;
    5773           0 :                         case 3:
    5774           0 :                             GainFrame_fx = 2097152; /* 8.0f in Q18 */
    5775           0 :                             move32();
    5776           0 :                             test();
    5777           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16Q18*/ ) )
    5778             :                             {
    5779           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5780           0 :                                 move16();
    5781             :                             }
    5782           0 :                             BREAK;
    5783           0 :                         default:
    5784           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
    5785             :                     }
    5786             : 
    5787           0 :                     IF( EQ_16( st->last_extl, SWB_TBE ) )
    5788             :                     {
    5789           0 :                         GainFrame_fx = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp_fx ), Mult_32_16( GainFrame_fx, sub( 32767, temp_fx ) ) );
    5790             :                         /*Q18*/
    5791             :                     }
    5792             : 
    5793           0 :                     IF( ( st->core == ACELP_CORE ) && ( st->last_core == ACELP_CORE ) )
    5794             :                     {
    5795           0 :                         if ( !st->prev_use_partial_copy && EQ_16( st->last_coder_type, VOICED ) && EQ_16( st->rf_frame_type, RF_GENPRED ) && GT_32( GainFrame_fx, 2097152 /*8.0f in Q18*/ ) && LT_32( GainFrame_fx, 3059606 /*11.67f in Q18*/ ) )
    5796             :                         {
    5797           0 :                             GainFrame_fx = Mult_32_16( GainFrame_fx, 9830 /*0.3f in Q15*/ ); // Q18
    5798             :                         }
    5799             :                     }
    5800             :                 }
    5801             :             }
    5802             :             ELSE
    5803             :             {
    5804             :                 /* de-quantization */
    5805      102585 :                 ivas_dequantizeSHBparams_fx_9_1( st, st->extl, st->extl_brate, lsf_shb_fx, GainShape_fx, &GainFrame_fx, &stemp,
    5806             :                                                  &shb_ener_sf_32, shb_res_gshape_fx, &mixFactors_fx, &MSFlag );
    5807      102585 :                 if ( hStereoICBWE != NULL )
    5808             :                 {
    5809       16283 :                     hStereoICBWE->MSFlag = MSFlag;
    5810       16283 :                     move16();
    5811             :                 }
    5812             :             }
    5813             :         }
    5814             :         ELSE
    5815             :         {
    5816        2131 :             Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
    5817        2131 :             test();
    5818        2131 :             IF( EQ_16( st->codec_mode, MODE1 ) && ( st->element_mode == EVS_MONO ) )
    5819             :             {
    5820           0 :                 gradientGainShape( st, GainShape_fx, &GainFrame_fx );
    5821             :             }
    5822             :             ELSE
    5823             :             {
    5824       10655 :                 FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5825             :                 {
    5826       42620 :                     FOR( j = 0; j < 4; j++ )
    5827             :                     {
    5828       34096 :                         GainShape_fx[i * 4 + j] = mult_r( st->cummulative_damping, hBWE_TD->GainShape_Delay_fx[4 + i] );
    5829       34096 :                         move16();
    5830             :                     }
    5831             :                 }
    5832        2131 :                 IF( GT_16( tilt_swb_fec_fx, ( 8 << 11 ) ) ) /* tilt_swb_fec_fx in Q11 */
    5833             :                 {
    5834          46 :                     IF( EQ_16( st->nbLostCmpt, 1 ) )
    5835             :                     {
    5836          41 :                         GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 19661 /*0.6f Q15*/ );
    5837             :                     }
    5838           5 :                     ELSE IF( EQ_16( st->nbLostCmpt, 2 ) )
    5839             :                     {
    5840           5 :                         GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 11469 /*0.35f Q15*/ );
    5841             :                     }
    5842             :                     ELSE
    5843             :                     {
    5844           0 :                         GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 6554 /*0.2f Q15*/ );
    5845             :                     }
    5846          46 :                     GainFrame_fx = Mult_32_16( GainFrame_fx, st->cummulative_damping );
    5847             :                 }
    5848             :                 ELSE
    5849             :                 {
    5850        2085 :                     GainFrame_fx = hBWE_TD->GainFrame_prevfrm_fx;
    5851        2085 :                     move32(); /* gain locking */
    5852             :                 }
    5853             :             }
    5854             : 
    5855        2131 :             IF( GE_32( st->extl_brate, SWB_TBE_2k8 ) )
    5856             :             {
    5857         690 :                 test();
    5858         690 :                 IF( EQ_16( st->codec_mode, MODE1 ) && ( st->element_mode == EVS_MONO ) )
    5859             :                 {
    5860           0 :                     L_tmp = L_mult( extract_l( hBWE_TD->prev2_shb_ener_sf_fx ), extract_l( hBWE_TD->prev3_shb_ener_sf_fx ) ); /*Q1*/
    5861           0 :                     tmp = round_fx( root_a_fx( L_tmp, 1, &exp ) );                                                            /* Q = 15-exp */
    5862           0 :                     tmp1 = extract_l( hBWE_TD->prev1_shb_ener_sf_fx );                                                        /*Q0*/
    5863           0 :                     i = sub( norm_s( tmp1 ), 1 );
    5864           0 :                     tmp1 = shl( tmp1, i ); /* Qi */
    5865           0 :                     IF( tmp == 0 )
    5866             :                     {
    5867           0 :                         tmp = 32767 /*1.0f Q15*/;
    5868           0 :                         move16(); /*Q15*/
    5869             :                     }
    5870             :                     ELSE
    5871             :                     {
    5872           0 :                         scale_fx = div_s( tmp1, tmp ); /* Q15 - Q(15-exp) + Qi  = Qexp+i */
    5873           0 :                         scale_fx = s_max( scale_fx, 0 );
    5874           0 :                         tmp = shl_sat( scale_fx, sub( sub( 15, exp ), i ) ); /*Q15*/
    5875             :                     }
    5876           0 :                     scale_fx = mult_r( hBWE_TD->prev_res_shb_gshape_fx, tmp ); /* Q14 */
    5877           0 :                     test();
    5878           0 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    5879             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    5880             :                     {
    5881           0 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, scale_fx );
    5882             : 
    5883           0 :                         if ( GT_16( st->nbLostCmpt, 1 ) )
    5884             :                         {
    5885           0 :                             shb_ener_sf_32 = L_shr( shb_ener_sf_32, 1 );
    5886             :                         }
    5887             :                     }
    5888             :                     ELSE
    5889             :                     {
    5890           0 :                         L_tmp = L_mult( scale_fx, scale_fx ); /* Q29 */
    5891           0 :                         shb_ener_sf_32 = L_shl( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, round_fx( L_tmp ) ), 2 );
    5892             :                     }
    5893             :                 }
    5894             :                 ELSE
    5895             :                 {
    5896         690 :                     test();
    5897         690 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    5898             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    5899             :                     {
    5900          60 :                         shb_ener_sf_32 = L_shr( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st->cummulative_damping ), 1 );
    5901             :                     }
    5902             :                     ELSE
    5903             :                     {
    5904         630 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st->cummulative_damping );
    5905             :                     }
    5906             :                 }
    5907             :             }
    5908             : 
    5909        2131 :             shb_ener_sf_32 = L_max( shb_ener_sf_32, 1l /*1.0f Q0*/ );
    5910        2131 :             mixFactors_fx = hBWE_TD->prev_mixFactors_fx;
    5911        2131 :             move16();
    5912             : 
    5913        2131 :             IF( EQ_16( st->codec_mode, MODE1 ) )
    5914             :             {
    5915        2131 :                 set16_fx( shb_res_gshape_fx, 3277 /*0.2f Q14*/, 5 ); /* Q14 */
    5916             :             }
    5917             :             ELSE
    5918             :             {
    5919           0 :                 set16_fx( shb_res_gshape_fx, 16384 /*1.0f Q14*/, 5 ); /* Q14 */
    5920             :             }
    5921             :         }
    5922             :     }
    5923             : 
    5924             :     /* get the gainshape delay */
    5925      104728 :     Copy( &hBWE_TD->GainShape_Delay_fx[4], &hBWE_TD->GainShape_Delay_fx[0], NUM_SHB_SUBFR / 4 );
    5926      523640 :     FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5927             :     {
    5928      418912 :         hBWE_TD->GainShape_Delay_fx[i + 4] = GainShape_fx[i * 4]; /*Q15*/
    5929      418912 :         move16();
    5930             :     }
    5931             : 
    5932      104728 :     L_tmp = L_mult( voice_factors_fx[0], 8192 );
    5933      104728 :     L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
    5934      104728 :     L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
    5935      104728 :     mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
    5936             : 
    5937      104728 :     Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
    5938             : 
    5939      104728 :     test();
    5940      104728 :     IF( EQ_16( st->coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
    5941             :     {
    5942      102840 :         FOR( i = 1; i < NB_SUBFR; i++ )
    5943             :         {
    5944       77130 :             L_tmp = L_mult( voice_factors_fx[i], 26214 /*0.8f Q15*/ );
    5945       77130 :             vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 /*0.2f Q15*/ );
    5946       77130 :             move16();
    5947             :         }
    5948             : 
    5949       25710 :         IF( st->L_frame != L_FRAME )
    5950             :         {
    5951       14528 :             L_tmp = L_mult( voice_factors_fx[4], 26214 /*0.8f Q15*/ );
    5952       14528 :             vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 /*0.2f Q15*/ );
    5953       14528 :             move16();
    5954             :         }
    5955             :     }
    5956             : 
    5957      104728 :     test();
    5958      104728 :     IF( st->use_partial_copy && st->nelp_mode_dec )
    5959             :     {
    5960           0 :         set16_fx( vf_modified_fx, 0, NB_SUBFR16k );
    5961             :     }
    5962             : 
    5963             :     /* SHB LSF from current frame; and convert to LSP for interpolation */
    5964      104728 :     E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
    5965             : 
    5966      104728 :     test();
    5967      104728 :     IF( EQ_16( st->last_extl, SWB_TBE ) || EQ_16( st->last_extl, FB_TBE ) )
    5968             :     {
    5969             :         /* SHB LSP values from prev. frame for interpolation */
    5970       98096 :         Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    5971             :     }
    5972             :     ELSE
    5973             :     {
    5974             :         /* Use current frame's LSPs; in effect no interpolation */
    5975        6632 :         Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    5976             :     }
    5977             : 
    5978      104728 :     test();
    5979      104728 :     test();
    5980      104728 :     test();
    5981      104728 :     IF( ( st->bws_cnt == 0 ) && ( st->bws_cnt1 == 0 ) && ( st->prev_use_partial_copy == 0 ) && ( st->use_partial_copy == 0 ) )
    5982             :     {
    5983      104430 :         lsf_diff_fx[0] = 16384;
    5984      104430 :         move16(); /*Q15*/
    5985      104430 :         lsf_diff_fx[LPC_SHB_ORDER - 1] = 16384;
    5986      104430 :         move16(); /*Q15*/
    5987      939870 :         FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    5988             :         {
    5989      835440 :             lsf_diff_fx[i] = sub( lsf_shb_fx[i], lsf_shb_fx[i - 1] );
    5990      835440 :             move16();
    5991             :         }
    5992             : 
    5993      104430 :         a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl_fx, M );
    5994      104430 :         tmp = add( 16384, shr( refl_fx[0], 1 ) ); /*Q14*/
    5995      104430 :         tmp1 = mult( 27425, tmp );
    5996      104430 :         tmp1 = mult( tmp1, tmp );                      /*Q10*/
    5997      104430 :         tmp2 = shr( mult( 31715, tmp ), 2 );           /*Q10*/
    5998      104430 :         tilt_para_fx = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
    5999             : 
    6000      104430 :         test();
    6001      104430 :         IF( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) )
    6002             :         {
    6003       58626 :             FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    6004             :             {
    6005       52112 :                 hBWE_TD->prev_lsf_diff_fx[i - 1] = shr( lsf_diff_fx[i], 1 );
    6006       52112 :                 move16();
    6007             :             }
    6008             :         }
    6009             : 
    6010      104430 :         IF( LE_32( st->extl_brate, FB_TBE_1k8 ) )
    6011             :         {
    6012       81737 :             test();
    6013       81737 :             test();
    6014       81737 :             test();
    6015       81737 :             test();
    6016       81737 :             test();
    6017       81737 :             IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( st->coder_type, TRANSITION ) || LT_16( tilt_para_fx, 1024 ) ) ) &&
    6018             :                 !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( st->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para_fx, 5120 ) ) )
    6019             :             {
    6020      717921 :                 FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    6021             :                 {
    6022      638152 :                     IF( LT_16( lsf_diff_fx[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
    6023             :                     {
    6024      259613 :                         tmp = mult( 26214, lsf_diff_fx[i] );
    6025             : 
    6026      259613 :                         test();
    6027      259613 :                         IF( ( hBWE_TD->prev_lsf_diff_fx[i - 1] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    6028             :                         {
    6029           0 :                             st->BER_detect = 1;
    6030           0 :                             move16();
    6031           0 :                             tmp = 0;
    6032           0 :                             move16();
    6033             :                         }
    6034             :                         ELSE
    6035             :                         {
    6036      259613 :                             tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    6037             :                         }
    6038             : 
    6039      259613 :                         tmp = s_max( tmp, 16384 );
    6040      259613 :                         w_fx[i] = s_min( tmp, 32767 );
    6041      259613 :                         move16();
    6042             :                     }
    6043             :                     ELSE
    6044             :                     {
    6045      378539 :                         tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    6046             : 
    6047      378539 :                         test();
    6048      378539 :                         IF( ( lsf_diff_fx[i] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    6049             :                         {
    6050           0 :                             st->BER_detect = 1;
    6051           0 :                             move16();
    6052           0 :                             tmp = 0;
    6053           0 :                             move16();
    6054             :                         }
    6055             :                         ELSE
    6056             :                         {
    6057      378539 :                             tmp = div_s( tmp, lsf_diff_fx[i] );
    6058             :                         }
    6059             : 
    6060      378539 :                         tmp = s_max( tmp, 16384 );
    6061      378539 :                         w_fx[i] = s_min( tmp, 32767 );
    6062      378539 :                         move16();
    6063             :                     }
    6064             :                 }
    6065       79769 :                 w_fx[0] = w_fx[1];
    6066       79769 :                 move16();
    6067       79769 :                 w_fx[LPC_SHB_ORDER - 1] = w_fx[LPC_SHB_ORDER - 2];
    6068       79769 :                 move16();
    6069             : 
    6070      877459 :                 FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    6071             :                 {
    6072      797690 :                     tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w_fx[i] ) );
    6073      797690 :                     tmp2 = mult( lsp_shb_2_fx[i], w_fx[i] );
    6074      797690 :                     lsp_temp_fx[i] = add( tmp1, tmp2 );
    6075      797690 :                     move16();
    6076             :                 }
    6077             :             }
    6078             :             ELSE
    6079             :             {
    6080        1968 :                 Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
    6081             :             }
    6082             : 
    6083             :             /* convert from lsp to lsf */
    6084       81737 :             lsp2lsf_fx( lsp_temp_fx, lsf_shb_fx, LPC_SHB_ORDER, 1 );
    6085             :         }
    6086             : 
    6087      104430 :         Copy( lsf_diff_fx + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
    6088      104430 :         hBWE_TD->prev_tilt_para_fx = tilt_para_fx;
    6089      104430 :         move16();
    6090             :     }
    6091             :     ELSE
    6092             :     {
    6093         298 :         Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
    6094             :     }
    6095             : 
    6096      104728 :     IF( GE_32( st->extl_brate, SWB_TBE_2k8 ) )
    6097             :     {
    6098             :         /* SHB LSP interpolation */
    6099       22740 :         ptr_lsp_interp_coef_fx = interpol_frac_shb; /*Q15*/
    6100      113700 :         FOR( j = 0; j < 4; j++ )
    6101             :         {
    6102     1000560 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    6103             :             {
    6104      909600 :                 L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
    6105      909600 :                 lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
    6106      909600 :                 move16();
    6107             :             }
    6108       90960 :             ptr_lsp_interp_coef_fx += 2;
    6109             : 
    6110       90960 :             tmp = i_mult( j, ( LPC_SHB_ORDER + 1 ) );
    6111             :             /* convert LSPs to LP coefficients */
    6112       90960 :             E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
    6113             :         }
    6114             :     }
    6115             : 
    6116             :     /* Save the SWB LSP values from current frame for interpolation */
    6117      104728 :     Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER );
    6118             : 
    6119             :     /* save the shb_ener and mixFactor values */
    6120      104728 :     hBWE_TD->prev3_shb_ener_sf_fx = hBWE_TD->prev2_shb_ener_sf_fx;
    6121      104728 :     move32();
    6122      104728 :     hBWE_TD->prev2_shb_ener_sf_fx = hBWE_TD->prev1_shb_ener_sf_fx;
    6123      104728 :     move32();
    6124      104728 :     hBWE_TD->prev1_shb_ener_sf_fx = shb_ener_sf_32;
    6125      104728 :     move32();
    6126      104728 :     hBWE_TD->prev_res_shb_gshape_fx = shb_res_gshape_fx[4];
    6127      104728 :     move16();
    6128      104728 :     hBWE_TD->prev_mixFactors_fx = mixFactors_fx;
    6129      104728 :     move16();
    6130             : 
    6131             :     /* SWB CNG/DTX - update memories */
    6132      104728 :     IF( st->hTdCngDec != NULL )
    6133             :     {
    6134       76292 :         Copy( st->hTdCngDec->lsp_shb_prev_fx, st->hTdCngDec->lsp_shb_prev_prev_fx, LPC_SHB_ORDER );
    6135       76292 :         Copy( lsf_shb_fx, st->hTdCngDec->lsp_shb_prev_fx, LPC_SHB_ORDER );
    6136             :     }
    6137             : 
    6138             :     /* convert LSPs back into LP coeffs */
    6139      104728 :     E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
    6140      104728 :     Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
    6141      104728 :     lpc_shb_fx[0] = ONE_IN_Q12;
    6142      104728 :     move16();
    6143             : 
    6144      104728 :     test();
    6145      104728 :     IF( EQ_32( st->extl_brate, SWB_TBE_1k10 ) || EQ_32( st->extl_brate, SWB_TBE_1k75 ) )
    6146        4171 :     {
    6147        4171 :         Word32 vind_temp = Mpy_32_32( L_shl( L_add( L_deposit_l( mixFactors_fx ), 1 ), 15 ), ( ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) << 16 ) ); // check addition of 1
    6148        4171 :         vind = extract_l( L_shr( vind_temp, 15 ) ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */                                         /*mixFactors*7*/
    6149             :         /* i: mixFactors_fx in Q15 */
    6150             :         /* o: vind in Q0        */
    6151             :     }
    6152             :     ELSE
    6153             :     {
    6154      100557 :         vind = shl( mixFactors_fx, 3 - 15 ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */ /*mixFactors*8*/
    6155             :         /* i: mixFactors_fx in Q15 */
    6156             :         /* o: vind in Q0        */
    6157             :     }
    6158             : 
    6159             :     /* Determine formant PF strength */
    6160      104728 :     formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
    6161             :     /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
    6162      104728 :     IF( GT_32( st->total_brate, ACELP_32k ) )
    6163             :     {
    6164       45135 :         FOR( j = 0; j < 4; j++ )
    6165             :         {
    6166       36108 :             Copy( lpc_shb_fx, &lpc_shb_sf_fx[i_mult( j, ( LPC_SHB_ORDER + 1 ) )], LPC_SHB_ORDER + 1 );
    6167             :         }
    6168             :     }
    6169             : 
    6170             :     /* From low band excitation, generate highband excitation */
    6171             : 
    6172             :     /* -------- start of  memory rescaling  -------- */
    6173             :     /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
    6174      104728 :     Lmax = 0;
    6175      104728 :     move32();
    6176    68387384 :     FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
    6177             :     {
    6178    68282656 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended_fx[cnt] ) );
    6179             :     }
    6180      104728 :     Q_bwe_exc = norm_l( Lmax );
    6181      104728 :     if ( Lmax == 0 )
    6182             :     {
    6183         368 :         Q_bwe_exc = 31;
    6184         368 :         move16();
    6185             :     }
    6186      104728 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
    6187      104728 :     find_max_mem_dec( st, &n_mem, &n_mem2, &n_mem3 ); /* for >=24.4, use n_mem2 lpc_syn, shb_20sample, and mem_stp_swb_fx memory */
    6188             : 
    6189      104728 :     tmp = add( st->prev_Q_bwe_exc, n_mem );
    6190      104728 :     if ( GT_16( Q_bwe_exc, tmp ) )
    6191             :     {
    6192       16719 :         Q_bwe_exc = tmp;
    6193       16719 :         move16();
    6194             :     }
    6195             : 
    6196             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    6197      104728 :     sc = sub( Q_bwe_exc, st->prev_Q_bwe_exc );
    6198      104728 :     IF( sc != 0 )
    6199             :     {
    6200       46084 :         rescale_genSHB_mem_dec_ivas( st, sc );
    6201             :     }
    6202             : 
    6203             :     /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm  */
    6204      104728 :     sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
    6205             : 
    6206    68387384 :     FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
    6207             :     {
    6208    68282656 :         bwe_exc_extended_16[cnt] = round_fx_sat( L_shl_sat( bwe_exc_extended_fx[cnt], sc ) );
    6209    68282656 :         move16();
    6210             :     }
    6211             : 
    6212             :     /* state_syn_shbexc_fx is kept at (st_fx->prev_Q_bwe_syn) for 24.4/32kbps or is kept at Q_bwe_exc for 13.2/16.4kbps */
    6213             : 
    6214             :     /* save the previous Q factor (32-bit) of the buffer */
    6215      104728 :     st->prev_Q_bwe_exc = Q_bwe_exc;
    6216      104728 :     move16();
    6217             : 
    6218      104728 :     Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
    6219             : 
    6220             :     /* -------- end of rescaling memories -------- */
    6221             : 
    6222      104728 :     Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
    6223      104728 :     move16();
    6224             : 
    6225      104728 :     Q_shb = 0;
    6226      104728 :     move16();
    6227             : 
    6228      104728 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    6229      104728 :     GenShapedSHBExcitation_ivas_dec_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx,
    6230      104728 :                                         hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    6231      104728 :                                         st->coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified_fx, st->extl,
    6232             :                                         &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_32,
    6233      104728 :                                         shb_res_gshape_fx, shb_res_dummy_fx, &vind, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx,
    6234      104728 :                                         &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st->total_brate, st->prev_bfi,
    6235      104728 :                                         st->element_mode, st->flag_ACELP16k, nlExc16k_fx, mixExc16k_fx, st->extl_brate, MSFlag,
    6236             :                                         NULL, &( hBWE_TD->prev_pow_exc16kWhtnd_fx32 ), &( hBWE_TD->prev_mix_factor_fx ), NULL, NULL );
    6237             : 
    6238      104728 :     *Q_white_exc = Q_bwe_exc_fb;
    6239      104728 :     move16();
    6240      104728 :     IF( EQ_16( st->extl, FB_TBE ) )
    6241             :     {
    6242       40669 :         hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
    6243       40669 :         move16();
    6244             :     }
    6245             :     ELSE
    6246             :     {
    6247             :         /*Indirectly a memory reset of FB memories for next frame such that rescaling of memories would lead to 0 due to such high prev. Q value.
    6248             :         51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
    6249       64059 :         hBWE_TD->prev_Q_bwe_exc_fb = 51;
    6250       64059 :         move16();
    6251             :     }
    6252             : 
    6253             :     /* rescale the TBE post proc memory */
    6254     1152008 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    6255             :     {
    6256     1047280 :         hBWE_TD->mem_stp_swb_fx[i] = shl_sat( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn ) );
    6257     1047280 :         move16();
    6258             :     }
    6259             :     /* fill-in missing SHB excitation */
    6260      104728 :     test();
    6261      104728 :     test();
    6262      104728 :     IF( ( EQ_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) && LE_32( st->last_core_brate, SID_2k40 ) )
    6263             :     {
    6264         176 :         Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    6265             :     }
    6266             : 
    6267      104728 :     IF( hStereoICBWE != NULL )
    6268             :     {
    6269       16532 :         Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
    6270             :     }
    6271             : 
    6272      104728 :     test();
    6273      104728 :     IF( NE_32( st->extl_brate, SWB_TBE_1k10 ) && NE_32( st->extl_brate, SWB_TBE_1k75 ) )
    6274             :     {
    6275      502785 :         FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    6276             :         {
    6277             :             /* TD BWE post-processing */
    6278      402228 :             PostShortTerm_ivas_dec_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
    6279      402228 :                                        hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
    6280             :         }
    6281             : 
    6282      100557 :         Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k ); /* Q_bwe_exc */
    6283             : 
    6284      100557 :         tmp = sub( shl( Q_bwe_exc, 1 ), 31 + 16 );
    6285      100557 :         prev_pow_fx = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    6286      100557 :         curr_pow_fx = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    6287     3117267 :         FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
    6288             :         {
    6289     3016710 :             prev_pow_fx = L_mac0_sat( prev_pow_fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] );                                         /*2*Q_bwe_exc*/
    6290     3016710 :             curr_pow_fx = L_mac0_sat( curr_pow_fx, shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
    6291             :         }
    6292             : 
    6293      100557 :         if ( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
    6294             :         {
    6295        5775 :             curr_pow_fx = L_shr( curr_pow_fx, 2 ); /* Q(2*Q_bwe_exc) */
    6296             :         }
    6297             : 
    6298      100557 :         Lscale = root_a_over_b_fx( curr_pow_fx, shl( Q_bwe_exc, 1 ), prev_pow_fx, shl( Q_bwe_exc, 1 ), &exp );
    6299             : 
    6300     2111697 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    6301             :         {
    6302     2011140 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    6303     2011140 :             shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    6304     2011140 :             move16();
    6305             :         }
    6306      100557 :         IF( exp < 0 )
    6307             :         {
    6308        3207 :             Lscale = L_shl( Lscale, exp );
    6309        3207 :             exp = 0;
    6310        3207 :             move16();
    6311             :         }
    6312             :         /*
    6313             :            code for EVS and IVAS are basically identical with the exception of i_mult_sat() which has precision issues
    6314             :            thus is was replaced for IVAS and kept for EVS, in order to keep EVS BE to test sequences and legacy implementations
    6315             :         */
    6316      100557 :         IF( EQ_16( st->element_mode, EVS_MONO ) )
    6317             :         {
    6318           0 :             FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    6319             :             {
    6320           0 :                 temp_fx = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ );        /* Q15 */
    6321           0 :                 L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp_fx ); /* Q31-exp */
    6322           0 :                 temp_fx = sub( 32767 /*1.0f Q15*/, temp_fx );
    6323           0 :                 Lscale = L_add( Mult_32_16( Lscale, temp_fx ), L_tmp1 );
    6324           0 :                 L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    6325           0 :                 shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    6326           0 :                 move16();
    6327             :             }
    6328             :         }
    6329             :         ELSE
    6330             :         {
    6331     1106127 :             FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    6332             :             {
    6333     1005570 :                 temp_fx = round_fx_sat( L_shl( L_mult( 0x6666 /* 0.1 in Q12 */, shl( sub( i, 19 ), 11 ) ), 1 ) );
    6334     1005570 :                 L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp_fx ); /* Q31-exp */
    6335     1005570 :                 temp_fx = sub( 32767 /*1.0f Q15*/, temp_fx );
    6336     1005570 :                 Lscale = L_add( Mult_32_16( Lscale, temp_fx ), L_tmp1 );
    6337     1005570 :                 L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    6338     1005570 :                 shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    6339     1005570 :                 move16();
    6340             :             }
    6341             :         }
    6342             :     }
    6343             :     ELSE
    6344             :     {
    6345             :         /* reset the PF memories if the PF is not running */
    6346        4171 :         set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
    6347        4171 :         hBWE_TD->gain_prec_swb_fx = ONE_IN_Q14;
    6348        4171 :         move16();
    6349        4171 :         set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
    6350             :     }
    6351             : 
    6352             :     /* Update SHB excitation */
    6353      104728 :     Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
    6354      104728 :     l_subframe = L_FRAME16k / NUM_SHB_SUBGAINS;
    6355      104728 :     move16();
    6356      104728 :     L_ener = EPSILON_FX_SMALL;
    6357      104728 :     move32();
    6358             : 
    6359      523640 :     FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6360             :     {
    6361      418912 :         L_tmp = 0;
    6362      418912 :         move32();
    6363      418912 :         ener_tmp_fx[i] = EPSILON_FX_SMALL;
    6364      418912 :         move32();
    6365             : 
    6366      418912 :         Word64 tmp64 = 0;
    6367      418912 :         move64();
    6368    33931872 :         FOR( j = 0; j < l_subframe; j++ )
    6369             :         {
    6370    33512960 :             tmp64 = W_mac0_16_16( tmp64, shaped_shb_excitation_fx[add( i_mult( i, l_subframe ), j )], shaped_shb_excitation_fx[add( i_mult( i, l_subframe ), j )] ); /* 2*Q_bwe_exc */
    6371             :         }
    6372      418912 :         L_tmp = W_sat_l( tmp64 );
    6373             : 
    6374      418912 :         L_tmp = Mult_32_16( L_tmp, 410 /*0.0125 Q15*/ ); /* 2*Q_bwe_exc: ener_tmp_fx in (2*Q_bwe_exc) */
    6375      418912 :         IF( L_tmp != 0 )
    6376             :         {
    6377      417977 :             exp = norm_l( L_tmp );
    6378      417977 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    6379      417977 :             exp = sub( exp, sub( 30, i_mult( 2, Q_bwe_exc ) ) );
    6380             : 
    6381      417977 :             tmp = div_s( 16384, tmp );
    6382      417977 :             L_tmp = L_deposit_h( tmp );
    6383      417977 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    6384      417977 :             ener_tmp_fx[i] = L_shl_sat( L_tmp, sub( add( exp, shl( Q_bwe_exc, 1 ) ), 31 ) ); /*2 * Q_bwe_exc: Q31 -exp +exp +2 * Q_bwe_exc -31  */
    6385      417977 :             move32();
    6386      417977 :             L_ener = L_add_sat( L_ener, L_shr( ener_tmp_fx[i], 2 ) ); /* 2*Q_bwe_exc */
    6387             :         }
    6388             :     }
    6389      104728 :     ener_fx = s_max( 1, round_fx_sat( L_shl_sat( L_ener, sub( 18, shl( Q_bwe_exc, 1 ) ) ) ) ); /* Q2: 2*Q_bwe_exc+18-2*Q_bwe_exc-16 */
    6390             :     /* WB/SWB bandwidth switching */
    6391      104728 :     IF( st->bws_cnt > 0 )
    6392             :     {
    6393          12 :         IF( is_fractive == 0 )
    6394             :         {
    6395          12 :             IF( GT_16( st->tilt_wb_fx, 2048 ) ) /*assuming st->tilt_wb_fx in Q11*/
    6396             :             {
    6397           0 :                 st->tilt_wb_fx = 2048;
    6398           0 :                 move16();
    6399             :             }
    6400          12 :             ELSE IF( LT_16( st->tilt_wb_fx, 1024 ) )
    6401             :             {
    6402          12 :                 st->tilt_wb_fx = 1024;
    6403          12 :                 move16();
    6404             :             }
    6405          12 :             test();
    6406          12 :             if ( EQ_16( st->prev_fractive, 1 ) && GT_16( st->tilt_wb_fx, 1024 ) )
    6407             :             {
    6408           0 :                 st->tilt_wb_fx = 1024;
    6409           0 :                 move16();
    6410             :             }
    6411             :         }
    6412             :         ELSE
    6413             :         {
    6414           0 :             IF( GT_16( st->tilt_wb_fx, 8192 ) )
    6415             :             {
    6416           0 :                 IF( st->prev_fractive == 0 )
    6417             :                 {
    6418           0 :                     st->tilt_wb_fx = 8192;
    6419           0 :                     move16();
    6420             :                 }
    6421             :                 ELSE
    6422             :                 {
    6423           0 :                     st->tilt_wb_fx = 16384;
    6424           0 :                     move16();
    6425             :                 }
    6426             :             }
    6427             :             ELSE
    6428             :             {
    6429           0 :                 st->tilt_wb_fx = shl( st->tilt_wb_fx, 2 );
    6430           0 :                 move16();
    6431             :             }
    6432             :         }
    6433             : 
    6434          12 :         IF( ener_fx != 0 )
    6435             :         {
    6436          12 :             L_tmp = L_shl( L_mult0( ener_fx, st->tilt_wb_fx ), sub( st->Q_syn2, 13 ) ); /* 2+11 +st->Q_syn2 -13 = st->Q_syn2*/
    6437          12 :             exp_ener = norm_s( ener_fx );
    6438          12 :             tmp = shl( ener_fx, exp_ener );           /*Q(2+exp)*/
    6439          12 :             inv_ener = shr( div_s( 16384, tmp ), 1 ); /*Q(15+14-2-exp-1) = 26 - exp*/
    6440             : 
    6441          12 :             test();
    6442          12 :             IF( GT_32( L_tmp, st->enerLH_fx ) ) /*st->Q_syn2*/
    6443             :             {
    6444           0 :                 st->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st->enerLH_fx, inv_ener ), sub( sub( st->Q_syn2, exp_ener ), 16 ) ) ); /*Q11*/
    6445           0 :                 move16();
    6446             :                 /*st->Q_syn2 -1 + 26- exp_ener -15 -(st->Q_syn2 -exp_ener -16 ) -16    +1 -1 = (11) *0.5*/
    6447             :             }
    6448          12 :             ELSE IF( LT_32( L_tmp, Mult_32_16( st->enerLH_fx, 1638 ) ) && EQ_16( is_fractive, 1 ) )
    6449             :             {
    6450           0 :                 st->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st->enerLH_fx, inv_ener ), sub( sub( st->Q_syn2, exp_ener ), 15 ) ) ); /*Q11*/
    6451           0 :                 move16();
    6452             :                 /*st->Q_syn2 -1 + 26- exp_ener -15 -(st->Q_syn2 -exp_ener -15 ) -16 = (11) 0.25*/
    6453             :             }
    6454          12 :             L_tmp = L_mult0( st->prev_ener_shb_fx, inv_ener );         /*Q(1+15+14-3-exp_ener) = 27 -exp_ener*/
    6455          12 :             GainFrame_prevfrm_fx = L_shr( L_tmp, sub( 9, exp_ener ) ); /*27 -exp_ener -(9-exp_ener )= Q18*/
    6456             :         }
    6457             :         ELSE
    6458             :         {
    6459           0 :             GainFrame_prevfrm_fx = 0;
    6460           0 :             move32();
    6461             :         }
    6462             : 
    6463          12 :         IF( EQ_16( is_fractive, 1 ) )
    6464             :         {
    6465           0 :             GainFrame_fx = L_shl( L_deposit_l( st->tilt_wb_fx ), 10 );
    6466             :         }
    6467             :         ELSE
    6468             :         {
    6469          12 :             GainFrame_fx = L_shl( L_deposit_l( st->tilt_wb_fx ), 8 );
    6470             :         }
    6471             : 
    6472          12 :         test();
    6473          12 :         IF( EQ_16( ( is_fractive & st->prev_fractive ), 1 ) && GT_32( GainFrame_fx, GainFrame_prevfrm_fx ) )
    6474             :         {
    6475           0 :             GainFrame_fx = L_add( Mult_32_16( GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame_fx, 6554 ) ); /* 18 +15 -15 = 18*/
    6476             :         }
    6477             :         ELSE
    6478             :         {
    6479          12 :             test();
    6480          12 :             test();
    6481          12 :             test();
    6482          12 :             test();
    6483          12 :             IF( ( LT_32( L_shr( st->prev_enerLH_fx, 1 ), st->enerLH_fx ) && GT_32( st->prev_enerLH_fx, L_shr( st->enerLH_fx, 1 ) ) ) && ( LT_32( L_shr( st->prev_enerLL_fx, 1 ), st->enerLL_fx ) && GT_32( st->prev_enerLL_fx, L_shr( st->enerLL_fx, 1 ) ) ) && ( s_xor( is_fractive, st->prev_fractive ) == 0 ) )
    6484             :             {
    6485           0 :                 GainFrame_fx = L_add( L_shr( GainFrame_fx, 1 ), L_shr( GainFrame_prevfrm_fx, 1 ) );
    6486             :             }
    6487             :             ELSE
    6488             :             {
    6489          12 :                 test();
    6490          12 :                 IF( ( is_fractive == 0 ) && EQ_16( st->prev_fractive, 1 ) )
    6491             :                 {
    6492           0 :                     L_tmp1 = L_shl( Mult_32_16( GainFrame_fx, 3277 ), 13 );                                                /* 31 */
    6493           0 :                     L_tmp = L_sub( 2147483647, L_tmp1 );                                                                   /* 31 */
    6494           0 :                     GainFrame_fx = L_add( Mult_32_32( GainFrame_fx, L_tmp ), Mult_32_32( GainFrame_prevfrm_fx, L_tmp1 ) ); /* 18 */
    6495             :                 }
    6496             :                 ELSE
    6497             :                 {
    6498          12 :                     GainFrame_fx = L_add( L_shr( GainFrame_fx, 1 ), L_shr( L_min( GainFrame_prevfrm_fx, GainFrame_fx ), 1 ) ); /* 18 */
    6499             :                 }
    6500             :             }
    6501             :         }
    6502             : 
    6503          12 :         GainFrame_fx = Mult_32_16( GainFrame_fx, i_mult( sub( N_WS2N_FRAMES, st->bws_cnt ), 819 ) ); /*Q18*/
    6504             :     }
    6505             :     ELSE
    6506             :     {
    6507      104716 :         IF( st->bws_cnt1 > 0 )
    6508             :         {
    6509         286 :             GainFrame_fx = Mult_32_16( GainFrame_fx, i_mult( st->bws_cnt1, 819 ) ); /*Q18*/
    6510             :         }
    6511      104716 :         IF( GE_16( st->nbLostCmpt, 1 ) )
    6512             :         {
    6513        2131 :             ener_fx = s_max( 1, ener_fx );
    6514        2131 :             exp_ener = norm_s( ener_fx );
    6515        2131 :             tmp = shl( ener_fx, exp_ener );                                                                            /*Q(2+exp)*/
    6516        2131 :             inv_ener = div_s( 16384, tmp );                                                                            /*Q(15+14-2-exp)*/
    6517        2131 :             prev_ener_ratio_fx = L_shr_sat( L_mult0( st->prev_ener_shb_fx, inv_ener ), add( sub( 9, exp_ener ), 1 ) ); /*Q: 1+27-exp-9+exp-1 = 18 */
    6518             :         }
    6519             : 
    6520      104716 :         IF( EQ_16( st->nbLostCmpt, 1 ) )
    6521             :         {
    6522        1187 :             test();
    6523        1187 :             test();
    6524        1187 :             test();
    6525        1187 :             test();
    6526        1187 :             test();
    6527        1187 :             test();
    6528        1187 :             test();
    6529        1187 :             test();
    6530        1187 :             test();
    6531        1187 :             test();
    6532        1187 :             IF( ( st->clas_dec != UNVOICED_CLAS ) && NE_16( st->clas_dec, UNVOICED_TRANSITION ) && LT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) &&
    6533             :                 ( ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) && LT_32( L_shr( st->enerLL_fx, 1 ), st->prev_enerLL_fx ) ) || ( GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) && LT_32( L_shr( st->enerLH_fx, 1 ), st->prev_enerLH_fx ) ) ) )
    6534             :             {
    6535           0 :                 IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) ) /*18*/
    6536             :                 {
    6537           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 13107 ), Mult_32_16( GainFrame_fx, 19661 ) ); /*18*/
    6538             :                 }
    6539           0 :                 ELSE IF( GT_32( L_shr( prev_ener_ratio_fx, 1 ), GainFrame_fx ) )
    6540             :                 {
    6541           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame_fx, 6554 ) );
    6542             :                 }
    6543             :                 ELSE
    6544             :                 {
    6545           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame_fx, 26214 ) );
    6546             :                 }
    6547             : 
    6548           0 :                 test();
    6549           0 :                 IF( GT_16( tilt_swb_fec_fx, hBWE_TD->tilt_swb_fec_fx ) && ( hBWE_TD->tilt_swb_fec_fx > 0 ) )
    6550             :                 {
    6551           0 :                     exp = norm_s( hBWE_TD->tilt_swb_fec_fx );
    6552           0 :                     tmp = shl( hBWE_TD->tilt_swb_fec_fx, exp );                                 /*Q(11+exp)*/
    6553           0 :                     tmp = div_s( 16384, tmp );                                                  /*Q(15+14-11-exp)*/
    6554           0 :                     tmp = extract_h( L_shl( L_mult0( tmp, st->tilt_wb_fx ), sub( exp, 1 ) ) );  /*18 -exp +11  + exp -1 -16  =12;          */
    6555           0 :                     GainFrame_fx = L_shl( Mult_32_16( GainFrame_fx, s_min( tmp, 20480 ) ), 3 ); /*Q18 = 18 +12 -15 +3 */
    6556             :                 }
    6557             :             }
    6558        1187 :             ELSE IF( ( ( st->clas_dec != UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 4096 ) ) && GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) &&
    6559             :                      ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) )
    6560             :             {
    6561           0 :                 GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame_fx, 26214 ) );
    6562             :             }
    6563             :         }
    6564      103529 :         ELSE IF( GT_16( st->nbLostCmpt, 1 ) )
    6565             :         {
    6566         944 :             test();
    6567         944 :             test();
    6568         944 :             test();
    6569         944 :             test();
    6570         944 :             test();
    6571         944 :             test();
    6572         944 :             test();
    6573         944 :             test();
    6574         944 :             IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) && ( ( EQ_16( st->codec_mode, MODE1 ) && GT_32( st->enerLL_fx, st->prev_enerLL_fx ) && GT_32( st->enerLH_fx, st->prev_enerLH_fx ) ) || EQ_16( st->codec_mode, MODE2 ) ) )
    6575             :             {
    6576           0 :                 test();
    6577           0 :                 IF( GT_16( tilt_swb_fec_fx, 20480 /*10.0f in Q11*/ ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 /*10.0f in Q11*/ ) )
    6578             :                 {
    6579           0 :                     GainFrame_fx = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame_fx, 6554 /*0.2f in Q15*/ ) ), L_shl( Mult_32_16( GainFrame_fx, 16384 /*4.0f in Q12*/ ), 3 ) ); /*Q18*/
    6580             :                 }
    6581             :                 ELSE
    6582             :                 {
    6583           0 :                     GainFrame_fx = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame_fx, 16384 ) ), L_shl( Mult_32_16( GainFrame_fx, 16384 ), 3 ) ); /*Q18*/
    6584             :                 }
    6585             :             }
    6586         944 :             ELSE IF( GT_32( prev_ener_ratio_fx, GainFrame_fx ) && ( ( EQ_16( st->codec_mode, MODE1 ) && GT_32( st->enerLL_fx, st->prev_enerLL_fx ) && GT_32( st->enerLH_fx, st->prev_enerLH_fx ) ) || EQ_16( st->codec_mode, MODE2 ) ) )
    6587             :             {
    6588           0 :                 test();
    6589           0 :                 IF( GT_16( tilt_swb_fec_fx, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
    6590             :                 {
    6591           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 16384 /*0.5f in Q15*/ ), Mult_32_16( GainFrame_fx, 16384 /*0.5f in Q15*/ ) ); /* Q18  */
    6592             :                 }
    6593             :                 ELSE
    6594             :                 {
    6595           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 /*0.2f in Q15*/ ), Mult_32_16( GainFrame_fx, 26214 /*0.8f in Q15*/ ) ); /* Q18  */
    6596             :                 }
    6597             :             }
    6598             :         }
    6599             :     }
    6600             : 
    6601      104728 :     st->prev_fractive = is_fractive;
    6602      104728 :     move16();
    6603             : 
    6604             :     /* Adjust the subframe and frame gain of the synthesized shb signal */
    6605             :     /* Scale the shaped excitation */
    6606      104728 :     IF( EQ_16( st->L_frame, L_FRAME ) )
    6607             :     {
    6608       49209 :         L_tmp = L_mult( pitch_buf_fx[0], 8192 );
    6609      196836 :         FOR( i = 1; i < NB_SUBFR; i++ )
    6610             :         {
    6611      147627 :             L_tmp = L_mac( L_tmp, pitch_buf_fx[i], 8192 ); /* pitch_buf in Q6 x 0.25 in Q15 */
    6612             :         }
    6613       49209 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    6614             :     }
    6615             :     ELSE
    6616             :     {
    6617       55519 :         L_tmp = L_mult( pitch_buf_fx[0], 6554 );
    6618      277595 :         FOR( i = 1; i < NB_SUBFR16k; i++ )
    6619             :         {
    6620      222076 :             L_tmp = L_mac( L_tmp, pitch_buf_fx[i], 6554 ); /* pitch_buf in Q6 x 0.2 in Q15 */
    6621             :         }
    6622       55519 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    6623             :     }
    6624             : 
    6625      104728 :     test();
    6626      104728 :     test();
    6627      104728 :     test();
    6628      104728 :     test();
    6629      104728 :     test();
    6630      104728 :     test();
    6631      104728 :     test();
    6632      104728 :     test();
    6633      104728 :     test();
    6634      104728 :     test();
    6635      104728 :     test();
    6636      104728 :     test();
    6637      104728 :     IF( ( ( GE_32( st->extl_brate, SWB_TBE_2k8 ) && EQ_16( st->prev_coder_type, st->coder_type ) && NE_16( st->coder_type, UNVOICED ) ) || ( LT_32( st->extl_brate, SWB_TBE_2k8 ) && ( EQ_16( st->prev_coder_type, st->coder_type ) || ( EQ_16( st->prev_coder_type, VOICED ) && EQ_16( st->coder_type, GENERIC ) ) || ( EQ_16( st->prev_coder_type, GENERIC ) && EQ_16( st->coder_type, VOICED ) ) ) ) ) && GT_16( pitch_fx, 4480 /*70 in Q6*/ ) && LT_16( st->extl, FB_TBE ) && NE_32( st->extl_brate, SWB_TBE_1k10 ) && NE_32( st->extl_brate, SWB_TBE_1k75 ) )
    6638             :     {
    6639      147085 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6640             :         {
    6641      117668 :             GainShape_tmp_fx[i] = GainShape_fx[i * 4]; /* Q15 */
    6642      117668 :             move16();
    6643             :         }
    6644             : 
    6645      147085 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6646             :         {
    6647      117668 :             L_tmp1 = Mult_32_16( ener_tmp_fx[i], GainShape_tmp_fx[i] );               /* (2*Q_bwe_exc) */
    6648      117668 :             L_tmp2 = Mult_32_16( hBWE_TD->prev_ener_fx, hBWE_TD->prev_GainShape_fx ); /* (2*st->prev_ener_fx_Q) */
    6649      117668 :             tmp = sub( shl( Q_bwe_exc, 1 ), shl( hBWE_TD->prev_ener_fx_Q, 1 ) );
    6650      117668 :             L_tmp2 = L_shl_sat( L_tmp2, tmp ); /* new Q = (2*Q_bwe_exc) */
    6651      117668 :             IF( GT_32( L_tmp1, L_tmp2 ) )
    6652             :             {
    6653       68959 :                 L_tmp = L_tmp2;
    6654       68959 :                 move32();
    6655       68959 :                 if ( L_tmp2 < 0 )
    6656             :                 {
    6657           0 :                     L_tmp = L_negate( L_tmp2 );
    6658             :                 }
    6659             : 
    6660       68959 :                 expb = norm_l( L_tmp );
    6661       68959 :                 fracb = round_fx_sat( L_shl_sat( L_tmp, expb ) );
    6662       68959 :                 expb = sub( 30, expb ); /* - (2*Q_bwe_exc_ext);                    */
    6663             : 
    6664       68959 :                 expa = norm_l( ener_tmp_fx[i] );
    6665       68959 :                 fraca = extract_h( L_shl( ener_tmp_fx[i], expa ) );
    6666       68959 :                 expa = sub( 30, expa );
    6667             : 
    6668       68959 :                 scale_fx = shr( sub( fraca, fracb ), 15 );
    6669       68959 :                 fracb = shl( fracb, scale_fx );
    6670       68959 :                 expb = sub( expb, scale_fx );
    6671             : 
    6672       68959 :                 tmp = div_s( fracb, fraca );
    6673       68959 :                 exp = sub( sub( expb, expa ), 1 );
    6674       68959 :                 tmp = shl( tmp, exp );
    6675       68959 :                 GainShape_tmp_fx[i] = add( tmp, shr( GainShape_tmp_fx[i], 1 ) ); /* Q15 */
    6676       68959 :                 move16();
    6677             :             }
    6678             : 
    6679      117668 :             hBWE_TD->prev_ener_fx = ener_tmp_fx[i];
    6680      117668 :             move32();
    6681      117668 :             hBWE_TD->prev_GainShape_fx = GainShape_tmp_fx[i];
    6682      117668 :             move16();
    6683      117668 :             hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    6684      117668 :             move16();
    6685             :         }
    6686             : 
    6687      500089 :         FOR( i = 0; i < NUM_SHB_SUBFR; i++ )
    6688             :         {
    6689      470672 :             Word16 idx = 0;
    6690      470672 :             move16();
    6691      470672 :             IF( i != 0 )
    6692             :             {
    6693      441255 :                 idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
    6694             :             }
    6695      470672 :             GainShape_fx[i] = GainShape_tmp_fx[idx];
    6696      470672 :             move16();
    6697             :         }
    6698             :     }
    6699             :     ELSE
    6700             :     {
    6701       75311 :         hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    6702       75311 :         move16();
    6703             :     }
    6704      104728 :     hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
    6705      104728 :     move16();
    6706             : 
    6707             : 
    6708             :     /* Gain shape smoothing after quantization */
    6709      104728 :     test();
    6710      104728 :     IF( EQ_32( st->extl_brate, SWB_TBE_1k10 ) || EQ_32( st->extl_brate, SWB_TBE_1k75 ) )
    6711             :     {
    6712       20855 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6713             :         {
    6714       16684 :             GainShape_tmp_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS];
    6715       16684 :             move16();
    6716             :         }
    6717             : 
    6718        4171 :         lls_interp_n_fx( GainShape_tmp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp_fx, 1 );
    6719             : 
    6720        4171 :         test();
    6721        4171 :         IF( GE_16( vind, 6 ) && LT_16( abs_s( GainShape_tilt_fx ), 3932 ) )
    6722             :         {
    6723        1908 :             feedback_fx = 9830;
    6724        1908 :             move16();
    6725        9540 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6726             :             {
    6727        7632 :                 GainShape_fx[i] = add_sat( mult( sub( 32767, feedback_fx ), GainShape_fx[i * NUM_SHB_SUBGAINS] ), mult( feedback_fx, GainShape_tmp_fx[i] ) );
    6728        7632 :                 move16();
    6729             :             }
    6730             : 
    6731       30528 :             FOR( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
    6732             :             {
    6733       28620 :                 Word16 idx = 0;
    6734       28620 :                 move16();
    6735       28620 :                 IF( i != 0 )
    6736             :                 {
    6737       28620 :                     idx = shr( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR_LOG2 );
    6738             :                 }
    6739       28620 :                 GainShape_fx[i] = GainShape_fx[idx];
    6740       28620 :                 move16();
    6741             :             }
    6742             :         }
    6743             :     }
    6744             : 
    6745             :     /* fil-in missing memory */
    6746      104728 :     test();
    6747      104728 :     test();
    6748      104728 :     IF( ( EQ_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) && LE_32( st->last_core_brate, SID_2k40 ) )
    6749             :     {
    6750        3696 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    6751             :         {
    6752        3520 :             Word16 intermediate = mult( shaped_shb_excitation_fx[i], subwin_shb_fx[L_SHB_LAHEAD - i] );
    6753        3520 :             Word32 intermediate_32 = Mpy_32_16_1( Mpy_32_16_1( GainFrame_fx, window_shb_fx[L_SHB_LAHEAD - 1 - i] ), intermediate );
    6754        3520 :             hBWE_TD->syn_overlap_fx[i] = round_fx( L_shl_sat( intermediate_32, sub( 16, ( add( Q_bwe_exc, 18 - 15 ) ) ) ) );
    6755        3520 :             move16();
    6756             :         }
    6757             :     }
    6758             : 
    6759      104728 :     Word16 n_mem3_new = 0;
    6760      104728 :     move16();
    6761      104728 :     find_max_mem_dec_m3( st, &n_mem3_new );
    6762             : 
    6763      104728 :     ScaleShapedSHB_fx( SHB_OVERLAP_LEN,
    6764             :                        shaped_shb_excitation_fx, /* i/o: Q_bwe_exc */
    6765      104728 :                        hBWE_TD->syn_overlap_fx,
    6766             :                        GainShape_fx, /* Q15  */
    6767             :                        GainFrame_fx, /* Q18  */
    6768             :                        window_shb_fx,
    6769             :                        subwin_shb_fx,
    6770      104728 :                        &Q_bwe_exc, &Qx, n_mem3_new, hBWE_TD->prev_Q_bwe_syn2 );
    6771             : 
    6772      104728 :     IF( hStereoICBWE != NULL )
    6773             :     {
    6774       16532 :         Copy_Scale_sig_16_32_DEPREC( lpc_shb_fx, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER + 1, 0 );
    6775       16532 :         Copy( GainShape_fx, hStereoICBWE->gshapeRef_fx, NUM_SHB_SUBFR );
    6776       16532 :         hStereoICBWE->gFrameRef_fx = GainFrame_fx;
    6777       16532 :         move32();
    6778             : 
    6779       16532 :         Copy( shaped_shb_excitation_fx, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
    6780             :     }
    6781             : 
    6782      104728 :     max_val = 0;
    6783      104728 :     move16();
    6784    33617688 :     FOR( i = 0; i < L_FRAME16k; i++ )
    6785             :     {
    6786    33512960 :         max_val = s_max( max_val, abs_s( shaped_shb_excitation_fx[i] ) ); /* Q0 */
    6787             :     }
    6788      104728 :     IF( max_val == 0 )
    6789             :     {
    6790          23 :         curr_frame_pow_fx = 0;
    6791          23 :         move32();
    6792          23 :         n = 0;
    6793          23 :         move16();
    6794             :     }
    6795             :     ELSE
    6796             :     {
    6797      104705 :         n = norm_s( max_val );
    6798      104705 :         max_val = 0;
    6799      104705 :         move16();
    6800    33610305 :         FOR( i = 0; i < L_FRAME16k; i++ )
    6801             :         {
    6802    33505600 :             shaped_shb_excitation_frac[i] = shl_sat( shaped_shb_excitation_fx[i], n ); /*Q_bwe_exc+n*/
    6803    33505600 :             move16();
    6804             :         }
    6805             : 
    6806      104705 :         curr_frame_pow_fx = 0;
    6807      104705 :         move32();
    6808    33610305 :         FOR( i = 0; i < L_FRAME16k; i++ )
    6809             :         {
    6810    33505600 :             L_tmp = L_mult0( shaped_shb_excitation_frac[i], shaped_shb_excitation_frac[i] ); /*2*(Q_bwe_exc+n)*/
    6811    33505600 :             curr_frame_pow_fx = L_add( curr_frame_pow_fx, L_shr( L_tmp, 9 ) );               /*2*(Q_bwe_exc+n)-9*/
    6812             :         }
    6813             :     }
    6814      104728 :     curr_frame_pow_exp = sub( shl( add( Q_bwe_exc, n ), 1 ), 9 );
    6815      104728 :     tmp = sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp );
    6816      104728 :     IF( tmp > 0 ) /* shifting prev */
    6817             :     {
    6818         942 :         IF( GT_16( tmp, 32 ) )
    6819             :         {
    6820           0 :             hBWE_TD->prev_frame_pow_exp = add( curr_frame_pow_exp, 32 );
    6821           0 :             move16();
    6822           0 :             tmp = 32;
    6823           0 :             move16();
    6824             :         }
    6825         942 :         hBWE_TD->prev_swb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, tmp );
    6826         942 :         move32();
    6827         942 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    6828         942 :         move16();
    6829             :     }
    6830             :     ELSE /* shifting curr */
    6831             :     {
    6832      103786 :         IF( LT_16( tmp, -32 ) )
    6833             :         {
    6834         182 :             curr_frame_pow_exp = sub( hBWE_TD->prev_frame_pow_exp, 32 );
    6835         182 :             tmp = -32;
    6836         182 :             move16();
    6837             :         }
    6838      103786 :         curr_frame_pow_fx = L_shr( curr_frame_pow_fx, -tmp );
    6839      103786 :         curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
    6840      103786 :         move16();
    6841             :     }
    6842      104728 :     test();
    6843      104728 :     test();
    6844      104728 :     IF( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
    6845             :     {
    6846        1207 :         test();
    6847        1207 :         test();
    6848        1207 :         IF( ( GT_32( L_shr( curr_frame_pow_fx, 1 ), hBWE_TD->prev_swb_bwe_frame_pow_fx ) ) &&
    6849             :             ( GT_32( hBWE_TD->prev_swb_bwe_frame_pow_fx, L_tmp ) ) && EQ_16( st->prev_coder_type, UNVOICED ) )
    6850             :         {
    6851           1 :             L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6852           1 :             scale_fx = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    6853             : 
    6854           1 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6855           1 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6856           1 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6857           1 :             temp_fx = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    6858             :         }
    6859             :         ELSE
    6860             :         {
    6861        1206 :             scale_fx = temp_fx = 32767;
    6862        1206 :             move16(); /*Q15*/
    6863        1206 :             move16(); /*Q15*/
    6864             :         }
    6865             : 
    6866       10863 :         FOR( j = 0; j < 8; j++ )
    6867             :         {
    6868        9656 :             GainShape_fx[2 * j] = mult_r( GainShape_fx[2 * j], scale_fx );
    6869        9656 :             move16();
    6870        9656 :             GainShape_fx[2 * j + 1] = mult_r( GainShape_fx[2 * j + 1], scale_fx );
    6871        9656 :             move16();
    6872      395896 :             FOR( i = 0; i < L_FRAME16k / 8; i++ )
    6873             :             {
    6874      386240 :                 shaped_shb_excitation_fx[add( i, j * ( L_FRAME16k / 8 ) )] = mult_r( shaped_shb_excitation_fx[add( i, j * ( L_FRAME16k / 8 ) )], scale_fx );
    6875      386240 :                 move16();
    6876             :             }
    6877             : 
    6878        9656 :             IF( temp_fx > 0 )
    6879             :             {
    6880             :                 /* scale_fx <= temp_fx, due to scale_fx = sqrt( st->prev_swb_bwe_frame_pow_fx/curr_frame_pow_fx ), temp_fx = sqrt( scale_fx, 1.f/8.f )
    6881             :                    and curr_frame_pow_fx > st->prev_swb_bwe_frame_pow_fx -> scale_fx <= 1.0, sqrt(scale_fx, 1.f/8.f) >= scale_fx  */
    6882        9656 :                 IF( LT_16( scale_fx, temp_fx ) )
    6883             :                 {
    6884           8 :                     scale_fx = div_s( scale_fx, temp_fx );
    6885             :                 }
    6886             :                 ELSE
    6887             :                 {
    6888        9648 :                     scale_fx = 32767;
    6889        9648 :                     move16();
    6890             :                 }
    6891             :             }
    6892             :             ELSE
    6893             :             {
    6894           0 :                 scale_fx = 0;
    6895           0 :                 move16();
    6896             :             }
    6897             :         }
    6898             :     }
    6899             : 
    6900             :     /* adjust the FEC frame energy */
    6901      104728 :     IF( st->bfi )
    6902             :     {
    6903        2143 :         scale_fx = temp_fx = 4096;
    6904        2143 :         move16(); /*Q12*/
    6905        2143 :         move16();
    6906        2143 :         IF( EQ_16( st->nbLostCmpt, 1 ) )
    6907             :         {
    6908        1196 :             test();
    6909        1196 :             test();
    6910        1196 :             test();
    6911        1196 :             test();
    6912        1196 :             test();
    6913        1196 :             test();
    6914        1196 :             test();
    6915        1196 :             test();
    6916        1196 :             IF( GT_32( curr_frame_pow_fx, hBWE_TD->prev_swb_bwe_frame_pow_fx ) &&
    6917             :                 NE_16( st->prev_coder_type, UNVOICED ) &&
    6918             :                 ( st->last_good != UNVOICED_CLAS ) )
    6919             :             {
    6920          81 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp ); /*31 - exp*/
    6921          81 :                 scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );                                                                            /*Q12*/
    6922          81 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6923          81 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6924          81 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6925          81 :                 temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6926             :             }
    6927        1115 :             ELSE IF( LT_32( curr_frame_pow_fx, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) && EQ_16( st->nbLostCmpt, 1 ) &&
    6928             :                      ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) &&
    6929             :                      ( EQ_16( st->prev_coder_type, UNVOICED ) || ( st->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    6930             :             {
    6931           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6932           0 :                 scale_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6933           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6934           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6935           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6936           0 :                 temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6937             :             }
    6938             :         }
    6939         947 :         ELSE IF( GT_16( st->nbLostCmpt, 1 ) )
    6940             :         {
    6941         947 :             test();
    6942         947 :             test();
    6943         947 :             test();
    6944         947 :             test();
    6945         947 :             test();
    6946         947 :             IF( GT_32( curr_frame_pow_fx, hBWE_TD->prev_swb_bwe_frame_pow_fx ) )
    6947             :             {
    6948         237 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6949         237 :                 scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6950         237 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6951         237 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6952         237 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6953         237 :                 temp_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6954             :             }
    6955         710 :             ELSE IF( LT_32( curr_frame_pow_fx, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) &&
    6956             :                      ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) &&
    6957             :                      ( EQ_16( st->prev_coder_type, UNVOICED ) || ( st->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    6958             :             {
    6959           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6960           0 :                 L_tmp = L_min( L_tmp, L_shl_sat( 2, sub( 31, exp ) ) ); /*31 - exp*/
    6961           0 :                 scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );   /*Q12*/
    6962           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6963           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6964           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6965           0 :                 temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6966             :             }
    6967             :         }
    6968             : 
    6969       19287 :         FOR( j = 0; j < 8; j++ )
    6970             :         {
    6971       17144 :             GainShape_fx[2 * j] = shl_sat( mult_r( GainShape_fx[2 * j], scale_fx ), 3 );
    6972       17144 :             move16(); /* 15 +12 +3-15 =15*/
    6973       17144 :             GainShape_fx[add( 2 * j, 1 )] = shl_sat( mult_r( GainShape_fx[add( 2 * j, 1 )], scale_fx ), 3 );
    6974       17144 :             move16();
    6975      702904 :             FOR( i = 0; i < 40; i++ )
    6976             :             {
    6977      685760 :                 shaped_shb_excitation_fx[i + j * L_FRAME16k / 8] = shl( mult_r( shaped_shb_excitation_fx[i + j * L_FRAME16k / 8], scale_fx ), 3 );
    6978      685760 :                 move16(); /* Q_bwe_exc +12+3 -15  =Q_bwe_exc*/
    6979             :             }
    6980             : 
    6981       17144 :             IF( temp_fx > 0 )
    6982             :             {
    6983       17144 :                 IF( LT_16( scale_fx, temp_fx ) )
    6984             :                 {
    6985        2544 :                     scale_fx = shr( div_s( scale_fx, temp_fx ), 3 );
    6986             :                 }
    6987             :                 ELSE
    6988             :                 {
    6989       14600 :                     tmp1 = sub( norm_s( scale_fx ), 1 );
    6990       14600 :                     tmp2 = norm_s( temp_fx );
    6991       14600 :                     scale_fx = div_s( shl( scale_fx, tmp1 ), shl( temp_fx, tmp2 ) );
    6992       14600 :                     scale_fx = shr( scale_fx, add( sub( tmp1, tmp2 ), 3 ) );
    6993             :                 }
    6994             :             }
    6995             :             ELSE
    6996             :             {
    6997           0 :                 scale_fx = 0;
    6998           0 :                 move16();
    6999             :             }
    7000             :         }
    7001             :     }
    7002             : 
    7003      104728 :     hBWE_TD->prev_swb_bwe_frame_pow_fx = curr_frame_pow_fx;
    7004      104728 :     move32();
    7005      104728 :     hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    7006      104728 :     move16();
    7007             : 
    7008      104728 :     Word64 prev_ener_shb64 = 0;
    7009      104728 :     move64();
    7010    33617688 :     FOR( i = 0; i < L_FRAME16k; i++ )
    7011             :     {
    7012    33512960 :         prev_ener_shb64 = W_mac0_16_16( prev_ener_shb64, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] ); /* Q0 */
    7013             :     }
    7014      104728 :     L_prev_ener_shb = W_sat_l( prev_ener_shb64 );
    7015             : 
    7016      104728 :     L_prev_ener_shb = Mult_32_16( L_prev_ener_shb, 26214 ); /* 2*Q_bwe_exc_mod+8; 26214=(1/L_FRAME16k) in Q23 */
    7017      104728 :     st->prev_ener_shb_fx = 0;
    7018      104728 :     move16();
    7019      104728 :     IF( L_prev_ener_shb != 0 )
    7020             :     {
    7021      104688 :         exp = norm_l( L_prev_ener_shb );
    7022      104688 :         tmp = extract_h( L_shl( L_prev_ener_shb, exp ) );
    7023      104688 :         exp = sub( exp, sub( 30, ( add( i_mult( 2, Q_bwe_exc ), 8 ) ) ) );
    7024             : 
    7025      104688 :         tmp = div_s( 16384, tmp );
    7026      104688 :         L_tmp = L_deposit_h( tmp );
    7027      104688 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    7028      104688 :         st->prev_ener_shb_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    7029      104688 :         move16();
    7030             :     }
    7031             : 
    7032      104728 :     IF( st->hBWE_FD != NULL )
    7033             :     {
    7034      104728 :         L_tmp = Mult_32_16( curr_frame_pow_fx, 26214 ); /* curr_frame_pow_exp+8; 26214=(1/L_FRAME16k) in Q23 */
    7035      104728 :         tmp = 0;
    7036      104728 :         move16();
    7037      104728 :         IF( L_tmp != 0 )
    7038             :         {
    7039      104170 :             exp = norm_l( L_tmp );
    7040      104170 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    7041      104170 :             exp = sub( exp, sub( 30, add( curr_frame_pow_exp, 8 ) ) );
    7042             : 
    7043      104170 :             tmp = div_s( 16384, tmp );
    7044      104170 :             L_tmp = L_deposit_h( tmp );
    7045      104170 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    7046      104170 :             tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    7047             :         }
    7048      104728 :         set16_fx( st->hBWE_FD->prev_SWB_fenv_fx, tmp, SWB_FENV ); /* Q1 */
    7049             :     }
    7050             : 
    7051    33617688 :     FOR( i = 0; i < L_FRAME16k; i++ )
    7052             :     {
    7053    33512960 :         shaped_shb_excitation_fx_32[i] = L_shl( shaped_shb_excitation_fx[i], sub( Q11, Q_bwe_exc ) );
    7054    33512960 :         move32();
    7055             :     }
    7056             : 
    7057             :     /* generate 32kHz SHB synthesis from 12.8(16)kHz signal */
    7058      104728 :     GenSHBSynth_fx32( shaped_shb_excitation_fx_32, error_fx, hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, st->L_frame, &( hBWE_TD->syn_dm_phase ) );
    7059      104728 :     Copy_Scale_sig_32_16( st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, 2 * ALLPASSSECTIONS_STEEP, -( Q11 - Q_bwe_exc ) );
    7060      104728 :     Copy32( error_fx + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx_32, L_SHB_TRANSITION_LENGTH );
    7061             : 
    7062             :     /* resample SHB synthesis (if needed) and scale down */
    7063      104728 :     synth_scale_fx = 32767;
    7064      104728 :     move16(); /* 1.0 in Q15 */
    7065      104728 :     if ( EQ_16( st->codec_mode, MODE1 ) )
    7066             :     {
    7067      104728 :         synth_scale_fx = 29491;
    7068      104728 :         move16(); /* 0.9 in Q15 */
    7069             :     }
    7070             : 
    7071      104728 :     IF( EQ_32( st->output_Fs, 48000 ) )
    7072             :     {
    7073       72708 :         IF( EQ_16( st->extl, FB_TBE ) )
    7074             :         {
    7075       35133 :             tmp = norm_l( GainFrame_fx );
    7076       35133 :             if ( GainFrame_fx == 0 )
    7077             :             {
    7078           0 :                 tmp = 31;
    7079           0 :                 move16();
    7080             :             }
    7081       35133 :             L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
    7082             : 
    7083       35133 :             tmp1 = 0;
    7084       35133 :             move16();
    7085             : 
    7086       35133 :             Word32 idx32 = L_shr_r( 0x00333333, 10 ); /*NUM_SHB_SUBFR/L_FRAME16k*/ // Q16
    7087             : 
    7088    11277693 :             FOR( i = 0; i < L_FRAME16k; i++ )
    7089             :             {
    7090             :                 Word16 idx;
    7091    11242560 :                 idx = extract_h( imult3216( idx32, i ) );                                  /*Q0*/
    7092    11242560 :                 L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[idx] );                           /* Q : 18 + tmp +15 -15*/
    7093    11242560 :                 White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
    7094    11242560 :                 move16();
    7095    11242560 :                 tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
    7096             :             }
    7097             : 
    7098       35133 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
    7099       35133 :             move16();
    7100       35133 :             tmp = norm_s( tmp1 );
    7101       35133 :             if ( tmp1 == 0 )
    7102             :             {
    7103          22 :                 tmp = 15;
    7104          22 :                 move16();
    7105             :             }
    7106             : 
    7107    11277693 :             FOR( i = 0; i < L_FRAME16k; i++ )
    7108             :             {
    7109    11242560 :                 White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
    7110    11242560 :                 move16();
    7111             :             }
    7112       35133 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
    7113       35133 :             move16();
    7114             :         }
    7115             : 
    7116       72708 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    7117             :         {
    7118    46605828 :             FOR( i = 0; i < L_FRAME32k; i++ )
    7119             :             {
    7120    46533120 :                 error_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
    7121    46533120 :                 move32();
    7122             :             }
    7123             :         }
    7124       72708 :         interpolate_3_over_2_allpass_fx32( error_fx, L_FRAME32k, synth_fx, hBWE_TD->int_3_over_2_tbemem_dec_fx_32 );
    7125             :     }
    7126       32020 :     ELSE IF( EQ_32( st->output_Fs, 32000 ) )
    7127             :     {
    7128       27660 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    7129             :         {
    7130    17730060 :             FOR( i = 0; i < L_FRAME32k; i++ )
    7131             :             {
    7132    17702400 :                 synth_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
    7133    17702400 :                 move32(); /*Qx*/
    7134             :             }
    7135             :         }
    7136             :         ELSE
    7137             :         {
    7138           0 :             Copy32( error_fx, synth_fx, L_FRAME32k );
    7139             :         }
    7140             :     }
    7141        4360 :     ELSE IF( EQ_32( st->output_Fs, 16000 ) )
    7142             :     {
    7143        4360 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    7144             :         {
    7145     2794760 :             FOR( i = 0; i < L_FRAME32k; i++ )
    7146             :             {
    7147     2790400 :                 error_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
    7148     2790400 :                 move32();
    7149             :             }
    7150             :         }
    7151             : 
    7152        4360 :         Decimate_allpass_steep_fx32( error_fx, hBWE_TD->mem_resamp_HB_32k_fx_32, L_FRAME32k, synth_fx );
    7153             :     }
    7154             : 
    7155             :     /* Update previous frame parameters for FEC */
    7156      104728 :     Copy( lsf_shb_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    7157      104728 :     IF( EQ_16( st->codec_mode, MODE1 ) )
    7158             :     {
    7159      104728 :         hBWE_TD->GainFrame_prevfrm_fx = GainFrame_fx;
    7160      104728 :         move32(); /*Q18*/
    7161      104728 :         hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec_fx;
    7162      104728 :         move16();
    7163             : 
    7164      104728 :         if ( !st->bfi )
    7165             :         {
    7166      102585 :             hBWE_TD->GainAttn_fx = 32767; /*1.0f in Q15*/
    7167      102585 :             move16();
    7168             :         }
    7169             :     }
    7170             :     ELSE
    7171             :     {
    7172           0 :         IF( !st->bfi )
    7173             :         {
    7174           0 :             hBWE_TD->GainFrame_prevfrm_fx = GainFrame_fx;
    7175           0 :             move32(); /*Q18*/
    7176           0 :             hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec_fx;
    7177           0 :             move16();
    7178           0 :             hBWE_TD->GainAttn_fx = 32767; /*1.0f in Q15*/
    7179           0 :             move16();
    7180             :         }
    7181             :     }
    7182             : 
    7183      104728 :     hBWE_TD->prev_ener_fx = ener_tmp_fx[NUM_SHB_SUBGAINS - 1];
    7184      104728 :     move32();
    7185      104728 :     hBWE_TD->prev_GainShape_fx = GainShape_fx[NUM_SHB_SUBFR - 1];
    7186      104728 :     move16();
    7187      104728 :     hBWE_TD->prev_Q_bwe_syn2 = Q_bwe_exc;
    7188      104728 :     move16();
    7189      104728 :     hBWE_TD->prev_Qx = Q_bwe_exc;
    7190      104728 :     move16();
    7191             : 
    7192      104728 :     return;
    7193             : }

Generated by: LCOV version 1.14