changeset 625:d46c2e4c4392

libgsmhr1: integrate sp_enc.c, encoder main function
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 12 Mar 2026 03:54:18 +0000
parents fd35fd05bc8b
children c15ae3f06ee9
files libgsmhr1/Makefile libgsmhr1/sp_enc.c
diffstat 2 files changed, 79 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/libgsmhr1/Makefile	Thu Mar 12 03:53:21 2026 +0000
+++ b/libgsmhr1/Makefile	Thu Mar 12 03:54:18 2026 +0000
@@ -2,8 +2,8 @@
 	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_frm.o sp_rom.o sp_sfrm.o tfo.o twts002_in.o \
-	twts002_out.o unpack_frame.o vad.o
+	sid_reset.o sp_dec.o sp_enc.o sp_frm.o sp_rom.o sp_sfrm.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_frm.h sp_rom.h sp_sfrm.h tw_gsmhr.h typedefs.h \
--- a/libgsmhr1/sp_enc.c	Thu Mar 12 03:53:21 2026 +0000
+++ b/libgsmhr1/sp_enc.c	Thu Mar 12 03:54:18 2026 +0000
@@ -16,14 +16,19 @@
  |_________________________________________________________________________|
 */
 
+#include <stdint.h>
+#include "typedefs.h"
+#include "tw_gsmhr.h"
+#include "namespace.h"
 #include "mathhalf.h"
 #include "mathdp31.h"
 #include "sp_rom.h"
-#include "sp_dec.h"
+#include "dec_func.h"
+#include "dtx_rxfe.h"
+#include "enc_state.h"
 #include "sp_frm.h"
 #include "sp_sfrm.h"
-#include "sp_enc.h"
-#include "host.h"
+#include "enc_out_order.h"
 #include "vad.h"
 
 /*_________________________________________________________________________
@@ -36,7 +41,7 @@
                                        /* one interpolation                 */
 #define ASCALE       0x0800
 #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 NUM_CLOSED        3            /* Maximum number of lags searched */
                                       /* in closed loop.                 */
 
@@ -49,45 +54,24 @@
 #define HPFSHIFT          1            /* no right shifts high pass shifts
                                         * speech */
 
-/*_________________________________________________________________________
- |                                                                         |
- |                     State variables (globals)                           |
- |_________________________________________________________________________|
-*/
-
-Shortword swOldR0;
-Shortword swOldR0Index;
+static void preen_clear_3lsb(const int16_t *in, Shortword *out)
+{
+	int i;
 
-struct NormSw psnsWSfrmEngSpace[2 * N_SUB];
-
-Shortword pswHPFXState[4];
-Shortword pswHPFYState[8];
-Shortword pswOldFrmKs[NP];
-Shortword pswOldFrmAs[NP];
-Shortword pswOldFrmSNWCoefs[NP];
-Shortword pswWgtSpeechSpace[F_LEN + LMAX + CG_INT_MACS / 2];
-
-Shortword pswSpeech[INBUFFSZ];         /* input speech */
+	for (i = 0; i < F_LEN; i++)
+		out[i] = in[i] & 0xFFF8;
+}
 
-Shortword swPtch = 1;
-
-/*_________________________________________________________________________
- |                                                                         |
- |                         Global DTX variables                            |
- |_________________________________________________________________________|
-*/
-
-Shortword swTxGsHistPtr = 0;
+static int check_for_ehf(const Shortword *frame)
+{
+	int i;
 
-Shortword pswCNVSCode1[N_SUB],
-          pswCNVSCode2[N_SUB],
-          pswCNGsp0Code[N_SUB],
-          pswCNLpc[3],
-          swCNR0;
-
-
-extern Longword pL_GsHist[];
-extern LongwordRom ppLr_gsTable[4][32];
+	for (i = 0; i < F_LEN; i++) {
+		if (frame[i] != 0x0008)
+			return 0;
+	}
+	return 1;
+}
 
 /***************************************************************************
  *
@@ -120,25 +104,25 @@
  *
  *************************************************************************/
 
