changeset 416:48c7f8e8c9af

libtwamr: integrate p_ol_wgh.c
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 07 May 2024 03:15:19 +0000
parents 01c4becb9fda
children f17e42c63058
files libtwamr/Makefile libtwamr/corrwght.tab libtwamr/namespace.list libtwamr/p_ol_wgh.c libtwamr/p_ol_wgh.h
diffstat 5 files changed, 421 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/libtwamr/Makefile	Tue May 07 03:01:01 2024 +0000
+++ b/libtwamr/Makefile	Tue May 07 03:15:19 2024 +0000
@@ -10,13 +10,13 @@
 	g_adapt.o g_code.o g_pitch.o gain_q.o gains_tab.o gc_pred.o gmed_n.o \
 	graytab.o hp_max.o int_lpc.o int_lsf.o inter_36.o inv_sqrt.o lag_wind.o\
 	levinson.o lflg_upd.o log2.o lpc.o lsfwt.o lsp.o lsp_avg.o lsp_az.o \
-	lsp_lsf.o lsp_tab.o mac_32.o oper_32b.o ph_disp.o pitch_fr.o pitch_ol.o\
-	post_pro.o pow2.o pre_big.o pre_proc.o pred_lt.o preemph.o prm2bits.o \
-	prmno.o pstfilt.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 r_fft.o reorder.o residu.o s10_8pf.o set_sign.o \
-	sid_sync.o spreproc.o spstproc.o sqrt_l.o syn_filt.o tls_flags.o \
-	ton_stab.o vad1.o vad2.o vad_reset.o weight_a.o window.o
+	lsp_lsf.o lsp_tab.o mac_32.o oper_32b.o p_ol_wgh.o ph_disp.o pitch_fr.o\
+	pitch_ol.o post_pro.o pow2.o pre_big.o pre_proc.o pred_lt.o preemph.o \
+	prm2bits.o prmno.o pstfilt.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 r_fft.o reorder.o residu.o s10_8pf.o \
+	set_sign.o sid_sync.o spreproc.o spstproc.o sqrt_l.o syn_filt.o \
+	tls_flags.o ton_stab.o vad1.o vad2.o vad_reset.o weight_a.o window.o
 HDRS=	a_refl.h agc.h autocorr.h az_lsp.h b_cn_cod.h basic_op.h bgnscd.h \
 	bitno.h bits2prm.h c1035pf.h c2_11pf.h c2_9pf.h c3_14pf.h c4_17pf.h \
 	c8_31pf.h c_g_aver.h calc_cor.h calc_en.h cbsearch.h cl_ltp.h cnst.h \
@@ -27,13 +27,13 @@
 	gain_q.h gains_tab.h gc_pred.h gmed_n.h graytab.h hp_max.h int_defs.h \
 	int_lpc.h int_lsf.h inter_36.h inv_sqrt.h lag_wind.h levinson.h log2.h \
 	lpc.h lsfwt.h lsp.h lsp_avg.h lsp_az.h lsp_lsf.h lsp_tab.h mac_32.h \
