www.pudn.com > SMV_Code.rar > encoder.c


/*=========================================================================*/ 
/* Each of the companies; Ericsson, Lucent, Mindspeed, Motorola, Nokia,    */ 
/* Nortel Networks, and Qualcomm (hereinafter referred to individually as  */ 
/* “Source” or collectively as “Sources”) do hereby state:                 */ 
/*                                                                         */ 
/* To the extent to which the Source(s) may legally and freely do so,      */ 
/* the Source(s), upon submission of a Contribution, grant(s) a free,      */ 
/* irrevocable, non-exclusive, license to the Third Generation Partnership */ 
/* Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA,     */ 
/* TTA, and TTC, under the Source’s copyright or copyright license rights  */ 
/* in the Contribution, to, in whole or in part, copy, make derivative     */ 
/* works, perform, display and distribute the Contribution and derivative  */ 
/* works thereof consistent with 3GPP2’s and each Organizational Partner’s */ 
/* policies and procedures, with the right to (i) sublicense the foregoing */ 
/* rights consistent with 3GPP2’s and each Organizational Partner’s        */ 
/* policies and procedures and (ii) copyright and sell, if applicable) in  */ 
/* 3GPP2's name or each Organizational Partner’s name any 3GPP2 or         */ 
/* transposed Publication even though this Publication may contain the     */ 
/* Contribution or a derivative work thereof.  The Contribution shall      */ 
/* disclose any known limitations on the Source’s rights to license as     */ 
/* herein provided.                                                        */ 
/*                                                                         */ 
/* When a Contribution is submitted by the Source(s) to assist the         */ 
/* formulating groups of 3GPP2 or any of its Organizational Partners,      */ 
/* it is proposed to the Committee as a basis for discussion and is not    */ 
/* to be construed as a binding proposal on the Source(s).  The Source(s)  */ 
/* specifically reserve(s) the right to amend or modify the material       */ 
/* contained in the Contribution. Nothing contained in the Contribution    */ 
/* shall, except as herein expressly provided, be construed as conferring  */ 
/* by implication, estoppel or otherwise, any license or right under       */ 
/* (i) any existing or later issuing patent, whether or not the use of     */ 
/* information in the document necessarily employs an invention of any     */ 
/* existing or later issued patent, (ii) any copyright, (iii) any          */ 
/* trademark, or (iv) any other intellectual property right.               */ 
/*                                                                         */ 
/* With respect to the Software necessary for the practice of any or all   */ 
/* Normative portions of the Selectable Mode Vocoder (SMV) as it exists on */ 
/* the date of submittal of this form, should the SMV be approved as a     */ 
/* Specification or Report by 3GPP2, or as a transposed Standard by any of */ 
/* the 3GPP2’s Organizational Partners, the Source(s) state(s) that a      */ 
/* worldwide license to reproduce, use and distribute the Software, the    */ 
/* license rights to which are held by the Source(s), will be made         */ 
/* available to applicants under terms and conditions that are reasonable  */ 
/* and non-discriminatory, which may include monetary compensation,        */ 
/* and only to the extent necessary for the practice of any or all of the  */ 
/* Normative portions of the SMV or the field of use of practice of the    */ 
/* SMV Specification, Report, or Standard.  The statement contained above  */ 
/* is irrevocable and shall be binding upon the Source(s).  In the event   */ 
/* the rights of the Source(s) in and to copyright or copyright license    */ 
/* rights subject to such commitment are assigned or transferred,          */ 
/* the Source(s) shall notify the assignee or transferee of the existence  */ 
/* of such commitments.                                                    */ 
/*=========================================================================*/ 
/*                                                                   */ 
/*-------------------------------------------------------------------*/ 
/*===================================================================*/ 
/* FILE: encoder.c                                                   */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE : Speech Coding @ Selectable Bitrate (SMV)                */ 
/*===================================================================*/ 
 
/*----------------------------------------------------------------------------*/ 
/*-------------------------------- INCLUDE -----------------------------------*/ 
/*----------------------------------------------------------------------------*/ 
 
#include "typedef.h" 
#include "main.h" 
#include "const.h" 
#include "gputil.h" 
#include "mcutil.h" 
 
#include "ext_var.h" 
 
#include "lib_ini.h" 
 
#include "lib_qlsf.h" 
#include "lib_geq.h" 
#include "lib_lpc.h" 
#include "lib_cla.h" 
#include "lib_snr.h" 
#include "lib_pwf.h" 
#include "lib_bit.h" 
#include "lib_pit.h" 
#include "lib_smo.h" 
#include "lib_prc.h" 
#include "lib_flt.h" 
#include "lib_fcs.h" 
#include "lib_ltp.h" 
#include "lib_ppr.h" 
#include "lib_ppp.h" 
#include "lib_gcb.h" 
 
#ifdef VAD_A 
#include "lib_vad.h" 
#endif 
 
#include "lib_mus.h" 
 
#include "nelp.h" 
 
#ifdef VAD_B 
 
#include "lib_evad.h" 
 
#endif 
 
 
#ifdef WMOPS 
 
#include "lib_wmp.h" 
 
#endif 
 
/*----------------------------------------------------------------------------*/ 
/*-------------------------------- FUNCTIONS ---------------------------------*/ 
/*----------------------------------------------------------------------------*/ 
 
/*===================================================================*/ 
/* FUNCTION  : enc_smv_frame ().                                     */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE   : Performs processing on one frame.                     */ 
/*-------------------------------------------------------------------*/ 
/* ALGORITHM :                                                       */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*      _ (FLOAT64 []) sigpp        : input frame.                   */ 
/*      _ (INT16     ) smv_mode     : selected SMV mode.             */ 
/*      _ (INT16     ) half_rate_max: HALF-RATE-MAX mode flag.       */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*           _ (INT16   []) PackedWords : output frame bitstream.    */ 
/*           _ (FLOAT64 []) enc_sigout  : output processed frame.    */ 
/*           _ (INT16    *) FlatSpFlag  : flat input speech flag.    */ 
/*           _ (FLOAT64  *) avg_rate    : average bit rate.          */ 
/*           _ (INT16    *) VoicingClass: Frame classification.      */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*               _ None.                                             */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*               _ None.                                             */ 
/*===================================================================*/ 
 
