comparison libtwamr/calc_en.c @ 327:2df212a012af

libtwamr: integrate calc_en.c
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 18 Apr 2024 20:28:33 +0000
parents
children
comparison
equal deleted inserted replaced
326:bfe74a9edd5a 327:2df212a012af
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 : calc_en.c
11 * Purpose : (pre-) quantization of pitch gain for MR795
12 *
13 ********************************************************************************
14 */
15
16 /*
17 ********************************************************************************
18 * MODULE INCLUDE FILE AND VERSION ID
19 ********************************************************************************
20 */
21 #include "namespace.h"
22 #include "calc_en.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 "log2.h"
35
36 /*
37 ********************************************************************************
38 * PUBLIC PROGRAM CODE
39 ********************************************************************************
40 */
41
42 /*************************************************************************
43 *
44 * FUNCTION: calc_unfilt_energies
45 *
46 * PURPOSE: calculation of several energy coefficients for unfiltered
47 * excitation signals and the LTP coding gain
48 *
49 * frac_en[0]*2^exp_en[0] = <res res> // LP residual energy
50 * frac_en[1]*2^exp_en[1] = <exc exc> // LTP residual energy
51 * frac_en[2]*2^exp_en[2] = <exc code> // LTP/CB innovation dot product
52 * frac_en[3]*2^exp_en[3] = <lres lres> // LTP residual energy
53 * // (lres = res - gain_pit*exc)
54 * ltpg = log2(LP_res_en / LTP_res_en)
55 *
56 *************************************************************************/
57 void
58 calc_unfilt_energies(
59 Word16 res[], /* i : LP residual, Q0 */
60 Word16 exc[], /* i : LTP excitation (unfiltered), Q0 */
61 Word16 code[], /* i : CB innovation (unfiltered), Q13 */
62 Word16 gain_pit, /* i : pitch gain, Q14 */
63 Word16 L_subfr, /* i : Subframe length */
64
65 Word16 frac_en[], /* o : energy coefficients (4), fraction part, Q15 */
66 Word16 exp_en[], /* o : energy coefficients (4), exponent part, Q0 */
67 Word16 *ltpg /* o : LTP coding gain (log2()), Q13 */
68 )
69 {
70 Word32 s, L_temp;
71 Word16 i, exp, tmp;
72 Word16 ltp_res_en, pred_gain;
73 Word16 ltpg_exp, ltpg_frac;
74
75 /* Compute residual energy */
76 s = L_mac((Word32) 0, res[0], res[0]);
77 for (i = 1; i < L_subfr; i++)
78 s = L_mac(s, res[i], res[i]);
79
80 /* ResEn := 0 if ResEn < 200.0 (= 400 Q1) */
81 test();
82 if (L_sub (s, 400L) < 0)
83 {
84 frac_en[0] = 0; move16 ();
85 exp_en[0] = -15; move16 ();
86 }
87 else
88 {
89 exp = norm_l(s);
90 frac_en[0] = extract_h(L_shl(s, exp)); move16 ();
91 exp_en[0] = sub(15, exp); move16 ();
92 }
93
94 /* Compute ltp excitation energy */
95 s = L_mac((Word32) 0, exc[0], exc[0]);
96 for (i = 1; i < L_subfr; i++)
97 s = L_mac(s, exc[i], exc[i]);
98
99 exp = norm_l(s);
100 frac_en[1] = extract_h(L_shl(s, exp)); move16 ();
101 exp_en[1] = sub(15, exp); move16 ();
102
103 /* Compute scalar product <exc[],code[]> */
104 s = L_mac((Word32) 0, exc[0], code[0]);
105 for (i = 1; i < L_subfr; i++)
106 s = L_mac(s, exc[i], code[i]);
107
108 exp = norm_l(s);
109 frac_en[2] = extract_h(L_shl(s, exp)); move16 ();
110 exp_en[2] = sub(16-14, exp); move16 ();
111
112 /* Compute energy of LTP residual */
113 s = 0L; move32 ();
114 for (i = 0; i < L_subfr; i++)
115 {
116 L_temp = L_mult(exc[i], gain_pit);
117 L_temp = L_shl(L_temp, 1);
118 tmp = sub(res[i], round(L_temp)); /* LTP residual, Q0 */
119 s = L_mac (s, tmp, tmp);
120 }
121
122 exp = norm_l(s);
123 ltp_res_en = extract_h (L_shl (s, exp));
124 exp = sub (15, exp);
125
126 frac_en[3] = ltp_res_en; move16 ();
127 exp_en[3] = exp; move16 ();
128
129 /* calculate LTP coding gain, i.e. energy reduction LP res -> LTP res */
130 test (); test ();
131 if (ltp_res_en > 0 && frac_en[0] != 0)
132 {
133 /* gain = ResEn / LTPResEn */
134 pred_gain = div_s (shr (frac_en[0], 1), ltp_res_en);
135 exp = sub (exp, exp_en[0]);
136
137 /* L_temp = ltpGain * 2^(30 + exp) */
138 L_temp = L_deposit_h (pred_gain);
139 /* L_temp = ltpGain * 2^27 */
140 L_temp = L_shr (L_temp, add (exp, 3));
141
142 /* Log2 = log2() + 27 */
143 Log2(L_temp, &ltpg_exp, &ltpg_frac);
144
145 /* ltpg = log2(LtpGain) * 2^13 --> range: +- 4 = +- 12 dB */
146 L_temp = L_Comp (sub (ltpg_exp, 27), ltpg_frac);
147 *ltpg = round (L_shl (L_temp, 13)); /* Q13 */
148 }
149 else
150 {
151 *ltpg = 0; move16 ();
152 }
153 }
154
155 /*************************************************************************
156 *
157 * FUNCTION: calc_filt_energies
158 *
159 * PURPOSE: calculation of several energy coefficients for filtered
160 * excitation signals
161 *
162 * Compute coefficients need for the quantization and the optimum
163 * codebook gain gcu (for MR475 only).
164 *
165 * coeff[0] = y1 y1
166 * coeff[1] = -2 xn y1
167 * coeff[2] = y2 y2
168 * coeff[3] = -2 xn y2
169 * coeff[4] = 2 y1 y2
170 *
171 *
172 * gcu = <xn2, y2> / <y2, y2> (0 if <xn2, y2> <= 0)
173 *
174 * Product <y1 y1> and <xn y1> have been computed in G_pitch() and
175 * are in vector g_coeff[].
176 *
177 *************************************************************************/
178 void
179 calc_filt_energies(
180 enum Mode mode, /* i : coder mode */
181 Word16 xn[], /* i : LTP target vector, Q0 */
182 Word16 xn2[], /* i : CB target vector, Q0 */
183 Word16 y1[], /* i : Adaptive codebook, Q0 */
184 Word16 Y2[], /* i : Filtered innovative vector, Q12 */
185 Word16 g_coeff[], /* i : Correlations <xn y1> <y1 y1> */
186 /* computed in G_pitch() */
187
188 Word16 frac_coeff[],/* o : energy coefficients (5), fraction part, Q15 */
189 Word16 exp_coeff[], /* o : energy coefficients (5), exponent part, Q0 */
190 Word16 *cod_gain_frac,/* o: optimum codebook gain (fraction part), Q15 */
191 Word16 *cod_gain_exp /* o: optimum codebook gain (exponent part), Q0 */
192 )
193 {
194 Word32 s, ener_init;
195 Word16 i, exp, frac;
196 Word16 y2[L_SUBFR];
197
198 if (test(), sub(mode, MR795) == 0 || sub(mode, MR475) == 0)
199 {
200 ener_init = 0L; move32 ();
201 }
202 else
203 {
204 ener_init = 1L; move32 ();
205 }
206
207 for (i = 0; i < L_SUBFR; i++) {
208 y2[i] = shr(Y2[i], 3); move16 ();
209 }
210
211 frac_coeff[0] = g_coeff[0]; move16 ();
212 exp_coeff[0] = g_coeff[1]; move16 ();
213 frac_coeff[1] = negate(g_coeff[2]); move16 (); /* coeff[1] = -2 xn y1 */
214 exp_coeff[1] = add(g_coeff[3], 1); move16 ();
215
216
217 /* Compute scalar product <y2[],y2[]> */
218
219 s = L_mac(ener_init, y2[0], y2[0]);
220 for (i = 1; i < L_SUBFR; i++)
221 s = L_mac(s, y2[i], y2[i]);
222
223 exp = norm_l(s);
224 frac_coeff[2] = extract_h(L_shl(s, exp)); move16 ();
225 exp_coeff[2] = sub(15 - 18, exp); move16();
226
227 /* Compute scalar product -2*<xn[],y2[]> */
228
229 s = L_mac(ener_init, xn[0], y2[0]);
230 for (i = 1; i < L_SUBFR; i++)
231 s = L_mac(s, xn[i], y2[i]);
232
233 exp = norm_l(s);
234 frac_coeff[3] = negate(extract_h(L_shl(s, exp))); move16 ();
235 exp_coeff[3] = sub(15 - 9 + 1, exp); move16 ();
236
237
238 /* Compute scalar product 2*<y1[],y2[]> */
239
240 s = L_mac(ener_init, y1[0], y2[0]);
241 for (i = 1; i < L_SUBFR; i++)
242 s = L_mac(s, y1[i], y2[i]);
243
244 exp = norm_l(s);
245 frac_coeff[4] = extract_h(L_shl(s, exp)); move16 ();
246 exp_coeff[4] = sub(15 - 9 + 1, exp); move16();
247
248 if (test(), test (), sub(mode, MR475) == 0 || sub(mode, MR795) == 0)
249 {
250 /* Compute scalar product <xn2[],y2[]> */
251
252 s = L_mac(ener_init, xn2[0], y2[0]);
253 for (i = 1; i < L_SUBFR; i++)
254 s = L_mac(s, xn2[i], y2[i]);
255
256 exp = norm_l(s);
257 frac = extract_h(L_shl(s, exp));
258 exp = sub(15 - 9, exp);
259
260
261 if (test (), frac <= 0)
262 {
263 *cod_gain_frac = 0; move16 ();
264 *cod_gain_exp = 0; move16 ();
265 }
266 else
267 {
268 /*
269 gcu = <xn2, y2> / c[2]
270 = (frac>>1)/frac[2] * 2^(exp+1-exp[2])
271 = div_s(frac>>1, frac[2])*2^-15 * 2^(exp+1-exp[2])
272 = div_s * 2^(exp-exp[2]-14)
273 */
274 *cod_gain_frac = div_s (shr (frac,1), frac_coeff[2]); move16 ();
275 *cod_gain_exp = sub (sub (exp, exp_coeff[2]), 14); move16 ();
276
277 }
278 }
279 }
280
281 /*************************************************************************
282 *
283 * FUNCTION: calc_target_energy
284 *
285 * PURPOSE: calculation of target energy
286 *
287 * en = <xn, xn>
288 *
289 *************************************************************************/
290 void
291 calc_target_energy(
292 Word16 xn[], /* i: LTP target vector, Q0 */
293 Word16 *en_exp, /* o: optimum codebook gain (exponent part), Q0 */
294 Word16 *en_frac /* o: optimum codebook gain (fraction part), Q15 */
295 )
296 {
297 Word32 s;
298 Word16 i, exp;
299
300 /* Compute scalar product <xn[], xn[]> */
301 s = L_mac(0L, xn[0], xn[0]);
302 for (i = 1; i < L_SUBFR; i++)
303 s = L_mac(s, xn[i], xn[i]);
304
305 /* s = SUM 2*xn(i) * xn(i) = <xn xn> * 2 */
306 exp = norm_l(s);
307 *en_frac = extract_h(L_shl(s, exp));
308 *en_exp = sub(16, exp); move16();
309 }