LCOV - code coverage report
Current view: top level - lib_com - swb_bwe_com_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 633e3f2e309758d10805ef21e0436356fe719b7a Lines: 1705 1913 89.1 %
Date: 2025-08-23 01:22:27 Functions: 14 16 87.5 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include "prot_fx.h"
      36             : #include "basop_util.h"
      37             : #include "rom_com.h"
      38             : 
      39             : 
      40             : /*==========================================================================*/
      41             : /* FUNCTION      : Word16 WB_BWE_gain_pred_fx ()             */
      42             : /*--------------------------------------------------------------------------*/
      43             : /* PURPOSE       : predict WB frequency envelopes for 0b WB BWE        */
      44             : /*--------------------------------------------------------------------------*/
      45             : /* INPUT ARGUMENTS  :                            */
      46             : /* _Word16 *core_dec_freq        i  : Frequency domain core decoded signal  */
      47             : /* _Word16 coder_type            i  : coding type                           */
      48             : /* _Word16 prev_coder_type       i  : coding type of last frame             */
      49             : /* _Word16 prev_WB_fenv          i  : envelope for last frame               */
      50             : /* _Word16 *voice_factors        i  : voicing factors      //Q15            */
      51             : /* _Word16 pitch_buf[]           i  : pitch buffer         //Q6             */
      52             : /* _Word16  last_core_brate      i  : previous frame core bitrate           */
      53             : /* _Word16 last_wb_bwe_ener      i  : previous frame wb bwe signal energy   */
      54             : /*--------------------------------------------------------------------------*/
      55             : /* OUTPUT ARGUMENTS :                                                       */
      56             : /* _Word16 *WB_fenv,             o  : WB frequency envelopes   Q3           */
      57             : /*--------------------------------------------------------------------------*/
      58             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
      59             : /*--------------------------------------------------------------------------*/
      60             : /* RETURN ARGUMENTS :                                                       */
      61             : /*          _Word16 mode                                                    */
      62             : /*--------------------------------------------------------------------------*/
      63             : /* CALLED FROM :                                                            */
      64             : /*==========================================================================*/
      65             : 
      66        4752 : Word16 WB_BWE_gain_pred_fx(
      67             :     Word16 *WB_fenv,             /* o  : WB frequency envelopes               Q3            */
      68             :     const Word16 *core_dec_freq, /* i  : Frequency domain core decoded signal Q_syn         */
      69             :     const Word16 coder_type,     /* i  : coding type                                        */
      70             :     Word16 prev_coder_type,      /* i  : coding type of last frame                          */
      71             :     Word16 prev_WB_fenv,         /* i  : envelope for last frame              Q3            */
      72             :     Word16 *voice_factors,       /* i  : voicing factors                      Q15           */
      73             :     const Word16 pitch_buf[],    /* i  : pitch buffer                         Q6            */
      74             :     Word32 last_core_brate,      /* i  : previous frame core bitrate                        */
      75             :     Word16 last_wb_bwe_ener,     /* i  : previous frame wb bwe signal energy  Q3            */
      76             :     Word16 Q_syn,                /* i  : synthesis scaling                                  */
      77             :     Word16 last_extl_fx,         /* i  : extl. layer for last frame                         */
      78             :     Word16 tilt_wb_fx            /*                                           Q4            */
      79             : )
      80             : {
      81             :     Word32 enerL;
      82             :     Word16 n_freq, mode, pitch;
      83        4752 :     Word16 ener_var_flag = 0;
      84        4752 :     move16();
      85             :     Word16 voice_factor, enerL_16, enerL_40, enerL_64;
      86        4752 :     Word16 env_var_flag = 0;
      87        4752 :     move16();
      88             :     Word16 exp;
      89             :     Word16 tmp, tmp1;
      90             :     Word32 L_tmp;
      91             :     Word32 L_WB_fenv0, L_WB_fenv1;
      92             :     Word16 pitch_buf_tmp[4];
      93        4752 :     Word16 alfa = MAX_16;
      94        4752 :     move16();
      95             : 
      96        4752 :     mode = NORMAL;
      97        4752 :     move16();
      98             : 
      99        4752 :     enerL = L_deposit_l( 0 );
     100      308880 :     FOR( n_freq = 128; n_freq < 192; n_freq++ )
     101             :     {
     102      304128 :         enerL = L_mac0_sat( enerL, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2(Q_syn)  */
     103             :     }
     104             : 
     105        4752 :     L_WB_fenv0 = L_deposit_l( 0 );
     106      156816 :     FOR( n_freq = 192; n_freq < 224; n_freq++ )
     107             :     {
     108      152064 :         L_WB_fenv0 = L_mac0_sat( L_WB_fenv0, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
     109             :     }
     110             : 
     111        4752 :     L_WB_fenv1 = L_deposit_l( 0 );
     112      156816 :     FOR( n_freq = 224; n_freq < 256; n_freq++ )
     113             :     {
     114      152064 :         L_WB_fenv1 = L_mac0_sat( L_WB_fenv1, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
     115             :     }
     116             : 
     117        4752 :     L_tmp = sum16_32_fx( voice_factors, 4 );
     118        4752 :     voice_factor = extract_l( L_shr( L_tmp, 2 ) ); /*Q13 */
     119        4752 :     Copy_Scale_sig( pitch_buf, pitch_buf_tmp, 4, -1 );
     120        4752 :     pitch = sum16_fx( pitch_buf_tmp, 4 );
     121             :     /*Q5 */
     122             : 
     123        4752 :     L_tmp = L_shr( enerL, 4 );
     124        4752 :     test();
     125        4752 :     if ( GT_32( L_max( L_WB_fenv1, L_WB_fenv0 ), L_tmp ) && GT_16( 9856 /* 308 in Q5 */, pitch ) )
     126             :     {
     127        4394 :         ener_var_flag = 1;
     128        4394 :         move16();
     129             :     }
     130             : 
     131        4752 :     test();
     132        4752 :     IF( GT_32( L_WB_fenv0, L_shl_sat( L_WB_fenv1, 1 ) ) )
     133             :     {
     134         998 :         exp = norm_l( L_WB_fenv0 );
     135         998 :         tmp = extract_h( L_shl( L_WB_fenv0, exp ) );
     136         998 :         tmp = div_s( 16384 /* 1 in Q14 */, tmp );                                   /*Q(15+14-2*Q_syn-exp) */
     137         998 :         L_tmp = L_shr( Mult_32_16( L_shl( L_WB_fenv1, 1 ), tmp ), sub( 15, exp ) ); /*2*Q_syn+15+exp-15->2*Q_syn+exp */
     138             :         /*L_tmp Q15   */
     139         998 :         tmp = extract_l( L_tmp );
     140         998 :         alfa = s_max( tmp, 3277 /* 0.1 in Q15 */ );  /*Q15 */
     141         998 :         L_WB_fenv0 = Mult_32_16( L_WB_fenv0, alfa ); /*2*Q_syn+15-15->2*Q_syn */
     142             :     }
     143        3754 :     ELSE IF( GT_32( L_WB_fenv1, L_shl_sat( L_WB_fenv0, 1 ) ) && NE_16( coder_type, UNVOICED ) )
     144             :     {
     145         157 :         exp = norm_l( L_WB_fenv1 );
     146         157 :         tmp = extract_h( L_shl( L_WB_fenv1, exp ) );
     147         157 :         tmp = div_s( 16384 /* 1 in Q14 */, tmp );                                   /*Q(15+14-2*Q_syn-exp) */
     148         157 :         L_tmp = L_shr( Mult_32_16( L_shl( L_WB_fenv0, 1 ), tmp ), sub( 15, exp ) ); /*2*Q_syn+15+exp-15->2*Q_syn+exp */
     149             :         /*L_tmp Q15 */
     150         157 :         tmp = extract_l( L_tmp );
     151         157 :         alfa = s_max( tmp, 3277 /* 0.1 in Q15 */ );  /*Q15 */
     152         157 :         L_WB_fenv1 = Mult_32_16( L_WB_fenv1, alfa ); /*2*Q_syn+15-15->2*Q_syn                      */
     153             :     }
     154             : 
     155        4752 :     if ( L_WB_fenv0 == 0 )
     156             :     {
     157           0 :         L_WB_fenv0 = L_deposit_l( 1 );
     158             :     }
     159             : 
     160        4752 :     if ( L_WB_fenv1 == 0 )
     161             :     {
     162           0 :         L_WB_fenv1 = L_deposit_l( 1 );
     163             :     }
     164             : 
     165        4752 :     if ( enerL == 0 )
     166             :     {
     167           0 :         enerL = L_deposit_l( 1 );
     168             :     }
     169             : 
     170        4752 :     L_tmp = L_add_sat( L_WB_fenv0, L_WB_fenv1 ); /* In 2*Q_syn */
     171        4752 :     exp = norm_l( L_tmp );
     172        4752 :     tmp = extract_h( L_shl( L_tmp, exp ) );
     173             :     /*exp = sub(exp, 30-(2*Q_syn+6)); //+6(/64) */
     174        4752 :     exp = sub( exp, sub( 30, add( shl( Q_syn, 1 ), 6 ) ) ); /*+6(/64) */
     175             : 
     176        4752 :     tmp = div_s( 16384, tmp );
     177        4752 :     L_tmp = L_deposit_h( tmp );
     178        4752 :     L_tmp = Isqrt_lc( L_tmp, &exp );
     179             : 
     180        4752 :     WB_fenv[0] = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /* Q3 */
     181        4752 :     move16();
     182        4752 :     test();
     183        4752 :     test();
     184        4752 :     IF( NE_16( coder_type, AUDIO ) && NE_16( coder_type, UNVOICED ) && ener_var_flag == 0 )
     185             :     {
     186         358 :         WB_fenv[0] = add_sat( WB_fenv[0], mult_r_sat( WB_fenv[0], 16384 /* 0.5 in Q15 */ ) );
     187         358 :         move16();
     188             :     }
     189             : 
     190        4752 :     exp = norm_l( enerL );
     191        4752 :     tmp = extract_h( L_shl( enerL, exp ) );
     192        4752 :     exp = sub( exp, sub( 30, shl( Q_syn, 1 ) ) );
     193             : 
     194        4752 :     tmp = div_s( 16384, tmp );
     195        4752 :     L_tmp = L_deposit_h( tmp );
     196        4752 :     L_tmp = Isqrt_lc( L_tmp, &exp );
     197        4752 :     enerL_16 = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 15 ) ) ); /* Q0 */
     198        4752 :     enerL_40 = mult_r( 6554 /* 0.2 in Q15 */, enerL_16 );          /*1/40 in Q18  ->Q3    */
     199             : 
     200        4752 :     test();
     201        4752 :     test();
     202        4752 :     test();
     203        4752 :     test();
     204        4752 :     test();
     205        4752 :     test();
     206        4752 :     IF( NE_16( coder_type, TRANSITION ) && NE_16( coder_type, AUDIO ) && NE_16( coder_type, UNVOICED ) &&
     207             :         GT_16( enerL_40, WB_fenv[0] ) && GT_16( alfa, 29491 /*0.9 in Q15*/ ) && !( EQ_16( coder_type, prev_coder_type ) && GT_16( WB_fenv[0], prev_WB_fenv ) ) )
     208             :     {
     209           0 :         IF( WB_fenv[0] != 0 )
     210             :         {
     211           0 :             exp = norm_s( WB_fenv[0] );
     212           0 :             tmp = div_s( shl( 1, sub( 14, exp ) ), WB_fenv[0] );     /*Q(29-exp-3) */
     213           0 :             L_tmp = L_mult( enerL_40, tmp );                         /*Q(30-exp) */
     214           0 :             tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 2 ) ) ); /*Q12 */
     215           0 :             tmp = s_min( tmp, 16384 /*4 in Q12*/ );
     216           0 :             L_tmp = L_shr( L_mult0( tmp, WB_fenv[0] ), 12 ); /*Q15 */
     217           0 :             WB_fenv[0] = extract_l( L_tmp );                 /*Q3 */
     218           0 :             move16();
     219             :         }
     220             : 
     221           0 :         IF( GT_16( WB_fenv[0], prev_WB_fenv ) )
     222             :         {
     223             :             /*WB_fenv[0]= add(mult_r(9830, WB_fenv[0]), mult_r(22938, prev_WB_fenv));    move16();//Q3 */
     224           0 :             WB_fenv[0] = round_fx( L_mac( L_mult( 9830 /* 0.5 in Q15 */, WB_fenv[0] ), 22938 /* 0.7 in Q15 */, prev_WB_fenv ) ); /*Q3 */
     225           0 :             move16();
     226             :         }
     227             :     }
     228             : 
     229        4752 :     L_tmp = L_mult0( voice_factor, 77 );
     230        4752 :     tmp1 = extract_l( L_shr( L_tmp, 13 ) );
     231             : 
     232        4752 :     exp = norm_s( pitch );
     233        4752 :     tmp = div_s( shl( 1, sub( 14, exp ) ), pitch );          /*Q(29-exp-5) */
     234        4752 :     L_tmp = L_mult0( tmp1, tmp );                            /*29-exp-5->24-exp */
     235        4752 :     tmp = round_fx_sat( L_shl_sat( L_tmp, add( exp, 6 ) ) ); /*14          */
     236        4752 :     tmp1 = s_max( tmp, 8192 /* 0.5 in Q14 */ );
     237        4752 :     alfa = s_min( 24576 /* 1.5 in Q14 */, tmp1 ); /*Q14 */
     238             : 
     239        4752 :     L_tmp = L_mult0( alfa, WB_fenv[0] ); /*Q14+Q3->Q17 */
     240        4752 :     L_tmp = L_shr( L_tmp, 14 );          /*Q3 */
     241        4752 :     tmp = extract_l( L_tmp );
     242             : 
     243        4752 :     enerL = L_deposit_l( enerL_16 );
     244        4752 :     enerL = L_shl( enerL, 6 ); /*Q6 */
     245             :     /* Here, we do not multiply L_tmp by 3 to avoid overflow */
     246        4752 :     L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */
     247             : 
     248        4752 :     test();
     249        4752 :     test();
     250        4752 :     test();
     251             : #define ONE_DIV_3 ( (Word32) 0x2AAAAAAA )
     252             :     /* Here, L_tmp is not pre-multiplied with 3, we multiply enerL with 1/3 */
     253        4752 :     IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( Mpy_32_32( ONE_DIV_3, enerL ), L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 )
     254             :     {
     255          31 :         env_var_flag = 1;
     256          31 :         move16();
     257          31 :         enerL_64 = mult_r( 4096, enerL_16 ); /* 1/64 in Q18  ->Q3 */
     258             : 
     259          31 :         exp = norm_s( WB_fenv[0] );
     260          31 :         tmp = div_s( shl( 1, sub( 14, exp ) ), WB_fenv[0] );     /*Q(29-exp-3) */
     261          31 :         L_tmp = L_mult( enerL_64, tmp );                         /*Q(30-exp) */
     262          31 :         tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 2 ) ) ); /*Q12 */
     263          31 :         tmp = s_min( tmp, 16384 /*4 in Q12*/ );
     264          31 :         L_tmp = L_shr( L_mult0( tmp, WB_fenv[0] ), 12 ); /*Q3 */
     265          31 :         WB_fenv[0] = extract_l( L_tmp );                 /*Q3 */
     266          31 :         move16();
     267          31 :         IF( GT_16( WB_fenv[0], prev_WB_fenv ) )
     268             :         {
     269             :             /*WB_fenv[0] = add(mult_r(9830, WB_fenv[0]), mult_r(22938, prev_WB_fenv));//Q3 */
     270          29 :             WB_fenv[0] = round_fx( L_mac( L_mult( 9830 /* 0.3 in Q15 */, WB_fenv[0] ), 22938 /* 0.7 in Q15 */, prev_WB_fenv ) ); /*Q3 */
     271          29 :             move16();
     272             :         }
     273             :     }
     274             : 
     275        4752 :     test();
     276        4752 :     IF( EQ_16( coder_type, UNVOICED ) || EQ_16( prev_coder_type, UNVOICED ) )
     277             :     {
     278          35 :         WB_fenv[0] = shr( WB_fenv[0], 1 );
     279          35 :         move16(); /*Q3 */
     280             :     }
     281             : 
     282        4752 :     IF( NE_16( coder_type, AUDIO ) )
     283             :     {
     284        4752 :         tmp = mult_r( voice_factor, 19661 /* 1.2 in Q14 */ ); /*Q12 */
     285        4752 :         tmp = s_max( tmp, 4096 /* 1 in Q12 */ );
     286        4752 :         exp = norm_s( tmp );
     287        4752 :         tmp = div_s( shl( 1, sub( 14, exp ) ), tmp ); /*Q(29-exp-12) */
     288        4752 :         L_tmp = L_mult( WB_fenv[0], tmp );            /*Q(21-exp) */
     289             : 
     290        4752 :         WB_fenv[0] = round_fx( L_shl( L_tmp, sub( exp, 2 ) ) ); /*Q3 */
     291        4752 :         move16();
     292        4752 :         tmp1 = mult_r( 328 /* (1/400) in Q17 */, pitch );                      /*Q7 */
     293        4752 :         tmp = s_min( s_max( tmp1, 16 /* 0.125 in Q7 */ ), 256 /* 1 in Q7 */ ); /*Q7 */
     294        4752 :         L_tmp = L_shr( L_mult0( WB_fenv[0], tmp ), 7 );                        /*Q3 */
     295             :                                                                                /*WB_fenv[0] = saturate(L_tmp); //Q3 */
     296        4752 :         WB_fenv[0] = round_fx_sat( L_shl_sat( L_tmp, 16 ) );                   /*Q3 */
     297        4752 :         move16();
     298             :     }
     299        4752 :     test();
     300        4752 :     IF( GT_32( last_core_brate, ACELP_8k00 ) && GT_16( WB_fenv[0], last_wb_bwe_ener ) )
     301             :     {
     302             :         /*WB_fenv[0]= add(mult_r(29491, last_wb_bwe_ener), mult_r(3277, WB_fenv[0]));//Q3 */
     303         720 :         WB_fenv[0] = round_fx( L_mac( L_mult( 29491 /* 0.9 in Q15 */, last_wb_bwe_ener ), 3277, WB_fenv[0] ) ); /*Q3 */
     304         720 :         move16();
     305             :     }
     306             : 
     307        4752 :     IF( NE_16( last_extl_fx, WB_BWE ) && LT_16( tilt_wb_fx, 128 /* 8 in Q4 */ ) )
     308             :     {
     309           8 :         WB_fenv[0] = mult_r( s_min( 16384 /* 0.5 in Q15 */, shl( tilt_wb_fx, 8 ) ), WB_fenv[0] );
     310           8 :         move16();
     311             :     }
     312             : 
     313        4752 :     IF( EQ_16( env_var_flag, 1 ) )
     314             :     {
     315          31 :         WB_fenv[1] = add( WB_fenv[0], mult_r( WB_fenv[0], 16384 /* 0.5 in Q15 */ ) );
     316          31 :         move16();
     317          31 :         WB_fenv[0] = mult_r( 24576 /* 0.75 in Q15 */, WB_fenv[0] );
     318          31 :         move16(); /*Q3                 */
     319             :     }
     320             :     ELSE
     321             :     {
     322        4721 :         WB_fenv[1] = WB_fenv[0];
     323        4721 :         move16();
     324             :     }
     325             : 
     326        4752 :     test();
     327        4752 :     IF( EQ_16( coder_type, UNVOICED ) || EQ_16( prev_coder_type, UNVOICED ) )
     328             :     {
     329          35 :         WB_fenv[1] = shr( WB_fenv[1], 1 );
     330          35 :         move16(); /*Q3 */
     331             :     }
     332             : 
     333        4752 :     return ( mode );
     334             : }
     335             : 
     336             : /*-------------------------------------------------------------------*
     337             :  * calc_norm_envelop_lf_fx()
     338             :  *
     339             :  * calc_envelope of low frequency spectrum
     340             :  *-------------------------------------------------------------------*/
     341        2851 : static void calc_norm_envelop_lf_fx(
     342             :     const Word32 SWB_signal[],
     343             :     /* i  : SWB spectrum                                    */ /* Q12 */
     344             :     Word32 *envelope,
     345             :     /* o  : normalized envelope                             */ /* Q12 */
     346             :     Word16 *L_swb_norm,                                        /* i/o : length of envelope                              */
     347             :     const Word16 HQ_mode,
     348             :     /* i  : HQ mode                                         */ /* Q0 */
     349             :     const Word16 hq_generic_offset,
     350             :     /* i  : frequency offset for representing hq generic    */ /* Q0 */
     351             :     Word16 *sfreq,                                             /* i  : starting frequency index                        */
     352             :     Word16 *efreq                                              /* i  : ending frequency index                          */
     353             : )
     354             : {
     355             :     Word16 lookback, env_index, n_freq, n_lag_now, n_lag;
     356             : 
     357        2851 :     *sfreq = 2;
     358        2851 :     move16();
     359        2851 :     IF( EQ_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
     360             :     {
     361        2851 :         *efreq = 146;
     362        2851 :         move16();
     363        2851 :         if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
     364             :         {
     365        2115 :             *efreq = 306;
     366        2115 :             move16();
     367             :         }
     368        2851 :         IF( LT_16( add( shl( sub( 328, *efreq ), 1 ), 1 ), *L_swb_norm ) )
     369             :         {
     370          39 :             *L_swb_norm = add( shl( sub( 328, *efreq ), 1 ), 1 );
     371          39 :             move16();
     372             :         }
     373             :     }
     374             :     ELSE
     375             :     {
     376           0 :         *efreq = 130;
     377           0 :         move16();
     378           0 :         if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
     379             :         {
     380           0 :             *efreq = 290;
     381           0 :             move16();
     382             :         }
     383           0 :         IF( LT_16( add( shl( sub( 400, *efreq ), 1 ), 1 ), *L_swb_norm ) )
     384             :         {
     385           0 :             *L_swb_norm = add( shl( sub( 400, *efreq ), 1 ), 1 );
     386             :         }
     387             :     }
     388        2851 :     lookback = shr( *L_swb_norm, 1 );
     389        2851 :     env_index = 0;
     390        2851 :     move16();
     391        2851 :     n_lag_now = *L_swb_norm;
     392        2851 :     move16();
     393             : 
     394       19690 :     FOR( n_freq = 0; n_freq < lookback; n_freq++ )
     395             :     {
     396             :         Word16 tmp;
     397             :         Word32 L_tmp;
     398             : 
     399       16839 :         L_tmp = L_deposit_l( 1 );
     400       16839 :         tmp = add( lookback, n_freq );
     401      280875 :         FOR( n_lag = 0; n_lag < tmp; n_lag++ )
     402             :         {
     403      264036 :             L_tmp = L_add_sat( L_tmp, L_abs( SWB_signal[n_lag] ) );
     404             :         }
     405       16839 :         envelope[env_index] = L_tmp;
     406       16839 :         move32();
     407       16839 :         env_index = add( env_index, 1 );
     408             :     }
     409             : 
     410      740658 :     FOR( ; n_freq < *efreq; n_freq++ )
     411             :     {
     412             :         /* Apply MA filter */
     413      737807 :         Word32 L_tmp = L_deposit_l( 1 );
     414     9298115 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
     415             :         {
     416     8560308 :             L_tmp = L_add_sat( L_tmp, L_abs( SWB_signal[n_freq - lookback + n_lag] ) );
     417             :         }
     418             : 
     419      737807 :         envelope[env_index] = L_tmp;
     420      737807 :         move32();
     421      737807 :         env_index = add( env_index, 1 );
     422             :     }
     423             : 
     424        2851 :     return;
     425             : }
     426             : /*-------------------------------------------------------------------*
     427             :  * calc_normal_length()
     428             :  *
     429             :  *-------------------------------------------------------------------*/
     430             : 
     431       21519 : void calc_normal_length_fx(
     432             :     const Word16 core,       /* i  : core                                 */
     433             :     const Word16 *sp,        /* i  : input signal                Q_syn    */
     434             :     const Word16 mode,       /* i  : input mode                           */
     435             :     const Word16 extl,       /* i  : extension layer                      */
     436             :     Word16 *L_swb_norm,      /* o  : normalize length                     */
     437             :     Word16 *prev_L_swb_norm, /*i/o : last normalize length                */
     438             :     Word16 Q_syn )
     439             : {
     440             :     Word16 i, n_freq, n_band, THRES;
     441             :     const Word16 *pit;
     442             :     Word16 peak, mag;
     443             :     Word16 L_swb_norm_trans, L_swb_norm_norm, L_swb_norm_harm, L_swb_norm_cur;
     444             :     Word16 N;
     445             :     Word32 L_mean, L_tmp, L_tmp1;
     446             : 
     447       21519 :     THRES = 4;
     448       21519 :     move16();
     449       21519 :     test();
     450       21519 :     test();
     451       21519 :     if ( EQ_16( core, HQ_CORE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
     452             :     {
     453        9855 :         THRES = 8;
     454        9855 :         move16();
     455             :     }
     456             : 
     457       21519 :     N = 16;
     458       21519 :     move16();
     459       21519 :     test();
     460       21519 :     test();
     461       21519 :     if ( EQ_16( core, HQ_CORE ) && ( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) ) )
     462             :     {
     463           0 :         N = 13;
     464           0 :         move16();
     465             :     }
     466             : 
     467       21519 :     n_band = 0;
     468       21519 :     move16();
     469       21519 :     pit = sp;
     470      365823 :     FOR( i = 0; i < N; i++ )
     471             :     {
     472      344304 :         peak = 0;
     473      344304 :         move16();
     474      344304 :         L_mean = L_deposit_l( 0 );
     475             : 
     476     5853168 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
     477             :         {
     478     5508864 :             mag = abs_s( *pit );
     479             : 
     480     5508864 :             peak = s_max( peak, mag );
     481     5508864 :             L_mean = L_add( L_mean, mag ); /*Q_syn */
     482     5508864 :             pit++;
     483             :         }
     484             : 
     485      344304 :         L_tmp = L_mult0( peak, 15 + THRES ); /*Q_syn */
     486      344304 :         IF( EQ_16( THRES, 8 ) )
     487             :         {
     488      157680 :             L_tmp1 = L_shl( Mpy_32_16_1( L_mean, 32767 ), 3 );
     489             :         }
     490             :         ELSE
     491             :         {
     492      186624 :             L_tmp1 = L_shl( Mpy_32_16_1( L_mean, 32767 ), 2 );
     493             :         }
     494             : 
     495      344304 :         test();
     496      344304 :         if ( GT_32( L_tmp, L_tmp1 ) && ( GT_16( peak, shl( 10, Q_syn ) ) ) )
     497             :         {
     498       36129 :             n_band = add( 1, n_band );
     499             :         }
     500             :     }
     501             : 
     502       21519 :     IF( EQ_16( core, ACELP_CORE ) )
     503             :     {
     504       21519 :         L_swb_norm_trans = add( 4, mult( n_band, 8192 /* 0.25 in Q15 */ ) );
     505       21519 :         L_swb_norm_norm = add( 8, mult( n_band, 16384 /* 0.5 in Q15 */ ) );
     506             : 
     507       21519 :         L_tmp = L_add( 65536, L_mult0( n_band, 4096 ) );              /*Q16 */
     508       21519 :         L_swb_norm_harm = s_max( round_fx( L_shl( L_tmp, 5 ) ), 24 ); /* Q0 */
     509             : 
     510       21519 :         IF( EQ_16( mode, HARMONIC ) )
     511             :         {
     512        1647 :             L_swb_norm_cur = L_swb_norm_harm;
     513        1647 :             move16();
     514             :         }
     515       19872 :         ELSE IF( EQ_16( mode, NORMAL ) )
     516             :         {
     517       19161 :             L_swb_norm_cur = L_swb_norm_norm;
     518       19161 :             move16();
     519             :         }
     520             :         ELSE
     521             :         {
     522         711 :             L_swb_norm_cur = L_swb_norm_trans;
     523         711 :             move16();
     524             :         }
     525       21519 :         *L_swb_norm = shr( add( L_swb_norm_cur, *prev_L_swb_norm ), 1 );
     526       21519 :         move16();
     527       21519 :         *prev_L_swb_norm = L_swb_norm_cur;
     528       21519 :         move16();
     529             :     }
     530             :     ELSE
     531             :     {
     532           0 :         test();
     533           0 :         IF( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) )
     534             :         {
     535           0 :             L_tmp = L_add( 65536, L_mult( n_band, 2560 /* 2.5 in Q10*/ ) );
     536           0 :             L_swb_norm_cur = round_fx( L_shl( L_tmp, 5 ) ); /*Q0 */
     537             :         }
     538             :         ELSE
     539             :         {
     540           0 :             L_tmp = L_add( 65536, L_mult( n_band, 2048 ) ); /*Q16 */
     541           0 :             L_swb_norm_cur = round_fx( L_shl( L_tmp, 3 ) ); /*Q0 */
     542             :         }
     543             : 
     544             :         /**L_swb_norm = add(mult_r(L_swb_norm_cur, 3277), mult_r(*prev_L_swb_norm, 29491)); */
     545           0 :         *L_swb_norm = round_fx( L_mac( L_mult( L_swb_norm_cur, 3277 /* 0.1 in Q15 */ ), *prev_L_swb_norm, 29491 /* 0.9 in Q15 */ ) );
     546           0 :         move16();
     547           0 :         *prev_L_swb_norm = L_swb_norm_cur;
     548           0 :         move16();
     549             :     }
     550             : 
     551       21519 :     return;
     552             : }
     553             : /*-------------------------------------------------------------------*
     554             :  * calc_tilt_bwe()
     555             :  *
     556             :  * calculate tilt parameter
     557             :  *-------------------------------------------------------------------*/
     558             : 
     559      160502 : Word32 calc_tilt_bwe_fx(                      /* o  : Tilt in Q24                      */
     560             :                          const Word16 *sp,    /* i  : input signal     Q(15 - exp_sp)  */
     561             :                          const Word16 exp_sp, /* i  : Exp of inp signal                */
     562             :                          const Word16 N       /* i  : signal length                    */
     563             : )
     564             : {
     565             :     Word16 i, j;
     566             :     Word32 L_ener, L_ener_tot, L_temp;
     567             :     Word16 tmp1, tmp2;
     568             :     const Word16 *ptr;
     569             :     Word16 exp2;
     570             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     571      160502 :     Flag Overflow = 0;
     572      160502 :     move32();
     573             : #endif
     574             : 
     575             :     BASOP_SATURATE_WARNING_OFF_EVS
     576             : 
     577             :     /* this is required for adaptative precision energy summation loop, do not remove */
     578      160502 :     Overflow = 0;
     579      160502 :     move16();
     580      160502 :     exp2 = 0;
     581      160502 :     move16();
     582             : 
     583      160502 :     ptr = sp;
     584      160502 :     move16();
     585      160502 :     L_ener_tot = L_deposit_l( 1 );
     586             : 
     587             :     /* Divide Frame Length by 32 */
     588     1608312 :     FOR( j = shr( N, 5 ); j > 0; j-- )
     589             :     {
     590     1447810 :         tmp1 = mult_ro( *ptr++, 8192 /*0.25 in Q15 */, &Overflow ); /* Divide by 4 */
     591     1447810 :         L_ener = L_mult0( tmp1, tmp1 );
     592             :         /* With the shift by 4 and the L_mult0, no overflow possible for 32 samples */
     593    46329920 :         FOR( i = 1; i < 32; i++ )
     594             :         {
     595    44882110 :             tmp1 = mult_ro( *ptr++, 8192 /*0.25 in Q15 */, &Overflow ); /* Divide by 4 */
     596    44882110 :             L_ener = L_mac0_o( L_ener, tmp1, tmp1, &Overflow );
     597             :         }
     598             :         /*Overflow will never happen because exp2 is always positive*/
     599     1447810 :         L_ener = L_shr( L_ener, exp2 );
     600     1447810 :         L_temp = L_add_o( L_ener_tot, L_ener, &Overflow );
     601     1447810 :         IF( Overflow != 0 )
     602             :         {
     603          29 :             L_ener_tot = L_shr( L_ener_tot, 1 );
     604          29 :             L_ener = L_shr( L_ener, 1 );
     605          29 :             exp2 = add( exp2, 1 );
     606             :             /* this is required, do not remove */
     607          29 :             Overflow = 0;
     608          29 :             move16();
     609             :         }
     610     1447810 :         L_ener_tot = L_add_o( L_ener_tot, L_ener, &Overflow );
     611             :     }
     612      160502 :     L_ener = L_deposit_l( abs_s( sub_o( sp[1], sp[0], &Overflow ) ) );
     613    46169418 :     FOR( i = 2; i < N; i++ )
     614             :     {
     615             :         /* Eq to (sp[i] - sp[i-1]) * (sp[i-1] - sp[i-2]) < 0 */
     616    46008916 :         tmp1 = sub_o( sp[i], sp[i - 1], &Overflow );
     617    46008916 :         tmp2 = sub_o( sp[i - 1], sp[i - 2], &Overflow );
     618    46008916 :         tmp2 = mult( tmp1, tmp2 );
     619    46008916 :         tmp1 = abs_s( tmp1 );
     620             :         /* to Get either 0 or -1 in 'tmp2' */
     621    46008916 :         tmp2 = shr( tmp2, 15 );
     622             :         /* this allows this code */
     623    46008916 :         L_ener = L_msu0( L_ener, tmp2, tmp1 );
     624             :         /* instead of this one */
     625             :         /* test(); */
     626             :         /* if (tmp2 < 0) */
     627             :         /* { */
     628             :         /*     L_ener = L_mac0(L_ener, 1, tmp1); */
     629             :         /* } */
     630             :         /* it saves one op */
     631             :     }
     632             : 
     633      160502 :     tmp1 = norm_l( L_ener_tot );
     634      160502 :     L_temp = L_shl( L_ener_tot, tmp1 );
     635      160502 :     tmp1 = sub( add( 31 + 4, exp2 ), add( tmp1, shl( exp_sp, 1 ) ) );
     636      160502 :     L_temp = Isqrt_lc( L_temp, &tmp1 );
     637             : 
     638             :     /* *tilt_flt = (float)(r1/sqrt(r0)); */
     639      160502 :     exp2 = norm_l( L_ener );
     640      160502 :     L_temp = Mult_32_16( L_temp, round_fx_sat( L_shl_sat( L_ener, exp2 ) ) );
     641      160502 :     exp2 = sub( exp2, tmp1 );
     642      160502 :     exp2 = add( exp2, exp_sp );
     643             : 
     644             :     /* Put in Q24 */
     645      160502 :     L_temp = L_shr_sat( L_temp, sub( exp2, 24 ) );
     646             :     BASOP_SATURATE_WARNING_ON_EVS
     647             : 
     648      160502 :     return L_temp;
     649             : }
     650      174040 : Word16 ivas_calc_tilt_bwe_fx(                      /* o  : Tilt in Q24                        */
     651             :                               const Word32 *sp,    /* i  : input signal                       */
     652             :                               const Word16 exp_sp, /* i  : Exp of inp signal   Q(15 - exp_sp) */
     653             :                               const Word16 N       /* i  : signal length                      */
     654             : )
     655             : {
     656             :     Word16 i, j;
     657             :     Word32 L_ener, L_ener_tot, L_temp;
     658             :     Word32 tmp1, tmp2;
     659      174040 :     Word16 sign = 0;
     660      174040 :     move16();
     661             :     const Word32 *ptr;
     662             :     Word16 exp2, tmp_exp;
     663             : 
     664             :     BASOP_SATURATE_WARNING_OFF_EVS
     665             : 
     666             :     /* this is required for adaptative precision energy summation loop, do not remove */
     667      174040 :     exp2 = 0;
     668      174040 :     move16();
     669      174040 :     ptr = sp;
     670             : 
     671      174040 :     L_ener_tot = L_deposit_l( 1 );
     672             :     /* Divide Frame Length by 32 */
     673             :     Word64 W_ener;
     674      174040 :     W_ener = Mpy_32_32( *ptr, *ptr );
     675      174040 :     ptr++;
     676   111385600 :     FOR( j = 1; j < N; j++ )
     677             :     {
     678             :         /* With the shift by 4 and the L_mult0, no overflow possible for 32 samples */
     679   111211560 :         W_ener = W_mac_32_32( W_ener, *ptr, *ptr );
     680   111211560 :         ptr++;
     681             :     }
     682      174040 :     L_ener_tot = W_round64_L( W_ener );
     683      174040 :     L_ener_tot = L_max( L_ener_tot, 1 ); /* L_ener_tot is energy, it is always positive, but have to be > 0 for the following division */
     684      174040 :     L_ener = L_abs( L_sub( sp[1], sp[0] ) );
     685             : 
     686   111211560 :     FOR( i = 2; i < N; i++ )
     687             :     {
     688             :         /* Eq to (sp[i] - sp[i-1]) * (sp[i-1] - sp[i-2]) < 0 */
     689   111037520 :         tmp1 = L_sub( sp[i], sp[i - 1] );
     690   111037520 :         tmp2 = L_sub( sp[i - 1], sp[i - 2] );
     691             : 
     692   111037520 :         tmp2 = Mpy_32_32( tmp1, tmp2 );
     693   111037520 :         tmp1 = L_abs( tmp1 );
     694             :         /* to Get either 0 or -1 in 'tmp2' */
     695   111037520 :         tmp2 = L_shr( tmp2, 31 );
     696   111037520 :         sign = extract_l( L_shl( tmp2, Q15 ) );
     697             :         /* this allows this code */
     698   111037520 :         L_ener = Msub_32_16( L_ener, tmp1, sign );
     699             :         /* instead of this one */
     700             :         /* test(); */
     701             :         /* if (tmp2 < 0) */
     702             :         /* { */
     703             :         /*     L_ener = L_mac0(L_ener, 1, tmp1); */
     704             :         /* } */
     705             :         /* it saves one op */
     706             :     }
     707             : 
     708      174040 :     L_temp = L_ener_tot;
     709      174040 :     move32();
     710      174040 :     tmp_exp = sub( Q31, sub( shl( exp_sp, 1 ), Q31 ) );
     711      174040 :     L_temp = Sqrt32( L_temp, &tmp_exp );
     712      174040 :     L_temp = L_shl( L_temp, sub( exp_sp, sub( Q31, tmp_exp ) ) );
     713             : 
     714             :     /* *tilt_flt = (float)(r1/sqrt(r0)); */
     715      174040 :     L_temp = L_deposit_l( BASOP_Util_Divide3232_Scale( L_ener, L_temp, &exp2 ) );
     716      174040 :     IF( GT_16( sub( exp2, 4 ), norm_l( L_temp ) ) )
     717             :     {
     718       13553 :         L_temp = MAX_16;
     719       13553 :         move32();
     720             :     }
     721             :     ELSE
     722             :     {
     723      160487 :         L_temp = L_shl( L_temp, sub( exp2, 4 ) ); // Output is expected in Q11
     724             :     }
     725             :     BASOP_SATURATE_WARNING_ON_EVS
     726      174040 :     return extract_h( L_shl_sat( L_temp, 16 ) );
     727             : }
     728             : 
     729             : /*-------------------------------------------------------------------*
     730             :  * calc_norm_envelop()
     731             :  *
     732             :  * calculate normalized parameter
     733             :  *-------------------------------------------------------------------*/
     734             : 
     735       20727 : void calc_norm_envelop_fx(
     736             :     const Word16 SWB_signal[], /* i  : SWB spectrum            Q_syn*/
     737             :     Word32 *envelope,          /* o  : normalized envelope     Q_syn*/
     738             :     const Word16 L_swb_norm,   /* i  : length of envelope      Q0   */
     739             :     const Word16 SWB_flength,  /* i  : Length of input/output       */
     740             :     const Word16 st_offset     /* i  : offset                       */
     741             : )
     742             : {
     743             :     Word16 i, lookback, env_index, n_freq, n_lag_now, n_lag, tmp;
     744             : 
     745       20727 :     lookback = shr( L_swb_norm, 1 );
     746       20727 :     env_index = add( swb_bwe_subband[0], st_offset );
     747       20727 :     n_lag_now = L_swb_norm;
     748       20727 :     move16();
     749       20727 :     tmp = sub( add( SWB_flength, st_offset ), L_swb_norm );
     750     4409265 :     FOR( n_freq = sub( add( swb_bwe_trans_subband[0], st_offset ), lookback ); n_freq < tmp; n_freq++ )
     751             :     {
     752             :         /* Apply MA filter */
     753     4388538 :         Word32 L_tmp = L_deposit_l( 0 );
     754    44289543 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
     755             :         {
     756    39901005 :             L_tmp = L_add( L_tmp, abs_s( SWB_signal[n_freq + n_lag] ) );
     757             :         }
     758     4388538 :         envelope[env_index] = L_tmp;
     759     4388538 :         move32();
     760     4388538 :         env_index++;
     761             :     }
     762             : 
     763       20727 :     i = 0;
     764       20727 :     move16();
     765       20727 :     tmp = sub( add( SWB_flength, st_offset ), lookback );
     766      137469 :     FOR( n_freq = sub( add( SWB_flength, st_offset ), L_swb_norm ); n_freq < tmp; n_freq++ )
     767             :     {
     768             :         Word32 L_tmp;
     769             : 
     770      116742 :         n_lag_now = sub( L_swb_norm, i );
     771             :         /* Apply MA filter */
     772      116742 :         L_tmp = L_deposit_l( 0 );
     773     1849895 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
     774             :         {
     775     1733153 :             L_tmp = L_add( L_tmp, abs_s( SWB_signal[n_freq + n_lag] ) );
     776             :         }
     777      116742 :         envelope[env_index] = L_tmp;
     778      116742 :         move32();
     779      116742 :         env_index++;
     780      116742 :         i = add( i, 1 );
     781             :     }
     782             : 
     783       20727 :     return;
     784             : }
     785             : 
     786             : /*==========================================================================*/
     787             : /* FUNCTION      : void WB_BWE_decoding_fx ()                               */
     788             : /*--------------------------------------------------------------------------*/
     789             : /* PURPOSE       : WB BWE decoder                                           */
     790             : /*--------------------------------------------------------------------------*/
     791             : /* INPUT ARGUMENTS  :                                                       */
     792             : /* _Word16 *core_dec_freq     i  : Frequency domain core decoded signal Q_syn*/
     793             : /* _Word16 *WB_fenv       i  : WB frequency envelopes   Q3                  */
     794             : /* _Word16 WB_flength         i  : Length of input/output                   */
     795             : /* _Word16 mode               i  : classification for WB signal             */
     796             : /* _Word16 prev_mode          i  : classification for last frame            */
     797             : /* _Word16 last_extl          i  : extl. layer for last frame               */
     798             : /* _Word16 extl               i  : extension layer                          */
     799             : /* _Word16 coder_type         i  : coding type                              */
     800             : /* _Word16 total_brate        i  : core layer bitrate                       */
     801             : /* _Word16 prev_coder_type    i  : coding type of last frame                */
     802             : /* _Word16 Q_syn        i  : Q format                                       */
     803             : /*--------------------------------------------------------------------------*/
     804             : /* OUTPUT ARGUMENTS :                                                       */
     805             : /* _Word16 *WB_signal,         o  : WB signal in MDCT domain          Q_syn */
     806             : /*--------------------------------------------------------------------------*/
     807             : /* INPUT/OUTPUT ARGUMENTS :                         */
     808             : /* _Word32 *prev_Energy,       i/o: energy for last frame           Q_syn   */
     809             : /* _Word16 *prev_WB_fenv,      i/o: envelope for last frame         Q3      */
     810             : /* _Word16 *prev_L_wb_norm,    i/o: length for last frame wb norm   Q0      */
     811             : /* _Word16 *Seed,              i/o: random generator seed           Q15     */
     812             : /* _Word16 *prev_flag,         i/o: attenu flag of last frame        Q0     */
     813             : /*--------------------------------------------------------------------------*/
     814             : /* RETURN ARGUMENTS :                           */
     815             : /*           _ None                         */
     816             : /*--------------------------------------------------------------------------*/
     817             : /* CALLED FROM :                              */
     818             : /*==========================================================================*/
     819        8864 : void WB_BWE_decoding_fx(
     820             :     const Word16 *core_dec_freq, /* i  : Frequency domain core decoded signal */
     821             :     Word16 *WB_fenv,             /* i  : WB frequency envelopes               */
     822             :     Word32 *WB_signal_32,        /* o  : WB signal in MDCT domain             */
     823             :     const Word16 WB_flength,     /* i  : Length of input/output               */
     824             :     const Word16 mode,           /* i  : classification for WB signal         */
     825             :     const Word16 last_extl,      /* i  : extl. layer for last frame           */
     826             :     Word32 *prev_Energy,         /* i/o: energy for last frame                */
     827             :     Word16 *prev_WB_fenv,        /* i/o: envelope for last frame              */
     828             :     Word16 *prev_L_wb_norm,      /* i/o: length for last frame wb norm        */
     829             :     const Word16 extl,           /* i  : extension layer                      */
     830             :     const Word16 coder_type,     /* i  : coding type                          */
     831             :     const Word32 total_brate,    /* i  : core layer bitrate                   */
     832             :     Word16 *Seed,                /* i/o: random generator seed                */
     833             :     Word16 *prev_flag,           /* i/o: attenu flag of last frame            */
     834             :     Word16 prev_coder_type,      /* i  : coding type of last frame            */
     835             :     Word16 Q_syn,
     836             :     Word16 *Q_syn_hb /*o   : Q value of WB_signal_32              */
     837             : )
     838             : {
     839             :     Word16 n_freq, n_band;
     840             :     Word16 i, L;
     841             :     Word32 envelope[L_FRAME16k];
     842             :     Word32 energy, EnergyL;
     843             :     Word32 *pit1;
     844             :     Word16 WB_signal[L_FRAME16k];
     845             :     Word16 L_wb_norm, wfenv[2];
     846             :     Word16 alfa, beta;
     847        8864 :     Word16 flag = 0;
     848        8864 :     Word16 core_type = 1;
     849        8864 :     move16();
     850        8864 :     move16();
     851             :     Word16 tmp, tmp1, exp, tmp2;
     852             :     Word32 L_tmp, L_tmp1, L_tmp2;
     853             :     Word32 prev_ener_alpha, prev_ener_beta;
     854             :     Word16 signum[L_FRAME16k];
     855             :     Word16 inv_L_wb_norm, weight;
     856             : 
     857        8864 :     calc_normal_length_fx( ACELP_CORE, core_dec_freq, mode, extl, &L_wb_norm, prev_L_wb_norm, Q_syn );
     858        8864 :     set32_fx( WB_signal_32, 0, L_FRAME16k );
     859        8864 :     set16_fx( WB_signal, 0, L_FRAME16k );
     860             : 
     861             :     /* copy excitation */
     862        8864 :     test();
     863        8864 :     if ( NE_16( coder_type, AUDIO ) && LE_32( total_brate, ACELP_8k00 ) )
     864             :     {
     865        4052 :         core_type = 0;
     866        4052 :         move16();
     867             :     }
     868             : 
     869        8864 :     IF( core_type == 0 )
     870             :     {
     871        4052 :         Copy( &core_dec_freq[160], &WB_signal[240], 80 );
     872             :     }
     873             :     ELSE
     874             :     {
     875        4812 :         Copy( &core_dec_freq[80], &WB_signal[240], 80 );
     876             :     }
     877             : 
     878             :     /* calculate envelope */
     879        8864 :     calc_norm_envelop_fx( WB_signal, envelope, L_wb_norm, WB_flength, 0 );
     880        8864 :     test();
     881        8864 :     IF( coder_type != UNVOICED && total_brate <= ACELP_8k00 )
     882             :     {
     883        7717 :         exp = norm_s( L_wb_norm );
     884        7717 :         inv_L_wb_norm = shl( div_s( shl( 1, sub( 14, exp ) ), L_wb_norm ), sub( exp, 14 ) ); /* Q(15) */
     885        7717 :         IF( NE_16( mode, HARMONIC ) )
     886             :         {
     887        6251 :             tmp = add( shl( inv_L_wb_norm, 1 ), inv_L_wb_norm );
     888        6251 :             weight = s_max( s_min( tmp, 16384 /* 0.5 in Q15 */ ), 8192 /*0.25 in Q15 */ );
     889             :         }
     890             :         ELSE
     891             :         {
     892        1466 :             weight = 8192; /*0.25 in Q15 */
     893        1466 :             move16();
     894             :         }
     895      625077 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
     896             :         {
     897      617360 :             signum[n_freq] = 1;
     898      617360 :             move16();
     899      617360 :             IF( WB_signal[n_freq] < 0 )
     900             :             {
     901      308001 :                 signum[n_freq] = -1;
     902      308001 :                 move16();
     903      308001 :                 WB_signal[n_freq] = negate( WB_signal[n_freq] );
     904      308001 :                 move16();
     905             :             }
     906      617360 :             L_tmp = Mult_32_16( envelope[n_freq], inv_L_wb_norm ); /* Q_syn */
     907      617360 :             L_tmp = Mult_32_16( L_tmp, 14746 );                    /* Q_syn */
     908      617360 :             L_tmp1 = L_deposit_l( WB_signal[n_freq] );             /* Q_syn */
     909      617360 :             L_tmp = L_sub( L_tmp1, L_tmp );                        /* Q_syn */
     910      617360 :             WB_signal[n_freq] = extract_l( L_tmp );                /* Q_syn */
     911      617360 :             move16();
     912      617360 :             IF( L_tmp > 0 )
     913             :             {
     914      453711 :                 tmp = sub( 18022, weight );                                /* Q15 */
     915      453711 :                 WB_signal[n_freq] = extract_l( Mult_32_16( L_tmp, tmp ) ); /* Q_syn */
     916      453711 :                 move16();
     917             :             }
     918             : 
     919      617360 :             IF( NE_16( signum[n_freq], 1 ) )
     920             :             {
     921      308001 :                 WB_signal[n_freq] = negate( WB_signal[n_freq] );
     922      308001 :                 move16();
     923             :             }
     924             :         }
     925             :     }
     926             : 
     927             :     /* Normalize with envelope */
     928      717984 :     FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
     929             :     {
     930      709120 :         IF( envelope[n_freq] != 0 )
     931             :         {
     932      709120 :             exp = norm_l( envelope[n_freq] );
     933      709120 :             tmp = extract_h( L_shl( envelope[n_freq], exp ) );
     934      709120 :             exp = sub( sub( 30, exp ), Q_syn );
     935      709120 :             tmp = div_s( 16384, tmp );
     936      709120 :             L_tmp = L_shr( L_mult0( WB_signal[n_freq], tmp ), add( exp, Q_syn ) ); /*Q15 */
     937      709120 :             WB_signal[n_freq] = extract_l( L_tmp );                                /*Q15 */
     938      709120 :             move16();
     939             :         }
     940             :         ELSE
     941             :         {
     942           0 :             WB_signal[n_freq] = shl( WB_signal[n_freq], sub( 15, Q_syn ) ); /*Q15 */
     943           0 :             move16();
     944             :         }
     945             :     }
     946             : 
     947        8864 :     L = 1;
     948        8864 :     move16();
     949        8864 :     if ( EQ_16( mode, HARMONIC ) )
     950             :     {
     951        1468 :         L = 4;
     952        1468 :         move16();
     953             :     }
     954             : 
     955        8864 :     IF( EQ_16( coder_type, UNVOICED ) )
     956             :     {
     957        2430 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
     958             :         {
     959        2400 :             *Seed = extract_l( L_mac0( 20101L, *Seed, 12345 ) );
     960        2400 :             L_tmp = L_shl( *Seed, add( Q_syn, 1 ) );
     961        2400 :             WB_signal_32[n_freq] = L_tmp;
     962        2400 :             move32();
     963             :         }
     964             :     }
     965             :     ELSE
     966             :     {
     967       39766 :         FOR( n_band = 0; n_band < 4; n_band += L )
     968             :         {
     969       30932 :             energy = L_deposit_l( 0 );
     970      737652 :             FOR( n_freq = swb_bwe_subband[n_band]; n_freq < swb_bwe_subband[n_band + L]; n_freq++ )
     971             :             {
     972      706720 :                 L_tmp = L_mult0( WB_signal[n_freq], WB_signal[n_freq] ); /*Q30 */
     973      706720 :                 energy = L_add( energy, L_shr( L_tmp, 5 ) );             /*Q25 */
     974             :             }
     975             : 
     976       30932 :             tmp = sub( swb_bwe_subband[n_band + L], swb_bwe_subband[n_band] );
     977       30932 :             tmp = div_s( 1, tmp );              /*Q15 */
     978       30932 :             energy = Mult_32_16( energy, tmp ); /*Q(15+25-15)->Q(25) */
     979             : 
     980       30932 :             exp = norm_l( energy );
     981       30932 :             L_tmp1 = L_shl( energy, exp );
     982       30932 :             exp = 31 - exp - ( 25 );
     983       30932 :             move16();
     984       30932 :             L_tmp1 = Isqrt_lc( L_tmp1, &exp ); /*Q(31-exp) */
     985             : 
     986      737652 :             FOR( n_freq = swb_bwe_subband[n_band]; n_freq < swb_bwe_subband[n_band + L]; n_freq++ )
     987             :             {
     988      706720 :                 L_tmp2 = Mult_32_16( L_tmp1, WB_signal[n_freq] );
     989      706720 :                 WB_signal_32[n_freq] = L_shl( L_tmp2, sub( add( exp, Q_syn ), 15 ) );
     990      706720 :                 move32();
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995        8864 :     EnergyL = L_deposit_l( 0 );
     996        8864 :     IF( EQ_16( core_type, 1 ) )
     997             :     {
     998        4812 :         test();
     999        4812 :         IF( NE_16( prev_coder_type, AUDIO ) && LE_32( total_brate, ACELP_8k00 ) )
    1000             :         {
    1001        4860 :             FOR( i = 160; i < 240; i++ )
    1002             :             {
    1003        4800 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL );
    1004             :             }
    1005             :         }
    1006             :         ELSE
    1007             :         {
    1008      765072 :             FOR( i = 80; i < 240; i++ )
    1009             :             {
    1010      760320 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL );
    1011             :             }
    1012             :         }
    1013             : 
    1014        4812 :         IF( LE_32( total_brate, ACELP_8k00 ) )
    1015             :         {
    1016        3677 :             alfa = 26214;
    1017        3677 :             move16(); /*0.8f in Q15; */
    1018        3677 :             beta = 10240;
    1019        3677 :             move16(); /*1.25f in Q13; */
    1020             :         }
    1021             :         ELSE
    1022             :         {
    1023        1135 :             alfa = 16384;
    1024        1135 :             move16(); /* 0.5f in Q15; */
    1025        1135 :             beta = 16384;
    1026        1135 :             move16(); /*2.0f in Q13 */
    1027             :         }
    1028             :     }
    1029             :     ELSE
    1030             :     {
    1031        4052 :         IF( EQ_16( prev_coder_type, AUDIO ) )
    1032             :         {
    1033         644 :             FOR( i = 80; i < 240; i++ )
    1034             :             {
    1035         640 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL ); /*Q_syn */
    1036             :             }
    1037             :         }
    1038             :         ELSE
    1039             :         {
    1040      327888 :             FOR( i = 160; i < 240; i++ )
    1041             :             {
    1042      323840 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL );
    1043             :             }
    1044             :         }
    1045             : 
    1046        4052 :         test();
    1047        4052 :         IF( EQ_16( prev_coder_type, coder_type ) && GT_16( WB_fenv[0], prev_WB_fenv[0] ) )
    1048             :         {
    1049         364 :             alfa = 13107;
    1050         364 :             move16(); /*.4 in Q15 */
    1051         364 :             beta = 20480;
    1052         364 :             move16(); /*2.5 in Q13 */
    1053             :         }
    1054             :         ELSE
    1055             :         {
    1056        3688 :             alfa = 19661;
    1057        3688 :             move16(); /*.6 in Q15 */
    1058        3688 :             beta = 13681;
    1059        3688 :             move16(); /*1.67 in Q13 */
    1060             :         }
    1061             : 
    1062        4052 :         test();
    1063        4052 :         test();
    1064        4052 :         test();
    1065        4052 :         IF( EQ_16( coder_type, GENERIC ) || ( GT_32( EnergyL, L_shr( *prev_Energy, 1 ) ) && GT_32( *prev_Energy, L_shr( EnergyL, 1 ) ) && EQ_16( *prev_flag, 1 ) ) )
    1066             :         {
    1067          56 :             WB_fenv[0] = shr( WB_fenv[0], 1 );
    1068          56 :             move16();
    1069          56 :             WB_fenv[1] = shr( WB_fenv[1], 1 );
    1070          56 :             move16();
    1071          56 :             flag = 1;
    1072          56 :             move16();
    1073             :         }
    1074             :     }
    1075        8864 :     L_tmp1 = Mult_32_16( EnergyL, prev_WB_fenv[0] );               /*Qsyn+3-15 */
    1076        8864 :     L_tmp2 = Mult_32_16( *prev_Energy, WB_fenv[0] );               /*Q_syn+3-15 */
    1077        8864 :     prev_ener_alpha = Mult_32_16( *prev_Energy, alfa );            /*Q_syn+15-15->Q_syn */
    1078        8864 :     prev_ener_beta = L_shl( Mult_32_16( *prev_Energy, beta ), 2 ); /*Q_syn+13-15+2 ->Q_syn */
    1079             : 
    1080        8864 :     test();
    1081        8864 :     test();
    1082        8864 :     IF( ( EQ_16( mode, HARMONIC ) && GT_16( shr( WB_fenv[0], 2 ), WB_fenv[1] ) ) || EQ_16( mode, NORMAL ) )
    1083             :     {
    1084        7420 :         test();
    1085        7420 :         test();
    1086        7420 :         test();
    1087        7420 :         test();
    1088        7420 :         test();
    1089        7420 :         test();
    1090        7420 :         test();
    1091        7420 :         test();
    1092        7420 :         test();
    1093        7420 :         test();
    1094        7420 :         test();
    1095        7420 :         test();
    1096        7420 :         test();
    1097        7420 :         IF( EQ_16( last_extl, WB_BWE ) &&
    1098             :             ( ( EQ_16( prev_coder_type, AUDIO ) && NE_16( coder_type, AUDIO ) ) ||
    1099             :               ( NE_16( prev_coder_type, AUDIO ) && EQ_16( coder_type, AUDIO ) ) ) &&
    1100             :             LE_32( total_brate, ACELP_8k00 ) )
    1101             :         {
    1102           8 :             IF( GT_16( WB_fenv[0], prev_WB_fenv[0] ) )
    1103             :             {
    1104             :                 /*wfenv[0]= add(mult_r(9830, WB_fenv[0]), mult_r(22938, prev_WB_fenv[0]));//Q3 */
    1105           4 :                 wfenv[0] = round_fx( L_mac( L_mult( 9830, WB_fenv[0] ), 22938, prev_WB_fenv[0] ) ); /*Q3 */
    1106           4 :                 move16();
    1107             :                 /*wfenv[1]= add(mult_r(9830, WB_fenv[1]), mult_r(22938, prev_WB_fenv[1]));//Q3 */
    1108           4 :                 wfenv[1] = round_fx( L_mac( L_mult( 9830, WB_fenv[1] ), 22938, prev_WB_fenv[1] ) ); /*Q3 */
    1109           4 :                 move16();
    1110             :             }
    1111             :             ELSE
    1112             :             {
    1113             :                 /*wfenv[0]= add(mult_r(16384,WB_fenv[0]),mult_r(16384,prev_WB_fenv[0]));//Q3 */
    1114           4 :                 wfenv[0] = round_fx( L_mac( L_mult( 16384, WB_fenv[0] ), 16384, prev_WB_fenv[0] ) ); /*Q3 */
    1115           4 :                 move16();
    1116             :                 /*wfenv[1]= add(mult_r(13108,WB_fenv[1]),mult_r(13108,prev_WB_fenv[1]));//Q3 */
    1117           4 :                 wfenv[1] = round_fx( L_mac( L_mult( 13108, WB_fenv[1] ), 13108, prev_WB_fenv[1] ) ); /*Q3 */
    1118           4 :                 move16();
    1119             :             }
    1120             :         }
    1121        7412 :         ELSE IF( EQ_16( last_extl, WB_BWE ) && LT_32( L_tmp1, L_tmp2 ) && GT_16( WB_fenv[0], prev_WB_fenv[0] ) &&
    1122             :                  NE_16( coder_type, AUDIO ) && NE_16( coder_type, UNVOICED ) && LE_32( total_brate, ACELP_8k00 ) )
    1123             :         {
    1124             :             /*wfenv[0]= add(mult_r(9830,WB_fenv[0]),mult_r(22938,prev_WB_fenv[0]));//Q3 */
    1125          56 :             wfenv[0] = round_fx( L_mac( L_mult( 9830, WB_fenv[0] ), 22938, prev_WB_fenv[0] ) ); /*Q3 */
    1126          56 :             move16();
    1127             :             /*wfenv[1]= add(mult_r(9830,WB_fenv[1]),mult_r(22938,prev_WB_fenv[1]));//Q3 */
    1128          56 :             wfenv[1] = round_fx( L_mac( L_mult( 9830, WB_fenv[1] ), 22938, prev_WB_fenv[1] ) ); /*Q3 */
    1129          56 :             move16();
    1130             :         }
    1131        7356 :         ELSE IF( EQ_16( last_extl, WB_BWE ) && GT_32( EnergyL, prev_ener_alpha ) && GT_32( prev_ener_beta, EnergyL ) &&
    1132             :                  NE_16( prev_coder_type, UNVOICED ) )
    1133             :         {
    1134             :             /*wfenv[0] = add(shr(WB_fenv[0],1), shr(prev_WB_fenv[0],1));//Q3 */
    1135        5955 :             wfenv[0] = round_fx( L_mac( L_mult( WB_fenv[0], 16384 ), prev_WB_fenv[0], 16384 ) ); /*Q3 */
    1136        5955 :             move16();
    1137             :             /*wfenv[1] = add(shr(WB_fenv[1],1), shr(prev_WB_fenv[1],1));//Q3 */
    1138        5955 :             wfenv[1] = round_fx( L_mac( L_mult( WB_fenv[1], 16384 ), prev_WB_fenv[1], 16384 ) ); /*Q3 */
    1139        5955 :             move16();
    1140             :         }
    1141             :         ELSE
    1142             :         {
    1143        1401 :             wfenv[0] = WB_fenv[0];
    1144        1401 :             move16();
    1145        1401 :             wfenv[1] = WB_fenv[1];
    1146        1401 :             move16();
    1147             :         }
    1148      304220 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[2]; n_freq++ )
    1149             :         {
    1150      296800 :             WB_signal_32[n_freq] = Mult_32_16( WB_signal_32[n_freq], wfenv[0] );
    1151      296800 :             move32(); /* Q_syn+3+1 */
    1152             :         }
    1153             : 
    1154      304220 :         FOR( n_freq = swb_bwe_subband[2]; n_freq < swb_bwe_subband[4]; n_freq++ )
    1155             :         {
    1156      296800 :             WB_signal_32[n_freq] = Mult_32_16( WB_signal_32[n_freq], wfenv[1] );
    1157      296800 :             move32(); /* Q_syn+3+1 */
    1158             :         }
    1159             : 
    1160        7420 :         prev_WB_fenv[0] = wfenv[0];
    1161        7420 :         move16();
    1162        7420 :         prev_WB_fenv[1] = wfenv[1];
    1163        7420 :         move16();
    1164             :     }
    1165             :     ELSE
    1166             :     {
    1167        1444 :         wfenv[0] = add( shr( WB_fenv[0], 1 ), shr( WB_fenv[1], 1 ) ); /*Q3 */
    1168             : 
    1169        1444 :         test();
    1170        1444 :         test();
    1171        1444 :         IF( EQ_16( last_extl, WB_BWE ) && GT_32( EnergyL, L_shr( *prev_Energy, 1 ) ) && GT_32( *prev_Energy, L_shr( EnergyL, 1 ) ) )
    1172             :         {
    1173        1400 :             L_tmp1 = L_mac( L_mult( 8192 /* 0.25 in Q15 */, wfenv[0] ), 12288 /* 0.375 in Q15 */, prev_WB_fenv[0] ); /* Q3 */
    1174        1400 :             wfenv[0] = round_fx( L_mac( L_tmp1, 12288 /* 0.375 in Q15 */, prev_WB_fenv[1] ) );                       /* Q3 */
    1175             :         }
    1176      116964 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
    1177             :         {
    1178      115520 :             WB_signal_32[n_freq] = Mult_32_16( WB_signal_32[n_freq], wfenv[0] );
    1179      115520 :             move32(); /* Q_syn+3+1 */
    1180             :         }
    1181        1444 :         prev_WB_fenv[0] = wfenv[0]; /* Q3 */
    1182        1444 :         move16();
    1183        1444 :         prev_WB_fenv[1] = wfenv[0]; /* Q3 */
    1184        1444 :         move16();
    1185             :     }
    1186             : 
    1187        8864 :     *prev_flag = flag;
    1188        8864 :     move16();
    1189        8864 :     *prev_Energy = EnergyL;
    1190        8864 :     move32();
    1191        8864 :     pit1 = &WB_signal_32[240];
    1192             : 
    1193      150688 :     FOR( n_freq = 0; n_freq < 16; n_freq++ )
    1194             :     {
    1195      141824 :         tmp1 = extract_l( L_mult0( n_freq, 1638 /* 0.05 in Q15 */ ) ); /*Q15 */
    1196      141824 :         tmp2 = add( 6554 /* 0.2 in Q15 */, tmp1 );                     /*Q15 */
    1197      141824 :         L_tmp1 = Mult_32_16( *pit1, tmp2 );                            /*Q_syn+3+1 */
    1198      141824 :         *( pit1++ ) = L_tmp1;
    1199      141824 :         move32();
    1200             :     }
    1201             : 
    1202        8864 :     IF( EQ_16( core_type, 1 ) )
    1203             :     {
    1204        4812 :         pit1 = &WB_signal_32[280];
    1205      197292 :         FOR( n_freq = 0; n_freq < 40; n_freq++ )
    1206             :         {
    1207      192480 :             tmp1 = extract_l( L_mult0( n_freq, 655 /* 0.02 in Q15 */ ) ); /*Q15 */
    1208      192480 :             tmp2 = sub( 32767, tmp1 );
    1209      192480 :             L_tmp1 = Mult_32_16( *pit1, tmp2 ); /*Q_syn+3+1 */
    1210      192480 :             *( pit1++ ) = L_tmp1;
    1211      192480 :             move32();
    1212             :         }
    1213             :     }
    1214             :     ELSE
    1215             :     {
    1216        4052 :         pit1 = &WB_signal_32[300];
    1217       85092 :         FOR( n_freq = 0; n_freq < 20; n_freq++ )
    1218             :         {
    1219       81040 :             tmp1 = extract_l( L_mult0( n_freq, 1311 /* 0.04 in Q15 */ ) ); /*Q15 */
    1220       81040 :             tmp2 = sub( 32767, tmp1 );
    1221       81040 :             L_tmp1 = Mult_32_16( *pit1, tmp2 ); /*Q_syn+3+1 */
    1222       81040 :             *( pit1++ ) = L_tmp1;
    1223       81040 :             move32();
    1224             :         }
    1225             :     }
    1226        8864 :     pit1 = &WB_signal_32[240];
    1227        8864 :     tmp = Find_Max_Norm32( pit1, 80 );
    1228      717984 :     FOR( i = 0; i < 80; i++ )
    1229             :     {
    1230      709120 :         L_tmp = *pit1;
    1231      709120 :         *( pit1++ ) = L_shl( L_tmp, sub( tmp, 1 ) );
    1232      709120 :         move32();
    1233             :     }
    1234        8864 :     *Q_syn_hb = add( Q_syn, add( tmp, 3 ) );
    1235        8864 :     move16();
    1236        8864 :     return;
    1237             : }
    1238             : 
    1239             : 
    1240             : /*==========================================================================*/
    1241             : /* FUNCTION      : void SWB_BWE_decoding_fx()                               */
    1242             : /*--------------------------------------------------------------------------*/
    1243             : /* PURPOSE       :    SWB BWE decoder                                       */
    1244             : /*--------------------------------------------------------------------------*/
    1245             : /* INPUT ARGUMENTS                                                          */
    1246             : /* _(Word16*) core_dec_freq     :Frequency domain core decoded signal Q_syn */
    1247             : /* _(Word16) SWB_flength    :Length of input/output         Q0              */
    1248             : /* _(Word16) mode         :classification for SWB signal                    */
    1249             : /* _(Word16) tilt_nb      :tilt of synthesis wb signal      Q11             */
    1250             : /* _(Word16) st_offset      :offset value due to different core             */
    1251             : /* _(Word16) Q_syn          :Q format                                       */
    1252             : /*--------------------------------------------------------------------------*/
    1253             : /* OUTPUT ARGUMENTS :                                                       */
    1254             : /*  _(Word16*)SWB_fenv        : SWB frequency envelopes         Q1          */
    1255             : /*  _(Word16*)prev_Energy     : energy for last frame           Q3          */
    1256             : /*  _(Word16*)prev_SWB_fenv   : envelope for last frame         Q1          */
    1257             : /*  _(Word16*)prev_L_swb_norm : length for last frame wb norm   Q0          */
    1258             : /*  _(Word16*)Seed        : random generator seed           Q0              */
    1259             : /*--------------------------------------------------------------------------*/
    1260             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
    1261             : /*  _(Word16*)SWB_signal    : SWB signal in MDCT domain       Q0            */
    1262             : /*  _(Word16*)frica_flag    : fricative signal flag           Q0            */
    1263             : /*--------------------------------------------------------------------------*/
    1264             : /* RETURN ARGUMENTS :                                                       */
    1265             : /*   _ None                                                                 */
    1266             : /*--------------------------------------------------------------------------*/
    1267             : 
    1268       12655 : void SWB_BWE_decoding_fx(
    1269             :     const Word16 *core_dec_freq, /* i  : Frequency domain core decoded signal  */
    1270             :     Word16 *SWB_fenv,            /* i/o: SWB frequency envelopes               */
    1271             :     Word32 *SWB_signal_32,       /* o  : SWB signal in MDCT domain             */
    1272             :     const Word16 SWB_flength,    /* i  : Length of input/output                */
    1273             :     const Word16 mode,           /* i  : classification for SWB signal         */
    1274             :     Word16 *frica_flag,          /* o  : fricative signal flag                 */
    1275             :     Word16 *prev_Energy,         /* i/o: energy for last frame                 */
    1276             :     Word16 *prev_SWB_fenv,       /* i/o: envelope for last frame               */
    1277             :     Word16 *prev_L_swb_norm,     /* i/o: length for last frame wb norm         */
    1278             :     const Word16 tilt_nb,        /* i  : tilt of synthesis wb signal           */
    1279             :     Word16 *Seed,                /* i/o: random generator seed                 */
    1280             :     const Word16 st_offset,      /* i  : offset value due to different core    */
    1281             :     Word16 *prev_weight,         /* i/o: excitation weight value of last frame */
    1282             :     const Word16 extl,           /* i  : extension layer                       */
    1283             :     Word16 Q_syn,
    1284             :     const Word16 last_extl /* i  : extension layer of last frame         */
    1285             : )
    1286             : {
    1287             :     Word16 n_freq, n_band, L, L_swb_norm;
    1288             :     Word32 *pit1_32;
    1289             :     Word16 *pit1;
    1290             :     Word32 envelope[L_FRAME32k];
    1291             :     Word32 fenvL, EnergyL, Energy, energy, L_mean;
    1292             :     Word16 fenvL_16, EnergyL_16, Energy_16, tmp, exp, exp1;
    1293             :     Word16 SWB_signal[L_FRAME32k];
    1294             :     Word16 mean, factor, factor1, tmp1, tmp2, tmp3, tmp_exp, tmp_ener, weight, wfenv;
    1295             :     Word32 L_tmp, L_tmp3, L_tmp4, Ltmp_ener, L_tmp1;
    1296             :     Word32 L_energy;
    1297             :     Word16 signum[L_FRAME32k];
    1298             :     Word16 inv_L_swb_norm;
    1299             : 
    1300       12655 :     fenvL = L_deposit_l( 0 );
    1301       12655 :     EnergyL = L_deposit_l( 0 );
    1302      215135 :     FOR( n_freq = 224 + st_offset; n_freq < swb_bwe_trans_subband[0] + st_offset; n_freq++ )
    1303             :     {
    1304      202480 :         fenvL = L_mac0_sat( fenvL, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
    1305             :     }
    1306             : 
    1307     3049855 :     FOR( n_freq = 16; n_freq < L_FRAME; n_freq++ )
    1308             :     {
    1309     3037200 :         EnergyL = L_mac0_sat( EnergyL, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
    1310             :     }
    1311             : 
    1312       12655 :     fenvL_16 = 0;
    1313       12655 :     move16();
    1314       12655 :     IF( fenvL != 0 )
    1315             :     {
    1316       12642 :         exp = norm_l( fenvL ); /* In 2*Q_syn */
    1317       12642 :         tmp = extract_h( L_shl( fenvL, exp ) );
    1318       12642 :         exp = sub( exp, sub( 30, add( shl( Q_syn, 1 ), 4 ) ) ); /*+4(/16) */
    1319             : 
    1320       12642 :         tmp = div_s( 16384, tmp );
    1321       12642 :         L_tmp = L_deposit_h( tmp );
    1322       12642 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    1323             : 
    1324       12642 :         fenvL_16 = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /* Q3 */
    1325             :     }
    1326       12655 :     IF( GT_16( fenvL_16, shl_sat( SWB_fenv[0], 5 ) ) )
    1327             :     {
    1328        2162 :         fenvL_16 = shl( SWB_fenv[0], 2 );
    1329        2162 :         move16();
    1330             :     }
    1331       12655 :     EnergyL = Mult_32_16( EnergyL, 17476 ); /*2*Q_syn+3; 17476=(1/15) in Q18 */
    1332       12655 :     EnergyL_16 = 0;
    1333       12655 :     move16();
    1334       12655 :     IF( EnergyL != 0 )
    1335             :     {
    1336       12655 :         exp = norm_l( EnergyL ); /* In 2*Q_syn+3 */
    1337       12655 :         tmp = extract_h( L_shl( EnergyL, exp ) );
    1338       12655 :         exp = sub( exp, sub( 30, add( shl( Q_syn, 1 ), 3 + 4 ) ) ); /*+4(/16) */
    1339             : 
    1340       12655 :         tmp = div_s( 16384, tmp );
    1341       12655 :         L_tmp = L_deposit_h( tmp );
    1342       12655 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    1343             : 
    1344       12655 :         EnergyL_16 = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /* Q3 */
    1345             :     }
    1346       12655 :     calc_normal_length_fx( ACELP_CORE, core_dec_freq, mode, extl, &L_swb_norm, prev_L_swb_norm, Q_syn );
    1347             : 
    1348       12655 :     set16_fx( SWB_signal, 0, L_FRAME32k );
    1349       12655 :     IF( EQ_16( mode, TRANSIENT ) )
    1350             :     {
    1351          63 :         Energy = L_deposit_l( 0 );
    1352         315 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1353             :         {
    1354         252 :             Energy = L_mac( Energy, SWB_fenv[n_band], SWB_fenv[n_band] ); /*Q(2*1+1)->Q3 */
    1355             :         }
    1356          63 :         exp = norm_s( SWB_FENV_TRANS );
    1357          63 :         tmp = div_s( shl( 1, sub( 14, exp ) ), SWB_FENV_TRANS ); /*Q(29-exp) */
    1358          63 :         L_tmp = Mult_32_16( Energy, tmp );                       /*Q(3+29-exp+1-16)->Q(17-exp) */
    1359          63 :         Energy_16 = round_fx( L_shl( L_tmp, add( exp, 2 ) ) );   /*Q3 */
    1360             : 
    1361             :         /* Reconstruct excitation from LF signal */
    1362          63 :         Copy( &core_dec_freq[112], &SWB_signal[240 + st_offset], 128 );
    1363          63 :         Copy( &core_dec_freq[112], &SWB_signal[368 + st_offset], 128 );
    1364          63 :         Copy( &core_dec_freq[176], &SWB_signal[496 + st_offset], 64 );
    1365             : 
    1366             :         /* calculate envelope */
    1367          63 :         calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, SWB_flength, st_offset );
    1368             : 
    1369             :         /* Normalize with envelope */
    1370          63 :         tmp_exp = sub( 15, Q_syn );
    1371       20223 :         FOR( n_freq = swb_bwe_trans_subband[0] + st_offset; n_freq < swb_bwe_trans_subband[SWB_FENV_TRANS] + st_offset; n_freq++ )
    1372             :         {
    1373       20160 :             IF( envelope[n_freq] != 0 )
    1374             :             {
    1375       20160 :                 exp = norm_l( envelope[n_freq] );
    1376       20160 :                 tmp = extract_h( L_shl( envelope[n_freq], exp ) );
    1377       20160 :                 exp = sub( sub( 30, exp ), Q_syn );
    1378       20160 :                 tmp = div_s( 16384 /* 0.5 in Q15 */, tmp );                             /*Q(15+exp) */
    1379       20160 :                 L_tmp = L_shr( L_mult0( SWB_signal[n_freq], tmp ), add( exp, Q_syn ) ); /*Q15 */
    1380       20160 :                 SWB_signal[n_freq] = extract_l( L_tmp );                                /*Q15 */
    1381       20160 :                 move16();
    1382             :             }
    1383             :             ELSE
    1384             :             {
    1385           0 :                 SWB_signal[n_freq] = shl( SWB_signal[n_freq], tmp_exp );
    1386           0 :                 move16(); /*Q15 */
    1387             :             }
    1388             :         }
    1389             : 
    1390         315 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1391             :         {
    1392         252 :             energy = L_deposit_l( 0 );
    1393         252 :             tmp = add( swb_bwe_trans_subband[n_band + 1], st_offset );
    1394       20412 :             FOR( n_freq = add( swb_bwe_trans_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1395             :             {
    1396       20160 :                 L_tmp = L_mult_sat( SWB_signal[n_freq], SWB_signal[n_freq] ); /*Q31 */
    1397       20160 :                 energy = L_add_sat( energy, L_shr( L_tmp, 6 ) );              /*Q25 */
    1398             :             }
    1399             : 
    1400         252 :             IF( energy == 0 )
    1401             :             {
    1402           0 :                 Ltmp_ener = L_mult( sqrt_swb_bwe_trans_subband_width_fx[n_band], SWB_fenv[n_band] ); /*Q13 */
    1403           0 :                 tmp = add( swb_bwe_trans_subband[n_band + 1], st_offset );
    1404           0 :                 FOR( n_freq = add( swb_bwe_trans_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1405             :                 {
    1406           0 :                     SWB_signal_32[n_freq] = L_shl( Mult_32_16( Ltmp_ener, SWB_signal[n_freq] ), 2 + Q_syn );
    1407           0 :                     move32(); /*15+Qsyn */
    1408             :                 }
    1409             :             }
    1410             :             ELSE
    1411             :             {
    1412         252 :                 exp1 = norm_s( swb_bwe_trans_subband_width[n_band] );
    1413         252 :                 tmp = div_s( shl( 1, sub( 14, exp1 ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-exp1) */
    1414         252 :                 energy = Mult_32_16( energy, tmp );                                            /*Q(29-exp1+25-15)->Q(-exp1+39) */
    1415             : 
    1416         252 :                 exp = norm_l( energy );
    1417         252 :                 L_tmp = L_shl( energy, exp );
    1418             :                 /*exp = 31-exp-(-exp1+39); */
    1419         252 :                 exp = sub( sub( exp1, exp ), 8 );
    1420         252 :                 L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    1421             : 
    1422         252 :                 Ltmp_ener = Mult_32_16( L_tmp, SWB_fenv[n_band] ); /*Q(31-exp+1+1-16)->Q(17-exp) */
    1423         252 :                 tmp = add( swb_bwe_trans_subband[n_band + 1], st_offset );
    1424         252 :                 tmp_exp = add( Q_syn, sub( exp, 2 ) );
    1425       20412 :                 FOR( n_freq = add( swb_bwe_trans_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1426             :                 {
    1427       20160 :                     SWB_signal_32[n_freq] = L_shl( Mult_32_16( Ltmp_ener, SWB_signal[n_freq] ), tmp_exp );
    1428       20160 :                     move32(); /*15+Qsyn */
    1429             :                 }
    1430             :             }
    1431             :         }
    1432             : 
    1433         567 :         FOR( n_band = 0; n_band < 8; n_band++ )
    1434             :         {
    1435         504 :             L_tmp = L_mult( SWB_fenv[n_band / 4], SWB_fenv[n_band / 4] ); /*Q3 */
    1436         504 :             prev_SWB_fenv[n_band] = round_fx( L_shl( L_tmp, 14 ) );       /*Q1 */
    1437         504 :             move16();
    1438             :         }
    1439             : 
    1440         441 :         FOR( n_band = 0; n_band < 6; n_band++ )
    1441             :         {
    1442         378 :             L_tmp = L_mult( SWB_fenv[2 + n_band / 3], SWB_fenv[2 + n_band / 3] ); /*Q3 */
    1443         378 :             prev_SWB_fenv[8 + n_band] = round_fx( L_shl( L_tmp, 14 ) );           /*Q1 */
    1444         378 :             move16();
    1445             :         }
    1446             : 
    1447          63 :         *prev_weight = 16384 /* 0.5 in Q15 */;
    1448          63 :         move16();
    1449             :     }
    1450             :     ELSE
    1451             :     {
    1452       12592 :         Energy_16 = 0;
    1453       12592 :         move16();
    1454       12592 :         L_energy = L_deposit_l( 0 );
    1455      188880 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1456             :         {
    1457      176288 :             L_energy = L_add( L_energy, SWB_fenv[n_band] ); /*Q1 */
    1458             :         }
    1459       12592 :         exp = norm_s( SWB_FENV );
    1460       12592 :         tmp = div_s( shl( 1, sub( 14, exp ) ), SWB_FENV ); /*Q(29-exp) */
    1461       12592 :         L_tmp = Mult_32_16( L_energy, tmp );               /*Q(1+29-exp+1)->Q(15-exp) */
    1462             : 
    1463       12592 :         Energy_16 = round_fx_sat( L_shl_sat( L_tmp, add( exp, 4 ) ) ); /* Q3 */
    1464             : 
    1465       12592 :         test();
    1466       12592 :         IF( NE_16( last_extl, SWB_BWE ) && NE_16( last_extl, FB_BWE ) )
    1467             :         {
    1468        4404 :             test();
    1469        4404 :             IF( LT_16( Energy_16, shr( EnergyL_16, 4 ) ) && EQ_16( extl, FB_BWE ) )
    1470             :             {
    1471           0 :                 FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1472             :                 {
    1473           0 :                     SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], 6554 /* 0.2 in Q15 */ );
    1474           0 :                     move16();
    1475             :                 }
    1476           0 :                 fenvL_16 = mult_r( fenvL_16, 6554 /* 0.2 in Q15 */ );
    1477             :             }
    1478        4404 :             Copy( SWB_fenv, prev_SWB_fenv, SWB_FENV );
    1479             :         }
    1480             : 
    1481       12592 :         IF( EQ_16( mode, HARMONIC ) )
    1482             :         {
    1483         179 :             Copy( core_dec_freq, &SWB_signal[240 + st_offset], 240 );
    1484         179 :             Copy( &core_dec_freq[128], &SWB_signal[480 + st_offset], 80 );
    1485             :             /* calculate envelope */
    1486         179 :             calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, SWB_flength, st_offset );
    1487             :         }
    1488             :         ELSE
    1489             :         {
    1490       12413 :             test();
    1491       12413 :             test();
    1492       12413 :             test();
    1493       12413 :             test();
    1494       12413 :             test();
    1495       12413 :             test();
    1496       12413 :             IF( EQ_16( mode, NOISE ) || ( ( GT_16( Energy_16, EnergyL_16 ) || ( GT_16( tilt_nb, 14336 /*7 in Q11*/ ) && GT_16( Energy_16, shr( EnergyL_16, 1 ) ) ) ||
    1497             :                                             GT_16( tilt_nb, 24576 /*12 in Q11*/ ) ) &&
    1498             :                                           GT_16( Energy_16, 600 /* 75 in Q3*/ ) && GT_16( fenvL_16, 200 /* 25 in Q3*/ ) ) )
    1499             :             {
    1500         792 :                 tmp = add( swb_bwe_subband[SWB_FENV], st_offset );
    1501      254232 :                 FOR( n_freq = add( swb_bwe_subband[0], st_offset ); n_freq < tmp; n_freq++ )
    1502             :                 {
    1503      253440 :                     *Seed = extract_l( L_mac0( 20101L, 12345, *Seed ) );
    1504      253440 :                     move16();
    1505      253440 :                     SWB_signal[n_freq] = mult_r( *Seed, 32767 );
    1506      253440 :                     move16(); /*Q15 */
    1507             :                 }
    1508         792 :                 if ( NE_16( mode, NOISE ) )
    1509             :                 {
    1510         144 :                     *frica_flag = 1;
    1511         144 :                     move16();
    1512             :                 }
    1513             :             }
    1514             :             ELSE
    1515             :             {
    1516             :                 /* modify SHB frequency envelopes when SHB spectrum is unflat */
    1517      162694 :                 FOR( n_band = 0; n_band < 13; n_band++ )
    1518             :                 {
    1519      151073 :                     IF( GT_16( mult_r( SWB_fenv[n_band], 29491 /* 0.9 in Q15 */ ), SWB_fenv[n_band + 1] ) )
    1520             :                     {
    1521       43971 :                         tmp = extract_l( L_mac0( 26214 /* 0.8 in Q15 */, n_band, 492 ) ); /*Q15; 0.015 in Q15 = 492  */
    1522       43971 :                         SWB_fenv[n_band + 1] = mult_r( SWB_fenv[n_band + 1], tmp );
    1523       43971 :                         move16(); /*Q1 */
    1524             :                     }
    1525             : 
    1526      151073 :                     IF( GT_16( mult_r( SWB_fenv[n_band + 1], 29491 /* 0.9 in Q15 */ ), SWB_fenv[n_band] ) )
    1527             :                     {
    1528       39440 :                         tmp = extract_l( L_mac0( 26214 /* 0.8 in Q15 */, n_band, 492 ) ); /*Q15; 0.015 in Q15 = 492  */
    1529       39440 :                         SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], tmp );
    1530       39440 :                         move16(); /*Q1 */
    1531             :                     }
    1532             :                 }
    1533       11621 :                 Copy( &core_dec_freq[112], &SWB_signal[240 + st_offset], 128 );
    1534       11621 :                 Copy( &core_dec_freq[112], &SWB_signal[368 + st_offset], 128 );
    1535       11621 :                 Copy( &core_dec_freq[176], &SWB_signal[496 + st_offset], 64 );
    1536             : 
    1537       11621 :                 tmp1 = add_sat( abs_s( SWB_signal[368 + st_offset] ), abs_s( SWB_signal[369 + st_offset] ) ); /*Q_syn */
    1538       11621 :                 tmp2 = add_sat( abs_s( SWB_signal[365 + st_offset] ), abs_s( SWB_signal[366 + st_offset] ) ); /*Q_syn */
    1539       11621 :                 pit1 = &SWB_signal[368 + st_offset];
    1540       11621 :                 move16();
    1541             : 
    1542       11621 :                 test();
    1543       11621 :                 IF( ( tmp2 == 0 ) || ( LT_16( tmp2, mult_r( tmp1, 9830 /* 0.3 in Q15*/ ) ) ) )
    1544             :                 {
    1545        3348 :                     tmp3 = 9830;
    1546        3348 :                     move16(); /*0.3 in Q15 */
    1547       26784 :                     WHILE( LT_16( tmp3, 32767 ) )
    1548             :                     {
    1549       23436 :                         *pit1 = mult_r( *pit1, tmp3 );
    1550       23436 :                         move16(); /*Q_syn */
    1551       23436 :                         pit1++;
    1552       23436 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15*/ ); /*Q15 */
    1553             :                     }
    1554             :                 }
    1555        8273 :                 ELSE IF( LT_16( tmp2, tmp1 ) )
    1556             :                 {
    1557        5261 :                     exp = norm_s( tmp1 );
    1558        5261 :                     tmp = div_s( shl( 1, sub( 14, exp ) ), tmp1 );                          /*Q(29-exp) */
    1559        5261 :                     tmp3 = round_fx_sat( L_shl_sat( L_mult( tmp2, tmp ), add( exp, 2 ) ) ); /*Q15 */
    1560       12179 :                     WHILE( LT_16( tmp3, 32767 ) )
    1561             :                     {
    1562        6918 :                         *pit1 = mult_r( *pit1, tmp3 );
    1563        6918 :                         move16(); /*Q_syn */
    1564        6918 :                         pit1++;
    1565        6918 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15*/ ); /*Q15 */
    1566             :                     }
    1567             :                 }
    1568             : 
    1569       11621 :                 pit1 = &SWB_signal[367 + st_offset];
    1570             : 
    1571       11621 :                 IF( GT_16( mult_r( tmp1, 6554 /*0.2 in Q15*/ ), tmp2 ) )
    1572             :                 {
    1573             :                     /*20480 = 5 in Q12 */
    1574       19629 :                     FOR( tmp3 = 20480; tmp3 > 4096; tmp3 -= 2048 )
    1575             :                     {
    1576       17448 :                         *pit1 = round_fx_sat( L_shl_sat( L_mult( *pit1, tmp3 ), 3 ) ); /*Q_syn */
    1577       17448 :                         move16();
    1578       17448 :                         pit1--;
    1579             :                     }
    1580             :                 }
    1581             : 
    1582       11621 :                 tmp1 = add_sat( abs_s( SWB_signal[496 + st_offset] ), abs_s( SWB_signal[497 + st_offset] ) ); /*Q_syn */
    1583       11621 :                 tmp2 = add_sat( add_sat( abs_s( SWB_signal[492 + st_offset] ), abs_s( SWB_signal[493 + st_offset] ) ), add_sat( abs_s( SWB_signal[494 + st_offset] ), abs_s( SWB_signal[495 + st_offset] ) ) );
    1584       11621 :                 pit1 = &SWB_signal[496 + st_offset];
    1585             : 
    1586       11621 :                 test();
    1587       11621 :                 IF( ( tmp2 == 0 ) || ( LT_16( tmp2, mult_r( tmp1, 9830 /* 0.3 in Q15 */ ) ) ) )
    1588             :                 {
    1589         599 :                     tmp3 = 9830;
    1590         599 :                     move16(); /*0.3 in Q15 */
    1591        4792 :                     WHILE( LT_16( tmp3, 32767 ) )
    1592             :                     {
    1593        4193 :                         *pit1 = mult_r( *pit1, tmp3 );
    1594        4193 :                         move16(); /*Q_syn */
    1595        4193 :                         pit1++;
    1596        4193 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15 */ ); /*Q15 */
    1597             :                     }
    1598             :                 }
    1599       11022 :                 ELSE IF( LT_16( tmp2, tmp1 ) )
    1600             :                 {
    1601        3047 :                     exp = norm_s( tmp1 );
    1602        3047 :                     tmp = div_s( shl( 1, sub( 14, exp ) ), tmp1 );                          /*Q(29-exp) */
    1603        3047 :                     tmp3 = round_fx_sat( L_shl_sat( L_mult( tmp2, tmp ), add( exp, 2 ) ) ); /*Q15 */
    1604        4872 :                     WHILE( LT_16( tmp3, 32767 ) )
    1605             :                     {
    1606        1825 :                         *pit1 = mult_r( *pit1, tmp3 );
    1607        1825 :                         move16(); /*Q_syn */
    1608        1825 :                         pit1++;
    1609        1825 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15 */ ); /*Q15 */
    1610             :                     }
    1611             :                 }
    1612       11621 :                 pit1 = &SWB_signal[495 + st_offset];
    1613             : 
    1614       11621 :                 L_tmp3 = L_deposit_h( tmp1 );                         /*Q17 */
    1615       11621 :                 L_tmp4 = Mult_32_16( L_tmp3, 1638 /* 0.05 in Q15*/ ); /*Q17 */
    1616       11621 :                 exp = 14;
    1617       11621 :                 move16();
    1618       11621 :                 IF( tmp2 != 0 )
    1619             :                 {
    1620       11508 :                     exp = norm_s( tmp2 );
    1621       11508 :                     tmp = div_s( shl( 1, sub( 14, exp ) ), tmp2 );         /*Q(29-exp) */
    1622       11508 :                     L_tmp3 = L_shr( L_mult( tmp1, tmp ), 1 );              /*Q(30-exp+1)->Q(30-exp) (+1) due to *0.5 */
    1623       11508 :                     L_tmp4 = Mult_32_16( L_tmp3, 1638 /* 0.05 in Q15 */ ); /*Q(30-exp) */
    1624             :                 }
    1625             : 
    1626       11621 :                 L_tmp1 = L_shl( 1L, sub( 30, exp ) );
    1627       22448 :                 WHILE( GT_32( L_tmp3, L_tmp1 ) )
    1628             :                 {
    1629       10827 :                     L_tmp = Mult_32_16( L_tmp3, *pit1 );       /*Q(16-exp) */
    1630       10827 :                     *pit1-- = round_fx( L_shl( L_tmp, exp ) ); /*Q_syn */
    1631       10827 :                     move16();
    1632       10827 :                     L_tmp3 = L_sub( L_tmp3, L_tmp4 );
    1633             :                 }
    1634             : 
    1635             :                 /* calculate envelope */
    1636       11621 :                 calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, SWB_flength, st_offset );
    1637             :             }
    1638             :         }
    1639             : 
    1640             :         /* Normalize with envelope */
    1641       12592 :         test();
    1642       12592 :         IF( *frica_flag == 0 && NE_16( mode, NOISE ) )
    1643             :         {
    1644       11800 :             L = add( swb_bwe_subband[0], st_offset );
    1645       11800 :             exp = norm_s( L_swb_norm );
    1646       11800 :             inv_L_swb_norm = shl( div_s( shl( 1, sub( 14, exp ) ), L_swb_norm ), sub( exp, 14 ) ); /* Q15 */
    1647             : 
    1648       11800 :             IF( NE_16( mode, HARMONIC ) )
    1649             :             {
    1650       11621 :                 tmp = add( shl( inv_L_swb_norm, 1 ), inv_L_swb_norm );
    1651       11621 :                 weight = s_max( s_min( tmp, 16384 /* 0.5 in Q15 */ ), 6554 /* 0.2 in Q15 */ );
    1652             :             }
    1653             :             ELSE
    1654             :             {
    1655         179 :                 weight = 6554; /* 0.2 in Q15 */
    1656         179 :                 move16();
    1657             :             }
    1658             : 
    1659       11800 :             weight = mac_r( L_mult( 13107 /* 0.4 in Q15 */, weight ), 19661 /* 0.6 in Q15 */, ( *prev_weight ) );
    1660             : 
    1661     3787800 :             FOR( n_freq = L; n_freq < swb_bwe_subband[SWB_FENV] + st_offset; n_freq++ )
    1662             :             {
    1663     3776000 :                 signum[n_freq] = 1;
    1664     3776000 :                 move16();
    1665     3776000 :                 IF( SWB_signal[n_freq] < 0 )
    1666             :                 {
    1667     1796054 :                     signum[n_freq] = -1;
    1668     1796054 :                     move16();
    1669     1796054 :                     SWB_signal[n_freq] = negate( SWB_signal[n_freq] );
    1670     1796054 :                     move16();
    1671             :                 }
    1672     3776000 :                 L_tmp = Mult_32_16( envelope[n_freq], inv_L_swb_norm ); /* Q_syn */
    1673     3776000 :                 L_tmp1 = L_deposit_l( SWB_signal[n_freq] );             /* Q_syn */
    1674     3776000 :                 L_tmp = L_sub( L_tmp1, L_tmp );                         /* Q_syn */
    1675     3776000 :                 IF( L_tmp > 0 )
    1676             :                 {
    1677     1690106 :                     tmp = shr( weight, 1 );                                                 /* Q14 */
    1678     1690106 :                     tmp = sub( 19661, tmp );                                                /* Q14 */
    1679     1690106 :                     SWB_signal[n_freq] = extract_l( L_shl( Mult_32_16( L_tmp, tmp ), 1 ) ); /* Q_syn */
    1680             :                 }
    1681             :                 ELSE
    1682             :                 {
    1683     2085894 :                     SWB_signal[n_freq] = extract_l( L_tmp ); /* Q_syn */
    1684             :                 }
    1685     3776000 :                 move16();
    1686     3776000 :                 IF( NE_16( signum[n_freq], 1 ) )
    1687             :                 {
    1688     1796054 :                     SWB_signal[n_freq] = negate( SWB_signal[n_freq] );
    1689     1796054 :                     move16();
    1690             :                 }
    1691             :             }
    1692             : 
    1693       11800 :             tmp_exp = sub( 15, Q_syn );
    1694     3787800 :             FOR( n_freq = L; n_freq < swb_bwe_subband[SWB_FENV] + st_offset; n_freq++ ){
    1695     3776000 :                 IF( envelope[n_freq] != 0 ){
    1696     3770383 :                     exp = norm_l( envelope[n_freq] );
    1697     3770383 :             tmp = extract_h( L_shl( envelope[n_freq], exp ) );
    1698     3770383 :             exp = sub( sub( 30, exp ), Q_syn );
    1699     3770383 :             tmp = div_s( 16384 /* 0.5 in Q15*/, tmp );                              /* Q(15+exp) */
    1700     3770383 :             L_tmp = L_shr( L_mult0( SWB_signal[n_freq], tmp ), add( exp, Q_syn ) ); /* Q15 */
    1701     3770383 :             SWB_signal[n_freq] = extract_l( L_tmp );
    1702     3770383 :             move16(); /* Q15 */
    1703             :         }
    1704             :         ELSE
    1705             :         {
    1706        5617 :             SWB_signal[n_freq] = shl( SWB_signal[n_freq], tmp_exp );
    1707        5617 :             move16(); /* Q15 */
    1708             :         }
    1709             :     }
    1710       11800 :     *prev_weight = weight;
    1711       11800 :     move16();
    1712             : }
    1713             : ELSE
    1714             : {
    1715         792 :     exp = norm_s( L_swb_norm );
    1716         792 :     tmp = shl( div_s( shl( 1, sub( 14, exp ) ), L_swb_norm ), sub( exp, 14 ) ); /* Q15 */
    1717         792 :     tmp = add( shl( tmp, 1 ), tmp );
    1718         792 :     *prev_weight = s_max( s_min( tmp, 16384 /* 0.5 in Q15 */ ), 6554 /* 0.2 in Q15 */ ); /* Q15 */
    1719         792 :     move16();
    1720             : }
    1721             : 
    1722       12592 : IF( EQ_16( mode, HARMONIC ) )
    1723             : {
    1724         179 :     pit1 = &SWB_signal[swb_bwe_subband[0] + st_offset];
    1725         179 :     move16();
    1726        3580 :     FOR( n_band = 0; n_band < 19; n_band++ )
    1727             :     {
    1728        3401 :         L_mean = L_deposit_l( 0 );
    1729       57817 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
    1730             :         {
    1731       54416 :             L_mean = L_add( L_mean, abs_s( *pit1 ) ); /*Q15 */
    1732       54416 :             pit1++;
    1733             :         }
    1734        3401 :         mean = extract_l( Mult_32_16( L_mean, 2048 /* 1/16 in Q15 */ ) ); /*Q15  */
    1735        3401 :         pit1 -= 16;
    1736       57817 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
    1737             :         {
    1738       54416 :             IF( LT_16( abs_s( *pit1 ), mean ) )
    1739             :             {
    1740       31906 :                 *pit1 = mult_r( *pit1, 6554 /* 0.2 in Q15 */ ); /*Q15*/
    1741       31906 :                 move16();
    1742             :             }
    1743       54416 :             pit1++;
    1744             :         }
    1745             :     }
    1746             : }
    1747             : 
    1748       12592 : L = 1;
    1749       12592 : move16();
    1750       12592 : if ( EQ_16( mode, HARMONIC ) )
    1751             : {
    1752         179 :     L = 2;
    1753         179 :     move16();
    1754             : }
    1755             : 
    1756      187627 : FOR( n_band = 0; n_band < SWB_FENV; n_band += L )
    1757             : {
    1758      175035 :     energy = L_deposit_l( 0 );
    1759      175035 :     tmp = add( swb_bwe_subband[n_band + L], st_offset );
    1760     4204475 :     FOR( n_freq = add( swb_bwe_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1761             :     {
    1762     4029440 :         L_tmp = L_mult( SWB_signal[n_freq], SWB_signal[n_freq] ); /*Q31 */
    1763     4029440 :         energy = L_add( energy, L_shr( L_tmp, 6 ) );              /*Q25 */
    1764             :     }
    1765             : 
    1766      175035 :     IF( energy == 0 )
    1767             :     {
    1768        4148 :         tmp_ener = sqrt_swb_bwe_subband_fx_L1[n_band]; /*Q12 */
    1769        4148 :         move16();
    1770        4148 :         if ( NE_16( L, 1 ) )
    1771             :         {
    1772           3 :             tmp_ener = sqrt_swb_bwe_subband_fx_L2[shr( n_band, 1 )]; /*Q12 */
    1773           3 :             move16();
    1774             :         }
    1775             : 
    1776        4148 :         tmp = add( swb_bwe_subband[n_band + L], st_offset );
    1777        4148 :         tmp_exp = sub( Q_syn, 12 );
    1778      103132 :         FOR( n_freq = add( swb_bwe_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1779             :         {
    1780       98984 :             SWB_signal_32[n_freq] = L_shl( L_mult( tmp_ener, SWB_signal[n_freq] ), tmp_exp ); /*Qsyn */
    1781       98984 :             move32();
    1782             :         }
    1783             :     }
    1784             :     ELSE
    1785             :     {
    1786      170887 :         tmp = sub( swb_bwe_subband[n_band + L], swb_bwe_subband[n_band] );
    1787      170887 :         exp1 = norm_s( tmp );
    1788      170887 :         tmp = div_s( shl( 1, sub( 14, exp1 ) ), tmp ); /*Q(29-exp1) */
    1789      170887 :         energy = Mult_32_16( energy, tmp );            /*Q(29-exp1+25-15)->Q(-exp1+39) */
    1790             : 
    1791      170887 :         exp = norm_l( energy );
    1792      170887 :         L_tmp = L_shl( energy, exp );
    1793             :         /*exp = 31-exp-(-exp1+39);move16(); */
    1794      170887 :         exp = sub( sub( exp1, exp ), 8 );
    1795      170887 :         Ltmp_ener = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    1796             : 
    1797      170887 :         tmp = add( swb_bwe_subband[n_band + L], st_offset );
    1798      170887 :         tmp_exp = add( Q_syn, sub( exp, 15 ) );
    1799     4101343 :         FOR( n_freq = add( swb_bwe_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1800             :         {
    1801     3930456 :             SWB_signal_32[n_freq] = L_shl( Mult_32_16( Ltmp_ener, SWB_signal[n_freq] ), tmp_exp );
    1802     3930456 :             move32(); /*Qsyn+16 */
    1803             :         }
    1804             :     }
    1805             : }
    1806             : 
    1807       12592 : IF( GT_16( *prev_Energy, add_sat( Energy_16, shr( Energy_16, 2 ) ) ) && Energy_16 > 0 )
    1808             : {
    1809        1287 :     weight = shr( div_s( Energy_16, *prev_Energy ), 1 ); /*Q15 */
    1810             : }
    1811             : ELSE
    1812             : {
    1813       11305 :     weight = 16384;
    1814       11305 :     move16(); /*Q15 */
    1815             : }
    1816       12592 : L_tmp = L_mult( weight, prev_SWB_fenv[0] );                /*Q17 */
    1817       12592 : L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[0] ); /*Q17 */
    1818       12592 : wfenv = round_fx( L_tmp );                                 /*Q1 */
    1819             : 
    1820       12592 : tmp = norm_s( wfenv );
    1821       12592 : IF( GT_16( tmp, 4 ) )
    1822             : {
    1823       10269 :     tmp = 12;
    1824       10269 :     move16();
    1825       10269 :     factor = fenvL_16;
    1826       10269 :     move16();                                                                      /*Q3 */
    1827       10269 :     factor1 = mult_r( sub( shl( wfenv, 2 ), fenvL_16 ), 4096 /* 0.125 in Q15 */ ); /*Q3  */
    1828             : }
    1829             : ELSE
    1830             : {
    1831        2323 :     tmp = 14;
    1832        2323 :     move16();
    1833        2323 :     factor = shr( fenvL_16, 2 );                                       /*Q1 */
    1834        2323 :     factor1 = mult_r( sub( wfenv, factor ), 4096 /* 0.125 in Q15 */ ); /*Q1 */
    1835             : }
    1836             : 
    1837       12592 : tmp2 = add( add( swb_bwe_subband[0], 8 ), st_offset );
    1838      113328 : FOR( n_freq = add( swb_bwe_subband[0], st_offset ); n_freq < tmp2; n_freq++ )
    1839             : {
    1840      100736 :     L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1841      100736 :     SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, tmp - 1 );
    1842      100736 :     move32();                        /*15+Qsyn */
    1843      100736 :     factor = add( factor, factor1 ); /*Q3 */
    1844             : }
    1845             : 
    1846      163696 : FOR( n_band = 0; n_band < 12; n_band++ )
    1847             : {
    1848      151104 :     L_tmp = L_mult( weight, prev_SWB_fenv[n_band + 1] );                /*Q17 */
    1849      151104 :     L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[n_band + 1] ); /*Q17 */
    1850      151104 :     wfenv = round_fx( L_tmp );                                          /*Q1 */
    1851      151104 :     factor = SWB_fenv[n_band];
    1852      151104 :     move16();                                                                     /*Q1 */
    1853      151104 :     factor1 = mult_r( sub( wfenv, SWB_fenv[n_band] ), smooth_factor_fx[n_band] ); /*Q1 */
    1854      151104 :     tmp = norm_s( factor );
    1855      151104 :     IF( GT_16( tmp, 4 ) )
    1856             :     {
    1857      122025 :         tmp = 12;
    1858      122025 :         move16();
    1859      122025 :         factor = shl( factor, 2 );
    1860      122025 :         factor1 = shl( factor1, 2 );
    1861             :     }
    1862             :     ELSE
    1863             :     {
    1864       29079 :         tmp = 14;
    1865       29079 :         move16();
    1866             :     }
    1867             : 
    1868      151104 :     tmp2 = add( swb_bwe_sm_subband[n_band + 1], st_offset );
    1869     3475392 :     FOR( ; n_freq < tmp2; n_freq++ )
    1870             :     {
    1871     3324288 :         L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1872     3324288 :         SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, tmp - 1 );
    1873     3324288 :         move32();                            /*15+Qsyn */
    1874     3324288 :         factor = add_sat( factor, factor1 ); /*Q1 */
    1875             :     }
    1876             : }
    1877       12592 : L_tmp = L_mult( weight, prev_SWB_fenv[13] );                /*Q17 */
    1878       12592 : L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[13] ); /*Q17 */
    1879       12592 : wfenv = round_fx( L_tmp );                                  /*Q1 */
    1880       12592 : factor = SWB_fenv[12];
    1881       12592 : move16();                                                             /*Q1 */
    1882       12592 : factor1 = mult_r( sub( wfenv, SWB_fenv[12] ), smooth_factor_fx[12] ); /*Q1 */
    1883       12592 : tmp2 = add( swb_bwe_sm_subband[13], st_offset );
    1884      415536 : FOR( ; n_freq < tmp2; n_freq++ )
    1885             : {
    1886      402944 :     L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1887      402944 :     SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, 13 );
    1888      402944 :     move32();                        /*15+Qsyn */
    1889      402944 :     factor = add( factor, factor1 ); /*Q1 */
    1890             : }
    1891             : 
    1892       25184 : FOR( n_band = 13; n_band < SWB_FENV; n_band++ )
    1893             : {
    1894       12592 :     L_tmp = L_mult( weight, prev_SWB_fenv[n_band] );                /*Q17 */
    1895       12592 :     L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[n_band] ); /*Q17 */
    1896       12592 :     wfenv = round_fx( L_tmp );                                      /*Q1 */
    1897       12592 :     tmp2 = add( swb_bwe_subband[n_band + 1], st_offset );
    1898      214064 :     FOR( ; n_freq < tmp2; n_freq++ )
    1899             :     {
    1900      201472 :         L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1901      201472 :         SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, 13 );
    1902      201472 :         move32(); /*15+Qsyn */
    1903             :     }
    1904             : }
    1905      188880 : FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1906             : {
    1907      176288 :     prev_SWB_fenv[n_band] = SWB_fenv[n_band];
    1908      176288 :     move16(); /*Q1 */
    1909             : }
    1910             : }
    1911       12655 : pit1_32 = &SWB_signal_32[240 + st_offset]; /*15+Qsyn */
    1912       63275 : FOR( n_freq = 0; n_freq < 4; n_freq++ )
    1913             : {
    1914       50620 :     L_tmp1 = Mult_32_16( *pit1_32, 16384 /* 0.5 in Q15 */ ); /*15+Qsyn */
    1915       50620 :     *( pit1_32++ ) = L_tmp1;
    1916       50620 :     move32();
    1917             : }
    1918       12655 : *prev_Energy = Energy_16;
    1919       12655 : move16();
    1920             : 
    1921       12655 : return;
    1922             : }
    1923             : 
    1924             : /*==========================================================================*/
    1925             : /* FUNCTION      : void time_envelop_shaping_fx()                           */
    1926             : /*--------------------------------------------------------------------------*/
    1927             : /* PURPOSE       :   Time shaping of SHB signal                             */
    1928             : /*--------------------------------------------------------------------------*/
    1929             : /* INPUT ARGUMENTS                                                          */
    1930             : /* _(Word16) L          :length                                             */
    1931             : /* _(Word16) Q_syn      :Q format                                           */
    1932             : /*--------------------------------------------------------------------------*/
    1933             : /* OUTPUT ARGUMENTS :                                                       */
    1934             : /*   _None                                                                  */
    1935             : /*--------------------------------------------------------------------------*/
    1936             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
    1937             : /*   _(Word16[])werr      : SHB synthesis                                   */
    1938             : /*   _(Word16[])SWB_tenv    : frequency envelope                            */
    1939             : /*--------------------------------------------------------------------------*/
    1940             : /* RETURN ARGUMENTS :                                                       */
    1941             : /*   _ None                                                                 */
    1942             : /*--------------------------------------------------------------------------*/
    1943           0 : void time_envelop_shaping_fx(
    1944             :     Word16 werr[],     /* i/o: SHB synthesis           Q_synth*/
    1945             :     Word32 SWB_tenv[], /* i/o: frequency envelope          Q15*/
    1946             :     const Word16 L,    /* i  : frame length                   */
    1947             :     Word16 *Q_synth )
    1948             : {
    1949             :     Word16 *pit;
    1950             :     Word32 Energy;
    1951             :     Word16 i, j;
    1952             :     Word16 tmp_ener, Energy_16;
    1953             :     Word16 exp_L, exp, frac, tmp, inv_L;
    1954             :     Word32 L_tmp;
    1955             : 
    1956           0 :     pit = werr;
    1957           0 :     exp_L = norm_s( L );
    1958           0 :     inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */
    1959           0 :     FOR( i = 0; i < SWB_TENV; i++ )
    1960             :     {
    1961           0 :         Energy = L_deposit_l( 0 );
    1962           0 :         FOR( j = 0; j < L / 4; j++ )
    1963             :         {
    1964           0 :             Energy = L_mac0_sat( Energy, *pit, *pit ); /*(2*Q_synth) */
    1965           0 :             pit++;
    1966             :         }
    1967           0 :         Energy = Mult_32_16( Energy, inv_L ); /*Q(29-exp_L-15) -> Q(-exp_L+14+2*Q_synth) */
    1968           0 :         Energy_16 = 0;
    1969           0 :         move16();
    1970             :         /*exp = 31-(-exp_L+14 +(2*(*Q_synth))); */
    1971           0 :         exp = sub( 17, sub( shl( ( *Q_synth ), 1 ), exp_L ) );
    1972             : 
    1973           0 :         IF( Energy != 0 )
    1974             :         {
    1975           0 :             exp = norm_l( Energy );
    1976           0 :             frac = extract_h( L_shl( Energy, exp ) );
    1977             :             /*exp = sub(exp, 30-(-exp_L+14-2+(2*(*Q_synth))));  */
    1978           0 :             exp = sub( exp, sub( 30, add( sub( shl( ( *Q_synth ), 1 ), exp_L ), 14 - 2 ) ) );
    1979             : 
    1980           0 :             tmp = div_s( 16384, frac );
    1981           0 :             L_tmp = L_deposit_h( tmp );
    1982           0 :             Energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    1983           0 :             Energy_16 = round_fx( L_shl( Energy, sub( exp, 15 ) ) ); /*Q0 */
    1984             :         }
    1985             : 
    1986           0 :         test();
    1987           0 :         IF( LT_32( SWB_tenv[i], 65536 /* 2 in Q15 */ ) && LT_32( Energy, L_shl_sat( SWB_tenv[i], sub( 16, exp ) ) ) )
    1988             :         {
    1989           0 :             *Q_synth = add( *Q_synth, 3 );
    1990           0 :             move16();
    1991             :         }
    1992             :         ELSE
    1993             :         {
    1994           0 :             pit -= shr( L, 2 );
    1995           0 :             move16();
    1996           0 :             tmp_ener = 0;
    1997           0 :             move16();
    1998           0 :             exp = 0;
    1999           0 :             move16();
    2000             : 
    2001           0 :             IF( Energy_16 != 0 )
    2002             :             {
    2003           0 :                 exp = norm_s( Energy_16 );
    2004           0 :                 tmp_ener = div_s( shl( 1, sub( 14, exp ) ), Energy_16 ); /*Q(29-exp) */
    2005             :             }
    2006             : 
    2007           0 :             L_tmp = Mult_32_16( SWB_tenv[i], tmp_ener ); /*Q(29-exp) */
    2008           0 :             tmp = round_fx( L_tmp );                     /*Q(13-exp) */
    2009             : 
    2010           0 :             FOR( j = 0; j < L / 4; j++ )
    2011             :             {
    2012           0 :                 *pit = round_fx_sat( L_shl_sat( L_mult( tmp, *pit ), sub( exp, 1 ) ) ); /*Q(13-exp+1)->Q(14-exp)->Qsyn-3 */
    2013           0 :                 move16();
    2014           0 :                 pit++;
    2015             :             }
    2016             :         }
    2017             :     }
    2018             : 
    2019           0 :     return;
    2020             : }
    2021             : 
    2022          63 : void time_envelop_shaping_ivas_fx(
    2023             :     Word16 werr[],     /* i/o: SHB synthesis           Q_synth*/
    2024             :     Word32 SWB_tenv[], /* i/o: frequency envelope          Q15*/
    2025             :     const Word16 L,    /* i  : frame length                   */
    2026             :     Word16 *Q_synth )
    2027             : {
    2028             :     Word16 *pit;
    2029             :     Word32 Energy;
    2030             :     Word16 i, j;
    2031             :     Word16 tmp_ener, Energy_16;
    2032             :     Word64 Energy_64;
    2033             :     Word16 exp_L, exp, frac, tmp, inv_L, w_tmp, Energy_Q;
    2034             :     Word32 L_tmp;
    2035             : 
    2036          63 :     pit = werr;
    2037          63 :     exp_L = norm_s( L );
    2038          63 :     inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */
    2039         315 :     FOR( i = 0; i < SWB_TENV; i++ )
    2040             :     {
    2041         252 :         Energy_64 = L_deposit_l( 0 );
    2042       54332 :         FOR( j = 0; j < L / 4; j++ )
    2043             :         {
    2044       54080 :             Energy_64 = W_mac0_16_16( Energy_64, *pit, *pit ); /*(2*Q_synth) */
    2045       54080 :             pit++;
    2046             :         }
    2047         252 :         w_tmp = W_norm( Energy_64 );
    2048         252 :         Energy_64 = W_shl( Energy_64, w_tmp );
    2049         252 :         Energy = W_extract_h( Energy_64 ); /*2*Q_synth + w_tmp -32*/
    2050         252 :         Energy_Q = sub( add( shl( ( *Q_synth ), 1 ), w_tmp ), 32 );
    2051         252 :         Energy = Mult_32_16( Energy, inv_L ); /*Q(29-exp_L-15) -> Q(-exp_L+14+2*Q_synth+w_tmp-32) */
    2052         252 :         Energy_16 = 0;
    2053         252 :         move16();
    2054             :         /*exp = 31-(-exp_L+14 +(2*(*Q_synth)+w_tmp-32)); */
    2055         252 :         exp = sub( 17, sub( Energy_Q, exp_L ) );
    2056             : 
    2057         252 :         IF( Energy != 0 )
    2058             :         {
    2059         252 :             exp = norm_l( Energy );
    2060         252 :             frac = extract_h( L_shl( Energy, exp ) );
    2061             :             /*exp = sub(exp, 30-(-exp_L+14-2+(2*(*Q_synth)+w_tmp-32)));  */
    2062         252 :             exp = sub( exp, sub( 30, add( sub( Energy_Q, exp_L ), 14 - 2 ) ) );
    2063             : 
    2064         252 :             tmp = div_s( 16384, frac );
    2065         252 :             L_tmp = L_deposit_h( tmp );
    2066         252 :             Energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    2067         252 :             Energy_16 = round_fx( L_shl( Energy, sub( exp, 15 ) ) ); /*Q0 */
    2068             :         }
    2069             : 
    2070         252 :         test();
    2071         252 :         IF( LT_32( SWB_tenv[i], 65536 /* 2 in Q15 */ ) && LT_32( Energy, L_shl_sat( SWB_tenv[i], sub( 16, exp ) ) ) )
    2072             :         {
    2073           4 :             *Q_synth = add( *Q_synth, 3 );
    2074           4 :             move16();
    2075             :         }
    2076             :         ELSE
    2077             :         {
    2078         248 :             pit -= shr( L, 2 );
    2079         248 :             tmp_ener = 0;
    2080         248 :             move16();
    2081         248 :             exp = 0;
    2082         248 :             move16();
    2083             : 
    2084         248 :             IF( Energy_16 != 0 )
    2085             :             {
    2086         247 :                 exp = norm_s( Energy_16 );
    2087         247 :                 tmp_ener = div_s( shl( 1, sub( 14, exp ) ), Energy_16 ); /*Q(29-exp) */
    2088             :             }
    2089             : 
    2090         248 :             L_tmp = Mult_32_16( SWB_tenv[i], tmp_ener ); /*Q(29-exp) */
    2091         248 :             tmp = round_fx( L_tmp );                     /*Q(13-exp) */
    2092             : 
    2093       53368 :             FOR( j = 0; j < L / 4; j++ )
    2094             :             {
    2095       53120 :                 *pit = round_fx_sat( L_shl_sat( L_mult( tmp, *pit ), sub( exp, 1 ) ) ); /*Q(13-exp+1)->Q(14-exp)->Qsyn-3 */
    2096       53120 :                 move16();
    2097       53120 :                 pit++;
    2098             :             }
    2099             :         }
    2100             :     }
    2101             : 
    2102          63 :     return;
    2103             : }
    2104             : 
    2105             : /*==========================================================================*/
    2106             : /* FUNCTION      : void time_reduce_pre_echo_fx()                           */
    2107             : /*--------------------------------------------------------------------------*/
    2108             : /* PURPOSE       :   Windowing and time-domain aliasing                     */
    2109             : /*--------------------------------------------------------------------------*/
    2110             : /* INPUT ARGUMENTS                                                          */
    2111             : /* _(Word16*) synth         :ACELP core synthesis    Q_syn                  */
    2112             : /* _(Word16) L          :subframe length                                    */
    2113             : /* _(Word16) Q_syn        :Q format                                         */
    2114             : /*--------------------------------------------------------------------------*/
    2115             : /* OUTPUT ARGUMENTS :                                                       */
    2116             : /*   _(Word16*)error        : SHB BWE synthesis       Q_syn                 */
    2117             : /*   _(Word16)prev_td_energy    : last td energy          Q_syn             */
    2118             : /*--------------------------------------------------------------------------*/
    2119             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
    2120             : /*   _None                                                                  */
    2121             : /*--------------------------------------------------------------------------*/
    2122             : /* RETURN ARGUMENTS :                                                       */
    2123             : /*   _ None                                                                 */
    2124             : /*--------------------------------------------------------------------------*/
    2125          97 : void time_reduce_pre_echo_fx(
    2126             :     const Word16 *synth,   /* i  : ACELP core synthesis    Q_syn*/
    2127             :     Word16 *error,         /* i/o: SHB BWE synthesis          Q0*/
    2128             :     Word16 prev_td_energy, /* o  : last td energy             Q0*/
    2129             :     const Word16 L,        /* i  : subframe length              */
    2130             :     Word16 Q_syn,
    2131             :     Word16 Q_synth )
    2132             : {
    2133          97 :     Word16 i, j, pos = 0, Len;
    2134             :     Word32 energy;
    2135             :     Word16 energy_16;
    2136             :     Word32 energyL[4];
    2137             :     Word16 tmp_ener;
    2138             :     Word16 *pit;
    2139             :     Word16 tmpi, tmp_exp;
    2140             :     Word16 exp_L, exp, frac, inv_L, exp_j, tmp;
    2141             :     Word32 L_tmp, L_tmp1, Ltmp_ener;
    2142             : 
    2143          97 :     exp_L = norm_s( L );
    2144          97 :     inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */
    2145         485 :     FOR( i = 0; i < 4; i++ )
    2146             :     {
    2147         388 :         Len = i_mult( L, i );
    2148             : 
    2149         388 :         L_tmp = L_deposit_l( 0 );
    2150       88388 :         FOR( j = 0; j < L; j++ )
    2151             :         {
    2152       88000 :             L_tmp = L_mac0_sat( L_tmp, synth[Len + j], synth[Len + j] ); /*2*Q_syn */
    2153             :         }
    2154         388 :         energyL[i] = Mult_32_16( L_shr( L_tmp, sub( 14, exp_L ) ), inv_L );
    2155         388 :         move32(); /*Q(2*Q_syn +29-exp_L-15 - (14-exp_L) ) -> Q(2*Q_syn) */
    2156             : 
    2157         388 :         IF( energyL[i] != 0 )
    2158             :         {
    2159         356 :             exp = norm_l( energyL[i] );
    2160         356 :             frac = extract_h( L_shl( energyL[i], exp ) );
    2161         356 :             exp = sub( exp, sub( 30, shl( Q_syn, 1 ) ) );
    2162             : 
    2163         356 :             tmp = div_s( 16384, frac );
    2164         356 :             L_tmp = L_deposit_h( tmp );
    2165         356 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    2166         356 :             energyL[i] = L_shl( L_tmp, sub( exp, 16 ) );
    2167         356 :             move32(); /* Q15 */
    2168             :         }
    2169             :     }
    2170             : 
    2171         367 :     FOR( i = 0; i < 3; i++ )
    2172             :     {
    2173         281 :         L_tmp = Mult_32_16( energyL[i], 29491 /* 0.9 in Q15 */ ); /*Q14 */
    2174         281 :         IF( GT_32( L_shr( energyL[i + 1], 1 ), L_tmp ) && GT_32( energyL[i + 1], 1638400 /* 100 in Q14 */ ) )
    2175             :         {
    2176          11 :             pos = add( i, 1 );
    2177          11 :             move16();
    2178          11 :             BREAK;
    2179             :         }
    2180             :     }
    2181             : 
    2182          97 :     IF( pos > 0 )
    2183             :     {
    2184          11 :         IF( LT_16( pos, 3 ) )
    2185             :         {
    2186           6 :             pos = add( pos, 1 );
    2187             :         }
    2188          11 :         energy = L_deposit_l( 0 );
    2189          11 :         j = i_mult( L, pos );
    2190        6971 :         FOR( i = 0; i < j; i++ )
    2191             :         {
    2192        6960 :             energy = L_mac0_sat( energy, error[i], error[i] ); /*Q(2*Q_synth) */
    2193             :         }
    2194             : 
    2195          11 :         exp_j = norm_s( j );
    2196          11 :         tmp = div_s( shl( 1, sub( 14, exp_j ) ), j ); /*Q(29-exp_j) */
    2197          11 :         energy = Mult_32_16( energy, tmp );           /*Q(29-exp_j+1-16) -> Q(-exp_j+14 +2*Q_synth) */
    2198          11 :         energy_16 = 0;
    2199          11 :         move16();
    2200             : 
    2201          11 :         IF( energy != 0 )
    2202             :         {
    2203          11 :             exp = norm_l( energy );
    2204          11 :             frac = extract_h( L_shl( energy, exp ) );
    2205             :             /*exp = sub(exp, 30-(-exp_j+14 +2*Q_synth));  */
    2206          11 :             exp = sub( exp, sub( 14, sub( shl( Q_synth, 1 ), exp_j ) ) );
    2207          11 :             tmp = div_s( 16384, frac );
    2208          11 :             L_tmp = L_deposit_h( tmp );
    2209          11 :             energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    2210          11 :             energy_16 = round_fx( L_shl( energy, sub( exp, 15 ) ) ); /*Q0 */
    2211             :         }
    2212             : 
    2213          11 :         tmp = mult_r( energy_16, 6554 /* 0.2 in Q15 */ ); /*Q0 */
    2214          11 :         if ( LT_16( prev_td_energy, tmp ) )
    2215             :         {
    2216           8 :             prev_td_energy = tmp;
    2217           8 :             move16();
    2218             :         }
    2219             : 
    2220          11 :         tmp_ener = 0;
    2221          11 :         move16();
    2222          11 :         exp = 0;
    2223          11 :         move16();
    2224          11 :         IF( energy_16 != 0 )
    2225             :         {
    2226          11 :             exp = norm_s( energy_16 );
    2227          11 :             tmp_ener = div_s( shl( 1, sub( 14, exp ) ), energy_16 ); /*Q(29-exp) */
    2228             :         }
    2229          11 :         L_tmp = L_mult( prev_td_energy, tmp_ener ); /*Q(30-exp) */
    2230          11 :         tmp_exp = add( 1, exp );
    2231        6971 :         FOR( i = 0; i < j; i++ )
    2232             :         {
    2233        6960 :             error[i] = round_fx_sat( L_shl_sat( Mult_32_16( L_tmp, error[i] ), tmp_exp ) ); /*Q(30-exp+1-16)->Q(15-exp)->Q_synth */
    2234        6960 :             move16();
    2235             :         }
    2236             : 
    2237          11 :         energy = L_deposit_l( 0 );
    2238        2651 :         FOR( i = j; i < ( j + L ); i++ )
    2239             :         {
    2240        2640 :             energy = L_mac0_sat( energy, error[i], error[i] ); /*(2*Q_synth) */
    2241             :         }
    2242             : 
    2243          11 :         energy = Mult_32_16( energy, inv_L ); /*Q(29-exp_L+1-16) -> Q(-exp_L+14) */
    2244          11 :         energy_16 = 0;
    2245          11 :         move16();
    2246          11 :         IF( energy != 0 )
    2247             :         {
    2248          11 :             exp = norm_l( energy );
    2249          11 :             frac = extract_h( L_shl( energy, exp ) );
    2250             :             /*exp = sub(exp, 30-(-exp_L+14+2*Q_synth));  */
    2251          11 :             exp = sub( exp, sub( 14, sub( shl( Q_synth, 1 ), exp_L ) ) );
    2252             : 
    2253          11 :             tmp = div_s( 16384, frac );
    2254          11 :             L_tmp = L_deposit_h( tmp );
    2255          11 :             energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    2256          11 :             energy_16 = round_fx( L_shl( energy, sub( exp, 15 ) ) ); /*Q0 */
    2257             :         }
    2258             : 
    2259          11 :         tmp_ener = 0;
    2260          11 :         move16();
    2261          11 :         exp = 0;
    2262          11 :         move16();
    2263          11 :         IF( energy_16 != 0 )
    2264             :         {
    2265          11 :             exp = norm_s( energy_16 );
    2266          11 :             tmp_ener = div_s( shl( 1, sub( 14, exp ) ), energy_16 ); /*Q(29-exp) */
    2267             :         }
    2268          11 :         Ltmp_ener = L_mult( prev_td_energy, tmp_ener ); /*Q(30-exp) */
    2269          11 :         L_tmp1 = L_shl( 1, sub( 30, exp ) );
    2270             : 
    2271          11 :         pit = &error[j];
    2272          11 :         move16();
    2273        2651 :         FOR( i = 0; i < L; i++ )
    2274             :         {
    2275        2640 :             tmpi = round_fx( L_shl( L_mult( i, inv_L ), add( 1, exp_L ) ) );             /*Q15 */
    2276        2640 :             L_tmp = L_sub( L_tmp1, Ltmp_ener );                                          /*Q(30-exp) */
    2277        2640 :             L_tmp = Mult_32_16( L_tmp, tmpi );                                           /*Q(30-exp) */
    2278        2640 :             L_tmp = L_add( Ltmp_ener, L_tmp );                                           /*Q(30-exp) */
    2279        2640 :             tmp = round_fx_sat( L_shl_sat( Mult_32_16( L_tmp, *pit ), add( 1, exp ) ) ); /*Q0 */
    2280        2640 :             *pit++ = tmp;
    2281        2640 :             move16();
    2282             :         }
    2283             :     }
    2284             : 
    2285          97 :     return;
    2286             : }
    2287             : 
    2288             : /*-------------------------------------------------------------------*
    2289             :  * calc_normal_length_fx_32()
    2290             :  *
    2291             :  *-------------------------------------------------------------------*/
    2292        4834 : void calc_normal_length_fx_32(
    2293             :     const Word16 core,      /* i  : core                   : Q0  */
    2294             :     const Word32 *sp,       /* i  : input signal           : Q12 */
    2295             :     const Word16 mode,      /* i  : input mode             : Q0  */
    2296             :     const Word16 extl,      /* i  : extension layer        : Q0  */
    2297             :     Word16 *L_swb_norm,     /* o  : normalize length       : Q0  */
    2298             :     Word16 *prev_L_swb_norm /*i/o : last normalize length  : Q0  */
    2299             : )
    2300             : {
    2301             :     Word16 i, n_freq, n_band, THRES;
    2302             :     Word16 L_swb_norm_trans, L_swb_norm_norm, L_swb_norm_harm, L_swb_norm_cur;
    2303             :     Word16 N;
    2304             : 
    2305             :     const Word32 *pit;
    2306             :     Word32 peak, mean, mag;
    2307             :     Word32 L_tmp1, L_tmp2;
    2308             : 
    2309        4834 :     THRES = 4;
    2310        4834 :     move16();
    2311        4834 :     test();
    2312        4834 :     test();
    2313        4834 :     if ( EQ_16( core, HQ_CORE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
    2314             :     {
    2315        4834 :         THRES = 8;
    2316        4834 :         move16();
    2317             :     }
    2318             : 
    2319        4834 :     N = 16;
    2320        4834 :     move16();
    2321        4834 :     test();
    2322        4834 :     test();
    2323        4834 :     if ( EQ_16( core, HQ_CORE ) && ( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) ) )
    2324             :     {
    2325        1983 :         N = 13;
    2326        1983 :         move16();
    2327             :     }
    2328             : 
    2329        4834 :     n_band = 0;
    2330        4834 :     move16();
    2331        4834 :     pit = sp;
    2332             : 
    2333       76229 :     FOR( i = 0; i < N; i++ )
    2334             :     {
    2335       71395 :         peak = 0;
    2336       71395 :         move16();
    2337       71395 :         mean = 0;
    2338       71395 :         move16();
    2339             : 
    2340     1213715 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
    2341             :         {
    2342     1142320 :             mag = L_abs( *pit );
    2343     1142320 :             if ( GT_32( mag, peak ) )
    2344             :             {
    2345      215535 :                 peak = mag;
    2346      215535 :                 move16();
    2347             :             }
    2348     1142320 :             mean = L_add_sat( mean, mag );
    2349     1142320 :             pit++;
    2350             :         }
    2351             : 
    2352       71395 :         L_tmp1 = Mult_32_16( peak, shl( add( 15, THRES ), 10 ) );
    2353       71395 :         L_tmp2 = Mult_32_16( mean, shl( THRES, 10 ) );
    2354       71395 :         test();
    2355       71395 :         IF( GT_32( L_tmp1, L_tmp2 ) && GT_32( peak, 40960 /* 10 in Q12 */ ) )
    2356             :         {
    2357       15558 :             n_band = add( n_band, 1 );
    2358             :         }
    2359             :     }
    2360             : 
    2361        4834 :     IF( core == ACELP_CORE )
    2362             :     {
    2363           0 :         L_swb_norm_trans = add( 4, shr( n_band, 2 ) );
    2364           0 :         L_swb_norm_norm = add( 8, shr( n_band, 1 ) );
    2365           0 :         L_swb_norm_harm = s_max( add( 32, shl( n_band, 1 ) ), 24 );
    2366             : 
    2367           0 :         IF( EQ_16( mode, HARMONIC ) )
    2368             :         {
    2369           0 :             L_swb_norm_cur = L_swb_norm_harm;
    2370           0 :             move16();
    2371             :         }
    2372           0 :         ELSE IF( EQ_16( mode, NORMAL ) )
    2373             :         {
    2374           0 :             L_swb_norm_cur = L_swb_norm_norm;
    2375           0 :             move16();
    2376             :         }
    2377             :         ELSE
    2378             :         {
    2379           0 :             L_swb_norm_cur = L_swb_norm_trans;
    2380           0 :             move16();
    2381             :         }
    2382             : 
    2383           0 :         *L_swb_norm = shr( add( L_swb_norm_cur, *prev_L_swb_norm ), 1 );
    2384           0 :         move16();
    2385           0 :         *prev_L_swb_norm = L_swb_norm_cur;
    2386           0 :         move16();
    2387             :     }
    2388             :     ELSE
    2389             :     {
    2390        4834 :         test();
    2391        4834 :         IF( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) )
    2392             :         {
    2393        1983 :             L_swb_norm_cur = add( 32, add( shl( n_band, 1 ), shr( n_band, 1 ) ) );
    2394             :         }
    2395             :         ELSE
    2396             :         {
    2397        2851 :             L_swb_norm_cur = add( 8, shr( n_band, 1 ) );
    2398             :         }
    2399             : 
    2400        4834 :         *L_swb_norm = extract_h( L_add( L_mac( L_mult( L_swb_norm_cur, 3276 /* 0.1 in Q15 */ ), *prev_L_swb_norm, 29491 /* 0.9 in Q15 */ ), 32768 /* 1 in Q15 */ ) );
    2401        4834 :         *prev_L_swb_norm = L_swb_norm_cur;
    2402        4834 :         move16();
    2403        4834 :         move16();
    2404             :     }
    2405             : 
    2406        4834 :     return;
    2407             : }
    2408             : 
    2409             : 
    2410             : /*-------------------------------------------------------------------*
    2411             :  * calc_norm_envelop_fx_32()
    2412             :  *
    2413             :  *-------------------------------------------------------------------*/
    2414           0 : void calc_norm_envelop_fx_32(
    2415             :     const Word32 SWB_signal_fx[], /* i  : SWB spectrum           : Q12 */
    2416             :     Word32 *envelope_fx,          /* o  : normalized envelope    : Q16 */
    2417             :     const Word16 L_swb_norm,      /* i  : length of envelope     : Q0  */
    2418             :     const Word16 SWB_flength,     /* i  : Length of input/output : Q0  */
    2419             :     const Word16 st_offset        /* i  : offset                 : Q0  */
    2420             : )
    2421             : {
    2422             :     Word16 i, lookback, env_index, n_freq, n_lag_now, n_lag, tmp;
    2423             : 
    2424           0 :     lookback = shr( L_swb_norm, 1 );
    2425           0 :     move16();
    2426           0 :     env_index = add( swb_bwe_subband[0], st_offset );
    2427           0 :     move16();
    2428           0 :     n_lag_now = L_swb_norm;
    2429           0 :     move16();
    2430           0 :     tmp = sub( add( SWB_flength, st_offset ), L_swb_norm );
    2431           0 :     FOR( n_freq = sub( add( swb_bwe_trans_subband[0], st_offset ), lookback ); n_freq < tmp; n_freq++ )
    2432             :     {
    2433             :         /* Apply MA filter */
    2434           0 :         envelope_fx[env_index] = 0;
    2435           0 :         move16();
    2436           0 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
    2437             :         {
    2438           0 :             envelope_fx[env_index] = L_add_sat( envelope_fx[env_index], L_abs( SWB_signal_fx[n_freq + n_lag] ) );
    2439           0 :             move32();
    2440             :         }
    2441           0 :         env_index = add( env_index, 1 );
    2442             :     }
    2443           0 :     i = 0;
    2444           0 :     move16();
    2445           0 :     tmp = sub( add( SWB_flength, st_offset ), lookback );
    2446           0 :     FOR( n_freq = sub( add( SWB_flength, st_offset ), L_swb_norm ); n_freq < tmp; n_freq++ )
    2447             :     {
    2448             :         Word32 L_tmp;
    2449             : 
    2450           0 :         n_lag_now = sub( L_swb_norm, i );
    2451           0 :         move16();
    2452             :         /* Apply MA filter */
    2453           0 :         L_tmp = L_deposit_l( 0 );
    2454           0 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
    2455             :         {
    2456           0 :             L_tmp = L_add( L_tmp, L_abs( SWB_signal_fx[n_freq + n_lag] ) );
    2457             :         }
    2458           0 :         envelope_fx[env_index] = L_tmp;
    2459           0 :         move32();
    2460           0 :         env_index = add( env_index, 1 );
    2461           0 :         i++;
    2462             :     }
    2463             : 
    2464           0 :     return;
    2465             : }
    2466             : 
    2467             : /*-------------------------------------------------------------------*
    2468             :  * hq_generic_decoding_fx()
    2469             :  *
    2470             :  *-------------------------------------------------------------------*/
    2471          50 : void hq_generic_decoding_fx(
    2472             :     const Word16 HQ_mode,             /* i  : HQ mode                                      : Q0  */
    2473             :     Word32 *coeff_out1_fx,            /* i/o: BWE input & temporary buffer                 : Q12 */
    2474             :     const Word16 *hq_generic_fenv_fx, /* i  : SWB frequency envelopes                      : Q1  */
    2475             :     Word32 *coeff_out_fx,             /* o  : SWB signal in MDCT domain                    : Q12 */
    2476             :     const Word16 hq_generic_offset,   /* i  : frequency offset for representing hq generci : Q0  */
    2477             :     Word16 *prev_L_swb_norm,          /* i/o: last normalize length                        : Q0  */
    2478             :     const Word16 hq_generic_exc_clas, /* i  : bwe excitation class                         " Q0  */
    2479             :     const Word16 *R )
    2480             : {
    2481             :     Word16 i, n_freq, n_band, L_swb_norm;
    2482             :     Word16 k;
    2483             :     Word16 nenv;
    2484             :     Word16 tenv;
    2485             : 
    2486             :     Word16 exp, exp1, exp2, frac, tmp, tmp2, cs;
    2487             :     Word32 L_tmp, L_tmp1, L_tmp2, max_coeff_fx;
    2488             :     Word16 fenvL_fx, wfenv_fx, factor_fx;
    2489             :     Word32 *pit1_fx;
    2490             :     Word16 tmp1_fx, tmp2_fx, tmp3_fx, tmp4_fx;
    2491             :     Word32 energy_fx;
    2492             : 
    2493             :     Word32 envelope_fx[L_FRAME16k];
    2494             :     Word32 mean_vector_fx[20];
    2495             :     Word16 rn_weight0_fx;
    2496             :     Word16 s;
    2497             :     Word16 blen, nband_lf, sfidx, efidx;
    2498             :     Word16 bwe_seed;
    2499             :     Word16 signum[L_FRAME16k];
    2500             : 
    2501          50 :     nenv = sub( SWB_FENV, 2 );
    2502          50 :     if ( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2503             :     {
    2504          50 :         nenv = SWB_FENV;
    2505          50 :         move16();
    2506             :     }
    2507             : 
    2508          50 :     tenv = nenv;
    2509          50 :     move16();
    2510          50 :     if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
    2511             :     {
    2512           0 :         tenv = add( nenv, DIM_FB );
    2513             :     }
    2514             : 
    2515          50 :     max_coeff_fx = 0;
    2516          50 :     move16();
    2517          50 :     tmp = add( swb_bwe_subband[0], hq_generic_offset );
    2518         850 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp; n_freq++ )
    2519             :     {
    2520         800 :         max_coeff_fx = L_max( max_coeff_fx, L_abs( coeff_out1_fx[n_freq] ) );
    2521             :     }
    2522          50 :     cs = norm_l( max_coeff_fx );
    2523             : 
    2524          50 :     L_tmp = 0;
    2525          50 :     move16();
    2526          50 :     tmp2 = add( swb_bwe_subband[0], hq_generic_offset );
    2527         850 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp2; n_freq++ )
    2528             :     {
    2529         800 :         tmp = extract_h( L_shl( coeff_out1_fx[n_freq], cs ) ); /*12 + cs - 16 */
    2530         800 :         L_tmp1 = L_mult0( tmp, tmp );                          /*2*(cs-2) */
    2531         800 :         L_tmp = L_add( L_tmp, L_shr( L_tmp1, 5 ) );            /*2*(cs-2) - 5 */
    2532             :     }
    2533          50 :     cs = sub( shl( cs, 1 ), 9 );
    2534          50 :     fenvL_fx = 0;
    2535          50 :     move16();
    2536          50 :     IF( L_tmp != 0 )
    2537             :     {
    2538          50 :         exp = norm_l( L_tmp );
    2539          50 :         frac = round_fx_sat( L_shl( L_tmp, exp ) ); /*cs+exp-16 */
    2540          50 :         tmp = div_s( 16384, frac );                 /*15 + 14 - (cs+exp-16) */
    2541          50 :         exp = sub( add( cs, exp ), 30 );
    2542          50 :         L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                  /*Q31 - exp */
    2543          50 :         fenvL_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /*Q1 */
    2544             :     }
    2545             : 
    2546          50 :     calc_normal_length_fx_32( HQ_CORE, coeff_out1_fx, HQ_GEN_SWB, -1, &L_swb_norm, prev_L_swb_norm );
    2547             : 
    2548          50 :     calc_norm_envelop_lf_fx( coeff_out1_fx, envelope_fx, &L_swb_norm, HQ_mode, hq_generic_offset, &sfidx, &efidx );
    2549             : 
    2550          50 :     blen = 16;
    2551          50 :     move16();
    2552             : 
    2553          50 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    2554             :     {
    2555          25 :         rn_weight0_fx = 819;
    2556          25 :         move16(); /* 0.8 Q10 */
    2557             :     }
    2558          25 :     ELSE IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC1 ) )
    2559             :     {
    2560           0 :         rn_weight0_fx = 51;
    2561           0 :         move16(); /* 0.05 Q10*/
    2562             :     }
    2563             :     ELSE
    2564             :     {
    2565          25 :         rn_weight0_fx = 205;
    2566          25 :         move16(); /* 0.02 Q10 */
    2567             :     }
    2568             : 
    2569          50 :     tmp = sub( efidx, sfidx );
    2570          50 :     IF( tmp == 0 )
    2571             :     {
    2572           0 :         nband_lf = 0;
    2573           0 :         move16();
    2574             :     }
    2575             :     ELSE
    2576             :     {
    2577          50 :         exp = norm_s( tmp );
    2578          50 :         nband_lf = shl( tmp, sub( exp, 1 ) );
    2579          50 :         exp1 = norm_s( blen );
    2580          50 :         tmp = shl( blen, exp1 );
    2581          50 :         nband_lf = shr( div_s( nband_lf, tmp ), add( sub( 14, exp1 ), exp ) ); /* 15 + exp-1 - exp1, Q0*/
    2582             :     }
    2583             : 
    2584        7250 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2585             :     {
    2586        7200 :         IF( coeff_out1_fx[n_freq] < 0 )
    2587             :         {
    2588        3639 :             signum[n_freq] = -1;
    2589        3639 :             move16();
    2590        3639 :             coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2591        3639 :             move32();
    2592             :         }
    2593             :         ELSE
    2594             :         {
    2595        3561 :             signum[n_freq] = 1;
    2596        3561 :             move16();
    2597             :         }
    2598             :     }
    2599             : 
    2600             :     /* applying whitening */
    2601        7250 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2602             :     {
    2603        7200 :         exp = norm_l( coeff_out1_fx[n_freq] ) - 1;
    2604        7200 :         exp1 = norm_l( envelope_fx[n_freq] );
    2605             : 
    2606        7200 :         L_tmp = L_shl( coeff_out1_fx[n_freq], exp );
    2607        7200 :         L_tmp1 = L_shl( envelope_fx[n_freq], exp1 );
    2608             : 
    2609        7200 :         logic16();
    2610        7200 :         coeff_out1_fx[n_freq] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2611        7200 :         move32(); /*31 + exp1 - exp2*/
    2612             : 
    2613        7200 :         exp = add( 31, sub( exp, exp1 ) );
    2614        7200 :         coeff_out1_fx[n_freq] = L_shl( coeff_out1_fx[n_freq], sub( 20, exp ) );
    2615        7200 :         move32(); /*Q12->Q20*/
    2616             :     }
    2617             : 
    2618             :     /* mean vector generation for controlling dynamic range */
    2619         500 :     FOR( k = 0; k < nband_lf; ++k )
    2620             :     {
    2621         450 :         energy_fx = 1;
    2622         450 :         move16();
    2623             : 
    2624         450 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2625        7650 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    2626             :         {
    2627        7200 :             energy_fx = L_add( energy_fx, coeff_out1_fx[i] );
    2628             :         }
    2629         450 :         exp = sub( norm_l( energy_fx ), 1 );
    2630         450 :         L_tmp = L_shl( energy_fx, exp );
    2631         450 :         exp1 = norm_l( L_and( blen, 0x00007fff ) );
    2632         450 :         L_tmp1 = L_shl( blen, exp1 );
    2633         450 :         logic16();
    2634         450 :         mean_vector_fx[k] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2635         450 :         move32(); /*31 + 20 + exp1 - exp2*/
    2636         450 :         exp = add( 51, sub( exp, exp1 ) );
    2637         450 :         mean_vector_fx[k] = L_shl( mean_vector_fx[k], sub( 20, exp ) );
    2638         450 :         move32(); /*Q12->Q20*/
    2639             :     }
    2640             : 
    2641             :     /* dynamics control */
    2642         500 :     FOR( k = 0; k < nband_lf; ++k )
    2643             :     {
    2644         450 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2645        7650 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    2646             :         {
    2647        7200 :             L_tmp = L_sub( coeff_out1_fx[i], mean_vector_fx[k] );
    2648        7200 :             exp = norm_l( L_tmp );
    2649        7200 :             L_tmp = L_shl( L_tmp, exp ); /* exp+12*/
    2650        7200 :             exp1 = norm_l( rn_weight0_fx );
    2651        7200 :             L_tmp1 = L_shl( rn_weight0_fx, exp1 ); /* exp1+10*/
    2652        7200 :             L_tmp = L_mult( extract_h( L_tmp ), extract_h( L_tmp1 ) );
    2653        7200 :             L_tmp = L_shr( L_tmp, add( exp, sub( exp1, 21 ) ) ); /* Q20*/
    2654        7200 :             coeff_out1_fx[i] = L_sub( coeff_out1_fx[i], L_tmp );
    2655        7200 :             move32();
    2656             :         }
    2657             :     }
    2658             : 
    2659          50 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    2660             :     {
    2661          25 :         bwe_seed = add( add( shl( R[0], 3 ), shl( R[1], 2 ) ), add( shl( R[2], 1 ), R[3] ) );
    2662             : 
    2663        3625 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2664             :         {
    2665        3600 :             IF( signum[n_freq] < 0 )
    2666             :             {
    2667        1831 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2668        1831 :                 move32();
    2669             :             }
    2670             : 
    2671        3600 :             IF( Random( &bwe_seed ) < 0 )
    2672             :             {
    2673        1816 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2674        1816 :                 move32();
    2675             :             }
    2676             :         }
    2677             :     }
    2678             :     ELSE
    2679             :     {
    2680        3625 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2681             :         {
    2682        3600 :             IF( signum[n_freq] < 0 )
    2683             :             {
    2684        1808 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2685        1808 :                 move32();
    2686             :             }
    2687             :         }
    2688             :     }
    2689             : 
    2690             :     /* normalizing modified low frequency spectrum */
    2691         500 :     FOR( k = 0; k < nband_lf; ++k )
    2692             :     {
    2693         450 :         energy_fx = 1;
    2694         450 :         move16();
    2695         450 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2696        7650 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    2697             :         {
    2698        7200 :             exp = norm_l( coeff_out1_fx[i] );
    2699        7200 :             L_tmp1 = L_shl( coeff_out1_fx[i], exp ); /* exp + 12*/
    2700             : 
    2701        7200 :             L_tmp = Mult_32_32( L_tmp1, L_tmp1 );
    2702        7200 :             L_tmp = L_shr( L_tmp, sub( i_mult2( 2, exp ), 11 ) ); /*Q20  */
    2703        7200 :             energy_fx = L_add( energy_fx, L_tmp );
    2704             :         }
    2705             : 
    2706         450 :         exp = norm_l( energy_fx );
    2707         450 :         L_tmp = L_shl( energy_fx, sub( exp, 1 ) );
    2708         450 :         exp1 = norm_s( blen );
    2709         450 :         L_tmp1 = L_shl( (Word32) blen, add( exp1, 16 ) );
    2710             : 
    2711         450 :         L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2712         450 :         exp = sub( add( 34, exp ), exp1 );
    2713         450 :         L_tmp = L_shr( L_tmp, sub( exp, 31 ) );
    2714         450 :         exp = 31;
    2715         450 :         move16();
    2716             : 
    2717         450 :         exp = sub( 31, exp );
    2718         450 :         IF( exp & 0x1 )
    2719             :         {
    2720           0 :             L_tmp = L_shr( L_tmp, 1 );
    2721           0 :             exp = add( exp, 1 );
    2722             :         }
    2723         450 :         L_tmp = Sqrt_l( L_tmp, &exp1 );
    2724         450 :         exp = add( 31, sub( shr( exp1, 1 ), shr( exp, 1 ) ) );
    2725         450 :         energy_fx = L_shl( L_tmp, sub( 31, exp ) ); /*Q31*/
    2726             : 
    2727         450 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2728        7650 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    2729             :         {
    2730        7200 :             IF( NE_32( L_abs( coeff_out1_fx[i] ), coeff_out1_fx[i] ) )
    2731             :             {
    2732        3627 :                 s = -1;
    2733        3627 :                 move16();
    2734        3627 :                 coeff_out1_fx[i] = L_abs( coeff_out1_fx[i] );
    2735        3627 :                 move32();
    2736             :             }
    2737             :             ELSE
    2738             :             {
    2739        3573 :                 s = 0;
    2740        3573 :                 move16();
    2741             :             }
    2742        7200 :             exp = norm_l( coeff_out1_fx[i] );
    2743        7200 :             L_tmp = L_shl( coeff_out1_fx[i], sub( exp, 1 ) );
    2744        7200 :             exp1 = norm_l( energy_fx );
    2745        7200 :             L_tmp1 = L_shl( (Word32) energy_fx, exp1 );
    2746        7200 :             logic16();
    2747        7200 :             L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2748        7200 :             exp = add( sub( 19, exp1 ), exp );
    2749        7200 :             coeff_out1_fx[i] = L_shl( L_tmp, add( sub( 12, exp ), 15 ) );
    2750        7200 :             move32(); /* Q12 -> Q27 */
    2751        7200 :             IF( s )
    2752             :             {
    2753        3627 :                 coeff_out1_fx[i] = L_negate( coeff_out1_fx[i] );
    2754        3627 :                 move32();
    2755             :             }
    2756             :         }
    2757             :     }
    2758             : 
    2759          50 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH0 + hq_generic_offset], HQ_GENERIC_LEN0 );
    2760          50 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset], HQ_GENERIC_LEN0 );
    2761             : 
    2762          50 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2763             :     {
    2764          50 :         Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0], &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset], HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
    2765             :     }
    2766             : 
    2767          50 :     IF( EQ_16( HQ_mode, HQ_GEN_FB ) )
    2768             :     {
    2769           0 :         IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2770             :         {
    2771           0 :             Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0 + HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    2772             :         }
    2773             :         ELSE
    2774             :         {
    2775           0 :             Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET + HQ_GENERIC_LEN0], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    2776             :         }
    2777             :     }
    2778             : 
    2779             : 
    2780          50 :     L_tmp1 = L_deposit_l( 0 );
    2781          50 :     L_tmp2 = L_deposit_l( 0 );
    2782         300 :     FOR( i = 0; i < 5; ++i )
    2783             :     {
    2784         250 :         L_tmp1 = L_add( L_tmp1, L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset + i] ) );
    2785         250 :         L_tmp2 = L_add( L_tmp2, L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 - 2 + hq_generic_offset - i] ) );
    2786             :     }
    2787             : 
    2788          50 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset];
    2789          50 :     L_tmp1 = L_max( L_tmp1, 1 );
    2790          50 :     L_tmp2 = L_max( L_tmp2, 1 );
    2791          50 :     exp1 = norm_l( L_tmp1 );
    2792          50 :     exp2 = sub( norm_l( L_tmp2 ), 1 );
    2793          50 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2794          50 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2795          50 :     tmp3_fx = div_s( tmp2_fx, tmp1_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    2796          50 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    2797             : 
    2798          50 :     if ( LT_16( tmp3_fx, 307 /*0.3 in Q10 */ ) )
    2799             :     {
    2800           0 :         tmp3_fx = 307 /*0.3 in Q10 */;
    2801           0 :         move16();
    2802             :     }
    2803          96 :     FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    2804             :     {
    2805          46 :         *pit1_fx = Mult_32_16( *pit1_fx, shl( tmp3_fx, 5 ) );
    2806          46 :         move32(); /*15 + 5 + 10 -15 */
    2807          46 :         pit1_fx++;
    2808             :     }
    2809             : 
    2810          50 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 - 1 + hq_generic_offset];
    2811             : 
    2812          50 :     exp1 = sub( norm_l( L_tmp1 ), 1 );
    2813          50 :     exp2 = norm_l( L_tmp2 );
    2814          50 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2815          50 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2816          50 :     tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    2817          50 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    2818             : 
    2819          50 :     IF( GT_16( tmp3_fx, 5120 ) )
    2820             :     {
    2821           0 :         FOR( tmp3_fx = 5120; tmp3_fx > 1024; tmp3_fx -= 512 )
    2822             :         {
    2823             :             /* Adding saturation suggested as fix for issue #957 */
    2824           0 :             L_tmp1 = L_shl_sat( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    2825           0 :             *pit1_fx-- = L_tmp1;
    2826           0 :             move32();
    2827             :         }
    2828             :     }
    2829             : 
    2830          50 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2831             :     {
    2832          50 :         L_tmp1 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + 1 + hq_generic_offset] ) );
    2833          50 :         L_tmp2 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 4 + hq_generic_offset] ), L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 3 + hq_generic_offset] ),
    2834          50 :                                                                                                 L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset] ) ) ) );
    2835             : 
    2836          50 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset];
    2837             : 
    2838          50 :         L_tmp1 = L_max( L_tmp1, 1 );
    2839          50 :         L_tmp2 = L_max( L_tmp2, 1 );
    2840          50 :         exp1 = norm_l( L_tmp1 );
    2841          50 :         exp2 = sub( norm_l( L_tmp2 ), 1 );
    2842          50 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2843          50 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2844          50 :         tmp3_fx = div_s( tmp2_fx, tmp1_fx );                       /*15 + exp2 + 15 - (exp1 + 15) */
    2845          50 :         tmp3_fx = shr_sat( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    2846          50 :         if ( LT_16( tmp3_fx, 307 /* 0.3 in Q10*/ ) )
    2847             :         {
    2848           0 :             tmp3_fx = 307; /* 0.3 in Q10*/
    2849           0 :             move16();
    2850             :         }
    2851          56 :         FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    2852             :         {
    2853           6 :             L_tmp = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    2854           6 :             *pit1_fx++ = L_tmp;
    2855           6 :             move32();
    2856             :         }
    2857             : 
    2858          50 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset];
    2859             : 
    2860          50 :         exp1 = sub( norm_l( L_tmp1 ), 1 );
    2861          50 :         exp2 = norm_l( L_tmp2 );
    2862          50 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2863          50 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2864          50 :         tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    2865          50 :         tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    2866          50 :         tmp3_fx = shr( tmp3_fx, 1 );
    2867          50 :         tmp4_fx = mult_r( tmp3_fx, 1638 /* 0.05 in Q15 */ );
    2868          50 :         WHILE( tmp3_fx > 1024 /* 1 in Q10*/ )
    2869             :         {
    2870           0 :             L_tmp1 = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    2871           0 :             *pit1_fx-- = L_tmp1;
    2872           0 :             move32();
    2873           0 :             tmp3_fx = sub( tmp3_fx, tmp4_fx );
    2874             :         }
    2875             :     }
    2876             : 
    2877             : 
    2878          50 :     wfenv_fx = hq_generic_fenv_fx[0];
    2879          50 :     move16(); /*1 */
    2880          50 :     i = 0;
    2881          50 :     move16();
    2882          50 :     tmp2 = add( add( swb_bwe_subband[0], hq_generic_offset ), 8 );
    2883         450 :     FOR( n_freq = add( swb_bwe_subband[0], hq_generic_offset ); n_freq < tmp2; n_freq++ )
    2884             :     {
    2885         400 :         factor_fx = shl( i, 12 );                             /*15 */
    2886         400 :         L_tmp1 = L_mult( sub( 32767, factor_fx ), fenvL_fx ); /*17 */
    2887         400 :         L_tmp2 = L_mult( factor_fx, wfenv_fx );               /*17 */
    2888         400 :         L_tmp1 = L_add( L_tmp1, L_tmp2 );                     /*17 */
    2889             : 
    2890         400 :         cs = norm_l( L_tmp1 );
    2891         400 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*17 + cs - 16 */
    2892         400 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 17 + cs - 16 - 15 */
    2893         400 :         coeff_out_fx[n_freq] = L_shr( L_tmp, add( 1, cs ) );
    2894         400 :         move32(); /*12 */
    2895         400 :         i++;
    2896         400 :         move16();
    2897             :     }
    2898             : 
    2899          50 :     k = sub( nenv, 2 );
    2900         650 :     FOR( n_band = 0; n_band < k; n_band++ )
    2901             :     {
    2902         600 :         wfenv_fx = hq_generic_fenv_fx[n_band + 1];
    2903         600 :         move16(); /*1 */
    2904             : 
    2905         600 :         tmp2 = swb_bwe_sm_subband[n_band + 1] + hq_generic_offset;
    2906       13800 :         FOR( i = 0; n_freq < tmp2; i++ )
    2907             :         {
    2908       13200 :             L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band] ), smooth_factor_fx[n_band] ); /*17 */
    2909       13200 :             L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                              /*17 + 10 - 15 */
    2910       13200 :             L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band], 11 ) );                        /*12 */
    2911             : 
    2912       13200 :             cs = norm_l( L_tmp1 );
    2913       13200 :             tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    2914       13200 :             L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    2915       13200 :             coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    2916       13200 :             move32(); /*12 */
    2917       13200 :             n_freq++;
    2918             :         }
    2919             :     }
    2920             : 
    2921          50 :     wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    2922          50 :     move16(); /*1 */
    2923          50 :     tmp2 = add( swb_bwe_sm_subband[nenv - 1], hq_generic_offset );
    2924        1650 :     FOR( i = 0; n_freq < tmp2; i++ )
    2925             :     {
    2926        1600 :         L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[nenv - 2] ), smooth_factor_fx[nenv - 2] ); /*17 */
    2927        1600 :         L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                                  /*17 + 10 - 15 */
    2928        1600 :         L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[nenv - 2], 11 ) );                          /*12 */
    2929             : 
    2930        1600 :         cs = norm_l( L_tmp1 );
    2931        1600 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    2932        1600 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    2933        1600 :         coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    2934        1600 :         move32(); /*12 */
    2935        1600 :         n_freq++;
    2936             :     }
    2937             : 
    2938          50 :     IF( EQ_16( HQ_mode, HQ_GEN_SWB ) )
    2939             :     {
    2940         100 :         FOR( n_band = sub( nenv, 1 ); n_band < nenv; ++n_band )
    2941             :         {
    2942          50 :             wfenv_fx = hq_generic_fenv_fx[n_band];
    2943          50 :             move16(); /*1 */
    2944          50 :             tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    2945         850 :             FOR( ; n_freq < tmp2; n_freq++ )
    2946             :             {
    2947         800 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    2948         800 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    2949         800 :                 move32(); /*12 */
    2950             :             }
    2951             :         }
    2952             :     }
    2953             :     ELSE
    2954             :     {
    2955           0 :         test();
    2956           0 :         IF( GT_16( sub( hq_generic_fenv_fx[nenv - 1], hq_generic_fenv_fx[nenv] ), 30 ) || LT_16( hq_generic_fenv_fx[nenv], 10 ) )
    2957             :         {
    2958           0 :             wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    2959           0 :             move16(); /*1 */
    2960           0 :             FOR( i = 0; n_freq < fb_bwe_subband[0]; i++ )
    2961             :             {
    2962           0 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    2963           0 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    2964           0 :                 move32(); /*12 */
    2965           0 :                 n_freq++;
    2966             :             }
    2967             : 
    2968           0 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    2969             :             {
    2970           0 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv];
    2971           0 :                 move16(); /*1 */
    2972           0 :                 tmp2 = fb_bwe_subband[n_band + 1];
    2973           0 :                 FOR( i = 0; n_freq < tmp2; i++ )
    2974             :                 {
    2975           0 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    2976           0 :                     coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    2977           0 :                     move32(); /*12 */
    2978           0 :                     n_freq++;
    2979             :                 }
    2980             :             }
    2981             :         }
    2982             :         ELSE
    2983             :         {
    2984           0 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    2985             :             {
    2986           0 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv - 1];
    2987           0 :                 move16(); /*1 */
    2988             : 
    2989           0 :                 FOR( i = 0; n_freq < fb_bwe_sm_subband[n_band]; i++ )
    2990             :                 {
    2991           0 :                     L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band + nenv] ), fb_smooth_factor_fx[n_band] ); /*17 */
    2992           0 :                     L_tmp1 = Mult_32_16( L_tmp1, shl( i, 9 ) );                                                         /*17 + 9 - 15 */
    2993           0 :                     L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band + nenv], 10 ) );                           /*11 */
    2994             : 
    2995           0 :                     cs = norm_l( L_tmp1 );
    2996           0 :                     tmp = extract_h( L_shl( L_tmp1, cs ) );          /*11 + cs - 16 */
    2997           0 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 11 + cs - 16 - 15 */
    2998           0 :                     coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 5, cs ) );
    2999           0 :                     move32(); /*12 */
    3000           0 :                     n_freq = add( n_freq, 1 );
    3001             :                 }
    3002             :             }
    3003             : 
    3004           0 :             wfenv_fx = hq_generic_fenv_fx[tenv - 1];
    3005           0 :             move16(); /*1 */
    3006             : 
    3007           0 :             FOR( ; n_freq < fb_bwe_subband[DIM_FB]; n_freq++ )
    3008             :             {
    3009           0 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3010           0 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3011           0 :                 move32(); /*12 */
    3012             :             }
    3013             :         }
    3014             :     }
    3015             : 
    3016          50 :     return;
    3017             : }
    3018             : 
    3019        2801 : void hq_generic_decoding_ivas_fx(
    3020             :     const Word16 HQ_mode,             /* i  : HQ mode                                      : Q0  */
    3021             :     Word32 *coeff_out1_fx,            /* i/o: BWE input & temporary buffer                 : Q12 */
    3022             :     const Word16 *hq_generic_fenv_fx, /* i  : SWB frequency envelopes                      : Q1  */
    3023             :     Word32 *coeff_out_fx,             /* o  : SWB signal in MDCT domain                    : Q12 */
    3024             :     const Word16 hq_generic_offset,   /* i  : frequency offset for representing hq generci : Q0  */
    3025             :     Word16 *prev_L_swb_norm,          /* i/o: last normalize length                        : Q0  */
    3026             :     const Word16 hq_generic_exc_clas, /* i  : bwe excitation class                         " Q0  */
    3027             :     const Word16 *R )
    3028             : {
    3029             :     Word16 i, n_freq, n_band, L_swb_norm;
    3030             :     Word16 k;
    3031             :     Word16 nenv;
    3032             :     Word16 tenv;
    3033             : 
    3034             :     Word16 exp, exp1, exp2, frac, tmp, tmp2, cs;
    3035             :     Word32 L_tmp, L_tmp1, L_tmp2, max_coeff_fx;
    3036             :     Word16 fenvL_fx, wfenv_fx, factor_fx;
    3037             :     Word32 *pit1_fx;
    3038             :     Word16 tmp1_fx, tmp2_fx, tmp3_fx, tmp4_fx;
    3039             :     Word32 energy_fx;
    3040             : 
    3041             :     Word32 envelope_fx[L_FRAME16k];
    3042             :     Word32 mean_vector_fx[20];
    3043             :     Word16 rn_weight0_fx;
    3044             :     Word16 s;
    3045             :     Word16 blen, nband_lf, sfidx, efidx;
    3046             :     Word16 bwe_seed;
    3047             :     Word16 signum[L_FRAME16k];
    3048             : 
    3049        2801 :     nenv = sub( SWB_FENV, 2 );
    3050        2801 :     if ( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3051             :     {
    3052        2801 :         nenv = SWB_FENV;
    3053        2801 :         move16();
    3054             :     }
    3055             : 
    3056        2801 :     tenv = nenv;
    3057        2801 :     move16();
    3058        2801 :     if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
    3059             :     {
    3060        2115 :         tenv = add( nenv, DIM_FB );
    3061             :     }
    3062             : 
    3063        2801 :     max_coeff_fx = 0;
    3064        2801 :     move16();
    3065        2801 :     tmp = add( swb_bwe_subband[0], hq_generic_offset );
    3066       47617 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp; n_freq++ )
    3067             :     {
    3068       44816 :         max_coeff_fx = L_max( max_coeff_fx, L_abs( coeff_out1_fx[n_freq] ) );
    3069             :     }
    3070        2801 :     cs = norm_l( max_coeff_fx );
    3071             : 
    3072        2801 :     L_tmp = 0;
    3073        2801 :     move16();
    3074        2801 :     tmp2 = add( swb_bwe_subband[0], hq_generic_offset );
    3075       47617 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp2; n_freq++ )
    3076             :     {
    3077       44816 :         tmp = extract_h( L_shl( coeff_out1_fx[n_freq], cs ) ); /*12 + cs - 16 */
    3078       44816 :         L_tmp1 = L_mult0( tmp, tmp );                          /*2*(cs-2) */
    3079       44816 :         L_tmp = L_add( L_tmp, L_shr( L_tmp1, 5 ) );            /*2*(cs-2) - 5 */
    3080             :     }
    3081        2801 :     cs = sub( shl( cs, 1 ), 9 );
    3082        2801 :     fenvL_fx = 0;
    3083        2801 :     move16();
    3084        2801 :     IF( L_tmp != 0 )
    3085             :     {
    3086        2801 :         exp = norm_l( L_tmp );
    3087        2801 :         frac = round_fx_sat( L_shl( L_tmp, exp ) ); /*cs+exp-16 */
    3088        2801 :         tmp = div_s( 16384, frac );                 /*15 + 14 - (cs+exp-16) */
    3089        2801 :         exp = sub( add( cs, exp ), 30 );
    3090        2801 :         L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                  /*Q31 - exp */
    3091        2801 :         fenvL_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /*Q1 */
    3092             :     }
    3093             : 
    3094        2801 :     calc_normal_length_fx_32( HQ_CORE, coeff_out1_fx, HQ_GEN_SWB, -1, &L_swb_norm, prev_L_swb_norm );
    3095             : 
    3096        2801 :     calc_norm_envelop_lf_fx( coeff_out1_fx, envelope_fx, &L_swb_norm, HQ_mode, hq_generic_offset, &sfidx, &efidx );
    3097             : 
    3098        2801 :     blen = 16;
    3099        2801 :     move16();
    3100             : 
    3101        2801 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    3102             :     {
    3103        2675 :         rn_weight0_fx = 819;
    3104        2675 :         move16(); /* 0.8 Q10 */
    3105             :     }
    3106         126 :     ELSE IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC1 ) )
    3107             :     {
    3108          14 :         rn_weight0_fx = 51;
    3109          14 :         move16(); /* 0.05 Q10*/
    3110             :     }
    3111             :     ELSE
    3112             :     {
    3113         112 :         rn_weight0_fx = 205;
    3114         112 :         move16(); /* 0.02 Q10 */
    3115             :     }
    3116             : 
    3117        2801 :     tmp = sub( efidx, sfidx );
    3118        2801 :     IF( tmp == 0 )
    3119             :     {
    3120           0 :         nband_lf = 0;
    3121           0 :         move16();
    3122             :     }
    3123             :     ELSE
    3124             :     {
    3125        2801 :         exp = norm_s( tmp );
    3126        2801 :         nband_lf = shl( tmp, sub( exp, 1 ) );
    3127        2801 :         exp1 = norm_s( blen );
    3128        2801 :         tmp = shl( blen, exp1 );
    3129        2801 :         nband_lf = shr( div_s( nband_lf, tmp ), add( sub( 14, exp1 ), exp ) ); /* 15 + exp-1 - exp1, Q0*/
    3130             :     }
    3131             : 
    3132      744545 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3133             :     {
    3134      741744 :         IF( coeff_out1_fx[n_freq] < 0 )
    3135             :         {
    3136      368886 :             signum[n_freq] = -1;
    3137      368886 :             move16();
    3138      368886 :             coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3139      368886 :             move32();
    3140             :         }
    3141             :         ELSE
    3142             :         {
    3143      372858 :             signum[n_freq] = 1;
    3144      372858 :             move16();
    3145             :         }
    3146             :     }
    3147             : 
    3148             :     /* applying whitening */
    3149      744545 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3150             :     {
    3151      741744 :         exp = norm_l( coeff_out1_fx[n_freq] ) - 1;
    3152      741744 :         exp1 = norm_l( envelope_fx[n_freq] );
    3153             : 
    3154      741744 :         L_tmp = L_shl( coeff_out1_fx[n_freq], exp );
    3155      741744 :         L_tmp1 = L_shl( envelope_fx[n_freq], exp1 );
    3156             : 
    3157      741744 :         logic16();
    3158      741744 :         coeff_out1_fx[n_freq] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3159      741744 :         move32(); /*31 + exp1 - exp2*/
    3160             : 
    3161      741744 :         exp = add( 31, sub( exp, exp1 ) );
    3162      741744 :         coeff_out1_fx[n_freq] = L_shl( coeff_out1_fx[n_freq], sub( 20, exp ) );
    3163      741744 :         move32(); /*Q12->Q20*/
    3164             :     }
    3165             : 
    3166             :     /* mean vector generation for controlling dynamic range */
    3167       49160 :     FOR( k = 0; k < nband_lf; ++k )
    3168             :     {
    3169       46359 :         energy_fx = 1;
    3170       46359 :         move16();
    3171             : 
    3172       46359 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3173      788103 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    3174             :         {
    3175      741744 :             energy_fx = L_add( energy_fx, coeff_out1_fx[i] );
    3176             :         }
    3177       46359 :         exp = sub( norm_l( energy_fx ), 1 );
    3178       46359 :         L_tmp = L_shl( energy_fx, exp );
    3179       46359 :         exp1 = norm_l( L_and( blen, 0x00007fff ) );
    3180       46359 :         L_tmp1 = L_shl( blen, exp1 );
    3181       46359 :         logic16();
    3182       46359 :         mean_vector_fx[k] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3183       46359 :         move32(); /*31 + 20 + exp1 - exp2*/
    3184       46359 :         exp = add( 51, sub( exp, exp1 ) );
    3185       46359 :         mean_vector_fx[k] = L_shl( mean_vector_fx[k], sub( 20, exp ) );
    3186       46359 :         move32(); /*Q12->Q20*/
    3187             :     }
    3188             : 
    3189             :     /* dynamics control */
    3190       49160 :     FOR( k = 0; k < nband_lf; ++k )
    3191             :     {
    3192       46359 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3193      788103 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    3194             :         {
    3195      741744 :             L_tmp = L_sub( coeff_out1_fx[i], mean_vector_fx[k] );
    3196      741744 :             exp = norm_l( L_tmp );
    3197      741744 :             L_tmp = L_shl( L_tmp, exp ); /* exp+12*/
    3198      741744 :             exp1 = norm_l( rn_weight0_fx );
    3199      741744 :             L_tmp1 = L_shl( rn_weight0_fx, exp1 ); /* exp1+10*/
    3200      741744 :             L_tmp = L_mult( extract_h( L_tmp ), extract_h( L_tmp1 ) );
    3201      741744 :             L_tmp = L_shr( L_tmp, add( exp, sub( exp1, 21 ) ) ); /* Q20*/
    3202      741744 :             coeff_out1_fx[i] = L_sub( coeff_out1_fx[i], L_tmp );
    3203      741744 :             move32();
    3204             :         }
    3205             :     }
    3206             : 
    3207        2801 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    3208             :     {
    3209        2675 :         bwe_seed = add( add( shl( R[0], 3 ), shl( R[1], 2 ) ), add( shl( R[2], 1 ), R[3] ) );
    3210             : 
    3211      713315 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3212             :         {
    3213      710640 :             IF( signum[n_freq] < 0 )
    3214             :             {
    3215      353547 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3216      353547 :                 move32();
    3217             :             }
    3218             : 
    3219      710640 :             IF( Random( &bwe_seed ) < 0 )
    3220             :             {
    3221      352595 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3222      352595 :                 move32();
    3223             :             }
    3224             :         }
    3225             :     }
    3226             :     ELSE
    3227             :     {
    3228       31230 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3229             :         {
    3230       31104 :             IF( signum[n_freq] < 0 )
    3231             :             {
    3232       15339 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3233       15339 :                 move32();
    3234             :             }
    3235             :         }
    3236             :     }
    3237             : 
    3238             :     /* normalizing modified low frequency spectrum */
    3239       49160 :     FOR( k = 0; k < nband_lf; ++k )
    3240             :     {
    3241       46359 :         energy_fx = 1;
    3242       46359 :         move16();
    3243       46359 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3244      788103 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    3245             :         {
    3246      741744 :             exp = norm_l( coeff_out1_fx[i] );
    3247      741744 :             L_tmp1 = L_shl( coeff_out1_fx[i], exp ); /* exp + 12*/
    3248             : 
    3249      741744 :             L_tmp = Mult_32_32( L_tmp1, L_tmp1 );
    3250      741744 :             L_tmp = L_shr( L_tmp, sub( i_mult2( 2, exp ), 11 ) ); /*Q20  */
    3251      741744 :             energy_fx = L_add( energy_fx, L_tmp );
    3252             :         }
    3253             : 
    3254       46359 :         exp = norm_l( energy_fx );
    3255       46359 :         L_tmp = L_shl( energy_fx, sub( exp, 1 ) );
    3256       46359 :         exp1 = norm_s( blen );
    3257       46359 :         L_tmp1 = L_shl( (Word32) blen, add( exp1, 16 ) );
    3258             : 
    3259       46359 :         L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3260       46359 :         exp = sub( add( 34, exp ), exp1 );
    3261       46359 :         L_tmp = L_shr( L_tmp, sub( exp, 31 ) );
    3262       46359 :         exp = 31;
    3263       46359 :         move16();
    3264             : 
    3265       46359 :         exp = sub( 31, exp );
    3266       46359 :         IF( exp & 0x1 )
    3267             :         {
    3268           0 :             L_tmp = L_shr( L_tmp, 1 );
    3269           0 :             exp = add( exp, 1 );
    3270             :         }
    3271       46359 :         L_tmp = Sqrt_l( L_tmp, &exp1 );
    3272       46359 :         exp = add( 31, sub( shr( exp1, 1 ), shr( exp, 1 ) ) );
    3273       46359 :         energy_fx = L_shl( L_tmp, sub( 31, exp ) ); /*Q31*/
    3274             : 
    3275       46359 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3276      788103 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    3277             :         {
    3278      741744 :             IF( NE_32( L_abs( coeff_out1_fx[i] ), coeff_out1_fx[i] ) )
    3279             :             {
    3280      370445 :                 s = -1;
    3281      370445 :                 move16();
    3282      370445 :                 coeff_out1_fx[i] = L_abs( coeff_out1_fx[i] );
    3283      370445 :                 move32();
    3284             :             }
    3285             :             ELSE
    3286             :             {
    3287      371299 :                 s = 0;
    3288      371299 :                 move16();
    3289             :             }
    3290      741744 :             exp = norm_l( coeff_out1_fx[i] );
    3291      741744 :             L_tmp = L_shl( coeff_out1_fx[i], sub( exp, 1 ) );
    3292      741744 :             exp1 = norm_l( energy_fx );
    3293      741744 :             L_tmp1 = L_shl( (Word32) energy_fx, exp1 );
    3294      741744 :             logic16();
    3295      741744 :             L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3296      741744 :             exp = add( sub( 19, exp1 ), exp );
    3297      741744 :             coeff_out1_fx[i] = L_shl( L_tmp, add( sub( 12, exp ), 15 ) );
    3298      741744 :             move32(); /* Q12 -> Q27 */
    3299      741744 :             IF( s )
    3300             :             {
    3301      370445 :                 coeff_out1_fx[i] = L_negate( coeff_out1_fx[i] );
    3302      370445 :                 move32();
    3303             :             }
    3304             :         }
    3305             :     }
    3306             : 
    3307        2801 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH0 + hq_generic_offset], HQ_GENERIC_LEN0 );
    3308        2801 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset], HQ_GENERIC_LEN0 );
    3309             : 
    3310        2801 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3311             :     {
    3312        2801 :         Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0], &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset], HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
    3313             :     }
    3314             : 
    3315        2801 :     IF( EQ_16( HQ_mode, HQ_GEN_FB ) )
    3316             :     {
    3317        2115 :         IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3318             :         {
    3319        2115 :             Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0 + HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    3320             :         }
    3321             :         ELSE
    3322             :         {
    3323           0 :             Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET + HQ_GENERIC_LEN0], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    3324             :         }
    3325             :     }
    3326             : 
    3327             : 
    3328        2801 :     L_tmp1 = L_deposit_l( 0 );
    3329        2801 :     L_tmp2 = L_deposit_l( 0 );
    3330       16806 :     FOR( i = 0; i < 5; ++i )
    3331             :     {
    3332       14005 :         L_tmp1 = L_add( L_tmp1, L_shr( L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset + i] ), 3 ) );     // adding guard bits
    3333       14005 :         L_tmp2 = L_add( L_tmp2, L_shr( L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 - 2 + hq_generic_offset - i] ), 3 ) ); // adding guard bits
    3334             :     }
    3335             : 
    3336        2801 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset];
    3337        2801 :     L_tmp1 = L_max( L_tmp1, 1 );
    3338        2801 :     L_tmp2 = L_max( L_tmp2, 1 );
    3339        2801 :     exp1 = norm_l( L_tmp1 );
    3340        2801 :     exp2 = sub( norm_l( L_tmp2 ), 1 );
    3341        2801 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3342        2801 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3343        2801 :     tmp3_fx = div_s( tmp2_fx, tmp1_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    3344        2801 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    3345             : 
    3346        2801 :     if ( LT_16( tmp3_fx, 307 /*0.3 in Q10 */ ) )
    3347             :     {
    3348           0 :         tmp3_fx = 307 /*0.3 in Q10 */;
    3349           0 :         move16();
    3350             :     }
    3351        5136 :     FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    3352             :     {
    3353        2335 :         *pit1_fx = Mult_32_16( *pit1_fx, shl( tmp3_fx, 5 ) );
    3354        2335 :         move32(); /*15 + 5 + 10 -15 */
    3355        2335 :         pit1_fx++;
    3356             :     }
    3357             : 
    3358        2801 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 - 1 + hq_generic_offset];
    3359             : 
    3360        2801 :     exp1 = sub( norm_l( L_tmp1 ), 1 );
    3361        2801 :     exp2 = norm_l( L_tmp2 );
    3362        2801 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3363        2801 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3364        2801 :     tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    3365        2801 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    3366             : 
    3367        2801 :     IF( GT_16( tmp3_fx, 5120 ) )
    3368             :     {
    3369           0 :         FOR( tmp3_fx = 5120; tmp3_fx > 1024; tmp3_fx -= 512 )
    3370             :         {
    3371             :             /* Adding saturation suggested as fix for issue #957 */
    3372           0 :             L_tmp1 = L_shl_sat( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    3373           0 :             *pit1_fx-- = L_tmp1;
    3374           0 :             move32();
    3375             :         }
    3376             :     }
    3377             : 
    3378        2801 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3379             :     {
    3380        2801 :         L_tmp1 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + 1 + hq_generic_offset] ) );
    3381        2801 :         L_tmp2 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 4 + hq_generic_offset] ), L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 3 + hq_generic_offset] ),
    3382        2801 :                                                                                                 L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset] ) ) ) );
    3383             : 
    3384        2801 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset];
    3385             : 
    3386        2801 :         L_tmp1 = L_max( L_tmp1, 1 );
    3387        2801 :         L_tmp2 = L_max( L_tmp2, 1 );
    3388        2801 :         exp1 = norm_l( L_tmp1 );
    3389        2801 :         exp2 = sub( norm_l( L_tmp2 ), 1 );
    3390        2801 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3391        2801 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3392        2801 :         tmp3_fx = div_s( tmp2_fx, tmp1_fx );                       /*15 + exp2 + 15 - (exp1 + 15) */
    3393        2801 :         tmp3_fx = shr_sat( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    3394        2801 :         if ( LT_16( tmp3_fx, 307 /* 0.3 in Q10*/ ) )
    3395             :         {
    3396           0 :             tmp3_fx = 307; /* 0.3 in Q10*/
    3397           0 :             move16();
    3398             :         }
    3399        2802 :         FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    3400             :         {
    3401           1 :             L_tmp = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    3402           1 :             *pit1_fx++ = L_tmp;
    3403           1 :             move32();
    3404             :         }
    3405             : 
    3406        2801 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset];
    3407             : 
    3408        2801 :         exp1 = sub( norm_l( L_tmp1 ), 1 );
    3409        2801 :         exp2 = norm_l( L_tmp2 );
    3410        2801 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3411        2801 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3412        2801 :         tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    3413        2801 :         tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    3414        2801 :         tmp3_fx = shr( tmp3_fx, 1 );
    3415        2801 :         tmp4_fx = mult_r( tmp3_fx, 1638 /* 0.05 in Q15 */ );
    3416        2801 :         WHILE( tmp3_fx > 1024 /* 1 in Q10*/ )
    3417             :         {
    3418           0 :             L_tmp1 = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    3419           0 :             *pit1_fx-- = L_tmp1;
    3420           0 :             move32();
    3421           0 :             tmp3_fx = sub( tmp3_fx, tmp4_fx );
    3422             :         }
    3423             :     }
    3424             : 
    3425             : 
    3426        2801 :     wfenv_fx = hq_generic_fenv_fx[0];
    3427        2801 :     move16(); /*1 */
    3428        2801 :     i = 0;
    3429        2801 :     move16();
    3430        2801 :     tmp2 = add( add( swb_bwe_subband[0], hq_generic_offset ), 8 );
    3431       25209 :     FOR( n_freq = add( swb_bwe_subband[0], hq_generic_offset ); n_freq < tmp2; n_freq++ )
    3432             :     {
    3433       22408 :         factor_fx = shl( i, 12 );                             /*15 */
    3434       22408 :         L_tmp1 = L_mult( sub( 32767, factor_fx ), fenvL_fx ); /*17 */
    3435       22408 :         L_tmp2 = L_mult( factor_fx, wfenv_fx );               /*17 */
    3436       22408 :         L_tmp1 = L_add( L_tmp1, L_tmp2 );                     /*17 */
    3437             : 
    3438       22408 :         cs = norm_l( L_tmp1 );
    3439       22408 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*17 + cs - 16 */
    3440       22408 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 17 + cs - 16 - 15 */
    3441       22408 :         coeff_out_fx[n_freq] = L_shr( L_tmp, add( 1, cs ) );
    3442       22408 :         move32(); /*12 */
    3443       22408 :         i++;
    3444       22408 :         move16();
    3445             :     }
    3446             : 
    3447        2801 :     k = sub( nenv, 2 );
    3448       36413 :     FOR( n_band = 0; n_band < k; n_band++ )
    3449             :     {
    3450       33612 :         wfenv_fx = hq_generic_fenv_fx[n_band + 1];
    3451       33612 :         move16(); /*1 */
    3452             : 
    3453       33612 :         tmp2 = swb_bwe_sm_subband[n_band + 1] + hq_generic_offset;
    3454      773076 :         FOR( i = 0; n_freq < tmp2; i++ )
    3455             :         {
    3456      739464 :             L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band] ), smooth_factor_fx[n_band] ); /*17 */
    3457      739464 :             L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                              /*17 + 10 - 15 */
    3458      739464 :             L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band], 11 ) );                        /*12 */
    3459             : 
    3460      739464 :             cs = norm_l( L_tmp1 );
    3461      739464 :             tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    3462      739464 :             L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    3463      739464 :             coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    3464      739464 :             move32(); /*12 */
    3465      739464 :             n_freq++;
    3466             :         }
    3467             :     }
    3468             : 
    3469        2801 :     wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    3470        2801 :     move16(); /*1 */
    3471        2801 :     tmp2 = add( swb_bwe_sm_subband[nenv - 1], hq_generic_offset );
    3472       92433 :     FOR( i = 0; n_freq < tmp2; i++ )
    3473             :     {
    3474       89632 :         L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[nenv - 2] ), smooth_factor_fx[nenv - 2] ); /*17 */
    3475       89632 :         L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                                  /*17 + 10 - 15 */
    3476       89632 :         L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[nenv - 2], 11 ) );                          /*12 */
    3477             : 
    3478       89632 :         cs = norm_l( L_tmp1 );
    3479       89632 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    3480       89632 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    3481       89632 :         coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    3482       89632 :         move32(); /*12 */
    3483       89632 :         n_freq++;
    3484             :     }
    3485             : 
    3486        2801 :     IF( EQ_16( HQ_mode, HQ_GEN_SWB ) )
    3487             :     {
    3488        1372 :         FOR( n_band = sub( nenv, 1 ); n_band < nenv; ++n_band )
    3489             :         {
    3490         686 :             wfenv_fx = hq_generic_fenv_fx[n_band];
    3491         686 :             move16(); /*1 */
    3492         686 :             tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    3493       11662 :             FOR( ; n_freq < tmp2; n_freq++ )
    3494             :             {
    3495       10976 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3496       10976 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3497       10976 :                 move32(); /*12 */
    3498             :             }
    3499             :         }
    3500             :     }
    3501             :     ELSE
    3502             :     {
    3503        2115 :         test();
    3504        2115 :         IF( GT_16( sub( hq_generic_fenv_fx[nenv - 1], hq_generic_fenv_fx[nenv] ), 30 ) || LT_16( hq_generic_fenv_fx[nenv], 10 ) )
    3505             :         {
    3506        1642 :             wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    3507        1642 :             move16(); /*1 */
    3508       27914 :             FOR( i = 0; n_freq < fb_bwe_subband[0]; i++ )
    3509             :             {
    3510       26272 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3511       26272 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3512       26272 :                 move32(); /*12 */
    3513       26272 :                 n_freq++;
    3514             :             }
    3515             : 
    3516        6568 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    3517             :             {
    3518        4926 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv];
    3519        4926 :                 move16(); /*1 */
    3520        4926 :                 tmp2 = fb_bwe_subband[n_band + 1];
    3521      267646 :                 FOR( i = 0; n_freq < tmp2; i++ )
    3522             :                 {
    3523      262720 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3524      262720 :                     coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3525      262720 :                     move32(); /*12 */
    3526      262720 :                     n_freq++;
    3527             :                 }
    3528             :             }
    3529             :         }
    3530             :         ELSE
    3531             :         {
    3532        1892 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    3533             :             {
    3534        1419 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv - 1];
    3535        1419 :                 move16(); /*1 */
    3536             : 
    3537       65747 :                 FOR( i = 0; n_freq < fb_bwe_sm_subband[n_band]; i++ )
    3538             :                 {
    3539       64328 :                     L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band + nenv] ), fb_smooth_factor_fx[n_band] ); /*17 */
    3540       64328 :                     L_tmp1 = Mult_32_16( L_tmp1, shl( i, 9 ) );                                                         /*17 + 9 - 15 */
    3541       64328 :                     L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band + nenv], 10 ) );                           /*11 */
    3542             : 
    3543       64328 :                     cs = norm_l( L_tmp1 );
    3544       64328 :                     tmp = extract_h( L_shl( L_tmp1, cs ) );          /*11 + cs - 16 */
    3545       64328 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 11 + cs - 16 - 15 */
    3546       64328 :                     coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 5, cs ) );
    3547       64328 :                     move32(); /*12 */
    3548       64328 :                     n_freq = add( n_freq, 1 );
    3549             :                 }
    3550             :             }
    3551             : 
    3552         473 :             wfenv_fx = hq_generic_fenv_fx[tenv - 1];
    3553         473 :             move16(); /*1 */
    3554             : 
    3555       19393 :             FOR( ; n_freq < fb_bwe_subband[DIM_FB]; n_freq++ )
    3556             :             {
    3557       18920 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3558       18920 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3559       18920 :                 move32(); /*12 */
    3560             :             }
    3561             :         }
    3562             :     }
    3563             : 
    3564        2801 :     return;
    3565             : }
    3566             : 
    3567             : 
    3568             : /*-------------------------------------------------------------------*
    3569             :  * save_old_syn()
    3570             :  *
    3571             :  * Save and delay the ACELP core synthesis signal by
    3572             :  * DELAY_FD_BWE_ENC_xxkx to be used by SWB BWE
    3573             :  *-------------------------------------------------------------------*/
    3574             : 
    3575      157719 : void save_old_syn_fx(
    3576             :     const Word16 L_frame,     /* i  : frame length                */
    3577             :     const Word16 syn[],       /* i  : ACELP synthesis             */
    3578             :     Word16 old_syn[],         /* o  : old synthesis buffer        */
    3579             :     Word16 old_syn_mem[],     /* i/o: old synthesis buffer memory */
    3580             :     const Word16 preemph_fac, /* i  : preemphasis factor          */
    3581             :     Word16 *mem_deemph        /* i/o: deemphasis filter memory    */
    3582             : )
    3583             : {
    3584             :     Word16 tmps;
    3585             : 
    3586      157719 :     tmps = NS2SA( 16000, DELAY_FD_BWE_ENC_16k_NS );
    3587      157719 :     move16();
    3588      157719 :     if ( EQ_16( L_frame, L_FRAME ) )
    3589             :     {
    3590       81745 :         tmps = NS2SA( 12800, DELAY_FD_BWE_ENC_12k8_NS );
    3591       81745 :         move16();
    3592             :     }
    3593      157719 :     Copy( old_syn_mem, old_syn, tmps );
    3594      157719 :     Copy( syn, old_syn + tmps, sub( L_frame, tmps ) );
    3595      157719 :     Copy( syn + L_frame - tmps, old_syn_mem, tmps );
    3596             : 
    3597      157719 :     deemph_fx( old_syn, preemph_fac, L_frame, mem_deemph );
    3598             : 
    3599      157719 :     return;
    3600             : }

Generated by: LCOV version 1.14