changeset 369:a01de4e40540

libtwamr: integrate q_gain_p.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 03:22:07 +0000
parents 3a25bdfad0d8
children 8861f41e4507
files libtwamr/Makefile libtwamr/namespace.list libtwamr/q_gain_p.c libtwamr/q_gain_p.h
diffstat 4 files changed, 176 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libtwamr/Makefile	Mon May 06 03:18:47 2024 +0000
+++ b/libtwamr/Makefile	Mon May 06 03:22:07 2024 +0000
@@ -8,8 +8,9 @@
 	dec_lag6.o dhf_check.o dhf_tables.o e_homing.o ec_gains.o enc_lag3.o \
 	enc_lag6.o ex_ctrl.o g_adapt.o g_code.o g_pitch.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_gain_c.o q_plsf3_tab.o q_plsf5_tab.o qua_gain_tab.o reorder.o\
-	s10_8pf.o set_sign.o sqrt_l.o tls_flags.o window.o
+	prmno.o q_gain_c.o q_gain_p.o q_plsf3_tab.o q_plsf5_tab.o \
+	qua_gain_tab.o reorder.o s10_8pf.o set_sign.o sqrt_l.o tls_flags.o \
+	window.o
 HDRS=	namespace.h
 LIB=	libtwamr.a
 
--- a/libtwamr/namespace.list	Mon May 06 03:18:47 2024 +0000
+++ b/libtwamr/namespace.list	Mon May 06 03:22:07 2024 +0000
@@ -34,7 +34,7 @@
 gain_adapt gain_adapt_reset
 gc_pred gc_pred_copy gc_pred_reset gc_pred_update gc_pred_average_limited
 gmed_n
-q_gain_code
+q_gain_code q_gain_pitch
 
 Bits2prm Prm2bits
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/q_gain_p.c	Mon May 06 03:22:07 2024 +0000
@@ -0,0 +1,127 @@
+/*
+********************************************************************************
+*
+*      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             : q_gain_p.c
+*      Purpose          : Scalar quantization of the pitch gain
+*
+********************************************************************************
+*/
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "q_gain_p.h"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "tw_amr.h"
+#include "typedef.h"
+#include "basic_op.h"
+#include "oper_32b.h"
+#include "no_count.h"
+#include "cnst.h"
+#include "gains_tab.h"
+ 
+/*
+********************************************************************************
+*                         PUBLIC PROGRAM CODE
+********************************************************************************
+*/
+Word16 q_gain_pitch (   /* Return index of quantization                      */
+    enum Mode mode,     /* i  : AMR mode                                     */
+    Word16 gp_limit,    /* i  : pitch gain limit                             */
+    Word16 *gain,       /* i/o: Pitch gain (unquant/quant),              Q14 */
+    Word16 gain_cand[], /* o  : pitch gain candidates (3),   MR795 only, Q14 */ 
+    Word16 gain_cind[]  /* o  : pitch gain cand. indices (3),MR795 only, Q0  */ 
+)
+{
+    Word16 i, index, err, err_min;
+
+    err_min = abs_s (sub (*gain, qua_gain_pitch[0]));
+    index = 0;                                              move16 (); 
+
+    for (i = 1; i < NB_QUA_PITCH; i++)
+    {
+        test ();
+        if (sub (qua_gain_pitch[i], gp_limit) <= 0)
+        {
+            err = abs_s (sub (*gain, qua_gain_pitch[i]));
+            
+            test (); 
+            if (sub (err, err_min) < 0)
+            {
+                err_min = err;                                  move16 (); 
+                index = i;                                      move16 (); 
+            }
+        }
+    }
+
+    test ();
+    if (sub (mode, MR795) == 0)
+    {
+        /* in MR795 mode, compute three gain_pit candidates around the index
+         * found in the quantization loop: the index found and the two direct
+         * neighbours, except for the extreme cases (i=0 or i=NB_QUA_PITCH-1),
+         * where the direct neighbour and the neighbour to that is used.
+         */
+        Word16 ii;
+
+        test ();
+        if (index == 0)
+        {
+            ii = index;                                     move16 ();
+        }
+        else
+        {
+            test (); test ();
+            if (   sub (index, NB_QUA_PITCH-1) == 0
+                || sub (qua_gain_pitch[index+1], gp_limit) > 0)
+            {
+                ii = sub (index, 2);
+            }
+            else
+            {
+                ii = sub (index, 1);
+            }
+        }
+
+        /* store candidate indices and values */
+        for (i = 0; i < 3; i++)
+        {
+            gain_cind[i] = ii;                              move16 ();
+            gain_cand[i] = qua_gain_pitch[ii];              move16 ();
+            ii = add (ii, 1);
+        }
+        
+        *gain = qua_gain_pitch[index];                      move16 (); 
+    }
+    else
+    {
+        /* in MR122 mode, just return the index and gain pitch found.
+         * If bitexactness is required, mask away the two LSBs (because
+         * in the original EFR, gain_pit was scaled Q12)
+         */
+       test ();
+       if (sub(mode, MR122) == 0)
+       {
+          /* clear 2 LSBits */
+          *gain = qua_gain_pitch[index] & 0xFFFC; logic16 (); move16 ();
+       }
+       else
+       {
+          *gain = qua_gain_pitch[index];                      move16 (); 
+       }
+    }
+    return index;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/q_gain_p.h	Mon May 06 03:22:07 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             : q_gain_p.h
+*      Purpose          : Scalar quantization of the pitch gain
+*
+********************************************************************************
+*/
+#ifndef q_gain_p_h
+#define q_gain_p_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "tw_amr.h"
+#include "typedef.h"
+
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+Word16 q_gain_pitch (   /* Return index of quantization                      */
+    enum Mode mode,     /* i  : AMR mode                                     */
+    Word16 gp_limit,    /* i  : pitch gain limit                             */
+    Word16 *gain,       /* i/o: Pitch gain (unquant/quant),              Q14 */
+    Word16 gain_cand[], /* o  : pitch gain candidates (3),   MR795 only, Q14 */ 
+    Word16 gain_cind[]  /* o  : pitch gain cand. indices (3),MR795 only, Q0  */ 
+);
+ 
+#endif