LCOV - code coverage report
Current view: top level - lib_enc - swb_tbe_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ a17e51f9ff5a27406f0609d97377f4ae607c6718 Lines: 2936 3586 81.9 %
Date: 2025-11-06 02:08:20 Functions: 43 45 95.6 %

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

Generated by: LCOV version 1.14