LCOV - code coverage report
Current view: top level - lib_com - bits_alloc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 4c82f1d24d39d0296b18d775f18a006f4c7d024b Lines: 744 935 79.6 %
Date: 2025-05-17 01:59:02 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       21334 : 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       21334 :     rate_mode_index = (Word8) GT_32( bit_rate, ACELP_9k60 );
      55             : 
      56       21334 :     pConfigAcelp->mode_index = rate_mode_index;
      57       21334 :     move16();
      58             : 
      59             :     /*LPC: midLpc should be swithced off?*/
      60       21334 :     pConfigAcelp->midLpc_enable = 1;
      61       21334 :     move16();
      62             : 
      63             :     /*ACELP ICB config*/
      64       21334 :     test();
      65       21334 :     IF( ( rate_mode_index == 0 ) || ( narrowBand != 0 ) )
      66             :     {
      67         438 :         move16();
      68         438 :         move16();
      69         438 :         move16();
      70         438 :         move16();
      71         438 :         move16();
      72         438 :         move16();
      73         438 :         pConfigAcelp->pre_emphasis = 1;
      74         438 :         pConfigAcelp->formant_enh = 1;
      75         438 :         pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
      76         438 :         pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2;
      77         438 :         pConfigAcelp->formant_tilt = 0;
      78         438 :         pConfigAcelp->voice_tilt = 0;
      79             :     }
      80             :     ELSE
      81             :     {
      82       20896 :         move16();
      83       20896 :         move16();
      84       20896 :         move16();
      85       20896 :         move16();
      86       20896 :         move16();
      87       20896 :         move16();
      88       20896 :         pConfigAcelp->pre_emphasis = 0;
      89       20896 :         pConfigAcelp->formant_enh = 1;
      90       20896 :         pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1;
      91       20896 :         pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2;
      92       20896 :         pConfigAcelp->formant_tilt = 1;
      93       20896 :         pConfigAcelp->voice_tilt = 1;
      94             :     }
      95             : 
      96             :     /*Wide band @ 16kHz*/
      97       21334 :     IF( EQ_16( nb_subfr, NB_SUBFR16k ) )
      98             :     {
      99       10914 :         move16();
     100       10914 :         move16();
     101       10914 :         move16();
     102       10914 :         move16();
     103       10914 :         move16();
     104       10914 :         move16();
     105       10914 :         pConfigAcelp->pre_emphasis = 1;
     106       10914 :         pConfigAcelp->formant_enh = 1;
     107       10914 :         pConfigAcelp->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     108       10914 :         pConfigAcelp->formant_enh_den = FORMANT_SHARPENING_G2_16k;
     109       10914 :         pConfigAcelp->formant_tilt = 0;
     110       10914 :         pConfigAcelp->voice_tilt = 2;
     111             :     }
     112       21334 : }
     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         610 : 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         610 :     move16();
     137         610 :     move16();
     138         610 :     move16();
     139         610 :     mode_index = pConfigAcelp->mode_index;
     140         610 :     band_index = ( narrowBand == 0 );
     141         610 :     bits = 0;
     142             : 
     143         610 :     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         610 :     test();
     155         610 :     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         610 :     IF( EQ_16( coder_type, UNVOICED ) )
     185             :     {
     186          26 :         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          26 :             pConfigAcelp->pitch_sharpening = 0;
     196          26 :             move16();
     197          26 :             pConfigAcelp->phase_scrambling = 0;
     198          26 :             move16();
     199             :         }
     200             :     }
     201             :     ELSE
     202             :     {
     203         584 :         pConfigAcelp->pitch_sharpening = 1;
     204         584 :         move16();
     205         584 :         pConfigAcelp->phase_scrambling = 0;
     206         584 :         move16();
     207             :     }
     208             : 
     209         610 :     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         610 :     move16();
     219         610 :     pConfigAcelp->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
     220         610 :     bits = add( bits, ACELP_BPF_BITS[pConfigAcelp->bpf_mode] );
     221             : 
     222         610 :     move16();
     223         610 :     move16();
     224         610 :     pConfigAcelp->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
     225         610 :     pConfigAcelp->nrg_bits = ACELP_NRG_BITS[pConfigAcelp->nrg_mode];
     226         610 :     bits = add( bits, pConfigAcelp->nrg_bits );
     227             : 
     228         610 :     move16();
     229         610 :     pConfigAcelp->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
     230             : 
     231         610 :     move16();
     232         610 :     pConfigAcelp->ltp_bits = 0;
     233             : 
     234         610 :     move16();
     235         610 :     pConfigAcelp->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
     236             : 
     237         610 :     move16();
     238         610 :     pConfigAcelp->ltf_bits = ACELP_LTF_BITS[pConfigAcelp->ltf_mode];
     239         610 :     test();
     240         610 :     IF( EQ_16( nb_subfr, 5 ) && EQ_16( pConfigAcelp->ltf_bits, 4 ) )
     241             :     {
     242         268 :         pConfigAcelp->ltf_bits = add( pConfigAcelp->ltf_bits, 1 );
     243         268 :         move16();
     244             :     }
     245         610 :     bits = add( bits, pConfigAcelp->ltf_bits );
     246             : 
     247             : 
     248        3660 :     FOR( i = 0; i < nb_subfr; i++ )
     249             :     {
     250        3050 :         pConfigAcelp->gains_mode[i] = ACELP_GAINS_MODE[mode_index][band_index][coder_type];
     251        3050 :         move16();
     252             : 
     253             :         /* skip subframe 1, 3 gain encoding, and use from subframe 0, and 3, respectively */
     254        3050 :         test();
     255        3050 :         test();
     256        3050 :         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        3050 :         bits = add( bits, ACELP_GAINS_BITS[pConfigAcelp->gains_mode[i]] );
     263             : 
     264        3050 :         move16();
     265        3050 :         bits = add( bits, ACELP_LTP_BITS_SFR[pConfigAcelp->ltp_mode][i] );
     266        3050 :         pConfigAcelp->ltp_bits = add( pConfigAcelp->ltp_bits, ACELP_LTP_BITS_SFR[pConfigAcelp->ltp_mode][i] );
     267             :     }
     268             : 
     269             :     /*Innovation*/
     270             : 
     271         610 :     IF( LT_16( bits_frame, bits ) )
     272             :     {
     273           0 :         printf( "Warning: bits per frame too low\n" );
     274           0 :         return -1;
     275             :     }
     276             : 
     277         610 :     IF( EQ_16( coder_type, RF_ALLPRED ) )
     278             :     {
     279           0 :         set16_fx( pConfigAcelp->fixed_cdk_index, -1, nb_subfr );
     280             :     }
     281         610 :     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         610 :     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         610 :         bits = add( bits, BITS_ALLOC_adjust_acelp_fixed_cdk( sub( bits_frame, bits ), pConfigAcelp->fixed_cdk_index, nb_subfr ) );
     303             :     }
     304             : 
     305         610 :     remaining_bits = sub( bits_frame, bits );
     306             : 
     307             :     /*Sanity check*/
     308         610 :     if ( remaining_bits < 0 )
     309             :     {
     310           0 :         move16();
     311           0 :         bits = -1;
     312             :     }
     313             : 
     314             : 
     315         610 :     return ( bits );
     316             : }
     317             : 
     318             : 
     319         610 : 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         610 :     bits_subframe2 = bits_frame;
     330         610 :     move16();
     331         610 :     inb_subfr = 8192 /*1.0f/NB_SUBFR Q15*/;
     332         610 :     move16();
     333         610 :     if ( EQ_16( nb_subfr, NB_SUBFR16k ) )
     334             :     {
     335         610 :         inb_subfr = 6554 /*1.0f/NB_SUBFR16k Q15*/;
     336         610 :         move16();
     337             :     }
     338             : 
     339         610 :     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       16340 :     FOR( k = 0; k < pulseconfig_size - 1; k++ )
     346             :     {
     347             : 
     348       16340 :         IF( GT_16( i_mult2( pulseconfigbits[k], nb_subfr ), bits_subframe2 ) )
     349             :         {
     350         610 :             k = sub( k, 1 ); /* previous mode did not exceed bit-budget */
     351         610 :             BREAK;
     352             :         }
     353             :     }
     354             : 
     355         610 :     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         610 :     move16();
     361         610 :     fixed_cdk_index[0] = k;
     362         610 :     bitsused = i_mult2( pulseconfigbits[k], nb_subfr );
     363             : 
     364        3050 :     FOR( sfr = 1; sfr < nb_subfr; sfr++ )
     365             :     {
     366             :         /*bits_currsubframe = (int)(((float)sfr+1.0f)*bits_subframe) - bitsused;*/
     367        2440 :         bits_currsubframe = sub( add( i_mult2( sfr, bits_subframe2 ), bits_subframe2 ), bitsused );
     368             : 
     369             :         /* try increasing mode while below threshold */
     370        2440 :         test();
     371        2834 :         WHILE( ( ( k < sub( pulseconfig_size, 1 ) ) ) && ( ( i_mult2( pulseconfigbits[k + 1], nb_subfr ) <= bits_currsubframe ) ) )
     372             :         {
     373         394 :             test();
     374         394 :             k = add( k, 1 );
     375             :         }
     376             : 
     377             :         /* try decreasing mode until below threshold */
     378        2704 :         WHILE( ( i_mult2( pulseconfigbits[k], nb_subfr ) > bits_currsubframe ) )
     379             :         {
     380         264 :             k = sub( k, 1 );
     381             : 
     382         264 :             IF( k == 0 )
     383             :             {
     384           0 :                 BREAK;
     385             :             }
     386             :         }
     387             : 
     388             :         /* store mode */
     389        2440 :         move16();
     390        2440 :         fixed_cdk_index[sfr] = k;
     391        2440 :         bitsused = add( bitsused, i_mult2( pulseconfigbits[k], nb_subfr ) );
     392             :     }
     393             : 
     394         610 :     return mult_r( bitsused, inb_subfr );
     395             : }
     396             : 
     397         610 : 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         610 :     bitsused = BITS_ALLOC_adjust_generic( bits_frame, fixed_cdk_index, nb_subfr, ACELP_CDK_BITS, ACELP_FIXED_CDK_NB );
     406             : 
     407             : 
     408         610 :     return bitsused;
     409             : }
     410             : 
     411             : 
     412             : /*-------------------------------------------------------------------*
     413             :  * fcb_table()
     414             :  *
     415             :  * Selection of fixed innovation codebook bitbudget table
     416             :  *--------------------------------------------------------------------*/
     417             : 
     418     2172142 : 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     2172142 :     out = PulseConfTable[n].bits;
     426     2172142 :     move16();
     427     2172142 :     if ( GT_16( L_subfr, L_SUBFR ) )
     428             :     {
     429           0 :         out = fast_FCB_bits_2sfr[n];
     430           0 :         move16();
     431             :     }
     432             : 
     433     2172142 :     return ( out );
     434             : }
     435             : 
     436             : /*-------------------------------------------------------------------*
     437             :  * acelp_FCB_allocator_fx()
     438             :  *
     439             :  * Routine to allocate fixed innovation codebook bit-budget
     440             :  *--------------------------------------------------------------------*/
     441             : 
     442      110185 : static ivas_error acelp_FCB_allocator_fx(
     443             :     Word16 *nBits,            /* i/o: available bit-budget    */
     444             :     Word16 fixed_cdk_index[], /* o  : codebook index       Q0 */
     445             :     Word16 nb_subfr,          /* i  : number of subframes     */
     446             :     const Word16 L_subfr,     /* i  : subframe length         */
     447             :     const Word16 coder_type,  /* i  : coder type              */
     448             :     const Word16 tc_subfr,    /* i  : TC subframe index       */
     449             :     const Word16 fix_first    /* i  : flag to indicate whether the first subframe bit-budget was fixed */
     450             : )
     451             : {
     452             :     Word16 cdbk, sfr, step;
     453             :     Word16 nBits_tmp;
     454             :     Word16 *p_fixed_cdk_index;
     455             :     Word16 max_n;
     456             :     ivas_error error;
     457             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
     458             : 
     459      110185 :     error = IVAS_ERR_OK;
     460      110185 :     move32();
     461             : 
     462      110185 :     cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
     463      110185 :     move16();
     464             : 
     465      110185 :     p_fixed_cdk_index = fixed_cdk_index;
     466             : 
     467             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse not in the first subframe */
     468      110185 :     test();
     469      110185 :     IF( GE_16( tc_subfr, L_SUBFR ) && fix_first )
     470             :     {
     471             :         Word16 i;
     472             : 
     473           0 :         FOR( i = 0; i < nb_subfr; i++ )
     474             :         {
     475           0 :             *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] ) );
     476           0 :             move16();
     477             :         }
     478           0 :         return error;
     479             :     }
     480             : 
     481             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
     482      110185 :     sfr = 0;
     483      110185 :     move16();
     484      110185 :     IF( fix_first )
     485             :     {
     486        3470 :         *nBits = sub( *nBits, ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] ) );
     487        3470 :         move16();
     488        3470 :         sfr = 1;
     489        3470 :         move16();
     490        3470 :         p_fixed_cdk_index++;
     491        3470 :         nb_subfr = 3;
     492        3470 :         move16();
     493             :     }
     494             : 
     495             :     /* distribute the bit-budget equally between subframes */
     496      110185 :     IF( GT_16( L_subfr, L_SUBFR ) ) /* access fast_FCB_bits_2sfr */
     497             :     {
     498           0 :         max_n = 6;
     499           0 :         move16();
     500             :     }
     501             :     ELSE
     502             :     {
     503      110185 :         max_n = ACELP_FIXED_CDK_NB;
     504      110185 :         move16();
     505             :     }
     506     1731402 :     FOR( cdbk = 0; cdbk < max_n; cdbk++ )
     507             :     {
     508     1731402 :         IF( GT_32( L_mult0( fcb_table( cdbk, L_subfr ), nb_subfr ), L_deposit_l( *nBits ) ) )
     509             :         {
     510      110185 :             BREAK;
     511             :         }
     512             :     }
     513      110185 :     cdbk = sub( cdbk, 1 );
     514             : 
     515      110185 :     set16_fx( p_fixed_cdk_index, cdbk, nb_subfr );
     516      110185 :     nBits_tmp = 0;
     517      110185 :     move16();
     518      110185 :     IF( cdbk >= 0 )
     519             :     {
     520      110185 :         nBits_tmp = fcb_table( cdbk, L_subfr );
     521             :     }
     522             :     ELSE
     523             :     {
     524           0 :         nBits_tmp = 0;
     525           0 :         move16();
     526             :     }
     527      110185 :     *nBits = sub( *nBits, i_mult( nBits_tmp, nb_subfr ) );
     528      110185 :     move16();
     529             : 
     530             :     /* try to increase the FCB bit-budget of the first subframe(s) */
     531      110185 :     IF( LT_16( cdbk, ACELP_FIXED_CDK_NB - 1 ) )
     532             :     {
     533      110185 :         step = sub( fcb_table( add( cdbk, 1 ), L_subfr ), nBits_tmp );
     534      296241 :         WHILE( *nBits >= step )
     535             :         {
     536      186056 :             ( *p_fixed_cdk_index )++;
     537      186056 :             *nBits = sub( *nBits, step );
     538      186056 :             move16();
     539      186056 :             p_fixed_cdk_index++;
     540             :         }
     541             : 
     542             :         /* try to increase the FCB of the first subframe in cases when the next step is lower than the current step */
     543      110185 :         IF( LT_16( fixed_cdk_index[sfr], ACELP_FIXED_CDK_NB - 1 ) )
     544             :         {
     545      110185 :             step = sub( fcb_table( add( fixed_cdk_index[sfr], 1 ), L_subfr ), fcb_table( fixed_cdk_index[sfr], L_subfr ) );
     546      110185 :             test();
     547      110185 :             IF( GE_16( *nBits, step ) && cdbk >= 0 )
     548             :             {
     549       16777 :                 fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
     550       16777 :                 move16();
     551       16777 :                 *nBits = sub( *nBits, step );
     552       16777 :                 move16();
     553       16777 :                 test();
     554       16777 :                 IF( GE_16( *nBits, step ) && EQ_16( fixed_cdk_index[sfr + 1], sub( fixed_cdk_index[sfr], 1 ) ) )
     555             :                 {
     556        1484 :                     sfr = add( sfr, 1 );
     557        1484 :                     fixed_cdk_index[sfr] = add( fixed_cdk_index[sfr], 1 );
     558        1484 :                     move16();
     559        1484 :                     *nBits = sub( *nBits, step );
     560        1484 :                     move16();
     561             :                 }
     562             :             }
     563             :         }
     564             :     }
     565             :     /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
     566      110185 :     IF( GE_16( tc_subfr, L_SUBFR ) )
     567             :     {
     568             :         Word16 tempr;
     569             : 
     570        5860 :         SWAP( fixed_cdk_index[0], fixed_cdk_index[tc_subfr / L_SUBFR] );
     571             : 
     572             :         /* TRANSITION coding: allocate second highest FCBQ bit-budget to the last subframe */
     573        5860 :         IF( LT_16( idiv1616( tc_subfr, L_SUBFR ), sub( nb_subfr, 1 ) ) )
     574             :         {
     575        2855 :             SWAP( fixed_cdk_index[sub( tc_subfr, L_SUBFR ) / L_SUBFR], fixed_cdk_index[nb_subfr - 1] );
     576             :         }
     577             :     }
     578             : 
     579             :     /* when subframe length > L_SUBFR, number of bits instead of codebook index is signalled */
     580      110185 :     IF( GT_16( L_subfr, L_SUBFR ) )
     581             :     {
     582             :         Word16 i, j;
     583           0 :         FOR( i = 0; i < nb_subfr; i++ )
     584             :         {
     585           0 :             j = fixed_cdk_index[i];
     586           0 :             move16();
     587           0 :             fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
     588           0 :             move16();
     589             :         }
     590             :     }
     591             : 
     592      110185 :     return error;
     593             : }
     594             : 
     595             : 
     596             : /*-------------------------------------------------------------------*
     597             :  * config_acelp1_fx()
     598             :  *
     599             :  * Configure ACELP bit allocation
     600             :  *   - should be in range of <6700; 24350> for ACELP@12.8kHz
     601             :  *   - per channel bitrate minimum is 13250 kbps for ACELP@16kHz
     602             :  *--------------------------------------------------------------------*/
     603             : 
     604      157466 : ivas_error config_acelp1_fx(
     605             :     const Word16 enc_dec,                  /* i  : encoder/decoder flag            */
     606             :     const Word32 total_brate,              /* i  : total bitrate                   */
     607             :     const Word32 core_brate_inp,           /* i  : core bitrate                    */
     608             :     const Word16 core,                     /* i  : core                            */
     609             :     const Word16 extl,                     /* i  : extension layer                 */
     610             :     const Word32 extl_brate,               /* i  : extension layer bitrate         */
     611             :     const Word16 L_frame,                  /* i  : frame length at internal Fs     */
     612             :     const Word16 GSC_noisy_speech,         /* i  : GSC on SWB noisy speech flag    */
     613             :     ACELP_config *acelp_cfg,               /* i  : ACELP bit-allocation            */
     614             :     const Word16 signaling_bits,           /* i  : number of signaling bits        */
     615             :     const Word16 coder_type,               /* i  : coder type                      */
     616             :     const Word16 inactive_coder_type_flag, /* i  : AVQ (0) or GSC (1) IC flag      */
     617             :     const Word16 tc_subfr,                 /* i  : TC subfr ID                     */
     618             :     const Word16 tc_call,                  /* i  : TC call number (0,1,2,3,5(DEC)) */
     619             :     Word16 *nBits_es_Pred,                 /* o  : number of bits for Es_pred Q    */
     620             :     Word16 *unbits,                        /* o  : number of unused bits           */
     621             :     const Word16 element_mode,             /* i  : element mode                    */
     622             :     Word16 *uc_two_stage_flag,             /* o  : flag undicating two-stage UC    */
     623             :     const Word16 tdm_lp_reuse_flag,        /* i  : LPC reuse flag (can be 1 only with secondary channel */
     624             :     const Word16 tdm_low_rate_mode,        /* i  : secondary channel low rate mode flag */
     625             :     const Word16 idchan,                   /* i  : stereo channel ID               */
     626             :     const Word16 active_cnt,               /* i  : Active frame counter            */
     627             :     const Word16 tdm_Pitch_reuse_flag,     /* i  : primary channel pitch reuse flag*/
     628             :     const Word16 tdm_LRTD_flag,            /* i  : LRTD stereo mode flag           */
     629             :     const Word16 GSC_IVAS_mode             /* i  : GSC IVAS mode                   */
     630             : )
     631             : {
     632             :     Word16 i, bits, nb_subfr;
     633             :     Word16 flag_hardcoded, coder_type_sw, fix_first;
     634             :     Word32 core_brate;
     635             :     ivas_error error;
     636             : 
     637      157466 :     error = IVAS_ERR_OK;
     638      157466 :     move32();
     639             : 
     640             :     /*-----------------------------------------------------------------*
     641             :      * Set the flag indicating two-stage Unvoiced (UC) frame
     642             :      *-----------------------------------------------------------------*/
     643             : 
     644      157466 :     *uc_two_stage_flag = 0;
     645      157466 :     move16();
     646             : 
     647      157466 :     IF( EQ_16( coder_type, UNVOICED ) )
     648             :     {
     649        1401 :         test();
     650        1401 :         test();
     651        1401 :         test();
     652        1401 :         test();
     653        1401 :         test();
     654        1401 :         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 ) ) ) )
     655             :         {
     656        1375 :             *uc_two_stage_flag = 1;
     657        1375 :             move16();
     658             :         }
     659             :     }
     660             : 
     661             :     /*-----------------------------------------------------------------*
     662             :      * Set the number of subframes
     663             :      *-----------------------------------------------------------------*/
     664             : 
     665      157466 :     IF( EQ_16( L_frame, L_FRAME ) )
     666             :     {
     667       88120 :         nb_subfr = NB_SUBFR;
     668       88120 :         move16();
     669             :     }
     670             :     ELSE /* L_frame == L_FRAME16k */
     671             :     {
     672       69346 :         nb_subfr = NB_SUBFR16k;
     673       69346 :         move16();
     674             :     }
     675             : 
     676      157466 :     coder_type_sw = coder_type;
     677      157466 :     move16();
     678      157466 :     IF( core != ACELP_CORE )
     679             :     {
     680             :         /* used in acelp_core_switch_enc() */
     681          24 :         nb_subfr = 1;
     682          24 :         move16();
     683          24 :         if ( EQ_16( L_frame, L_FRAME ) )
     684             :         {
     685           5 :             coder_type_sw = TRANSITION;
     686           5 :             move16();
     687             :         }
     688             :     }
     689             : 
     690             :     /*-----------------------------------------------------------------*
     691             :      * Check if the core_brate is hard coded (to keep BE for mono core) or not
     692             :      *-----------------------------------------------------------------*/
     693             : 
     694      157466 :     flag_hardcoded = 0;
     695      157466 :     move16();
     696      157466 :     i = 0;
     697      157466 :     move16();
     698             : 
     699     1458929 :     WHILE( i < SIZE_BRATE_INTERMED_TBL )
     700             :     {
     701     1458929 :         IF( EQ_32( core_brate_inp, brate_intermed_tbl[i] ) )
     702             :         {
     703        6846 :             flag_hardcoded = 1;
     704        6846 :             move16();
     705        6846 :             BREAK;
     706             :         }
     707             : 
     708     1452083 :         IF( LT_32( core_brate_inp, brate_intermed_tbl[i] ) )
     709             :         {
     710      150620 :             flag_hardcoded = 0;
     711      150620 :             move16();
     712      150620 :             BREAK;
     713             :         }
     714             : 
     715     1301463 :         i = add( i, 1 );
     716             :     }
     717             : 
     718      157466 :     test();
     719      157466 :     test();
     720      157466 :     test();
     721      157496 :     if ( EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( coder_type, AUDIO ) &&
     722          60 :          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 */
     723             :     {
     724           4 :         i = sub( i, 1 );
     725             :     }
     726             : 
     727      157466 :     core_brate = brate_intermed_tbl[i];
     728      157466 :     move32();
     729             : 
     730      157466 :     if ( element_mode > EVS_MONO )
     731             :     {
     732      155964 :         flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
     733      155964 :         move16();
     734             :     }
     735             : 
     736      157466 :     test();
     737      157466 :     if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
     738             :     {
     739          24 :         flag_hardcoded = 1;
     740          24 :         move16();
     741             :     }
     742             : 
     743             :     /*-----------------------------------------------------------------*
     744             :      * ACELP bit allocation
     745             :      *-----------------------------------------------------------------*/
     746             : 
     747      157466 :     test();
     748      157466 :     test();
     749      157466 :     IF( !( EQ_16( coder_type, TRANSITION ) && NE_16( tc_subfr, -1 ) ) || EQ_16( enc_dec, DEC ) )
     750             :     {
     751             :         /* Set the bit-budget */
     752      157466 :         bits = divide3216( core_brate_inp, FRAMES_PER_SEC / 2 );
     753             : 
     754      157466 :         test();
     755      157466 :         test();
     756      157466 :         IF( EQ_16( coder_type, TRANSITION ) && EQ_16( enc_dec, DEC ) && EQ_16( tc_call, 1 ) )
     757             :         {
     758       12668 :             bits = add( bits, *nBits_es_Pred ); /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
     759             :         }
     760             : 
     761      157466 :         test();
     762      157466 :         test();
     763             :         /* Subtract signaling bits */
     764      157466 :         IF( EQ_16( enc_dec, DEC ) && EQ_16( idchan, 1 ) && element_mode > EVS_MONO )
     765             :         {
     766        3695 :             bits = sub( bits, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS );
     767             : 
     768        3695 :             IF( EQ_16( tdm_LRTD_flag, 1 ) )
     769             :             {
     770        3569 :                 bits = add( bits, STEREO_BITS_TCA );
     771             :             }
     772             : 
     773        3695 :             test();
     774        3695 :             test();
     775        3695 :             test();
     776        3695 :             test();
     777        3695 :             test();
     778        3695 :             test();
     779             :             /* subtract TBE/BWE flag */
     780        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 ) ) )
     781             :             {
     782        2793 :                 bits = sub( bits, 1 );
     783             :             }
     784             :         }
     785             :         ELSE
     786             :         {
     787             :             /* Subtract signaling bits */
     788      153771 :             bits = sub( bits, signaling_bits );
     789             :         }
     790             : 
     791      157466 :         test();
     792      157466 :         test();
     793      157466 :         test();
     794      157466 :         test();
     795      157466 :         test();
     796      157466 :         test();
     797      157466 :         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 ) ) )
     798             :         {
     799             :             /* extension layer signaling bit is counted in the extension layer bitbudget */
     800      140609 :             bits = add( bits, 1 );
     801             :         }
     802             : 
     803             :         /*-----------------------------------------------------------------*
     804             :          * LSF Q bit-budget
     805             :          *-----------------------------------------------------------------*/
     806             : 
     807      157466 :         test();
     808      157466 :         test();
     809      157466 :         test();
     810      157466 :         IF( !tdm_lp_reuse_flag || idchan == 0 )
     811             :         {
     812             :             /* LSF Q bit-budget */
     813      157325 :             acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     814      157325 :             move16();
     815             : 
     816      157325 :             IF( !flag_hardcoded )
     817             :             {
     818      155823 :                 IF( EQ_16( L_frame, L_FRAME ) )
     819             :                 {
     820       87165 :                     test();
     821       87165 :                     test();
     822       87165 :                     test();
     823       87165 :                     test();
     824       87165 :                     test();
     825       87165 :                     test();
     826       87165 :                     test();
     827       87165 :                     IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
     828             :                     {
     829        6736 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     830        6736 :                         move16();
     831             :                     }
     832       80429 :                     ELSE IF( ( LT_32( total_brate, 7200 ) || coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( idchan, 1 ) )
     833             :                     {
     834             :                         /* TD stereo, secondary channel: do nothing */
     835          50 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     836          50 :                         move16();
     837             :                     }
     838       80379 :                     ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) && LT_32( brate_intermed_tbl[i], ACELP_9k60 ) )
     839             :                     {
     840             :                         /* primary channel: do nothing */
     841             :                     }
     842       70027 :                     ELSE IF( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
     843             :                     {
     844        9194 :                         acelp_cfg->lsf_bits = 42;
     845        9194 :                         move16();
     846             :                     }
     847       60833 :                     ELSE IF( LE_32( total_brate, 9600 ) || EQ_16( coder_type, UNVOICED ) )
     848             :                     {
     849        8440 :                         acelp_cfg->lsf_bits = 31;
     850        8440 :                         move16();
     851             :                     }
     852       52393 :                     ELSE IF( LE_32( total_brate, 20000 ) )
     853             :                     {
     854       51055 :                         acelp_cfg->lsf_bits = 36;
     855       51055 :                         move16();
     856             :                     }
     857             :                     ELSE
     858             :                     {
     859        1338 :                         acelp_cfg->lsf_bits = 41;
     860        1338 :                         move16();
     861             :                     }
     862             :                 }
     863             :                 ELSE /* L_frame == L_FRAME16k */
     864             :                 {
     865       68658 :                     acelp_cfg->lsf_bits = 41;
     866       68658 :                     move16();
     867             :                 }
     868             :             }
     869             : 
     870      157325 :             bits = sub( bits, acelp_cfg->lsf_bits );
     871             : 
     872             :             /* mid-LSF Q bit-budget */
     873      157325 :             acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     874      157325 :             move16();
     875             : 
     876      157325 :             test();
     877      157325 :             if ( element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
     878             :             {
     879       19546 :                 acelp_cfg->mid_lsf_bits = 5;
     880       19546 :                 move16();
     881             :                 /* primary channel: do nothing */
     882             :             }
     883             : 
     884      157325 :             bits = sub( bits, acelp_cfg->mid_lsf_bits );
     885             :         }
     886         141 :         ELSE IF( EQ_16( tdm_lp_reuse_flag, 1 ) && EQ_16( idchan, 1 ) && NE_16( active_cnt, 1 ) )
     887             :         {
     888         141 :             bits = sub( bits, TDM_IC_LSF_PRED_BITS );
     889             :         }
     890             : 
     891      157466 :         test();
     892      157466 :         test();
     893      157466 :         test();
     894      157466 :         test();
     895      157466 :         test();
     896      157466 :         test();
     897             :         /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
     898      157466 :         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 */ ||
     899             :             ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */ )
     900             :         {
     901      118534 :             *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
     902      118534 :             move16();
     903      118534 :             bits = sub( bits, *nBits_es_Pred );
     904             :         }
     905       38932 :         ELSE IF( *uc_two_stage_flag )
     906             :         {
     907        1375 :             *nBits_es_Pred = 4;
     908        1375 :             move16();
     909        1375 :             bits = sub( bits, *nBits_es_Pred );
     910             :         }
     911             :     }
     912             :     ELSE
     913             :     {
     914           0 :         bits = *unbits;
     915           0 :         move16();
     916             :     }
     917             : 
     918      157466 :     test();
     919      157466 :     IF( EQ_16( coder_type, TRANSITION ) && EQ_16( tc_call, 0 ) )
     920             :     {
     921           0 :         *unbits = bits;
     922           0 :         move16();
     923           0 :         return error;
     924             :     }
     925             : 
     926             :     /*-----------------------------------------------------------------*
     927             :      * Low-rate mode - bits are allocated in tdm_low_rate_enc()
     928             :      *-----------------------------------------------------------------*/
     929             : 
     930      157466 :     test();
     931      157466 :     IF( EQ_16( element_mode, IVAS_SCE ) && tdm_low_rate_mode )
     932             :     {
     933        6736 :         acelp_cfg->FEC_mode = 0;
     934        6736 :         move16();
     935        6736 :         acelp_cfg->ltf_mode = FULL_BAND;
     936        6736 :         move16();
     937        6736 :         *nBits_es_Pred = 0;
     938        6736 :         move16();
     939        6736 :         *unbits = 0;
     940        6736 :         move16();
     941        6736 :         acelp_cfg->ubits = 0;
     942        6736 :         move16();
     943             : 
     944        6736 :         return error;
     945             :     }
     946             : 
     947             :     /*-----------------------------------------------------------------*
     948             :      * Supplementary information for FEC
     949             :      *-----------------------------------------------------------------*/
     950             : 
     951      150730 :     acelp_cfg->FEC_mode = 0;
     952      150730 :     move16();
     953      150730 :     test();
     954      150730 :     test();
     955      150730 :     IF( GE_32( core_brate, ACELP_11k60 ) && ( idchan == 0 || element_mode == EVS_MONO ) )
     956             :     {
     957      118655 :         acelp_cfg->FEC_mode = 1;
     958      118655 :         move16();
     959             : 
     960      118655 :         test();
     961      118655 :         test();
     962      118655 :         if ( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) && NE_16( coder_type, VOICED ) )
     963             :         {
     964       98967 :             bits = sub( bits, FEC_BITS_CLS );
     965             :         }
     966             : 
     967      118655 :         IF( NE_16( coder_type, TRANSITION ) )
     968             :         {
     969      104529 :             IF( GE_32( total_brate, ACELP_16k40 ) )
     970             :             {
     971       65243 :                 acelp_cfg->FEC_mode = 2;
     972       65243 :                 move16();
     973             : 
     974       65243 :                 test();
     975       65243 :                 IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
     976             :                 {
     977       57642 :                     bits = sub( bits, FEC_BITS_ENR );
     978             :                 }
     979             :             }
     980             : 
     981      104529 :             IF( GE_32( total_brate, ACELP_32k ) )
     982             :             {
     983       10358 :                 acelp_cfg->FEC_mode = 3;
     984       10358 :                 move16();
     985             : 
     986       10358 :                 test();
     987       10358 :                 IF( GT_16( coder_type, UNVOICED ) && LT_16( coder_type, AUDIO ) )
     988             :                 {
     989        9018 :                     bits = sub( bits, FEC_BITS_POS );
     990             :                 }
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995             :     /*-----------------------------------------------------------------*
     996             :      * LP filtering of the adaptive excitation
     997             :      *-----------------------------------------------------------------*/
     998             : 
     999      150730 :     test();
    1000      150730 :     test();
    1001      150730 :     test();
    1002      150730 :     test();
    1003      150730 :     test();
    1004      150730 :     test();
    1005      150730 :     IF( idchan > 0 && element_mode > EVS_MONO )
    1006             :     {
    1007        3695 :         acelp_cfg->ltf_mode = FULL_BAND;
    1008        3695 :         move16();
    1009             :     }
    1010      147035 :     ELSE IF( EQ_16( coder_type, UNVOICED ) )
    1011             :     {
    1012        1306 :         acelp_cfg->ltf_mode = FULL_BAND;
    1013        1306 :         move16();
    1014             :     }
    1015      145729 :     ELSE IF( ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) ) && LT_32( core_brate, ACELP_11k60 ) )
    1016             :     {
    1017        9951 :         acelp_cfg->ltf_mode = LOW_PASS;
    1018        9951 :         move16();
    1019             :     }
    1020      135778 :     ELSE IF( GE_32( core_brate, ACELP_11k60 ) && ( NE_16( coder_type, AUDIO ) && !( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME ) ) ) )
    1021             :     {
    1022      110011 :         test();
    1023      110011 :         test();
    1024      110011 :         IF( coder_type == INACTIVE && EQ_16( L_frame, L_FRAME16k ) && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
    1025             :         {
    1026        5050 :             acelp_cfg->ltf_mode = FULL_BAND;
    1027        5050 :             move16();
    1028             :         }
    1029             :         ELSE
    1030             :         {
    1031      104961 :             acelp_cfg->ltf_mode = NORMAL_OPERATION;
    1032      104961 :             move16();
    1033      104961 :             if ( coder_type != TRANSITION )
    1034             :             {
    1035       90835 :                 bits = sub( bits, nb_subfr );
    1036             :             }
    1037             :         }
    1038             :     }
    1039             :     ELSE
    1040             :     {
    1041       25767 :         acelp_cfg->ltf_mode = FULL_BAND;
    1042       25767 :         move16();
    1043             :     }
    1044             : 
    1045             :     /*-----------------------------------------------------------------*
    1046             :      * UC bit-budget
    1047             :      *-----------------------------------------------------------------*/
    1048             : 
    1049      150730 :     test();
    1050      150730 :     test();
    1051      150730 :     test();
    1052      150730 :     test();
    1053      150730 :     test();
    1054      150730 :     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 ) )
    1055             :     {
    1056         674 :         bits = sub( bits, NBITS_NOISENESS ); /* noiseness */
    1057             :     }
    1058      150730 :     test();
    1059      150730 :     IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
    1060             :     {
    1061          26 :         bits = sub( bits, ( 3 * NB_SUBFR ) ); /* tilt factor */
    1062             :     }
    1063             : 
    1064             :     /*-----------------------------------------------------------------*
    1065             :      * TC bit-budget
    1066             :      *-----------------------------------------------------------------*/
    1067             : 
    1068      150730 :     fix_first = 0;
    1069      150730 :     move16();
    1070      150730 :     IF( EQ_16( coder_type, TRANSITION ) )
    1071             :     {
    1072       16187 :         IF( EQ_16( tc_call, 2 ) )
    1073             :         {
    1074        3519 :             fix_first = 1;
    1075        3519 :             move16();
    1076             :         }
    1077             : 
    1078             :         /* TC signaling */
    1079       16187 :         IF( EQ_16( L_frame, L_FRAME ) )
    1080             :         {
    1081        9998 :             IF( EQ_16( tc_subfr, TC_0_0 ) )
    1082             :             {
    1083        1532 :                 IF( enc_dec == ENC )
    1084             :                 {
    1085           0 :                     bits = sub( bits, 1 ); /* TC signaling */
    1086             :                 }
    1087             : 
    1088        1532 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1089             :                 {
    1090        1192 :                     bits = sub( bits, 3 ); /* LP filtering flag */
    1091             :                 }
    1092             :             }
    1093        8466 :             ELSE IF( EQ_16( tc_subfr, TC_0_64 ) )
    1094             :             {
    1095        1180 :                 IF( enc_dec == ENC )
    1096             :                 {
    1097           0 :                     bits = sub( bits, 4 ); /* TC signaling */
    1098             :                 }
    1099             : 
    1100        1180 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1101             :                 {
    1102         916 :                     bits = sub( bits, 3 ); /* LP filtering flag */
    1103             :                 }
    1104             :             }
    1105        7286 :             ELSE IF( EQ_16( tc_subfr, TC_0_128 ) )
    1106             :             {
    1107         619 :                 IF( enc_dec == ENC )
    1108             :                 {
    1109           0 :                     bits = sub( bits, 4 ); /* TC signaling */
    1110             :                 }
    1111             : 
    1112         619 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1113             :                 {
    1114         529 :                     bits = sub( bits, 2 ); /* LP filtering flag */
    1115             :                 }
    1116             :             }
    1117        6667 :             ELSE IF( EQ_16( tc_subfr, TC_0_192 ) )
    1118             :             {
    1119         188 :                 IF( enc_dec == ENC )
    1120             :                 {
    1121           0 :                     bits = sub( bits, 3 ); /* TC signaling */
    1122             :                 }
    1123             : 
    1124         188 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) )
    1125             :                 {
    1126         163 :                     bits = sub( bits, 1 ); /* LP filtering flag */
    1127             :                 }
    1128             :             }
    1129        6479 :             ELSE IF( EQ_16( tc_subfr, L_SUBFR ) )
    1130             :             {
    1131        1011 :                 IF( enc_dec == ENC )
    1132             :                 {
    1133           0 :                     bits = sub( bits, 3 ); /* TC signaling */
    1134             :                 }
    1135             : 
    1136        1011 :                 test();
    1137        1011 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) && NE_16( tc_subfr, 192 ) )
    1138             :                 {
    1139         782 :                     bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    1140             :                 }
    1141             :             }
    1142             :             ELSE
    1143             :             {
    1144        5468 :                 IF( enc_dec == ENC )
    1145             :                 {
    1146           0 :                     bits = sub( bits, 4 ); /* TC signaling */
    1147             :                 }
    1148             : 
    1149        5468 :                 test();
    1150        5468 :                 IF( EQ_16( acelp_cfg->ltf_mode, NORMAL_OPERATION ) && NE_16( tc_subfr, 192 ) )
    1151             :                 {
    1152        3136 :                     bits = sub( bits, idiv1616( sub( L_FRAME - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    1153             :                 }
    1154             :             }
    1155             :         }
    1156             :         ELSE /* L_frame == L_FRAME16k */
    1157             :         {
    1158        6189 :             IF( enc_dec == ENC )
    1159             :             {
    1160           0 :                 IF( LE_16( tc_subfr, 2 * L_SUBFR ) )
    1161             :                 {
    1162           0 :                     bits = sub( bits, 2 ); /* TC signaling */
    1163             :                 }
    1164             :                 ELSE
    1165             :                 {
    1166           0 :                     bits = sub( bits, 3 ); /* TC signaling */
    1167             :                 }
    1168             :             }
    1169             : 
    1170        6189 :             IF( NE_16( tc_subfr, 256 ) )
    1171             :             {
    1172        4361 :                 bits = sub( bits, idiv1616( sub( L_FRAME16k - L_SUBFR, tc_subfr ), L_SUBFR ) ); /* LP filtering flag */
    1173             :             }
    1174             :         }
    1175             : 
    1176             :         /* glottal-shape codebook bits */
    1177       16187 :         bits = sub( bits, ( 3 + 6 + 1 + 3 ) );
    1178             :     }
    1179             : 
    1180             :     /*-----------------------------------------------------------------*
    1181             :      * pitch, innovation, gains bit-budget
    1182             :      *-----------------------------------------------------------------*/
    1183             : 
    1184      150730 :     acelp_cfg->fcb_mode = 0;
    1185      150730 :     move16();
    1186             : 
    1187      150730 :     test();
    1188      150730 :     test();
    1189      150730 :     test();
    1190      150730 :     test();
    1191      150730 :     test();
    1192      150730 :     test();
    1193      150730 :     test();
    1194      150730 :     test();
    1195      150730 :     test();
    1196      150730 :     test();
    1197      150730 :     test();
    1198      150730 :     test();
    1199      150730 :     test();
    1200      150730 :     test();
    1201      150730 :     test();
    1202      150730 :     test();
    1203      150730 :     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 */
    1204             :     {
    1205           0 :         set16_fx( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
    1206           0 :         set16_fx( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
    1207             : 
    1208           0 :         FOR( i = 0; i < 2; i++ )
    1209             :         {
    1210           0 :             acelp_cfg->pitch_bits[i] = 0;
    1211           0 :             move16();
    1212           0 :             IF( tdm_Pitch_reuse_flag == 0 )
    1213             :             {
    1214           0 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1215           0 :                 move16();
    1216           0 :                 bits = sub( bits, acelp_cfg->pitch_bits[i] );
    1217             :             }
    1218           0 :             acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1219           0 :             move16();
    1220           0 :             bits = sub( bits, acelp_cfg->gains_mode[i] );
    1221             :         }
    1222           0 :         acelp_cfg->fcb_mode = 1;
    1223           0 :         move16();
    1224             : 
    1225           0 :         IF( GE_16( bits, 16 ) )
    1226             :         {
    1227           0 :             acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1228             :         }
    1229             :         ELSE
    1230             :         {
    1231           0 :             acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1232           0 :             acelp_cfg->fixed_cdk_index[1] = -1;
    1233           0 :             move16();
    1234             :         }
    1235           0 :         acelp_cfg->fixed_cdk_index[2] = -1;
    1236           0 :         move16();
    1237           0 :         acelp_cfg->fixed_cdk_index[3] = -1;
    1238           0 :         move16();
    1239             :     }
    1240      150730 :     ELSE IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) /* @12.8kHz core except of GSC */ ||
    1241             :              ( EQ_16( nb_subfr, NB_SUBFR16k ) && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */ ||
    1242             :              EQ_16( core, HQ_CORE ) /* ACELP -> HQ switching in EVS */ )
    1243             :     {
    1244             :         /* pitch Q & gain Q bit-budget - part 2*/
    1245      679885 :         FOR( i = 0; i < nb_subfr; i++ )
    1246             :         {
    1247      556749 :             IF( EQ_16( L_frame, L_FRAME ) )
    1248             :             {
    1249      235345 :                 test();
    1250      235345 :                 IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
    1251             :                 {
    1252         132 :                     acelp_cfg->pitch_bits[i] = 0;
    1253         132 :                     move16();
    1254             :                 }
    1255             :                 ELSE
    1256             :                 {
    1257      235213 :                     acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1258      235213 :                     move16();
    1259             :                 }
    1260      235345 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1261      235345 :                 move16();
    1262             :             }
    1263             :             ELSE /* L_frame == L_FRAME16k */
    1264             :             {
    1265      321404 :                 test();
    1266      321404 :                 IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) && EQ_16( idchan, 1 ) )
    1267             :                 {
    1268           0 :                     acelp_cfg->pitch_bits[i] = 0;
    1269           0 :                     move16();
    1270             :                 }
    1271             :                 ELSE
    1272             :                 {
    1273      321404 :                     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 ) )];
    1274      321404 :                     move16();
    1275             :                 }
    1276      321404 :                 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 ) )];
    1277      321404 :                 move16();
    1278             :             }
    1279             : 
    1280      556749 :             bits = sub( bits, acelp_cfg->pitch_bits[i] );
    1281             : 
    1282      556749 :             test();
    1283      556749 :             IF( coder_type == INACTIVE && EQ_16( acelp_cfg->gains_mode[i], 6 ) /* VQ vs. SQ threshold @32 kbps */ )
    1284             :             {
    1285        3135 :                 bits = sub( bits, 5 );
    1286             :             }
    1287             :             ELSE
    1288             :             {
    1289      553614 :                 IF( EQ_16( *uc_two_stage_flag, 1 ) )
    1290             :                 {
    1291        5500 :                     acelp_cfg->gains_mode[i] = 7;
    1292        5500 :                     move16();
    1293             :                 }
    1294             : 
    1295      553614 :                 bits = sub( bits, acelp_cfg->gains_mode[i] );
    1296             :             }
    1297             :         }
    1298             : 
    1299      123136 :         test();
    1300      123136 :         test();
    1301      123136 :         test();
    1302      123136 :         test();
    1303      123136 :         test();
    1304      123136 :         test();
    1305             :         /* algebraic codebook bit-budget */
    1306      123136 :         IF( flag_hardcoded /* EVS */ ||
    1307             :             ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1308             :             ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1309             :         {
    1310       76651 :             FOR( i = 0; i < nb_subfr; i++ )
    1311             :             {
    1312       63727 :                 IF( EQ_16( L_frame, L_FRAME ) )
    1313             :                 {
    1314        3193 :                     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 ) )];
    1315        3193 :                     move16();
    1316             :                 }
    1317             :                 ELSE /* L_frame == L_FRAME16k */
    1318             :                 {
    1319       60534 :                     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 ) )];
    1320       60534 :                     move16();
    1321             :                 }
    1322       63727 :                 bits = sub( bits, acelp_cfg->fixed_cdk_index[i] );
    1323             :             }
    1324             :         }
    1325      110212 :         ELSE IF( !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    1326             :         {
    1327      110185 :             test();
    1328      110185 :             IF( EQ_16( coder_type, UNVOICED ) && !( *uc_two_stage_flag ) )
    1329             :             {
    1330           0 :                 i = idiv1616( bits, NB_SUBFR );
    1331           0 :                 IF( s_and( i, 1 ) == 0 )
    1332             :                 {
    1333           0 :                     i = sub( i, 1 ); /* must be odd */
    1334             :                 }
    1335           0 :                 i = s_min( i, 13 );
    1336           0 :                 i = s_max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
    1337           0 :                 set16_fx( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
    1338           0 :                 bits = sub( bits, imult1616( i, NB_SUBFR ) );
    1339             :             }
    1340             :             ELSE
    1341             :             {
    1342      110185 :                 acelp_cfg->fcb_mode = 1;
    1343      110185 :                 move16();
    1344      110185 :                 test();
    1345      110185 :                 test();
    1346      110185 :                 IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    1347             :                 {
    1348        7272 :                     IF( GE_16( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), nb_subfr ) ) ) /* enough bits for all fcb */
    1349             :                     {
    1350        7254 :                         acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1351             :                     }
    1352          18 :                     ELSE IF( GE_16( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 1 ) ) ) )
    1353             :                     {
    1354           0 :                         acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 1 ), L_SUBFR, coder_type, tc_subfr, fix_first );
    1355           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1356           0 :                         move16();
    1357             :                     }
    1358          18 :                     ELSE IF( GE_32( bits, imult1616( ACELP_FIXED_CDK_BITS( 0 ), sub( nb_subfr, 2 ) ) ) )
    1359             :                     {
    1360          18 :                         acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, sub( nb_subfr, 2 ), L_SUBFR, coder_type, tc_subfr, fix_first );
    1361          18 :                         acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
    1362          18 :                         move16();
    1363          18 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1364          18 :                         move16();
    1365          18 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1366          18 :                         move16();
    1367             :                     }
    1368           0 :                     ELSE IF( GE_32( bits, ACELP_FIXED_CDK_BITS( 0 ) ) )
    1369             :                     {
    1370           0 :                         acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1371           0 :                         acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
    1372           0 :                         move16();
    1373           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1374           0 :                         move16();
    1375           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1376           0 :                         move16();
    1377           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1378           0 :                         move16();
    1379             :                     }
    1380             :                     ELSE /* No FCB */
    1381             :                     {
    1382           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1383           0 :                         move16();
    1384           0 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1385           0 :                         move16();
    1386           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1387           0 :                         move16();
    1388           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1389           0 :                         move16();
    1390             :                     }
    1391             :                 }
    1392      102913 :                 ELSE IF( NE_16( element_mode, IVAS_CPE_TD ) && GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) )
    1393             :                 {
    1394           0 :                     bits = 100; /* 9 kbps for fcb */
    1395           0 :                     move16();
    1396           0 :                     acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1397             :                 }
    1398             :                 ELSE
    1399             :                 {
    1400      102913 :                     acelp_FCB_allocator_fx( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1401             :                 }
    1402             :             }
    1403             :         }
    1404             : 
    1405      123136 :         test();
    1406      123136 :         test();
    1407      123136 :         test();
    1408             :         /* AVQ codebook */
    1409      123136 :         IF( ( GE_32( core_brate_inp, MIN_BRATE_AVQ_EXC ) && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1410             :             ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1411             :         {
    1412       72618 :             FOR( i = 0; i < nb_subfr; i++ )
    1413             :             {
    1414       60515 :                 IF( flag_hardcoded )
    1415             :                 {
    1416        3345 :                     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 ) )];
    1417        3345 :                     move16();
    1418             :                     {
    1419        3345 :                         bits = sub( bits, acelp_cfg->AVQ_cdk_bits[i] );
    1420             :                     }
    1421             :                 }
    1422             : 
    1423       60515 :                 bits = sub( bits, G_AVQ_BITS );
    1424             :             }
    1425             : 
    1426       12103 :             test();
    1427       12103 :             test();
    1428       12103 :             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 ) )
    1429             :             {
    1430             :                 /* harm. flag ACELP AVQ */
    1431        6629 :                 bits = sub( bits, 1 );
    1432             :             }
    1433             : 
    1434       12103 :             IF( !flag_hardcoded )
    1435             :             {
    1436             :                 Word16 bit_tmp;
    1437             : 
    1438       11434 :                 bit_tmp = idiv1616( bits, nb_subfr );
    1439       11434 :                 set16_fx( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
    1440       11434 :                 bits = sub( bits, imult1616( bit_tmp, nb_subfr ) );
    1441             : 
    1442       11434 :                 bit_tmp = bits % nb_subfr;
    1443       11434 :                 acelp_cfg->AVQ_cdk_bits[0] = add( acelp_cfg->AVQ_cdk_bits[0], bit_tmp );
    1444       11434 :                 bits = sub( bits, bit_tmp );
    1445             :             }
    1446             :         }
    1447             :     }
    1448       27594 :     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 */ ||
    1449             :              ( ( coder_type == INACTIVE || EQ_16( coder_type, AUDIO ) ) && EQ_16( nb_subfr, NB_SUBFR ) ) /* GSC @12.8kHz */ ||
    1450             :              ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
    1451             :     {
    1452             :         Word32 Local_BR, Pitch_BR;
    1453             :         Word16 Pitch_CT;
    1454             : 
    1455             :         /* as defined at the beginning of [enc,dec]_pit_exc() */
    1456       27594 :         test();
    1457       27594 :         test();
    1458       27594 :         IF( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || GT_32( core_brate, GSC_H_RATE_STG ) ) )
    1459             :         {
    1460         167 :             Local_BR = ACELP_8k00;
    1461         167 :             move32();
    1462         167 :             Pitch_CT = GENERIC;
    1463         167 :             move16();
    1464         167 :             Pitch_BR = ACELP_8k00;
    1465         167 :             move32();
    1466         167 :             IF( EQ_16( L_frame, L_FRAME16k ) )
    1467             :             {
    1468           0 :                 Local_BR = ACELP_14k80;
    1469           0 :                 move32();
    1470           0 :                 test();
    1471           0 :                 if ( GSC_IVAS_mode > 0 && LT_32( core_brate, IVAS_24k4 ) )
    1472             :                 {
    1473           0 :                     Local_BR = ACELP_9k60;
    1474           0 :                     move32();
    1475             :                 }
    1476           0 :                 Pitch_BR = core_brate;
    1477           0 :                 move32();
    1478             :             }
    1479             :         }
    1480       27427 :         ELSE IF( GSC_noisy_speech )
    1481             :         {
    1482         695 :             Local_BR = ACELP_7k20;
    1483         695 :             move32();
    1484         695 :             Pitch_CT = GENERIC;
    1485         695 :             move16();
    1486         695 :             Pitch_BR = ACELP_7k20;
    1487         695 :             move32();
    1488         695 :             if ( EQ_16( L_frame, L_FRAME16k ) )
    1489             :             {
    1490           0 :                 Pitch_BR = core_brate;
    1491           0 :                 move16();
    1492             :             }
    1493             :         }
    1494             :         ELSE
    1495             :         {
    1496       26732 :             Local_BR = ACELP_7k20;
    1497       26732 :             move32();
    1498       26732 :             Pitch_CT = AUDIO;
    1499       26732 :             move16();
    1500       26732 :             Pitch_BR = core_brate;
    1501       26732 :             move32();
    1502             : 
    1503       26732 :             IF( EQ_16( L_frame, L_FRAME16k ) )
    1504             :             {
    1505        5050 :                 Local_BR = ACELP_13k20;
    1506        5050 :                 move32();
    1507        5050 :                 Pitch_CT = GENERIC;
    1508        5050 :                 move16();
    1509             :             }
    1510             :         }
    1511             : 
    1512      143020 :         FOR( i = 0; i < nb_subfr; i++ )
    1513             :         {
    1514      115426 :             IF( EQ_16( L_frame, L_FRAME16k ) )
    1515             :             {
    1516       25250 :                 acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
    1517       25250 :                 move16();
    1518       25250 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    1519       25250 :                 move16();
    1520             :             }
    1521             :             ELSE
    1522             :             {
    1523       90176 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i_mult( i, L_SUBFR ), 0 )];
    1524       90176 :                 move16();
    1525       90176 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    1526       90176 :                 move16();
    1527       90176 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i_mult( i, L_SUBFR ), 0 )];
    1528       90176 :                 move16();
    1529             :             }
    1530             :         }
    1531             :     }
    1532             : 
    1533      150730 :     test();
    1534      150730 :     test();
    1535      150730 :     test();
    1536      150730 :     IF( EQ_16( coder_type, TRANSITION ) && ( EQ_16( tc_call, 1 ) && tc_subfr == 0 && EQ_16( L_frame, L_FRAME ) ) )
    1537             :     {
    1538        3519 :         return error;
    1539             :     }
    1540             : 
    1541             :     /*-----------------------------------------------------------------*
    1542             :      * unused bits handling
    1543             :      *-----------------------------------------------------------------*/
    1544             : 
    1545      147211 :     acelp_cfg->ubits = 0; /* these bits could be reused for something else */
    1546      147211 :     move16();
    1547             : 
    1548      147211 :     test();
    1549      147211 :     IF( flag_hardcoded && NE_32( core_brate, PPP_NELP_2k80 ) )
    1550             :     {
    1551             :         /* unused bits */
    1552        1453 :         test();
    1553        1453 :         test();
    1554        1453 :         IF( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE && LE_32( core_brate, ACELP_24k40 ) ) )
    1555             :         {
    1556          12 :             acelp_cfg->ubits = 0;
    1557          12 :             move16();
    1558             :         }
    1559        1441 :         ELSE IF( EQ_16( L_frame, L_FRAME ) )
    1560             :         {
    1561         753 :             acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
    1562         753 :             move16();
    1563             :         }
    1564             :         ELSE
    1565             :         {
    1566         688 :             acelp_cfg->ubits = 0;
    1567         688 :             move16();
    1568             :         }
    1569             : 
    1570        1453 :         bits = sub( bits, acelp_cfg->ubits );
    1571             :     }
    1572             : 
    1573             :     /* sanity check */
    1574      147211 :     test();
    1575      147211 :     test();
    1576      147211 :     test();
    1577      147211 :     IF( ( coder_type != INACTIVE && EQ_16( nb_subfr, NB_SUBFR ) && NE_16( coder_type, AUDIO ) ) || EQ_16( nb_subfr, NB_SUBFR16k ) )
    1578             :     {
    1579      124643 :         test();
    1580      124643 :         test();
    1581      124643 :         test();
    1582      124643 :         test();
    1583      124643 :         test();
    1584      124643 :         test();
    1585      124643 :         test();
    1586      124643 :         test();
    1587      124643 :         test();
    1588      124643 :         test();
    1589      124643 :         test();
    1590      124643 :         test();
    1591      124643 :         IF( ( EQ_16( L_frame, L_FRAME16k ) && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
    1592             :             ( GSC_IVAS_mode > 0 && EQ_16( L_frame, L_FRAME16k ) ) ) /* IVAS GSC @16kHz */
    1593             :         {
    1594        5050 :             acelp_cfg->ubits = 0;
    1595        5050 :             move16();
    1596             :         }
    1597      119593 :         ELSE IF( flag_hardcoded && core == ACELP_CORE && bits != 0 )
    1598             :         {
    1599             :         }
    1600      119593 :         ELSE IF( bits > 0 && !( EQ_16( coder_type, UNVOICED ) && EQ_16( tdm_low_rate_mode, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) ) )
    1601             :         {
    1602       42177 :             test();
    1603       42177 :             test();
    1604       42177 :             test();
    1605       42177 :             test();
    1606       42177 :             IF( idchan > 0 && EQ_16( element_mode, IVAS_CPE_TD ) )
    1607             :             {
    1608        1754 :                 IF( !tdm_lp_reuse_flag )
    1609             :                 {
    1610        1691 :                     acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
    1611        1691 :                     move16();
    1612        1691 :                     bits = 0;
    1613        1691 :                     move16();
    1614             :                 }
    1615             :                 ELSE
    1616             :                 {
    1617          63 :                     Word16 nb_prm = 4;
    1618          63 :                     move16();
    1619             : 
    1620          63 :                     if ( EQ_16( tdm_low_rate_mode, 1 ) )
    1621             :                     {
    1622           0 :                         nb_prm = 2;
    1623           0 :                         move16();
    1624             :                     }
    1625             :                     /* First add remaining bits on gains */
    1626          63 :                     bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode ) );
    1627             : 
    1628             :                     /* Then, Increase pitch bit budget */
    1629          63 :                     test();
    1630          63 :                     IF( tdm_Pitch_reuse_flag == 0 && bits > 0 )
    1631             :                     {
    1632           0 :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits ) );
    1633             :                     }
    1634             : 
    1635             :                     /* Increase mid-lsf bit budget */
    1636          63 :                     test();
    1637          63 :                     IF( tdm_lp_reuse_flag == 0 && bits > 0 )
    1638             :                     {
    1639           0 :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits ) );
    1640           0 :                         bits = sub( bits, allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits ) );
    1641             :                     }
    1642             :                 }
    1643             :             }
    1644             : 
    1645       40423 :             ELSE IF( core == ACELP_CORE && GE_16( coder_type, UNVOICED ) && LE_16( coder_type, GENERIC ) && EQ_16( L_frame, L_FRAME ) )
    1646             :             {
    1647       22508 :                 acelp_cfg->lsf_bits = add( acelp_cfg->lsf_bits, bits ); /* increase LSF Q bits */
    1648       22508 :                 move16();
    1649             : 
    1650       22508 :                 test();
    1651       22508 :                 IF( GT_16( acelp_cfg->lsf_bits, 46 ) )
    1652             :                 {
    1653           0 :                     acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 46 );
    1654           0 :                     move16();
    1655           0 :                     acelp_cfg->lsf_bits = 46;
    1656           0 :                     move16();
    1657             :                 }
    1658       22508 :                 ELSE IF( GT_16( acelp_cfg->lsf_bits, 42 ) && EQ_16( L_frame, L_FRAME ) )
    1659             :                 {
    1660         113 :                     acelp_cfg->ubits = sub( acelp_cfg->lsf_bits, 42 );
    1661         113 :                     move16();
    1662         113 :                     acelp_cfg->lsf_bits = 42;
    1663         113 :                     move16();
    1664             :                 }
    1665             :             }
    1666             :             ELSE
    1667             :             {
    1668       17915 :                 acelp_cfg->ubits = bits;
    1669       17915 :                 move16();
    1670             :             }
    1671             :         }
    1672             :         /*else if ( bits < 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1673             :         {
    1674             :         }*/
    1675             :     }
    1676             : 
    1677      147211 :     return error;
    1678             : }
    1679             : 
    1680             : /*-------------------------------------------------------------------*
    1681             :  * allocate_unused()
    1682             :  *
    1683             :  * Allocate unused bits
    1684             :  *--------------------------------------------------------------------*/
    1685             : 
    1686         252 : static Word16 allocate_unused(
    1687             :     const Word32 core_brate,
    1688             :     const Word16 coder_type,
    1689             :     const Word16 unused_bits,
    1690             :     const Word16 nb_prm,
    1691             :     const Word16 subfr,
    1692             :     const Word16 prm_type,
    1693             :     Word16 *prm_bit_mode )
    1694             : {
    1695         252 :     Word16 max_bit_per_pos = 0, bit_added = 0;
    1696         252 :     move16();
    1697         252 :     move16();
    1698             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
    1699             : 
    1700         252 :     IF( prm_type == GAINSPRM )
    1701             :     {
    1702         252 :         max_bit_per_pos = 6;
    1703         252 :         move16();
    1704         252 :         IF( GT_32( core_brate, ACELP_8k00 ) )
    1705             :         {
    1706         208 :             max_bit_per_pos = 7;
    1707         208 :             move16();
    1708             :         }
    1709          44 :         ELSE IF( NE_16( coder_type, UNVOICED ) )
    1710             :         {
    1711          44 :             IF( GE_16( subfr, 1 ) )
    1712             :             {
    1713          33 :                 max_bit_per_pos = 7;
    1714          33 :                 move16();
    1715             :             }
    1716          11 :             ELSE IF( subfr == 0 )
    1717             :             {
    1718          11 :                 max_bit_per_pos = 8;
    1719          11 :                 move16();
    1720             :             }
    1721             :         }
    1722           0 :         ELSE IF( EQ_16( coder_type, UNVOICED ) )
    1723             :         {
    1724           0 :             max_bit_per_pos = 9; /* No real limit on UC gain bit budget of the secondary channel */
    1725           0 :             move16();
    1726             :         }
    1727             :     }
    1728           0 :     ELSE IF( EQ_16( prm_type, PITCHPRM ) )
    1729             :     {
    1730           0 :         max_bit_per_pos = 6;
    1731           0 :         move16();
    1732           0 :         test();
    1733           0 :         test();
    1734           0 :         if ( subfr == 0 || EQ_16( subfr, 2 ) || EQ_16( nb_prm, 2 ) )
    1735             :         {
    1736           0 :             max_bit_per_pos = 10;
    1737           0 :             move16();
    1738             :         }
    1739             : 
    1740           0 :         if ( EQ_16( coder_type, UNVOICED ) )
    1741             :         {
    1742           0 :             max_bit_per_pos = 0; /* Should not allocate bits in case of unvoiced coder type */
    1743           0 :             move16();
    1744             :         }
    1745             :     }
    1746           0 :     ELSE IF( EQ_16( prm_type, MID_LSFSPRM ) )
    1747             :     {
    1748           0 :         max_bit_per_pos = 5;
    1749           0 :         move16();
    1750             :     }
    1751           0 :     ELSE IF( EQ_16( prm_type, LSFPRM ) )
    1752             :     {
    1753           0 :         max_bit_per_pos = 42;
    1754           0 :         move16();
    1755             :     }
    1756             :     ELSE
    1757             :     {
    1758             : #ifdef DEBUG_MODE_TD
    1759             :         IVAS_ERROR( IVAS_ERR_WRONG_MODE, "unknown mode in bit_alloc.c" );
    1760             : #endif
    1761             :     }
    1762             : 
    1763         252 :     max_bit_per_pos = s_min( unused_bits, sub( max_bit_per_pos, prm_bit_mode[subfr] ) );
    1764         252 :     test();
    1765         252 :     IF( max_bit_per_pos < 0 )
    1766             :     {
    1767           0 :         return 0;
    1768             :     }
    1769         252 :     ELSE IF( max_bit_per_pos >= 0 && EQ_16( subfr, sub( nb_prm, 1 ) ) )
    1770             :     {
    1771          63 :         prm_bit_mode[subfr] = add( prm_bit_mode[subfr], max_bit_per_pos );
    1772          63 :         move16();
    1773             :     }
    1774             :     ELSE
    1775             :     {
    1776         189 :         prm_bit_mode[subfr] = add( prm_bit_mode[subfr], max_bit_per_pos );
    1777         189 :         move16();
    1778         189 :         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] ) );
    1779             :     }
    1780             : 
    1781         252 :     return add( bit_added, max_bit_per_pos );
    1782             : }
    1783             : 
    1784             : 
    1785             : /*-------------------------------------------------------------------*
    1786             :  * set_ACELP_flag()
    1787             :  *
    1788             :  * set ACELP@16kHz flag
    1789             :  *--------------------------------------------------------------------*/
    1790             : 
    1791             : /*! r: ACELP16k flag */
    1792       10333 : Word16 set_ACELP_flag(
    1793             :     const Word16 element_mode,  /* i  : element mode                */
    1794             :     const Word32 element_brate, /* i  : element bitrate             */
    1795             :     const Word32 total_brate,   /* i  : total bitrate per channel   */
    1796             :     const Word16 idchan,        /* i  : Channel id                  */
    1797             :     const Word16 tdm_LRTD_flag, /* i  : LRTD stereo mode flag       */
    1798             :     const Word16 bwidth,        /* i  : audio bandwidth             */
    1799             :     const Word16 cng_type       /* i  : CNG type                    */
    1800             : )
    1801             : {
    1802             :     // PMT("Not floating point computation, but fixed point operator are still missing ")
    1803       10333 :     test();
    1804       10333 :     test();
    1805       10333 :     test();
    1806       10333 :     test();
    1807       10333 :     IF( EQ_16( element_mode, IVAS_CPE_DFT ) && ( idchan == 0 ) && LE_32( total_brate, SID_2k40 ) && EQ_16( bwidth, WB ) && ( cng_type == LP_CNG ) )
    1808             :     {
    1809           0 :         return 1;
    1810             :     }
    1811       10333 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    1812             :     {
    1813           0 :         test();
    1814           0 :         test();
    1815           0 :         test();
    1816           0 :         IF( GE_32( element_brate, IVAS_24k4 ) && ( idchan == 0 ) && ( ( tdm_LRTD_flag == 0 ) || GT_32( element_brate, IVAS_24k4 ) ) )
    1817             :         {
    1818           0 :             return 1;
    1819             :         }
    1820             :         ELSE
    1821             :         {
    1822           0 :             return 0;
    1823             :         }
    1824             :     }
    1825       10333 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
    1826             :     {
    1827         194 :         IF( GE_32( element_brate, IVAS_24k4 ) )
    1828             :         {
    1829           0 :             return 1;
    1830             :         }
    1831             :         ELSE
    1832             :         {
    1833         194 :             return 0;
    1834             :         }
    1835             :     }
    1836       10139 :     ELSE IF( EQ_16( element_mode, IVAS_SCE ) )
    1837             :     {
    1838         983 :         IF( GE_32( element_brate, SCE_CORE_16k_LOW_LIMIT ) )
    1839             :         {
    1840           0 :             return 1;
    1841             :         }
    1842             :         ELSE
    1843             :         {
    1844         983 :             return 0;
    1845             :         }
    1846             :     }
    1847        9156 :     ELSE IF( GE_32( total_brate, ACELP_16k_LOW_LIMIT ) ) /* EVS_MONO */
    1848             :     {
    1849        2050 :         return 1;
    1850             :     }
    1851             :     ELSE
    1852             :     {
    1853        7106 :         return 0;
    1854             :     }
    1855             : }
    1856             : 
    1857             : /*-------------------------------------------------------------------*
    1858             :  * set_ACELP_flag_IVAS()
    1859             :  *
    1860             :  * set ACELP@16kHz flag
    1861             :  *--------------------------------------------------------------------*/
    1862             : 
    1863             : /*! r: ACELP16k flag */
    1864     1022832 : Word16 set_ACELP_flag_IVAS(
    1865             :     const Word16 element_mode,  /* i  : element mode                */
    1866             :     const Word32 element_brate, /* i  : element bitrate             */
    1867             :     const Word32 total_brate,   /* i  : total bitrate per channel   */
    1868             :     const Word16 idchan,        /* i  : Channel id                  */
    1869             :     const Word16 tdm_LRTD_flag, /* i  : LRTD stereo mode flag       */
    1870             :     const Word16 bwidth,        /* i  : audio bandwidth             */
    1871             :     const Word16 cng_type       /* i  : CNG type                    */
    1872             : )
    1873             : {
    1874     1022832 :     test();
    1875     1022832 :     test();
    1876     1022832 :     test();
    1877     1022832 :     test();
    1878     1022832 :     IF( EQ_16( element_mode, IVAS_CPE_DFT ) && ( idchan == 0 ) && LE_32( total_brate, SID_2k40 ) && EQ_16( bwidth, WB ) && ( cng_type == LP_CNG ) )
    1879             :     {
    1880         366 :         return 1;
    1881             :     }
    1882     1022466 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_TD ) )
    1883             :     {
    1884        7502 :         test();
    1885        7502 :         test();
    1886        7502 :         test();
    1887        7502 :         IF( GE_32( element_brate, IVAS_24k4 ) && ( idchan == 0 ) && ( ( tdm_LRTD_flag == 0 ) || GT_32( element_brate, IVAS_24k4 ) ) )
    1888             :         {
    1889        2082 :             return 1;
    1890             :         }
    1891             :         ELSE
    1892             :         {
    1893        5420 :             return 0;
    1894             :         }
    1895             :     }
    1896     1014964 :     ELSE IF( EQ_16( element_mode, IVAS_CPE_DFT ) )
    1897             :     {
    1898       58681 :         IF( GE_32( element_brate, IVAS_24k4 ) )
    1899             :         {
    1900       26889 :             return 1;
    1901             :         }
    1902             :         ELSE
    1903             :         {
    1904       31792 :             return 0;
    1905             :         }
    1906             :     }
    1907      956283 :     ELSE IF( EQ_16( element_mode, IVAS_SCE ) )
    1908             :     {
    1909      308799 :         IF( GE_32( element_brate, SCE_CORE_16k_LOW_LIMIT ) )
    1910             :         {
    1911      217932 :             return 1;
    1912             :         }
    1913             :         ELSE
    1914             :         {
    1915       90867 :             return 0;
    1916             :         }
    1917             :     }
    1918      647484 :     ELSE IF( GE_32( total_brate, ACELP_16k_LOW_LIMIT ) ) /* EVS_MONO */
    1919             :     {
    1920      639140 :         return 1;
    1921             :     }
    1922             :     ELSE
    1923             :     {
    1924        8344 :         return 0;
    1925             :     }
    1926             : }

Generated by: LCOV version 1.14