FreeCalypso > hg > gsm-codec-lib
comparison libtwamr/c_g_aver.c @ 324:dc3b7caa59c4
libtwamr: integrate c_g_aver.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 18 Apr 2024 20:05:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
323:dfd5f159574b | 324:dc3b7caa59c4 |
---|---|
1 /************************************************************************* | |
2 * | |
3 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001 | |
4 * R99 Version 3.3.0 | |
5 * REL-4 Version 4.1.0 | |
6 * | |
7 ******************************************************************************** | |
8 * | |
9 * File : c_g_aver.c | |
10 * Purpose : | |
11 * | |
12 ******************************************************************************** | |
13 */ | |
14 | |
15 /* | |
16 ******************************************************************************** | |
17 * MODULE INCLUDE FILE AND VERSION ID | |
18 ******************************************************************************** | |
19 */ | |
20 #include "namespace.h" | |
21 #include "c_g_aver.h" | |
22 | |
23 #include "tw_amr.h" | |
24 #include "typedef.h" | |
25 #include "basic_op.h" | |
26 #include "no_count.h" | |
27 #include "cnst.h" | |
28 #include "memops.h" | |
29 | |
30 /* | |
31 ******************************************************************************** | |
32 * LOCAL VARIABLES AND TABLES | |
33 ******************************************************************************** | |
34 */ | |
35 /*-----------------------------------------------------------------* | |
36 * Decoder constant parameters (defined in "cnst.h") * | |
37 *-----------------------------------------------------------------* | |
38 * L_FRAME : Frame size. * | |
39 * L_SUBFR : Sub-frame size. * | |
40 *-----------------------------------------------------------------*/ | |
41 | |
42 /* | |
43 ******************************************************************************** | |
44 * PUBLIC PROGRAM CODE | |
45 ******************************************************************************** | |
46 */ | |
47 /* | |
48 ************************************************************************** | |
49 * | |
50 * Function : Cb_gain_average_reset | |
51 * Purpose : Resets state memory | |
52 * | |
53 ************************************************************************** | |
54 */ | |
55 void Cb_gain_average_reset (Cb_gain_averageState *state) | |
56 { | |
57 /* Static vectors to zero */ | |
58 Set_zero (state->cbGainHistory, L_CBGAINHIST); | |
59 | |
60 /* Initialize hangover handling */ | |
61 state->hangVar = 0; | |
62 state->hangCount= 0; | |
63 } | |
64 | |
65 /* | |
66 ************************************************************************** | |
67 * | |
68 * Function : Cb_gain_average | |
69 * Purpose : | |
70 * Returns : The mix cb gains for MR475, MR515, MR59, MR67, MR102; gain_code other modes | |
71 * | |
72 ************************************************************************** | |
73 */ | |
74 Word16 Cb_gain_average ( | |
75 Cb_gain_averageState *st, /* i/o : State variables for CB gain avergeing */ | |
76 enum Mode mode, /* i : AMR mode */ | |
77 Word16 gain_code, /* i : CB gain Q1 */ | |
78 Word16 lsp[], /* i : The LSP for the current frame Q15 */ | |
79 Word16 lspAver[], /* i : The average of LSP for 8 frames Q15 */ | |
80 Word16 bfi, /* i : bad frame indication flag */ | |
81 Word16 prev_bf, /* i : previous bad frame indication flag */ | |
82 Word16 pdfi, /* i : potential degraded bad frame ind flag */ | |
83 Word16 prev_pdf, /* i : prev pot. degraded bad frame ind flag */ | |
84 Word16 inBackgroundNoise, /* i : background noise decision */ | |
85 Word16 voicedHangover /* i : # of frames after last voiced frame */ | |
86 ) | |
87 { | |
88 /*---------------------------------------------------------* | |
89 * Compute mixed cb gain, used to make cb gain more * | |
90 * smooth in background noise for modes 5.15, 5.9 and 6.7 * | |
91 * states that needs to be updated by all * | |
92 *---------------------------------------------------------*/ | |
93 Word16 i; | |
94 Word16 cbGainMix, diff, tmp_diff, bgMix, cbGainMean; | |
95 Word32 L_sum; | |
96 Word16 tmp[M], tmp1, tmp2, shift1, shift2, shift; | |
97 | |
98 /* set correct cbGainMix for MR74, MR795, MR122 */ | |
99 cbGainMix = gain_code; move16 (); | |
100 | |
101 /*-------------------------------------------------------* | |
102 * Store list of CB gain needed in the CB gain * | |
103 * averaging * | |
104 *-------------------------------------------------------*/ | |
105 for (i = 0; i < (L_CBGAINHIST-1); i++) | |
106 { | |
107 st->cbGainHistory[i] = st->cbGainHistory[i+1]; move16 (); | |
108 } | |
109 st->cbGainHistory[L_CBGAINHIST-1] = gain_code; move16 (); | |
110 | |
111 /* compute lsp difference */ | |
112 for (i = 0; i < M; i++) { | |
113 tmp1 = abs_s(sub(lspAver[i], lsp[i])); /* Q15 */ | |
114 shift1 = sub(norm_s(tmp1), 1); /* Qn */ | |
115 tmp1 = shl(tmp1, shift1); /* Q15+Qn */ | |
116 shift2 = norm_s(lspAver[i]); /* Qm */ | |
117 tmp2 = shl(lspAver[i], shift2); /* Q15+Qm */ | |
118 tmp[i] = div_s(tmp1, tmp2); /* Q15+(Q15+Qn)-(Q15+Qm) */ | |
119 move16 (); | |
120 shift = sub(add(2, shift1), shift2); | |
121 test (); | |
122 if (shift >= 0) | |
123 { | |
124 tmp[i] = shr(tmp[i], shift); move16 (); /* Q15+Qn-Qm-Qx=Q13 */ | |
125 } | |
126 else | |
127 { | |
128 tmp[i] = shl(tmp[i], negate(shift)); move16 (); /* Q15+Qn-Qm-Qx=Q13 */ | |
129 } | |
130 } | |
131 | |
132 diff = tmp[0]; move16 (); | |
133 for (i = 1; i < M; i++) { | |
134 diff = add(diff, tmp[i]); /* Q13 */ | |
135 } | |
136 | |
137 /* Compute hangover */ | |
138 test (); | |
139 if (sub(diff, 5325) > 0) /* 0.65 in Q11 */ | |
140 { | |
141 st->hangVar = add(st->hangVar, 1); | |
142 } | |
143 else | |
144 { | |
145 st->hangVar = 0; move16 (); | |
146 } | |
147 | |
148 test (); | |
149 if (sub(st->hangVar, 10) > 0) | |
150 { | |
151 st->hangCount = 0; /* Speech period, reset hangover variable */ move16 (); | |
152 } | |
153 | |
154 /* Compute mix constant (bgMix) */ | |
155 bgMix = 8192; /* 1 in Q13 */ move16 (); | |
156 test (); | |
157 if ((sub(mode, MR67) <= 0) || (sub(mode, MR102) == 0)) | |
158 /* MR475, MR515, MR59, MR67, MR102 */ | |
159 { | |
160 /* if errors and presumed noise make smoothing probability stronger */ | |
161 test (); test (); test (); test (); test (); test(); test (); test (); test (); | |
162 if (((((pdfi != 0) && (prev_pdf != 0)) || (bfi != 0) || (prev_bf != 0)) && | |
163 (sub(voicedHangover, 1) > 0) && (inBackgroundNoise != 0) && | |
164 ((sub(mode, MR475) == 0) || | |
165 (sub(mode, MR515) == 0) || | |
166 (sub(mode, MR59) == 0)) )) | |
167 { | |
168 /* bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25; */ | |
169 tmp_diff = sub(diff, 4506); /* 0.55 in Q13 */ | |
170 | |
171 /* max(0.0, diff-0.55) */ | |
172 test (); | |
173 if (tmp_diff > 0) | |
174 { | |
175 tmp1 = tmp_diff; move16 (); | |
176 } | |
177 else | |
178 { | |
179 tmp1 = 0; move16 (); | |
180 } | |
181 | |
182 /* min(0.25, tmp1) */ | |
183 test (); | |
184 if (sub(2048, tmp1) < 0) | |
185 { | |
186 bgMix = 8192; move16 (); | |
187 } | |
188 else | |
189 { | |
190 bgMix = shl(tmp1, 2); | |
191 } | |
192 } | |
193 else | |
194 { | |
195 /* bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25; */ | |
196 tmp_diff = sub(diff, 3277); /* 0.4 in Q13 */ | |
197 | |
198 /* max(0.0, diff-0.40) */ | |
199 test (); | |
200 if (tmp_diff > 0) | |
201 { | |
202 tmp1 = tmp_diff; move16 (); | |
203 } | |
204 else | |
205 { | |
206 tmp1 = 0; move16 (); | |
207 } | |
208 | |
209 /* min(0.25, tmp1) */ | |
210 test (); | |
211 if (sub(2048, tmp1) < 0) | |
212 { | |
213 bgMix = 8192; move16 (); | |
214 } | |
215 else | |
216 { | |
217 bgMix = shl(tmp1, 2); | |
218 } | |
219 } | |
220 | |
221 test (); test (); | |
222 if ((sub(st->hangCount, 40) < 0) || (sub(diff, 5325) > 0)) /* 0.65 in Q13 */ | |
223 { | |
224 bgMix = 8192; /* disable mix if too short time since */ move16 (); | |
225 } | |
226 | |
227 /* Smoothen the cb gain trajectory */ | |
228 /* smoothing depends on mix constant bgMix */ | |
229 L_sum = L_mult(6554, st->cbGainHistory[2]); /* 0.2 in Q15; L_sum in Q17 */ | |
230 for (i = 3; i < L_CBGAINHIST; i++) | |
231 { | |
232 L_sum = L_mac(L_sum, 6554, st->cbGainHistory[i]); | |
233 } | |
234 cbGainMean = round(L_sum); /* Q1 */ | |
235 | |
236 /* more smoothing in error and bg noise (NB no DFI used here) */ | |
237 test (); test (); test (); test (); test(); test(); | |
238 if (((bfi != 0) || (prev_bf != 0)) && (inBackgroundNoise != 0) && | |
239 ((sub(mode, MR475) == 0) || | |
240 (sub(mode, MR515) == 0) || | |
241 (sub(mode, MR59) == 0)) ) | |
242 { | |
243 L_sum = L_mult(4681, st->cbGainHistory[0]); /* 0.143 in Q15; L_sum in Q17 */ | |
244 for (i = 1; i < L_CBGAINHIST; i++) | |
245 { | |
246 L_sum = L_mac(L_sum, 4681, st->cbGainHistory[i]); | |
247 } | |
248 cbGainMean = round(L_sum); /* Q1 */ | |
249 } | |
250 | |
251 /* cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean; */ | |
252 L_sum = L_mult(bgMix, cbGainMix); /* L_sum in Q15 */ | |
253 L_sum = L_mac(L_sum, 8192, cbGainMean); | |
254 L_sum = L_msu(L_sum, bgMix, cbGainMean); | |
255 cbGainMix = round(L_shl(L_sum, 2)); /* Q1 */ | |
256 } | |
257 | |
258 st->hangCount = add(st->hangCount, 1); | |
259 return cbGainMix; | |
260 } |