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

Generated by: LCOV version 1.14