changeset 242:f081a6850fb5

libgsmfrp: new refined implementation The previous implementation exhibited the following defects, which are now fixed: 1) The last received valid SID was cached forever for the purpose of handling future invalid SIDs - we could have received some valid SID ages ago, then lots of speech or NO_DATA, and if we then get an invalid SID, we would resurrect the last valid SID from ancient history - a bad design. In our new design, we handle invalid SID based on the current state, much like BFI. 2) GSM 06.11 spec says clearly that after the second lost SID (received BFI=1 && TAF=1 in CN state) we need to gradually decrease the output level, rather than jump directly to emitting silence frames - we previously failed to implement such logic. 3) Per GSM 06.12 section 5.2, Xmaxc should be the same in all 4 subframes in a SID frame. What should we do if we receive an otherwise valid SID frame with different Xmaxc? Our previous approach would replicate this Xmaxc oddity in every subsequent generated CN frame, which is rather bad. In our new design, the very first CN frame (which can be seen as a transformation of the SID frame itself) retains the original 4 distinct Xmaxc, but all subsequent CN frames are based on the Xmaxc from the last subframe of the most recent SID.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 09 May 2023 05:16:31 +0000
parents a48ddaa0f9a7
children 5176f470489d
files libgsmfrp/bad_frame.c libgsmfrp/comfort_noise.c libgsmfrp/good_frame.c libgsmfrp/internal.h
diffstat 4 files changed, 119 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/libgsmfrp/bad_frame.c	Tue May 09 03:13:22 2023 +0000
+++ b/libgsmfrp/bad_frame.c	Tue May 09 05:16:31 2023 +0000
@@ -1,5 +1,6 @@
 /*
- * In this module we implement our handling of BFI frame gaps.
+ * In this module we implement our handling of BFI frame gaps
+ * and invalid SID frames.
  */
 
 #include <stdint.h>
@@ -38,6 +39,17 @@
 	}
 }
 
+static int reduce_xmaxc_sid(struct gsmfr_preproc_state *st)
+{
+	if (st->sid_xmaxc > 4) {
+		st->sid_xmaxc -= 4;
+		return 0;
+	} else {
+		st->sid_xmaxc = 0;
+		return 1;
+	}
+}
+
 void gsmfr_preproc_bfi(struct gsmfr_preproc_state *st, int taf, gsm_byte *frame)
 {
 	int mute;
@@ -58,14 +70,65 @@
 			st->rx_state = NO_DATA;
 		return;
 	case COMFORT_NOISE:
-		gsmfr_preproc_gen_cn(st, frame);
 		if (taf)
 			st->rx_state = LOST_SID;
+		gsmfr_preproc_gen_cn(st, frame);
 		return;
 	case LOST_SID:
+		if (taf) {
+			st->rx_state = CN_MUTING;
+			reduce_xmaxc_sid(st);
+		}
 		gsmfr_preproc_gen_cn(st, frame);
-		if (taf)
+		return;
+	case CN_MUTING:
+		if (reduce_xmaxc_sid(st)) {
 			st->rx_state = NO_DATA;
+			memcpy(frame, &gsmfr_preproc_silence_frame,
+				sizeof(gsm_frame));
+		} else
+			gsmfr_preproc_gen_cn(st, frame);
 		return;
 	}
 }
+
+void gsmfr_preproc_invalid_sid(struct gsmfr_preproc_state *st, gsm_byte *frame)
+{
+	switch (st->rx_state) {
+	case NO_DATA:
+		memcpy(frame, &gsmfr_preproc_silence_frame, sizeof(gsm_frame));
+		return;
+	case SPEECH:
+		/*
+		 * Make CN out of the last good speech frame, following the
+		 * "NOTE" at the end of section 6.1.2 in TS 46.031.
+		 */
+		st->rx_state = COMFORT_NOISE;
+		memcpy(st->sid_prefix, &st->speech_frame, 5);
+		st->sid_xmaxc = ((st->speech_frame[27] & 0x1F) << 1) |
+				(st->speech_frame[28] >> 7);
+		gsmfr_preproc_gen_cn(st, frame);
+		return;
+	case SPEECH_MUTING:
+		/* same as above, but set the state to CN_MUTING */
+		st->rx_state = CN_MUTING;
+		memcpy(st->sid_prefix, &st->speech_frame, 5);
+		st->sid_xmaxc = ((st->speech_frame[27] & 0x1F) << 1) |
+				(st->speech_frame[28] >> 7);
+		gsmfr_preproc_gen_cn(st, frame);
+		return;
+	case COMFORT_NOISE:
+	case LOST_SID:
+		st->rx_state = COMFORT_NOISE;
+		gsmfr_preproc_gen_cn(st, frame);
+		return;
+	case CN_MUTING:
+		if (reduce_xmaxc_sid(st)) {
+			st->rx_state = NO_DATA;
+			memcpy(frame, &gsmfr_preproc_silence_frame,
+				sizeof(gsm_frame));
+		} else
+			gsmfr_preproc_gen_cn(st, frame);
+		return;
+	}
+}
--- a/libgsmfrp/comfort_noise.c	Tue May 09 03:13:22 2023 +0000
+++ b/libgsmfrp/comfort_noise.c	Tue May 09 05:16:31 2023 +0000
@@ -48,8 +48,8 @@
 		       | ((bc[sub] >> 1) & 0x1);
 		*c++ =   ((bc[sub] & 0x1) << 7)
 		       | ((Mc & 0x3) << 5)
