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

Generated by: LCOV version 1.14