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

Generated by: LCOV version 1.14