LCOV - code coverage report
Current view: top level - lib_com - bits_alloc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 791 935 84.6 %
Date: 2025-05-03 01:55:50 Functions: 10 10 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 <stdlib.h>
       6             : #include <assert.h>
       7             : #include "options.h"
       8             : #include "prot_fx.h"
       9             : #include "ivas_prot_fx.h"
      10             : #include "basop_util.h"
      11             : #include "rom_com.h"
      12             : 
      13             : #include "ivas_cnst.h"
      14             : #include "ivas_rom_com.h"
      15             : #ifndef SWAP
      16             : #define SWAP( a, b )   \
      17             :     {                  \
      18             :         tempr = ( a ); \
      19             :         ( a ) = ( b ); \
      20             :         ( b ) = tempr; \
      21             :     }
      22             : #endif
      23             : /*-------------------------------------------------------------------*
      24             :  * Local function prototypes
      25             :  *--------------------------------------------------------------------*/
      26             : static Word16 BITS_ALLOC_adjust_acelp_fixed_cdk( const Word16 bits_frame, Word16 *fixed_cdk_index, const Word16 nb_subfr );
      27             : static Word16 BITS_ALLOC_adjust_generic( const Word16 bits_frame, Word16 *fixed_cdk_index, const Word16 nb_subfr, const Word16 *pulseconfigbits, const Word16 pulseconfig_size );
      28             : static Word16 allocate_unused( const Word32 core_brate, const Word16 coder_type, const Word16 unused_bits, const Word16 nb_prm, const Word16 subfr, const Word16 prm_type, Word16 *prm_bit_mode );
      29             : 
      30             : typedef enum
      31             : {
      32             :     GAINSPRM = 0x0,
      33             :     PITCHPRM = 0x1,
      34             :     MID_LSFSPRM = 0x3,
      35             :     LSFPRM = 0x4
      36             : } PRM_TYPES;
      37             : 
      38             : /*
      39             :  * function  BITS_ALLOC_init_config_acelp()
      40             :  *
      41             :  * description: initial configuration for ACELP
      42             :  *
      43             :  *  return: void
      44             :  */
      45       60563 : void BITS_ALLOC_init_config_acelp(
      46             :     const Word32 bit_rate,
      47             :     const Word16 narrowBand,
      48             :     const Word16 nb_subfr,
      49             :     ACELP_config *pConfigAcelp /*o:  configuration structure of ACELP*/
      50             : )
      51             : {
      52             :     Word8 rate_mode_index;
      53             : 
      54       60563 :     rate_mode_index = (Word8) GT_32( bit_rate, ACELP_9k60 );
      55             : 
      56       60563 :     pConfigAcelp->mode_index = rate_mode_index;
      57       60563 :     move16();
      58             : 
      59             :     /*LPC: midLpc should be swithced off?*/
      60       60563 :     pConfigAcelp->midLpc_enable = 1;
      61       60563 :     move16();
      62             : 
      63             :     /*ACELP ICB config*/
      64       60563 :     test();
      65       60563 :     IF( ( rate_mode_index == 0 ) || ( narrowBand != 0 ) )
      66             :     {
      67        2349 :         move16();
      68        2349 :         move16();
      69        2349 :         move16();
      70        2349 :         move16();
      71        2349 :         move16();
      72        2349 :         move16();
      73        2349 :         pConfigAcelp->pre_emphasis = 1;
      74        2349 :         pConfigAcelp->formant_enh = 1;
      75        2349 :         pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
      76        2349 :         pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2;
      77        2349 :         pConfigAcelp->formant_tilt = 0;
      78        2349 :         pConfigAcelp->voice_tilt = 0;
      79             :     }
      80             :     ELSE
      81             :     {
      82       58214 :         move16();
      83       58214 :         move16();
      84       58214 :         move16();
      85       58214 :         move16();
      86       58214 :         move16();
      87       58214 :         move16();
      88       58214 :         pConfigAcelp->pre_emphasis = 0;
      89       58214 :         pConfigAcelp->formant_enh = 1;
      90       58214 :         pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
      91       58214 :         pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2;
      92       58214 :         pConfigAcelp->formant_tilt = 1;
      93       58214 :         pConfigAcelp->voice_tilt = 1;
      94             :     }
      95             : 
      96             :     /*Wide band @ 16kHz*/
      97       60563 :     IF( EQ_16( nb_subfr, NB_SUBFR16k ) )
      98             :     {
      99       25629 :         move16();
     100       25629 :         move16();
     101       25629 :         move16();
     102       25629 :         move16();
     103       25629 :         move16();
     104       25629 :         move16();
     105       25629 :         pConfigAcelp->pre_emphasis = 1;
     106       25629 :         pConfigAcelp->formant_enh = 1;
     107       25629 :         pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     108       25629 :         pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2_16k;
     109       25629 :         pConfigAcelp->formant_tilt = 0;
     110       25629 :         pConfigAcelp->voice_tilt = 2;
     111             :     }
     112       60563 : }
     113             : 
     114             : 
     115             : /*
     116             :  * function  BITS_ALLOC_config_acelp()
     117             :  *
     118             :  * description: configure all Mode 2 ACELP coder types and allocate the bits
     119             :  *
     120             :  *  return: bit demand
     121             :  */
     122        1214 : Word16 BITS_ALLOC_config_acelp(
     123             :     const Word16 bits_frame,    /* i: remaining bit budget for the frame*/
     124             :     const Word16 coder_type,    /* i: acelp coder type*/
     125             :     ACELP_config *pConfigAcelp, /* i/o:  configuration structure of ACELP*/
     126             :     const Word16 narrowBand,    /* i  : narrowband flag                     */
     127             :     const Word16 nb_subfr       /* i  : number of subframes                 */
     128             : )
     129             : {
     130             :     Word16 mode_index;
     131             :     Word16 band_index;
     132             :     Word16 i;
     133             :     Word16 remaining_bits, bits;
     134             : 
     135             : 
     136        1214 :     move16();
     137        1214 :     move16();
     138        1214 :     move16();
     139        1214 :     mode_index = pConfigAcelp->mode_index;
     140        1214 :     band_index = ( narrowBand == 0 );
     141        1214 :     bits = 0;
     142             : 
     143        1214 :     IF( band_index == 0 )
     144             :     {
     145           0 :         move16();
     146           0 :         pConfigAcelp->formant_enh = 1;
     147           0 :         if ( coder_type == INACTIVE )
     148             :         {
     149           0 :             move16();
     150           0 :             pConfigAcelp->formant_enh = 0;
     151             :         }
     152             :     }
     153             : 
     154        1214 :     test();
     155        1214 :     IF( EQ_16( band_index, 1 ) && EQ_16( nb_subfr, 4 ) )
     156             :     {
     157           0 :         IF( coder_type == INACTIVE )
     158             :         {
     159           0 :             pConfigAcelp->pre_emphasis = 0;
     160           0 :             move16();
     161           0 :             pConfigAcelp->formant_enh = 0;
     162           0 :             move16();
     163           0 :             pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     164           0 :             move16();
     165           0 :             pConfigAcelp->voice_tilt = 1;
     166           0 :             move16();
     167           0 :             pConfigAcelp->formant_tilt = 1;
     168           0 :             move16();
     169             :         }
     170             :         ELSE
     171             :         {
     172           0 :             pConfigAcelp->pre_emphasis = 1;
     173           0 :             move16();
     174           0 :             pConfigAcelp->formant_enh = 1;
     175           0 :             move16();
     176           0 :             pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
     177           0 :             move16();
     178           0 :             pConfigAcelp->voice_tilt = 0;
     179           0 :             move16();
     180           0 :             pConfigAcelp->formant_tilt = 0;
     181           0 :             move16();
     182             :         }
     183             :     }
     184        1214 :     IF( EQ_16( coder_type, UNVOICED ) )
     185             :     {
     186          57 :         IF( EQ_16( ACELP_GAINS_MODE[mode_index][band_index][coder_type], 6 ) )
     187             :         {
     188           0 :             pConfigAcelp->pitch_sharpening = 0;
     189           0 :             move16();
     190           0 :             pConfigAcelp->phase_scrambling = 1;
     191           0 :             move16();
     192             :         }
     193             :         ELSE
     194             :         {
     195          57 :             pConfigAcelp->pitch_sharpening = 0;
     196          57 :             move16();
     197          57 :             pConfigAcelp->phase_scrambling = 0;
     198          57 :             move16();
     199             :         }
     200             :     }
     201             :     ELSE
     202             :     {
     203        1157 :         pConfigAcelp->pitch_sharpening = 1;
     204        1157 :         move16();
     205        1157 :         pConfigAcelp->phase_scrambling = 0;
     206        1157 :         move16();
     207             :     }
     208             : 
     209        1214 :     IF( GT_16( coder_type, ACELP_MODE_MAX ) ) /* keep pitch sharpening for RF_ALLPRED mode */
     210             :     {
     211           0 :         pConfigAcelp->pitch_sharpening = 0;
     212           0 :         move16();
     213           0 :         pConfigAcelp->phase_scrambling = 0;
     214           0 :         move16();
     215             :     }
     216             : 
     217             :     /*Allocate bits and different modes*/
     218        1214 :     move16();
     219        1214 :     pConfigAcelp->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
     220        1214 :     bits = add( bits, ACELP_BPF_BITS[pConfigAcelp->bpf_mode] );
     221             : 
     222        1214 :     move16();
     223        1214 :     move16();
     224        1214 :     pConfigAcelp->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
     225        1214 :     pConfigAcelp->nrg_bits = ACELP_NRG_BITS[pConfigAcelp->nrg_mode];
     226        1214 :     bits = add( bits, pConfigAcelp->nrg_bits );
     227             : 
     228        1214 :     move16();
     229        1214 :     pConfigAcelp->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
     230             : 
     231        1214 :     move16();
     232        1214 :     pConfigAcelp->ltp_bits = 0;
     233             : 
     234        1214 :     move16();
     235        1214 :     pConfigAcelp->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
     236             : 
     237        1214 :     move16();
     238        1214 :     pConfigAcelp->ltf_bits = ACELP_LTF_BITS[pConfigAcelp->ltf_mode];
     239        1214 :     test();
     240        1214 :     IF( EQ_16( nb_subfr, 5 ) && EQ_16( pConfigAcelp->ltf_bits, 4 ) )
     241             :     {
     242         534 :         pConfigAcelp->ltf_bits = add( pConfigAcelp->ltf_bits, 1 );
     243         534 :         move16();
     244             :     }
     245        1214 :     bits = add( bits, pConfigAcelp->ltf_bits );
     246             : 
     247             : 
     248        7284 :     FOR( i = 0; i < nb_subfr; i++ )
     249             :     {
     250        6070 :         pConfigAcelp->gains_mode[i] = ACELP_GAINS_MODE[mode_index][band_index][coder_type];
     251        6070 :         move16();
     252             : 
     253             :         /* skip subframe 1, 3 gain encoding, and use from subframe 0, and 3, respectively */
     254        6070 :         test();
     255        6070 :         test();
     256        6070 :         if ( GE_16( coder_type, ACELP_MODE_MAX ) && ( EQ_16( i, 1 ) || EQ_16( i, 3 ) ) )
     257             :         {
     258           0 :             pConfigAcelp->gains_mode[i] = 0;
     259           0 :             move16();
     260             :         }
     261             : 
     262        6070 :         bits = add( bits, ACELP_GAINS_BITS[pConfigAcelp->gains_mode[i]] );
     263             : 
     264        6070 :         move16();
     265        6070 :         bits = add( bits, ACELP_LTP_BITS_SFR[pConfigAcelp->ltp_mode][i] );
     266        6070 :         pConfigAcelp->ltp_bits = add( pConfigAcelp->ltp_bits, ACELP_LTP_BITS_SFR[pConfigAcelp->ltp_mode][i] );
     267             :     }
     268             : 
     269             :     /*Innovation*/
     270             : 
     271        1214 :     IF( LT_16( bits_frame, bits ) )
     272             :     {
     273           0 :         printf( "Warning: bits per frame too low\n" );
     274           0 :         return -1;
     275             :     }
     276             : 
     277        1214 :     IF( EQ_16( coder_type, RF_ALLPRED ) )
     278             :     {
     279           0 :         set16_fx( pConfigAcelp->fixed_cdk_index, -1, nb_subfr );
     280             :     }
     281        1214 :     ELSE IF( EQ_16( coder_type, RF_GENPRED ) )
     282             :     {
     283           0 :         pConfigAcelp->fixed_cdk_index[0] = 0; /* 7 bits */
     284           0 :         move16();
     285           0 :         pConfigAcelp->fixed_cdk_index[1] = -1;
     286           0 :         move16();
     287           0 :         pConfigAcelp->fixed_cdk_index[2] = 0; /* 7 bits */
     288           0 :         move16();
     289           0 :         pConfigAcelp->fixed_cdk_index[3] = -1;
     290           0 :         move16();
     291           0 :         pConfigAcelp->fixed_cdk_index[4] = -1;
     292           0 :         move16();
     293           0 :         bits = add( bits, 14 );
     294             :     }
     295        1214 :     ELSE IF( EQ_16( coder_type, RF_NOPRED ) )
     296             :     {
     297           0 :         set16_fx( pConfigAcelp->fixed_cdk_index, 0, nb_subfr );
     298           0 :         bits = add( bits, 28 );
     299             :     }
     300             :     ELSE
     301             :     {
     302        1214 :         bits = add( bits, BITS_ALLOC_adjust_acelp_fixed_cdk( sub( bits_frame, bits ), pConfigAcelp->fixed_cdk_index, nb_subfr ) );
     303             :     }
     304             : 
     305        1214 :     remaining_bits = sub( bits_frame, bits );
     306             : 
     307             :     /*Sanity check*/
     308        1214 :     if ( remaining_bits < 0 )
     309             :     {
     310           0 :         move16();
     311           0 :         bits = -1;
     312             :     }
     313             : 
     314             : 
     315        1214 :     return ( bits );
     316             : }
     317             : 
     318             : 
     319        1214 : static Word16 BITS_ALLOC_adjust_generic(
     320             :     const Word16 bits_frame, /*i: bit budget*/
     321             :     Word16 *fixed_cdk_index,
     322             :     const Word16 nb_subfr,
     323             :     const Word16 *pulseconfigbits,
     324             :     const Word16 pulseconfig_size )
     325             : {
     326             :     Word16 bits_subframe2, inb_subfr;
     327             :     Word16 sfr, k, bitsused, bits_currsubframe;
     328             : 
     329        1214 :     bits_subframe2 = bits_frame;
     330        1214 :     move16();
     331        1214 :     inb_subfr = 8192 /*1.0f/NB_SUBFR Q15*/;
     332        1214 :     move16();
     333        1214 :     if ( EQ_16( nb_subfr, NB_SUBFR16k ) )
     334             :     {
     335        1214 :         inb_subfr = 6554 /*1.0f/NB_SUBFR16k Q15*/;
     336        1214 :         move16();
     337             :     }
     338             : 
     339        1214 :     IF( LT_16( bits_subframe2, i_mult2( pulseconfigbits[0], nb_subfr ) ) ) /* not in final code - not instrumented */
     340             :     {
     341           0 :         return add( bits_frame, 1 ); /* Not enough bits for lowest mode. -> trigger alarm*/
     342             :     }
     343             : 
     344             :     /* search cdk-index for first subframe */
     345       32520 :     FOR( k = 0; k < pulseconfig_size - 1; k++ )
     346             :     {
     347             : 
     348       32520 :         IF( GT_16( i_mult2( pulseconfigbits[k], nb_subfr ), bits_subframe2 ) )
     349             :         {
     350        1214 :             k = sub( k, 1 ); /* previous mode did not exceed bit-budget */
     351        1214 :             BREAK;
     352             :         }
     353             :     }
     354             : 
     355        1214 :     if ( GT_16( i_mult2( pulseconfigbits[k], nb_subfr ), bits_subframe2 ) )
     356             :     {
     357           0 :         k = sub( k, 1 ); /* previous mode did not exceed bit-budget */
     358             :     }
     359             : 
     360        1214 :     move16();
     361        1214 :     fixed_cdk_index[0] = k;
     362        1214 :     bitsused = i_mult2( pulseconfigbits[k], nb_subfr );
     363             : 
     364        6070 :     FOR( sfr = 1; sfr < nb_subfr; sfr++ )
     365             :     {
     366             :         /*bits_currsubframe = (int)(((float)sfr+1.0f)*bits_subframe) - bitsused;*/
     367        4856 :         bits_currsubframe = sub( add( i_mult2( sfr, bits_subframe2 ), bits_subframe2 ), bitsused );
     368             : 
     369             :         /* try increasing mode while below threshold */
     370        4856 :         test();
     371        5650 :         WHILE( ( ( k < sub( pulseconfig_size, 1 ) ) ) && ( ( i_mult2( pulseconfigbits[k + 1], nb_subfr ) <= bits_currsubframe ) ) )
     372             :         {
     373         794 :             test();
     374         794 :             k = add( k, 1 );
     375             :         }
     376             : 
     377             :         /* try decreasing mode until below threshold */
     378        5392 :         WHILE( ( i_mult2( pulseconfigbits[k], nb_subfr ) > bits_currsubframe ) )
     379             :         {
     380         536 :             k = sub( k, 1 );
     381             : 
     382         536 :             IF( k == 0 )
     383             :             {
     384           0 :                 BREAK;
     385             :             }
     386             :         }
     387             : 
     388             :         /* store mode */
     389        4856 :         move16();
     390        4856 :         fixed_cdk_index[sfr] = k;
     391        4856 :         bitsused = add( bitsused, i_mult2( pulseconfigbits[k], nb_subfr ) );
     392             :     }
     393             : 
     394        1214 :     return mult_r( bitsused, inb_subfr );
     395             : }
     396             : 
     397        1214 : static Word16 BITS_ALLOC_adjust_acelp_fixed_cdk(
     398             :     const Word16 bits_frame, /*i: bit budget*/
     399             :     Word16 *fixed_cdk_index,
     400             :     const Word16 nb_subfr )
     401             : {
     402             :     Word16 bitsused;
     403             : 
     404             : 
     405        1214 :     bitsused = BITS_ALLOC_adjust_generic( bits_frame, fixed_cdk_index, nb_subfr, ACELP_CDK_BITS, ACELP_FIXED_CDK_NB );
     406             : 
     407             : 
     408        1214 :     return bitsused;
     409             : }
     410             : 
     411             : 
     412             : /*-------------------------------------------------------------------*
     413             :  * fcb_table()
     414             :  *
     415             :  * Selection of fixed innovation codebook bitbudget table
     416             :  *--------------------------------------------------------------------*/
     417             : 
     418     4606238 : static Word16 fcb_table(
     419             :     const Word16 n,
     420             :     const Word16 L_subfr )
     421             : {
     422             :     Word16 out;
     423             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
     424             : 
     425     4606238 :     out = PulseConfTable[n].bits;
     426     4606238 :     move16();
     427     4606238 :     if ( GT_16( L_subfr, L_SUBFR ) )
     428             :     {
     429          40 :         out = fast_FCB_bits_2sfr[n];
     430          40 :         move16();
     431             :     }
     432             : 
     433     4606238 :     return ( out );
     434             : }
     435             : 
     436             : /*-------------------------------------------------------------------*
     437             :  * acelp_FCB_allocator()
     438             :  *
     439             :  * Routine to allocate fixed innovation codebook bit-budget
     440             :  *--------------------------------------------------------------------*/
     441             : 
     442             : #ifndef REMOVE_EVS_DUPLICATES
     443             : static ivas_error acelp_FCB_allocator(
     444             :     Word16 *nBits,            /* i/o: available bit-budget    */
     445             :     Word16 fixed_cdk_index[], /* o  : codebook index       Q0 */
     446             :     Word16 nb_subfr,          /* i  : number of subframes     */
     447             :     const Word16 L_subfr,     /* i  : subframe length         */
     448             :     const Word16 coder_type,  /* i  : coder type              */
     449             :     const Word16 tc_subfr,    /* i  : TC subframe index       */
     450             :     const Word16 fix_first    /* i  : flag to indicate whether the first subframe bit-budget was fixed */
     451             : )
     452             : {
     453             :     Word16 cdbk, sfr, step;
     454             :     Word16 nBits_tmp;
     455             :     Word16 *p_fixed_cdk_index;
     456             :     Word16 max_n;
     457             :     ivas_error error;
     458             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
     459             : 
     460             :     error = IVAS_ERR_OK;
     461             :     move32();
     462             : 
     463             :     cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
     464             :     move16();
     465             : 
     466             :     p_fixed_cdk_index = fixed_cdk_index;
     467             : 
     468             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse not in the first subframe */
     469             :     test();
     470             :     IF( GE_16( tc_subfr, L_SUBFR ) && fix_first )
     471             :     {
     472             :         Word16 i;
     473             : 
     474             :         FOR( i = 0; i < nb_subfr; i++ )
     475             :         {
     476             :             *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] ) );
     477             :             move16();
     478             :         }
     479             :         return error;
     480             :     }
     481             : 
     482             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
     483             :     sfr = 0;
     484             :     move16();
     485             :     IF( fix_first )
     486             :     {
     487             :         *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] ) );
     488             :         move16();
     489             :         sfr = 1;
     490             :         move16();
     491             :         p_fixed_cdk_index++;
     492             :         nb_subfr = 3;
     493             :         move16();
     494             :     }
     495             : 
     496             :     /* distribute the bit-budget equally between subframes */
     497             :     IF( GT_16( L_subfr, L_SUBFR ) ) /* access fast_FCB_bits_2sfr */
     498             :     {
     499             :         max_n = 6;
     500             :         move16();
     501             :     }
     502             :     ELSE
     503             :     {
     504             :         max_n = ACELP_FIXED_CDK_NB;
     505             :         move16();
     506             :     }
     507             :     FOR( cdbk = 0; cdbk < max_n; cdbk++ )
     508             :     {
     509             :         IF( GT_32( L_mult0( fcb_table( cdbk, L_subfr ), nb_subfr ), L_deposit_l( *nBits ) ) )
     510             :         {
     511             :             BREAK;
     512             :         }
     513             :     }
     514             :     cdbk = sub( cdbk, 1 );
     515             : 
     516             : #ifdef DEBUGGING
     517             :     if ( cdbk < 0 && coder_type != TRANSITION )
     518             :     {
     519             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bit-budget for fixed innovation codebook (frame = %d). Exiting! \n" );
     520             :     }
     521             :     if ( ( L_subfr == L_SUBFR && cdbk >= ACELP_FIXED_CDK_NB ) || ( L_subfr == 2 * L_SUBFR && fcb_table( cdbk, L_subfr ) == 128 /*stop value*/ ) )
     522             :     {
     523             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too high bit-budget for fixed innovation codebook (frame = %d). Exiting! \n" );
     524             :     }
     525             : #endif
     526             : 
     527             :     set16_fx( p_fixed_cdk_index, cdbk, nb_subfr );
     528             :     nBits_tmp = 0;
     529             :     move16();
     530             :     IF( cdbk >= 0 )
     531             :     {
     532             :         nBits_tmp = fcb_table( cdbk, L_subfr );
     533             :     }
     534             :     ELSE
     535             :     {
     536             :         nBits_tmp = 0;
     537             :         move16();
     538             :     }
     539             :     *nBits = sub( *nBits, i_mult( nBits_tmp, nb_subfr ) );
     540             :     move16();
     541             : 
     542             :     /* try to increase the FCB bit-budget of the first subframe(s) */
     543             :     IF( LT_16( cdbk, ACELP_FIXED_CDK_NB - 1 ) )
     544             :     {
     545             :         step = sub( fcb_table( add( cdbk, 1 ), L_subfr ), nBits_tmp );
     546             :         WHILE( *nBits >= step )
     547             :         {
     548             :             ( *p_fixed_cdk_index )++;
     549             :             *nBits = sub( *nBits, step );
     550             :             move16();
     551             :             p_fixed_cdk_index++;
     552             :         }
     553             : 
     554             :         /* try to increase the FCB of the first subframe in cases when the next step is lower than the current step */
     555             :         step = sub( fcb_table( add( fixed_cdk_index[sfr], 1 ), L_subfr ), fcb_table( fixed_cdk_index[sfr], L_subfr ) );
     556             :         test();
     557             :         IF( GE_16( *nBits, step ) && cdbk >= 0 )
     558             :         {
     559             :             fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
     560             :             move16();
     561             :             *nBits = sub( *nBits, step );
     562             :             move16();
     563             :             test();
     564             :             IF( GE_16( *nBits, step ) && EQ_16( fixed_cdk_index[sfr + 1], sub( fixed_cdk_index[sfr], 1 ) ) )
     565             :             {
     566             :                 sfr = add( sfr, 1 );
     567             :                 fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
     568             :                 move16();
     569             :                 *nBits = sub( *nBits, step );
     570             :                 move16();
     571             :             }
     572             :         }
     573             :     }
     574             :     /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
     575             :     IF( GE_16( tc_subfr, L_SUBFR ) )
     576             :     {
     577             :         Word16 tempr;
     578             : 
     579             :         SWAP( fixed_cdk_index[0], fixed_cdk_index[tc_subfr / L_SUBFR] );
     580             : 
     581             :         /* TRANSITION coding: allocate second highest FCBQ bit-budget to the last subframe */
     582             :         IF( idiv1616( tc_subfr, L_SUBFR ) < sub( nb_subfr, 1 ) )
     583             :         {
     584             :             SWAP( fixed_cdk_index[( tc_subfr - L_SUBFR ) / L_SUBFR], fixed_cdk_index[nb_subfr - 1] );
     585             :         }
     586             :     }
     587             : 
     588             :     /* when subframe length > L_SUBFR, number of bits instead of codebook index is signalled */
     589             :     IF( GT_16( L_subfr, L_SUBFR ) )
     590             :     {
     591             :         Word16 i, j;
     592             :         FOR( i = 0; i < nb_subfr; i++ )
     593             :         {
     594             :             j = fixed_cdk_index[i];
     595             :             move16();
     596             :             fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
     597             :             move16();
     598             :         }
     599             :     }
     600             : 
     601             :     return error;
     602             : }
     603             : #endif
     604      236414 : static ivas_error acelp_FCB_allocator_ivas(
     605             :     Word16 *nBits,            /* i/o: available bit-budget    */
     606             :     Word16 fixed_cdk_index[], /* o  : codebook index       Q0 */
     607             :     Word16 nb_subfr,          /* i  : number of subframes     */
     608             :     const Word16 L_subfr,     /* i  : subframe length         */
     609             :     const Word16 coder_type,  /* i  : coder type              */
     610             :     const Word16 tc_subfr,    /* i  : TC subframe index       */
     611             :     const Word16 fix_first    /* i  : flag to indicate whether the first subframe bit-budget was fixed */
     612             : )
     613             : {
     614             :     Word16 cdbk, sfr, step;
     615             :     Word16 nBits_tmp;
     616             :     Word16 *p_fixed_cdk_index;
     617             :     Word16 max_n;
     618             :     ivas_error error;
     619             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
     620             : 
     621      236414 :     error = IVAS_ERR_OK;
     622      236414 :     move32();
     623             : 
     624      236414 :     cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
     625      236414 :     move16();
     626             : 
     627      236414 :     p_fixed_cdk_index = fixed_cdk_index;
     628             : 
     629             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse not in the first subframe */
     630      236414 :     test();
     631      236414 :     IF( GE_16( tc_subfr, L_SUBFR ) && fix_first )
     632             :     {
     633             :         Word16 i;
     634             : 
     635        8585 :         FOR( i = 0; i < nb_subfr; i++ )
     636             :         {
     637        6868 :             *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] ) );
     638        6868 :             move16();
     639             :         }
     640        1717 :         return error;
     641             :     }
     642             : 
     643             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
     644      234697 :     sfr = 0;
     645      234697 :     move16();
     646      234697 :     IF( fix_first )
     647             :     {
     648        7301 :         *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] ) );
     649        7301 :         move16();
     650        7301 :         sfr = 1;
     651        7301 :         move16();
     652        7301 :         p_fixed_cdk_index++;
     653        7301 :         nb_subfr = 3;
     654        7301 :         move16();
     655             :     }
     656             : 
     657             :     /* distribute the bit-budget equally between subframes */
     658      234697 :     IF( GT_16( L_subfr, L_SUBFR ) ) /* access fast_FCB_bits_2sfr */
     659             :     {
     660           5 :         max_n = 6;
     661           5 :         move16();
     662             :     }
     663             :     ELSE
     664             :     {
     665      234692 :         max_n = ACELP_FIXED_CDK_NB;
     666      234692 :         move16();
     667             :     }
     668     3667450 :     FOR( cdbk = 0; cdbk < max_n; cdbk++ )
     669             :     {
     670     3667450 :         IF( GT_32( L_mult0( fcb_table( cdbk, L_subfr ), nb_subfr ), L_deposit_l( *nBits ) ) )
     671             :         {
     672      234697 :             BREAK;
     673             :         }
     674             :     }
     675      234697 :     cdbk = sub( cdbk, 1 );
     676             : 
     677      234697 :     set16_fx( p_fixed_cdk_index, cdbk, nb_subfr );
     678      234697 :     nBits_tmp = 0;
     679      234697 :     move16();
     680      234697 :     IF( cdbk >= 0 )
     681             :     {
     682      234697 :         nBits_tmp = fcb_table( cdbk, L_subfr );
     683             :     }
     684             :     ELSE
     685             :     {
     686           0 :         nBits_tmp = 0;
     687           0 :         move16();
     688             :     }
     689      234697 :     *nBits = sub( *nBits, i_mult( nBits_tmp, nb_subfr ) );
     690      234697 :     move16();
     691             : 
     692             :     /* try to increase the FCB bit-budget of the first subframe(s) */
     693      234697 :     IF( LT_16( cdbk, ACELP_FIXED_CDK_NB - 1 ) )
     694             :     {
     695      234697 :         step = sub( fcb_table( add( cdbk, 1 ), L_subfr ), nBits_tmp );
     696      630735 :         WHILE( *nBits >= step )
     697             :         {
     698      396038 :             ( *p_fixed_cdk_index )++;
     699      396038 :             *nBits = sub( *nBits, step );
     700      396038 :             move16();
     701      396038 :             p_fixed_cdk_index++;
     702             :         }
     703             : 
     704             :         /* try to increase the FCB of the first subframe in cases when the next step is lower than the current step */
     705      234697 :         IF( LT_16( fixed_cdk_index[sfr], ACELP_FIXED_CDK_NB - 1 ) )
     706             :         {
     707      234697 :             step = sub( fcb_table( add( fixed_cdk_index[sfr], 1 ), L_subfr ), fcb_table( fixed_cdk_index[sfr], L_subfr ) );
     708      234697 :             test();
     709      234697 :             IF( GE_16( *nBits, step ) && cdbk >= 0 )
     710             :             {
     711       35096 :                 fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
     712       35096 :                 move16();
     713       35096 :                 *nBits = sub( *nBits, step );
     714       35096 :                 move16();
     715       35096 :                 test();
     716       35096 :                 IF( GE_16( *nBits, step ) && EQ_16( fixed_cdk_index[sfr + 1], sub( fixed_cdk_index[sfr], 1 ) ) )
     717             :                 {
     718        3052 :                     sfr = add( sfr, 1 );
     719        3052 :                     fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
     720        3052 :                     move16();
     721        3052 :                     *nBits = sub( *nBits, step );
     722        3052 :                     move16();
     723             :                 }
     724             :             }
     725             :         }
     726             :     }
     727             :     /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
     728      234697 :     IF( GE_16( tc_subfr, L_SUBFR ) )
     729             :     {
     730             :         Word16 tempr;
     731             : 
     732       12528 :         SWAP( fixed_cdk_index[0], fixed_cdk_index[tc_subfr / L_SUBFR] );
     733             : 
     734             :         /* TRANSITION coding: allocate second highest FCBQ bit-budget to the last subframe */
     735       12528 :         IF( LT_16( idiv1616( tc_subfr, L_SUBFR ), sub( nb_subfr, 1 ) ) )
     736             :         {
     737        6047 :             SWAP( fixed_cdk_index[sub( tc_subfr, L_SUBFR ) / L_SUBFR], fixed_cdk_index[nb_subfr - 1] );
     738             :         }
     739             :     }
     740             : 
     741             :     /* when subframe length > L_SUBFR, number of bits instead of codebook index is signalled */
     742      234697 :     IF( GT_16( L_subfr, L_SUBFR ) )
     743             :     {
     744             :         Word16 i, j;
     745          15 :         FOR( i = 0; i < nb_subfr; i++ )
     746             :         {
     747          10 :             j = fixed_cdk_index[i];
     748          10 :             move16();
     749          10 :             fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
     750          10 :             move16();
     751             :         }
     752             :     }
     753             : 
     754      234697 :     return error;
     755             : }
     756             : 
     757             : /*-------------------------------------------------------------------*
     758             :  * config_acelp1()
     759             :  *
     760             :  * Configure ACELP bit allocation
     761             :  *   - should be in range of <6700; 24350> for ACELP@12.8kHz
     762             :  *   - per channel bitrate minimum is 13250 kbps for ACELP@16kHz
     763             :  *--------------------------------------------------------------------*/
     764             : 
     765             : #ifndef REMOVE_EVS_DUPLICATES
     766             : ivas_error config_acelp1(
     767             :     const Word16 enc_dec,                  /* i  : encoder/decoder flag            */
     768             :     const Word32 total_brate,              /* i  : total bitrate                   */
     769             :     const Word32 core_brate_inp,           /* i  : core bitrate                    */
     770             :     const Word16 core,                     /* i  : core                            */
     771             :     const Word16 extl,                     /* i  : extension layer                 */
     772             :     const Word32 extl_brate,               /* i  : extension layer bitrate         */
     773             :     const Word16 L_frame,                  /* i  : frame length at internal Fs     */
     774             :     const Word16 GSC_noisy_speech,         /* i  : GSC on SWB noisy speech flag    */
     775             :     ACELP_config *acelp_cfg,               /* i  : ACELP bit-allocation            */
     776             :     const Word16 signalling_bits,          /* i  : number of signalling bits       */
     777             :     const Word16 coder_type,               /* i  : coder type                      */
     778             :     const Word16 inactive_coder_type_flag, /* i  : AVQ (0) or GSC (1) IC flag      */
     779             :     const Word16 tc_subfr,                 /* i  : TC subfr ID                     */
     780             :     const Word16 tc_call,                  /* i  : TC call number (0,1,2,3,5(DEC)) */
     781             :     Word16 *nBits_es_Pred,                 /* o  : number of bits for Es_pred Q    */
     782             :     Word16 *unbits,                        /* o  : number of unused bits           */
     783             :     const Word16 element_mode,             /* i  : element mode                    */
     784             :     Word16 *uc_two_stage_flag,             /* o  : flag undicating two-stage UC    */
     785             :     const Word16 tdm_lp_reuse_flag,        /* i  : LPC reuse flag (can be 1 only with secondary channel */
     786             :     const Word16 tdm_low_rate_mode,        /* i  : secondary channel low rate mode flag */
     787             :     const Word16 idchan,                   /* i  : stereo channel ID               */
     788             :     const Word16 active_cnt,               /* i  : Active frame counter            */
     789             :     const Word16 tdm_Pitch_reuse_flag,     /* i  : primary channel pitch reuse flag*/
     790             :     const Word16 tdm_LRTD_flag,            /* i  : LRTD stereo mode flag           */
     791             :     const Word16 GSC_IVAS_mode             /* i  : GSC IVAS mode                   */
     792             : )
     793             : {
     794             :     Word16 i, bits, nb_subfr;
     795             :     Word16 flag_hardcoded, coder_type_sw, fix_first;
     796             :     Word32 core_brate;
     797             : #ifdef DEBUGGING
     798             :     (void) active_cnt;
     799             : #endif
     800             :     ivas_error error;
     801             : 
     802             :     error = IVAS_ERR_OK;
     803             :     move32();
     804             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
     805             :     /*-----------------------------------------------------------------*
     806             :      * Set the flag indicating two-stage Unvoiced (UC) frame
     807             :      *-----------------------------------------------------------------*/
     808             : 
     809             :     *uc_two_stage_flag = 0;
     810             :     move16();
     811             :     IF( EQ_16( coder_type, UNVOICED ) )
     812             :     {
     813             :         test();
     814             :         test();
     815             :         test();
     816             :         test();
     817             :         test();
     818             :         if ( GE_32( total_brate, MIN_UNVOICED_TWO_STAGE_BRATE ) && element_mode > EVS_MONO && ( idchan == 0 || ( ( GE_32( total_brate, 8500 ) || extl_brate == 0 ) && EQ_16( tdm_LRTD_flag, 1 ) ) ) )
     819             :         {
     820             :             *uc_two_stage_flag = 1;
     821             :             move16();
     822             :         }
     823             :     }
     824             : 
     825             :     /*-----------------------------------------------------------------*
     826             :      * Set the number of subframes
     827             :      *-----------------------------------------------------------------*/
     828             : 
     829             :     IF( EQ_16( L_frame, L_FRAME ) )
     830             :     {
     831             :         nb_subfr = NB_SUBFR;
     832             :         move16();
     833             : 
     834             : #ifdef DEBUGGING
     835             :         if ( ( ( core_brate_inp < 5900 && coder_type > UNVOICED ) && !( core_brate_inp < MIN_TC_BRATE && coder_type == TRANSITION ) ) && !( idchan > 0 && element_mode == IVAS_CPE_TD ) && !( element_mode == IVAS_SCE && tdm_low_rate_mode ) )
     836             :         {
     837             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bitrate (%d bps) for ACELP@12k8 in frame %d. Exiting!\n", core_brate_inp );
     838             :         }
     839             : 
     840             :         if ( core_brate_inp > ACELP_12k8_HIGH_LIMIT && core == ACELP_CORE )
     841             :         {
     842             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too high bitrate (%d bps) for ACELP@12k8 in frame %d. Exiting!\n", core_brate_inp );
     843             :         }
     844             : #endif
     845             :     }
     846             :     ELSE /* L_frame == L_FRAME16k */
     847             :     {
     848             :         nb_subfr = NB_SUBFR16k;
     849             :         move16();
     850             : 
     851             : #ifdef DEBUGGING
     852             :         if ( core_brate_inp < ACELP_16k_LOW_LIMIT && core == ACELP_CORE )
     853             :         {
     854             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bitrate (%d bps) for ACELP@16k in frame %d. Exiting!\n", core_brate_inp );
     855             :         }
     856             : #endif
     857             :     }
     858             : 
     859             :     coder_type_sw = coder_type;
     860             :     move16();
     861             :     IF( core != ACELP_CORE )
     862             :     {
     863             :         /* used in acelp_core_switch_enc() */
     864             :         nb_subfr = 1;
     865             :         move16();
     866             :         if ( EQ_16( L_frame, L_FRAME ) )
     867             :         {
     868             :             coder_type_sw = TRANSITION;
     869             :             move16();
     870             :         }
     871             :     }
     872             : 
     873             :     /*-----------------------------------------------------------------*
     874             :      * Check if the core_brate is hard coded (to keep BE for mono core) or not
     875             :      *-----------------------------------------------------------------*/
     876             : 
     877             :     flag_hardcoded = 0;
     878             :     move16();
     879             :     i = 0;
     880             :     move16();
     881             : 
     882             :     WHILE( i < SIZE_BRATE_INTERMED_TBL )
     883             :     {
     884             :         IF( EQ_32( core_brate_inp, brate_intermed_tbl[i] ) )
     885             :         {
     886             :             flag_hardcoded = 1;
     887             :             move16();
     888             :             BREAK;
     889             :         }
     890             : 
     891             :         IF( LT_32( core_brate_inp, brate_intermed_tbl[i] ) )
     892             :         {
     893             :             flag_hardcoded = 0;
     894             :             move16();
     895             :             BREAK;
     896             :         }
     897             : 
     898             :         i = add( i, 1 );
     899             :     }
     900             : 
     901             :     test();
     902             :     test();
     903             :     test();
     904             :     IF( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( coder_type, AUDIO ) &&
     905             :         LE_32( core_brate_inp, 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 */
     906             :     {
     907             :         i = sub( i, 1 );
     908             :     }
     909             : 
     910             :     core_brate = brate_intermed_tbl[i];
     911             :     move32();
     912             : 
     913             :     if ( element_mode > EVS_MONO )
     914             :     {
     915             :         flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
     916             :         move16();
     917             :     }
     918             : 
     919             :     test();
     920             :     if ( ( core != ACELP_CORE ) && ( element_mode == EVS_MONO ) ) /* needed for mode1 core switching in EVS mono */
     921             :     {
     922             :         flag_hardcoded = 1;
     923             :         move16();
     924             :     }
     925             : 
     926             :     /*-----------------------------------------------------------------*
     927             :      * ACELP bit allocation
     928             :      *-----------------------------------------------------------------*/
     929             :     test();
     930             :     test();
     931             :     IF( !( EQ_16( coder_type, TRANSITION ) && NE_16( tc_subfr, -1 ) ) || EQ_16( enc_dec, DEC ) )
     932             :     {
     933             :         /* Set the bit-budget */
     934             :         bits = extract_l( Mpy_32_32( core_brate_inp, ONE_BY_FRAMES_PER_SEC_Q31 ) ); // Q0
     935             : 
     936             :         test();
     937             :         test();
     938             :         IF( EQ_16( coder_type, TRANSITION ) && EQ_16( enc_dec, DEC ) && EQ_16( tc_call, 1 ) )
     939             :         {
     940             :             bits = add( bits, *nBits_es_Pred ); /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
     941             :         }
     942             : 
     943             :         /* Subtract signalling bits */
     944             :         test();
     945             :         test();
     946             :         IF( EQ_16( enc_dec, DEC ) && EQ_16( idchan, 1 ) && element_mode > EVS_MONO )
     947             :         {
     948             :             bits = sub( bits, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS );
     949             : 
     950             :             IF( EQ_16( tdm_LRTD_flag, 1 ) )
     951             :             {
     952             :                 bits = add( bits, STEREO_BITS_TCA );
     953             :             }
     954             : 
     955             :             /* subtract TBE/BWE flag */
     956             :             test();
     957             :             test();
     958             :             test();
     959             :             test();
     960             :             test();
     961             :             test();
     962             :             IF( extl_brate > 0 && ( EQ_16( extl, WB_TBE ) || EQ_16( extl, SWB_TBE ) || EQ_16( extl, FB_TBE ) || EQ_16( extl, WB_BWE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) ) )
     963             :             {
     964             :                 bits = sub( bits, 1 );
     965             :             }
     966             :         }
     967             :         ELSE
     968             :         {
     969             :             /* Subtract signalling bits */
     970             :             bits = sub( bits, signalling_bits );
     971             :         }
     972             : 
     973             :         test();
     974             :         test();
     975             :         test();
     976             :         test();
     977             :         test();
     978             :         test();
     979             :         IF( extl_brate > 0 && ( EQ_16( extl, WB_TBE ) || EQ_16( extl, SWB_TBE ) || EQ_16( extl, FB_TBE ) || EQ_16( extl, WB_BWE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) ) )
     980             :         {
     981             :             /* extension layer signalling bit is counted in the extension layer bitbudget */
     982             :             bits = add( bits, 1 );
     983             :         }
     984             : 
     985             :         /*-----------------------------------------------------------------*
     986             :          * LSF Q bit-budget
     987             :          *-----------------------------------------------------------------*/
     988             :         test();
     989             :         test();
     990             :         test();
     991             :         IF( !tdm_lp_reuse_flag || idchan == 0 )
     992             :         {
     993             :             /* LSF Q bit-budget */
     994             :             acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX_fx( core_brate, coder_type )];
     995             :             move16();
     996             : 
     997             :             IF( !flag_hardcoded )
     998             :             {
     999             :                 IF( EQ_16( L_frame, L_FRAME ) )
    1000             :                 {
    1001             :                     test();
    1002             :                     test();
    1003             :                     test();
    1004             :                     test();
    1005             :                     test();
    1006             :                     test();
    1007             :                     test();
    1008             :                     test();
    1009             :                     IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
    1010             :                     {
    1011             :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX_fx( core_brate, coder_type )];
    1012             :                         move16();
    1013             :                     }
    1014             :                     ELSE IF( ( LT_32( total_brate, 7200 ) || coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( idchan, 1 ) )
    1015             :                     {
    1016             :                         /* TD stereo, secondary channel: do nothing */
    1017             :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX_fx( core_brate, coder_type )];
    1018             :                         move16();
    1019             :                     }
    1020             :                     ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) && LT_32( brate_intermed_tbl[i], ACELP_9k60 ) )
    1021             :                     {
    1022             :                         /* primary channel: do nothing */
    1023             :                     }
    1024             :                     ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
    1025             :                     {
    1026             :                         acelp_cfg->lsf_bits = 42;
    1027             :                         move16();
    1028             :                     }
    1029             :                     ELSE IF( LE_32( total_brate, 9600 ) || EQ_16( coder_type, UNVOICED ) )
    1030             :                     {
    1031             :                         acelp_cfg->lsf_bits = 31;
    1032             :                         move16();
    1033             :                     }
    1034             :                     ELSE IF( LE_32( total_brate, 20000 ) )
    1035             :                     {
    1036             :                         acelp_cfg->lsf_bits = 36;
    1037             :                         move16();
    1038             :                     }
    1039             :                     ELSE
    1040             :                     {
    1041             :                         acelp_cfg->lsf_bits = 41;
    1042             :                         move16();
    1043             :                     }
    1044             :                 }
    1045             :                 ELSE /* L_frame == L_FRAME16k */
    1046             :                 {
    1047             :                     acelp_cfg->lsf_bits = 41;
    1048             :                     move16();
    1049             :                 }
    1050             :             }
    1051             : 
    1052             :             bits = sub( bits, acelp_cfg->lsf_bits );
    1053             : 
    1054             :             /* mid-LSF Q bit-budget */
    1055             :             acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX_fx( core_brate, coder_type )];
    1056             :             move16();
    1057             : 
    1058             :             test();
    1059             :             if ( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
    1060             :             {
    1061             :                 acelp_cfg->mid_lsf_bits = 5;
    1062             :                 move16();
    1063             :                 /* primary channel: do nothing */
    1064             :             }
    1065             : 
    1066             :             bits = sub( bits, acelp_cfg->mid_lsf_bits );
    1067             :         }
    1068             :         ELSE IF( EQ_16( tdm_lp_reuse_flag, 1 ) && EQ_16( idchan, 1 ) && NE_16( active_cnt, 1 ) )
    1069             :         {
    1070             :             bits = sub( bits, TDM_IC_LSF_PRED_BITS );
    1071             :         }
    1072             :         /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
    1073             :         test();
    1074             :         test();
    1075             :         test();
    1076             :         test();
    1077             :         test();
    1078             :         test();
    1079             :         IF( ( NE_16( coder_type, UNVOICED ) && NE_16( coder_type, AUDIO ) && coder_type != INACTIVE && !( LE_32( core_brate, ACELP_8k00 ) && NE_16( coder_type, TRANSITION ) ) ) /* mid bitrates in GC and VC, low+mid bitrates in TC */ ||
    1080             :             ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */
    1081             :         )
    1082             :         {
    1083             :             *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX_fx( core_brate, coder_type, -1, -1 )];
    1084             :             move16();
    1085             :             bits = sub( bits, *nBits_es_Pred );
    1086             :         }
    1087             :         ELSE IF( *uc_two_stage_flag )
    1088             :         {
    1089             :             *nBits_es_Pred = 4;
    1090             :             move16();
    1091             :             bits = sub( bits, *nBits_es_Pred );
    1092             :         }
    1093             :     }
    1094             :     ELSE
    1095             :     {
    1096             :         bits = *unbits;
    1097             :         move16();
    1098             :     }
    1099             : 
    1100             :     test();
    1101             :     IF( EQ_16( coder_type, TRANSITION ) && tc_call == 0 )
    1102             :     {
    1103             :         *unbits = bits;
    1104             :         return error;
    1105             :     }
    1106             : 
    1107             :     /*-----------------------------------------------------------------*
    1108             :      * Low-rate mode - bits are allocated in tdm_low_rate_enc()
    1109             :      *-----------------------------------------------------------------*/
    1110             :     test();
    1111             :     IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
    1112             :     {
    1113             :         acelp_cfg->FEC_mode = 0;
    1114             :         acelp_cfg->ltf_mode = FULL_BAND;
    1115             :         *nBits_es_Pred = 0;
    1116             :         *unbits = 0;
    1117             :         acelp_cfg->ubits = 0;
    1118             :         move16();
    1119             :         move16();
    1120             :         move16();
    1121             :         move16();
    1122             :         move16();
    1123             : 
    1124             :         return error;
    1125             :     }
    1126             : 
    1127             :     /*-----------------------------------------------------------------*
    1128             :      * Supplementary information for FEC
    1129             :      *-----------------------------------------------------------------*/
    1130             : 
    1131             :     acelp_cfg->FEC_mode = 0;
    1132             :     move16();
    1133             :     test();
    1134             :     test();
    1135             :     IF( GE_32( core_brate, ACELP_11k60 ) && ( idchan == 0 || element_mode == EVS_MONO ) )
    1136             :     {
    1137             :         acelp_cfg->FEC_mode = 1;
    1138             :         move16();
    1139             : 
    1140             :         test();
    1141             :         test();
    1142             :         IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) && NE_16( coder_type, VOICED ) )
    1143             :         {
    1144             :             bits = sub( bits, FEC_BITS_CLS );
    1145             :         }
    1146             : 
    1147             :         IF( NE_16( coder_type, TRANSITION ) )
    1148             :         {
    1149             :             IF( GE_32( total_brate, ACELP_16k40 ) )
    1150             :             {
    1151             :                 acelp_cfg->FEC_mode = 2;
    1152             :                 move16();
    1153             :                 test();
    1154             :                 IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
    1155             :                 {
    1156             :                     bits = sub( bits, FEC_BITS_ENR );
    1157             :                 }
    1158             :             }
    1159             : 
    1160             :             IF( GE_32( total_brate, ACELP_32k ) )
    1161             :             {
    1162             :                 acelp_cfg->FEC_mode = 3;
    1163             :                 move16();
    1164             : 
    1165             :                 test();
    1166             :                 IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
    1167             :                 {
    1168             :                     bits = sub( bits, FEC_BITS_POS );
    1169             :                 }
    1170             :             }
    1171             :         }
    1172             :     }
    1173             : 
    1174             :     /*-----------------------------------------------------------------*
    1175             :      * LP filtering of the adaptive excitation
    1176             :      *-----------------------------------------------------------------*/
    1177             :     test();
    1178             :     test();
    1179             :     test();
    1180             :     test();
    1181             :     test();
    1182             :     test();
    1183             :     IF( idchan > 0 && element_mode > EVS_MONO )
    1184             :     {
    1185             :         acelp_cfg->ltf_mode = FULL_BAND;
    1186             :         move16();
    1187             :     }
    1188             :     ELSE IF( EQ_16( coder_type, UNVOICED ) )
    1189             :     {
    1190             :         acelp_cfg->ltf_mode = FULL_BAND;
    1191             :         move16();
    1192             :     }
    1193             :     ELSE IF( ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) ) && LT_32( core_brate, ACELP_11k60 ) )
    1194             :     {
    1195             :         acelp_cfg->ltf_mode = LOW_PASS;
    1196             :         move16();
    1197             :     }
    1198             :     ELSE IF( GE_32( core_brate, ACELP_11k60 ) && ( NE_16( coder_type, AUDIO ) && !( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME ) ) ) )
    1199             :     {
    1200             :         test();
    1201             :         test();
    1202             :         IF( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME16k ) && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
    1203             :         {
    1204             :             acelp_cfg->ltf_mode = FULL_BAND;
    1205             :             move16();
    1206             :         }
    1207             :         ELSE
    1208             :         {
    1209             :             acelp_cfg->ltf_mode = NORMAL_OPERATION;
    1210             :             move16();
    1211             :             IF( coder_type != TRANSITION )
    1212             :             {
    1213             :                 bits = sub( bits, nb_subfr );
    1214             :             }
    1215             :         }
    1216             :     }
    1217             :     ELSE
    1218             :     {
    1219             :         acelp_cfg->ltf_mode = FULL_BAND;
    1220             :         move16();
    1221             :     }
    1222             : 
    1223             :     /*-----------------------------------------------------------------*
    1224             :      * UC bit-budget
    1225             :      *-----------------------------------------------------------------*/
    1226             :     test();
    1227             :     test();
    1228             :     test();
    1229             :     test();
    1230             :     test();
    1231             :     test();
    1232             :     IF( ( ( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_9k60 ) ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
    1233             :     {
    1234             :         bits = sub( bits, NBITS_NOISENESS ); /* noiseness */
    1235             :     }
    1236             :     IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
    1237             :     {
    1238             :         bits = sub( bits, 3 * NB_SUBFR ); /* tilt factor */
    1239             :     }
    1240             : 
    1241             :     /*-----------------------------------------------------------------*
    1242             :      * TC bit-budget
    1243             :      *-----------------------------------------------------------------*/
    1244             : 
    1245             :     fix_first = 0;
    1246             :     move16();
    1247             :     IF( EQ_16( coder_type, TRANSITION ) )
    1248             :     {
    1249             :         if ( EQ_16( tc_call, 2 ) )
    1250             :         {
    1251             :             fix_first = 1;
    1252             :             move16();
    1253             :         }
    1254             : 
    1255             :         /* TC signalling */
    1256             :         IF( EQ_16( L_frame, L_FRAME ) )
    1257             :         {
    1258             :             IF( EQ_16( tc_subfr, TC_0_0 ) )
    1259             :             {
    1260             :                 IF( enc_dec == ENC )
    1261             :                 {
    1262             :                     bits = sub( bits, 1 ); /* TC signalling */
    1263             :                 }
    1264             : 
    1265             :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1266             :                 {
    1267             :                     bits = sub( bits, 3 ); /* LP filtering flag */
    1268             :                 }
    1269             :             }
    1270             :             ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
    1271             :             {
    1272             :                 IF( enc_dec == ENC )
    1273             :                 {
    1274             :                     bits = sub( bits, 4 ); /* TC signalling */
    1275             :                 }
    1276             : 
    1277             :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1278             :                 {
    1279             :                     bits = sub( bits, 3 ); /* LP filtering flag */
    1280             :                 }
    1281             :             }
    1282             :             ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
    1283             :             {
    1284             :                 IF( enc_dec == ENC )
    1285             :                 {
    1286             :                     bits = sub( bits, 4 ); /* TC signalling */
    1287             :                 }
    1288             : 
    1289             :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1290             :                 {
    1291             :                     bits = sub( bits, 2 ); /* LP filtering flag */
    1292             :                 }
    1293             :             }
    1294             :             ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
    1295             :             {
    1296             :                 IF( enc_dec == ENC )
    1297             :                 {
    1298             :                     bits = sub( bits, 3 ); /* TC signalling */
    1299             :                 }
    1300             : 
    1301             :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1302             :                 {
    1303             :                     bits = sub( bits, 1 ); /* LP filtering flag */
    1304             :                 }
    1305             :             }
    1306             :             ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
    1307             :             {
    1308             :                 IF( enc_dec == ENC )
    1309             :                 {
    1310             :                     bits = sub( bits, 3 ); /* TC signalling */
    1311             :                 }
    1312             : 
    1313             :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1314             :                 {
    1315             :                     bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    1316             :                 }
    1317             :             }
    1318             :             ELSE
    1319             :             {
    1320             :                 IF( enc_dec == ENC )
    1321             :                 {
    1322             :                     bits = sub( bits, 4 ); /* TC signalling */
    1323             :                 }
    1324             : 
    1325             :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1326             :                 {
    1327             :                     bits = sub( bits, idiv1616_1( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    1328             :                 }
    1329             :             }
    1330             :         }
    1331             :         ELSE /* L_frame == L_FRAME16k */
    1332             :         {
    1333             :             IF( enc_dec == ENC )
    1334             :             {
    1335             :                 IF( LE_16( tc_subfr, 2 * L_SUBFR ) )
    1336             :                 {
    1337             :                     bits = sub( bits, 2 ); /* TC signalling */
    1338             :                 }
    1339             :                 ELSE
    1340             :                 {
    1341             :                     bits = sub( bits, 3 ); /* TC signalling */
    1342             :                 }
    1343             :             }
    1344             : 
    1345             :             // bits -= ( L_FRAME16k - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
    1346             :             bits = sub( bits, idiv1616_1( sub( L_FRAME16k - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    1347             :         }
    1348             : 
    1349             :         /* glottal-shape codebook bits */
    1350             :         bits = sub( bits, 3 + 6 + 1 + 3 );
    1351             :     }
    1352             : 
    1353             :     /*-----------------------------------------------------------------*
    1354             :      * pitch, innovation, gains bit-budget
    1355             :      *-----------------------------------------------------------------*/
    1356             : 
    1357             :     acelp_cfg->fcb_mode = 0;
    1358             :     move16();
    1359             :     test();
    1360             :     test();
    1361             :     test();
    1362             :     test();
    1363             :     test();
    1364             :     test();
    1365             :     test();
    1366             :     test();
    1367             :     test();
    1368             :     test();
    1369             :     test();
    1370             :     test();
    1371             :     test();
    1372             :     test();
    1373             :     test();
    1374             :     test();
    1375             :     IF( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( tdm_low_rate_mode, 1 ) && ( coder_type != INACTIVE ) && NE_16( coder_type, UNVOICED ) ) /* GENERIC low rate mode for secondary channel */
    1376             :     {
    1377             :         set16_fx( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
    1378             :         set16_fx( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
    1379             : 
    1380             :         FOR( i = 0; i < 2; i++ )
    1381             :         {
    1382             :             acelp_cfg->pitch_bits[i] = 0;
    1383             :             move16();
    1384             :             IF( tdm_Pitch_reuse_flag == 0 )
    1385             :             {
    1386             :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX_fx( ACELP_7k20, GENERIC, i_mult( 2 * L_SUBFR, i ), TC_SUBFR2IDX_fx( tc_subfr ) )];
    1387             :                 move16();
    1388             :                 bits = sub( bits, acelp_cfg->pitch_bits[i] );
    1389             :             }
    1390             :             acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX_fx( ACELP_7k20, GENERIC, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_fx( tc_subfr ) )];
    1391             :             move16();
    1392             :             bits = sub( bits, acelp_cfg->gains_mode[i] );
    1393             :         }
    1394             :         acelp_cfg->fcb_mode = 1;
    1395             :         move16();
    1396             : 
    1397             : #ifdef DEBUGGING
    1398             :         if ( bits >= 55 )
    1399             :         {
    1400             :             printf( "too much bits -> %d, LPC = %d and pitch = %d\n", bits, tdm_lp_reuse_flag, tdm_Pitch_reuse_flag );
    1401             :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1402             :         }
    1403             :         else
    1404             : #endif
    1405             :             IF( GE_16( bits, 16 ) )
    1406             :             {
    1407             :                 acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1408             :             }
    1409             :         ELSE
    1410             :         {
    1411             :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1412             :             acelp_cfg->fixed_cdk_index[1] = -1;
    1413             :             move16();
    1414             :         }
    1415             :         acelp_cfg->fixed_cdk_index[2] = -1;
    1416             :         move16();
    1417             :         acelp_cfg->fixed_cdk_index[3] = -1;
    1418             :         move16();
    1419             :     }
    1420             :     ELSE IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) || /* @12.8kHz core except of GSC */
    1421             :              ( EQ_16( nb_subfr, NB_SUBFR16k ) && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */ ||
    1422             :              EQ_16( core, HQ_CORE ) /* ACELP -> HQ switching in EVS */
    1423             :     )
    1424             :     {
    1425             :         /* pitch Q & gain Q bit-budget - part 2*/
    1426             :         FOR( i = 0; i < nb_subfr; i++ )
    1427             :         {
    1428             :             IF( EQ_16( L_frame, L_FRAME ) )
    1429             :             {
    1430             :                 test();
    1431             :                 IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
    1432             :                 {
    1433             :                     acelp_cfg->pitch_bits[i] = 0;
    1434             :                     move16();
    1435             :                 }
    1436             :                 ELSE
    1437             :                 {
    1438             :                     acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX_fx( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_fx( tc_subfr ) )];
    1439             :                     move16();
    1440             :                 }
    1441             :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX_fx( core_brate, coder_type_sw, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_fx( tc_subfr ) )];
    1442             :                 move16();
    1443             :             }
    1444             :             ELSE /* L_frame == L_FRAME16k */
    1445             :             {
    1446             :                 test();
    1447             :                 IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
    1448             :                 {
    1449             :                     acelp_cfg->pitch_bits[i] = 0;
    1450             :                     move16();
    1451             :                 }
    1452             :                 ELSE
    1453             :                 {
    1454             :                     acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ_fx( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ_fx( tc_subfr ) )];
    1455             :                     move16();
    1456             :                 }
    1457             :                 acelp_cfg->gains_mode[i] = gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ_fx( core_brate, coder_type_sw, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ_fx( tc_subfr ) )];
    1458             :                 move16();
    1459             :             }
    1460             : 
    1461             :             bits = sub( bits, acelp_cfg->pitch_bits[i] );
    1462             : 
    1463             :             test();
    1464             :             IF( coder_type == INACTIVE && EQ_16( acelp_cfg->gains_mode[i], 6 ) /* VQ vs. SQ threshold @32 kbps */ )
    1465             :             {
    1466             :                 bits = sub( bits, 5 );
    1467             :             }
    1468             :             ELSE
    1469             :             {
    1470             :                 if ( EQ_16( *uc_two_stage_flag, 1 ) )
    1471             :                 {
    1472             :                     acelp_cfg->gains_mode[i] = 7;
    1473             :                     move16();
    1474             :                 }
    1475             : 
    1476             :                 bits = sub( bits, acelp_cfg->gains_mode[i] );
    1477             :             }
    1478             :         }
    1479             : 
    1480             :         /* algebraic codebook bit-budget */
    1481             :         test();
    1482             :         test();
    1483             :         test();
    1484             :         test();
    1485             :         test();
    1486             :         test();
    1487             :         IF( flag_hardcoded /* EVS */ ||
    1488             :             ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1489             :             ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1490             :         {
    1491             :             FOR( i = 0; i < nb_subfr; i++ )
    1492             :             {
    1493             :                 IF( EQ_16( L_frame, L_FRAME ) )
    1494             :                 {
    1495             :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX_fx( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_fx( tc_subfr ) )];
    1496             :                     move16();
    1497             :                 }
    1498             :                 ELSE /* L_frame == L_FRAME16k */
    1499             :                 {
    1500             :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ_fx( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ_fx( tc_subfr ) )];
    1501             :                     move16();
    1502             :                 }
    1503             :                 bits = sub( bits, acelp_cfg->fixed_cdk_index[i] );
    1504             :             }
    1505             :         }
    1506             :         ELSE IF( !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    1507             :         {
    1508             :             test();
    1509             :             IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
    1510             :             {
    1511             :                 i = idiv1616( bits, NB_SUBFR );
    1512             :                 IF( s_and( i, 1 ) == 0 )
    1513             :                 {
    1514             :                     i = sub( i, 1 ); /* must be odd */
    1515             :                 }
    1516             :                 i = s_min( i, 13 );
    1517             : #ifdef DEBUG_MODE_TD
    1518             :                 if ( i < 0 )
    1519             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR::: UC negative index should not happen at frame %d\n" );
    1520             : #endif
    1521             :                 i = s_max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
    1522             :                 set16_fx( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
    1523             :                 bits = sub( bits, i_mult( i, NB_SUBFR ) );
    1524             :             }
    1525             :             ELSE
    1526             :             {
    1527             : 
    1528             :                 acelp_cfg->fcb_mode = 1;
    1529             :                 move16();
    1530             :                 test();
    1531             :                 test();
    1532             :                 IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    1533             :                 {
    1534             :                     IF( GE_16( bits, i_mult( ACELP_FIXED_CDK_BITS( 0 ), ( nb_subfr ) ) ) ) /* enough bits for all fcb */
    1535             :                     {
    1536             :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1537             :                     }
    1538             :                     ELSE IF( GE_16( bits, i_mult( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 1 ) ) ) )
    1539             :                     {
    1540             :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1541             :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1542             :                         move16();
    1543             :                     }
    1544             :                     ELSE IF( GE_16( bits, i_mult( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 2 ) ) ) )
    1545             :                     {
    1546             :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 2 ), L_SUBFR, coder_type, tc_subfr, fix_first );
    1547             :                         acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
    1548             :                         move16();
    1549             :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1550             :                         move16();
    1551             :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1552             :                         move16();
    1553             :                     }
    1554             :                     ELSE IF( GE_16( bits, ACELP_FIXED_CDK_BITS( 0 ) ) )
    1555             :                     {
    1556             :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1557             :                         acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
    1558             :                         move16();
    1559             :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1560             :                         move16();
    1561             :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1562             :                         move16();
    1563             :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1564             :                         move16();
    1565             :                     }
    1566             :                     ELSE /* No FCB */
    1567             :                     {
    1568             : #ifdef DEBUGGING
    1569             :                         IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING!!!, No bit allocated to FCB, check frame %d\n" );
    1570             : #endif
    1571             :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1572             :                         move16();
    1573             :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1574             :                         move16();
    1575             :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1576             :                         move16();
    1577             :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1578             :                         move16();
    1579             :                     }
    1580             :                 }
    1581             :                 ELSE IF( NE_16( element_mode, IVAS_CPE_TD ) && GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) )
    1582             :                 {
    1583             :                     bits = 100; /* 9 kbps for fcb */
    1584             :                     move16();
    1585             :                     acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1586             :                 }
    1587             :                 ELSE
    1588             :                 {
    1589             :                     acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1590             :                 }
    1591             :             }
    1592             :         }
    1593             : 
    1594             :         /* AVQ codebook */
    1595             :         test();
    1596             :         test();
    1597             :         test();
    1598             :         IF( ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1599             :             ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1600             :         {
    1601             :             FOR( i = 0; i < nb_subfr; i++ )
    1602             :             {
    1603             :                 IF( flag_hardcoded )
    1604             :                 {
    1605             :                     acelp_cfg->AVQ_cdk_bits[i] = AVQ_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ_fx( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ_fx( tc_subfr ) )];
    1606             :                     move16();
    1607             :                     {
    1608             :                         bits = sub( bits, acelp_cfg->AVQ_cdk_bits[i] );
    1609             :                     }
    1610             :                 }
    1611             : 
    1612             :                 bits = sub( bits, G_AVQ_BITS );
    1613             :             }
    1614             : 
    1615             :             test();
    1616             :             test();
    1617             :             IF( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && LE_32( core_brate_inp, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( coder_type, GENERIC ) )
    1618             :             {
    1619             :                 /* harm. flag ACELP AVQ */
    1620             :                 bits = sub( bits, 1 );
    1621             :             }
    1622             : 
    1623             :             IF( !flag_hardcoded )
    1624             :             {
    1625             :                 Word16 bit_tmp;
    1626             : 
    1627             :                 bit_tmp = idiv1616( bits, nb_subfr );
    1628             :                 set16_fx( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
    1629             :                 bits = sub( bits, i_mult( bit_tmp, nb_subfr ) );
    1630             : 
    1631             :                 bit_tmp = bits % nb_subfr;
    1632             :                 move16();
    1633             :                 acelp_cfg->AVQ_cdk_bits[0] = add( acelp_cfg->AVQ_cdk_bits[0], bit_tmp );
    1634             :                 move16();
    1635             :                 bits = sub( bits, bit_tmp );
    1636             :             }
    1637             :         }
    1638             :     }
    1639             :     ELSE IF( ( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) /* LBR secondary channel in TD stereo */ ||
    1640             :              ( ( coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( nb_subfr, NB_SUBFR ) ) /* GSC @12.8kHz */ ||
    1641             :              ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
    1642             :     {
    1643             :         Word32 Local_BR, Pitch_BR;
    1644             :         Word16 Pitch_CT;
    1645             : 
    1646             :         /* as defined at the beginning of [enc,dec]_pit_exc() */
    1647             :         test();
    1648             :         test();
    1649             :         IF( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || GT_32( core_brate, GSC_H_RATE_STG ) ) )
    1650             :         {
    1651             :             Local_BR = ACELP_8k00;
    1652             :             move32();
    1653             :             Pitch_CT = GENERIC;
    1654             :             move16();
    1655             :             Pitch_BR = ACELP_8k00;
    1656             :             move32();
    1657             :             IF( EQ_16( L_frame, L_FRAME16k ) )
    1658             :             {
    1659             :                 Local_BR = ACELP_14k80;
    1660             :                 move32();
    1661             :                 test();
    1662             :                 if ( GSC_IVAS_mode > 0 && LT_32( core_brate, IVAS_24k4 ) )
    1663             :                 {
    1664             :                     Local_BR = ACELP_9k60;
    1665             :                     move32();
    1666             :                 }
    1667             :                 Pitch_BR = core_brate;
    1668             :                 move32();
    1669             :             }
    1670             :         }
    1671             :         ELSE IF( GSC_noisy_speech )
    1672             :         {
    1673             :             Local_BR = ACELP_7k20;
    1674             :             move32();
    1675             :             Pitch_CT = GENERIC;
    1676             :             move16();
    1677             :             Pitch_BR = ACELP_7k20;
    1678             :             move32();
    1679             :             if ( EQ_16( L_frame, L_FRAME16k ) )
    1680             :             {
    1681             :                 Pitch_BR = core_brate;
    1682             :                 move32();
    1683             :             }
    1684             :         }
    1685             :         ELSE
    1686             :         {
    1687             :             Local_BR = ACELP_7k20;
    1688             :             move32();
    1689             :             Pitch_CT = AUDIO;
    1690             :             move16();
    1691             :             Pitch_BR = core_brate;
    1692             :             move32();
    1693             : 
    1694             :             IF( EQ_16( L_frame, L_FRAME16k ) )
    1695             :             {
    1696             :                 Local_BR = ACELP_13k20;
    1697             :                 move32();
    1698             :                 Pitch_CT = GENERIC;
    1699             :                 move16();
    1700             :             }
    1701             :         }
    1702             : 
    1703             :         FOR( i = 0; i < nb_subfr; i++ )
    1704             :         {
    1705             :             IF( EQ_16( L_frame, L_FRAME16k ) )
    1706             :             {
    1707             :                 acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ_fx( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
    1708             :                 move16();
    1709             :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX_fx( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    1710             :                 move16();
    1711             :             }
    1712             :             ELSE
    1713             :             {
    1714             :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX_fx( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
    1715             :                 move16();
    1716             :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX_fx( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    1717             :                 move16();
    1718             :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX_fx( ACELP_7k20, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    1719             :                 move16();
    1720             :             }
    1721             :         }
    1722             :     }
    1723             : 
    1724             :     test();
    1725             :     test();
    1726             :     test();
    1727             :     IF( EQ_16( coder_type, TRANSITION ) && ( EQ_16( tc_call, 1 ) && tc_subfr == 0 && EQ_16( L_frame, L_FRAME ) ) )
    1728             :     {
    1729             :         return error;
    1730             :     }
    1731             : 
    1732             :     /*-----------------------------------------------------------------*
    1733             :      * unused bits handling
    1734             :      *-----------------------------------------------------------------*/
    1735             : 
    1736             :     acelp_cfg->ubits = 0; /* these bits could be reused for something else */
    1737             :     move16();
    1738             : 
    1739             :     test();
    1740             :     IF( flag_hardcoded && NE_32( core_brate, PPP_NELP_2k80 ) )
    1741             :     {
    1742             :         test();
    1743             :         test();
    1744             :         /* unused bits */
    1745             :         IF( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_24k40 ) ) )
    1746             :         {
    1747             :             acelp_cfg->ubits = 0;
    1748             :             move16();
    1749             :         }
    1750             :         ELSE IF( EQ_16( L_frame, L_FRAME ) )
    1751             :         {
    1752             :             acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX_fx( core_brate, coder_type, -1, TC_SUBFR2IDX_fx( tc_subfr ) )];
    1753             :             move16();
    1754             :         }
    1755             :         ELSE
    1756             :         {
    1757             :             acelp_cfg->ubits = 0;
    1758             :             move16();
    1759             :         }
    1760             : 
    1761             :         bits = sub( bits, acelp_cfg->ubits );
    1762             :     }
    1763             : 
    1764             :     /* sanity check */
    1765             :     test();
    1766             :     test();
    1767             :     test();
    1768             :     IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) || EQ_16( nb_subfr, NB_SUBFR16k ) )
    1769             :     {
    1770             :         test();
    1771             :         test();
    1772             :         test();
    1773             :         test();
    1774             :         test();
    1775             :         test();
    1776             :         test();
    1777             :         test();
    1778             :         test();
    1779             :         test();
    1780             :         test();
    1781             :         test();
    1782             :         IF( ( EQ_16( L_frame, L_FRAME16k ) && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
    1783             :             ( GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) ) ) /* IVAS GSC @16kHz */
    1784             :         {
    1785             :             acelp_cfg->ubits = 0;
    1786             :             move16();
    1787             :         }
    1788             :         ELSE IF( flag_hardcoded && core == ACELP_CORE && bits != 0 )
    1789             :         {
    1790             : #ifdef DEBUGGING
    1791             :             IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (Word32) bits );
    1792             : #endif
    1793             :         }
    1794             :         ELSE IF( bits > 0 && !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    1795             :         {
    1796             :             test();
    1797             :             test();
    1798             :             test();
    1799             :             test();
    1800             :             IF( idchan > 0 && EQ_16( element_mode, IVAS_CPE_TD ) )
    1801             :             {
    1802             :                 IF( !tdm_lp_reuse_flag )
    1803             :                 {
    1804             :                     acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
    1805             :                     move16();
    1806             :                     bits = 0;
    1807             :                     move16();
    1808             :                 }
    1809             :                 ELSE
    1810             :                 {
    1811             :                     Word16 nb_prm = 4;
    1812             :                     move16();
    1813             :                     if ( EQ_16( tdm_low_rate_mode, 1 ) )
    1814             :                     {
    1815             :                         nb_prm = 2;
    1816             :                         move16();
    1817             :                     }
    1818             :                     /* First add remaining bits on gains */
    1819             :                     bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode ) );
    1820             : 
    1821             :                     /* Then, Increase pitch bit budget */
    1822             :                     test();
    1823             :                     IF( tdm_Pitch_reuse_flag == 0 && bits > 0 )
    1824             :                     {
    1825             :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits ) );
    1826             :                     }
    1827             : 
    1828             :                     /* Increase mid-lsf bit budget */
    1829             :                     test();
    1830             :                     IF( tdm_lp_reuse_flag == 0 && bits > 0 )
    1831             :                     {
    1832             :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits ) );
    1833             :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits ) );
    1834             :                     }
    1835             :                 }
    1836             : 
    1837             : #ifdef DEBUGGING
    1838             :                 if ( idchan > 0 && bits > 0 && ( coder_type > UNVOICED || tdm_low_rate_mode == 0 ) )
    1839             :                 {
    1840             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING !! Unused bits in secondary channel at frame %d\n" );
    1841             :                 }
    1842             : #endif
    1843             :             }
    1844             : 
    1845             :             ELSE IF( core == ACELP_CORE && GE_16( coder_type, UNVOICED ) && LE_16( coder_type, GENERIC ) && EQ_16( L_frame, L_FRAME ) )
    1846             :             {
    1847             :                 acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
    1848             :                 move16();
    1849             : 
    1850             :                 test();
    1851             :                 IF( GT_16( acelp_cfg->lsf_bits, 46 ) )
    1852             :                 {
    1853             :                     acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 46 );
    1854             :                     move16();
    1855             :                     acelp_cfg->lsf_bits = 46;
    1856             :                     move16();
    1857             :                 }
    1858             :                 ELSE IF( GT_16( acelp_cfg->lsf_bits, 42 ) && EQ_16( L_frame, L_FRAME ) )
    1859             :                 {
    1860             :                     acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 42 );
    1861             :                     move16();
    1862             :                     acelp_cfg->lsf_bits = 42;
    1863             :                     move16();
    1864             :                 }
    1865             :             }
    1866             :             ELSE
    1867             :             {
    1868             :                 acelp_cfg->ubits = bits;
    1869             :                 move16();
    1870             :             }
    1871             :         }
    1872             :         ELSE IF( bits < 0 && !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    1873             :         {
    1874             : #ifdef DEBUGGING
    1875             :             IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (Word32) bits );
    1876             : #endif
    1877             :         }
    1878             :     }
    1879             : 
    1880             :     return error;
    1881             : }
    1882             : 
    1883             : /*-------------------------------------------------------------------*
    1884             :  * config_acelp1_IVAS()
    1885             :  *
    1886             :  * Configure ACELP bit allocation
    1887             :  *   - should be in range of <6700; 24350> for ACELP@12.8kHz
    1888             :  *   - per channel bitrate minimum is 13250 kbps for ACELP@16kHz
    1889             :  *--------------------------------------------------------------------*/
    1890             : #endif
    1891      336257 : ivas_error config_acelp1_IVAS(
    1892             :     const Word16 enc_dec,                  /* i  : encoder/decoder flag            */
    1893             :     const Word32 total_brate,              /* i  : total bitrate                   */
    1894             :     const Word32 core_brate_inp,           /* i  : core bitrate                    */
    1895             :     const Word16 core,                     /* i  : core                            */
    1896             :     const Word16 extl,                     /* i  : extension layer                 */
    1897             :     const Word32 extl_brate,               /* i  : extension layer bitrate         */
    1898             :     const Word16 L_frame,                  /* i  : frame length at internal Fs     */
    1899             :     const Word16 GSC_noisy_speech,         /* i  : GSC on SWB noisy speech flag    */
    1900             :     ACELP_config *acelp_cfg,               /* i  : ACELP bit-allocation            */
    1901             :     const Word16 signaling_bits,           /* i  : number of signaling bits        */
    1902             :     const Word16 coder_type,               /* i  : coder type                      */
    1903             :     const Word16 inactive_coder_type_flag, /* i  : AVQ (0) or GSC (1) IC flag      */
    1904             :     const Word16 tc_subfr,                 /* i  : TC subfr ID                     */
    1905             :     const Word16 tc_call,                  /* i  : TC call number (0,1,2,3,5(DEC)) */
    1906             :     Word16 *nBits_es_Pred,                 /* o  : number of bits for Es_pred Q    */
    1907             :     Word16 *unbits,                        /* o  : number of unused bits           */
    1908             :     const Word16 element_mode,             /* i  : element mode                    */
    1909             :     Word16 *uc_two_stage_flag,             /* o  : flag undicating two-stage UC    */
    1910             :     const Word16 tdm_lp_reuse_flag,        /* i  : LPC reuse flag (can be 1 only with secondary channel */
    1911             :     const Word16 tdm_low_rate_mode,        /* i  : secondary channel low rate mode flag */
    1912             :     const Word16 idchan,                   /* i  : stereo channel ID               */
    1913             :     const Word16 active_cnt,               /* i  : Active frame counter            */
    1914             :     const Word16 tdm_Pitch_reuse_flag,     /* i  : primary channel pitch reuse flag*/
    1915             :     const Word16 tdm_LRTD_flag,            /* i  : LRTD stereo mode flag           */
    1916             :     const Word16 GSC_IVAS_mode             /* i  : GSC IVAS mode                   */
    1917             : )
    1918             : {
    1919             :     Word16 i, bits, nb_subfr;
    1920             :     Word16 flag_hardcoded, coder_type_sw, fix_first;
    1921             :     Word32 core_brate;
    1922             :     ivas_error error;
    1923             : 
    1924      336257 :     error = IVAS_ERR_OK;
    1925      336257 :     move32();
    1926             : 
    1927             :     /*-----------------------------------------------------------------*
    1928             :      * Set the flag indicating two-stage Unvoiced (UC) frame
    1929             :      *-----------------------------------------------------------------*/
    1930             : 
    1931      336257 :     *uc_two_stage_flag = 0;
    1932      336257 :     move16();
    1933             : 
    1934      336257 :     IF( EQ_16( coder_type, UNVOICED ) )
    1935             :     {
    1936        3420 :         test();
    1937        3420 :         test();
    1938        3420 :         test();
    1939        3420 :         test();
    1940        3420 :         test();
    1941        3420 :         IF( GE_32( total_brate, MIN_UNVOICED_TWO_STAGE_BRATE ) && element_mode > EVS_MONO && ( idchan == 0 || ( ( GE_32( total_brate, 8500 ) || extl_brate == 0 ) && EQ_16( tdm_LRTD_flag, 1 ) ) ) )
    1942             :         {
    1943        3376 :             *uc_two_stage_flag = 1;
    1944        3376 :             move16();
    1945             :         }
    1946             :     }
    1947             : 
    1948             :     /*-----------------------------------------------------------------*
    1949             :      * Set the number of subframes
    1950             :      *-----------------------------------------------------------------*/
    1951             : 
    1952      336257 :     IF( EQ_16( L_frame, L_FRAME ) )
    1953             :     {
    1954      186269 :         nb_subfr = NB_SUBFR;
    1955      186269 :         move16();
    1956             :     }
    1957             :     ELSE /* L_frame == L_FRAME16k */
    1958             :     {
    1959      149988 :         nb_subfr = NB_SUBFR16k;
    1960      149988 :         move16();
    1961             :     }
    1962             : 
    1963      336257 :     coder_type_sw = coder_type;
    1964      336257 :     move16();
    1965      336257 :     IF( core != ACELP_CORE )
    1966             :     {
    1967             :         /* used in acelp_core_switch_enc() */
    1968          46 :         nb_subfr = 1;
    1969          46 :         move16();
    1970          46 :         if ( EQ_16( L_frame, L_FRAME ) )
    1971             :         {
    1972           8 :             coder_type_sw = TRANSITION;
    1973           8 :             move16();
    1974             :         }
    1975             :     }
    1976             : 
    1977             :     /*-----------------------------------------------------------------*
    1978             :      * Check if the core_brate is hard coded (to keep BE for mono core) or not
    1979             :      *-----------------------------------------------------------------*/
    1980             : 
    1981      336257 :     flag_hardcoded = 0;
    1982      336257 :     move16();
    1983      336257 :     i = 0;
    1984      336257 :     move16();
    1985             : 
    1986     3159888 :     WHILE( i < SIZE_BRATE_INTERMED_TBL )
    1987             :     {
    1988     3159888 :         IF( EQ_32( core_brate_inp, brate_intermed_tbl[i] ) )
    1989             :         {
    1990       13852 :             flag_hardcoded = 1;
    1991       13852 :             move16();
    1992       13852 :             BREAK;
    1993             :         }
    1994             : 
    1995     3146036 :         IF( LT_32( core_brate_inp, brate_intermed_tbl[i] ) )
    1996             :         {
    1997      322405 :             flag_hardcoded = 0;
    1998      322405 :             move16();
    1999      322405 :             BREAK;
    2000             :         }
    2001             : 
    2002     2823631 :         i = add( i, 1 );
    2003             :     }
    2004             : 
    2005      336257 :     test();
    2006      336257 :     test();
    2007      336257 :     test();
    2008      336296 :     if ( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( coder_type, AUDIO ) &&
    2009          78 :          LE_32( core_brate_inp, 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 */
    2010             :     {
    2011           8 :         i = sub( i, 1 );
    2012             :     }
    2013             : 
    2014      336257 :     core_brate = brate_intermed_tbl[i];
    2015      336257 :     move32();
    2016             : 
    2017      336257 :     if ( element_mode > EVS_MONO )
    2018             :     {
    2019      333099 :         flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
    2020      333099 :         move16();
    2021             :     }
    2022             : 
    2023      336257 :     test();
    2024      336257 :     if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
    2025             :     {
    2026          46 :         flag_hardcoded = 1;
    2027          46 :         move16();
    2028             :     }
    2029             : 
    2030             :     /*-----------------------------------------------------------------*
    2031             :      * ACELP bit allocation
    2032             :      *-----------------------------------------------------------------*/
    2033             : 
    2034      336257 :     test();
    2035      336257 :     test();
    2036      336257 :     IF( !( EQ_16( coder_type, TRANSITION ) && NE_16( tc_subfr, -1 ) ) || EQ_16( enc_dec, DEC ) )
    2037             :     {
    2038             :         /* Set the bit-budget */
    2039      314831 :         bits = divide3216( core_brate_inp, FRAMES_PER_SEC / 2 );
    2040             : 
    2041      314831 :         test();
    2042      314831 :         test();
    2043      314831 :         IF( EQ_16( coder_type, TRANSITION ) && EQ_16( enc_dec, DEC ) && EQ_16( tc_call, 1 ) )
    2044             :         {
    2045       12668 :             bits = add( bits, *nBits_es_Pred ); /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
    2046             :         }
    2047             : 
    2048      314831 :         test();
    2049      314831 :         test();
    2050             :         /* Subtract signaling bits */
    2051      314831 :         IF( EQ_16( enc_dec, DEC ) && EQ_16( idchan, 1 ) && element_mode > EVS_MONO )
    2052             :         {
    2053        3695 :             bits = sub( bits, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS );
    2054             : 
    2055        3695 :             IF( EQ_16( tdm_LRTD_flag, 1 ) )
    2056             :             {
    2057        3569 :                 bits = add( bits, STEREO_BITS_TCA );
    2058             :             }
    2059             : 
    2060        3695 :             test();
    2061        3695 :             test();
    2062        3695 :             test();
    2063        3695 :             test();
    2064        3695 :             test();
    2065        3695 :             test();
    2066             :             /* subtract TBE/BWE flag */
    2067        3695 :             IF( extl_brate > 0 && ( EQ_16( extl, WB_TBE ) || EQ_16( extl, SWB_TBE ) || EQ_16( extl, FB_TBE ) || EQ_16( extl, WB_BWE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) ) )
    2068             :             {
    2069        2793 :                 bits = sub( bits, 1 );
    2070             :             }
    2071             :         }
    2072             :         ELSE
    2073             :         {
    2074             :             /* Subtract signaling bits */
    2075      311136 :             bits = sub( bits, signaling_bits );
    2076             :         }
    2077             : 
    2078      314831 :         test();
    2079      314831 :         test();
    2080      314831 :         test();
    2081      314831 :         test();
    2082      314831 :         test();
    2083      314831 :         test();
    2084      314831 :         IF( extl_brate > 0 && ( EQ_16( extl, WB_TBE ) || EQ_16( extl, SWB_TBE ) || EQ_16( extl, FB_TBE ) || EQ_16( extl, WB_BWE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) ) )
    2085             :         {
    2086             :             /* extension layer signaling bit is counted in the extension layer bitbudget */
    2087      279707 :             bits = add( bits, 1 );
    2088             :         }
    2089             : 
    2090             :         /*-----------------------------------------------------------------*
    2091             :          * LSF Q bit-budget
    2092             :          *-----------------------------------------------------------------*/
    2093             : 
    2094      314831 :         test();
    2095      314831 :         test();
    2096      314831 :         test();
    2097      314831 :         IF( !tdm_lp_reuse_flag || idchan == 0 )
    2098             :         {
    2099             :             /* LSF Q bit-budget */
    2100      314560 :             acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
    2101      314560 :             move16();
    2102             : 
    2103      314560 :             IF( !flag_hardcoded )
    2104             :             {
    2105      311609 :                 IF( EQ_16( L_frame, L_FRAME ) )
    2106             :                 {
    2107      169757 :                     test();
    2108      169757 :                     test();
    2109      169757 :                     test();
    2110      169757 :                     test();
    2111      169757 :                     test();
    2112      169757 :                     test();
    2113      169757 :                     test();
    2114      169757 :                     IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
    2115             :                     {
    2116       14254 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
    2117       14254 :                         move16();
    2118             :                     }
    2119      155503 :                     ELSE IF( ( LT_32( total_brate, 7200 ) || coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( idchan, 1 ) )
    2120             :                     {
    2121             :                         /* TD stereo, secondary channel: do nothing */
    2122         101 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
    2123         101 :                         move16();
    2124             :                     }
    2125      155402 :                     ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) && LT_32( brate_intermed_tbl[i], ACELP_9k60 ) )
    2126             :                     {
    2127             :                         /* primary channel: do nothing */
    2128             :                     }
    2129      139297 :                     ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
    2130             :                     {
    2131       14426 :                         acelp_cfg->lsf_bits = 42;
    2132       14426 :                         move16();
    2133             :                     }
    2134      124871 :                     ELSE IF( LE_32( total_brate, 9600 ) || EQ_16( coder_type, UNVOICED ) )
    2135             :                     {
    2136       18313 :                         acelp_cfg->lsf_bits = 31;
    2137       18313 :                         move16();
    2138             :                     }
    2139      106558 :                     ELSE IF( LE_32( total_brate, 20000 ) )
    2140             :                     {
    2141      104026 :                         acelp_cfg->lsf_bits = 36;
    2142      104026 :                         move16();
    2143             :                     }
    2144             :                     ELSE
    2145             :                     {
    2146        2532 :                         acelp_cfg->lsf_bits = 41;
    2147        2532 :                         move16();
    2148             :                     }
    2149             :                 }
    2150             :                 ELSE /* L_frame == L_FRAME16k */
    2151             :                 {
    2152      141852 :                     acelp_cfg->lsf_bits = 41;
    2153      141852 :                     move16();
    2154             :                 }
    2155             :             }
    2156             : 
    2157      314560 :             bits = sub( bits, acelp_cfg->lsf_bits );
    2158             : 
    2159             :             /* mid-LSF Q bit-budget */
    2160      314560 :             acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
    2161      314560 :             move16();
    2162             : 
    2163      314560 :             test();
    2164      314560 :             if ( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
    2165             :             {
    2166       30531 :                 acelp_cfg->mid_lsf_bits = 5;
    2167       30531 :                 move16();
    2168             :                 /* primary channel: do nothing */
    2169             :             }
    2170             : 
    2171      314560 :             bits = sub( bits, acelp_cfg->mid_lsf_bits );
    2172             :         }
    2173         271 :         ELSE IF( EQ_16( tdm_lp_reuse_flag, 1 ) && EQ_16( idchan, 1 ) && NE_16( active_cnt, 1 ) )
    2174             :         {
    2175         271 :             bits = sub( bits, TDM_IC_LSF_PRED_BITS );
    2176             :         }
    2177             : 
    2178      314831 :         test();
    2179      314831 :         test();
    2180      314831 :         test();
    2181      314831 :         test();
    2182      314831 :         test();
    2183      314831 :         test();
    2184             :         /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
    2185      314831 :         IF( ( NE_16( coder_type, UNVOICED ) && NE_16( coder_type, AUDIO ) && coder_type != INACTIVE && !( LE_32( core_brate, ACELP_8k00 ) && NE_16( coder_type, TRANSITION ) ) ) /* mid bitrates in GC and VC, low+mid bitrates in TC */ ||
    2186             :             ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */ )
    2187             :         {
    2188      247817 :             *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
    2189      247817 :             move16();
    2190      247817 :             bits = sub( bits, *nBits_es_Pred );
    2191             :         }
    2192       67014 :         ELSE IF( *uc_two_stage_flag )
    2193             :         {
    2194        3376 :             *nBits_es_Pred = 4;
    2195        3376 :             move16();
    2196        3376 :             bits = sub( bits, *nBits_es_Pred );
    2197             :         }
    2198             :     }
    2199             :     ELSE
    2200             :     {
    2201       21426 :         bits = *unbits;
    2202       21426 :         move16();
    2203             :     }
    2204             : 
    2205      336257 :     test();
    2206      336257 :     IF( EQ_16( coder_type, TRANSITION ) && EQ_16( tc_call, 0 ) )
    2207             :     {
    2208       14181 :         *unbits = bits;
    2209       14181 :         move16();
    2210       14181 :         return error;
    2211             :     }
    2212             : 
    2213             :     /*-----------------------------------------------------------------*
    2214             :      * Low-rate mode - bits are allocated in tdm_low_rate_enc()
    2215             :      *-----------------------------------------------------------------*/
    2216             : 
    2217      322076 :     test();
    2218      322076 :     IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
    2219             :     {
    2220       14254 :         acelp_cfg->FEC_mode = 0;
    2221       14254 :         move16();
    2222       14254 :         acelp_cfg->ltf_mode = FULL_BAND;
    2223       14254 :         move16();
    2224       14254 :         *nBits_es_Pred = 0;
    2225       14254 :         move16();
    2226       14254 :         *unbits = 0;
    2227       14254 :         move16();
    2228       14254 :         acelp_cfg->ubits = 0;
    2229       14254 :         move16();
    2230             : 
    2231       14254 :         return error;
    2232             :     }
    2233             : 
    2234             :     /*-----------------------------------------------------------------*
    2235             :      * Supplementary information for FEC
    2236             :      *-----------------------------------------------------------------*/
    2237             : 
    2238      307822 :     acelp_cfg->FEC_mode = 0;
    2239      307822 :     move16();
    2240      307822 :     test();
    2241      307822 :     test();
    2242      307822 :     IF( GE_32( core_brate, ACELP_11k60 ) && ( idchan == 0 || element_mode == EVS_MONO ) )
    2243             :     {
    2244      246232 :         acelp_cfg->FEC_mode = 1;
    2245      246232 :         move16();
    2246             : 
    2247      246232 :         test();
    2248      246232 :         test();
    2249      246232 :         if ( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) && NE_16( coder_type, VOICED ) )
    2250             :         {
    2251      211149 :             bits = sub( bits, FEC_BITS_CLS );
    2252             :         }
    2253             : 
    2254      246232 :         IF( NE_16( coder_type, TRANSITION ) )
    2255             :         {
    2256      213763 :             IF( GE_32( total_brate, ACELP_16k40 ) )
    2257             :             {
    2258      134063 :                 acelp_cfg->FEC_mode = 2;
    2259      134063 :                 move16();
    2260             : 
    2261      134063 :                 test();
    2262      134063 :                 IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
    2263             :                 {
    2264      120814 :                     bits = sub( bits, FEC_BITS_ENR );
    2265             :                 }
    2266             :             }
    2267             : 
    2268      213763 :             IF( GE_32( total_brate, ACELP_32k ) )
    2269             :             {
    2270       23000 :                 acelp_cfg->FEC_mode = 3;
    2271       23000 :                 move16();
    2272             : 
    2273       23000 :                 test();
    2274       23000 :                 IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
    2275             :                 {
    2276       19760 :                     bits = sub( bits, FEC_BITS_POS );
    2277             :                 }
    2278             :             }
    2279             :         }
    2280             :     }
    2281             : 
    2282             :     /*-----------------------------------------------------------------*
    2283             :      * LP filtering of the adaptive excitation
    2284             :      *-----------------------------------------------------------------*/
    2285             : 
    2286      307822 :     test();
    2287      307822 :     test();
    2288      307822 :     test();
    2289      307822 :     test();
    2290      307822 :     test();
    2291      307822 :     test();
    2292      307822 :     IF( idchan > 0 && element_mode > EVS_MONO )
    2293             :     {
    2294        7522 :         acelp_cfg->ltf_mode = FULL_BAND;
    2295        7522 :         move16();
    2296             :     }
    2297      300300 :     ELSE IF( EQ_16( coder_type, UNVOICED ) )
    2298             :     {
    2299        3230 :         acelp_cfg->ltf_mode = FULL_BAND;
    2300        3230 :         move16();
    2301             :     }
    2302      297070 :     ELSE IF( ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) ) && LT_32( core_brate, ACELP_11k60 ) )
    2303             :     {
    2304       22521 :         acelp_cfg->ltf_mode = LOW_PASS;
    2305       22521 :         move16();
    2306             :     }
    2307      274549 :     ELSE IF( GE_32( core_brate, ACELP_11k60 ) && ( NE_16( coder_type, AUDIO ) && !( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME ) ) ) )
    2308             :     {
    2309      232409 :         test();
    2310      232409 :         test();
    2311      232409 :         IF( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME16k ) && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
    2312             :         {
    2313        7700 :             acelp_cfg->ltf_mode = FULL_BAND;
    2314        7700 :             move16();
    2315             :         }
    2316             :         ELSE
    2317             :         {
    2318      224709 :             acelp_cfg->ltf_mode = NORMAL_OPERATION;
    2319      224709 :             move16();
    2320      224709 :             if ( coder_type != TRANSITION )
    2321             :             {
    2322      192240 :                 bits = sub( bits, nb_subfr );
    2323             :             }
    2324             :         }
    2325             :     }
    2326             :     ELSE
    2327             :     {
    2328       42140 :         acelp_cfg->ltf_mode = FULL_BAND;
    2329       42140 :         move16();
    2330             :     }
    2331             : 
    2332             :     /*-----------------------------------------------------------------*
    2333             :      * UC bit-budget
    2334             :      *-----------------------------------------------------------------*/
    2335             : 
    2336      307822 :     test();
    2337      307822 :     test();
    2338      307822 :     test();
    2339      307822 :     test();
    2340      307822 :     test();
    2341      307822 :     IF( ( ( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_9k60 ) ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
    2342             :     {
    2343        1013 :         bits = sub( bits, NBITS_NOISENESS ); /* noiseness */
    2344             :     }
    2345      307822 :     test();
    2346      307822 :     IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
    2347             :     {
    2348          44 :         bits = sub( bits, ( 3 * NB_SUBFR ) ); /* tilt factor */
    2349             :     }
    2350             : 
    2351             :     /*-----------------------------------------------------------------*
    2352             :      * TC bit-budget
    2353             :      *-----------------------------------------------------------------*/
    2354             : 
    2355      307822 :     fix_first = 0;
    2356      307822 :     move16();
    2357      307822 :     IF( EQ_16( coder_type, TRANSITION ) )
    2358             :     {
    2359       37613 :         IF( EQ_16( tc_call, 2 ) )
    2360             :         {
    2361        9119 :             fix_first = 1;
    2362        9119 :             move16();
    2363             :         }
    2364             : 
    2365             :         /* TC signaling */
    2366       37613 :         IF( EQ_16( L_frame, L_FRAME ) )
    2367             :         {
    2368       24669 :             IF( EQ_16( tc_subfr, TC_0_0 ) )
    2369             :             {
    2370        4822 :                 IF( enc_dec == ENC )
    2371             :                 {
    2372        3290 :                     bits = sub( bits, 1 ); /* TC signaling */
    2373             :                 }
    2374             : 
    2375        4822 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    2376             :                 {
    2377        3728 :                     bits = sub( bits, 3 ); /* LP filtering flag */
    2378             :                 }
    2379             :             }
    2380       19847 :             ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
    2381             :             {
    2382        2513 :                 IF( enc_dec == ENC )
    2383             :                 {
    2384        1333 :                     bits = sub( bits, 4 ); /* TC signaling */
    2385             :                 }
    2386             : 
    2387        2513 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    2388             :                 {
    2389        1948 :                     bits = sub( bits, 3 ); /* LP filtering flag */
    2390             :                 }
    2391             :             }
    2392       17334 :             ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
    2393             :             {
    2394        1311 :                 IF( enc_dec == ENC )
    2395             :                 {
    2396         692 :                     bits = sub( bits, 4 ); /* TC signaling */
    2397             :                 }
    2398             : 
    2399        1311 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    2400             :                 {
    2401        1119 :                     bits = sub( bits, 2 ); /* LP filtering flag */
    2402             :                 }
    2403             :             }
    2404       16023 :             ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
    2405             :             {
    2406         395 :                 IF( enc_dec == ENC )
    2407             :                 {
    2408         207 :                     bits = sub( bits, 3 ); /* TC signaling */
    2409             :                 }
    2410             : 
    2411         395 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    2412             :                 {
    2413         339 :                     bits = sub( bits, 1 ); /* LP filtering flag */
    2414             :                 }
    2415             :             }
    2416       15628 :             ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
    2417             :             {
    2418        3355 :                 IF( enc_dec == ENC )
    2419             :                 {
    2420        2344 :                     bits = sub( bits, 3 ); /* TC signaling */
    2421             :                 }
    2422             : 
    2423        3355 :                 test();
    2424        3355 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) && NE_16( tc_subfr, 192 ) )
    2425             :                 {
    2426        2608 :                     bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    2427             :                 }
    2428             :             }
    2429             :             ELSE
    2430             :             {
    2431       12273 :                 IF( enc_dec == ENC )
    2432             :                 {
    2433        6805 :                     bits = sub( bits, 4 ); /* TC signaling */
    2434             :                 }
    2435             : 
    2436       12273 :                 test();
    2437       12273 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) && NE_16( tc_subfr, 192 ) )
    2438             :                 {
    2439        7092 :                     bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    2440             :                 }
    2441             :             }
    2442             :         }
    2443             :         ELSE /* L_frame == L_FRAME16k */
    2444             :         {
    2445       12944 :             IF( enc_dec == ENC )
    2446             :             {
    2447        6755 :                 IF( LE_16( tc_subfr, 2 * L_SUBFR ) )
    2448             :                 {
    2449        4458 :                     bits = sub( bits, 2 ); /* TC signaling */
    2450             :                 }
    2451             :                 ELSE
    2452             :                 {
    2453        2297 :                     bits = sub( bits, 3 ); /* TC signaling */
    2454             :                 }
    2455             :             }
    2456             : 
    2457       12944 :             IF( NE_16( tc_subfr, 256 ) )
    2458             :             {
    2459        9052 :                 bits = sub( bits, idiv1616( sub( L_FRAME16k - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    2460             :             }
    2461             :         }
    2462             : 
    2463             :         /* glottal-shape codebook bits */
    2464       37613 :         bits = sub( bits, ( 3 + 6 + 1 + 3 ) );
    2465             :     }
    2466             : 
    2467             :     /*-----------------------------------------------------------------*
    2468             :      * pitch, innovation, gains bit-budget
    2469             :      *-----------------------------------------------------------------*/
    2470             : 
    2471      307822 :     acelp_cfg->fcb_mode = 0;
    2472      307822 :     move16();
    2473             : 
    2474      307822 :     test();
    2475      307822 :     test();
    2476      307822 :     test();
    2477      307822 :     test();
    2478      307822 :     test();
    2479      307822 :     test();
    2480      307822 :     test();
    2481      307822 :     test();
    2482      307822 :     test();
    2483      307822 :     test();
    2484      307822 :     test();
    2485      307822 :     test();
    2486      307822 :     test();
    2487      307822 :     test();
    2488      307822 :     test();
    2489      307822 :     test();
    2490      307822 :     IF( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( tdm_low_rate_mode, 1 ) && coder_type != INACTIVE && NE_16( coder_type, UNVOICED ) ) /* GENERIC low rate mode for secondary channel */
    2491             :     {
    2492           5 :         set16_fx( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
    2493           5 :         set16_fx( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
    2494             : 
    2495          15 :         FOR( i = 0; i < 2; i++ )
    2496             :         {
    2497          10 :             acelp_cfg->pitch_bits[i] = 0;
    2498          10 :             move16();
    2499          10 :             IF( tdm_Pitch_reuse_flag == 0 )
    2500             :             {
    2501          10 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    2502          10 :                 move16();
    2503          10 :                 bits = sub( bits, acelp_cfg->pitch_bits[i] );
    2504             :             }
    2505          10 :             acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    2506          10 :             move16();
    2507          10 :             bits = sub( bits, acelp_cfg->gains_mode[i] );
    2508             :         }
    2509           5 :         acelp_cfg->fcb_mode = 1;
    2510           5 :         move16();
    2511             : 
    2512           5 :         IF( GE_16( bits, 16 ) )
    2513             :         {
    2514           5 :             acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    2515             :         }
    2516             :         ELSE
    2517             :         {
    2518           0 :             acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    2519           0 :             acelp_cfg->fixed_cdk_index[1] = -1;
    2520           0 :             move16();
    2521             :         }
    2522           5 :         acelp_cfg->fixed_cdk_index[2] = -1;
    2523           5 :         move16();
    2524           5 :         acelp_cfg->fixed_cdk_index[3] = -1;
    2525           5 :         move16();
    2526             :     }
    2527      307817 :     ELSE IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) /* @12.8kHz core except of GSC */ ||
    2528             :              ( EQ_16( nb_subfr, NB_SUBFR16k ) && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */ ||
    2529             :              EQ_16( core, HQ_CORE ) /* ACELP -> HQ switching in EVS */ )
    2530             :     {
    2531             :         /* pitch Q & gain Q bit-budget - part 2*/
    2532     1460642 :         FOR( i = 0; i < nb_subfr; i++ )
    2533             :         {
    2534     1195585 :             IF( EQ_16( L_frame, L_FRAME ) )
    2535             :             {
    2536      518072 :                 test();
    2537      518072 :                 IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
    2538             :                 {
    2539         272 :                     acelp_cfg->pitch_bits[i] = 0;
    2540         272 :                     move16();
    2541             :                 }
    2542             :                 ELSE
    2543             :                 {
    2544      517800 :                     acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    2545      517800 :                     move16();
    2546             :                 }
    2547      518072 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    2548      518072 :                 move16();
    2549             :             }
    2550             :             ELSE /* L_frame == L_FRAME16k */
    2551             :             {
    2552      677513 :                 test();
    2553      677513 :                 IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
    2554             :                 {
    2555           0 :                     acelp_cfg->pitch_bits[i] = 0;
    2556           0 :                     move16();
    2557             :                 }
    2558             :                 ELSE
    2559             :                 {
    2560      677513 :                     acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    2561      677513 :                     move16();
    2562             :                 }
    2563      677513 :                 acelp_cfg->gains_mode[i] = gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type_sw, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    2564      677513 :                 move16();
    2565             :             }
    2566             : 
    2567     1195585 :             bits = sub( bits, acelp_cfg->pitch_bits[i] );
    2568             : 
    2569     1195585 :             test();
    2570     1195585 :             IF( coder_type == INACTIVE && EQ_16( acelp_cfg->gains_mode[i], 6 ) /* VQ vs. SQ threshold @32 kbps */ )
    2571             :             {
    2572        8460 :                 bits = sub( bits, 5 );
    2573             :             }
    2574             :             ELSE
    2575             :             {
    2576     1187125 :                 IF( EQ_16( *uc_two_stage_flag, 1 ) )
    2577             :                 {
    2578       13504 :                     acelp_cfg->gains_mode[i] = 7;
    2579       13504 :                     move16();
    2580             :                 }
    2581             : 
    2582     1187125 :                 bits = sub( bits, acelp_cfg->gains_mode[i] );
    2583             :             }
    2584             :         }
    2585             : 
    2586      265057 :         test();
    2587      265057 :         test();
    2588      265057 :         test();
    2589      265057 :         test();
    2590      265057 :         test();
    2591      265057 :         test();
    2592             :         /* algebraic codebook bit-budget */
    2593      265057 :         IF( flag_hardcoded /* EVS */ ||
    2594             :             ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    2595             :             ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    2596             :         {
    2597      169803 :             FOR( i = 0; i < nb_subfr; i++ )
    2598             :             {
    2599      141201 :                 IF( EQ_16( L_frame, L_FRAME ) )
    2600             :                 {
    2601        6508 :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX( tc_subfr ) )];
    2602        6508 :                     move16();
    2603             :                 }
    2604             :                 ELSE /* L_frame == L_FRAME16k */
    2605             :                 {
    2606      134693 :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    2607      134693 :                     move16();
    2608             :                 }
    2609      141201 :                 bits = sub( bits, acelp_cfg->fixed_cdk_index[i] );
    2610             :             }
    2611             :         }
    2612      236455 :         ELSE IF( !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    2613             :         {
    2614      236409 :             test();
    2615      236409 :             IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
    2616             :             {
    2617           0 :                 i = idiv1616( bits, NB_SUBFR );
    2618           0 :                 IF( s_and( i, 1 ) == 0 )
    2619             :                 {
    2620           0 :                     i = sub( i, 1 ); /* must be odd */
    2621             :                 }
    2622           0 :                 i = s_min( i, 13 );
    2623           0 :                 i = s_max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
    2624           0 :                 set16_fx( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
    2625           0 :                 bits = sub( bits, imult1616( i, NB_SUBFR ) );
    2626             :             }
    2627             :             ELSE
    2628             :             {
    2629      236409 :                 acelp_cfg->fcb_mode = 1;
    2630      236409 :                 move16();
    2631      236409 :                 test();
    2632      236409 :                 test();
    2633      236409 :                 IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    2634             :                 {
    2635       14929 :                     IF( GE_16( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), nb_subfr ) ) ) /* enough bits for all fcb */
    2636             :                     {
    2637       14892 :                         acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    2638             :                     }
    2639          37 :                     ELSE IF( GE_16( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 1 ) ) ) )
    2640             :                     {
    2641           0 :                         acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 1 ), L_SUBFR, coder_type, tc_subfr, fix_first );
    2642           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    2643           0 :                         move16();
    2644             :                     }
    2645          37 :                     ELSE IF( GE_32( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 2 ) ) ) )
    2646             :                     {
    2647          37 :                         acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 2 ), L_SUBFR, coder_type, tc_subfr, fix_first );
    2648          37 :                         acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
    2649          37 :                         move16();
    2650          37 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    2651          37 :                         move16();
    2652          37 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    2653          37 :                         move16();
    2654             :                     }
    2655           0 :                     ELSE IF( GE_32( bits, ACELP_FIXED_CDK_BITS( 0 ) ) )
    2656             :                     {
    2657           0 :                         acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    2658           0 :                         acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
    2659           0 :                         move16();
    2660           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    2661           0 :                         move16();
    2662           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    2663           0 :                         move16();
    2664           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    2665           0 :                         move16();
    2666             :                     }
    2667             :                     ELSE /* No FCB */
    2668             :                     {
    2669           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    2670           0 :                         move16();
    2671           0 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    2672           0 :                         move16();
    2673           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    2674           0 :                         move16();
    2675           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    2676           0 :                         move16();
    2677             :                     }
    2678             :                 }
    2679      221480 :                 ELSE IF( NE_16( element_mode, IVAS_CPE_TD ) && GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) )
    2680             :                 {
    2681           0 :                     bits = 100; /* 9 kbps for fcb */
    2682           0 :                     move16();
    2683           0 :                     acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    2684             :                 }
    2685             :                 ELSE
    2686             :                 {
    2687      221480 :                     acelp_FCB_allocator_ivas( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    2688             :                 }
    2689             :             }
    2690             :         }
    2691             : 
    2692      265057 :         test();
    2693      265057 :         test();
    2694      265057 :         test();
    2695             :         /* AVQ codebook */
    2696      265057 :         IF( ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    2697             :             ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    2698             :         {
    2699      161586 :             FOR( i = 0; i < nb_subfr; i++ )
    2700             :             {
    2701      134655 :                 IF( flag_hardcoded )
    2702             :                 {
    2703        6715 :                     acelp_cfg->AVQ_cdk_bits[i] = AVQ_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i_mult( i, L_SUBFR ), TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    2704        6715 :                     move16();
    2705             :                     {
    2706        6715 :                         bits = sub( bits, acelp_cfg->AVQ_cdk_bits[i] );
    2707             :                     }
    2708             :                 }
    2709             : 
    2710      134655 :                 bits = sub( bits, G_AVQ_BITS );
    2711             :             }
    2712             : 
    2713       26931 :             test();
    2714       26931 :             test();
    2715       26931 :             IF( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && LE_32( core_brate_inp, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( coder_type, GENERIC ) )
    2716             :             {
    2717             :                 /* harm. flag ACELP AVQ */
    2718       14660 :                 bits = sub( bits, 1 );
    2719             :             }
    2720             : 
    2721       26931 :             IF( !flag_hardcoded )
    2722             :             {
    2723             :                 Word16 bit_tmp;
    2724             : 
    2725       25588 :                 bit_tmp = idiv1616( bits, nb_subfr );
    2726       25588 :                 set16_fx( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
    2727       25588 :                 bits = sub( bits, imult1616( bit_tmp, nb_subfr ) );
    2728             : 
    2729       25588 :                 bit_tmp = bits % nb_subfr;
    2730       25588 :                 acelp_cfg->AVQ_cdk_bits[0] = add( acelp_cfg->AVQ_cdk_bits[0], bit_tmp );
    2731       25588 :                 bits = sub( bits, bit_tmp );
    2732             :             }
    2733             :         }
    2734             :     }
    2735       42760 :     ELSE IF( ( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) /* LBR secondary channel in TD stereo */ ||
    2736             :              ( ( coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( nb_subfr, NB_SUBFR ) ) /* GSC @12.8kHz */ ||
    2737             :              ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
    2738             :     {
    2739             :         Word32 Local_BR, Pitch_BR;
    2740             :         Word16 Pitch_CT;
    2741             : 
    2742             :         /* as defined at the beginning of [enc,dec]_pit_exc() */
    2743       42760 :         test();
    2744       42760 :         test();
    2745       42760 :         IF( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || GT_32( core_brate, GSC_H_RATE_STG ) ) )
    2746             :         {
    2747         304 :             Local_BR = ACELP_8k00;
    2748         304 :             move32();
    2749         304 :             Pitch_CT = GENERIC;
    2750         304 :             move16();
    2751         304 :             Pitch_BR = ACELP_8k00;
    2752         304 :             move32();
    2753         304 :             IF( EQ_16( L_frame, L_FRAME16k ) )
    2754             :             {
    2755           0 :                 Local_BR = ACELP_14k80;
    2756           0 :                 move32();
    2757           0 :                 test();
    2758           0 :                 if ( GSC_IVAS_mode > 0 && LT_32( core_brate, IVAS_24k4 ) )
    2759             :                 {
    2760           0 :                     Local_BR = ACELP_9k60;
    2761           0 :                     move32();
    2762             :                 }
    2763           0 :                 Pitch_BR = core_brate;
    2764           0 :                 move32();
    2765             :             }
    2766             :         }
    2767       42456 :         ELSE IF( GSC_noisy_speech )
    2768             :         {
    2769        1391 :             Local_BR = ACELP_7k20;
    2770        1391 :             move32();
    2771        1391 :             Pitch_CT = GENERIC;
    2772        1391 :             move16();
    2773        1391 :             Pitch_BR = ACELP_7k20;
    2774        1391 :             move32();
    2775        1391 :             if ( EQ_16( L_frame, L_FRAME16k ) )
    2776             :             {
    2777           0 :                 Pitch_BR = core_brate;
    2778           0 :                 move16();
    2779             :             }
    2780             :         }
    2781             :         ELSE
    2782             :         {
    2783       41065 :             Local_BR = ACELP_7k20;
    2784       41065 :             move32();
    2785       41065 :             Pitch_CT = AUDIO;
    2786       41065 :             move16();
    2787       41065 :             Pitch_BR = core_brate;
    2788       41065 :             move32();
    2789             : 
    2790       41065 :             IF( EQ_16( L_frame, L_FRAME16k ) )
    2791             :             {
    2792        7700 :                 Local_BR = ACELP_13k20;
    2793        7700 :                 move32();
    2794        7700 :                 Pitch_CT = GENERIC;
    2795        7700 :                 move16();
    2796             :             }
    2797             :         }
    2798             : 
    2799      221500 :         FOR( i = 0; i < nb_subfr; i++ )
    2800             :         {
    2801      178740 :             IF( EQ_16( L_frame, L_FRAME16k ) )
    2802             :             {
    2803       38500 :                 acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
    2804       38500 :                 move16();
    2805       38500 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    2806       38500 :                 move16();
    2807             :             }
    2808             :             ELSE
    2809             :             {
    2810      140240 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
    2811      140240 :                 move16();
    2812      140240 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    2813      140240 :                 move16();
    2814      140240 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    2815      140240 :                 move16();
    2816             :             }
    2817             :         }
    2818             :     }
    2819             : 
    2820      307822 :     test();
    2821      307822 :     test();
    2822      307822 :     test();
    2823      307822 :     IF( EQ_16( coder_type, TRANSITION ) && ( EQ_16( tc_call, 1 ) && tc_subfr == 0 && EQ_16( L_frame, L_FRAME ) ) )
    2824             :     {
    2825        7396 :         return error;
    2826             :     }
    2827             : 
    2828             :     /*-----------------------------------------------------------------*
    2829             :      * unused bits handling
    2830             :      *-----------------------------------------------------------------*/
    2831             : 
    2832      300426 :     acelp_cfg->ubits = 0; /* these bits could be reused for something else */
    2833      300426 :     move16();
    2834             : 
    2835      300426 :     test();
    2836      300426 :     IF( flag_hardcoded && NE_32( core_brate, PPP_NELP_2k80 ) )
    2837             :     {
    2838             :         /* unused bits */
    2839        2938 :         test();
    2840        2938 :         test();
    2841        2938 :         IF( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_24k40 ) ) )
    2842             :         {
    2843          19 :             acelp_cfg->ubits = 0;
    2844          19 :             move16();
    2845             :         }
    2846        2919 :         ELSE IF( EQ_16( L_frame, L_FRAME ) )
    2847             :         {
    2848        1538 :             acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
    2849        1538 :             move16();
    2850             :         }
    2851             :         ELSE
    2852             :         {
    2853        1381 :             acelp_cfg->ubits = 0;
    2854        1381 :             move16();
    2855             :         }
    2856             : 
    2857        2938 :         bits = sub( bits, acelp_cfg->ubits );
    2858             :     }
    2859             : 
    2860             :     /* sanity check */
    2861      300426 :     test();
    2862      300426 :     test();
    2863      300426 :     test();
    2864      300426 :     IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) || EQ_16( nb_subfr, NB_SUBFR16k ) )
    2865             :     {
    2866      265320 :         test();
    2867      265320 :         test();
    2868      265320 :         test();
    2869      265320 :         test();
    2870      265320 :         test();
    2871      265320 :         test();
    2872      265320 :         test();
    2873      265320 :         test();
    2874      265320 :         test();
    2875      265320 :         test();
    2876      265320 :         test();
    2877      265320 :         test();
    2878      265320 :         IF( ( EQ_16( L_frame, L_FRAME16k ) && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
    2879             :             ( GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) ) ) /* IVAS GSC @16kHz */
    2880             :         {
    2881        7700 :             acelp_cfg->ubits = 0;
    2882        7700 :             move16();
    2883             :         }
    2884      257620 :         ELSE IF( flag_hardcoded && core == ACELP_CORE && bits != 0 )
    2885             :         {
    2886             :         }
    2887      257620 :         ELSE IF( bits > 0 && !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    2888             :         {
    2889       92089 :             test();
    2890       92089 :             test();
    2891       92089 :             test();
    2892       92089 :             test();
    2893       92089 :             IF( idchan > 0 && EQ_16( element_mode, IVAS_CPE_TD ) )
    2894             :             {
    2895        3562 :                 IF( !tdm_lp_reuse_flag )
    2896             :                 {
    2897        3433 :                     acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
    2898        3433 :                     move16();
    2899        3433 :                     bits = 0;
    2900        3433 :                     move16();
    2901             :                 }
    2902             :                 ELSE
    2903             :                 {
    2904         129 :                     Word16 nb_prm = 4;
    2905         129 :                     move16();
    2906             : 
    2907         129 :                     if ( EQ_16( tdm_low_rate_mode, 1 ) )
    2908             :                     {
    2909           0 :                         nb_prm = 2;
    2910           0 :                         move16();
    2911             :                     }
    2912             :                     /* First add remaining bits on gains */
    2913         129 :                     bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode ) );
    2914             : 
    2915             :                     /* Then, Increase pitch bit budget */
    2916         129 :                     test();
    2917         129 :                     IF( tdm_Pitch_reuse_flag == 0 && bits > 0 )
    2918             :                     {
    2919           0 :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits ) );
    2920             :                     }
    2921             : 
    2922             :                     /* Increase mid-lsf bit budget */
    2923         129 :                     test();
    2924         129 :                     IF( tdm_lp_reuse_flag == 0 && bits > 0 )
    2925             :                     {
    2926           0 :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits ) );
    2927           0 :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits ) );
    2928             :                     }
    2929             :                 }
    2930             :             }
    2931             : 
    2932       88527 :             ELSE IF( core == ACELP_CORE && GE_16( coder_type, UNVOICED ) && LE_16( coder_type, GENERIC ) && EQ_16( L_frame, L_FRAME ) )
    2933             :             {
    2934       48663 :                 acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
    2935       48663 :                 move16();
    2936             : 
    2937       48663 :                 test();
    2938       48663 :                 IF( GT_16( acelp_cfg->lsf_bits, 46 ) )
    2939             :                 {
    2940           0 :                     acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 46 );
    2941           0 :                     move16();
    2942           0 :                     acelp_cfg->lsf_bits = 46;
    2943           0 :                     move16();
    2944             :                 }
    2945       48663 :                 ELSE IF( GT_16( acelp_cfg->lsf_bits, 42 ) && EQ_16( L_frame, L_FRAME ) )
    2946             :                 {
    2947         228 :                     acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 42 );
    2948         228 :                     move16();
    2949         228 :                     acelp_cfg->lsf_bits = 42;
    2950         228 :                     move16();
    2951             :                 }
    2952             :             }
    2953             :             ELSE
    2954             :             {
    2955       39864 :                 acelp_cfg->ubits = bits;
    2956       39864 :                 move16();
    2957             :             }
    2958             :         }
    2959             :         /*else if ( bits < 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    2960             :         {
    2961             :         }*/
    2962             :     }
    2963             : 
    2964      300426 :     return error;
    2965             : }
    2966             : 
    2967             : /*-------------------------------------------------------------------*
    2968             :  * allocate_unused()
    2969             :  *
    2970             :  * Allocate unused bits
    2971             :  *--------------------------------------------------------------------*/
    2972             : 
    2973         516 : static Word16 allocate_unused(
    2974             :     const Word32 core_brate,
    2975             :     const Word16 coder_type,
    2976             :     const Word16 unused_bits,
    2977             :     const Word16 nb_prm,
    2978             :     const Word16 subfr,
    2979             :     const Word16 prm_type,
    2980             :     Word16 *prm_bit_mode )
    2981             : {
    2982         516 :     Word16 max_bit_per_pos = 0, bit_added = 0;
    2983         516 :     move16();
    2984         516 :     move16();
    2985             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
    2986             : 
    2987         516 :     IF( prm_type == GAINSPRM )
    2988             :     {
    2989         516 :         max_bit_per_pos = 6;
    2990         516 :         move16();
    2991         516 :         IF( GT_32( core_brate, ACELP_8k00 ) )
    2992             :         {
    2993         424 :             max_bit_per_pos = 7;
    2994         424 :             move16();
    2995             :         }
    2996          92 :         ELSE IF( NE_16( coder_type, UNVOICED ) )
    2997             :         {
    2998          92 :             IF( GE_16( subfr, 1 ) )
    2999             :             {
    3000          69 :                 max_bit_per_pos = 7;
    3001          69 :                 move16();
    3002             :             }
    3003          23 :             ELSE IF( subfr == 0 )
    3004             :             {
    3005          23 :                 max_bit_per_pos = 8;
    3006          23 :                 move16();
    3007             :             }
    3008             :         }
    3009           0 :         ELSE IF( EQ_16( coder_type, UNVOICED ) )
    3010             :         {
    3011           0 :             max_bit_per_pos = 9; /* No real limit on UC gain bit budget of the secondary channel */
    3012           0 :             move16();
    3013             :         }
    3014             :     }
    3015           0 :     ELSE IF( EQ_16( prm_type, PITCHPRM ) )
    3016             :     {
    3017           0 :         max_bit_per_pos = 6;
    3018           0 :         move16();
    3019           0 :         test();
    3020           0 :         test();
    3021           0 :         if ( subfr == 0 || EQ_16( subfr, 2 ) || EQ_16( nb_prm, 2 ) )
    3022             :         {
    3023           0 :             max_bit_per_pos = 10;
    3024           0 :             move16();
    3025             :         }
    3026             : 
    3027           0 :         if ( EQ_16( coder_type, UNVOICED ) )
    3028             :         {
    3029           0 :             max_bit_per_pos = 0; /* Should not allocate bits in case of unvoiced coder type */
    3030           0 :             move16();
    3031             :         }
    3032             :     }
    3033           0 :     ELSE IF( EQ_16( prm_type, MID_LSFSPRM ) )
    3034             :     {
    3035           0 :         max_bit_per_pos = 5;
    3036           0 :         move16();
    3037             :     }
    3038           0 :     ELSE IF( EQ_16( prm_type, LSFPRM ) )
    3039             :     {
    3040           0 :         max_bit_per_pos = 42;
    3041           0 :         move16();
    3042             :     }
    3043             :     ELSE
    3044             :     {
    3045             : #ifdef DEBUG_MODE_TD
    3046             :         IVAS_ERROR( IVAS_ERR_WRONG_MODE, "unknown mode in bit_alloc.c" );
    3047             : #endif
    3048             :     }
    3049             : 
    3050         516 :     max_bit_per_pos = s_min( unused_bits, sub( max_bit_per_pos, prm_bit_mode[subfr] ) );
    3051         516 :     test();
    3052         516 :     IF( max_bit_per_pos < 0 )
    3053             :     {
    3054           0 :         return 0;
    3055             :     }
    3056         516 :     ELSE IF( max_bit_per_pos >= 0 && EQ_16( subfr, sub( nb_prm, 1 ) ) )
    3057             :     {
    3058         129 :         prm_bit_mode[subfr] = add( prm_bit_mode[subfr], max_bit_per_pos );
    3059         129 :         move16();
    3060             :     }
    3061             :     ELSE
    3062             :     {
    3063         387 :         prm_bit_mode[subfr] = add( prm_bit_mode[subfr], max_bit_per_pos );
    3064         387 :         move16();
    3065         387 :         bit_added = add( bit_added, allocate_unused( core_brate, coder_type, sub( unused_bits, max_bit_per_pos ), nb_prm, add( subfr, 1 ), prm_type, &prm_bit_mode[0] ) );
    3066             :     }
    3067             : 
    3068         516 :     return add( bit_added, max_bit_per_pos );
    3069             : }
    3070             : 
    3071             : 
    3072             : /*-------------------------------------------------------------------*
    3073             :  * set_ACELP_flag()
    3074             :  *
    3075             :  * set ACELP@16kHz flag
    3076             :  *--------------------------------------------------------------------*/
    3077             : 
    3078             : /*! r: ACELP16k flag */
    3079       13433 : Word16 set_ACELP_flag(
    3080             :     const Word16 element_mode,  /* i  : element mode                */
    3081             :     const Word32 element_brate, /* i  : element bitrate             */
    3082             :     const Word32 total_brate,   /* i  : total bitrate per channel   */
    3083             :     const Word16 idchan,        /* i  : Channel id                  */
    3084             :     const Word16 tdm_LRTD_flag, /* i  : LRTD stereo mode flag       */
    3085             :     const Word16 bwidth,        /* i  : audio bandwidth             */
    3086             :     const Word16 cng_type       /* i  : CNG type                    */
    3087             : )
    3088             : {
    3089             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
    3090       13433 :     test();
    3091       13433 :     test();
    3092       13433 :     test();
    3093       13433 :     test();
    3094       13433 :     IF( EQ_16( element_mode, IVAS_CPE_DFT ) && ( idchan == 0 ) && LE_32( total_brate, SID_2k40 ) && EQ_16( bwidth, WB ) && ( cng_type == LP_CNG ) )
    3095             :     {
    3096           0 :         return 1;
    3097             :     }
    3098       13433 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    3099             :     {
    3100           0 :         test();
    3101           0 :         test();
    3102           0 :         test();
    3103           0 :         IF( GE_32( element_brate, IVAS_24k4 ) && ( idchan == 0 ) && ( ( tdm_LRTD_flag == 0 ) || GT_32( element_brate, IVAS_24k4 ) ) )
    3104             :         {
    3105           0 :             return 1;
    3106             :         }
    3107             :         ELSE
    3108             :         {
    3109           0 :             return 0;
    3110             :         }
    3111             :     }
    3112       13433 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
    3113             :     {
    3114         194 :         IF( GE_32( element_brate, IVAS_24k4 ) )
    3115             :         {
    3116           0 :             return 1;
    3117             :         }
    3118             :         ELSE
    3119             :         {
    3120         194 :             return 0;
    3121             :         }
    3122             :     }
    3123       13239 :     ELSE IF( EQ_16( element_mode, IVAS_SCE ) )
    3124             :     {
    3125         983 :         IF( GE_32( element_brate, SCE_CORE_16k_LOW_LIMIT ) )
    3126             :         {
    3127           0 :             return 1;
    3128             :         }
    3129             :         ELSE
    3130             :         {
    3131         983 :             return 0;
    3132             :         }
    3133             :     }
    3134       12256 :     ELSE IF( GE_32( total_brate, ACELP_16k_LOW_LIMIT ) ) /* EVS_MONO */
    3135             :     {
    3136        4100 :         return 1;
    3137             :     }
    3138             :     ELSE
    3139             :     {
    3140        8156 :         return 0;
    3141             :     }
    3142             : }
    3143             : 
    3144             : /*-------------------------------------------------------------------*
    3145             :  * set_ACELP_flag_IVAS()
    3146             :  *
    3147             :  * set ACELP@16kHz flag
    3148             :  *--------------------------------------------------------------------*/
    3149             : 
    3150             : /*! r: ACELP16k flag */
    3151     2134776 : Word16 set_ACELP_flag_IVAS(
    3152             :     const Word16 element_mode,  /* i  : element mode                */
    3153             :     const Word32 element_brate, /* i  : element bitrate             */
    3154             :     const Word32 total_brate,   /* i  : total bitrate per channel   */
    3155             :     const Word16 idchan,        /* i  : Channel id                  */
    3156             :     const Word16 tdm_LRTD_flag, /* i  : LRTD stereo mode flag       */
    3157             :     const Word16 bwidth,        /* i  : audio bandwidth             */
    3158             :     const Word16 cng_type       /* i  : CNG type                    */
    3159             : )
    3160             : {
    3161     2134776 :     test();
    3162     2134776 :     test();
    3163     2134776 :     test();
    3164     2134776 :     test();
    3165     2134776 :     IF( EQ_16( element_mode, IVAS_CPE_DFT ) && ( idchan == 0 ) && LE_32( total_brate, SID_2k40 ) && EQ_16( bwidth, WB ) && ( cng_type == LP_CNG ) )
    3166             :     {
    3167         732 :         return 1;
    3168             :     }
    3169     2134044 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    3170             :     {
    3171       15156 :         test();
    3172       15156 :         test();
    3173       15156 :         test();
    3174       15156 :         IF( GE_32( element_brate, IVAS_24k4 ) && ( idchan == 0 ) && ( ( tdm_LRTD_flag == 0 ) || GT_32( element_brate, IVAS_24k4 ) ) )
    3175             :         {
    3176        4223 :             return 1;
    3177             :         }
    3178             :         ELSE
    3179             :         {
    3180       10933 :             return 0;
    3181             :         }
    3182             :     }
    3183     2118888 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
    3184             :     {
    3185      118156 :         IF( GE_32( element_brate, IVAS_24k4 ) )
    3186             :         {
    3187       59776 :             return 1;
    3188             :         }
    3189             :         ELSE
    3190             :         {
    3191       58380 :             return 0;
    3192             :         }
    3193             :     }
    3194     2000732 :     ELSE IF( EQ_16( element_mode, IVAS_SCE ) )
    3195             :     {
    3196      654628 :         IF( GE_32( element_brate, SCE_CORE_16k_LOW_LIMIT ) )
    3197             :         {
    3198      454629 :             return 1;
    3199             :         }
    3200             :         ELSE
    3201             :         {
    3202      199999 :             return 0;
    3203             :         }
    3204             :     }
    3205     1346104 :     ELSE IF( GE_32( total_brate, ACELP_16k_LOW_LIMIT ) ) /* EVS_MONO */
    3206             :     {
    3207     1337760 :         return 1;
    3208             :     }
    3209             :     ELSE
    3210             :     {
    3211        8344 :         return 0;
    3212             :     }
    3213             : }

Generated by: LCOV version 1.14