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

Generated by: LCOV version 1.14