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

Generated by: LCOV version 1.14