changeset 362:9cbd1b5d061f

libtwamr: integrate ec_gains.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 02:08:43 +0000
parents 9aa554f8cf39
children 0349de7c45b7
files libtwamr/Makefile libtwamr/ec_gains.c libtwamr/ec_gains.h libtwamr/namespace.list
diffstat 4 files changed, 410 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libtwamr/Makefile	Mon May 06 01:57:25 2024 +0000
+++ b/libtwamr/Makefile	Mon May 06 02:08:43 2024 +0000
@@ -5,10 +5,10 @@
 	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 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 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 qua_gain_tab.o reorder.o s10_8pf.o \
-	set_sign.o sqrt_l.o tls_flags.o window.o
+	dec_lag6.o dhf_check.o dhf_tables.o e_homing.o ec_gains.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 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/ec_gains.c	Mon May 06 02:08:43 2024 +0000
@@ -0,0 +1,257 @@
+/*
+********************************************************************************
+*
+*      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             : ec_gains.c
+*      Purpose:         : Error concealment for pitch and codebook gains
+*
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "ec_gains.h"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+#include "basic_op.h"
+#include "oper_32b.h"
+#include "no_count.h"
+#include "cnst.h"
+#include "gmed_n.h"
+#include "gc_pred.h"
+#include "gains_tab.h"
+
+/*
+********************************************************************************
+*                         PUBLIC PROGRAM CODE
+********************************************************************************
+*/
+
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_code_reset
+*  Purpose     : Resets state memory
+*
+**************************************************************************
+*/
+void ec_gain_code_reset (ec_gain_codeState *state)
+{
+  Word16 i;
+  
+  for ( i = 0; i < 5; i++)
+      state->gbuf[i] = 1;
+  state->past_gain_code = 0;
+  state->prev_gc = 1;       
+}
+ 
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_code
+*  Purpose     : conceal the codebook gain
+*                Call this function only in BFI (instead of normal gain
+*                decoding function)
+*
+**************************************************************************
+*/
+void ec_gain_code (
+    ec_gain_codeState *st,    /* i/o : State struct                     */
+    gc_predState *pred_state, /* i/o : MA predictor state               */
+    Word16 state,             /* i   : state of the state machine       */
+    Word16 *gain_code         /* o   : decoded innovation gain          */
+)
+{
+    static const Word16 cdown[7] =
+    {
+        32767, 32112, 32112, 32112,
+        32112, 32112, 22937
+    };
+
+    Word16 tmp;
+    Word16 qua_ener_MR122;
+    Word16 qua_ener;
+    
+    /* calculate median of last five gain values */
+    tmp = gmed_n (st->gbuf,5);                                 move16 ();
+
+    /* new gain = minimum(median, past_gain) * cdown[state] */
+    test (); 
+    if (sub (tmp, st->past_gain_code) > 0)
+    {
+        tmp = st->past_gain_code;                              move16 (); 
+    }
+    tmp = mult (tmp, cdown[state]);
+    *gain_code = tmp;                                          move16 (); 
+
+    /* update table of past quantized energies with average of
+     * current values
+     */
+    gc_pred_average_limited(pred_state, &qua_ener_MR122, &qua_ener);
+    gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
+}
+
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_code_update
+*  Purpose     : update the codebook gain concealment state;
+*                limit gain_code if the previous frame was bad
+*                Call this function always after decoding (or concealing)
+*                the gain
+*
+**************************************************************************
+*/
+void ec_gain_code_update (
+    ec_gain_codeState *st,    /* i/o : State struct                     */
+    Word16 bfi,               /* i   : flag: frame is bad               */
+    Word16 prev_bf,           /* i   : flag: previous frame was bad     */
+    Word16 *gain_code         /* i/o : decoded innovation gain          */
+)
+{
+    Word16 i;
+
+    /* limit gain_code by previous good gain if previous frame was bad */
+    test ();
+    if (bfi == 0)
+    {
+	test ();
+        if (prev_bf != 0)
+        {
+            test (); 
+            if (sub (*gain_code, st->prev_gc) > 0)
+            {
+                *gain_code = st->prev_gc;     move16 (); 
+            }
+        }
+        st->prev_gc = *gain_code;                          move16 (); 
+    }
+
+    /* update EC states: previous gain, gain buffer */
+    st->past_gain_code = *gain_code;                       move16 (); 
+    
+    for (i = 1; i < 5; i++)
+    {
+        st->gbuf[i - 1] = st->gbuf[i];                     move16 (); 
+    }
+    st->gbuf[4] = *gain_code;                              move16 (); 
+
+    return;
+}
+
+/*
+**************************************************************************
+*
+*  Function:   ec_gain_pitch_reset
+*  Purpose:    Resets state memory
+*
+**************************************************************************
+*/
+void ec_gain_pitch_reset (ec_gain_pitchState *state)
+{
+  Word16 i;
+  
+  for(i = 0; i < 5; i++)
+      state->pbuf[i] = 1640;
+  state->past_gain_pit = 0; 
+  state->prev_gp = 16384;   
+}
+ 
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_pitch
+*  Purpose     : conceal the pitch gain
+*                Call this function only in BFI (instead of normal gain
+*                decoding function)
+*
+**************************************************************************
+*/
+void ec_gain_pitch (
+    ec_gain_pitchState *st, /* i/o : state variables                   */
+    Word16 state,           /* i   : state of the state machine        */
+    Word16 *gain_pitch      /* o   : pitch gain (Q14)                  */
+)
+{
+    static const Word16 pdown[7] =
+    {
+        32767, 32112, 32112, 26214,
+        9830, 6553, 6553
+    };
+
+    Word16 tmp;
+
+    /* calculate median of last five gains */
+    tmp = gmed_n (st->pbuf, 5);                        move16 (); 
+
+    /* new gain = minimum(median, past_gain) * pdown[state] */
+    test (); 
+    if (sub (tmp, st->past_gain_pit) > 0)
+    {
+        tmp = st->past_gain_pit;                       move16 (); 
+    }
+    *gain_pitch = mult (tmp, pdown[state]);
+}
+
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_pitch_update
+*  Purpose     : update the pitch gain concealment state;
+*                limit gain_pitch if the previous frame was bad
+*                Call this function always after decoding (or concealing)
+*                the gain
+*
+**************************************************************************
+*/
+void ec_gain_pitch_update (
+    ec_gain_pitchState *st, /* i/o : state variables                   */
+    Word16 bfi,             /* i   : flag: frame is bad                */
+    Word16 prev_bf,         /* i   : flag: previous frame was bad      */
+    Word16 *gain_pitch      /* i/o : pitch gain                        */
+)
+{
+    Word16 i;
+
+    test (); 
+    if (bfi == 0)
+    {
+        test ();
+        if (prev_bf != 0)
+        {
+            test (); 
+            if (sub (*gain_pitch, st->prev_gp) > 0)
+            {
+                *gain_pitch = st->prev_gp;
+            }
+        }
+        st->prev_gp = *gain_pitch;                         move16 (); 
+    }
+    
+    st->past_gain_pit = *gain_pitch;                       move16 ();
+
+    test (); 
+    if (sub (st->past_gain_pit, 16384) > 0)  /* if (st->past_gain_pit > 1.0) */
+    {
+        st->past_gain_pit = 16384;                         move16 (); 
+    }
+    for (i = 1; i < 5; i++)
+    {
+        st->pbuf[i - 1] = st->pbuf[i];                     move16 (); 
+    }
+    st->pbuf[4] = st->past_gain_pit;                       move16 (); 
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/ec_gains.h	Mon May 06 02:08:43 2024 +0000
@@ -0,0 +1,147 @@
+/*
+********************************************************************************
+*
+*      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             : ec_gains.h
+*      Purpose:         : Error concealment for pitch and codebook gains
+*
+********************************************************************************
+*/
+#ifndef ec_gains_h
+#define ec_gains_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "typedef.h"
+#include "gc_pred.h"
+
+/*
+********************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+********************************************************************************
+*/
+ 
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+typedef struct {
+  Word16 pbuf[5];
+  Word16 past_gain_pit;
+  Word16 prev_gp;
+} ec_gain_pitchState;
+ 
+typedef struct {
+  Word16 gbuf[5];
+  Word16 past_gain_code;
+  Word16 prev_gc;
+} ec_gain_codeState;
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_code_reset
+*  Purpose     : Resets state memory
+*
+**************************************************************************
+*/
+void ec_gain_code_reset (
+    ec_gain_codeState *state
+);
+ 
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_code
+*  Purpose     : conceal the codebook gain
+*                Call this function only in BFI (instead of normal gain
+*                decoding function)
+*
+**************************************************************************
+*/
+void ec_gain_code (
+    ec_gain_codeState *st,    /* i/o : State struct                     */
+    gc_predState *pred_state, /* i/o : MA predictor state               */
+    Word16 state,             /* i   : state of the state machine       */
+    Word16 *gain_code         /* o   : decoded innovation gain          */
+);
+
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_code_update
+*  Purpose     : update the codebook gain concealment state;
+*                limit gain_code if the previous frame was bad
+*                Call this function always after decoding (or concealing)
+*                the gain
+*
+**************************************************************************
+*/
+void ec_gain_code_update (
+    ec_gain_codeState *st,    /* i/o : State struct                     */
+    Word16 bfi,               /* i   : flag: frame is bad               */
+    Word16 prev_bf,           /* i   : flag: previous frame was bad     */
+    Word16 *gain_code         /* i/o : decoded innovation gain          */
+);
+
+/*
+**************************************************************************
+*
+*  Function:   ec_gain_pitch_reset
+*  Purpose:    Resets state memory
+*
+**************************************************************************
+*/
+void ec_gain_pitch_reset (
+    ec_gain_pitchState *state
+);
+ 
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_pitch
+*  Purpose     : conceal the pitch gain
+*                Call this function only in BFI (instead of normal gain
+*                decoding function)
+*
+**************************************************************************
+*/
+void ec_gain_pitch (
+    ec_gain_pitchState *st, /* i/o : state variables                   */
+    Word16 state,           /* i   : state of the state machine        */
+    Word16 *gain_pitch      /* o   : pitch gain (Q14)                  */
+);
+
+/*
+**************************************************************************
+*
+*  Function    : ec_gain_pitch_update
+*  Purpose     : update the pitch gain concealment state;
+*                limit gain_pitch if the previous frame was bad
+*                Call this function always after decoding (or concealing)
+*                the gain
+*
+**************************************************************************
+*/
+void ec_gain_pitch_update (
+    ec_gain_pitchState *st, /* i/o : state variables                   */
+    Word16 bfi,             /* i   : flag: frame is bad               */
+    Word16 prev_bf,         /* i   : flag: previous frame was bad     */
+    Word16 *gain_pitch      /* i/o : pitch gain                        */
+);
+ 
+#endif
--- a/libtwamr/namespace.list	Mon May 06 01:57:25 2024 +0000
+++ b/libtwamr/namespace.list	Mon May 06 02:08:43 2024 +0000
@@ -52,3 +52,5 @@
 decode_2i40_9bits decode_2i40_11bits decode_3i40_14bits decode_4i40_17bits
 dec_8i40_31bits dec_10i40_35bits
 encoder_homing_frame_test
+ec_gain_code_reset ec_gain_code ec_gain_code_update
+ec_gain_pitch_reset ec_gain_pitch ec_gain_pitch_update