LCOV - code coverage report
Current view: top level - lib_enc - swb_bwe_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 3fe45f3a13339b229bdd4169e742797bb5eb1c89 Lines: 1603 2078 77.1 %
Date: 2025-10-27 01:59:47 Functions: 26 29 89.7 %

          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       11634 : 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       11634 :     Word16 mode = 0;
     209       11634 :     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       11634 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
     217             :     Word16 WB_fenv_fx[SWB_FENV];
     218             : 
     219       11634 :     IF( st_fx->extl_brate > 0 )
     220             :     {
     221             :         /*---------------------------------------------------------------------*
     222             :          * Delay the original input signal to be synchronized with ACELP core synthesis
     223             :          *---------------------------------------------------------------------*/
     224        4158 :         set16_fx( old_input_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
     225        4158 :         Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
     226        4158 :         move16();
     227             : 
     228        4158 :         new_input_fx = old_input_fx + Sample_Delay_WB_BWE;
     229        4158 :         Copy( hBWE_FD->old_input_wb_fx, old_input_fx, Sample_Delay_WB_BWE );
     230        4158 :         Copy( new_wb_speech_fx, new_input_fx, L_FRAME16k );
     231        4158 :         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        4158 :         new_input_fx_exp = -1;
     240        4158 :         move16();
     241             : 
     242        4158 :         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        4158 :         direct_transform_fx( L_wtda_synth_fx, yorig_32, 0, L_FRAME16k, &new_input_fx_exp, st_fx->element_mode );
     248             : 
     249        4158 :         mode = WB_BWE_encoding_ivas_fx( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp );
     250        4158 :         move16();
     251        4158 :         push_indice( st_fx->hBstr, IND_WB_CLASS, sub( mode, 2 ), 1 );
     252             :     }
     253             : 
     254       11634 :     hBWE_FD->prev_mode = mode;
     255       11634 :     move16();
     256             : 
     257       11634 :     return;
     258             : }
     259             : 
     260             : /*-------------------------------------------------------------------*
     261             :  * swb_bwe_enc()
     262             :  *
     263             :  * SWB BWE encoder (only for 32kHz signals)
     264             :  *-------------------------------------------------------------------*/
     265       11705 : 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       11705 :     Word16 idxGain = 0;
     296       11705 :     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       11705 :     Word16 ener_adjust_quan_fx = 0;
     304       11705 :     move16();
     305             :     Word16 fb_band_begin;
     306             :     Word16 q_new_input_hp;
     307             : 
     308       11705 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
     309       11705 :     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       11705 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     316             :     {
     317        4789 :         inner_frame = L_FRAME48k;
     318        4789 :         move16();
     319        4789 :         inner_Fs = 48000;
     320        4789 :         move32();
     321             :     }
     322             :     ELSE
     323             :     {
     324        6916 :         inner_frame = L_FRAME32k;
     325        6916 :         move16();
     326        6916 :         inner_Fs = 32000;
     327        6916 :         move32();
     328             :     }
     329             : 
     330       11705 :     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       11705 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     333             :     {
     334        7160 :         Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
     335        7160 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     336        7160 :         IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
     337             :         {
     338           5 :             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        7160 :         Sample_Delay_LP = NS2SA( 12800, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS );
     341             : 
     342        7160 :         IF( st_fx->element_mode > EVS_MONO )
     343             :         {
     344        7160 :             Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
     345        7160 :             Sample_Delay_HP = sub( Sample_Delay_HP, NS2SA( 16000, DELAY_FIR_RESAMPL_NS ) );
     346             : 
     347        7160 :             IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) )
     348             :             {
     349        1931 :                 Copy( old_input_12k8_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP );
     350        1931 :                 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        7160 :         Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP );
     354        7160 :         Copy( old_input_12k8_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME - Sample_Delay_LP );
     355        7160 :         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        4545 :         Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
     360        4545 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     361        4545 :         IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) )
     362             :         {
     363           0 :             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        4545 :         Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS );
     366        4545 :         IF( st_fx->element_mode > EVS_MONO )
     367             :         {
     368        4545 :             Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) );
     369        4545 :             Sample_Delay_HP = sub( Sample_Delay_HP, NS2SA( 16000, DELAY_FIR_RESAMPL_NS ) );
     370             : 
     371        4545 :             IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) )
     372             :             {
     373         539 :                 Copy( old_input_16k_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP );
     374         539 :                 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        4545 :         Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP );
     378        4545 :         Copy( old_input_16k_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP );
     379        4545 :         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       11705 :     q_new_input_hp = s_min( Q_shb_speech, hBWE_FD->Q_new_input_hp );
     383       11705 :     IF( LT_16( Q_shb_speech, hBWE_FD->Q_new_input_hp ) )
     384             :     {
     385        1744 :         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        1744 :         Copy( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP );                                     // Q_shb_speech
     387             :     }
     388             :     ELSE
     389             :     {
     390        9961 :         Copy( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP );                                                                             // hBWE_FD->Q_new_input_hp
     391        9961 :         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       11705 :     hBWE_FD->Q_new_input_hp = Q_shb_speech;
     395       11705 :     move16();
     396       11705 :     Copy( shb_speech_fx + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp_fx, Sample_Delay_HP );
     397       11705 :     new_input_fx = old_input_fx + Sample_Delay_SWB_BWE;
     398       11705 :     Copy( hBWE_FD->old_input_fx, old_input_fx, Sample_Delay_SWB_BWE );
     399       11705 :     Copy( new_swb_speech_fx, new_input_fx, inner_frame );
     400       11705 :     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       11705 :     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       11705 :     new_input_fx_q = Q_new_swb_speech;
     413       11705 :     move16();
     414       11705 :     test();
     415       11705 :     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       11705 :     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       11705 :     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       11705 :     IF( st_fx->bwidth_sw_cnt > 0 )
     433             :     {
     434           9 :         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       11705 :     scl = sub( 16 + 8, new_input_fx_q );
     439             :     /* Possible to Upscale? */
     440       11705 :     IF( scl > 0 )
     441             :     {
     442             :         /* Yes */
     443             :         /* Calc Room to Upscale */
     444       11705 :         Q_synth = Find_Max_Norm32( yorig_32, inner_frame );
     445             :         /* Stay within MAX_Q_NEW_INPUT */
     446       11705 :         scl = s_min( Q_synth, scl );
     447             :     }
     448       11705 :     Copy_Scale_sig32_16( yorig_32, yorig_fx, inner_frame, scl );
     449       11705 :     Q_synth = add( sub( new_input_fx_q, 16 ), scl );
     450       11705 :     max = 0;
     451       11705 :     move16();
     452       11705 :     Q_synth_hf = 0;
     453       11705 :     move16();
     454       11705 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     455             :     {
     456        4545 :         scl = 300;
     457        4545 :         move16();
     458             :     }
     459             :     ELSE
     460             :     {
     461        7160 :         scl = 240;
     462        7160 :         move16();
     463             :     }
     464     5953485 :     FOR( i = scl; i < inner_frame; i++ )
     465             :     {
     466     5941780 :         max = s_max( max, abs_s( yorig_fx[i] ) );
     467             :     }
     468             : 
     469       11705 :     IF( max == 0 )
     470             :     {
     471         282 :         exp = 15;
     472         282 :         move16();
     473             :     }
     474             :     ELSE
     475             :     {
     476       11423 :         exp = norm_s( max );
     477             :     }
     478       11705 :     Copy_Scale_sig( &yorig_fx[scl], &yorig_fx[scl], sub( inner_frame, scl ), exp );
     479       11705 :     Q_synth_hf = add( exp, Q_synth );
     480             : 
     481       11705 :     test();
     482       11705 :     IF( EQ_16( st_fx->last_extl, SWB_BWE ) || EQ_16( st_fx->last_extl, FB_BWE ) )
     483             :     {
     484        9645 :         exp = norm_l( st_fx->EnergyLT_fx );
     485        9645 :         IF( GT_16( add( st_fx->EnergyLT_fx_exp, exp ), shl( sub( Q_synth_hf, 4 ), 1 ) ) )
     486             :         {
     487        9375 :             Q_shb = sub( Q_synth_hf, 4 );
     488        9375 :             st_fx->EnergyLT_fx = L_shr( st_fx->EnergyLT_fx, sub( st_fx->EnergyLT_fx_exp, shl( Q_shb, 1 ) ) );
     489        9375 :             move32();
     490             :         }
     491             :         ELSE
     492             :         {
     493         270 :             Q_shb = shr( add( st_fx->EnergyLT_fx_exp, exp ), 1 );
     494         270 :             if ( EQ_16( s_and( exp, 0x0001 ), 1 ) )
     495             :             {
     496           4 :                 exp = sub( exp, 1 );
     497             :             }
     498         270 :             st_fx->EnergyLT_fx = L_shl( st_fx->EnergyLT_fx, exp );
     499         270 :             move32();
     500             :         }
     501             :     }
     502             :     ELSE
     503             :     {
     504        2060 :         Q_shb = sub( Q_synth_hf, 4 );
     505             :     }
     506       11705 :     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       11705 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     510             :     {
     511        4789 :         fb_band_begin = FB_BAND_BEGIN;
     512        4789 :         move16();
     513        4789 :         if ( EQ_16( st_fx->L_frame, L_FRAME ) )
     514             :         {
     515         745 :             fb_band_begin = FB_BAND_BEGIN_12k8;
     516         745 :             move16();
     517             :         }
     518        4789 :         energy_fbe_fb_fx = L_deposit_l( 0 );
     519      911509 :         FOR( i = fb_band_begin; i < FB_BAND_END; i++ )
     520             :         {
     521      906720 :             tmp = shr( yorig_fx[i], 4 );
     522      906720 :             energy_fbe_fb_fx = L_mac0( energy_fbe_fb_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */
     523             :         }
     524        4789 :         ener_low_fx = 0;
     525        4789 :         move16();
     526      866809 :         FOR( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ )
     527             :         {
     528      862020 :             tmp = shr( yorig_fx[i], 4 );
     529      862020 :             ener_low_fx = L_mac0( ener_low_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */
     530             :         }
     531             : 
     532        4789 :         IF( energy_fbe_fb_fx != 0 )
     533             :         {
     534        4789 :             exp = norm_l( energy_fbe_fb_fx );
     535        4789 :             frac = extract_h( L_shl( energy_fbe_fb_fx, exp ) );
     536        4789 :             tmp = div_s( 16384, frac );             /*15+14-(exp+2*(Q_synth_hf-4)-16) -->45-(exp+2*(Q_synth_hf-4)) */
     537        4789 :             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        4789 :             exp1 = norm_l( L_tmp );
     539        4789 :             L_tmp = L_shl( L_tmp, exp1 );
     540        4789 :             exp = sub( sub( 31, exp1 ), sub( 30, exp ) );
     541        4789 :             L_tmp = Isqrt_lc( L_tmp, &exp );                             /*31-exp */
     542        4789 :             fb_ener_adjust_fx = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /*Q15 */
     543             :         }
     544             :         ELSE
     545             :         {
     546           0 :             fb_ener_adjust_fx = 0;
     547           0 :             move16();
     548             :         }
     549             : 
     550        4789 :         fb_ener_adjust_fx = s_min( fb_ener_adjust_fx, 16384 ); /*Q15 */
     551        4789 :         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       11705 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     556             :     {
     557        4545 :         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        7160 :         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       11705 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
     569             :     {
     570        4789 :         push_indice( st_fx->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN );
     571             :     }
     572             : 
     573       11705 :     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        4158 : 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        4158 :     Word16 i, j, indx = 0;
     851        4158 :     move16();
     852             :     Word32 dist, min_dist;
     853        4158 :     const Word16 *pit = cb; /*Q10 */
     854             :     Word16 tmp;
     855             :     Word32 L_tmp;
     856             : 
     857        4158 :     min_dist = MAX_32;
     858        4158 :     move32();
     859      137214 :     FOR( i = 0; i < cb_length; i++ )
     860             :     {
     861      133056 :         dist = L_deposit_l( 0 );
     862      399168 :         FOR( j = 0; j < cb_dim; j++ )
     863             :         {
     864      266112 :             tmp = sub_sat( x[j], *pit ); /*Q10 */
     865      266112 :             L_tmp = L_mult0( tmp, tmp ); /*Q(10+10)->Q20 */
     866      266112 :             dist = L_add_sat( dist, L_tmp );
     867      266112 :             pit++;
     868             :         }
     869             : 
     870      133056 :         IF( LT_32( dist, min_dist ) )
     871             :         {
     872       18156 :             min_dist = dist;
     873       18156 :             move32();
     874       18156 :             indx = i;
     875       18156 :             move16();
     876             :         }
     877             :     }
     878             : 
     879       12474 :     FOR( j = 0; j < cb_dim; j++ )
     880             :     {
     881        8316 :         x[j] = cb[cb_dim * indx + j];
     882        8316 :         move16();
     883             :     }
     884             : 
     885        4158 :     return ( indx );
     886             : }
     887             : 
     888       15785 : 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       15785 :     set16_fx( SWB_signal, 0, add( HQ_GENERIC_HIGH0, offset ) );
     907       15785 :     calc_normal_length_fx( core, org_fx, mode, extl, &L_swb_norm, prev_L_swb_norm, Q_new_lf );
     908             : 
     909       15785 :     test();
     910       15785 :     IF( EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
     911             :     {
     912       11627 :         IF( EQ_16( mode, HARMONIC ) )
     913             :         {
     914         301 :             Copy( org_fx, &SWB_signal[add( 240, offset )], 240 );
     915         301 :             Copy( &org_fx[128], &SWB_signal[add( 480, offset )], 80 );
     916             :         }
     917             :         ELSE
     918             :         {
     919       11326 :             Copy( &org_fx[112], &SWB_signal[add( 240, offset )], 128 );
     920       11326 :             Copy( &org_fx[112], &SWB_signal[add( 368, offset )], 128 );
     921       11326 :             Copy( &org_fx[176], &SWB_signal[add( 496, offset )], 64 );
     922             :         }
     923       11627 :         frq_end = add( 560, offset );
     924             :     }
     925        4158 :     ELSE IF( EQ_16( extl, WB_BWE ) )
     926             :     {
     927        4158 :         IF( core_type == 0 )
     928             :         {
     929          11 :             Copy( &org_fx[160], &SWB_signal[240], 80 );
     930             :         }
     931             :         ELSE
     932             :         {
     933        4147 :             Copy( &org_fx[80], &SWB_signal[240], 80 );
     934             :         }
     935        4158 :         frq_end = L_FRAME16k;
     936        4158 :         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       15785 :     calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, sub( frq_end, offset ), offset );
     951             : 
     952             :     /* Normalize with envelope */
     953     4069065 :     FOR( n_freq = add( swb_bwe_subband[0], offset ); n_freq < frq_end; n_freq++ )
     954             :     {
     955     4053280 :         IF( envelope[n_freq] != 0 )
     956             :         {
     957     3964320 :             exp = norm_l( envelope[n_freq] );
     958     3964320 :             tmp = extract_h( L_shl( envelope[n_freq], exp ) );
     959     3964320 :             exp = sub( sub( 30, exp ), Q_new_lf );
     960     3964320 :             tmp = div_s( 16384, tmp );                                                   /*Q(15+exp) */
     961     3964320 :             L_tmp_m = L_shr( L_mult0( SWB_signal[n_freq], tmp ), add( exp, Q_new_lf ) ); /*Q15 */
     962     3964320 :             SWB_signal[n_freq] = extract_l( L_tmp_m );                                   /*Q15 */
     963     3964320 :             move16();
     964             :         }
     965             :         ELSE
     966             :         {
     967       88960 :             SWB_signal[n_freq] = 1;
     968       88960 :             move16();
     969             :         }
     970             :     }
     971             : 
     972       15785 :     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       15785 : 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       15785 :     Word16 i, j, k, noise, sharpMod = 0;
     991       15785 :     move16();
     992             :     Word16 peak, mag;
     993             :     Word32 mean[20];
     994             :     Word16 sharpPeak;
     995       15785 :     const Word16 *input_hi = 0;
     996             :     Word16 sharp;
     997       15785 :     Word16 gain_tmp = 0;
     998       15785 :     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       15785 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    1005             : 
    1006       15785 :     mode = NORMAL;
    1007       15785 :     move16();
    1008       15785 :     k = 0;
    1009       15785 :     move16();
    1010       15785 :     noise = 0;
    1011       15785 :     move16();
    1012       15785 :     sharpPeak = 0;
    1013       15785 :     move16();
    1014       15785 :     numsharp = 0;
    1015       15785 :     move16();
    1016       15785 :     numharmonic = 4;
    1017       15785 :     move16();
    1018       15785 :     sharplimit = 10;
    1019       15785 :     move16();
    1020             : 
    1021       15785 :     L_mean_d = 0L; /* to avoid compilation warnings */
    1022       15785 :     move32();
    1023             : 
    1024       15785 :     test();
    1025       15785 :     IF( EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
    1026             :     {
    1027       11627 :         input_hi = &fSpectrum[256];
    1028       11627 :         numsharp = NUM_SHARP;
    1029       11627 :         move16();
    1030             : 
    1031       11627 :         test();
    1032       11627 :         test();
    1033       11627 :         test();
    1034       11627 :         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        9566 :             IF( hBWE_FD->prev_global_gain_fx == 0 )
    1037             :             {
    1038         268 :                 gain_tmp = round_fx_sat( L_shl_sat( fGain, 30 ) ); /*Q14 */
    1039             :             }
    1040             :             ELSE
    1041             :             {
    1042        9298 :                 expn = norm_l( fGain );
    1043        9298 :                 num = extract_h( L_shl( fGain, expn ) );
    1044        9298 :                 expn = sub( sub( 30, expn ), shl( Q_shb, 1 ) );
    1045             : 
    1046        9298 :                 expd = norm_l( hBWE_FD->prev_global_gain_fx );
    1047        9298 :                 den = extract_h( L_shl( hBWE_FD->prev_global_gain_fx, expd ) );
    1048        9298 :                 expd = sub( sub( 30, expd ), shl( st_fx->prev_Q_shb, 1 ) );
    1049             : 
    1050        9298 :                 scale = shr( sub( den, num ), 15 );
    1051        9298 :                 num = shl_sat( num, scale );
    1052        9298 :                 expn = sub( expn, scale );
    1053             : 
    1054        9298 :                 tmp = div_s( num, den );
    1055        9298 :                 expn = sub( expn, expd );
    1056        9298 :                 gain_tmp = shl_sat( tmp, sub( expn, 1 ) ); /*Q14 */
    1057             :             }
    1058        9566 :             test();
    1059        9566 :             IF( EQ_16( hBWE_FD->prev_mode, TRANSIENT ) )
    1060             :             {
    1061          31 :                 numharmonic = shl( numharmonic, 1 );
    1062             :             }
    1063        9535 :             ELSE IF( EQ_16( hBWE_FD->prev_mode, NORMAL ) || hBWE_FD->prev_mode == NOISE )
    1064             :             {
    1065        9332 :                 numharmonic = add( shr( numharmonic, 1 ), numharmonic );
    1066             :             }
    1067             :         }
    1068             :         ELSE
    1069             :         {
    1070        2061 :             gain_tmp = 16384;
    1071        2061 :             move16();
    1072        2061 :             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        2061 :                 numharmonic = shl( numharmonic, 1 );
    1080        2061 :                 sharplimit = shl( sharplimit, 1 );
    1081             :             }
    1082             :         }
    1083             :     }
    1084        4158 :     ELSE IF( EQ_16( st_fx->extl, WB_BWE ) )
    1085             :     {
    1086        4158 :         input_hi = &fSpectrum[224];
    1087        4158 :         numsharp = 3;
    1088        4158 :         move16();
    1089             : 
    1090        4158 :         IF( EQ_16( hBWE_FD->prev_mode, HARMONIC ) )
    1091             :         {
    1092        1462 :             numharmonic = shr( numharmonic, 2 );
    1093             :         }
    1094             :         ELSE
    1095             :         {
    1096        2696 :             numharmonic = shr( numharmonic, 1 );
    1097             :         }
    1098        4158 :         IF( NE_16( st_fx->last_extl, WB_BWE ) )
    1099             :         {
    1100         122 :             IF( EQ_16( hBWE_FD->prev_mode, HARMONIC ) )
    1101             :             {
    1102           0 :                 sharplimit = shr( sharplimit, 1 );
    1103             :             }
    1104             :             ELSE
    1105             :             {
    1106         122 :                 sharplimit = shl( sharplimit, 1 );
    1107             :             }
    1108             :         }
    1109             :     }
    1110             : 
    1111       15785 :     L_meanH = L_deposit_l( 0 );
    1112      132902 :     FOR( i = 0; i < numsharp; i++ )
    1113             :     {
    1114      117117 :         peak = 0;
    1115      117117 :         move16();
    1116      117117 :         mean[i] = L_deposit_l( 0 );
    1117      117117 :         move32();
    1118             : 
    1119     3864861 :         FOR( j = 0; j < SHARP_WIDTH; j++ )
    1120             :         {
    1121     3747744 :             mag = abs_s( *input_hi );
    1122     3747744 :             if ( GT_16( mag, peak ) )
    1123             :             {
    1124      460720 :                 peak = mag;
    1125      460720 :                 move16(); /*Q_syn */
    1126             :             }
    1127     3747744 :             mean[i] = L_add( mean[i], mag );
    1128     3747744 :             move32(); /*Q_syn */
    1129     3747744 :             input_hi++;
    1130             :         }
    1131             : 
    1132      117117 :         L_meanH = L_add( L_meanH, mean[i] ); /*Q_syn */
    1133             : 
    1134      117117 :         IF( NE_32( mean[i], L_deposit_l( peak ) ) )
    1135             :         {
    1136      114615 :             L_tmp = L_sub( mean[i], peak );       /*Q_syn */
    1137      114615 :             L_tmp = Mpy_32_16_1( L_tmp, 16913 );  /* 1/31->Q19 -> Q_syn+19-15 */
    1138      114615 :             den = extract_l( L_shr( L_tmp, 4 ) ); /*Q_syn */
    1139      114615 :             if ( den == 0 )
    1140             :             {
    1141          47 :                 den = 1;
    1142          47 :                 move16();
    1143             :             }
    1144      114615 :             expd = norm_s( den );
    1145      114615 :             tmp = div_s( shl( 1, sub( 14, expd ) ), den );              /*Q(29-expd-Q_syn) */
    1146      114615 :             L_tmp = L_mult( tmp, peak );                                /*Q(30-expd) */
    1147      114615 :             sharp = round_fx_sat( L_shl_sat( L_tmp, sub( expd, 4 ) ) ); /*Q10 */
    1148             :         }
    1149             :         ELSE
    1150             :         {
    1151        2502 :             sharp = 0;
    1152        2502 :             move16();
    1153             :         }
    1154             : 
    1155      117117 :         test();
    1156      117117 :         IF( GT_16( sharp, 4608 ) && GT_16( peak, shl_sat( 1, add( Q_syn, 3 ) ) ) )
    1157             :         {
    1158       17023 :             k = add( k, 1 );
    1159       17023 :             move16();
    1160             :         }
    1161      100094 :         ELSE IF( LT_16( sharp, 3072 ) )
    1162             :         {
    1163       40844 :             noise = add( noise, 1 );
    1164       40844 :             move16();
    1165             :         }
    1166             : 
    1167      117117 :         if ( GT_16( sharp, sharpPeak ) )
    1168             :         {
    1169       38538 :             sharpPeak = sharp;
    1170       38538 :             move16();
    1171             :         }
    1172             :     }
    1173             : 
    1174       15785 :     test();
    1175       15785 :     IF( EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
    1176             :     {
    1177       11627 :         test();
    1178       11627 :         test();
    1179       11627 :         test();
    1180       11627 :         IF( GE_16( k, numharmonic ) && GT_16( gain_tmp, 8192 ) && LT_16( gain_tmp, 29491 ) && GT_16( sharpPeak, shl( sharplimit, 10 ) ) )
    1181             :         {
    1182         270 :             sharpMod = 1;
    1183             :         }
    1184             :         ELSE
    1185             :         {
    1186       11357 :             sharpMod = 0;
    1187             :         }
    1188       11627 :         move16();
    1189             : 
    1190       11627 :         L_meanH = Mpy_32_16_1( L_meanH, 29127 ); /*Q_syn+8 */
    1191       11627 :         L_mean_d = 0;
    1192       11627 :         move16();
    1193      116270 :         FOR( i = 0; i < NUM_SHARP; i++ )
    1194             :         {
    1195      104643 :             L_tmp = L_sub( L_shl( mean[i], 8 - 5 ), L_meanH ); /*Q_syn+8 */
    1196      104643 :             L_mean_d = L_add( L_mean_d, L_abs( L_tmp ) );      /*Q_syn+8 */
    1197             :         }
    1198             :     }
    1199        4158 :     ELSE IF( EQ_16( st_fx->extl, WB_BWE ) )
    1200             :     {
    1201        4158 :         test();
    1202        4158 :         IF( GE_16( k, numharmonic ) && GT_16( sharpPeak, shl( sharplimit, 10 ) ) )
    1203             :         {
    1204        1000 :             sharpMod = 1;
    1205             :         }
    1206             :         ELSE
    1207             :         {
    1208        3158 :             sharpMod = 0;
    1209             :         }
    1210        4158 :         move16();
    1211             :     }
    1212             : 
    1213       15785 :     test();
    1214       15785 :     test();
    1215       15785 :     IF( sharpMod && LT_16( hBWE_FD->modeCount, 12 ) )
    1216             :     {
    1217         936 :         hBWE_FD->modeCount = add( hBWE_FD->modeCount, 1 );
    1218         936 :         move16();
    1219             :     }
    1220       14849 :     ELSE IF( sharpMod == 0 && hBWE_FD->modeCount > 0 )
    1221             :     {
    1222         713 :         hBWE_FD->modeCount = sub( hBWE_FD->modeCount, 1 );
    1223         713 :         move16();
    1224             :     }
    1225             : 
    1226       15785 :     if ( GE_16( hBWE_FD->modeCount, 2 ) )
    1227             :     {
    1228        1544 :         sharpMod = 1;
    1229        1544 :         move16();
    1230             :     }
    1231             : 
    1232       15785 :     test();
    1233       15785 :     IF( sharpMod )
    1234             :     {
    1235        1769 :         mode = HARMONIC;
    1236        1769 :         move16();
    1237             :     }
    1238       14016 :     ELSE IF( EQ_16( st_fx->extl, SWB_BWE ) || EQ_16( st_fx->extl, FB_BWE ) )
    1239             :     {
    1240       11326 :         L_tmp = Mpy_32_16_1( L_mean_d, 6827 ); /*Q_syn+8 ; 1/4.8 in Q15 */
    1241             : 
    1242       11326 :         test();
    1243       11326 :         test();
    1244       11326 :         test();
    1245       11326 :         if ( GT_16( noise, 4 ) && ( LT_32( L_tmp, L_meanH ) || L_meanH == 0 ) && LT_16( tilt_nb, 10240 ) )
    1246             :         {
    1247         717 :             mode = NOISE;
    1248         717 :             move16();
    1249             :         }
    1250             :     }
    1251             : 
    1252       15785 :     return ( mode );
    1253             : }
    1254             : 
    1255             : /*-------------------------------------------------------------------*
    1256             :  * freq_weights_fx()
    1257             :  *
    1258             :  *-------------------------------------------------------------------*/
    1259       14488 : 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       14488 :     min_b = Band_Ener[0];
    1274       14488 :     move16();
    1275       14488 :     max_b = Band_Ener[0];
    1276       14488 :     move16();
    1277      202832 :     FOR( i = 1; i < Nbands; i++ )
    1278             :     {
    1279      188344 :         if ( LT_16( Band_Ener[i], min_b ) )
    1280             :         {
    1281       57902 :             min_b = Band_Ener[i];
    1282       57902 :             move16(); /*Q8 */
    1283             :         }
    1284             : 
    1285      188344 :         if ( GT_16( Band_Ener[i], max_b ) )
    1286             :         {
    1287       19505 :             max_b = Band_Ener[i];
    1288       19505 :             move16(); /*Q8 */
    1289             :         }
    1290             :     }
    1291             : 
    1292             :     /* Find weighting function */
    1293       14488 :     tmp = sub( max_b, min_b ); /*Q8 */
    1294       14488 :     IF( tmp != 0 )
    1295             :     {
    1296       14210 :         exp = norm_s( tmp );
    1297       14210 :         tmp = div_s( shl( 1, sub( 14, exp ) ), tmp ); /*(21-exp) */
    1298       14210 :         tmp = shl_sat( tmp, sub( exp, 6 ) );          /*Q15 */
    1299             :     }
    1300             :     ELSE
    1301             :     {
    1302         278 :         tmp = 32767;
    1303         278 :         move16();
    1304             :     }
    1305             : 
    1306      217320 :     FOR( i = 0; i < Nbands; i++ )
    1307             :     {
    1308      202832 :         tmp1 = sub( Band_Ener[i], min_b );
    1309      202832 :         L_tmp = L_mult( tmp1, tmp );      /*Q24 */
    1310      202832 :         L_tmp = L_add( L_tmp, 16777216 ); /*Q24 */
    1311      202832 :         L_tmp = L_shl( L_tmp, 5 );        /*Q29 */
    1312      202832 :         w1[i] = round_fx( L_tmp );        /*Q13 */
    1313      202832 :         move16();
    1314      202832 :         w2[i] = f_weighting[i];
    1315      202832 :         move16(); /*Q15                         */ /*1~0.75*/
    1316      202832 :         w_env[i] = mult_r( w1[i], w2[i] );
    1317      202832 :         move16(); /*Q13 */
    1318             :     }
    1319             : 
    1320       14488 :     return;
    1321             : }
    1322             : 
    1323             : /*-------------------------------------------------------------------*
    1324             :  * vqWithCand_w_fx()
    1325             :  *
    1326             :  *-------------------------------------------------------------------*/
    1327       14574 : 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       14574 :     IF( flag )
    1345             :     {
    1346       14488 :         set32_fx( dist_min, MAX_32, surv ); /* FLT_MAX */
    1347             : 
    1348       43464 :         FOR( i = 0; i < surv; i++ )
    1349             :         {
    1350       28976 :             index[i] = i;
    1351       28976 :             move16();
    1352             :         }
    1353             : 
    1354       14488 :         p_E_ROM_dico = E_ROM_dico;
    1355       14488 :         move16();
    1356             : 
    1357      478104 :         FOR( i = 0; i < E_ROM_dico_size; i++ )
    1358             :         {
    1359      463616 :             dist = sub_sat( x[0], *p_E_ROM_dico++ ); /*Q8 */
    1360      463616 :             L_dist = L_mult( dist, w[0] );           /*Q22 */
    1361      463616 :             L_dist = Mult_32_16( L_dist, dist );     /*Q15 */
    1362      463616 :             L_dist = L_shr( L_dist, 10 );            /*Q5 */
    1363             : 
    1364     3245312 :             FOR( j = 1; j < dim; j++ )
    1365             :             {
    1366     2781696 :                 temp1 = sub_sat( x[j], *p_E_ROM_dico++ );
    1367     2781696 :                 L_tmp = L_mult( temp1, w[j] );                /*Q22 */
    1368     2781696 :                 L_tmp = Mult_32_16( L_tmp, temp1 );           /*Q15 */
    1369     2781696 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) ); /*Q5 */
    1370             :             }
    1371             : 
    1372     1242991 :             FOR( k = 0; k < surv; k++ )
    1373             :             {
    1374      873117 :                 IF( LT_32( L_dist, dist_min[k] ) )
    1375             :                 {
    1376      147857 :                     FOR( l = sub( surv, 1 ); l > k; l-- )
    1377             :                     {
    1378       54115 :                         dist_min[l] = dist_min[l - 1];
    1379       54115 :                         move32();
    1380       54115 :                         index[l] = index[l - 1];
    1381       54115 :                         move16();
    1382             :                     }
    1383       93742 :                     dist_min[k] = L_dist;
    1384       93742 :                     move32();
    1385       93742 :                     index[k] = i;
    1386       93742 :                     move16();
    1387       93742 :                     BREAK;
    1388             :                 }
    1389             :             }
    1390             :         }
    1391             :     }
    1392             :     ELSE
    1393             :     {
    1394          86 :         set32_fx( dist_min, MAX_32, surv ); /* FLT_MAX */
    1395             : 
    1396         344 :         FOR( i = 0; i < surv; i++ )
    1397             :         {
    1398         258 :             index[i] = i;
    1399         258 :             move16();
    1400             :         }
    1401             : 
    1402          86 :         p_E_ROM_dico = E_ROM_dico;
    1403          86 :         move16();
    1404             : 
    1405       11094 :         FOR( i = 0; i < E_ROM_dico_size; i++ )
    1406             :         {
    1407       11008 :             dist = sub_sat( x[0], *p_E_ROM_dico++ ); /*Q8 */
    1408       11008 :             L_dist = L_mult_sat( dist, dist );       /*Q17 */
    1409       11008 :             L_dist = L_shr( L_dist, 12 );            /*Q5 */
    1410             : 
    1411       22016 :             FOR( j = 1; j < dim; j++ )
    1412             :             {
    1413       11008 :                 temp1 = sub( x[j], *p_E_ROM_dico++ );         /*Q8 */
    1414       11008 :                 L_tmp = L_mult( temp1, temp1 );               /*Q17 */
    1415       11008 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 12 ) ); /*Q5 */
    1416             :             }
    1417             : 
    1418       41695 :             FOR( k = 0; k < surv; k++ )
    1419             :             {
    1420       31734 :                 IF( LT_32( L_dist, dist_min[k] ) )
    1421             :                 {
    1422        2337 :                     FOR( l = sub( surv, 1 ); l > k; l-- )
    1423             :                     {
    1424        1290 :                         dist_min[l] = dist_min[l - 1];
    1425        1290 :                         move32();
    1426        1290 :                         index[l] = index[l - 1];
    1427        1290 :                         move16();
    1428             :                     }
    1429        1047 :                     dist_min[k] = L_dist;
    1430        1047 :                     move32();
    1431        1047 :                     index[k] = i;
    1432        1047 :                     move16();
    1433        1047 :                     BREAK;
    1434             :                 }
    1435             :             }
    1436             :         }
    1437             :     }
    1438             : 
    1439       14574 :     return;
    1440             : }
    1441             : 
    1442             : /*-------------------------------------------------------------------*
    1443             :  * vqSimple_w_fx()
    1444             :  *
    1445             :  *-------------------------------------------------------------------*/
    1446             : 
    1447      118372 : 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      118372 :     index = 0;
    1463      118372 :     move16();
    1464      118372 :     L_dist_min = L_add( MAX_32, 0 ); /* FLT_MAX */
    1465      118372 :     cbP = cb;
    1466      118372 :     move16();
    1467      118372 :     IF( flag )
    1468             :     {
    1469     8460992 :         FOR( i = 0; i < l; i++ )
    1470             :         {
    1471             :             /*dist = x[0] - *cbP++; */
    1472             :             /*dist *= (dist * w[0]); */
    1473     8345088 :             dist = sub( x[0], *cbP++ );          /*Q8 */
    1474     8345088 :             L_dist = L_mult( dist, w[0] );       /*Q22 */
    1475     8345088 :             L_dist = Mult_32_16( L_dist, dist ); /*Q15 */
    1476     8345088 :             L_dist = L_shr( L_dist, 10 );        /*Q5 */
    1477             : 
    1478    28744192 :             FOR( j = 1; j < dim; j++ )
    1479             :             {
    1480             :                 /*temp = x[j] - *cbP++; */
    1481             :                 /*dist += temp * temp * w[j]; */
    1482    20399104 :                 temp = sub( x[j], *cbP++ );
    1483    20399104 :                 L_tmp = L_mult( temp, w[j] );                 /*Q22 */
    1484    20399104 :                 L_tmp = Mult_32_16( L_tmp, temp );            /*Q15 */
    1485    20399104 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) ); /*Q5 */
    1486             :             }
    1487     8345088 :             IF( LT_32( L_dist, L_dist_min ) )
    1488             :             {
    1489      529645 :                 L_dist_min = L_add( L_dist, 0 ); /*Q5 */
    1490      529645 :                 index = i;
    1491      529645 :                 move16();
    1492             :             }
    1493             :         }
    1494             :     }
    1495             :     ELSE
    1496             :     {
    1497       89700 :         FOR( i = 0; i < l; i++ )
    1498             :         {
    1499             :             /*dist = x[0] - *cbP++; */
    1500       87232 :             dist = sub( x[0], *cbP++ );
    1501             :             /*dist *= dist; */
    1502       87232 :             L_dist = L_mult( dist, dist ); /*Q17 */
    1503       87232 :             L_dist = L_shr( L_dist, 12 );
    1504             : 
    1505      245184 :             FOR( j = 1; j < dim; j++ )
    1506             :             {
    1507             :                 /*temp = x[j] - *cbP++; */
    1508      157952 :                 temp = sub( x[j], *cbP++ );
    1509             :                 /*dist += temp * temp; */
    1510      157952 :                 L_tmp = L_mult( temp, temp );                 /*Q17 */
    1511      157952 :                 L_dist = L_add( L_dist, L_shr( L_tmp, 12 ) ); /*Q5 */
    1512             :             }
    1513       87232 :             IF( LT_32( L_dist, L_dist_min ) )
    1514             :             {
    1515        8526 :                 L_dist_min = L_add( L_dist, 0 );
    1516        8526 :                 index = i;
    1517        8526 :                 move16();
    1518             :             }
    1519             :         }
    1520             :     }
    1521             : 
    1522             : 
    1523             :     /* Reading the selected vector */
    1524      118372 :     Copy( &cb[index * dim], y, dim );
    1525             : 
    1526      118372 :     return ( index );
    1527             : }
    1528             : 
    1529             : 
    1530             : /*-------------------------------------------------------------------*
    1531             :  * MSVQ_Interpol_Tran_fx()
    1532             :  *
    1533             :  *-------------------------------------------------------------------*/
    1534          86 : 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         258 :     FOR( n_band = 0; n_band < DIM_TR1; n_band++ )
    1549             :     {
    1550         172 :         env_temp11[n_band] = SWB_env_energy[2 * n_band];
    1551         172 :         move16(); /*Q8 */
    1552         172 :         env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
    1553         172 :         move16(); /*Q8 */
    1554             :     }
    1555             : 
    1556          86 :     vqWithCand_w_fx( env_temp11, Env_TR_Cdbk1_fx, DIM_TR1, N_CB_TR1, candInd, N_CAND_TR, distCand, NULL, 0 );
    1557             : 
    1558          86 :     L_minDist = L_add( MAX_32, 0 ); /* FLT_MAX */
    1559             : 
    1560         344 :     FOR( k = 0; k < N_CAND_TR; k++ )
    1561             :     {
    1562         774 :         FOR( n_band = 0; n_band < DIM_TR1; n_band++ )
    1563             :         {
    1564         516 :             quant_tmp[n_band] = Env_TR_Cdbk1_fx[add( shl( candInd[k], 1 ), n_band )];
    1565         516 :             move16(); /*DIM_TR1 == 2*/
    1566             :         }
    1567             : 
    1568         516 :         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         258 :             tmp = add( quant_tmp[n_band], quant_tmp[n_band + 1] ); /*Q8 */
    1572         258 :             tmp = shr( tmp, 1 );
    1573         258 :             quant_tmp2[n_band] = sub( env_temp12[n_band], tmp );
    1574         258 :             move16(); /*Q8 */
    1575             :         }
    1576             :         /*quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band]; */
    1577         258 :         quant_tmp2[n_band] = sub( env_temp12[n_band], quant_tmp[n_band] );
    1578         258 :         move16(); /*Q8 */
    1579         258 :         ind_tmp[0] = vqSimple_w_fx( quant_tmp2, quant_tmp2, Env_TR_Cdbk2_fx, NULL, DIM_TR2, N_CB_TR2, 0 );
    1580         258 :         move16();
    1581             : 
    1582         774 :         FOR( n_band = 0; n_band < DIM_TR1; n_band++ )
    1583             :         {
    1584         516 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1585         516 :             move16();
    1586             :         }
    1587             : 
    1588         516 :         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         258 :             tmp = add( quant_tmp[n_band], quant_tmp[n_band + 1] ); /*Q8 */
    1592         258 :             tmp = shr( tmp, 1 );
    1593         258 :             quant_select[n_band * 2 + 1] = add( tmp, quant_tmp2[n_band] );
    1594         258 :             move16();
    1595             :         }
    1596             :         /*quant_select[n_band*2+1] = quant_tmp[n_band]+quant_tmp2[n_band]; */
    1597         258 :         quant_select[n_band * 2 + 1] = add( quant_tmp[n_band], quant_tmp2[n_band] );
    1598         258 :         move16();
    1599             : 
    1600         258 :         L_dist = L_deposit_l( 0 );
    1601        1290 :         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        1032 :             tmp_q = sub( SWB_env_energy[n_band], quant_select[n_band] );
    1605             :             /*dist += tmp_q*tmp_q; */
    1606        1032 :             L_tmp = L_mult( tmp_q, tmp_q );               /*Q17 */
    1607        1032 :             L_dist = L_add( L_dist, L_shr( L_tmp, 12 ) ); /*Q5 */
    1608             :         }
    1609             : 
    1610             :         /* Check optimal candidate */
    1611         258 :         IF( LT_32( L_dist, L_minDist ) )
    1612             :         {
    1613          92 :             L_minDist = L_add( L_dist, 0 );
    1614          92 :             indice[0] = candInd[k];
    1615          92 :             move16();
    1616          92 :             indice[1] = ind_tmp[0];
    1617          92 :             move16();
    1618             :         }
    1619             :     }
    1620          86 :     return;
    1621             : }
    1622             : 
    1623             : /*-------------------------------------------------------------------*
    1624             :  * MSVQ_Interpol_fx()
    1625             :  *
    1626             :  *-------------------------------------------------------------------*/
    1627             : 
    1628       14488 : 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      115904 :     FOR( n_band = 0; n_band < DIM11; n_band++ )
    1644             :     {
    1645      101416 :         n_band2 = shl( n_band, 1 );
    1646      101416 :         n_band2p1 = add( n_band2, 1 );
    1647      101416 :         env_temp11[n_band] = SWB_env_energy[n_band2];
    1648      101416 :         move16(); /*Q8 */
    1649      101416 :         env_temp12[n_band] = SWB_env_energy[n_band2p1];
    1650      101416 :         move16(); /*Q8 */
    1651             : 
    1652      101416 :         w_env11[n_band] = w_env[n_band2];
    1653      101416 :         move16(); /*Q13 */
    1654      101416 :         w_env12[n_band] = w_env[n_band2p1];
    1655      101416 :         move16(); /*Q13 */
    1656             :     }
    1657             : 
    1658       14488 :     vqWithCand_w_fx( env_temp11, EnvCdbk11_fx, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
    1659             : 
    1660       14488 :     L_minDist = L_add( MAX_32, 0 ); /* FLT_MAX */
    1661             : 
    1662       43464 :     FOR( k = 0; k < N_CAND; k++ )
    1663             :     {
    1664      231808 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1665             :         {
    1666      202832 :             quant_tmp1[n_band] = EnvCdbk11_fx[add( i_mult2( candInd[k], DIM11 ), n_band )];
    1667      202832 :             move16(); /*Q8 */
    1668      202832 :             quant_tmp2[n_band] = sub( env_temp11[n_band], quant_tmp1[n_band] );
    1669      202832 :             move16(); /*Q8 */
    1670             :         }
    1671             : 
    1672       28976 :         ind_tmp[0] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk1st_fx, w_env11, DIM1ST, N_CB1ST, 1 );
    1673       28976 :         move16();
    1674       28976 :         ind_tmp[1] = vqSimple_w_fx( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd_fx, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
    1675       28976 :         move16();
    1676             : 
    1677             :         /* Extract vector for odd position */
    1678      231808 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1679             :         {
    1680      202832 :             quant_tmp[n_band] = add_sat( quant_tmp1[n_band], quant_tmp2[n_band] );
    1681      202832 :             move16();
    1682             :         }
    1683             : 
    1684      202832 :         FOR( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1685             :         {
    1686      173856 :             tmp = add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ); /*Q8 */
    1687      173856 :             tmp = shr( tmp, 1 );
    1688      173856 :             quant_tmp2[n_band] = sub( env_temp12[n_band], tmp );
    1689      173856 :             move16(); /*Q8 */
    1690             :         }
    1691             : 
    1692             :         /*quant_tmp2[n_band] = env_temp12[n_band]-quant_tmp[n_band]; */
    1693       28976 :         quant_tmp2[n_band] = sub( env_temp12[n_band], quant_tmp[n_band] );
    1694       28976 :         move16(); /*Q8 */
    1695             : 
    1696       28976 :         ind_tmp[2] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk3rd_fx, w_env12, DIM3RD, N_CB3RD, 1 );
    1697       28976 :         move16();
    1698       28976 :         ind_tmp[3] = vqSimple_w_fx( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk4th_fx, w_env12 + DIM3RD, DIM4TH, N_CB4TH, 1 );
    1699       28976 :         move16();
    1700             : 
    1701      231808 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1702             :         {
    1703      202832 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1704      202832 :             move16(); /*Q8 */
    1705             :         }
    1706             : 
    1707      202832 :         FOR( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1708             :         {
    1709      173856 :             tmp = add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] );
    1710      173856 :             tmp = shr( tmp, 1 );
    1711      173856 :             quant_select[( n_band << 1 ) + 1] = add( tmp, quant_tmp2[n_band] );
    1712      173856 :             move16(); /*Q8 */
    1713             :         }
    1714       28976 :         quant_select[( n_band << 1 ) + 1] = add( quant_tmp[n_band], quant_tmp2[n_band] );
    1715       28976 :         move16(); /*Q8 */
    1716             : 
    1717       28976 :         L_dist = L_deposit_l( 0 );
    1718      434640 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1719             :         {
    1720      405664 :             tmp_q = sub( SWB_env_energy[n_band], quant_select[n_band] ); /*Q8 */
    1721      405664 :             L_tmp = L_mult( tmp_q, tmp_q );                              /*Q17 */
    1722      405664 :             L_tmp = Mult_32_16( L_tmp, w_env[n_band] );                  /*Q15 */
    1723      405664 :             L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) );
    1724             :         }
    1725             : 
    1726             :         /* Check optimal candidate */
    1727       28976 :         IF( LT_32( L_dist, L_minDist ) )
    1728             :         {
    1729       17871 :             L_minDist = L_add( L_dist, 0 );
    1730             : 
    1731       17871 :             Copy( quant_select, synth_energy, SWB_FENV );
    1732             : 
    1733       17871 :             indice[0] = candInd[k];
    1734       17871 :             move16();
    1735       17871 :             indice[1] = ind_tmp[0];
    1736       17871 :             move16();
    1737       17871 :             indice[2] = ind_tmp[1];
    1738       17871 :             move16();
    1739       17871 :             indice[3] = ind_tmp[2];
    1740       17871 :             move16();
    1741       17871 :             indice[4] = ind_tmp[3];
    1742       17871 :             move16();
    1743             :         }
    1744             :     }
    1745             : 
    1746       14488 :     Copy( synth_energy, SWB_env_energy, SWB_FENV );
    1747             : 
    1748       14488 :     return;
    1749             : }
    1750             : 
    1751             : /*-------------------------------------------------------------------*
    1752             :  * msvq_interpol_2_fx()
    1753             :  *
    1754             :  *-------------------------------------------------------------------*/
    1755           0 : 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           0 :     FOR( n_band = 0; n_band < DIM11 - 1; n_band++ )
    1772             :     {
    1773           0 :         n_band2 = shl( n_band, 1 );
    1774           0 :         env_temp11[n_band] = hq_generic_fenv[n_band2];
    1775           0 :         move16(); /*Q8 */
    1776           0 :         w_env11[n_band] = w_env[n_band2];
    1777           0 :         move16(); /*Q13 */
    1778             :     }
    1779           0 :     env_temp11[DIM11 - 1] = hq_generic_fenv[2 * ( DIM11 - 2 ) + 1];
    1780           0 :     move16(); /*Q8 */
    1781           0 :     w_env11[DIM11 - 1] = w_env[2 * ( DIM11 - 2 ) + 1];
    1782           0 :     move16(); /*Q13 */
    1783             : 
    1784           0 :     env_temp12[0] = hq_generic_fenv[0];
    1785           0 :     move16(); /*Q8 */
    1786           0 :     w_env12[0] = w_env[0];
    1787           0 :     move16(); /*Q13 */
    1788           0 :     FOR( n_band = 1; n_band < DIM11 - 1; n_band++ )
    1789             :     {
    1790           0 :         n_band2 = sub( shl( n_band, 1 ), 1 );
    1791           0 :         env_temp12[n_band] = hq_generic_fenv[n_band2 /*2*n_band-1*/];
    1792           0 :         move16(); /*Q8 */
    1793           0 :         w_env12[n_band] = w_env[n_band2 /*2*n_band-1*/];
    1794           0 :         move16(); /*Q13 */
    1795             :     }
    1796             : 
    1797           0 :     vqWithCand_w_fx( env_temp11, EnvCdbk11_fx, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
    1798             : 
    1799           0 :     L_minDist = L_add( MAX_32, 0 ); /* FLT_MAX */
    1800           0 :     FOR( k = 0; k < N_CAND; k++ )
    1801             :     {
    1802           0 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1803             :         {
    1804           0 :             quant_tmp1[n_band] = EnvCdbk11_fx[add( i_mult2( candInd[k], DIM11 ), n_band )];
    1805           0 :             move16(); /*Q8 */
    1806           0 :             quant_tmp2[n_band] = sub( env_temp11[n_band], quant_tmp1[n_band] );
    1807           0 :             move16(); /*Q8 */
    1808             :         }
    1809             : 
    1810           0 :         ind_tmp[0] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk1st_fx, w_env11, DIM1ST, N_CB1ST, 1 );
    1811           0 :         move16();
    1812           0 :         ind_tmp[1] = vqSimple_w_fx( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd_fx, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
    1813           0 :         move16();
    1814             : 
    1815             :         /* Extract vector for odd position */
    1816           0 :         FOR( n_band = 0; n_band < DIM11; n_band++ )
    1817             :         {
    1818           0 :             quant_tmp[n_band] = add( quant_tmp1[n_band], quant_tmp2[n_band] );
    1819           0 :             move16();
    1820             :         }
    1821             : 
    1822           0 :         quant_tmp2[0] = sub( env_temp12[0], quant_tmp[0] );
    1823           0 :         move16();
    1824           0 :         FOR( n_band = 1; n_band < DIM12 - 1; n_band++ )
    1825             :         {
    1826           0 :             tmp_q = add_sat( quant_tmp[n_band - 1], quant_tmp[n_band] );
    1827           0 :             tmp_q = shr( tmp_q, 1 );
    1828           0 :             quant_tmp2[n_band] = sub_sat( env_temp12[n_band], tmp_q );
    1829           0 :             move16();
    1830             :         }
    1831             : 
    1832           0 :         ind_tmp[2] = vqSimple_w_fx( quant_tmp2, quant_tmp2, EnvCdbk3rd_fx, w_env12, DIM3RD, N_CB3RD, 1 );
    1833           0 :         move16();
    1834           0 :         ind_tmp[3] = vqSimple_w_fx( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk3rd_fx, w_env12 + DIM3RD, DIM3RD, N_CB3RD, 1 );
    1835           0 :         move16();
    1836             : 
    1837           0 :         FOR( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1838             :         {
    1839           0 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1840           0 :             move16(); /*Q8 */
    1841             :         }
    1842           0 :         quant_select[11] = quant_tmp[DIM12 - 1];
    1843           0 :         move16(); /*Q8 */
    1844             : 
    1845           0 :         quant_select[0] = add( quant_select[0], quant_tmp2[0] );
    1846           0 :         move16(); /*Q8 */
    1847           0 :         FOR( n_band = 1; n_band < DIM12 - 1; n_band++ )
    1848             :         {
    1849           0 :             tmp_q = add_sat( quant_tmp[n_band - 1], quant_tmp[n_band] );
    1850           0 :             tmp_q = shr( tmp_q, 1 );
    1851           0 :             quant_select[( n_band << 1 ) - 1] = add_sat( quant_tmp2[n_band], tmp_q );
    1852             :         }
    1853             : 
    1854           0 :         L_dist = L_deposit_l( 0 );
    1855           0 :         FOR( n_band = 0; n_band < SWB_FENV - 2; n_band++ )
    1856             :         {
    1857           0 :             tmp_q = sub( hq_generic_fenv[n_band], quant_select[n_band] ); /*Q8 */
    1858           0 :             L_tmp = L_mult( tmp_q, tmp_q );                               /*Q17 */
    1859           0 :             L_tmp = Mult_32_16( L_tmp, w_env[n_band] );                   /*Q15 */
    1860           0 :             L_dist = L_add( L_dist, L_shr( L_tmp, 10 ) );
    1861             :         }
    1862             : 
    1863             :         /* Check optimal candidate */
    1864           0 :         IF( LT_32( L_dist, L_minDist ) )
    1865             :         {
    1866           0 :             L_minDist = L_add( L_dist, 0 );
    1867           0 :             Copy( quant_select, synth_energy, SWB_FENV - 2 );
    1868           0 :             synth_energy[SWB_FENV - 2] = 0;
    1869           0 :             move16();
    1870           0 :             synth_energy[SWB_FENV - 1] = 0;
    1871           0 :             move16();
    1872             : 
    1873           0 :             indice[0] = candInd[k];
    1874           0 :             move16();
    1875           0 :             indice[1] = ind_tmp[0];
    1876           0 :             move16();
    1877           0 :             indice[2] = ind_tmp[1];
    1878           0 :             move16();
    1879           0 :             indice[3] = ind_tmp[2];
    1880           0 :             move16();
    1881           0 :             indice[4] = ind_tmp[3];
    1882           0 :             move16();
    1883             :         }
    1884             :     }
    1885             : 
    1886           0 :     Copy( synth_energy, hq_generic_fenv, nenv );
    1887             : 
    1888           0 :     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      170982 : 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      170982 :     max = 0;
    2070      170982 :     move16();
    2071     4221702 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2072             :     {
    2073     4050720 :         org_spec[n_coeff] = abs_s( org[n_coeff] );
    2074     4050720 :         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     4050720 :         max = s_max( max, org_spec[n_coeff] );
    2081             :     }
    2082      170982 :     l_shift = norm_s( max );
    2083     4221702 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2084             :     {
    2085     4050720 :         org_spec[n_coeff] = shl( org_spec[n_coeff], l_shift );
    2086     4050720 :         move16();
    2087     4050720 :         IF( org_spec[n_coeff] == 0 )
    2088             :         {
    2089      135283 :             org_spec[n_coeff] = shl( 1, l_shift );
    2090      135283 :             move16();
    2091             :         }
    2092             :     }
    2093             : 
    2094      170982 :     max = 0;
    2095      170982 :     move16();
    2096     4221702 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2097             :     {
    2098     4050720 :         gen_spec[n_coeff] = abs_s( gen[n_coeff] );
    2099     4050720 :         move16(); /*Q15 */
    2100             :         /*test();
    2101             :         if( sub(max,gen_spec[n_coeff]) < 0)
    2102             :         {
    2103             :             max = gen_spec[n_coeff];move16();
    2104             :         }*/
    2105     4050720 :         max = s_max( max, gen_spec[n_coeff] );
    2106             :     }
    2107      170982 :     l_shift = norm_s( max );
    2108     4221702 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2109             :     {
    2110     4050720 :         gen_spec[n_coeff] = shl_sat( gen_spec[n_coeff], l_shift );
    2111     4050720 :         move16();
    2112     4050720 :         IF( gen_spec[n_coeff] == 0 )
    2113             :         {
    2114       18031 :             gen_spec[n_coeff] = shl( 1, l_shift );
    2115       18031 :             move16();
    2116             :         }
    2117             :     }
    2118             : 
    2119      170982 :     exp = norm_s( length );
    2120      170982 :     inv_len = div_s( shl( 1, exp ), shl( length, exp ) ); /*Q15 */
    2121             : 
    2122      170982 :     L_am_org = L_deposit_l( 0 );
    2123      170982 :     L_am_gen = L_deposit_l( 0 );
    2124      170982 :     L_log_gm_org = 0;
    2125      170982 :     move32();
    2126      170982 :     L_log_gm_gen = 0;
    2127      170982 :     move32();
    2128             : 
    2129     4221702 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    2130             :     {
    2131     4050720 :         L_am_org = L_add( L_am_org, L_deposit_l( org_spec[n_coeff] ) ); /*Q10 */
    2132     4050720 :         L_am_gen = L_add( L_am_gen, L_deposit_l( gen_spec[n_coeff] ) ); /*Q10 */
    2133             : 
    2134     4050720 :         IF( org_spec[n_coeff] != 0 )
    2135             :         {
    2136     4050720 :             L_tmp = L_deposit_h( org_spec[n_coeff] ); /*Q26 */
    2137     4050720 :             e_tmp = norm_l( L_tmp );
    2138     4050720 :             f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2139     4050720 :             e_tmp = sub( sub( 30, e_tmp ), 26 );
    2140     4050720 :             L_tmp = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2141     4050720 :             L_log_gm_org = L_add( L_log_gm_org, L_tmp );        /*Q14 */
    2142             :         }
    2143             : 
    2144     4050720 :         IF( gen_spec[n_coeff] != 0 )
    2145             :         {
    2146     4050720 :             L_tmp = L_deposit_h( gen_spec[n_coeff] ); /*Q26 */
    2147     4050720 :             e_tmp = norm_l( L_tmp );
    2148     4050720 :             f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2149     4050720 :             e_tmp = sub( sub( 30, e_tmp ), 26 );
    2150     4050720 :             L_tmp = Mpy_32_16( e_tmp, f_tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    2151     4050720 :             L_log_gm_gen = L_add( L_log_gm_gen, L_tmp );        /*Q14 */
    2152             :         }
    2153             :     }
    2154             : 
    2155      170982 :     IF( L_am_org != 0 )
    2156             :     {
    2157      170982 :         L_tmp = Mpy_32_16_1( L_am_org, inv_len ); /*Q10 */
    2158      170982 :         e_tmp = norm_l( L_tmp );
    2159      170982 :         f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2160      170982 :         e_tmp = sub( sub( 30, e_tmp ), 10 );
    2161      170982 :         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      170982 :     L_tmp2 = Mpy_32_16_1( L_log_gm_org, inv_len ); /* Q14 */
    2169             : 
    2170      170982 :     L_tmp = L_sub( L_tmp1, L_tmp2 );
    2171      170982 :     *SFM_org = round_fx_sat( L_shl_sat( L_tmp, 14 ) ); /*Q12 */
    2172      170982 :     move16();
    2173      170982 :     *SFM_org = s_max( 0, s_min( *SFM_org, 24547 ) );
    2174      170982 :     move16(); /*0.0001 and 5.993 in Q12 */
    2175             : 
    2176      170982 :     IF( L_am_gen != 0 )
    2177             :     {
    2178      170982 :         L_tmp = Mpy_32_16_1( L_am_gen, inv_len ); /*Q10 */
    2179      170982 :         e_tmp = norm_l( L_tmp );
    2180      170982 :         f_tmp = Log2_norm_lc( L_shl( L_tmp, e_tmp ) );
    2181      170982 :         e_tmp = sub( sub( 30, e_tmp ), 10 );
    2182      170982 :         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      170982 :     L_tmp2 = Mpy_32_16_1( L_log_gm_gen, inv_len ); /* Q14 */
    2190             : 
    2191      170982 :     L_tmp = L_sub( L_tmp1, L_tmp2 );
    2192      170982 :     *SFM_gen = round_fx_sat( L_shl_sat( L_tmp, 14 ) ); /*Q12 */
    2193      170982 :     move16();
    2194      170982 :     *SFM_gen = s_max( 0, s_min( *SFM_gen, 24547 ) );
    2195      170982 :     move16(); /*0.0001 and 5.993 in Q12 */
    2196             : 
    2197      170982 :     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       15777 : 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       15777 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    2311             : 
    2312       15777 :     IF( EQ_16( core, ACELP_CORE ) )
    2313             :     {
    2314       15777 :         gamma_fx = 11469;
    2315       15777 :         move16(); /*.35 in Q15 */
    2316       15777 :         test();
    2317       15777 :         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       15760 :             core_type = 1;
    2324             :         }
    2325       15777 :         move16();
    2326       15777 :         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       15777 :         IF( EQ_16( st_fx->extl, WB_BWE ) )
    2329             :         {
    2330        4158 :             max_band = 4;
    2331        4158 :             band_step = 2;
    2332             :         }
    2333             :         ELSE
    2334             :         {
    2335       11619 :             max_band = SWB_FENV;
    2336       11619 :             band_step = 1;
    2337             :         }
    2338       15777 :         move16();
    2339       15777 :         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      186759 :     FOR( n_band = 0; n_band < max_band; )
    2361             :     {
    2362      170982 :         calculate_Tonality_ivas_fx( org_fx + swb_bwe_subband[n_band] + offset, SWB_signal_fx + swb_bwe_subband[n_band] + offset,
    2363      170982 :                                     &SFM_org_fx[n_band], &SFM_gen_fx[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] );
    2364             : 
    2365      170982 :         IF( LT_16( SFM_gen_fx[n_band], mult_r( 24576, SFM_org_fx[n_band] ) ) )
    2366             :         {
    2367       49563 :             energy_factor_fx[n_band] = div_s( SFM_gen_fx[n_band], SFM_org_fx[n_band] ); /*Q15 */
    2368       49563 :             move16();
    2369       49563 :             if ( LT_16( energy_factor_fx[n_band], gamma_fx ) )
    2370             :             {
    2371        6149 :                 energy_factor_fx[n_band] = gamma_fx;
    2372        6149 :                 move16();
    2373             :             }
    2374             :         }
    2375             :         ELSE
    2376             :         {
    2377      121419 :             energy_factor_fx[n_band] = 32767;
    2378      121419 :             move16(); /* Q15 */
    2379             :         }
    2380      170982 :         n_band = add( n_band, band_step );
    2381             :     }
    2382       15777 :     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        4158 : 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        4158 :     n_band = 0;
    2483        4158 :     move16();
    2484       12474 :     FOR( i = 0; i < 2; i++ )
    2485             :     {
    2486        8316 :         energy_fx_64 = 0;
    2487        8316 :         move64();
    2488      340956 :         FOR( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ )
    2489             :         {
    2490      332640 :             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        8316 :         q_shift = W_norm( energy_fx_64 );
    2493        8316 :         energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/
    2494        8316 :         q_shift = sub( q_shift, 32 );
    2495             : 
    2496        8316 :         L_WB_fenv_fx[i] = energy_fx;
    2497        8316 :         move32();
    2498        8316 :         q_WB_fenv[i] = add( shl( Q_synth, 1 ), q_shift );
    2499        8316 :         move16();
    2500        8316 :         n_band = add( n_band, 2 );
    2501             :     }
    2502             : 
    2503        4158 :     scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ );
    2504        4158 :     Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale );
    2505             : 
    2506        4158 :     mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx );
    2507             : 
    2508        4158 :     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       12474 :     FOR( i = 0; i < 2; i++ )
    2511             :     {
    2512        8316 :         ener_40 = mult_r( shr( energy_factor_fx[shl( i, 1 )], 1 ), 26214 ); /*Q19 */
    2513        8316 :         L_tmp = Mpy_32_16_1( L_WB_fenv_fx[i], ener_40 );                    /*q_WB_fenv[i]+4 */
    2514        8316 :         IF( L_tmp )
    2515             :         {
    2516        8316 :             exp = norm_l( L_tmp );
    2517        8316 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    2518             :             /*exp = 30-exp-(q_WB_fenv[i]+4); */
    2519        8316 :             exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) );
    2520        8316 :             L_tmp = Mpy_32_16( exp, tmp, 32767 );           /* Q16 */
    2521        8316 :             WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /*Q10 */
    2522        8316 :             move16();
    2523             :         }
    2524             :         ELSE
    2525             :         {
    2526           0 :             WB_fenv_fx[i] = 0;
    2527           0 :             move16();
    2528             :         }
    2529             :     }
    2530             : 
    2531        4158 :     index = WB_BWE_fenv_q_fx( WB_fenv_fx, F_2_5_fx, 32, 2 );
    2532             : 
    2533        4158 :     push_indice( st_fx->hBstr, IND_WB_FENV, index, 5 );
    2534             : 
    2535        4158 :     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       11705 : 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       11705 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    3026       11705 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    3027             : 
    3028       11705 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    3029             :     {
    3030        7160 :         L = L_SUBFR;
    3031        7160 :         move16();
    3032             :     }
    3033             :     ELSE
    3034             :     {
    3035        4545 :         L = L_SUBFR16k;
    3036        4545 :         move16();
    3037             :     }
    3038             : 
    3039       11705 :     IF( EQ_16( st_fx->extl, FB_BWE ) )
    3040             :     {
    3041        4789 :         inner_frame = L_FRAME48k;
    3042        4789 :         move16();
    3043             :     }
    3044             :     ELSE
    3045             :     {
    3046        6916 :         inner_frame = L_FRAME32k;
    3047        6916 :         move16();
    3048             :     }
    3049             : 
    3050             :     /* HF transient detect */
    3051       11705 :     IsTransient = detect_transient_fx( insig_hp_fx, L_FRAME16k, Q_shb, st_fx );
    3052       11705 :     st_fx->EnergyLT_fx_exp = shl( Q_shb, 1 );
    3053       11705 :     move16();
    3054             : 
    3055             :     /* LF transient detect */
    3056       11705 :     IsTransient_LF = 0;
    3057       11705 :     move16();
    3058       58525 :     FOR( n_band = 0; n_band < 4; n_band++ )
    3059             :     {
    3060       46820 :         tmp = i_mult2( n_band, L );
    3061       46820 :         energy_fx = L_deposit_l( 0 );
    3062     3334180 :         FOR( i = 0; i < L; i++ )
    3063             :         {
    3064     3287360 :             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       46820 :         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        2458 :             IsTransient_LF = 1;
    3070        2458 :             move16();
    3071             :         }
    3072             : 
    3073       46820 :         hBWE_FD->EnergyLF_fx = energy_fx;
    3074       46820 :         hBWE_FD->EnergyLF_exp = sub( 31 + 7, shl( Q_insig_lp, 1 ) );
    3075       46820 :         move32();
    3076       46820 :         move16();
    3077             :     }
    3078             : 
    3079             :     /* tilt returned in Q24 go to Q11 */
    3080       11705 :     tilt_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( insig_fx, 0, L_FRAME32k ), 3 ) );
    3081       11705 :     test();
    3082       11705 :     test();
    3083       11705 :     IF( EQ_16( IsTransient, 1 ) && ( GT_16( tilt_fx, 16384 ) || GT_16( st_fx->clas, 1 ) ) )
    3084             :     {
    3085          71 :         IsTransient = 0;
    3086          71 :         move16();
    3087          71 :         st_fx->TransientHangOver = 0;
    3088          71 :         move16();
    3089             :     }
    3090             : 
    3091       11705 :     IF( EQ_16( IsTransient, 1 ) )
    3092             :     {
    3093          86 :         mode = IsTransient;
    3094          86 :         move16();
    3095          86 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    3096             : 
    3097             :         /* Energy for the different bands and global energies */
    3098          86 :         global_gain_fx = L_deposit_l( 0 );
    3099         430 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    3100             :         {
    3101         344 :             energy_fx_64 = W_deposit32_l( 0 );
    3102       27864 :             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       27520 :                 W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */
    3105       27520 :                 energy_fx_64 = W_add( W_tmp, energy_fx_64 );               /*2*Q_synth */
    3106             :             }
    3107         344 :             q_shift = W_norm( energy_fx_64 );
    3108         344 :             energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/
    3109         344 :             q_shift = sub( q_shift, 32 );
    3110             : 
    3111         344 :             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         344 :             L_SWB_fenv_fx[n_band] = energy_fx;
    3113         344 :             move32();
    3114         344 :             IF( L_SWB_fenv_fx[n_band] == 0 )
    3115             :             {
    3116          16 :                 q_SWB_fenv[n_band] = Q31;
    3117          16 :                 move16();
    3118             :             }
    3119             :             ELSE
    3120             :             {
    3121         328 :                 q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift );
    3122         328 :                 move16();
    3123             :             }
    3124             :         }
    3125          86 :         global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb  */
    3126             : 
    3127         430 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    3128             :         {
    3129         344 :             expd = norm_s( swb_bwe_trans_subband_width[n_band] );
    3130         344 :             tmp = div_s( shl( 1, sub( 14, expd ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-expd) */
    3131         344 :             L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], tmp );                              /*q_SWB_fenv[n_band]+29-expd - 15                */
    3132         344 :             exp = norm_l( L_tmp );
    3133         344 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    3134         344 :             exp = sub( sub( 30, exp ), sub( add( q_SWB_fenv[n_band], Q14 ), expd ) );
    3135         344 :             L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */
    3136         344 :             tmp = round_fx( L_shl( L_tmp, 10 ) );           /* Q8 */
    3137             : 
    3138         344 :             SWB_fenv_fx[n_band] = sub( tmp, Mean_env_tr_fx[n_band] );
    3139         344 :             move16(); /*Q8 */
    3140             :         }
    3141             : 
    3142          86 :         WB_tenv_orig_fx = L_deposit_l( 0 );
    3143          86 :         WB_tenv_syn_fx = L_deposit_l( 1 );
    3144         430 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3145             :         {
    3146         344 :             tmp = i_mult2( n_band, L_SUBFR16k );
    3147         344 :             L_SWB_tenv = L_deposit_l( 0 );
    3148       27864 :             FOR( i = 0; i < L_SUBFR16k; i++ )
    3149             :             {
    3150       27520 :                 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         344 :             tmp = i_mult2( n_band, L );
    3154       22744 :             FOR( i = 0; i < L; i++ )
    3155             :             {
    3156       22400 :                 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       22400 :                 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         344 :             L_tmp = Mult_32_16( L_SWB_tenv, INV_L_SUBFR16k_FX ); /*2*Q_shb */
    3161         344 :             SWB_tenv_fx[n_band] = 0;
    3162         344 :             move16();
    3163         344 :             IF( L_tmp != 0 )
    3164             :             {
    3165         342 :                 exp = norm_l( L_tmp );
    3166         342 :                 tmp = extract_h( L_shl( L_tmp, exp ) );
    3167         342 :                 exp = sub( exp, sub( 30, shl( Q_shb, 1 ) ) );
    3168             : 
    3169         342 :                 tmp = div_s( 16384, tmp );
    3170         342 :                 L_tmp = L_deposit_h( tmp );
    3171         342 :                 L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    3172             : 
    3173         342 :                 SWB_tenv_fx[n_band] = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /*Q3           */
    3174         342 :                 move16();
    3175             :             }
    3176             :         }
    3177             : 
    3178          86 :         IF( WB_tenv_orig_fx != 0 )
    3179             :         {
    3180          82 :             expn = norm_l( WB_tenv_orig_fx );
    3181          82 :             num = extract_h( L_shl( WB_tenv_orig_fx, expn ) );
    3182          82 :             expn = sub( sub( 30, expn ), sub( shl( Q_insig_lp, 1 ), 7 ) );
    3183             : 
    3184          82 :             expd = norm_l( WB_tenv_syn_fx );
    3185          82 :             den = round_fx_sat( L_shl( WB_tenv_syn_fx, expd ) );
    3186          82 :             expd = sub( sub( 30, expd ), sub( shl( Q_insig_lp, 1 ), 7 ) );
    3187             : 
    3188          82 :             scale = shr( sub( den, num ), 15 );
    3189          82 :             num = shl( num, scale );
    3190          82 :             expn = sub( expn, scale );
    3191             : 
    3192          82 :             tmp = div_s( num, den );
    3193          82 :             expn = sub( expn, expd );
    3194             : 
    3195          82 :             L_tmp = L_deposit_h( tmp );
    3196          82 :             L_tmp = Isqrt_lc( L_tmp, &expn ); /*31-expn */
    3197             : 
    3198          82 :             Rat_tenv_fx = round_fx_sat( L_shl_sat( L_tmp, sub( expn, 1 ) ) ); /*Q14 */
    3199             :         }
    3200             :         ELSE
    3201             :         {
    3202           4 :             Rat_tenv_fx = 16384;
    3203           4 :             move16();
    3204             :         }
    3205             : 
    3206          86 :         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          77 :         ELSE IF( GT_16( Rat_tenv_fx, 16384 ) )
    3212             :         {
    3213          52 :             Rat_tenv_fx = 32767;
    3214          52 :             move16();
    3215             :         }
    3216             : 
    3217         430 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3218             :         {
    3219         344 :             SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], Rat_tenv_fx );
    3220         344 :             move16(); /*Q3 */
    3221             :         }
    3222             : 
    3223          86 :         max_fx = SWB_tenv_fx[0];
    3224          86 :         move16();
    3225          86 :         pos = 0;
    3226          86 :         move16();
    3227         344 :         FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    3228             :         {
    3229         258 :             IF( GT_16( SWB_tenv_fx[n_band], max_fx ) )
    3230             :             {
    3231          85 :                 max_fx = SWB_tenv_fx[n_band];
    3232          85 :                 move16();
    3233          85 :                 pos = n_band;
    3234          85 :                 move16();
    3235             :             }
    3236             :         }
    3237             : 
    3238          86 :         max_fx = SWB_tenv_fx[0];
    3239          86 :         move16();
    3240         313 :         FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    3241             :         {
    3242         244 :             tmp = sub( mult_r( SWB_tenv_fx[n_band], 6554 ), SWB_tenv_fx[n_band - 1] );
    3243         244 :             IF( tmp > 0 )
    3244             :             {
    3245          17 :                 BREAK;
    3246             :             }
    3247             :         }
    3248             : 
    3249          86 :         IF( LT_16( n_band, SWB_TENV ) )
    3250             :         {
    3251          17 :             energy_fx = L_deposit_l( 0 );
    3252          27 :             FOR( n_band = ( pos + 1 ); n_band < SWB_TENV; n_band++ )
    3253             :             {
    3254          10 :                 energy_fx = L_add( energy_fx, SWB_tenv_fx[n_band] ); /*Q3 */
    3255             :             }
    3256             : 
    3257          17 :             IF( EQ_16( pos, sub( SWB_TENV, 1 ) ) )
    3258             :             {
    3259           9 :                 energy_fx = L_deposit_l( 0 );
    3260             :             }
    3261             :             ELSE
    3262             :             {
    3263           8 :                 tmp = sub( SWB_TENV, add( pos, 1 ) );
    3264           8 :                 tmp = div_s( 1, tmp );                    /*Q15 */
    3265           8 :                 energy_fx = Mult_32_16( energy_fx, tmp ); /*Q3 */
    3266             :             }
    3267             : 
    3268          58 :             FOR( n_band = 0; n_band < pos; n_band++ )
    3269             :             {
    3270          41 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 16384 );
    3271          41 :                 move16();
    3272             :             }
    3273             : 
    3274             :             /*SWB_tenv_fx[pos] = add(SWB_tenv_fx[pos], mult_r(SWB_tenv_fx[pos], 164));    move16();//Q3 */
    3275          17 :             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          17 :             move16();
    3277             : 
    3278          17 :             IF( LT_32( energy_fx, SWB_tenv_fx[pos] ) )
    3279             :             {
    3280          27 :                 FOR( n_band = pos + 1; n_band < SWB_TENV; n_band++ )
    3281             :                 {
    3282          10 :                     SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 29491 );
    3283          10 :                     move16(); /*Q3 */
    3284             :                 }
    3285             :             }
    3286             :         }
    3287             :         ELSE
    3288             :         {
    3289         276 :             FOR( n_band = 1; n_band < SWB_TENV; n_band++ )
    3290             :             {
    3291         207 :                 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          93 :                     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          93 :                     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         114 :                     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         114 :                     move16();
    3302             :                 }
    3303             :             }
    3304             : 
    3305         345 :             FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3306             :             {
    3307         276 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 29491 );
    3308         276 :                 move16(); /*Q3 */
    3309             :             }
    3310             :         }
    3311             : 
    3312          86 :         test();
    3313          86 :         test();
    3314          86 :         IF( IsTransient_LF == 0 && st_fx->coder_type == INACTIVE && EQ_16( st_fx->TransientHangOver, 1 ) )
    3315             :         {
    3316          25 :             FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3317             :             {
    3318          20 :                 SWB_tenv_fx[n_band] = mult_r( SWB_tenv_fx[n_band], 16384 );
    3319          20 :                 move16();
    3320             :             }
    3321          25 :             FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    3322             :             {
    3323          20 :                 SWB_fenv_fx[n_band] = mult_r( SWB_fenv_fx[n_band], 1638 );
    3324          20 :                 move16();
    3325             :             }
    3326             :         }
    3327             :         ELSE
    3328             :         {
    3329          81 :             SWB_fenv_fx[2] = mult_r( SWB_fenv_fx[2], 3277 );
    3330          81 :             move16();
    3331          81 :             SWB_fenv_fx[3] = mult_r( SWB_fenv_fx[3], 1638 );
    3332          81 :             move16();
    3333             :         }
    3334             : 
    3335         430 :         FOR( n_band = 0; n_band < SWB_TENV; n_band++ )
    3336             :         {
    3337         344 :             IF( SWB_tenv_fx[n_band] == 0 )
    3338             :             {
    3339          16 :                 SWB_tenv_tmp_fx[n_band] = -32768;
    3340          16 :                 move16(); /*-16 in Q11 */
    3341             :             }
    3342             :             ELSE
    3343             :             {
    3344         328 :                 L_tmp = L_deposit_h( SWB_tenv_fx[n_band] ); /*Q19 */
    3345         328 :                 expn = norm_l( L_tmp );
    3346         328 :                 tmp = Log2_norm_lc( L_shl( L_tmp, expn ) );
    3347         328 :                 expn = sub( sub( 30, expn ), 19 );
    3348         328 :                 L_tmp = Mpy_32_16( expn, tmp, 32767 ); /* Q16 */          /*1 in Q15 */
    3349         328 :                 SWB_tenv_tmp_fx[n_band] = round_fx( L_shl( L_tmp, 11 ) ); /* Q11 */
    3350         328 :                 move16();
    3351             :             }
    3352             : 
    3353         344 :             IF( GT_16( SWB_tenv_tmp_fx[n_band], 30720 ) )
    3354             :             {
    3355           0 :                 index = 15;
    3356           0 :                 move16();
    3357             :             }
    3358         344 :             ELSE IF( SWB_tenv_tmp_fx[n_band] < 0 )
    3359             :             {
    3360          19 :                 index = 0;
    3361          19 :                 move16();
    3362             :             }
    3363             :             ELSE
    3364             :             {
    3365         325 :                 index = shr( add( SWB_tenv_tmp_fx[n_band], 1024 ), 11 );
    3366             :             }
    3367             : 
    3368         344 :             push_indice( hBstr, IND_SWB_TENV, index, 4 );
    3369             :         }
    3370             : 
    3371          86 :         MSVQ_Interpol_Tran_fx( SWB_fenv_fx, indice );
    3372             : 
    3373          86 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 7 );
    3374          86 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 6 );
    3375             :     }
    3376             :     ELSE
    3377             :     {
    3378             :         /* Energy for the different bands and global energies */
    3379       11619 :         global_gain_fx = L_deposit_l( 0 );
    3380      174285 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    3381             :         {
    3382      162666 :             energy_fx_64 = W_deposit32_l( 0 );
    3383     3880746 :             FOR( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ )
    3384             :             {
    3385     3718080 :                 W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */
    3386     3718080 :                 energy_fx_64 = W_add( W_tmp, energy_fx_64 );               /*2*Q_synth */
    3387             :             }
    3388      162666 :             q_shift = W_norm( energy_fx_64 );
    3389      162666 :             energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/
    3390      162666 :             q_shift = sub( q_shift, 32 );
    3391             : 
    3392      162666 :             IF( LT_16( n_band, SWB_FENV - 2 ) )
    3393             :             {
    3394      139428 :                 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      162666 :             L_SWB_fenv_fx[n_band] = energy_fx;
    3397      162666 :             move32();
    3398      162666 :             q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift );
    3399      162666 :             move16();
    3400             :         }
    3401             : 
    3402       11619 :         global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */
    3403             : 
    3404       11619 :         scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ );
    3405       11619 :         Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale );
    3406       11619 :         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       11619 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    3408             : 
    3409       11619 :         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      174285 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    3412             :         {
    3413      162666 :             L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], energy_factor_fx[n_band] ); /*q_SWB_fenv[n_band] */
    3414      162666 :             L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] );     /*q_SWB_fenv[n_band] */
    3415             : 
    3416      162666 :             IF( L_tmp != 0 )
    3417             :             {
    3418      158774 :                 expn = norm_l( L_tmp );
    3419      158774 :                 tmp = Log2_norm_lc( L_shl( L_tmp, expn ) );
    3420      158774 :                 expn = sub( 30, add( expn, q_SWB_fenv[n_band] ) );
    3421      158774 :                 L_tmp = Mpy_32_16( expn, tmp, 24660 ); /* Q14 */      /*10log10(2) in Q13 */
    3422      158774 :                 SWB_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */
    3423      158774 :                 move16();
    3424             :             }
    3425             :             ELSE
    3426             :             {
    3427        3892 :                 SWB_fenv_fx[n_band] = -24576;
    3428        3892 :                 move16();
    3429             :             }
    3430             :         }
    3431       11619 :         freq_weights_fx( SWB_fenv_fx, w_NOR_fx, w_env_fx, SWB_FENV );
    3432             : 
    3433      174285 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    3434             :         {
    3435      162666 :             SWB_fenv_fx[n_band] = sub( SWB_fenv_fx[n_band], Mean_env_fx[n_band] );
    3436      162666 :             move16();
    3437             :         }
    3438             : 
    3439             :         /* Energy VQ */
    3440       11619 :         msvq_interpol_fx( SWB_fenv_fx, w_env_fx, indice );
    3441             : 
    3442       11619 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 5 );
    3443       11619 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 7 );
    3444       11619 :         push_indice( hBstr, IND_SWB_FENV, indice[2], 6 );
    3445       11619 :         push_indice( hBstr, IND_SWB_FENV, indice[3], 5 );
    3446       11619 :         push_indice( hBstr, IND_SWB_FENV, indice[4], 6 );
    3447             :     }
    3448       11705 :     hBWE_FD->prev_mode = mode;
    3449       11705 :     move16();
    3450       11705 :     hBWE_FD->prev_global_gain_fx = global_gain_fx;
    3451       11705 :     move32();
    3452       11705 :     st_fx->prev_Q_shb = Q_shb;
    3453       11705 :     move16();
    3454             : 
    3455       11705 :     return mode;
    3456             : }
    3457             : /*-------------------------------------------------------------------*
    3458             :  * get_normalize_spec_fx_32()
    3459             :  *
    3460             :  *-------------------------------------------------------------------*/
    3461             : 
    3462        2861 : 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        2861 :     set32_fx( SWB_signal_fx, 0, add( HQ_GENERIC_HIGH0, offset ) );
    3480        2861 :     calc_normal_length_fx_32( core, org_fx, mode, extl, &L_swb_norm, prev_L_swb_norm );
    3481        2861 :     test();
    3482        2861 :     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        2861 :     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        2861 :         Copy32( org_fx + HQ_GENERIC_OFFSET, SWB_signal_fx + HQ_GENERIC_HIGH0 + offset, HQ_GENERIC_LEN0 );
    3514        2861 :         Copy32( org_fx + HQ_GENERIC_OFFSET, SWB_signal_fx + HQ_GENERIC_HIGH1 + offset, HQ_GENERIC_LEN0 );
    3515        2861 :         IF( EQ_16( offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3516             :         {
    3517        2861 :             Copy32( org_fx + HQ_GENERIC_LOW0, SWB_signal_fx + HQ_GENERIC_HIGH2 + offset, HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
    3518             :         }
    3519        2861 :         frq_end = L_FRAME32k;
    3520        2861 :         move16();
    3521             :     }
    3522             : 
    3523             :     /* calculate envelope */
    3524        2861 :     calc_norm_envelop_fx_32( SWB_signal_fx, envelope_fx, L_swb_norm, frq_end - offset, offset );
    3525             : 
    3526             :     /* Normalize with envelope */
    3527      918381 :     FOR( n_freq = add( swb_bwe_subband[0], offset ); n_freq < frq_end; n_freq++ )
    3528             :     {
    3529      915520 :         IF( envelope_fx[n_freq] != 0 )
    3530             :         {
    3531      915520 :             exp1 = norm_l( envelope_fx[n_freq] );
    3532      915520 :             exp2 = norm_l( SWB_signal_fx[n_freq] );
    3533      915520 :             tmp = extract_h( L_shl( envelope_fx[n_freq], exp1 ) );                /*12 + exp1 - 16 */
    3534      915520 :             tmp = div_s( 16384, tmp );                                            /*15 + 14 - (12 + exp1 - 16) */
    3535      915520 :             L_tmp = Mult_32_16( L_shl( SWB_signal_fx[n_freq], exp2 ), tmp );      /*exp2 + 12 + (15 + 14 - (12 + exp1 - 16)) - 15 */
    3536      915520 :             SWB_signal_fx[n_freq] = L_shr( L_tmp, sub( 10, sub( exp1, exp2 ) ) ); /*20 */
    3537      915520 :             move32();
    3538             :         }
    3539             :         ELSE
    3540             :         {
    3541           0 :             SWB_signal_fx[n_freq] = 0;
    3542           0 :             move32(); /*20 */
    3543             :         }
    3544             :     }
    3545             : 
    3546        2861 :     return;
    3547             : }
    3548             : 
    3549             : /*-------------------------------------------------------------------*
    3550             :  * calculate_tonality_fx_32()
    3551             :  *
    3552             :  *-------------------------------------------------------------------*/
    3553       40054 : 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       40054 :     max_fx = 0;
    3571       40054 :     move16();
    3572      955574 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    3573             :     {
    3574      915520 :         org_spec_fx[n_coeff] = L_abs( org_fx[n_coeff] );
    3575      915520 :         move32();
    3576             : 
    3577      915520 :         if ( LT_32( max_fx, org_spec_fx[n_coeff] ) )
    3578             :         {
    3579      145271 :             max_fx = org_spec_fx[n_coeff];
    3580      145271 :             move16();
    3581             :         }
    3582             :     }
    3583       40054 :     max_fx = 0;
    3584       40054 :     move16();
    3585      955574 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    3586             :     {
    3587      915520 :         gen_spec_fx[n_coeff] = L_abs( gen_fx[n_coeff] );
    3588      915520 :         move32();
    3589      915520 :         if ( LT_32( max_fx, gen_spec_fx[n_coeff] ) )
    3590             :         {
    3591      146196 :             max_fx = gen_spec_fx[n_coeff];
    3592      146196 :             move16();
    3593             :         }
    3594             :     }
    3595             : 
    3596       40054 :     exp = norm_s( length );
    3597       40054 :     inv_len_fx = div_s( shl( 1, exp ), shl( length, exp ) ); /*15 */
    3598             : 
    3599       40054 :     am_org_fx = 0;
    3600       40054 :     move16();
    3601       40054 :     am_gen_fx = 0;
    3602       40054 :     move16();
    3603       40054 :     log_gm_org_sum_fx = 0;
    3604       40054 :     move16();
    3605       40054 :     log_gm_gen_sum_fx = 0;
    3606       40054 :     move16();
    3607             : 
    3608      955574 :     FOR( n_coeff = 0; n_coeff < length; n_coeff++ )
    3609             :     {
    3610      915520 :         am_org_fx = L_add_sat( am_org_fx, org_spec_fx[n_coeff] );
    3611      915520 :         am_gen_fx = L_add_sat( am_gen_fx, gen_spec_fx[n_coeff] );
    3612      915520 :         IF( org_spec_fx[n_coeff] != 0 )
    3613             :         {
    3614      912661 :             exp = norm_l( org_spec_fx[n_coeff] );
    3615      912661 :             tmp = Log2_norm_lc( L_shl( org_spec_fx[n_coeff], exp ) ); /*15 */
    3616      912661 :             exp = sub( 30, add( exp, 12 ) );
    3617      912661 :             L_tmp = L_add( L_shl( exp, 16 ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3618      912661 :             log_gm_org_sum_fx = L_add( log_gm_org_sum_fx, L_tmp );              /*Q16 */
    3619             :         }
    3620      915520 :         IF( gen_spec_fx[n_coeff] != 0 )
    3621             :         {
    3622      914958 :             exp = norm_l( gen_spec_fx[n_coeff] );
    3623      914958 :             tmp = Log2_norm_lc( L_shl( gen_spec_fx[n_coeff], exp ) );
    3624      914958 :             exp = sub( 30, add( exp, 12 ) );
    3625      914958 :             L_tmp = L_add( L_shl( exp, 16 ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3626      914958 :             log_gm_gen_sum_fx = L_add( log_gm_gen_sum_fx, L_tmp );              /*16 */
    3627             :         }
    3628             :     }
    3629             : 
    3630       40054 :     IF( am_org_fx != 0 )
    3631             :     {
    3632       40054 :         exp1 = norm_l( am_org_fx );
    3633       40054 :         L_tmp1 = Mult_32_16( L_shl( am_org_fx, exp1 ), inv_len_fx ); /*12 + exp1 + 15 - 15 */
    3634       40054 :         exp2 = norm_l( L_tmp1 );                                     /*12 + exp1 + exp2 */
    3635       40054 :         tmp = Log2_norm_lc( L_shl( L_tmp1, exp2 ) );
    3636       40054 :         exp1 = sub( 30, add( add( exp1, exp2 ), 12 ) );
    3637       40054 :         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       40054 :     exp = norm_l( log_gm_org_sum_fx );
    3646       40054 :     L_tmp2 = Mult_32_16( L_shl( log_gm_org_sum_fx, exp ), 24660 ); /*16 + exp + 13 - 15 */
    3647       40054 :     L_tmp2 = Mult_32_16( L_tmp2, inv_len_fx );                     /*14 + exp + 15 - 15 */
    3648       40054 :     L_tmp2 = L_shr( L_tmp2, exp );                                 /*14 */
    3649       40054 :     L_tmp = L_sub( L_tmp1, L_tmp2 );                               /*14 */
    3650             : 
    3651       40054 :     *SFM_org = L_max( 0, L_min( L_tmp, 98189 ) );
    3652       40054 :     move32();
    3653             : 
    3654       40054 :     IF( am_gen_fx != 0 )
    3655             :     {
    3656       40054 :         exp1 = norm_l( am_gen_fx );
    3657       40054 :         L_tmp1 = Mult_32_16( L_shl( am_gen_fx, exp1 ), inv_len_fx ); /*12 + exp1 + 15 - 15 */
    3658       40054 :         exp2 = norm_l( L_tmp1 );                                     /*12 + exp1 + exp2 */
    3659       40054 :         tmp = Log2_norm_lc( L_shl( L_tmp1, exp2 ) );
    3660       40054 :         exp1 = sub( 30, add( add( exp1, exp2 ), 12 ) );
    3661       40054 :         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       40054 :     exp = norm_l( log_gm_gen_sum_fx );
    3670       40054 :     L_tmp2 = Mult_32_16( L_shl( log_gm_gen_sum_fx, exp ), 24660 ); /*16 + exp + 13 - 15 */
    3671       40054 :     L_tmp2 = Mult_32_16( L_tmp2, inv_len_fx );                     /*14 + 15 - 15 */
    3672       40054 :     L_tmp2 = L_shr( L_tmp2, exp );                                 /*14 */
    3673       40054 :     L_tmp = L_sub( L_tmp1, L_tmp2 );                               /*14 */
    3674             : 
    3675       40054 :     *SFM_gen = L_max( 0, L_min( L_tmp, 98189 ) );
    3676       40054 :     move32();
    3677             : 
    3678       40054 :     return;
    3679             : }
    3680             : 
    3681             : /*-------------------------------------------------------------------*
    3682             :  * energy_control_fx_32()
    3683             :  *
    3684             :  *-------------------------------------------------------------------*/
    3685        2861 : 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        2861 :     FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD;
    3704             : 
    3705        2861 :     max_band = SWB_FENV;
    3706        2861 :     move16();
    3707        2861 :     band_step = 1;
    3708        2861 :     move16();
    3709             : 
    3710        2861 :     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        2861 :         gamma_fx = 18021;
    3739        2861 :         move16();
    3740        2861 :         get_normalize_spec_fx_32( core, -1, mode, -1, org_fx, SWB_signal_fx, &( hBWE_FD->prev_L_swb_norm1 ), offset );
    3741             : 
    3742        2861 :         if ( EQ_16( offset, HQ_GENERIC_FOFFSET_32K ) )
    3743             :         {
    3744           0 :             max_band = 12;
    3745           0 :             move16();
    3746             :         }
    3747             :     }
    3748             : 
    3749       42915 :     FOR( n_band = 0; n_band < max_band; n_band += band_step )
    3750             :     {
    3751       40054 :         calculate_tonality_fx_32( &org_fx[add( swb_bwe_subband[n_band], offset )], &SWB_signal_fx[add( swb_bwe_subband[n_band], offset )],
    3752       40054 :                                   &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       40054 :         L_temp1 = L_shl( SFM_gen_fx[n_band], 2 );
    3755       40054 :         L_temp2 = L_add( SFM_org_fx[n_band], L_shl( SFM_org_fx[n_band], 1 ) );
    3756       40054 :         IF( LT_32( L_temp1, L_temp2 ) )
    3757             :         {
    3758        2790 :             exp1 = sub( norm_l( SFM_gen_fx[n_band] ), 1 );
    3759        2790 :             exp2 = norm_l( SFM_org_fx[n_band] );
    3760        2790 :             tmp1 = extract_h( L_shl( SFM_gen_fx[n_band], exp1 ) );
    3761        2790 :             tmp2 = extract_h( L_shl( SFM_org_fx[n_band], exp2 ) );
    3762        2790 :             tmp = div_s( tmp1, tmp2 );                                /*15 + (14 + exp1 ) - (14 + exp2) */
    3763        2790 :             energy_factor_fx[n_band] = shl( tmp, sub( exp2, exp1 ) ); /*15 */
    3764        2790 :             move16();
    3765             : 
    3766        2790 :             if ( LT_16( energy_factor_fx[n_band], gamma_fx ) )
    3767             :             {
    3768        1001 :                 energy_factor_fx[n_band] = gamma_fx;
    3769        1001 :                 move16();
    3770             :             }
    3771             :         }
    3772             :         ELSE
    3773             :         {
    3774       37264 :             energy_factor_fx[n_band] = 32767;
    3775       37264 :             move16(); /*15 */
    3776             :         }
    3777             :     }
    3778             : 
    3779        2861 :     return;
    3780             : }
    3781             : 
    3782             : /*-------------------------------------------------------------------*
    3783             :  * decision_hq_generic_class_fx_32()
    3784             :  *
    3785             :  *-------------------------------------------------------------------*/
    3786        2746 : 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        2746 :     IF( EQ_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3802             :     {
    3803        2746 :         nband = 10;
    3804        2746 :         move16();
    3805        2746 :         inv_band_fx = 3277;
    3806        2746 :         move16(); /*15 */
    3807             :     }
    3808             :     ELSE
    3809             :     {
    3810           0 :         nband = 8;
    3811           0 :         move16();
    3812           0 :         inv_band_fx = 4096;
    3813           0 :         move16(); /*15 */
    3814             :     }
    3815             : 
    3816        2746 :     avgp2a_fx = L_deposit_l( 0 );
    3817       30206 :     FOR( k = 0; k < nband; k++ )
    3818             :     {
    3819       27460 :         a_fx = L_deposit_l( 0 );
    3820       27460 :         p_fx = L_deposit_l( 0 );
    3821       27460 :         tmp2 = add( swb_bwe_subband[k + 1], hq_generic_offset );
    3822      598628 :         FOR( i = add( swb_bwe_subband[k], hq_generic_offset ); i < tmp2; i++ )
    3823             :         {
    3824      571168 :             exp = norm_l( coefs_fx[i] );
    3825      571168 :             tmp = extract_h( L_shl( coefs_fx[i], exp ) );         /*12 + exp - 16 */
    3826      571168 :             L_tmp = L_mult0( tmp, tmp );                          /*2 * exp - 8 */
    3827      571168 :             L_tmp = L_shl_sat( L_tmp, sub( 14, shl( exp, 1 ) ) ); /*6 */
    3828      571168 :             IF( GT_32( L_tmp, p_fx ) )
    3829             :             {
    3830       97347 :                 p_fx = L_add( L_tmp, 0 ); /*6 */
    3831             :             }
    3832      571168 :             a_fx = L_add_sat( a_fx, L_tmp ); /*6 */
    3833             :         }
    3834             : 
    3835       27460 :         IF( a_fx > 0 )
    3836             :         {
    3837       27460 :             a_fx = Mult_32_16( a_fx, swb_inv_bwe_subband_width_fx[k] ); /*6 */
    3838             : 
    3839       27460 :             exp = norm_l( p_fx );
    3840       27460 :             tmp = Log2_norm_lc( L_shl( p_fx, exp ) ); /*15 */
    3841       27460 :             exp = sub( 30, add( exp, 6 ) );
    3842       27460 :             L_tmp1 = L_add( L_deposit_h( exp ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3843             : 
    3844       27460 :             exp = norm_l( a_fx );
    3845       27460 :             tmp = Log2_norm_lc( L_shl( a_fx, exp ) );
    3846       27460 :             exp = sub( 30, add( exp, 6 ) );
    3847       27460 :             L_tmp2 = L_add( L_deposit_h( exp ), L_shr( L_deposit_h( tmp ), 15 ) ); /*16 */
    3848             : 
    3849       27460 :             p2a_fx = L_sub( L_tmp1, L_tmp2 );       /*16 */
    3850       27460 :             avgp2a_fx = L_add( avgp2a_fx, p2a_fx ); /*16 */
    3851             :         }
    3852             :     }
    3853        2746 :     avgp2a_fx = Mult_32_16( avgp2a_fx, inv_band_fx ); /*16 + 15 - 15 */
    3854        2746 :     IF( GT_32( avgp2a_fx, 187227 ) )                  /*8.6 / 10log10(2), Q16 */
    3855             :     {
    3856          17 :         return HQ_GENERIC_EXC1;
    3857             :     }
    3858             :     ELSE
    3859             :     {
    3860        2729 :         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        3898 : void fd_bwe_enc_init_fx(
    4093             :     FD_BWE_ENC_HANDLE hBWE_FD /* i/o: FD BWE data handle  */
    4094             : )
    4095             : {
    4096        3898 :     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        3898 :     hBWE_FD->Q_new_input_hp = 0;
    4098        3898 :     move16();
    4099        3898 :     set16_fx( hBWE_FD->old_input_fx, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) );
    4100        3898 :     set16_fx( hBWE_FD->old_input_wb_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS ) );
    4101        3898 :     set16_fx( hBWE_FD->old_input_lp_fx, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_NS ) );
    4102        3898 :     set16_fx( hBWE_FD->old_syn_12k8_16k_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
    4103        3898 :     hBWE_FD->prev_mode = NORMAL;
    4104        3898 :     move16();
    4105        3898 :     set16_fx( hBWE_FD->L_old_wtda_swb_fx, 0, L_FRAME48k );
    4106        3898 :     hBWE_FD->prev_L_swb_norm1 = 8;
    4107        3898 :     move16();
    4108        3898 :     hBWE_FD->prev_global_gain_fx = 0;
    4109        3898 :     move32();
    4110        3898 :     hBWE_FD->modeCount = 0;
    4111        3898 :     move16();
    4112        3898 :     hBWE_FD->EnergyLF_fx = 0;
    4113        3898 :     hBWE_FD->EnergyLF_exp = 0;
    4114        3898 :     hBWE_FD->mem_old_wtda_swb_fx = 0;
    4115        3898 :     move32();
    4116        3898 :     move16();
    4117        3898 :     move32();
    4118             : 
    4119        3898 :     hBWE_FD->prev_Q_input_lp = 0;
    4120        3898 :     move16();
    4121        3898 :     set16_fx( hBWE_FD->old_fdbwe_speech_fx, 0, L_FRAME48k );
    4122        3898 :     hBWE_FD->mem_deemph_old_syn_fx = 0;
    4123        3898 :     move16();
    4124             : 
    4125        3898 :     hBWE_FD->q_mem_deemph_old_syn = 0;
    4126        3898 :     move16();
    4127             :     // hBWE_FD->mem_old_wtda_swb = 0.0f;
    4128             : 
    4129        3898 :     return;
    4130             : }
    4131             : 
    4132             : /*-------------------------------------------------------------------*
    4133             :  * hq_generic_encoding_fx()
    4134             :  *
    4135             :  *-------------------------------------------------------------------*/
    4136        2815 : 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        2815 :     HQ_ENC_HANDLE hHQ_core = st_fx->hHQ_core;
    4154        2815 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    4155             :     Word16 EnvCdbkFB_fx_loc[N_CB_FB * DIM_FB];
    4156             : 
    4157        2815 :     set16_fx( energy_factor_fx, 0, SWB_FENV );
    4158             : 
    4159        2815 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4160             :     {
    4161        2815 :         nenv = SWB_FENV;
    4162        2815 :         move16();
    4163             :     }
    4164             :     ELSE
    4165             :     {
    4166           0 :         nenv = SWB_FENV - 2;
    4167           0 :         move16();
    4168             :     }
    4169             : 
    4170             : 
    4171        2815 :     energy_control_fx_32( st_fx, HQ_CORE, -1, -1, coefs_fx, hq_generic_offset, energy_factor_fx );
    4172             : 
    4173        2815 :     IF( EQ_16( hHQ_core->hq_generic_speech_class, 1 ) )
    4174             :     {
    4175          94 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 1, 1 );
    4176          94 :         *hq_generic_exc_clas = HQ_GENERIC_SP_EXC;
    4177          94 :         move16();
    4178             :     }
    4179             :     ELSE
    4180             :     {
    4181        2721 :         *hq_generic_exc_clas = decision_hq_generic_class_fx_32( coefs_fx, hq_generic_offset );
    4182        2721 :         move16();
    4183        2721 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 0, 1 );
    4184        2721 :         push_indice( hBstr, IND_HQ_SWB_EXC_CLAS, *hq_generic_exc_clas, 1 );
    4185             :     }
    4186             : 
    4187       42225 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4188             :     {
    4189       39410 :         energy_fx = L_deposit_l( 0 );
    4190       39410 :         max_coefs_fx = L_deposit_l( 0 );
    4191       39410 :         tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    4192      940210 :         FOR( n_coeff = add( swb_bwe_subband[n_band], hq_generic_offset ); n_coeff < tmp2; n_coeff++ )
    4193             :         {
    4194      900800 :             IF( LT_32( max_coefs_fx, L_abs( coefs_fx[n_coeff] ) ) )
    4195             :             {
    4196      142938 :                 max_coefs_fx = L_abs( coefs_fx[n_coeff] );
    4197             :             }
    4198             :         }
    4199       39410 :         cs = norm_l( max_coefs_fx );
    4200       39410 :         tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    4201      940210 :         FOR( n_coeff = add( swb_bwe_subband[n_band], hq_generic_offset ); n_coeff < tmp2; n_coeff++ )
    4202             :         {
    4203      900800 :             tmp = extract_h( L_shl( coefs_fx[n_coeff], cs ) ); /*12 + cs - 16 */
    4204      900800 :             L_tmp = L_mult0( tmp, tmp );                       /*2*cs - 8 */
    4205      900800 :             energy_fx = L_add( energy_fx, L_shr( L_tmp, 5 ) );
    4206             :         }
    4207             : 
    4208       39410 :         IF( energy_fx != 0 )
    4209             :         {
    4210       39410 :             L_tmp = Mult_32_16( energy_fx, energy_factor_fx[n_band] );         /*2*cs - 13 */
    4211       39410 :             L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] ); /*2*cs - 13 + 15 - 15 */
    4212             : 
    4213       39410 :             exp = norm_l( L_tmp );
    4214       39410 :             tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    4215       39410 :             exp = sub( 30, add( exp, sub( shl( cs, 1 ), 13 ) ) );
    4216             : 
    4217       39410 :             L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */              /*10log10(2) in Q13 */
    4218       39410 :             hq_generic_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /*Q8 */
    4219       39410 :             move16();
    4220             :         }
    4221             :         ELSE
    4222             :         {
    4223           0 :             hq_generic_fenv_fx[n_band] = -24576;
    4224           0 :             move16();
    4225             :         }
    4226             :     }
    4227             : 
    4228        2815 :     IF( EQ_16( length, L_SPEC48k ) )
    4229             :     {
    4230        8840 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4231             :         {
    4232        6630 :             energy_fx = L_deposit_l( 0 );
    4233        6630 :             max_coefs_fx = L_deposit_l( 0 );
    4234        6630 :             tmp2 = fb_bwe_subband[n_band + 1];
    4235      360230 :             FOR( n_coeff = fb_bwe_subband[n_band]; n_coeff < tmp2; n_coeff++ )
    4236             :             {
    4237      353600 :                 IF( LT_32( max_coefs_fx, L_abs( coefs_fx[n_coeff] ) ) )
    4238             :                 {
    4239       30069 :                     max_coefs_fx = L_abs( coefs_fx[n_coeff] );
    4240             :                 }
    4241             :             }
    4242        6630 :             cs = norm_l( max_coefs_fx );
    4243        6630 :             tmp2 = fb_bwe_subband[n_band + 1];
    4244      360230 :             FOR( n_coeff = fb_bwe_subband[n_band]; n_coeff < tmp2; n_coeff++ )
    4245             :             {
    4246      353600 :                 tmp = extract_h( L_shl( coefs_fx[n_coeff], cs ) ); /*12 + cs - 16 */
    4247      353600 :                 L_tmp = L_mult0( tmp, tmp );                       /*2*cs - 8 */
    4248      353600 :                 energy_fx = L_add( energy_fx, L_shr( L_tmp, 5 ) );
    4249             :             }
    4250             : 
    4251        6630 :             IF( energy_fx != 0 )
    4252             :             {
    4253        6630 :                 L_tmp = Mult_32_16( energy_fx, fb_inv_bwe_subband_width_fx[n_band] ); /*2*cs - 13 + 18 - 15 */
    4254             : 
    4255        6630 :                 exp = norm_l( L_tmp );
    4256        6630 :                 tmp = Log2_norm_lc( L_shl( L_tmp, exp ) );
    4257        6630 :                 exp = sub( 30, add( exp, sub( shl( cs, 1 ), 13 ) ) );
    4258             : 
    4259        6630 :                 L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */                     /*10log10(2) in Q13 */
    4260        6630 :                 hq_generic_fenv_fx[n_band + nenv] = round_fx( L_shl( L_tmp, 10 ) ); /*Q8 */
    4261        6630 :                 move16();
    4262             :             }
    4263             :             ELSE
    4264             :             {
    4265           0 :                 hq_generic_fenv_fx[n_band + nenv] = -24576;
    4266           0 :                 move16();
    4267             :             }
    4268             :         }
    4269             :     }
    4270             : 
    4271        2815 :     freq_weights_fx( hq_generic_fenv_fx, w_NOR_fx, w_env_fx, nenv );
    4272             : 
    4273       42225 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4274             :     {
    4275       39410 :         hq_generic_fenv_fx[n_band] = sub( hq_generic_fenv_fx[n_band], Mean_env_fx[n_band] );
    4276       39410 :         move16();
    4277             :     }
    4278             : 
    4279        2815 :     IF( EQ_16( length, L_SPEC48k ) )
    4280             :     {
    4281        8840 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4282             :         {
    4283        6630 :             hq_generic_fenv_fx[n_band + nenv] = sub( shr( hq_generic_fenv_fx[n_band + nenv], 1 ), Mean_env_fb_fx[n_band] );
    4284        6630 :             move16();
    4285             :         }
    4286             :     }
    4287             : 
    4288             : 
    4289             :     /* Energy VQ */
    4290        2815 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4291             :     {
    4292        2815 :         msvq_interpol_fx( hq_generic_fenv_fx, w_env_fx, indice );
    4293             :     }
    4294             :     ELSE
    4295             :     {
    4296           0 :         msvq_interpol_2_fx( hq_generic_fenv_fx, w_env_fx, indice, nenv );
    4297             :     }
    4298             : 
    4299        2815 :     IF( EQ_16( length, L_SPEC48k ) )
    4300             :     {
    4301        2210 :         Word16 sf = getScaleFactor16( hq_generic_fenv_fx, add( DIM_FB, nenv ) );
    4302        2210 :         Scale_sig( hq_generic_fenv_fx, add( DIM_FB, nenv ), negate( sf ) );
    4303        2210 :         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        2210 :         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        2210 :         move16();
    4308             : 
    4309        2210 :         Scale_sig( hq_generic_fenv_fx, add( DIM_FB, nenv ), sf );
    4310             :     }
    4311             : 
    4312        2815 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[0], 5 );
    4313        2815 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[1], 7 );
    4314        2815 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[2], 6 );
    4315        2815 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[3], 5 );
    4316             : 
    4317        2815 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    4318             :     {
    4319        2815 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 6 );
    4320             :     }
    4321             :     ELSE
    4322             :     {
    4323           0 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 5 );
    4324             :     }
    4325             : 
    4326        2815 :     IF( EQ_16( length, L_SPEC48k ) )
    4327             :     {
    4328        2210 :         push_indice( hBstr, IND_FB_FENV_HQ, indice[5], 5 );
    4329             :     }
    4330             : 
    4331       42225 :     FOR( n_band = 0; n_band < nenv; n_band++ )
    4332             :     {
    4333       39410 :         tmp = add( hq_generic_fenv_fx[n_band], Mean_env_fx[n_band] ); /*8 */
    4334       39410 :         L_tmp = L_mult( tmp, 21771 );                                 /*26 */
    4335       39410 :         L_tmp = L_shr( L_tmp, 10 );                                   /*16 */
    4336       39410 :         L_Extract( L_tmp, &exp, &tmp );                               /* */
    4337       39410 :         tmp = extract_l( Pow2( 13, tmp ) );
    4338       39410 :         exp = sub( exp, 13 );
    4339       39410 :         hq_generic_fenv_fx[n_band] = shl_sat( tmp, add( exp, 1 ) ); /*1 */
    4340       39410 :         move16();
    4341             :     }
    4342             : 
    4343             : 
    4344        2815 :     IF( EQ_16( length, L_SPEC48k ) )
    4345             :     {
    4346        8840 :         FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    4347             :         {
    4348        6630 :             tmp = add( hq_generic_fenv_fx[n_band + nenv], Mean_env_fb_fx[n_band] ); /*7 */
    4349        6630 :             L_tmp = L_mult( tmp, 21771 );                                           /*25 */
    4350        6630 :             L_tmp = L_shr( L_tmp, 9 );                                              /*16 */
    4351        6630 :             L_Extract( L_tmp, &exp, &tmp );
    4352        6630 :             tmp = extract_l( Pow2( 13, tmp ) );
    4353        6630 :             exp = sub( exp, 13 );
    4354        6630 :             hq_generic_fenv_fx[n_band + nenv] = shl( tmp, add( exp, 1 ) ); /*1 */
    4355        6630 :             move16();
    4356             :         }
    4357             :     }
    4358             : 
    4359        2815 :     return;
    4360             : }

Generated by: LCOV version 1.14