LCOV - code coverage report
Current view: top level - lib_com - gs_bitallocation_ivas_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 445 537 82.9 %
Date: 2025-05-03 01:55:50 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : #include <stdint.h>
       5             : #include "options.h" /* Compilation switches                   */
       6             : #include "cnst.h"    /* Common constants                       */
       7             : #include "rom_com.h" /* Static table prototypes                */
       8             : #include "prot_fx.h" /* Function prototypes                    */
       9             : #include "assert.h"  /* Debug prototypes                       */
      10             : #include "stl.h"
      11             : #include "ivas_prot_fx.h"
      12             : 
      13             : static void reajust_bits_fx( Word32 *bits_per_bands, const Word16 st_band, const Word16 end_band, const Word16 sum_bit_in, const Word16 bit_bdgt_in );
      14             : 
      15             : /*-------------------------------------------------------------------*
      16             :  * Local constants
      17             :  *-------------------------------------------------------------------*/
      18             : #define Q15_0_33             10922                     /* 0.33 */
      19             : #define Q18_0_1              26214                     /* 0.1  */
      20             : #define Q18_0_50             131072                    /* 0.50 */
      21             : #define Q18_0_75             196608                    /* 0.75 */
      22             : #define Q18_0_76             199229                    /* 76/100 */
      23             : #define Q18_1_0              262144                    /* 1.0 */
      24             : #define Q18_1_2              314573                    /* 1.2 */
      25             : #define Q18_112              29360128                  /* 112 */
      26             : #define Q18_DSR_NB_PULSE     1179648                   /* 4.5  */
      27             : #define Q18_1_5xDSR_NB_PULSE 1769472                   /* 1.5x4.5  */
      28             : #define Q18_2_0xDSR_NB_PULSE ( Q18_DSR_NB_PULSE << 1 ) /* 2.0x4.5  */
      29             : 
      30             : #define Q31_0_00125 2684355   /* 0.125/100  */
      31             : #define Q31_0_0125  26843546  /* 0.0125  */
      32             : #define Q31_0_015   32212255  /* 0.0125  */
      33             : #define Q31_0_02    42949673  /* 0.02  */
      34             : #define Q31_0_17    365072220 /* 0.17  */
      35             : #define Q31_0_23    493921239 /* 0.23  */
      36             : /*-----------------------------------------------------------------*
      37             :  * Local functions
      38             :  *-----------------------------------------------------------------*/
      39             : static Word16 Find_norm_inv_fx( const Word32 ToDivide, Word16 *e_div );
      40             : static Word16 Find_bit_alloc_IVAS_fx( const Word32 core_brate, const Word16 GSC_IVAS_mode, const Word16 Diff_len, const Word16 nb_tot_bands, Word16 *bit, Word16 *max_ener_band, Word16 *ener_vec, Word32 *bits_per_bands );
      41             : static Word32 Find_bit_frac_fx( const Word16 nb_band, const Word16 remaining_bits );
      42             : /*==================================================================================*/
      43             : /* FUNCTION : void bands_and_bit_alloc_ivas_fx();                                                                   */
      44             : /*----------------------------------------------------------------------------------*/
      45             : /* PURPOSE  :  AC mode (GSC) bands and bits allocation                              */
      46             : /*----------------------------------------------------------------------------------*/
      47             : /*    INPUT ARGUMENTS :                                                                                                         */
      48             : /* _ (Word16) cor_strong_limit   : HF correlation                                                   */
      49             : /* _ (Word16) noise_lev          : dwn scaling factor                                Q0         */
      50             : /* _ (Word32) core_brate         : core codec used                                               Q0         */
      51             : /* _ (Word16) Diff_len           : Lenght of the difference signal               Q0             */
      52             : /* _ (Word16) bits_used          : Number of bit used before frequency   Q0         */
      53             : /* _ (Word16) idx                : Energy band 14                                            Q0         */
      54             : /* _ (Word16*) exc_diff          : Difference signal to quantize (Encoder only)     */
      55             : /* _ (Word16) coder_type         : coding type                                           Q0         */
      56             : /* _ (Word16) bwidth             : input signal bandwidth                                Q0         */
      57             : /*----------------------------------------------------------------------------------*/
      58             : /* OUTPUT ARGUMENTS :                                                                                                               */
      59             : /* _ (Word16*) max_ener_band     : Sorted order                                                     */
      60             : /* _ (Word16*) nb_subbands       : Number of subband allowed                    Q0          */
      61             : /* _ (Word16*) concat_in         :  Concatened PVQ's input vector  (Encoder Only)   */
      62             : /* _ (Word16*) pvq_len           : Number of bin covered with the PVQ   Q0          */
      63             : /*----------------------------------------------------------------------------------*/
      64             : /* INPUT/OUTPUT ARGUMENTS :                                                                                                 */
      65             : /* _ (Word16*) bit                         :Number of bit allowed for frequency quantization    */
      66             : /* _ (Word16*) Ener_per_bd_iQ  : Quantized energy vector                        Q12 */
      67             : /* _ (Word32*) bits_per_bands  : Number of bit allowed per allowed subband      Q18 */
      68             : /*----------------------------------------------------------------------------------*/
      69             : /* RETURN ARGUMENTS :                                                                                                               */
      70             : /* _ None                                                                                                                                           */
      71             : /*==================================================================================*/
      72       43256 : void bands_and_bit_alloc_ivas_fx(
      73             :     const Word16 cor_strong_limit, /* i  : HF correlation                                        */
      74             :     const Word16 noise_lev,        /* i  : dwn scaling factor                                    */
      75             :     const Word32 core_brate,       /* i  : core bit rate                                         */
      76             :     const Word16 Diff_len,         /* i  : Lenght of the difference signal (before pure spectral)*/
      77             :     const Word16 bits_used,        /* i  : Number of bit used before frequency Q                 */
      78             :     Word16 *bit,                   /* i/o: Number of bit allowed for frequency quantization      */
      79             :     const Word16 *Ener_per_bd_iQ,  /* i/o: Quantized energy vector                          Q12  */
      80             :     Word16 *max_ener_band,         /* o  : Sorted order                                          */
      81             :     Word16 *out_bits_per_bands,    /* i/o: Number of bit allowed per allowed subband        Q3   */
      82             :     Word16 *nb_subbands,           /* o  : Number of subband allowed                             */
      83             :     const Word16 *exc_diff,        /* i  : Difference signal to quantize (encoder side only)     */
      84             :     Word16 *concat_in,             /* o  : Concatened PVQ's input vector (encoder side only)     */
      85             :     Word16 *pvq_len,               /* o  : Number of bin covered with the PVQ                    */
      86             :     const Word16 coder_type,       /* i  : coding type                                           */
      87             :     const Word16 bwidth,           /* i  : input signal bandwidth                                */
      88             :     const Word16 GSC_noisy_speech, /* i  : GSC noisy speech flag                                 */
      89             :     const Word16 L_frame,          /* i  : frame length                                          */
      90             :     const Word16 element_mode,     /* i  : element mode                                          */
      91             :     const Word16 GSC_IVAS_mode     /* i  : GSC IVAS mode                                         */
      92             : )
      93             : {
      94             : 
      95             :     Word16 bandoffset, i, j, nb_bands_max, bit_new_bands, bit_tmp, st_band, nb_bands;
      96             :     Word16 ener_vec[MBANDS_GN_BITALLOC16k]; /*Q12 */
      97       43256 :     Word16 nb_tot_bands = 16;
      98             :     Word16 bit_index, bit_index_mem, imax;
      99             :     Word32 L_tmp;
     100             :     Word32 sum_bit /*Q0*/, bit_fracf /*Q18*/;
     101             :     Word16 etmp;
     102             :     Word16 tmp;
     103             :     Word16 pos, band;
     104             :     Word32 SWB_bit_budget;                        /* Q0 -> Q18 */
     105             :     Word32 bits_per_bands[MBANDS_GN_BITALLOC16k]; /* Q18 */
     106             :     Word16 w_sum_bit;
     107             :     Word16 fzero_val;
     108             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     109       43256 :     Flag Overflow = 0;
     110             : #endif
     111       43256 :     nb_tot_bands = MBANDS_GN;
     112       43256 :     move16();
     113       43256 :     if ( EQ_16( L_frame, L_FRAME16k ) )
     114             :     {
     115        5175 :         nb_tot_bands = MBANDS_GN_BITALLOC16k;
     116        5175 :         move16();
     117             :     }
     118             : 
     119       43256 :     set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
     120       43256 :     set16_fx( out_bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
     121             : 
     122             :     /* To adapt current energy band to PVQ freq band for sorting*/
     123       43256 :     ener_vec[0] = add_o( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1], &Overflow ); /*Q12 */
     124       43256 :     move16();
     125       43256 :     Copy( Ener_per_bd_iQ + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */
     126       43256 :     ener_vec[MBANDS_GN - 1] = ener_vec[MBANDS_GN - 2];
     127       43256 :     move16();
     128       43256 :     IF( EQ_16( L_frame, L_FRAME16k ) )
     129             :     {
     130        5175 :         ener_vec[16] = Ener_per_bd_iQ[16];                                                                                    /*Q12*/
     131        5175 :         ener_vec[17] = mac_r( L_mult( Ener_per_bd_iQ[16], 16384 /*0.5 in Q15*/ ), Ener_per_bd_iQ[17], 16384 /*0.5 in Q15*/ ); /*Q12*/
     132        5175 :         ener_vec[18] = Ener_per_bd_iQ[17];                                                                                    /*Q12*/
     133        5175 :         ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 /*0.8 in Q15*/ );                                                    /*Q12*/
     134        5175 :         move16();
     135        5175 :         move16();
     136        5175 :         move16();
     137        5175 :         move16();
     138             :     }
     139             : 
     140             :     /*------------------------------------------------------------------------
     141             :      * Determination of the number of bits available to the frequency domain
     142             :      * Allocation of a maximum number of band to be encoded
     143             :      *-----------------------------------------------------------------------*/
     144             : 
     145       43256 :     nb_bands_max = nb_tot_bands;
     146       43256 :     move16();
     147       43256 :     bit_new_bands = 5;
     148       43256 :     move16();
     149             : 
     150       43256 :     test();
     151       43256 :     if ( GT_32( core_brate, ACELP_16k40 ) && EQ_16( L_frame, L_FRAME16k ) )
     152             :     {
     153        4713 :         bit_new_bands = 7;
     154        4713 :         move16();
     155             :     }
     156             : 
     157       43256 :     i = 0;
     158       43256 :     move16();
     159      202603 :     WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) )
     160             :     {
     161      202603 :         IF( LE_32( core_brate, brate_intermed_tbl[i] ) )
     162             :         {
     163       42863 :             BREAK;
     164             :         }
     165             : 
     166      159740 :         IF( EQ_32( brate_intermed_tbl[i], ACELP_24k40 ) )
     167             :         {
     168         393 :             BREAK;
     169             :         }
     170             : 
     171      159347 :         i = add( i, 1 );
     172             :     }
     173             : 
     174       43256 :     test();
     175       43256 :     test();
     176       43256 :     test();
     177       64014 :     if ( ( element_mode > EVS_MONO ) && EQ_16( coder_type, AUDIO ) &&
     178       34203 :          LE_32( core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
     179             :     {
     180        2522 :         i = sub( i, 1 );
     181             :     }
     182             : 
     183       43256 :     bit_index = i_mult2( BRATE2IDX_fx( brate_intermed_tbl[i] ), 17 );
     184       43256 :     bit_index_mem = bit_index;
     185       43256 :     move16();
     186             : 
     187       43256 :     test();
     188       43256 :     test();
     189       43256 :     IF( ( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE ) ) && ( bwidth == NB ) )
     190             :     {
     191           0 :         IF( GE_32( core_brate, ACELP_9k60 ) )
     192             :         {
     193             :             /*  *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - 25; */
     194           0 :             L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
     195           0 :             tmp = extract_l( L_shr_r( L_tmp, 5 ) );
     196           0 :             *bit = sub( sub( tmp, bits_used ), 25 ); /*Q0*/
     197           0 :             move16();
     198             :         }
     199             :         ELSE
     200             :         {
     201           0 :             L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
     202           0 :             tmp = extract_l( L_shr_r( L_tmp, 5 ) );
     203           0 :             *bit = sub( sub( tmp, bits_used ), 21 ); /*Q0*/
     204           0 :             move16();
     205             :         }
     206           0 :         nb_tot_bands = 10;
     207           0 :         move16();
     208             :     }
     209             :     ELSE
     210             :     {
     211             :         /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - GSC_freq_bits[bit_index]; */
     212             : 
     213       43256 :         L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ );
     214       43256 :         tmp = extract_l( L_shr_r( L_tmp, 5 ) );
     215       43256 :         *bit = sub( sub( tmp, bits_used ), GSC_freq_bits[bit_index] );
     216       43256 :         move16();
     217             :     }
     218             : 
     219       43256 :     IF( EQ_16( L_frame, L_FRAME16k ) )
     220             :     {
     221        5175 :         *bit = sub( *bit, 8 );
     222        5175 :         move16();
     223             :     }
     224             : 
     225       43256 :     test();
     226       43256 :     IF( coder_type == INACTIVE && LE_32( core_brate, GSC_LRES_GAINQ_LIMIT ) ) /* can happen only for 2nd channel inactive */
     227             :     {
     228       13341 :         *bit = add( *bit, GSC_LRES_NB_NITS );
     229       13341 :         move16();
     230             :     }
     231             : 
     232       43256 :     IF( *bit > 0 )
     233             :     {
     234       43256 :         IF( GSC_IVAS_mode > 0 )
     235             :         {
     236        9060 :             nb_tot_bands = Find_bit_alloc_IVAS_fx( core_brate, GSC_IVAS_mode, Diff_len, nb_tot_bands, bit, max_ener_band, ener_vec, bits_per_bands );
     237        9060 :             nb_bands = nb_tot_bands;
     238             :         }
     239       34196 :         ELSE IF( EQ_16( GSC_noisy_speech, 1 ) )
     240             :         {
     241       15691 :             SWB_bit_budget = *bit; /*Q0*/
     242       15691 :             move32();
     243       15691 :             nb_bands = 5;
     244       15691 :             move16();
     245       15691 :             fzero_val = 0;
     246       15691 :             move16();
     247             : 
     248       15691 :             if ( element_mode > EVS_MONO )
     249             :             {
     250       15691 :                 fzero_val = MIN16B;
     251       15691 :                 move16();
     252             :             }
     253             : 
     254       15691 :             test();
     255       15691 :             IF( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO )
     256             :             {
     257          46 :                 nb_bands = 3;
     258          46 :                 move16();
     259             : 
     260          46 :                 if ( GT_32( SWB_bit_budget, 20 ) )
     261             :                 {
     262          46 :                     nb_bands = 5;
     263          46 :                     move16();
     264             :                 }
     265             :             }
     266       15645 :             ELSE IF( LT_16( bwidth, SWB ) )
     267             :             {
     268       14254 :                 nb_bands = 7;
     269       14254 :                 move16();
     270             :             }
     271             : 
     272       15691 :             st_band = nb_bands;
     273       15691 :             move16();
     274             : 
     275       15691 :             set32_fx( bits_per_bands, 0, MBANDS_GN );
     276             :             /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */
     277       15691 :             bit_fracf = Find_bit_frac_fx( nb_bands, extract_l( SWB_bit_budget ) ); /*Q18*/
     278       15691 :             nb_tot_bands = sub( nb_bands_max, 6 );
     279       15691 :             nb_tot_bands = s_min( nb_tot_bands, 16 );
     280             : 
     281       47073 :             FOR( j = 0; j < 2; j++ )
     282             :             {
     283       31382 :                 i = j;
     284       31382 :                 move16();
     285       31382 :                 max_ener_band[j] = i;
     286       31382 :                 move16();
     287       31382 :                 ener_vec[i] = fzero_val;
     288       31382 :                 move16();
     289             :             }
     290             : 
     291       15691 :             IF( LT_16( bwidth, SWB ) )
     292             :             {
     293       14282 :                 test();
     294       14282 :                 if ( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO )
     295             :                 {
     296          28 :                     nb_tot_bands = 5;
     297          28 :                     move16();
     298             :                 }
     299             : 
     300       85636 :                 FOR( ; j < nb_bands; j++ )
     301             :                 {
     302       71354 :                     i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
     303       71354 :                     max_ener_band[j] = i;
     304       71354 :                     move16();
     305       71354 :                     ener_vec[i] = fzero_val;
     306       71354 :                     move16();
     307             :                 }
     308             :             }
     309             :             ELSE
     310             :             {
     311        5636 :                 FOR( ; j < nb_bands; j++ )
     312             :                 {
     313        4227 :                     i = maximum_fx( ener_vec, nb_tot_bands, &etmp );
     314        4227 :                     max_ener_band[j] = i;
     315        4227 :                     move16();
     316        4227 :                     ener_vec[i] = fzero_val;
     317        4227 :                     move16();
     318             :                 }
     319             :             }
     320             : 
     321       15691 :             set32_fx( bits_per_bands, bit_fracf, nb_bands );
     322             :         }
     323             :         ELSE
     324             :         {
     325       18505 :             bit_index = add( bit_index, 1 );
     326       18505 :             bit_tmp = sub( *bit, GSC_freq_bits[bit_index] );
     327       18505 :             bit_index = add( bit_index, 1 );
     328       18505 :             nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] );
     329       18505 :             bit_index = add( bit_index, 1 );
     330             : 
     331       18505 :             *pvq_len = 112;
     332       18505 :             move16();
     333       18505 :             st_band = 7;
     334       18505 :             move16();
     335             : 
     336       18505 :             test();
     337       18505 :             IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) )
     338             :             {
     339        4713 :                 *pvq_len = 160;
     340        4713 :                 move16();
     341        4713 :                 st_band = 10;
     342        4713 :                 move16();
     343        4713 :                 nb_bands = shr( *pvq_len, 4 );
     344        4713 :                 bit_tmp = sub( bit_tmp, 35 );
     345        4713 :                 bit_new_bands = 5;
     346        4713 :                 move16();
     347             :             }
     348             : 
     349       18505 :             IF( LE_32( core_brate, ACELP_9k60 ) )
     350             :             {
     351       10821 :                 *pvq_len = 80;
     352       10821 :                 move16();
     353       10821 :                 st_band = 5;
     354       10821 :                 move16();
     355             : 
     356       10821 :                 IF( Diff_len == 0 )
     357             :                 {
     358         239 :                     nb_bands_max = add( nb_bands_max, 2 );
     359         239 :                     bit_tmp = sub( bit_tmp, 13 );
     360             :                 }
     361             :             }
     362             : 
     363        7684 :             ELSE IF( Diff_len == 0 )
     364             :             {
     365        4092 :                 nb_bands_max = add( nb_bands_max, 2 );
     366        4092 :                 bit_tmp = sub( bit_tmp, 17 );
     367             :             }
     368             : 
     369       18505 :             nb_bands = shr( *pvq_len, 4 );
     370       18505 :             nb_bands_max = s_min( nb_bands_max, MBANDS_GN_BITALLOC16k );
     371             : 
     372             :             /*------------------------------------------------------------------------
     373             :              * Ajustement of the maximum number of bands in function of the
     374             :              * dynamics of the spectrum (more or less speech like)
     375             :              *-----------------------------------------------------------------------*/
     376       18505 :             test();
     377       18505 :             test();
     378       18505 :             test();
     379       18505 :             test();
     380       18505 :             IF( coder_type == INACTIVE || GE_16( noise_lev, NOISE_LEVEL_SP3 ) )
     381             :             {
     382             :                 /* Probably classification error -> concentrate bits on LF */
     383       12003 :                 IF( EQ_16( L_frame, L_FRAME16k ) && GE_32( core_brate, ACELP_24k40 ) )
     384             :                 {
     385         393 :                     nb_bands_max = sub( nb_tot_bands, 2 );
     386             :                 }
     387       11610 :                 ELSE IF( GE_32( core_brate, ACELP_16k40 ) )
     388             :                 {
     389        4597 :                     nb_bands_max = add( nb_bands, 2 );
     390             :                 }
     391        7013 :                 ELSE IF( GE_32( core_brate, ACELP_8k00 ) )
     392             :                 {
     393        3614 :                     nb_bands_max = add( nb_bands, 1 );
     394             :                 }
     395             :                 ELSE
     396             :                 {
     397        3399 :                     nb_bands_max = nb_bands;
     398        3399 :                     move16();
     399             :                 }
     400             :             }
     401        6502 :             ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP2 ) ||
     402             :                      ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) /* Very low dynamic, tend to speech, do not try to code HF at all */
     403             :             {
     404        6488 :                 nb_bands_max = sub( nb_bands_max, 2 );
     405             :             }
     406          14 :             ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */
     407             :             {
     408           7 :                 nb_bands_max = sub( nb_bands_max, 1 );
     409             :             }
     410             : 
     411       18505 :             IF( EQ_16( L_frame, L_FRAME16k ) )
     412             :             {
     413        5175 :                 IF( LT_32( core_brate, ACELP_24k40 ) )
     414             :                 {
     415        4782 :                     nb_bands_max = sub( nb_bands_max, 4 );
     416             :                 }
     417         393 :                 ELSE IF( LT_32( core_brate, ACELP_32k ) )
     418             :                 {
     419         393 :                     test();
     420         393 :                     IF( GT_16( Diff_len, 0 ) || GE_16( noise_lev, NOISE_LEVEL_SP2 ) )
     421             :                     {
     422         393 :                         nb_bands_max = sub( nb_bands_max, 2 );
     423         393 :                         bit_new_bands = shl( bit_new_bands, 1 );
     424             :                     }
     425             :                 }
     426           0 :                 ELSE IF( core_brate >= ACELP_32k )
     427             :                 {
     428           0 :                     bit_new_bands = shl( bit_new_bands, 1 );
     429             :                 }
     430             :             }
     431             : 
     432       18505 :             test();
     433       18505 :             if ( ( bwidth == NB ) && GT_16( nb_bands_max, 10 ) )
     434             :             {
     435           0 :                 nb_bands_max = 10;
     436           0 :                 move16();
     437             :             }
     438             : 
     439             :             /*------------------------------------------------------------------------
     440             :              * Find extra number of band to code according to bit rate availables
     441             :              *-----------------------------------------------------------------------*/
     442       18505 :             test();
     443       25863 :             WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) )
     444             :             {
     445        7358 :                 test();
     446        7358 :                 bit_tmp = sub( bit_tmp, bit_new_bands );
     447        7358 :                 nb_bands = add( nb_bands, 1 );
     448             :             }
     449             : 
     450             :             /*------------------------------------------------------------------------
     451             :              * Fractional bits to distribute on the first x bands
     452             :              *-----------------------------------------------------------------------*/
     453             : 
     454       18505 :             test();
     455       18505 :             IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) )
     456             :             {
     457           0 :                 bit_fracf = 0;
     458           0 :                 move32();
     459             :             }
     460             :             ELSE
     461             :             {
     462       18505 :                 bit_fracf = Find_bit_frac_fx( st_band, bit_tmp ); /*Q18*/
     463             :             }
     464             : 
     465             :             /*------------------------------------------------------------------------
     466             :              * Complete the bit allocation per frequency band
     467             :              *-----------------------------------------------------------------------*/
     468       18505 :             imax = 5;
     469       18505 :             move16();
     470             : 
     471       18505 :             IF( GT_32( core_brate, ACELP_9k60 ) )
     472             :             {
     473        7684 :                 imax = 7;
     474        7684 :                 move16();
     475             :             }
     476      126398 :             FOR( i = 0; i < imax; i++ )
     477             :             {
     478      107893 :                 bits_per_bands[i] = L_add( GSC_freq_bits_fx_Q18[bit_index], bit_fracf ); /*Q18*/
     479      107893 :                 move32();                                                                /* Q18 */
     480      107893 :                 bit_index = add( bit_index, 1 );
     481             :             }
     482             : 
     483       18505 :             IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) )
     484             :             {
     485        4713 :                 bit_index = 0;
     486        4713 :                 move16();
     487        4713 :                 i = sub( imax, 1 );
     488        4713 :                 bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( Compl_GSC_freq_bits[bit_index], Q18 ) ); /*Q18*/
     489        4713 :                 move32();
     490        4713 :                 i = add( i, 1 );
     491        4713 :                 bit_index = add( bit_index, 1 );
     492             : 
     493       18852 :                 FOR( ; i < 10; i++ )
     494             :                 {
     495       14139 :                     bits_per_bands[i] = L_add( bits_per_bands[i], L_add( L_shl( Compl_GSC_freq_bits[bit_index], Q18 ), bit_fracf ) ); /*Q18*/
     496       14139 :                     move32();
     497       14139 :                     bit_index = add( bit_index, 1 );
     498             :                 }
     499             :             }
     500             : 
     501       18505 :             IF( Diff_len == 0 )
     502             :             {
     503        4331 :                 bit_index = add( bit_index_mem, 10 );
     504       34648 :                 FOR( i = 0; i < 7; i++ )
     505             :                 {
     506       30317 :                     bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx_Q18[bit_index] ); /*Q18*/
     507       30317 :                     move32();                                                                        /*chk Q18 */
     508       30317 :                     bit_index = add( bit_index, 1 );
     509             :                 }
     510             :             }
     511             : 
     512       18505 :             IF( bit_fracf < 0 )
     513             :             {
     514       46529 :                 FOR( j = 0; j < nb_tot_bands; j++ )
     515             :                 {
     516       43792 :                     bits_per_bands[j] = L_max( bits_per_bands[j], 0 );
     517       43792 :                     move32();
     518             :                 }
     519             :             }
     520             : 
     521             :             /*--------------------------------------------------------------------------
     522             :              * Complete the bit allocation per frequency band for 16kHz high brate mode
     523             :              *--------------------------------------------------------------------------*/
     524             : 
     525       18505 :             test();
     526       18505 :             IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) )
     527             :             {
     528           0 :                 FOR( j = st_band; j < nb_bands; j++ )
     529             :                 {
     530           0 :                     bits_per_bands[j] = L_shl( bit_new_bands, Q18 );
     531           0 :                     move32();
     532             :                 }
     533             : 
     534           0 :                 bit_fracf = L_shl( L_mult0( idiv1616( 16384, nb_bands ), bit_tmp ), 4 ); /*Q18*/
     535             : 
     536           0 :                 etmp = divide3216( L_shr( bit_fracf, Q2 ), add( nb_bands, 1 ) ); /* Q15 */
     537             : 
     538           0 :                 bit_fracf = L_shl( etmp, Q3 ); /* Q18 */
     539             : 
     540           0 :                 FOR( j = nb_bands - 1; j >= 0; j-- )
     541             :                 {
     542           0 :                     bits_per_bands[j] = L_add( bits_per_bands[j], L_shl( etmp, Q3 ) ); /* Q18 */
     543           0 :                     move32();
     544           0 :                     etmp = extract_l( L_add( etmp, L_shr( bit_fracf, Q3 ) ) );
     545             :                 }
     546             :             }
     547             :             ELSE
     548             :             {
     549       25863 :                 FOR( j = st_band; j < nb_bands; j++ )
     550             :                 {
     551        7358 :                     bits_per_bands[j] = L_shl( bit_new_bands, 18 );
     552        7358 :                     move32(); /*chk Q18 */
     553             :                 }
     554             :             }
     555             : 
     556             :             /*--------------------------------------------------------------------------
     557             :              * Compute a maximum band (band offset) for the search on maximal energy
     558             :              * This is function of the spectral dynamic and the bitrate
     559             :              *--------------------------------------------------------------------------*/
     560             : 
     561       18505 :             bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) );
     562             : 
     563       18505 :             test();
     564       18505 :             test();
     565       18505 :             test();
     566       18505 :             test();
     567       18505 :             test();
     568       18505 :             IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) )
     569             :             {
     570           7 :                 bandoffset = sub( bandoffset, 1 );
     571             :             }
     572       18498 :             ELSE IF( ( LE_32( core_brate, ACELP_13k20 ) && ( ( coder_type == INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) ) ||
     573             :                      ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) )
     574             :             {
     575        5583 :                 bandoffset = add( bandoffset, 1 );
     576             :             }
     577             : 
     578       18505 :             bandoffset = s_max( bandoffset, 0 );
     579             : 
     580             :             /*--------------------------------------------------------------------------
     581             :              * Initiazed sorted vector
     582             :              * For the first x bands to be included in th final sorted vector
     583             :              * Sort the remaining bands in decrease energy order
     584             :              *--------------------------------------------------------------------------*/
     585      335285 :             FOR( j = 0; j < nb_tot_bands; j++ )
     586             :             {
     587      316780 :                 max_ener_band[j] = -10;
     588      316780 :                 move16();
     589             :             }
     590      140537 :             FOR( j = 0; j < st_band; j++ )
     591             :             {
     592      122032 :                 max_ener_band[j] = j;
     593      122032 :                 move16();
     594      122032 :                 ener_vec[j] = -10;
     595      122032 :                 move16();
     596             :             }
     597       18505 :             pos = st_band;
     598       18505 :             move16();
     599       25863 :             FOR( ; j < nb_bands; j++ )
     600             :             {
     601        7358 :                 i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
     602        7358 :                 pos = s_max( pos, i );
     603        7358 :                 max_ener_band[j] = i;
     604        7358 :                 move16();
     605        7358 :                 ener_vec[i] = -10;
     606        7358 :                 move16();
     607             :             }
     608             : 
     609             :             /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */
     610       18505 :             test();
     611       18505 :             test();
     612       18505 :             test();
     613       18505 :             IF( GT_16( sub( nb_tot_bands, bandoffset ), nb_bands ) && ( GT_16( pos, 7 ) && EQ_32( core_brate, ACELP_8k00 ) ) && EQ_16( bwidth, WB ) )
     614             :             {
     615           0 :                 band = sub( nb_tot_bands, add( bandoffset, nb_bands ) );
     616           0 :                 FOR( j = 0; j < band; j++ )
     617             :                 {
     618           0 :                     i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp );
     619           0 :                     max_ener_band[nb_bands + j] = i;
     620           0 :                     move16();
     621           0 :                     ener_vec[i] = -10;
     622           0 :                     move16();
     623           0 :                     bits_per_bands[nb_bands + j] = 1310720; /*5 in Q18*/
     624           0 :                     move32();                               /*Q18 */
     625             :                 }
     626           0 :                 nb_bands = add( nb_bands, band );
     627             : 
     628           0 :                 bit_tmp = i_mult2( band, 5 );
     629             : 
     630           0 :                 IF( LE_16( band, 2 ) )
     631             :                 {
     632           0 :                     FOR( j = st_band - 1; j < nb_bands; j++ )
     633             :                     {
     634           0 :                         bits_per_bands[j] = L_add( bits_per_bands[j], 262144 ); /*Q18 */
     635           0 :                         move32();
     636             :                     }
     637           0 :                     bit_tmp = add( bit_tmp, add( sub( nb_bands, st_band ), 1 ) );
     638             :                 }
     639             : 
     640           0 :                 i = 0;
     641           0 :                 move16();
     642           0 :                 j = 0;
     643           0 :                 move16();
     644           0 :                 FOR( ; bit_tmp > 0; bit_tmp-- )
     645             :                 {
     646           0 :                     bits_per_bands[j] = L_sub( bits_per_bands[j], ONE_IN_Q18 ); /*Q18 */
     647           0 :                     j = add( j, 1 );
     648           0 :                     IF( EQ_16( j, sub( st_band, i ) ) )
     649             :                     {
     650           0 :                         j = 0;
     651           0 :                         move16();
     652             :                     }
     653           0 :                     test();
     654           0 :                     if ( j == 0 && LT_16( i, sub( st_band, 1 ) ) )
     655             :                     {
     656           0 :                         i = add( i, 1 );
     657             :                     }
     658             :                 }
     659             :             }
     660             :         }
     661             : 
     662             :         /*--------------------------------------------------------------------------
     663             :          * Bit sum verification for GSC inactive at very high rate
     664             :          * The maximum number of bits per band of length 16 is 112
     665             :          * Redistribute the overage bits if needed
     666             :          *--------------------------------------------------------------------------*/
     667       43256 :         sum_bit = 0;
     668       43256 :         move16();
     669       43256 :         j = 0;
     670       43256 :         move16();
     671      352881 :         FOR( i = 0; i < nb_bands; i++ )
     672             :         {
     673      309625 :             L_tmp = Mult_32_16( sum_bit, 10923 /*1/3 in Q15*/ );
     674             : 
     675      309625 :             IF( GT_32( bits_per_bands[i], 29360128 ) ) /* 112 in Q18 */
     676             :             {
     677           0 :                 sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], 29360128 /* 112 in Q18 */ ) ); /* Q18 */
     678           0 :                 bits_per_bands[i] = 29360128 /* 112 in Q18 */;
     679           0 :                 move32();
     680           0 :                 j = add( i, 1 );
     681             :             }
     682      309625 :             ELSE IF( GT_32( L_add( bits_per_bands[i], L_tmp ), 29360128 ) ) /* Q18 */
     683             :             {
     684           0 :                 j = add( i, 1 );
     685             :             }
     686             :         }
     687             : 
     688       43256 :         IF( sum_bit != 0 )
     689             :         {
     690           0 :             tmp = sub( nb_bands, j );
     691           0 :             sum_bit = Mult_32_16( sum_bit, div_s( 1, tmp ) ); /* Q18 */
     692           0 :             FOR( i = j; i < nb_bands; i++ )
     693             :             {
     694           0 :                 bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
     695           0 :                 move32(); /* Q18 */
     696             :             }
     697             :         }
     698             : 
     699             :         /*--------------------------------------------------------------------------
     700             :          * second step of bit sum verification, normally sum_bit == *bit
     701             :          *--------------------------------------------------------------------------*/
     702             : 
     703       43256 :         sum_bit = 0;
     704       43256 :         move32();
     705      352881 :         FOR( i = 0; i < nb_bands; i++ )
     706             :         {
     707      309625 :             bits_per_bands[i] = L_shl( L_shr( bits_per_bands[i], Q18 ), Q18 );
     708      309625 :             move32();
     709      309625 :             sum_bit = L_add( sum_bit, L_shr( bits_per_bands[i], Q18 ) ); /*Q0*/
     710             :         }
     711             : 
     712       43256 :         test();
     713       43256 :         IF( GSC_IVAS_mode != 0 && LT_32( sum_bit, *bit ) ) /* If we need to add bits, we are doing it on the LF */
     714             :         {
     715        9044 :             reajust_bits_fx( bits_per_bands, 0, nb_bands, (Word16) sum_bit, *bit );
     716             :         }
     717             :         ELSE
     718             :         {
     719       34212 :             reajust_bits_fx( bits_per_bands, sub( nb_bands, 1 ), 0, (Word16) sum_bit, *bit );
     720             :         }
     721       43256 :         w_sum_bit = 0;
     722       43256 :         move16();
     723      352881 :         FOR( i = 0; i < nb_bands; i++ )
     724             :         {
     725      309625 :             out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 );
     726      309625 :             move16();
     727      309625 :             w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */
     728             :         }
     729       43256 :         tmp = shl( *bit, 3 );
     730             : 
     731             :         /*--------------------------------------------------------------------------
     732             :          * Recompute the real number/length of frequency bands to encode
     733             :          *--------------------------------------------------------------------------*/
     734       43256 :         *nb_subbands = nb_bands;
     735       43256 :         move16();
     736       43256 :         *pvq_len = shl( *nb_subbands, 4 );
     737       43256 :         move16();
     738             : 
     739             :         /*--------------------------------------------------------------------------
     740             :          * Concatenate bands (encoder only)
     741             :          *--------------------------------------------------------------------------*/
     742       43256 :         IF( exc_diff != NULL )
     743             :         {
     744      185569 :             FOR( j = 0; j < nb_bands; j++ )
     745             :             {
     746      162873 :                 Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 );
     747             :             }
     748             :         }
     749             :     }
     750             :     ELSE /* *bit == 0 */
     751             :     {
     752           0 :         set16_fx( out_bits_per_bands, 0, nb_tot_bands );
     753           0 :         *nb_subbands = 0;
     754           0 :         move16();
     755           0 :         *pvq_len = 0;
     756           0 :         move16();
     757             :     }
     758             : 
     759       43256 :     return;
     760             : }
     761             : 
     762             : 
     763             : /*-------------------------------------------------------------------*
     764             :  * reajust_bits()
     765             :  *
     766             :  *
     767             :  *-------------------------------------------------------------------*/
     768             : 
     769       43256 : static void reajust_bits_fx(
     770             :     Word32 *bits_per_bands, /*i : Q18*/
     771             :     const Word16 st_band,
     772             :     const Word16 end_band,
     773             :     const Word16 sum_bit_in, /*i : Q0*/
     774             :     const Word16 bit_bdgt_in /*i : Q0*/ )
     775             : {
     776             :     Word16 i, amount_to_add, incr;
     777             :     Word16 bit_bdgt, sum_bit;
     778             : 
     779       43256 :     incr = 1;
     780       43256 :     move16();
     781       43256 :     if ( LT_16( end_band, st_band ) )
     782             :     {
     783       34212 :         incr = -1;
     784       34212 :         move16();
     785             :     }
     786             : 
     787       43256 :     IF( LT_16( bit_bdgt_in, sum_bit_in ) )
     788             :     {
     789           0 :         amount_to_add = -1;
     790           0 :         move16();
     791           0 :         bit_bdgt = sum_bit_in;
     792           0 :         move16();
     793           0 :         sum_bit = bit_bdgt_in;
     794           0 :         move16();
     795             :     }
     796             :     ELSE
     797             :     {
     798       43256 :         bit_bdgt = bit_bdgt_in;
     799       43256 :         move16();
     800       43256 :         sum_bit = sum_bit_in;
     801       43256 :         move16();
     802       43256 :         amount_to_add = 1;
     803       43256 :         move16();
     804             :     }
     805             : 
     806       43256 :     i = st_band;
     807       43256 :     move16();
     808      194899 :     WHILE( GT_16( bit_bdgt, sum_bit ) )
     809             :     {
     810             :         /* if (amount_to_add > 0 || (amount_to_add < 0 && bits_per_bands[i] > 1))*/
     811      151643 :         test();
     812      151643 :         test();
     813      151643 :         IF( amount_to_add > 0 || ( amount_to_add < 0 && GT_32( bits_per_bands[i], ONE_IN_Q18 ) ) )
     814             :         {
     815             :             /* bits_per_bands[i] += amount_to_add;*/
     816      151643 :             bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( amount_to_add, Q18 ) ); /*Q18*/
     817      151643 :             move32();
     818      151643 :             sum_bit = add( sum_bit, abs_s( amount_to_add ) );
     819             :         }
     820             : 
     821      151643 :         i = add( i, incr );
     822      151643 :         if ( EQ_16( i, end_band ) )
     823             :         {
     824        4369 :             i = st_band;
     825        4369 :             move16();
     826             :         }
     827             :     }
     828             : 
     829       43256 :     return;
     830             : }
     831             : 
     832             : /*-------------------------------------------------------------------*
     833             :  * Find_bit_frac()
     834             :  *
     835             :  * Computes the fraction of the remaining bit budget to allocate to the bands
     836             :  *-------------------------------------------------------------------*/
     837             : 
     838       34196 : static Word32 Find_bit_frac_fx(
     839             :     const Word16 nb_band,
     840             :     const Word16 remaining_bits )
     841             : {
     842             :     Word16 inv_bandQ15;
     843             :     Word32 L_out;
     844             : 
     845       34196 :     inv_bandQ15 = 6553; /*Q15*/
     846       34196 :     move16();
     847       34196 :     IF( EQ_16( nb_band, 7 ) )
     848             :     {
     849       17225 :         inv_bandQ15 = 4681; /*Q15*/
     850       17225 :         move16();
     851             :     }
     852       16971 :     ELSE IF( EQ_16( nb_band, 3 ) )
     853             :     {
     854           0 :         inv_bandQ15 = 10922; /*Q15*/
     855           0 :         move16();
     856             :     }
     857       16971 :     ELSE IF( EQ_16( nb_band, 4 ) )
     858             :     {
     859           0 :         inv_bandQ15 = 8192; /*Q15*/
     860           0 :         move16();
     861             :     }
     862       16971 :     ELSE IF( EQ_16( nb_band, 5 ) )
     863             :     {
     864       12258 :         inv_bandQ15 = 6536; /*Q15*/
     865       12258 :         move16();
     866             :     }
     867        4713 :     ELSE IF( EQ_16( nb_band, 10 ) )
     868             :     {
     869        4713 :         inv_bandQ15 = 3277; /*Q15*/
     870        4713 :         move16();
     871             :     }
     872             :     ELSE
     873             :     {
     874             : #ifdef DEBUGGING
     875             :         printf( "1/%d NOT DEFINED in Find_bit_frac\n", nb_band );
     876             : #endif
     877             :     }
     878       34196 :     L_out = L_mult( inv_bandQ15, shl( remaining_bits, 2 ) ); /*Q18*/
     879             : 
     880       34196 :     return ( L_out );
     881             : }
     882             : 
     883             : 
     884             : /* o  : Number of band to encode */
     885        9060 : static Word16 Find_bit_alloc_IVAS_fx(
     886             :     const Word32 core_brate,      /* i  : core bit rate                                         */
     887             :     const Word16 GSC_IVAS_mode,   /* i  : GSC IVAS mode                                         */
     888             :     const Word16 Diff_len,        /* i  : Length of the difference signal (before pure spectral)*/
     889             :     const Word16 nb_tot_bands_in, /* i  : total number of band */
     890             :     Word16 *bit,                  /* i/o: Number of bit allowed for frequency quantization      */
     891             :     Word16 *max_ener_band,        /* i/o: Energy based sorted order                             */
     892             :     Word16 *ener_vec,             /* i/o: Energy per band order                                 */
     893             :     Word32 *bits_per_bands        /* o  : Number of bit allowed per allowed subband        Q18  */
     894             : )
     895             : {
     896             :     Word32 mp, mb, nb_bands_adj, bit_adj;
     897             :     Word16 nb_pulse_per_band[MBANDS_GN_BITALLOC16k];
     898             :     Word32 SWB_bit_budget; /* Q0 -> Q18 */
     899             :     Word16 i, j, nb_bands_max, st_band, nb_tot_bands_loc, etmp;
     900             :     Word32 sum_bit /*Q18*/, bit_fracf /*Q18*/;
     901             :     Word16 d_tmp, e_div, tmp16;
     902             :     Word32 Ltmp, etmp_32fx;
     903             : 
     904        9060 :     SWB_bit_budget = *bit; /* Q0 */
     905        9060 :     st_band = 5;
     906        9060 :     nb_bands_max = nb_tot_bands_in;
     907             : 
     908        9060 :     move16();
     909        9060 :     move16();
     910        9060 :     move16();
     911             : 
     912        9060 :     set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );
     913             : 
     914             :     /* Decide the percentage of bits allocated to LF (between 50-75%) depending of the temporal contribution in GSC */
     915             :     /* bit_fracf = ( -0.125f * Diff_len + 76.0f ) / 100; */
     916        9060 :     bit_fracf = L_add( Mpy_32_32( -Q31_0_00125, L_shl( Diff_len, Q18 ) ), Q18_0_76 ); /* Q18 */
     917             : 
     918             :     /* bit_fracf = check_bounds(bit_fracf, 0.50f, 0.75f); */
     919        9060 :     bit_fracf = check_bounds_l( bit_fracf, Q18_0_50, Q18_0_75 ); /* Q18 */
     920             : 
     921             :     /* Adjustment of the bitrate between LF and HF base on the content type */
     922             :     /* 1 = new GSC bit alloc
     923             :                2 = GSC bit alloc for tc frame
     924             :                3 = more music like (should not happen often given music is coded with dft) */
     925             : 
     926        9060 :     if ( LE_16( GSC_IVAS_mode, 3 ) )
     927             :     {
     928        9060 :         nb_bands_max = sub( nb_bands_max, 6 );
     929             :     }
     930        9060 :     IF( EQ_16( GSC_IVAS_mode, 2 ) )
     931             :     {
     932             :         /* bit_fracf += 0.1f; */
     933         457 :         bit_fracf = L_add( bit_fracf, Q18_0_1 ); /* Q18*/
     934         457 :         nb_bands_max = sub( nb_bands_max, 1 );
     935             :     }
     936        9060 :     IF( EQ_16( GSC_IVAS_mode, 3 ) )
     937             :     {
     938             :         /* bit_fracf -= 0.1f; */
     939        5083 :         bit_fracf = L_sub( bit_fracf, Q18_0_1 ); /* Q18*/
     940        5083 :         nb_bands_max = add( nb_bands_max, 3 );
     941             :     }
     942             : 
     943             :     /* First find how much we want to share between LF and HF, at low bitrate, a miminum of bits is needed in LF by limitating the number of bands*/
     944             :     /* Adjust the number of band based on the content type and bitrate */
     945             : 
     946             :     /* nb_bands_adj = 1.0f; */
     947        9060 :     nb_bands_adj = Q18_1_0;
     948        9060 :     move32();
     949        9060 :     test();
     950        9060 :     test();
     951        9060 :     IF( EQ_16( GSC_IVAS_mode, 1 ) && LT_32( core_brate, GSC_L_RATE_STG ) )
     952             :     {
     953             :         /* nb_bands_adj = 0.0125f * SWB_bit_budget - 0.75f;*/
     954        3482 :         nb_bands_adj = L_sub( Mpy_32_32( Q31_0_0125, L_shl( SWB_bit_budget, Q18 ) ), Q18_0_75 ); /* Q18 */
     955             :     }
     956        5578 :     ELSE IF( NE_16( GSC_IVAS_mode, 2 ) && GT_32( core_brate, GSC_H_RATE_STG ) )
     957             :     {
     958             :         /*nb_bands_adj = 0.02f * SWB_bit_budget - 1.2f;*/
     959           0 :         nb_bands_adj = L_sub( Mpy_32_32( Q31_0_02, L_shl( SWB_bit_budget, Q18 ) ), Q18_1_2 ); /* Q18 */
     960             :     }
     961             :     /*nb_bands_max = (int16_t)(nb_bands_max * nb_bands_adj + 0.5f);*/
     962             : 
     963        9060 :     nb_bands_max = round_fx( Mpy_32_16_1( L_shl( nb_bands_adj, 5 ), shl( nb_bands_max, 10 - 2 ) ) ); /* Q0 */
     964        9060 :     nb_bands_max = check_bounds_s_fx( nb_bands_max, 5, nb_tot_bands_in );
     965             : 
     966             :     /* bit_fracf *= SWB_bit_budget;*/
     967             : 
     968             :     /* At this point bit_fracf has a value below 1.0 */
     969        9060 :     bit_fracf = Mpy_32_16_1( L_shl( bit_fracf, 10 ), extract_l( L_shl( SWB_bit_budget, 5 ) ) ); /* (Q(18+10)*Q(0+5) + 1 - 16 =  Q18 */
     970             : 
     971             :     /* Estimation of the number of bit used in HF */
     972             :     /* with only the first weighting The number of bits in max_ener_band[st_band-1] = 17% of bit_fracf */
     973             :     /* mb = .17f * bit_fracf;*/
     974        9060 :     mb = Mpy_32_32( Q31_0_17, bit_fracf ); /* Q18 */
     975             : 
     976             :     /* mp = 2 * DSR_NB_PULSE;*/
     977        9060 :     mp = Q18_2_0xDSR_NB_PULSE;
     978        9060 :     move32();
     979             : 
     980        9060 :     test();
     981        9060 :     IF( LT_32( core_brate, GSC_L_RATE_STG ) && EQ_16( GSC_IVAS_mode, 3 ) )
     982             :     {
     983             :         /* mp = 1.5f * DSR_NB_PULSE;*/
     984        4966 :         mp = Q18_1_5xDSR_NB_PULSE;
     985        4966 :         move32();
     986             :     }
     987        4094 :     ELSE IF( LT_32( core_brate, GSC_L_RATE_STG ) )
     988             :     {
     989             :         /* mp = DSR_NB_PULSE;*/
     990        3926 :         mp = Q18_DSR_NB_PULSE;
     991        3926 :         move32();
     992             :     }
     993             : 
     994             :     /* We want  max_ener_band[st_band] <=  max_ener_band[st_band-1] and  max_ener_band[nb_bands_max-1] <=  max_ener_band[st_band]*/
     995             :     /* We will estimate the number of bits to allocate of HF and put the remaining bits, if any, back on LF */
     996             :     /* compute the total possible number of band to be coded */
     997             : 
     998             :     /* nb_tot_bands = (int16_t)((SWB_bit_budget - bit_fracf) / (mp + (mb - mp) / 2.0f)); */
     999        9060 :     d_tmp = Find_norm_inv_fx( L_add( mp, mb ), &e_div );
    1000        9060 :     Ltmp = Mpy_32_16_1( L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ), d_tmp ); /* Perform mult by 1/den */
    1001        9060 :     nb_tot_bands_loc = extract_h( L_shl( Ltmp, sub( 1, e_div ) ) );                /* adjust exponent:   1 is to take into account the  / 2.0f, and e_div for the num and den of the division*/
    1002        9060 :     mp = L_min( mp, mb );
    1003             : 
    1004        9060 :     tmp16 = sub( add( nb_tot_bands_loc, st_band ), nb_bands_max );
    1005        9060 :     IF( tmp16 > 0 )
    1006             :     {
    1007             :         /* bit_adj = ( ( mb + mp ) / 2 ) * ( nb_tot_bands_loc + st_band - nb_bands_max ); */
    1008        3612 :         bit_adj = Mpy_32_16_1( L_shl( L_add( mb, mp ), 5 ), shl( tmp16, 10 - 1 ) ); /* Q18+5 * Q0+10 + 1 -1 - 16 = Q18 (-1 is to cover for the /2 in the equation) */
    1009        3612 :         bit_adj = L_max( 0, bit_adj );
    1010        3612 :         nb_tot_bands_loc = sub( nb_bands_max, st_band );
    1011             :         /* bit_fracf += bit_adj; */
    1012        3612 :         bit_fracf = L_add( bit_fracf, bit_adj ); /* Q18 */
    1013             :     }
    1014        9060 :     nb_tot_bands_loc = add( nb_tot_bands_loc, st_band );
    1015             : 
    1016             :     /* Allocate bits to LF */
    1017             :     /* etmp = 0.23f; */
    1018        9060 :     etmp_32fx = Q31_0_23;
    1019        9060 :     move32();
    1020       54360 :     FOR( j = 0; j < st_band; j++ )
    1021             :     {
    1022       45300 :         i = j;
    1023       45300 :         move16();
    1024       45300 :         max_ener_band[j] = i;
    1025       45300 :         move16();
    1026       45300 :         ener_vec[i] = MIN16B;
    1027       45300 :         move16();
    1028             :         /* bits_per_bands[j] = etmp * bit_fracf; */
    1029       45300 :         bits_per_bands[j] = Mpy_32_32( bit_fracf, etmp_32fx ); /* 18 + 31 + 1 - 32 = Q18 */
    1030       45300 :         move32();
    1031             :         /* etmp -= 0.015f; */
    1032       45300 :         etmp_32fx = L_sub( etmp_32fx, Q31_0_015 ); /* Q18 */
    1033             :     }
    1034             : 
    1035             :     /* SWB_bit_budget -= bit_fracf; */
    1036        9060 :     SWB_bit_budget = L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ); /* Q0->Q18 */
    1037             : 
    1038             :     /* Find low energy band in HF */
    1039        9060 :     set16_fx( nb_pulse_per_band, 2, MBANDS_GN_BITALLOC16k );
    1040       19893 :     FOR( i = st_band + 2; i < nb_tot_bands_loc - 1; i++ )
    1041             :     {
    1042       10833 :         test();
    1043       10833 :         if ( LT_16( ener_vec[i], ener_vec[i - 1] ) && LT_16( ener_vec[i], ener_vec[i + 1] ) ) /* i +1 and i -1 can be considered as 2 ptrs */
    1044             :         {
    1045        3508 :             nb_pulse_per_band[i] = 1;
    1046        3508 :             move16();
    1047             :         }
    1048             :     }
    1049       37032 :     FOR( j = st_band; j < nb_tot_bands_loc; j++ )
    1050             :     {
    1051       27972 :         IF( GT_16( j, 6 ) )
    1052             :         {
    1053       16345 :             i = maximum_fx( ener_vec, nb_tot_bands_loc, &etmp );
    1054             :         }
    1055             :         ELSE
    1056             :         {
    1057       11627 :             i = j;
    1058       11627 :             move16();
    1059             :         }
    1060       27972 :         max_ener_band[j] = i;
    1061       27972 :         move16();
    1062       27972 :         ener_vec[i] = MIN16B;
    1063       27972 :         move16();
    1064             :     }
    1065             : 
    1066             :     /* Recompute the final bit distribution for HF */
    1067        9060 :     IF( GT_16( nb_tot_bands_loc, st_band ) )
    1068             :     {
    1069             :         /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp;  */
    1070             :         /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
    1071             :         /* This is not bit exact because of the precision lost */
    1072             : 
    1073             :         /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp; */
    1074        5877 :         d_tmp = Find_norm_inv_fx( L_deposit_h( sub( nb_tot_bands_loc, st_band ) ), &e_div );
    1075        5877 :         mb = L_sub( L_shr( Mpy_32_16_1( L_shl( SWB_bit_budget, 1 ), d_tmp ), e_div ), mp ); /* Q18 */
    1076             : 
    1077             :         /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
    1078        5877 :         bit_fracf = L_shr( Mpy_32_16_1( L_sub( mb, mp ), d_tmp ), e_div ); /* Q18 */
    1079             : 
    1080        5877 :         mb = L_sub( mb, bit_fracf );
    1081             :         /* Do the distribution */
    1082       33849 :         FOR( j = st_band; j < nb_tot_bands_loc; j++ )
    1083             :         {
    1084       27972 :             bits_per_bands[max_ener_band[j]] = Q18_DSR_NB_PULSE;
    1085       27972 :             move32();
    1086       27972 :             if ( GT_16( nb_pulse_per_band[max_ener_band[j]], 1 ) )
    1087             :             {
    1088       24464 :                 bits_per_bands[max_ener_band[j]] = mb;
    1089       24464 :                 move32();
    1090             :             }
    1091       27972 :             mb = L_sub( mb, bit_fracf );
    1092       27972 :             SWB_bit_budget = L_sub( SWB_bit_budget, bits_per_bands[max_ener_band[j]] ); /* Q18 */
    1093             :         }
    1094             :     }
    1095             : 
    1096             :     /* Series of verification in case bit allocated != the budget */
    1097        9060 :     IF( SWB_bit_budget > 0 )
    1098             :     {
    1099        9055 :         i = sub( st_band, 1 );
    1100       42954 :         WHILE( SWB_bit_budget > 0 )
    1101             :         {
    1102             :             /* bits_per_bands[i]++; */
    1103       33899 :             bits_per_bands[i] = L_add( bits_per_bands[i], Q18_1_0 ); /*Q18*/
    1104       33899 :             move32();
    1105             :             /* SWB_bit_budget--; */
    1106       33899 :             SWB_bit_budget = L_sub( SWB_bit_budget, Q18_1_0 ); /*Q18*/
    1107       33899 :             i = sub( i, 1 );
    1108       33899 :             if ( EQ_16( i, -1 ) )
    1109             :             {
    1110        2801 :                 i = sub( st_band, 1 );
    1111             :             }
    1112             :         }
    1113             :     }
    1114             : 
    1115             :     /*nb_bands = nb_tot_bands_loc;*/
    1116             : 
    1117        9060 :     sum_bit = 0;
    1118        9060 :     move32();
    1119        9060 :     j = 0;
    1120        9060 :     move16();
    1121       82332 :     FOR( i = 0; i < nb_tot_bands_loc; i++ )
    1122             :     {
    1123             :         /* if (bits_per_bands[i] > 112) */
    1124       73272 :         IF( GT_32( bits_per_bands[i], Q18_112 ) )
    1125             :         {
    1126             :             /* sum_bit += bits_per_bands[i] - 112; */
    1127           0 :             sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], Q18_112 ) );
    1128             :             /* bits_per_bands[i] = 112; */
    1129           0 :             bits_per_bands[i] = Q18_112;
    1130           0 :             move32();
    1131           0 :             j = add( j, add( i, 1 ) );
    1132             :         }
    1133             : 
    1134             :         /* safety check for overage bit reallocation */
    1135             :         /* else if (bits_per_bands[i] + sum_bit / 3 > 112) */
    1136       73272 :         ELSE IF( GT_32( L_add( bits_per_bands[i], Mpy_32_16_1( sum_bit, Q15_0_33 ) ), Q18_112 ) )
    1137             :         {
    1138           0 :             j = add( j, add( i, 1 ) );
    1139             :         }
    1140             :     }
    1141             : 
    1142        9060 :     IF( sum_bit != 0 )
    1143             :     {
    1144             :         /* sum_bit /= (nb_bands - j); */
    1145           0 :         d_tmp = Find_norm_inv_fx( L_deposit_h( sub( nb_tot_bands_loc, j ) ), &e_div );
    1146           0 :         sum_bit = L_shr( Mpy_32_16_1( sum_bit, d_tmp ), e_div ); /* Q18 */
    1147           0 :         FOR( i = j; i < nb_tot_bands_loc; i++ )
    1148             :         {
    1149           0 :             bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
    1150           0 :             move32();
    1151             :         }
    1152             :     }
    1153             : 
    1154        9060 :     return nb_tot_bands_loc;
    1155             : }
    1156             : 
    1157             : 
    1158             : /* Find normalized 1 / ToDivide */
    1159       14937 : static Word16 Find_norm_inv_fx(
    1160             :     const Word32 ToDivide,
    1161             :     Word16 *e_div )
    1162             : {
    1163             :     Word16 d_tmp, e_tmp;
    1164             : 
    1165       14937 :     e_tmp = norm_l( ToDivide );
    1166       14937 :     d_tmp = round_fx( L_shl( ToDivide, e_tmp ) );
    1167       14937 :     d_tmp = div_s( 16384, d_tmp ); /* 1.0 in Q14, dividend is normalize so >= 16384 as required for the division */
    1168       14937 :     *e_div = sub( 14, e_tmp );
    1169       14937 :     move16();
    1170             : 
    1171       14937 :     return d_tmp;
    1172             : }

Generated by: LCOV version 1.14