changeset 344:05a46720af0f

libtwamr: integrate d_plsf_3.c
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 19 Apr 2024 01:23:15 +0000
parents 3f574255c3aa
children 29769a9b89d3
files libtwamr/Makefile libtwamr/d_plsf_3.c libtwamr/lsp_lsf.c libtwamr/lsp_lsf.h libtwamr/lsp_lsf.tab libtwamr/namespace.h libtwamr/reorder.c libtwamr/reorder.h
diffstat 8 files changed, 530 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libtwamr/Makefile	Fri Apr 19 01:08:39 2024 +0000
+++ b/libtwamr/Makefile	Fri Apr 19 01:23:15 2024 +0000
@@ -4,9 +4,10 @@
 	bitno.o bits2prm.o c1035pf.o c2_11pf.o c2_9pf.o c3_14pf.o c4_17pf.o \
 	c8_31pf.o c_g_aver.o calc_cor.o calc_en.o cbsearch.o convolve.o cor_h.o\
 	d1035pf.o d2_11pf.o d2_9pf.o d3_14pf.o d4_17pf.o d8_31pf.o d_gain_c.o \
-	d_gain_p.o d_plsf.o gains_tab.o gc_pred.o gmed_n.o graytab.o inv_sqrt.o\
-	log2.o oper_32b.o pow2.o prmno.o q_plsf3_tab.o q_plsf5_tab.o s10_8pf.o \
-	set_sign.o sqrt_l.o tls_flags.o window.o
+	d_gain_p.o d_plsf.o d_plsf_3.o gains_tab.o gc_pred.o gmed_n.o graytab.o\
+	inv_sqrt.o log2.o lsp_lsf.o oper_32b.o pow2.o prmno.o q_plsf3_tab.o \
+	q_plsf5_tab.o reorder.o s10_8pf.o set_sign.o sqrt_l.o tls_flags.o \
+	window.o
 LIB=	libtwamr.a
 
 INSTALL_PREFIX=	/usr/local
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/d_plsf_3.c	Fri Apr 19 01:23:15 2024 +0000
@@ -0,0 +1,199 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : d_plsf_3.c
+*      Purpose          : Decodes the LSP parameters using the received
+*                         quantization indices. 1st order MA prediction and
+*                         split by 3 vector quantization (split-VQ)
+*
+********************************************************************************
+*/
+
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "d_plsf.h"
+
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+#include "basic_op.h"
+#include "no_count.h"
+#include "lsp_lsf.h"
+#include "reorder.h"
+#include "memops.h"
+#include "q_plsf3_tab.h"
+
+/*
+********************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+********************************************************************************
+*/
+
+/* ALPHA    ->  0.9                                            */
+/* ONE_ALPHA-> (1.0-ALPHA)                                     */
+
+#define ALPHA     29491
+#define ONE_ALPHA 3277
+
+/*
+********************************************************************************
+*                         PUBLIC PROGRAM CODE
+********************************************************************************
+*/
+/*************************************************************************
+ *
+ *  FUNCTION:   D_plsf_3()
+ *
+ *  PURPOSE: Decodes the LSP parameters using the received quantization
+ *           indices.1st order MA prediction and split by 3 vector
+ *           quantization (split-VQ)
+ *
+ *************************************************************************/
+
+void D_plsf_3(
+    D_plsfState *st,   /* i/o: State struct                               */
+    enum Mode mode,    /* i  : coder mode                                 */
+    Word16 bfi,        /* i  : bad frame indicator (set to 1 if a         */
+                       /*      bad frame is received)                     */
+    Word16 * indice,   /* i  : quantization indices of 3 submatrices, Q0  */
+    Word16 * lsp1_q    /* o  : quantized 1st LSP vector,              Q15 */
+)
+{
+    Word16 i, index;
+    const Word16 *p_cb1, *p_cb2, *p_cb3, *p_dico;
+    Word16 temp;
+    Word16 lsf1_r[M];
+    Word16 lsf1_q[M];
+    
+    test ();
+    if (bfi != 0)   /* if bad frame */
+    {
+        /* use the past LSFs slightly shifted towards their mean */
+
+        for (i = 0; i < M; i++)
+        {
+            /* lsfi_q[i] = ALPHA*past_lsf_q[i] + ONE_ALPHA*mean_lsf3[i]; */
+
+            lsf1_q[i] = add(mult(st->past_lsf_q[i], ALPHA),
+                            mult(mean_lsf3[i], ONE_ALPHA));
+                                                move16 ();
+        }
+
+        /* estimate past quantized residual to be used in next frame */
+	test();
+	if (sub(mode, MRDTX) != 0) {
+	  for (i = 0; i < M; i++) {
+            /* temp  = mean_lsf3[i] +  past_r2_q[i] * PRED_FAC; */
+	    
+            temp = add(mean_lsf3[i], mult(st->past_r_q[i], pred_fac[i]));
+	    
+            st->past_r_q[i] = sub(lsf1_q[i], temp);                   move16 ();
+	  }
+	} else {
+	  for (i = 0; i < M; i++) {
+            /* temp  = mean_lsf3[i] +  past_r2_q[i]; */
+	    
+            temp = add(mean_lsf3[i], st->past_r_q[i]);
+            st->past_r_q[i] = sub(lsf1_q[i], temp);                   move16 ();
+	  }	  
+	}
+    }
+    else  /* if good LSFs received */
+    {
+       test (); test ();
+       if (sub (mode, MR475) == 0 || sub (mode, MR515) == 0)
+       {   /* MR475, MR515 */
+          p_cb1 = dico1_lsf3;                 move16 ();
+          p_cb2 = dico2_lsf3;                 move16 ();
+          p_cb3 = mr515_3_lsf;                move16 ();
+       }
+       else if (sub (mode, MR795) == 0)
+       {   /* MR795 */
+          test();
+          p_cb1 = mr795_1_lsf;                move16 ();
+          p_cb2 = dico2_lsf3;                 move16 ();
+          p_cb3 = dico3_lsf3;                 move16 ();
+       }
+       else 
+       {   /* MR59, MR67, MR74, MR102, MRDTX */
+          test();          
+          p_cb1 = dico1_lsf3;                 move16 ();
+          p_cb2 = dico2_lsf3;                 move16 ();
+          p_cb3 = dico3_lsf3;                 move16 ();
+       }
+       
+       /* decode prediction residuals from 3 received indices */
+
+        index = *indice++;                      move16 ();
+        p_dico = &p_cb1[add(index, add(index, index))];               move16 ();
+        lsf1_r[0] = *p_dico++;                  move16 ();
+        lsf1_r[1] = *p_dico++;                  move16 ();
+        lsf1_r[2] = *p_dico++;                  move16 ();
+
+        index = *indice++;                      move16 ();
+        
+        test (); test ();
+        if ((sub (mode, MR475) == 0) || (sub (mode, MR515) == 0))
+        {   /* MR475, MR515 only using every second entry */
+            index = shl(index,1);
+        }
+        
+        p_dico = &p_cb2[add(index, add(index, index))];               move16 ();
+        lsf1_r[3] = *p_dico++;                  move16 ();
+        lsf1_r[4] = *p_dico++;                  move16 ();
+        lsf1_r[5] = *p_dico++;                  move16 ();
+
+        index = *indice++;                      move16 ();
+        p_dico = &p_cb3[shl(index, 2)];         move16 ();
+        lsf1_r[6] = *p_dico++;                  move16 ();
+        lsf1_r[7] = *p_dico++;                  move16 ();
+        lsf1_r[8] = *p_dico++;                  move16 ();
+        lsf1_r[9] = *p_dico++;                  move16 ();
+
+        /* Compute quantized LSFs and update the past quantized residual */
+
+	if (sub(mode, MRDTX) != 0) 
+           for (i = 0; i < M; i++) {
+              temp = add(mean_lsf3[i], mult(st->past_r_q[i], pred_fac[i]));
+              lsf1_q[i] = add(lsf1_r[i], temp);   move16 ();
+              st->past_r_q[i] = lsf1_r[i];        move16 ();
+           }
+        else
+           for (i = 0; i < M; i++) {
+              temp = add(mean_lsf3[i], st->past_r_q[i]);
+              lsf1_q[i] = add(lsf1_r[i], temp);   move16 ();
+              st->past_r_q[i] = lsf1_r[i];        move16 ();
+           }
+    }
+
+    /* verification that LSFs has minimum distance of LSF_GAP Hz */
+
+    Reorder_lsf(lsf1_q, LSF_GAP, M);
+
+    Copy (lsf1_q, st->past_lsf_q, M);
+
+    /*  convert LSFs to the cosine domain */
+
+    Lsf_lsp(lsf1_q, lsp1_q, M);
+
+    return;
+}
+
+void Init_D_plsf_3(D_plsfState *st,  /* i/o: State struct                */
+		   Word16 index      /* i  : past_rq_init[] index [0, 7] */)
+{
+  Copy(&past_rq_init[index * M], st->past_r_q, M);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/lsp_lsf.c	Fri Apr 19 01:23:15 2024 +0000
@@ -0,0 +1,113 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : lsp_lsf.c
+*      Purpose          : Lsp_lsf:   Transformation lsp to lsf
+*                       : Lsf_lsp:   Transformation lsf to lsp
+*
+********************************************************************************
+*/
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "lsp_lsf.h"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+#include "basic_op.h"
+#include "no_count.h"
+ 
+/*
+********************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+********************************************************************************
+*/
+#include "lsp_lsf.tab"          /* Look-up table for transformations */
+ 
+/*
+********************************************************************************
+*                         PUBLIC PROGRAM CODE
+********************************************************************************
+*/
+/*************************************************************************
+ *
+ *   FUNCTIONS:  Lsp_lsf and Lsf_lsp
+ *
+ *   PURPOSE:
+ *      Lsp_lsf:   Transformation lsp to lsf
+ *      Lsf_lsp:   Transformation lsf to lsp
+ *
+ *   DESCRIPTION:
+ *         lsp[i] = cos(2*pi*lsf[i]) and lsf[i] = arccos(lsp[i])/(2*pi)
+ *
+ *   The transformation from lsp[i] to lsf[i] and lsf[i] to lsp[i] are
+ *   approximated by a look-up table and interpolation.
+ *
+ *************************************************************************/
+void Lsf_lsp (
+    Word16 lsf[],       /* (i) : lsf[m] normalized (range: 0.0<=val<=0.5) */
+    Word16 lsp[],       /* (o) : lsp[m] (range: -1<=val<1)                */
+    Word16 m            /* (i) : LPC order                                */
+)
+{
+    Word16 i, ind, offset;
+    Word32 L_tmp;
+
+    for (i = 0; i < m; i++)
+    {
+        ind = shr (lsf[i], 8);      /* ind    = b8-b15 of lsf[i] */
+        offset = lsf[i] & 0x00ff;   logic16 (); /* offset = b0-b7  of lsf[i] */
+
+        /* lsp[i] = table[ind]+ ((table[ind+1]-table[ind])*offset) / 256 */
+
+        L_tmp = L_mult (sub (table[ind + 1], table[ind]), offset);
+        lsp[i] = add (table[ind], extract_l (L_shr (L_tmp, 9)));
+                                    move16 (); 
+    }
+    return;
+}
+
+void Lsp_lsf (
+    Word16 lsp[],       /* (i)  : lsp[m] (range: -1<=val<1)                */
+    Word16 lsf[],       /* (o)  : lsf[m] normalized (range: 0.0<=val<=0.5) */
+    Word16 m            /* (i)  : LPC order                                */
+)
+{
+    Word16 i, ind;
+    Word32 L_tmp;
+
+    ind = 63;  move16 ();                      /* begin at end of table -1 */
+
+    for (i = m - 1; i >= 0; i--)
+    {
+        /* find value in table that is just greater than lsp[i] */
+        test (); 
+        while (sub (table[ind], lsp[i]) < 0)
+        {
+            ind--;
+            test (); 
+        }
+
+        /* acos(lsp[i])= ind*256 + ( ( lsp[i]-table[ind] ) *
+           slope[ind] )/4096 */
+
+        L_tmp = L_mult (sub (lsp[i], table[ind]), slope[ind]);
+        /*(lsp[i]-table[ind])*slope[ind])>>12*/
+        lsf[i] = round (L_shl (L_tmp, 3));      move16 (); 
+        lsf[i] = add (lsf[i], shl (ind, 8));    move16 (); 
+    }
+    return;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/lsp_lsf.h	Fri Apr 19 01:23:15 2024 +0000
@@ -0,0 +1,48 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : lsp_lsf.h
+*      Purpose          : Lsp_lsf:   Transformation lsp to lsf
+*                       : Lsf_lsp:   Transformation lsf to lsp
+*
+********************************************************************************
+*/
+#ifndef lsp_lsf_h
+#define lsp_lsf_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+ 
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+void Lsf_lsp (
+    Word16 lsf[],      /* (i)    : lsf[m] normalized (range: 0.0<=val<=0.5) */
+    Word16 lsp[],      /* (o)    : lsp[m] (range: -1<=val<1)                */
+    Word16 m           /* (i)    : LPC order                                */
+);
+void Lsp_lsf (
+    Word16 lsp[],      /* (i)    : lsp[m] (range: -1<=val<1)                */
+    Word16 lsf[],      /* (o)    : lsf[m] normalized (range: 0.0<=val<=0.5) */
+    Word16 m           /* (i)    : LPC order                                */
+);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/lsp_lsf.tab	Fri Apr 19 01:23:15 2024 +0000
@@ -0,0 +1,45 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : lsp_lsf.tab
+*      Purpose          : Tables for function Lsf_lsp() and Lsp_lsf()
+*      $Id $
+*
+********************************************************************************
+*/
+/* table of cos(x) */
+
+static const Word16 table[65] =
+{
+    32767, 32729, 32610, 32413, 32138, 31786, 31357, 30853,
+    30274, 29622, 28899, 28106, 27246, 26320, 25330, 24279,
+    23170, 22006, 20788, 19520, 18205, 16846, 15447, 14010,
+    12540, 11039, 9512, 7962, 6393, 4808, 3212, 1608,
+    0, -1608, -3212, -4808, -6393, -7962, -9512, -11039,
+    -12540, -14010, -15447, -16846, -18205, -19520, -20788, -22006,
+    -23170, -24279, -25330, -26320, -27246, -28106, -28899, -29622,
+    -30274, -30853, -31357, -31786, -32138, -32413, -32610, -32729,
+    (Word16) 0x8000
+};
+
+/* 0x8000 = -32768 (used to silence the compiler) */
+
+/* slope used to compute y = acos(x) */
+
+static const Word16 slope[64] =
+{
+    -26887, -8812, -5323, -3813, -2979, -2444, -2081, -1811,
+    -1608, -1450, -1322, -1219, -1132, -1059, -998, -946,
+    -901, -861, -827, -797, -772, -750, -730, -713,
+    -699, -687, -677, -668, -662, -657, -654, -652,
+    -652, -654, -657, -662, -668, -677, -687, -699,
+    -713, -730, -750, -772, -797, -827, -861, -901,
+    -946, -998, -1059, -1132, -1219, -1322, -1450, -1608,
+    -1811, -2081, -2444, -2979, -3813, -5323, -8812, -26887
+};
--- a/libtwamr/namespace.h	Fri Apr 19 01:08:39 2024 +0000
+++ b/libtwamr/namespace.h	Fri Apr 19 01:23:15 2024 +0000
@@ -69,6 +69,9 @@
 #define	D_plsf_5	AMR__D_plsf_5
 #define	D_plsf_3	AMR__D_plsf_3
 #define	Init_D_plsf_3	AMR__Init_D_plsf_3
+#define	Lsf_lsp		AMR__Lsf_lsp
+#define	Lsp_lsf		AMR__Lsp_lsf
+#define	Reorder_lsf	AMR__Reorder_lsf
 
 #define	agc		AMR__agc
 #define	agc2		AMR__agc2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/reorder.c	Fri Apr 19 01:23:15 2024 +0000
@@ -0,0 +1,74 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : reorder.c
+*      Purpose          : To make sure that the LSFs are properly ordered
+*                       : and to keep a certain minimum distance between
+*                       : adjacent LSFs. 
+*
+********************************************************************************
+*/
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "reorder.h"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+#include "basic_op.h"
+#include "no_count.h"
+ 
+/*
+********************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         PUBLIC PROGRAM CODE
+********************************************************************************
+*/
+/*************************************************************************
+ *
+ *  FUNCTION:  Reorder_lsf()
+ *
+ *  PURPOSE: To make sure that the LSFs are properly ordered and to keep a
+ *           certain minimum distance between adjacent LSFs.
+ *
+ *           The LSFs are in the frequency range 0-0.5 and represented in Q15
+ *
+ *************************************************************************/
+void Reorder_lsf (
+    Word16 *lsf,        /* (i/o)     : vector of LSFs   (range: 0<=val<=0.5) */
+    Word16 min_dist,    /* (i)       : minimum required distance             */
+    Word16 n            /* (i)       : LPC order                             */
+)
+{
+    Word16 i;
+    Word16 lsf_min;
+
+    lsf_min = min_dist;         move16 (); 
+    for (i = 0; i < n; i++)
+    {
+        test (); 
+        if (sub (lsf[i], lsf_min) < 0)
+        {
+            lsf[i] = lsf_min;   move16 (); 
+        }
+        lsf_min = add (lsf[i], min_dist);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/reorder.h	Fri Apr 19 01:23:15 2024 +0000
@@ -0,0 +1,44 @@
+/*
+********************************************************************************
+*
+*      GSM AMR-NB speech codec   R98   Version 7.6.0   December 12, 2001
+*                                R99   Version 3.3.0                
+*                                REL-4 Version 4.1.0                
+*
+********************************************************************************
+*
+*      File             : reorder.h
+*      Purpose          : To make sure that the LSFs are properly ordered
+*                       : and to keep a certain minimum distance between
+*                       : adjacent LSFs. 
+*
+********************************************************************************
+*/
+#ifndef reorder_h
+#define reorder_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+ 
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+void Reorder_lsf (
+    Word16 *lsf,       /* (i/o)  : vector of LSFs   (range: 0<=val<=0.5)    */
+    Word16 min_dist,   /* (i)    : minimum required distance                */
+    Word16 n           /* (i)    : LPC order                                */
+);
+ 
+#endif