-	memops.h namespace.h no_count.h oper_32b.h ph_disp.h pitch_fr.h \
-	pitch_ol.h post_pro.h pow2.h pre_big.h pre_proc.h pred_lt.h preemph.h \
-	prm2bits.h pstfilt.h q_gain_c.h q_gain_p.h q_plsf.h q_plsf3_tab.h \
-	q_plsf5_tab.h qgain475.h qgain795.h qua_gain.h qua_gain_tab.h reorder.h\
-	residu.h s10_8pf.h set_sign.h sid_sync.h spreproc.h spstproc.h sqrt_l.h\
-	syn_filt.h ton_stab.h tw_amr.h typedef.h vad.h vad1.h vad2.h weight_a.h\
-	window.h
+	memops.h namespace.h no_count.h oper_32b.h p_ol_wgh.h ph_disp.h \
+	pitch_fr.h pitch_ol.h post_pro.h pow2.h pre_big.h pre_proc.h pred_lt.h \
+	preemph.h prm2bits.h pstfilt.h q_gain_c.h q_gain_p.h q_plsf.h \
+	q_plsf3_tab.h q_plsf5_tab.h qgain475.h qgain795.h qua_gain.h \
+	qua_gain_tab.h reorder.h residu.h s10_8pf.h set_sign.h sid_sync.h \
+	spreproc.h spstproc.h sqrt_l.h syn_filt.h ton_stab.h tw_amr.h typedef.h\
+	vad.h vad1.h vad2.h weight_a.h window.h
 LIB=	libtwamr.a
 
 INSTALL_PREFIX=	/usr/local
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/corrwght.tab	Tue May 07 03:15:19 2024 +0000
@@ -0,0 +1,52 @@
+/*
+********************************************************************************
+*
+*      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             : corrwght.tab
+*      Purpose          : Tables for correlation weights
+*      $Id $
+*
+********************************************************************************
+*/
+static const Word16 corrweight[251] = {
+	20473,	20506,	20539,	20572,	20605,	20644,	20677,
+	20716,	20749,	20788,	20821,	20860,	20893,	20932,
+	20972,	21011,	21050,	21089,	21129,	21168,	21207,
+	21247,	21286,	21332,	21371,	21417,	21456,	21502,
+	21542,	21588,	21633,	21679,	21725,	21771,	21817,
+	21863,	21909,	21961,	22007,	22059,	22105,	22158,
+	22210,	22263,	22315,	22367,	22420,	22472,	22531,
+	22584,	22643,	22702,	22761,	22820,	22879,	22938,
+	23003,	23062,	23128,	23193,	23252,	23324,	23390,
+	23455,	23527,	23600,	23665,	23744,	23816,	23888,
+	23967,	24045,	24124,	24202,	24288,	24366,	24451,
+	24537,	24628,	24714,	24805,	24904,	24995,	25094,
+	25192,	25297,	25395,	25500,	25611,	25723,	25834,
+	25952,	26070,	26188,	26313,	26444,	26575,	26706,
+	26844,	26988,	27132,	27283,	27440,	27597,	27761,
+	27931,	28108,	28285,	28475,	28665,	28869,	29078,
+	29295,	29524,	29760,	30002,	30258,	30527,	30808,
+	31457,	32767,	32767,	32767,	32767,	32767,
+	32767,	32767,	31457,	30808,	30527,	30258,	30002,
+	29760,	29524,	29295,	29078,	28869,	28665,	28475,
+	28285,	28108,	27931,	27761,	27597,	27440,	27283,
+	27132,	26988,	26844,	26706,	26575,	26444,	26313,
+	26188,	26070,	25952,	25834,	25723,	25611,	25500,
+	25395,	25297,	25192,	25094,	24995,	24904,	24805,
+	24714,	24628,	24537,	24451,	24366,	24288,	24202,
+	24124,	24045,	23967,	23888,	23816,	23744,	23665,
+	23600,	23527,	23455,	23390,	23324,	23252,	23193,
+	23128,	23062,	23003,	22938,	22879,	22820,	22761,
+	22702,	22643,	22584,	22531,	22472,	22420,	22367,
+	22315,	22263,	22210,	22158,	22105,	22059,	22007,
+	21961,	21909,	21863,	21817,	21771,	21725,	21679,
+	21633,	21588,	21542,	21502,	21456,	21417,	21371,
+	21332,	21286,	21247,	21207,	21168,	21129,	21089,
+	21050,	21011,	20972,	20932,	20893,	20860,	20821,
+	20788,	20749,	20716,	20677,	20644,	20605,	20572,
+	20539,	20506,	20473,	20434,	20401,	20369,	20336};
--- a/libtwamr/namespace.list	Tue May 07 03:01:01 2024 +0000
+++ b/libtwamr/namespace.list	Tue May 07 03:15:19 2024 +0000
@@ -27,7 +27,7 @@
 Int_lsf Interpol_3or6
 Lag_window Levinson Levinson_reset
 Lsf_lsp Lsp_lsf Reorder_lsf Lsf_wt Lsp_Az
