LCOV - code coverage report
Current view: top level - lib_enc - enc_prm_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 375 714 52.5 %
Date: 2025-05-03 01:55:50 Functions: 6 9 66.7 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdlib.h>
       6             : #include <assert.h>
       7             : #include <limits.h>
       8             : #include <string.h>
       9             : #include "options.h"
      10             : #include "cnst.h"
      11             : #include "rom_com_fx.h"
      12             : #include "rom_com.h"
      13             : #include "stl.h"
      14             : #include "prot_fx.h"
      15             : #include "prot_fx_enc.h" /* Function prototypes                    */
      16             : 
      17             : 
      18           0 : static void enc_prm_hm(
      19             :     Word16 *prm_hm, /* Q0 */
      20             :     Encoder_State *st,
      21             :     Word16 L_frame /* Q0 */
      22             : )
      23             : {
      24             :     Word8 flag;
      25           0 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
      26             : 
      27             : 
      28             :     /* Disable HM for non-GC,VC modes */
      29           0 :     test();
      30           0 :     IF( NE_16( st->hTcxCfg->coder_type, VOICED ) && NE_16( st->hTcxCfg->coder_type, GENERIC ) )
      31             :     {
      32           0 :         return;
      33             :     }
      34             : 
      35             :     /* Flag */
      36           0 :     push_next_indice( hBstr, prm_hm[0], 1 );
      37             : 
      38           0 :     IF( prm_hm[0] )
      39             :     {
      40             :         /* Periodicy index */
      41           0 :         flag = 0;
      42           0 :         move16();
      43           0 :         if ( GE_16( L_frame, 256 ) )
      44             :         {
      45           0 :             flag = 1;
      46           0 :             move16();
      47             :         }
      48           0 :         EncodeIndex_fx( flag, prm_hm[1], hBstr );
      49             : 
      50           0 :         IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) )
      51             :         {
      52             :             /* Gain index */
      53           0 :             push_next_indice( hBstr, prm_hm[2], kTcxHmNumGainBits );
      54             :         }
      55             :     }
      56             : }
      57       15849 : static void enc_prm_hm_ivas_fx(
      58             :     Word16 *prm_hm, /* Q0 */
      59             :     Encoder_State *st,
      60             :     Word16 L_frame /* Q0 */
      61             : )
      62             : {
      63             :     Word8 flag;
      64       15849 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
      65             : 
      66             : 
      67             :     /* Disable HM for non-GC,VC modes */
      68       15849 :     test();
      69       15849 :     IF( NE_16( st->hTcxCfg->coder_type, VOICED ) && NE_16( st->hTcxCfg->coder_type, GENERIC ) )
      70             :     {
      71        2120 :         return;
      72             :     }
      73             : 
      74             :     /* Flag */
      75       13729 :     push_next_indice( hBstr, prm_hm[0], 1 );
      76             : 
      77       13729 :     IF( prm_hm[0] )
      78             :     {
      79             :         /* Periodicy index */
      80        7039 :         flag = 0;
      81        7039 :         move16();
      82        7039 :         if ( GE_16( L_frame, L_FRAME ) )
      83             :         {
      84        7039 :             flag = 1;
      85        7039 :             move16();
      86             :         }
      87        7039 :         EncodeIndex_ivas_fx( flag, prm_hm[1], hBstr );
      88             : 
      89        7039 :         IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) )
      90             :         {
      91             :             /* Gain index */
      92        2416 :             push_next_indice( hBstr, prm_hm[2], kTcxHmNumGainBits );
      93             :         }
      94             :     }
      95             : }
      96             : 
      97             : /*-----------------------------------------------------------------*
      98             :  * Function  enc_prm_rf_fx()                                          *
      99             :  * ~~~~~~~~~~~~~~~~~~~~~~                                          *
     100             :  *                                                                 *
     101             :  * encode RF parameters for ACELP and TCX partial copy             *
     102             :  *-----------------------------------------------------------------*/
     103             : 
     104           0 : void enc_prm_rf_ivas_fx(
     105             :     Encoder_State *st,
     106             :     const Word16 rf_frame_type, /* Q0 */
     107             :     const Word16 fec_offset     /* Q0 */
     108             : )
     109             : {
     110             :     Word16 sfr, nb_subfr, n, index;
     111             :     Word16 ltp_mode, ltf_mode, gains_mode;
     112           0 :     RF_ENC_HANDLE hRF = st->hRF;
     113           0 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     114           0 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     115             : 
     116             : 
     117           0 :     nb_subfr = st->nb_subfr; /* Q0 */
     118           0 :     move16();
     119             :     /* partial copy bitstream writing */
     120           0 :     test();
     121           0 :     IF( GE_16( rf_frame_type, RF_TCXFD ) && LE_16( rf_frame_type, RF_TCXTD2 ) )
     122             :     {
     123             :         /* TCX frames partial copy write */
     124             : 
     125             :         /* LSF indices */
     126           0 :         IF( EQ_16( rf_frame_type, RF_TCXFD ) )
     127             :         {
     128           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], lsf_numbits[0] ); /* VQ 1 */
     129           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], lsf_numbits[1] ); /* VQ 2 */
     130           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][2], lsf_numbits[2] ); /* VQ 3 */
     131             :         }
     132             : 
     133             :         /* classification */
     134           0 :         test();
     135           0 :         IF( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_CLAS ) )
     136             :         {
     137           0 :             index = 0;
     138           0 :             move16();
     139             :         }
     140           0 :         ELSE IF( ( EQ_16( hRF->rf_clas[fec_offset], VOICED_TRANSITION ) ) || ( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_TRANSITION ) ) )
     141             :         {
     142           0 :             index = 1;
     143           0 :             move16();
     144             :         }
     145           0 :         ELSE IF( EQ_16( hRF->rf_clas[fec_offset], VOICED_CLAS ) )
     146             :         {
     147           0 :             index = 2;
     148           0 :             move16();
     149             :         }
     150             :         ELSE
     151             :         {
     152           0 :             index = 3;
     153           0 :             move16();
     154             :         }
     155           0 :         push_next_indice( hBstr, index, 2 );
     156             : 
     157           0 :         IF( EQ_16( rf_frame_type, RF_TCXFD ) )
     158             :         {
     159             :             /* TCX global gain  = 7 bits */
     160           0 :             push_next_indice( hBstr, hRF->rf_gain_tcx[fec_offset], 7 );
     161             :             /*window info
     162             :             1 bit for long overlap
     163             :             2 if minimum or half overlap*/
     164             :         }
     165             :         ELSE
     166             :         {
     167             :             /*gains adapt
     168             :             gains inov*/
     169             : 
     170             :             /*LPC on full rate -> excitation */
     171             :             /* pitch and gain */
     172             :             /* LTP data */
     173           0 :             test();
     174           0 :             test();
     175           0 :             IF( ( EQ_16( rf_frame_type, RF_TCXTD1 ) || EQ_16( rf_frame_type, RF_TCXTD2 ) ) && hTcxEnc->tcxltp != 0 )
     176             :             {
     177           0 :                 push_next_indice( hBstr, hRF->rf_tcxltp_param[fec_offset], 9 );
     178             :             }
     179             :         }
     180             :     }
     181           0 :     ELSE IF( EQ_16( rf_frame_type, 7 ) ) /* NELP bitstream writing */
     182             :     {
     183             :         /* LSF indices */
     184           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     185           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     186             : 
     187             :         /* NELP gain indices */
     188           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG1[fec_offset], 5 );
     189           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][0], 6 );
     190           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][1], 6 );
     191             : 
     192             :         /* NELP filter selection index */
     193           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_fid[fec_offset], 2 );
     194             : 
     195             :         /* tbe gainFr */
     196           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 5 );
     197             :     }
     198           0 :     ELSE IF( GE_16( rf_frame_type, 4 ) ) /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6 */
     199             :     {
     200             :         /* LSF indices */
     201           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     202           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     203             : 
     204             :         /* ES pred */
     205           0 :         push_next_indice( hBstr, hRF->rf_indx_EsPred[fec_offset], 3 );
     206             : 
     207           0 :         ltp_mode = ACELP_LTP_MODE[1][1][rf_frame_type];
     208           0 :         ltf_mode = ACELP_LTF_MODE[1][1][rf_frame_type];
     209           0 :         gains_mode = ACELP_GAINS_MODE[1][1][rf_frame_type];
     210           0 :         move16();
     211           0 :         move16();
     212           0 :         move16();
     213             :         /* Subframe parameters */
     214           0 :         FOR( sfr = 0; sfr < nb_subfr; sfr++ )
     215             :         {
     216             :             /* Pitch lag (5, or 8 bits) */
     217           0 :             n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
     218           0 :             move16();
     219           0 :             IF( n != 0 )
     220             :             {
     221           0 :                 push_next_indice( hBstr, hRF->rf_indx_pitch[fec_offset][sfr], n );
     222             :             }
     223             : 
     224             :             /* Adaptive codebook filtering (1 bit) */
     225           0 :             IF( EQ_16( ltf_mode, 2 ) )
     226             :             {
     227           0 :                 push_next_indice( hBstr, hRF->rf_indx_ltfMode[fec_offset][sfr], 1 );
     228             :             }
     229             : 
     230             :             /*Innovative codebook*/
     231           0 :             test();
     232           0 :             test();
     233           0 :             test();
     234           0 :             IF( ( EQ_16( rf_frame_type, RF_NOPRED ) ) ||
     235             :                 ( EQ_16( rf_frame_type, RF_GENPRED ) &&
     236             :                   ( sfr == 0 || EQ_16( sfr, 2 ) ) ) )
     237             :             {
     238           0 :                 push_next_indice( hBstr, hRF->rf_indx_fcb[fec_offset][sfr], 7 );
     239             :             }
     240             : 
     241             :             /* Gains (5b, 6b or 7b / subfr) */
     242           0 :             test();
     243           0 :             IF( sfr == 0 || EQ_16( sfr, 2 ) )
     244             :             {
     245           0 :                 n = ACELP_GAINS_BITS[gains_mode];
     246           0 :                 move16();
     247           0 :                 push_next_indice( hBstr, hRF->rf_indx_gain[fec_offset][sfr], n );
     248             :             }
     249             :         }
     250             :         /* tbe gainFr */
     251           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 2 );
     252             :     }
     253             : 
     254             :     /***************/
     255             :     /*IMPORTANT: The last three bits are always the rf_frame_type in the bitstream (for both acelp and tcx partial copy);
     256             :                  the rf_frame_type indicates the length of the partial copy payload at the decoder.
     257             :                  The 2 bits before the rf_frame_type contains the fec_offset */
     258             : 
     259             :     /***************/
     260             :     /* write FEC offset just before the rf_frame_type */
     261           0 :     test();
     262           0 :     test();
     263           0 :     IF( EQ_16( fec_offset, 2 ) )
     264             :     {
     265           0 :         push_next_indice( hBstr, 0, 2 );
     266             :     }
     267           0 :     ELSE IF( EQ_16( fec_offset, 3 ) || EQ_16( fec_offset, 5 ) || EQ_16( fec_offset, 7 ) )
     268             :     {
     269           0 :         push_next_indice( hBstr, shr( sub( fec_offset, 1 ), 1 ), 2 );
     270             :     }
     271             : 
     272             :     /* write RF frame type last in the bitstream */
     273           0 :     push_next_indice( hBstr, rf_frame_type, 3 );
     274           0 : }
     275           0 : void enc_prm_rf_fx(
     276             :     Encoder_State *st,
     277             :     const Word16 rf_frame_type, /* Q0 */
     278             :     const Word16 fec_offset     /* Q0 */
     279             : )
     280             : {
     281             :     Word16 sfr, nb_subfr, n, index;
     282             :     Word16 ltp_mode, ltf_mode, gains_mode;
     283           0 :     RF_ENC_HANDLE hRF = st->hRF;
     284           0 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     285           0 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     286             : 
     287             : 
     288           0 :     nb_subfr = st->nb_subfr; /* Q0 */
     289             : 
     290             :     /* partial copy bitstream writing */
     291           0 :     test();
     292           0 :     IF( GE_16( rf_frame_type, RF_TCXFD ) && LE_16( rf_frame_type, RF_TCXTD2 ) )
     293             :     {
     294             :         /* TCX frames partial copy write */
     295             : 
     296             :         /* LSF indices */
     297           0 :         IF( EQ_16( rf_frame_type, RF_TCXFD ) )
     298             :         {
     299           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], lsf_numbits[0] ); /* VQ 1 */
     300           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], lsf_numbits[1] ); /* VQ 2 */
     301           0 :             push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][2], lsf_numbits[2] ); /* VQ 3 */
     302             :         }
     303             : 
     304             :         /* classification */
     305           0 :         test();
     306           0 :         test();
     307           0 :         IF( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_CLAS ) )
     308             :         {
     309           0 :             index = 0;
     310           0 :             move16();
     311             :         }
     312           0 :         ELSE IF( ( EQ_16( hRF->rf_clas[fec_offset], VOICED_TRANSITION ) ) || ( EQ_16( hRF->rf_clas[fec_offset], UNVOICED_TRANSITION ) ) )
     313             :         {
     314           0 :             index = 1;
     315           0 :             move16();
     316             :         }
     317           0 :         ELSE IF( EQ_16( hRF->rf_clas[fec_offset], VOICED_CLAS ) )
     318             :         {
     319           0 :             index = 2;
     320           0 :             move16();
     321             :         }
     322             :         ELSE
     323             :         {
     324           0 :             index = 3;
     325           0 :             move16();
     326             :         }
     327           0 :         push_next_indice( hBstr, index, 2 );
     328             : 
     329           0 :         IF( EQ_16( rf_frame_type, RF_TCXFD ) )
     330             :         {
     331             :             /* TCX global gain  = 7 bits */
     332           0 :             push_next_indice( hBstr, hRF->rf_gain_tcx[fec_offset], 7 );
     333             :             /*window info
     334             :             1 bit for long overlap
     335             :             2 if minimum or half overlap*/
     336             :         }
     337             :         ELSE
     338             :         {
     339             :             /*gains adapt
     340             :             gains inov*/
     341             : 
     342             :             /*LPC on full rate -> excitation */
     343             :             /* pitch and gain */
     344             :             /* LTP data */
     345           0 :             test();
     346           0 :             IF( ( EQ_16( rf_frame_type, RF_TCXTD1 ) || EQ_16( rf_frame_type, RF_TCXTD2 ) ) && hTcxEnc->tcxltp != 0 )
     347             :             {
     348           0 :                 push_next_indice( hBstr, hRF->rf_tcxltp_param[fec_offset], 9 );
     349             :             }
     350             :         }
     351             :     }
     352           0 :     ELSE IF( EQ_16( rf_frame_type, 7 ) ) /* NELP bitstream writing */
     353             :     {
     354             :         /* LSF indices */
     355           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     356           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     357             : 
     358             :         /* NELP gain indices */
     359           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG1[fec_offset], 5 );
     360           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][0], 6 );
     361           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_iG2[fec_offset][1], 6 );
     362             : 
     363             :         /* NELP filter selection index */
     364           0 :         push_next_indice( hBstr, hRF->rf_indx_nelp_fid[fec_offset], 2 );
     365             : 
     366             :         /* tbe gainFr */
     367           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 5 );
     368             :     }
     369           0 :     ELSE IF( GE_16( rf_frame_type, 4 ) ) /* rf_frame_type ALL_PRED: 4, NO_PRED: 5, GEN_PRED: 6 */
     370             :     {
     371             :         /* LSF indices */
     372           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][0], 8 ); /* VQ 1 */
     373           0 :         push_next_indice( hBstr, hRF->rf_indx_lsf[fec_offset][1], 8 ); /* VQ 2 */
     374             : 
     375             :         /* ES pred */
     376           0 :         push_next_indice( hBstr, hRF->rf_indx_EsPred[fec_offset], 3 );
     377             : 
     378           0 :         ltp_mode = ACELP_LTP_MODE[1][1][rf_frame_type];     /* Q0 */
     379           0 :         ltf_mode = ACELP_LTF_MODE[1][1][rf_frame_type];     /* Q0 */
     380           0 :         gains_mode = ACELP_GAINS_MODE[1][1][rf_frame_type]; /* Q0 */
     381             : 
     382             :         /* Subframe parameters */
     383           0 :         FOR( sfr = 0; sfr < nb_subfr; sfr++ )
     384             :         {
     385             :             /* Pitch lag (5, or 8 bits) */
     386           0 :             n = ACELP_LTP_BITS_SFR[ltp_mode][sfr];
     387           0 :             IF( n != 0 )
     388             :             {
     389           0 :                 push_next_indice( hBstr, hRF->rf_indx_pitch[fec_offset][sfr], n );
     390             :             }
     391             : 
     392             :             /* Adaptive codebook filtering (1 bit) */
     393           0 :             IF( EQ_16( ltf_mode, 2 ) )
     394             :             {
     395           0 :                 push_next_indice( hBstr, hRF->rf_indx_ltfMode[fec_offset][sfr], 1 );
     396             :             }
     397             : 
     398             :             /*Innovative codebook*/
     399           0 :             test();
     400           0 :             test();
     401           0 :             test();
     402           0 :             IF( ( EQ_16( rf_frame_type, RF_NOPRED ) ) ||
     403             :                 ( EQ_16( rf_frame_type, RF_GENPRED ) &&
     404             :                   ( sfr == 0 || EQ_16( sfr, 2 ) ) ) )
     405             :             {
     406           0 :                 push_next_indice( hBstr, hRF->rf_indx_fcb[fec_offset][sfr], 7 );
     407             :             }
     408             : 
     409             :             /* Gains (5b, 6b or 7b / subfr) */
     410           0 :             test();
     411           0 :             IF( sfr == 0 || EQ_16( sfr, 2 ) )
     412             :             {
     413           0 :                 n = ACELP_GAINS_BITS[gains_mode];
     414           0 :                 push_next_indice( hBstr, hRF->rf_indx_gain[fec_offset][sfr], n );
     415             :             }
     416             :         }
     417             :         /* tbe gainFr */
     418           0 :         push_next_indice( hBstr, hRF->rf_indx_tbeGainFr[fec_offset], 2 );
     419             :     }
     420             : 
     421             :     /***************/
     422             :     /*IMPORTANT: The last three bits are always the rf_frame_type in the bitstream (for both acelp and tcx partial copy);
     423             :                  the rf_frame_type indicates the length of the partial copy payload at the decoder.
     424             :                  The 2 bits before the rf_frame_type contains the fec_offset */
     425             : 
     426             :     /***************/
     427             :     /* write FEC offset just before the rf_frame_type */
     428           0 :     test();
     429           0 :     test();
     430           0 :     IF( EQ_16( fec_offset, 2 ) )
     431             :     {
     432           0 :         push_next_indice( hBstr, 0, 2 );
     433             :     }
     434           0 :     ELSE IF( EQ_16( fec_offset, 3 ) || EQ_16( fec_offset, 5 ) || EQ_16( fec_offset, 7 ) )
     435             :     {
     436           0 :         push_next_indice( hBstr, ( fec_offset - 1 ) / 2, 2 );
     437             :     }
     438             : 
     439             :     /* write RF frame type last in the bitstream */
     440           0 :     push_next_indice( hBstr, rf_frame_type, 3 );
     441           0 : }
     442             : 
     443             : 
     444             : /*-----------------------------------------------------------------*
     445             :  * Funtion  enc_prm_fx()                                              *
     446             :  * ~~~~~~~~~~~~~~~~~~~~~~                                          *
     447             :  *                                                                 *
     448             :  * encode parameters according to selected mode including          *
     449             :  * the FAC parameters when transition occurs.                      *
     450             :  *-----------------------------------------------------------------*/
     451             : 
     452        1266 : void enc_prm_fx(
     453             :     const Word16 coder_type, /* (i) : coding type                      */
     454             :     Word16 param[],          /* (i) : parameters                       Q0*/
     455             :     Word16 param_lpc[],      /* (i) : LPC parameters                   Q0*/
     456             :     Encoder_State *st,       /* i/o : quantization Analysis values     */
     457             :     Word16 L_frame,          /* Q0 */
     458             :     CONTEXT_HM_CONFIG hm_cfg[],
     459             :     Word16 *bits_param_lpc, /* Q0 */
     460             :     Word16 no_param_lpc     /* Q0 */
     461             : )
     462             : {
     463             :     Word16 j, k, n, sfr, core, last_core, *prm, tmp;
     464             :     Word16 nbits_start, total_nbbits;
     465             :     Word16 nbits_header;
     466             :     Word16 nbits_lpc;
     467             :     Word16 nbits_tcx;
     468             :     Word16 lg, nb_subfr;
     469             :     Word16 lgFB;
     470             :     Word16 nTnsParams;
     471             :     Word16 nTnsBits;
     472             :     Word16 ix, j_old, wordcnt, bitcnt;
     473             :     Word16 hm_size;
     474             :     Word16 numlpc;
     475             :     Word8 flag_ctx_hm;
     476             :     Word16 index;
     477             :     Word32 tmp32;
     478             :     CONTEXT_HM_CONFIG *phm_cfg;
     479             :     Word16 idx;
     480             :     Word16 start_idx;
     481             :     Word16 nBits;
     482        1266 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     483        1266 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     484             : 
     485             :     /*--------------------------------------------------------------------------------*
     486             :      * INIT
     487             :      *--------------------------------------------------------------------------------*/
     488             : 
     489        1266 :     move16();
     490        1266 :     move16();
     491        1266 :     move16();
     492        1266 :     move16();
     493        1266 :     move16();
     494        1266 :     move16();
     495        1266 :     nbits_lpc = 0;
     496        1266 :     nbits_tcx = 0;
     497             : 
     498        1266 :     flag_ctx_hm = 0;
     499             : 
     500             :     /* Useful parameters */
     501        1266 :     move16();
     502        1266 :     move16();
     503        1266 :     move16();
     504        1266 :     nb_subfr = st->nb_subfr;
     505        1266 :     core = st->core;
     506        1266 :     last_core = st->last_core;
     507             : 
     508             :     /* Initialize pointers */
     509        1266 :     move16();
     510        1266 :     prm = param;
     511             : 
     512             :     /* Init counters */
     513        1266 :     move16();
     514        1266 :     move16();
     515        1266 :     j = 0;
     516        1266 :     nbits_start = hBstr->nb_bits_tot; /* Q0 */
     517             : 
     518             : 
     519             :     /*--------------------------------------------------------------------------------*
     520             :      * HEADER
     521             :      *--------------------------------------------------------------------------------*/
     522             : 
     523        1266 :     IF( EQ_16( st->mdct_sw, MODE1 ) )
     524             :     {
     525             :         /* Adjust st->bits_frame_core not to subtract MODE2 bandwidth signaling */
     526         260 :         st->bits_frame_core = add( st->bits_frame_core, FrameSizeConfig[st->frame_size_index].bandwidth_bits ); /* Q0 */
     527             : 
     528             :         /* Write MODE1 core mode signaling */
     529         260 :         signalling_mode1_tcx20_enc_fx( st, 1 );
     530             :     }
     531             : 
     532             :     /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
     533             : 
     534        1266 :     IF( st->tcxonly )
     535             :     {
     536           0 :         push_next_indice( hBstr, core == TCX_10_CORE, 1 );
     537             :         {
     538           0 :             index = 3;
     539           0 :             move16();
     540           0 :             test();
     541           0 :             IF( EQ_16( st->clas, UNVOICED_CLAS ) )
     542             :             {
     543           0 :                 index = 0;
     544           0 :                 move16();
     545             :             }
     546           0 :             ELSE IF( ( EQ_16( st->clas, VOICED_TRANSITION ) ) || ( EQ_16( st->clas, UNVOICED_TRANSITION ) ) )
     547             :             {
     548           0 :                 index = 1;
     549           0 :                 move16();
     550             :             }
     551           0 :             ELSE IF( EQ_16( st->clas, VOICED_CLAS ) )
     552             :             {
     553           0 :                 index = 2;
     554           0 :                 move16();
     555             :             }
     556           0 :             push_next_indice( hBstr, index, 2 );
     557             :         }
     558             :     }
     559             :     ELSE
     560             :     {
     561        1266 :         IF( core == ACELP_CORE )
     562             :         {
     563             :             /* write the RF signalling information */
     564         604 :             IF( EQ_16( st->rf_mode, 1 ) )
     565             :             {
     566             :                 /* find the section in the ACELP signalling table corresponding to bitrate */
     567           0 :                 idx = 0;
     568           0 :                 WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) ) /* total bitrate is kept at 13.2kbps */
     569             :                 {
     570           0 :                     idx = add( idx, 1 );
     571             :                 }
     572             : 
     573             :                 /* retrieve the number of bits for signalling */
     574           0 :                 nBits = (Word16) acelp_sig_tbl[++idx]; /* Q0 */
     575             : 
     576             :                 /* retrieve the signalling index */
     577           0 :                 idx = add( idx, 1 ); /* Q0 */
     578           0 :                 start_idx = idx;     /* Q0 */
     579           0 :                 tmp32 = SIG2IND_fx( coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
     580           0 :                 WHILE( NE_32( acelp_sig_tbl[idx], tmp32 ) )
     581             :                 {
     582           0 :                     idx = add( idx, 1 ); /* Q0 */
     583             :                 }
     584           0 :                 push_next_indice( hBstr, idx - start_idx, nBits );
     585           0 :                 push_next_indice( hBstr, 0, 1 ); /* Indicate to the decoder that the core is ACELP*/
     586           0 :                 nbits_start = 3;                 /* Q0 */
     587             :             }
     588             :             ELSE
     589             :             {
     590         604 :                 push_next_indice( hBstr, coder_type, 3 );
     591             :             }
     592             :         }
     593             :         ELSE
     594             :         {
     595         662 :             IF( EQ_16( st->mdct_sw, MODE1 ) )
     596             :             {
     597             :                 /* 2 bits instead of 3 as TCX is already signaled */
     598         260 :                 push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
     599             :             }
     600             :             ELSE
     601             :             {
     602         402 :                 IF( EQ_16( st->mdct_sw_enable, MODE2 ) )
     603             :                 {
     604         402 :                     push_next_indice( hBstr, 1, 1 ); /* TCX */
     605         402 :                     push_next_indice( hBstr, 0, 1 ); /* not HQ_CORE */
     606         402 :                     push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
     607             :                 }
     608             :                 ELSE
     609             :                 {
     610             :                     /*write the RF signalling information*/
     611           0 :                     IF( EQ_16( st->rf_mode, 1 ) )
     612             :                     {
     613             :                         /* find the section in the ACELP signalling table corresponding to bitrate */
     614           0 :                         idx = 0;
     615           0 :                         WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) )
     616             :                         {
     617           0 :                             idx = add( idx, 1 );
     618             :                         }
     619             : 
     620             :                         /* retrieve the number of bits for signalling */
     621           0 :                         nBits = (Word16) acelp_sig_tbl[++idx]; /* Q0 */
     622             : 
     623           0 :                         test();
     624           0 :                         test();
     625           0 :                         IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) ||
     626             :                             EQ_16( st->hTcxCfg->coder_type, GENERIC ) ||
     627             :                             EQ_16( st->hTcxCfg->coder_type, TRANSITION ) )
     628             :                         {
     629           0 :                             st->sharpFlag = 1;
     630             :                         }
     631             :                         ELSE
     632             :                         {
     633           0 :                             st->sharpFlag = 0;
     634             :                         }
     635             : 
     636             :                         /* retrieve the signalling index */
     637           0 :                         idx = add( idx, 1 ); /* Q0 */
     638           0 :                         start_idx = idx;     /* Q0 */
     639           0 :                         tmp32 = SIG2IND_fx( st->hTcxCfg->coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
     640           0 :                         WHILE( NE_32( acelp_sig_tbl[idx], tmp32 ) )
     641             :                         {
     642           0 :                             idx = add( idx, 1 );
     643             :                         }
     644           0 :                         push_next_indice( hBstr, idx - start_idx, nBits );
     645           0 :                         push_next_indice( hBstr, 1, 1 ); /* Indicate to the decoder that the core is TCX*/
     646           0 :                         nbits_start = 3;
     647             :                     }
     648             :                     ELSE
     649             :                     {
     650           0 :                         push_next_indice( hBstr, 4 + st->hTcxCfg->coder_type, 3 );
     651             :                     }
     652             :                 }
     653             :             }
     654             :         }
     655             :     }
     656             : 
     657             :     /* Encode previous mode for error concealment */
     658        1266 :     test();
     659        1266 :     IF( !( core == ACELP_CORE && st->hTcxCfg->lfacNext <= 0 ) )
     660             :     {
     661         662 :         tmp = 0;
     662         662 :         move16();
     663         662 :         test();
     664         662 :         IF( NE_16( last_core, ACELP_CORE ) || EQ_16( core, TCX_10_CORE ) )
     665             : 
     666             :         {
     667         623 :             tmp = TCX_20_CORE;
     668         623 :             move16();
     669             :         }
     670         662 :         push_next_indice( hBstr, tmp, 1 );
     671             :     }
     672             : 
     673             :     /* write TCX overlap mode (1 bit: full, 2 bits: half or no overlap) */
     674        1266 :     IF( core != ACELP_CORE )
     675             :     {
     676             :         Word16 overlap_code;
     677         662 :         assert( st->hTcxCfg->tcx_curr_overlap_mode != NOT_SUPPORTED && st->hTcxCfg->tcx_curr_overlap_mode <= ALDO_WINDOW && st->hTcxCfg->tcx_curr_overlap_mode >= FULL_OVERLAP ); /*1 is not allowed!*/
     678         662 :         IF( EQ_16( st->hTcxCfg->tcx_curr_overlap_mode, MIN_OVERLAP ) )
     679             :         {
     680           6 :             nbits_tcx = 2;
     681           6 :             move16();
     682           6 :             overlap_code = 2;
     683           6 :             move16();
     684             :         }
     685         656 :         ELSE IF( EQ_16( st->hTcxCfg->tcx_curr_overlap_mode, HALF_OVERLAP ) )
     686             :         {
     687           4 :             nbits_tcx = 2;
     688           4 :             move16();
     689           4 :             overlap_code = 3;
     690           4 :             move16();
     691             :         }
     692             :         ELSE
     693             :         {
     694         652 :             nbits_tcx = 1;
     695         652 :             move16();
     696         652 :             overlap_code = 0;
     697         652 :             move16();
     698             :         }
     699         662 :         push_next_indice( hBstr, overlap_code, nbits_tcx );
     700             :     }
     701             : 
     702        1266 :     IF( st->hPlcExt->enableGplc )
     703             :     {
     704             :         /* encode side information. */
     705        1006 :         enc_prm_side_Info_fx( st->hPlcExt, st );
     706             :     }
     707             : 
     708        1266 :     IF( st->glr != 0 )
     709             :     {
     710        1006 :         test();
     711        1006 :         test();
     712        1006 :         test();
     713        1006 :         test();
     714        1006 :         if ( core != ACELP_CORE || EQ_16( coder_type, INACTIVE ) || ( st->last_core == ACELP_CORE && EQ_16( st->last_coder_type_raw, INACTIVE ) ) || st->glr_reset != 0 )
     715             :         {
     716         406 :             st->glr_idx[0] = 0;
     717         406 :             move16();
     718             :         }
     719             : 
     720        1006 :         IF( EQ_16( core, ACELP_CORE ) )
     721             :         {
     722         604 :             push_next_indice( hBstr, st->glr_idx[0], G_LPC_RECOVERY_BITS );
     723             :         }
     724             :     }
     725             : 
     726        1266 :     st->glr_reset = 0;
     727        1266 :     move16();
     728             : 
     729        1266 :     nbits_header = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
     730             : 
     731             : 
     732             :     /*--------------------------------------------------------------------------------*
     733             :      * LPC PARAMETERS
     734             :      *--------------------------------------------------------------------------------*/
     735             : 
     736        1266 :     IF( s_and( st->enableTcxLpc != 0, core != ACELP_CORE ) )
     737             :     {
     738             :         /* Encode the indices */
     739           0 :         nbits_lpc = enc_lsf_tcxlpc_fx( &param_lpc, hBstr ); /* Q0 */
     740             :     }
     741             :     ELSE
     742             :     {
     743        1266 :         IF( st->lpcQuantization == 0 )
     744             :         {
     745             :             /* LPC quantizer */
     746           0 :             numlpc = 2;
     747           0 :             move16();
     748           0 :             if ( EQ_16( core, TCX_20_CORE ) )
     749             :             {
     750           0 :                 numlpc = 1;
     751           0 :                 move16();
     752             :             }
     753             : 
     754           0 :             nbits_lpc = encode_lpc_avq_fx( hBstr, numlpc, param_lpc, st->core, st->element_mode ); /* Q0 */
     755             :         }
     756        1266 :         ELSE IF( EQ_16( st->lpcQuantization, 1 ) )
     757             :         {
     758        1266 :             test();
     759        1266 :             test();
     760        1266 :             IF( EQ_32( st->sr_core, 16000 ) && EQ_16( coder_type, VOICED ) && EQ_16( core, ACELP_CORE ) )
     761             :             {
     762         266 :                 nbits_lpc = lsf_bctcvq_encprm_fx( hBstr, param_lpc, bits_param_lpc, no_param_lpc ); /* Q0 */
     763             :             }
     764             :             ELSE
     765             :             {
     766        1000 :                 nbits_lpc = lsf_msvq_ma_encprm_fx( hBstr, param_lpc, core, coder_type, st->acelp_cfg.midLpc, bits_param_lpc, no_param_lpc ); /* Q0 */
     767             :             }
     768             :         }
     769             :         ELSE
     770             :         {
     771           0 :             assert( 0 && "LPC quant not supported!" );
     772             :         }
     773             :     }
     774             : 
     775             : 
     776             :     /*--------------------------------------------------------------------------------*
     777             :      * PRINT BIT ALLOCATION
     778             :      *--------------------------------------------------------------------------------*/
     779             : 
     780             : 
     781             :     /*--------------------------------------------------------------------------------*
     782             :      * ACELP
     783             :      *--------------------------------------------------------------------------------*/
     784             : 
     785        1266 :     IF( core == ACELP_CORE )
     786             :     {
     787             :         /* Adaptive BPF (2 bits)*/
     788         604 :         n = ACELP_BPF_BITS[st->acelp_cfg.bpf_mode];
     789             : 
     790         604 :         IF( n != 0 )
     791             :         {
     792         573 :             push_next_indice( hBstr, st->bpf_gain_param, n );
     793             :         }
     794             : 
     795             :         /* Mean energy (2 or 3 bits) */
     796         604 :         n = ACELP_NRG_BITS[st->acelp_cfg.nrg_mode]; /* Q0 */
     797             : 
     798         604 :         IF( n != 0 )
     799             :         {
     800         604 :             push_next_indice( hBstr, prm[j++], n );
     801             :         }
     802             : 
     803             :         /* Subframe parameters */
     804             : 
     805        3624 :         FOR( sfr = 0; sfr < nb_subfr; sfr++ )
     806             :         {
     807             :             /* Pitch lag (4, 5, 6, 8 or 9 bits) */
     808        3020 :             move16();
     809        3020 :             n = ACELP_LTP_BITS_SFR[st->acelp_cfg.ltp_mode][sfr]; /* Q0 */
     810             : 
     811        3020 :             IF( n != 0 )
     812             :             {
     813        3020 :                 push_next_indice( hBstr, prm[j++], n );
     814             :             }
     815             : 
     816             :             /* Adaptive codebook filtering (1 bit) */
     817             : 
     818        3020 :             IF( EQ_16( st->acelp_cfg.ltf_mode, 2 ) )
     819             :             {
     820        1330 :                 push_next_indice( hBstr, prm[j++], 1 );
     821             :             }
     822             : 
     823             :             /*Innovative codebook*/
     824             :             {
     825        3020 :                 move16();
     826        3020 :                 j_old = j;
     827             : 
     828        3020 :                 if ( ( st->acelp_cfg.fixed_cdk_index[sfr] >= ACELP_FIXED_CDK_NB ) || ( st->acelp_cfg.fixed_cdk_index[sfr] < 0 ) )
     829             :                 {
     830           0 :                     fprintf( stderr, "ACELP bits allocation: wrong fixed cdk bit allocation\n" );
     831           0 :                     assert( 0 );
     832             :                 }
     833             : 
     834             : 
     835        3020 :                 wordcnt = shr( ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ), 4 );
     836             : 
     837        3020 :                 bitcnt = s_and( ACELP_FIXED_CDK_BITS( st->acelp_cfg.fixed_cdk_index[sfr] ), 15 );
     838             : 
     839             : 
     840       12224 :                 FOR( ix = 0; ix < wordcnt; ix++ )
     841             :                 {
     842        9204 :                     push_next_indice( hBstr, prm[j++], 16 );
     843             :                 }
     844             : 
     845        3020 :                 IF( bitcnt )
     846             :                 {
     847        3020 :                     push_next_indice( hBstr, prm[j++], bitcnt );
     848             :                 }
     849             : 
     850        3020 :                 j = add( j_old, 8 ); /* Q0 */
     851             :             }
     852             : 
     853             :             /* Gains (5b, 6b or 7b / subfr) */
     854        3020 :             n = ACELP_GAINS_BITS[st->acelp_cfg.gains_mode[sfr]];
     855        3020 :             push_next_indice( hBstr, prm[j++], n );
     856             :         } /*end of for(sfr)*/
     857             :     }     /*end of mode[0]==0*/
     858             : 
     859             : 
     860             :     /*--------------------------------------------------------------------------------*
     861             :      * TCX20
     862             :      *--------------------------------------------------------------------------------*/
     863        1266 :     IF( s_or( (Word16) EQ_16( core, TCX_20_CORE ), (Word16) EQ_16( core, HQ_CORE ) ) )
     864             :     {
     865         662 :         flag_ctx_hm = 0;
     866         662 :         move16();
     867         662 :         IF( st->enablePlcWaveadjust )
     868             :         {
     869           0 :             push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
     870             :         }
     871             : 
     872             :         /* TCX Gain = 7 bits */
     873         662 :         push_next_indice( hBstr, prm[j++], 7 );
     874             : 
     875             :         /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
     876         662 :         push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
     877             : 
     878             :         /* LTP data */
     879         662 :         test();
     880         662 :         IF( hTcxEnc->tcxltp || GT_32( st->sr_core, 25600 ) )
     881             :         {
     882         662 :             IF( prm[j] )
     883             :             {
     884         466 :                 push_next_indice( hBstr, 1, 1 );
     885         466 :                 push_next_indice( hBstr, prm[j + 1], 9 );
     886         466 :                 push_next_indice( hBstr, prm[j + 2], 2 );
     887             :             }
     888             :             ELSE
     889             :             {
     890         196 :                 push_next_indice( hBstr, 0, 1 );
     891             :             }
     892             :         }
     893         662 :         j = add( j, 3 );
     894             : 
     895             :         /* TCX spectral data */
     896         662 :         lg = L_frame; /* Q0 */
     897         662 :         move16();
     898         662 :         lgFB = st->hTcxCfg->tcx_coded_lines; /* Q0 */
     899         662 :         move16();
     900             : 
     901         662 :         IF( last_core == ACELP_CORE )
     902             :         {
     903             :             /* ACE->TCX transition */
     904          39 :             lg = add( lg, st->hTcxCfg->tcx_offset );
     905          39 :             lgFB = add( lgFB, shr( lgFB, 2 ) );
     906          39 :             if ( st->hTcxCfg->lfacNext < 0 )
     907             :             {
     908          39 :                 lg = sub( lg, st->hTcxCfg->lfacNext ); /* Q0 */
     909             :             }
     910             :         }
     911             : 
     912             :         /* TNS data */
     913         662 :         nTnsParams = 0;
     914         662 :         move16();
     915         662 :         nTnsBits = 0;
     916         662 :         move16();
     917             : 
     918         662 :         IF( st->hTcxCfg->fIsTNSAllowed )
     919             :         {
     920         402 :             WriteTnsData_fx( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, st, &nTnsBits );
     921         402 :             j = add( j, nTnsParams ); /* Q0 */
     922             :         }
     923             : 
     924         662 :         hm_size = shl( mult( st->hTcxCfg->bandwidth, lg ), 1 );
     925         662 :         test();
     926         662 :         IF( hTcxEnc->tcx_lpc_shaped_ari && NE_16( last_core, ACELP_CORE ) )
     927             :         {
     928           0 :             enc_prm_hm( &prm[j], st, hm_size );
     929             :         }
     930             : 
     931             :         /*Context HM flag*/
     932         662 :         test();
     933         662 :         IF( st->hTcxCfg->ctx_hm && NE_16( last_core, ACELP_CORE ) )
     934             :         {
     935         623 :             push_next_indice( hBstr, prm[j], 1 );
     936             : 
     937         623 :             IF( prm[j] )
     938             :             {
     939          55 :                 EncodeIndex_fx( sub( hm_size, 256 ) >= 0, prm[j + 1], hBstr );
     940          55 :                 flag_ctx_hm = 1;
     941          55 :                 move16();
     942             :             }
     943             :         }
     944         662 :         j = add( j, NPRM_CTX_HM );
     945             : 
     946         662 :         IF( st->igf )
     947             :         {
     948         662 :             st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
     949         662 :             move16();
     950         662 :             IF( EQ_16( st->last_core, ACELP_CORE ) )
     951             :             {
     952          39 :                 IGFEncWriteBitstream_fx( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, IGF_GRID_LB_TRAN, 1 );
     953             :             }
     954             :             ELSE
     955             :             {
     956         623 :                 IGFEncWriteBitstream_fx( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, IGF_GRID_LB_NORM, 1 );
     957             :             }
     958             :         }
     959         662 :         total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
     960         662 :         if ( EQ_16( st->rf_mode, 1 ) )
     961             :         {
     962           0 :             total_nbbits = add( total_nbbits, st->rf_target_bits_write ); /* Q0 */
     963             :         }
     964         662 :         nbits_tcx = sub( st->bits_frame_core, total_nbbits ); /* Q0 */
     965             : 
     966         662 :         IF( hTcxEnc->tcx_lpc_shaped_ari != 0 )
     967             :         {
     968           0 :             push_next_bits( hBstr, (UWord16 *) &prm[++j], nbits_tcx );
     969           0 :             j = add( j, nbits_tcx ); /* Q0 */
     970             :         }
     971             :         ELSE
     972             :         {
     973         662 :             phm_cfg = NULL;
     974         662 :             move16();
     975         662 :             if ( flag_ctx_hm )
     976             :             {
     977          55 :                 phm_cfg = hm_cfg;
     978          55 :                 move16();
     979             :             }
     980         662 :             ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j,
     981             :                                                        lgFB,
     982         662 :                                                        prm[j - 1], /* lastnz */
     983         662 :                                                        nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, phm_cfg );
     984             :         }
     985             :     }
     986             : 
     987             : 
     988             :     /*--------------------------------------------------------------------------------*
     989             :      * TCX10
     990             :      *--------------------------------------------------------------------------------*/
     991             : 
     992             : 
     993        1266 :     IF( EQ_16( core, TCX_10_CORE ) )
     994             :     {
     995           0 :         Word16 nbits_igf = 0;
     996           0 :         move16();
     997           0 :         IF( st->igf )
     998             :         {
     999           0 :             nbits_igf = IGFEncWriteConcatenatedBitstream_fx( st->hIGFEnc, hBstr ); /* Q0 */
    1000             :         }
    1001           0 :         FOR( k = 0; k < 2; k++ )
    1002             :         {
    1003           0 :             flag_ctx_hm = 0;
    1004           0 :             move16();
    1005             : 
    1006           0 :             move16();
    1007           0 :             move16();
    1008           0 :             prm = param + ( k * NPRM_DIV );
    1009             : 
    1010           0 :             j = 0;
    1011             : 
    1012           0 :             move16();
    1013           0 :             nbits_tcx = total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
    1014             : 
    1015           0 :             test();
    1016           0 :             IF( st->enablePlcWaveadjust && k )
    1017             :             {
    1018           0 :                 push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
    1019             :             }
    1020             : 
    1021             :             /* TCX Gain = 7 bits */
    1022           0 :             push_next_indice( hBstr, prm[j++], 7 );
    1023             : 
    1024             :             /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
    1025           0 :             push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
    1026             : 
    1027             :             /* LTP data */
    1028           0 :             test();
    1029           0 :             test();
    1030           0 :             IF( ( k == 0 ) && ( hTcxEnc->tcxltp != 0 || GT_32( st->sr_core, 25600 ) ) ) /* PLC pitch info for HB */
    1031             :             {
    1032           0 :                 IF( prm[j] )
    1033             :                 {
    1034           0 :                     push_next_indice( hBstr, 1, 1 );
    1035           0 :                     push_next_indice( hBstr, prm[j + 1], 9 );
    1036           0 :                     push_next_indice( hBstr, prm[j + 2], 2 );
    1037             :                 }
    1038             :                 ELSE
    1039             :                 {
    1040           0 :                     push_next_indice( hBstr, 0, 1 );
    1041             :                 }
    1042             :             }
    1043           0 :             j = add( j, 3 );
    1044             : 
    1045             :             /* TCX spectral data */
    1046           0 :             lg = shr( L_frame, 1 );
    1047           0 :             lgFB = shr( st->hTcxCfg->tcx_coded_lines, 1 );
    1048             : 
    1049           0 :             IF( s_and( k == 0, last_core == ACELP_CORE ) )
    1050             :             {
    1051             :                 /* ACE->TCX transition */
    1052           0 :                 lg = add( lg, st->hTcxCfg->tcx_offset ); /* Q0 */
    1053           0 :                 lgFB = add( lgFB, shr( lgFB, 1 ) );
    1054           0 :                 if ( st->hTcxCfg->lfacNext < 0 )
    1055             :                 {
    1056           0 :                     lg = sub( lg, st->hTcxCfg->lfacNext ); /* Q0 */
    1057             :                 }
    1058             :             }
    1059             : 
    1060             :             /* TNS data */
    1061           0 :             move16();
    1062           0 :             move16();
    1063           0 :             nTnsParams = 0;
    1064           0 :             nTnsBits = 0;
    1065             : 
    1066           0 :             IF( st->hTcxCfg->fIsTNSAllowed )
    1067             :             {
    1068             : 
    1069           0 :                 SetTnsConfig( st->hTcxCfg, 0, ( last_core == ACELP_CORE ) && ( k == 0 ) );
    1070             : 
    1071           0 :                 WriteTnsData_fx( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, st, &nTnsBits );
    1072           0 :                 j = add( j, nTnsParams );
    1073             :             }
    1074             : 
    1075           0 :             hm_size = shl( mult( st->hTcxCfg->bandwidth, lgFB ), 1 );
    1076             : 
    1077             :             /*Context HM flag*/
    1078           0 :             test();
    1079           0 :             test();
    1080           0 :             IF( st->hTcxCfg->ctx_hm && !( last_core == ACELP_CORE && k == 0 ) )
    1081             :             {
    1082           0 :                 push_next_indice( hBstr, prm[j], 1 );
    1083             : 
    1084           0 :                 IF( prm[j] )
    1085             :                 {
    1086           0 :                     EncodeIndex_fx( sub( hm_size, 256 ) >= 0, prm[j + 1], hBstr );
    1087           0 :                     flag_ctx_hm = 1;
    1088           0 :                     move16();
    1089             :                 }
    1090             :             }
    1091           0 :             j = add( j, NPRM_CTX_HM );
    1092             : 
    1093           0 :             total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
    1094             : 
    1095           0 :             nbits_tcx = sub( shr( sub( add( sub( sub( sub( st->bits_frame_core, nbits_header ), nbits_lpc ), nbits_igf ), 1 ), k ), 1 ), sub( total_nbbits, nbits_tcx ) ); /* Q0 */
    1096             : 
    1097           0 :             phm_cfg = NULL;
    1098           0 :             move16();
    1099           0 :             if ( flag_ctx_hm )
    1100             :             {
    1101           0 :                 phm_cfg = &hm_cfg[k];
    1102           0 :                 move16();
    1103             :             }
    1104           0 :             ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j,
    1105             :                                                        lgFB,
    1106           0 :                                                        prm[j - 1], /* lastnz */
    1107           0 :                                                        nbits_tcx, NPRM_RESQ * st->hTcxCfg->resq, phm_cfg );
    1108             : 
    1109             :         } /* k, window index */
    1110             :     }
    1111             : 
    1112             : 
    1113             :     /*--------------------------------------------------------------------------------*
    1114             :      * END
    1115             :      *--------------------------------------------------------------------------------*/
    1116             : 
    1117             : 
    1118        1266 :     total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
    1119             : 
    1120             : 
    1121             :     /* Check if total encoded bits does not exceed CBR target bits (->this must never happen) */
    1122        1266 :     if ( st->bits_frame_core && ( total_nbbits > st->bits_frame_core ) )
    1123             :     {
    1124           0 :         fprintf( stderr, "AllocatedBits: %d Used bits: %d \n", st->bits_frame_core, total_nbbits );
    1125           0 :         assert( !"Core totalbits > CBR target bitrate" );
    1126             :     }
    1127             : 
    1128        1266 :     return;
    1129             : }
    1130             : 
    1131             : 
    1132             : /*-------------------------------------------------------------------*
    1133             :  * writeLPCparam_fx()
    1134             :  *
    1135             :  * write LTC parameters
    1136             :  *-------------------------------------------------------------------*/
    1137             : 
    1138      235779 : void writeLPCparam_fx(
    1139             :     Encoder_State *st,             /* i/o: encoder state structure     */
    1140             :     BSTR_ENC_HANDLE hBstr,         /* i/o: bitstream handle            */
    1141             :     const Word16 param_lpc[],      /* i  : LPC parameters to write     Q0*/
    1142             :     const Word16 bits_param_lpc[], /* i  : bits per LPC parameter      Q0*/
    1143             :     const Word16 no_param_lpc,     /* i  : number of LPC parameters    Q0*/
    1144             :     Word16 *nbits_lpc              /* o  : LPC bits written            Q0*/
    1145             : )
    1146             : {
    1147             :     Word16 numlpc;
    1148             : 
    1149      235779 :     test();
    1150      235779 :     IF( st->enableTcxLpc && st->core != ACELP_CORE )
    1151             :     {
    1152             :         /* Encode the indices */
    1153       17495 :         *nbits_lpc = enc_lsf_tcxlpc_ivas_fx( &param_lpc, hBstr ); /* Q0 */
    1154       17495 :         move16();
    1155             :     }
    1156             :     ELSE
    1157             :     {
    1158      218284 :         IF( st->lpcQuantization == 0 )
    1159             :         {
    1160             :             /* LPC quantizer */
    1161      106874 :             IF( EQ_16( st->core, TCX_20_CORE ) )
    1162             :             {
    1163      104815 :                 numlpc = 1;
    1164      104815 :                 move16();
    1165             :             }
    1166             :             ELSE
    1167             :             {
    1168        2059 :                 numlpc = 2;
    1169        2059 :                 move16();
    1170             :             }
    1171             : 
    1172      106874 :             *nbits_lpc = encode_lpc_avq_ivas_fx( hBstr, numlpc, param_lpc, st->core, st->element_mode ); /* Q0 */
    1173      106874 :             move16();
    1174             :         }
    1175      111410 :         ELSE IF( EQ_16( st->lpcQuantization, 1 ) )
    1176             :         {
    1177      111410 :             test();
    1178      111410 :             test();
    1179      111410 :             IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( st->coder_type, VOICED ) && EQ_16( st->core, ACELP_CORE ) )
    1180             :             {
    1181           0 :                 assert( st->element_mode == EVS_MONO );
    1182             : 
    1183           0 :                 *nbits_lpc = lsf_bctcvq_encprm_ivas_fx( hBstr, param_lpc, bits_param_lpc, no_param_lpc ); /* Q0 */
    1184           0 :                 move16();
    1185             :             }
    1186             :             ELSE
    1187             :             {
    1188      111410 :                 *nbits_lpc = lsf_msvq_ma_encprm_ivas_fx( hBstr, param_lpc, st->core, st->coder_type, st->acelp_cfg.midLpc, bits_param_lpc, no_param_lpc ); /* Q0 */
    1189      111410 :                 move16();
    1190             :             }
    1191             :         }
    1192             :         ELSE
    1193             :         {
    1194           0 :             assert( 0 );
    1195             :         }
    1196             :     }
    1197             : 
    1198      235779 :     return;
    1199             : }
    1200             : 
    1201             : /*-------------------------------------------------------------------*
    1202             :  * writeTCXparam_fx()
    1203             :  *
    1204             :  * write TCX core parameters
    1205             :  *-------------------------------------------------------------------*/
    1206             : 
    1207     1455358 : void writeTCXparam_fx(
    1208             :     Encoder_State *st,                /* i/o: Encoder State handle    */
    1209             :     BSTR_ENC_HANDLE hBstr,            /* i/o: bitstream handle        */
    1210             :     CONTEXT_HM_CONFIG hm_cfg[],       /* i/o: HM config               */
    1211             :     Word16 param[],                   /* i  : parameters              Q0*/
    1212             :     const Word16 nbits_header,        /* Q0 */
    1213             :     const Word16 nbits_start,         /* Q0 */
    1214             :     const Word16 nbits_lpc,           /* Q0 */
    1215             :     const Word16 *no_param_tns,       /* i  : number of TNS parameters per subframe          Q0*/
    1216             :     Word16 p_param[2],                /* i/o: pointer to parameters from previous bs writing Q0*/
    1217             :     const Word16 target_bitsTCX10[2], /* Q0 */
    1218             :     const Word16 pre_past_flag        /* Q0 */
    1219             : )
    1220             : {
    1221             :     Word16 *prm;
    1222             :     Word16 j, k, nSubframes, core, last_core;
    1223             :     Word16 lg, lgFB, hm_size, flag_ctx_hm;
    1224             :     Word16 total_nbbits, nbits_igf, nbits_tcx;
    1225             :     Word16 nTnsParams, nTnsBits;
    1226             :     Word16 pre_part, post_part;
    1227             : 
    1228     1455358 :     IF( pre_past_flag == 0 )
    1229             :     {
    1230      609716 :         pre_part = 1;
    1231      609716 :         post_part = 0;
    1232      609716 :         move16();
    1233      609716 :         move16();
    1234             :     }
    1235      845642 :     ELSE IF( EQ_16( pre_past_flag, 1 ) )
    1236             :     {
    1237      609863 :         pre_part = 0;
    1238      609863 :         post_part = 1;
    1239      609863 :         move16();
    1240      609863 :         move16();
    1241             :     }
    1242             :     ELSE
    1243             :     {
    1244      235779 :         pre_part = 1;
    1245      235779 :         post_part = 1;
    1246      235779 :         move16();
    1247      235779 :         move16();
    1248             :     }
    1249             : 
    1250             :     /* Initialization */
    1251     1455358 :     core = st->core;
    1252     1455358 :     last_core = st->last_core;
    1253     1455358 :     nbits_igf = 0;
    1254     1455358 :     move16();
    1255     1455358 :     move16();
    1256     1455358 :     move16();
    1257             : 
    1258     1455358 :     nSubframes = 1;
    1259     1455358 :     move16();
    1260     1455358 :     if ( EQ_16( core, TCX_10_CORE ) )
    1261             :     {
    1262       29539 :         nSubframes = 2;
    1263       29539 :         move16();
    1264             :     }
    1265             : 
    1266             :     /* loop over subframes */
    1267     2940255 :     FOR( k = 0; k < nSubframes; k++ )
    1268             :     {
    1269     1484897 :         test();
    1270     1484897 :         test();
    1271     1484897 :         test();
    1272     1484897 :         IF( LT_16( st->element_mode, IVAS_CPE_MDCT ) && k == 0 && st->igf && EQ_16( core, TCX_10_CORE ) )
    1273             :         {
    1274        1996 :             nbits_igf = IGFEncWriteConcatenatedBitstream_ivas_fx( st->hIGFEnc, hBstr ); /* Q0 */
    1275             :         }
    1276             : 
    1277     1484897 :         flag_ctx_hm = 0;
    1278     1484897 :         move16();
    1279             : 
    1280     1484897 :         prm = param + imult1616( k, NPRM_DIV );
    1281     1484897 :         j = 0;
    1282     1484897 :         move16();
    1283             : 
    1284     1484897 :         nbits_tcx = total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
    1285     1484897 :         move16();
    1286             : 
    1287     1484897 :         test();
    1288     1484897 :         test();
    1289     1484897 :         IF( pre_part && st->enablePlcWaveadjust && EQ_16( k, sub( nSubframes, 1 ) ) )
    1290             :         {
    1291           0 :             push_next_indice( hBstr, st->Tonal_SideInfo, 1 );
    1292             :         }
    1293             : 
    1294     1484897 :         IF( post_part )
    1295             :         {
    1296             :             /* TCX Gain */
    1297      861441 :             push_next_indice( hBstr, prm[j++], NBITS_TCX_GAIN );
    1298             : 
    1299             :             /* TCX Noise Filling = NBITS_NOISE_FILL_LEVEL bits */
    1300      861441 :             push_next_indice( hBstr, prm[j++], NBITS_NOISE_FILL_LEVEL );
    1301             :         }
    1302             :         ELSE
    1303             :         {
    1304      623456 :             j = add( j, 1 + NOISE_FILL_RANGES );
    1305             :         }
    1306             : 
    1307             :         /* LTP data */
    1308     1484897 :         IF( pre_part )
    1309             :         {
    1310      861294 :             test();
    1311      861294 :             test();
    1312      861294 :             IF( ( k == 0 ) && ( st->hTcxEnc->tcxltp || GT_32( st->sr_core, 25600 ) ) ) /* PLC pitch info for HB */
    1313             :             {
    1314      845495 :                 IF( prm[j] )
    1315             :                 {
    1316      564775 :                     push_next_indice( hBstr, 1, 1 );
    1317      564775 :                     push_next_indice( hBstr, prm[j + 1], 9 );
    1318      564775 :                     push_next_indice( hBstr, prm[j + 2], 2 );
    1319             :                 }
    1320             :                 ELSE
    1321             :                 {
    1322      280720 :                     push_next_indice( hBstr, 0, 1 );
    1323             :                 }
    1324             :             }
    1325      861294 :             j = add( j, 3 );
    1326             :         }
    1327             :         ELSE
    1328             :         {
    1329      623603 :             j = add( j, sub( p_param[k], 2 ) ); /* Q0 */
    1330             :         }
    1331             : 
    1332             :         /* TCX spectral data */
    1333     1484897 :         lg = shr( st->last_L_frame, sub( nSubframes, 1 ) );
    1334     1484897 :         lgFB = shr( st->hTcxCfg->tcx_coded_lines, sub( nSubframes, 1 ) );
    1335             : 
    1336     1484897 :         test();
    1337     1484897 :         test();
    1338     1484897 :         IF( post_part && k == 0 && last_core == ACELP_CORE )
    1339             :         {
    1340             :             /* ACE->TCX transition */
    1341        9214 :             lg = add( lg, st->hTcxCfg->tcx_offset );               /* Q0 */
    1342        9214 :             lgFB = add( lgFB, shr( lgFB, sub( 3, nSubframes ) ) ); /* Q0 */
    1343             : 
    1344        9214 :             IF( st->hTcxCfg->lfacNext < 0 )
    1345             :             {
    1346        9214 :                 lg = sub( lg, st->hTcxCfg->lfacNext ); /* Q0 */
    1347             :             }
    1348             :         }
    1349             : 
    1350     1484897 :         IF( pre_part )
    1351             :         {
    1352             :             /* TNS data */
    1353      861294 :             nTnsParams = 0;
    1354      861294 :             nTnsBits = 0;
    1355      861294 :             move16();
    1356      861294 :             move16();
    1357             : 
    1358      861294 :             IF( st->hTcxCfg->fIsTNSAllowed )
    1359             :             {
    1360      785163 :                 test();
    1361      785163 :                 SetTnsConfig( st->hTcxCfg, core == TCX_20_CORE, ( last_core == ACELP_CORE ) && ( k == 0 ) );
    1362             : 
    1363      785163 :                 IF( no_param_tns )
    1364             :                 {
    1365       10662 :                     IF( st->hTcxEnc->tnsData[k].nFilters < 0 )
    1366             :                     {
    1367        2481 :                         push_next_indice( hBstr, 1, 1 ); /* common_tns_data[] for subframe k */
    1368             :                     }
    1369             :                     ELSE
    1370             :                     {
    1371        8181 :                         push_next_indice( hBstr, 0, 1 ); /* common_tns_data[] for subframe k */
    1372             :                     }
    1373             :                 }
    1374      785163 :                 test();
    1375      785163 :                 IF( no_param_tns && ( st->hTcxEnc->tnsData[k].nFilters < 0 ) )
    1376             :                 {
    1377             :                     /* a negative filter count means that the filters are identical to those in the first channel at the same sub-frame */
    1378        2481 :                     nTnsParams = no_param_tns[k]; /* Q0 */
    1379        2481 :                     move16();
    1380             :                 }
    1381             :                 ELSE
    1382             :                 {
    1383      782682 :                     WriteTnsData_ivas_fx( st->hTcxCfg->pCurrentTnsConfig, prm + j, &nTnsParams, hBstr, &nTnsBits );
    1384             :                 }
    1385      785163 :                 IF( no_param_tns )
    1386             :                 {
    1387       10662 :                     nTnsBits = add( nTnsBits, 1 ); /* Q0 */
    1388             :                 }
    1389      785163 :                 j = add( j, nTnsParams ); /* Q0 */
    1390             :             }
    1391             : 
    1392      861294 :             IF( post_part )
    1393             :             {
    1394      237838 :                 hm_size = imult1616( mult( 2, st->hTcxCfg->bandwidth ), lg ); /* Q0 */
    1395             : 
    1396      237838 :                 test();
    1397      237838 :                 test();
    1398      237838 :                 IF( st->hTcxEnc->tcx_lpc_shaped_ari && last_core != ACELP_CORE && EQ_16( core, TCX_20_CORE ) )
    1399             :                 {
    1400       15849 :                     enc_prm_hm_ivas_fx( &prm[j], st, hm_size );
    1401             :                 }
    1402             : 
    1403             :                 /*Context HM flag*/
    1404      237838 :                 test();
    1405      237838 :                 test();
    1406      237838 :                 test();
    1407      237838 :                 IF( st->hTcxCfg != NULL && st->hTcxCfg->ctx_hm && !( last_core == ACELP_CORE && k == 0 ) )
    1408             :                 {
    1409      180593 :                     push_next_indice( hBstr, prm[j], 1 );
    1410             : 
    1411      180593 :                     IF( prm[j] )
    1412             :                     {
    1413       12616 :                         EncodeIndex_ivas_fx( (Word16) GE_16( hm_size, 256 ), prm[j + 1], hBstr );
    1414             : 
    1415       12616 :                         flag_ctx_hm = 1;
    1416       12616 :                         move16();
    1417             :                     }
    1418             :                 }
    1419             :             }
    1420      861294 :             j = add( j, NPRM_CTX_HM );
    1421             : 
    1422      861294 :             IF( post_part )
    1423             :             {
    1424             :                 /* IGF data */
    1425      237838 :                 IF( st->igf && EQ_16( core, TCX_20_CORE ) )
    1426             :                 {
    1427             :                     Word16 gridIdx;
    1428      223105 :                     gridIdx = IGF_GRID_LB_NORM;
    1429      223105 :                     move16();
    1430      223105 :                     if ( last_core == ACELP_CORE )
    1431             :                     {
    1432        7485 :                         gridIdx = IGF_GRID_LB_TRAN;
    1433        7485 :                         move16();
    1434             :                     }
    1435      223105 :                     st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    1436      223105 :                     move16();
    1437      223105 :                     IGFEncWriteBitstream_ivas_fx( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, gridIdx, 1 );
    1438             :                 }
    1439             : 
    1440      237838 :                 total_nbbits = sub( hBstr->nb_bits_tot, nbits_start ); /* Q0 */
    1441             : 
    1442      237838 :                 IF( EQ_16( core, TCX_20_CORE ) )
    1443             :                 {
    1444      233720 :                     IF( st->rf_mode )
    1445             :                     {
    1446           0 :                         total_nbbits = add( total_nbbits, st->rf_target_bits_write ); /* Q0 */
    1447             :                     }
    1448      233720 :                     nbits_tcx = sub( st->bits_frame_core, total_nbbits ); /* Q0 */
    1449             :                 }
    1450             :                 ELSE /* TCX_10_CORE */
    1451             :                 {
    1452        4118 :                     nbits_tcx = sub( shr( sub( add( sub( sub( sub( st->bits_frame_core, nbits_header ), nbits_lpc ), nbits_igf ), 1 ), k ), 1 ), sub( total_nbbits, nbits_tcx ) ); /* Q0 */
    1453             :                 }
    1454             :             }
    1455             :             ELSE
    1456             :             {
    1457             :                 /*Context HM flag*/
    1458      623456 :                 p_param[k] = j; /* Q0 */
    1459      623456 :                 move16();
    1460             :             }
    1461             :         }
    1462             :         ELSE
    1463             :         {
    1464      623603 :             nbits_tcx = st->bits_frame_channel; /* Q0 */
    1465      623603 :             move16();
    1466      623603 :             IF( EQ_16( core, TCX_10_CORE ) )
    1467             :             {
    1468       27480 :                 nbits_tcx = sub( target_bitsTCX10[k], NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ); /* Q0 */
    1469             :             }
    1470             :         }
    1471             : 
    1472     1484897 :         IF( post_part )
    1473             :         {
    1474      861441 :             test();
    1475      861441 :             IF( st->hTcxEnc->tcx_lpc_shaped_ari && EQ_16( core, TCX_20_CORE ) )
    1476             :             {
    1477       17495 :                 push_next_bits( hBstr, (UWord16 *) &prm[++j], nbits_tcx );
    1478       17495 :                 j = add( j, nbits_tcx ); /* Q0 */
    1479             :             }
    1480             :             ELSE
    1481             :             {
    1482      843946 :                 IF( st->element_mode > EVS_MONO )
    1483             :                 {
    1484      843946 :                     IF( flag_ctx_hm )
    1485             :                     {
    1486       12616 :                         RCcontextMapping_encode2_no_mem_s17_LCS_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
    1487       12616 :                                                                     nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), &hm_cfg[k] );
    1488             :                     }
    1489             :                     ELSE
    1490             :                     {
    1491      831330 :                         RCcontextMapping_encode2_no_mem_s17_LCS_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
    1492      831330 :                                                                     nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), NULL );
    1493             :                     }
    1494             :                 }
    1495             :                 ELSE
    1496             :                 {
    1497           0 :                     IF( flag_ctx_hm )
    1498             :                     {
    1499           0 :                         ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
    1500           0 :                                                                    nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), &hm_cfg[k] );
    1501             :                     }
    1502             :                     ELSE
    1503             :                     {
    1504           0 :                         ACcontextMapping_encode2_no_mem_s17_LC_fx( hBstr, prm + j, lgFB, prm[j - 1], /* lastnz */
    1505           0 :                                                                    nbits_tcx, imult1616( NPRM_RESQ, st->hTcxCfg->resq ), NULL );
    1506             :                     }
    1507             :                 }
    1508             :             }
    1509             :         }
    1510             :     }
    1511             : 
    1512     1455358 :     return;
    1513             : }
    1514             : 
    1515             : /*-------------------------------------------------------------------*
    1516             :  * writeTCXMode_fx()
    1517             :  *
    1518             :  * write TCX mode
    1519             :  *--------------------------------------------------------------------*/
    1520             : 
    1521      845495 : void writeTCXMode_fx(
    1522             :     Encoder_State *st,     /* i/o: encoder state structure                                      */
    1523             :     BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle                                                     */
    1524             :     const Word16 MCT_flag, /* i  : hMCT handle allocated (1) or not (0)         Q0*/
    1525             :     Word16 *nbits_start    /* o  : nbits start                                                          Q0*/
    1526             : )
    1527             : {
    1528             :     UWord16 index;
    1529             :     Word16 idx, start_idx;
    1530             :     Word16 nBits;
    1531             : 
    1532      845495 :     IF( st->tcxonly )
    1533             :     {
    1534      716590 :         push_next_indice( hBstr, (UWord16) EQ_16( st->core, TCX_10_CORE ), 1 );
    1535             : 
    1536      716590 :         test();
    1537      716590 :         IF( EQ_16( st->clas, UNVOICED_CLAS ) )
    1538             :         {
    1539      370702 :             index = 0;
    1540      370702 :             move16();
    1541             :         }
    1542      345888 :         ELSE IF( EQ_16( st->clas, VOICED_TRANSITION ) || EQ_16( st->clas, UNVOICED_TRANSITION ) )
    1543             :         {
    1544       44216 :             index = 1;
    1545       44216 :             move16();
    1546             :         }
    1547      301672 :         ELSE IF( EQ_16( st->clas, VOICED_CLAS ) )
    1548             :         {
    1549      262721 :             index = 2;
    1550      262721 :             move16();
    1551             :         }
    1552             :         ELSE
    1553             :         {
    1554       38951 :             index = 3;
    1555       38951 :             move16();
    1556             :         }
    1557             : 
    1558      716590 :         push_next_indice( hBstr, index, 2 );
    1559             : 
    1560      716590 :         test();
    1561      716590 :         IF( EQ_16( st->element_mode, IVAS_CPE_MDCT ) && !MCT_flag )
    1562             :         {
    1563      163762 :             push_next_indice( hBstr, st->vad_flag, 1 );
    1564             :         }
    1565             :     }
    1566             :     ELSE
    1567             :     {
    1568      128905 :         IF( EQ_16( st->core, ACELP_CORE ) )
    1569             :         {
    1570             :             /* write the RF signaling information */
    1571           0 :             IF( EQ_16( st->rf_mode, 1 ) )
    1572             :             {
    1573             :                 /* find the section in the ACELP signaling table corresponding to bitrate */
    1574           0 :                 idx = 0;
    1575           0 :                 move16();
    1576           0 :                 WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) ) /* total bitrate is kept at 13.2kbps */
    1577             :                 {
    1578           0 :                     idx = add( idx, 1 );
    1579             :                 }
    1580             : 
    1581             :                 /* retrieve the number of bits for signaling */
    1582           0 :                 idx = add( idx, 1 );                     /* Q0 */
    1583           0 :                 nBits = extract_l( acelp_sig_tbl[idx] ); /* Q0 */
    1584             : 
    1585             :                 /* retrieve the signaling index */
    1586           0 :                 idx = add( idx, 1 );
    1587           0 :                 start_idx = idx;
    1588           0 :                 move16();
    1589           0 :                 WHILE( NE_32( acelp_sig_tbl[idx], SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode ) ) )
    1590             :                 {
    1591           0 :                     idx = add( idx, 1 );
    1592             :                 }
    1593           0 :                 push_next_indice( hBstr, sub( idx, start_idx ), nBits );
    1594           0 :                 push_next_indice( hBstr, 0, 1 ); /* Indicate to the decoder that the core is ACELP*/
    1595           0 :                 *nbits_start = 3;
    1596           0 :                 move16();
    1597             :             }
    1598             :             ELSE
    1599             :             {
    1600           0 :                 push_next_indice( hBstr, st->coder_type, 3 );
    1601             :             }
    1602             :         }
    1603             :         ELSE
    1604             :         {
    1605      128905 :             IF( EQ_16( st->mdct_sw, MODE1 ) )
    1606             :             {
    1607             :                 /* 2 bits instead of 3 as TCX is already signaled */
    1608      128905 :                 push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
    1609             :             }
    1610             :             ELSE
    1611             :             {
    1612           0 :                 IF( EQ_16( st->mdct_sw_enable, MODE2 ) )
    1613             :                 {
    1614           0 :                     push_next_indice( hBstr, 1, 1 ); /* TCX */
    1615           0 :                     push_next_indice( hBstr, 0, 1 ); /* not HQ_CORE */
    1616           0 :                     push_next_indice( hBstr, st->hTcxCfg->coder_type, 2 );
    1617             :                 }
    1618             :                 ELSE
    1619             :                 {
    1620             :                     /*write the RF signaling information*/
    1621           0 :                     IF( EQ_16( st->rf_mode, 1 ) )
    1622             :                     {
    1623             :                         /* find the section in the ACELP signaling table corresponding to bitrate */
    1624           0 :                         idx = 0;
    1625           0 :                         move16();
    1626           0 :                         WHILE( NE_32( acelp_sig_tbl[idx], st->total_brate ) )
    1627             :                         {
    1628           0 :                             idx = add( idx, 1 );
    1629             :                         }
    1630             : 
    1631             :                         /* retrieve the number of bits for signaling */
    1632           0 :                         idx = add( idx, 1 );
    1633           0 :                         nBits = extract_l( acelp_sig_tbl[idx] );
    1634             : 
    1635           0 :                         test();
    1636           0 :                         test();
    1637           0 :                         IF( EQ_16( st->hTcxCfg->coder_type, VOICED ) || EQ_16( st->hTcxCfg->coder_type, GENERIC ) || EQ_16( st->hTcxCfg->coder_type, TRANSITION ) )
    1638             :                         {
    1639           0 :                             st->sharpFlag = 1;
    1640           0 :                             move16();
    1641             :                         }
    1642             :                         ELSE
    1643             :                         {
    1644           0 :                             st->sharpFlag = 0;
    1645           0 :                             move16();
    1646             :                         }
    1647             : 
    1648             :                         /* retrieve the signaling index */
    1649           0 :                         idx = add( idx, 1 );
    1650           0 :                         start_idx = idx;
    1651           0 :                         move16();
    1652           0 :                         WHILE( NE_32( acelp_sig_tbl[idx], SIG2IND( st->hTcxCfg->coder_type, st->bwidth, st->sharpFlag, st->rf_mode ) ) )
    1653             :                         {
    1654           0 :                             idx = add( idx, 1 );
    1655             :                         }
    1656           0 :                         push_next_indice( hBstr, sub( idx, start_idx ), nBits );
    1657           0 :                         push_next_indice( hBstr, 1, 1 ); /* Indicate to the decoder that the core is TCX*/
    1658           0 :                         *nbits_start = 3;
    1659           0 :                         move16();
    1660             :                     }
    1661             :                     ELSE
    1662             :                     {
    1663           0 :                         push_next_indice( hBstr, add( ACELP_MODE_MAX, st->hTcxCfg->coder_type ), 3 );
    1664             :                     }
    1665             :                 }
    1666             :             }
    1667             :         }
    1668             :     }
    1669             : 
    1670      845495 :     return;
    1671             : }
    1672             : 
    1673             : 
    1674             : /*-------------------------------------------------------------------*
    1675             :  * writeTCXWindowing_fx()
    1676             :  *
    1677             :  * write TCX transform type
    1678             :  *-------------------------------------------------------------------*/
    1679             : 
    1680      861294 : void writeTCXWindowing_fx(
    1681             :     BSTR_ENC_HANDLE hBstr,    /* i/o: bitstream handle        */
    1682             :     const Word16 overlap_mode /* i  : overlap mode            Q0*/
    1683             : )
    1684             : {
    1685             : 
    1686      861294 :     IF( EQ_16( overlap_mode, MIN_OVERLAP ) )
    1687             :     {
    1688        6553 :         push_next_indice( hBstr, 2, 2 );
    1689             :     }
    1690      854741 :     ELSE IF( EQ_16( overlap_mode, HALF_OVERLAP ) )
    1691             :     {
    1692       12149 :         push_next_indice( hBstr, 3, 2 );
    1693             :     }
    1694             :     ELSE
    1695             :     {
    1696      842592 :         push_next_indice( hBstr, 0, 1 );
    1697             :     }
    1698             : 
    1699      861294 :     return;
    1700             : }

Generated by: LCOV version 1.14