changeset 617:ff5857556c44

libgsmhr1: integrate sp_frm.c
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 05 Mar 2026 19:48:10 +0000
parents 877956ca4601
children e6e9d5104503
files libgsmhr1/Makefile libgsmhr1/enc_state.h libgsmhr1/namespace.list libgsmhr1/sp_frm.c libgsmhr1/sp_frm.h
diffstat 5 files changed, 154 insertions(+), 186 deletions(-) [+]
line wrap: on
line diff
--- a/libgsmhr1/Makefile	Thu Mar 05 17:53:42 2026 +0000
+++ b/libgsmhr1/Makefile	Thu Mar 05 19:48:10 2026 +0000
@@ -2,11 +2,11 @@
 	dtx_rxfe.o enc_out_order.o enc_state.o err_conc.o mathdp31.o mathhalf.o\
 	pack_frame.o paramval_cod.o paramval_common.o paramval_dec.o rtp_in.o \
 	rtp_in_direct.o rxfe.o rxfe_create.o sid_cw_params.o sid_detect.o \
-	sid_reset.o sp_dec.o sp_rom.o tfo.o twts002_in.o twts002_out.o \
+	sid_reset.o sp_dec.o sp_frm.o sp_rom.o tfo.o twts002_in.o twts002_out.o\
 	unpack_frame.o vad.o
 HDRS=	dec_func.h dec_state.h dtx_const.h dtx_dec.h dtx_enc.h dtx_rxfe.h \
 	enc_out_order.h enc_state.h err_conc.h mathdp31.h mathhalf.h \
-	namespace.h rxfe.h sp_rom.h tw_gsmhr.h typedefs.h vad.h
+	namespace.h rxfe.h sp_frm.h sp_rom.h tw_gsmhr.h typedefs.h vad.h
 LIB=	libgsmhr1.a
 
 include ../config.defs
--- a/libgsmhr1/enc_state.h	Thu Mar 05 17:53:42 2026 +0000
+++ b/libgsmhr1/enc_state.h	Thu Mar 05 19:48:10 2026 +0000
@@ -12,7 +12,7 @@
 #define CG_INT_MACS       6            /* Number of Multiply-Accumulates in */
                                        /* one interpolation                 */
 #define LMAX            142            /* largest lag (integer sense) */
-#define LSMAX           (LMAX+ CG_INT_MACS/2)   /* Lag Search Array Length */
+#define LSMAX           (LMAX + CG_INT_MACS/2)   /* Lag Search Array Length */
 #define LPCSTARTINDEX     25           /* Where the LPC analysis window
                                         * starts */
 #define INBUFFSZ          LPCSTARTINDEX + A_LEN /* Input buffer size  */
@@ -64,6 +64,12 @@
 	Shortword pswAnalysisState[NP];
 	Shortword pswWStateNum[NP];
 	Shortword pswWStateDenom[NP];
+	const Shortword *psrTable;
+	int iLimit;
+	int iLow;
+	int iThree;
+	int iWordHalfPtr;
+	int iWordPtr;
 	/* state variables from sp_sfrm.c */
 	Shortword pswLtpStateBase[LTP_LEN + S_LEN];
 	Shortword pswHState[NP];
--- a/libgsmhr1/namespace.list	Thu Mar 05 17:53:42 2026 +0000
+++ b/libgsmhr1/namespace.list	Thu Mar 05 19:48:10 2026 +0000
@@ -38,3 +38,9 @@
 vad_algorithm energy_computation average_acf predictor_values schur_recursion
 step_up compute_rav1 spectral_comparison tone_detection threshold_adaptation
 vad_decision vad_hangover periodicity_update
+
+iir_d filt4_2nd initPBarVBarL initPBarFullVBarFullL aflatRecursion
+aflatNewBarRecursionL setupPreQ setupQuant getNextVec aflat fnExp2 fnLog2
+weightSpeechFrame getSfrmLpcTx fnBest_CG compResidEnergy r0Quant cov32 flat
+openLoopLagSearch getCCThreshold pitchLags CGInterpValid CGInterp quantLag
+findBestInQuantList findPeak bestDelta maxCCOverGWithSign getNWCoefs
--- a/libgsmhr1/sp_frm.c	Thu Mar 05 17:53:42 2026 +0000
+++ b/libgsmhr1/sp_frm.c	Thu Mar 05 19:48:10 2026 +0000
@@ -66,14 +66,18 @@
  |_________________________________________________________________________|
 */
 
