changeset 627:45e727b53da1

hrutil: new program gsmhr-etsi-enc
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 12 Mar 2026 04:46:03 +0000
parents c15ae3f06ee9
children c53ce88d67a7
files .hgignore hrutil/Makefile hrutil/etsi-enc.c
diffstat 3 files changed, 115 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Mar 12 04:00:15 2026 +0000
+++ b/.hgignore	Thu Mar 12 04:46:03 2026 +0000
@@ -91,6 +91,7 @@
 ^hrutil/gsmhr-decode$
 ^hrutil/gsmhr-decode-r$
 ^hrutil/gsmhr-etsi-dec$
+^hrutil/gsmhr-etsi-enc$
 ^hrutil/gsmhr-hex2dec$
 ^hrutil/gsmhr-hex2rpf$
 ^hrutil/gsmhr-rpf2hex$
--- a/hrutil/Makefile	Thu Mar 12 04:00:15 2026 +0000
+++ b/hrutil/Makefile	Thu Mar 12 04:46:03 2026 +0000
@@ -1,6 +1,6 @@
 PROGS=	gsmhr-cod-craft gsmhr-cod-parse gsmhr-cod2hex gsmhr-dec-craft \
 	gsmhr-dec-parse gsmhr-dec2hex gsmhr-decode gsmhr-decode-r \
-	gsmhr-etsi-dec gsmhr-hex2dec gsmhr-hex2rpf gsmhr-rpf2hex \
+	gsmhr-etsi-dec gsmhr-etsi-enc gsmhr-hex2dec gsmhr-hex2rpf gsmhr-rpf2hex\
 	gsmhr-tfo-xfrm gsmhr-tfo-xfrm-dc tw5b-dump
 LIBHR1=	../libgsmhr1/libgsmhr1.a
 LIBTEST=../libtest/libtest.a
@@ -37,6 +37,9 @@
 gsmhr-etsi-dec:	etsi-dec.o etsi-pcm-out.o read-dec.o ${LIBS}
 	${CC} ${CFLAGS} -o $@ $^
 
+gsmhr-etsi-enc:	cod-out-endian.o etsi-enc.o ${LIBS}
+	${CC} ${CFLAGS} -o $@ $^
+
 gsmhr-hex2dec:	hex2dec.o ${LIBS}
 	${CC} ${CFLAGS} -o $@ $^
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hrutil/etsi-enc.c	Thu Mar 12 04:46:03 2026 +0000
@@ -0,0 +1,110 @@
+/*
+ * gsmhr-etsi-enc is a test program for our GSM-HR encoder: it reads raw
+ * 16-bit PCM (matching ETSI's *.inp) as input and writes ETSI's *.cod
+ * format as output, allowing our encoder to be tested with ETSI's
+ * official test sequences.
+ *
+ * ETSI input and output files are read and written in the local machine's
+ * native byte order by default; -b and -l options can be used to force
+ * BE or LE, respectively.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include "../libgsmhr1/tw_gsmhr.h"
+#include "../libtest/local_endian.h"
+
+static int
+read_input(inf, pcm, filename_for_errs, big_endian)
+	FILE *inf;
+	int16_t *pcm;
+	char *filename_for_errs;
+{
+	uint8_t file_bytes[320], *sp;
+	int cc;
+	unsigned n;
+
+	cc = fread(file_bytes, 2, 160, inf);
+	if (cc == 0)
+		return 0;
+	if (cc != 160) {
+		fprintf(stderr, "error: short read from %s\n",
+			filename_for_errs);
+		exit(1);
+	}
+	sp = file_bytes;
+	for (n = 0; n < 160; n++) {
+		if (big_endian)
+			pcm[n] = (sp[0] << 8) | sp[1];
+		else
+			pcm[n] = sp[0] | (sp[1] << 8);
+		sp += 2;
+	}
+	return 1;
+}
+
+main(argc, argv)
+	char **argv;
+{
+	char *infname, *outfname;
+	FILE *inf, *outf;
+	int opt, rc, big_endian, dtx = 0;
+	struct gsmhr_encoder_state *state;
+	int16_t pcm[160];
+	int16_t prm_out[GSMHR_NUM_PARAMS_ENC];
+	extern int optind;
+
+	big_endian = is_native_big_endian();
+	while ((opt = getopt(argc, argv, "bdl")) != EOF) {
+		switch (opt) {
+		case 'b':
+			big_endian = 1;
+			continue;
+		case 'd':
+			dtx = 1;
+			continue;
+		case 'l':
+			big_endian = 0;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+				"usage: %s [-b|-l] [-d] input.inp output.cod\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind + 2)
+		goto usage;
+	infname = argv[optind];
+	outfname = argv[optind+1];
+
+	inf = fopen(infname, "r");
+	if (!inf) {
+		perror(infname);
+		exit(1);
+	}
+	outf = fopen(outfname, "w");
+	if (!outf) {
+		perror(outfname);
+		exit(1);
+	}
+	state = gsmhr_encoder_create(dtx);
+	if (!state) {
+		perror("gsmhr_encoder_create()");
+		exit(1);
+	}
+	for (;;) {
+		rc = read_input(inf, pcm, infname, big_endian);
+		if (!rc)
+			break;
+		gsmhr_encode_frame(state, pcm, prm_out);
+		emit_cod_to_endian(outf, prm_out, big_endian);
+	}
+	fclose(outf);
+	exit(0);
+}