changeset 378:ccba5812fa44

libtwamr: integrate gain_q.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 04:38:55 +0000
parents b02e043dcba0
children 176a44ff94a1
files libtwamr/Makefile libtwamr/gain_q.c libtwamr/gain_q.h libtwamr/namespace.list
diffstat 4 files changed, 319 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libtwamr/Makefile	Mon May 06 04:24:17 2024 +0000
+++ b/libtwamr/Makefile	Mon May 06 04:38:55 2024 +0000
@@ -6,12 +6,12 @@
 	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 d_plsf_3.o d_plsf_5.o dec_gain.o dec_lag3.o \
 	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 lsfwt.o lsp_lsf.o mac_32.o \
-	oper_32b.o pow2.o prmno.o q_gain_c.o q_gain_p.o q_plsf.o q_plsf3_tab.o \
-	q_plsf5_tab.o q_plsf_3.o q_plsf_5.o qgain475.o qgain795.o qua_gain.o \
-	qua_gain_tab.o reorder.o s10_8pf.o set_sign.o sqrt_l.o tls_flags.o \
-	window.o
+	enc_lag6.o ex_ctrl.o g_adapt.o g_code.o g_pitch.o gain_q.o gains_tab.o \
+	gc_pred.o gmed_n.o graytab.o inv_sqrt.o log2.o lsfwt.o lsp_lsf.o \
+	mac_32.o oper_32b.o pow2.o prmno.o q_gain_c.o q_gain_p.o q_plsf.o \
+	q_plsf3_tab.o q_plsf5_tab.o q_plsf_3.o q_plsf_5.o qgain475.o qgain795.o\
+	qua_gain.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
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/gain_q.c	Mon May 06 04:38:55 2024 +0000
@@ -0,0 +1,232 @@
+/*
+********************************************************************************
+*
+*      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             : gain_q.c
+*      Purpose          : Quantazation of gains
+*
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "gain_q.h"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "tw_amr.h"
+#include "typedef.h"
+#include "basic_op.h"
+#include "no_count.h"
+#include "qua_gain.h"
+#include "cnst.h"
+#include "g_code.h"
+#include "q_gain_c.h"
+#include "gc_pred.h"
+#include "calc_en.h"
+#include "qgain795.h"
+#include "qgain475.h"
+#include "memops.h"
+
+/*
+********************************************************************************
+*                         PUBLIC PROGRAM CODE
+********************************************************************************
+*/
+
+/*************************************************************************
+*
+*  Function:   gainQuant_reset
+*  Purpose:    Initializes state memory to zero
+*
+**************************************************************************
+*/
+void gainQuant_reset (gainQuantState *state)
+{
+  state->sf0_exp_gcode0 = 0;
+  state->sf0_frac_gcode0 = 0;
+  state->sf0_exp_target_en = 0;
+  state->sf0_frac_target_en = 0;
+
+  Set_zero (state->sf0_exp_coeff, 5);
+  Set_zero (state->sf0_frac_coeff, 5);
+
+  gc_pred_reset(&state->gc_predSt);
+  gc_pred_reset(&state->gc_predUnqSt);
+  gain_adapt_reset(&state->adaptSt);
+}
+ 
+int gainQuant(
+    gainQuantState *st,   /* i/o : State struct                      */
+    enum Mode mode,       /* i   : coder mode                        */
+    Word16 res[],         /* i   : LP residual,                 Q0   */
+    Word16 exc[],         /* i   : LTP excitation (unfiltered), Q0   */
+    Word16 code[],        /* i   : CB innovation (unfiltered),  Q13  */
+                          /*       (unsharpened for MR475)           */
+    Word16 xn[],          /* i   : Target vector.                    */
+    Word16 xn2[],         /* i   : Target vector.                    */
+    Word16 y1[],          /* i   : Adaptive codebook.                */
+    Word16 Y2[],          /* i   : Filtered innovative vector.       */
+    Word16 g_coeff[],     /* i   : Correlations <xn y1> <y1 y1>      */
+                          /*       Compute in G_pitch().             */
+    Word16 even_subframe, /* i   : even subframe indicator flag      */
+    Word16 gp_limit,      /* i   : pitch gain limit                  */
+    Word16 *sf0_gain_pit, /* o   : Pitch gain sf 0.   MR475          */
+    Word16 *sf0_gain_cod, /* o   : Code gain sf 0.    MR475          */
+    Word16 *gain_pit,     /* i/o : Pitch gain.                       */
+    Word16 *gain_cod,     /* o   : Code gain.                        */
+                          /*       MR475: gain_* unquantized in even */
+                          /*       subframes, quantized otherwise    */
+    Word16 **anap         /* o   : Index of quantization             */
+)
+{
+    Word16 exp_gcode0;
+    Word16 frac_gcode0;
+    Word16 qua_ener_MR122;
+    Word16 qua_ener;
+    Word16 frac_coeff[5];
+    Word16 exp_coeff[5];
+    Word16 exp_en, frac_en;
+    Word16 cod_gain_exp, cod_gain_frac;
+            
+    test ();
+    if (sub (mode, MR475) == 0)
+    {
+        test ();
+        if (even_subframe != 0)
+        {
+            /* save position in output parameter stream and current
+               state of codebook gain predictor */
+            st->gain_idx_ptr = (*anap)++;
+            gc_pred_copy(&st->gc_predSt, &st->gc_predUnqSt);
+
+            /* predict codebook gain (using "unquantized" predictor)*/
+            /* (note that code[] is unsharpened in MR475)           */
+            gc_pred(&st->gc_predUnqSt, mode, code,
+                    &st->sf0_exp_gcode0, &st->sf0_frac_gcode0,
+                    &exp_en, &frac_en);
+
+            /* calculate energy coefficients for quantization
+               and store them in state structure (will be used
+               in next subframe when real quantizer is run) */
+            calc_filt_energies(mode, xn, xn2, y1, Y2, g_coeff,
+                               st->sf0_frac_coeff, st->sf0_exp_coeff,
+                               &cod_gain_frac, &cod_gain_exp);
+
+            /* store optimum codebook gain (Q1) */
+            *gain_cod = shl (cod_gain_frac, add (cod_gain_exp, 1));
+                                                         move16 ();
+
+            calc_target_energy(xn,
+                               &st->sf0_exp_target_en, &st->sf0_frac_target_en);
+
+            /* calculate optimum codebook gain and update
+               "unquantized" predictor                    */
+            MR475_update_unq_pred(&st->gc_predUnqSt,
+                                  st->sf0_exp_gcode0, st->sf0_frac_gcode0,
+                                  cod_gain_exp, cod_gain_frac);
+
+            /* the real quantizer is not run here... */
+        }
+        else
+        {
+            /* predict codebook gain (using "unquantized" predictor) */
+            /* (note that code[] is unsharpened in MR475)            */
+            gc_pred(&st->gc_predUnqSt, mode, code,
+                    &exp_gcode0, &frac_gcode0,
+                    &exp_en, &frac_en);
+
+            /* calculate energy coefficients for quantization */
+            calc_filt_energies(mode, xn, xn2, y1, Y2, g_coeff,
+                               frac_coeff, exp_coeff,
+                               &cod_gain_frac, &cod_gain_exp);
+
+            calc_target_energy(xn, &exp_en, &frac_en);
+
+            /* run real (4-dim) quantizer and update real gain predictor */
+            *st->gain_idx_ptr = MR475_gain_quant(
+                &st->gc_predSt,
+                st->sf0_exp_gcode0, st->sf0_frac_gcode0, 
+                st->sf0_exp_coeff,  st->sf0_frac_coeff,
+                st->sf0_exp_target_en, st->sf0_frac_target_en,
+                code,
+                exp_gcode0, frac_gcode0, 
+                exp_coeff, frac_coeff,
+                exp_en, frac_en,
+                gp_limit,
+                sf0_gain_pit, sf0_gain_cod,   
+                gain_pit, gain_cod);
+        }
+    }
+    else
+    {
+        /*-------------------------------------------------------------------*
+         *  predict codebook gain and quantize                               *
+         *  (also compute normalized CB innovation energy for MR795)         *
+         *-------------------------------------------------------------------*/
+        gc_pred(&st->gc_predSt, mode, code, &exp_gcode0, &frac_gcode0,
+                &exp_en, &frac_en);
+
+        test ();
+        if (sub(mode, MR122) == 0)
+        {
+            *gain_cod = G_code (xn2, Y2); move16 ();
+            *(*anap)++ = q_gain_code (mode, exp_gcode0, frac_gcode0,
+                                      gain_cod, &qua_ener_MR122, &qua_ener);
+            move16 ();
+        }
+        else
+        {
+            /* calculate energy coefficients for quantization */
+            calc_filt_energies(mode, xn, xn2, y1, Y2, g_coeff,
+                               frac_coeff, exp_coeff,
+                               &cod_gain_frac, &cod_gain_exp);
+            
+            test ();
+            if (sub (mode, MR795) == 0)
+            {
+                MR795_gain_quant(&st->adaptSt, res, exc, code,
+                                 frac_coeff, exp_coeff,
+                                 exp_en, frac_en,
+                                 exp_gcode0, frac_gcode0, L_SUBFR,
+                                 cod_gain_frac, cod_gain_exp,
+                                 gp_limit, gain_pit, gain_cod,
+                                 &qua_ener_MR122, &qua_ener,
+                                 anap);
+            }
+            else
+            {
+                *(*anap)++ = Qua_gain(mode,
+                                      exp_gcode0, frac_gcode0,
+                                      frac_coeff, exp_coeff, gp_limit,
+                                      gain_pit, gain_cod,
+                                      &qua_ener_MR122, &qua_ener);
+                move16 ();
+            }
+        }
+
+        /*------------------------------------------------------------------*
+         *  update table of past quantized energies                         *
+         *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                         *
+         *  st->past_qua_en(Q10) = 20 * Log10(qua_gain_code) / constant     *
+         *                       = Log2(qua_gain_code)                      *
+         *                       = qua_ener                                 *
+         *                                           constant = 20*Log10(2) *
+         *------------------------------------------------------------------*/
+        gc_pred_update(&st->gc_predSt, qua_ener_MR122, qua_ener);
+    }
+
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/gain_q.h	Mon May 06 04:38:55 2024 +0000
@@ -0,0 +1,80 @@
+/*
+********************************************************************************
+*
+*      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             : gainQuant.h
+*      Purpose          : Quantazation of gains
+*
+********************************************************************************
+*/
+#ifndef gain_q_h
+#define gain_q_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "tw_amr.h"
+#include "typedef.h"
+#include "gc_pred.h"
+#include "g_adapt.h"
+
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+typedef struct {
+    Word16 sf0_exp_gcode0;
+    Word16 sf0_frac_gcode0;
+    Word16 sf0_exp_target_en;
+    Word16 sf0_frac_target_en;
+    Word16 sf0_exp_coeff[5];
+    Word16 sf0_frac_coeff[5];
+    Word16 *gain_idx_ptr;
+    gc_predState     gc_predSt;
+    gc_predState     gc_predUnqSt;
+    GainAdaptState   adaptSt;
+} gainQuantState;
+ 
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+void gainQuant_reset (gainQuantState *st);
+/* reset of pre processing state (i.e. set state memory to zero)
+   returns 0 on success
+ */
+
+int gainQuant(
+    gainQuantState *st,   /* i/o : State struct                      */
+    enum Mode mode,       /* i   : coder mode                        */
+    Word16 res[],         /* i   : LP residual,                 Q0   */
+    Word16 exc[],         /* i   : LTP excitation (unfiltered), Q0   */
+    Word16 code[],        /* i   : CB innovation (unfiltered),  Q13  */
+                          /*       (unsharpened for MR475)           */
+    Word16 xn[],          /* i   : Target vector.                    */
+    Word16 xn2[],         /* i   : Target vector.                    */
+    Word16 y1[],          /* i   : Adaptive codebook.                */
+    Word16 Y2[],          /* i   : Filtered innovative vector.       */
+    Word16 g_coeff[],     /* i   : Correlations <xn y1> <y1 y1>      */
+                          /*       Compute in G_pitch().             */
+    Word16 even_subframe, /* i   : even subframe indicator flag      */
+    Word16 gp_limit,      /* i   : pitch gain limit                  */
+    Word16 *sf0_gain_pit, /* o   : Pitch gain sf 0.   MR475          */
+    Word16 *sf0_gain_cod, /* o   : Code gain sf 0.    MR475          */
+    Word16 *gain_pit,     /* i/o : Pitch gain.                       */
+    Word16 *gain_cod,     /* o   : Code gain.                        */
+                          /*       MR475: gain_* unquantized in even */
+                          /*       subframes, quantized otherwise    */
+    Word16 **anap         /* o   : Index of quantization             */
+);
+
+#endif
--- a/libtwamr/namespace.list	Mon May 06 04:24:17 2024 +0000
+++ b/libtwamr/namespace.list	Mon May 06 04:38:55 2024 +0000
@@ -34,6 +34,7 @@
 cbsearch
 d_gain_code d_gain_pitch
 gain_adapt gain_adapt_reset
+gainQuant gainQuant_reset
 gc_pred gc_pred_copy gc_pred_reset gc_pred_update gc_pred_average_limited
 gmed_n
 q_gain_code q_gain_pitch