LCOV - code coverage report
Current view: top level - lib_enc - hq_lr_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 686 751 91.3 %
Date: 2025-05-28 04:28:20 Functions: 8 8 100.0 %

          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             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "rom_enc.h"
      45             : #include "rom_com.h"
      46             : #include "prot.h"
      47             : #include "stl.h"
      48             : #include "basop_util.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : 
      52             : /*--------------------------------------------------------------------------*
      53             :  * Local function prototypes
      54             :  *--------------------------------------------------------------------------*/
      55             : 
      56             : static int16_t small_symbol_enc( BSTR_ENC_HANDLE hBstr, const int16_t *qbidx, const int16_t bands, int16_t *hLCmode, const int16_t flag_pack, const int16_t is_transient );
      57             : 
      58             : static int16_t small_symbol_enc_tran( BSTR_ENC_HANDLE hBstr, const int16_t *qbidx, const int16_t bands, int16_t *hLCmode, const int16_t flag_pack, const int16_t is_transient );
      59             : 
      60             : static float band_energy_quant( BSTR_ENC_HANDLE hBstr, const float *t_audio, const int16_t band_start[], const int16_t band_end[], float band_energy[], const int16_t bands, const Word32 L_qint, const Word16 eref_fx, const int16_t is_transient );
      61             : 
      62             : static int16_t p2a_threshold_quant( BSTR_ENC_HANDLE hBstr, const float *t_audio, const int16_t band_start[], const int16_t band_end[], const int16_t band_width[], const int16_t bands, const int16_t p2a_bands, const float p2a_th, int16_t *p2a_flags );
      63             : 
      64             : static void mdct_spectrum_fine_gain_enc( BSTR_ENC_HANDLE hBstr, const float ybuf[], float y2[], const int16_t band_start[], const int16_t band_end[], const int16_t k_sort[], const int16_t bands, const Word32 L_qint, const int16_t Ngq, const int16_t gqlevs, const int16_t gqbits );
      65             : 
      66             : 
      67             : /*--------------------------------------------------------------------------*
      68             :  * spt_shorten_domain_set()
      69             :  *
      70             :  * Track the spectral peak based on peak -avg analysis
      71             :  *--------------------------------------------------------------------------*/
      72             : 
      73        1450 : static void spt_shorten_domain_set(
      74             :     BSTR_ENC_HANDLE hBstr,          /* i/o: encoder bitstream handle            */
      75             :     HQ_ENC_HANDLE hHQ_core,         /* i/o: HQ core encoder handle              */
      76             :     const float t_audio[],          /* i  : input spectrum                      */
      77             :     const int16_t p2a_flags[],      /* i  : p2a anlysis information             */
      78             :     const int16_t new_band_start[], /* i  : new band start position             */
      79             :     const int16_t new_band_end[],   /* i  : new band end position               */
      80             :     const int16_t new_band_width[], /* i  : new subband band width              */
      81             :     const int16_t bands,            /* i  : total number of subbands            */
      82             :     int16_t band_start[],           /* i/o: band start position                 */
      83             :     int16_t band_end[],             /* i/o: band end position                   */
      84             :     int16_t band_width[],           /* i  : sub band band width                 */
      85             :     int16_t *bit_budget             /* i/o: bit budget                          */
      86             : )
      87             : {
      88             :     int16_t i, j, k;
      89             :     int16_t kpos;
      90             :     float max_y2;
      91             :     int16_t max_y2_pos;
      92             :     int16_t spt_shorten_flag[SPT_SHORTEN_SBNUM];
      93             : 
      94        1450 :     kpos = 0;
      95        1450 :     j = 0;
      96        7250 :     for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
      97             :     {
      98        5800 :         if ( p2a_flags[k] == 1 )
      99             :         {
     100         204 :             spt_shorten_flag[j] = 0;
     101         204 :             if ( hHQ_core->prev_SWB_peak_pos[kpos] != 0 )
     102             :             {
     103          14 :                 max_y2 = 0.0f;
     104          14 :                 max_y2_pos = 0;
     105         986 :                 for ( i = band_start[k]; i <= band_end[k]; i++ )
     106             :                 {
     107         972 :                     if ( max_y2 < fabs( t_audio[i] ) )
     108             :                     {
     109          96 :                         max_y2 = (float) fabs( t_audio[i] );
     110          96 :                         max_y2_pos = i;
     111             :                     }
     112             :                 }
     113          14 :                 if ( max_y2_pos >= new_band_start[j] && max_y2_pos <= new_band_end[j] )
     114             :                 {
     115          13 :                     band_start[k] = new_band_start[j];
     116          13 :                     band_end[k] = new_band_end[j];
     117          13 :                     band_width[k] = new_band_width[j];
     118          13 :                     spt_shorten_flag[j] = 1;
     119             :                 }
     120             :             }
     121         204 :             push_indice( hBstr, IND_HQ2_SPT_SHORTEN, spt_shorten_flag[j], 1 );
     122         204 :             *bit_budget -= 1;
     123             :         }
     124             : 
     125        5800 :         kpos++;
     126        5800 :         j++;
     127             :     }
     128             : 
     129        1450 :     return;
     130             : }
     131             : 
     132             : /*--------------------------------------------------------------------------*
     133             :  * hq_lr_enc()
     134             :  *
     135             :  * HQ low rate encoding routine
     136             :  *--------------------------------------------------------------------------*/
     137             : 
     138        1976 : void hq_lr_enc(
     139             :     Encoder_State *st,         /* i/o: encoder state structure             */
     140             :     float t_audio[],           /* i/o: transform-domain coefs.             */
     141             :     const int16_t inner_frame, /* i  : inner frame length                  */
     142             :     int16_t *num_bits,         /* i/o: number of available bits            */
     143             :     const int16_t is_transient /* i  : transient flag                      */
     144             : )
     145             : {
     146             :     int16_t i, k1, k2;
     147             :     int16_t bit_budget, pbits;
     148             :     int16_t bands, length, ni_seed, gqlevs, gqbits, Ngq, p2a_bands;
     149             :     int16_t p2a_flags[BANDS_MAX];
     150             :     int16_t band_start[BANDS_MAX], band_end[BANDS_MAX], band_width[BANDS_MAX];
     151             :     float band_energy[BANDS_MAX], Rk[BANDS_MAX];
     152             :     Word32 Rk_fx[BANDS_MAX];
     153             :     float ebits;
     154             :     float p2a_th, ni_coef, ni_pd_th, pd_thresh, ld_slope;
     155             :     Word32 L_qint;              /* Q29 */
     156             :     Word16 eref_fx;             /* Q10 */
     157             :     Word16 bit_alloc_weight_fx; /* Q13 */
     158             :     int16_t k_sort[BANDS_MAX];
     159             :     int16_t npulses[BANDS_MAX];
     160             :     int32_t inp_vector[L_FRAME48k];
     161             :     float y2[L_FRAME48k];
     162             :     float y2_ni[L_FRAME48k];
     163             :     int16_t hqswb_clas;
     164             :     int16_t lowlength;
     165             :     int16_t highlength;
     166             :     float m[L_FRAME32k];
     167             :     int16_t har_bands;
     168        1976 :     float Ep[BANDS_MAX], enerH = 0.0f, enerL = 0.0f;
     169        1976 :     int16_t lowband, highband, bw_low = 0, bw_high = 20;
     170             :     float band_energy_tmp[BANDS_MAX];
     171             :     int32_t bwe_br;
     172             :     int16_t trans_bit, p2a_flags_tmp[BANDS_MAX];
     173        1976 :     int16_t adjustFlag = 0;
     174             :     int16_t prev_SWB_peak_pos_tmp[SPT_SHORTEN_SBNUM];
     175             :     int16_t k, j;
     176             :     int16_t flag_spt;
     177             :     int16_t org_band_start[SPT_SHORTEN_SBNUM];
     178             :     int16_t org_band_end[SPT_SHORTEN_SBNUM];
     179             :     int16_t org_band_width[SPT_SHORTEN_SBNUM];
     180             :     int16_t new_band_start[SPT_SHORTEN_SBNUM];
     181             :     int16_t new_band_end[SPT_SHORTEN_SBNUM];
     182             :     int16_t new_band_width[SPT_SHORTEN_SBNUM];
     183        1976 :     int16_t bws_cnt = 0;
     184             :     Word32 L_tmp, L_tmp2, L_tmp3;
     185             :     Word16 exp, tmp, exp2, tmp1, tmp2, tmp3, alpha_fx, frac1;
     186             :     Word32 enerH_fx;
     187             :     Word32 enerL_fx;
     188             :     Word32 Ep_fx[BANDS_MAX];
     189             :     Word32 Ep_avrg_fx, Ep_vari_fx;
     190             :     Word32 Ep_avrgL_fx;
     191             :     Word32 Ep_peak_fx;
     192             :     Word32 Ep_tmp_fx[BANDS_MAX];
     193             :     Word16 gama_fx; /*Q15 0.85f; */
     194             :     Word16 beta_fx; /*Q14 1.05f; */
     195             :     Word32 L_band_energy[BANDS_MAX], L_band_energy_tmp[BANDS_MAX];
     196             :     UWord16 lo;
     197             :     Word16 Q_band_energy;
     198             : #ifdef BASOP_NOGLOB
     199             :     Flag Overflow;
     200             : #endif /* BASOP_NOGLOB */
     201             : 
     202        1976 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     203        1976 :     HQ_ENC_HANDLE hHQ_core = st->hHQ_core;
     204             : 
     205        1976 :     set_f( y2, 0.0f, L_FRAME48k );
     206        1976 :     set_l( inp_vector, 0, inner_frame );
     207        1976 :     flag_spt = 0;
     208        1976 :     set_s( prev_SWB_peak_pos_tmp, 0, SPT_SHORTEN_SBNUM );
     209             : 
     210        1976 :     bwe_br = st->core_brate;
     211        1976 :     hqswb_clas = HQ_NORMAL;
     212        1976 :     if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     213             :     {
     214        1556 :         if ( is_transient == 1 )
     215             :         {
     216          58 :             hqswb_clas = HQ_TRANSIENT;
     217             :         }
     218             :         else
     219             :         {
     220             :             /* classification of HQ_HARMONIC and HQ_NORMAL frames for SWB BWE */
     221        1498 :             hqswb_clas = peak_avrg_ratio( st->total_brate, t_audio, NUMC_N, &hHQ_core->mode_count, &hHQ_core->mode_count1 );
     222             :         }
     223             : 
     224             :         /* write the classification information into the bitstream */
     225        1556 :         push_indice( hBstr, IND_HQ2_SWB_CLAS, hqswb_clas, 2 );
     226        1556 :         ( *num_bits ) -= 2;
     227             : 
     228        1556 :         if ( hqswb_clas == HQ_NORMAL )
     229             :         {
     230        1450 :             flag_spt = 1;
     231             :         }
     232             :     }
     233             :     else
     234             :     {
     235             :         /* write the transient bit into the bitstream */
     236         420 :         push_indice( hBstr, IND_HQ2_SWB_CLAS, is_transient, 1 );
     237             : 
     238             :         /* subtract one bit for the transient flag */
     239         420 :         ( *num_bits )--;
     240             :     }
     241             : 
     242             :     /* Configure encoder for different bandwidths, bitrates, etc. */
     243        1976 :     hq2_core_configure( inner_frame, *num_bits, is_transient, &bands, &length, band_width, band_start, band_end, &L_qint, &eref_fx, &bit_alloc_weight_fx, &gqlevs, &Ngq, &p2a_bands, &p2a_th, &pd_thresh, &ld_slope, &ni_coef, &ni_pd_th, bwe_br );
     244             : 
     245        1976 :     highlength = band_end[bands - 1];
     246        1976 :     har_bands = bands;
     247             : 
     248        1976 :     if ( st->bwidth == SWB && is_transient == 0 && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     249             :     {
     250             :         /* reserve bits for HQ_NORMAL and HQ_HARMONIC modes */
     251        1498 :         if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
     252             :         {
     253        1498 :             ( *num_bits ) -= get_usebit_npswb( hqswb_clas );
     254             :         }
     255             :     }
     256             : 
     257        1976 :     if ( ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
     258             :     {
     259        1556 :         if ( hHQ_core->prev_hqswb_clas != HQ_NORMAL )
     260             :         {
     261         105 :             j = 0;
     262         525 :             for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
     263             :             {
     264         420 :                 hHQ_core->prev_SWB_peak_pos[j] = 0;
     265         420 :                 j++;
     266             :             }
     267             :         }
     268             :     }
     269             : 
     270             :     /* Check if input frame is larger than coded bandwidth */
     271        1976 :     if ( inner_frame > length && is_transient )
     272             :     {
     273             :         /* If so, collapse transient frame (4 short transforms) to remove uncoded coefficients */
     274         232 :         for ( i = 1; i < NUM_TIME_SWITCHING_BLOCKS; i++ )
     275             :         {
     276         174 :             k1 = i * length / NUM_TIME_SWITCHING_BLOCKS;
     277         174 :             k2 = i * inner_frame / NUM_TIME_SWITCHING_BLOCKS;
     278             : 
     279         174 :             mvr2r( &t_audio[k2], &t_audio[k1], length / NUM_TIME_SWITCHING_BLOCKS );
     280             :         }
     281             :     }
     282             : 
     283             :     /* Spectral energy calculation/quantization */
     284        1976 :     ebits = band_energy_quant( hBstr, t_audio, band_start, band_end, band_energy, bands, L_qint, eref_fx, is_transient );
     285             : 
     286       43143 :     for ( i = 0; i < bands; i++ )
     287             :     {
     288       41167 :         L_band_energy[i] = (Word32) ( band_energy[i] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
     289             :     }
     290             : 
     291             :     /* First pass bit budget for TCQ of spectral band information */
     292        1976 :     gqbits = (int16_t) log2_f( (float) gqlevs );
     293        1976 :     bit_budget = ( *num_bits ) - (int16_t) ceil( ebits ) - Ngq * gqbits;
     294             : 
     295        1976 :     pbits = 0;
     296        1976 :     if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     297             :     {
     298        1556 :         if ( hqswb_clas == HQ_HARMONIC )
     299             :         {
     300          48 :             set_s( p2a_flags, 1, har_bands );
     301             :         }
     302             :         else
     303             :         {
     304             :             /* High band tonality detector based on per band peak-to-average ratio */
     305        1508 :             pbits = p2a_threshold_quant( hBstr, t_audio, band_start, band_end, band_width, bands, p2a_bands, p2a_th, p2a_flags );
     306        1508 :             bit_budget -= pbits;
     307             : 
     308        1508 :             if ( hqswb_clas == HQ_NORMAL )
     309             :             {
     310        1450 :                 return_bits_normal2( &bit_budget, p2a_flags, bands, bits_lagIndices_modeNormal );
     311             :             }
     312             :         }
     313             :     }
     314             :     else
     315             :     {
     316             :         /* High band tonality detector based on per band peak-to-average ratio */
     317         420 :         pbits = p2a_threshold_quant( hBstr, t_audio, band_start, band_end, band_width, bands, p2a_bands, p2a_th, p2a_flags );
     318         420 :         bit_budget -= pbits;
     319             :     }
     320             : 
     321        1976 :     if ( flag_spt == 1 )
     322             :     {
     323             :         /* initialize the desired parameters for SPT */
     324        1450 :         spt_shorten_domain_band_save( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
     325        1450 :         spt_shorten_domain_pre( band_start, band_end, hHQ_core->prev_SWB_peak_pos, bands, bwe_br, new_band_start, new_band_end, new_band_width );
     326        1450 :         spt_shorten_domain_set( hBstr, hHQ_core, t_audio, p2a_flags, new_band_start, new_band_end, new_band_width, bands, band_start, band_end, band_width, &bit_budget );
     327             :     }
     328             : 
     329             : #define WMC_TOOL_SKIP
     330             :     /* Estimate number of bits per band */
     331        1976 :     Q_band_energy = SWB_BWE_LR_Qbe;
     332       43143 :     FOR( i = 0; i < bands; i++ )
     333             :     {
     334       41167 :         L_tmp = L_shl( L_band_energy[i], sub( 16, Q_band_energy ) ); /*Q16 */
     335             : 
     336       41167 :         frac1 = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
     337       41167 :         L_tmp = Pow2( 30, frac1 );
     338       41167 :         exp = sub( exp, 30 );
     339       41167 :         Ep_fx[i] = L_shl( L_tmp, sub( exp, 6 ) ); /* Q -6 */
     340       41167 :         Ep[i] = (float) ( Ep_fx[i] / pow( 2.0, -6 ) );
     341             :     }
     342             : 
     343       43143 :     FOR( i = 0; i < bands; i++ )
     344             :     {
     345       41167 :         L_tmp2 = Ep_fx[i];
     346       41167 :         L_tmp = L_max( 1, L_tmp2 );
     347       41167 :         exp = norm_l( L_tmp );
     348       41167 :         tmp = extract_h( L_shl( L_tmp, exp ) );
     349             : 
     350       41167 :         L_tmp3 = (Word32) band_width[i];
     351       41167 :         exp2 = norm_l( L_tmp3 );
     352       41167 :         tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
     353             : 
     354       41167 :         exp2 = sub( exp, exp2 ); /* Denormalize and substract */
     355             : 
     356       41167 :         tmp3 = sub( tmp2, tmp );
     357       41167 :         IF( tmp3 > 0 )
     358             :         {
     359       17821 :             tmp2 = shr( tmp2, 1 );
     360             :         }
     361       41167 :         IF( tmp3 > 0 )
     362             :         {
     363       17821 :             exp2 = add( exp2, 1 );
     364             :         }
     365       41167 :         tmp = div_s( tmp2, tmp );
     366       41167 :         L_tmp = L_deposit_h( tmp );
     367       41167 :         L_tmp = Isqrt_lc1( L_tmp, &exp2 );
     368       41167 :         move32();                                       /*Q(31-exp2) */
     369       41167 :         Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
     370             :     }
     371             : #undef WMC_TOOL_SKIP
     372             : 
     373        1976 :     if ( is_transient == 0 && inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 )
     374             :     {
     375             : #define WMC_TOOL_SKIP
     376         333 :         lowband = 6;
     377         333 :         move16();
     378         333 :         trans_bit = 2;
     379         333 :         move16();
     380         333 :         bit_budget = sub( bit_budget, trans_bit );
     381         333 :         gama_fx = 27852; /*Q15 0.85f; */
     382         333 :         beta_fx = 17203;
     383         333 :         move16(); /*Q14 1.05f; */
     384         333 :         set_s( &p2a_flags_tmp[bands - trans_bit], 0, 2 );
     385             : 
     386         333 :         IF( st->core_brate == ACELP_13k20 )
     387             :         {
     388          69 :             beta_fx = 13107;
     389          69 :             move16(); /*14 1.25f; */
     390          69 :             gama_fx = 31130;
     391          69 :             move16(); /*0.95f; */
     392          69 :             mvs2s( &p2a_flags[sub( bands, trans_bit )], &p2a_flags_tmp[sub( bands, trans_bit )], trans_bit );
     393             :         }
     394             : 
     395             :         /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
     396         333 :         Ep_vari_fx = 0;
     397         333 :         move32();
     398         333 :         Ep_avrg_fx = 0;
     399         333 :         move32();
     400         333 :         Ep_avrgL_fx = 0;
     401         333 :         move32();
     402         333 :         Ep_peak_fx = 0;
     403         333 :         move32();
     404        4938 :         FOR( i = 0; i < bands; i++ )
     405             :         {
     406        4605 :             IF( sub( i, lowband ) >= 0 )
     407             :             {
     408        2607 :                 Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
     409        2607 :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] );                                           /*Q15 */
     410             :             }
     411             :             ELSE
     412             :             {
     413             : #ifdef BASOP_NOGLOB
     414        1998 :                 Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
     415             : #else
     416             :                 Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] );        /*Q15 */
     417             : #endif
     418        1998 :                 IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
     419             :                 {
     420         771 :                     Ep_peak_fx = Ep_tmp_fx[i];
     421         771 :                     move32(); /*Q15 */
     422             :                 }
     423             :             }
     424             :         }
     425             :         /* modify the last p2a_bands subbands band_energies */
     426         333 :         k = bands;
     427         333 :         mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
     428         333 :         Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
     429         333 :         Mpy_32_16_ss( Ep_peak_fx, shl( sub( bands, lowband ), 9 ), &L_tmp2, &lo );
     430         333 :         Mpy_32_16_ss( Ep_avrg_fx, 1126, &L_tmp3, &lo );
     431             : 
     432         333 :         IF( ( ( L_sub( L_tmp, L_shr( Ep_avrgL_fx, 1 ) ) < 0 && st->core_brate == ACELP_13k20 ) || st->core_brate < ACELP_13k20 ) &&
     433             :             L_sub( L_tmp2, L_tmp3 ) < 0 && L_sub( L_tmp2, L_shr( Ep_avrg_fx, 7 ) ) > 0 )
     434             :         {
     435         144 :             FOR( i = lowband; i < bands; i++ )
     436             :             {
     437         127 :                 Mpy_32_16_ss( Ep_avrg_fx, 24576, &L_tmp, &lo );
     438         127 :                 IF( L_sub( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) < 0 )
     439             :                 {
     440         127 :                     Mpy_32_16_ss( Ep_peak_fx, sub( bands, lowband ), &L_tmp, &lo );
     441         127 :                     tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
     442         127 :                     IF( tmp != 0 )
     443             :                     {
     444         127 :                         exp = norm_s( tmp );
     445         127 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     446         127 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     447         127 :                         exp = sub( 29, exp );
     448             :                     }
     449             :                     ELSE
     450             :                     {
     451             :                         /*when the divisor is zero, happens rarely*/
     452           0 :                         tmp = 0x7fff;
     453           0 :                         move16();
     454           0 :                         exp = 0;
     455           0 :                         move16();
     456             :                     }
     457         127 :                     Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
     458         127 :                     L_tmp = L_shl( L_tmp, sub( 13, exp ) );                       /*Q(13+exp-15 +13-exp +4 = 15) */
     459         127 :                     L_tmp2 = L_add( L_tmp, 13107 );                               /*15 */
     460         127 :                     tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
     461         127 :                     Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_band_energy_tmp[i], &lo );
     462             :                 }
     463             :             }
     464             :         }
     465             :         ELSE
     466             :         {
     467         316 :             j = 0;
     468         948 :             FOR( i = sub( bands, trans_bit ); i < bands; i++ )
     469             :             {
     470         632 :                 alpha_fx = 16384;
     471         632 :                 move16(); /*Q14 */
     472         632 :                 IF( sub( p2a_flags_tmp[i], 1 ) == 0 )
     473             :                 {
     474           0 :                     Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
     475           0 :                     tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
     476           0 :                     IF( tmp != 0 )
     477             :                     {
     478           0 :                         exp = norm_s( tmp );
     479           0 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     480           0 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     481           0 :                         exp = sub( 29, exp );
     482             :                     }
     483             :                     ELSE
     484             :                     {
     485             :                         /*when the divisor is zero, happens rarely*/
     486           0 :                         tmp = 0x7fff;
     487           0 :                         move16();
     488           0 :                         exp = 0;
     489           0 :                         move16();
     490             :                     }
     491           0 :                     Mpy_32_16_ss( Ep_vari_fx, 3277, &L_tmp, &lo );
     492           0 :                     Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     493           0 :                     L_tmp = L_shl( L_tmp, sub( 12, exp ) ); /*Q(13+exp-15 +12-exp +4 = 14) */
     494             : 
     495           0 :                     tmp2 = extract_h( Ep_avrg_fx ); /*Q13-16=-3 */
     496           0 :                     IF( tmp2 != 0 )
     497             :                     {
     498           0 :                         exp = norm_s( tmp2 );
     499           0 :                         tmp2 = shl( tmp2, exp );     /*Q(exp) */
     500           0 :                         tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
     501           0 :                         exp = sub( 29, exp );
     502             :                     }
     503             :                     ELSE
     504             :                     {
     505           0 :                         tmp2 = 0x7fff;
     506           0 :                         move16();
     507           0 :                         exp = 0;
     508           0 :                         move16();
     509             :                     }
     510           0 :                     Mpy_32_16_ss( Ep_vari_fx, 6554, &L_tmp2, &lo );
     511           0 :                     Mpy_32_16_ss( L_tmp2, tmp2, &L_tmp2, &lo );
     512           0 :                     L_tmp2 = L_shl( L_tmp2, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +3 = 14) */
     513           0 :                     L_tmp = L_min( L_tmp, L_tmp2 );           /*14 */
     514           0 :                     tmp = extract_l( L_min( L_tmp, 13107 ) ); /*14 */
     515           0 :                     alpha_fx = add( 16384, tmp );
     516             :                 }
     517         632 :                 IF( sub( hHQ_core->last_bitalloc_max_band[j++], 1 ) == 0 )
     518             :                 {
     519         180 :                     Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
     520         180 :                     tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-2 */
     521         180 :                     IF( tmp != 0 )
     522             :                     {
     523         179 :                         exp = norm_s( tmp );
     524         179 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     525         179 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     526         179 :                         exp = sub( 29, exp );
     527             :                     }
     528             :                     ELSE
     529             :                     {
     530           1 :                         tmp = 0x7fff;
     531           1 :                         move16();
     532           1 :                         exp = 0;
     533           1 :                         move16();
     534             :                     }
     535         180 :                     Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
     536             : #ifndef BASOP_NOGLOB
     537             :                     L_tmp = L_shl( L_tmp, sub( 14, exp ) );     /*Q(13+exp-15 +14-exp+2 = 14) */
     538             :                     L_tmp = L_max( L_tmp, 16384 );              /*14 */
     539             :                     tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
     540             :                     alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
     541             : #else                                                           /* BASOP_NOGLOB */
     542         180 :                     L_tmp = L_shl_o( L_tmp, sub( 14, exp ), &Overflow ); /*Q(13+exp-15 +14-exp+2 = 14) */
     543         180 :                     L_tmp = L_max( L_tmp, 16384 );                       /*14 */
     544         180 :                     tmp = extract_l( L_min( L_tmp, beta_fx ) );          /*14 */
     545         180 :                     alpha_fx = shl( mult( alpha_fx, tmp ), 1 );          /*14+14-15 +1=14 */
     546             : #endif                                                          /* BASOP_NOGLOB */
     547             :                 }
     548             :                 ELSE
     549             :                 {
     550         452 :                     tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
     551         452 :                     IF( tmp2 != 0 )
     552             :                     {
     553         452 :                         exp = norm_s( tmp2 );
     554         452 :                         tmp2 = shl( tmp2, exp );     /*Q(exp) */
     555         452 :                         tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
     556         452 :                         exp = sub( 29, exp );
     557             :                     }
     558             :                     ELSE
     559             :                     {
     560             :                         /*when the divisor is zero, happens rarely*/
     561           0 :                         tmp2 = 0x7fff;
     562           0 :                         move16();
     563           0 :                         exp = 0;
     564           0 :                         move16();
     565             :                     }
     566         452 :                     Mpy_32_16_ss( Ep_tmp_fx[i], tmp2, &L_tmp, &lo );
     567         452 :                     L_tmp = L_shl( L_tmp, sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
     568         452 :                     Mpy_32_16_ss( L_tmp, shl( sub( bands, lowband ), 9 ), &L_tmp, &lo );
     569         452 :                     L_tmp = L_max( L_tmp, 13926 );               /*14 */
     570         452 :                     tmp2 = extract_l( L_min( L_tmp, 16384 ) );   /*14 */
     571         452 :                     alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
     572             :                 }
     573         632 :                 Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
     574         632 :                 L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
     575             :             }
     576             :         }
     577         333 :         lowband = 3;
     578         333 :         move16();
     579         333 :         Ep_avrg_fx = 0;
     580         333 :         move32();
     581         333 :         Ep_avrgL_fx = 0;
     582         333 :         move32();
     583         333 :         Ep_peak_fx = 0;
     584         333 :         move32();
     585        4938 :         FOR( i = 0; i < bands; i++ )
     586             :         {
     587        4605 :             IF( sub( i, lowband ) >= 0 )
     588             :             {
     589             : #ifdef BASOP_NOGLOB
     590        3606 :                 Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
     591             : #else
     592             :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] );          /*Q15 */
     593             : #endif
     594             :             }
     595             :             ELSE
     596             :             {
     597         999 :                 Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
     598         999 :                 IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
     599             :                 {
     600         707 :                     Ep_peak_fx = Ep_tmp_fx[i];
     601         707 :                     move32(); /*Q13 */
     602             :                 }
     603             :             }
     604             :         }
     605         333 :         Mpy_32_16_ss( Ep_peak_fx, 28262, &L_tmp, &lo );
     606         333 :         Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp2, &lo );
     607         333 :         IF( L_sub( L_shr( Ep_avrg_fx, 2 ), L_tmp2 ) > 0 && L_sub( L_shr( Ep_avrg_fx, 4 ), L_tmp2 ) < 0 && L_sub( L_tmp, Ep_avrgL_fx ) > 0 )
     608             :         {
     609          75 :             adjustFlag = 1;
     610          75 :             move16();
     611         300 :             FOR( i = 0; i < lowband; i++ )
     612             :             {
     613         225 :                 tmp = extract_h( Ep_avrgL_fx ); /*Q-4 */
     614         225 :                 IF( tmp != 0 )
     615             :                 {
     616         225 :                     exp = norm_s( tmp );
     617         225 :                     tmp = shl( tmp, exp );     /*Q(exp) */
     618         225 :                     tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     619         225 :                     exp = sub( 29, exp );
     620             :                 }
     621             :                 ELSE
     622             :                 {
     623             :                     /*when the divisor is zero, happens rarely*/
     624           0 :                     tmp = 0x7fff;
     625           0 :                     move16();
     626           0 :                     exp = 0;
     627           0 :                     move16();
     628             :                 }
     629         225 :                 Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
     630         225 :                 Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
     631         225 :                 Mpy_32_16_ss( L_tmp, 18842, &L_tmp, &lo );
     632             : #ifndef BASOP_NOGLOB
     633             :                 L_tmp = L_shl( L_tmp, sub( 27, exp ) );    /*Q14 0.5 */
     634             :                 tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
     635             : #else                                                      /* BASOP_NOGLOB */
     636         225 :                 L_tmp = L_shl_o( L_tmp, sub( 27, exp ), &Overflow );     /*Q14 0.5 */
     637         225 :                 tmp2 = extract_l( L_min( L_tmp, 19661 ) );               /*14 */
     638             : #endif                                                     /* BASOP_NOGLOB */
     639         225 :                 Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_tmp, &lo );
     640         225 :                 L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy  */
     641             :             }
     642             :         }
     643             : #undef WMC_TOOL_SKIP
     644             : 
     645        4938 :         for ( i = 0; i < bands; i++ )
     646             :         {
     647        4605 :             band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
     648             :         }
     649             : 
     650         333 :         hq2_bit_alloc( band_energy_tmp, bands, Rk_fx, &bit_budget, p2a_flags, bit_alloc_weight_fx, band_width, *num_bits, hqswb_clas, st->bwidth, is_transient );
     651             : 
     652             :         /* encode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
     653         999 :         for ( i = 0; i < 2; i++ )
     654             :         {
     655         666 :             push_indice( hBstr, IND_HQ2_LAST_BA_MAX_BAND, hHQ_core->last_bitalloc_max_band[i], 1 );
     656             :         }
     657             :     }
     658        1643 :     else if ( is_transient == 0 && inner_frame == L_FRAME16k )
     659             :     {
     660             : #define WMC_TOOL_SKIP
     661          39 :         bit_budget = sub( bit_budget, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not */
     662         771 :         FOR( i = 0; i < bands; i++ )
     663             :         {
     664             : #ifndef BASOP_NOGLOB
     665             :             Ep_tmp_fx[i] = L_shl( Ep_tmp_fx[i], 2 );
     666             : #else  /* BASOP_NOGLOB */
     667         732 :             Ep_tmp_fx[i] = L_shl_o( Ep_tmp_fx[i], 2, &Overflow );
     668             : #endif /* BASOP_NOGLOB */
     669             :         }
     670          39 :         IF( st->core_brate == ACELP_13k20 )
     671             :         {
     672          24 :             lowband = 8;
     673          24 :             move16();
     674          24 :             highband = 15;
     675          24 :             move16();
     676          24 :             bw_low = sub( band_start[highband], band_start[lowband] );
     677          24 :             bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
     678             :         }
     679             :         ELSE
     680             :         {
     681          15 :             lowband = 8;
     682          15 :             move16();
     683          15 :             highband = 16;
     684          15 :             move16();
     685          15 :             bw_low = sub( band_start[highband], band_start[lowband] );
     686          15 :             bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
     687             :         }
     688             :         /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
     689          39 :         enerL_fx = 0;
     690          39 :         move32();
     691          39 :         enerH_fx = 0;
     692          39 :         move32();
     693          39 :         Ep_vari_fx = 0;
     694          39 :         move32();
     695          39 :         Ep_avrg_fx = 0;
     696          39 :         move32();
     697         771 :         FOR( i = 0; i < bands; i++ )
     698             :         {
     699         732 :             IF( sub( i, lowband ) >= 0 && add( sub( i, bands ), p2a_bands ) < 0 )
     700             :             {
     701         303 :                 Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
     702             : #ifndef BASOP_NOGLOB
     703             :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
     704             : #else                                                           /* BASOP_NOGLOB */
     705         303 :                 Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow );   /*Q15 */
     706             : #endif                                                          /* BASOP_NOGLOB */
     707             :             }
     708             : 
     709         732 :             IF( sub( i, highband ) >= 0 )
     710             :             {
     711         132 :                 enerH_fx = L_add( enerH_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
     712             :             }
     713         600 :             ELSE IF( sub( i, lowband ) >= 0 )
     714             :             {
     715         288 :                 enerL_fx = L_add( enerL_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
     716             :             }
     717             :         }
     718          39 :         enerL = (float) ( enerL_fx / pow( 2.0, -4 ) );
     719          39 :         enerH = (float) ( enerH_fx / pow( 2.0, -4 ) );
     720             :         /* modify the last p2a_bands subbands band_energies */
     721          39 :         k = bands;
     722          39 :         mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
     723             : 
     724          39 :         L_tmp = L_max( enerH_fx, enerL_fx );
     725          39 :         tmp = s_max( bw_low, bw_high );
     726          39 :         i = norm_l( L_tmp );
     727          39 :         j = norm_s( tmp );
     728          39 :         Mpy_32_16_ss( L_shl( enerH_fx, i ), shl( bw_low, j ), &L_tmp, &lo );
     729          39 :         Mpy_32_16_ss( L_shl( enerL_fx, i ), shl( bw_high, j ), &L_tmp2, &lo );
     730          39 :         L_tmp2 = L_sub( L_tmp, L_tmp2 );
     731             : 
     732         156 :         FOR( i = sub( bands, p2a_bands ); i < bands; i++ )
     733             :         {
     734         117 :             IF( sub( p2a_flags[i], 1 ) == 0 || L_tmp2 > 0 )
     735             :             {
     736          19 :                 tmp = sub( bands, p2a_bands );
     737          19 :                 tmp = sub( tmp, lowband ); /*Q0 */
     738             : 
     739             : #ifndef BASOP_NOGLOB
     740             :                 tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
     741             : #else
     742          19 :                 tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) );       /*Q0 */
     743             : #endif
     744          19 :                 IF( tmp1 != 0 )
     745             :                 {
     746          19 :                     exp = norm_s( tmp1 );
     747          19 :                     tmp1 = shl( tmp1, exp );     /*Q(exp) */
     748          19 :                     tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
     749          19 :                     exp = sub( 29, exp );
     750             :                 }
     751             :                 ELSE
     752             :                 {
     753           0 :                     tmp1 = 0x7fff;
     754           0 :                     move16();
     755           0 :                     exp = 0;
     756           0 :                     move16();
     757             :                 }
     758          19 :                 Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
     759          19 :                 Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     760          19 :                 Mpy_32_16_ss( L_tmp, 16384, &L_tmp, &lo );
     761          19 :                 L_tmp = L_shl( L_tmp, sub( 32, exp ) );  /*Q15 */
     762          19 :                 tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
     763          19 :                 Mpy_32_16_ss( Ep_vari_fx, tmp1, &L_tmp, &lo );
     764          19 :                 Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     765          19 :                 L_tmp = L_shl( L_tmp, sub( 15, exp ) );               /*Q15 */
     766          19 :                 tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
     767          19 :                 alpha_fx = add( tmp, 16384 );                         /*Q14 */
     768             :             }
     769             :             ELSE
     770             :             {
     771          98 :                 alpha_fx = 16384;
     772          98 :                 move16(); /*Q14 */
     773             :             }
     774             : 
     775         117 :             IF( add( sub( i, bands ), p2a_bands ) > 0 )
     776             :             {
     777          78 :                 tmp = sub( bands, p2a_bands );
     778          78 :                 IF( sub( hHQ_core->last_bitalloc_max_band[sub( i, add( tmp, 1 ) )], 1 ) == 0 )
     779             :                 {
     780          19 :                     tmp = sub( tmp, lowband );
     781          19 :                     Mpy_32_16_ss( Ep_tmp_fx[i], tmp, &L_tmp, &lo );
     782             : #ifndef BASOP_NOGLOB
     783             :                     tmp = extract_h( L_shl( L_tmp, 16 ) ); /*Q0 */
     784             : #else                                                      /* BASOP_NOGLOB */
     785          19 :                     tmp = extract_h( L_shl_o( L_tmp, 16, &Overflow ) );        /*Q0 */
     786             : #endif                                                     /* BASOP_NOGLOB */
     787          19 :                     IF( tmp != 0 )
     788             :                     {
     789          19 :                         exp = norm_s( tmp );
     790          19 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     791          19 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     792          19 :                         exp = sub( 29, exp );
     793             :                     }
     794             :                     ELSE
     795             :                     {
     796           0 :                         tmp = 0x7fff;
     797           0 :                         move16();
     798           0 :                         exp = 0;
     799           0 :                         move16();
     800             :                     }
     801          19 :                     Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
     802          19 :                     L_tmp = L_shl( L_tmp, sub( 14, exp ) );                   /*Q14 */
     803          19 :                     tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
     804          19 :                     L_tmp = L_mult( alpha_fx, tmp );                          /*Q(14+14+1=29) */
     805          19 :                     alpha_fx = extract_l( L_shr( L_tmp, 15 ) );               /*Q14*/
     806             :                 }
     807             :                 ELSE
     808             :                 {
     809          59 :                     tmp = sub( tmp, lowband );
     810             : 
     811             : #ifndef BASOP_NOGLOB
     812             :                     tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
     813             : #else
     814          59 :                     tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) );   /*Q0 */
     815             : #endif
     816          59 :                     IF( tmp1 != 0 )
     817             :                     {
     818          59 :                         exp = norm_s( tmp1 );
     819          59 :                         tmp1 = shl( tmp1, exp );     /*Q(exp) */
     820          59 :                         tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
     821          59 :                         exp = sub( 29, exp );
     822             :                     }
     823             :                     ELSE
     824             :                     {
     825           0 :                         tmp1 = 0x7fff;
     826           0 :                         move16();
     827           0 :                         exp = 0;
     828           0 :                         move16();
     829             :                     }
     830          59 :                     Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
     831          59 :                     Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     832          59 :                     L_tmp = L_shl( L_tmp, sub( 29, exp ) );                   /*Q14 */
     833          59 :                     tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
     834          59 :                     L_tmp = L_mult( alpha_fx, tmp );                          /*Q(14+14+1=29) */
     835          59 :                     alpha_fx = extract_l( L_shr( L_tmp, 15 ) );               /*Q14  */
     836             :                 }
     837             :             }
     838         117 :             Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
     839         117 :             L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
     840             :         }
     841          39 :         lowband = 6;
     842          39 :         move16();
     843          39 :         Ep_avrg_fx = 0;
     844          39 :         move32();
     845          39 :         Ep_avrgL_fx = 0;
     846          39 :         move32();
     847          39 :         Ep_peak_fx = 0;
     848          39 :         move32();
     849         771 :         FOR( i = 0; i < bands; i++ )
     850             :         {
     851         732 :             IF( sub( i, lowband ) >= 0 )
     852             :             {
     853             : #ifndef BASOP_NOGLOB
     854             :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
     855             : #else                                                           /* BASOP_NOGLOB */
     856         498 :                 Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow );   /*Q15 */
     857             : #endif                                                          /* BASOP_NOGLOB */
     858             :             }
     859             :             ELSE
     860             :             {
     861             : #ifndef BASOP_NOGLOB
     862             :                 Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
     863             : #else                                                             /* BASOP_NOGLOB */
     864         234 :                 Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow ); /*Q15 */
     865             : #endif                                                            /* BASOP_NOGLOB */
     866         234 :                 IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
     867             :                 {
     868          55 :                     Ep_peak_fx = Ep_tmp_fx[i];
     869          55 :                     move32(); /*Q15 */
     870             :                 }
     871             :             }
     872             :         }
     873             : 
     874          39 :         Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
     875          39 :         Mpy_32_16_ss( Ep_peak_fx, 19661, &L_tmp2, &lo );
     876          39 :         Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp3, &lo );
     877             : 
     878          39 :         IF( ( L_sub( L_shr( Ep_avrgL_fx, 1 ), Ep_avrg_fx ) > 0 && L_sub( L_tmp, L_shr( Ep_avrgL_fx, 2 ) ) > 0 && L_sub( L_shr( Ep_avrgL_fx, 1 ), L_tmp2 ) < 0 ) ||
     879             :             ( L_sub( L_shr( Ep_avrg_fx, 1 ), Ep_avrgL_fx ) > 0 && L_sub( L_shr( Ep_avrg_fx, 3 ), L_tmp3 ) < 0 && L_sub( L_tmp, L_shr( Ep_avrgL_fx, 2 ) ) > 0 ) )
     880             :         {
     881           6 :             adjustFlag = 1;
     882           6 :             move16();
     883          42 :             FOR( i = 0; i < lowband; i++ )
     884             :             {
     885             : #ifndef BASOP_NOGLOB
     886             :                 tmp = extract_h( L_shl( Ep_avrgL_fx, 1 ) ); /*Q0 */
     887             : #else
     888          36 :                 tmp = extract_h( L_shl_o( Ep_avrgL_fx, 1, &Overflow ) );       /*Q0 */
     889             : #endif
     890          36 :                 IF( tmp != 0 )
     891             :                 {
     892          36 :                     exp = norm_s( tmp );
     893          36 :                     tmp = shl( tmp, exp );     /*Q(exp) */
     894          36 :                     tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     895          36 :                     exp = sub( 29, exp );
     896             :                 }
     897             :                 ELSE
     898             :                 {
     899           0 :                     tmp = 0x7fff;
     900           0 :                     move16();
     901           0 :                     exp = 0;
     902           0 :                     move16();
     903             :                 }
     904          36 :                 Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
     905          36 :                 Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
     906          36 :                 L_tmp = L_shl( L_tmp, sub( 28, exp ) );   /*Q14 0.5 */
     907          36 :                 tmp = extract_l( L_min( L_tmp, 19661 ) ); /*Q14 */
     908          36 :                 Mpy_32_16_ss( L_band_energy_tmp[i], tmp, &L_tmp, &lo );
     909          36 :                 L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy  */
     910             :             }
     911             :         }
     912             : #undef WMC_TOOL_SKIP
     913             : 
     914         771 :         for ( i = 0; i < bands; i++ )
     915             :         {
     916         732 :             band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
     917             :         }
     918             : 
     919          39 :         hq2_bit_alloc( band_energy_tmp, bands, Rk_fx, &bit_budget, p2a_flags, bit_alloc_weight_fx, band_width, *num_bits, hqswb_clas, st->bwidth, is_transient );
     920             : 
     921             :         /* encode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
     922         117 :         for ( i = 0; i < 2; i++ )
     923             :         {
     924          78 :             push_indice( hBstr, IND_HQ2_LAST_BA_MAX_BAND, hHQ_core->last_bitalloc_max_band[i], 1 );
     925             :         }
     926             :     }
     927        1604 :     else if ( st->bwidth == SWB && hqswb_clas == HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     928             :     {
     929             :         /* bit allocation for harmonic mode */
     930          48 :         hq2_bit_alloc_har( band_energy, bit_budget, bands, Rk_fx, p2a_bands, bwe_br, p2a_flags, band_width );
     931             :     }
     932             :     else
     933             :     {
     934             : 
     935             :         /* estimate number of bits per band */
     936        1556 :         hq2_bit_alloc( band_energy, bands, Rk_fx, &bit_budget, p2a_flags, bit_alloc_weight_fx, band_width, *num_bits, hqswb_clas, st->bwidth, is_transient );
     937             :     }
     938             : 
     939             : #ifdef DEBUGGING
     940             :     tcq_core_LR_enc( hBstr, st->idchan, inp_vector, t_audio, y2, bit_budget, bands, band_start, band_end, band_width, Rk_fx, npulses, k_sort, p2a_flags, p2a_bands, hHQ_core->last_bitalloc_max_band, inner_frame, adjustFlag, is_transient );
     941             : #else
     942        1976 :     tcq_core_LR_enc( hBstr, inp_vector, t_audio, y2, bit_budget, bands, band_start, band_end, band_width, Rk_fx, npulses, k_sort, p2a_flags, p2a_bands, hHQ_core->last_bitalloc_max_band, inner_frame, adjustFlag, is_transient );
     943             : #endif
     944             : 
     945        1976 :     if ( ( inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 ) || inner_frame == L_FRAME16k )
     946             :     {
     947         420 :         j = 0;
     948        1260 :         for ( i = 2; i > 0; i-- )
     949             :         {
     950         840 :             if ( npulses[bands - i] > 0 )
     951             :             {
     952         280 :                 hHQ_core->last_bitalloc_max_band[j] = 1;
     953             :             }
     954             :             else
     955             :             {
     956         560 :                 hHQ_core->last_bitalloc_max_band[j] = 0;
     957             :             }
     958         840 :             j++;
     959             :         }
     960             :     }
     961             : 
     962             :     /* Prepare floating Rk for next modules */
     963       43143 :     for ( k = 0; k < bands; k++ )
     964             :     {
     965       41167 :         Rk[k] = WORD322FL_SCALE( Rk_fx[k], SWB_BWE_LR_QRk - 1 );
     966             :     }
     967             : 
     968             :     /* Denormalize the coded MDCT spectrum */
     969        1976 :     mdct_spectrum_denorm( inp_vector, y2, band_start, band_end, band_width, band_energy, npulses, bands, ld_slope, pd_thresh );
     970             : 
     971             :     /* Apply fine gain quantization to denormalized coded spectrum */
     972        1976 :     mdct_spectrum_fine_gain_enc( hBstr, t_audio, y2, band_start, band_end, k_sort, bands, L_qint, Ngq, gqlevs, gqbits );
     973             : 
     974             :     /* reStore the subband information*/
     975        1976 :     if ( flag_spt == 1 )
     976             :     {
     977        1450 :         spt_shorten_domain_band_restore( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
     978             :     }
     979             : 
     980             :     /* Inject noise into components having relatively low pulse energy per band */
     981        1976 :     ni_seed = npulses[0] + npulses[1] + npulses[2] + npulses[3];
     982             : 
     983      964080 :     for ( i = 0; i < band_end[bands - 1] + 1; i++ )
     984      962104 :         y2_ni[i] = y2[i];
     985        1976 :     hq2_noise_inject( y2_ni, band_start, band_end, band_width, Ep, Rk, npulses, ni_seed, bands, 0, bw_low, bw_high, enerL, enerH, hHQ_core->last_ni_gain, hHQ_core->last_env, &hHQ_core->last_max_pos_pulse, p2a_flags, p2a_bands, hqswb_clas, st->bwidth, bwe_br );
     986             : 
     987        1976 :     if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     988             :     {
     989        1556 :         if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
     990             :         {
     991        1498 :             preset_hq2_swb( hqswb_clas, band_end, &har_bands, p2a_bands, length, bands, &lowlength, &highlength, m );
     992             : 
     993        1498 :             swb_bwe_enc_lr( st, y2, t_audio, m, bwe_br, bands, band_start, band_end, band_energy, p2a_flags, hqswb_clas, lowlength, highlength, hHQ_core->prev_frm_index, har_bands, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, band_width, y2_ni, &ni_seed );
     994             : 
     995        1498 :             post_hq2_swb( m, lowlength, highlength, hqswb_clas, har_bands, bands, p2a_flags, band_start, band_end, y2, npulses );
     996             : 
     997        1498 :             if ( hqswb_clas == HQ_NORMAL )
     998             :             {
     999        1450 :                 spt_swb_peakpos_tmp_save( y2, bands, band_start, band_end, prev_SWB_peak_pos_tmp );
    1000        7250 :                 for ( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
    1001             :                 {
    1002        5800 :                     if ( p2a_flags[bands - SPT_SHORTEN_SBNUM + k] == 0 || npulses[bands - SPT_SHORTEN_SBNUM + k] == 0 )
    1003             :                     {
    1004        5738 :                         prev_SWB_peak_pos_tmp[k] = 0;
    1005             :                     }
    1006             :                 }
    1007             :             }
    1008             : 
    1009        1498 :             mvr2r( y2_ni, y2, lowlength );
    1010             :         }
    1011             :         else
    1012             :         {
    1013          58 :             mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* HQ_TRANSIENT */
    1014             :         }
    1015             :     }
    1016             :     else
    1017             :     {
    1018         420 :         mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* NB, WB */
    1019             :     }
    1020             : 
    1021             : 
    1022        1976 :     updat_prev_frm( y2, t_audio, bwe_br, length, inner_frame, bands, st->bwidth, is_transient, hqswb_clas, &hHQ_core->prev_hqswb_clas, hHQ_core->prev_SWB_peak_pos, prev_SWB_peak_pos_tmp, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, bws_cnt );
    1023             : 
    1024        1976 :     if ( st->bwidth != SWB )
    1025             :     {
    1026             :         /* reset HQ classifier memories */
    1027         420 :         hHQ_core->mode_count = 0;
    1028         420 :         hHQ_core->mode_count1 = 0;
    1029             :     }
    1030        1976 :     if ( hqswb_clas != HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
    1031             :     {
    1032        1508 :         hHQ_core->prev_frm_index[0] = -1;
    1033        1508 :         hHQ_core->prev_frm_index[1] = -1;
    1034             :     }
    1035             :     /* update number of unused bits */
    1036        1976 :     *num_bits = 0;
    1037             : 
    1038        1976 :     hHQ_core->hvq_hangover = 0;
    1039             : 
    1040        1976 :     return;
    1041             : }
    1042             : 
    1043             : /*--------------------------------------------------------------------------*
    1044             :  * small_symbol_enc_tran()
    1045             :  *
    1046             :  * Huffman encoding of differential energies, estimating or packing bits
    1047             :  * if flag_pack = 0, LC mode info. is output else LC mode info. is input
    1048             :  * if flag_pack = 0, estimatng else packing bits
    1049             :  *--------------------------------------------------------------------------*/
    1050             : 
    1051             : /*! r: bits */
    1052         193 : static int16_t small_symbol_enc_tran(
    1053             :     BSTR_ENC_HANDLE hBstr,   /* i/o: encoder bitstream handle                 */
    1054             :     const int16_t *qbidx,    /* i  : input of dequantized differential energy */
    1055             :     const int16_t BANDS,     /* i  : number of bands                          */
    1056             :     int16_t *hLCmode,        /* i/o: LC mode info                             */
    1057             :     const int16_t flag_pack, /* i  : indicator of packing or estimating bits  */
    1058             :     const int16_t is_transient )
    1059             : {
    1060             :     int16_t i, bits;
    1061             :     int16_t difidx[BANDS_MAX];
    1062             : 
    1063        5125 :     for ( i = 0; i < BANDS; i++ )
    1064             :     {
    1065        4932 :         difidx[i] = qbidx[i] + LRMDCT_BE_OFFSET;
    1066             :     }
    1067             : 
    1068        5125 :     for ( i = 0; i < BANDS; ++i )
    1069             :     {
    1070        4932 :         if ( difidx[i] > LRMDCT_BE_LIMIT || difidx[i] < 0 )
    1071             :         {
    1072             :             /* Huffman cannot encode this vector */
    1073           0 :             return -1;
    1074             :         }
    1075             :     }
    1076             : 
    1077             :     /* Preparing lossless coding input */
    1078         193 :     if ( flag_pack == 0 )
    1079             :     {
    1080             :         /* estimating # of bits */
    1081         106 :         bits = encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE_TRAN, is_transient );
    1082         106 :         bits += BITS_DE_FCOMP; /* xx bits for diff. energies + BITS_DE_FCOMP bits for first energies */
    1083             :     }
    1084             :     else
    1085             :     {
    1086          87 :         bits = 0;
    1087          87 :         encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE_TRAN, is_transient );
    1088             :     }
    1089             : 
    1090         193 :     return bits + BITS_DE_HMODE; /* xx bits for diff. energies + 1 bit for LC coding mode */
    1091             : }
    1092             : 
    1093             : 
    1094             : /*--------------------------------------------------------------------------*
    1095             :  * small_symbol_enc()
    1096             :  *
    1097             :  * Huffman encoding of differential energies, estimating or packing bits
    1098             :  * if flag_pack = 0, LC mode info. is output else LC mode info. is input
    1099             :  * if flag_pack = 0, estimatng else packing bits
    1100             :  *--------------------------------------------------------------------------*/
    1101             : 
    1102             : /*! r: bits */
    1103        3299 : static int16_t small_symbol_enc(
    1104             :     BSTR_ENC_HANDLE hBstr,   /* i/o: encoder bitstream handle                 */
    1105             :     const int16_t *qbidx,    /* i  : input of dequantized differential energy */
    1106             :     const int16_t BANDS,     /* i  : number of bands                          */
    1107             :     int16_t *hLCmode,        /* i/o: LC mode info                             */
    1108             :     const int16_t flag_pack, /* i  : indicator of packing or estimating bits  */
    1109             :     const int16_t is_transient )
    1110             : {
    1111             :     int16_t i, bits;
    1112             :     int16_t difidx[BANDS_MAX], LSB[BANDS_MAX];
    1113             : 
    1114             :     /* Preparing lossless coding input */
    1115        3299 :     difidx[0] = qbidx[0] + DE_OFFSET0;
    1116             : 
    1117       67391 :     for ( i = 1; i < BANDS; ++i )
    1118             :     {
    1119       64092 :         difidx[i] = qbidx[i] + DE_OFFSET1;
    1120             :     }
    1121             : 
    1122       70690 :     for ( i = 0; i < BANDS; ++i )
    1123             :     {
    1124       67391 :         if ( difidx[i] >= DE_LIMIT || difidx[i] < 0 )
    1125             :         {
    1126             :             /* Huffman cannot encode this vector */
    1127           0 :             return -1;
    1128             :         }
    1129             :     }
    1130             : 
    1131             :     /* splitting MSB and LSB */
    1132       70690 :     for ( i = 0; i < BANDS; ++i )
    1133             :     {
    1134       67391 :         LSB[i] = difidx[i] & 1;
    1135       67391 :         difidx[i] >>= 1;
    1136             :     }
    1137             : 
    1138             :     /* Preparing lossless coding input */
    1139        3299 :     if ( flag_pack == 0 )
    1140             :     {
    1141             :         /* estimating # of bits */
    1142             :         /* Encoding MSB bits */
    1143        1870 :         bits = encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE, is_transient );
    1144        1870 :         bits += BITS_DE_FCOMP; /* xx bits for diff. energies + BITS_DE_FCOMP bits for first energies */
    1145             : 
    1146             :         /* Encoding LSB bit packing */
    1147        1870 :         bits += BANDS;
    1148             :     }
    1149             :     else
    1150             :     {
    1151             :         /* Encoding MSB bits */
    1152        1429 :         bits = 0;
    1153        1429 :         encode_envelope_indices( hBstr, BANDS, -1, difidx, hLCmode, flag_pack, LOW_RATE_HQ_CORE, is_transient );
    1154             : 
    1155             :         /* Encoding LSB bit packing */
    1156       30401 :         for ( i = 0; i < BANDS; ++i )
    1157             :         {
    1158       28972 :             push_indice( hBstr, IND_HQ2_DIFF_ENERGY, LSB[i], BITS_DE_LSB );
    1159             :         }
    1160             :     }
    1161             : 
    1162        3299 :     return bits + BITS_DE_HMODE; /* xx bits for diff. energies + 1 bit for LC coding mode */
    1163             : }
    1164             : 
    1165             : 
    1166             : /*--------------------------------------------------------------------------*
    1167             :  * large_symbol_enc()
    1168             :  *
    1169             :  *
    1170             :  *--------------------------------------------------------------------------*/
    1171             : 
    1172             : /*! r: bits */
    1173        2436 : static int16_t large_symbol_enc(
    1174             :     BSTR_ENC_HANDLE hBstr,  /* i/o: encoder bitstream handle                 */
    1175             :     int16_t *qbidx,         /* i  : input of dequantized differential energy */
    1176             :     const int16_t BANDS,    /* i  : number of bands                          */
    1177             :     int16_t *hLCmode0,      /* i/o: LC mode info                             */
    1178             :     const int16_t flag_pack /* i  : indicator of packing or estimating bits  */
    1179             : )
    1180             : {
    1181             :     int16_t i, bits;
    1182             :     int16_t LSB1[BANDS_MAX];
    1183        2436 :     int16_t min_q = 513, max_q = -1, offset0;
    1184             :     int16_t min_bits, min_bits_pos;
    1185             :     int16_t tdifidx0[BANDS_MAX], tdifidx1[BANDS_MAX];
    1186             :     int16_t basic_shift;
    1187             :     int16_t bitsmode0, bitsmode1;
    1188             :     int16_t lsbdepth1;
    1189             :     int16_t cnt_outlyer, pos_outlyer, cnt_outlyer0;
    1190             : 
    1191        2436 :     cnt_outlyer0 = 0;
    1192        2436 :     cnt_outlyer = 0;
    1193        2436 :     bitsmode0 = 0;
    1194        2436 :     bitsmode1 = 0;
    1195        2436 :     pos_outlyer = 0;
    1196        2436 :     lsbdepth1 = 0;
    1197             : 
    1198        2436 :     if ( flag_pack == 0 || ( flag_pack == 1 && *hLCmode0 == 0 ) )
    1199             :     {
    1200        2075 :         if ( qbidx[0] > ABS_ENG_OFFSET - 1 || qbidx[0] < -ABS_ENG_OFFSET )
    1201             :         {
    1202           0 :             cnt_outlyer0 = 2;
    1203             :         }
    1204        2075 :         else if ( qbidx[0] > 3 || qbidx[0] < -4 )
    1205             :         {
    1206        1146 :             cnt_outlyer0 = 1;
    1207             :         }
    1208             :         else
    1209             :         {
    1210         929 :             cnt_outlyer0 = 0;
    1211             :         }
    1212             : 
    1213        2075 :         cnt_outlyer = 0;
    1214        2075 :         pos_outlyer = -1;
    1215       43472 :         for ( i = 1; i < BANDS; ++i )
    1216             :         {
    1217       41397 :             if ( qbidx[i] > 3 || qbidx[i] < -4 )
    1218             :             {
    1219        6198 :                 cnt_outlyer++;
    1220        6198 :                 pos_outlyer = i;
    1221             :             }
    1222             : 
    1223       41397 :             if ( qbidx[i] > ABS_ENG_OFFSET - 1 || qbidx[i] < -ABS_ENG_OFFSET )
    1224             :             {
    1225           0 :                 cnt_outlyer++;
    1226             :             }
    1227             :         }
    1228             : 
    1229        2075 :         if ( cnt_outlyer0 == 0 && cnt_outlyer <= 1 )
    1230             :         {
    1231         346 :             bitsmode0 = BITS_DE_8SMODE + BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
    1232         346 :             if ( cnt_outlyer == 1 )
    1233             :             {
    1234             :                 /* 01 */
    1235         149 :                 bitsmode0 += BITS_DE_8SPOS + BITS_ABS_ENG;
    1236             :             }
    1237             : 
    1238        1201 :             for ( i = 0; i < pos_outlyer; ++i )
    1239             :             {
    1240         855 :                 tdifidx0[i] = qbidx[i];
    1241         855 :                 bitsmode0 += hessize[tdifidx0[i] + 4];
    1242             :             }
    1243             : 
    1244        7409 :             for ( i = pos_outlyer + 1; i < BANDS; ++i )
    1245             :             {
    1246        7063 :                 tdifidx0[i] = qbidx[i];
    1247        7063 :                 bitsmode0 += hessize[tdifidx0[i] + 4];
    1248             :             }
    1249             :         }
    1250        1729 :         else if ( cnt_outlyer0 == 1 && cnt_outlyer <= 1 )
    1251             :         {
    1252         419 :             bitsmode0 = BITS_DE_8SMODE + BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
    1253         419 :             tdifidx0[0] = qbidx[0];
    1254         419 :             bitsmode0 += BITS_ABS_ENG;
    1255         419 :             if ( cnt_outlyer == 1 )
    1256             :             {
    1257             :                 /* 11 */
    1258         206 :                 bitsmode0 += BITS_DE_8SPOS + BITS_ABS_ENG;
    1259             :             }
    1260             :             else
    1261             :             {
    1262         213 :                 pos_outlyer = 0;
    1263             :             }
    1264             : 
    1265        1243 :             for ( i = 1; i < pos_outlyer; ++i )
    1266             :             {
    1267         824 :                 tdifidx0[i] = qbidx[i];
    1268         824 :                 bitsmode0 += hessize[tdifidx0[i] + 4];
    1269             :             }
    1270             : 
    1271        7777 :             for ( i = pos_outlyer + 1; i < BANDS; ++i )
    1272             :             {
    1273        7358 :                 tdifidx0[i] = qbidx[i];
    1274        7358 :                 bitsmode0 += hessize[tdifidx0[i] + 4];
    1275             :             }
    1276             :         }
    1277             :         else
    1278             :         {
    1279        1310 :             bitsmode0 = 20000;
    1280             :         }
    1281             :     }
    1282             : 
    1283        2436 :     if ( flag_pack == 0 || ( flag_pack == 1 && *hLCmode0 == 1 ) )
    1284             :     {
    1285             :         /* components 0 range : -256~255 */
    1286        2337 :         max_q = MINIMUM_ENERGY_LOWBRATE;
    1287        2337 :         min_q = MAXIMUM_ENERGY_LOWBRATE;
    1288       51210 :         for ( i = 0; i < BANDS; ++i )
    1289             :         {
    1290       48873 :             if ( qbidx[i] > max_q )
    1291             :             {
    1292        6571 :                 max_q = qbidx[i];
    1293             :             }
    1294             : 
    1295       48873 :             if ( qbidx[i] < min_q )
    1296             :             {
    1297        5498 :                 min_q = qbidx[i];
    1298             :             }
    1299             :         }
    1300             : 
    1301             :         /* Counting bits for transmitting all components using same method */
    1302        5240 :         for ( i = 0;; ++i )
    1303             :         {
    1304        5240 :             if ( max_q <= ( ( 2 << ( i + 1 ) ) - 1 ) && min_q >= -( 2 << ( i + 1 ) ) )
    1305             :             {
    1306        2337 :                 break;
    1307             :             }
    1308             :         }
    1309        2337 :         basic_shift = i;
    1310             : 
    1311        2337 :         min_bits = 1000;
    1312        2337 :         min_bits_pos = basic_shift;
    1313        9348 :         for ( offset0 = basic_shift; offset0 < basic_shift + 3; offset0++ )
    1314             :         {
    1315        7011 :             max_q = MINIMUM_ENERGY_LOWBRATE;
    1316        7011 :             min_q = MAXIMUM_ENERGY_LOWBRATE;
    1317             : 
    1318        7011 :             bitsmode1 = BITS_DE_8SMODE + BITS_MAX_DEPTH;
    1319      153630 :             for ( i = 0; i < BANDS; ++i )
    1320             :             {
    1321      146619 :                 bitsmode1 += ( hessize[( qbidx[i] >> offset0 ) + 4] + ( offset0 ) );
    1322             :             }
    1323             : 
    1324        7011 :             if ( min_bits > bitsmode1 )
    1325             :             {
    1326        2919 :                 min_bits_pos = offset0;
    1327        2919 :                 min_bits = bitsmode1;
    1328             :             }
    1329             :         }
    1330             : 
    1331        2337 :         bitsmode1 = min_bits;
    1332        2337 :         lsbdepth1 = min_bits_pos;
    1333             : 
    1334       51210 :         for ( i = 0; i < BANDS; ++i )
    1335             :         {
    1336       48873 :             LSB1[i] = qbidx[i] & ( ( 1 << lsbdepth1 ) - 1 );
    1337       48873 :             tdifidx1[i] = qbidx[i] >> lsbdepth1;
    1338             :         }
    1339             :     }
    1340             : 
    1341             :     /* Preparing lossless coding input */
    1342        2436 :     if ( flag_pack == 0 )
    1343             :     {
    1344             :         /* estimating # of bits */
    1345             :         /* Encoding MSB bits */
    1346        1976 :         if ( bitsmode0 < bitsmode1 )
    1347             :         {
    1348         187 :             bits = bitsmode0;
    1349         187 :             *hLCmode0 = 0;
    1350             :         }
    1351             :         else
    1352             :         {
    1353        1789 :             bits = bitsmode1;
    1354        1789 :             *hLCmode0 = 1;
    1355             :         }
    1356             :     }
    1357             :     else
    1358             :     {
    1359             :         /* Encoding MSB bits */
    1360         460 :         if ( *hLCmode0 == 0 )
    1361             :         {
    1362          99 :             push_indice( hBstr, IND_HQ2_DENG_8SMODE, 0, BITS_DE_8SMODE );
    1363          99 :             bits = BITS_DE_8SMODE;
    1364          99 :             if ( cnt_outlyer0 == 0 )
    1365             :             {
    1366          35 :                 push_indice( hBstr, IND_HQ2_DENG_8SMODE_N0, 0, BITS_DE_8SMODE_N0 );
    1367          35 :                 bits += BITS_DE_8SMODE_N0;
    1368          35 :                 if ( cnt_outlyer == 1 )
    1369             :                 {
    1370             :                     /* 01 */
    1371           4 :                     push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 1, BITS_DE_8SMODE_N1 );
    1372           4 :                     bits += BITS_DE_8SMODE_N1;
    1373           4 :                     push_indice( hBstr, IND_HQ2_DENG_8SPOS, pos_outlyer, BITS_DE_8SPOS );
    1374           4 :                     bits += BITS_DE_8SPOS;
    1375           4 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[pos_outlyer] + ABS_ENG_OFFSET, BITS_ABS_ENG );
    1376           4 :                     bits += BITS_ABS_ENG;
    1377             :                 }
    1378             :                 else
    1379             :                 {
    1380             :                     /* 00 */
    1381          31 :                     push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 0, BITS_DE_8SMODE_N1 );
    1382          31 :                     bits += BITS_DE_8SMODE_N1;
    1383             :                 }
    1384             : 
    1385          54 :                 for ( i = 0; i < pos_outlyer; ++i )
    1386             :                 {
    1387          19 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
    1388          19 :                     bitsmode0 += hessize[tdifidx0[i] + 4];
    1389             :                 }
    1390             : 
    1391         927 :                 for ( i = pos_outlyer + 1; i < BANDS; ++i )
    1392             :                 {
    1393         892 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
    1394         892 :                     bitsmode0 += hessize[tdifidx0[i] + 4];
    1395             :                 }
    1396             :             }
    1397          64 :             else if ( cnt_outlyer0 == 1 )
    1398             :             {
    1399          64 :                 push_indice( hBstr, IND_HQ2_DENG_8SMODE_N0, 1, BITS_DE_8SMODE_N0 );
    1400          64 :                 bits += BITS_DE_8SMODE_N0;
    1401          64 :                 if ( cnt_outlyer == 1 )
    1402             :                 {
    1403          16 :                     push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 1, BITS_DE_8SMODE_N1 );
    1404          16 :                     bits += BITS_DE_8SMODE_N1;
    1405          16 :                     push_indice( hBstr, IND_HQ2_DENG_8SPOS, pos_outlyer, BITS_DE_8SPOS );
    1406          16 :                     bits += BITS_DE_8SPOS;
    1407          16 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[0] + ABS_ENG_OFFSET, BITS_ABS_ENG );
    1408          16 :                     bits += BITS_ABS_ENG;
    1409          16 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[pos_outlyer] + ABS_ENG_OFFSET, BITS_ABS_ENG );
    1410          16 :                     bits += BITS_ABS_ENG;
    1411             :                 }
    1412             :                 else
    1413             :                 {
    1414          48 :                     push_indice( hBstr, IND_HQ2_DENG_8SMODE_N1, 0, BITS_DE_8SMODE_N1 );
    1415          48 :                     bits += BITS_DE_8SMODE_N1;
    1416          48 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, qbidx[0] + ABS_ENG_OFFSET, BITS_ABS_ENG );
    1417          48 :                     bits += BITS_ABS_ENG;
    1418             :                 }
    1419             : 
    1420         109 :                 for ( i = 1; i < pos_outlyer; ++i )
    1421             :                 {
    1422          45 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
    1423          45 :                     bits += hessize[tdifidx0[i] + 4];
    1424             :                 }
    1425             : 
    1426        1329 :                 for ( i = pos_outlyer + 1; i < BANDS; ++i )
    1427             :                 {
    1428        1265 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx0[i] + 4], hessize[tdifidx0[i] + 4] );
    1429        1265 :                     bits += hessize[tdifidx0[i] + 4];
    1430             :                 }
    1431             :             }
    1432             :         }
    1433             :         else
    1434             :         {
    1435         361 :             bits = BITS_DE_8SMODE + BITS_MAX_DEPTH;
    1436         361 :             push_indice( hBstr, IND_HQ2_DENG_8SMODE, 1, BITS_DE_8SMODE );
    1437         361 :             push_indice( hBstr, IND_HQ2_DENG_8SDEPTH, lsbdepth1, BITS_MAX_DEPTH );
    1438             : 
    1439        8067 :             for ( i = 0; i < BANDS; ++i )
    1440             :             {
    1441        7706 :                 push_indice( hBstr, IND_HQ2_DIFF_ENERGY, hescode[tdifidx1[i] + 4], hessize[tdifidx1[i] + 4] );
    1442        7706 :                 bits += hessize[tdifidx1[i] + 4];
    1443             :             }
    1444             : 
    1445         361 :             if ( lsbdepth1 > 0 )
    1446             :             {
    1447        8067 :                 for ( i = 0; i < BANDS; ++i )
    1448             :                 {
    1449        7706 :                     push_indice( hBstr, IND_HQ2_DIFF_ENERGY, LSB1[i], lsbdepth1 );
    1450             :                 }
    1451         361 :                 bits += BANDS * lsbdepth1;
    1452             :             }
    1453             :         }
    1454             :     }
    1455             : 
    1456        2436 :     return bits; /* xx bits for diff. energies + 1 bit for LC coding mode */
    1457             : }
    1458             : 
    1459             : /*-------------------------------------------------------------------*
    1460             :  * band_energy_quant()
    1461             :  *
    1462             :  *
    1463             :  *-------------------------------------------------------------------*/
    1464             : 
    1465        1976 : static float band_energy_quant(
    1466             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle    */
    1467             :     const float *t_audio,
    1468             :     const int16_t band_start[],
    1469             :     const int16_t band_end[],
    1470             :     float band_energy[],
    1471             :     const int16_t bands,
    1472             :     const Word32 L_qint,  /* Q29 */
    1473             :     const Word16 eref_fx, /* Q10 */
    1474             :     const int16_t is_transient )
    1475             : {
    1476             :     int16_t i, k;
    1477             :     float E;
    1478             :     int16_t ebits;
    1479             :     int16_t hLCmode0, hLCmode1, deng_bits;
    1480             :     int16_t bq1_temp[BANDS_MAX], bq2_temp[BANDS_MAX];
    1481             :     int16_t bq0;
    1482             :     int16_t bq1[BANDS_MAX];
    1483             :     int16_t bq2[BANDS_MAX];
    1484        1976 :     int16_t deng_cmode = 0;
    1485             :     int16_t hbits;
    1486             :     Word32 L_tmp;
    1487             :     Word32 L_band_energy[BANDS_MAX];
    1488             :     Word16 exp_normd;
    1489             :     Word16 rev_qint_fx;
    1490             :     Word16 Qrev_qint;
    1491             : 
    1492             :     /* Calculate the band energies */
    1493       43143 :     for ( k = 0; k < bands; k++ )
    1494             :     {
    1495       41167 :         E = 0.0f;
    1496     1003271 :         for ( i = band_start[k]; i <= band_end[k]; i++ )
    1497             :         {
    1498      962104 :             E += t_audio[i] * t_audio[i];
    1499             :         }
    1500             : 
    1501       41167 :         band_energy[k] = (float) log2_f( E + 0.18e-1f );
    1502             :     }
    1503             : 
    1504        1976 :     if ( is_transient )
    1505             :     {
    1506         106 :         reverse_transient_frame_energies( band_energy, bands );
    1507             :     }
    1508             : 
    1509             :     /* Quantize the reference and band energies */
    1510       43143 :     for ( k = 0; k < bands; k++ )
    1511       41167 :         L_band_energy[k] = (Word32) ( band_energy[k] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
    1512             : 
    1513             : #define WMC_TOOL_SKIP
    1514        1976 :     exp_normd = norm_l( L_qint );
    1515        1976 :     rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
    1516        1976 :     Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
    1517             : 
    1518        1976 :     bq0 = round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
    1519       43143 :     FOR( k = 0; k < bands; k++ )
    1520             :     {
    1521             :         /*bq1[k] = round_f (band_energy[k] / qint); */
    1522       41167 :         L_tmp = L_mls( L_band_energy[k], rev_qint_fx );            /* Q14+Qrev_qint-15 */
    1523       41167 :         bq1[k] = round_fx( L_shl( L_tmp, sub( 17, Qrev_qint ) ) ); /* 16-(14+Qrev_qint-15) */
    1524             :     }
    1525             : #undef WMC_TOOL_SKIP
    1526             : 
    1527        1976 :     if ( is_transient )
    1528             :     {
    1529         106 :         mvs2s( bq1, bq1_temp, bands );
    1530             : 
    1531             :         /* Calculate the differential energies */
    1532         106 :         diffcod_lrmdct( bands, bq0, bq1_temp, bq2_temp, is_transient );
    1533             :     }
    1534             : 
    1535             :     /* Calculate the differential energies */
    1536        1976 :     bq2[0] = bq1[0] - bq0;
    1537       41167 :     for ( k = 1; k < bands; k++ )
    1538             :     {
    1539       39191 :         bq2[k] = bq1[k] - bq1[k - 1];
    1540             :     }
    1541             : 
    1542             :     /* Modifying qbidx to be located in the range -256~255 */
    1543       43143 :     for ( i = 0; i < bands; ++i )
    1544             :     {
    1545       41167 :         if ( bq2[i] > MAXIMUM_ENERGY_LOWBRATE )
    1546             :         {
    1547           0 :             bq2[i] = MAXIMUM_ENERGY_LOWBRATE;
    1548             :         }
    1549       41167 :         if ( bq2[i] < MINIMUM_ENERGY_LOWBRATE )
    1550             :         {
    1551           0 :             bq2[i] = MINIMUM_ENERGY_LOWBRATE;
    1552             :         }
    1553             :     }
    1554             : 
    1555             :     /* Get number of bits by Huffman0 coding */
    1556        1976 :     ebits = large_symbol_enc( hBstr, bq2, bands, &hLCmode0, 0 );
    1557             : 
    1558        1976 :     if ( is_transient )
    1559             :     {
    1560             :         /* Get number of bits by Huffman coding */
    1561         106 :         hbits = small_symbol_enc_tran( hBstr, bq2_temp, bands, &hLCmode1, 0, is_transient );
    1562             :     }
    1563             :     else
    1564             :     {
    1565             :         /* Get number of bits by Huffman coding */
    1566        1870 :         hbits = small_symbol_enc( hBstr, bq2, bands, &hLCmode1, 0, is_transient );
    1567             :     }
    1568             : 
    1569             :     /* comparing used bits */
    1570        1976 :     if ( ebits < hbits || hbits == -1 )
    1571             :     {
    1572         460 :         deng_cmode = 0;
    1573         460 :         push_indice( hBstr, IND_HQ2_DENG_MODE, deng_cmode, BITS_DE_CMODE );
    1574         460 :         large_symbol_enc( hBstr, bq2, bands, &hLCmode0, 1 );
    1575         460 :         deng_bits = ebits + BITS_DE_CMODE;
    1576             :     }
    1577             :     else
    1578             :     {
    1579             :         /* setting energy difference coding mode and storing it */
    1580        1516 :         deng_cmode = 1;
    1581        1516 :         push_indice( hBstr, IND_HQ2_DENG_MODE, deng_cmode, BITS_DE_CMODE );
    1582             : 
    1583        1516 :         deng_bits = hbits + BITS_DE_CMODE;
    1584             : 
    1585             :         /* packing indice */
    1586        1516 :         if ( is_transient )
    1587             :         {
    1588          87 :             mvs2s( bq2_temp, bq2, bands );
    1589             : 
    1590          87 :             small_symbol_enc_tran( hBstr, bq2, bands, &hLCmode1, 1, is_transient );
    1591             :         }
    1592             :         else
    1593             :         {
    1594        1429 :             small_symbol_enc( hBstr, bq2, bands, &hLCmode1, 1, is_transient );
    1595             :         }
    1596             :     }
    1597             : 
    1598             :     /* Reconstruct quantized spectrum */
    1599        1976 :     bq1[0] = bq2[0] + bq0;
    1600       41167 :     for ( k = 1; k < bands; k++ )
    1601             :     {
    1602       39191 :         bq1[k] = bq2[k] + bq1[k - 1];
    1603             :     }
    1604             : 
    1605       43143 :     for ( k = 0; k < bands; k++ )
    1606             :     {
    1607             : #define WMC_TOOL_SKIP
    1608       41167 :         L_band_energy[k] = L_mls( L_qint, (Word16) bq1[k] );
    1609       41167 :         move32(); /* 29+0-15 -> Qbe(Q14) */
    1610             : #undef WMC_TOOL_SKIP
    1611       41167 :         band_energy[k] = (float) ( L_band_energy[k] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
    1612             :     }
    1613             : 
    1614        1976 :     if ( is_transient )
    1615             :     {
    1616         106 :         reverse_transient_frame_energies( band_energy, bands );
    1617             :     }
    1618             : 
    1619        1976 :     return ( deng_bits );
    1620             : }
    1621             : 
    1622             : 
    1623             : /*-------------------------------------------------------------------*
    1624             :  * p2a_threshold_quant()
    1625             :  *
    1626             :  *
    1627             :  *-------------------------------------------------------------------*/
    1628             : 
    1629        1928 : static int16_t p2a_threshold_quant(
    1630             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle    */
    1631             :     const float *t_audio,
    1632             :     const int16_t band_start[],
    1633             :     const int16_t band_end[],
    1634             :     const int16_t band_width[],
    1635             :     const int16_t bands,
    1636             :     const int16_t p2a_bands,
    1637             :     const float p2a_th,
    1638             :     int16_t *p2a_flags )
    1639             : {
    1640             :     int16_t i, j, k;
    1641             :     float p, a, e;
    1642             :     float p2a;
    1643             : 
    1644        1928 :     set_s( p2a_flags, 1, bands );
    1645             : 
    1646        1928 :     j = 0;
    1647        9433 :     for ( k = bands - p2a_bands; k < bands; k++ )
    1648             :     {
    1649        7505 :         a = 0.0f;
    1650        7505 :         p = 0.0f;
    1651      530816 :         for ( i = band_start[k]; i <= band_end[k]; i++ )
    1652             :         {
    1653      523311 :             e = t_audio[i] * t_audio[i];
    1654      523311 :             if ( e > p )
    1655             :             {
    1656       34993 :                 p = e;
    1657             :             }
    1658      523311 :             a += e;
    1659             :         }
    1660             : 
    1661        7505 :         if ( a > 0.0f )
    1662             :         {
    1663        7505 :             a /= band_width[k];
    1664        7505 :             p2a = 10.0f * (float) log10( p / a );
    1665             : 
    1666        7505 :             if ( p2a <= p2a_th )
    1667             :             {
    1668        7275 :                 p2a_flags[k] = 0;
    1669             :             }
    1670             :         }
    1671             : 
    1672        7505 :         push_indice( hBstr, IND_HQ2_P2A_FLAGS, p2a_flags[k], 1 );
    1673        7505 :         j++;
    1674             :     }
    1675             : 
    1676        1928 :     return ( j );
    1677             : }
    1678             : 
    1679             : 
    1680             : /*-------------------------------------------------------------------*
    1681             :  * mdct_spectrum_fine_gain_enc()
    1682             :  *
    1683             :  *
    1684             :  *-------------------------------------------------------------------*/
    1685             : 
    1686        1976 : static void mdct_spectrum_fine_gain_enc(
    1687             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle    */
    1688             :     const float ybuf[],
    1689             :     float y2[],
    1690             :     const int16_t band_start[],
    1691             :     const int16_t band_end[],
    1692             :     const int16_t k_sort[],
    1693             :     const int16_t bands,
    1694             :     const Word32 L_qint,
    1695             :     const int16_t Ngq,
    1696             :     const int16_t gqlevs,
    1697             :     const int16_t gqbits )
    1698             : {
    1699             :     int16_t i, k, imin;
    1700             :     float Eyy, Exy, gamma;
    1701             :     float dmin, d;
    1702             :     float gain_table[MAX_GQLEVS];
    1703             :     Word16 exp_normn, exp_normd;
    1704             :     Word16 delta_fx, Qdelta;
    1705             :     Word32 L_delta, L_q;
    1706             :     Word32 L_temp;
    1707             :     Word16 gain_table_fx[MAX_GQLEVS];
    1708             :     Word16 Qgt;
    1709             :     Word16 temp_lo_fx, temp_hi_fx;
    1710             : 
    1711             : #define WMC_TOOL_SKIP
    1712             :     /* Fine gain quantization on only the most significant energy bands */
    1713             :     /*delta = qint / gqlevs; */
    1714        1976 :     exp_normn = norm_l( L_qint );
    1715        1976 :     exp_normn = sub( exp_normn, 1 );
    1716        1976 :     exp_normd = norm_s( gqlevs );
    1717        1976 :     delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) );
    1718        1976 :     Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
    1719        1976 :     L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) );
    1720             :     /*q = (-qint + delta) / 2.0f; */
    1721        1976 :     L_q = L_shr( L_sub( L_delta, L_qint ), 1 );
    1722             : 
    1723        5928 :     FOR( i = 0; i < gqlevs; i++ )
    1724             :     {
    1725             :         /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
    1726        3952 :         L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
    1727        3952 :         temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
    1728        3952 :         Qgt = sub( 14, temp_hi_fx );
    1729        3952 :         gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
    1730             : 
    1731             :         /*q += delta; */
    1732        3952 :         L_q = L_add( L_q, L_delta );
    1733        3952 :         gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
    1734        3952 :         gain_table[i] = (float) ( gain_table_fx[i] / pow( 2.0f, 14 ) );
    1735             :     }
    1736             : #undef WMC_TOOL_SKIP
    1737             : 
    1738        9392 :     for ( k = bands - Ngq; k < bands; k++ )
    1739             :     {
    1740        7416 :         Eyy = 0.0f;
    1741        7416 :         Exy = 0.0f;
    1742       75559 :         for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
    1743             :         {
    1744       68143 :             Eyy += y2[i] * y2[i];
    1745       68143 :             Exy += ybuf[i] * y2[i];
    1746             :         }
    1747             : 
    1748        7416 :         if ( Eyy > 0.0f && Exy > 0.0f )
    1749             :         {
    1750        7416 :             gamma = Exy / Eyy;
    1751        7416 :             dmin = FLT_MAX;
    1752        7416 :             imin = -1;
    1753       22248 :             for ( i = 0; i < gqlevs; i++ )
    1754             :             {
    1755       14832 :                 d = (float) fabs( gamma - gain_table[i] );
    1756       14832 :                 if ( d < dmin )
    1757             :                 {
    1758        9976 :                     dmin = d;
    1759        9976 :                     imin = i;
    1760             :                 }
    1761             :             }
    1762             : 
    1763        7416 :             gamma = gain_table[imin];
    1764             : 
    1765       75559 :             for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
    1766             :             {
    1767       68143 :                 y2[i] *= gamma;
    1768             :             }
    1769             :         }
    1770             :         else
    1771             :         {
    1772           0 :             imin = 0;
    1773             :         }
    1774             : 
    1775        7416 :         push_indice( hBstr, IND_HQ2_SUBBAND_GAIN, imin, gqbits );
    1776             :     }
    1777             : 
    1778        1976 :     return;
    1779             : }

Generated by: LCOV version 1.14