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 @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 447 549 81.4 %
Date: 2025-07-09 08:36:12 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 tdm_Pitch_reuse_flag,     /* i  : primary channel pitch reuse flag*/
     556             :     const int16_t tdm_LRTD_flag,            /* i  : LRTD stereo mode flag           */
     557             :     const int16_t GSC_IVAS_mode             /* i  : GSC IVAS mode                   */
     558             : )
     559             : {
     560             :     int16_t i, bits, nb_subfr;
     561             :     int16_t flag_hardcoded, coder_type_sw, fix_first;
     562             :     int32_t core_brate;
     563             : #ifdef DEBUGGING
     564             :     int32_t core_brate_inpI = core_brate_inp;
     565             : #endif
     566             :     ivas_error error;
     567             : 
     568      696637 :     error = IVAS_ERR_OK;
     569             : 
     570             :     /*-----------------------------------------------------------------*
     571             :      * Set the flag indicating two-stage Unvoiced (UC) frame
     572             :      *-----------------------------------------------------------------*/
     573             : 
     574      696637 :     *uc_two_stage_flag = 0;
     575      696637 :     if ( coder_type == UNVOICED )
     576             :     {
     577        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 ) ) )
     578             :         {
     579        8041 :             *uc_two_stage_flag = 1;
     580             :         }
     581             :     }
     582             : 
     583             :     /*-----------------------------------------------------------------*
     584             :      * Set the number of subframes
     585             :      *-----------------------------------------------------------------*/
     586             : 
     587      696637 :     if ( L_frame == L_FRAME )
     588             :     {
     589      392824 :         nb_subfr = NB_SUBFR;
     590             : 
     591             : #ifdef DEBUGGING
     592             :         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 ) )
     593             :         {
     594             :             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 );
     595             :         }
     596             : 
     597             :         if ( core_brate_inp > ACELP_12k8_HIGH_LIMIT && core == ACELP_CORE )
     598             :         {
     599             :             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 );
     600             :         }
     601             : #endif
     602             :     }
     603             :     else /* L_frame == L_FRAME16k */
     604             :     {
     605      303813 :         nb_subfr = NB_SUBFR16k;
     606             : 
     607             : #ifdef DEBUGGING
     608             :         if ( core_brate_inp < ACELP_16k_LOW_LIMIT && core == ACELP_CORE )
     609             :         {
     610             :             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 );
     611             :         }
     612             : #endif
     613             :     }
     614             : 
     615      696637 :     coder_type_sw = coder_type;
     616      696637 :     if ( core != ACELP_CORE )
     617             :     {
     618             :         /* used in acelp_core_switch_enc() */
     619         100 :         nb_subfr = 1;
     620         100 :         if ( L_frame == L_FRAME )
     621             :         {
     622          20 :             coder_type_sw = TRANSITION;
     623             :         }
     624             :     }
     625             : 
     626             :     /*-----------------------------------------------------------------*
     627             :      * Check if the core_brate is hard coded (to keep BE for mono core) or not
     628             :      *-----------------------------------------------------------------*/
     629             : 
     630      696637 :     flag_hardcoded = 0;
     631      696637 :     i = 0;
     632             : 
     633     6483167 :     while ( i < SIZE_BRATE_INTERMED_TBL )
     634             :     {
     635     6483167 :         if ( core_brate_inp == brate_intermed_tbl[i] )
     636             :         {
     637       29121 :             flag_hardcoded = 1;
     638       29121 :             break;
     639             :         }
     640             : 
     641     6454046 :         if ( core_brate_inp < brate_intermed_tbl[i] )
     642             :         {
     643      667516 :             flag_hardcoded = 0;
     644      667516 :             break;
     645             :         }
     646             : 
     647     5786530 :         i++;
     648             :     }
     649             : 
     650      696637 :     if ( element_mode == IVAS_CPE_TD && coder_type == AUDIO &&
     651         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 */
     652             :     {
     653          14 :         i--;
     654             :     }
     655             : 
     656      696637 :     core_brate = brate_intermed_tbl[i];
     657             : 
     658      696637 :     if ( element_mode > EVS_MONO )
     659             :     {
     660      690510 :         flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
     661             :     }
     662             : 
     663      696637 :     if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
     664             :     {
     665         100 :         flag_hardcoded = 1;
     666             :     }
     667             : 
     668             :     /*-----------------------------------------------------------------*
     669             :      * ACELP bit allocation
     670             :      *-----------------------------------------------------------------*/
     671             : 
     672      696637 :     if ( !( coder_type == TRANSITION && tc_subfr != -1 ) || enc_dec == DEC )
     673             :     {
     674             :         /* Set the bit-budget */
     675      674795 :         bits = (int16_t) ( core_brate_inp / FRAMES_PER_SEC );
     676             : 
     677      674795 :         if ( coder_type == TRANSITION && enc_dec == DEC && tc_call == 1 )
     678             :         {
     679       41832 :             bits += *nBits_es_Pred; /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
     680             :         }
     681             : 
     682             :         /* Subtract signaling bits */
     683      674795 :         if ( enc_dec == DEC && idchan == 1 && element_mode > EVS_MONO )
     684             :         {
     685       10956 :             bits -= TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS;
     686             : 
     687       10956 :             if ( tdm_LRTD_flag == 1 )
     688             :             {
     689       10602 :                 bits += STEREO_BITS_TCA;
     690             :             }
     691             : 
     692             :             /* subtract TBE/BWE flag */
     693       10956 :             if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
     694             :             {
     695        8298 :                 bits--;
     696             :             }
     697             :         }
     698             :         else
     699             :         {
     700             :             /* Subtract signaling bits */
     701      663839 :             bits -= signaling_bits;
     702             :         }
     703             : 
     704      674795 :         if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
     705             :         {
     706             :             /* extension layer signaling bit is counted in the extension layer bitbudget */
     707      603906 :             bits++;
     708             :         }
     709             : 
     710             :         /*-----------------------------------------------------------------*
     711             :          * LSF Q bit-budget
     712             :          *-----------------------------------------------------------------*/
     713             : 
     714      674795 :         if ( !tdm_lp_reuse_flag || idchan == 0 )
     715             :         {
     716             :             /* LSF Q bit-budget */
     717      674227 :             acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     718             : 
     719      674227 :             if ( !flag_hardcoded )
     720             :             {
     721      668313 :                 if ( L_frame == L_FRAME )
     722             :                 {
     723      374075 :                     if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
     724             :                     {
     725       29962 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     726             :                     }
     727      344113 :                     else if ( ( total_brate < 7200 || coder_type == INACTIVE || coder_type == AUDIO ) && idchan == 1 )
     728             :                     {
     729             :                         /* TD stereo, secondary channel: do nothing */
     730         195 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     731             :                     }
     732      343918 :                     else if ( element_mode > EVS_MONO && coder_type == AUDIO && brate_intermed_tbl[i] < ACELP_9k60 )
     733             :                     {
     734             :                         /* primary channel: do nothing */
     735             :                     }
     736      304041 :                     else if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
     737             :                     {
     738       36931 :                         acelp_cfg->lsf_bits = 42;
     739             :                     }
     740      267110 :                     else if ( total_brate <= 9600 || coder_type == UNVOICED )
     741             :                     {
     742       40541 :                         acelp_cfg->lsf_bits = 31;
     743             :                     }
     744      226569 :                     else if ( total_brate <= 20000 )
     745             :                     {
     746      221181 :                         acelp_cfg->lsf_bits = 36;
     747             :                     }
     748             :                     else
     749             :                     {
     750        5388 :                         acelp_cfg->lsf_bits = 41;
     751             :                     }
     752             :                 }
     753             :                 else /* L_frame == L_FRAME16k */
     754             :                 {
     755      294238 :                     acelp_cfg->lsf_bits = 41;
     756             :                 }
     757             :             }
     758             : 
     759      674227 :             bits -= acelp_cfg->lsf_bits;
     760             : 
     761             :             /* mid-LSF Q bit-budget */
     762      674227 :             acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     763             : 
     764      674227 :             if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
     765             :             {
     766       76808 :                 acelp_cfg->mid_lsf_bits = 5;
     767             :                 /* primary channel: do nothing */
     768             :             }
     769             : 
     770      674227 :             bits -= acelp_cfg->mid_lsf_bits;
     771             :         }
     772         568 :         else if ( tdm_lp_reuse_flag == 1 && idchan == 1 )
     773             :         {
     774         568 :             bits -= TDM_IC_LSF_PRED_BITS;
     775             :         }
     776             : 
     777             :         /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
     778      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 */ ||
     779       69147 :              ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */
     780             :         )
     781             :         {
     782      517823 :             *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
     783      517823 :             bits -= *nBits_es_Pred;
     784             :         }
     785      156972 :         else if ( *uc_two_stage_flag )
     786             :         {
     787        8041 :             *nBits_es_Pred = 4;
     788        8041 :             bits -= *nBits_es_Pred;
     789             :         }
     790             :     }
     791             :     else
     792             :     {
     793       21842 :         bits = *unbits;
     794             :     }
     795             : 
     796      696637 :     if ( coder_type == TRANSITION && tc_call == 0 )
     797             :     {
     798       14366 :         *unbits = bits;
     799       14366 :         return error;
     800             :     }
     801             : 
     802             :     /*-----------------------------------------------------------------*
     803             :      * Low-rate mode - bits are allocated in tdm_low_rate_enc()
     804             :      *-----------------------------------------------------------------*/
     805             : 
     806      682271 :     if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
     807             :     {
     808       29962 :         acelp_cfg->FEC_mode = 0;
     809       29962 :         acelp_cfg->ltf_mode = FULL_BAND;
     810       29962 :         *nBits_es_Pred = 0;
     811       29962 :         *unbits = 0;
     812       29962 :         acelp_cfg->ubits = 0;
     813             : 
     814       29962 :         return error;
     815             :     }
     816             : 
     817             :     /*-----------------------------------------------------------------*
     818             :      * Supplementary information for FEC
     819             :      *-----------------------------------------------------------------*/
     820             : 
     821      652309 :     acelp_cfg->FEC_mode = 0;
     822      652309 :     if ( core_brate >= ACELP_11k60 && ( idchan == 0 || element_mode == EVS_MONO ) )
     823             :     {
     824      516504 :         acelp_cfg->FEC_mode = 1;
     825             : 
     826      516504 :         if ( coder_type > UNVOICED && coder_type < AUDIO && coder_type != VOICED )
     827             :         {
     828      433804 :             bits -= FEC_BITS_CLS;
     829             :         }
     830             : 
     831      516504 :         if ( coder_type != TRANSITION )
     832             :         {
     833      451103 :             if ( total_brate >= ACELP_16k40 )
     834             :             {
     835      278846 :                 acelp_cfg->FEC_mode = 2;
     836             : 
     837      278846 :                 if ( coder_type > UNVOICED && coder_type < AUDIO )
     838             :                 {
     839      247371 :                     bits -= FEC_BITS_ENR;
     840             :                 }
     841             :             }
     842             : 
     843      451103 :             if ( total_brate >= ACELP_32k )
     844             :             {
     845       49999 :                 acelp_cfg->FEC_mode = 3;
     846             : 
     847       49999 :                 if ( coder_type > UNVOICED && coder_type < AUDIO )
     848             :                 {
     849       42460 :                     bits -= FEC_BITS_POS;
     850             :                 }
     851             :             }
     852             :         }
     853             :     }
     854             : 
     855             :     /*-----------------------------------------------------------------*
     856             :      * LP filtering of the adaptive excitation
     857             :      *-----------------------------------------------------------------*/
     858             : 
     859      652309 :     if ( idchan > 0 && element_mode > EVS_MONO )
     860             :     {
     861       14747 :         acelp_cfg->ltf_mode = FULL_BAND;
     862             :     }
     863      637562 :     else if ( coder_type == UNVOICED )
     864             :     {
     865        7765 :         acelp_cfg->ltf_mode = FULL_BAND;
     866             :     }
     867      629797 :     else if ( ( coder_type == GENERIC || coder_type == TRANSITION ) && core_brate < ACELP_11k60 )
     868             :     {
     869       45488 :         acelp_cfg->ltf_mode = LOW_PASS;
     870             :     }
     871      584309 :     else if ( core_brate >= ACELP_11k60 && ( coder_type != AUDIO && !( coder_type == INACTIVE && L_frame == L_FRAME ) ) )
     872             :     {
     873      482195 :         if ( coder_type == INACTIVE && L_frame == L_FRAME16k && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
     874             :         {
     875       18381 :             acelp_cfg->ltf_mode = FULL_BAND;
     876             :         }
     877             :         else
     878             :         {
     879      463814 :             acelp_cfg->ltf_mode = NORMAL_OPERATION;
     880      463814 :             if ( coder_type != TRANSITION )
     881             :             {
     882      398413 :                 bits -= nb_subfr;
     883             :             }
     884             :         }
     885             :     }
     886             :     else
     887             :     {
     888      102114 :         acelp_cfg->ltf_mode = FULL_BAND;
     889             :     }
     890             : 
     891             :     /*-----------------------------------------------------------------*
     892             :      * UC bit-budget
     893             :      *-----------------------------------------------------------------*/
     894             : 
     895      652309 :     if ( ( ( coder_type == UNVOICED && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && core_brate <= ACELP_9k60 ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
     896             :     {
     897        2426 :         bits -= NBITS_NOISENESS; /* noiseness */
     898             :     }
     899      652309 :     if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
     900             :     {
     901         104 :         bits -= ( 3 * NB_SUBFR ); /* tilt factor */
     902             :     }
     903             : 
     904             :     /*-----------------------------------------------------------------*
     905             :      * TC bit-budget
     906             :      *-----------------------------------------------------------------*/
     907             : 
     908      652309 :     fix_first = 0;
     909      652309 :     if ( coder_type == TRANSITION )
     910             :     {
     911       75407 :         if ( tc_call == 2 )
     912             :         {
     913       17459 :             fix_first = 1;
     914             :         }
     915             : 
     916             :         /* TC signaling */
     917       75407 :         if ( L_frame == L_FRAME )
     918             :         {
     919       48833 :             if ( tc_subfr == TC_0_0 )
     920             :             {
     921        8642 :                 if ( enc_dec == ENC )
     922             :                 {
     923        3500 :                     bits -= 1; /* TC signaling */
     924             :                 }
     925             : 
     926        8642 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     927             :                 {
     928        6603 :                     bits -= 3; /* LP filtering flag */
     929             :                 }
     930             :             }
     931       40191 :             else if ( tc_subfr == TC_0_64 )
     932             :             {
     933        5294 :                 if ( enc_dec == ENC )
     934             :                 {
     935        1343 :                     bits -= 4; /* TC signaling */
     936             :                 }
     937             : 
     938        5294 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     939             :                 {
     940        4183 :                     bits -= 3; /* LP filtering flag */
     941             :                 }
     942             :             }
     943       34897 :             else if ( tc_subfr == TC_0_128 )
     944             :             {
     945        2686 :                 if ( enc_dec == ENC )
     946             :                 {
     947         691 :                     bits -= 4; /* TC signaling */
     948             :                 }
     949             : 
     950        2686 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     951             :                 {
     952        2308 :                     bits -= 2; /* LP filtering flag */
     953             :                 }
     954             :             }
     955       32211 :             else if ( tc_subfr == TC_0_192 )
     956             :             {
     957         866 :                 if ( enc_dec == ENC )
     958             :                 {
     959         221 :                     bits -= 3; /* TC signaling */
     960             :                 }
     961             : 
     962         866 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     963             :                 {
     964         720 :                     bits -= 1; /* LP filtering flag */
     965             :                 }
     966             :             }
     967       31345 :             else if ( tc_subfr == L_SUBFR )
     968             :             {
     969        5714 :                 if ( enc_dec == ENC )
     970             :                 {
     971        2318 :                     bits -= 3; /* TC signaling */
     972             :                 }
     973             : 
     974        5714 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     975             :                 {
     976        4521 :                     bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
     977             :                 }
     978             :             }
     979             :             else
     980             :             {
     981       25631 :                 if ( enc_dec == ENC )
     982             :                 {
     983        6950 :                     bits -= 4; /* TC signaling */
     984             :                 }
     985             : 
     986       25631 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     987             :                 {
     988       20492 :                     bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
     989             :                 }
     990             :             }
     991             :         }
     992             :         else /* L_frame == L_FRAME16k */
     993             :         {
     994       26574 :             if ( enc_dec == ENC )
     995             :             {
     996        6819 :                 if ( tc_subfr <= 2 * L_SUBFR )
     997             :                 {
     998        4494 :                     bits -= 2; /* TC signaling */
     999             :                 }
    1000             :                 else
    1001             :                 {
    1002        2325 :                     bits -= 3; /* TC signaling */
    1003             :                 }
    1004             :             }
    1005             : 
    1006       26574 :             bits -= ( L_FRAME16k - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
    1007             :         }
    1008             : 
    1009             :         /* glottal-shape codebook bits */
    1010       75407 :         bits -= ( 3 + 6 + 1 + 3 );
    1011             :     }
    1012             : 
    1013             :     /*-----------------------------------------------------------------*
    1014             :      * pitch, innovation, gains bit-budget
    1015             :      *-----------------------------------------------------------------*/
    1016             : 
    1017      652309 :     acelp_cfg->fcb_mode = 0;
    1018             : 
    1019      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 */
    1020             :     {
    1021           0 :         set_s( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
    1022           0 :         set_s( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
    1023             : 
    1024           0 :         for ( i = 0; i < 2; i++ )
    1025             :         {
    1026           0 :             acelp_cfg->pitch_bits[i] = 0;
    1027           0 :             if ( tdm_Pitch_reuse_flag == 0 )
    1028             :             {
    1029           0 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1030           0 :                 bits -= acelp_cfg->pitch_bits[i];
    1031             :             }
    1032           0 :             acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1033           0 :             bits -= acelp_cfg->gains_mode[i];
    1034             :         }
    1035           0 :         acelp_cfg->fcb_mode = 1;
    1036             : 
    1037             : #ifdef DEBUGGING
    1038             :         if ( bits >= 55 )
    1039             :         {
    1040             :             printf( "too much bits -> %d, LPC = %d and pitch = %d\n", bits, tdm_lp_reuse_flag, tdm_Pitch_reuse_flag );
    1041             :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1042             :         }
    1043             :         else
    1044             : #endif
    1045           0 :             if ( bits >= 16 )
    1046             :         {
    1047           0 :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1048             :         }
    1049             :         else
    1050             :         {
    1051           0 :             acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 2, 2 * L_SUBFR, GENERIC, -1, 0 );
    1052           0 :             acelp_cfg->fixed_cdk_index[1] = -1;
    1053             :         }
    1054           0 :         acelp_cfg->fixed_cdk_index[2] = -1;
    1055           0 :         acelp_cfg->fixed_cdk_index[3] = -1;
    1056             :     }
    1057      652309 :     else if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) ||           /* @12.8kHz core except of GSC */
    1058      296914 :               ( nb_subfr == NB_SUBFR16k && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */
    1059      105765 :               || core == HQ_CORE /* ACELP -> HQ switching in EVS */ )
    1060             :     {
    1061             :         /* pitch Q & gain Q bit-budget - part 2*/
    1062     3011453 :         for ( i = 0; i < nb_subfr; i++ )
    1063             :         {
    1064     2464809 :             if ( L_frame == L_FRAME )
    1065             :             {
    1066     1072064 :                 if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
    1067             :                 {
    1068         528 :                     acelp_cfg->pitch_bits[i] = 0;
    1069             :                 }
    1070             :                 else
    1071             :                 {
    1072     1071536 :                     acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1073             :                 }
    1074     1072064 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type_sw, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1075             :             }
    1076             :             else /* L_frame == L_FRAME16k */
    1077             :             {
    1078     1392745 :                 if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
    1079             :                 {
    1080           0 :                     acelp_cfg->pitch_bits[i] = 0;
    1081             :                 }
    1082             :                 else
    1083             :                 {
    1084     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 ) )];
    1085             :                 }
    1086     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 ) )];
    1087             :             }
    1088             : 
    1089     2464809 :             bits -= acelp_cfg->pitch_bits[i];
    1090             : 
    1091     2464809 :             if ( coder_type == INACTIVE && acelp_cfg->gains_mode[i] == 6 /* VQ vs. SQ threshold @32 kbps */ )
    1092             :             {
    1093       21050 :                 bits -= 5;
    1094             :             }
    1095             :             else
    1096             :             {
    1097     2443759 :                 if ( *uc_two_stage_flag == 1 )
    1098             :                 {
    1099       32164 :                     acelp_cfg->gains_mode[i] = 7;
    1100             :                 }
    1101             : 
    1102     2443759 :                 bits -= acelp_cfg->gains_mode[i];
    1103             :             }
    1104             :         }
    1105             : 
    1106             :         /* algebraic codebook bit-budget */
    1107      546644 :         if ( flag_hardcoded || /* EVS */
    1108      540683 :              ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1109       40525 :              ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1110             :         {
    1111      367593 :             for ( i = 0; i < nb_subfr; i++ )
    1112             :             {
    1113      305730 :                 if ( L_frame == L_FRAME )
    1114             :                 {
    1115       12760 :                     acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1116             :                 }
    1117             :                 else /* L_frame == L_FRAME16k */
    1118             :                 {
    1119      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 ) )];
    1120             :                 }
    1121      305730 :                 bits -= acelp_cfg->fixed_cdk_index[i];
    1122             :             }
    1123             :         }
    1124      484781 :         else if ( !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1125             :         {
    1126      484673 :             if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
    1127             :             {
    1128           0 :                 i = bits / NB_SUBFR;
    1129           0 :                 if ( i % 2 == 0 )
    1130             :                 {
    1131           0 :                     i--; /* must be odd */
    1132             :                 }
    1133           0 :                 i = min( i, 13 );
    1134             : #ifdef DEBUG_MODE_TD
    1135             :                 if ( i < 0 )
    1136             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR::: UC negative index should not happen at frame %d\n", frame );
    1137             : #endif
    1138           0 :                 i = max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
    1139           0 :                 set_s( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
    1140           0 :                 bits -= ( i * NB_SUBFR );
    1141             :             }
    1142             :             else
    1143             :             {
    1144             : 
    1145      484673 :                 acelp_cfg->fcb_mode = 1;
    1146      484673 :                 if ( element_mode == IVAS_CPE_TD )
    1147             :                 {
    1148       29071 :                     if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr ) ) /* enough bits for all fcb */
    1149             :                     {
    1150       28995 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1151             :                     }
    1152          76 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 1 ) )
    1153             :                     {
    1154           0 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1155           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1156             :                     }
    1157          76 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 2 ) )
    1158             :                     {
    1159          76 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 2, L_SUBFR, coder_type, tc_subfr, fix_first );
    1160          76 :                         acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
    1161          76 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1162          76 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1163             :                     }
    1164           0 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) )
    1165             :                     {
    1166           0 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, 1, L_SUBFR, coder_type, tc_subfr, fix_first );
    1167           0 :                         acelp_cfg->fixed_cdk_index[1] = acelp_cfg->fixed_cdk_index[0];
    1168           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1169           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1170           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1171             :                     }
    1172             :                     else /* No FCB */
    1173             :                     {
    1174             : #ifdef DEBUGGING
    1175             :                         IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING!!!, No bit allocated to FCB, check frame %d\n", frame );
    1176             : #endif
    1177           0 :                         acelp_cfg->fixed_cdk_index[0] = -1;
    1178           0 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1179           0 :                         acelp_cfg->fixed_cdk_index[2] = -1;
    1180           0 :                         acelp_cfg->fixed_cdk_index[3] = -1;
    1181             :                     }
    1182             :                 }
    1183      455602 :                 else if ( element_mode != IVAS_CPE_TD && GSC_IVAS_mode > 0 && L_frame == L_FRAME16k )
    1184             :                 {
    1185           0 :                     bits = 100; /* 9 kbps for fcb */
    1186           0 :                     acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1187             :                 }
    1188             :                 else
    1189             :                 {
    1190      455602 :                     acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1191             :                 }
    1192             :             }
    1193             :         }
    1194             : 
    1195             :         /* AVQ codebook */
    1196      546644 :         if ( ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1197       40581 :              ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1198             :         {
    1199      351468 :             for ( i = 0; i < nb_subfr; i++ )
    1200             :             {
    1201      292890 :                 if ( flag_hardcoded )
    1202             :                 {
    1203       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 ) )];
    1204             :                     {
    1205       13380 :                         bits -= acelp_cfg->AVQ_cdk_bits[i];
    1206             :                     }
    1207             :                 }
    1208             : 
    1209      292890 :                 bits -= G_AVQ_BITS;
    1210             :             }
    1211             : 
    1212       58578 :             if ( core_brate_inp >= MIN_BRATE_AVQ_EXC && core_brate_inp <= MAX_BRATE_AVQ_EXC_TD && coder_type == GENERIC )
    1213             :             {
    1214             :                 /* harm. flag ACELP AVQ */
    1215       31829 :                 bits--;
    1216             :             }
    1217             : 
    1218       58578 :             if ( !flag_hardcoded )
    1219             :             {
    1220             :                 int16_t bit_tmp;
    1221             : 
    1222       55902 :                 bit_tmp = bits / nb_subfr;
    1223       55902 :                 set_s( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
    1224       55902 :                 bits -= bit_tmp * nb_subfr;
    1225             : 
    1226       55902 :                 bit_tmp = bits % nb_subfr;
    1227       55902 :                 acelp_cfg->AVQ_cdk_bits[0] += bit_tmp;
    1228       55902 :                 bits -= bit_tmp;
    1229             :             }
    1230             :         }
    1231             :     }
    1232      105665 :     else if ( ( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) /* LBR secondary channel in TD stereo */ ||
    1233      105665 :               ( ( coder_type == INACTIVE || coder_type == AUDIO ) && nb_subfr == NB_SUBFR ) /* GSC @12.8kHz */ ||
    1234       18381 :               ( coder_type == INACTIVE && inactive_coder_type_flag ) /* AVQ inactive */ )
    1235             :     {
    1236             :         int32_t Local_BR, Pitch_BR;
    1237             :         int16_t Pitch_CT;
    1238             : 
    1239             :         /* as defined at the beginning of [enc,dec]_pit_exc() */
    1240      105665 :         if ( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || core_brate > GSC_H_RATE_STG ) )
    1241             :         {
    1242         625 :             Local_BR = ACELP_8k00;
    1243         625 :             Pitch_CT = GENERIC;
    1244         625 :             Pitch_BR = ACELP_8k00;
    1245         625 :             if ( L_frame == L_FRAME16k )
    1246             :             {
    1247           0 :                 Local_BR = ACELP_14k80;
    1248           0 :                 if ( GSC_IVAS_mode > 0 && core_brate < IVAS_24k4 )
    1249             :                 {
    1250           0 :                     Local_BR = ACELP_9k60;
    1251             :                 }
    1252           0 :                 Pitch_BR = core_brate;
    1253             :             }
    1254             :         }
    1255      105040 :         else if ( GSC_noisy_speech )
    1256             :         {
    1257        2744 :             Local_BR = ACELP_7k20;
    1258        2744 :             Pitch_CT = GENERIC;
    1259        2744 :             Pitch_BR = ACELP_7k20;
    1260        2744 :             if ( L_frame == L_FRAME16k )
    1261             :             {
    1262           0 :                 Pitch_BR = core_brate;
    1263             :             }
    1264             :         }
    1265             :         else
    1266             :         {
    1267      102296 :             Local_BR = ACELP_7k20;
    1268      102296 :             Pitch_CT = AUDIO;
    1269      102296 :             Pitch_BR = core_brate;
    1270             : 
    1271      102296 :             if ( L_frame == L_FRAME16k )
    1272             :             {
    1273       18381 :                 Local_BR = ACELP_13k20;
    1274       18381 :                 Pitch_CT = GENERIC;
    1275             :             }
    1276             :         }
    1277             : 
    1278      546706 :         for ( i = 0; i < nb_subfr; i++ )
    1279             :         {
    1280      441041 :             if ( L_frame == L_FRAME16k )
    1281             :             {
    1282       91905 :                 acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
    1283       91905 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
    1284             :             }
    1285             :             else
    1286             :             {
    1287      349136 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
    1288      349136 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
    1289      349136 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i * L_SUBFR, 0 )];
    1290             :             }
    1291             :         }
    1292             :     }
    1293             : 
    1294      652309 :     if ( coder_type == TRANSITION && ( tc_call == 1 && tc_subfr == 0 && L_frame == L_FRAME ) )
    1295             :     {
    1296       15738 :         return error;
    1297             :     }
    1298             : 
    1299             :     /*-----------------------------------------------------------------*
    1300             :      * unused bits handling
    1301             :      *-----------------------------------------------------------------*/
    1302             : 
    1303      636571 :     acelp_cfg->ubits = 0; /* these bits could be reused for something else */
    1304             : 
    1305      636571 :     if ( flag_hardcoded && core_brate != PPP_NELP_2k80 )
    1306             :     {
    1307             :         /* unused bits */
    1308        5795 :         if ( coder_type == AUDIO || ( coder_type == INACTIVE && core_brate <= ACELP_24k40 ) )
    1309             :         {
    1310          42 :             acelp_cfg->ubits = 0;
    1311             :         }
    1312        5753 :         else if ( L_frame == L_FRAME )
    1313             :         {
    1314        2997 :             acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
    1315             :         }
    1316             :         else
    1317             :         {
    1318        2756 :             acelp_cfg->ubits = 0;
    1319             :         }
    1320             : 
    1321        5795 :         bits -= acelp_cfg->ubits;
    1322             :     }
    1323             : 
    1324             :     /* sanity check */
    1325      636571 :     if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || nb_subfr == NB_SUBFR16k )
    1326             :     {
    1327      549187 :         if ( ( L_frame == L_FRAME16k && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
    1328          26 :              ( GSC_IVAS_mode > 0 && L_frame == L_FRAME16k ) ) /* IVAS GSC @16kHz */
    1329             :         {
    1330       18381 :             acelp_cfg->ubits = 0;
    1331             :         }
    1332      530806 :         else if ( flag_hardcoded && core == ACELP_CORE && bits != 0 )
    1333             :         {
    1334             : #ifdef DEBUGGING
    1335             :             IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (int32_t) bits, frame );
    1336             : #endif
    1337             :         }
    1338      530806 :         else if ( bits > 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1339             :         {
    1340      188208 :             if ( idchan > 0 && element_mode == IVAS_CPE_TD )
    1341             :             {
    1342        6990 :                 if ( !tdm_lp_reuse_flag )
    1343             :                 {
    1344        6734 :                     acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
    1345        6734 :                     bits = 0;
    1346             :                 }
    1347             :                 else
    1348             :                 {
    1349         256 :                     int16_t nb_prm = 4;
    1350             : 
    1351         256 :                     if ( tdm_low_rate_mode == 1 )
    1352             :                     {
    1353           0 :                         nb_prm = 2;
    1354             :                     }
    1355             :                     /* First add remaining bits on gains */
    1356         256 :                     bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode );
    1357             : 
    1358             :                     /* Then, Increase pitch bit budget */
    1359         256 :                     if ( tdm_Pitch_reuse_flag == 0 && bits > 0 )
    1360             :                     {
    1361           0 :                         bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, PITCHPRM, acelp_cfg->pitch_bits );
    1362             :                     }
    1363             : 
    1364             :                     /* Increase mid-lsf bit budget */
    1365         256 :                     if ( tdm_lp_reuse_flag == 0 && bits > 0 )
    1366             :                     {
    1367           0 :                         bits -= allocate_unused( core_brate, coder_type, bits, 1, 0, MID_LSFSPRM, &acelp_cfg->mid_lsf_bits );
    1368           0 :                         bits -= allocate_unused( core_brate, coder_type, bits, 1, 0, LSFPRM, &acelp_cfg->lsf_bits );
    1369             :                     }
    1370             :                 }
    1371             : #ifdef DEBUGGING
    1372             :                 if ( idchan > 0 && bits > 0 && ( coder_type > UNVOICED || tdm_low_rate_mode == 0 ) )
    1373             :                 {
    1374             :                     IVAS_ERROR( IVAS_ERR_INTERNAL, "WARNING !! Unused bits in secondary channel at frame %d\n", frame );
    1375             :                 }
    1376             : #endif
    1377             :             }
    1378             : 
    1379      181218 :             else if ( core == ACELP_CORE && coder_type >= UNVOICED && coder_type <= GENERIC && L_frame == L_FRAME )
    1380             :             {
    1381      101440 :                 acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
    1382             : 
    1383      101440 :                 if ( acelp_cfg->lsf_bits > 46 )
    1384             :                 {
    1385           0 :                     acelp_cfg->ubits = acelp_cfg->lsf_bits - 46;
    1386           0 :                     acelp_cfg->lsf_bits = 46;
    1387             :                 }
    1388      101440 :                 else if ( acelp_cfg->lsf_bits > 42 && L_frame == L_FRAME )
    1389             :                 {
    1390         452 :                     acelp_cfg->ubits = acelp_cfg->lsf_bits - 42;
    1391         452 :                     acelp_cfg->lsf_bits = 42;
    1392             :                 }
    1393             :             }
    1394             :             else
    1395             :             {
    1396       79778 :                 acelp_cfg->ubits = bits;
    1397             :             }
    1398             :         }
    1399             :         else if ( bits < 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1400             :         {
    1401             : #ifdef DEBUGGING
    1402             :             IVAS_ERROR( IVAS_ERR_INTERNAL, "ERROR: bit-budget incorrect (%d bits) in frame %d.\n", (int32_t) bits, frame );
    1403             : #endif
    1404             :         }
    1405             :     }
    1406             : 
    1407      636571 :     return error;
    1408             : }
    1409             : 
    1410             : /*-------------------------------------------------------------------*
    1411             :  * allocate_unused()
    1412             :  *
    1413             :  * Allocate unused bits
    1414             :  *--------------------------------------------------------------------*/
    1415             : 
    1416        1024 : static int16_t allocate_unused(
    1417             :     const int32_t core_brate,
    1418             :     const int16_t coder_type,
    1419             :     const int16_t unused_bits,
    1420             :     const int16_t nb_prm,
    1421             :     const int16_t subfr,
    1422             :     const int16_t prm_type,
    1423             :     int16_t *prm_bit_mode )
    1424             : {
    1425        1024 :     int16_t max_bit_per_pos = 0, bit_added = 0;
    1426             : 
    1427        1024 :     if ( prm_type == GAINSPRM )
    1428             :     {
    1429        1024 :         max_bit_per_pos = 6;
    1430        1024 :         if ( core_brate > ACELP_8k00 )
    1431             :         {
    1432         832 :             max_bit_per_pos = 7;
    1433             :         }
    1434         192 :         else if ( coder_type != UNVOICED )
    1435             :         {
    1436         192 :             if ( subfr >= 1 )
    1437             :             {
    1438         144 :                 max_bit_per_pos = 7;
    1439             :             }
    1440          48 :             else if ( subfr == 0 )
    1441             :             {
    1442          48 :                 max_bit_per_pos = 8;
    1443             :             }
    1444             :         }
    1445           0 :         else if ( coder_type == UNVOICED )
    1446             :         {
    1447           0 :             max_bit_per_pos = 9; /* No real limit on UC gain bit budget of the secondary channel */
    1448             :         }
    1449             :     }
    1450           0 :     else if ( prm_type == PITCHPRM )
    1451             :     {
    1452           0 :         max_bit_per_pos = 6;
    1453           0 :         if ( subfr == 0 || subfr == 2 || nb_prm == 2 )
    1454             :         {
    1455           0 :             max_bit_per_pos = 10;
    1456             :         }
    1457             : 
    1458           0 :         if ( coder_type == UNVOICED )
    1459             :         {
    1460           0 :             max_bit_per_pos = 0; /* Should not allocate bits in case of unvoiced coder type */
    1461             :         }
    1462             :     }
    1463           0 :     else if ( prm_type == MID_LSFSPRM )
    1464             :     {
    1465           0 :         max_bit_per_pos = 5;
    1466             :     }
    1467           0 :     else if ( prm_type == LSFPRM )
    1468             :     {
    1469           0 :         max_bit_per_pos = 42;
    1470             :     }
    1471             :     else
    1472             :     {
    1473             : #ifdef DEBUG_MODE_TD
    1474             :         IVAS_ERROR( IVAS_ERR_WRONG_MODE, "unknown mode in bit_alloc.c" );
    1475             : #endif
    1476             :     }
    1477             : 
    1478        1024 :     max_bit_per_pos = min( unused_bits, max_bit_per_pos - prm_bit_mode[subfr] );
    1479        1024 :     if ( max_bit_per_pos < 0 )
    1480             :     {
    1481           0 :         return 0;
    1482             :     }
    1483        1024 :     else if ( max_bit_per_pos >= 0 && subfr == ( nb_prm - 1 ) )
    1484             :     {
    1485         256 :         prm_bit_mode[subfr] += max_bit_per_pos;
    1486             :     }
    1487             :     else
    1488             :     {
    1489         768 :         prm_bit_mode[subfr] += max_bit_per_pos;
    1490         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] );
    1491             :     }
    1492             : 
    1493        1024 :     return bit_added + max_bit_per_pos;
    1494             : }
    1495             : 
    1496             : 
    1497             : /*-------------------------------------------------------------------*
    1498             :  * set_ACELP_flag()
    1499             :  *
    1500             :  * set ACELP@16kHz flag
    1501             :  *--------------------------------------------------------------------*/
    1502             : 
    1503             : /*! r: ACELP16k flag */
    1504     4540924 : int16_t set_ACELP_flag(
    1505             :     const int16_t element_mode,  /* i  : element mode                */
    1506             :     const int32_t element_brate, /* i  : element bitrate             */
    1507             :     const int32_t total_brate,   /* i  : total bitrate per channel   */
    1508             :     const int16_t idchan,        /* i  : Channel id                  */
    1509             :     const int16_t tdm_LRTD_flag, /* i  : LRTD stereo mode flag       */
    1510             :     const int16_t bwidth,        /* i  : audio bandwidth             */
    1511             :     const int16_t cng_type       /* i  : CNG type                    */
    1512             : )
    1513             : {
    1514     4540924 :     if ( element_mode == IVAS_CPE_DFT && idchan == 0 && total_brate <= SID_2k40 && bwidth == WB && cng_type == LP_CNG )
    1515             :     {
    1516             : 
    1517        1464 :         return 1;
    1518             :     }
    1519     4539460 :     else if ( element_mode == IVAS_CPE_TD )
    1520             :     {
    1521       30262 :         if ( element_brate >= IVAS_24k4 && idchan == 0 && ( tdm_LRTD_flag == 0 || element_brate > IVAS_24k4 ) )
    1522             :         {
    1523        8403 :             return 1;
    1524             :         }
    1525             :         else
    1526             :         {
    1527       21859 :             return 0;
    1528             :         }
    1529             :     }
    1530     4509198 :     else if ( element_mode == IVAS_CPE_DFT )
    1531             :     {
    1532      238065 :         if ( element_brate >= IVAS_24k4 )
    1533             :         {
    1534      114528 :             return 1;
    1535             :         }
    1536             :         else
    1537             :         {
    1538      123537 :             return 0;
    1539             :         }
    1540             :     }
    1541     4271133 :     else if ( element_mode == IVAS_SCE )
    1542             :     {
    1543     1375098 :         if ( element_brate >= SCE_CORE_16k_LOW_LIMIT )
    1544             :         {
    1545      942792 :             return 1;
    1546             :         }
    1547             :         else
    1548             :         {
    1549      432306 :             return 0;
    1550             :         }
    1551             :     }
    1552     2896035 :     else if ( total_brate >= ACELP_16k_LOW_LIMIT ) /* EVS_MONO */
    1553             :     {
    1554     2845099 :         return 1;
    1555             :     }
    1556             :     else
    1557             :     {
    1558       50936 :         return 0;
    1559             :     }
    1560             : }

Generated by: LCOV version 1.14