LCOV - code coverage report
Current view: top level - lib_enc - lsf_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 1546 1714 90.2 %
Date: 2025-09-14 03:13:15 Functions: 23 24 95.8 %

          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 "rom_enc.h"
       8             : #include "rom_com_fx.h" /* Static table prototypes                */
       9             : #include "rom_com.h"    /* Static table prototypes                */
      10             : //#include "prot_fx.h"       /* Function prototypes                    */
      11             : #include "prot_fx.h"      /* Function prototypes                    */
      12             : #include "prot_fx_enc.h"  /* Function prototypes                    */
      13             : #include "ivas_prot_fx.h" /* Function prototypes                    */
      14             : 
      15             : 
      16             : /*-----------------------------------------------------------------*
      17             :  * Local constants
      18             :  *-----------------------------------------------------------------*/
      19             : #define MIN_LOG_FX     0
      20             : #define MIN_LOG_VAL_FX -15360 /* -60.0f in Q8 */
      21             : #define MSVQ_MAXCNT    3000
      22             : #define MAXINT32       MAX_32
      23             : 
      24             : /*---------------------------------------------------------------------*
      25             :  * Local function prototypes
      26             :  *---------------------------------------------------------------------*/
      27             : 
      28             : static void lsfq_CNG_fx( BSTR_ENC_HANDLE hBstr, const Word16 *lsf, const Word16 *wghts, Word16 *qlsf, Word32 *p_offset_scale1, Word32 *p_offset_scale2, Word16 *p_no_scales );
      29             : 
      30             : static Word32 vq_lvq_lsf_enc( Word16 pred_flag, Word16 mode, Word16 u[], Word16 *levels, Word16 stages, Word16 w[], Word16 Idx[], const Word16 *lsf, const Word16 *pred, Word32 p_offset_scale1[][MAX_NO_SCALES + 1], Word32 p_offset_scale2[][MAX_NO_SCALES + 1], Word16 p_no_scales[][2], Word16 *resq, Word16 *lsfq );
      31             : 
      32             : static Word32 vq_lvq_lsf_enc_ivas_fx( Word16 pred_flag, Word16 mode, Word16 u[], Word16 *levels, Word16 stages, Word16 w[], Word16 Idx[], const Word16 *lsf, const Word16 *pred, Word16 *resq, Word16 *lsfq );
      33             : 
      34             : static void lsf_mid_enc_fx( BSTR_ENC_HANDLE hBstr, Word16 nb_bits, const Word16 int_fs, const Word16 qlsp0[], const Word16 qlsp1[], Word16 lsp[], const Word16 coder_type, const Word16 bwidth, Word32 Bin_Ener_old[], Word32 Bin_Ener[], Word16 Q_ener, Word16 ppp_mode, Word16 nelp_mode );
      35             : 
      36             : static void lsf_mid_enc_ivas_fx( BSTR_ENC_HANDLE hBstr, Word16 nb_bits, const Word32 int_fs, const Word16 qlsp0[], const Word16 qlsp1[], Word16 lsp[], const Word16 coder_type, const Word16 bwidth, Word32 Bin_Ener_old[], Word16 Q_ener, Word16 ppp_mode, Word16 nelp_mode );
      37             : 
      38             : /*===========================================================================*/
      39             : /* FUNCTION : lsf_enc_fx()                                                   */
      40             : /*---------------------------------------------------------------------------*/
      41             : /* PURPOSE : Quantization of LSF vector                                      */
      42             : /*---------------------------------------------------------------------------*/
      43             : /* INPUT ARGUMENTS :                                                         */
      44             : /* _ (Word16) L_frame       : length of the frame                            */
      45             : /* _ (Word16) coder_type    : coding type                                    */
      46             : /*---------------------------------------------------------------------------*/
      47             : /* OUTPUT ARGUMENTS :                                                        */
      48             : /* _ (Word16*) Aq                : LP filter coefficient    Q12              */
      49             : /* _ (Word16*) lsf_ne     w      : LP filter coefficient    Q(x2.56)         */
      50             : /* _ (Word16) st_fx->stab_fac_fx : LSF stability factor     Q15              */
      51             : /*---------------------------------------------------------------------------*/
      52             : /* INPUT/OUTPUT ARGUMENTS :                                                  */
      53             : /* _ (Struct)  st_fx        : state structure                                */
      54             : /* _ (Word16*) lsp_new      : LP filter coefficient    Q15                   */
      55             : /* _ (Word16*) lsp_mid      : LP filter coefficient    Q15                   */
      56             : /* _ (Word16[]) st_fx->mem_AR_fx : AR memory of LSF quantizer                */
      57             : /*                  (past quantized LSFs without mean)    x2.56              */
      58             : /* _ (Word16[]) st_fx->clip_var_fx : pitch gain clipping memory         x2.56*/
      59             : /*---------------------------------------------------------------------------*/
      60             : 
      61             : /*---------------------------------------------------------------------------*/
      62             : /* RETURN ARGUMENTS :                                                        */
      63             : /* _ None                                                                    */
      64             : /*===========================================================================*/
      65        1423 : void lsf_enc_fx(
      66             :     Encoder_State *st_fx, /* i/o: state structure                   */
      67             :     Word16 *lsf_new,      /* o  : quantized LSF vector              */
      68             :     Word16 *lsp_new,      /* i/o: LSP vector to quantize/quantized  */
      69             :     Word16 *lsp_mid,      /* i/o  : mid-frame LSP vector            */
      70             :     Word16 *Aq,           /* o  : quantized A(z) for 4 subframes    */
      71             :     const Word16 Nb_ACELP_frames,
      72             :     const Word16 tdm_low_rate_mode, /* i  : secondary channel low rate mode flag */
      73             :     const Word16 GSC_IVAS_mode,     /* i  : GSC IVAS mode                        */
      74             :     const Word16 Q_new )
      75             : {
      76        1423 :     Word16 nBits = 0;
      77        1423 :     move16();
      78             :     Word16 int_fs;
      79        1423 :     Word16 force_sf = 0;
      80        1423 :     move16();
      81             :     Word16 fec_lsf[M], stab, i;
      82             :     Word32 L_tmp;
      83             :     Word16 coder_type, ppp_mode, nelp_mode;
      84             : 
      85        1423 :     test();
      86        1423 :     IF( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, SID_1k75 ) )
      87             :     {
      88           0 :         coder_type = INACTIVE;
      89           0 :         move16();
      90             :     }
      91             :     ELSE
      92             :     {
      93        1423 :         coder_type = st_fx->coder_type;
      94        1423 :         move16();
      95             :     }
      96             : 
      97        1423 :     test();
      98        1423 :     if ( EQ_16( coder_type, AUDIO ) && GSC_IVAS_mode > 0 )
      99             :     {
     100           0 :         coder_type = GENERIC;
     101           0 :         move16();
     102             :     }
     103             : 
     104        1423 :     move16();
     105             : 
     106        1423 :     IF( st_fx->Opt_SC_VBR )
     107             :     {
     108           0 :         ppp_mode = st_fx->hSC_VBR->ppp_mode;
     109           0 :         nelp_mode = st_fx->hSC_VBR->nelp_mode;
     110           0 :         move16();
     111           0 :         move16();
     112             :     }
     113             :     ELSE
     114             :     {
     115        1423 :         ppp_mode = 0;
     116        1423 :         nelp_mode = 0;
     117        1423 :         move16();
     118        1423 :         move16();
     119             :     }
     120             : 
     121             :     /* initialize */
     122        1423 :     assert( st_fx->sr_core <= 32000 );
     123        1423 :     int_fs = extract_l( st_fx->sr_core );
     124        1423 :     move32();
     125             :     /* convert LSPs to LSFs */
     126        1423 :     lsp2lsf_fx( lsp_new, lsf_new, M, int_fs );
     127             : 
     128             :     /* check resonance for pitch clipping algorithm */
     129        1423 :     gp_clip_test_lsf_fx( st_fx->element_mode, lsf_new, st_fx->clip_var_fx, 0 );
     130             : 
     131             :     /* Find the number of bits for LSF quantization */
     132        1423 :     nBits = 0;
     133        1423 :     move16();
     134        1423 :     IF( EQ_32( st_fx->core_brate, SID_2k40 ) )
     135             :     {
     136           0 :         nBits = LSF_BITS_CNG;
     137           0 :         move16();
     138             :     }
     139             :     ELSE
     140             :     {
     141        1423 :         test();
     142        1423 :         IF( ( nelp_mode == 0 ) && ( ppp_mode == 0 ) )
     143             :         {
     144        1423 :             nBits = st_fx->acelp_cfg.lsf_bits;
     145        1423 :             move16();
     146             :         }
     147           0 :         ELSE IF( EQ_16( nelp_mode, 1 ) )
     148             :         {
     149           0 :             nBits = 30;
     150           0 :             move16();
     151             : 
     152           0 :             if ( EQ_16( st_fx->bwidth, NB ) )
     153             :             {
     154           0 :                 nBits = 32;
     155           0 :                 move16();
     156             :             }
     157             :         }
     158           0 :         ELSE IF( EQ_16( ppp_mode, 1 ) )
     159             :         {
     160           0 :             nBits = 26;
     161           0 :             move16();
     162             :         }
     163             :     }
     164        1423 :     force_sf = 0;
     165        1423 :     move16();
     166             :     /* first three ACELP frames after an HQ frame shall be processed only with safety-net quantizer */
     167        1423 :     test();
     168        1423 :     if ( LT_16( Nb_ACELP_frames, 3 ) && NE_32( st_fx->core_brate, SID_2k40 ) )
     169             :     {
     170          65 :         force_sf = 1;
     171          65 :         move16();
     172             :     }
     173             : 
     174             :     /* in case of unstable filter in decoder FEC, choose safety-net to help FEC */
     175        1423 :     IF( EQ_16( st_fx->next_force_safety_net, 1 ) )
     176             :     {
     177         120 :         force_sf = 1;
     178         120 :         move16();
     179         120 :         st_fx->next_force_safety_net = 0;
     180         120 :         move16();
     181             :     }
     182             : 
     183             :     /*-------------------------------------------------------------------------------------*
     184             :      * Frame end LSF quantization
     185             :      *-------------------------------------------------------------------------------------*/
     186             : 
     187        1423 :     lsf_end_enc_fx( st_fx, lsf_new, lsf_new, nBits, coder_type, Q_new + QSCALE - 2,
     188        1423 :                     force_sf, NULL, NULL, NULL, st_fx->coder_type_raw );
     189             : 
     190             :     /* convert quantized LSFs back to LSPs */
     191        1423 :     lsf2lsp_fx( lsf_new, lsp_new, M, int_fs );
     192             : 
     193        1423 :     test();
     194        1423 :     IF( EQ_16( st_fx->last_core, HQ_CORE ) && ( st_fx->core == ACELP_CORE ) )
     195             :     {
     196             :         /* don't use old LSF values if this is the first ACELP frame after HQ frames */
     197          32 :         Copy( lsf_new, st_fx->lsf_old_fx, M );
     198             :     }
     199             :     /* set seed_acelp used in UC mode */
     200        1423 :     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           0 :         return;
     204             :     }
     205             : 
     206             :     /*-------------------------------------------------------------------------------------*
     207             :      * FEC - enforce safety-net in the next frame in case of unstable filter
     208             :      *-------------------------------------------------------------------------------------*/
     209             : 
     210        1423 :     IF( NE_16( st_fx->last_L_frame, st_fx->L_frame ) )
     211             :     {
     212             :         /* FEC - in case of core switching, use old LSFs */
     213           0 :         Copy( st_fx->lsf_old_fx, st_fx->lsfoldbfi1_fx, M );
     214           0 :         Copy( st_fx->lsf_old_fx, st_fx->lsfoldbfi0_fx, M );
     215           0 :         Copy( st_fx->lsf_old_fx, st_fx->lsf_adaptive_mean_fx, M );
     216             :     }
     217             : 
     218        1423 :     FEC_lsf_estim_enc_fx( st_fx, fec_lsf );
     219             : 
     220             :     /* in case of FEC in decoder - calculate LSF stability */
     221        1423 :     stab = lsf_stab_fx( lsf_new, fec_lsf, 0, st_fx->L_frame ); // Q15
     222             : 
     223        1423 :     test();
     224        1423 :     test();
     225        1423 :     test();
     226             :     /* If decoder FEC frame may be unstable force safety-net usage */
     227        1423 :     IF( ( EQ_16( st_fx->L_frame, L_FRAME16k ) ) && ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( coder_type, GENERIC ) ) )
     228             :     {
     229          90 :         st_fx->next_force_safety_net = 1;
     230          90 :         move16();
     231             :     }
     232        1333 :     ELSE IF( ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( st_fx->clas, VOICED_CLAS ) || ( LT_16( st_fx->clas, VOICED_CLAS ) && EQ_16( coder_type, AUDIO ) ) ) )
     233             :     {
     234          31 :         st_fx->next_force_safety_net = 1;
     235          31 :         move16();
     236             :     }
     237             : 
     238             : 
     239             :     /* FEC - update adaptive LSF mean vector */
     240       24191 :     FOR( i = 0; i < M; i++ )
     241             :     {
     242       22768 :         L_tmp = L_mult( lsf_new[i], 10922 );                    /*Q(x2.56+16)*/
     243       22768 :         L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi1_fx[i], 10922 ); /*Q(x2.56+16)*/
     244       22768 :         L_tmp = L_mac( L_tmp, st_fx->lsfoldbfi0_fx[i], 10922 ); /*Q(x2.56+16)*/
     245       22768 :         st_fx->lsf_adaptive_mean_fx[i] = round_fx( L_tmp );     /*Q(x2.56)*/
     246             :     }
     247             : 
     248             :     /* FEC - update LSF memories */
     249        1423 :     Copy( st_fx->lsfoldbfi0_fx, st_fx->lsfoldbfi1_fx, M );
     250        1423 :     Copy( lsf_new, st_fx->lsfoldbfi0_fx, M );
     251             : 
     252             : 
     253             :     /*-------------------------------------------------------------------------------------*
     254             :      * Mid-frame LSF encoding
     255             :      * LSP interpolation and conversion of LSPs to A(z)
     256             :      *-------------------------------------------------------------------------------------*/
     257        1423 :     if ( st_fx->rate_switching_reset )
     258             :     {
     259             :         /*extrapolation in case of unstable LSF convert*/
     260           0 :         Copy( lsp_new, st_fx->lsp_old_fx, M ); // Q15
     261           0 :         Copy( lsf_new, st_fx->lsf_old_fx, M ); // Q15
     262             :     }
     263             :     /* Mid-frame LSF encoding */
     264        1423 :     lsf_mid_enc_fx( st_fx->hBstr, st_fx->acelp_cfg.mid_lsf_bits, int_fs, st_fx->lsp_old_fx, lsp_new, lsp_mid, coder_type, st_fx->bwidth, st_fx->Bin_E_old_fx, st_fx->Bin_E_fx, Q_new + QSCALE - 2, ppp_mode, nelp_mode );
     265             : 
     266        1423 :     test();
     267        1423 :     IF( EQ_16( st_fx->last_core, HQ_CORE ) && EQ_16( st_fx->core, ACELP_CORE ) )
     268             :     {
     269             :         /* don't use old LSP/LSF values if this is the first ACELP frame after HQ frames */
     270          32 :         Copy( lsp_mid, st_fx->lsp_old_fx, M ); // Q15
     271          32 :         lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, int_fs );
     272             :     }
     273             : 
     274             :     /* LSP interpolation and conversion of LSPs to A(z) */
     275        1423 :     test();
     276        1423 :     IF( EQ_16( tdm_low_rate_mode, 1 ) && GT_16( coder_type, UNVOICED ) )
     277             :     {
     278           0 :         IF( EQ_16( st_fx->active_cnt, 1 ) )
     279             :         {
     280           0 :             Copy( lsp_mid, st_fx->lsp_old_fx, M );
     281           0 :             lsp2lsf_fx( lsp_mid, st_fx->lsf_old_fx, M, int_fs );
     282           0 :             Copy( lsp_new, lsp_mid, M );
     283             :         }
     284             : 
     285             :         /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode  */
     286           0 :         int_lsp4_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, -2 );
     287             :     }
     288             :     ELSE
     289             :     {
     290        1423 :         int_lsp4_fx( st_fx->L_frame, st_fx->lsp_old_fx, lsp_mid, lsp_new, Aq, M, 0 );
     291             :     }
     292             : 
     293             :     /*------------------------------------------------------------------*
     294             :      * Check LSF stability (distance between old LSFs and current LSFs)
     295             :      *------------------------------------------------------------------*/
     296             : 
     297        1423 :     IF( NE_32( st_fx->core_brate, SID_2k40 ) )
     298             :     {
     299        1423 :         st_fx->stab_fac_fx = lsf_stab_fx( lsf_new, st_fx->lsf_old_fx, 0, st_fx->L_frame );
     300             :     }
     301             : 
     302        1423 :     return;
     303             : }
     304             : 
     305      157047 : void lsf_enc_ivas_fx(
     306             :     Encoder_State *st,              /* i/o: state structure                   */
     307             :     Word16 *lsf_new,                /* o  : quantized LSF vector              Q(x2.56)*/
     308             :     Word16 *lsp_new,                /* i/o: LSP vector to quantize/quantized  Q15*/
     309             :     Word16 *lsp_mid,                /* i/o  : mid-frame LSP vector            Q15*/
     310             :     Word16 *Aq,                     /* o  : quantized A(z) for 4 subframes    Q12*/
     311             :     const Word16 tdm_low_rate_mode, /* i  : secondary channel low rate mode flag */
     312             :     const Word16 GSC_IVAS_mode,     /* i  : GSC IVAS mode                        */
     313             :     const Word16 tdm_lsfQ_PCh[M],   /* i  : Q LSFs for primary channel          */
     314             :     const Word16 Q_new )
     315             : {
     316             :     Word16 nBits;
     317             :     Word16 force_sf;
     318             :     Word16 fec_lsf[M], stab, i;
     319             :     Word16 no_param_lpc;
     320             : 
     321             :     Word16 param_lpc[NPRM_LPC_NEW];
     322             :     Word32 L_tmp;
     323             :     Word16 coder_type, ppp_mode, nelp_mode;
     324             : 
     325      157047 :     nBits = 0;
     326      157047 :     force_sf = 0;
     327      157047 :     move16();
     328      157047 :     move16();
     329             : 
     330      157047 :     test();
     331      157047 :     IF( EQ_32( st->core_brate, SID_2k40 ) || EQ_32( st->core_brate, SID_1k75 ) )
     332             :     {
     333         447 :         coder_type = INACTIVE;
     334         447 :         move16();
     335             :     }
     336             :     ELSE
     337             :     {
     338      156600 :         coder_type = st->coder_type;
     339      156600 :         move16();
     340             :     }
     341             : 
     342      157047 :     test();
     343      157047 :     if ( EQ_16( coder_type, AUDIO ) && GSC_IVAS_mode > 0 )
     344             :     {
     345        4685 :         coder_type = GENERIC;
     346        4685 :         move16();
     347             :     }
     348             : 
     349      157047 :     no_param_lpc = 0;
     350      157047 :     move16();
     351             : 
     352      157047 :     IF( st->Opt_SC_VBR )
     353             :     {
     354           0 :         ppp_mode = st->hSC_VBR->ppp_mode;
     355           0 :         nelp_mode = st->hSC_VBR->nelp_mode;
     356           0 :         move16();
     357           0 :         move16();
     358             :     }
     359             :     ELSE
     360             :     {
     361      157047 :         ppp_mode = 0;
     362      157047 :         nelp_mode = 0;
     363      157047 :         move16();
     364      157047 :         move16();
     365             :     }
     366             : 
     367             :     /* convert LSPs to LSFs */
     368      157047 :     lsp2lsf_fx( lsp_new, lsf_new, M, st->sr_core );
     369             : 
     370             :     /* check resonance for pitch clipping algorithm */
     371      157047 :     gp_clip_test_lsf_ivas_fx( st->element_mode, st->core_brate, lsf_new, st->clip_var_fx, 0 );
     372             : 
     373             :     /* Find the number of bits for LSF quantization */
     374      157047 :     nBits = 0;
     375      157047 :     move16();
     376      157047 :     IF( EQ_32( st->core_brate, SID_2k40 ) )
     377             :     {
     378         447 :         nBits = LSF_BITS_CNG;
     379         447 :         move16();
     380             :     }
     381             :     ELSE
     382             :     {
     383      156600 :         test();
     384      156600 :         IF( ( nelp_mode == 0 ) && ( ppp_mode == 0 ) )
     385             :         {
     386      156600 :             nBits = st->acelp_cfg.lsf_bits;
     387      156600 :             move16();
     388             :         }
     389           0 :         ELSE IF( EQ_16( nelp_mode, 1 ) )
     390             :         {
     391           0 :             IF( st->bwidth == NB )
     392             :             {
     393           0 :                 nBits = 32;
     394           0 :                 move16();
     395             :             }
     396           0 :             ELSE IF( EQ_16( st->bwidth, WB ) )
     397             :             {
     398           0 :                 nBits = 30;
     399           0 :                 move16();
     400             :             }
     401             :         }
     402           0 :         ELSE IF( EQ_16( ppp_mode, 1 ) )
     403             :         {
     404           0 :             nBits = 26;
     405           0 :             move16();
     406             :         }
     407             :     }
     408      157047 :     force_sf = 0;
     409      157047 :     move16();
     410             :     /* first three ACELP frames after an HQ frame shall be processed only with safety-net quantizer */
     411      157047 :     test();
     412      157047 :     if ( LT_16( st->Nb_ACELP_frames, 3 ) && NE_32( st->core_brate, SID_2k40 ) )
     413             :     {
     414       18698 :         force_sf = 1;
     415       18698 :         move16();
     416             :     }
     417             : 
     418             :     /* in case of unstable filter in decoder FEC, choose safety-net to help FEC */
     419      157047 :     IF( EQ_16( st->next_force_safety_net, 1 ) )
     420             :     {
     421       13998 :         force_sf = 1;
     422       13998 :         move16();
     423       13998 :         st->next_force_safety_net = 0;
     424       13998 :         move16();
     425             :     }
     426             : 
     427             :     /*-------------------------------------------------------------------------------------*
     428             :      * Frame end LSF quantization
     429             :      *-------------------------------------------------------------------------------------*/
     430      157047 :     lsf_end_enc_ivas_fx( st, lsf_new, lsf_new, nBits, coder_type, Q_new + QSCALE - 2,
     431      157047 :                          force_sf, param_lpc, &no_param_lpc, NULL, st->coder_type_raw, tdm_lsfQ_PCh );
     432             : 
     433             :     /* convert quantized LSFs back to LSPs */
     434      157047 :     lsf2lsp_fx( lsf_new, lsp_new, M, st->sr_core );
     435             : 
     436      157047 :     test();
     437      157047 :     IF( EQ_16( st->last_core, HQ_CORE ) && ( st->core == ACELP_CORE ) )
     438             :     {
     439             :         /* don't use old LSF values if this is the first ACELP frame after HQ frames */
     440         219 :         Copy( lsf_new, st->lsf_old_fx, M );
     441             :     }
     442             :     /* set seed_acelp used in UC mode */
     443             : 
     444      157047 :     test();
     445      157047 :     IF( EQ_16( coder_type, UNVOICED ) && ( st->element_mode > EVS_MONO ) )
     446             :     {
     447        2041 :         st->seed_acelp = 0;
     448        2041 :         move16();
     449        2041 :         FOR( i = no_param_lpc - 1; i >= 0; i-- )
     450             :         {
     451             :             /* rightshift before *seed_acelp+param_lpc[i] to avoid overflows*/
     452           0 :             st->seed_acelp = add( i_mult( add( shr( st->seed_acelp, 1 ), param_lpc[i] ), 31821 ), 13849 );
     453           0 :             move16();
     454             :         }
     455             :     }
     456             : 
     457      157047 :     IF( EQ_32( st->core_brate, SID_2k40 ) )
     458             :     {
     459             :         /* return if SID frame (conversion to A(z) done in the calling function) */
     460         447 :         return;
     461             :     }
     462             : 
     463             :     /*-------------------------------------------------------------------------------------*
     464             :      * FEC - enforce safety-net in the next frame in case of unstable filter
     465             :      *-------------------------------------------------------------------------------------*/
     466             : 
     467      156600 :     IF( NE_16( st->last_L_frame, st->L_frame ) )
     468             :     {
     469             :         /* FEC - in case of core switching, use old LSFs */
     470        1794 :         Copy( st->lsf_old_fx, st->lsfoldbfi1_fx, M );        // Q15
     471        1794 :         Copy( st->lsf_old_fx, st->lsfoldbfi0_fx, M );        // Q15
     472        1794 :         Copy( st->lsf_old_fx, st->lsf_adaptive_mean_fx, M ); // Q15
     473             :     }
     474             : 
     475      156600 :     FEC_lsf_estim_enc_fx( st, fec_lsf );
     476             : 
     477             :     /* in case of FEC in decoder - calculate LSF stability */
     478      156600 :     stab = lsf_stab_ivas_fx( lsf_new, fec_lsf, 0, st->L_frame ); // Q15
     479             : 
     480      156600 :     test();
     481      156600 :     test();
     482      156600 :     test();
     483             :     /* If decoder FEC frame may be unstable force safety-net usage */
     484      156600 :     IF( ( EQ_16( st->L_frame, L_FRAME16k ) ) && ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( coder_type, GENERIC ) ) )
     485             :     {
     486        9684 :         st->next_force_safety_net = 1;
     487        9684 :         move16();
     488             :     }
     489      146916 :     ELSE IF( ( LT_16( stab, STAB_FAC_LIMIT_FX ) ) && ( EQ_16( st->clas, VOICED_CLAS ) || ( LT_16( st->clas, VOICED_CLAS ) && EQ_16( coder_type, AUDIO ) ) ) )
     490             :     {
     491        4621 :         st->next_force_safety_net = 1;
     492        4621 :         move16();
     493             :     }
     494             : 
     495             : 
     496             :     /* FEC - update adaptive LSF mean vector */
     497     2662200 :     FOR( i = 0; i < M; i++ )
     498             :     {
     499     2505600 :         L_tmp = L_mult( lsf_new[i], 10922 );                 /*Q(x2.56+16)*/
     500     2505600 :         L_tmp = L_mac( L_tmp, st->lsfoldbfi1_fx[i], 10922 ); /*Q(x2.56+16)*/
     501     2505600 :         L_tmp = L_mac( L_tmp, st->lsfoldbfi0_fx[i], 10922 ); /*Q(x2.56+16)*/
     502     2505600 :         st->lsf_adaptive_mean_fx[i] = extract_h( L_tmp );    /*Q(x2.56)*/
     503             :     }
     504             : 
     505             :     /* FEC - update LSF memories */
     506      156600 :     Copy( st->lsfoldbfi0_fx, st->lsfoldbfi1_fx, M );
     507      156600 :     Copy( lsf_new, st->lsfoldbfi0_fx, M );
     508             : 
     509             : 
     510             :     /*-------------------------------------------------------------------------------------*
     511             :      * Mid-frame LSF encoding
     512             :      * LSP interpolation and conversion of LSPs to A(z)
     513             :      *-------------------------------------------------------------------------------------*/
     514      156600 :     IF( st->rate_switching_reset )
     515             :     {
     516             :         /*extrapolation in case of unstable LSF convert*/
     517           0 :         Copy( lsp_new, st->lsp_old_fx, M ); // Q15
     518           0 :         Copy( lsf_new, st->lsf_old_fx, M ); // Q15
     519             :     }
     520             :     /* Mid-frame LSF encoding */
     521      156600 :     lsf_mid_enc_ivas_fx( st->hBstr, st->acelp_cfg.mid_lsf_bits, st->sr_core, st->lsp_old_fx, lsp_new, lsp_mid, coder_type, st->bwidth, st->Bin_E_old_fx, Q_new + QSCALE - 2, ppp_mode, nelp_mode );
     522             : 
     523      156600 :     test();
     524      156600 :     IF( EQ_16( st->last_core, HQ_CORE ) && EQ_16( st->core, ACELP_CORE ) )
     525             :     {
     526             :         /* don't use old LSP/LSF values if this is the first ACELP frame after HQ frames */
     527         219 :         Copy( lsp_mid, st->lsp_old_fx, M );
     528         219 :         lsp2lsf_fx( lsp_mid, st->lsf_old_fx, M, st->sr_core );
     529             :     }
     530             : 
     531             :     /* LSP interpolation and conversion of LSPs to A(z) */
     532      156600 :     test();
     533      156600 :     IF( EQ_16( tdm_low_rate_mode, 1 ) && GT_16( coder_type, UNVOICED ) )
     534             :     {
     535           5 :         IF( EQ_16( st->active_cnt, 1 ) )
     536             :         {
     537           0 :             Copy( lsp_mid, st->lsp_old_fx, M ); // Q15
     538           0 :             lsp2lsf_fx( lsp_mid, st->lsf_old_fx, M, st->sr_core );
     539           0 :             Copy( lsp_new, lsp_mid, M ); // Q15
     540             :         }
     541             : 
     542             :         /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode  */
     543           5 :         int_lsp4_ivas_fx( st->L_frame, st->lsp_old_fx, lsp_mid, lsp_new, Aq, M, -2 );
     544             :     }
     545             :     ELSE
     546             :     {
     547      156595 :         int_lsp4_ivas_fx( st->L_frame, st->lsp_old_fx, lsp_mid, lsp_new, Aq, M, 0 );
     548             :     }
     549             :     /*------------------------------------------------------------------*
     550             :      * Check LSF stability (distance between old LSFs and current LSFs)
     551             :      *------------------------------------------------------------------*/
     552      156600 :     IF( NE_32( st->core_brate, SID_2k40 ) )
     553             :     {
     554      156600 :         st->stab_fac_fx = lsf_stab_ivas_fx( lsf_new, st->lsf_old_fx, 0, st->L_frame ); // Q15
     555             :     }
     556      156600 :     return;
     557             : }
     558             : /*-------------------------------------------------------------------*
     559             :  * lsfq_CNG_fx()
     560             :  *
     561             :  * LSF quantizer for SID frames (uses 29 bits, 4 for VQ, 25 for LVQ)
     562             :  * Note:
     563             :  * The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
     564             :  * coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
     565             :  * the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
     566             :  * either NB or WB LSF data.
     567             :  *-------------------------------------------------------------------*/
     568             : 
     569           0 : static void lsfq_CNG_fx(
     570             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle    */
     571             :     const Word16 *lsf,     /*x2.56  unquantized LSF vector */
     572             :     const Word16 *wghts,   /*Q10    LSF weights            */
     573             :     Word16 *qlsf,          /*x2.56  quantized LSF vecotor  */
     574             :     Word32 *p_offset_scale1,
     575             :     Word32 *p_offset_scale2,
     576             :     Word16 *p_no_scales )
     577             : {
     578             :     Word16 i, j, idx_cv, idx_lvq[3];
     579             :     Word32 min_dist, dist;
     580             :     Word16 dd[M], ddq[M];
     581             :     const Word16 *p_cb;
     582             :     Word16 first_cb, last_cb;
     583             :     Word16 idx_lead_cng[2], idx_scale_cng[2];
     584             :     Word16 tmp;
     585             : 
     586           0 :     idx_cv = 0;
     587           0 :     move16();
     588             : 
     589             :     /* quantize first stage with 4 bits
     590             :        The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
     591             :        coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
     592             :        the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
     593             :        either NB or WB LSF data.  */
     594           0 :     IF( GT_16( lsf[M - 1], WB_LIMIT_LSF_FX ) ) /* 16kHz sampled LSF vector*/
     595             :     {
     596           0 :         p_cb = &CNG_SN1_fx[0];
     597           0 :         move16();
     598           0 :         first_cb = 0;
     599           0 :         move16();
     600           0 :         last_cb = 6;
     601           0 :         move16();
     602             :     }
     603             :     ELSE /* 12.8kHz sampled LSF vector*/
     604             :     {
     605           0 :         p_cb = &CNG_SN1_fx[6 * M];
     606           0 :         move16();
     607           0 :         first_cb = 6;
     608           0 :         move16();
     609           0 :         last_cb = M;
     610           0 :         move16();
     611             :     }
     612             : 
     613             : 
     614           0 :     min_dist = L_add( MAXINT32, 0 );
     615           0 :     FOR( i = first_cb; i < last_cb; i++ )
     616             :     {
     617           0 :         tmp = sub( *p_cb, shl( lsf[0], 1 ) );                 /*x2.56 */
     618           0 :         dist = Mult_32_16( L_mult0( wghts[0], *p_cb ), tmp ); /*Q8 + x2.56 -Q15 + x2.56 = Q-7 + x2.56+x.256 */
     619           0 :         p_cb++;
     620           0 :         FOR( j = 1; j < M; j++ )
     621             :         {
     622           0 :             tmp = sub( *p_cb, lsf[j] );
     623           0 :             tmp = sub( tmp, lsf[j] );
     624             : 
     625           0 :             dist = L_add( dist, Mult_32_16( L_mult0( wghts[j], *p_cb ), tmp ) );
     626           0 :             p_cb++;
     627             :         }
     628           0 :         IF( LT_32( dist, min_dist ) )
     629             :         {
     630           0 :             min_dist = dist;
     631           0 :             move16(); /*Q-4 */
     632           0 :             idx_cv = i;
     633           0 :             move16();
     634             :         }
     635             :     }
     636             : 
     637             :     /* calculate difference */
     638           0 :     FOR( i = 0; i < M; i++ )
     639             :     {
     640           0 :         dd[i] = sub( lsf[i], CNG_SN1_fx[idx_cv * M + i] ); /*x2.56 */
     641           0 :         move16();
     642             :     }
     643             : 
     644             :     /* quantize the difference with LVQ */
     645             :     /* MSVQ_ROM to be updated */
     646           0 :     mslvq_cng_fx( idx_cv, dd, qlsf, ddq, idx_lead_cng, idx_scale_cng, wghts, p_no_scales );
     647             : 
     648           0 :     index_lvq_fx( ddq, idx_lead_cng, idx_scale_cng, START_CNG + idx_cv, idx_lvq,
     649             :                   p_offset_scale1, p_offset_scale2, p_no_scales );
     650           0 :     Vr_add( qlsf, &CNG_SN1_fx[idx_cv * M], qlsf, M );
     651             : 
     652             :     /* write the VQ index to the bitstream */
     653           0 :     push_indice( hBstr, IND_ISF_0_0, idx_cv, 4 );
     654             : 
     655             :     /* write the LVQ index to the bitstream */
     656           0 :     push_indice( hBstr, IND_ISF_0_1, idx_lvq[0], LEN_INDICE );
     657           0 :     push_indice( hBstr, IND_ISF_0_1, idx_lvq[1], LSF_BITS_CNG - 4 - LEN_INDICE );
     658             : 
     659           0 :     return;
     660             : }
     661             : 
     662         447 : static void lsfq_CNG_ivas_fx(
     663             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle    */
     664             :     const Word16 *lsf,     /*x2.56  unquantized LSF vector */
     665             :     const Word16 *wghts,   /*Q10    LSF weights            */
     666             :     Word16 *qlsf           /*x2.56  quantized LSF vecotor  */
     667             : )
     668             : {
     669             :     Word16 i, j, idx_cv, idx_lvq[3];
     670             :     Word32 min_dist, dist;
     671             :     Word16 dd[M], ddq[M];
     672             :     const Word16 *p_cb;
     673             :     Word16 first_cb, last_cb;
     674             :     Word16 idx_lead_cng[2], idx_scale_cng[2];
     675             :     Word16 tmp;
     676             : 
     677         447 :     idx_cv = 0;
     678         447 :     move16();
     679             : 
     680             :     /* quantize first stage with 4 bits
     681             :        The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
     682             :        coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
     683             :        the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
     684             :        either NB or WB LSF data.  */
     685         447 :     IF( GT_16( lsf[M - 1], WB_LIMIT_LSF_FX ) ) /* 16kHz sampled LSF vector*/
     686             :     {
     687         277 :         p_cb = &CNG_SN1_fx[0];
     688         277 :         move16();
     689         277 :         first_cb = 0;
     690         277 :         move16();
     691         277 :         last_cb = 6;
     692         277 :         move16();
     693             :     }
     694             :     ELSE /* 12.8kHz sampled LSF vector*/
     695             :     {
     696         170 :         p_cb = &CNG_SN1_fx[6 * M];
     697         170 :         move16();
     698         170 :         first_cb = 6;
     699         170 :         move16();
     700         170 :         last_cb = M;
     701         170 :         move16();
     702             :     }
     703             : 
     704             : 
     705         447 :     min_dist = L_add( MAXINT32, 0 );
     706        3809 :     FOR( i = first_cb; i < last_cb; i++ )
     707             :     {
     708        3362 :         tmp = sub( *p_cb, shl( lsf[0], 1 ) );                 /*x2.56 */
     709        3362 :         dist = Mult_32_16( L_mult0( wghts[0], *p_cb ), tmp ); /*Q8 + x2.56 -Q15 + x2.56 = Q-7 + x2.56+x.256 */
     710        3362 :         p_cb++;
     711       53792 :         FOR( j = 1; j < M; j++ )
     712             :         {
     713       50430 :             tmp = sub( *p_cb, lsf[j] );
     714       50430 :             tmp = sub( tmp, lsf[j] );
     715             : 
     716       50430 :             dist = L_add( dist, Mult_32_16( L_mult0( wghts[j], *p_cb ), tmp ) );
     717       50430 :             p_cb++;
     718             :         }
     719        3362 :         IF( LT_32( dist, min_dist ) )
     720             :         {
     721        1177 :             min_dist = dist;
     722        1177 :             move16(); /*Q-4 */
     723        1177 :             idx_cv = i;
     724        1177 :             move16();
     725             :         }
     726             :     }
     727             : 
     728             :     /* calculate difference */
     729        7599 :     FOR( i = 0; i < M; i++ )
     730             :     {
     731        7152 :         dd[i] = sub( lsf[i], CNG_SN1_fx[idx_cv * M + i] ); /*x2.56 */
     732        7152 :         move16();
     733             :     }
     734             : 
     735             :     /* quantize the difference with LVQ */
     736             :     /* MSVQ_ROM to be updated */
     737         447 :     mslvq_cng_ivas_fx( idx_cv, dd, qlsf, ddq, idx_lead_cng, idx_scale_cng, wghts );
     738             : 
     739         447 :     index_lvq_ivas_fx( ddq, idx_lead_cng, idx_scale_cng, START_CNG_IVAS + idx_cv, idx_lvq, 0 );
     740         447 :     Vr_add( qlsf, &CNG_SN1_fx[idx_cv * M], qlsf, M );
     741             : 
     742             :     /* write the VQ index to the bitstream */
     743         447 :     push_indice( hBstr, IND_ISF_0_0, idx_cv, 4 );
     744             : 
     745             :     /* write the LVQ index to the bitstream */
     746         447 :     push_indice( hBstr, IND_ISF_0_1, idx_lvq[0], LEN_INDICE );
     747         447 :     push_indice( hBstr, IND_ISF_0_1, idx_lvq[1], LSF_BITS_CNG - 4 - LEN_INDICE );
     748             : 
     749         447 :     return;
     750             : }
     751             : /*-------------------------------------------------------------------*
     752             :  * qlsf_Mode_Select_fx()
     753             :  *
     754             :  * Mode selection for LSF quantizer
     755             :  *-------------------------------------------------------------------*/
     756             : 
     757             : 
     758         629 : static Word16 qlsf_Mode_Select_fx(
     759             :     const Word16 *w,          /* i : weighting vector           Q8 */
     760             :     const Word16 *pred1,      /* i : prediction vector       x2.56 */
     761             :     const Word16 streaklimit, /* i : predictive streak limit   Q15 */
     762             :     const Word32 op_loop_thr  /* i : Open-loop Threshold           */
     763             : )
     764             : {
     765             :     Word16 pred_pow2[M];
     766         629 :     Word32 temp32, En = 0;
     767             :     Word16 safety_net;
     768             :     Word16 i, cs, cl;
     769             : #ifndef ISSUE_1867_replace_overflow_libenc
     770             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     771             :     Flag Overflow = 0;
     772             : #endif
     773             : #endif
     774             : 
     775             :     /* calculate the prediction residual */
     776         629 :     cl = 0;
     777         629 :     move16();
     778       10693 :     FOR( i = 0; i < M; i++ )
     779             :     {
     780       10064 :         cl = s_max( cl, abs_s( pred1[i] ) );
     781             :     }
     782         629 :     cs = norm_s( cl );
     783         629 :     En = 0;
     784         629 :     move16();
     785       10693 :     FOR( i = 0; i < M; i++ )
     786             :     {
     787       10064 :         pred_pow2[i] = shl( pred1[i], cs );
     788       10064 :         move16();
     789             : #ifdef ISSUE_1867_replace_overflow_libenc
     790       10064 :         En = L_mac_sat( En, mult( pred_pow2[i], shl_sat( w[i], 2 ) ), pred_pow2[i] ); /* 2.56*2.56 at Q-4 */
     791             : #else
     792             :         En = L_mac_o( En, mult( pred_pow2[i], shl_sat( w[i], 2 ) ), pred_pow2[i], &Overflow ); /* 2.56*2.56 at Q-4 */
     793             : #endif
     794             :     }
     795             : 
     796         629 :     cs = shl( cs, 1 );
     797         629 :     En = L_shr( En, cs );
     798         629 :     temp32 = Mult_32_16( op_loop_thr, streaklimit );
     799             : 
     800             :     /* choose the mode */
     801         629 :     IF( GT_32( En, temp32 ) )
     802             :     {
     803             :         /* Safety-net */
     804          56 :         safety_net = 1;
     805          56 :         move16();
     806             :     }
     807             :     ELSE
     808             :     {
     809             :         /* Predictive */
     810         573 :         safety_net = 0;
     811         573 :         move16();
     812             :     }
     813         629 :     return safety_net;
     814             : }
     815             : 
     816             : 
     817             : /*========================================================================*/
     818             : /* FUNCTION : lsf_end_enc_fx()                                            */
     819             : /*------------------------------------------------------------------------*/
     820             : /* PURPOSE : Quantization of LSF parameters                               */
     821             : /*------------------------------------------------------------------------*/
     822             : /* INPUT ARGUMENTS :                                                      */
     823             : /* _ (Word16*) lsf        : LSF in the frequency domain (0..6400)   x2.56 */
     824             : /* _ (Word16) coder_type  : coding type                                   */
     825             : /* _ (Word16) bwidth      : input signal bandwidth                        */
     826             : /* _ (Word16) nBits       : number of bits used for ISF quantization      */
     827             : /* _ (Word16*) stable_isp : most recent stable ISP (can be                */
     828             : /*                          removed after passing to LSF)             Q15 */
     829             : /* _ (Word16*) stable_lsp : most recent stable LSP                    Q15 */
     830             : /* _ (Word32*) grid       : Table of 100 grid points for evaluating       */
     831             : /*                          Chebyshev polynomials                     Q31 */
     832             : /* _ (Word16) int_fs      : sampling frequency                            */
     833             : /* _ (Word32) core_brate  : Coding Bit Rate                               */
     834             : /* _ (Word16) force_sf    : Force safety-net usage if possible            */
     835             : /* _ (Word32*) Bin_Ener   : FFT Bin energy 128 *2 sets             Q_ener */
     836             : /* _ (Word16) Q_ener      : Q format of Bin_Ener                          */
     837             : /* _ (Word32*) offset_scale1: offsets for LSF LVQ structure 1st           */
     838             : /*                            8-dim subvector                          Q0 */
     839             : /* _ (Word32*) offset_scale2: offsets for LSF LVQ structure 2nd           */
     840             : /*                            8-dim subvector                          Q0 */
     841             : /* _ (Word32*) offset_scale1_p: offsets for LSF LVQ structure, pred.      */
     842             : /*                              case, 1st 8-dim subvector              Q0 */
     843             : /* _ (Word32*) offset_scale2_p: offsets for LSF LVQ structure,            */
     844             : /*                              pred. case, 2nd 8-dim subvector        Q0 */
     845             : /* _ (Word16*) no_scales      : LSF LVQ structure                      Q0 */
     846             : /* _ (Word16*) no_scales_p    : LSF LVQ structure                      Q0 */
     847             : /*------------------------------------------------------------------------*/
     848             : /* INPUT/OUTPUT ARGUMENTS :                                               */
     849             : /* _ (Word16*) mem_AR  : quantizer memory for AR model              x2.56 */
     850             : /*------------------------------------------------------------------------*/
     851             : /* OUTPUT ARGUMENTS :                                                     */
     852             : /* _ (Word16*) qlsf    : quantized LSFs in the cosine domain        x2.56 */
     853             : /*------------------------------------------------------------------------*/
     854             : 
     855             : /*------------------------------------------------------------------------*/
     856             : /* RETURN ARGUMENTS :                                                     */
     857             : /* _ None                                                                 */
     858             : /*========================================================================*/
     859        2669 : void lsf_end_enc_fx(
     860             :     Encoder_State *st,           /* i/o: encoder state structure                                */
     861             :     const Word16 *lsf,           /* i  : LSF in the frequency domain (0..6400)                  x2.56*/
     862             :     Word16 *qlsf,                /* o  : quantized LSF                                          x2.56*/
     863             :     const Word16 nBits_in,       /* i  : number of bits to spend on ISF quantization            */
     864             :     const Word16 coder_type_org, /* i  : coding type                                       */
     865             :     Word16 Q_ener,               /* i  : Q valuen for Bin_Ener                             */
     866             :     Word16 force_sf,             /* i  : Force safety-net usage if coding type supports    */
     867             :     Word16 *lpc_param,
     868             :     Word16 *no_indices,
     869             :     Word16 *bits_param_lpc,
     870             :     Word16 coder_type_raw /* i  : Coder type (LSF coder_type have some special cases)*/
     871             : )
     872             : {
     873             :     Word16 i;
     874             :     Word16 Idx0[MAX_VQ_STAGES + 3];      /* Optimal codebook indices for safety-net quantizer                 */
     875             :     Word16 Idx1[MAX_VQ_STAGES + 3];      /* Optimal codebook indices for predictive quantizer                 */
     876             :     Word16 indice[MAX_VQ_STAGES + 3];    /* Temp. array of indice for vector de-quantizer                     */
     877        2669 :     Word16 mode_lvq = 0, mode_lvq_p = 0; /* LVQ mode and predictive LVQ mode                                */
     878             :     Word16 bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
     879        2669 :     const Word16 *Bit_alloc1 = NULL;
     880             :     Word32 Err[2];                 /* Quantization error for safety-net(0) and predictive(1) quantizers */
     881             :     Word16 Tmp[M];                 /* Temporary target vector (mean and prediction removed)             */
     882             :     Word16 pred0[M];               /* Prediction for the safety-net quantizer (usually mean)            */
     883             :     Word16 pred1[M];               /* Prediction for the predictive quantizer                           */
     884             :     Word16 pred2[M];               /* Prediction for the predictive quantizer                           */
     885             :     Word16 wghts[M];               /* Weighting used for quantizer (currently GSM based)                */
     886             :     Word16 stages0;                /* Amount of stages used by safety-net quantizer                     */
     887             :     Word16 stages1;                /* Amount of stages used by predictive quantizer                     */
     888             :     Word16 levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer       */
     889             :     Word16 levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer       */
     890             :     Word16 predmode;               /* 0: safety-net only, 1: predictive only, 2: best of the two        */
     891             :     Word16 safety_net, cumleft, num_bits;
     892             :     Word16 *Idx, stages, *bits;
     893             :     Word16 Tmp2[M], Tmp1[M];
     894             :     Word32 abs_threshold; /* Absolute threshold depending on signal bandwidth, that indicates
     895             :                              very good perceptual LSF quantization performance                 */
     896             :     Word16 lsfq[M * 2], resq[M * 2];
     897             :     Word16 coder_type;     /* coder type (from LSF quantizer point of view)                     */
     898             :     Word16 nBits;          /* Number of bits                                                    */
     899             :     Word16 TCQIdx0[M + 2]; /* Optimal codebook indices for VQ-TCQ quantizer                     */
     900             :     Word16 *TCQIdx;
     901             :     Word16 tmp;
     902             :     Word16 flag_1bit_gran;
     903        2669 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     904             : 
     905        2669 :     flag_1bit_gran = (Word16) GT_16( st->element_mode, EVS_MONO );
     906             : 
     907        2669 :     nBits = nBits_in;
     908        2669 :     move16();
     909             :     /* Update LSF coder_type for LSF quantizer for some special cases */
     910        2669 :     test();
     911        2669 :     test();
     912        2669 :     test();
     913        2669 :     IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( st->codec_mode, MODE1 ) )
     914             :     {
     915         624 :         IF( EQ_16( coder_type_raw, VOICED ) )
     916             :         {
     917         359 :             coder_type = VOICED;
     918         359 :             move16(); /* Reflect Inactive mode */
     919         359 :             if ( EQ_16( flag_1bit_gran, 1 ) )
     920             :             {
     921           0 :                 nBits = sub( nBits, 1 ); /* This is for real Generic*/
     922             :             }
     923             :         }
     924             :         ELSE
     925             :         {
     926         265 :             nBits = sub( nBits, 1 ); /* This is for real Generic*/
     927         265 :             coder_type = coder_type_org;
     928         265 :             move16();
     929             :         }
     930             :     }
     931             :     ELSE
     932             :     {
     933        2045 :         coder_type = coder_type_org;
     934        2045 :         move16();
     935             :     }
     936             : 
     937             :     /*----------------------------------------------------------------------------------- -*
     938             :      * Calculate the number of stages and levels for each stage based on allowed bit budget
     939             :      * Set absolute threshold for codebook-type decision logic depending on signal bandwidth
     940             :      *------------------------------------------------------------------------------------ -*/
     941        2669 :     IF( EQ_16( st->bwidth, NB ) )
     942             :     {
     943           0 :         abs_threshold = L_add( SFNETLOWLIMIT_NB, 0 );
     944             :     }
     945             :     ELSE
     946             :     {
     947        2669 :         abs_threshold = L_add( SFNETLOWLIMIT_WB, 0 );
     948             :     }
     949             :     /* Calculate LSF weighting coefficients */
     950        2669 :     Unified_weighting_fx( &st->Bin_E_fx[L_FFT / 2], Q_ener, lsf, wghts, (Word16) EQ_16( st->bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), st->sr_core, M );
     951             : 
     952             :     /*--------------------------------------------------------------------------------*
     953             :      * LSF quantization of SID frames
     954             :      *--------------------------------------------------------------------------------*/
     955        2669 :     IF( EQ_32( st->core_brate, SID_2k40 ) )
     956             :     {
     957           0 :         lsfq_CNG_fx( hBstr, lsf, wghts, qlsf, &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->no_scales_fx[0][0] );
     958           0 :         sort_fx( qlsf, 0, M - 1 );
     959           0 :         reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
     960             : 
     961           0 :         return;
     962             :     }
     963             :     /* Find allowed predictor mode for current coder_type. (SN only (0), SN/AR switched (2) or MA predictive (1) */
     964        2669 :     find_pred_mode( &predmode, coder_type, st->bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
     965             : 
     966             :     /*----------------------------------------------------------------*
     967             :      * Calculate number of stages and levels for each stage based on the allowed bit allocation
     968             :      * (subtract one bit for LSF predictor selection)
     969             :      *----------------------------------------------------------------*/
     970        2669 :     lsf_allocate_fx( sub( nBits, shr( predmode, 1 ) ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1, bits0, bits1 );
     971             : 
     972             : 
     973             :     /*--------------------------------------------------------------------------------*
     974             :      * LSF quantization of all other frames but SID frames
     975             :      * Select safety-net or predictive mode
     976             :      *--------------------------------------------------------------------------------*/
     977             : 
     978        2669 :     Err[0] = MAXINT32;
     979        2669 :     move32();
     980        2669 :     Err[1] = MAXINT32;
     981        2669 :     move32();
     982             :     /* for mem_MA update */
     983       45373 :     FOR( i = 0; i < M; i++ )
     984             :     {
     985       42704 :         pred1[i] = add( ModeMeans_fx[mode_lvq][i], mult_r( MU_MA_FX, st->mem_MA_fx[i] ) );
     986       42704 :         move16();
     987             :     }
     988             : 
     989        2669 :     IF( predmode == 0 )
     990             :     {
     991             :         /* Subtract only mean */
     992         124 :         Copy( ModeMeans_fx[mode_lvq], pred0, M );
     993         124 :         Vr_subt( lsf, pred0, Tmp, M );
     994             : 
     995             :         /* LVQ quantization (safety-net only) */
     996         248 :         Err[0] = vq_lvq_lsf_enc( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0,
     997         124 :                                  st->offset_scale1_fx, st->offset_scale2_fx, st->no_scales_fx, resq, lsfq );
     998         124 :         safety_net = 1;
     999         124 :         move16();
    1000         124 :         st->pstreaklen = 0;
    1001         124 :         move16(); /* predictive LSF quantizer streak is ended with safety-net */
    1002             :     }
    1003        2545 :     ELSE IF( EQ_16( predmode, 1 ) ) /* only MA prediction */
    1004             :     {
    1005        1442 :         Vr_subt( lsf, pred1, Tmp1, M );
    1006        2884 :         Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp1, levels1, stages1, wghts, Idx1, lsf, pred1,
    1007        1442 :                                  st->offset_scale1_p_fx, st->offset_scale2_p_fx, st->no_scales_p_fx, resq, lsfq );
    1008             : 
    1009        1442 :         safety_net = 0;
    1010        1442 :         move16();
    1011             :     }
    1012             :     ELSE
    1013             :     {
    1014             :         /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
    1015             :             case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
    1016        1103 :         test();
    1017        1103 :         test();
    1018        1103 :         test();
    1019        1103 :         IF( ( ( GT_16( st->pstreaklen, ( STREAKLEN + 3 ) ) ) && ( EQ_16( coder_type, VOICED ) ) ) || ( ( GT_16( st->pstreaklen, ( STREAKLEN ) ) ) && ( NE_16( coder_type, VOICED ) ) ) )
    1020             :         {
    1021             :             /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
    1022         213 :             st->streaklimit_fx = mult( st->streaklimit_fx, STREAKMULT_FX ); // Q15
    1023         213 :             move16();
    1024             :         }
    1025             : 
    1026        1103 :         IF( st->pstreaklen == 0 )
    1027             :         {
    1028             :             /* reset the consecutive AR-predictor multiplier */
    1029         326 :             st->streaklimit_fx = 32767; /*1.0 in Q15 */
    1030         326 :             move16();
    1031             :         }
    1032             : 
    1033             :         /* VOICED_WB@16kHz */
    1034        1103 :         test();
    1035        1103 :         IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( coder_type, VOICED ) && flag_1bit_gran == 0 )
    1036             :         {
    1037             :             /* Subtract mean and AR prediction */
    1038         629 :             Copy( ModeMeans_fx[mode_lvq], pred0, M );
    1039             :             /* subtract only mean */
    1040         629 :             Vr_subt( lsf, pred0, Tmp, M );
    1041             : 
    1042       10693 :             FOR( i = 0; i < M; i++ )
    1043             :             {
    1044             :                 /* subtract mean and AR prediction */
    1045       10064 :                 pred2[i] = mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) );
    1046       10064 :                 Tmp2[i] = sub( Tmp[i], pred2[i] );
    1047       10064 :                 pred2[i] = add( pred2[i], pred0[i] );
    1048             :             }
    1049             : 
    1050             :             /* select safety_net or predictive */
    1051         629 :             safety_net = qlsf_Mode_Select_fx( wghts, Tmp2, st->streaklimit_fx, OP_LOOP_THR_HVO );
    1052         629 :             IF( EQ_16( force_sf, 1 ) )
    1053             :             {
    1054          57 :                 safety_net = 1;
    1055          57 :                 move16();
    1056             :             }
    1057             : 
    1058         629 :             IF( safety_net )
    1059             :             {
    1060             :                 /* Safety-net - BC-TCQ quantization : SN */
    1061         104 :                 Err[0] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
    1062         104 :                 st->pstreaklen = 0;
    1063         104 :                 move16();
    1064             :             }
    1065             :             ELSE
    1066             :             {
    1067             :                 /* predictive - BC-TCQ quantization : AR */
    1068         525 :                 Err[1] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp2, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
    1069         525 :                 st->pstreaklen = add( st->pstreaklen, 1 );
    1070             :             }
    1071             :         }
    1072             :         /* all other frames (not VOICED@16kHz) */
    1073             :         ELSE
    1074             :         {
    1075             :             /* Subtract mean and AR prediction */
    1076         474 :             Copy( ModeMeans_fx[mode_lvq], pred0, M );
    1077             :             /* subtract only mean */
    1078         474 :             Vr_subt( lsf, pred0, Tmp, M );
    1079             : 
    1080        8058 :             FOR( i = 0; i < M; i++ )
    1081             :             {
    1082             :                 /* subtract mean and AR prediction */
    1083        7584 :                 pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) );
    1084        7584 :                 Tmp2[i] = sub( lsf[i], pred2[i] );
    1085             :             }
    1086             : 
    1087             :             /* safety-net */
    1088         474 :             Err[0] = vq_lvq_lsf_enc( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, st->offset_scale1_fx, st->offset_scale2_fx, st->no_scales_fx, resq, lsfq );
    1089             :             /* Predictive quantizer is calculated only if it can be selected */
    1090         474 :             test();
    1091         474 :             IF( !force_sf || GT_32( Err[0], abs_threshold ) )
    1092             :             {
    1093         468 :                 Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp2, levels1, stages1, wghts, Idx1, lsf, pred2,
    1094         468 :                                          st->offset_scale1_p_fx, st->offset_scale2_p_fx, st->no_scales_p_fx, &resq[M], &lsfq[M] );
    1095             :             }
    1096         474 :             test();
    1097         474 :             test();
    1098             :             /* Select whether to use safety-net (non-predictive) or predictive LSF quantizer. The decision is based on following:
    1099             :                 if the non-predictive (safety-net) quantization error (Err[0]) is low enough (spectral distortion is low) it is selected
    1100             :                 or if the predictively quantized error (Err[1]) is by at least adaptive margin smaller than non-predictive quantizer.
    1101             :                 or if the in case of frame erasure the resulting concealed predictive LSF would be unstable safety-net is selected */
    1102         474 :             IF( force_sf || LT_32( Mult_32_16( Err[0], ( st->streaklimit_fx ) ), L_add( Err[1], Mult_32_16( Err[1], PREFERSFNET_FX ) ) ) || LT_32( Err[0], abs_threshold ) )
    1103             :             {
    1104         166 :                 safety_net = 1;
    1105         166 :                 move16();
    1106         166 :                 st->pstreaklen = 0;
    1107         166 :                 move16(); /* Reset the consecutive predictive frame counter */
    1108             :             }
    1109             :             ELSE
    1110             :             {
    1111         308 :                 safety_net = 0;
    1112         308 :                 move16(); /* Increase the consecutive predictive frame counter by one */
    1113         308 :                 st->pstreaklen = add( st->pstreaklen, 1 );
    1114             :             }
    1115             :         }
    1116             :     }
    1117             : 
    1118             :     /*--------------------------------------------------------------------------* \
    1119             :      * Write indices to array                                                     \
    1120             :      *--------------------------------------------------------------------------*/
    1121             : 
    1122        2669 :     IF( EQ_16( st->codec_mode, MODE1 ) && EQ_16( st->core, ACELP_CORE ) )
    1123             :     {
    1124             :         /* write coder_type bit for VOICED@16kHz or GENERIC@16kHz */
    1125        1423 :         test();
    1126        1423 :         IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) )
    1127             :         {
    1128             :             /* VOICED =2 and GENERIC=3, so "coder_type-2" means VOICED =0 and GENERIC=1*/
    1129         624 :             push_indice( hBstr, IND_LSF_PREDICTOR_SELECT_BIT, sub( coder_type, 2 ), 1 );
    1130             :         }
    1131             : 
    1132             :         /* write predictor selection bit */
    1133        1423 :         IF( EQ_16( predmode, 2 ) )
    1134             :         {
    1135         571 :             push_indice( st->hBstr, IND_LSF_PREDICTOR_SELECT_BIT, safety_net, 1 );
    1136             :         }
    1137             : 
    1138        1423 :         test();
    1139        1423 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1140             :         {
    1141             :             /* BC-TCVQ (only for VOICED@16kHz) */
    1142         359 :             TCQIdx = &TCQIdx0[1];
    1143         359 :             Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
    1144        4308 :             FOR( i = 0; i < ( M / 2 ) + 3; i++ )
    1145             :             {
    1146        3949 :                 push_indice( hBstr, IND_LSF, TCQIdx[i], Bit_alloc1[i] );
    1147             :             }
    1148             :         }
    1149             :         ELSE
    1150             :         {
    1151        1064 :             cumleft = nBits;
    1152        1064 :             move16();
    1153        1064 :             IF( EQ_16( predmode, 2 ) )
    1154             :             {
    1155             :                 /* subtract predictor selection bit */
    1156         212 :                 cumleft = sub( nBits, 1 );
    1157             :             }
    1158             : 
    1159        1064 :             IF( safety_net )
    1160             :             {
    1161         205 :                 stages = stages0;
    1162         205 :                 move16();
    1163         205 :                 Idx = Idx0;
    1164         205 :                 move16();
    1165         205 :                 bits = bits0;
    1166         205 :                 move16();
    1167             :             }
    1168             :             ELSE
    1169             :             {
    1170         859 :                 stages = stages1;
    1171         859 :                 move16();
    1172         859 :                 Idx = Idx1;
    1173         859 :                 move16();
    1174         859 :                 bits = bits1;
    1175         859 :                 move16();
    1176             :             }
    1177             : 
    1178        1064 :             tmp = sub( stages, 1 );
    1179        2464 :             FOR( i = 0; i < tmp; i++ )
    1180             :             {
    1181        1400 :                 indice[i] = Idx[i];
    1182        1400 :                 move16();
    1183        1400 :                 num_bits = bits[i];
    1184        1400 :                 move16();
    1185        1400 :                 cumleft -= num_bits;
    1186        1400 :                 move16();
    1187        1400 :                 push_indice( hBstr, IND_LSF, indice[i], num_bits );
    1188             :             }
    1189             : 
    1190        4036 :             WHILE( cumleft > 0 )
    1191             :             {
    1192        2972 :                 indice[i] = Idx[i];
    1193        2972 :                 move16();
    1194             : 
    1195        2972 :                 IF( GT_16( cumleft, LEN_INDICE ) )
    1196             :                 {
    1197        1908 :                     num_bits = LEN_INDICE;
    1198        1908 :                     move16();
    1199             :                 }
    1200             :                 ELSE
    1201             :                 {
    1202        1064 :                     num_bits = cumleft;
    1203        1064 :                     move16();
    1204             :                 }
    1205             : 
    1206        2972 :                 cumleft = sub( cumleft, num_bits );
    1207        2972 :                 push_indice( hBstr, IND_LSF, indice[i], num_bits );
    1208        2972 :                 i = add( i, 1 );
    1209             :             }
    1210             :         }
    1211             :     }
    1212             :     ELSE
    1213             :     {
    1214        1246 :         test();
    1215        1246 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) )
    1216             :         {
    1217             :             /* BC-TCVQ (only for VOICED@16kHz) */
    1218             :             /* Number of quantization indices */
    1219         270 :             *no_indices = 10;
    1220         270 :             move16();
    1221        2970 :             FOR( i = 0; i < *no_indices; i++ )
    1222             :             {
    1223        2700 :                 lpc_param[i] = TCQIdx0[i];
    1224        2700 :                 move16();
    1225        2700 :                 bits_param_lpc[i] = BC_TCVQ_BIT_ALLOC_40B[i]; // Q0
    1226        2700 :                 move16();
    1227             :             }
    1228             :         }
    1229             :         ELSE
    1230             :         {
    1231             :             /* Number of quantization indices */
    1232             : 
    1233             :             /* there are 31 bits */
    1234         976 :             IF( EQ_16( safety_net, 1 ) )
    1235             :             {
    1236          85 :                 Idx = Idx0;
    1237          85 :                 move16();
    1238          85 :                 *no_indices = add( stages0, 1 );
    1239         255 :                 FOR( i = 0; i < stages0; i++ )
    1240             :                 {
    1241         170 :                     lpc_param[i] = Idx[i];
    1242         170 :                     move16();
    1243         170 :                     indice[i] = Idx[i];
    1244         170 :                     move16();
    1245         170 :                     bits_param_lpc[i] = bits0[i];
    1246         170 :                     move16();
    1247             :                 }
    1248          85 :                 lpc_param[stages0] = Idx[stages0];
    1249          85 :                 move16();
    1250          85 :                 indice[stages0] = Idx[stages0];
    1251          85 :                 move16();
    1252          85 :                 tmp = sub( stages0, 1 );
    1253          85 :                 bits_param_lpc[tmp] = LEN_INDICE;
    1254          85 :                 move16();
    1255          85 :                 bits_param_lpc[stages0] = sub( bits0[tmp], LEN_INDICE );
    1256             :             }
    1257             :             ELSE
    1258             :             {
    1259         891 :                 *no_indices = add( stages1, 1 );
    1260         891 :                 Idx = Idx1;
    1261         891 :                 move16();
    1262        2496 :                 FOR( i = 0; i < stages1; i++ )
    1263             :                 {
    1264        1605 :                     lpc_param[i] = ( Idx[i] );
    1265        1605 :                     move16();
    1266        1605 :                     indice[i] = Idx[i];
    1267        1605 :                     move16();
    1268        1605 :                     bits_param_lpc[i] = bits1[i];
    1269        1605 :                     move16();
    1270             :                 }
    1271         891 :                 lpc_param[stages1] = ( Idx[stages1] );
    1272         891 :                 move16();
    1273         891 :                 indice[stages1] = Idx[stages1];
    1274         891 :                 move16();
    1275         891 :                 tmp = sub( stages1, 1 );
    1276         891 :                 bits_param_lpc[tmp] = LEN_INDICE;
    1277         891 :                 move16();
    1278         891 :                 bits_param_lpc[stages1] = sub( bits1[tmp], LEN_INDICE );
    1279             :             }
    1280         976 :             IF( EQ_16( predmode, 2 ) )
    1281             :             {
    1282         871 :                 FOR( i = *no_indices; i > 0; i-- )
    1283             :                 {
    1284         609 :                     tmp = sub( i, 1 );
    1285         609 :                     lpc_param[i] = lpc_param[tmp];
    1286         609 :                     move16();
    1287         609 :                     bits_param_lpc[i] = bits_param_lpc[tmp];
    1288         609 :                     move16();
    1289             :                 }
    1290         262 :                 lpc_param[0] = safety_net;
    1291         262 :                 move16(); /* put the safety net info on the last param */
    1292         262 :                 bits_param_lpc[0] = 1;
    1293         262 :                 move16();
    1294         262 :                 *no_indices = add( *no_indices, 1 );
    1295             :             }
    1296             :         }
    1297             :     }
    1298             : 
    1299             : 
    1300             :     /*--------------------------------------------------------------------------*
    1301             :      *  De-quantize encoded LSF vector
    1302             :      *--------------------------------------------------------------------------*/
    1303             : 
    1304        2669 :     IF( safety_net )
    1305             :     {
    1306             :         /* Safety-net */
    1307         394 :         test();
    1308         394 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1309             :         {
    1310             :             /* BC-TCQ */
    1311         104 :             Copy( lsfq, st->mem_MA_fx, M );
    1312         104 :             Vr_add( lsfq, pred0, qlsf, M );
    1313             :         }
    1314             :         ELSE
    1315             :         {
    1316             :             {
    1317         290 :                 vq_dec_lvq_fx( 1, qlsf, &indice[0], stages0, M, mode_lvq, levels0[stages0 - 1],
    1318             :                                &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],
    1319             :                                &st->no_scales_fx[0][0], &st->no_scales_p_fx[0][0] );
    1320         290 :                 Vr_add( qlsf, pred0, qlsf, M );
    1321         290 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
    1322             :             }
    1323             :         }
    1324             :     }
    1325             :     ELSE
    1326             :     {
    1327        2275 :         test();
    1328        2275 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1329             :         {
    1330             :             /* BC-TCVQ */
    1331         525 :             Copy( lsfq, st->mem_MA_fx, M );
    1332         525 :             Vr_add( lsfq, pred2, qlsf, M );
    1333             :         }
    1334             :         ELSE
    1335             :         {
    1336             :             /* LVQ */
    1337        1750 :             vq_dec_lvq_fx( 0, qlsf, &indice[0], stages1, M, mode_lvq_p, levels1[stages1 - 1],
    1338             :                            &st->offset_scale1_fx[0][0], &st->offset_scale2_fx[0][0], &st->offset_scale1_p_fx[0][0],
    1339             :                            &st->offset_scale2_p_fx[0][0], &st->no_scales_fx[0][0], &st->no_scales_p_fx[0][0] );
    1340        1750 :             IF( EQ_16( predmode, 1 ) )
    1341             :             {
    1342        1442 :                 Copy( qlsf, st->mem_MA_fx, M );
    1343        1442 :                 Vr_add( qlsf, pred1, qlsf, M );
    1344             :             }
    1345             :             ELSE
    1346             :             {
    1347         308 :                 Vr_add( qlsf, pred2, qlsf, M );
    1348         308 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
    1349             :             }
    1350             :         }
    1351             :     }
    1352             : 
    1353             :     /* Sort the quantized vector to ascending order */
    1354        2669 :     sort_fx( qlsf, 0, M - 1 );
    1355             : 
    1356             :     /* Verify stability by adding minimum separation */
    1357        2669 :     reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
    1358             : 
    1359             :     /* Update AR-predictor memories */
    1360        2669 :     Copy( qlsf, st->mem_AR_fx, M );
    1361        2669 :     return;
    1362             : }
    1363             : 
    1364      269164 : void lsf_end_enc_ivas_fx(
    1365             :     Encoder_State *st,           /* i/o: encoder state structure                                */
    1366             :     const Word16 *lsf,           /* i  : LSF in the frequency domain (0..6400)                  x2.56*/
    1367             :     Word16 *qlsf,                /* o  : quantized LSF                                          x2.56*/
    1368             :     const Word16 nBits_in,       /* i  : number of bits to spend on ISF quantization            */
    1369             :     const Word16 coder_type_org, /* i  : coding type                                       */
    1370             :     Word16 Q_ener,               /* i  : Q valuen for Bin_Ener                             */
    1371             :     Word16 force_sf,             /* i  : Force safety-net usage if coding type supports    */
    1372             :     Word16 *lpc_param,
    1373             :     Word16 *no_indices,
    1374             :     Word16 *bits_param_lpc,
    1375             :     Word16 coder_type_raw /* i  : Coder type (LSF coder_type have some special cases)*/
    1376             :     ,
    1377             :     const Word16 tdm_lsfQ_PCh[M] /* i  : Q LSFs for primary channel          */
    1378             : )
    1379             : {
    1380             :     Word16 i;
    1381             :     Word16 Idx0[MAX_VQ_STAGES + 3];      /* Optimal codebook indices for safety-net quantizer                 */
    1382             :     Word16 Idx1[MAX_VQ_STAGES + 3];      /* Optimal codebook indices for predictive quantizer                 */
    1383             :     Word16 indice[MAX_VQ_STAGES + 3];    /* Temp. array of indice for vector de-quantizer                     */
    1384      269164 :     Word16 mode_lvq = 0, mode_lvq_p = 0; /* LVQ mode and predictive LVQ mode                                */
    1385             :     Word16 bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
    1386      269164 :     const Word16 *Bit_alloc1 = NULL;
    1387             :     Word32 Err[2];                 /* Quantization error for safety-net(0) and predictive(1) quantizers */
    1388             :     Word16 Tmp[M];                 /* Temporary target vector (mean and prediction removed)             */
    1389             :     Word16 pred0[M];               /* Prediction for the safety-net quantizer (usually mean)            */
    1390             :     Word16 pred1[M];               /* Prediction for the predictive quantizer                           */
    1391             :     Word16 pred2[M];               /* Prediction for the predictive quantizer                           */
    1392             :     Word16 wghts[M];               /* Weighting used for quantizer (currently GSM based)                */
    1393             :     Word16 stages0;                /* Amount of stages used by safety-net quantizer                     */
    1394             :     Word16 stages1;                /* Amount of stages used by predictive quantizer                     */
    1395             :     Word16 levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer       */
    1396             :     Word16 levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer       */
    1397             :     Word16 predmode;               /* 0: safety-net only, 1: predictive only, 2: best of the two        */
    1398             :     Word16 safety_net, cumleft, num_bits;
    1399             :     Word16 *Idx, stages, *bits;
    1400             :     Word16 Tmp2[M], Tmp1[M];
    1401             :     Word32 abs_threshold; /* Absolute threshold depending on signal bandwidth, that indicates
    1402             :                              very good perceptual LSF quantization performance                 */
    1403             :     Word16 lsfq[M * 2], resq[M * 2];
    1404             :     Word16 coder_type;     /* coder type (from LSF quantizer point of view)                     */
    1405             :     Word16 nBits;          /* Number of bits                                                    */
    1406             :     Word16 TCQIdx0[M + 2]; /* Optimal codebook indices for VQ-TCQ quantizer                     */
    1407             :     Word16 *TCQIdx;
    1408             :     Word16 tmp;
    1409             :     Word16 flag_1bit_gran;
    1410      269164 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
    1411             :     Word16 pred3[M];
    1412             :     Word16 dummy, dummy_v[5];
    1413             : 
    1414      269164 :     flag_1bit_gran = (Word16) GT_16( st->element_mode, EVS_MONO );
    1415             : 
    1416      269164 :     nBits = nBits_in;
    1417      269164 :     move16();
    1418             :     /* Update LSF coder_type for LSF quantizer for some special cases */
    1419      269164 :     test();
    1420      269164 :     test();
    1421      269164 :     test();
    1422             : 
    1423      269164 :     IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( st->codec_mode, MODE1 ) && ( st->idchan == 0 ) ) /* this bit is used only for primary channel or mono */
    1424             :     {
    1425       61280 :         IF( EQ_16( coder_type_raw, VOICED ) )
    1426             :         {
    1427       18775 :             coder_type = VOICED;
    1428       18775 :             move16(); /* Reflect Inactive mode */
    1429       18775 :             if ( EQ_16( flag_1bit_gran, 1 ) )
    1430             :             {
    1431       18775 :                 nBits = sub( nBits, 1 ); /* This is for real Generic*/
    1432             :             }
    1433             :         }
    1434             :         ELSE
    1435             :         {
    1436       42505 :             nBits = sub( nBits, 1 ); /* This is for real Generic*/
    1437       42505 :             coder_type = coder_type_org;
    1438       42505 :             move16();
    1439             :         }
    1440             :     }
    1441             :     ELSE
    1442             :     {
    1443      207884 :         coder_type = coder_type_org;
    1444      207884 :         move16();
    1445             :     }
    1446             : 
    1447             :     /*----------------------------------------------------------------------------------- -*
    1448             :      * Calculate the number of stages and levels for each stage based on allowed bit budget
    1449             :      * Set absolute threshold for codebook-type decision logic depending on signal bandwidth
    1450             :      *------------------------------------------------------------------------------------ -*/
    1451      269164 :     IF( st->bwidth == NB )
    1452             :     {
    1453           0 :         abs_threshold = SFNETLOWLIMIT_NB / 2;
    1454           0 :         move32();
    1455             :     }
    1456             :     ELSE
    1457             :     {
    1458      269164 :         abs_threshold = SFNETLOWLIMIT_WB / 2;
    1459      269164 :         move32();
    1460             :     }
    1461             :     /* Calculate LSF weighting coefficients */
    1462      269164 :     Unified_weighting_fx( &st->Bin_E_fx[L_FFT / 2], Q_ener, lsf, wghts, (Word16) EQ_16( st->bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), st->sr_core, M );
    1463             : 
    1464             :     /*--------------------------------------------------------------------------------*
    1465             :      * LSF quantization of SID frames
    1466             :      *--------------------------------------------------------------------------------*/
    1467      269164 :     IF( EQ_32( st->core_brate, SID_2k40 ) )
    1468             :     {
    1469         447 :         lsfq_CNG_ivas_fx( hBstr, lsf, wghts, qlsf );
    1470         447 :         sort_fx( qlsf, 0, M - 1 );
    1471         447 :         reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
    1472             : 
    1473         447 :         return;
    1474             :     }
    1475             :     /* Find allowed predictor mode for current coder_type. (SN only (0), SN/AR switched (2) or MA predictive (1) */
    1476      268717 :     find_pred_mode( &predmode, coder_type, st->bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
    1477             : 
    1478             :     /*----------------------------------------------------------------*
    1479             :      * Calculate number of stages and levels for each stage based on the allowed bit allocation
    1480             :      * (subtract one bit for LSF predictor selection)
    1481             :      *----------------------------------------------------------------*/
    1482      268717 :     lsf_allocate_fx( sub( nBits, shr( predmode, 1 ) ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1, bits0, bits1 );
    1483             : 
    1484             : 
    1485             :     /*--------------------------------------------------------------------------------*
    1486             :      * LSF quantization of all other frames but SID frames
    1487             :      * Select safety-net or predictive mode
    1488             :      *--------------------------------------------------------------------------------*/
    1489             : 
    1490      268717 :     Err[0] = MAXINT32;
    1491      268717 :     move32();
    1492      268717 :     Err[1] = MAXINT32;
    1493      268717 :     move32();
    1494             :     /* for mem_MA update */
    1495     4568189 :     FOR( i = 0; i < M; i++ )
    1496             :     {
    1497     4299472 :         pred1[i] = add( ModeMeans_fx[mode_lvq][i], mult_r( MU_MA_FX, st->mem_MA_fx[i] ) );
    1498     4299472 :         move16();
    1499             :     }
    1500             : 
    1501      268717 :     test();
    1502      268717 :     test();
    1503             :     /* TD stereo SCh: perform intra-frame prediction with pulling-to-mean */
    1504      268717 :     IF( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
    1505             :     {
    1506             :         /* if secondary channel predmode is set to be > 2 */
    1507         120 :         predmode = add( predmode, 3 );
    1508             : 
    1509         120 :         tdm_SCh_LSF_intra_pred_fx( st->element_brate, tdm_lsfQ_PCh, pred3 );
    1510             :     }
    1511             : 
    1512      268717 :     IF( predmode == 0 )
    1513             :     {
    1514             :         /* Subtract only mean */
    1515       14289 :         Copy( ModeMeans_fx[mode_lvq], pred0, M );
    1516       14289 :         Vr_subt( lsf, pred0, Tmp, M );
    1517             : 
    1518             :         /* LVQ quantization (safety-net only) */
    1519       14289 :         Err[0] = vq_lvq_lsf_enc_ivas_fx( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, resq, lsfq );
    1520       14289 :         safety_net = 1;
    1521       14289 :         move16();
    1522       14289 :         st->pstreaklen = 0;
    1523       14289 :         move16(); /* predictive LSF quantizer streak is ended with safety-net */
    1524             :     }
    1525      254428 :     ELSE IF( EQ_16( predmode, 1 ) ) /* only MA prediction */
    1526             :     {
    1527      177570 :         Vr_subt( lsf, pred1, Tmp1, M );
    1528      177570 :         Err[1] = vq_lvq_lsf_enc_ivas_fx( 2, mode_lvq_p, Tmp1, levels1, stages1, wghts, Idx1, lsf, pred1, resq, lsfq );
    1529             : 
    1530      177570 :         safety_net = 0;
    1531      177570 :         move16();
    1532             :     }
    1533             :     ELSE
    1534             :     {
    1535       76858 :         IF( EQ_16( predmode, 2 ) )
    1536             :         {
    1537             :             /* Subtract mean and AR prediction */
    1538       76738 :             Copy( ModeMeans_fx[mode_lvq], pred0, M );
    1539             : 
    1540             :             /* subtract only mean */
    1541       76738 :             Vr_subt( lsf, pred0, Tmp, M );
    1542             : 
    1543     1304546 :             FOR( i = 0; i < M; i++ )
    1544             :             {
    1545             :                 /* subtract mean and AR prediction */
    1546     1227808 :                 pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) );
    1547     1227808 :                 Tmp2[i] = sub( lsf[i], pred2[i] );
    1548             :             }
    1549             :             /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
    1550             :             case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
    1551       76738 :             test();
    1552       76738 :             test();
    1553       76738 :             test();
    1554       76738 :             if ( ( ( GT_16( st->pstreaklen, ( STREAKLEN + 3 ) ) ) && ( EQ_16( coder_type, VOICED ) ) ) || ( ( GT_16( st->pstreaklen, ( STREAKLEN ) ) ) && ( NE_16( coder_type, VOICED ) ) ) )
    1555             :             {
    1556             :                 /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
    1557       11996 :                 st->streaklimit_fx = mult( st->streaklimit_fx, STREAKMULT_FX );
    1558       11996 :                 move16();
    1559             :             }
    1560             : 
    1561       76738 :             if ( st->pstreaklen == 0 )
    1562             :             {
    1563             :                 /* reset the consecutive AR-predictor multiplier */
    1564       28273 :                 st->streaklimit_fx = 32767; /*1.0 in Q15 */
    1565       28273 :                 move16();
    1566             :             }
    1567             : 
    1568             :             /* VOICED_WB@16kHz */
    1569       76738 :             test();
    1570       76738 :             test();
    1571       76738 :             IF( EQ_32( st->sr_core, INT_FS_16k ) && EQ_16( coder_type, VOICED ) && flag_1bit_gran == 0 )
    1572             :             {
    1573             :                 /* select safety_net or predictive */
    1574           0 :                 safety_net = qlsf_Mode_Select_fx( wghts, Tmp2, st->streaklimit_fx, OP_LOOP_THR_HVO );
    1575           0 :                 if ( EQ_16( force_sf, 1 ) )
    1576             :                 {
    1577           0 :                     safety_net = 1;
    1578           0 :                     move16();
    1579             :                 }
    1580             : 
    1581           0 :                 IF( safety_net )
    1582             :                 {
    1583             :                     /* Safety-net - BC-TCQ quantization : SN */
    1584           0 :                     Err[0] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
    1585           0 :                     st->pstreaklen = 0;
    1586           0 :                     move16();
    1587             :                 }
    1588             :                 ELSE
    1589             :                 {
    1590             :                     /* predictive - BC-TCQ quantization : AR */
    1591           0 :                     Err[1] = qlsf_ARSN_tcvq_Enc_16k_fx( Tmp2, lsfq, TCQIdx0, wghts, sub( nBits, 1 ), safety_net );
    1592           0 :                     st->pstreaklen = add( st->pstreaklen, 1 );
    1593             :                 }
    1594             :             }
    1595             :             /* all other frames (not VOICED@16kHz) */
    1596             :             ELSE
    1597             :             {
    1598             :                 /* safety-net */
    1599             : 
    1600       76738 :                 Err[0] = vq_lvq_lsf_enc_ivas_fx( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, resq, lsfq );
    1601             :                 /* Predictive quantizer is calculated only if it can be selected */
    1602       76738 :                 test();
    1603       76738 :                 IF( !force_sf || GT_32( Err[0], abs_threshold ) )
    1604             :                 {
    1605       76702 :                     Err[1] = vq_lvq_lsf_enc_ivas_fx( 2, mode_lvq_p, Tmp2, levels1, stages1, wghts, Idx1, lsf, pred2, &resq[M], &lsfq[M] );
    1606             :                 }
    1607       76738 :                 test();
    1608       76738 :                 test();
    1609             :                 /* Select whether to use safety-net (non-predictive) or predictive LSF quantizer. The decision is based on following:
    1610             :                 if the non-predictive (safety-net) quantization error (Err[0]) is low enough (spectral distortion is low) it is selected
    1611             :                 or if the predictively quantized error (Err[1]) is by at least adaptive margin smaller than non-predictive quantizer.
    1612             :                 or if the in case of frame erasure the resulting concealed predictive LSF would be unstable safety-net is selected */
    1613       76738 :                 IF( force_sf || LT_32( Mult_32_16( Err[0], ( st->streaklimit_fx ) ), L_add( Err[1], Mult_32_16( Err[1], PREFERSFNET_FX ) ) ) || LT_32( Err[0], abs_threshold ) )
    1614             :                 {
    1615       23816 :                     safety_net = 1;
    1616       23816 :                     move16();
    1617       23816 :                     st->pstreaklen = 0;
    1618       23816 :                     move16(); /* Reset the consecutive predictive frame counter */
    1619             :                 }
    1620             :                 ELSE
    1621             :                 {
    1622       52922 :                     safety_net = 0;
    1623       52922 :                     move16(); /* Increase the consecutive predictive frame counter by one */
    1624       52922 :                     st->pstreaklen = add( st->pstreaklen, 1 );
    1625             :                 }
    1626             :             }
    1627             :         }
    1628             :         ELSE /* of "if (predmode==2)" */
    1629             :         {
    1630         120 :             Copy( ModeMeans_fx[mode_lvq], pred0, M );
    1631             : 
    1632         120 :             if ( EQ_16( predmode, 4 ) )
    1633             :             {
    1634          99 :                 mode_lvq_p = 9; /* force to Generic WB with AR*/
    1635          99 :                 move16();
    1636             :             }
    1637             : 
    1638        2040 :             FOR( i = 0; i < M; i++ )
    1639             :             {
    1640             :                 /* subtract mean and AR prediction */
    1641        1920 :                 pred2[i] = add( pred0[i], mult( Predictors_fx[mode_lvq_p][i], sub( st->mem_AR_fx[i], pred0[i] ) ) );
    1642        1920 :                 Tmp[i] = sub( lsf[i], pred2[i] );
    1643        1920 :                 Tmp2[i] = sub( lsf[i], pred3[i] );
    1644             :             }
    1645             : 
    1646             :             /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
    1647             :             case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
    1648         120 :             IF( GT_16( st->pstreaklen, ( STREAKLEN ) ) )
    1649             :             {
    1650             :                 /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
    1651           1 :                 st->streaklimit_fx = mult( st->streaklimit_fx, STREAKMULT_FX );
    1652           1 :                 move16();
    1653             :             }
    1654             : 
    1655         120 :             if ( st->pstreaklen == 0 )
    1656             :             {
    1657             :                 /* reset the adaptive scaling factor */
    1658          74 :                 st->streaklimit_fx = 32767;
    1659          74 :                 move16();
    1660             :             }
    1661             : 
    1662             :             /* intra pred */
    1663             :             /* use G AR pred for the intra mode (as a safety mode, this is why the indexes 0/1 are interchanged)*/
    1664             : 
    1665         120 :             lsf_allocate_fx( sub( nBits, 1 ), mode_lvq, 9, &stages1, &stages0, levels1, levels0, bits1, bits0 );
    1666         120 :             Err[0] = vq_lvq_lsf_enc_ivas_fx( 2, 9, Tmp2, levels0, stages0, wghts, Idx0, lsf, pred3, &resq[M], &lsfq[M] );
    1667         120 :             IF( force_sf )
    1668             :             {
    1669           0 :                 safety_net = 1;     /* intra-frame prediction */
    1670           0 :                 st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
    1671             :             }
    1672             :             ELSE
    1673             :             { /* try also the inter frame prediction */
    1674             : 
    1675             :                 /* AR inter-frame prediction */
    1676         120 :                 lsf_allocate_fx( nBits - 1, mode_lvq, mode_lvq_p, &dummy, &stages1, dummy_v, levels1, dummy_v, bits1 );
    1677         120 :                 Err[1] = vq_lvq_lsf_enc_ivas_fx( 2, mode_lvq_p, Tmp, levels1, stages1, wghts, Idx1, lsf, pred2, resq, lsfq );
    1678         120 :                 IF( LT_32( Mult_32_16( Err[0], ( st->streaklimit_fx ) ), L_add( Err[1], Mult_32_16( Err[1], PREFERSFNET_FX ) ) ) )
    1679             :                 {
    1680          66 :                     safety_net = 1;
    1681          66 :                     move16();
    1682          66 :                     st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
    1683          66 :                     move16();
    1684             :                 }
    1685             :                 ELSE
    1686             :                 {
    1687          54 :                     safety_net = 0;
    1688          54 :                     move16();
    1689          54 :                     st->pstreaklen = add( st->pstreaklen, 1 ); /* Increase the consecutive predictive frame counter by one */
    1690          54 :                     move16();
    1691             :                 }
    1692             :             }
    1693             :         }
    1694             :     }
    1695             :     /*--------------------------------------------------------------------------*
    1696             :      * Write indices to array
    1697             :      *--------------------------------------------------------------------------*/
    1698             : 
    1699      268717 :     test();
    1700      268717 :     IF( EQ_16( st->codec_mode, MODE1 ) && EQ_16( st->core, ACELP_CORE ) )
    1701             :     {
    1702             :         /* write coder_type bit for VOICED@16kHz or GENERIC@16kHz */
    1703      156600 :         test();
    1704      156600 :         test();
    1705      156600 :         IF( EQ_16( coder_type_org, GENERIC ) && EQ_32( st->sr_core, INT_FS_16k ) && ( st->idchan == 0 ) )
    1706             :         {
    1707             :             /* VOICED =2 and GENERIC=3, so "coder_type-2" means VOICED =0 and GENERIC=1*/
    1708       61280 :             push_indice( hBstr, IND_LSF_PREDICTOR_SELECT_BIT, sub( coder_type, 2 ), 1 );
    1709             :         }
    1710             : 
    1711             :         /* write predictor selection bit */
    1712      156600 :         IF( GE_16( predmode, 2 ) )
    1713             :         {
    1714       39706 :             push_indice( st->hBstr, IND_LSF_PREDICTOR_SELECT_BIT, safety_net, 1 );
    1715             :         }
    1716             : 
    1717      156600 :         test();
    1718      156600 :         test();
    1719      156600 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1720             :         {
    1721             :             /* BC-TCVQ (only for VOICED@16kHz) */
    1722           0 :             TCQIdx = &TCQIdx0[1];
    1723           0 :             Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
    1724           0 :             FOR( i = 0; i < ( M / 2 ) + 3; i++ )
    1725             :             {
    1726           0 :                 push_indice( hBstr, IND_LSF, TCQIdx[i], Bit_alloc1[i] );
    1727             :             }
    1728             :         }
    1729             :         ELSE
    1730             :         {
    1731      156600 :             cumleft = nBits;
    1732      156600 :             move16();
    1733      156600 :             IF( GE_16( predmode, 2 ) )
    1734             :             {
    1735             :                 /* subtract predictor selection bit */
    1736       39706 :                 cumleft = sub( nBits, 1 );
    1737             :             }
    1738             : 
    1739      156600 :             IF( safety_net )
    1740             :             {
    1741       25602 :                 stages = stages0;
    1742       25602 :                 move16();
    1743       25602 :                 Idx = Idx0;
    1744       25602 :                 move16();
    1745       25602 :                 bits = bits0;
    1746       25602 :                 move16();
    1747             :             }
    1748             :             ELSE
    1749             :             {
    1750      130998 :                 stages = stages1;
    1751      130998 :                 move16();
    1752      130998 :                 Idx = Idx1;
    1753      130998 :                 move16();
    1754      130998 :                 bits = bits1;
    1755      130998 :                 move16();
    1756             :             }
    1757             : 
    1758      156600 :             tmp = sub( stages, 1 );
    1759      361331 :             FOR( i = 0; i < tmp; i++ )
    1760             :             {
    1761      204731 :                 indice[i] = Idx[i];
    1762      204731 :                 move16();
    1763      204731 :                 num_bits = bits[i];
    1764      204731 :                 move16();
    1765      204731 :                 cumleft -= num_bits;
    1766      204731 :                 move16();
    1767      204731 :                 push_indice( hBstr, IND_LSF, indice[i], num_bits );
    1768             :             }
    1769             : 
    1770      589590 :             WHILE( cumleft > 0 )
    1771             :             {
    1772      432990 :                 indice[i] = Idx[i];
    1773      432990 :                 move16();
    1774             : 
    1775      432990 :                 IF( GT_16( cumleft, LEN_INDICE ) )
    1776             :                 {
    1777      276390 :                     num_bits = LEN_INDICE;
    1778      276390 :                     move16();
    1779             :                 }
    1780             :                 ELSE
    1781             :                 {
    1782      156600 :                     num_bits = cumleft;
    1783      156600 :                     move16();
    1784             :                 }
    1785             : 
    1786      432990 :                 cumleft = sub( cumleft, num_bits );
    1787      432990 :                 push_indice( hBstr, IND_LSF, indice[i], num_bits );
    1788      432990 :                 i = add( i, 1 );
    1789             :             }
    1790             :         }
    1791             :     }
    1792             :     ELSE
    1793             :     {
    1794      112117 :         test();
    1795      112117 :         test();
    1796      112117 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1797             :         {
    1798             :             /* BC-TCVQ (only for VOICED@16kHz) */
    1799             :             /* Number of quantization indices */
    1800           0 :             *no_indices = 10;
    1801           0 :             move16();
    1802           0 :             FOR( i = 0; i < *no_indices; i++ )
    1803             :             {
    1804           0 :                 lpc_param[i] = TCQIdx0[i];
    1805           0 :                 move16();
    1806           0 :                 bits_param_lpc[i] = BC_TCVQ_BIT_ALLOC_40B[i];
    1807           0 :                 move16();
    1808             :             }
    1809             :         }
    1810             :         ELSE
    1811             :         {
    1812             :             /* Number of quantization indices */
    1813             : 
    1814             :             /* there are 31 bits */
    1815      112117 :             IF( EQ_16( safety_net, 1 ) )
    1816             :             {
    1817       12569 :                 Idx = Idx0;
    1818       12569 :                 move16();
    1819       12569 :                 *no_indices = add( stages0, 1 );
    1820       37707 :                 FOR( i = 0; i < stages0; i++ )
    1821             :                 {
    1822       25138 :                     lpc_param[i] = Idx[i];
    1823       25138 :                     move16();
    1824       25138 :                     indice[i] = Idx[i];
    1825       25138 :                     move16();
    1826       25138 :                     bits_param_lpc[i] = bits0[i];
    1827       25138 :                     move16();
    1828             :                 }
    1829       12569 :                 lpc_param[stages0] = Idx[stages0];
    1830       12569 :                 move16();
    1831       12569 :                 indice[stages0] = Idx[stages0];
    1832       12569 :                 move16();
    1833       12569 :                 tmp = sub( stages0, 1 );
    1834       12569 :                 bits_param_lpc[tmp] = LEN_INDICE;
    1835       12569 :                 move16();
    1836       12569 :                 bits_param_lpc[stages0] = sub( bits0[tmp], LEN_INDICE );
    1837             :             }
    1838             :             ELSE
    1839             :             {
    1840       99548 :                 *no_indices = add( stages1, 1 );
    1841       99548 :                 Idx = Idx1;
    1842       99548 :                 move16();
    1843      274061 :                 FOR( i = 0; i < stages1; i++ )
    1844             :                 {
    1845      174513 :                     lpc_param[i] = ( Idx[i] );
    1846      174513 :                     move16();
    1847      174513 :                     indice[i] = Idx[i];
    1848      174513 :                     move16();
    1849      174513 :                     bits_param_lpc[i] = bits1[i];
    1850      174513 :                     move16();
    1851             :                 }
    1852       99548 :                 lpc_param[stages1] = ( Idx[stages1] );
    1853       99548 :                 move16();
    1854       99548 :                 indice[stages1] = Idx[stages1];
    1855       99548 :                 move16();
    1856       99548 :                 tmp = sub( stages1, 1 );
    1857       99548 :                 bits_param_lpc[tmp] = LEN_INDICE;
    1858       99548 :                 move16();
    1859       99548 :                 bits_param_lpc[stages1] = sub( bits1[tmp], LEN_INDICE );
    1860             :             }
    1861      112117 :             IF( EQ_16( predmode, 2 ) )
    1862             :             {
    1863      124025 :                 FOR( i = *no_indices; i > 0; i-- )
    1864             :                 {
    1865       86873 :                     tmp = sub( i, 1 );
    1866       86873 :                     lpc_param[i] = lpc_param[tmp];
    1867       86873 :                     move16();
    1868       86873 :                     bits_param_lpc[i] = bits_param_lpc[tmp];
    1869       86873 :                     move16();
    1870             :                 }
    1871       37152 :                 lpc_param[0] = safety_net;
    1872       37152 :                 move16(); /* put the safety net info on the last param */
    1873       37152 :                 bits_param_lpc[0] = 1;
    1874       37152 :                 move16();
    1875       37152 :                 *no_indices = add( *no_indices, 1 );
    1876             :             }
    1877             :         }
    1878             :     }
    1879             : 
    1880             : 
    1881             :     /*--------------------------------------------------------------------------*
    1882             :      *  De-quantize encoded LSF vector
    1883             :      *--------------------------------------------------------------------------*/
    1884             : 
    1885      268717 :     IF( safety_net )
    1886             :     {
    1887             :         /* Safety-net */
    1888       38171 :         test();
    1889       38171 :         test();
    1890       38171 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1891             :         {
    1892             :             /* BC-TCQ */
    1893           0 :             Copy( lsfq, st->mem_MA_fx, M );
    1894           0 :             Vr_add( lsfq, pred0, qlsf, M );
    1895             :         }
    1896             :         ELSE
    1897             :         {
    1898       38171 :             IF( EQ_16( st->tdm_LRTD_flag, 0 ) && EQ_16( st->idchan, 1 ) && tdm_lsfQ_PCh != NULL )
    1899             :             {
    1900             :                 /* intra mode*/
    1901          66 :                 vq_dec_lvq_ivas_fx( 0, qlsf, &indice[0], stages0, M, 9, levels0[stages0 - 1] );
    1902          66 :                 Vr_add( qlsf, pred3, qlsf, M );
    1903          66 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
    1904             :             }
    1905             :             ELSE
    1906             :             {
    1907       38105 :                 vq_dec_lvq_ivas_fx( 1, qlsf, &indice[0], stages0, M, mode_lvq, levels0[stages0 - 1] );
    1908       38105 :                 Vr_add( qlsf, pred0, qlsf, M );
    1909       38105 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
    1910             :             }
    1911             :         }
    1912             :     }
    1913             :     ELSE
    1914             :     {
    1915      230546 :         test();
    1916      230546 :         IF( EQ_16( coder_type, VOICED ) && EQ_32( st->sr_core, INT_FS_16k ) && flag_1bit_gran == 0 )
    1917             :         {
    1918             :             /* BC-TCVQ */
    1919           0 :             Copy( lsfq, st->mem_MA_fx, M );
    1920           0 :             Vr_add( lsfq, pred2, qlsf, M );
    1921             :         }
    1922             :         ELSE
    1923             :         {
    1924             :             /* LVQ */
    1925      230546 :             vq_dec_lvq_ivas_fx( 0, qlsf, &indice[0], stages1, M, mode_lvq_p, levels1[stages1 - 1] );
    1926      230546 :             IF( EQ_16( predmode, 1 ) )
    1927             :             {
    1928      177570 :                 Copy( qlsf, st->mem_MA_fx, M );
    1929      177570 :                 Vr_add( qlsf, pred1, qlsf, M );
    1930             :             }
    1931             :             ELSE
    1932             :             {
    1933       52976 :                 Vr_add( qlsf, pred2, qlsf, M );
    1934       52976 :                 Vr_subt( qlsf, pred1, st->mem_MA_fx, M );
    1935             :             }
    1936             :         }
    1937             :     }
    1938             : 
    1939             :     /* Sort the quantized vector to ascending order */
    1940      268717 :     sort_fx( qlsf, 0, M - 1 );
    1941             : 
    1942             :     /* Verify stability by adding minimum separation */
    1943      268717 :     reorder_lsf_fx( qlsf, MODE1_LSF_GAP_FX, M, st->sr_core );
    1944             : 
    1945             :     /* Update AR-predictor memories */
    1946      268717 :     Copy( qlsf, st->mem_AR_fx, M );
    1947      268717 :     return;
    1948             : }
    1949             : 
    1950             : /*-------------------------------------------------------------------*
    1951             :  * first_VQstages()
    1952             :  *
    1953             :  *
    1954             :  *-------------------------------------------------------------------*/
    1955             : 
    1956        2247 : static void first_VQstages(
    1957             :     const Word16 *const *cb,
    1958             :     Word16 u[],       /* i  : vector to be encoded (prediction and mean removed)  x2.56*/
    1959             :     Word16 *levels,   /* i  : number of levels in each stage                      */
    1960             :     Word16 stagesVQ,  /* i  : number of stages                                    */
    1961             :     Word16 w[],       /* i  : weights                                             Q8*/
    1962             :     Word16 N,         /* i  : vector dimension                                    */
    1963             :     Word16 max_inner, /* i  : maximum number of swaps in inner loop               */
    1964             :     Word16 indices_VQstage[] )
    1965             : {
    1966             :     Word16 resid_buf[2 * LSFMBEST * M], *resid[2];
    1967             :     Word32 dist_buf[2 * LSFMBEST], *dist[2], en;
    1968             :     Word32 f_tmp, L_tmp, L_tmp1, *pTmp32;
    1969             :     Word16 Tmp[M], *pTmp, cs;
    1970        2247 :     Word16 *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j,
    1971             :                                                                                   m, s, c, c2, p_max, *indices[2];
    1972        2247 :     Word16 maxC = LSFMBEST;
    1973             : #ifndef ISSUE_1867_replace_overflow_libenc
    1974             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1975             :     Flag Overflow = 0;
    1976             : #endif
    1977             : #endif
    1978             : 
    1979             :     /*float dd[16];*/
    1980             :     const Word16 *cb_stage, *cbp;
    1981             : 
    1982             :     /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
    1983        2247 :     indices[0] = idx_buf;
    1984        2247 :     move16();
    1985        2247 :     indices[1] = idx_buf + maxC * stagesVQ;
    1986        2247 :     move16();
    1987        2247 :     resid[0] = resid_buf;
    1988        2247 :     move16();
    1989        2247 :     resid[1] = resid_buf + maxC * N;
    1990        2247 :     move16();
    1991        2247 :     dist[0] = dist_buf;
    1992        2247 :     move16();
    1993        2247 :     dist[1] = dist_buf + maxC;
    1994        2247 :     move16();
    1995             : 
    1996        2247 :     set16_fx( idx_buf, 0, ( const Word16 )( 2 * stagesVQ * maxC ) );
    1997        2247 :     set16_fx( parents, 0, maxC );
    1998             : 
    1999             :     /* Set up inital distance vector */
    2000        2247 :     L_tmp = L_deposit_l( 0 );
    2001       38199 :     FOR( j = 0; j < N; j++ )
    2002             :     {
    2003             : #ifdef ISSUE_1867_replace_overflow_libenc
    2004       35952 :         L_tmp1 = L_shl_sat( L_mult0( u[j], w[j] ), 7 ); /*x2.56 + Q8 + Q7 */
    2005             : #else
    2006             :         L_tmp1 = L_shl_o( L_mult0( u[j], w[j] ), 7, &Overflow );                               /*x2.56 + Q8 + Q7 */
    2007             : #endif
    2008       35952 :         L_tmp1 = Mult_32_16( L_tmp1, u[j] ); /*x2.56 + Q15 + x2.56 -Q15 */
    2009       35952 :         L_tmp = L_add( L_tmp, L_tmp1 );      /*Q0 + x2.56 +x2.56 */
    2010             :     }
    2011        2247 :     set32_fx( dist[1], L_tmp, maxC );
    2012             : 
    2013             :     /* Set up initial error (residual) vectors */
    2014        2247 :     pTmp = resid[1];
    2015        2247 :     move16();
    2016        6741 :     FOR( c = 0; c < maxC; c++ )
    2017             :     {
    2018        4494 :         Copy( u, pTmp, N );
    2019        4494 :         pTmp += N;
    2020             :     }
    2021             : 
    2022             :     /*----------------------------------------------------------------*
    2023             :      * LSF quantization
    2024             :      *----------------------------------------------------------------*/
    2025             : 
    2026             :     /* Loop over all stages */
    2027        2247 :     m = 1;
    2028        2247 :     move16();
    2029        5037 :     FOR( s = 0; s < stagesVQ; s++ )
    2030             :     {
    2031             :         /* set codebook pointer to point to first stage */
    2032        2790 :         cbp = cb[s];
    2033        2790 :         move16();
    2034             : 
    2035             :         /* save pointer to the beginning of the current stage */
    2036        2790 :         cb_stage = cbp;
    2037        2790 :         move16();
    2038             : 
    2039             :         /* swap pointers to parent and current nodes */
    2040        2790 :         pTmp_short = indices[0];
    2041        2790 :         indices[0] = indices[1];
    2042        2790 :         move16();
    2043        2790 :         indices[1] = pTmp_short;
    2044        2790 :         move16();
    2045             : 
    2046        2790 :         pTmp = resid[0];
    2047        2790 :         resid[0] = resid[1];
    2048        2790 :         move16();
    2049        2790 :         resid[1] = pTmp;
    2050        2790 :         move16();
    2051             : 
    2052        2790 :         pTmp32 = dist[0];
    2053        2790 :         dist[0] = dist[1];
    2054        2790 :         move32();
    2055        2790 :         dist[1] = pTmp32;
    2056        2790 :         move32();
    2057             : 
    2058             :         /* p_max points to maximum distortion node (worst of best) */
    2059        2790 :         p_max = 0;
    2060        2790 :         move16();
    2061             : 
    2062             :         /* set distortions to a large value */
    2063        2790 :         set32_fx( dist[1], MAXINT32, maxC );
    2064             : 
    2065       68374 :         FOR( j = 0; j < levels[s]; j++ )
    2066             :         {
    2067             :             /* compute weighted codebook element and its energy */
    2068     1114928 :             FOR( c2 = 0; c2 < N; c2++ )
    2069             :             {
    2070     1049344 :                 Tmp[c2] = shl( mult( w[c2], cbp[c2] ), 2 ); /* Q8 + x2.56 -Q15 +Q2 */
    2071     1049344 :                 move16();
    2072             :             }
    2073             : 
    2074       65584 :             en = L_mult( cbp[0], Tmp[0] );
    2075             : 
    2076     1049344 :             FOR( c2 = 1; c2 < N; c2++ )
    2077             :             {
    2078      983760 :                 en = L_mac( en, cbp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-5 +Q1 */
    2079             :             }
    2080       65584 :             cbp += N;
    2081       65584 :             move16();
    2082             : 
    2083             :             /* iterate over all parent nodes */
    2084      138200 :             FOR( c = 0; c < m; c++ )
    2085             :             {
    2086       72616 :                 pTmp = &resid[0][c * N];
    2087       72616 :                 move16();
    2088       72616 :                 L_tmp = L_mult( pTmp[0], Tmp[0] );
    2089     1161856 :                 FOR( c2 = 1; c2 < N; c2++ )
    2090             :                 {
    2091     1089240 :                     L_tmp = L_mac( L_tmp, pTmp[c2], Tmp[c2] ); /* */
    2092             :                 }
    2093             : 
    2094       72616 :                 L_tmp = L_add( dist[0][c], L_sub( en, L_shl( L_tmp, 1 ) ) );
    2095             : 
    2096       72616 :                 IF( LE_32( L_tmp, dist[1][p_max] ) )
    2097             :                 {
    2098             :                     /* replace worst */
    2099       20766 :                     dist[1][p_max] = L_tmp;
    2100       20766 :                     move32();
    2101       20766 :                     indices[1][p_max * stagesVQ + s] = j;
    2102       20766 :                     move16();
    2103       20766 :                     parents[p_max] = c;
    2104       20766 :                     move16();
    2105             : 
    2106             :                     /* limit number of times inner loop is entered */
    2107       20766 :                     IF( LT_16( counter, max_inner ) )
    2108             :                     {
    2109       20766 :                         counter = add( counter, 1 );
    2110       20766 :                         IF( LT_16( counter, max_inner ) )
    2111             :                         {
    2112             :                             /* find new worst */
    2113       20766 :                             p_max = maximum_32_fx( dist[1], maxC, &f_tmp );
    2114             :                         }
    2115             :                         ELSE
    2116             :                         {
    2117             :                             /* find minimum distortion */
    2118           0 :                             p_max = minimum_32_fx( dist[1], maxC, &f_tmp );
    2119             :                         }
    2120             :                     }
    2121             :                 }
    2122             :             }
    2123             :         }
    2124             : 
    2125             :         /*------------------------------------------------------------*
    2126             :          * Compute error vectors for each node
    2127             :          *------------------------------------------------------------*/
    2128        2790 :         cs = 0;
    2129        2790 :         move16();
    2130        8370 :         FOR( c = 0; c < maxC; c++ )
    2131             :         {
    2132             :             /* subtract codebook entry from the residual vector of the parent node */
    2133        5580 :             pTmp = resid[1] + c * N;
    2134        5580 :             move16();
    2135        5580 :             Copy( resid[0] + parents[c] * N, pTmp, N );
    2136        5580 :             Vr_subt( pTmp, cb_stage + ( indices[1][cs + s] ) * N, pTmp, N );
    2137             : 
    2138             :             /* get indices that were used for parent node */
    2139        5580 :             Copy( indices[0] + parents[c] * stagesVQ, indices[1] + cs, s );
    2140        5580 :             cs = add( cs, stagesVQ );
    2141             :         }
    2142             : 
    2143        2790 :         m = maxC;
    2144        2790 :         move16();
    2145             :     }
    2146             : 
    2147        2247 :     Copy( indices[1], indices_VQstage, maxC * stagesVQ );
    2148             : 
    2149        2247 :     return;
    2150             : }
    2151             : 
    2152      302177 : static void first_VQstages_ivas_fx(
    2153             :     const Word16 *const *cb,
    2154             :     Word16 u[],       /* i  : vector to be encoded (prediction and mean removed)  */
    2155             :     Word16 *levels,   /* i  : number of levels in each stage                      */
    2156             :     Word16 stagesVQ,  /* i  : number of stages                                    */
    2157             :     Word16 w[],       /* i  : weights                                             */
    2158             :     Word16 N,         /* i  : vector dimension                                    */
    2159             :     Word16 max_inner, /* i  : maximum number of swaps in inner loop               */
    2160             :     Word16 indices_VQstage[] )
    2161             : {
    2162             :     Word16 resid_buf[2 * LSFMBEST * M], *resid[2];
    2163             :     Word32 dist_buf[2 * LSFMBEST], *dist[2], en;
    2164             :     Word32 f_tmp, L_tmp, L_tmp1, *pTmp32;
    2165             :     Word16 Tmp[M], *pTmp, cs;
    2166      302177 :     Word16 *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j,
    2167             :                                                                                   m, s, c, c2, p_max, *indices[2];
    2168      302177 :     Word16 maxC = LSFMBEST;
    2169             : #ifndef ISSUE_1867_replace_overflow_libenc
    2170             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    2171             :     Flag Overflow = 0;
    2172             : #endif
    2173             : #endif
    2174             : 
    2175             :     /*float dd[16];*/
    2176             :     const Word16 *cb_stage, *cbp;
    2177             : 
    2178             :     /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
    2179      302177 :     indices[0] = idx_buf;
    2180      302177 :     move16();
    2181      302177 :     indices[1] = idx_buf + maxC * stagesVQ;
    2182      302177 :     move16();
    2183      302177 :     resid[0] = resid_buf;
    2184      302177 :     move16();
    2185      302177 :     resid[1] = resid_buf + maxC * N;
    2186      302177 :     move16();
    2187      302177 :     dist[0] = dist_buf;
    2188      302177 :     move16();
    2189      302177 :     dist[1] = dist_buf + maxC;
    2190      302177 :     move16();
    2191             : 
    2192      302177 :     set16_fx( idx_buf, 0, ( const Word16 )( 2 * stagesVQ * maxC ) );
    2193      302177 :     set16_fx( parents, 0, maxC );
    2194             : 
    2195             :     /* Set up inital distance vector */
    2196      302177 :     L_tmp = L_deposit_l( 0 );
    2197     5137009 :     FOR( j = 0; j < N; j++ )
    2198             :     {
    2199             : #ifdef ISSUE_1867_replace_overflow_libenc
    2200     4834832 :         L_tmp1 = L_shl_sat( L_mult0( u[j], w[j] ), 7 ); /*x2.56 + Q8 + Q7 */
    2201             : #else
    2202             :         L_tmp1 = L_shl_o( L_mult0( u[j], w[j] ), 7, &Overflow );                               /*x2.56 + Q8 + Q7 */
    2203             : #endif
    2204     4834832 :         L_tmp1 = Mult_32_16( L_tmp1, u[j] ); /*x2.56 + Q15 + x2.56 -Q15 */
    2205     4834832 :         L_tmp = L_add( L_tmp, L_tmp1 );      /*Q0 + x2.56 +x2.56 */
    2206             :     }
    2207      302177 :     set32_fx( dist[1], L_shr( L_tmp, 1 ), maxC ); /*Q-1 + x2.56 +x2.56 */
    2208             : 
    2209             :     /* Set up initial error (residual) vectors */
    2210      302177 :     pTmp = resid[1];
    2211      302177 :     move16();
    2212      906531 :     FOR( c = 0; c < maxC; c++ )
    2213             :     {
    2214      604354 :         Copy( u, pTmp, N );
    2215      604354 :         pTmp += N;
    2216             :     }
    2217             : 
    2218             :     /*----------------------------------------------------------------*
    2219             :      * LSF quantization
    2220             :      *----------------------------------------------------------------*/
    2221             : 
    2222             :     /* Loop over all stages */
    2223      302177 :     m = 1;
    2224      302177 :     move16();
    2225      689636 :     FOR( s = 0; s < stagesVQ; s++ )
    2226             :     {
    2227             :         /* set codebook pointer to point to first stage */
    2228      387459 :         cbp = cb[s];
    2229      387459 :         move16();
    2230             : 
    2231             :         /* save pointer to the beginning of the current stage */
    2232      387459 :         cb_stage = cbp;
    2233      387459 :         move16();
    2234             : 
    2235             :         /* swap pointers to parent and current nodes */
    2236      387459 :         pTmp_short = indices[0];
    2237      387459 :         indices[0] = indices[1];
    2238      387459 :         move16();
    2239      387459 :         indices[1] = pTmp_short;
    2240      387459 :         move16();
    2241             : 
    2242      387459 :         pTmp = resid[0];
    2243      387459 :         resid[0] = resid[1];
    2244      387459 :         move16();
    2245      387459 :         resid[1] = pTmp;
    2246      387459 :         move16();
    2247             : 
    2248      387459 :         pTmp32 = dist[0];
    2249      387459 :         dist[0] = dist[1];
    2250      387459 :         move32();
    2251      387459 :         dist[1] = pTmp32;
    2252      387459 :         move32();
    2253             : 
    2254             :         /* p_max points to maximum distortion node (worst of best) */
    2255      387459 :         p_max = 0;
    2256      387459 :         move16();
    2257             : 
    2258             :         /* set distortions to a large value */
    2259      387459 :         set32_fx( dist[1], MAXINT32, maxC );
    2260             : 
    2261     9072435 :         FOR( j = 0; j < levels[s]; j++ )
    2262             :         {
    2263             :             /* compute weighted codebook element and its energy */
    2264   147644592 :             FOR( c2 = 0; c2 < N; c2++ )
    2265             :             {
    2266   138959616 :                 Tmp[c2] = extract_h( L_shl( L_mult0( w[c2], cbp[c2] ), 6 ) ); /* Q8 + x2.56 + q6 -q16 */
    2267   138959616 :                 move16();
    2268             :             }
    2269             : 
    2270     8684976 :             en = L_mult( cbp[0], Tmp[0] ); /*x2.56 + x2.56 + Q-2 +Q1 */
    2271             : 
    2272   138959616 :             FOR( c2 = 1; c2 < N; c2++ )
    2273             :             {
    2274   130274640 :                 en = L_mac( en, cbp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-2 +Q1 */
    2275             :             }
    2276     8684976 :             cbp += N;
    2277     8684976 :             move16();
    2278             : 
    2279             :             /* iterate over all parent nodes */
    2280    18465824 :             FOR( c = 0; c < m; c++ )
    2281             :             {
    2282     9780848 :                 pTmp = &resid[0][c * N]; /*x2.56*/
    2283     9780848 :                 move16();
    2284     9780848 :                 L_tmp = L_mult( pTmp[0], Tmp[0] ); /*x2.56 + x2.56 + Q-2 +Q1 */
    2285   156493568 :                 FOR( c2 = 1; c2 < N; c2++ )
    2286             :                 {
    2287   146712720 :                     L_tmp = L_mac( L_tmp, pTmp[c2], Tmp[c2] ); /*x2.56 + x2.56 + Q-2 +Q1 */
    2288             :                 }
    2289             : 
    2290     9780848 :                 L_tmp = L_add( dist[0][c], L_sub( en, L_shl( L_tmp, 1 ) ) ); /*x2.56 + x2.56 -1*/
    2291             : 
    2292     9780848 :                 IF( LE_32( L_tmp, dist[1][p_max] ) )
    2293             :                 {
    2294             :                     /* replace worst */
    2295     2836455 :                     dist[1][p_max] = L_tmp;
    2296     2836455 :                     move32();
    2297     2836455 :                     indices[1][p_max * stagesVQ + s] = j;
    2298     2836455 :                     move16();
    2299     2836455 :                     parents[p_max] = c;
    2300     2836455 :                     move16();
    2301             : 
    2302             :                     /* limit number of times inner loop is entered */
    2303     2836455 :                     IF( LT_16( counter, max_inner ) )
    2304             :                     {
    2305     2836455 :                         counter = add( counter, 1 );
    2306     2836455 :                         IF( LT_16( counter, max_inner ) )
    2307             :                         {
    2308             :                             /* find new worst */
    2309     2836455 :                             p_max = maximum_32_fx( dist[1], maxC, &f_tmp );
    2310             :                         }
    2311             :                         ELSE
    2312             :                         {
    2313             :                             /* find minimum distortion */
    2314           0 :                             p_max = minimum_32_fx( dist[1], maxC, &f_tmp );
    2315             :                         }
    2316             :                     }
    2317             :                 }
    2318             :             }
    2319             :         }
    2320             : 
    2321             :         /*------------------------------------------------------------*
    2322             :          * Compute error vectors for each node
    2323             :          *------------------------------------------------------------*/
    2324      387459 :         cs = 0;
    2325      387459 :         move16();
    2326     1162377 :         FOR( c = 0; c < maxC; c++ )
    2327             :         {
    2328             :             /* subtract codebook entry from the residual vector of the parent node */
    2329      774918 :             pTmp = resid[1] + c * N;
    2330      774918 :             move16();
    2331      774918 :             Copy( resid[0] + parents[c] * N, pTmp, N );
    2332      774918 :             Vr_subt( pTmp, cb_stage + ( indices[1][cs + s] ) * N, pTmp, N );
    2333             : 
    2334             :             /* get indices that were used for parent node */
    2335      774918 :             Copy( indices[0] + parents[c] * stagesVQ, indices[1] + cs, s );
    2336      774918 :             cs = add( cs, stagesVQ );
    2337             :         }
    2338             : 
    2339      387459 :         m = maxC;
    2340      387459 :         move16();
    2341             :     }
    2342             : 
    2343      302177 :     Copy( indices[1], indices_VQstage, maxC * stagesVQ );
    2344             : 
    2345      302177 :     return;
    2346             : }
    2347             : /*---------------------------------------------------------------------------
    2348             :  * vq_enc_lsf_lvq()
    2349             :  *
    2350             :  *  Multi-stage VQ encoder for LSF quantization. Trained codebooks are used in initial stages
    2351             :  *  and lattice-VQ quantization is applied on residual vector in other stages.
    2352             :  *
    2353             :  * Note:
    2354             :  *    Compared to normal multistage VQ resulting LSF vector is reordered to ascending order before
    2355             :  *    weighted error calculation (spectral distortion) at the final stage.
    2356             :  *
    2357             :  * Returns:
    2358             :  *    Weighted error
    2359             :  *--------------------------------------------------------------------------*/
    2360             : 
    2361        2508 : static Word32 vq_lvq_lsf_enc(
    2362             :     Word16 pred_flag,
    2363             :     Word16 mode,
    2364             :     Word16 u[], // x2.56
    2365             :     Word16 *levels,
    2366             :     Word16 stages,
    2367             :     Word16 w[], // Q8
    2368             :     Word16 Idx[],
    2369             :     const Word16 *lsf,
    2370             :     const Word16 *pred,
    2371             :     Word32 p_offset_scale1[][MAX_NO_SCALES + 1],
    2372             :     Word32 p_offset_scale2[][MAX_NO_SCALES + 1],
    2373             :     Word16 p_no_scales[][2],
    2374             :     Word16 *resq,
    2375             :     Word16 *lsfq )
    2376             : {
    2377             :     Word16 i;
    2378             :     const Word16 *const *cb, *cb_stage;
    2379             :     Word16 cand[LSFMBEST][M];
    2380        2508 :     Word16 maxC = LSFMBEST, stagesVQ;
    2381             :     Word16 mode_glb, j, indices_firstVQ[LSFMBEST * MAX_VQ_STAGES], c2;
    2382             :     Word32 e[LSFMBEST], L_tmp, L_ftmp;
    2383             :     Word16 quant[LSFMBEST][M], diff[M], dd[M];
    2384             :     Word16 lat_cv[LSFMBEST][M];
    2385             :     Word16 idx_lead[LSFMBEST][2], idx_scale[LSFMBEST][2];
    2386             : 
    2387        2508 :     stagesVQ = sub( stages, 1 );
    2388             :     /* Codebook selection */
    2389        2508 :     IF( pred_flag == 0 ) /* safety net*/
    2390             :     {
    2391         598 :         cb = &Quantizers_fx[CB_lsf[mode]];
    2392         598 :         move16();
    2393         598 :         mode_glb = add( offset_lvq_modes_SN_fx[mode], offset_in_lvq_mode_SN_fx[mode][( levels[stagesVQ] - min_lat_bits_SN_fx[mode] )] ); // Q0
    2394             :     }
    2395             :     ELSE /*  predictive */
    2396             :     {
    2397        1910 :         cb = &Quantizers_p_fx[CB_p_lsf[mode]];
    2398        1910 :         move16();
    2399        1910 :         mode_glb = add( offset_lvq_modes_pred_fx[mode], offset_in_lvq_mode_pred_fx[mode][( levels[stagesVQ] - min_lat_bits_pred_fx[mode] )] ); // Q0
    2400             :     }
    2401        2508 :     IF( stagesVQ > 0 )
    2402             :     {
    2403             :         /* first VQ stages */
    2404        2247 :         first_VQstages( cb, u, levels, stagesVQ, w, M, MSVQ_MAXCNT, indices_firstVQ );
    2405             :     }
    2406             : 
    2407             : 
    2408        7524 :     FOR( i = 0; i < maxC; i++ )
    2409             :     {
    2410        5016 :         Copy( pred, cand[i], M );
    2411       10596 :         FOR( j = 0; j < stagesVQ; j++ )
    2412             :         {
    2413        5580 :             Idx[j] = indices_firstVQ[i * stagesVQ + j];
    2414        5580 :             move16();
    2415             :         }
    2416             : 
    2417       10596 :         FOR( j = 0; j < stagesVQ; j++ )
    2418             :         {
    2419        5580 :             cb_stage = cb[j];
    2420        5580 :             move16();
    2421        5580 :             Vr_add( cand[i], cb_stage + Idx[j] * M, cand[i], M );
    2422             :         }
    2423             : 
    2424             :         /* LVQ quantization */
    2425        5016 :         Vr_subt( lsf, cand[i], dd, M );
    2426        5016 :         mslvq_fx( dd, quant[i], lat_cv[i], idx_lead[i], idx_scale[i], w, mode, mode_glb, pred_flag, p_no_scales );
    2427        5016 :         Vr_add( cand[i], quant[i], cand[i], M );
    2428             : 
    2429             :         /* arrange the LSF candidate vector prior to selection to an ascending order*/
    2430        5016 :         sort_fx( cand[i], 0, M - 1 );
    2431             : 
    2432             :         /* calculate the spectral distortion using weighted MSE of sorted LSF vector*/
    2433        5016 :         Vr_subt( cand[i], lsf, diff, M );
    2434       85272 :         FOR( j = 0; j < M; j++ )
    2435             :         {
    2436       80256 :             diff[j] = shl_sat( diff[j], 4 );
    2437       80256 :             move16();
    2438             :         }
    2439        5016 :         L_tmp = L_mult( mult( diff[0], shl_sat( w[0], 1 ) ), diff[0] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
    2440       80256 :         FOR( j = 1; j < M; j++ )
    2441             :         {
    2442       75240 :             L_tmp = L_mac( L_tmp, mult( diff[j], shl_sat( w[j], 1 ) ), diff[j] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
    2443             :         }
    2444        5016 :         e[i] = L_tmp; /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
    2445        5016 :         move32();
    2446             :     }
    2447             : 
    2448             :     /* find the optimal candidate */
    2449        2508 :     c2 = minimum_32_fx( e, maxC, &L_ftmp );
    2450        2508 :     set16_fx( resq, 0, M );
    2451        5298 :     FOR( j = 0; j < stagesVQ; j++ )
    2452             :     {
    2453        2790 :         Idx[j] = indices_firstVQ[c2 * stagesVQ + j];
    2454        2790 :         move16();
    2455        2790 :         cb_stage = cb[j];
    2456        2790 :         move16();
    2457        2790 :         Vr_add( resq, cb_stage + Idx[j] * M, resq, M );
    2458             :     }
    2459        2508 :     Vr_add( resq, quant[c2], resq, M ); /* quantized prediction residual */
    2460        2508 :     Copy( cand[c2], lsfq, M );
    2461        2508 :     index_lvq_fx( lat_cv[c2], idx_lead[c2], idx_scale[c2], mode_glb, &Idx[stagesVQ],
    2462             :                   &p_offset_scale1[0][0], &p_offset_scale2[0][0], &p_no_scales[0][0] );
    2463             : 
    2464        2508 :     return e[c2];
    2465             : }
    2466             : 
    2467      345539 : static Word32 vq_lvq_lsf_enc_ivas_fx(
    2468             :     Word16 pred_flag,
    2469             :     Word16 mode,
    2470             :     Word16 u[], // x2.56
    2471             :     Word16 *levels,
    2472             :     Word16 stages,
    2473             :     Word16 w[], // Q8
    2474             :     Word16 Idx[],
    2475             :     const Word16 *lsf,
    2476             :     const Word16 *pred,
    2477             :     Word16 *resq,
    2478             :     Word16 *lsfq )
    2479             : {
    2480             :     Word16 i;
    2481             :     const Word16 *const *cb, *cb_stage;
    2482             :     Word16 cand[LSFMBEST][M];
    2483      345539 :     Word16 maxC = LSFMBEST, stagesVQ;
    2484             :     Word16 mode_glb, j, indices_firstVQ[LSFMBEST * MAX_VQ_STAGES], c2;
    2485             :     Word32 e[LSFMBEST], L_tmp, L_ftmp;
    2486             :     Word16 quant[LSFMBEST][M], diff[M], dd[M];
    2487             :     Word16 lat_cv[LSFMBEST][M];
    2488             :     Word16 idx_lead[LSFMBEST][2], idx_scale[LSFMBEST][2];
    2489             : 
    2490      345539 :     stagesVQ = sub( stages, 1 );
    2491             :     /* Codebook selection */
    2492      345539 :     IF( pred_flag == 0 ) /* safety net*/
    2493             :     {
    2494       91027 :         cb = &Quantizers_fx[CB_lsf[mode]];
    2495       91027 :         IF( LT_16( mode, 6 ) )
    2496             :         {
    2497           0 :             move16();
    2498           0 :             mode_glb = add( offset_lvq_modes_SN[mode], offset_in_lvq_mode_SN[mode][( levels[stagesVQ] - min_lat_bits_SN[mode] )] );
    2499             :         }
    2500             :         ELSE
    2501             :         {
    2502       91027 :             move16();
    2503       91027 :             mode_glb = add( offset_lvq_modes_SN[mode], levels[stagesVQ] - min_lat_bits_SN[mode] );
    2504             :         }
    2505             :     }
    2506             :     ELSE /*  predictive */
    2507             :     {
    2508      254512 :         cb = &Quantizers_p_fx[CB_p_lsf[mode]];
    2509      254512 :         IF( LT_16( mode, 6 ) || EQ_16( mode, 12 ) )
    2510             :         {
    2511        5265 :             move16();
    2512        5265 :             mode_glb = add( offset_lvq_modes_pred[mode], offset_in_lvq_mode_pred[mode][( levels[stagesVQ] - min_lat_bits_pred_fx[mode] )] );
    2513             :         }
    2514             :         ELSE
    2515             :         {
    2516      249247 :             move16();
    2517      249247 :             mode_glb = sub( add( offset_lvq_modes_pred[mode], levels[stagesVQ] ), min_lat_bits_pred_fx[mode] );
    2518             :         }
    2519             :     }
    2520             : 
    2521      345539 :     IF( stagesVQ > 0 )
    2522             :     {
    2523             :         /* first VQ stages */
    2524      302177 :         first_VQstages_ivas_fx( cb, u, levels, stagesVQ, w, M, MSVQ_MAXCNT, indices_firstVQ );
    2525             :     }
    2526             : 
    2527             : 
    2528     1036617 :     FOR( i = 0; i < maxC; i++ )
    2529             :     {
    2530      691078 :         Copy( pred, cand[i], M );
    2531     1465996 :         FOR( j = 0; j < stagesVQ; j++ )
    2532             :         {
    2533      774918 :             Idx[j] = indices_firstVQ[i * stagesVQ + j];
    2534      774918 :             move16();
    2535             :         }
    2536             : 
    2537     1465996 :         FOR( j = 0; j < stagesVQ; j++ )
    2538             :         {
    2539      774918 :             cb_stage = cb[j];
    2540      774918 :             move16();
    2541      774918 :             Vr_add( cand[i], cb_stage + Idx[j] * M, cand[i], M );
    2542             :         }
    2543             : 
    2544             :         /* LVQ quantization */
    2545      691078 :         Vr_subt( lsf, cand[i], dd, M );
    2546      691078 :         mslvq_ivas_16( dd, quant[i], lat_cv[i], idx_lead[i], idx_scale[i], w, mode, mode_glb, pred_flag );
    2547      691078 :         Vr_add( cand[i], quant[i], cand[i], M );
    2548             : 
    2549             :         /* arrange the LSF candidate vector prior to selection to an ascending order*/
    2550      691078 :         sort_fx( cand[i], 0, M - 1 );
    2551             : 
    2552             :         /* calculate the spectral distortion using weighted MSE of sorted LSF vector*/
    2553      691078 :         Vr_subt( cand[i], lsf, diff, M );
    2554    11748326 :         FOR( j = 0; j < M; j++ )
    2555             :         {
    2556    11057248 :             diff[j] = shl_sat( diff[j], 4 );
    2557    11057248 :             move16();
    2558             :         }
    2559      691078 :         L_tmp = L_mult( mult( diff[0], shl_sat( w[0], 1 ) ), diff[0] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
    2560    11057248 :         FOR( j = 1; j < M; j++ )
    2561             :         {
    2562             : #ifdef FIX_1979_SAT_MISSING_IN_LSF_ENC
    2563    10366170 :             L_tmp = L_mac_sat( L_tmp, mult( diff[j], shl_sat( w[j], 1 ) ), diff[j] ); /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
    2564             : #else
    2565             :             L_tmp = L_mac( L_tmp, mult( diff[j], shl_sat( w[j], 1 ) ), diff[j] );              /*(2.56+Q5+ Q10 -Q15) + 2.56+ Q5 + Q1 = 2.56 + 2.56 + Q6 */
    2566             : #endif
    2567             :         }
    2568      691078 :         e[i] = L_tmp;
    2569      691078 :         move32();
    2570             :     }
    2571             : 
    2572             :     /* find the optimal candidate */
    2573      345539 :     c2 = minimum_32_fx( e, maxC, &L_ftmp );
    2574      345539 :     set16_fx( resq, 0, M );
    2575      732998 :     FOR( j = 0; j < stagesVQ; j++ )
    2576             :     {
    2577      387459 :         Idx[j] = indices_firstVQ[c2 * stagesVQ + j];
    2578      387459 :         move16();
    2579      387459 :         cb_stage = cb[j];
    2580      387459 :         move16();
    2581      387459 :         Vr_add( resq, cb_stage + Idx[j] * M, resq, M );
    2582             :     }
    2583      345539 :     Vr_add( resq, quant[c2], resq, M ); /* quantized prediction residual */
    2584      345539 :     Copy( cand[c2], lsfq, M );
    2585      345539 :     index_lvq_ivas_fx( lat_cv[c2], idx_lead[c2], idx_scale[c2], mode_glb, &Idx[stagesVQ], pred_flag );
    2586             : 
    2587      345539 :     return e[c2];
    2588             : }
    2589         629 : static void BcTcvq_1st_fx(
    2590             :     Word16 x_fx[][2],             /*x2.56*/
    2591             :     const Word16 CB_fx[][128][2], /*x2.56*/
    2592             :     Word16 s[][16],
    2593             :     Word16 c[][16],
    2594             :     Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2)*/
    2595             :     Word16 Q_fx[][16][2],
    2596             :     Word16 W_fx[][2] /*Q8*/
    2597             : )
    2598             : {
    2599             :     Word16 state, prev_state;
    2600             :     Word16 index, bestCode;
    2601             :     Word32 dist_fx, minDist_fx;
    2602             :     Word16 temp16_fx;
    2603             : 
    2604        5661 :     FOR( state = 0; state < NUM_STATE; state += 2 )
    2605             :     {
    2606        5032 :         prev_state = NTRANS[0][state];
    2607        5032 :         move16();
    2608        5032 :         index = NTRANS[2][state];
    2609        5032 :         move16();
    2610        5032 :         temp16_fx = sub( x_fx[0][0], CB_fx[0][index][0] );
    2611        5032 :         minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][0] ); /* 2.56*2.56*Q(-5) */
    2612        5032 :         temp16_fx = sub( x_fx[0][1], CB_fx[0][index][1] );
    2613        5032 :         minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][1] ) ); /* 2.56*2.56*Q(-5) */
    2614        5032 :         bestCode = index;
    2615        5032 :         move16();
    2616             : 
    2617       80512 :         FOR( index = index + 8; index < 128; index += 8 )
    2618             :         {
    2619       75480 :             temp16_fx = sub( x_fx[0][0], CB_fx[0][index][0] );
    2620       75480 :             dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][0] ); /* 2.56*2.56*Q(-5) */
    2621       75480 :             temp16_fx = sub( x_fx[0][1], CB_fx[0][index][1] );
    2622       75480 :             dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[0][1] ) ); /* 2.56*2.56*Q(-5) */
    2623             : 
    2624       75480 :             if ( LT_32( dist_fx, minDist_fx ) )
    2625             :             {
    2626       16632 :                 bestCode = index;
    2627       16632 :                 move16();
    2628             :             }
    2629       75480 :             minDist_fx = L_min( minDist_fx, dist_fx );
    2630             :         }
    2631             : 
    2632             :         /* Update */
    2633        5032 :         s[0][state] = prev_state;
    2634        5032 :         move16();
    2635        5032 :         c[0][state] = bestCode;
    2636        5032 :         move16();
    2637             : 
    2638        5032 :         cDist_fx[0][state] = L_shr( minDist_fx, 2 );
    2639        5032 :         move32(); /*2.56*2.56*Q(-5 - 2)*/
    2640        5032 :         Q_fx[0][state][0] = CB_fx[0][bestCode][0];
    2641        5032 :         move16();
    2642        5032 :         Q_fx[0][state][1] = CB_fx[0][bestCode][1];
    2643        5032 :         move16();
    2644             :     }
    2645             : 
    2646         629 :     return;
    2647             : }
    2648             : 
    2649             : 
    2650         629 : static void BcTcvq_2nd_fx(
    2651             :     Word16 x_fx[][2],             /*x2.56*/
    2652             :     const Word16 CB_fx[][128][2], /*x2.56*/
    2653             :     Word16 s[][16],
    2654             :     Word16 c[][16],
    2655             :     Word32 cDist_fx[][16],      /*2.56*2.56*Q(-5 - 2) */
    2656             :     Word16 Q_fx[][16][2],       /*x2.56*/
    2657             :     Word16 W_fx[][2],           /*Q8*/
    2658             :     const Word16 itc_fx[][2][2] /*Q15*/
    2659             : )
    2660             : {
    2661             :     Word16 state, prev_state;
    2662             :     Word16 index, bestCode;
    2663             :     Word32 dist_fx, minDist_fx;
    2664             :     Word16 pred_fx[N_DIM], target_fx[N_DIM];
    2665             :     Word16 temp16_fx;
    2666             : 
    2667       10693 :     FOR( state = 0; state < NUM_STATE; state++ )
    2668             :     {
    2669       10064 :         prev_state = NTRANS[0][state];
    2670       10064 :         move16();
    2671       10064 :         index = NTRANS[2][state];
    2672       10064 :         move16();
    2673             : 
    2674             :         /* Prediction */
    2675       10064 :         pred_fx[0] = add( mult_r( itc_fx[0][0][0], Q_fx[0][prev_state][0] ), mult_r( itc_fx[0][0][1], Q_fx[0][prev_state][1] ) );
    2676       10064 :         move16();
    2677       10064 :         pred_fx[1] = add( mult_r( itc_fx[0][1][0], Q_fx[0][prev_state][0] ), mult_r( itc_fx[0][1][1], Q_fx[0][prev_state][1] ) );
    2678       10064 :         move16();
    2679       10064 :         target_fx[0] = sub( x_fx[1][0], pred_fx[0] );
    2680       10064 :         move16(); /* x2.65 */
    2681       10064 :         target_fx[1] = sub( x_fx[1][1], pred_fx[1] );
    2682       10064 :         move16(); /* x2.65 */
    2683             : 
    2684       10064 :         temp16_fx = sub( target_fx[0], CB_fx[1][index][0] );
    2685       10064 :         minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][0] ); /* 2.65*2.65*Q(-5) */
    2686       10064 :         temp16_fx = sub( target_fx[1], CB_fx[1][index][1] );
    2687       10064 :         minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][1] ) ); /* 2.65*2.65*Q(-5) */
    2688             : 
    2689       10064 :         bestCode = index;
    2690       10064 :         move16();
    2691             : 
    2692      161024 :         FOR( index = index + 8; index < 128; index += 8 )
    2693             :         {
    2694      150960 :             temp16_fx = sub( target_fx[0], CB_fx[1][index][0] );
    2695      150960 :             dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][0] ); /* 2.65*2.65*Q(-5) */
    2696      150960 :             temp16_fx = sub( target_fx[1], CB_fx[1][index][1] );
    2697      150960 :             dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[1][1] ) ); /* 2.65*2.65*Q(-5) */
    2698             : 
    2699      150960 :             if ( LT_32( dist_fx, minDist_fx ) )
    2700             :             {
    2701       29286 :                 bestCode = index;
    2702       29286 :                 move16();
    2703             :             }
    2704      150960 :             minDist_fx = L_min( minDist_fx, dist_fx );
    2705             :         }
    2706             : 
    2707             :         /* Update */
    2708       10064 :         s[1][state] = prev_state;
    2709       10064 :         move16();
    2710       10064 :         c[1][state] = bestCode;
    2711       10064 :         move16();
    2712             : 
    2713       10064 :         cDist_fx[1][state] = L_add( cDist_fx[0][prev_state], L_shr( minDist_fx, 2 ) );
    2714       10064 :         move32(); /* 2.56*2.56*Q(-5 - 2) */
    2715       10064 :         Q_fx[1][state][0] = add( CB_fx[1][bestCode][0], pred_fx[0] );
    2716       10064 :         move16();
    2717       10064 :         Q_fx[1][state][1] = add( CB_fx[1][bestCode][1], pred_fx[1] );
    2718       10064 :         move16();
    2719             :     }
    2720             : 
    2721         629 :     return;
    2722             : }
    2723             : 
    2724        1258 : static void BcTcvq_SubBlock_fx(
    2725             :     Word16 x_fx[][2],            /*x2.56*/
    2726             :     const Word16 CB_fx[][64][2], /*x2.56*/
    2727             :     Word16 s[][16],
    2728             :     Word16 c[][16],
    2729             :     Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2)*/
    2730             :     Word16 Q_fx[][16][2],
    2731             :     Word16 stage,
    2732             :     Word16 W_fx[][2],           /*Q10*/
    2733             :     const Word16 itc_fx[][2][2] /*Q15*/
    2734             : )
    2735             : {
    2736             :     Word16 stage1, stage2, state, prev_state, branch;
    2737             :     Word16 index, bestCode, brCode[N_DIM];
    2738             :     Word16 temp16_fx;
    2739             : 
    2740             :     Word32 dist_fx, minDist_fx, brDist_fx[N_DIM];
    2741             :     Word16 pred_fx[N_DIM], target_fx[N_DIM], brQuant_fx[N_DIM][N_DIM];
    2742             : 
    2743        1258 :     stage1 = sub( stage, 1 );
    2744        1258 :     stage2 = sub( stage, 2 );
    2745             : 
    2746       21386 :     FOR( state = 0; state < NUM_STATE; state++ )
    2747             :     {
    2748             : 
    2749             :         /* 1st branch search */
    2750       20128 :         prev_state = NTRANS[0][state];
    2751       20128 :         move16();
    2752       20128 :         index = NTRANS[2][state];
    2753       20128 :         move16();
    2754             : 
    2755             :         /* Prediction */
    2756       20128 :         pred_fx[0] = add( mult_r( itc_fx[stage1][0][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][0][1], Q_fx[stage1][prev_state][1] ) );
    2757       20128 :         move16();
    2758       20128 :         pred_fx[1] = add( mult_r( itc_fx[stage1][1][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][1][1], Q_fx[stage1][prev_state][1] ) );
    2759       20128 :         move16();
    2760       20128 :         target_fx[0] = sub( x_fx[stage][0], pred_fx[0] );
    2761       20128 :         move16();
    2762       20128 :         target_fx[1] = sub( x_fx[stage][1], pred_fx[1] );
    2763       20128 :         move16();
    2764             : 
    2765       20128 :         temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
    2766       20128 :         minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] ); /* 2.65*2.65*Q(-5) */
    2767       20128 :         temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
    2768       20128 :         minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) ); /* 2.65*2.65*Q(-5) */
    2769             : 
    2770       20128 :         bestCode = index;
    2771       20128 :         move16();
    2772             : 
    2773      161024 :         FOR( index = index + 8; index < 64; index += 8 )
    2774             :         {
    2775      140896 :             temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
    2776      140896 :             dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] );
    2777      140896 :             temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
    2778      140896 :             dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) );
    2779             : 
    2780      140896 :             if ( LT_32( dist_fx, minDist_fx ) )
    2781             :             {
    2782       39368 :                 bestCode = index;
    2783       39368 :                 move16();
    2784             :             }
    2785      140896 :             minDist_fx = L_min( minDist_fx, dist_fx );
    2786             :         }
    2787             : 
    2788       20128 :         brCode[0] = bestCode;
    2789       20128 :         move16();
    2790             : 
    2791       20128 :         brDist_fx[0] = L_add( cDist_fx[stage1][prev_state], L_shr( minDist_fx, 2 ) );
    2792       20128 :         move32();
    2793       20128 :         brQuant_fx[0][0] = add( CB_fx[stage2][bestCode][0], pred_fx[0] );
    2794       20128 :         move16();
    2795       20128 :         brQuant_fx[0][1] = add( CB_fx[stage2][bestCode][1], pred_fx[1] );
    2796       20128 :         move16();
    2797             : 
    2798             :         /* 2nd branch search */
    2799       20128 :         prev_state = NTRANS[1][state];
    2800       20128 :         move16();
    2801       20128 :         index = NTRANS[3][state];
    2802       20128 :         move16();
    2803             : 
    2804             :         /* Prediction */
    2805       20128 :         pred_fx[0] = add( mult_r( itc_fx[stage1][0][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][0][1], Q_fx[stage1][prev_state][1] ) );
    2806       20128 :         move16();
    2807       20128 :         pred_fx[1] = add( mult_r( itc_fx[stage1][1][0], Q_fx[stage1][prev_state][0] ), mult_r( itc_fx[stage1][1][1], Q_fx[stage1][prev_state][1] ) );
    2808       20128 :         move16();
    2809       20128 :         target_fx[0] = sub( x_fx[stage][0], pred_fx[0] );
    2810       20128 :         move16();
    2811       20128 :         target_fx[1] = sub( x_fx[stage][1], pred_fx[1] );
    2812       20128 :         move16();
    2813             : 
    2814       20128 :         temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
    2815       20128 :         minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] ); /* 2.65*2.65*Q(-5) */
    2816       20128 :         temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
    2817       20128 :         minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) ); /* 2.65*2.65*Q(-5) */
    2818             : 
    2819       20128 :         bestCode = index;
    2820       20128 :         move16();
    2821             : 
    2822      161024 :         FOR( index = index + 8; index < 64; index += 8 )
    2823             :         {
    2824      140896 :             temp16_fx = sub( target_fx[0], CB_fx[stage2][index][0] );
    2825      140896 :             dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] );
    2826      140896 :             temp16_fx = sub( target_fx[1], CB_fx[stage2][index][1] );
    2827      140896 :             dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) );
    2828      140896 :             if ( LT_32( dist_fx, minDist_fx ) )
    2829             :             {
    2830       39821 :                 bestCode = index;
    2831       39821 :                 move16();
    2832             :             }
    2833      140896 :             minDist_fx = L_min( minDist_fx, dist_fx );
    2834             :         }
    2835             : 
    2836       20128 :         brCode[1] = bestCode;
    2837       20128 :         move16();
    2838             : 
    2839       20128 :         brDist_fx[1] = L_add( cDist_fx[stage1][prev_state], L_shr( minDist_fx, 2 ) );
    2840       20128 :         move32();
    2841       20128 :         brQuant_fx[1][0] = add( CB_fx[stage2][bestCode][0], pred_fx[0] );
    2842       20128 :         move16();
    2843       20128 :         brQuant_fx[1][1] = add( CB_fx[stage2][bestCode][1], pred_fx[1] );
    2844       20128 :         move16();
    2845             : 
    2846             :         /* Select Best branch */
    2847       20128 :         branch = 1;
    2848       20128 :         move16();
    2849             : 
    2850       20128 :         if ( LE_32( brDist_fx[0], brDist_fx[1] ) )
    2851             :         {
    2852        9511 :             branch = 0;
    2853        9511 :             move16();
    2854             :         }
    2855             : 
    2856             :         /* Update */
    2857       20128 :         s[stage][state] = NTRANS[branch][state];
    2858       20128 :         move16();
    2859       20128 :         c[stage][state] = brCode[branch];
    2860       20128 :         move16();
    2861             : 
    2862       20128 :         cDist_fx[stage][state] = brDist_fx[branch];
    2863       20128 :         move32();
    2864       20128 :         Q_fx[stage][state][0] = brQuant_fx[branch][0];
    2865       20128 :         move16();
    2866       20128 :         Q_fx[stage][state][1] = brQuant_fx[branch][1];
    2867       20128 :         move16();
    2868             :     }
    2869             : 
    2870        1258 :     return;
    2871             : }
    2872             : 
    2873      161024 : static Word32 BcTcvq_FixSearch_fx(
    2874             :     Word16 x_fx[][2],            /*x2.56*/
    2875             :     const Word16 CB_fx[][32][2], /*x2.56*/
    2876             :     Word16 c[][4],
    2877             :     Word16 Q_fx[][16][2],
    2878             :     const Word16 FixBranch[][4][4],
    2879             :     Word16 stage,
    2880             :     Word16 inis,
    2881             :     Word16 fins,
    2882             :     Word16 *prev_state,
    2883             :     Word16 W_fx[][2],           /*Q10*/
    2884             :     const Word16 itc_fx[][2][2] /*Q15*/
    2885             : )
    2886             : {
    2887             :     Word16 stage1, stage4, branch;
    2888             :     Word16 index, bestCode;
    2889             :     Word32 dist_fx, minDist_fx;
    2890             :     Word16 pred_fx[N_DIM], target_fx[N_DIM];
    2891             :     Word16 temp16_fx;
    2892             : 
    2893      161024 :     stage1 = sub( stage, 1 );
    2894      161024 :     stage4 = sub( stage, 4 );
    2895             : 
    2896      161024 :     branch = FixBranch[shr( inis, 2 )][fins][stage4];
    2897      161024 :     move16();
    2898      161024 :     index = NTRANS2[add( branch, 2 )][*prev_state];
    2899      161024 :     move16();
    2900             : 
    2901             :     /* Prediction */
    2902      161024 :     pred_fx[0] = add( mult_r( itc_fx[stage1][0][0], Q_fx[stage1][*prev_state][0] ), mult_r( itc_fx[stage1][0][1], Q_fx[stage1][*prev_state][1] ) );
    2903      161024 :     move16();
    2904      161024 :     pred_fx[1] = add( mult_r( itc_fx[stage1][1][0], Q_fx[stage1][*prev_state][0] ), mult_r( itc_fx[stage1][1][1], Q_fx[stage1][*prev_state][1] ) );
    2905      161024 :     move16();
    2906      161024 :     target_fx[0] = sub( x_fx[stage][0], pred_fx[0] );
    2907      161024 :     move16();
    2908      161024 :     target_fx[1] = sub( x_fx[stage][1], pred_fx[1] );
    2909      161024 :     move16();
    2910             : 
    2911      161024 :     temp16_fx = sub( target_fx[0], CB_fx[stage4][index][0] );
    2912      161024 :     minDist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] ); /* 2.65*2.65*Q(-5) */
    2913      161024 :     temp16_fx = sub( target_fx[1], CB_fx[stage4][index][1] );
    2914      161024 :     minDist_fx = L_add( minDist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) ); /* 2.65*2.65*Q(-5) */
    2915             : 
    2916      161024 :     bestCode = index;
    2917      161024 :     move16();
    2918             : 
    2919      644096 :     FOR( index = index + 8; index < 32; index += 8 )
    2920             :     {
    2921      483072 :         temp16_fx = sub( target_fx[0], CB_fx[stage4][index][0] );
    2922      483072 :         dist_fx = Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][0] );
    2923      483072 :         temp16_fx = sub( target_fx[1], CB_fx[stage4][index][1] );
    2924      483072 :         dist_fx = L_add( dist_fx, Mult_32_16( L_mult0( temp16_fx, temp16_fx ), W_fx[stage][1] ) );
    2925             : 
    2926      483072 :         if ( LT_32( dist_fx, minDist_fx ) )
    2927             :         {
    2928      197362 :             bestCode = index;
    2929      197362 :             move16();
    2930             :         }
    2931      483072 :         minDist_fx = L_min( minDist_fx, dist_fx );
    2932             :     }
    2933             : 
    2934             :     /* Update */
    2935      161024 :     *prev_state = NTRANS2[branch][*prev_state];
    2936      161024 :     move16();
    2937      161024 :     c[fins][stage4] = bestCode;
    2938      161024 :     move16();
    2939             : 
    2940      161024 :     Q_fx[stage][*prev_state][0] = add( CB_fx[stage4][bestCode][0], pred_fx[0] );
    2941      161024 :     move16();
    2942      161024 :     Q_fx[stage][*prev_state][1] = add( CB_fx[stage4][bestCode][1], pred_fx[1] );
    2943      161024 :     move16();
    2944             : 
    2945      161024 :     minDist_fx = L_shr( minDist_fx, 2 ); /*2.56*2.56*Q(-5 - 2)*/
    2946      161024 :     return minDist_fx;
    2947             : }
    2948         629 : static Word16 optimalPath_fx(
    2949             :     Word32 cDist_fx[][16], /*2.56*2.56*Q(-5 - 2)*/
    2950             :     Word32 blockDist_fx[], /*2.56*2.56*Q(-5 - 2)*/
    2951             :     Word16 blockCodeword[][4],
    2952             :     Word16 bestCodeword[],
    2953             :     Word16 codeWord[][16],
    2954             :     Word16 bestState[],
    2955             :     Word16 preState[][16] )
    2956             : {
    2957             :     Word16 stage, state;
    2958             :     Word32 opDist_fx[NUM_STATE];
    2959             :     Word32 minDist_fx;
    2960             :     Word16 fBlock;
    2961             :     Word16 prev_state;
    2962             : 
    2963       10693 :     FOR( state = 0; state < NUM_STATE; state++ )
    2964             :     {
    2965       10064 :         opDist_fx[state] = L_add( L_shr( cDist_fx[3][state], 1 ), L_shr( blockDist_fx[state], 1 ) );
    2966       10064 :         move32();
    2967             :     }
    2968             : 
    2969         629 :     minDist_fx = L_add( opDist_fx[0], 0 );
    2970         629 :     fBlock = 0;
    2971         629 :     move16();
    2972             : 
    2973       10064 :     FOR( state = 1; state < NUM_STATE; state++ )
    2974             :     {
    2975        9435 :         if ( LT_32( opDist_fx[state], minDist_fx ) )
    2976             :         {
    2977        1662 :             fBlock = state;
    2978        1662 :             move16();
    2979             :         }
    2980        9435 :         minDist_fx = L_min( minDist_fx, opDist_fx[state] );
    2981             :     }
    2982             : 
    2983         629 :     prev_state = bestState[4] = fBlock;
    2984         629 :     move16();
    2985         629 :     move16();
    2986             : 
    2987        3145 :     FOR( stage = N_STAGE_VQ - 5; stage >= 0; stage-- )
    2988             :     {
    2989        2516 :         bestCodeword[stage] = codeWord[stage][prev_state];
    2990        2516 :         move16();
    2991        2516 :         bestState[stage] = preState[stage][prev_state];
    2992        2516 :         move16();
    2993        2516 :         prev_state = bestState[stage];
    2994        2516 :         move16();
    2995             :     }
    2996             : 
    2997        3145 :     FOR( stage = 0; stage < 4; stage++ )
    2998             :     {
    2999        2516 :         bestCodeword[stage + 4] = blockCodeword[fBlock][stage];
    3000        2516 :         move16();
    3001             :     }
    3002             : 
    3003         629 :     return fBlock;
    3004             : }
    3005             : 
    3006         629 : static void quantEnc_fx(
    3007             :     Word16 *y_fx,
    3008             :     Word16 c[],
    3009             :     const Word16 CB_SUB1_fx[][128][2], /*x2.56*/
    3010             :     const Word16 CB_SUB2_fx[][64][2],  /*x2.56*/
    3011             :     const Word16 CB_SUB3_fx[][32][2],  /*x2.56*/
    3012             :     const Word16 itc_fx[][2][2]        // Q15
    3013             : )
    3014             : {
    3015             :     Word16 i, j;
    3016             :     Word16 stage;
    3017             :     Word16 pred_fx[N_DIM], Y_fx[8][2];
    3018             : 
    3019             :     /* stage #1 */
    3020         629 :     Y_fx[0][0] = CB_SUB1_fx[0][c[0]][0];
    3021         629 :     move16();
    3022         629 :     Y_fx[0][1] = CB_SUB1_fx[0][c[0]][1];
    3023         629 :     move16();
    3024             : 
    3025             :     /* stage #2 */
    3026         629 :     pred_fx[0] = add( mult_r( itc_fx[0][0][0], Y_fx[0][0] ), mult_r( itc_fx[0][0][1], Y_fx[0][1] ) );
    3027         629 :     move16();
    3028         629 :     pred_fx[1] = add( mult_r( itc_fx[0][1][0], Y_fx[0][0] ), mult_r( itc_fx[0][1][1], Y_fx[0][1] ) );
    3029         629 :     move16();
    3030         629 :     Y_fx[1][0] = add( CB_SUB1_fx[1][c[1]][0], pred_fx[0] );
    3031         629 :     move16();
    3032         629 :     Y_fx[1][1] = add( CB_SUB1_fx[1][c[1]][1], pred_fx[1] );
    3033         629 :     move16();
    3034             : 
    3035             :     /* stage #3 - #4 */
    3036        1887 :     FOR( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
    3037             :     {
    3038        1258 :         pred_fx[0] = add( mult_r( itc_fx[stage - 1][0][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][0][1], Y_fx[stage - 1][1] ) );
    3039        1258 :         move16();
    3040        1258 :         pred_fx[1] = add( mult_r( itc_fx[stage - 1][1][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][1][1], Y_fx[stage - 1][1] ) );
    3041        1258 :         move16();
    3042             : 
    3043        1258 :         Y_fx[stage][0] = add( CB_SUB2_fx[stage - 2][c[stage]][0], pred_fx[0] );
    3044        1258 :         move16();
    3045        1258 :         Y_fx[stage][1] = add( CB_SUB2_fx[stage - 2][c[stage]][1], pred_fx[1] );
    3046        1258 :         move16();
    3047             :     }
    3048             : 
    3049             :     /* stage #5 - #8 */
    3050        3145 :     FOR( stage = N_STAGE_VQ - 4; stage < N_STAGE_VQ; stage++ )
    3051             :     {
    3052        2516 :         pred_fx[0] = add( mult_r( itc_fx[stage - 1][0][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][0][1], Y_fx[stage - 1][1] ) );
    3053        2516 :         move16();
    3054        2516 :         pred_fx[1] = add( mult_r( itc_fx[stage - 1][1][0], Y_fx[stage - 1][0] ), mult_r( itc_fx[stage - 1][1][1], Y_fx[stage - 1][1] ) );
    3055        2516 :         move16();
    3056             : 
    3057        2516 :         Y_fx[stage][0] = add( CB_SUB3_fx[stage - 4][c[stage]][0], pred_fx[0] );
    3058        2516 :         move16();
    3059        2516 :         Y_fx[stage][1] = add( CB_SUB3_fx[stage - 4][c[stage]][1], pred_fx[1] );
    3060        2516 :         move16();
    3061             :     }
    3062             : 
    3063             :     /* Transform Vector to Scalar */
    3064        5661 :     FOR( i = 0; i < N_STAGE_VQ; i++ )
    3065             :     {
    3066       15096 :         FOR( j = 0; j < N_DIM; j++ )
    3067             :         {
    3068       10064 :             y_fx[i * N_DIM + j] = Y_fx[i][j];
    3069       10064 :             move16();
    3070             :         }
    3071             :     }
    3072             : 
    3073         629 :     return;
    3074             : }
    3075             : 
    3076         629 : static void buildCode_fx(
    3077             :     Word16 *ind,
    3078             :     Word16 fins,
    3079             :     Word16 c[],
    3080             :     Word16 s[] )
    3081             : {
    3082             :     Word16 stage;
    3083             :     Word16 BrIndex[4];
    3084             : 
    3085         629 :     set16_fx( BrIndex, 0, ( N_STAGE_VQ - 4 ) );
    3086             : 
    3087             : 
    3088        3145 :     FOR( stage = N_STAGE_VQ - 4; stage >= 1; stage-- )
    3089             :     {
    3090        2516 :         if ( GT_16( s[stage], 7 ) )
    3091             :         {
    3092        1262 :             BrIndex[stage - 1] = 1;
    3093        1262 :             move16();
    3094             :         }
    3095             :     }
    3096         629 :     ind[0] = fins;
    3097         629 :     move16();
    3098             : 
    3099             :     /* stage #1 - #2 */
    3100        1887 :     FOR( stage = 0; stage < 2; stage++ )
    3101             :     {
    3102        1258 :         ind[stage + 1] = shl( BrIndex[stage], 4 );
    3103        1258 :         move16();
    3104        1258 :         ind[stage + 1] = add( ind[stage + 1], shr( c[stage], 3 ) );
    3105        1258 :         move16();
    3106             :     }
    3107             : 
    3108             :     /* stage #3 - #4 */
    3109        1887 :     FOR( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
    3110             :     {
    3111        1258 :         ind[stage + 1] = shl( BrIndex[stage], 3 );
    3112        1258 :         move16();
    3113        1258 :         ind[stage + 1] = add( ind[stage + 1], shr( c[stage], 3 ) );
    3114        1258 :         move16();
    3115             :     }
    3116             : 
    3117             :     /* Stage #5 - #8 */
    3118        3145 :     FOR( stage = N_STAGE_VQ - 4; stage < N_STAGE_VQ; stage++ )
    3119             :     {
    3120        2516 :         ind[stage + 1] = shr( c[stage], 3 );
    3121        2516 :         move16();
    3122             :     }
    3123             : 
    3124         629 :     return;
    3125             : }
    3126         629 : static void BcTcvq_fx(
    3127             :     Word16 snFlag,
    3128             :     const Word16 *x_fx,      // x2.65
    3129             :     Word16 *y_fx,            // x2.65
    3130             :     const Word16 *weight_fx, // Q8
    3131             :     Word16 *ind )
    3132             : {
    3133             :     Word16 X_fx[N_STAGE_VQ][N_DIM], W_fx[N_STAGE_VQ][N_DIM];
    3134             : 
    3135             :     /* Count Variable */
    3136             :     Word16 i, j;
    3137             : 
    3138             :     /* TCVQ Structure */
    3139             :     Word16 stage, state, prev_state;
    3140             :     Word16 preState[N_STAGE_VQ][NUM_STATE];
    3141             :     Word16 codeWord[N_STAGE_VQ][NUM_STATE];
    3142             :     Word32 acumDist_fx[N_STAGE_VQ - 4][NUM_STATE];
    3143             :     Word16 inis, fins, ptr_fins;
    3144             :     Word16 fBlock;
    3145             :     Word16 fState[NUM_STATE];
    3146             :     Word16 fCodeword[4][4];
    3147             :     Word16 iniBlock[NUM_STATE];
    3148             :     Word16 blockCodeword[NUM_STATE][4];
    3149             : 
    3150             :     /* Prediction variable */
    3151             :     Word16 quant_fx[N_STAGE_VQ][NUM_STATE][N_DIM];
    3152             : 
    3153             :     /* Distortion variable */
    3154             :     Word32 minDist_fx;
    3155             :     Word32 fDist_fx;
    3156             :     Word32 blockDist_fx[NUM_STATE];
    3157             : 
    3158             :     /* Decoding variable */
    3159             :     Word16 bestCodeword[N_STAGE_VQ];
    3160             :     Word16 bestState[N_STAGE_VQ];
    3161             : 
    3162             :     /* Code Share variable */
    3163             :     const Word16( *TCVQ_CB_SUB1_fx )[128][2], ( *TCVQ_CB_SUB2_fx )[64][2], ( *TCVQ_CB_SUB3_fx )[32][2] /**/;
    3164             :     const Word16( *IntraCoeff_fx )[2][2];
    3165             : 
    3166             :     /* Memoryless Module */
    3167         629 :     IF( snFlag )
    3168             :     {
    3169         104 :         TCVQ_CB_SUB1_fx = SN_TCVQ_CB_SUB1_fx;
    3170         104 :         TCVQ_CB_SUB2_fx = SN_TCVQ_CB_SUB2_fx;
    3171         104 :         TCVQ_CB_SUB3_fx = SN_TCVQ_CB_SUB3_fx;
    3172         104 :         IntraCoeff_fx = SN_IntraCoeff_fx;
    3173             :     }
    3174             :     ELSE /* Memory Module */
    3175             :     {
    3176         525 :         TCVQ_CB_SUB1_fx = AR_TCVQ_CB_SUB1_fx;
    3177         525 :         TCVQ_CB_SUB2_fx = AR_TCVQ_CB_SUB2_fx;
    3178         525 :         TCVQ_CB_SUB3_fx = AR_TCVQ_CB_SUB3_fx;
    3179         525 :         IntraCoeff_fx = AR_IntraCoeff_fx;
    3180             :     }
    3181             : 
    3182             :     /* Transform Scalar to Vector */
    3183        5661 :     FOR( i = 0; i < N_STAGE_VQ; i++ )
    3184             :     {
    3185       15096 :         FOR( j = 0; j < N_DIM; j++ )
    3186             :         {
    3187       10064 :             X_fx[i][j] = x_fx[( N_DIM * i ) + j]; // x2.65
    3188       10064 :             move16();
    3189       10064 :             W_fx[i][j] = weight_fx[( N_DIM * i ) + j]; // x2.56
    3190       10064 :             move16();
    3191             :         }
    3192             :     }
    3193             : 
    3194             :     /* Initialzie */
    3195        3145 :     FOR( i = 0; i < N_STAGE_VQ - 4; i++ )
    3196             :     {
    3197       42772 :         FOR( j = 0; j < NUM_STATE; j++ )
    3198             :         {
    3199       40256 :             acumDist_fx[i][j] = L_deposit_l( 0 );
    3200             :         }
    3201             :     }
    3202             : 
    3203             :     /* BcTcvq Search */
    3204             :     /* stage #1 */
    3205         629 :     BcTcvq_1st_fx( X_fx, TCVQ_CB_SUB1_fx, preState, codeWord, acumDist_fx, quant_fx, W_fx );
    3206             : 
    3207             :     /* stage #2 */
    3208         629 :     BcTcvq_2nd_fx( X_fx, TCVQ_CB_SUB1_fx, preState, codeWord, acumDist_fx, quant_fx, W_fx, IntraCoeff_fx );
    3209             : 
    3210             :     /* stage #3 - #4 */
    3211        1887 :     FOR( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
    3212             :     {
    3213        1258 :         BcTcvq_SubBlock_fx( X_fx, TCVQ_CB_SUB2_fx, preState, codeWord, acumDist_fx, quant_fx, stage, W_fx, IntraCoeff_fx );
    3214             :     }
    3215             : 
    3216             :     /* Search initial state at each block */
    3217       10693 :     FOR( state = 0; state < NUM_STATE; state++ )
    3218             :     {
    3219       10064 :         prev_state = state;
    3220       10064 :         move16();
    3221             : 
    3222       50320 :         FOR( stage = N_STAGE_VQ - 5; stage >= 0; stage-- )
    3223             :         {
    3224       40256 :             prev_state = preState[stage][prev_state];
    3225       40256 :             move16();
    3226             :         }
    3227       10064 :         iniBlock[state] = prev_state;
    3228       10064 :         move16();
    3229             :     }
    3230             : 
    3231             :     /* stage #5 - #8 */
    3232       10693 :     FOR( state = 0; state < NUM_STATE; state++ )
    3233             :     {
    3234       10064 :         inis = iniBlock[state];
    3235       10064 :         move16();
    3236       10064 :         ptr_fins = shr( inis, 2 );
    3237       10064 :         minDist_fx = L_add( MAX_32, 0 );
    3238             : 
    3239       50320 :         FOR( i = 0; i < 4; i++ )
    3240             :         {
    3241       40256 :             fins = add( shl( ptr_fins, 2 ), i );
    3242       40256 :             prev_state = state;
    3243       40256 :             move16();
    3244       40256 :             fDist_fx = BcTcvq_FixSearch_fx( X_fx, TCVQ_CB_SUB3_fx, fCodeword, quant_fx, FixBranch_tbl, N_STAGE_VQ - 4, inis, i, &prev_state, W_fx, IntraCoeff_fx );
    3245             : 
    3246      161024 :             FOR( stage = N_STAGE_VQ - 3; stage < N_STAGE_VQ; stage++ )
    3247             :             {
    3248      120768 :                 fDist_fx = L_add( fDist_fx, BcTcvq_FixSearch_fx( X_fx, TCVQ_CB_SUB3_fx, fCodeword, quant_fx, FixBranch_tbl, stage, inis, i, &prev_state, W_fx, IntraCoeff_fx ) );
    3249             :             }
    3250       40256 :             IF( LT_32( fDist_fx, minDist_fx ) )
    3251             :             {
    3252       21371 :                 minDist_fx = L_add( fDist_fx, 0 );
    3253       21371 :                 blockDist_fx[state] = minDist_fx;
    3254       21371 :                 move32();
    3255             : 
    3256       21371 :                 fState[state] = fins;
    3257       21371 :                 move16();
    3258             : 
    3259       21371 :                 blockCodeword[state][0] = fCodeword[i][0];
    3260       21371 :                 move16();
    3261       21371 :                 blockCodeword[state][1] = fCodeword[i][1];
    3262       21371 :                 move16();
    3263       21371 :                 blockCodeword[state][2] = fCodeword[i][2];
    3264       21371 :                 move16();
    3265       21371 :                 blockCodeword[state][3] = fCodeword[i][3];
    3266       21371 :                 move16();
    3267             :             }
    3268             :         }
    3269             :     }
    3270             : 
    3271             :     /* Select optimal path */
    3272         629 :     fBlock = optimalPath_fx( acumDist_fx, blockDist_fx, blockCodeword, bestCodeword, codeWord, bestState, preState );
    3273             : 
    3274             :     /* Select Quantized Value */
    3275         629 :     quantEnc_fx( y_fx, bestCodeword, TCVQ_CB_SUB1_fx, TCVQ_CB_SUB2_fx, TCVQ_CB_SUB3_fx, IntraCoeff_fx );
    3276             : 
    3277             :     /* Buid Code for Decoder */
    3278         629 :     buildCode_fx( ind, fState[fBlock], bestCodeword, bestState );
    3279             : 
    3280         629 :     return;
    3281             : }
    3282             : 
    3283         718 : static Word16 SVQ_2d_fx(
    3284             :     Word16 *x_fx,            // x2.65
    3285             :     Word16 *y_fx,            // x2.65
    3286             :     const Word16 *W_fx,      // Q8
    3287             :     const Word16 CB_fx[][8], // x2.65
    3288             :     Word16 Size )
    3289             : {
    3290             :     Word16 i, j;
    3291         718 :     Word16 index = 0;
    3292             :     Word32 distortion_fx;
    3293             :     Word32 temp_fx;
    3294             :     Word16 temp16_fx;
    3295             : 
    3296         718 :     temp_fx = L_add( MAX_32, 0 );
    3297             : 
    3298       17950 :     FOR( i = 0; i < Size; i++ )
    3299             :     {
    3300       17232 :         distortion_fx = L_deposit_l( 0 );
    3301      155088 :         FOR( j = 0; j < 8; j++ )
    3302             :         {
    3303      137856 :             temp16_fx = sub( x_fx[j], CB_fx[i][j] );
    3304      137856 :             distortion_fx = L_add( distortion_fx,
    3305      137856 :                                    L_shr( Mult_32_16( L_mult( temp16_fx, temp16_fx ), W_fx[j] ), 1 ) ); // (2*x2.65 + Q1 + Q8) - Q15 - Q1
    3306             :         }
    3307             : 
    3308       17232 :         IF( LT_32( distortion_fx, temp_fx ) )
    3309             :         {
    3310        2778 :             temp_fx = L_add( distortion_fx, 0 );
    3311        2778 :             index = i;
    3312        2778 :             move16();
    3313             :         }
    3314             :     }
    3315             : 
    3316        6462 :     FOR( i = 0; i < M / 2; i++ )
    3317             :     {
    3318        5744 :         y_fx[i] = CB_fx[index][i];
    3319        5744 :         move16();
    3320             :     }
    3321             : 
    3322         718 :     return index;
    3323             : }
    3324             : 
    3325             : 
    3326         629 : Word32 qlsf_ARSN_tcvq_Enc_16k_fx(
    3327             :     const Word16 *x_fx, /* i  : Vector to be encoded  x2.65  */
    3328             :     Word16 *y_fx,       /* o  : Quantized LSF vector  x2.65  */
    3329             :     Word16 *indice,     /* o  : Indices                      */
    3330             :     const Word16 *w_fx, /* i  : LSF Weights           Q8     */
    3331             :     const Word16 nBits, /* i  : number of bits               */
    3332             :     Word16 safety_net   /* i  : safety_net flag              */
    3333             : )
    3334             : {
    3335             :     Word16 i;
    3336             :     Word16 x_q_fx[M];
    3337             :     Word16 yy_fx[M];
    3338             :     Word16 error_svq_fx[M], error_svq_q_fx[M];
    3339             :     Word16 cl, cs;
    3340             :     Word32 temp_l;
    3341             : #ifndef ISSUE_1867_replace_overflow_libenc
    3342             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    3343             :     Flag Overflow = 0;
    3344             : #endif
    3345             : #endif
    3346         629 :     IF( EQ_16( safety_net, 1 ) )
    3347             :     {
    3348         104 :         indice[0] = 1;
    3349         104 :         move16();
    3350         104 :         BcTcvq_fx( 1, /*x, x_q, w, */ x_fx, x_q_fx, w_fx, &indice[1] );
    3351             : 
    3352         104 :         IF( GT_16( nBits, 30 ) )
    3353             :         {
    3354             :             /* SVQ  */
    3355        1003 :             FOR( i = 0; i < M; i++ )
    3356             :             {
    3357         944 :                 error_svq_fx[i] = mult_r( sub( x_fx[i], x_q_fx[i] ), scale_inv_ARSN_fx[i] );
    3358         944 :                 move16();
    3359             :             }
    3360             : 
    3361             :             /* 5bits 1st Split VQ for Residual*/
    3362          59 :             indice[10] = SVQ_2d_fx( error_svq_fx, error_svq_q_fx, w_fx, AR_SVQ_CB1_fx, 32 );
    3363             :             /* 4bits 2nd Split VQ for Residual*/
    3364          59 :             indice[11] = SVQ_2d_fx( &error_svq_fx[8], &error_svq_q_fx[8], &w_fx[8], AR_SVQ_CB2_fx, 16 );
    3365             : 
    3366        1003 :             FOR( i = 0; i < M; i++ )
    3367             :             {
    3368         944 :                 x_q_fx[i] = add( x_q_fx[i], extract_h( L_shl( L_mult0( error_svq_q_fx[i], scale_ARSN_fx[i] ), 2 ) ) );
    3369         944 :                 move16();
    3370             :             }
    3371             :         }
    3372             :     }
    3373             :     ELSE
    3374             :     {
    3375         525 :         indice[0] = 0;
    3376         525 :         move16();
    3377         525 :         BcTcvq_fx( 0, /*x, x_q, w, */ x_fx, x_q_fx, w_fx, &indice[1] );
    3378             : 
    3379         525 :         IF( GT_16( nBits, 30 ) )
    3380             :         {
    3381             :             /* SVQ */
    3382        5100 :             FOR( i = 0; i < M; i++ )
    3383             :             {
    3384        4800 :                 error_svq_fx[i] = sub( x_fx[i], x_q_fx[i] );
    3385        4800 :                 move16();
    3386             :             }
    3387             : 
    3388             :             /* 5bits 1st Split VQ for Residual*/
    3389         300 :             indice[10] = SVQ_2d_fx( error_svq_fx, error_svq_q_fx, w_fx, AR_SVQ_CB1_fx, 32 );
    3390             :             /* 4bits 2nd Split VQ for Residual*/
    3391         300 :             indice[11] = SVQ_2d_fx( &error_svq_fx[8], &error_svq_q_fx[8], &w_fx[8], AR_SVQ_CB2_fx, 16 );
    3392             : 
    3393        5100 :             FOR( i = 0; i < M; i++ )
    3394             :             {
    3395        4800 :                 x_q_fx[i] = add( x_q_fx[i], error_svq_q_fx[i] );
    3396        4800 :                 move16();
    3397             :             }
    3398             :         }
    3399             :     }
    3400             : 
    3401         629 :     cl = 0;
    3402         629 :     move16();
    3403       10693 :     FOR( i = 0; i < M; i++ )
    3404             :     {
    3405       10064 :         yy_fx[i] = sub( x_fx[i], x_q_fx[i] );
    3406       10064 :         move16();
    3407       10064 :         cl = s_max( cl, abs_s( yy_fx[i] ) );
    3408             :     }
    3409         629 :     cs = norm_s( cl );
    3410         629 :     temp_l = 0;
    3411         629 :     move16();
    3412             : 
    3413       10693 :     FOR( i = 0; i < M; i++ )
    3414             :     {
    3415       10064 :         yy_fx[i] = shl( yy_fx[i], cs );
    3416       10064 :         move16();
    3417             : #ifdef ISSUE_1867_replace_overflow_libenc
    3418       10064 :         temp_l = L_mac_sat( temp_l, mult( yy_fx[i], shl_sat( w_fx[i], 2 ) ), yy_fx[i] );
    3419             : #else
    3420             :         temp_l = L_mac_o( temp_l, mult( yy_fx[i], shl_sat( w_fx[i], 2 ) ), yy_fx[i], &Overflow );
    3421             : #endif
    3422             :     }
    3423         629 :     cs = shl( cs, 1 );
    3424         629 :     temp_l = L_shr( temp_l, cs );
    3425         629 :     temp_l = Mult_32_16( temp_l, LSF_1_OVER_256SQ ); /* Q-4 */
    3426             : 
    3427             :     /* Recover the quantized LSF */
    3428         629 :     Copy( x_q_fx, y_fx, M );
    3429             : 
    3430         629 :     return temp_l;
    3431             : }
    3432             : 
    3433        1423 : static void FFT_Mid_Interpol_16k_fx(
    3434             :     Word32 Bin_Ener_old[], /* i/o: Old 2nd FFT Bin energy (128)               Q_ener*/
    3435             :     Word32 Bin_Ener[],     /* i  : Current 2nd FFT Bin energy (128)           Q_ener*/
    3436             :     Word32 Bin_Ener_mid[]  /* o  : LP weighting filter (numerator)            Q_ener*/
    3437             : )
    3438             : {
    3439             :     Word16 i;
    3440             : #ifndef ISSUE_1867_replace_overflow_libenc
    3441             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    3442             :     Flag Overflow = 0;
    3443             : #endif
    3444             : #endif
    3445             : 
    3446      183567 :     FOR( i = 0; i < L_FFT / 2; i++ )
    3447             :     {
    3448             :         /* Interpolation */
    3449             : #ifdef ISSUE_1867_replace_overflow_libenc
    3450      182144 :         Bin_Ener_mid[i] = L_shr( L_add_sat( Bin_Ener_old[i], Bin_Ener[i] ), 1 );
    3451             : #else
    3452             :         Bin_Ener_mid[i] = L_shr( L_add_o( Bin_Ener_old[i], Bin_Ener[i], &Overflow ), 1 );
    3453             : #endif
    3454             : 
    3455             :         /* Memory update */
    3456      182144 :         Bin_Ener_old[i] = Bin_Ener[i];
    3457      182144 :         move32();
    3458             :     }
    3459             : 
    3460        1423 :     return;
    3461             : }
    3462             : 
    3463             : 
    3464             : /*========================================================================*/
    3465             : /* FUNCTION : lsf_mid_enc_fx()                                            */
    3466             : /*------------------------------------------------------------------------*/
    3467             : /* PURPOSE : Mid-frame LSF quantization                                   */
    3468             : /*------------------------------------------------------------------------*/
    3469             : /* INPUT ARGUMENTS :                                                      */
    3470             : /* _ (Word16) coder_type  : Coder type                                    */
    3471             : /* _ (Word16) bwidth      : input signal bandwidth                        */
    3472             : /* _ (Word16) int_fs      : internal (ACELP) sampling frequency           */
    3473             : /* _ (Word32) core_brate  : core bitrate                                  */
    3474             : /* _ (Word32)  ppp_mode   : PPP mode                                      */
    3475             : /* _ (Word32)  nelp_mode  : NELP mode                                     */
    3476             : /* _ (Word16[]) qlsp0     : quantized LSPs from frame beginning       Q15 */
    3477             : /* _ (Word16[]) qlsp1     : quantized LSPs from frame end             Q15 */
    3478             : /* _ (Word16[]) Bin_Ener  : per bin log energy spectrum            Q_ener */
    3479             : /* _ (Word16) Q_ener      :                                               */
    3480             : /* _ (Word16) ppp_mode    :                                               */
    3481             : /* _ (Word16) nelp_mode   :                                               */
    3482             : /*------------------------------------------------------------------------*/
    3483             : /* INPUT/OUTPUT ARGUMENTS :                                               */
    3484             : /* _ (Word16[]) lsp       : quantized LSPs                            Q15 */
    3485             : /* _ (Word16[]) Bin_Ener_old : per bin old log energy spectrum     Q_ener */
    3486             : /*------------------------------------------------------------------------*/
    3487             : /* OUTPUT ARGUMENTS :                                                     */
    3488             : /*------------------------------------------------------------------------*/
    3489             : 
    3490             : /*------------------------------------------------------------------------*/
    3491             : /* RETURN ARGUMENTS :                                                     */
    3492             : /* _ None                                                                 */
    3493             : /*========================================================================*/
    3494             : 
    3495             : 
    3496        1423 : static void lsf_mid_enc_fx(
    3497             :     BSTR_ENC_HANDLE hBstr,   /* i/o: encoder bitstream handle            */
    3498             :     Word16 nb_bits,          /* i  : number of bits                      */
    3499             :     const Word16 int_fs,     /* i  : internal (ACELP) sampling frequency*/
    3500             :     const Word16 qlsp0[],    /* i  : quantized LSPs from frame beginning*/
    3501             :     const Word16 qlsp1[],    /* i  : quantized LSPs from frame end      */
    3502             :     Word16 lsp[],            /* i/o: mid-frame LSP                      */
    3503             :     const Word16 coder_type, /* i  : coding type                        */
    3504             :     const Word16 bwidth,     /* i  : input signal bandwidth             */
    3505             :     Word32 Bin_Ener_old[],   /* i/o: per bin old log energy spectrum    */
    3506             :     Word32 Bin_Ener[],       /* i  : per bin log energy spectrum        */
    3507             :     Word16 Q_ener,           /* i  : Q value of Bin_ener                */
    3508             :     Word16 ppp_mode,
    3509             :     Word16 nelp_mode )
    3510             : {
    3511             :     Word16 lsf[M], qlsf[M], qlsf1[M], qlsf0[M], wghts[M];
    3512             :     Word32 err, err_min;
    3513        1423 :     Word16 j, k, idx, size = 0;
    3514             :     Word32 Bin_Ener_mid[L_FFT / 2];
    3515             :     Word32 L_tmp;
    3516             :     Word16 tmp, k1;
    3517        1423 :     const Word16 *ratio = NULL;
    3518             : #ifndef ISSUE_1867_replace_overflow_libenc
    3519             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    3520             :     Flag Overflow = 0;
    3521             : #endif
    3522             : #endif
    3523             : 
    3524             :     /* convert LSPs to LSFs */
    3525        1423 :     lsp2lsf_fx( lsp, lsf, M, int_fs );
    3526        1423 :     lsp2lsf_fx( qlsp0, qlsf0, M, int_fs );
    3527        1423 :     lsp2lsf_fx( qlsp1, qlsf1, M, int_fs );
    3528             : 
    3529             :     /* calculate weights */
    3530        1423 :     FFT_Mid_Interpol_16k_fx( Bin_Ener_old, &Bin_Ener[L_FFT / 2], Bin_Ener_mid );
    3531             : 
    3532             :     /* LSF weighting */
    3533        1423 :     Unified_weighting_fx( Bin_Ener_mid, Q_ener, lsf, wghts, (Word16) EQ_16( bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), int_fs, M );
    3534        1423 :     move16();
    3535             :     /* codebook selection, number of bits, size of the codebook */
    3536        1423 :     test();
    3537        1423 :     IF( ppp_mode == 0 && nelp_mode == 0 )
    3538             :     {
    3539             :         /* codebook selection */
    3540        1423 :         IF( EQ_16( coder_type, VOICED ) )
    3541             :         {
    3542         212 :             SWITCH( nb_bits )
    3543             :             {
    3544           0 :                 case 5:
    3545             :                 {
    3546           0 :                     ratio = tbl_mid_voi_wb_5b_fx;
    3547           0 :                     move16();
    3548           0 :                     BREAK;
    3549             :                 }
    3550         212 :                 case 4:
    3551             :                 {
    3552         212 :                     ratio = tbl_mid_voi_wb_4b_fx;
    3553         212 :                     move16();
    3554         212 :                     BREAK;
    3555             :                 }
    3556             :             }
    3557         212 :         }
    3558        1211 :         ELSE IF( EQ_16( coder_type, UNVOICED ) )
    3559             :         {
    3560           0 :             ratio = tbl_mid_unv_wb_5b_fx;
    3561             :         }
    3562             :         ELSE
    3563             :         {
    3564             :             /* GENERIC, TRANSITION, AUDIO and INACTIVE */
    3565        1211 :             SWITCH( nb_bits )
    3566             :             {
    3567        1203 :                 case 5:
    3568             :                 {
    3569        1203 :                     ratio = tbl_mid_gen_wb_5b_fx;
    3570        1203 :                     move16();
    3571        1203 :                     BREAK;
    3572             :                 }
    3573           8 :                 case 2:
    3574             :                 {
    3575           8 :                     ratio = tbl_mid_gen_wb_2b_fx;
    3576           8 :                     move16();
    3577           8 :                     BREAK;
    3578             :                 }
    3579             :             }
    3580        1423 :         }
    3581             : 
    3582        1423 :         size = (Word16) pow2[nb_bits];
    3583        1423 :         move16();
    3584             :     }
    3585           0 :     ELSE IF( EQ_16( ppp_mode, 1 ) )
    3586             :     {
    3587           0 :         ratio = tbl_mid_voi_wb_1b_fx;
    3588           0 :         move16();
    3589           0 :         nb_bits = 1;
    3590           0 :         move16();
    3591           0 :         size = 2;
    3592           0 :         move16();
    3593             :     }
    3594           0 :     ELSE IF( EQ_16( nelp_mode, 1 ) )
    3595             :     {
    3596           0 :         ratio = tbl_mid_unv_wb_4b_fx;
    3597           0 :         move16();
    3598           0 :         nb_bits = 4;
    3599           0 :         move16();
    3600           0 :         size = 16;
    3601           0 :         move16();
    3602             :     }
    3603             : 
    3604             :     /* loop over codevectors */
    3605        1423 :     err_min = MAXINT32;
    3606        1423 :     move16();
    3607        1423 :     idx = 0;
    3608        1423 :     move16();
    3609        1423 :     k1 = 0;
    3610        1423 :     move16();
    3611       43343 :     FOR( k = 0; k < size; k++ )
    3612             :     {
    3613       41920 :         err = L_deposit_l( 0 );
    3614             : 
    3615      712640 :         FOR( j = 0; j < M; j++ )
    3616             :         {
    3617             :             /*      qlsf[j] = (1.0f - ratio[k*M+j]) * qlsf0[j] + ratio[k*M+j] * qlsf1[j]; */
    3618      670720 :             L_tmp = L_mult( sub( 0x2000, ratio[k1 + j] ), qlsf0[j] );
    3619      670720 :             L_tmp = L_mac( L_tmp, ratio[k1 + j], qlsf1[j] );
    3620      670720 :             qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
    3621             : 
    3622      670720 :             test();
    3623      670720 :             test();
    3624      670720 :             IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
    3625             :             {
    3626        6245 :                 qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
    3627        6245 :                 move16();
    3628             :             }
    3629             : 
    3630      670720 :             tmp = sub( lsf[j], qlsf[j] );
    3631             :             /*        err +=  wghts[j] * ftemp * ftemp; */
    3632             :             /* tmp is usually very small, we can have some extra precision with very rare saturation */
    3633      670720 :             tmp = shl_sat( tmp, 4 );
    3634             : #ifdef ISSUE_1867_replace_overflow_libenc
    3635      670720 :             tmp = mult_r_sat( tmp, tmp );
    3636      670720 :             err = L_mac_sat( err, tmp, shl_sat( wghts[j], 2 ) );
    3637             : #else
    3638             :             tmp = mult_ro( tmp, tmp, &Overflow );
    3639             :             err = L_mac_o( err, tmp, shl_sat( wghts[j], 2 ), &Overflow );
    3640             : #endif
    3641             :         }
    3642             :         /*    err = L_shl(err,Wscale); */
    3643       41920 :         err = Mult_32_16( err, LSF_1_OVER_256SQ );
    3644             :         /*    err = Mult_32_16(err,Wmult); */
    3645             : 
    3646       41920 :         IF( LT_32( err, err_min ) )
    3647             :         {
    3648        5468 :             err_min = L_add( err, 0 );
    3649        5468 :             idx = k;
    3650        5468 :             move16();
    3651             :         }
    3652       41920 :         k1 += M;
    3653       41920 :         move16();
    3654             :     }
    3655             : 
    3656             :     /* calculate the quantized LSF vector */
    3657       24191 :     FOR( j = 0; j < M; j++ )
    3658             :     {
    3659             :         /*     qlsf[j] = (1.0f - ratio[idx*M+j]) * qlsf0[j] + ratio[idx*M+j] * qlsf1[j]; */
    3660       22768 :         L_tmp = L_mult( sub( 0x2000, ratio[idx * M + j] ), qlsf0[j] );
    3661       22768 :         L_tmp = L_mac( L_tmp, ratio[idx * M + j], qlsf1[j] );
    3662       22768 :         qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
    3663             : 
    3664       22768 :         test();
    3665       22768 :         test();
    3666       22768 :         IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
    3667             :         {
    3668         232 :             qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
    3669         232 :             move16();
    3670             :         }
    3671             :     }
    3672             : 
    3673        1423 :     reorder_lsf_fx( qlsf, LSF_GAP_MID_FX, M, int_fs );
    3674             : 
    3675             :     /* convert LSFs back to LSPs */
    3676        1423 :     lsf2lsp_fx( qlsf, lsp, M, int_fs );
    3677        1423 :     push_indice( hBstr, IND_MID_FRAME_LSF_INDEX, idx, nb_bits );
    3678             : 
    3679        1423 :     return;
    3680             : }
    3681             : 
    3682      156600 : static void lsf_mid_enc_ivas_fx(
    3683             :     BSTR_ENC_HANDLE hBstr,   /* i/o: encoder bitstream handle            */
    3684             :     Word16 nb_bits,          /* i  : number of bits                      */
    3685             :     const Word32 int_fs,     /* i  : internal (ACELP) sampling frequency*/
    3686             :     const Word16 qlsp0[],    /* i  : quantized LSPs from frame beginning Q15*/
    3687             :     const Word16 qlsp1[],    /* i  : quantized LSPs from frame end      Q15*/
    3688             :     Word16 lsp[],            /* i/o: mid-frame LSP                      Q15*/
    3689             :     const Word16 coder_type, /* i  : coding type                        */
    3690             :     const Word16 bwidth,     /* i  : input signal bandwidth             */
    3691             :     Word32 Bin_Ener[],       /* i  : per bin log energy spectrum        Q_ener*/
    3692             :     Word16 Q_ener,           /* i  : Q value of Bin_ener                */
    3693             :     Word16 ppp_mode,
    3694             :     Word16 nelp_mode )
    3695             : {
    3696             :     Word16 lsf[M], qlsf[M], qlsf1[M], qlsf0[M], wghts[M];
    3697             :     Word32 err, err_min;
    3698      156600 :     Word16 j, k, idx, size = 0;
    3699             :     Word32 L_tmp;
    3700             :     Word16 tmp, k1;
    3701      156600 :     const Word16 *ratio = NULL;
    3702             : #ifndef ISSUE_1867_replace_overflow_libenc
    3703             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    3704             :     Flag Overflow = 0;
    3705             : #endif
    3706             : #endif
    3707             : 
    3708             :     /* convert LSPs to LSFs */
    3709      156600 :     lsp2lsf_fx( lsp, lsf, M, int_fs );
    3710      156600 :     lsp2lsf_fx( qlsp0, qlsf0, M, int_fs );
    3711      156600 :     lsp2lsf_fx( qlsp1, qlsf1, M, int_fs );
    3712             : 
    3713             :     /* LSF weighting */
    3714      156600 :     Unified_weighting_fx( Bin_Ener, Q_ener, lsf, wghts, (Word16) EQ_16( bwidth, NB ), (Word16) EQ_16( coder_type, UNVOICED ), int_fs, M );
    3715      156600 :     move16();
    3716             :     /* codebook selection, number of bits, size of the codebook */
    3717      156600 :     test();
    3718      156600 :     IF( ppp_mode == 0 && nelp_mode == 0 )
    3719             :     {
    3720             :         /* codebook selection */
    3721      156600 :         IF( EQ_16( coder_type, VOICED ) )
    3722             :         {
    3723        8685 :             SWITCH( nb_bits )
    3724             :             {
    3725        1709 :                 case 5:
    3726             :                 {
    3727        1709 :                     ratio = tbl_mid_voi_wb_5b_fx; // Q13
    3728        1709 :                     move16();
    3729        1709 :                     BREAK;
    3730             :                 }
    3731        6976 :                 case 4:
    3732             :                 {
    3733        6976 :                     ratio = tbl_mid_voi_wb_4b_fx; // Q13
    3734        6976 :                     move16();
    3735        6976 :                     BREAK;
    3736             :                 }
    3737           0 :                 case 1:
    3738             :                 {
    3739           0 :                     ratio = tbl_mid_voi_wb_1b_fx; // Q13
    3740           0 :                     move16();
    3741           0 :                     BREAK;
    3742             :                 }
    3743             :             }
    3744        8685 :         }
    3745      147915 :         ELSE IF( EQ_16( coder_type, UNVOICED ) )
    3746             :         {
    3747        2041 :             ratio = tbl_mid_unv_wb_5b_fx; // Q13
    3748             :         }
    3749             :         ELSE
    3750             :         {
    3751             :             /* GENERIC, TRANSITION, AUDIO and INACTIVE */
    3752      145874 :             SWITCH( nb_bits )
    3753             :             {
    3754      136693 :                 case 5:
    3755             :                 {
    3756      136693 :                     ratio = tbl_mid_gen_wb_5b_fx; // Q13
    3757      136693 :                     move16();
    3758      136693 :                     BREAK;
    3759             :                 }
    3760         734 :                 case 4:
    3761             :                 {
    3762         734 :                     ratio = tbl_mid_gen_wb_4b_fx; // Q13
    3763         734 :                     move16();
    3764         734 :                     BREAK;
    3765             :                 }
    3766        8447 :                 case 2:
    3767             :                 {
    3768        8447 :                     ratio = tbl_mid_gen_wb_2b_fx; // Q13
    3769        8447 :                     move16();
    3770        8447 :                     BREAK;
    3771             :                 }
    3772             :             }
    3773      156600 :         }
    3774             : 
    3775      156600 :         size = (Word16) pow2[nb_bits];
    3776      156600 :         move16();
    3777             :     }
    3778           0 :     ELSE IF( EQ_16( ppp_mode, 1 ) )
    3779             :     {
    3780           0 :         ratio = tbl_mid_voi_wb_1b_fx; // Q13
    3781           0 :         move16();
    3782           0 :         nb_bits = 1;
    3783           0 :         move16();
    3784           0 :         size = 2;
    3785           0 :         move16();
    3786             :     }
    3787           0 :     ELSE IF( EQ_16( nelp_mode, 1 ) )
    3788             :     {
    3789           0 :         ratio = tbl_mid_unv_wb_4b_fx; // Q13
    3790           0 :         move16();
    3791           0 :         nb_bits = 4;
    3792           0 :         move16();
    3793           0 :         size = 16;
    3794           0 :         move16();
    3795             :     }
    3796             : 
    3797             :     /* loop over codevectors */
    3798      156600 :     err_min = MAXINT32;
    3799      156600 :     move16();
    3800      156600 :     idx = 0;
    3801      156600 :     move16();
    3802      156600 :     k1 = 0;
    3803      156600 :     move16();
    3804     4807924 :     FOR( k = 0; k < size; k++ )
    3805             :     {
    3806     4651324 :         err = L_deposit_l( 0 );
    3807             : 
    3808    79072508 :         FOR( j = 0; j < M; j++ )
    3809             :         {
    3810             :             /*      qlsf[j] = (1.0f - ratio[k*M+j]) * qlsf0[j] + ratio[k*M+j] * qlsf1[j]; */
    3811    74421184 :             L_tmp = L_mult( sub( 0x2000 /*1.Q13*/, ratio[k1 + j] ), qlsf0[j] );
    3812    74421184 :             L_tmp = L_mac( L_tmp, ratio[k1 + j], qlsf1[j] );
    3813    74421184 :             qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
    3814             : 
    3815    74421184 :             test();
    3816    74421184 :             test();
    3817    74421184 :             IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
    3818             :             {
    3819      452461 :                 qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
    3820      452461 :                 move16();
    3821             :             }
    3822             : 
    3823    74421184 :             tmp = sub( lsf[j], qlsf[j] );
    3824             :             /*        err +=  wghts[j] * ftemp * ftemp; */
    3825             :             /* tmp is usually very small, we can have some extra precision with very rare saturation */
    3826    74421184 :             tmp = shl_sat( tmp, 4 );
    3827             : #ifdef ISSUE_1867_replace_overflow_libenc
    3828    74421184 :             tmp = mult_r_sat( tmp, tmp );
    3829    74421184 :             err = L_mac_sat( err, tmp, shl_sat( wghts[j], 2 ) );
    3830             : #else
    3831             :             tmp = mult_ro( tmp, tmp, &Overflow );
    3832             :             err = L_mac_o( err, tmp, shl_sat( wghts[j], 2 ), &Overflow );
    3833             : #endif
    3834             :         }
    3835             :         /*    err = L_shl(err,Wscale); */
    3836     4651324 :         err = Mult_32_16( err, LSF_1_OVER_256SQ );
    3837             :         /*    err = Mult_32_16(err,Wmult); */
    3838             : 
    3839     4651324 :         IF( LT_32( err, err_min ) )
    3840             :         {
    3841      611147 :             err_min = L_add( err, 0 );
    3842      611147 :             idx = k;
    3843      611147 :             move16();
    3844             :         }
    3845     4651324 :         k1 += M;
    3846     4651324 :         move16();
    3847             :     }
    3848             : 
    3849             :     /* calculate the quantized LSF vector */
    3850     2662200 :     FOR( j = 0; j < M; j++ )
    3851             :     {
    3852             :         /*     qlsf[j] = (1.0f - ratio[idx*M+j]) * qlsf0[j] + ratio[idx*M+j] * qlsf1[j]; */
    3853     2505600 :         L_tmp = L_mult( sub( 0x2000 /*1.Q13*/, ratio[idx * M + j] ), qlsf0[j] );
    3854     2505600 :         L_tmp = L_mac( L_tmp, ratio[idx * M + j], qlsf1[j] );
    3855     2505600 :         qlsf[j] = round_fx( L_shl( L_tmp, 2 ) );
    3856             : 
    3857     2505600 :         test();
    3858     2505600 :         test();
    3859     2505600 :         IF( j > 0 && LT_16( j, M ) && LT_16( qlsf[j], add( qlsf[j - 1], LSF_GAP_MID_FX ) ) )
    3860             :         {
    3861       13947 :             qlsf[j] = add( qlsf[j - 1], LSF_GAP_MID_FX );
    3862       13947 :             move16();
    3863             :         }
    3864             :     }
    3865             : 
    3866      156600 :     reorder_lsf_fx( qlsf, LSF_GAP_MID_FX, M, int_fs );
    3867             : 
    3868             :     /* convert LSFs back to LSPs */
    3869      156600 :     lsf2lsp_fx( qlsf, lsp, M, int_fs );
    3870      156600 :     push_indice( hBstr, IND_MID_FRAME_LSF_INDEX, idx, nb_bits );
    3871             : 
    3872      156600 :     return;
    3873             : }

Generated by: LCOV version 1.14