LCOV - code coverage report
Current view: top level - lib_com - bits_alloc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 510 549 92.9 %
Date: 2025-05-29 08:28:55 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     1303468 : 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     1303468 :     if ( bit_rate <= ACELP_9k60 )
      80             :     {
      81       24886 :         rate_mode_index = 0;
      82             :     }
      83             :     else
      84             :     {
      85     1278582 :         rate_mode_index = 1;
      86             :     }
      87             : 
      88     1303468 :     acelp_cfg->mode_index = rate_mode_index;
      89             : 
      90             :     /*LPC: midLpc should be swithced off?*/
      91     1303468 :     acelp_cfg->midLpc_enable = 1;
      92             : 
      93             :     /*ACELP ICB config*/
      94     1303468 :     if ( ( rate_mode_index == 0 ) || ( narrowBand == 1 ) )
      95             :     {
      96       24978 :         acelp_cfg->pre_emphasis = 1;
      97       24978 :         acelp_cfg->formant_enh = 1;
      98       24978 :         acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
      99       24978 :         acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
     100       24978 :         acelp_cfg->formant_tilt = 0;
     101       24978 :         acelp_cfg->voice_tilt = 0;
     102             :     }
     103             :     else
     104             :     {
     105     1278490 :         acelp_cfg->pre_emphasis = 0;
     106     1278490 :         acelp_cfg->formant_enh = 1;
     107     1278490 :         acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
     108     1278490 :         acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2;
     109     1278490 :         acelp_cfg->formant_tilt = 1;
     110     1278490 :         acelp_cfg->voice_tilt = 1;
     111             :     }
     112             : 
     113             :     /*Wide band @ 16kHz*/
     114     1303468 :     if ( nb_subfr == NB_SUBFR16k )
     115             :     {
     116      630259 :         acelp_cfg->pre_emphasis = 1;
     117      630259 :         acelp_cfg->formant_enh = 1;
     118      630259 :         acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     119      630259 :         acelp_cfg->formant_enh_den = FORMANT_SHARPENING_G2_16k;
     120      630259 :         acelp_cfg->formant_tilt = 0;
     121      630259 :         acelp_cfg->voice_tilt = 2;
     122             :     }
     123             : 
     124     1303468 :     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       35661 : 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       35661 :     mode_index = acelp_cfg->mode_index;
     149       35661 :     band_index = ( narrowBand == 0 );
     150       35661 :     bits = 0;
     151             : 
     152       35661 :     if ( band_index == 0 )
     153             :     {
     154        2503 :         if ( coder_type == INACTIVE )
     155             :         {
     156           0 :             acelp_cfg->formant_enh = 0;
     157             :         }
     158             :         else
     159             :         {
     160        2503 :             acelp_cfg->formant_enh = 1;
     161             :         }
     162             :     }
     163             : 
     164       35661 :     if ( band_index == 1 && nb_subfr == NB_SUBFR )
     165             :     {
     166             : 
     167        9865 :         if ( coder_type == INACTIVE )
     168             :         {
     169         222 :             acelp_cfg->pre_emphasis = 0;
     170         222 :             acelp_cfg->formant_enh = 0;
     171         222 :             acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1_16k;
     172         222 :             acelp_cfg->formant_tilt = 1;
     173         222 :             acelp_cfg->voice_tilt = 1;
     174             :         }
     175             :         else
     176             :         {
     177        9643 :             acelp_cfg->pre_emphasis = 1;
     178        9643 :             acelp_cfg->formant_enh = 1;
     179        9643 :             acelp_cfg->formant_enh_num = FORMANT_SHARPENING_G1;
     180        9643 :             acelp_cfg->formant_tilt = 0;
     181        9643 :             acelp_cfg->voice_tilt = 0;
     182             :         }
     183             :     }
     184             : 
     185       35661 :     if ( coder_type == UNVOICED )
     186             :     {
     187        1899 :         if ( ACELP_GAINS_MODE[mode_index][band_index][coder_type] == 6 )
     188             :         {
     189         112 :             acelp_cfg->pitch_sharpening = 0;
     190         112 :             acelp_cfg->phase_scrambling = 1;
     191             :         }
     192             :         else
     193             :         {
     194        1787 :             acelp_cfg->pitch_sharpening = 0;
     195        1787 :             acelp_cfg->phase_scrambling = 0;
     196             :         }
     197             :     }
     198             :     else
     199             :     {
     200       33762 :         acelp_cfg->pitch_sharpening = 1;
     201       33762 :         acelp_cfg->phase_scrambling = 0;
     202             :     }
     203             : 
     204       35661 :     if ( coder_type > ACELP_MODE_MAX )
     205             :     {
     206             :         /* keep pitch sharpening for RF_ALLPRED mode */
     207         568 :         acelp_cfg->pitch_sharpening = 0;
     208         568 :         acelp_cfg->phase_scrambling = 0;
     209             :     }
     210             : 
     211             :     /*Allocate bits and different modes*/
     212       35661 :     acelp_cfg->bpf_mode = ACELP_BPF_MODE[mode_index][band_index][coder_type];
     213       35661 :     bits += ACELP_BPF_BITS[acelp_cfg->bpf_mode];
     214             : 
     215       35661 :     acelp_cfg->nrg_mode = ACELP_NRG_MODE[mode_index][band_index][coder_type];
     216       35661 :     acelp_cfg->nrg_bits = ACELP_NRG_BITS[acelp_cfg->nrg_mode];
     217       35661 :     bits += acelp_cfg->nrg_bits;
     218             : 
     219       35661 :     acelp_cfg->ltp_mode = ACELP_LTP_MODE[mode_index][band_index][coder_type];
     220       35661 :     acelp_cfg->ltp_bits = 0;
     221       35661 :     acelp_cfg->ltf_mode = ACELP_LTF_MODE[mode_index][band_index][coder_type];
     222       35661 :     acelp_cfg->ltf_bits = ACELP_LTF_BITS[acelp_cfg->ltf_mode];
     223             : 
     224       35661 :     if ( nb_subfr == NB_SUBFR16k && acelp_cfg->ltf_bits == 4 )
     225             :     {
     226       10714 :         acelp_cfg->ltf_bits++;
     227             :     }
     228       35661 :     bits += acelp_cfg->ltf_bits;
     229             : 
     230             : 
     231      201598 :     for ( i = 0; i < nb_subfr; i++ )
     232             :     {
     233      165937 :         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      165937 :         if ( coder_type >= ACELP_MODE_MAX && ( i == 1 || i == 3 ) )
     237             :         {
     238        3146 :             acelp_cfg->gains_mode[i] = 0;
     239             :         }
     240             : 
     241      165937 :         bits += ACELP_GAINS_BITS[acelp_cfg->gains_mode[i]];
     242      165937 :         bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
     243      165937 :         acelp_cfg->ltp_bits += ACELP_LTP_BITS_SFR[acelp_cfg->ltp_mode][i];
     244             :     }
     245             : 
     246             :     /*Innovation*/
     247       35661 :     if ( bits_frame < bits )
     248             :     {
     249           0 :         printf( "\nWarning: bits per frame too low\n" );
     250           0 :         return -1;
     251             :     }
     252             : 
     253       35661 :     if ( coder_type == RF_ALLPRED )
     254             :     {
     255        1005 :         set_s( acelp_cfg->fixed_cdk_index, -1, nb_subfr );
     256             :     }
     257       34656 :     else if ( coder_type == RF_GENPRED )
     258             :     {
     259         522 :         acelp_cfg->fixed_cdk_index[0] = 0; /* 7 bits */
     260         522 :         acelp_cfg->fixed_cdk_index[1] = -1;
     261         522 :         acelp_cfg->fixed_cdk_index[2] = 0; /* 7 bits */
     262         522 :         acelp_cfg->fixed_cdk_index[3] = -1;
     263         522 :         acelp_cfg->fixed_cdk_index[4] = -1;
     264         522 :         bits += 14;
     265             :     }
     266       34134 :     else if ( coder_type == RF_NOPRED )
     267             :     {
     268           6 :         set_s( acelp_cfg->fixed_cdk_index, 0, nb_subfr );
     269           6 :         bits += 28;
     270             :     }
     271             :     else
     272             :     {
     273       34128 :         bits += BITS_ALLOC_adjust_acelp_fixed_cdk( bits_frame - bits, acelp_cfg->fixed_cdk_index, nb_subfr );
     274             :     }
     275             : 
     276       35661 :     remaining_bits = bits_frame - bits;
     277             : 
     278             :     /*Sanity check*/
     279       35661 :     if ( remaining_bits < 0 )
     280             :     {
     281           0 :         bits = -1;
     282             :     }
     283             : 
     284             : 
     285       35661 :     return ( bits );
     286             : }
     287             : 
     288             : /*-------------------------------------------------------------------*
     289             :  * BITS_ALLOC_adjust_acelp_fixed_cdk()
     290             :  *
     291             :  *
     292             :  *--------------------------------------------------------------------*/
     293             : 
     294       34128 : 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       34128 :     bits_subframe2 = bits_frame;
     303             : 
     304       34128 :     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      638838 :     for ( k = 0; k < ACELP_FIXED_CDK_NB - 1; k++ )
     311             :     {
     312      638680 :         if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
     313             :         {
     314       33970 :             k--; /* previous mode did not exceed bit-budget */
     315       33970 :             break;
     316             :         }
     317             :     }
     318             : 
     319       34128 :     if ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_subframe2 )
     320             :     {
     321         158 :         k--; /* previous mode did not exceed bit-budget */
     322             :     }
     323       34128 :     fixed_cdk_index[0] = k;
     324             : 
     325       34128 :     bitsused = ACELP_FIXED_CDK_BITS( k );
     326             : 
     327      159805 :     for ( sfr = 1; sfr < nb_subfr; sfr++ )
     328             :     {
     329      125677 :         bits_currsubframe = ( sfr * bits_subframe2 + bits_subframe2 ) - bitsused * nb_subfr;
     330             : 
     331             :         /* try increasing mode while below threshold */
     332      161844 :         while ( ( k < ACELP_FIXED_CDK_NB - 1 ) && ( ACELP_FIXED_CDK_BITS( k + 1 ) * nb_subfr <= bits_currsubframe ) )
     333             :         {
     334       36167 :             k++;
     335             :         }
     336             : 
     337             :         /* try decreasing mode until below threshold */
     338      142321 :         while ( ACELP_FIXED_CDK_BITS( k ) * nb_subfr > bits_currsubframe )
     339             :         {
     340       16644 :             k--;
     341       16644 :             if ( k == 0 )
     342             :             {
     343           0 :                 break;
     344             :             }
     345             :         }
     346             : 
     347             :         /* store mode */
     348      125677 :         fixed_cdk_index[sfr] = k;
     349      125677 :         bitsused += ACELP_FIXED_CDK_BITS( k );
     350             :     }
     351             : 
     352       34128 :     return bitsused;
     353             : }
     354             : 
     355             : 
     356             : /*-------------------------------------------------------------------*
     357             :  * fcb_table()
     358             :  *
     359             :  * Selection of fixed innovation codebook bitbudget table
     360             :  *--------------------------------------------------------------------*/
     361             : 
     362    73456545 : static int16_t fcb_table(
     363             :     const int16_t n,
     364             :     const int16_t L_subfr )
     365             : {
     366             :     int16_t out;
     367             : 
     368    73456545 :     out = PulseConfTable[n].bits;
     369    73456545 :     if ( L_subfr > L_SUBFR )
     370             :     {
     371        4874 :         out = fast_FCB_bits_2sfr[n];
     372             :     }
     373             : 
     374    73456545 :     return ( out );
     375             : }
     376             : 
     377             : /*-------------------------------------------------------------------*
     378             :  * acelp_FCB_allocator()
     379             :  *
     380             :  * Routine to allocate fixed innovation codebook bit-budget
     381             :  *--------------------------------------------------------------------*/
     382             : 
     383     3574672 : 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     3574672 :     error = IVAS_ERR_OK;
     400             : 
     401     3574672 :     cdbk = coder_type; /* just to avoid warning when DEBUGGING is deactivated */
     402             : 
     403     3574672 :     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     3574672 :     if ( tc_subfr >= L_SUBFR && fix_first )
     407             :     {
     408             :         int16_t i;
     409             : 
     410       81315 :         for ( i = 0; i < nb_subfr; i++ )
     411             :         {
     412       65052 :             *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[i] );
     413             :         }
     414       16263 :         return error;
     415             :     }
     416             : 
     417             :     /* TRANSITION coding: first subframe bit-budget was already fixed, glottal pulse in the first subframe */
     418     3558409 :     sfr = 0;
     419     3558409 :     if ( fix_first )
     420             :     {
     421      108211 :         *nBits -= ACELP_FIXED_CDK_BITS( fixed_cdk_index[0] );
     422      108211 :         sfr = 1;
     423      108211 :         p_fixed_cdk_index++;
     424      108211 :         nb_subfr = 3;
     425             :     }
     426             : 
     427             :     /* distribute the bit-budget equally between subframes */
     428     3558409 :     if ( L_subfr > L_SUBFR ) /* access fast_FCB_bits_2sfr */
     429             :     {
     430         542 :         max_n = 6;
     431             :     }
     432             :     else
     433             :     {
     434     3557867 :         max_n = ACELP_FIXED_CDK_NB;
     435             :     }
     436    59222925 :     for ( cdbk = 0; cdbk < max_n; cdbk++ )
     437             :     {
     438    59222923 :         if ( fcb_table( cdbk, L_subfr ) * nb_subfr > *nBits )
     439             :         {
     440     3558407 :             break;
     441             :         }
     442             :     }
     443     3558409 :     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     3558409 :     set_s( p_fixed_cdk_index, cdbk, nb_subfr );
     456     3558409 :     nBits_tmp = 0;
     457     3558409 :     if ( cdbk >= 0 )
     458             :     {
     459     3558405 :         nBits_tmp = fcb_table( cdbk, L_subfr );
     460             :     }
     461             :     else
     462             :     {
     463           4 :         nBits_tmp = 0;
     464             :     }
     465     3558409 :     *nBits -= nBits_tmp * nb_subfr;
     466             : 
     467             :     /* try to increase the FCB bit-budget of the first subframe(s) */
     468     3558409 :     if ( cdbk < ACELP_FIXED_CDK_NB - 1 )
     469             :     {
     470     3558407 :         step = fcb_table( cdbk + 1, L_subfr ) - nBits_tmp;
     471     9752284 :         while ( *nBits >= step )
     472             :         {
     473     6193877 :             ( *p_fixed_cdk_index )++;
     474     6193877 :             *nBits -= step;
     475     6193877 :             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     3558407 :         if ( fixed_cdk_index[sfr] < ACELP_FIXED_CDK_NB - 1 )
     480             :         {
     481     3558405 :             step = fcb_table( fixed_cdk_index[sfr] + 1, L_subfr ) - fcb_table( fixed_cdk_index[sfr], L_subfr );
     482     3558405 :             if ( *nBits >= step && cdbk >= 0 )
     483             :             {
     484      524605 :                 fixed_cdk_index[sfr]++;
     485      524605 :                 *nBits -= step;
     486             : 
     487      524605 :                 if ( *nBits >= step && fixed_cdk_index[sfr + 1] == fixed_cdk_index[sfr] - 1 )
     488             :                 {
     489       33182 :                     sfr++;
     490       33182 :                     fixed_cdk_index[sfr]++;
     491       33182 :                     *nBits -= step;
     492             :                 }
     493             :             }
     494             :         }
     495             :     }
     496             :     /* TRANSITION coding: allocate highest FCBQ bit-budget to the subframe with the glottal-shape codebook */
     497     3558409 :     if ( tc_subfr >= L_SUBFR )
     498             :     {
     499             :         int16_t tempr;
     500             : 
     501      203834 :         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      203834 :         if ( tc_subfr / L_SUBFR < nb_subfr - 1 )
     505             :         {
     506      101379 :             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     3558409 :     if ( L_subfr > L_SUBFR )
     512             :     {
     513             :         int16_t i, j;
     514        1626 :         for ( i = 0; i < nb_subfr; i++ )
     515             :         {
     516        1084 :             j = fixed_cdk_index[i];
     517        1084 :             fixed_cdk_index[i] = fast_FCB_bits_2sfr[j];
     518             :         }
     519             :     }
     520             : 
     521     3558409 :     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     5226712 : 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     5226712 :     error = IVAS_ERR_OK;
     570             : 
     571             :     /*-----------------------------------------------------------------*
     572             :      * Set the flag indicating two-stage Unvoiced (UC) frame
     573             :      *-----------------------------------------------------------------*/
     574             : 
     575     5226712 :     *uc_two_stage_flag = 0;
     576     5226712 :     if ( coder_type == UNVOICED )
     577             :     {
     578       52299 :         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       51629 :             *uc_two_stage_flag = 1;
     581             :         }
     582             :     }
     583             : 
     584             :     /*-----------------------------------------------------------------*
     585             :      * Set the number of subframes
     586             :      *-----------------------------------------------------------------*/
     587             : 
     588     5226712 :     if ( L_frame == L_FRAME )
     589             :     {
     590     3031324 :         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     2195388 :         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     5226712 :     coder_type_sw = coder_type;
     617     5226712 :     if ( core != ACELP_CORE )
     618             :     {
     619             :         /* used in acelp_core_switch_enc() */
     620         714 :         nb_subfr = 1;
     621         714 :         if ( L_frame == L_FRAME )
     622             :         {
     623         147 :             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     5226712 :     flag_hardcoded = 0;
     632     5226712 :     i = 0;
     633             : 
     634    46900311 :     while ( i < SIZE_BRATE_INTERMED_TBL )
     635             :     {
     636    46900311 :         if ( core_brate_inp == brate_intermed_tbl[i] )
     637             :         {
     638      241183 :             flag_hardcoded = 1;
     639      241183 :             break;
     640             :         }
     641             : 
     642    46659128 :         if ( core_brate_inp < brate_intermed_tbl[i] )
     643             :         {
     644     4985529 :             flag_hardcoded = 0;
     645     4985529 :             break;
     646             :         }
     647             : 
     648    41673599 :         i++;
     649             :     }
     650             : 
     651     5226712 :     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     5226712 :     core_brate = brate_intermed_tbl[i];
     658             : 
     659     5226712 :     if ( element_mode > EVS_MONO )
     660             :     {
     661     5132866 :         flag_hardcoded = 0; /* use automatic and flexible ACELP bit-budget allocation */
     662             :     }
     663             : 
     664     5226712 :     if ( core != ACELP_CORE && element_mode == EVS_MONO ) /* needed for mode1 core switching in EVS mono */
     665             :     {
     666         714 :         flag_hardcoded = 1;
     667             :     }
     668             : 
     669             :     /*-----------------------------------------------------------------*
     670             :      * ACELP bit allocation
     671             :      *-----------------------------------------------------------------*/
     672             : 
     673     5226712 :     if ( !( coder_type == TRANSITION && tc_subfr != -1 ) || enc_dec == DEC )
     674             :     {
     675             :         /* Set the bit-budget */
     676     4998406 :         bits = (int16_t) ( core_brate_inp / FRAMES_PER_SEC );
     677             : 
     678     4998406 :         if ( coder_type == TRANSITION && enc_dec == DEC && tc_call == 1 )
     679             :         {
     680      289388 :             bits += *nBits_es_Pred; /* equalize for 4th signaling bit estimated at the encoder in TC_0_192 */
     681             :         }
     682             : 
     683             :         /* Subtract signaling bits */
     684     4998406 :         if ( enc_dec == DEC && idchan == 1 && element_mode > EVS_MONO )
     685             :         {
     686       37938 :             bits -= TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS;
     687             : 
     688       37938 :             if ( tdm_LRTD_flag == 1 )
     689             :             {
     690       35006 :                 bits += STEREO_BITS_TCA;
     691             :             }
     692             : 
     693             :             /* subtract TBE/BWE flag */
     694       37938 :             if ( extl_brate > 0 && ( extl == WB_TBE || extl == SWB_TBE || extl == FB_TBE || extl == WB_BWE || extl == SWB_BWE || extl == FB_BWE ) )
     695             :             {
     696       26123 :                 bits--;
     697             :             }
     698             :         }
     699             :         else
     700             :         {
     701             :             /* Subtract signaling bits */
     702     4960468 :             bits -= signaling_bits;
     703             :         }
     704             : 
     705     4998406 :         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     3904397 :             bits++;
     709             :         }
     710             : 
     711             :         /*-----------------------------------------------------------------*
     712             :          * LSF Q bit-budget
     713             :          *-----------------------------------------------------------------*/
     714             : 
     715     4998406 :         if ( !tdm_lp_reuse_flag || idchan == 0 )
     716             :         {
     717             :             /* LSF Q bit-budget */
     718     4992364 :             acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     719             : 
     720     4992364 :             if ( !flag_hardcoded )
     721             :             {
     722     4903564 :                 if ( L_frame == L_FRAME )
     723             :                 {
     724     2821108 :                     if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
     725             :                     {
     726      546522 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     727             :                     }
     728     2274586 :                     else if ( ( total_brate < 7200 || coder_type == INACTIVE || coder_type == AUDIO ) && idchan == 1 )
     729             :                     {
     730             :                         /* TD stereo, secondary channel: do nothing */
     731       12799 :                         acelp_cfg->lsf_bits = LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     732             :                     }
     733     2261787 :                     else if ( element_mode > EVS_MONO && coder_type == AUDIO && brate_intermed_tbl[i] < ACELP_9k60 )
     734             :                     {
     735             :                         /* primary channel: do nothing */
     736             :                     }
     737     2132916 :                     else if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] >= ACELP_9k60*/ )
     738             :                     {
     739      267192 :                         acelp_cfg->lsf_bits = 42;
     740             :                     }
     741     1865724 :                     else if ( total_brate <= 9600 || coder_type == UNVOICED )
     742             :                     {
     743      260418 :                         acelp_cfg->lsf_bits = 31;
     744             :                     }
     745     1605306 :                     else if ( total_brate <= 20000 )
     746             :                     {
     747     1510990 :                         acelp_cfg->lsf_bits = 36;
     748             :                     }
     749             :                     else
     750             :                     {
     751       94316 :                         acelp_cfg->lsf_bits = 41;
     752             :                     }
     753             :                 }
     754             :                 else /* L_frame == L_FRAME16k */
     755             :                 {
     756     2082456 :                     acelp_cfg->lsf_bits = 41;
     757             :                 }
     758             :             }
     759             : 
     760     4992364 :             bits -= acelp_cfg->lsf_bits;
     761             : 
     762             :             /* mid-LSF Q bit-budget */
     763     4992364 :             acelp_cfg->mid_lsf_bits = mid_LSF_bits_tbl[LSF_BIT_ALLOC_IDX( core_brate, coder_type )];
     764             : 
     765     4992364 :             if ( element_mode > EVS_MONO && coder_type == AUDIO /*&& brate_intermed_tbl[i] < ACELP_9k60*/ )
     766             :             {
     767      396063 :                 acelp_cfg->mid_lsf_bits = 5;
     768             :                 /* primary channel: do nothing */
     769             :             }
     770             : 
     771     4992364 :             bits -= acelp_cfg->mid_lsf_bits;
     772             :         }
     773        6042 :         else if ( tdm_lp_reuse_flag == 1 && idchan == 1 && active_cnt != 1 )
     774             :         {
     775        6032 :             bits -= TDM_IC_LSF_PRED_BITS;
     776             :         }
     777             : 
     778             :         /* gain Q bit-budget - part 1: 'Es_pred' of memory-less gain Q */
     779     4998406 :         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      723033 :              ( coder_type == INACTIVE && !inactive_coder_type_flag ) /* AVQ inactive */
     781             :         )
     782             :         {
     783     3785065 :             *nBits_es_Pred = Es_pred_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, -1 )];
     784     3785065 :             bits -= *nBits_es_Pred;
     785             :         }
     786     1213341 :         else if ( *uc_two_stage_flag )
     787             :         {
     788       51629 :             *nBits_es_Pred = 4;
     789       51629 :             bits -= *nBits_es_Pred;
     790             :         }
     791             :     }
     792             :     else
     793             :     {
     794      228306 :         bits = *unbits;
     795             :     }
     796             : 
     797     5226712 :     if ( coder_type == TRANSITION && tc_call == 0 )
     798             :     {
     799      154676 :         *unbits = bits;
     800      154676 :         return error;
     801             :     }
     802             : 
     803             :     /*-----------------------------------------------------------------*
     804             :      * Low-rate mode - bits are allocated in tdm_low_rate_enc()
     805             :      *-----------------------------------------------------------------*/
     806             : 
     807     5072036 :     if ( element_mode == IVAS_SCE && tdm_low_rate_mode )
     808             :     {
     809      546522 :         acelp_cfg->FEC_mode = 0;
     810      546522 :         acelp_cfg->ltf_mode = FULL_BAND;
     811      546522 :         *nBits_es_Pred = 0;
     812      546522 :         *unbits = 0;
     813      546522 :         acelp_cfg->ubits = 0;
     814             : 
     815      546522 :         return error;
     816             :     }
     817             : 
     818             :     /*-----------------------------------------------------------------*
     819             :      * Supplementary information for FEC
     820             :      *-----------------------------------------------------------------*/
     821             : 
     822     4525514 :     acelp_cfg->FEC_mode = 0;
     823     4525514 :     if ( core_brate >= ACELP_11k60 && ( idchan == 0 || element_mode == EVS_MONO ) )
     824             :     {
     825     3645773 :         acelp_cfg->FEC_mode = 1;
     826             : 
     827     3645773 :         if ( coder_type > UNVOICED && coder_type < AUDIO && coder_type != VOICED )
     828             :         {
     829     3194252 :             bits -= FEC_BITS_CLS;
     830             :         }
     831             : 
     832     3645773 :         if ( coder_type != TRANSITION )
     833             :         {
     834     3137968 :             if ( total_brate >= ACELP_16k40 )
     835             :             {
     836     2006350 :                 acelp_cfg->FEC_mode = 2;
     837             : 
     838     2006350 :                 if ( coder_type > UNVOICED && coder_type < AUDIO )
     839             :                 {
     840     1890170 :                     bits -= FEC_BITS_ENR;
     841             :                 }
     842             :             }
     843             : 
     844     3137968 :             if ( total_brate >= ACELP_32k )
     845             :             {
     846      276444 :                 acelp_cfg->FEC_mode = 3;
     847             : 
     848      276444 :                 if ( coder_type > UNVOICED && coder_type < AUDIO )
     849             :                 {
     850      258962 :                     bits -= FEC_BITS_POS;
     851             :                 }
     852             :             }
     853             :         }
     854             :     }
     855             : 
     856             :     /*-----------------------------------------------------------------*
     857             :      * LP filtering of the adaptive excitation
     858             :      *-----------------------------------------------------------------*/
     859             : 
     860     4525514 :     if ( idchan > 0 && element_mode > EVS_MONO )
     861             :     {
     862       66570 :         acelp_cfg->ltf_mode = FULL_BAND;
     863             :     }
     864     4458944 :     else if ( coder_type == UNVOICED )
     865             :     {
     866       49714 :         acelp_cfg->ltf_mode = FULL_BAND;
     867             :     }
     868     4409230 :     else if ( ( coder_type == GENERIC || coder_type == TRANSITION ) && core_brate < ACELP_11k60 )
     869             :     {
     870      434486 :         acelp_cfg->ltf_mode = LOW_PASS;
     871             :     }
     872     3974744 :     else if ( core_brate >= ACELP_11k60 && ( coder_type != AUDIO && !( coder_type == INACTIVE && L_frame == L_FRAME ) ) )
     873             :     {
     874     3412748 :         if ( coder_type == INACTIVE && L_frame == L_FRAME16k && inactive_coder_type_flag ) /* GSC Inactive @16kHz */
     875             :         {
     876       71344 :             acelp_cfg->ltf_mode = FULL_BAND;
     877             :         }
     878             :         else
     879             :         {
     880     3341404 :             acelp_cfg->ltf_mode = NORMAL_OPERATION;
     881     3341404 :             if ( coder_type != TRANSITION )
     882             :             {
     883     2833599 :                 bits -= nb_subfr;
     884             :             }
     885             :         }
     886             :     }
     887             :     else
     888             :     {
     889      561996 :         acelp_cfg->ltf_mode = FULL_BAND;
     890             :     }
     891             : 
     892             :     /*-----------------------------------------------------------------*
     893             :      * UC bit-budget
     894             :      *-----------------------------------------------------------------*/
     895             : 
     896     4525514 :     if ( ( ( coder_type == UNVOICED && !( *uc_two_stage_flag ) ) || ( coder_type == INACTIVE && core_brate <= ACELP_9k60 ) ) && ( idchan == 0 || element_mode == EVS_MONO ) )
     897             :     {
     898       13231 :         bits -= NBITS_NOISENESS; /* noiseness */
     899             :     }
     900     4525514 :     if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
     901             :     {
     902         670 :         bits -= ( 3 * NB_SUBFR ); /* tilt factor */
     903             :     }
     904             : 
     905             :     /*-----------------------------------------------------------------*
     906             :      * TC bit-budget
     907             :      *-----------------------------------------------------------------*/
     908             : 
     909     4525514 :     fix_first = 0;
     910     4525514 :     if ( coder_type == TRANSITION )
     911             :     {
     912      591347 :         if ( tc_call == 2 )
     913             :         {
     914      127288 :             fix_first = 1;
     915             :         }
     916             : 
     917             :         /* TC signaling */
     918      591347 :         if ( L_frame == L_FRAME )
     919             :         {
     920      361120 :             if ( tc_subfr == TC_0_0 )
     921             :             {
     922       70441 :                 if ( enc_dec == ENC )
     923             :                 {
     924       39990 :                     bits -= 1; /* TC signaling */
     925             :                 }
     926             : 
     927       70441 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     928             :                 {
     929       55382 :                     bits -= 3; /* LP filtering flag */
     930             :                 }
     931             :             }
     932      290679 :             else if ( tc_subfr == TC_0_64 )
     933             :             {
     934       41356 :                 if ( enc_dec == ENC )
     935             :                 {
     936        9296 :                     bits -= 4; /* TC signaling */
     937             :                 }
     938             : 
     939       41356 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     940             :                 {
     941       29754 :                     bits -= 3; /* LP filtering flag */
     942             :                 }
     943             :             }
     944      249323 :             else if ( tc_subfr == TC_0_128 )
     945             :             {
     946       14343 :                 if ( enc_dec == ENC )
     947             :                 {
     948        5711 :                     bits -= 4; /* TC signaling */
     949             :                 }
     950             : 
     951       14343 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     952             :                 {
     953       10880 :                     bits -= 2; /* LP filtering flag */
     954             :                 }
     955             :             }
     956      234980 :             else if ( tc_subfr == TC_0_192 )
     957             :             {
     958        4664 :                 if ( enc_dec == ENC )
     959             :                 {
     960        2154 :                     bits -= 3; /* TC signaling */
     961             :                 }
     962             : 
     963        4664 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     964             :                 {
     965        3547 :                     bits -= 1; /* LP filtering flag */
     966             :                 }
     967             :             }
     968      230316 :             else if ( tc_subfr == L_SUBFR )
     969             :             {
     970       38994 :                 if ( enc_dec == ENC )
     971             :                 {
     972       21528 :                     bits -= 3; /* TC signaling */
     973             :                 }
     974             : 
     975       38994 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     976             :                 {
     977       28684 :                     bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
     978             :                 }
     979             :             }
     980             :             else
     981             :             {
     982      191322 :                 if ( enc_dec == ENC )
     983             :                 {
     984       70160 :                     bits -= 4; /* TC signaling */
     985             :                 }
     986             : 
     987      191322 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     988             :                 {
     989      149331 :                     bits -= ( L_FRAME - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
     990             :                 }
     991             :             }
     992             :         }
     993             :         else /* L_frame == L_FRAME16k */
     994             :         {
     995      230227 :             if ( enc_dec == ENC )
     996             :             {
     997       79467 :                 if ( tc_subfr <= 2 * L_SUBFR )
     998             :                 {
     999       53844 :                     bits -= 2; /* TC signaling */
    1000             :                 }
    1001             :                 else
    1002             :                 {
    1003       25623 :                     bits -= 3; /* TC signaling */
    1004             :                 }
    1005             :             }
    1006             : 
    1007      230227 :             bits -= ( L_FRAME16k - tc_subfr - L_SUBFR ) / L_SUBFR; /* LP filtering flag */
    1008             :         }
    1009             : 
    1010             :         /* glottal-shape codebook bits */
    1011      591347 :         bits -= ( 3 + 6 + 1 + 3 );
    1012             :     }
    1013             : 
    1014             :     /*-----------------------------------------------------------------*
    1015             :      * pitch, innovation, gains bit-budget
    1016             :      *-----------------------------------------------------------------*/
    1017             : 
    1018     4525514 :     acelp_cfg->fcb_mode = 0;
    1019             : 
    1020     4525514 :     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         542 :         set_s( acelp_cfg->pitch_bits, 0, NB_SUBFR16k );
    1023         542 :         set_s( acelp_cfg->gains_mode, 0, NB_SUBFR16k );
    1024             : 
    1025        1626 :         for ( i = 0; i < 2; i++ )
    1026             :         {
    1027        1084 :             acelp_cfg->pitch_bits[i] = 0;
    1028        1084 :             if ( tdm_Pitch_reuse_flag == 0 )
    1029             :             {
    1030        1084 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, 2 * i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1031        1084 :                 bits -= acelp_cfg->pitch_bits[i];
    1032             :             }
    1033        1084 :             acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, GENERIC, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1034        1084 :             bits -= acelp_cfg->gains_mode[i];
    1035             :         }
    1036         542 :         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         542 :             if ( bits >= 16 )
    1047             :         {
    1048         542 :             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         542 :         acelp_cfg->fixed_cdk_index[2] = -1;
    1056         542 :         acelp_cfg->fixed_cdk_index[3] = -1;
    1057             :     }
    1058     4524972 :     else if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) ||           /* @12.8kHz core except of GSC */
    1059     2115354 :               ( nb_subfr == NB_SUBFR16k && ( !inactive_coder_type_flag || coder_type != INACTIVE ) ) /* @16kHz core GC, TC, AVQ inactive */
    1060      550898 :               || core == HQ_CORE /* ACELP -> HQ switching in EVS */ )
    1061             :     {
    1062             :         /* pitch Q & gain Q bit-budget - part 2*/
    1063    21915808 :         for ( i = 0; i < nb_subfr; i++ )
    1064             :         {
    1065    17941020 :             if ( L_frame == L_FRAME )
    1066             :             {
    1067     7720403 :                 if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
    1068             :                 {
    1069        1080 :                     acelp_cfg->pitch_bits[i] = 0;
    1070             :                 }
    1071             :                 else
    1072             :                 {
    1073     7719323 :                     acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, i * L_SUBFR, TC_SUBFR2IDX( tc_subfr ) )];
    1074             :                 }
    1075     7720403 :                 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    10220617 :                 if ( tdm_Pitch_reuse_flag == 1 && idchan == 1 )
    1080             :                 {
    1081           0 :                     acelp_cfg->pitch_bits[i] = 0;
    1082             :                 }
    1083             :                 else
    1084             :                 {
    1085    10220617 :                     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    10220617 :                 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    17941020 :             bits -= acelp_cfg->pitch_bits[i];
    1091             : 
    1092    17941020 :             if ( coder_type == INACTIVE && acelp_cfg->gains_mode[i] == 6 /* VQ vs. SQ threshold @32 kbps */ )
    1093             :             {
    1094       75335 :                 bits -= 5;
    1095             :             }
    1096             :             else
    1097             :             {
    1098    17865685 :                 if ( *uc_two_stage_flag == 1 )
    1099             :                 {
    1100      206516 :                     acelp_cfg->gains_mode[i] = 7;
    1101             :                 }
    1102             : 
    1103    17865685 :                 bits -= acelp_cfg->gains_mode[i];
    1104             :             }
    1105             :         }
    1106             : 
    1107             :         /* algebraic codebook bit-budget */
    1108     3974788 :         if ( flag_hardcoded || /* EVS */
    1109     3895335 :              ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1110      158850 :              ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1111             :         {
    1112     2353919 :             for ( i = 0; i < nb_subfr; i++ )
    1113             :             {
    1114     1953483 :                 if ( L_frame == L_FRAME )
    1115             :                 {
    1116      183511 :                     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     1769972 :                     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     1953483 :                 bits -= acelp_cfg->fixed_cdk_index[i];
    1123             :             }
    1124             :         }
    1125     3574352 :         else if ( !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1126             :         {
    1127     3574144 :             if ( coder_type == UNVOICED && !( *uc_two_stage_flag ) )
    1128             :             {
    1129          14 :                 i = bits / NB_SUBFR;
    1130          14 :                 if ( i % 2 == 0 )
    1131             :                 {
    1132          14 :                     i--; /* must be odd */
    1133             :                 }
    1134          14 :                 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          14 :                 i = max( i, 0 ); /* If i == 0-> random noise generator will be used as FCB */
    1140          14 :                 set_s( acelp_cfg->fixed_cdk_index, i, NB_SUBFR );
    1141          14 :                 bits -= ( i * NB_SUBFR );
    1142             :             }
    1143             :             else
    1144             :             {
    1145             : 
    1146     3574130 :                 acelp_cfg->fcb_mode = 1;
    1147     3574130 :                 if ( element_mode == IVAS_CPE_TD )
    1148             :                 {
    1149      116260 :                     if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr ) ) /* enough bits for all fcb */
    1150             :                     {
    1151      116095 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr, L_SUBFR, coder_type, tc_subfr, fix_first );
    1152             :                     }
    1153         165 :                     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         165 :                     else if ( bits >= ACELP_FIXED_CDK_BITS( 0 ) * ( nb_subfr - 2 ) )
    1159             :                     {
    1160         165 :                         acelp_FCB_allocator( &bits, acelp_cfg->fixed_cdk_index, nb_subfr - 2, L_SUBFR, coder_type, tc_subfr, fix_first );
    1161         165 :                         acelp_cfg->fixed_cdk_index[2] = acelp_cfg->fixed_cdk_index[1];
    1162         165 :                         acelp_cfg->fixed_cdk_index[1] = -1;
    1163         165 :                         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     3457870 :                 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     3457870 :                     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     3974788 :         if ( ( core_brate_inp >= MIN_BRATE_AVQ_EXC && coder_type != INACTIVE ) /* high-birate ACELP except IC */ ||
    1198      162044 :              ( !inactive_coder_type_flag && coder_type == INACTIVE ) /* AVQ inactive */ )
    1199             :         {
    1200     2123286 :             for ( i = 0; i < nb_subfr; i++ )
    1201             :             {
    1202     1769405 :                 if ( flag_hardcoded )
    1203             :                 {
    1204      164490 :                     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      164490 :                         bits -= acelp_cfg->AVQ_cdk_bits[i];
    1207             :                     }
    1208             :                 }
    1209             : 
    1210     1769405 :                 bits -= G_AVQ_BITS;
    1211             :             }
    1212             : 
    1213      353881 :             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      159508 :                 bits--;
    1217             :             }
    1218             : 
    1219      353881 :             if ( !flag_hardcoded )
    1220             :             {
    1221             :                 int16_t bit_tmp;
    1222             : 
    1223      320983 :                 bit_tmp = bits / nb_subfr;
    1224      320983 :                 set_s( acelp_cfg->AVQ_cdk_bits, bit_tmp, nb_subfr );
    1225      320983 :                 bits -= bit_tmp * nb_subfr;
    1226             : 
    1227      320983 :                 bit_tmp = bits % nb_subfr;
    1228      320983 :                 acelp_cfg->AVQ_cdk_bits[0] += bit_tmp;
    1229      320983 :                 bits -= bit_tmp;
    1230             :             }
    1231             :         }
    1232             :     }
    1233      550184 :     else if ( ( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) /* LBR secondary channel in TD stereo */ ||
    1234      550184 :               ( ( coder_type == INACTIVE || coder_type == AUDIO ) && nb_subfr == NB_SUBFR ) /* GSC @12.8kHz */ ||
    1235       71344 :               ( 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      550184 :         if ( GSC_IVAS_mode > 0 && ( GSC_noisy_speech || core_brate > GSC_H_RATE_STG ) )
    1242             :         {
    1243       16554 :             Local_BR = ACELP_8k00;
    1244       16554 :             Pitch_CT = GENERIC;
    1245       16554 :             Pitch_BR = ACELP_8k00;
    1246       16554 :             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      533630 :         else if ( GSC_noisy_speech )
    1257             :         {
    1258        5698 :             Local_BR = ACELP_7k20;
    1259        5698 :             Pitch_CT = GENERIC;
    1260        5698 :             Pitch_BR = ACELP_7k20;
    1261        5698 :             if ( L_frame == L_FRAME16k )
    1262             :             {
    1263           0 :                 Pitch_BR = core_brate;
    1264             :             }
    1265             :         }
    1266             :         else
    1267             :         {
    1268      527932 :             Local_BR = ACELP_7k20;
    1269      527932 :             Pitch_CT = AUDIO;
    1270      527932 :             Pitch_BR = core_brate;
    1271             : 
    1272      527932 :             if ( L_frame == L_FRAME16k )
    1273             :             {
    1274       71344 :                 Local_BR = ACELP_13k20;
    1275       71344 :                 Pitch_CT = GENERIC;
    1276             :             }
    1277             :         }
    1278             : 
    1279     2822264 :         for ( i = 0; i < nb_subfr; i++ )
    1280             :         {
    1281     2272080 :             if ( L_frame == L_FRAME16k )
    1282             :             {
    1283      356720 :                 acelp_cfg->pitch_bits[i] = ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
    1284      356720 :                 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     1915360 :                 acelp_cfg->pitch_bits[i] = ACB_bits_tbl[BIT_ALLOC_IDX( Pitch_BR, Pitch_CT, i * L_SUBFR, 0 )];
    1289     1915360 :                 acelp_cfg->fixed_cdk_index[i] = FCB_bits_tbl[BIT_ALLOC_IDX( Local_BR, LOCAL_CT, i * L_SUBFR, 0 )];
    1290     1915360 :                 acelp_cfg->gains_mode[i] = gain_bits_tbl[BIT_ALLOC_IDX( ACELP_7k20, LOCAL_CT, i * L_SUBFR, 0 )];
    1291             :             }
    1292             :         }
    1293             :     }
    1294             : 
    1295     4525514 :     if ( coder_type == TRANSITION && ( tc_call == 1 && tc_subfr == 0 && L_frame == L_FRAME ) )
    1296             :     {
    1297      110809 :         return error;
    1298             :     }
    1299             : 
    1300             :     /*-----------------------------------------------------------------*
    1301             :      * unused bits handling
    1302             :      *-----------------------------------------------------------------*/
    1303             : 
    1304     4414705 :     acelp_cfg->ubits = 0; /* these bits could be reused for something else */
    1305             : 
    1306     4414705 :     if ( flag_hardcoded && core_brate != PPP_NELP_2k80 )
    1307             :     {
    1308             :         /* unused bits */
    1309       88300 :         if ( coder_type == AUDIO || ( coder_type == INACTIVE && core_brate <= ACELP_24k40 ) )
    1310             :         {
    1311       11445 :             acelp_cfg->ubits = 0;
    1312             :         }
    1313       76855 :         else if ( L_frame == L_FRAME )
    1314             :         {
    1315       43390 :             acelp_cfg->ubits = reserved_bits_tbl[BIT_ALLOC_IDX( core_brate, coder_type, -1, TC_SUBFR2IDX( tc_subfr ) )];
    1316             :         }
    1317             :         else
    1318             :         {
    1319       33465 :             acelp_cfg->ubits = 0;
    1320             :         }
    1321             : 
    1322       88300 :         bits -= acelp_cfg->ubits;
    1323             :     }
    1324             : 
    1325             :     /* sanity check */
    1326     4414705 :     if ( ( coder_type != INACTIVE && nb_subfr == NB_SUBFR && coder_type != AUDIO ) || nb_subfr == NB_SUBFR16k )
    1327             :     {
    1328     3935151 :         if ( ( L_frame == L_FRAME16k && coder_type == INACTIVE && inactive_coder_type_flag ) /* GSC Inactive @16kHz */ ||
    1329         889 :              ( GSC_IVAS_mode > 0 && L_frame == L_FRAME16k ) ) /* IVAS GSC @16kHz */
    1330             :         {
    1331       71344 :             acelp_cfg->ubits = 0;
    1332             :         }
    1333     3863807 :         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     3863807 :         else if ( bits > 0 && !( coder_type == UNVOICED && tdm_low_rate_mode == 1 && element_mode == IVAS_CPE_TD ) )
    1340             :         {
    1341     1390485 :             if ( idchan > 0 && element_mode == IVAS_CPE_TD )
    1342             :             {
    1343       24535 :                 if ( !tdm_lp_reuse_flag )
    1344             :                 {
    1345       22958 :                     acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
    1346       22958 :                     bits = 0;
    1347             :                 }
    1348             :                 else
    1349             :                 {
    1350        1577 :                     int16_t nb_prm = 4;
    1351             : 
    1352        1577 :                     if ( tdm_low_rate_mode == 1 )
    1353             :                     {
    1354           0 :                         nb_prm = 2;
    1355             :                     }
    1356             :                     /* First add remaining bits on gains */
    1357        1577 :                     bits -= allocate_unused( core_brate, coder_type, bits, nb_prm, 0, GAINSPRM, acelp_cfg->gains_mode );
    1358             : 
    1359             :                     /* Then, Increase pitch bit budget */
    1360        1577 :                     if ( tdm_Pitch_reuse_flag == 0 && bits > 0 )
    1361             :                     {
    1362           6 :                         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        1577 :                     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     1365950 :             else if ( core == ACELP_CORE && coder_type >= UNVOICED && coder_type <= GENERIC && L_frame == L_FRAME )
    1381             :             {
    1382      766893 :                 acelp_cfg->lsf_bits += bits; /* increase LSF Q bits */
    1383             : 
    1384      766893 :                 if ( acelp_cfg->lsf_bits > 46 )
    1385             :                 {
    1386           2 :                     acelp_cfg->ubits = acelp_cfg->lsf_bits - 46;
    1387           2 :                     acelp_cfg->lsf_bits = 46;
    1388             :                 }
    1389      766891 :                 else if ( acelp_cfg->lsf_bits > 42 && L_frame == L_FRAME )
    1390             :                 {
    1391        4363 :                     acelp_cfg->ubits = acelp_cfg->lsf_bits - 42;
    1392        4363 :                     acelp_cfg->lsf_bits = 42;
    1393             :                 }
    1394             :             }
    1395             :             else
    1396             :             {
    1397      599057 :                 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     4414705 :     return error;
    1409             : }
    1410             : 
    1411             : /*-------------------------------------------------------------------*
    1412             :  * allocate_unused()
    1413             :  *
    1414             :  * Allocate unused bits
    1415             :  *--------------------------------------------------------------------*/
    1416             : 
    1417        6332 : 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        6332 :     int16_t max_bit_per_pos = 0, bit_added = 0;
    1427             : 
    1428        6332 :     if ( prm_type == GAINSPRM )
    1429             :     {
    1430        6308 :         max_bit_per_pos = 6;
    1431        6308 :         if ( core_brate > ACELP_8k00 )
    1432             :         {
    1433        5548 :             max_bit_per_pos = 7;
    1434             :         }
    1435         760 :         else if ( coder_type != UNVOICED )
    1436             :         {
    1437         760 :             if ( subfr >= 1 )
    1438             :             {
    1439         570 :                 max_bit_per_pos = 7;
    1440             :             }
    1441         190 :             else if ( subfr == 0 )
    1442             :             {
    1443         190 :                 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          24 :     else if ( prm_type == PITCHPRM )
    1452             :     {
    1453          24 :         max_bit_per_pos = 6;
    1454          24 :         if ( subfr == 0 || subfr == 2 || nb_prm == 2 )
    1455             :         {
    1456          12 :             max_bit_per_pos = 10;
    1457             :         }
    1458             : 
    1459          24 :         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        6332 :     max_bit_per_pos = min( unused_bits, max_bit_per_pos - prm_bit_mode[subfr] );
    1480        6332 :     if ( max_bit_per_pos < 0 )
    1481             :     {
    1482           0 :         return 0;
    1483             :     }
    1484        6332 :     else if ( max_bit_per_pos >= 0 && subfr == ( nb_prm - 1 ) )
    1485             :     {
    1486        1583 :         prm_bit_mode[subfr] += max_bit_per_pos;
    1487             :     }
    1488             :     else
    1489             :     {
    1490        4749 :         prm_bit_mode[subfr] += max_bit_per_pos;
    1491        4749 :         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        6332 :     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    51341140 : 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    51341140 :     if ( element_mode == IVAS_CPE_DFT && idchan == 0 && total_brate <= SID_2k40 && bwidth == WB && cng_type == LP_CNG )
    1516             :     {
    1517             : 
    1518        3371 :         return 1;
    1519             :     }
    1520    51337769 :     else if ( element_mode == IVAS_CPE_TD )
    1521             :     {
    1522      135532 :         if ( element_brate >= IVAS_24k4 && idchan == 0 && ( tdm_LRTD_flag == 0 || element_brate > IVAS_24k4 ) )
    1523             :         {
    1524       47581 :             return 1;
    1525             :         }
    1526             :         else
    1527             :         {
    1528       87951 :             return 0;
    1529             :         }
    1530             :     }
    1531    51202237 :     else if ( element_mode == IVAS_CPE_DFT )
    1532             :     {
    1533     2000506 :         if ( element_brate >= IVAS_24k4 )
    1534             :         {
    1535     1035382 :             return 1;
    1536             :         }
    1537             :         else
    1538             :         {
    1539      965124 :             return 0;
    1540             :         }
    1541             :     }
    1542    49201731 :     else if ( element_mode == IVAS_SCE )
    1543             :     {
    1544    11857721 :         if ( element_brate >= SCE_CORE_16k_LOW_LIMIT )
    1545             :         {
    1546     8742004 :             return 1;
    1547             :         }
    1548             :         else
    1549             :         {
    1550     3115717 :             return 0;
    1551             :         }
    1552             :     }
    1553    37344010 :     else if ( total_brate >= ACELP_16k_LOW_LIMIT ) /* EVS_MONO */
    1554             :     {
    1555    36841598 :         return 1;
    1556             :     }
    1557             :     else
    1558             :     {
    1559      502412 :         return 0;
    1560             :     }
    1561             : }

Generated by: LCOV version 1.14