changeset 140:5efc377326da

gsmefr-dlcap-gsmx: EFR counterpart to gsmfr-cvt-dlcap
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 13 Dec 2022 07:14:41 +0000
parents be57e06bed84
children c1dc094f0821
files .hgignore efrtest/Makefile efrtest/dlcap-gsmx.c efrtest/tidsp.c
diffstat 4 files changed, 151 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Dec 13 07:03:55 2022 +0000
+++ b/.hgignore	Tue Dec 13 07:14:41 2022 +0000
@@ -14,6 +14,7 @@
 ^efrtest/gsmefr-cod-parse$
 ^efrtest/gsmefr-dec-parse$
 ^efrtest/gsmefr-decode$
+^efrtest/gsmefr-dlcap-gsmx$
 ^efrtest/gsmefr-encode$
 ^efrtest/gsmefr-etsi-dec$
 ^efrtest/gsmefr-etsi-enc$
--- a/efrtest/Makefile	Tue Dec 13 07:03:55 2022 +0000
+++ b/efrtest/Makefile	Tue Dec 13 07:14:41 2022 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	gsmefr-cod-parse gsmefr-dec-parse gsmefr-decode gsmefr-encode \
-	gsmefr-etsi-dec gsmefr-etsi-enc gsmefr-rec2etsi
+PROGS=	gsmefr-cod-parse gsmefr-dec-parse gsmefr-decode gsmefr-dlcap-gsmx \
+	gsmefr-encode gsmefr-etsi-dec gsmefr-etsi-enc gsmefr-rec2etsi
 LIBEFR=	../libgsmefr/libgsmefr.a
 LIBTEST=../libtest/libtest.a
 INSTBIN=/opt/freecalypso/bin
@@ -17,6 +17,9 @@
 gsmefr-decode:	decode.o ${LIBTEST} ${LIBEFR}
 	${CC} ${CFLAGS} -o $@ decode.o ${LIBTEST} ${LIBEFR}
 
+gsmefr-dlcap-gsmx:	dlcap-gsmx.o tidsp.o ${LIBTEST}
+	${CC} ${CFLAGS} -o $@ dlcap-gsmx.o tidsp.o ${LIBTEST}
+
 gsmefr-encode:	encode.o ${LIBTEST} ${LIBEFR}
 	${CC} ${CFLAGS} -o $@ encode.o ${LIBTEST} ${LIBEFR}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/efrtest/dlcap-gsmx.c	Tue Dec 13 07:14:41 2022 +0000
