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 @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 198 282 70.2 %
Date: 2025-05-23 08:37:30 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      874966 : 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      874966 :     if ( st->tcxonly )
      67             :     {
      68      745402 :         push_next_indice( hBstr, st->core == TCX_10_CORE, 1 );
      69             : 
      70      745402 :         if ( st->clas == UNVOICED_CLAS )
      71             :         {
      72      387638 :             index = 0;
      73             :         }
      74      357764 :         else if ( st->clas == VOICED_TRANSITION || st->clas == UNVOICED_TRANSITION )
      75             :         {
      76       44874 :             index = 1;
      77             :         }
      78      312890 :         else if ( st->clas == VOICED_CLAS )
      79             :         {
      80      272345 :             index = 2;
      81             :         }
      82             :         else
      83             :         {
      84       40545 :             index = 3;
      85             :         }
      86             : 
      87      745402 :         push_next_indice( hBstr, index, 2 );
      88             : 
      89      745402 :         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      874966 :     return;
     185             : }
     186             : 
     187             : 
     188             : /*-------------------------------------------------------------------*
     189             :  * writeTCXWindowing()
     190             :  *
     191             :  * write TCX transform type
     192             :  *-------------------------------------------------------------------*/
     193             : 
     194      891250 : 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      891250 :     if ( overlap_mode == MIN_OVERLAP )
     204             :     {
     205        6797 :         push_next_indice( hBstr, 2, 2 );
     206             :     }
     207      884453 :     else if ( overlap_mode == HALF_OVERLAP )
     208             :     {
     209       12598 :         push_next_indice( hBstr, 3, 2 );
     210             :     }
     211             :     else
     212             :     {
     213      871855 :         push_next_indice( hBstr, 0, 1 );
     214             :     }
     215             : 
     216      891250 :     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     1513011 : 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     1513011 :     if ( pre_past_flag == 0 )
     348             :     {
     349      638504 :         pre_part = 1;
     350      638504 :         post_part = 0;
     351             :     }
     352      874507 :     else if ( pre_past_flag == 1 )
     353             :     {
     354      638649 :         pre_part = 0;
     355      638649 :         post_part = 1;
     356             :     }
     357             :     else
     358             :     {
     359      235858 :         pre_part = 1;
     360      235858 :         post_part = 1;
     361             :     }
     362             : 
     363             :     /* Initialization */
     364     1513011 :     core = st->core;
     365     1513011 :     last_core = st->last_core;
     366     1513011 :     nbits_igf = 0;
     367             : 
     368     1513011 :     nSubframes = 1;
     369     1513011 :     if ( core == TCX_10_CORE )
     370             :     {
     371       31699 :         nSubframes = 2;
     372             :     }
     373             : 
     374             :     /* loop over subframes */
     375     3057721 :     for ( k = 0; k < nSubframes; k++ )
     376             :     {
     377     1544710 :         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     1544710 :         flag_ctx_hm = 0;
     383             : 
     384     1544710 :         prm = param + ( k * NPRM_DIV );
     385     1544710 :         j = 0;
     386             : 
     387     1544710 :         nbits_tcx = total_nbbits = hBstr->nb_bits_tot - nbits_start;
     388             : 
     389     1544710 :         if ( pre_part && st->enablePlcWaveadjust && k == ( nSubframes - 1 ) )
     390             :         {
     391           0 :             push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
     392             :         }
     393             : 
     394     1544710 :         if ( post_part )
     395             :         {
     396             :             /* TCX Gain */
     397      891395 :             push_next_indice( hBstr, prm[j++], NBITS_TCX_GAIN );
     398             : 
     399             :             /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
     400      891395 :             push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
     401             :         }
     402             :         else
     403             :         {
     404      653315 :             j += 1 + NOISE_FILL_RANGES;
     405             :         }
     406             : 
     407             :         /* LTP data */
     408     1544710 :         if ( pre_part )
     409             :         {
     410      891250 :             if ( ( k == 0 ) && ( st->hTcxEnc->tcxltp || ( st->sr_core > 25600 ) ) ) /* PLC pitch info for HB */
     411             :             {
     412      874362 :                 if ( prm[j] )
     413             :                 {
     414      587713 :                     push_next_indice( hBstr, 1, 1 );
     415      587713 :                     push_next_indice( hBstr, prm[j + 1], 9 );
     416      587713 :                     push_next_indice( hBstr, prm[j + 2], 2 );
     417             :                 }
     418             :                 else
     419             :                 {
     420      286649 :                     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      891250 :             j += 3;
     428             :         }
     429             :         else
     430             :         {
     431      653460 :             j += p_param[k] - 2;
     432             :         }
     433             : 
     434             :         /* TCX spectral data */
     435     1544710 :         lg = st->last_L_frame >> ( nSubframes - 1 );
     436     1544710 :         lgFB = st->hTcxCfg->tcx_coded_lines >> ( nSubframes - 1 );
     437     1544710 :         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     1544710 :         if ( pre_part )
     450             :         {
     451             :             /* TNS data */
     452      891250 :             nTnsParams = 0;
     453      891250 :             nTnsBits = 0;
     454             : 
     455      891250 :             if ( st->hTcxCfg->fIsTNSAllowed )
     456             :             {
     457      815268 :                 SetTnsConfig( st->hTcxCfg, core == TCX_20_CORE, ( last_core == ACELP_CORE ) && ( k == 0 ) );
     458             : 
     459      815268 :                 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      815268 :                 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      812687 :                     WriteTnsData( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, hBstr, &nTnsBits );
     471             :                 }
     472      815268 :                 if ( no_param_tns )
     473             :                 {
     474       10729 :                     nTnsBits++;
     475             :                 }
     476      815268 :                 j += nTnsParams;
     477             :             }
     478             : #ifdef DEBUG_PLOT_BITS
     479             :             dbgwrite( &nTnsBits, sizeof( int16_t ), 1, 1, "./res/bits_TNS" );
     480             : #endif
     481             : 
     482      891250 :             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      891250 :             j += NPRM_CTX_HM;
     505             : 
     506      891250 :             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      653315 :                 p_param[k] = j;
     537             :             }
     538             :         }
     539             :         else
     540             :         {
     541      653460 :             nbits_tcx = st->bits_frame_channel;
     542      653460 :             if ( core == TCX_10_CORE )
     543             :             {
     544       29622 :                 nbits_tcx = target_bitsTCX10[k] - NBITS_TCX_GAIN - NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL;
     545             :             }
     546             :         }
     547             : 
     548     1544710 :         if ( post_part )
     549             :         {
     550      891395 :             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      873938 :                 if ( st->element_mode > EVS_MONO )
     558             :                 {
     559      873292 :                     RCcontextMapping_encode2_no_mem_s17_LCS( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
     560      873292 :                                                              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             :         }
     569             :     }
     570             : #ifdef DEBUG_PLOT_BITS
     571             :     if ( pre_part )
     572             :     {
     573             :         if ( nSubframes == 1 )
     574             :         {
     575             :             tmp = 0;
     576             :             dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_TNS" );
     577             :         }
     578             :     }
     579             : #endif
     580             : 
     581     1513011 :     return;
     582             : }
     583             : 
     584             : 
     585             : /*-----------------------------------------------------------------*
     586             :  * Function  enc_prm_rf()                                          *
     587             :  * ~~~~~~~~~~~~~~~~~~~~~~                                          *
     588             :  *                                                                 *
     589             :  * encode RF parameters for ACELP and TCX partial copy             *
     590             :  *-----------------------------------------------------------------*/
     591             : 
     592           0 : void enc_prm_rf(
     593             :     Encoder_State *st,
     594             :     const int16_t rf_frame_type,
     595             :     const int16_t fec_offset )
     596             : {
     597             :     int16_t sfr, nb_subfr, n, index;
     598             :     int16_t ltp_mode, ltf_mode, gains_mode;
     599             : 
     600           0 :     RF_ENC_HANDLE hRF = st->hRF;
     601           0 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     602           0 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     603             : 
     604           0 :     nb_subfr = st->nb_subfr;
     605             : 
     606             :     /* partial copy bitstream writing */
     607           0 :     if ( rf_frame_type >= RF_TCXFD && rf_frame_type <= RF_TCXTD2 )
     608             :     {
     609             :         /* TCX frames partial copy write */
     610           0 :         if ( rf_frame_type == RF_TCXFD )
     611             :         {
     612           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], lsf_numbits[0] ); /* VQ 1 */
     613           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], lsf_numbits[1] ); /* VQ 2 */
     614           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][2], lsf_numbits[2] ); /* VQ 3 */
     615             :         }
     616             : 
     617             :         /* classification */
     618           0 :         if ( hRF->rf_clas[fec_offset] == UNVOICED_CLAS )
     619             :         {
     620           0 :             index = 0;
     621             :         }
     622           0 :         else if ( ( hRF->rf_clas[fec_offset] == VOICED_TRANSITION ) || ( hRF->rf_clas[fec_offset] == UNVOICED_TRANSITION ) )
     623             :         {
     624           0 :             index = 1;
     625             :         }
     626           0 :         else if ( hRF->rf_clas[fec_offset] == VOICED_CLAS )
     627             :         {
     628           0 :             index = 2;
     629             :         }
     630             :         else
     631             :         {
     632           0 :             index = 3;
     633             :         }
     634           0 :         push_next_indice( hBstr, index, 2 );
     635             : 
     636           0 :         if ( rf_frame_type == RF_TCXFD )
     637             :         {
     638             :             /* TCX global gain  = 7 bits */
     639           0 :             push_next_indice( hBstr, hRF->rf_gain_tcx[fec_offset], 7 );
     640             :         }
     641             :         else
     642             :         {
     643             :             /* LTP data */
     644           0 :             if ( ( rf_frame_type == RF_TCXTD1 || rf_frame_type == RF_TCXTD2 ) && hTcxEnc->tcxltp )
     645             :             {
     646           0 :                 push_next_indice( hBstr, hRF->rf_tcxltp_param[fec_offset], 9 );
     647             :             }
     648             :         }
     649             :     }
     650           0 :     else if ( rf_frame_type == 7 ) /* NELP bitstream writing */
     651             :     {
     652             :         /* LSF indices */
     653           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     654           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     655             : 
     656             :         /* NELP gain indices */
     657           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG1[fec_offset], 5 );
     658           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][0], 6 );
     659           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][1], 6 );
     660             : 
     661             :         /* NELP filter selection index */
     662           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_fid[fec_offset], 2 );
     663             : 
     664             :         /* tbe gainFr */
     665           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 5 );
     666             :     }
     667           0 :     else if ( rf_frame_type >= 4 ) /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6 */
     668             :     {
     669             :         /* LSF indices */
     670           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     671           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     672             : 
     673             :         /* ES pred */
     674           0 :         push_next_indice( hBstr, hRF->rf_indx_EsPred[fec_offset], 3 );
     675             : 
     676           0 :         ltp_mode = ACELP_LTP_MODE[1][1][rf_frame_type];
     677           0 :         ltf_mode = ACELP_LTF_MODE[1][1][rf_frame_type];
     678           0 :         gains_mode = ACELP_GAINS_MODE[1][1][rf_frame_type];
     679             : 
     680             :         /* Subframe parameters */
     681           0 :         for ( sfr = 0; sfr < nb_subfr; sfr++ )
     682             :         {
     683             :             /* Pitch lag (5, or 8 bits) */
     684           0 :             n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
     685           0 :             if ( n != 0 )
     686             :             {
     687           0 :                 push_next_indice( hBstr, hRF->rf_indx_pitch[fec_offset][sfr], n );
     688             :             }
     689             : 
     690             :             /* Adaptive codebook filtering (1 bit) */
     691           0 :             if ( ltf_mode == 2 )
     692             :             {
     693           0 :                 push_next_indice( hBstr, hRF->rf_indx_ltfMode[fec_offset][sfr], 1 );
     694             :             }
     695             : 
     696             :             /*Innovative codebook*/
     697           0 :             if ( ( rf_frame_type == RF_NOPRED ) || ( rf_frame_type == RF_GENPRED && ( sfr == 0 || sfr == 2 ) ) )
     698             :             {
     699           0 :                 push_next_indice( hBstr, hRF->rf_indx_fcb[fec_offset][sfr], 7 );
     700             :             }
     701             : 
     702             :             /* Gains (5b, 6b or 7b / subfr) */
     703           0 :             if ( sfr == 0 || sfr == 2 )
     704             :             {
     705           0 :                 n = ACELP_GAINS_BITS[gains_mode];
     706           0 :                 push_next_indice( hBstr, hRF->rf_indx_gain[fec_offset][sfr], n );
     707             :             }
     708             :         }
     709             :         /* tbe gainFr */
     710           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 2 );
     711             :     }
     712             : 
     713             :     /***************/
     714             :     /*IMPORTANT: The last three bits are always the rf_frame_type in the bitstream (for both acelp and tcx partial copy);
     715             :                  the rf_frame_type indicates the length of the partial copy payload at the decoder.
     716             :                  The 2 bits before the rf_frame_type contains the fec_offset */
     717             : 
     718             :     /***************/
     719             :     /* write FEC offset just before the rf_frame_type */
     720           0 :     if ( fec_offset == 2 )
     721             :     {
     722           0 :         push_next_indice( hBstr, 0, 2 );
     723             :     }
     724           0 :     else if ( fec_offset == 3 || fec_offset == 5 || fec_offset == 7 )
     725             :     {
     726           0 :         push_next_indice( hBstr, ( fec_offset - 1 ) / 2, 2 );
     727             :     }
     728             : 
     729             :     /* write RF frame type last in the bitstream */
     730           0 :     push_next_indice( hBstr, rf_frame_type, 3 );
     731             : 
     732           0 :     return;
     733             : }
     734             : 
     735             : /*-----------------------------------------------------------------*
     736             :  * Function  enc_prm()                                             *
     737             :  * ~~~~~~~~~~~~~~~~~~~~~~                                          *
     738             :  *                                                                 *
     739             :  * encode parameters according to selected mode including          *
     740             :  * the FAC parameters when transition occurs.                      *
     741             :  *-----------------------------------------------------------------*/
     742             : 
     743        1250 : void enc_prm(
     744             :     Encoder_State *st,   /* i/o: encoder state structure */
     745             :     int16_t param[],     /* i  : parameters              */
     746             :     int16_t param_lpc[], /* i  : LPC parameters          */
     747             :     CONTEXT_HM_CONFIG hm_cfg[],
     748             :     const int16_t bits_param_lpc[],
     749             :     const int16_t no_param_lpc )
     750             : {
     751             :     int16_t j, n, sfr, core, last_core, nb_subfr;
     752             :     int16_t *prm;
     753             :     int16_t nbits_start, /*total_nbbits,*/ nbits_header, nbits_lpc;
     754             :     int16_t ix, j_old, wordcnt, bitcnt;
     755             : 
     756        1250 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     757             : 
     758             :     /*--------------------------------------------------------------------------------*
     759             :      * initialization
     760             :      *--------------------------------------------------------------------------------*/
     761             : 
     762        1250 :     nbits_lpc = 0;
     763             : 
     764             :     /* Useful parameters */
     765        1250 :     nb_subfr = st->nb_subfr;
     766        1250 :     core = st->core;
     767        1250 :     last_core = st->last_core;
     768             : 
     769             :     /* Initialize pointers */
     770        1250 :     prm = param;
     771             : 
     772             :     /* Init counters */
     773        1250 :     j = 0;
     774        1250 :     nbits_start = hBstr->nb_bits_tot;
     775             : 
     776             :     /*--------------------------------------------------------------------------------*
     777             :      * HEADER
     778             :      *--------------------------------------------------------------------------------*/
     779             : 
     780        1250 :     if ( st->mdct_sw == MODE1 )
     781             :     {
     782             :         /* Adjust st->bits_frame_core not to subtract MODE2 bandwidth signaling */
     783         275 :         st->bits_frame_core += FrameSizeConfig[st->frame_size_index].bandwidth_bits;
     784             : 
     785             :         /* Write MODE1 core & coder_type signaling */
     786         275 :         signaling_mode1_tcx20_enc( st, 1 );
     787             :     }
     788             : 
     789             :     /* EVS header */
     790             :     /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
     791        1250 :     writeTCXMode( st, st->hBstr, 0 /* MCT_flag */, &nbits_start );
     792             : 
     793             :     /* write last_core for error concealment */
     794        1250 :     if ( !( core == ACELP_CORE && st->hTcxCfg->lfacNext <= 0 ) )
     795             :     {
     796         646 :         push_next_indice( hBstr, ( ( last_core != ACELP_CORE ) || ( core == TCX_10_CORE ) ), 1 );
     797             :     }
     798             : 
     799             :     /* write TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
     800        1250 :     if ( core != ACELP_CORE )
     801             :     {
     802         646 :         writeTCXWindowing( hBstr, st->hTcxCfg->tcx_curr_overlap_mode );
     803             :     }
     804             : 
     805        1250 :     if ( st->hPlcExt && st->hPlcExt->enableGplc )
     806             :     {
     807             :         /* encode side information. */
     808         975 :         enc_prm_side_Info( st->hPlcExt, st );
     809             :     }
     810             : 
     811        1250 :     if ( st->glr )
     812             :     {
     813         975 :         if ( core != ACELP_CORE || st->coder_type == INACTIVE ||
     814         604 :              ( st->last_core == ACELP_CORE && st->last_coder_type_raw == INACTIVE ) ||
     815         604 :              st->glr_reset )
     816             :         {
     817         379 :             st->glr_idx[0] = 0;
     818             :         }
     819             : 
     820         975 :         if ( core == ACELP_CORE )
     821             :         {
     822         604 :             push_next_indice( hBstr, st->glr_idx[0], G_LPC_RECOVERY_BITS );
     823             :         }
     824             :     }
     825             : 
     826        1250 :     st->glr_reset = 0;
     827             : 
     828        1250 :     nbits_header = hBstr->nb_bits_tot - nbits_start;
     829             : 
     830             :     /*--------------------------------------------------------------------------------*
     831             :      * LPC parameters
     832             :      *--------------------------------------------------------------------------------*/
     833             : 
     834        1250 :     writeLPCparam( st, hBstr, param_lpc, bits_param_lpc, no_param_lpc, &nbits_lpc );
     835             : 
     836             : 
     837             :     /*--------------------------------------------------------------------------------*
     838             :      * ACELP parameters
     839             :      *--------------------------------------------------------------------------------*/
     840             : 
     841        1250 :     if ( core == ACELP_CORE )
     842             :     {
     843             :         /* Adaptive BPF (2 bits)*/
     844         604 :         n = ACELP_BPF_BITS[st->acelp_cfg.bpf_mode];
     845             : 
     846         604 :         if ( n != 0 )
     847             :         {
     848         578 :             push_next_indice( hBstr, st->bpf_gain_param, n );
     849             :         }
     850             : 
     851             :         /* Mean energy (2 or 3 bits) */
     852         604 :         n = ACELP_NRG_BITS[st->acelp_cfg.nrg_mode];
     853             : 
     854         604 :         if ( n != 0 )
     855             :         {
     856         604 :             push_next_indice( hBstr, prm[j++], n );
     857             :         }
     858             : 
     859             :         /* Subframe parameters */
     860        3624 :         for ( sfr = 0; sfr < nb_subfr; sfr++ )
     861             :         {
     862             :             /* Pitch lag (4, 5, 6, 8 or 9 bits) */
     863        3020 :             n = ACELP_LTP_BITS_SFR[st->acelp_cfg.ltp_mode][sfr];
     864             : 
     865        3020 :             if ( n != 0 )
     866             :             {
     867        3020 :                 push_next_indice( hBstr, prm[j++], n );
     868             :             }
     869             : 
     870             :             /* Adaptive codebook filtering (1 bit) */
     871        3020 :             if ( st->acelp_cfg.ltf_mode == 2 )
     872             :             {
     873        1340 :                 push_next_indice( hBstr, prm[j++], 1 );
     874             :             }
     875             : 
     876             :             /*Innovative codebook*/
     877             : 
     878        3020 :             j_old = j;
     879        3020 :             if ( ( st->acelp_cfg.fixed_cdk_index[sfr] >= ACELP_FIXED_CDK_NB ) || ( st->acelp_cfg.fixed_cdk_index[sfr] < 0 ) )
     880             :             {
     881           0 :                 IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "ACELP bits allocation: wrong fixed cdk bit allocation" );
     882             :             }
     883             : 
     884        3020 :             wordcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ) >> 4;
     885        3020 :             bitcnt = ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ) & 15;
     886       12214 :             for ( ix = 0; ix < wordcnt; ix++ )
     887             :             {
     888        9194 :                 push_next_indice( hBstr, prm[j++], 16 );
     889             :             }
     890             : 
     891        3020 :             if ( bitcnt )
     892             :             {
     893        3020 :                 push_next_indice( hBstr, prm[j++], bitcnt );
     894             :             }
     895        3020 :             j = j_old + 8;
     896             : 
     897             :             /* Gains (5b, 6b or 7b / subfr) */
     898        3020 :             n = ACELP_GAINS_BITS[st->acelp_cfg.gains_mode[sfr]];
     899        3020 :             push_next_indice( hBstr, prm[j++], n );
     900             :         }
     901             :     }
     902             : 
     903             : 
     904             :     /*--------------------------------------------------------------------------------*
     905             :      * TCX20/10 parameters
     906             :      *--------------------------------------------------------------------------------*/
     907             : 
     908        1250 :     if ( core == TCX_20_CORE || core == TCX_10_CORE )
     909             :     {
     910         646 :         writeTCXparam( st, hBstr, hm_cfg, param, nbits_header, nbits_start, nbits_lpc, NULL, NULL, NULL, -1 );
     911             :     }
     912             : 
     913             :     /*total_nbbits = hBstr->nb_bits_tot - nbits_start;*/
     914             : 
     915        1250 :     return;
     916             : }

Generated by: LCOV version 1.14