LCOV - code coverage report
Current view: top level - lib_enc - swb_tbe_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ d6739f868d005b89f6b8a19b0accc1255692b205 Lines: 2963 3582 82.7 %
Date: 2025-11-21 02:40:13 Functions: 43 45 95.6 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Nov 04, 2021. Version 16.4.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdlib.h>
       6             : #include "options.h"
       7             : #include "cnst.h"
       8             : #include "rom_com.h"
       9             : #include "prot_fx.h"     /* Function prototypes                    */
      10             : #include "prot_fx_enc.h" /* Function prototypes                    */
      11             : #include "stl.h"
      12             : #include "ivas_prot_fx.h"
      13             : 
      14             : 
      15             : /*-----------------------------------------------------------------*
      16             :  * Local constants
      17             :  *-----------------------------------------------------------------*/
      18             : 
      19             : #define ENVSHBRES_ACORR_MIN 40 /* minimum lag for calculating autocorrelation function on SHB residual TD envelope */
      20             : #define ENVSHBRES_ACORR_MAX 80 /* maximum lag for calculating autocorrelation function on SHB residual TD envelope */
      21             : 
      22             : /*-----------------------------------------------------------------*
      23             :  * Local functions
      24             :  *-----------------------------------------------------------------*/
      25             : 
      26             : static void return_M_Least_fx_GainFrame( const Word32 *inp, const Word32 *codebook, const Word16 num_grp, const Word16 interNum, Word16 *least );
      27             : 
      28             : static void singlevectortest_gain_fx( const Word32 *inp, const Word16 dimen, const Word16 cb_size, Word16 *index, Word32 *recon, const Word32 *codebook );
      29             : 
      30             : static void determine_gain_weights_fx( const Word32 *gain, Word16 *weights, const Word16 dims );
      31             : 
      32             : static void QuantizeSHBsubgains_fx( Encoder_State *st_fx, Word16 *subgains, const Word16 extl );
      33             : 
      34             : static void QuantizeSHBsubgains_ivas_fx( Encoder_State *st_fx, Word16 subgains[], const Word16 extl );
      35             : 
      36             : static void QuantizeSHBframegain_fx( Encoder_State *st_fx, Word32 *GainFrame, const Word16 extl, Word32 extl_brate, Word16 *rf_gainFrame_ind );
      37             : 
      38             : static void QuantizeSHBframegain_ivas_fx( Encoder_State *st_fx, Word32 *GainFrame, const Word16 extl, Word32 extl_brate, Word16 *rf_gainFrame_ind, const Word16 flag_conservative );
      39             : 
      40             : static Word16 closest_centroid_fx( const Word16 *data, const Word16 *weights, const Word16 *quantizer, const Word16 centroids, const Word16 length );
      41             : 
      42             : static Word16 closest_centroid_lc_fx( const Word16 *data, const Word16 *quantizer, const Word16 centroids );
      43             : 
      44             : static void EstimateSHBFrameGain_fx( const Word16 length, const Word16 *oriSHB, const Word16 Q_oriSHB, const Word16 *synSHB, const Word16 Q_synSHB, Word16 *subgain, Word32 *GainFrame, const Word16 *win_shb, const Word16 *subwin_shb, const Word16 n_subfr_saturation );
      45             : 
      46             : static void EstimateSHBFrameGain_ivas_fx( const Word16 length, const Word16 *oriSHB, const Word16 Q_oriSHB, const Word16 *synSHB, const Word16 Q_synSHB, Word16 *subgain, Word32 *GainFrame, const Word16 *win_shb, const Word16 *subwin_shb, const Word16 n_subfr_saturation );
      47             : 
      48             : static void EstimateSHBGainShape_fx( const Word16 length, const Word16 *oriSHB, const Word16 Q_oriSHB, const Word16 *synSHB, const Word16 Q_synSHB, Word16 *subgain, const Word16 *subwin, Word16 *n_subfr_saturation, const Flag limit_min_gain );
      49             : 
      50             : static Word32 pow_off_pk_fx( Word16 a[], Word16 len, Word16 step );
      51             : 
      52             : static Word32 pow_off_pk_corrected_fx( Word16 a[], Word16 len, Word16 step );
      53             : 
      54             : static void find_max_mem_enc( Encoder_State *st_fx, Word16 *n_mem, Word16 *n_mem2 );
      55             : 
      56             : static void rescale_genSHB_mem_enc( Encoder_State *st_fx, Word16 sf );
      57             : 
      58             : static void find_max_mem_wb_enc( Encoder_State *st_fx, Word16 *n_mem );
      59             : 
      60             : static void rescale_genWB_mem_enc( Encoder_State *st_fx, Word16 sf );
      61             : 
      62             : static void Quant_lower_LSF_fx( const Word16 lsf[], Word16 lsf_q[], Word16 lsf_idx[] );
      63             : 
      64             : static Word16 Quant_mirror_point_fx( const Word16 lsf[], const Word16 lsf_q[], Word16 *m );
      65             : 
      66             : static Word16 Find_LSF_grid_fx( const Word16 lsf[], Word16 lsf_q[], const Word16 m );
      67             : 
      68             : static void Quant_BWE_LSF_fx( Encoder_State *st_fx, const Word16 lsp_shb[], Word16 Q_lsfs[] );
      69             : 
      70             : static void Quant_BWE_LSF_ivas_fx( BSTR_ENC_HANDLE hBstr, TD_BWE_ENC_HANDLE hBWE_TD, const Word16 codec_mode, const Word16 lsf_shb_fx[], Word16 Q_lsfs_fx[], const Word32 extl_brate );
      71             : 
      72             : static void Quant_shb_ener_sf_fx( Encoder_State *st_fx, Word32 *shb_ener_sf_fx_32, Word16 Q_shb );
      73             : 
      74             : static void Quant_shb_ener_sf_ivas_fx( Encoder_State *st_fx, Word32 *shb_ener_sf_Q31, Word16 Q_ener );
      75             : 
      76             : static void Quant_shb_res_gshape_fx( Encoder_State *st_fx, Word16 *shb_res_gshape_fx );
      77             : 
      78             : static void Quant_shb_res_gshape_ivas_fx( Encoder_State *st, Word16 shb_res_gshape_fx[] );
      79             : 
      80             : static void gainFrSmooth_En_fx( Encoder_State *st_fx, Word16 *shb_frame_fx, const Word16 *lpc_shb_fx, const Word16 *lsp_shb_fx, Word16 *MA_lsp_shb_spacing, Word16 *frGainAttenuate, Word16 *frGainSmoothEn );
      81             : 
      82             : static void gainFrSmooth_En_ivas_fx( Encoder_State *st_fx, Word16 *shb_frame_fx /* Q_in */, const Word16 *lpc_shb_fx /* Q12 */, const Word16 *lsp_shb_fx /* Q15 */, Word16 *MA_lsp_shb_spacing /* Q15 */, Word16 *frGainAttenuate /* Q0 */, Word16 *frGainSmoothEn /* Q0 */ );
      83             : 
      84             : 
      85             : /*-------------------------------------------------------------------*
      86             :  * find_max_mem_enc()
      87             :  *
      88             :  * Find norm and max in TBE memories and past buffers
      89             :  *-------------------------------------------------------------------*/
      90             : 
      91      148674 : void find_max_mem_enc(
      92             :     Encoder_State *st_fx,
      93             :     Word16 *n_mem,
      94             :     Word16 *n_mem2 )
      95             : {
      96             :     Word16 i;
      97             :     Word16 n_mem_32;
      98      148674 :     Word16 max = 0;
      99      148674 :     move16();
     100      148674 :     Word32 Lmax = 0;
     101      148674 :     move32();
     102      148674 :     Word16 tempQ15, max2 = 0;
     103      148674 :     move16();
     104      148674 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
     105             : 
     106             :     /* old BWE exc max */
     107     1932762 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     108             :     {
     109     1784088 :         tempQ15 = abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] );
     110     1784088 :         max = s_max( max, tempQ15 );
     111             :     }
     112             : 
     113             :     /* decimate all-pass steep memory */
     114     1189392 :     FOR( i = 0; i < ( 2 * ALLPASSSECTIONS_STEEP + 1 ); i++ )
     115             :     {
     116     1040718 :         tempQ15 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
     117     1040718 :         max = s_max( max, tempQ15 );
     118             :     }
     119             : 
     120             :     /*  -- keep norm of state_lpc_syn_fx, state_syn_shbexc_fx,
     121             :            and mem_stp_swb_fx separately for 24.4 and 32kbps ----*/
     122             :     /* findMaxMem2() inside tbe com */
     123     1635414 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     124             :     {
     125     1486740 :         tempQ15 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
     126     1486740 :         max2 = s_max( max2, tempQ15 );
     127             :     }
     128             : 
     129             :     /* findMaxMem2() inside tbe com */
     130     3122154 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
     131             :     {
     132     2973480 :         tempQ15 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
     133     2973480 :         max2 = s_max( max2, tempQ15 );
     134             :     }
     135             : 
     136             :     /* findMaxMem2() inside tbe com */
     137     1635414 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     138             :     {
     139     1486740 :         tempQ15 = abs_s( hBWE_TD->mem_stp_swb_fx[i] );
     140     1486740 :         max2 = s_max( max2, tempQ15 );
     141             :     }
     142             : 
     143             :     /* for total_brate > 16.4kbps, use n_mem2; else account for the max2 for n_mem calculation */
     144      148674 :     *n_mem2 = norm_s( max2 );
     145      148674 :     move16();
     146      148674 :     if ( max2 == 0 )
     147             :     {
     148      120960 :         *n_mem2 = 15;
     149      120960 :         move16();
     150             :     }
     151      148674 :     IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
     152             :     {
     153      113689 :         max = s_max( max, max2 );
     154             :     }
     155             : 
     156             :     /* de-emph and pre-emph memory */
     157      148674 :     tempQ15 = abs_s( hBWE_TD->tbe_demph_fx );
     158      148674 :     max = s_max( max, tempQ15 );
     159             : 
     160      148674 :     tempQ15 = abs_s( hBWE_TD->tbe_premph_fx );
     161      148674 :     max = s_max( max, tempQ15 );
     162             : 
     163      148674 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
     164             :     {
     165      678887 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     166             :         {
     167      617170 :             tempQ15 = abs_s( hBWE_TD->fb_state_lpc_syn_fx[i] );
     168      617170 :             max = s_max( max, tempQ15 );
     169             :         }
     170             :         /* FB de-emph memory */
     171       61717 :         tempQ15 = abs_s( hBWE_TD->fb_tbe_demph_fx );
     172       61717 :         max = s_max( max, tempQ15 );
     173             :     }
     174             :     /* estimate the norm for 16-bit memories  */
     175      148674 :     *n_mem = norm_s( max );
     176      148674 :     move16();
     177      148674 :     if ( max == 0 )
     178             :     {
     179        1095 :         *n_mem = 15;
     180        1095 :         move16();
     181             :     }
     182             : 
     183             :     /* estimate the norm for 32-bit memories */
     184      148674 :     Lmax = L_abs( hBWE_TD->mem_csfilt_fx[0] ); /* only element [0] is used in env. shaping */
     185             : 
     186      148674 :     n_mem_32 = norm_l( Lmax );
     187      148674 :     if ( Lmax == 0 )
     188             :     {
     189       51768 :         n_mem_32 = 31;
     190       51768 :         move16();
     191             :     }
     192             : 
     193      148674 :     tempQ15 = sub( s_min( *n_mem, n_mem_32 ), 1 );
     194      148674 :     *n_mem = s_max( tempQ15, 0 );
     195      148674 :     move16();
     196             : 
     197      148674 :     return;
     198             : }
     199             : 
     200             : 
     201             : /*-------------------------------------------------------------------*
     202             :  * rescale_genSHB_mem_enc()
     203             :  *
     204             :  * Rescale genSHB memories
     205             :  *-------------------------------------------------------------------*/
     206             : 
     207       51270 : void rescale_genSHB_mem_enc(
     208             :     Encoder_State *st_fx,
     209             :     Word16 sf )
     210             : {
     211             :     Word16 i;
     212       51270 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
     213             : 
     214      666510 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     215             :     {
     216      615240 :         hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
     217      615240 :         move16();
     218             :     }
     219             : 
     220      410160 :     FOR( i = 0; i < 7; i++ )
     221             :     {
     222      358890 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
     223      358890 :         move16();
     224             :     }
     225             : 
     226             :     /*  -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
     227       51270 :     IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
     228             :     {
     229      437767 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     230             :         {
     231      397970 :             hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
     232      397970 :             move16();
     233             :         }
     234             : 
     235      835737 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
     236             :         {
     237      795940 :             hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
     238      795940 :             move16();
     239             :         }
     240             :     }
     241             : 
     242       51270 :     hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
     243       51270 :     move32();
     244             : 
     245       51270 :     hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
     246       51270 :     move16();
     247       51270 :     hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
     248       51270 :     move16();
     249       51270 : }
     250             : 
     251        7291 : void find_max_mem_wb_enc( Encoder_State *st_fx, Word16 *n_mem )
     252             : {
     253             :     Word16 i;
     254             :     Word16 n_mem_32;
     255        7291 :     Word16 max_16 = 0;
     256        7291 :     Word32 Lmax = 0;
     257        7291 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
     258        7291 :     move16();
     259        7291 :     move32();
     260             : 
     261       94783 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     262       87492 :     max_16 = s_max( max_16, abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] ) );
     263             : 
     264       58328 :     FOR( i = 0; i < 7; i++ )
     265             :     {
     266       51037 :         IF( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] ), max_16 ) )
     267        5471 :         max_16 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
     268             :     }
     269             : 
     270       58328 :     FOR( i = 0; i < 7; i++ )
     271             :     {
     272       51037 :         IF( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] ), max_16 ) )
     273        4333 :         max_16 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] );
     274             :     }
     275             : 
     276       58328 :     FOR( i = 0; i < 7; i++ )
     277             :     {
     278       51037 :         IF( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] ), max_16 ) )
     279        1281 :         max_16 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] );
     280             :     }
     281             : 
     282       80201 :     FOR( i = 0; i < 10; i++ )
     283             :     {
     284       72910 :         IF( GT_16( abs_s( hBWE_TD->state_lpc_syn_fx[i] ), max_16 ) )
     285        1922 :         max_16 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
     286             :     }
     287             : 
     288       43746 :     FOR( i = 0; i < 5; i++ )
     289             :     {
     290       36455 :         IF( GT_16( abs_s( hBWE_TD->state_syn_shbexc_fx[i] ), max_16 ) )
     291           0 :         max_16 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
     292             :     }
     293             : 
     294        7291 :     IF( max_16 == 0 )
     295             :     {
     296          14 :         *n_mem = 15;
     297          14 :         move16();
     298             :     }
     299             :     ELSE
     300             :     {
     301        7277 :         *n_mem = norm_s( max_16 );
     302        7277 :         move16();
     303             :     }
     304             : 
     305       21873 :     FOR( i = 0; i < 2; i++ )
     306             :     {
     307       14582 :         IF( GT_32( L_abs( hBWE_TD->mem_csfilt_fx[i] ), Lmax ) )
     308        6938 :         Lmax = L_abs( hBWE_TD->mem_csfilt_fx[i] );
     309             :     }
     310             : 
     311             : 
     312        7291 :     IF( Lmax == 0 )
     313             :     {
     314         353 :         n_mem_32 = 31;
     315         353 :         move16();
     316             :     }
     317             :     ELSE
     318             :     {
     319        6938 :         n_mem_32 = norm_l( Lmax );
     320             :     }
     321             : 
     322        7291 :     *n_mem = sub( s_min( *n_mem, n_mem_32 ), 1 );
     323        7291 :     move16();
     324        7291 :     *n_mem = s_max( *n_mem, 0 );
     325        7291 :     move16();
     326        7291 : }
     327             : 
     328        7291 : void rescale_genWB_mem_enc( Encoder_State *st_fx, Word16 sf )
     329             : {
     330             :     Word16 i;
     331        7291 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
     332             : 
     333       94783 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     334             :     {
     335       87492 :         hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
     336       87492 :         move16();
     337             :     }
     338             : 
     339       80201 :     FOR( i = 0; i < 10; i++ )
     340             :     {
     341       72910 :         hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
     342       72910 :         move16();
     343             :     }
     344             : 
     345       43746 :     FOR( i = 0; i < 5; i++ )
     346             :     {
     347       36455 :         hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
     348       36455 :         move16();
     349             :     }
     350             : 
     351       58328 :     FOR( i = 0; i < 7; i++ )
     352             :     {
     353       51037 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
     354       51037 :         move16();
     355             :     }
     356             : 
     357       58328 :     FOR( i = 0; i < 7; i++ )
     358             :     {
     359       51037 :         hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i], sf );
     360       51037 :         move16();
     361             :     }
     362             : 
     363       58328 :     FOR( i = 0; i < 7; i++ )
     364             :     {
     365       51037 :         hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i], sf );
     366       51037 :         move16();
     367             :     }
     368             : 
     369       21873 :     FOR( i = 0; i < 2; i++ )
     370             :     {
     371       14582 :         hBWE_TD->mem_csfilt_fx[i] = L_shl( hBWE_TD->mem_csfilt_fx[i], sf );
     372       14582 :         move32();
     373             :     }
     374        7291 : }
     375             : 
     376             : 
     377             : /*-------------------------------------------------------------------*
     378             :  * InitSWBencBuffer()
     379             :  *
     380             :  * Initialize SWB buffers
     381             :  *-------------------------------------------------------------------*/
     382             : 
     383           5 : void InitSWBencBuffer_fx(
     384             :     TD_BWE_ENC_HANDLE hBWE_TD /* i/o: TD BWE data handle  */
     385             : )
     386             : {
     387             :     Word16 i;
     388             : 
     389           5 :     set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
     390           5 :     hBWE_TD->bwe_seed[0] = 23;
     391           5 :     move16();
     392           5 :     hBWE_TD->bwe_seed[1] = 59;
     393           5 :     move16();
     394           5 :     set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
     395           5 :     hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
     396           5 :     move32();
     397             : 
     398           5 :     set16_fx( hBWE_TD->state_ana_filt_shb_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     399           5 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[0], 0, 4 );
     400           5 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[1], 0, 4 );
     401           5 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[2], 0, 4 );
     402           5 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[3], 0, 4 );
     403           5 :     hBWE_TD->prev_fb_energy_fx = 0;
     404           5 :     move16();
     405             : 
     406           5 :     set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
     407           5 :     set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
     408           5 :     set16_fx( hBWE_TD->old_input_fhb_fx, 0, NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2 );
     409             : 
     410             :     // IVAS_CODE
     411             :     // InitSWBencBufferStates_fx(hBWE_TD, NULL );
     412           5 :     set16_fx( hBWE_TD->prev_lsp_shb_fx, 0, LPC_SHB_ORDER );
     413             : 
     414           5 :     hBWE_TD->cldfbHBLT = 8192 /*1.0f Q13*/;
     415           5 :     move16();
     416           5 :     hBWE_TD->prev_gainFr_SHB_fx = 0;
     417           5 :     move32();
     418           5 :     set16_fx( hBWE_TD->lsp_shb_slow_interpl_fx, 0, LPC_SHB_ORDER );
     419           5 :     set16_fx( hBWE_TD->lsp_shb_fast_interpl_fx, 0, LPC_SHB_ORDER );
     420           5 :     set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
     421           5 :     set16_fx( hBWE_TD->lsp_shb_spacing_fx, 3277, 3 );
     422           5 :     set16_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, 0, 4 );
     423             : 
     424           5 :     hBWE_TD->prev_swb_GainShape_fx = 0;
     425           5 :     move16();
     426           5 :     hBWE_TD->prev_frGainAtten = 0;
     427           5 :     move16();
     428           5 :     hBWE_TD->prev_wb_GainShape = 0;
     429           5 :     move16();
     430           5 :     hBWE_TD->prev_Q_bwe_exc_fb = 51;
     431           5 :     move16();
     432           5 :     set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     433           5 :     hBWE_TD->fb_tbe_demph_fx = 0;
     434           5 :     move16();
     435           5 :     hBWE_TD->tilt_mem_fx = 0;
     436           5 :     move16();
     437             : 
     438           5 :     hBWE_TD->prev_coder_type = GENERIC;
     439           5 :     move16();
     440           5 :     set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384, LPC_SHB_ORDER - 2 );
     441           5 :     hBWE_TD->prev_tilt_para_fx = 0;
     442           5 :     move16();
     443           5 :     set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
     444             : 
     445             :     /* TD BWE post-processing */
     446           5 :     hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
     447           5 :     set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
     448             : 
     449          55 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     450             :     {
     451          50 :         hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
     452          50 :         move16();
     453             :     }
     454             : 
     455           5 :     set16_fx( hBWE_TD->dec_2_over_3_mem_fx, 0, 12 );
     456           5 :     set16_fx( hBWE_TD->dec_2_over_3_mem_lp_fx, 0, 6 );
     457             : 
     458             : 
     459           5 :     hBWE_TD->prev_fb_energy_fx_Q = 0;
     460           5 :     move16();
     461             : 
     462           5 :     hBWE_TD->prev_Q_bwe_exc = 31;
     463           5 :     move16();
     464           5 :     hBWE_TD->prev_Q_bwe_syn = 31;
     465           5 :     move16();
     466           5 :     set16_fx( hBWE_TD->prev_lsp_wb_fx, 0, 6 );
     467           5 :     set16_fx( hBWE_TD->prev_lsp_wb_temp_fx, 0, 6 );
     468           5 :     set16_fx( hBWE_TD->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
     469             : 
     470           5 :     return;
     471             : }
     472             : 
     473             : 
     474             : /*-------------------------------------------------------------------*
     475             :  * InitSWBencBufferStates()
     476             :  *
     477             :  * Initialize SWB buffer states
     478             :  *-------------------------------------------------------------------*/
     479             : 
     480       11291 : void InitSWBencBuffer_ivas_fx(
     481             :     TD_BWE_ENC_HANDLE hBWE_TD /* i/o: TD BWE data handle  */
     482             : )
     483             : {
     484             :     Word16 i;
     485             : 
     486       11291 :     set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
     487       11291 :     hBWE_TD->bwe_seed[0] = 23;
     488       11291 :     move16();
     489       11291 :     hBWE_TD->bwe_seed[1] = 59;
     490       11291 :     move16();
     491       11291 :     set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
     492       11291 :     hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
     493       11291 :     move32();
     494             : 
     495       11291 :     set16_fx( hBWE_TD->state_ana_filt_shb_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
     496       11291 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[0], 0, 4 );
     497       11291 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[1], 0, 4 );
     498       11291 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[2], 0, 4 );
     499       11291 :     set32_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx[3], 0, 4 );
     500       11291 :     hBWE_TD->prev_fb_energy_fx = 0;
     501       11291 :     move16();
     502             : 
     503       11291 :     set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
     504       11291 :     set16_fx( hBWE_TD->old_speech_wb_fx, 0, ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16 );
     505       11291 :     set16_fx( hBWE_TD->old_input_fhb_fx, 0, NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2 );
     506       11291 :     hBWE_TD->old_input_fhb_fx_Q = 0;
     507       11291 :     move16();
     508             : 
     509       11291 :     InitSWBencBufferStates_fx( hBWE_TD, NULL );
     510             : 
     511      124201 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     512             :     {
     513      112910 :         hBWE_TD->prev_lsp_shb_fx[i] = lsp_shb_prev_tbl_swb_tbe_enc_fx[i];
     514      112910 :         move16();
     515             :     }
     516             : 
     517       11291 :     hBWE_TD->cldfbHBLT = 8192; /* 1.0f Q13 */
     518       11291 :     move16();
     519       11291 :     hBWE_TD->prev_gainFr_SHB_fx = 0;
     520       11291 :     move32();
     521       11291 :     set16_fx( hBWE_TD->lsp_shb_slow_interpl_fx, 0, LPC_SHB_ORDER );
     522       11291 :     set16_fx( hBWE_TD->lsp_shb_fast_interpl_fx, 0, LPC_SHB_ORDER );
     523       11291 :     set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
     524       11291 :     set16_fx( hBWE_TD->lsp_shb_spacing_fx, 3277 /* 0.1f in Q15 */, 3 );
     525       11291 :     set16_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, 0, 4 );
     526             : 
     527       11291 :     hBWE_TD->prev_swb_GainShape_fx = 0;
     528       11291 :     move16();
     529       11291 :     hBWE_TD->prev_frGainAtten = 0;
     530       11291 :     move16();
     531       11291 :     hBWE_TD->prev_wb_GainShape = 0;
     532       11291 :     move16();
     533       11291 :     hBWE_TD->prev_Q_bwe_exc_fb = 51;
     534       11291 :     move16();
     535       11291 :     set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     536       11291 :     hBWE_TD->fb_tbe_demph_fx = 0;
     537       11291 :     move16();
     538       11291 :     hBWE_TD->tilt_mem_fx = 0;
     539       11291 :     move16();
     540             : 
     541       11291 :     hBWE_TD->prev_coder_type = GENERIC;
     542       11291 :     move16();
     543       11291 :     set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384 /* 0.5f in Q15 */, LPC_SHB_ORDER - 2 );
     544       11291 :     hBWE_TD->prev_tilt_para_fx = 0;
     545       11291 :     move16();
     546       11291 :     set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
     547             : 
     548             :     /* TD BWE post-processing */
     549       11291 :     hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
     550       11291 :     set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
     551             : 
     552      124201 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     553             :     {
     554      112910 :         hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
     555      112910 :         move16();
     556             :     }
     557             : 
     558       11291 :     set16_fx( hBWE_TD->dec_2_over_3_mem_fx, 0, 12 );
     559       11291 :     set16_fx( hBWE_TD->dec_2_over_3_mem_lp_fx, 0, 6 );
     560             : 
     561       11291 :     hBWE_TD->prev_fb_energy_fx_Q = 0;
     562       11291 :     move16();
     563             : 
     564       11291 :     hBWE_TD->prev_Q_bwe_exc = 15;
     565       11291 :     move16();
     566       11291 :     hBWE_TD->prev_Q_bwe_syn = 15;
     567       11291 :     move16();
     568       11291 :     set16_fx( hBWE_TD->prev_lsp_wb_fx, 0, 6 );
     569       11291 :     set16_fx( hBWE_TD->prev_lsp_wb_temp_fx, 0, 6 );
     570       11291 :     set16_fx( hBWE_TD->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
     571             : 
     572       11291 :     return;
     573             : }
     574             : 
     575             : 
     576             : /*-------------------------------------------------------------------*
     577             :  * ResetSHBbuffer_Enc()
     578             :  *
     579             :  *-------------------------------------------------------------------*/
     580             : 
     581       11296 : void ResetSHBbuffer_Enc_fx(
     582             :     TD_BWE_ENC_HANDLE hBWE_TD /* i/o: TD BWE data handle  */
     583             : )
     584             : {
     585             :     /* states for the filters used in generating SHB excitation from WB excitation*/
     586       11296 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     587       11296 :     set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
     588             : 
     589             :     /* states for the filters used in generating SHB signal from SHB excitation*/
     590       11296 :     set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
     591       11296 :     set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     592       11296 :     set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     593       11296 :     hBWE_TD->fb_tbe_demph_fx = 0;
     594       11296 :     move16();
     595             : 
     596             :     /* states for the filters used in generating WB signal from WB excitation*/
     597       11296 :     set16_fx( hBWE_TD->decim_state1_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     598       11296 :     set16_fx( hBWE_TD->decim_state2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     599       11296 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     600       11296 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     601             : 
     602             :     /* overlap buffer used to Adjust SHB Frame Gain */
     603       11296 :     set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
     604       11296 :     hBWE_TD->gain_prec_swb_fx = 16384; /*Q14=1 */
     605       11296 :     move16();
     606       11296 :     set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
     607       11296 :     hBWE_TD->tbe_demph_fx = 0;
     608       11296 :     move16();
     609       11296 :     hBWE_TD->tbe_premph_fx = 0;
     610       11296 :     move16();
     611             : 
     612       11296 :     return;
     613             : }
     614             : 
     615             : 
     616             : /*==========================================================================*/
     617             : /* FUNCTION : void wb_tbe_enc_fx() */
     618             : /*--------------------------------------------------------------------------*/
     619             : /* PURPOSE : WB TBE encoder, 6 - 8 kHz band encoding module */
     620             : /*--------------------------------------------------------------------------*/
     621             : /* INPUT ARGUMENTS : */
     622             : /* Word16 coder_type i : coding type */
     623             : /* Word16 *new_speech i : original input signal Q0 */
     624             : /* Word32 *bwe_exc_extended i : bandwidth extended exciatation 2*Q_new*/
     625             : /* Word16 voice_factors[] i : voicing factors Q15 */
     626             : /* Word16 pitch_buf[] i : pitch for each subframe Q6 */
     627             : /*--------------------------------------------------------------------------*/
     628             : /* OUTPUT ARGUMENTS : */
     629             : /* Word16 *synth o : WB SHB final synthesis */
     630             : /*--------------------------------------------------------------------------*/
     631             : /* INPUT/OUTPUT ARGUMENTS : */
     632             : /* Encoder_State *st_fx i/o: encoder state structure */
     633             : /*--------------------------------------------------------------------------*/
     634             : /* RETURN ARGUMENTS : */
     635             : /* _ None */
     636             : /*--------------------------------------------------------------------------*/
     637             : /* */
     638             : /*==========================================================================*/
     639             : 
     640             : #define WBTBE_LOOK_LSUFBR_5_OVER_16 ( L_LOOK_12k8 + L_SUBFR ) * 5 / 16
     641             : #define WBTBE_LSUBFR_5_OVER_16      L_SUBFR * 5 / 16
     642             : #define WBTBE_ANA_ALIGNDELAY        -( L_SHB_LAHEAD / 4 + 5 )
     643             : #define LFRAME16K_OVER_4            L_FRAME16k / 4
     644             : #define WBTBE_LPCWIN_LENGTH         ( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16 - 1
     645             : 
     646           0 : void wb_tbe_enc_fx(
     647             :     Encoder_State *st_fx,           /* i/o: encoder state structure             */
     648             :     const Word16 coder_type,        /* i  : coding type                         */
     649             :     const Word16 *hb_speech,        /* i  : HB target signal (6-8kHz) at 16kHz at Q-1 */
     650             :     const Word32 *bwe_exc_extended, /* i  : bandwidth extended exciatation      */
     651             :     const Word16 Q_new,             /* i  : input HB speech Q factor */
     652             :     const Word16 voice_factors[],   /* i  : voicing factors                     */
     653             :     const Word16 pitch_buf[],       /* i  : pitch for each subframe             */
     654             :     const Word16 voicing_fx[]       /* i  : OL maximum normalized correlation   */
     655             : )
     656             : {
     657             :     Word16 i, j;
     658             :     Word16 hb_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
     659             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
     660             : 
     661             :     Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
     662             :     Word16 exc4kWhtnd[L_FRAME16k / 4];
     663             :     /*Word16 ana_align_delay = WBTBE_ANA_ALIGNDELAY;  */ /* -L_SHB_LAHEAD/4 - 5 */
     664             :     Word32 GainFrame;
     665             :     Word16 GainShape[NUM_SHB_SUBFR];
     666             :     Word16 lpc_wb[LPC_SHB_ORDER_WB + 1];
     667             :     Word32 lpc_wb_32_fx[LPC_SHB_ORDER_WB + 1];
     668           0 :     Word16 lsp_wb[LPC_SHB_ORDER_WB], weights_lsp[LPC_SHB_ORDER_WB] = { 32767, 32767 };
     669           0 :     move16();
     670           0 :     move16();
     671             :     Word16 *hb_new_speech, *hb_frame /*, hb_speech[ L_FRAME16k ]*/;
     672             :     Word16 R_h[LPC_SHB_ORDER_WB + 2], R_l[LPC_SHB_ORDER_WB + 2];
     673             :     Word16 Q_R;
     674             :     Word32 LepsP[LPC_SHB_ORDER_WB + 1];
     675             : 
     676             :     Word32 prev_pow, curr_pow, Lscale;
     677             :     /* Word16 scale; */
     678             :     /*Word16 ramp_flag;*/
     679             :     Word32 p2m_in, p2m_out;
     680             :     /*Word16 cnt, max =0;*/
     681             :     Word16 n_mem, Q_bwe_exc, Q_bwe_exc_ext, exp_out; /* Q_hb_frame; */
     682             :     Word32 L_tmp, Lmax;
     683             :     Word16 tmp, exp, Q_out, sc;
     684           0 :     Word16 Q_ns = -1;
     685           0 :     move16();
     686             : 
     687             :     Word16 pitBufAvg_fx, voicingBufAvg_fx;
     688             :     Word16 vf_modified_fx[NB_SUBFR16k];
     689             :     Word16 temp_wb_fac_fx, feedback_fx;
     690             :     Word16 lsp_spacing_fx;
     691             :     Word16 lsp_wb_temp_fx[LPC_SHB_ORDER_WB], lpc_wb_temp_fx[LPC_SHB_ORDER_WB + 1];
     692             :     Word32 L_feedback;
     693             :     Word16 frac, exp1;
     694             :     Word16 uv_flag;
     695           0 :     Word16 dummy = 0;
     696           0 :     move16();
     697             :     Word16 avg_voice_fac;
     698           0 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
     699           0 :     RF_ENC_HANDLE hRF = st_fx->hRF;
     700             : 
     701             :     /*Word16 att = 32767;*/
     702             : 
     703           0 :     hb_new_speech = hb_old_speech + WBTBE_LOOK_LSUFBR_5_OVER_16;
     704           0 :     hb_frame = hb_old_speech + WBTBE_LSUBFR_5_OVER_16 + WBTBE_ANA_ALIGNDELAY;
     705             : 
     706           0 :     Copy( hBWE_TD->old_speech_wb_fx, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
     707           0 :     Copy( hb_speech, hb_new_speech, LFRAME16K_OVER_4 );
     708           0 :     Copy( hb_old_speech + LFRAME16K_OVER_4, hBWE_TD->old_speech_wb_fx, WBTBE_LOOK_LSUFBR_5_OVER_16 );
     709             : 
     710           0 :     test();
     711           0 :     test();
     712           0 :     test();
     713           0 :     test();
     714           0 :     test();
     715           0 :     test();
     716           0 :     IF( ( NE_16( st_fx->last_extl, WB_TBE ) && NE_16( st_fx->last_extl, WB_BWE ) ) && ( st_fx->clas == UNVOICED_CLAS || ( LT_16( voicing_fx[0], 16384 ) && LT_16( voicing_fx[1], 16384 ) && LT_16( voicing_fx[2], 16384 ) ) ) && st_fx->igf == 0 )
     717             :     {
     718             :         /* In case of unvoiced signals after switching cores, back-propagate the target signal */
     719           0 :         Copy( hb_speech, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
     720             : 
     721           0 :         i = WBTBE_LOOK_LSUFBR_5_OVER_16;
     722           0 :         move16();
     723             : 
     724           0 :         FOR( j = 0; j < L_SUBFR16k; j = j + 4 )
     725             :         {
     726           0 :             L_tmp = L_mult( hb_old_speech[i], ola_win_shb_switch_fold_fx[j] );
     727           0 :             hb_old_speech[i] = mac_r( L_tmp, hb_speech[j], ola_win_shb_switch_fold_fx[L_SUBFR16k - 4 - j] );
     728           0 :             move16();
     729           0 :             i--;
     730             :         }
     731             :     }
     732             : 
     733           0 :     autocorr_fx( hb_old_speech, LPC_SHB_ORDER_WB + 1, R_h, R_l, &Q_R, WBTBE_LPCWIN_LENGTH, win_lpc_hb_wb_fx, 0, 1 );
     734             : 
     735           0 :     if ( st_fx->element_mode > EVS_MONO )
     736             :     {
     737             :         /* Ensure R[0] isn't zero when entering Levinson-Durbin */
     738           0 :         R_l[0] = s_max( R_l[0], 1 );
     739           0 :         move16();
     740             :     }
     741             : 
     742           0 :     E_LPC_lev_dur( R_h, R_l, lpc_wb_temp_fx, LepsP, LPC_SHB_ORDER_WB, NULL );
     743             : 
     744           0 :     Copy_Scale_sig( lpc_wb_temp_fx, lpc_wb_temp_fx, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb_temp_fx[0] ), 2 ) );
     745             : 
     746             :     /* convert into lsps and calculate weights */
     747           0 :     FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
     748             :     {
     749           0 :         lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb_temp_fx[i] ), 1 ) );
     750           0 :         move32();
     751             :     }
     752             : 
     753           0 :     lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb_temp_fx, hBWE_TD->prev_lsp_wb_temp_fx, LPC_SHB_ORDER_WB );
     754             : 
     755           0 :     FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     756             :     {
     757           0 :         hBWE_TD->prev_lsp_wb_temp_fx[i] = lsp_wb_temp_fx[i];
     758           0 :         move16();
     759             :     }
     760             : 
     761             :     /* lsp_spacing_fx = 16384;                                             move16(); */
     762           0 :     lsp_spacing_fx = lsp_wb_temp_fx[0];
     763           0 :     move16();
     764           0 :     FOR( i = 1; i < LPC_SHB_ORDER_WB; i++ )
     765             :     {
     766             :         /*if ( i == 0 )
     767             :         {
     768             :             tmp = lsp_wb_temp_fx[0]; move16();
     769             :         }
     770             :         else
     771             :         {*/
     772           0 :         tmp = sub( lsp_wb_temp_fx[i], lsp_wb_temp_fx[i - 1] );
     773             :         /*} */
     774             : 
     775           0 :         lsp_spacing_fx = s_min( lsp_spacing_fx, tmp );
     776             :     }
     777             : 
     778             :     /* Spectral smoothing of autocorrelation coefficients */
     779           0 :     FOR( i = 1; i <= LPC_SHB_ORDER_WB; i++ )
     780             :     {
     781           0 :         L_tmp = Mpy_32( R_h[i], R_l[i], wac_h[i - 1], wac_l[i - 1] );
     782           0 :         L_Extract( L_tmp, &R_h[i], &R_l[i] );
     783             :     }
     784           0 :     R_l[0] = s_max( R_l[0], 1 );
     785           0 :     move16();
     786             : 
     787           0 :     test();
     788           0 :     IF( EQ_16( st_fx->rf_mode, 1 ) || EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
     789             :     {
     790           0 :         E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_LBR_WB, NULL );
     791           0 :         Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_LBR_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
     792             : 
     793             :         /* Expand bandwidth of the LP coeffs */
     794           0 :         FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
     795             :         {
     796           0 :             lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
     797           0 :             move16();
     798             :         }
     799             : 
     800             :         /* convert into lsps and calculate weights */
     801           0 :         FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
     802             :         {
     803           0 :             lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
     804           0 :             move32(); /*Q27 */
     805             :         }
     806           0 :         lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, hBWE_TD->prev_lsp_wb_fx, LPC_SHB_ORDER_LBR_WB );
     807             : 
     808           0 :         FOR( i = 0; i < LPC_SHB_ORDER_LBR_WB; i++ )
     809             :         {
     810           0 :             hBWE_TD->prev_lsp_wb_fx[i] = lsp_wb[i];
     811           0 :             move16();
     812             :         }
     813             : 
     814           0 :         lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_LBR_WB, &Q_out );
     815             : 
     816             :         /* Quantization of LSFs */
     817           0 :         i = closest_centroid_fx( lsp_wb, weights_lsp, lbr_wb_bwe_lsfvq_cbook_2bit_fx, 4, LPC_SHB_ORDER_LBR_WB );
     818           0 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
     819             :         {
     820           0 :             hBWE_TD->lsf_WB = i;
     821           0 :             move16();
     822             :         }
     823             :         ELSE
     824             :         {
     825           0 :             push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_LBR_WB_LSF );
     826             :         }
     827             : 
     828           0 :         Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i * LPC_SHB_ORDER_LBR_WB, lsp_wb, LPC_SHB_ORDER_LBR_WB );
     829             : 
     830           0 :         lsp2lpc_fx( &lpc_wb[1], lsp_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
     831             : 
     832           0 :         set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
     833           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     834             :         {
     835           0 :             hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
     836           0 :             move16();
     837             :         }
     838             : 
     839           0 :         FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
     840             :         {
     841           0 :             lpc_wb[i] = negate( lpc_wb[i] );
     842           0 :             move16();
     843             :         }
     844           0 :         lpc_wb[0] = 4096;
     845           0 :         move16();
     846             :     }
     847             :     ELSE /* 13.2kbps */
     848             :     {
     849           0 :         E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_WB, NULL );
     850           0 :         Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
     851             : 
     852             :         /* Expand bandwidth of the LP coeffs */
     853           0 :         FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
     854             :         {
     855           0 :             lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
     856           0 :             move16();
     857             :         }
     858             : 
     859             :         /* convert into lsps and calculate weights */
     860           0 :         FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
     861             :         {
     862           0 :             lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
     863           0 :             move32(); /*Q27 */
     864             :         }
     865             : 
     866           0 :         lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, hBWE_TD->prev_lsp_wb_fx, LPC_SHB_ORDER_WB );
     867             : 
     868           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     869             :         {
     870           0 :             hBWE_TD->prev_lsp_wb_fx[i] = lsp_wb[i];
     871           0 :             move16();
     872             :         }
     873             : 
     874           0 :         lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_WB, &Q_out );
     875             : 
     876             :         /* Quantization of LSFs */
     877           0 :         i = closest_centroid_fx( lsp_wb, weights_lsp, wb_bwe_lsfvq_cbook_8bit_fx, 256, LPC_SHB_ORDER_WB ); /*move16(); */
     878             : 
     879           0 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
     880             :         {
     881           0 :             hBWE_TD->lsf_WB = i;
     882           0 :             move16();
     883             :         }
     884             :         ELSE
     885             :         {
     886           0 :             push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_WB_LSF );
     887             :         }
     888           0 :         Copy( wb_bwe_lsfvq_cbook_8bit_fx + i * LPC_SHB_ORDER_WB, lsp_wb, LPC_SHB_ORDER_WB );
     889             : 
     890           0 :         lsp2lpc_fx( &lpc_wb[1], lsp_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
     891             : 
     892           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     893             :         {
     894           0 :             hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
     895           0 :             move16();
     896             :         }
     897           0 :         FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
     898             :         {
     899           0 :             lpc_wb[i] = negate( lpc_wb[i] );
     900           0 :             move16();
     901             :         }
     902           0 :         lpc_wb[0] = 4096;
     903           0 :         move16();
     904             :     }
     905             : 
     906           0 :     uv_flag = 0;
     907           0 :     move16();
     908           0 :     test();
     909           0 :     if ( EQ_32( st_fx->extl_brate, WB_TBE_1k05 ) && EQ_16( st_fx->coder_type_raw, UNVOICED ) )
     910             :     {
     911           0 :         uv_flag = 1;
     912           0 :         move16();
     913             :     }
     914             : 
     915           0 :     Copy( voice_factors, vf_modified_fx, NB_SUBFR16k );
     916           0 :     IF( EQ_16( coder_type, VOICED ) )
     917             :     {
     918           0 :         FOR( i = 1; i < NB_SUBFR; i++ )
     919             :         {
     920             :             /*vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i-1];*/
     921           0 :             vf_modified_fx[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6553, voice_factors[i - 1] ) );
     922             :         }
     923           0 :         IF( NE_16( st_fx->L_frame, L_FRAME ) )
     924             :         {
     925           0 :             vf_modified_fx[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6553, voice_factors[3] ) );
     926             :         }
     927             :     }
     928             : 
     929             :     /* From low band excitation, generate highband excitation */
     930           0 :     Lmax = L_deposit_l( 0 );
     931           0 :     FOR( i = 0; i < L_FRAME32k; i++ )
     932             :     {
     933           0 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended[i] ) );
     934             :     }
     935             : 
     936           0 :     Q_bwe_exc = 31;
     937           0 :     move16();
     938           0 :     if ( Lmax != 0 )
     939             :     {
     940           0 :         Q_bwe_exc = norm_l( Lmax );
     941             :     }
     942           0 :     Q_bwe_exc = sub( Q_bwe_exc, 3 );
     943           0 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
     944             : 
     945           0 :     find_max_mem_wb_enc( st_fx, &n_mem );
     946             : 
     947           0 :     IF( GT_16( sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_exc ), n_mem ) )
     948             :     {
     949           0 :         Q_bwe_exc = add( hBWE_TD->prev_Q_bwe_exc, n_mem );
     950             :     }
     951             : 
     952           0 :     IF( uv_flag )
     953             :     {
     954           0 :         if ( GT_16( Q_bwe_exc, 20 ) )
     955             :         {
     956           0 :             Q_bwe_exc = 20;
     957           0 :             move16(); /* restrict this to 20 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
     958             :         }
     959             :     }
     960             : 
     961           0 :     prev_pow = 0;
     962           0 :     move32();
     963           0 :     IF( st_fx->element_mode > EVS_MONO )
     964             :     {
     965           0 :         tmp = sub( shl( sub( hBWE_TD->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
     966           0 :         prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
     967             :     }
     968           0 :     FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
     969             :     {
     970           0 :         prev_pow = L_mac0( prev_pow, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /* Q(2*st_fx->prev_Q_bwe_exc) */
     971             :     }
     972             : 
     973           0 :     rescale_genWB_mem_enc( st_fx, sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_exc ) );
     974             : 
     975           0 :     Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
     976           0 :     sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
     977           0 :     FOR( i = 0; i < L_FRAME32k; i++ )
     978             :     {
     979           0 :         bwe_exc_extended_16[i + NL_BUFF_OFFSET] = round_fx( L_shl( bwe_exc_extended[i], sc ) );
     980           0 :         move16();
     981             :     }
     982           0 :     Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
     983             : 
     984           0 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     985           0 :     Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
     986             : 
     987           0 :     GenShapedWBExcitation_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
     988           0 :                               hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx,
     989           0 :                               hBWE_TD->state_lpc_syn_fx, coder_type, bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed,
     990           0 :                               vf_modified_fx, uv_flag, st_fx->igf );
     991             : 
     992           0 :     curr_pow = 0;
     993           0 :     move16();
     994           0 :     IF( st_fx->element_mode > EVS_MONO )
     995             :     {
     996           0 :         tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
     997           0 :         curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
     998             :     }
     999           0 :     FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1000             :     {
    1001           0 :         curr_pow = L_mac0( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
    1002             :     }
    1003             : 
    1004           0 :     IF( GT_16( voice_factors[0], 24576 ) )
    1005             :     {
    1006           0 :         curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
    1007             :     }
    1008             : 
    1009           0 :     Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow, shl_r( sub( hBWE_TD->prev_Q_bwe_exc, 16 ), 1 ), &exp );
    1010             : 
    1011           0 :     FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
    1012             :     {
    1013           0 :         L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] );             /* Q(16-exp+Q_bwe_exc_ext) */
    1014           0 :         shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1015           0 :         move16();
    1016             :     }
    1017             : 
    1018           0 :     Lscale = root_a_fx( Lscale, 31 - exp, &exp );
    1019           0 :     L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] );     /* Q(16-exp+Q_bwe_exc_ext) */
    1020           0 :     shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1021           0 :     move16();
    1022             :     /* Update WB excitation */
    1023           0 :     Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
    1024             : 
    1025           0 :     EstimateSHBGainShape_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns,
    1026             :                              shaped_wb_excitation, Q_bwe_exc_ext, GainShape, subwin_wb_fx, NULL, 1 );
    1027             : 
    1028             :     /* Gain frame adjustment factor */
    1029           0 :     test();
    1030           0 :     IF( GainShape[0] && hBWE_TD->prev_wb_GainShape )
    1031             :     {
    1032           0 :         exp = norm_s( hBWE_TD->prev_wb_GainShape );
    1033           0 :         tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_wb_GainShape );
    1034           0 :         L_tmp = L_mult( GainShape[0], tmp ); /* Q(30 - exp) */
    1035             : 
    1036           0 :         exp1 = norm_l( L_tmp );
    1037           0 :         frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
    1038           0 :         exp1 = sub( exp, exp1 );
    1039           0 :         L_tmp = Mpy_32_16( exp1, frac, 22713 );
    1040           0 :         temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
    1041             :     }
    1042             :     ELSE
    1043             :     {
    1044           0 :         temp_wb_fac_fx = 0;
    1045           0 :         move16();
    1046             :     }
    1047           0 :     L_feedback = L_mult0( temp_wb_fac_fx, temp_wb_fac_fx );
    1048           0 :     FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    1049             :     {
    1050             :         /* temp_swb_fac = (float)log( (GainShape[i]+0.00001f) / (GainShape[i-1]+0.0001f) ); */
    1051           0 :         test();
    1052           0 :         IF( GainShape[i] && GainShape[i - 1] )
    1053             :         {
    1054           0 :             exp = norm_s( GainShape[i - 1] );
    1055           0 :             tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape[i - 1] );
    1056           0 :             L_tmp = L_mult( GainShape[i], tmp ); /*Q(30 - exp) */
    1057             : 
    1058           0 :             exp1 = norm_l( L_tmp );
    1059           0 :             frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
    1060           0 :             move16();
    1061           0 :             exp1 = sub( exp, exp1 );
    1062           0 :             L_tmp = Mpy_32_16( exp1, frac, 22713 );
    1063           0 :             temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
    1064             :         }
    1065             :         ELSE
    1066             :         {
    1067           0 :             temp_wb_fac_fx = 0;
    1068           0 :             move16();
    1069             :         }
    1070             : 
    1071           0 :         L_feedback = L_mac( L_feedback, temp_wb_fac_fx, temp_wb_fac_fx );
    1072             :     }
    1073           0 :     L_tmp = L_add( L_shr( L_feedback, 1 ), 1 << 21 ); /* Q30 */
    1074             : 
    1075           0 :     IF( L_tmp != 0 )
    1076             :     {
    1077           0 :         exp = norm_l( L_tmp );
    1078           0 :         tmp = extract_h( L_shl( L_tmp, exp ) );
    1079           0 :         exp = sub( sub( 30, exp ), 21 );
    1080           0 :         tmp = div_s( 16384, tmp );                  /* Q(15+exp)         */
    1081           0 :         L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /* Q31 */
    1082           0 :         feedback_fx = round_fx( L_tmp );            /* Q15 */
    1083             :     }
    1084             :     ELSE
    1085             :     {
    1086           0 :         feedback_fx = 8738;
    1087           0 :         move16(); /* Q15 */
    1088             :     }
    1089             : 
    1090           0 :     temp_wb_fac_fx = hBWE_TD->prev_wb_GainShape;
    1091           0 :     move16();
    1092           0 :     FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    1093             :     {
    1094           0 :         GainShape[i] = add( mult_r( sub( 32767, feedback_fx ), GainShape[i] ), mult_r( feedback_fx, temp_wb_fac_fx ) );
    1095           0 :         move16();
    1096           0 :         temp_wb_fac_fx = GainShape[i];
    1097           0 :         move16();
    1098             :     }
    1099             : 
    1100           0 :     hBWE_TD->prev_wb_GainShape = GainShape[sub( shr( NUM_SHB_SUBFR, 2 ), 1 )];
    1101           0 :     move16();
    1102           0 :     p2m_in = pow_off_pk_fx( GainShape, shr( NUM_SHB_SUBFR, 2 ), 1 );
    1103           0 :     move16();
    1104             : 
    1105           0 :     IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1106             :     {
    1107           0 :         FOR( i = 0; i < 8; i++ )
    1108             :         {
    1109           0 :             GainShape[i] = RECIP_ROOT_EIGHT_FX;
    1110           0 :             move16();
    1111             :         }
    1112             :     }
    1113             :     ELSE
    1114             :     {
    1115           0 :         push_indice( st_fx->hBstr, IND_UV_FLAG, uv_flag, 1 );
    1116             : 
    1117             :         /* Quantization of the subframe gain parameter */
    1118           0 :         QuantizeSHBsubgains_fx( st_fx, GainShape, st_fx->extl );
    1119             :     }
    1120             : 
    1121             :     /* Compute the power of gains away from the peak gain after quantization */
    1122           0 :     p2m_out = pow_off_pk_fx( GainShape, NUM_SHB_SUBFR / 2, 2 );
    1123             : 
    1124             :     /* Estimate the gain parameter */
    1125           0 :     EstimateSHBFrameGain_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns, shaped_wb_excitation, Q_bwe_exc_ext, GainShape,
    1126             :                              &GainFrame, window_wb_fx, subwin_wb_fx, 0 );
    1127             : 
    1128             :     /* If there's a big difference in the power of gains away from the peak gain */
    1129             :     /* due to poor quantization then suppress energy of the high band. */
    1130             : 
    1131           0 :     IF( GT_32( p2m_out, L_shl( p2m_in, 1 ) ) )
    1132             :     {
    1133           0 :         L_tmp = root_a_over_b_fx( L_shl( p2m_in, 1 ), 29, p2m_out, 29, &exp_out );
    1134           0 :         GainFrame = L_shl( Mult_32_32( GainFrame, L_tmp ), exp_out ); /* Q18 */
    1135             :     }
    1136             : 
    1137           0 :     pitBufAvg_fx = 0;
    1138           0 :     move16();
    1139             : 
    1140           0 :     FOR( i = 0; i < NB_SUBFR; i++ )
    1141             :     {
    1142           0 :         pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[i], 82 ) ); /*Q6 */
    1143             :     }
    1144           0 :     voicingBufAvg_fx = 0;
    1145           0 :     move16();
    1146           0 :     FOR( i = 0; i < 3; i++ )
    1147             :     {
    1148           0 :         voicingBufAvg_fx = add( voicingBufAvg_fx, mult_r( voicing_fx[i], 10912 ) ); /*Q15 */
    1149             :     }
    1150             :     /* GainFrame *= max(min((float)(pitBufAvg/voicingBufAvg), 1.0f), 0.7f); */
    1151           0 :     tmp = 32767;
    1152           0 :     move16();
    1153           0 :     IF( voicingBufAvg_fx > 0 )
    1154             :     {
    1155           0 :         exp = norm_s( voicingBufAvg_fx );
    1156           0 :         tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
    1157           0 :         L_tmp = L_mult( pitBufAvg_fx, tmp );                       /* (21-exp) */
    1158           0 :         L_tmp = L_shl_sat( L_tmp, add( exp, 10 ) );
    1159           0 :         tmp = round_fx_sat( L_tmp ); /* Q15 */
    1160             :     }
    1161             : 
    1162           0 :     tmp = s_max( s_min( tmp, 32767 ), 22938 ); /* Q15 */
    1163           0 :     GainFrame = Mult_32_16( GainFrame, tmp );  /* Q18 */
    1164             : 
    1165           0 :     test();
    1166           0 :     IF( LT_16( lsp_spacing_fx, 328 ) && lsp_spacing_fx )
    1167             :     {
    1168           0 :         GainFrame = Mult_32_16( GainFrame, 21299 ); /* Q18 */
    1169             :     }
    1170             : 
    1171           0 :     IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    1172             :     {
    1173             :         /*wbbwe_em_factor = add( mult_r( 29491, st_fx->prev_wbbwe_em_factor_fx ), mult_r( 3277, wbbwe_em_factor ) ); */ /* Q15 */
    1174             :     }
    1175             : 
    1176             :     /*0.25f*sum_f(voice_factors, NB_SUBFR)*/
    1177           0 :     L_tmp = L_mult( voice_factors[0], 8192 );
    1178           0 :     FOR( i = 1; i < NB_SUBFR; i++ )
    1179             :     {
    1180           0 :         L_tmp = L_mac( L_tmp, voice_factors[i], 8192 );
    1181             :     }
    1182           0 :     avg_voice_fac = round_fx( L_tmp );
    1183             : 
    1184           0 :     test();
    1185           0 :     test();
    1186           0 :     IF( st_fx->igf != 0 && EQ_16( coder_type, VOICED ) )
    1187             :     {
    1188             :         /*GainFrame *= 0.5f;*/
    1189             : 
    1190           0 :         GainFrame = Mult_32_16( GainFrame, 16384 );
    1191             :     }
    1192           0 :     ELSE IF( st_fx->igf != 0 && GT_16( avg_voice_fac, 11469 ) ) /*Q15 -> 0.35f*/
    1193             :     {
    1194             :         /*GainFrame *= 0.75f;*/
    1195           0 :         GainFrame = Mult_32_16( GainFrame, 24576 );
    1196             :     }
    1197             : 
    1198             :     /* Quantization of the frame gain parameter */
    1199           0 :     QuantizeSHBframegain_fx( st_fx, &GainFrame, st_fx->extl, st_fx->extl_brate, &hRF->RF_bwe_gainFr_ind );
    1200             : 
    1201             :     /* Adjust the subframe and frame gain of the synthesized SHB signal */
    1202             :     /* Scale the shaped excitation*/
    1203             : 
    1204           0 :     ScaleShapedSHB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame, window_wb_fx, subwin_wb_fx,
    1205             :                        &Q_bwe_exc_ext, &dummy, dummy, dummy );
    1206             : 
    1207           0 :     hBWE_TD->prev_Q_bwe_exc = Q_bwe_exc;
    1208           0 :     move16();
    1209             : 
    1210           0 :     return;
    1211             : }
    1212             : 
    1213             : 
    1214             : /*==========================================================================*/
    1215             : /* FUNCTION : void wb_tbe_enc_ivas_fx() */
    1216             : /*--------------------------------------------------------------------------*/
    1217             : /* PURPOSE : WB TBE encoder, 6 - 8 kHz band encoding module */
    1218             : /*--------------------------------------------------------------------------*/
    1219             : /* INPUT ARGUMENTS : */
    1220             : /* Word16 *new_speech i : original input signal                     Q(-1) */
    1221             : /* Word32 *bwe_exc_extended i : bandwidth extended exciatation Q(2*Q_new) */
    1222             : /* Word16 voice_factors[] i : voicing factors                         Q15 */
    1223             : /* Word16 pitch_buf[] i : pitch for each subframe                      Q6 */
    1224             : /*--------------------------------------------------------------------------*/
    1225             : /* OUTPUT ARGUMENTS : */
    1226             : /* Word16 *synth o : WB SHB final synthesis */
    1227             : /*--------------------------------------------------------------------------*/
    1228             : /* INPUT/OUTPUT ARGUMENTS : */
    1229             : /* Encoder_State *st_fx i/o: encoder state structure */
    1230             : /*--------------------------------------------------------------------------*/
    1231             : /* RETURN ARGUMENTS : */
    1232             : /* _ None */
    1233             : /*--------------------------------------------------------------------------*/
    1234             : /* */
    1235             : /*==========================================================================*/
    1236             : 
    1237        7291 : void wb_tbe_enc_ivas_fx(
    1238             :     Encoder_State *st_fx,           /* i/o: encoder state structure                       */
    1239             :     const Word16 *hb_speech,        /* i  : HB target signal (6-8kHz) at 16kHz      Q(-1) */
    1240             :     const Word32 *bwe_exc_extended, /* i  : bandwidth extended exciatation     Q(2*Q_new) */
    1241             :     const Word16 Q_new,             /* i  : input HB speech Q factor                      */
    1242             :     const Word16 voice_factors[],   /* i  : voicing factors                           Q15 */
    1243             :     const Word16 pitch_buf[]        /* i  : pitch for each subframe                    Q6 */
    1244             : )
    1245             : {
    1246             :     Word16 i, j, k;
    1247             :     Word16 hb_old_speech[( L_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 16];
    1248             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    1249             : 
    1250             :     Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
    1251             :     Word16 shaped_wb_exc_scale[L_SHB_LAHEAD / 4];
    1252             :     Word16 exc4kWhtnd[L_FRAME16k / 4];
    1253             :     /*Word16 ana_align_delay = WBTBE_ANA_ALIGNDELAY;  */ /* -L_SHB_LAHEAD/4 - 5 */
    1254             :     Word32 GainFrame;
    1255             :     Word16 GainShape[NUM_SHB_SUBFR];
    1256             :     Word16 lpc_wb[LPC_SHB_ORDER_WB + 1];
    1257             :     Word32 lpc_wb_32_fx[LPC_SHB_ORDER_WB + 1];
    1258        7291 :     Word16 lsp_wb[LPC_SHB_ORDER_WB], weights_lsp[LPC_SHB_ORDER_WB] = { 32767, 32767 };
    1259        7291 :     move16();
    1260        7291 :     move16();
    1261             :     Word16 *hb_new_speech, *hb_frame /*, hb_speech[ L_FRAME16k ]*/;
    1262             :     Word16 R_h[LPC_SHB_ORDER_WB + 2], R_l[LPC_SHB_ORDER_WB + 2];
    1263             :     Word16 Q_R;
    1264             :     Word32 LepsP[LPC_SHB_ORDER_WB + 1];
    1265             : 
    1266             :     Word32 prev_pow, curr_pow, Lscale;
    1267             :     /* Word16 scale; */
    1268             :     /*Word16 ramp_flag;*/
    1269             :     Word32 p2m_in, p2m_out;
    1270             :     /*Word16 cnt, max =0;*/
    1271             :     Word16 n_mem, Q_bwe_exc, Q_bwe_exc_ext, exp_out, Q_cur_pow, Q_prev_pow; /* Q_hb_frame; */
    1272             :     Word32 L_tmp, Lmax;
    1273             :     Word16 tmp, exp, Q_out, sc;
    1274        7291 :     Word16 Q_ns = -1;
    1275        7291 :     move16();
    1276             : 
    1277             :     Word16 pitBufAvg_fx, voicingBufAvg_fx;
    1278             :     Word16 vf_modified_fx[NB_SUBFR16k];
    1279             :     Word16 temp_wb_fac_fx, feedback_fx;
    1280             :     Word16 lsp_spacing_fx;
    1281             :     Word16 lsp_wb_temp_fx[LPC_SHB_ORDER_WB], lpc_wb_temp_fx[LPC_SHB_ORDER_WB + 1];
    1282             :     Word32 L_feedback;
    1283             :     Word16 frac, exp1;
    1284             :     Word16 uv_flag;
    1285        7291 :     Word16 dummy = 0;
    1286             :     Word16 avg_voice_fac;
    1287        7291 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    1288        7291 :     move16();
    1289             : 
    1290             :     /*Word16 att = 32767;*/
    1291        7291 :     set16_fx( bwe_exc_extended_16, 0, L_FRAME32k + NL_BUFF_OFFSET );
    1292        7291 :     hb_new_speech = hb_old_speech + WBTBE_LOOK_LSUFBR_5_OVER_16;
    1293        7291 :     hb_frame = hb_old_speech + WBTBE_LSUBFR_5_OVER_16 + WBTBE_ANA_ALIGNDELAY;
    1294             : 
    1295        7291 :     Copy( hBWE_TD->old_speech_wb_fx, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
    1296        7291 :     Copy( hb_speech, hb_new_speech, LFRAME16K_OVER_4 );
    1297        7291 :     Copy( hb_old_speech + LFRAME16K_OVER_4, hBWE_TD->old_speech_wb_fx, WBTBE_LOOK_LSUFBR_5_OVER_16 );
    1298             : 
    1299        7291 :     test();
    1300        7291 :     test();
    1301        7291 :     test();
    1302        7291 :     test();
    1303        7291 :     test();
    1304        7291 :     test();
    1305        7291 :     IF( ( NE_16( st_fx->last_extl, WB_TBE ) && NE_16( st_fx->last_extl, WB_BWE ) ) && ( st_fx->clas == UNVOICED_CLAS || ( LT_16( st_fx->voicing_fx[0], 16384 ) && LT_16( st_fx->voicing_fx[1], 16384 ) && LT_16( st_fx->voicing_fx[2], 16384 ) ) ) && !st_fx->igf )
    1306             :     {
    1307             :         /* In case of unvoiced signals after switching cores, back-propagate the target signal */
    1308         126 :         Copy( hb_speech, hb_old_speech, WBTBE_LOOK_LSUFBR_5_OVER_16 );
    1309             : 
    1310         126 :         i = WBTBE_LOOK_LSUFBR_5_OVER_16;
    1311         126 :         move16();
    1312         126 :         k = 0;
    1313         126 :         move16();
    1314             : 
    1315        2646 :         FOR( j = 0; j < L_SUBFR16k; j = j + 4 )
    1316             :         {
    1317        2520 :             L_tmp = L_mult( hb_old_speech[i], ola_win_shb_switch_fold_fx[j] );
    1318        2520 :             hb_old_speech[i] = mac_r_sat( L_tmp, hb_speech[k], ola_win_shb_switch_fold_fx[L_SUBFR16k - 4 - j] );
    1319        2520 :             move16();
    1320        2520 :             i--;
    1321        2520 :             k++;
    1322             :         }
    1323             :     }
    1324             : 
    1325        7291 :     autocorr_fx( hb_old_speech, LPC_SHB_ORDER_WB + 1, R_h, R_l, &Q_R, ( ( NS2SA( INT_FS_12k8, 5000000L ) + L_SUBFR + L_FRAME ) * 5 / 16 ), win_lpc_hb_wb_ivas_fx, 0, 1 );
    1326             : 
    1327             :     /* Ensure R[0] isn't zero when entering Levinson-Durbin */
    1328        7291 :     R_l[0] = s_max( R_l[0], 1 );
    1329        7291 :     move16();
    1330             : 
    1331        7291 :     E_LPC_lev_dur( R_h, R_l, lpc_wb_temp_fx, LepsP, LPC_SHB_ORDER_WB, NULL ); // Since 0th index will be 1 in floating point buffer, in fixed point one has to take norm of 0th index to identify the Q-factor
    1332             : 
    1333        7291 :     Copy_Scale_sig( lpc_wb_temp_fx, lpc_wb_temp_fx, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb_temp_fx[0] ), 2 ) ); // Q12
    1334             : 
    1335             :     /* convert into lsps and calculate weights */
    1336       58328 :     FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
    1337             :     {
    1338       51037 :         lpc_wb_32_fx[i] = L_negate( L_deposit_h( shr( lpc_wb_temp_fx[i], 1 ) ) ); // Q27
    1339       51037 :         move32();
    1340             :     }
    1341             : 
    1342        7291 :     lpc2lsp_ivas_fx( &lpc_wb_32_fx[1], lsp_wb_temp_fx, hBWE_TD->prev_lsp_wb_temp_fx, LPC_SHB_ORDER_WB );
    1343             : 
    1344       51037 :     FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1345             :     {
    1346       43746 :         hBWE_TD->prev_lsp_wb_temp_fx[i] = lsp_wb_temp_fx[i];
    1347       43746 :         move16();
    1348             :     }
    1349             : 
    1350             :     /* lsp_spacing_fx = 16384;                                             move16(); */
    1351        7291 :     lsp_spacing_fx = lsp_wb_temp_fx[0];
    1352        7291 :     move16();
    1353       43746 :     FOR( i = 1; i < LPC_SHB_ORDER_WB; i++ )
    1354             :     {
    1355             :         /*if ( i == 0 )
    1356             :         {
    1357             :             tmp = lsp_wb_temp_fx[0]; move16();
    1358             :         }
    1359             :         else
    1360             :         {*/
    1361       36455 :         tmp = sub( lsp_wb_temp_fx[i], lsp_wb_temp_fx[i - 1] );
    1362             :         /*} */
    1363             : 
    1364       36455 :         lsp_spacing_fx = s_min( lsp_spacing_fx, tmp );
    1365             :     }
    1366             : 
    1367             :     /* Spectral smoothing of autocorrelation coefficients */
    1368       51037 :     FOR( i = 1; i <= LPC_SHB_ORDER_WB; i++ )
    1369             :     {
    1370       43746 :         L_tmp = Mpy_32( R_h[i], R_l[i], wac_h[i - 1], wac_l[i - 1] );
    1371       43746 :         L_Extract( L_tmp, &R_h[i], &R_l[i] );
    1372             :     }
    1373        7291 :     R_l[0] = s_max( R_l[0], 1 );
    1374        7291 :     move16();
    1375             : 
    1376        7291 :     test();
    1377        7291 :     IF( EQ_16( st_fx->rf_mode, 1 ) || EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1378             :     {
    1379        2442 :         E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_LBR_WB, NULL );
    1380        2442 :         Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_LBR_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
    1381             : 
    1382             :         /* Expand bandwidth of the LP coeffs */
    1383       14652 :         FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
    1384             :         {
    1385       12210 :             lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
    1386       12210 :             move16();
    1387             :         }
    1388             : 
    1389             :         /* convert into lsps and calculate weights */
    1390       14652 :         FOR( i = 0; i <= LPC_SHB_ORDER_LBR_WB; i++ )
    1391             :         {
    1392       12210 :             lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
    1393       12210 :             move32(); /*Q27 */
    1394             :         }
    1395        2442 :         lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, hBWE_TD->prev_lsp_wb_fx, LPC_SHB_ORDER_LBR_WB );
    1396             : 
    1397       12210 :         FOR( i = 0; i < LPC_SHB_ORDER_LBR_WB; i++ )
    1398             :         {
    1399        9768 :             hBWE_TD->prev_lsp_wb_fx[i] = lsp_wb[i];
    1400        9768 :             move16();
    1401             :         }
    1402             : 
    1403        2442 :         lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_LBR_WB, &Q_out );
    1404             : 
    1405             :         /* Quantization of LSFs */
    1406        2442 :         i = closest_centroid_fx( lsp_wb, weights_lsp, lbr_wb_bwe_lsfvq_cbook_2bit_fx, 4, LPC_SHB_ORDER_LBR_WB );
    1407        2442 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    1408             :         {
    1409           0 :             hBWE_TD->lsf_WB = i;
    1410           0 :             move16();
    1411             :         }
    1412             :         ELSE
    1413             :         {
    1414        2442 :             push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_LBR_WB_LSF );
    1415             :         }
    1416             : 
    1417        2442 :         Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i * LPC_SHB_ORDER_LBR_WB, lsp_wb, LPC_SHB_ORDER_LBR_WB );
    1418             : 
    1419        2442 :         lsp2lpc_fx( &lpc_wb[1], lsp_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
    1420             : 
    1421        2442 :         set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
    1422       17094 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1423             :         {
    1424       14652 :             hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
    1425       14652 :             move16();
    1426             :         }
    1427             : 
    1428       12210 :         FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
    1429             :         {
    1430        9768 :             lpc_wb[i] = negate( lpc_wb[i] );
    1431        9768 :             move16();
    1432             :         }
    1433        2442 :         lpc_wb[0] = 4096;
    1434        2442 :         move16();
    1435             :     }
    1436             :     ELSE /* 13.2kbps */
    1437             :     {
    1438        4849 :         E_LPC_lev_dur( R_h, R_l, lpc_wb, LepsP, LPC_SHB_ORDER_WB, NULL );
    1439        4849 :         Copy_Scale_sig( lpc_wb, lpc_wb, LPC_SHB_ORDER_WB + 1, sub( norm_s( lpc_wb[0] ), 2 ) );
    1440             : 
    1441             :         /* Expand bandwidth of the LP coeffs */
    1442       38792 :         FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
    1443             :         {
    1444       33943 :             lpc_wb[i] = mult_r( lpc_wb[i], lpc_weights_fx[i] );
    1445       33943 :             move16();
    1446             :         }
    1447             : 
    1448             :         /* convert into lsps and calculate weights */
    1449       38792 :         FOR( i = 0; i <= LPC_SHB_ORDER_WB; i++ )
    1450             :         {
    1451       33943 :             lpc_wb_32_fx[i] = L_negate( L_shr( L_deposit_h( lpc_wb[i] ), 1 ) );
    1452       33943 :             move32(); /*Q27 */
    1453             :         }
    1454             : 
    1455        4849 :         lpc2lsp_fx( &lpc_wb_32_fx[1], lsp_wb, hBWE_TD->prev_lsp_wb_fx, LPC_SHB_ORDER_WB );
    1456             : 
    1457       33943 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1458             :         {
    1459       29094 :             hBWE_TD->prev_lsp_wb_fx[i] = lsp_wb[i];
    1460       29094 :             move16();
    1461             :         }
    1462             : 
    1463        4849 :         lsp_weights_fx( lsp_wb, weights_lsp, LPC_SHB_ORDER_WB, &Q_out );
    1464             : 
    1465             :         /* Quantization of LSFs */
    1466        4849 :         i = closest_centroid_fx( lsp_wb, weights_lsp, wb_bwe_lsfvq_cbook_8bit_fx, 256, LPC_SHB_ORDER_WB ); /*move16(); */
    1467             : 
    1468        4849 :         push_indice( st_fx->hBstr, IND_SHB_LSF, i, NUM_BITS_WB_LSF );
    1469        4849 :         Copy( wb_bwe_lsfvq_cbook_8bit_fx + i * LPC_SHB_ORDER_WB, lsp_wb, LPC_SHB_ORDER_WB );
    1470             : 
    1471        4849 :         lsp2lpc_fx( &lpc_wb[1], lsp_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
    1472             : 
    1473       33943 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1474             :         {
    1475       29094 :             hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
    1476       29094 :             move16();
    1477             :         }
    1478       33943 :         FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
    1479             :         {
    1480       29094 :             lpc_wb[i] = negate( lpc_wb[i] );
    1481       29094 :             move16();
    1482             :         }
    1483        4849 :         lpc_wb[0] = 4096;
    1484        4849 :         move16();
    1485             :     }
    1486             : 
    1487        7291 :     uv_flag = 0;
    1488        7291 :     move16();
    1489        7291 :     test();
    1490        7291 :     if ( EQ_32( st_fx->extl_brate, WB_TBE_1k05 ) && EQ_16( st_fx->coder_type_raw, UNVOICED ) )
    1491             :     {
    1492         427 :         uv_flag = 1;
    1493         427 :         move16();
    1494             :     }
    1495             : 
    1496        7291 :     Copy( voice_factors, vf_modified_fx, NB_SUBFR16k );
    1497        7291 :     IF( EQ_16( st_fx->coder_type, VOICED ) )
    1498             :     {
    1499        4968 :         FOR( i = 1; i < NB_SUBFR; i++ )
    1500             :         {
    1501             :             /*vf_modified[i] = 0.8f * voice_factors[i] + 0.2f * voice_factors[i-1];*/
    1502        3726 :             vf_modified_fx[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6553, voice_factors[i - 1] ) );
    1503        3726 :             move16();
    1504             :         }
    1505        1242 :         IF( NE_16( st_fx->L_frame, L_FRAME ) )
    1506             :         {
    1507           0 :             vf_modified_fx[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6553, voice_factors[3] ) );
    1508           0 :             move16();
    1509             :         }
    1510             :     }
    1511             : 
    1512             :     /* From low band excitation, generate highband excitation */
    1513        7291 :     Lmax = L_deposit_l( 0 );
    1514     4673531 :     FOR( i = 0; i < L_FRAME32k; i++ )
    1515             :     {
    1516     4666240 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended[i] ) );
    1517             :     }
    1518             : 
    1519        7291 :     Q_bwe_exc = 31;
    1520        7291 :     move16();
    1521        7291 :     if ( Lmax != 0 )
    1522             :     {
    1523        7291 :         Q_bwe_exc = norm_l( Lmax );
    1524             :     }
    1525        7291 :     Q_bwe_exc = sub( Q_bwe_exc, 3 );
    1526        7291 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
    1527             : 
    1528        7291 :     find_max_mem_wb_enc( st_fx, &n_mem );
    1529             : 
    1530        7291 :     IF( GT_16( sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_exc ), n_mem ) )
    1531             :     {
    1532          87 :         Q_bwe_exc = add( hBWE_TD->prev_Q_bwe_exc, n_mem );
    1533             :     }
    1534             : 
    1535        7291 :     IF( uv_flag )
    1536             :     {
    1537         427 :         if ( GT_16( Q_bwe_exc, 20 ) )
    1538             :         {
    1539         200 :             Q_bwe_exc = 20;
    1540         200 :             move16(); /* restrict this to 20 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
    1541             :         }
    1542             :     }
    1543             : 
    1544        7291 :     prev_pow = 0;
    1545        7291 :     move32();
    1546        7291 :     Q_prev_pow = norm_arr( hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
    1547             :     /* Compare with the guard bits needed in the subsequent operations.
    1548             :      * Guard bits for L_SHB_LAHEAD / 4 (expression evaluates to 5) is 3.
    1549             :      */
    1550        7291 :     IF( LT_16( Q_prev_pow, 3 ) )
    1551             :     {
    1552           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1553             :         {
    1554           0 :             shaped_wb_exc_scale[i] = shr( hBWE_TD->state_syn_shbexc_fx[i], 3 );
    1555           0 :             move16();
    1556             :         }
    1557           0 :         Q_prev_pow = shl_r( sub( sub( hBWE_TD->prev_Q_bwe_exc, 16 ), 3 ), 1 );
    1558           0 :         move16();
    1559             :     }
    1560             :     ELSE
    1561             :     {
    1562       43746 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1563             :         {
    1564       36455 :             shaped_wb_exc_scale[i] = hBWE_TD->state_syn_shbexc_fx[i];
    1565       36455 :             move16();
    1566             :         }
    1567        7291 :         Q_prev_pow = shl_r( sub( hBWE_TD->prev_Q_bwe_exc, 16 ), 1 );
    1568        7291 :         move16();
    1569             :     }
    1570             : 
    1571        7291 :     IF( st_fx->element_mode > EVS_MONO )
    1572             :     {
    1573        7291 :         tmp = sub( Q_prev_pow, 31 + 16 );
    1574        7291 :         prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q_prev_pow*/
    1575             :     }
    1576       43746 :     FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1577             :     {
    1578       36455 :         prev_pow = L_mac0( prev_pow, shaped_wb_exc_scale[i], shaped_wb_exc_scale[i] ); /* Q_prev_pow */
    1579             :     }
    1580             : 
    1581        7291 :     rescale_genWB_mem_enc( st_fx, sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_exc ) );
    1582             : 
    1583        7291 :     sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
    1584     4673531 :     FOR( i = 0; i < L_FRAME32k; i++ )
    1585             :     {
    1586     4666240 :         bwe_exc_extended_16[i + NL_BUFF_OFFSET] = round_fx( L_shl( bwe_exc_extended[i], sc ) );
    1587     4666240 :         move16();
    1588             :     }
    1589             : 
    1590        7291 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
    1591        7291 :     Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
    1592             : 
    1593        7291 :     GenShapedWBExcitation_ivas_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
    1594        7291 :                                    hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx,
    1595        7291 :                                    hBWE_TD->state_lpc_syn_fx, st_fx->coder_type, bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed,
    1596        7291 :                                    vf_modified_fx, uv_flag, st_fx->igf );
    1597             : 
    1598        7291 :     curr_pow = 0;
    1599        7291 :     move32();
    1600        7291 :     Q_cur_pow = norm_arr( &shaped_wb_excitation[L_SHB_LAHEAD / 4], L_SHB_LAHEAD / 4 );
    1601             :     /* Compare with the guard bits needed in the subsequent operations.
    1602             :      * Guard bits for L_SHB_LAHEAD / 4 (expression evaluates to 5) is 3.
    1603             :      */
    1604        7291 :     IF( LT_16( Q_cur_pow, 3 ) )
    1605             :     {
    1606          48 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1607             :         {
    1608          40 :             shaped_wb_exc_scale[i] = shr( shaped_wb_excitation[i + L_SHB_LAHEAD / 4], 3 );
    1609          40 :             move16();
    1610             :         }
    1611           8 :         Q_cur_pow = shl_r( sub( Q_bwe_exc_ext, 3 ), 1 );
    1612           8 :         move16();
    1613             :     }
    1614             :     ELSE
    1615             :     {
    1616       43698 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1617             :         {
    1618       36415 :             shaped_wb_exc_scale[i] = shaped_wb_excitation[i + L_SHB_LAHEAD / 4];
    1619       36415 :             move16();
    1620             :         }
    1621        7283 :         Q_cur_pow = shl_r( Q_bwe_exc_ext, 1 );
    1622        7283 :         move16();
    1623             :     }
    1624        7291 :     IF( st_fx->element_mode > EVS_MONO )
    1625             :     {
    1626        7291 :         tmp = sub( Q_cur_pow, 31 + 16 );
    1627        7291 :         curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(Q_cur_pow)*/
    1628             :     }
    1629       43746 :     FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1630             :     {
    1631       36455 :         curr_pow = L_mac0( curr_pow, shaped_wb_exc_scale[i], shaped_wb_exc_scale[i] ); /* Q(Q_cur_pow) */
    1632             :     }
    1633             : 
    1634        7291 :     IF( GT_16( voice_factors[0], 24576 ) )
    1635             :     {
    1636         267 :         curr_pow = L_shr( curr_pow, 2 ); /* Q(Q_pow) */
    1637             :     }
    1638             : 
    1639        7291 :     Lscale = root_a_over_b_fx( curr_pow, Q_cur_pow, prev_pow, Q_prev_pow, &exp );
    1640             : 
    1641       36455 :     FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
    1642             :     {
    1643       29164 :         L_tmp = Mpy_32_16_1( Lscale, shaped_wb_excitation[i] );            /* Q(16-exp+Q_bwe_exc_ext) */
    1644       29164 :         shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1645       29164 :         move16();
    1646             :     }
    1647             : 
    1648        7291 :     Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
    1649        7291 :     L_tmp = Mpy_32_16_1( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] );            /* Q(16-exp+Q_bwe_exc_ext) */
    1650        7291 :     shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1651        7291 :     move16();
    1652             : 
    1653             :     /* Update WB excitation */
    1654        7291 :     Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
    1655             : 
    1656        7291 :     EstimateSHBGainShape_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns,
    1657             :                              shaped_wb_excitation, Q_bwe_exc_ext, GainShape, subwin_wb_fx, NULL, 1 );
    1658             : 
    1659             :     /* Gain frame adjustment factor */
    1660        7291 :     test();
    1661        7291 :     IF( GainShape[0] && hBWE_TD->prev_wb_GainShape )
    1662             :     {
    1663        7256 :         exp = norm_s( hBWE_TD->prev_wb_GainShape );
    1664        7256 :         tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_wb_GainShape );
    1665        7256 :         L_tmp = L_mult( GainShape[0], tmp ); /* Q(30 - exp) */
    1666             : 
    1667        7256 :         exp1 = norm_l( L_tmp );
    1668        7256 :         frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
    1669        7256 :         exp1 = sub( exp, exp1 );
    1670        7256 :         L_tmp = Mpy_32_16( exp1, frac, 22713 );
    1671        7256 :         temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
    1672             :     }
    1673             :     ELSE
    1674             :     {
    1675          35 :         temp_wb_fac_fx = 0;
    1676          35 :         move16();
    1677             :     }
    1678        7291 :     L_feedback = L_mult0( temp_wb_fac_fx, temp_wb_fac_fx );
    1679       29164 :     FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    1680             :     {
    1681             :         /* temp_swb_fac = (float)log( (GainShape[i]+0.00001f) / (GainShape[i-1]+0.0001f) ); */
    1682       21873 :         test();
    1683       21873 :         IF( GainShape[i] && GainShape[i - 1] )
    1684             :         {
    1685       21873 :             exp = norm_s( GainShape[i - 1] );
    1686       21873 :             tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape[i - 1] );
    1687       21873 :             L_tmp = L_mult( GainShape[i], tmp ); /*Q(30 - exp) */
    1688             : 
    1689       21873 :             exp1 = norm_l( L_tmp );
    1690       21873 :             frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
    1691       21873 :             exp1 = sub( exp, exp1 );
    1692       21873 :             L_tmp = Mpy_32_16( exp1, frac, 22713 );
    1693       21873 :             temp_wb_fac_fx = round_fx( L_shl( L_tmp, 10 ) );
    1694             :         }
    1695             :         ELSE
    1696             :         {
    1697           0 :             temp_wb_fac_fx = 0;
    1698           0 :             move16();
    1699             :         }
    1700             : 
    1701       21873 :         L_feedback = L_mac( L_feedback, temp_wb_fac_fx, temp_wb_fac_fx );
    1702             :     }
    1703        7291 :     L_tmp = L_add( L_shr( L_feedback, 1 ), 1 << 21 ); /* Q30 */
    1704             : 
    1705        7291 :     IF( L_tmp != 0 )
    1706             :     {
    1707        7291 :         exp = norm_l( L_tmp );
    1708        7291 :         tmp = extract_h( L_shl( L_tmp, exp ) );
    1709        7291 :         exp = sub( sub( 30, exp ), 21 );
    1710        7291 :         tmp = div_s( 16384, tmp );                  /* Q(15+exp)         */
    1711        7291 :         L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /* Q31 */
    1712        7291 :         feedback_fx = round_fx( L_tmp );            /* Q15 */
    1713             :     }
    1714             :     ELSE
    1715             :     {
    1716           0 :         feedback_fx = 8738;
    1717           0 :         move16(); /* Q15 */
    1718             :     }
    1719             : 
    1720        7291 :     temp_wb_fac_fx = hBWE_TD->prev_wb_GainShape;
    1721        7291 :     move16();
    1722       36455 :     FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    1723             :     {
    1724       29164 :         GainShape[i] = add( mult_r( sub( 32767, feedback_fx ), GainShape[i] ), mult_r( feedback_fx, temp_wb_fac_fx ) );
    1725       29164 :         move16();
    1726       29164 :         temp_wb_fac_fx = GainShape[i];
    1727       29164 :         move16();
    1728             :     }
    1729             : 
    1730        7291 :     hBWE_TD->prev_wb_GainShape = GainShape[( NUM_SHB_SUBFR >> 2 ) - 1];
    1731        7291 :     move16();
    1732             : 
    1733             :     /* Compute the power of gains away from the peak gain prior to quantization */
    1734        7291 :     IF( st_fx->element_mode > EVS_MONO )
    1735             :     {
    1736        7291 :         p2m_in = pow_off_pk_corrected_fx( GainShape, NUM_SHB_SUBFR / 4, 1 );
    1737             :     }
    1738             :     ELSE
    1739             :     {
    1740           0 :         p2m_in = pow_off_pk_fx( GainShape, NUM_SHB_SUBFR / 4, 1 );
    1741             :     }
    1742             : 
    1743        7291 :     IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1744             :     {
    1745       21978 :         FOR( i = 0; i < 8; i++ )
    1746             :         {
    1747       19536 :             GainShape[i] = RECIP_ROOT_EIGHT_FX;
    1748       19536 :             move16();
    1749             :         }
    1750             :     }
    1751             :     ELSE
    1752             :     {
    1753        4849 :         push_indice( st_fx->hBstr, IND_UV_FLAG, uv_flag, 1 );
    1754             : 
    1755             :         /* Quantization of the subframe gain parameter */
    1756        4849 :         QuantizeSHBsubgains_ivas_fx( st_fx, GainShape, st_fx->extl );
    1757             :     }
    1758             : 
    1759             :     /* Compute the power of gains away from the peak gain after quantization */
    1760        7291 :     IF( st_fx->element_mode > EVS_MONO )
    1761             :     {
    1762        7291 :         p2m_out = pow_off_pk_corrected_fx( GainShape, NUM_SHB_SUBFR / 2, 2 );
    1763             :     }
    1764             :     ELSE
    1765             :     {
    1766           0 :         p2m_out = pow_off_pk_fx( GainShape, NUM_SHB_SUBFR / 2, 2 );
    1767             :     }
    1768             : 
    1769             :     /* Estimate the gain parameter */
    1770        7291 :     EstimateSHBFrameGain_ivas_fx( SHB_OVERLAP_LEN / 2, hb_frame, Q_ns, shaped_wb_excitation, Q_bwe_exc_ext, GainShape,
    1771             :                                   &GainFrame, window_wb_fx, subwin_wb_fx, 0 );
    1772             : 
    1773             : 
    1774             :     /* If there's a big difference in the power of gains away from the peak gain */
    1775             :     /* due to poor quantization then suppress energy of the high band. */
    1776             : 
    1777        7291 :     IF( GT_32( p2m_out, L_shl( p2m_in, 1 ) ) )
    1778             :     {
    1779         265 :         L_tmp = root_a_over_b_fx( L_shl( p2m_in, 1 ), 29, p2m_out, 29, &exp_out );
    1780         265 :         GainFrame = L_shl( Mpy_32_32( GainFrame, L_tmp ), exp_out ); /* Q18 */
    1781             :     }
    1782             : 
    1783        7291 :     pitBufAvg_fx = 0;
    1784        7291 :     move16();
    1785             : 
    1786       36455 :     FOR( i = 0; i < NB_SUBFR; i++ )
    1787             :     {
    1788       29164 :         pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[i], 82 ) ); /*Q6 */
    1789             :     }
    1790        7291 :     voicingBufAvg_fx = 0;
    1791        7291 :     move16();
    1792       29164 :     FOR( i = 0; i < 3; i++ )
    1793             :     {
    1794       21873 :         voicingBufAvg_fx = add( voicingBufAvg_fx, mult_r( st_fx->voicing_fx[i], 10912 ) ); /*Q15 */
    1795             :     }
    1796             :     /* GainFrame *= max(min((float)(pitBufAvg/voicingBufAvg), 1.0f), 0.7f); */
    1797        7291 :     tmp = 32767;
    1798        7291 :     move16();
    1799        7291 :     IF( voicingBufAvg_fx > 0 )
    1800             :     {
    1801        7287 :         exp = norm_s( voicingBufAvg_fx );
    1802        7287 :         tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
    1803        7287 :         L_tmp = L_mult( pitBufAvg_fx, tmp );                       /* (21-exp) */
    1804        7287 :         L_tmp = L_shl_sat( L_tmp, add( exp, 10 ) );
    1805        7287 :         tmp = round_fx_sat( L_tmp ); /* Q15 */
    1806             :     }
    1807             : 
    1808        7291 :     tmp = s_max( s_min( tmp, 32767 ), 22938 ); /* Q15 */
    1809        7291 :     GainFrame = Mpy_32_16_1( GainFrame, tmp ); /* Q18 */
    1810             : 
    1811        7291 :     IF( LT_16( lsp_spacing_fx, 328 ) && lsp_spacing_fx )
    1812             :     {
    1813           1 :         GainFrame = Mpy_32_16_1( GainFrame, 21299 ); /* Q18 */
    1814             :     }
    1815             : 
    1816             :     /*0.25f*sum_f(voice_factors, NB_SUBFR)*/
    1817        7291 :     L_tmp = L_mult( voice_factors[0], 8192 );
    1818       29164 :     FOR( i = 1; i < NB_SUBFR; i++ )
    1819             :     {
    1820       21873 :         L_tmp = L_mac( L_tmp, voice_factors[i], 8192 );
    1821             :     }
    1822        7291 :     avg_voice_fac = round_fx( L_tmp );
    1823             : 
    1824        7291 :     test();
    1825        7291 :     test();
    1826        7291 :     IF( st_fx->igf != 0 && EQ_16( st_fx->coder_type, VOICED ) )
    1827             :     {
    1828             :         /*GainFrame *= 0.5f;*/
    1829           0 :         GainFrame = Mpy_32_16_1( GainFrame, 16384 );
    1830             :     }
    1831        7291 :     ELSE IF( st_fx->igf != 0 && GT_16( avg_voice_fac, 11469 ) ) /*Q15 -> 0.35f*/
    1832             :     {
    1833             :         /*GainFrame *= 0.75f;*/
    1834           0 :         GainFrame = Mpy_32_16_1( GainFrame, 24576 );
    1835             :     }
    1836             : 
    1837             :     /* Quantization of the frame gain parameter */
    1838        7291 :     IF( st_fx->rf_mode )
    1839             :     {
    1840           0 :         QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame, st_fx->extl, st_fx->extl_brate, &st_fx->hRF->RF_bwe_gainFr_ind, 0 );
    1841             :     }
    1842             :     ELSE
    1843             :     {
    1844        7291 :         QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame, st_fx->extl, st_fx->extl_brate, NULL, 0 );
    1845             :     }
    1846             : 
    1847             :     /* Adjust the subframe and frame gain of the synthesized SHB signal */
    1848             :     /* Scale the shaped excitation*/
    1849        7291 :     scale_sig( shaped_wb_excitation, ( L_FRAME16k + L_SHB_LAHEAD ) / 4, -1 );
    1850        7291 :     scale_sig( hBWE_TD->syn_overlap_fx, L_SHB_LAHEAD, -1 );
    1851        7291 :     Q_bwe_exc_ext = sub( Q_bwe_exc_ext, 1 );
    1852        7291 :     ScaleShapedSHB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame, window_wb_fx, subwin_wb_fx,
    1853             :                        &Q_bwe_exc_ext, &dummy, dummy, dummy );
    1854             : 
    1855        7291 :     Scale_sig( hBWE_TD->syn_overlap_fx, L_SHB_LAHEAD, 1 );
    1856        7291 :     hBWE_TD->prev_Q_bwe_exc = Q_bwe_exc;
    1857        7291 :     move16();
    1858             : 
    1859        7291 :     return;
    1860             : }
    1861             : 
    1862             : 
    1863      210897 : void fb_tbe_reset_enc_fx(
    1864             :     Word32 elliptic_bpf_2_48k_mem_fx[][4],
    1865             :     Word32 *prev_fb_energy_fx,
    1866             :     Word16 elliptic_bpf_2_48k_mem_fx_Q[],
    1867             :     Word16 *prev_fb_energy_fx_Q )
    1868             : {
    1869      210897 :     set32_fx( elliptic_bpf_2_48k_mem_fx[0], 0, 4 );
    1870      210897 :     set32_fx( elliptic_bpf_2_48k_mem_fx[1], 0, 4 );
    1871      210897 :     set32_fx( elliptic_bpf_2_48k_mem_fx[2], 0, 4 );
    1872      210897 :     set32_fx( elliptic_bpf_2_48k_mem_fx[3], 0, 4 );
    1873      210897 :     *prev_fb_energy_fx = 0;
    1874      210897 :     move16();
    1875      210897 :     set16_fx( elliptic_bpf_2_48k_mem_fx_Q, 0, 4 );
    1876      210897 :     *prev_fb_energy_fx_Q = 0;
    1877      210897 :     move16();
    1878             : 
    1879      210897 :     return;
    1880             : }
    1881             : 
    1882             : 
    1883             : /*======================================================================================*/
    1884             : /* FUNCTION : void swb_tbe_enc_fx () */
    1885             : /*--------------------------------------------------------------------------------------*/
    1886             : /* PURPOSE : SWB TBE encoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band encoding module */
    1887             : /*--------------------------------------------------------------------------------------*/
    1888             : /* INPUT ARGUMENTS : */
    1889             : /* _(Word16) coder_type : coding type */
    1890             : /* _(Word16*) shb_speech_fx : SHB target signal (6-14kHz) at 16kHz Q0 */
    1891             : /* _(Word16*) bwe_exc_extended :bandwidth extended exciatation Q0 */
    1892             : /* _(Word16[]) voice_factors :voicing factors Q15 */
    1893             : /* _(Word16*) Q_white_exc :Q Format of White Exc */
    1894             : /*--------------------------------------------------------------------------------------*/
    1895             : /* OUTPUT ARGUMENTS : */
    1896             : /* _(Word16[])White_exc16k_fx : shaped white excitation for the FB TBE Q_white_exc */
    1897             : /* _(Word16*)fb_slope_fx : slope +ve (high freq > low freq), -ve or neutral Q12 */
    1898             : /*--------------------------------------------------------------------------------------*/
    1899             : /* INPUT/OUTPUT ARGUMENTS : */
    1900             : /* _ Encoder_State *st_fx: : Encoder state structure */
    1901             : /*--------------------------------------------------------------------------------------*/
    1902             : /* RETURN ARGUMENTS : */
    1903             : /* _ None */
    1904             : /*--------------------------------------------------------------------------------------*/
    1905             : /* CALLED FROM : TX */
    1906             : /*======================================================================================*/
    1907             : 
    1908        2639 : void swb_tbe_enc_fx(
    1909             :     Encoder_State *st_fx,            /* i/o: encoder state structure */
    1910             :     const Word16 coder_type,         /* i : coding type */
    1911             :     Word16 *shb_speech_fx,           /* i : SHB target signal (6-14kHz) at 16kHz Q_shb*/
    1912             :     Word32 *bwe_exc_extended,        /* i : bandwidth extended exciatation 2*Q_new*/
    1913             :     const Word16 voice_factors_fx[], /* i : voicing factors Q15*/
    1914             :     Word16 *White_exc16k_fx,         /* o : shaped white excitation for the FB TBE Q_white_exc*/
    1915             :     Word16 *Q_white_exc,
    1916             :     Word16 Q_new,
    1917             :     Word16 Q_shb,
    1918             :     Word16 *voicing,         /* i  : OL maximum normalized correlation      */
    1919             :     const Word16 pitch_buf[] /* i : subframe pitch Q6*/
    1920             : )
    1921             : {
    1922             :     Word16 i, j;
    1923             : 
    1924             :     Word16 shb_old_speech_fx[L_LOOK_16k + L_SUBFR16k + L_FRAME16k];
    1925             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    1926             : 
    1927             :     Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
    1928             :     Word32 GainFrame_fx;
    1929             :     Word16 GainShape_fx[NUM_SHB_SUBFR];
    1930             :     Word16 lpc_shb_fx[LPC_SHB_ORDER + 1], lsp_shb_fx[LPC_SHB_ORDER], lsf_shb_fx[LPC_SHB_ORDER];
    1931             :     Word16 weights_lsp[LPC_SHB_ORDER];
    1932             :     Word16 Q_out;
    1933             :     Word16 *shb_frame_fx, *shb_new_speech_fx;
    1934             :     Word16 R_h[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech MSB */
    1935             :     Word16 R_l[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech LSB */
    1936             :     Word16 Q_R;
    1937             :     Word32 LepsP[LPC_SHB_ORDER + 1];
    1938             : 
    1939        2639 :     Word16 ana_align_delay[2] = { -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ), -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ) };
    1940        2639 :     move16();
    1941        2639 :     move16();
    1942             :     Word32 prev_pow_fx, curr_pow_fx, Lscale;
    1943             :     Word32 p2m_in_fx, p2m_out_fx;
    1944             : 
    1945             :     Word16 exp_out, exp, exp1, frac;
    1946             :     Word16 cnt, n_mem, n_mem2;
    1947             :     Word32 L_tmp, L_tmp1;
    1948             :     Word16 Q_bwe_exc;
    1949             : 
    1950             :     Word16 frGainAttenuate, frGainSmoothEn;
    1951             :     Word16 MA_lsp_shb_spacing;
    1952             :     Word16 temp_swb_fac, feedback;
    1953             :     Word32 L_feedback;
    1954             :     Word16 tmp, tmp1, tmp2;
    1955             :     Word32 Lmax;
    1956             :     Word16 sc;
    1957             :     Word16 lsf_shb_orig_fx[LPC_SHB_ORDER];
    1958             :     Word16 sd_uq_q_fx;
    1959             :     Word16 vf_modified_fx[NB_SUBFR16k];
    1960             :     Word16 pitBufAvg_fx;
    1961             :     Word16 voicingBufAvg_fx;
    1962             :     Word16 sum1, sum2;
    1963             :     Word16 recip, Q_recip;
    1964             :     const Word16 *ptr_lsp_interp_coef_fx;
    1965             : 
    1966             :     Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
    1967             :     Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
    1968             : 
    1969             :     /*Word32 shb_ener_sf_fx_32[4];*/
    1970             :     Word32 shb_ener_sf_Q31;
    1971             :     Word16 shb_res_fx[L_FRAME16k];
    1972             :     Word16 shb_res_gshape_fx[NB_SUBFR16k];
    1973             :     Word32 shb_res_gshape_fx_32[NB_SUBFR16k];
    1974             :     Word16 vf_ind_fx;
    1975             : 
    1976             :     Word16 formant_fac_fx;
    1977             :     Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
    1978             : 
    1979             :     Word16 mean_vf;
    1980             :     Word16 lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
    1981             :     Word16 refl[M];
    1982             :     Word16 tilt_para;
    1983             :     Word16 Q_bwe_exc_fb;
    1984             :     Word16 n_subfr_saturation;
    1985        2639 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    1986        2639 :     RF_ENC_HANDLE hRF = st_fx->hRF;
    1987             : 
    1988             :     /*    init and buffers set up   */
    1989        2639 :     exp1 = 0; /* to avoid compilation warnings */
    1990        2639 :     set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
    1991             : 
    1992             :     /* compensate for the delay in target generation and subframe LA */
    1993        2639 :     shb_frame_fx = shb_old_speech_fx + L_SUBFR16k + ana_align_delay[0];
    1994        2639 :     move16();
    1995             : 
    1996             :     /* set up the speech buffers for TBE processing*/
    1997        2639 :     shb_new_speech_fx = shb_old_speech_fx + ( L_LOOK_16k + L_SUBFR16k );
    1998        2639 :     move16();
    1999        2639 :     Copy( hBWE_TD->old_speech_shb_fx, shb_old_speech_fx, ( L_LOOK_16k + L_SUBFR16k ) );
    2000        2639 :     Copy( shb_speech_fx, shb_new_speech_fx, L_FRAME16k );
    2001        2639 :     Copy( shb_old_speech_fx + L_FRAME16k, hBWE_TD->old_speech_shb_fx, ( L_LOOK_16k + L_SUBFR16k ) );
    2002             : 
    2003             :     /* autocorrelation of SHB speech for 10-th order LP analysis */
    2004        2639 :     autocorr_fx( shb_old_speech_fx,
    2005             :                  LPC_SHB_ORDER + 1,
    2006             :                  R_h, /* autocorr (msb)  Q15 */
    2007             :                  R_l, /* autocorr (lsb)      */
    2008             :                  &Q_R,
    2009             :                  NS2SA( INT_FS_16k, ACELP_LOOK_NS ) + L_SUBFR16k + L_FRAME16k,
    2010             :                  win_lpc_shb_fx,
    2011             :                  0,
    2012             :                  1 );
    2013             : 
    2014             : 
    2015             :     /* Spectral smoothing of autocorrelation coefficients */
    2016        2639 :     test();
    2017        2639 :     IF( ( st_fx->rf_mode != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
    2018             :     {
    2019           0 :         FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
    2020             :         {
    2021           0 :             L_tmp = Mpy_32( R_h[i], R_l[i], wac_swb_h[i - 1], wac_swb_l[i - 1] );
    2022           0 :             L_Extract( L_tmp, &R_h[i], &R_l[i] );
    2023             :         }
    2024             :     }
    2025             : 
    2026             :     /* Set the autocorr[0] element to a non-negative value */
    2027        2639 :     R_l[0] = s_max( R_l[0], 1 );
    2028        2639 :     move16();
    2029             : 
    2030        2639 :     E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, LPC_SHB_ORDER, NULL ); /* LPC in Q14 */
    2031             :     {
    2032             :         Word16 enerG, lpc_shb1[M + 1];
    2033             : 
    2034             :         /* extend the lpc_shb to a 16th order gain calc */
    2035        2639 :         set16_fx( lpc_shb1, 0, M + 1 );
    2036        2639 :         Copy( lpc_shb_fx, lpc_shb1, LPC_SHB_ORDER + 1 );
    2037             : 
    2038             :         /* estimate the LP gain */
    2039        2639 :         enerG = Enr_1_Az_fx( lpc_shb1, 2 * L_SUBFR ); /* Q3 */
    2040             : 
    2041             :         /* if the LP gain is greater than a threshold, avoid saturation */
    2042        2639 :         IF( GT_16( enerG, 256 /*32 Q3*/ ) )
    2043             :         {
    2044           1 :             set16_fx( lpc_shb_fx, 0, LPC_SHB_ORDER + 1 );
    2045           1 :             E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, 2, NULL ); /* LPC in Q14 */
    2046             :         }
    2047             :     }
    2048             : 
    2049             :     /* this is needed as the E_LPC_lev_dur function outputs lpc in Q14 */
    2050        2639 :     Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) );
    2051             : 
    2052             :     /* Expand bandwidth of the LP coeffs */
    2053        2639 :     test();
    2054        2639 :     IF( ( st_fx->rf_mode != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
    2055             :     {
    2056           0 :         FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
    2057             :         {
    2058           0 :             lpc_shb_fx[i] = mult_r( lpc_shb_fx[i], lpc_weights_fx[i] );
    2059           0 :             move16();
    2060             :         }
    2061             :     }
    2062             : 
    2063             :     /* LPC to LSP conversion */
    2064             :     /* LPC: Q12, LSP: Q15 */
    2065        2639 :     E_LPC_a_lsp_conversion( lpc_shb_fx, lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
    2066             : 
    2067             :     /* LSP to LSF conversion */
    2068             :     /* LSP: Q15, LSF: Q15 */
    2069        2639 :     E_LPC_lsp_lsf_conversion( lsp_shb_fx, lsf_shb_fx, LPC_SHB_ORDER );
    2070             : 
    2071             :     /* Input signal filtering in case of tonal sounds in the high band
    2072             :        gain Frame smoothing and attenuation control */
    2073        2639 :     gainFrSmooth_En_fx( st_fx, shb_frame_fx, lpc_shb_fx, lsf_shb_fx, &MA_lsp_shb_spacing, &frGainAttenuate, &frGainSmoothEn );
    2074             : 
    2075        2639 :     Copy( lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
    2076        2639 :     Copy( lsf_shb_fx, lsf_shb_orig_fx, LPC_SHB_ORDER );
    2077             : 
    2078        2639 :     test();
    2079        2639 :     IF( ( EQ_16( st_fx->rf_mode, 1 ) ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
    2080             :     {
    2081           0 :         lsp_weights_fx( lsf_shb_fx, weights_lsp, LPC_SHB_ORDER, &Q_out );
    2082             : 
    2083             :         /* to compensate for the 1.1* weighting done inside the function lsp_weights */
    2084             :         /*weights_lsp[3]*=0.909091f; weights_lsp[4]*=0.909091f; */
    2085           0 :         weights_lsp[3] = mult_r( weights_lsp[3], 29789 /*0.909091f Q15*/ );
    2086           0 :         move16();
    2087           0 :         weights_lsp[4] = mult_r( weights_lsp[4], 29789 /*0.909091f Q15*/ );
    2088           0 :         move16();
    2089             : 
    2090             :         /* 8-bit VQ, 10 dimension */
    2091           0 :         i = closest_centroid_fx( lsf_shb_fx, weights_lsp, swb_tbe_lsfvq_cbook_8b, 256, LPC_SHB_ORDER );
    2092           0 :         Copy( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb_fx, LPC_SHB_ORDER );
    2093             : 
    2094           0 :         set16_fx( hBWE_TD->lsf_idx, 0, NUM_Q_LSF );
    2095           0 :         hBWE_TD->lsf_idx[0] = i;
    2096           0 :         move16();
    2097             :     }
    2098             :     ELSE
    2099             :     {
    2100             :         /* LSF quantization (21 bits) */
    2101        2639 :         Quant_BWE_LSF_fx( st_fx, lsf_shb_fx, lsf_shb_fx );
    2102             :     }
    2103             : 
    2104             :     /* space the lsfs to assert a minimum distance */
    2105        2639 :     space_lsfs_fx( lsf_shb_fx, LPC_SHB_ORDER );
    2106             : 
    2107             :     /* voice factor adjustment and gainframe attenuation factor */
    2108        2639 :     tmp = sub( lsf_shb_fx[0], lsf_shb_orig_fx[0] );
    2109        2639 :     L_tmp = L_mult( tmp, tmp );
    2110       26390 :     FOR( i = 1; i < LPC_SHB_ORDER; i++ )
    2111             :     {
    2112             :         /* Estimate the QD in lsfs between UQ and Q */
    2113       23751 :         tmp = sub( lsf_shb_fx[i], lsf_shb_orig_fx[i] );
    2114       23751 :         L_tmp = L_mac( L_tmp, tmp, tmp );
    2115             :     }
    2116        2639 :     sd_uq_q_fx = round_fx( L_tmp ); /* sd_uq_q_fx in Q15 */
    2117             :     /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
    2118             :     /* mean(voice_factors_fx[i], 4); */
    2119             : 
    2120        2639 :     L_tmp = L_mult( voice_factors_fx[0], 8192 );
    2121        2639 :     L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
    2122        2639 :     L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
    2123        2639 :     mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
    2124             : 
    2125        2639 :     Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
    2126             : 
    2127        2639 :     test();
    2128        2639 :     IF( EQ_16( coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
    2129             :     {
    2130        4024 :         FOR( i = 1; i < NB_SUBFR; i++ )
    2131             :         {
    2132        3018 :             L_tmp = L_mult( voice_factors_fx[i], 26214 );
    2133        3018 :             vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 );
    2134        3018 :             move16();
    2135             :         }
    2136        1006 :         IF( NE_16( st_fx->L_frame, L_FRAME ) )
    2137             :         {
    2138         548 :             L_tmp = L_mult( voice_factors_fx[4], 26214 );
    2139         548 :             vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 );
    2140         548 :             move16();
    2141             :         }
    2142             :     }
    2143             : 
    2144             :     /* convert quantized LSFs to LSPs for interpolation */
    2145        2639 :     E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
    2146             : 
    2147        2639 :     test();
    2148        2639 :     IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
    2149             :     {
    2150             :         /* SHB LSP values from prev. frame for interpolation */
    2151        2547 :         Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    2152             :     }
    2153             :     ELSE
    2154             :     {
    2155             :         /* Use current frame's LSPs; in effect no interpolation */
    2156          92 :         Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    2157             :     }
    2158             : 
    2159        2639 :     lsf_diff[0] = lsf_diff[LPC_SHB_ORDER - 1] = 16384; /*Q15*/
    2160        2639 :     move16();
    2161       23751 :     FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2162             :     {
    2163       21112 :         lsf_diff[i] = sub( lsf_shb_fx[i], lsf_shb_fx[i - 1] );
    2164       21112 :         move16();
    2165             :     }
    2166        2639 :     a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl, M );
    2167             : 
    2168             :     /* LSP interpolation for 13.2 kbps and 16.4 kbps */
    2169             :     /* tilt_para = 6.6956f * (1.0f + refl[0]) * (1.0f + refl[0])
    2170             :                  - 3.8714f * (1.0f + refl[0])
    2171             :                  + 1.3041f; */
    2172        2639 :     tmp = add( 16384, shr( refl[0], 1 ) ); /*Q14*/
    2173        2639 :     tmp1 = mult( 27425 /*Q12*/, tmp );     /*Q11*/
    2174        2639 :     tmp1 = mult( tmp1, tmp );
    2175        2639 :     tmp2 = shr( mult( 31715, tmp ), 2 );        /* Q11 */
    2176        2639 :     tilt_para = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
    2177             : 
    2178        2639 :     IF( NE_16( st_fx->last_extl, SWB_TBE ) )
    2179             :     {
    2180         828 :         FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2181             :         {
    2182         736 :             hBWE_TD->prev_lsf_diff_fx[i - 1] = mult( lsf_diff[i], 16384 ); /*Q15*/
    2183         736 :             move16();
    2184             :         }
    2185             :     }
    2186             : 
    2187        2639 :     IF( LE_32( st_fx->total_brate, ACELP_16k40 ) )
    2188             :     {
    2189        1415 :         test();
    2190        1415 :         test();
    2191        1415 :         test();
    2192        1415 :         test();
    2193        1415 :         test();
    2194        1415 :         IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( coder_type, TRANSITION ) || LT_16( tilt_para, 1024 ) ) ) &&
    2195             :             !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( hBWE_TD->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para, 5120 ) ) )
    2196             :         {
    2197       12429 :             FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2198             :             {
    2199       11048 :                 IF( LT_16( lsf_diff[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
    2200             :                 {
    2201        4804 :                     tmp = mult( 26214, lsf_diff[i] );
    2202        4804 :                     tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    2203        4804 :                     tmp = s_max( tmp, 16384 );
    2204        4804 :                     w[i] = s_min( tmp, 32767 );
    2205        4804 :                     move16();
    2206             :                 }
    2207             :                 ELSE
    2208             :                 {
    2209        6244 :                     tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    2210        6244 :                     tmp = div_s( tmp, lsf_diff[i] );
    2211        6244 :                     tmp = s_max( tmp, 16384 );
    2212        6244 :                     w[i] = s_min( tmp, 32767 );
    2213        6244 :                     move16();
    2214             :                 }
    2215             :             }
    2216        1381 :             w[0] = w[1];
    2217        1381 :             move16();
    2218        1381 :             w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
    2219        1381 :             move16();
    2220             : 
    2221       15191 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2222             :             {
    2223       13810 :                 tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w[i] ) );
    2224       13810 :                 tmp2 = mult( lsp_shb_2_fx[i], w[i] );
    2225       13810 :                 lsp_temp_fx[i] = add( tmp1, tmp2 );
    2226       13810 :                 move16();
    2227             :             }
    2228             :         }
    2229             :         ELSE
    2230             :         {
    2231          34 :             Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
    2232             :         }
    2233             :     }
    2234             : 
    2235        2639 :     Copy( lsf_diff + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
    2236        2639 :     hBWE_TD->prev_tilt_para_fx = tilt_para;
    2237        2639 :     move16();
    2238             : 
    2239        2639 :     shb_ener_sf_Q31 = 0;
    2240        2639 :     move16();
    2241        2639 :     test();
    2242        2639 :     IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    2243             :     {
    2244             :         /* ---------- SHB LSP interpolation ---------- */
    2245        1224 :         ptr_lsp_interp_coef_fx = interpol_frac_shb; /* Q15 */
    2246        6120 :         FOR( j = 0; j < 4; j++ )
    2247             :         {
    2248       53856 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2249             :             {
    2250             :                 /*lsp_temp_fx[i] =  lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx)       */
    2251             :                 /*                + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1));  */
    2252       48960 :                 L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
    2253       48960 :                 lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
    2254       48960 :                 move16();
    2255             :             }
    2256        4896 :             ptr_lsp_interp_coef_fx += 2;
    2257             : 
    2258        4896 :             tmp = i_mult2( j, ( LPC_SHB_ORDER + 1 ) );
    2259             :             /* convert LSPs to LPC coefficients for SHB synthesis*/
    2260        4896 :             E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
    2261             :             /* Bring the LPCs to Q12 */
    2262        4896 :             Copy_Scale_sig( lpc_shb_sf_fx + tmp, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf_fx[tmp] ), 2 ) );
    2263             :         }
    2264             : 
    2265             :         /* -------- Calculate the SHB Energy --------  */
    2266             :         /*shb_ener_sf[0] = 0.003125f * sum2_f( shb_frame + L_SHB_LAHEAD,     320 );*/
    2267      392904 :         FOR( i = L_SHB_LAHEAD; i < L_FRAME16k + L_SHB_LAHEAD; i++ )
    2268             :         {
    2269             :             /* shbEner = shbEner + in[i] * in[i] */
    2270      391680 :             shb_ener_sf_Q31 = L_mac0_sat( shb_ener_sf_Q31, shb_frame_fx[i], shb_frame_fx[i] );
    2271             :             /* o: shb_ener_sf_Q31 in (2*Q_shb)      */
    2272             :         }
    2273        1224 :         shb_ener_sf_Q31 = Mult_32_16( shb_ener_sf_Q31, 102 /*0.003125f Q15*/ );
    2274        1224 :         shb_ener_sf_Q31 = L_add( 1l /*1 Q0*/, shb_ener_sf_Q31 );
    2275        1224 :         Quant_shb_ener_sf_fx( st_fx, &shb_ener_sf_Q31, ( 2 * Q_shb ) );
    2276             : 
    2277             :         /* --------  calculate the residuals using the FOUR subframe LPCs --------  */
    2278        1224 :         set16_fx( shb_res_fx, 0, L_FRAME16k );
    2279        1224 :         Residu3_10_fx( lpc_shb_sf_fx, shb_frame_fx + L_SHB_LAHEAD, shb_res_fx, 80, 0 );
    2280        1224 :         Residu3_10_fx( lpc_shb_sf_fx + ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 80, shb_res_fx + 80, 80, 0 );
    2281        1224 :         Residu3_10_fx( lpc_shb_sf_fx + 2 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 160, shb_res_fx + 160, 80, 0 );
    2282        1224 :         Residu3_10_fx( lpc_shb_sf_fx + 3 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 240, shb_res_fx + 240, 80, 0 );
    2283             :         /* i: shb_frame_fx in Q_shb */
    2284             :         /* o: shb_res_fx in Q_shb   */
    2285             : 
    2286        1224 :         set32_fx( shb_res_gshape_fx_32, 0, NB_SUBFR16k );
    2287        7344 :         FOR( i = 0; i < NB_SUBFR16k; i++ )
    2288             :         {
    2289        6120 :             shb_res_gshape_fx_32[i] = sum2_fx( shb_res_fx + i * 64, 64 );
    2290        6120 :             move32();
    2291             :         }
    2292             :         /* o: shb_res_gshape_fx_32  in (2*Q_shb+1) */
    2293             : 
    2294        1224 :         maximum_32_fx( shb_res_gshape_fx_32, NB_SUBFR16k, &L_tmp );
    2295             : 
    2296             :         /* root_a_over_b_fx(shb_res_gshape_fx_32[i], (2*Q_shb+1), L_tmp, (2*Q_shb+1), &exp);*/
    2297             :         /* First, find 1/L_tmp, L_tmp in QA = (2*Q_shb+1) */
    2298             : 
    2299             :         /* case when den = 0 */
    2300        1224 :         recip = 0; /*instead of 32767 to be compatible with previous root_a_over_b_fx() output */
    2301        1224 :         Q_recip = 0;
    2302             : 
    2303        1224 :         IF( L_tmp )
    2304             :         {
    2305        1224 :             exp = norm_l( L_tmp );
    2306        1224 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    2307        1224 :             recip = div_s( 16384, tmp );
    2308             :             // Q_recip = 31 - ( exp - 14 ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
    2309        1224 :             Q_recip = sub( 31 + 14, exp ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
    2310        1224 :             move16();
    2311             :         }
    2312             : 
    2313        7344 :         FOR( i = 0; i < NB_SUBFR16k; i++ )
    2314             :         {
    2315        6120 :             L_tmp1 = Mult_32_16( shb_res_gshape_fx_32[i], recip ); /*Q = Q_recip+1-16*/
    2316        6120 :             L_tmp = root_a_fx( L_tmp1, add( Q_recip, 1 - 16 ), &exp );
    2317        6120 :             shb_res_gshape_fx[i] = round_fx( L_shl( L_tmp, sub( exp, 1 ) ) ); /* Q14 */
    2318        6120 :             move16();
    2319             :         }
    2320             : 
    2321        1224 :         Quant_shb_res_gshape_fx( st_fx, shb_res_gshape_fx );
    2322             :     }
    2323             : 
    2324             :     /* for 13.2 and 16.4kbps */
    2325        2639 :     E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
    2326        2639 :     Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
    2327             : 
    2328             :     /* Save the SWB LSP values from current frame for interpolation */
    2329        2639 :     Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER ); /* lsp_shb_2_fx in Q15 */
    2330             : 
    2331             :     /* -------- start of  memory rescaling  -------- */
    2332             :     /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
    2333        2639 :     Lmax = L_deposit_l( 0 );
    2334     1691599 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    2335             :     {
    2336     1688960 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
    2337             :     }
    2338        2639 :     Q_bwe_exc = norm_l( Lmax );
    2339        2639 :     if ( Lmax == 0 )
    2340             :     {
    2341           0 :         Q_bwe_exc = 31;
    2342           0 :         move16();
    2343             :     }
    2344        2639 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
    2345             : 
    2346             :     /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
    2347        2639 :     find_max_mem_enc( st_fx, &n_mem, &n_mem2 );
    2348             : 
    2349        2639 :     tmp2 = add( hBWE_TD->prev_Q_bwe_exc, n_mem );
    2350        2639 :     if ( GT_16( Q_bwe_exc, tmp2 ) )
    2351             :     {
    2352         152 :         Q_bwe_exc = tmp2;
    2353         152 :         move16();
    2354             :     }
    2355             : 
    2356             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    2357        2639 :     sc = sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_exc );
    2358        2639 :     IF( sc != 0 )
    2359             :     {
    2360        1337 :         rescale_genSHB_mem_enc( st_fx, sc );
    2361             :     }
    2362             : 
    2363             :     /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm  */
    2364        2639 :     Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
    2365        2639 :     sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
    2366             : 
    2367     1691599 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    2368             :     {
    2369     1688960 :         bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
    2370     1688960 :         move16();
    2371             :     }
    2372        2639 :     Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
    2373             : 
    2374             :     /* 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 */
    2375        2639 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    2376             : 
    2377             :     /* save the previous Q factor of the buffer */
    2378        2639 :     hBWE_TD->prev_Q_bwe_exc = Q_bwe_exc;
    2379        2639 :     move16();
    2380             : 
    2381        2639 :     Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
    2382             : 
    2383             :     /* -------- end of rescaling memories -------- */
    2384        2639 :     Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
    2385        2639 :     move16();
    2386             :     /* Determine formant PF strength */
    2387        2639 :     formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
    2388             :     /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
    2389             : 
    2390             :     /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the ACELP core excitation */
    2391        2639 :     vf_ind_fx = 20;
    2392        2639 :     move16();
    2393             : 
    2394        2639 :     GenShapedSHBExcitation_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx,
    2395        2639 :                                hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    2396        2639 :                                coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified_fx, st_fx->extl,
    2397             :                                &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_Q31,
    2398        2639 :                                shb_res_gshape_fx, shb_res_fx, &vf_ind_fx, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx,
    2399        2639 :                                &( 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, 0 );
    2400             : 
    2401        2639 :     *Q_white_exc = Q_bwe_exc_fb;
    2402        2639 :     move16();
    2403        2639 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
    2404             :     {
    2405           0 :         hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
    2406           0 :         move16();
    2407             :     }
    2408             :     ELSE
    2409             :     {
    2410             :         /*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.
    2411             :         51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
    2412        2639 :         hBWE_TD->prev_Q_bwe_exc_fb = 51;
    2413        2639 :         move16();
    2414             :     }
    2415             : 
    2416        2639 :     test();
    2417        2639 :     IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    2418             :     {
    2419        1224 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    2420             :         {
    2421        1224 :             hBWE_TD->idx_mixFac = vf_ind_fx;
    2422        1224 :             move16();
    2423             :         }
    2424             :         ELSE
    2425             :         {
    2426           0 :             push_indice( st_fx->hBstr, IND_SHB_VF, vf_ind_fx, NUM_BITS_SHB_VF );
    2427             :         }
    2428             :     }
    2429             : 
    2430       29029 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2431             :     {
    2432       26390 :         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 ) );
    2433       26390 :         move16();
    2434             :     }
    2435             : 
    2436       13195 :     FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    2437             :     {
    2438       10556 :         PostShortTerm_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
    2439       10556 :                           hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
    2440             :     }
    2441        2639 :     Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k );
    2442             : 
    2443        2639 :     tmp = sub( shl( Q_bwe_exc, 1 ), 31 );
    2444        2639 :     prev_pow_fx = L_shl( 21475l /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
    2445        2639 :     curr_pow_fx = L_shl( 21475l /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
    2446       81809 :     FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
    2447             :     {
    2448       79170 :         prev_pow_fx = L_mac0_sat( prev_pow_fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] );                                         /* 2*Q_bwe_exc */
    2449       79170 :         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 */
    2450             :     }
    2451             : 
    2452        2639 :     if ( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
    2453             :     {
    2454             :         /*curr_pow_fx = Mult_32_16( curr_pow_fx, 8192);*/ /* Q(2*Q_bwe_exc) */
    2455          84 :         curr_pow_fx = L_shr( curr_pow_fx, 2 );            /* scale by 0.25 */
    2456             :     }
    2457             : 
    2458        2639 :     Lscale = root_a_over_b_fx( curr_pow_fx,
    2459        2639 :                                shl( Q_bwe_exc, 1 ),
    2460             :                                prev_pow_fx,
    2461        2639 :                                shl( Q_bwe_exc, 1 ),
    2462             :                                &exp );
    2463       55419 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    2464             :     {
    2465       52780 :         L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q(16-exp+Q_bwe_exc) */
    2466       52780 :         shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    2467       52780 :         move16();
    2468             :     }
    2469        2639 :     IF( exp < 0 )
    2470             :     {
    2471         279 :         Lscale = L_shl( Lscale, exp );
    2472         279 :         exp = 0;
    2473         279 :         move16();
    2474             :     }
    2475             :     /*
    2476             :        code for EVS and IVAS are basically identical with the exception of i_mult_sat() which has precision issues
    2477             :        thus is was replaced for IVAS and kept for EVS, in order to keep EVS BE to test sequences and legacy implementations
    2478             :     */
    2479        2639 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
    2480             :     {
    2481       29029 :         FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    2482             :         {
    2483       26390 :             tmp = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ );        /* Q15 */
    2484       26390 :             L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), tmp ); /* Q31-exp */
    2485       26390 :             tmp = sub( 32767 /*1.0f Q15*/, tmp );
    2486       26390 :             Lscale = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 );
    2487       26390 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    2488       26390 :             shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    2489       26390 :             move16();
    2490             :         }
    2491             :     }
    2492             :     ELSE
    2493             :     {
    2494           0 :         FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    2495             :         {
    2496           0 :             tmp = round_fx_sat( L_shl( L_mult( 0x6666 /* 0.1 in Q12 */, shl( sub( i, 19 ), 11 ) ), 1 ) );
    2497           0 :             L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), tmp ); /* Q31-exp */
    2498           0 :             tmp = sub( 32767 /*1.0f Q15*/, tmp );
    2499           0 :             Lscale = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 );
    2500           0 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    2501           0 :             shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    2502           0 :             move16();
    2503             :         }
    2504             :     }
    2505             : 
    2506             :     /* Update SHB excitation */
    2507        2639 :     Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
    2508             : 
    2509             :     /* Estimate the gain-shape parameter */
    2510        2639 :     n_subfr_saturation = 0;
    2511        2639 :     move16();
    2512        2639 :     EstimateSHBGainShape_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx,
    2513             :                              Q_bwe_exc, GainShape_fx, subwin_shb_fx, &n_subfr_saturation, 1 );
    2514             : 
    2515             :     /* Gain shape BWS/high band low energy fix */
    2516        2639 :     IF( LT_16( hBWE_TD->cldfbHBLT, 8192 /*1.0f Q13*/ ) ) /* cldfbHBLT in Q13 */
    2517             :     {
    2518             :         /* There is not much HB past 10kHz; the temporal resolution is quite coarse, so reduce the dynamic range */
    2519         165 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    2520             :         {
    2521             :             /* 0.35f +/- delta variation; delta = 0.1*(GS-0.35)*/
    2522             :             /* GainShape[i] = 0.315f + 0.1f * GainShape[i]; */
    2523         132 :             GainShape_fx[i] = mac_r( 676457349l /*0.315 Q31*/, 3277 /*0.1 Q15*/, GainShape_fx[i] );
    2524         132 :             move16();
    2525             :         }
    2526             :     }
    2527             : 
    2528             :     /* Gain frame adjustment factor */
    2529             :     /* log( (GainShape[0]) / (st->prev_wb_GainShape) )*/
    2530        2639 :     test();
    2531        2639 :     IF( GainShape_fx[0] && hBWE_TD->prev_swb_GainShape_fx )
    2532             :     {
    2533        2635 :         exp = norm_s( hBWE_TD->prev_swb_GainShape_fx );
    2534        2635 :         tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_swb_GainShape_fx );
    2535        2635 :         L_tmp = L_mult( GainShape_fx[0], tmp ); /*Q(30 - exp) */
    2536             : 
    2537        2635 :         exp1 = norm_l( L_tmp );
    2538        2635 :         frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) ); /*move16(); */
    2539        2635 :         exp1 = sub( exp, exp1 );                     /*move16(); */
    2540        2635 :         L_tmp = Mpy_32_16( exp1, frac, 22713 );
    2541        2635 :         temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
    2542             :     }
    2543             :     ELSE
    2544             :     {
    2545           4 :         temp_swb_fac = 0;
    2546           4 :         move16();
    2547             :     }
    2548        2639 :     L_feedback = L_mult0( temp_swb_fac, temp_swb_fac );
    2549             : 
    2550             : 
    2551       10556 :     FOR( i = 1; i < NUM_SHB_SUBGAINS; i++ )
    2552             :     {
    2553        7917 :         test();
    2554        7917 :         IF( GainShape_fx[i] && GainShape_fx[i - 1] )
    2555             :         {
    2556        7917 :             exp = norm_s( GainShape_fx[i - 1] );
    2557        7917 :             tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape_fx[i - 1] );
    2558        7917 :             L_tmp = L_mult( GainShape_fx[i], tmp ); /* Q(30 - exp) */
    2559             : 
    2560        7917 :             exp1 = norm_l( L_tmp );
    2561        7917 :             frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
    2562        7917 :             exp1 = sub( exp, exp1 );
    2563        7917 :             L_tmp = Mpy_32_16( exp1, frac, 22713 );
    2564        7917 :             temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
    2565             :         }
    2566             :         ELSE
    2567             :         {
    2568           0 :             temp_swb_fac = 0;
    2569           0 :             move16();
    2570             :         }
    2571             : 
    2572        7917 :         L_feedback = L_mac( L_feedback, temp_swb_fac, temp_swb_fac );
    2573             :     }
    2574             : 
    2575             :     /* feedback = 0.4f / (1 + 0.5f * feedback) */
    2576        2639 :     L_tmp = L_add( L_shr( L_feedback, 1 ), L_shl( 1, 21 ) ); /* Q21 */
    2577        2639 :     IF( L_tmp != 0 )
    2578             :     {
    2579        2639 :         exp = norm_l( L_tmp );
    2580        2639 :         tmp = extract_h( L_shl( L_tmp, exp ) );
    2581        2639 :         exp = sub( sub( 30, exp ), 21 );
    2582        2639 :         tmp = div_s( 16384, tmp );                  /*Q(15+exp) */
    2583        2639 :         L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /*Q31 */
    2584        2639 :         feedback = round_fx( L_tmp );               /*Q15 */
    2585             :     }
    2586             :     ELSE
    2587             :     {
    2588           0 :         feedback = 8738;
    2589           0 :         move16(); /*Q15 */
    2590             :     }
    2591             : 
    2592             :     /* voicing in Q15 */
    2593        2639 :     L_tmp = L_mult( voicing[0], 8192 );
    2594        2639 :     L_tmp = L_mac( L_tmp, voicing[1], 8192 );
    2595        2639 :     L_tmp = L_mac( L_tmp, voicing[2], 8192 );
    2596        2639 :     tmp = sum1 = round_fx( L_tmp ); /* sum1 used again below - Q13 */
    2597        2639 :     tmp = add( tmp, 1 );            /* To ensure change is BE */
    2598             : 
    2599             :     /* voice_factors_fx in Q15 */
    2600        2639 :     L_tmp = L_mult( voice_factors_fx[0], 8192 );
    2601        2639 :     L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
    2602        2639 :     L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
    2603        2639 :     L_tmp = L_mac( L_tmp, voice_factors_fx[3], 8192 );
    2604        2639 :     tmp1 = sum2 = round_fx( L_tmp ); /* sum2 used again below - Q13 */
    2605             : 
    2606             : 
    2607        2639 :     test();
    2608        2639 :     test();
    2609        2639 :     IF( EQ_16( frGainAttenuate, 1 ) || ( GT_16( tmp, 19661 ) && GT_16( tmp1, 6554 ) ) )
    2610             : 
    2611             :     {
    2612        1066 :         temp_swb_fac = hBWE_TD->prev_swb_GainShape_fx;
    2613        1066 :         move16();
    2614        5330 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    2615             :         {
    2616             :             /*GainShape_fx[i] = (1 - feedback) * GainShape[i] + feedback * temp_swb_fac; */
    2617        4264 :             GainShape_fx[i] = round_fx( L_mac( L_mult( sub( 32767, feedback ), GainShape_fx[i] ), feedback, temp_swb_fac ) );
    2618        4264 :             move16();
    2619        4264 :             temp_swb_fac = GainShape_fx[i];
    2620        4264 :             move16();
    2621             :         }
    2622             :     }
    2623        2639 :     hBWE_TD->prev_swb_GainShape_fx = GainShape_fx[3];
    2624        2639 :     move16();
    2625             : 
    2626             :     /* Compute the power of gains away from the peak gain prior to quantization */
    2627        2639 :     p2m_in_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBGAINS, 1 );
    2628             : 
    2629             :     /* Quantization of the gain shape parameter */
    2630             : 
    2631        2639 :     QuantizeSHBsubgains_fx( st_fx, GainShape_fx, st_fx->extl );
    2632             :     /* o: GainShape_fx in Q15 */
    2633             :     /* Compute the power of gains away from the peak gain after quantization */
    2634        2639 :     p2m_out_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBFR, 4 );
    2635             : 
    2636             :     /* Estimate the gain parameter */
    2637        2639 :     EstimateSHBFrameGain_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx, Q_bwe_exc,
    2638             :                              GainShape_fx, &GainFrame_fx, window_shb_fx, subwin_shb_fx, n_subfr_saturation );
    2639             : 
    2640        2639 :     IF( EQ_16( st_fx->tec_tfa, 1 ) )
    2641             :     {
    2642        1224 :         tfaCalcEnv_fx( shb_frame_fx, st_fx->tfa_enr );
    2643             :     }
    2644             : 
    2645             :     /* If there's a big difference in the power of gains away from the peak gain */
    2646             :     /* due to poor quantization then suppress energy of the high band. */
    2647        2639 :     IF( GT_32( p2m_out_fx, L_shl( p2m_in_fx, 1 ) ) )
    2648             :     {
    2649         224 :         L_tmp = root_a_over_b_fx( p2m_in_fx, 28, p2m_out_fx, 29, &exp_out );
    2650         224 :         GainFrame_fx = L_shl( Mult_32_32( GainFrame_fx, L_tmp ), exp_out ); /* Q18 */
    2651             :     }
    2652        2639 :     test();
    2653        2639 :     IF( EQ_16( frGainSmoothEn, 1 ) && LT_32( hBWE_TD->prev_gainFr_SHB_fx, GainFrame_fx ) )
    2654             :     {
    2655           0 :         GainFrame_fx = L_add( L_shr( hBWE_TD->prev_gainFr_SHB_fx, 1 ), L_shr( GainFrame_fx, 1 ) ); /* Q18 */
    2656             :     }
    2657             : 
    2658        2639 :     test();
    2659        2639 :     test();
    2660        2639 :     IF( EQ_16( frGainAttenuate, 1 ) && LE_16( MA_lsp_shb_spacing, 79 /*0.0024f Q15*/ ) )
    2661             :     {
    2662           0 :         exp1 = norm_l( GainFrame_fx );
    2663           0 :         frac = Log2_norm_lc( L_shl( GainFrame_fx, exp1 ) );
    2664           0 :         exp = sub( 30, add( exp1, 21 ) );
    2665           0 :         L_tmp = Mpy_32_16( exp, frac, 26214 ); /* Q16 */
    2666           0 :         frac = L_Extract_lc( L_tmp, &exp );
    2667           0 :         GainFrame_fx = Pow2( 30, frac );
    2668           0 :         exp = sub( exp, 30 );
    2669           0 :         GainFrame_fx = L_shl( GainFrame_fx, add( exp, 18 ) ); /* Q18 */
    2670             :     }
    2671        2639 :     ELSE IF( EQ_16( hBWE_TD->prev_frGainAtten, 1 ) && GT_32( Mult_32_16( GainFrame_fx, 10923 ), hBWE_TD->prev_gainFr_SHB_fx ) )
    2672             :     {
    2673             :         /*GainFrame *= (0.8f + 0.5f*feedback); */
    2674           0 :         tmp = add( 26214, mult_r( feedback, 16384 ) );
    2675           0 :         GainFrame_fx = Mult_32_16( GainFrame_fx, tmp ); /* Q18 */
    2676             :     }
    2677        2639 :     hBWE_TD->prev_frGainAtten = frGainAttenuate;
    2678        2639 :     move16();
    2679        2639 :     hBWE_TD->prev_gainFr_SHB_fx = GainFrame_fx;
    2680        2639 :     move16(); /* Q18 */
    2681             : 
    2682             : 
    2683             :     /* Gain attenuation when the SWB LSF quantization error is larger than a threshold */
    2684        2639 :     tmp = mult_r( 25600 /*400 Q6*/, sd_uq_q_fx ); /* Q6 * Q15 => Q6 */
    2685        2639 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    2686             :     {
    2687        1415 :         tmp1 = mult_r( 6554 /*0.2f Q15*/, tmp );        /* Q15, Q6  => Q6 */
    2688        1415 :         L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
    2689             :     }
    2690             :     ELSE
    2691             :     {
    2692        1224 :         tmp1 = mult_r( 3277 /*0.1f Q15*/, tmp );        /* Q15, Q6  => Q6 */
    2693        1224 :         L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
    2694             :     }
    2695             :     /* sd_uq_q =  max(min(sd_uq_q, 1.0f), 0.5f); */
    2696        2639 :     L_tmp = L_min( L_tmp, 8192l /*1.0f Q13*/ );
    2697        2639 :     L_tmp = L_max( L_tmp, 4096l /*0.5f Q13*/ ); /* Q13 */
    2698             :     /* keep the L_tmp; dont overwrite */
    2699             : 
    2700             :     /* pitBufAvg =  0.0025f * sum_f(pitch_buf, 4); */
    2701             :     /* pitch_buf: Q6 */
    2702        2639 :     pitBufAvg_fx = mult_r( pitch_buf[0], 1311 );
    2703        2639 :     pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[1], 1311 ) );
    2704        2639 :     pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[2], 1311 ) );
    2705        2639 :     pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[3], 1311 ) ); /* Q10 */
    2706             : 
    2707             :     /* voicingBufAvg = (sum_f(voice_factors, 4)=sum2 > 0.6f) ? 0.333f : 0.1667f; */
    2708        2639 :     tmp2 = 5462 /*0.1667f Q15*/;
    2709        2639 :     move16();
    2710        2639 :     if ( GT_16( sum2, 4915 /*0.6f Q13*/ ) )
    2711             :     {
    2712        1697 :         tmp2 = 10912 /*0.333f Q15*/; /* Q15 */
    2713        1697 :         move16();
    2714             :     }
    2715        2639 :     voicingBufAvg_fx = shl( mult( tmp2, sum1 ), 2 ); /* Q15 */
    2716             : 
    2717             : 
    2718             :     /* max(min((float)(sd_uq_q*pitBufAvg/voicingBufAvg), 1.0f), 0.6f) */
    2719             :     /* sd_uq_q: Q13, pitBufAvg_fx: Q6, voicingBufAvg_fx: Q15 */
    2720             : 
    2721             :     /* 1/voicingBufAvg_fx */
    2722        2639 :     tmp = 32767;
    2723        2639 :     move16();
    2724        2639 :     IF( voicingBufAvg_fx > 0 )
    2725             :     {
    2726        2639 :         exp = norm_s( voicingBufAvg_fx );
    2727        2639 :         tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
    2728             : 
    2729             :         /* sd_uq_q*pitBufAvg */
    2730        2639 :         L_tmp = Mult_32_16( L_tmp, pitBufAvg_fx );                  /* Q13 * Q10 + 1 -16 => Q8 */
    2731        2639 :         L_tmp = Mult_32_16( L_tmp, tmp );                           /* Q8 + (14 - exp) - 15 => Q7 - exp */
    2732        2639 :         tmp = round_fx_sat( L_shl_sat( L_tmp, 31 - ( 7 - exp ) ) ); /* Q15 */
    2733             :     }
    2734             : 
    2735        2639 :     tmp = s_min( tmp, 32767 /*1.0f Q15*/ );
    2736        2639 :     tmp = s_max( tmp, 19661 /*0.6f Q15*/ );
    2737        2639 :     GainFrame_fx = Mult_32_16( GainFrame_fx, tmp ); /* Q18 + Q15 + 1 - 16 : Q18 */
    2738             : 
    2739        2639 :     test();
    2740        2639 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) || EQ_16( st_fx->rf_mode, 1 ) )
    2741             :     {
    2742             :         /* Compensate for energy increase mismatch due to memory-less synthesis*/
    2743        1224 :         GainFrame_fx = Mult_32_16( GainFrame_fx, 27853 /*0.85f Q15*/ ); /* Q18 */
    2744             :     }
    2745             : 
    2746             :     /* Quantization of the frame gain parameter */
    2747        2639 :     QuantizeSHBframegain_fx( st_fx, &GainFrame_fx, st_fx->extl, 0, &hRF->RF_bwe_gainFr_ind );
    2748             : 
    2749             :     /* Adjust the subframe and frame gain of the synthesized SHB signal */
    2750             :     /* Scale the shaped excitation */
    2751        2639 :     IF( EQ_32( st_fx->extl, FB_TBE ) )
    2752             :     {
    2753           0 :         tmp = norm_l( GainFrame_fx );
    2754           0 :         if ( GainFrame_fx == 0 )
    2755             :         {
    2756           0 :             tmp = 31;
    2757           0 :             move16();
    2758             :         }
    2759           0 :         L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
    2760             : 
    2761           0 :         tmp1 = 0;
    2762           0 :         move16();
    2763           0 :         FOR( i = 0; i < L_FRAME16k; i++ )
    2764             :         {
    2765           0 :             L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[NUM_SHB_SUBFR * i / L_FRAME16k] ); /* Q : 18 + tmp +15 -15*/
    2766             :             /*White_exc16k_fx[i] = round_fx( L_shl(Mult_32_16( L_tmp1, White_exc16k_fx[i]),(23 - tmp -*Q_white_exc)) );*/
    2767             :             /*18 + tmp +*Q_white_exc -15 + 23 - tmp -*Q_white_exc -16  = 10*/
    2768           0 :             White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
    2769           0 :             move16();
    2770             :             /*Lmax = L_max(Lmax,White_exc16k_fx[i]);*/
    2771           0 :             tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
    2772             :             /*White_exc16k_fx[i] =32767;*/
    2773             :             /*White_exc16k_fx[i] = shr(White_exc16k_fx[i],5);*/
    2774             :         }
    2775           0 :         *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
    2776           0 :         tmp = norm_s( tmp1 );
    2777           0 :         if ( tmp1 == 0 )
    2778             :         {
    2779           0 :             tmp = 15;
    2780           0 :             move16();
    2781             :         }
    2782             : 
    2783           0 :         FOR( i = 0; i < L_FRAME16k; i++ )
    2784             :         {
    2785           0 :             White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
    2786           0 :             move16();
    2787             :         }
    2788           0 :         *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
    2789           0 :         move16();
    2790             :     }
    2791             : 
    2792             :     /* *Q_white_exc = Q_bwe_exc_mod; move16(); output Qwhiteexc_FB from the GenShapedSHB function*/
    2793        2639 :     hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
    2794        2639 :     move16();
    2795             : 
    2796        2639 :     return;
    2797             : }
    2798             : 
    2799             : 
    2800             : /*======================================================================================*/
    2801             : /* FUNCTION : void swb_tbe_enc_ivas_fx () */
    2802             : /*--------------------------------------------------------------------------------------*/
    2803             : /* PURPOSE : SWB TBE encoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band encoding module */
    2804             : /*--------------------------------------------------------------------------------------*/
    2805             : /* INPUT ARGUMENTS : */
    2806             : /* _(Word16) coder_type : coding type */
    2807             : /* _(Word16*) shb_speech_fx : SHB target signal (6-14kHz) at 16kHz Q0 */
    2808             : /* _(Word16*) bwe_exc_extended :bandwidth extended exciatation Q0 */
    2809             : /* _(Word16[]) voice_factors :voicing factors Q15 */
    2810             : /* _(Word16*) Q_white_exc :Q Format of White Exc */
    2811             : /*--------------------------------------------------------------------------------------*/
    2812             : /* OUTPUT ARGUMENTS : */
    2813             : /* _(Word16[])White_exc16k_fx : shaped white excitation for the FB TBE Q_white_exc */
    2814             : /* _(Word16*)fb_slope_fx : slope +ve (high freq > low freq), -ve or neutral Q12 */
    2815             : /*--------------------------------------------------------------------------------------*/
    2816             : /* INPUT/OUTPUT ARGUMENTS : */
    2817             : /* _ Encoder_State *st_fx: : Encoder state structure */
    2818             : /*--------------------------------------------------------------------------------------*/
    2819             : /* RETURN ARGUMENTS : */
    2820             : /* _ None */
    2821             : /*--------------------------------------------------------------------------------------*/
    2822             : /* CALLED FROM : TX */
    2823             : /*======================================================================================*/
    2824             : 
    2825      146035 : void swb_tbe_enc_ivas_fx(
    2826             :     Encoder_State *st_fx,                 /* i/o: encoder state structure                          */
    2827             :     STEREO_ICBWE_ENC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure                            */
    2828             :     Word16 *shb_speech_fx,                /* i : SHB target signal (6-14kHz) at 16kHz         Q_shb */
    2829             :     Word32 *bwe_exc_extended,             /* i : bandwidth extended exciatation           2 * Q_new */
    2830             :     const Word16 voice_factors_fx[],      /* i : voicing factors                                Q15 */
    2831             :     Word16 *White_exc16k_fx,              /* o : shaped white excitation for the FB TBE Q_white_exc */
    2832             :     Word16 *Q_white_exc,
    2833             :     Word16 Q_new,
    2834             :     Word16 Q_shb,
    2835             :     Word16 *voicing,         /* i : OL maximum normalized correlation    */
    2836             :     const Word16 pitch_buf[] /* i : subframe pitch                    Q6 */
    2837             : )
    2838             : {
    2839             :     Word16 i, j, k, delay;
    2840             : 
    2841             :     Word16 shb_old_speech_fx[L_LOOK_16k + L_SUBFR16k + L_FRAME16k];
    2842             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    2843             : 
    2844             :     Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
    2845             :     Word32 GainFrame_fx;                /* Q18 */
    2846             :     Word16 GainShape_fx[NUM_SHB_SUBFR]; /* Q15 */
    2847             :     Word16 lpc_shb_fx[LPC_SHB_ORDER + 1], lsp_shb_fx[LPC_SHB_ORDER], lsf_shb_fx[LPC_SHB_ORDER];
    2848             :     Word16 weights_lsp[LPC_SHB_ORDER];
    2849             :     Word16 Q_out;
    2850             :     Word16 *shb_frame_fx, *shb_new_speech_fx;
    2851             :     Word16 R_h[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech MSB */
    2852             :     Word16 R_l[LPC_SHB_ORDER + 2]; /* Autocorrelations of windowed speech LSB */
    2853             :     Word16 Q_R;
    2854             :     Word32 LepsP[LPC_SHB_ORDER + 1];
    2855             : 
    2856             :     Word16 ana_align_delay;
    2857             :     Word32 prev_pow_fx, curr_pow_fx, Lscale;
    2858             :     Word32 p2m_in_fx, p2m_out_fx;
    2859             : 
    2860             :     Word16 exp_out, exp, exp1, frac;
    2861             :     Word16 cnt, n_mem, n_mem2;
    2862             :     Word32 L_tmp, L_tmp1;
    2863             :     Word16 Q_bwe_exc;
    2864             : 
    2865             :     Word16 frGainAttenuate, frGainSmoothEn;
    2866             :     Word16 MA_lsp_shb_spacing;
    2867             :     Word16 temp_swb_fac, feedback;
    2868             :     Word32 L_feedback;
    2869             :     Word16 tmp, tmp1, tmp2;
    2870             :     Word32 Lmax;
    2871             :     Word16 sc;
    2872             :     Word16 lsf_shb_orig_fx[LPC_SHB_ORDER];
    2873             :     Word16 sd_uq_q_fx;
    2874             :     Word16 vf_modified_fx[NB_SUBFR16k];
    2875             :     Word16 pitBufAvg_fx;
    2876             :     Word16 voicingBufAvg_fx;
    2877             :     Word16 sum1, sum2;
    2878             :     Word16 recip, Q_recip;
    2879             :     const Word16 *ptr_lsp_interp_coef_fx;
    2880             : 
    2881             :     Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
    2882             :     Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
    2883             : 
    2884             :     /*Word32 shb_ener_sf_fx_32[4];*/
    2885             :     Word32 shb_ener_sf_Q31;
    2886             :     Word16 shb_res_fx[L_FRAME16k];
    2887             :     Word16 shb_res_gshape_fx[NB_SUBFR16k];
    2888             :     Word32 shb_res_gshape_fx_32[NB_SUBFR16k];
    2889             :     Word16 vf_ind_fx;
    2890             : 
    2891             :     Word16 formant_fac_fx;
    2892             :     // int16_t stab_check = 1;
    2893             :     Word16 MSFlag;
    2894             :     Word16 *nlExc16k_fx, *nlExc16k_e, *mixExc16k_fx, *mixExc16k_e;
    2895             : 
    2896             :     Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
    2897             : 
    2898             :     Word16 acorr_v2a_fx; /* Q15 */
    2899             : 
    2900             :     Word16 acorr_EnvSHBres[ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN], *p_acorr, shb_env_tilt_fx;
    2901             :     Word16 buf_EnvSHBres_fx[2 * L_FRAME4k], *p_buf, EnvSHBres_fx[L_FRAME16k], EnvSHBres_4k_fx[L_FRAME4k], EnvSHBres_4k_norm_fx[L_FRAME4k], env_mean_normf_fx[L_FRAME4k];
    2902             :     Word32 tmp_buf[L_FRAME4k];
    2903      146035 :     Word16 Q_EnvSHBres_4k_norm = Q31;
    2904      146035 :     move16();
    2905             :     Word16 GainShape_Interp_fx[NUM_SHB_SUBGAINS], GainShape_tilt_fx; /* Q15 */
    2906             :     Word16 seg_mean[4], den_seg_mean[4], *p_env, step;
    2907             :     Word16 temp, scale_fx, scale_e, pow_e, tmp_e, tmp1_e;
    2908             :     Word16 seg_len, seg_len_2;
    2909             :     Word16 den_seg_len, den_seg_len_2, fact_atten_fx; /* Q15 */
    2910             :     Word32 sum_gain_fx, normFact_fx;
    2911             :     Word32 pow0_fx, alpha_fx;
    2912             :     Word16 Env_error_part_fx[NUM_SHB_SUBGAINS], Env_error_fx; /* Q0 */
    2913             : 
    2914             :     Word16 mean_vf;
    2915             :     Word16 lsf_diff_fx[LPC_SHB_ORDER], w_fx[LPC_SHB_ORDER];
    2916             :     Word16 refl_fx[M];
    2917             :     Word16 tilt_para_fx;
    2918             :     Word16 Q_bwe_exc_fb;
    2919             :     Word16 n_subfr_saturation;
    2920             : 
    2921      146035 :     Env_error_fx = 0;
    2922      146035 :     move16();
    2923      146035 :     pitBufAvg_fx = 0;
    2924      146035 :     move16();
    2925      146035 :     voicingBufAvg_fx = 0;
    2926      146035 :     move16();
    2927      146035 :     set16_fx( Env_error_part_fx, 0, NUM_SHB_SUBGAINS );
    2928      146035 :     shb_env_tilt_fx = 0;
    2929      146035 :     move16();
    2930      146035 :     sum1 = 0;
    2931      146035 :     move16();
    2932      146035 :     sum2 = 0;
    2933      146035 :     move16();
    2934             : 
    2935      146035 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    2936      146035 :     RF_ENC_HANDLE hRF = st_fx->hRF;
    2937      146035 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    2938             : 
    2939             :     /*    init and buffers set up   */
    2940      146035 :     exp1 = 0; /* to avoid compilation warnings */
    2941      146035 :     move16();
    2942             : 
    2943      146035 :     test();
    2944      146035 :     IF( st_fx->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
    2945             :     {
    2946       33208 :         nlExc16k_fx = hStereoICBWE->nlExc16k_fx;
    2947       33208 :         nlExc16k_e = &hStereoICBWE->nlExc16k_e;
    2948       33208 :         mixExc16k_fx = hStereoICBWE->mixExc16k_fx;
    2949       33208 :         mixExc16k_e = &hStereoICBWE->mixExc16k_e;
    2950       33208 :         MSFlag = hStereoICBWE->MSFlag;
    2951       33208 :         move16();
    2952             :     }
    2953             :     ELSE
    2954             :     {
    2955      112827 :         nlExc16k_fx = NULL;
    2956      112827 :         nlExc16k_e = NULL;
    2957      112827 :         mixExc16k_fx = NULL;
    2958      112827 :         mixExc16k_e = NULL;
    2959      112827 :         MSFlag = 0;
    2960      112827 :         move16();
    2961             :     }
    2962             : 
    2963      146035 :     set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
    2964             : 
    2965      146035 :     test();
    2966      146035 :     test();
    2967      146035 :     IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) && ( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) ) )
    2968             :     {
    2969        4369 :         ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 ) + 20; /* 20 corresponds to the 1.25 ms CLDFB delay */
    2970        4369 :         move16();
    2971             :     }
    2972             :     ELSE
    2973             :     {
    2974      141666 :         ana_align_delay = -L_SHB_LAHEAD - ( NL_BUFF_OFFSET / 2 );
    2975      141666 :         move16();
    2976             :     }
    2977             : 
    2978             :     /* compensate for the delay in target generation and subframe LA */
    2979      146035 :     shb_frame_fx = shb_old_speech_fx + L_SUBFR16k + ana_align_delay;
    2980             : 
    2981      146035 :     set16_fx( shb_old_speech_fx, 0, L_LOOK_16k + L_SUBFR16k + L_FRAME16k );
    2982             : 
    2983             :     /* set up the speech buffers for TBE processing*/
    2984      146035 :     delay = L_LOOK_16k + L_SUBFR16k;
    2985      146035 :     move16();
    2986      146035 :     IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
    2987             :     {
    2988        6549 :         shb_new_speech_fx = shb_old_speech_fx + delay - L_MEM_RECALC_TBE_16K;
    2989             :     }
    2990             :     ELSE
    2991             :     {
    2992      139486 :         shb_new_speech_fx = shb_old_speech_fx + delay;
    2993             :     }
    2994             : 
    2995      146035 :     Copy( hBWE_TD->old_speech_shb_fx, shb_old_speech_fx, delay );
    2996      146035 :     Copy( shb_speech_fx, shb_new_speech_fx, L_FRAME16k );
    2997      146035 :     Copy( shb_old_speech_fx + L_FRAME16k, hBWE_TD->old_speech_shb_fx, delay );
    2998             : 
    2999             :     /* autocorrelation of SHB speech for 10-th order LP analysis */
    3000      146035 :     autocorr_fx( shb_old_speech_fx,
    3001             :                  LPC_SHB_ORDER + 1,
    3002             :                  R_h, /* autocorr (msb)  Q15 */
    3003             :                  R_l, /* autocorr (lsb)      */
    3004             :                  &Q_R,
    3005             :                  NS2SA( INT_FS_16k, ACELP_LOOK_NS ) + L_SUBFR16k + L_FRAME16k,
    3006             :                  win_lpc_shb_fx,
    3007             :                  0,
    3008             :                  1 );
    3009             : 
    3010             : 
    3011             :     /* Spectral smoothing of autocorrelation coefficients */
    3012      146035 :     test();
    3013      146035 :     test();
    3014      146035 :     IF( st_fx->rf_mode || EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) )
    3015             :     {
    3016      413490 :         FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
    3017             :         {
    3018      375900 :             L_tmp = Mpy_32( R_h[i], R_l[i], wac_swb_ivas_h[i - 1], wac_swb_ivas_l[i - 1] );
    3019      375900 :             L_Extract( L_tmp, &R_h[i], &R_l[i] );
    3020             :         }
    3021             :     }
    3022             : 
    3023             :     /* Set the autocorr[0] element to a non-negative value */
    3024      146035 :     R_l[0] = s_max( R_l[0], 1 );
    3025      146035 :     move16();
    3026             : 
    3027      146035 :     E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, LPC_SHB_ORDER, NULL ); /* LPC in Q14 */
    3028             :     {
    3029             :         Word16 enerG, lpc_shb1[M + 1];
    3030             : 
    3031             :         /* extend the lpc_shb to a 16th order gain calc */
    3032      146035 :         set16_fx( lpc_shb1, 0, M + 1 );
    3033      146035 :         Copy( lpc_shb_fx, lpc_shb1, LPC_SHB_ORDER + 1 );
    3034             : 
    3035             :         /* estimate the LP gain */
    3036      146035 :         enerG = Enr_1_Az_fx( lpc_shb1, 2 * L_SUBFR ); /* Q3 */
    3037             : 
    3038             :         /* if the LP gain is greater than a threshold, avoid saturation */
    3039      146035 :         Word16 flag_sat = 0;
    3040      146035 :         Word16 lpc_shb_fx0_req_shift = sub( norm_s( lpc_shb_fx[0] ), 2 );
    3041      146035 :         IF( GT_16( lpc_shb_fx0_req_shift, 0 ) )
    3042             :         {
    3043         632 :             FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
    3044             :             {
    3045         632 :                 IF( LT_16( norm_s( lpc_shb_fx[i] ), lpc_shb_fx0_req_shift ) )
    3046             :                 {
    3047         632 :                     flag_sat = 1;
    3048         632 :                     break;
    3049             :                 }
    3050             :             }
    3051             :         }
    3052             : 
    3053      146035 :         IF( GT_16( enerG, 256 /* 32.0 in Q3 */ ) || flag_sat )
    3054             :         {
    3055         748 :             set16_fx( lpc_shb_fx, 0, LPC_SHB_ORDER + 1 );
    3056         748 :             E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, 2, NULL ); /* LPC in Q14 */
    3057             :         }
    3058             :     }
    3059             : 
    3060             :     /* this is needed as the E_LPC_lev_dur function outputs lpc in Q14 */
    3061      146035 :     scale_sig( lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) );
    3062             : 
    3063             :     /* Expand bandwidth of the LP coeffs */
    3064      146035 :     test();
    3065      146035 :     test();
    3066      146035 :     IF( st_fx->rf_mode || EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) )
    3067             :     {
    3068      413490 :         FOR( i = 1; i <= LPC_SHB_ORDER; i++ )
    3069             :         {
    3070      375900 :             lpc_shb_fx[i] = mult_r( lpc_shb_fx[i], lpc_weights_fx[i] );
    3071      375900 :             move16();
    3072             :         }
    3073             :     }
    3074             : 
    3075             :     /* stab_check = a2lsp( lsf_shb, lpc_shb, LPC_SHB_ORDER ); */
    3076             : 
    3077             :     /* LPC to LSP conversion */
    3078             :     /* LPC: Q12, LSP: Q15 */
    3079      146035 :     E_LPC_a_lsp_conversion( lpc_shb_fx, lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
    3080             : 
    3081             :     /* LSP to LSF conversion */
    3082             :     /* LSP: Q15, LSF: Q15 */
    3083      146035 :     E_LPC_lsp_lsf_conversion( lsp_shb_fx, lsf_shb_fx, LPC_SHB_ORDER );
    3084             : 
    3085      146035 :     test();
    3086      146035 :     test();
    3087      146035 :     IF( ( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) ) || st_fx->ini_frame == 0 )
    3088             :     {
    3089      163625 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    3090             :         {
    3091             :             // hBWE_TD->prev_lsp_shb_fx[i] = i / 20.0f; // This value in float enc is lsf.
    3092      148750 :             hBWE_TD->prev_lsp_shb_fx[i] = lsp_shb_prev_tbl_swb_tbe_enc_fx[i]; // lsf converted to lsp as fixed enc stores lsp.
    3093      148750 :             move16();
    3094             :         }
    3095             :     }
    3096             : 
    3097             :     // if ( stab_check == 0 )
    3098             :     //{
    3099             :     //     mvr2r( hBWE_TD->prev_lsp_shb, lsf_shb, LPC_SHB_ORDER );
    3100             :     // }
    3101             : 
    3102      146035 :     Copy( lsp_shb_fx, hBWE_TD->prev_lsp_shb_fx, LPC_SHB_ORDER );
    3103      146035 :     Copy( lsf_shb_fx, lsf_shb_orig_fx, LPC_SHB_ORDER );
    3104             : 
    3105             :     /* Input signal filtering in case of tonal sounds in the high band
    3106             :        gain Frame smoothing and attenuation control */
    3107      146035 :     gainFrSmooth_En_ivas_fx( st_fx, shb_frame_fx, lpc_shb_fx, lsf_shb_fx, &MA_lsp_shb_spacing, &frGainAttenuate, &frGainSmoothEn );
    3108             : 
    3109      146035 :     test();
    3110      146035 :     test();
    3111      146035 :     IF( st_fx->rf_mode || EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) )
    3112             :     {
    3113       37590 :         lsp_weights_ivas_fx( lsf_shb_fx, weights_lsp, LPC_SHB_ORDER, &Q_out );
    3114             : 
    3115             :         /* to compensate for the 1.1* weighting done inside the function lsp_weights */
    3116             :         /*weights_lsp[3]*=0.909091f; weights_lsp[4]*=0.909091f; */
    3117       37590 :         weights_lsp[3] = mult_r( weights_lsp[3], 29789 /*0.909091f Q15*/ );
    3118       37590 :         move16();
    3119       37590 :         weights_lsp[4] = mult_r( weights_lsp[4], 29789 /*0.909091f Q15*/ );
    3120       37590 :         move16();
    3121             : 
    3122             :         /* 8-bit VQ, 10 dimension */
    3123       37590 :         i = closest_centroid_fx( lsf_shb_fx, weights_lsp, swb_tbe_lsfvq_cbook_8b, 256, LPC_SHB_ORDER );
    3124       37590 :         Copy( swb_tbe_lsfvq_cbook_8b + i * LPC_SHB_ORDER, lsf_shb_fx, LPC_SHB_ORDER );
    3125             : 
    3126       37590 :         set16_fx( hBWE_TD->lsf_idx, 0, NUM_Q_LSF );
    3127             : 
    3128       37590 :         IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    3129             :         {
    3130       37590 :             push_indice( hBstr, IND_SHB_LSF, i, 8 );
    3131             :         }
    3132             :         ELSE
    3133             :         {
    3134           0 :             hBWE_TD->lsf_idx[0] = i;
    3135           0 :             move16();
    3136             :         }
    3137             :     }
    3138             :     ELSE
    3139             :     {
    3140             :         /* LSF quantization (21 bits) */
    3141      108445 :         Quant_BWE_LSF_ivas_fx( hBstr, st_fx->hBWE_TD, st_fx->codec_mode, lsf_shb_fx, lsf_shb_fx, st_fx->extl_brate );
    3142             :     }
    3143             : 
    3144             :     /* space the lsfs to assert a minimum distance */
    3145      146035 :     space_lsfs_fx( lsf_shb_fx, LPC_SHB_ORDER );
    3146             : 
    3147             :     /* voice factor adjustment and gainframe attenuation factor */
    3148      146035 :     tmp = sub( lsf_shb_fx[0], lsf_shb_orig_fx[0] );
    3149      146035 :     L_tmp = L_mult( tmp, tmp );
    3150     1460350 :     FOR( i = 1; i < LPC_SHB_ORDER; i++ )
    3151             :     {
    3152             :         /* Estimate the QD in lsfs between UQ and Q */
    3153     1314315 :         tmp = sub( lsf_shb_fx[i], lsf_shb_orig_fx[i] );
    3154     1314315 :         L_tmp = L_mac( L_tmp, tmp, tmp );
    3155             :     }
    3156      146035 :     sd_uq_q_fx = round_fx( L_tmp ); /* sd_uq_q_fx in Q15 */
    3157             :     /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
    3158             :     /* mean(voice_factors_fx[i], 4); */
    3159             : 
    3160      146035 :     L_tmp = L_mult( voice_factors_fx[0], 8192 );
    3161      146035 :     L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
    3162      146035 :     L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
    3163      146035 :     mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
    3164             : 
    3165      146035 :     Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
    3166             : 
    3167      146035 :     test();
    3168      146035 :     IF( EQ_16( st_fx->coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
    3169             :     {
    3170      160588 :         FOR( i = 1; i < NB_SUBFR; i++ )
    3171             :         {
    3172      120441 :             L_tmp = L_mult( voice_factors_fx[i], 26214 );
    3173      120441 :             vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 );
    3174      120441 :             move16();
    3175             :         }
    3176       40147 :         IF( NE_16( st_fx->L_frame, L_FRAME ) )
    3177             :         {
    3178       23198 :             L_tmp = L_mult( voice_factors_fx[4], 26214 );
    3179       23198 :             vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 );
    3180       23198 :             move16();
    3181             :         }
    3182             :     }
    3183             : 
    3184             :     /* convert quantized LSFs to LSPs for interpolation */
    3185      146035 :     E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
    3186             : 
    3187      146035 :     test();
    3188      146035 :     IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
    3189             :     {
    3190             :         /* SHB LSP values from prev. frame for interpolation */
    3191      135861 :         Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    3192             :     }
    3193             :     ELSE
    3194             :     {
    3195             :         /* Use current frame's LSPs; in effect no interpolation */
    3196       10174 :         Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    3197             :     }
    3198             : 
    3199      146035 :     lsf_diff_fx[0] = lsf_diff_fx[sub( LPC_SHB_ORDER, 1 )] = 16384; /*Q15*/
    3200      146035 :     move16();
    3201     1314315 :     FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    3202             :     {
    3203     1168280 :         lsf_diff_fx[i] = sub( lsf_shb_fx[i], lsf_shb_fx[sub( i, 1 )] );
    3204     1168280 :         move16();
    3205             :     }
    3206      146035 :     a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl_fx, M );
    3207             : 
    3208             :     /* LSP interpolation for 13.2 kbps and 16.4 kbps */
    3209             :     /* tilt_para_fx = 6.6956f * (1.0f + refl_fx[0]) * (1.0f + refl_fx[0])
    3210             :                  - 3.8714f * (1.0f + refl_fx[0])
    3211             :                  + 1.3041f; */
    3212      146035 :     tmp = add( 16384, shr( refl_fx[0], 1 ) ); /*Q14*/
    3213      146035 :     tmp1 = mult( 27425 /*Q12*/, tmp );        /*Q11*/
    3214      146035 :     tmp1 = mult( tmp1, tmp );
    3215      146035 :     tmp2 = shr( mult( 31715, tmp ), 2 );           /* Q11 */
    3216      146035 :     tilt_para_fx = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
    3217             : 
    3218      146035 :     IF( NE_16( st_fx->last_extl, SWB_TBE ) )
    3219             :     {
    3220      610614 :         FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    3221             :         {
    3222      542768 :             hBWE_TD->prev_lsf_diff_fx[i - 1] = mult( lsf_diff_fx[i], 16384 ); /*Q15*/
    3223      542768 :             move16();
    3224             :         }
    3225             :     }
    3226             : 
    3227      146035 :     IF( LE_32( st_fx->extl_brate, FB_TBE_1k8 ) )
    3228             :     {
    3229      113765 :         test();
    3230      113765 :         test();
    3231      113765 :         test();
    3232      113765 :         test();
    3233      113765 :         test();
    3234      113765 :         IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 /* 5.0f in Q10 */ ) && ( EQ_16( st_fx->coder_type, TRANSITION ) || LT_16( tilt_para_fx, 1024 /* 1.0f in Q10 */ ) ) ) &&
    3235             :             !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 /* 3.0f in Q10 */ ) && GE_16( hBWE_TD->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para_fx, 5120 /* 5.0f in Q10 */ ) ) )
    3236             :         {
    3237      989775 :             FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    3238             :             {
    3239      879800 :                 IF( LT_16( lsf_diff_fx[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
    3240             :                 {
    3241      241988 :                     tmp = mult( 26214, lsf_diff_fx[i] );
    3242      241988 :                     tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    3243      241988 :                     tmp = s_max( tmp, 16384 );
    3244      241988 :                     w_fx[i] = s_min( tmp, 32767 );
    3245      241988 :                     move16();
    3246             :                 }
    3247             :                 ELSE
    3248             :                 {
    3249      637812 :                     tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    3250      637812 :                     tmp = div_s( tmp, lsf_diff_fx[i] );
    3251      637812 :                     tmp = s_max( tmp, 16384 );
    3252      637812 :                     w_fx[i] = s_min( tmp, 32767 );
    3253      637812 :                     move16();
    3254             :                 }
    3255             :             }
    3256      109975 :             w_fx[0] = w_fx[1];
    3257      109975 :             move16();
    3258      109975 :             w_fx[LPC_SHB_ORDER - 1] = w_fx[LPC_SHB_ORDER - 2];
    3259      109975 :             move16();
    3260             : 
    3261     1209725 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    3262             :             {
    3263     1099750 :                 tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w_fx[i] ) );
    3264     1099750 :                 tmp2 = mult( lsp_shb_2_fx[i], w_fx[i] );
    3265     1099750 :                 lsp_temp_fx[i] = add( tmp1, tmp2 );
    3266     1099750 :                 move16();
    3267             :             }
    3268             :         }
    3269             :         ELSE
    3270             :         {
    3271        3790 :             Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
    3272             :         }
    3273             :     }
    3274             : 
    3275      146035 :     Copy( lsf_diff_fx + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
    3276      146035 :     hBWE_TD->prev_tilt_para_fx = tilt_para_fx;
    3277      146035 :     move16();
    3278             : 
    3279      146035 :     shb_ener_sf_Q31 = 0;
    3280      146035 :     move16();
    3281      146035 :     test();
    3282      146035 :     IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) )
    3283             :     {
    3284             :         /* ---------- SHB LSP interpolation ---------- */
    3285       32270 :         ptr_lsp_interp_coef_fx = interpol_frac_shb; /* Q15 */
    3286      161350 :         FOR( j = 0; j < 4; j++ )
    3287             :         {
    3288     1419880 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    3289             :             {
    3290             :                 /*lsp_temp_fx[i] =  lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx)       */
    3291             :                 /*                + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1));  */
    3292     1290800 :                 L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
    3293     1290800 :                 lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
    3294     1290800 :                 move16();
    3295             :             }
    3296      129080 :             ptr_lsp_interp_coef_fx += 2;
    3297             : 
    3298      129080 :             tmp = i_mult2( j, ( LPC_SHB_ORDER + 1 ) );
    3299             :             /* convert LSPs to LPC coefficients for SHB synthesis*/
    3300      129080 :             E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
    3301             :             /* Bring the LPCs to Q12 */
    3302      129080 :             Copy_Scale_sig( lpc_shb_sf_fx + tmp, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf_fx[tmp] ), 2 ) );
    3303             :         }
    3304             : 
    3305             :         /* -------- Calculate the SHB Energy --------  */
    3306             :         /*shb_ener_sf[0] = 0.003125f * sum2_f( shb_frame + L_SHB_LAHEAD,     320 );*/
    3307    10358670 :         FOR( i = L_SHB_LAHEAD; i < L_FRAME16k + L_SHB_LAHEAD; i++ )
    3308             :         {
    3309             :             /* shbEner = shbEner + in[i] * in[i] */
    3310    10326400 :             shb_ener_sf_Q31 = L_mac0_sat( shb_ener_sf_Q31, shb_frame_fx[i], shb_frame_fx[i] );
    3311             :             /* o: shb_ener_sf_Q31 in (2*Q_shb)      */
    3312             :         }
    3313       32270 :         shb_ener_sf_Q31 = Mult_32_16( shb_ener_sf_Q31, 102 /*0.003125f Q15*/ );
    3314       32270 :         shb_ener_sf_Q31 = L_add( 1l /*1 Q0*/, shb_ener_sf_Q31 );
    3315       32270 :         Quant_shb_ener_sf_ivas_fx( st_fx, &shb_ener_sf_Q31, shl( Q_shb, 1 ) );
    3316             : 
    3317             :         /* --------  calculate the residuals using the FOUR subframe LPCs --------  */
    3318       32270 :         set16_fx( shb_res_fx, 0, L_FRAME16k );
    3319       32270 :         Residu3_10_fx( lpc_shb_sf_fx, shb_frame_fx + L_SHB_LAHEAD, shb_res_fx, 80, 0 );
    3320       32270 :         Residu3_10_fx( lpc_shb_sf_fx + ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 80, shb_res_fx + 80, 80, 0 );
    3321       32270 :         Residu3_10_fx( lpc_shb_sf_fx + 2 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 160, shb_res_fx + 160, 80, 0 );
    3322       32270 :         Residu3_10_fx( lpc_shb_sf_fx + 3 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 240, shb_res_fx + 240, 80, 0 );
    3323             :         /* i: shb_frame_fx in Q_shb */
    3324             :         /* o: shb_res_fx in Q_shb   */
    3325             : 
    3326       32270 :         set32_fx( shb_res_gshape_fx_32, 0, NB_SUBFR16k );
    3327      193620 :         FOR( i = 0; i < NB_SUBFR16k; i++ )
    3328             :         {
    3329      161350 :             shb_res_gshape_fx_32[i] = sum2_fx( shb_res_fx + i_mult( i, 64 ), 64 );
    3330      161350 :             move32();
    3331             :         }
    3332             :         /* o: shb_res_gshape_fx_32  in (2*Q_shb+1) */
    3333             : 
    3334       32270 :         maximum_32_fx( shb_res_gshape_fx_32, NB_SUBFR16k, &L_tmp );
    3335             : 
    3336             :         /* root_a_over_b_fx(shb_res_gshape_fx_32[i], (2*Q_shb+1), L_tmp, (2*Q_shb+1), &exp);*/
    3337             :         /* First, find 1/L_tmp, L_tmp in QA = (2*Q_shb+1) */
    3338             : 
    3339             :         /* case when den = 0 */
    3340       32270 :         recip = 0; /*instead of 32767 to be compatible with previous root_a_over_b_fx() output */
    3341       32270 :         move16();
    3342       32270 :         Q_recip = 0;
    3343       32270 :         move16();
    3344             : 
    3345       32270 :         IF( L_tmp )
    3346             :         {
    3347       32248 :             exp = norm_l( L_tmp );
    3348       32248 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    3349       32248 :             recip = div_s( 16384, tmp );
    3350             :             // Q_recip = sub( 31, sub( exp, 14 ) ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
    3351       32248 :             Q_recip = sub( 31 + 14, exp ); /* = 31-(exp+2*Q_shb+1-14), but adjusted by (2*Q_shb+1) for use at Mult_32_16 below */
    3352             :         }
    3353             : 
    3354      193620 :         FOR( i = 0; i < NB_SUBFR16k; i++ )
    3355             :         {
    3356      161350 :             L_tmp1 = Mult_32_16( shb_res_gshape_fx_32[i], recip ); /*Q = Q_recip+1-16*/
    3357      161350 :             L_tmp = root_a_fx( L_tmp1, sub( Q_recip, 15 ), &exp );
    3358      161350 :             shb_res_gshape_fx[i] = round_fx( L_shl( L_tmp, sub( exp, 1 ) ) ); /* Q14 */
    3359      161350 :             move16();
    3360             :         }
    3361             : 
    3362       32270 :         Quant_shb_res_gshape_ivas_fx( st_fx, shb_res_gshape_fx );
    3363             :     }
    3364      113765 :     ELSE IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3365             :     {
    3366       21845 :         FOR( j = 0; j < 4; j++ )
    3367             :         {
    3368       17476 :             tmp = i_mult2( j, ( LPC_SHB_ORDER + 1 ) );
    3369             :             /* convert LSPs to LPC coefficients for SHB synthesis*/
    3370       17476 :             E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
    3371             :             /* Bring the LPCs to Q12 */
    3372       17476 :             Copy_Scale_sig( lpc_shb_sf_fx + tmp, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf_fx[tmp] ), 2 ) );
    3373             :         }
    3374             : 
    3375             :         /* --------  calculate the residuals using the FOUR subframe LPCs --------  */
    3376        4369 :         set16_fx( shb_res_fx, 0, L_FRAME16k );
    3377        4369 :         Residu3_10_fx( lpc_shb_sf_fx, shb_frame_fx + L_SHB_LAHEAD, shb_res_fx, 80, 0 );
    3378        4369 :         Residu3_10_fx( lpc_shb_sf_fx + ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 80, shb_res_fx + 80, 80, 0 );
    3379        4369 :         Residu3_10_fx( lpc_shb_sf_fx + 2 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 160, shb_res_fx + 160, 80, 0 );
    3380        4369 :         Residu3_10_fx( lpc_shb_sf_fx + 3 * ( LPC_SHB_ORDER + 1 ), shb_frame_fx + L_SHB_LAHEAD + 240, shb_res_fx + 240, 80, 0 );
    3381             :     }
    3382             : 
    3383      146035 :     Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER ); /* lsp_shb_2_fx in Q15 */
    3384             :     /* for 13.2 and 16.4kbps */
    3385      146035 :     E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
    3386      146035 :     Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
    3387             : 
    3388             :     /* Determine formant PF strength */
    3389      146035 :     formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
    3390             :     /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
    3391             : 
    3392             :     /* calculate SHB auto-correlation function and convert to SHB voicing factor */
    3393             :     // acorr_v2a = 0.0f;
    3394      146035 :     L_tmp = 0;
    3395      146035 :     move32();
    3396      146035 :     acorr_v2a_fx = 0;
    3397      146035 :     move16();
    3398             : 
    3399      146035 :     test();
    3400      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3401             :     {
    3402             :         /* calculate the TD envelope of the SHB residual signal */
    3403        4369 :         Scale_sig( hBWE_TD->mem_shb_res_fx, MAX_LEN_MA_FILTER, sub( hBWE_TD->prev_Q_shb, Q_shb ) ); /* Q(hBWE_TD->prev_Q_shb) -> Q(Q_shb) */
    3404        4369 :         find_td_envelope_fx( shb_res_fx, L_FRAME16k, 20, hBWE_TD->mem_shb_res_fx, EnvSHBres_fx );   /* Q_shb */
    3405             : 
    3406             :         /* downsample the TD envelope by 4 */
    3407      353889 :         FOR( k = 0; k < L_FRAME4k; k++ )
    3408             :         {
    3409      349520 :             EnvSHBres_4k_fx[k] = EnvSHBres_fx[4 * k]; /* Q_shb */
    3410      349520 :             move16();
    3411             :         }
    3412             : 
    3413             :         /* calculate long-term mean envelope over four consecutive segments using linear interpolation */
    3414        4369 :         seg_len = L_FRAME4k / 4;
    3415        4369 :         move16();
    3416        4369 :         den_seg_len = 1638; /* Q15 */
    3417        4369 :         move16();
    3418        4369 :         seg_len_2 = L_FRAME4k / 8;
    3419        4369 :         move16();
    3420        4369 :         den_seg_len_2 = 3641; /* Q15 */
    3421        4369 :         move16();
    3422       21845 :         FOR( k = 0; k < 4; k++ )
    3423             :         {
    3424             :             // seg_mean[k] = max(1.0f, mean(&EnvSHBres_4k[k * seg_len], seg_len));
    3425       17476 :             seg_mean[k] = s_max( 1, mean_no_sat_fx( &EnvSHBres_4k_fx[k * seg_len], seg_len ) ); /* Q_shb */
    3426       17476 :             move16();
    3427             :             // den_seg_mean[k] = 1.0f / seg_mean[k];
    3428       17476 :             tmp = BASOP_Util_Divide1616_Scale( MAX16B, seg_mean[k], &tmp_e );
    3429       17476 :             tmp_e = sub( tmp_e, sub( 15, Q_shb ) );
    3430       17476 :             den_seg_mean[k] = shl_sat( tmp, tmp_e ); /* Q15 */
    3431       17476 :             move16();
    3432             :         }
    3433             : 
    3434             :         /* first half of the first segment */
    3435        4369 :         IF( hBWE_TD->old_mean_EnvSHBres_fx == 0 )
    3436             :         {
    3437         117 :             set16_fx( env_mean_normf_fx, den_seg_mean[0], seg_len_2 );
    3438             :         }
    3439             :         ELSE
    3440             :         {
    3441        4252 :             env_mean_normf_fx[0] = hBWE_TD->old_mean_EnvSHBres_fx; /* Q15 */
    3442        4252 :             move16();
    3443             :             // step = (den_seg_mean[0] - hBWE_TD->old_mean_EnvSHBres) * den_seg_len_2;
    3444        4252 :             step = mult( sub( den_seg_mean[0], hBWE_TD->old_mean_EnvSHBres_fx ), den_seg_len_2 ); /* Q15 */
    3445       42520 :             FOR( i = 1; i < seg_len_2; i++ )
    3446             :             {
    3447       38268 :                 env_mean_normf_fx[i] = add_sat( env_mean_normf_fx[i - 1], step ); /* Q15 */
    3448       38268 :                 move16();
    3449             :             }
    3450             :         }
    3451             : 
    3452             :         /* segments 1-4 */
    3453        4369 :         p_env = &env_mean_normf_fx[seg_len_2];
    3454       17476 :         FOR( k = 1; k < 4; k++ )
    3455             :         {
    3456             :             // step = ( den_seg_mean[k] - den_seg_mean[k - 1] ) * den_seg_len;
    3457       13107 :             step = mult( sub( den_seg_mean[k], den_seg_mean[k - 1] ), den_seg_len );
    3458      275247 :             FOR( i = 0; i < seg_len; i++ )
    3459             :             {
    3460      262140 :                 *p_env = add_sat( *( p_env - 1 ), step );
    3461      262140 :                 move16();
    3462      262140 :                 p_env++;
    3463             :             }
    3464             :         }
    3465             : 
    3466             :         /* last half of the last segment */
    3467             :         // set_f( p_env, den_seg_mean[3], seg_len_2 );
    3468        4369 :         set16_fx( p_env, den_seg_mean[3], seg_len_2 );
    3469        4369 :         hBWE_TD->old_mean_EnvSHBres_fx = den_seg_mean[3];
    3470        4369 :         move16();
    3471             : 
    3472             :         /* normalize residual SHB envelope with its long-term mean envelope */
    3473      353889 :         FOR( k = 0; k < L_FRAME4k; k++ )
    3474             :         {
    3475      349520 :             tmp_buf[k] = L_mult( EnvSHBres_4k_fx[k], env_mean_normf_fx[k] ); // Q_shb + Q15 + Q1 => Q_shb + Q16
    3476      349520 :             move32();
    3477             :         }
    3478        4369 :         Q_EnvSHBres_4k_norm = sub( getScaleFactor32( tmp_buf, L_FRAME4k ), 1 /* Guard bit */ );
    3479      353889 :         FOR( k = 0; k < L_FRAME4k; k++ )
    3480             :         {
    3481      349520 :             EnvSHBres_4k_norm_fx[k] = extract_h( L_shl( tmp_buf[k], Q_EnvSHBres_4k_norm ) ); // Q_shb + Q16 + Q_EnvSHBres_4k_norm - Q16 => Q_EnvSHBres_4k_norm + Q_shb
    3482      349520 :             move16();
    3483             :         }
    3484             : 
    3485             :         /* calculate tilt of the long-term mean envelope */
    3486        4369 :         lls_interp_n_fx( seg_mean, 4, &shb_env_tilt_fx, &temp, 0 );
    3487             : 
    3488             :         /* copy previous residual envelope to the buffer */
    3489        4369 :         Copy_Scale_sig( hBWE_TD->old_EnvSHBres_fx, buf_EnvSHBres_fx, L_FRAME4k, sub( hBWE_TD->prev_Q_shb, Q_shb ) ); /* Q(hBWE_TD->prev_Q_shb) -> Q(Q_shb) */
    3490             : 
    3491             :         /* subtract mean value from the normalized SHB residual envelope */
    3492        4369 :         p_buf = &buf_EnvSHBres_fx[L_FRAME4k];
    3493        4369 :         temp = mean_no_sat_fx( EnvSHBres_4k_norm_fx, L_FRAME4k ); // Q_EnvSHBres_4k_norm + Q_shb
    3494      353889 :         FOR( k = 0; k < L_FRAME4k; k++ )
    3495             :         {
    3496      349520 :             *p_buf++ = shr( sub( EnvSHBres_4k_norm_fx[k], temp ), Q_EnvSHBres_4k_norm ); // Q_shb
    3497      349520 :             move16();
    3498             :         }
    3499        4369 :         Q_EnvSHBres_4k_norm = add( Q_EnvSHBres_4k_norm, Q_shb );
    3500             : 
    3501             :         /* update memory */
    3502        4369 :         Copy( &buf_EnvSHBres_fx[L_FRAME4k], hBWE_TD->old_EnvSHBres_fx, L_FRAME4k ); // Q_shb
    3503             : 
    3504             :         /* calculate energy normalization factor for the auto-correlation function */
    3505             :         // pow0 = sum2_f( &buf_EnvSHBres[L_FRAME4k], L_FRAME4k ) + 1.0f;
    3506        4369 :         pow_e = sub( 15, Q_shb );
    3507        4369 :         pow0_fx = sum2_16_exp_fx( &buf_EnvSHBres_fx[L_FRAME4k], L_FRAME4k, &pow_e, 0 /* Give guard bits */ ) + 1;
    3508             :         // if (hBWE_TD->prev_enr_EnvSHBres == 1.0f)
    3509        4369 :         IF( BASOP_Util_Cmp_Mant32Exp( hBWE_TD->prev_enr_EnvSHBres_fx, hBWE_TD->prev_enr_EnvSHBres_e, 1, 31 ) == 0 )
    3510             :         {
    3511             :             // scale = 1.0f / pow0;
    3512         634 :             scale_fx = BASOP_Util_Divide3232_Scale( ONE_IN_Q31, pow0_fx, &tmp_e );
    3513         634 :             scale_e = sub( tmp_e, pow_e );
    3514             :         }
    3515             :         ELSE
    3516             :         {
    3517             :             // scale = 1.0f / max( pow0, hBWE_TD->prev_enr_EnvSHBres );
    3518        3735 :             L_tmp1 = pow0_fx;
    3519        3735 :             move32();
    3520        3735 :             IF( BASOP_Util_Cmp_Mant32Exp( L_tmp1, pow_e, hBWE_TD->prev_enr_EnvSHBres_fx, hBWE_TD->prev_enr_EnvSHBres_e ) < 0 )
    3521             :             {
    3522        3250 :                 L_tmp1 = hBWE_TD->prev_enr_EnvSHBres_fx;
    3523        3250 :                 move32();
    3524        3250 :                 pow_e = hBWE_TD->prev_enr_EnvSHBres_e;
    3525        3250 :                 move16();
    3526             :             }
    3527        3735 :             scale_fx = BASOP_Util_Divide3232_Scale( ONE_IN_Q31, L_tmp1, &tmp_e ); // Qs to be handled
    3528        3735 :             scale_e = sub( tmp_e, pow_e );
    3529             :         }
    3530        4369 :         hBWE_TD->prev_enr_EnvSHBres_fx = pow0_fx;
    3531        4369 :         move32();
    3532        4369 :         hBWE_TD->prev_enr_EnvSHBres_e = pow_e;
    3533        4369 :         move16();
    3534             : 
    3535             :         /* calculate normalized auto-correlation function on the residual normalized SHB envelope */
    3536        4369 :         p_acorr = acorr_EnvSHBres;
    3537        4369 :         p_buf = &buf_EnvSHBres_fx[L_FRAME4k - ENVSHBRES_ACORR_MIN];
    3538             : 
    3539      179129 :         FOR( k = 0; k < ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN; k++ )
    3540             :         {
    3541             :             //*p_acorr++ = scale * dotp(&buf_EnvSHBres[L_FRAME4k], p_buf--, L_FRAME4k);
    3542      174760 :             Word64 W_tmp = 0;
    3543      174760 :             move64();
    3544             : 
    3545             :             // tmp = *p_buf;
    3546    14155560 :             FOR( i = 0; i < L_FRAME4k; ++i )
    3547             :             {
    3548    13980800 :                 L_tmp = L_mult( buf_EnvSHBres_fx[L_FRAME4k + i], p_buf[i] ); // 2 * Q_shb + 1
    3549    13980800 :                 W_tmp = W_add( W_tmp, W_deposit32_l( L_tmp ) );
    3550             :             }
    3551      174760 :             p_buf--;
    3552      174760 :             tmp_e = W_norm( W_tmp );
    3553      174760 :             L_tmp1 = W_extract_h( W_shl( W_tmp, tmp_e ) ); // 2 * Q_shb + tmp_e - 31
    3554      174760 :             L_tmp = Mpy_32_16_1( L_tmp1, scale_fx );       /* 2 * Q_shb + tmp_e - 31 - scale_e */
    3555             :             //*p_acorr++ = extract_h( L_shl( L_tmp, add( 16, Q_shb ) - ( 2 * Q_shb + tmp_e - 31 - scale_e ) ) );
    3556      174760 :             *p_acorr++ = extract_h( L_shl( L_tmp, sub( add( 16, Q_shb ), sub( sub( add( shl( Q_shb, 1 ), tmp_e ), 31 ), scale_e ) ) ) );
    3557      174760 :             move16();
    3558             :         }
    3559             : 
    3560             :         /* calculate variance of the SHB autocorrelation function */
    3561             :         // acorr_v2a = sum2_f( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN ) / ( ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN );
    3562        4369 :         tmp_e = sub( 15, Q_shb );
    3563        4369 :         L_tmp = Mpy_32_32( sum2_16_exp_fx( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN, &tmp_e, 0 ), 53687091 /* 1 / ( ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN ) in Q31 */ );
    3564             : 
    3565             :         /* multiply with the maximum of the SHB autocorrelation function */
    3566        4369 :         maximum_fx( acorr_EnvSHBres, ENVSHBRES_ACORR_MAX - ENVSHBRES_ACORR_MIN, &temp );
    3567             :         // acorr_v2a *= temp;
    3568        4369 :         L_tmp = Mpy_32_16_1( L_tmp, temp ); /* 31 - tmp_e + Q_shb - 15 */
    3569             : 
    3570             :         // alpha = 25.0f;
    3571        4369 :         alpha_fx = -1677721600; // Q26
    3572        4369 :         move32();
    3573             : 
    3574             :         // acorr_v2a = 2.0f / ( 1.0f + (float) expf( -alpha * acorr_v2a ) ) - 1.0f;
    3575        4369 :         Word32 exp_tmp = 1459366444;
    3576        4369 :         Word16 exp_e = 2;
    3577        4369 :         Word16 res_e = 0;
    3578        4369 :         move32();
    3579        4369 :         move16();
    3580        4369 :         move16();
    3581             : 
    3582        4369 :         L_tmp = Mpy_32_32( alpha_fx, L_tmp ); // 31 - tmp_e + Q_shb - 15 - 5
    3583             : 
    3584        4369 :         tmp_e = sub( 31, sub( sub( add( sub( 31, tmp_e ), Q_shb ), 15 ), 5 ) );
    3585             : 
    3586             :         // L_tmp = BASOP_Util_fPow(exp_tmp, exp_e, 2, 31, &res_e);
    3587             : 
    3588        4369 :         L_tmp = BASOP_Util_fPow( exp_tmp, exp_e, L_tmp, tmp_e, &res_e );
    3589        4369 :         L_tmp = BASOP_Util_Add_Mant32Exp( ONE_IN_Q31, 0, L_tmp, res_e, &tmp_e );
    3590        4369 :         acorr_v2a_fx = BASOP_Util_Divide3232_Scale( 2, L_tmp, &res_e );
    3591             : 
    3592        4369 :         res_e = sub( add( res_e, 31 ), tmp_e );
    3593             : 
    3594        4369 :         res_e = BASOP_Util_Add_MantExp( acorr_v2a_fx, res_e, -1, 15, &acorr_v2a_fx );
    3595             : 
    3596             :         /* limit in the range 0.0 - 1.0 */
    3597             :         // acorr_v2a = min( 1.0f, max( 0.0f, acorr_v2a ) );
    3598        4369 :         acorr_v2a_fx = shl_sat( acorr_v2a_fx, res_e );
    3599             : 
    3600        4369 :         if ( acorr_v2a_fx < 0 )
    3601             :         {
    3602           9 :             acorr_v2a_fx = 0;
    3603           9 :             move16();
    3604             :         }
    3605             : 
    3606        4369 :         hBWE_TD->prev_shb_env_tilt_fx = shb_env_tilt_fx;
    3607        4369 :         move16();
    3608        4369 :         hBWE_TD->prev_Q_shb = Q_shb;
    3609        4369 :         move16();
    3610             :     }
    3611             : 
    3612             :     /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the ACELP core excitation */
    3613      146035 :     vf_ind_fx = 20;
    3614      146035 :     move16();
    3615             : 
    3616             :     /* Save the SWB LSP values from current frame for interpolation */
    3617             : 
    3618             :     /* -------- start of  memory rescaling  -------- */
    3619             :     /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
    3620      146035 :     Lmax = L_deposit_l( 0 );
    3621    93608435 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    3622             :     {
    3623    93462400 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
    3624             :     }
    3625      146035 :     Q_bwe_exc = norm_l( Lmax );
    3626      146035 :     if ( Lmax == 0 )
    3627             :     {
    3628           3 :         Q_bwe_exc = 31;
    3629           3 :         move16();
    3630             :     }
    3631      146035 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_new, Q_new ) );
    3632             : 
    3633             :     /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
    3634      146035 :     find_max_mem_enc( st_fx, &n_mem, &n_mem2 );
    3635             : 
    3636      146035 :     tmp2 = add( hBWE_TD->prev_Q_bwe_exc, n_mem );
    3637      146035 :     if ( GT_16( Q_bwe_exc, tmp2 ) )
    3638             :     {
    3639       52821 :         Q_bwe_exc = tmp2;
    3640       52821 :         move16();
    3641             :     }
    3642             : 
    3643             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    3644      146035 :     sc = sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_exc );
    3645      146035 :     IF( sc != 0 )
    3646             :     {
    3647       49933 :         rescale_genSHB_mem_enc( st_fx, sc );
    3648             :     }
    3649             : 
    3650      146035 :     sc = sub( Q_bwe_exc, add( Q_new, Q_new ) );
    3651             :     /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm  */
    3652    95360855 :     FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
    3653             :     {
    3654    95214820 :         bwe_exc_extended_16[cnt] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
    3655    95214820 :         move16();
    3656             :     }
    3657             : 
    3658             :     /* 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 */
    3659      146035 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    3660             : 
    3661             :     /* save the previous Q factor of the buffer */
    3662      146035 :     hBWE_TD->prev_Q_bwe_exc = Q_bwe_exc;
    3663      146035 :     move16();
    3664             : 
    3665      146035 :     Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
    3666             : 
    3667             :     /* -------- end of rescaling memories -------- */
    3668      146035 :     Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
    3669      146035 :     move16();
    3670             : 
    3671      146035 :     GenShapedSHBExcitation_ivas_enc_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx, hBWE_TD->mem_csfilt_fx,
    3672      146035 :                                         hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx, st_fx->coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed,
    3673      146035 :                                         vf_modified_fx, st_fx->extl, &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_Q31,
    3674      146035 :                                         shb_res_gshape_fx, shb_res_fx, &vf_ind_fx, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx, &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc,
    3675      146035 :                                         &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st_fx->total_brate, 0, st_fx->element_mode, st_fx->flag_ACELP16k, nlExc16k_fx,
    3676             :                                         nlExc16k_e, mixExc16k_fx, mixExc16k_e, st_fx->extl_brate, MSFlag, EnvSHBres_4k_norm_fx, Q_EnvSHBres_4k_norm,
    3677             :                                         &( hBWE_TD->prev_pow_exc16kWhtnd_fx32 ), &( hBWE_TD->prev_mix_factor_fx ), &Env_error_fx, Env_error_part_fx );
    3678             : 
    3679      146035 :     *Q_white_exc = Q_bwe_exc_fb;
    3680      146035 :     move16();
    3681      146035 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
    3682             :     {
    3683       61717 :         hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
    3684       61717 :         move16();
    3685             :     }
    3686             :     ELSE
    3687             :     {
    3688             :         /*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.
    3689             :         51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
    3690       84318 :         hBWE_TD->prev_Q_bwe_exc_fb = 51;
    3691       84318 :         move16();
    3692             :     }
    3693             : 
    3694             :     /* fill-in missing SHB excitation */
    3695      146035 :     test();
    3696      146035 :     test();
    3697      146035 :     IF( ( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) || EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) ) && LE_32( st_fx->last_core_brate, SID_2k40 ) )
    3698             :     {
    3699         203 :         Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    3700             :     }
    3701             : 
    3702      146035 :     test();
    3703      146035 :     IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && hStereoICBWE != NULL )
    3704             :     {
    3705       33208 :         Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
    3706       33208 :         hStereoICBWE->shbSynthRef_e = sub( 15, Q_bwe_exc );
    3707       33208 :         move16();
    3708             :     }
    3709             : 
    3710      146035 :     test();
    3711      146035 :     test();
    3712      146035 :     IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3713             :     {
    3714       36639 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    3715             :         {
    3716           0 :             hBWE_TD->idx_mixFac = vf_ind_fx;
    3717           0 :             move16();
    3718             :         }
    3719             :         ELSE
    3720             :         {
    3721       36639 :             push_indice( hBstr, IND_SHB_VF, vf_ind_fx, NUM_BITS_SHB_VF );
    3722             :         }
    3723             :     }
    3724             : 
    3725     1606385 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    3726             :     {
    3727     1460350 :         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 ) );
    3728     1460350 :         move16();
    3729             :     }
    3730             : 
    3731      146035 :     test();
    3732      146035 :     IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3733      141666 :     {
    3734             : 
    3735      708330 :         FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    3736             :         {
    3737      566664 :             PostShortTerm_ivas_enc_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
    3738      566664 :                                        hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
    3739             :             /* i: shaped_shb_excitation_fx in Q_bwe_exc */
    3740             :             /* i: lpc_shb_fx in Q12 */
    3741             :         }
    3742      141666 :         Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k );
    3743             : 
    3744             :         Word16 max_val;
    3745      141666 :         maximum_abs_16_fx( shaped_shb_excitation_fx, L_FRAME16k + L_SHB_LAHEAD, &max_val );
    3746      141666 :         IF( max_val == 0 )
    3747             :         {
    3748           3 :             Lscale = ONE_IN_Q31; /* 1.0f in Q31 */
    3749           3 :             move32();
    3750           3 :             IF( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
    3751             :             {
    3752           0 :                 Lscale = ONE_IN_Q30; /* sqrtf(0.25) = 0.5 in Q31 */
    3753           0 :                 move32();
    3754             :             }
    3755           3 :             exp = 0;
    3756           3 :             move16();
    3757             :         }
    3758             :         ELSE
    3759             :         {
    3760             :             Word64 prev_pow_64fx, curr_pow_64fx;
    3761             :             Word16 w_shift;
    3762      141663 :             tmp = sub( shl( Q_bwe_exc, 1 ), 31 );
    3763      141663 :             prev_pow_64fx = W_shl( 21475ll /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
    3764      141663 :             curr_pow_64fx = W_shl( 21475ll /*0.00001f Q31*/, tmp ); /* 2*(Q_bwe_exc) */
    3765     4391553 :             FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
    3766             :             {
    3767     4249890 :                 prev_pow_64fx = W_mac0_16_16( prev_pow_64fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] );                                         /* 2*Q_bwe_exc */
    3768     4249890 :                 curr_pow_64fx = W_mac0_16_16( curr_pow_64fx, shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
    3769             :             }
    3770             : 
    3771      141663 :             IF( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
    3772             :             {
    3773             :                 /*curr_pow_fx = Mult_32_16( curr_pow_fx, 8192);*/ /* Q(2*Q_bwe_exc) */
    3774       11054 :                 curr_pow_64fx = W_shr( curr_pow_64fx, 2 );        /* scale by 0.25 */
    3775             :             }
    3776             : 
    3777      141663 :             w_shift = s_min( W_norm( prev_pow_64fx ), W_norm( curr_pow_64fx ) );
    3778      141663 :             prev_pow_fx = W_extract_h( W_shl( prev_pow_64fx, w_shift ) ); // 2*(Q_bwe_exc)+w_shift-32
    3779      141663 :             curr_pow_fx = W_extract_h( W_shl( curr_pow_64fx, w_shift ) ); // 2*(Q_bwe_exc)+w_shift-32
    3780      141663 :             w_shift = sub( add( shl( Q_bwe_exc, 1 ), w_shift ), 32 );
    3781             : 
    3782      141663 :             Lscale = root_a_over_b_fx( curr_pow_fx,
    3783             :                                        w_shift,
    3784             :                                        prev_pow_fx,
    3785             :                                        w_shift,
    3786             :                                        &exp );
    3787             :         }
    3788             : 
    3789     2974986 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    3790             :         {
    3791     2833320 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q(16-exp+Q_bwe_exc) */
    3792     2833320 :             shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    3793     2833320 :             move16();
    3794             :         }
    3795      141666 :         IF( exp < 0 )
    3796             :         {
    3797        4898 :             Lscale = L_shl( Lscale, exp );
    3798        4898 :             exp = 0;
    3799        4898 :             move16();
    3800             :         }
    3801             :         /*
    3802             :            code for EVS and IVAS are basically identical with the exception of i_mult_sat() which has precision issues
    3803             :            thus is was replaced for IVAS and kept for EVS, in order to keep EVS BE to test sequences and legacy implementations
    3804             :         */
    3805      141666 :         IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
    3806             :         {
    3807           0 :             FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    3808             :             {
    3809           0 :                 tmp = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ );        /* Q15 */
    3810           0 :                 L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), tmp ); /* Q31-exp */
    3811           0 :                 tmp = sub( 32767 /*1.0f Q15*/, tmp );
    3812           0 :                 L_tmp = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 );
    3813           0 :                 L_tmp = Mult_32_16( L_tmp, shaped_shb_excitation_fx[i] );              /* Q_bwe_exc + (31-exp) - 15 */
    3814           0 :                 shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    3815           0 :                 move16();
    3816             :             }
    3817             :         }
    3818             :         ELSE
    3819             :         {
    3820     1558326 :             FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    3821             :             {
    3822     1416660 :                 tmp = round_fx_sat( L_shl( L_mult( 0x6666 /* 0.1 in Q12 */, shl( sub( i, 19 ), 11 ) ), 1 ) );
    3823     1416660 :                 L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), tmp ); /* Q31-exp */
    3824     1416660 :                 tmp = sub( 32767 /*1.0f Q15*/, tmp );
    3825     1416660 :                 L_tmp = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 );
    3826     1416660 :                 L_tmp = Mult_32_16( L_tmp, shaped_shb_excitation_fx[i] );              /* Q_bwe_exc + (31-exp) - 15 */
    3827     1416660 :                 shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    3828     1416660 :                 move16();
    3829             :             }
    3830             :         }
    3831             :     }
    3832             :     ELSE
    3833             :     {
    3834             :         /* reset the PF memories if the PF is not running */
    3835        4369 :         set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
    3836        4369 :         hBWE_TD->gain_prec_swb_fx = ONE_IN_Q14;
    3837        4369 :         move16();
    3838        4369 :         set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
    3839             :     }
    3840             : 
    3841             :     /* Update SHB excitation */
    3842      146035 :     Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
    3843             : 
    3844             :     /* Estimate the gain-shape parameter */
    3845      146035 :     n_subfr_saturation = 0;
    3846      146035 :     move16();
    3847      146035 :     EstimateSHBGainShape_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx,
    3848             :                              Q_bwe_exc, GainShape_fx, subwin_shb_fx, &n_subfr_saturation, 0 );
    3849             : 
    3850      146035 :     test();
    3851      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3852             :     {
    3853             :         /* Gain shape attenuation in case of big error in envelope modelling */
    3854       21845 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3855             :         {
    3856       17476 :             IF( GT_16( Env_error_part_fx[i], 100 ) )
    3857             :             {
    3858             :                 /* set gain shape to the average of the remaining gains */
    3859             :                 // GainShape[i] = ( sum_f( GainShape, NUM_SHB_SUBGAINS ) - GainShape[i] ) / ( NUM_SHB_SUBGAINS - 1 );
    3860          66 :                 L_tmp = L_sub( sum16_32_fx( GainShape_fx, NUM_SHB_SUBGAINS ), GainShape_fx[i] );
    3861          66 :                 tmp = BASOP_Util_Divide3232_Scale( L_tmp, NUM_SHB_SUBGAINS - 1, &exp );
    3862          66 :                 GainShape_fx[i] = shl( tmp, sub( exp, 15 ) ); /* Q15 */
    3863          66 :                 move16();
    3864             :             }
    3865             :         }
    3866             :     }
    3867             : 
    3868      146035 :     test();
    3869      146035 :     IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3870             :     {
    3871             :         /* Gain shape BWS/high band low energy fix */
    3872      141666 :         IF( LT_16( hBWE_TD->cldfbHBLT, 8192 /*1.0f Q13*/ ) ) /* cldfbHBLT in Q13 */
    3873             :         {
    3874             :             /* There is not much HB past 10kHz; the temporal resolution is quite coarse, so reduce the dynamic range */
    3875       28180 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3876             :             {
    3877             :                 /* 0.35f +/- delta variation; delta = 0.1*(GS-0.35)*/
    3878             :                 /* GainShape[i] = 0.315f + 0.1f * GainShape[i]; */
    3879       22544 :                 GainShape_fx[i] = mac_r( 676457349l /*0.315 Q31*/, 3277 /*0.1 Q15*/, GainShape_fx[i] );
    3880       22544 :                 move16();
    3881             :             }
    3882             :         }
    3883             :     }
    3884             : 
    3885             :     /* high-band gain control in case of BWS */
    3886      146035 :     test();
    3887      146035 :     IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3888             :     {
    3889      141666 :         IF( st_fx->bwidth_sw_cnt > 0 )
    3890             :         {
    3891        4170 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3892             :             {
    3893             :                 // GainShape[i] *= ( (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
    3894        3336 :                 tmp = i_mult( st_fx->bwidth_sw_cnt, ONE_BY_BWS_TRAN_PERIOD_Q15 );
    3895        3336 :                 GainShape_fx[i] = mult( GainShape_fx[i], tmp );
    3896        3336 :                 move16();
    3897             :             }
    3898             :         }
    3899             :     }
    3900             : 
    3901             :     /* Gain frame adjustment factor */
    3902             :     /* log( (GainShape[0]) / (st->prev_wb_GainShape) )*/
    3903      146035 :     IF( hBWE_TD->prev_swb_GainShape_fx == 0 )
    3904             :     {
    3905        7321 :         exp = 13 /* norm_s(3) */;
    3906        7321 :         tmp = 21845 /* div_s( shl(1, sub(14, exp)), 3 /\* 0.0001 in Q15 *\/ ) */;
    3907             :     }
    3908             :     ELSE
    3909             :     {
    3910      138714 :         exp = norm_s( hBWE_TD->prev_swb_GainShape_fx );
    3911      138714 :         tmp = div_s( shl( 1, sub( 14, exp ) ), hBWE_TD->prev_swb_GainShape_fx );
    3912             :     }
    3913             : 
    3914      146035 :     L_tmp = L_mult( GainShape_fx[0], tmp ); /*Q(30 - exp) */
    3915             : 
    3916      146035 :     exp1 = norm_l( L_tmp );
    3917      146035 :     frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) ); /*move16(); */
    3918      146035 :     exp1 = sub( exp, exp1 );                     /*move16(); */
    3919      146035 :     L_tmp = Mpy_32_16( exp1, frac, 22713 );
    3920      146035 :     temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
    3921             : 
    3922      146035 :     L_feedback = L_mult( temp_swb_fac, temp_swb_fac );
    3923             : 
    3924             : 
    3925      584140 :     FOR( i = 1; i < NUM_SHB_SUBGAINS; i++ )
    3926             :     {
    3927      438105 :         test();
    3928      438105 :         IF( GainShape_fx[i - 1] == 0 )
    3929             :         {
    3930        9748 :             exp = 13 /* norm_s(3) */;
    3931        9748 :             tmp = 21845 /* div_s( shl(1, sub(14, exp)), 3 /\* 0.0001 in Q15 *\/ ) */;
    3932             :         }
    3933             :         ELSE
    3934             :         {
    3935      428357 :             exp = norm_s( GainShape_fx[i - 1] );
    3936      428357 :             tmp = div_s( shl( 1, sub( 14, exp ) ), GainShape_fx[i - 1] );
    3937             :         }
    3938      438105 :         L_tmp = L_mult( GainShape_fx[i], tmp ); /* Q(30 - exp) */
    3939             : 
    3940      438105 :         exp1 = norm_l( L_tmp );
    3941      438105 :         frac = Log2_norm_lc( L_shl( L_tmp, exp1 ) );
    3942      438105 :         exp1 = sub( exp, exp1 );
    3943      438105 :         L_tmp = Mpy_32_16( exp1, frac, 22713 );
    3944      438105 :         temp_swb_fac = round_fx( L_shl( L_tmp, 10 ) );
    3945             : 
    3946      438105 :         L_feedback = L_mac( L_feedback, temp_swb_fac, temp_swb_fac );
    3947             :     }
    3948             : 
    3949             :     /* feedback = 0.4f / (1 + 0.5f * feedback) */
    3950      146035 :     L_tmp = L_add( L_shr( L_feedback, 1 ), L_shl( 1, 21 ) ); /* Q21 */
    3951      146035 :     IF( L_tmp != 0 )
    3952             :     {
    3953      146035 :         exp = norm_l( L_tmp );
    3954      146035 :         tmp = extract_h( L_shl( L_tmp, exp ) );
    3955      146035 :         exp = sub( sub( 30, exp ), 21 );
    3956      146035 :         tmp = div_s( 16384, tmp );                  /*Q(15+exp) */
    3957      146035 :         L_tmp = L_shr( L_mult( 13107, tmp ), exp ); /*Q31 */
    3958      146035 :         feedback = round_fx( L_tmp );               /*Q15 */
    3959             :     }
    3960             :     ELSE
    3961             :     {
    3962           0 :         feedback = 8738;
    3963           0 :         move16(); /*Q15 */
    3964             :     }
    3965             : 
    3966             :     /* calculate tilt in all sub-frame gains */
    3967      146035 :     Copy( GainShape_fx, GainShape_Interp_fx, NUM_SHB_SUBGAINS );
    3968      146035 :     lls_interp_n_fx( GainShape_Interp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp, 1 );
    3969             : 
    3970      146035 :     test();
    3971      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3972             :     {
    3973             :         // if (acorr_v2a < 0.4f && vf_ind >= 5 && fabs(GainShape_tilt) < 0.2f && shb_env_tilt < 500.0f && hBWE_TD->prev_shb_env_tilt < 500.0f)
    3974        4369 :         test();
    3975        4369 :         test();
    3976        4369 :         test();
    3977        4369 :         test();
    3978        4369 :         IF( LT_16( acorr_v2a_fx, 13107 ) /* 0.4f in Q15 */ && GE_16( vf_ind_fx, 5 ) && LT_16( abs_s( GainShape_tilt_fx ), 6554 /* 0.2f in Q15 */ ) && LT_32( L_deposit_l( shb_env_tilt_fx ), L_shl( 500, Q_shb ) ) && LT_32( L_deposit_l( hBWE_TD->prev_shb_env_tilt_fx ), L_shl( 500, hBWE_TD->prev_Q_shb ) ) )
    3979             :         {
    3980             :             /* stronger smoothing in case of unvoiced SHB residual signal with gaussian excitation */
    3981             :             // feedback = lin_interp(acorr_v2a, 0.4f, 0.0f, 0.0f, 0.95f, 0);
    3982        2876 :             feedback = lin_interp_ivas_fx( acorr_v2a_fx, 13107 /* 0.4f in Q15 */, 0, 0, 31130 /* 0.95f in Q15 */, 0 );
    3983             :             // feedback = min( max( feedback, 0.0f ), 1.0f );
    3984        2876 :             feedback = s_min( s_max( feedback, 0 ), MAX16B );
    3985             : 
    3986       14380 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3987             :             {
    3988             :                 // GainShape[i] = ( 1 - feedback ) * GainShape[i] + feedback * GainShape_Interp[i];
    3989       11504 :                 GainShape_fx[i] = extract_h( L_add( L_mult( sub( MAX16B, feedback ), GainShape_fx[i] ), L_mult( feedback, GainShape_Interp_fx[i] ) ) );
    3990       11504 :                 move16();
    3991             :             }
    3992             :         }
    3993             :     }
    3994             :     ELSE
    3995             :     {
    3996             :         /* voicing in Q15 */
    3997      141666 :         L_tmp = L_mult( voicing[0], 8192 );
    3998      141666 :         L_tmp = L_mac( L_tmp, voicing[1], 8192 );
    3999      141666 :         L_tmp = L_mac( L_tmp, voicing[2], 8192 );
    4000      141666 :         tmp = sum1 = round_fx( L_tmp ); /* sum1 used again below - Q13 */
    4001      141666 :         tmp = add( tmp, 1 );            /* To ensure change is BE */
    4002             : 
    4003             :         /* voice_factors_fx in Q15 */
    4004      141666 :         L_tmp = L_mult( voice_factors_fx[0], 8192 );
    4005      141666 :         L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
    4006      141666 :         L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
    4007      141666 :         L_tmp = L_mac( L_tmp, voice_factors_fx[3], 8192 );
    4008      141666 :         tmp1 = sum2 = round_fx( L_tmp ); /* sum2 used again below - Q13 */
    4009             : 
    4010             : 
    4011      141666 :         test();
    4012      141666 :         test();
    4013      141666 :         IF( EQ_16( frGainAttenuate, 1 ) || ( GT_16( tmp, 19661 /* 2.4f in Q13 */ ) && GT_16( tmp1, 6554 /* 0.8f in Q13 */ ) ) )
    4014             : 
    4015             :         {
    4016       43355 :             temp_swb_fac = hBWE_TD->prev_swb_GainShape_fx;
    4017       43355 :             move16();
    4018      216775 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4019             :             {
    4020             :                 /*GainShape_fx[i] = (1 - feedback) * GainShape[i] + feedback * temp_swb_fac; */
    4021      173420 :                 GainShape_fx[i] = round_fx( L_mac( L_mult( sub( 32767, feedback ), GainShape_fx[i] ), feedback, temp_swb_fac ) );
    4022      173420 :                 move16();
    4023      173420 :                 temp_swb_fac = GainShape_fx[i];
    4024      173420 :                 move16();
    4025             :             }
    4026             :         }
    4027             :     }
    4028             : 
    4029      146035 :     test();
    4030      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4031             :     {
    4032             :         /* Re-normalize gain shape before quantization */
    4033             :         // sum_gain = sum2_f(GainShape, NUM_SHB_SUBGAINS);
    4034        4369 :         tmp = 0;
    4035        4369 :         move16();
    4036        4369 :         sum_gain_fx = sum2_16_exp_fx( GainShape_fx, NUM_SHB_SUBGAINS, &tmp, 2 );
    4037        4369 :         IF( sum_gain_fx == 0 )
    4038             :         {
    4039           9 :             normFact_fx = 0;
    4040           9 :             move16();
    4041             :         }
    4042             :         ELSE
    4043             :         {
    4044             :             // normFact = (float) sqrt( 1.0f / sum_gain );
    4045        4360 :             normFact_fx = ISqrt32( sum_gain_fx, &tmp );
    4046             :         }
    4047             : 
    4048             :         // v_multc( GainShape, normFact, GainShape, NUM_SHB_SUBGAINS );
    4049       21845 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; ++i )
    4050             :         {
    4051       17476 :             GainShape_fx[i] = extract_h( L_shl( Mpy_32_16_1( normFact_fx, GainShape_fx[i] ), tmp ) ); /* Q15 */
    4052       17476 :             move16();
    4053             :         }
    4054             :     }
    4055             : 
    4056      146035 :     hBWE_TD->prev_swb_GainShape_fx = GainShape_fx[3];
    4057      146035 :     move16();
    4058             : 
    4059             :     /* Compute the power of gains away from the peak gain prior to quantization */
    4060      146035 :     IF( st_fx->element_mode > EVS_MONO )
    4061             :     {
    4062      146035 :         p2m_in_fx = pow_off_pk_corrected_fx( GainShape_fx, NUM_SHB_SUBGAINS, 1 );
    4063             :     }
    4064             :     ELSE
    4065             :     {
    4066           0 :         p2m_in_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBGAINS, 1 );
    4067             :     }
    4068             : 
    4069             :     /* Quantization of the gain shape parameter */
    4070      146035 :     QuantizeSHBsubgains_ivas_fx( st_fx, GainShape_fx, st_fx->extl );
    4071             : 
    4072             :     /* Compute the power of gains away from the peak gain after quantization */
    4073      146035 :     IF( st_fx->element_mode > EVS_MONO )
    4074             :     {
    4075      146035 :         p2m_out_fx = pow_off_pk_corrected_fx( GainShape_fx, NUM_SHB_SUBFR, 4 );
    4076             :     }
    4077             :     ELSE
    4078             :     {
    4079           0 :         p2m_out_fx = pow_off_pk_fx( GainShape_fx, NUM_SHB_SUBFR, 4 );
    4080             :     }
    4081             : 
    4082             :     /* Gain shape smoothing after quantization */
    4083      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4084             :     {
    4085       21845 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4086             :         {
    4087       17476 :             GainShape_Interp_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS];
    4088       17476 :             move16();
    4089             :         }
    4090             : 
    4091        4369 :         lls_interp_n_fx( GainShape_Interp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp, 1 );
    4092             : 
    4093             :         // if ( vf_ind >= 6 && fabs( GainShape_tilt ) < 0.12f )
    4094        4369 :         test();
    4095        4369 :         IF( GE_16( vf_ind_fx, 6 ) && LT_16( abs_s( GainShape_tilt_fx ), 3932 /* 0.12f in Q15 */ ) )
    4096             :         {
    4097             :             // feedback = 0.3f;
    4098        1982 :             feedback = 9830; /* 0.3f in Q15 */
    4099        1982 :             move16();
    4100        9910 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4101             :             {
    4102             :                 // GainShape[i] = ( 1 - feedback ) * GainShape[i * NUM_SHB_SUBGAINS] + feedback * GainShape_Interp[i];
    4103        7928 :                 GainShape_fx[i] = extract_h( L_add( L_mult( sub( MAX16B, feedback ), GainShape_fx[i * NUM_SHB_SUBGAINS] ), L_mult( feedback, GainShape_Interp_fx[i] ) ) ); // Q15
    4104        7928 :                 move16();
    4105             :             }
    4106             : 
    4107       31712 :             FOR( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
    4108             :             {
    4109       29730 :                 GainShape_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    4110       29730 :                 move16();
    4111             :             }
    4112             :         }
    4113             :     }
    4114             : 
    4115             :     /* Estimate the gain parameter */
    4116      146035 :     EstimateSHBFrameGain_ivas_fx( SHB_OVERLAP_LEN, shb_frame_fx, Q_shb, shaped_shb_excitation_fx, Q_bwe_exc,
    4117             :                                   GainShape_fx, &GainFrame_fx, window_shb_fx, subwin_shb_fx, n_subfr_saturation );
    4118             : 
    4119      146035 :     IF( EQ_16( st_fx->tec_tfa, 1 ) )
    4120             :     {
    4121           0 :         tfaCalcEnv_fx( shb_frame_fx, st_fx->tfa_enr );
    4122             :     }
    4123             : 
    4124             :     /* If there's a big difference in the power of gains away from the peak gain */
    4125             :     /* due to poor quantization then suppress energy of the high band. */
    4126      146035 :     IF( GT_32( p2m_out_fx, L_shl( p2m_in_fx, 1 ) ) )
    4127             :     {
    4128        4260 :         L_tmp = root_a_over_b_fx( p2m_in_fx, 28, p2m_out_fx, 29, &exp_out );
    4129        4260 :         GainFrame_fx = L_shl( Mult_32_32( GainFrame_fx, L_tmp ), exp_out ); /* Q18 */
    4130             :     }
    4131             : 
    4132             :     // if ( st_fx->element_mode > EVS_MONO && st_fx->L_frame != st_fx->last_L_frame && hBWE_TD->prev_gainFr_SHB != 0 && ( st_fx->last_extl == SWB_TBE || st_fx->last_extl == FB_TBE ) && st_fx->coder_type == TRANSITION && st_fx->coder_type_raw != VOICED && st_fx->clas == VOICED_CLAS && st_fx->last_clas == VOICED_CLAS && ( 3.0f * voice_factors[0] < voice_factors[( st_fx->L_frame >> 6 ) - 1] ) )
    4133      146035 :     test();
    4134      146035 :     test();
    4135      146035 :     test();
    4136      146035 :     test();
    4137      146035 :     test();
    4138      146035 :     test();
    4139      146035 :     test();
    4140      146035 :     test();
    4141      146035 :     test();
    4142      146035 :     IF( st_fx->element_mode > EVS_MONO && NE_16( st_fx->L_frame, st_fx->last_L_frame ) && hBWE_TD->prev_gainFr_SHB_fx != 0 && ( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) ) && EQ_16( st_fx->coder_type, TRANSITION ) && NE_16( st_fx->coder_type_raw, VOICED ) && EQ_16( st_fx->clas, VOICED_CLAS ) && EQ_16( st_fx->last_clas, VOICED_CLAS ) && LT_16( mult( 24576 /* 3.0f in Q13 */, voice_factors_fx[0] ), shr( voice_factors_fx[( st_fx->L_frame >> 6 ) - 1], 2 ) ) )
    4143             :     {
    4144             :         // float fac = GainFrame / hBWE_TD->prev_gainFr_SHB;
    4145          76 :         Word16 fac = BASOP_Util_Divide3232_Scale( GainFrame_fx, hBWE_TD->prev_gainFr_SHB_fx, &tmp_e );
    4146             : 
    4147             :         // if ( fac > 4.0f )
    4148          76 :         IF( BASOP_Util_Cmp_Mant32Exp( L_deposit_h( fac ), tmp_e, 1073741824, 3 ) > 0 )
    4149             :         {
    4150             :             // GainFrame = 4.0f * GainFrame / fac;
    4151           2 :             GainFrame_fx = BASOP_Util_Divide3216_Scale( GainFrame_fx, fac, &tmp1_e );
    4152           2 :             GainFrame_fx = L_shl( GainFrame_fx, add( tmp1_e, 2 ) ); /* Q18 */
    4153             :         }
    4154             :     }
    4155             : 
    4156      146035 :     test();
    4157      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4158             :     {
    4159             :         // if ( acorr_v2a > 0.1f && Env_error > 5.0f )
    4160        4369 :         test();
    4161        4369 :         test();
    4162        4369 :         IF( GT_16( acorr_v2a_fx, 3277 /* 0.1f in Q15 */ ) && GT_16( Env_error_fx, 5 ) )
    4163             :         {
    4164             :             /* attenuate frame gain in case of voiced SHB residual signal with gaussian excitation */
    4165             :             // fact_atten = lin_interp( Env_error, 5.0f, 1.0f, 25.0f, 0.2f, 1 );
    4166           0 :             fact_atten_fx = lin_interp_ivas_fx( Env_error_fx, 5, MAX16B, 25, 6554 /* 0.2f in Q15 */, 1 );
    4167             :             // GainFrame *= fact_atten;
    4168           0 :             GainFrame_fx = Mpy_32_16_1( GainFrame_fx, fact_atten_fx );
    4169             :         }
    4170             :         /* Frame gain attenuation in case of big error in envelope modelling */
    4171             :         // else if ( Env_error > 100.0f || hBWE_TD->prev_Env_error > 100.0f )
    4172        4369 :         ELSE IF( GT_16( Env_error_fx, 100 ) || GT_16( hBWE_TD->prev_Env_error_fx, 100 ) )
    4173             :         {
    4174             :             // if ( Env_error > 100.0f )
    4175         106 :             IF( GT_16( Env_error_fx, 100 ) )
    4176             :             {
    4177             :                 // fact_atten = lin_interp( Env_error, 100.0f, 1.0f, 300.0f, 0.1f, 1 );
    4178          57 :                 fact_atten_fx = lin_interp_ivas_fx( Env_error_fx, 100, MAX16B, 300, 3277 /* 0.1f in Q15 */, 1 );
    4179             :             }
    4180             :             ELSE
    4181             :             {
    4182             :                 // fact_atten = lin_interp( hBWE_TD->prev_Env_error, 100.0f, 1.0f, 300.0f, 0.5f, 1 );
    4183          49 :                 fact_atten_fx = lin_interp_ivas_fx( hBWE_TD->prev_Env_error_fx, 100, MAX16B, 300, ONE_IN_Q14 /* 0.5f in Q15 */, 1 );
    4184             :             }
    4185             :             // GainFrame *= fact_atten;
    4186         106 :             GainFrame_fx = Mpy_32_16_1( GainFrame_fx, fact_atten_fx );
    4187             :         }
    4188             :     }
    4189             :     ELSE
    4190             :     {
    4191      141666 :         test();
    4192      141666 :         IF( EQ_16( frGainSmoothEn, 1 ) && LT_32( hBWE_TD->prev_gainFr_SHB_fx, GainFrame_fx ) )
    4193             :         {
    4194           4 :             GainFrame_fx = L_add( L_shr( hBWE_TD->prev_gainFr_SHB_fx, 1 ), L_shr( GainFrame_fx, 1 ) ); /* Q18 */
    4195             :         }
    4196             :     }
    4197             : 
    4198      146035 :     test();
    4199      146035 :     test();
    4200      146035 :     IF( EQ_16( frGainAttenuate, 1 ) && LE_16( MA_lsp_shb_spacing, 79 /*0.0024f Q15*/ ) )
    4201             :     {
    4202           7 :         exp1 = norm_l( GainFrame_fx );
    4203           7 :         frac = Log2_norm_lc( L_shl( GainFrame_fx, exp1 ) );
    4204           7 :         exp = sub( 30, add( exp1, 21 ) );
    4205           7 :         L_tmp = Mpy_32_16( exp, frac, 26214 ); /* Q16 */
    4206           7 :         frac = L_Extract_lc( L_tmp, &exp );
    4207           7 :         GainFrame_fx = Pow2( 30, frac );
    4208           7 :         exp = sub( exp, 30 );
    4209           7 :         GainFrame_fx = L_shl( GainFrame_fx, add( exp, 18 ) ); /* Q18 */
    4210             :     }
    4211      146028 :     ELSE IF( EQ_16( hBWE_TD->prev_frGainAtten, 1 ) && GT_32( Mult_32_16( GainFrame_fx, 10923 ), hBWE_TD->prev_gainFr_SHB_fx ) )
    4212             :     {
    4213             :         /*GainFrame *= (0.8f + 0.5f*feedback); */
    4214          51 :         tmp = add( 13107 /* 0.8 in Q14 */, shr( feedback, 2 ) );
    4215          51 :         GainFrame_fx = L_shl( Mult_32_16( GainFrame_fx, tmp ), 1 ); /* Q18 */
    4216             :     }
    4217             : 
    4218      146035 :     hBWE_TD->prev_frGainAtten = frGainAttenuate;
    4219      146035 :     move16();
    4220             : 
    4221      146035 :     hBWE_TD->prev_gainFr_SHB_fx = GainFrame_fx;
    4222      146035 :     move16(); /* Q18 */
    4223             : 
    4224             :     // if ( GainFrame > 153.0f && st_fx->element_mode >= IVAS_CPE_DFT && hStereoICBWE != NULL )
    4225      146035 :     test();
    4226      146035 :     test();
    4227      146035 :     if ( GT_32( GainFrame_fx, 40108032 /* 153.0f in Q18 */ ) && GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && hStereoICBWE != NULL )
    4228             :     {
    4229          99 :         hStereoICBWE->MSFlag = 1;
    4230          99 :         move16();
    4231             :     }
    4232             : 
    4233             :     /* Gain attenuation when the SWB LSF quantization error is larger than a threshold */
    4234      146035 :     tmp = mult_r( 25600 /*400 Q6*/, sd_uq_q_fx ); /* Q6 * Q15 => Q6 */
    4235             : 
    4236      146035 :     test();
    4237      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4238             :     {
    4239             :         // sd_uq_q = 1 - 0.7f * ( sd_uq_q * sd_uq_q );
    4240        4369 :         tmp1 = mult_r( 22938 /*0.7f Q15*/, tmp );       /* Q15, Q6  => Q6 */
    4241        4369 :         L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
    4242             :     }
    4243             :     ELSE
    4244             :     {
    4245      141666 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    4246             :         {
    4247       65754 :             tmp1 = mult_r( 6554 /*0.2f Q15*/, tmp );        /* Q15, Q6  => Q6 */
    4248       65754 :             L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
    4249             :         }
    4250             :         ELSE
    4251             :         {
    4252       75912 :             tmp1 = mult_r( 3277 /*0.1f Q15*/, tmp );        /* Q15, Q6  => Q6 */
    4253       75912 :             L_tmp = L_msu( 8192l /*1.0f Q13*/, tmp1, tmp ); /* Q13 */
    4254             :         }
    4255             :     }
    4256             :     /* sd_uq_q =  max(min(sd_uq_q, 1.0f), 0.5f); */
    4257      146035 :     L_tmp = L_min( L_tmp, 8192l /*1.0f Q13*/ );
    4258      146035 :     L_tmp = L_max( L_tmp, 4096l /*0.5f Q13*/ ); /* Q13 */
    4259             :     /* keep the L_tmp; dont overwrite */
    4260             : 
    4261      146035 :     test();
    4262      146035 :     IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4263             :     {
    4264             :         /* pitBufAvg =  0.0025f * sum_f(pitch_buf, 4); */
    4265             :         /* pitch_buf: Q6 */
    4266      141666 :         pitBufAvg_fx = mult_r( pitch_buf[0], 1311 );
    4267      141666 :         pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[1], 1311 ) );
    4268      141666 :         pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[2], 1311 ) );
    4269      141666 :         pitBufAvg_fx = add( pitBufAvg_fx, mult_r( pitch_buf[3], 1311 ) ); /* Q10 */
    4270             : 
    4271             :         /* voicingBufAvg = (sum_f(voice_factors, 4)=sum2 > 0.6f) ? 0.333f : 0.1667f; */
    4272      141666 :         tmp2 = 5462 /*0.1667f Q15*/;
    4273      141666 :         move16();
    4274      141666 :         if ( GT_16( sum2, 4915 /*0.6f Q13*/ ) )
    4275             :         {
    4276       85057 :             tmp2 = 10912 /*0.333f Q15*/; /* Q15 */
    4277       85057 :             move16();
    4278             :         }
    4279      141666 :         voicingBufAvg_fx = shl( mult( tmp2, sum1 ), 2 ); /* Q15 */
    4280             : 
    4281             :         // if ( voicingBufAvg <= 0.0f && sd_uq_q * pitBufAvg != 0 )
    4282      141666 :         test();
    4283      141666 :         IF( voicingBufAvg_fx <= 0 && Mpy_32_16_1( L_tmp, pitBufAvg_fx ) != 0 )
    4284             :         {
    4285             :             // voicingBufAvg = sd_uq_q * pitBufAvg / 1.001f;
    4286         274 :             tmp = BASOP_Util_Divide1616_Scale( pitBufAvg_fx, 16400, &tmp_e );
    4287         274 :             tmp_e = sub( add( tmp_e, 5 ), 1 );
    4288         274 :             L_tmp1 = Mpy_32_16_1( L_tmp, tmp );
    4289         274 :             voicingBufAvg_fx = extract_h( L_tmp1 );
    4290             :         }
    4291             :         // else if ( voicingBufAvg <= 0.0f )
    4292      141392 :         ELSE IF( voicingBufAvg_fx <= 0 )
    4293             :         {
    4294             :             // voicingBufAvg = 1.0f;
    4295           0 :             voicingBufAvg_fx = MAX16B;
    4296           0 :             move16();
    4297             :         }
    4298             :     }
    4299             : 
    4300             :     /* high-band gain control in case of BWS */
    4301      146035 :     test();
    4302      146035 :     IF( NE_32( st_fx->extl_brate, SWB_TBE_1k10 ) && NE_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4303             :     {
    4304      141666 :         IF( st_fx->bwidth_sw_cnt > 0 )
    4305             :         {
    4306             :             // GainFrame *= ( (float) ( st_fx->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD );
    4307         834 :             tmp = i_mult( st_fx->bwidth_sw_cnt, ONE_BY_BWS_TRAN_PERIOD_Q15 );
    4308         834 :             GainFrame_fx = Mpy_32_16_1( GainFrame_fx, tmp );
    4309             :         }
    4310             :     }
    4311             : 
    4312             :     /* Controlled gain evolution in SWB for stronger voiced segments */
    4313      146035 :     test();
    4314      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4315             :     {
    4316             :         // GainFrame *= sd_uq_q;
    4317        4369 :         GainFrame_fx = Mpy_32_16_1( GainFrame_fx, extract_l( L_shl( L_tmp, 2 ) ) );
    4318             :     }
    4319             :     ELSE
    4320             :     {
    4321             :         /* max(min((float)(sd_uq_q*pitBufAvg/voicingBufAvg), 1.0f), 0.6f) */
    4322             :         /* sd_uq_q: Q13, pitBufAvg_fx: Q6, voicingBufAvg_fx: Q15 */
    4323             :         /* 1/voicingBufAvg_fx */
    4324      141666 :         tmp = 32767;
    4325      141666 :         move16();
    4326      141666 :         IF( voicingBufAvg_fx > 0 )
    4327             :         {
    4328      141392 :             exp = norm_s( voicingBufAvg_fx );
    4329      141392 :             tmp = div_s( shl( 1, sub( 14, exp ) ), voicingBufAvg_fx ); /* (14-exp) */
    4330             : 
    4331             :             /* sd_uq_q*pitBufAvg */
    4332      141392 :             L_tmp = Mult_32_16( L_tmp, pitBufAvg_fx );                  /* Q13 * Q10 + 1 -16 => Q8 */
    4333      141392 :             L_tmp = Mult_32_16( L_tmp, tmp );                           /* Q8 + (14 - exp) - 15 => Q7 - exp */
    4334      141392 :             tmp = round_fx_sat( L_shl_sat( L_tmp, 31 - ( 7 - exp ) ) ); /* Q15 */
    4335             :         }
    4336      141666 :         tmp = s_min( tmp, 32767 /*1.0f Q15*/ );
    4337      141666 :         tmp = s_max( tmp, 19661 /*0.6f Q15*/ );
    4338      141666 :         GainFrame_fx = Mult_32_16( GainFrame_fx, tmp ); /* Q18 + Q15 + 1 - 16 : Q18 */
    4339             :     }
    4340             : 
    4341      146035 :     test();
    4342      146035 :     IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4343             :     {
    4344        4369 :         if ( EQ_16( st_fx->rf_mode, 1 ) )
    4345             :         {
    4346             :             /* Compensate for energy increase mismatch due to memory-less synthesis */
    4347             :             // GainFrame *= 0.85f;
    4348           0 :             GainFrame_fx = Mpy_32_16_1( GainFrame_fx, 27853 );
    4349             :         }
    4350             :     }
    4351             :     ELSE
    4352             :     {
    4353      141666 :         test();
    4354      141666 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) || EQ_16( st_fx->rf_mode, 1 ) )
    4355             :         {
    4356             :             /* Compensate for energy increase mismatch due to memory-less synthesis*/
    4357       75912 :             GainFrame_fx = Mult_32_16( GainFrame_fx, 27853 /*0.85f Q15*/ ); /* Q18 */
    4358             :         }
    4359             :     }
    4360             : 
    4361             :     /* Quantization of the frame gain parameter */
    4362      146035 :     IF( st_fx->rf_mode )
    4363             :     {
    4364           0 :         QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame_fx, st_fx->extl, 0, &hRF->RF_bwe_gainFr_ind, 0 );
    4365             :     }
    4366             :     ELSE
    4367             :     {
    4368      146035 :         QuantizeSHBframegain_ivas_fx( st_fx, &GainFrame_fx, st_fx->extl, 0, NULL, ( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) ) ? 1 : 0 );
    4369             :     }
    4370             : 
    4371      146035 :     IF( hStereoICBWE != NULL )
    4372             :     {
    4373       33208 :         push_indice( hBstr, IND_STEREO_ICBWE_MSFLAG, hStereoICBWE->MSFlag, STEREO_ICBWE_MSFLAG_BITS );
    4374             :     }
    4375             : 
    4376             :     /* Backup for the IC BWE parameters */
    4377      146035 :     test();
    4378      146035 :     IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && hStereoICBWE != NULL )
    4379             :     {
    4380             :         // Check Qs
    4381       33208 :         Copy_Scale_sig_16_32_DEPREC( lpc_shb_fx, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER + 1, 0 );
    4382       33208 :         hStereoICBWE->lpSHBRef_e = 31 - Q12;
    4383       33208 :         move16();
    4384             :     }
    4385             : 
    4386             :     /* Adjust the subframe and frame gain of the synthesized SHB signal */
    4387             :     /* Scale the shaped excitation */
    4388      146035 :     IF( EQ_32( st_fx->extl, FB_TBE ) )
    4389             :     {
    4390       61717 :         tmp = norm_l( GainFrame_fx );
    4391       61717 :         if ( GainFrame_fx == 0 )
    4392             :         {
    4393           0 :             tmp = 31;
    4394           0 :             move16();
    4395             :         }
    4396       61717 :         L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
    4397             : 
    4398       61717 :         tmp1 = 0;
    4399       61717 :         move16();
    4400    19811157 :         FOR( i = 0; i < L_FRAME16k; i++ )
    4401             :         {
    4402    19749440 :             L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[NUM_SHB_SUBFR * i / L_FRAME16k] ); /* Q : 18 + tmp +15 -15*/
    4403             :             /*White_exc16k_fx[i] = round_fx( L_shl(Mult_32_16( L_tmp1, White_exc16k_fx[i]),(23 - tmp -*Q_white_exc)) );*/
    4404             :             /*18 + tmp +*Q_white_exc -15 + 23 - tmp -*Q_white_exc -16  = 10*/
    4405    19749440 :             White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
    4406    19749440 :             move16();
    4407             :             /*Lmax = L_max(Lmax,White_exc16k_fx[i]);*/
    4408    19749440 :             tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
    4409             :             /*White_exc16k_fx[i] =32767;*/
    4410             :             /*White_exc16k_fx[i] = shr(White_exc16k_fx[i],5);*/
    4411             :         }
    4412       61717 :         *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
    4413       61717 :         move16();
    4414       61717 :         tmp = norm_s( tmp1 );
    4415       61717 :         if ( tmp1 == 0 )
    4416             :         {
    4417           0 :             tmp = 15;
    4418           0 :             move16();
    4419             :         }
    4420             : 
    4421    19811157 :         FOR( i = 0; i < L_FRAME16k; i++ )
    4422             :         {
    4423    19749440 :             White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
    4424    19749440 :             move16();
    4425             :         }
    4426       61717 :         *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
    4427       61717 :         move16();
    4428             :     }
    4429             : 
    4430      146035 :     hBWE_TD->prev_Env_error_fx = Env_error_fx;
    4431      146035 :     move16();
    4432             : 
    4433             :     /* *Q_white_exc = Q_bwe_exc_mod; move16(); output Qwhiteexc_FB from the GenShapedSHB function*/
    4434      146035 :     hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
    4435      146035 :     move16();
    4436             : 
    4437      146035 :     return;
    4438             : }
    4439             : 
    4440             : 
    4441             : /*==========================================================================*/
    4442             : /* FUNCTION : static void EstimateSHBFrameGain_fx() */
    4443             : /*--------------------------------------------------------------------------*/
    4444             : /* PURPOSE : Estimate the overall gain factor needed to scale */
    4445             : /* synthesized highband to original highband signal level. */
    4446             : /*--------------------------------------------------------------------------*/
    4447             : /* INPUT ARGUMENTS : */
    4448             : /* _(Word16) length : SHB overlap length Q0 */
    4449             : /* _(Word16*) oriSHB : target original SHB frame Q_oriSHB */
    4450             : /* _(Word16*) synSHB : shaped SHB excitation Q_synSHB */
    4451             : /* _(Word16*) win_shb : SHB window Q15 */
    4452             : /* _(Word16*) subwin_shb_local : SHB subframe window Q15 */
    4453             : /* _(Word16) Q_oriSHB : Q format of oriSHB */
    4454             : /* _(Word16) Q_synSHB : Q format of synSHB */
    4455             : /*--------------------------------------------------------------------------*/
    4456             : /* OUTPUT ARGUMENTS : */
    4457             : /* _(Word32*)GainFrame :estimat of gain frame Q18 */
    4458             : /*--------------------------------------------------------------------------*/
    4459             : /* INPUT/OUTPUT ARGUMENTS : */
    4460             : /* _None */
    4461             : /*--------------------------------------------------------------------------*/
    4462             : /* RETURN ARGUMENTS : */
    4463             : /* _ None */
    4464             : /*--------------------------------------------------------------------------*/
    4465        2639 : static void EstimateSHBFrameGain_fx(
    4466             :     const Word16 length,            /* i : SHB overlap length                                                  */
    4467             :     const Word16 *oriSHB,           /* i : target original SHB frame                               Q(Q_oriSHB) */
    4468             :     const Word16 Q_oriSHB,          /* i : Q of arget original SHB frame                                       */
    4469             :     const Word16 *synSHB,           /* i : shaped SHB excitation                                   Q(Q_synSHB) */
    4470             :     const Word16 Q_synSHB,          /* i : Q of shaped SHB excitation                                          */
    4471             :     Word16 *subgain,                /* i : estimate of gain shape                                          Q15 */
    4472             :     Word32 *GainFrame,              /* o : estimat of gain frame                                           Q18 */
    4473             :     const Word16 *win_shb,          /* i : SHB window                                                      Q15 */
    4474             :     const Word16 *subwin_shb_local, /* i : SHB subframe window                                             Q15 */
    4475             :     const Word16 n_subfr_saturation /* i : Number of subframes which saturated in GainShape oriNrg calculation */
    4476             : )
    4477             : {
    4478             :     const Word16 *skip;
    4479             :     Word16 i, j, k, l_shb_lahead, l_frame;
    4480             :     Word16 join_length, num_join, sig;
    4481             :     Word32 mod_syn[L_FRAME16k + L_SHB_LAHEAD];
    4482             :     Word32 oriNrg, synNrg;
    4483             :     Word16 sum_gain;
    4484             :     Word32 frame_gain;
    4485             :     Word32 L_tmp;
    4486             :     Word16 exp_out;
    4487             :     Word16 tmp;
    4488        2639 :     Word16 scaling = 0;
    4489        2639 :     move16();
    4490             : 
    4491             :     /* initilaization */
    4492        2639 :     l_frame = L_FRAME16k;
    4493        2639 :     move16();
    4494        2639 :     l_shb_lahead = L_SHB_LAHEAD;
    4495        2639 :     move16();
    4496        2639 :     skip = skip_bands_SWB_TBE;
    4497             : 
    4498        2639 :     IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
    4499             :     {
    4500           0 :         skip = skip_bands_WB_TBE;
    4501           0 :         l_frame = L_FRAME16k / 4;
    4502           0 :         move16();
    4503           0 :         l_shb_lahead = L_SHB_LAHEAD / 4;
    4504           0 :         move16();
    4505             :     }
    4506             : 
    4507             :     /* apply gain for each subframe, and store noise output signal using overlap-add*/
    4508        2639 :     set32_fx( mod_syn, 0, l_frame + l_shb_lahead );
    4509             : 
    4510        2639 :     IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
    4511             :     {
    4512           0 :         sum_gain = 0;
    4513           0 :         move16();
    4514           0 :         j = skip[0];
    4515           0 :         move16();
    4516           0 :         FOR( k = 0; k < length / 2; k++ )
    4517             :         {
    4518           0 :             sum_gain = mult_r( subwin_shb_local[2 * k + 2], subgain[0] ); /* Q15 */
    4519           0 :             mod_syn[j + k] = L_mult( synSHB[j + k], sum_gain );
    4520           0 :             move32(); /* Q(16+Q_synSHB) */
    4521           0 :             mod_syn[j + k + length / 2] = L_mult( synSHB[j + k + length / 2], subgain[0] );
    4522           0 :             move32(); /* Q(16+Q_synSHB) */
    4523             :         }
    4524             : 
    4525           0 :         FOR( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
    4526             :         {
    4527           0 :             j = skip[i];
    4528           0 :             move16();
    4529           0 :             FOR( k = 0; k < length; k++ )
    4530             :             {
    4531           0 :                 L_tmp = L_mult0( subwin_shb_local[k + 1], subgain[i] );                                   /* Q30 */
    4532           0 :                 sum_gain = round_fx( L_mac0( L_tmp, subwin_shb_local[length - k - 1], subgain[i - 1] ) ); /* Q14 */
    4533           0 :                 mod_syn[j + k] = L_shl( L_mult( sum_gain, synSHB[j + k] ), 1 );
    4534           0 :                 move32(); /* Q(16+Q_synSHB) */
    4535             :             }
    4536             :         }
    4537           0 :         FOR( k = 0; k < length / 2; k++ )
    4538             :         {
    4539           0 :             j = skip[i];
    4540           0 :             move16();
    4541           0 :             sum_gain = mult_r( subwin_shb_fx[length - 2 * k - 2], subgain[i - 1] ); /* Q15 */
    4542           0 :             mod_syn[j + k] = L_mult( synSHB[j + k], sum_gain );
    4543           0 :             move32(); /* Q(16+Q_synSHB) */
    4544             :         }
    4545             :     }
    4546             :     ELSE
    4547             :     {
    4548        2639 :         num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
    4549        2639 :         move16();
    4550        2639 :         join_length = i_mult2( num_join, length );
    4551        2639 :         j = 0;
    4552        2639 :         move16();
    4553       55419 :         FOR( k = 0; k < length; k++ )
    4554             :         {
    4555       52780 :             sum_gain = mult_r( subwin_shb_local[k + 1], subgain[0] ); /* Q15 */
    4556       52780 :             mod_syn[j] = L_mult( synSHB[j], sum_gain );
    4557       52780 :             move32(); /* Q(16+Q_synSHB) */
    4558       52780 :             j++;
    4559             :         }
    4560       10556 :         FOR( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
    4561             :         {
    4562      482937 :             FOR( k = 0; k < join_length - length; k++ )
    4563             :             {
    4564      475020 :                 mod_syn[j] = L_mult( synSHB[j], subgain[i * num_join] );
    4565      475020 :                 move32(); /* Q(16+Q_synSHB) */
    4566      475020 :                 j++;
    4567             :             }
    4568             : 
    4569      166257 :             FOR( k = 0; k < length; k++ )
    4570             :             {
    4571      158340 :                 L_tmp = L_mult0( subwin_shb_local[length - k - 1], subgain[i * num_join] );
    4572      158340 :                 tmp = round_fx( L_mac0( L_tmp, subwin_shb_local[k + 1], subgain[( i + 1 ) * num_join] ) ); /* Q14 */
    4573      158340 :                 mod_syn[j] = L_shl( L_mult( tmp, synSHB[j] ), 1 );
    4574      158340 :                 move32(); /* Q(16+Q_synSHB) */
    4575      158340 :                 j++;
    4576             :             }
    4577             :         }
    4578      160979 :         FOR( k = 0; k < join_length - length; k++ )
    4579             :         {
    4580      158340 :             mod_syn[j] = L_mult( synSHB[j], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] );
    4581      158340 :             move32(); /* Q(16+Q_synSHB)*/
    4582      158340 :             j++;
    4583             :         }
    4584       55419 :         FOR( k = 0; k < length; k++ )
    4585             :         {
    4586       52780 :             tmp = mult_r( subwin_shb_local[length - k - 1], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] ); /* Q15 */
    4587       52780 :             mod_syn[j] = L_mult( tmp, synSHB[j] );
    4588       52780 :             move32(); /* Q(16+Q_synSHB ) */
    4589       52780 :             j++;
    4590             :         }
    4591             :     }
    4592             :     /* adjust frame energy */
    4593        2639 :     oriNrg = L_deposit_l( 0 );
    4594        2639 :     synNrg = L_deposit_l( 0 );
    4595             : 
    4596       55419 :     FOR( i = 0; i < l_shb_lahead; i++ )
    4597             :     {
    4598       52780 :         sig = mult_r_sat( oriSHB[i], win_shb[i] );                  /* Q_oriSHB */
    4599       52780 :         oriNrg = L_mac0_sat( oriNrg, sig, sig );                    /* 2*Q_orisHB*/
    4600       52780 :         sig = round_fx_sat( Mult_32_16( mod_syn[i], win_shb[i] ) ); /*Q_synSHB */
    4601       52780 :         synNrg = L_mac0_sat( synNrg, sig, sig );                    /* 2*Q_synSHB */
    4602             :     }
    4603             : 
    4604      794339 :     FOR( ; i < l_frame; i++ )
    4605             :     {
    4606      791700 :         oriNrg = L_mac0_sat( oriNrg, oriSHB[i], oriSHB[i] ); /* 2*Q_oriSHB */
    4607      791700 :         sig = round_fx_sat( mod_syn[i] );                    /* Q_oriSHB */
    4608      791700 :         synNrg = L_mac0_sat( synNrg, sig, sig );             /* 2*Q_oriSHB */
    4609             :     }
    4610             : 
    4611        2639 :     tmp = add( l_frame, l_shb_lahead );
    4612       55419 :     FOR( ; i < tmp; i++ )
    4613             :     {
    4614       52780 :         sig = mult_r_sat( oriSHB[i], win_shb[l_frame + l_shb_lahead - 1 - i] );                  /* Q_oriSHB */
    4615       52780 :         oriNrg = L_mac0_sat( oriNrg, sig, sig );                                                 /* 2*Q_oriSHB */
    4616       52780 :         sig = round_fx_sat( Mult_32_16( mod_syn[i], win_shb[l_frame + l_shb_lahead - 1 - i] ) ); /* Q_oriSHB */
    4617       52780 :         synNrg = L_mac0_sat( synNrg, sig, sig );                                                 /* 2*Q_oriSHB */
    4618             :     }
    4619        2639 :     IF( EQ_32( oriNrg, MAX_32 ) )
    4620             :     {
    4621          14 :         scaling = negate( add( shr( n_subfr_saturation, 1 ), 1 ) );
    4622          14 :         oriNrg = 0;
    4623          14 :         move32();
    4624         294 :         FOR( i = 0; i < l_shb_lahead; i++ )
    4625             :         {
    4626         280 :             sig = mult_r( shl_sat( oriSHB[i], scaling ), win_shb[i] );
    4627         280 :             oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB + 2*scaling */
    4628             :         }
    4629        4214 :         FOR( ; i < l_frame; i++ )
    4630             :         {
    4631        4200 :             sig = shl_sat( oriSHB[i], scaling );
    4632        4200 :             oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB + 2*scaling */
    4633             :         }
    4634         294 :         FOR( ; i < tmp; i++ )
    4635             :         {
    4636         280 :             sig = mult_r( shl_sat( oriSHB[i], scaling ), win_shb[l_frame + l_shb_lahead - 1 - i] );
    4637         280 :             oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB + 2*scaling */
    4638             :         }
    4639             :     }
    4640        2639 :     L_tmp = root_a_over_b_fx( oriNrg, 2 * Q_oriSHB, synNrg, 2 * Q_synSHB, &exp_out );
    4641        2639 :     exp_out = sub( exp_out, scaling );
    4642        2639 :     frame_gain = L_shl_sat( L_tmp, sub( exp_out, 13 ) ); /* Q18 */
    4643        2639 :     *GainFrame = frame_gain;
    4644        2639 :     move32();
    4645             : 
    4646        2639 :     return;
    4647             : }
    4648             : 
    4649      153326 : static void EstimateSHBFrameGain_ivas_fx(
    4650             :     const Word16 length,            /* i : SHB overlap length                                                  */
    4651             :     const Word16 *oriSHB,           /* i : target original SHB frame                               Q(Q_oriSHB) */
    4652             :     const Word16 Q_oriSHB,          /* i : Q of arget original SHB frame                                       */
    4653             :     const Word16 *synSHB,           /* i : shaped SHB excitation                                   Q(Q_synSHB) */
    4654             :     const Word16 Q_synSHB,          /* i : Q of shaped SHB excitation                                          */
    4655             :     Word16 *subgain,                /* i : estimate of gain shape                                          Q15 */
    4656             :     Word32 *GainFrame,              /* o : estimat of gain frame                                           Q18 */
    4657             :     const Word16 *win_shb,          /* i : SHB window                                                      Q15 */
    4658             :     const Word16 *subwin_shb_local, /* i : SHB subframe window                                             Q15 */
    4659             :     const Word16 n_subfr_saturation /* i : Number of subframes which saturated in GainShape oriNrg calculation */
    4660             : )
    4661             : {
    4662             :     const Word16 *skip;
    4663             :     Word16 i, j, k, l_shb_lahead, l_frame;
    4664             :     Word16 join_length, num_join, sig;
    4665             :     Word32 mod_syn[L_FRAME16k + L_SHB_LAHEAD];
    4666             :     Word32 oriNrg, synNrg;
    4667             :     Word16 sum_gain;
    4668             :     Word32 frame_gain;
    4669             :     Word32 L_tmp;
    4670             :     Word16 exp_out;
    4671             :     Word16 tmp;
    4672      153326 :     Word16 scaling = 0;
    4673      153326 :     move16();
    4674             : 
    4675             :     /* initilaization */
    4676      153326 :     l_frame = L_FRAME16k;
    4677      153326 :     move16();
    4678      153326 :     l_shb_lahead = L_SHB_LAHEAD;
    4679      153326 :     move16();
    4680      153326 :     skip = skip_bands_SWB_TBE;
    4681             : 
    4682      153326 :     IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
    4683             :     {
    4684        7291 :         skip = skip_bands_WB_TBE;
    4685        7291 :         l_frame = L_FRAME16k / 4;
    4686        7291 :         move16();
    4687        7291 :         l_shb_lahead = L_SHB_LAHEAD / 4;
    4688        7291 :         move16();
    4689             :     }
    4690             : 
    4691             :     /* apply gain for each subframe, and store noise output signal using overlap-add*/
    4692      153326 :     set32_fx( mod_syn, 0, l_frame + l_shb_lahead );
    4693             : 
    4694      153326 :     IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
    4695             :     {
    4696        7291 :         sum_gain = 0;
    4697        7291 :         move16();
    4698        7291 :         j = skip[0];
    4699        7291 :         move16();
    4700       43746 :         FOR( k = 0; k < length / 2; k++ )
    4701             :         {
    4702       36455 :             sum_gain = mult_r( subwin_shb_local[2 * k + 2], subgain[0] ); /* Q15 */
    4703       36455 :             mod_syn[j + k] = L_mult0( synSHB[j + k], sum_gain );
    4704       36455 :             move32(); /* Q(16+Q_synSHB) */
    4705       36455 :             mod_syn[j + k + length / 2] = L_mult0( synSHB[j + k + length / 2], subgain[0] );
    4706       36455 :             move32(); /* Q(16+Q_synSHB) */
    4707             :         }
    4708             : 
    4709       58328 :         FOR( i = 1; i < NUM_SHB_SUBFR / 2; i++ )
    4710             :         {
    4711       51037 :             j = skip[i];
    4712       51037 :             move16();
    4713      561407 :             FOR( k = 0; k < length; k++ )
    4714             :             {
    4715      510370 :                 L_tmp = L_mult0( subwin_shb_local[k + 1], subgain[i] );                                   /* Q30 */
    4716      510370 :                 sum_gain = round_fx( L_mac0( L_tmp, subwin_shb_local[length - k - 1], subgain[i - 1] ) ); /* Q14 */
    4717      510370 :                 mod_syn[j + k] = L_mult( sum_gain, synSHB[j + k] );
    4718      510370 :                 move32(); /* Q(16+Q_synSHB) */
    4719             :             }
    4720             :         }
    4721       43746 :         FOR( k = 0; k < length / 2; k++ )
    4722             :         {
    4723       36455 :             j = skip[i];
    4724       36455 :             move16();
    4725       36455 :             sum_gain = mult_r( subwin_shb_fx[length - 2 * k - 2], subgain[i - 1] ); /* Q15 */
    4726       36455 :             mod_syn[j + k] = L_mult0( synSHB[j + k], sum_gain );
    4727       36455 :             move32(); /* Q(16+Q_synSHB) */
    4728             :         }
    4729             :     }
    4730             :     ELSE
    4731             :     {
    4732      146035 :         num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
    4733      146035 :         move16();
    4734      146035 :         join_length = i_mult2( num_join, length );
    4735      146035 :         j = 0;
    4736      146035 :         move16();
    4737     3066735 :         FOR( k = 0; k < length; k++ )
    4738             :         {
    4739     2920700 :             sum_gain = mult_r( subwin_shb_local[k + 1], subgain[0] ); /* Q15 */
    4740     2920700 :             mod_syn[j] = L_mult0( synSHB[j], sum_gain );
    4741     2920700 :             move32(); /* Q(16+Q_synSHB) */
    4742     2920700 :             j++;
    4743             :         }
    4744      584140 :         FOR( i = 0; i < NUM_SHB_SUBGAINS - 1; i++ )
    4745             :         {
    4746    26724405 :             FOR( k = 0; k < join_length - length; k++ )
    4747             :             {
    4748    26286300 :                 mod_syn[j] = L_mult0( synSHB[j], subgain[i * num_join] );
    4749    26286300 :                 move32(); /* Q(16+Q_synSHB) */
    4750    26286300 :                 j++;
    4751             :             }
    4752             : 
    4753     9200205 :             FOR( k = 0; k < length; k++ )
    4754             :             {
    4755     8762100 :                 L_tmp = L_mult0( subwin_shb_local[length - k - 1], subgain[i * num_join] );
    4756     8762100 :                 tmp = round_fx( L_mac0( L_tmp, subwin_shb_local[k + 1], subgain[( i + 1 ) * num_join] ) ); /* Q14 */
    4757     8762100 :                 mod_syn[j] = L_mult( tmp, synSHB[j] );
    4758     8762100 :                 move32(); /* Q(16+Q_synSHB) */
    4759     8762100 :                 j++;
    4760             :             }
    4761             :         }
    4762     8908135 :         FOR( k = 0; k < join_length - length; k++ )
    4763             :         {
    4764     8762100 :             mod_syn[j] = L_mult0( synSHB[j], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] );
    4765     8762100 :             move32(); /* Q(16+Q_synSHB)*/
    4766     8762100 :             j++;
    4767             :         }
    4768     3066735 :         FOR( k = 0; k < length; k++ )
    4769             :         {
    4770     2920700 :             tmp = mult_r( subwin_shb_local[length - k - 1], subgain[( NUM_SHB_SUBGAINS - 1 ) * num_join] ); /* Q15 */
    4771     2920700 :             mod_syn[j] = L_mult0( tmp, synSHB[j] );
    4772     2920700 :             move32(); /* Q(16+Q_synSHB ) */
    4773     2920700 :             j++;
    4774             :         }
    4775             :     }
    4776             :     /* adjust frame energy */
    4777      153326 :     oriNrg = L_deposit_l( 0 );
    4778      153326 :     synNrg = L_deposit_l( 0 );
    4779             : 
    4780     3110481 :     FOR( i = 0; i < l_shb_lahead; i++ )
    4781             :     {
    4782     2957155 :         sig = mult_r_sat( oriSHB[i], win_shb[i] );                  /* Q_oriSHB */
    4783     2957155 :         oriNrg = L_mac0_sat( oriNrg, sig, sig );                    /* 2*Q_orisHB*/
    4784     2957155 :         sig = round_fx_sat( Mult_32_16( mod_syn[i], win_shb[i] ) ); /*Q_synSHB */
    4785     2957155 :         synNrg = L_mac0_sat( synNrg, sig, sig );                    /* 2*Q_synSHB - 2*/
    4786             :     }
    4787             : 
    4788    44510651 :     FOR( ; i < l_frame; i++ )
    4789             :     {
    4790    44357325 :         oriNrg = L_mac0_sat( oriNrg, oriSHB[i], oriSHB[i] ); /* 2*Q_oriSHB */
    4791    44357325 :         sig = round_fx_sat( mod_syn[i] );                    /* Q_oriSHB */
    4792    44357325 :         synNrg = L_mac0_sat( synNrg, sig, sig );             /* 2*Q_oriSHB - 2*/
    4793             :     }
    4794             : 
    4795      153326 :     tmp = add( l_frame, l_shb_lahead );
    4796     3110481 :     FOR( ; i < tmp; i++ )
    4797             :     {
    4798     2957155 :         sig = mult_r_sat( oriSHB[i], win_shb[l_frame + l_shb_lahead - 1 - i] );                  /* Q_oriSHB */
    4799     2957155 :         oriNrg = L_mac0_sat( oriNrg, sig, sig );                                                 /* 2*Q_oriSHB */
    4800     2957155 :         sig = round_fx_sat( Mult_32_16( mod_syn[i], win_shb[l_frame + l_shb_lahead - 1 - i] ) ); /* Q_oriSHB */
    4801     2957155 :         synNrg = L_mac0_sat( synNrg, sig, sig );                                                 /* 2*Q_oriSHB - 2*/
    4802             :     }
    4803      153326 :     IF( EQ_32( oriNrg, MAX_32 ) )
    4804             :     {
    4805         542 :         scaling = negate( add( shr( n_subfr_saturation, 1 ), 1 ) );
    4806         542 :         oriNrg = 0;
    4807         542 :         move32();
    4808       11382 :         FOR( i = 0; i < l_shb_lahead; i++ )
    4809             :         {
    4810       10840 :             sig = mult_r( shl_sat( oriSHB[i], scaling ), win_shb[i] );
    4811       10840 :             oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB + 2*scaling */
    4812             :         }
    4813      163142 :         FOR( ; i < l_frame; i++ )
    4814             :         {
    4815      162600 :             sig = shl_sat( oriSHB[i], scaling );
    4816      162600 :             oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB + 2*scaling */
    4817             :         }
    4818       11382 :         FOR( ; i < tmp; i++ )
    4819             :         {
    4820       10840 :             sig = mult_r( shl_sat( oriSHB[i], scaling ), win_shb[l_frame + l_shb_lahead - 1 - i] );
    4821       10840 :             oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB + 2*scaling */
    4822             :         }
    4823             :     }
    4824      153326 :     L_tmp = root_a_over_b_fx( oriNrg, shl( Q_oriSHB, 1 ), synNrg, sub( shl( Q_synSHB, 1 ), 2 ), &exp_out );
    4825      153326 :     exp_out = sub( exp_out, scaling );
    4826      153326 :     frame_gain = L_shl_sat( L_tmp, sub( exp_out, 13 ) ); /* Q18 */
    4827      153326 :     *GainFrame = frame_gain;
    4828      153326 :     move32();
    4829             : 
    4830      153326 :     return;
    4831             : }
    4832             : 
    4833             : 
    4834        5278 : static Word32 pow_off_pk_fx( Word16 a[], Word16 len, Word16 step )
    4835             : {
    4836             :     Word16 i, j;
    4837             :     Word32 sum, L_tmp;
    4838             : 
    4839        5278 :     sum = L_shr( L_mult0( a[0], a[0] ), 1 ); /* Q29 */
    4840             : 
    4841        5278 :     j = 0;
    4842        5278 :     move16();
    4843       23751 :     FOR( i = 1; i < len; i += step )
    4844             :     {
    4845       18473 :         L_tmp = L_shr( L_mult0( a[i], a[i] ), 1 ); /* Q29 */
    4846       18473 :         sum = L_add( sum, L_tmp );                 /* Q29 */
    4847       18473 :         if ( GT_16( a[i], a[j] ) )
    4848             :         {
    4849        6120 :             j = i;
    4850        6120 :             move16();
    4851             :         }
    4852             :     }
    4853        5278 :     L_tmp = L_shr( L_mult0( a[j], a[j] ), 1 ); /* Q29 */
    4854        5278 :     sum = L_sub( sum, L_tmp );                 /* Q29 */
    4855             : 
    4856        5278 :     return ( sum );
    4857             : }
    4858             : 
    4859             : 
    4860      306652 : static Word32 pow_off_pk_corrected_fx( Word16 a[], Word16 len, Word16 step )
    4861             : {
    4862             :     Word16 i, j;
    4863             :     Word32 sum, L_tmp;
    4864             : 
    4865      306652 :     sum = L_shr( L_mult0( a[0], a[0] ), 1 ); /* Q29 */
    4866             : 
    4867      306652 :     j = 0;
    4868      306652 :     move16();
    4869     1226608 :     FOR( i = step; i < len; i += step )
    4870             :     {
    4871      919956 :         L_tmp = L_shr( L_mult0( a[i], a[i] ), 1 ); /* Q29 */
    4872      919956 :         sum = L_add( sum, L_tmp );                 /* Q29 */
    4873      919956 :         if ( GT_16( a[i], a[j] ) )
    4874             :         {
    4875      378936 :             j = i;
    4876      378936 :             move16();
    4877             :         }
    4878             :     }
    4879      306652 :     L_tmp = L_shr( L_mult0( a[j], a[j] ), 1 ); /* Q29 */
    4880      306652 :     sum = L_sub( sum, L_tmp );                 /* Q29 */
    4881             : 
    4882      306652 :     return ( sum );
    4883             : }
    4884             : 
    4885             : 
    4886             : /*==========================================================================*/
    4887             : /* FUNCTION : static void EstimateSHBGainShape_fx() */
    4888             : /*--------------------------------------------------------------------------*/
    4889             : /* PURPOSE : Estimate temporal gain parameters */
    4890             : /*--------------------------------------------------------------------------*/
    4891             : /* INPUT ARGUMENTS : */
    4892             : /* _(Word16) length : SHB overlap length Q0 */
    4893             : /* _(Word16*) oriSHB : target original SHB frame Q_oriSHB */
    4894             : /* _(Word16*) synSHB : shaped SHB excitation Q_synSHB */
    4895             : /* _(Word16*) subwin : SHB subframe window Q15 */
    4896             : /* _(Word16) Q_oriSHB : Q format of oriSHB */
    4897             : /* _(Word16) Q_synSHB : Q format of synSHB */
    4898             : /*--------------------------------------------------------------------------*/
    4899             : /* OUTPUT ARGUMENTS : */
    4900             : /* _(Word16*)subgain :estimate of gain shape Q15 */
    4901             : /*--------------------------------------------------------------------------*/
    4902             : /* INPUT/OUTPUT ARGUMENTS : */
    4903             : /* _None */
    4904             : /*--------------------------------------------------------------------------*/
    4905             : /* RETURN ARGUMENTS : */
    4906             : /* _ None */
    4907             : /*--------------------------------------------------------------------------*/
    4908             : 
    4909      155965 : static void EstimateSHBGainShape_fx(
    4910             :     const Word16 length,        /* i : SHB overlap length                                           */
    4911             :     const Word16 *oriSHB,       /* i : target original SHB frame                        Q(Q_oriSHB) */
    4912             :     const Word16 Q_oriSHB,      /* i : Q of target original SHB frame                               */
    4913             :     const Word16 *synSHB,       /* i : shaped SHB excitation                            Q(Q_synSHB) */
    4914             :     const Word16 Q_synSHB,      /* i : Q of shaped SHB excitation                                   */
    4915             :     Word16 *subgain,            /* o : estimate of gain shape                                   Q15 */
    4916             :     const Word16 *subwin,       /* i : SHB subframe window                                      Q15 */
    4917             :     Word16 *n_subfr_saturation, /* o : Number of subframes which saturated while calculating oriNrg */
    4918             :     const Flag limit_min_gain   /* i : gain shape limiting flag                                     */
    4919             : )
    4920             : {
    4921             :     const Word16 *skip;
    4922             :     Word16 i, j, k;
    4923             :     Word16 sig;
    4924             :     Word32 L_subgain[NUM_SHB_SUBFR];
    4925      155965 :     Word32 L_sum_gain = 0;
    4926             :     Word32 oriNrg, synNrg;
    4927             :     Word16 num_join, num_gains, join_length;
    4928             :     Word16 norm[NUM_SHB_SUBFR];
    4929      155965 :     Word16 n_max = -32768;
    4930             :     Word16 n;
    4931      155965 :     Word16 length2 = shl( length, 1 );
    4932             :     Word16 length_tmp;
    4933             :     Word32 L_tmp, normFact;
    4934      155965 :     Word16 scaling = 0;
    4935      155965 :     move32();
    4936      155965 :     move16();
    4937      155965 :     move16();
    4938             : 
    4939      155965 :     num_join = NUM_SHB_SUBFR / NUM_SHB_SUBGAINS;
    4940      155965 :     move16();
    4941      155965 :     num_gains = NUM_SHB_SUBGAINS;
    4942      155965 :     move16();
    4943      155965 :     skip = skip_bands_SWB_TBE;
    4944             : 
    4945      155965 :     IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
    4946             :     {
    4947        7291 :         num_gains = NUM_SHB_SUBFR / 4;
    4948        7291 :         move16();
    4949        7291 :         skip = skip_bands_WB_TBE;
    4950             :     }
    4951             :     /* calculate and normalize the subgain */
    4952      155965 :     oriNrg = 0;
    4953      155965 :     move32();
    4954      155965 :     synNrg = 0;
    4955      155965 :     move32();
    4956             : 
    4957      155965 :     IF( EQ_16( length, SHB_OVERLAP_LEN / 2 ) )
    4958             :     {
    4959       65619 :         FOR( i = 0; i < NUM_SHB_SUBFR / 2; i++ )
    4960             :         {
    4961       58328 :             logic16();
    4962       58328 :             IF( s_and( i, 0x1 ) == 0 )
    4963             :             {
    4964       29164 :                 oriNrg = 0;
    4965       29164 :                 move32();
    4966       29164 :                 synNrg = 0;
    4967       29164 :                 move32();
    4968             :             }
    4969       58328 :             j = skip[i];
    4970       58328 :             move16();
    4971       58328 :             IF( i == 0 )
    4972             :             {
    4973       43746 :                 FOR( k = 0; k < length / 2; k++ )
    4974             :                 {
    4975       36455 :                     sig = mult_r( oriSHB[j + k], subwin[2 * k + 2] ); /* Q_oriSHB */
    4976       36455 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig );          /* 2* Q_oriSHB */
    4977       36455 :                     sig = mult_r( synSHB[j + k], subwin[2 * k + 2] ); /* Q_synSHB */
    4978       36455 :                     synNrg = L_mac0_sat( synNrg, sig, sig );          /* 2* Q_synSHB */
    4979             :                 }
    4980       43746 :                 FOR( k = length / 2; k < length; k++ )
    4981             :                 {
    4982       36455 :                     sig = oriSHB[j + k];
    4983       36455 :                     move16();
    4984       36455 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2* Q_oriSHB */
    4985       36455 :                     sig = synSHB[j + k];
    4986       36455 :                     move16();
    4987       36455 :                     synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2* Q_synSHB */
    4988             :                 }
    4989             :             }
    4990             :             ELSE
    4991             :             {
    4992      561407 :                 FOR( k = 0; k < length; k++ )
    4993             :                 {
    4994      510370 :                     sig = mult_r( oriSHB[j + k], subwin[k + 1] ); /* Q_oriSHB */
    4995      510370 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig );      /* 2* Q_oriSHB */
    4996      510370 :                     sig = mult_r( synSHB[j + k], subwin[k + 1] ); /* Q_synSHB */
    4997      510370 :                     synNrg = L_mac0_sat( synNrg, sig, sig );      /* 2* Q_synSHB */
    4998             :                 }
    4999             :             }
    5000       58328 :             IF( EQ_16( i, NUM_SHB_SUBFR / 2 - 1 ) )
    5001             :             {
    5002        7291 :                 length_tmp = sub( length2, shr( length, 1 ) );
    5003       43746 :                 FOR( ; k < length_tmp; k++ )
    5004             :                 {
    5005       36455 :                     sig = mult_r( oriSHB[j + k], subwin[3 * length - 2 * k - 2] ); /* Q_oriSHB */
    5006       36455 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig );                       /* 2* Q_oriSHB */
    5007             : 
    5008       36455 :                     sig = mult_r( synSHB[j + k], subwin[3 * length - 2 * k - 2] ); /* Q_synSHB */
    5009       36455 :                     synNrg = L_mac0_sat( synNrg, sig, sig );                       /* 2* Q_synSHB */
    5010             :                 }
    5011             :             }
    5012             :             ELSE
    5013             :             {
    5014      561407 :                 FOR( ; k < length2; k++ )
    5015             :                 {
    5016      510370 :                     sig = mult_r( oriSHB[j + k], subwin[2 * length - k - 1] ); /* Q_oriSHB */
    5017      510370 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig );                   /* 2* Q_oriSHB */
    5018      510370 :                     sig = mult_r( synSHB[j + k], subwin[2 * length - k - 1] ); /* Q_synSHB */
    5019      510370 :                     synNrg = L_mac0_sat( synNrg, sig, sig );                   /* 2* Q_synSHB */
    5020             :                 }
    5021             :             }
    5022             : 
    5023       58328 :             logic16();
    5024       58328 :             IF( EQ_16( s_and( i, 0x1 ), 1 ) )
    5025             :             {
    5026       29164 :                 L_subgain[i / 2] = root_a_over_b_fx( oriNrg, shl( Q_oriSHB, 1 ), synNrg, shl( Q_synSHB, 1 ), &n );
    5027       29164 :                 move16(); /* Q(31-n) */
    5028       29164 :                 norm[i / 2] = n;
    5029       29164 :                 move16();
    5030       29164 :                 if ( GT_16( norm[i / 2], n_max ) )
    5031             :                 {
    5032       10641 :                     n_max = norm[i / 2];
    5033       10641 :                     move16();
    5034             :                 }
    5035             :             }
    5036             :         }
    5037             :     }
    5038             :     ELSE
    5039             :     {
    5040      148674 :         join_length = i_mult2( num_join, length );
    5041      743370 :         FOR( i = 0; i < num_gains; i++ )
    5042             :         {
    5043      594696 :             oriNrg = 0;
    5044      594696 :             move32();
    5045      594696 :             synNrg = 0;
    5046      594696 :             move32();
    5047             : 
    5048      594696 :             j = i_mult2( join_length, i );
    5049    12488616 :             FOR( k = 0; k < length; k++ )
    5050             :             {
    5051    11893920 :                 sig = mult_r( oriSHB[j + k], subwin[k + 1] ); /* Q_oriSHB */
    5052    11893920 :                 oriNrg = L_mac0_sat( oriNrg, sig, sig );      /* 2* Q_oriSHB */
    5053    11893920 :                 sig = mult_r( synSHB[j + k], subwin[k + 1] ); /* Q_oriSHB */
    5054    11893920 :                 synNrg = L_mac0_sat( synNrg, sig, sig );      /* 2* Q_oriSHB */
    5055             :             }
    5056             : 
    5057    36276456 :             FOR( k = 0; k < ( join_length - length ); k++ )
    5058             :             {
    5059    35681760 :                 sig = mult_r( oriSHB[length + j + k], 32767 ); /* Q_oriSHB */
    5060    35681760 :                 oriNrg = L_mac0_sat( oriNrg, sig, sig );       /* 2* Q_oriSHB */
    5061    35681760 :                 sig = mult_r( synSHB[length + j + k], 32767 ); /* Q_oriSHB */
    5062    35681760 :                 synNrg = L_mac0_sat( synNrg, sig, sig );       /* 2* Q_oriSHB */
    5063             :             }
    5064             : 
    5065    12488616 :             FOR( k = 0; k < length; k++ )
    5066             :             {
    5067    11893920 :                 sig = mult_r( oriSHB[j + join_length + k], subwin[length - k - 1] ); /* Q_oriSHB */
    5068    11893920 :                 oriNrg = L_mac0_sat( oriNrg, sig, sig );                             /* 2* Q_oriSHB */
    5069    11893920 :                 sig = mult_r( synSHB[j + join_length + k], subwin[length - k - 1] ); /* Q_oriSHB */
    5070    11893920 :                 synNrg = L_mac0_sat( synNrg, sig, sig );                             /* 2* Q_oriSHB */
    5071             :             }
    5072             :             /* Only implemented in SWB because the length of samples in SWB frame is longer, more likely to saturate */
    5073      594696 :             scaling = 0;
    5074      594696 :             move16();
    5075      594696 :             IF( EQ_32( oriNrg, MAX_32 ) )
    5076             :             {
    5077         213 :                 IF( n_subfr_saturation != NULL )
    5078             :                 {
    5079         213 :                     *n_subfr_saturation = add( *n_subfr_saturation, 1 );
    5080         213 :                     move16();
    5081             :                 }
    5082         213 :                 scaling = -1;
    5083         213 :                 move16();
    5084         213 :                 oriNrg = 0;
    5085         213 :                 move32();
    5086        4473 :                 FOR( k = 0; k < length; k++ )
    5087             :                 {
    5088        4260 :                     sig = mult_r( oriSHB[j + k], subwin[k + 1] );
    5089        4260 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB - 2*/
    5090             :                 }
    5091       12993 :                 FOR( k = 0; k < ( join_length - length ); k++ )
    5092             :                 {
    5093       12780 :                     sig = shr( oriSHB[length + j + k], 1 );
    5094       12780 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig ); /* 2*Q_oriSHB - 2*/
    5095             :                 }
    5096        4473 :                 FOR( k = 0; k < length; k++ )
    5097             :                 {
    5098        4260 :                     sig = mult_r( shr( oriSHB[j + join_length + k], 1 ), subwin[length - k - 1] ); /* Q_oriSHB - 1(scaling) */
    5099        4260 :                     oriNrg = L_mac0_sat( oriNrg, sig, sig );                                       /* 2*Q_oriSHB - 2 */
    5100             :                 }
    5101             :             }
    5102      594696 :             L_subgain[i] = root_a_over_b_fx( oriNrg, shl( Q_oriSHB, 1 ), synNrg, shl( Q_synSHB, 1 ), &n );
    5103      594696 :             move32(); /* Q(31-n) */
    5104      594696 :             n = sub( n, scaling );
    5105      594696 :             norm[i] = n;
    5106      594696 :             move16();
    5107      594696 :             if ( GT_16( norm[i], n_max ) )
    5108             :             {
    5109      224111 :                 n_max = norm[i];
    5110      224111 :                 move16();
    5111             :             }
    5112             :         }
    5113             :     }
    5114             : 
    5115      779825 :     FOR( i = 0; i < num_gains; i++ )
    5116             :     {
    5117      623860 :         subgain[i] = round_fx_sat( L_shl_sat( L_subgain[i], sub( norm[i], n_max + 1 ) ) ); /* Q(14-n_max) */
    5118      623860 :         L_sum_gain = L_mac0_sat( L_sum_gain, subgain[i], subgain[i] );                     /* Q(28-2*n_max) */
    5119      623860 :         move16();
    5120             :     }
    5121             : 
    5122             :     /* normalize the subgain */
    5123      155965 :     n = norm_l( L_sum_gain );
    5124      155965 :     L_sum_gain = L_shl( L_sum_gain, n );
    5125      155965 :     n = sub( 31, add( n, ( sub( 28, shl( n_max, 1 ) ) ) ) );
    5126      155965 :     normFact = Isqrt_lc( L_sum_gain, &n );
    5127             : 
    5128      155965 :     IF( limit_min_gain )
    5129             :     {
    5130       49650 :         FOR( i = 0; i < num_gains; i++ )
    5131             :         {
    5132       39720 :             L_tmp = Mpy_32_16_1( normFact, subgain[i] ); /*Q(31-n) * Q(31-norm[i])      */                    /* Q(30-n-n_max) */
    5133       39720 :             subgain[i] = s_max( round_fx_sat( L_shl_sat( L_tmp, add( n, n_max + 1 ) ) ), 3277 /*0.1f Q15*/ ); /* Q15 */
    5134       39720 :             move16();
    5135             :         }
    5136             :     }
    5137             :     ELSE
    5138             :     {
    5139      730175 :         FOR( i = 0; i < num_gains; i++ )
    5140             :         {
    5141      584140 :             L_tmp = Mpy_32_16_1( normFact, subgain[i] ); /*Q(31-n) * Q(31-norm[i])      */ /* Q(30-n-n_max) */
    5142      584140 :             subgain[i] = round_fx_sat( L_shl_sat( L_tmp, add( n, n_max + 1 ) ) );          /* Q15 */
    5143      584140 :             move16();
    5144             :         }
    5145             :     }
    5146             : 
    5147      155965 :     return;
    5148             : }
    5149             : 
    5150             : 
    5151             : /*==========================================================================*/
    5152             : /* FUNCTION :static short closest_centroid_fx () */
    5153             : /*--------------------------------------------------------------------------*/
    5154             : /* PURPOSE : Determine a set of closest VQ centroids for a given input*/
    5155             : /*--------------------------------------------------------------------------*/
    5156             : /* INPUT ARGUMENTS : */
    5157             : /* _(Word16*) data : input data Q15 */
    5158             : /* _(Word16*) weights : weights Q0 */
    5159             : /* _(Word16*) quantizer : quantizer table Q15 */
    5160             : /* _(Word16) centroids : number of centroids Q0 */
    5161             : /* _(Word16) length : dimension of quantiser Q0 */
    5162             : /*--------------------------------------------------------------------------*/
    5163             : /* OUTPUT ARGUMENTS : */
    5164             : /* _None */
    5165             : /*--------------------------------------------------------------------------*/
    5166             : /* INPUT/OUTPUT ARGUMENTS : */
    5167             : /* _None */
    5168             : /*--------------------------------------------------------------------------*/
    5169             : /* RETURN ARGUMENTS : */
    5170             : /* _ Word16 : index Q0 */
    5171             : /*--------------------------------------------------------------------------*/
    5172       44881 : static Word16 closest_centroid_fx(
    5173             :     const Word16 *data,      /* i : input data Qx*/
    5174             :     const Word16 *weights,   /* i : weights */
    5175             :     const Word16 *quantizer, /* i : quantizer table Qx*/
    5176             :     const Word16 centroids,  /* i : number of centroids */
    5177             :     const Word16 length )    /* i : dimension of quantiser */
    5178             : {
    5179             :     Word16 i, j, index;
    5180             :     Word16 tmp, tmpL;
    5181             :     Word64 werr_64;
    5182             :     Word32 L_tmp, werr, best_werr;
    5183             : 
    5184       44881 :     index = 0;
    5185       44881 :     move16();
    5186       44881 :     best_werr = MAX_32;
    5187       44881 :     move32();
    5188             : 
    5189    10919033 :     FOR( i = 0; i < centroids; i++ )
    5190             :     {
    5191    10874152 :         werr_64 = 0;
    5192    10874152 :         move64();
    5193    10874152 :         tmpL = i_mult2( i, length );
    5194   114591688 :         FOR( j = 0; j < length; j++ )
    5195             :         {
    5196   103717536 :             tmp = sub( data[j], quantizer[tmpL + j] );
    5197   103717536 :             L_tmp = L_mult( tmp, tmp );
    5198   103717536 :             werr_64 = W_mac_32_16( werr_64, L_tmp, weights[j] );
    5199             :         }
    5200    10874152 :         werr = W_sat_m( werr_64 );
    5201    10874152 :         if ( LT_32( werr, best_werr ) )
    5202             :         {
    5203      293443 :             index = i;
    5204      293443 :             move16();
    5205             :         }
    5206    10874152 :         best_werr = L_min( best_werr, werr );
    5207             :     }
    5208       44881 :     return index;
    5209             : }
    5210             : 
    5211             : /*==========================================================================*/
    5212             : /* FUNCTION :static short closest_centroid_lc_fx () */
    5213             : /*--------------------------------------------------------------------------*/
    5214             : /* PURPOSE : Determine a set of closest VQ centroids for a given input      */
    5215             : /*           Gain shape is 4 dimensional                                    */
    5216             : /*--------------------------------------------------------------------------*/
    5217        4849 : static Word16 closest_centroid_lc_fx(
    5218             :     const Word16 *data,      /* i : input data Qx*/
    5219             :     const Word16 *quantizer, /* i : quantizer table Qx*/
    5220             :     const Word16 centroids ) /* i : number of centroids */
    5221             : {
    5222             :     Word16 i, index, tmp, tmpL;
    5223             :     Word32 werr, best_werr;
    5224             : 
    5225        4849 :     index = 0;
    5226        4849 :     move16();
    5227        4849 :     best_werr = MAX_32;
    5228        4849 :     move32();
    5229             : 
    5230      160017 :     FOR( i = 0; i < centroids; i++ )
    5231             :     {
    5232             :         /* Gain shape dimension 4 */
    5233      155168 :         tmpL = shl( i, 2 );
    5234             : 
    5235             :         /* index 0 */
    5236      155168 :         tmp = sub( data[0], quantizer[tmpL] );
    5237      155168 :         werr = L_mult( tmp, tmp );
    5238             : 
    5239             :         /* index 1 */
    5240      155168 :         tmp = sub( data[1], quantizer[tmpL + 1] );
    5241      155168 :         werr = L_mac( werr, tmp, tmp );
    5242             : 
    5243             :         /* index 2 */
    5244      155168 :         tmp = sub( data[2], quantizer[tmpL + 2] );
    5245      155168 :         werr = L_mac( werr, tmp, tmp );
    5246             : 
    5247             :         /* index 3 */
    5248      155168 :         tmp = sub( data[3], quantizer[tmpL + 3] );
    5249      155168 :         werr = L_mac( werr, tmp, tmp );
    5250             : 
    5251      155168 :         if ( LT_32( werr, best_werr ) )
    5252             :         {
    5253       23719 :             index = i;
    5254       23719 :             move16();
    5255             :         }
    5256      155168 :         best_werr = L_min( best_werr, werr );
    5257             :     }
    5258             : 
    5259        4849 :     return index;
    5260             : }
    5261             : 
    5262             : /*============================================================*/
    5263             : /* FUNCTION : static void QuantizeSHBsubgains_fx() */
    5264             : /*------------------------------------------------------------*/
    5265             : /* PURPOSE : Quantize super highband temporal gains */
    5266             : /*------------------------------------------------------------*/
    5267             : /* INPUT ARGUMENTS : */
    5268             : /* _(Word16) extl : extension layer Q0 */
    5269             : /*------------------------------------------------------------*/
    5270             : /* OUTPUT ARGUMENTS : */
    5271             : /* _ None */
    5272             : /*------------------------------------------------------------*/
    5273             : /* INPUT/OUTPUT ARGUMENTS : */
    5274             : /* _(Word16[])subgains :super highband temporal gains Q15 */
    5275             : /*------------------------------------------------------------*/
    5276             : /* RETURN ARGUMENTS : */
    5277             : /* _ None */
    5278             : /*------------------------------------------------------------*/
    5279             : 
    5280        2639 : static void QuantizeSHBsubgains_fx(
    5281             :     Encoder_State *st_fx, /* i/o: encoder state structure */
    5282             :     Word16 subgains[],    /* i/o: super highband temporal gains Q15*/
    5283             :     const Word16 extl     /* i : extension layer */
    5284             : )
    5285             : {
    5286             :     Word16 i, idxSubGain;
    5287             :     Word16 Unit_weights10[NUM_SHB_SUBFR];
    5288             :     Word16 exp, frac;
    5289             :     Word32 L_tmp;
    5290        2639 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5291        2639 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    5292             : 
    5293        2639 :     IF( EQ_16( extl, WB_TBE ) )
    5294             :     {
    5295           0 :         set16_fx( Unit_weights10, 32767, (Word16) NUM_SHB_SUBFR / 4 );
    5296           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5297             :         {
    5298           0 :             IF( EQ_16( subgains[i], 0 ) )
    5299             :             {
    5300           0 :                 subgains[i + NUM_SHB_SUBFR / 4] = -18432;
    5301           0 :                 move16(); /* (-72) in Q8 */
    5302             :             }
    5303             :             ELSE
    5304             :             {
    5305           0 :                 L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
    5306           0 :                 exp = norm_l( L_tmp );
    5307           0 :                 frac = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* move16(); */
    5308             :                 /*exp = -1 - exp;  */
    5309           0 :                 exp = sub( -1, exp );
    5310           0 :                 L_tmp = Mpy_32_16( exp, frac, 24660 );                            /* Q13 ; 20.0 * log10(2) in Q12*/
    5311           0 :                 subgains[i + NUM_SHB_SUBFR / 4] = round_fx( L_shl( L_tmp, 11 ) ); /* Q8 */
    5312           0 :                 move16();
    5313             :             }
    5314             :         }
    5315           0 :         idxSubGain = closest_centroid_lc_fx( subgains + NUM_SHB_SUBFR / 4, HBCB_SubGain5bit_fx, 1 << NUM_BITS_SHB_SUBGAINS );
    5316           0 :         Copy( HBCB_SubGain5bit_fx + idxSubGain * NUM_SHB_SUBFR / 4, subgains, NUM_SHB_SUBFR / 4 );
    5317             : 
    5318           0 :         push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
    5319             : 
    5320           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5321             :         {
    5322           0 :             L_tmp = L_mult( subgains[i], 21771 ); /* *0.166096 in Q17 -> Q26 */
    5323           0 :             L_tmp = L_shr( L_tmp, 10 );           /* From Q26 to Q16 */
    5324           0 :             frac = L_Extract_lc( L_tmp, &exp );
    5325           0 :             subgains[i] = extract_l( Pow2( 14, frac ) );
    5326           0 :             move16();
    5327             :             /* Put 14 as exponent so that */
    5328             :             /* output of Pow2() will be: */
    5329             :             /* 16384 < Pow2() <= 32767 */
    5330           0 :             subgains[i] = shl( subgains[i], add( exp, 1 ) );
    5331           0 :             move16(); /*Q15 */
    5332             :         }
    5333             : 
    5334           0 :         FOR( i = NUM_SHB_SUBFR / 2 - 1; i >= 0; i-- )
    5335             :         {
    5336           0 :             subgains[i] = subgains[i / 2];
    5337           0 :             move16();
    5338             :         }
    5339             :     }
    5340             :     ELSE
    5341             :     {
    5342       13195 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    5343             :         {
    5344       10556 :             IF( EQ_16( subgains[i], 0 ) )
    5345             :             {
    5346           0 :                 subgains[i] = -12288;
    5347           0 :                 move16(); /* (-3) in Q12 */
    5348             :             }
    5349             :             ELSE
    5350             :             {
    5351       10556 :                 L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
    5352       10556 :                 exp = norm_l( L_tmp );
    5353       10556 :                 frac = Log2_norm_lc( L_shl( L_tmp, exp ) );
    5354             :                 /*exp = -1 - exp;  */
    5355       10556 :                 exp = sub( -1, exp );
    5356             : 
    5357       10556 :                 L_tmp = Mpy_32_16( exp, frac, 9864 );         /*move32(); // Q16 ; log10(2) in Q15 */
    5358       10556 :                 subgains[i] = round_fx( L_shl( L_tmp, 12 ) ); /*Q12 */
    5359       10556 :                 move16();
    5360             :             }
    5361             :         }
    5362             : 
    5363        2639 :         idxSubGain = (Word16) vquant_fx( subgains, 0, subgains, SHBCB_SubGain5bit_12_fx, NUM_SHB_SUBGAINS, 1 << NUM_BITS_SHB_SUBGAINS );
    5364             : 
    5365       13195 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    5366             :         {
    5367       10556 :             L_tmp = L_mult( subgains[i], 27213 ); /* *3.321928 in Q13 -> Q26 */
    5368       10556 :             L_tmp = L_shr( L_tmp, 10 );           /* From Q26 to Q16 */
    5369       10556 :             frac = L_Extract_lc( L_tmp, &exp );
    5370       10556 :             subgains[i] = extract_l( Pow2( 14, frac ) );
    5371       10556 :             move16();
    5372             :             /* Put 14 as exponent so that */
    5373             :             /* output of Pow2() will be: */
    5374             :             /* 16384 < Pow2() <= 32767 */
    5375       10556 :             subgains[i] = shl( subgains[i], add( exp, 1 ) );
    5376       10556 :             move16(); /*Q15 */
    5377             :         }
    5378             : 
    5379       44863 :         FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    5380             :         {
    5381       42224 :             subgains[i] = subgains[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    5382       42224 :             move16();
    5383             :         }
    5384             : 
    5385        2639 :         hBWE_TD->idxSubGains = idxSubGain;
    5386        2639 :         move16();
    5387        2639 :         IF( NE_16( st_fx->codec_mode, MODE2 ) )
    5388             :         {
    5389        1415 :             push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
    5390             :         }
    5391             :     }
    5392             : 
    5393        2639 :     return;
    5394             : }
    5395             : 
    5396             : 
    5397             : /*============================================================*/
    5398             : /* FUNCTION : static void QuantizeSHBsubgains_ivas_fx() */
    5399             : /*------------------------------------------------------------*/
    5400             : /* PURPOSE : Quantize super highband temporal gains */
    5401             : /*------------------------------------------------------------*/
    5402             : /* INPUT ARGUMENTS : */
    5403             : /* _(Word16) extl : extension layer Q0 */
    5404             : /*------------------------------------------------------------*/
    5405             : /* OUTPUT ARGUMENTS : */
    5406             : /* _ None */
    5407             : /*------------------------------------------------------------*/
    5408             : /* INPUT/OUTPUT ARGUMENTS : */
    5409             : /* _(Word16[])subgains :super highband temporal gains Q15 */
    5410             : /*------------------------------------------------------------*/
    5411             : /* RETURN ARGUMENTS : */
    5412             : /* _ None */
    5413             : /*------------------------------------------------------------*/
    5414             : 
    5415      150884 : static void QuantizeSHBsubgains_ivas_fx(
    5416             :     Encoder_State *st_fx, /* i/o: encoder state structure           */
    5417             :     Word16 subgains[],    /* i/o: super highband temporal gains Q15 */
    5418             :     const Word16 extl     /* i : extension layer                    */
    5419             : )
    5420             : {
    5421             :     Word16 i, idxSubGain;
    5422             :     Word16 Unit_weights10[NUM_SHB_SUBFR];
    5423             :     Word16 exp, frac;
    5424             :     Word32 L_tmp;
    5425      150884 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5426      150884 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    5427             : 
    5428      150884 :     IF( EQ_16( extl, WB_TBE ) )
    5429             :     {
    5430        4849 :         set16_fx( Unit_weights10, 32767, NUM_SHB_SUBFR / 4 );
    5431       24245 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5432             :         {
    5433       19396 :             IF( EQ_16( subgains[i], 0 ) )
    5434             :             {
    5435           0 :                 subgains[i + NUM_SHB_SUBFR / 4] = -18432;
    5436           0 :                 move16(); /* (-72) in Q8 */
    5437             :             }
    5438             :             ELSE
    5439             :             {
    5440       19396 :                 L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
    5441       19396 :                 exp = norm_l( L_tmp );
    5442       19396 :                 frac = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* move16(); */
    5443             :                 /*exp = -1 - exp;  */
    5444       19396 :                 exp = sub( -1, exp );
    5445       19396 :                 L_tmp = Mpy_32_16( exp, frac, 24660 );                            /* Q13 ; 20.0 * log10(2) in Q12*/
    5446       19396 :                 subgains[i + NUM_SHB_SUBFR / 4] = round_fx( L_shl( L_tmp, 11 ) ); /* Q8 */
    5447       19396 :                 move16();
    5448             :             }
    5449             :         }
    5450        4849 :         idxSubGain = closest_centroid_lc_fx( subgains + NUM_SHB_SUBFR / 4, HBCB_SubGain5bit_fx, 1 << NUM_BITS_SHB_SUBGAINS );
    5451        4849 :         Copy( HBCB_SubGain5bit_fx + idxSubGain * NUM_SHB_SUBFR / 4, subgains, NUM_SHB_SUBFR / 4 );
    5452             : 
    5453        4849 :         push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
    5454             : 
    5455       24245 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5456             :         {
    5457       19396 :             L_tmp = L_mult( subgains[i], 21771 ); /* *0.166096 in Q17 -> Q26 */
    5458       19396 :             L_tmp = L_shr( L_tmp, 10 );           /* From Q26 to Q16 */
    5459       19396 :             frac = L_Extract_lc( L_tmp, &exp );
    5460       19396 :             subgains[i] = extract_l( Pow2( 14, frac ) );
    5461       19396 :             move16();
    5462             :             /* Put 14 as exponent so that */
    5463             :             /* output of Pow2() will be: */
    5464             :             /* 16384 < Pow2() <= 32767 */
    5465       19396 :             subgains[i] = shl( subgains[i], add( exp, 1 ) );
    5466       19396 :             move16(); /*Q15 */
    5467             :         }
    5468             : 
    5469       43641 :         FOR( i = NUM_SHB_SUBFR / 2 - 1; i >= 0; i-- )
    5470             :         {
    5471       38792 :             subgains[i] = subgains[i / 2];
    5472       38792 :             move16();
    5473             :         }
    5474             :     }
    5475             :     ELSE
    5476             :     {
    5477      730175 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    5478             :         {
    5479      584140 :             IF( EQ_16( subgains[i], 0 ) )
    5480             :             {
    5481        9418 :                 subgains[i] = -12288;
    5482        9418 :                 move16(); /* (-3) in Q12 */
    5483             :             }
    5484             :             ELSE
    5485             :             {
    5486      574722 :                 L_tmp = L_deposit_h( subgains[i] ); /* Q31 */
    5487      574722 :                 exp = norm_l( L_tmp );
    5488      574722 :                 frac = Log2_norm_lc( L_shl( L_tmp, exp ) );
    5489             :                 /*exp = -1 - exp;  */
    5490      574722 :                 exp = sub( -1, exp );
    5491             : 
    5492      574722 :                 L_tmp = Mpy_32_16( exp, frac, 9864 );         /*move32(); // Q16 ; log10(2) in Q15 */
    5493      574722 :                 subgains[i] = round_fx( L_shl( L_tmp, 12 ) ); /*Q12 */
    5494      574722 :                 move16();
    5495             :             }
    5496             :         }
    5497             : 
    5498      146035 :         idxSubGain = vquant_fx( subgains, 0, subgains, SHBCB_SubGain5bit_12_fx, NUM_SHB_SUBGAINS, 1 << NUM_BITS_SHB_SUBGAINS );
    5499             : 
    5500      730175 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    5501             :         {
    5502      584140 :             L_tmp = L_mult( subgains[i], 27213 ); /* *3.321928 in Q13 -> Q26 */
    5503      584140 :             L_tmp = L_shr( L_tmp, 10 );           /* From Q26 to Q16 */
    5504      584140 :             frac = L_Extract_lc( L_tmp, &exp );
    5505      584140 :             subgains[i] = extract_l( Pow2( 14, frac ) );
    5506      584140 :             move16();
    5507             :             /* Put 14 as exponent so that */
    5508             :             /* output of Pow2() will be: */
    5509             :             /* 16384 < Pow2() <= 32767 */
    5510      584140 :             subgains[i] = shl( subgains[i], add( exp, 1 ) );
    5511      584140 :             move16(); /*Q15 */
    5512             :         }
    5513             : 
    5514     2482595 :         FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    5515             :         {
    5516     2336560 :             subgains[i] = subgains[i * NUM_SHB_SUBGAINS / NUM_SHB_SUBFR];
    5517     2336560 :             move16();
    5518             :         }
    5519             : 
    5520      146035 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    5521             :         {
    5522           0 :             hBWE_TD->idxSubGains = idxSubGain;
    5523           0 :             move16();
    5524             :         }
    5525             :         ELSE
    5526             :         {
    5527      146035 :             push_indice( hBstr, IND_SHB_SUBGAIN, idxSubGain, NUM_BITS_SHB_SUBGAINS );
    5528             :         }
    5529             :     }
    5530             : 
    5531      150884 :     return;
    5532             : }
    5533             : 
    5534             : 
    5535             : /*-------------------------------------------------------------------*
    5536             :  * Quant_shb_ener_sf_fx_fx()
    5537             :  *
    5538             :  * Quantize SHB subframe energies
    5539             :  *-------------------------------------------------------------------*/
    5540             : 
    5541        1224 : static void Quant_shb_ener_sf_fx(
    5542             :     Encoder_State *st_fx,    /* i/o: encoder state structure      */
    5543             :     Word32 *shb_ener_sf_Q31, /* i/o: super highband subframe energies  */
    5544             :     Word16 Q_ener )
    5545             : {
    5546             :     Word16 idxSubEner_fx;
    5547             :     Word16 temp_shb_ener_sf_fx;
    5548             :     Word16 exp /*, exp2*/, frac;
    5549             :     Word32 L_tmp1, L_tmp;
    5550             :     Word32 sum;
    5551             :     Word16 tmp;
    5552        1224 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5553             : 
    5554             :     /* shb_ener_sf_fx[0] = log10(0.003125*shb_ener_sf[0:319]); */
    5555        1224 :     sum = *shb_ener_sf_Q31; /* L_tmp in Q_ener = (2*Q_shb+1) */
    5556        1224 :     move32();
    5557             : 
    5558        1224 :     exp = norm_l( sum );
    5559        1224 :     frac = Log2_norm_lc( L_shl( sum, exp ) );
    5560        1224 :     exp = sub( 30, add( exp, Q_ener ) );  /* 30-(exp+Q_ener ) */
    5561        1224 :     L_tmp1 = Mpy_32_16( exp, frac, 617 ); /* 2466=LOG10(2) in Q11, so answer Ltmp in Q12 */
    5562             : 
    5563        1224 :     tmp = round_fx( L_shl( L_tmp1, 30 - 14 ) ); /* tmp in Q12 */
    5564             : 
    5565        1224 :     temp_shb_ener_sf_fx = 0;
    5566        1224 :     move16();
    5567        1224 :     idxSubEner_fx = usquant_fx( tmp, &temp_shb_ener_sf_fx, 0, 86, shl( 1, NUM_BITS_SHB_ENER_SF ) ); /* 86 = 0.042f in Q11 = Qin-1 */
    5568             :     /* o: temp_shb_ener_sf_fx in Q12 */
    5569             : 
    5570             :     /* shb_ener_sf_fx[0] = pow(10.0, temp_shb_ener_sf_fx );     */
    5571             :     /*                   = pow(2, 3.321928*temp_shb_ener_sf_fx) */
    5572        1224 :     L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
    5573        1224 :     L_tmp = L_shl( L_tmp, -10 );                  /* bring L_tmp from Q26 to Q16 */
    5574        1224 :     frac = L_Extract_lc( L_tmp, &exp );           /* Extract exponent */
    5575        1224 :     L_tmp = Pow2( 14, frac );
    5576             :     // *shb_ener_sf_Q31 = L_shl( L_tmp, exp - 14 + Q_ener );       /* In Q_ener */
    5577        1224 :     *shb_ener_sf_Q31 = L_shl( L_tmp, add( sub( exp, 14 ), Q_ener ) ); /* In Q_ener */
    5578        1224 :     move32();
    5579             : 
    5580        1224 :     hBWE_TD->idx_shb_fr_gain = idxSubEner_fx;
    5581        1224 :     move16();
    5582        1224 :     IF( NE_16( st_fx->codec_mode, MODE2 ) )
    5583             :     {
    5584           0 :         push_indice( st_fx->hBstr, IND_SHB_ENER_SF, idxSubEner_fx, NUM_BITS_SHB_ENER_SF );
    5585             :     }
    5586             : 
    5587        1224 :     return;
    5588             : }
    5589             : 
    5590             : 
    5591             : /*-------------------------------------------------------------------*
    5592             :  * Quant_shb_ener_sf_ivas_fx()
    5593             :  *
    5594             :  * Quantize SHB subframe energies
    5595             :  *-------------------------------------------------------------------*/
    5596             : 
    5597       32270 : static void Quant_shb_ener_sf_ivas_fx(
    5598             :     Encoder_State *st_fx,    /* i/o: encoder state structure                 */
    5599             :     Word32 *shb_ener_sf_Q31, /* i/o: super highband subframe energies Q_ener */
    5600             :     Word16 Q_ener )
    5601             : {
    5602             :     Word16 idxSubEner_fx;
    5603             :     Word16 temp_shb_ener_sf_fx;
    5604             :     Word16 exp /*, exp2*/, frac;
    5605             :     Word32 L_tmp1, L_tmp;
    5606             :     Word32 sum;
    5607             :     Word16 tmp;
    5608       32270 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5609             : 
    5610             :     /* shb_ener_sf_fx[0] = log10(0.003125*shb_ener_sf[0:319]); */
    5611       32270 :     sum = *shb_ener_sf_Q31; /* L_tmp in Q_ener = (2*Q_shb+1) */
    5612       32270 :     move32();
    5613             : 
    5614       32270 :     exp = norm_l( sum );
    5615       32270 :     frac = Log2_norm_lc( L_shl( sum, exp ) );
    5616       32270 :     exp = sub( 30, add( exp, Q_ener ) );  /* 30-(exp+Q_ener ) */
    5617       32270 :     L_tmp1 = Mpy_32_16( exp, frac, 308 ); /* 308=LOG10(2) in Q10, so answer Ltmp in Q11 */
    5618             : 
    5619       32270 :     tmp = extract_l( L_tmp1 ); /* tmp in Q11 */
    5620             : 
    5621       32270 :     temp_shb_ener_sf_fx = 0;
    5622       32270 :     move16();
    5623       32270 :     idxSubEner_fx = usquant_fx( tmp, &temp_shb_ener_sf_fx, 0, 43, 1 << NUM_BITS_SHB_ENER_SF ); /* 43 = 0.042f in Q10 = Qin-1 */
    5624             :     /* o: temp_shb_ener_sf_fx in Q11 */
    5625             : 
    5626             :     /* shb_ener_sf_fx[0] = pow(10.0, temp_shb_ener_sf_fx );     */
    5627             :     /*                   = pow(2, 3.321928*temp_shb_ener_sf_fx) */
    5628       32270 :     L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q11+Q13+1 = Q25 */
    5629       32270 :     L_tmp = L_shl( L_tmp, -9 );                   /* bring L_tmp from Q25 to Q16 */
    5630       32270 :     frac = L_Extract_lc( L_tmp, &exp );           /* Extract exponent */
    5631       32270 :     L_tmp = Pow2( 14, frac );
    5632       32270 :     *shb_ener_sf_Q31 = L_shl( L_tmp, add( sub( exp, 14 ), Q_ener ) ); /* In Q_ener */
    5633       32270 :     move32();
    5634             : 
    5635       32270 :     hBWE_TD->idx_shb_fr_gain = idxSubEner_fx;
    5636       32270 :     move16();
    5637       32270 :     IF( NE_16( st_fx->codec_mode, MODE2 ) )
    5638             :     {
    5639       32270 :         push_indice( st_fx->hBstr, IND_SHB_ENER_SF, idxSubEner_fx, NUM_BITS_SHB_ENER_SF );
    5640             :     }
    5641             : 
    5642       32270 :     return;
    5643             : }
    5644             : 
    5645             : 
    5646             : /*-------------------------------------------------------------------*
    5647             :  * Quant_shb_res_gshape_fx()
    5648             :  *
    5649             :  * Quantize SHB gain shapes in residual domain
    5650             :  *-------------------------------------------------------------------*/
    5651             : 
    5652        1224 : static void Quant_shb_res_gshape_fx(
    5653             :     Encoder_State *st_fx,      /* i/o: encoder state structure      */
    5654             :     Word16 shb_res_gshape_fx[] /* i/o: super highband gain shapes  Q14 */
    5655             : )
    5656             : {
    5657             :     Word16 i, idxSubGain_fx[NB_SUBFR16k];
    5658        1224 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5659             : 
    5660        7344 :     FOR( i = 0; i < NB_SUBFR16k; i++ )
    5661             :     {
    5662       12240 :         idxSubGain_fx[i] = usquant_fx( shb_res_gshape_fx[i],
    5663        6120 :                                        &shb_res_gshape_fx[i],
    5664             :                                        2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
    5665             :                                        1024 /*0.125f Q13*/, /*1024 = 0.125 in Q13 */
    5666        6120 :                                        shl( 1, NUM_BITS_SHB_RES_GS ) );
    5667        6120 :         move16();
    5668             : 
    5669        6120 :         hBWE_TD->idx_res_gs[i] = idxSubGain_fx[i];
    5670        6120 :         move16();
    5671        6120 :         IF( NE_16( st_fx->codec_mode, MODE2 ) )
    5672             :         {
    5673           0 :             push_indice( st_fx->hBstr, IND_SHB_RES_GS + i, idxSubGain_fx[i], NUM_BITS_SHB_RES_GS );
    5674             :         }
    5675             :     }
    5676             : 
    5677        1224 :     return;
    5678             : }
    5679             : 
    5680             : 
    5681             : /*-------------------------------------------------------------------*
    5682             :  * Quant_shb_res_gshape_ivas_fx()
    5683             :  *
    5684             :  * Quantize SHB gain shapes in residual domain
    5685             :  *-------------------------------------------------------------------*/
    5686             : 
    5687       32270 : static void Quant_shb_res_gshape_ivas_fx(
    5688             :     Encoder_State *st,         /* i/o: encoder state structure        */
    5689             :     Word16 shb_res_gshape_fx[] /* i/o: super highband gain shapes Q14 */
    5690             : )
    5691             : {
    5692             :     Word16 i, idxSubGain_fx[NB_SUBFR16k];
    5693       32270 :     TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
    5694             : 
    5695      193620 :     FOR( i = 0; i < NB_SUBFR16k; i++ )
    5696             :     {
    5697      322700 :         idxSubGain_fx[i] = usquant_fx( shb_res_gshape_fx[i],
    5698      161350 :                                        &shb_res_gshape_fx[i],
    5699             :                                        2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
    5700             :                                        1024 /*0.125f Q13*/, /*1024 = 0.125 in Q13 */
    5701      161350 :                                        shl( 1, NUM_BITS_SHB_RES_GS ) );
    5702      161350 :         move16();
    5703             : 
    5704      161350 :         IF( EQ_16( st->codec_mode, MODE2 ) )
    5705             :         {
    5706           0 :             hBWE_TD->idx_res_gs[i] = idxSubGain_fx[i];
    5707           0 :             move16();
    5708             :         }
    5709             :         ELSE
    5710             :         {
    5711      161350 :             push_indice( st->hBstr, IND_SHB_RES_GS + i, idxSubGain_fx[i], NUM_BITS_SHB_RES_GS );
    5712             :         }
    5713             :     }
    5714             : 
    5715       32270 :     return;
    5716             : }
    5717             : 
    5718             : 
    5719             : /*==========================================================================*/
    5720             : /* FUNCTION : static void QuantizeSHBframegain_fx() */
    5721             : /*--------------------------------------------------------------------------*/
    5722             : /* PURPOSE : QQuantize super highband frame gain */
    5723             : /*--------------------------------------------------------------------------*/
    5724             : /* INPUT ARGUMENTS : */
    5725             : /* Word16 extl i : extension layer */
    5726             : /* Word32 extl_brate i : extension layer bitrate */
    5727             : /*--------------------------------------------------------------------------*/
    5728             : /* OUTPUT ARGUMENTS : */
    5729             : /*--------------------------------------------------------------------------*/
    5730             : /* INPUT/OUTPUT ARGUMENTS : */
    5731             : /* Word32 *GainFrame i/o: Gain Q18 */
    5732             : /*--------------------------------------------------------------------------*/
    5733             : /* RETURN ARGUMENTS : */
    5734             : /* _ None */
    5735             : /*--------------------------------------------------------------------------*/
    5736             : /* */
    5737             : /*==========================================================================*/
    5738             : 
    5739        2639 : static void QuantizeSHBframegain_fx(
    5740             :     Encoder_State *st_fx, /* i/o: encoder state structure */
    5741             :     Word32 *GainFrame,    /* i/o: Gain Q18 */
    5742             :     const Word16 extl,    /* i : extension layer */
    5743             :     Word32 extl_brate,    /* i : extension layer bitrate */
    5744             :     Word16 *rf_gainFrame_ind )
    5745             : {
    5746             :     Word16 idxFrameGain;
    5747             :     Word32 Q_GainFrame;
    5748        2639 :     Word16 Unit_weights1 = 1;
    5749        2639 :     move16();
    5750             :     Word16 exp, frac, tmp;
    5751             :     Word32 L_tmp;
    5752             :     Word32 GainFrameLog;
    5753        2639 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5754        2639 :     RF_ENC_HANDLE hRF = st_fx->hRF;
    5755             : 
    5756        2639 :     IF( EQ_16( extl, WB_TBE ) )
    5757             :     {
    5758           0 :         determine_gain_weights_fx( GainFrame, &( Unit_weights1 ), 1 );
    5759           0 :         IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
    5760             :         {
    5761           0 :             singlevectortest_gain_fx( GainFrame, 1,
    5762             :                                       1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain,
    5763             :                                       &Q_GainFrame, SHBCB_FrameGain16_fx );
    5764           0 :             test();
    5765           0 :             IF( GT_32( Q_GainFrame, L_shl( Mult_32_16( *GainFrame, 17367 ), 1 ) ) && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */
    5766             :             {
    5767           0 :                 idxFrameGain--;
    5768           0 :                 Q_GainFrame = L_add( SHBCB_FrameGain16_fx[idxFrameGain], 0 ); /* Q18 */
    5769             :             }
    5770           0 :             hBWE_TD->gFrame_WB = idxFrameGain;
    5771           0 :             move16();
    5772           0 :             *rf_gainFrame_ind = idxFrameGain;
    5773             :         }
    5774             :         ELSE
    5775             :         {
    5776           0 :             singlevectortest_gain_fx( GainFrame, 1,
    5777             :                                       1 << NUM_BITS_SHB_FrameGain, &idxFrameGain, &Q_GainFrame,
    5778             :                                       SHBCB_FrameGain64_fx );
    5779             : 
    5780           0 :             push_indice( st_fx->hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain );
    5781           0 :             *rf_gainFrame_ind = idxFrameGain;
    5782           0 :             move16(); /* Q18 */
    5783             :         }
    5784             :     }
    5785             :     ELSE
    5786             :     {
    5787        2639 :         IF( *GainFrame == 0 )
    5788             :         {
    5789           0 :             GainFrameLog = -196608;
    5790           0 :             move32();
    5791             :         }
    5792             :         ELSE
    5793             :         {
    5794        2639 :             exp = norm_l( *GainFrame );
    5795        2639 :             frac = Log2_norm_lc( L_shl( *GainFrame, exp ) );
    5796             :             // exp = ( 30 - exp - 18 );
    5797        2639 :             exp = sub( 30 - 18, exp );
    5798        2639 :             GainFrameLog = Mpy_32_16( exp, frac, 9864 );
    5799             :             /*GainFrameLog= round_fx(L_shl(L_tmp,12)); //Q12 */
    5800             :         }
    5801             : 
    5802        2639 :         exp = norm_s( SHB_GAIN_QDELTA_FX_15 );
    5803        2639 :         tmp = div_s( shl( 1, sub( 14, exp ) ), SHB_GAIN_QDELTA_FX_15 );
    5804        2639 :         L_tmp = Mult_32_16( L_sub( GainFrameLog, SHB_GAIN_QLOW_FX_16 ), tmp );
    5805        2639 :         idxFrameGain = extract_l( L_shr( L_add( L_tmp, shl( 1, sub( 14, exp ) ) ), sub( 15, exp ) ) ); /*Q0*/
    5806        2639 :         IF( GT_16( idxFrameGain, ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1 ) )
    5807             :         {
    5808           0 :             idxFrameGain = ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1;
    5809           0 :             move16();
    5810             :         }
    5811             :         ELSE
    5812             :         {
    5813        2639 :             if ( idxFrameGain < 0 )
    5814             :             {
    5815           0 :                 idxFrameGain = 0;
    5816           0 :                 move16();
    5817             :             }
    5818             :         }
    5819             : 
    5820        2639 :         L_tmp = SHB_GAIN_QLOW_FX_16;
    5821        2639 :         Q_GainFrame = L_mac( L_tmp, idxFrameGain, SHB_GAIN_QDELTA_FX_15 );
    5822             : 
    5823        2639 :         test();
    5824        2652 :         WHILE( GT_32( Q_GainFrame, L_add( GainFrameLog, 4866 ) ) &&
    5825             :                idxFrameGain != 0 )
    5826             :         {
    5827          13 :             test();
    5828          13 :             idxFrameGain = sub( idxFrameGain, 1 );
    5829          13 :             Q_GainFrame = L_mac0( SHB_GAIN_QLOW_FX_16, idxFrameGain, SHB_GAIN_QDELTA_FX_16 );
    5830             :         }
    5831             : 
    5832             :         /* Q_GainFrame = (float) pow(10.0, Q_GainFrame ); */
    5833             :         /* i: Q_GainFrame in Q16 */
    5834        2639 :         L_tmp = Mult_32_16( Q_GainFrame, 27213 ); /* *3.321928 in Q13 -> Q25 */
    5835        2639 :         L_tmp = L_shr( L_tmp, -2 );               /* From Q26 to Q16 */
    5836        2639 :         frac = L_Extract_lc( L_tmp, &exp );
    5837        2639 :         Q_GainFrame = Pow2( 30, frac );
    5838        2639 :         exp = sub( exp, 30 );
    5839        2639 :         Q_GainFrame = L_shl( Q_GainFrame, add( exp, 18 ) ); /* Q18 */
    5840             : 
    5841        2639 :         hBWE_TD->idxFrameGain = idxFrameGain;
    5842        2639 :         move16();
    5843        2639 :         IF( NE_16( st_fx->codec_mode, MODE2 ) )
    5844             :         {
    5845        1415 :             push_indice( st_fx->hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
    5846             :         }
    5847        2639 :         *rf_gainFrame_ind = idxFrameGain;
    5848        2639 :         move16();
    5849             :     }
    5850             : 
    5851        2639 :     IF( EQ_16( st_fx->rf_mode, 1 ) ){
    5852             :         /*Currently intended for SWB only. Modify for WB is needed later!*/
    5853           0 :         IF( EQ_16( hRF->rf_frame_type, RF_NELP ) ){
    5854           0 :                 *rf_gainFrame_ind = idxFrameGain; /* NELP Frame uses full 5 bits */
    5855           0 :     move16();
    5856             : }
    5857             : ELSE /*RF_ALLPRED, RF_GENPRED, RF_NOPRED modes*/
    5858             : {
    5859           0 :     IF( *GainFrame <= 327680 /*1.25 in Q18*/ ) /* [0 to 1.25] range --> 0.5*/
    5860             :     {
    5861           0 :         *rf_gainFrame_ind = 0;
    5862             :     }
    5863           0 :     ELSE IF( *GainFrame <= 786432 /*3 in Q18*/ ) /* (1.25 to 3] --> 2 */
    5864             :     {
    5865           0 :         *rf_gainFrame_ind = 1;
    5866             :     }
    5867           0 :     ELSE IF( *GainFrame <= 1572864 /*6 in Q18*/ ) /* (3 to 6] --> 4 */
    5868             :     {
    5869           0 :         *rf_gainFrame_ind = 2;
    5870             :     }
    5871             :     ELSE /* (6 to Inf) --> 8 */
    5872             :     {
    5873           0 :         *rf_gainFrame_ind = 3;
    5874             :     }
    5875           0 :     move16();
    5876             : }
    5877             : }
    5878             : 
    5879        2639 : *GainFrame = Q_GainFrame;
    5880        2639 : move32(); /* Q18 */
    5881             : 
    5882        2639 : return;
    5883             : }
    5884             : 
    5885             : 
    5886             : /*==========================================================================*/
    5887             : /* FUNCTION : static void QuantizeSHBframegain_ivas_fx() */
    5888             : /*--------------------------------------------------------------------------*/
    5889             : /* PURPOSE : QQuantize super highband frame gain */
    5890             : /*--------------------------------------------------------------------------*/
    5891             : /* INPUT ARGUMENTS : */
    5892             : /* Word16 extl i : extension layer */
    5893             : /* Word32 extl_brate i : extension layer bitrate */
    5894             : /*--------------------------------------------------------------------------*/
    5895             : /* OUTPUT ARGUMENTS : */
    5896             : /*--------------------------------------------------------------------------*/
    5897             : /* INPUT/OUTPUT ARGUMENTS : */
    5898             : /* Word32 *GainFrame i/o: Gain Q18 */
    5899             : /*--------------------------------------------------------------------------*/
    5900             : /* RETURN ARGUMENTS : */
    5901             : /* _ None */
    5902             : /*--------------------------------------------------------------------------*/
    5903             : /* */
    5904             : /*==========================================================================*/
    5905             : 
    5906      153326 : static void QuantizeSHBframegain_ivas_fx(
    5907             :     Encoder_State *st_fx, /* i/o: encoder state structure     */
    5908             :     Word32 *GainFrame,    /* i/o: Gain                    Q18 */
    5909             :     const Word16 extl,    /* i : extension layer              */
    5910             :     Word32 extl_brate,    /* i : extension layer bitrate      */
    5911             :     Word16 *rf_gainFrame_ind,
    5912             :     const Word16 flag_conservative /* i : flag to indicate more conservative gain quantization */
    5913             : )
    5914             : {
    5915             :     Word16 idxFrameGain;
    5916             :     Word32 Q_GainFrame;
    5917      153326 :     Word16 Unit_weights1 = 1;
    5918             :     Word16 exp, frac, tmp;
    5919             :     Word32 L_tmp;
    5920             :     Word32 GainFrameLog;
    5921      153326 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    5922      153326 :     RF_ENC_HANDLE hRF = st_fx->hRF;
    5923      153326 :     move16();
    5924             : 
    5925      153326 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    5926             : 
    5927      153326 :     IF( EQ_16( extl, WB_TBE ) )
    5928             :     {
    5929        7291 :         determine_gain_weights_fx( GainFrame, &( Unit_weights1 ), 1 );
    5930        7291 :         IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
    5931             :         {
    5932        2442 :             singlevectortest_gain_fx( GainFrame, 1,
    5933             :                                       1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain,
    5934             :                                       &Q_GainFrame, SHBCB_FrameGain16_fx );
    5935        2442 :             test();
    5936        2442 :             IF( GT_64( W_deposit32_l( Q_GainFrame ), W_shl( W_mult_32_16( *GainFrame, 17367 ), 15 ) ) && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */
    5937             :             {
    5938           0 :                 idxFrameGain--;
    5939           0 :                 Q_GainFrame = L_add( SHBCB_FrameGain16_fx[idxFrameGain], 0 ); /* Q18 */
    5940             :             }
    5941             : 
    5942        2442 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    5943             :             {
    5944           0 :                 hBWE_TD->gFrame_WB = idxFrameGain;
    5945           0 :                 move16();
    5946             :             }
    5947             :             ELSE
    5948             :             {
    5949        2442 :                 push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain_LBR_WB );
    5950             :             }
    5951             : 
    5952        2442 :             if ( st_fx->rf_mode )
    5953             :             {
    5954           0 :                 *rf_gainFrame_ind = idxFrameGain;
    5955           0 :                 move16();
    5956             :             }
    5957             :         }
    5958             :         ELSE
    5959             :         {
    5960        4849 :             singlevectortest_gain_fx( GainFrame, 1,
    5961             :                                       1 << NUM_BITS_SHB_FrameGain, &idxFrameGain, &Q_GainFrame,
    5962             :                                       SHBCB_FrameGain64_fx );
    5963             : 
    5964        4849 :             push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FrameGain );
    5965             : 
    5966        4849 :             if ( st_fx->rf_mode )
    5967             :             {
    5968           0 :                 *rf_gainFrame_ind = idxFrameGain;
    5969           0 :                 move16();
    5970             :             }
    5971             :         }
    5972             :     }
    5973             :     ELSE
    5974             :     {
    5975      146035 :         IF( *GainFrame == 0 )
    5976             :         {
    5977         615 :             GainFrameLog = -196608; /* Q16 */
    5978         615 :             move32();
    5979             :         }
    5980             :         ELSE
    5981             :         {
    5982      145420 :             exp = norm_l( *GainFrame );
    5983      145420 :             frac = Log2_norm_lc( L_shl( *GainFrame, exp ) );
    5984      145420 :             exp = sub( 30 - 18, exp );
    5985      145420 :             GainFrameLog = Mpy_32_16( exp, frac, 9864 );
    5986             :             /*GainFrameLog= round_fx(L_shl(L_tmp,12)); //Q12 */
    5987             :         }
    5988             : 
    5989      146035 :         IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    5990             :         {
    5991             :             /* idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW_1k75, SHB_GAIN_QDELTA_1k75, 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 ); */
    5992        3429 :             exp = norm_s( SHB_GAIN_QDELTA_1k75_FX_15 );
    5993        3429 :             tmp = div_s( shl( 1, sub( 14, exp ) ), SHB_GAIN_QDELTA_1k75_FX_15 );
    5994        3429 :             L_tmp = Mpy_32_16_1( L_sub( GainFrameLog, SHB_GAIN_QLOW_1k75_FX ), tmp );
    5995        3429 :             idxFrameGain = extract_l( L_shr( L_add( L_tmp, shl( 1, sub( 14, exp ) ) ), sub( 15, exp ) ) ); /*Q0*/
    5996        3429 :             IF( GT_16( idxFrameGain, ( 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 ) - 1 ) )
    5997             :             {
    5998           0 :                 idxFrameGain = ( 1 << NUM_BITS_SHB_FRAMEGAIN_1k75 ) - 1;
    5999           0 :                 move16();
    6000             :             }
    6001             :             ELSE
    6002             :             {
    6003        3429 :                 if ( idxFrameGain < 0 )
    6004             :                 {
    6005          86 :                     idxFrameGain = 0;
    6006          86 :                     move16();
    6007             :                 }
    6008             :             }
    6009        3429 :             L_tmp = SHB_GAIN_QLOW_1k75_FX;
    6010        3429 :             move32();
    6011        3429 :             Q_GainFrame = L_mac( L_tmp, idxFrameGain, SHB_GAIN_QDELTA_1k75_FX_15 );
    6012             :         }
    6013             :         ELSE
    6014             :         {
    6015             :             /* idxFrameGain = (int16_t) usquant( GainFrameLog, &Q_GainFrame, SHB_GAIN_QLOW, SHB_GAIN_QDELTA, 1 << NUM_BITS_SHB_FRAMEGAIN ); */
    6016      142606 :             exp = norm_s( SHB_GAIN_QDELTA_FX_15 );
    6017      142606 :             tmp = div_s( shl( 1, sub( 14, exp ) ), SHB_GAIN_QDELTA_FX_15 );
    6018      142606 :             L_tmp = Mpy_32_16_1( L_sub( GainFrameLog, SHB_GAIN_QLOW_FX_16 ), tmp );
    6019      142606 :             idxFrameGain = extract_l( L_shr( L_add( L_tmp, shl( 1, sub( 14, exp ) ) ), sub( 15, exp ) ) ); /*Q0*/
    6020      142606 :             IF( GT_16( idxFrameGain, ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1 ) )
    6021             :             {
    6022           3 :                 idxFrameGain = ( 1 << NUM_BITS_SHB_FRAMEGAIN ) - 1;
    6023           3 :                 move16();
    6024             :             }
    6025             :             ELSE
    6026             :             {
    6027      142603 :                 if ( idxFrameGain < 0 )
    6028             :                 {
    6029         626 :                     idxFrameGain = 0;
    6030         626 :                     move16();
    6031             :                 }
    6032             :             }
    6033      142606 :             L_tmp = SHB_GAIN_QLOW_FX_16;
    6034      142606 :             move32();
    6035      142606 :             Q_GainFrame = L_mac( L_tmp, idxFrameGain, SHB_GAIN_QDELTA_FX_15 );
    6036             :         }
    6037             : 
    6038      146035 :         test();
    6039      146646 :         WHILE( GT_32( Q_GainFrame, L_add( GainFrameLog, 4866 ) ) &&
    6040             :                idxFrameGain != 0 )
    6041             :         {
    6042         611 :             test();
    6043         611 :             idxFrameGain = sub( idxFrameGain, 1 );
    6044         611 :             Q_GainFrame = L_mac0( SHB_GAIN_QLOW_FX_16, idxFrameGain, SHB_GAIN_QDELTA_FX_16 );
    6045             :         }
    6046             : 
    6047      146035 :         test();
    6048      146035 :         test();
    6049      146035 :         IF( flag_conservative && GT_32( Q_GainFrame, L_add( GainFrameLog, Mpy_32_16_1( SHB_GAIN_QDELTA_FX_16, 13435 /* 0.41 in Q15 */ ) ) ) && idxFrameGain != 0 )
    6050             :         {
    6051             :             /* be more conservative in the quantization process -> prevent energy overshoot */
    6052          80 :             idxFrameGain = sub( idxFrameGain, 1 );
    6053          80 :             Q_GainFrame = L_mac0( SHB_GAIN_QLOW_FX_16, idxFrameGain, SHB_GAIN_QDELTA_FX_16 );
    6054             :         }
    6055             : 
    6056             :         /* Q_GainFrame = (float) pow(10.0, Q_GainFrame ); */
    6057             :         /* i: Q_GainFrame in Q16 */
    6058      146035 :         L_tmp = Mpy_32_16_1( Q_GainFrame, 27213 ); /* *3.321928 in Q13 -> Q25 */
    6059      146035 :         L_tmp = L_shr( L_tmp, -2 );                /* From Q26 to Q16 */
    6060      146035 :         frac = L_Extract_lc( L_tmp, &exp );
    6061      146035 :         Q_GainFrame = Pow2( 30, frac );
    6062      146035 :         exp = sub( exp, 30 );
    6063      146035 :         Q_GainFrame = L_shl( Q_GainFrame, add( exp, 18 ) ); /* Q18 */
    6064             : 
    6065      146035 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    6066             :         {
    6067           0 :             st_fx->hBWE_TD->idxFrameGain = idxFrameGain;
    6068           0 :             move16();
    6069             :         }
    6070             :         ELSE
    6071             :         {
    6072      146035 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    6073             :             {
    6074        3429 :                 push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN_1k75 );
    6075             :             }
    6076             :             ELSE
    6077             :             {
    6078      142606 :                 push_indice( hBstr, IND_SHB_FRAMEGAIN, idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
    6079             :             }
    6080             :         }
    6081             : 
    6082      146035 :         if ( st_fx->rf_mode )
    6083             :         {
    6084           0 :             *rf_gainFrame_ind = idxFrameGain;
    6085           0 :             move16();
    6086             :         }
    6087             :     }
    6088             : 
    6089      153326 :     IF( EQ_16( st_fx->rf_mode, 1 ) ){
    6090             :         /*Currently intended for SWB only. Modify for WB is needed later!*/
    6091           0 :         IF( EQ_16( hRF->rf_frame_type, RF_NELP ) ){
    6092           0 :                 *rf_gainFrame_ind = idxFrameGain; /* NELP Frame uses full 5 bits */
    6093           0 :     move16();
    6094             : }
    6095             : ELSE /*RF_ALLPRED, RF_GENPRED, RF_NOPRED modes*/
    6096             : {
    6097           0 :     IF( LE_32( *GainFrame, 327680 /*1.25 in Q18*/ ) ) /* [0 to 1.25] range --> 0.5*/
    6098             :     {
    6099           0 :         *rf_gainFrame_ind = 0;
    6100           0 :         move16();
    6101             :     }
    6102           0 :     ELSE IF( LE_32( *GainFrame, 786432 /*3 in Q18*/ ) ) /* (1.25 to 3] --> 2 */
    6103             :     {
    6104           0 :         *rf_gainFrame_ind = 1;
    6105           0 :         move16();
    6106             :     }
    6107           0 :     ELSE IF( LE_32( *GainFrame, 1572864 /*6 in Q18*/ ) ) /* (3 to 6] --> 4 */
    6108             :     {
    6109           0 :         *rf_gainFrame_ind = 2;
    6110           0 :         move16();
    6111             :     }
    6112             :     ELSE /* (6 to Inf) --> 8 */
    6113             :     {
    6114           0 :         *rf_gainFrame_ind = 3;
    6115           0 :         move16();
    6116             :     }
    6117             : }
    6118             : }
    6119             : 
    6120      153326 : *GainFrame = Q_GainFrame;
    6121      153326 : move32(); /* Q18 */
    6122             : 
    6123      153326 : return;
    6124             : }
    6125             : 
    6126             : 
    6127             : /*============================================================*/
    6128             : /* FUNCTION : static void determine_gain_weights_fx() */
    6129             : /*------------------------------------------------------------*/
    6130             : /* PURPOSE : Determine weights for gain quantization */
    6131             : /*------------------------------------------------------------*/
    6132             : /* INPUT ARGUMENTS : */
    6133             : /* _(Word32*)gain :Gain parameter Q18 */
    6134             : /* _(Word16) dims : number of gains */
    6135             : /*------------------------------------------------------------*/
    6136             : /* OUTPUT ARGUMENTS : */
    6137             : /* _(Word16*)weights : gain weights Q12/Q6 */
    6138             : /*------------------------------------------------------------*/
    6139             : /* INPUT/OUTPUT ARGUMENTS : */
    6140             : /* _ None */
    6141             : /*------------------------------------------------------------*/
    6142             : /* RETURN ARGUMENTS : */
    6143             : /* _ None */
    6144             : /*------------------------------------------------------------*/
    6145        7291 : static void determine_gain_weights_fx(
    6146             :     const Word32 *gain, /* i : Gain parameter Q18 */
    6147             :     Word16 *weights,    /* o : gain weights Q12/Q6*/
    6148             :     const Word16 dims /* i : number of gains */ )
    6149             : {
    6150             :     Word16 j;
    6151             :     Word16 exp, exp1, frac, tmp, exp2;
    6152             :     Word32 L_tmp;
    6153             : 
    6154       14582 :     FOR( j = 0; j < dims; j++ )
    6155             :     {
    6156        7291 :         IF( GT_32( gain[j], 8 ) ) /* 8 = 0.001 in Q13 */
    6157             :         {
    6158             :             /*weights[j] = (float)(pow (fabs (gain[j]), -0.9f));  = pow(2,(-0.9)*log2(gain[j]))           */
    6159        7177 :             exp1 = norm_l( gain[j] );
    6160        7177 :             frac = Log2_norm_lc( L_shl( gain[j], exp1 ) );
    6161        7177 :             move16();
    6162        7177 :             exp = sub( 30 - 18, exp1 );
    6163        7177 :             move16();
    6164        7177 :             L_tmp = Mpy_32_16( exp, frac, -29491 ); /* Q16 */
    6165        7177 :             frac = L_Extract_lc( L_tmp, &exp );
    6166        7177 :             tmp = extract_l( Pow2( 14, frac ) ); /* Q14 */
    6167             : 
    6168        7177 :             exp2 = sub( exp, 8 );
    6169        7177 :             IF( LE_16( exp1, 21 ) )
    6170             :             {
    6171        7177 :                 exp2 = sub( exp, 2 );
    6172             :             }
    6173        7177 :             weights[j] = shl_sat( tmp, exp2 );
    6174        7177 :             move16(); /* Q12 */
    6175             :         }
    6176             :         ELSE
    6177             :         {
    6178         114 :             weights[j] = 32076;
    6179         114 :             move16(); /* (501.187233628f) in Q6*/
    6180             :         }
    6181             :     }
    6182             : 
    6183        7291 :     return;
    6184             : }
    6185             : 
    6186             : 
    6187             : /*==============================================================================*/
    6188             : /* FUNCTION : static singlevectortest_gain_fx () */
    6189             : /*------------------------------------------------------------------------------*/
    6190             : /* PURPOSE : Single stage VQ for coding */
    6191             : /*------------------------------------------------------------------------------*/
    6192             : /* INPUT ARGUMENTS : */
    6193             : /* _(Word32*) inp : input gain vector Q18 */
    6194             : /* _(Word16) dimen : dimension of the input vector */
    6195             : /* _(Word16) cb_size : codebook size */
    6196             : /* _(Word16*) weight : Weights for the quanitzation */
    6197             : /* _(Word32*) codebook : Codebook 19Q13 */
    6198             : /*------------------------------------------------------------------------------*/
    6199             : /* OUTPUT ARGUMENTS : */
    6200             : /* _(Word16*)index :quanitzation index */
    6201             : /* _(Word32*)recon :Reconstruction 19Q13 */
    6202             : /*------------------------------------------------------------------------------*/
    6203             : /* INPUT/OUTPUT ARGUMENTS : */
    6204             : /* _None */
    6205             : /*------------------------------------------------------------------------------*/
    6206             : /* RETURN ARGUMENTS : */
    6207             : /* _ None */
    6208             : /*------------------------------------------------------------------------------*/
    6209        7291 : static void singlevectortest_gain_fx(
    6210             :     const Word32 *inp,    /* i : input gain vector Q18*/
    6211             :     const Word16 dimen,   /* i : dimension of the input vector */
    6212             :     const Word16 cb_size, /* i : codebook size */
    6213             :     Word16 *index,        /* o : quanitzation index */
    6214             :     Word32 *recon,        /* o : Reconstruction Q18 */
    6215             :     const Word32 *codebook
    6216             :     /* i : Codebook Q18*/ )
    6217             : {
    6218             :     Word16 k, interNum, flag;
    6219             :     Word32 meanU, meanQ;
    6220             :     Word16 least[4];
    6221             : 
    6222        7291 :     interNum = 4;
    6223        7291 :     move16();
    6224             : 
    6225        7291 :     return_M_Least_fx_GainFrame( inp, codebook, cb_size, interNum, least );
    6226             : 
    6227        7291 :     meanU = sum32_fx( inp, dimen ); /* Q18 */
    6228        7291 :     Copy32( codebook + dimen * least[0], recon, dimen );
    6229             : 
    6230        7291 :     index[0] = least[0];
    6231        7291 :     move16();
    6232        7291 :     flag = 0;
    6233        7291 :     move16();
    6234       36455 :     FOR( k = 0; k < interNum; k++ )
    6235             :     {
    6236       29164 :         IF( flag == 0 )
    6237             :         {
    6238        8369 :             meanQ = sum32_fx( codebook + dimen * least[k], dimen ); /* Q18 */
    6239             :             /* if ( meanQ <= 1.1 * meanU ) */
    6240        8369 :             IF( LE_32( Mpy_32_16_1( meanQ, 29789 /* 1/1.1 in Q15*/ ), meanU ) )
    6241             :             {
    6242        7161 :                 flag = 1;
    6243        7161 :                 move16();
    6244        7161 :                 Copy32( codebook + dimen * least[k], recon, dimen );
    6245        7161 :                 index[0] = least[k];
    6246        7161 :                 move16();
    6247             :             }
    6248             :         }
    6249             :     }
    6250             : 
    6251        7291 :     return;
    6252             : }
    6253             : 
    6254             : 
    6255             : /*==============================================================================*/
    6256             : /* FUNCTION : return_M_Least_fx_GainFrame () */
    6257             : /*------------------------------------------------------------------------------*/
    6258             : /* PURPOSE : */
    6259             : /*------------------------------------------------------------------------------*/
    6260             : /* INPUT ARGUMENTS : */
    6261             : /* _(Word32*) inp : input Q18 */
    6262             : /* _(Word16*) weight : input gain weights */
    6263             : /* _(Word16) n_cols : vector size */
    6264             : /* _(Word16) num_grp : number of centroids for 1st stage */
    6265             : /* _(Word16) interNum : number on short list prior to 2nd stage search */
    6266             : /* _(Word32*) codebook : first stage codebook Q18 */
    6267             : /*------------------------------------------------------------------------------*/
    6268             : /* OUTPUT ARGUMENTS : */
    6269             : /* _(Word16*)least :return value */
    6270             : /*------------------------------------------------------------------------------*/
    6271             : /* INPUT/OUTPUT ARGUMENTS : */
    6272             : /* _None */
    6273             : /*------------------------------------------------------------------------------*/
    6274             : /* RETURN ARGUMENTS : */
    6275             : /* _ None */
    6276             : /*------------------------------------------------------------------------------*/
    6277        7291 : static void return_M_Least_fx_GainFrame(
    6278             :     const Word32 *inp,      /* i: input Q18*/
    6279             :     const Word32 *codebook, /* i: codebook Q18*/
    6280             :     const Word16 num_grp,   /* i: number of centroids */
    6281             :     const Word16 interNum,  /* i: number on short list prior to 2nd stage search*/
    6282             :     Word16 *least /* o: return value */ )
    6283             : {
    6284             :     Word16 i, k;
    6285             :     Word32 distance[1024], mindist;
    6286             : 
    6287             :     Word16 n;
    6288             :     Word32 diff[1024];
    6289             :     Word32 max_diff;
    6290        7291 :     mindist = L_add( MAX_32, 0 );
    6291             : 
    6292        7291 :     diff[0] = L_sub( *inp, codebook[0] );
    6293        7291 :     move32();
    6294        7291 :     max_diff = L_abs( diff[0] );
    6295      349408 :     FOR( i = 1; i < num_grp; i++ )
    6296             :     {
    6297      342117 :         diff[i] = L_sub( *inp, codebook[i] );
    6298      342117 :         move32();
    6299      342117 :         max_diff = L_max( max_diff, L_abs( diff[i] ) );
    6300             :     }
    6301        7291 :     n = 31;
    6302        7291 :     if ( max_diff != 0 )
    6303             :     {
    6304        7291 :         n = norm_l( max_diff );
    6305             :     }
    6306             : 
    6307      356699 :     FOR( i = 0; i < num_grp; i++ )
    6308             :     {
    6309      349408 :         diff[i] = L_shl( diff[i], n );
    6310      349408 :         move32();
    6311             :     }
    6312             : 
    6313      356699 :     FOR( i = 0; i < num_grp; i++ )
    6314             :     {
    6315      349408 :         distance[i] = L_deposit_l( 0 );
    6316      349408 :         distance[i] = Mult_32_32( diff[i], diff[i] );
    6317      349408 :         move32();
    6318             : 
    6319      349408 :         if ( LT_32( distance[i], mindist ) )
    6320             :         {
    6321      157716 :             least[0] = i;
    6322      157716 :             move16();
    6323             :         }
    6324      349408 :         mindist = L_min( mindist, distance[i] );
    6325             :     }
    6326             : 
    6327        7291 :     distance[least[0]] = MAX_32;
    6328        7291 :     move32();
    6329             : 
    6330       29164 :     FOR( k = 1; k < interNum; k++ )
    6331             :     {
    6332       21873 :         mindist = L_add( MAX_32, 0 );
    6333     1070097 :         FOR( i = 0; i < num_grp; i++ )
    6334             :         {
    6335     1048224 :             if ( LT_32( distance[i], mindist ) )
    6336             :             {
    6337      446330 :                 least[k] = i;
    6338      446330 :                 move16();
    6339             :             }
    6340     1048224 :             mindist = L_min( mindist, distance[i] );
    6341             :         }
    6342             : 
    6343       21873 :         distance[least[k]] = MAX_32;
    6344       21873 :         move32();
    6345             :     }
    6346             : 
    6347        7291 :     return;
    6348             : }
    6349             : 
    6350             : 
    6351             : /*-------------------------------------------------------------------*
    6352             :  * Quant_lower_LSF_fx()
    6353             :  *-------------------------------------------------------------------*/
    6354             : 
    6355      107655 : static void Quant_lower_LSF_fx(
    6356             :     const Word16 lsf[], /* i : Input LSFs             Q15 */
    6357             :     Word16 lsf_q[],     /* o : Quantized LSFs         Q15 */
    6358             :     Word16 lsf_idx[]    /* o : Quantized LSFs indices     */
    6359             : )
    6360             : {
    6361             :     Word16 i;
    6362             : 
    6363      107655 :     lsf_idx[0] = squant_fx( lsf[0], &lsf_q[0], lsf_q_cb_fx[0], lsf_q_cb_size[0] );
    6364      107655 :     move16();
    6365      538275 :     FOR( i = 1; i < NUM_Q_LSF; i++ )
    6366             :     {
    6367      430620 :         lsf_idx[i] = squant_fx( sub( lsf[i], lsf_q[i - 1] ), &lsf_q[i], lsf_q_cb_fx[i], lsf_q_cb_size[i] );
    6368      430620 :         move16();
    6369      430620 :         lsf_q[i] = add( lsf_q[i - 1], lsf_q[i] );
    6370      430620 :         move16();
    6371             :     }
    6372             : 
    6373      107655 :     return;
    6374             : }
    6375             : 
    6376             : 
    6377             : /*-------------------------------------------------------------------*
    6378             :  * Quant_mirror_point_fx()
    6379             :  *-------------------------------------------------------------------*/
    6380             : 
    6381      107655 : static Word16 Quant_mirror_point_fx(
    6382             :     const Word16 lsf[],   /* i : Input LSFs     Q15 */
    6383             :     const Word16 lsf_q[], /* i : Quantized LSFs Q15 */
    6384             :     Word16 *m             /* o : Mirror point   Q15 */
    6385             : )
    6386             : {
    6387             :     Word16 m_diff;
    6388             :     Word16 m_idx;
    6389             : 
    6390      107655 :     m_diff = mult_r( sub( lsf[NUM_Q_LSF], lsf_q[NUM_Q_LSF - 1] ), 16384 /* 0.5f in Q15 */ );
    6391             : 
    6392      107655 :     m_idx = squant_fx( m_diff, m, mirror_point_q_cb_fx, MIRROR_POINT_Q_CB_SIZE );
    6393             : 
    6394      107655 :     *m = add( lsf_q[NUM_Q_LSF - 1], *m );
    6395      107655 :     move16();
    6396             : 
    6397      107655 :     return m_idx;
    6398             : }
    6399             : 
    6400             : 
    6401             : /*-------------------------------------------------------------------*
    6402             :  * Find_LSF_grid()
    6403             :  *
    6404             :  * Find the best grid for the LSFs
    6405             :  *-------------------------------------------------------------------*/
    6406             : 
    6407      107655 : static Word16 Find_LSF_grid_fx(
    6408             :     const Word16 lsf[], /* i : Input LSFs     Q15 */
    6409             :     Word16 lsf_q[],     /* o : Quantized LSFs Q15 */
    6410             :     const Word16 m      /* i : Mirror point   Q15 */
    6411             : )
    6412             : {
    6413             :     Word16 lsf_map[NUM_MAP_LSF];
    6414             :     Word16 grid[NUM_LSF_GRIDS][NUM_MAP_LSF];
    6415             :     Word16 offset;
    6416             :     Word16 last_q_lsf;
    6417             :     Word16 lsf_t[NUM_MAP_LSF];
    6418             :     Word16 lsf_smooth[NUM_MAP_LSF];
    6419             :     Word32 D, D_best;
    6420      107655 :     Word16 I_best = 0;
    6421             :     Word16 i, j;
    6422             :     Word16 scale;
    6423      107655 :     move16();
    6424             : 
    6425             :     Word16 tmp, exp, tmp1;
    6426             :     Word32 L_tmp;
    6427             : 
    6428             : 
    6429      107655 :     tmp = shl( m, 1 );
    6430      107655 :     lsf_map[0] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 0] );
    6431      107655 :     move16();
    6432      107655 :     lsf_map[1] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 1] );
    6433      107655 :     move16();
    6434      107655 :     lsf_map[2] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 2] );
    6435      107655 :     move16();
    6436      107655 :     lsf_map[3] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 3] );
    6437      107655 :     move16();
    6438      107655 :     lsf_map[4] = sub( tmp, lsf_q[NUM_MAP_LSF - 1 - 4] );
    6439      107655 :     move16();
    6440             : 
    6441             : 
    6442      107655 :     IF( GT_16( m, MAX_LSF_FX_2 ) )
    6443             :     {
    6444       20133 :         offset = lsf_map[0];
    6445       20133 :         move16();
    6446       20133 :         exp = norm_s( m );
    6447       20133 :         tmp = div_s( shl( 1, sub( 14, exp ) ), m );
    6448       20133 :         L_tmp = L_mult( sub( MAX_LSF_FX, m ), tmp );
    6449       20133 :         scale = round_fx( L_shl( L_tmp, add( exp, 1 ) ) );
    6450             : 
    6451      120798 :         FOR( i = 0; i < NUM_MAP_LSF; i++ )
    6452             :         {
    6453      100665 :             tmp = mult_r( sub( lsf_map[i], offset ), scale );
    6454      100665 :             lsf_map[i] = add( tmp, offset );
    6455      100665 :             move16();
    6456             :         }
    6457             :     }
    6458             : 
    6459      107655 :     last_q_lsf = lsf_q[NUM_Q_LSF - 1];
    6460      107655 :     move16();
    6461      107655 :     scale = sub( MAX_LSF_FX, last_q_lsf );
    6462             : 
    6463      538275 :     FOR( i = 0; i < NUM_LSF_GRIDS; i++ )
    6464             :     {
    6465     2583720 :         FOR( j = 0; j < NUM_MAP_LSF; j++ )
    6466             :         {
    6467     2153100 :             grid[i][j] = add( mult_r( lsf_grid_fx[i][j], scale ), last_q_lsf );
    6468     2153100 :             move16();
    6469             :         }
    6470             :     }
    6471             : 
    6472      107655 :     D_best = L_add( MAX_32, 0 );
    6473      538275 :     FOR( i = 0; i < NUM_LSF_GRIDS; i++ )
    6474             :     {
    6475      430620 :         D = L_deposit_l( 0 );
    6476     2583720 :         FOR( j = 0; j < NUM_MAP_LSF; j++ )
    6477             :         {
    6478             :             /*lsf_t[j] = (1 - grid_smoothing[j])*lsf_map[j] + grid_smoothing[j]*grid[i][j]; */
    6479     2153100 :             tmp = sub( 32767, grid_smoothing_fx[j] );
    6480     2153100 :             tmp = mult_r( tmp, lsf_map[j] );
    6481     2153100 :             tmp1 = mult_r( grid_smoothing_fx[j], grid[i][j] );
    6482     2153100 :             lsf_t[j] = add( tmp, tmp1 );
    6483     2153100 :             move16();
    6484             : 
    6485     2153100 :             tmp = sub( lsf_t[j], lsf[NUM_Q_LSF + j] );
    6486     2153100 :             D = L_mac0( D, tmp, tmp );
    6487             : 
    6488             :             /* D += (lsf_t[j] - lsf[NUM_Q_LSF + j])*(lsf_t[j] - lsf[NUM_Q_LSF + j]); */
    6489             :         }
    6490      430620 :         IF( LT_32( D, D_best ) )
    6491             :         {
    6492      161747 :             Copy( lsf_t, lsf_smooth, NUM_MAP_LSF );
    6493      161747 :             D_best = L_add( D, 0 );
    6494      161747 :             I_best = i;
    6495      161747 :             move16();
    6496             :         }
    6497             :     }
    6498             : 
    6499      645930 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    6500             :     {
    6501      538275 :         lsf_q[NUM_Q_LSF + i] = lsf_smooth[i];
    6502      538275 :         move16();
    6503             :     }
    6504             : 
    6505      107655 :     return I_best;
    6506             : }
    6507             : 
    6508             : /*-------------------------------------------------------------------*
    6509             :  * gainFrSmooth_En_fx()
    6510             :  *
    6511             :  * Gain frame smoothing and attenuation control
    6512             :  *-------------------------------------------------------------------*/
    6513        2639 : static void gainFrSmooth_En_fx( Encoder_State *st_fx,
    6514             :                                 Word16 *shb_frame_fx,
    6515             :                                 const Word16 *lpc_shb_fx,
    6516             :                                 const Word16 *lsp_shb_fx,
    6517             :                                 Word16 *MA_lsp_shb_spacing,
    6518             :                                 Word16 *frGainAttenuate,
    6519             :                                 Word16 *frGainSmoothEn )
    6520             : {
    6521             :     Word16 temp_shb_frame[L_FRAME16k + L_SHB_LAHEAD];
    6522             :     Word32 lsp_slow_evol_rate, lsp_fast_evol_rate;
    6523             :     Word16 lsp_spacing;
    6524             : 
    6525             :     Word32 tempQ31;
    6526             :     Word16 tempQ15_1, tempQ15_2;
    6527             :     Word16 i;
    6528        2639 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    6529             : 
    6530             :     /* inits */
    6531        2639 :     *frGainAttenuate = 0;
    6532        2639 :     move16();
    6533        2639 :     *frGainSmoothEn = 0;
    6534        2639 :     move16();
    6535        2639 :     *MA_lsp_shb_spacing = 16384;
    6536        2639 :     move16();
    6537        2639 :     lsp_spacing = lsp_shb_fx[0];
    6538        2639 :     move16();
    6539             : 
    6540             :     /* estimate the mean square error in lsps from current frame to past frames */
    6541        2639 :     tempQ15_1 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_slow_interpl_fx[0] );
    6542        2639 :     tempQ15_2 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_fast_interpl_fx[0] );
    6543        2639 :     lsp_slow_evol_rate = L_mult( tempQ15_1, tempQ15_1 );
    6544        2639 :     lsp_fast_evol_rate = L_mult( tempQ15_2, tempQ15_2 );
    6545             : 
    6546             :     /* update the slow and fast lsp interp for next frame */
    6547        2639 :     tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[0], 22937 );
    6548        2639 :     hBWE_TD->lsp_shb_slow_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 9830 );
    6549        2639 :     move16();
    6550        2639 :     tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[0], 9830 );
    6551        2639 :     hBWE_TD->lsp_shb_fast_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 22937 );
    6552        2639 :     move16();
    6553             : 
    6554       26390 :     FOR( i = 1; i < LPC_SHB_ORDER; i++ )
    6555             :     {
    6556       23751 :         tempQ15_1 = sub( lsp_shb_fx[i], lsp_shb_fx[i - 1] );
    6557       23751 :         lsp_spacing = s_min( lsp_spacing, tempQ15_1 );
    6558             : 
    6559             :         /* estimate the mean square error in lsps from current frame to past frames */
    6560       23751 :         tempQ15_1 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_slow_interpl_fx[i] );
    6561       23751 :         tempQ15_2 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_fast_interpl_fx[i] );
    6562       23751 :         lsp_slow_evol_rate = L_mac( lsp_slow_evol_rate, tempQ15_1, tempQ15_1 );
    6563       23751 :         lsp_fast_evol_rate = L_mac( lsp_fast_evol_rate, tempQ15_2, tempQ15_2 );
    6564             : 
    6565             :         /* update the slow and fast interpolation lsps for next frame */
    6566       23751 :         tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[i], 22937 );
    6567       23751 :         hBWE_TD->lsp_shb_slow_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 9830 );
    6568       23751 :         move16();
    6569       23751 :         tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[i], 9830 );
    6570       23751 :         hBWE_TD->lsp_shb_fast_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 22937 );
    6571       23751 :         move16();
    6572             :     }
    6573             : 
    6574        2639 :     test();
    6575        2639 :     test();
    6576        2639 :     IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) && LT_16( lsp_spacing, 262 ) )
    6577             :     {
    6578           0 :         hBWE_TD->lsp_shb_spacing_fx[0] = lsp_spacing;
    6579           0 :         move16();
    6580           0 :         hBWE_TD->lsp_shb_spacing_fx[1] = lsp_spacing;
    6581           0 :         move16();
    6582           0 :         hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
    6583           0 :         move16();
    6584           0 :         hBWE_TD->prev_frGainAtten = 1;
    6585           0 :         move16();
    6586           0 :         set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
    6587             :     }
    6588             : 
    6589             :     /* Estimate the moving average LSP spacing */
    6590        2639 :     tempQ31 = L_mult( hBWE_TD->lsp_shb_spacing_fx[0], 3277 );         /* 0.1f */
    6591        2639 :     tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[1], 6553 ); /* 0.2f */
    6592        2639 :     tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[2], 9830 ); /* 0.3f */
    6593        2639 :     *MA_lsp_shb_spacing = mac_r( tempQ31, lsp_spacing, 13107 );       /* 0.4f */
    6594        2639 :     move16();
    6595             : 
    6596        2639 :     hBWE_TD->lsp_shb_spacing_fx[0] = hBWE_TD->lsp_shb_spacing_fx[1];
    6597        2639 :     move16();
    6598        2639 :     hBWE_TD->lsp_shb_spacing_fx[1] = hBWE_TD->lsp_shb_spacing_fx[2];
    6599        2639 :     move16();
    6600        2639 :     hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
    6601        2639 :     move16();
    6602             : 
    6603        2639 :     test();
    6604        2639 :     test();
    6605        2639 :     test();
    6606        2639 :     IF( ( LT_16( lsp_spacing, 262 ) && ( LT_16( *MA_lsp_shb_spacing, 164 ) || EQ_16( hBWE_TD->prev_frGainAtten, 1 ) ) ) || LE_16( lsp_spacing, 105 ) )
    6607             :     {
    6608           6 :         *frGainAttenuate = 1;
    6609           6 :         move16();
    6610             : 
    6611           6 :         IF( NE_32( st_fx->total_brate, ACELP_24k40 ) )
    6612             :         {
    6613           2 :             Copy( shb_frame_fx, temp_shb_frame, L_FRAME16k + L_SHB_LAHEAD );
    6614           2 :             fir_fx( temp_shb_frame, lpc_shb_fx, shb_frame_fx, hBWE_TD->shb_inv_filt_mem_fx, L_FRAME16k + L_SHB_LAHEAD, LPC_SHB_ORDER, 1, 3 );
    6615             :         }
    6616             :         ELSE
    6617             :         {
    6618           4 :             set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
    6619             :         }
    6620             : 
    6621           6 :         test();
    6622           6 :         if ( LT_32( lsp_slow_evol_rate, INV_1000_Q31 ) && LT_32( lsp_fast_evol_rate, 2147484l /*0.001f Q31*/ ) )
    6623             :         {
    6624           0 :             *frGainSmoothEn = 1;
    6625           0 :             move16();
    6626             :         }
    6627             :     }
    6628        2639 : }
    6629             : 
    6630             : /*-------------------------------------------------------------------*
    6631             :  * gainFrSmooth_En_ivas_fx()
    6632             :  *
    6633             :  * Gain frame smoothing and attenuation control
    6634             :  *-------------------------------------------------------------------*/
    6635      146035 : static void gainFrSmooth_En_ivas_fx( Encoder_State *st_fx,
    6636             :                                      Word16 *shb_frame_fx /* Q_in */,
    6637             :                                      const Word16 *lpc_shb_fx /* Q12 */,
    6638             :                                      const Word16 *lsp_shb_fx /* Q15 */,
    6639             :                                      Word16 *MA_lsp_shb_spacing /* Q15 */,
    6640             :                                      Word16 *frGainAttenuate /* Q0 */,
    6641             :                                      Word16 *frGainSmoothEn /* Q0 */ )
    6642             : {
    6643             :     Word16 temp_shb_frame[L_FRAME16k + L_SHB_LAHEAD];
    6644             :     Word32 lsp_slow_evol_rate, lsp_fast_evol_rate;
    6645             :     Word16 lsp_slow_evol_rate_e, lsp_fast_evol_rate_e;
    6646             :     Word16 lsp_spacing;
    6647             : 
    6648             :     Word32 tempQ31;
    6649             :     Word16 tempQ15_1, tempQ15_2;
    6650             :     Word16 i;
    6651      146035 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    6652             : 
    6653             :     /* inits */
    6654      146035 :     *frGainAttenuate = 0;
    6655      146035 :     move16();
    6656      146035 :     *frGainSmoothEn = 0;
    6657      146035 :     move16();
    6658      146035 :     *MA_lsp_shb_spacing = 16384; /* 0.5f in Q15 */
    6659      146035 :     move16();
    6660      146035 :     lsp_spacing = lsp_shb_fx[0];
    6661      146035 :     move16();
    6662             : 
    6663             :     /* estimate the mean square error in lsps from current frame to past frames */
    6664      146035 :     tempQ15_1 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_slow_interpl_fx[0] );
    6665      146035 :     tempQ15_2 = sub( lsp_shb_fx[0], hBWE_TD->lsp_shb_fast_interpl_fx[0] );
    6666      146035 :     lsp_slow_evol_rate = L_mult( tempQ15_1, tempQ15_1 );
    6667      146035 :     lsp_slow_evol_rate_e = 0;
    6668      146035 :     move16();
    6669      146035 :     lsp_fast_evol_rate = L_mult( tempQ15_2, tempQ15_2 );
    6670      146035 :     lsp_fast_evol_rate_e = 0;
    6671      146035 :     move16();
    6672             : 
    6673             :     /* update the slow and fast lsp interp for next frame */
    6674      146035 :     tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[0], 22937 /* 0.7f in Q15 */ );
    6675      146035 :     hBWE_TD->lsp_shb_slow_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 9830 /* 0.3f in Q15 */ );
    6676      146035 :     move16();
    6677      146035 :     tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[0], 9830 /* 0.3f in Q15 */ );
    6678      146035 :     hBWE_TD->lsp_shb_fast_interpl_fx[0] = mac_r( tempQ31, lsp_shb_fx[0], 22937 /* 0.7f in Q15 */ );
    6679      146035 :     move16();
    6680             : 
    6681     1460350 :     FOR( i = 1; i < LPC_SHB_ORDER; i++ )
    6682             :     {
    6683     1314315 :         tempQ15_1 = sub( lsp_shb_fx[i], lsp_shb_fx[i - 1] );
    6684     1314315 :         lsp_spacing = s_min( lsp_spacing, tempQ15_1 );
    6685             : 
    6686             :         /* estimate the mean square error in lsps from current frame to past frames */
    6687     1314315 :         tempQ15_1 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_slow_interpl_fx[i] );
    6688     1314315 :         tempQ15_2 = sub( lsp_shb_fx[i], hBWE_TD->lsp_shb_fast_interpl_fx[i] );
    6689     1314315 :         lsp_slow_evol_rate = BASOP_Util_Add_Mant32Exp( lsp_slow_evol_rate, lsp_slow_evol_rate_e, L_mult( tempQ15_1, tempQ15_1 ), 0, &lsp_slow_evol_rate_e );
    6690     1314315 :         lsp_fast_evol_rate = BASOP_Util_Add_Mant32Exp( lsp_fast_evol_rate, lsp_fast_evol_rate_e, L_mult( tempQ15_2, tempQ15_2 ), 0, &lsp_fast_evol_rate_e );
    6691             : 
    6692             :         /* update the slow and fast interpolation lsps for next frame */
    6693     1314315 :         tempQ31 = L_mult( hBWE_TD->lsp_shb_slow_interpl_fx[i], 22937 /* 0.7f in Q15 */ );
    6694     1314315 :         hBWE_TD->lsp_shb_slow_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 9830 /* 0.3f in Q15 */ );
    6695     1314315 :         move16();
    6696     1314315 :         tempQ31 = L_mult( hBWE_TD->lsp_shb_fast_interpl_fx[i], 9830 /* 0.3f in Q15 */ );
    6697     1314315 :         hBWE_TD->lsp_shb_fast_interpl_fx[i] = mac_r( tempQ31, lsp_shb_fx[i], 22937 /* 0.7f in Q15 */ );
    6698     1314315 :         move16();
    6699             :     }
    6700             : 
    6701      146035 :     test();
    6702      146035 :     test();
    6703      146035 :     IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) && LT_16( lsp_spacing, 262 /* 0.008f in Q15 */ ) )
    6704             :     {
    6705          93 :         hBWE_TD->lsp_shb_spacing_fx[0] = lsp_spacing;
    6706          93 :         move16();
    6707          93 :         hBWE_TD->lsp_shb_spacing_fx[1] = lsp_spacing;
    6708          93 :         move16();
    6709          93 :         hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
    6710          93 :         move16();
    6711          93 :         hBWE_TD->prev_frGainAtten = 1;
    6712          93 :         move16();
    6713          93 :         set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
    6714             :     }
    6715             : 
    6716             :     /* Estimate the moving average LSP spacing */
    6717      146035 :     tempQ31 = L_mult( hBWE_TD->lsp_shb_spacing_fx[0], 3277 );         /* 0.1f in Q15 */
    6718      146035 :     tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[1], 6553 ); /* 0.2f in Q15 */
    6719      146035 :     tempQ31 = L_mac( tempQ31, hBWE_TD->lsp_shb_spacing_fx[2], 9830 ); /* 0.3f in Q15 */
    6720      146035 :     *MA_lsp_shb_spacing = mac_r( tempQ31, lsp_spacing, 13107 );       /* 0.4f in Q15 */
    6721      146035 :     move16();
    6722             : 
    6723      146035 :     hBWE_TD->lsp_shb_spacing_fx[0] = hBWE_TD->lsp_shb_spacing_fx[1];
    6724      146035 :     move16();
    6725      146035 :     hBWE_TD->lsp_shb_spacing_fx[1] = hBWE_TD->lsp_shb_spacing_fx[2];
    6726      146035 :     move16();
    6727      146035 :     hBWE_TD->lsp_shb_spacing_fx[2] = lsp_spacing;
    6728      146035 :     move16();
    6729             : 
    6730      146035 :     test();
    6731      146035 :     test();
    6732      146035 :     test();
    6733      146035 :     IF( ( LT_16( lsp_spacing, 262 /* 0.008f in Q15 */ ) && ( LT_16( *MA_lsp_shb_spacing, 164 /* 0.005f in Q15 */ ) || EQ_16( hBWE_TD->prev_frGainAtten, 1 ) ) ) || LE_16( lsp_spacing, 105 /* 0.0032f in Q15 */ ) )
    6734             :     {
    6735         112 :         *frGainAttenuate = 1;
    6736         112 :         move16();
    6737             : 
    6738         112 :         IF( NE_32( st_fx->total_brate, ACELP_24k40 ) )
    6739             :         {
    6740         112 :             Copy( shb_frame_fx, temp_shb_frame, L_FRAME16k + L_SHB_LAHEAD );
    6741         112 :             fir_fx( temp_shb_frame, lpc_shb_fx, shb_frame_fx, hBWE_TD->shb_inv_filt_mem_fx, L_FRAME16k + L_SHB_LAHEAD, LPC_SHB_ORDER, 1, 3 );
    6742             :         }
    6743             :         ELSE
    6744             :         {
    6745           0 :             set16_fx( hBWE_TD->shb_inv_filt_mem_fx, 0, LPC_SHB_ORDER );
    6746             :         }
    6747             : 
    6748         112 :         test();
    6749         112 :         if ( ( BASOP_Util_Cmp_Mant32Exp( lsp_slow_evol_rate, lsp_slow_evol_rate_e, INV_1000_Q31, 0 ) < 0 ) && ( BASOP_Util_Cmp_Mant32Exp( lsp_fast_evol_rate, lsp_fast_evol_rate_e, 2147484l /*0.001f in Q31*/, 0 ) < 0 ) )
    6750             :         {
    6751           6 :             *frGainSmoothEn = 1;
    6752           6 :             move16();
    6753             :         }
    6754             :     }
    6755      146035 : }
    6756             : 
    6757             : #define MAXINT32 MAX_32
    6758        3429 : static void first_VQstages_fx(
    6759             :     const Word16 *const *cb,
    6760             :     Word16 u[],       /* i  : vector to be encoded (prediction and mean removed)  */
    6761             :     Word16 *levels,   /* i  : number of levels in each stage                      */
    6762             :     Word16 stagesVQ,  /* i  : number of stages                                    */
    6763             :     Word16 w[],       /* i  : weights                                             */
    6764             :     Word16 N,         /* i  : vector dimension                                    */
    6765             :     Word16 max_inner, /* i  : maximum number of swaps in inner loop               */
    6766             :     Word16 indices_VQstage[] )
    6767             : {
    6768             :     Word16 resid_buf[2 * LSFMBEST * M], *resid[2];
    6769             :     Word32 dist_buf[2 * LSFMBEST], *dist[2], en;
    6770             :     Word32 f_tmp, L_tmp, L_tmp1, *pTmp32;
    6771             :     Word16 Tmp[M], *pTmp, cs;
    6772        3429 :     Word16 *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j,
    6773             :                                                                                   m, s, c, c2, p_max, *indices[2];
    6774        3429 :     move16();
    6775        3429 :     Word16 maxC = LSFMBEST;
    6776        3429 :     move16();
    6777             : 
    6778             :     /*float dd[16];*/
    6779             :     const Word16 *cb_stage, *cbp;
    6780             : 
    6781             :     /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
    6782        3429 :     indices[0] = idx_buf;
    6783        3429 :     move16();
    6784        3429 :     indices[1] = idx_buf + maxC * stagesVQ;
    6785        3429 :     move16();
    6786        3429 :     resid[0] = resid_buf;
    6787        3429 :     move16();
    6788        3429 :     resid[1] = resid_buf + maxC * N;
    6789        3429 :     move16();
    6790        3429 :     dist[0] = dist_buf;
    6791        3429 :     move16();
    6792        3429 :     dist[1] = dist_buf + maxC;
    6793        3429 :     move16();
    6794             : 
    6795        3429 :     set16_fx( idx_buf, 0, ( const Word16 )( 2 * stagesVQ * maxC ) );
    6796        3429 :     set16_fx( parents, 0, maxC );
    6797             : 
    6798             :     /* Set up inital distance vector */
    6799        3429 :     L_tmp = L_deposit_l( 0 );
    6800       24003 :     FOR( j = 0; j < N; j++ )
    6801             :     {
    6802       20574 :         L_tmp1 = L_shl_sat( L_mult0( u[j], w[j] ), 7 ); /*x2.56 + Q8 + Q7 */
    6803       20574 :         L_tmp1 = Mult_32_16( L_tmp1, u[j] );            /*x2.56 + Q15 + x2.56 -Q15 */
    6804       20574 :         L_tmp = L_add( L_tmp, L_tmp1 );                 /*Q0 + x2.56 +x2.56 */
    6805             :     }
    6806        3429 :     set32_fx( dist[1], L_tmp, maxC );
    6807             : 
    6808             :     /* Set up initial error (residual) vectors */
    6809        3429 :     pTmp = resid[1];
    6810       10287 :     FOR( c = 0; c < maxC; c++ )
    6811             :     {
    6812        6858 :         Copy( u, pTmp, N );
    6813        6858 :         pTmp += N;
    6814             :     }
    6815             : 
    6816             :     /*----------------------------------------------------------------*
    6817             :      * LSF quantization
    6818             :      *----------------------------------------------------------------*/
    6819             : 
    6820             :     /* Loop over all stages */
    6821        3429 :     m = 1;
    6822        3429 :     move16();
    6823        6858 :     FOR( s = 0; s < stagesVQ; s++ )
    6824             :     {
    6825             :         /* set codebook pointer to point to first stage */
    6826        3429 :         cbp = cb[s];
    6827             : 
    6828             :         /* save pointer to the beginning of the current stage */
    6829        3429 :         cb_stage = cbp;
    6830             : 
    6831             :         /* swap pointers to parent and current nodes */
    6832        3429 :         pTmp_short = indices[0];
    6833        3429 :         indices[0] = indices[1];
    6834        3429 :         indices[1] = pTmp_short;
    6835             : 
    6836        3429 :         pTmp = resid[0];
    6837        3429 :         resid[0] = resid[1];
    6838        3429 :         resid[1] = pTmp;
    6839             : 
    6840        3429 :         pTmp32 = dist[0];
    6841        3429 :         dist[0] = dist[1];
    6842        3429 :         dist[1] = pTmp32;
    6843             : 
    6844             :         /* p_max points to maximum distortion node (worst of best) */
    6845        3429 :         p_max = 0;
    6846        3429 :         move16();
    6847             : 
    6848             :         /* set distortions to a large value */
    6849        3429 :         set32_fx( dist[1], MAXINT32, maxC );
    6850             : 
    6851       58293 :         FOR( j = 0; j < levels[s]; j++ )
    6852             :         {
    6853             :             /* compute weighted codebook element and its energy */
    6854      384048 :             FOR( c2 = 0; c2 < N; c2++ )
    6855             :             {
    6856      329184 :                 Tmp[c2] = shl( mult( w[c2], cbp[c2] ), 2 ); /* Q8 + x2.56 -Q15 +Q2 */
    6857      329184 :                 move16();
    6858             :             }
    6859             : 
    6860       54864 :             en = L_mult( cbp[0], Tmp[0] );
    6861             : 
    6862      329184 :             FOR( c2 = 1; c2 < N; c2++ )
    6863             :             {
    6864      274320 :                 en = L_mac( en, cbp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-5 +Q1 */
    6865             :             }
    6866       54864 :             cbp += N;
    6867       54864 :             move16();
    6868             : 
    6869             :             /* iterate over all parent nodes */
    6870      109728 :             FOR( c = 0; c < m; c++ )
    6871             :             {
    6872       54864 :                 pTmp = &resid[0][c * N];
    6873       54864 :                 L_tmp = L_mult( pTmp[0], Tmp[0] );
    6874      329184 :                 FOR( c2 = 1; c2 < N; c2++ )
    6875             :                 {
    6876      274320 :                     L_tmp = L_mac( L_tmp, pTmp[c2], Tmp[c2] ); /* */
    6877             :                 }
    6878             : 
    6879       54864 :                 L_tmp = L_add( dist[0][c], L_sub( en, L_shl( L_tmp, 1 ) ) );
    6880             : 
    6881       54864 :                 IF( LE_32( L_tmp, dist[1][p_max] ) )
    6882             :                 {
    6883             :                     /* replace worst */
    6884       20692 :                     dist[1][p_max] = L_tmp;
    6885       20692 :                     move32();
    6886       20692 :                     indices[1][p_max * stagesVQ + s] = j;
    6887       20692 :                     move16();
    6888       20692 :                     parents[p_max] = c;
    6889       20692 :                     move16();
    6890             : 
    6891             :                     /* limit number of times inner loop is entered */
    6892       20692 :                     IF( LT_16( counter, max_inner ) )
    6893             :                     {
    6894       20692 :                         counter = add( counter, 1 );
    6895       20692 :                         IF( LT_16( counter, max_inner ) )
    6896             :                         {
    6897             :                             /* find new worst */
    6898       20692 :                             p_max = maximum_32_fx( dist[1], maxC, &f_tmp );
    6899             :                         }
    6900             :                         ELSE
    6901             :                         {
    6902             :                             /* find minimum distortion */
    6903           0 :                             p_max = minimum_32_fx( dist[1], maxC, &f_tmp );
    6904             :                         }
    6905             :                     }
    6906             :                 }
    6907             :             }
    6908             :         }
    6909             : 
    6910             :         /*------------------------------------------------------------*
    6911             :          * Compute error vectors for each node
    6912             :          *------------------------------------------------------------*/
    6913        3429 :         cs = 0;
    6914        3429 :         move16();
    6915       10287 :         FOR( c = 0; c < maxC; c++ )
    6916             :         {
    6917             :             /* subtract codebook entry from the residual vector of the parent node */
    6918        6858 :             pTmp = resid[1] + c * N;
    6919             : 
    6920        6858 :             Copy( resid[0] + parents[c] * N, pTmp, N );
    6921        6858 :             Vr_subt( pTmp, cb_stage + ( indices[1][cs + s] ) * N, pTmp, N );
    6922             : 
    6923             :             /* get indices that were used for parent node */
    6924        6858 :             Copy( indices[0] + parents[c] * stagesVQ, indices[1] + cs, s );
    6925        6858 :             cs = add( cs, stagesVQ );
    6926             :         }
    6927             : 
    6928        3429 :         m = maxC;
    6929        3429 :         move16();
    6930             :     }
    6931             : 
    6932        3429 :     Copy( indices[1], indices_VQstage, maxC * stagesVQ );
    6933             : 
    6934        3429 :     return;
    6935             : }
    6936             : 
    6937       20574 : static Word16 dotp_loc(
    6938             :     const Word16 x[], /* i  : vector x[]                    */
    6939             :     const Word32 y[], /* i  : vector y[]                    */
    6940             :     const Word16 n    /* i  : vector length                 */
    6941             : )
    6942             : {
    6943             :     Word16 i;
    6944             :     Word32 suma;
    6945       20574 :     Word16 guarded_bits = find_guarded_bits_fx( n );
    6946       20574 :     suma = L_shr( Mpy_32_16_1( y[0], x[0] ), guarded_bits );
    6947             : 
    6948      164592 :     FOR( i = 1; i < n; i++ )
    6949             :     {
    6950      144018 :         suma = L_add( suma, L_shr( Mpy_32_16_1( y[i], x[i] ), guarded_bits ) );
    6951             :     }
    6952       20574 :     suma = L_shl_sat( suma, guarded_bits );
    6953             : 
    6954       20574 :     return extract_h( suma );
    6955             : }
    6956             : 
    6957        3429 : static void LVQQuant_BWE_LSF_fx(
    6958             :     BSTR_ENC_HANDLE hBstr,     /* i/o: encoder bitstream handle     */
    6959             :     const Word16 lsf_shb_fx[], /* i  : unquanitzed LSFs         Q15 */
    6960             :     Word16 Q_lsfs_fx[],        /* o  : quanitzed LSFs           Q15 */
    6961             :     Word16 nbits )
    6962             : {
    6963             : 
    6964             :     Word16 lsf_fx[LPC_SHB_ORDER];
    6965             :     Word16 lsf_q_fx[LPC_SHB_ORDER];
    6966        3429 :     Word16 i, c2, dim1 = 6;
    6967        3429 :     move16();
    6968             :     Word16 levels[1], numbits_VQ;
    6969             :     Word16 c2_fx_temp;
    6970             : 
    6971        3429 :     Word16 w_fx[8] = { 5120, 5120, 2048, 2048, 2048, 1024, 1024, 1024 }; /* Q10 */
    6972        3429 :     move16();
    6973        3429 :     move16();
    6974        3429 :     move16();
    6975        3429 :     move16();
    6976        3429 :     move16();
    6977        3429 :     move16();
    6978        3429 :     move16();
    6979        3429 :     move16();
    6980             :     Word16 cand_fx[LPC_SHB_ORDER], quant_fx[LPC_SHB_ORDER], e_fx[2], ftmp_fx;
    6981             :     const Word16 *cb_stage_fx;
    6982             :     Word16 indices_firstVQ[LSFMBEST];
    6983             : 
    6984             :     Word16 Idx, idx_lead, idx_scale;
    6985             :     UWord32 LVQ_index;
    6986             :     Word16 dd_fx[LATTICE_DIM], cv_out_fx[LATTICE_DIM];
    6987             :     Word16 bits_lvq, predictor_bits, empty_bits;
    6988             : 
    6989        3429 :     empty_bits = 0; /* This is the difference to 21 bits, if we want lower number of bits */
    6990        3429 :     move16();
    6991        3429 :     nbits = sub( nbits, empty_bits ); /* THIS IS TO SIMULATE DIFFERENT NUMBER OF input BITS - should be removed */
    6992        3429 :     c2 = sub( NUM_BITS_SHB_MSLVQ, nbits );
    6993        3429 :     numbits_VQ = config_LSF_BWE[c2 * 3];
    6994        3429 :     levels[0] = config_LSF_BWE[c2 * 3 + 1];
    6995        3429 :     move16();
    6996        3429 :     predictor_bits = config_LSF_BWE[c2 * 3 + 2];
    6997        3429 :     IF( LT_16( nbits, 19 ) )
    6998             :     {
    6999           0 :         cb_stage_fx = cb_LSF_BWE_fx[1];
    7000             :     }
    7001             :     ELSE
    7002             :     {
    7003        3429 :         cb_stage_fx = cb_LSF_BWE_fx[0];
    7004             :     }
    7005             : 
    7006       37719 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    7007             :     {
    7008       34290 :         lsf_fx[i] = sub( 16384 /* 0.5f in Q15 */, lsf_shb_fx[LPC_SHB_ORDER - 1 - i] );
    7009       34290 :         move16();
    7010             :     }
    7011             : 
    7012             :     /* remove mean */
    7013        3429 :     v_sub_16( lsf_fx, SHB_LSF_mean_fx, Q_lsfs_fx, LPC_SHB_ORDER );
    7014             : 
    7015             :     /* 6D multi-stage 6bit coding */
    7016        3429 :     first_VQstages_fx( &cb_stage_fx, Q_lsfs_fx, levels, 1, w_fx, dim1, 3000, indices_firstVQ );
    7017             : 
    7018             : 
    7019             :     /* 8D MSLVQ coding */
    7020        3429 :     bits_lvq = sub( sub( nbits, numbits_VQ ), predictor_bits );
    7021        3429 :     Copy( SHB_LSF_mean_fx, cand_fx, LPC_SHB_ORDER );
    7022             : 
    7023        3429 :     Idx = indices_firstVQ[0];
    7024        3429 :     move16();
    7025             : 
    7026        3429 :     v_add_16( cand_fx, cb_stage_fx + Idx * dim1, cand_fx, dim1 );
    7027             : 
    7028             :     /* LVQ quantization */
    7029        3429 :     Copy( cand_fx, quant_fx, LPC_SHB_ORDER );
    7030             : 
    7031        3429 :     v_sub_16( lsf_fx, cand_fx, dd_fx, LATTICE_DIM );
    7032             : 
    7033        3429 :     mslvq_ivas_16( dd_fx, quant_fx, cv_out_fx, &idx_lead, &idx_scale, w_fx, 0, ( nbits < 19 ), bits_lvq );
    7034             : 
    7035        3429 :     IF( EQ_16( idx_scale, -1 ) )
    7036             :     {
    7037           0 :         set16_fx( cv_out_fx, 0, LATTICE_DIM );
    7038             :     }
    7039        3429 :     v_add_16( cand_fx, quant_fx, cand_fx, LATTICE_DIM );
    7040             : 
    7041             :     /* arrange the LSF candidate vector prior to selection to an ascending order*/
    7042        3429 :     v_sort( cand_fx, 0, LPC_SHB_ORDER - 1 );
    7043             : 
    7044        3429 :     set16_fx( Q_lsfs_fx, 0, LPC_SHB_ORDER );
    7045             :     /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
    7046             : 
    7047        3429 :     v_add_16( Q_lsfs_fx, cb_stage_fx + Idx * dim1, Q_lsfs_fx, dim1 );
    7048             : 
    7049             :     /* write VQ indexes */
    7050        3429 :     push_indice( hBstr, IND_SHB_LSF, Idx, numbits_VQ );
    7051             : 
    7052             :     /* write MSLVQ index */
    7053        3429 :     LVQ_index = index_lvq_SHB_fx( idx_lead, idx_scale, bits_lvq, cv_out_fx, ( nbits < 19 ) );
    7054             : 
    7055        3429 :     IF( GT_16( bits_lvq, 15 ) )
    7056             :     {
    7057           0 :         push_indice( hBstr, IND_SHB_LSF, (Word16) ( ( LVQ_index ) & ( 0xffff >> 1 ) ), 15 );
    7058           0 :         push_indice( hBstr, IND_SHB_LSF, (Word16) ( ( LVQ_index ) >> 15 ) & ( 0xffff >> 1 ), bits_lvq - 15 );
    7059             :     }
    7060             :     ELSE
    7061             :     {
    7062        3429 :         push_indice( hBstr, IND_SHB_LSF, (Word16) LVQ_index, bits_lvq );
    7063             :     }
    7064             : 
    7065        3429 :     v_add_16( Q_lsfs_fx, quant_fx, lsf_q_fx, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
    7066             : 
    7067             :     /* predict last 2 components */
    7068        3429 :     IF( predictor_bits == 0 )
    7069             :     {
    7070           0 :         lsf_q_fx[LATTICE_DIM] = dotp_loc( lsf_q_fx, LastCoefPred_0bit_fx, LATTICE_DIM );
    7071           0 :         move16();
    7072           0 :         lsf_q_fx[LATTICE_DIM + 1] = dotp_loc( lsf_q_fx, &LastCoefPred_0bit_fx[LATTICE_DIM + 1], LATTICE_DIM );
    7073           0 :         move16();
    7074             :     }
    7075             :     ELSE
    7076             :     {
    7077        3429 :         v_sub_16( &lsf_fx[LATTICE_DIM], &SHB_LSF_mean_fx[LATTICE_DIM], &lsf_q_fx[LATTICE_DIM], 2 );
    7078             : 
    7079       10287 :         FOR( i = 0; i < 2; i++ )
    7080             :         {
    7081        6858 :             ftmp_fx = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * i], LATTICE_DIM ) - lsf_q_fx[LATTICE_DIM];
    7082             : 
    7083        6858 :             e_fx[i] = mult( ftmp_fx, ftmp_fx );
    7084        6858 :             move16();
    7085             : 
    7086        6858 :             ftmp_fx = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * i + LATTICE_DIM + 1], LATTICE_DIM ) - lsf_q_fx[LATTICE_DIM + 1];
    7087             : 
    7088        6858 :             e_fx[i] = add_sat( e_fx[i], mult( ftmp_fx, ftmp_fx ) );
    7089        6858 :             move16();
    7090             :         }
    7091             : 
    7092        3429 :         c2_fx_temp = minimum_fx( e_fx, 2, &ftmp_fx );
    7093             : 
    7094        3429 :         lsf_q_fx[LATTICE_DIM] = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * c2_fx_temp], LATTICE_DIM );
    7095        3429 :         move16();
    7096             : 
    7097        3429 :         lsf_q_fx[LATTICE_DIM + 1] = dotp_loc( lsf_q_fx, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * c2_fx_temp + LATTICE_DIM + 1], LATTICE_DIM );
    7098        3429 :         move16();
    7099             : 
    7100        3429 :         push_indice( hBstr, IND_SHB_LSF, c2, 1 );
    7101             :     }
    7102             : 
    7103        3429 :     if ( empty_bits > 0 )
    7104             :     {
    7105           0 :         push_indice( hBstr, IND_SHB_LSF, 0, empty_bits );
    7106             :     }
    7107             : 
    7108        3429 :     v_add_16( SHB_LSF_mean_fx, lsf_q_fx, lsf_q_fx, LPC_SHB_ORDER );
    7109             : 
    7110        3429 :     v_sort( lsf_q_fx, 0, LPC_SHB_ORDER - 1 );
    7111             : 
    7112       37719 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    7113             :     {
    7114       34290 :         Q_lsfs_fx[i] = sub_sat( 16384 /* 0.5f in Q15 */, lsf_q_fx[LPC_SHB_ORDER - 1 - i] );
    7115       34290 :         move16();
    7116             :     }
    7117             : 
    7118        3429 :     return;
    7119             : }
    7120             : 
    7121             : /*-------------------------------------------------------------------*
    7122             :  * Quant_BWE_LSF()
    7123             :  *
    7124             :  * Quantize super highband spectral envolope
    7125             :  *-------------------------------------------------------------------*/
    7126             : 
    7127        2639 : static void Quant_BWE_LSF_fx(
    7128             : 
    7129             :     Encoder_State *st_fx,   /* i/o: encoder state structure */
    7130             :     const Word16 lsf_shb[], /* i : unquanitzed LSFs */
    7131             :     Word16 Q_lsfs[]         /* o : quanitzed LSFs */
    7132             : )
    7133             : {
    7134             :     Word16 lsf[LPC_SHB_ORDER];
    7135             :     Word16 lsf_q[LPC_SHB_ORDER];
    7136             :     Word16 lsf_idx[NUM_Q_LSF];
    7137             :     Word16 i;
    7138             :     Word16 m_idx;
    7139             :     Word16 m;
    7140             :     Word16 grid_idx;
    7141        2639 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    7142        2639 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    7143             : 
    7144             : 
    7145       29029 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    7146             :     {
    7147       26390 :         lsf[i] = sub( 16384, lsf_shb[LPC_SHB_ORDER - 1 - i] );
    7148       26390 :         move16();
    7149             :     }
    7150             : 
    7151        2639 :     Quant_lower_LSF_fx( lsf, lsf_q, lsf_idx );
    7152             :     /* i: lsf in Q15 */
    7153             :     /* o: lsf_q in Q15 */
    7154             : 
    7155       15834 :     FOR( i = 0; i < NUM_Q_LSF; i++ )
    7156             :     {
    7157       13195 :         hBWE_TD->lsf_idx[i] = lsf_idx[i];
    7158       13195 :         move16();
    7159       13195 :         IF( NE_16( st_fx->codec_mode, MODE2 ) )
    7160             :         {
    7161        7075 :             push_indice( hBstr, IND_SHB_LSF, lsf_idx[i], lsf_q_num_bits[i] );
    7162             :         }
    7163             :     }
    7164             : 
    7165        2639 :     m_idx = Quant_mirror_point_fx( lsf, lsf_q, &m );
    7166             : 
    7167        2639 :     hBWE_TD->m_idx = m_idx;
    7168        2639 :     move16();
    7169        2639 :     IF( NE_16( st_fx->codec_mode, MODE2 ) )
    7170             :     {
    7171        1415 :         push_indice( hBstr, IND_SHB_MIRROR, m_idx, MIRROR_POINT_BITS );
    7172             :     }
    7173             : 
    7174        2639 :     grid_idx = Find_LSF_grid_fx( lsf, lsf_q, m );
    7175             : 
    7176        2639 :     hBWE_TD->grid_idx = grid_idx;
    7177        2639 :     IF( NE_16( st_fx->codec_mode, MODE2 ) )
    7178             :     {
    7179        1415 :         push_indice( hBstr, IND_SHB_GRID, grid_idx, NUM_LSF_GRID_BITS );
    7180             :     }
    7181             : 
    7182       29029 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    7183             :     {
    7184       26390 :         Q_lsfs[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
    7185       26390 :         move16();
    7186             :     }
    7187             : 
    7188        2639 :     return;
    7189             : }
    7190             : 
    7191             : 
    7192      108445 : static void Quant_BWE_LSF_ivas_fx(
    7193             :     BSTR_ENC_HANDLE hBstr,     /* i/o: encoder bitstream handle     */
    7194             :     TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle           */
    7195             :     const Word16 codec_mode,   /* i  : codec mode                   */
    7196             :     const Word16 lsf_shb_fx[], /* i : unquanitzed LSFs          Q15 */
    7197             :     Word16 Q_lsfs_fx[],        /* o : quanitzed LSFs            Q15 */
    7198             :     const Word32 extl_brate    /* i  : extension layer bit rate     */
    7199             : )
    7200             : {
    7201             :     Word16 lsf_fx[LPC_SHB_ORDER];
    7202             :     Word16 lsf_q_fx[LPC_SHB_ORDER];
    7203             :     Word16 lsf_idx[NUM_Q_LSF];
    7204             :     Word16 i;
    7205             :     Word16 m_idx;
    7206             :     Word16 m_fx;
    7207             :     Word16 grid_idx;
    7208             : 
    7209      108445 :     IF( EQ_32( extl_brate, SWB_TBE_1k75 ) )
    7210             :     {
    7211        3429 :         LVQQuant_BWE_LSF_fx( hBstr, lsf_shb_fx, Q_lsfs_fx, NUM_BITS_SHB_MSLVQ );
    7212             :     }
    7213             :     ELSE
    7214             :     {
    7215     1155176 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    7216             :         {
    7217     1050160 :             lsf_fx[i] = sub( 16384 /* 0.5f in Q15 */, lsf_shb_fx[LPC_SHB_ORDER - 1 - i] );
    7218     1050160 :             move16();
    7219             :         }
    7220      105016 :         Quant_lower_LSF_fx( lsf_fx, lsf_q_fx, lsf_idx );
    7221             : 
    7222      630096 :         FOR( i = 0; i < NUM_Q_LSF; i++ )
    7223             :         {
    7224      525080 :             IF( EQ_16( codec_mode, MODE2 ) )
    7225             :             {
    7226           0 :                 hBWE_TD->lsf_idx[i] = lsf_idx[i];
    7227           0 :                 move16();
    7228             :             }
    7229             :             ELSE
    7230             :             {
    7231      525080 :                 push_indice( hBstr, IND_SHB_LSF, lsf_idx[i], lsf_q_num_bits[i] );
    7232             :             }
    7233             :         }
    7234             : 
    7235      105016 :         m_idx = Quant_mirror_point_fx( lsf_fx, lsf_q_fx, &m_fx );
    7236             : 
    7237      105016 :         IF( EQ_16( codec_mode, MODE2 ) )
    7238             :         {
    7239           0 :             hBWE_TD->m_idx = m_idx;
    7240           0 :             move16();
    7241             :         }
    7242             :         ELSE
    7243             :         {
    7244      105016 :             push_indice( hBstr, IND_SHB_MIRROR, m_idx, MIRROR_POINT_BITS );
    7245             :         }
    7246             : 
    7247      105016 :         grid_idx = Find_LSF_grid_fx( lsf_fx, lsf_q_fx, m_fx );
    7248             : 
    7249      105016 :         IF( EQ_16( codec_mode, MODE2 ) )
    7250             :         {
    7251           0 :             hBWE_TD->grid_idx = grid_idx;
    7252           0 :             move16();
    7253             :         }
    7254             :         ELSE
    7255             :         {
    7256      105016 :             push_indice( hBstr, IND_SHB_GRID, grid_idx, NUM_LSF_GRID_BITS );
    7257             :         }
    7258             : 
    7259     1155176 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    7260             :         {
    7261     1050160 :             Q_lsfs_fx[i] = sub( 16384 /* 0.5f in Q15 */, lsf_q_fx[LPC_SHB_ORDER - 1 - i] );
    7262     1050160 :             move16();
    7263             :         }
    7264             :     }
    7265             : 
    7266      108445 :     return;
    7267             : }
    7268             : 
    7269             : /*-------------------------------------------------------------------*
    7270             :  * fb_tbe_enc()
    7271             :  *
    7272             :  * FB TBE encoder, 14(resp. 15.5) - 20 kHz band encoding module
    7273             :  *-------------------------------------------------------------------*/
    7274             : 
    7275           0 : void fb_tbe_enc_fx(
    7276             :     Encoder_State *st,        /* i/o: encoder state structure                 */
    7277             :     const Word16 new_input[], /* i  : input speech at 48 kHz sample rate      */
    7278             :     const Word16 fb_exc[],    /* i  : FB excitation from the SWB part         */
    7279             :     Word16 Q_fb_exc )
    7280             : {
    7281             :     Word16 ratio;
    7282             :     Word16 tmp_vec[L_FRAME48k];
    7283             :     Word16 idxGain;
    7284             :     Word16 input_fhb_buffer[L_FRAME48k + 4];
    7285           0 :     Word16 *input_fhb = &input_fhb_buffer[0] + 4;
    7286             :     Word16 Sample_Delay_HP;
    7287             :     Word32 fb_exc_energy, temp2;
    7288             :     Word32 L_tmp;
    7289             :     Word16 tmp, tmp1, tmp2, exp, exp2, exp_norm;
    7290             :     Word16 s_max_value, exp_temp, i;
    7291           0 :     TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
    7292             : 
    7293           0 :     s_max_value = 0;
    7294           0 :     FOR( i = 0; i < L_FRAME48k; i++ )
    7295             :     {
    7296           0 :         s_max_value = s_max( s_max_value, abs_s( new_input[i] ) );
    7297             :     }
    7298           0 :     exp_temp = norm_s( s_max_value );
    7299           0 :     if ( s_max_value == 0 )
    7300             :     {
    7301           0 :         exp_temp = 15;
    7302           0 :         move16();
    7303             :     }
    7304           0 :     exp_temp = sub( exp_temp, 1 );
    7305             : 
    7306           0 :     Copy_Scale_sig( new_input, input_fhb, L_FRAME48k, exp_temp );
    7307             : 
    7308           0 :     elliptic_bpf_48k_generic_fx(
    7309           0 :         st->element_mode,
    7310             :         0, // IsUpsampled3
    7311           0 :         input_fhb, &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
    7312           0 :     Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2;
    7313             : 
    7314           0 :     IF( NE_16( st->last_extl, FB_TBE ) )
    7315             :     {
    7316           0 :         set16_fx( hBWE_TD->old_input_fhb_fx, 0, Sample_Delay_HP );
    7317           0 :         hBWE_TD->old_input_fhb_fx_Q = 0;
    7318           0 :         move16();
    7319           0 :         set16_fx( tmp_vec, 0, L_FRAME16k );
    7320             :     }
    7321           0 :     Copy_Scale_sig( hBWE_TD->old_input_fhb_fx, hBWE_TD->old_input_fhb_fx, Sample_Delay_HP, sub( exp_temp, hBWE_TD->old_input_fhb_fx_Q ) );
    7322           0 :     hBWE_TD->old_input_fhb_fx_Q = exp_temp;
    7323           0 :     Copy( hBWE_TD->old_input_fhb_fx, input_fhb, Sample_Delay_HP );
    7324           0 :     Copy( tmp_vec, input_fhb + Sample_Delay_HP, L_FRAME48k - Sample_Delay_HP );
    7325           0 :     Copy( tmp_vec + L_FRAME48k - Sample_Delay_HP, hBWE_TD->old_input_fhb_fx, Sample_Delay_HP );
    7326           0 :     temp2 = sum2_fx_mod( input_fhb, L_FRAME48k / 2 );                                                                                              /* Q11 */
    7327           0 :     temp2 = L_add_sat( temp2, L_shl_sat( hBWE_TD->prev_fb_energy_fx, sub( sub( add( exp_temp, exp_temp ), 6 ), hBWE_TD->prev_fb_energy_fx_Q ) ) ); /* Q11 */
    7328           0 :     hBWE_TD->prev_fb_energy_fx = sum2_fx_mod( input_fhb + L_FRAME48k / 2, L_FRAME48k / 2 );                                                        /*Q11*/
    7329           0 :     hBWE_TD->prev_fb_energy_fx_Q = sub( add( exp_temp, exp_temp ), 6 );
    7330           0 :     fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k ); /* Q(2*Q_fb_exc+1 -7) */
    7331             : 
    7332             :     /*ratio = (float) sqrt( temp2 / fb_exc_energy );*/
    7333           0 :     L_tmp = L_max( 1, temp2 ); /*Q6*/
    7334           0 :     exp = norm_l( L_tmp );
    7335           0 :     tmp = extract_h( L_shl( L_tmp, exp ) );
    7336           0 :     exp = sub( sub( 31, sub( add( exp_temp, exp_temp ), 8 ) ), exp ); /* in Q15 (L_tmp in Q6)*/
    7337             : 
    7338           0 :     exp2 = norm_l( fb_exc_energy );
    7339           0 :     tmp2 = extract_h( L_shl( fb_exc_energy, exp2 ) );
    7340           0 :     tmp1 = sub( add( Q_fb_exc, Q_fb_exc ), 8 ); /*1 - 9*/
    7341           0 :     exp2 = sub( sub( 31, tmp1 ), exp2 );        /* in Q15 (L_tmp in Q6)*/
    7342             : 
    7343           0 :     exp = sub( exp2, exp ); /* Denormalize and substract */
    7344           0 :     IF( GT_16( tmp2, tmp ) )
    7345             :     {
    7346           0 :         tmp2 = shr( tmp2, 1 );
    7347           0 :         exp = add( exp, 1 );
    7348             :     }
    7349           0 :     IF( 0 != tmp )
    7350             :     {
    7351           0 :         tmp = div_s( tmp2, tmp );
    7352           0 :         L_tmp = L_deposit_h( tmp );
    7353           0 :         L_tmp = Isqrt_lc( L_tmp, &exp );                          /*Q(31-exp)*/
    7354           0 :         L_tmp = L_max( L_shr_sat( L_tmp, sub( 31, exp ) ), 0x1 ); /* Q0 */
    7355             :     }
    7356             :     ELSE
    7357             :     {
    7358           0 :         L_tmp = 0;
    7359             :     }
    7360             : 
    7361             :     /* idxGain = (short)( log2_f ((float)ratio) + 0.5f );
    7362             :        idxGain = max( 0, min(15,idxGain) ); */
    7363           0 :     ratio = 0;
    7364           0 :     move16();
    7365           0 :     IF( GE_32( L_tmp, 32768 ) )
    7366             :     {
    7367           0 :         idxGain = 15;
    7368           0 :         move16();
    7369             :     }
    7370             :     ELSE
    7371             :     {
    7372           0 :         ratio = extract_l( L_tmp );
    7373           0 :         exp_norm = norm_s( ratio );
    7374           0 :         idxGain = sub( 14, exp_norm );
    7375           0 :         idxGain = s_max( 0, idxGain );
    7376             :     }
    7377             : 
    7378           0 :     test();
    7379           0 :     test();
    7380           0 :     IF( GT_16( idxGain, 2 ) && LT_16( idxGain, 15 ) && GT_16( ratio, add( shl( 2, sub( idxGain, 1 ) ), shl( 2, sub( idxGain, 2 ) ) ) ) )
    7381             :     {
    7382           0 :         idxGain = add( idxGain, 1 );
    7383             :     }
    7384             : 
    7385             :     /* ratio = (float)(1 << idxGain);*/
    7386           0 :     IF( st->codec_mode == MODE2 )
    7387             :     {
    7388           0 :         hBWE_TD->idxGain = idxGain;
    7389           0 :         move16();
    7390             :     }
    7391             :     ELSE
    7392             :     {
    7393           0 :         push_indice( st->hBstr, IND_FB_SLOPE, idxGain, 4 );
    7394             :     }
    7395             : 
    7396           0 :     return;
    7397             : }
    7398             : 
    7399       61717 : void fb_tbe_enc_ivas_fx(
    7400             :     Encoder_State *st,        /* i/o: encoder state structure                 */
    7401             :     const Word16 new_input[], /* i  : input speech at 48 kHz sample rate     Q_new_input */
    7402             :     const Word16 fb_exc[],    /* i  : FB excitation from the SWB part        Q_fb_exc */
    7403             :     Word16 Q_fb_exc,
    7404             :     Word16 Q_new_input )
    7405             : {
    7406             :     Word16 ratio;
    7407             :     Word16 tmp_vec[L_FRAME48k];
    7408             :     Word16 idxGain;
    7409             :     Word16 input_fhb_new_buffer[L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) + 4];
    7410       61717 :     Word16 *input_fhb_new = &input_fhb_new_buffer[0] + 4;
    7411             :     Word16 input_fhb[L_FRAME48k];
    7412             :     Word16 Sample_Delay_HP;
    7413             :     Word64 fb_exc_energy;
    7414             :     Word16 tmp, tmp2, exp, exp2;
    7415             :     Word16 s_max_value, exp_temp, i;
    7416       61717 :     TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
    7417             :     Word16 q_diff, q_input_fhb;
    7418             :     Word64 temp1, temp2;
    7419             : 
    7420       61717 :     s_max_value = 0;
    7421       61717 :     move16();
    7422    62087302 :     FOR( i = 0; i < L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ); i++ )
    7423             :     {
    7424    62025585 :         s_max_value = s_max( s_max_value, abs_s( new_input[i - NS2SA( 48000, DELAY_FIR_RESAMPL_NS )] ) );
    7425             :     }
    7426       61717 :     exp_temp = norm_s( s_max_value );
    7427       61717 :     if ( s_max_value == 0 )
    7428             :     {
    7429           0 :         exp_temp = 15;
    7430           0 :         move16();
    7431             :     }
    7432             : 
    7433       61717 :     Copy_Scale_sig( new_input - NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), input_fhb_new, L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), exp_temp );
    7434       61717 :     exp_temp = add( exp_temp, Q_new_input );
    7435       61717 :     IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) )
    7436             :     {
    7437        7969 :         elliptic_bpf_48k_generic_fx(
    7438        7969 :             st->element_mode,
    7439             :             0, // IsUpsampled3
    7440        7969 :             input_fhb_new, &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
    7441             :     }
    7442             :     ELSE
    7443             :     {
    7444       53748 :         elliptic_bpf_48k_generic_fx(
    7445       53748 :             st->element_mode,
    7446             :             0, // IsUpsampled3
    7447       53748 :             input_fhb_new + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), &exp_temp, tmp_vec, hBWE_TD->elliptic_bpf_2_48k_mem_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, full_band_bpf_2_fx );
    7448             :     }
    7449             : 
    7450       61717 :     test();
    7451       61717 :     IF( GT_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_SCE ) )
    7452             :     {
    7453             : 
    7454       53748 :         IF( EQ_16( st->L_frame, L_FRAME ) )
    7455             :         {
    7456       12976 :             Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS ) - L_FRAME48k / 2;
    7457             :         }
    7458             :         ELSE
    7459             :         {
    7460       40772 :             Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) - L_FRAME48k / 2;
    7461             :         }
    7462             :     }
    7463             :     ELSE
    7464             :     {
    7465        7969 :         Sample_Delay_HP = NS2SA( 48000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) - L_FRAME48k / 2;
    7466             :     }
    7467             : 
    7468       61717 :     IF( NE_16( st->last_extl, FB_TBE ) )
    7469             :     {
    7470        4569 :         set16_fx( hBWE_TD->old_input_fhb_fx, 0, Sample_Delay_HP );
    7471        4569 :         hBWE_TD->old_input_fhb_fx_Q = 0;
    7472        4569 :         move16();
    7473        4569 :         set16_fx( tmp_vec, 0, L_FRAME16k );
    7474             :     }
    7475             : 
    7476       61717 :     q_diff = sub( exp_temp, hBWE_TD->old_input_fhb_fx_Q );
    7477       61717 :     Copy( hBWE_TD->old_input_fhb_fx, input_fhb, Sample_Delay_HP );                    // hBWE_TD->old_input_fhb_fx_Q
    7478       61717 :     Copy( tmp_vec, input_fhb + Sample_Delay_HP, sub( L_FRAME48k, Sample_Delay_HP ) ); // exp_temp
    7479       61717 :     IF( q_diff > 0 )
    7480             :     {
    7481       21579 :         scale_sig( input_fhb + Sample_Delay_HP, sub( L_FRAME48k, Sample_Delay_HP ), negate( q_diff ) ); // hBWE_TD->old_input_fhb_fx_Q
    7482       21579 :         q_input_fhb = hBWE_TD->old_input_fhb_fx_Q;
    7483       21579 :         move16();
    7484             :     }
    7485             :     ELSE
    7486             :     {
    7487       40138 :         scale_sig( input_fhb, Sample_Delay_HP, q_diff ); // exp_temp
    7488       40138 :         q_input_fhb = exp_temp;
    7489       40138 :         move16();
    7490             :     }
    7491       61717 :     Copy( tmp_vec + L_FRAME48k - Sample_Delay_HP, hBWE_TD->old_input_fhb_fx, Sample_Delay_HP );
    7492       61717 :     hBWE_TD->old_input_fhb_fx_Q = exp_temp;
    7493       61717 :     move16();
    7494             : 
    7495       61717 :     temp2 = sum2_fx_no_sat( input_fhb, L_FRAME48k / 2 );                                                                     // 2*q_input_fhb                                                                                                               /* Q(2*Q_input_fhb-8) */
    7496       61717 :     temp2 = W_add( temp2, W_shl( hBWE_TD->prev_fb_energy_fx, sub( shl( q_input_fhb, 1 ), hBWE_TD->prev_fb_energy_fx_Q ) ) ); // 2*q_input_fhb
    7497             : 
    7498       61717 :     temp1 = sum2_fx_no_sat( input_fhb + L_FRAME48k / 2, L_FRAME48k / 2 ); // 2*q_input_fhb
    7499       61717 :     exp = W_norm( temp1 );
    7500       61717 :     temp1 = W_shl( temp1, exp );                       // 2*q_input_fhb+exp
    7501       61717 :     hBWE_TD->prev_fb_energy_fx = W_extract_h( temp1 ); // 2*q_input_fhb+exp-32
    7502       61717 :     move32();
    7503       61717 :     hBWE_TD->prev_fb_energy_fx_Q = sub( add( shl( q_input_fhb, 1 ), exp ), 32 );
    7504       61717 :     move16();
    7505             : 
    7506       61717 :     fb_exc_energy = sum2_fx_no_sat( fb_exc, L_FRAME16k ); // 2*Q_fb_exc
    7507             : 
    7508             :     /*ratio = (float) sqrt( temp2 / fb_exc_energy );*/
    7509       61717 :     exp = sub( W_norm( temp2 ), 1 );
    7510       61717 :     tmp = extract_h( W_extract_h( W_shl( temp2, exp ) ) ); // 2*q_input_fhb+exp-48
    7511       61717 :     exp = sub( 63, add( shl( q_input_fhb, 1 ), exp ) );    // 15-(2*q_input_fhb+exp-48)
    7512             : 
    7513       61717 :     exp2 = W_norm( fb_exc_energy );
    7514       61717 :     tmp2 = extract_h( W_extract_h( W_shl( fb_exc_energy, exp2 ) ) ); // 2*Q_fb_exc+exp2-48
    7515       61717 :     exp2 = sub( 63, add( shl( Q_fb_exc, 1 ), exp2 ) );               // 15-(2*Q_fb_exc+exp2-48)
    7516             : 
    7517       61717 :     ratio = 0;
    7518       61717 :     idxGain = 0;
    7519       61717 :     move16();
    7520       61717 :     move16();
    7521       61717 :     test();
    7522       61717 :     IF( tmp != 0 && tmp2 != 0 )
    7523             :     {
    7524       61610 :         ratio = div_s( tmp, tmp2 ); // exp: exp-exp2
    7525       61610 :         exp = sub( exp, exp2 );
    7526             :         /*     ratio = (float) sqrt( temp2 / fb_exc_energy );
    7527             :                idxGain = (short)( log2_f ((float)ratio) + 0.5f );
    7528             : 
    7529             :               => idxGain = (short)( log2_f (sqrt( temp2 / fb_exc_energy )) + 0.5f )
    7530             :                          = (short)(log2f(sqrt(temp2 / fb_exc_energy)) + log2f(sqrt(2)))
    7531             :                          = (short)log2f(sqrt(2 * (temp2 / fb_exc_energy)))
    7532             :                 increase the exp of division result by one, two accommodate the multiplication with 2 in sqrt
    7533             :         */
    7534       61610 :         exp = add( exp, 1 );
    7535       61610 :         ratio = Sqrt16( ratio, &exp );
    7536             :         /* Since ratio always lie betwwen 16384 and 32767, subtract the Q of sqrt result from 14 to get the integral part of log value */
    7537       61610 :         idxGain = sub( exp, 1 ); // 14 - (15-exp)
    7538             : 
    7539             :         /* idxGain = max( 0, min( 15, idxGain ) ); */
    7540       61610 :         idxGain = s_max( 0, s_min( 15, idxGain ) );
    7541             :     }
    7542             : 
    7543             :     /* ratio = (float)(1 << idxGain);*/
    7544       61717 :     IF( EQ_16( st->codec_mode, MODE2 ) )
    7545             :     {
    7546           0 :         hBWE_TD->idxGain = idxGain;
    7547           0 :         move16();
    7548             :     }
    7549             :     ELSE
    7550             :     {
    7551       61717 :         push_indice( st->hBstr, IND_FB_SLOPE, idxGain, 4 );
    7552             :     }
    7553       61717 :     return;
    7554             : }
    7555        1224 : void tbe_write_bitstream_fx(
    7556             :     Encoder_State *st_fx /* i/o: encoder state structure                 */
    7557             : )
    7558             : {
    7559             :     Word16 i;
    7560        1224 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
    7561        1224 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    7562             : 
    7563             : 
    7564        1224 :     test();
    7565        1224 :     test();
    7566        1224 :     test();
    7567        1224 :     test();
    7568        1224 :     test();
    7569        1224 :     IF( ( st_fx->rf_mode || EQ_32( st_fx->total_brate, ACELP_9k60 ) ) && ( EQ_16( st_fx->bwidth, WB ) ) )
    7570             :     {
    7571             :         /* WB LSF */
    7572           0 :         push_next_indice( hBstr, hBWE_TD->lsf_WB, NUM_BITS_LBR_WB_LSF );
    7573             : 
    7574             :         /* WB frame */
    7575           0 :         push_next_indice( hBstr, hBWE_TD->gFrame_WB, NUM_BITS_SHB_FrameGain_LBR_WB );
    7576             :     }
    7577        1224 :     ELSE IF( ( GE_32( st_fx->total_brate, ACELP_9k60 ) ) && ( LE_32( st_fx->total_brate, ACELP_32k ) ) &&
    7578             :              ( ( EQ_16( st_fx->bwidth, SWB ) ) || ( EQ_16( st_fx->bwidth, FB ) ) ) )
    7579             :     {
    7580             :         /* LSF coefficients */
    7581             : 
    7582        1224 :         test();
    7583        1224 :         IF( ( EQ_16( st_fx->rf_mode, 1 ) ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
    7584             :         {
    7585           0 :             push_next_indice( hBstr, hBWE_TD->lsf_idx[0], 8 );
    7586             :         }
    7587             :         ELSE
    7588             :         {
    7589        7344 :             FOR( i = 0; i < NUM_Q_LSF; i++ )
    7590             :             {
    7591        6120 :                 push_next_indice( hBstr, hBWE_TD->lsf_idx[i], lsf_q_num_bits[i] );
    7592             :             }
    7593             : 
    7594             :             /* LSF mirror points */
    7595        1224 :             push_next_indice( hBstr, hBWE_TD->m_idx, MIRROR_POINT_BITS );
    7596             : 
    7597             :             /* LSF grid points */
    7598        1224 :             push_next_indice( hBstr, hBWE_TD->grid_idx, NUM_LSF_GRID_BITS );
    7599             :         }
    7600             : 
    7601             :         /* Gain shape */
    7602        1224 :         push_next_indice( hBstr, hBWE_TD->idxSubGains, NUM_BITS_SHB_SUBGAINS );
    7603             : 
    7604             :         /* frame gain */
    7605        1224 :         push_next_indice( hBstr, hBWE_TD->idxFrameGain, NUM_BITS_SHB_FRAMEGAIN );
    7606             : 
    7607        1224 :         IF( GE_32( st_fx->total_brate, ACELP_24k40 ) )
    7608             :         {
    7609             :             /* sub frame energy*/
    7610        1224 :             push_next_indice( hBstr, hBWE_TD->idx_shb_fr_gain, NUM_BITS_SHB_ENER_SF );
    7611             : 
    7612             :             /* gain shapes residual */
    7613        7344 :             FOR( i = 0; i < NB_SUBFR16k; i++ )
    7614             :             {
    7615        6120 :                 push_next_indice( hBstr, hBWE_TD->idx_res_gs[i], NUM_BITS_SHB_RES_GS );
    7616             :             }
    7617             : 
    7618             :             /* voicing factor */
    7619        1224 :             push_next_indice( hBstr, hBWE_TD->idx_mixFac, NUM_BITS_SHB_VF );
    7620             :         }
    7621             : 
    7622        1224 :         IF( EQ_16( st_fx->tec_tfa, 1 ) )
    7623             :         {
    7624        1224 :             push_next_indice( hBstr, st_fx->tec_flag, BITS_TEC );
    7625        1224 :             push_next_indice( hBstr, st_fx->tfa_flag, BITS_TFA );
    7626             :         }
    7627             :     }
    7628             : 
    7629        1224 :     IF( EQ_16( st_fx->bwidth, FB ) )
    7630             :     {
    7631           0 :         push_next_indice( hBstr, hBWE_TD->idxGain, 4 );
    7632             :     }
    7633             : 
    7634        1224 :     return;
    7635             : }
    7636             : 
    7637             : 
    7638      304576 : void TBEreset_enc_fx(
    7639             :     TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
    7640             :     const Word16 last_core,    /* i  : last core               */
    7641             :     Word16 bandwidth           /* i  : bandwidth mode          */
    7642             : )
    7643             : {
    7644      304576 :     IF( NE_16( last_core, ACELP_CORE ) )
    7645             :     {
    7646      302513 :         set16_fx( hBWE_TD->old_bwe_exc_fx, 0, PIT16k_MAX * 2 );
    7647      302513 :         hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
    7648      302513 :         move32();
    7649      302513 :         hBWE_TD->prev_Q_bwe_exc = 31;
    7650      302513 :         move16();
    7651             :     }
    7652             : 
    7653      304576 :     test();
    7654      304576 :     IF( EQ_16( bandwidth, WB ) )
    7655             :     {
    7656       10342 :         wb_tbe_extras_reset_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx );
    7657       10342 :         set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 7 );
    7658       10342 :         set16_fx( hBWE_TD->state_lpc_syn_fx, 0, 10 );
    7659       10342 :         set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD / 4 );
    7660       10342 :         set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
    7661       10342 :         set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
    7662             :     }
    7663      294234 :     ELSE IF( ( EQ_16( bandwidth, SWB ) ) || ( EQ_16( bandwidth, FB ) ) )
    7664             :     {
    7665      294234 :         set16_fx( hBWE_TD->state_ana_filt_shb_fx, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
    7666             : 
    7667      294234 :         swb_tbe_reset_fx( hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    7668      294234 :                           hBWE_TD->syn_overlap_fx, hBWE_TD->state_syn_shbexc_fx, &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ),
    7669      294234 :                           hBWE_TD->mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ) );
    7670             : 
    7671      294234 :         IF( EQ_16( bandwidth, FB ) )
    7672             :         {
    7673      208682 :             set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
    7674      208682 :             hBWE_TD->fb_tbe_demph_fx = 0;
    7675      208682 :             move16();
    7676      208682 :             fb_tbe_reset_enc_fx( hBWE_TD->elliptic_bpf_2_48k_mem_fx, &hBWE_TD->prev_fb_energy_fx, hBWE_TD->elliptic_bpf_2_48k_mem_fx_Q, &hBWE_TD->prev_fb_energy_fx_Q );
    7677             :         }
    7678             :     }
    7679             : 
    7680      304576 :     return;
    7681             : }
    7682             : 
    7683             : /*-------------------------------------------------------------------*
    7684             :  * InitSWBencBufferStates()
    7685             :  *
    7686             :  * Initialize SWB buffer states
    7687             :  *-------------------------------------------------------------------*/
    7688             : 
    7689       17203 : void InitSWBencBufferStates_fx(
    7690             :     TD_BWE_ENC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                   */
    7691             :     Word16 *shb_speech         /* o  : SHB target signal (6-14kHz) at 16kHz */
    7692             : )
    7693             : {
    7694       17203 :     IF( shb_speech != NULL )
    7695             :     {
    7696           0 :         set16_fx( shb_speech, 0, L_FRAME16k );
    7697             :     }
    7698             : 
    7699       17203 :     set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
    7700       17203 :     set16_fx( hBWE_TD->mem_shb_res_fx, 0, MAX_LEN_MA_FILTER );
    7701       17203 :     set16_fx( hBWE_TD->old_EnvSHBres_fx, 0, L_FRAME4k );
    7702       17203 :     hBWE_TD->old_mean_EnvSHBres_fx = 0;
    7703       17203 :     move16();
    7704       17203 :     hBWE_TD->prev_enr_EnvSHBres_fx = 1; /*1.0f in Q0*/
    7705       17203 :     move32();
    7706       17203 :     hBWE_TD->prev_enr_EnvSHBres_e = 31;
    7707       17203 :     move16();
    7708       17203 :     hBWE_TD->prev_shb_env_tilt_fx = 0;
    7709       17203 :     move16();
    7710       17203 :     hBWE_TD->prev_Q_shb = 15;
    7711       17203 :     move16();
    7712       17203 :     hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /*1.0f in Q0*/
    7713       17203 :     move32();
    7714       17203 :     hBWE_TD->prev_mix_factor_fx = 32767; /*1.0f in Q15*/
    7715       17203 :     move16();
    7716       17203 :     hBWE_TD->prev_Env_error_fx = 0;
    7717       17203 :     move16();
    7718             : 
    7719       17203 :     return;
    7720             : }

Generated by: LCOV version 1.14