blob: 561ad6089f93bcf8893e223dab7dbc3825128c85 [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;
98};
99
100/* This function is the frame input to the ECU - all inputs to this
101 * function have been received by the Radio Subsystem as good traffic
102 * frames in the GSM 06.31 definition.
103 */
104static void fr_ecu_input(struct fr_ecu_state *fr, const uint8_t *frame)
105{
106 enum osmo_gsm631_sid_class sidc;
107
108 sidc = osmo_fr_sid_classify(frame);
109 switch (sidc) {
110 case OSMO_GSM631_SID_CLASS_SPEECH:
111 memcpy(fr->speech_frame, frame, GSM_FR_BYTES);
112 fr->pr_state = STATE_SPEECH;
113 return;
114 case OSMO_GSM631_SID_CLASS_INVALID:
115 /* GSM 06.31 section 6.1.2 says: "an invalid SID frame
116 * shall be substituted by the last valid SID frame
117 * and the procedure for valid SID frames be applied."
118 * However, libosmocodec ECU architecture prevents us
119 * from doing what the spec says: the frame_in() method
120 * gets a const frame that can't be modified, and
121 * frame_out() will never get called when BFI=0, even
122 * when the "good traffic frame" (in the BFI=0 sense)
123 * is an invalid SID by the bit-counting rule.
124 * Thus there is no place where we can re-emit a cached
125 * copy of the last valid SID upon receiving an invalid SID.
126 *
127 * In the standard GSM architecture this problem never
128 * arises because the ECU is not a separate component
129 * but is coupled with the CN generator, thus the output
130 * from the Rx DTX handler block will be a CN frame,
131 * for both valid-SID and invalid-SID inputs to the block.
132 * But what can we do within the constraints of libosmocodec
133 * ECU framework? We treat the invalid SID almost like a
134 * BFI, doing almost nothing in the frame_in() method,
135 * but we reset sid_reemit_count because by the rules of
136 * GSM 06.31 an invalid SID is still an accepted SID frame
137 * for the purpose of "lost SID" logic. */
138 fr->sid_reemit_count = 0;
139 return;
140 case OSMO_GSM631_SID_CLASS_VALID:
141 /* save LARc part */
142 memcpy(fr->sid_prefix, frame, SID_PREFIX_LEN);
143 /* save Xmaxc from the last subframe */
144 fr->sid_xmaxc = ((frame[27] & 0x1F) << 1) | (frame[28] >> 7);
145 fr->pr_state = STATE_SID;
146 fr->sid_reemit_count = 0;
147 return;
148 default:
149 /* There are only 3 possible SID classifications per GSM 06.31
150 * section 6.1.1, thus any other return value is a grave error
151 * in the code. */
152 OSMO_ASSERT(0);
153 }
154}
155
156/* Reduce all 4 Xmaxc fields in the frame. When all 4 Xmaxc fields
157 * reach 0, the function will return true for "mute".
158 */
159static bool reduce_xmaxc(uint8_t *frame)
160{
161 bool mute_flag = true;
162 uint8_t sub, xmaxc;
163
164 for (sub = 0; sub < 4; sub++) {
165 xmaxc = ((frame[sub*7+6] & 0x1F) << 1) | (frame[sub*7+7] >> 7);
166 if (xmaxc > GSM611_XMAXC_REDUCE) {
167 xmaxc -= GSM611_XMAXC_REDUCE;
168 mute_flag = false;
169 } else
170 xmaxc = 0;
171 frame[sub*7+6] &= 0xE0;
172 frame[sub*7+6] |= xmaxc >> 1;
173 frame[sub*7+7] &= 0x7F;
174 frame[sub*7+7] |= (xmaxc & 1) << 7;
175 }
176 return mute_flag;
177}
178
179/* TS 46.011 chapter 6, paragraph 4, last sentence: "The grid position
180 * parameters are chosen randomly between 0 and 3 during this time."
181 * (The "during this time" qualifier refers to the speech muting state.)
182 * This sentence in the spec must have been overlooked by previous ECU
183 * implementors, as this aspect of the muting logic was missing.
184 */
185static void random_grid_pos(struct fr_ecu_state *fr, uint8_t *frame)
186{
187 uint8_t sub;
188
189 for (sub = 0; sub < 4; sub++) {
190 frame[sub*7+6] &= 0x9F;
191 frame[sub*7+6] |= osmo_prbs_get_ubit(&fr->prng) << 6;
192 frame[sub*7+6] |= osmo_prbs_get_ubit(&fr->prng) << 5;
193 }
194}
195
196/* Like reduce_xmaxc() above, but for comfort noise rather than speech. */
197static bool reduce_xmaxc_sid(struct fr_ecu_state *fr)
198{
199 if (fr->sid_xmaxc > GSM611_XMAXC_REDUCE) {
200 fr->sid_xmaxc -= GSM611_XMAXC_REDUCE;
201 return false;
202 }
203 fr->sid_xmaxc = 0;
204 return true;
205}
206
207/* This function implements the part which is peculiar to the present
208 * "standalone" packaging of GSM-FR ECU, without a directly coupled
209 * comfort noise generator - it re-emits synthetic SID frames during
210 * DTX pauses, initially unchanged from the saved SID and later muted.
211 */
212static void reemit_sid(struct fr_ecu_state *fr, uint8_t *frame)
213{
214 uint8_t *p, sub;
215
216 memcpy(frame, fr->sid_prefix, SID_PREFIX_LEN);
217 p = frame + SID_PREFIX_LEN;
218 for (sub = 0; sub < 4; sub++) {
219 *p++ = 0;
220 *p++ = fr->sid_xmaxc >> 1;
221 *p++ = (fr->sid_xmaxc & 1) << 7;
222 *p++ = 0;
223 *p++ = 0;
224 *p++ = 0;
225 *p++ = 0;
226 }
227}
228
229/* This function is responsible for generating the ECU's output
230 * in the event that the Radio Subsystem does not have a good
231 * traffic frame - conditions corresponding to BFI=1 in the specs.
232 */
233static void fr_ecu_output(struct fr_ecu_state *fr, uint8_t *frame)
234{
235 bool mute;
236
237 switch (fr->pr_state) {
238 case STATE_NO_DATA:
239 memcpy(frame, osmo_gsm611_silence_frame, GSM_FR_BYTES);
240 return;
241 case STATE_SPEECH:
242 /* TS 46.011 chapter 6: "The first lost speech frame is
243 * replaced at the speech decoder input by the previous
244 * good speech frame." */
245 memcpy(frame, fr->speech_frame, GSM_FR_BYTES);
246 fr->pr_state = STATE_SP_MUTING;
247 return;
248 case STATE_SP_MUTING:
249 mute = reduce_xmaxc(fr->speech_frame);
250 memcpy(frame, fr->speech_frame, GSM_FR_BYTES);
251 random_grid_pos(fr, frame);
252 if (mute)
253 fr->pr_state = STATE_NO_DATA;
254 return;
255 case STATE_SID:
256 fr->sid_reemit_count++;
257 if (fr->sid_reemit_count >= 48) {
258 fr->pr_state = STATE_SID_MUTING;
259 reduce_xmaxc_sid(fr);
260 }
261 reemit_sid(fr, frame);
262 return;
263 case STATE_SID_MUTING:
264 if (reduce_xmaxc_sid(fr)) {
265 fr->pr_state = STATE_NO_DATA;
266 memcpy(frame, osmo_gsm611_silence_frame, GSM_FR_BYTES);
267 } else
268 reemit_sid(fr, frame);
269 return;
270 default:
271 /* a severe bug in the state machine! */
272 OSMO_ASSERT(0);
273 }
274}
275
Harald Welte750d8312019-08-01 20:05:05 +0200276/***********************************************************************
277 * Integration with ECU core
278 ***********************************************************************/
279
280static struct osmo_ecu_state *ecu_fr_init(void *ctx, enum osmo_ecu_codec codec)
281{
282 struct osmo_ecu_state *st;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000283 struct fr_ecu_state *fr;
284 size_t size = sizeof(*st) + sizeof(*fr);
Harald Welte750d8312019-08-01 20:05:05 +0200285
286 st = talloc_named_const(ctx, size, "ecu_state_FR");
287 if (!st)
288 return NULL;
289
290 memset(st, 0, size);
291 st->codec = codec;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000292 fr = (struct fr_ecu_state *) &st->data;
293 fr->pr_state = STATE_NO_DATA;
294 osmo_prbs_state_init(&fr->prng, &osmo_prbs15);
Harald Welte750d8312019-08-01 20:05:05 +0200295
296 return st;
297}
298
299static int ecu_fr_frame_in(struct osmo_ecu_state *st, bool bfi, const uint8_t *frame,
300 unsigned int frame_bytes)
301{
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000302 struct fr_ecu_state *fr = (struct fr_ecu_state *) &st->data;
303
Harald Welte750d8312019-08-01 20:05:05 +0200304 if (bfi)
305 return 0;
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000306 if (frame_bytes != GSM_FR_BYTES)
307 return 0;
308 if ((frame[0] & 0xF0) != 0xD0)
309 return 0;
Harald Welte750d8312019-08-01 20:05:05 +0200310
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000311 fr_ecu_input(fr, frame);
Harald Welte750d8312019-08-01 20:05:05 +0200312 return 0;
313}
314
315static int ecu_fr_frame_out(struct osmo_ecu_state *st, uint8_t *frame_out)
316{
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000317 struct fr_ecu_state *fr = (struct fr_ecu_state *) &st->data;
Harald Welte750d8312019-08-01 20:05:05 +0200318
Mychaela N. Falconia5eb356b2023-05-12 17:21:25 +0000319 fr_ecu_output(fr, frame_out);
320 return GSM_FR_BYTES;
Harald Welte750d8312019-08-01 20:05:05 +0200321}
322
323static const struct osmo_ecu_ops osmo_ecu_ops_fr = {
324 .init = ecu_fr_init,
325 .frame_in = ecu_fr_frame_in,
326 .frame_out = ecu_fr_frame_out,
327};
328
329static __attribute__((constructor)) void on_dso_load_ecu_fr(void)
330{
331 osmo_ecu_register(&osmo_ecu_ops_fr, OSMO_ECU_CODEC_FR);
332}