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

Generated by: LCOV version 1.14