-		       | ((st->sid_xmaxc[sub] >> 1) & 0x1F);
-		*c++ =   ((st->sid_xmaxc[sub] & 0x1) << 7)
+		       | ((st->sid_xmaxc >> 1) & 0x1F);
+		*c++ =   ((st->sid_xmaxc & 0x1) << 7)
 		       | ((xmc[0] & 0x7) << 4)
 		       | ((xmc[1] & 0x7) << 1)
 		       | ((xmc[2] >> 2) & 0x1);
@@ -68,3 +68,44 @@
 		       | (xmc[12] & 0x7);
 	}
 }
+
+void gsmfr_preproc_sid2cn(struct gsmfr_preproc_state *st, gsm_byte *frame)
+{
+	unsigned sub, pulse;
+	uint8_t Mc, xmc[13];
+	gsm_byte *c;
+
+	/* save LARc and Xmaxc from the last subframe for subsequent CN gen */
+	memcpy(st->sid_prefix, frame, 5);
+	st->sid_xmaxc = ((frame[27] & 0x1F) << 1) | (frame[28] >> 7);
+	/* ... and turn *this* frame into very first CN output */
+	c = frame + 5;
+	for (sub = 0; sub < 4; sub++) {
+		Mc = gsmfr_preproc_prng(st, 2);
+		for (pulse = 0; pulse < 13; pulse++)
+			xmc[pulse] = random_1to6(st);
+		/* keep each of Xmaxc and replace the rest with CN */
+		*c++ =   ((Nc[sub] & 0x7F) << 1)
+		       | ((bc[sub] >> 1) & 0x1);
+		*c &= 0x1F;
+		*c++ |=  ((bc[sub] & 0x1) << 7)
+		       | ((Mc & 0x3) << 5);
+		*c &= 0x80;
+		*c++ |=  ((xmc[0] & 0x7) << 4)
+		       | ((xmc[1] & 0x7) << 1)
+		       | ((xmc[2] >> 2) & 0x1);
+		*c++ =   ((xmc[2] & 0x3) << 6)
+		       | ((xmc[3] & 0x7) << 3)
+		       | (xmc[4] & 0x7);
+		*c++ =   ((xmc[5] & 0x7) << 5)
+		       | ((xmc[6] & 0x7) << 2)
+		       | ((xmc[7] >> 1) & 0x3);
+		*c++ =   ((xmc[7] & 0x1) << 7)
+		       | ((xmc[8] & 0x7) << 4)
+		       | ((xmc[9] & 0x7) << 1)
+		       | ((xmc[10] >> 2) & 0x1);
+		*c++ =   ((xmc[10] & 0x3) << 6)
+		       | ((xmc[11] & 0x7) << 3)
+		       | (xmc[12] & 0x7);
+	}
+}
--- a/libgsmfrp/good_frame.c	Tue May 09 03:13:22 2023 +0000
+++ b/libgsmfrp/good_frame.c	Tue May 09 05:16:31 2023 +0000
@@ -17,28 +17,16 @@
 	sid = gsmfr_preproc_sid_classify(frame);
 	switch (sid) {
 	case 0:		/* good speech frame */
+		st->rx_state = SPEECH;
 		memcpy(&st->speech_frame, frame, sizeof(gsm_frame));
-		st->rx_state = SPEECH;
 		return;
 	case 1:		/* invalid SID frame */
-		if (st->got_valid_sid) {
-			st->rx_state = COMFORT_NOISE;
-			gsmfr_preproc_gen_cn(st, frame);
-		} else {
-			st->rx_state = NO_DATA;
-			memcpy(frame, &gsmfr_preproc_silence_frame,
-				sizeof(gsm_frame));
-		}
+		/* state-dependent handling, similar to BFI */
+		gsmfr_preproc_invalid_sid(st, frame);
 		return;
 	case 2:		/* valid SID frame */
-		st->got_valid_sid = 1;
-		memcpy(st->sid_prefix, frame, 5);
-		st->sid_xmaxc[0] = ((frame[6] & 0x1F) << 1) | (frame[7] >> 7);
-		st->sid_xmaxc[1] = ((frame[13] & 0x1F) << 1) | (frame[14] >> 7);
-		st->sid_xmaxc[2] = ((frame[20] & 0x1F) << 1) | (frame[21] >> 7);
-		st->sid_xmaxc[3] = ((frame[27] & 0x1F) << 1) | (frame[28] >> 7);
 		st->rx_state = COMFORT_NOISE;
-		gsmfr_preproc_gen_cn(st, frame);
+		gsmfr_preproc_sid2cn(st, frame);
 		return;
 	}
 }
--- a/libgsmfrp/internal.h	Tue May 09 03:13:22 2023 +0000
+++ b/libgsmfrp/internal.h	Tue May 09 05:16:31 2023 +0000
@@ -9,14 +9,14 @@
 	SPEECH_MUTING,
 	COMFORT_NOISE,
 	LOST_SID,
+	CN_MUTING,
 };
 
 struct gsmfr_preproc_state {
 	enum rx_state	rx_state;
-	int		got_valid_sid;
 	gsm_frame	speech_frame;
 	gsm_byte	sid_prefix[5];
-	uint8_t		sid_xmaxc[4];
+	uint8_t		sid_xmaxc;
 	uint32_t	cn_random_lfsr;
 	unsigned	cn_random_6fold;
 };
@@ -27,5 +27,9 @@
 /* internal functions */
 extern void gsmfr_preproc_gen_cn(struct gsmfr_preproc_state *state,
 				 gsm_byte *frame);
+extern void gsmfr_preproc_sid2cn(struct gsmfr_preproc_state *state,
+				 gsm_byte *frame);
+extern void gsmfr_preproc_invalid_sid(struct gsmfr_preproc_state *state,
+				      gsm_byte *frame);
 extern uint16_t gsmfr_preproc_prng(struct gsmfr_preproc_state *state,
 				   uint16_t no_bits);