LCOV - code coverage report
Current view: top level - lib_enc - swb_bwe_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ c885e40061398f347265d25cc3dfa959226433c0 Lines: 1701 2078 81.9 %
Date: 2025-11-10 02:25:36 Functions: 27 29 93.1 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdlib.h>
       6             : #include "options.h"
       7             : #include "prot_fx.h"
       8             : #include "cnst.h"
       9             : #include "rom_com.h"
      10             : #include "stl.h"
      11             : #include "prot_fx_enc.h" /* Function prototypes                    */
      12             : 
      13             : /*---------------------------------------------------------------------*
      14             :  * Local functions
      15             :  *---------------------------------------------------------------------*/
      16             : 
      17             : static Word16 SWB_BWE_encoding_fx(
      18             :     Encoder_State *st_fx,      /* i/o: encoder state structure   */
      19             :     Word16 *insig_fx,          /* i/o: delayed original input signal at 32kHz (might be rescaled)*/
      20             :     const Word16 *insig_lp_fx, /* i  : delayed original lowband input signal at 32kHz */
      21             :     const Word16 *insig_hp_fx, /* i  : delayed original highband input signal at 32kHz */
      22             :     const Word16 *synth_fx,    /* i  : delayed ACELP core synthesis at 12.8kHz */
      23             :     const Word16 *yos_fx,      /* i  : MDCT coefficients of the windowed original input signal at 32kHz */
      24             :     Word16 *SWB_fenv_fx,       /* o  : frequency-domain quantized BWE envelope */
      25             :     const Word16 tilt_nb_fx,   /* i  : SWB tilt */
      26             :     const Word16 st_offset,    /* i  : start frequency offset for BWE envelope */
      27             :     const Word16 coder_type,   /* i  : coding type                              */
      28             :     Word16 Q_insig_lp,
      29             :     Word16 Q_shb,
      30             :     Word16 Q_synth,
      31             :     Word16 Q_synth_lf );
      32             : 
      33             : static Word16 SWB_BWE_encoding_ivas_fx(
      34             :     Encoder_State *st_fx,      /* i/o: encoder state structure   */
      35             :     Word16 *insig_fx,          /* i/o: delayed original input signal at 32kHz (might be rescaled)*/
      36             :     const Word16 *insig_lp_fx, /* i  : delayed original lowband input signal at 32kHz */
      37             :     const Word16 *insig_hp_fx, /* i  : delayed original highband input signal at 32kHz */
      38             :     const Word16 *synth_fx,    /* i  : delayed ACELP core synthesis at 12.8kHz */
      39             :     const Word32 *yos_fx,      /* i  : MDCT coefficients of the windowed original input signal at 32kHz */
      40             :     Word16 *SWB_fenv_fx,       /* o  : frequency-domain quantized BWE envelope */
      41             :     const Word16 tilt_nb_fx,   /* i  : SWB tilt */
      42             :     const Word16 st_offset,    /* i  : start frequency offset for BWE envelope */
      43             :     Word16 Q_insig_lp,
      44             :     Word16 Q_shb,
      45             :     Word16 Q_synth,
      46             :     Word16 Q_synth_lf );
      47             : 
      48           8 : static void delay_input_signal_fx(
      49             :     Word16 *old_sig,
      50             :     Word16 *cur_sig,
      51             :     Word16 *new_sig,
      52             :     Word16 m1,
      53             :     Word16 m2,
      54             :     Word16 *Q_old,
      55             :     Word16 *Q_new )
      56             : {
      57             :     Word16 i;
      58             :     Word16 max;
      59             :     Word16 max1_exp, max2_exp;
      60             : 
      61           8 :     max = abs_s( old_sig[0] );
      62        1120 :     FOR( i = 1; i < m1; i++ )
      63             :     {
      64        1112 :         max = s_max( max, abs_s( old_sig[i] ) );
      65             :     }
      66           8 :     IF( max == 0 )
      67             :     {
      68           0 :         max1_exp = 15;
      69           0 :         move16();
      70             :     }
      71             :     ELSE
      72             :     {
      73           8 :         max1_exp = norm_s( max );
      74             :     }
      75             : 
      76           8 :     max = abs_s( new_sig[0] );
      77        2048 :     FOR( i = 1; i < m2; i++ )
      78             :     {
      79        2040 :         max = s_max( max, abs_s( new_sig[i] ) );
      80             :     }
      81           8 :     IF( max == 0 )
      82             :     {
      83           0 :         max2_exp = 15;
      84           0 :         move16();
      85             :     }
      86             :     ELSE
      87             :     {
      88           8 :         max2_exp = norm_s( max );
      89             :     }
      90             : 
      91           8 :     IF( GT_16( add( max1_exp, *Q_old ), add( max2_exp, *Q_new ) ) )
      92             :     {
      93           0 :         Copy_Scale_sig( new_sig, new_sig, m2, max2_exp );
      94           0 :         Copy_Scale_sig( old_sig, old_sig, m1, sub( add( max2_exp, *Q_new ), *Q_old ) );
      95           0 :         *Q_new = add( max2_exp, *Q_new );
      96             :     }
      97           8 :     ELSE IF( LT_16( add( max1_exp, *Q_old ), add( max2_exp, *Q_new ) ) )
      98             :     {
      99           3 :         Copy_Scale_sig( new_sig, new_sig, m2, sub( add( max1_exp, *Q_old ), *Q_new ) );
     100           3 :         Copy_Scale_sig( old_sig, old_sig, m1, max1_exp );
     101           3 :         *Q_new = add( max1_exp, *Q_old );
     102             :     }
     103             :     ELSE
     104             :     {
     105           5 :         Copy_Scale_sig( new_sig, new_sig, m2, max2_exp );
     106           5 :         Copy_Scale_sig( old_sig, old_sig, m1, max1_exp );
     107           5 :         *Q_new = add( max1_exp, *Q_old );
     108             :     }
     109           8 :     *Q_old = *Q_new;
     110           8 :     move16();
     111           8 :     Copy( old_sig, cur_sig, m1 );
     112           8 :     Copy( new_sig, &cur_sig[m1], sub( m2, m1 ) );
     113           8 :     Copy( new_sig + sub( m2, m1 ), old_sig, m1 );
     114             : 
     115           8 :     return;
     116             : }
     117             : 
     118             : /*-------------------------------------------------------------------*
     119             :  * wb_bwe_enc()
     120             :  *
     121             :  * WB BWE encoder
     122             :  *-------------------------------------------------------------------*/
     123             : 
     124           0 : void wb_bwe_enc_fx(
     125             :     Encoder_State *st_fx,           /* i/o: encoder state structure                  */
     126             :     const Word16 *new_wb_speech_fx, /* i  : original input signal at 16kHz           */
     127             :     Word16 coder_type               /* i  : coding type                              */
     128             : )
     129             : {
     130           0 :     Word16 mode = 0;
     131           0 :     move16();
     132             :     Word16 Sample_Delay_WB_BWE;
     133             :     Word16 old_input_fx[NS2SA( 16000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k];
     134             :     Word32 yorig_32[L_FRAME16k];
     135             :     Word16 yorig_fx[L_FRAME16k];
     136             :     Word32 L_wtda_synth_fx[2 * L_FRAME16k];
     137             :     Word16 *new_input_fx; /* pointer to original input signal         */
     138             :     Word16 scl, new_input_fx_exp;
     139             :     Word16 Q_synth;
     140           0 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
     141             :     Word16 WB_fenv_fx[SWB_FENV];
     142             : 
     143           0 :     IF( EQ_32( st_fx->total_brate, ACELP_13k20 ) )
     144             :     {
     145             :         /*---------------------------------------------------------------------*
     146             :          * Delay the original input signal to be synchronized with ACELP core synthesis
     147             :          *---------------------------------------------------------------------*/
     148           0 :         set16_fx( old_input_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
     149           0 :         Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
     150             : 
     151           0 :         new_input_fx = old_input_fx + Sample_Delay_WB_BWE;
     152           0 :         Copy( hBWE_FD->old_input_wb_fx, old_input_fx, Sample_Delay_WB_BWE );
     153           0 :         Copy( new_wb_speech_fx, new_input_fx, L_FRAME16k );
     154           0 :         Copy( old_input_fx + L_FRAME16k, hBWE_FD->old_input_wb_fx, Sample_Delay_WB_BWE );
     155             : 
     156             :         /*---------------------------------------------------------------------*/
     157             :         /* WB BWE encoding                                                     */
     158             : 
     159             : 
     160             :         /* MDCT of the core synthesis signal */
     161             :         /*---------------------------------------------------------------------*/
     162           0 :         new_input_fx_exp = 0;
     163           0 :         move16();
     164             : 
     165           0 :         wtda_fx( old_input_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx,
     166             :                  &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */
     167             :                  L_FRAME16k );
     168             : 
     169             :         /* DCT of the ACELP core synthesis */
     170           0 :         direct_transform_fx( L_wtda_synth_fx, yorig_32, 0, L_FRAME16k, &new_input_fx_exp, /*st_fx->element_mode*/ EVS_MONO );
     171             : 
     172             :         /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */
     173           0 :         scl = sub( 16 + 8 /*MAX_Q_NEW_INPUT*/, new_input_fx_exp );
     174             :         /* Possible to Upscale? */
     175           0 :         IF( scl > 0 )
     176             :         {
     177             :             /* Yes */
     178             :             /* Calc Room to Upscale */
     179           0 :             Q_synth = Find_Max_Norm32( yorig_32, L_FRAME16k );
     180             : 
     181             :             /* Stay within MAX_Q_NEW_INPUT */
     182           0 :             scl = s_min( Q_synth, scl );
     183             :         }
     184           0 :         Copy_Scale_sig32_16( yorig_32, yorig_fx, L_FRAME16k, scl );
     185           0 :         Q_synth = sub( add( sub( new_input_fx_exp, 16 ), scl ), 1 );
     186             : 
     187           0 :         mode = WB_BWE_encoding_fx( coder_type, yorig_fx, WB_fenv_fx, st_fx, Q_synth, Q_synth );
     188           0 :         push_indice( st_fx->hBstr, IND_WB_CLASS, sub( mode, 2 ), 1 );
     189             :     }
     190             : 
     191           0 :     hBWE_FD->prev_mode = mode;
     192           0 :     move16();
     193             : 
     194           0 :     return;
     195             : }
     196             : 
     197             : /*-------------------------------------------------------------------*
     198             :  * wb_bwe_enc_ivas_fx()
     199             :  *
     200             :  * WB BWE encoder
     201             :  *-------------------------------------------------------------------*/
     202             : 
     203       12109 : void wb_bwe_enc_ivas_fx(
     204             :     Encoder_State *st_fx,          /* i/o: encoder state structure                  */
     205             :     const Word16 *new_wb_speech_fx /* i  : original input signal at 16kHz           */
     206             : )
     207             : {
     208       12109 :     Word16 mode = 0;
     209       12109 :     move16();
     210             :     Word16 Sample_Delay_WB_BWE;
     211             :     Word16 old_input_fx[NS2SA( 16000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k];
     212             :     Word32 yorig_32[L_FRAME16k];
     213             :     Word32 L_wtda_synth_fx[2 * L_FRAME16k];
     214             :     Word16 *new_input_fx; /* pointer to original input signal         */
     215             :     Word16 new_input_fx_exp;
     216       12109 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
     217             :     Word16 WB_fenv_fx[SWB_FENV];
     218             : 
     219       12109 :     IF( st_fx->extl_brate > 0 )
     220             :     {
     221             :         /*---------------------------------------------------------------------*
     222             :          * Delay the original input signal to be synchronized with ACELP core synthesis
     223             :          *---------------------------------------------------------------------*/
     224        4241 :         set16_fx( old_input_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
     225        4241 :         Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
     226        4241 :         move16();
     227             : 
     228        4241 :         new_input_fx = old_input_fx + Sample_Delay_WB_BWE;
     229        4241 :         Copy( hBWE_FD->old_input_wb_fx, old_input_fx, Sample_Delay_WB_BWE );
     230        4241 :         Copy( new_wb_speech_fx, new_input_fx, L_FRAME16k );
     231        4241 :         Copy( old_input_fx + L_FRAME16k, hBWE_FD->old_input_wb_fx, Sample_Delay_WB_BWE );
     232             : 
     233             :         /*---------------------------------------------------------------------*/
     234             :         /* WB BWE encoding                                                     */
     235             : 
     236             : 
     237             :         /* MDCT of the core synthesis signal */
     238             :         /*---------------------------------------------------------------------*/
     239        4241 :         new_input_fx_exp = -1;
     240        4241 :         move16();
     241             : 
     242        4241 :         wtda_fx( old_input_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx,
     243             :                  &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */
     244             :                  L_FRAME16k );
     245             : 
     246             :         /* DCT of the ACELP core synthesis */
     247        4241 :         direct_transform_fx( L_wtda_synth_fx, yorig_32, 0, L_FRAME16k, &new_input_fx_exp, st_fx->element_mode );
     248             : 
     249        4241 :         mode = WB_BWE_encoding_ivas_fx( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp );
     250        4241 :         move16();
     251        4241 :         push_indice( st_fx->hBstr, IND_WB_CLASS, sub( mode, 2 ), 1 );
     252             :     }
     253             : 
     254       12109 :     hBWE_FD->prev_mode = mode;
     255       12109 :     move16();
     256             : 
     257       12109 :     return;
     258             : }
     259             : 
     260             : /*-------------------------------------------------------------------*
     261             :  * swb_bwe_enc()
     262             :  *
     263             :  * SWB BWE encoder (only for 32kHz signals)
     264             :  *-------------------------------------------------------------------*/
     265       13689 : void swb_bwe_enc_ivas_fx(
     266             :     Encoder_State *st_fx,              /* i/o: encoder state structure                  */
     267             :     const Word16 last_element_mode,    /* i  : last element mode                       */
     268             :     Word16 *old_input_12k8_fx,         /* i  : input signal @12.8kHz for SWB BWE       */
     269             :     Word16 *old_input_16k_fx,          /* i  : input signal @16kHz for SWB BWE         */
     270             :     const Word16 *old_syn_12k8_16k_fx, /* i  : ACELP core synthesis at 12.8kHz or 16kHz */
     271             :     const Word16 *new_swb_speech_fx,   /* i  : original input signal at 32kHz           */
     272             :     const Word16 Q_new_swb_speech,     /* i  : Q for new_swb_speech_fx                  */
     273             :     Word16 *shb_speech_fx,             /* i  : SHB target signal (6-14kHz) at 16kHz     */
     274             :     Word16 Q_shb_speech,
     275             :     Word16 Q_slb_speech )
     276             : {
     277             :     Word16 i;
     278             :     Word16 *new_input_fx;
     279             :     Word16 tmp, exp, exp1;
     280             :     Word16 frac;
     281             :     Word32 L_tmp;
     282             :     Word16 inner_frame;
     283             :     Word32 inner_Fs;
     284             :     Word32 L_old_input_fx[2 * L_FRAME48k];
     285             :     Word32 yorig_32[L_FRAME48k];
     286             :     Word16 old_input_fx[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k];
     287             :     Word16 old_input_lp_fx[L_FRAME16k];
     288             :     Word16 new_input_hp_fx[L_FRAME16k];
     289             :     Word16 yorig_fx[L_FRAME48k];
     290             :     Word16 scl, new_input_fx_q;
     291             :     Word16 max;
     292             :     Word16 Sample_Delay_SWB_BWE;
     293             :     Word16 Sample_Delay_HP;
     294             :     Word16 Sample_Delay_LP;
     295       13689 :     Word16 idxGain = 0;
     296       13689 :     move16();
     297             :     Word16 Q_synth_hf, Q_synth, Q_shb;
     298             :     Word16 tilt_nb_fx;
     299             :     Word16 SWB_fenv_fx[SWB_FENV];
     300             :     Word32 ener_low_fx;
     301             :     Word32 energy_fbe_fb_fx;
     302             :     Word16 fb_ener_adjust_fx;
     303       13689 :     Word16 ener_adjust_quan_fx = 0;
     304       13689 :     move16();
     305             :     Word16 fb_band_begin;
     306             :     Word16 q_new_input_hp;
     307             : 
     308       13689 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
     309       13689 :     TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD;
     310             : 
     311             : 
     312             :     /*---------------------------------------------------------------------*
     313             :      * Delay the original input signal to be synchronized with ACELP core synthesis
     314             :      *---------------------------------------------------------------------*/
     315       13689 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     316             :     {
     317        4792 :         inner_frame = L_FRAME48k;
     318        4792 :         move16();
     319        4792 :         inner_Fs = 48000;
     320        4792 :         move32();
     321             :     }
     322             :     ELSE
     323             :     {
     324        8897 :         inner_frame = L_FRAME32k;
     325        8897 :         move16();
     326        8897 :         inner_Fs = 32000;
     327        8897 :         move32();
     328             :     }
     329             : 
     330       13689 :     set16_fx( old_input_fx, 0, add( NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ), inner_frame ) );
     331             : 
     332       13689 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     333             :     {
     334        9155 :         Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
     335        9155 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     336        9155 :         IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
     337             :         {
     338           2 :             Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS );
     339             :         }
     340        9155 :         Sample_Delay_LP = NS2SA( 12800, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS );
     341             : 
     342        9155 :         IF( st_fx->element_mode > EVS_MONO )
     343             :         {
     344        9155 :             Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
     345        9155 :             Sample_Delay_HP = sub( Sample_Delay_HP, NS2SA( 16000, DELAY_FIR_RESAMPL_NS ) );
     346             : 
     347        9155 :             IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) )
     348             :             {
     349        3670 :                 Copy( old_input_12k8_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP );
     350        3670 :                 Copy( hBWE_TD->old_speech_shb_fx + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp_fx, Sample_Delay_HP );
     351             :             }
     352             :         }
     353        9155 :         Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP );
     354        9155 :         Copy( old_input_12k8_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME - Sample_Delay_LP );
     355        9155 :         Copy( old_input_12k8_fx + L_INP_MEM + L_FRAME - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP );
     356             :     }
     357             :     ELSE
     358             :     {
     359        4534 :         Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
     360        4534 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     361        4534 :         IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
     362             :         {
     363          19 :             Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS );
     364             :         }
     365        4534 :         Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS );
     366        4534 :         IF( st_fx->element_mode > EVS_MONO )
     367             :         {
     368        4534 :             Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
     369        4534 :             Sample_Delay_HP = sub( Sample_Delay_HP, NS2SA( 16000, DELAY_FIR_RESAMPL_NS ) );
     370             : 
     371        4534 :             IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) )
     372             :             {
     373         695 :                 Copy( old_input_16k_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP );
     374         695 :                 Copy( hBWE_TD->old_speech_shb_fx + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp_fx, Sample_Delay_HP );
     375             :             }
     376             :         }
     377        4534 :         Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP );
     378        4534 :         Copy( old_input_16k_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP );
     379        4534 :         Copy( old_input_16k_fx + L_INP_MEM + L_FRAME16k - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP );
     380             :     }
     381             : 
     382       13689 :     q_new_input_hp = s_min( Q_shb_speech, hBWE_FD->Q_new_input_hp );
     383       13689 :     IF( LT_16( Q_shb_speech, hBWE_FD->Q_new_input_hp ) )
     384             :     {
     385        1993 :         Copy_Scale_sig( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP, sub( Q_shb_speech, hBWE_FD->Q_new_input_hp ) ); // Q_shb_speech
     386        1993 :         Copy( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP );                                     // Q_shb_speech
     387             :     }
     388             :     ELSE
     389             :     {
     390       11696 :         Copy( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP );                                                                             // hBWE_FD->Q_new_input_hp
     391       11696 :         Copy_Scale_sig( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP, sub( hBWE_FD->Q_new_input_hp, Q_shb_speech ) ); // hBWE_FD->Q_new_input_hp
     392             :     }
     393             : 
     394       13689 :     hBWE_FD->Q_new_input_hp = Q_shb_speech;
     395       13689 :     move16();
     396       13689 :     Copy( shb_speech_fx + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp_fx, Sample_Delay_HP );
     397       13689 :     new_input_fx = old_input_fx + Sample_Delay_SWB_BWE;
     398       13689 :     Copy( hBWE_FD->old_input_fx, old_input_fx, Sample_Delay_SWB_BWE );
     399       13689 :     Copy( new_swb_speech_fx, new_input_fx, inner_frame );
     400       13689 :     Copy( old_input_fx + inner_frame, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
     401             :     /*----------------------------------------------------------------------*
     402             :      * Calculate tilt of the input signal and the ACELP core synthesis
     403             :      *----------------------------------------------------------------------*/
     404             : 
     405             :     /* tilt returned in Q24 goto to Q11 */
     406       13689 :     tilt_nb_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( old_input_lp_fx, Q_slb_speech, st_fx->L_frame ), 3 ) );
     407             : 
     408             :     /*---------------------------------------------------------------------*
     409             :      * SWB BWE encoding
     410             :      * FB BWE encoding
     411             :      *---------------------------------------------------------------------*/
     412       13689 :     new_input_fx_q = Q_new_swb_speech;
     413       13689 :     move16();
     414       13689 :     test();
     415       13689 :     IF( ( EQ_16( st_fx->idchan, 1 ) ) && ( EQ_16( last_element_mode, IVAS_CPE_DFT ) ) )
     416             :     {
     417           0 :         FOR( i = 0; i < inner_frame; i++ )
     418             :         {
     419           0 :             hBWE_FD->L_old_wtda_swb_fx[i] = mult_r( hBWE_FD->L_old_wtda_swb_fx[i], div_s( i, inner_frame ) );
     420           0 :             move16();
     421             :         }
     422             :     }
     423             :     /* MDCT of the core synthesis signal */
     424       13689 :     wtda_fx( old_input_fx, &new_input_fx_q, L_old_input_fx, hBWE_FD->L_old_wtda_swb_fx,
     425             :              &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */
     426             :              inner_frame );
     427             : 
     428             :     /* DCT of the ACELP core synthesis */
     429       13689 :     direct_transform_fx( L_old_input_fx, yorig_32, 0, inner_frame, &new_input_fx_q, st_fx->element_mode );
     430             : 
     431             :     /* high-band gain control in case of BWS */
     432       13689 :     IF( st_fx->bwidth_sw_cnt > 0 )
     433             :     {
     434          28 :         v_multc_fx_16( &yorig_32[L_FRAME16k], div_s( st_fx->bwidth_sw_cnt, BWS_TRAN_PERIOD ), &yorig_32[L_FRAME16k], sub( inner_frame, L_FRAME16k ) );
     435             :     }
     436             : 
     437             :     /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */
     438       13689 :     scl = sub( 16 + 8, new_input_fx_q );
     439             :     /* Possible to Upscale? */
     440       13689 :     IF( scl > 0 )
     441             :     {
     442             :         /* Yes */
     443             :         /* Calc Room to Upscale */
     444       13689 :         Q_synth = Find_Max_Norm32( yorig_32, inner_frame );
     445             :         /* Stay within MAX_Q_NEW_INPUT */
     446       13689 :         scl = s_min( Q_synth, scl );
     447             :     }
     448       13689 :     Copy_Scale_sig32_16( yorig_32, yorig_fx, inner_frame, scl );
     449       13689 :     Q_synth = add( sub( new_input_fx_q, 16 ), scl );
     450       13689 :     max = 0;
     451       13689 :     move16();
     452       13689 :     Q_synth_hf = 0;
     453       13689 :     move16();
     454       13689 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     455             :     {
     456        4534 :         scl = 300;
     457        4534 :         move16();
     458             :     }
     459             :     ELSE
     460             :     {
     461        9155 :         scl = 240;
     462        9155 :         move16();
     463             :     }
     464     6750689 :     FOR( i = scl; i < inner_frame; i++ )
     465             :     {
     466     6737000 :         max = s_max( max, abs_s( yorig_fx[i] ) );
     467             :     }
     468             : 
     469       13689 :     IF( max == 0 )
     470             :     {
     471         396 :         exp = 15;
     472         396 :         move16();
     473             :     }
     474             :     ELSE
     475             :     {
     476       13293 :         exp = norm_s( max );
     477             :     }
     478       13689 :     Copy_Scale_sig( &yorig_fx[scl], &yorig_fx[scl], sub( inner_frame, scl ), exp );
     479       13689 :     Q_synth_hf = add( exp, Q_synth );
     480             : 
     481       13689 :     test();
     482       13689 :     IF( EQ_16( st_fx->last_extl, SWB_BWE ) || EQ_16( st_fx->last_extl, FB_BWE ) )
     483             :     {
     484       11278 :         exp = norm_l( st_fx->EnergyLT_fx );
     485       11278 :         IF( GT_16( add( st_fx->EnergyLT_fx_exp, exp ), shl( sub( Q_synth_hf, 4 ), 1 ) ) )
     486             :         {
     487       10919 :             Q_shb = sub( Q_synth_hf, 4 );
     488       10919 :             st_fx->EnergyLT_fx = L_shr( st_fx->EnergyLT_fx, sub( st_fx->EnergyLT_fx_exp, shl( Q_shb, 1 ) ) );
     489       10919 :             move32();
     490             :         }
     491             :         ELSE
     492             :         {
     493         359 :             Q_shb = shr( add( st_fx->EnergyLT_fx_exp, exp ), 1 );
     494         359 :             if ( EQ_16( s_and( exp, 0x0001 ), 1 ) )
     495             :             {
     496          23 :                 exp = sub( exp, 1 );
     497             :             }
     498         359 :             st_fx->EnergyLT_fx = L_shl( st_fx->EnergyLT_fx, exp );
     499         359 :             move32();
     500             :         }
     501             :     }
     502             :     ELSE
     503             :     {
     504        2411 :         Q_shb = sub( Q_synth_hf, 4 );
     505             :     }
     506       13689 :     Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, q_new_input_hp ) );
     507             : 
     508             :     /* FB BWE encoding */
     509       13689 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     510             :     {
     511        4792 :         fb_band_begin = FB_BAND_BEGIN;
     512        4792 :         move16();
     513        4792 :         if ( EQ_16( st_fx->L_frame, L_FRAME ) )
     514             :         {
     515         745 :             fb_band_begin = FB_BAND_BEGIN_12k8;
     516         745 :             move16();
     517             :         }
     518        4792 :         energy_fbe_fb_fx = L_deposit_l( 0 );
     519      912052 :         FOR( i = fb_band_begin; i < FB_BAND_END; i++ )
     520             :         {
     521      907260 :             tmp = shr( yorig_fx[i], 4 );
     522      907260 :             energy_fbe_fb_fx = L_mac0( energy_fbe_fb_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */
     523             :         }
     524        4792 :         ener_low_fx = 0;
     525        4792 :         move16();
     526      867352 :         FOR( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ )
     527             :         {
     528      862560 :             tmp = shr( yorig_fx[i], 4 );
     529      862560 :             ener_low_fx = L_mac0( ener_low_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */
     530             :         }
     531             : 
     532        4792 :         IF( energy_fbe_fb_fx != 0 )
     533             :         {
     534        4734 :             exp = norm_l( energy_fbe_fb_fx );
     535        4734 :             frac = extract_h( L_shl( energy_fbe_fb_fx, exp ) );
     536        4734 :             tmp = div_s( 16384, frac );             /*15+14-(exp+2*(Q_synth_hf-4)-16) -->45-(exp+2*(Q_synth_hf-4)) */
     537        4734 :             L_tmp = Mult_32_16( ener_low_fx, tmp ); /*45-(exp+2*(Q_synth_hf-4)) + 2*(Q_synth_hf-4) - 15 = 30-exp */
     538        4734 :             exp1 = norm_l( L_tmp );
     539        4734 :             L_tmp = L_shl( L_tmp, exp1 );
     540        4734 :             exp = sub( sub( 31, exp1 ), sub( 30, exp ) );
     541        4734 :             L_tmp = Isqrt_lc( L_tmp, &exp );                             /*31-exp */
     542        4734 :             fb_ener_adjust_fx = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /*Q15 */
     543             :         }
     544             :         ELSE
     545             :         {
     546          58 :             fb_ener_adjust_fx = 0;
     547          58 :             move16();
     548             :         }
     549             : 
     550        4792 :         fb_ener_adjust_fx = s_min( fb_ener_adjust_fx, 16384 ); /*Q15 */
     551        4792 :         idxGain = usquant_fx( fb_ener_adjust_fx, &ener_adjust_quan_fx, 0, 512, shl( 1, NUM_BITS_FB_FRAMEGAIN ) );
     552             :     }
     553             : 
     554             :     /* SWB BWE encoding */
     555       13689 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     556             :     {
     557        4534 :         SWB_BWE_encoding_ivas_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32,
     558             :                                   SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q );
     559             :     }
     560             :     ELSE
     561             :     {
     562        9155 :         SWB_BWE_encoding_ivas_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32,
     563             :                                   SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q );
     564             :     }
     565             : 
     566             : 
     567             :     /* write FB BWE frame gain to the bitstream */
     568       13689 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     569             :     {
     570        4792 :         push_indice( st_fx->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN );
     571             :     }
     572             : 
     573       13689 :     return;
     574             : }
     575             : 
     576             : /*-------------------------------------------------------------------*
     577             :  * swb_bwe_enc()
     578             :  *
     579             :  * SWB BWE encoder (only for 32kHz signals)
     580             :  *-------------------------------------------------------------------*/
     581           8 : void swb_bwe_enc_fx(
     582             :     Encoder_State *st_fx,              /* i/o: encoder state structure                  */
     583             :     Word16 *old_input_12k8_fx,         /* i  : input signal @12.8kHz for SWB BWE       */
     584             :     Word16 *old_input_16k_fx,          /* i  : input signal @16kHz for SWB BWE         */
     585             :     const Word16 *old_syn_12k8_16k_fx, /* i  : ACELP core synthesis at 12.8kHz or 16kHz */
     586             :     const Word16 *new_swb_speech_fx,   /* i  : original input signal at 32kHz           */
     587             :     Word16 *shb_speech_fx,             /* i  : SHB target signal (6-14kHz) at 16kHz     */
     588             :     const Word16 coder_type,           /* i  : coding type                              */
     589             :     Word16 Q_shb_speech,
     590             :     Word16 Q_slb_speech )
     591             : {
     592             :     Word16 i;
     593             :     Word16 *new_input_fx;
     594             :     Word16 tmp, exp, exp1;
     595             :     Word16 frac;
     596             :     Word32 L_tmp;
     597             :     Word16 inner_frame;
     598             :     Word32 inner_Fs;
     599             :     Word32 L_old_input_fx[2 * L_FRAME48k];
     600             :     Word32 yorig_32[L_FRAME48k];
     601             :     Word16 old_input_fx[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k];
     602             :     Word16 old_input_lp_fx[L_FRAME16k];
     603             :     Word16 new_input_hp_fx[L_FRAME16k];
     604             :     Word16 yorig_fx[L_FRAME48k];
     605             :     Word16 scl, new_input_fx_exp;
     606             :     Word16 max;
     607             :     Word16 Sample_Delay_SWB_BWE;
     608             :     Word16 Sample_Delay_HP;
     609             :     Word16 Sample_Delay_LP;
     610           8 :     Word16 idxGain = 0;
     611           8 :     move16();
     612             : 
     613             :     Word16 Q_synth_hf, Q_synth, Q_shb;
     614             :     Word16 tilt_nb_fx;
     615             :     Word16 SWB_fenv_fx[SWB_FENV];
     616             :     Word32 ener_low_fx;
     617           8 :     Word32 energy_fbe_fb_fx = 0;
     618           8 :     move32();
     619             :     Word16 fb_ener_adjust_fx;
     620           8 :     Word16 ener_adjust_quan_fx = 0;
     621           8 :     move16();
     622           8 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
     623             : 
     624             :     /*---------------------------------------------------------------------*
     625             :      * Delay the original input signal to be synchronized with ACELP core synthesis
     626             :      *---------------------------------------------------------------------*/
     627           8 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     628             :     {
     629           0 :         inner_frame = L_FRAME48k;
     630           0 :         move16();
     631           0 :         inner_Fs = 48000;
     632           0 :         move32();
     633             :     }
     634             :     ELSE
     635             :     {
     636           8 :         inner_frame = L_FRAME32k;
     637           8 :         move16();
     638           8 :         inner_Fs = 32000;
     639           8 :         move32();
     640             :     }
     641             : 
     642           8 :     set16_fx( old_input_fx, 0, add( NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ), inner_frame ) );
     643             : 
     644           8 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     645             :     {
     646           8 :         Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
     647           8 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     648           8 :         Sample_Delay_LP = NS2SA( 12800, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS );
     649             : 
     650           8 :         delay_input_signal_fx( hBWE_FD->old_input_lp_fx, old_input_lp_fx, &old_input_12k8_fx[L_INP_MEM], Sample_Delay_LP, L_FRAME, &hBWE_FD->prev_Q_input_lp, &Q_slb_speech );
     651             :     }
     652             :     ELSE
     653             :     {
     654           0 :         Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
     655           0 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     656           0 :         Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS );
     657             : 
     658           0 :         delay_input_signal_fx( hBWE_FD->old_input_lp_fx, old_input_lp_fx, &old_input_16k_fx[L_INP_MEM], Sample_Delay_LP, L_FRAME16k, &hBWE_FD->prev_Q_input_lp, &Q_slb_speech );
     659             :     }
     660           8 :     move16();
     661           8 :     move16();
     662           8 :     move16();
     663             : 
     664           8 :     Copy( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP );
     665           8 :     Copy( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP );
     666           8 :     Copy( shb_speech_fx + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp_fx, Sample_Delay_HP );
     667           8 :     new_input_fx = old_input_fx + Sample_Delay_SWB_BWE;
     668           8 :     Copy( hBWE_FD->old_input_fx, old_input_fx, Sample_Delay_SWB_BWE );
     669           8 :     Copy( new_swb_speech_fx, new_input_fx, inner_frame );
     670           8 :     Copy( old_input_fx + inner_frame, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE );
     671             :     /*----------------------------------------------------------------------*
     672             :      * Calculate tilt of the input signal and the ACELP core synthesis
     673             :      *----------------------------------------------------------------------*/
     674             : 
     675             :     /* tilt returned in Q24 goto to Q11 */
     676           8 :     tilt_nb_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( old_input_lp_fx, Q_slb_speech, st_fx->L_frame ), 3 ) );
     677             : 
     678             :     /*---------------------------------------------------------------------*
     679             :      * SWB BWE encoding
     680             :      * FB BWE encoding
     681             :      *---------------------------------------------------------------------*/
     682             : 
     683           8 :     new_input_fx_exp = 0;
     684           8 :     move16();
     685             :     /* MDCT of the core synthesis signal */
     686           8 :     wtda_fx( old_input_fx, &new_input_fx_exp, L_old_input_fx, hBWE_FD->L_old_wtda_swb_fx,
     687             :              &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */
     688             :              inner_frame );
     689             : 
     690             :     /* DCT of the ACELP core synthesis */
     691           8 :     direct_transform_fx( L_old_input_fx, yorig_32, 0, inner_frame, &new_input_fx_exp, /*st_fx->element_mode*/ EVS_MONO );
     692             : 
     693             :     /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */
     694           8 :     scl = sub( 16 + 8, new_input_fx_exp );
     695             :     /* Possible to Upscale? */
     696           8 :     IF( scl > 0 )
     697             :     {
     698             :         /* Yes */
     699             :         /* Calc Room to Upscale */
     700           8 :         Q_synth = Find_Max_Norm32( yorig_32, inner_frame );
     701             :         /* Stay within MAX_Q_NEW_INPUT */
     702           8 :         scl = s_min( Q_synth, scl );
     703             :     }
     704           8 :     Copy_Scale_sig32_16( yorig_32, yorig_fx, inner_frame, scl );
     705           8 :     Q_synth = add( sub( new_input_fx_exp, 16 ), scl );
     706             : 
     707           8 :     max = 0;
     708           8 :     move16();
     709           8 :     Q_synth_hf = 0;
     710           8 :     move16();
     711           8 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     712             :     {
     713           0 :         scl = 300;
     714             :     }
     715             :     ELSE
     716             :     {
     717           8 :         scl = 240;
     718             :     }
     719           8 :     move16();
     720        3208 :     FOR( i = scl; i < inner_frame; i++ )
     721             :     {
     722        3200 :         max = s_max( max, abs_s( yorig_fx[i] ) );
     723             :     }
     724             : 
     725           8 :     IF( max == 0 )
     726             :     {
     727           0 :         exp = 15;
     728           0 :         move16();
     729             :     }
     730             :     ELSE
     731             :     {
     732           8 :         exp = norm_s( max );
     733             :     }
     734             : 
     735           8 :     Copy_Scale_sig( &yorig_fx[scl], &yorig_fx[scl], sub( inner_frame, scl ), exp );
     736           8 :     Q_synth_hf = add( exp, Q_synth );
     737           8 :     IF( EQ_16( st_fx->last_extl, SWB_BWE ) || EQ_16( st_fx->last_extl, FB_BWE ) )
     738             :     {
     739           6 :         exp = norm_l( st_fx->EnergyLT_fx );
     740           6 :         IF( add( st_fx->EnergyLT_fx_exp, exp ) > shl( sub( Q_synth_hf, 4 ), 1 ) )
     741             :         {
     742           6 :             Q_shb = sub( Q_synth_hf, 4 );
     743           6 :             st_fx->EnergyLT_fx = L_shr( st_fx->EnergyLT_fx, sub( st_fx->EnergyLT_fx_exp, shl( Q_shb, 1 ) ) );
     744             :         }
     745             :         ELSE
     746             :         {
     747           0 :             Q_shb = shr( add( st_fx->EnergyLT_fx_exp, exp ), 1 );
     748           0 :             if ( EQ_16( s_and( exp, 0x0001 ), 1 ) )
     749             :             {
     750           0 :                 exp = sub( exp, 1 );
     751             :             }
     752           0 :             st_fx->EnergyLT_fx = L_shl( st_fx->EnergyLT_fx, exp );
     753           0 :             move32();
     754             :         }
     755             :     }
     756             :     ELSE
     757             :     {
     758           2 :         Q_shb = sub( Q_synth_hf, 4 );
     759             :     }
     760           8 :     Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, Q_shb_speech ) );
     761             :     /* SWB BWE encoding */
     762           8 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     763             :     {
     764           0 :         SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx,
     765             :                              SWB_fenv_fx, tilt_nb_fx, 80, coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth );
     766             :     }
     767             :     ELSE
     768             :     {
     769           8 :         SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx,
     770             :                              SWB_fenv_fx, tilt_nb_fx, 6, coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth );
     771             :     }
     772             : 
     773             :     /* FB BWE encoding */
     774           8 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     775             :     {
     776           0 :         energy_fbe_fb_fx = L_deposit_l( 0 );
     777           0 :         FOR( i = FB_BAND_BEGIN; i < FB_BAND_END; i++ )
     778             :         {
     779           0 :             tmp = shr( yorig_fx[i], 4 );
     780           0 :             energy_fbe_fb_fx = L_mac0( energy_fbe_fb_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */
     781             :         }
     782           0 :         ener_low_fx = 0;
     783           0 :         move16();
     784           0 :         FOR( i = FB_BAND_BEGIN - FB_BAND_WIDTH; i < FB_BAND_BEGIN; i++ )
     785             :         {
     786           0 :             tmp = shr( yorig_fx[i], 4 );
     787           0 :             ener_low_fx = L_mac0( ener_low_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */
     788             :         }
     789             : 
     790           0 :         IF( energy_fbe_fb_fx != 0 )
     791             :         {
     792           0 :             exp = norm_l( energy_fbe_fb_fx );
     793           0 :             frac = extract_h( L_shl( energy_fbe_fb_fx, exp ) );
     794           0 :             tmp = div_s( 16384, frac );             /*15+14-(exp+2*(Q_synth_hf-4)-16) -->45-(exp+2*(Q_synth_hf-4)) */
     795           0 :             L_tmp = Mult_32_16( ener_low_fx, tmp ); /*45-(exp+2*(Q_synth_hf-4)) + 2*(Q_synth_hf-4) - 15 = 30-exp */
     796           0 :             exp1 = norm_l( L_tmp );
     797           0 :             L_tmp = L_shl( L_tmp, exp1 );
     798             :             // exp = 31 - exp1 - ( 30 - exp );
     799           0 :             exp = add( 31 - 30, sub( exp, exp1 ) );
     800           0 :             L_tmp = Isqrt_lc( L_tmp, &exp );                             /*31-exp */
     801           0 :             fb_ener_adjust_fx = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /*Q15 */
     802             :         }
     803             :         ELSE
     804             :         {
     805           0 :             fb_ener_adjust_fx = 0;
     806           0 :             move16();
     807             :         }
     808             : 
     809           0 :         fb_ener_adjust_fx = s_min( fb_ener_adjust_fx, 16384 ); /*Q15 */
     810           0 :         idxGain = usquant_fx( fb_ener_adjust_fx, &ener_adjust_quan_fx, 0, 512, shl( 1, NUM_BITS_FB_FRAMEGAIN ) );
     811             :     }
     812             : 
     813             :     /* write FB BWE frame gain to the bitstream */
     814           8 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     815             :     {
     816           0 :         push_indice( st_fx->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN );
     817             :     }
     818             : 
     819           8 :     return;
     820             : }
     821             : 
     822             : 
     823             : /*==========================================================================*/
     824             : /* FUNCTION      :   static Word16 WB_BWE_fenv_q_fx()            */
     825             : /*--------------------------------------------------------------------------*/
     826             : /* PURPOSE       :   Scalar quantizer routine                */
     827             : /*--------------------------------------------------------------------------*/
     828             : /* INPUT ARGUMENTS  :                            */
     829             : /* Word16 *cb                  i:   quantizer codebook          Q10 */
     830             : /* Word16 cb_length            i:   length of codebook            */
     831             : /* Word16 cb_dim               i:   dimension of codebook          */
     832             : /*--------------------------------------------------------------------------*/
     833             : /* OUTPUT ARGUMENTS :                            */
     834             : /*--------------------------------------------------------------------------*/
     835             : /* INPUT/OUTPUT ARGUMENTS :                          */
     836             : /* Word16 *x                   i/o: energy of WB envelop        Q10 */
     837             : /*--------------------------------------------------------------------------*/
     838             : /* RETURN ARGUMENTS :                            */
     839             : /*           _ None                          */
     840             : /*--------------------------------------------------------------------------*/
     841             : /*                                      */
     842             : /*==========================================================================*/
     843        4241 : static Word16 WB_BWE_fenv_q_fx(                         /* o:   quantized gain index  */
     844             :                                 Word16 *x,              /* i/o: energy of WB envelop  Q10*/
     845             :                                 const Word16 *cb,       /* i:   quantizer codebook   Q10 */
     846             :                                 const Word16 cb_length, /* i:   length of codebook    */
     847             :                                 const Word16 cb_dim     /* i:   dimension of codebook */
     848             : )
     849             : {
     850        4241 :     Word16 i, j, indx = 0;
     851        4241 :     move16();
     852             :     Word32 dist, min_dist;
     853        4241 :     const Word16 *pit = cb; /*Q10 */
     854             :     Word16 tmp;
     855             :     Word32 L_tmp;
     856             : 
     857        4241 :     min_dist = MAX_32;
     858        4241 :     move32();
     859      139953 :     FOR( i = 0; i < cb_length; i++ )
     860             :     {
     861      135712 :         dist = L_deposit_l( 0 );
     862      407136 :         FOR( j = 0; j < cb_dim; j++ )
     863             :         {
     864      271424 :             tmp = sub_sat( x[j], *pit ); /*Q10 */
     865      271424 :             L_tmp = L_mult0( tmp, tmp ); /*Q(10+10)->Q20 */
     866      271424 :             dist = L_add_sat( dist, L_tmp );
     867      271424 :             pit++;
     868             :         }
     869             : 
     870      135712 :         IF( LT_32( dist, min_dist ) )
     871             :         {
     872       18558 :             min_dist = dist;
     873       18558 :             move32();
     874       18558 :             indx = i;
     875       18558 :             move16();
     876             :         }
     877             :     }
     878             : 
     879       12723 :     FOR( j = 0; j < cb_dim; j++ )
     880             :     {
     881        8482 :         x[j] = cb[cb_dim * indx + j];
     882        8482 :         move16();
     883             :     }
     884             : 
     885        4241 :     return ( indx );
     886             : }
     887             : 
     888       17793 : static void get_normalize_spec_fx(
     889             :     const Word16 core,       /* i  : core selected            */
     890             :     const Word16 extl,       /* i  : extension layer selected */
     891             :     const Word16 mode,       /* i  : SHB BWE class            */
     892             :     const Word16 core_type,  /* i  : coding type              */
     893             :     const Word16 *org_fx,    /* i  : input spectrum           */
     894             :     Word16 *SWB_signal,      /* o  : output spectrum          */
     895             :     Word16 *prev_L_swb_norm, /* i  : previous norm. len       */
     896             :     const Word16 offset,     /* i  : frequency offset         */
     897             :     Word16 Q_new_lf )
     898             : {
     899             :     Word16 n_freq, L_swb_norm;
     900             :     Word32 envelope[L_FRAME32k];
     901             :     Word16 frq_end;
     902             :     Word16 tmp;
     903             :     Word16 exp;
     904             :     Word32 L_tmp_m;
     905             : 
     906       17793 :     set16_fx( SWB_signal, 0, add( HQ_GENERIC_HIGH0, offset ) );
     907       17793 :     calc_normal_length_fx( core, org_fx, mode, extl, &L_swb_norm, prev_L_swb_norm, Q_new_lf );
     908             : 
     909       17793 :     test();
     910       17793 :     IF( EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
     911             :     {
     912       13552 :         IF( EQ_16( mode, HARMONIC ) )
     913             :         {
     914         340 :             Copy( org_fx, &SWB_signal[add( 240, offset )], 240 );
     915         340 :             Copy( &org_fx[128], &SWB_signal[add( 480, offset )], 80 );
     916             :         }
     917             :         ELSE
     918             :         {
     919       13212 :             Copy( &org_fx[112], &SWB_signal[add( 240, offset )], 128 );
     920       13212 :             Copy( &org_fx[112], &SWB_signal[add( 368, offset )], 128 );
     921       13212 :             Copy( &org_fx[176], &SWB_signal[add( 496, offset )], 64 );
     922             :         }
     923       13552 :         frq_end = add( 560, offset );
     924             :     }
     925        4241 :     ELSE IF( EQ_16( extl, WB_BWE ) )
     926             :     {
     927        4241 :         IF( core_type == 0 )
     928             :         {
     929          11 :             Copy( &org_fx[160], &SWB_signal[240], 80 );
     930             :         }
     931             :         ELSE
     932             :         {
     933        4230 :             Copy( &org_fx[80], &SWB_signal[240], 80 );
     934             :         }
     935        4241 :         frq_end = L_FRAME16k;
     936        4241 :         move16();
     937             :     }
     938             :     ELSE
     939             :     {
     940           0 :         Copy( org_fx + HQ_GENERIC_OFFSET, SWB_signal + add( HQ_GENERIC_HIGH0, offset ), HQ_GENERIC_LEN0 );
     941           0 :         Copy( org_fx + HQ_GENERIC_OFFSET, SWB_signal + add( HQ_GENERIC_HIGH1, offset ), HQ_GENERIC_LEN0 );
     942           0 :         IF( EQ_16( offset, HQ_GENERIC_FOFFSET_24K4 ) )
     943             :         {
     944           0 :             Copy( org_fx + HQ_GENERIC_LOW0, SWB_signal + add( HQ_GENERIC_HIGH2, offset ), sub( HQ_GENERIC_END_FREQ, HQ_GENERIC_HIGH2 ) );
     945             :         }
     946           0 :         frq_end = sub( L_FRAME32k, offset );
     947             :     }
     948             : 
     949             :     /* calculate envelope */
     950       17793 :     calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, sub( frq_end, offset ), offset );
     951             : 
     952             :     /* Normalize with envelope */
     953     4693713 :     FOR( n_freq = add( swb_bwe_subband[0], offset ); n_freq < frq_end; n_freq++ )
     954             :     {
     955     4675920 :         IF( envelope[n_freq] != 0 )
     956             :         {
     957     4551676 :             exp = norm_l( envelope[n_freq] );
     958     4551676 :             tmp = extract_h( L_shl( envelope[n_freq], exp ) );
     959     4551676 :             exp = sub( sub( 30, exp ), Q_new_lf );
     960     4551676 :             tmp = div_s( 16384, tmp );                                                   /*Q(15+exp) */
     961     4551676 :             L_tmp_m = L_shr( L_mult0( SWB_signal[n_freq], tmp ), add( exp, Q_new_lf ) ); /*Q15 */
     962     4551676 :             SWB_signal[n_freq] = extract_l( L_tmp_m );                                   /*Q15 */
     963     4551676 :             move16();
     964             :         }
     965             :         ELSE
     966             :         {
     967      124244 :             SWB_signal[n_freq] = 1;
     968      124244 :             move16();
     969             :         }
     970             :     }
     971             : 
     972       17793 :     return;
     973             : }
     974             : 
     975             : /*-------------------------------------------------------------------*
     976             :  * FD_BWE_class()
     977             :  *
     978             :  * classify signal of above 6.4kHz, can be used for WB/SWB switch
     979             :  *-------------------------------------------------------------------*/
     980             : 
     981       17793 : static Word16 FD_BWE_class_fx(                          /* o  : FD BWE class        */
     982             :                                const Word16 *fSpectrum, /* i  : input spectrum      */
     983             :                                const Word32 fGain,      /* i  : global gain         */
     984             :                                const Word16 tilt_nb,    /* i  : BWE tilt            */
     985             :                                Word16 Q_syn,
     986             :                                Word16 Q_shb,
     987             :                                Encoder_State *st_fx /* i/o: Encoder structure   */
     988             : )
     989             : {
     990       17793 :     Word16 i, j, k, noise, sharpMod = 0;
     991       17793 :     move16();
     992             :     Word16 peak, mag;
     993             :     Word32 mean[20];
     994             :     Word16 sharpPeak;
     995       17793 :     const Word16 *input_hi = 0;
     996             :     Word16 sharp;
     997       17793 :     Word16 gain_tmp = 0;
     998       17793 :     move16();
     999             :     Word16 mode;
    1000             :     Word32 L_meanH, L_mean_d, L_tmp;
    1001             :     Word16 sharplimit;
    1002             :     Word16 numsharp, num, den;
    1003             :     Word16 numharmonic, tmp, expn, expd, scale;
    1004       17793 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    1005             : 
    1006       17793 :     mode = NORMAL;
    1007       17793 :     move16();
    1008       17793 :     k = 0;
    1009       17793 :     move16();
    1010       17793 :     noise = 0;
    1011       17793 :     move16();
    1012       17793 :     sharpPeak = 0;
    1013       17793 :     move16();
    1014       17793 :     numsharp = 0;
    1015       17793 :     move16();
    1016       17793 :     numharmonic = 4;
    1017       17793 :     move16();
    1018       17793 :     sharplimit = 10;
    1019       17793 :     move16();
    1020             : 
    1021       17793 :     L_mean_d = 0L; /* to avoid compilation warnings */
    1022       17793 :     move32();
    1023             : 
    1024       17793 :     test();
    1025       17793 :     IF( EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
    1026             :     {
    1027       13552 :         input_hi = &fSpectrum[256];
    1028       13552 :         numsharp = NUM_SHARP;
    1029       13552 :         move16();
    1030             : 
    1031       13552 :         test();
    1032       13552 :         test();
    1033       13552 :         test();
    1034       13552 :         IF( ( EQ_16( st_fx->last_extl, SWB_BWE ) && EQ_16( st_fx->extl, SWB_BWE ) ) || ( EQ_16( st_fx->last_extl, FB_BWE ) && EQ_16( st_fx->extl, FB_BWE ) ) )
    1035             :         {
    1036       11142 :             IF( hBWE_FD->prev_global_gain_fx == 0 )
    1037             :             {
    1038         347 :                 gain_tmp = round_fx_sat( L_shl_sat( fGain, 30 ) ); /*Q14 */
    1039             :             }
    1040             :             ELSE
    1041             :             {
    1042       10795 :                 expn = norm_l( fGain );
    1043       10795 :                 num = extract_h( L_shl( fGain, expn ) );
    1044       10795 :                 expn = sub( sub( 30, expn ), shl( Q_shb, 1 ) );
    1045             : 
    1046       10795 :                 expd = norm_l( hBWE_FD->prev_global_gain_fx );
    1047       10795 :                 den = extract_h( L_shl( hBWE_FD->prev_global_gain_fx, expd ) );
    1048       10795 :                 expd = sub( sub( 30, expd ), shl( st_fx->prev_Q_shb, 1 ) );
    1049             : 
    1050       10795 :                 scale = shr( sub( den, num ), 15 );
    1051       10795 :                 num = shl_sat( num, scale );
    1052       10795 :                 expn = sub( expn, scale );
    1053             : 
    1054       10795 :                 tmp = div_s( num, den );
    1055       10795 :                 expn = sub( expn, expd );
    1056       10795 :                 gain_tmp = shl_sat( tmp, sub( expn, 1 ) ); /*Q14 */
    1057             :             }
    1058       11142 :             test();
    1059       11142 :             IF( EQ_16( hBWE_FD->prev_mode, TRANSIENT ) )
    1060             :             {
    1061          45 :                 numharmonic = shl( numharmonic, 1 );
    1062             :             }
    1063       11097 :             ELSE IF( EQ_16( hBWE_FD->prev_mode, NORMAL ) || hBWE_FD->prev_mode == NOISE )
    1064             :             {
    1065       10875 :                 numharmonic = add( shr( numharmonic, 1 ), numharmonic );
    1066             :             }
    1067             :         }
    1068             :         ELSE
    1069             :         {
    1070        2410 :             gain_tmp = 16384;
    1071        2410 :             move16();
    1072        2410 :             IF( EQ_16( hBWE_FD->prev_mode, HARMONIC ) )
    1073             :             {
    1074           0 :                 numharmonic = shr( numharmonic, 1 );
    1075           0 :                 sharplimit = shr( sharplimit, 1 );
    1076             :             }
    1077             :             ELSE
    1078             :             {
    1079        2410 :                 numharmonic = shl( numharmonic, 1 );
    1080        2410 :                 sharplimit = shl( sharplimit, 1 );
    1081             :             }
    1082             :         }
    1083             :     }
    1084        4241 :     ELSE IF( EQ_16( st_fx->extl, WB_BWE ) )
    1085             :     {
    1086        4241 :         input_hi = &fSpectrum[224];
    1087        4241 :         numsharp = 3;
    1088        4241 :         move16();
    1089             : 
    1090        4241 :         IF( EQ_16( hBWE_FD->prev_mode, HARMONIC ) )
    1091             :         {
    1092        1448 :             numharmonic = shr( numharmonic, 2 );
    1093             :         }
    1094             :         ELSE
    1095             :         {
    1096        2793 :             numharmonic = shr( numharmonic, 1 );
    1097             :         }
    1098        4241 :         IF( NE_16( st_fx->last_extl, WB_BWE ) )
    1099             :         {
    1100         125 :             IF( EQ_16( hBWE_FD->prev_mode, HARMONIC ) )
    1101             :             {
    1102           0 :                 sharplimit = shr( sharplimit, 1 );
    1103             :             }
    1104             :             ELSE
    1105             :             {
    1106         125 :                 sharplimit = shl( sharplimit, 1 );
    1107             :             }
    1108             :         }
    1109             :     }
    1110             : 
    1111       17793 :     L_meanH = L_deposit_l( 0 );
    1112      152484 :     FOR( i = 0; i < numsharp; i++ )
    1113             :     {
    1114      134691 :         peak = 0;
    1115      134691 :         move16();
    1116      134691 :         mean[i] = L_deposit_l( 0 );
    1117      134691 :         move32();
    1118             : 
    1119     4444803 :         FOR( j = 0; j < SHARP_WIDTH; j++ )
    1120             :         {
    1121     4310112 :             mag = abs_s( *input_hi );
    1122     4310112 :             if ( GT_16( mag, peak ) )
    1123             :             {
    1124      528106 :                 peak = mag;
    1125      528106 :                 move16(); /*Q_syn */
    1126             :             }
    1127     4310112 :             mean[i] = L_add( mean[i], mag );
    1128     4310112 :             move32(); /*Q_syn */
    1129     4310112 :             input_hi++;
    1130             :         }
    1131             : 
    1132      134691 :         L_meanH = L_add( L_meanH, mean[i] ); /*Q_syn */
    1133             : 
    1134      134691 :         IF( NE_32( mean[i], L_deposit_l( peak ) ) )
    1135             :         {
    1136      131196 :             L_tmp = L_sub( mean[i], peak );       /*Q_syn */
    1137      131196 :             L_tmp = Mpy_32_16_1( L_tmp, 16913 );  /* 1/31->Q19 -> Q_syn+19-15 */
    1138      131196 :             den = extract_l( L_shr( L_tmp, 4 ) ); /*Q_syn */
    1139      131196 :             if ( den == 0 )
    1140             :             {
    1141          60 :                 den = 1;
    1142          60 :                 move16();
    1143             :             }
    1144      131196 :             expd = norm_s( den );
    1145      131196 :             tmp = div_s( shl( 1, sub( 14, expd ) ), den );              /*Q(29-expd-Q_syn) */
    1146      131196 :             L_tmp = L_mult( tmp, peak );                                /*Q(30-expd) */
    1147      131196 :             sharp = round_fx_sat( L_shl_sat( L_tmp, sub( expd, 4 ) ) ); /*Q10 */
    1148             :         }
    1149             :         ELSE
    1150             :         {
    1151        3495 :             sharp = 0;
    1152        3495 :             move16();
    1153             :         }
    1154             : 
    1155      134691 :         test();
    1156      134691 :         IF( GT_16( sharp, 4608 ) && GT_16( peak, shl_sat( 1, add( Q_syn, 3 ) ) ) )
    1157             :         {
    1158       19402 :             k = add( k, 1 );
    1159       19402 :             move16();
    1160             :         }
    1161      115289 :         ELSE IF( LT_16( sharp, 3072 ) )
    1162             :         {
    1163       47670 :             noise = add( noise, 1 );
    1164       47670 :             move16();
    1165             :         }
    1166             : 
    1167      134691 :         if ( GT_16( sharp, sharpPeak ) )
    1168             :         {
    1169       43318 :             sharpPeak = sharp;
    1170       43318 :             move16();
    1171             :         }
    1172             :     }
    1173             : 
    1174       17793 :     test();
    1175       17793 :     IF( EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
    1176             :     {
    1177       13552 :         test();
    1178       13552 :         test();
    1179       13552 :         test();
    1180       13552 :         IF( GE_16( k, numharmonic ) && GT_16( gain_tmp, 8192 ) && LT_16( gain_tmp, 29491 ) && GT_16( sharpPeak, shl( sharplimit, 10 ) ) )
    1181             :         {
    1182         310 :             sharpMod = 1;
    1183             :         }
    1184             :         ELSE
    1185             :         {
    1186       13242 :             sharpMod = 0;
    1187             :         }
    1188       13552 :         move16();
    1189             : 
    1190       13552 :         L_meanH = Mpy_32_16_1( L_meanH, 29127 ); /*Q_syn+8 */
    1191       13552 :         L_mean_d = 0;
    1192       13552 :         move16();
    1193      135520 :         FOR( i = 0; i < NUM_SHARP; i++ )
    1194             :         {
    1195      121968 :             L_tmp = L_sub( L_shl( mean[i], 8 - 5 ), L_meanH ); /*Q_syn+8 */
    1196      121968 :             L_mean_d = L_add( L_mean_d, L_abs( L_tmp ) );      /*Q_syn+8 */
    1197             :         }
    1198             :     }
    1199        4241 :     ELSE IF( EQ_16( st_fx->extl, WB_BWE ) )
    1200             :     {
    1201        4241 :         test();
    1202        4241 :         IF( GE_16( k, numharmonic ) && GT_16( sharpPeak, shl( sharplimit, 10 ) ) )
    1203             :         {
    1204         998 :             sharpMod = 1;
    1205             :         }
    1206             :         ELSE
    1207             :         {
    1208        3243 :             sharpMod = 0;
    1209             :         }
    1210        4241 :         move16();
    1211             :     }
    1212             : 
    1213       17793 :     test();
    1214       17793 :     test();
    1215       17793 :     IF( sharpMod && LT_16( hBWE_FD->modeCount, 12 ) )
    1216             :     {
    1217         970 :         hBWE_FD->modeCount = add( hBWE_FD->modeCount, 1 );
    1218         970 :         move16();
    1219             :     }
    1220       16823 :     ELSE IF( sharpMod == 0 && hBWE_FD->modeCount > 0 )
    1221             :     {
    1222         716 :         hBWE_FD->modeCount = sub( hBWE_FD->modeCount, 1 );
    1223         716 :         move16();
    1224             :     }
    1225             : 
    1226       17793 :     if ( GE_16( hBWE_FD->modeCount, 2 ) )
    1227             :     {
    1228        1543 :         sharpMod = 1;
    1229        1543 :         move16();
    1230             :     }
    1231             : 
    1232       17793 :     test();
    1233       17793 :     IF( sharpMod )
    1234             :     {
    1235        1794 :         mode = HARMONIC;
    1236        1794 :         move16();
    1237             :     }
    1238       15999 :     ELSE IF( EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
    1239             :     {
    1240       13212 :         L_tmp = Mpy_32_16_1( L_mean_d, 6827 ); /*Q_syn+8 ; 1/4.8 in Q15 */
    1241             : 
    1242       13212 :         test();
    1243       13212 :         test();
    1244       13212 :         test();
    1245       13212 :         if ( GT_16( noise, 4 ) && ( LT_32( L_tmp, L_meanH ) || L_meanH == 0 ) && LT_16( tilt_nb, 10240 ) )
    1246             :         {
    1247        1107 :             mode = NOISE;
    1248        1107 :             move16();
    1249             :         }
    1250             :     }
    1251             : 
    1252       17793 :     return ( mode );
    1253             : }
    1254             : 
    1255             : /*-------------------------------------------------------------------*
    1256             :  * freq_weights_fx()
    1257             :  *
    1258             :  *-------------------------------------------------------------------*/
    1259       17051 : static void freq_weights_fx(
    1260             :     const Word16 Band_Ener[],   /* i  : Band energy              Q8 */
    1261             :     const Word16 f_weighting[], /* i  : weigting coefs.         Q15 */
    1262             :     Word16 w_env[],             /* o  : Freq. weighting         Q13 */
    1263             :     const Word16 Nbands         /* i  : Number of bands             */
    1264             : )
    1265             : {
    1266             :     Word16 i;
    1267             :     Word16 tmp, tmp1, w1[SWB_FENV], w2[SWB_FENV];
    1268             :     Word16 min_b, max_b;
    1269             :     Word32 L_tmp;
    1270             :     Word16 exp;
    1271             : 
    1272             :     /* Find Max band energy */
    1273       17051 :     min_b = Band_Ener[0];
    1274       17051 :     move16();
    1275       17051 :     max_b = Band_Ener[0];
    1276       17051 :     move16();
    1277      238712 :     FOR( i = 1; i < Nbands; i++ )
    1278             :     {
    1279      221661 :         if ( LT_16( Band_Ener[i], min_b ) )
    1280             :         {
    1281       68618 :             min_b = Band_Ener[i];
    1282       68618 :             move16(); /*Q8 */
    1283             :         }
    1284             : 
    1285      221661 :         if ( GT_16( Band_Ener[i], max_b ) )
    1286             :         {
    1287       22611 :             max_b = Band_Ener[i];
    1288       22611 :             move16(); /*Q8 */
    1289             :         }
    1290             :     }
    1291             : 
    1292             :     /* Find weighting function */
    1293       17051 :     tmp = sub( max_b, min_b ); /*Q8 */
    1294       17051 :     IF( tmp != 0 )
    1295             :     {
    1296       16663 :         exp = norm_s( tmp );
    1297       16663 :         tmp = div_s( shl( 1, sub( 14, exp ) ), tmp ); /*(21-exp) */
    1298       16663 :         tmp = shl_sat( tmp, sub( exp, 6 ) );          /*Q15 */
    1299             :     }
    1300             :     ELSE
    1301             :     {
    1302         388 :         tmp = 32767;
    1303         388 :         move16();
    1304             :     }
    1305             : 
    1306      255763 :     FOR( i = 0; i < Nbands; i++ )
    1307             :     {
    1308      238712 :         tmp1 = sub( Band_Ener[i], min_b );
    1309      238712 :         L_tmp = L_mult( tmp1, tmp );      /*Q24 */
    1310      238712 :         L_tmp = L_add( L_tmp, 16777216 ); /*Q24 */
    1311      238712 :         L_tmp = L_shl( L_tmp, 5 );        /*Q29 */
    1312      238712 :         w1[i] = round_fx( L_tmp );        /*Q13 */
    1313      238712 :         move16();
    1314      238712 :         w2[i] = f_weighting[i];
    1315      238712 :         move16(); /*Q15                         */ /*1~0.75*/
    1316      238712 :         w_env[i] = mult_r( w1[i], w2[i] );
    1317      238712 :         move16(); /*Q13 */
    1318             :     }
    1319             : 
    1320       17051 :     return;
    1321             : }
    1322             : 
    1323             : /*-------------------------------------------------------------------*
    1324             :  * vqWithCand_w_fx()
    1325             :  *
    1326             :  *-------------------------------------------------------------------*/
    1327       17196 : static void vqWithCand_w_fx(
    1328             :     const Word16 *x,              /* i  : input vector                             Q8 */
    1329             :     const Word16 *E_ROM_dico,     /* i  : codebook                                 Q8 */
    1330             :     const Word16 dim,             /* i  : codebook dimension                          */
    1331             :     const Word16 E_ROM_dico_size, /* i  : codebook size                               */
    1332             :     Word16 *index,                /* o  : survivors indices                           */
    1333             :     const Word16 surv,            /* i  : survivor number                             */
    1334             :     Word32 dist_min[],            /* o  : minimum distortion                       Q5 */
    1335             :     const Word16 *w,              /* i  : weighting                                Q13*/
    1336             :     const Word16 flag             /* i  : flag indicationg weighted distortion metric */
    1337             : )
    1338             : {
    1339             :     Word16 i, j, k, l;
    1340             :     const Word16 *p_E_ROM_dico;
    1341             :     Word16 dist, temp1;
    1342             :     Word32 L_dist, L_tmp;
    1343             : 
    1344       17196 :     IF( flag )
    1345             :     {
    1346       17051 :         set32_fx( dist_min, MAX_32, surv ); /* FLT_MAX */
    1347             : 
    1348       51153 :         FOR( i = 0; i < surv; i++ )
    1349             :         {
    1350       34102 :             index[i] = i;
    1351       34102 :             move16();
    1352             :         }
    1353             : 
    1354       17051 :         p_E_ROM_dico = E_ROM_dico;
    1355       17051 :         move16();
    1356             : 
    1357      562683 :         FOR( i = 0; i < E_ROM_dico_size; i++ )
    1358             :         {
    1359      545632 :             dist = sub_sat( x[0], *p_E_ROM_dico++ ); /*Q8 */
    1360      545632 :             L_dist = L_mult( dist, w[0] );           /*Q22 */
    1361      545632 :             L_dist = Mult_32_16( L_dist, dist );     /*Q15 */
    1362      545632 :             L_dist = L_shr( L_dist, 10 );            /*Q5 */
    1363             : 
    1364     3819424 :             FOR( j = 1; j < dim; j++ )
    1365             :             {
    1366     3273792 :                 temp1 = sub_sat( x[j], *p_E_ROM_dico++ );
    1367     3273792 :                 L_tmp = L_mult( temp1, w[j] );                /*Q22 */
    1368     3273792 :                 L_tmp = Mult_32_16( L_tmp, temp1 );           /*Q15 */
    1369     3273792 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) ); /*Q5 */
    1370             :             }
    1371             : 
    1372     1450602 :             FOR( k = 0; k < surv; k++ )
    1373             :             {
    1374     1022021 :                 IF( LT_32( L_dist, dist_min[k] ) )
    1375             :                 {
    1376      186294 :                     FOR( l = sub( surv, 1 ); l > k; l-- )
    1377             :                     {
    1378       69243 :                         dist_min[l] = dist_min[l - 1];
    1379       69243 :                         move32();
    1380       69243 :                         index[l] = index[l - 1];
    1381       69243 :                         move16();
    1382             :                     }
    1383      117051 :                     dist_min[k] = L_dist;
    1384      117051 :                     move32();
    1385      117051 :                     index[k] = i;
    1386      117051 :                     move16();
    1387      117051 :                     BREAK;
    1388             :                 }
    1389             :             }
    1390             :         }
    1391             :     }
    1392             :     ELSE
    1393             :     {
    1394         145 :         set32_fx( dist_min, MAX_32, surv ); /* FLT_MAX */
    1395             : 
    1396         580 :         FOR( i = 0; i < surv; i++ )
    1397             :         {
    1398         435 :             index[i] = i;
    1399         435 :             move16();
    1400             :         }
    1401             : 
    1402         145 :         p_E_ROM_dico = E_ROM_dico;
    1403         145 :         move16();
    1404             : 
    1405       18705 :         FOR( i = 0; i < E_ROM_dico_size; i++ )
    1406             :         {
    1407       18560 :             dist = sub_sat( x[0], *p_E_ROM_dico++ ); /*Q8 */
    1408       18560 :             L_dist = L_mult_sat( dist, dist );       /*Q17 */
    1409       18560 :             L_dist = L_shr( L_dist, 12 );            /*Q5 */
    1410             : 
    1411       37120 :             FOR( j = 1; j < dim; j++ )
    1412             :             {
    1413       18560 :                 temp1 = sub( x[j], *p_E_ROM_dico++ );         /*Q8 */
    1414       18560 :                 L_tmp = L_mult( temp1, temp1 );               /*Q17 */
    1415       18560 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 12 ) ); /*Q5 */
    1416             :             }
    1417             : 
    1418       70212 :             FOR( k = 0; k < surv; k++ )
    1419             :             {
    1420       53475 :                 IF( LT_32( L_dist, dist_min[k] ) )
    1421             :                 {
    1422        4028 :                     FOR( l = sub( surv, 1 ); l > k; l-- )
    1423             :                     {
    1424        2205 :                         dist_min[l] = dist_min[l - 1];
    1425        2205 :                         move32();
    1426        2205 :                         index[l] = index[l - 1];
    1427        2205 :                         move16();
    1428             :                     }
    1429        1823 :                     dist_min[k] = L_dist;
    1430        1823 :                     move32();
    1431        1823 :                     index[k] = i;
    1432        1823 :                     move16();
    1433        1823 :                     BREAK;
    1434             :                 }
    1435             :             }
    1436             :         }
    1437             :     }
    1438             : 
    1439       17196 :     return;
    1440             : }
    1441             : 
    1442             : /*-------------------------------------------------------------------*
    1443             :  * vqSimple_w_fx()
    1444             :  *
    1445             :  *-------------------------------------------------------------------*/
    1446             : 
    1447      139526 : static Word16 vqSimple_w_fx(
    1448             :     const Word16 *x,  /* i  : input for quantizer                     Q8  */
    1449             :     Word16 *y,        /* i  : quantized value                         Q8  */
    1450             :     const Word16 *cb, /* i  : codebooks                               Q8  */
    1451             :     const Word16 *w,  /* i  : weight                                  Q13 */
    1452             :     const Word16 dim, /* i  : dimension                                   */
    1453             :     const Word16 l,   /* i  : number of candidates                        */
    1454             :     const Word16 flag /* i  : flag indicationg weighted distortion metric */
    1455             : )
    1456             : {
    1457             :     Word16 i, j, index;
    1458             :     const Word16 *cbP;
    1459             :     Word16 dist, temp;
    1460             :     Word32 L_dist, L_tmp, L_dist_min;
    1461             : 
    1462      139526 :     index = 0;
    1463      139526 :     move16();
    1464      139526 :     L_dist_min = L_add( MAX_32, 0 ); /* FLT_MAX */
    1465      139526 :     cbP = cb;
    1466      139526 :     move16();
    1467      139526 :     IF( flag )
    1468             :     {
    1469     9957720 :         FOR( i = 0; i < l; i++ )
    1470             :         {
    1471             :             /*dist = x[0] - *cbP++; */
    1472             :             /*dist *= (dist * w[0]); */
    1473     9821312 :             dist = sub( x[0], *cbP++ );          /*Q8 */
    1474     9821312 :             L_dist = L_mult( dist, w[0] );       /*Q22 */
    1475     9821312 :             L_dist = Mult_32_16( L_dist, dist ); /*Q15 */
    1476     9821312 :             L_dist = L_shr( L_dist, 10 );        /*Q5 */
    1477             : 
    1478    33828864 :             FOR( j = 1; j < dim; j++ )
    1479             :             {
    1480             :                 /*temp = x[j] - *cbP++; */
    1481             :                 /*dist += temp * temp * w[j]; */
    1482    24007552 :                 temp = sub( x[j], *cbP++ );
    1483    24007552 :                 L_tmp = L_mult( temp, w[j] );                 /*Q22 */
    1484    24007552 :                 L_tmp = Mult_32_16( L_tmp, temp );            /*Q15 */
    1485    24007552 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) ); /*Q5 */
    1486             :             }
    1487     9821312 :             IF( LT_32( L_dist, L_dist_min ) )
    1488             :             {
    1489      622809 :                 L_dist_min = L_add( L_dist, 0 ); /*Q5 */
    1490      622809 :                 index = i;
    1491      622809 :                 move16();
    1492             :             }
    1493             :         }
    1494             :     }
    1495             :     ELSE
    1496             :     {
    1497      116814 :         FOR( i = 0; i < l; i++ )
    1498             :         {
    1499             :             /*dist = x[0] - *cbP++; */
    1500      113696 :             dist = sub( x[0], *cbP++ );
    1501             :             /*dist *= dist; */
    1502      113696 :             L_dist = L_mult( dist, dist ); /*Q17 */
    1503      113696 :             L_dist = L_shr( L_dist, 12 );
    1504             : 
    1505      313248 :             FOR( j = 1; j < dim; j++ )
    1506             :             {
    1507             :                 /*temp = x[j] - *cbP++; */
    1508      199552 :                 temp = sub( x[j], *cbP++ );
    1509             :                 /*dist += temp * temp; */
    1510      199552 :                 L_tmp = L_mult( temp, temp );                 /*Q17 */
    1511      199552 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 12 ) ); /*Q5 */
    1512             :             }
    1513      113696 :             IF( LT_32( L_dist, L_dist_min ) )
    1514             :             {
    1515       10920 :                 L_dist_min = L_add( L_dist, 0 );
    1516       10920 :                 index = i;
    1517       10920 :                 move16();
    1518             :             }
    1519             :         }
    1520             :     }
    1521             : 
    1522             : 
    1523             :     /* Reading the selected vector */
    1524      139526 :     Copy( &cb[index * dim], y, dim );
    1525             : 
    1526      139526 :     return ( index );
    1527             : }
    1528             : 
    1529             : 
    1530             : /*-------------------------------------------------------------------*
    1531             :  * MSVQ_Interpol_Tran_fx()
    1532             :  *
    1533             :  *-------------------------------------------------------------------*/
    1534         145 : static void MSVQ_Interpol_Tran_fx(
    1535             :     Word16 *SWB_env_energy, /* i/o  : (original/quantized) energy   Q8 */
    1536             :     Word16 *indice          /* o    : quantized index                  */
    1537             : )
    1538             : 
    1539             : {
    1540             :     Word16 k, n_band, candInd[N_CAND_TR], ind_tmp[2], tmp;
    1541             :     Word16 env_temp11[SWB_FENV_TRANS / 2], env_temp12[SWB_FENV_TRANS / 2];
    1542             :     Word16 tmp_q;
    1543             :     Word16 quant_tmp[SWB_FENV_TRANS], quant_tmp2[SWB_FENV_TRANS];
    1544             :     Word16 quant_select[SWB_FENV_TRANS];
    1545             :     Word32 L_tmp, L_dist, L_minDist, distCand[N_CAND_TR];
    1546             : 
    1547             :     /* Extract target vector */
    1548         435 :     FOR( n_band = 0; n_band < DIM_TR1; n_band++ )
    1549             :     {
    1550         290 :         env_temp11[n_band] = SWB_env_energy[2 * n_band];
    1551         290 :         move16(); /*Q8 */
    1552         290 :         env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
    1553         290 :         move16(); /*Q8 */
    1554             :     }
    1555             : 
    1556         145 :     vqWithCand_w_fx( env_temp11, Env_TR_Cdbk1_fx, DIM_TR1, N_CB_TR1, candInd, N_CAND_TR, distCand, NULL, 0 );
    1557             : 
    1558         145 :     L_minDist = L_add( MAX_32, 0 ); /* FLT_MAX */
    1559             : 
    1560         580 :     FOR( k = 0; k < N_CAND_TR; k++ )
    1561             :     {
    1562        1305 :         FOR( n_band = 0; n_band < DIM_TR1; n_band++ )
    1563             :         {
    1564         870 :             quant_tmp[n_band] = Env_TR_Cdbk1_fx[add( shl( candInd[k], 1 ), n_band )];
    1565         870 :             move16(); /*DIM_TR1 == 2*/
    1566             :         }
    1567             : 
    1568         870 :         FOR( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
    1569             :         {
    1570             :             /*quant_tmp2[n_band] = env_temp12[n_band] - ((quant_tmp[n_band]+quant_tmp[n_band+1])/2.f); */
    1571         435 :             tmp = add( quant_tmp[n_band], quant_tmp[n_band + 1] ); /*Q8 */
    1572         435 :             tmp = shr( tmp, 1 );
    1573         435 :             quant_tmp2[n_band] = sub( env_temp12[n_band], tmp );
    1574         435 :             move16(); /*Q8 */
    1575             :         }
    1576             :         /*quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band]; */
    1577         435 :         quant_tmp2[n_band] = sub( env_temp12[n_band], quant_tmp[n_band] );
    1578         435 :         move16(); /*Q8 */
    1579         435 :         ind_tmp[0] = vqSimple_w_fx( quant_tmp2, quant_tmp2, Env_TR_Cdbk2_fx, NULL, DIM_TR2, N_CB_TR2, 0 );
    1580         435 :         move16();
    1581             : 
    1582        1305 :         FOR( n_band = 0; n_band < DIM_TR1; n_band++ )
    1583             :         {
    1584         870 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1585         870 :             move16();
    1586             :         }
    1587             : 
    1588         870 :         FOR( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
    1589             :         {
    1590             :             /*quant_select[n_band*2+1] = ((quant_tmp[n_band]+quant_tmp[n_band+1])/2.f) + quant_tmp2[n_band]; */
    1591         435 :             tmp = add( quant_tmp[n_band], quant_tmp[n_band + 1] ); /*Q8 */
    1592         435 :             tmp = shr( tmp, 1 );
    1593         435 :             quant_select[n_band * 2 + 1] = add( tmp, quant_tmp2[n_band] );
    1594         435 :             move16();
    1595             :         }
    1596             :         /*quant_select[n_band*2+1] = quant_tmp[n_band]+quant_tmp2[n_band]; */
    1597         435 :         quant_select[n_band * 2 + 1] = add( quant_tmp[n_band], quant_tmp2[n_band] );
    1598         435 :         move16();
    1599             : 
    1600         435 :         L_dist = L_deposit_l( 0 );
    1601        2175 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1602             :         {
    1603             :             /*tmp_q = SWB_env_energy[n_band] - quant_select[n_band]; */
    1604        1740 :             tmp_q = sub( SWB_env_energy[n_band], quant_select[n_band] );
    1605             :             /*dist += tmp_q*tmp_q; */
    1606        1740 :             L_tmp = L_mult( tmp_q, tmp_q );               /*Q17 */
    1607        1740 :             L_dist = L_add( L_dist, L_shr( L_tmp, 12 ) ); /*Q5 */
    1608             :         }
    1609             : 
    1610             :         /* Check optimal candidate */
    1611         435 :         IF( LT_32( L_dist, L_minDist ) )
    1612             :         {
    1613         161 :             L_minDist = L_add( L_dist, 0 );
    1614         161 :             indice[0] = candInd[k];
    1615         161 :             move16();
    1616         161 :             indice[1] = ind_tmp[0];
    1617         161 :             move16();
    1618             :         }
    1619             :     }
    1620         145 :     return;
    1621             : }
    1622             : 
    1623             : /*-------------------------------------------------------------------*
    1624             :  * MSVQ_Interpol_fx()
    1625             :  *
    1626             :  *-------------------------------------------------------------------*/
    1627             : 
    1628       17050 : static void msvq_interpol_fx(
    1629             :     Word16 *SWB_env_energy, /* i/o  : (original/quantized) energy   Q8*/
    1630             :     Word16 *w_env,          /* i/o  : weighting coffecients         Q13*/
    1631             :     Word16 *indice          /* o    : quantized index               */
    1632             : )
    1633             : {
    1634             :     Word16 k, n_band, n_band2, n_band2p1, candInd[N_CAND], ind_tmp[4];
    1635             :     Word16 tmp_q;
    1636             :     Word16 env_temp11[SWB_FENV / 2], env_temp12[SWB_FENV / 2];
    1637             :     Word16 quant_tmp[SWB_FENV], quant_tmp1[SWB_FENV], quant_tmp2[SWB_FENV];
    1638             :     Word16 quant_select[SWB_FENV], w_env11[SWB_FENV / 2], w_env12[SWB_FENV / 2], tmp;
    1639             :     Word32 L_tmp, distCand[N_CAND], L_dist, L_minDist;
    1640             :     Word16 synth_energy[SWB_FENV];
    1641             : 
    1642             :     /* Extract target vector */
    1643      136400 :     FOR( n_band = 0; n_band < DIM11; n_band++ )
    1644             :     {
    1645      119350 :         n_band2 = shl( n_band, 1 );
    1646      119350 :         n_band2p1 = add( n_band2, 1 );
    1647      119350 :         env_temp11[n_band] = SWB_env_energy[n_band2];
    1648      119350 :         move16(); /*Q8 */
    1649      119350 :         env_temp12[n_band] = SWB_env_energy[n_band2p1];
    1650      119350 :         move16(); /*Q8 */
    1651             : 
    1652      119350 :         w_env11[n_band] = w_env[n_band2];
    1653      119350 :         move16(); /*Q13 */
    1654      119350 :         w_env12[n_band] = w_env[n_band2p1];
    1655      119350 :         move16(); /*Q13 */
    1656             :     }
    1657             : 
    1658       17050 :     vqWithCand_w_fx( env_temp11, EnvCdbk11_fx, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
    1659             : 
    1660       17050 :     L_minDist = L_add( MAX_32, 0 ); /* FLT_MAX */
    1661             : 
    1662       51150 :     FOR( k = 0; k < N_CAND; k++ )
    1663             :     {
    1664      272800 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1665             :         {
    1666      238700 :             quant_tmp1[n_band] = EnvCdbk11_fx[add( i_mult2( candInd[k], DIM11 ), n_band )];
    1667      238700 :             move16(); /*Q8 */
    1668      238700 :             quant_tmp2[n_band] = sub( env_temp11[n_band], quant_tmp1[n_band] );
    1669      238700 :             move16(); /*Q8 */
    1670             :         }
    1671             : 
    1672       34100 :         ind_tmp[0] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk1st_fx, w_env11, DIM1ST, N_CB1ST, 1 );
    1673       34100 :         move16();
    1674       34100 :         ind_tmp[1] = vqSimple_w_fx( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd_fx, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
    1675       34100 :         move16();
    1676             : 
    1677             :         /* Extract vector for odd position */
    1678      272800 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1679             :         {
    1680      238700 :             quant_tmp[n_band] = add_sat( quant_tmp1[n_band], quant_tmp2[n_band] );
    1681      238700 :             move16();
    1682             :         }
    1683             : 
    1684      238700 :         FOR( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1685             :         {
    1686      204600 :             tmp = add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ); /*Q8 */
    1687      204600 :             tmp = shr( tmp, 1 );
    1688      204600 :             quant_tmp2[n_band] = sub( env_temp12[n_band], tmp );
    1689      204600 :             move16(); /*Q8 */
    1690             :         }
    1691             : 
    1692             :         /*quant_tmp2[n_band] = env_temp12[n_band]-quant_tmp[n_band]; */
    1693       34100 :         quant_tmp2[n_band] = sub( env_temp12[n_band], quant_tmp[n_band] );
    1694       34100 :         move16(); /*Q8 */
    1695             : 
    1696       34100 :         ind_tmp[2] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk3rd_fx, w_env12, DIM3RD, N_CB3RD, 1 );
    1697       34100 :         move16();
    1698       34100 :         ind_tmp[3] = vqSimple_w_fx( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk4th_fx, w_env12 + DIM3RD, DIM4TH, N_CB4TH, 1 );
    1699       34100 :         move16();
    1700             : 
    1701      272800 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1702             :         {
    1703      238700 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1704      238700 :             move16(); /*Q8 */
    1705             :         }
    1706             : 
    1707      238700 :         FOR( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1708             :         {
    1709      204600 :             tmp = add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] );
    1710      204600 :             tmp = shr( tmp, 1 );
    1711      204600 :             quant_select[( n_band << 1 ) + 1] = add( tmp, quant_tmp2[n_band] );
    1712      204600 :             move16(); /*Q8 */
    1713             :         }
    1714       34100 :         quant_select[( n_band << 1 ) + 1] = add( quant_tmp[n_band], quant_tmp2[n_band] );
    1715       34100 :         move16(); /*Q8 */
    1716             : 
    1717       34100 :         L_dist = L_deposit_l( 0 );
    1718      511500 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1719             :         {
    1720      477400 :             tmp_q = sub( SWB_env_energy[n_band], quant_select[n_band] ); /*Q8 */
    1721      477400 :             L_tmp = L_mult( tmp_q, tmp_q );                              /*Q17 */
    1722      477400 :             L_tmp = Mult_32_16( L_tmp, w_env[n_band] );                  /*Q15 */
    1723      477400 :             L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) );
    1724             :         }
    1725             : 
    1726             :         /* Check optimal candidate */
    1727       34100 :         IF( LT_32( L_dist, L_minDist ) )
    1728             :         {
    1729       21089 :             L_minDist = L_add( L_dist, 0 );
    1730             : 
    1731       21089 :             Copy( quant_select, synth_energy, SWB_FENV );
    1732             : 
    1733       21089 :             indice[0] = candInd[k];
    1734       21089 :             move16();
    1735       21089 :             indice[1] = ind_tmp[0];
    1736       21089 :             move16();
    1737       21089 :             indice[2] = ind_tmp[1];
    1738       21089 :             move16();
    1739       21089 :             indice[3] = ind_tmp[2];
    1740       21089 :             move16();
    1741       21089 :             indice[4] = ind_tmp[3];
    1742       21089 :             move16();
    1743             :         }
    1744             :     }
    1745             : 
    1746       17050 :     Copy( synth_energy, SWB_env_energy, SWB_FENV );
    1747             : 
    1748       17050 :     return;
    1749             : }
    1750             : 
    1751             : /*-------------------------------------------------------------------*
    1752             :  * msvq_interpol_2_fx()
    1753             :  *
    1754             :  *-------------------------------------------------------------------*/
    1755           1 : static void msvq_interpol_2_fx(
    1756             :     Word16 *hq_generic_fenv, /* i/o: (original/quantized) energy */
    1757             :     const Word16 *w_env,     /* i  : weighting coffecients       */
    1758             :     Word16 *indice,          /* o  : quantized index             */
    1759             :     const Word16 nenv        /* i  : the number of envelopes     */
    1760             : )
    1761             : {
    1762             :     Word16 k, n_band, n_band2, candInd[N_CAND], ind_tmp[4];
    1763             :     Word16 tmp_q;
    1764             :     Word16 env_temp11[SWB_FENV / 2], env_temp12[SWB_FENV / 2];
    1765             :     Word16 quant_tmp[SWB_FENV], quant_tmp1[SWB_FENV], quant_tmp2[SWB_FENV];
    1766             :     Word16 quant_select[SWB_FENV], w_env11[SWB_FENV / 2], w_env12[SWB_FENV / 2];
    1767             :     Word32 L_tmp, distCand[N_CAND], L_dist, L_minDist;
    1768             :     Word16 synth_energy[SWB_FENV];
    1769             : 
    1770             :     /* Extract target vector */
    1771           7 :     FOR( n_band = 0; n_band < DIM11 - 1; n_band++ )
    1772             :     {
    1773           6 :         n_band2 = shl( n_band, 1 );
    1774           6 :         env_temp11[n_band] = hq_generic_fenv[n_band2];
    1775           6 :         move16(); /*Q8 */
    1776           6 :         w_env11[n_band] = w_env[n_band2];
    1777           6 :         move16(); /*Q13 */
    1778             :     }
    1779           1 :     env_temp11[DIM11 - 1] = hq_generic_fenv[2 * ( DIM11 - 2 ) + 1];
    1780           1 :     move16(); /*Q8 */
    1781           1 :     w_env11[DIM11 - 1] = w_env[2 * ( DIM11 - 2 ) + 1];
    1782           1 :     move16(); /*Q13 */
    1783             : 
    1784           1 :     env_temp12[0] = hq_generic_fenv[0];
    1785           1 :     move16(); /*Q8 */
    1786           1 :     w_env12[0] = w_env[0];
    1787           1 :     move16(); /*Q13 */
    1788           6 :     FOR( n_band = 1; n_band < DIM11 - 1; n_band++ )
    1789             :     {
    1790           5 :         n_band2 = sub( shl( n_band, 1 ), 1 );
    1791           5 :         env_temp12[n_band] = hq_generic_fenv[n_band2 /*2*n_band-1*/];
    1792           5 :         move16(); /*Q8 */
    1793           5 :         w_env12[n_band] = w_env[n_band2 /*2*n_band-1*/];
    1794           5 :         move16(); /*Q13 */
    1795             :     }
    1796             : 
    1797           1 :     vqWithCand_w_fx( env_temp11, EnvCdbk11_fx, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
    1798             : 
    1799           1 :     L_minDist = L_add( MAX_32, 0 ); /* FLT_MAX */
    1800           3 :     FOR( k = 0; k < N_CAND; k++ )
    1801             :     {
    1802          16 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1803             :         {
    1804          14 :             quant_tmp1[n_band] = EnvCdbk11_fx[add( i_mult2( candInd[k], DIM11 ), n_band )];
    1805          14 :             move16(); /*Q8 */
    1806          14 :             quant_tmp2[n_band] = sub( env_temp11[n_band], quant_tmp1[n_band] );
    1807          14 :             move16(); /*Q8 */
    1808             :         }
    1809             : 
    1810           2 :         ind_tmp[0] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk1st_fx, w_env11, DIM1ST, N_CB1ST, 1 );
    1811           2 :         move16();
    1812           2 :         ind_tmp[1] = vqSimple_w_fx( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd_fx, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
    1813           2 :         move16();
    1814             : 
    1815             :         /* Extract vector for odd position */
    1816          16 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1817             :         {
    1818          14 :             quant_tmp[n_band] = add( quant_tmp1[n_band], quant_tmp2[n_band] );
    1819          14 :             move16();
    1820             :         }
    1821             : 
    1822           2 :         quant_tmp2[0] = sub( env_temp12[0], quant_tmp[0] );
    1823           2 :         move16();
    1824          12 :         FOR( n_band = 1; n_band < DIM12 - 1; n_band++ )
    1825             :         {
    1826          10 :             tmp_q = add_sat( quant_tmp[n_band - 1], quant_tmp[n_band] );
    1827          10 :             tmp_q = shr( tmp_q, 1 );
    1828          10 :             quant_tmp2[n_band] = sub_sat( env_temp12[n_band], tmp_q );
    1829          10 :             move16();
    1830             :         }
    1831             : 
    1832           2 :         ind_tmp[2] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk3rd_fx, w_env12, DIM3RD, N_CB3RD, 1 );
    1833           2 :         move16();
    1834           2 :         ind_tmp[3] = vqSimple_w_fx( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk3rd_fx, w_env12 + DIM3RD, DIM3RD, N_CB3RD, 1 );
    1835           2 :         move16();
    1836             : 
    1837          14 :         FOR( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1838             :         {
    1839          12 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1840          12 :             move16(); /*Q8 */
    1841             :         }
    1842           2 :         quant_select[11] = quant_tmp[DIM12 - 1];
    1843           2 :         move16(); /*Q8 */
    1844             : 
    1845           2 :         quant_select[0] = add( quant_select[0], quant_tmp2[0] );
    1846           2 :         move16(); /*Q8 */
    1847          12 :         FOR( n_band = 1; n_band < DIM12 - 1; n_band++ )
    1848             :         {
    1849          10 :             tmp_q = add_sat( quant_tmp[n_band - 1], quant_tmp[n_band] );
    1850          10 :             tmp_q = shr( tmp_q, 1 );
    1851          10 :             quant_select[( n_band << 1 ) - 1] = add_sat( quant_tmp2[n_band], tmp_q );
    1852             :         }
    1853             : 
    1854           2 :         L_dist = L_deposit_l( 0 );
    1855          26 :         FOR( n_band = 0; n_band < SWB_FENV - 2; n_band++ )
    1856             :         {
    1857          24 :             tmp_q = sub( hq_generic_fenv[n_band], quant_select[n_band] ); /*Q8 */
    1858          24 :             L_tmp = L_mult( tmp_q, tmp_q );                               /*Q17 */
    1859          24 :             L_tmp = Mult_32_16( L_tmp, w_env[n_band] );                   /*Q15 */
    1860          24 :             L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) );
    1861             :         }
    1862             : 
    1863             :         /* Check optimal candidate */
    1864           2 :         IF( LT_32( L_dist, L_minDist ) )
    1865             :         {
    1866           2 :             L_minDist = L_add( L_dist, 0 );
    1867           2 :             Copy( quant_select, synth_energy, SWB_FENV - 2 );
    1868           2 :             synth_energy[SWB_FENV - 2] = 0;
    1869           2 :             move16();
    1870           2 :             synth_energy[SWB_FENV - 1] = 0;
    1871           2 :             move16();
    1872             : 
    1873           2 :             indice[0] = candInd[k];
    1874           2 :             move16();
    1875           2 :             indice[1] = ind_tmp[0];
    1876           2 :             move16();
    1877           2 :             indice[2] = ind_tmp[1];
    1878           2 :             move16();
    1879           2 :             indice[3] = ind_tmp[2];
    1880           2 :             move16();
    1881           2 :             indice[4] = ind_tmp[3];
    1882           2 :             move16();
    1883             :         }
    1884             :     }
    1885             : 
    1886           1 :     Copy( synth_energy, hq_generic_fenv, nenv );
    1887             : 
    1888           1 :     return;
    1889             : }
    1890             : 
    1891             : 
    1892             : /*-------------------------------------------------------------------*
    1893             :  * calculate_Tonality_fx()
    1894             :  *
    1895             :  * Calculate tonality
    1896             :  *-------------------------------------------------------------------*/
    1897             : 
    1898         112 : static void calculate_Tonality_fx(
    1899             :     const Word16 *org,  /* i  : MDCT coefficients of original              Q_new*/
    1900             :     const Word16 *gen,  /* i  : MDCT coefficients of generated signal    Q15*/
    1901             :     Word16 *SFM_org,    /* o  : Spectral Flatness results          Q12*/
    1902             :     Word16 *SFM_gen,    /* o  : Spectral Flatness results                  Q12*/
    1903             :     const Word16 length /* i  : length for calculating tonality         */
    1904             : )
    1905             : {
    1906             :     Word16 n_coeff;
    1907             :     Word16 inv_len, max;
    1908             :     Word16 exp, e_tmp, f_tmp;
    1909             :     Word32 L_tmp, L_tmp2, L_am_org, L_am_gen, L_tmp1;
    1910             :     Word16 org_spec[80], gen_spec[80];
    1911             :     Word32 L_log_gm_org, L_log_gm_gen;
    1912             :     Word16 l_shift;
    1913             : 
    1914             :     /* to reduce dynamic range of original spectrum */
    1915         112 :     max = 0;
    1916         112 :     move16();
    1917        2672 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    1918             :     {
    1919        2560 :         org_spec[n_coeff] = abs_s( org[n_coeff] );
    1920        2560 :         move16(); /*Q_new */
    1921             :         /*test(); */
    1922             :         /*if( sub(max, org_spec[n_coeff]) < 0) */
    1923             :         /*{ */
    1924             :         /*    max = org_spec[n_coeff];move16();//Q_new */
    1925             :         /*} */
    1926        2560 :         max = s_max( max, org_spec[n_coeff] );
    1927             :     }
    1928         112 :     l_shift = norm_s( max );
    1929        2672 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    1930             :     {
    1931        2560 :         org_spec[n_coeff] = shl( org_spec[n_coeff], l_shift );
    1932        2560 :         move16();
    1933        2560 :         IF( org_spec[n_coeff] == 0 )
    1934             :         {
    1935          13 :             org_spec[n_coeff] = shl( 1, l_shift );
    1936          13 :             move16();
    1937             :         }
    1938             :     }
    1939             : 
    1940         112 :     max = 0;
    1941         112 :     move16();
    1942        2672 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    1943             :     {
    1944        2560 :         gen_spec[n_coeff] = abs_s( gen[n_coeff] );
    1945        2560 :         move16(); /*Q15 */
    1946             :         /*test();
    1947             :         if( sub(max,gen_spec[n_coeff]) < 0)
    1948             :         {
    1949             :             max = gen_spec[n_coeff];move16();
    1950             :         }*/
    1951        2560 :         max = s_max( max, org_spec[n_coeff] );
    1952             :     }
    1953         112 :     l_shift = norm_s( max );
    1954        2672 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    1955             :     {
    1956        2560 :         gen_spec[n_coeff] = shl_sat( gen_spec[n_coeff], l_shift );
    1957        2560 :         move16();
    1958        2560 :         IF( gen_spec[n_coeff] == 0 )
    1959             :         {
    1960          13 :             gen_spec[n_coeff] = shl( 1, l_shift );
    1961          13 :             move16();
    1962             :         }
    1963             :     }
    1964             : 
    1965         112 :     exp = norm_s( length );
    1966         112 :     inv_len = div_s( shl( 1, exp ), shl( length, exp ) ); /*Q15 */
    1967             : 
    1968         112 :     L_am_org = L_deposit_l( 0 );
    1969         112 :     L_am_gen = L_deposit_l( 0 );
    1970         112 :     L_log_gm_org = 0;
    1971         112 :     move32();
    1972         112 :     L_log_gm_gen = 0;
    1973         112 :     move32();
    1974             : 
    1975        2672 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    1976             :     {
    1977        2560 :         L_am_org = L_add( L_am_org, L_deposit_l( org_spec[n_coeff] ) ); /*Q10 */
    1978        2560 :         L_am_gen = L_add( L_am_gen, L_deposit_l( gen_spec[n_coeff] ) ); /*Q10 */
    1979             : 
    1980        2560 :         IF( org_spec[n_coeff] != 0 )
    1981             :         {
    1982        2560 :             L_tmp = L_deposit_h( org_spec[n_coeff] ); /*Q26 */
    1983        2560 :             e_tmp = norm_l( L_tmp );
    1984        2560 :             f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    1985        2560 :             e_tmp = sub( sub( 30, e_tmp ), 26 );
    1986        2560 :             L_tmp = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    1987        2560 :             L_log_gm_org = L_add( L_log_gm_org, L_tmp );        /*Q14 */
    1988             :         }
    1989             : 
    1990        2560 :         IF( gen_spec[n_coeff] != 0 )
    1991             :         {
    1992        2560 :             L_tmp = L_deposit_h( gen_spec[n_coeff] ); /*Q26 */
    1993        2560 :             e_tmp = norm_l( L_tmp );
    1994        2560 :             f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    1995        2560 :             e_tmp = sub( sub( 30, e_tmp ), 26 );
    1996        2560 :             L_tmp = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    1997        2560 :             L_log_gm_gen = L_add( L_log_gm_gen, L_tmp );        /*Q14 */
    1998             :         }
    1999             :     }
    2000             : 
    2001         112 :     IF( L_am_org != 0 )
    2002             :     {
    2003         112 :         L_tmp = Mpy_32_16_1( L_am_org, inv_len ); /*Q10 */
    2004         112 :         e_tmp = norm_l( L_tmp );
    2005         112 :         f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2006         112 :         e_tmp = sub( sub( 30, e_tmp ), 10 );
    2007         112 :         L_tmp1 = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2008             :     }
    2009             :     ELSE
    2010             :     {
    2011           0 :         L_tmp1 = L_deposit_l( 0 );
    2012             :     }
    2013             : 
    2014         112 :     L_tmp2 = Mpy_32_16_1( L_log_gm_org, inv_len ); /* Q14 */
    2015             : 
    2016         112 :     L_tmp = L_sub( L_tmp1, L_tmp2 );
    2017         112 :     *SFM_org = round_fx_sat( L_shl_sat( L_tmp, 14 ) ); /*Q12 */
    2018         112 :     move16();
    2019         112 :     *SFM_org = s_max( 0, s_min( *SFM_org, 24547 ) );
    2020         112 :     move16(); /*0.0001 and 5.993 in Q12 */
    2021             : 
    2022         112 :     IF( L_am_gen != 0 )
    2023             :     {
    2024         112 :         L_tmp = Mpy_32_16_1( L_am_gen, inv_len ); /*Q10 */
    2025         112 :         e_tmp = norm_l( L_tmp );
    2026         112 :         f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2027         112 :         e_tmp = sub( sub( 30, e_tmp ), 10 );
    2028         112 :         L_tmp1 = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2029             :     }
    2030             :     ELSE
    2031             :     {
    2032           0 :         L_tmp1 = L_deposit_l( 0 );
    2033             :     }
    2034             : 
    2035         112 :     L_tmp2 = Mpy_32_16_1( L_log_gm_gen, inv_len ); /* Q14 */
    2036             : 
    2037         112 :     L_tmp = L_sub( L_tmp1, L_tmp2 );
    2038         112 :     *SFM_gen = round_fx_sat( L_shl_sat( L_tmp, 14 ) ); /*Q12 */
    2039         112 :     move16();
    2040         112 :     *SFM_gen = s_max( 0, s_min( *SFM_gen, 24547 ) );
    2041         112 :     move16(); /*0.0001 and 5.993 in Q12 */
    2042             : 
    2043         112 :     return;
    2044             : }
    2045             : 
    2046             : /*-------------------------------------------------------------------*
    2047             :  * calculate_Tonality_ivas_fx()
    2048             :  *
    2049             :  * Calculate tonality
    2050             :  *-------------------------------------------------------------------*/
    2051             : 
    2052      198098 : static void calculate_Tonality_ivas_fx(
    2053             :     const Word16 *org,  /* i  : MDCT coefficients of original              Q_new*/
    2054             :     const Word16 *gen,  /* i  : MDCT coefficients of generated signal    Q15*/
    2055             :     Word16 *SFM_org,    /* o  : Spectral Flatness results          Q12*/
    2056             :     Word16 *SFM_gen,    /* o  : Spectral Flatness results                  Q12*/
    2057             :     const Word16 length /* i  : length for calculating tonality         */
    2058             : )
    2059             : {
    2060             :     Word16 n_coeff;
    2061             :     Word16 inv_len, max;
    2062             :     Word16 exp, e_tmp, f_tmp;
    2063             :     Word32 L_tmp, L_tmp2, L_am_org, L_am_gen, L_tmp1;
    2064             :     Word16 org_spec[80], gen_spec[80];
    2065             :     Word32 L_log_gm_org, L_log_gm_gen;
    2066             :     Word16 l_shift;
    2067             : 
    2068             :     /* to reduce dynamic range of original spectrum */
    2069      198098 :     max = 0;
    2070      198098 :     move16();
    2071     4871458 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2072             :     {
    2073     4673360 :         org_spec[n_coeff] = abs_s( org[n_coeff] );
    2074     4673360 :         move16(); /*Q_new */
    2075             :         /*test(); */
    2076             :         /*if( sub(max, org_spec[n_coeff]) < 0) */
    2077             :         /*{ */
    2078             :         /*    max = org_spec[n_coeff];move16();//Q_new */
    2079             :         /*} */
    2080     4673360 :         max = s_max( max, org_spec[n_coeff] );
    2081             :     }
    2082      198098 :     l_shift = norm_s( max );
    2083     4871458 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2084             :     {
    2085     4673360 :         org_spec[n_coeff] = shl( org_spec[n_coeff], l_shift );
    2086     4673360 :         move16();
    2087     4673360 :         IF( org_spec[n_coeff] == 0 )
    2088             :         {
    2089      177782 :             org_spec[n_coeff] = shl( 1, l_shift );
    2090      177782 :             move16();
    2091             :         }
    2092             :     }
    2093             : 
    2094      198098 :     max = 0;
    2095      198098 :     move16();
    2096     4871458 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2097             :     {
    2098     4673360 :         gen_spec[n_coeff] = abs_s( gen[n_coeff] );
    2099     4673360 :         move16(); /*Q15 */
    2100             :         /*test();
    2101             :         if( sub(max,gen_spec[n_coeff]) < 0)
    2102             :         {
    2103             :             max = gen_spec[n_coeff];move16();
    2104             :         }*/
    2105     4673360 :         max = s_max( max, gen_spec[n_coeff] );
    2106             :     }
    2107      198098 :     l_shift = norm_s( max );
    2108     4871458 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2109             :     {
    2110     4673360 :         gen_spec[n_coeff] = shl_sat( gen_spec[n_coeff], l_shift );
    2111     4673360 :         move16();
    2112     4673360 :         IF( gen_spec[n_coeff] == 0 )
    2113             :         {
    2114       20659 :             gen_spec[n_coeff] = shl( 1, l_shift );
    2115       20659 :             move16();
    2116             :         }
    2117             :     }
    2118             : 
    2119      198098 :     exp = norm_s( length );
    2120      198098 :     inv_len = div_s( shl( 1, exp ), shl( length, exp ) ); /*Q15 */
    2121             : 
    2122      198098 :     L_am_org = L_deposit_l( 0 );
    2123      198098 :     L_am_gen = L_deposit_l( 0 );
    2124      198098 :     L_log_gm_org = 0;
    2125      198098 :     move32();
    2126      198098 :     L_log_gm_gen = 0;
    2127      198098 :     move32();
    2128             : 
    2129     4871458 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2130             :     {
    2131     4673360 :         L_am_org = L_add( L_am_org, L_deposit_l( org_spec[n_coeff] ) ); /*Q10 */
    2132     4673360 :         L_am_gen = L_add( L_am_gen, L_deposit_l( gen_spec[n_coeff] ) ); /*Q10 */
    2133             : 
    2134     4673360 :         IF( org_spec[n_coeff] != 0 )
    2135             :         {
    2136     4673360 :             L_tmp = L_deposit_h( org_spec[n_coeff] ); /*Q26 */
    2137     4673360 :             e_tmp = norm_l( L_tmp );
    2138     4673360 :             f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2139     4673360 :             e_tmp = sub( sub( 30, e_tmp ), 26 );
    2140     4673360 :             L_tmp = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2141     4673360 :             L_log_gm_org = L_add( L_log_gm_org, L_tmp );        /*Q14 */
    2142             :         }
    2143             : 
    2144     4673360 :         IF( gen_spec[n_coeff] != 0 )
    2145             :         {
    2146     4673360 :             L_tmp = L_deposit_h( gen_spec[n_coeff] ); /*Q26 */
    2147     4673360 :             e_tmp = norm_l( L_tmp );
    2148     4673360 :             f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2149     4673360 :             e_tmp = sub( sub( 30, e_tmp ), 26 );
    2150     4673360 :             L_tmp = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2151     4673360 :             L_log_gm_gen = L_add( L_log_gm_gen, L_tmp );        /*Q14 */
    2152             :         }
    2153             :     }
    2154             : 
    2155      198098 :     IF( L_am_org != 0 )
    2156             :     {
    2157      198098 :         L_tmp = Mpy_32_16_1( L_am_org, inv_len ); /*Q10 */
    2158      198098 :         e_tmp = norm_l( L_tmp );
    2159      198098 :         f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2160      198098 :         e_tmp = sub( sub( 30, e_tmp ), 10 );
    2161      198098 :         L_tmp1 = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2162             :     }
    2163             :     ELSE
    2164             :     {
    2165           0 :         L_tmp1 = L_deposit_l( 0 );
    2166             :     }
    2167             : 
    2168      198098 :     L_tmp2 = Mpy_32_16_1( L_log_gm_org, inv_len ); /* Q14 */
    2169             : 
    2170      198098 :     L_tmp = L_sub( L_tmp1, L_tmp2 );
    2171      198098 :     *SFM_org = round_fx_sat( L_shl_sat( L_tmp, 14 ) ); /*Q12 */
    2172      198098 :     move16();
    2173      198098 :     *SFM_org = s_max( 0, s_min( *SFM_org, 24547 ) );
    2174      198098 :     move16(); /*0.0001 and 5.993 in Q12 */
    2175             : 
    2176      198098 :     IF( L_am_gen != 0 )
    2177             :     {
    2178      198098 :         L_tmp = Mpy_32_16_1( L_am_gen, inv_len ); /*Q10 */
    2179      198098 :         e_tmp = norm_l( L_tmp );
    2180      198098 :         f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2181      198098 :         e_tmp = sub( sub( 30, e_tmp ), 10 );
    2182      198098 :         L_tmp1 = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2183             :     }
    2184             :     ELSE
    2185             :     {
    2186           0 :         L_tmp1 = L_deposit_l( 0 );
    2187             :     }
    2188             : 
    2189      198098 :     L_tmp2 = Mpy_32_16_1( L_log_gm_gen, inv_len ); /* Q14 */
    2190             : 
    2191      198098 :     L_tmp = L_sub( L_tmp1, L_tmp2 );
    2192      198098 :     *SFM_gen = round_fx_sat( L_shl_sat( L_tmp, 14 ) ); /*Q12 */
    2193      198098 :     move16();
    2194      198098 :     *SFM_gen = s_max( 0, s_min( *SFM_gen, 24547 ) );
    2195      198098 :     move16(); /*0.0001 and 5.993 in Q12 */
    2196             : 
    2197      198098 :     return;
    2198             : }
    2199             : 
    2200             : /*-------------------------------------------------------------------*
    2201             :  * energy_control_fx()
    2202             :  *
    2203             :  *-------------------------------------------------------------------*/
    2204             : 
    2205           8 : static void energy_control_fx(
    2206             :     Encoder_State *st_fx,     /* i/o: Encoder structure   */
    2207             :     const Word16 core,        /* i  : core                */
    2208             :     const Word16 mode,        /* i  : SHB BWE class       */
    2209             :     const Word16 coder_type,  /* i  : coder type          */
    2210             :     const Word16 *org_fx,     /* i  : input spectrum      */
    2211             :     const Word16 offset,      /* i  : frequency offset    */
    2212             :     Word16 *energy_factor_fx, /* o  : energy factor       */
    2213             :     Word16 Q_new_lf )
    2214             : {
    2215             :     Word16 n_band, max_band, band_step;
    2216             :     Word16 gamma_fx, core_type;
    2217             :     Word16 SWB_signal_fx[L_FRAME32k], SFM_org_fx[SWB_FENV], SFM_gen_fx[SWB_FENV];
    2218           8 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    2219             : 
    2220           8 :     IF( EQ_16( core, ACELP_CORE ) )
    2221             :     {
    2222           8 :         gamma_fx = 11469;
    2223           8 :         move16(); /*.35 in Q15 */
    2224           8 :         test();
    2225           8 :         IF( NE_16( coder_type, AUDIO ) && LT_32( st_fx->total_brate, ACELP_8k85 ) )
    2226             :         {
    2227           0 :             core_type = 0;
    2228             :         }
    2229             :         ELSE
    2230             :         {
    2231           8 :             core_type = 1;
    2232             :         }
    2233           8 :         move16();
    2234           8 :         get_normalize_spec_fx( core, st_fx->extl, mode, core_type, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset, Q_new_lf );
    2235             : 
    2236           8 :         IF( EQ_16( st_fx->extl, WB_BWE ) )
    2237             :         {
    2238           0 :             max_band = 4;
    2239           0 :             band_step = 2;
    2240             :         }
    2241             :         ELSE
    2242             :         {
    2243           8 :             max_band = SWB_FENV;
    2244           8 :             band_step = 1;
    2245             :         }
    2246           8 :         move16();
    2247           8 :         move16();
    2248             :     }
    2249             :     ELSE /* HQ core */
    2250             :     {
    2251           0 :         gamma_fx = 18022;
    2252           0 :         move16(); /*.55 in Q15 */
    2253           0 :         get_normalize_spec_fx( core, st_fx->extl, mode, -1, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset, Q_new_lf );
    2254             : 
    2255           0 :         band_step = 1;
    2256           0 :         move16();
    2257           0 :         IF( EQ_16( offset, HQ_GENERIC_FOFFSET_32K ) )
    2258             :         {
    2259           0 :             max_band = 12;
    2260             :         }
    2261             :         ELSE
    2262             :         {
    2263           0 :             max_band = SWB_FENV;
    2264             :         }
    2265           0 :         move16();
    2266             :     }
    2267             : 
    2268         120 :     FOR( n_band = 0; n_band < max_band; )
    2269             :     {
    2270         112 :         calculate_Tonality_fx( org_fx + swb_bwe_subband[n_band] + offset, SWB_signal_fx + swb_bwe_subband[n_band] + offset,
    2271         112 :                                &SFM_org_fx[n_band], &SFM_gen_fx[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] );
    2272             : 
    2273         112 :         IF( LT_16( SFM_gen_fx[n_band], mult_r( 24576, SFM_org_fx[n_band] ) ) )
    2274             :         {
    2275          56 :             energy_factor_fx[n_band] = div_s( SFM_gen_fx[n_band], SFM_org_fx[n_band] ); /*Q15 */
    2276          56 :             if ( LT_16( energy_factor_fx[n_band], gamma_fx ) )
    2277             :             {
    2278           7 :                 energy_factor_fx[n_band] = gamma_fx;
    2279           7 :                 move16();
    2280             :             }
    2281             :         }
    2282             :         ELSE
    2283             :         {
    2284          56 :             energy_factor_fx[n_band] = 32767;
    2285          56 :             move16(); /* Q15 */
    2286             :         }
    2287         112 :         n_band = add( n_band, band_step );
    2288             :     }
    2289           8 :     return;
    2290             : }
    2291             : 
    2292             : /*-------------------------------------------------------------------*
    2293             :  * energy_control_ivas_fx()
    2294             :  *
    2295             :  *-------------------------------------------------------------------*/
    2296             : 
    2297       17785 : static void energy_control_ivas_fx(
    2298             :     Encoder_State *st_fx,     /* i/o: Encoder structure   */
    2299             :     const Word16 core,        /* i  : core                */
    2300             :     const Word16 mode,        /* i  : SHB BWE class       */
    2301             :     const Word16 coder_type,  /* i  : coder type          */
    2302             :     const Word16 *org_fx,     /* i  : input spectrum      */
    2303             :     const Word16 offset,      /* i  : frequency offset    */
    2304             :     Word16 *energy_factor_fx, /* o  : energy factor       */
    2305             :     Word16 Q_new_lf )
    2306             : {
    2307             :     Word16 n_band, max_band, band_step;
    2308             :     Word16 gamma_fx, core_type;
    2309             :     Word16 SWB_signal_fx[L_FRAME32k], SFM_org_fx[SWB_FENV], SFM_gen_fx[SWB_FENV];
    2310       17785 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    2311             : 
    2312       17785 :     IF( EQ_16( core, ACELP_CORE ) )
    2313             :     {
    2314       17785 :         gamma_fx = 11469;
    2315       17785 :         move16(); /*.35 in Q15 */
    2316       17785 :         test();
    2317       17785 :         IF( NE_16( coder_type, AUDIO ) && LE_32( st_fx->total_brate, ACELP_8k00 ) )
    2318             :         {
    2319          17 :             core_type = 0;
    2320             :         }
    2321             :         ELSE
    2322             :         {
    2323       17768 :             core_type = 1;
    2324             :         }
    2325       17785 :         move16();
    2326       17785 :         get_normalize_spec_fx( core, st_fx->extl, mode, core_type, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset, Q_new_lf );
    2327             : 
    2328       17785 :         IF( EQ_16( st_fx->extl, WB_BWE ) )
    2329             :         {
    2330        4241 :             max_band = 4;
    2331        4241 :             band_step = 2;
    2332             :         }
    2333             :         ELSE
    2334             :         {
    2335       13544 :             max_band = SWB_FENV;
    2336       13544 :             band_step = 1;
    2337             :         }
    2338       17785 :         move16();
    2339       17785 :         move16();
    2340             :     }
    2341             :     ELSE /* HQ core */
    2342             :     {
    2343           0 :         gamma_fx = 18022;
    2344           0 :         move16(); /*.55 in Q15 */
    2345           0 :         get_normalize_spec_fx( core, st_fx->extl, mode, -1, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset, Q_new_lf );
    2346             : 
    2347           0 :         band_step = 1;
    2348           0 :         move16();
    2349           0 :         IF( EQ_16( offset, HQ_GENERIC_FOFFSET_32K ) )
    2350             :         {
    2351           0 :             max_band = 12;
    2352             :         }
    2353             :         ELSE
    2354             :         {
    2355           0 :             max_band = SWB_FENV;
    2356             :         }
    2357           0 :         move16();
    2358             :     }
    2359             : 
    2360      215883 :     FOR( n_band = 0; n_band < max_band; )
    2361             :     {
    2362      198098 :         calculate_Tonality_ivas_fx( org_fx + swb_bwe_subband[n_band] + offset, SWB_signal_fx + swb_bwe_subband[n_band] + offset,
    2363      198098 :                                     &SFM_org_fx[n_band], &SFM_gen_fx[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] );
    2364             : 
    2365      198098 :         IF( LT_16( SFM_gen_fx[n_band], mult_r( 24576, SFM_org_fx[n_band] ) ) )
    2366             :         {
    2367       59061 :             energy_factor_fx[n_band] = div_s( SFM_gen_fx[n_band], SFM_org_fx[n_band] ); /*Q15 */
    2368       59061 :             move16();
    2369       59061 :             if ( LT_16( energy_factor_fx[n_band], gamma_fx ) )
    2370             :             {
    2371        7888 :                 energy_factor_fx[n_band] = gamma_fx;
    2372        7888 :                 move16();
    2373             :             }
    2374             :         }
    2375             :         ELSE
    2376             :         {
    2377      139037 :             energy_factor_fx[n_band] = 32767;
    2378      139037 :             move16(); /* Q15 */
    2379             :         }
    2380      198098 :         n_band = add( n_band, band_step );
    2381             :     }
    2382       17785 :     return;
    2383             : }
    2384             : 
    2385             : /*-------------------------------------------------------------------*
    2386             :  * WB_BWE_encoding()
    2387             :  *
    2388             :  * WB BWE main encoder
    2389             :  *-------------------------------------------------------------------*/
    2390             : 
    2391           0 : Word16 WB_BWE_encoding_fx(                          /* o  : classification of wb signal            */
    2392             :                            const Word16 coder_type, /* i  : coder type                             */
    2393             :                            const Word16 *yos_fx,    /* i  : MDCT coefficients of weighted original */
    2394             :                            Word16 *WB_fenv_fx,      /* i/o: energy of WB envelope                  */
    2395             :                            Encoder_State *st_fx,    /* i/o: Encoder structure                      */
    2396             :                            Word16 Q_synth,
    2397             :                            Word16 Q_synth_lf )
    2398             : {
    2399             :     Word16 mode;
    2400             :     Word16 i, n_coeff, n_band;
    2401             :     Word16 index;
    2402             :     Word32 energy_fx;
    2403             :     Word32 L_WB_fenv_fx[2];
    2404             :     Word16 energy_factor_fx[4];
    2405             :     Word16 ener_40, exp;
    2406             :     Word32 L_tmp;
    2407             :     Word16 tmp;
    2408             : 
    2409           0 :     n_band = 0;
    2410           0 :     move16();
    2411           0 :     FOR( i = 0; i < 2; i++ )
    2412             :     {
    2413           0 :         energy_fx = L_deposit_l( 0 );
    2414           0 :         FOR( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ )
    2415             :         {
    2416           0 :             energy_fx = L_add( energy_fx, L_shr( L_mult0( yos_fx[n_coeff], yos_fx[n_coeff] ), 6 ) ); /*2*Q_synth-6 */
    2417             :         }
    2418             : 
    2419           0 :         L_WB_fenv_fx[i] = energy_fx;
    2420           0 :         move32(); /*2*Q_synth-6 */
    2421           0 :         n_band = add( n_band, 2 );
    2422             :     }
    2423           0 :     mode = FD_BWE_class_fx( yos_fx, 0, 0, Q_synth, 0, st_fx );
    2424             : 
    2425             : 
    2426           0 :     energy_control_fx( st_fx, ACELP_CORE, mode, coder_type, yos_fx, 0, energy_factor_fx, Q_synth_lf );
    2427             : 
    2428           0 :     FOR( i = 0; i < 2; i++ )
    2429             :     {
    2430           0 :         ener_40 = mult_r( shr( energy_factor_fx[shl( i, 1 )], 1 ), 26214 ); /*Q19 */
    2431           0 :         L_tmp = Mult_32_16( L_WB_fenv_fx[i], ener_40 );                     /*2*Q_synth-2 */
    2432           0 :         IF( L_tmp )
    2433             :         {
    2434           0 :             exp = norm_l( L_tmp );
    2435           0 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    2436             :             /*exp = 30-exp-(2*Q_synth-2); */
    2437           0 :             exp = sub( sub( 30, exp ), ( sub( shl( Q_synth, 1 ), 2 ) ) );
    2438           0 :             L_tmp = Mpy_32_16( exp, tmp, 32767 );           /* Q16 */
    2439           0 :             WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /*Q10 */
    2440           0 :             move16();
    2441             :         }
    2442             :         ELSE
    2443             :         {
    2444           0 :             WB_fenv_fx[i] = 0;
    2445           0 :             move16();
    2446             :         }
    2447             :     }
    2448             : 
    2449           0 :     index = WB_BWE_fenv_q_fx( WB_fenv_fx, F_2_5_fx, 32, 2 );
    2450             : 
    2451           0 :     push_indice( st_fx->hBstr, IND_WB_FENV, index, 5 );
    2452             : 
    2453           0 :     return ( mode );
    2454             : }
    2455             : 
    2456             : /*-------------------------------------------------------------------*
    2457             :  * WB_BWE_encoding_ivas()
    2458             :  *
    2459             :  * WB BWE main encoder
    2460             :  *-------------------------------------------------------------------*/
    2461             : 
    2462        4241 : Word16 WB_BWE_encoding_ivas_fx(                       /* o  : classification of wb signal            */
    2463             :                                 Encoder_State *st_fx, /* i/o: Encoder structure                      */
    2464             :                                 const Word32 *yos_fx, /* i  : MDCT coefficients of weighted original */
    2465             :                                 Word16 *WB_fenv_fx,   /* i/o: energy of WB envelope                  */
    2466             :                                 Word16 Q_synth,
    2467             :                                 Word16 Q_synth_lf )
    2468             : {
    2469             :     Word16 mode;
    2470             :     Word16 i, n_coeff, n_band;
    2471             :     Word16 index;
    2472             :     Word32 energy_fx;
    2473             :     Word32 L_WB_fenv_fx[2];
    2474             :     Word16 energy_factor_fx[4];
    2475             :     Word16 ener_40, exp;
    2476             :     Word32 L_tmp;
    2477             :     Word16 tmp;
    2478             :     Word64 energy_fx_64;
    2479             :     Word16 q_shift, scale;
    2480             :     Word16 q_WB_fenv[2];
    2481             :     Word16 yos_fx_16[L_FRAME16k];
    2482        4241 :     n_band = 0;
    2483        4241 :     move16();
    2484       12723 :     FOR( i = 0; i < 2; i++ )
    2485             :     {
    2486        8482 :         energy_fx_64 = 0;
    2487        8482 :         move64();
    2488      347762 :         FOR( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ )
    2489             :         {
    2490      339280 :             energy_fx_64 = W_add( energy_fx_64, W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ) ); /*2*Q_synth*/
    2491             :         }
    2492        8482 :         q_shift = W_norm( energy_fx_64 );
    2493        8482 :         energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/
    2494        8482 :         q_shift = sub( q_shift, 32 );
    2495             : 
    2496        8482 :         L_WB_fenv_fx[i] = energy_fx;
    2497        8482 :         move32();
    2498        8482 :         q_WB_fenv[i] = add( shl( Q_synth, 1 ), q_shift );
    2499        8482 :         move16();
    2500        8482 :         n_band = add( n_band, 2 );
    2501             :     }
    2502             : 
    2503        4241 :     scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ );
    2504        4241 :     Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale );
    2505             : 
    2506        4241 :     mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx );
    2507             : 
    2508        4241 :     energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) );
    2509             : 
    2510       12723 :     FOR( i = 0; i < 2; i++ )
    2511             :     {
    2512        8482 :         ener_40 = mult_r( shr( energy_factor_fx[shl( i, 1 )], 1 ), 26214 ); /*Q19 */
    2513        8482 :         L_tmp = Mpy_32_16_1( L_WB_fenv_fx[i], ener_40 );                    /*q_WB_fenv[i]+4 */
    2514        8482 :         IF( L_tmp )
    2515             :         {
    2516        8480 :             exp = norm_l( L_tmp );
    2517        8480 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    2518             :             /*exp = 30-exp-(q_WB_fenv[i]+4); */
    2519        8480 :             exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) );
    2520        8480 :             L_tmp = Mpy_32_16( exp, tmp, 32767 );           /* Q16 */
    2521        8480 :             WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /*Q10 */
    2522        8480 :             move16();
    2523             :         }
    2524             :         ELSE
    2525             :         {
    2526           2 :             WB_fenv_fx[i] = 0;
    2527           2 :             move16();
    2528             :         }
    2529             :     }
    2530             : 
    2531        4241 :     index = WB_BWE_fenv_q_fx( WB_fenv_fx, F_2_5_fx, 32, 2 );
    2532             : 
    2533        4241 :     push_indice( st_fx->hBstr, IND_WB_FENV, index, 5 );
    2534             : 
    2535        4241 :     return ( mode );
    2536             : }
    2537             : 
    2538             : /*-------------------------------------------------------------------*
    2539             :  * SWB_BWE_encoding()
    2540             :  *
    2541             :  * SWB BWE encoder
    2542             :  *-------------------------------------------------------------------*/
    2543           8 : static Word16 SWB_BWE_encoding_fx(
    2544             :     Encoder_State *st_fx,      /* i/o: encoder state structure   */
    2545             :     Word16 *insig_fx,          /* i  : delayed original input signal at 32kHz */
    2546             :     const Word16 *insig_lp_fx, /* i  : delayed original lowband input signal at 32kHz */
    2547             :     const Word16 *insig_hp_fx, /* i  : delayed original highband input signal at 32kHz */
    2548             :     const Word16 *synth_fx,    /* i  : delayed ACELP core synthesis at 12.8kHz */
    2549             :     const Word16 *yos_fx,      /* i  : MDCT coefficients of the windowed original input signal at 32kHz */
    2550             :     Word16 *SWB_fenv_fx,       /* o  : frequency-domain quantized BWE envelope */
    2551             :     const Word16 tilt_nb_fx,   /* i  : SWB tilt */
    2552             :     const Word16 st_offset,    /* i  : start frequency offset for BWE envelope */
    2553             :     const Word16 coder_type,   /* i  : coding type                              */
    2554             :     Word16 Q_insig_lp,
    2555             :     Word16 Q_shb,
    2556             :     Word16 Q_synth,
    2557             :     Word16 Q_synth_lf )
    2558             : {
    2559             :     Word16 IsTransient, mode;
    2560             :     Word16 index;
    2561             :     Word16 i, n_coeff, n_band, pos, indice[6];
    2562             :     Word16 L;
    2563             :     Word16 IsTransient_LF;
    2564             : 
    2565             :     Word16 tmp;
    2566             :     Word32 energy_fx;
    2567             :     Word16 tilt_fx;
    2568             :     Word32 global_gain_fx;
    2569             :     Word32 L_tmp;
    2570             :     Word32 L_SWB_fenv_fx[SWB_FENV];
    2571             :     Word16 SWB_tenv_fx[SWB_TENV];
    2572             :     Word32 L_SWB_tenv, WB_tenv_syn_fx, WB_tenv_orig_fx;
    2573             :     Word16 exp, expn, expd;
    2574             :     Word16 num, den;
    2575             :     Word16 scale;
    2576             :     Word16 Rat_tenv_fx;
    2577             :     Word16 SWB_tenv_tmp_fx[SWB_TENV];
    2578             :     Word16 max_fx;
    2579             :     Word16 energy_factor_fx[SWB_FENV], w_env_fx[SWB_FENV];
    2580           8 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    2581           8 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    2582             : 
    2583           8 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    2584             :     {
    2585           8 :         L = L_SUBFR;
    2586             :     }
    2587             :     ELSE
    2588             :     {
    2589           0 :         L = L_SUBFR16k;
    2590             :     }
    2591           8 :     move16();
    2592             : 
    2593             :     /* HF transient detect */
    2594           8 :     IsTransient = detect_transient_fx( insig_hp_fx, L_FRAME16k, Q_shb, st_fx );
    2595           8 :     st_fx->EnergyLT_fx_exp = shl( Q_shb, 1 );
    2596             : 
    2597             :     /* LF transient detect */
    2598           8 :     IsTransient_LF = 0;
    2599           8 :     move16();
    2600          40 :     FOR( n_band = 0; n_band < 4; n_band++ )
    2601             :     {
    2602          32 :         tmp = i_mult2( n_band, L );
    2603          32 :         energy_fx = L_deposit_l( 0 );
    2604        2080 :         FOR( i = 0; i < L; i++ )
    2605             :         {
    2606        2048 :             energy_fx = L_add( energy_fx, L_shr( L_mult0( insig_lp_fx[i + tmp], insig_lp_fx[i + tmp] ), 7 ) ); /*2*Q_slb_speech - 7 */
    2607             :         }
    2608             : 
    2609          32 :         if ( GT_32( Mult_32_16( energy_fx, 5958 ), hBWE_FD->EnergyLF_fx ) )
    2610             :         {
    2611           8 :             IsTransient_LF = 1;
    2612           8 :             move16();
    2613             :         }
    2614             : 
    2615          32 :         hBWE_FD->EnergyLF_fx = energy_fx;
    2616          32 :         move32();
    2617             :     }
    2618             : 
    2619             :     /* tilt returned in Q24 go to Q11 */
    2620           8 :     tilt_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( insig_fx, 0, L_FRAME32k ), 3 ) );
    2621             : 
    2622           8 :     test();
    2623           8 :     test();
    2624           8 :     IF( EQ_16( IsTransient, 1 ) && ( GT_16( tilt_fx, 16384 ) || GT_16( st_fx->clas, 1 ) ) )
    2625             :     {
    2626           0 :         IsTransient = 0;
    2627           0 :         move16();
    2628           0 :         st_fx->TransientHangOver = 0;
    2629           0 :         move16();
    2630             :     }
    2631             : 
    2632           8 :     IF( EQ_16( IsTransient, 1 ) )
    2633             :     {
    2634           0 :         mode = IsTransient;
    2635           0 :         move16();
    2636           0 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    2637             : 
    2638             :         /* Energy for the different bands and global energies */
    2639           0 :         global_gain_fx = L_deposit_l( 0 );
    2640           0 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    2641             :         {
    2642           0 :             energy_fx = L_deposit_l( 0 );
    2643           0 :             FOR( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ )
    2644             :             {
    2645           0 :                 L_tmp = L_shr( L_mult0( yos_fx[n_coeff], yos_fx[n_coeff] ), 7 ); /*2*Q_synth-7 */
    2646           0 :                 energy_fx = L_add( L_tmp, energy_fx );                           /*2*Q_synth-7 */
    2647             :             }
    2648           0 :             global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( sub( shl( Q_synth, 1 ), 7 ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */
    2649           0 :             L_SWB_fenv_fx[n_band] = energy_fx;
    2650           0 :             move32();
    2651             :         }
    2652           0 :         global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb  */
    2653             : 
    2654           0 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    2655             :         {
    2656           0 :             expd = norm_s( swb_bwe_trans_subband_width[n_band] );
    2657           0 :             tmp = div_s( shl( 1, sub( 14, expd ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-expd) */
    2658           0 :             L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], tmp );                              /*2*Q_synth-7+29-expd - 15                */
    2659           0 :             exp = norm_l( L_tmp );
    2660           0 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    2661           0 :             exp = sub( sub( 30, exp ), sub( add( shl( Q_synth, 1 ), 7 ), expd ) );
    2662           0 :             L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2663           0 :             tmp = round_fx( L_shl( L_tmp, 10 ) );           /* Q8 */
    2664             : 
    2665           0 :             SWB_fenv_fx[n_band] = sub( tmp, Mean_env_tr_fx[n_band] );
    2666           0 :             move16(); /*Q8 */
    2667             :         }
    2668             : 
    2669           0 :         WB_tenv_orig_fx = L_deposit_l( 0 );
    2670           0 :         WB_tenv_syn_fx = L_deposit_l( 1 );
    2671           0 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    2672             :         {
    2673           0 :             tmp = i_mult2( n_band, L_SUBFR16k );
    2674           0 :             L_SWB_tenv = L_deposit_l( 0 );
    2675           0 :             FOR( i = 0; i < L_SUBFR16k; i++ )
    2676             :             {
    2677           0 :                 L_SWB_tenv = L_add_sat( L_SWB_tenv, L_mult0( insig_hp_fx[i + tmp], insig_hp_fx[i + tmp] ) ); /*2*Q_shb */
    2678             :             }
    2679             : 
    2680           0 :             tmp = i_mult2( n_band, L );
    2681           0 :             FOR( i = 0; i < L; i++ )
    2682             :             {
    2683           0 :                 WB_tenv_syn_fx = L_add( WB_tenv_syn_fx, L_shr( L_mult0( synth_fx[i + tmp], synth_fx[i + tmp] ), 7 ) );         /*2*st_fx->Q_syn2 - 7 */
    2684           0 :                 WB_tenv_orig_fx = L_add( WB_tenv_orig_fx, L_shr( L_mult0( insig_lp_fx[i + tmp], insig_lp_fx[i + tmp] ), 7 ) ); /*2*Q_insig_lp - 7 */
    2685             :             }
    2686             : 
    2687           0 :             L_tmp = Mult_32_16( L_SWB_tenv, INV_L_SUBFR16k_FX ); /*2*Q_shb */
    2688           0 :             SWB_tenv_fx[n_band] = 0;
    2689           0 :             move16();
    2690           0 :             IF( L_tmp != 0 )
    2691             :             {
    2692           0 :                 exp = norm_l( L_tmp );
    2693           0 :                 tmp = extract_h( L_shl( L_tmp, exp ) );
    2694           0 :                 exp = sub( exp, sub( 30, 2 * Q_shb ) );
    2695             : 
    2696           0 :                 tmp = div_s( 16384, tmp );
    2697           0 :                 L_tmp = L_deposit_h( tmp );
    2698           0 :                 L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    2699             : 
    2700           0 :                 SWB_tenv_fx[n_band] = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /*Q3*/
    2701           0 :                 move16();
    2702             :             }
    2703             :         }
    2704             : 
    2705           0 :         IF( WB_tenv_orig_fx != 0 )
    2706             :         {
    2707           0 :             expn = norm_l( WB_tenv_orig_fx );
    2708           0 :             num = extract_h( L_shl( WB_tenv_orig_fx, expn ) );
    2709           0 :             expn = sub( sub( 30, expn ), sub( shl( Q_insig_lp, 1 ), 7 ) );
    2710             : 
    2711           0 :             expd = norm_l( WB_tenv_syn_fx );
    2712           0 :             den = round_fx( L_shl( WB_tenv_syn_fx, expd ) );
    2713           0 :             expd = sub( sub( 30, expd ), sub( shl( st_fx->Q_syn2, 1 ), 7 ) );
    2714             : 
    2715           0 :             scale = shr( sub( den, num ), 15 );
    2716           0 :             num = shl( num, scale );
    2717           0 :             expn = sub( expn, scale );
    2718             : 
    2719           0 :             tmp = div_s( num, den );
    2720           0 :             expn = sub( expn, expd );
    2721             : 
    2722           0 :             L_tmp = L_deposit_h( tmp );
    2723           0 :             L_tmp = Isqrt_lc( L_tmp, &expn ); /*31-expn */
    2724             : 
    2725           0 :             Rat_tenv_fx = round_fx_sat( L_shl_sat( L_tmp, sub( expn, 1 ) ) ); /*Q14 */
    2726             :         }
    2727             :         ELSE
    2728             :         {
    2729           0 :             Rat_tenv_fx = 16384;
    2730           0 :             move16();
    2731             :         }
    2732             : 
    2733           0 :         IF( LT_16( Rat_tenv_fx, 8192 ) )
    2734             :         {
    2735           0 :             L_tmp = L_mult( Rat_tenv_fx, 19661 ); /*Q29 */
    2736             : 
    2737           0 :             Rat_tenv_fx = round_fx_sat( L_shl_sat( L_tmp, 2 ) ); /*Q15 */
    2738             :         }
    2739           0 :         ELSE IF( GT_16( Rat_tenv_fx, 16384 ) )
    2740             :         {
    2741           0 :             Rat_tenv_fx = 32767;
    2742           0 :             move16();
    2743             :         }
    2744             : 
    2745           0 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    2746             :         {
    2747           0 :             SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], Rat_tenv_fx );
    2748           0 :             move16(); /*Q3 */
    2749             :         }
    2750             : 
    2751           0 :         max_fx = SWB_tenv_fx[0];
    2752           0 :         move16();
    2753           0 :         pos = 0;
    2754           0 :         move16();
    2755           0 :         FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    2756             :         {
    2757           0 :             IF( GT_16( SWB_tenv_fx[n_band], max_fx ) )
    2758             :             {
    2759           0 :                 max_fx = SWB_tenv_fx[n_band];
    2760           0 :                 move16();
    2761           0 :                 pos = n_band;
    2762           0 :                 move16();
    2763             :             }
    2764             :         }
    2765             : 
    2766           0 :         max_fx = SWB_tenv_fx[0];
    2767           0 :         move16();
    2768           0 :         FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    2769             :         {
    2770           0 :             tmp = sub( mult_r( SWB_tenv_fx[n_band], 6554 ), SWB_tenv_fx[n_band - 1] );
    2771           0 :             IF( tmp > 0 )
    2772             :             {
    2773           0 :                 BREAK;
    2774             :             }
    2775             :         }
    2776             : 
    2777           0 :         IF( n_band < SWB_TENV )
    2778             :         {
    2779           0 :             energy_fx = L_deposit_l( 0 );
    2780           0 :             FOR( n_band = ( pos + 1 ); n_band < SWB_TENV; n_band++ )
    2781             :             {
    2782           0 :                 energy_fx = L_add( energy_fx, SWB_tenv_fx[n_band] ); /*Q3 */
    2783             :             }
    2784             : 
    2785           0 :             IF( pos == sub( SWB_TENV, 1 ) )
    2786             :             {
    2787           0 :                 energy_fx = L_deposit_l( 0 );
    2788             :             }
    2789             :             ELSE
    2790             :             {
    2791           0 :                 tmp = sub( SWB_TENV, pos + 1 );
    2792           0 :                 tmp = div_s( 1, tmp );                    /*Q15 */
    2793           0 :                 energy_fx = Mult_32_16( energy_fx, tmp ); /*Q3 */
    2794             :             }
    2795             : 
    2796           0 :             FOR( n_band = 0; n_band < pos; n_band++ )
    2797             :             {
    2798           0 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 16384 );
    2799           0 :                 move16();
    2800             :             }
    2801             : 
    2802             :             /*SWB_tenv_fx[pos] = add(SWB_tenv_fx[pos], mult_r(SWB_tenv_fx[pos], 164));    move16();//Q3 */
    2803           0 :             SWB_tenv_fx[pos] = round_fx_sat( L_mac_sat( L_mult_sat( SWB_tenv_fx[pos], 32767 ), SWB_tenv_fx[pos], 164 ) ); /*Q3 */
    2804           0 :             move16();
    2805             : 
    2806           0 :             IF( LT_32( energy_fx, SWB_tenv_fx[pos] ) )
    2807             :             {
    2808           0 :                 FOR( n_band = pos + 1; n_band < SWB_TENV; n_band++ )
    2809             :                 {
    2810           0 :                     SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 29491 );
    2811           0 :                     move16(); /*Q3 */
    2812             :                 }
    2813             :             }
    2814             :         }
    2815             :         ELSE
    2816             :         {
    2817           0 :             FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    2818             :             {
    2819           0 :                 IF( GT_16( SWB_tenv_fx[n_band - 1], SWB_tenv_fx[n_band] ) )
    2820             :                 {
    2821             :                     /*SWB_tenv_fx[n_band-1] = add(mult_r(SWB_tenv_fx[n_band-1], 16384), mult_r(SWB_tenv_fx[n_band], 16384)); move16();//Q3 */
    2822           0 :                     SWB_tenv_fx[n_band - 1] = round_fx_sat( L_mac_sat( L_mult( SWB_tenv_fx[n_band - 1], 16384 ), SWB_tenv_fx[n_band], 16384 ) ); /*Q3 */
    2823             :                 }
    2824             :                 ELSE
    2825             :                 {
    2826             :                     /*SWB_tenv_fx[n_band] = add(mult_r(SWB_tenv_fx[n_band-1], 16384), mult_r(SWB_tenv_fx[n_band], 16384)); move16();//Q3 */
    2827           0 :                     SWB_tenv_fx[n_band] = round_fx_sat( L_mac_sat( L_mult( SWB_tenv_fx[n_band - 1], 16384 ), SWB_tenv_fx[n_band], 16384 ) ); /*Q3 */
    2828             :                 }
    2829           0 :                 move16();
    2830             :             }
    2831             : 
    2832           0 :             FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    2833             :             {
    2834           0 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 29491 );
    2835           0 :                 move16(); /*Q3 */
    2836             :             }
    2837             :         }
    2838             : 
    2839           0 :         test();
    2840           0 :         test();
    2841           0 :         IF( IsTransient_LF == 0 && EQ_16( coder_type, INACTIVE ) && EQ_16( st_fx->TransientHangOver, 1 ) )
    2842             :         {
    2843           0 :             FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    2844             :             {
    2845           0 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 16384 );
    2846           0 :                 move16();
    2847             :             }
    2848           0 :             FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    2849             :             {
    2850           0 :                 SWB_fenv_fx[n_band] = mult_r( SWB_fenv_fx[n_band], 1638 );
    2851           0 :                 move16();
    2852             :             }
    2853             :         }
    2854             :         ELSE
    2855             :         {
    2856           0 :             SWB_fenv_fx[2] = mult_r( SWB_fenv_fx[2], 3277 );
    2857           0 :             move16();
    2858           0 :             SWB_fenv_fx[3] = mult_r( SWB_fenv_fx[3], 1638 );
    2859           0 :             move16();
    2860             :         }
    2861             : 
    2862           0 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    2863             :         {
    2864           0 :             IF( SWB_tenv_fx[n_band] == 0 )
    2865             :             {
    2866           0 :                 SWB_tenv_tmp_fx[n_band] = -32768;
    2867           0 :                 move16(); /*-16 in Q11 */
    2868             :             }
    2869             :             ELSE
    2870             :             {
    2871           0 :                 L_tmp = L_deposit_h( SWB_tenv_fx[n_band] ); /*Q19 */
    2872           0 :                 expn = norm_l( L_tmp );
    2873           0 :                 tmp = Log2_norm_lc( L_shl( L_tmp, expn ) );
    2874           0 :                 expn = sub( sub( 30, expn ), 19 );
    2875           0 :                 L_tmp = Mpy_32_16( expn, tmp, 32767 ); /* Q16 */          /*1 in Q15 */
    2876           0 :                 SWB_tenv_tmp_fx[n_band] = round_fx( L_shl( L_tmp, 11 ) ); /* Q11 */
    2877           0 :                 move16();
    2878             :             }
    2879             : 
    2880           0 :             IF( GT_16( SWB_tenv_tmp_fx[n_band], 30720 ) )
    2881             :             {
    2882           0 :                 index = 15;
    2883           0 :                 move16();
    2884             :             }
    2885           0 :             ELSE IF( SWB_tenv_tmp_fx[n_band] < 0 )
    2886             :             {
    2887           0 :                 index = 0;
    2888           0 :                 move16();
    2889             :             }
    2890             :             ELSE
    2891             :             {
    2892           0 :                 index = shr( add( SWB_tenv_tmp_fx[n_band], 1024 ), 11 );
    2893             :             }
    2894             : 
    2895           0 :             push_indice( hBstr, IND_SWB_TENV, index, 4 );
    2896             :         }
    2897             : 
    2898           0 :         MSVQ_Interpol_Tran_fx( SWB_fenv_fx, indice );
    2899             : 
    2900           0 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 7 );
    2901           0 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 6 );
    2902             :     }
    2903             :     ELSE
    2904             :     {
    2905             :         /* Energy for the different bands and global energies */
    2906           8 :         global_gain_fx = L_deposit_l( 0 );
    2907         120 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    2908             :         {
    2909         112 :             energy_fx = L_deposit_l( 0 );
    2910        2672 :             FOR( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ )
    2911             :             {
    2912        2560 :                 L_tmp = L_shr( L_mult0( yos_fx[n_coeff], yos_fx[n_coeff] ), 5 ); /*2*Q_synth-5 */
    2913        2560 :                 energy_fx = L_add( L_tmp, energy_fx );                           /*2*Q_synth-5 */
    2914             :             }
    2915             : 
    2916         112 :             IF( LT_16( n_band, sub( SWB_FENV, 2 ) ) )
    2917             :             {
    2918          96 :                 global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( 2 * Q_synth - 5, 2 * Q_shb ) ) ); /*2*Q_shb */
    2919             :             }
    2920         112 :             L_SWB_fenv_fx[n_band] = energy_fx;
    2921         112 :             move32();
    2922             :         }
    2923             : 
    2924           8 :         global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */
    2925           8 :         mode = FD_BWE_class_fx( yos_fx, global_gain_fx, tilt_nb_fx, Q_synth, Q_shb, st_fx );
    2926           8 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    2927             : 
    2928           8 :         energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx, st_offset, energy_factor_fx, Q_synth_lf );
    2929             : 
    2930         120 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    2931             :         {
    2932         112 :             L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], energy_factor_fx[n_band] ); /*2*Q_synth-5 */
    2933         112 :             L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] );     /*2*Q_synth-5 */
    2934             : 
    2935         112 :             IF( L_tmp != 0 )
    2936             :             {
    2937         112 :                 expn = norm_l( L_tmp );
    2938         112 :                 tmp = Log2_norm_lc( L_shl( L_tmp, expn ) );
    2939         112 :                 expn = sub( 30, add( expn, sub( shl( Q_synth, 1 ), 5 ) ) );
    2940         112 :                 L_tmp = Mpy_32_16( expn, tmp, 24660 ); /* Q14 */      /*10log10(2) in Q13 */
    2941         112 :                 SWB_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */
    2942         112 :                 move16();
    2943             :             }
    2944             :             ELSE
    2945             :             {
    2946           0 :                 SWB_fenv_fx[n_band] = -24576;
    2947           0 :                 move16();
    2948             :             }
    2949             :         }
    2950           8 :         freq_weights_fx( SWB_fenv_fx, w_NOR_fx, w_env_fx, SWB_FENV );
    2951             : 
    2952         120 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    2953             :         {
    2954         112 :             SWB_fenv_fx[n_band] = sub( SWB_fenv_fx[n_band], Mean_env_fx[n_band] );
    2955         112 :             move16();
    2956             :         }
    2957             : 
    2958             :         /* Energy VQ */
    2959           8 :         msvq_interpol_fx( SWB_fenv_fx, w_env_fx, indice );
    2960             : 
    2961           8 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 5 );
    2962           8 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 7 );
    2963           8 :         push_indice( hBstr, IND_SWB_FENV, indice[2], 6 );
    2964           8 :         push_indice( hBstr, IND_SWB_FENV, indice[3], 5 );
    2965           8 :         push_indice( hBstr, IND_SWB_FENV, indice[4], 6 );
    2966             :     }
    2967           8 :     hBWE_FD->prev_mode = mode;
    2968           8 :     move16();
    2969           8 :     hBWE_FD->prev_global_gain_fx = global_gain_fx;
    2970           8 :     move32();
    2971           8 :     st_fx->prev_Q_shb = Q_shb;
    2972           8 :     move16();
    2973             : 
    2974           8 :     return mode;
    2975             : }
    2976             : 
    2977             : /*-------------------------------------------------------------------*
    2978             :  * SWB_BWE_encoding()
    2979             :  *
    2980             :  * SWB BWE encoder
    2981             :  *-------------------------------------------------------------------*/
    2982       13689 : static Word16 SWB_BWE_encoding_ivas_fx(
    2983             :     Encoder_State *st_fx,      /* i/o: encoder state structure   */
    2984             :     Word16 *insig_fx,          /* i  : delayed original input signal at 32kHz */
    2985             :     const Word16 *insig_lp_fx, /* i  : delayed original lowband input signal at 32kHz */
    2986             :     const Word16 *insig_hp_fx, /* i  : delayed original highband input signal at 32kHz */
    2987             :     const Word16 *synth_fx,    /* i  : delayed ACELP core synthesis at 12.8kHz */
    2988             :     const Word32 *yos_fx,      /* i  : MDCT coefficients of the windowed original input signal at 32kHz */
    2989             :     Word16 *SWB_fenv_fx,       /* o  : frequency-domain quantized BWE envelope */
    2990             :     const Word16 tilt_nb_fx,   /* i  : SWB tilt */
    2991             :     const Word16 st_offset,    /* i  : start frequency offset for BWE envelope */
    2992             :     Word16 Q_insig_lp,
    2993             :     Word16 Q_shb,
    2994             :     Word16 Q_synth,
    2995             :     Word16 Q_synth_lf )
    2996             : {
    2997             :     Word16 IsTransient, mode;
    2998             :     Word16 index;
    2999             :     Word16 i, n_coeff, n_band, pos, indice[6];
    3000             :     Word16 L;
    3001             :     Word16 IsTransient_LF;
    3002             : 
    3003             :     Word16 tmp;
    3004             :     Word32 energy_fx;
    3005             :     Word64 energy_fx_64;
    3006             :     Word16 tilt_fx;
    3007             :     Word32 global_gain_fx;
    3008             :     Word32 L_tmp;
    3009             :     Word64 W_tmp;
    3010             :     Word32 L_SWB_fenv_fx[SWB_FENV];
    3011             :     Word16 q_SWB_fenv[SWB_FENV];
    3012             :     Word16 SWB_tenv_fx[SWB_TENV];
    3013             :     Word32 L_SWB_tenv, WB_tenv_syn_fx, WB_tenv_orig_fx;
    3014             :     Word16 exp, expn, expd;
    3015             :     Word16 num, den;
    3016             :     Word16 scale;
    3017             :     Word16 Rat_tenv_fx;
    3018             :     Word16 SWB_tenv_tmp_fx[SWB_TENV];
    3019             :     Word16 max_fx;
    3020             :     Word16 energy_factor_fx[SWB_FENV], w_env_fx[SWB_FENV];
    3021             :     Word16 inner_frame;
    3022             :     Word16 q_shift;
    3023             :     Word16 yos_fx_16[L_FRAME_MAX];
    3024             : 
    3025       13689 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    3026       13689 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    3027             : 
    3028       13689 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    3029             :     {
    3030        9155 :         L = L_SUBFR;
    3031        9155 :         move16();
    3032             :     }
    3033             :     ELSE
    3034             :     {
    3035        4534 :         L = L_SUBFR16k;
    3036        4534 :         move16();
    3037             :     }
    3038             : 
    3039       13689 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
    3040             :     {
    3041        4792 :         inner_frame = L_FRAME48k;
    3042        4792 :         move16();
    3043             :     }
    3044             :     ELSE
    3045             :     {
    3046        8897 :         inner_frame = L_FRAME32k;
    3047        8897 :         move16();
    3048             :     }
    3049             : 
    3050             :     /* HF transient detect */
    3051       13689 :     IsTransient = detect_transient_fx( insig_hp_fx, L_FRAME16k, Q_shb, st_fx );
    3052       13689 :     st_fx->EnergyLT_fx_exp = shl( Q_shb, 1 );
    3053       13689 :     move16();
    3054             : 
    3055             :     /* LF transient detect */
    3056       13689 :     IsTransient_LF = 0;
    3057       13689 :     move16();
    3058       68445 :     FOR( n_band = 0; n_band < 4; n_band++ )
    3059             :     {
    3060       54756 :         tmp = i_mult2( n_band, L );
    3061       54756 :         energy_fx = L_deposit_l( 0 );
    3062     3849316 :         FOR( i = 0; i < L; i++ )
    3063             :         {
    3064     3794560 :             energy_fx = L_add( energy_fx, L_shr( L_mult0( insig_lp_fx[i + tmp], insig_lp_fx[i + tmp] ), 7 ) ); /*Q = 2 * Q_insig_lp - 7 */
    3065             :         }
    3066             : 
    3067       54756 :         if ( BASOP_Util_Cmp_Mant32Exp( Mpy_32_16_1( energy_fx, 5958 /* 1/5.5f in Q15 */ ), sub( 31 + 7, shl( Q_insig_lp, 1 ) ), hBWE_FD->EnergyLF_fx, hBWE_FD->EnergyLF_exp ) > 0 )
    3068             :         {
    3069        2943 :             IsTransient_LF = 1;
    3070        2943 :             move16();
    3071             :         }
    3072             : 
    3073       54756 :         hBWE_FD->EnergyLF_fx = energy_fx;
    3074       54756 :         hBWE_FD->EnergyLF_exp = sub( 31 + 7, shl( Q_insig_lp, 1 ) );
    3075       54756 :         move32();
    3076       54756 :         move16();
    3077             :     }
    3078             : 
    3079             :     /* tilt returned in Q24 go to Q11 */
    3080       13689 :     tilt_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( insig_fx, 0, L_FRAME32k ), 3 ) );
    3081       13689 :     test();
    3082       13689 :     test();
    3083       13689 :     IF( EQ_16( IsTransient, 1 ) && ( GT_16( tilt_fx, 16384 ) || GT_16( st_fx->clas, 1 ) ) )
    3084             :     {
    3085         100 :         IsTransient = 0;
    3086         100 :         move16();
    3087         100 :         st_fx->TransientHangOver = 0;
    3088         100 :         move16();
    3089             :     }
    3090             : 
    3091       13689 :     IF( EQ_16( IsTransient, 1 ) )
    3092             :     {
    3093         145 :         mode = IsTransient;
    3094         145 :         move16();
    3095         145 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    3096             : 
    3097             :         /* Energy for the different bands and global energies */
    3098         145 :         global_gain_fx = L_deposit_l( 0 );
    3099         725 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    3100             :         {
    3101         580 :             energy_fx_64 = W_deposit32_l( 0 );
    3102       46980 :             FOR( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ )
    3103             :             {
    3104       46400 :                 W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */
    3105       46400 :                 energy_fx_64 = W_add( W_tmp, energy_fx_64 );               /*2*Q_synth */
    3106             :             }
    3107         580 :             q_shift = W_norm( energy_fx_64 );
    3108         580 :             energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/
    3109         580 :             q_shift = sub( q_shift, 32 );
    3110             : 
    3111         580 :             global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( add( shl( Q_synth, 1 ), q_shift ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */
    3112         580 :             L_SWB_fenv_fx[n_band] = energy_fx;
    3113         580 :             move32();
    3114         580 :             IF( L_SWB_fenv_fx[n_band] == 0 )
    3115             :             {
    3116          32 :                 q_SWB_fenv[n_band] = Q31;
    3117          32 :                 move16();
    3118             :             }
    3119             :             ELSE
    3120             :             {
    3121         548 :                 q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift );
    3122         548 :                 move16();
    3123             :             }
    3124             :         }
    3125         145 :         global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb  */
    3126             : 
    3127         725 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    3128             :         {
    3129         580 :             expd = norm_s( swb_bwe_trans_subband_width[n_band] );
    3130         580 :             tmp = div_s( shl( 1, sub( 14, expd ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-expd) */
    3131         580 :             L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], tmp );                              /*q_SWB_fenv[n_band]+29-expd - 15                */
    3132         580 :             exp = norm_l( L_tmp );
    3133         580 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    3134         580 :             exp = sub( sub( 30, exp ), sub( add( q_SWB_fenv[n_band], Q14 ), expd ) );
    3135         580 :             L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    3136         580 :             tmp = round_fx( L_shl( L_tmp, 10 ) );           /* Q8 */
    3137             : 
    3138         580 :             SWB_fenv_fx[n_band] = sub( tmp, Mean_env_tr_fx[n_band] );
    3139         580 :             move16(); /*Q8 */
    3140             :         }
    3141             : 
    3142         145 :         WB_tenv_orig_fx = L_deposit_l( 0 );
    3143         145 :         WB_tenv_syn_fx = L_deposit_l( 1 );
    3144         725 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3145             :         {
    3146         580 :             tmp = i_mult2( n_band, L_SUBFR16k );
    3147         580 :             L_SWB_tenv = L_deposit_l( 0 );
    3148       46980 :             FOR( i = 0; i < L_SUBFR16k; i++ )
    3149             :             {
    3150       46400 :                 L_SWB_tenv = L_add_sat( L_SWB_tenv, L_mult0( insig_hp_fx[i + tmp], insig_hp_fx[i + tmp] ) ); /*2*Q_shb */
    3151             :             }
    3152             : 
    3153         580 :             tmp = i_mult2( n_band, L );
    3154       39044 :             FOR( i = 0; i < L; i++ )
    3155             :             {
    3156       38464 :                 WB_tenv_syn_fx = L_add( WB_tenv_syn_fx, L_shr( L_mult0( synth_fx[i + tmp], synth_fx[i + tmp] ), 7 ) );         /*2*Q_insig_lp-7 */
    3157       38464 :                 WB_tenv_orig_fx = L_add( WB_tenv_orig_fx, L_shr( L_mult0( insig_lp_fx[i + tmp], insig_lp_fx[i + tmp] ), 7 ) ); /*2*Q_insig_lp - 7 */
    3158             :             }
    3159             : 
    3160         580 :             L_tmp = Mult_32_16( L_SWB_tenv, INV_L_SUBFR16k_FX ); /*2*Q_shb */
    3161         580 :             SWB_tenv_fx[n_band] = 0;
    3162         580 :             move16();
    3163         580 :             IF( L_tmp != 0 )
    3164             :             {
    3165         565 :                 exp = norm_l( L_tmp );
    3166         565 :                 tmp = extract_h( L_shl( L_tmp, exp ) );
    3167         565 :                 exp = sub( exp, sub( 30, shl( Q_shb, 1 ) ) );
    3168             : 
    3169         565 :                 tmp = div_s( 16384, tmp );
    3170         565 :                 L_tmp = L_deposit_h( tmp );
    3171         565 :                 L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    3172             : 
    3173         565 :                 SWB_tenv_fx[n_band] = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /*Q3           */
    3174         565 :                 move16();
    3175             :             }
    3176             :         }
    3177             : 
    3178         145 :         IF( WB_tenv_orig_fx != 0 )
    3179             :         {
    3180         137 :             expn = norm_l( WB_tenv_orig_fx );
    3181         137 :             num = extract_h( L_shl( WB_tenv_orig_fx, expn ) );
    3182         137 :             expn = sub( sub( 30, expn ), sub( shl( Q_insig_lp, 1 ), 7 ) );
    3183             : 
    3184         137 :             expd = norm_l( WB_tenv_syn_fx );
    3185         137 :             den = round_fx_sat( L_shl( WB_tenv_syn_fx, expd ) );
    3186         137 :             expd = sub( sub( 30, expd ), sub( shl( Q_insig_lp, 1 ), 7 ) );
    3187             : 
    3188         137 :             scale = shr( sub( den, num ), 15 );
    3189         137 :             num = shl( num, scale );
    3190         137 :             expn = sub( expn, scale );
    3191             : 
    3192         137 :             tmp = div_s( num, den );
    3193         137 :             expn = sub( expn, expd );
    3194             : 
    3195         137 :             L_tmp = L_deposit_h( tmp );
    3196         137 :             L_tmp = Isqrt_lc( L_tmp, &expn ); /*31-expn */
    3197             : 
    3198         137 :             Rat_tenv_fx = round_fx_sat( L_shl_sat( L_tmp, sub( expn, 1 ) ) ); /*Q14 */
    3199             :         }
    3200             :         ELSE
    3201             :         {
    3202           8 :             Rat_tenv_fx = 16384;
    3203           8 :             move16();
    3204             :         }
    3205             : 
    3206         145 :         IF( LT_16( Rat_tenv_fx, 8192 ) )
    3207             :         {
    3208           9 :             L_tmp = L_mult( Rat_tenv_fx, 19661 );                /*Q29 */
    3209           9 :             Rat_tenv_fx = round_fx_sat( L_shl_sat( L_tmp, 2 ) ); /*Q15 */
    3210             :         }
    3211         136 :         ELSE IF( GT_16( Rat_tenv_fx, 16384 ) )
    3212             :         {
    3213         103 :             Rat_tenv_fx = 32767;
    3214         103 :             move16();
    3215             :         }
    3216             : 
    3217         725 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3218             :         {
    3219         580 :             SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], Rat_tenv_fx );
    3220         580 :             move16(); /*Q3 */
    3221             :         }
    3222             : 
    3223         145 :         max_fx = SWB_tenv_fx[0];
    3224         145 :         move16();
    3225         145 :         pos = 0;
    3226         145 :         move16();
    3227         580 :         FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    3228             :         {
    3229         435 :             IF( GT_16( SWB_tenv_fx[n_band], max_fx ) )
    3230             :             {
    3231         178 :                 max_fx = SWB_tenv_fx[n_band];
    3232         178 :                 move16();
    3233         178 :                 pos = n_band;
    3234         178 :                 move16();
    3235             :             }
    3236             :         }
    3237             : 
    3238         145 :         max_fx = SWB_tenv_fx[0];
    3239         145 :         move16();
    3240         509 :         FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    3241             :         {
    3242         400 :             tmp = sub( mult_r( SWB_tenv_fx[n_band], 6554 ), SWB_tenv_fx[n_band - 1] );
    3243         400 :             IF( tmp > 0 )
    3244             :             {
    3245          36 :                 BREAK;
    3246             :             }
    3247             :         }
    3248             : 
    3249         145 :         IF( LT_16( n_band, SWB_TENV ) )
    3250             :         {
    3251          36 :             energy_fx = L_deposit_l( 0 );
    3252          56 :             FOR( n_band = ( pos + 1 ); n_band < SWB_TENV; n_band++ )
    3253             :             {
    3254          20 :                 energy_fx = L_add( energy_fx, SWB_tenv_fx[n_band] ); /*Q3 */
    3255             :             }
    3256             : 
    3257          36 :             IF( EQ_16( pos, sub( SWB_TENV, 1 ) ) )
    3258             :             {
    3259          18 :                 energy_fx = L_deposit_l( 0 );
    3260             :             }
    3261             :             ELSE
    3262             :             {
    3263          18 :                 tmp = sub( SWB_TENV, add( pos, 1 ) );
    3264          18 :                 tmp = div_s( 1, tmp );                    /*Q15 */
    3265          18 :                 energy_fx = Mult_32_16( energy_fx, tmp ); /*Q3 */
    3266             :             }
    3267             : 
    3268         124 :             FOR( n_band = 0; n_band < pos; n_band++ )
    3269             :             {
    3270          88 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 16384 );
    3271          88 :                 move16();
    3272             :             }
    3273             : 
    3274             :             /*SWB_tenv_fx[pos] = add(SWB_tenv_fx[pos], mult_r(SWB_tenv_fx[pos], 164));    move16();//Q3 */
    3275          36 :             SWB_tenv_fx[pos] = round_fx_sat( L_mac_sat( L_mult_sat( SWB_tenv_fx[pos], 32767 ), SWB_tenv_fx[pos], 164 ) ); /*Q3 */
    3276          36 :             move16();
    3277             : 
    3278          36 :             IF( LT_32( energy_fx, SWB_tenv_fx[pos] ) )
    3279             :             {
    3280          56 :                 FOR( n_band = pos + 1; n_band < SWB_TENV; n_band++ )
    3281             :                 {
    3282          20 :                     SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 29491 );
    3283          20 :                     move16(); /*Q3 */
    3284             :                 }
    3285             :             }
    3286             :         }
    3287             :         ELSE
    3288             :         {
    3289         436 :             FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    3290             :             {
    3291         327 :                 IF( GT_16( SWB_tenv_fx[n_band - 1], SWB_tenv_fx[n_band] ) )
    3292             :                 {
    3293             :                     /*SWB_tenv_fx[n_band-1] = add(mult_r(SWB_tenv_fx[n_band-1], 16384), mult_r(SWB_tenv_fx[n_band], 16384)); move16();//Q3 */
    3294         130 :                     SWB_tenv_fx[n_band - 1] = round_fx_sat( L_mac_sat( L_mult( SWB_tenv_fx[n_band - 1], 16384 ), SWB_tenv_fx[n_band], 16384 ) ); /*Q3 */
    3295         130 :                     move16();
    3296             :                 }
    3297             :                 ELSE
    3298             :                 {
    3299             :                     /*SWB_tenv_fx[n_band] = add(mult_r(SWB_tenv_fx[n_band-1], 16384), mult_r(SWB_tenv_fx[n_band], 16384)); move16();//Q3 */
    3300         197 :                     SWB_tenv_fx[n_band] = round_fx_sat( L_mac_sat( L_mult( SWB_tenv_fx[n_band - 1], 16384 ), SWB_tenv_fx[n_band], 16384 ) ); /*Q3 */
    3301         197 :                     move16();
    3302             :                 }
    3303             :             }
    3304             : 
    3305         545 :             FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3306             :             {
    3307         436 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 29491 );
    3308         436 :                 move16(); /*Q3 */
    3309             :             }
    3310             :         }
    3311             : 
    3312         145 :         test();
    3313         145 :         test();
    3314         145 :         IF( IsTransient_LF == 0 && st_fx->coder_type == INACTIVE && EQ_16( st_fx->TransientHangOver, 1 ) )
    3315             :         {
    3316          35 :             FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3317             :             {
    3318          28 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 16384 );
    3319          28 :                 move16();
    3320             :             }
    3321          35 :             FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    3322             :             {
    3323          28 :                 SWB_fenv_fx[n_band] = mult_r( SWB_fenv_fx[n_band], 1638 );
    3324          28 :                 move16();
    3325             :             }
    3326             :         }
    3327             :         ELSE
    3328             :         {
    3329         138 :             SWB_fenv_fx[2] = mult_r( SWB_fenv_fx[2], 3277 );
    3330         138 :             move16();
    3331         138 :             SWB_fenv_fx[3] = mult_r( SWB_fenv_fx[3], 1638 );
    3332         138 :             move16();
    3333             :         }
    3334             : 
    3335         725 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3336             :         {
    3337         580 :             IF( SWB_tenv_fx[n_band] == 0 )
    3338             :             {
    3339          45 :                 SWB_tenv_tmp_fx[n_band] = -32768;
    3340          45 :                 move16(); /*-16 in Q11 */
    3341             :             }
    3342             :             ELSE
    3343             :             {
    3344         535 :                 L_tmp = L_deposit_h( SWB_tenv_fx[n_band] ); /*Q19 */
    3345         535 :                 expn = norm_l( L_tmp );
    3346         535 :                 tmp = Log2_norm_lc( L_shl( L_tmp, expn ) );
    3347         535 :                 expn = sub( sub( 30, expn ), 19 );
    3348         535 :                 L_tmp = Mpy_32_16( expn, tmp, 32767 ); /* Q16 */          /*1 in Q15 */
    3349         535 :                 SWB_tenv_tmp_fx[n_band] = round_fx( L_shl( L_tmp, 11 ) ); /* Q11 */
    3350         535 :                 move16();
    3351             :             }
    3352             : 
    3353         580 :             IF( GT_16( SWB_tenv_tmp_fx[n_band], 30720 ) )
    3354             :             {
    3355           0 :                 index = 15;
    3356           0 :                 move16();
    3357             :             }
    3358         580 :             ELSE IF( SWB_tenv_tmp_fx[n_band] < 0 )
    3359             :             {
    3360          63 :                 index = 0;
    3361          63 :                 move16();
    3362             :             }
    3363             :             ELSE
    3364             :             {
    3365         517 :                 index = shr( add( SWB_tenv_tmp_fx[n_band], 1024 ), 11 );
    3366             :             }
    3367             : 
    3368         580 :             push_indice( hBstr, IND_SWB_TENV, index, 4 );
    3369             :         }
    3370             : 
    3371         145 :         MSVQ_Interpol_Tran_fx( SWB_fenv_fx, indice );
    3372             : 
    3373         145 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 7 );
    3374         145 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 6 );
    3375             :     }
    3376             :     ELSE
    3377             :     {
    3378             :         /* Energy for the different bands and global energies */
    3379       13544 :         global_gain_fx = L_deposit_l( 0 );
    3380      203160 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    3381             :         {
    3382      189616 :             energy_fx_64 = W_deposit32_l( 0 );
    3383     4523696 :             FOR( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ )
    3384             :             {
    3385     4334080 :                 W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */
    3386     4334080 :                 energy_fx_64 = W_add( W_tmp, energy_fx_64 );               /*2*Q_synth */
    3387             :             }
    3388      189616 :             q_shift = W_norm( energy_fx_64 );
    3389      189616 :             energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/
    3390      189616 :             q_shift = sub( q_shift, 32 );
    3391             : 
    3392      189616 :             IF( LT_16( n_band, SWB_FENV - 2 ) )
    3393             :             {
    3394      162528 :                 global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( add( shl( Q_synth, 1 ), q_shift ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */
    3395             :             }
    3396      189616 :             L_SWB_fenv_fx[n_band] = energy_fx;
    3397      189616 :             move32();
    3398      189616 :             q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift );
    3399      189616 :             move16();
    3400             :         }
    3401             : 
    3402       13544 :         global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */
    3403             : 
    3404       13544 :         scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ );
    3405       13544 :         Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale );
    3406       13544 :         mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx );
    3407       13544 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    3408             : 
    3409       13544 :         energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) );
    3410             : 
    3411      203160 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    3412             :         {
    3413      189616 :             L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], energy_factor_fx[n_band] ); /*q_SWB_fenv[n_band] */
    3414      189616 :             L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] );     /*q_SWB_fenv[n_band] */
    3415             : 
    3416      189616 :             IF( L_tmp != 0 )
    3417             :             {
    3418      184184 :                 expn = norm_l( L_tmp );
    3419      184184 :                 tmp = Log2_norm_lc( L_shl( L_tmp, expn ) );
    3420      184184 :                 expn = sub( 30, add( expn, q_SWB_fenv[n_band] ) );
    3421      184184 :                 L_tmp = Mpy_32_16( expn, tmp, 24660 ); /* Q14 */      /*10log10(2) in Q13 */
    3422      184184 :                 SWB_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */
    3423      184184 :                 move16();
    3424             :             }
    3425             :             ELSE
    3426             :             {
    3427        5432 :                 SWB_fenv_fx[n_band] = -24576;
    3428        5432 :                 move16();
    3429             :             }
    3430             :         }
    3431       13544 :         freq_weights_fx( SWB_fenv_fx, w_NOR_fx, w_env_fx, SWB_FENV );
    3432             : 
    3433      203160 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    3434             :         {
    3435      189616 :             SWB_fenv_fx[n_band] = sub( SWB_fenv_fx[n_band], Mean_env_fx[n_band] );
    3436      189616 :             move16();
    3437             :         }
    3438             : 
    3439             :         /* Energy VQ */
    3440       13544 :         msvq_interpol_fx( SWB_fenv_fx, w_env_fx, indice );
    3441             : 
    3442       13544 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 5 );
    3443       13544 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 7 );
    3444       13544 :         push_indice( hBstr, IND_SWB_FENV, indice[2], 6 );
    3445       13544 :         push_indice( hBstr, IND_SWB_FENV, indice[3], 5 );
    3446       13544 :         push_indice( hBstr, IND_SWB_FENV, indice[4], 6 );
    3447             :     }
    3448       13689 :     hBWE_FD->prev_mode = mode;
    3449       13689 :     move16();
    3450       13689 :     hBWE_FD->prev_global_gain_fx = global_gain_fx;
    3451       13689 :     move32();
    3452       13689 :     st_fx->prev_Q_shb = Q_shb;
    3453       13689 :     move16();
    3454             : 
    3455       13689 :     return mode;
    3456             : }
    3457             : /*-------------------------------------------------------------------*
    3458             :  * get_normalize_spec_fx_32()
    3459             :  *
    3460             :  *-------------------------------------------------------------------*/
    3461             : 
    3462        3499 : static void get_normalize_spec_fx_32(
    3463             :     const Word16 core,       /* i  : core selected           : Q0  */
    3464             :     const Word16 extl,       /* i  : extension layer selected: Q0  */
    3465             :     const Word16 mode,       /* i  : SHB BWE class           : Q0  */
    3466             :     const Word16 core_type,  /* i  : coding type             : Q0  */
    3467             :     const Word32 *org_fx,    /* i  : input spectrum          : Q12 */
    3468             :     Word32 *SWB_signal_fx,   /* o  : output spectrum         : Q20 */
    3469             :     Word16 *prev_L_swb_norm, /* i  : previous norm. len      : Q0  */
    3470             :     const Word16 offset      /* i  : frequency offset        : Q0  */
    3471             : )
    3472             : {
    3473             :     Word16 n_freq, L_swb_norm;
    3474             :     Word16 frq_end;
    3475             :     Word16 exp1, exp2, tmp;
    3476             :     Word32 L_tmp;
    3477             :     Word32 envelope_fx[L_FRAME32k];
    3478             : 
    3479        3499 :     set32_fx( SWB_signal_fx, 0, add( HQ_GENERIC_HIGH0, offset ) );
    3480        3499 :     calc_normal_length_fx_32( core, org_fx, mode, extl, &L_swb_norm, prev_L_swb_norm );
    3481        3499 :     test();
    3482        3499 :     IF( EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
    3483             :     {
    3484           0 :         IF( EQ_16( mode, HARMONIC ) )
    3485             :         {
    3486           0 :             Copy32( org_fx, &SWB_signal_fx[add( 240, offset )], 240 );
    3487           0 :             Copy32( &org_fx[128], &SWB_signal_fx[add( 480, offset )], 80 );
    3488             :         }
    3489             :         ELSE
    3490             :         {
    3491           0 :             Copy32( &org_fx[112], &SWB_signal_fx[add( 240, offset )], 128 );
    3492           0 :             Copy32( &org_fx[112], &SWB_signal_fx[add( 368, offset )], 128 );
    3493           0 :             Copy32( &org_fx[176], &SWB_signal_fx[add( 496, offset )], 64 );
    3494             :         }
    3495           0 :         frq_end = add( 560, offset );
    3496           0 :         move16();
    3497             :     }
    3498        3499 :     ELSE IF( EQ_16( extl, WB_BWE ) )
    3499             :     {
    3500           0 :         IF( core_type == 0 )
    3501             :         {
    3502           0 :             Copy32( &org_fx[160], &SWB_signal_fx[240], 80 );
    3503             :         }
    3504             :         ELSE
    3505             :         {
    3506           0 :             Copy32( &org_fx[80], &SWB_signal_fx[240], 80 );
    3507             :         }
    3508           0 :         frq_end = L_FRAME16k;
    3509           0 :         move16();
    3510             :     }
    3511             :     ELSE
    3512             :     {
    3513        3499 :         Copy32( org_fx + HQ_GENERIC_OFFSET, SWB_signal_fx + HQ_GENERIC_HIGH0 + offset, HQ_GENERIC_LEN0 );
    3514        3499 :         Copy32( org_fx + HQ_GENERIC_OFFSET, SWB_signal_fx + HQ_GENERIC_HIGH1 + offset, HQ_GENERIC_LEN0 );
    3515        3499 :         IF( EQ_16( offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3516             :         {
    3517        3498 :             Copy32( org_fx + HQ_GENERIC_LOW0, SWB_signal_fx + HQ_GENERIC_HIGH2 + offset, HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
    3518             :         }
    3519        3499 :         frq_end = L_FRAME32k;
    3520        3499 :         move16();
    3521             :     }
    3522             : 
    3523             :     /* calculate envelope */
    3524        3499 :     calc_norm_envelop_fx_32( SWB_signal_fx, envelope_fx, L_swb_norm, frq_end - offset, offset );
    3525             : 
    3526             :     /* Normalize with envelope */
    3527     1123115 :     FOR( n_freq = add( swb_bwe_subband[0], offset ); n_freq < frq_end; n_freq++ )
    3528             :     {
    3529     1119616 :         IF( envelope_fx[n_freq] != 0 )
    3530             :         {
    3531     1119616 :             exp1 = norm_l( envelope_fx[n_freq] );
    3532     1119616 :             exp2 = norm_l( SWB_signal_fx[n_freq] );
    3533     1119616 :             tmp = extract_h( L_shl( envelope_fx[n_freq], exp1 ) );                /*12 + exp1 - 16 */
    3534     1119616 :             tmp = div_s( 16384, tmp );                                            /*15 + 14 - (12 + exp1 - 16) */
    3535     1119616 :             L_tmp = Mult_32_16( L_shl( SWB_signal_fx[n_freq], exp2 ), tmp );      /*exp2 + 12 + (15 + 14 - (12 + exp1 - 16)) - 15 */
    3536     1119616 :             SWB_signal_fx[n_freq] = L_shr( L_tmp, sub( 10, sub( exp1, exp2 ) ) ); /*20 */
    3537     1119616 :             move32();
    3538             :         }
    3539             :         ELSE
    3540             :         {
    3541           0 :             SWB_signal_fx[n_freq] = 0;
    3542           0 :             move32(); /*20 */
    3543             :         }
    3544             :     }
    3545             : 
    3546        3499 :     return;
    3547             : }
    3548             : 
    3549             : /*-------------------------------------------------------------------*
    3550             :  * calculate_tonality_fx_32()
    3551             :  *
    3552             :  *-------------------------------------------------------------------*/
    3553       48984 : static void calculate_tonality_fx_32(
    3554             :     const Word32 *org_fx, /* i  : MDCT coefficients of original         : Q12  */
    3555             :     const Word32 *gen_fx, /* i  : MDCT coefficients of generated signal : Q12  */
    3556             :     Word32 *SFM_org,      /* o  : Spectral Flatness results             : Q14  */
    3557             :     Word32 *SFM_gen,      /* o  : Spectral Flatness results             : Q14  */
    3558             :     const Word16 length   /* i  : length for calculating tonality       : Q0   */
    3559             : )
    3560             : {
    3561             :     Word16 n_coeff;
    3562             :     Word32 am_org_fx, am_gen_fx, log_gm_org_sum_fx, log_gm_gen_sum_fx;
    3563             :     Word16 exp, exp1, exp2, tmp;
    3564             :     Word32 L_tmp, L_tmp1, L_tmp2;
    3565             :     Word16 inv_len_fx;
    3566             :     Word32 max_fx;
    3567             :     Word32 org_spec_fx[80], gen_spec_fx[80];
    3568             : 
    3569             :     /* to reduce dynamic range of original spectrum */
    3570       48984 :     max_fx = 0;
    3571       48984 :     move16();
    3572     1168600 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    3573             :     {
    3574     1119616 :         org_spec_fx[n_coeff] = L_abs( org_fx[n_coeff] );
    3575     1119616 :         move32();
    3576             : 
    3577     1119616 :         if ( LT_32( max_fx, org_spec_fx[n_coeff] ) )
    3578             :         {
    3579      178023 :             max_fx = org_spec_fx[n_coeff];
    3580      178023 :             move16();
    3581             :         }
    3582             :     }
    3583       48984 :     max_fx = 0;
    3584       48984 :     move16();
    3585     1168600 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    3586             :     {
    3587     1119616 :         gen_spec_fx[n_coeff] = L_abs( gen_fx[n_coeff] );
    3588     1119616 :         move32();
    3589     1119616 :         if ( LT_32( max_fx, gen_spec_fx[n_coeff] ) )
    3590             :         {
    3591      177560 :             max_fx = gen_spec_fx[n_coeff];
    3592      177560 :             move16();
    3593             :         }
    3594             :     }
    3595             : 
    3596       48984 :     exp = norm_s( length );
    3597       48984 :     inv_len_fx = div_s( shl( 1, exp ), shl( length, exp ) ); /*15 */
    3598             : 
    3599       48984 :     am_org_fx = 0;
    3600       48984 :     move16();
    3601       48984 :     am_gen_fx = 0;
    3602       48984 :     move16();
    3603       48984 :     log_gm_org_sum_fx = 0;
    3604       48984 :     move16();
    3605       48984 :     log_gm_gen_sum_fx = 0;
    3606       48984 :     move16();
    3607             : 
    3608     1168600 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    3609             :     {
    3610     1119616 :         am_org_fx = L_add_sat( am_org_fx, org_spec_fx[n_coeff] );
    3611     1119616 :         am_gen_fx = L_add_sat( am_gen_fx, gen_spec_fx[n_coeff] );
    3612     1119616 :         IF( org_spec_fx[n_coeff] != 0 )
    3613             :         {
    3614     1114614 :             exp = norm_l( org_spec_fx[n_coeff] );
    3615     1114614 :             tmp = Log2_norm_lc( L_shl( org_spec_fx[n_coeff], exp ) ); /*15 */
    3616     1114614 :             exp = sub( 30, add( exp, 12 ) );
    3617     1114614 :             L_tmp = L_add( L_shl( exp, 16 ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3618     1114614 :             log_gm_org_sum_fx = L_add( log_gm_org_sum_fx, L_tmp );              /*Q16 */
    3619             :         }
    3620     1119616 :         IF( gen_spec_fx[n_coeff] != 0 )
    3621             :         {
    3622     1118862 :             exp = norm_l( gen_spec_fx[n_coeff] );
    3623     1118862 :             tmp = Log2_norm_lc( L_shl( gen_spec_fx[n_coeff], exp ) );
    3624     1118862 :             exp = sub( 30, add( exp, 12 ) );
    3625     1118862 :             L_tmp = L_add( L_shl( exp, 16 ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3626     1118862 :             log_gm_gen_sum_fx = L_add( log_gm_gen_sum_fx, L_tmp );              /*16 */
    3627             :         }
    3628             :     }
    3629             : 
    3630       48984 :     IF( am_org_fx != 0 )
    3631             :     {
    3632       48984 :         exp1 = norm_l( am_org_fx );
    3633       48984 :         L_tmp1 = Mult_32_16( L_shl( am_org_fx, exp1 ), inv_len_fx ); /*12 + exp1 + 15 - 15 */
    3634       48984 :         exp2 = norm_l( L_tmp1 );                                     /*12 + exp1 + exp2 */
    3635       48984 :         tmp = Log2_norm_lc( L_shl( L_tmp1, exp2 ) );
    3636       48984 :         exp1 = sub( 30, add( add( exp1, exp2 ), 12 ) );
    3637       48984 :         L_tmp1 = Mpy_32_16( exp1, tmp, 24660 ); /*15 + 1 + 13 - 15  */
    3638             :     }
    3639             :     ELSE
    3640             :     {
    3641           0 :         L_tmp1 = 0;
    3642           0 :         move16();
    3643             :     }
    3644             : 
    3645       48984 :     exp = norm_l( log_gm_org_sum_fx );
    3646       48984 :     L_tmp2 = Mult_32_16( L_shl( log_gm_org_sum_fx, exp ), 24660 ); /*16 + exp + 13 - 15 */
    3647       48984 :     L_tmp2 = Mult_32_16( L_tmp2, inv_len_fx );                     /*14 + exp + 15 - 15 */
    3648       48984 :     L_tmp2 = L_shr( L_tmp2, exp );                                 /*14 */
    3649       48984 :     L_tmp = L_sub( L_tmp1, L_tmp2 );                               /*14 */
    3650             : 
    3651       48984 :     *SFM_org = L_max( 0, L_min( L_tmp, 98189 ) );
    3652       48984 :     move32();
    3653             : 
    3654       48984 :     IF( am_gen_fx != 0 )
    3655             :     {
    3656       48984 :         exp1 = norm_l( am_gen_fx );
    3657       48984 :         L_tmp1 = Mult_32_16( L_shl( am_gen_fx, exp1 ), inv_len_fx ); /*12 + exp1 + 15 - 15 */
    3658       48984 :         exp2 = norm_l( L_tmp1 );                                     /*12 + exp1 + exp2 */
    3659       48984 :         tmp = Log2_norm_lc( L_shl( L_tmp1, exp2 ) );
    3660       48984 :         exp1 = sub( 30, add( add( exp1, exp2 ), 12 ) );
    3661       48984 :         L_tmp1 = Mpy_32_16( exp1, tmp, 24660 ); /*15 + 1 + 13 - 15  */
    3662             :     }
    3663             :     ELSE
    3664             :     {
    3665           0 :         L_tmp1 = 0;
    3666           0 :         move16();
    3667             :     }
    3668             : 
    3669       48984 :     exp = norm_l( log_gm_gen_sum_fx );
    3670       48984 :     L_tmp2 = Mult_32_16( L_shl( log_gm_gen_sum_fx, exp ), 24660 ); /*16 + exp + 13 - 15 */
    3671       48984 :     L_tmp2 = Mult_32_16( L_tmp2, inv_len_fx );                     /*14 + 15 - 15 */
    3672       48984 :     L_tmp2 = L_shr( L_tmp2, exp );                                 /*14 */
    3673       48984 :     L_tmp = L_sub( L_tmp1, L_tmp2 );                               /*14 */
    3674             : 
    3675       48984 :     *SFM_gen = L_max( 0, L_min( L_tmp, 98189 ) );
    3676       48984 :     move32();
    3677             : 
    3678       48984 :     return;
    3679             : }
    3680             : 
    3681             : /*-------------------------------------------------------------------*
    3682             :  * energy_control_fx_32()
    3683             :  *
    3684             :  *-------------------------------------------------------------------*/
    3685        3499 : static void energy_control_fx_32(
    3686             :     Encoder_State *st_fx,    /* i/o: encoder structure   */
    3687             :     const Word16 core,       /* i  : core                : Q0  */
    3688             :     const Word16 mode,       /* i  : SHB BWE class       : Q0  */
    3689             :     const Word16 coder_type, /* i  : SHB BWE class       : Q0  */
    3690             :     const Word32 *org_fx,    /* i  : input spectrum      : Q12 */
    3691             :     const Word16 offset,     /* i  : frequency offset    : Q0  */
    3692             :     Word16 *energy_factor_fx /* o  : energy factor       : Q15 */
    3693             : )
    3694             : {
    3695             :     Word16 n_band;
    3696             :     Word16 core_type;
    3697             :     Word16 max_band, band_step;
    3698             :     Word32 SWB_signal_fx[L_FRAME32k];
    3699             :     Word32 SFM_org_fx[SWB_FENV], SFM_gen_fx[SWB_FENV];
    3700             :     Word32 L_temp1, L_temp2;
    3701             :     Word16 exp1, exp2, tmp1, tmp2, tmp;
    3702             :     Word16 gamma_fx;
    3703        3499 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    3704             : 
    3705        3499 :     max_band = SWB_FENV;
    3706        3499 :     move16();
    3707        3499 :     band_step = 1;
    3708        3499 :     move16();
    3709             : 
    3710        3499 :     IF( core == ACELP_CORE )
    3711             :     {
    3712           0 :         gamma_fx = 11468;
    3713           0 :         move16();
    3714           0 :         test();
    3715           0 :         IF( NE_16( coder_type, AUDIO ) && LE_32( st_fx->total_brate, ACELP_8k00 ) )
    3716             :         {
    3717           0 :             core_type = 0;
    3718           0 :             move16();
    3719             :         }
    3720             :         ELSE
    3721             :         {
    3722           0 :             core_type = 1;
    3723           0 :             move16();
    3724             :         }
    3725             : 
    3726           0 :         get_normalize_spec_fx_32( core, st_fx->extl, mode, core_type, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset );
    3727             : 
    3728           0 :         IF( EQ_16( st_fx->extl, WB_BWE ) )
    3729             :         {
    3730           0 :             max_band = 4;
    3731           0 :             move16();
    3732           0 :             band_step = 2;
    3733           0 :             move16();
    3734             :         }
    3735             :     }
    3736             :     ELSE /* HQ core */
    3737             :     {
    3738        3499 :         gamma_fx = 18021;
    3739        3499 :         move16();
    3740        3499 :         get_normalize_spec_fx_32( core, -1, mode, -1, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset );
    3741             : 
    3742        3499 :         if ( EQ_16( offset, HQ_GENERIC_FOFFSET_32K ) )
    3743             :         {
    3744           1 :             max_band = 12;
    3745           1 :             move16();
    3746             :         }
    3747             :     }
    3748             : 
    3749       52483 :     FOR( n_band = 0; n_band < max_band; n_band += band_step )
    3750             :     {
    3751       48984 :         calculate_tonality_fx_32( &org_fx[add( swb_bwe_subband[n_band], offset )], &SWB_signal_fx[add( swb_bwe_subband[n_band], offset )],
    3752       48984 :                                   &SFM_org_fx[n_band], &SFM_gen_fx[n_band], sub( swb_bwe_subband[add( n_band, band_step )], swb_bwe_subband[n_band] ) );
    3753             : 
    3754       48984 :         L_temp1 = L_shl( SFM_gen_fx[n_band], 2 );
    3755       48984 :         L_temp2 = L_add( SFM_org_fx[n_band], L_shl( SFM_org_fx[n_band], 1 ) );
    3756       48984 :         IF( LT_32( L_temp1, L_temp2 ) )
    3757             :         {
    3758        4621 :             exp1 = sub( norm_l( SFM_gen_fx[n_band] ), 1 );
    3759        4621 :             exp2 = norm_l( SFM_org_fx[n_band] );
    3760        4621 :             tmp1 = extract_h( L_shl( SFM_gen_fx[n_band], exp1 ) );
    3761        4621 :             tmp2 = extract_h( L_shl( SFM_org_fx[n_band], exp2 ) );
    3762        4621 :             tmp = div_s( tmp1, tmp2 );                                /*15 + (14 + exp1 ) - (14 + exp2) */
    3763        4621 :             energy_factor_fx[n_band] = shl( tmp, sub( exp2, exp1 ) ); /*15 */
    3764        4621 :             move16();
    3765             : 
    3766        4621 :             if ( LT_16( energy_factor_fx[n_band], gamma_fx ) )
    3767             :             {
    3768        1599 :                 energy_factor_fx[n_band] = gamma_fx;
    3769        1599 :                 move16();
    3770             :             }
    3771             :         }
    3772             :         ELSE
    3773             :         {
    3774       44363 :             energy_factor_fx[n_band] = 32767;
    3775       44363 :             move16(); /*15 */
    3776             :         }
    3777             :     }
    3778             : 
    3779        3499 :     return;
    3780             : }
    3781             : 
    3782             : /*-------------------------------------------------------------------*
    3783             :  * decision_hq_generic_class_fx_32()
    3784             :  *
    3785             :  *-------------------------------------------------------------------*/
    3786        3283 : static Word16 decision_hq_generic_class_fx_32(
    3787             :     const Word32 *coefs_fx,        /* i: original MDCT spectrum                      : Q12   */
    3788             :     const Word16 hq_generic_offset /* i: frequency offset of high frequency spectrum : Q0    */
    3789             : )
    3790             : {
    3791             :     Word16 i, k;
    3792             :     Word16 nband;
    3793             : 
    3794             :     Word16 inv_band_fx;
    3795             :     Word32 L_tmp, L_tmp1, L_tmp2;
    3796             :     Word16 exp, tmp, tmp2;
    3797             :     Word32 p_fx, a_fx;
    3798             :     Word32 p2a_fx;
    3799             :     Word32 avgp2a_fx;
    3800             : 
    3801        3283 :     IF( EQ_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3802             :     {
    3803        3282 :         nband = 10;
    3804        3282 :         move16();
    3805        3282 :         inv_band_fx = 3277;
    3806        3282 :         move16(); /*15 */
    3807             :     }
    3808             :     ELSE
    3809             :     {
    3810           1 :         nband = 8;
    3811           1 :         move16();
    3812           1 :         inv_band_fx = 4096;
    3813           1 :         move16(); /*15 */
    3814             :     }
    3815             : 
    3816        3283 :     avgp2a_fx = L_deposit_l( 0 );
    3817       36111 :     FOR( k = 0; k < nband; k++ )
    3818             :     {
    3819       32828 :         a_fx = L_deposit_l( 0 );
    3820       32828 :         p_fx = L_deposit_l( 0 );
    3821       32828 :         tmp2 = add( swb_bwe_subband[k + 1], hq_generic_offset );
    3822      715644 :         FOR( i = add( swb_bwe_subband[k], hq_generic_offset ); i < tmp2; i++ )
    3823             :         {
    3824      682816 :             exp = norm_l( coefs_fx[i] );
    3825      682816 :             tmp = extract_h( L_shl( coefs_fx[i], exp ) );         /*12 + exp - 16 */
    3826      682816 :             L_tmp = L_mult0( tmp, tmp );                          /*2 * exp - 8 */
    3827      682816 :             L_tmp = L_shl_sat( L_tmp, sub( 14, shl( exp, 1 ) ) ); /*6 */
    3828      682816 :             IF( GT_32( L_tmp, p_fx ) )
    3829             :             {
    3830      116149 :                 p_fx = L_add( L_tmp, 0 ); /*6 */
    3831             :             }
    3832      682816 :             a_fx = L_add_sat( a_fx, L_tmp ); /*6 */
    3833             :         }
    3834             : 
    3835       32828 :         IF( a_fx > 0 )
    3836             :         {
    3837       32828 :             a_fx = Mult_32_16( a_fx, swb_inv_bwe_subband_width_fx[k] ); /*6 */
    3838             : 
    3839       32828 :             exp = norm_l( p_fx );
    3840       32828 :             tmp = Log2_norm_lc( L_shl( p_fx, exp ) ); /*15 */
    3841       32828 :             exp = sub( 30, add( exp, 6 ) );
    3842       32828 :             L_tmp1 = L_add( L_deposit_h( exp ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3843             : 
    3844       32828 :             exp = norm_l( a_fx );
    3845       32828 :             tmp = Log2_norm_lc( L_shl( a_fx, exp ) );
    3846       32828 :             exp = sub( 30, add( exp, 6 ) );
    3847       32828 :             L_tmp2 = L_add( L_deposit_h( exp ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3848             : 
    3849       32828 :             p2a_fx = L_sub( L_tmp1, L_tmp2 );       /*16 */
    3850       32828 :             avgp2a_fx = L_add( avgp2a_fx, p2a_fx ); /*16 */
    3851             :         }
    3852             :     }
    3853        3283 :     avgp2a_fx = Mult_32_16( avgp2a_fx, inv_band_fx ); /*16 + 15 - 15 */
    3854        3283 :     IF( GT_32( avgp2a_fx, 187227 ) )                  /*8.6 / 10log10(2), Q16 */
    3855             :     {
    3856          45 :         return HQ_GENERIC_EXC1;
    3857             :     }
    3858             :     ELSE
    3859             :     {
    3860        3238 :         return HQ_GENERIC_EXC0;
    3861             :     }
    3862             : }
    3863             : 
    3864             : /*-------------------------------------------------------------------*
    3865             :  * hq_generic_encoding_fx()
    3866             :  *
    3867             :  *-------------------------------------------------------------------*/
    3868          46 : void hq_generic_encoding_fx(
    3869             :     const Word32 *coefs_fx,         /* i  : MDCT coefficients of weighted original : Q12   */
    3870             :     Word16 *hq_generic_fenv_fx,     /* i/o: energy of SWB envelope                 : Q3    */
    3871             :     const Word16 hq_generic_offset, /* i  : frequency offset for extracting energy : Q0    */
    3872             :     Encoder_State *st_fx,           /* i/o: encoder state structure                        */
    3873             :     Word16 *hq_generic_exc_clas     /* o  : bwe excitation class                   : Q0    */
    3874             : )
    3875             : {
    3876             :     Word16 n_coeff, n_band;
    3877             :     Word16 indice[HQ_GENERIC_NVQIDX];
    3878             :     Word16 nenv;
    3879             : 
    3880             :     Word16 energy_factor_fx[SWB_FENV];
    3881             :     Word16 cs, exp, tmp, tmp2;
    3882             :     Word32 energy_fx;
    3883             :     Word32 L_tmp, max_coefs_fx;
    3884             :     Word16 w_env_fx[SWB_FENV];
    3885          46 :     HQ_ENC_HANDLE hHQ_core = st_fx->hHQ_core;
    3886          46 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    3887             : 
    3888          46 :     set16_fx( energy_factor_fx, 0, SWB_FENV );
    3889             : 
    3890          46 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3891             :     {
    3892          46 :         nenv = SWB_FENV;
    3893          46 :         move16();
    3894             :     }
    3895             :     ELSE
    3896             :     {
    3897           0 :         nenv = SWB_FENV - 2;
    3898           0 :         move16();
    3899             :     }
    3900             : 
    3901             : 
    3902          46 :     energy_control_fx_32( st_fx, HQ_CORE, -1, -1, coefs_fx, hq_generic_offset, energy_factor_fx );
    3903             : 
    3904          46 :     IF( EQ_16( hHQ_core->hq_generic_speech_class, 1 ) )
    3905             :     {
    3906          21 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 1, 1 );
    3907          21 :         *hq_generic_exc_clas = HQ_GENERIC_SP_EXC;
    3908          21 :         move16();
    3909             :     }
    3910             :     ELSE
    3911             :     {
    3912          25 :         *hq_generic_exc_clas = decision_hq_generic_class_fx_32( coefs_fx, hq_generic_offset );
    3913          25 :         move16();
    3914          25 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 0, 1 );
    3915          25 :         push_indice( hBstr, IND_HQ_SWB_EXC_CLAS, *hq_generic_exc_clas, 1 );
    3916             :     }
    3917             : 
    3918         690 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    3919             :     {
    3920         644 :         energy_fx = L_deposit_l( 0 );
    3921         644 :         max_coefs_fx = L_deposit_l( 0 );
    3922         644 :         tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    3923       15364 :         FOR( n_coeff = swb_bwe_subband[n_band] + hq_generic_offset; n_coeff < tmp2; n_coeff++ )
    3924             :         {
    3925       14720 :             IF( LT_32( max_coefs_fx, L_abs( coefs_fx[n_coeff] ) ) )
    3926             :             {
    3927        2333 :                 max_coefs_fx = L_abs( coefs_fx[n_coeff] );
    3928             :             }
    3929             :         }
    3930         644 :         cs = norm_l( max_coefs_fx );
    3931         644 :         tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    3932       15364 :         FOR( n_coeff = swb_bwe_subband[n_band] + hq_generic_offset; n_coeff < tmp2; n_coeff++ )
    3933             :         {
    3934       14720 :             tmp = extract_h( L_shl( coefs_fx[n_coeff], cs ) ); /*12 + cs - 16 */
    3935       14720 :             L_tmp = L_mult0( tmp, tmp );                       /*2*cs - 8 */
    3936       14720 :             energy_fx = L_add( energy_fx, L_shr( L_tmp, 5 ) );
    3937             :         }
    3938             : 
    3939         644 :         IF( energy_fx != 0 )
    3940             :         {
    3941         644 :             L_tmp = Mult_32_16( energy_fx, energy_factor_fx[n_band] );         /*2*cs - 13 */
    3942         644 :             L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] ); /*2*cs - 13 + 15 - 15 */
    3943             : 
    3944         644 :             exp = norm_l( L_tmp );
    3945         644 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    3946         644 :             exp = sub( 30, add( exp, 2 * cs - 13 ) );
    3947             : 
    3948         644 :             L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */              /*10log10(2) in Q13 */
    3949         644 :             hq_generic_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /*Q8 */
    3950         644 :             move16();
    3951             :         }
    3952             :         ELSE
    3953             :         {
    3954           0 :             hq_generic_fenv_fx[n_band] = -24576;
    3955           0 :             move16();
    3956             :         }
    3957             :     }
    3958             : 
    3959          46 :     IF( EQ_16( st_fx->bwidth, FB ) )
    3960             :     {
    3961           0 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    3962             :         {
    3963           0 :             energy_fx = L_deposit_l( 0 );
    3964           0 :             max_coefs_fx = L_deposit_l( 0 );
    3965           0 :             tmp2 = fb_bwe_subband[add( n_band, 1 )];
    3966           0 :             FOR( n_coeff = fb_bwe_subband[n_band]; n_coeff < tmp2; n_coeff++ )
    3967             :             {
    3968           0 :                 IF( LT_32( max_coefs_fx, L_abs( coefs_fx[n_coeff] ) ) )
    3969             :                 {
    3970           0 :                     max_coefs_fx = L_abs( coefs_fx[n_coeff] );
    3971             :                 }
    3972             :             }
    3973           0 :             cs = norm_l( max_coefs_fx );
    3974           0 :             tmp2 = fb_bwe_subband[n_band + 1];
    3975           0 :             FOR( n_coeff = fb_bwe_subband[n_band]; n_coeff < tmp2; n_coeff++ )
    3976             :             {
    3977           0 :                 tmp = extract_h( L_shl( coefs_fx[n_coeff], cs ) ); /*12 + cs - 16 */
    3978           0 :                 L_tmp = L_mult0( tmp, tmp );                       /*2*cs - 8 */
    3979           0 :                 energy_fx = L_add( energy_fx, L_shr( L_tmp, 5 ) );
    3980             :             }
    3981             : 
    3982           0 :             IF( energy_fx != 0 )
    3983             :             {
    3984           0 :                 L_tmp = Mult_32_16( energy_fx, fb_inv_bwe_subband_width_fx[n_band] ); /*2*cs - 13 + 18 - 15 */
    3985             : 
    3986           0 :                 exp = norm_l( L_tmp );
    3987           0 :                 tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    3988           0 :                 exp = sub( 30, add( exp, 2 * cs - 13 ) );
    3989             : 
    3990           0 :                 L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */                     /*10log10(2) in Q13 */
    3991           0 :                 hq_generic_fenv_fx[n_band + nenv] = round_fx( L_shl( L_tmp, 10 ) ); /*Q8 */
    3992           0 :                 move16();
    3993             :             }
    3994             :             ELSE
    3995             :             {
    3996           0 :                 hq_generic_fenv_fx[n_band + nenv] = -24576;
    3997           0 :                 move16();
    3998             :             }
    3999             :         }
    4000             :     }
    4001             : 
    4002          46 :     freq_weights_fx( hq_generic_fenv_fx, w_NOR_fx, w_env_fx, nenv );
    4003             : 
    4004         690 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4005             :     {
    4006         644 :         hq_generic_fenv_fx[n_band] = sub( hq_generic_fenv_fx[n_band], Mean_env_fx[n_band] );
    4007         644 :         move16();
    4008             :     }
    4009             : 
    4010          46 :     IF( st_fx->bwidth == FB )
    4011             :     {
    4012           0 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4013             :         {
    4014           0 :             hq_generic_fenv_fx[n_band + nenv] = sub( shr( hq_generic_fenv_fx[n_band + nenv], 1 ), Mean_env_fb_fx[n_band] );
    4015           0 :             move16();
    4016             :         }
    4017             :     }
    4018             : 
    4019             : 
    4020             :     /* Energy VQ */
    4021          46 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4022             :     {
    4023          46 :         msvq_interpol_fx( hq_generic_fenv_fx, w_env_fx, indice );
    4024             :     }
    4025             :     ELSE
    4026             :     {
    4027           0 :         msvq_interpol_2_fx( hq_generic_fenv_fx, w_env_fx, indice, nenv );
    4028             :     }
    4029             : 
    4030          46 :     IF( EQ_16( st_fx->bwidth, FB ) )
    4031             :     {
    4032           0 :         indice[5] = vqSimple_w_fx( hq_generic_fenv_fx + nenv, hq_generic_fenv_fx + nenv, EnvCdbkFB_fx, NULL, DIM_FB, N_CB_FB, 0 );
    4033           0 :         move16();
    4034             :     }
    4035             : 
    4036          46 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[0], 5 );
    4037          46 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[1], 7 );
    4038          46 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[2], 6 );
    4039          46 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[3], 5 );
    4040             : 
    4041          46 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4042             :     {
    4043          46 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 6 );
    4044             :     }
    4045             :     ELSE
    4046             :     {
    4047           0 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 5 );
    4048             :     }
    4049             : 
    4050          46 :     IF( EQ_16( st_fx->bwidth, FB ) )
    4051             :     {
    4052           0 :         push_indice( hBstr, IND_FB_FENV_HQ, indice[5], 5 );
    4053             :     }
    4054             : 
    4055         690 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4056             :     {
    4057         644 :         tmp = add( hq_generic_fenv_fx[n_band], Mean_env_fx[n_band] ); /*8 */
    4058         644 :         L_tmp = L_mult( tmp, 21771 );                                 /*26 */
    4059         644 :         L_tmp = L_shr( L_tmp, 10 );                                   /*16 */
    4060         644 :         L_Extract( L_tmp, &exp, &tmp );                               /* */
    4061         644 :         tmp = extract_l( Pow2( 13, tmp ) );
    4062         644 :         exp = sub( exp, 13 );
    4063         644 :         hq_generic_fenv_fx[n_band] = shl_sat( tmp, add( exp, 1 ) ); /*1 */
    4064         644 :         move16();
    4065             :     }
    4066             : 
    4067             : 
    4068          46 :     IF( EQ_16( st_fx->bwidth, FB ) )
    4069             :     {
    4070           0 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4071             :         {
    4072           0 :             tmp = add( hq_generic_fenv_fx[n_band + nenv], Mean_env_fb_fx[n_band] ); /*7 */
    4073           0 :             L_tmp = L_mult( tmp, 21771 );                                           /*25 */
    4074           0 :             L_tmp = L_shr( L_tmp, 9 );                                              /*16 */
    4075           0 :             L_Extract( L_tmp, &exp, &tmp );
    4076           0 :             tmp = extract_l( Pow2( 13, tmp ) );
    4077           0 :             exp = sub( exp, 13 );
    4078           0 :             hq_generic_fenv_fx[n_band + nenv] = shl( tmp, add( exp, 1 ) ); /*2 */
    4079           0 :             move16();
    4080             :         }
    4081             :     }
    4082             : 
    4083          46 :     return;
    4084             : }
    4085             : 
    4086             : /*-------------------------------------------------------------------*
    4087             :  * fd_bwe_enc_init_fx()
    4088             :  *
    4089             :  * Initialize FD BWE state structure at the encoder
    4090             :  *-------------------------------------------------------------------*/
    4091             : 
    4092        7520 : void fd_bwe_enc_init_fx(
    4093             :     FD_BWE_ENC_HANDLE hBWE_FD /* i/o: FD BWE data handle  */
    4094             : )
    4095             : {
    4096        7520 :     set16_fx( hBWE_FD->new_input_hp_fx, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS ) );
    4097        7520 :     hBWE_FD->Q_new_input_hp = 0;
    4098        7520 :     move16();
    4099        7520 :     set16_fx( hBWE_FD->old_input_fx, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) );
    4100        7520 :     set16_fx( hBWE_FD->old_input_wb_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS ) );
    4101        7520 :     set16_fx( hBWE_FD->old_input_lp_fx, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_NS ) );
    4102        7520 :     set16_fx( hBWE_FD->old_syn_12k8_16k_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
    4103        7520 :     hBWE_FD->prev_mode = NORMAL;
    4104        7520 :     move16();
    4105        7520 :     set16_fx( hBWE_FD->L_old_wtda_swb_fx, 0, L_FRAME48k );
    4106        7520 :     hBWE_FD->prev_L_swb_norm1 = 8;
    4107        7520 :     move16();
    4108        7520 :     hBWE_FD->prev_global_gain_fx = 0;
    4109        7520 :     move32();
    4110        7520 :     hBWE_FD->modeCount = 0;
    4111        7520 :     move16();
    4112        7520 :     hBWE_FD->EnergyLF_fx = 0;
    4113        7520 :     hBWE_FD->EnergyLF_exp = 0;
    4114        7520 :     hBWE_FD->mem_old_wtda_swb_fx = 0;
    4115        7520 :     move32();
    4116        7520 :     move16();
    4117        7520 :     move32();
    4118             : 
    4119        7520 :     hBWE_FD->prev_Q_input_lp = 0;
    4120        7520 :     move16();
    4121        7520 :     set16_fx( hBWE_FD->old_fdbwe_speech_fx, 0, L_FRAME48k );
    4122        7520 :     hBWE_FD->mem_deemph_old_syn_fx = 0;
    4123        7520 :     move16();
    4124             : 
    4125        7520 :     hBWE_FD->q_mem_deemph_old_syn = 0;
    4126        7520 :     move16();
    4127             :     // hBWE_FD->mem_old_wtda_swb = 0.0f;
    4128             : 
    4129        7520 :     return;
    4130             : }
    4131             : 
    4132             : /*-------------------------------------------------------------------*
    4133             :  * hq_generic_encoding_fx()
    4134             :  *
    4135             :  *-------------------------------------------------------------------*/
    4136        3453 : void hq_generic_hf_encoding_fx(
    4137             :     const Word32 *coefs_fx,         /* i  : MDCT coefficients of weighted original : Q12   */
    4138             :     Word16 *hq_generic_fenv_fx,     /* i/o: energy of SWB envelope                 : Q1    */
    4139             :     const Word16 hq_generic_offset, /* i  : frequency offset for extracting energy : Q0    */
    4140             :     Encoder_State *st_fx,           /* i/o: encoder state structure                        */
    4141             :     Word16 *hq_generic_exc_clas,    /* o  : bwe excitation class                   : Q0    */
    4142             :     Word16 length )
    4143             : {
    4144             :     Word16 n_coeff, n_band;
    4145             :     Word16 indice[HQ_GENERIC_NVQIDX];
    4146             :     Word16 nenv;
    4147             : 
    4148             :     Word16 energy_factor_fx[SWB_FENV];
    4149             :     Word16 cs, exp, tmp, tmp2;
    4150             :     Word32 energy_fx;
    4151             :     Word32 L_tmp, max_coefs_fx;
    4152             :     Word16 w_env_fx[SWB_FENV];
    4153        3453 :     HQ_ENC_HANDLE hHQ_core = st_fx->hHQ_core;
    4154        3453 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    4155             :     Word16 EnvCdbkFB_fx_loc[N_CB_FB * DIM_FB];
    4156             : 
    4157        3453 :     set16_fx( energy_factor_fx, 0, SWB_FENV );
    4158             : 
    4159        3453 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4160             :     {
    4161        3452 :         nenv = SWB_FENV;
    4162        3452 :         move16();
    4163             :     }
    4164             :     ELSE
    4165             :     {
    4166           1 :         nenv = SWB_FENV - 2;
    4167           1 :         move16();
    4168             :     }
    4169             : 
    4170             : 
    4171        3453 :     energy_control_fx_32( st_fx, HQ_CORE, -1, -1, coefs_fx, hq_generic_offset, energy_factor_fx );
    4172             : 
    4173        3453 :     IF( EQ_16( hHQ_core->hq_generic_speech_class, 1 ) )
    4174             :     {
    4175         195 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 1, 1 );
    4176         195 :         *hq_generic_exc_clas = HQ_GENERIC_SP_EXC;
    4177         195 :         move16();
    4178             :     }
    4179             :     ELSE
    4180             :     {
    4181        3258 :         *hq_generic_exc_clas = decision_hq_generic_class_fx_32( coefs_fx, hq_generic_offset );
    4182        3258 :         move16();
    4183        3258 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 0, 1 );
    4184        3258 :         push_indice( hBstr, IND_HQ_SWB_EXC_CLAS, *hq_generic_exc_clas, 1 );
    4185             :     }
    4186             : 
    4187       51793 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4188             :     {
    4189       48340 :         energy_fx = L_deposit_l( 0 );
    4190       48340 :         max_coefs_fx = L_deposit_l( 0 );
    4191       48340 :         tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    4192     1153236 :         FOR( n_coeff = add( swb_bwe_subband[n_band], hq_generic_offset ); n_coeff < tmp2; n_coeff++ )
    4193             :         {
    4194     1104896 :             IF( LT_32( max_coefs_fx, L_abs( coefs_fx[n_coeff] ) ) )
    4195             :             {
    4196      175690 :                 max_coefs_fx = L_abs( coefs_fx[n_coeff] );
    4197             :             }
    4198             :         }
    4199       48340 :         cs = norm_l( max_coefs_fx );
    4200       48340 :         tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    4201     1153236 :         FOR( n_coeff = add( swb_bwe_subband[n_band], hq_generic_offset ); n_coeff < tmp2; n_coeff++ )
    4202             :         {
    4203     1104896 :             tmp = extract_h( L_shl( coefs_fx[n_coeff], cs ) ); /*12 + cs - 16 */
    4204     1104896 :             L_tmp = L_mult0( tmp, tmp );                       /*2*cs - 8 */
    4205     1104896 :             energy_fx = L_add( energy_fx, L_shr( L_tmp, 5 ) );
    4206             :         }
    4207             : 
    4208       48340 :         IF( energy_fx != 0 )
    4209             :         {
    4210       48340 :             L_tmp = Mult_32_16( energy_fx, energy_factor_fx[n_band] );         /*2*cs - 13 */
    4211       48340 :             L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] ); /*2*cs - 13 + 15 - 15 */
    4212             : 
    4213       48340 :             exp = norm_l( L_tmp );
    4214       48340 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    4215       48340 :             exp = sub( 30, add( exp, sub( shl( cs, 1 ), 13 ) ) );
    4216             : 
    4217       48340 :             L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */              /*10log10(2) in Q13 */
    4218       48340 :             hq_generic_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /*Q8 */
    4219       48340 :             move16();
    4220             :         }
    4221             :         ELSE
    4222             :         {
    4223           0 :             hq_generic_fenv_fx[n_band] = -24576;
    4224           0 :             move16();
    4225             :         }
    4226             :     }
    4227             : 
    4228        3453 :     IF( EQ_16( length, L_SPEC48k ) )
    4229             :     {
    4230       10732 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4231             :         {
    4232        8049 :             energy_fx = L_deposit_l( 0 );
    4233        8049 :             max_coefs_fx = L_deposit_l( 0 );
    4234        8049 :             tmp2 = fb_bwe_subband[n_band + 1];
    4235      437329 :             FOR( n_coeff = fb_bwe_subband[n_band]; n_coeff < tmp2; n_coeff++ )
    4236             :             {
    4237      429280 :                 IF( LT_32( max_coefs_fx, L_abs( coefs_fx[n_coeff] ) ) )
    4238             :                 {
    4239       36178 :                     max_coefs_fx = L_abs( coefs_fx[n_coeff] );
    4240             :                 }
    4241             :             }
    4242        8049 :             cs = norm_l( max_coefs_fx );
    4243        8049 :             tmp2 = fb_bwe_subband[n_band + 1];
    4244      437329 :             FOR( n_coeff = fb_bwe_subband[n_band]; n_coeff < tmp2; n_coeff++ )
    4245             :             {
    4246      429280 :                 tmp = extract_h( L_shl( coefs_fx[n_coeff], cs ) ); /*12 + cs - 16 */
    4247      429280 :                 L_tmp = L_mult0( tmp, tmp );                       /*2*cs - 8 */
    4248      429280 :                 energy_fx = L_add( energy_fx, L_shr( L_tmp, 5 ) );
    4249             :             }
    4250             : 
    4251        8049 :             IF( energy_fx != 0 )
    4252             :             {
    4253        8049 :                 L_tmp = Mult_32_16( energy_fx, fb_inv_bwe_subband_width_fx[n_band] ); /*2*cs - 13 + 18 - 15 */
    4254             : 
    4255        8049 :                 exp = norm_l( L_tmp );
    4256        8049 :                 tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    4257        8049 :                 exp = sub( 30, add( exp, sub( shl( cs, 1 ), 13 ) ) );
    4258             : 
    4259        8049 :                 L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */                     /*10log10(2) in Q13 */
    4260        8049 :                 hq_generic_fenv_fx[n_band + nenv] = round_fx( L_shl( L_tmp, 10 ) ); /*Q8 */
    4261        8049 :                 move16();
    4262             :             }
    4263             :             ELSE
    4264             :             {
    4265           0 :                 hq_generic_fenv_fx[n_band + nenv] = -24576;
    4266           0 :                 move16();
    4267             :             }
    4268             :         }
    4269             :     }
    4270             : 
    4271        3453 :     freq_weights_fx( hq_generic_fenv_fx, w_NOR_fx, w_env_fx, nenv );
    4272             : 
    4273       51793 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4274             :     {
    4275       48340 :         hq_generic_fenv_fx[n_band] = sub( hq_generic_fenv_fx[n_band], Mean_env_fx[n_band] );
    4276       48340 :         move16();
    4277             :     }
    4278             : 
    4279        3453 :     IF( EQ_16( length, L_SPEC48k ) )
    4280             :     {
    4281       10732 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4282             :         {
    4283        8049 :             hq_generic_fenv_fx[n_band + nenv] = sub( shr( hq_generic_fenv_fx[n_band + nenv], 1 ), Mean_env_fb_fx[n_band] );
    4284        8049 :             move16();
    4285             :         }
    4286             :     }
    4287             : 
    4288             : 
    4289             :     /* Energy VQ */
    4290        3453 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4291             :     {
    4292        3452 :         msvq_interpol_fx( hq_generic_fenv_fx, w_env_fx, indice );
    4293             :     }
    4294             :     ELSE
    4295             :     {
    4296           1 :         msvq_interpol_2_fx( hq_generic_fenv_fx, w_env_fx, indice, nenv );
    4297             :     }
    4298             : 
    4299        3453 :     IF( EQ_16( length, L_SPEC48k ) )
    4300             :     {
    4301        2683 :         Word16 sf = getScaleFactor16( hq_generic_fenv_fx, add( DIM_FB, nenv ) );
    4302        2683 :         Scale_sig( hq_generic_fenv_fx, add( DIM_FB, nenv ), negate( sf ) );
    4303        2683 :         Copy_Scale_sig( EnvCdbkFB_fx, EnvCdbkFB_fx_loc, N_CB_FB * DIM_FB, negate( sf ) );
    4304             :         // Scaling done to handel overflow inside vqSimple_w_fx
    4305             : 
    4306        2683 :         indice[5] = vqSimple_w_fx( hq_generic_fenv_fx + nenv, hq_generic_fenv_fx + nenv, EnvCdbkFB_fx_loc, NULL, DIM_FB, N_CB_FB, 0 );
    4307        2683 :         move16();
    4308             : 
    4309        2683 :         Scale_sig( hq_generic_fenv_fx, add( DIM_FB, nenv ), sf );
    4310             :     }
    4311             : 
    4312        3453 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[0], 5 );
    4313        3453 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[1], 7 );
    4314        3453 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[2], 6 );
    4315        3453 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[3], 5 );
    4316             : 
    4317        3453 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4318             :     {
    4319        3452 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 6 );
    4320             :     }
    4321             :     ELSE
    4322             :     {
    4323           1 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 5 );
    4324             :     }
    4325             : 
    4326        3453 :     IF( EQ_16( length, L_SPEC48k ) )
    4327             :     {
    4328        2683 :         push_indice( hBstr, IND_FB_FENV_HQ, indice[5], 5 );
    4329             :     }
    4330             : 
    4331       51793 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4332             :     {
    4333       48340 :         tmp = add( hq_generic_fenv_fx[n_band], Mean_env_fx[n_band] ); /*8 */
    4334       48340 :         L_tmp = L_mult( tmp, 21771 );                                 /*26 */
    4335       48340 :         L_tmp = L_shr( L_tmp, 10 );                                   /*16 */
    4336       48340 :         L_Extract( L_tmp, &exp, &tmp );                               /* */
    4337       48340 :         tmp = extract_l( Pow2( 13, tmp ) );
    4338       48340 :         exp = sub( exp, 13 );
    4339       48340 :         hq_generic_fenv_fx[n_band] = shl_sat( tmp, add( exp, 1 ) ); /*1 */
    4340       48340 :         move16();
    4341             :     }
    4342             : 
    4343             : 
    4344        3453 :     IF( EQ_16( length, L_SPEC48k ) )
    4345             :     {
    4346       10732 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4347             :         {
    4348        8049 :             tmp = add( hq_generic_fenv_fx[n_band + nenv], Mean_env_fb_fx[n_band] ); /*7 */
    4349        8049 :             L_tmp = L_mult( tmp, 21771 );                                           /*25 */
    4350        8049 :             L_tmp = L_shr( L_tmp, 9 );                                              /*16 */
    4351        8049 :             L_Extract( L_tmp, &exp, &tmp );
    4352        8049 :             tmp = extract_l( Pow2( 13, tmp ) );
    4353        8049 :             exp = sub( exp, 13 );
    4354        8049 :             hq_generic_fenv_fx[n_band + nenv] = shl( tmp, add( exp, 1 ) ); /*1 */
    4355        8049 :             move16();
    4356             :         }
    4357             :     }
    4358             : 
    4359        3453 :     return;
    4360             : }

Generated by: LCOV version 1.14