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

Generated by: LCOV version 1.14