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

Generated by: LCOV version 1.14