LCOV - code coverage report
Current view: top level - lib_com - hq2_bit_alloc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 136 469 29.0 %
Date: 2025-07-09 08:36:12 Functions: 1 4 25.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 "prot.h"
      44             : #include "rom_com.h"
      45             : #include "basop_util.h"
      46             : #include "basop_mpy.h"
      47             : #include "stl.h"
      48             : #include "wmc_auto.h"
      49             : 
      50             : #define WMC_TOOL_SKIP
      51             : 
      52             : /*-------------------------------------------------------------------
      53             :  * Local constants
      54             :  *-------------------------------------------------------------------*/
      55             : 
      56             : #define MIN_BITS_FIX   0
      57             : #define HQ_16k40_BIT   ( HQ_16k40 / FRAMES_PER_SEC ) /* 16400/50=328, FOR FIX TMP */
      58             : #define C1_QRk         ( 1 << SWB_BWE_LR_QRk )       /* 1 */
      59             : #define Qbf            14
      60             : #define C1_Qbf         ( 1 << Qbf ) /* 1 */
      61             : #define BITS_FACT_1p10 ( Word16 )( 1.10f * (float) pow( 2, Qbf ) + 0.5f )
      62             : #define BITS_FACT_1p05 ( Word16 )( 1.05f * (float) pow( 2, Qbf ) + 0.5f )
      63             : #define BITS_FACT_1p00 ( Word16 )( 1.00f * (float) pow( 2, Qbf ) + 0.5f )
      64             : #define BITS_FACT_0p97 ( Word16 )( 0.97f * (float) pow( 2, Qbf ) + 0.5f )
      65             : #define BITS_FACT_0p92 ( Word16 )( 0.92f * (float) pow( 2, Qbf ) + 0.5f )
      66             : 
      67             : #define L_Comp( hi, lo ) L_mac( L_deposit_h( hi ), lo, 1 )
      68             : 
      69             : /*-------------------------------------------------------------------
      70             :  * div_s_ss()
      71             :  *
      72             :  *
      73             :  *-------------------------------------------------------------------*/
      74             : 
      75             : /*! r: result of division (Word16 Q0) */
      76           0 : static Word16 div_s_ss(
      77             :     const Word16 n, /* i  : numerator   (Word16 Q0         */
      78             :     const Word16 d  /* i  : denominator (Word16 Q0)        */
      79             : )
      80             : {
      81             :     Word16 norm_n, norm_d;
      82             :     Word16 ns, ds;
      83             :     Word16 res;
      84             :     Word16 tmp;
      85             : 
      86           0 :     test();
      87           0 :     IF( n == 0 || d == 0 )
      88             :     {
      89           0 :         return 0;
      90             :     }
      91             : 
      92           0 :     norm_n = norm_s( n );
      93           0 :     norm_n = sub( norm_n, 1 );
      94           0 :     ns = shl( n, norm_n );
      95             : 
      96           0 :     norm_d = norm_s( d );
      97           0 :     ds = shl( d, norm_d );
      98             : 
      99           0 :     tmp = div_s( ns, ds );
     100           0 :     res = shr( tmp, add( sub( norm_n, norm_d ), 15 ) );
     101             : 
     102           0 :     return res;
     103             : }
     104             : 
     105             : 
     106             : /*-------------------------------------------------------------------*
     107             :  * Bits2indvsb()
     108             :  *
     109             :  * Bit allocation to individual SB's in a group
     110             :  *-------------------------------------------------------------------*/
     111           0 : static void Bits2indvsb_fx(
     112             :     const Word32 *L_be,         /* i  : Qbe Band Energy of sub-band                       */
     113             :     const Word16 start_band,    /* i  : Q0  start band indices                            */
     114             :     const Word16 end_band,      /* i  : Q0  end band indices                              */
     115             :     const Word16 Bits,          /* i  : Q0  Total number of bits allocated to a group     */
     116             :     const Word32 L_Bits_needed, /* i  : QRk smallest bit number for allocation in group   */
     117             :     Word32 *L_Rsubband,         /* o  : QRk bit allocation of sub-band                    */
     118             :     Word16 *p2aflags_fx         /* i/o: Q0  peaky/noise subband flag                      */
     119             : )
     120             : {
     121             :     Word16 i, j, k;
     122             :     Word32 L_R_temp[14]; /* QRk = QL_Rsubband; */
     123             :     Word16 Ravg_fx;
     124             :     Word16 QRavg;
     125             : 
     126             :     const Word32 *L_y_ptr;
     127             :     Word32 *L_R_ptr;
     128             : 
     129             :     Word16 Bits_avg_fx;
     130             :     Word16 QBavg;
     131             :     Word16 scale_fact_fx;
     132             : 
     133             :     Word16 band_num_fx;
     134             :     Word16 index_fx[14];
     135             : 
     136             :     Word16 y_index_fx[14];
     137             : 
     138             :     Word16 be_sum_fx; /* Q0 */
     139             : 
     140             :     Word16 exp_normn, exp_normd;
     141             :     Word16 enr_diffcnt_fx;
     142             :     Word16 th_5_fx;
     143             :     Word16 Rcnt_fx;
     144             : 
     145             :     Word16 be_cnt_fx;
     146             :     Word16 *p2aflags_fx_ptr;
     147             : 
     148             :     Word32 L_temp1;
     149             :     Word32 L_temp2;
     150             : 
     151           0 :     band_num_fx = sub( end_band, start_band );
     152           0 :     L_y_ptr = L_be + start_band;
     153           0 :     L_R_ptr = L_Rsubband + start_band;
     154           0 :     p2aflags_fx_ptr = p2aflags_fx + start_band;
     155             : 
     156           0 :     FOR( i = 0; i < band_num_fx; i++ )
     157             :     {
     158           0 :         y_index_fx[i] = extract_h( L_shr( L_y_ptr[i], sub( SWB_BWE_LR_Qbe, 16 ) ) );
     159           0 :         move16();
     160           0 :         index_fx[i] = i;
     161           0 :         move16();
     162             :     }
     163             : 
     164             : 
     165             :     /* Rearrange norm vector in decreasing order */
     166           0 :     reordvct( y_index_fx, band_num_fx, index_fx );
     167             : 
     168           0 :     be_sum_fx = 0;
     169           0 :     move16();
     170           0 :     be_cnt_fx = 0;
     171           0 :     move16();
     172           0 :     FOR( j = 0; j < band_num_fx; j++ )
     173             :     {
     174           0 :         test();
     175           0 :         IF( y_index_fx[j] <= 0 || p2aflags_fx_ptr[index_fx[j]] == 0 )
     176             :         {
     177           0 :             y_index_fx[j] = 0;
     178           0 :             move16();
     179           0 :             L_R_temp[j] = 0x0L;
     180           0 :             move32();
     181             :         }
     182             :         ELSE
     183             :         {
     184           0 :             L_R_temp[j] = C1_QRk;
     185           0 :             move32(); /* filled not zero value */
     186           0 :             be_cnt_fx = add( be_cnt_fx, 1 );
     187             :         }
     188             :     }
     189             : 
     190           0 :     i = sub( be_cnt_fx, 1 );
     191           0 :     FOR( k = 0; k <= i; k++ )
     192             :     {
     193           0 :         test();
     194           0 :         if ( L_R_temp[k] > 0 )
     195             :         {
     196           0 :             be_sum_fx = add( be_sum_fx, y_index_fx[k] );
     197             :         }
     198             :     }
     199           0 :     QBavg = 0;
     200           0 :     move16();
     201             : 
     202             :     /*Ravg = (float) be_sum/be_cnt;*/
     203           0 :     Ravg_fx = 0;
     204           0 :     move16();
     205           0 :     QRavg = 0;
     206           0 :     move16();
     207           0 :     IF( be_cnt_fx != 0x0 )
     208             :     {
     209           0 :         exp_normn = norm_s( be_sum_fx );
     210           0 :         exp_normn = sub( exp_normn, 1 );
     211           0 :         exp_normd = norm_s( be_cnt_fx );
     212           0 :         Ravg_fx = div_s( shl( be_sum_fx, exp_normn ), shl( be_cnt_fx, exp_normd ) );
     213             : 
     214           0 :         Ravg_fx = shr( Ravg_fx, 2 ); /* safe shift */
     215           0 :         QRavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
     216             :     }
     217             : 
     218           0 :     enr_diffcnt_fx = 0;
     219           0 :     move16();
     220           0 :     th_5_fx = shl( 5, QRavg );
     221           0 :     FOR( j = 0; j < be_cnt_fx; j++ )
     222             :     {
     223           0 :         IF( sub( abs_s( sub( Ravg_fx, shl( y_index_fx[j], QRavg ) ) ), th_5_fx ) > 0 )
     224             :         {
     225           0 :             enr_diffcnt_fx = add( enr_diffcnt_fx, 1 );
     226             :         }
     227             :     }
     228             : 
     229           0 :     IF( enr_diffcnt_fx > 0 )
     230             :     {
     231           0 :         scale_fact_fx = 11468;
     232           0 :         move16(); /* 0.35f 11468.8(Q15) */
     233             :     }
     234             :     ELSE
     235             :     {
     236           0 :         scale_fact_fx = 19661;
     237           0 :         move16(); /* 0.60f 19660.8(Q15) */
     238             :     }
     239             : 
     240             :     /* Bits allocation to individual SB's in a group based on Band Energies */
     241           0 :     FOR( j = 0; j < be_cnt_fx; j++ )
     242             :     {
     243           0 :         Rcnt_fx = add( i, 1 );
     244             : 
     245             :         /* Ravg = (float) be_sum/Rcnt; */
     246           0 :         exp_normn = norm_s( be_sum_fx );
     247           0 :         exp_normn = sub( exp_normn, 1 );
     248           0 :         exp_normd = norm_s( Rcnt_fx );
     249           0 :         Ravg_fx = div_s( shl( be_sum_fx, exp_normn ), shl( Rcnt_fx, exp_normd ) );
     250           0 :         Ravg_fx = shr( Ravg_fx, 2 ); /* safe shift */
     251           0 :         QRavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
     252             : 
     253           0 :         test();
     254           0 :         if ( be_sum_fx <= 0 )
     255             :         {
     256           0 :             be_sum_fx = 1;
     257           0 :             move16();
     258             :         }
     259             : 
     260             :         /* Bits_avg = (float) Bits/(be_sum+EPSILON); */
     261           0 :         Bits_avg_fx = 0;
     262           0 :         move16();
     263           0 :         QBavg = 0;
     264           0 :         move16();
     265           0 :         IF( Bits != 0 )
     266             :         {
     267           0 :             exp_normn = norm_s( Bits );
     268           0 :             exp_normn = sub( exp_normn, 1 );
     269           0 :             exp_normd = norm_s( be_sum_fx );
     270           0 :             Bits_avg_fx = div_s( shl( Bits, exp_normn ), shl( be_sum_fx, exp_normd ) );
     271           0 :             Bits_avg_fx = shr( Bits_avg_fx, 2 ); /* safe_shift */
     272           0 :             QBavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
     273             :         }
     274           0 :         FOR( k = 0; k <= i; k++ )
     275             :         {
     276           0 :             IF( L_R_temp[k] > 0 ) /* Rtemp -> SWB_BWE_LR_QRk */
     277             :             {
     278             :                 /* Allocate more bits to SB, if SB bandenergy is higher than average energy */
     279             :                 /* R_temp[k] = (float)( Bits_avg * y_index[k]+( scale_fact * (y_index[k] - Ravg))); */
     280           0 :                 L_temp1 = L_mult( Bits_avg_fx, y_index_fx[k] );                                                                                             /* QBavg+1 */
     281           0 :                 L_temp2 = L_mult( scale_fact_fx, sub( shl( y_index_fx[k], QRavg ), Ravg_fx ) );                                                             /* 15+QRavg+1 */
     282           0 :                 L_R_temp[k] = L_add( L_shr( L_temp1, sub( add( QBavg, 1 ), SWB_BWE_LR_QRk ) ), L_shr( L_temp2, sub( add( QRavg, 16 ), SWB_BWE_LR_QRk ) ) ); /* SWB_BWE_LR_QRk */
     283             :             }
     284             :         }
     285           0 :         IF( L_sub( L_R_temp[i], L_Bits_needed ) < 0 )
     286             :         {
     287           0 :             L_R_temp[i] = 0x0L;
     288           0 :             move32();
     289             : 
     290           0 :             p2aflags_fx_ptr[index_fx[i]] = 0;
     291           0 :             move16();
     292             : 
     293             :             /* be_sum -= y_index[i]; */
     294           0 :             be_sum_fx = sub( be_sum_fx, y_index_fx[i] );
     295             : 
     296           0 :             i = sub( i, 1 );
     297             :         }
     298             :         ELSE
     299             :         {
     300           0 :             BREAK;
     301             :         }
     302             :     }
     303             : 
     304             :     /* Rearrange the bit allocation to align with original */
     305           0 :     FOR( k = 0; k < band_num_fx; k++ )
     306             :     {
     307           0 :         j = index_fx[k];
     308           0 :         move16();
     309           0 :         L_R_ptr[j] = L_R_temp[k];
     310           0 :         move32();
     311             :     }
     312             : 
     313           0 :     return;
     314             : }
     315             : 
     316             : /*-------------------------------------------------------------------*
     317             :  * hq2_bit_alloc_har()
     318             :  *
     319             :  * Bit allocation mechanism for HQ_HARMONIC mode
     320             :  *-------------------------------------------------------------------*/
     321             : 
     322           0 : void hq2_bit_alloc_har(
     323             :     float *y,     /* i  : band energy of sub-vectors                    */
     324             :     int16_t B_fx, /* i  : number of available bits                      */
     325             :     int16_t N_fx, /* i  : number of sub-vectors                         */
     326             :     Word32 *L_Rsubband,
     327             :     int16_t p2a_bands_fx,
     328             :     int32_t L_core_brate, /* i  : core bitrate                                 */
     329             :     int16_t p2a_flags_fx[],
     330             :     int16_t band_width_fx[] )
     331             : {
     332             :     Word16 i, j, k;
     333             : 
     334             :     Word32 L_norm_sum;                    /* Qbe */
     335             :     Word32 L_Ravg_sub[GRP_SB];            /* Qbe */
     336             :     Word32 L_temp_band_energy[BANDS_MAX]; /* Qbe */
     337             : 
     338             :     Word16 j_fx, k_fx, Bits_grp_fx[GRP_SB];
     339             : 
     340             :     Word32 L_temp_band_energydiff[BANDS_MAX];
     341             :     Word16 G1_BE_DIFF_POS_fx; /* Q0 */
     342             :     Word32 L_G1_BE_DIFF_VAL;  /* Qbe  Word32 */
     343             :     Word16 final_gr_fact_pos_fx, gmax_range_fx[2], temp_fx;
     344             :     Word16 bits_fact_fx, bits_fact1_fx; /* Q? */
     345             :     Word16 grp_rngmax_fx[2];
     346             :     Word16 index_fx[NB_SWB_SUBBANDS_HAR], y_index_fx[NB_SWB_SUBBANDS_HAR], esthf_bits_fx, grp_bit_avg_fx, harmonic_band_fx;
     347             :     Word32 L_norm_sum_avg;
     348             :     Word32 L_norm_diff;        /* Qbe */
     349             :     Word16 bits_allocweigh_fx; /* Q15 */
     350             :     Word16 grp_bound_fx[5];
     351             :     Word32 L_grp_thr[GRP_SB];  /* not require Word32 precission */
     352             :     Word16 lf_hf_ge_r_fx;      /* Q15 */
     353             :     Word32 L_avg_enhf_en_diff; /* Qbe */
     354             : 
     355             :     Word16 B_norm_fx;
     356             : 
     357             :     Word32 L_temp, L_temp2;
     358             :     Word16 exp, frac;
     359             : 
     360             :     Word32 L_THR1, L_THR2, L_THR3;
     361             : 
     362             :     Word16 exp_norm;
     363             :     Word16 norm_sum_fx;
     364             :     Word16 Qns;             /* Q value for norm_sum_fx */
     365             :     Word16 Inv_norm_sum_fx; /* 1/norm_sum */
     366             :     Word16 QIns;            /* Q value for Inv_norm_sum_fx */
     367             : 
     368             :     Word16 exp_normn, exp_normd;
     369             :     Word16 div_fx;
     370             : 
     371             :     Word16 Inv_p2a_bands_fx;
     372             :     Word16 QIpb;
     373             : 
     374             :     Word16 exp_shift;
     375             : 
     376             :     Word32 L_y[BANDS_MAX];
     377             : 
     378             : #ifdef BASOP_NOGLOB
     379             :     Flag Overflow;
     380           0 :     Overflow = 0;
     381             : #endif
     382             : 
     383           0 :     grp_rngmax_fx[0] = 0;
     384           0 :     grp_rngmax_fx[1] = 0;
     385             : 
     386           0 :     for ( i = 0; i < N_fx; i++ )
     387             :     {
     388           0 :         L_y[i] = (Word32) ( y[i] * (float) pow( 2.0f, SWB_BWE_LR_Qbe ) );
     389             :     }
     390             : 
     391           0 :     L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk );
     392           0 :     L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk );
     393           0 :     L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk );
     394             : 
     395           0 :     set_val_Word16( Bits_grp_fx, 0, GRP_SB );
     396             : 
     397             :     /* Initialize subbands bits allocation vector based on harmonic bands */
     398           0 :     harmonic_band_fx = add( sub( N_fx, p2a_bands_fx ), 1 );
     399             :     /*printf("harmonic_band= %d %d\n", harmonic_band, harmonic_band_fx);*/
     400           0 :     FOR( k = 0; k < N_fx; k++ )
     401             :     {
     402           0 :         L_Rsubband[k] = (Word32) ( C1_QRk );
     403           0 :         move32(); /* Constant Value */
     404           0 :         L_temp_band_energy[k] = L_y[k];
     405           0 :         move32(); /* SWB_BWE_LR_Qbe */
     406             :     }
     407           0 :     final_gr_fact_pos_fx = 2;
     408           0 :     move16();
     409           0 :     bits_fact_fx = C1_Qbf;
     410           0 :     move16();
     411           0 :     bits_fact1_fx = C1_Qbf;
     412           0 :     move16();
     413             : 
     414           0 :     gmax_range_fx[0] = G1_RANGE;
     415           0 :     move16();
     416           0 :     gmax_range_fx[1] = G1G2_RANGE;
     417           0 :     move16();
     418             : 
     419           0 :     IF( L_sub( L_core_brate, HQ_16k40 ) == 0 )
     420             :     {
     421           0 :         gmax_range_fx[1] = add( gmax_range_fx[1], 2 );
     422           0 :         move16();
     423             :     }
     424             : 
     425             :     /* decide each group range, for grouping spectral coefficients */
     426           0 :     grp_rngmax_fx[1] = 16;
     427           0 :     move16();
     428           0 :     grp_rngmax_fx[0] = 7;
     429           0 :     move16();
     430           0 :     temp_fx = 0;
     431           0 :     move16();
     432           0 :     FOR( i = 0; i < 2; i++ )
     433             :     {
     434           0 :         j_fx = gmax_range_fx[i];
     435           0 :         move16();
     436           0 :         k_fx = 0;
     437           0 :         move16();
     438           0 :         test();
     439           0 :         WHILE( L_sub( L_temp_band_energy[gmax_range_fx[i] - 1], L_temp_band_energy[j_fx] ) >= 0x0L && sub( j_fx, grp_rngmax_fx[i] ) < 0x0 )
     440             :         {
     441           0 :             test();
     442           0 :             k_fx = add( k_fx, 1 );
     443           0 :             j_fx = add( j_fx, 1 );
     444             :         }
     445             : 
     446           0 :         temp_fx = k_fx;
     447           0 :         move16();
     448           0 :         IF( sub( temp_fx, 1 ) > 0 )
     449             :         {
     450           0 :             FOR( temp_fx = 2; temp_fx <= k_fx; )
     451             :             {
     452           0 :                 IF( L_sub( L_temp_band_energy[gmax_range_fx[i] + temp_fx - 1], L_temp_band_energy[gmax_range_fx[i] + temp_fx] ) < 0 )
     453             :                 {
     454           0 :                     BREAK;
     455             :                 }
     456           0 :                 ELSE IF( L_sub( L_temp_band_energy[gmax_range_fx[i] + temp_fx - 1], L_temp_band_energy[gmax_range_fx[i] + temp_fx] ) >= 0 )
     457             :                 {
     458           0 :                     temp_fx = add( temp_fx, 1 );
     459           0 :                     IF( sub( temp_fx, k_fx ) > 0 )
     460             :                     {
     461           0 :                         temp_fx = sub( temp_fx, 1 );
     462           0 :                         BREAK;
     463             :                     }
     464             :                 }
     465             :             }
     466             : 
     467           0 :             gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx );
     468             :         }
     469             :         ELSE
     470             :         {
     471           0 :             gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx );
     472             :         }
     473             :     }
     474             : 
     475           0 :     grp_bound_fx[0] = 0;
     476           0 :     move16();
     477           0 :     FOR( i = 1; i < GRP_SB - 1; i++ )
     478             :     {
     479           0 :         grp_bound_fx[i] = gmax_range_fx[i - 1];
     480           0 :         move16();
     481             :     }
     482           0 :     grp_bound_fx[i] = harmonic_band_fx;
     483           0 :     move16();
     484           0 :     grp_bound_fx[i + 1] = N_fx;
     485           0 :     move16();
     486             : 
     487             : 
     488           0 :     FOR( i = 0; i < GRP_SB; i++ )
     489             :     {
     490           0 :         L_Ravg_sub[i] = 0x0L;
     491           0 :         move32();
     492           0 :         FOR( j = grp_bound_fx[i]; j < grp_bound_fx[i + 1]; j++ )
     493             :         {
     494           0 :             IF( L_temp_band_energy[j] > 0x0L )
     495             :             {
     496           0 :                 L_Ravg_sub[i] = L_add( L_Ravg_sub[i], L_temp_band_energy[j] );
     497           0 :                 move32();
     498             :             }
     499             :         }
     500             :     }
     501             : 
     502           0 :     L_temp_band_energydiff[0] = L_temp_band_energy[0];
     503           0 :     move32();
     504           0 :     FOR( j = 1; j < harmonic_band_fx; j++ )
     505             :     {
     506           0 :         L_temp_band_energydiff[j] = L_abs( L_sub( L_temp_band_energy[j], L_temp_band_energy[j - 1] ) );
     507           0 :         move32();
     508             :     }
     509             : 
     510           0 :     G1_BE_DIFF_POS_fx = 0;
     511           0 :     move16();
     512           0 :     L_G1_BE_DIFF_VAL = 0x0L;
     513           0 :     move32();
     514             : 
     515           0 :     FOR( j = 1; j < harmonic_band_fx; j++ )
     516             :     {
     517           0 :         IF( L_sub( L_temp_band_energydiff[j], L_G1_BE_DIFF_VAL ) > 0 )
     518             :         {
     519           0 :             G1_BE_DIFF_POS_fx = j;
     520           0 :             move16();
     521           0 :             L_G1_BE_DIFF_VAL = L_temp_band_energydiff[j];
     522           0 :             move32();
     523             :         }
     524             :     }
     525             : 
     526           0 :     test();
     527           0 :     test();
     528           0 :     IF( sub( G1_BE_DIFF_POS_fx, gmax_range_fx[0] ) < 0 && G1_BE_DIFF_POS_fx > 0 )
     529             :     {
     530           0 :         final_gr_fact_pos_fx = 0;
     531           0 :         move16();
     532             :     }
     533           0 :     ELSE IF( sub( G1_BE_DIFF_POS_fx, gmax_range_fx[0] ) >= 0 && sub( G1_BE_DIFF_POS_fx, gmax_range_fx[1] ) < 0 )
     534             :     {
     535           0 :         final_gr_fact_pos_fx = 1;
     536           0 :         move16();
     537             :     }
     538             :     ELSE
     539             :     {
     540           0 :         final_gr_fact_pos_fx = 2;
     541           0 :         move16();
     542             :     }
     543             : 
     544           0 :     test();
     545           0 :     IF( final_gr_fact_pos_fx == 0 || sub( final_gr_fact_pos_fx, 1 ) == 0 )
     546             :     {
     547           0 :         IF( L_sub( L_core_brate, HQ_16k40 ) == 0 )
     548             :         {
     549           0 :             bits_fact_fx = BITS_FACT_1p10;
     550           0 :             move16(); /* 1.10f; */ /* G1 */
     551           0 :             bits_fact1_fx = BITS_FACT_0p92;
     552           0 :             move16(); /* 0.92f; */ /* G3 */
     553             :         }
     554             :         ELSE
     555             :         {
     556           0 :             bits_fact_fx = BITS_FACT_1p05;
     557           0 :             move16(); /* 1.05f; */ /* G1 */
     558           0 :             bits_fact1_fx = BITS_FACT_0p97;
     559           0 :             move16(); /* 0.97f; */ /* G3 */
     560             :         }
     561             :     }
     562             :     ELSE
     563             :     {
     564           0 :         IF( L_sub( L_core_brate, HQ_16k40 ) == 0 )
     565             :         {
     566           0 :             bits_fact_fx = BITS_FACT_0p97;
     567           0 :             move16(); /* 0.97f; */ /* G1 */
     568           0 :             bits_fact1_fx = BITS_FACT_1p00;
     569           0 :             move16(); /* 1.00f; */ /* G3 */
     570             :         }
     571             :         ELSE
     572             :         {
     573           0 :             bits_fact_fx = BITS_FACT_0p92;
     574           0 :             move16(); /* 0.92f; */ /* G1 */
     575           0 :             bits_fact1_fx = BITS_FACT_1p00;
     576           0 :             move16(); /* 1.00f; */ /* G3 */
     577             :         }
     578             :     }
     579             : 
     580           0 :     FOR( i = 0; i < sub( N_fx, harmonic_band_fx ); i++ )
     581             :     {
     582           0 :         y_index_fx[i] = extract_h( L_shl( L_temp_band_energy[harmonic_band_fx + i], sub( 16, SWB_BWE_LR_Qbe ) ) );
     583           0 :         move16();
     584           0 :         index_fx[i] = add( harmonic_band_fx, i );
     585           0 :         move16();
     586             :     }
     587             : 
     588           0 :     reordvct( y_index_fx, sub( N_fx, harmonic_band_fx ), index_fx );
     589             : 
     590             :     /* Log2 */
     591           0 :     L_temp = L_deposit_l( band_width_fx[index_fx[0]] );
     592           0 :     exp = norm_l( L_temp );
     593           0 :     frac = Log2_norm_lc( L_shl( L_temp, exp ) );
     594           0 :     exp = sub( 30, exp );
     595           0 :     L_temp = L_Comp( exp, frac );
     596             :     /* ceil */
     597           0 :     if ( L_and( 0x0000ffff, L_temp ) > 0 )
     598             :     {
     599           0 :         L_temp = L_add( L_temp, 0x00010000 );
     600             :     }
     601           0 :     esthf_bits_fx = extract_h( L_temp );
     602             : 
     603           0 :     L_grp_thr[0] = L_THR1;
     604           0 :     move32();
     605           0 :     L_grp_thr[1] = L_THR2;
     606           0 :     move32();
     607           0 :     L_grp_thr[2] = L_THR3;
     608           0 :     move32();
     609           0 :     L_grp_thr[3] = L_shl( L_deposit_l( esthf_bits_fx ), SWB_BWE_LR_QRk );
     610           0 :     move16();
     611             : 
     612           0 :     L_norm_sum = 1;
     613           0 :     move32();
     614           0 :     FOR( i = 0; i < 3; i++ )
     615             :     {
     616           0 :         L_norm_sum = L_add( L_norm_sum, L_Ravg_sub[i] );
     617           0 :         move32();
     618             :     }
     619             : 
     620             :     /*reserve bits for HF coding */
     621           0 :     L_temp = L_add( L_norm_sum, L_Ravg_sub[GRP_SB - 1] );
     622           0 :     exp_normn = norm_l( L_temp );
     623           0 :     exp_normn = sub( exp_normn, 1 );
     624           0 :     exp_normd = norm_s( N_fx );
     625             : 
     626           0 :     div_fx = div_l( L_shl( L_temp, exp_normn ), shl( N_fx, exp_normd ) );                    /* (Qbe+exp_normn)-(0+exp_normd)-1) */
     627           0 :     L_norm_sum_avg = L_shr( L_deposit_h( div_fx ), add( sub( exp_normn, exp_normd ), 15 ) ); /* -> Qbe */
     628             : 
     629           0 :     exp_norm = norm_l( L_norm_sum );
     630           0 :     norm_sum_fx = extract_h( L_shl( L_norm_sum, exp_norm ) ); /* SWB_BWE_LR_Qbe+exp_norm-16 */
     631           0 :     Qns = sub( add( SWB_BWE_LR_Qbe, exp_norm ), 16 );
     632             : 
     633           0 :     Inv_norm_sum_fx = div_s( 0x4000 /* Q15 */, norm_sum_fx );
     634           0 :     QIns = sub( 31, exp_norm ); /* 14 - (14+exp_norm-16) + 15 */
     635             : 
     636           0 :     grp_bit_avg_fx = div_s_ss( B_fx, GRP_SB ); /* Q0 */
     637             : 
     638           0 :     exp_normd = norm_s( p2a_bands_fx );
     639           0 :     Inv_p2a_bands_fx = div_s( 0x3fff, shl( p2a_bands_fx, exp_normd ) ); /* 14-exp_normd+15 */
     640           0 :     QIpb = sub( 29, exp_normd );
     641             : 
     642           0 :     L_temp = L_shl( Mpy_32_16( L_Ravg_sub[GRP_SB - 1], Inv_p2a_bands_fx ), sub( SWB_BWE_LR_Qbe, sub( QIpb, 1 ) ) );
     643           0 :     L_norm_diff = L_sub( L_temp, L_norm_sum_avg ); /* Qbe */
     644             : 
     645           0 :     L_temp = Mpy_32_16( L_Ravg_sub[GRP_SB - 1], sub( GRP_SB, 1 ) ); /* Qbe+0+1 */
     646           0 :     L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );                  /* Qbe+1+QIpb+1 */
     647             : #ifdef BASOP_NOGLOB
     648           0 :     lf_hf_ge_r_fx = round_fx_o( L_shl_o( L_temp, sub( 15 + 16, sub( add( SWB_BWE_LR_Qbe, QIns ), 30 ) ), &Overflow ), &Overflow );
     649           0 :     Overflow = 0; /* reset BASOP Overflow */
     650             : #else
     651             :     lf_hf_ge_r_fx = round_fx( L_shl( L_temp, sub( 15 + 16, sub( add( SWB_BWE_LR_Qbe, QIns ), 30 ) ) ) );
     652             : #endif
     653             : 
     654           0 :     exp_normn = norm_s( norm_sum_fx );
     655           0 :     exp_normn = sub( exp_normn, 1 );
     656           0 :     exp_normd = norm_s( harmonic_band_fx );
     657             : 
     658           0 :     div_fx = div_s( shl( norm_sum_fx, exp_normn ), shl( harmonic_band_fx, exp_normd ) );
     659           0 :     L_avg_enhf_en_diff = L_sub( L_temp_band_energy[index_fx[0]], L_shl( L_deposit_h( div_fx ), sub( sub( SWB_BWE_LR_Qbe, ( add( Qns, sub( exp_normn, exp_normd ) ) ) ), 31 ) ) ); /* Qbe - (Qns+exp_normn-(exp_normd)+15) -16 */
     660             : 
     661           0 :     IF( sub( lf_hf_ge_r_fx, 26214 ) > 0x0 && L_sub( L_avg_enhf_en_diff, (Word32) ( 8 << SWB_BWE_LR_Qbe ) ) > 0x0L ) /* 0.8=26214.4(Q15) 8.0f=131072(Qbe) */
     662             :     {
     663           0 :         bits_allocweigh_fx = 6554;
     664           0 :         move16(); /* 0.2 6553.6(Q15) */
     665           0 :         IF( L_norm_diff < 0x0L )
     666             :         {
     667           0 :             bits_allocweigh_fx = 13107;
     668           0 :             move16(); /* 0.4 13107.2(Q15) */
     669             :         }
     670             : 
     671             :         /*allocate bits*/
     672             :         /*Bits_grp[GRP_SB-1] = (int16_t)min((grp_bit_avg/p2a_bands + bits_allocweigh*norm_diff),10);*/
     673           0 :         L_temp = L_mult( grp_bit_avg_fx, Inv_p2a_bands_fx );    /* Q0+QIpb+1 */
     674           0 :         L_temp2 = Mpy_32_16( L_norm_diff, bits_allocweigh_fx ); /* Qbe+Q15-15 */
     675             : 
     676           0 :         L_temp = L_shr( L_temp, add( QIpb, 1 ) );
     677           0 :         L_temp = L_add( L_shl( L_temp, SWB_BWE_LR_Qbe ), L_temp2 );
     678             : 
     679           0 :         Bits_grp_fx[GRP_SB - 1] = extract_h( L_shl( L_temp, sub( 16, SWB_BWE_LR_Qbe ) ) );
     680           0 :         Bits_grp_fx[GRP_SB - 1] = s_min( Bits_grp_fx[GRP_SB - 1], 10 );
     681             : 
     682           0 :         test();
     683           0 :         if ( sub( Bits_grp_fx[GRP_SB - 1], esthf_bits_fx ) < 0 )
     684             :         {
     685           0 :             Bits_grp_fx[GRP_SB - 1] = 0;
     686           0 :             move16();
     687             :         }
     688           0 :         B_fx = sub( B_fx, Bits_grp_fx[GRP_SB - 1] );
     689             :     }
     690             : 
     691           0 :     exp_shift = sub( add( SWB_BWE_LR_Qbe, QIns ), 47 ); /* (SWB_BWE_LR_Qbe+14+1+QIns-15-16) */
     692           0 :     exp_norm = norm_s( B_fx );
     693           0 :     B_norm_fx = shl( B_fx, exp_norm );
     694           0 :     exp_shift = add( exp_shift, exp_norm );
     695             : 
     696           0 :     IF( sub( final_gr_fact_pos_fx, 1 ) == 0 )
     697             :     {
     698           0 :         L_temp = Mpy_32_16( L_Ravg_sub[1], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
     699           0 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     700           0 :         Bits_grp_fx[1] = extract_h( L_shr( L_temp, exp_shift ) );
     701             : 
     702           0 :         L_temp = Mpy_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
     703           0 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     704           0 :         Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) );
     705             : 
     706           0 :         Bits_grp_fx[0] = sub( sub( B_fx, Bits_grp_fx[1] ), Bits_grp_fx[2] );
     707             :     }
     708             :     ELSE
     709             :     {
     710           0 :         L_temp = Mpy_32_16( L_Ravg_sub[0], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
     711           0 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     712           0 :         Bits_grp_fx[0] = extract_h( L_shr( L_temp, exp_shift ) );
     713             : 
     714           0 :         L_temp = Mpy_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
     715           0 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     716           0 :         Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) );
     717             : 
     718           0 :         Bits_grp_fx[1] = sub( sub( B_fx, Bits_grp_fx[0] ), Bits_grp_fx[2] );
     719             :     }
     720             : 
     721           0 :     IF( sub( Bits_grp_fx[2], THR2 ) < 0 )
     722             :     {
     723           0 :         Bits_grp_fx[1] = add( Bits_grp_fx[1], Bits_grp_fx[2] );
     724           0 :         move16();
     725           0 :         Bits_grp_fx[2] = 0;
     726           0 :         move16();
     727             :     }
     728             : 
     729           0 :     FOR( i = 0; i < GRP_SB; i++ )
     730             :     {
     731           0 :         IF( Bits_grp_fx[i] > 0 )
     732             :         {
     733           0 :             Bits2indvsb_fx( L_temp_band_energy, grp_bound_fx[i], grp_bound_fx[i + 1], Bits_grp_fx[i], L_grp_thr[i], L_Rsubband, p2a_flags_fx );
     734             :         }
     735             :         ELSE
     736             :         {
     737           0 :             set_val_Word32( L_Rsubband + grp_bound_fx[i], 0x0L, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
     738           0 :             IF( sub( i, GRP_SB - 1 ) == 0 )
     739             :             {
     740           0 :                 set_val_Word16( p2a_flags_fx + grp_bound_fx[i], 0, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
     741             :             }
     742             :         }
     743             :     }
     744           0 :     return;
     745             : }
     746             : 
     747         136 : void hq2_bit_alloc(
     748             :     const float band_energy[],  /* i  : band energy of each subband                 */
     749             :     const int16_t bands,        /* i  : total number of subbands in a frame         */
     750             :     Word32 L_Rk[],              /* i/o: Bit allocation/Adjusted bit alloc.          */
     751             :     int16_t *bit_budget_fx,     /* i/o: bit bugdet                                  */
     752             :     int16_t *p2a_flags,         /* i  : HF tonal indicator                          */
     753             :     const Word16 weight_fx,     /* i  : weight (Q13)                                */
     754             :     const int16_t band_width[], /* i  : Sub band bandwidth                          */
     755             :     const int16_t num_bits,     /* i  : available bits                              */
     756             :     const int16_t hqswb_clas,   /* i  : HQ2 class information                       */
     757             :     const int16_t bwidth,       /* i  : input bandwidth                             */
     758             :     const int16_t is_transient  /* i  : indicator HQ_TRANSIENT or not               */
     759             : )
     760             : {
     761             :     Word16 j, k;
     762             :     Word16 tmp;
     763             :     Word16 bit_budget_norm_fx;
     764             : 
     765             :     Word32 L_Rcalc, L_Ravg, L_Rcalc1;
     766             : 
     767             :     Word16 exp_normn, exp_normd;
     768             : 
     769             :     Word16 Rcnt_fx;
     770             : 
     771             :     Word16 div_fx;
     772             :     Word16 Qdiv;
     773             : 
     774             :     Word32 L_tmp;
     775             :     Word16 tmp_fx;
     776             : 
     777             :     Word32 L_maxxy;
     778             :     Word16 maxdex_fx;
     779             :     Word32 L_dummy;
     780             : 
     781             :     Word16 bit_budget_temp_fx;
     782             : 
     783             :     Word16 negflag;
     784             : 
     785             :     Word32 L_THR1, L_THR2, L_THR3;
     786             : 
     787             :     Word32 L_band_energy[BANDS_MAX];
     788             : 
     789        3248 :     for ( k = 0; k < bands; k++ )
     790             :     {
     791        3112 :         L_band_energy[k] = (Word32) ( band_energy[k] * (float) pow( 2.0f, SWB_BWE_LR_Qbe ) );
     792             :     }
     793             : 
     794         136 :     L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk );
     795         136 :     L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk );
     796         136 :     L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk );
     797             : 
     798             :     /* Init Rk to non-zero values for bands to be allocated bits */
     799         136 :     IF( sub( num_bits, HQ_16k40_BIT ) <= 0 )
     800             :     {
     801         136 :         set_val_Word32( L_Rk, (Word32) ( C1_QRk ), bands ); /* 1<<SWB_BWE_LR_QRk */
     802             : 
     803         136 :         test();
     804         136 :         IF( is_transient && sub( bands, 32 ) == 0 )
     805             :         {
     806          12 :             L_Rk[6] = 0x0L;
     807          12 :             move32();
     808          12 :             L_Rk[7] = 0x0L;
     809          12 :             move32();
     810          12 :             L_Rk[14] = 0x0L;
     811          12 :             move32();
     812          12 :             L_Rk[15] = 0x0L;
     813          12 :             move32();
     814          12 :             L_Rk[22] = 0x0L;
     815          12 :             move32();
     816          12 :             L_Rk[23] = 0x0L;
     817          12 :             move32();
     818          12 :             L_Rk[30] = 0x0L;
     819          12 :             move32();
     820          12 :             L_Rk[31] = 0x0L;
     821          12 :             move32();
     822             :         }
     823             :     }
     824             :     ELSE
     825             :     {
     826             :         /*mvs2r( p2a_flags, Rk, bands ); */
     827           0 :         FOR( k = 0; k < bands; k++ )
     828             :         {
     829           0 :             L_Rk[k] = L_shl( L_deposit_l( p2a_flags[k] ), SWB_BWE_LR_QRk );
     830             :         }
     831             :     }
     832             : 
     833         136 :     L_Rcalc = 0x0L;
     834         136 :     move32();
     835         136 :     L_Rcalc1 = 0x0L;
     836         136 :     move32();
     837             : 
     838         984 :     FOR( j = 0; j < bands; j++ )
     839             :     {
     840         984 :         Rcnt_fx = 0;
     841         984 :         move16();
     842         984 :         L_Ravg = 0x0L;
     843         984 :         move32();
     844             : 
     845       23032 :         FOR( k = 0; k < bands; k++ )
     846             :         {
     847       22048 :             IF( L_Rk[k] > 0 )
     848             :             {
     849       18120 :                 L_Ravg = L_add( L_Ravg, L_shl( L_band_energy[k], sub( SWB_BWE_LR_QRk, SWB_BWE_LR_Qbe ) ) ); /* SWB_BWE_LR_QRk-SWB_BWE_LR_Qbe */
     850       18120 :                 Rcnt_fx = add( Rcnt_fx, 1 );
     851             :             }
     852             :         }
     853             :         /* Ravg Qband_energy */
     854             : 
     855             :         /*L_Ravg /= Rcnt; */
     856         984 :         exp_normd = norm_l( L_Ravg );
     857         984 :         exp_normd = sub( exp_normd, 1 );
     858         984 :         exp_normn = norm_s( Rcnt_fx );
     859             : 
     860         984 :         tmp = shl( Rcnt_fx, exp_normn );
     861         984 :         tmp = s_max( tmp, 1 );
     862         984 :         IF( L_Ravg > 0 )
     863             :         {
     864         984 :             div_fx = div_l( L_shl( L_Ravg, exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
     865             :         }
     866             :         ELSE
     867             :         {
     868           0 :             div_fx = div_l( L_shl( L_abs( L_Ravg ), exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
     869           0 :             div_fx = negate( div_fx );
     870             :         }
     871             : 
     872             :         /*Qdiv = QRk+exp_normd-(exp_normn)-1; */
     873         984 :         Qdiv = sub( sub( add( SWB_BWE_LR_QRk, exp_normd ), exp_normn ), 1 );
     874             : 
     875         984 :         L_Ravg = L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) );
     876             : 
     877         984 :         exp_normd = norm_s( *bit_budget_fx );
     878         984 :         exp_normd = sub( exp_normd, 1 );
     879         984 :         bit_budget_norm_fx = shl( *bit_budget_fx, exp_normd );
     880         984 :         div_fx = 0;
     881         984 :         move16();
     882             : 
     883         984 :         test();
     884         984 :         IF( bit_budget_norm_fx > 0 && sub( bit_budget_norm_fx, tmp ) < 0 )
     885             :         {
     886         984 :             div_fx = div_s( bit_budget_norm_fx, tmp );
     887             :         }
     888         984 :         Qdiv = add( sub( exp_normd, exp_normn ), 15 );
     889             : 
     890       23032 :         FOR( k = 0; k < bands; k++ )
     891             :         {
     892       22048 :             IF( L_Rk[k] > 0 )
     893             :             {
     894             :                 /*Rk[k] = ((float) *bit_budget / Rcnt + weight * (band_energy[k] - Ravg)); */
     895             : 
     896       18120 :                 L_tmp = Mpy_32_16( L_sub( L_shl( L_band_energy[k], sub( SWB_BWE_LR_QRk, SWB_BWE_LR_Qbe ) ), L_Ravg ), weight_fx ); /* SWB_BWE_LR_QRk + Q13 - 15 */
     897       18120 :                 L_tmp = L_shl( L_tmp, 2 );                                                                                         /* -> SWB_BWE_LR_QRk */
     898             : 
     899       18120 :                 L_Rk[k] = L_add( L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) ), L_tmp );
     900             :             }
     901             :         }
     902             : 
     903         984 :         negflag = 0;
     904         984 :         move16();
     905         984 :         L_Rcalc = 0;
     906         984 :         move32();
     907       23032 :         FOR( k = 0; k < bands; k++ )
     908             :         {
     909       22048 :             IF( L_sub( L_Rk[k], MIN_BITS_FIX ) < 0 )
     910             :             {
     911         120 :                 L_Rk[k] = 0x0L;
     912         120 :                 move32();
     913         120 :                 negflag = 1;
     914         120 :                 move16();
     915             :             }
     916       22048 :             L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
     917             :         }
     918             : 
     919             :         /* prune noiselike bands with low allocation */
     920         984 :         IF( sub( num_bits, HQ_16k40_BIT ) <= 0 && negflag == 0 )
     921             :         {
     922         944 :             L_maxxy = 0;
     923         944 :             move32();
     924         944 :             maxdex_fx = -1;
     925         944 :             move16();
     926         944 :             L_Rcalc = 0;
     927         944 :             move32();
     928             : 
     929             :             /* find worst under-allocation */
     930       21952 :             FOR( k = sub( bands, 1 ); k >= 0; k-- )
     931             :             {
     932             :                 /* dummy = ((float) min (band_width[k], max (12, band_width[k] / 4))) - Rk[k];                                                                                                                                                                                              */
     933       21008 :                 tmp_fx = s_min( band_width[k], s_max( 12, shr( band_width[k], 2 ) ) );
     934       21008 :                 L_dummy = L_sub( L_shl( L_deposit_l( tmp_fx ), SWB_BWE_LR_QRk ), L_Rk[k] ); /*SWB_BWE_LR_QRk */
     935       21008 :                 test();
     936       21008 :                 test();
     937       21008 :                 test();
     938       21008 :                 IF( p2a_flags[k] == 0 && L_sub( L_dummy, L_maxxy ) > 0 && L_Rk[k] > 0 )
     939             :                 {
     940         584 :                     maxdex_fx = k;
     941         584 :                     move16();
     942         584 :                     L_maxxy = L_dummy;
     943         584 :                     move32(); /*SWB_BWE_LR_QRk */
     944             :                 }
     945             :             }
     946             : 
     947             :             /* prune worst allocation and recalculate total allocation */
     948         944 :             IF( sub( maxdex_fx, -1 ) > 0 )
     949             :             {
     950         560 :                 L_Rk[maxdex_fx] = 0;
     951         560 :                 move32();
     952             :             }
     953       21952 :             FOR( k = 0; k < bands; k++ )
     954             :             {
     955       21008 :                 L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
     956             :             }
     957             :         }
     958         984 :         test();
     959         984 :         IF( L_sub( L_Rcalc, L_Rcalc1 ) == 0 && sub( bwidth, SWB ) == 0 )
     960             :         {
     961             :             /* Reallocate bits to individual subbands for HQ_NORMAL mode */
     962             :             /* if bits allocated to subbands areless than predefined threshold */
     963         192 :             IF( sub( hqswb_clas, HQ_NORMAL ) == 0 && sub( num_bits, HQ_16k40_BIT ) < 0 )
     964             :             {
     965         180 :                 L_dummy = 0x0L;
     966         180 :                 move32();
     967        4140 :                 FOR( k = 0; k < bands; k++ )
     968             :                 {
     969        3960 :                     test();
     970        3960 :                     test();
     971        3960 :                     test();
     972        3960 :                     test();
     973        3960 :                     test();
     974        3960 :                     IF( sub( k, 11 ) < 0 && L_sub( L_Rk[k], L_THR1 ) < 0 )
     975             :                     {
     976         248 :                         L_Rk[k] = 0x0L;
     977         248 :                         move32();
     978             :                     }
     979        3712 :                     ELSE IF( sub( k, 11 ) >= 0 && sub( k, 16 ) < 0 && L_sub( L_Rk[k], L_THR2 ) < 0 )
     980             :                     {
     981          88 :                         L_Rk[k] = 0x0L;
     982          88 :                         move32();
     983             :                     }
     984        3624 :                     ELSE IF( sub( k, 16 ) >= 0 && sub( k, bands ) < 0 && L_sub( L_Rk[k], L_THR3 ) < 0 )
     985             :                     {
     986         876 :                         L_Rk[k] = 0x0L;
     987         876 :                         move32();
     988             :                     }
     989             : 
     990        3960 :                     L_dummy = L_add( L_dummy, L_Rk[k] );
     991             :                 }
     992             : 
     993         180 :                 IF( L_sub( L_dummy, L_Rcalc ) == 0 )
     994             :                 {
     995         132 :                     test();
     996         132 :                     IF( sub( hqswb_clas, HQ_NORMAL ) == 0 && sub( num_bits, HQ_16k40_BIT ) < 0 )
     997             :                     {
     998         132 :                         bit_budget_temp_fx = *bit_budget_fx;
     999         132 :                         move16();
    1000         660 :                         FOR( k = 0; k < NB_SWB_SUBBANDS; k++ )
    1001             :                         {
    1002         528 :                             IF( p2a_flags[bands - NB_SWB_SUBBANDS + k] == 1 && L_Rk[bands - NB_SWB_SUBBANDS + k] == 0.0f )
    1003             :                             {
    1004           8 :                                 p2a_flags[bands - NB_SWB_SUBBANDS + k] = 0;
    1005           8 :                                 move16();
    1006           8 :                                 bit_budget_temp_fx = sub( bit_budget_temp_fx, bits_lagIndices_modeNormal[k] );
    1007             :                             }
    1008             :                         }
    1009             : 
    1010         132 :                         IF( sub( bit_budget_temp_fx, *bit_budget_fx ) < 0 )
    1011             :                         {
    1012           8 :                             *bit_budget_fx = bit_budget_temp_fx;
    1013           8 :                             move16();
    1014             :                             /* a negative *bit_budget_fx may occur here due to Bit Errors              */
    1015             :                             /* handled outside this function to properly set flag: st_fx->BER_detect */
    1016             :                         }
    1017         124 :                         ELSE IF( sub( bit_budget_temp_fx, *bit_budget_fx ) == 0 )
    1018             :                         {
    1019         124 :                             BREAK;
    1020             :                         }
    1021             :                     }
    1022             :                     ELSE
    1023             :                     {
    1024             :                         BREAK;
    1025             :                     }
    1026             :                 }
    1027             :             }
    1028             :             ELSE
    1029             :             {
    1030             :                 BREAK;
    1031             :             }
    1032             :         }
    1033         792 :         ELSE IF( L_sub( L_Rcalc, L_Rcalc1 ) == 0 && sub( bwidth, SWB ) != 0 )
    1034             :         {
    1035           0 :             BREAK;
    1036             :         }
    1037             : 
    1038         848 :         L_Rcalc1 = L_Rcalc;
    1039         848 :         move32();
    1040             :     }
    1041         136 :     return;
    1042             : }
    1043             : 
    1044             : #undef WMC_TOOL_SKIP

Generated by: LCOV version 1.14