LCOV - code coverage report
Current view: top level - lib_enc - lsf_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 676 710 95.2 %
Date: 2025-05-23 08:37:30 Functions: 17 17 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #include <assert.h>
      42             : #endif
      43             : #include "cnst.h"
      44             : #include "rom_enc.h"
      45             : #include "rom_com.h"
      46             : #include "prot.h"
      47             : #include "basop_proto_func.h"
      48             : #include "ivas_prot.h"
      49             : #include "ivas_rom_com.h"
      50             : #include "wmc_auto.h"
      51             : 
      52             : /*-----------------------------------------------------------------*
      53             :  * Local constants
      54             :  *-----------------------------------------------------------------*/
      55             : 
      56             : #define MSVQ_MAXCNT 3000
      57             : 
      58             : 
      59             : /*---------------------------------------------------------------------*
      60             :  *  Local function prototypes
      61             :  *---------------------------------------------------------------------*/
      62             : 
      63             : static void lsfq_CNG( BSTR_ENC_HANDLE hBstr, const float *lsf, const float *wghts, float *qlsf );
      64             : 
      65             : static void lsf_mid_enc( BSTR_ENC_HANDLE hBstr, int16_t nb_bits, const int32_t int_fs, const float qisp0[], const float qisp1[], float isp[], const int16_t coder_type, const int16_t bwidth, const float Bin_Ener[], const int16_t ppp_mode, const int16_t nelp_mode );
      66             : 
      67             : static float vq_lvq_lsf_enc( const int16_t pred_flag, const int16_t mode, const float u[], const int16_t *levels, const int16_t stages, const float w[], int16_t Idx[], const float *lsf, const float *pred, float *resq, float *lsfq );
      68             : 
      69             : static float qlsf_ARSN_tcvq_Enc_16k( const float *x, float *y, int16_t *indice, const float *w, const int16_t nBits, const int16_t safety_net );
      70             : 
      71             : 
      72             : /*-------------------------------------------------------------------*
      73             :  * lsf_enc()
      74             :  *
      75             :  * Quantization of LSF vector
      76             :  *-------------------------------------------------------------------*/
      77             : 
      78      158089 : void lsf_enc(
      79             :     Encoder_State *st,               /* i/o: state structure                      */
      80             :     float *lsf_new,                  /* o  : quantized LSF vector                 */
      81             :     float *lsp_new,                  /* i/o: LSP vector to quantize/quantized     */
      82             :     float *lsp_mid,                  /* i/o: mid-frame LSP vector                 */
      83             :     float *Aq,                       /* o  : quantized A(z) for 4 subframes       */
      84             :     const int16_t tdm_low_rate_mode, /* i  : secondary channel low rate mode flag */
      85             :     const int16_t GSC_IVAS_mode,     /* i  : GSC IVAS mode                        */
      86             :     const float tdm_lsfQ_PCh[M]      /* i  : Q LSFs for primary channel           */
      87             : )
      88             : {
      89             :     int16_t i, nBits, force_sf, no_param_lpc;
      90             :     float fec_lsf[M], stab;
      91             :     int16_t param_lpc[NPRM_LPC_NEW];
      92             :     int16_t coder_type, ppp_mode, nelp_mode;
      93             : 
      94      158089 :     if ( st->core_brate == SID_2k40 || st->core_brate == SID_1k75 )
      95             :     {
      96         433 :         coder_type = INACTIVE;
      97             :     }
      98             :     else
      99             :     {
     100      157656 :         coder_type = st->coder_type;
     101             :     }
     102             : 
     103      158089 :     if ( coder_type == AUDIO && GSC_IVAS_mode > 0 )
     104             :     {
     105        4851 :         coder_type = GENERIC;
     106             :     }
     107             : 
     108             : 
     109             :     /* initialize */
     110      158089 :     no_param_lpc = 0;
     111             : 
     112      158089 :     if ( st->Opt_SC_VBR )
     113             :     {
     114           0 :         ppp_mode = st->hSC_VBR->ppp_mode;
     115           0 :         nelp_mode = st->hSC_VBR->nelp_mode;
     116             :     }
     117             :     else
     118             :     {
     119      158089 :         ppp_mode = 0;
     120      158089 :         nelp_mode = 0;
     121             :     }
     122             : 
     123             :     /* convert LSPs to LSFs */
     124      158089 :     lsp2lsf( lsp_new, lsf_new, M, st->sr_core );
     125             : 
     126             :     /* check resonance for pitch clipping algorithm */
     127      158089 :     gp_clip_test_lsf( st->element_mode, st->core_brate, lsf_new, st->clip_var, 0 );
     128             : 
     129             :     /*-------------------------------------------------------------------------------------*
     130             :      * Find the number of bits for LSF quantization
     131             :      *-------------------------------------------------------------------------------------*/
     132             : 
     133      158089 :     nBits = 0;
     134      158089 :     if ( st->core_brate == SID_2k40 )
     135             :     {
     136         433 :         nBits = LSF_BITS_CNG;
     137             :     }
     138             :     else
     139             :     {
     140      157656 :         if ( nelp_mode == 0 && ppp_mode == 0 )
     141             :         {
     142      157656 :             nBits = st->acelp_cfg.lsf_bits;
     143             :         }
     144           0 :         else if ( nelp_mode == 1 )
     145             :         {
     146           0 :             if ( st->bwidth == NB )
     147             :             {
     148           0 :                 nBits = 32; /* Stole 1 bit for SID/NELP harmonization*/
     149             :             }
     150           0 :             else if ( st->bwidth == WB )
     151             :             {
     152           0 :                 nBits = 30; /* Stole 1 bit for SID/NELP harmonization */
     153             :             }
     154             :         }
     155           0 :         else if ( ppp_mode == 1 )
     156             :         {
     157             : 
     158             :             /*The LSF bit scaling does not work here. */
     159           0 :             nBits = 26; /*Stole 1 bit for SID PPP harmonization*/
     160             :         }
     161             :     }
     162             : 
     163             : 
     164      158089 :     force_sf = 0;
     165      158089 :     if ( st->Nb_ACELP_frames < 3 && st->core_brate != SID_2k40 )
     166             :     {
     167             :         /* first three ACELP frames after an HQ frame shall be processed only with safety-net quantizer */
     168       18885 :         force_sf = 1;
     169             :     }
     170             : 
     171      158089 :     if ( st->next_force_safety_net == 1 )
     172             :     {
     173             :         /* in case of unstable filter, choose safety-net to help FEC */
     174       14277 :         force_sf = 1;
     175       14277 :         st->next_force_safety_net = 0;
     176             :     }
     177             : 
     178             :     /*-------------------------------------------------------------------------------------*
     179             :      * LSF quantization
     180             :      *-------------------------------------------------------------------------------------*/
     181             : 
     182      158089 :     lsf_end_enc( st, lsf_new, lsf_new, nBits, coder_type, force_sf, param_lpc, &no_param_lpc, NULL, st->coder_type_raw, tdm_lsfQ_PCh );
     183             : 
     184             :     /* convert quantized LSFs back to LSPs */
     185      158089 :     lsf2lsp( lsf_new, lsp_new, M, st->sr_core );
     186             : 
     187      158089 :     if ( st->last_core == HQ_CORE && st->core == ACELP_CORE )
     188             :     {
     189             :         /* don't use old LSF values if this is the first ACELP frame after HQ frames */
     190         258 :         mvr2r( lsf_new, st->lsf_old, M );
     191             :     }
     192             : 
     193             :     /* set seed_acelp used in UC mode */
     194      158089 :     if ( coder_type == UNVOICED && st->element_mode > EVS_MONO )
     195             :     {
     196        2056 :         st->seed_acelp = 0;
     197        2056 :         for ( i = no_param_lpc - 1; i >= 0; i-- )
     198             :         {
     199             :             /* rightshift before *seed_acelp+param_lpc[i] to avoid overflows*/
     200           0 :             st->seed_acelp = (int16_t) ( ( ( ( st->seed_acelp ) >> 1 ) + param_lpc[i] ) * 31821L + 13849L );
     201             :         }
     202             :     }
     203             : 
     204      158089 :     if ( st->core_brate == SID_2k40 )
     205             :     {
     206             :         /* return if SID frame (conversion to A(z) done in the calling function) */
     207         433 :         return;
     208             :     }
     209             : 
     210             :     /*-------------------------------------------------------------------------------------*
     211             :      * FEC - enforce safety-net in the next frame in case of unstable filter
     212             :      *-------------------------------------------------------------------------------------*/
     213             : 
     214      157656 :     if ( st->last_L_frame != st->L_frame )
     215             :     {
     216             :         /* FEC - in case of core switching, use old LSFs that have been smoothed with adaptive mean */
     217        1774 :         mvr2r( st->lsf_old, st->lsfoldbfi1, M );
     218        1774 :         mvr2r( st->lsf_old, st->lsfoldbfi0, M );
     219        1774 :         mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
     220             :     }
     221             : 
     222      157656 :     FEC_lsf_estim_enc( st, fec_lsf );
     223             : 
     224             :     /* FEC - calculate LSF stability */
     225      157656 :     stab = lsf_stab( lsf_new, fec_lsf, 0, st->L_frame );
     226             : 
     227      157656 :     if ( st->L_frame == L_FRAME16k && stab < STAB_FAC_LIMIT && coder_type == GENERIC )
     228             :     {
     229        9885 :         st->next_force_safety_net = 1;
     230             :     }
     231             :     else
     232             :     {
     233      147771 :         if ( stab < STAB_FAC_LIMIT && ( st->clas == VOICED_CLAS || ( st->clas < VOICED_CLAS && coder_type == AUDIO ) ) )
     234             :         {
     235        4687 :             st->next_force_safety_net = 1;
     236             :         }
     237             :     }
     238             : 
     239             :     /* FEC - update adaptive LSF mean vector */
     240     2680152 :     for ( i = 0; i < M; i++ )
     241             :     {
     242     2522496 :         st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsf_new[i] ) / 3;
     243             :     }
     244             : 
     245             :     /* FEC - update LSF memories */
     246      157656 :     mvr2r( st->lsfoldbfi0, st->lsfoldbfi1, M );
     247      157656 :     mvr2r( lsf_new, st->lsfoldbfi0, M );
     248             : 
     249             :     /*-------------------------------------------------------------------------------------*
     250             :      * Mid-frame LSF encoding
     251             :      * LSP interpolation and conversion of LSPs to A(z)
     252             :      *-------------------------------------------------------------------------------------*/
     253             : 
     254      157656 :     if ( st->rate_switching_reset )
     255             :     {
     256             :         /*extrapolation in case of unstable LSF convert*/
     257           0 :         mvr2r( lsp_new, st->lsp_old, M );
     258           0 :         mvr2r( lsf_new, st->lsf_old, M );
     259             :     }
     260             : 
     261             :     /* Mid-frame LSF encoding */
     262      157656 :     lsf_mid_enc( st->hBstr, st->acelp_cfg.mid_lsf_bits, st->sr_core, st->lsp_old, lsp_new, lsp_mid, coder_type, st->bwidth, st->Bin_E_old, ppp_mode, nelp_mode );
     263             : 
     264      157656 :     if ( st->last_core == HQ_CORE && st->core == ACELP_CORE )
     265             :     {
     266             :         /* don't use old LSP/LSF values if this is the first ACELP frame after HQ frames */
     267         258 :         mvr2r( lsp_mid, st->lsp_old, M );
     268         258 :         lsp2lsf( lsp_mid, st->lsf_old, M, st->sr_core );
     269             :     }
     270             : 
     271      157656 :     if ( tdm_low_rate_mode == 1 && coder_type > UNVOICED )
     272             :     {
     273           0 :         if ( st->active_cnt == 1 )
     274             :         {
     275           0 :             mvr2r( lsp_mid, st->lsp_old, M );
     276           0 :             lsp2lsf( lsp_mid, st->lsf_old, M, st->sr_core );
     277           0 :             mvr2r( lsp_new, lsp_mid, M );
     278             :         }
     279             : 
     280             :         /* LSP interpolation and conversion of LSPs to A(z) - two-subframe mode  */
     281           0 :         int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, -2 );
     282             :     }
     283             :     else
     284             :     {
     285             :         /* LSP interpolation and conversion of LSPs to A(z) */
     286      157656 :         int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, 0 );
     287             :     }
     288             : 
     289             :     /*------------------------------------------------------------------*
     290             :      * Check LSF stability (distance between old LSFs and current LSFs)
     291             :      *------------------------------------------------------------------*/
     292             : 
     293      157656 :     if ( st->core_brate != SID_2k40 )
     294             :     {
     295      157656 :         st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
     296             :     }
     297             : 
     298      157656 :     return;
     299             : }
     300             : 
     301             : 
     302             : /*-------------------------------------------------------------------*
     303             :  * lsfq_CNG()
     304             :  *
     305             :  * LSF quantizer for SID frames (uses 29 bits, 4 for VQ, 25 for LVQ)
     306             :  * Note:
     307             :  * The sampling frequency of the LP-CNG frame can be determined by checking the value of the highest order LSF
     308             :  * coefficient (last coefficient of lsf). If the last LSF coefficient (lsf[M-1]) is larger than 6350
     309             :  * the decoded frame is WB2 with sampling rate of 16 kHz, otherwise it is sampled at 12.8kHz and contains
     310             :  * either NB or WB LSF data.
     311             :  *-------------------------------------------------------------------*/
     312             : 
     313         433 : static void lsfq_CNG(
     314             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle    */
     315             :     const float *lsf,
     316             :     const float *wghts,
     317             :     float *qlsf_out )
     318             : {
     319             :     int16_t i, j, idx_cv, idx_lvq[3];
     320             :     float min_dist, dist, dd[M], ddq[M];
     321             :     const float *p_cb;
     322             :     int16_t first_cb, last_cb;
     323             :     int16_t idx_lead_cng[2], idx_scale_cng[2];
     324             : 
     325             :     float qlsf[M];
     326         433 :     idx_cv = 0;
     327             : 
     328             :     /* quantize first stage with 4 bits */
     329         433 :     if ( lsf[M - 1] > WB_LIMIT_LSF ) /* 16kHz sampled LSF vector*/
     330             :     {
     331         277 :         p_cb = &CNG_SN1[0];
     332         277 :         first_cb = 0;
     333         277 :         last_cb = 6;
     334             :     }
     335             :     else /* 12.8kHz sampled LSF vector*/
     336             :     {
     337         156 :         p_cb = &CNG_SN1[6 * M];
     338         156 :         first_cb = 6;
     339         156 :         last_cb = M;
     340             :     }
     341             : 
     342         433 :     min_dist = 1.0e30f;
     343        3655 :     for ( i = first_cb; i < last_cb; i++ )
     344             :     {
     345        3222 :         dist = 0.0f;
     346       54774 :         for ( j = 0; j < M; j++ )
     347             :         {
     348       51552 :             dist += wghts[j] * ( *p_cb ) * ( *p_cb - 2 * lsf[j] );
     349       51552 :             p_cb++;
     350             :         }
     351             : 
     352        3222 :         if ( dist < min_dist )
     353             :         {
     354        1149 :             min_dist = dist;
     355        1149 :             idx_cv = i;
     356             :         }
     357             :     }
     358             : 
     359             :     /* calculate difference */
     360        7361 :     for ( i = 0; i < M; i++ )
     361             :     {
     362        6928 :         dd[i] = lsf[i] - CNG_SN1[idx_cv * M + i];
     363             :     }
     364             : 
     365             :     /* quantize the difference with LVQ */
     366         433 :     mslvq_cng( idx_cv, dd, qlsf, ddq, idx_lead_cng, idx_scale_cng, wghts );
     367             : 
     368         433 :     index_lvq( ddq, idx_lead_cng, idx_scale_cng, START_CNG + idx_cv, idx_lvq, 0 );
     369             : 
     370         433 :     v_add( qlsf, &CNG_SN1[idx_cv * M], qlsf, M );
     371             :     /* write the VQ index to the bitstream */
     372         433 :     push_indice( hBstr, IND_ISF_0_0, idx_cv, 4 );
     373             : 
     374             :     /* write the LVQ index to the bitstream */
     375         433 :     push_indice( hBstr, IND_ISF_0_1, idx_lvq[0], LEN_INDICE );
     376         433 :     push_indice( hBstr, IND_ISF_0_1, idx_lvq[1], LSF_BITS_CNG - 4 - LEN_INDICE );
     377             : 
     378         433 :     mvr2r( qlsf, qlsf_out, M );
     379             : 
     380         433 :     return;
     381             : }
     382             : 
     383             : /*-------------------------------------------------------------------*
     384             :  * qlsf_Mode_Select()
     385             :  *
     386             :  * Mode selection for LSF quantizer
     387             :  *-------------------------------------------------------------------*/
     388             : 
     389         574 : static int16_t qlsf_Mode_Select(
     390             :     const float *lsf,        /* i  : LSF vector               */
     391             :     const float *w,          /* i  : weighting vector         */
     392             :     const float *pred1,      /* i  : prediction vector        */
     393             :     const float streaklimit, /* i  : predictive streak limit  */
     394             :     const float op_loop_thr  /* i  : Open-loop Threshold      */
     395             : )
     396             : {
     397             :     int16_t i;
     398             :     int16_t safety_net;
     399             :     float pred_pow2[M], En;
     400             : 
     401             :     /* calculate the prediction residual */
     402        9758 :     for ( i = 0; i < M; i++ )
     403             :     {
     404        9184 :         pred_pow2[i] = lsf[i] - pred1[i];
     405             :     }
     406             : 
     407             :     /* calculate its energy */
     408         574 :     v_mult( pred_pow2, pred_pow2, pred_pow2, M );
     409         574 :     En = dotp( pred_pow2, w, M );
     410             : 
     411             :     /* choose the mode */
     412         574 :     if ( En > streaklimit * op_loop_thr )
     413             :     {
     414             :         /* Safety-net */
     415          47 :         safety_net = 1;
     416             :     }
     417             :     else
     418             :     {
     419             :         /* Predictive */
     420         527 :         safety_net = 0;
     421             :     }
     422             : 
     423         574 :     return safety_net;
     424             : }
     425             : 
     426             : 
     427             : /*-------------------------------------------------------------------*
     428             :  * lsf_end_enc()
     429             :  *
     430             :  * Quantization of LSF parameters
     431             :  *-------------------------------------------------------------------*/
     432             : 
     433      270196 : void lsf_end_enc(
     434             :     Encoder_State *st,            /* i/o: encoder state structure                                */
     435             :     const float *lsf,             /* i  : LSF in the frequency domain (0..6400)                  */
     436             :     float *qlsf,                  /* o  : quantized LSF                                          */
     437             :     const int16_t nBits_in,       /* i  : number of bits to spend on ISF quantization            */
     438             :     const int16_t coder_type_org, /* i  : coding type                                            */
     439             :     const int16_t force_sf,       /* i  : Force safety-net usage (if possible), due to filter instability, MDCT-core switching etc. */
     440             :     int16_t *lpc_param,
     441             :     int16_t *no_indices,
     442             :     int16_t *bits_param_lpc,
     443             :     const int16_t coder_type_raw,
     444             :     const float tdm_lsfQ_PCh[M] /* i  : Q LSFs for primary channel          */
     445             : )
     446             : {
     447             :     int16_t i;
     448             :     int16_t Idx0[MAX_VQ_STAGES + 3];   /* Optimal codebook indices for safety-net quantizer                 */
     449             :     int16_t Idx1[MAX_VQ_STAGES + 3];   /* Optimal codebook indices for predictive quantizer                 */
     450             :     int16_t indice[MAX_VQ_STAGES + 3]; /* Temp. array of indice for vector de-quantizer                     */
     451      270196 :     int16_t mode_lvq = 0, mode_lvq_p = 0;
     452             :     int16_t bits0[MAX_VQ_STAGES], bits1[MAX_VQ_STAGES];
     453      270196 :     const int16_t *Bit_alloc1 = NULL;
     454             :     float Err[2];                   /* Quantization error for safety-net(0) and predictive(1) quantizers */
     455             :     float Tmp[M];                   /* Temporary target vector (mean and prediction removed)             */
     456             :     float pred0[M];                 /* Prediction for the safety-net quantizer (usually mean)            */
     457             :     float pred1[M];                 /* Prediction for the predictive quantizer                           */
     458             :     float pred2[M];                 /* Prediction for the predictive quantizer                           */
     459             :     float wghts[M];                 /* Weighting used for quantizer (currently GSM based)                */
     460             :     int16_t stages0;                /* Amount of stages used by safety-net quantizer                     */
     461             :     int16_t stages1;                /* Amount of stages used by predictive quantizer                     */
     462             :     int16_t levels0[MAX_VQ_STAGES]; /* Sizes of different codebook stages for safety-net quantizer       */
     463             :     int16_t levels1[MAX_VQ_STAGES]; /* Sizes of different codebook stages for predictive quantizer       */
     464             :     int16_t predmode;               /* 0: safety-net only, 1: predictive only, 2: best of the two        */
     465             :     int16_t safety_net, cumleft, num_bits;
     466             :     int16_t *Idx, stages, *bits;
     467             :     float Tmp1[M], Tmp2[M]; /* Temporary target vectors for MA and AR quantizers respectively     */
     468             :     float abs_threshold;    /* Absolute Error value that is considered as "good enough" (in practice close to SD of 1.0dB)*/
     469             :     float lsfq[M * 2], resq[M * 2];
     470             :     int16_t coder_type;
     471             :     int16_t nBits;
     472             :     int16_t TCQIdx0[M + 2]; /* Optimal codebook indices for VQ-TCQ quantizer                 */
     473             :     int16_t *TCQIdx;
     474             :     int16_t flag_1bit_gran;
     475      270196 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     476             :     float pred3[M];
     477             :     int16_t dummy, dummy_v[5];
     478             : 
     479      270196 :     flag_1bit_gran = ( st->element_mode > EVS_MONO );
     480      270196 :     nBits = nBits_in;
     481             : 
     482      270196 :     if ( coder_type_org == GENERIC && st->sr_core == INT_FS_16k && st->codec_mode == MODE1 && ( st->idchan == 0 ) /* this bit is used only for primary channel or mono */ )
     483             :     {
     484       62132 :         if ( coder_type_raw == VOICED )
     485             :         {
     486       19332 :             coder_type = VOICED; /* Reflect Inactive mode */
     487       19332 :             if ( flag_1bit_gran == 1 )
     488             :             {
     489       19026 :                 nBits--;
     490             :             }
     491             :         }
     492             :         else
     493             :         {
     494       42800 :             nBits--; /* This is for real Generic*/
     495       42800 :             coder_type = coder_type_org;
     496             :         }
     497             :     }
     498             :     else
     499             :     {
     500      208064 :         coder_type = coder_type_org;
     501             :     }
     502             : 
     503             : 
     504             :     /*--------------------------------------------------------------------------------*
     505             :      * Calculate the number of stages and levels for each stage based on allowed bit budget
     506             :      * Set absolute threshold for codebook-type decision logic
     507             :      *--------------------------------------------------------------------------------*/
     508             : 
     509      270196 :     if ( st->bwidth == NB )
     510             :     {
     511           0 :         abs_threshold = SFNETLOWLIMIT_NB;
     512             :     }
     513             :     else
     514             :     {
     515      270196 :         abs_threshold = SFNETLOWLIMIT_WB;
     516             :     }
     517             : 
     518      270196 :     Unified_weighting( &st->Bin_E[L_FFT / 2], lsf, wghts, st->bwidth == NB, coder_type == UNVOICED, st->sr_core, M );
     519             : 
     520             :     /*--------------------------------------------------------------------------------*
     521             :      * LSF quantization of SID frames
     522             :      *--------------------------------------------------------------------------------*/
     523             : 
     524      270196 :     if ( st->core_brate == SID_2k40 )
     525             :     {
     526         433 :         lsfq_CNG( st->hBstr, lsf, wghts, qlsf );
     527         433 :         v_sort( qlsf, 0, M - 1 );
     528         433 :         reorder_lsf( qlsf, MODE1_LSF_GAP, M, st->sr_core );
     529             : 
     530         433 :         return;
     531             :     }
     532             : 
     533      269763 :     find_pred_mode( &predmode, coder_type, st->bwidth, st->sr_core, &mode_lvq, &mode_lvq_p, st->total_brate );
     534             : 
     535             :     /*----------------------------------------------------------------*
     536             :      * Calculate number of stages and levels for each stage based on the allowed bit allocation
     537             :      * (subtract one bit for LSF predictor selection)
     538             :      *----------------------------------------------------------------*/
     539             : 
     540      269763 :     lsf_allocate( nBits - ( predmode >> 1 ), mode_lvq, mode_lvq_p, &stages0, &stages1, levels0, levels1, bits0, bits1 );
     541             : 
     542             :     /*--------------------------------------------------------------------------------*
     543             :      * LSF quantization of all other frames but SID frames
     544             :      * Select safety-net or predictive mode
     545             :      *--------------------------------------------------------------------------------*/
     546             : 
     547      269763 :     Err[0] = FLT_MAX;
     548      269763 :     Err[1] = FLT_MAX;
     549             :     /* for mem_MA update */
     550     4585971 :     for ( i = 0; i < M; i++ )
     551             :     {
     552     4316208 :         pred1[i] = ModeMeans[mode_lvq][i] + MU_MA * st->mem_MA[i];
     553             :     }
     554             : 
     555             :     /* TD stereo SCh: perform intra-frame prediction with pulling-to-mean */
     556      269763 :     if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
     557             :     {
     558             :         /* if secondary channel predmode is set to be > 2 */
     559         110 :         predmode += 3;
     560             : 
     561         110 :         tdm_SCh_LSF_intra_pred( st->element_brate, tdm_lsfQ_PCh, pred3 );
     562             :     }
     563             : 
     564      269763 :     if ( predmode == 0 ) /* Safety-net only */
     565             :     {
     566             :         /* Subtract only mean */
     567       14366 :         mvr2r( ModeMeans[mode_lvq], pred0, M );
     568       14366 :         v_sub( lsf, pred0, Tmp, M );
     569             : 
     570             :         /* LVQ quantization (safety-net only) */
     571       14366 :         Err[0] = vq_lvq_lsf_enc( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, resq, lsfq );
     572       14366 :         safety_net = 1;
     573       14366 :         st->pstreaklen = 0; /* Streak is ended with safety-net */
     574             :     }
     575      255397 :     else if ( predmode == 1 ) /* only MA prediction */
     576             :     {
     577      177606 :         v_sub( lsf, pred1, Tmp1, M );
     578      177606 :         Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp1, levels1, stages1, wghts, Idx1, lsf, pred1, resq, lsfq );
     579             : 
     580      177606 :         safety_net = 0;
     581             :     }
     582             :     else /* Switched Safety-Net/AR prediction */
     583             :     {
     584       77791 :         if ( predmode == 2 )
     585             :         {
     586             :             /* Subtract mean and AR prediction */
     587       77681 :             mvr2r( ModeMeans[mode_lvq], pred0, M );
     588             : 
     589             :             /* subtract only mean */
     590       77681 :             v_sub( lsf, pred0, Tmp, M );
     591             : 
     592     1320577 :             for ( i = 0; i < M; i++ )
     593             :             {
     594             :                 /* subtract mean and AR prediction */
     595     1242896 :                 pred2[i] = pred0[i] + Predictors[mode_lvq_p][i] * ( st->mem_AR[i] - pred0[i] );
     596     1242896 :                 Tmp2[i] = lsf[i] - pred2[i];
     597             :             }
     598             : 
     599             :             /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
     600             :                case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
     601       77681 :             if ( ( ( st->pstreaklen > ( STREAKLEN + 3 ) ) && ( coder_type == VOICED ) ) || ( ( st->pstreaklen > ( STREAKLEN ) ) && ( coder_type != VOICED ) ) )
     602             :             {
     603             :                 /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
     604       12039 :                 st->streaklimit *= STREAKMULT;
     605             :             }
     606             : 
     607       77681 :             if ( st->pstreaklen == 0 )
     608             :             {
     609             :                 /* reset the adaptive scaling factor */
     610       28638 :                 st->streaklimit = 1.0f;
     611             :             }
     612             : 
     613             :             /* VOICED_WB@16kHz */
     614       77681 :             if ( st->sr_core == INT_FS_16k && coder_type == VOICED && flag_1bit_gran == 0 )
     615             :             {
     616             :                 /* select safety_net or predictive in open loop*/
     617         574 :                 safety_net = qlsf_Mode_Select( lsf, wghts, pred2, st->streaklimit, OP_LOOP_THR_HVO );
     618             : 
     619         574 :                 if ( force_sf == 1 )
     620             :                 {
     621          51 :                     safety_net = 1;
     622             :                 }
     623             : 
     624         574 :                 if ( safety_net )
     625             :                 {
     626             :                     /* Safety-net - BC-TCQ quantization : SN */
     627          88 :                     Err[0] = qlsf_ARSN_tcvq_Enc_16k( Tmp, lsfq, TCQIdx0, wghts, nBits - 1, safety_net );
     628          88 :                     st->pstreaklen = 0;
     629             :                 }
     630             :                 else
     631             :                 {
     632             :                     /* predictive - BC-TCQ quantization : AR */
     633             :                     /* For consistency Err[1] contains predictive error */
     634         486 :                     Err[1] = qlsf_ARSN_tcvq_Enc_16k( Tmp2, lsfq, TCQIdx0, wghts, nBits - 1, safety_net );
     635         486 :                     ( st->pstreaklen )++;
     636             :                 }
     637             :             }
     638             :             /* all other frames (not VOICED@16kHz) */
     639             :             else
     640             :             {
     641             :                 /* safety-net */
     642       77107 :                 Err[0] = vq_lvq_lsf_enc( 0, mode_lvq, Tmp, levels0, stages0, wghts, Idx0, lsf, pred0, resq, lsfq );
     643             :                 /* Predictive quantizer is calculated only if it can be selected, this saves computation */
     644       77107 :                 if ( !force_sf || Err[0] > abs_threshold )
     645             :                 {
     646       77076 :                     Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp2, levels1, stages1, wghts, Idx1, lsf, pred2, &resq[M], &lsfq[M] );
     647             :                 }
     648             :                 /* Select whether to use safety-net or predictive LSF quantizer. The decision is based on following:
     649             :                    if the non-predictive (safety-net) quantization error (Err[0]) is low enough it is selected
     650             :                    or if the predictively quantized error (Err[1]) is by at least adaptive margin smaller than non-predictive quantizer.
     651             :                    or if the in case of frame erasure the resulting concealed predictive LSF would be unstable safety-net is selected */
     652       77107 :                 if ( force_sf || Err[0] * ( st->streaklimit ) < PREFERSFNET * Err[1] || Err[0] < abs_threshold )
     653             :                 {
     654       24084 :                     safety_net = 1;
     655       24084 :                     st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
     656             :                 }
     657             :                 else
     658             :                 {
     659       53023 :                     safety_net = 0;
     660       53023 :                     ( st->pstreaklen )++; /* Increase the consecutive predictive frame counter by one */
     661             :                 }
     662             :             }
     663             :         }
     664             :         else /* of "if (predmode==2)" */
     665             :         {
     666         110 :             mvr2r( ModeMeans[mode_lvq], pred0, M );
     667             : 
     668         110 :             if ( predmode == 4 )
     669             :             {
     670          89 :                 mode_lvq_p = 9; /* force to Generic WB with AR*/
     671             :             }
     672             : 
     673        1870 :             for ( i = 0; i < M; i++ )
     674             :             {
     675             :                 /* subtract mean and AR prediction */
     676        1760 :                 pred2[i] = pred0[i] + Predictors[mode_lvq_p][i] * ( st->mem_AR[i] - pred0[i] );
     677        1760 :                 Tmp[i] = lsf[i] - pred2[i];
     678        1760 :                 Tmp2[i] = lsf[i] - pred3[i];
     679             :             }
     680             : 
     681             :             /* Adaptive scaling factor (multiplier) is updated in order to reduce the amount of consecutive predictive frames in
     682             :                case of possible frame erasure. AR-predictive usage for VOICED mode is allowed to be higher than other modes. */
     683         110 :             if ( st->pstreaklen > ( STREAKLEN ) )
     684             :             {
     685             :                 /* update the adaptive scaling factor to become smaller with increasing number of concecutive predictive frames. */
     686           0 :                 st->streaklimit *= STREAKMULT;
     687             :             }
     688             : 
     689         110 :             if ( st->pstreaklen == 0 )
     690             :             {
     691             :                 /* reset the adaptive scaling factor */
     692          54 :                 st->streaklimit = 1.0f;
     693             :             }
     694             : 
     695             :             /* intra pred */
     696             :             /* use G AR pred for the intra mode (as a safety mode, this is why the indexes 0/1 are interchanged)*/
     697             : 
     698         110 :             lsf_allocate( nBits - 1, mode_lvq, 9, &stages1, &stages0, levels1, levels0, bits1, bits0 );
     699             : 
     700         110 :             Err[0] = vq_lvq_lsf_enc( 2, 9, Tmp2, levels0, stages0, wghts, Idx0, lsf, pred3, &resq[M], &lsfq[M] );
     701             : 
     702         110 :             if ( force_sf )
     703             :             {
     704           0 :                 safety_net = 1;     /* intra-frame prediction */
     705           0 :                 st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
     706             :             }
     707             :             else
     708             :             { /* try also the inter frame prediction */
     709             : 
     710             :                 /* AR inter-frame prediction */
     711         110 :                 lsf_allocate( nBits - 1, mode_lvq, mode_lvq_p, &dummy, &stages1, dummy_v, levels1, dummy_v, bits1 );
     712             : 
     713         110 :                 Err[1] = vq_lvq_lsf_enc( 2, mode_lvq_p, Tmp, levels1, stages1, wghts, Idx1, lsf, pred2, resq, lsfq );
     714             : 
     715         110 :                 if ( Err[0] * ( st->streaklimit ) < PREFERSFNET * Err[1] )
     716             :                 {
     717          36 :                     safety_net = 1;
     718          36 :                     st->pstreaklen = 0; /* Reset the consecutive predictive frame counter */
     719             :                 }
     720             :                 else
     721             :                 {
     722          74 :                     safety_net = 0;
     723          74 :                     ( st->pstreaklen )++; /* Increase the consecutive predictive frame counter by one */
     724             :                 }
     725             :             }
     726             :         }
     727             :     }
     728             : 
     729             :     /*--------------------------------------------------------------------------*
     730             :      * Write indices to array
     731             :      *--------------------------------------------------------------------------*/
     732             : 
     733      269763 :     if ( st->codec_mode == MODE1 && st->core == ACELP_CORE )
     734             :     {
     735             :         /* write coder_type bit for VOICED@16kHz or GENERIC@16kHz */
     736      157656 :         if ( coder_type_org == GENERIC && st->sr_core == INT_FS_16k && st->idchan == 0 )
     737             :         {
     738             :             /* VOICED =2 and GENERIC=3, so "coder_type-2" means VOICED =0 and GENERIC=1*/
     739       62132 :             push_indice( hBstr, IND_LSF_PREDICTOR_SELECT_BIT, coder_type - 2, 1 );
     740             :         }
     741             : 
     742             :         /* write predictor selection bit */
     743      157656 :         if ( predmode >= 2 )
     744             :         {
     745       40405 :             push_indice( hBstr, IND_LSF_PREDICTOR_SELECT_BIT, safety_net, 1 );
     746             :         }
     747             : 
     748      157656 :         if ( coder_type == VOICED && st->sr_core == INT_FS_16k && flag_1bit_gran == 0 )
     749             :         {
     750             :             /* BC-TCVQ (only for VOICED@16kHz) */
     751         306 :             TCQIdx = &TCQIdx0[1];
     752         306 :             Bit_alloc1 = &BC_TCVQ_BIT_ALLOC_40B[1];
     753        3672 :             for ( i = 0; i < M / 2 + 3; i++ )
     754             :             {
     755        3366 :                 push_indice( hBstr, IND_LSF, TCQIdx[i], Bit_alloc1[i] );
     756             :             }
     757             :         }
     758             :         else
     759             :         {
     760      157350 :             cumleft = nBits;
     761      157350 :             if ( predmode >= 2 )
     762             :             {
     763             :                 /* subtract predictor selection bit */
     764       40099 :                 cumleft = nBits - 1;
     765             :             }
     766             : 
     767      157350 :             if ( safety_net )
     768             :             {
     769       25862 :                 stages = stages0;
     770       25862 :                 Idx = Idx0;
     771       25862 :                 bits = bits0;
     772             :             }
     773             :             else
     774             :             {
     775      131488 :                 stages = stages1;
     776      131488 :                 Idx = Idx1;
     777      131488 :                 bits = bits1;
     778             :             }
     779             : 
     780      363277 :             for ( i = 0; i < stages - 1; i++ )
     781             :             {
     782      205927 :                 indice[i] = Idx[i];
     783      205927 :                 num_bits = bits[i];
     784      205927 :                 cumleft -= num_bits;
     785             : 
     786      205927 :                 push_indice( hBstr, IND_LSF, indice[i], num_bits );
     787             :             }
     788             : 
     789      592466 :             while ( cumleft > 0 )
     790             :             {
     791      435116 :                 indice[i] = Idx[i];
     792             : 
     793      435116 :                 if ( cumleft > LEN_INDICE )
     794             :                 {
     795      277766 :                     num_bits = LEN_INDICE;
     796             :                 }
     797             :                 else
     798             :                 {
     799      157350 :                     num_bits = cumleft;
     800             :                 }
     801             : 
     802      435116 :                 cumleft -= num_bits;
     803             : 
     804      435116 :                 push_indice( hBstr, IND_LSF, indice[i], num_bits );
     805             : 
     806      435116 :                 i++;
     807             :             }
     808             :         }
     809             :     }
     810             :     else
     811             :     {
     812      112107 :         if ( coder_type == VOICED && st->sr_core == INT_FS_16k && flag_1bit_gran == 0 )
     813             :         {
     814             :             /* BC-TCVQ (only for VOICED@16kHz) */
     815             :             /* Number of quantization indices */
     816         268 :             *no_indices = 10;
     817        2948 :             for ( i = 0; i < *no_indices; i++ )
     818             :             {
     819        2680 :                 lpc_param[i] = TCQIdx0[i];
     820        2680 :                 bits_param_lpc[i] = BC_TCVQ_BIT_ALLOC_40B[i];
     821             :             }
     822             :         }
     823             :         else
     824             :         {
     825             :             /* Number of quantization indices */
     826             : 
     827             :             /* there are 31 bits */
     828      111839 :             if ( safety_net == 1 )
     829             :             {
     830       12624 :                 Idx = Idx0;
     831       12624 :                 *no_indices = stages0 + 1;
     832       37872 :                 for ( i = 0; i < stages0; i++ )
     833             :                 {
     834       25248 :                     lpc_param[i] = Idx[i];
     835       25248 :                     indice[i] = Idx[i];
     836       25248 :                     bits_param_lpc[i] = bits0[i];
     837             :                 }
     838       12624 :                 lpc_param[stages0] = Idx[stages0];
     839       12624 :                 indice[stages0] = Idx[stages0];
     840             : 
     841       12624 :                 bits_param_lpc[stages0 - 1] = LEN_INDICE;
     842       12624 :                 bits_param_lpc[stages0] = bits0[stages0 - 1] - LEN_INDICE;
     843             :             }
     844             :             else
     845             :             {
     846       99215 :                 *no_indices = stages1 + 1;
     847       99215 :                 Idx = Idx1;
     848      273151 :                 for ( i = 0; i < stages1; i++ )
     849             :                 {
     850      173936 :                     lpc_param[i] = Idx[i];
     851      173936 :                     indice[i] = Idx[i];
     852      173936 :                     bits_param_lpc[i] = bits1[i];
     853             :                 }
     854       99215 :                 lpc_param[stages1] = Idx[stages1];
     855       99215 :                 indice[stages1] = Idx[stages1];
     856             : 
     857       99215 :                 bits_param_lpc[stages1 - 1] = LEN_INDICE;
     858       99215 :                 bits_param_lpc[stages1] = bits1[stages1 - 1] - LEN_INDICE;
     859             :             }
     860      111839 :             if ( predmode == 2 )
     861             :             {
     862      123978 :                 for ( i = *no_indices; i > 0; i-- )
     863             :                 {
     864       86860 :                     lpc_param[i] = lpc_param[i - 1];
     865       86860 :                     bits_param_lpc[i] = bits_param_lpc[i - 1];
     866             :                 }
     867       37118 :                 lpc_param[0] = safety_net; /* put the safety net info on the last param */
     868       37118 :                 bits_param_lpc[0] = 1;
     869       37118 :                 *no_indices = *no_indices + 1;
     870             :             }
     871             :         }
     872             :     }
     873             : 
     874             : 
     875             :     /*--------------------------------------------------------------------------*
     876             :      *  De-quantize encoded LSF vector
     877             :      *--------------------------------------------------------------------------*/
     878             : 
     879      269763 :     if ( safety_net )
     880             :     {
     881             :         /* Safety-net */
     882       38574 :         if ( coder_type == VOICED && st->sr_core == INT_FS_16k && flag_1bit_gran == 0 )
     883             :         {
     884             :             /* BC-TCQ */
     885          88 :             mvr2r( lsfq, st->mem_MA, M );
     886          88 :             v_add( lsfq, pred0, qlsf, M );
     887             :         }
     888             :         else
     889             :         {
     890       38486 :             if ( st->tdm_LRTD_flag == 0 && st->idchan == 1 && tdm_lsfQ_PCh != NULL )
     891             :             {
     892             :                 /* intra mode*/
     893          36 :                 vq_dec_lvq( 0, qlsf, &indice[0], stages0, M, 9, /*mode_lvq_p,*/ levels0[stages0 - 1] );
     894          36 :                 v_add( qlsf, pred3, qlsf, M );
     895          36 :                 v_sub( qlsf, pred1, st->mem_MA, M );
     896             :             }
     897             :             else
     898             :             {
     899       38450 :                 vq_dec_lvq( 1, qlsf, &indice[0], stages0, M, mode_lvq, levels0[stages0 - 1] );
     900             : 
     901       38450 :                 v_add( qlsf, pred0, qlsf, M );
     902       38450 :                 v_sub( qlsf, pred1, st->mem_MA, M );
     903             :             }
     904             :         }
     905             :     }
     906             :     else
     907             :     {
     908      231189 :         if ( coder_type == VOICED && st->sr_core == INT_FS_16k && flag_1bit_gran == 0 )
     909             :         {
     910             :             /* BC-TCVQ */
     911         486 :             mvr2r( lsfq, st->mem_MA, M );
     912         486 :             v_add( lsfq, pred2, qlsf, M );
     913             :         }
     914             :         else
     915             :         {
     916             :             /* LVQ */
     917      230703 :             vq_dec_lvq( 0, qlsf, &indice[0], stages1, M, mode_lvq_p, levels1[stages1 - 1] );
     918      230703 :             if ( predmode == 1 )
     919             :             {
     920      177606 :                 mvr2r( qlsf, st->mem_MA, M );
     921      177606 :                 v_add( qlsf, pred1, qlsf, M );
     922             :             }
     923             :             else
     924             :             {
     925       53097 :                 v_add( qlsf, pred2, qlsf, M );
     926       53097 :                 v_sub( qlsf, pred1, st->mem_MA, M );
     927             :             }
     928             :         }
     929             :     }
     930             : 
     931             :     /*--------------------------------------------------------------------------*
     932             :      * Sort the quantized vector
     933             :      * Verify stability
     934             :      * Update AR-predictor memory
     935             :      *--------------------------------------------------------------------------*/
     936             : 
     937             :     /* Sort the quantized vector to ascending order */
     938      269763 :     v_sort( qlsf, 0, M - 1 );
     939             : 
     940             :     /* Verify stability by adding minimum separation */
     941      269763 :     reorder_lsf( qlsf, MODE1_LSF_GAP, M, st->sr_core );
     942             : 
     943             :     /* Update predictor memories */
     944      269763 :     mvr2r( qlsf, st->mem_AR, M );
     945             : 
     946      269763 :     return;
     947             : }
     948             : 
     949             : 
     950             : /*-------------------------------------------------------------------*
     951             :  * first_VQstages()
     952             :  *
     953             :  *
     954             :  *-------------------------------------------------------------------*/
     955      306367 : void first_VQstages(
     956             :     const float *const *cb,
     957             :     const float u[],         /* i  : vector to be encoded (prediction and mean removed)  */
     958             :     const int16_t *levels,   /* i  : number of levels in each stage                      */
     959             :     const int16_t stagesVQ,  /* i  : number of stages                                    */
     960             :     const float w[],         /* i  : weights                                             */
     961             :     const int16_t N,         /* i  : vector dimension                                    */
     962             :     const int16_t max_inner, /* i  : maximum number of swaps in inner loop               */
     963             :     int16_t indices_VQstage[] )
     964             : {
     965             :     float ftmp, resid_buf[2 * LSFMBEST * M], *resid[2], dist_buf[2 * LSFMBEST], *dist[2];
     966             :     float en, tmp, Tmp[M], *pTmp;
     967      306367 :     int16_t *pTmp_short, idx_buf[2 * LSFMBEST * MAX_VQ_STAGES], parents[LSFMBEST], counter = 0, j, m, s, c, c2, p_max, *indices[2];
     968      306367 :     int16_t maxC = LSFMBEST;
     969             : 
     970             :     /*float dd[16];*/
     971             :     const float *cb_stage, *cbp;
     972             : 
     973             :     /* Set pointers to previous (parent) and current node (parent node is indexed [0], current node is indexed [1]) */
     974      306367 :     indices[0] = idx_buf;
     975      306367 :     indices[1] = idx_buf + maxC * stagesVQ;
     976      306367 :     resid[0] = resid_buf;
     977      306367 :     resid[1] = resid_buf + maxC * N;
     978      306367 :     dist[0] = dist_buf;
     979      306367 :     dist[1] = dist_buf + maxC;
     980             : 
     981      306367 :     set_s( idx_buf, 0, 2 * stagesVQ * maxC );
     982      306367 :     set_s( parents, 0, maxC );
     983             : 
     984             :     /* Set up inital distance vector */
     985     5174859 :     for ( tmp = 0, j = 0; j < N; j++ )
     986             :     {
     987     4868492 :         tmp += u[j] * u[j] * w[j];
     988             :     }
     989      306367 :     set_f( dist[1], tmp, maxC );
     990             : 
     991             :     /* Set up inital error (residual) vectors */
     992      306367 :     pTmp = resid[1];
     993      919101 :     for ( c = 0; c < maxC; c++ )
     994             :     {
     995      612734 :         mvr2r( u, pTmp, N );
     996      612734 :         pTmp += N;
     997             :     }
     998             : 
     999             :     /*----------------------------------------------------------------*
    1000             :      * LSF quantization
    1001             :      *----------------------------------------------------------------*/
    1002             : 
    1003             :     /* Loop over all stages */
    1004      698890 :     for ( m = 1, s = 0; s < stagesVQ; s++ )
    1005             :     {
    1006             :         /* set codebook pointer to point to first stage */
    1007      392523 :         cbp = cb[s];
    1008             : 
    1009             :         /* save pointer to the beginning of the current stage */
    1010      392523 :         cb_stage = cbp;
    1011             : 
    1012             :         /* swap pointers to parent and current nodes */
    1013      392523 :         pTmp_short = indices[0];
    1014      392523 :         indices[0] = indices[1];
    1015      392523 :         indices[1] = pTmp_short;
    1016             : 
    1017      392523 :         pTmp = resid[0];
    1018      392523 :         resid[0] = resid[1];
    1019      392523 :         resid[1] = pTmp;
    1020             : 
    1021      392523 :         pTmp = dist[0];
    1022      392523 :         dist[0] = dist[1];
    1023      392523 :         dist[1] = pTmp;
    1024             : 
    1025             :         /* p_max points to maximum distortion node (worst of best) */
    1026      392523 :         p_max = 0;
    1027             : 
    1028             :         /* set distortions to a large value */
    1029      392523 :         set_f( dist[1], 99e10f, maxC );
    1030             : 
    1031     9152779 :         for ( j = 0; j < levels[s]; j++ )
    1032             :         {
    1033             :             /* compute weighted codebook element and its energy */
    1034   148390272 :             for ( c2 = 0; c2 < N; c2++ )
    1035             :             {
    1036   139630016 :                 Tmp[c2] = w[c2] * cbp[c2];
    1037             :             }
    1038             : 
    1039     8760256 :             en = cbp[0] * Tmp[0];
    1040   139630016 :             for ( c2 = 1; c2 < N; c2++ )
    1041             :             {
    1042   130869760 :                 en += cbp[c2] * Tmp[c2];
    1043             :             }
    1044     8760256 :             cbp += N;
    1045             : 
    1046             :             /* iterate over all parent nodes */
    1047    18627408 :             for ( c = 0; c < m; c++ )
    1048             :             {
    1049     9867152 :                 pTmp = &resid[0][c * N];
    1050     9867152 :                 tmp = pTmp[0] * Tmp[0];
    1051   157340352 :                 for ( c2 = 1; c2 < N; c2++ )
    1052             :                 {
    1053   147473200 :                     tmp += pTmp[c2] * Tmp[c2];
    1054             :                 }
    1055     9867152 :                 tmp = en - 2.0f * tmp;
    1056     9867152 :                 tmp += dist[0][c];
    1057             : 
    1058     9867152 :                 if ( tmp <= dist[1][p_max] )
    1059             :                 {
    1060             :                     /* replace worst */
    1061     2861661 :                     dist[1][p_max] = tmp;
    1062     2861661 :                     indices[1][p_max * stagesVQ + s] = j;
    1063     2861661 :                     parents[p_max] = c;
    1064             : 
    1065             :                     /* limit number of times inner loop is entered */
    1066     2861661 :                     if ( counter < max_inner )
    1067             :                     {
    1068     2861661 :                         counter++;
    1069     2861661 :                         if ( counter < max_inner )
    1070             :                         {
    1071             :                             /* find new worst */
    1072     2861661 :                             p_max = maximum( dist[1], maxC, &ftmp );
    1073             :                         }
    1074             :                         else
    1075             :                         {
    1076             :                             /* find minimum distortion */
    1077           0 :                             p_max = minimum( dist[1], maxC, &ftmp );
    1078             :                         }
    1079             :                     }
    1080             :                 }
    1081             :             }
    1082             :         }
    1083             : 
    1084             :         /*------------------------------------------------------------*
    1085             :          * Compute error vectors for each node
    1086             :          *------------------------------------------------------------*/
    1087             : 
    1088     1177569 :         for ( c = 0; c < maxC; c++ )
    1089             :         {
    1090             :             /* subtract codebook entry from the residual vector of the parent node */
    1091      785046 :             pTmp = resid[1] + c * N;
    1092      785046 :             mvr2r( resid[0] + parents[c] * N, pTmp, N );
    1093      785046 :             v_sub( pTmp, cb_stage + ( indices[1][c * stagesVQ + s] ) * N, pTmp, N );
    1094             : 
    1095             :             /* get indices that were used for parent node */
    1096      785046 :             mvs2s( indices[0] + parents[c] * stagesVQ, indices[1] + c * stagesVQ, s );
    1097             :         }
    1098             : 
    1099      392523 :         m = maxC;
    1100             :     }
    1101             : 
    1102      306367 :     mvs2s( indices[1], indices_VQstage, maxC * stagesVQ );
    1103             : 
    1104      306367 :     return;
    1105             : }
    1106             : 
    1107             : /*---------------------------------------------------------------------------
    1108             :  * vq_enc_lsf_lvq()
    1109             :  *
    1110             :  *  Multi-stage VQ encoder for LSF quantization. Trained codebooks are used in initial stages
    1111             :  *  and lattice-VQ quantization is applied on residual vector in other stages.
    1112             :  *
    1113             :  * Note:
    1114             :  *    Compared to normal multistage VQ resulting LSF vector is reordered to ascending order before
    1115             :  *    weighted error calculation (spectral distortion) at the final stage.
    1116             :  *
    1117             :  * Returns:
    1118             :  *    Weighted error
    1119             :  *--------------------------------------------------------------------------*/
    1120             : 
    1121      346375 : static float vq_lvq_lsf_enc(
    1122             :     const int16_t pred_flag,
    1123             :     const int16_t mode,
    1124             :     const float u[],
    1125             :     const int16_t levels[],
    1126             :     const int16_t stages,
    1127             :     const float w[],
    1128             :     int16_t Idx[],
    1129             :     const float *lsf,
    1130             :     const float *pred,
    1131             :     float *resq,
    1132             :     float *lsfq )
    1133             : {
    1134             :     int16_t i;
    1135             :     const float *const *cb, *cb_stage;
    1136             :     float cand[LSFMBEST][M];
    1137      346375 :     int16_t maxC = LSFMBEST, stagesVQ;
    1138             :     int16_t mode_glb, j, indices_firstVQ[LSFMBEST * MAX_VQ_STAGES], c2;
    1139             :     float dd[M];
    1140             :     float quant[LSFMBEST][M], diff[M];
    1141             :     float lat_cv[LSFMBEST][M], e[LSFMBEST], ftmp, tmp;
    1142             :     int16_t idx_lead[LSFMBEST][2], idx_scale[LSFMBEST][2];
    1143             : 
    1144      346375 :     stagesVQ = stages - 1;
    1145             :     /* Codebook selection */
    1146      346375 :     if ( pred_flag == 0 ) /* safety net*/
    1147             :     {
    1148       91473 :         cb = &Quantizers[CB_lsf[mode]];
    1149             : #ifdef DEBUGGING
    1150             :         assert( levels[stagesVQ] >= min_lat_bits_SN[mode] );
    1151             : #endif
    1152       91473 :         if ( mode < 6 ) /* for NB */
    1153             :         {
    1154           0 :             mode_glb = offset_lvq_modes_SN[mode] + offset_in_lvq_mode_SN[mode][levels[stagesVQ] - min_lat_bits_SN[mode]];
    1155             :         }
    1156             :         else
    1157             :         {
    1158       91473 :             mode_glb = offset_lvq_modes_SN[mode] + levels[stagesVQ] - min_lat_bits_SN[mode]; /* there is granularity of 1 bit */
    1159             :         }
    1160             :     }
    1161             :     else /*  pred */
    1162             :     {
    1163      254902 :         cb = &Quantizers_p[CB_p_lsf[mode]];
    1164             : #ifdef DEBUGGING
    1165             :         assert( levels[stagesVQ] >= min_lat_bits_pred[mode] );
    1166             : #endif
    1167      254902 :         if ( ( mode < 6 ) || ( mode == 12 ) ) /* for NB or I 16k */
    1168             :         {
    1169        5243 :             mode_glb = offset_lvq_modes_pred[mode] + offset_in_lvq_mode_pred[mode][levels[stagesVQ] - min_lat_bits_pred[mode]];
    1170             :         }
    1171             :         else
    1172             :         {
    1173      249659 :             mode_glb = offset_lvq_modes_pred[mode] + levels[stagesVQ] - min_lat_bits_pred[mode];
    1174             :         }
    1175             :     }
    1176             : 
    1177      346375 :     if ( stagesVQ > 0 )
    1178             :     {
    1179             :         /* first VQ stages */
    1180      303029 :         first_VQstages( cb, u, levels, stagesVQ, w, M, MSVQ_MAXCNT, indices_firstVQ );
    1181             :     }
    1182             : 
    1183     1039125 :     for ( i = 0; i < maxC; i++ )
    1184             :     {
    1185      692750 :         mvr2r( pred, cand[i], M );
    1186     1471120 :         for ( j = 0; j < stagesVQ; j++ )
    1187             :         {
    1188      778370 :             Idx[j] = indices_firstVQ[i * stagesVQ + j];
    1189             :         }
    1190             : 
    1191     1471120 :         for ( j = 0; j < stagesVQ; j++ )
    1192             :         {
    1193      778370 :             cb_stage = cb[j];
    1194      778370 :             v_add( cand[i], cb_stage + Idx[j] * M, cand[i], M );
    1195             :         }
    1196             : 
    1197             : 
    1198             :         /* LVQ quantization */
    1199      692750 :         v_sub( lsf, cand[i], dd, M );
    1200      692750 :         mslvq( dd, quant[i], lat_cv[i], idx_lead[i], idx_scale[i], w, mode, mode_glb, pred_flag );
    1201      692750 :         v_add( cand[i], quant[i], cand[i], M );
    1202             : 
    1203             :         /* arrange the LSF candidate vector prior to selection to an ascending order*/
    1204      692750 :         v_sort( cand[i], 0, M - 1 );
    1205             : 
    1206             :         /* calculate the spectral distortion using weighted MSE of sorted LSF vector*/
    1207      692750 :         v_sub( cand[i], lsf, diff, M );
    1208      692750 :         v_mult( diff, diff, diff, M );
    1209      692750 :         e[i] = dotp( diff, w, M );
    1210             :     }
    1211             : 
    1212             :     /* find the optimal candidate */
    1213      346375 :     c2 = minimum( e, maxC, &ftmp );
    1214      346375 :     set_f( resq, 0.0f, M );
    1215      735560 :     for ( j = 0; j < stagesVQ; j++ )
    1216             :     {
    1217      389185 :         Idx[j] = indices_firstVQ[c2 * stagesVQ + j];
    1218      389185 :         cb_stage = cb[j];
    1219      389185 :         v_add( resq, cb_stage + Idx[j] * M, resq, M );
    1220             :     }
    1221      346375 :     v_add( resq, quant[c2], resq, M ); /* quantized prediction residual */
    1222             : 
    1223      346375 :     mvr2r( cand[c2], lsfq, M );
    1224      346375 :     index_lvq( lat_cv[c2], idx_lead[c2], idx_scale[c2], mode_glb, &Idx[stagesVQ], pred_flag );
    1225             : 
    1226      346375 :     tmp = e[c2];
    1227             : 
    1228      346375 :     return tmp;
    1229             : }
    1230             : 
    1231             : 
    1232             : /*---------------------------------------------------------------------------
    1233             :  * BcTcvq_1st()
    1234             :  *
    1235             :  *
    1236             :  *--------------------------------------------------------------------------*/
    1237             : 
    1238         574 : static void BcTcvq_1st(
    1239             :     /*const*/ float x[][2],
    1240             :     const float CB[][128][2],
    1241             :     int16_t s[][16],
    1242             :     int16_t c[][16],
    1243             :     float cDist[][16],
    1244             :     float Q[][16][2],
    1245             :     /*const*/ float W[][2] )
    1246             : {
    1247             :     int16_t state, prev_state;
    1248             :     int16_t index, bestCode;
    1249             :     float dist, minDist;
    1250             : 
    1251        5166 :     for ( state = 0; state < NUM_STATE; state += 2 )
    1252             :     {
    1253        4592 :         prev_state = NTRANS[0][state];
    1254        4592 :         index = NTRANS[2][state];
    1255             : 
    1256        4592 :         minDist = ( x[0][0] - CB[0][index][0] ) * ( x[0][0] - CB[0][index][0] ) * W[0][0];
    1257        4592 :         minDist += ( x[0][1] - CB[0][index][1] ) * ( x[0][1] - CB[0][index][1] ) * W[0][1];
    1258        4592 :         bestCode = index;
    1259             : 
    1260       73472 :         for ( index = index + 8; index < 128; index += 8 )
    1261             :         {
    1262       68880 :             dist = ( x[0][0] - CB[0][index][0] ) * ( x[0][0] - CB[0][index][0] ) * W[0][0];
    1263       68880 :             dist += ( x[0][1] - CB[0][index][1] ) * ( x[0][1] - CB[0][index][1] ) * W[0][1];
    1264             : 
    1265       68880 :             if ( dist < minDist )
    1266             :             {
    1267       15329 :                 minDist = dist;
    1268       15329 :                 bestCode = index;
    1269             :             }
    1270             :         }
    1271             : 
    1272             :         /* Update */
    1273        4592 :         s[0][state] = prev_state;
    1274        4592 :         c[0][state] = bestCode;
    1275        4592 :         cDist[0][state] = minDist;
    1276        4592 :         Q[0][state][0] = CB[0][bestCode][0];
    1277        4592 :         Q[0][state][1] = CB[0][bestCode][1];
    1278             :     }
    1279             : 
    1280         574 :     return;
    1281             : }
    1282             : 
    1283             : /*---------------------------------------------------------------------------
    1284             :  * BcTcvq_2nd()
    1285             :  *
    1286             :  *
    1287             :  *--------------------------------------------------------------------------*/
    1288             : 
    1289         574 : static void BcTcvq_2nd(
    1290             :     /*const*/ float x[][2],
    1291             :     const float CB[][128][2],
    1292             :     int16_t s[][16],
    1293             :     int16_t c[][16],
    1294             :     float cDist[][16],
    1295             :     float Q[][16][2],
    1296             :     /*const*/ float W[][2],
    1297             :     const float itc[][2][2] )
    1298             : {
    1299             :     int16_t state, prev_state;
    1300             :     int16_t index, bestCode;
    1301             :     float dist, minDist;
    1302             :     float pred[N_DIM], target[N_DIM];
    1303             : 
    1304        9758 :     for ( state = 0; state < NUM_STATE; state++ )
    1305             :     {
    1306        9184 :         prev_state = NTRANS[0][state];
    1307        9184 :         index = NTRANS[2][state];
    1308             : 
    1309             :         /* Prediction */
    1310        9184 :         pred[0] = itc[0][0][0] * Q[0][prev_state][0] + itc[0][0][1] * Q[0][prev_state][1];
    1311        9184 :         pred[1] = itc[0][1][0] * Q[0][prev_state][0] + itc[0][1][1] * Q[0][prev_state][1];
    1312        9184 :         target[0] = x[1][0] - pred[0];
    1313        9184 :         target[1] = x[1][1] - pred[1];
    1314             : 
    1315        9184 :         minDist = ( target[0] - CB[1][index][0] ) * ( target[0] - CB[1][index][0] ) * W[1][0];
    1316        9184 :         minDist += ( target[1] - CB[1][index][1] ) * ( target[1] - CB[1][index][1] ) * W[1][1];
    1317        9184 :         bestCode = index;
    1318             : 
    1319      146944 :         for ( index = index + 8; index < 128; index += 8 )
    1320             :         {
    1321      137760 :             dist = ( target[0] - CB[1][index][0] ) * ( target[0] - CB[1][index][0] ) * W[1][0];
    1322      137760 :             dist += ( target[1] - CB[1][index][1] ) * ( target[1] - CB[1][index][1] ) * W[1][1];
    1323             : 
    1324      137760 :             if ( dist < minDist )
    1325             :             {
    1326       26420 :                 minDist = dist;
    1327       26420 :                 bestCode = index;
    1328             :             }
    1329             :         }
    1330             : 
    1331             :         /* Update */
    1332        9184 :         s[1][state] = prev_state;
    1333        9184 :         c[1][state] = bestCode;
    1334        9184 :         cDist[1][state] = cDist[0][prev_state] + minDist;
    1335        9184 :         Q[1][state][0] = CB[1][bestCode][0] + pred[0];
    1336        9184 :         Q[1][state][1] = CB[1][bestCode][1] + pred[1];
    1337             :     }
    1338             : 
    1339         574 :     return;
    1340             : }
    1341             : 
    1342             : 
    1343             : /*---------------------------------------------------------------------------
    1344             :  * BcTcvq_SubBlock()
    1345             :  *
    1346             :  *
    1347             :  *--------------------------------------------------------------------------*/
    1348             : 
    1349        1148 : static void BcTcvq_SubBlock(
    1350             :     /*const*/ float x[][2],
    1351             :     const float CB[][64][2],
    1352             :     int16_t s[][16],
    1353             :     int16_t c[][16],
    1354             :     float cDist[][16],
    1355             :     float Q[][16][2],
    1356             :     const int16_t stage,
    1357             :     /*const*/ float W[][2],
    1358             :     const float itc[][2][2] )
    1359             : {
    1360             :     int16_t stage1, stage2, state, prev_state, branch;
    1361             :     int16_t index, bestCode, brCode[N_DIM];
    1362             :     float dist, minDist, brDist[N_DIM];
    1363             :     float pred[N_DIM], target[N_DIM], brQuant[N_DIM][N_DIM];
    1364             : 
    1365        1148 :     stage1 = stage - 1;
    1366        1148 :     stage2 = stage - 2;
    1367             : 
    1368       19516 :     for ( state = 0; state < NUM_STATE; state++ )
    1369             :     {
    1370             : 
    1371             :         /* 1st brarnch search */
    1372       18368 :         prev_state = NTRANS[0][state];
    1373       18368 :         index = NTRANS[2][state];
    1374             : 
    1375             :         /* Prediction */
    1376       18368 :         pred[0] = itc[stage1][0][0] * Q[stage1][prev_state][0] + itc[stage1][0][1] * Q[stage1][prev_state][1];
    1377       18368 :         pred[1] = itc[stage1][1][0] * Q[stage1][prev_state][0] + itc[stage1][1][1] * Q[stage1][prev_state][1];
    1378       18368 :         target[0] = x[stage][0] - pred[0];
    1379       18368 :         target[1] = x[stage][1] - pred[1];
    1380             : 
    1381       18368 :         minDist = ( target[0] - CB[stage2][index][0] ) * ( target[0] - CB[stage2][index][0] ) * W[stage][0];
    1382       18368 :         minDist += ( target[1] - CB[stage2][index][1] ) * ( target[1] - CB[stage2][index][1] ) * W[stage][1];
    1383       18368 :         bestCode = index;
    1384             : 
    1385      146944 :         for ( index = index + 8; index < 64; index += 8 )
    1386             :         {
    1387      128576 :             dist = ( target[0] - CB[stage2][index][0] ) * ( target[0] - CB[stage2][index][0] ) * W[stage][0];
    1388      128576 :             dist += ( target[1] - CB[stage2][index][1] ) * ( target[1] - CB[stage2][index][1] ) * W[stage][1];
    1389             : 
    1390      128576 :             if ( dist < minDist )
    1391             :             {
    1392       35755 :                 minDist = dist;
    1393       35755 :                 bestCode = index;
    1394             :             }
    1395             :         }
    1396             : 
    1397       18368 :         brCode[0] = bestCode;
    1398       18368 :         brDist[0] = cDist[stage1][prev_state] + minDist;
    1399       18368 :         brQuant[0][0] = CB[stage2][bestCode][0] + pred[0];
    1400       18368 :         brQuant[0][1] = CB[stage2][bestCode][1] + pred[1];
    1401             : 
    1402             :         /* 2nd branch search */
    1403       18368 :         prev_state = NTRANS[1][state];
    1404       18368 :         index = NTRANS[3][state];
    1405             : 
    1406             :         /* Prediction */
    1407       18368 :         pred[0] = itc[stage1][0][0] * Q[stage1][prev_state][0] + itc[stage1][0][1] * Q[stage1][prev_state][1];
    1408       18368 :         pred[1] = itc[stage1][1][0] * Q[stage1][prev_state][0] + itc[stage1][1][1] * Q[stage1][prev_state][1];
    1409       18368 :         target[0] = x[stage][0] - pred[0];
    1410       18368 :         target[1] = x[stage][1] - pred[1];
    1411             : 
    1412       18368 :         minDist = ( target[0] - CB[stage2][index][0] ) * ( target[0] - CB[stage2][index][0] ) * W[stage][0];
    1413       18368 :         minDist += ( target[1] - CB[stage2][index][1] ) * ( target[1] - CB[stage2][index][1] ) * W[stage][1];
    1414       18368 :         bestCode = index;
    1415             : 
    1416      146944 :         for ( index = index + 8; index < 64; index += 8 )
    1417             :         {
    1418             : 
    1419      128576 :             dist = ( target[0] - CB[stage2][index][0] ) * ( target[0] - CB[stage2][index][0] ) * W[stage][0];
    1420      128576 :             dist += ( target[1] - CB[stage2][index][1] ) * ( target[1] - CB[stage2][index][1] ) * W[stage][1];
    1421             : 
    1422      128576 :             if ( dist < minDist )
    1423             :             {
    1424       36198 :                 minDist = dist;
    1425       36198 :                 bestCode = index;
    1426             :             }
    1427             :         }
    1428             : 
    1429       18368 :         brCode[1] = bestCode;
    1430       18368 :         brDist[1] = cDist[stage1][prev_state] + minDist;
    1431       18368 :         brQuant[1][0] = CB[stage2][bestCode][0] + pred[0];
    1432       18368 :         brQuant[1][1] = CB[stage2][bestCode][1] + pred[1];
    1433             : 
    1434             :         /* Select Best branch */
    1435       18368 :         branch = 1;
    1436             : 
    1437       18368 :         if ( brDist[0] <= brDist[1] )
    1438             :         {
    1439        8733 :             branch = 0;
    1440             :         }
    1441             : 
    1442             :         /* Update */
    1443       18368 :         s[stage][state] = NTRANS[branch][state];
    1444       18368 :         c[stage][state] = brCode[branch];
    1445       18368 :         cDist[stage][state] = brDist[branch];
    1446       18368 :         Q[stage][state][0] = brQuant[branch][0];
    1447       18368 :         Q[stage][state][1] = brQuant[branch][1];
    1448             :     }
    1449             : 
    1450        1148 :     return;
    1451             : }
    1452             : 
    1453             : 
    1454             : /*---------------------------------------------------------------------------
    1455             :  * BcTcvq_FixSearch()
    1456             :  *
    1457             :  *
    1458             :  *--------------------------------------------------------------------------*/
    1459             : 
    1460      146944 : static float BcTcvq_FixSearch(
    1461             :     /*const*/ float x[][2],
    1462             :     const float CB[][32][2],
    1463             :     int16_t c[][4],
    1464             :     float Q[][16][2],
    1465             :     const int16_t FixBranch[][4][4],
    1466             :     const int16_t stage,
    1467             :     const int16_t inis,
    1468             :     const int16_t fins,
    1469             :     int16_t *prev_state,
    1470             :     /*const*/ float W[][2],
    1471             :     const float itc[][2][2] )
    1472             : {
    1473             :     int16_t stage1, stage4, branch;
    1474             :     int16_t index, bestCode;
    1475             :     float dist, minDist;
    1476             :     float pred[N_DIM], target[N_DIM];
    1477             : 
    1478      146944 :     stage1 = stage - 1;
    1479      146944 :     stage4 = stage - 4;
    1480             : 
    1481      146944 :     branch = FixBranch[inis >> 2][fins][stage4];
    1482      146944 :     index = NTRANS2[branch + 2][*prev_state];
    1483             : 
    1484             :     /* Prediction */
    1485      146944 :     pred[0] = itc[stage1][0][0] * Q[stage1][*prev_state][0] + itc[stage1][0][1] * Q[stage1][*prev_state][1];
    1486      146944 :     pred[1] = itc[stage1][1][0] * Q[stage1][*prev_state][0] + itc[stage1][1][1] * Q[stage1][*prev_state][1];
    1487      146944 :     target[0] = x[stage][0] - pred[0];
    1488      146944 :     target[1] = x[stage][1] - pred[1];
    1489             : 
    1490      146944 :     minDist = ( target[0] - CB[stage4][index][0] ) * ( target[0] - CB[stage4][index][0] ) * W[stage][0];
    1491      146944 :     minDist += ( target[1] - CB[stage4][index][1] ) * ( target[1] - CB[stage4][index][1] ) * W[stage][1];
    1492      146944 :     bestCode = index;
    1493             : 
    1494      587776 :     for ( index = index + 8; index < 32; index += 8 )
    1495             :     {
    1496      440832 :         dist = ( target[0] - CB[stage4][index][0] ) * ( target[0] - CB[stage4][index][0] ) * W[stage][0];
    1497      440832 :         dist += ( target[1] - CB[stage4][index][1] ) * ( target[1] - CB[stage4][index][1] ) * W[stage][1];
    1498             : 
    1499      440832 :         if ( dist < minDist )
    1500             :         {
    1501      179168 :             minDist = dist;
    1502      179168 :             bestCode = index;
    1503             :         }
    1504             :     }
    1505             : 
    1506             :     /* Update */
    1507      146944 :     *prev_state = NTRANS2[branch][*prev_state];
    1508      146944 :     c[fins][stage4] = bestCode;
    1509      146944 :     Q[stage][*prev_state][0] = CB[stage4][bestCode][0] + pred[0];
    1510      146944 :     Q[stage][*prev_state][1] = CB[stage4][bestCode][1] + pred[1];
    1511             : 
    1512      146944 :     return minDist;
    1513             : }
    1514             : 
    1515             : /*---------------------------------------------------------------------------
    1516             :  * optimalPath()
    1517             :  *
    1518             :  *
    1519             :  *--------------------------------------------------------------------------*/
    1520             : 
    1521         574 : static int16_t optimalPath(
    1522             :     /*const*/ float cDist[][16],
    1523             :     /*const*/ float blockDist[],
    1524             :     /*const*/ int16_t blockCodeword[][4],
    1525             :     int16_t bestCodeword[],
    1526             :     /*const*/ int16_t codeWord[][16],
    1527             :     int16_t bestState[],
    1528             :     int16_t preState[][16] )
    1529             : {
    1530             :     int16_t stage, state;
    1531             :     float opDist[NUM_STATE];
    1532             :     float minDist;
    1533             :     int16_t fBlock;
    1534             :     int16_t prev_state;
    1535             : 
    1536        9758 :     for ( state = 0; state < NUM_STATE; state++ )
    1537             :     {
    1538        9184 :         opDist[state] = cDist[3][state] + blockDist[state];
    1539             :     }
    1540             : 
    1541         574 :     minDist = opDist[0];
    1542         574 :     fBlock = 0;
    1543             : 
    1544        9184 :     for ( state = 1; state < NUM_STATE; state++ )
    1545             :     {
    1546        8610 :         if ( opDist[state] < minDist )
    1547             :         {
    1548        1508 :             minDist = opDist[state];
    1549        1508 :             fBlock = state;
    1550             :         }
    1551             :     }
    1552             : 
    1553         574 :     prev_state = bestState[4] = fBlock;
    1554             : 
    1555        2870 :     for ( stage = N_STAGE_VQ - 5; stage >= 0; stage-- )
    1556             :     {
    1557        2296 :         bestCodeword[stage] = codeWord[stage][prev_state];
    1558        2296 :         bestState[stage] = preState[stage][prev_state];
    1559        2296 :         prev_state = bestState[stage];
    1560             :     }
    1561             : 
    1562        2870 :     for ( stage = 0; stage < 4; stage++ )
    1563             :     {
    1564        2296 :         bestCodeword[stage + 4] = blockCodeword[fBlock][stage];
    1565             :     }
    1566             : 
    1567         574 :     return fBlock;
    1568             : }
    1569             : 
    1570             : /*---------------------------------------------------------------------------
    1571             :  * quantEnc()
    1572             :  *
    1573             :  *
    1574             :  *--------------------------------------------------------------------------*/
    1575             : 
    1576         574 : static void quantEnc(
    1577             :     float *y,
    1578             :     const int16_t c[],
    1579             :     const float CB_SUB1[][128][2],
    1580             :     const float CB_SUB2[][64][2],
    1581             :     const float CB_SUB3[][32][2],
    1582             :     const float itc[][2][2] )
    1583             : {
    1584             :     int16_t i, j;
    1585             :     int16_t stage;
    1586             :     float pred[N_DIM], Y[8][2];
    1587             : 
    1588             :     /* stage #1 */
    1589         574 :     Y[0][0] = CB_SUB1[0][c[0]][0];
    1590         574 :     Y[0][1] = CB_SUB1[0][c[0]][1];
    1591             : 
    1592             :     /* stage #2 */
    1593         574 :     pred[0] = itc[0][0][0] * Y[0][0] + itc[0][0][1] * Y[0][1];
    1594         574 :     pred[1] = itc[0][1][0] * Y[0][0] + itc[0][1][1] * Y[0][1];
    1595         574 :     Y[1][0] = CB_SUB1[1][c[1]][0] + pred[0];
    1596         574 :     Y[1][1] = CB_SUB1[1][c[1]][1] + pred[1];
    1597             : 
    1598             :     /* stage #3 - #4 */
    1599        1722 :     for ( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
    1600             :     {
    1601        1148 :         pred[0] = itc[stage - 1][0][0] * Y[stage - 1][0] + itc[stage - 1][0][1] * Y[stage - 1][1];
    1602        1148 :         pred[1] = itc[stage - 1][1][0] * Y[stage - 1][0] + itc[stage - 1][1][1] * Y[stage - 1][1];
    1603             : 
    1604        1148 :         Y[stage][0] = CB_SUB2[stage - 2][c[stage]][0] + pred[0];
    1605        1148 :         Y[stage][1] = CB_SUB2[stage - 2][c[stage]][1] + pred[1];
    1606             :     }
    1607             : 
    1608             :     /* stage #5 - #8 */
    1609        2870 :     for ( stage = N_STAGE_VQ - 4; stage < N_STAGE_VQ; stage++ )
    1610             :     {
    1611        2296 :         pred[0] = itc[stage - 1][0][0] * Y[stage - 1][0] + itc[stage - 1][0][1] * Y[stage - 1][1];
    1612        2296 :         pred[1] = itc[stage - 1][1][0] * Y[stage - 1][0] + itc[stage - 1][1][1] * Y[stage - 1][1];
    1613             : 
    1614        2296 :         Y[stage][0] = CB_SUB3[stage - 4][c[stage]][0] + pred[0];
    1615        2296 :         Y[stage][1] = CB_SUB3[stage - 4][c[stage]][1] + pred[1];
    1616             :     }
    1617             : 
    1618             :     /* Transform Vector to Scalar */
    1619        5166 :     for ( i = 0; i < N_STAGE_VQ; i++ )
    1620             :     {
    1621       13776 :         for ( j = 0; j < N_DIM; j++ )
    1622             :         {
    1623        9184 :             y[i * N_DIM + j] = Y[i][j];
    1624             :         }
    1625             :     }
    1626             : 
    1627         574 :     return;
    1628             : }
    1629             : 
    1630             : /*---------------------------------------------------------------------------
    1631             :  * buildCode()
    1632             :  *
    1633             :  *
    1634             :  *--------------------------------------------------------------------------*/
    1635             : 
    1636         574 : static void buildCode( int16_t *ind, const int16_t fins, const int16_t c[], const int16_t s[] )
    1637             : {
    1638             :     int16_t stage;
    1639             :     int16_t BrIndex[4];
    1640             : 
    1641         574 :     set_s( BrIndex, 0, ( N_STAGE_VQ - 4 ) );
    1642             : 
    1643             : 
    1644        2870 :     for ( stage = N_STAGE_VQ - 4; stage >= 1; stage-- )
    1645             :     {
    1646        2296 :         if ( s[stage] > 7 )
    1647             :         {
    1648        1175 :             BrIndex[stage - 1] = 1;
    1649             :         }
    1650             :     }
    1651         574 :     ind[0] = fins;
    1652             : 
    1653             :     /* stage #1 - #2 */
    1654        1722 :     for ( stage = 0; stage < 2; stage++ )
    1655             :     {
    1656        1148 :         ind[stage + 1] = BrIndex[stage] << 4;
    1657        1148 :         ind[stage + 1] += c[stage] >> 3;
    1658             :     }
    1659             : 
    1660             :     /* stage #3 - #4 */
    1661        1722 :     for ( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
    1662             :     {
    1663        1148 :         ind[stage + 1] = BrIndex[stage] << 3;
    1664        1148 :         ind[stage + 1] += c[stage] >> 3;
    1665             :     }
    1666             : 
    1667             :     /* Stage #5 - #8 */
    1668        2870 :     for ( stage = N_STAGE_VQ - 4; stage < N_STAGE_VQ; stage++ )
    1669             :     {
    1670        2296 :         ind[stage + 1] = c[stage] >> 3;
    1671             :     }
    1672             : 
    1673         574 :     return;
    1674             : }
    1675             : 
    1676             : /*---------------------------------------------------------------------------
    1677             :  * BcTcvq()
    1678             :  *
    1679             :  *
    1680             :  *--------------------------------------------------------------------------*/
    1681             : 
    1682         574 : static void BcTcvq( int16_t snFlag, const float *x, float *y, const float *weight, int16_t *ind )
    1683             : {
    1684             :     float X[N_STAGE_VQ][N_DIM], W[N_STAGE_VQ][N_DIM];
    1685             : 
    1686             :     /* Count Variable */
    1687             :     int16_t i, j;
    1688             : 
    1689             :     /* TCVQ Structure */
    1690             :     int16_t stage, state, prev_state;
    1691             :     int16_t preState[N_STAGE_VQ][NUM_STATE];
    1692             :     int16_t codeWord[N_STAGE_VQ][NUM_STATE];
    1693             :     float acumDist[N_STAGE_VQ - 4][NUM_STATE];
    1694             :     int16_t inis, fins, ptr_fins;
    1695             :     int16_t fBlock;
    1696             :     int16_t fState[NUM_STATE];
    1697             :     int16_t fCodeword[4][4];
    1698             :     int16_t iniBlock[NUM_STATE];
    1699             :     int16_t blockCodeword[NUM_STATE][4];
    1700             : 
    1701             :     /* Prediction variable */
    1702             :     float quant[N_STAGE_VQ][NUM_STATE][N_DIM];
    1703             : 
    1704             :     /* Distortion variable */
    1705             :     float minDist;
    1706             :     float fDist;
    1707             :     float blockDist[NUM_STATE];
    1708             : 
    1709             :     /* Decoding variable */
    1710             :     int16_t bestCodeword[N_STAGE_VQ];
    1711             :     int16_t bestState[N_STAGE_VQ];
    1712             : 
    1713             :     /* Code Share variable */
    1714             :     const float( *TCVQ_CB_SUB1 )[128][2], ( *TCVQ_CB_SUB2 )[64][2], ( *TCVQ_CB_SUB3 )[32][2];
    1715             :     const float( *IntraCoeff )[2][2];
    1716             : 
    1717             :     /* Memoryless Module */
    1718         574 :     if ( snFlag )
    1719             :     {
    1720          88 :         TCVQ_CB_SUB1 = SN_TCVQ_CB_SUB1;
    1721          88 :         TCVQ_CB_SUB2 = SN_TCVQ_CB_SUB2;
    1722          88 :         TCVQ_CB_SUB3 = SN_TCVQ_CB_SUB3;
    1723          88 :         IntraCoeff = SN_IntraCoeff;
    1724             :     }
    1725             :     else /* Memory Module */
    1726             :     {
    1727         486 :         TCVQ_CB_SUB1 = AR_TCVQ_CB_SUB1;
    1728         486 :         TCVQ_CB_SUB2 = AR_TCVQ_CB_SUB2;
    1729         486 :         TCVQ_CB_SUB3 = AR_TCVQ_CB_SUB3;
    1730         486 :         IntraCoeff = AR_IntraCoeff;
    1731             :     }
    1732             : 
    1733             :     /* Transform Scalar to Vector */
    1734        5166 :     for ( i = 0; i < N_STAGE_VQ; i++ )
    1735             :     {
    1736       13776 :         for ( j = 0; j < N_DIM; j++ )
    1737             :         {
    1738        9184 :             X[i][j] = x[( N_DIM * i ) + j];
    1739        9184 :             W[i][j] = weight[( N_DIM * i ) + j];
    1740             :         }
    1741             :     }
    1742             : 
    1743             :     /* Initialzie */
    1744        2870 :     for ( i = 0; i < N_STAGE_VQ - 4; i++ )
    1745             :     {
    1746       39032 :         for ( j = 0; j < NUM_STATE; j++ )
    1747             :         {
    1748       36736 :             acumDist[i][j] = 0.f;
    1749             :         }
    1750             :     }
    1751             : 
    1752             :     /* BcTcvq Search */
    1753             :     /* stage #1 */
    1754         574 :     BcTcvq_1st( X, TCVQ_CB_SUB1, preState, codeWord, acumDist, quant, W );
    1755             : 
    1756             :     /* stage #2 */
    1757         574 :     BcTcvq_2nd( X, TCVQ_CB_SUB1, preState, codeWord, acumDist, quant, W, IntraCoeff );
    1758             : 
    1759             :     /* stage #3 - #4 */
    1760        1722 :     for ( stage = 2; stage < N_STAGE_VQ - 4; stage++ )
    1761             :     {
    1762        1148 :         BcTcvq_SubBlock( X, TCVQ_CB_SUB2, preState, codeWord, acumDist, quant, stage, W, IntraCoeff );
    1763             :     }
    1764             : 
    1765             :     /* Search initial state at each block */
    1766        9758 :     for ( state = 0; state < NUM_STATE; state++ )
    1767             :     {
    1768        9184 :         prev_state = state;
    1769             : 
    1770       45920 :         for ( stage = N_STAGE_VQ - 5; stage >= 0; stage-- )
    1771             :         {
    1772       36736 :             prev_state = preState[stage][prev_state];
    1773             :         }
    1774        9184 :         iniBlock[state] = prev_state;
    1775             :     }
    1776             : 
    1777             :     /* stage #5 - #8 */
    1778        9758 :     for ( state = 0; state < NUM_STATE; state++ )
    1779             :     {
    1780        9184 :         inis = iniBlock[state];
    1781        9184 :         ptr_fins = inis >> 2;
    1782             : 
    1783        9184 :         minDist = FLT_MAX;
    1784             : 
    1785       45920 :         for ( i = 0; i < 4; i++ )
    1786             :         {
    1787       36736 :             fins = ptr_fins * 4 + i;
    1788             : 
    1789       36736 :             prev_state = state;
    1790       36736 :             fDist = BcTcvq_FixSearch( X, TCVQ_CB_SUB3, fCodeword, quant, FixBranch_tbl, N_STAGE_VQ - 4, inis, i, &prev_state, W, IntraCoeff );
    1791             : 
    1792      146944 :             for ( stage = N_STAGE_VQ - 3; stage < N_STAGE_VQ; stage++ )
    1793             :             {
    1794      110208 :                 fDist += BcTcvq_FixSearch( X, TCVQ_CB_SUB3, fCodeword, quant, FixBranch_tbl, stage, inis, i, &prev_state, W, IntraCoeff );
    1795             :             }
    1796             : 
    1797       36736 :             if ( fDist < minDist )
    1798             :             {
    1799       19522 :                 minDist = fDist;
    1800       19522 :                 blockDist[state] = minDist;
    1801       19522 :                 fState[state] = fins;
    1802             : 
    1803       97610 :                 for ( j = 0; j < 4; j++ )
    1804             :                 {
    1805       78088 :                     blockCodeword[state][j] = fCodeword[i][j];
    1806             :                 }
    1807             :             }
    1808             :         }
    1809             :     }
    1810             : 
    1811             :     /* Select optimal path */
    1812         574 :     fBlock = optimalPath( acumDist, blockDist, blockCodeword, bestCodeword, codeWord, bestState, preState );
    1813             : 
    1814             :     /* Select Quantized Value */
    1815         574 :     quantEnc( y, bestCodeword, TCVQ_CB_SUB1, TCVQ_CB_SUB2, TCVQ_CB_SUB3, IntraCoeff );
    1816             : 
    1817             :     /* Buid Code for Decoder */
    1818         574 :     buildCode( ind, fState[fBlock], bestCodeword, bestState );
    1819             : 
    1820         574 :     return;
    1821             : }
    1822             : 
    1823             : 
    1824             : /*---------------------------------------------------------------------------
    1825             :  * SVQ_2d()
    1826             :  *
    1827             :  *
    1828             :  *--------------------------------------------------------------------------*/
    1829             : 
    1830         612 : static int16_t SVQ_2d(
    1831             :     const float *x,
    1832             :     float *y,
    1833             :     const float *W,
    1834             :     const float CB[][8],
    1835             :     const int16_t Size )
    1836             : {
    1837             :     int16_t i, j;
    1838         612 :     int16_t index = 0;
    1839             :     float distortion;
    1840             :     float temp;
    1841             : 
    1842         612 :     temp = FLT_MAX;
    1843             : 
    1844       15300 :     for ( i = 0; i < Size; i++ )
    1845             :     {
    1846       14688 :         distortion = 0.0;
    1847      132192 :         for ( j = 0; j < 8; j++ )
    1848             :         {
    1849      117504 :             distortion += ( x[j] - CB[i][j] ) * ( x[j] - CB[i][j] ) * W[j];
    1850             :         }
    1851             : 
    1852       14688 :         if ( distortion < temp )
    1853             :         {
    1854        2357 :             temp = distortion;
    1855        2357 :             index = i;
    1856             :         }
    1857             :     }
    1858             : 
    1859        5508 :     for ( i = 0; i < M / 2; i++ )
    1860             :     {
    1861        4896 :         y[i] = CB[index][i];
    1862             :     }
    1863             : 
    1864         612 :     return index;
    1865             : }
    1866             : 
    1867             : 
    1868             : /*---------------------------------------------------------------------------
    1869             :  * qlsf_ARSN_tcvq_Enc_16k()
    1870             :  *
    1871             :  * Predictive BC-TCQ encoder for LSF quantization
    1872             :  *--------------------------------------------------------------------------*/
    1873             : 
    1874         574 : static float qlsf_ARSN_tcvq_Enc_16k(
    1875             :     const float *x,          /* i  : Vector to be encoded    */
    1876             :     float *y,                /* o  : Quantized LSF vector    */
    1877             :     int16_t *indice,         /* o  : Indices                 */
    1878             :     const float *w,          /* i  : LSF Weights             */
    1879             :     const int16_t nBits,     /* i  : number of bits          */
    1880             :     const int16_t safety_net /* i  : safety_net flag         */
    1881             : )
    1882             : {
    1883             :     int16_t i;
    1884             :     float temp_f;
    1885             :     float yy[M];
    1886             :     float error_svq[M], error_svq_q[M];
    1887             :     float x_q[M];
    1888         574 :     if ( safety_net == 1 )
    1889             :     {
    1890          88 :         indice[0] = 1;
    1891          88 :         BcTcvq( 1, x, x_q, w, &indice[1] );
    1892             : 
    1893          88 :         if ( nBits > 30 )
    1894             :         {
    1895             :             /* SVQ  */
    1896         748 :             for ( i = 0; i < M; i++ )
    1897             :             {
    1898         704 :                 error_svq[i] = ( x[i] - x_q[i] ) * scale_inv_ARSN[i];
    1899             :             }
    1900             : 
    1901             :             /* 5bits 1st Split VQ for Residual*/
    1902          44 :             indice[10] = SVQ_2d( error_svq, error_svq_q, w, AR_SVQ_CB1, 32 );
    1903             :             /* 4bits 2nd Split VQ for Residual*/
    1904          44 :             indice[11] = SVQ_2d( &error_svq[8], &error_svq_q[8], &w[8], AR_SVQ_CB2, 16 );
    1905             : 
    1906         748 :             for ( i = 0; i < M; i++ )
    1907             :             {
    1908         704 :                 x_q[i] = x_q[i] + ( error_svq_q[i] * scale_ARSN[i] );
    1909             :             }
    1910             :         }
    1911             :     }
    1912             :     else
    1913             :     {
    1914         486 :         indice[0] = 0;
    1915         486 :         BcTcvq( 0, x, x_q, w, &indice[1] );
    1916             : 
    1917         486 :         if ( nBits > 30 )
    1918             :         {
    1919             :             /* SVQ */
    1920        4454 :             for ( i = 0; i < M; i++ )
    1921             :             {
    1922        4192 :                 error_svq[i] = x[i] - x_q[i];
    1923             :             }
    1924             : 
    1925             :             /* 5bits 1st Split VQ for Residual*/
    1926         262 :             indice[10] = SVQ_2d( error_svq, error_svq_q, w, AR_SVQ_CB1, 32 );
    1927             :             /* 4bits 2nd Split VQ for Residual*/
    1928         262 :             indice[11] = SVQ_2d( &error_svq[8], &error_svq_q[8], &w[8], AR_SVQ_CB2, 16 );
    1929             : 
    1930        4454 :             for ( i = 0; i < M; i++ )
    1931             :             {
    1932        4192 :                 x_q[i] = x_q[i] + error_svq_q[i];
    1933             :             }
    1934             :         }
    1935             :     }
    1936             : 
    1937         574 :     v_sub( x_q, x, yy, M );
    1938         574 :     v_mult( yy, yy, yy, M );
    1939         574 :     temp_f = dotp( yy, w, M );
    1940             : 
    1941             :     /* Recover the quantized LSF */
    1942         574 :     mvr2r( x_q, y, M );
    1943             : 
    1944         574 :     return temp_f;
    1945             : }
    1946             : 
    1947             : /*-------------------------------------------------------------------*
    1948             :  * lsf_mid_enc()
    1949             :  *
    1950             :  * Mid-frame LSF quantization
    1951             :  --------------------------------------------------------------------*/
    1952             : 
    1953      157656 : static void lsf_mid_enc(
    1954             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle            */
    1955             :     int16_t nb_bits,          /* i  : number of bits                      */
    1956             :     const int32_t int_fs,     /* i  : internal (ACELP) sampling frequency */
    1957             :     const float qlsp0[],      /* i  : quantized LSPs from frame beginning */
    1958             :     const float qlsp1[],      /* i  : quantized LSPs from frame end       */
    1959             :     float lsp[],              /* i/o: mid-frame LSP                       */
    1960             :     const int16_t coder_type, /* i  : coding type                         */
    1961             :     const int16_t bwidth,     /* i  : input signal bandwidth              */
    1962             :     const float Bin_Ener[],   /* i  : per bin log energy spectrum         */
    1963             :     const int16_t ppp_mode,
    1964             :     const int16_t nelp_mode )
    1965             : {
    1966             :     float ftemp, lsf[M], qlsf[M], qlsf1[M], qlsf0[M], wghts[M], err, err_min;
    1967      157656 :     int16_t j, k, idx, size = 0;
    1968      157656 :     const float *ratio = 0;
    1969             : 
    1970             :     /* convert LSPs to LSFs */
    1971      157656 :     lsp2lsf( lsp, lsf, M, int_fs );
    1972      157656 :     lsp2lsf( qlsp0, qlsf0, M, int_fs );
    1973      157656 :     lsp2lsf( qlsp1, qlsf1, M, int_fs );
    1974             : 
    1975      157656 :     Unified_weighting( Bin_Ener, lsf, wghts, bwidth == NB, coder_type == UNVOICED, (int32_t) ( int_fs ), M );
    1976             : 
    1977             :     /* codebook selection, number of bits, size of the codebook */
    1978      157656 :     if ( ppp_mode == 0 && nelp_mode == 0 )
    1979             :     {
    1980             :         /* codebook selection */
    1981      157656 :         if ( coder_type == VOICED )
    1982             :         {
    1983        8880 :             switch ( nb_bits )
    1984             :             {
    1985        1700 :                 case 5:
    1986             :                 {
    1987        1700 :                     ratio = tbl_mid_voi_wb_5b;
    1988        1700 :                     break;
    1989             :                 }
    1990        7180 :                 case 4:
    1991             :                 {
    1992        7180 :                     ratio = tbl_mid_voi_wb_4b;
    1993        7180 :                     break;
    1994             :                 }
    1995           0 :                 case 1:
    1996             :                 {
    1997           0 :                     ratio = tbl_mid_voi_wb_1b;
    1998           0 :                     break;
    1999             :                 }
    2000             :             }
    2001        8880 :         }
    2002      148776 :         else if ( coder_type == UNVOICED )
    2003             :         {
    2004        2056 :             ratio = tbl_mid_unv_wb_5b;
    2005             :         }
    2006             :         else
    2007             :         {
    2008             :             /* GENERIC, TRANSITION, AUDIO and INACTIVE */
    2009      146720 :             switch ( nb_bits )
    2010             :             {
    2011      137552 :                 case 5:
    2012             :                 {
    2013      137552 :                     ratio = tbl_mid_gen_wb_5b;
    2014      137552 :                     break;
    2015             :                 }
    2016         738 :                 case 4:
    2017             :                 {
    2018         738 :                     ratio = tbl_mid_gen_wb_4b;
    2019         738 :                     break;
    2020             :                 }
    2021        8430 :                 case 2:
    2022             :                 {
    2023        8430 :                     ratio = tbl_mid_gen_wb_2b;
    2024        8430 :                     break;
    2025             :                 }
    2026             :             }
    2027      157656 :         }
    2028             : 
    2029      157656 :         size = 1 << nb_bits;
    2030             :     }
    2031           0 :     else if ( ppp_mode == 1 )
    2032             :     {
    2033           0 :         ratio = tbl_mid_voi_wb_1b;
    2034           0 :         nb_bits = 1;
    2035           0 :         size = 2;
    2036             :     }
    2037           0 :     else if ( nelp_mode == 1 )
    2038             :     {
    2039           0 :         ratio = tbl_mid_unv_wb_4b;
    2040           0 :         nb_bits = 4;
    2041           0 :         size = 16;
    2042             :     }
    2043             : 
    2044             :     /* loop over codevectors */
    2045      157656 :     err_min = 1e30f;
    2046      157656 :     idx = 0;
    2047     4839920 :     for ( k = 0; k < size; k++ )
    2048             :     {
    2049     4682264 :         err = 0;
    2050             : 
    2051    79598488 :         for ( j = 0; j < M; j++ )
    2052             :         {
    2053    74916224 :             qlsf[j] = ( 1.0f - ratio[k * M + j] ) * qlsf0[j] + ratio[k * M + j] * qlsf1[j];
    2054             : 
    2055    74916224 :             if ( j > 0 && j < M && qlsf[j] < qlsf[j - 1] + LSF_GAP_MID )
    2056             :             {
    2057      477141 :                 qlsf[j] = qlsf[j - 1] + LSF_GAP_MID;
    2058             :             }
    2059             : 
    2060    74916224 :             ftemp = lsf[j] - qlsf[j];
    2061    74916224 :             err += wghts[j] * ftemp * ftemp;
    2062             :         }
    2063             : 
    2064     4682264 :         if ( err < err_min )
    2065             :         {
    2066      614234 :             err_min = err;
    2067      614234 :             idx = k;
    2068             :         }
    2069             :     }
    2070             : 
    2071             :     /* calculate the quantized LSF vector */
    2072     2680152 :     for ( j = 0; j < M; j++ )
    2073             :     {
    2074     2522496 :         qlsf[j] = ( 1.0f - ratio[idx * M + j] ) * qlsf0[j] + ratio[idx * M + j] * qlsf1[j];
    2075             : 
    2076     2522496 :         if ( j > 0 && j < M && qlsf[j] < qlsf[j - 1] + LSF_GAP_MID )
    2077             :         {
    2078       14822 :             qlsf[j] = qlsf[j - 1] + LSF_GAP_MID;
    2079             :         }
    2080             :     }
    2081             : 
    2082      157656 :     reorder_lsf( qlsf, LSF_GAP_MID, M, int_fs );
    2083             : 
    2084             :     /* convert LSFs back to LSPs */
    2085      157656 :     lsf2lsp( qlsf, lsp, M, int_fs );
    2086             : 
    2087      157656 :     push_indice( hBstr, IND_MID_FRAME_LSF_INDEX, idx, nb_bits );
    2088             : 
    2089      157656 :     return;
    2090             : }

Generated by: LCOV version 1.14