+#include "typedefs.h"
+#include "namespace.h"
 #include "mathhalf.h"
 #include "mathdp31.h"
 #include "sp_rom.h"
-#include "sp_dec.h"
+#include "dec_func.h"
+#include "enc_state.h"
 #include "sp_frm.h"
-#include "sp_sfrm.h"
 #include "vad.h"
-#include "dtx.h"
+#include "dtx_const.h"
+#include "dtx_enc.h"
+#include "dtx_rxfe.h"
 
 /*_________________________________________________________________________
  |                                                                         |
@@ -132,40 +136,6 @@
 #define odd(x) (x & 0x0001)            /* This macro will determine if an
                                         * integer is odd */
 
-/*_________________________________________________________________________
- |                                                                         |
- |                         State Variables (globals)                       |
- |_________________________________________________________________________|
-*/
-
-Shortword pswAnalysisState[NP];
-
-Shortword pswWStateNum[NP],
-       pswWStateDenom[NP];
-
-/*_________________________________________________________________________
- |                                                                         |
- |                         Other External Variables                        |
- |_________________________________________________________________________|
-*/
-
-static ShortwordRom *psrTable;         /* points to correct table of
-                                        * vectors */
-int iLimit;                            /* accessible to all in this file
-                                        * and to lpcCorrQntz() in dtx.c */
-static int iLow;                       /* the low element in this segment */
-static int iThree;                     /* boolean, is this a three element
-                                        * vector */
-static int iWordHalfPtr;               /* points to the next byte */
-static int iWordPtr;                   /* points to the next word to be
-                                        * read */
-
-extern Shortword pswCNVSCode1[],       /* comfort noise parameters */
-                 pswCNVSCode2[],
-                 pswCNGsp0Code[],
-                 pswCNLpc[],
-                 swCNR0;
-
 /***************************************************************************
  *
  *    FUNCTION NAME: aflat
@@ -226,13 +196,9 @@
  *
  *************************************************************************/
 
