comparison libtwamr/g_adapt.c @ 365:2a265be82195

libtwamr: integrate g_adapt.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 03:01:15 +0000
parents
children
comparison
equal deleted inserted replaced
364:3f27ca24c620 365:2a265be82195
1 /*
2 ********************************************************************************
3 *
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
5 * R99 Version 3.3.0
6 * REL-4 Version 4.1.0
7 *
8 ********************************************************************************
9 *
10 * File : g_adapt.c
11 * Purpose : gain adaptation for MR795 gain quantization
12 *
13 ********************************************************************************
14 */
15
16 /*
17 ********************************************************************************
18 * MODULE INCLUDE FILE AND VERSION ID
19 ********************************************************************************
20 */
21 #include "namespace.h"
22 #include "g_adapt.h"
23
24 /*
25 ********************************************************************************
26 * INCLUDE FILES
27 ********************************************************************************
28 */
29 #include "typedef.h"
30 #include "basic_op.h"
31 #include "oper_32b.h"
32 #include "no_count.h"
33 #include "cnst.h"
34 #include "gmed_n.h"
35
36 /*
37 ********************************************************************************
38 * LOCAL VARIABLES AND TABLES
39 ********************************************************************************
40 */
41 #define LTP_GAIN_THR1 2721 /* 2721 Q13 = 0.3322 ~= 1.0 / (10*log10(2)) */
42 #define LTP_GAIN_THR2 5443 /* 5443 Q13 = 0.6644 ~= 2.0 / (10*log10(2)) */
43
44 /*
45 ********************************************************************************
46 * PUBLIC PROGRAM CODE
47 ********************************************************************************
48 */
49
50 /*************************************************************************
51 *
52 * Function: gain_adapt_reset
53 * Purpose: Initializes state memory to zero
54 *
55 **************************************************************************
56 */
57 void gain_adapt_reset (GainAdaptState *st)
58 {
59 Word16 i;
60
61 st->onset = 0;
62 st->prev_alpha = 0;
63 st->prev_gc = 0;
64
65 for (i = 0; i < LTPG_MEM_SIZE; i++)
66 {
67 st->ltpg_mem[i] = 0;
68 }
69 }
70
71 /*************************************************************************
72 *
73 * Function: gain_adapt()
74 * Purpose: calculate pitch/codebook gain adaptation factor alpha
75 * (and update the adaptor state)
76 *
77 **************************************************************************
78 */
79 void gain_adapt(
80 GainAdaptState *st, /* i : state struct */
81 Word16 ltpg, /* i : ltp coding gain (log2()), Q13 */
82 Word16 gain_cod, /* i : code gain, Q1 */
83 Word16 *alpha /* o : gain adaptation factor, Q15 */
84 )
85 {
86 Word16 adapt; /* adaptdation status; 0, 1, or 2 */
87 Word16 result; /* alpha factor, Q13 */
88 Word16 filt; /* median-filtered LTP coding gain, Q13 */
89 Word16 tmp, i;
90
91 /* basic adaptation */
92 test ();
93 if (sub (ltpg, LTP_GAIN_THR1) <= 0)
94 {
95 adapt = 0; move16 ();
96 }
97 else
98 {
99 test ();
100 if (sub (ltpg, LTP_GAIN_THR2) <= 0)
101 {
102 adapt = 1; move16 ();
103 }
104 else
105 {
106 adapt = 2; move16 ();
107 }
108 }
109
110 /*
111 * // onset indicator
112 * if ((cbGain > onFact * cbGainMem[0]) && (cbGain > 100.0))
113 * onset = 8;
114 * else
115 * if (onset)
116 * onset--;
117 */
118 /* tmp = cbGain / onFact; onFact = 2.0; 200 Q1 = 100.0 */
119 tmp = shr_r (gain_cod, 1);
120 test (); test ();
121 if ((sub (tmp, st->prev_gc) > 0) && sub(gain_cod, 200) > 0)
122 {
123 st->onset = 8; move16 ();
124 }
125 else
126 {
127 test ();
128 if (st->onset != 0)
129 {
130 st->onset = sub (st->onset, 1); move16 ();
131 }
132 }
133
134 /*
135 * // if onset, increase adaptor state
136 * if (onset && (gainAdapt < 2)) gainAdapt++;
137 */
138 test(); test ();
139 if ((st->onset != 0) && (sub (adapt, 2) < 0))
140 {
141 adapt = add (adapt, 1);
142 }
143
144 st->ltpg_mem[0] = ltpg; move16 ();
145 filt = gmed_n (st->ltpg_mem, 5); move16 (); /* function result */
146
147 test ();
148 if (adapt == 0)
149 {
150 test ();
151 if (sub (filt, 5443) > 0) /* 5443 Q13 = 0.66443... */
152 {
153 result = 0; move16 ();
154 }
155 else
156 {
157 test ();
158 if (filt < 0)
159 {
160 result = 16384; move16 (); /* 16384 Q15 = 0.5 */
161 }
162 else
163 { /* result = 0.5 - 0.75257499*filt */
164 /* result (Q15) = 16384 - 24660 * (filt << 2) */
165 filt = shl (filt, 2); /* Q15 */
166 result = sub (16384, mult (24660, filt));
167 }
168 }
169 }
170 else
171 {
172 result = 0; move16 ();
173 }
174 /*
175 * if (prevAlpha == 0.0) result = 0.5 * (result + prevAlpha);
176 */
177 test ();
178 if (st->prev_alpha == 0)
179 {
180 result = shr (result, 1);
181 }
182
183 /* store the result */
184 *alpha = result; move16 ();
185
186 /* update adapter state memory */
187 st->prev_alpha = result; move16 ();
188 st->prev_gc = gain_cod; move16 ();
189
190 for (i = LTPG_MEM_SIZE-1; i > 0; i--)
191 {
192 st->ltpg_mem[i] = st->ltpg_mem[i-1]; move16 ();
193 }
194 /* mem[0] is just present for convenience in calling the gmed_n[5]
195 * function above. The memory depth is really LTPG_MEM_SIZE-1.
196 */
197 }