LCOV - code coverage report
Current view: top level - lib_com - swb_bwe_com_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ a878b04bf8eaca781b28fa146981883bca858fc4 Lines: 1734 1913 90.6 %
Date: 2025-07-06 02:38:44 Functions: 15 16 93.8 %

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

Generated by: LCOV version 1.14