-Pitch_fr Pitch_fr_reset Pitch_ol
+Pitch_fr Pitch_fr_reset Pitch_ol Pitch_ol_wgh p_ol_wgh_reset
 Pre_Process Pre_Process_reset Pred_lt_3or6
 Post_Filter Post_Filter_reset Post_Process Post_Process_reset
 Q_plsf_reset Q_plsf_3 Q_plsf_5 Qua_gain
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/p_ol_wgh.c	Tue May 07 03:15:19 2024 +0000
@@ -0,0 +1,290 @@
+/*
+*****************************************************************************
+*
+*      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             : p_ol_wgh.c
+*      Purpose          : Compute the open loop pitch lag with weighting      
+*
+*************************************************************************/
+/*
+********************************************************************************
+*                         MODULE INCLUDE FILE AND VERSION ID
+********************************************************************************
+*/
+#include "namespace.h"
+#include "p_ol_wgh.h"
+
+/*
+*****************************************************************************
+*                         INCLUDE FILES
+*****************************************************************************
+*/
+#include "typedef.h"
+#include "basic_op.h"
+#include "oper_32b.h"
+#include "no_count.h"
+#include "cnst.h"
+#include "corrwght.tab"
+#include "gmed_n.h"
+#include "inv_sqrt.h"
+#include "vad.h"
+#include "calc_cor.h"
+#include "hp_max.h"
+                      
+/*
+*****************************************************************************
+*                         LOCAL VARIABLES AND TABLES
+*****************************************************************************
+*/
+/*************************************************************************
+ *
+ *  FUNCTION:  Lag_max
+ *
+ *  PURPOSE: Find the lag that has maximum correlation of scal_sig[] in a
+ *           given delay range.
+ *
+ *  DESCRIPTION:
+ *      The correlation is given by
+ *           cor[t] = <scal_sig[n],scal_sig[n-t]>,  t=lag_min,...,lag_max
+ *      The functions outputs the maximum correlation after normalization
+ *      and the corresponding lag.
+ *
+ *************************************************************************/
+static Word16 Lag_max ( /* o : lag found                               */
+    vadState *vadSt,    /* i/o : VAD state struct                      */
+    Word32 corr[],      /* i   : correlation vector.                   */
+    Word16 scal_sig[],  /* i : scaled signal.                          */
+    Word16 L_frame,     /* i : length of frame to compute pitch        */
+    Word16 lag_max,     /* i : maximum lag                             */
+    Word16 lag_min,     /* i : minimum lag                             */
+    Word16 old_lag,     /* i : old open-loop lag                       */
+    Word16 *cor_max,    /* o : normalized correlation of selected lag  */
+    Word16 wght_flg,    /* i : is weighting function used              */ 
+    Word16 *gain_flg,   /* o : open-loop flag                          */
+    Flag dtx            /* i   : dtx flag; use dtx=1, do not use dtx=0 */
+    )
+{
+    Word16 i, j;
+    Word16 *p, *p1;
+    Word32 max, t0;
+    Word16 t0_h, t0_l;
+    Word16 p_max;
+    const Word16 *ww, *we;
+    Word32 t1;
+    
+    ww = &corrweight[250];                                 move16 ();
+    we = &corrweight[123 + lag_max - old_lag];             move16 ();
+
+    max = MIN_32;                                          move32 ();
+    p_max = lag_max;                                       move16 ();
+
+    for (i = lag_max; i >= lag_min; i--)
+    {
+       t0 = corr[-i];                                      move32 ();   
+       
+       /* Weighting of the correlation function.   */
+       L_Extract (corr[-i], &t0_h, &t0_l);
+       t0 = Mpy_32_16 (t0_h, t0_l, *ww);
+       ww--;                                               move16();
+       test ();
+       if (wght_flg > 0) {
+          /* Weight the neighbourhood of the old lag. */
+          L_Extract (t0, &t0_h, &t0_l);
+          t0 = Mpy_32_16 (t0_h, t0_l, *we);
+          we--;                                            move16();
+       }
+       
+       test (); 
+       if (L_sub (t0, max) >= 0)
+       {
+          max = t0;                                        move32 (); 
+          p_max = i;                                       move16 (); 
+       }
+    }
+    
+    p  = &scal_sig[0];                                     move16 (); 
+    p1 = &scal_sig[-p_max];                                move16 (); 
+    t0 = 0;                                                move32 (); 
+    t1 = 0;                                                move32 (); 
+    
+    for (j = 0; j < L_frame; j++, p++, p1++)
+    {
+       t0 = L_mac (t0, *p, *p1);               
+       t1 = L_mac (t1, *p1, *p1);
+    }
+
+    if (dtx)
+    {  /* no test() call since this if is only in simulation env */
+	if (vadSt->use_vad2) {
+		/* Save max correlation */
+		vadSt->u.v2.L_Rmax = L_add(vadSt->u.v2.L_Rmax, t0);
+		/* Save max energy */
+		vadSt->u.v2.L_R0 =   L_add(vadSt->u.v2.L_R0, t1);
+	} else {
+		/* update and detect tone */
+		vad_tone_detection_update (&vadSt->u.v1, 0);
+		vad_tone_detection (&vadSt->u.v1, t0, t1);
+	}
+    }
+
+    /* gain flag is set according to the open_loop gain */
+    /* is t2/t1 > 0.4 ? */    
+    *gain_flg = round(L_msu(t0, round(t1), 13107));        move16(); 
+    
+    *cor_max = 0;                                          move16 ();
+
+    return (p_max);
+}
+
+/*
+*****************************************************************************
+*                         PUBLIC PROGRAM CODE
+*****************************************************************************
+*/
+
+/*************************************************************************
+*
+*  Function:   p_ol_wgh_reset
+*  Purpose:    Initializes state memory to zero
+*
+**************************************************************************
+*/
+void p_ol_wgh_reset (pitchOLWghtState *st)
+{
+   /* Reset pitch search states */
+   st->old_T0_med = 40;
+   st->ada_w = 0;
+   st->wght_flg = 0; 
+}
+ 
+/*************************************************************************
+*
+*  Function:   p_ol_wgh
+*  Purpose:    open-loop pitch search with weighting
+*
+**************************************************************************
+*/
+Word16 Pitch_ol_wgh (     /* o   : open loop pitch lag                            */
+    pitchOLWghtState *st, /* i/o : State struct                                   */
+    vadState *vadSt,      /* i/o : VAD state struct                               */
+    Word16 signal[],      /* i   : signal used to compute the open loop pitch     */
+                          /*       signal[-pit_max] to signal[-1] should be known */
+    Word16 pit_min,       /* i   : minimum pitch lag                              */
+    Word16 pit_max,       /* i   : maximum pitch lag                              */
+    Word16 L_frame,       /* i   : length of frame to compute pitch               */
+    Word16 old_lags[],    /* i   : history with old stored Cl lags                */
+    Word16 ol_gain_flg[], /* i   : OL gain flag                                   */
+    Word16 idx,           /* i   : index                                          */
+    Flag dtx              /* i   : dtx flag; use dtx=1, do not use dtx=0          */
+    )
+{
+    Word16 i;
+    Word16 max1;
+    Word16 p_max1;
+    Word32 t0;
+    Word16 corr_hp_max;
+    Word32 corr[PIT_MAX+1], *corr_ptr;    
+
+    /* Scaled signal */
+    Word16 scaled_signal[PIT_MAX + L_FRAME];
+    Word16 *scal_sig;
+
+    scal_sig = &scaled_signal[pit_max];                          move16 (); 
+
+    t0 = 0L;                                                     move32 (); 
+    for (i = -pit_max; i < L_frame; i++)
+    {
+        t0 = L_mac (t0, signal[i], signal[i]);
+    }
+    /*--------------------------------------------------------*
+     * Scaling of input signal.                               *
+     *                                                        *
+     *   if Overflow        -> scal_sig[i] = signal[i]>>2     *
+     *   else if t0 < 1^22  -> scal_sig[i] = signal[i]<<2     *
+     *   else               -> scal_sig[i] = signal[i]        *
+     *--------------------------------------------------------*/
+
+    /*--------------------------------------------------------*
+     *  Verification for risk of overflow.                    *
+     *--------------------------------------------------------*/
+
+    test (); test (); 
+    if (L_sub (t0, MAX_32) == 0L)               /* Test for overflow */
+    {
+        for (i = -pit_max; i < L_frame; i++)
+        {
+            scal_sig[i] = shr (signal[i], 3);   move16 (); 
+        }
+    }
+    else if (L_sub (t0, (Word32) 1048576L) < (Word32) 0)
+    {
+        for (i = -pit_max; i < L_frame; i++)
+        {
+            scal_sig[i] = shl (signal[i], 3);   move16 (); 
+        }
+    }
+    else
+    {
+        for (i = -pit_max; i < L_frame; i++)
+        {
+            scal_sig[i] = signal[i];            move16 (); 
+        }
+    }
+
+    /* calculate all coreelations of scal_sig, from pit_min to pit_max */
+    corr_ptr = &corr[pit_max];                  move32 ();
+    comp_corr (scal_sig, L_frame, pit_max, pit_min, corr_ptr); 
+
+    p_max1 = Lag_max (vadSt, corr_ptr, scal_sig, L_frame, pit_max, pit_min,
+                      st->old_T0_med, &max1, st->wght_flg, &ol_gain_flg[idx],
+                      dtx);
+    move16 ();
+
+    test (); move16 ();
+    if (ol_gain_flg[idx] > 0)
+    {
+       /* Calculate 5-point median of previous lags */
+       for (i = 4; i > 0; i--) /* Shift buffer */
+       {
+          old_lags[i] = old_lags[i-1];              move16 ();
+       }
+       old_lags[0] = p_max1;                        move16 (); 
+       st->old_T0_med = gmed_n (old_lags, 5);       move16 ();
+       st->ada_w = 32767;                           move16 (); /* Q15 = 1.0 */
+    }	
+    else
+    {        
+       st->old_T0_med = p_max1;                     move16 ();
+       st->ada_w = mult(st->ada_w, 29491);      /* = ada_w = ada_w * 0.9 */
+    }
+    
+    test ();
+    if (sub(st->ada_w, 9830) < 0)  /* ada_w - 0.3 */
+    { 
+       st->wght_flg = 0;                            move16 ();
+    } 
+    else
+    {
+       st->wght_flg = 1;                            move16 ();
+    }
+
+    if (dtx && !vadSt->use_vad2)
+    {  /* no test() call since this if is only in simulation env */
+       test ();
+       if (sub(idx, 1) == 0)
+       {
+          /* calculate max high-passed filtered correlation of all lags */
+          hp_max (corr_ptr, scal_sig, L_frame, pit_max, pit_min, &corr_hp_max); 
+          
+          /* update complex background detector */
+          vad_complex_detection_update(&vadSt->u.v1, corr_hp_max);
+       }
+    }
+
+    return (p_max1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libtwamr/p_ol_wgh.h	Tue May 07 03:15:19 2024 +0000
@@ -0,0 +1,64 @@
+/*
+********************************************************************************
+*
+*      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             : p_ol_wgh.h
+*      Purpose          : Compute the open loop pitch lag with weighting.
+*
+********************************************************************************
+*/
+#ifndef p_ol_wgh_h
+#define p_ol_wgh_h "$Id $"
+ 
+/*
+********************************************************************************
+*                         INCLUDE FILES
+********************************************************************************
+*/
+#include "tw_amr.h"
+#include "typedef.h"
+#include "vad.h"
+
+/*
+********************************************************************************
+*                         DEFINITION OF DATA TYPES
+********************************************************************************
+*/
+/* state variable */
+typedef struct {
+   Word16 old_T0_med;
+   Word16 ada_w;
+   Word16 wght_flg; 
+} pitchOLWghtState;
+
+/*
+********************************************************************************
+*                         DECLARATION OF PROTOTYPES
+********************************************************************************
+*/
+ 
+void p_ol_wgh_reset (pitchOLWghtState *st);
+/* reset of pre processing state (i.e. set state memory to zero)
+   returns 0 on success
+ */
+
+Word16 Pitch_ol_wgh (     /* o   : open loop pitch lag                            */
+    pitchOLWghtState *st, /* i/o : State struct                                   */
+    vadState *vadSt,      /* i/o : VAD state struct                               */
+    Word16 signal[],      /* i   : signal used to compute the open loop pitch     */
+                          /*       signal[-pit_max] to signal[-1] should be known */
+    Word16 pit_min,       /* i   : minimum pitch lag                              */
+    Word16 pit_max,       /* i   : maximum pitch lag                              */
+    Word16 L_frame,       /* i   : length of frame to compute pitch               */
+    Word16 old_lags[],    /* i   : history with old stored Cl lags                */
+    Word16 ol_gain_flg[], /* i   : OL gain flag                                   */
+    Word16 idx,           /* i   : index                                          */
+    Flag dtx              /* i   : dtx flag; use dtx=1, do not use dtx=0          */
+    );
+ 
+#endif