LCOV - code coverage report
Current view: top level - lib_dec - hq_lr_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 532 604 88.1 %
Date: 2025-09-14 08:49:17 Functions: 9 9 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_com.h"
      45             : #include "rom_dec.h"
      46             : #include "prot.h"
      47             : #include "stl.h"
      48             : #include "basop_util.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*--------------------------------------------------------------------------*
      52             :  * Local function prototypes
      53             :  *--------------------------------------------------------------------------*/
      54             : 
      55             : static int16_t p2a_threshold_dequant( Decoder_State *st, int16_t *p2a_flags, const int16_t bands, const int16_t p2a_bands );
      56             : 
      57             : static void mdct_spectrum_fine_gain_dec( Decoder_State *st, 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 );
      58             : 
      59             : static float band_energy_dequant( Decoder_State *st, float band_energy[], const int16_t bands, const Word32 L_qint, const Word16 eref_fx, const int16_t is_transient );
      60             : 
      61             : static void spt_shorten_domain_set_dec( Decoder_State *st, const int16_t p2a_flags[], const int16_t new_band_start[], const int16_t new_band_end[], const int16_t new_band_width[], const int16_t bands, int16_t band_start[], int16_t band_end[], int16_t band_width[], int16_t *bit_budget );
      62             : 
      63             : 
      64             : /*-------------------------------------------------------------------*
      65             :  * hq_lr_dec()
      66             :  *
      67             :  * HQ low rate decoding routine
      68             :  *-------------------------------------------------------------------*/
      69             : 
      70        2044 : void hq_lr_dec(
      71             :     Decoder_State *st,         /* i/o: decoder state structure         */
      72             :     float yout[],              /* o  : transform-domain output coefs.  */
      73             :     const int16_t inner_frame, /* i  : inner frame length              */
      74             :     int16_t num_bits,          /* i  : number of available bits        */
      75             :     int16_t *is_transient      /* o  : transient flag                  */
      76             : )
      77             : {
      78             :     int16_t i, k1, pbits, p2a_flags[BANDS_MAX], bit_budget, bands, length, gqlevs, gqbits, Ngq, p2a_bands, ni_seed;
      79             :     int16_t band_start[BANDS_MAX], band_end[BANDS_MAX], band_width[BANDS_MAX];
      80             :     int16_t k_sort[BANDS_MAX], npulses[BANDS_MAX];
      81             :     int32_t inp_vector[L_FRAME48k];
      82             :     float ni_coef, ni_pd_th, pd_thresh, ld_slope;
      83             :     float ebits, Rk[BANDS_MAX], band_energy[BANDS_MAX], y2[L_FRAME48k], p2a_th;
      84             :     Word32 Rk_fx[BANDS_MAX];
      85             :     Word32 L_qint;              /* Q29 */
      86             :     Word16 eref_fx;             /* Q10 */
      87             :     Word16 bit_alloc_weight_fx; /* Q13 */
      88             :     float y2_ni[L_FRAME48k], y2_org[L_FRAME48k];
      89        2044 :     int16_t hqswb_clas = 0;
      90        2044 :     int16_t lowlength, highlength, har_bands = 0;
      91             :     float m[L_FRAME32k];
      92        2044 :     float Ep[BANDS_MAX], enerH = 0.0f, enerL = 0.0f;
      93        2044 :     int16_t lowband, highband, bw_low = 0, bw_high = 20;
      94             :     float Ep_tmp[BANDS_MAX];
      95             :     float band_energy_tmp[BANDS_MAX];
      96             :     int16_t last_bitalloc_max_band[2];
      97             :     int32_t bwe_br;
      98             :     int16_t trans_bit, p2a_flags_tmp[BANDS_MAX];
      99        2044 :     int16_t adjustFlag = 0;
     100             :     int16_t prev_SWB_peak_pos_tmp[SPT_SHORTEN_SBNUM];
     101             :     int16_t j, k;
     102             :     int16_t flag_spt;
     103             :     int16_t org_band_start[SPT_SHORTEN_SBNUM];
     104             :     int16_t org_band_end[SPT_SHORTEN_SBNUM];
     105             :     int16_t org_band_width[SPT_SHORTEN_SBNUM];
     106             :     int16_t new_band_start[SPT_SHORTEN_SBNUM];
     107             :     int16_t new_band_end[SPT_SHORTEN_SBNUM];
     108             :     int16_t new_band_width[SPT_SHORTEN_SBNUM];
     109             :     Word32 L_tmp, L_tmp2, L_tmp3;
     110             :     Word16 exp, tmp, exp2, tmp1, tmp2, tmp3, alpha_fx, frac1;
     111             :     Word32 enerH_fx;
     112             :     Word32 enerL_fx;
     113             :     Word32 Ep_fx[BANDS_MAX];
     114             :     Word32 Ep_avrg_fx, Ep_vari_fx;
     115             :     Word32 Ep_avrgL_fx;
     116             :     Word32 Ep_peak_fx;
     117             :     Word32 Ep_tmp_fx[BANDS_MAX];
     118             :     Word16 gama_fx; /*Q15 0.85f; */
     119             :     Word16 beta_fx; /*Q14 1.05f; */
     120             :     Word32 L_band_energy[BANDS_MAX], L_band_energy_tmp[BANDS_MAX];
     121             :     UWord16 lo;
     122             :     Word16 Q_band_energy;
     123             : #ifdef BASOP_NOGLOB
     124             :     Flag Overflow;
     125             : #endif /* BASOP_NOGLOB */
     126             : 
     127        2044 :     HQ_DEC_HANDLE hHQ_core = st->hHQ_core;
     128             : 
     129        2044 :     set_s( last_bitalloc_max_band, 0, 2 );
     130        2044 :     set_f( y2, 0.0f, L_FRAME48k );
     131        2044 :     set_l( inp_vector, 0, inner_frame );
     132        2044 :     flag_spt = 0;
     133        2044 :     set_s( prev_SWB_peak_pos_tmp, 0, SPT_SHORTEN_SBNUM );
     134        2044 :     bwe_br = st->core_brate;
     135             : 
     136             : 
     137        2044 :     if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     138             :     {
     139        1619 :         hqswb_clas = get_next_indice( st, 2 );
     140        1619 :         num_bits -= 2;
     141             : 
     142        1619 :         *is_transient = 0;
     143        1619 :         if ( hqswb_clas == HQ_TRANSIENT )
     144             :         {
     145          64 :             *is_transient = 1;
     146             :         }
     147             :     }
     148             :     else
     149             :     {
     150             :         /* decode transient flag */
     151         425 :         *is_transient = get_next_indice( st, 1 );
     152         425 :         num_bits--;
     153             :     }
     154             : 
     155             : 
     156             :     /* Configure decoder for different bandwidths, bitrates, etc. */
     157        2044 :     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 );
     158             : 
     159        2044 :     highlength = band_end[bands - 1];
     160        2044 :     har_bands = bands;
     161             : 
     162        2044 :     if ( st->bwidth == SWB && *is_transient == 0 && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     163             :     {
     164             :         /* reserve bits for HQ_NORMAL and HQ_HARMONIC */
     165        1555 :         if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
     166             :         {
     167        1555 :             num_bits -= get_usebit_npswb( hqswb_clas );
     168             :         }
     169             : 
     170        1555 :         if ( hqswb_clas == HQ_NORMAL )
     171             :         {
     172        1507 :             flag_spt = 1;
     173             :         }
     174             :     }
     175             : 
     176        2044 :     if ( ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) && st->bwidth == SWB )
     177             :     {
     178        1619 :         if ( hHQ_core->prev_hqswb_clas != HQ_NORMAL )
     179             :         {
     180         111 :             j = 0;
     181         555 :             for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
     182             :             {
     183         444 :                 hHQ_core->prev_SWB_peak_pos[j] = 0;
     184         444 :                 j++;
     185             :             }
     186             :         }
     187             :     }
     188             : 
     189             :     /* Spectral energy calculation/quantization */
     190        2044 :     ebits = band_energy_dequant( st, band_energy, bands, L_qint, eref_fx, *is_transient );
     191             : 
     192             :     /* simple check: band_energy is too large, Abnormal Situation of bit errors */
     193       44739 :     for ( k = 0; k < bands; k++ )
     194             :     {
     195             :         /* Max: 45.0(737279,Q14) at 32kHz, highest band, Min: -6.600037(-108135,Q14) at 8kHz(NB),8kbps, is_transient (-6.7f is safty-threshold) */
     196       42695 :         if ( band_energy[k] > 45.0f || band_energy[k] < -6.7f )
     197             :         {
     198           0 :             st->BER_detect = 1;
     199           0 :             set_f( yout, 0, inner_frame );
     200           0 :             return;
     201             :         }
     202             :     }
     203             : 
     204       44739 :     for ( i = 0; i < bands; i++ )
     205             :     {
     206       42695 :         L_band_energy[i] = (Word32) ( band_energy[i] * pow( 2.0f, SWB_BWE_LR_Qbe ) );
     207             :     }
     208             : 
     209             :     /* First pass bit budget for TCQ of spectral band information */
     210        2044 :     gqbits = (int16_t) log2_f( (float) gqlevs );
     211        2044 :     bit_budget = num_bits - (int16_t) ceil( ebits ) - Ngq * gqbits;
     212             : 
     213        2044 :     pbits = 0;
     214        2044 :     if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     215             :     {
     216        1619 :         if ( hqswb_clas == HQ_HARMONIC )
     217             :         {
     218          48 :             set_s( p2a_flags, 1, har_bands );
     219             :         }
     220             :         else
     221             :         {
     222             :             /* High band tonality detector based on per band peak-to-average ratio */
     223        1571 :             pbits = p2a_threshold_dequant( st, p2a_flags, bands, p2a_bands );
     224        1571 :             bit_budget -= pbits;
     225             : 
     226        1571 :             if ( hqswb_clas == HQ_NORMAL )
     227             :             {
     228        1507 :                 return_bits_normal2( &bit_budget, p2a_flags, bands, bits_lagIndices_modeNormal );
     229             :             }
     230             :         }
     231             :     }
     232             :     else
     233             :     {
     234             :         /* High band tonality detector based on per band peak-to-average ratio */
     235         425 :         pbits = p2a_threshold_dequant( st, p2a_flags, bands, p2a_bands );
     236         425 :         bit_budget -= pbits;
     237             :     }
     238             : 
     239        2044 :     if ( flag_spt == 1 )
     240             :     {
     241             :         /* initialize the desired parameters for SPT */
     242        1507 :         spt_shorten_domain_band_save( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
     243        1507 :         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 );
     244        1507 :         spt_shorten_domain_set_dec( st, p2a_flags, new_band_start, new_band_end, new_band_width, bands, band_start, band_end, band_width, &bit_budget );
     245             :     }
     246             : 
     247             :     /* safety check in case of bit errors */
     248        2044 :     if ( bit_budget < 2 )
     249             :     {
     250           0 :         st->BER_detect = 1;
     251           0 :         set_f( yout, 0, inner_frame );
     252           0 :         return;
     253             :     }
     254             : 
     255             : #define WMC_TOOL_SKIP
     256             :     /* Estimate number of bits per sub-band */
     257        2044 :     Q_band_energy = SWB_BWE_LR_Qbe;
     258       44739 :     FOR( i = 0; i < bands; i++ )
     259             :     {
     260       42695 :         L_tmp = L_shl( L_band_energy[i], sub( 16, Q_band_energy ) ); /*Q16 */
     261             : 
     262       42695 :         frac1 = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
     263       42695 :         L_tmp = Pow2( 30, frac1 );
     264       42695 :         exp = sub( exp, 30 );
     265             : #ifdef BASOP_NOGLOB
     266       42695 :         Ep_fx[i] = L_shl_o( L_tmp, s_max( sub( exp, 6 ), -31 ), &Overflow ); /* Q -6 */
     267             : #else
     268             :         Ep_fx[i] = L_shl( L_tmp, s_max( sub( exp, 6 ), -31 ) );          /* Q -6 */
     269             : #endif
     270       42695 :         Ep[i] = (float) ( Ep_fx[i] / pow( 2.0, -6 ) );
     271             :     }
     272             : 
     273       44739 :     FOR( i = 0; i < bands; i++ )
     274             :     {
     275       42695 :         L_tmp2 = Ep_fx[i];
     276       42695 :         L_tmp = L_max( 1, L_tmp2 );
     277       42695 :         exp = norm_l( L_tmp );
     278       42695 :         tmp = extract_h( L_shl( L_tmp, exp ) );
     279             : 
     280       42695 :         L_tmp3 = (Word32) band_width[i];
     281       42695 :         exp2 = norm_l( L_tmp3 );
     282       42695 :         tmp2 = extract_h( L_shl( L_tmp3, exp2 ) );
     283             : 
     284       42695 :         exp2 = sub( exp, exp2 ); /* Denormalize and substract */
     285             : 
     286       42695 :         tmp3 = sub( tmp2, tmp );
     287       42695 :         IF( tmp3 > 0 )
     288             :         {
     289       18406 :             tmp2 = shr( tmp2, 1 );
     290             :         }
     291       42695 :         IF( tmp3 > 0 )
     292             :         {
     293       18406 :             exp2 = add( exp2, 1 );
     294             :         }
     295       42695 :         tmp = div_s( tmp2, tmp );
     296       42695 :         L_tmp = L_deposit_h( tmp );
     297       42695 :         L_tmp = Isqrt_lc1( L_tmp, &exp2 );
     298       42695 :         move32();                                       /*Q(31-exp2) */
     299       42695 :         Ep_tmp_fx[i] = L_shr( L_tmp, sub( 15, exp2 ) ); /*Q13 */
     300       42695 :         Ep_tmp[i] = (float) ( Ep_tmp_fx[i] / pow( 2.0, 13 ) );
     301             :     }
     302             : #undef WMC_TOOL_SKIP
     303             : 
     304        2044 :     if ( *is_transient == 0 && inner_frame == L_FRAME8k && st->core_brate <= ACELP_13k20 )
     305             :     {
     306             :         /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
     307         331 :         j = 0;
     308         993 :         for ( i = 0; i < 2; i++ )
     309             :         {
     310         662 :             last_bitalloc_max_band[i] = get_next_indice( st, 1 );
     311             :         }
     312             : 
     313             : #define WMC_TOOL_SKIP
     314         331 :         lowband = 6;
     315         331 :         move16();
     316         331 :         trans_bit = 2;
     317         331 :         move16();
     318         331 :         bit_budget = sub( bit_budget, trans_bit );
     319         331 :         gama_fx = 27852; /*Q15 0.85f;*/
     320         331 :         beta_fx = 17203;
     321         331 :         move16(); /*Q14 1.05f; */
     322         331 :         set_s( &p2a_flags_tmp[bands - trans_bit], 0, 2 );
     323             : 
     324         331 :         IF( st->core_brate == ACELP_13k20 )
     325             :         {
     326          67 :             beta_fx = 13107;
     327          67 :             move16(); /*14 1.25f; */
     328          67 :             gama_fx = 31130;
     329          67 :             move16(); /*0.95f; */
     330          67 :             mvs2s( &p2a_flags[sub( bands, trans_bit )], &p2a_flags_tmp[sub( bands, trans_bit )], trans_bit );
     331             :         }
     332             : 
     333             :         /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
     334         331 :         Ep_vari_fx = 0;
     335         331 :         move32();
     336         331 :         Ep_avrg_fx = 0;
     337         331 :         move32();
     338         331 :         Ep_avrgL_fx = 0;
     339         331 :         move32();
     340         331 :         Ep_peak_fx = 0;
     341         331 :         move32();
     342        4902 :         FOR( i = 0; i < bands; i++ )
     343             :         {
     344        4571 :             IF( sub( i, lowband ) >= 0 )
     345             :             {
     346        2585 :                 Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
     347        2585 :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] );                                           /*Q15 */
     348             :             }
     349             :             ELSE
     350             :             {
     351        1986 :                 Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
     352        1986 :                 IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
     353             :                 {
     354         767 :                     Ep_peak_fx = Ep_tmp_fx[i];
     355         767 :                     move32(); /*Q15 */
     356             :                 }
     357             :             }
     358             :         }
     359             :         /* modify the last p2a_bands subbands band_energies */
     360         331 :         k = bands;
     361         331 :         mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
     362         331 :         Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
     363         331 :         Mpy_32_16_ss( Ep_peak_fx, shl( sub( bands, lowband ), 9 ), &L_tmp2, &lo );
     364         331 :         Mpy_32_16_ss( Ep_avrg_fx, 1126, &L_tmp3, &lo );
     365             : 
     366         331 :         IF( ( ( L_sub( L_tmp, L_shr( Ep_avrgL_fx, 1 ) ) < 0 && st->core_brate == ACELP_13k20 ) || st->core_brate < ACELP_13k20 ) &&
     367             :             L_sub( L_tmp2, L_tmp3 ) < 0 && L_sub( L_tmp2, L_shr( Ep_avrg_fx, 7 ) ) > 0 )
     368             :         {
     369         120 :             FOR( i = lowband; i < bands; i++ )
     370             :             {
     371         105 :                 Mpy_32_16_ss( Ep_avrg_fx, 24576, &L_tmp, &lo );
     372         105 :                 IF( L_sub( L_shr( Ep_tmp_fx[i], 1 ), L_tmp ) < 0 )
     373             :                 {
     374         105 :                     Mpy_32_16_ss( Ep_peak_fx, sub( bands, lowband ), &L_tmp, &lo );
     375         105 :                     tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
     376         105 :                     IF( tmp != 0 )
     377             :                     {
     378         105 :                         exp = norm_s( tmp );
     379         105 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     380         105 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     381         105 :                         exp = sub( 29, exp );
     382             :                     }
     383             :                     ELSE
     384             :                     {
     385           0 :                         tmp = 0x7fff;
     386           0 :                         move16();
     387           0 :                         exp = 0;
     388           0 :                         move16();
     389             :                     }
     390         105 :                     Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
     391         105 :                     L_tmp = L_shl( L_tmp, sub( 13, exp ) );                       /*Q(13+exp-15 +13-exp +4 = 15) */
     392         105 :                     L_tmp2 = L_add( L_tmp, 13107 );                               /*15 */
     393         105 :                     tmp2 = extract_l( L_min( L_max( L_tmp2, 16384 ), gama_fx ) ); /*15 = 15 */
     394         105 :                     Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_band_energy_tmp[i], &lo );
     395             :                 }
     396             :             }
     397             :         }
     398             :         ELSE
     399             :         {
     400         948 :             FOR( i = sub( bands, trans_bit ); i < bands; i++ )
     401             :             {
     402         632 :                 alpha_fx = 16384;
     403         632 :                 move16(); /*Q14 */
     404         632 :                 IF( sub( p2a_flags_tmp[i], 1 ) == 0 )
     405             :                 {
     406           0 :                     Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
     407           0 :                     tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-4 */
     408           0 :                     IF( tmp != 0 )
     409             :                     {
     410           0 :                         exp = norm_s( tmp );
     411           0 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     412           0 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     413           0 :                         exp = sub( 29, exp );
     414             :                     }
     415             :                     ELSE
     416             :                     {
     417           0 :                         tmp = 0x7fff;
     418           0 :                         move16();
     419           0 :                         exp = 0;
     420           0 :                         move16();
     421             :                     }
     422           0 :                     Mpy_32_16_ss( Ep_vari_fx, 3277, &L_tmp, &lo );
     423           0 :                     Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     424           0 :                     L_tmp = L_shl( L_tmp, sub( 12, exp ) ); /*Q(13+exp-15 +12-exp +4 = 14) */
     425             : 
     426           0 :                     tmp2 = extract_h( Ep_avrg_fx ); /*Q13-16=-3 */
     427           0 :                     IF( tmp2 != 0 )
     428             :                     {
     429           0 :                         exp = norm_s( tmp2 );
     430           0 :                         tmp2 = shl( tmp2, exp );     /*Q(exp) */
     431           0 :                         tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
     432           0 :                         exp = sub( 29, exp );
     433             :                     }
     434             :                     ELSE
     435             :                     {
     436             :                         /*when the divisor is zero, happens rarely*/
     437           0 :                         tmp2 = 0x7fff;
     438           0 :                         move16();
     439           0 :                         exp = 0;
     440           0 :                         move16();
     441             :                     }
     442           0 :                     Mpy_32_16_ss( Ep_vari_fx, 6554, &L_tmp2, &lo );
     443           0 :                     Mpy_32_16_ss( L_tmp2, tmp2, &L_tmp2, &lo );
     444           0 :                     L_tmp2 = L_shl( L_tmp2, sub( 13, exp ) ); /*Q(13+exp-15 +13-exp +3 = 14) */
     445           0 :                     L_tmp = L_min( L_tmp, L_tmp2 );           /*14 */
     446           0 :                     tmp = extract_l( L_min( L_tmp, 13107 ) ); /*14 */
     447           0 :                     alpha_fx = add( 16384, tmp );
     448             :                 }
     449         632 :                 IF( sub( last_bitalloc_max_band[j++], 1 ) == 0 )
     450             :                 {
     451         180 :                     Mpy_32_16_ss( Ep_tmp_fx[i], sub( bands, lowband ), &L_tmp, &lo );
     452         180 :                     tmp = extract_h( L_shl( L_tmp, 14 ) ); /*Q-2 */
     453         180 :                     IF( tmp != 0 )
     454             :                     {
     455         179 :                         exp = norm_s( tmp );
     456         179 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     457         179 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     458         179 :                         exp = sub( 29, exp );
     459             :                     }
     460             :                     ELSE
     461             :                     {
     462           1 :                         tmp = 0x7fff;
     463           1 :                         move16();
     464           1 :                         exp = 0;
     465           1 :                         move16();
     466             :                     }
     467         180 :                     Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
     468             : #ifndef BASOP_NOGLOB
     469             :                     L_tmp = L_shl( L_tmp, sub( 14, exp ) );     /*Q(13+exp-15 +14-exp+2 = 14) */
     470             :                     L_tmp = L_max( L_tmp, 16384 );              /*14 */
     471             :                     tmp = extract_l( L_min( L_tmp, beta_fx ) ); /*14 */
     472             :                     alpha_fx = shl( mult( alpha_fx, tmp ), 1 ); /*14+14-15 +1=14 */
     473             : #else                                                           /* BASOP_NOGLOB */
     474         180 :                     L_tmp = L_shl_o( L_tmp, sub( 14, exp ), &Overflow ); /*Q(13+exp-15 +14-exp+2 = 14) */
     475         180 :                     L_tmp = L_max( L_tmp, 16384 );                       /*14 */
     476         180 :                     tmp = extract_l( L_min( L_tmp, beta_fx ) );          /*14 */
     477         180 :                     alpha_fx = shl( mult( alpha_fx, tmp ), 1 );          /*14+14-15 +1=14 */
     478             : #endif                                                          /* BASOP_NOGLOB */
     479             :                 }
     480             :                 ELSE
     481             :                 {
     482         452 :                     tmp2 = extract_h( Ep_avrg_fx ); /*13 -16 =-3 */
     483         452 :                     IF( tmp2 != 0 )
     484             :                     {
     485         452 :                         exp = norm_s( tmp2 );
     486         452 :                         tmp2 = shl( tmp2, exp );     /*Q(exp) */
     487         452 :                         tmp2 = div_s( 16384, tmp2 ); /*Q(15+14-exp=29-exp) */
     488         452 :                         exp = sub( 29, exp );
     489             :                     }
     490             :                     ELSE
     491             :                     {
     492             :                         /*when the divisor is zero, happens rarely*/
     493           0 :                         tmp2 = 0x7fff;
     494           0 :                         move16();
     495           0 :                         exp = 0;
     496           0 :                         move16();
     497             :                     }
     498         452 :                     Mpy_32_16_ss( Ep_tmp_fx[i], tmp2, &L_tmp, &lo );
     499         452 :                     L_tmp = L_shl( L_tmp, sub( 19, exp ) ); /*Q(13+exp-15 +19-exp +3 = 20) */
     500         452 :                     Mpy_32_16_ss( L_tmp, shl( sub( bands, lowband ), 9 ), &L_tmp, &lo );
     501         452 :                     L_tmp = L_max( L_tmp, 13926 );               /*14 */
     502         452 :                     tmp2 = extract_l( L_min( L_tmp, 16384 ) );   /*14 */
     503         452 :                     alpha_fx = shl( mult( alpha_fx, tmp2 ), 1 ); /*14+14-15+1 =14 */
     504             :                 }
     505         632 :                 Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
     506         632 :                 L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q(Q_band_energy+14-15 +1= Q_band_energy) */
     507             :             }
     508             :         }
     509         331 :         lowband = 3;
     510         331 :         move16();
     511         331 :         Ep_avrg_fx = 0;
     512         331 :         move32();
     513         331 :         Ep_avrgL_fx = 0;
     514         331 :         move32();
     515         331 :         Ep_peak_fx = 0;
     516         331 :         move32();
     517        4902 :         FOR( i = 0; i < bands; i++ )
     518             :         {
     519        4571 :             IF( sub( i, lowband ) >= 0 )
     520             :             {
     521        3578 :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
     522             :             }
     523             :             ELSE
     524             :             {
     525         993 :                 Ep_avrgL_fx = L_add( Ep_avrgL_fx, L_shr( Ep_tmp_fx[i], 1 ) ); /*Q12 */
     526         993 :                 IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
     527             :                 {
     528         703 :                     Ep_peak_fx = Ep_tmp_fx[i];
     529         703 :                     move32(); /*Q13 */
     530             :                 }
     531             :             }
     532             :         }
     533         331 :         Mpy_32_16_ss( Ep_peak_fx, 28262, &L_tmp, &lo );
     534         331 :         Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp2, &lo );
     535         331 :         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 )
     536             :         {
     537          75 :             adjustFlag = 1;
     538          75 :             move16();
     539         300 :             FOR( i = 0; i < lowband; i++ )
     540             :             {
     541         225 :                 tmp = extract_h( Ep_avrgL_fx ); /*Q-4 */
     542         225 :                 IF( tmp != 0 )
     543             :                 {
     544         225 :                     exp = norm_s( tmp );
     545         225 :                     tmp = shl( tmp, exp );     /*Q(exp) */
     546         225 :                     tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     547         225 :                     exp = sub( 29, exp );
     548             :                 }
     549             :                 ELSE
     550             :                 {
     551           0 :                     tmp = 0x7fff;
     552           0 :                     move16();
     553           0 :                     exp = 0;
     554           0 :                     move16();
     555             :                 }
     556         225 :                 Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
     557         225 :                 Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
     558         225 :                 Mpy_32_16_ss( L_tmp, 18842, &L_tmp, &lo );
     559             : #ifndef BASOP_NOGLOB
     560             :                 L_tmp = L_shl( L_tmp, sub( 27, exp ) );    /*Q14 0.5 */
     561             :                 tmp2 = extract_l( L_min( L_tmp, 19661 ) ); /*14 */
     562             : #else                                                      /* BASOP_NOGLOB */
     563         225 :                 L_tmp = L_shl_o( L_tmp, sub( 27, exp ), &Overflow );     /*Q14 0.5 */
     564         225 :                 tmp2 = extract_l( L_min( L_tmp, 19661 ) );               /*14 */
     565             : #endif                                                     /* BASOP_NOGLOB */
     566         225 :                 Mpy_32_16_ss( L_band_energy_tmp[i], tmp2, &L_tmp, &lo );
     567             : #ifndef BASOP_NOGLOB
     568             :                 L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy  */
     569             : #else                                                     /* BASOP_NOGLOB */
     570         225 :                 L_band_energy_tmp[i] = L_shl_o( L_tmp, 1, &Overflow );   /*Q_band_energy  */
     571             : #endif                                                    /* BASOP_NOGLOB */
     572             :             }
     573             :         }
     574             : #undef WMC_TOOL_SKIP
     575             : 
     576        4902 :         for ( i = 0; i < bands; i++ )
     577             :         {
     578        4571 :             band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
     579             :         }
     580             : 
     581         331 :         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 );
     582             :     }
     583        1713 :     else if ( *is_transient == 0 && inner_frame == L_FRAME16k )
     584             :     {
     585             : #define WMC_TOOL_SKIP
     586          38 :         bit_budget = sub( bit_budget, 2 ); /* bits in high bands to indicate the last 2 subbands is allocated bits or not */
     587             :         /* decode the last p2a_bands-1 subbands bit-allocation index of the previous frame */
     588         114 :         for ( i = 0; i < 2; i++ )
     589             :         {
     590          76 :             last_bitalloc_max_band[i] = get_next_indice( st, 1 );
     591             :         }
     592         750 :         FOR( i = 0; i < bands; i++ )
     593             :         {
     594             : #ifndef BASOP_NOGLOB
     595             :             Ep_tmp_fx[i] = L_shl( Ep_tmp_fx[i], 2 );
     596             : #else  /* BASOP_NOGLOB */
     597         712 :             Ep_tmp_fx[i] = L_shl_o( Ep_tmp_fx[i], 2, &Overflow );
     598             : #endif /* BASOP_NOGLOB */
     599             :         }
     600          38 :         IF( st->core_brate == ACELP_13k20 )
     601             :         {
     602          24 :             lowband = 8;
     603          24 :             move16();
     604          24 :             highband = 15;
     605          24 :             move16();
     606          24 :             bw_low = sub( band_start[highband], band_start[lowband] );
     607          24 :             bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
     608             :         }
     609             :         ELSE
     610             :         {
     611          14 :             lowband = 8;
     612          14 :             move16();
     613          14 :             highband = 16;
     614          14 :             move16();
     615          14 :             bw_low = sub( band_start[highband], band_start[lowband] );
     616          14 :             bw_high = sub( add( band_end[sub( bands, 1 )], 1 ), band_start[highband] );
     617             :         }
     618             :         /* calculate the the low band/high band energy and the variance/avrage of the envelopes */
     619          38 :         enerL_fx = 0;
     620          38 :         move32();
     621          38 :         enerH_fx = 0;
     622          38 :         move32();
     623          38 :         Ep_vari_fx = 0;
     624          38 :         move32();
     625          38 :         Ep_avrg_fx = 0;
     626          38 :         move32();
     627         750 :         FOR( i = 0; i < bands; i++ )
     628             :         {
     629         712 :             IF( sub( i, lowband ) >= 0 && add( sub( i, bands ), p2a_bands ) < 0 )
     630             :             {
     631             : #ifndef BASOP_NOGLOB
     632             :                 Ep_vari_fx = L_add( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ) ); /*Q15 */
     633             :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] );                                           /*Q15 */
     634             : #else                                                                                                     /* BASOP_NOGLOB */
     635         294 :                 Ep_vari_fx = L_add_o( Ep_vari_fx, L_abs( L_sub( Ep_tmp_fx[i], Ep_tmp_fx[sub( i, 1 )] ) ), &Overflow ); /*Q15 */
     636         294 :                 Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow );                                           /*Q15 */
     637             : #endif                                                                                                    /* BASOP_NOGLOB */
     638             :             }
     639             : 
     640         712 :             IF( sub( i, highband ) >= 0 )
     641             :             {
     642             :                 /* safety check in case of bit errors */
     643         128 :                 if ( Ep_fx[i] > 536788991 /* max(Q30) */ )
     644             :                 {
     645           0 :                     st->BER_detect = 1;
     646           0 :                     set_f( yout, 0, inner_frame );
     647           0 :                     return;
     648             :                 }
     649             : 
     650         128 :                 enerH_fx = L_add( enerH_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
     651             :             }
     652         584 :             ELSE IF( sub( i, lowband ) >= 0 )
     653             :             {
     654             :                 /* safety check in case of bit errors */
     655         280 :                 if ( Ep_fx[i] > 536788991 /* max(Q30) */ )
     656             :                 {
     657           0 :                     st->BER_detect = 1;
     658           0 :                     set_f( yout, 0, inner_frame );
     659           0 :                     return;
     660             :                 }
     661             : 
     662         280 :                 enerL_fx = L_add( enerL_fx, L_shl( Ep_fx[i], 2 ) ); /*Q0 */
     663             :             }
     664             :         }
     665             : 
     666          38 :         enerL = (float) ( enerL_fx / pow( 2.0, -4 ) );
     667          38 :         enerH = (float) ( enerH_fx / pow( 2.0, -4 ) );
     668             :         /* modify the last p2a_bands subbands band_energies */
     669          38 :         k = bands;
     670          38 :         mvl2l( L_band_energy, L_band_energy_tmp, k ); /*Q_band_energy */
     671             : 
     672          38 :         L_tmp = L_max( enerH_fx, enerL_fx );
     673          38 :         tmp = s_max( bw_low, bw_high );
     674          38 :         i = norm_l( L_tmp );
     675          38 :         j = norm_s( tmp );
     676          38 :         Mpy_32_16_ss( L_shl( enerH_fx, i ), shl( bw_low, j ), &L_tmp, &lo );
     677          38 :         Mpy_32_16_ss( L_shl( enerL_fx, i ), shl( bw_high, j ), &L_tmp2, &lo );
     678          38 :         L_tmp2 = L_sub( L_tmp, L_tmp2 );
     679             : 
     680         152 :         FOR( i = sub( bands, p2a_bands ); i < bands; i++ )
     681             :         {
     682         114 :             IF( sub( p2a_flags[i], 1 ) == 0 || L_tmp2 > 0 )
     683             :             {
     684          19 :                 tmp = sub( bands, p2a_bands );
     685          19 :                 tmp = sub( tmp, lowband ); /*Q0 */
     686             : 
     687             : #ifndef BASOP_NOGLOB
     688             :                 tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
     689             : #else
     690          19 :                 tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) );                                               /*Q0 */
     691             : #endif
     692          19 :                 IF( tmp1 != 0 )
     693             :                 {
     694          19 :                     exp = norm_s( tmp1 );
     695          19 :                     tmp1 = shl( tmp1, exp );     /*Q(exp) */
     696          19 :                     tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp = 29-exp) */
     697          19 :                     exp = sub( 29, exp );
     698             :                 }
     699             :                 ELSE
     700             :                 {
     701             :                     /*when the divisor is zero, happens rarely*/
     702           0 :                     tmp1 = 0x7fff;
     703           0 :                     move16();
     704           0 :                     exp = 0;
     705           0 :                     move16();
     706             :                 }
     707          19 :                 Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
     708          19 :                 Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     709          19 :                 Mpy_32_16_ss( L_tmp, 16384, &L_tmp, &lo );
     710          19 :                 L_tmp = L_shl( L_tmp, sub( 32, exp ) );  /*Q15 */
     711          19 :                 tmp = extract_l( L_min( L_tmp, 6554 ) ); /*Q15 */
     712          19 :                 Mpy_32_16_ss( Ep_vari_fx, tmp1, &L_tmp, &lo );
     713          19 :                 Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     714          19 :                 L_tmp = L_shl( L_tmp, sub( 15, exp ) );               /*Q15 */
     715          19 :                 tmp = extract_l( L_shr( L_min( L_tmp, 13107 ), 1 ) ); /*Q14 */
     716          19 :                 alpha_fx = add( tmp, 16384 );                         /*Q14 */
     717             :             }
     718             :             ELSE
     719             :             {
     720          95 :                 alpha_fx = 16384;
     721          95 :                 move16(); /*Q14 */
     722             :             }
     723             : 
     724         114 :             IF( add( sub( i, bands ), p2a_bands ) > 0 )
     725             :             {
     726          76 :                 tmp = sub( bands, p2a_bands );
     727          76 :                 IF( sub( last_bitalloc_max_band[sub( i, add( tmp, 1 ) )], 1 ) == 0 )
     728             :                 {
     729          17 :                     tmp = sub( tmp, lowband );
     730          17 :                     Mpy_32_16_ss( Ep_tmp_fx[i], tmp, &L_tmp, &lo );
     731             : #ifndef BASOP_NOGLOB
     732             :                     tmp = extract_h( L_shl( L_tmp, 16 ) ); /*Q0 */
     733             : #else
     734          17 :                     tmp = extract_h( L_shl_o( L_tmp, 16, &Overflow ) );                                                /*Q0 */
     735             : #endif
     736          17 :                     IF( tmp != 0 )
     737             :                     {
     738          17 :                         exp = norm_s( tmp );
     739          17 :                         tmp = shl( tmp, exp );     /*Q(exp) */
     740          17 :                         tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     741          17 :                         exp = sub( 29, exp );
     742             :                     }
     743             :                     ELSE
     744             :                     {
     745           0 :                         tmp = 0x7fff;
     746           0 :                         move16();
     747           0 :                         exp = 0;
     748           0 :                         move16();
     749             :                     }
     750          17 :                     Mpy_32_16_ss( Ep_avrg_fx, tmp, &L_tmp, &lo );
     751          17 :                     L_tmp = L_shl( L_tmp, sub( 14, exp ) );                   /*Q14 */
     752          17 :                     tmp = extract_l( L_min( L_max( L_tmp, 16384 ), 20480 ) ); /*Q14 */
     753          17 :                     L_tmp = L_mult( alpha_fx, tmp );                          /*Q(14+14+1=29) */
     754          17 :                     alpha_fx = extract_l( L_shr( L_tmp, 15 ) );               /*Q14                    */
     755             :                 }
     756             :                 ELSE
     757             :                 {
     758          59 :                     tmp = sub( tmp, lowband );
     759             : 
     760             : #ifndef BASOP_NOGLOB
     761             :                     tmp1 = extract_h( L_shl( Ep_avrg_fx, 1 ) ); /*Q0 */
     762             : #else
     763          59 :                     tmp1 = extract_h( L_shl_o( Ep_avrg_fx, 1, &Overflow ) );                                           /*Q0 */
     764             : #endif
     765          59 :                     IF( tmp1 != 0 )
     766             :                     {
     767          59 :                         exp = norm_s( tmp1 );
     768          59 :                         tmp1 = shl( tmp1, exp );     /*Q(exp) */
     769          59 :                         tmp1 = div_s( 16384, tmp1 ); /*Q(15+14-exp=29-exp) */
     770          59 :                         exp = sub( 29, exp );
     771             :                     }
     772             :                     ELSE
     773             :                     {
     774             :                         /*when the divisor is zero, happens rarely*/
     775           0 :                         tmp1 = 0x7fff;
     776           0 :                         move16();
     777           0 :                         exp = 0;
     778           0 :                         move16();
     779             :                     }
     780          59 :                     Mpy_32_16_ss( Ep_tmp_fx[i], tmp1, &L_tmp, &lo );
     781          59 :                     Mpy_32_16_ss( L_tmp, tmp, &L_tmp, &lo );
     782          59 :                     L_tmp = L_shl( L_tmp, sub( 29, exp ) );                   /*Q14 */
     783          59 :                     tmp = extract_l( L_min( L_max( L_tmp, 13926 ), 16384 ) ); /*Q14 */
     784          59 :                     L_tmp = L_mult( alpha_fx, tmp );                          /*Q(14+14+1=29) */
     785          59 :                     alpha_fx = extract_l( L_shr( L_tmp, 15 ) );               /*Q14  */
     786             :                 }
     787             :             }
     788         114 :             Mpy_32_16_ss( L_band_energy_tmp[i], alpha_fx, &L_tmp, &lo );
     789         114 :             L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q Q_band_energy */
     790             :         }
     791          38 :         lowband = 6;
     792          38 :         move16();
     793          38 :         Ep_avrg_fx = 0;
     794          38 :         move32();
     795          38 :         Ep_avrgL_fx = 0;
     796          38 :         move32();
     797          38 :         Ep_peak_fx = 0;
     798          38 :         move32();
     799         750 :         FOR( i = 0; i < bands; i++ )
     800             :         {
     801         712 :             IF( sub( i, lowband ) >= 0 )
     802             :             {
     803             : #ifndef BASOP_NOGLOB
     804             :                 Ep_avrg_fx = L_add( Ep_avrg_fx, Ep_tmp_fx[i] ); /*Q15 */
     805             : #else                                                           /* BASOP_NOGLOB */
     806         484 :                 Ep_avrg_fx = L_add_o( Ep_avrg_fx, Ep_tmp_fx[i], &Overflow );                                           /*Q15 */
     807             : #endif                                                          /* BASOP_NOGLOB */
     808             :             }
     809             :             ELSE
     810             :             {
     811             : #ifndef BASOP_NOGLOB
     812             :                 Ep_avrgL_fx = L_add( Ep_avrgL_fx, Ep_tmp_fx[i] ); /*Q15 */
     813             : #else                                                             /* BASOP_NOGLOB */
     814         228 :                 Ep_avrgL_fx = L_add_o( Ep_avrgL_fx, Ep_tmp_fx[i], &Overflow );                                         /*Q15 */
     815             : #endif                                                            /* BASOP_NOGLOB */
     816         228 :                 IF( L_sub( Ep_tmp_fx[i], Ep_peak_fx ) > 0 )
     817             :                 {
     818          54 :                     Ep_peak_fx = Ep_tmp_fx[i];
     819          54 :                     move32(); /*Q15 */
     820             :                 }
     821             :             }
     822             :         }
     823             : 
     824          38 :         Mpy_32_16_ss( Ep_peak_fx, 24576, &L_tmp, &lo );
     825          38 :         Mpy_32_16_ss( Ep_peak_fx, 19661, &L_tmp2, &lo );
     826          38 :         Mpy_32_16_ss( Ep_avrgL_fx, 24576, &L_tmp3, &lo );
     827             : 
     828          38 :         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 ) ||
     829             :             ( 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 ) )
     830             :         {
     831           5 :             adjustFlag = 1;
     832           5 :             move16();
     833          35 :             FOR( i = 0; i < lowband; i++ )
     834             :             {
     835             : #ifndef BASOP_NOGLOB
     836             :                 tmp = extract_h( L_shl( Ep_avrgL_fx, 1 ) ); /*Q0 */
     837             : #else
     838          30 :                 tmp = extract_h( L_shl_o( Ep_avrgL_fx, 1, &Overflow ) );                                               /*Q0 */
     839             : #endif
     840          30 :                 IF( tmp != 0 )
     841             :                 {
     842          30 :                     exp = norm_s( tmp );
     843          30 :                     tmp = shl( tmp, exp );     /*Q(exp) */
     844          30 :                     tmp = div_s( 16384, tmp ); /*Q(15+14-exp=29-exp) */
     845          30 :                     exp = sub( 29, exp );
     846             :                 }
     847             :                 ELSE
     848             :                 {
     849           0 :                     tmp = 0x7fff;
     850           0 :                     move16();
     851           0 :                     exp = 0;
     852           0 :                     move16();
     853             :                 }
     854          30 :                 Mpy_32_16_ss( Ep_peak_fx, tmp, &L_tmp, &lo );
     855          30 :                 Mpy_32_16_ss( L_tmp, lowband, &L_tmp, &lo );
     856          30 :                 L_tmp = L_shl( L_tmp, sub( 28, exp ) );   /*Q14 0.5 */
     857          30 :                 tmp = extract_l( L_min( L_tmp, 19661 ) ); /* Q14 */
     858          30 :                 Mpy_32_16_ss( L_band_energy_tmp[i], tmp, &L_tmp, &lo );
     859          30 :                 L_band_energy_tmp[i] = L_shl( L_tmp, 1 ); /*Q_band_energy  */
     860             :             }
     861             :         }
     862             : #undef WMC_TOOL_SKIP
     863             : 
     864         750 :         for ( i = 0; i < bands; i++ )
     865             :         {
     866         712 :             band_energy_tmp[i] = (float) ( L_band_energy_tmp[i] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
     867             :         }
     868             : 
     869          38 :         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 );
     870             :     }
     871        1675 :     else if ( st->bwidth == SWB && hqswb_clas == HQ_HARMONIC && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     872             :     {
     873          48 :         hq2_bit_alloc_har( band_energy, bit_budget, bands, Rk_fx, p2a_bands, bwe_br, p2a_flags, band_width );
     874             :     }
     875             :     else
     876             :     {
     877        1627 :         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 );
     878             :     }
     879             : 
     880        2044 :     if ( bit_budget < 0 )
     881             :     {
     882           0 :         st->BER_detect = 1;
     883           0 :         bit_budget = 0;
     884             :     }
     885             : 
     886        2044 :     tcq_core_LR_dec( st, inp_vector, bit_budget, bands, band_start, band_width, Rk_fx, npulses, k_sort, p2a_flags, p2a_bands, last_bitalloc_max_band, inner_frame, adjustFlag, is_transient );
     887             : 
     888             :     /* Prepare floating Rk for next modules */
     889       44739 :     for ( k = 0; k < bands; k++ )
     890             :     {
     891       42695 :         Rk[k] = WORD322FL_SCALE( Rk_fx[k], SWB_BWE_LR_QRk - 1 );
     892             :     }
     893             : 
     894             :     /* Denormalize the coded MDCT spectrum */
     895        2044 :     mdct_spectrum_denorm( inp_vector, y2, band_start, band_end, band_width, band_energy, npulses, bands, ld_slope, pd_thresh );
     896             : 
     897             :     /* Apply fine gain to denormalized coded spectrum */
     898        2044 :     mdct_spectrum_fine_gain_dec( st, y2, band_start, band_end, k_sort, bands, L_qint, Ngq, gqlevs, gqbits );
     899             : 
     900             :     /*restore the band information */
     901        2044 :     if ( flag_spt == 1 )
     902             :     {
     903        1507 :         spt_shorten_domain_band_restore( bands, band_start, band_end, band_width, org_band_start, org_band_end, org_band_width );
     904             :     }
     905             : 
     906        2044 :     mvr2r( y2, y2_org, L_FRAME32k );
     907             : 
     908             :     /* Inject noise into components having relatively low pulse energy per band */
     909        2044 :     ni_seed = npulses[0] + npulses[1] + npulses[2] + npulses[3];
     910             : 
     911     1000572 :     for ( i = 0; i < band_end[bands - 1] + 1; i++ )
     912      998528 :         y2_ni[i] = y2[i];
     913        2044 :     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 );
     914             : 
     915        2044 :     if ( st->bwidth == SWB && ( bwe_br == HQ_16k40 || bwe_br == HQ_13k20 ) )
     916             :     {
     917        1619 :         if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
     918             :         {
     919        1555 :             preset_hq2_swb( hqswb_clas, band_end, &har_bands, p2a_bands, length, bands, &lowlength, &highlength, m );
     920             : 
     921             :             /*Gap filling for the core coder*/
     922        1555 :             swb_bwe_dec_lr( st, y2, m, bwe_br, bands, band_start, band_end, band_energy, p2a_flags, hqswb_clas, lowlength, highlength, har_bands, &hHQ_core->prev_frm_hfe2, &hHQ_core->prev_stab_hfe2, band_width, y2_ni, &ni_seed );
     923             : 
     924        1555 :             post_hq2_swb( m, lowlength, highlength, hqswb_clas, har_bands, bands, p2a_flags, band_start, band_end, y2, npulses );
     925             : 
     926        1555 :             if ( hqswb_clas == HQ_NORMAL )
     927             :             {
     928        1507 :                 spt_swb_peakpos_tmp_save( y2, bands, band_start, band_end, prev_SWB_peak_pos_tmp );
     929        7535 :                 for ( k = 0; k < SPT_SHORTEN_SBNUM; k++ )
     930             :                 {
     931        6028 :                     if ( p2a_flags[bands - SPT_SHORTEN_SBNUM + k] == 0 || npulses[bands - SPT_SHORTEN_SBNUM + k] == 0 )
     932             :                     {
     933        5963 :                         prev_SWB_peak_pos_tmp[k] = 0;
     934             :                     }
     935             :                 }
     936             :             }
     937             : 
     938        1555 :             mvr2r( y2_ni, y2, lowlength );
     939             :         }
     940             :         else
     941             :         {
     942          64 :             mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* HQ_TRANSIENT */
     943             :         }
     944             :     }
     945             :     else
     946             :     {
     947         425 :         mvr2r( y2_ni, y2, band_end[bands - 1] + 1 ); /* NB, WB */
     948             :     }
     949             : 
     950             : 
     951             :     /* bandwidth switching */
     952        2044 :     if ( !( st->last_inner_frame >= L_FRAME16k && st->bws_cnt > 0 ) )
     953             :     {
     954        2044 :         k1 = *is_transient ? bands - 2 : bands - 6;
     955        2044 :         st->prev_ener_shb = 0.0f;
     956             : 
     957       13828 :         for ( i = k1; i < bands; i++ )
     958             :         {
     959       11784 :             st->prev_ener_shb += Ep_tmp[i] / ( bands - k1 );
     960             :         }
     961             :     }
     962             : 
     963        2044 :     if ( st->last_inner_frame >= L_FRAME32k && st->hBWE_FD != NULL )
     964             :     {
     965        1632 :         set_f( st->hBWE_FD->prev_SWB_fenv, st->prev_ener_shb, SWB_FENV );
     966             :     }
     967             : 
     968        2044 :     updat_prev_frm( y2, yout, 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, st->bws_cnt );
     969             : 
     970        2044 :     return;
     971             : }
     972             : 
     973             : 
     974             : /*------------------------------------------------------------------------------------
     975             :  * small_symbol_dec_tran()
     976             :  *
     977             :  * Huffman decoding of differential energies
     978             :  *--------------------------------------------------------------------------------------*/
     979             : 
     980             : /*! r: bits */
     981          99 : static int16_t small_symbol_dec_tran(
     982             :     Decoder_State *st,         /* i/o: decoder state structure                     */
     983             :     int16_t *qbidx,            /* o  : output of dequantized differential energy   */
     984             :     const int16_t bands,       /* i  : number of bands                             */
     985             :     const int16_t is_transient /* i  : transient flag                              */
     986             : )
     987             : {
     988             :     int16_t i, bits;
     989             :     int16_t difidx[BANDS_MAX];
     990             : 
     991             :     /* Decoding differential energies*/
     992          99 :     bits = decode_envelope_indices( st, 0, bands, 0, difidx, LOW_RATE_HQ_CORE_TRAN, is_transient );
     993          99 :     bits += BITS_DE_FCOMP;
     994             : 
     995             :     /* counting 1 bit for band_energy_huff_coding_mode */
     996          99 :     bits += BITS_DE_HMODE;
     997             : 
     998             :     /* converting to original values */
     999        2547 :     for ( i = 0; i < bands; i++ )
    1000             :     {
    1001        2448 :         qbidx[i] = difidx[i] - LRMDCT_BE_OFFSET;
    1002             :     }
    1003             : 
    1004          99 :     return ( bits );
    1005             : }
    1006             : 
    1007             : 
    1008             : /*--------------------------------------------------------------------------
    1009             :  * small_symbol_dec()
    1010             :  *
    1011             :  * Huffman decoding of differential energies (MSB and LSB)
    1012             :  *--------------------------------------------------------------------------*/
    1013             : 
    1014             : /*! r: bits */
    1015        1481 : static int16_t small_symbol_dec(
    1016             :     Decoder_State *st,   /* i/o: decoder state structure                     */
    1017             :     int16_t *qbidx,      /* o  : output of dequantized differential energy   */
    1018             :     const int16_t bands, /* i  : number of bands                             */
    1019             :     const int16_t is_transient )
    1020             : {
    1021             :     int16_t i, LSB, bits;
    1022             :     int16_t difidx[BANDS_MAX];
    1023             : 
    1024             :     /* Decoding MSB bits */
    1025        1481 :     bits = decode_envelope_indices( st, 0, bands, 0, difidx, LOW_RATE_HQ_CORE, is_transient );
    1026        1481 :     bits += BITS_DE_FCOMP;
    1027             : 
    1028             :     /* counting 1 bit for band_energy_huff_coding_mode */
    1029        1481 :     bits += BITS_DE_HMODE;
    1030             : 
    1031             :     /* Decoding LSB bit packing */
    1032       31609 :     for ( i = 0; i < bands; ++i )
    1033             :     {
    1034       30128 :         LSB = get_next_indice( st, BITS_DE_LSB );
    1035       30128 :         difidx[i] = ( difidx[i] << 1 ) | LSB;
    1036             :     }
    1037             : 
    1038             :     /* counting bits for LSB */
    1039        1481 :     bits += bands;
    1040             : 
    1041             :     /* converting to original values */
    1042       30128 :     for ( i = 1; i < bands; ++i )
    1043             :     {
    1044       28647 :         qbidx[i] = difidx[i] - DE_OFFSET1;
    1045             :     }
    1046             : 
    1047        1481 :     qbidx[0] = difidx[0] - DE_OFFSET0;
    1048             : 
    1049        1481 :     return ( bits );
    1050             : }
    1051             : 
    1052             : /*--------------------------------------------------------------------------
    1053             :  * decode_huff_8s()
    1054             :  *
    1055             :  *
    1056             :  *--------------------------------------------------------------------------*/
    1057             : 
    1058       10035 : static int16_t decode_huff_8s(
    1059             :     Decoder_State *st,
    1060             :     const int16_t *hufftab,
    1061             :     int16_t *rbits )
    1062             : {
    1063             :     int16_t bit;
    1064             : 
    1065       31850 :     while ( *hufftab > 0 )
    1066             :     {
    1067       21815 :         *rbits += ( *hufftab & 0xf );
    1068       21815 :         bit = get_next_indice( st, *hufftab & 0xf );
    1069       21815 :         hufftab += ( *hufftab >> 4 ) + bit;
    1070             :     }
    1071             : 
    1072       10035 :     return ( -*hufftab );
    1073             : }
    1074             : 
    1075             : 
    1076             : /*--------------------------------------------------------------------------
    1077             :  * large_symbol_dec()
    1078             :  *
    1079             :  *
    1080             :  *--------------------------------------------------------------------------*/
    1081             : 
    1082             : /*! r: bits */
    1083         464 : static int16_t large_symbol_dec(
    1084             :     Decoder_State *st,  /* i/o: decoder state structure                     */
    1085             :     int16_t *qbidx,     /* o  : output of dequantized differential energy   */
    1086             :     const int16_t bands /* i  : number of bands                             */
    1087             : )
    1088             : {
    1089             :     int16_t i, bits;
    1090             :     int16_t LSB[BANDS_MAX];
    1091             :     int16_t basic_shift, cntbits, ns2mode;
    1092             :     int16_t pos_outlyer;
    1093             :     int16_t ns2mode0, ns2mode1;
    1094             : 
    1095         464 :     cntbits = BITS_DE_8SMODE;
    1096         464 :     ns2mode = get_next_indice( st, BITS_DE_8SMODE );
    1097             : 
    1098         464 :     if ( ns2mode == 0 )
    1099             :     {
    1100         101 :         ns2mode0 = get_next_indice( st, BITS_DE_8SMODE_N0 );
    1101         101 :         ns2mode1 = get_next_indice( st, BITS_DE_8SMODE_N1 );
    1102         101 :         cntbits += BITS_DE_8SMODE_N0 + BITS_DE_8SMODE_N1;
    1103             : 
    1104         101 :         if ( ns2mode0 == 0 )
    1105             :         {
    1106          37 :             if ( ns2mode1 == 1 )
    1107             :             {
    1108           4 :                 pos_outlyer = get_next_indice( st, BITS_DE_8SPOS );
    1109           4 :                 cntbits += BITS_DE_8SPOS;
    1110           4 :                 qbidx[pos_outlyer] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
    1111           4 :                 cntbits += BITS_ABS_ENG;
    1112             :             }
    1113             :             else
    1114             :             {
    1115          33 :                 pos_outlyer = -1;
    1116             :             }
    1117             : 
    1118          56 :             for ( i = 0; i < pos_outlyer; ++i )
    1119             :             {
    1120          19 :                 bits = 0;
    1121          19 :                 qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
    1122          19 :                 cntbits += bits;
    1123             :             }
    1124             : 
    1125         993 :             for ( i = pos_outlyer + 1; i < bands; ++i )
    1126             :             {
    1127         956 :                 bits = 0;
    1128         956 :                 qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
    1129         956 :                 cntbits += bits;
    1130             :             }
    1131             :         }
    1132             :         else
    1133             :         {
    1134          64 :             if ( ns2mode1 == 1 )
    1135             :             {
    1136          16 :                 pos_outlyer = get_next_indice( st, BITS_DE_8SPOS );
    1137          16 :                 cntbits += BITS_DE_8SPOS;
    1138          16 :                 qbidx[0] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
    1139          16 :                 cntbits += BITS_ABS_ENG;
    1140          16 :                 qbidx[pos_outlyer] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
    1141          16 :                 cntbits += BITS_ABS_ENG;
    1142             :             }
    1143             :             else
    1144             :             {
    1145          48 :                 pos_outlyer = 0;
    1146          48 :                 qbidx[0] = get_next_indice( st, BITS_ABS_ENG ) - ABS_ENG_OFFSET;
    1147          48 :                 cntbits += BITS_ABS_ENG;
    1148             :             }
    1149             : 
    1150         109 :             for ( i = 1; i < pos_outlyer; ++i )
    1151             :             {
    1152          45 :                 bits = 0;
    1153          45 :                 qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
    1154          45 :                 cntbits += bits;
    1155             :             }
    1156             : 
    1157        1329 :             for ( i = pos_outlyer + 1; i < bands; ++i )
    1158             :             {
    1159        1265 :                 bits = 0;
    1160        1265 :                 qbidx[i] = ( decode_huff_8s( st, hestable, &bits ) - 4 );
    1161        1265 :                 cntbits += bits;
    1162             :             }
    1163             :         }
    1164             :     }
    1165             :     else
    1166             :     {
    1167         363 :         basic_shift = get_next_indice( st, BITS_MAX_DEPTH );
    1168         363 :         cntbits += BITS_MAX_DEPTH;
    1169             : 
    1170        8113 :         for ( i = 0; i < bands; ++i )
    1171             :         {
    1172        7750 :             bits = 0;
    1173        7750 :             qbidx[i] = decode_huff_8s( st, hestable, &bits ) - 4;
    1174        7750 :             qbidx[i] = qbidx[i] << basic_shift;
    1175        7750 :             cntbits += bits;
    1176             :         }
    1177             : 
    1178        8113 :         for ( i = 0; i < bands; ++i )
    1179             :         {
    1180        7750 :             LSB[0] = get_next_indice( st, basic_shift );
    1181        7750 :             qbidx[i] += LSB[0];
    1182        7750 :             cntbits += basic_shift;
    1183             :         }
    1184             :     }
    1185             : 
    1186         464 :     return cntbits; /* xx bits for diff. energies + 1 bit for LC coding mode */
    1187             : }
    1188             : 
    1189             : 
    1190             : /*--------------------------------------------------------------------------*
    1191             :  * band_energy_dequant()
    1192             :  *
    1193             :  *
    1194             :  *--------------------------------------------------------------------------*/
    1195             : 
    1196        2044 : static float band_energy_dequant(
    1197             :     Decoder_State *st, /* i/o: decoder state structure   */
    1198             :     float band_energy[],
    1199             :     const int16_t bands,
    1200             :     const Word32 L_qint,  /* Q29 */
    1201             :     const Word16 eref_fx, /* Q10 */
    1202             :     const int16_t is_transient )
    1203             : {
    1204             :     int16_t k;
    1205             :     int16_t deng_cmode;
    1206             :     int16_t bq0;
    1207             :     int16_t bq1[BANDS_MAX], bq2[BANDS_MAX];
    1208             :     float deng_bits;
    1209             :     Word32 L_band_energy[BANDS_MAX]; /* SWB_BWE_LR_Qbe */
    1210             :     Word16 exp_normd;
    1211             :     Word16 rev_qint_fx;
    1212             :     Word16 Qrev_qint;
    1213             : 
    1214             :     /* parsing energy difference coding mode */
    1215        2044 :     deng_cmode = get_next_indice( st, BITS_DE_CMODE );
    1216             : 
    1217        2044 :     if ( deng_cmode == 0 )
    1218             :     {
    1219         464 :         deng_bits = (float) large_symbol_dec( st, bq2, bands );
    1220             : 
    1221             :         /* counting 1 bit for deng coding mode */
    1222         464 :         deng_bits += BITS_DE_CMODE;
    1223             :     }
    1224             :     else
    1225             :     {
    1226        1580 :         if ( is_transient )
    1227             :         {
    1228          99 :             deng_bits = (float) small_symbol_dec_tran( st, bq2, bands, is_transient );
    1229             :         }
    1230             :         else
    1231             :         {
    1232        1481 :             deng_bits = (float) small_symbol_dec( st, bq2, bands, is_transient );
    1233             :         }
    1234             : 
    1235             :         /* counting 1 bit for deng coding mode */
    1236        1580 :         deng_bits += BITS_DE_CMODE;
    1237             :     }
    1238             : 
    1239             : #define WMC_TOOL_SKIP
    1240             :     /* Get the reference energy */
    1241        2044 :     exp_normd = norm_l( L_qint );
    1242        2044 :     rev_qint_fx = div_s( 0x4000, round_fx( L_shl( L_qint, exp_normd ) ) ); /* Q14-(29+exp_normd-16)+15 */
    1243        2044 :     Qrev_qint = sub( 14 - ( 29 - 16 ) + 15, exp_normd );
    1244             : 
    1245        2044 :     bq0 = (int16_t) round_fx( L_shl( L_mult( eref_fx, rev_qint_fx ), sub( 5, Qrev_qint ) ) ); /* 16-(10+Qrev_qint+1) */
    1246             : #undef WMC_TOOL_SKIP
    1247             : 
    1248             :     /* Reconstruct quantized spectrum */
    1249        2044 :     bq1[0] = bq2[0] + bq0;
    1250       42695 :     for ( k = 1; k < bands; k++ )
    1251             :     {
    1252       40651 :         bq1[k] = bq2[k] + bq1[k - 1];
    1253             :     }
    1254             : 
    1255       44739 :     for ( k = 0; k < bands; k++ )
    1256             :     {
    1257             : #define WMC_TOOL_SKIP
    1258       42695 :         L_band_energy[k] = L_mls( L_qint, bq1[k] );
    1259       42695 :         move32(); /* 29+0-15 -> Qbe(Q14) */
    1260       42695 :         band_energy[k] = (float) ( L_band_energy[k] / pow( 2.0f, SWB_BWE_LR_Qbe ) );
    1261             : #undef WMC_TOOL_SKIP
    1262             :     }
    1263             : 
    1264        2044 :     if ( is_transient )
    1265             :     {
    1266         120 :         reverse_transient_frame_energies( band_energy, bands );
    1267             :     }
    1268             : 
    1269        2044 :     return ( deng_bits );
    1270             : }
    1271             : 
    1272             : 
    1273             : /*--------------------------------------------------------------------------*
    1274             :  * p2a_threshold_dequant()
    1275             :  *
    1276             :  *
    1277             :  *--------------------------------------------------------------------------*/
    1278             : 
    1279        1996 : static int16_t p2a_threshold_dequant(
    1280             :     Decoder_State *st, /* i/o: decoder state structure   */
    1281             :     int16_t *p2a_flags,
    1282             :     const int16_t bands,
    1283             :     const int16_t p2a_bands )
    1284             : {
    1285             :     int16_t j, k;
    1286             : 
    1287       35852 :     for ( k = 0; k < bands - p2a_bands; k++ )
    1288             :     {
    1289       33856 :         p2a_flags[k] = 1;
    1290             :     }
    1291             : 
    1292        1996 :     j = 0;
    1293        9779 :     for ( k = bands - p2a_bands; k < bands; k++ )
    1294             :     {
    1295        7783 :         p2a_flags[k] = get_next_indice( st, 1 );
    1296        7783 :         j++;
    1297             :     }
    1298             : 
    1299        1996 :     return ( j );
    1300             : }
    1301             : 
    1302             : 
    1303             : /*--------------------------------------------------------------------------*
    1304             :  * mdct_spectrum_fine_gain_dec()
    1305             :  *
    1306             :  *
    1307             :  *--------------------------------------------------------------------------*/
    1308             : 
    1309        2044 : static void mdct_spectrum_fine_gain_dec(
    1310             :     Decoder_State *st, /* i/o: decoder state structure   */
    1311             :     float y2[],
    1312             :     const int16_t band_start[],
    1313             :     const int16_t band_end[],
    1314             :     const int16_t k_sort[],
    1315             :     const int16_t bands,
    1316             :     const Word32 L_qint,
    1317             :     const int16_t Ngq,
    1318             :     const int16_t gqlevs,
    1319             :     const int16_t gqbits )
    1320             : {
    1321             :     int16_t i, k, imin;
    1322             :     float gamma;
    1323             :     float gain_table[MAX_GQLEVS];
    1324             :     Word16 exp_normn, exp_normd;
    1325             :     Word16 delta_fx, Qdelta;
    1326             :     Word32 L_delta, L_q;
    1327             :     Word32 L_temp;
    1328             :     Word16 gain_table_fx[MAX_GQLEVS];
    1329             :     Word16 Qgt;
    1330             :     Word16 temp_lo_fx, temp_hi_fx;
    1331             : 
    1332             : #define WMC_TOOL_SKIP
    1333             :     /* Fine gain quantization on only the most significant energy bands */
    1334             :     /*delta = qint / gqlevs; */
    1335        2044 :     exp_normn = norm_l( L_qint );
    1336        2044 :     exp_normn = sub( exp_normn, 1 );
    1337        2044 :     exp_normd = norm_s( gqlevs );
    1338        2044 :     delta_fx = div_l( L_shl( L_qint, exp_normn ), shl( gqlevs, exp_normd ) );
    1339        2044 :     Qdelta = add( sub( exp_normn, exp_normd ), 28 ); /* 29+exp_normn-(exp_normd)-1; */
    1340        2044 :     L_delta = L_shl( L_deposit_h( delta_fx ), sub( 13, Qdelta ) );
    1341             :     /*q = (-qint + delta) / 2.0f; */
    1342        2044 :     L_q = L_shr( L_sub( L_delta, L_qint ), 1 );
    1343             : 
    1344        6132 :     FOR( i = 0; i < gqlevs; i++ )
    1345             :     {
    1346             :         /*gain_table[i] = (float) pow (2.0f, q * 0.5f); */
    1347        4088 :         L_temp = L_shr( L_shr( L_q, 1 ), sub( 29, 16 ) );
    1348        4088 :         temp_lo_fx = L_Extract_lc( L_temp, &temp_hi_fx );
    1349        4088 :         Qgt = sub( 14, temp_hi_fx );
    1350        4088 :         gain_table_fx[i] = extract_l( Pow2( 14, temp_lo_fx ) ); /* Qgt */
    1351             : 
    1352             :         /*q += delta; */
    1353        4088 :         L_q = L_add( L_q, L_delta );
    1354        4088 :         gain_table_fx[i] = shl( gain_table_fx[i], sub( 14, Qgt ) ); /* Qgt -> Q14 */
    1355        4088 :         gain_table[i] = (float) ( gain_table_fx[i] / pow( 2.0f, 14 ) );
    1356             :     }
    1357             : #undef WMC_TOOL_SKIP
    1358             : 
    1359        9722 :     for ( k = bands - Ngq; k < bands; k++ )
    1360             :     {
    1361        7678 :         imin = get_next_indice( st, gqbits );
    1362        7678 :         gamma = gain_table[imin];
    1363             : 
    1364       80241 :         for ( i = band_start[k_sort[k]]; i <= band_end[k_sort[k]]; i++ )
    1365             :         {
    1366       72563 :             y2[i] *= gamma;
    1367             :         }
    1368             :     }
    1369             : 
    1370        2044 :     return;
    1371             : }
    1372             : 
    1373             : /*--------------------------------------------------------------------------*
    1374             :  * spt_shorten_domain_set_dec()
    1375             :  *
    1376             :  * update the shorten band information based on p2a analysis
    1377             :  *--------------------------------------------------------------------------*/
    1378             : 
    1379        1507 : static void spt_shorten_domain_set_dec(
    1380             :     Decoder_State *st,              /* i  : decoder state structure             */
    1381             :     const int16_t p2a_flags[],      /* i  : p2a anlysis information             */
    1382             :     const int16_t new_band_start[], /* i  : new band start position             */
    1383             :     const int16_t new_band_end[],   /* i  : new band end position               */
    1384             :     const int16_t new_band_width[], /* i  : new subband band width              */
    1385             :     const int16_t bands,            /* i  : total number of subbands            */
    1386             :     int16_t band_start[],           /* o  : band start position                 */
    1387             :     int16_t band_end[],             /* o  : band end position                   */
    1388             :     int16_t band_width[],           /* o  : sub band band width                 */
    1389             :     int16_t *bit_budget             /* i/o: bit budget                          */
    1390             : )
    1391             : {
    1392             :     int16_t j, k;
    1393             :     int16_t spt_shorten_flag[SPT_SHORTEN_SBNUM];
    1394             : 
    1395        1507 :     j = 0;
    1396        7535 :     for ( k = bands - SPT_SHORTEN_SBNUM; k < bands; k++ )
    1397             :     {
    1398        6028 :         spt_shorten_flag[j] = 0;
    1399        6028 :         if ( p2a_flags[k] == 1 )
    1400             :         {
    1401         209 :             spt_shorten_flag[j] = get_next_indice( st, 1 );
    1402         209 :             *bit_budget -= 1;
    1403         209 :             if ( spt_shorten_flag[j] == 1 )
    1404             :             {
    1405          13 :                 band_start[k] = new_band_start[j];
    1406          13 :                 band_end[k] = new_band_end[j];
    1407          13 :                 band_width[k] = new_band_width[j];
    1408             :             }
    1409             :         }
    1410             : 
    1411        6028 :         j++;
    1412             :     }
    1413             : 
    1414        1507 :     return;
    1415             : }

Generated by: LCOV version 1.14