LCOV - code coverage report
Current view: top level - lib_enc - enc_prm.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 198 282 70.2 %
Date: 2025-09-11 08:49:07 Functions: 6 7 85.7 %

          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 <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * writeTCXMode()
      51             :  *
      52             :  * write TCX mode
      53             :  *--------------------------------------------------------------------*/
      54             : 
      55      877966 : void writeTCXMode(
      56             :     Encoder_State *st,      /* i/o: encoder state structure              */
      57             :     BSTR_ENC_HANDLE hBstr,  /* i/o: bitstream handle                     */
      58             :     const int16_t MCT_flag, /* i  : hMCT handle allocated (1) or not (0) */
      59             :     int16_t *nbits_start    /* o  : nbits start                          */
      60             : )
      61             : {
      62             :     uint16_t index;
      63             :     int16_t idx, start_idx;
      64             :     int16_t nBits;
      65             : 
      66      877966 :     if ( st->tcxonly )
      67             :     {
      68      748402 :         push_next_indice( hBstr, st->core == TCX_10_CORE, 1 );
      69             : 
      70      748402 :         if ( st->clas == UNVOICED_CLAS )
      71             :         {
      72      389350 :             index = 0;
      73             :         }
      74      359052 :         else if ( st->clas == VOICED_TRANSITION || st->clas == UNVOICED_TRANSITION )
      75             :         {
      76       45011 :             index = 1;
      77             :         }
      78      314041 :         else if ( st->clas == VOICED_CLAS )
      79             :         {
      80      273383 :             index = 2;
      81             :         }
      82             :         else
      83             :         {
      84       40658 :             index = 3;
      85             :         }
      86             : 
      87      748402 :         push_next_indice( hBstr, index, 2 );
      88             : 
      89      748402 :         if ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag )
      90             :         {
      91      165282 :             push_next_indice( hBstr, st->vad_flag, 1 );
      92             :         }
      93             :     }
      94             :     else
      95             :     {
      96      129564 :         if ( st->core == ACELP_CORE )
      97             :         {
      98             :             /* write the RF signaling information */
      99         604 :             if ( st->rf_mode == 1 )
     100             :             {
     101             :                 /* find the section in the ACELP signaling table corresponding to bitrate */
     102           0 :                 idx = 0;
     103           0 :                 while ( acelp_sig_tbl[idx] != st->total_brate ) /* total bitrate is kept at 13.2kbps */
     104             :                 {
     105           0 :                     idx++;
     106             :                 }
     107             : 
     108             :                 /* retrieve the number of bits for signaling */
     109           0 :                 nBits = (int16_t) acelp_sig_tbl[++idx];
     110             : 
     111             :                 /* retrieve the signaling index */
     112           0 :                 start_idx = ++idx;
     113           0 :                 while ( acelp_sig_tbl[idx] != SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode ) )
     114             :                 {
     115           0 :                     idx++;
     116             :                 }
     117           0 :                 push_next_indice( hBstr, idx - start_idx, nBits );
     118           0 :                 push_next_indice( hBstr, 0, 1 ); /* Indicate to the decoder that the core is ACELP*/
     119           0 :                 *nbits_start = 3;
     120             :             }
     121             :             else
     122             :             {
     123         604 :                 push_next_indice( hBstr, st->coder_type, 3 );
     124             :             }
     125             :         }
     126             :         else
     127             :         {
     128      128960 :             if ( st->mdct_sw == MODE1 )
     129             :             {
     130             :                 /* 2 bits instead of 3 as TCX is already signaled */
     131      128589 :                 push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
     132             :             }
     133             :             else
     134             :             {
     135         371 :                 if ( st->mdct_sw_enable == MODE2 )
     136             :                 {
     137         371 :                     push_next_indice( hBstr, 1, 1 ); /* TCX */
     138         371 :                     push_next_indice( hBstr, 0, 1 ); /* not HQ_CORE */
     139         371 :                     push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
     140             :                 }
     141             :                 else
     142             :                 {
     143             :                     /*write the RF signaling information*/
     144           0 :                     if ( st->rf_mode == 1 )
     145             :                     {
     146             :                         /* find the section in the ACELP signaling table corresponding to bitrate */
     147           0 :                         idx = 0;
     148           0 :                         while ( acelp_sig_tbl[idx] != st->total_brate )
     149             :                         {
     150           0 :                             idx++;
     151             :                         }
     152             : 
     153             :                         /* retrieve the number of bits for signaling */
     154           0 :                         nBits = (int16_t) acelp_sig_tbl[++idx];
     155             : 
     156           0 :                         if ( st->hTcxCfg->coder_type == VOICED || st->hTcxCfg->coder_type == GENERIC || st->hTcxCfg->coder_type == TRANSITION )
     157             :                         {
     158           0 :                             st->sharpFlag = 1;
     159             :                         }
     160             :                         else
     161             :                         {
     162           0 :                             st->sharpFlag = 0;
     163             :                         }
     164             : 
     165             :                         /* retrieve the signaling index */
     166           0 :                         start_idx = ++idx;
     167           0 :                         while ( acelp_sig_tbl[idx] != SIG2IND( st->hTcxCfg->coder_type, st->bwidth, st->sharpFlag, st->rf_mode ) )
     168             :                         {
     169           0 :                             idx++;
     170             :                         }
     171           0 :                         push_next_indice( hBstr, idx - start_idx, nBits );
     172           0 :                         push_next_indice( hBstr, 1, 1 ); /* Indicate to the decoder that the core is TCX*/
     173           0 :                         *nbits_start = 3;
     174             :                     }
     175             :                     else
     176             :                     {
     177           0 :                         push_next_indice( hBstr, ACELP_MODE_MAX + st->hTcxCfg->coder_type, 3 );
     178             :                     }
     179             :                 }
     180             :             }
     181             :         }
     182             :     }
     183             : 
     184      877966 :     return;
     185             : }
     186             : 
     187             : 
     188             : /*-------------------------------------------------------------------*
     189             :  * writeTCXWindowing()
     190             :  *
     191             :  * write TCX transform type
     192             :  *-------------------------------------------------------------------*/
     193             : 
     194      894284 : void writeTCXWindowing(
     195             :     BSTR_ENC_HANDLE hBstr,     /* i/o: bitstream handle        */
     196             :     const int16_t overlap_mode /* i  : overlap mode            */
     197             : )
     198             : {
     199             : #ifdef DEBUGGING
     200             :     assert( overlap_mode != NOT_SUPPORTED && overlap_mode <= ALDO_WINDOW && overlap_mode >= FULL_OVERLAP ); /*1 is not allowed!*/
     201             : #endif
     202             : 
     203      894284 :     if ( overlap_mode == MIN_OVERLAP )
     204             :     {
     205        6812 :         push_next_indice( hBstr, 2, 2 );
     206             :     }
     207      887472 :     else if ( overlap_mode == HALF_OVERLAP )
     208             :     {
     209       12611 :         push_next_indice( hBstr, 3, 2 );
     210             :     }
     211             :     else
     212             :     {
     213      874861 :         push_next_indice( hBstr, 0, 1 );
     214             :     }
     215             : 
     216      894284 :     return;
     217             : }
     218             : 
     219             : 
     220             : /*-------------------------------------------------------------------*
     221             :  * writeLPCparam()
     222             :  *
     223             :  * write LTC parameters
     224             :  *-------------------------------------------------------------------*/
     225             : 
     226      236462 : void writeLPCparam(
     227             :     Encoder_State *st,              /* i/o: encoder state structure     */
     228             :     BSTR_ENC_HANDLE hBstr,          /* i/o: bitstream handle            */
     229             :     const int16_t param_lpc[],      /* i  : LPC parameters to write     */
     230             :     const int16_t bits_param_lpc[], /* i  : bits per LPC parameter      */
     231             :     const int16_t no_param_lpc,     /* i  : number of LPC parameters    */
     232             :     int16_t *nbits_lpc              /* o  : LPC bits written            */
     233             : )
     234             : {
     235             :     int16_t numlpc;
     236             : 
     237      236462 :     if ( st->enableTcxLpc && st->core != ACELP_CORE )
     238             :     {
     239             :         /* Encode the indices */
     240       17457 :         *nbits_lpc = enc_lsf_tcxlpc( &param_lpc, hBstr );
     241             :     }
     242             :     else
     243             :     {
     244      219005 :         if ( st->lpcQuantization == 0 )
     245             :         {
     246             :             /* LPC quantizer */
     247      106898 :             if ( st->core == TCX_20_CORE )
     248             :             {
     249      104821 :                 numlpc = 1;
     250             :             }
     251             :             else
     252             :             {
     253        2077 :                 numlpc = 2;
     254             :             }
     255             : 
     256      106898 :             *nbits_lpc = encode_lpc_avq( hBstr, numlpc, param_lpc, st->core, st->element_mode );
     257             :         }
     258      112107 :         else if ( st->lpcQuantization == 1 )
     259             :         {
     260      112107 :             if ( st->sr_core == INT_FS_16k && st->coder_type == VOICED && st->core == ACELP_CORE )
     261             :             {
     262         268 :                 assert( st->element_mode == EVS_MONO );
     263             : 
     264         268 :                 *nbits_lpc = lsf_bctcvq_encprm( hBstr, param_lpc, bits_param_lpc, no_param_lpc );
     265             :             }
     266             :             else
     267             :             {
     268      111839 :                 *nbits_lpc = lsf_msvq_ma_encprm( hBstr, param_lpc, st->core, st->coder_type, st->acelp_cfg.midLpc, bits_param_lpc, no_param_lpc );
     269             :             }
     270             :         }
     271             :         else
     272             :         {
     273           0 :             assert( 0 );
     274             :         }
     275             :     }
     276             : 
     277      236462 :     return;
     278             : }
     279             : 
     280             : 
     281             : /*-------------------------------------------------------------------*
     282             :  * enc_prm_hm()
     283             :  *
     284             :  *
     285             :  *-------------------------------------------------------------------*/
     286             : 
     287       15771 : static void enc_prm_hm(
     288             :     const int16_t *prm_hm,
     289             :     BSTR_ENC_HANDLE hBstr,
     290             :     const int16_t coder_type,
     291             :     const int16_t L_frame )
     292             : {
     293             :     /* Disable HM for non-GC,VC modes */
     294       15771 :     if ( coder_type != VOICED && coder_type != GENERIC )
     295             :     {
     296        2161 :         return;
     297             :     }
     298             : 
     299             :     /* Flag */
     300       13610 :     push_next_indice( hBstr, prm_hm[0], 1 );
     301             : 
     302       13610 :     if ( prm_hm[0] )
     303             :     {
     304             :         /* Periodicy index */
     305        6868 :         EncodeIndex( L_frame >= L_FRAME, prm_hm[1], hBstr );
     306             : 
     307        6868 :         if ( coder_type == VOICED )
     308             :         {
     309             :             /* Gain index */
     310        2376 :             push_next_indice( hBstr, prm_hm[2], kTcxHmNumGainBits );
     311             :         }
     312             :     }
     313             : 
     314       13610 :     return;
     315             : }
     316             : 
     317             : 
     318             : /*-------------------------------------------------------------------*
     319             :  * writeTCXparam()
     320             :  *
     321             :  * write TCX core parameters
     322             :  *-------------------------------------------------------------------*/
     323             : 
     324     1519011 : void writeTCXparam(
     325             :     Encoder_State *st,          /* i/o: Encoder State handle    */
     326             :     BSTR_ENC_HANDLE hBstr,      /* i/o: bitstream handle        */
     327             :     CONTEXT_HM_CONFIG hm_cfg[], /* i/o: HM config               */
     328             :     int16_t param[],            /* i  : parameters              */
     329             :     const int16_t nbits_header,
     330             :     const int16_t nbits_start,
     331             :     const int16_t nbits_lpc,
     332             :     const int16_t *no_param_tns, /* i  : number of TNS parameters per subframe          */
     333             :     int16_t p_param[2],          /* i/o: pointer to parameters from previous bs writing */
     334             :     const int16_t target_bitsTCX10[2],
     335             :     const int16_t pre_past_flag )
     336             : {
     337             :     int16_t *prm;
     338             :     int16_t j, k, nSubframes, core, last_core;
     339             :     int16_t lg, lgFB, hm_size, flag_ctx_hm;
     340             :     int16_t total_nbbits, nbits_igf, nbits_tcx;
     341             :     int16_t nTnsParams, nTnsBits;
     342             :     int16_t pre_part, post_part;
     343             : #ifdef DEBUG_PLOT_BITS
     344             :     int16_t tmp = hBstr->nb_bits_tot;
     345             : #endif
     346             : 
     347     1519011 :     if ( pre_past_flag == 0 )
     348             :     {
     349      641504 :         pre_part = 1;
     350      641504 :         post_part = 0;
     351             :     }
     352      877507 :     else if ( pre_past_flag == 1 )
     353             :     {
     354      641649 :         pre_part = 0;
     355      641649 :         post_part = 1;
     356             :     }
     357             :     else
     358             :     {
     359      235858 :         pre_part = 1;
     360      235858 :         post_part = 1;
     361             :     }
     362             : 
     363             :     /* Initialization */
     364     1519011 :     core = st->core;
     365     1519011 :     last_core = st->last_core;
     366     1519011 :     nbits_igf = 0;
     367             : 
     368     1519011 :     nSubframes = 1;
     369     1519011 :     if ( core == TCX_10_CORE )
     370             :     {
     371       31767 :         nSubframes = 2;
     372             :     }
     373             : 
     374             :     /* loop over subframes */
     375     3069789 :     for ( k = 0; k < nSubframes; k++ )
     376             :     {
     377     1550778 :         if ( st->element_mode < IVAS_CPE_MDCT && k == 0 && st->igf && core == TCX_10_CORE )
     378             :         {
     379        2014 :             nbits_igf = IGFEncWriteConcatenatedBitstream( st->hIGFEnc, hBstr );
     380             :         }
     381             : 
     382     1550778 :         flag_ctx_hm = 0;
     383             : 
     384     1550778 :         prm = param + ( k * NPRM_DIV );
     385     1550778 :         j = 0;
     386             : 
     387     1550778 :         nbits_tcx = total_nbbits = hBstr->nb_bits_tot - nbits_start;
     388             : 
     389     1550778 :         if ( pre_part && st->enablePlcWaveadjust && k == ( nSubframes - 1 ) )
     390             :         {
     391           0 :             push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
     392             :         }
     393             : 
     394     1550778 :         if ( post_part )
     395             :         {
     396             :             /* TCX Gain */
     397      894429 :             push_next_indice( hBstr, prm[j++], NBITS_TCX_GAIN );
     398             : 
     399             :             /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
     400      894429 :             push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
     401             :         }
     402             :         else
     403             :         {
     404      656349 :             j += 1 + NOISE_FILL_RANGES;
     405             :         }
     406             : 
     407             :         /* LTP data */
     408     1550778 :         if ( pre_part )
     409             :         {
     410      894284 :             if ( ( k == 0 ) && ( st->hTcxEnc->tcxltp || ( st->sr_core > 25600 ) ) ) /* PLC pitch info for HB */
     411             :             {
     412      877362 :                 if ( prm[j] )
     413             :                 {
     414      589936 :                     push_next_indice( hBstr, 1, 1 );
     415      589936 :                     push_next_indice( hBstr, prm[j + 1], 9 );
     416      589936 :                     push_next_indice( hBstr, prm[j + 2], 2 );
     417             :                 }
     418             :                 else
     419             :                 {
     420      287426 :                     push_next_indice( hBstr, 0, 1 );
     421             :                 }
     422             : #ifdef DEBUG_PLOT_BITS
     423             :                 tmp = hBstr->nb_bits_tot - tmp;
     424             :                 dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_LTP" );
     425             : #endif
     426             :             }
     427      894284 :             j += 3;
     428             :         }
     429             :         else
     430             :         {
     431      656494 :             j += p_param[k] - 2;
     432             :         }
     433             : 
     434             :         /* TCX spectral data */
     435     1550778 :         lg = st->last_L_frame >> ( nSubframes - 1 );
     436     1550778 :         lgFB = st->hTcxCfg->tcx_coded_lines >> ( nSubframes - 1 );
     437     1550778 :         if ( post_part && k == 0 && last_core == ACELP_CORE )
     438             :         {
     439             :             /* ACE->TCX transition */
     440        9376 :             lg += st->hTcxCfg->tcx_offset;
     441        9376 :             lgFB += lgFB >> ( 3 - nSubframes );
     442             : 
     443        9376 :             if ( st->hTcxCfg->lfacNext < 0 )
     444             :             {
     445        9376 :                 lg -= st->hTcxCfg->lfacNext;
     446             :             }
     447             :         }
     448             : 
     449     1550778 :         if ( pre_part )
     450             :         {
     451             :             /* TNS data */
     452      894284 :             nTnsParams = 0;
     453      894284 :             nTnsBits = 0;
     454             : 
     455      894284 :             if ( st->hTcxCfg->fIsTNSAllowed )
     456             :             {
     457      818302 :                 SetTnsConfig( st->hTcxCfg, core == TCX_20_CORE, ( last_core == ACELP_CORE ) && ( k == 0 ) );
     458             : 
     459      818302 :                 if ( no_param_tns )
     460             :                 {
     461       10729 :                     push_next_indice( hBstr, ( st->hTcxEnc->tnsData[k].nFilters < 0 ? 1 : 0 ), 1 ); /* common_tns_data[] for subframe k */
     462             :                 }
     463      818302 :                 if ( no_param_tns && ( st->hTcxEnc->tnsData[k].nFilters < 0 ) )
     464             :                 {
     465             :                     /* a negative filter count means that the filters are identical to those in the first channel at the same sub-frame */
     466        2581 :                     nTnsParams = no_param_tns[k];
     467             :                 }
     468             :                 else
     469             :                 {
     470      815721 :                     WriteTnsData( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, hBstr, &nTnsBits );
     471             :                 }
     472      818302 :                 if ( no_param_tns )
     473             :                 {
     474       10729 :                     nTnsBits++;
     475             :                 }
     476      818302 :                 j += nTnsParams;
     477             :             }
     478             : #ifdef DEBUG_PLOT_BITS
     479             :             dbgwrite( &nTnsBits, sizeof( int16_t ), 1, 1, "./res/bits_TNS" );
     480             : #endif
     481             : 
     482      894284 :             if ( post_part )
     483             :             {
     484      237935 :                 hm_size = (int16_t) ( 2.0f * st->hTcxCfg->bandwidth * (float) lg );
     485             : 
     486      237935 :                 if ( st->hTcxEnc->tcx_lpc_shaped_ari && last_core != ACELP_CORE && core == TCX_20_CORE )
     487             :                 {
     488       15771 :                     enc_prm_hm( &prm[j], hBstr, st->hTcxCfg->coder_type, hm_size );
     489             :                 }
     490             : 
     491             :                 /*Context HM flag*/
     492      237935 :                 if ( st->hTcxCfg != NULL && st->hTcxCfg->ctx_hm && !( last_core == ACELP_CORE && k == 0 ) )
     493             :                 {
     494      180775 :                     push_next_indice( hBstr, prm[j], 1 );
     495             : 
     496      180775 :                     if ( prm[j] )
     497             :                     {
     498       12942 :                         EncodeIndex( hm_size >= 256, prm[j + 1], hBstr );
     499             : 
     500       12942 :                         flag_ctx_hm = 1;
     501             :                     }
     502             :                 }
     503             :             }
     504      894284 :             j += NPRM_CTX_HM;
     505             : 
     506      894284 :             if ( post_part )
     507             :             {
     508             :                 /* IGF data */
     509      237935 :                 if ( st->igf && core == TCX_20_CORE )
     510             :                 {
     511      223249 :                     st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
     512      223249 :                     IGFEncWriteBitstream( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, ( last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
     513             :                 }
     514             : 
     515      237935 :                 total_nbbits = hBstr->nb_bits_tot - nbits_start;
     516             : 
     517      237935 :                 if ( core == TCX_20_CORE )
     518             :                 {
     519      233781 :                     if ( st->rf_mode )
     520             :                     {
     521           0 :                         total_nbbits += st->rf_target_bits_write;
     522             :                     }
     523      233781 :                     nbits_tcx = st->bits_frame_core - total_nbbits;
     524             :                 }
     525             :                 else /* TCX_10_CORE */
     526             :                 {
     527        4154 :                     nbits_tcx = ( ( st->bits_frame_core - nbits_header - nbits_lpc - nbits_igf + 1 - k ) >> 1 ) - ( total_nbbits - nbits_tcx );
     528             :                 }
     529             :             }
     530             :             else
     531             :             {
     532             :                 /*Context HM flag*/
     533             : #ifdef DEBUGGING
     534             :                 assert( st->hTcxCfg->ctx_hm == 0 );
     535             : #endif
     536      656349 :                 p_param[k] = j;
     537             :             }
     538             :         }
     539             :         else
     540             :         {
     541      656494 :             nbits_tcx = st->bits_frame_channel;
     542      656494 :             if ( core == TCX_10_CORE )
     543             :             {
     544       29690 :                 nbits_tcx = target_bitsTCX10[k] - NBITS_TCX_GAIN - NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL;
     545             :             }
     546             :         }
     547             : 
     548     1550778 :         if ( post_part )
     549             :         {
     550      894429 :             if ( st->hTcxEnc->tcx_lpc_shaped_ari && core == TCX_20_CORE )
     551             :             {
     552       17457 :                 push_next_bits( hBstr, (uint16_t *) &prm[++j], nbits_tcx );
     553       17457 :                 j += nbits_tcx;
     554             :             }
     555             :             else
     556             :             {
     557      876972 :                 if ( st->element_mode > EVS_MONO )
     558             :                 {
     559      876326 :                     RCcontextMapping_encode2_no_mem_s17_LCS( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
     560      876326 :                                                              nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, flag_ctx_hm ? &hm_cfg[k] : NULL );
     561             :                 }
     562             :                 else
     563             :                 {
     564         646 :                     ACcontextMapping_encode2_no_mem_s17_LC( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
     565         646 :                                                             nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, flag_ctx_hm ? &hm_cfg[k] : NULL );
     566             :                 }
     567             :             }
     568             : #ifdef DEBUG_PLOT_BITS
     569             :             if ( core == TCX_10_CORE )
     570             :             {
     571             :                 dbgwrite( &nbits_tcx, sizeof( int16_t ), 1, 1, "./res/bits_RC" );
     572             :             }
     573             : #endif
     574             :         }
     575             :     }
     576             : #ifdef DEBUG_PLOT_BITS
     577             :     if ( pre_part )
     578             :     {
     579             :         if ( nSubframes == 1 )
     580             :         {
     581             :             tmp = 0;
     582             :             dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_TNS" );
     583             :         }
     584             :     }
     585             :     else
     586             :     {
     587             :         if ( nSubframes == 1 )
     588             :         {
     589             :             tmp = 0;
     590             :             dbgwrite( &nbits_tcx, sizeof( int16_t ), 1, 1, "./res/bits_RC" );
     591             :             dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_RC" );
     592             :         }
     593             :     }
     594             : #endif
     595             : 
     596     1519011 :     return;
     597             : }
     598             : 
     599             : 
     600             : /*-----------------------------------------------------------------*
     601             :  * Function  enc_prm_rf()                                          *
     602             :  * ~~~~~~~~~~~~~~~~~~~~~~                                          *
     603             :  *                                                                 *
     604             :  * encode RF parameters for ACELP and TCX partial copy             *
     605             :  *-----------------------------------------------------------------*/
     606             : 
     607           0 : void enc_prm_rf(
     608             :     Encoder_State *st,
     609             :     const int16_t rf_frame_type,
     610             :     const int16_t fec_offset )
     611             : {
     612             :     int16_t sfr, nb_subfr, n, index;
     613             :     int16_t ltp_mode, ltf_mode, gains_mode;
     614             : 
     615           0 :     RF_ENC_HANDLE hRF = st->hRF;
     616           0 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     617           0 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     618             : 
     619           0 :     nb_subfr = st->nb_subfr;
     620             : 
     621             :     /* partial copy bitstream writing */
     622           0 :     if ( rf_frame_type >= RF_TCXFD && rf_frame_type <= RF_TCXTD2 )
     623             :     {
     624             :         /* TCX frames partial copy write */
     625           0 :         if ( rf_frame_type == RF_TCXFD )
     626             :         {
     627           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], lsf_numbits[0] ); /* VQ 1 */
     628           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], lsf_numbits[1] ); /* VQ 2 */
     629           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][2], lsf_numbits[2] ); /* VQ 3 */
     630             :         }
     631             : 
     632             :         /* classification */
     633           0 :         if ( hRF->rf_clas[fec_offset] == UNVOICED_CLAS )
     634             :         {
     635           0 :             index = 0;
     636             :         }
     637           0 :         else if ( ( hRF->rf_clas[fec_offset] == VOICED_TRANSITION ) || ( hRF->rf_clas[fec_offset] == UNVOICED_TRANSITION ) )
     638             :         {
     639           0 :             index = 1;
     640             :         }
     641           0 :         else if ( hRF->rf_clas[fec_offset] == VOICED_CLAS )
     642             :         {
     643           0 :             index = 2;
     644             :         }
     645             :         else
     646             :         {
     647           0 :             index = 3;
     648             :         }
     649           0 :         push_next_indice( hBstr, index, 2 );
     650             : 
     651           0 :         if ( rf_frame_type == RF_TCXFD )
     652             :         {
     653             :             /* TCX global gain  = 7 bits */
     654           0 :             push_next_indice( hBstr, hRF->rf_gain_tcx[fec_offset], 7 );
     655             :         }
     656             :         else
     657             :         {
     658             :             /* LTP data */
     659           0 :             if ( ( rf_frame_type == RF_TCXTD1 || rf_frame_type == RF_TCXTD2 ) && hTcxEnc->tcxltp )
     660             :             {
     661           0 :                 push_next_indice( hBstr, hRF->rf_tcxltp_param[fec_offset], 9 );
     662             :             }
     663             :         }
     664             :     }
     665           0 :     else if ( rf_frame_type == 7 ) /* NELP bitstream writing */
     666             :     {
     667             :         /* LSF indices */
     668           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     669           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     670             : 
     671             :         /* NELP gain indices */
     672           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG1[fec_offset], 5 );
     673           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][0], 6 );
     674           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][1], 6 );
     675             : 
     676             :         /* NELP filter selection index */
     677           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_fid[fec_offset], 2 );
     678             : 
     679             :         /* tbe gainFr */
     680           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 5 );
     681             :     }
     682           0 :     else if ( rf_frame_type >= 4 ) /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6 */
     683             :     {
     684             :         /* LSF indices */
     685           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     686           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     687             : 
     688             :         /* ES pred */
     689           0 :         push_next_indice( hBstr, hRF->rf_indx_EsPred[fec_offset], 3 );
     690             : 
     691           0 :         ltp_mode = ACELP_LTP_MODE[1][1][rf_frame_type];
     692           0 :         ltf_mode = ACELP_LTF_MODE[1][1][rf_frame_type];
     693           0 :         gains_mode = ACELP_GAINS_MODE[1][1][rf_frame_type];
     694             : 
     695             :         /* Subframe parameters */
     696           0 :         for ( sfr = 0; sfr < nb_subfr; sfr++ )
     697             :         {
     698             :             /* Pitch lag (5, or 8 bits) */
     699           0 :             n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
     700           0 :             if ( n != 0 )
     701             :             {
     702           0 :                 push_next_indice( hBstr, hRF->rf_indx_pitch[fec_offset][sfr], n );
     703             :             }
     704             : 
     705             :             /* Adaptive codebook filtering (1 bit) */
     706           0 :             if ( ltf_mode == 2 )
     707             :             {
     708           0 :                 push_next_indice( hBstr, hRF->rf_indx_ltfMode[fec_offset][sfr], 1 );
     709             :             }
     710             : 
     711             :             /*Innovative codebook*/
     712           0 :             if ( ( rf_frame_type == RF_NOPRED ) || ( rf_frame_type == RF_GENPRED && ( sfr == 0 || sfr == 2 ) ) )
     713             :             {
     714           0 :                 push_next_indice( hBstr, hRF->rf_indx_fcb[fec_offset][sfr], 7 );
     715             :             }
     716             : 
     717             :             /* Gains (5b, 6b or 7b / subfr) */
     718           0 :             if ( sfr == 0 || sfr == 2 )
     719             :             {
     720           0 :                 n = ACELP_GAINS_BITS[gains_mode];
     721           0 :                 push_next_indice( hBstr, hRF->rf_indx_gain[fec_offset][sfr], n );
     722             :             }
     723             :         }
     724             :         /* tbe gainFr */
     725           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 2 );
     726             :     }
     727             : 
     728             :     /***************/
     729             :     /*IMPORTANT: The last three bits are always the rf_frame_type in the bitstream (for both acelp and tcx partial copy);
     730             :                  the rf_frame_type indicates the length of the partial copy payload at the decoder.
     731             :                  The 2 bits before the rf_frame_type contains the fec_offset */
     732             : 
     733             :     /***************/
     734             :     /* write FEC offset just before the rf_frame_type */
     735           0 :     if ( fec_offset == 2 )
     736             :     {
     737           0 :         push_next_indice( hBstr, 0, 2 );
     738             :     }
     739           0 :     else if ( fec_offset == 3 || fec_offset == 5 || fec_offset == 7 )
     740             :     {
     741           0 :         push_next_indice( hBstr, ( fec_offset - 1 ) / 2, 2 );
     742             :     }
     743             : 
     744             :     /* write RF frame type last in the bitstream */
     745           0 :     push_next_indice( hBstr, rf_frame_type, 3 );
     746             : 
     747           0 :     return;
     748             : }
     749             : 
     750             : /*-----------------------------------------------------------------*
     751             :  * Function  enc_prm()                                             *
     752             :  * ~~~~~~~~~~~~~~~~~~~~~~                                          *
     753             :  *                                                                 *
     754             :  * encode parameters according to selected mode including          *
     755             :  * the FAC parameters when transition occurs.                      *
     756             :  *-----------------------------------------------------------------*/
     757             : 
     758        1250 : void enc_prm(
     759             :     Encoder_State *st,   /* i/o: encoder state structure */
     760             :     int16_t param[],     /* i  : parameters              */
     761             :     int16_t param_lpc[], /* i  : LPC parameters          */
     762             :     CONTEXT_HM_CONFIG hm_cfg[],
     763             :     const int16_t bits_param_lpc[],
     764             :     const int16_t no_param_lpc )
     765             : {
     766             :     int16_t j, n, sfr, core, last_core, nb_subfr;
     767             :     int16_t *prm;
     768             :     int16_t nbits_start, /*total_nbbits,*/ nbits_header, nbits_lpc;
     769             :     int16_t ix, j_old, wordcnt, bitcnt;
     770             : 
     771        1250 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     772             : 
     773             :     /*--------------------------------------------------------------------------------*
     774             :      * initialization
     775             :      *--------------------------------------------------------------------------------*/
     776             : 
     777        1250 :     nbits_lpc = 0;
     778             : 
     779             :     /* Useful parameters */
     780        1250 :     nb_subfr = st->nb_subfr;
     781        1250 :     core = st->core;
     782        1250 :     last_core = st->last_core;
     783             : 
     784             :     /* Initialize pointers */
     785        1250 :     prm = param;
     786             : 
     787             :     /* Init counters */
     788        1250 :     j = 0;
     789        1250 :     nbits_start = hBstr->nb_bits_tot;
     790             : 
     791             :     /*--------------------------------------------------------------------------------*
     792             :      * HEADER
     793             :      *--------------------------------------------------------------------------------*/
     794             : 
     795        1250 :     if ( st->mdct_sw == MODE1 )
     796             :     {
     797             :         /* Adjust st->bits_frame_core not to subtract MODE2 bandwidth signaling */
     798         275 :         st->bits_frame_core += FrameSizeConfig[st->frame_size_index].bandwidth_bits;
     799             : 
     800             :         /* Write MODE1 core & coder_type signaling */
     801         275 :         signaling_mode1_tcx20_enc( st, 1 );
     802             :     }
     803             : 
     804             :     /* EVS header */
     805             :     /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
     806        1250 :     writeTCXMode( st, st->hBstr, 0 /* MCT_flag */, &nbits_start );
     807             : 
     808             :     /* write last_core for error concealment */
     809        1250 :     if ( !( core == ACELP_CORE && st->hTcxCfg->lfacNext <= 0 ) )
     810             :     {
     811         646 :         push_next_indice( hBstr, ( ( last_core != ACELP_CORE ) || ( core == TCX_10_CORE ) ), 1 );
     812             :     }
     813             : 
     814             :     /* write TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
     815        1250 :     if ( core != ACELP_CORE )
     816             :     {
     817         646 :         writeTCXWindowing( hBstr, st->hTcxCfg->tcx_curr_overlap_mode );
     818             :     }
     819             : 
     820        1250 :     if ( st->hPlcExt && st->hPlcExt->enableGplc )
     821             :     {
     822             :         /* encode side information. */
     823         975 :         enc_prm_side_Info( st->hPlcExt, st );
     824             :     }
     825             : 
     826        1250 :     if ( st->glr )
     827             :     {
     828         975 :         if ( core != ACELP_CORE || st->coder_type == INACTIVE ||
     829         604 :              ( st->last_core == ACELP_CORE && st->last_coder_type_raw == INACTIVE ) ||
     830         604 :              st->glr_reset )
     831             :         {
     832         379 :             st->glr_idx[0] = 0;
     833             :         }
     834             : 
     835         975 :         if ( core == ACELP_CORE )
     836             :         {
     837         604 :             push_next_indice( hBstr, st->glr_idx[0], G_LPC_RECOVERY_BITS );
     838             :         }
     839             :     }
     840             : 
     841        1250 :     st->glr_reset = 0;
     842             : 
     843        1250 :     nbits_header = hBstr->nb_bits_tot - nbits_start;
     844             : 
     845             :     /*--------------------------------------------------------------------------------*
     846             :      * LPC parameters
     847             :      *--------------------------------------------------------------------------------*/
     848             : 
     849        1250 :     writeLPCparam( st, hBstr, param_lpc, bits_param_lpc, no_param_lpc, &nbits_lpc );
     850             : 
     851             : 
     852             :     /*--------------------------------------------------------------------------------*
     853             :      * ACELP parameters
     854             :      *--------------------------------------------------------------------------------*/
     855             : 
     856        1250 :     if ( core == ACELP_CORE )
     857             :     {
     858             :         /* Adaptive BPF (2 bits)*/
     859         604 :         n = ACELP_BPF_BITS[st->acelp_cfg.bpf_mode];
     860             : 
     861         604 :         if ( n != 0 )
     862             :         {
     863         578 :             push_next_indice( hBstr, st->bpf_gain_param, n );
     864             :         }
     865             : 
     866             :         /* Mean energy (2 or 3 bits) */
     867         604 :         n = ACELP_NRG_BITS[st->acelp_cfg.nrg_mode];
     868             : 
     869         604 :         if ( n != 0 )
     870             :         {
     871         604 :             push_next_indice( hBstr, prm[j++], n );
     872             :         }
     873             : 
     874             :         /* Subframe parameters */
     875        3624 :         for ( sfr = 0; sfr < nb_subfr; sfr++ )
     876             :         {
     877             :             /* Pitch lag (4, 5, 6, 8 or 9 bits) */
     878        3020 :             n = ACELP_LTP_BITS_SFR[st->acelp_cfg.ltp_mode][sfr];
     879             : 
     880        3020 :             if ( n != 0 )
     881             :             {
     882        3020 :                 push_next_indice( hBstr, prm[j++], n );
     883             :             }
     884             : 
     885             :             /* Adaptive codebook filtering (1 bit) */
     886        3020 :             if ( st->acelp_cfg.ltf_mode == 2 )
     887             :             {
     888        1340 :                 push_next_indice( hBstr, prm[j++], 1 );
     889             :             }
     890             : 
     891             :             /*Innovative codebook*/
     892             : 
     893        3020 :             j_old = j;
     894        3020 :             if ( ( st->acelp_cfg.fixed_cdk_index[sfr] >= ACELP_FIXED_CDK_NB ) || ( st->acelp_cfg.fixed_cdk_index[sfr] < 0 ) )
     895             :             {
     896           0 :                 IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "ACELP bits allocation: wrong fixed cdk bit allocation" );
     897             :             }
     898             : 
     899        3020 :             wordcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ) >> 4;
     900        3020 :             bitcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ) & 15;
     901       12214 :             for ( ix = 0; ix < wordcnt; ix++ )
     902             :             {
     903        9194 :                 push_next_indice( hBstr, prm[j++], 16 );
     904             :             }
     905             : 
     906        3020 :             if ( bitcnt )
     907             :             {
     908        3020 :                 push_next_indice( hBstr, prm[j++], bitcnt );
     909             :             }
     910        3020 :             j = j_old + 8;
     911             : 
     912             :             /* Gains (5b, 6b or 7b / subfr) */
     913        3020 :             n = ACELP_GAINS_BITS[st->acelp_cfg.gains_mode[sfr]];
     914        3020 :             push_next_indice( hBstr, prm[j++], n );
     915             :         }
     916             :     }
     917             : 
     918             : 
     919             :     /*--------------------------------------------------------------------------------*
     920             :      * TCX20/10 parameters
     921             :      *--------------------------------------------------------------------------------*/
     922             : 
     923        1250 :     if ( core == TCX_20_CORE || core == TCX_10_CORE )
     924             :     {
     925         646 :         writeTCXparam( st, hBstr, hm_cfg, param, nbits_header, nbits_start, nbits_lpc, NULL, NULL, NULL, -1 );
     926             :     }
     927             : 
     928             :     /*total_nbbits = hBstr->nb_bits_tot - nbits_start;*/
     929             : 
     930        1250 :     return;
     931             : }

Generated by: LCOV version 1.14