void enc_smv_frame (FLOAT64 sigpp [], INT16 PackedWords [],  
		    FLOAT64 enc_sigout [], INT16 smv_mode, 
		    INT16 *FlatSpFlag, INT16 half_rate_max,  
		    FLOAT64 *avg_rate, INT16 *VoicingClass) 
	{ 
	 /*-------------------------------------------------------------------*/ 
	 /*                        Misc. parameters                           */ 
	 /*-------------------------------------------------------------------*/ 
 
	  FLOAT64 *px; 
	  FLOAT64 g1, g2, val1; 
 
	  INT16	 i_sf, i, j, k, l, i_s, l_sf, n_sf; 
	  FLOAT64 x; 
 
	  /*------------------------------------------------------------------*/ 
	  /*              Variables for Voice Activity Detection              */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef VAD_A 
	  FLOAT64 norm_energy, nnorm_energy, lags_std; 
#endif 
 
	  /*------------------------------------------------------------------*/ 
	  /*              Variables for Music Activity Detection              */ 
	  /*------------------------------------------------------------------*/ 
 
	  INT16   music_flg; 
 
	  FLOAT64 delta_lsf; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                   Variables for Pitch Open Loop                  */ 
	  /*------------------------------------------------------------------*/ 
 
	  INT16   low_pit, high_pit, lagl = 20; 
	  FLOAT64 *Rp_sub; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                   Variables for classification                   */ 
	  /*------------------------------------------------------------------*/ 
 
          INT16	 LPC_OnSet;   
	  INT16	 NoisyV_flag, OnSetFlag;   
	  FLOAT64 FrmResEng, *ResEng; 
	  FLOAT64 lpcgain_q; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                   Variables for LPC smoothing                    */ 
	  /*------------------------------------------------------------------*/ 
	  
	  FLOAT64 beta_frm, beta_sub; 
	  FLOAT64 *refl0; 
          FLOAT64 *lpc_g; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                     Variables for Smoothing                      */ 
	  /*------------------------------------------------------------------*/ 
 
	  INT16	 exc_mode; 
 
	  /*------------------------------------------------------------------*/ 
	  /*           Parameters for LPC analysis and quantization           */ 
	  /*------------------------------------------------------------------*/ 
 
	  FLOAT64 *LP_CF; 
	  FLOAT64 *lsf_int; 
	  FLOAT64 *rxx, **refl; 
	  FLOAT64 *lsf_new2, *lsf_new3, *lsfq_new; 
	  FLOAT64 **pdcf, **pdcfq; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                Variables for Pitch Interpolation                 */ 
	  /*------------------------------------------------------------------*/ 
 
	  FLOAT64 *ForPitch; 
 
	  /*------------------------------------------------------------------*/ 
	  /*               Parameters for pitch pre-processing                */ 
	  /*------------------------------------------------------------------*/ 
 
	  FLOAT64 gp_pp[N_SF_MAX]; 
	  FLOAT64 *wspeech; 
 
	  FLOAT64 **wpdcf_zero, **wpdcf_pole; 
 
	  /*------------------------------------------------------------------*/ 
	  /*              Parameters for Closed Loop processing               */ 
	  /*------------------------------------------------------------------*/ 
 
	  FLOAT64 **unfcod, **fcod; 
	  FLOAT64 **qua_fcod, **qua_unfcod; 
 
	  FLOAT64 *hh, *hh_m; 
	  FLOAT64 *Tg, *Tg_frm, *Tgs, *Tgs_m; 
 
	  FLOAT64 *res2; 
	  FLOAT64 nsr = 0.0; 
	 
	  FLOAT64 Rp_Wn, frm_sharp; 
	  FLOAT64 *NewTg; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                       Parameters for Gain VQ                     */ 
	  /*------------------------------------------------------------------*/ 
 
	  FLOAT64 gainQ [N_SF_MAX]; 
	  FLOAT64 *Ptr_gp_tab; 
	  FLOAT64 *qua_synth_mem = NULL, *qua_dif_mem = NULL,  
	          *qua_target_mem = NULL, *qua_ext = NULL; 
	  FLOAT64 **qua_gainQ;  
 
	  /*------------------------------------------------------------------*/ 
	  /*                  Variables for Signal Sinthesis                  */ 
	  /*------------------------------------------------------------------*/ 
 
	  FLOAT64 sigsyn[L_SF]; 
	  FLOAT64 qua_sigsyn[L_SF+NP]; 
 
	  /*------------------------------------------------------------------*/ 
	  /*               Parameters for bitstream processing                */ 
	  /*------------------------------------------------------------------*/ 
 
	  INT16 seed_enc_2k=0; 
	  INT16 parm[3]; 
	  INT16 shft_count; 
 
	  INT16   PackWdsPtr[2]; 
	  PARAMETER channel; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                   Variables for the VAD_B                        */ 
	  /*------------------------------------------------------------------*/ 
 
 
#ifdef VAD_B 
	  FLOAT64 *rxx4evad; 
	  FLOAT64 Rp_evrc; 
	  static ENCODER_MEM vad_mem; 
#endif 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤                 ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤        ENCODER       ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤                 ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Allocate temporary memory                     */ 
	  /*------------------------------------------------------------------*/ 
 
	  rxx      = dvector (0, NP); 
	  refl     = dmatrix (0, N_SF_MAX, 0, NP-1); 
	  pdcf     = dmatrix (0, N_SF_MAX, 0, NP-1); 
	  Rp_sub   = dvector (0, N_SF_MAX-1); 
	  refl0 = dvector (0, N_SF_MAX); 
	  lpc_g = dvector (0, N_SF_MAX); 
	  MaxIdx_enc = svector (0, MAXPN-1); 
	  lsf_int = dvector (0, NP-1); 
	  lsf_new2 = dvector (0, NP-1); 
	  lsf_new3 = dvector (0, NP-1); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                        Pointers set-up                           */ 
	  /*------------------------------------------------------------------*/ 
 
	  INI_pointer_setup_enc  (); 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ LPC ANALYSIS ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
	  
	  /*------------------------------------------------------------------*/ 
	  /*                        Second subframe LPC                       */ 
	  /*------------------------------------------------------------------*/ 
 
	  LPC_analysis(L_LPC, sigpp, lpc_window, rxx, bwe_factor,  
		       refl[1], &pderr, pdcf[1], lsf_new2, NP, NP, &lpc_g[1], 
		       (*FlatSpFlag)); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                        First subframe LPC                        */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	  WMP_cnt_add(2*NP); 
	  WMP_cnt_shift(2*NP); 
	  WMP_cnt_move(2*NP); 
#endif 
	  for (i = 0; i < NP; i++) 
	    refl[0][i] = 0.5*(refl[1][i] + refl_mem[i]);	 
 
	  for (i = 0; i < NP; i++) 
	    lsf_int [i] = 0.5*(lsf_new[i] + lsf_new2[i]); 
 
	  LPC_lsftop(lsf_int, pdcf[0], NP); 
 
          lpc_g[0]=0.5*(lpc_g[1]+lpcgain); 
 
	  /*------------------------------------------------------------------*/ 
	  /*      Fourth subframe LPC analysis, used for LSF quantization     */ 
	  /*------------------------------------------------------------------*/ 
 
 
#if (defined (VAD_B) && !defined(VAD_A)) 
 
	  rxx4evad = dvector (0, NP_EVAD+1); 
 
	  LPC_analysis (L_LPC, sigpp+L_PP-L_LPC, lpc_window1, rxx4evad,  
			bwe_factor, refl[N_SF4-1], &pderr, pdcf[N_SF4-1], 
			lsf_new, NP, NP_EVAD, &lpcgain, (*FlatSpFlag)); 
 
#endif 
#if (defined (VAD_A) && !defined(VAD_B)) 
	  LPC_analysis (L_LPC, sigpp+L_PP-L_LPC, lpc_window1, rxx, bwe_factor, 
			refl[N_SF4-1], &pderr, pdcf[N_SF4-1], lsf_new, NP, NP, 
			&lpcgain, (*FlatSpFlag)); 
 
#endif 
 
          lpc_g[N_SF4-1] = lpcgain; 
 
	  /*------------------------------------------------------------------*/ 
	  /*             VAD based on the fourth subframe LPC                 */ 
	  /*------------------------------------------------------------------*/ 
 
	  flag_vad_mem [1] = flag_vad_mem [0]; 
	  flag_vad_mem [0] = Vad; 
 
	  px = sigpp + L_PP-L_FRM-L_LPCLHD; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                        for Music detection                       */ 
	  /*------------------------------------------------------------------*/ 
 
	  delta_lsf = lsf_new[1] -lsf_new[0];  
          mlsf0 = 0.75*mlsf0+0.25*lsf_new[0]; 
 
#if (defined (VAD_A) && !defined(VAD_B)) 
 
	  VAD_voice_detection (px, refl[N_SF4-1], pderr, lag_buf, pgain_buf, 
			       lsf_new, rxx, frm_count, &Vad, flag_vad_mem, 
			       &norm_energy, &nnorm_energy, &lags_std); 
  
#endif 
 
	  /*------------------------------------------------------------------*/ 
	  /*                        Music detection                           */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
          WMP_fwc (""); 
          WMP_cnt_mac(1); 
          WMP_cnt_logic(1); 
#endif 
 
#if (defined (VAD_A) && !defined(VAD_B)) 
          music_flg = MUS_musdetect (delta_lsf, norm_energy, nnorm_energy,  
				     lags_std, lsf_new[0], mlsf0,  
				     refl[N_SF4-1], pcorr_buf, frm_count, 
				     flag_vad_mem[0], &Vad); 
 
          Vad = (Vad || music_flg); 
 
#ifdef WMOPS 
          WMP_fwc("Music Detector"); 
#endif 
 
#endif	   
 
	  /*------------------------------------------------------------------*/ 
	  /*                       Third subframe LPC                         */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	  WMP_cnt_add(2*NP); 
	  WMP_cnt_shift(2*NP); 
	  WMP_cnt_move(2*NP); 
#endif 
	  for (i = 0; i < NP; i++) 
	    refl[2][i] = 0.5*(refl[1][i] + refl[N_SF4-1][i]);	 
 
	  for (i = 0; i < NP; i++) 
	    lsf_int [i] = 0.5*(lsf_new[i]+lsf_new2[i]); 
 
	  LPC_lsftop(lsf_int, pdcf[2], NP); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                      LPC analysis for Look-Ahead                 */ 
	  /*------------------------------------------------------------------*/ 
 
	  energy_m = energy;   
      
	  LPC_analysis (L_LPC, sigpp+L_PP-L_LPC, lpc_window2, rxx, bwe_factor,  
			refl[N_SF4], &pderr, pdcf[N_SF4], lsf_new3, NP, NP,  
			&lpc_g[N_SF4], (*FlatSpFlag)); 
 
	  energy = rxx[0]; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Deallocate temporary memory                   */ 
	  /*------------------------------------------------------------------*/ 
 
	  free_dvector (rxx,     0, NP); 
	  free_dvector (lsf_int, 0, NP-1); 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ WEIGHTING SPEECH AND CLASSIFICATION ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*                     Allocate temporary memory                    */ 
	  /*------------------------------------------------------------------*/ 
 
	  wspeech  = dvector (0, L_OLPIT-1); 
	  wpdcf_pole  = dmatrix (0, N_SF_MAX, 0, NP-1); 
	  wpdcf_zero  = dmatrix (0, N_SF_MAX, 0, NP-1); 
	  ResEng = dvector (0, N_SF2-1); 
	  LP_CF = dvector (0, N_SF_MAX); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                Update the weighted speech buffer                 */ 
	  /*------------------------------------------------------------------*/ 
 
	  cpy_dvector(wspeech_mem, wspeech, 0, L_OLPIT-L_FRM-L_LPCLHD-1); 
	   
	  /*------------------------------------------------------------------*/ 
	  /*                    Generate the residual                         */ 
	  /*------------------------------------------------------------------*/ 
 
          for (i = 0; i <= N_SF4; i++) 
	    refl0[i] = refl[i][0]; 
	  lpc_g[2] = 0.5*(lpc_g[1]+lpc_g[3]); 
 
	  PWF_WeightingFilters (pdcf, refl0, lpc_g, GAMMA1, GAMMA2, N_SF4,  
				wpdcf_zero, wpdcf_pole); 
 
	  PWF_speech_to_residu (sigpp, wspeech, wpdcf_zero); 
 
	  /*------------------------------------------------------------------*/ 
	  /*          Temporal noise-like unvoied speech detector             */ 
	  /*------------------------------------------------------------------*/ 
  
#ifdef WMOPS 
	  WMP_fwc(""); 
#endif 
	   
	  VUV = 3; 
	  l = L_FRM + L_LPCLHD; 
	   
	  CLA_NoisUnvoiceDetect (wspeech+ L_OLPIT-l, sigpp+L_PP-l, &VUV, ResEng, 
				 &frm_sharp); 
	   
#ifdef WMOPS 
	  WMP_fwc("CLA_NoisUnvoiceDetect"); 
#endif 
	  FrmResEng = ResEng[0] + ResEng[1]; 
       
	  /*------------------------------------------------------------------*/ 
	  /*                  Generate the weighted speech                    */ 
	  /*------------------------------------------------------------------*/ 
 
	  PWF_residu_to_wspeech ((*FlatSpFlag), wspeech, wspeech, 
				 wpdcf_pole, refl0, LP_CF, ws_syn_buf,  
				 &Z1_ws_1); 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ OPEN-LOOP INTER PITCH LAG SEARCH ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
#ifdef WMOPS 
	  WMP_fwc(""); 
#endif 
 
	  PIT_ol_2pitchs (wspeech, ol_lag, Rp_sub, frame_class); 
 
#if (defined (VAD_B) && !defined(VAD_A)) 
 
	  Rp_evrc=Rp_sub[1]; 
	  Vad = EVAD_voice_detection (smv_mode, rxx4evad, 4, 1, Rp_evrc,  
				      &vad_mem); 
	  Vad = (Vad > 1) ? 1:0; 
 
          music_flg = MUS_musdetect (delta_lsf, rxx4evad[0], lsf_new[0], mlsf0, 
				     refl[N_SF4-1], lag_buf, pcorr_buf,  
				     frm_count, flag_vad_mem[0], &Vad); 
 
	  Vad = (Vad || music_flg); 
 
	 /*------------------------------------------------------------------*/ 
	 /*                    Deallocate temporary memory                   */ 
	 /*------------------------------------------------------------------*/ 
 
	  free_dvector (rxx4evad, 0, NP_EVAD+1); 
 
#endif 
 
	  for (i = 0; i < 3; i++) 
	    pcorr_buf[i] = pcorr_buf[i+2]; 
 
	  pcorr_buf[3] = Rp_sub[0]; 
	  pcorr_buf[4] = Rp_sub[1]; 
 
#ifdef WMOPS 
	  WMP_fwc("PIT_ol_2pitchs"); 
#endif 
 
#ifdef WMOPS 
	  WMP_cnt_move(4); 
#endif 
	  for (i = 0; i < 3; i++) 
	    lag_buf[i] = lag_buf[i+2]; 
 
	  lag_buf[3] = ol_lag[0]; 
	  lag_buf[4] = ol_lag[1]; 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ CLASSIFICATON ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
#ifdef WMOPS 
	  WMP_cnt_move(1); 
#endif 
	  frame_class_m = frame_class; 
 
#ifdef WMOPS 
	  WMP_fwc(""); 
#endif 
 
	  frame_class = CLA_signal_classifier(sigpp + L_PP-L_FRM - L_LPCLHD, 
					      Rp_sub[1], (FLOAT64)(ol_lag[1]),  
					      Vad);  
 
#ifdef WMOPS 
	  WMP_fwc("CLA_signal_classifier"); 
#endif 
 
	  /*-----------------------------------------------------------------*/ 
	  /*                      Background Noise Level                     */ 
	  /*-----------------------------------------------------------------*/  
	 
	  SNR_Calc_NSR_enc (sigpp+L_PP-L_FRM, lsf_new, Vad, frame_class, &nsr); 
 
	  /*-----------------------------------------------------------------*/ 
	  /*                        Class correction                         */ 
	  /*-----------------------------------------------------------------*/  
 
#ifdef WMOPS 
	  WMP_fwc(""); 
#endif 
 
	  CLA_Class_Correct(music_flg, &frame_class, &frame_class_pp, &Vad,  
			    &VUV, &OnSetFlag, &LPC_OnSet, frame_class_m,  
			    Rp_sub, nsr, refl, frm_sharp, smv_mode, energy_m, 
			    energy, FrmResEng, lpcgain, &NoisyV_flag); 
	   
 
#ifdef WMOPS 
	WMP_fwc("CLA_Class_Correct"); 
#endif 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ CODEC BIT-RATE SELECTION ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
        /*-------------------------------------------------------------------*/ 
	/*                        Fixed rate selection                       */ 
      	/*-------------------------------------------------------------------*/  
 
#ifdef WMOPS 
        WMP_cnt_move(3); 
        WMP_cnt_test(5); 
#endif 
	fix_rate_enc_mem = fix_rate; 
 
	if (smv_mode <= 1) 
	  fix_rate = RATE8_5K; 
	else 
	  fix_rate = RATE4_0K; 
	 
	 
#ifdef WMOPS 
	WMP_fwc(""); 
#endif 
	 
	CLA_Rate_Select (music_flg, smv_mode, frame_class,  
			 frame_class_m, OnSetFlag, Rp_sub, nsr, 
			 frm_sharp, refl[2][0], FrmResEng, &fix_rate);	 
 
 
#ifdef WMOPS 
	WMP_fwc("CLA_Rate_Select"); 
#endif	       
	 
	if ((half_rate_max==1) && (fix_rate == RATE8_5K)) 
	  fix_rate = RATE4_0K;	 
 
#ifdef DATA 
	/*--------------------------------------------------------------------*/ 
	/*                If data present, limit rate to half                 */ 
	/*--------------------------------------------------------------------*/ 
 
	if ((num_data > 0) && (data_mode == 2)) 
	  { 
	    half_rate_max = 1; 
	    fix_rate = RATE4_0K; 
	  } 
#endif 
	 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FINE PITCH AND INTERPOLATION ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*                     Temporary memory allocation                  */ 
	  /*------------------------------------------------------------------*/ 
 
	  ForPitch = dvector (0, L_FRM+L_FRM/2-1); 
 
	  if (((fix_rate == RATE4_0K) || (fix_rate == RATE2_0K)) &&  
	      (ol_lag[1] > HI_LAG) && (frame_class_pp>0)) 
	    frame_class_pp = 0; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                      Forward fine pitch                          */ 
	  /*------------------------------------------------------------------*/ 
     
#ifdef WMOPS 
	  WMP_cnt_move(7); 
	  WMP_cnt_test(6); 
	  WMP_cnt_logic(3); 
#endif 
	  if (frame_class_pp > 0) 
	    {    
	      low_pit  = MAX(MIN_LAG, ol_lag[1] - 5); 
	       
	      if (fix_rate == RATE8_5K) 
		high_pit = MIN(HI_LAG2, ol_lag[1] + 5); 
	      else 
		high_pit = MIN(HI_LAG, ol_lag[1] + 5); 
 
	      pitch_f_mem [1] = pitch_f_mem [0]; 
	      pitch_f_mem [0] = pitchf_enc; 
	       
	       
	      lagl = PIT_FinePitch (fix_rate, low_pit, high_pit, wspeech, 
				    Delay_pp, &pitchf_enc, &pitch_index); 
	       
	      channel.idx_pitch[0] = pitch_index; 
	       
	      /*--------------------------------------------------------------*/ 
	      /*   Pitch interpolation and modify pitch index to avoid too    */ 
	      /*                        large delay                           */ 
	      /*--------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	      WMP_cnt_test (4); 
	      WMP_cnt_move (2); 
#endif 
	       
	      if ((fix_rate_enc_mem == RATE2_0K) ||  
		  (fix_rate_enc_mem == RATE0_8K))  
		{ 
		  pitch_f_mem [1] = pitchf_enc; 
		  pitch_f_mem [0] = pitchf_enc; 
		}  
	      else if (frame_class_m < STATIONARY_VOICED) 
		{ 
		  if (fix_rate_enc_mem != RATE8_5K) 
		    { 
		      pitch_f_mem [1] = lag_f[N_SF2-1]; 
		      pitch_f_mem [0] = lag_f[N_SF2-1]; 
		       
		    }	 
		  else 
		    { 
		      pitch_f_mem [1] = lag_f[N_SF4-1]; 
		      pitch_f_mem [0] = lag_f[N_SF4-1]; 
		    }  
		} 
	       
#ifdef WMOPS 
	      WMP_fwc(""); 
#endif 
	       
	      PIT_PitchInterpolat (pitchf_enc, pitch_f_mem,  ForPitch, 1); 
	       
#ifdef WMOPS 
	      WMP_fwc("PIT_PitchInterpolat"); 
#endif 
	    } 
 
	  /*------------------------------------------------------------------*/  
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ LT PROCESSING ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
 
#ifdef WMOPS 
	WMP_fwc(""); 
	WMP_cnt_test(3); 
	WMP_cnt_move(3); 
#endif 
 
	  /*------------------------------------------------------------------*/ 
	  /*                      Temporary memory allocation                 */ 
	  /*------------------------------------------------------------------*/ 
 
	  NewTg    = dvector (0, L_FRM+NP-1); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                 Update memory from the previous frame            */ 
	  /*------------------------------------------------------------------*/ 
	   
	  cpy_dvector (NewTg_mem, NewTg, 0, NP-1);  
	   
	  PPP_pitch_preproc (half_rate_max, smv_mode, fix_rate, (*FlatSpFlag), 
			     nsr, wspeech, NewTg+NP, ForPitch, &Delay_pp, 
			     frame_class_pp, &frame_class, &VUV, gp_pp); 
	 
	  /*------------------------------------------------------------------*/ 
	  /*                  Save memory for the next frame                  */ 
	  /*------------------------------------------------------------------*/ 
	   
	  cpy_dvector(wspeech+L_FRM, wspeech_mem, 0, L_OLPIT-L_FRM-L_LPCLHD-1); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                   Deallocate temporary memory                    */ 
	  /*------------------------------------------------------------------*/ 
 
	  free_dvector (wspeech, 0, L_OLPIT-1); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                         frame_class correction                   */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	  WMP_cnt_move(3); 
	  WMP_cnt_test(11); 
	  WMP_cnt_logic(8); 
#endif 
	  if ((VUV == 2) && (frame_class > NOISE_LIKE) && (OnSetFlag != 1)) 
	    frame_class = NOISE_LIKE; 
 
	  /*------------------------------------------------------------------*/ 
	  /*             frame_class == STATIONARY_VOICED only for            */ 
	  /*                  fix_rate == RATE4_0K or RATE8_5K                */ 
	  /*------------------------------------------------------------------*/ 
 
	  if ((frame_class == STATIONARY_VOICED) &&  
	      ((fix_rate_enc_mem == RATE2_0K) ||  
	      (fix_rate_enc_mem == RATE0_8K))) 
	          frame_class = 5; 
 
	  if ((frame_class == STATIONARY_VOICED) && (frame_class_m < ONSET)) 
	    frame_class = NON_STATIONARY_VOICED; 
    
	  /*------------------------------------------------------------------*/ 
	  /*        Disallow big lag change under stationary condition        */ 
	  /*------------------------------------------------------------------*/ 
 
	  if ((frame_class_m==STATIONARY_VOICED) &&  
	      (frame_class == STATIONARY_VOICED) &&  
	      (fix_rate>=RATE4_0K) && (fabs(pitchf_enc-pitch_f_mem[0])>15)) 
	    { 
	      fix_rate = RATE8_5K; 
	      frame_class = NON_STATIONARY_VOICED; 
	    } 
              
	  /*------------------------------------------------------------------*/ 
	  /*            Rate modification because of the complexity           */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	  WMP_cnt_move(7); 
	  WMP_cnt_test(14); 
	  WMP_cnt_logic(7); 
	  WMP_cnt_shift(2); 
	  WMP_cnt_test (9); 
	  WMP_cnt_move (2); 
	  WMP_cnt_shift(2); 
#endif 
	   
	  if ((smv_mode >= 1)  && (fix_rate == RATE8_5K) &&  
	      (frame_class == NOISE_LIKE)) 
	    { 
	      if(music_flg == 0) 
		fix_rate = RATE4_0K; 
	    } 
	   
	  if ((frame_class_pp > 0) && (frame_class < STATIONARY_VOICED) &&  
	      (fix_rate == RATE4_0K) && ((ol_lag[0] < 30) || (ol_lag[1] < 30))) 
	    { 
	      if(music_flg == 0) 
		fix_rate = RATE8_5K;  
	    } 
	   
#ifdef DATA 
	  /*-----------------------------------------------------------------*/ 
	  /*     Force the rate during data transmissions  or splt frame     */ 
	  /*-----------------------------------------------------------------*/ 
 
	  if (( num_data>0  )&& (data_mode == 2)) 
	    { 
	      half_rate_max = 1; 
	      fix_rate = RATE4_0K; 
	    } 
#endif 
	   
	  if ((half_rate_max == 1) && (fix_rate == RATE8_5K)) 
	    { 
	      fix_rate = RATE4_0K;	  
	      if (frame_class == STATIONARY_VOICED) 
		frame_class = NON_STATIONARY_VOICED; 
	       
	      if (frame_class_pp > 0) 
		{ 
		  if (ol_lag[0] < MIN_LAG2) 
		    ol_lag[0] *=2; 
		   
		  if (ol_lag[1] < MIN_LAG2) 
		    ol_lag[1] *=2; 
		} 
	    } 
	   
	  PRC_average_rate (fix_rate, avg_rate); 
	   
	   
	  channel.fix_rate = fix_rate; 
	   
	  /*------------------------------------------------------------------*/ 
	  /*                       Reset open-loop pitch                      */ 
	  /*------------------------------------------------------------------*/ 
	   
#ifdef WMOPS 
	  WMP_cnt_move(6); 
	  WMP_cnt_test(3); 
	  WMP_cnt_test(4); 
#endif 
	  if (fix_rate == RATE8_5K) 
	    { 
	      for (i = 0; i < 2; i++) 
		lag[i] = ol_lag[0]; 
	       
	      for (i = 2; i < N_SF4; i++) 
		lag[i] = ol_lag[1]; 
	    } 
	  else 
	    { 
	      lag[0] = ol_lag[0]; 
	      lag[1] = ol_lag[1]; 
	    } 
  
	  if (frame_class_pp > 0) 
	    { 
	      if (fix_rate == RATE8_5K) 
		lag[N_SF4-1] = lagl; 
	      else if (lagl >= 30) 
		lag[N_SF2-1] = lagl;  
	    } 
	   
	  /*------------------------------------------------------------------*/ 
	  /*               Quantize 3 stable LTP gains with 4bits             */ 
	  /*------------------------------------------------------------------*/ 
	   
#ifdef WMOPS 
	  WMP_cnt_move(8); 
	  WMP_cnt_mult(8); 
	  WMP_cnt_test(6); 
	  WMP_cnt_logic(2); 
#endif 
 
	  /*------------------------------------------------------------------*/ 
	  /*             frame_class == STATIONARY_VOICED only for            */ 
	  /*                  fix_rate == RATE4_0K or RATE8_5K                */ 
	  /*------------------------------------------------------------------*/ 
 
	  if (frame_class == STATIONARY_VOICED) 
	    { 
	      channel.idx_SVS_deci = 1; 
	      if (NoisyV_flag == 1) 
		for (i = 0; i < N_SF4; i++) 
		  gp_pp[i] *= 0.95; 
	      else if (NoisyV_flag == 2) 
		for (i = 0; i < N_SF4; i++) 
		  gp_pp[i] *= 0.92; 
	      if ((*FlatSpFlag) == 0) 
		{ 
		  if (smv_mode == 0) 
		  { 
		    for (i = 0; i < N_SF4; i++) 
		      gp_pp[i] *= 0.95; 
		  } 
		  else 
		    { 
		      for (i = 0; i < N_SF4; i++) 
			gp_pp[i] *= 0.97; 
		    } 
		} 
	       
	      if (fix_rate != RATE8_5K) 
		{ 
		  /*----------------------------------------------------------*/ 
		  /*                   4 bits to  quantize 3 gp               */ 
		  /*----------------------------------------------------------*/ 
		   
		  Ptr_gp_tab = gp3_tab; 
		  GEQ_Quant_PitchGain(gp_pp, TAB_SIZE_GVQ_3D, Ptr_gp_tab,  
				      GVQ_VEC_SIZE_3D,&(channel.idx_Gp_VQ)); 
		} 
	      else 
		{ 
		  /*----------------------------------------------------------*/ 
		  /*                  6 bits to  quantize 4 gp                */ 
		  /*----------------------------------------------------------*/ 
		   
		  Ptr_gp_tab = gp4_tab; 
		  GEQ_Quant_PitchGain(gp_pp, TAB_SIZE_GVQ_4D,  Ptr_gp_tab,  
				      GVQ_VEC_SIZE_4D,&(channel.idx_Gp_VQ)); 
		} 
 
	    } 
	  else 
	    channel.idx_SVS_deci = 0; 
	   
	  /*------------------------------------------------------------------*/ 
	  /*                        Back to speech domain                     */ 
	  /*------------------------------------------------------------------*/ 
 
	  PWF_wspeech_to_speech(NewTg+NP, wpdcf_zero, wpdcf_pole, LP_CF, 
				ms_syn_buf, tmp_ws_m, &Z1_ws_2); 
	   
	  /*------------------------------------------------------------------*/ 
	  /*                    Deallocate temporary memories                 */ 
	  /*------------------------------------------------------------------*/ 
	   
	  free_dvector (LP_CF, 0, N_SF_MAX); 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ LPC QUANTIZATION AND INTERPOLATION ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/*============================================================================*/ 
       
	  /*------------------------------------------------------------------*/ 
	  /*     Estimate frame based and initial subframe based smoothing    */ 
	  /*------------------------------------------------------------------*/ 
 
	  beta_frm = SMO_lsf_smooth_est (Vad, flag_vad_mem [0], refl[1][0],  
					 lsf_new); 
 
	  /*------------------------------------------------------------------*/ 
	  /*        Smooth LSF, smoothing not active for coder_mode >= 3      */ 
	  /*------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	  WMP_cnt_test (3); 
	   
	  WMP_cnt_move (NP); 
	  WMP_cnt_mult (NP); 
	  WMP_cnt_mac  (NP); 
	   
	  WMP_cnt_sub  (NP); 
	  WMP_cnt_mac  (NP); 
	  WMP_cnt_mult (NP); 
#endif 
	   
	  if (LPC_OnSet == 1 && (half_rate_max==1 || smv_mode==3) )  
	    { 
	      for (i = 0; i < NP; i++) 
		lsf_smooth[i] = beta_frm*lsf_smooth[i] +   
		  (1.0-beta_frm)*(0.75*lsf_new3[i]+ 0.25*lsf_new[i]); 
	    } 
	  else 
	    { 
	      for (i = 0; i < NP; i++) 
		lsf_smooth[i] = beta_frm*lsf_smooth[i] +   
		  (1.0-beta_frm)*lsf_new[i]; 
	    }  
	  free_dvector (lsf_new3, 0, NP-1); 
	   
	   
	  /*------------------------------------------------------------------*/ 
	  /*                        LSF quantization                          */ 
	  /*------------------------------------------------------------------*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Allocate temporary memories                   */ 
	  /*------------------------------------------------------------------*/ 
 
	  lsfq_new = dvector (0, NP-1); 
 
#ifdef WMOPS 
	  WMP_fwc(""); 
#endif 
 
	  LSF_Q_lsfqnt(lsf_smooth, lsfq_new, channel.idx_lsf, fix_rate); 
 
#ifdef WMOPS 
	  WMP_fwc("LSF_Q_lsfqnt"); 
#endif 
       
	  /*------------------------------------------------------------------*/ 
	  /* Interpolation of LSF and weighting filter coefficients extraction*/ 
	  /*------------------------------------------------------------------*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*                   Temporary memory allocation                    */ 
	  /*------------------------------------------------------------------*/ 
	   
	  pdcfq = dmatrix (0, N_SF_MAX, 0, NP-1); 
        
	  /*--------------------------------------------------------------------*/ 
	   
	  if ((fix_rate == RATE8_5K) || (fix_rate == RATE2_0K) || 
	      (fix_rate == RATE0_8K)) 
	    {	   
	       
	      /*--------------------------------------------------------------*/ 
	      /*  Interpolate the QUANTIZED lsf and get the LPC coefficients  */ 
	      /*--------------------------------------------------------------*/ 
#ifdef WMOPS 
	      WMP_cnt_test(2); 
#endif 
	       
	      if ((fix_rate==RATE8_5K) && (frame_class != STATIONARY_VOICED)) 
		LPC_adptive_interp(lsfq_new, lsf_new2, lsfq_old, pdcfq, 
				   &(channel.idx_lpc_int)); 
	      else  
		LPC_adptive_interp_dec(lsfq_new, lsfq_old, pdcfq, 0); 
	     
	      cpy_dvector(lsf_new, lsf_old, 0, NP-1); 
 
	      /*--------------------------------------------------------------*/ 
	    } 
	  else 
	    {            
	      if (frame_class == STATIONARY_VOICED) 
		{ 
		  /*----------------------------------------------------------*/ 
		  /*      Interpolate the QUANTIZED lsf and get the LPC       */ 
		  /*                      coefficients                        */ 
		  /*----------------------------------------------------------*/ 
		   
#ifdef WMOPS 
		  WMP_cnt_test(1); 
		  WMP_cnt_mac(3); 
		  WMP_cnt_mult(3+4*N_SF3*NP); 
		  WMP_cnt_move(3+2*N_SF3+2*N_SF3*NP); 
#endif 
		   
		  LPC_interpolation (lsfq_new, (FLOAT64 *)NULL, lsfq_old,  
				     pdcfq, 1, 1); 
		   
		  /*----------------------------------------------------------*/ 
		  /*     Interpolate the UNQUANTIZED lsf and get the LPC      */ 
		  /*                      coefficients                        */ 
		  /*----------------------------------------------------------*/ 
 
		  LPC_interpolation (lsf_new, lsf_new2, lsf_old, pdcf, 0, 1); 
		   
		  /*----------------------------------------------------------*/ 
	      
		  x = 6.5 / 40; 
		  refl0[0] = (1-x)*refl[0][0] + x*refl[1][0];  
		  refl0[1] = 0.5*(refl[1][0]  + refl[2][0]); 
		  refl0[2] = (1-x)*refl[3][0] + x*refl[2][0];  
		   
		  /*----------------------------------------------------------*/ 
		  /*  Perceptual weighting filter coefficients calculation    */ 
		  /*----------------------------------------------------------*/ 
		   
		  lpc_g[0] = (1-x)*lpc_g[0] + x*lpc_g[1];  
		  lpc_g[1] = 0.5*(lpc_g[1]  + lpc_g[2]); 
		  lpc_g[2] = (1-x)*lpc_g[3] + x*lpc_g[2];  
		  PWF_WeightingFilters (pdcf, refl0, lpc_g, GAMMA1, GAMMA2,  
					N_SF3-1, wpdcf_zero, wpdcf_pole); 
		} 
	      else 
		{ 
		   
		  /*----------------------------------------------------------*/ 
		  /*     Interpolate the QUANTIZED lsf and get the LPC        */ 
		  /*                    coefficients                          */ 
		  /*----------------------------------------------------------*/ 
		   
		  LPC_interpolation (lsfq_new, (FLOAT64 *)NULL, lsfq_old,  
				     pdcfq, 1, 0); 
		   
		  /*----------------------------------------------------------*/ 
		  /*     Interpolate the UNQUANTIZED lsf and get the LPC      */ 
		  /*                     coefficients                         */ 
		  /*----------------------------------------------------------*/ 
 
		  LPC_interpolation (lsf_new, lsf_new2, lsf_old, pdcf, 0, 0); 
		   
		  /*----------------------------------------------------------*/ 
 
#ifdef WMOPS 
		  WMP_cnt_add(4); 
		  WMP_cnt_shift(4); 
#endif 
		  refl0[0] = 0.5*(refl[0][0] + refl[1][0]);   
		  refl0[1] = 0.5*(refl[2][0] + refl[3][0]); 
		   
		  /*----------------------------------------------------------*/ 
		  /*  Perceptual weighting filter coefficients calculation    */ 
		  /*----------------------------------------------------------*/ 
 
		  lpc_g[0] = 0.5*(lpc_g[0] + lpc_g[1]);   
		  lpc_g[1] = 0.5*(lpc_g[2] + lpc_g[3]); 
		  PWF_WeightingFilters (pdcf, refl0, lpc_g, GAMMA1, GAMMA2, 
					N_SF2-1, wpdcf_zero, wpdcf_pole); 
		} 
	    } 
 
	  /*------------------------------------------------------------------*/ 
	  /*                  Save memory for the next frame                  */ 
	  /*------------------------------------------------------------------*/ 
 
	  cpy_dvector(refl[N_SF4-1], refl_mem,    0, NP-1); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Deallocate temporary memory                   */ 
	  /*------------------------------------------------------------------*/ 
	   
	  free_dmatrix (refl,     0, N_SF_MAX, 0, NP-1);	  
	  free_dmatrix (pdcf,     0, N_SF_MAX, 0, NP-1); 
	  free_dvector (lsf_new2, 0, NP-1); 
	  free_dvector (lpc_g, 0, N_SF_MAX); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                      Flat input speech identifier                */ 
	  /*------------------------------------------------------------------*/ 
	   
#ifdef WMOPS 
	  WMP_fwc(""); 
#endif 
 
	  /*------------------------------------------------------------------*/ 
	  /*     Determination of the flatness of the input speech            */ 
	  /*------------------------------------------------------------------*/ 
 
	  CLA_Identify_Input (channel.idx_SVS_deci, pdcfq[0], lsfq_new[0], 
			      pgain_past, &lpcgain_q, fix_rate, FlatSpFlag, 
			      &SVS_Count_enc, &Vad_0_Count_enc, &Av_value_enc); 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Deallocate temporary memory                   */ 
	  /*------------------------------------------------------------------*/ 
 
	  free_dvector (lsfq_new, 0, NP-1); 
 
#ifdef WMOPS 
	  WMP_fwc("CLA_Identify_Input"); 
#endif 
	   
	  if (fix_rate != RATE0_8K ) 
	    seed_enc = (INT16)0x0E76; 
 
	  if (fix_rate == RATE2_0K ) 
	    { 
	      parm[0] = channel.idx_lsf[0]; 
	      parm[1] = channel.idx_lsf[1]; 
	      parm[2] = channel.idx_lsf[2]; 
	      seed_enc_2k = 0x0000; 
	      shft_count = 14; 
	      for (j = 0; j < 3; j++) 
		{ 
		  shft_count -= bitno0_2k[j]; 
		  seed_enc_2k = seed_enc_2k ^(((long) parm[j] ) << shft_count); 
		} 
	    } 
	  else if (fix_rate == RATE0_8K ) 
	    { 
	      parm[0] = channel.idx_lsf[0]; 
	      parm[1] = channel.idx_lsf[1]; 
	      parm[2] = channel.idx_lsf[2]; 
	       
	      shft_count = 11; 
	      for (j = 0; j < 3; j++) 
		{ 
		  shft_count -= bitno1[j]; 
		  seed_enc = seed_enc ^(((long) parm[j] ) << shft_count); 
		} 
	    } 
	   
	  /*-----------------------------------------------------------------*/ 
	  /*            Gaussian excitation for very low bit rates           */ 
	  /*-----------------------------------------------------------------*/ 
 
	  /*-----------------------------------------------------------------*/ 
	  /*                  Temporary Memory allocation                    */ 
	  /*-----------------------------------------------------------------*/ 
 
	  qua_unfcod = dmatrix (0, 1, 0, L_FRM-1); 
	  qua_gainQ       = dmatrix (0, 1, 0, N_SF_MAX-1); 
 
	  /*-----------------------------------------------------------------*/ 
 
	  if (nsr > 0.125) 
	    x = 1.0 - 0.6*nsr; 
	  else 
	    x = 1.0; 
	   
	  if ((fix_rate == RATE0_8K) && (nsr > 0.125)) 
	    x = 0.4; 
	  if (frame_class == STATIONARY_VOICED) 
	    x = 1.0; 
	   
	  NoiseGainFactor = 0.5*NoiseGainFactor + 0.5*x; 
	   
	  if ((fix_rate == RATE2_0K) || (fix_rate == RATE0_8K)) 
	    { 
	      if (fix_rate == RATE2_0K) 
		{ 
		  if ((fix_rate_enc_mem ==RATE8_5K ||  
		       fix_rate_enc_mem == RATE0_8K)) 
		    { 
		      GEQ_update_mem_4d_to_2d(); 
		    } 
		  else if( fix_rate_enc_mem == RATE4_0K &&  
			   frame_class_m == STATIONARY_VOICED) 
		    { 
		      GEQ_update_mem_3d_to_2d(); 
		    } 
#ifdef WMOPS 
		  WMP_fwc(""); 
		   
#endif 
		   
		  nelp_encoder(seed_enc_2k,fix_rate_enc_mem,NewTg, pdcfq,  
			       qua_unfcod[1], qua_gainQ[1],  
			       channel.idx_gainVQ); 
		   
#ifdef WMOPS 
		   
		  WMP_fwc("nelp_encoder"); 
#endif 
		} 
	      else 
		{ 
		  GCB_gauss_excit (&seed_enc, fix_rate, fix_rate_enc_mem, 
				   ResEng, qua_unfcod[1], qua_gainQ[1]); 
		   
		  qua_gainQ[1][0] *= NoiseGainFactor; 
		 
		  /*----------------------------------------------------------*/ 
		  /*            Quantize  'qua_gainQ[1][0]'  5 bits           */ 
		  /*----------------------------------------------------------*/ 
		   
		  if ((fix_rate_enc_mem == RATE4_0K) ||  
		      (fix_rate_enc_mem == RATE2_0K)) 
		    { 
		      if (frame_class_m != STATIONARY_VOICED) 
			GEQ_update_mem_2d_to_4d(); 
		      else 
			GEQ_update_mem_3d_to_4d(); 
		    }  
		   
		  /*----------------------------------------------------------*/ 
                  /*                   Eighth rate null                       */ 
		  /*----------------------------------------------------------*/ 
 
		  if ( (channel.idx_lsf[0] == LMS1_08k-1) && 
                       (channel.idx_lsf[1] == LMS2_08k-1) && 
                       (channel.idx_lsf[2] == LMS3_08k-1) ) 
                    channel.idx_gainVQ[0] = 1 ; 
                  else 
                    channel.idx_gainVQ[0] = 0 ; 
 
		  /*----------------------------------------------------------*/ 
 
		  GEQ_gainNSQMA_1_5(qua_unfcod[1], qua_gainQ,  
				    &(channel.idx_gainVQ[0])); 
		   
		  for (i = 1; i < N_SF4; i++) 
		    qua_gainQ[1][i] = qua_gainQ[1][0]; 
		} 
	    } 
	   
	  (*VoicingClass) = frame_class; 
 
/*============================================================================*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ SUB-FRAME PROCESSING ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ */ 
/* ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
/*============================================================================*/ 
 
/*----------------------------------------------------------------------------*/ 
/*-----------------------------VARIABLE DESCRIPTION --------------------------*/ 
/*----------------------------------------------------------------------------*/ 
/*	                                                                      */ 
/*	pdcfq      -  quantized prediction coefficients                       */ 
/*	wpdcf_pole -  poles of the perceptual weighting filter                */ 
/*	wpdcf_zero -  zeros of the perceptual weighting filter                */ 
/*	                                                                      */ 
/*	Tgs        -  target signal                                           */ 
/*	Tg         -  target signal for codebook search                       */ 
/*	              (no pitch contribution)                                 */ 
/*	hh         -  impulse response of the combined synthesis and          */ 
/*	              perceptual weighting filter                             */ 
/*	unfcod[0]  -  adaptive codebook (ACB) vector                          */ 
/*	unfcod[1]  -  algebraic codebook (CDBK) vector                        */ 
/*	fcod[0]    -  filtered ACB vector                                     */ 
/*	fcod[1]    -  filtered CDBK vector                                    */ 
/*	gainQ[0]   -  ACB gain                                                */ 
/*	gainQ[1]   -  CDBK gain                                               */ 
/*	gainQ[2]   -  CDBK gain                                               */ 
/*	                                                                      */ 
/*	ext        -  adaptive codebook                                       */ 
/*	lag[i_sf]  -  integral lag of the current subframe                    */ 
/*	lagf[i_sf] -  fractional lag of the current subframe                  */ 
/*----------------------------------------------------------------------------*/ 
 
 
/*----------------------------------------------------------------------------*/ 
/*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ First Loop With Unquantized Fixed CB Gains ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
/*----------------------------------------------------------------------------*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Allocate temporary memory                     */ 
	  /*------------------------------------------------------------------*/ 
 
	  qua_fcod  = dmatrix (0, 1, 0, L_FRM-1); 
	  unfcod    = dmatrix (0, 1, 0, L_SF-1); 
	  fcod      = dmatrix (0, 1, 0, L_SF-1); 
	  wsp_m_enc = dvector (0, L_WSP-1); 
	  hh_m      = dvector (0, L_FRM-1); 
	  Tg_frm    = dvector (0, L_FRM-1); 
	  Tg        = dvector (0, L_SF-1); 
	  Tgs       = dvector (0, L_SF-1); 
	  Tgs_m     = dvector (0, L_SF-1); 
	  res2      = dvector (0, L_SF-1); 
 
#ifdef WMOPS 
		 
	  WMP_cnt_test(3); 
#endif 
	  /*------------------------------------------------------------------*/ 
	  /*              frame_class == STATIONARY_VOICED only for           */ 
	  /*                   fix_rate == RATE4_0K or RATE8_5K               */ 
	  /*------------------------------------------------------------------*/ 
 
	  if (frame_class == STATIONARY_VOICED)  
	      { 
		qua_synth_mem	 = dvector (0, NP-1); 
		qua_dif_mem	 = dvector (0, L_SF+NP-1); 
		qua_target_mem = dvector (0, NP-1); 
		qua_ext	 = dvector (0, MAX_LAG+L_SF-1); 
		 
		/*------------------------------------------------------------*/ 
		/*     Modify the memories from previous frame for delayed    */ 
		/*                      gain quantization                     */ 
		/*------------------------------------------------------------*/ 
	     
		cpy_dvector (synth_mem,  qua_synth_mem,  0, NP-1); 
		cpy_dvector (dif_mem,    qua_dif_mem,    0, NP-1); 
		cpy_dvector (target_mem, qua_target_mem, 0, NP-1); 
		cpy_dvector (ext,        qua_ext,        0, MAX_LAG-1); 
	      } 
 
         /*-------------------------------------------------------------------*/ 
 
	 if (fix_rate != RATE4_0K) 
	    n_sf = N_SF4; 
         else 
	    { 
	      if (frame_class == STATIONARY_VOICED) 
		n_sf = N_SF3; 
	      else 
		n_sf = N_SF2; 
	    } 
         i_s = 0; 
	 
         for (i_sf = 0; i_sf < n_sf; i_sf++) 
	  { 
	    /*----------------------------------------------------------------*/ 
	    /*                 Set-up of the sub-frame parameters             */ 
	    /*----------------------------------------------------------------*/ 
 
	    if (fix_rate != RATE4_0K) 
	      l_sf = L_SF4;  
	    else 
	      {    
		if (frame_class == STATIONARY_VOICED) 
		  {     
		    if (i_sf == N_SF3-1) 
		      l_sf = L_SF3; 
		    else 
		      l_sf = L_SF0; 
		  } 
		else 
		  l_sf = L_SF; 
	      } 
 
	    /*----------------------------------------------------------------*/ 
	    /*    The impulse response with the all pole synthesis filter     */ 
	    /*              and  the percerptual weighting filter             */ 
	    /*----------------------------------------------------------------*/ 
 
	    hh =  hh_m+i_s; 
	    LPC_ImpulseResponse (hh, wpdcf_zero [i_sf], wpdcf_pole[i_sf], 
				 pdcfq [i_sf], l_sf); 
	 
	    /*----------------------------------------------------------------*/ 
	    /*                         Target signal                          */ 
	    /*----------------------------------------------------------------*/ 
		  
	    PRC_TargetSignal (wpdcf_zero[i_sf], wpdcf_pole[i_sf],  
			      pdcfq[i_sf], NewTg+i_s, ext+MAX_LAG, Tgs,  
			      dif_mem, target_mem, l_sf); 
	 
	    if (i_sf == 0) 
	      cpy_dvector (Tgs, Tgs_m, 0, l_sf-1); 
	     
	 
	    /*----------------------------------------------------------------*/ 
	    /*                        Ideal excitation                        */ 
	    /*----------------------------------------------------------------*/ 
 
	    PRC_Ideal_Excit (Tgs, pdcfq[i_sf], wpdcf_pole[i_sf], 
			     wpdcf_zero[i_sf], res2, l_sf); 
	 
	    /*----------------------------------------------------------------*/ 
	    /*                        LTP Contribution                        */ 
	    /*----------------------------------------------------------------*/ 
 
	    if ((fix_rate == RATE8_5K) || (fix_rate == RATE4_0K)) 
	      { 
		if (frame_class == STATIONARY_VOICED) 
		  { 
		    /*--------------------------------------------------------*/ 
		    /*                Pitch lag interpolation                 */ 
		    /*--------------------------------------------------------*/ 
#ifdef WMOPS 
		    WMP_fwc(""); 
#endif				  
		    LTP_PP_pitch_ext (ForPitch+i_s, Tgs, ext, hh, i_sf, 
				      unfcod[0], fcod[0], lag, lag_f, &Rp_Wn, 
				      l_sf); 
 
#ifdef WMOPS 
		    WMP_fwc("Type 1 LTP_PP_pitch_ext"); 
#endif 
		    /*--------------------------------------------------------*/ 
		    /*                      Pitch gain                        */ 
		    /*--------------------------------------------------------*/ 
 
		    gainQ[0] = gp_pp[i_sf]; 
		    if (NoisyV_flag < 2) 
		      gainQ[0] *= 0.3*Rp_Wn + 0.7;		     
		  } 
		else 
		  { 
		    /*--------------------------------------------------------*/ 
		    /*                   Close-loop search                    */ 
		    /*--------------------------------------------------------*/ 
#ifdef WMOPS 
		    WMP_cnt_test(1); 
		    WMP_fwc(""); 
#endif	 
		    if (fix_rate == RATE8_5K) 
		      {  
			LTP_close_8_5k_pitch (Tgs, ext, hh, i_sf, unfcod[0], 
					      fcod[0], lag, lag_f, &(gainQ[0]), 
					      &Rp_Wn, l_sf, frame_class_pp, 
					      channel.idx_pitch); 
 
 
#ifdef WMOPS 
			WMP_fwc("LTP_close_8_5k_pitch"); 
#endif 
		      } 
		    else  
		      { 
			LTP_close_7b_pitch (Tgs, ext, hh, i_sf, unfcod[0], 
					    fcod[0], lag, lag_f, &(gainQ[0]), 
					    &Rp_Wn, l_sf, frame_class_pp, 
					    channel.idx_pitch); 
 
 
#ifdef WMOPS 
			WMP_fwc("LTP_close_7b_pitch"); 
#endif 
		      } 
 
		    /*--------------------------------------------------------*/ 
		    /*          Modify the gainQ[0] to de-emphasize the       */ 
		    /*           pitch  contribution in the cdbk search       */ 
		    /*--------------------------------------------------------*/ 
 
		    gp_pp[i_sf] = gainQ[0]; 
		    gainQ[0] *= 0.5 * Rp_Wn + 0.5; 
		  } 
		/*------------------------------------------------------------*/ 
		/*          Save the results for the second loop              */ 
		/*------------------------------------------------------------*/ 
 
		cpy_dvector (unfcod[0], qua_unfcod[0]+i_s, 0, l_sf-1); 
		cpy_dvector (fcod[0], qua_fcod[0]+i_s, 0, l_sf-1); 
	      } 
	    else 
	      { 
		gainQ[0] = 0.0; 
		gp_pp[i_sf] = 0.0; 
		Rp_Wn = 0.0; 
		lag_f [i_sf] = lag [i_sf]; 
		ini_dvector(unfcod[0], 0, l_sf-1, 0.0); 
		ini_dvector(qua_unfcod[0]+i_s, 0, l_sf-1, 0.0); 
		ini_dvector(fcod[0], 0, l_sf-1, 0.0); 
		ini_dvector(qua_fcod[0], 0, l_sf-1, 0.0); 
	      } 
 
	    /*----------------------------------------------------------------*/ 
	    /*            Calculate the Target for codebook search            */ 
	    /*----------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	    WMP_cnt_mult(2*l_sf); 
	    WMP_cnt_mac(2*l_sf); 
	    WMP_cnt_move(2*l_sf); 
#endif 
	    for (i = 0; i < l_sf; i++) 
	      { 
		Tg[i] = Tgs[i] - gainQ[0]*fcod[0][i]; 
		res2[i] -= gainQ[0]*unfcod[0][i]; 
	      } 
	     
	    /*----------------------------------------------------------------*/ 
	    /*               Mode selection of fixed excitation:              */ 
	    /*                   mixed search vs pure Gaussian                */ 
	    /*----------------------------------------------------------------*/ 
 
	    beta_sub = SMO_excitation_analysis (NewTg+i_s+NP, res2, Vad, 
						lag_f[i_sf], refl0[i_sf], 
						&exc_mode, l_sf); 
 
	    /*----------------------------------------------------------------*/ 
	    /*                    Fixed Codebook search                       */ 
	    /*----------------------------------------------------------------*/ 
 
	    k = exc_mode*frame_class; 
	     
	    if (NoisyV_flag == 2) 
	      k = 2; 
	     
	    switch (fix_rate) 
	      { 
	       case RATE8_5K: 
		 if (frame_class == STATIONARY_VOICED) 
		   { 
#ifdef WMOPS 
		     WMP_fwc(""); 
#endif 
		     FCS_cdbk_search_8500BPS ( Tg, res2, hh, unfcod[1], l_sf,  
					       i_sf, lag[i_sf], k, gp_pp[i_sf], 
					       nsr, Rp_Wn, &channel, 1); 
#ifdef WMOPS 
		     WMP_fwc("FCS_cdbk_search_8500BPS"); 
#endif 
		   } 
		 else 
		   { 
#ifdef WMOPS 
		     WMP_fwc(""); 
#endif 
		     FCS_cdbk_search_8500BPS (Tg, res2, hh, unfcod[1], l_sf,  
					      i_sf, lag[i_sf], 
					      k, pgain_past, nsr, Rp_Wn, 
					      &channel, 0); 
#ifdef WMOPS 
		     WMP_fwc("FCS_cdbk_search_8500BPS"); 
#endif 
		   } 
		 break; 
		 
	       case RATE4_0K: 
		 
		 if (frame_class == STATIONARY_VOICED) 
		   { 
#ifdef WMOPS 
		     WMP_fwc(""); 
#endif 
		      
		     FCS_cdbk_search_4000BPS (ext, pdcfq[i_sf], Tg, res2, hh, 
					      unfcod[1], l_sf, i_sf, lag[i_sf], 
					      k, gp_pp[i_sf], nsr, Rp_Wn, 
					      fix_rate_enc_mem, lpcgain_q,  
					     &channel, 1); 
#ifdef WMOPS 
		     WMP_fwc("FCS_cdbk_search_4000BPS"); 
#endif 
		   } 
		 else 
		   {	   
		      
#ifdef WMOPS 
		     WMP_fwc(""); 
#endif 
		      
		     FCS_cdbk_search_4000BPS (ext, pdcfq[i_sf], Tg, res2, hh, 
					      unfcod[1], l_sf, i_sf, lag[i_sf], 
					      k,  pgain_past, nsr, Rp_Wn,  
					      fix_rate_enc_mem, lpcgain_q, 
					      &channel, 0); 
#ifdef WMOPS 
		     WMP_fwc("FCS_cdbk_search_4000BPS"); 
#endif 
		   } 
		 break; 
		       
	       case RATE2_0K: case RATE0_8K:	 
		 
		 cpy_dvector(qua_unfcod[1]+i_s, unfcod[1], 0, l_sf-1); 
		 gainQ[1] = qua_gainQ[1][i_sf]; 
		 if (fix_rate == RATE2_0K)  
		   gainQ[1] = qua_gainQ[1][i_sf]=1.0; 
		  
		 break; 
		  
	       default: nrerror ("Invalid fix_rate !!\n"); 
		 break; 
	      } 
 
	    /*----------------------------------------------------------------*/ 
	    /*              Filter the selected excitation vector             */ 
	    /*----------------------------------------------------------------*/ 
 
	    FLT_convolve(unfcod[1], hh, fcod[1], l_sf); 
 
	    /*----------------------------------------------------------------*/ 
	    /*                            Gains                               */ 
	    /*----------------------------------------------------------------*/ 
 
	    if ((fix_rate == RATE8_5K) || (fix_rate == RATE4_0K)) 
	      { 
		/*------------------------------------------------------------*/ 
		/*                   Unquantized gains                        */ 
		/*------------------------------------------------------------*/ 
 
		if ((frame_class < STATIONARY_VOICED) && (Rp_Wn < 0.75) ) 
		  GEQ_gain_reopt_2 (fcod[0], fcod[1], Tgs, gainQ, gainQ+1,  
				    l_sf); 
		else 
		  { 
		    gainQ[0] = gp_pp[i_sf]; 
#ifdef WMOPS 
		    WMP_cnt_mult(l_sf); 
		    WMP_cnt_mac(l_sf); 
		    WMP_cnt_move(l_sf); 
#endif 
		    for(i = 0; i < l_sf; i++) 
		      Tg[i] = Tgs[i] - gainQ[0]*fcod[0][i]; 
		     
		    dot_dvector (Tg, fcod[1], &gainQ[1], 0, l_sf-1); 
		     
		    dot_dvector (fcod[1], fcod[1], &val1, 0, l_sf-1); 
 
		    gainQ[1] /= MAX(val1, 0.01); 
		  } 
		 
		 
		/*------------------------------------------------------------*/ 
		/*                       Gain normalization                   */ 
		/*------------------------------------------------------------*/ 
 
		if (frame_class < STATIONARY_VOICED) 
		  { 
		    if (GainNormDeci == 0) 
		      beta_sub = 0.0; 
		    GainNormDeci = 0;	 
 
                    if ( (nsr > 0.125) || (frame_class <= NOISE_LIKE) ) 
		      { 
			GainNormDeci = 1; 
			if (fix_rate == RATE8_5K) 
			  x = ResEng[(short)(i_sf/2)]; 
			else 
			  x = ResEng[i_sf]; 
			 
			PRC_GainsNorm_Gc_Gp (nsr, x,  ext+MAX_LAG, gainQ,  
					     unfcod, fcod, Tgs, Vad*exc_mode, 
					     beta_sub, l_sf); 
		      } 
 
		    gainQ[0] *= NoiseGainFactor; 
		    gainQ[1] *= NoiseGainFactor; 
		  } 
		else 
		  { 
		    if (nsr > 0.25) 
		      PRC_GainNorm_Gc (ext+MAX_LAG, gainQ, unfcod, fcod, Tg, 
				       l_sf); 
		    GainNormDeci = 0; 
		  } 
		 
		 
		/*------------------------------------------------------------*/ 
		/*                     Gain Quantization                      */ 
		/*------------------------------------------------------------*/ 
 
		/*------------------------------------------------------------*/ 
		/*                      Limit pitch gain                      */ 
		/*------------------------------------------------------------*/ 
 
		if (gainQ[0] < 0.0) 
		  gainQ[0] = 0.0; 
		 	 
		if (gainQ[0] > 1.2) 
		  gainQ[0] = 1.2; 
 
		for (i = 0; i < LTP_BUFF_SIZE-1; i++) 
		  pgain_buf[i] = pgain_buf[i+1]; 
		 
		pgain_buf[LTP_BUFF_SIZE-1] = gainQ[0]; 
		 
		if (gainQ[1] < 0.0) 
		  FCS_ChangeSign (&channel, i_sf, l_sf, unfcod[1], fcod[1], 
				  gainQ+1);   
 
		/*------------------------------------------------------------*/ 
		 
		cpy_dvector (unfcod[1], qua_unfcod[1]+i_s, 0, l_sf-1); 
		cpy_dvector (fcod[1], qua_fcod[1]+i_s, 0, l_sf-1); 
 
		 
		wad_dvector (fcod[0], gainQ[0], fcod[1], gainQ[1], Tg, 0,  
			     l_sf-1); 
		 
		cpy_dvector (Tg, &Tg_frm[i_s], 0, l_sf-1); 
		  
		/*------------------------------------------------------------*/ 
 
		if (frame_class != STATIONARY_VOICED) 
		  { 
		    if ((fix_rate_enc_mem == RATE0_8K) && (i_sf == 0)) 
		      { 
			past_energyq_4d[1] = past_energyq_4d[0]; 
			past_energyq_4d[2] = past_energyq_4d[0]; 
			past_energyq_4d[3] = past_energyq_4d[0]; 
		      } 
		    if (fix_rate != RATE8_5K) 
		      { 
			if (((fix_rate_enc_mem == RATE8_5K) || 
			     (fix_rate_enc_mem == RATE0_8K)) && 
			    (i_sf == 0)) 
			  { 
			    GEQ_update_mem_4d_to_2d(); 
			  } 
			else if ((fix_rate_enc_mem == RATE4_0K) &&  
				 (frame_class_m == STATIONARY_VOICED) &&  
				 (i_sf == 0)) 
			  { 
			    GEQ_update_mem_3d_to_2d(); 
			  } 
#ifdef WMOPS 
			WMP_fwc(""); 
#endif                
			GEQ_gainVQMA_2 (Tg, unfcod, fcod, gainQ, 
					&(channel.idx_gainVQ[i_sf]), 
					l_sf, fix_rate); 
			 
#ifdef WMOPS 
			WMP_fwc("4K GEQ_gainVQMA_2"); 
#endif 
		      } 
		    else 
		      {  
			if (((fix_rate_enc_mem == RATE4_0K) ||  
			     (fix_rate_enc_mem == RATE2_0K)) &&  
			    (i_sf == 0)) 
			  { 
			    if (frame_class_m != STATIONARY_VOICED) 
			      GEQ_update_mem_2d_to_4d();              
			    else 
			      GEQ_update_mem_3d_to_4d();              
			  } 
#ifdef WMOPS 
			WMP_fwc(""); 
#endif 
			GEQ_gainVQMA_2 (Tg, unfcod, fcod, gainQ, 
					&(channel.idx_gainVQ[i_sf]), 
					l_sf, fix_rate); 
			 
#ifdef WMOPS 
			WMP_fwc("8K GEQ_gainVQMA_2"); 
#endif 
		      } 
		  } 
 
		/*------------------------------------------------------------*/ 
		/*    Update the past quantized pitch gain for cdbk search    */ 
		/*------------------------------------------------------------*/ 
 
		pgain_past = gainQ[0]; 
		if (pgain_past < PAST_PGAIN_MIN) 
		  pgain_past = PAST_PGAIN_MIN; 
		if (pgain_past > PAST_PGAIN_MAX)  
		  pgain_past = PAST_PGAIN_MAX; 
		 
		qua_gainQ[0][i_sf] = gainQ[0]; 
		qua_gainQ[1][i_sf] = gainQ[1]; 
	      } 
	    else 
	      { 
		/*------------------------------------------------------------*/ 
		/*            fix_rate==RATE2_0K || fix_rate==RATE0_8K        */ 
		/*------------------------------------------------------------*/ 
 
		GainNormDeci = 0; 
		gainQ[0] = 0.0; 
		pgain_past = gainQ[0]; 
		qua_gainQ[0][i_sf]=0; 
		gainQ[1]=qua_gainQ[1][i_sf]; 
		if (fix_rate== RATE2_0K)  
		  gainQ[1]=qua_gainQ[1][i_sf]=1.0; 
		cpy_dvector(fcod[1], qua_fcod[1]+i_s, 0, l_sf-1); 
	      }	 
	     
	    /*----------------------------------------------------------------*/ 
	    /*            Build the excitation and synthesized signal         */ 
	    /*----------------------------------------------------------------*/ 
 
	    wad_dvector (unfcod[0], gainQ[0], unfcod[1], gainQ[1],  
			 ext+MAX_LAG, 0, l_sf-1); 
	 
	    /*----------------------------------------------------------------*/ 
	    /*                         Update memory                          */ 
	    /*----------------------------------------------------------------*/ 
		  
	    FLT_filterAP (pdcfq[i_sf], ext+MAX_LAG,  sigsyn, synth_mem, 
			  NP, l_sf); 
 
#ifdef WMOPS 
	    WMP_cnt_mult(2*(l_sf-NP)); 
	    WMP_cnt_mac(2*(l_sf-NP)); 
	    WMP_cnt_sub(l_sf-NP); 
	    WMP_cnt_move(2*(l_sf-NP)); 
#endif 
	     
	    for (i = l_sf-NP, j=0; i < l_sf; i++,j++) 
	      { 
		dif_mem[j] = NewTg[i_s+NP+i] - sigsyn[i]; 
		target_mem[j] = Tgs[i] - gainQ[0]*fcod[0][i]  
		                            - gainQ[1]*fcod[1][i]; 
	      } 
	     
	    /*----------------------------------------------------------------*/ 
	    /*                   Update the adaptive codebook                 */ 
	    /*----------------------------------------------------------------*/ 
		  
	    cpy_dvector (ext+l_sf, ext, 0, MAX_LAG-1); 
		  
	    /*----------------------------------------------------------------*/ 
 
	    i_s += l_sf; 
         
	    /*================================================================*/ 
	  } 
 
	  /*------------------------------------------------------------------*/ 
	  /*                    Deallocate temporary memory                   */ 
	  /*------------------------------------------------------------------*/ 
 
	  free_dvector (wsp_m_enc, 0, L_WSP-1); 
	  free_dvector (ResEng, 0, N_SF2-1); 
	  free_dvector (refl0, 0, N_SF_MAX); 
	  free_dvector (Tg, 0, L_SF-1); 
	  free_dvector (res2, 0, L_SF-1); 
 
/*============================================================================*/ 
/*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Second Loop With Quantized Fixed CB Gains ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
/*============================================================================*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*              frame_class == STATIONARY_VOICED only for           */ 
	  /*                  fix_rate == RATE4_0K or RATE8_5K                */ 
	  /*------------------------------------------------------------------*/ 
 
	  if (frame_class == STATIONARY_VOICED) 
	    { 
	      /*--------------------------------------------------------------*/ 
	      /*                Quantize 3 fixed CB gains                     */ 
	      /*--------------------------------------------------------------*/ 
 
	      if (fix_rate != RATE8_5K) 
		{ 
		  if( ((fix_rate_enc_mem == RATE8_5K) ||  
		       (fix_rate_enc_mem == RATE0_8K))) 
		    GEQ_update_mem_4d_to_3d(); 
		  else if (frame_class_m != STATIONARY_VOICED) 
		    GEQ_update_mem_2d_to_3d(); 
		   
#ifdef WMOPS 
		  WMP_fwc(""); 
#endif 
		   
		  GEQ_gainVQMA_3(Tg_frm, qua_unfcod, qua_fcod, qua_gainQ,  
				 &(channel.idx_Gc_VQ)); 
		   
#ifdef WMOPS 
		  WMP_fwc("GEQ_gainVQMA_3"); 
#endif 
		} 
	      else 
		{ 
		  if( ((fix_rate_enc_mem == RATE4_0K) ||  
		       (fix_rate_enc_mem == RATE2_0K))) 
		    { 
		      if (frame_class_m != STATIONARY_VOICED) 
			GEQ_update_mem_2d_to_4d(); 
		      else 
			GEQ_update_mem_3d_to_4d(); 
		    } 
#ifdef WMOPS 
		  WMP_fwc(""); 
#endif	 
		   
		  GEQ_gainVQMA_4(Tg_frm, qua_unfcod, qua_fcod, qua_gainQ, 
				 &(channel.idx_Gc_VQ)); 
		   
#ifdef WMOPS 
		  WMP_fwc("GEQ_gainVQMA_4"); 
#endif 
		} 
	       
	      /*--------------------------------------------------------------*/ 
 
	      i_s = 0; 
	      for (i_sf = 0; i_sf < n_sf; i_sf++) 
		{ 
		  /*==========================================================*/ 
		   
		  if (fix_rate == RATE8_5K) 
		    l_sf = L_SF4;  
		  else 
		    {    
		      if (i_sf == N_SF3-1) 
			l_sf = L_SF3; 
		      else 
			l_sf = L_SF0; 
		    } 
		   
		  /*----------------------------------------------------------*/ 
		  /*                       Target signal                      */ 
		  /*----------------------------------------------------------*/ 
 
		  if (i_sf > 0) 
		    {                     
		      PRC_TargetSignal (wpdcf_zero[i_sf],  wpdcf_pole[i_sf], 
					pdcfq[i_sf], NewTg+i_s, ext+MAX_LAG, Tgs,  
					qua_dif_mem, qua_target_mem, l_sf); 
		    }     
		  else 
		    { 
		      cpy_dvector (Tgs_m, Tgs, 0, l_sf-1); 
		    } 
		   
		  /*----------------------------------------------------------*/ 
		  /*               Re-build adaptive CB excitation            */ 
		  /*----------------------------------------------------------*/ 
		      
		  if (i_sf > 0)  
		    { 
#ifdef WMOPS 
		      WMP_fwc(""); 
#endif 
		      LTP_PP_pitch_ext_decod (ForPitch+i_s, qua_ext, i_sf,  
					      qua_unfcod[0]+i_s, lag, lag_f, 
					      l_sf); 
		       
#ifdef WMOPS 
		      WMP_fwc("LTP_PP_pitch_ext_decod"); 
#endif 
		      /*------------------------------------------------------*/ 
		       
		      FLT_convolve(qua_unfcod[0]+i_s , hh_m+i_s, qua_fcod[0]+i_s, 
				   l_sf); 
			  
		      /*------------------------------------------------------*/ 
		    } 
		      
		  /*----------------------------------------------------------*/ 
		  /*     Build the excitation and synthesized signal          */ 
		  /*----------------------------------------------------------*/ 
		      
		  wad_dvector (qua_unfcod[0]+i_s, qua_gainQ[0][i_sf],  
			       qua_unfcod[1]+i_s, qua_gainQ[1][i_sf],  
			       qua_ext+MAX_LAG, 0, l_sf-1); 
		   
		  /*----------------------------------------------------------*/ 
		  /*                   Update memory                          */ 
		  /*----------------------------------------------------------*/ 
 
		  FLT_filterAP (pdcfq[i_sf], qua_ext+MAX_LAG, qua_sigsyn+NP, 
				qua_synth_mem, NP, l_sf); 
 
		  g1 = qua_gainQ[0][i_sf]; 
		  g2 = qua_gainQ[1][i_sf]; 
 
#ifdef WMOPS 
		  WMP_cnt_mult(2*(l_sf-NP)); 
		  WMP_cnt_mac(2*(l_sf-NP)); 
		  WMP_cnt_sub(l_sf-NP); 
		  WMP_cnt_move(2*(l_sf-NP)); 
#endif 
		  for (i = l_sf-NP, j = 0; i < l_sf; i++, j++) 
		    { 
		      qua_dif_mem[j] = NewTg[i_s+NP+i] - qua_sigsyn[i+NP]; 
		       
		      qua_target_mem[j] = Tgs[i] - g1 * qua_fcod[0][i+i_s] 
			                         - g2 * qua_fcod[1][i+i_s]; 
		    }	    
		 
		  /*----------------------------------------------------------*/ 
		  /*              Update the adaptive codebook                */ 
		  /*----------------------------------------------------------*/ 
 
		  cpy_dvector (qua_ext+l_sf, qua_ext, 0, MAX_LAG-1); 
		  i_s += l_sf; 
		   
		  /*==========================================================*/ 
		} 
 
	    /*----------------------------------------------------------------*/ 
	    /*                 Modify the memories for next frame             */ 
	    /*----------------------------------------------------------------*/ 
       
	    cpy_dvector (qua_synth_mem,  synth_mem,  0, NP-1); 
	    cpy_dvector (qua_dif_mem,    dif_mem,    0, NP-1); 
	    cpy_dvector (qua_target_mem, target_mem, 0, NP-1); 
	    cpy_dvector (qua_ext,        ext, 	     0, MAX_LAG-1); 
 
	    /*----------------------------------------------------------------*/ 
	    /*                   Deallocate temporary memory                  */ 
	    /*----------------------------------------------------------------*/ 
	     
	    free_dvector (qua_synth_mem,  0, NP-1); 
	    free_dvector (qua_dif_mem,	  0, L_SF+NP-1); 
	    free_dvector (qua_target_mem, 0, NP-1); 
	    free_dvector (qua_ext,        0, MAX_LAG+L_SF-1); 
 	     
	    /*================================================================*/ 
	  } 
 
        /*--------------------------------------------------------------------*/ 
        /*             Always set badrate_flag to 0 at the encoder            */ 
	/*--------------------------------------------------------------------*/ 
 
	channel.badrate_flag = 0; 
 
	/*--------------------------------------------------------------------*/ 
	/*                   Index to bitstream convertion                    */ 
	/*--------------------------------------------------------------------*/ 
 
	BIT_cdbk_index_to_bits(&channel, PackedWords, PackWdsPtr); 
 
#ifdef DATA 
	if  (( (num_data > 0) && (data_mode == 2))) 
	  { 
	    BIT_data_to_bits(data_buf, PackedWords, PackWdsPtr); 
	  } 
 
#endif 
 
	/*--------------------------------------------------------------------*/ 
	/*                    Modify the memories for next frame              */ 
	/*--------------------------------------------------------------------*/ 
	   
	cpy_dvector(lsf_new, lsf_mem, 0, NP-1); 
	cpy_dvector (NewTg+L_FRM, NewTg_mem, 0, NP-1);  
	 
	/*--------------------------------------------------------------------*/ 
	/*                      Deallocate temporary memory                   */ 
	/*--------------------------------------------------------------------*/ 
	 
	free_dvector (ForPitch, 0, L_FRM+L_FRM/2-1); 
 
	free_dmatrix (pdcfq,   0, N_SF_MAX, 0, NP-1); 
	free_dmatrix (wpdcf_pole,  0, N_SF_MAX, 0, NP-1); 
	free_dmatrix (wpdcf_zero,  0, N_SF_MAX, 0, NP-1); 
	free_dvector (Rp_sub,	0, N_SF_MAX-1); 
	free_dvector (NewTg , 0, L_FRM+NP-1); 
	 
	free_svector (MaxIdx_enc, 0, MAXPN-1); 
	free_dmatrix (unfcod,	0, 1, 0, L_SF-1); 
	free_dmatrix (fcod,	0, 1, 0, L_SF-1); 
	 
	free_dmatrix (qua_unfcod,	0, 1, 0, L_FRM-1); 
	free_dmatrix (qua_fcod,	0, 1, 0, L_FRM-1); 
	 
	free_dmatrix (qua_gainQ, 0, 1, 0, N_SF_MAX-1); 
	free_dvector (hh_m, 0, L_FRM-1); 
	free_dvector (Tgs , 0, L_SF-1); 
	free_dvector (Tgs_m, 0, L_SF-1); 
	free_dvector (Tg_frm, 0, L_FRM-1); 
 
	/*------------------------------------------------------------------*/ 
	 
        return; 
	 
        /*------------------------------------------------------------------*/ 
       } 
 
/*----------------------------------------------------------------------------*/ 
 
/*============================================================================*/ 
/*---------------------------------- END -------------------------------------*/ 
/*============================================================================*/