FreeCalypso > hg > gsm-codec-lib
annotate libtwamr/qgain795.c @ 542:f2d0f2f15d5f
libgsmefr: add wrapper for TW-TS-001 RTP input
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 28 Sep 2024 06:38:08 +0000 |
parents | 2aa98051d445 |
children |
rev | line source |
---|---|
376
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * R99 Version 3.3.0 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * REL-4 Version 4.1.0 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 * File : qgain795.c |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 * Purpose : pitch and codebook gain quantization for MR795 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 * MODULE INCLUDE FILE AND VERSION ID |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 #include "namespace.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 #include "qgain795.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 * INCLUDE FILES |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 #include "typedef.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 #include "basic_op.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 #include "oper_32b.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 #include "no_count.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 #include "cnst.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 #include "log2.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 #include "pow2.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 #include "sqrt_l.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 #include "g_adapt.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 #include "calc_en.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 #include "q_gain_p.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 #include "mac_32.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 #include "gains_tab.h" |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 * LOCAL PROGRAM CODE |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 /************************************************************************* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 * FUNCTION: MR795_gain_code_quant3 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 * PURPOSE: Pre-quantization of codebook gains, given three possible |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 * LTP gains (using predicted codebook gain) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 *************************************************************************/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 static void |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 MR795_gain_code_quant3( |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 Word16 gcode0, /* i : predicted CB gain (norm.), Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 Word16 g_pitch_cand[], /* i : Pitch gain candidates (3), Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 Word16 g_pitch_cind[], /* i : Pitch gain cand. indices (3), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 Word16 frac_coeff[], /* i : coefficients (5), Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 Word16 exp_coeff[], /* i : energy coefficients (5), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 /* coefficients from calc_filt_ener()*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 Word16 *gain_pit, /* o : Pitch gain, Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 Word16 *gain_pit_ind, /* o : Pitch gain index, Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 Word16 *gain_cod, /* o : Code gain, Q1 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 Word16 *gain_cod_ind, /* o : Code gain index, Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 /* (for MR122 MA predictor update) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 Word16 *qua_ener /* o : quantized energy error, Q10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 /* (for other MA predictor update) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 ) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 const Word16 *p; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 Word16 i, j, cod_ind, pit_ind; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 Word16 e_max, exp_code; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 Word16 g_pitch, g2_pitch, g_code, g2_code_h, g2_code_l; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 Word16 g_pit_cod_h, g_pit_cod_l; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 Word16 coeff[5], coeff_lo[5]; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 Word16 exp_max[5]; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 Word32 L_tmp, L_tmp0, dist_min; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 * The error energy (sum) to be minimized consists of five terms, t[0..4]. |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 * t[0] = gp^2 * <y1 y1> |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 * t[1] = -2*gp * <xn y1> |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 * t[2] = gc^2 * <y2 y2> |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 * t[3] = -2*gc * <xn y2> |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 * t[4] = 2*gp*gc * <y1 y2> |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 /* determine the scaling exponent for g_code: ec = ec0 - 10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 exp_code = sub(exp_gcode0, 10); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 /* calculate exp_max[i] = s[i]-1 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 exp_max[0] = sub(exp_coeff[0], 13); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 exp_max[1] = sub(exp_coeff[1], 14); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 exp_max[2] = add(exp_coeff[2], add(15, shl(exp_code, 1))); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 exp_max[3] = add(exp_coeff[3], exp_code); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 exp_max[4] = add(exp_coeff[4], add(exp_code,1)); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 /*-------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 * Find maximum exponent: * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 * ~~~~~~~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 * * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 * For the sum operation, all terms must have the same scaling; * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 * that scaling should be low enough to prevent overflow. There- * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 * fore, the maximum scale is determined and all coefficients are * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 * re-scaled: * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 * * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 * e_max = max(exp_max[i]) + 1; * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 * e = exp_max[i]-e_max; e <= 0! * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 * c[i] = c[i]*2^e * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 *-------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 e_max = exp_max[0]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 for (i = 1; i < 5; i++) /* implemented flattened */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 move16(); test(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
125 if (sub(exp_max[i], e_max) > 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
126 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
127 e_max = exp_max[i]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
128 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
129 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
130 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
131 e_max = add(e_max, 1); /* To avoid overflow */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
132 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
133 for (i = 0; i < 5; i++) { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
134 j = sub(e_max, exp_max[i]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
135 L_tmp = L_deposit_h(frac_coeff[i]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
136 L_tmp = L_shr(L_tmp, j); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
137 L_Extract(L_tmp, &coeff[i], &coeff_lo[i]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
138 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
139 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
140 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
141 /*-------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
142 * Codebook search: * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
143 * ~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
144 * * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
145 * For each of the candiates LTP gains in g_pitch_cand[], the terms * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
146 * t[0..4] are calculated from the values in the table (and the * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
147 * pitch gain candidate) and summed up; the result is the mean * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
148 * squared error for the LPT/CB gain pair. The index for the mini- * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
149 * mum MSE is stored and finally used to retrieve the quantized CB * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
150 * gain * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
151 *-------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
152 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
153 /* start with "infinite" MSE */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
154 dist_min = MAX_32; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
155 cod_ind = 0; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
156 pit_ind = 0; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
157 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
158 /* loop through LTP gain candidates */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
159 for (j = 0; j < 3; j++) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
160 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
161 /* pre-calculate terms only dependent on pitch gain */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
162 g_pitch = g_pitch_cand[j]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
163 g2_pitch = mult(g_pitch, g_pitch); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
164 L_tmp0 = Mpy_32_16( coeff[0], coeff_lo[0], g2_pitch); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
165 L_tmp0 = Mac_32_16(L_tmp0, coeff[1], coeff_lo[1], g_pitch); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
166 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
167 p = &qua_gain_code[0]; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
168 for (i = 0; i < NB_QUA_CODE; i++) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
169 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
170 g_code = *p++; move16 (); /* this is g_fac Q11 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
171 p++; /* skip log2(g_fac) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
172 p++; /* skip 20*log10(g_fac) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
173 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
174 g_code = mult(g_code, gcode0); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
175 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
176 L_tmp = L_mult (g_code, g_code); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
177 L_Extract (L_tmp, &g2_code_h, &g2_code_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
178 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
179 L_tmp = L_mult(g_code, g_pitch); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
180 L_Extract (L_tmp, &g_pit_cod_h, &g_pit_cod_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
181 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
182 L_tmp = Mac_32 (L_tmp0, coeff[2], coeff_lo[2], |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
183 g2_code_h, g2_code_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
184 L_tmp = Mac_32_16(L_tmp, coeff[3], coeff_lo[3], |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
185 g_code); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
186 L_tmp = Mac_32 (L_tmp, coeff[4], coeff_lo[4], |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
187 g_pit_cod_h, g_pit_cod_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
188 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
189 /* store table index if MSE for this index is lower |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
190 than the minimum MSE seen so far; also store the |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
191 pitch gain for this (so far) lowest MSE */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
192 test (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
193 if (L_sub(L_tmp, dist_min) < (Word32) 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
194 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
195 dist_min = L_tmp; move32 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
196 cod_ind = i; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
197 pit_ind = j; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
198 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
199 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
200 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
201 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
202 /*------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
203 * read quantized gains and new values for MA predictor memories * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
204 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
205 *------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
206 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
207 /* Read the quantized gains */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
208 p = &qua_gain_code[add (add (cod_ind, cod_ind), cod_ind)]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
209 g_code = *p++; move16(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
210 *qua_ener_MR122 = *p++; move16(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
211 *qua_ener = *p; move16(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
212 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
213 /*------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
214 * calculate final fixed codebook gain: * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
215 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
216 * * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
217 * gc = gc0 * g * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
218 *------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
219 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
220 L_tmp = L_mult(g_code, gcode0); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
221 L_tmp = L_shr(L_tmp, sub(9, exp_gcode0)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
222 *gain_cod = extract_h(L_tmp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
223 *gain_cod_ind = cod_ind; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
224 *gain_pit = g_pitch_cand[pit_ind]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
225 *gain_pit_ind = g_pitch_cind[pit_ind]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
226 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
227 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
228 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
229 /************************************************************************* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
230 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
231 * FUNCTION: MR795_gain_code_quant_mod |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
232 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
233 * PURPOSE: Modified quantization of the MR795 codebook gain |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
234 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
235 * Uses pre-computed energy coefficients in frac_en[]/exp_en[] |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
236 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
237 * frac_en[0]*2^exp_en[0] = <res res> // LP residual energy |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
238 * frac_en[1]*2^exp_en[1] = <exc exc> // LTP residual energy |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
239 * frac_en[2]*2^exp_en[2] = <exc code> // LTP/CB innovation dot product |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
240 * frac_en[3]*2^exp_en[3] = <code code> // CB innovation energy |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
241 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
242 *************************************************************************/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
243 static Word16 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
244 MR795_gain_code_quant_mod( /* o : index of quantization. */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
245 Word16 gain_pit, /* i : pitch gain, Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
246 Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
247 Word16 gcode0, /* i : predicted CB gain (norm.), Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
248 Word16 frac_en[], /* i : energy coefficients (4), |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
249 fraction part, Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
250 Word16 exp_en[], /* i : energy coefficients (4), |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
251 eponent part, Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
252 Word16 alpha, /* i : gain adaptor factor (>0), Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
253 Word16 gain_cod_unq, /* i : Code gain (unquantized) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
254 /* (scaling: Q10 - exp_gcode0) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
255 Word16 *gain_cod, /* i/o: Code gain (pre-/quantized), Q1 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
256 Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
257 /* (for MR122 MA predictor update) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
258 Word16 *qua_ener /* o : quantized energy error, Q10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
259 /* (for other MA predictor update) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
260 ) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
261 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
262 const Word16 *p; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
263 Word16 i, index, tmp; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
264 Word16 one_alpha; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
265 Word16 exp, e_max; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
266 Word16 g2_pitch, g_code; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
267 Word16 g2_code_h, g2_code_l; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
268 Word16 d2_code_h, d2_code_l; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
269 Word16 coeff[5], coeff_lo[5], exp_coeff[5]; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
270 Word32 L_tmp, L_t0, L_t1, dist_min; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
271 Word16 gain_code; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
272 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
273 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
274 Steps in calculation of the error criterion (dist): |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
275 --------------------------------------------------- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
276 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
277 underlined = constant; alp = FLP value of alpha, alpha = FIP |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
278 ---------- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
279 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
280 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
281 ExEn = gp^2 * LtpEn + 2.0*gp*gc[i] * XC + gc[i]^2 * InnEn; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
282 ------------ ------ -- ----- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
283 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
284 aExEn= alp * ExEn |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
285 = alp*gp^2*LtpEn + 2.0*alp*gp*XC* gc[i] + alp*InnEn* gc[i]^2 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
286 -------------- ------------- --------- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
287 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
288 = t[1] + t[2] + t[3] |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
289 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
290 dist = d1 + d2; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
291 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
292 d1 = (1.0 - alp) * InnEn * (gcu - gc[i])^2 = t[4] |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
293 ------------------- --- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
294 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
295 d2 = alp * (ResEn - 2.0 * sqrt(ResEn*ExEn) + ExEn); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
296 --- ----- --- ----- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
297 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
298 = alp * (sqrt(ExEn) - sqrt(ResEn))^2 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
299 --- ----------- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
300 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
301 = (sqrt(aExEn) - sqrt(alp*ResEn))^2 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
302 --------------- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
303 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
304 = (sqrt(aExEn) - t[0] )^2 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
305 ---- |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
306 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
307 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
308 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
309 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
310 * calculate scalings of the constant terms |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
311 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
312 gain_code = shl (*gain_cod, sub (10, exp_gcode0)); /* Q1 -> Q11 (-ec0) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
313 g2_pitch = mult (gain_pit, gain_pit); /* Q14 -> Q13 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
314 /* 0 < alpha <= 0.5 => 0.5 <= 1-alpha < 1, i.e one_alpha is normalized */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
315 one_alpha = add (sub (32767, alpha), 1); /* 32768 - alpha */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
316 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
317 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
318 /* alpha <= 0.5 -> mult. by 2 to keep precision; compensate in exponent */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
319 tmp = extract_h (L_shl (L_mult (alpha, frac_en[1]), 1)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
320 /* directly store in 32 bit variable because no further mult. required */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
321 L_t1 = L_mult (tmp, g2_pitch); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
322 exp_coeff[1] = sub (exp_en[1], 15); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
323 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
324 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
325 tmp = extract_h (L_shl (L_mult (alpha, frac_en[2]), 1)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
326 coeff[2] = mult (tmp, gain_pit); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
327 exp = sub (exp_gcode0, 10); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
328 exp_coeff[2] = add (exp_en[2], exp); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
329 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
330 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
331 /* alpha <= 0.5 -> mult. by 2 to keep precision; compensate in exponent */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
332 coeff[3] = extract_h (L_shl (L_mult (alpha, frac_en[3]), 1)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
333 exp = sub (shl (exp_gcode0, 1), 7); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
334 exp_coeff[3] = add (exp_en[3], exp); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
335 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
336 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
337 coeff[4] = mult (one_alpha, frac_en[3]); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
338 exp_coeff[4] = add (exp_coeff[3], 1); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
339 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
340 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
341 L_tmp = L_mult (alpha, frac_en[0]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
342 /* sqrt_l returns normalized value and 2*exponent |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
343 -> result = val >> (exp/2) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
344 exp_coeff holds 2*exponent for c[0] */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
345 /* directly store in 32 bit variable because no further mult. required */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
346 L_t0 = sqrt_l_exp (L_tmp, &exp); /* normalization included in sqrt_l_exp */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
347 move32 (); /* function result */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
348 exp = add (exp, 47); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
349 exp_coeff[0] = sub (exp_en[0], exp); move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
350 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
351 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
352 * Determine the maximum exponent occuring in the distance calculation |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
353 * and adjust all fractions accordingly (including a safety margin) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
354 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
355 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
356 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
357 /* find max(e[1..4],e[0]+31) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
358 e_max = add (exp_coeff[0], 31); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
359 for (i = 1; i <= 4; i++) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
360 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
361 test (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
362 if (sub (exp_coeff[i], e_max) > 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
363 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
364 e_max = exp_coeff[i]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
365 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
366 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
367 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
368 /* scale c[1] (requires no further multiplication) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
369 tmp = sub (e_max, exp_coeff[1]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
370 L_t1 = L_shr(L_t1, tmp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
371 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
372 /* scale c[2..4] (used in Mpy_32_16 in the quantizer loop) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
373 for (i = 2; i <= 4; i++) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
374 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
375 tmp = sub (e_max, exp_coeff[i]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
376 L_tmp = L_deposit_h(coeff[i]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
377 L_tmp = L_shr(L_tmp, tmp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
378 L_Extract(L_tmp, &coeff[i], &coeff_lo[i]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
379 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
380 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
381 /* scale c[0] (requires no further multiplication) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
382 exp = sub (e_max, 31); /* new exponent */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
383 tmp = sub (exp, exp_coeff[0]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
384 L_t0 = L_shr (L_t0, shr (tmp, 1)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
385 /* perform correction by 1/sqrt(2) if exponent difference is odd */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
386 test (); logic16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
387 if ((tmp & 0x1) != 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
388 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
389 L_Extract(L_t0, &coeff[0], &coeff_lo[0]); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
390 L_t0 = Mpy_32_16(coeff[0], coeff_lo[0], |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
391 23170); /* 23170 Q15 = 1/sqrt(2)*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
392 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
393 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
394 /* search the quantizer table for the lowest value |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
395 of the search criterion */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
396 dist_min = MAX_32; move32 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
397 index = 0; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
398 p = &qua_gain_code[0]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
399 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
400 for (i = 0; i < NB_QUA_CODE; i++) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
401 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
402 g_code = *p++; move16 (); /* this is g_fac (Q11) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
403 p++; /* skip log2(g_fac) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
404 p++; /* skip 20*log10(g_fac) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
405 g_code = mult (g_code, gcode0); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
406 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
407 /* only continue if gc[i] < 2.0*gc |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
408 which is equiv. to g_code (Q10-ec0) < gain_code (Q11-ec0) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
409 test (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
410 if (sub (g_code, gain_code) >= 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
411 break; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
412 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
413 L_tmp = L_mult (g_code, g_code); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
414 L_Extract (L_tmp, &g2_code_h, &g2_code_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
415 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
416 tmp = sub (g_code, gain_cod_unq); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
417 L_tmp = L_mult (tmp, tmp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
418 L_Extract (L_tmp, &d2_code_h, &d2_code_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
419 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
420 /* t2, t3, t4 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
421 L_tmp = Mac_32_16 (L_t1, coeff[2], coeff_lo[2], g_code); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
422 L_tmp = Mac_32(L_tmp, coeff[3], coeff_lo[3], g2_code_h, g2_code_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
423 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
424 L_tmp = sqrt_l_exp (L_tmp, &exp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
425 L_tmp = L_shr (L_tmp, shr (exp, 1)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
426 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
427 /* d2 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
428 tmp = round (L_sub (L_tmp, L_t0)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
429 L_tmp = L_mult (tmp, tmp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
430 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
431 /* dist */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
432 L_tmp = Mac_32(L_tmp, coeff[4], coeff_lo[4], d2_code_h, d2_code_l); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
433 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
434 /* store table index if distance measure for this |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
435 index is lower than the minimum seen so far */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
436 test (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
437 if (L_sub (L_tmp, dist_min) < (Word32) 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
438 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
439 dist_min = L_tmp; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
440 index = i; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
441 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
442 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
443 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
444 /*------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
445 * read quantized gains and new values for MA predictor memories * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
446 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
447 *------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
448 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
449 /* Read the quantized gains */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
450 p = &qua_gain_code[add (add (index, index), index)]; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
451 g_code = *p++; move16(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
452 *qua_ener_MR122 = *p++; move16(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
453 *qua_ener = *p; move16(); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
454 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
455 /*------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
456 * calculate final fixed codebook gain: * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
457 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
458 * * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
459 * gc = gc0 * g * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
460 *------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
461 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
462 L_tmp = L_mult(g_code, gcode0); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
463 L_tmp = L_shr(L_tmp, sub(9, exp_gcode0)); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
464 *gain_cod = extract_h(L_tmp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
465 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
466 return index; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
467 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
468 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
469 /* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
470 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
471 * PUBLIC PROGRAM CODE |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
472 ******************************************************************************** |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
473 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
474 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
475 /************************************************************************* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
476 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
477 * FUNCTION: MR795_gain_quant |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
478 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
479 * PURPOSE: pitch and codebook quantization for MR795 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
480 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
481 *************************************************************************/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
482 void |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
483 MR795_gain_quant( |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
484 GainAdaptState *adapt_st, /* i/o: gain adapter state structure */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
485 Word16 res[], /* i : LP residual, Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
486 Word16 exc[], /* i : LTP excitation (unfiltered), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
487 Word16 code[], /* i : CB innovation (unfiltered), Q13 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
488 Word16 frac_coeff[], /* i : coefficients (5), Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
489 Word16 exp_coeff[], /* i : energy coefficients (5), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
490 /* coefficients from calc_filt_ener() */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
491 Word16 exp_code_en, /* i : innovation energy (exponent), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
492 Word16 frac_code_en, /* i : innovation energy (fraction), Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
493 Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
494 Word16 frac_gcode0, /* i : predicted CB gain (fraction), Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
495 Word16 L_subfr, /* i : Subframe length */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
496 Word16 cod_gain_frac, /* i : opt. codebook gain (fraction),Q15 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
497 Word16 cod_gain_exp, /* i : opt. codebook gain (exponent), Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
498 Word16 gp_limit, /* i : pitch gain limit */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
499 Word16 *gain_pit, /* i/o: Pitch gain, Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
500 Word16 *gain_cod, /* o : Code gain, Q1 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
501 Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
502 /* (for MR122 MA predictor update) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
503 Word16 *qua_ener, /* o : quantized energy error, Q10 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
504 /* (for other MA predictor update) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
505 Word16 **anap /* o : Index of quantization */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
506 /* (first gain pitch, then code pitch)*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
507 ) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
508 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
509 Word16 frac_en[4]; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
510 Word16 exp_en[4]; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
511 Word16 ltpg, alpha, gcode0; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
512 Word16 g_pitch_cand[3]; /* pitch gain candidates Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
513 Word16 g_pitch_cind[3]; /* pitch gain indices Q0 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
514 Word16 gain_pit_index; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
515 Word16 gain_cod_index; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
516 Word16 exp; |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
517 Word16 gain_cod_unq; /* code gain (unq.) Q(10-exp_gcode0) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
518 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
519 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
520 /* get list of candidate quantized pitch gain values |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
521 * and corresponding quantization indices |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
522 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
523 gain_pit_index = q_gain_pitch (MR795, gp_limit, gain_pit, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
524 g_pitch_cand, g_pitch_cind); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
525 move16 (); /* function result */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
526 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
527 /*-------------------------------------------------------------------* |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
528 * predicted codebook gain * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
529 * ~~~~~~~~~~~~~~~~~~~~~~~ * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
530 * gc0 = 2^exp_gcode0 + 2^frac_gcode0 * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
531 * * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
532 * gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0) * |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
533 *-------------------------------------------------------------------*/ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
534 gcode0 = extract_l(Pow2(14, frac_gcode0)); /* Q14 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
535 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
536 /* pre-quantization of codebook gain |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
537 * (using three pitch gain candidates); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
538 * result: best guess of pitch gain and code gain |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
539 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
540 MR795_gain_code_quant3( |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
541 exp_gcode0, gcode0, g_pitch_cand, g_pitch_cind, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
542 frac_coeff, exp_coeff, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
543 gain_pit, &gain_pit_index, gain_cod, &gain_cod_index, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
544 qua_ener_MR122, qua_ener); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
545 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
546 /* calculation of energy coefficients and LTP coding gain */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
547 calc_unfilt_energies(res, exc, code, *gain_pit, L_subfr, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
548 frac_en, exp_en, <pg); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
549 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
550 /* run gain adaptor, calculate alpha factor to balance LTP/CB gain |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
551 * (this includes the gain adaptor update) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
552 * Note: ltpg = 0 if frac_en[0] == 0, so the update is OK in that case |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
553 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
554 gain_adapt(adapt_st, ltpg, *gain_cod, &alpha); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
555 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
556 /* if this is a very low energy signal (threshold: see |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
557 * calc_unfilt_energies) or alpha <= 0 then don't run the modified quantizer |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
558 */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
559 test (); move16 (); test (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
560 if (frac_en[0] != 0 && alpha > 0) |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
561 { |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
562 /* innovation energy <cod cod> was already computed in gc_pred() */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
563 /* (this overwrites the LtpResEn which is no longer needed) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
564 frac_en[3] = frac_code_en; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
565 exp_en[3] = exp_code_en; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
566 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
567 /* store optimum codebook gain in Q(10-exp_gcode0) */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
568 exp = add (sub (cod_gain_exp, exp_gcode0), 10); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
569 gain_cod_unq = shl (cod_gain_frac, exp); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
570 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
571 /* run quantization with modified criterion */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
572 gain_cod_index = MR795_gain_code_quant_mod( |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
573 *gain_pit, exp_gcode0, gcode0, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
574 frac_en, exp_en, alpha, gain_cod_unq, |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
575 gain_cod, qua_ener_MR122, qua_ener); move16 (); /* function result */ |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
576 } |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
577 |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
578 *(*anap)++ = gain_pit_index; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
579 *(*anap)++ = gain_cod_index; move16 (); |
2aa98051d445
libtwamr: integrate qgain795.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
580 } |