LCOV - code coverage report
Current view: top level - lib_dec - lsf_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 365 401 91.0 %
Date: 2025-05-03 01:55:50 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include "options.h" /* Compilation switches                   */
       7             : #include "cnst.h"    /* Common constants                       */
       8             : #include "rom_com.h" /* Static table prototypes                */
       9             : #include "prot_fx.h" /* Function prototypes                    */
      10             : #include "ivas_prot_fx.h"
      11             : /*-------------------------------------------------------------------*
      12             :  * Local functions
      13             :  *-------------------------------------------------------------------*/
      14             : static void dqlsf_CNG_fx( Decoder_State *st_fx, Word16 *lsf_q );
      15             : 
      16             : /*--------------------------------------------------------------------------------------*
      17             : * dqlsf_CNG_fx()
      18             : *
      19             : * LSF de-quantizer for SID frames (uses 28 bits, 4 for VQ, 24 for LVQ)
      20             : *
      21             : * Note:
      22             : * LP-CNG LSF decoder does not need to know the sampling rate,
      23             : * the sampling rate data is embedded inside the LSF coefficients.
      24             : * If the highest order LSF coefficient (lsf_q[M-1]) is smaller than 6350 then Fs=12.8kHz
      25             : * If the highest order LSF coefficient (lsf_q[M-1]) is larger than 6350 then Fs=16kHz
      26             : ----------------------------------------------------------------------------------------*/
      27             : 
      28         417 : static void dqlsf_CNG_fx(
      29             :     Decoder_State *st_fx, /* i/o: decoder state structure            */
      30             :     Word16 *lsf_q         /* o  : decoded LSFs                     Q9*/
      31             : )
      32             : {
      33             :     Word16 indice[4];
      34             :     Word16 ber_flag;
      35             : 
      36         417 :     indice[0] = (Word16) get_next_indice_fx( st_fx, 4 ); /* Q0 */
      37         417 :     move16();
      38         417 :     indice[1] = (Word16) get_next_indice_fx( st_fx, LEN_INDICE ); /* Q0 */
      39         417 :     move16();
      40         417 :     indice[2] = (Word16) get_next_indice_fx( st_fx, LSF_BITS_CNG - 4 - LEN_INDICE ); /* Q0 */
      41         417 :     move16();
      42             : 
      43             :     /* deindex_lvq_cng decoder does not need to know the sampling rate, the sampling rate data is embedded inside the LSF coefficients */
      44         417 :     IF( st_fx->element_mode == EVS_MONO )
      45             :     {
      46             :         ber_flag =
      47           0 :             deindex_lvq_cng_fx( &indice[1], lsf_q, indice[0], LSF_BITS_CNG - 4, &st_fx->offset_scale1_fx[0][0], &st_fx->offset_scale2_fx[0][0], &st_fx->no_scales_fx[0][0] ); /* Q0 */
      48             :     }
      49             :     ELSE
      50             :     {
      51             :         ber_flag =
      52         417 :             deindex_lvq_cng_ivas_fx( &indice[1], lsf_q, indice[0], LSF_BITS_CNG - 4 ); /* Q0 */
      53             :     }
      54         417 :     st_fx->BER_detect = s_or( ber_flag, st_fx->BER_detect ); /* Q0 */
      55         417 :     move16();
      56             : 
      57             :     /* The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF coefficient (last coefficient).
      58             :        If the last decoded LSF coefficient is larger than 6350 the decoded frame is WB2 with sampling rate of 16 kHz
      59             :        otherwise it is sampled at 12.8kHz and contains either NB or WB LSF data.  */
      60         417 :     Vr_add( lsf_q, &CNG_SN1_fx[indice[0] * M], lsf_q, M );
      61             : 
      62         417 :     test();
      63         417 :     test();
      64         417 :     test();
      65         417 :     IF( ( ( EQ_16( st_fx->L_frame, L_FRAME16k ) ) && ( LE_16( lsf_q[M - 1], WB_LIMIT_LSF_FX ) ) ) || ( ( LT_16( st_fx->L_frame, L_FRAME16k ) ) && ( GT_16( lsf_q[M - 1], WB_LIMIT_LSF_FX ) ) ) )
      66             :     {
      67           0 :         st_fx->BER_detect = 1; /* Q0 */
      68           0 :         move16();
      69             :     }
      70             : 
      71         417 :     return;
      72             : }
      73             : 
      74             : /*===========================================================================*/
      75             : /* FUNCTION : lsf_dec_fx()                                                   */
      76             : /*---------------------------------------------------------------------------*/
      77             : /* PURPOSE : LSF decoder                                                     */
      78             : /*---------------------------------------------------------------------------*/
      79             : /* INPUT ARGUMENTS :                                                         */
      80             : /* _ (Struct) st_fx       : decoder static memory                            */
      81             : /* _ (Word16) L_frame     : length of the frame                              */
      82             : /* _ (Word16) coder_type  : coding type                                      */
      83             : /* _ (Word16) bwidth      : input signal bandwidth                           */
      84             : /*---------------------------------------------------------------------------*/
      85             : /* OUTPUT ARGUMENTS :                                                        */
      86             : /* _ (Word16*) Aq          : LP filter coefficient    Q12                    */
      87             : /* _ (Word16*) lsf_new      : LP filter coefficient    Q(x2.56)              */
      88             : /* _ (Word16*) lsp_new      : LP filter coefficient    Q15                   */
      89             : /* _ (Word16*) lsp_mid      : LP filter coefficient    Q15                   */
      90             : /*---------------------------------------------------------------------------*/
      91             : 
      92             : /* _ (Word16[]) st_fx->lsf_adaptive_mean_fx  : FEC - adaptive mean LSF       */
      93             : /*                        vector for FEC  Q(x2.56)                           */
      94             : /* _ (Word16[]) st_fx->mem_AR_fx : AR memory of LSF quantizer                */
      95             : /*                  (past quantized LSFs without mean) Q(x2.56)              */
      96             : /* _ (Word16) st_fx->stab_fac_fx : LSF stability factor    Q15               */
      97             : /*---------------------------------------------------------------------------*/
      98             : /* RETURN ARGUMENTS :                                                        */
      99             : /* _ None                                                                    */
     100             : /*===========================================================================*/
     101             : 
     102             : #ifndef REMOVE_EVS_DUPLICATES
     103             : void lsf_dec_fx(
     104             :     Decoder_State *st_fx,          /* i/o: State structure                               */
     105             :     const Word16 tc_subfr,         /* i  : TC subframe index                           Q0*/
     106             :     Word16 *Aq,                    /* o  : quantized A(z) for 4 subframes             Q12*/
     107             :     Word16 *LSF_Q_prediction,      /* o  : LSF prediction mode                         Q0*/
     108             :     Word16 *lsf_new,               /* o  : de-quantized LSF vector               Q(x2.56)*/
     109             :     Word16 *lsp_new,               /* o  : de-quantized LSP vector                    Q15*/
     110             :     Word16 *lsp_mid,               /* o  : de-quantized mid-frame LSP vector          Q15*/
     111             :     const Word16 tdm_low_rate_mode /* i  : secondary channel low rate mode flag            Q0*/
     112             :     ,
     113             :     const Word16 tdm_lsfQ_PCh[M] /* i  : Q LSFs for primary channel                             Qx*/
     114             : )
     115             : {
     116             :     Word16 i;
     117             :     Word16 no_param_lpc;
     118             :     Word16 param_lpc[NPRM_LPC_NEW];
     119             :     Word32 L_tmp;
     120             :     Word16 nBits = 0;
     121             :     move16();
     122             :     Word16 tmp_old[M + 1], tmp_new[M + 1];
     123             :     Word16 enr_old = 0, enr_new = 0;
     124             :     move16();
     125             :     move16();
     126             :     Word16 lsf_diff, coder_type;
     127             : 
     128             :     /* initialize */
     129             :     coder_type = st_fx->coder_type;
     130             :     if ( EQ_32( st_fx->core_brate, SID_2k40 ) )
     131             :     {
     132             :         coder_type = INACTIVE; /* Q0 */
     133             :         move16();
     134             :     }
     135             :     test();
     136             :     if ( EQ_16( coder_type, AUDIO ) && st_fx->GSC_IVAS_mode > 0 )
     137             :     {
     138             :         coder_type = GENERIC; /* Q0 */
     139             :         move16();
     140             :     }
     141             :     no_param_lpc = 0;
     142             :     nBits = 0;
     143             :     move16();
     144             :     move16();
     145             : 
     146             :     /* Find the number of bits for LSF quantization */
     147             :     IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
     148             :     {
     149             :         nBits = LSF_BITS_CNG; /* Q0 */
     150             :         move16();
     151             :     }
     152             :     ELSE
     153             :     {
     154             :         test();
     155             :         IF( st_fx->nelp_mode_dec == 0 && st_fx->ppp_mode_dec == 0 )
     156             :         {
     157             :             nBits = st_fx->acelp_cfg.lsf_bits; /* Q0 */
     158             :             move16();
     159             :         }
     160             :         ELSE IF( EQ_16( st_fx->nelp_mode_dec, 1 ) )
     161             :         {
     162             :             IF( EQ_16( coder_type, UNVOICED ) )
     163             :             {
     164             :                 nBits = 30; /* Q0 */
     165             :                 move16();
     166             :                 if ( st_fx->bwidth == NB )
     167             :                 {
     168             :                     nBits = 32; /* Q0 */
     169             :                     move16();
     170             :                 }
     171             :             }
     172             :         }
     173             :         ELSE IF( EQ_16( st_fx->ppp_mode_dec, 1 ) )
     174             :         {
     175             :             nBits = 26; /* Q0 */
     176             :             move16();
     177             :         }
     178             :     }
     179             : 
     180             :     /* LSF de-quantization */
     181             :     lsf_end_dec_fx( st_fx, 0, coder_type, st_fx->bwidth, nBits, lsf_new, param_lpc, LSF_Q_prediction, &no_param_lpc,
     182             :                     tdm_lsfQ_PCh );
     183             : 
     184             :     /* convert quantized LSFs to LSPs */
     185             : 
     186             :     lsf2lsp_fx( lsf_new, lsp_new, M, st_fx->sr_core );
     187             :     /* set seed_acelp used in UC mode */
     188             :     test();
     189             :     IF( EQ_16( coder_type, UNVOICED ) && GT_16( st_fx->element_mode, EVS_MONO ) )
     190             :     {
     191             :         st_fx->seed_acelp = 0;
     192             :         move16();
     193             :         FOR( i = no_param_lpc - 1; i >= 0; i-- )
     194             :         {
     195             :             /* rightshift before *seed_acelp+param_lpc[i] to avoid overflows*/
     196             :             st_fx->seed_acelp = extract_l( L_add( imult3216( 31821L /* Q0 */, add( shr( ( st_fx->seed_acelp ), 1 ), param_lpc[i] ) ), 13849L /* Q0 */ ) ); /* Q0 */
     197             :             move16();
     198             :         }
     199             :     }
     200             :     IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
     201             :     {
     202             :         /* return if SID frame (conversion to A(z) done in the calling function) */
     203             :         return;
     204             :     }
     205             : 
     206             :     /*-------------------------------------------------------------------------------------*
     207             :      * FEC - update adaptive LSF mean vector
     208             :      *-------------------------------------------------------------------------------------*/
     209             : 
     210             :     FOR( i = 0; i < M; i++ )
     211             :     {
     212             :         L_tmp = L_mult( lsf_new[i], 10922 );                    /*Q(x2.56+16)*/
     213             :         L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi1_fx[i], 10922 ); /*Q(x2.56+16)*/
     214             :         L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi0_fx[i], 10922 ); /*Q(x2.56+16)*/
     215             :         st_fx->lsf_adaptive_mean_fx[i] = round_fx( L_tmp );     /*Q(x2.56)*/
     216             :         move16();
     217             :     }
     218             : 
     219             :     test();
     220             :     test();
     221             :     IF( ( st_fx->prev_bfi && ( EQ_16( coder_type, TRANSITION ) ) && ( EQ_16( tc_subfr, sub( st_fx->L_frame, L_SUBFR ) ) ) ) )
     222             :     {
     223             :         lsf_diff = 1205;
     224             :         move16(); /*int_fs / (float)(2*(M+1)); = 470.588 -> 1205 in Q2.56 */
     225             :         if ( EQ_16( st_fx->L_frame, L_FRAME ) )
     226             :         {
     227             :             lsf_diff = 964;
     228             :             move16(); /*int_fs / (float)(2*(M+1)); = 376.47 -> 964 in Q2.56 */
     229             :         }
     230             :         st_fx->lsf_old_fx[0] = lsf_diff;
     231             :         move16();
     232             : 
     233             :         FOR( i = 1; i < M; i++ )
     234             :         {
     235             :             st_fx->lsf_old_fx[i] = add( st_fx->lsf_old_fx[i - 1], lsf_diff ); /* Q2.56 */
     236             :             move16();
     237             :         }
     238             :         lsf2lsp_fx( st_fx->lsf_old_fx, st_fx->lsp_old_fx, M, st_fx->sr_core );
     239             :     }
     240             :     /*-------------------------------------------------------------------------------------*
     241             :      * Mid-frame LSF decoding
     242             :      * LSP interpolation and conversion of LSPs to A(z)
     243             :      *-------------------------------------------------------------------------------------*/
     244             :     IF( st_fx->rate_switching_reset )
     245             :     {
     246             :         /*extrapolation in case of unstable LSF convert*/
     247             :         Copy( lsp_new, st_fx->lsp_old_fx, M ); /* Q15 */
     248             :         Copy( lsf_new, st_fx->lsf_old_fx, M ); /* Q2.56 */
     249             :     }
     250             :     {
     251             :         /* Mid-frame LSF decoding */
     252             :         lsf_mid_dec_fx( st_fx, lsp_new, coder_type, lsp_mid );
     253             :     }
     254             :     test();
     255             :     test();
     256             :     IF( !( st_fx->prev_bfi && ( EQ_16( coder_type, TRANSITION ) ) && ( EQ_16( tc_subfr, sub( st_fx->L_frame, L_SUBFR ) ) ) ) )
     257             :     {
     258             :         IF( st_fx->prev_bfi )
     259             :         {
     260             :             /* check, if LSP interpolation can be relaxed */
     261             :             E_LPC_f_lsp_a_conversion( st_fx->lsp_old_fx, tmp_old, M );
     262             :             enr_old = Enr_1_Az_fx( tmp_old, 2 * L_SUBFR ); /* Q3 */
     263             : 
     264             :             E_LPC_f_lsp_a_conversion( lsp_new, tmp_new, M );
     265             :             enr_new = Enr_1_Az_fx( tmp_new, 2 * L_SUBFR ); /* Q3 */
     266             : 
     267             :             IF( LT_16( enr_new, mult_r( 9830 /*0.3 Q15*/, enr_old ) ) )
     268             :             {
     269             :                 /* OLD CODE : if( st->safety_net == 1), replaced with a decision similar to MODE2 */
     270             :                 st_fx->relax_prev_lsf_interp = -1;
     271             :                 move16();
     272             :                 test();
     273             :                 test();
     274             :                 test();
     275             :                 test();
     276             :                 if ( st_fx->clas_dec == UNVOICED_CLAS || EQ_16( st_fx->clas_dec, SIN_ONSET ) || st_fx->clas_dec == INACTIVE_CLAS || EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) )
     277             :                 {
     278             :                     st_fx->relax_prev_lsf_interp = 1;
     279             :                     move16();
     280             :                 }
     281             :             }
     282             :         }
     283             :     }
     284             :     test();
     285             :     IF( EQ_16( st_fx->last_core, HQ_CORE ) && st_fx->core == ACELP_CORE )
     286             :     {
     287             :         /* update old LSPs/LSFs in case of HQ->ACELP core switching */
     288             :         Copy( lsp_mid, st_fx->lsp_old_fx, M );                       /* Q15 */
     289             :         lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, st_fx->sr_core ); /* Q15 */
     290             :     }
     291             :     test();
     292             :     IF( EQ_16( tdm_low_rate_mode, 1 ) && GT_16( coder_type, UNVOICED ) )
     293             :     {
     294             :         // PMT("To be verified")
     295             :         IF( EQ_16( st_fx->active_cnt, 1 ) )
     296             :         {
     297             :             Copy( lsp_mid, st_fx->lsp_old_fx, M );                       /* Q15 */
     298             :             lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, st_fx->sr_core ); /* Q15 */
     299             :             Copy( lsp_new, lsp_mid, M );                                 /* Q15 */
     300             :         }
     301             : 
     302             :         /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode */
     303             :         int_lsp4_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, -2 );
     304             :     }
     305             :     ELSE
     306             :     {
     307             :         /* LSP interpolation and conversion of LSPs to A(z) */
     308             :         int_lsp4_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, st_fx->relax_prev_lsf_interp );
     309             :     }
     310             :     /*------------------------------------------------------------------*
     311             :      * Check LSF stability (distance between old LSFs and current LSFs)
     312             :      *------------------------------------------------------------------*/
     313             : 
     314             :     IF( st_fx->element_mode == EVS_MONO )
     315             :     {
     316             :         st_fx->stab_fac_fx = lsf_stab_fx( lsf_new, st_fx->lsf_old_fx, 0, st_fx->L_frame ); /*Q15*/
     317             :         move16();
     318             :     }
     319             :     ELSE
     320             :     {
     321             :         st_fx->stab_fac_fx = lsf_stab_ivas_fx( lsf_new, st_fx->lsf_old_fx, 0, st_fx->L_frame ); /* Q15 */
     322             :         move16();
     323             :     }
     324             : 
     325             :     return;
     326             : }
     327             : 
     328             : /*===========================================================================*/
     329             : /* FUNCTION : lsf_dec_ivas_fx()                                              */
     330             : /*---------------------------------------------------------------------------*/
     331             : /* PURPOSE : LSF decoder                                                     */
     332             : /*---------------------------------------------------------------------------*/
     333             : /* INPUT ARGUMENTS :                                                         */
     334             : /* _ (Struct) st_fx       : decoder static memory                            */
     335             : /* _ (Word16) L_frame     : length of the frame                              */
     336             : /* _ (Word16) coder_type  : coding type                                      */
     337             : /* _ (Word16) bwidth      : input signal bandwidth                           */
     338             : /*---------------------------------------------------------------------------*/
     339             : /* OUTPUT ARGUMENTS :                                                        */
     340             : /* _ (Word16*) Aq          : LP filter coefficient    Q12                    */
     341             : /* _ (Word16*) lsf_new      : LP filter coefficient    Q(x2.56)              */
     342             : /* _ (Word16*) lsp_new      : LP filter coefficient    Q15                   */
     343             : /* _ (Word16*) lsp_mid      : LP filter coefficient    Q15                   */
     344             : /*---------------------------------------------------------------------------*/
     345             : 
     346             : /* _ (Word16[]) st_fx->lsf_adaptive_mean_fx  : FEC - adaptive mean LSF       */
     347             : /*                        vector for FEC  Q(x2.56)                           */
     348             : /* _ (Word16[]) st_fx->mem_AR_fx : AR memory of LSF quantizer                */
     349             : /*                  (past quantized LSFs without mean) Q(x2.56)              */
     350             : /* _ (Word16) st_fx->stab_fac_fx : LSF stability factor    Q15               */
     351             : /*---------------------------------------------------------------------------*/
     352             : /* RETURN ARGUMENTS :                                                        */
     353             : /* _ None                                                                    */
     354             : /*===========================================================================*/
     355             : #endif
     356      140402 : void lsf_dec_ivas_fx(
     357             :     Decoder_State *st_fx,          /* i/o: State structure                               */
     358             :     const Word16 tc_subfr,         /* i  : TC subframe index                           Q0*/
     359             :     Word16 *Aq,                    /* o  : quantized A(z) for 4 subframes             Q12*/
     360             :     Word16 *LSF_Q_prediction,      /* o  : LSF prediction mode                         Q0*/
     361             :     Word16 *lsf_new,               /* o  : de-quantized LSF vector               Q(x2.56)*/
     362             :     Word16 *lsp_new,               /* o  : de-quantized LSP vector                    Q15*/
     363             :     Word16 *lsp_mid,               /* o  : de-quantized mid-frame LSP vector          Q15*/
     364             :     const Word16 tdm_low_rate_mode /* i  : secondary channel low rate mode flag            Q0*/
     365             :     ,
     366             :     const Word16 tdm_lsfQ_PCh[M] /* i  : Q LSFs for primary channel                             Qx*/
     367             : )
     368             : {
     369             :     Word16 i;
     370             :     Word16 no_param_lpc;
     371             :     Word16 param_lpc[NPRM_LPC_NEW];
     372             :     Word32 L_tmp;
     373      140402 :     Word16 nBits = 0;
     374      140402 :     move16();
     375             :     Word16 tmp_old[M + 1], tmp_new[M + 1];
     376      140402 :     Word16 enr_old = 0, enr_new = 0;
     377      140402 :     move16();
     378      140402 :     move16();
     379             :     Word16 lsf_diff, coder_type;
     380             : 
     381             :     /* initialize */
     382      140402 :     coder_type = st_fx->coder_type;
     383      140402 :     if ( EQ_32( st_fx->core_brate, SID_2k40 ) )
     384             :     {
     385         417 :         coder_type = INACTIVE; /* Q0 */
     386         417 :         move16();
     387             :     }
     388      140402 :     test();
     389      140402 :     if ( EQ_16( coder_type, AUDIO ) && st_fx->GSC_IVAS_mode > 0 )
     390             :     {
     391        3893 :         coder_type = GENERIC; /* Q0 */
     392        3893 :         move16();
     393             :     }
     394      140402 :     no_param_lpc = 0;
     395      140402 :     nBits = 0;
     396      140402 :     move16();
     397      140402 :     move16();
     398             : 
     399             :     /* Find the number of bits for LSF quantization */
     400      140402 :     IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
     401             :     {
     402         417 :         nBits = LSF_BITS_CNG; /* Q0 */
     403         417 :         move16();
     404             :     }
     405             :     ELSE
     406             :     {
     407      139985 :         test();
     408      139985 :         IF( st_fx->nelp_mode_dec == 0 && st_fx->ppp_mode_dec == 0 )
     409             :         {
     410      139985 :             nBits = st_fx->acelp_cfg.lsf_bits; /* Q0 */
     411      139985 :             move16();
     412             :         }
     413           0 :         ELSE IF( EQ_16( st_fx->nelp_mode_dec, 1 ) )
     414             :         {
     415           0 :             IF( EQ_16( coder_type, UNVOICED ) )
     416             :             {
     417           0 :                 nBits = 30; /* Q0 */
     418           0 :                 move16();
     419           0 :                 if ( st_fx->bwidth == NB )
     420             :                 {
     421           0 :                     nBits = 32; /* Q0 */
     422           0 :                     move16();
     423             :                 }
     424             :             }
     425             :         }
     426           0 :         ELSE IF( EQ_16( st_fx->ppp_mode_dec, 1 ) )
     427             :         {
     428           0 :             nBits = 26; /* Q0 */
     429           0 :             move16();
     430             :         }
     431             :     }
     432             : 
     433             :     /* LSF de-quantization */
     434      140402 :     lsf_end_dec_fx( st_fx, 0, coder_type, st_fx->bwidth, nBits, lsf_new, param_lpc, LSF_Q_prediction, &no_param_lpc,
     435             :                     tdm_lsfQ_PCh );
     436             : 
     437             :     /* convert quantized LSFs to LSPs */
     438             : 
     439      140402 :     lsf2lsp_fx( lsf_new, lsp_new, M, st_fx->sr_core );
     440             :     /* set seed_acelp used in UC mode */
     441      140402 :     test();
     442      140402 :     IF( EQ_16( coder_type, UNVOICED ) && GT_16( st_fx->element_mode, EVS_MONO ) )
     443             :     {
     444        1393 :         st_fx->seed_acelp = 0;
     445        1393 :         move16();
     446        1393 :         FOR( i = no_param_lpc - 1; i >= 0; i-- )
     447             :         {
     448             :             /* rightshift before *seed_acelp+param_lpc[i] to avoid overflows*/
     449           0 :             st_fx->seed_acelp = extract_l( L_add( imult3216( 31821L /* Q0 */, add( shr( ( st_fx->seed_acelp ), 1 ), param_lpc[i] ) ), 13849L /* Q0 */ ) ); /* Q0 */
     450           0 :             move16();
     451             :         }
     452             :     }
     453      140402 :     IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
     454             :     {
     455             :         /* return if SID frame (conversion to A(z) done in the calling function) */
     456         417 :         return;
     457             :     }
     458             : 
     459             :     /*-------------------------------------------------------------------------------------*
     460             :      * FEC - update adaptive LSF mean vector
     461             :      *-------------------------------------------------------------------------------------*/
     462             : 
     463     2379745 :     FOR( i = 0; i < M; i++ )
     464             :     {
     465     2239760 :         L_tmp = L_mult( lsf_new[i], 10922 );                    /*Q(x2.56+16)*/
     466     2239760 :         L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi1_fx[i], 10922 ); /*Q(x2.56+16)*/
     467     2239760 :         L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi0_fx[i], 10922 ); /*Q(x2.56+16)*/
     468     2239760 :         st_fx->lsf_adaptive_mean_fx[i] = round_fx( L_tmp );     /*Q(x2.56)*/
     469     2239760 :         move16();
     470             :     }
     471             : 
     472      139985 :     test();
     473      139985 :     test();
     474      139985 :     IF( ( st_fx->prev_bfi && ( EQ_16( coder_type, TRANSITION ) ) && ( EQ_16( tc_subfr, sub( st_fx->L_frame, L_SUBFR ) ) ) ) )
     475             :     {
     476          18 :         lsf_diff = 1205;
     477          18 :         move16(); /*int_fs / (float)(2*(M+1)); = 470.588 -> 1205 in Q2.56 */
     478          18 :         if ( EQ_16( st_fx->L_frame, L_FRAME ) )
     479             :         {
     480          11 :             lsf_diff = 964;
     481          11 :             move16(); /*int_fs / (float)(2*(M+1)); = 376.47 -> 964 in Q2.56 */
     482             :         }
     483          18 :         st_fx->lsf_old_fx[0] = lsf_diff;
     484          18 :         move16();
     485             : 
     486         288 :         FOR( i = 1; i < M; i++ )
     487             :         {
     488         270 :             st_fx->lsf_old_fx[i] = add( st_fx->lsf_old_fx[i - 1], lsf_diff ); /* Q2.56 */
     489         270 :             move16();
     490             :         }
     491          18 :         lsf2lsp_fx( st_fx->lsf_old_fx, st_fx->lsp_old_fx, M, st_fx->sr_core );
     492             :     }
     493             :     /*-------------------------------------------------------------------------------------*
     494             :      * Mid-frame LSF decoding
     495             :      * LSP interpolation and conversion of LSPs to A(z)
     496             :      *-------------------------------------------------------------------------------------*/
     497      139985 :     IF( st_fx->rate_switching_reset )
     498             :     {
     499             :         /*extrapolation in case of unstable LSF convert*/
     500           0 :         Copy( lsp_new, st_fx->lsp_old_fx, M ); /* Q15 */
     501           0 :         Copy( lsf_new, st_fx->lsf_old_fx, M ); /* Q2.56 */
     502             :     }
     503             :     {
     504             :         /* Mid-frame LSF decoding */
     505      139985 :         lsf_mid_dec_fx( st_fx, lsp_new, coder_type, lsp_mid );
     506             :     }
     507      139985 :     test();
     508      139985 :     test();
     509      139985 :     IF( !( st_fx->prev_bfi && ( EQ_16( coder_type, TRANSITION ) ) && ( EQ_16( tc_subfr, sub( st_fx->L_frame, L_SUBFR ) ) ) ) )
     510             :     {
     511      139967 :         IF( st_fx->prev_bfi )
     512             :         {
     513             :             /* check, if LSP interpolation can be relaxed */
     514        1584 :             E_LPC_f_lsp_a_conversion( st_fx->lsp_old_fx, tmp_old, M );
     515        1584 :             enr_old = Enr_1_Az_fx( tmp_old, 2 * L_SUBFR ); /* Q3 */
     516             : 
     517        1584 :             E_LPC_f_lsp_a_conversion( lsp_new, tmp_new, M );
     518        1584 :             enr_new = Enr_1_Az_fx( tmp_new, 2 * L_SUBFR ); /* Q3 */
     519             : 
     520        1584 :             IF( LT_16( enr_new, mult_r( 9830 /*0.3 Q15*/, enr_old ) ) )
     521             :             {
     522             :                 /* OLD CODE : if( st->safety_net == 1), replaced with a decision similar to MODE2 */
     523         181 :                 st_fx->relax_prev_lsf_interp = -1;
     524         181 :                 move16();
     525         181 :                 test();
     526         181 :                 test();
     527         181 :                 test();
     528         181 :                 test();
     529         181 :                 if ( st_fx->clas_dec == UNVOICED_CLAS || EQ_16( st_fx->clas_dec, SIN_ONSET ) || st_fx->clas_dec == INACTIVE_CLAS || EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, TRANSITION ) )
     530             :                 {
     531         178 :                     st_fx->relax_prev_lsf_interp = 1;
     532         178 :                     move16();
     533             :                 }
     534             :             }
     535             :         }
     536             :     }
     537      139985 :     test();
     538      139985 :     IF( EQ_16( st_fx->last_core, HQ_CORE ) && st_fx->core == ACELP_CORE )
     539             :     {
     540             :         /* update old LSPs/LSFs in case of HQ->ACELP core switching */
     541         245 :         Copy( lsp_mid, st_fx->lsp_old_fx, M );                       /* Q15 */
     542         245 :         lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, st_fx->sr_core ); /* Q15 */
     543             :     }
     544      139985 :     test();
     545      139985 :     IF( EQ_16( tdm_low_rate_mode, 1 ) && GT_16( coder_type, UNVOICED ) )
     546             :     {
     547             :         // PMT("To be verified")
     548           0 :         IF( EQ_16( st_fx->active_cnt, 1 ) )
     549             :         {
     550           0 :             Copy( lsp_mid, st_fx->lsp_old_fx, M );                       /* Q15 */
     551           0 :             lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, st_fx->sr_core ); /* Q15 */
     552           0 :             Copy( lsp_new, lsp_mid, M );                                 /* Q15 */
     553             :         }
     554             : 
     555             :         /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode */
     556           0 :         int_lsp4_ivas_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, -2 );
     557             :     }
     558             :     ELSE
     559             :     {
     560             :         /* LSP interpolation and conversion of LSPs to A(z) */
     561      139985 :         int_lsp4_ivas_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, st_fx->relax_prev_lsf_interp );
     562             :     }
     563             :     /*------------------------------------------------------------------*
     564             :      * Check LSF stability (distance between old LSFs and current LSFs)
     565             :      *------------------------------------------------------------------*/
     566             : 
     567      139985 :     IF( st_fx->element_mode == EVS_MONO )
     568             :     {
     569        1423 :         st_fx->stab_fac_fx = lsf_stab_fx( lsf_new, st_fx->lsf_old_fx, 0, st_fx->L_frame ); /*Q15*/
     570        1423 :         move16();
     571             :     }
     572             :     ELSE
     573             :     {
     574      138562 :         st_fx->stab_fac_fx = lsf_stab_ivas_fx( lsf_new, st_fx->lsf_old_fx, 0, st_fx->L_frame ); /* Q15 */
     575      138562 :         move16();
     576             :     }
     577             : 
     578      139985 :     return;
     579             : }
     580             : 
     581             : 
     582             : /*========================================================================*/
     583             : /* FUNCTION : lsf_end_dec_fx()                                            */
     584             : /*------------------------------------------------------------------------*/
     585             : /* PURPOSE : De-quantize  frame end LSF vector                            */
     586             : /*------------------------------------------------------------------------*/
     587             : /* INPUT ARGUMENTS :                                                      */
     588             : /* _ (Word16) coder_type  : coding type                                   */
     589             : /* _ (Word16) bwidth    : input signal bandwidth                          */
     590             : /* _ (Word16) nBits      : number of bits used for ISF quantization       */
     591             : /* _ (Word32*) grid       : Table of 100 grid points for evaluating       */
     592             : /*                  Chebyshev polynomials          Q31                    */
     593             : /* _ (Word16) int_fs    : sampling frequency                              */
     594             : /* _ (Word32) core_brate  : Coding Bit Rate                               */
     595             : /* _ (Word32*) p_offset_scale1    : offsets for LSF LVQ structure 1st     */
     596             : /*                    8-dim subvector          Q0                         */
     597             : /* _ (Word32*) p_offset_scale2    : offsets for LSF LVQ structure 2nd     */
     598             : /*                    8-dim subvector          Q0                         */
     599             : /* _ (Word32*) p_offset_scale1_p   : offsets for LSF LVQ structure, pred .*/
     600             : /*                    case, 1st 8-dim subvector      Q0                   */
     601             : /* _ (Word32*) p_offset_scale2_p  : offsets for LSF LVQ structure,        */
     602             : /*                    pred. case, 2nd 8-dim subvector Q0                  */
     603             : /* _ (Word16*)  p_no_scales        : LSF LVQ structure           Q0       */
     604             : /* _ (Word16*)  p_no_scales_p    : LSF LVQ structure             Q0       */
     605             : /*------------------------------------------------------------------------*/
     606             : /* INPUT/OUTPUT ARGUMENTS :                                               */
     607             : /* _ (Word16*) mem_AR  : quantizer memory for AR model        Q(x2.56)    */
     608             : /*------------------------------------------------------------------------*/
     609             : /* OUTPUT ARGUMENTS :                                                     */
     610             : /* _ (Word16*) qlsf    : quantized LSFs in the cosine domain Q(x2.56)     */
     611             : /*------------------------------------------------------------------------*/
     612             : 
     613             : /*------------------------------------------------------------------------*/
     614             : /* RETURN ARGUMENTS :                                                     */
     615             : /* _ None                                                                 */
     616             : /*========================================================================*/
     617             : 
     618      238859 : void lsf_end_dec_fx(
     619             :     Decoder_State *st,           /* i/o: decoder state structure                 */
     620             :     Word16 mode2_flag,           /* Q0 */
     621             :     const Word16 coder_type_org, /* i  : coding type                                                            Q0*/
     622             :     const Word16 bwidth,         /* i  : input signal bandwidth                                         Q0*/
     623             :     const Word16 nBits_in,       /* i  : number of bits used for ISF quantization       Q0*/
     624             :     Word16 *qlsf,                /* o  : quantized LSFs in the cosine domain            Qx2.56*/
     625             :     Word16 *lpc_param,           /* i  : LPC parameters                                                         Q0*/
     626             :     Word16 *LSF_Q_prediction,    /* o  : LSF prediction mode                                            Q0*/
     627             :     Word16 *nb_indices           /* o  : number of indices                                                      Q0*/
     628             :     ,
     629             :     const Word16 tdm_lsfQ_PCh[M] /* i  : Q LSFs for primary channel              Qx*/
     630             : )
     631             : {
     632             :     Word16 pred0[M];               /* Prediction for the safety-net quantizer (usually mean)*/
     633             :     Word16 pred1[M], pred2[M];     /* Prediction for the predictive quantizer*/
     634             :     Word16 stages0;                /* Amount of stages used by safety-net quantizer*/
     635             :     Word16 stages1;                /* Amount of stages used by predictive quantizer*/
     636             :     Word16 levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer*/
     637             :     Word16 levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer*/
     638             :     Word16 i;
     639             :     Word16 TCQIdx[M / 2 + 4];
     640             :     Word16 bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
     641             :     Word16 cumleft;
     642             :     Word16 lindice[MAX_VQ_STAGES + 3]; /* Predictor selector needs 1 bit and the LVQ indice uses 3 shorts */
     643             :     Word16 mode_lvq, mode_lvq_p;
     644             :     Word16 safety_net, predmode, stages, *levels;
     645      238859 :     const Word16 *Bit_alloc1 = NULL, *bits;
     646             :     Word16 num_bits;
     647             :     Word16 *p_lpc_param;
     648             : 
     649             :     Word16 nBits;
     650             : 
     651             :     Word16 coder_type;
     652             :     Word16 ber_flag;
     653             :     Word16 flag_1bit_gran;
     654             :     Word16 pred3[M];
     655      238859 :     flag_1bit_gran = (Word16) ( st->element_mode > EVS_MONO ); /* Q0 */
     656             : 
     657      238859 :     nBits = nBits_in; /* Q0 */
     658      238859 :     *nb_indices = 0;
     659      238859 :     move16();
     660      238859 :     move16();
     661      238859 :     move16();
     662             : 
     663      238859 :     test();
     664      238859 :     test();
     665      238859 :     IF( ( EQ_16( coder_type_org, GENERIC ) ) && ( EQ_32( st->sr_core, INT_FS_16k ) ) && ( mode2_flag == 0 ) )
     666             :     {
     667             :         /* this bit is used only for primary channel or mono */
     668       56283 :         coder_type = (Word16) get_next_indice_fx( st, 1 ); /* Q0 */
     669       56283 :         coder_type = add( coder_type, 2 );                 /* Q0 */
     670       56283 :         test();
     671       56283 :         test();
     672       56283 :         if ( EQ_16( coder_type, GENERIC ) || ( EQ_16( coder_type, VOICED ) && EQ_16( flag_1bit_gran, 1 ) ) )
     673             :         {
     674       55977 :             nBits = sub( nBits, 1 ); /* Q0 */
     675             :         }
     676             :     }
     677             :     ELSE
     678             :     {
     679      182576 :         coder_type = coder_type_org; /* Q0 */
     680      182576 :         move16();
     681             :     }
     682             : 
     683             :     /*--------------------------------------------------------------------------------*
     684             :      * LSF de-quantization of SID frames
     685             :      *--------------------------------------------------------------------------------*/
     686             : 
     687      238859 :     IF( st->core_brate == SID_2k40 )
     688             :     {
     689         417 :         dqlsf_CNG_fx( st, qlsf );
     690         417 :         sort_fx( qlsf, 0, M - 1 );
     691         417 :         reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
     692             : 
     693         417 :         return;
     694             :     }
     695             : 
     696             : 
     697      238442 :     find_pred_mode( &predmode, coder_type, bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
     698             :     /*----------------------------------------------------------------*
     699             :      * Calculate number of stages and levels for each stage based on the allowed bit allocation
     700             :      * (subtract one bit for LSF predictor selection)
     701             :      *----------------------------------------------------------------*/
     702      238442 :     lsf_allocate_fx( sub( nBits, shr( predmode, 1 ) ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1,
     703             :                      bits0, bits1 );
     704             : 
     705             : 
     706             :     /*--------------------------------------------------------------------------*
     707             :      * Select safety_net or predictive mode
     708             :      *--------------------------------------------------------------------------*/
     709      238442 :     test();
     710      238442 :     test();
     711      238442 :     IF( st->tdm_LRTD_flag == 0 && EQ_16( st->idchan, 1 ) && tdm_lsfQ_PCh != NULL )
     712             :     {
     713             :         /* if secondary channel predmode is set to be > 2  */
     714             :         /*predmode += 3;*/
     715         110 :         predmode = add( predmode, 3 ); /* Q0 */
     716             :     }
     717      238442 :     p_lpc_param = lpc_param; /* Q0 */
     718             : 
     719             : 
     720      238442 :     move16();
     721      238442 :     IF( predmode == 0 )
     722             :     {
     723       12668 :         safety_net = 1; /* Q0 */
     724       12668 :         move16();
     725             :     }
     726      225774 :     ELSE IF( predmode == 1 )
     727             :     {
     728      157688 :         safety_net = 0;
     729      157688 :         move16();
     730             :     }
     731             :     ELSE
     732             :     {
     733       68086 :         test();
     734       68086 :         test();
     735       68086 :         IF( EQ_16( mode2_flag, 1 ) || EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) )
     736             :         {
     737             :             /* read from param_lpc */
     738       32133 :             safety_net = p_lpc_param[0]; /* Q0 */
     739       32133 :             move16();
     740       32133 :             p_lpc_param++;
     741       32133 :             *nb_indices = add( *nb_indices, 1 ); /* Q0 */
     742             :         }
     743             :         ELSE
     744             :         {
     745       35953 :             safety_net = (Word16) get_next_indice_fx( st, 1 ); /* Q0 */
     746             :         }
     747             :     }
     748             : 
     749      238442 :     st->safety_net = safety_net; /* Q0 */
     750      238442 :     move16();
     751             : 
     752             :     /*--------------------------------------------------------------------------*
     753             :      * Read indices from array
     754             :      *--------------------------------------------------------------------------*/
     755      238442 :     test();
     756             :     /* Make sure there are the correct bit allocations */
     757      238442 :     IF( EQ_16( st->idchan, 1 ) && GT_16( predmode, 2 ) )
     758             :     {
     759             :         /* use same AR prediction bit allocation  for intra and inter modes*/
     760         110 :         lsf_allocate_fx( sub( nBits, 1 ), mode_lvq, 9, &stages0, &stages1, levels0, levels1, bits0, bits1 );
     761         110 :         stages0 = stages1;                 /* Q0 */
     762         110 :         Copy( levels1, levels0, stages0 ); /* Q0 */
     763         110 :         Copy( bits1, bits0, stages0 );     /* Q0 */
     764             :     }
     765             : 
     766      238442 :     test();
     767      238442 :     test();
     768      238442 :     IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( coder_type, VOICED ) && flag_1bit_gran == 0 )
     769             :     {
     770             :         /* BC-TCVQ - only in VOICED_WB@16kHz */
     771         574 :         test();
     772         574 :         test();
     773         574 :         IF( EQ_16( st->codec_mode, MODE2 ) || EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) )
     774             :         {
     775         268 :             *nb_indices = 10; /* Q0 */
     776         268 :             move16();
     777         268 :             TCQIdx[0] = safety_net; /* Q0 */
     778         268 :             move16();
     779        2680 :             FOR( i = 1; i < *nb_indices; i++ )
     780             :             {
     781        2412 :                 TCQIdx[i] = *p_lpc_param++; /* Q0 */
     782        2412 :                 move16();
     783             :             }
     784             :         }
     785             :         ELSE
     786             :         {
     787         306 :             Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1]; /* Q0 */
     788         306 :             TCQIdx[0] = safety_net;                 /* Q0 */
     789         306 :             move16();
     790        3672 :             FOR( i = 0; i < M / 2 + 3; i++ )
     791             :             {
     792        3366 :                 TCQIdx[i + 1] = get_next_indice_fx( st, Bit_alloc1[i] ); /* Q0 */
     793        3366 :                 move16();
     794             :             }
     795             :         }
     796             :     }
     797             :     ELSE
     798             :     {
     799      237868 :         IF( safety_net )
     800             :         {
     801       33487 :             stages = stages0; /* Q0 */
     802       33487 :             move16();
     803       33487 :             levels = levels0; /* Q0 */
     804       33487 :             move16();
     805       33487 :             bits = bits0; /* Q0 */
     806       33487 :             move16();
     807             :         }
     808             :         ELSE
     809             :         {
     810      204381 :             stages = stages1; /* Q0 */
     811      204381 :             move16();
     812      204381 :             levels = levels1; /* Q0 */
     813      204381 :             move16();
     814      204381 :             bits = bits1; /* Q0 */
     815      204381 :             move16();
     816             :         }
     817      237868 :         test();
     818      237868 :         test();
     819      237868 :         IF( EQ_16( mode2_flag, 1 ) || EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) )
     820             :         {
     821      175299 :             FOR( i = 0; i < sub( stages, 1 ); i++ )
     822             :             {
     823       77110 :                 num_bits = bits[i]; /* Q0 */
     824       77110 :                 move16();
     825       77110 :                 lindice[i + 1] = *p_lpc_param++; /* Q0 */
     826       77110 :                 move16();
     827             :             }
     828             : 
     829       98189 :             cumleft = levels[stages - 1]; /* Q0 */
     830       98189 :             move16();
     831      294567 :             WHILE( cumleft > 0 )
     832             :             {
     833      196378 :                 IF( GT_16( cumleft, LEN_INDICE ) )
     834             :                 {
     835       98189 :                     cumleft = sub( cumleft, LEN_INDICE ); /* Q0 */
     836             :                 }
     837             :                 ELSE
     838             :                 {
     839       98189 :                     cumleft = 0;
     840       98189 :                     move16();
     841             :                 }
     842             : 
     843      196378 :                 lindice[i + 1] = *p_lpc_param++; /* Q0 */
     844      196378 :                 move16();
     845      196378 :                 i++;
     846             :             }
     847       98189 :             *nb_indices = add( *nb_indices, i ); /* Q0 */
     848       98189 :             move16();
     849             :         }
     850             :         ELSE
     851             :         {
     852      320935 :             FOR( i = 0; i < stages - 1; i++ )
     853             :             {
     854      181256 :                 num_bits = bits[i]; /* Q0 */
     855      181256 :                 move16();
     856      181256 :                 lindice[i + 1] = (Word16) get_next_indice_fx( st, num_bits ); /* Q0 */
     857      181256 :                 move16();
     858             :             }
     859             : 
     860      139679 :             cumleft = levels[stages - 1]; /* Q0 */
     861      527159 :             WHILE( cumleft > 0 )
     862             :             {
     863      387480 :                 IF( GT_16( cumleft, LEN_INDICE ) )
     864             :                 {
     865      247801 :                     cumleft = sub( cumleft, LEN_INDICE ); /* Q0 */
     866      247801 :                     num_bits = LEN_INDICE;                /* Q0 */
     867      247801 :                     move16();
     868             :                 }
     869             :                 ELSE
     870             :                 {
     871      139679 :                     num_bits = (Word16) cumleft; /* Q0 */
     872      139679 :                     move16();
     873      139679 :                     cumleft = 0;
     874      139679 :                     move16();
     875             :                 }
     876             : 
     877      387480 :                 lindice[i + 1] = (Word16) get_next_indice_fx( st, num_bits ); /* Q0 */
     878      387480 :                 move16();
     879      387480 :                 i++;
     880             :             }
     881             :         }
     882             :     }
     883      238442 :     IF( EQ_16( st->reset_mem_AR, 1 ) )
     884             :     {
     885           0 :         FOR( i = 0; i < M; i++ )
     886             :         {
     887           0 :             st->mem_AR_fx[i] = ModeMeans_fx[mode_lvq][i]; /* Q2.56 */
     888           0 :             move16();
     889             :         }
     890           0 :         st->reset_mem_AR = 0;
     891           0 :         move16();
     892             :     }
     893             : 
     894             :     /*------------------------------------------------------------------------------------------*
     895             :      * De-quantize LSF vector
     896             :      *------------------------------------------------------------------------------------------*/
     897             : 
     898      238442 :     *LSF_Q_prediction = SAFETY_NET; /* Q0 */
     899      238442 :     move16();
     900             :     /* VOICED_WB@16kHz */
     901      238442 :     test();
     902      238442 :     test();
     903      238442 :     IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( coder_type, VOICED ) && flag_1bit_gran == 0 )
     904             :     {
     905             :         /* BC-TCVQ decoder */
     906         574 :         safety_net = qlsf_ARSN_tcvq_Dec_16k_fx( qlsf, TCQIdx, sub( nBits, 1 ) ); /* Q0 */
     907             : 
     908             :         /* Update mem_MA */
     909         574 :         Copy( qlsf, st->mem_MA_fx, M ); /* Q2.56 */
     910             : 
     911         574 :         IF( safety_net )
     912             :         {
     913          88 :             Copy( ModeMeans_fx[mode_lvq], pred0, M ); /* Q2.56 */
     914             :         }
     915             :         ELSE
     916             :         {
     917        8262 :             FOR( i = 0; i < M; i++ )
     918             :             {
     919        7776 :                 pred0[i] = add( ModeMeans_fx[mode_lvq][i], mult( Predictors_fx[mode_lvq_p][i], ( sub( st->mem_AR_fx[i], ModeMeans_fx[mode_lvq][i] ) ) ) ); /* Q(x2.56)*/
     920        7776 :                 move16();
     921             :             }
     922         486 :             *LSF_Q_prediction = AUTO_REGRESSIVE; /* Q0 */
     923         486 :             move16();
     924             :         }
     925         574 :         Vr_add( qlsf, pred0, qlsf, M );
     926             :     }
     927             :     ELSE
     928             :     {
     929             : 
     930             :         /* Safety-net */
     931      237868 :         Copy( ModeMeans_fx[mode_lvq], pred0, M ); /* Q2.56 */
     932             :         /* for mem_MA update */
     933     4043756 :         FOR( i = 0; i < M; i++ )
     934             :         {
     935     3805888 :             pred1[i] = add( pred0[i], mult_r( MU_MA_FX, st->mem_MA_fx[i] ) ); /* Q2.56 */
     936     3805888 :             move16();
     937             :         }
     938             :         /* TD stereo SCh: perform intra-frame prediction with pulling-to-mean */
     939      237868 :         IF( st->tdm_LRTD_flag == 0 && EQ_16( st->idchan, 1 ) && tdm_lsfQ_PCh != NULL )
     940             :         {
     941         110 :             tdm_SCh_LSF_intra_pred_fx( st->element_brate, tdm_lsfQ_PCh, pred3 );
     942             :         }
     943      237868 :         IF( safety_net )
     944             :         {
     945             :             /* LVQ */
     946       33487 :             test();
     947       33487 :             test();
     948       33487 :             IF( st->tdm_LRTD_flag == 0 && EQ_16( st->idchan, 1 ) && tdm_lsfQ_PCh != NULL )
     949             :             {
     950             : 
     951             :                 /* intra mode*/
     952          36 :                 st->BER_detect = s_or( st->BER_detect,
     953          36 :                                        vq_dec_lvq_ivas_fx( 0, qlsf, &lindice[1], stages0, M, 9, levels0[stages0 - 1] ) ); /* Q0 */
     954          36 :                 move16();
     955             : 
     956          36 :                 Vr_add( qlsf, pred3, qlsf, M );
     957          36 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
     958             :             }
     959             :             ELSE
     960             :             {
     961       33451 :                 IF( st->element_mode != EVS_MONO )
     962             :                 {
     963       33216 :                     ber_flag = vq_dec_lvq_ivas_fx( 1, qlsf, &lindice[1], stages0, M, mode_lvq, levels0[stages0 - 1] ); /* Q0 */
     964             :                 }
     965             :                 ELSE
     966             :                 {
     967         235 :                     ber_flag = vq_dec_lvq_fx( 1, qlsf, &lindice[1], stages0, M, mode_lvq, levels0[stages0 - 1],
     968             :                                               &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->offset_scale1_p_fx[0][0], &st->offset_scale2_p_fx[0][0],
     969             :                                               &st->no_scales_fx[0][0], &st->no_scales_p_fx[0][0] ); /* Q0 */
     970             :                 }
     971       33451 :                 st->BER_detect = s_or( st->BER_detect, ber_flag ); /* Q0 */
     972       33451 :                 move16();
     973       33451 :                 Vr_add( qlsf, pred0, qlsf, M );
     974       33451 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
     975             :             }
     976             :         }
     977             :         ELSE
     978             :         {
     979      204381 :             IF( EQ_16( predmode, 4 ) )
     980             :             {
     981          57 :                 mode_lvq_p = 9;
     982          57 :                 predmode = 2;
     983          57 :                 move16();
     984          57 :                 move16();
     985             :             }
     986      204381 :             IF( st->element_mode != EVS_MONO )
     987             :             {
     988      202519 :                 ber_flag = vq_dec_lvq_ivas_fx( 0, qlsf, &lindice[1], stages1, M, mode_lvq_p, levels1[stages1 - 1] ); /* Q0 */
     989             :             }
     990             :             ELSE
     991             :             {
     992        1862 :                 ber_flag = vq_dec_lvq_fx( 0, qlsf, &lindice[1], stages1, M, mode_lvq_p, levels1[stages1 - 1],
     993             :                                           &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->offset_scale1_p_fx[0][0], &st->offset_scale2_p_fx[0][0],
     994             :                                           &st->no_scales_fx[0][0], &st->no_scales_p_fx[0][0] ); /* Q0 */
     995             :             }
     996      204381 :             st->BER_detect = s_or( st->BER_detect, ber_flag ); /* Q0 */
     997      204381 :             move16();
     998      204381 :             test();
     999      204381 :             IF( EQ_16( predmode, 1 ) || EQ_16( predmode, 4 ) ) /* MA only */
    1000             :             {
    1001      157688 :                 Copy( qlsf, st->mem_MA_fx, M );     /* Q2.56 */
    1002      157688 :                 Vr_add( qlsf, pred1, qlsf, M );     /* Q2.56 */
    1003      157688 :                 *LSF_Q_prediction = MOVING_AVERAGE; /* Q0 */
    1004      157688 :                 move16();
    1005             :             }
    1006             :             ELSE
    1007             :             {
    1008             :                 /* AR  */
    1009      793781 :                 FOR( i = 0; i < M; i++ )
    1010             :                 {
    1011      747088 :                     pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) ); /* Q2.56 */
    1012      747088 :                     move16();
    1013             :                 }
    1014       46693 :                 Vr_add( qlsf, pred2, qlsf, M );
    1015       46693 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
    1016       46693 :                 *LSF_Q_prediction = AUTO_REGRESSIVE; /* Q0 */
    1017       46693 :                 move16();
    1018             :             }
    1019             :         }
    1020             :     }
    1021             : 
    1022             :     /*--------------------------------------------------------------------------*
    1023             :      * Sort the quantized vector
    1024             :      * Verify stability
    1025             :      * Update AR-predictor memory
    1026             :      *--------------------------------------------------------------------------*/
    1027             : 
    1028             :     /* Sort the quantized vector */
    1029      238442 :     sort_fx( qlsf, 0, M - 1 );
    1030             : 
    1031             :     /* Verify stability */
    1032      238442 :     reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
    1033             :     /* Update predictor memory */
    1034      238442 :     Copy( qlsf, st->mem_AR_fx, M ); /* Q2.56 */
    1035             : 
    1036      238442 :     st->mode_lvq = mode_lvq; /* Q0 */
    1037      238442 :     move16();
    1038             : 
    1039             : 
    1040      238442 :     return;
    1041             : }
    1042             : 
    1043             : 
    1044             : /*========================================================================*/
    1045             : /* FUNCTION : lsf_mid_dec_fx()                                            */
    1046             : /*------------------------------------------------------------------------*/
    1047             : /* PURPOSE : Decode mid-frame LSFs                                        */
    1048             : /*------------------------------------------------------------------------*/
    1049             : /* INPUT ARGUMENTS :                                                      */
    1050             : /* _ (Word16) coder_type  : Coder type                                    */
    1051             : /* _ (Word32) core_brate  : core bitrate                                  */
    1052             : /* _ (Word16[]) lsp_new    : quantized LSPs from frame beginning     Q15  */
    1053             : /* _ (Word16[]) lsp_mid    : quantized LSPs from frame end           Q15  */
    1054             : /*------------------------------------------------------------------------*/
    1055             : /* INPUT/OUTPUT ARGUMENTS :                                               */
    1056             : /*------------------------------------------------------------------------*/
    1057             : /* OUTPUT ARGUMENTS :                                                     */
    1058             : /* _ (Word16[]) qlsp    : quantized LSPs                        Q15       */
    1059             : /*------------------------------------------------------------------------*/
    1060             : 
    1061             : /*------------------------------------------------------------------------*/
    1062             : /* RETURN ARGUMENTS :                                                     */
    1063             : /* _ None                                                                 */
    1064             : /*========================================================================*/
    1065      139985 : void lsf_mid_dec_fx(
    1066             :     Decoder_State *st_fx, /* i/o: decoder state structure                          */
    1067             :     Word16 lsp_new[],     /* i  : quantized LSPs from frame endS    Q15*/
    1068             :     Word16 coder_type,    /* i  : Coder type                                             Q0*/
    1069             :     Word16 lsp_mid[]      /* o  : quantized LSPs                                        Q15*/
    1070             : )
    1071             : {
    1072             :     Word16 j, idx;
    1073             :     Word16 nb_bits;
    1074             :     Word16 qlsf0[M], qlsf1[M], qlsf[M];
    1075             :     Word32 L_tmp;
    1076             :     Word16 bad_spacing;
    1077      139985 :     const Word16 *ratio = NULL;
    1078      139985 :     bad_spacing = 0;
    1079      139985 :     move16();
    1080             : 
    1081             : 
    1082             :     /* Convert LSPs to LSFs */
    1083      139985 :     lsp2lsf_fx( st_fx->lsp_old_fx, qlsf0, M, st_fx->sr_core );
    1084      139985 :     lsp2lsf_fx( lsp_new, qlsf1, M, st_fx->sr_core );
    1085             : 
    1086             :     /* Codebook selection */
    1087      139985 :     IF( EQ_16( st_fx->ppp_mode_dec, 1 ) )
    1088             :     {
    1089           0 :         nb_bits = 1; /* Q0 */
    1090           0 :         move16();
    1091           0 :         ratio = &tbl_mid_voi_wb_1b_fx[0]; /* Q13 */
    1092             :     }
    1093      139985 :     ELSE IF( EQ_16( st_fx->nelp_mode_dec, 1 ) )
    1094             :     {
    1095           0 :         nb_bits = 4; /* Q0 */
    1096           0 :         move16();
    1097           0 :         ratio = &tbl_mid_unv_wb_4b_fx[0]; /* Q13 */
    1098             :     }
    1099             :     ELSE
    1100             :     {
    1101      139985 :         nb_bits = st_fx->acelp_cfg.mid_lsf_bits; /* Q0 */
    1102      139985 :         move16();
    1103             : 
    1104             :         /* codebook selection */
    1105             : 
    1106      139985 :         IF( EQ_16( coder_type, VOICED ) )
    1107             :         {
    1108        7412 :             SWITCH( nb_bits )
    1109             :             {
    1110        1464 :                 case 5:
    1111             :                 {
    1112        1464 :                     ratio = tbl_mid_voi_wb_5b_fx; /* Q13 */
    1113        1464 :                     BREAK;
    1114             :                 }
    1115        5948 :                 case 4:
    1116             :                 {
    1117        5948 :                     ratio = tbl_mid_voi_wb_4b_fx; /* Q13 */
    1118        5948 :                     BREAK;
    1119             :                 }
    1120           0 :                 case 1:
    1121             :                 {
    1122           0 :                     ratio = tbl_mid_voi_wb_1b_fx; /* Q13 */
    1123           0 :                     break;
    1124             :                 }
    1125             :             }
    1126        7412 :         }
    1127      132573 :         ELSE IF( EQ_16( coder_type, UNVOICED ) )
    1128             :         {
    1129        1393 :             ratio = tbl_mid_unv_wb_5b_fx; /* Q13 */
    1130             :         }
    1131             :         ELSE
    1132             :         {
    1133             :             /* GENERIC, TRANSITION, AUDIO and INACTIVE */
    1134      131180 :             SWITCH( nb_bits )
    1135             :             {
    1136      122828 :                 case 5:
    1137             :                 {
    1138      122828 :                     ratio = tbl_mid_gen_wb_5b_fx; /* Q13 */
    1139      122828 :                     BREAK;
    1140             :                 }
    1141         735 :                 case 4:
    1142             :                 {
    1143         735 :                     ratio = tbl_mid_gen_wb_4b_fx; /* Q13 */
    1144         735 :                     break;
    1145             :                 }
    1146        7617 :                 case 2:
    1147             :                 {
    1148        7617 :                     ratio = tbl_mid_gen_wb_2b_fx; /* Q13 */
    1149        7617 :                     BREAK;
    1150             :                 }
    1151             :             }
    1152      139985 :         }
    1153             :     }
    1154             : 
    1155             :     /* Retrieve mid-frame LSF index */
    1156      139985 :     idx = (Word16) get_next_indice_fx( st_fx, nb_bits ); /* Q0 */
    1157             : 
    1158             :     /* Calculation of mid-LSF vector */
    1159     2379745 :     FOR( j = 0; j < M; j++ )
    1160             :     {
    1161     2239760 :         L_tmp = L_mult( sub( 0x2000, ratio[add( i_mult( idx, M ), j )] ), qlsf0[j] ); /*Q(x2.56+13+1)->Q(x2.56+14)*/
    1162     2239760 :         L_tmp = L_mac( L_tmp, ratio[add( i_mult( idx, M ), j )], qlsf1[j] );          /*Q(x2.56+14)*/
    1163     2239760 :         qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );                                      /*Q(x2.56)*/
    1164     2239760 :         move16();
    1165             :     }
    1166             : 
    1167             :     /* check for incorrect LSF ordering */
    1168      139985 :     IF( EQ_16( st_fx->mid_lsf_int, 1 ) )
    1169             :     {
    1170      128176 :         FOR( j = 1; j < M; j++ )
    1171             :         {
    1172      120169 :             IF( LT_16( qlsf[j], qlsf[j - 1] ) )
    1173             :             {
    1174           5 :                 bad_spacing = 1; /* Q0 */
    1175           5 :                 move16();
    1176           5 :                 BREAK;
    1177             :             }
    1178             :         }
    1179             :     }
    1180             : 
    1181             :     /* Redo mid-LSF interpolation with 0.4 in case of LSF instability */
    1182      139985 :     test();
    1183      139985 :     test();
    1184      139985 :     IF( st_fx->prev_bfi || ( EQ_16( st_fx->mid_lsf_int, 1 ) && bad_spacing ) )
    1185             :     {
    1186       27319 :         FOR( j = 0; j < M; j++ )
    1187             :         {
    1188             :             /* redo mid-LSF interpolation with 0.4 */
    1189       25712 :             qlsf[j] = add( mult_r( 13107, qlsf0[j] ), mult_r( 19661, qlsf1[j] ) ); /* Q15 +x2.56 -Q15 13107 = 0.4(Q15), 19661 = 0.6 (Q15)*/
    1190       25712 :             move16();
    1191             : 
    1192             :             /* ensure correct ordering of LSF indices */
    1193       25712 :             test();
    1194       25712 :             test();
    1195       25712 :             IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
    1196             :             {
    1197          72 :                 qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX ); /* Q2.56 */
    1198          72 :                 move16();
    1199             :             }
    1200             :         }
    1201             :     }
    1202             :     ELSE
    1203             :     {
    1204             :         /* otherwise, use regular LSF spacing and ordering as in the encoder */
    1205     2352426 :         FOR( j = 0; j < M; j++ )
    1206             :         {
    1207     2214048 :             test();
    1208     2214048 :             test();
    1209     2214048 :             IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
    1210             :             {
    1211       13095 :                 qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX ); /* Q2.56 */
    1212       13095 :                 move16();
    1213             :             }
    1214             :         }
    1215             :     }
    1216             : 
    1217      139985 :     if ( st_fx->prev_bfi )
    1218             :     {
    1219             :         /* continue redoing mid-LSF interpolation with 0.4 in order not to propagate the error */
    1220        1602 :         st_fx->mid_lsf_int = 1; /* Q0 */
    1221        1602 :         move16();
    1222             :     }
    1223             : 
    1224      139985 :     if ( st_fx->safety_net )
    1225             :     {
    1226             :         /* safety-net encountered -> stop redoing mid-LSF interpolation with 0.4 */
    1227       22745 :         st_fx->mid_lsf_int = 0;
    1228       22745 :         move16();
    1229             :     }
    1230             : 
    1231      139985 :     reorder_lsf_fx( qlsf, LSF_GAP_MID_FX, M, st_fx->sr_core );
    1232             : 
    1233             :     /* convert back to LSPs */
    1234      139985 :     lsf2lsp_fx( qlsf, lsp_mid, M, st_fx->sr_core );
    1235             : 
    1236      139985 :     return;
    1237             : }

Generated by: LCOV version 1.14