LCOV - code coverage report
Current view: top level - lib_com - bits_alloc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 447 549 81.4 %
Date: 2025-05-23 08:37:30 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include "rom_com.h"
      43             : #include "prot.h"
      44             : #include "ivas_cnst.h"
      45             : #include "ivas_rom_com.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * Local function prototypes
      50             :  *--------------------------------------------------------------------*/
      51             : 
      52             : static int16_t BITS_ALLOC_adjust_acelp_fixed_cdk( const int16_t bits_frame, int16_t *fixed_cdk_index, const int16_t nb_subfr );
      53             : 
      54             : static int16_t allocate_unused( const int32_t core_brate, const int16_t coder_type, const int16_t unused_bits, const int16_t nb_prm, const int16_t subfr, const int16_t prm_type, int16_t *prm_bit_mode );
      55             : 
      56             : typedef enum
      57             : {
      58             :     GAINSPRM = 0x0,
      59             :     PITCHPRM = 0x1,
      60             :     MID_LSFSPRM = 0x3,
      61             :     LSFPRM = 0x4
      62             : } PRM_TYPES;
      63             : 
      64             : /*-------------------------------------------------------------------*
      65             :  * BITS_ALLOC_init_config_acelp()
      66             :  *
      67             :  * initial configuration for Mode 2 ACELP
      68             :  *--------------------------------------------------------------------*/
      69             : 
      70      115786 : void BITS_ALLOC_init_config_acelp(
      71             :     const int32_t bit_rate,
      72             :     const int16_t narrowBand,
      73             :     const int16_t nb_subfr,
      74             :     ACELP_config *acelp_cfg /* o  :  configuration structure of ACELP */
      75             : )
      76             : {
      77             :     int16_t rate_mode_index;
      78             : 
      79      115786 :     if ( bit_rate <= ACELP_9k60 )
      80             :     {
      81        3536 :         rate_mode_index = 0;
      82             :     }
      83             :     else
      84             :     {
      85      112250 :         rate_mode_index = 1;
      86             :     }
      87             : 
      88      115786 :     acelp_cfg->mode_index = rate_mode_index;
      89             : 
      90             :     /*LPC: midLpc should be swithced off?*/
      91      115786 :     acelp_cfg->midLpc_enable = 1;
      92             : 
      93             :     /*ACELP ICB config*/
      94      115786 :     if ( ( rate_mode_index == 0 ) || ( narrowBand == 1 ) )
      95             :     {
      96        3536 :         acelp_cfg->pre_emphasis = 1;
      97        3536 :         acelp_cfg->formant_enh = 1;
      98        3536 :         acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
      99        3536 :         acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
     100        3536 :         acelp_cfg->formant_tilt = 0;
     101        3536 :         acelp_cfg->voice_tilt = 0;
     102             :     }
     103             :     else
     104             :     {
     105      112250 :         acelp_cfg->pre_emphasis = 0;
     106      112250 :         acelp_cfg->formant_enh = 1;
     107      112250 :         acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
     108      112250 :         acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
     109      112250 :         acelp_cfg->formant_tilt = 1;
     110      112250 :         acelp_cfg->voice_tilt = 1;
     111             :     }
     112             : 
     113             :     /*Wide band @ 16kHz*/
     114      115786 :     if ( nb_subfr == NB_SUBFR16k )
     115             :     {
     116       52417 :         acelp_cfg->pre_emphasis = 1;
     117       52417 :         acelp_cfg->formant_enh = 1;
     118       52417 :         acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     119       52417 :         acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2_16k;
     120       52417 :         acelp_cfg->formant_tilt = 0;
     121       52417 :         acelp_cfg->voice_tilt = 2;
     122             :     }
     123             : 
     124      115786 :     return;
     125             : }
     126             : 
     127             : /*-------------------------------------------------------------------*
     128             :  * BITS_ALLOC_config_acelp()
     129             :  *
     130             :  * configure all Mode 2 ACELP coder types and allocate the bits
     131             :  *--------------------------------------------------------------------*/
     132             : 
     133        2416 : int16_t BITS_ALLOC_config_acelp(
     134             :     const int16_t bits_frame, /* i  : remaining bit budget for the frame  */
     135             :     const int16_t coder_type, /* i  : acelp coder type                    */
     136             :     ACELP_config *acelp_cfg,  /* i/o: configuration structure of ACELP    */
     137             :     const int16_t narrowBand, /* i  : narrowband flag                     */
     138             :     const int16_t nb_subfr    /* i  : number of subframes                 */
     139             : )
     140             : {
     141             :     int16_t mode_index;
     142             :     int16_t band_index;
     143             :     int16_t i;
     144             :     int16_t remaining_bits, bits;
     145             : 
     146             :     /*Sanity check*/
     147             : 
     148        2416 :     mode_index = acelp_cfg->mode_index;
     149        2416 :     band_index = ( narrowBand == 0 );
     150        2416 :     bits = 0;
     151             : 
     152        2416 :     if ( band_index == 0 )
     153             :     {
     154           0 :         if ( coder_type == INACTIVE )
     155             :         {
     156           0 :             acelp_cfg->formant_enh = 0;
     157             :         }
     158             :         else
     159             :         {
     160           0 :             acelp_cfg->formant_enh = 1;
     161             :         }
     162             :     }
     163             : 
     164        2416 :     if ( band_index == 1 && nb_subfr == NB_SUBFR )
     165             :     {
     166             : 
     167           0 :         if ( coder_type == INACTIVE )
     168             :         {
     169           0 :             acelp_cfg->pre_emphasis = 0;
     170           0 :             acelp_cfg->formant_enh = 0;
     171           0 :             acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     172           0 :             acelp_cfg->formant_tilt = 1;
     173           0 :             acelp_cfg->voice_tilt = 1;
     174             :         }
     175             :         else
     176             :         {
     177           0 :             acelp_cfg->pre_emphasis = 1;
     178           0 :             acelp_cfg->formant_enh = 1;
     179           0 :             acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
     180           0 :             acelp_cfg->formant_tilt = 0;
     181           0 :             acelp_cfg->voice_tilt = 0;
     182             :         }
     183             :     }
     184             : 
     185        2416 :     if ( coder_type == UNVOICED )
     186             :     {
     187         104 :         if ( ACELP_GAINS_MODE[mode_index][band_index][coder_type] == 6 )
     188             :         {
     189           0 :             acelp_cfg->pitch_sharpening = 0;
     190           0 :             acelp_cfg->phase_scrambling = 1;
     191             :         }
     192             :         else
     193             :         {
     194         104 :             acelp_cfg->pitch_sharpening = 0;
     195         104 :             acelp_cfg->phase_scrambling = 0;
     196             :         }
     197             :     }
     198             :     else
     199             :     {
     200        2312 :         acelp_cfg->pitch_sharpening = 1;
     201        2312 :         acelp_cfg->phase_scrambling = 0;
     202             :     }
     203             : 
     204        2416 :     if ( coder_type > ACELP_MODE_MAX )
     205             :     {
     206             :         /* keep pitch sharpening for RF_ALLPRED mode */
     207           0 :         acelp_cfg->pitch_sharpening = 0;
     208           0 :         acelp_cfg->phase_scrambling = 0;
     209             :     }
     210             : 
     211             :     /*Allocate bits and different modes*/
     212        2416 :     acelp_cfg->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
     213        2416 :     bits += ACELP_BPF_BITS[acelp_cfg->bpf_mode];
     214             : 
     215        2416 :     acelp_cfg->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
     216        2416 :     acelp_cfg->nrg_bits = ACELP_NRG_BITS[acelp_cfg->nrg_mode];
     217        2416 :     bits += acelp_cfg->nrg_bits;
     218             : 
     219        2416 :     acelp_cfg->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
     220        2416 :     acelp_cfg->ltp_bits = 0;
     221        2416 :     acelp_cfg->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
     222        2416 :     acelp_cfg->ltf_bits = ACELP_LTF_BITS[acelp_cfg->ltf_mode];
     223             : 
     224        2416 :     if ( nb_subfr == NB_SUBFR16k && acelp_cfg->ltf_bits == 4 )
     225             :     {
     226        1072 :         acelp_cfg->ltf_bits++;
     227             :     }
     228        2416 :     bits += acelp_cfg->ltf_bits;
     229             : 
     230             : 
     231       14496 :     for ( i = 0; i < nb_subfr; i++ )
     232             :     {
     233       12080 :         acelp_cfg->gains_mode[i] = ACELP_GAINS_MODE[mode_index][band_index][coder_type];
     234             : 
     235             :         /* skip subframe 1, 3 gain encoding, and use from subframe 0, and 3, respectively */
     236       12080 :         if ( coder_type >= ACELP_MODE_MAX && ( i == 1 || i == 3 ) )
     237             :         {
     238           0 :             acelp_cfg->gains_mode[i] = 0;
     239             :         }
     240             : 
     241       12080 :         bits += ACELP_GAINS_BITS[acelp_cfg->gains_mode[i]];
     242       12080 :         bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
     243       12080 :         acelp_cfg->ltp_bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
     244             :     }
     245             : 
     246             :     /*Innovation*/
     247        2416 :     if ( bits_frame < bits )
     248             :     {
     249           0 :         printf( "\nWarning: bits per frame too low\n" );
     250           0 :         return -1;
     251             :     }
     252             : 
     253        2416 :     if ( coder_type == RF_ALLPRED )
     254             :     {
     255           0 :         set_s( acelp_cfg->fixed_cdk_index, -1, nb_subfr );
     256             :     }
     257        2416 :     else if ( coder_type == RF_GENPRED )
     258             :     {
     259           0 :         acelp_cfg->fixed_cdk_index[0] = 0; /* 7 bits */
     260           0 :         acelp_cfg->fixed_cdk_index[1] = -1;
     261           0 :         acelp_cfg->fixed_cdk_index[2] = 0; /* 7 bits */
     262           0 :         acelp_cfg->fixed_cdk_index[3] = -1;
     263           0 :         acelp_cfg->fixed_cdk_index[4] = -1;
     264           0 :         bits += 14;
     265             :     }
     266        2416 :     else if ( coder_type == RF_NOPRED )
     267             :     {
     268           0 :         set_s( acelp_cfg->fixed_cdk_index, 0, nb_subfr );
     269           0 :         bits += 28;
     270             :     }
     271             :     else
     272             :     {
     273        2416 :         bits += BITS_ALLOC_adjust_acelp_fixed_cdk( bits_frame - bits, acelp_cfg->fixed_cdk_index, nb_subfr );
     274             :     }
     275             : 
     276        2416 :     remaining_bits = bits_frame - bits;
     277             : 
     278             :     /*Sanity check*/
     279        2416 :     if ( remaining_bits < 0 )
     280             :     {
     281           0 :         bits = -1;
     282             :     }
     283             : 
     284             : 
     285        2416 :     return ( bits );
     286             : }
     287             : 
     288             : /*-------------------------------------------------------------------*
     289             :  * BITS_ALLOC_adjust_acelp_fixed_cdk()
     290             :  *
     291             :  *
     292             :  *--------------------------------------------------------------------*/
     293             : 
     294        2416 : static int16_t BITS_ALLOC_adjust_acelp_fixed_cdk(
     295             :     const int16_t bits_frame, /* i  : bit budget */
     296             :     int16_t *fixed_cdk_index,
     297             :     const int16_t nb_subfr )
     298             : {
     299             :     int16_t bits_subframe2;
     300             :     int16_t sfr, k, bitsused, bits_currsubframe;
     301             : 
     302        2416 :     bits_subframe2 = bits_frame;
     303             : 
     304        2416 :     if ( bits_subframe2 < ACELP_FIXED_CDK_BITS( 0 ) * nb_subfr )
     305             :     {
     306           0 :         return ( bits_frame + 1 ); /* Not enough bits for lowest mode. -> trigger alarm*/
     307             :     }
     308             : 
     309             :     /* search cdk-index for first subframe */
     310       64716 :     for ( k = 0; k < ACELP_FIXED_CDK_NB - 1; k++ )
     311             :     {
     312       64716 :         if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
     313             :         {
     314        2416 :             k--; /* previous mode did not exceed bit-budget */
     315        2416 :             break;
     316             :         }
     317             :     }
     318             : 
     319        2416 :     if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
     320             :     {
     321           0 :         k--; /* previous mode did not exceed bit-budget */
     322             :     }
     323        2416 :     fixed_cdk_index[0] = k;
     324             : 
     325        2416 :     bitsused = ACELP_FIXED_CDK_BITS( k );
     326             : 
     327       12080 :     for ( sfr = 1; sfr < nb_subfr; sfr++ )
     328             :     {
     329        9664 :         bits_currsubframe = ( sfr * bits_subframe2 + bits_subframe2 ) - bitsused * nb_subfr;
     330             : 
     331             :         /* try increasing mode while below threshold */
     332       11232 :         while ( ( k < ACELP_FIXED_CDK_NB - 1 ) && ( ACELP_FIXED_CDK_BITS( k + 1 ) * nb_subfr <= bits_currsubframe ) )
     333             :         {
     334        1568 :             k++;
     335             :         }
     336             : 
     337             :         /* try decreasing mode until below threshold */
     338       10716 :         while ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_currsubframe )
     339             :         {
     340        1052 :             k--;
     341        1052 :             if ( k == 0 )
     342             :             {
     343           0 :                 break;
     344             :             }
     345             :         }
     346             : 
     347             :         /* store mode */
     348        9664 :         fixed_cdk_index[sfr] = k;
     349        9664 :         bitsused += ACELP_FIXED_CDK_BITS( k );
     350             :     }
     351             : 
     352        2416 :     return bitsused;
     353             : }
     354             : 
     355             : 
     356             : /*-------------------------------------------------------------------*
     357             :  * fcb_table()
     358             :  *
     359             :  * Selection of fixed innovation codebook bitbudget table
     360             :  *--------------------------------------------------------------------*/
     361             : 
     362     9473743 : static int16_t fcb_table(
     363             :     const int16_t n,
     364             :     const int16_t L_subfr )
     365             : {
     366             :     int16_t out;
     367             : 
     368     9473743 :     out = PulseConfTable[n].bits;
     369     9473743 :     if ( L_subfr > L_SUBFR )
     370             :     {
     371           0 :         out = fast_FCB_bits_2sfr[n];
     372             :     }
     373             : 
     374     9473743 :     return ( out );
     375             : }
     376             : 
     377             : /*-------------------------------------------------------------------*
     378             :  * acelp_FCB_allocator()
     379             :  *
     380             :  * Routine to allocate fixed innovation codebook bit-budget
     381             :  *--------------------------------------------------------------------*/
     382             : 
     383      484673 : static ivas_error acelp_FCB_allocator(
     384             :     int16_t *nBits,            /* i/o: available bit-budget    */
     385             :     int16_t fixed_cdk_index[], /* o  : codebook index          */
     386             :     int16_t nb_subfr,          /* i  : number of subframes     */
     387             :     const int16_t L_subfr,     /* i  : subframe length         */
     388             :     const int16_t coder_type,  /* i  : coder type              */
     389             :     const int16_t tc_subfr,    /* i  : TC subframe index       */
     390             :     const int16_t fix_first    /* i  : flag to indicate whether the first subframe bit-budget was fixed */
     391             : )
     392             : {
     393             :     int16_t cdbk, sfr, step;
     394             :     int16_t nBits_tmp;
     395             :     int16_t *p_fixed_cdk_index;
     396             :     ivas_error error;
     397             :     int16_t max_n;
     398             : 
     399      484673 :     error = IVAS_ERR_OK;
     400             : 
     401      484673 :     cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
     402             : 
     403      484673 :     p_fixed_cdk_index = fixed_cdk_index;
     404             : 
     405             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse not in the first subframe */
     406      484673 :     if ( tc_subfr >= L_SUBFR && fix_first )
     407             :     {
     408             :         int16_t i;
     409             : 
     410        8575 :         for ( i = 0; i < nb_subfr; i++ )
     411             :         {
     412        6860 :             *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] );
     413             :         }
     414        1715 :         return error;
     415             :     }
     416             : 
     417             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
     418      482958 :     sfr = 0;
     419      482958 :     if ( fix_first )
     420             :     {
     421       15530 :         *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] );
     422       15530 :         sfr = 1;
     423       15530 :         p_fixed_cdk_index++;
     424       15530 :         nb_subfr = 3;
     425             :     }
     426             : 
     427             :     /* distribute the bit-budget equally between subframes */
     428      482958 :     if ( L_subfr > L_SUBFR ) /* access fast_FCB_bits_2sfr */
     429             :     {
     430           0 :         max_n = 6;
     431             :     }
     432             :     else
     433             :     {
     434      482958 :         max_n = ACELP_FIXED_CDK_NB;
     435             :     }
     436     7541911 :     for ( cdbk = 0; cdbk < max_n; cdbk++ )
     437             :     {
     438     7541911 :         if ( fcb_table( cdbk, L_subfr ) * nb_subfr > *nBits )
     439             :         {
     440      482958 :             break;
     441             :         }
     442             :     }
     443      482958 :     cdbk--;
     444             : #ifdef DEBUGGING
     445             :     if ( cdbk < 0 && coder_type != TRANSITION )
     446             :     {
     447             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bit-budget for fixed innovation codebook (frame = %d). Exiting! \n", frame );
     448             :     }
     449             :     if ( ( L_subfr == L_SUBFR && cdbk >= ACELP_FIXED_CDK_NB ) || ( L_subfr == 2 * L_SUBFR && fcb_table( cdbk, L_subfr ) == 128 /*stop value*/ ) )
     450             :     {
     451             :         return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too high bit-budget for fixed innovation codebook (frame = %d). Exiting! \n", frame );
     452             :     }
     453             : #endif
     454             : 
     455      482958 :     set_s( p_fixed_cdk_index, cdbk, nb_subfr );
     456      482958 :     nBits_tmp = 0;
     457      482958 :     if ( cdbk >= 0 )
     458             :     {
     459      482958 :         nBits_tmp = fcb_table( cdbk, L_subfr );
     460             :     }
     461             :     else
     462             :     {
     463           0 :         nBits_tmp = 0;
     464             :     }
     465      482958 :     *nBits -= nBits_tmp * nb_subfr;
     466             : 
     467             :     /* try to increase the FCB bit-budget of the first subframe(s) */
     468      482958 :     if ( cdbk < ACELP_FIXED_CDK_NB - 1 )
     469             :     {
     470      482958 :         step = fcb_table( cdbk + 1, L_subfr ) - nBits_tmp;
     471     1295999 :         while ( *nBits >= step )
     472             :         {
     473      813041 :             ( *p_fixed_cdk_index )++;
     474      813041 :             *nBits -= step;
     475      813041 :             p_fixed_cdk_index++;
     476             :         }
     477             : 
     478             :         /* try to increase the FCB of the first subframe in cases when the next step is lower than the current step */
     479      482958 :         if ( fixed_cdk_index[sfr] < ACELP_FIXED_CDK_NB - 1 )
     480             :         {
     481      482958 :             step = fcb_table( fixed_cdk_index[sfr] + 1, L_subfr ) - fcb_table( fixed_cdk_index[sfr], L_subfr );
     482      482958 :             if ( *nBits >= step && cdbk >= 0 )
     483             :             {
     484       71420 :                 fixed_cdk_index[sfr]++;
     485       71420 :                 *nBits -= step;
     486             : 
     487       71420 :                 if ( *nBits >= step && fixed_cdk_index[sfr + 1] == fixed_cdk_index[sfr] - 1 )
     488             :                 {
     489        6040 :                     sfr++;
     490        6040 :                     fixed_cdk_index[sfr]++;
     491        6040 :                     *nBits -= step;
     492             :                 }
     493             :             }
     494             :         }
     495             :     }
     496             :     /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
     497      482958 :     if ( tc_subfr >= L_SUBFR )
     498             :     {
     499             :         int16_t tempr;
     500             : 
     501       26239 :         SWAP( fixed_cdk_index[0], fixed_cdk_index[tc_subfr / L_SUBFR] );
     502             : 
     503             :         /* TRANSITION coding: allocate second highest FCBQ bit-budget to the last subframe */
     504       26239 :         if ( tc_subfr / L_SUBFR < nb_subfr - 1 )
     505             :         {
     506       12673 :             SWAP( fixed_cdk_index[( tc_subfr - L_SUBFR ) / L_SUBFR], fixed_cdk_index[nb_subfr - 1] );
     507             :         }
     508             :     }
     509             : 
     510             :     /* when subframe length > L_SUBFR, number of bits instead of codebook index is signalled */
     511      482958 :     if ( L_subfr > L_SUBFR )
     512             :     {
     513             :         int16_t i, j;
     514           0 :         for ( i = 0; i < nb_subfr; i++ )
     515             :         {
     516           0 :             j = fixed_cdk_index[i];
     517           0 :             fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
     518             :         }
     519             :     }
     520             : 
     521      482958 :     return error;
     522             : }
     523             : 
     524             : 
     525             : /*-------------------------------------------------------------------*
     526             :  * config_acelp1()
     527             :  *
     528             :  * Configure ACELP bit allocation
     529             :  *   - should be in range of <6700; 24350> for ACELP@12.8kHz
     530             :  *   - per channel bitrate minimum is 13250 kbps for ACELP@16kHz
     531             :  *--------------------------------------------------------------------*/
     532             : 
     533      696637 : ivas_error config_acelp1(
     534             :     const int16_t enc_dec,                  /* i  : encoder/decoder flag            */
     535             :     const int32_t total_brate,              /* i  : total bitrate                   */
     536             :     const int32_t core_brate_inp,           /* i  : core bitrate                    */
     537             :     const int16_t core,                     /* i  : core                            */
     538             :     const int16_t extl,                     /* i  : extension layer                 */
     539             :     const int32_t extl_brate,               /* i  : extension layer bitrate         */
     540             :     const int16_t L_frame,                  /* i  : frame length at internal Fs     */
     541             :     const int16_t GSC_noisy_speech,         /* i  : GSC on SWB noisy speech flag    */
     542             :     ACELP_config *acelp_cfg,                /* i  : ACELP bit-allocation            */
     543             :     const int16_t signaling_bits,           /* i  : number of signaling bits        */
     544             :     const int16_t coder_type,               /* i  : coder type                      */
     545             :     const int16_t inactive_coder_type_flag, /* i  : AVQ (0) or GSC (1) IC flag      */
     546             :     const int16_t tc_subfr,                 /* i  : TC subfr ID                     */
     547             :     const int16_t tc_call,                  /* i  : TC call number (0,1,2,3,5(DEC)) */
     548             :     int16_t *nBits_es_Pred,                 /* o  : number of bits for Es_pred Q    */
     549             :     int16_t *unbits,                        /* o  : number of unused bits           */
     550             :     const int16_t element_mode,             /* i  : element mode                    */
     551             :     int16_t *uc_two_stage_flag,             /* o  : flag undicating two-stage UC    */
     552             :     const int16_t tdm_lp_reuse_flag,        /* i  : LPC reuse flag (can be 1 only with secondary channel */
     553             :     const int16_t tdm_low_rate_mode,        /* i  : secondary channel low rate mode flag */
     554             :     const int16_t idchan,                   /* i  : stereo channel ID               */
     555             :     const int16_t active_cnt,               /* i  : Active frame counter            */
     556             :     const int16_t tdm_Pitch_reuse_flag,     /* i  : primary channel pitch reuse flag*/
     557             :     const int16_t tdm_LRTD_flag,            /* i  : LRTD stereo mode flag           */
     558             :     const int16_t GSC_IVAS_mode             /* i  : GSC IVAS mode                   */
     559             : )
     560             : {
     561             :     int16_t i, bits, nb_subfr;
     562             :     int16_t flag_hardcoded, coder_type_sw, fix_first;
     563             :     int32_t core_brate;
     564             : #ifdef DEBUGGING
     565             :     int32_t core_brate_inpI = core_brate_inp;
     566             : #endif
     567             :     ivas_error error;
     568             : 
     569      696637 :     error = IVAS_ERR_OK;
     570             : 
     571             :     /*-----------------------------------------------------------------*
     572             :      * Set the flag indicating two-stage Unvoiced (UC) frame
     573             :      *-----------------------------------------------------------------*/
     574             : 
     575      696637 :     *uc_two_stage_flag = 0;
     576      696637 :     if ( coder_type == UNVOICED )
     577             :     {
     578        8145 :         if ( total_brate >= MIN_UNVOICED_TWO_STAGE_BRATE && element_mode > EVS_MONO && ( idchan == 0 || ( ( total_brate >= 8500 || extl_brate == 0 ) && tdm_LRTD_flag == 1 ) ) )
     579             :         {
     580        8041 :             *uc_two_stage_flag = 1;
     581             :         }
     582             :     }
     583             : 
     584             :     /*-----------------------------------------------------------------*
     585             :      * Set the number of subframes
     586             :      *-----------------------------------------------------------------*/
     587             : 
     588      696637 :     if ( L_frame == L_FRAME )
     589             :     {
     590      392824 :         nb_subfr = NB_SUBFR;
     591             : 
     592             : #ifdef DEBUGGING
     593             :         if ( ( ( core_brate_inp < 5900 && coder_type > UNVOICED ) && !( core_brate_inp < MIN_TC_BRATE && coder_type == TRANSITION ) ) && !( idchan > 0 && element_mode == IVAS_CPE_TD ) && !( element_mode == IVAS_SCE && tdm_low_rate_mode ) )
     594             :         {
     595             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bitrate (%d bps) for ACELP@12k8 in frame %d. Exiting!\n", core_brate_inpI, frame );
     596             :         }
     597             : 
     598             :         if ( core_brate_inp > ACELP_12k8_HIGH_LIMIT && core == ACELP_CORE )
     599             :         {
     600             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too high bitrate (%d bps) for ACELP@12k8 in frame %d. Exiting!\n", core_brate_inpI, frame );
     601             :         }
     602             : #endif
     603             :     }
     604             :     else /* L_frame == L_FRAME16k */
     605             :     {
     606      303813 :         nb_subfr = NB_SUBFR16k;
     607             : 
     608             : #ifdef DEBUGGING
     609             :         if ( core_brate_inp < ACELP_16k_LOW_LIMIT && core == ACELP_CORE )
     610             :         {
     611             :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Too low bitrate (%d bps) for ACELP@16k in frame %d. Exiting!\n", core_brate_inpI, frame );
     612             :         }
     613             : #endif
     614             :     }
     615             : 
     616      696637 :     coder_type_sw = coder_type;
     617      696637 :     if ( core != ACELP_CORE )
     618             :     {
     619             :         /* used in acelp_core_switch_enc() */
     620         100 :         nb_subfr = 1;
     621         100 :         if ( L_frame == L_FRAME )
     622             :         {
     623          20 :             coder_type_sw = TRANSITION;
     624             :         }
     625             :     }
     626             : 
     627             :     /*-----------------------------------------------------------------*
     628             :      * Check if the core_brate is hard coded (to keep BE for mono core) or not
     629             :      *-----------------------------------------------------------------*/
     630             : 
     631      696637 :     flag_hardcoded = 0;
     632      696637 :     i = 0;
     633             : 
     634     6483167 :     while ( i < SIZE_BRATE_INTERMED_TBL )
     635             :     {
     636     6483167 :         if ( core_brate_inp == brate_intermed_tbl[i] )
     637             :         {
     638       29121 :             flag_hardcoded = 1;
     639       29121 :             break;
     640             :         }
     641             : 
     642     6454046 :         if ( core_brate_inp < brate_intermed_tbl[i] )
     643             :         {
     644      667516 :             flag_hardcoded = 0;
     645      667516 :             break;
     646             :         }
     647             : 
     648     5786530 :         i++;
     649             :     }
     650             : 
     651      696637 :     if ( element_mode == IVAS_CPE_TD && coder_type == AUDIO &&
     652         105 :          core_brate_inp <= STEREO_GSC_BIT_RATE_ALLOC && brate_intermed_tbl[i] == ACELP_9k60 ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
     653             :     {
     654          14 :         i--;
     655             :     }
     656             : 
     657      696637 :     core_brate = brate_intermed_tbl[i];
     658             : 
     659      696637 :     if ( element_mode > EVS_MONO )
     660             :     {
     661      690510 :         flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
     662             :     }
     663             : 
     664      696637 :     if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
     665             :     {
     666         100 :         flag_hardcoded = 1;
     667             :     }
     668             : 
     669             :     /*-----------------------------------------------------------------*
     670             :      * ACELP bit allocation
     671             :      *-----------------------------------------------------------------*/
     672             : 
     673      696637 :     if ( !( coder_type == TRANSITION && tc_subfr != -1 ) || enc_dec == DEC )
     674             :     {
     675             :         /* Set the bit-budget */
     676      674795 :         bits = (int16_t) ( core_brate_inp / FRAMES_PER_SEC );
     677             : 
     678      674795 :         if ( coder_type == TRANSITION && enc_dec == DEC && tc_call == 1 )
     679             :         {
     680       41832 :             bits += *nBits_es_Pred; /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
     681             :         }
     682             : 
     683             :         /* Subtract signaling bits */
     684      674795 :         if ( enc_dec == DEC && idchan == 1 && element_mode > EVS_MONO )
     685             :         {
     686       10956 :             bits -= TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS;
     687             : 
     688       10956 :             if ( tdm_LRTD_flag == 1 )
     689             :             {
     690       10602 :                 bits += STEREO_BITS_TCA;
     691             :             }
     692             : 
     693             :             /* subtract TBE/BWE flag */
     694       10956 :             if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
     695             :             {
     696        8298 :                 bits--;
     697             :             }
     698             :         }
     699             :         else
     700             :         {
     701             :             /* Subtract signaling bits */
     702      663839 :             bits -= signaling_bits;
     703             :         }
     704             : 
     705      674795 :         if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
     706             :         {
     707             :             /* extension layer signaling bit is counted in the extension layer bitbudget */
     708      603906 :             bits++;
     709             :         }
     710             : 
     711             :         /*-----------------------------------------------------------------*
     712             :          * LSF Q bit-budget
     713             :          *-----------------------------------------------------------------*/
     714             : 
     715      674795 :         if ( !tdm_lp_reuse_flag || idchan == 0 )
     716             :         {
     717             :             /* LSF Q bit-budget */
     718      674227 :             acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     719             : 
     720      674227 :             if ( !flag_hardcoded )
     721             :             {
     722      668313 :                 if ( L_frame == L_FRAME )
     723             :                 {
     724      374075 :                     if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
     725             :                     {
     726       29962 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     727             :                     }
     728      344113 :                     else if ( ( total_brate < 7200 || coder_type == INACTIVE || coder_type == AUDIO ) && idchan == 1 )
     729             :                     {
     730             :                         /* TD stereo, secondary channel: do nothing */
     731         195 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     732             :                     }
     733      343918 :                     else if ( element_mode > EVS_MONO && coder_type == AUDIO && brate_intermed_tbl[i] < ACELP_9k60 )
     734             :                     {
     735             :                         /* primary channel: do nothing */
     736             :                     }
     737      304041 :                     else if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
     738             :                     {
     739       36931 :                         acelp_cfg->lsf_bits = 42;
     740             :                     }
     741      267110 :                     else if ( total_brate <= 9600 || coder_type == UNVOICED )
     742             :                     {
     743       40541 :                         acelp_cfg->lsf_bits = 31;
     744             :                     }
     745      226569 :                     else if ( total_brate <= 20000 )
     746             :                     {
     747      221181 :                         acelp_cfg->lsf_bits = 36;
     748             :                     }
     749             :                     else
     750             :                     {
     751        5388 :                         acelp_cfg->lsf_bits = 41;
     752             :                     }
     753             :                 }
     754             :                 else /* L_frame == L_FRAME16k */
     755             :                 {
     756      294238 :                     acelp_cfg->lsf_bits = 41;
     757             :                 }
     758             :             }
     759             : 
     760      674227 :             bits -= acelp_cfg->lsf_bits;
     761             : 
     762             :             /* mid-LSF Q bit-budget */
     763      674227 :             acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     764             : 
     765      674227 :             if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
     766             :             {
     767       76808 :                 acelp_cfg->mid_lsf_bits = 5;
     768             :                 /* primary channel: do nothing */
     769             :             }
     770             : 
     771      674227 :             bits -= acelp_cfg->mid_lsf_bits;
     772             :         }
     773         568 :         else if ( tdm_lp_reuse_flag == 1 && idchan == 1 && active_cnt != 1 )
     774             :         {
     775         568 :             bits -= TDM_IC_LSF_PRED_BITS;
     776             :         }
     777             : 
     778             :         /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
     779      674795 :         if ( ( coder_type != UNVOICED && coder_type != AUDIO && coder_type != INACTIVE && !( core_brate <= ACELP_8k00 && coder_type != TRANSITION ) ) /* mid bitrates in GC and VC, low+mid bitrates in TC */ ||
     780       69147 :              ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */
     781             :         )
     782             :         {
     783      517823 :             *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
     784      517823 :             bits -= *nBits_es_Pred;
     785             :         }
     786      156972 :         else if ( *uc_two_stage_flag )
     787             :         {
     788        8041 :             *nBits_es_Pred = 4;
     789        8041 :             bits -= *nBits_es_Pred;
     790             :         }
     791             :     }
     792             :     else
     793             :     {
     794       21842 :         bits = *unbits;
     795             :     }
     796             : 
     797      696637 :     if ( coder_type == TRANSITION && tc_call == 0 )
     798             :     {
     799       14366 :         *unbits = bits;
     800       14366 :         return error;
     801             :     }
     802             : 
     803             :     /*-----------------------------------------------------------------*
     804             :      * Low-rate mode - bits are allocated in tdm_low_rate_enc()
     805             :      *-----------------------------------------------------------------*/
     806             : 
     807      682271 :     if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
     808             :     {
     809       29962 :         acelp_cfg->FEC_mode = 0;
     810       29962 :         acelp_cfg->ltf_mode = FULL_BAND;
     811       29962 :         *nBits_es_Pred = 0;
     812       29962 :         *unbits = 0;
     813       29962 :         acelp_cfg->ubits = 0;
     814             : 
     815       29962 :         return error;
     816             :     }
     817             : 
     818             :     /*-----------------------------------------------------------------*
     819             :      * Supplementary information for FEC
     820             :      *-----------------------------------------------------------------*/
     821             : 
     822      652309 :     acelp_cfg->FEC_mode = 0;
     823      652309 :     if ( core_brate >= ACELP_11k60 && ( idchan == 0 || element_mode == EVS_MONO ) )
     824             :     {
     825      516504 :         acelp_cfg->FEC_mode = 1;
     826             : 
     827      516504 :         if ( coder_type > UNVOICED && coder_type < AUDIO && coder_type != VOICED )
     828             :         {
     829      433804 :             bits -= FEC_BITS_CLS;
     830             :         }
     831             : 
     832      516504 :         if ( coder_type != TRANSITION )
     833             :         {
     834      451103 :             if ( total_brate >= ACELP_16k40 )
     835             :             {
     836      278846 :                 acelp_cfg->FEC_mode = 2;
     837             : 
     838      278846 :                 if ( coder_type > UNVOICED && coder_type < AUDIO )
     839             :                 {
     840      247371 :                     bits -= FEC_BITS_ENR;
     841             :                 }
     842             :             }
     843             : 
     844      451103 :             if ( total_brate >= ACELP_32k )
     845             :             {
     846       49999 :                 acelp_cfg->FEC_mode = 3;
     847             : 
     848       49999 :                 if ( coder_type > UNVOICED && coder_type < AUDIO )
     849             :                 {
     850       42460 :                     bits -= FEC_BITS_POS;
     851             :                 }
     852             :             }
     853             :         }
     854             :     }
     855             : 
     856             :     /*-----------------------------------------------------------------*
     857             :      * LP filtering of the adaptive excitation
     858             :      *-----------------------------------------------------------------*/
     859             : 
     860      652309 :     if ( idchan > 0 && element_mode > EVS_MONO )
     861             :     {
     862       14747 :         acelp_cfg->ltf_mode = FULL_BAND;
     863             :     }
     864      637562 :     else if ( coder_type == UNVOICED )
     865             :     {
     866        7765 :         acelp_cfg->ltf_mode = FULL_BAND;
     867             :     }
     868      629797 :     else if ( ( coder_type == GENERIC || coder_type == TRANSITION ) && core_brate < ACELP_11k60 )
     869             :     {
     870       45488 :         acelp_cfg->ltf_mode = LOW_PASS;
     871             :     }
     872      584309 :     else if ( core_brate >= ACELP_11k60 && ( coder_type != AUDIO && !( coder_type == INACTIVE && L_frame == L_FRAME ) ) )
     873             :     {
     874      482195 :         if ( coder_type == INACTIVE && L_frame == L_FRAME16k && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
     875             :         {
     876       18381 :             acelp_cfg->ltf_mode = FULL_BAND;
     877             :         }
     878             :         else
     879             :         {
     880      463814 :             acelp_cfg->ltf_mode = NORMAL_OPERATION;
     881      463814 :             if ( coder_type != TRANSITION )
     882             :             {
     883      398413 :                 bits -= nb_subfr;
     884             :             }
     885             :         }
     886             :     }
     887             :     else
     888             :     {
     889      102114 :         acelp_cfg->ltf_mode = FULL_BAND;
     890             :     }
     891             : 
     892             :     /*-----------------------------------------------------------------*
     893             :      * UC bit-budget
     894             :      *-----------------------------------------------------------------*/
     895             : 
     896      652309 :     if ( ( ( coder_type == UNVOICED && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && core_brate <= ACELP_9k60 ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
     897             :     {
     898        2426 :         bits -= NBITS_NOISENESS; /* noiseness */
     899             :     }
     900      652309 :     if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
     901             :     {
     902         104 :         bits -= ( 3 * NB_SUBFR ); /* tilt factor */
     903             :     }
     904             : 
     905             :     /*-----------------------------------------------------------------*
     906             :      * TC bit-budget
     907             :      *-----------------------------------------------------------------*/
     908             : 
     909      652309 :     fix_first = 0;
     910      652309 :     if ( coder_type == TRANSITION )
     911             :     {
     912       75407 :         if ( tc_call == 2 )
     913             :         {
     914       17459 :             fix_first = 1;
     915             :         }
     916             : 
     917             :         /* TC signaling */
     918       75407 :         if ( L_frame == L_FRAME )
     919             :         {
     920       48833 :             if ( tc_subfr == TC_0_0 )
     921             :             {
     922        8642 :                 if ( enc_dec == ENC )
     923             :                 {
     924        3500 :                     bits -= 1; /* TC signaling */
     925             :                 }
     926             : 
     927        8642 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     928             :                 {
     929        6603 :                     bits -= 3; /* LP filtering flag */
     930             :                 }
     931             :             }
     932       40191 :             else if ( tc_subfr == TC_0_64 )
     933             :             {
     934        5294 :                 if ( enc_dec == ENC )
     935             :                 {
     936        1343 :                     bits -= 4; /* TC signaling */
     937             :                 }
     938             : 
     939        5294 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     940             :                 {
     941        4183 :                     bits -= 3; /* LP filtering flag */
     942             :                 }
     943             :             }
     944       34897 :             else if ( tc_subfr == TC_0_128 )
     945             :             {
     946        2686 :                 if ( enc_dec == ENC )
     947             :                 {
     948         691 :                     bits -= 4; /* TC signaling */
     949             :                 }
     950             : 
     951        2686 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     952             :                 {
     953        2308 :                     bits -= 2; /* LP filtering flag */
     954             :                 }
     955             :             }
     956       32211 :             else if ( tc_subfr == TC_0_192 )
     957             :             {
     958         866 :                 if ( enc_dec == ENC )
     959             :                 {
     960         221 :                     bits -= 3; /* TC signaling */
     961             :                 }
     962             : 
     963         866 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     964             :                 {
     965         720 :                     bits -= 1; /* LP filtering flag */
     966             :                 }
     967             :             }
     968       31345 :             else if ( tc_subfr == L_SUBFR )
     969             :             {
     970        5714 :                 if ( enc_dec == ENC )
     971             :                 {
     972        2318 :                     bits -= 3; /* TC signaling */
     973             :                 }
     974             : 
     975        5714 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     976             :                 {
     977        4521 :                     bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
     978             :                 }
     979             :             }
     980             :             else
     981             :             {
     982       25631 :                 if ( enc_dec == ENC )
     983             :                 {
     984        6950 :                     bits -= 4; /* TC signaling */
     985             :                 }
     986             : 
     987       25631 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     988             :                 {
     989       20492 :                     bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
     990             :                 }
     991             :             }
     992             :         }
     993             :         else /* L_frame == L_FRAME16k */
     994             :         {
     995       26574 :             if ( enc_dec == ENC )
     996             :             {
     997        6819 :                 if ( tc_subfr <= 2 * L_SUBFR )
     998             :                 {
     999        4494 :                     bits -= 2; /* TC signaling */
    1000             :                 }
    1001             :                 else
    1002             :                 {
    1003        2325 :                     bits -= 3; /* TC signaling */
    1004             :                 }
    1005             :             }
    1006             : 
    1007       26574 :             bits -= ( L_FRAME16k - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
    1008             :         }
    1009             : 
    1010             :         /* glottal-shape codebook bits */
    1011       75407 :         bits -= ( 3 + 6 + 1 + 3 );
    1012             :     }
    1013             : 
    1014             :     /*-----------------------------------------------------------------*
    1015             :      * pitch, innovation, gains bit-budget
    1016             :      *-----------------------------------------------------------------*/
    1017             : 
    1018      652309 :     acelp_cfg->fcb_mode = 0;
    1019             : 
    1020      652309 :     if ( element_mode == IVAS_CPE_TD && tdm_low_rate_mode == 1 && coder_type != INACTIVE && coder_type != UNVOICED ) /* GENERIC low rate mode for secondary channel */
    1021             :     {
    1022           0 :         set_s( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
    1023           0 :         set_s( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
    1024             : 
    1025           0 :         for ( i = 0; i < 2; i++ )
    1026             :         {
    1027           0 :             acelp_cfg->pitch_bits[i] = 0;
    1028           0 :             if ( tdm_Pitch_reuse_flag == 0 )
    1029             :             {
    1030           0 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1031           0 :                 bits -= acelp_cfg->pitch_bits[i];
    1032             :             }
    1033           0 :             acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1034           0 :             bits -= acelp_cfg->gains_mode[i];
    1035             :         }
    1036           0 :         acelp_cfg->fcb_mode = 1;
    1037             : 
    1038             : #ifdef DEBUGGING
    1039             :         if ( bits >= 55 )
    1040             :         {
    1041             :             printf( "too much bits -> %d, LPC = %d and pitch = %d\n", bits, tdm_lp_reuse_flag, tdm_Pitch_reuse_flag );
    1042             :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1043             :         }
    1044             :         else
    1045             : #endif
    1046           0 :             if ( bits >= 16 )
    1047             :         {
    1048           0 :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1049             :         }
    1050             :         else
    1051             :         {
    1052           0 :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1053           0 :             acelp_cfg->fixed_cdk_index[1] = -1;
    1054             :         }
    1055           0 :         acelp_cfg->fixed_cdk_index[2] = -1;
    1056           0 :         acelp_cfg->fixed_cdk_index[3] = -1;
    1057             :     }
    1058      652309 :     else if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) ||           /* @12.8kHz core except of GSC */
    1059      296914 :               ( nb_subfr == NB_SUBFR16k && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */
    1060      105765 :               || core == HQ_CORE /* ACELP -> HQ switching in EVS */ )
    1061             :     {
    1062             :         /* pitch Q & gain Q bit-budget - part 2*/
    1063     3011453 :         for ( i = 0; i < nb_subfr; i++ )
    1064             :         {
    1065     2464809 :             if ( L_frame == L_FRAME )
    1066             :             {
    1067     1072064 :                 if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
    1068             :                 {
    1069         528 :                     acelp_cfg->pitch_bits[i] = 0;
    1070             :                 }
    1071             :                 else
    1072             :                 {
    1073     1071536 :                     acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1074             :                 }
    1075     1072064 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1076             :             }
    1077             :             else /* L_frame == L_FRAME16k */
    1078             :             {
    1079     1392745 :                 if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
    1080             :                 {
    1081           0 :                     acelp_cfg->pitch_bits[i] = 0;
    1082             :                 }
    1083             :                 else
    1084             :                 {
    1085     1392745 :                     acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    1086             :                 }
    1087     1392745 :                 acelp_cfg->gains_mode[i] = gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    1088             :             }
    1089             : 
    1090     2464809 :             bits -= acelp_cfg->pitch_bits[i];
    1091             : 
    1092     2464809 :             if ( coder_type == INACTIVE && acelp_cfg->gains_mode[i] == 6 /* VQ vs. SQ threshold @32 kbps */ )
    1093             :             {
    1094       21050 :                 bits -= 5;
    1095             :             }
    1096             :             else
    1097             :             {
    1098     2443759 :                 if ( *uc_two_stage_flag == 1 )
    1099             :                 {
    1100       32164 :                     acelp_cfg->gains_mode[i] = 7;
    1101             :                 }
    1102             : 
    1103     2443759 :                 bits -= acelp_cfg->gains_mode[i];
    1104             :             }
    1105             :         }
    1106             : 
    1107             :         /* algebraic codebook bit-budget */
    1108      546644 :         if ( flag_hardcoded || /* EVS */
    1109      540683 :              ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1110       40525 :              ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1111             :         {
    1112      367593 :             for ( i = 0; i < nb_subfr; i++ )
    1113             :             {
    1114      305730 :                 if ( L_frame == L_FRAME )
    1115             :                 {
    1116       12760 :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1117             :                 }
    1118             :                 else /* L_frame == L_FRAME16k */
    1119             :                 {
    1120      292970 :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    1121             :                 }
    1122      305730 :                 bits -= acelp_cfg->fixed_cdk_index[i];
    1123             :             }
    1124             :         }
    1125      484781 :         else if ( !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1126             :         {
    1127      484673 :             if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
    1128             :             {
    1129           0 :                 i = bits / NB_SUBFR;
    1130           0 :                 if ( i % 2 == 0 )
    1131             :                 {
    1132           0 :                     i--; /* must be odd */
    1133             :                 }
    1134           0 :                 i = min( i, 13 );
    1135             : #ifdef DEBUG_MODE_TD
    1136             :                 if ( i < 0 )
    1137             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR::: UC negative index should not happen at frame %d\n", frame );
    1138             : #endif
    1139           0 :                 i = max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
    1140           0 :                 set_s( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
    1141           0 :                 bits -= ( i * NB_SUBFR );
    1142             :             }
    1143             :             else
    1144             :             {
    1145             : 
    1146      484673 :                 acelp_cfg->fcb_mode = 1;
    1147      484673 :                 if ( element_mode == IVAS_CPE_TD )
    1148             :                 {
    1149       29071 :                     if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr ) ) /* enough bits for all fcb */
    1150             :                     {
    1151       28995 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1152             :                     }
    1153          76 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 1 ) )
    1154             :                     {
    1155           0 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1156           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1157             :                     }
    1158          76 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 2 ) )
    1159             :                     {
    1160          76 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 2, L_SUBFR, coder_type, tc_subfr, fix_first );
    1161          76 :                         acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
    1162          76 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1163          76 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1164             :                     }
    1165           0 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) )
    1166             :                     {
    1167           0 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1168           0 :                         acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
    1169           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1170           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1171           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1172             :                     }
    1173             :                     else /* No FCB */
    1174             :                     {
    1175             : #ifdef DEBUGGING
    1176             :                         IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING!!!, No bit allocated to FCB, check frame %d\n", frame );
    1177             : #endif
    1178           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1179           0 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1180           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1181           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1182             :                     }
    1183             :                 }
    1184      455602 :                 else if ( element_mode != IVAS_CPE_TD && GSC_IVAS_mode > 0 && L_frame == L_FRAME16k )
    1185             :                 {
    1186           0 :                     bits = 100; /* 9 kbps for fcb */
    1187           0 :                     acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1188             :                 }
    1189             :                 else
    1190             :                 {
    1191      455602 :                     acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1192             :                 }
    1193             :             }
    1194             :         }
    1195             : 
    1196             :         /* AVQ codebook */
    1197      546644 :         if ( ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1198       40581 :              ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1199             :         {
    1200      351468 :             for ( i = 0; i < nb_subfr; i++ )
    1201             :             {
    1202      292890 :                 if ( flag_hardcoded )
    1203             :                 {
    1204       13380 :                     acelp_cfg->AVQ_cdk_bits[i] = AVQ_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX_16KHZ( tc_subfr ) )];
    1205             :                     {
    1206       13380 :                         bits -= acelp_cfg->AVQ_cdk_bits[i];
    1207             :                     }
    1208             :                 }
    1209             : 
    1210      292890 :                 bits -= G_AVQ_BITS;
    1211             :             }
    1212             : 
    1213       58578 :             if ( core_brate_inp >= MIN_BRATE_AVQ_EXC && core_brate_inp <= MAX_BRATE_AVQ_EXC_TD && coder_type == GENERIC )
    1214             :             {
    1215             :                 /* harm. flag ACELP AVQ */
    1216       31829 :                 bits--;
    1217             :             }
    1218             : 
    1219       58578 :             if ( !flag_hardcoded )
    1220             :             {
    1221             :                 int16_t bit_tmp;
    1222             : 
    1223       55902 :                 bit_tmp = bits / nb_subfr;
    1224       55902 :                 set_s( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
    1225       55902 :                 bits -= bit_tmp * nb_subfr;
    1226             : 
    1227       55902 :                 bit_tmp = bits % nb_subfr;
    1228       55902 :                 acelp_cfg->AVQ_cdk_bits[0] += bit_tmp;
    1229       55902 :                 bits -= bit_tmp;
    1230             :             }
    1231             :         }
    1232             :     }
    1233      105665 :     else if ( ( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) /* LBR secondary channel in TD stereo */ ||
    1234      105665 :               ( ( coder_type == INACTIVE || coder_type == AUDIO ) && nb_subfr == NB_SUBFR ) /* GSC @12.8kHz */ ||
    1235       18381 :               ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
    1236             :     {
    1237             :         int32_t Local_BR, Pitch_BR;
    1238             :         int16_t Pitch_CT;
    1239             : 
    1240             :         /* as defined at the beginning of [enc,dec]_pit_exc() */
    1241      105665 :         if ( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || core_brate > GSC_H_RATE_STG ) )
    1242             :         {
    1243         625 :             Local_BR = ACELP_8k00;
    1244         625 :             Pitch_CT = GENERIC;
    1245         625 :             Pitch_BR = ACELP_8k00;
    1246         625 :             if ( L_frame == L_FRAME16k )
    1247             :             {
    1248           0 :                 Local_BR = ACELP_14k80;
    1249           0 :                 if ( GSC_IVAS_mode > 0 && core_brate < IVAS_24k4 )
    1250             :                 {
    1251           0 :                     Local_BR = ACELP_9k60;
    1252             :                 }
    1253           0 :                 Pitch_BR = core_brate;
    1254             :             }
    1255             :         }
    1256      105040 :         else if ( GSC_noisy_speech )
    1257             :         {
    1258        2744 :             Local_BR = ACELP_7k20;
    1259        2744 :             Pitch_CT = GENERIC;
    1260        2744 :             Pitch_BR = ACELP_7k20;
    1261        2744 :             if ( L_frame == L_FRAME16k )
    1262             :             {
    1263           0 :                 Pitch_BR = core_brate;
    1264             :             }
    1265             :         }
    1266             :         else
    1267             :         {
    1268      102296 :             Local_BR = ACELP_7k20;
    1269      102296 :             Pitch_CT = AUDIO;
    1270      102296 :             Pitch_BR = core_brate;
    1271             : 
    1272      102296 :             if ( L_frame == L_FRAME16k )
    1273             :             {
    1274       18381 :                 Local_BR = ACELP_13k20;
    1275       18381 :                 Pitch_CT = GENERIC;
    1276             :             }
    1277             :         }
    1278             : 
    1279      546706 :         for ( i = 0; i < nb_subfr; i++ )
    1280             :         {
    1281      441041 :             if ( L_frame == L_FRAME16k )
    1282             :             {
    1283       91905 :                 acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
    1284       91905 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
    1285             :             }
    1286             :             else
    1287             :             {
    1288      349136 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
    1289      349136 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
    1290      349136 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i * L_SUBFR, 0 )];
    1291             :             }
    1292             :         }
    1293             :     }
    1294             : 
    1295      652309 :     if ( coder_type == TRANSITION && ( tc_call == 1 && tc_subfr == 0 && L_frame == L_FRAME ) )
    1296             :     {
    1297       15738 :         return error;
    1298             :     }
    1299             : 
    1300             :     /*-----------------------------------------------------------------*
    1301             :      * unused bits handling
    1302             :      *-----------------------------------------------------------------*/
    1303             : 
    1304      636571 :     acelp_cfg->ubits = 0; /* these bits could be reused for something else */
    1305             : 
    1306      636571 :     if ( flag_hardcoded && core_brate != PPP_NELP_2k80 )
    1307             :     {
    1308             :         /* unused bits */
    1309        5795 :         if ( coder_type == AUDIO || ( coder_type == INACTIVE && core_brate <= ACELP_24k40 ) )
    1310             :         {
    1311          42 :             acelp_cfg->ubits = 0;
    1312             :         }
    1313        5753 :         else if ( L_frame == L_FRAME )
    1314             :         {
    1315        2997 :             acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
    1316             :         }
    1317             :         else
    1318             :         {
    1319        2756 :             acelp_cfg->ubits = 0;
    1320             :         }
    1321             : 
    1322        5795 :         bits -= acelp_cfg->ubits;
    1323             :     }
    1324             : 
    1325             :     /* sanity check */
    1326      636571 :     if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || nb_subfr == NB_SUBFR16k )
    1327             :     {
    1328      549187 :         if ( ( L_frame == L_FRAME16k && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
    1329          26 :              ( GSC_IVAS_mode > 0 && L_frame == L_FRAME16k ) ) /* IVAS GSC @16kHz */
    1330             :         {
    1331       18381 :             acelp_cfg->ubits = 0;
    1332             :         }
    1333      530806 :         else if ( flag_hardcoded && core == ACELP_CORE && bits != 0 )
    1334             :         {
    1335             : #ifdef DEBUGGING
    1336             :             IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (int32_t) bits, frame );
    1337             : #endif
    1338             :         }
    1339      530806 :         else if ( bits > 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1340             :         {
    1341      188208 :             if ( idchan > 0 && element_mode == IVAS_CPE_TD )
    1342             :             {
    1343        6990 :                 if ( !tdm_lp_reuse_flag )
    1344             :                 {
    1345        6734 :                     acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
    1346        6734 :                     bits = 0;
    1347             :                 }
    1348             :                 else
    1349             :                 {
    1350         256 :                     int16_t nb_prm = 4;
    1351             : 
    1352         256 :                     if ( tdm_low_rate_mode == 1 )
    1353             :                     {
    1354           0 :                         nb_prm = 2;
    1355             :                     }
    1356             :                     /* First add remaining bits on gains */
    1357         256 :                     bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode );
    1358             : 
    1359             :                     /* Then, Increase pitch bit budget */
    1360         256 :                     if ( tdm_Pitch_reuse_flag == 0 && bits > 0 )
    1361             :                     {
    1362           0 :                         bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits );
    1363             :                     }
    1364             : 
    1365             :                     /* Increase mid-lsf bit budget */
    1366         256 :                     if ( tdm_lp_reuse_flag == 0 && bits > 0 )
    1367             :                     {
    1368           0 :                         bits -= allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits );
    1369           0 :                         bits -= allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits );
    1370             :                     }
    1371             :                 }
    1372             : #ifdef DEBUGGING
    1373             :                 if ( idchan > 0 && bits > 0 && ( coder_type > UNVOICED || tdm_low_rate_mode == 0 ) )
    1374             :                 {
    1375             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING !! Unused bits in secondary channel at frame %d\n", frame );
    1376             :                 }
    1377             : #endif
    1378             :             }
    1379             : 
    1380      181218 :             else if ( core == ACELP_CORE && coder_type >= UNVOICED && coder_type <= GENERIC && L_frame == L_FRAME )
    1381             :             {
    1382      101440 :                 acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
    1383             : 
    1384      101440 :                 if ( acelp_cfg->lsf_bits > 46 )
    1385             :                 {
    1386           0 :                     acelp_cfg->ubits = acelp_cfg->lsf_bits - 46;
    1387           0 :                     acelp_cfg->lsf_bits = 46;
    1388             :                 }
    1389      101440 :                 else if ( acelp_cfg->lsf_bits > 42 && L_frame == L_FRAME )
    1390             :                 {
    1391         452 :                     acelp_cfg->ubits = acelp_cfg->lsf_bits - 42;
    1392         452 :                     acelp_cfg->lsf_bits = 42;
    1393             :                 }
    1394             :             }
    1395             :             else
    1396             :             {
    1397       79778 :                 acelp_cfg->ubits = bits;
    1398             :             }
    1399             :         }
    1400             :         else if ( bits < 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1401             :         {
    1402             : #ifdef DEBUGGING
    1403             :             IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (int32_t) bits, frame );
    1404             : #endif
    1405             :         }
    1406             :     }
    1407             : 
    1408      636571 :     return error;
    1409             : }
    1410             : 
    1411             : /*-------------------------------------------------------------------*
    1412             :  * allocate_unused()
    1413             :  *
    1414             :  * Allocate unused bits
    1415             :  *--------------------------------------------------------------------*/
    1416             : 
    1417        1024 : static int16_t allocate_unused(
    1418             :     const int32_t core_brate,
    1419             :     const int16_t coder_type,
    1420             :     const int16_t unused_bits,
    1421             :     const int16_t nb_prm,
    1422             :     const int16_t subfr,
    1423             :     const int16_t prm_type,
    1424             :     int16_t *prm_bit_mode )
    1425             : {
    1426        1024 :     int16_t max_bit_per_pos = 0, bit_added = 0;
    1427             : 
    1428        1024 :     if ( prm_type == GAINSPRM )
    1429             :     {
    1430        1024 :         max_bit_per_pos = 6;
    1431        1024 :         if ( core_brate > ACELP_8k00 )
    1432             :         {
    1433         832 :             max_bit_per_pos = 7;
    1434             :         }
    1435         192 :         else if ( coder_type != UNVOICED )
    1436             :         {
    1437         192 :             if ( subfr >= 1 )
    1438             :             {
    1439         144 :                 max_bit_per_pos = 7;
    1440             :             }
    1441          48 :             else if ( subfr == 0 )
    1442             :             {
    1443          48 :                 max_bit_per_pos = 8;
    1444             :             }
    1445             :         }
    1446           0 :         else if ( coder_type == UNVOICED )
    1447             :         {
    1448           0 :             max_bit_per_pos = 9; /* No real limit on UC gain bit budget of the secondary channel */
    1449             :         }
    1450             :     }
    1451           0 :     else if ( prm_type == PITCHPRM )
    1452             :     {
    1453           0 :         max_bit_per_pos = 6;
    1454           0 :         if ( subfr == 0 || subfr == 2 || nb_prm == 2 )
    1455             :         {
    1456           0 :             max_bit_per_pos = 10;
    1457             :         }
    1458             : 
    1459           0 :         if ( coder_type == UNVOICED )
    1460             :         {
    1461           0 :             max_bit_per_pos = 0; /* Should not allocate bits in case of unvoiced coder type */
    1462             :         }
    1463             :     }
    1464           0 :     else if ( prm_type == MID_LSFSPRM )
    1465             :     {
    1466           0 :         max_bit_per_pos = 5;
    1467             :     }
    1468           0 :     else if ( prm_type == LSFPRM )
    1469             :     {
    1470           0 :         max_bit_per_pos = 42;
    1471             :     }
    1472             :     else
    1473             :     {
    1474             : #ifdef DEBUG_MODE_TD
    1475             :         IVAS_ERROR( IVAS_ERR_WRONG_MODE, "unknown mode in bit_alloc.c" );
    1476             : #endif
    1477             :     }
    1478             : 
    1479        1024 :     max_bit_per_pos = min( unused_bits, max_bit_per_pos - prm_bit_mode[subfr] );
    1480        1024 :     if ( max_bit_per_pos < 0 )
    1481             :     {
    1482           0 :         return 0;
    1483             :     }
    1484        1024 :     else if ( max_bit_per_pos >= 0 && subfr == ( nb_prm - 1 ) )
    1485             :     {
    1486         256 :         prm_bit_mode[subfr] += max_bit_per_pos;
    1487             :     }
    1488             :     else
    1489             :     {
    1490         768 :         prm_bit_mode[subfr] += max_bit_per_pos;
    1491         768 :         bit_added += allocate_unused( core_brate, coder_type, unused_bits - max_bit_per_pos, nb_prm, subfr + 1, prm_type, &prm_bit_mode[0] );
    1492             :     }
    1493             : 
    1494        1024 :     return bit_added + max_bit_per_pos;
    1495             : }
    1496             : 
    1497             : 
    1498             : /*-------------------------------------------------------------------*
    1499             :  * set_ACELP_flag()
    1500             :  *
    1501             :  * set ACELP@16kHz flag
    1502             :  *--------------------------------------------------------------------*/
    1503             : 
    1504             : /*! r: ACELP16k flag */
    1505     4540924 : int16_t set_ACELP_flag(
    1506             :     const int16_t element_mode,  /* i  : element mode                */
    1507             :     const int32_t element_brate, /* i  : element bitrate             */
    1508             :     const int32_t total_brate,   /* i  : total bitrate per channel   */
    1509             :     const int16_t idchan,        /* i  : Channel id                  */
    1510             :     const int16_t tdm_LRTD_flag, /* i  : LRTD stereo mode flag       */
    1511             :     const int16_t bwidth,        /* i  : audio bandwidth             */
    1512             :     const int16_t cng_type       /* i  : CNG type                    */
    1513             : )
    1514             : {
    1515     4540924 :     if ( element_mode == IVAS_CPE_DFT && idchan == 0 && total_brate <= SID_2k40 && bwidth == WB && cng_type == LP_CNG )
    1516             :     {
    1517             : 
    1518        1464 :         return 1;
    1519             :     }
    1520     4539460 :     else if ( element_mode == IVAS_CPE_TD )
    1521             :     {
    1522       30262 :         if ( element_brate >= IVAS_24k4 && idchan == 0 && ( tdm_LRTD_flag == 0 || element_brate > IVAS_24k4 ) )
    1523             :         {
    1524        8403 :             return 1;
    1525             :         }
    1526             :         else
    1527             :         {
    1528       21859 :             return 0;
    1529             :         }
    1530             :     }
    1531     4509198 :     else if ( element_mode == IVAS_CPE_DFT )
    1532             :     {
    1533      238065 :         if ( element_brate >= IVAS_24k4 )
    1534             :         {
    1535      114528 :             return 1;
    1536             :         }
    1537             :         else
    1538             :         {
    1539      123537 :             return 0;
    1540             :         }
    1541             :     }
    1542     4271133 :     else if ( element_mode == IVAS_SCE )
    1543             :     {
    1544     1375098 :         if ( element_brate >= SCE_CORE_16k_LOW_LIMIT )
    1545             :         {
    1546      942792 :             return 1;
    1547             :         }
    1548             :         else
    1549             :         {
    1550      432306 :             return 0;
    1551             :         }
    1552             :     }
    1553     2896035 :     else if ( total_brate >= ACELP_16k_LOW_LIMIT ) /* EVS_MONO */
    1554             :     {
    1555     2845099 :         return 1;
    1556             :     }
    1557             :     else
    1558             :     {
    1559       50936 :         return 0;
    1560             :     }
    1561             : }

Generated by: LCOV version 1.14