comparison libtwamr/qgain475.c @ 375:1d2b39027b70

libtwamr: integrate qgain475.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 04:14:52 +0000
parents
children
comparison
equal deleted inserted replaced
374:61047a2912a2 375:1d2b39027b70
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 : qg475.c
11 * Purpose : Quantization of pitch and codebook gains for MR475.
12 *
13 ********************************************************************************
14 */
15
16 /*
17 ********************************************************************************
18 * MODULE INCLUDE FILE AND VERSION ID
19 ********************************************************************************
20 */
21 #include "namespace.h"
22 #include "qgain475.h"
23
24 /*
25 ********************************************************************************
26 * INCLUDE FILES
27 ********************************************************************************
28 */
29 #include "tw_amr.h"
30 #include "typedef.h"
31 #include "basic_op.h"
32 #include "mac_32.h"
33 #include "no_count.h"
34 #include "cnst.h"
35 #include "pow2.h"
36 #include "log2.h"
37 #include "qua_gain_tab.h"
38
39 /*
40 ********************************************************************************
41 * LOCAL VARIABLES AND TABLES
42 ********************************************************************************
43 */
44
45 /* minimum allowed gain code prediction error: 102.887/4096 = 0.0251189 */
46 #define MIN_QUA_ENER ( -5443) /* Q10 <-> log2 (0.0251189) */
47 #define MIN_QUA_ENER_MR122 (-32768) /* Q10 <-> 20*log10(0.0251189) */
48
49 /* minimum allowed gain code prediction error: 32000/4096 = 7.8125 */
50 #define MAX_QUA_ENER ( 3037) /* Q10 <-> log2 (7.8125) */
51 #define MAX_QUA_ENER_MR122 ( 18284) /* Q10 <-> 20*log10(7.8125) */
52
53 /*
54 ********************************************************************************
55 * PRIVATE PROGRAM CODE
56 ********************************************************************************
57 */
58 static void MR475_quant_store_results(
59 gc_predState *pred_st, /* i/o: gain predictor state struct */
60 const Word16 *p, /* i : pointer to selected quantizer table entry */
61 Word16 gcode0, /* i : predicted CB gain, Q(14 - exp_gcode0) */
62 Word16 exp_gcode0, /* i : exponent of predicted CB gain, Q0 */
63 Word16 *gain_pit, /* o : Pitch gain, Q14 */
64 Word16 *gain_cod /* o : Code gain, Q1 */
65 )
66 {
67 Word16 g_code, exp, frac, tmp;
68 Word32 L_tmp;
69
70 Word16 qua_ener_MR122; /* o : quantized energy error, MR122 version Q10 */
71 Word16 qua_ener; /* o : quantized energy error, Q10 */
72
73 /* Read the quantized gains */
74 *gain_pit = *p++; move16 ();
75 g_code = *p++; move16 ();
76
77 /*------------------------------------------------------------------*
78 * calculate final fixed codebook gain: *
79 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
80 * *
81 * gc = gc0 * g *
82 *------------------------------------------------------------------*/
83
84 L_tmp = L_mult(g_code, gcode0);
85 L_tmp = L_shr(L_tmp, sub(10, exp_gcode0));
86 *gain_cod = extract_h(L_tmp);
87
88 /*------------------------------------------------------------------*
89 * calculate predictor update values and update gain predictor: *
90 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
91 * *
92 * qua_ener = log2(g) *
93 * qua_ener_MR122 = 20*log10(g) *
94 *------------------------------------------------------------------*/
95
96 Log2 (L_deposit_l (g_code), &exp, &frac); /* Log2(x Q12) = log2(x) + 12 */
97 exp = sub(exp, 12);
98
99 tmp = shr_r (frac, 5);
100 qua_ener_MR122 = add (tmp, shl (exp, 10));
101
102 L_tmp = Mpy_32_16(exp, frac, 24660); /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
103 qua_ener = round (L_shl (L_tmp, 13)); /* Q12 * Q0 = Q13 -> Q10 */
104
105 gc_pred_update(pred_st, qua_ener_MR122, qua_ener);
106 }
107
108 /*
109 ********************************************************************************
110 * PUBLIC PROGRAM CODE
111 ********************************************************************************
112 */
113
114 /*************************************************************************
115 *
116 * FUNCTION: MR475_update_unq_pred()
117 *
118 * PURPOSE: use optimum codebook gain and update "unquantized"
119 * gain predictor with the (bounded) prediction error
120 *
121 *************************************************************************/
122 void
123 MR475_update_unq_pred(
124 gc_predState *pred_st, /* i/o: gain predictor state struct */
125 Word16 exp_gcode0, /* i : predicted CB gain (exponent MSW), Q0 */
126 Word16 frac_gcode0, /* i : predicted CB gain (exponent LSW), Q15 */
127 Word16 cod_gain_exp, /* i : optimum codebook gain (exponent), Q0 */
128 Word16 cod_gain_frac /* i : optimum codebook gain (fraction), Q15 */
129 )
130 {
131 Word16 tmp, exp, frac;
132 Word16 qua_ener, qua_ener_MR122;
133 Word32 L_tmp;
134
135 /* calculate prediction error factor (given optimum CB gain gcu):
136 *
137 * predErrFact = gcu / gcode0
138 * (limit to MIN_PRED_ERR_FACT <= predErrFact <= MAX_PRED_ERR_FACT
139 * -> limit qua_ener*)
140 *
141 * calculate prediction error (log):
142 *
143 * qua_ener_MR122 = log2(predErrFact)
144 * qua_ener = 20*log10(predErrFact)
145 *
146 */
147
148 if (test(), cod_gain_frac <= 0)
149 {
150 /* if gcu <= 0 -> predErrFact = 0 < MIN_PRED_ERR_FACT */
151 /* -> set qua_ener(_MR122) directly */
152 qua_ener = MIN_QUA_ENER; move16 ();
153 qua_ener_MR122 = MIN_QUA_ENER_MR122; move16 ();
154 }
155 else
156 {
157 /* convert gcode0 from DPF to standard fraction/exponent format */
158 /* with normalized frac, i.e. 16384 <= frac <= 32767 */
159 /* Note: exponent correction (exp=exp-14) is done after div_s */
160 frac_gcode0 = extract_l (Pow2 (14, frac_gcode0));
161
162 /* make sure cod_gain_frac < frac_gcode0 for div_s */
163 if (test (), sub(cod_gain_frac, frac_gcode0) >= 0)
164 {
165 cod_gain_frac = shr (cod_gain_frac, 1);
166 cod_gain_exp = add (cod_gain_exp, 1);
167 }
168
169 /*
170 predErrFact
171 = gcu / gcode0
172 = cod_gain_frac/frac_gcode0 * 2^(cod_gain_exp-(exp_gcode0-14))
173 = div_s (c_g_f, frac_gcode0)*2^-15 * 2^(c_g_e-exp_gcode0+14)
174 = div_s * 2^(cod_gain_exp-exp_gcode0 - 1)
175 */
176 frac = div_s (cod_gain_frac, frac_gcode0);
177 tmp = sub (sub (cod_gain_exp, exp_gcode0), 1);
178
179 Log2 (L_deposit_l (frac), &exp, &frac);
180 exp = add (exp, tmp);
181
182 /* calculate prediction error (log2, Q10) */
183 qua_ener_MR122 = shr_r (frac, 5);
184 qua_ener_MR122 = add (qua_ener_MR122, shl (exp, 10));
185
186 if (test (), sub(qua_ener_MR122, MIN_QUA_ENER_MR122) < 0)
187 {
188 qua_ener = MIN_QUA_ENER; move16 ();
189 qua_ener_MR122 = MIN_QUA_ENER_MR122; move16 ();
190 }
191 else if (test (), sub(qua_ener_MR122, MAX_QUA_ENER_MR122) > 0)
192 {
193 qua_ener = MAX_QUA_ENER; move16 ();
194 qua_ener_MR122 = MAX_QUA_ENER_MR122; move16 ();
195 }
196 else
197 {
198 /* calculate prediction error (20*log10, Q10) */
199 L_tmp = Mpy_32_16(exp, frac, 24660);
200 /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
201 qua_ener = round (L_shl (L_tmp, 13));
202 /* Q12 * Q0 = Q13 -> Q26 -> Q10 */
203 }
204 }
205
206 /* update MA predictor memory */
207 gc_pred_update(pred_st, qua_ener_MR122, qua_ener);
208 }
209
210
211 /*************************************************************************
212 *
213 * FUNCTION: MR475_gain_quant()
214 *
215 * PURPOSE: Quantization of pitch and codebook gains for two subframes
216 * (using predicted codebook gain)
217 *
218 *************************************************************************/
219 Word16
220 MR475_gain_quant( /* o : index of quantization. */
221 gc_predState *pred_st, /* i/o: gain predictor state struct */
222
223 /* data from subframe 0 (or 2) */
224 Word16 sf0_exp_gcode0, /* i : predicted CB gain (exponent), Q0 */
225 Word16 sf0_frac_gcode0, /* i : predicted CB gain (fraction), Q15 */
226 Word16 sf0_exp_coeff[], /* i : energy coeff. (5), exponent part, Q0 */
227 Word16 sf0_frac_coeff[], /* i : energy coeff. (5), fraction part, Q15 */
228 /* (frac_coeff and exp_coeff computed in */
229 /* calc_filt_energies()) */
230 Word16 sf0_exp_target_en, /* i : exponent of target energy, Q0 */
231 Word16 sf0_frac_target_en, /* i : fraction of target energy, Q15 */
232
233 /* data from subframe 1 (or 3) */
234 Word16 sf1_code_nosharp[], /* i : innovative codebook vector (L_SUBFR) */
235 /* (whithout pitch sharpening) */
236 Word16 sf1_exp_gcode0, /* i : predicted CB gain (exponent), Q0 */
237 Word16 sf1_frac_gcode0, /* i : predicted CB gain (fraction), Q15 */
238 Word16 sf1_exp_coeff[], /* i : energy coeff. (5), exponent part, Q0 */
239 Word16 sf1_frac_coeff[], /* i : energy coeff. (5), fraction part, Q15 */
240 /* (frac_coeff and exp_coeff computed in */
241 /* calc_filt_energies()) */
242 Word16 sf1_exp_target_en, /* i : exponent of target energy, Q0 */
243 Word16 sf1_frac_target_en, /* i : fraction of target energy, Q15 */
244
245 Word16 gp_limit, /* i : pitch gain limit */
246
247 Word16 *sf0_gain_pit, /* o : Pitch gain, Q14 */
248 Word16 *sf0_gain_cod, /* o : Code gain, Q1 */
249
250 Word16 *sf1_gain_pit, /* o : Pitch gain, Q14 */
251 Word16 *sf1_gain_cod /* o : Code gain, Q1 */
252 )
253 {
254 const Word16 *p;
255 Word16 i, index = 0;
256 Word16 tmp;
257 Word16 exp;
258 Word16 sf0_gcode0, sf1_gcode0;
259 Word16 g_pitch, g2_pitch, g_code, g2_code, g_pit_cod;
260 Word16 coeff[10], coeff_lo[10], exp_max[10]; /* 0..4: sf0; 5..9: sf1 */
261 Word32 L_tmp, dist_min;
262
263 /*-------------------------------------------------------------------*
264 * predicted codebook gain *
265 * ~~~~~~~~~~~~~~~~~~~~~~~ *
266 * gc0 = 2^exp_gcode0 + 2^frac_gcode0 *
267 * *
268 * gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0) *
269 *-------------------------------------------------------------------*/
270
271 sf0_gcode0 = extract_l(Pow2(14, sf0_frac_gcode0));
272 sf1_gcode0 = extract_l(Pow2(14, sf1_frac_gcode0));
273
274 /*
275 * For each subframe, the error energy (sum) to be minimized consists
276 * of five terms, t[0..4].
277 *
278 * t[0] = gp^2 * <y1 y1>
279 * t[1] = -2*gp * <xn y1>
280 * t[2] = gc^2 * <y2 y2>
281 * t[3] = -2*gc * <xn y2>
282 * t[4] = 2*gp*gc * <y1 y2>
283 *
284 */
285
286 /* sf 0 */
287 /* determine the scaling exponent for g_code: ec = ec0 - 11 */
288 exp = sub(sf0_exp_gcode0, 11);
289
290 /* calculate exp_max[i] = s[i]-1 */
291 exp_max[0] = sub(sf0_exp_coeff[0], 13); move16 ();
292 exp_max[1] = sub(sf0_exp_coeff[1], 14); move16 ();
293 exp_max[2] = add(sf0_exp_coeff[2], add(15, shl(exp, 1))); move16 ();
294 exp_max[3] = add(sf0_exp_coeff[3], exp); move16 ();
295 exp_max[4] = add(sf0_exp_coeff[4], add(1, exp)); move16 ();
296
297 /* sf 1 */
298 /* determine the scaling exponent for g_code: ec = ec0 - 11 */
299 exp = sub(sf1_exp_gcode0, 11);
300
301 /* calculate exp_max[i] = s[i]-1 */
302 exp_max[5] = sub(sf1_exp_coeff[0], 13); move16 ();
303 exp_max[6] = sub(sf1_exp_coeff[1], 14); move16 ();
304 exp_max[7] = add(sf1_exp_coeff[2], add(15, shl(exp, 1))); move16 ();
305 exp_max[8] = add(sf1_exp_coeff[3], exp); move16 ();
306 exp_max[9] = add(sf1_exp_coeff[4], add(1, exp)); move16 ();
307
308 /*-------------------------------------------------------------------*
309 * Gain search equalisation: *
310 * ~~~~~~~~~~~~~~~~~~~~~~~~~ *
311 * The MSE for the two subframes is weighted differently if there *
312 * is a big difference in the corresponding target energies *
313 *-------------------------------------------------------------------*/
314
315 /* make the target energy exponents the same by de-normalizing the
316 fraction of the smaller one. This is necessary to be able to compare
317 them
318 */
319 exp = sf0_exp_target_en - sf1_exp_target_en;
320 test ();
321 if (exp > 0)
322 {
323 sf1_frac_target_en = shr (sf1_frac_target_en, exp);
324 }
325 else
326 {
327 sf0_frac_target_en = shl (sf0_frac_target_en, exp);
328 }
329
330 /* assume no change of exponents */
331 exp = 0; move16 ();
332
333 /* test for target energy difference; set exp to +1 or -1 to scale
334 * up/down coefficients for sf 1
335 */
336 tmp = shr_r (sf1_frac_target_en, 1); /* tmp = ceil(0.5*en(sf1)) */
337 test ();
338 if (sub (tmp, sf0_frac_target_en) > 0) /* tmp > en(sf0)? */
339 {
340 /*
341 * target_energy(sf1) > 2*target_energy(sf0)
342 * -> scale up MSE(sf0) by 2 by adding 1 to exponents 0..4
343 */
344 exp = 1; move16 ();
345 }
346 else
347 {
348 tmp = shr (add (sf0_frac_target_en, 3), 2); /* tmp=ceil(0.25*en(sf0)) */
349 test();
350 if (sub (tmp, sf1_frac_target_en) > 0) /* tmp > en(sf1)? */
351 {
352 /*
353 * target_energy(sf1) < 0.25*target_energy(sf0)
354 * -> scale down MSE(sf0) by 0.5 by subtracting 1 from
355 * coefficients 0..4
356 */
357 exp = -1; move16 ();
358 }
359 }
360
361 for (i = 0; i < 5; i++)
362 {
363 exp_max[i] = add (exp_max[i], exp); move16 ();
364 }
365
366 /*-------------------------------------------------------------------*
367 * Find maximum exponent: *
368 * ~~~~~~~~~~~~~~~~~~~~~~ *
369 * *
370 * For the sum operation, all terms must have the same scaling; *
371 * that scaling should be low enough to prevent overflow. There- *
372 * fore, the maximum scale is determined and all coefficients are *
373 * re-scaled: *
374 * *
375 * exp = max(exp_max[i]) + 1; *
376 * e = exp_max[i]-exp; e <= 0! *
377 * c[i] = c[i]*2^e *
378 *-------------------------------------------------------------------*/
379
380 exp = exp_max[0]; move16 ();
381 for (i = 1; i < 10; i++)
382 {
383 move16(); test();
384 if (sub(exp_max[i], exp) > 0)
385 {
386 exp = exp_max[i]; move16 ();
387 }
388 }
389 exp = add(exp, 1); /* To avoid overflow */
390
391 p = &sf0_frac_coeff[0]; move16 ();
392 for (i = 0; i < 5; i++) {
393 tmp = sub(exp, exp_max[i]);
394 L_tmp = L_deposit_h(*p++);
395 L_tmp = L_shr(L_tmp, tmp);
396 L_Extract(L_tmp, &coeff[i], &coeff_lo[i]);
397 }
398 p = &sf1_frac_coeff[0]; move16 ();
399 for (; i < 10; i++) {
400 tmp = sub(exp, exp_max[i]);
401 L_tmp = L_deposit_h(*p++);
402 L_tmp = L_shr(L_tmp, tmp);
403 L_Extract(L_tmp, &coeff[i], &coeff_lo[i]);
404 }
405
406 /*-------------------------------------------------------------------*
407 * Codebook search: *
408 * ~~~~~~~~~~~~~~~~ *
409 * *
410 * For each pair (g_pitch, g_fac) in the table calculate the *
411 * terms t[0..4] and sum them up; the result is the mean squared *
412 * error for the quantized gains from the table. The index for the *
413 * minimum MSE is stored and finally used to retrieve the quantized *
414 * gains *
415 *-------------------------------------------------------------------*/
416
417 /* start with "infinite" MSE */
418 dist_min = MAX_32; move32();
419
420 p = &table_gain_MR475[0]; move16 ();
421
422 for (i = 0; i < MR475_VQ_SIZE; i++)
423 {
424 /* subframe 0 (and 2) calculations */
425 g_pitch = *p++; move16 ();
426 g_code = *p++; move16 ();
427
428 g_code = mult(g_code, sf0_gcode0);
429 g2_pitch = mult(g_pitch, g_pitch);
430 g2_code = mult(g_code, g_code);
431 g_pit_cod = mult(g_code, g_pitch);
432
433 L_tmp = Mpy_32_16( coeff[0], coeff_lo[0], g2_pitch);
434 L_tmp = Mac_32_16(L_tmp, coeff[1], coeff_lo[1], g_pitch);
435 L_tmp = Mac_32_16(L_tmp, coeff[2], coeff_lo[2], g2_code);
436 L_tmp = Mac_32_16(L_tmp, coeff[3], coeff_lo[3], g_code);
437 L_tmp = Mac_32_16(L_tmp, coeff[4], coeff_lo[4], g_pit_cod);
438
439 tmp = sub (g_pitch, gp_limit);
440
441 /* subframe 1 (and 3) calculations */
442 g_pitch = *p++; move16 ();
443 g_code = *p++; move16 ();
444
445 test (); test (); test ();
446 if (tmp <= 0 && sub(g_pitch, gp_limit) <= 0)
447 {
448 g_code = mult(g_code, sf1_gcode0);
449 g2_pitch = mult(g_pitch, g_pitch);
450 g2_code = mult(g_code, g_code);
451 g_pit_cod = mult(g_code, g_pitch);
452
453 L_tmp = Mac_32_16(L_tmp, coeff[5], coeff_lo[5], g2_pitch);
454 L_tmp = Mac_32_16(L_tmp, coeff[6], coeff_lo[6], g_pitch);
455 L_tmp = Mac_32_16(L_tmp, coeff[7], coeff_lo[7], g2_code);
456 L_tmp = Mac_32_16(L_tmp, coeff[8], coeff_lo[8], g_code);
457 L_tmp = Mac_32_16(L_tmp, coeff[9], coeff_lo[9], g_pit_cod);
458
459 /* store table index if MSE for this index is lower
460 than the minimum MSE seen so far */
461 test ();
462 if (L_sub(L_tmp, dist_min) < (Word32) 0)
463 {
464 dist_min = L_tmp; move32 ();
465 index = i; move16 ();
466 }
467 }
468 }
469
470 /*------------------------------------------------------------------*
471 * read quantized gains and update MA predictor memories *
472 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
473 *------------------------------------------------------------------*/
474
475 /* for subframe 0, the pre-calculated gcode0/exp_gcode0 are the same
476 as those calculated from the "real" predictor using quantized gains */
477 tmp = shl(index, 2);
478 MR475_quant_store_results(pred_st,
479 &table_gain_MR475[tmp],
480 sf0_gcode0,
481 sf0_exp_gcode0,
482 sf0_gain_pit,
483 sf0_gain_cod);
484
485 /* calculate new predicted gain for subframe 1 (this time using
486 the real, quantized gains) */
487 gc_pred(pred_st, MR475, sf1_code_nosharp,
488 &sf1_exp_gcode0, &sf1_frac_gcode0,
489 &sf0_exp_gcode0, &sf0_gcode0); /* last two args are dummy */
490 sf1_gcode0 = extract_l(Pow2(14, sf1_frac_gcode0));
491
492 tmp = add (tmp, 2);
493 MR475_quant_store_results(pred_st,
494 &table_gain_MR475[tmp],
495 sf1_gcode0,
496 sf1_exp_gcode0,
497 sf1_gain_pit,
498 sf1_gain_cod);
499
500 return index;
501 }