comparison libgsmhr1/rxfe.c @ 579:1dc5d9320e96

libgsmhr1: implement RxFE block
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Feb 2025 09:10:12 +0000
parents
children d4979cdbc081
comparison
equal deleted inserted replaced
578:7756b23b78cd 579:1dc5d9320e96
1 /*
2 * Rx front end implementation, common between the full decoder and TFO.
3 */
4
5 #include <stdint.h>
6 #include <string.h>
7 #include "tw_gsmhr.h"
8 #include "typedefs.h"
9 #include "namespace.h"
10 #include "rxfe.h"
11 #include "dtx_const.h"
12 #include "dtx_rxfe.h"
13
14 /*
15 * If the very first frame(s) we have to process out of reset (or after DHF)
16 * are either BFI or invalid SID, what is our default fallback frame?
17 * In the original GSM 06.06 code it is a frame of all zero params - but
18 * because that code provides only a full decoder and not TFO, this oddity
19 * is not readily visible. (The PCM output from the full decoder is all
20 * zeros.) OTOH, if we feed all zeros as PCM input to a homed standard
21 * encoder, we get this frame, repeating endlessly as long as all-zeros
22 * PCM input continues:
23 *
24 * R0=00 LPC=164,171,cb Int=0 Mode=0
25 * s1=00,00,00 s2=00,00,00 s3=00,00,00 s4=00,00,00
26 *
27 * This frame differs from all-zero params only in the LPC set, and this
28 * sane-LPC silence frame is the one we shall use as our reset-default
29 * fallback frame.
30 */
31 static const Shortword default_lpc[3] = {0x164, 0x171, 0xcb};
32
33 /*
34 * UFI R0 handling tables: when we get a speech frame marked with BFI=0 UFI=1,
35 * we look at the R0 parameter, both in the received frame and in the saved
36 * one. Both of the following LUTs are indexed by old R0 just like in the
37 * original GSM 06.06 code, but the values in them are absolute thresholds
38 * for the new R0, instead of diff values used in the original code.
39 */
40 static const Shortword ufi_r0_thresh_bfi[32] = {
41 15, 16, 17, 15, 16, 17, 18, 18,
42 18, 19, 19, 20, 21, 22, 22, 23,
43 23, 23, 23, 24, 25, 25, 26, 26,
44 26, 27, 28, 29, 30, 31, 32, 32
45 };
46
47 static const Shortword ufi_r0_thresh_mute[32] = {
48 14, 13, 13, 12, 13, 14, 15, 14,
49 15, 16, 17, 18, 18, 19, 20, 20,
50 21, 21, 21, 22, 23, 24, 25, 25,
51 25, 26, 27, 28, 29, 30, 30, 30
52 };
53
54 /* state transition tables for ECU state machine */
55 static const uint8_t ecu_state_trans_good[8] = {0, 0, 0, 0, 0, 0, 7, 0};
56 static const uint8_t ecu_state_trans_bad[8] = {1, 2, 3, 4, 5, 6, 6, 6};
57
58 void gsmhr_rxfe_reset(struct gsmhr_rxfe_state *st)
59 {
60 memset(st, 0, sizeof(struct gsmhr_rxfe_state));
61 memcpy(st->saved_frame + 1, default_lpc, sizeof(Shortword) * 3);
62 st->ecu_state = 7;
63 }
64
65 static Shortword input_frame_class(Shortword bfi, Shortword ufi, Shortword sid)
66 {
67 Shortword bfi_dtx;
68
69 bfi_dtx = bfi || ufi;
70 if (sid == 2 && bfi_dtx == 0)
71 return VALIDSID;
72 if (sid == 0 && bfi_dtx == 0)
73 return GOODSPEECH;
74 if (sid == 0 && bfi_dtx != 0)
75 return UNUSABLE;
76 return INVALIDSID;
77 }
78
79 static Shortword compute_last_lag(const Shortword *prm_in)
80 {
81 Shortword accum, delta, i;
82
83 accum = prm_in[6];
84 for (i = 0; i < 3; i++) {
85 delta = prm_in[9 + i * 3] - 8;
86 accum += delta;
87 if (accum > 0xFF)
88 accum = 0xFF;
89 else if (accum < 0)
90 accum = 0;
91 }
92 return accum;
93 }
94
95 static void save_speech_frame(struct gsmhr_rxfe_state *st,
96 const Shortword *prm_in)
97 {
98 memcpy(st->saved_frame, prm_in, sizeof(Shortword) * 6);
99 if (prm_in[5]) {
100 /* voiced modes */
101 st->saved_frame[6] = compute_last_lag(prm_in);
102 st->saved_frame[7] = prm_in[7];
103 st->saved_frame[9] = 8;
104 st->saved_frame[10] = prm_in[10];
105 st->saved_frame[12] = 8;
106 st->saved_frame[13] = prm_in[13];
107 st->saved_frame[15] = 8;
108 st->saved_frame[16] = prm_in[16];
109 } else {
110 /* unvoiced mode */
111 st->saved_frame[6] = prm_in[6];
112 st->saved_frame[7] = prm_in[7];
113 st->saved_frame[9] = prm_in[9];
114 st->saved_frame[10] = prm_in[10];
115 st->saved_frame[12] = prm_in[12];
116 st->saved_frame[13] = prm_in[13];
117 st->saved_frame[15] = prm_in[15];
118 st->saved_frame[16] = prm_in[16];
119 }
120 /* all 4 gsp0 params come from the last subframe */
121 st->saved_frame[8] = prm_in[17];
122 st->saved_frame[11] = prm_in[17];
123 st->saved_frame[14] = prm_in[17];
124 st->saved_frame[17] = prm_in[17];
125 }
126
127 static void speech_ecu(struct gsmhr_rxfe_state *st, const Shortword *prm_in,
128 Shortword *prm_out, Shortword *mute_permit,
129 Shortword *dtxd_sp)
130 {
131 Shortword bfi = prm_in[18];
132 Shortword ufi = prm_in[19];
133 uint8_t prev_state = st->ecu_state;
134
135 if (ufi && !bfi) {
136 if (prm_in[0] >= ufi_r0_thresh_bfi[st->saved_frame[0]])
137 bfi = 1;
138 else if (prm_in[0] >= ufi_r0_thresh_mute[st->saved_frame[0]]) {
139 if (mute_permit)
140 *mute_permit = 1;
141 }
142 }
143 if (bfi)
144 st->ecu_state = ecu_state_trans_bad[prev_state];
145 else
146 st->ecu_state = ecu_state_trans_good[prev_state];
147 switch (st->ecu_state) {
148 case 0:
149 memcpy(prm_out, prm_in, sizeof(Shortword) * GSMHR_NUM_PARAMS);
150 save_speech_frame(st, prm_in);
151 return;
152 /* all remaining cases return the saved frame, differ only in R0 */
153 case 1:
154 case 2:
155 /* no muting yet */
156 break;
157 case 3:
158 case 4:
159 case 5:
160 st->saved_frame[0] -= 2;
161 if (st->saved_frame[0] < 0)
162 st->saved_frame[0] = 0;
163 break;
164 case 6:
165 case 7:
166 st->saved_frame[0] = 0;
167 break;
168 }
169 memcpy(prm_out, st->saved_frame, sizeof(Shortword) * GSMHR_NUM_PARAMS);
170 if (bfi < 2) {
171 if (st->saved_frame[5] == 0 && prm_in[5] == 0) {
172 /* both unvoiced */
173 prm_out[6] = prm_in[6];
174 prm_out[7] = prm_in[7];
175 prm_out[9] = prm_in[9];
176 prm_out[10] = prm_in[10];
177 prm_out[12] = prm_in[12];
178 prm_out[13] = prm_in[13];
179 prm_out[15] = prm_in[15];
180 prm_out[16] = prm_in[16];
181 } else if (st->saved_frame[5] != 0 && prm_in[5] != 0) {
182 /* both voiced */
183 prm_out[7] = prm_in[7];
184 prm_out[10] = prm_in[10];
185 prm_out[13] = prm_in[13];
186 prm_out[16] = prm_in[16];
187 }
188 }
189 if (dtxd_sp && prev_state >= 6)
190 *dtxd_sp = 0;
191 }
192
193 static void save_speech_gs(struct gsmhr_rxfe_state *st,
194 const Shortword *sp_param)
195 {
196 Shortword vmode = sp_param[5];
197 uint8_t ptr = st->gs_history_ptr;
198 int i;
199
200 for (i = 0; i < N_SUB; i++) {
201 st->gs_history[ptr] = ppLr_gsTable[vmode][sp_param[8 + i * 3]];
202 ptr++;
203 if (ptr >= GS_HISTORY_SIZE)
204 ptr = 0;
205 }
206 st->gs_history_ptr = ptr;
207 }
208
209 static void init_cn_gen(struct gsmhr_rxfe_state *st)
210 {
211 Longword L_RxDTXGs;
212
213 st->cn_prng = PN_INIT_SEED;
214 avgGsHistQntz(st->gs_history, &L_RxDTXGs);
215 st->gs_cn_out = gsQuant(L_RxDTXGs, 0);
216 st->dtx_bfi_count = 0;
217 st->dtx_muting = 0;
218 }
219
220 static void cn_output(struct gsmhr_rxfe_state *st, Shortword *prm_out)
221 {
222 int i;
223
224 memcpy(prm_out, st->saved_frame, sizeof(Shortword) * 4);
225 prm_out[4] = 1; /* soft interpolation */
226 prm_out[5] = 0; /* unvoiced mode */
227 for (i = 0; i < N_SUB; i++) {
228 prm_out[6 + i * 3] = getPnBits(7, &st->cn_prng);
229 prm_out[7 + i * 3] = getPnBits(7, &st->cn_prng);
230 prm_out[8 + i * 3] = st->gs_cn_out;
231 }
232 }
233
234 void rxfe_main(struct gsmhr_rxfe_state *st, const Shortword *prm_in,
235 Shortword *prm_out, int fast_cn_muting,
236 Shortword *deco_mode_out, Shortword *mute_permit,
237 Shortword *dtxd_sp)
238 {
239 Shortword frame_class, deco_mode;
240
241 frame_class = input_frame_class(prm_in[18], prm_in[19], prm_in[20]);
242 if (!st->in_dtx) {
243 /* speech decoding mode */
244 if (frame_class == VALIDSID || frame_class == INVALIDSID)
245 deco_mode = CNIFIRSTSID;
246 else
247 deco_mode = SPEECH;
248 } else {
249 /* comfort noise insertion mode */
250 if (frame_class == VALIDSID || frame_class == INVALIDSID)
251 deco_mode = CNICONT;
252 else if (frame_class == UNUSABLE)
253 deco_mode = CNIBFI;
254 else
255 deco_mode = SPEECH;
256 }
257 /* Muting permission for the full decoder will be set only in one
258 * special case of speech UFI - default it to 0.
259 */
260 if (mute_permit)
261 *mute_permit = 0;
262 /* now real per-mode processing */
263 switch (deco_mode) {
264 case SPEECH:
265 /* Except for the special case of prolonged BFI that
266 * led to total muting, TFO with DTXd gets SP=1.
267 */
268 if (dtxd_sp)
269 *dtxd_sp = 1;
270 st->in_dtx = 0;
271 speech_ecu(st, prm_in, prm_out, mute_permit, dtxd_sp);
272 save_speech_gs(st, prm_out);
273 break;
274 case CNIFIRSTSID:
275 if (dtxd_sp)
276 *dtxd_sp = 0;
277 st->in_dtx = 1;
278 if (frame_class == VALIDSID)
279 memcpy(st->saved_frame, prm_in, sizeof(Shortword) * 4);
280 init_cn_gen(st);
281 cn_output(st, prm_out);
282 break;
283 case CNICONT:
284 if (dtxd_sp)
285 *dtxd_sp = 0;
286 st->in_dtx = 1;
287 if (frame_class == VALIDSID)
288 memcpy(st->saved_frame, prm_in, sizeof(Shortword) * 4);
289 else
290 deco_mode = CNIBFI; /* for interpolation */
291 st->dtx_bfi_count = 0;
292 st->dtx_muting = 0;
293 cn_output(st, prm_out);
294 break;
295 case CNIBFI:
296 if (dtxd_sp)
297 *dtxd_sp = 0;
298 st->in_dtx = 1;
299 if (st->dtx_muting) {
300 if (fast_cn_muting)
301 st->saved_frame[0] = 0;
302 else {
303 st->saved_frame[0] -= 2;
304 if (st->saved_frame[0] < 0)
305 st->saved_frame[0] = 0;
306 }
307 }
308 st->dtx_bfi_count++;
309 if (st->dtx_bfi_count >= (prm_in[21] ? 25 : 36))
310 st->dtx_muting = 1;
311 cn_output(st, prm_out);
312 break;
313 }
314 if (deco_mode_out)
315 *deco_mode_out = deco_mode;
316 }