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

Generated by: LCOV version 1.14