blob: adde03e5a46cdd07be72fe9368aa4e5bacea2951 [file] [log] [blame]
Philipp Maier40def492017-12-16 03:42:15 +07001/*
2 * (C) 2017 by sysmocom - s.f.m.c. GmbH
3 * (C) 2017 by Philipp Maier <pmaier@sysmocom.de>
Philipp Maier40def492017-12-16 03:42:15 +07004 * All Rights Reserved
5 *
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +00006 * Significantly reworked in 2023 by Mother
7 * Mychaela N. Falconia <falcon@freecalypso.org> - however,
8 * Mother Mychaela's contributions are NOT subject to copyright.
9 * No rights reserved, all rights relinquished.
10 * Portions of this code are based on Themyscira libgsmfrp,
11 * a public domain library by the same author.
12 *
Philipp Maier40def492017-12-16 03:42:15 +070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +000023 *
24 * The present ECU implementation for GSM-FR is closely based on the
25 * TS 46.011 spec from 3GPP; more specifically, it is based on the
26 * Example solution presented in Chapter 6 of that spec, adapted for
27 * libosmocodec ECU architecture, and comes as close to fulfilling
28 * the spec's officially stated requirements (Chapter 5) as is
29 * possible within this Osmocom-imposed architecture. Please note
30 * the following areas where the present implementation fails to
31 * fulfill the original intent of GSM spec authors:
32 *
33 * - The "lost SID" criterion, defined in GSM 06.31, is based on the
34 * TAF bit from the Radio Subsystem. However, libosmocodec ECU API
35 * does not include this flag, thus spec requirements related to
36 * lost SID conditions cannot be implemented in a strictly compliant
37 * manner. The present implementation improvises its own "lost SID"
38 * detector (not strictly spec-compliant) by counting frame_out()
39 * calls in between good traffic frame inputs via frame_in().
40 *
41 * - In the architecture envisioned and assumed in the GSM specs,
42 * the ECU function of GSM 06.11 was never intended to be a fully
43 * modular component with its own bona fide I/O interfaces - this
44 * approach appears to be an Osmocom invention - instead this ECU
45 * function was intended to be subsumed in the Rx DTX handler
46 * component of GSM 06.31, also incorporating the comfort noise
47 * generator of GSM 06.12 - and unlike the narrower-scope ECU,
48 * this slightly-larger-scope Rx DTX handler is a modular component
49 * with well-defined I/O interfaces. In the case of BFI conditions
50 * following a SID, GSM 06.11 spec was written with the assumption
51 * that the ECU controls the comfort noise generator via internal
52 * signals, as opposed to emitting "corrected" SID frames on a
53 * modular interface going to a CN generator located somewhere else.
54 * Thus the "correct" behavior for a fully modularized ECU is unclear,
55 * and an argument can be made that the very existence of such a
56 * fully modularized ECU is incorrect in itself. The present
57 * implementation re-emits a "rejuvenated" form of the last saved
58 * SID frame during BFI conditions following a SID within the
59 * permitted window of 48 frames, then starts emitting muted SIDs
60 * with Xmaxc decreasing by 4 on each frame, and finally switches
61 * to emitting non-SID silence frames (Table 1 of TS 46.011)
62 * once Xmaxc reaches 0.
Philipp Maier40def492017-12-16 03:42:15 +070063 */
64
65#include <stdbool.h>
66#include <string.h>
67#include <stdint.h>
68#include <errno.h>
69
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +000070#include <osmocom/core/prbs.h>
71
Philipp Maier40def492017-12-16 03:42:15 +070072#include <osmocom/codec/codec.h>
73#include <osmocom/codec/ecu.h>
74
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +000075/* See TS 46.011, Chapter 6 Example solution */
76#define GSM611_XMAXC_REDUCE 4
77
78/* The first 5 bytes of RTP encoding neatly contain the magic nibble
79 * and LARc parameters, which also happens to be the part of SID frames
80 * that needs to be passed through as-is. */
81#define SID_PREFIX_LEN 5
82
83enum ecu_principal_state {
84 STATE_NO_DATA,
85 STATE_SPEECH,
86 STATE_SP_MUTING,
87 STATE_SID,
88 STATE_SID_MUTING,
89};
90
91struct fr_ecu_state {
92 enum ecu_principal_state pr_state;
93 uint8_t speech_frame[GSM_FR_BYTES];
94 uint8_t sid_prefix[SID_PREFIX_LEN];
95 uint8_t sid_xmaxc;
96 uint8_t sid_reemit_count;
97 struct osmo_prbs_state prng;
Mychaela N. Falconia315e78a2023-06-23 18:42:11 +000098 bool last_input_was_sid;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +000099};
100
101/* This function is the frame input to the ECU - all inputs to this
102 * function have been received by the Radio Subsystem as good traffic
103 * frames in the GSM 06.31 definition.
104 */
105static void fr_ecu_input(struct fr_ecu_state *fr, const uint8_t *frame)
106{
107 enum osmo_gsm631_sid_class sidc;
108
109 sidc = osmo_fr_sid_classify(frame);
110 switch (sidc) {
111 case OSMO_GSM631_SID_CLASS_SPEECH:
112 memcpy(fr->speech_frame, frame, GSM_FR_BYTES);
113 fr->pr_state = STATE_SPEECH;
Mychaela N. Falconia315e78a2023-06-23 18:42:11 +0000114 fr->last_input_was_sid = false;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000115 return;
116 case OSMO_GSM631_SID_CLASS_INVALID:
117 /* GSM 06.31 section 6.1.2 says: "an invalid SID frame
118 * shall be substituted by the last valid SID frame
119 * and the procedure for valid SID frames be applied."
120 * However, libosmocodec ECU architecture prevents us
121 * from doing what the spec says: the frame_in() method
122 * gets a const frame that can't be modified, and
123 * frame_out() will never get called when BFI=0, even
124 * when the "good traffic frame" (in the BFI=0 sense)
125 * is an invalid SID by the bit-counting rule.
126 * Thus there is no place where we can re-emit a cached
127 * copy of the last valid SID upon receiving an invalid SID.
128 *
129 * In the standard GSM architecture this problem never
130 * arises because the ECU is not a separate component
131 * but is coupled with the CN generator, thus the output
132 * from the Rx DTX handler block will be a CN frame,
133 * for both valid-SID and invalid-SID inputs to the block.
134 * But what can we do within the constraints of libosmocodec
135 * ECU framework? We treat the invalid SID almost like a
136 * BFI, doing almost nothing in the frame_in() method,
137 * but we reset sid_reemit_count because by the rules of
138 * GSM 06.31 an invalid SID is still an accepted SID frame
139 * for the purpose of "lost SID" logic. */
140 fr->sid_reemit_count = 0;
Mychaela N. Falconia315e78a2023-06-23 18:42:11 +0000141 fr->last_input_was_sid = true;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000142 return;
143 case OSMO_GSM631_SID_CLASS_VALID:
144 /* save LARc part */
145 memcpy(fr->sid_prefix, frame, SID_PREFIX_LEN);
146 /* save Xmaxc from the last subframe */
147 fr->sid_xmaxc = ((frame[27] & 0x1F) << 1) | (frame[28] >> 7);
148 fr->pr_state = STATE_SID;
149 fr->sid_reemit_count = 0;
Mychaela N. Falconia315e78a2023-06-23 18:42:11 +0000150 fr->last_input_was_sid = true;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000151 return;
152 default:
153 /* There are only 3 possible SID classifications per GSM 06.31
154 * section 6.1.1, thus any other return value is a grave error
155 * in the code. */
156 OSMO_ASSERT(0);
157 }
158}
159
160/* Reduce all 4 Xmaxc fields in the frame. When all 4 Xmaxc fields
161 * reach 0, the function will return true for "mute".
162 */
163static bool reduce_xmaxc(uint8_t *frame)
164{
165 bool mute_flag = true;
166 uint8_t sub, xmaxc;
167
168 for (sub = 0; sub < 4; sub++) {
169 xmaxc = ((frame[sub*7+6] & 0x1F) << 1) | (frame[sub*7+7] >> 7);
170 if (xmaxc > GSM611_XMAXC_REDUCE) {
171 xmaxc -= GSM611_XMAXC_REDUCE;
172 mute_flag = false;
173 } else
174 xmaxc = 0;
175 frame[sub*7+6] &= 0xE0;
176 frame[sub*7+6] |= xmaxc >> 1;
177 frame[sub*7+7] &= 0x7F;
178 frame[sub*7+7] |= (xmaxc & 1) << 7;
179 }
180 return mute_flag;
181}
182
183/* TS 46.011 chapter 6, paragraph 4, last sentence: "The grid position
184 * parameters are chosen randomly between 0 and 3 during this time."
185 * (The "during this time" qualifier refers to the speech muting state.)
186 * This sentence in the spec must have been overlooked by previous ECU
187 * implementors, as this aspect of the muting logic was missing.
188 */
189static void random_grid_pos(struct fr_ecu_state *fr, uint8_t *frame)
190{
191 uint8_t sub;
192
193 for (sub = 0; sub < 4; sub++) {
194 frame[sub*7+6] &= 0x9F;
195 frame[sub*7+6] |= osmo_prbs_get_ubit(&fr->prng) << 6;
196 frame[sub*7+6] |= osmo_prbs_get_ubit(&fr->prng) << 5;
197 }
198}
199
200/* Like reduce_xmaxc() above, but for comfort noise rather than speech. */
201static bool reduce_xmaxc_sid(struct fr_ecu_state *fr)
202{
203 if (fr->sid_xmaxc > GSM611_XMAXC_REDUCE) {
204 fr->sid_xmaxc -= GSM611_XMAXC_REDUCE;
205 return false;
206 }
207 fr->sid_xmaxc = 0;
208 return true;
209}
210
211/* This function implements the part which is peculiar to the present
212 * "standalone" packaging of GSM-FR ECU, without a directly coupled
213 * comfort noise generator - it re-emits synthetic SID frames during
214 * DTX pauses, initially unchanged from the saved SID and later muted.
215 */
216static void reemit_sid(struct fr_ecu_state *fr, uint8_t *frame)
217{
218 uint8_t *p, sub;
219
220 memcpy(frame, fr->sid_prefix, SID_PREFIX_LEN);
221 p = frame + SID_PREFIX_LEN;
222 for (sub = 0; sub < 4; sub++) {
223 *p++ = 0;
224 *p++ = fr->sid_xmaxc >> 1;
225 *p++ = (fr->sid_xmaxc & 1) << 7;
226 *p++ = 0;
227 *p++ = 0;
228 *p++ = 0;
229 *p++ = 0;
230 }
231}
232
233/* This function is responsible for generating the ECU's output
234 * in the event that the Radio Subsystem does not have a good
235 * traffic frame - conditions corresponding to BFI=1 in the specs.
236 */
237static void fr_ecu_output(struct fr_ecu_state *fr, uint8_t *frame)
238{
239 bool mute;
240
241 switch (fr->pr_state) {
242 case STATE_NO_DATA:
243 memcpy(frame, osmo_gsm611_silence_frame, GSM_FR_BYTES);
244 return;
245 case STATE_SPEECH:
246 /* TS 46.011 chapter 6: "The first lost speech frame is
247 * replaced at the speech decoder input by the previous
248 * good speech frame." */
249 memcpy(frame, fr->speech_frame, GSM_FR_BYTES);
250 fr->pr_state = STATE_SP_MUTING;
251 return;
252 case STATE_SP_MUTING:
253 mute = reduce_xmaxc(fr->speech_frame);
254 memcpy(frame, fr->speech_frame, GSM_FR_BYTES);
255 random_grid_pos(fr, frame);
256 if (mute)
257 fr->pr_state = STATE_NO_DATA;
258 return;
259 case STATE_SID:
260 fr->sid_reemit_count++;
261 if (fr->sid_reemit_count >= 48) {
262 fr->pr_state = STATE_SID_MUTING;
263 reduce_xmaxc_sid(fr);
264 }
265 reemit_sid(fr, frame);
266 return;
267 case STATE_SID_MUTING:
268 if (reduce_xmaxc_sid(fr)) {
269 fr->pr_state = STATE_NO_DATA;
270 memcpy(frame, osmo_gsm611_silence_frame, GSM_FR_BYTES);
271 } else
272 reemit_sid(fr, frame);
273 return;
274 default:
275 /* a severe bug in the state machine! */
276 OSMO_ASSERT(0);
277 }
278}
279
Harald Welte750d8312019-08-01 20:05:05 +0200280/***********************************************************************
281 * Integration with ECU core
282 ***********************************************************************/
283
284static struct osmo_ecu_state *ecu_fr_init(void *ctx, enum osmo_ecu_codec codec)
285{
286 struct osmo_ecu_state *st;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000287 struct fr_ecu_state *fr;
288 size_t size = sizeof(*st) + sizeof(*fr);
Harald Welte750d8312019-08-01 20:05:05 +0200289
290 st = talloc_named_const(ctx, size, "ecu_state_FR");
291 if (!st)
292 return NULL;
293
294 memset(st, 0, size);
295 st->codec = codec;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000296 fr = (struct fr_ecu_state *) &st->data;
297 fr->pr_state = STATE_NO_DATA;
298 osmo_prbs_state_init(&fr->prng, &osmo_prbs15);
Harald Welte750d8312019-08-01 20:05:05 +0200299
300 return st;
301}
302
303static int ecu_fr_frame_in(struct osmo_ecu_state *st, bool bfi, const uint8_t *frame,
304 unsigned int frame_bytes)
305{
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000306 struct fr_ecu_state *fr = (struct fr_ecu_state *) &st->data;
307
Harald Welte750d8312019-08-01 20:05:05 +0200308 if (bfi)
309 return 0;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000310 if (frame_bytes != GSM_FR_BYTES)
311 return 0;
312 if ((frame[0] & 0xF0) != 0xD0)
313 return 0;
Harald Welte750d8312019-08-01 20:05:05 +0200314
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000315 fr_ecu_input(fr, frame);
Harald Welte750d8312019-08-01 20:05:05 +0200316 return 0;
317}
318
319static int ecu_fr_frame_out(struct osmo_ecu_state *st, uint8_t *frame_out)
320{
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000321 struct fr_ecu_state *fr = (struct fr_ecu_state *) &st->data;
Harald Welte750d8312019-08-01 20:05:05 +0200322
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000323 fr_ecu_output(fr, frame_out);
324 return GSM_FR_BYTES;
Harald Welte750d8312019-08-01 20:05:05 +0200325}
326
Mychaela N. Falconia315e78a2023-06-23 18:42:11 +0000327static bool ecu_fr_is_dtx_pause(struct osmo_ecu_state *st)
328{
329 struct fr_ecu_state *fr = (struct fr_ecu_state *) &st->data;
330
331 return fr->last_input_was_sid;
332}
333
Harald Welte750d8312019-08-01 20:05:05 +0200334static const struct osmo_ecu_ops osmo_ecu_ops_fr = {
335 .init = ecu_fr_init,
336 .frame_in = ecu_fr_frame_in,
337 .frame_out = ecu_fr_frame_out,
Mychaela N. Falconia315e78a2023-06-23 18:42:11 +0000338 .is_dtx_pause = ecu_fr_is_dtx_pause,
Harald Welte750d8312019-08-01 20:05:05 +0200339};
340
341static __attribute__((constructor)) void on_dso_load_ecu_fr(void)
342{
343 osmo_ecu_register(&osmo_ecu_ops_fr, OSMO_ECU_CODEC_FR);
344}