LCOV - code coverage report
Current view: top level - lib_com - swb_bwe_com_lr_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 633e3f2e309758d10805ef21e0436356fe719b7a Lines: 711 1600 44.4 %
Date: 2025-08-23 01:22:27 Functions: 18 27 66.7 %

          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 "cnst.h"
      36             : #include "prot_fx.h"
      37             : #include "rom_com.h"
      38             : 
      39             : #include <assert.h>
      40             : 
      41             : /*-------------------------------------------------------------------*
      42             :  * GetPredictedSignal()
      43             :  *
      44             :  * Routine for calculating the predicted signal
      45             :  *-------------------------------------------------------------------*/
      46         124 : void GetPredictedSignal_fx(
      47             :     const Word16 *predBuf_fx, /* i:  Q8     */
      48             :     Word32 *L_outBuf,         /* o:  Q9     */
      49             :     const Word16 lag_fx,      /* i:  Q0     */
      50             :     const Word16 fLen_fx,     /* i:  Q0     */
      51             :     const Word16 lagGains_fx, /* i:  Qgain  */
      52             :     const Word16 Qgain        /* i:  Q0     */
      53             : )
      54             : {
      55             :     Word16 i;
      56             :     const Word16 *p_predBuf;
      57             :     Word32 *p_L_outBuf;
      58             : 
      59         124 :     p_predBuf = predBuf_fx + lag_fx;
      60         124 :     p_L_outBuf = L_outBuf;
      61             : 
      62        9796 :     FOR( i = 0; i < fLen_fx; i++ )
      63             :     {
      64             :         /* Q8 x Q0 --> Q9, 9+7-16=Q0 */
      65        9672 :         *p_L_outBuf++ = L_shr( L_mult( *p_predBuf++, lagGains_fx ), Qgain );
      66        9672 :         move32();
      67             :     }
      68         124 : }
      69             : 
      70             : /*-------------------------------------------------------------------*
      71             :  * est_freq_har_decis_fx()
      72             :  *
      73             :  * Harmonic frequency decision matrix
      74             :  *-------------------------------------------------------------------*/
      75        1983 : static void est_freq_har_decis_fx(
      76             :     Word16 *har_freq_est1,  /* o: harmonic analysis 1 */
      77             :     Word16 *har_freq_est2,  /* o: harmonic analysis 2 */
      78             :     Word16 sharp,           /* i: pka-avg for group 1 */
      79             :     Word16 sharp1,          /* i: pka-avg for group 2 */
      80             :     Word16 hfe_est_countk1, /* i: group pks count 1   */
      81             :     Word16 hfe_est_countk2, /* i: group pks count 2   */
      82             :     Word16 k,               /* i: group count         */
      83             :     Word16 k1,              /* i:                     */
      84             :     Word16 k2,              /* i:                     */
      85             :     Word16 *prev_frm_hfe2   /* i: harmonic estimation */
      86             : )
      87             : {
      88        1983 :     Word16 temp_hfe2 = 0;
      89        1983 :     move16();
      90             :     Word16 har_freq_est2_2;
      91             :     Word16 prev_frm_hfe2_2;
      92             : 
      93        1983 :     IF( k != 0 )
      94             :     {
      95        1983 :         *har_freq_est1 = div_s_ss( sharp, k );
      96        1983 :         move16();
      97             :     }
      98             : 
      99        1983 :     test();
     100        1983 :     test();
     101        1983 :     IF( GT_16( k1, 1 ) )
     102             :     {
     103         817 :         *har_freq_est2 = div_s_ss( sharp1, k1 );
     104         817 :         move16();
     105             :     }
     106        1166 :     ELSE IF( LT_16( k1, 2 ) && ( k2 != 0 || GT_16( k, 1 ) ) )
     107             :     {
     108        1133 :         *har_freq_est2 = *har_freq_est1;
     109        1133 :         move16();
     110             :     }
     111             :     ELSE
     112             :     {
     113          33 :         test();
     114          33 :         test();
     115          33 :         test();
     116          33 :         IF( ( hfe_est_countk1 != 0 || hfe_est_countk2 != 0 ) && ( k1 == 0 && k2 == 0 ) )
     117             :         {
     118          29 :             *har_freq_est2 = ( *har_freq_est1 );
     119          29 :             move16();
     120             :         }
     121             :         ELSE
     122             :         {
     123           4 :             *har_freq_est2 = shl( *har_freq_est1, 1 );
     124           4 :             move16();
     125             :         }
     126             :     }
     127             : 
     128             :     /* Consider Estimation Error upto 200Hz */
     129        1983 :     test();
     130        1983 :     test();
     131        1983 :     test();
     132        1983 :     har_freq_est2_2 = shl( *har_freq_est2, 1 );
     133        1983 :     prev_frm_hfe2_2 = shl( *prev_frm_hfe2, 1 );
     134        1983 :     IF( *prev_frm_hfe2 != 0 && ( LT_16( abs_s( sub( *prev_frm_hfe2, *har_freq_est2 ) ), 10 ) || LT_16( abs_s( sub( *prev_frm_hfe2, har_freq_est2_2 ) ), 10 ) ) )
     135             :     {
     136           0 :         *har_freq_est2 = *prev_frm_hfe2;
     137           0 :         move16();
     138             :     }
     139        1983 :     ELSE IF( *prev_frm_hfe2 != 0 && LT_16( abs_s( sub( *har_freq_est2, prev_frm_hfe2_2 ) ), 10 ) )
     140             :     {
     141           0 :         *har_freq_est2 = prev_frm_hfe2_2;
     142           0 :         move16();
     143             :     }
     144             :     ELSE
     145             :     {
     146        1983 :         temp_hfe2 = shr( add( *prev_frm_hfe2, *har_freq_est2 ), 1 );
     147        1983 :         move16();
     148             : 
     149        1983 :         IF( LT_16( abs_s( sub( temp_hfe2, *prev_frm_hfe2 ) ), 2 ) )
     150             :         {
     151           0 :             temp_hfe2 = *prev_frm_hfe2;
     152           0 :             move16();
     153           0 :             *har_freq_est2 = temp_hfe2;
     154           0 :             move16();
     155             :         }
     156             :     }
     157             : 
     158        1983 :     test();
     159        1983 :     test();
     160        1983 :     if ( LT_16( *har_freq_est2, *har_freq_est1 ) && ( GT_16( k, 1 ) && LT_16( k1, 2 ) ) )
     161             :     {
     162           0 :         *har_freq_est2 = *har_freq_est1;
     163           0 :         move16();
     164             :     }
     165             : 
     166        1983 :     return;
     167             : }
     168             : 
     169             : /*--------------------------------------------------------------------------*
     170             :  * har_est_fx()
     171             :  *
     172             :  * Harmonic Structure analysis using LF spectrum
     173             :  *--------------------------------------------------------------------------*/
     174             : 
     175        1983 : Word16 har_est_fx(
     176             :     Word32 L_spectra[],                   /* i  : coded spectrum                 */
     177             :     Word16 N,                             /* i  : length of the desired spectrum */
     178             :     Word16 *har_freq_est1,                /* i/o: Estimation harmonics 1         */
     179             :     Word16 *har_freq_est2,                /* o  : Estimation harmonics 2         */
     180             :     Word16 *flag_dis,                     /* i/o: flag for BWE reconstruction    */
     181             :     Word16 *prev_frm_hfe2,                /* i/o: Estimated harmonic update      */
     182             :     const Word16 subband_search_offset[], /* i  : Subband Search range           */
     183             :     const Word16 sbWidth[],               /* i  : Subband Search range           */
     184             :     Word16 *prev_stab_hfe2                /* i/o: Estimated harmonic position    */
     185             : )
     186             : {
     187             :     Word32 L_peak;
     188             :     Word32 L_input_abs[L_FRAME32k], L_blk_peak[30];
     189             :     Word32 L_blk_peak_te[30];
     190             :     Word32 L_blk_peak_max;
     191             :     Word32 *p_L_blk_peak, *pm1_L_blk_peak;
     192             : 
     193             :     Word16 i, j, q, k, k1, k2;
     194             :     Word16 blk_end, blk_st;
     195             :     Word16 peak_pos, blk_peak_pos[30], diff_peak_pos[30], sharp, sharp1;
     196             :     Word16 min_har_pos;
     197             :     Word16 blk_peak_pos_te[30];
     198             :     Word16 temp;
     199             :     Word16 hfe_est_countk, hfe_est_countk1, hfe_est_countk2;
     200             :     Word16 r1, r2, r3;
     201             :     Word16 start_pos;
     202             :     Word16 blk_peak_pos_max;
     203             : 
     204             :     Word16 nlags, nlags_half, ct_hfsb2, sum_diff;
     205             :     Word16 blk_peak_pos_hfsb2[30], diff_peak_pos_hfsb2[30];
     206             :     Word16 rem_hfe2, q_diffpos_hfe2, diff_posmax_hfe2, q_diffpos_prevhfe2;
     207             : 
     208             :     Word16 blk_end_LEN;
     209             : 
     210             :     Word16 *p_blk_peak_pos, *pm1_blk_peak_pos;
     211             :     Word16 *pm1_diff_peak_pos;
     212             :     Word16 blk_peak_max_idx, blk_peak_pos_max_diff, diff_peak_pos_te[30];
     213             :     Word16 thr1, thr2;
     214             : 
     215        1983 :     set32_fx( L_input_abs, 0x0L, L_FRAME32k );
     216        1983 :     set32_fx( L_blk_peak, 0x0L, 30 );
     217        1983 :     set16_fx( blk_peak_pos, 0, 30 );
     218        1983 :     set16_fx( blk_peak_pos_te, 0, 30 );
     219             : 
     220        1983 :     rem_hfe2 = 0;
     221        1983 :     move16();
     222        1983 :     q_diffpos_hfe2 = 0;
     223        1983 :     move16();
     224        1983 :     diff_posmax_hfe2 = 0;
     225        1983 :     move16();
     226        1983 :     q_diffpos_prevhfe2 = 0;
     227        1983 :     move16();
     228             : 
     229        1983 :     set16_fx( diff_peak_pos, 0, 30 );
     230             : 
     231        1983 :     r1 = SWB_HAR_RAN1;
     232        1983 :     move16();
     233        1983 :     r2 = SWB_HAR_RAN2;
     234        1983 :     move16();
     235        1983 :     r3 = SWB_HAR_RAN3;
     236        1983 :     move16();
     237        1983 :     start_pos = r1;
     238        1983 :     move16();
     239             : 
     240             :     /* Copy the abs values of LF spectrum*/
     241      438243 :     FOR( i = start_pos; i < N; i++ )
     242             :     {
     243      436260 :         L_input_abs[i] = L_abs( L_spectra[i] );
     244      436260 :         move32();
     245             :     }
     246             : 
     247        1983 :     blk_end = div_s_ss( N, LR_BLK_LEN );
     248        1983 :     blk_st = div_s_ss( start_pos, LR_BLK_LEN );
     249             : 
     250             :     /*if( N/(LR_BLK_LEN) - blk_end > 0.0f) */
     251        1983 :     blk_end_LEN = i_mult( blk_end, LR_BLK_LEN );
     252        1983 :     if ( GT_16( N, blk_end_LEN ) )
     253             :     {
     254        1983 :         blk_end = add( blk_end, 1 );
     255             :     }
     256             : 
     257             :     /* initialization of over buffer for fractional point */
     258        1983 :     temp = i_mult( blk_end, LR_BLK_LEN );
     259        9915 :     FOR( i = N; i < temp; i++ )
     260             :     {
     261        7932 :         L_input_abs[i] = L_deposit_l( 0 );
     262        7932 :         move16();
     263             :     }
     264             : 
     265        1983 :     q = start_pos;
     266        1983 :     move16();
     267             : 
     268             :     /* Block Processing, to detect the spectral peaks*/
     269       29745 :     FOR( i = blk_st; i < blk_end; i++ )
     270             :     {
     271       27762 :         L_peak = L_deposit_l( 0 );
     272       27762 :         peak_pos = 0;
     273       27762 :         move16();
     274             : 
     275      471954 :         FOR( j = 0; j < LR_BLK_LEN; j++ )
     276             :         {
     277      444192 :             IF( GT_32( L_input_abs[q], L_peak ) )
     278             :             {
     279       69057 :                 L_peak = L_input_abs[q];
     280       69057 :                 move32();
     281       69057 :                 peak_pos = q;
     282       69057 :                 move16();
     283             :             }
     284             : 
     285      444192 :             test();
     286      444192 :             test();
     287      444192 :             test();
     288      444192 :             IF( GT_16( i, blk_st ) && L_input_abs[q] != 0x0L && EQ_32( L_input_abs[q], L_peak ) && LT_16( sub( peak_pos, blk_peak_pos[i - 1] ), LR_HLF_PK_BLK_LEN ) )
     289             :             {
     290       13185 :                 L_peak = L_input_abs[q];
     291       13185 :                 move32();
     292       13185 :                 peak_pos = q;
     293       13185 :                 move16();
     294             :             }
     295      444192 :             q = add( q, 1 );
     296             :         }
     297             : 
     298       27762 :         L_blk_peak[i] = L_peak;
     299       27762 :         move32();
     300       27762 :         blk_peak_pos[i] = peak_pos;
     301       27762 :         move16();
     302             :     }
     303             : 
     304        1983 :     p_L_blk_peak = &L_blk_peak[blk_st];
     305        1983 :     pm1_L_blk_peak = &L_blk_peak[sub( blk_st, 1 )];
     306        1983 :     p_blk_peak_pos = &blk_peak_pos[blk_st];
     307        1983 :     pm1_blk_peak_pos = &blk_peak_pos[sub( blk_st, 1 )];
     308       29745 :     FOR( i = blk_st; i < blk_end; i++ )
     309             :     {
     310       27762 :         IF( GT_16( i, blk_st ) )
     311             :         {
     312             : 
     313       25779 :             test();
     314       25779 :             IF( *p_blk_peak_pos != 0 && *pm1_blk_peak_pos != 0 )
     315             :             {
     316       18877 :                 IF( LT_16( sub( *p_blk_peak_pos, *pm1_blk_peak_pos ), LR_LOWBAND_DIF_PK_LEN ) )
     317             :                 {
     318        3176 :                     IF( GT_32( *p_L_blk_peak, *pm1_L_blk_peak ) )
     319             :                     {
     320        2023 :                         *pm1_L_blk_peak = L_deposit_l( 0 );
     321        2023 :                         move32();
     322        2023 :                         *pm1_blk_peak_pos = 0;
     323        2023 :                         move16();
     324             :                     }
     325             :                     ELSE
     326             :                     {
     327        1153 :                         *p_L_blk_peak = *pm1_L_blk_peak;
     328        1153 :                         move32();
     329        1153 :                         *p_blk_peak_pos = *pm1_blk_peak_pos;
     330        1153 :                         move16();
     331        1153 :                         *pm1_L_blk_peak = L_deposit_l( 0 );
     332        1153 :                         move32();
     333        1153 :                         *pm1_blk_peak_pos = 0;
     334        1153 :                         move16();
     335             :                     }
     336             :                 }
     337             :             }
     338             :         }
     339       27762 :         p_L_blk_peak++;
     340       27762 :         pm1_L_blk_peak++;
     341             : 
     342       27762 :         p_blk_peak_pos++;
     343       27762 :         pm1_blk_peak_pos++;
     344             :     }
     345             : 
     346             :     /* peak counts in each group */
     347        1983 :     j = 0;
     348        1983 :     move16();
     349        1983 :     hfe_est_countk = 0;
     350        1983 :     move16();
     351        1983 :     hfe_est_countk1 = 0;
     352        1983 :     move16();
     353        1983 :     hfe_est_countk2 = 0;
     354        1983 :     move16();
     355       29745 :     FOR( i = blk_st; i < blk_end; i++ )
     356             :     {
     357       27762 :         IF( blk_peak_pos[i] != 0 )
     358             :         {
     359       18551 :             blk_peak_pos_te[j] = blk_peak_pos[i];
     360       18551 :             move16();
     361       18551 :             IF( LT_16( blk_peak_pos[i], r2 ) )
     362             :             {
     363        6242 :                 hfe_est_countk = add( hfe_est_countk, 1 );
     364             :             }
     365       12309 :             ELSE IF( LT_16( blk_peak_pos[i], r3 ) )
     366             :             {
     367        5642 :                 hfe_est_countk1 = add( hfe_est_countk1, 1 );
     368             :             }
     369             :             ELSE
     370             :             {
     371        6667 :                 hfe_est_countk2 = add( hfe_est_countk2, 1 );
     372             :             }
     373       18551 :             L_blk_peak_te[j] = L_blk_peak[i];
     374       18551 :             move32();
     375       18551 :             j = add( j, 1 );
     376             :         }
     377             :     }
     378             : 
     379        1983 :     min_har_pos = SWB_HAR_RAN1;
     380        1983 :     move16();
     381        1983 :     temp = 0;
     382        1983 :     move16();
     383        1983 :     L_blk_peak_max = L_blk_peak_te[0];
     384        1983 :     move32();
     385        1983 :     blk_peak_pos_max = blk_peak_pos_te[0];
     386        1983 :     move16();
     387        1983 :     blk_peak_max_idx = 0;
     388        1983 :     move16();
     389             : 
     390        1983 :     pm1_diff_peak_pos = &diff_peak_pos[1 - 1];
     391       18551 :     FOR( i = 1; i < j; i++ )
     392             :     {
     393       16568 :         *pm1_diff_peak_pos = sub( blk_peak_pos_te[i], blk_peak_pos_te[sub( i, 1 )] );
     394       16568 :         if ( LE_16( *pm1_diff_peak_pos, min_har_pos ) )
     395             :         {
     396        6006 :             min_har_pos = *pm1_diff_peak_pos;
     397        6006 :             move16();
     398             :         }
     399             : 
     400       16568 :         IF( GT_32( L_blk_peak_te[sub( i, 1 )], L_blk_peak_max ) )
     401             :         {
     402        3249 :             L_blk_peak_max = L_blk_peak_te[sub( i, 1 )];
     403        3249 :             move32();
     404        3249 :             blk_peak_pos_max = blk_peak_pos_te[sub( i, 1 )];
     405        3249 :             move16();
     406        3249 :             blk_peak_max_idx = sub( i, 1 );
     407             :         }
     408             : 
     409       16568 :         temp = add( temp, 1 );
     410             : 
     411       16568 :         pm1_diff_peak_pos++;
     412             :     }
     413        1983 :     blk_peak_pos_max_diff = diff_peak_pos[blk_peak_max_idx];
     414        1983 :     move16();
     415             : 
     416             :     /* Decision for BWE reconstruction */
     417        1983 :     test();
     418        1983 :     test();
     419        1983 :     test();
     420        1983 :     IF( ( LT_16( hfe_est_countk, 2 ) && LT_16( hfe_est_countk1, 2 ) && LT_16( hfe_est_countk2, 2 ) ) || GE_16( min_har_pos, SWB_HAR_RAN1 ) )
     421             :     {
     422           0 :         *flag_dis = 0;
     423           0 :         move16();
     424           0 :         test();
     425           0 :         test();
     426           0 :         test();
     427           0 :         if ( ( EQ_16( hfe_est_countk, 1 ) && EQ_16( hfe_est_countk1, 1 ) ) && ( EQ_16( hfe_est_countk2, 1 ) || hfe_est_countk2 == 0 ) )
     428             :         {
     429           0 :             *flag_dis = 1;
     430           0 :             move16();
     431             :         }
     432             :     }
     433        1983 :     thr1 = add( blk_peak_pos_max_diff, LR_LOWBAND_DIF_PK_LEN );
     434       18551 :     FOR( i = 0; i < temp; i++ )
     435             :     {
     436       16568 :         if ( LT_16( thr1, diff_peak_pos[i] ) )
     437             :         {
     438        1731 :             diff_peak_pos[i] = 0;
     439        1731 :             move16();
     440             :         }
     441             :     }
     442        1983 :     Copy( diff_peak_pos, diff_peak_pos_te, temp );
     443        1983 :     set16_fx( diff_peak_pos, -1, temp );
     444        1983 :     j = 0;
     445       18551 :     FOR( i = 0; i < temp; i++ )
     446             :     {
     447       16568 :         IF( diff_peak_pos_te[i] != 0 )
     448             :         {
     449       14837 :             diff_peak_pos[j] = diff_peak_pos_te[i];
     450       14837 :             move16();
     451       14837 :             j = add( j, 1 );
     452             :         }
     453             :     }
     454        1983 :     temp = j;
     455        1983 :     move16();
     456             : 
     457             :     /* harmonic estimation analysis to perform BWE Reconstruction */
     458        1983 :     IF( *flag_dis )
     459             :     {
     460        1983 :         sharp = 0;
     461        1983 :         move16();
     462        1983 :         k = 0;
     463        1983 :         move16();
     464        1983 :         k1 = 0;
     465        1983 :         move16();
     466        1983 :         sharp1 = 0;
     467        1983 :         move16();
     468        1983 :         k2 = 0;
     469        1983 :         move16();
     470             : 
     471        1983 :         q = 1;
     472        1983 :         move16();
     473        1983 :         thr1 = add( min_har_pos, LR_LOWBAND_DIF_PK_LEN );
     474        1983 :         thr2 = add( min_har_pos, shl( LR_LOWBAND_DIF_PK_LEN, 1 ) );
     475       16820 :         FOR( i = 0; i < temp; i++ )
     476             :         {
     477       14837 :             test();
     478       14837 :             test();
     479       14837 :             IF( LE_16( diff_peak_pos[i], thr1 ) && diff_peak_pos[i] > 0 )
     480             :             {
     481       11749 :                 sharp = add( sharp, diff_peak_pos[i] );
     482       11749 :                 k = add( k, 1 );
     483             :             }
     484        3088 :             ELSE IF( LE_16( diff_peak_pos[i], thr2 ) && diff_peak_pos[i] > 0 )
     485             :             {
     486        2768 :                 sharp1 = add( sharp1, diff_peak_pos[i] );
     487        2768 :                 k1 = add( k1, 1 );
     488             :             }
     489         320 :             ELSE IF( diff_peak_pos[i] > 0 )
     490             :             {
     491         320 :                 k2 = add( k2, 1 );
     492             :             }
     493       14837 :             q = add( q, 1 );
     494             :         }
     495             : 
     496        1983 :         est_freq_har_decis_fx( har_freq_est1, har_freq_est2, sharp, sharp1, hfe_est_countk1, hfe_est_countk2, k, k1, k2, prev_frm_hfe2 );
     497             : 
     498        1983 :         blk_peak_pos_max = blk_peak_pos_te[sub( temp, 1 )];
     499        1983 :         move16();
     500             : 
     501        1983 :         test();
     502        1983 :         test();
     503        1983 :         IF( ( *prev_stab_hfe2 ) > 0 && ( *prev_frm_hfe2 ) > 0 && *prev_stab_hfe2 < N )
     504             :         {
     505           0 :             rem_hfe2 = sub( *har_freq_est2, extract_h( L_shl( L_mult( div_s_ss( *har_freq_est2, *prev_frm_hfe2 ), *prev_frm_hfe2 ), 15 ) ) );
     506           0 :             diff_posmax_hfe2 = abs_s( sub( blk_peak_pos_max, *prev_stab_hfe2 ) );
     507           0 :             IF( rem_hfe2 == 0 )
     508             :             {
     509           0 :                 test();
     510           0 :                 IF( LT_16( diff_posmax_hfe2, 9 ) || *har_freq_est2 == 0 )
     511             :                 {
     512           0 :                     blk_peak_pos_max = *prev_stab_hfe2;
     513           0 :                     move16();
     514             :                 }
     515             :                 ELSE
     516             :                 {
     517           0 :                     q_diffpos_hfe2 = div_s_ss( diff_posmax_hfe2, *har_freq_est2 );
     518           0 :                     q_diffpos_prevhfe2 = div_s_ss( diff_posmax_hfe2, *prev_frm_hfe2 );
     519           0 :                     test();
     520           0 :                     IF( LT_16( q_diffpos_hfe2, 10 ) || LT_16( q_diffpos_prevhfe2, 10 ) )
     521             :                     {
     522           0 :                         blk_peak_pos_max = *prev_stab_hfe2;
     523           0 :                         move16();
     524             :                     }
     525             :                     ELSE
     526             :                     {
     527           0 :                         *prev_stab_hfe2 = blk_peak_pos_max;
     528           0 :                         move16();
     529             :                     }
     530             :                 }
     531             :             }
     532             :             ELSE
     533             :             {
     534           0 :                 *prev_stab_hfe2 = blk_peak_pos_max;
     535           0 :                 move16();
     536             :             }
     537             :         }
     538             :         ELSE
     539             :         {
     540        1983 :             *prev_stab_hfe2 = blk_peak_pos_max;
     541        1983 :             move16();
     542             :         }
     543             : 
     544        1983 :         test();
     545        1983 :         if ( *har_freq_est1 == 0 || *har_freq_est2 == 0 )
     546             :         {
     547           0 :             *flag_dis = 0;
     548           0 :             move16();
     549             :         }
     550             :     }
     551             : 
     552        1983 :     IF( *flag_dis == 0 )
     553             :     {
     554           0 :         IF( *prev_frm_hfe2 != 0 )
     555             :         {
     556           0 :             *har_freq_est2 = *prev_frm_hfe2;
     557           0 :             move16();
     558             :         }
     559             :         ELSE
     560             :         {
     561           0 :             nlags = shl( 1, bits_lagIndices_mode0_Har[0] );
     562           0 :             nlags_half = shr( nlags, 1 );
     563           0 :             ct_hfsb2 = 0;
     564           0 :             move16();
     565           0 :             FOR( i = 0; i < j; i++ )
     566             :             {
     567           0 :                 test();
     568           0 :                 IF( GE_16( blk_peak_pos_te[i], sub( subband_search_offset[0], nlags_half ) ) &&
     569             :                     LT_16( blk_peak_pos_te[i], add( add( subband_search_offset[0], sbWidth[0] ), nlags_half ) ) )
     570             :                 {
     571           0 :                     blk_peak_pos_hfsb2[ct_hfsb2] = blk_peak_pos_te[i];
     572           0 :                     move16();
     573           0 :                     ct_hfsb2 = add( ct_hfsb2, 1 );
     574             :                 }
     575             :             }
     576             : 
     577           0 :             IF( GT_16( ct_hfsb2, 1 ) )
     578             :             {
     579           0 :                 sum_diff = 0;
     580           0 :                 move16();
     581           0 :                 FOR( i = 1; i < ct_hfsb2; i++ )
     582             :                 {
     583           0 :                     diff_peak_pos_hfsb2[i - 1] = sub( blk_peak_pos_hfsb2[i], blk_peak_pos_hfsb2[i - 1] );
     584           0 :                     move16();
     585           0 :                     sum_diff = add( sum_diff, diff_peak_pos_hfsb2[i - 1] );
     586             :                 }
     587           0 :                 *har_freq_est2 = div_s_ss( sum_diff, ct_hfsb2 );
     588           0 :                 move16();
     589             :             }
     590             :             ELSE
     591             :             {
     592           0 :                 *har_freq_est2 = min_har_pos;
     593           0 :                 move16();
     594             :             }
     595             :         }
     596             :     }
     597        1983 :     return blk_peak_pos_max;
     598             : }
     599             : 
     600           0 : void genhf_noise_fx(
     601             :     const Word16 noise_flr_fx[],
     602             :     /* i  : Qss smoothed non tonal                           */ /* sspectra_diff_fx:Qss */
     603             :     const Word16 Qss,                                           /* i  : Q0  Q value                                      */
     604             :     Word32 L_xSynth_har[],
     605             :     /* o  : QsL hf non tonal components                      */ /* xSynth_har:QsL */
     606             :     const Word16 QsL,                                           /* i  : Q0  Q value                                      */
     607             :     const Word16 *predBuf_fx,
     608             :     /* i  : Qss smoothed tonal compone                       */ /* sspectra:Qss */
     609             :     const Word16 bands,                                         /* i  : Q0  total number of subbands in a frame          */
     610             :     const Word16 harmonic_band,                                 /* i  : Q0  Number of LF harmonic frames                 */
     611             :     const Word16 har_freq_est2,                                 /* i  : Q0  harmonic signal parameter                    */
     612             :     const Word16 pos_max_hfe2,                                  /* i  : Q0  last pulse in core coder                     */
     613             :     Word16 *pul_res,                                            /* o  : Q0  pulse resolution                             */
     614             :     GainItem_fx pk_sf_fx[],                                     /* o  :     representative region                        */
     615             :     const Word16 fLenLow,                                       /* i  : Q0  low frequency length                         */
     616             :     const Word16 fLenHigh,                                      /* i  : Q0  high frequency length                        */
     617             :     const Word16 sbWidth[],                                     /* i  : Q0  bandwidth for high bands                     */
     618             :     const Word16 lagIndices[],                                  /* i  : Q0  correlation indices for most representative  */
     619             :     const Word16 subband_offsets[],                             /* i  : Q0  band offsets for HF reconstruction           */
     620             :     const Word16 subband_search_offset[]                        /* i  : Q0  most representative regions offsets in LF    */
     621             : )
     622             : {
     623             :     Word16 k, j, ii, st_pos, dst_pos;
     624             :     Word16 nlags[NB_SWB_SUBBANDS_HAR_SEARCH_SB];
     625             :     Word32 L_tmpbuf[L_FRAME32k];
     626             :     Word16 hfband_end[NB_SWB_SUBBANDS];
     627             :     Word16 rem_hfe, /*last_peakpos,*/ temp_last_peakpos, i, l, pos, res;
     628             :     Word16 hf_pulse_peaks_fx[160], pulse_peak_sb_fx[320]; /* Qss */
     629             :     Word16 st_last_peakpos;
     630             :     Word16 tmp_fx;
     631             : 
     632           0 :     set32_fx( L_tmpbuf, 0x0L, L_FRAME32k );
     633           0 :     FOR( k = 0; k < 3; k++ )
     634             :     {
     635           0 :         hfband_end[k] = add( fLenLow, subband_offsets[k + 1] );
     636           0 :         move16();
     637             :     }
     638           0 :     hfband_end[3] = add( fLenLow, fLenHigh );
     639           0 :     move16();
     640             : 
     641           0 :     tmp_fx = sub( sub( fLenLow, pos_max_hfe2 ), 1 );
     642           0 :     rem_hfe = div_s_ss( tmp_fx, har_freq_est2 );
     643             : 
     644           0 :     st_last_peakpos = add( pos_max_hfe2, i_mult( rem_hfe, har_freq_est2 ) );
     645             : 
     646           0 :     temp_last_peakpos = st_last_peakpos;
     647           0 :     move16();
     648           0 :     i = 0;
     649           0 :     move16();
     650             : 
     651           0 :     FOR( k = 0; k < 2; k++ )
     652             :     {
     653           0 :         nlags[k] = shl( 1, bits_lagIndices_mode0_Har[k] );
     654           0 :         move16();
     655           0 :         l = 0;
     656           0 :         move16();
     657           0 :         WHILE( LT_16( st_last_peakpos, add( fLenLow, subband_offsets[k] ) ) )
     658             :         {
     659           0 :             st_last_peakpos = add( st_last_peakpos, har_freq_est2 );
     660             :         }
     661           0 :         st_last_peakpos = sub( st_last_peakpos, har_freq_est2 );
     662             : 
     663           0 :         IF( k == 0 )
     664             :         {
     665           0 :             st_pos = add( sub( subband_search_offset[k], shr( nlags[k], 1 ) ), lagIndices[k] );
     666             : 
     667             :             /*Copy the LF Smoothed Noise to the HF*/
     668           0 :             FOR( j = 0; j < sbWidth[k]; j++ )
     669             :             {
     670           0 :                 L_xSynth_har[j] = L_shl( L_deposit_l( noise_flr_fx[st_pos + j] ), sub( QsL, Qss ) );
     671           0 :                 move32();
     672           0 :                 L_tmpbuf[j] = L_xSynth_har[j];
     673           0 :                 move32();
     674           0 :                 IF( predBuf_fx[st_pos + j] != 0x0 )
     675             :                 {
     676           0 :                     hf_pulse_peaks_fx[l] = predBuf_fx[st_pos + j];
     677           0 :                     move16(); /* Qss */
     678           0 :                     l = add( l, 1 );
     679             :                 }
     680             :             }
     681             :         }
     682             :         ELSE
     683             :         {
     684           0 :             st_pos = sub( add( subband_search_offset[k], shr( nlags[k], 1 ) ), lagIndices[k] );
     685           0 :             dst_pos = sub( st_pos, sbWidth[k] );
     686           0 :             ii = sbWidth[k - 1];
     687           0 :             move16();
     688             :             /*Copy the LF Smoothed Noise floor to the HF*/
     689           0 :             FOR( j = st_pos; j > ( dst_pos ); j-- )
     690             :             {
     691           0 :                 IF( GE_16( ii, add( sbWidth[k], sbWidth[k - 1] ) ) )
     692             :                 {
     693           0 :                     BREAK;
     694             :                 }
     695             : 
     696             :                 /*xSynth_har[ii] = noise_flr[j];*/
     697           0 :                 L_xSynth_har[ii] = L_shl( L_deposit_l( noise_flr_fx[j] ), sub( QsL, Qss ) );
     698           0 :                 move32();
     699           0 :                 L_tmpbuf[ii] = L_xSynth_har[ii];
     700           0 :                 move32();
     701           0 :                 IF( predBuf_fx[j] != 0x0 )
     702             :                 {
     703           0 :                     hf_pulse_peaks_fx[l] = predBuf_fx[j];
     704           0 :                     move16();
     705           0 :                     l = add( l, 1 );
     706             :                 }
     707           0 :                 ii = add( ii, 1 );
     708             :             }
     709             :         }
     710           0 :         pos = 0;
     711           0 :         move16();
     712           0 :         FOR( j = 0; j < l; j++ )
     713             :         {
     714           0 :             st_last_peakpos = add( st_last_peakpos, har_freq_est2 );
     715           0 :             IF( LT_16( st_last_peakpos, hfband_end[k] ) )
     716             :             {
     717           0 :                 pk_sf_fx[k * 8 + pos].nmrValue_fx = hf_pulse_peaks_fx[j];
     718           0 :                 move16(); /* Qss */
     719           0 :                 pk_sf_fx[k * 8 + pos].gainIndex_fx = sub( st_last_peakpos, fLenLow );
     720           0 :                 move16();
     721           0 :                 pul_res[k] = add( pul_res[k], 1 );
     722           0 :                 move16();
     723           0 :                 pulse_peak_sb_fx[i] = hf_pulse_peaks_fx[j];
     724           0 :                 move16(); /* Qss */
     725           0 :                 i = add( i, 1 );
     726           0 :                 pos = add( pos, 1 );
     727             :             }
     728             :         }
     729           0 :         st_last_peakpos = temp_last_peakpos;
     730           0 :         move16();
     731             :     }
     732           0 :     res = sub( i, 1 );
     733           0 :     l = 1;
     734           0 :     move16();
     735           0 :     ii = sub( sub( hfband_end[k - 1], fLenLow ), 1 );
     736           0 :     tmp_fx = sub( bands, harmonic_band );
     737           0 :     FOR( ; k < tmp_fx; k++ )
     738             :     {
     739             :         Word16 tmp2;
     740             : 
     741           0 :         tmp2 = ( sub( hfband_end[k], fLenLow ) );
     742           0 :         FOR( j = sub( hfband_end[k - 1], fLenLow ); j < tmp2; j++ )
     743             :         {
     744           0 :             L_xSynth_har[j] = L_tmpbuf[ii];
     745           0 :             move32();
     746           0 :             L_tmpbuf[j] = L_xSynth_har[j];
     747           0 :             move32();
     748           0 :             ii = sub( ii, 1 );
     749             :         }
     750           0 :         pos = 0;
     751           0 :         move16();
     752           0 :         WHILE( LT_16( st_last_peakpos, hfband_end[k - 1] ) )
     753             :         {
     754           0 :             st_last_peakpos = add( st_last_peakpos, har_freq_est2 );
     755             :         }
     756           0 :         test();
     757           0 :         test();
     758           0 :         WHILE( LT_16( st_last_peakpos, hfband_end[k] ) && LT_16( pul_res[k], pul_res[2 - l] ) && LE_16( l, 2 ) )
     759             :         {
     760           0 :             test();
     761           0 :             test();
     762           0 :             pk_sf_fx[k * 8 + pos].nmrValue_fx = pulse_peak_sb_fx[res];
     763           0 :             move16(); /* Qss */
     764           0 :             pk_sf_fx[k * 8 + pos].gainIndex_fx = sub( st_last_peakpos, fLenLow );
     765           0 :             move16();
     766           0 :             pul_res[k] = add( pul_res[k], 1 );
     767           0 :             move16();
     768           0 :             res = sub( res, 1 );
     769           0 :             pos = add( pos, 1 );
     770           0 :             st_last_peakpos = add( st_last_peakpos, har_freq_est2 );
     771             :         }
     772           0 :         l = add( l, 1 );
     773             :     }
     774             : 
     775           0 :     return;
     776             : }
     777             : 
     778             : /*-------------------------------------------------------------------*
     779             :  * SmoothSpec()
     780             :  *
     781             :  * Smoothes specified samples using moving average method. The number
     782             :  * of points in the average is given by 'span'. Note that current
     783             :  * implementation does not accept 'span' to be smaller than 'fLen'.
     784             :  *-------------------------------------------------------------------*/
     785           0 : static void SmoothSpec_fx(
     786             :     Word16 *inBuf,     /* i : Input spectrum       Q8 */
     787             :     Word16 *outBuf,    /* o : Smoothed spectrum    Q8 */
     788             :     Word16 num_subband /* i : subband number          */
     789             : )
     790             : {
     791             :     Word16 i, tmp;
     792             :     Word16 span1;            /* */
     793             :     Word16 nItems, inItems;  /* inverse */
     794             :     Word32 L_sum;            /* */
     795             :     Word16 *oldPtr, *newPtr; /* */
     796             :     Word16 hi, lo;
     797             :     /* ========  Q8  ======== */
     798           0 :     span1 = shr( MA_LEN, 1 );
     799             : 
     800             :     /*-- First sample. --*/
     801           0 :     L_sum = L_deposit_l( *inBuf );
     802           0 :     *outBuf++ = *inBuf;
     803           0 :     move16();
     804             : 
     805           0 :     oldPtr = inBuf;
     806           0 :     newPtr = inBuf + 2;
     807             : 
     808             :     /*-- Handle start. --*/
     809           0 :     inBuf++;
     810           0 :     L_sum = L_mac0( L_sum, 0x0001, *inBuf );
     811             : 
     812             :     /* nItems = 3 --> inItems = 1/3 = 0.33f, 85(Q8) */
     813             :     /* 1/3 = 0.3333f -> 0x2AAA Q15 */
     814           0 :     inItems = 0x2AAA;
     815           0 :     move16();
     816           0 :     FOR( i = 1; i < span1; i++ )
     817             :     {
     818           0 :         L_sum = L_mac0( L_sum, 0x0001, *newPtr++ );
     819             : 
     820           0 :         lo = L_Extract_lc( L_sum, &hi );
     821           0 :         *outBuf++ = round_fx( L_shl( Mpy_32_16( hi, lo, inItems ), 16 ) ); /* Q(8+15+1-16)=Q8 -> Q(8+16-16)=Q8 */
     822           0 :         move16();
     823           0 :         L_sum = L_mac0( L_sum, 0x0001, *newPtr++ );
     824             :         /* nItems += 2,
     825             :          * only used value is 5 -->
     826             :          * inItems = 1/5 = 0.2f, 51(Q8)
     827             :          */
     828             :         /* 1/5 = 0.2f -> 0x1999 Q15 */
     829           0 :         inItems = 0x1999;
     830           0 :         move16();
     831           0 :         inBuf++;
     832             :     }
     833             : 
     834           0 :     inBuf++;
     835           0 :     L_sum = L_mac0( L_sum, 0x0001, *newPtr++ );
     836             : 
     837           0 :     lo = L_Extract_lc( L_sum, &hi );
     838             :     /* 4681 (in Q15) = 0.1428 = 1/7 */
     839           0 :     *outBuf++ = round_fx( L_shl( Mpy_32_16( hi, lo, 4681 ), 16 ) ); /* Q(8+15+1-16)=Q8 -> Q(8+16-16)=Q8 */
     840           0 :     move16();
     841           0 :     i = add( i, 1 );
     842             : 
     843             :     /*-- Moving average. --*/
     844           0 :     tmp = sub( num_subband, span1 );
     845           0 :     FOR( ; i < tmp; i++ )
     846             :     {
     847           0 :         L_sum = L_mac0( L_sum, 0x0001, *newPtr++ );
     848           0 :         L_sum = L_msu0( L_sum, 0x0001, *oldPtr++ );
     849             : 
     850           0 :         lo = L_Extract_lc( L_sum, &hi );
     851             :         /* 4681 (in Q15) = 0.1428 = 1/7 */
     852           0 :         *outBuf++ = round_fx( L_shl( Mpy_32_16( hi, lo, 4681 ), 16 ) ); /* Q(8+15+1-16)=Q8 -> Q(8+16-16)=Q8 */
     853           0 :         move16();
     854           0 :         inBuf++;
     855             :     }
     856             : 
     857             :     /*-- Handle end. --*/
     858             :     /* nItems = span - 2; (nItems = 5, so we can maintain inItems = 1/5 = 0.2f from above) */
     859           0 :     nItems = sub( MA_LEN, 2 );
     860           0 :     L_sum = L_msu0( L_sum, 0x0001, *oldPtr++ );
     861             : 
     862           0 :     tmp = sub( num_subband, 1 );
     863           0 :     FOR( ; i < tmp; i++ )
     864             :     {
     865           0 :         L_sum = L_msu0( L_sum, 0x0001, *oldPtr++ );
     866             : 
     867           0 :         lo = L_Extract_lc( L_sum, &hi );
     868           0 :         *outBuf++ = round_fx( L_shl( Mpy_32_16( hi, lo, inItems ), 16 ) ); /* Q(8+15+1-16)=Q8 -> Q(8+16-16)=Q8 */
     869             : 
     870             :         /* nItems -= 2; */
     871           0 :         nItems = sub( nItems, 2 );
     872             : 
     873             :         /* 1.0f -> 0x7fff Q15 */
     874           0 :         inItems = 0x7fff;
     875           0 :         move16();
     876           0 :         if ( EQ_16( nItems, 3 ) )
     877             :         {
     878             :             /* 1/3 = 0.333f -> 0x2AAA Q15 */
     879           0 :             inItems = 0x2AAA;
     880           0 :             move16();
     881             :         }
     882           0 :         L_sum = L_msu0( L_sum, 0x0001, *oldPtr++ );
     883             : 
     884           0 :         inBuf++;
     885             :     }
     886             : 
     887             :     /*-- Last sample. --*/
     888           0 :     *outBuf = *inBuf;
     889           0 :     move16();
     890           0 : }
     891             : 
     892             : /*-------------------------------------------------------------------*
     893             :  * SpectrumSmoothing()
     894             :  *
     895             :  * Smoothing of the low-frequency envelope
     896             :  *-------------------------------------------------------------------*/
     897             : 
     898          62 : void SpectrumSmoothing_fx(
     899             :     const Word32 *L_inBuf, /* i  : Qs  Low band MDCT             */
     900             :     Word16 *outBuf_fx,     /* o  : Qss output                    */
     901             :     Word16 *Qss,           /* o  : Q0  Q value of output vector  */
     902             :     const Word16 fLen,     /* i  : Q0  length                    */
     903             :     const Word16 th_cut_fx /* i  : Qss threshold of cut          */
     904             : )
     905             : {
     906             :     /* internal variable */
     907             :     Word16 i, j, k;
     908             : 
     909             :     Word16 num_subband_smooth_fx;
     910             :     Word16 num_subband_smooth_pre_fx;
     911             : 
     912             :     Word16 exp_normd;
     913             :     Word16 exp_shift;
     914             : 
     915             :     Word16 max_val_norm_fx;
     916             : 
     917             :     Word16 Qmax_val_norm[L_FRAME32k / L_SB];
     918             : 
     919             :     Word32 L_inBuf_abs;
     920             :     Word32 L_inBuf_pss[L_FRAME32k];
     921             :     Word32 L_max_val[L_FRAME32k / L_SB];
     922             :     Word16 outBuf_pss_fx[L_FRAME32k];
     923             : 
     924             :     Word16 m, n;
     925             :     Word16 cnt_zero_cont;
     926             :     Word16 n_list[BANDS_MAX];
     927             :     Word16 reset_flag;
     928             :     Word16 pp, pk;
     929             :     Word16 exp_norm;
     930             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     931          62 :     Flag Overflow = 0;
     932          62 :     move32();
     933             : #endif
     934             : 
     935          62 :     *Qss = 10;
     936          62 :     move16();
     937          62 :     num_subband_smooth_pre_fx = mult( fLen, 21845 ); /* 1/L_SB = 1/12 = 21845(Q18)  Q = exp_normn-18 */
     938          62 :     num_subband_smooth_fx = shr( num_subband_smooth_pre_fx, 18 - 15 );
     939          62 :     IF( NE_16( num_subband_smooth_pre_fx, shl( num_subband_smooth_fx, 18 - 15 ) ) )
     940             :     {
     941          62 :         num_subband_smooth_fx++;
     942             :     }
     943             : 
     944       15934 :     FOR( i = 0; i < fLen; i++ )
     945             :     {
     946       15872 :         L_inBuf_pss[i] = L_inBuf[i];
     947       15872 :         move32();
     948       15872 :         outBuf_pss_fx[i] = 0;
     949       15872 :         move16();
     950             :     }
     951             : 
     952         558 :     FOR( i = fLen; i < fLen + ( num_subband_smooth_fx * L_SB - fLen ); i++ )
     953             :     {
     954         496 :         L_inBuf_pss[i] = L_deposit_l( 0 );
     955         496 :         move32();
     956         496 :         outBuf_pss_fx[i] = 0;
     957         496 :         move16();
     958             :     }
     959             : 
     960          62 :     j = 0;
     961          62 :     move16();
     962        1426 :     FOR( i = 0; i < num_subband_smooth_fx; i++ )
     963             :     {
     964        1364 :         L_max_val[i] = L_deposit_l( 0 );
     965        1364 :         move32();
     966       17732 :         FOR( k = 0; k < L_SB; k++ )
     967             :         {
     968       16368 :             L_inBuf_abs = L_abs( L_inBuf_pss[j] );
     969       16368 :             if ( LT_32( L_max_val[i], L_inBuf_abs ) )
     970             :             {
     971        2397 :                 L_max_val[i] = L_inBuf_abs;
     972        2397 :                 move32();
     973             :             }
     974             : 
     975       16368 :             j++;
     976             :         }
     977             :     }
     978             : 
     979             :     /* convert to maximum amplitude frequency log scale envelope */
     980          62 :     j = 0;
     981          62 :     move16();
     982        1426 :     FOR( i = 0; i < num_subband_smooth_fx; i++ )
     983             :     {
     984             :         /* max_val_norm = 10.0f / (max_val[i] + 0.001f); */
     985             :         /* 10.0f : 0x2800, Q10 */
     986        1364 :         IF( GT_32( L_max_val[i], 0x1L ) )
     987             :         {
     988         986 :             exp_normd = norm_l( L_max_val[i] );
     989         986 :             max_val_norm_fx = div_s( 0x2800, round_fx_o( L_shl_o( L_max_val[i], exp_normd, &Overflow ), &Overflow ) ); /* Q10-(Qs+exp_normd-16) */
     990         986 :             Qmax_val_norm[i] = sub( 10 - 12 + 16 + 15, exp_normd );
     991         986 :             move16(); /* 10 - (12+exp_normd-16) +15 */
     992             :             ;
     993             :         }
     994             :         ELSE
     995             :         {
     996         378 :             max_val_norm_fx = 0;
     997         378 :             move16();
     998         378 :             Qmax_val_norm[i] = 0;
     999         378 :             move16();
    1000             :         }
    1001             : 
    1002        1364 :         exp_shift = sub( *Qss, add( Qmax_val_norm[i], -19 ) );
    1003       17732 :         FOR( k = 0; k < L_SB; k++ )
    1004             :         {
    1005       16368 :             exp_norm = norm_l( L_inBuf_pss[j] );
    1006       16368 :             IF( L_inBuf_pss[j] == 0x0L )
    1007             :             {
    1008        9150 :                 outBuf_pss_fx[j] = 0;
    1009        9150 :                 move16();
    1010             :             }
    1011        7218 :             ELSE IF( LT_32( L_abs( L_inBuf_pss[j] ), L_max_val[i] ) )
    1012             :             {
    1013        6113 :                 IF( L_inBuf_pss[j] >= 0 )
    1014             :                 {
    1015        3033 :                     outBuf_pss_fx[j] = round_fx_o( L_shl_o( Mpy_32_16_r( L_shl_o( L_inBuf_pss[j], exp_norm, &Overflow ), max_val_norm_fx ), sub( exp_shift, exp_norm ), &Overflow ), &Overflow );
    1016        3033 :                     move32();
    1017             :                 }
    1018             :                 ELSE
    1019             :                 {
    1020        3080 :                     outBuf_pss_fx[j] = negate( round_fx_o( L_shl_o( Mpy_32_16_r( L_shl_o( L_abs( L_inBuf_pss[j] ), exp_norm, &Overflow ), max_val_norm_fx ), sub( exp_shift, exp_norm ), &Overflow ), &Overflow ) );
    1021        3080 :                     move16();
    1022             :                 }
    1023             :             }
    1024             :             ELSE
    1025             :             {
    1026             :                 /* CLIP, for avoiding computational difference */
    1027        1105 :                 outBuf_pss_fx[j] = 0x2800;
    1028        1105 :                 move16();
    1029        1105 :                 if ( L_inBuf_pss[j] < 0x0L )
    1030             :                 {
    1031         550 :                     outBuf_pss_fx[j] = -0x2800;
    1032         550 :                     move16();
    1033             :                 }
    1034             :             }
    1035       16368 :             j++;
    1036             :         }
    1037             :     }
    1038             : 
    1039          62 :     k = 0;
    1040          62 :     move16();
    1041          62 :     m = 0;
    1042          62 :     move16();
    1043          62 :     n = 0;
    1044          62 :     move16();
    1045          62 :     reset_flag = 0;
    1046          62 :     move16();
    1047          62 :     n_list[0] = 0;
    1048          62 :     move16();
    1049        1426 :     FOR( j = 0; j < num_subband_smooth_fx; j++ )
    1050             :     {
    1051        1364 :         cnt_zero_cont = 0;
    1052        1364 :         move16();
    1053       17732 :         FOR( i = 0; i < L_SB; i++ )
    1054             :         {
    1055       16368 :             cnt_zero_cont = add( cnt_zero_cont, 1 );
    1056       16368 :             if ( outBuf_pss_fx[k] != 0 )
    1057             :             {
    1058        7218 :                 cnt_zero_cont = 0;
    1059        7218 :                 move16();
    1060             :             }
    1061       16368 :             k = add( k, 1 );
    1062             :         }
    1063             : 
    1064        1364 :         IF( cnt_zero_cont != 0 )
    1065             :         {
    1066         782 :             test();
    1067         782 :             IF( GT_16( j, div_s_ss( subband_search_offsets[0], L_SB ) ) && reset_flag == 0 )
    1068             :             {
    1069          62 :                 n = 0;
    1070          62 :                 move16();
    1071          62 :                 reset_flag = 1;
    1072          62 :                 move16();
    1073             :             }
    1074         782 :             n_list[n] = j;
    1075         782 :             move16();
    1076         782 :             n = add( n, 1 );
    1077             :         }
    1078             : 
    1079        1364 :         test();
    1080        1364 :         if ( EQ_16( reset_flag, 1 ) && EQ_16( n, 1 ) )
    1081             :         {
    1082         127 :             m = 0;
    1083         127 :             move16();
    1084             :         }
    1085             : 
    1086        1364 :         pk = sub( k, L_SB );
    1087        1364 :         IF( GT_16( cnt_zero_cont, mult_r( L_SB, 24576 ) ) ) /* cnt_zero_cont > 3*L_SB/4 */
    1088             :         {
    1089         413 :             pp = round_fx( L_shl( L_mult( n_list[m], L_SB ), 15 ) );
    1090        5369 :             FOR( i = 0; i < L_SB; i++ )
    1091             :             {
    1092        4956 :                 if ( outBuf_pss_fx[pk + i] == 0 )
    1093             :                 {
    1094        4916 :                     outBuf_pss_fx[pk + i] = shr( outBuf_pss_fx[pp + i], 1 );
    1095        4916 :                     move16();
    1096             :                 }
    1097             :             }
    1098         413 :             m = add( m, 1 );
    1099             :         }
    1100             :     }
    1101             : 
    1102       15934 :     FOR( i = 0; i < fLen; i++ )
    1103             :     {
    1104       15872 :         outBuf_fx[i] = 0x0;
    1105       15872 :         move16();
    1106       15872 :         if ( GT_16( abs_s( outBuf_pss_fx[i] ), th_cut_fx ) )
    1107             :         {
    1108        7864 :             outBuf_fx[i] = outBuf_pss_fx[i];
    1109        7864 :             move16();
    1110             :         }
    1111             :     }
    1112             : 
    1113          62 :     return;
    1114             : }
    1115             : 
    1116             : /*-------------------------------------------------------------------*
    1117             :  * Get20Log10Spec()
    1118             :  *
    1119             :  * Calculates 20*log10() for the specified samples. Input and output buffers can be the same.
    1120             :  *-------------------------------------------------------------------*/
    1121             : 
    1122           0 : void Get20Log10Spec_fx(
    1123             :     const Word32 *L_inBuf,
    1124             :     /* i  : input         Q_inBuf   */ /* L_inBuf >=0, so L_abs is omitted. */
    1125             :     Word16 *outBuf_fx,                 /* o  : output        Q7        */
    1126             :     const Word16 fLen,                 /* i  : loop length             */
    1127             :     const Word16 Q_inBuf               /* i  : Qvalue of L_inBuf       */
    1128             : )
    1129             : {
    1130             :     Word16 i;
    1131             :     Word16 exp, frac;
    1132             : 
    1133             :     Word32 L_tmp;
    1134             : 
    1135             :     Word32 L_lamda;
    1136             :     Word16 Q_inBuf_1;
    1137             : 
    1138           0 :     Q_inBuf_1 = sub( Q_inBuf, 1 );
    1139           0 :     L_lamda = L_shl( 1L, Q_inBuf_1 ); /* +1 : Q_inBuf -> Q_inBuf-1 for overflow problem */
    1140             : 
    1141           0 :     FOR( i = 0; i < fLen; i++ )
    1142             :     {
    1143             :         /*outBuf++ = (float) (20.0f * log10(fabs(*inBuf + 1.0))); */
    1144           0 :         L_tmp = L_add( L_shr( *L_inBuf++, 1 ), L_lamda );
    1145           0 :         exp = 31;
    1146           0 :         move16();
    1147           0 :         if ( L_tmp != 0x0L )
    1148             :         {
    1149           0 :             exp = norm_l( L_tmp );
    1150             :         }
    1151           0 :         frac = Log2_norm_lc( L_shl( L_tmp, exp ) );
    1152           0 :         exp = sub( 30, exp );
    1153           0 :         exp = sub( exp, Q_inBuf_1 );
    1154           0 :         L_tmp = L_Comp( exp, frac );
    1155             : 
    1156           0 :         L_tmp = Mpy_32_16_1( L_tmp, 24660 ); /* 6.0206 in Q12 */
    1157           0 :         L_tmp = L_shl( L_tmp, 2 + 8 );       /* Q7 */
    1158           0 :         *outBuf_fx++ = round_fx( L_tmp );
    1159           0 :         move16();
    1160             :     }
    1161             : 
    1162           0 :     return;
    1163             : }
    1164             : /*-------------------------------------------------------------------*
    1165             :  * convert_lagIndices_pls2smp()
    1166             :  *
    1167             :  *
    1168             :  *-------------------------------------------------------------------*/
    1169          31 : void convert_lagIndices_pls2smp_fx(
    1170             :     Word16 lagIndices_in_fx[],
    1171             :     Word16 nBands_search_fx,
    1172             :     Word16 lagIndices_out_fx[],
    1173             :     const Word16 sspectra_fx[],
    1174             :     const Word16 sbWidth_fx[],
    1175             :     const Word16 fLenLow_fx )
    1176             : {
    1177             :     Word16 sb;
    1178             :     Word16 i, cnt;
    1179             : 
    1180         155 :     FOR( sb = 0; sb < nBands_search_fx; sb++ )
    1181             :     {
    1182         124 :         cnt = 0;
    1183         124 :         move16();
    1184         124 :         i = 0;
    1185         124 :         move16();
    1186             : 
    1187        1281 :         WHILE( LE_16( cnt, lagIndices_in_fx[sb] ) )
    1188             :         {
    1189        1157 :             if ( sspectra_fx[subband_search_offsets[sb] + i] != 0 )
    1190             :             {
    1191         288 :                 cnt = add( cnt, 1 );
    1192             :             }
    1193             : 
    1194        1157 :             i = add( i, 1 );
    1195             : 
    1196        1157 :             IF( GE_16( add( subband_search_offsets[sb], add( i, sbWidth_fx[sb] ) ), fLenLow_fx ) )
    1197             :             {
    1198           0 :                 BREAK;
    1199             :             }
    1200             :         }
    1201             : 
    1202         124 :         lagIndices_out_fx[sb] = add( sub( i, 1 ), subband_search_offsets[sb] );
    1203         124 :         move16();
    1204             :     }
    1205             : 
    1206          31 :     return;
    1207             : }
    1208             : 
    1209             : /*-------------------------------------------------------------------*
    1210             :  * get_usebit_npswb()
    1211             :  *
    1212             :  *
    1213             :  *-------------------------------------------------------------------*/
    1214          31 : Word16 get_usebit_npswb_fx(
    1215             :     Word16 hqswb_clas_fx )
    1216             : {
    1217             :     Word16 i;
    1218             :     Word16 bits;
    1219             :     Word16 up_lmt;
    1220             :     const Word16 *bits_req;
    1221             : 
    1222          31 :     up_lmt = 0;
    1223          31 :     move16();
    1224          31 :     bits_req = bits_lagIndices_modeNormal;
    1225          31 :     move16();
    1226          31 :     bits = 0;
    1227          31 :     move16();
    1228             : 
    1229          31 :     IF( EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
    1230             :     {
    1231          31 :         up_lmt = NB_SWB_SUBBANDS;
    1232          31 :         move16();
    1233          31 :         bits_req = bits_lagIndices_modeNormal;
    1234          31 :         move16();
    1235             :     }
    1236           0 :     ELSE IF( EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
    1237             :     {
    1238           0 :         up_lmt = NB_SWB_SUBBANDS_HAR_SEARCH_SB;
    1239           0 :         move16();
    1240           0 :         bits_req = bits_lagIndices_mode0_Har;
    1241           0 :         move16();
    1242           0 :         bits = 2;
    1243           0 :         move16(); /*noise gain*/
    1244             :     }
    1245             : 
    1246         155 :     FOR( i = 0; i < up_lmt; i++ )
    1247             :     {
    1248         124 :         bits = add( bits, bits_req[i] );
    1249         124 :         move16();
    1250             :     }
    1251             : 
    1252          31 :     return bits;
    1253             : }
    1254             : /*-------------------------------------------------------------------*
    1255             :  * SpectrumSmoothing_nss()
    1256             :  *
    1257             :  *
    1258             :  *-------------------------------------------------------------------*/
    1259           0 : void SpectrumSmoothing_nss_fx(
    1260             :     const Word32 *L_inBuf, /* i  : lowband MDCT              */
    1261             :     Word16 *outBuf_fx,     /* o  : output                    */
    1262             :     Word16 *Qss,           /* o  : Q value of output vector  */
    1263             :     const Word16 fLen      /* i  : length                    */
    1264             : )
    1265             : {
    1266             :     /* internal variable */
    1267             :     Word16 i, k;
    1268             : 
    1269             :     Word16 inBuf_fx[L_FRAME32k];
    1270             :     Word16 Qm;
    1271             :     Word32 L_tmp[L_FRAME32k];
    1272             : 
    1273             :     Word16 num_subband_smooth_fx;
    1274             :     Word16 exp_tmp;
    1275             : 
    1276             :     Word16 inBufw_fx[L_FRAME32k + L_SB_NSS];
    1277             :     Word16 outBufw_fx[L_FRAME32k + L_SB_NSS];
    1278             :     Word32 L_outBufw[L_FRAME32k + L_SB_NSS];
    1279             :     Word16 Qo[NUM_SUBBAND_SMOOTH_MAX];
    1280             : 
    1281             :     Word16 avg_val_fx;
    1282             :     Word32 L_avg_val;
    1283             :     Word16 r0_fx;
    1284             :     Word32 L_r0;
    1285             :     Word32 L_temp;
    1286             :     Word16 temp_fx;
    1287             : 
    1288             :     Word16 max_peak_fx;
    1289             : 
    1290             :     Word16 smr_fx;
    1291             :     Word32 L_smr;
    1292             : 
    1293             :     Word32 L_temp_sum_1[NUM_SUBBAND_SMOOTH_MAX];
    1294             :     Word32 L_temp_sum_2[NUM_SUBBAND_SMOOTH_MAX];
    1295             :     Word32 L_temp_sum_3[NUM_SUBBAND_SMOOTH_MAX];
    1296             : 
    1297             : 
    1298             :     Word16 temp_sum_smooth_fx[NUM_SUBBAND_SMOOTH_MAX];
    1299             :     Word16 temp_sum_div_fx[NUM_SUBBAND_SMOOTH_MAX];
    1300             :     Word16 Qsumdiv[NUM_SUBBAND_SMOOTH_MAX];
    1301             :     Word32 L_temp1;
    1302             :     Word16 temp_hi;
    1303             :     Word16 temp_lo;
    1304             : 
    1305             : 
    1306             :     Word16 avg_val2_fx;
    1307             :     Word32 L_avg_val2;
    1308             :     Word16 Qavg_val;
    1309             :     Word16 Qsmr;
    1310             :     Word16 exp, frac;
    1311             : 
    1312             :     Word16 clip_cof_fx;
    1313             : 
    1314             :     Word16 thre_fx, thre_fx_neg;
    1315             :     Word16 thre_min_fx;
    1316             : 
    1317             :     Word16 temp_sum_log_fx[NUM_SUBBAND_SMOOTH_MAX];
    1318             :     Word16 exp_norm;
    1319             :     Word16 exp_normn;
    1320             :     Word16 exp_normd;
    1321             :     Word16 exp_shift;
    1322             : 
    1323           0 :     L_tmp[0] = L_deposit_l( 0 );
    1324           0 :     move32();
    1325           0 :     FOR( i = 0; i < fLen; i++ )
    1326             :     {
    1327           0 :         L_tmp[0] = L_or( L_tmp[0], L_abs( L_inBuf[i] ) );
    1328           0 :         move32();
    1329             :     }
    1330           0 :     exp_norm = norm_l( L_tmp[0] );
    1331           0 :     Qm = sub( exp_norm, 4 ); /* Qm = sub(add(12, exp_norm), 16); */
    1332             : 
    1333           0 :     FOR( i = 0; i < fLen; i++ )
    1334             :     {
    1335           0 :         L_tmp[i] = L_shl( L_inBuf[i], exp_norm ); /* Q(12+exp_norm) */
    1336           0 :         move32();
    1337           0 :         inBuf_fx[i] = round_fx( L_tmp[i] ); /* Qm */
    1338           0 :         move16();
    1339             :     }
    1340             : 
    1341           0 :     num_subband_smooth_fx = shr( fLen, 3 ); /* L_SB_NSS=8 shr(target, 3); */
    1342             : 
    1343             :     /* buffer copy for fractional point */
    1344           0 :     FOR( i = 0; i < fLen; i++ )
    1345             :     {
    1346           0 :         inBufw_fx[i] = inBuf_fx[i];
    1347           0 :         move16();
    1348           0 :         outBufw_fx[i] = 0;
    1349           0 :         move16();
    1350             :     }
    1351             : 
    1352             :     /* initialization of over buffer for fractional point */
    1353           0 :     k = add( fLen, L_SB_NSS );
    1354           0 :     FOR( i = fLen; i < k; i++ )
    1355             :     {
    1356           0 :         inBufw_fx[i] = 0;
    1357           0 :         move16();
    1358           0 :         outBufw_fx[i] = 0;
    1359           0 :         move16();
    1360             :     }
    1361             : 
    1362           0 :     L_avg_val = L_deposit_l( 0 );
    1363           0 :     FOR( i = 0; i < fLen; i++ )
    1364             :     {
    1365           0 :         L_r0 = L_abs( L_deposit_l( inBufw_fx[i] ) );
    1366           0 :         L_avg_val = L_add( L_avg_val, L_r0 );
    1367             :     }
    1368           0 :     exp_normn = norm_l( L_avg_val );
    1369           0 :     exp_normn = sub( exp_normn, 1 );
    1370           0 :     exp_normd = norm_s( fLen );
    1371           0 :     avg_val_fx = div_l( L_shl( L_avg_val, exp_normn ), shl( fLen, exp_normd ) ); /* (Qs+exp_norm+exp_normn) - (exp_normd) - 15 */
    1372           0 :     Qavg_val = sub( add( Qm, sub( exp_normn, exp_normd ) ), 1 );
    1373             : 
    1374           0 :     max_peak_fx = 0;
    1375           0 :     move16();
    1376           0 :     FOR( i = 0; i < fLen; i++ )
    1377             :     {
    1378           0 :         r0_fx = abs_s( inBufw_fx[i] );
    1379           0 :         if ( LT_16( max_peak_fx, r0_fx ) )
    1380             :         {
    1381           0 :             max_peak_fx = r0_fx;
    1382           0 :             move16(); /* Qm */
    1383             :         }
    1384             :     }
    1385             : 
    1386             :     /*smr = 10.0f * (float)log10( max_peak/(avg_val + 1.0e-20) + 1.0e-20 ); */
    1387           0 :     exp_normn = norm_s( max_peak_fx );
    1388           0 :     exp_normn = sub( exp_normn, 1 );
    1389             : 
    1390           0 :     avg_val_fx = s_max( avg_val_fx, 0x1 );
    1391           0 :     exp_normd = norm_s( avg_val_fx );
    1392             : 
    1393           0 :     smr_fx = div_s( shl( max_peak_fx, exp_normn ), shl( avg_val_fx, exp_normd ) ); /* Q(exp_normn-exp_normd+15) */
    1394           0 :     exp_tmp = sub( exp_normn, exp_normd );
    1395           0 :     L_smr = L_deposit_h( smr_fx ); /* Q+16 -> Q(exp_normn-exp_normd+15+16) */
    1396           0 :     Qsmr = add( sub( add( Qm, exp_tmp ), Qavg_val ), 31 );
    1397             : 
    1398           0 :     L_temp = L_add( L_shr( L_smr, 1 ), 0x1L ); /* add minimum value */
    1399           0 :     exp = norm_l( L_temp );
    1400           0 :     frac = Log2_norm_lc( L_shl( L_temp, exp ) );
    1401           0 :     exp = sub( 30, exp );
    1402           0 :     exp = sub( exp, sub( Qsmr, 1 ) );
    1403           0 :     L_temp = L_Comp( exp, frac );
    1404             : 
    1405           0 :     L_temp = Mpy_32_16_1( L_temp, 12330 ); /* 3.0103 in Q12 */
    1406           0 :     L_temp = L_shl( L_temp, 2 + 8 );       /* Q7 */
    1407           0 :     smr_fx = round_fx( L_temp );
    1408             : 
    1409           0 :     FOR( i = 0; i < num_subband_smooth_fx; i++ )
    1410             :     {
    1411           0 :         L_temp_sum_1[i] = L_deposit_l( 0 );
    1412           0 :         L_temp_sum_2[i] = L_deposit_l( 0 );
    1413           0 :         move32();
    1414           0 :         move32();
    1415           0 :         FOR( k = 0; k < L_SB_NSS_HALF; k++ )
    1416             :         {
    1417           0 :             L_temp_sum_1[i] = L_add( L_temp_sum_1[i], extract_l( abs_s( inBufw_fx[k + L_SB_NSS * i] ) ) ); /* Qm */
    1418           0 :             move32();
    1419             :         }
    1420             : 
    1421           0 :         FOR( k = L_SB_NSS_HALF; k < L_SB_NSS; k++ )
    1422             :         {
    1423           0 :             L_temp_sum_2[i] = L_add( L_temp_sum_2[i], extract_l( abs_s( inBufw_fx[k + L_SB_NSS * i] ) ) ); /* Qm */
    1424           0 :             move32();
    1425             :         }
    1426             : 
    1427           0 :         L_temp_sum_1[i] = L_shr( L_temp_sum_1[i], 2 ); /* *0.25   guarantee low-side 16bit for L_temp_sum_* */
    1428           0 :         move32();
    1429           0 :         L_temp_sum_2[i] = L_shr( L_temp_sum_2[i], 2 ); /* *0.25 */
    1430           0 :         move32();
    1431           0 :         L_temp_sum_3[i] = L_mult( extract_l( L_temp_sum_1[i] ), extract_l( L_temp_sum_2[i] ) ); /* Qm*2+1 */
    1432           0 :         move32();
    1433             : 
    1434           0 :         IF( L_temp_sum_3[i] == 0 )
    1435             :         {
    1436           0 :             L_temp_sum_3[i] = L_shl( L_add( L_temp_sum_1[i], L_temp_sum_2[i] ), add( Qm, 1 ) ); /*Q(Qm+Qm+1) */
    1437           0 :             move32();
    1438             :         }
    1439             :     }
    1440             : 
    1441           0 :     exp_norm = add( shl( Qm, 1 ), 1 );
    1442           0 :     Get20Log10Spec_fx( L_temp_sum_3, temp_sum_log_fx, num_subband_smooth_fx, exp_norm );
    1443             : 
    1444             :     /* temp_sum_log_fx // *0.5  Q7 -> Q8 (not change) */
    1445           0 :     SmoothSpec_fx( temp_sum_log_fx, temp_sum_smooth_fx, num_subband_smooth_fx );
    1446             : 
    1447           0 :     FOR( i = 0; i < num_subband_smooth_fx; i++ )
    1448             :     {
    1449           0 :         L_temp1 = L_mult( temp_sum_smooth_fx[i], 1360 ); /* Q8+Q13+1=Q22, 1360(Q13) = 0.1660 = 3.321928(log2^10) * 0.05 */
    1450           0 :         L_temp1 = L_shr( L_temp1, 6 );                   /* Q22 -> Q16 */
    1451           0 :         L_temp1 = L_negate( L_temp1 );
    1452           0 :         temp_lo = L_Extract_lc( L_temp1, &temp_hi );
    1453           0 :         Qsumdiv[i] = sub( 14, temp_hi );
    1454           0 :         move16();
    1455           0 :         temp_sum_div_fx[i] = extract_l( Pow2( 14, temp_lo ) ); /* Qsumdiv[i] */
    1456           0 :         move16();
    1457           0 :         exp_norm = norm_s( temp_sum_div_fx[i] );
    1458           0 :         temp_sum_div_fx[i] = shl( temp_sum_div_fx[i], exp_norm );
    1459           0 :         move16();
    1460           0 :         Qsumdiv[i] = add( Qsumdiv[i], exp_norm );
    1461           0 :         move16();
    1462             :     }
    1463             : 
    1464           0 :     *Qss = 31;
    1465           0 :     move16();
    1466           0 :     FOR( i = 0; i < num_subband_smooth_fx; i++ )
    1467             :     {
    1468           0 :         Qo[i] = add( add( Qm, Qsumdiv[i] ), 1 );
    1469           0 :         move16();
    1470           0 :         L_temp1 = 0x0L;
    1471           0 :         FOR( k = 0; k < L_SB_NSS; k++ )
    1472             :         {
    1473           0 :             L_outBufw[k + L_SB_NSS * i] = L_mult( inBufw_fx[k + L_SB_NSS * i], temp_sum_div_fx[i] );
    1474           0 :             move32();
    1475           0 :             L_temp1 = L_or( L_temp1, L_abs( L_outBufw[k + L_SB_NSS * i] ) );
    1476             :         }
    1477           0 :         exp_norm = 31;
    1478           0 :         if ( L_temp1 != 0x0L )
    1479             :         {
    1480           0 :             exp_norm = norm_l( L_temp1 );
    1481             :         }
    1482           0 :         FOR( k = 0; k < L_SB_NSS; k++ )
    1483             :         {
    1484           0 :             L_outBufw[k + L_SB_NSS * i] = L_shl( L_outBufw[k + L_SB_NSS * i], exp_norm );
    1485           0 :             move32();
    1486             :         }
    1487           0 :         Qo[i] = add( Qo[i], exp_norm );
    1488           0 :         move16();
    1489           0 :         *Qss = s_min( *Qss, Qo[i] );
    1490           0 :         move16();
    1491             :     }
    1492             : 
    1493           0 :     FOR( i = 0; i < num_subband_smooth_fx; i++ )
    1494             :     {
    1495           0 :         exp_shift = sub( *Qss, Qo[i] );
    1496           0 :         exp_shift = s_max( -31, exp_shift );
    1497           0 :         FOR( k = 0; k < L_SB_NSS; k++ )
    1498             :         {
    1499           0 :             L_outBufw[k + L_SB_NSS * i] = L_shl( L_outBufw[k + L_SB_NSS * i], exp_shift );
    1500           0 :             move16();
    1501           0 :             outBufw_fx[k + L_SB_NSS * i] = round_fx( L_outBufw[k + L_SB_NSS * i] );
    1502           0 :             move16();
    1503             :         }
    1504             :     }
    1505           0 :     *Qss = sub( *Qss, 16 );
    1506             : 
    1507           0 :     L_avg_val2 = L_deposit_l( 0 );
    1508           0 :     FOR( i = 0; i < fLen; i++ )
    1509             :     {
    1510           0 :         L_r0 = L_abs( L_deposit_l( outBufw_fx[i] ) );
    1511           0 :         L_avg_val2 = L_add( L_avg_val2, L_r0 ); /* Qss */
    1512             :     }
    1513             : 
    1514           0 :     exp_normn = norm_l( L_avg_val2 );
    1515           0 :     exp_normn = sub( exp_normn, 1 );
    1516           0 :     exp_normd = norm_s( fLen );
    1517           0 :     temp_fx = div_l( L_shl( L_avg_val2, exp_normn ), shl( fLen, exp_normd ) ); /* Q(obw+exp_normn - exp_normd) - 1 */
    1518           0 :     avg_val2_fx = shr( temp_fx, sub( sub( exp_normn, exp_normd ), 1 ) );       /* Qss */
    1519             : 
    1520             :     /*clip_cof = smr - 16.0f; */
    1521           0 :     clip_cof_fx = sub( smr_fx, 2048 ); /* 2048: 16.0f (Q7) */
    1522           0 :     if ( clip_cof_fx < 0 )
    1523             :     {
    1524           0 :         clip_cof_fx = 0;
    1525           0 :         move16();
    1526             :     }
    1527             :     /*clip_cof += 2.5f; */
    1528           0 :     clip_cof_fx = add( clip_cof_fx, 320 ); /* 320: 2.5f (Q7) */
    1529             : 
    1530           0 :     thre_fx = round_fx( L_shl( L_mult( avg_val2_fx, clip_cof_fx ), 8 ) ); /* Q(Qss+7+1) -> Qss */
    1531           0 :     thre_fx_neg = negate( thre_fx );
    1532           0 :     thre_min_fx = shr( avg_val2_fx, 2 ); /* *0.25f  // Qss */
    1533             : 
    1534           0 :     FOR( i = 0; i < fLen; i++ )
    1535             :     {
    1536           0 :         IF( GT_16( abs_s( outBufw_fx[i] ), thre_fx ) )
    1537             :         {
    1538           0 :             temp_fx = thre_fx;
    1539           0 :             move16();
    1540           0 :             if ( outBufw_fx[i] < 0 )
    1541             :             {
    1542           0 :                 temp_fx = thre_fx_neg;
    1543           0 :                 move16();
    1544             :             }
    1545           0 :             outBufw_fx[i] = temp_fx;
    1546           0 :             move16();
    1547             :         }
    1548             : 
    1549           0 :         if ( LT_16( abs_s( outBufw_fx[i] ), thre_min_fx ) )
    1550             :         {
    1551           0 :             outBufw_fx[i] = 0;
    1552           0 :             move16();
    1553             :         }
    1554             :     }
    1555             : 
    1556           0 :     FOR( i = 0; i < fLen; i++ )
    1557             :     {
    1558           0 :         outBuf_fx[i] = outBufw_fx[i];
    1559           0 :         move16(); /* Qss */
    1560             :     }
    1561             : 
    1562           0 :     return;
    1563             : }
    1564             : 
    1565             : /*-------------------------------------------------------------------*
    1566             :  * return_bits_normal2
    1567             :  *
    1568             :  * arrange bit_budget when HQ_NORMAL
    1569             :  *-------------------------------------------------------------------*/
    1570             : 
    1571          31 : void return_bits_normal2_fx(
    1572             :     Word16 *bit_budget_fx,            /* i/o : bit budget                          */
    1573             :     const Word16 p2a_flags_fx[],      /* i   : HF tonal indicator                  */
    1574             :     const Word16 bands_fx,            /* i   : Total number of Subbands in a frame */
    1575             :     const Word16 bits_lagIndices_fx[] /* i   : bits for lagIndices                 */
    1576             : )
    1577             : {
    1578             :     Word16 i;
    1579             :     const Word16 *p_p2a_flags_fx;
    1580             : 
    1581          31 :     p_p2a_flags_fx = &p2a_flags_fx[sub( bands_fx, NB_SWB_SUBBANDS )];
    1582         155 :     FOR( i = 0; i < NB_SWB_SUBBANDS; i++ )
    1583             :     {
    1584         124 :         if ( EQ_16( *p_p2a_flags_fx++, 1 ) )
    1585             :         {
    1586           4 :             *bit_budget_fx = add( *bit_budget_fx, bits_lagIndices_fx[i] );
    1587           4 :             move16();
    1588             :         }
    1589             :     }
    1590             : 
    1591          31 :     return;
    1592             : }
    1593             : 
    1594             : /*-------------------------------------------------------------------*
    1595             :  * preset_hq2_swb
    1596             :  *
    1597             :  * preset before swb_bwe_{enc,dec}_lr
    1598             :  *-------------------------------------------------------------------*/
    1599             : 
    1600          31 : void preset_hq2_swb_fx(
    1601             :     const Word16 hqswb_clas_fx, /* i   : HQ2 class information               */
    1602             :     const Word16 band_end_fx[], /* i   : band end of each SB                 */
    1603             :     Word16 *har_bands_fx,       /* i/o : Number of LF harmonic bands         */
    1604             :     Word16 p2a_bands_fx,        /* i   : flag for peakness                   */
    1605             :     const Word16 length_fx,     /* i   : processed band length               */
    1606             :     const Word16 bands_fx,      /* i   : Total number of Subbands in a frame */
    1607             :     Word16 *lowlength_fx,       /* o   : lowband length                      */
    1608             :     Word16 *highlength_fx,      /* o   : highband length                     */
    1609             :     Word32 L_m[]                /* o   : MDCT                                */
    1610             : )
    1611             : {
    1612          31 :     IF( EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
    1613             :     {
    1614           0 :         *har_bands_fx = add( sub( bands_fx, p2a_bands_fx ), 1 );
    1615           0 :         move16();
    1616           0 :         *lowlength_fx = add( band_end_fx[*har_bands_fx - 1], 1 );
    1617           0 :         move16();
    1618             :     }
    1619             :     ELSE
    1620             :     {
    1621          31 :         *lowlength_fx = add( band_end_fx[bands_fx - NB_SWB_SUBBANDS - 1], 1 );
    1622          31 :         move16();
    1623             :     }
    1624             : 
    1625          31 :     *highlength_fx = sub( length_fx, *lowlength_fx );
    1626          31 :     move16();
    1627             : 
    1628          31 :     set32_fx( L_m, 0, length_fx );
    1629             : 
    1630          31 :     return;
    1631             : }
    1632             : 
    1633             : /*-------------------------------------------------------------------*
    1634             :  * preset_hq2_swb
    1635             :  *
    1636             :  * post process after swb_bwe_{enc,dec}_lr
    1637             :  *-------------------------------------------------------------------*/
    1638             : 
    1639          31 : void post_hq2_swb_fx(
    1640             :     const Word32 L_m[],           /* i   : input_signal                        */
    1641             :     const Word16 lowlength_fx,    /* i   : lowband length                      */
    1642             :     const Word16 highlength_fx,   /* i   : highband length                     */
    1643             :     const Word16 hqswb_clas_fx,   /* i   : HQ2 class information               */
    1644             :     const Word16 har_bands_fx,    /* i   : Number of LF harmonic bands         */
    1645             :     const Word16 bands_fx,        /* i   : Total number of Subbands in a frame */
    1646             :     const Word16 p2a_flags_fx[],  /* i   : HF tonal indicator                  */
    1647             :     const Word16 band_start_fx[], /* i   : band start of each SB               */
    1648             :     const Word16 band_end_fx[],   /* i   : band end of each SB                 */
    1649             :     Word32 L_y2[],                /* o   : output signal                       */
    1650             :     Word16 npulses_fx[]           /* i/o : Number of coded spectrum            */
    1651             : )
    1652             : {
    1653             :     Word16 i, k;
    1654             : 
    1655             :     /* copy the scratch buffer to the output */
    1656          31 :     Copy32( &L_m[lowlength_fx], &L_y2[lowlength_fx], highlength_fx );
    1657             : 
    1658          31 :     IF( EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
    1659             :     {
    1660           0 :         k = har_bands_fx;
    1661           0 :         move16();
    1662             :     }
    1663             :     ELSE
    1664             :     {
    1665          31 :         k = sub( bands_fx, NB_SWB_SUBBANDS );
    1666             :     }
    1667             : 
    1668         155 :     FOR( ; k < bands_fx; k++ )
    1669             :     {
    1670         124 :         test();
    1671         124 :         IF( p2a_flags_fx[k] == 0 && npulses_fx[k] == 0 )
    1672             :         {
    1673        9325 :             FOR( i = band_start_fx[k]; i <= band_end_fx[k]; i++ )
    1674             :             {
    1675        9208 :                 if ( L_y2[i] != 0 )
    1676             :                 {
    1677        9208 :                     npulses_fx[k] = add( npulses_fx[k], 1 );
    1678        9208 :                     move16();
    1679             :                 }
    1680             :             }
    1681             :         }
    1682             :     }
    1683             : 
    1684          31 :     return;
    1685             : }
    1686             : 
    1687             : /*--------------------------------------------------------------------------*
    1688             :  * GetSynthesizedSpecThinOut()
    1689             :  *
    1690             :  * Synthesize the spectrum in generic subband coding
    1691             :  *--------------------------------------------------------------------------*/
    1692             : 
    1693          31 : void GetSynthesizedSpecThinOut_fx(
    1694             :     const Word16 *predBuf_fx,    /* i  : Qss: prediction buffer (i.e., lowband)  */
    1695             :     const Word16 Qss,            /* i  :      Q value of input vector            */
    1696             :     Word32 *L_outBuf,            /* o  : QsL: synthesized spectrum               */
    1697             :     Word16 QsL,                  /* o  :      Q value of synthesized spectrum    */
    1698             :     const Word16 nBands_fx,      /* i  : Q0: number of subbands calculated       */
    1699             :     const Word16 *sbWidth_fx,    /* i  : Q0: subband lengths                     */
    1700             :     const Word16 *lagIndices_fx, /* i  : Q0: lowband index for each subband      */
    1701             :     const Word16 *lagGains_fx,   /* i  : Qgain: lagGain for each subband         */
    1702             :     const Word16 *QlagGains_fx,  /* i  : Q0: Q value of lagGains_fx              */
    1703             :     const Word16 predBufLen_fx   /* i  : Q0: lowband length                      */
    1704             : )
    1705             : {
    1706             :     Word16 i, sb;
    1707             :     Word16 fLen_fx, lag_fx;
    1708             : 
    1709             :     const Word16 *ptr_predBuf_fx;
    1710             :     Word32 *ptr_L_outBuf;
    1711             :     Word32 *ptr_L_in_outBuf;
    1712             : 
    1713             :     Word16 exp_shift;
    1714             : 
    1715          31 :     ptr_L_in_outBuf = L_outBuf;
    1716          31 :     ptr_L_outBuf = L_outBuf;
    1717             : 
    1718         155 :     FOR( sb = 0; sb < nBands_fx; sb++ )
    1719             :     {
    1720         124 :         fLen_fx = sbWidth_fx[sb];
    1721         124 :         lag_fx = lagIndices_fx[sb];
    1722         124 :         move16();
    1723         124 :         move16();
    1724         124 :         if ( GT_16( add( lag_fx, fLen_fx ), predBufLen_fx ) )
    1725             :         {
    1726             :             /* should never happen */
    1727           0 :             lag_fx = sub( predBufLen_fx, fLen_fx );
    1728             :         }
    1729         124 :         ptr_predBuf_fx = predBuf_fx + lag_fx;
    1730             : 
    1731             : 
    1732         124 :         exp_shift = sub( add( add( Qss, QlagGains_fx[sb] ), 1 ), QsL );
    1733             : 
    1734        9796 :         FOR( i = 0; i < fLen_fx; i++ )
    1735             :         {
    1736        9672 :             IF( *ptr_predBuf_fx >= 0x0L )
    1737             :             {
    1738        4700 :                 *ptr_L_outBuf++ = L_shr( L_mult( *ptr_predBuf_fx++, lagGains_fx[sb] ), exp_shift );
    1739        4700 :                 move32(); /* Qss+QlagGains+1 -> QsL */
    1740             :             }
    1741             :             ELSE
    1742             :             {
    1743        4972 :                 *ptr_L_outBuf++ = L_negate( L_shr( L_mult( abs_s( *ptr_predBuf_fx++ ), lagGains_fx[sb] ), exp_shift ) );
    1744        4972 :                 move32(); /* Qss+QlagGains+1 -> QsL */
    1745             :             }
    1746             :         }
    1747             :     }
    1748             : 
    1749          31 :     ptr_L_outBuf = ptr_L_in_outBuf;
    1750             : 
    1751          31 :     return;
    1752             : }
    1753             : 
    1754             : /*--------------------------------------------------------------------------*
    1755             :  * div_s_ss
    1756             :  *
    1757             :  * compute division with Word16 Q0.   ex.   10/2 -> 5
    1758             :  *--------------------------------------------------------------------------*/
    1759             : 
    1760        7548 : Word16 div_s_ss(                 /* o: result of division (Word16 Q0) */
    1761             :                  const Word16 n, /* i: numerator   (Word16 Q0         */
    1762             :                  const Word16 d  /* i: denominator (Word16 Q0)        */
    1763             : )
    1764             : {
    1765             :     Word16 norm_n, norm_d;
    1766             :     Word16 ns, ds;
    1767             :     Word16 res;
    1768             : 
    1769        7548 :     test();
    1770        7548 :     IF( n == 0 || d == 0 )
    1771             :     {
    1772         782 :         return 0;
    1773             :     }
    1774             : 
    1775        6766 :     norm_n = norm_s( n );
    1776        6766 :     norm_n = sub( norm_n, 1 );
    1777        6766 :     ns = shl( n, norm_n );
    1778             : 
    1779        6766 :     norm_d = norm_s( d );
    1780        6766 :     ds = shl( d, norm_d );
    1781             : 
    1782        6766 :     res = shr( div_s( ns, ds ), add( sub( norm_n, norm_d ), 15 ) );
    1783             : 
    1784        6766 :     return res;
    1785             : }
    1786             : /*-------------------------------------------------------------------*
    1787             :  * hf_parinitiz()
    1788             :  *
    1789             :  *
    1790             :  *-------------------------------------------------------------------*/
    1791          31 : void hf_parinitiz_fx(
    1792             :     const Word32 L_total_brate,
    1793             :     const Word16 hqswb_clas_fx,
    1794             :     Word16 lowlength_fx,
    1795             :     Word16 highlength_fx,
    1796             :     Word16 wBands_fx[],
    1797             :     const Word16 **subband_search_offset_fx,
    1798             :     const Word16 **subband_offsets_fx,
    1799             :     Word16 *nBands_fx,
    1800             :     Word16 *nBands_search_fx,
    1801             :     Word16 *swb_lowband_fx,
    1802             :     Word16 *swb_highband_fx )
    1803             : {
    1804          31 :     *swb_lowband_fx = lowlength_fx;
    1805          31 :     move16();
    1806          31 :     *swb_highband_fx = highlength_fx;
    1807          31 :     move16();
    1808             : 
    1809          31 :     IF( EQ_16( hqswb_clas_fx, HQ_HARMONIC ) )
    1810             :     {
    1811             :         /* Mode dependent initializations (performed every frame in case mode-switching implemented) */
    1812           0 :         *nBands_fx = NB_SWB_SUBBANDS_HAR;
    1813           0 :         move16();
    1814           0 :         *nBands_search_fx = NB_SWB_SUBBANDS_HAR_SEARCH_SB;
    1815           0 :         move16();
    1816             : 
    1817           0 :         IF( EQ_32( L_total_brate, HQ_13k20 ) )
    1818             :         {
    1819           0 :             wBands_fx[0] = SWB_SB_BW_LEN0_12KBPS_HAR;
    1820           0 :             move16();
    1821           0 :             wBands_fx[1] = SWB_SB_BW_LEN1_12KBPS_HAR;
    1822           0 :             move16();
    1823           0 :             wBands_fx[2] = SWB_SB_BW_LEN2_12KBPS_HAR;
    1824           0 :             move16();
    1825           0 :             wBands_fx[3] = SWB_SB_BW_LEN3_12KBPS_HAR;
    1826           0 :             move16();
    1827           0 :             *subband_offsets_fx = subband_offsets_sub5_13p2kbps_Har;
    1828           0 :             move16();
    1829           0 :             *subband_search_offset_fx = subband_search_offsets_13p2kbps_Har;
    1830           0 :             move16();
    1831             :         }
    1832             :         ELSE
    1833             :         {
    1834           0 :             wBands_fx[0] = SWB_SB_BW_LEN0_16KBPS_HAR;
    1835           0 :             move16();
    1836           0 :             wBands_fx[1] = SWB_SB_BW_LEN1_16KBPS_HAR;
    1837           0 :             move16();
    1838           0 :             wBands_fx[2] = SWB_SB_BW_LEN2_16KBPS_HAR;
    1839           0 :             move16();
    1840           0 :             wBands_fx[3] = SWB_SB_BW_LEN3_16KBPS_HAR;
    1841           0 :             move16();
    1842           0 :             *subband_offsets_fx = subband_offsets_sub5_16p4kbps_Har;
    1843           0 :             move16();
    1844           0 :             *subband_search_offset_fx = subband_search_offsets_16p4kbps_Har;
    1845           0 :             move16();
    1846             :         }
    1847             :     }
    1848             :     ELSE
    1849             :     {
    1850             :         /* Mode-dependent initializations (performed every frame in case mode-switching implemented) */
    1851          31 :         *nBands_fx = NB_SWB_SUBBANDS;
    1852          31 :         move16();
    1853          31 :         *nBands_search_fx = NB_SWB_SUBBANDS;
    1854          31 :         move16();
    1855             : 
    1856          31 :         IF( EQ_32( L_total_brate, HQ_13k20 ) )
    1857             :         {
    1858          31 :             wBands_fx[0] = SWB_SB_LEN0_12KBPS;
    1859          31 :             move16();
    1860          31 :             wBands_fx[1] = SWB_SB_LEN1_12KBPS;
    1861          31 :             move16();
    1862          31 :             wBands_fx[2] = SWB_SB_LEN2_12KBPS;
    1863          31 :             move16();
    1864          31 :             wBands_fx[3] = SWB_SB_LEN3_12KBPS;
    1865          31 :             move16();
    1866          31 :             *subband_offsets_fx = subband_offsets_12KBPS;
    1867          31 :             move16();
    1868             :         }
    1869             :         ELSE
    1870             :         {
    1871           0 :             wBands_fx[0] = SWB_SB_LEN0_16KBPS;
    1872           0 :             move16();
    1873           0 :             wBands_fx[1] = SWB_SB_LEN1_16KBPS;
    1874           0 :             move16();
    1875           0 :             wBands_fx[2] = SWB_SB_LEN2_16KBPS;
    1876           0 :             move16();
    1877           0 :             wBands_fx[3] = SWB_SB_LEN3_16KBPS;
    1878           0 :             move16();
    1879           0 :             *subband_offsets_fx = subband_offsets_16KBPS;
    1880           0 :             move16();
    1881             :         }
    1882             :     }
    1883             : 
    1884          31 :     return;
    1885             : }
    1886             : 
    1887          31 : void GetlagGains_fx(
    1888             :     const Word16 *predBuf_fx,    /* i: Qss  Low freq. Smoothed Spectrum */
    1889             :     const Word16 Qss,            /* i: Q0   Q value of predBuf          */
    1890             :     const Word32 *L_band_energy, /* i: Qbe  Band Energy                 */
    1891             :     const Word16 Qbe,            /* i: Q0   Q value of band energy      */
    1892             :     const Word16 nBands,         /* i: Q0   number of SWB subbands      */
    1893             :     const Word16 *sbWidth,       /* i: Q0   width of SWB subbands       */
    1894             :     const Word16 *lagIndices,    /* i: Q0   lagIndices                  */
    1895             :     const Word16 predBufLen,     /* i: Q0   length of predBuf           */
    1896             :     Word16 *lagGains_fx,         /* o: QlagGains lagGains               */
    1897             :     Word16 *QlagGains            /* o: Q0   Q value of lagGains         */
    1898             : )
    1899             : {
    1900             :     Word16 i;
    1901             :     Word16 sb, fLen, lag;
    1902             : 
    1903             :     Word32 L_outBuf[L_FRAME32k];
    1904             : 
    1905             :     Word16 *ptr_ssBuf_fx;
    1906             :     Word32 L_lagEnergy;
    1907             :     Word16 Qene;
    1908             : 
    1909             :     Word16 temp_lo_fx, temp_hi_fx;
    1910             :     Word16 pow_fx;
    1911             :     Word16 Qpow;
    1912             :     Word16 exp_normd, exp_normn;
    1913             :     Word16 Qdiv;
    1914             :     Word16 exp_norm;
    1915             : 
    1916             :     Word16 temp_fx;
    1917             :     Word32 L_temp;
    1918             : 
    1919             :     Word16 ssBuf_fx[L_FRAME32k];
    1920             :     Word16 exp_norm_ss;
    1921             : 
    1922          31 :     exp_norm_ss = 2;
    1923          31 :     move16();
    1924        7967 :     FOR( i = 0; i < predBufLen; i++ )
    1925             :     {
    1926        7936 :         ssBuf_fx[i] = shr( predBuf_fx[i], exp_norm_ss );
    1927        7936 :         move16(); /* Qss+exp_norm_ss */
    1928             :     }
    1929             : 
    1930         155 :     FOR( sb = 0; sb < nBands; sb++ )
    1931             :     {
    1932         124 :         fLen = sbWidth[sb];
    1933         124 :         move16();
    1934         124 :         lag = lagIndices[sb];
    1935         124 :         move16();
    1936             : 
    1937         124 :         IF( GT_16( add( lag, fLen ), predBufLen ) )
    1938             :         {
    1939             :             /* should never happen */
    1940           0 :             lag = sub( predBufLen, fLen );
    1941           0 :             move16();
    1942             :         }
    1943             : 
    1944         124 :         GetPredictedSignal_fx( predBuf_fx, L_outBuf, lag, fLen, 0x7fff, 15 );
    1945             : 
    1946         124 :         ptr_ssBuf_fx = ssBuf_fx + lag;
    1947         124 :         L_lagEnergy = L_deposit_l( 0 );
    1948        9796 :         FOR( i = 0; i < fLen; i++ )
    1949             :         {
    1950        9672 :             L_lagEnergy = L_mac( L_lagEnergy, *ptr_ssBuf_fx, *ptr_ssBuf_fx ); /* (Qss-exp_norm_ss)*2+1 */
    1951        9672 :             ptr_ssBuf_fx++;
    1952             :         }
    1953         124 :         Qene = add( shl( sub( Qss, exp_norm_ss ), 1 ), 1 );
    1954             : 
    1955         124 :         IF( L_lagEnergy != 0x0L )
    1956             :         {
    1957             :             /* lagGains[sb] = (float)sqrt( pow(2.0f, band_energy[sb]) / lagEnergy ); */
    1958             :             /* Pow part  (pow(2.0f, band_energy) ) */
    1959         124 :             L_temp = L_shr( L_band_energy[sb], sub( Qbe, 16 ) ); /* Qbe -> Q16 */
    1960         124 :             temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
    1961         124 :             Qpow = sub( 14, temp_hi_fx );
    1962         124 :             pow_fx = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qpow */
    1963             : 
    1964             :             /* Div part  ( pow (2.0f, band_energy[i])/lagEenegy ) */
    1965         124 :             exp_normn = norm_s( pow_fx );
    1966         124 :             exp_normn = sub( exp_normn, 1 );
    1967         124 :             exp_normd = norm_l( L_lagEnergy );
    1968         124 :             temp_fx = div_s( shl( pow_fx, exp_normn ), extract_h( L_shl( L_lagEnergy, exp_normd ) ) );
    1969         124 :             Qdiv = add( sub( add( Qpow, exp_normn ), add( Qene, exp_normd ) ), 31 );
    1970             : 
    1971         124 :             exp_norm = norm_s( temp_fx );
    1972         124 :             temp_fx = shl( temp_fx, exp_norm );
    1973         124 :             Qdiv = add( Qdiv, exp_norm );
    1974             : 
    1975             :             /* Sqrt part sqrt(pow (2.0f, band_energy[i])/lagEnergy) */
    1976         124 :             QlagGains[sb] = add( Qdiv, 16 );
    1977         124 :             move16();
    1978         124 :             IF( s_and( Qdiv, 1 ) == 0 ) /* Qdiv % 2 == 0 */
    1979             :             {
    1980          64 :                 L_temp = Sqrt_l( L_shr( L_deposit_h( temp_fx ), 1 ), &exp_norm );
    1981          64 :                 L_temp = L_shr( L_temp, exp_norm );
    1982          64 :                 QlagGains[sb] = sub( shr( QlagGains[sb], 1 ), 1 );
    1983          64 :                 move16();
    1984          64 :                 lagGains_fx[sb] = round_fx( L_temp );
    1985          64 :                 move16();
    1986             :             }
    1987             :             ELSE
    1988             :             {
    1989          60 :                 L_temp = Sqrt_l( L_deposit_h( temp_fx ), &exp_norm );
    1990          60 :                 L_temp = L_shr( L_temp, exp_norm );
    1991          60 :                 QlagGains[sb] = shr( QlagGains[sb], 1 );
    1992          60 :                 move16();
    1993          60 :                 lagGains_fx[sb] = round_fx( L_temp );
    1994          60 :                 move16();
    1995             :             }
    1996             :         }
    1997             :         ELSE
    1998             :         {
    1999             :             /* lagGains[sb] = 0.0f; */
    2000           0 :             lagGains_fx[sb] = 0;
    2001           0 :             move16();
    2002           0 :             QlagGains[sb] = 15;
    2003           0 :             move16();
    2004             :         }
    2005             :     }
    2006             : 
    2007          31 :     return;
    2008             : }
    2009             : 
    2010             : /*--------------------------------------------------------------------------*
    2011             :  * noise_extr_corcod()
    2012             :  * Spectrum normalization for the core coder
    2013             :  *--------------------------------------------------------------------------*/
    2014             : 
    2015           0 : void noise_extr_corcod_fx(
    2016             :     Word32 L_spectra[],          /* i  : QsL core coder                                  */
    2017             :     const Word32 L_spectra_ni[], /* i  : QsL core coder with sparse filling              */
    2018             :     Word16 sspectra_fx[],        /* o  : Qss Smoothed tonal information from core coder  */
    2019             :     Word16 sspectra_diff_fx[],   /* o  : Qss non tonal infomration for gap filling       */
    2020             :     Word16 sspectra_ni_fx[],     /* o  : Qss smoothed core coder                         */
    2021             :     const Word16 fLenLow_fx,     /* i  : Q0  low frequency bands width                   */
    2022             :     Word16 prev_hqswb_clas,      /* i  : Q0  classification information                  */
    2023             :     Word16 *prev_ni_ratio_fx,    /* i  : Q15 noise parameter                             */
    2024             :     Word16 *Qss                  /* o  : Q0  Q value for sspectra_*_fx                   */
    2025             : )
    2026             : {
    2027             :     Word16 i, pulse_num_fx;
    2028             :     Word32 L_spectra_diff[L_FRAME32k]; /* QsL */
    2029             :     Word16 Qss_s, Qss_d;
    2030             :     Word16 ni_ratio_fx, ni_ratio_cur_fx, br_adj_fx; /* Q15 */
    2031             :     Word16 tmp_fx;
    2032             :     Word16 exp_normn, exp_normd, exp_shift;
    2033             :     Word16 exp_norm;
    2034             : 
    2035             :     /*Spectrum Smoothing for tonal signals*/
    2036           0 :     SpectrumSmoothing_nss_fx( L_spectra, sspectra_fx, &Qss_s, fLenLow_fx );
    2037           0 :     Copy( sspectra_fx, sspectra_ni_fx, fLenLow_fx );
    2038           0 :     tmp_fx = 0;
    2039           0 :     move16();
    2040           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2041             :     {
    2042           0 :         tmp_fx |= abs_s( sspectra_fx[i] );
    2043           0 :         logic16();
    2044             :     }
    2045           0 :     exp_norm = norm_s( tmp_fx );
    2046           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2047             :     {
    2048           0 :         sspectra_fx[i] = shl( sspectra_fx[i], exp_norm );
    2049           0 :         move16();
    2050             :     }
    2051           0 :     Qss_s = add( Qss_s, exp_norm );
    2052             : 
    2053             :     /*noise extraction*/
    2054           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2055             :     {
    2056           0 :         L_spectra_diff[i] = L_sub( L_spectra_ni[i], L_spectra[i] );
    2057           0 :         move32();
    2058             :     }
    2059           0 :     SpectrumSmoothing_nss_fx( L_spectra_diff, sspectra_diff_fx, &Qss_d, fLenLow_fx );
    2060           0 :     tmp_fx = 0;
    2061           0 :     move16();
    2062           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2063             :     {
    2064           0 :         tmp_fx |= abs_s( sspectra_diff_fx[i] );
    2065           0 :         logic16();
    2066             :     }
    2067           0 :     exp_norm = norm_s( tmp_fx );
    2068           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2069             :     {
    2070           0 :         sspectra_diff_fx[i] = shl( sspectra_diff_fx[i], exp_norm );
    2071           0 :         move16();
    2072             :     }
    2073           0 :     Qss_d = add( Qss_d, exp_norm );
    2074             : 
    2075           0 :     IF( LT_16( Qss_s, Qss_d ) )
    2076             :     {
    2077           0 :         *Qss = Qss_s;
    2078           0 :         move16();
    2079           0 :         exp_shift = sub( Qss_d, *Qss );
    2080           0 :         FOR( i = 0; i < fLenLow_fx; i++ )
    2081             :         {
    2082           0 :             sspectra_diff_fx[i] = shr( sspectra_diff_fx[i], exp_shift );
    2083           0 :             move16();
    2084             :         }
    2085             :     }
    2086             :     ELSE
    2087             :     {
    2088           0 :         *Qss = Qss_d;
    2089           0 :         move16();
    2090           0 :         exp_shift = sub( Qss_s, *Qss );
    2091           0 :         FOR( i = 0; i < fLenLow_fx; i++ )
    2092             :         {
    2093           0 :             sspectra_fx[i] = shr( sspectra_fx[i], exp_shift );
    2094           0 :             move16();
    2095           0 :             sspectra_ni_fx[i] = shr( sspectra_ni_fx[i], exp_shift );
    2096           0 :             move16();
    2097             :         }
    2098             :     }
    2099             : 
    2100             :     /*Smoothing the noise components*/
    2101           0 :     br_adj_fx = 29491; /* br_adj = 0.9f;  Q15 */
    2102           0 :     move16();
    2103           0 :     pulse_num_fx = 0;
    2104           0 :     move16();
    2105           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2106             :     {
    2107           0 :         if ( L_spectra[i] != 0x0L )
    2108             :         {
    2109           0 :             pulse_num_fx = add( pulse_num_fx, 1 );
    2110             :         }
    2111             :     }
    2112             : 
    2113           0 :     ni_ratio_cur_fx = 0x0;
    2114           0 :     move16();
    2115           0 :     IF( pulse_num_fx != 0 )
    2116             :     {
    2117             :         /*ni_ratio_cur = (fLenLow-pulse_num)/(fLenLow+0.0f);*/
    2118           0 :         tmp_fx = sub( fLenLow_fx, pulse_num_fx );
    2119           0 :         exp_normn = norm_s( tmp_fx );
    2120           0 :         exp_normn = sub( exp_normn, 1 );
    2121           0 :         exp_normd = norm_s( fLenLow_fx );
    2122           0 :         ni_ratio_cur_fx = div_s( shl( tmp_fx, exp_normn ), shl( fLenLow_fx, exp_normd ) ); /* exp_normn - exp_normd + 15 */
    2123           0 :         ni_ratio_cur_fx = shl( ni_ratio_cur_fx, sub( exp_normn, exp_normd ) );             /* 15 - (exp_normn - exp-normd + 15) */
    2124             : 
    2125             :         /*ni_ratio_cur *= br_adj;*/
    2126           0 :         ni_ratio_cur_fx = mult_r( ni_ratio_cur_fx, br_adj_fx );
    2127             :     }
    2128             : 
    2129           0 :     IF( EQ_16( prev_hqswb_clas, HQ_HARMONIC ) )
    2130             :     {
    2131           0 :         IF( GT_16( ni_ratio_cur_fx, *prev_ni_ratio_fx ) )
    2132             :         {
    2133             :             /* 0.8: 26214(Q15) 0.2: 6554(Q15) */
    2134           0 :             ni_ratio_fx = mac_r( L_mult( ni_ratio_cur_fx, 26214 ), *prev_ni_ratio_fx, 6554 );
    2135             :         }
    2136             :         ELSE
    2137             :         {
    2138             :             /* 0.6: 19661(Q15) 0.4: 13107(Q15) */
    2139           0 :             ni_ratio_fx = mac_r( L_mult( ni_ratio_cur_fx, 19661 ), *prev_ni_ratio_fx, 13107 );
    2140             :         }
    2141             :     }
    2142             :     ELSE
    2143             :     {
    2144             :         /* 0.7: 22938(Q15) */
    2145           0 :         ni_ratio_fx = mult_r( ni_ratio_cur_fx, 22938 );
    2146             :     }
    2147           0 :     *prev_ni_ratio_fx = ni_ratio_fx;
    2148           0 :     move16();
    2149             : 
    2150           0 :     FOR( i = 0; i < fLenLow_fx; i++ )
    2151             :     {
    2152           0 :         sspectra_diff_fx[i] = mult_r( sspectra_diff_fx[i], ni_ratio_fx );
    2153           0 :         move16();
    2154           0 :         sspectra_ni_fx[i] = add( sspectra_fx[i], sspectra_diff_fx[i] );
    2155           0 :         move16();
    2156             :     }
    2157             : 
    2158           0 :     return;
    2159             : }
    2160             : 
    2161             : /*--------------------------------------------------------------------------*
    2162             :  * ton_ene_est()
    2163             :  * band energies for missing bands in the core coder
    2164             :  *--------------------------------------------------------------------------*/
    2165             : 
    2166           0 : void ton_ene_est_fx(
    2167             :     Word32 L_xSynth_har[],        /* i/o: QsL  buffer with non tonal compoents   */
    2168             :     const Word16 QsL,             /* i  : Q0   Q value for xSynth_har            */
    2169             :     Word32 L_be_tonal[],          /* o  : QbeL tonal energy of the missing bands */
    2170             :     Word16 *QbeL,                 /* o  : Q0   Q value for be_tonal              */
    2171             :     const Word32 L_band_energy[], /* i  : Qbe  subband energies                  */
    2172             :     const Word16 Qbe,             /* i  : Q0   Q value for band_energy           */
    2173             :     const Word16 band_start[],    /* i  : Q0   subband start indices             */
    2174             :     const Word16 band_end[],      /* i  : Q0   subband end indices               */
    2175             :     const Word16 band_width[],    /* i  : Q0   subband widths                    */
    2176             :     const Word16 fLenLow,         /* i  : Q0   low frequency width               */
    2177             :     const Word16 fLenHigh,        /* i  : Q0   High frequency width              */
    2178             :     const Word16 bands,           /* i  : Q0   total subbands                    */
    2179             :     const Word16 har_bands,       /* i  : Q0   total number of harmonics bands   */
    2180             :     const Word16 ni_lvl_fx,       /* i  : Q11  noise enve for the hf bands       */
    2181             :     GainItem_fx pk_sf_fx[],       /* i  :                                        */
    2182             :     const Word16 Qss,             /* i  : Q0   Q value for GainItem_fx->nmrValue */
    2183             :     const Word16 *pul_res         /* i  : Q0   tonal resolution                  */
    2184             : )
    2185             : {
    2186             :     Word16 i, j, k;
    2187             :     Word16 exp_norm;
    2188             :     Word16 Inv_band_width_fx[BANDS_MAX];
    2189             :     Word16 QInvBW[BANDS_MAX];
    2190             : 
    2191             :     Word16 xSynth_har_fx[L_FRAME32k];
    2192             :     Word16 QxSynth;     /* Q value for xSynth_har_fx */
    2193             :     Word16 QxSynth_sft; /* Q value for xSynth_har_fx */
    2194             : 
    2195             :     Word16 sb_ton_loc_fx[SWB_HAR_RAN1]; /* Q0 */
    2196             :     Word16 sb_ton_fx[SWB_HAR_RAN1];     /* Qss */
    2197             :     Word16 ni_gain_fx[NB_SWB_SUBBANDS];
    2198             :     Word16 Qni_gain;
    2199             :     Word16 avg_pe_fx[NB_SWB_SUBBANDS];
    2200             :     Word16 Qavg_pe[NB_SWB_SUBBANDS];
    2201             :     Word16 QsN;      /* Q value for xSynth_har after multipy ni_lvl */
    2202             :     Word16 exp_safe; /* overflow prevent shift */
    2203             : 
    2204             :     Word16 pos, count_pos_st, count_pos_end;
    2205             :     Word16 pul_res_bnd[NB_SWB_SUBBANDS];
    2206             :     Word16 peak_fx[NB_SWB_SUBBANDS]; /* Qss */
    2207             : 
    2208             :     Word32 L_E;
    2209             :     Word16 QE;
    2210             :     Word16 temp_lo_fx, temp_hi_fx;
    2211             :     Word32 L_temp;
    2212             :     Word16 exp_pow;
    2213             :     Word32 L_band_energy_Linear[BANDS_MAX];
    2214             : 
    2215             :     Word16 exp_normd, exp_normn;
    2216             : 
    2217             :     Word16 E_r_fx;
    2218             :     Word16 QE_r; /* Q value for E_r_fx */
    2219             : 
    2220             :     Word16 exp_shift;
    2221             : 
    2222             :     Word16 E_r_shift_fx;
    2223             :     Word16 fac_fx;
    2224             :     Word16 Qtemp;
    2225             :     Word16 temp2_fx, Qtemp2;
    2226             :     Word16 temp_fx;
    2227             : 
    2228             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    2229           0 :     Flag Overflow = 0;
    2230           0 :     move32();
    2231             : #endif
    2232             : 
    2233           0 :     *QbeL = 3;
    2234           0 :     move16();
    2235           0 :     Qni_gain = 8;
    2236           0 :     move16();
    2237             : 
    2238           0 :     FOR( k = 0; k < bands; k++ )
    2239             :     {
    2240           0 :         exp_norm = norm_s( band_width[k] );
    2241           0 :         exp_norm = sub( exp_norm, 1 );
    2242           0 :         Inv_band_width_fx[k] = div_s( 0x1fff, shl( band_width[k], exp_norm ) ); /* */
    2243           0 :         QInvBW[k] = sub( 28, exp_norm );                                        /* 13-exp_norm+15 */
    2244           0 :         move16();
    2245             :     }
    2246             : 
    2247           0 :     set16_fx( sb_ton_loc_fx, -1, SWB_HAR_RAN1 );
    2248           0 :     set16_fx( ni_gain_fx, 0, NB_SWB_SUBBANDS );
    2249           0 :     set16_fx( avg_pe_fx, 0, NB_SWB_SUBBANDS );
    2250           0 :     set16_fx( Qavg_pe, 0, NB_SWB_SUBBANDS );
    2251           0 :     set16_fx( sb_ton_fx, 0, NB_SWB_SUBBANDS );
    2252           0 :     set16_fx( peak_fx, 0, NB_SWB_SUBBANDS );
    2253           0 :     FOR( i = 0; i < fLenHigh; i++ )
    2254             :     {
    2255           0 :         L_xSynth_har[i] = Mult_32_16( L_xSynth_har[i], ni_lvl_fx ); /* QsL(=12)+11-15=8 */
    2256           0 :         move32();
    2257             :     }
    2258           0 :     QsN = sub( add( QsL, 11 ), 15 );
    2259             : 
    2260           0 :     exp_safe = 4; /*move16();*/
    2261           0 :     move16();
    2262           0 :     norm_vec_32_16_scale_fx( L_xSynth_har, QsN, fLenHigh, xSynth_har_fx, &QxSynth, exp_safe );
    2263             : 
    2264           0 :     pos = 0;
    2265           0 :     move16();
    2266           0 :     i = sub( bands, har_bands );
    2267           0 :     FOR( k = 0; k < i; k++ )
    2268             :     {
    2269           0 :         FOR( j = 0; j < pul_res[k]; j++ )
    2270             :         {
    2271           0 :             sb_ton_loc_fx[pos] = pk_sf_fx[k * 8 + j].gainIndex_fx;
    2272           0 :             move16();
    2273           0 :             sb_ton_fx[pos] = pk_sf_fx[k * 8 + j].nmrValue_fx;
    2274           0 :             move16();
    2275           0 :             pos = add( pos, 1 );
    2276             :         }
    2277             :     }
    2278           0 :     k = 0;
    2279           0 :     move16();
    2280           0 :     pos = 0;
    2281           0 :     move16();
    2282             :     DO
    2283             :     {
    2284           0 :         count_pos_st = pos;
    2285           0 :         WHILE( sb_ton_loc_fx[pos] <= ( band_end[k + har_bands] - fLenLow ) && sb_ton_loc_fx[pos] >= 0 )
    2286             :         {
    2287           0 :             pos = add( pos, 1 );
    2288             :         }
    2289           0 :         count_pos_end = pos;
    2290           0 :         move16();
    2291           0 :         pul_res_bnd[k] = sub( count_pos_end, count_pos_st );
    2292           0 :         move16();
    2293           0 :         if ( pul_res_bnd[k] > 0 )
    2294             :         {
    2295           0 :             peak_fx[k] = abs_s( sb_ton_fx[count_pos_st] );
    2296           0 :             move16();
    2297             :         }
    2298           0 :         k = add( k, 1 );
    2299             :     }
    2300           0 :     WHILE( LT_16( k, NB_SWB_SUBBANDS ) );
    2301             : 
    2302           0 :     k = 0;
    2303           0 :     move16();
    2304             :     /*energy calculation for tonal components*/
    2305           0 :     FOR( i = har_bands; i < bands; i++ )
    2306             :     {
    2307           0 :         L_E = sum2_fx( &xSynth_har_fx[sub( band_start[i], fLenLow )], band_width[i] );
    2308           0 :         QE = add( shl( QxSynth, 1 ), 1 ); /* QxSynth*2+1 */
    2309             : 
    2310             :         /* E_r = (float) E/(float)pow(2.0f,band_energy[i]); -> E * pow(2.0f, -band_energy) */
    2311             :         /* Pow Part */
    2312           0 :         L_temp = L_shr( L_band_energy[i], sub( Qbe, 16 ) );
    2313           0 :         temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
    2314             : 
    2315           0 :         exp_pow = sub( 14, temp_hi_fx );
    2316           0 :         L_band_energy_Linear[i] = Pow2( 14, temp_lo_fx );
    2317           0 :         move32(); /* Qexp_pow */
    2318           0 :         L_band_energy_Linear[i] = L_shl_sat( L_band_energy_Linear[i], sub( *QbeL, exp_pow ) );
    2319           0 :         move32();
    2320             :         /* Div Part */
    2321           0 :         E_r_fx = 0x7fff;
    2322           0 :         move16();
    2323           0 :         QE_r = 0;
    2324           0 :         move16();
    2325           0 :         IF( L_band_energy_Linear[i] != 0x0L )
    2326             :         {
    2327           0 :             exp_normd = norm_l( L_E );
    2328           0 :             exp_normd = sub( exp_normd, 1 );
    2329           0 :             exp_normn = norm_l( L_band_energy_Linear[i] );
    2330             : 
    2331           0 :             E_r_fx = div_s( extract_h( L_shl( L_E, exp_normd ) ), extract_h( L_shl( L_band_energy_Linear[i], exp_normn ) ) );
    2332             :             /* QE_r = (QE-16) - (QbeL+exp_normn-16) + 15; */
    2333           0 :             QE_r = add( sub( add( QE, exp_normd ), add( *QbeL, exp_normn ) ), 15 );
    2334             :         }
    2335             : 
    2336           0 :         L_E = L_shl( L_E, sub( *QbeL, QE ) );
    2337           0 :         QE = *QbeL;
    2338           0 :         move16();
    2339             :         /* 0.06=15729(Q18) */
    2340           0 :         exp_shift = sub( 18, QE_r );
    2341             : #ifdef ISSUE_1796_replace_shl_o
    2342           0 :         E_r_shift_fx = shl_sat( E_r_fx, exp_shift );
    2343             : #else
    2344             :         E_r_shift_fx = shl_o( E_r_fx, exp_shift, &Overflow );
    2345             : #endif
    2346           0 :         IF( LT_16( E_r_shift_fx, 15729 ) ) /* E_r < 0.06  */
    2347             :         {
    2348             :             /* avg_pe[k] = (float) sqrt(pow(2.0f,band_energy[i])/band_width[i]); */
    2349             :             /* Pre SQRT part */
    2350             :             /*L_temp = Mpy_32_16_1(L_band_energy_Linear[i], Inv_band_width_fx[i]);*/ /* QbeL + QInvBW -15 */
    2351           0 :             L_temp = Mult_32_16( L_band_energy_Linear[i], Inv_band_width_fx[i] );    /* QbeL + QInvBW -15 */
    2352           0 :             Qtemp = sub( add( *QbeL, QInvBW[i] ), 15 );
    2353             : 
    2354           0 :             sqrt_32n_16_fx( L_temp, Qtemp, &avg_pe_fx[k], &Qavg_pe[k] );
    2355             : 
    2356           0 :             fac_fx = 19661; /* 0.6(Q15) */
    2357           0 :             move16();
    2358           0 :             IF( pul_res_bnd[k] != 0 )
    2359             :             {
    2360             :                 /* Div Part */
    2361           0 :                 L_temp = Mult_32_16( L_E, Inv_band_width_fx[i] ); /* QE+exp_norm+QInvBW[i]+1 */
    2362           0 :                 Qtemp = sub( add( QE, QInvBW[i] ), 15 );
    2363             : 
    2364             :                 /* SQRT Part */
    2365           0 :                 sqrt_32n_16_fx( L_temp, Qtemp, &temp2_fx, &Qtemp2 );
    2366             : 
    2367             :                 /* Div Part */
    2368           0 :                 exp_normd = norm_s( temp2_fx );
    2369           0 :                 exp_normd = sub( exp_normd, 1 );
    2370           0 :                 exp_normn = norm_s( peak_fx[k] );
    2371             : 
    2372           0 :                 fac_fx = div_s( shl( temp2_fx, exp_normd ), shl( peak_fx[k], exp_normn ) );
    2373           0 :                 fac_fx = shl_sat( fac_fx, sub( add( Qss, exp_normn ), add( Qtemp2, exp_normd ) ) ); /* Qtemp2+exp_normd-(Qss+exp_normn)+15 -> 15*/
    2374             :             }
    2375             : 
    2376           0 :             ni_gain_fx[k] = mult_r( avg_pe_fx[k], fac_fx ); /* Qavg_pe[k] */
    2377           0 :             move16();
    2378             : 
    2379           0 :             L_temp = L_mult( ni_gain_fx[k], ni_gain_fx[k] );
    2380           0 :             L_temp = Mult_32_16( L_temp, E_r_fx );
    2381             : 
    2382             :             /* 0.12f: 257698038 (Q31) */
    2383           0 :             if ( GE_32( L_shl_o( L_temp, sub( 31, add( add( shl( Qavg_pe[k], 1 ), QE_r ), 1 - 15 ) ), &Overflow ), 257698038 ) )
    2384             :             {
    2385           0 :                 ni_gain_fx[k] = mult_r( 1638, ni_gain_fx[k] ); /* 0.05 : 1638(Q15) */
    2386           0 :                 move16();
    2387             :             }
    2388           0 :             Overflow = 0;
    2389           0 :             move16();
    2390           0 :             ni_gain_fx[k] = shl( ni_gain_fx[k], sub( Qni_gain, Qavg_pe[k] ) );
    2391           0 :             move16();
    2392           0 :             assert( Qni_gain == 8 );                     /* 358 is '(short)(1.4*pow(2,Qni_gain))' */
    2393           0 :             ni_gain_fx[k] = s_max( ni_gain_fx[k], 358 ); /* 1.4 -> 22938(Q14) */
    2394           0 :             move16();
    2395           0 :             exp_shift = QsL - ( QxSynth + Qni_gain + 1 ); /* QsL - (QxSynth+Qni_gain+1) */
    2396           0 :             FOR( j = band_start[i]; j <= band_end[i]; j++ )
    2397             :             {
    2398           0 :                 L_xSynth_har[j - fLenLow] = L_shl( L_mult( xSynth_har_fx[j - fLenLow], ni_gain_fx[k] ), exp_shift ); /* QsL - (QxSynth+Qni_gain+1) */
    2399           0 :                 move32();
    2400             :             }
    2401             : 
    2402           0 :             exp_safe = 4; /* move16(); */
    2403           0 :             move16();
    2404           0 :             norm_vec_32_16_scale_fx( &L_xSynth_har[band_start[i] - fLenLow], QsL, band_width[i], &xSynth_har_fx[band_start[i] - fLenLow], &QxSynth_sft, exp_safe );
    2405             : 
    2406           0 :             L_E = sum2_fx( &xSynth_har_fx[sub( band_start[i], fLenLow )], band_width[i] );
    2407           0 :             QE = add( shl( QxSynth_sft, 1 ), 1 );
    2408             : 
    2409           0 :             L_E = L_shl_sat( L_E, sub( *QbeL, QE ) );
    2410           0 :             QE = *QbeL;
    2411           0 :             move16();
    2412             :         }
    2413             :         ELSE
    2414             :         {
    2415             :             /* Q8 -> Q12 */
    2416           0 :             FOR( j = band_start[i]; j <= band_end[i]; j++ )
    2417             :             {
    2418           0 :                 L_xSynth_har[j - fLenLow] = L_shl( L_xSynth_har[j - fLenLow], 4 ); /* Q8(12+11-15) -> Q12 */
    2419           0 :                 move32();
    2420             :             }
    2421             :         }
    2422             : 
    2423           0 :         k = add( k, 1 );
    2424             : 
    2425           0 :         L_be_tonal[i] = L_sub( L_band_energy_Linear[i], L_E );
    2426           0 :         move32();
    2427           0 :         IF( L_be_tonal[i] < 0x0L )
    2428             :         {
    2429           0 :             L_E = L_deposit_l( 0 );
    2430           0 :             FOR( j = ( band_start[i] - fLenLow ); j <= ( band_end[i] - fLenLow ); j++ )
    2431             :             {
    2432           0 :                 temp_fx = round_fx( L_shl( L_xSynth_har[j], ( *QbeL + 7 ) / 2 ) ); /* (12+x-16)*2+1 => QbeL */
    2433           0 :                 L_xSynth_har[j] = L_shr( L_xSynth_har[j], 2 );                     /* 1/4 */
    2434           0 :                 move32();
    2435           0 :                 L_E = L_mac( L_E, temp_fx, temp_fx );
    2436             :             }
    2437             : 
    2438           0 :             L_E = L_shr( L_E, 4 ); /* 1/4 */
    2439           0 :             L_be_tonal[i] = L_sub( L_band_energy_Linear[i], L_E );
    2440           0 :             move32();
    2441             :         }
    2442             :     }
    2443             : 
    2444           0 :     return;
    2445             : }
    2446             : 
    2447             : 
    2448             : /*--------------------------------------------------------------------------*
    2449             :  * Gettonl_scalfact()
    2450             :  * Gap filling for the core coder
    2451             :  *--------------------------------------------------------------------------*/
    2452           0 : void Gettonl_scalfact_fx(
    2453             :     Word32 *L_outBuf,           /* i/o: QsL synthesized spectrum                        */
    2454             :     Word16 QsL,                 /* i  : Q0  Q value for outBuf                          */
    2455             :     const Word32 *L_codbuf,     /* i  : QsL core coder                                  */
    2456             :     const Word16 fLenLow,       /* i  : Q0  lowband length                              */
    2457             :     const Word16 fLenHigh,      /* i  : Q0  highband length                             */
    2458             :     const Word16 harmonic_band, /* i  : Q0  total number of Low frequency bands         */
    2459             :     const Word16 bands,         /* i  : Q0  total number of subbands in a frame         */
    2460             :     Word32 *L_band_energy,      /* i  : Qbe band energy of each subband                 */
    2461             :     Word16 Qbe,                 /* i  : Q0  Q value for band_energy                     */
    2462             :     const Word16 *band_start,   /* i  : Q0  subband start indices                       */
    2463             :     const Word16 *band_end,     /* i  : Q0  subband end indices                         */
    2464             :     const Word16 p2aflags[],    /* i  : Q0  missing bands in the core coder             */
    2465             :     Word32 L_be_tonal[],        /* i  : QbeL tonal energy                               */
    2466             :     Word16 QbeL,                /* i  : Q0  Q value for be_tonal                        */
    2467             :     GainItem_fx *pk_sf_fx,      /* i  :     toanl information for Sparse filling        */
    2468             :     Word16 Qss,                 /* i  : Q0  Q value for pk_sf.nmrValue                  */
    2469             :     Word16 *pul_res_pk          /* i  : Q0  pulse resolution information                */
    2470             : )
    2471             : {
    2472             :     Word16 i, j, tmp;
    2473             :     Word16 sb_ton_fx[SWB_HAR_RAN1];        /* Qss */
    2474             :     Word16 sb_ton_loc_fx[SWB_HAR_RAN1];    /* Q0 */
    2475             :     Word32 L_est_ton_ene[NB_SWB_SUBBANDS]; /* QetEne */
    2476             :     Word16 QetEne;
    2477             :     Word16 band_sf_fx[SWB_HAR_RAN1]; /* Qton_sf */
    2478             :     Word16 pos_fx, k_fx, pos_tmp_fx; /* Q0 */
    2479             :     Word16 exp_safe;
    2480             :     Word16 temp_fx;
    2481             :     Word16 Qtemp;
    2482             : 
    2483             :     Word16 band_pos_fx;
    2484             :     Word16 count_pos_st_fx, count_pos_end_fx;
    2485             : 
    2486             :     Word16 exp_normd, exp_normn;
    2487             : 
    2488             :     Word16 ton_sf_fx; /* Qton_sf */
    2489             :     Word16 Qton_sf;
    2490             : 
    2491             :     Word16 step_fx; /* Q15 */
    2492             :     Word32 L_temp;
    2493             : 
    2494             :     Word16 enrd_r_fx; /* Q15 */
    2495             : 
    2496             :     Word32 L_band_energy_Linear[BANDS_MAX]; /* QbeL */
    2497             :     Word16 temp_hi_fx, temp_lo_fx;
    2498             :     Word16 exp_pow;
    2499             :     Word16 exp_shift;
    2500             : 
    2501             :     Word16 Qbsf2[SWB_HAR_RAN1];
    2502             : 
    2503           0 :     Qton_sf = sub( sub( QsL, Qss ), 1 );
    2504             : 
    2505           0 :     enrd_r_fx = 29491;
    2506           0 :     move16(); /* 0.9: 29491.2(Q15) */
    2507             : 
    2508           0 :     set32_fx( L_est_ton_ene, 0x0L, NB_SWB_SUBBANDS );
    2509           0 :     set16_fx( sb_ton_loc_fx, -1, SWB_HAR_RAN1 );
    2510             : 
    2511             :     /* Get the tonal information for sparse filling  */
    2512           0 :     pos_fx = 0;
    2513           0 :     move16();
    2514           0 :     FOR( k_fx = 0; k_fx < sub( bands, harmonic_band ); k_fx++ )
    2515             :     {
    2516           0 :         FOR( j = 0; j < pul_res_pk[k_fx]; j++ )
    2517             :         {
    2518           0 :             sb_ton_loc_fx[pos_fx] = pk_sf_fx[k_fx * 8 + j].gainIndex_fx;
    2519           0 :             move16();
    2520           0 :             sb_ton_fx[pos_fx] = pk_sf_fx[k_fx * 8 + j].nmrValue_fx;
    2521           0 :             move16();
    2522           0 :             pos_fx = add( pos_fx, 1 );
    2523             :         }
    2524             :     }
    2525           0 :     k_fx = 0;
    2526           0 :     move16();
    2527           0 :     pos_fx = 0;
    2528           0 :     move16();
    2529           0 :     pos_tmp_fx = 0;
    2530           0 :     move16();
    2531             : 
    2532             :     DO
    2533             :     {
    2534           0 :         band_pos_fx = add( k_fx, harmonic_band );
    2535           0 :         count_pos_st_fx = pos_fx;
    2536           0 :         move16();
    2537           0 :         test();
    2538           0 :         WHILE( LE_16( sb_ton_loc_fx[pos_fx], sub( band_end[band_pos_fx], fLenLow ) ) && sb_ton_loc_fx[pos_fx] >= 0 )
    2539             :         {
    2540           0 :             test();
    2541           0 :             pos_fx = add( pos_fx, 1 );
    2542             :         }
    2543           0 :         count_pos_end_fx = pos_fx;
    2544           0 :         move16();
    2545             : 
    2546           0 :         exp_safe = 2; /* move16(); */
    2547           0 :         move16();
    2548           0 :         QetEne = add( shl( sub( Qss, exp_safe ), 1 ), 1 );
    2549           0 :         L_temp = L_add( L_est_ton_ene[k_fx], 0 );
    2550           0 :         FOR( i = count_pos_st_fx; i < count_pos_end_fx; i++ )
    2551             :         {
    2552           0 :             temp_fx = shr( sb_ton_fx[i], exp_safe );
    2553           0 :             L_temp = L_mac( L_temp, temp_fx, temp_fx );
    2554             :         }
    2555           0 :         L_est_ton_ene[k_fx] = L_temp;
    2556           0 :         move32();
    2557             : 
    2558           0 :         IF( L_est_ton_ene[k_fx] <= 0x0L )
    2559             :         {
    2560             :             /* 1.0 */
    2561           0 :             L_est_ton_ene[k_fx] = L_deposit_l( MAX_16 );
    2562           0 :             QetEne = 15;
    2563           0 :             move16();
    2564             :         }
    2565             : 
    2566             :         /*ton_sf= (float) sqrt(be_tonal[band_pos]/est_ton_ene[k]);*/ /* be_tonal: QbeL,  est_ton_ene: (Qss-exp_safe)*2+1 */
    2567             :         /* Div Part */
    2568           0 :         exp_normd = norm_l( L_be_tonal[band_pos_fx] );
    2569           0 :         exp_normd = sub( exp_normd, 1 );
    2570           0 :         exp_normn = norm_l( L_est_ton_ene[k_fx] );
    2571             : 
    2572           0 :         ton_sf_fx = 0x0;
    2573           0 :         move16();
    2574           0 :         IF( L_be_tonal[band_pos_fx] > 0x0L )
    2575             :         {
    2576           0 :             ton_sf_fx = div_l( L_shl( L_be_tonal[band_pos_fx], exp_normd ), extract_h( L_shl( L_est_ton_ene[k_fx], exp_normn ) ) ); /* QbeL+exp_normd - (QetEne+exp_normn-16) - 1 */
    2577             :         }
    2578             : 
    2579             :         /* Sqrt Part */
    2580             :         /*sqrt_32n_16_fx(L_deposit_h(ton_sf_fx), (QbeL+exp_normd)-(QetEne+exp_normn-16)-1+16, &ton_sf_fx, &Qton_sf);*/
    2581           0 :         sqrt_32n_16_fx( L_deposit_h( ton_sf_fx ), add( sub( add( QbeL, exp_normd ), add( QetEne, exp_normn ) ), 31 ), &ton_sf_fx, &Qton_sf );
    2582             : 
    2583           0 :         FOR( i = count_pos_st_fx; i < count_pos_end_fx; i++ )
    2584             :         {
    2585           0 :             band_sf_fx[pos_tmp_fx] = ton_sf_fx;
    2586           0 :             move16();
    2587           0 :             Qbsf2[pos_tmp_fx] = Qton_sf;
    2588           0 :             move16();
    2589           0 :             pos_tmp_fx = add( pos_tmp_fx, 1 );
    2590             :         }
    2591           0 :         k_fx = add( k_fx, 1 );
    2592             :     }
    2593           0 :     WHILE( LT_16( k_fx, NB_SWB_SUBBANDS ) );
    2594             : 
    2595             :     /* Gap filling for the core coder  */
    2596             :     /* 0.077=20185(Q18) */
    2597           0 :     L_temp = L_mult( 20185, fLenHigh ); /* 18+0+1= 19 */
    2598           0 :     exp_normn = norm_l( L_temp );
    2599             : 
    2600           0 :     step_fx = div_s( 0x3fff, extract_h( L_shl( L_temp, exp_normn ) ) );
    2601           0 :     step_fx = shl( step_fx, sub( exp_normn, 11 ) ); /* 15 - (14-(19+exp_normn-16)+15) */ /* Q15 */
    2602             : 
    2603           0 :     pos_tmp_fx = 0;
    2604           0 :     move16();
    2605           0 :     tmp = sub( bands, harmonic_band );
    2606           0 :     FOR( k_fx = 0; k_fx < tmp; k_fx++ )
    2607             :     {
    2608           0 :         band_pos_fx = add( k_fx, harmonic_band );
    2609             : 
    2610           0 :         IF( L_be_tonal[band_pos_fx] > 0x0L )
    2611             :         {
    2612             :             /* enrd_r *=(float)sqrt(be_tonal[band_pos]/pow(2.0f,band_energy[band_pos])); */
    2613             :             /* Pow Part */
    2614           0 :             L_temp = L_shr( L_band_energy[band_pos_fx], sub( Qbe, 16 ) );
    2615           0 :             temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
    2616           0 :             exp_pow = sub( 14, temp_hi_fx );
    2617           0 :             L_band_energy_Linear[band_pos_fx] = Pow2( 14, temp_lo_fx );
    2618           0 :             move32(); /* Qexp_pow */
    2619           0 :             L_band_energy_Linear[band_pos_fx] = L_shl_sat( L_band_energy_Linear[band_pos_fx], sub( QbeL, exp_pow ) );
    2620           0 :             move32();
    2621             :             /* Div Part */
    2622           0 :             exp_normd = norm_l( L_be_tonal[band_pos_fx] );
    2623           0 :             exp_normd = sub( exp_normd, 1 );
    2624           0 :             exp_normn = norm_l( L_band_energy_Linear[band_pos_fx] );
    2625           0 :             temp_fx = div_l( L_shl( L_be_tonal[band_pos_fx], exp_normd ), extract_h( L_shl( L_band_energy_Linear[band_pos_fx], exp_normn ) ) ); /* QbeL+exp_normd-(QbeL+exp_normn-16)-1 */
    2626           0 :             sqrt_32n_16_fx( L_deposit_h( temp_fx ), add( sub( exp_normd, exp_normn ), 31 ), &temp_fx, &Qtemp );
    2627           0 :             enrd_r_fx = extract_h( L_shl_sat( L_mult( enrd_r_fx, temp_fx ), sub( 15, Qtemp ) ) );
    2628           0 :             enrd_r_fx = sub( enrd_r_fx, step_fx );
    2629             :         }
    2630             :         ELSE
    2631             :         {
    2632           0 :             enrd_r_fx = 0x0;
    2633           0 :             move16();
    2634             :         }
    2635             : 
    2636           0 :         IF( EQ_16( p2aflags[band_pos_fx], 1 ) )
    2637             :         {
    2638           0 :             FOR( i = band_start[band_pos_fx]; i <= band_end[band_pos_fx]; i++ )
    2639             :             {
    2640           0 :                 L_outBuf[i - fLenLow] = L_codbuf[i];
    2641           0 :                 move32();
    2642             :             }
    2643             :         }
    2644             :         ELSE
    2645             :         {
    2646           0 :             pos_fx = 0;
    2647           0 :             move16();
    2648           0 :             pos_fx = add( pos_fx, pos_tmp_fx );
    2649           0 :             exp_shift = sub( sub( QsL, Qss ), 1 );
    2650           0 :             FOR( j = 0; j < pul_res_pk[k_fx]; j++ )
    2651             :             {
    2652             :                 /*outBuf[pk_sf[k*8+j].gainIndex] = pk_sf[k*8+j].nmrValue*band_sf[pos]*enrd_r;*/
    2653             :                 /* outBuf:QsL , pk_sf.nmrValue:Qss, band_sf:Qbsf2(Qton_sf), enrd_r:Q15 */
    2654           0 :                 L_outBuf[pk_sf_fx[k_fx * 8 + j].gainIndex_fx] = L_shl( Mult_32_16( L_mult( band_sf_fx[pos_fx], enrd_r_fx ), pk_sf_fx[k_fx * 8 + j].nmrValue_fx ), sub( exp_shift, Qbsf2[pos_fx] ) );
    2655           0 :                 move32(); /* QsL - (Qbsf2[pos_fx]+Qss+16-15) */
    2656             : 
    2657           0 :                 pos_fx = add( pos_fx, 1 );
    2658             :             }
    2659             :         }
    2660           0 :         pos_tmp_fx = add( pos_tmp_fx, pul_res_pk[k_fx] );
    2661             :     }
    2662             : 
    2663           0 :     return;
    2664             : }
    2665             : 
    2666             : 
    2667             : /*-------------------------------------------------------------------*
    2668             :  * sqrt_32n_16_fx()
    2669             :  *
    2670             :  * Routine for Sqrt
    2671             :  *-------------------------------------------------------------------*/
    2672         244 : void sqrt_32n_16_fx(
    2673             :     Word32 L_in,    /* i  : input vector (Word32)       */
    2674             :     Word16 Qin,     /* i  : Q value for L_in            */
    2675             :     Word16 *out_fx, /* o  : sqrt input vector (Word16)  */
    2676             :     Word16 *Qout    /* o  : Q value for out_fx          */
    2677             : )
    2678             : {
    2679             :     Word16 exp_norm;
    2680             :     Word32 L_in_t;
    2681             :     Word16 Qin_t;
    2682             : 
    2683             :     /* Input Normalization */
    2684         244 :     exp_norm = norm_l( L_in );
    2685         244 :     L_in_t = L_shl( L_in, exp_norm );
    2686         244 :     Qin_t = add( Qin, exp_norm );
    2687             : 
    2688             :     /* SQRT part */
    2689         244 :     IF( s_and( Qin_t, 1 ) == 0 )
    2690             :     {
    2691         110 :         L_in_t = Sqrt_l( L_shr( L_in_t, 1 ), &exp_norm );
    2692         110 :         L_in_t = L_shr( L_in_t, exp_norm );
    2693         110 :         *Qout = sub( shr( Qin_t, 1 ), 1 );
    2694             :     }
    2695             :     ELSE
    2696             :     {
    2697         134 :         L_in_t = Sqrt_l( L_in_t, &exp_norm );
    2698         134 :         L_in_t = L_shr( L_in_t, exp_norm );
    2699         134 :         *Qout = shr( Qin_t, 1 );
    2700             :     }
    2701         244 :     move16();
    2702         244 :     *out_fx = round_fx( L_in_t );
    2703         244 : }
    2704             : 
    2705             : /*-------------------------------------------------------------------*
    2706             :  * norm_vec_32_16_scale_fx()
    2707             :  *
    2708             :  * Routine for normilization and convert Word32 to Word16
    2709             :  *-------------------------------------------------------------------*/
    2710          31 : void norm_vec_32_16_scale_fx(
    2711             :     Word32 *L_vec_in,   /* i  : input vector                                        */
    2712             :     Word16 Qin,         /* i  : Q value for input vector                            */
    2713             :     Word16 length_fx,   /* i  :vector size                                          */
    2714             :     Word16 *vec_out_fx, /* o  : output vectror                                      */
    2715             :     Word16 *Qout,       /* o  : Q value for output vectro                           */
    2716             :     Word16 exp_safe     /* i  : suppress left shift: for prevend overflow on sum    */
    2717             : )
    2718             : {
    2719             :     Word16 i;
    2720             :     Word32 L_temp;
    2721             :     Word16 exp_norm, exp_shift;
    2722             : 
    2723          31 :     L_temp = L_deposit_l( 0 );
    2724        9703 :     FOR( i = 0; i < length_fx; i++ )
    2725             :     {
    2726        9672 :         L_temp = L_or( L_temp, L_abs( L_vec_in[i] ) );
    2727             :     }
    2728          31 :     exp_norm = norm_l( L_temp );
    2729             : 
    2730          31 :     exp_shift = sub( exp_norm, exp_safe );
    2731          31 :     *Qout = sub( add( Qin, exp_shift ), 16 );
    2732        9703 :     FOR( i = 0; i < length_fx; i++ )
    2733             :     {
    2734        9672 :         vec_out_fx[i] = round_fx( L_shl( L_vec_in[i], exp_shift ) );
    2735        9672 :         move16();
    2736             :     }
    2737          31 : }
    2738             : 
    2739           0 : void get_sigma_fx_har(
    2740             :     const Word32 L_x_abs[], /* i: Qi     absolute input    */
    2741             :     const Word16 Qi,        /* i: Q0     Q value of x_abs  */
    2742             :     const Word16 avg_fx,    /* i: Qavg   average of x_abs  */
    2743             :     const Word16 Qavg,      /* i: Q0     Q value of avg    */
    2744             :     const Word16 length_fx, /* i: Q0     length            */
    2745             :     Word16 *sigma_fx,       /* o: Qsigma sigma             */
    2746             :     Word16 *Qsigma          /* o: Q0     Q value of sigma  */
    2747             : )
    2748             : {
    2749             :     Word16 i;
    2750             :     Word32 L_d;
    2751             :     Word16 d_fx;
    2752             : 
    2753             :     Word16 length1_fx;
    2754             :     Word16 exp_normd;
    2755             :     Word16 exp_normn;
    2756             :     Word16 exp_shift;
    2757             : 
    2758             :     Word16 exp_norm;
    2759             : 
    2760             :     Word32 L_temp;
    2761             : 
    2762             :     Word16 temp_fx;
    2763             : 
    2764             :     Word32 L_tmp;
    2765             :     Word32 L_x_abs_sh[L_FRAME32k];
    2766             :     Word16 Qd;
    2767             : 
    2768             :     Word16 exp_safe;
    2769             : 
    2770           0 :     exp_safe = 4;
    2771           0 :     move16(); /* max 103 < 2^7  -> 4+4=8 */
    2772             : 
    2773           0 :     L_tmp = L_deposit_l( 0 );
    2774           0 :     FOR( i = 0; i < length_fx; i++ )
    2775             :     {
    2776           0 :         L_tmp = L_or( L_tmp, L_x_abs[i] );
    2777             :     }
    2778           0 :     exp_norm = norm_l( L_tmp );
    2779           0 :     exp_norm = sub( exp_norm, exp_safe );
    2780           0 :     FOR( i = 0; i < length_fx; i++ )
    2781             :     {
    2782           0 :         L_x_abs_sh[i] = L_shl( L_x_abs[i], exp_norm );
    2783           0 :         move32();
    2784             :     }
    2785             : 
    2786           0 :     L_d = L_deposit_l( 0 );
    2787           0 :     FOR( i = 0; i < length_fx; i++ )
    2788             :     {
    2789           0 :         temp_fx = extract_h( L_x_abs_sh[i] );
    2790           0 :         L_d = L_mac( L_d, temp_fx, temp_fx ); /* (Qi+exp_norm-16)*2+1 */
    2791             :     }
    2792           0 :     Qd = add( shl( sub( add( Qi, exp_norm ), 16 ), 1 ), 1 );
    2793           0 :     IF( L_d == 0x0L )
    2794             :     {
    2795           0 :         *sigma_fx = 0;
    2796           0 :         move16();
    2797           0 :         *Qsigma = 15;
    2798           0 :         move16();
    2799           0 :         return;
    2800             :     }
    2801             : 
    2802             :     /* d /= (length-1);  */
    2803           0 :     length1_fx = sub( length_fx, 1 );
    2804             : 
    2805             :     /* d /= (length-1);  */
    2806           0 :     exp_normn = norm_l( L_d );
    2807           0 :     exp_normn = sub( exp_normn, 1 );
    2808           0 :     exp_normd = norm_s( length1_fx );
    2809             : 
    2810           0 :     exp_shift = sub( sub( exp_normn, exp_normd ), 1 );
    2811           0 :     d_fx = div_l( L_shl( L_d, exp_normn ), shl( length1_fx, exp_normd ) ); /* Qabs*2+1+exp_normn - exp_normd - 1 */
    2812           0 :     L_d = L_shr( L_deposit_l( d_fx ), exp_shift );                         /* Qabs*2+1 + 16 */
    2813             :     /* Qd = ((Qd+exp_normn) - exp_normd-exp_shift -1); */
    2814           0 :     Qd = sub( sub( add( Qd, exp_normn ), add( exp_normd, exp_shift ) ), 1 );
    2815             : 
    2816             :     /* d -= avg*avg; */
    2817           0 :     IF( avg_fx != 0x0 )
    2818             :     {
    2819           0 :         L_tmp = L_mult( avg_fx, avg_fx ); /* Qavg*2+1 */
    2820           0 :         L_tmp = L_shr( L_tmp, sub( add( shl( Qavg, 1 ), 1 ), Qd ) );
    2821           0 :         L_d = L_sub( L_d, L_tmp );
    2822             :     }
    2823             : 
    2824           0 :     exp_norm = norm_l( L_d );
    2825           0 :     L_d = L_shl( L_d, exp_norm );
    2826           0 :     Qd = add( Qd, exp_norm );
    2827             : 
    2828             :     /* sigma = (float)sqrt(d); */
    2829           0 :     IF( s_and( Qd, 1 ) == 0 ) /* Qd % 2 == 0 */
    2830             :     {
    2831           0 :         L_temp = Sqrt_l( L_shr( L_d, 1 ), &exp_norm );
    2832           0 :         L_temp = L_shr( L_temp, exp_norm );
    2833           0 :         *Qsigma = sub( shr( Qd, 1 ), 1 );
    2834           0 :         *sigma_fx = round_fx( L_temp );
    2835             :     }
    2836             :     ELSE
    2837             :     {
    2838           0 :         L_temp = Sqrt_l( L_d, &exp_norm );
    2839           0 :         L_temp = L_shr( L_temp, exp_norm );
    2840           0 :         *Qsigma = shr( Qd, 1 );
    2841           0 :         *sigma_fx = round_fx( L_temp );
    2842             :     }
    2843           0 :     move16();
    2844           0 :     move16();
    2845           0 :     return;
    2846             : }
    2847             : 
    2848           0 : void FindNBiggest2_simple_fx_har(
    2849             :     const Word32 *L_inBuf,  /* i  : input buffer (searched)                     */
    2850             :     const Word16 Qabs_in,   /* i  : Q value of input buffer                     */
    2851             :     GainItem_fx *pk_sf_fx,  /* o  : N biggest components found                  */
    2852             :     const Word16 nIdx_fx,   /* i  : search length                               */
    2853             :     Word16 *n_fx,           /* i  : number of components searched (N biggest)   */
    2854             :     Word16 n_nbiggestsearch /* i  :                                             */
    2855             : )
    2856             : {
    2857             :     Word16 j;
    2858             :     Word32 L_avg_in;
    2859             :     Word32 L_abs_in[400];
    2860             :     Word32 L_abs_in_sft[400];
    2861             :     Word16 avg_in_fx;
    2862             :     Word32 L_max_in;
    2863             :     Word16 Qavg_in;
    2864             : 
    2865             :     Word16 exp_normd;
    2866             :     Word16 exp_normn;
    2867             : 
    2868             :     Word16 temp_fx;
    2869             : 
    2870             :     Word16 sigma_fx;
    2871             :     Word16 Qsigma;
    2872             : 
    2873             :     Word16 peak_cnt_fx;
    2874             :     Word32 L_thr;
    2875             :     Word32 L_temp;
    2876             : 
    2877           0 :     L_max_in = L_deposit_l( 0 );
    2878           0 :     L_avg_in = L_deposit_l( 0 );
    2879             : 
    2880           0 :     FOR( j = 0; j < nIdx_fx; j++ )
    2881             :     {
    2882           0 :         L_abs_in[j] = L_abs( L_inBuf[j] );         /* Qabs_in */
    2883           0 :         L_abs_in_sft[j] = L_shr( L_abs_in[j], 8 ); /* 8 is safe shift */
    2884           0 :         move32();
    2885           0 :         move32();
    2886           0 :         if ( LT_32( L_max_in, L_abs_in_sft[j] ) )
    2887             :         {
    2888           0 :             L_max_in = L_abs_in_sft[j];
    2889           0 :             move32();
    2890             :         }
    2891             : 
    2892           0 :         L_avg_in = L_add( L_avg_in, L_abs_in_sft[j] );
    2893             :     }
    2894             : 
    2895             :     /*avg_in /= (float)nIdx;*/
    2896           0 :     avg_in_fx = 0;
    2897           0 :     move16();
    2898           0 :     Qavg_in = 15;
    2899           0 :     move16();
    2900           0 :     IF( L_avg_in != 0 )
    2901             :     {
    2902           0 :         exp_normn = norm_l( L_avg_in );
    2903           0 :         exp_normn = sub( exp_normn, 1 );
    2904           0 :         L_avg_in = L_shl( L_avg_in, exp_normn );
    2905           0 :         exp_normd = norm_s( nIdx_fx );
    2906           0 :         temp_fx = shl( nIdx_fx, exp_normd );
    2907           0 :         avg_in_fx = div_l( L_avg_in, temp_fx );
    2908           0 :         Qavg_in = sub( add( sub( Qabs_in, 8 ), exp_normn ), add( exp_normd, 1 ) );
    2909             :     }
    2910             : 
    2911           0 :     peak_cnt_fx = 0;
    2912           0 :     move16();
    2913           0 :     IF( LE_32( L_max_in, 0x1 ) )
    2914             :     {
    2915           0 :         FOR( j = 0; j < n_nbiggestsearch; j++ )
    2916             :         {
    2917           0 :             pk_sf_fx[peak_cnt_fx].nmrValue_fx = 0x0;
    2918           0 :             move16();
    2919           0 :             pk_sf_fx[peak_cnt_fx].gainIndex_fx = j;
    2920           0 :             move16();
    2921           0 :             peak_cnt_fx = add( peak_cnt_fx, 1 );
    2922             :         }
    2923             :     }
    2924             : 
    2925           0 :     get_sigma_fx_har( L_abs_in, Qabs_in, avg_in_fx, Qavg_in, nIdx_fx, &sigma_fx, &Qsigma );
    2926             : 
    2927           0 :     temp_fx = mult_r( sigma_fx, 18841 ); /* 18841 = 1.15(Q14)  Qsigma + Q14 + 1 */
    2928           0 :     L_thr = L_add( extract_l( avg_in_fx ), L_shr( extract_l( temp_fx ), sub( sub( Qsigma, 1 ), Qavg_in ) ) );
    2929           0 :     L_thr = L_shr( L_thr, sub( Qavg_in, Qabs_in ) );
    2930             : 
    2931           0 :     IF( LT_16( peak_cnt_fx, n_nbiggestsearch ) )
    2932             :     {
    2933           0 :         FOR( j = 0; j < nIdx_fx; j++ )
    2934             :         {
    2935           0 :             IF( GT_32( L_abs_in[j], L_thr ) )
    2936             :             {
    2937           0 :                 pk_sf_fx[peak_cnt_fx].nmrValue_fx = round_fx( L_abs_in[j] ); /* Qabs_in-16 */
    2938           0 :                 move16();
    2939           0 :                 pk_sf_fx[peak_cnt_fx].gainIndex_fx = j;
    2940           0 :                 move16();
    2941           0 :                 L_abs_in[j] = L_deposit_l( 0 );
    2942           0 :                 peak_cnt_fx = add( peak_cnt_fx, 1 );
    2943           0 :                 move16();
    2944             :             }
    2945             : 
    2946           0 :             IF( EQ_16( peak_cnt_fx, n_nbiggestsearch ) )
    2947             :             {
    2948           0 :                 BREAK;
    2949             :             }
    2950             :         }
    2951             :     }
    2952             : 
    2953             :     /* thr *= (0.3f / n_nbiggestsearch) * peak_cnt + 0.7f; */
    2954             :     /* 0.3=19661(Q16) */
    2955           0 :     temp_fx = div_s_ss( 19661, n_nbiggestsearch );
    2956           0 :     L_temp = L_mult( temp_fx, peak_cnt_fx );                                             /* 16+0+1 */
    2957           0 :     temp_fx = add( round_fx( L_shl( L_temp, 14 ) ), 22938 ); /* shift: 17+14-16 -> 15 */ /* 0.7(22937.6:Q15)*/
    2958           0 :     L_thr = Mult_32_16( L_thr, temp_fx );
    2959             : 
    2960           0 :     IF( LT_16( peak_cnt_fx, n_nbiggestsearch ) )
    2961             :     {
    2962           0 :         FOR( j = 0; j < nIdx_fx; j++ )
    2963             :         {
    2964           0 :             IF( GT_32( L_abs_in[j], L_thr ) )
    2965             :             {
    2966           0 :                 pk_sf_fx[peak_cnt_fx].nmrValue_fx = round_fx( L_abs_in[j] ); /* Qabs_in-16 */
    2967           0 :                 pk_sf_fx[peak_cnt_fx].gainIndex_fx = j;
    2968           0 :                 move16();
    2969           0 :                 L_abs_in[j] = L_deposit_l( 0 );
    2970           0 :                 peak_cnt_fx = add( peak_cnt_fx, 1 );
    2971           0 :                 move16();
    2972             :             }
    2973             : 
    2974           0 :             IF( EQ_16( peak_cnt_fx, n_nbiggestsearch ) )
    2975             :             {
    2976           0 :                 BREAK;
    2977             :             }
    2978             :         }
    2979             :     }
    2980             : 
    2981             :     /* thr *= (0.6f / n_nbiggestsearch) * peak_cnt + 0.3f; */
    2982             :     /* 0.6=19661(Q15) */
    2983           0 :     temp_fx = div_s_ss( 19661, n_nbiggestsearch );
    2984           0 :     L_temp = L_mult( temp_fx, peak_cnt_fx );                                            /* 15+0+1 */
    2985           0 :     temp_fx = add( round_fx( L_shl( L_temp, 15 ) ), 9830 ); /* shift: 16+15-16 -> 15 */ /* 0.3(9830.4:Q15)*/
    2986             : 
    2987           0 :     L_thr = Mult_32_16( L_thr, temp_fx );
    2988           0 :     IF( LT_16( peak_cnt_fx, n_nbiggestsearch ) )
    2989             :     {
    2990           0 :         FOR( j = 0; j < nIdx_fx; j++ )
    2991             :         {
    2992           0 :             IF( GT_32( L_abs_in[j], L_thr ) )
    2993             :             {
    2994           0 :                 pk_sf_fx[peak_cnt_fx].nmrValue_fx = round_fx( L_abs_in[j] ); /* Qabs_in-16 */
    2995           0 :                 pk_sf_fx[peak_cnt_fx].gainIndex_fx = j;
    2996           0 :                 move16();
    2997           0 :                 L_abs_in[j] = L_deposit_l( 0 );
    2998           0 :                 peak_cnt_fx = add( peak_cnt_fx, 1 );
    2999           0 :                 move16();
    3000             :             }
    3001             : 
    3002           0 :             IF( EQ_16( peak_cnt_fx, n_nbiggestsearch ) )
    3003             :             {
    3004           0 :                 BREAK;
    3005             :             }
    3006             :         }
    3007             :     }
    3008             : 
    3009           0 :     *n_fx = peak_cnt_fx;
    3010           0 :     move16();
    3011           0 : }
    3012             : 
    3013             : /*--------------------------------------------------------------------------*
    3014             :  * spectrumsmooth_noiseton()
    3015             :  * Spectrum normalization for the the core coder
    3016             :  *--------------------------------------------------------------------------*/
    3017          31 : Word16 spectrumsmooth_noiseton_fx(                              /* o  : Qss  ss_min                                      */
    3018             :                                    Word32 L_spectra[],          /* i  : Qs   core coder                                  */
    3019             :                                    /*Word16 Qs,*/               /* i  : Q0   Q value for spectra, spectra_ni             */
    3020             :                                    const Word32 L_spectra_ni[], /* i  : Qs   core coder with sparse filling              */
    3021             :                                    Word16 sspectra_fx[],        /* o  : Qss  Smoothed tonal information from core coder  */
    3022             :                                    Word16 sspectra_diff_fx[],   /* o  : Qss  non tonal infomration for gap filling       */
    3023             :                                    Word16 sspectra_ni_fx[],     /* o  : Qss  smoothed core coder                         */
    3024             :                                    Word16 *Qss,                 /* o  : Q0   Q value for sspectra*                       */
    3025             :                                    const Word16 fLenLow_fx,     /* i  : Q0   low frequency boundaries                    */
    3026             :                                    Word16 *ni_seed_fx           /* io : Q0   random seed                                 */
    3027             : )
    3028             : {
    3029             :     Word16 i;
    3030             :     Word32 L_spectra_diff[L_FRAME32k];
    3031             :     Word16 ni_ratio_fx;   /* Q15 */
    3032             :     Word16 ss_min_fx;     /* Q10 */
    3033             :     Word16 cut_sig_th_fx; /* Q10 */
    3034             :     Word16 cut_ni_th_fx;  /* Q10 */
    3035             :     Word16 pcnt_fx, sign_fx;
    3036             :     Word16 exp_normn, exp_normd;
    3037             : 
    3038             :     Word16 ratio_fx;
    3039             :     Word32 L_temp;
    3040             :     Word32 L_spectra_rm[L_FRAME32k];
    3041          31 :     Word32 L_cut_input = 410;
    3042          31 :     move32();
    3043             :     Word16 rand_a_fx[L_FRAME32k];
    3044             : 
    3045             : 
    3046             :     /* pre-prepare random array for float-fix interoperability */
    3047        7967 :     FOR( i = 0; i < fLenLow_fx; i++ )
    3048             :     {
    3049        7936 :         rand_a_fx[i] = Random( ni_seed_fx );
    3050        7936 :         move16();
    3051             :     }
    3052             : 
    3053             : 
    3054             :     /*Get the pulse resolution for the core coder*/
    3055          31 :     pcnt_fx = 0;
    3056          31 :     move16();
    3057        7967 :     FOR( i = 0; i < fLenLow_fx; i++ )
    3058             :     {
    3059        7936 :         if ( L_spectra[i] != 0x0L )
    3060             :         {
    3061         856 :             pcnt_fx = add( pcnt_fx, 1 );
    3062             :         }
    3063             :     }
    3064             : 
    3065             :     /*ni_ratio = 4.0f*(pcnt)/(fLenLow+0.0f);*/
    3066          31 :     exp_normn = norm_s( pcnt_fx );
    3067          31 :     exp_normn = sub( exp_normn, 1 );
    3068          31 :     exp_normd = norm_s( fLenLow_fx );
    3069             : 
    3070          31 :     ni_ratio_fx = div_s( shl( pcnt_fx, exp_normn ), shl( fLenLow_fx, exp_normd ) ); /* exp_normn - exp_normd + 15 - 2 */
    3071             : 
    3072          31 :     ni_ratio_fx = shl( ni_ratio_fx, add( sub( exp_normd, exp_normn ), 2 ) ); /* 15 - (exp_normn-exp_normd+15-2) */
    3073             : 
    3074             :     /*ni_ratio = min(0.9f, ni_ratio);*/
    3075          31 :     ni_ratio_fx = s_min( 29491, ni_ratio_fx ); /* 0.9: 29491(Q15) */
    3076             : 
    3077          31 :     move16();
    3078             : 
    3079          31 :     ss_min_fx = mult_r( ni_ratio_fx, 10240 ); /* Q15+Q10-15 = Q10 */ /* 10.0: 10240(Q10) */
    3080          31 :     cut_sig_th_fx = shr( ss_min_fx, 2 );                             /* 1/4 */
    3081          31 :     cut_sig_th_fx = s_max( 973, cut_sig_th_fx );                     /* 0.95: 972.8(Q10) */
    3082             : 
    3083             :     /*core coder normalization for gap filling*/
    3084        7967 :     FOR( i = 0; i < fLenLow_fx; i++ )
    3085             :     {
    3086        7936 :         L_spectra_rm[i] = L_deposit_l( 0 );
    3087        7936 :         if ( GE_32( L_abs( L_spectra[i] ), L_cut_input ) )
    3088             :         {
    3089         856 :             L_spectra_rm[i] = L_spectra[i];
    3090         856 :             move32();
    3091             :         }
    3092             :     }
    3093          31 :     SpectrumSmoothing_fx( L_spectra_rm, sspectra_fx, Qss, fLenLow_fx, cut_sig_th_fx );
    3094             : 
    3095             :     /*Extract noise informaton from the core coder*/
    3096          31 :     Copy( sspectra_fx, sspectra_ni_fx, fLenLow_fx );
    3097        7967 :     FOR( i = 0; i < fLenLow_fx; i++ )
    3098             :     {
    3099        7936 :         L_spectra_diff[i] = L_sub( L_spectra_ni[i], L_spectra[i] );
    3100        7936 :         move32();
    3101             :     }
    3102          31 :     cut_ni_th_fx = 0x0;
    3103          31 :     move16();
    3104             :     /*normalize sparse filled components*/
    3105        7967 :     FOR( i = 0; i < fLenLow_fx; i++ )
    3106             :     {
    3107        7936 :         L_spectra_rm[i] = L_deposit_l( 0 );
    3108        7936 :         move32();
    3109        7936 :         if ( GE_32( L_abs( L_spectra_diff[i] ), L_cut_input ) )
    3110             :         {
    3111        6362 :             L_spectra_rm[i] = L_spectra_diff[i];
    3112        6362 :             move32();
    3113             :         }
    3114             :     }
    3115          31 :     SpectrumSmoothing_fx( L_spectra_rm, sspectra_diff_fx, Qss, fLenLow_fx, cut_ni_th_fx );
    3116             : 
    3117             :     /*Normalized corecoder for Gap filling */
    3118             :     /* ratio = 1 - ss_min/10.0 */
    3119          31 :     ratio_fx = sub( 0x7fff, shl( mult_r( ss_min_fx, 3277 ), 15 - 10 ) ); /* Q15 */
    3120        7967 :     FOR( i = 0; i < fLenLow_fx; i++ )
    3121             :     {
    3122        7936 :         sign_fx = 0;
    3123        7936 :         move16();
    3124        7936 :         if ( sspectra_fx[i] < 0 )
    3125             :         {
    3126         707 :             sign_fx = 1;
    3127         707 :             move16();
    3128             :         }
    3129        7936 :         IF( GT_16( abs_s( sspectra_fx[i] ), ss_min_fx ) )
    3130             :         {
    3131             :             /*sspectra[i] = sign*((10-ss_min)/10.0f*(float)fabs(sspectra[i])+ss_min);*/
    3132         888 :             sspectra_fx[i] = add( mult_r( ratio_fx, abs_s( sspectra_fx[i] ) ), ss_min_fx );
    3133         888 :             move16();
    3134         888 :             IF( sign_fx != 0 )
    3135             :             {
    3136         432 :                 sspectra_fx[i] = negate( sspectra_fx[i] );
    3137         432 :                 move16();
    3138             :             }
    3139             :         }
    3140        7936 :         IF( sspectra_fx[i] != 0x0 )
    3141             :         {
    3142        1458 :             sspectra_ni_fx[i] = sspectra_fx[i];
    3143        1458 :             move16();
    3144             :         }
    3145             :         ELSE
    3146             :         {
    3147        6478 :             sspectra_ni_fx[i] = mult_r( sspectra_diff_fx[i], ni_ratio_fx );
    3148        6478 :             move16();
    3149             :         }
    3150             : 
    3151        7936 :         IF( sspectra_ni_fx[i] == 0x0 )
    3152             :         {
    3153             :             /*sspectra_ni[i] = 0.5f*10.0f*ni_ratio* own_random(ni_seed)/32768.0f;*/
    3154         698 :             L_temp = L_mult( ni_ratio_fx, 20480 );              /* ni_ratio*5 */
    3155         698 :             L_temp = Mult_32_16( L_temp, rand_a_fx[i] );        /* Q28 */
    3156         698 :             sspectra_ni_fx[i] = round_fx( L_shr( L_temp, 2 ) ); /* Qss */
    3157         698 :             move16();
    3158             :         }
    3159             :     }
    3160             : 
    3161          31 :     return ( ss_min_fx );
    3162             : }
    3163             : 
    3164             : /*--------------------------------------------------------------------------*
    3165             :  * noiseinj_hf()
    3166             :  * level adjustments for the missing bands in the core coder
    3167             :  *--------------------------------------------------------------------------*/
    3168          31 : void noiseinj_hf_fx(
    3169             :     Word32 L_xSynth_har[],        /* i/o : Qs   gap filled information            */
    3170             :     Word16 Qs,                    /* i   : Q0   Q value for xSynth_har            */
    3171             :     Word32 L_th_g[],              /* i   : Qs   level adjustment information      */
    3172             :     Word32 L_band_energy[],       /* i   : Qbe  subband energies                  */
    3173             :     Word16 Qbe,                   /* i   : Q0   Q value for band_energy           */
    3174             :     Word16 *prev_En_sb_fx,        /* i/o : QsEn smoothed sqrt band Energies       */
    3175             :     const Word16 p2a_flags_fx[],  /* i   : Q0   Missing bands in the core coder   */
    3176             :     const Word16 BANDS_fx,        /* i   : Q0   total bands                       */
    3177             :     const Word16 band_start_fx[], /* i   : Q0   band start indices                */
    3178             :     const Word16 band_end_fx[],   /* i   : Q0   band end indices                  */
    3179             :     const Word16 fLenLow_fx,      /* i   : Q0   low frequency bandwidth           */
    3180             :     const Word16 fLenHigh_fx      /* i   : Q0   SWB frequency bandwidth           */
    3181             : )
    3182             : {
    3183             :     Word16 k, i;
    3184             : 
    3185             :     Word16 *p_prev_En_sb_fx;
    3186          31 :     Word16 QbeL = 7; /* Don't need 3, because this E only use under th samples */ /* QsEn=3 */
    3187          31 :     move16();
    3188             :     Word16 map_pulse_t_fx[L_FRAME32k];
    3189             :     Word16 map_pulse_fx[L_FRAME32k];
    3190             : 
    3191          31 :     Word16 QsEn = 4; /* kiken */
    3192          31 :     move16();
    3193             :     Word32 L_En[NB_SWB_SUBBANDS];
    3194             :     Word32 *p_L_En;
    3195             :     Word16 QE;
    3196             :     Word16 sqrt_En_fx[NB_SWB_SUBBANDS]; /* QsEn */
    3197             :     Word16 *p_sqrt_En_fx;               /* QsEn */
    3198             : 
    3199             :     Word16 Enn_sm_sb_fx[NB_SWB_SUBBANDS];
    3200             :     Word16 *p_Enn_sm_sb_fx;
    3201             : 
    3202             :     Word16 exp_safe;
    3203             :     Word16 xSynth_har_fx[L_FRAME32k];
    3204             :     Word16 QxSynth;
    3205             : 
    3206             :     Word16 Qtemp;
    3207             : 
    3208             :     Word16 ni_scale_fx; /* Q14 */
    3209             : 
    3210             :     Word16 temp_fx;
    3211             :     Word32 L_temp;
    3212             : 
    3213             :     Word16 exp_normn, exp_normd;
    3214             :     Word16 div_fx;
    3215             :     Word16 Qdiv;
    3216             : #ifndef ISSUE_1796_replace_shl_o
    3217             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    3218             :     Flag Overflow = 0;
    3219             :     move32();
    3220             : #endif
    3221             : #endif
    3222          31 :     set16_fx( map_pulse_t_fx, 0, band_end_fx[BANDS_fx - 1] + 1 );
    3223          31 :     set16_fx( map_pulse_fx, 0, band_end_fx[BANDS_fx - 1] + 1 );
    3224             : 
    3225             :     /*level adjust the missing bands in the core coder */
    3226          31 :     exp_safe = 4; /*move16();*/
    3227          31 :     move16();
    3228          31 :     norm_vec_32_16_scale_fx( L_xSynth_har, Qs, fLenHigh_fx, xSynth_har_fx, &QxSynth, exp_safe );
    3229          31 :     QE = add( shl( QxSynth, 1 ), 1 );
    3230             : 
    3231          31 :     p_L_En = L_En;
    3232          31 :     move32();
    3233          31 :     p_sqrt_En_fx = sqrt_En_fx;
    3234         155 :     FOR( k = sub( BANDS_fx, NB_SWB_SUBBANDS ); k < BANDS_fx; k++ )
    3235             :     {
    3236         124 :         *p_L_En = L_deposit_l( 0 );
    3237         124 :         move32();
    3238         124 :         IF( p2a_flags_fx[k] == 0 )
    3239             :         {
    3240        9605 :             FOR( i = band_start_fx[k]; i <= band_end_fx[k]; i++ ){
    3241        9483 :                 IF( LE_32( L_abs( L_xSynth_har[i - fLenLow_fx] ), L_th_g[k - ( BANDS_fx - NB_SWB_SUBBANDS )] ) ){
    3242        8198 :                         *p_L_En = L_mac( *p_L_En, xSynth_har_fx[i - fLenLow_fx], xSynth_har_fx[i - fLenLow_fx] );
    3243        8198 :             move32();
    3244             :         }
    3245             :         ELSE
    3246             :         {
    3247        1285 :             map_pulse_t_fx[i] = 1;
    3248        1285 :             move16();
    3249             :         }
    3250             :     }
    3251         122 :     *p_L_En = L_shl_sat( *p_L_En, sub( QbeL, QE ) );
    3252         122 :     move32();
    3253             :     /**p_L_En = (float)sqrt(*p_En);*/
    3254         122 :     sqrt_32n_16_fx( *p_L_En, QbeL, p_sqrt_En_fx, &Qtemp );
    3255             : #ifdef ISSUE_1796_replace_shl_o
    3256         122 :     *p_sqrt_En_fx = shl_sat( *p_sqrt_En_fx, sub( QsEn, Qtemp ) ); /* -> Q2 */
    3257             : #else
    3258             :     *p_sqrt_En_fx = shl_o( *p_sqrt_En_fx, sub( QsEn, Qtemp ), &Overflow ); /* -> Q2 */
    3259             : #endif
    3260         122 :     move16();
    3261             : }
    3262         124 : p_L_En++;
    3263         124 : p_sqrt_En_fx++;
    3264             : }
    3265             : 
    3266          31 : p_sqrt_En_fx = sqrt_En_fx;
    3267          31 : p_Enn_sm_sb_fx = Enn_sm_sb_fx;
    3268          31 : p_prev_En_sb_fx = prev_En_sb_fx;
    3269         155 : FOR( k = BANDS_fx - NB_SWB_SUBBANDS; k < BANDS_fx; k++ )
    3270             : {
    3271         124 :     *p_Enn_sm_sb_fx = prev_En_sb_fx[k - ( BANDS_fx - NB_SWB_SUBBANDS )];
    3272         124 :     move16(); /* QsEn */
    3273         124 :     IF( p2a_flags_fx[k] == 0 )
    3274             :     {
    3275         122 :         L_temp = Mult_32_16( L_band_energy[k], 26214 ); /* 0.8: 26214(Q15) */
    3276         122 :         temp_fx = round_fx( L_shl( L_temp, sub( QsEn, sub( Qbe, 16 ) ) ) );
    3277         122 :         IF( LT_16( *p_prev_En_sb_fx, temp_fx ) )
    3278             :         {
    3279             :             /**p_Enn_sm_sb = (0.15f*(*p_En)) + (0.85f*prev_En_sb[k-(BANDS-NB_SWB_SUBBANDS)]);*/
    3280             :             /* 0.15: 4915.2(Q15) 0.85: 27852.80(Q15) */
    3281          15 :             *p_Enn_sm_sb_fx = round_fx( L_mac( L_mult( *p_sqrt_En_fx, 4915 ), *p_prev_En_sb_fx, 27853 ) );
    3282             :         }
    3283             :         ELSE
    3284             :         {
    3285             :             /**p_Enn_sm_sb = (0.8f*(*p_En)) + (0.2f*prev_En_sb[k-(BANDS-NB_SWB_SUBBANDS)]);*/
    3286             :             /* 0.8: 26214.4(Q15) 0.2:6553.6(Q15) */
    3287         107 :             *p_Enn_sm_sb_fx = round_fx( L_mac( L_mult( *p_sqrt_En_fx, 26214 ), *p_prev_En_sb_fx, 6554 ) );
    3288             :         }
    3289         122 :         move16();
    3290             :     }
    3291             : 
    3292         124 :     p_Enn_sm_sb_fx++;
    3293         124 :     p_sqrt_En_fx++;
    3294         124 :     p_prev_En_sb_fx++;
    3295             : }
    3296             : 
    3297          31 : p_sqrt_En_fx = sqrt_En_fx;
    3298          31 : p_Enn_sm_sb_fx = Enn_sm_sb_fx;
    3299          31 : p_prev_En_sb_fx = prev_En_sb_fx;
    3300          31 : map_pulse_fx[fLenLow_fx] = ( map_pulse_t_fx[fLenLow_fx] | map_pulse_t_fx[fLenLow_fx + 1] );
    3301          31 : logic16();
    3302          31 : move16();
    3303        9641 : FOR( i = fLenLow_fx + 1; i < band_end_fx[BANDS_fx - 1]; i++ )
    3304             : {
    3305        9610 :     map_pulse_fx[i] = ( map_pulse_t_fx[i - 1] | map_pulse_t_fx[i] | map_pulse_t_fx[i + 1] );
    3306        9610 :     logic16();
    3307        9610 :     logic16();
    3308        9610 :     move16();
    3309             : }
    3310          31 : map_pulse_fx[i] = ( map_pulse_t_fx[i - 1] | map_pulse_t_fx[i] );
    3311          31 : logic16();
    3312          31 : move16();
    3313             : 
    3314         155 : FOR( k = BANDS_fx - NB_SWB_SUBBANDS; k < BANDS_fx; k++ )
    3315             : {
    3316         124 :     test();
    3317         124 :     IF( p2a_flags_fx[k] == 0 && *p_sqrt_En_fx != 0x0 )
    3318             :     {
    3319             :         /*ni_scale = sqrt((*p_Enn_sm_sb)/(*p_En));*/
    3320             :         /* Div Part */
    3321         122 :         exp_normn = norm_s( *p_Enn_sm_sb_fx );
    3322         122 :         exp_normn = sub( exp_normn, 1 );
    3323         122 :         exp_normd = norm_s( *p_sqrt_En_fx );
    3324         122 :         div_fx = div_s( shl( *p_Enn_sm_sb_fx, exp_normn ), shl( *p_sqrt_En_fx, exp_normd ) );
    3325         122 :         Qdiv = add( sub( exp_normn, exp_normd ), 15 );
    3326             : 
    3327             :         /* SQRT Part */
    3328         122 :         sqrt_32n_16_fx( L_deposit_h( div_fx ), add( Qdiv, 16 ), &ni_scale_fx, &Qtemp );
    3329             : #ifdef ISSUE_1796_replace_shl_o
    3330         122 :         ni_scale_fx = shl_sat( ni_scale_fx, sub( 14, Qtemp ) );
    3331             : #else
    3332             :         ni_scale_fx = shl_o( ni_scale_fx, sub( 14, Qtemp ), &Overflow );
    3333             : #endif
    3334         122 :         ni_scale_fx = s_min( 20408, ni_scale_fx ); /* 1.25=20408.0(Q14) */
    3335             : 
    3336         122 :         ni_scale_fx = s_max( 12288, ni_scale_fx ); /* 0.75=12288.0(Q14) */
    3337             : 
    3338         122 :         ni_scale_fx = mult_r( ni_scale_fx, 26214 ); /* 0.8=26214.4(Q15)   -> Q14 */
    3339        9605 :         FOR( i = band_start_fx[k]; i <= band_end_fx[k]; i++ ){
    3340        9483 :             IF( LE_32( L_abs( L_xSynth_har[i - fLenLow_fx] ), L_th_g[k - ( BANDS_fx - NB_SWB_SUBBANDS )] ) ){
    3341        8198 :                 IF( map_pulse_fx[i] == 0 ){
    3342        6379 :                     L_xSynth_har[i - fLenLow_fx] = L_shl( Mult_32_16( L_xSynth_har[i - fLenLow_fx], ni_scale_fx ), 1 ); /* Q12+Q14-15-1 = Q12 */
    3343        6379 :         move32();
    3344             :     }
    3345             : }
    3346             : }
    3347         122 : *p_prev_En_sb_fx = *p_Enn_sm_sb_fx;
    3348         122 : move16();
    3349             : }
    3350         124 : p_Enn_sm_sb_fx++;
    3351         124 : p_sqrt_En_fx++;
    3352         124 : p_prev_En_sb_fx++;
    3353             : }
    3354             : 
    3355          31 : return;
    3356             : }
    3357             : 
    3358             : /*--------------------------------------------------------------------------*
    3359             :  * updat_prev_frm()
    3360             :  *
    3361             :  *
    3362             :  *--------------------------------------------------------------------------*/
    3363          34 : void updat_prev_frm_fx(
    3364             :     Word32 L_y2[],                     /* i/o: core coder buffer                 */
    3365             :     Word32 L_t_audio[],                /*   o: core coder buffer                 */
    3366             :     Word32 L_bwe_br,                   /*   i: core bitrate                      */
    3367             :     Word16 length_fx,                  /*   i: frame length coded bw             */
    3368             :     const Word16 inner_frame_fx,       /*   i: input frame length                */
    3369             :     Word16 bands_fx,                   /*   i: sub band resolution               */
    3370             :     Word16 bwidth_fx,                  /*   i: NB/WB/SWB indicator               */
    3371             :     const Word16 is_transient_fx,      /*   i: signal class information          */
    3372             :     Word16 hqswb_clas_fx,              /*   i: signal class information          */
    3373             :     Word16 *prev_hqswb_clas,           /*   o: update signal class information   */
    3374             :     Word16 prev_SWB_peak_pos_fx[],     /*   o: update core coder last coded peaks*/
    3375             :     Word16 prev_SWB_peak_pos_tmp_fx[], /*   o: update core coder last coded peaks*/
    3376             :     Word16 *prev_frm_hfe2,             /*   o: update harmonics                  */
    3377             :     Word16 *prev_stab_hfe2,            /*   o: update harmonics                  */
    3378             :     Word16 bws_cnt_fx                  /*   i: band width detector               */
    3379             : )
    3380             : {
    3381             :     Word16 i, j, k;
    3382             :     Word16 k1_fx, k2_fx;
    3383             :     Word16 length1_fx, length2_fx, length3_fx;
    3384             : 
    3385             :     /* Copy the coded MDCT coefficient to the output buffer */
    3386          34 :     IF( !is_transient_fx )
    3387             :     {
    3388             :         /* Copy the scratch buffer to the output */
    3389          31 :         Copy32( L_y2, L_t_audio, length_fx );
    3390             : 
    3391             :         /* If the input frame is larger than coded bandwidth, zero out uncoded MDCT coefficients */
    3392          31 :         IF( GT_16( inner_frame_fx, length_fx ) )
    3393             :         {
    3394          31 :             set32_fx( L_t_audio + length_fx, 0x0L, sub( inner_frame_fx, length_fx ) );
    3395             :         }
    3396             :     }
    3397             :     ELSE /* transient frame */
    3398             :     {
    3399           3 :         test();
    3400           3 :         IF( EQ_16( inner_frame_fx, length_fx ) || bws_cnt_fx > 0 )
    3401             :         {
    3402             :             /* Copy the scratch buffer to the output */
    3403           0 :             Copy32( L_y2, L_t_audio, length_fx );
    3404             :         }
    3405             :         ELSE
    3406             :         {
    3407             :             /* length/NUM_TIME_SWITCHING_BLOCKS */
    3408             :             /*length1_fx = div_s_ss(length_fx, NUM_TIME_SWITCHING_BLOCKS); */
    3409           3 :             length1_fx = shr( length_fx, 2 ); /* length1 = length/NUM_TIME_SWITCHING_BLOCKS */
    3410             :             /* inner_frame/NUM_TIME_SWITCHING_BLOCKS */
    3411             :             /*length2_fx = div_s_ss(inner_frame_fx, NUM_TIME_SWITCHING_BLOCKS); */
    3412           3 :             length2_fx = shr( inner_frame_fx, 2 ); /* length2 = inner_frame/NUM_TIME_SWITCHING_BLOCKS */
    3413             :             /* (inner_frame-length)/NUM_TIME_SWITCHING_BLOCKS */
    3414             :             /*length3_fx = div_s_ss(sub(inner_frame_fx, length_fx), NUM_TIME_SWITCHING_BLOCKS); */
    3415           3 :             length3_fx = shr( sub( inner_frame_fx, length_fx ), 2 ); /* (inner_frame-length)/NUM_TIME_SWITCHING_BLOCKS */
    3416             : 
    3417           3 :             k1_fx = 0;
    3418           3 :             move16();
    3419           3 :             k2_fx = 0;
    3420           3 :             move16();
    3421             : 
    3422             :             /* un-collapse transient frame and interleave zeros */
    3423          15 :             FOR( i = 0; i < NUM_TIME_SWITCHING_BLOCKS; i++ )
    3424             :             {
    3425             :                 /*k1 = i*length/NUM_TIME_SWITCHING_BLOCKS; */
    3426             :                 /*k2 = i*inner_frame/NUM_TIME_SWITCHING_BLOCKS; */
    3427             : 
    3428          12 :                 Copy32( L_y2 + k1_fx, L_t_audio + k2_fx, length1_fx );
    3429          12 :                 set32_fx( L_t_audio + k2_fx + length1_fx, 0x0L, length3_fx );
    3430             : 
    3431          12 :                 k1_fx = add( k1_fx, length1_fx );
    3432          12 :                 k2_fx = add( k2_fx, length2_fx );
    3433             :             }
    3434             :         }
    3435             :     }
    3436             : 
    3437          34 :     test();
    3438          34 :     test();
    3439          34 :     IF( ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) && EQ_16( bwidth_fx, SWB ) )
    3440             :     {
    3441          34 :         *prev_hqswb_clas = hqswb_clas_fx;
    3442          34 :         move16();
    3443          34 :         IF( NE_16( hqswb_clas_fx, HQ_HARMONIC ) )
    3444             :         {
    3445          34 :             *prev_frm_hfe2 = 0;
    3446          34 :             move16();
    3447          34 :             *prev_stab_hfe2 = 0;
    3448          34 :             move16();
    3449             :         }
    3450             :     }
    3451             :     ELSE
    3452             :     {
    3453           0 :         *prev_hqswb_clas = is_transient_fx;
    3454           0 :         move16();
    3455             :     }
    3456             : 
    3457          34 :     test();
    3458          34 :     test();
    3459          34 :     test();
    3460          34 :     IF( ( EQ_32( L_bwe_br, HQ_16k40 ) || EQ_32( L_bwe_br, HQ_13k20 ) ) && EQ_16( bwidth_fx, SWB ) && EQ_16( hqswb_clas_fx, HQ_NORMAL ) )
    3461             :     {
    3462          31 :         j = 0;
    3463          31 :         move16();
    3464         155 :         FOR( k = sub( bands_fx, SPT_SHORTEN_SBNUM ); k < bands_fx; k++ )
    3465             :         {
    3466         124 :             prev_SWB_peak_pos_fx[j] = prev_SWB_peak_pos_tmp_fx[j];
    3467         124 :             move16();
    3468         124 :             j = add( j, 1 );
    3469             :         }
    3470             :     }
    3471             : 
    3472          34 :     return;
    3473             : }

Generated by: LCOV version 1.14