-void   speechEncoder(Shortword pswSpeechIn[], Shortword pswFrmCodes[])
+void gsmhr_encode_frame(struct gsmhr_encoder_state *st, const int16_t *pcm_in,
+			int16_t *param_out)
 {
+  /* preened speech input, formerly input to speechEncoder() */
+  Shortword pswSpeechIn[F_LEN];
+  int reset_flag;
 
-/*_________________________________________________________________________
- |                                                                         |
- |                            Static Variables                             |
- |_________________________________________________________________________|
-*/
+  /* former static variables, converted to automatic */
 
   /* 1st point in analysis window */
-  static Shortword *pswLpcStart = &pswSpeech[LPCSTARTINDEX];
+  Shortword *pswLpcStart = &st->pswSpeech[LPCSTARTINDEX];
 
   /* 1st point of new frame other than 1st */
-  static Shortword *pswNewSpeech = &pswSpeech[NUMSTARTUPSMP];
+  Shortword *pswNewSpeech = &st->pswSpeech[NUMSTARTUPSMP];
 
   /* sample 0 of weighted speech */
-  static Shortword *pswWgtSpeech = &pswWgtSpeechSpace[LSMAX];
+  Shortword *pswWgtSpeech = &st->pswWgtSpeechSpace[LSMAX];
 
-  static struct NormSw *psnsWSfrmEng = &psnsWSfrmEngSpace[N_SUB];
+  struct NormSw *psnsWSfrmEng = &st->psnsWSfrmEngSpace[N_SUB];
 
 /*_________________________________________________________________________
  |                                                                         |
@@ -184,18 +168,24 @@
   struct NormSw
          psnsSqrtRs[N_SUB];
 
+  int giSfrmCnt;
+  short siPrevLagCode;
+
 /*_________________________________________________________________________
  |                                                                         |
  |                              Executable Code                            |
  |_________________________________________________________________________|
 */
 
+  preen_clear_3lsb(pcm_in, pswSpeechIn);
+  reset_flag = check_for_ehf(pswSpeechIn);
+
   /* Speech frame processing     */
   /* High pass filter the speech */
   /* ---------------------------- */
 
   filt4_2nd(psrHPFCoefs, pswSpeechIn,
-            pswHPFXState, pswHPFYState, F_LEN, HPFSHIFT);
+            st->pswHPFXState, st->pswHPFYState, F_LEN, HPFSHIFT);
 
   /* copy high passed filtered speech into encoder's speech buff */
   /*-------------------------------------------------------------*/
@@ -204,13 +194,11 @@
     pswNewSpeech[i] = pswSpeechIn[i];
 
 
-
-
   /* Calculate and quantize LPC coefficients */
   /* --------------------------------------- */
 
-  aflat(pswLpcStart, &iR0, pswFrmKs, piVq,
-        swPtch, &swVadFlag, &swSP);
+  aflat(st, pswLpcStart, &iR0, pswFrmKs, piVq,
+        st->swPtch, &swVadFlag, &swSP);
 
 
   /* Lookup frame energy r0 */
@@ -231,21 +219,21 @@
 
     for (i = 0; i < NP; i++)
     {
-      pswFrmKs[i] = pswOldFrmKs[i];
-      pswFrmAs[i] = pswOldFrmAs[i];
-      pswFrmSNWCoefs[i] = pswOldFrmSNWCoefs[i];
+      pswFrmKs[i] = st->pswOldFrmKs[i];
+      pswFrmAs[i] = st->pswOldFrmAs[i];
+      pswFrmSNWCoefs[i] = st->pswOldFrmSNWCoefs[i];
     }
   }
 
   /* Interpolate, or otherwise get sfrm reflection coefs */
   /* --------------------------------------------------- */
 
