FreeCalypso > hg > gsm-codec-lib
comparison libtwamr/gain_q.c @ 378:ccba5812fa44
libtwamr: integrate gain_q.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 06 May 2024 04:38:55 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
377:b02e043dcba0 | 378:ccba5812fa44 |
---|---|
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 : gain_q.c | |
11 * Purpose : Quantazation of gains | |
12 * | |
13 ******************************************************************************** | |
14 */ | |
15 | |
16 /* | |
17 ******************************************************************************** | |
18 * MODULE INCLUDE FILE AND VERSION ID | |
19 ******************************************************************************** | |
20 */ | |
21 #include "namespace.h" | |
22 #include "gain_q.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 "no_count.h" | |
33 #include "qua_gain.h" | |
34 #include "cnst.h" | |
35 #include "g_code.h" | |
36 #include "q_gain_c.h" | |
37 #include "gc_pred.h" | |
38 #include "calc_en.h" | |
39 #include "qgain795.h" | |
40 #include "qgain475.h" | |
41 #include "memops.h" | |
42 | |
43 /* | |
44 ******************************************************************************** | |
45 * PUBLIC PROGRAM CODE | |
46 ******************************************************************************** | |
47 */ | |
48 | |
49 /************************************************************************* | |
50 * | |
51 * Function: gainQuant_reset | |
52 * Purpose: Initializes state memory to zero | |
53 * | |
54 ************************************************************************** | |
55 */ | |
56 void gainQuant_reset (gainQuantState *state) | |
57 { | |
58 state->sf0_exp_gcode0 = 0; | |
59 state->sf0_frac_gcode0 = 0; | |
60 state->sf0_exp_target_en = 0; | |
61 state->sf0_frac_target_en = 0; | |
62 | |
63 Set_zero (state->sf0_exp_coeff, 5); | |
64 Set_zero (state->sf0_frac_coeff, 5); | |
65 | |
66 gc_pred_reset(&state->gc_predSt); | |
67 gc_pred_reset(&state->gc_predUnqSt); | |
68 gain_adapt_reset(&state->adaptSt); | |
69 } | |
70 | |
71 int gainQuant( | |
72 gainQuantState *st, /* i/o : State struct */ | |
73 enum Mode mode, /* i : coder mode */ | |
74 Word16 res[], /* i : LP residual, Q0 */ | |
75 Word16 exc[], /* i : LTP excitation (unfiltered), Q0 */ | |
76 Word16 code[], /* i : CB innovation (unfiltered), Q13 */ | |
77 /* (unsharpened for MR475) */ | |
78 Word16 xn[], /* i : Target vector. */ | |
79 Word16 xn2[], /* i : Target vector. */ | |
80 Word16 y1[], /* i : Adaptive codebook. */ | |
81 Word16 Y2[], /* i : Filtered innovative vector. */ | |
82 Word16 g_coeff[], /* i : Correlations <xn y1> <y1 y1> */ | |
83 /* Compute in G_pitch(). */ | |
84 Word16 even_subframe, /* i : even subframe indicator flag */ | |
85 Word16 gp_limit, /* i : pitch gain limit */ | |
86 Word16 *sf0_gain_pit, /* o : Pitch gain sf 0. MR475 */ | |
87 Word16 *sf0_gain_cod, /* o : Code gain sf 0. MR475 */ | |
88 Word16 *gain_pit, /* i/o : Pitch gain. */ | |
89 Word16 *gain_cod, /* o : Code gain. */ | |
90 /* MR475: gain_* unquantized in even */ | |
91 /* subframes, quantized otherwise */ | |
92 Word16 **anap /* o : Index of quantization */ | |
93 ) | |
94 { | |
95 Word16 exp_gcode0; | |
96 Word16 frac_gcode0; | |
97 Word16 qua_ener_MR122; | |
98 Word16 qua_ener; | |
99 Word16 frac_coeff[5]; | |
100 Word16 exp_coeff[5]; | |
101 Word16 exp_en, frac_en; | |
102 Word16 cod_gain_exp, cod_gain_frac; | |
103 | |
104 test (); | |
105 if (sub (mode, MR475) == 0) | |
106 { | |
107 test (); | |
108 if (even_subframe != 0) | |
109 { | |
110 /* save position in output parameter stream and current | |
111 state of codebook gain predictor */ | |
112 st->gain_idx_ptr = (*anap)++; | |
113 gc_pred_copy(&st->gc_predSt, &st->gc_predUnqSt); | |
114 | |
115 /* predict codebook gain (using "unquantized" predictor)*/ | |
116 /* (note that code[] is unsharpened in MR475) */ | |
117 gc_pred(&st->gc_predUnqSt, mode, code, | |
118 &st->sf0_exp_gcode0, &st->sf0_frac_gcode0, | |
119 &exp_en, &frac_en); | |
120 | |
121 /* calculate energy coefficients for quantization | |
122 and store them in state structure (will be used | |
123 in next subframe when real quantizer is run) */ | |
124 calc_filt_energies(mode, xn, xn2, y1, Y2, g_coeff, | |
125 st->sf0_frac_coeff, st->sf0_exp_coeff, | |
126 &cod_gain_frac, &cod_gain_exp); | |
127 | |
128 /* store optimum codebook gain (Q1) */ | |
129 *gain_cod = shl (cod_gain_frac, add (cod_gain_exp, 1)); | |
130 move16 (); | |
131 | |
132 calc_target_energy(xn, | |
133 &st->sf0_exp_target_en, &st->sf0_frac_target_en); | |
134 | |
135 /* calculate optimum codebook gain and update | |
136 "unquantized" predictor */ | |
137 MR475_update_unq_pred(&st->gc_predUnqSt, | |
138 st->sf0_exp_gcode0, st->sf0_frac_gcode0, | |
139 cod_gain_exp, cod_gain_frac); | |
140 | |
141 /* the real quantizer is not run here... */ | |
142 } | |
143 else | |
144 { | |
145 /* predict codebook gain (using "unquantized" predictor) */ | |
146 /* (note that code[] is unsharpened in MR475) */ | |
147 gc_pred(&st->gc_predUnqSt, mode, code, | |
148 &exp_gcode0, &frac_gcode0, | |
149 &exp_en, &frac_en); | |
150 | |
151 /* calculate energy coefficients for quantization */ | |
152 calc_filt_energies(mode, xn, xn2, y1, Y2, g_coeff, | |
153 frac_coeff, exp_coeff, | |
154 &cod_gain_frac, &cod_gain_exp); | |
155 | |
156 calc_target_energy(xn, &exp_en, &frac_en); | |
157 | |
158 /* run real (4-dim) quantizer and update real gain predictor */ | |
159 *st->gain_idx_ptr = MR475_gain_quant( | |
160 &st->gc_predSt, | |
161 st->sf0_exp_gcode0, st->sf0_frac_gcode0, | |
162 st->sf0_exp_coeff, st->sf0_frac_coeff, | |
163 st->sf0_exp_target_en, st->sf0_frac_target_en, | |
164 code, | |
165 exp_gcode0, frac_gcode0, | |
166 exp_coeff, frac_coeff, | |
167 exp_en, frac_en, | |
168 gp_limit, | |
169 sf0_gain_pit, sf0_gain_cod, | |
170 gain_pit, gain_cod); | |
171 } | |
172 } | |
173 else | |
174 { | |
175 /*-------------------------------------------------------------------* | |
176 * predict codebook gain and quantize * | |
177 * (also compute normalized CB innovation energy for MR795) * | |
178 *-------------------------------------------------------------------*/ | |
179 gc_pred(&st->gc_predSt, mode, code, &exp_gcode0, &frac_gcode0, | |
180 &exp_en, &frac_en); | |
181 | |
182 test (); | |
183 if (sub(mode, MR122) == 0) | |
184 { | |
185 *gain_cod = G_code (xn2, Y2); move16 (); | |
186 *(*anap)++ = q_gain_code (mode, exp_gcode0, frac_gcode0, | |
187 gain_cod, &qua_ener_MR122, &qua_ener); | |
188 move16 (); | |
189 } | |
190 else | |
191 { | |
192 /* calculate energy coefficients for quantization */ | |
193 calc_filt_energies(mode, xn, xn2, y1, Y2, g_coeff, | |
194 frac_coeff, exp_coeff, | |
195 &cod_gain_frac, &cod_gain_exp); | |
196 | |
197 test (); | |
198 if (sub (mode, MR795) == 0) | |
199 { | |
200 MR795_gain_quant(&st->adaptSt, res, exc, code, | |
201 frac_coeff, exp_coeff, | |
202 exp_en, frac_en, | |
203 exp_gcode0, frac_gcode0, L_SUBFR, | |
204 cod_gain_frac, cod_gain_exp, | |
205 gp_limit, gain_pit, gain_cod, | |
206 &qua_ener_MR122, &qua_ener, | |
207 anap); | |
208 } | |
209 else | |
210 { | |
211 *(*anap)++ = Qua_gain(mode, | |
212 exp_gcode0, frac_gcode0, | |
213 frac_coeff, exp_coeff, gp_limit, | |
214 gain_pit, gain_cod, | |
215 &qua_ener_MR122, &qua_ener); | |
216 move16 (); | |
217 } | |
218 } | |
219 | |
220 /*------------------------------------------------------------------* | |
221 * update table of past quantized energies * | |
222 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * | |
223 * st->past_qua_en(Q10) = 20 * Log10(qua_gain_code) / constant * | |
224 * = Log2(qua_gain_code) * | |
225 * = qua_ener * | |
226 * constant = 20*Log10(2) * | |
227 *------------------------------------------------------------------*/ | |
228 gc_pred_update(&st->gc_predSt, qua_ener_MR122, qua_ener); | |
229 } | |
230 | |
231 return 0; | |
232 } |