@@ -0,0 +1,78 @@
+/*
+ * This program reads a TCH/EFS downlink capture produced with FreeCalypso tools
+ * (fw version with TCH downlink sniffing feature and fc-shell tch record)
+ * and converts it into our extended-libgsm binary format, to be further
+ * fed to gsmefr-decode.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+main(argc, argv)
+	char **argv;
+{
+	FILE *inf, *outf;
+	char linebuf[128];
+	int lineno, rc;
+	uint16_t status_words[3];
+	uint8_t tidsp_bytes[33], efr_bytes[31], bfi[2];
+	unsigned fn_mod_104;
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
+		exit(1);
+	}
+	inf = fopen(argv[1], "r");
+	if (!inf) {
+		perror(argv[1]);
+		exit(1);
+	}
+	outf = fopen(argv[2], "w");
+	if (!outf) {
+		perror(argv[2]);
+		exit(1);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
+		/* support both old and new formats */
+		if (isxdigit(linebuf[0]) && isxdigit(linebuf[1]) &&
+		    isxdigit(linebuf[2]) && isxdigit(linebuf[3])) {
+			rc = parse_dlcap_common(linebuf, status_words,
+						tidsp_bytes);
+			if (rc < 0) {
+invalid:			fprintf(stderr,
+				"error: %s is not in the expected format\n",
+					argv[1]);
+				exit(1);
+			}
+			fn_mod_104 = 0;		/* won't have TAF */
+		} else if (!strncmp(linebuf, "EFR ", 4)) {
+			rc = parse_dlcap_common(linebuf + 4, status_words,
+						tidsp_bytes);
+			if (rc < 0)
+				goto invalid;
+			if (linebuf[85] != ' ')
+				goto invalid;
+			if (!isdigit(linebuf[86]))
+				goto invalid;
+			fn_mod_104 = strtoul(linebuf + 86, 0, 10);
+		} else
+			goto invalid;
+		/*
+		 * Bit 15 of status word 0 is buffer validity flag,
+		 * bit 2 is BFI.
+		 */
+		if (!(status_words[0] & 0x8000) || (status_words[0] & 0x0004)) {
+			bfi[0] = 0xBF;
+			bfi[1] = fn_mod_104 == 60;
+			fwrite(bfi, 1, 2, outf);
+		} else {
+			efr_tidsp_to_std(tidsp_bytes, efr_bytes);
+			fwrite(efr_bytes, 1, 31, outf);
+		}
+	}
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/efrtest/tidsp.c	Tue Dec 13 07:14:41 2022 +0000
@@ -0,0 +1,67 @@
+/*
+ * The utility function implemented in this module extracts a 244-bit EFR
+ * codec frame from a 33-byte readout of Calypso DSP buffer, and presents
+ * this frame in the RTP encoding format of ETSI TS 101 318.
+ */
+
+#include <stdint.h>
+
+static const uint16_t efr_to_tidsp_table[244] = {
+	 50,  24,  25,  37,  38,  43,  44,  26,  42,  27,  39,  40,
+	 45,  51,  52,  41,  46,  28,  29,  47,  53,  77,  78,  30,
+	 54,  55,  79,  56,  80, 109, 110, 111, 112, 113, 114, 115,
+	180, 181,   0,   1,   2,   3,   4,   5,  20,  31,  32,  16,
+	 73, 105, 157,  81, 118, 138, 165,  82, 119, 139, 166,  83,
+	120, 140, 167,  84, 121, 141, 168,  85, 122, 186, 200, 204,
+	224, 244, 205, 225, 245, 206, 226, 246, 207, 227, 247, 208,
+	228, 248,  17,  61, 101, 153, 161,  12,  13,  22,  35,  48,
+	116,  18,  74, 106, 158,  86, 123, 142, 169,  87, 124, 143,
+	170,  88, 125, 144, 171,  89, 126, 145, 172,  90, 127, 189,
+	201, 209, 229, 249, 210, 230, 250, 211, 231, 251, 212, 232,
+	252, 213, 233, 253,  19,  62, 102, 154, 162,   6,   7,   8,
+	  9,  10,  11,  21,  33,  34,  57,  75, 107, 159,  91, 128,
+	146, 173,  92, 129, 147, 174,  93, 130, 148, 175,  94, 131,
+	149, 176,  95, 132, 192, 202, 214, 234, 254, 215, 235, 255,
+	216, 236, 256, 217, 237, 257, 218, 238, 258,  59,  63, 103,
+	155, 163,  14,  15,  23,  36,  49, 117,  58,  76, 108, 160,
+	 96, 133, 150, 177,  97, 134, 198, 199,  98, 135, 151, 178,
+	 99, 136, 152, 179, 100, 137, 195, 203, 219, 239, 259, 220,
+	240, 260, 221, 241, 261, 222, 242, 262, 223, 243, 263,  60,
+	 64, 104, 156, 164
+};
+
+static int
+msb_get_bit(buf, bn)
+	uint8_t *buf;
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = 7 - (bn & 7);
+
+	return (buf[pos_byte] >> pos_bit) & 1;
+}
+
+static void
+msb_set_bit(buf, bn, bit)
+	uint8_t *buf;
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = 7 - (bn & 7);
+
+	if (bit)
+		buf[pos_byte] |= (1 << pos_bit);
+	else
+		buf[pos_byte] &= ~(1 << pos_bit);
+}
+
+void
+efr_tidsp_to_std(inbytes, outbytes)
+	uint8_t *inbytes, *outbytes;
+{
+	int i, si;
+
+	outbytes[0] = 0xC0;
+	for (i = 0; i < 244; i++) {
+		si = efr_to_tidsp_table[i];
+		msb_set_bit(outbytes, i + 4, msb_get_bit(inbytes, si));
+        }
+}