-  getSfrmLpcTx(swOldR0, swR0,
-               pswOldFrmKs, pswOldFrmAs,
-               pswOldFrmSNWCoefs,
+  getSfrmLpcTx(st, st->swOldR0, swR0,
+               st->pswOldFrmKs, st->pswOldFrmAs,
+               st->pswOldFrmSNWCoefs,
                pswFrmKs, pswFrmAs,
                pswFrmSNWCoefs,
-               pswSpeech,
+               st->pswSpeech,
                &siSi,
                psnsSqrtRs,
                ppswSynthAs, ppswSNWCoefAs);
@@ -256,14 +244,14 @@
   /* Weight the entire speech frame */
   /* ------------------------------ */
 
-  weightSpeechFrame(pswSpeech, ppswSynthAs[0], ppswSNWCoefAs[0],
-                    pswWgtSpeechSpace);
+  weightSpeechFrame(st, st->pswSpeech, ppswSynthAs[0], ppswSNWCoefAs[0],
+                    st->pswWgtSpeechSpace);
 
   /* Perform open-loop lag search, get harmonic-noise-weighting parameters */
   /* --------------------------------------------------------------------- */
 
-  openLoopLagSearch(&pswWgtSpeechSpace[LSMAX],
-                    swOldR0Index,
+  openLoopLagSearch(&st->pswWgtSpeechSpace[LSMAX],
+                    st->swOldR0Index,
                     (Shortword) iR0,
                     &siUVCode,
                     pswLagList,
@@ -281,7 +269,7 @@
   /* parameter                                    */      /* DTX mode */
   /* -------------------------------------------- */      /* DTX mode */
   
-  periodicity_update(pswVadLags, &swPtch);                /* DTX mode */
+  periodicity_update(&st->vad, pswVadLags, &st->swPtch);  /* DTX mode */
 
 
   /* Subframe processing loop */
@@ -292,14 +280,15 @@
   for (giSfrmCnt = 0; giSfrmCnt < N_SUB; giSfrmCnt++)
   {
 
-    if (swSP == 0)                                        /* DTX mode */
-    {                                                     /* DTX mode */
-      pswVSCode1[giSfrmCnt] = pswCNVSCode1[giSfrmCnt];    /* DTX mode */
-      pswVSCode2[giSfrmCnt] = pswCNVSCode2[giSfrmCnt];    /* DTX mode */
-      pswGsp0Code[giSfrmCnt] = pswCNGsp0Code[giSfrmCnt];  /* DTX mode */
-    }                                                     /* DTX mode */
+    /* DTX mode */
+    if (swSP == 0)
+    {
+      pswVSCode1[giSfrmCnt] = st->pswCNVSCode1[giSfrmCnt];
+      pswVSCode2[giSfrmCnt] = st->pswCNVSCode2[giSfrmCnt];
+      pswGsp0Code[giSfrmCnt] = st->pswCNGsp0Code[giSfrmCnt];
+    }
 
-    sfrmAnalysis(&pswWgtSpeech[giSfrmCnt * S_LEN],
+    sfrmAnalysis(st, &pswWgtSpeech[giSfrmCnt * S_LEN],
                  siUVCode,
                  psnsSqrtRs[giSfrmCnt],
                  ppswSNWCoefAs[giSfrmCnt],
@@ -309,7 +298,7 @@
                  pswHNWCoefBuf[giSfrmCnt],
                  &pswLagCode[giSfrmCnt], &pswVSCode1[giSfrmCnt],
                  &pswVSCode2[giSfrmCnt], &pswGsp0Code[giSfrmCnt],
-                 swSP);
+                 swSP, giSfrmCnt, &siPrevLagCode);
 
     pswLagListPtr = &pswLagListPtr[pswNumLagList[giSfrmCnt]];
 
@@ -326,9 +315,9 @@
     /* copy comfort noise frame parameter */               /* DTX mode */
     /* ---------------------------------- */               /* DTX mode */
   
-    iR0 = swCNR0; /* quantized R0 index */                 /* DTX mode */
+    iR0 = st->swCNR0; /* quantized R0 index */             /* DTX mode */
     for (i=0; i < 3; i++)                                  /* DTX mode */
-      piVq[i] = pswCNLpc[i];                               /* DTX mode */
+      piVq[i] = st->pswCNLpc[i];                           /* DTX mode */
 
   }                                                        /* DTX mode */
   else                                                     /* DTX mode */
@@ -338,11 +327,11 @@
     /* -----------------------------------------*/         /* DTX mode */
 
     for (i=0; i < N_SUB; i++){                             /* DTX mode */
-      pL_GsHist[swTxGsHistPtr] =                           /* DTX mode */
+      st->pL_GsHist[st->swTxGsHistPtr] =                   /* DTX mode */
         ppLr_gsTable[siUVCode][pswGsp0Code[i]];            /* DTX mode */
-      swTxGsHistPtr++;                                     /* DTX mode */
-      if (swTxGsHistPtr > ((OVERHANG-1)*N_SUB)-1)          /* DTX mode */
-        swTxGsHistPtr=0;                                   /* DTX mode */
+      st->swTxGsHistPtr++;                                 /* DTX mode */
+      if (st->swTxGsHistPtr > ((OVERHANG-1)*N_SUB)-1)      /* DTX mode */
+        st->swTxGsHistPtr=0;                               /* DTX mode */
     }                                                      /* DTX mode */
 
   }                                                        /* DTX mode */
@@ -359,14 +348,14 @@
     piGsp0Code[i] = pswGsp0Code[i];
   }
 
-  swOldR0Index = (Shortword) iR0;
-  swOldR0 = swR0;
+  st->swOldR0Index = (Shortword) iR0;
+  st->swOldR0 = swR0;
 
   for (i = 0; i < NP; i++)
   {
-    pswOldFrmKs[i] = pswFrmKs[i];
-    pswOldFrmAs[i] = pswFrmAs[i];
-    pswOldFrmSNWCoefs[i] = pswFrmSNWCoefs[i];
+    st->pswOldFrmKs[i] = pswFrmKs[i];
+    st->pswOldFrmAs[i] = pswFrmAs[i];
+    st->pswOldFrmSNWCoefs[i] = pswFrmSNWCoefs[i];
   }
 
   /* Insert SID Codeword */                                /* DTX mode */
@@ -393,7 +382,7 @@
 
   fillBitAlloc(iVoicing, iR0, piVq, iSi, piLagCode,
                piVSCode1, piVSCode2,
-               piGsp0Code, swVadFlag, swSP, pswFrmCodes);
+               piGsp0Code, swVadFlag, swSP, param_out);
 
 
   /* delay the input speech by 1 frame */
@@ -401,6 +390,6 @@
 
   for (i = 0, j = F_LEN; j < INBUFFSZ; i++, j++)
   {
-    pswSpeech[i] = pswSpeech[j];
+    st->pswSpeech[i] = st->pswSpeech[j];
   }
 }