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

Generated by: LCOV version 1.14