-  void   aflat(Shortword pswSpeechToLPC[],
-                      int piR0Index[],
-                      Shortword pswFinalRc[],
-                      int piVQCodewds[],
-                      Shortword swPtch,
-                      Shortword *pswVadFlag,
-                      Shortword *pswSP)
+void aflat(struct gsmhr_encoder_state *st, Shortword pswSpeechToLPC[],
+	   int piR0Index[], Shortword pswFinalRc[], int piVQCodewds[],
+	   Shortword swPtch, Shortword *pswVadFlag, Shortword *pswSP)
 {
 
 /*_________________________________________________________________________
@@ -317,19 +283,20 @@
   /*  modified.                                           */ /* DTX mode */
   /* ---------------------------------------------------- */ /* DTX mode */
 
-  vad_algorithm(pL_VadAcf, swVadScalAuto, pswVadRc, swPtch,  /* DTX mode */
+  /* DTX mode */
+  vad_algorithm(&st->vad, pL_VadAcf, swVadScalAuto, pswVadRc, swPtch,
                 pswVadFlag);
 
 
   /* if DTX mode off, then always voice activity */          /* DTX mode */
   /* ------------------------------------------- */          /* DTX mode */
-  if (!giDTXon) *pswVadFlag = 1;                             /* DTX mode */
+  if (!st->dtx_mode) *pswVadFlag = 1;                        /* DTX mode */
 
 
   /* determination of comfort noise parameters */            /* DTX mode */
   /* ----------------------------------------- */            /* DTX mode */
 
-  *pswSP = swComfortNoise(*pswVadFlag,                       /* DTX mode */
+  *pswSP = swComfortNoise(st, *pswVadFlag,                   /* DTX mode */
                           L_R0,                              /* DTX mode */
                           pL_CorrelSeq);                     /* DTX mode */
 
@@ -377,25 +344,25 @@
       /* do aflat for all vectors in the list */
       /*--------------------------------------*/
 
-      setupPreQ(iSeg, quantList.iRCIndex);        /* set up vector ptrs */
+      setupPreQ(st, iSeg, quantList.iRCIndex);    /* set up vector ptrs */
 
       for (iCnt = 0; iCnt < quantList.iNum; iCnt++)
       {
         /* get a vector */
         /*--------------*/
 
-        getNextVec(pswRc);
+        getNextVec(st, pswRc);
 
         /* clear the limiter flag */
         /*------------------------*/
 
-        iLimit = 0;
+        st->iLimit = 0;
 
         /* find the error values for each vector */
         /*---------------------------------------*/
 
         quantList.pswPredErr[iCnt] =
-                aflatRecursion(&pswRc[psvqIndex[iSeg - 1].l],
+                aflatRecursion(st, &pswRc[psvqIndex[iSeg - 1].l],
                                pswPBar, pswVBar,
                                ppswPAddrs, ppswVAddrs,
                                psvqIndex[iSeg - 1].len);
@@ -403,7 +370,7 @@
         /* check the limiter flag */
         /*------------------------*/
 
-        if (iLimit)
+        if (st->iLimit)
         {
           quantList.pswPredErr[iCnt] = 0x7fff;    /* set error to bad value */
         }
@@ -424,7 +391,7 @@
         quantList.iNum = psrQuantSz[iSeg - 1];
         quantList.iRCIndex = bestPql[iVec].iRCIndex * psrQuantSz[iSeg - 1];
 
-        setupQuant(iSeg, quantList.iRCIndex);     /* set up vector ptrs */
+        setupQuant(st, iSeg, quantList.iRCIndex);   /* set up vector ptrs */
 
         /* do aflat recursion on each element of list */
         /*--------------------------------------------*/
@@ -435,18 +402,18 @@
           /* get a vector */
           /*--------------*/
 
-          getNextVec(pswRc);
+          getNextVec(st, pswRc);
 
           /* clear the limiter flag */
           /*------------------------*/
 
-          iLimit = 0;
+          st->iLimit = 0;
 
           /* find the error values for each vector */
           /*---------------------------------------*/
 
           quantList.pswPredErr[iCnt] =
-                  aflatRecursion(&pswRc[psvqIndex[iSeg - 1].l],
+                  aflatRecursion(st, &pswRc[psvqIndex[iSeg - 1].l],
                                  pswPBar, pswVBar,
                                  ppswPAddrs, ppswVAddrs,
                                  psvqIndex[iSeg - 1].len);
@@ -454,7 +421,7 @@
           /* check the limiter flag */
           /*------------------------*/
 
-          if (iLimit)
+          if (st->iLimit)
           {
             quantList.pswPredErr[iCnt] = 0x7fff;  /* set error to the worst
                                                    * value */
@@ -483,8 +450,8 @@
       /* find the quantized reflection coefficients */
       /*--------------------------------------------*/
 
-      setupQuant(iSeg, bestQl[iSeg].iRCIndex);    /* set up vector ptrs */
-      getNextVec((Shortword *) (pswFinalRc - 1));
+      setupQuant(st, iSeg, bestQl[iSeg].iRCIndex);   /* set up vector ptrs */
+      getNextVec(st, (Shortword *) (pswFinalRc - 1));
 
 
       /* Update pBarFull and vBarFull for the next Rc-VQ segment, and */
@@ -861,12 +828,10 @@
  *
  *************************************************************************/
 
-Shortword aflatRecursion(Shortword pswQntRc[],
-                                Shortword pswPBar[],
-                                Shortword pswVBar[],
-                                Shortword *ppswPAddrs[],
-                                Shortword *ppswVAddrs[],
-                                Shortword swSegmentOrder)
+Shortword aflatRecursion(struct gsmhr_encoder_state *st, Shortword pswQntRc[],
+			 Shortword pswPBar[], Shortword pswVBar[],
+			 Shortword *ppswPAddrs[], Shortword *ppswVAddrs[],
+			 Shortword swSegmentOrder)
 {
 
 /*_________________________________________________________________________
@@ -942,7 +907,7 @@
     /*-----------------------------------------*/
 
     if (sub(pswPNew[0], 0x4000) >= 0)
-      iLimit = 1;
+      st->iLimit = 1;
 
     /* Compute the new Vsubj(i) */
     /*--------------------------*/
@@ -1486,11 +1451,11 @@
 
 
 
-short  compResidEnergy(Shortword pswSpeech[],
-                              Shortword ppswInterpCoef[][NP],
-                              Shortword pswPreviousCoef[],
-                              Shortword pswCurrentCoef[],
-                              struct NormSw psnsSqrtRs[])
+short compResidEnergy(struct gsmhr_encoder_state *st, Shortword pswSpeech[],
+			Shortword ppswInterpCoef[][NP],
+			Shortword pswPreviousCoef[],
+			Shortword pswCurrentCoef[],
+			struct NormSw psnsSqrtRs[])
 {
 
 /*_________________________________________________________________________
@@ -1560,7 +1525,7 @@
   /*--------------------------------------------------*/
 
   for (i = 0; i < NP; i++)
-    pswTempState[i] = pswAnalysisState[i];
+    pswTempState[i] = st->pswAnalysisState[i];
 
   /* Send the speech frame, one subframe at a time, through the analysis */
   /* filter which is based on interpolated coefficients.  After each     */
@@ -1626,7 +1591,7 @@
         break;
     }
 
-    lpcFir(&pswSpeech[i * S_LEN], pswCoef, pswAnalysisState,
+    lpcFir(&pswSpeech[i * S_LEN], pswCoef, st->pswAnalysisState,
            pswResidual);
 
     if (siOverflowPossible)
@@ -3614,7 +3579,7 @@
  *
  *************************************************************************/
 
-void   getNextVec(Shortword pswRc[])
+void getNextVec(struct gsmhr_encoder_state *st, Shortword pswRc[])
 {
 
 /*_________________________________________________________________________
@@ -3629,33 +3594,33 @@
  |                              Executable Code                            |
  |_________________________________________________________________________|
 */
-  i = iLow;
-
-  if (iThree)
+  i = st->iLow;
+
+  if (st->iThree)
   {
 
-    if (iWordHalfPtr == HIGH)
+    if (st->iWordHalfPtr == HIGH)
     {
-      pswRc[i++] = psrSQuant[high(psrTable[iWordPtr])];
-      pswRc[i++] = psrSQuant[low(psrTable[iWordPtr++])];
-      pswRc[i] = psrSQuant[high(psrTable[iWordPtr])];
-      iWordHalfPtr = LOW;
+      pswRc[i++] = psrSQuant[high(st->psrTable[st->iWordPtr])];
+      pswRc[i++] = psrSQuant[low(st->psrTable[st->iWordPtr++])];
+      pswRc[i] = psrSQuant[high(st->psrTable[st->iWordPtr])];
+      st->iWordHalfPtr = LOW;
     }
     else
     {
-      pswRc[i++] = psrSQuant[low(psrTable[iWordPtr++])];
-      pswRc[i++] = psrSQuant[high(psrTable[iWordPtr])];
-      pswRc[i] = psrSQuant[low(psrTable[iWordPtr++])];
-      iWordHalfPtr = HIGH;
+      pswRc[i++] = psrSQuant[low(st->psrTable[st->iWordPtr++])];
+      pswRc[i++] = psrSQuant[high(st->psrTable[st->iWordPtr])];
+      pswRc[i] = psrSQuant[low(st->psrTable[st->iWordPtr++])];
+      st->iWordHalfPtr = HIGH;
     }
 
   }
   else
   {
-    pswRc[i++] = psrSQuant[high(psrTable[iWordPtr])];
-    pswRc[i++] = psrSQuant[low(psrTable[iWordPtr++])];
-    pswRc[i++] = psrSQuant[high(psrTable[iWordPtr])];
-    pswRc[i] = psrSQuant[low(psrTable[iWordPtr++])];
+    pswRc[i++] = psrSQuant[high(st->psrTable[st->iWordPtr])];
+    pswRc[i++] = psrSQuant[low(st->psrTable[st->iWordPtr++])];
+    pswRc[i++] = psrSQuant[high(st->psrTable[st->iWordPtr])];
+    pswRc[i] = psrSQuant[low(st->psrTable[st->iWordPtr++])];
   }
 }
 
@@ -3718,18 +3683,17 @@
  *************************************************************************/
 
 
-void   getSfrmLpcTx(Shortword swPrevR0, Shortword swNewR0,
-/* last frm*/
-                       Shortword pswPrevFrmKs[], Shortword pswPrevFrmAs[],
-                           Shortword pswPrevFrmSNWCoef[],
-/* this frm*/
-                         Shortword pswNewFrmKs[], Shortword pswNewFrmAs[],
-                           Shortword pswNewFrmSNWCoef[],
-                           Shortword pswHPFSpeech[],
-/* output */
-                           short *pswSoftInterp,
-                           struct NormSw *psnsSqrtRs,
-               Shortword ppswSynthAs[][NP], Shortword ppswSNWCoefAs[][NP])
+void getSfrmLpcTx(struct gsmhr_encoder_state *st, Shortword swPrevR0,
+		  Shortword swNewR0,
+		  /* last frm*/
+		  Shortword pswPrevFrmKs[], Shortword pswPrevFrmAs[],
+		  Shortword pswPrevFrmSNWCoef[],
+		  /* this frm*/
+		  Shortword pswNewFrmKs[], Shortword pswNewFrmAs[],
+		  Shortword pswNewFrmSNWCoef[], Shortword pswHPFSpeech[],
+		  /* output */
+		  short *pswSoftInterp, struct NormSw *psnsSqrtRs,
+		  Shortword ppswSynthAs[][NP], Shortword ppswSNWCoefAs[][NP])
 {
 
 /*_________________________________________________________________________
@@ -3837,7 +3801,7 @@
   /* make the decision whether to interpolate (1) or not (0)       */
   /*---------------------------------------------------------------*/
 
-  swSi = compResidEnergy(pswHPFSpeech,
+  swSi = compResidEnergy(st, pswHPFSpeech,
                          ppswSynthAs, pswPrevFrmAs,
                          pswNewFrmAs, psnsSqrtRs);
 
@@ -5699,7 +5663,7 @@
  *
  *************************************************************************/
 
-void   setupPreQ(int iSeg, int iVector)
+void setupPreQ(struct gsmhr_encoder_state *st, int iSeg, int iVector)
 {
 
 /*_________________________________________________________________________
@@ -5708,38 +5672,38 @@
  |_________________________________________________________________________|
 */
 
-  iLow = psvqIndex[iSeg - 1].l;
-  iThree = ((psvqIndex[iSeg - 1].h - iLow) == 2);
+  st->iLow = psvqIndex[iSeg - 1].l;
+  st->iThree = ((psvqIndex[iSeg - 1].h - st->iLow) == 2);
 
   switch (iSeg)
   {
     case 1:
       {
-        psrTable = psrPreQ1;
-        iWordPtr = (iVector * 3) >> 1;
+        st->psrTable = psrPreQ1;
+        st->iWordPtr = (iVector * 3) >> 1;
         if (odd(iVector))
-          iWordHalfPtr = LOW;
+          st->iWordHalfPtr = LOW;
         else
-          iWordHalfPtr = HIGH;
+          st->iWordHalfPtr = HIGH;
         break;
       }
 
     case 2:
       {
-        psrTable = psrPreQ2;
-        iWordPtr = (iVector * 3) >> 1;
+        st->psrTable = psrPreQ2;
+        st->iWordPtr = (iVector * 3) >> 1;
         if (odd(iVector))
-          iWordHalfPtr = LOW;
+          st->iWordHalfPtr = LOW;
         else
-          iWordHalfPtr = HIGH;
+          st->iWordHalfPtr = HIGH;
         break;
       }
 
     case 3:
       {
-        psrTable = psrPreQ3;
-        iWordPtr = iVector * 2;
-        iWordHalfPtr = HIGH;
+        st->psrTable = psrPreQ3;
+        st->iWordPtr = iVector * 2;
+        st->iWordHalfPtr = HIGH;
         break;
       }
   }
@@ -5768,7 +5732,7 @@
  *
  *************************************************************************/
 
-void   setupQuant(int iSeg, int iVector)
+void setupQuant(struct gsmhr_encoder_state *st, int iSeg, int iVector)
 {
 
 /*_________________________________________________________________________
@@ -5777,38 +5741,38 @@
  |_________________________________________________________________________|
 */
 
-  iLow = psvqIndex[iSeg - 1].l;
-  iThree = ((psvqIndex[iSeg - 1].h - iLow) == 2);
+  st->iLow = psvqIndex[iSeg - 1].l;
+  st->iThree = ((psvqIndex[iSeg - 1].h - st->iLow) == 2);
 
   switch (iSeg)
   {
     case 1:
       {
-        psrTable = psrQuant1;
-        iWordPtr = (iVector * 3) >> 1;
+        st->psrTable = psrQuant1;
+        st->iWordPtr = (iVector * 3) >> 1;
         if (odd(iVector))
-          iWordHalfPtr = LOW;
+          st->iWordHalfPtr = LOW;
         else
-          iWordHalfPtr = HIGH;
+          st->iWordHalfPtr = HIGH;
         break;
       }
 
     case 2:
       {
-        psrTable = psrQuant2;
-        iWordPtr = (iVector * 3) >> 1;
+        st->psrTable = psrQuant2;
+        st->iWordPtr = (iVector * 3) >> 1;
         if (odd(iVector))
-          iWordHalfPtr = LOW;
+          st->iWordHalfPtr = LOW;
         else
-          iWordHalfPtr = HIGH;
+          st->iWordHalfPtr = HIGH;
         break;
       }
 
     case 3:
       {
-        psrTable = psrQuant3;
-        iWordPtr = iVector * 2;
-        iWordHalfPtr = HIGH;
+        st->psrTable = psrQuant3;
+        st->iWordPtr = iVector * 2;
+        st->iWordHalfPtr = HIGH;
         break;
       }
   }
@@ -5859,10 +5823,10 @@
  *
  *************************************************************************/
 
-void   weightSpeechFrame(Shortword pswSpeechFrm[],
-                                Shortword pswWNumSpace[],
-                                Shortword pswWDenomSpace[],
-                                Shortword pswWSpeechBuffBase[])
+void weightSpeechFrame(struct gsmhr_encoder_state *st,
+			Shortword pswSpeechFrm[],
+			Shortword pswWNumSpace[], Shortword pswWDenomSpace[],
+			Shortword pswWSpeechBuffBase[])
 {
 
 /*_________________________________________________________________________
@@ -5897,12 +5861,12 @@
   for (siI = 0; siI < N_SUB; siI++)
   {
     lpcFir(&pswSpeechFrm[siI * S_LEN], &pswWNumSpace[siI * NP],
-           pswWStateNum, &pswScratch0[siI * S_LEN]);
+           st->pswWStateNum, &pswScratch0[siI * S_LEN]);
   }
 
   for (siI = 0; siI < N_SUB; siI++)
   {
     lpcIir(&pswScratch0[siI * S_LEN], &pswWDenomSpace[siI * NP],
-           pswWStateDenom, &pswWSpeechFrm[siI * S_LEN]);
+           st->pswWStateDenom, &pswWSpeechFrm[siI * S_LEN]);
   }
 }
--- a/libgsmhr1/sp_frm.h	Thu Mar 05 17:53:42 2026 +0000
+++ b/libgsmhr1/sp_frm.h	Thu Mar 05 19:48:10 2026 +0000
@@ -21,6 +21,44 @@
  |_________________________________________________________________________|
 */
 
+void aflat(struct gsmhr_encoder_state *st, Shortword pswSpeechToLPC[],
+	   int piR0Index[], Shortword pswFinalRc[], int piVQCodewds[],
+	   Shortword swPtch, Shortword *pswVadFlag, Shortword *pswSP);
+
+Shortword aflatRecursion(struct gsmhr_encoder_state *st, Shortword pswQntRc[],
+			 Shortword pswPBar[], Shortword pswVBar[],
+			 Shortword *ppswPAddrs[], Shortword *ppswVAddrs[],
+			 Shortword swSegmentOrder);
+
+void getSfrmLpcTx(struct gsmhr_encoder_state *st, Shortword swPrevR0,
+		  Shortword swNewR0,
+		  /* last frm*/
+		  Shortword pswPrevFrmKs[], Shortword pswPrevFrmAs[],
+		  Shortword pswPrevFrmSNWCoef[],
+		  /* this frm*/
+		  Shortword pswNewFrmKs[], Shortword pswNewFrmAs[],
+		  Shortword pswNewFrmSNWCoef[], Shortword pswHPFSpeech[],
+		  /* output */
+		  short *pswSoftInterp, struct NormSw *psnsSqrtRs,
+		  Shortword ppswSynthAs[][NP], Shortword ppswSNWCoefAs[][NP]);
+
+short compResidEnergy(struct gsmhr_encoder_state *st, Shortword pswSpeech[],
+			Shortword ppswInterpCoef[][NP],
+			Shortword pswPreviousCoef[],
+			Shortword pswCurrentCoef[],
+			struct NormSw psnsSqrtRs[]);
+
+void getNextVec(struct gsmhr_encoder_state *st, Shortword pswRc[]);
+
+void setupPreQ(struct gsmhr_encoder_state *st, int iSeg, int iVector);
+
+void setupQuant(struct gsmhr_encoder_state *st, int iSeg, int iVector);
+
+void weightSpeechFrame(struct gsmhr_encoder_state *st,
+			Shortword pswSpeechFrm[],
+			Shortword pswWNumSpace[], Shortword pswWDenomSpace[],
+			Shortword pswWSpeechBuffBase[]);
+
 void   iir_d(Shortword pswCoeff[], Shortword pswIn[],
                     Shortword pswXstate[],
                     Shortword pswYstate[],
@@ -44,13 +82,6 @@
                                       Longword pL_PBarFull[],
                                       Longword pL_VBarFull[]);
 
-  Shortword aflatRecursion(Shortword pswQntRc[],
-                                  Shortword pswPBar[],
-                                  Shortword pswVBar[],
-                                  Shortword *ppswPAddrs[],
-                                  Shortword *ppswVAddrs[],
-                                  Shortword swSegmentOrder);
-
   void   aflatNewBarRecursionL(Shortword pswQntRc[],
                                       int iSegment,
                                       Longword pL_PBar[],
@@ -59,55 +90,16 @@
                                       Shortword pswVBar[]);
 
 
-  void   setupPreQ(int iSeg, int iVector);
-
-  void   setupQuant(int iSeg, int iVector);
-
-  void   getNextVec(Shortword pswRc[]);
-
-  void   aflat(Shortword pswSpeechToLPC[],
-                      int piR0Index[],
-                      Shortword pswFinalRc[],
-                      int piVQCodewds[],
-                      Shortword swPtch,
-                      Shortword *pswVadFlag,
-                      Shortword *pswSP);
-
-
   Shortword fnExp2(Longword L_Input);
 
   Shortword fnLog2(Longword L_Input);
 
-  void   weightSpeechFrame(Shortword pswSpeechFrm[],
-                                  Shortword pswWNumSpace[],
-                                  Shortword pswWDenomSpace[],
-                                  Shortword pswWSpeechBuffBase[]);
-
-  void   getSfrmLpcTx(Shortword swPrevR0, Shortword swNewR0,
-                             Shortword pswPrevFrmKs[],
-                             Shortword pswPrevFrmAs[],
-                             Shortword pswPrevFrmSNWCoef[],
-                             Shortword pswNewFrmKs[],
-                             Shortword pswNewFrmAs[],
-                             Shortword pswNewFrmSNWCoef[],
-                             Shortword pswHPFSpeech[],
-                             short *pswSoftInterp,
-                             struct NormSw *psnsSqrtRs,
-                             Shortword ppswSynthAs[][NP],
-                             Shortword ppswSNWCoefAs[][NP]);
-
   short int fnBest_CG(Shortword pswCframe[],
                              Shortword pswGframe[],
                              Shortword *pswCmaxSqr,
                              Shortword *pswGmax,
                              short int siNumPairs);
 
-  short  compResidEnergy(Shortword pswSpeech[],
-                                Shortword ppswInterpCoef[][NP],
-                                Shortword pswPreviousCoef[],
-                                Shortword pswCurrentCoef[],
-                                struct NormSw psnsSqrtRs[]);
-
   Shortword r0Quant(Longword L_UnqntzdR0);
 
   Shortword cov32(Shortword pswIn[],