LCOV - code coverage report
Current view: top level - lib_com - hq2_bit_alloc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 435 469 92.8 %
Date: 2025-11-01 09:50:07 Functions: 4 4 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 "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         100 : 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         100 :     test();
      87         100 :     IF( n == 0 || d == 0 )
      88             :     {
      89           0 :         return 0;
      90             :     }
      91             : 
      92         100 :     norm_n = norm_s( n );
      93         100 :     norm_n = sub( norm_n, 1 );
      94         100 :     ns = shl( n, norm_n );
      95             : 
      96         100 :     norm_d = norm_s( d );
      97         100 :     ds = shl( d, norm_d );
      98             : 
      99         100 :     tmp = div_s( ns, ds );
     100         100 :     res = shr( tmp, add( sub( norm_n, norm_d ), 15 ) );
     101             : 
     102         100 :     return res;
     103             : }
     104             : 
     105             : 
     106             : /*-------------------------------------------------------------------*
     107             :  * Bits2indvsb()
     108             :  *
     109             :  * Bit allocation to individual SB's in a group
     110             :  *-------------------------------------------------------------------*/
     111         300 : 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         300 :     band_num_fx = sub( end_band, start_band );
     152         300 :     L_y_ptr = L_be + start_band;
     153         300 :     L_R_ptr = L_Rsubband + start_band;
     154         300 :     p2aflags_fx_ptr = p2aflags_fx + start_band;
     155             : 
     156        2100 :     FOR( i = 0; i < band_num_fx; i++ )
     157             :     {
     158        1800 :         y_index_fx[i] = extract_h( L_shr( L_y_ptr[i], sub( SWB_BWE_LR_Qbe, 16 ) ) );
     159        1800 :         move16();
     160        1800 :         index_fx[i] = i;
     161        1800 :         move16();
     162             :     }
     163             : 
     164             : 
     165             :     /* Rearrange norm vector in decreasing order */
     166         300 :     reordvct( y_index_fx, band_num_fx, index_fx );
     167             : 
     168         300 :     be_sum_fx = 0;
     169         300 :     move16();
     170         300 :     be_cnt_fx = 0;
     171         300 :     move16();
     172        2100 :     FOR( j = 0; j < band_num_fx; j++ )
     173             :     {
     174        1800 :         test();
     175        1800 :         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        1800 :             L_R_temp[j] = C1_QRk;
     185        1800 :             move32(); /* filled not zero value */
     186        1800 :             be_cnt_fx = add( be_cnt_fx, 1 );
     187             :         }
     188             :     }
     189             : 
     190         300 :     i = sub( be_cnt_fx, 1 );
     191        2100 :     FOR( k = 0; k <= i; k++ )
     192             :     {
     193        1800 :         test();
     194        1800 :         if ( L_R_temp[k] > 0 )
     195             :         {
     196        1800 :             be_sum_fx = add( be_sum_fx, y_index_fx[k] );
     197             :         }
     198             :     }
     199         300 :     QBavg = 0;
     200         300 :     move16();
     201             : 
     202             :     /*Ravg = (float) be_sum/be_cnt;*/
     203         300 :     Ravg_fx = 0;
     204         300 :     move16();
     205         300 :     QRavg = 0;
     206         300 :     move16();
     207         300 :     IF( be_cnt_fx != 0x0 )
     208             :     {
     209         300 :         exp_normn = norm_s( be_sum_fx );
     210         300 :         exp_normn = sub( exp_normn, 1 );
     211         300 :         exp_normd = norm_s( be_cnt_fx );
     212         300 :         Ravg_fx = div_s( shl( be_sum_fx, exp_normn ), shl( be_cnt_fx, exp_normd ) );
     213             : 
     214         300 :         Ravg_fx = shr( Ravg_fx, 2 ); /* safe shift */
     215         300 :         QRavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
     216             :     }
     217             : 
     218         300 :     enr_diffcnt_fx = 0;
     219         300 :     move16();
     220         300 :     th_5_fx = shl( 5, QRavg );
     221        2100 :     FOR( j = 0; j < be_cnt_fx; j++ )
     222             :     {
     223        1800 :         IF( sub( abs_s( sub( Ravg_fx, shl( y_index_fx[j], QRavg ) ) ), th_5_fx ) > 0 )
     224             :         {
     225          16 :             enr_diffcnt_fx = add( enr_diffcnt_fx, 1 );
     226             :         }
     227             :     }
     228             : 
     229         300 :     IF( enr_diffcnt_fx > 0 )
     230             :     {
     231           8 :         scale_fact_fx = 11468;
     232           8 :         move16(); /* 0.35f 11468.8(Q15) */
     233             :     }
     234             :     ELSE
     235             :     {
     236         292 :         scale_fact_fx = 19661;
     237         292 :         move16(); /* 0.60f 19660.8(Q15) */
     238             :     }
     239             : 
     240             :     /* Bits allocation to individual SB's in a group based on Band Energies */
     241         364 :     FOR( j = 0; j < be_cnt_fx; j++ )
     242             :     {
     243         364 :         Rcnt_fx = add( i, 1 );
     244             : 
     245             :         /* Ravg = (float) be_sum/Rcnt; */
     246         364 :         exp_normn = norm_s( be_sum_fx );
     247         364 :         exp_normn = sub( exp_normn, 1 );
     248         364 :         exp_normd = norm_s( Rcnt_fx );
     249         364 :         Ravg_fx = div_s( shl( be_sum_fx, exp_normn ), shl( Rcnt_fx, exp_normd ) );
     250         364 :         Ravg_fx = shr( Ravg_fx, 2 ); /* safe shift */
     251         364 :         QRavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
     252             : 
     253         364 :         test();
     254         364 :         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         364 :         Bits_avg_fx = 0;
     262         364 :         move16();
     263         364 :         QBavg = 0;
     264         364 :         move16();
     265         364 :         IF( Bits != 0 )
     266             :         {
     267         364 :             exp_normn = norm_s( Bits );
     268         364 :             exp_normn = sub( exp_normn, 1 );
     269         364 :             exp_normd = norm_s( be_sum_fx );
     270         364 :             Bits_avg_fx = div_s( shl( Bits, exp_normn ), shl( be_sum_fx, exp_normd ) );
     271         364 :             Bits_avg_fx = shr( Bits_avg_fx, 2 ); /* safe_shift */
     272         364 :             QBavg = add( sub( exp_normn, exp_normd ), 15 - 2 );
     273             :         }
     274        2458 :         FOR( k = 0; k <= i; k++ )
     275             :         {
     276        2094 :             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        2094 :                 L_temp1 = L_mult( Bits_avg_fx, y_index_fx[k] );                                                                                             /* QBavg+1 */
     281        2094 :                 L_temp2 = L_mult( scale_fact_fx, sub( shl( y_index_fx[k], QRavg ), Ravg_fx ) );                                                             /* 15+QRavg+1 */
     282        2094 :                 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         364 :         IF( L_sub( L_R_temp[i], L_Bits_needed ) < 0 )
     286             :         {
     287          64 :             L_R_temp[i] = 0x0L;
     288          64 :             move32();
     289             : 
     290          64 :             p2aflags_fx_ptr[index_fx[i]] = 0;
     291          64 :             move16();
     292             : 
     293             :             /* be_sum -= y_index[i]; */
     294          64 :             be_sum_fx = sub( be_sum_fx, y_index_fx[i] );
     295             : 
     296          64 :             i = sub( i, 1 );
     297             :         }
     298             :         ELSE
     299             :         {
     300         300 :             BREAK;
     301             :         }
     302             :     }
     303             : 
     304             :     /* Rearrange the bit allocation to align with original */
     305        2100 :     FOR( k = 0; k < band_num_fx; k++ )
     306             :     {
     307        1800 :         j = index_fx[k];
     308        1800 :         move16();
     309        1800 :         L_R_ptr[j] = L_R_temp[k];
     310        1800 :         move32();
     311             :     }
     312             : 
     313         300 :     return;
     314             : }
     315             : 
     316             : /*-------------------------------------------------------------------*
     317             :  * hq2_bit_alloc_har()
     318             :  *
     319             :  * Bit allocation mechanism for HQ_HARMONIC mode
     320             :  *-------------------------------------------------------------------*/
     321             : 
     322         100 : 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         100 :     Overflow = 0;
     381             : #endif
     382             : 
     383         100 :     grp_rngmax_fx[0] = 0;
     384         100 :     grp_rngmax_fx[1] = 0;
     385             : 
     386        2300 :     for ( i = 0; i < N_fx; i++ )
     387             :     {
     388        2200 :         L_y[i] = (Word32) ( y[i] * (float) pow( 2.0f, SWB_BWE_LR_Qbe ) );
     389             :     }
     390             : 
     391         100 :     L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk );
     392         100 :     L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk );
     393         100 :     L_THR3 = L_shl( L_deposit_l( THR3 ), SWB_BWE_LR_QRk );
     394             : 
     395         100 :     set_val_Word16( Bits_grp_fx, 0, GRP_SB );
     396             : 
     397             :     /* Initialize subbands bits allocation vector based on harmonic bands */
     398         100 :     harmonic_band_fx = add( sub( N_fx, p2a_bands_fx ), 1 );
     399             :     /*printf("harmonic_band= %d %d\n", harmonic_band, harmonic_band_fx);*/
     400        2300 :     FOR( k = 0; k < N_fx; k++ )
     401             :     {
     402        2200 :         L_Rsubband[k] = (Word32) ( C1_QRk );
     403        2200 :         move32(); /* Constant Value */
     404        2200 :         L_temp_band_energy[k] = L_y[k];
     405        2200 :         move32(); /* SWB_BWE_LR_Qbe */
     406             :     }
     407         100 :     final_gr_fact_pos_fx = 2;
     408         100 :     move16();
     409         100 :     bits_fact_fx = C1_Qbf;
     410         100 :     move16();
     411         100 :     bits_fact1_fx = C1_Qbf;
     412         100 :     move16();
     413             : 
     414         100 :     gmax_range_fx[0] = G1_RANGE;
     415         100 :     move16();
     416         100 :     gmax_range_fx[1] = G1G2_RANGE;
     417         100 :     move16();
     418             : 
     419         100 :     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         100 :     grp_rngmax_fx[1] = 16;
     427         100 :     move16();
     428         100 :     grp_rngmax_fx[0] = 7;
     429         100 :     move16();
     430         100 :     temp_fx = 0;
     431         100 :     move16();
     432         300 :     FOR( i = 0; i < 2; i++ )
     433             :     {
     434         200 :         j_fx = gmax_range_fx[i];
     435         200 :         move16();
     436         200 :         k_fx = 0;
     437         200 :         move16();
     438         200 :         test();
     439         278 :         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          78 :             test();
     442          78 :             k_fx = add( k_fx, 1 );
     443          78 :             j_fx = add( j_fx, 1 );
     444             :         }
     445             : 
     446         200 :         temp_fx = k_fx;
     447         200 :         move16();
     448         200 :         IF( sub( temp_fx, 1 ) > 0 )
     449             :         {
     450           8 :             FOR( temp_fx = 2; temp_fx <= k_fx; )
     451             :             {
     452           8 :                 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           4 :                     BREAK;
     455             :                 }
     456           4 :                 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           4 :                     temp_fx = add( temp_fx, 1 );
     459           4 :                     IF( sub( temp_fx, k_fx ) > 0 )
     460             :                     {
     461           0 :                         temp_fx = sub( temp_fx, 1 );
     462           0 :                         BREAK;
     463             :                     }
     464             :                 }
     465             :             }
     466             : 
     467           4 :             gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx );
     468             :         }
     469             :         ELSE
     470             :         {
     471         196 :             gmax_range_fx[i] = add( gmax_range_fx[i], temp_fx );
     472             :         }
     473             :     }
     474             : 
     475         100 :     grp_bound_fx[0] = 0;
     476         100 :     move16();
     477         300 :     FOR( i = 1; i < GRP_SB - 1; i++ )
     478             :     {
     479         200 :         grp_bound_fx[i] = gmax_range_fx[i - 1];
     480         200 :         move16();
     481             :     }
     482         100 :     grp_bound_fx[i] = harmonic_band_fx;
     483         100 :     move16();
     484         100 :     grp_bound_fx[i + 1] = N_fx;
     485         100 :     move16();
     486             : 
     487             : 
     488         500 :     FOR( i = 0; i < GRP_SB; i++ )
     489             :     {
     490         400 :         L_Ravg_sub[i] = 0x0L;
     491         400 :         move32();
     492        2600 :         FOR( j = grp_bound_fx[i]; j < grp_bound_fx[i + 1]; j++ )
     493             :         {
     494        2200 :             IF( L_temp_band_energy[j] > 0x0L )
     495             :             {
     496        2200 :                 L_Ravg_sub[i] = L_add( L_Ravg_sub[i], L_temp_band_energy[j] );
     497        2200 :                 move32();
     498             :             }
     499             :         }
     500             :     }
     501             : 
     502         100 :     L_temp_band_energydiff[0] = L_temp_band_energy[0];
     503         100 :     move32();
     504        1800 :     FOR( j = 1; j < harmonic_band_fx; j++ )
     505             :     {
     506        1700 :         L_temp_band_energydiff[j] = L_abs( L_sub( L_temp_band_energy[j], L_temp_band_energy[j - 1] ) );
     507        1700 :         move32();
     508             :     }
     509             : 
     510         100 :     G1_BE_DIFF_POS_fx = 0;
     511         100 :     move16();
     512         100 :     L_G1_BE_DIFF_VAL = 0x0L;
     513         100 :     move32();
     514             : 
     515        1800 :     FOR( j = 1; j < harmonic_band_fx; j++ )
     516             :     {
     517        1700 :         IF( L_sub( L_temp_band_energydiff[j], L_G1_BE_DIFF_VAL ) > 0 )
     518             :         {
     519         364 :             G1_BE_DIFF_POS_fx = j;
     520         364 :             move16();
     521         364 :             L_G1_BE_DIFF_VAL = L_temp_band_energydiff[j];
     522         364 :             move32();
     523             :         }
     524             :     }
     525             : 
     526         100 :     test();
     527         100 :     test();
     528         100 :     IF( sub( G1_BE_DIFF_POS_fx, gmax_range_fx[0] ) < 0 && G1_BE_DIFF_POS_fx > 0 )
     529             :     {
     530          10 :         final_gr_fact_pos_fx = 0;
     531          10 :         move16();
     532             :     }
     533          90 :     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          90 :         final_gr_fact_pos_fx = 1;
     536          90 :         move16();
     537             :     }
     538             :     ELSE
     539             :     {
     540           0 :         final_gr_fact_pos_fx = 2;
     541           0 :         move16();
     542             :     }
     543             : 
     544         100 :     test();
     545         100 :     IF( final_gr_fact_pos_fx == 0 || sub( final_gr_fact_pos_fx, 1 ) == 0 )
     546             :     {
     547         100 :         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         100 :             bits_fact_fx = BITS_FACT_1p05;
     557         100 :             move16(); /* 1.05f; */ /* G1 */
     558         100 :             bits_fact1_fx = BITS_FACT_0p97;
     559         100 :             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         500 :     FOR( i = 0; i < sub( N_fx, harmonic_band_fx ); i++ )
     581             :     {
     582         400 :         y_index_fx[i] = extract_h( L_shl( L_temp_band_energy[harmonic_band_fx + i], sub( 16, SWB_BWE_LR_Qbe ) ) );
     583         400 :         move16();
     584         400 :         index_fx[i] = add( harmonic_band_fx, i );
     585         400 :         move16();
     586             :     }
     587             : 
     588         100 :     reordvct( y_index_fx, sub( N_fx, harmonic_band_fx ), index_fx );
     589             : 
     590             :     /* Log2 */
     591         100 :     L_temp = L_deposit_l( band_width_fx[index_fx[0]] );
     592         100 :     exp = norm_l( L_temp );
     593         100 :     frac = Log2_norm_lc( L_shl( L_temp, exp ) );
     594         100 :     exp = sub( 30, exp );
     595         100 :     L_temp = L_Comp( exp, frac );
     596             :     /* ceil */
     597         100 :     if ( L_and( 0x0000ffff, L_temp ) > 0 )
     598             :     {
     599         100 :         L_temp = L_add( L_temp, 0x00010000 );
     600             :     }
     601         100 :     esthf_bits_fx = extract_h( L_temp );
     602             : 
     603         100 :     L_grp_thr[0] = L_THR1;
     604         100 :     move32();
     605         100 :     L_grp_thr[1] = L_THR2;
     606         100 :     move32();
     607         100 :     L_grp_thr[2] = L_THR3;
     608         100 :     move32();
     609         100 :     L_grp_thr[3] = L_shl( L_deposit_l( esthf_bits_fx ), SWB_BWE_LR_QRk );
     610         100 :     move16();
     611             : 
     612         100 :     L_norm_sum = 1;
     613         100 :     move32();
     614         400 :     FOR( i = 0; i < 3; i++ )
     615             :     {
     616         300 :         L_norm_sum = L_add( L_norm_sum, L_Ravg_sub[i] );
     617         300 :         move32();
     618             :     }
     619             : 
     620             :     /*reserve bits for HF coding */
     621         100 :     L_temp = L_add( L_norm_sum, L_Ravg_sub[GRP_SB - 1] );
     622         100 :     exp_normn = norm_l( L_temp );
     623         100 :     exp_normn = sub( exp_normn, 1 );
     624         100 :     exp_normd = norm_s( N_fx );
     625             : 
     626         100 :     div_fx = div_l( L_shl( L_temp, exp_normn ), shl( N_fx, exp_normd ) );                    /* (Qbe+exp_normn)-(0+exp_normd)-1) */
     627         100 :     L_norm_sum_avg = L_shr( L_deposit_h( div_fx ), add( sub( exp_normn, exp_normd ), 15 ) ); /* -> Qbe */
     628             : 
     629         100 :     exp_norm = norm_l( L_norm_sum );
     630         100 :     norm_sum_fx = extract_h( L_shl( L_norm_sum, exp_norm ) ); /* SWB_BWE_LR_Qbe+exp_norm-16 */
     631         100 :     Qns = sub( add( SWB_BWE_LR_Qbe, exp_norm ), 16 );
     632             : 
     633         100 :     Inv_norm_sum_fx = div_s( 0x4000 /* Q15 */, norm_sum_fx );
     634         100 :     QIns = sub( 31, exp_norm ); /* 14 - (14+exp_norm-16) + 15 */
     635             : 
     636         100 :     grp_bit_avg_fx = div_s_ss( B_fx, GRP_SB ); /* Q0 */
     637             : 
     638         100 :     exp_normd = norm_s( p2a_bands_fx );
     639         100 :     Inv_p2a_bands_fx = div_s( 0x3fff, shl( p2a_bands_fx, exp_normd ) ); /* 14-exp_normd+15 */
     640         100 :     QIpb = sub( 29, exp_normd );
     641             : 
     642         100 :     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         100 :     L_norm_diff = L_sub( L_temp, L_norm_sum_avg ); /* Qbe */
     644             : 
     645         100 :     L_temp = Mpy_32_16( L_Ravg_sub[GRP_SB - 1], sub( GRP_SB, 1 ) ); /* Qbe+0+1 */
     646         100 :     L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );                  /* Qbe+1+QIpb+1 */
     647             : #ifdef BASOP_NOGLOB
     648         100 :     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         100 :     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         100 :     exp_normn = norm_s( norm_sum_fx );
     655         100 :     exp_normn = sub( exp_normn, 1 );
     656         100 :     exp_normd = norm_s( harmonic_band_fx );
     657             : 
     658         100 :     div_fx = div_s( shl( norm_sum_fx, exp_normn ), shl( harmonic_band_fx, exp_normd ) );
     659         100 :     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         100 :     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           8 :         bits_allocweigh_fx = 6554;
     664           8 :         move16(); /* 0.2 6553.6(Q15) */
     665           8 :         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           8 :         L_temp = L_mult( grp_bit_avg_fx, Inv_p2a_bands_fx );    /* Q0+QIpb+1 */
     674           8 :         L_temp2 = Mpy_32_16( L_norm_diff, bits_allocweigh_fx ); /* Qbe+Q15-15 */
     675             : 
     676           8 :         L_temp = L_shr( L_temp, add( QIpb, 1 ) );
     677           8 :         L_temp = L_add( L_shl( L_temp, SWB_BWE_LR_Qbe ), L_temp2 );
     678             : 
     679           8 :         Bits_grp_fx[GRP_SB - 1] = extract_h( L_shl( L_temp, sub( 16, SWB_BWE_LR_Qbe ) ) );
     680           8 :         Bits_grp_fx[GRP_SB - 1] = s_min( Bits_grp_fx[GRP_SB - 1], 10 );
     681             : 
     682           8 :         test();
     683           8 :         if ( sub( Bits_grp_fx[GRP_SB - 1], esthf_bits_fx ) < 0 )
     684             :         {
     685           8 :             Bits_grp_fx[GRP_SB - 1] = 0;
     686           8 :             move16();
     687             :         }
     688           8 :         B_fx = sub( B_fx, Bits_grp_fx[GRP_SB - 1] );
     689             :     }
     690             : 
     691         100 :     exp_shift = sub( add( SWB_BWE_LR_Qbe, QIns ), 47 ); /* (SWB_BWE_LR_Qbe+14+1+QIns-15-16) */
     692         100 :     exp_norm = norm_s( B_fx );
     693         100 :     B_norm_fx = shl( B_fx, exp_norm );
     694         100 :     exp_shift = add( exp_shift, exp_norm );
     695             : 
     696         100 :     IF( sub( final_gr_fact_pos_fx, 1 ) == 0 )
     697             :     {
     698          90 :         L_temp = Mpy_32_16( L_Ravg_sub[1], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
     699          90 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     700          90 :         Bits_grp_fx[1] = extract_h( L_shr( L_temp, exp_shift ) );
     701             : 
     702          90 :         L_temp = Mpy_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
     703          90 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     704          90 :         Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) );
     705             : 
     706          90 :         Bits_grp_fx[0] = sub( sub( B_fx, Bits_grp_fx[1] ), Bits_grp_fx[2] );
     707             :     }
     708             :     ELSE
     709             :     {
     710          10 :         L_temp = Mpy_32_16( L_Ravg_sub[0], extract_h( L_mult( bits_fact_fx, B_norm_fx ) ) );
     711          10 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     712          10 :         Bits_grp_fx[0] = extract_h( L_shr( L_temp, exp_shift ) );
     713             : 
     714          10 :         L_temp = Mpy_32_16( L_Ravg_sub[2], extract_h( L_mult( bits_fact1_fx, B_norm_fx ) ) );
     715          10 :         L_temp = Mpy_32_16( L_temp, Inv_norm_sum_fx );
     716          10 :         Bits_grp_fx[2] = extract_h( L_shr( L_temp, exp_shift ) );
     717             : 
     718          10 :         Bits_grp_fx[1] = sub( sub( B_fx, Bits_grp_fx[0] ), Bits_grp_fx[2] );
     719             :     }
     720             : 
     721         100 :     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         500 :     FOR( i = 0; i < GRP_SB; i++ )
     730             :     {
     731         400 :         IF( Bits_grp_fx[i] > 0 )
     732             :         {
     733         300 :             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         100 :             set_val_Word32( L_Rsubband + grp_bound_fx[i], 0x0L, sub( grp_bound_fx[i + 1], grp_bound_fx[i] ) );
     738         100 :             IF( sub( i, GRP_SB - 1 ) == 0 )
     739             :             {
     740         100 :                 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         100 :     return;
     745             : }
     746             : 
     747        6068 : 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      135236 :     for ( k = 0; k < bands; k++ )
     790             :     {
     791      129168 :         L_band_energy[k] = (Word32) ( band_energy[k] * (float) pow( 2.0f, SWB_BWE_LR_Qbe ) );
     792             :     }
     793             : 
     794        6068 :     L_THR1 = L_shl( L_deposit_l( THR1 ), SWB_BWE_LR_QRk );
     795        6068 :     L_THR2 = L_shl( L_deposit_l( THR2 ), SWB_BWE_LR_QRk );
     796        6068 :     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        6068 :     IF( sub( num_bits, HQ_16k40_BIT ) <= 0 )
     800             :     {
     801        6068 :         set_val_Word32( L_Rk, (Word32) ( C1_QRk ), bands ); /* 1<<SWB_BWE_LR_QRk */
     802             : 
     803        6068 :         test();
     804        6068 :         IF( is_transient && sub( bands, 32 ) == 0 )
     805             :         {
     806         150 :             L_Rk[6] = 0x0L;
     807         150 :             move32();
     808         150 :             L_Rk[7] = 0x0L;
     809         150 :             move32();
     810         150 :             L_Rk[14] = 0x0L;
     811         150 :             move32();
     812         150 :             L_Rk[15] = 0x0L;
     813         150 :             move32();
     814         150 :             L_Rk[22] = 0x0L;
     815         150 :             move32();
     816         150 :             L_Rk[23] = 0x0L;
     817         150 :             move32();
     818         150 :             L_Rk[30] = 0x0L;
     819         150 :             move32();
     820         150 :             L_Rk[31] = 0x0L;
     821         150 :             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        6068 :     L_Rcalc = 0x0L;
     834        6068 :     move32();
     835        6068 :     L_Rcalc1 = 0x0L;
     836        6068 :     move32();
     837             : 
     838       41003 :     FOR( j = 0; j < bands; j++ )
     839             :     {
     840       41003 :         Rcnt_fx = 0;
     841       41003 :         move16();
     842       41003 :         L_Ravg = 0x0L;
     843       41003 :         move32();
     844             : 
     845      928583 :         FOR( k = 0; k < bands; k++ )
     846             :         {
     847      887580 :             IF( L_Rk[k] > 0 )
     848             :             {
     849      760863 :                 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      760863 :                 Rcnt_fx = add( Rcnt_fx, 1 );
     851             :             }
     852             :         }
     853             :         /* Ravg Qband_energy */
     854             : 
     855             :         /*L_Ravg /= Rcnt; */
     856       41003 :         exp_normd = norm_l( L_Ravg );
     857       41003 :         exp_normd = sub( exp_normd, 1 );
     858       41003 :         exp_normn = norm_s( Rcnt_fx );
     859             : 
     860       41003 :         tmp = shl( Rcnt_fx, exp_normn );
     861       41003 :         tmp = s_max( tmp, 1 );
     862       41003 :         IF( L_Ravg > 0 )
     863             :         {
     864       40811 :             div_fx = div_l( L_shl( L_Ravg, exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
     865             :         }
     866             :         ELSE
     867             :         {
     868         192 :             div_fx = div_l( L_shl( L_abs( L_Ravg ), exp_normd ), tmp ); /* Qdiv = 14+exp_normd-(exp_normn)-1 */
     869         192 :             div_fx = negate( div_fx );
     870             :         }
     871             : 
     872             :         /*Qdiv = QRk+exp_normd-(exp_normn)-1; */
     873       41003 :         Qdiv = sub( sub( add( SWB_BWE_LR_QRk, exp_normd ), exp_normn ), 1 );
     874             : 
     875       41003 :         L_Ravg = L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) );
     876             : 
     877       41003 :         exp_normd = norm_s( *bit_budget_fx );
     878       41003 :         exp_normd = sub( exp_normd, 1 );
     879       41003 :         bit_budget_norm_fx = shl( *bit_budget_fx, exp_normd );
     880       41003 :         div_fx = 0;
     881       41003 :         move16();
     882             : 
     883       41003 :         test();
     884       41003 :         IF( bit_budget_norm_fx > 0 && sub( bit_budget_norm_fx, tmp ) < 0 )
     885             :         {
     886       41003 :             div_fx = div_s( bit_budget_norm_fx, tmp );
     887             :         }
     888       41003 :         Qdiv = add( sub( exp_normd, exp_normn ), 15 );
     889             : 
     890      928583 :         FOR( k = 0; k < bands; k++ )
     891             :         {
     892      887580 :             IF( L_Rk[k] > 0 )
     893             :             {
     894             :                 /*Rk[k] = ((float) *bit_budget / Rcnt + weight * (band_energy[k] - Ravg)); */
     895             : 
     896      760863 :                 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      760863 :                 L_tmp = L_shl( L_tmp, 2 );                                                                                         /* -> SWB_BWE_LR_QRk */
     898             : 
     899      760863 :                 L_Rk[k] = L_add( L_shr( L_deposit_l( div_fx ), sub( Qdiv, SWB_BWE_LR_QRk ) ), L_tmp );
     900             :             }
     901             :         }
     902             : 
     903       41003 :         negflag = 0;
     904       41003 :         move16();
     905       41003 :         L_Rcalc = 0;
     906       41003 :         move32();
     907      928583 :         FOR( k = 0; k < bands; k++ )
     908             :         {
     909      887580 :             IF( L_sub( L_Rk[k], MIN_BITS_FIX ) < 0 )
     910             :             {
     911        2494 :                 L_Rk[k] = 0x0L;
     912        2494 :                 move32();
     913        2494 :                 negflag = 1;
     914        2494 :                 move16();
     915             :             }
     916      887580 :             L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
     917             :         }
     918             : 
     919             :         /* prune noiselike bands with low allocation */
     920       41003 :         IF( sub( num_bits, HQ_16k40_BIT ) <= 0 && negflag == 0 )
     921             :         {
     922       40093 :             L_maxxy = 0;
     923       40093 :             move32();
     924       40093 :             maxdex_fx = -1;
     925       40093 :             move16();
     926       40093 :             L_Rcalc = 0;
     927       40093 :             move32();
     928             : 
     929             :             /* find worst under-allocation */
     930      912609 :             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      872516 :                 tmp_fx = s_min( band_width[k], s_max( 12, shr( band_width[k], 2 ) ) );
     934      872516 :                 L_dummy = L_sub( L_shl( L_deposit_l( tmp_fx ), SWB_BWE_LR_QRk ), L_Rk[k] ); /*SWB_BWE_LR_QRk */
     935      872516 :                 test();
     936      872516 :                 test();
     937      872516 :                 test();
     938      872516 :                 IF( p2a_flags[k] == 0 && L_sub( L_dummy, L_maxxy ) > 0 && L_Rk[k] > 0 )
     939             :                 {
     940       25213 :                     maxdex_fx = k;
     941       25213 :                     move16();
     942       25213 :                     L_maxxy = L_dummy;
     943       25213 :                     move32(); /*SWB_BWE_LR_QRk */
     944             :                 }
     945             :             }
     946             : 
     947             :             /* prune worst allocation and recalculate total allocation */
     948       40093 :             IF( sub( maxdex_fx, -1 ) > 0 )
     949             :             {
     950       24755 :                 L_Rk[maxdex_fx] = 0;
     951       24755 :                 move32();
     952             :             }
     953      912609 :             FOR( k = 0; k < bands; k++ )
     954             :             {
     955      872516 :                 L_Rcalc = L_add( L_Rcalc, L_Rk[k] ); /*SWB_BWE_LR_QRk */
     956             :             }
     957             :         }
     958       41003 :         test();
     959       41003 :         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        6802 :             IF( sub( hqswb_clas, HQ_NORMAL ) == 0 && sub( num_bits, HQ_16k40_BIT ) < 0 )
     964             :             {
     965        6652 :                 L_dummy = 0x0L;
     966        6652 :                 move32();
     967      153360 :                 FOR( k = 0; k < bands; k++ )
     968             :                 {
     969      146708 :                     test();
     970      146708 :                     test();
     971      146708 :                     test();
     972      146708 :                     test();
     973      146708 :                     test();
     974      146708 :                     IF( sub( k, 11 ) < 0 && L_sub( L_Rk[k], L_THR1 ) < 0 )
     975             :                     {
     976        1084 :                         L_Rk[k] = 0x0L;
     977        1084 :                         move32();
     978             :                     }
     979      145624 :                     ELSE IF( sub( k, 11 ) >= 0 && sub( k, 16 ) < 0 && L_sub( L_Rk[k], L_THR2 ) < 0 )
     980             :                     {
     981        1422 :                         L_Rk[k] = 0x0L;
     982        1422 :                         move32();
     983             :                     }
     984      144202 :                     ELSE IF( sub( k, 16 ) >= 0 && sub( k, bands ) < 0 && L_sub( L_Rk[k], L_THR3 ) < 0 )
     985             :                     {
     986       34697 :                         L_Rk[k] = 0x0L;
     987       34697 :                         move32();
     988             :                     }
     989             : 
     990      146708 :                     L_dummy = L_add( L_dummy, L_Rk[k] );
     991             :                 }
     992             : 
     993        6652 :                 IF( L_sub( L_dummy, L_Rcalc ) == 0 )
     994             :                 {
     995        5230 :                     test();
     996        5230 :                     IF( sub( hqswb_clas, HQ_NORMAL ) == 0 && sub( num_bits, HQ_16k40_BIT ) < 0 )
     997             :                     {
     998        5230 :                         bit_budget_temp_fx = *bit_budget_fx;
     999        5230 :                         move16();
    1000       26150 :                         FOR( k = 0; k < NB_SWB_SUBBANDS; k++ )
    1001             :                         {
    1002       20920 :                             IF( p2a_flags[bands - NB_SWB_SUBBANDS + k] == 1 && L_Rk[bands - NB_SWB_SUBBANDS + k] == 0.0f )
    1003             :                             {
    1004         240 :                                 p2a_flags[bands - NB_SWB_SUBBANDS + k] = 0;
    1005         240 :                                 move16();
    1006         240 :                                 bit_budget_temp_fx = sub( bit_budget_temp_fx, bits_lagIndices_modeNormal[k] );
    1007             :                             }
    1008             :                         }
    1009             : 
    1010        5230 :                         IF( sub( bit_budget_temp_fx, *bit_budget_fx ) < 0 )
    1011             :                         {
    1012         181 :                             *bit_budget_fx = bit_budget_temp_fx;
    1013         181 :                             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        5049 :                         ELSE IF( sub( bit_budget_temp_fx, *bit_budget_fx ) == 0 )
    1018             :                         {
    1019        5049 :                             BREAK;
    1020             :                         }
    1021             :                     }
    1022             :                     ELSE
    1023             :                     {
    1024             :                         BREAK;
    1025             :                     }
    1026             :                 }
    1027             :             }
    1028             :             ELSE
    1029             :             {
    1030             :                 BREAK;
    1031             :             }
    1032             :         }
    1033       34201 :         ELSE IF( L_sub( L_Rcalc, L_Rcalc1 ) == 0 && sub( bwidth, SWB ) != 0 )
    1034             :         {
    1035         869 :             BREAK;
    1036             :         }
    1037             : 
    1038       34935 :         L_Rcalc1 = L_Rcalc;
    1039       34935 :         move32();
    1040             :     }
    1041        6068 :     return;
    1042             : }
    1043             : 
    1044             : #undef WMC_TOOL_SKIP

Generated by: LCOV version 1.14