changeset 591:2f1e91dd552b

amrconv: new program amr-hex-bwe2oa
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Nov 2025 23:19:57 +0000
parents 6b2900fe20f4
children 24aba0e7aa35
files .hgignore amrconv/Makefile amrconv/bwe2oa.c
diffstat 3 files changed, 101 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Nov 06 22:42:09 2025 +0000
+++ b/.hgignore	Thu Nov 06 23:19:57 2025 +0000
@@ -5,6 +5,7 @@
 
 ^amrconv/amr-cod-parse$
 ^amrconv/amr-cod2ietf$
+^amrconv/amr-hex-bwe2oa$
 ^amrconv/amr-hex-oa2bwe$
 ^amrconv/amr-hexoa2ietf$
 ^amrconv/amr-ietf-parse$
--- a/amrconv/Makefile	Thu Nov 06 22:42:09 2025 +0000
+++ b/amrconv/Makefile	Thu Nov 06 23:19:57 2025 +0000
@@ -1,5 +1,5 @@
-PROGS=	amr-cod-parse amr-cod2ietf amr-hex-oa2bwe amr-hexoa2ietf amr-ietf-parse\
-	amr-ietf2cod amr-ietf2hexoa gsm-amr2efr gsm-efr2amr
+PROGS=	amr-cod-parse amr-cod2ietf amr-hex-bwe2oa amr-hex-oa2bwe amr-hexoa2ietf\
+	amr-ietf-parse amr-ietf2cod amr-ietf2hexoa gsm-amr2efr gsm-efr2amr
 LIBTEST=../libtest/libtest.a
 
 include ../config.defs
@@ -19,6 +19,7 @@
 HEX2IETF_OBJS=	hex2ietf.o ietf_common.o
 IETF2HEX_OBJS=	ietf2hex.o ietf_common.o
 
+BWE2OA_OBJS=	bwe2oa.o bwe_conv_common.o
 OA2BWE_OBJS=	oa2bwe.o bwe_conv_common.o
 
 all:	${PROGS}
@@ -29,6 +30,9 @@
 amr-cod2ietf:	${COD2IETF_OBJS}
 	${CC} ${CFLAGS} -o $@ ${COD2IETF_OBJS}
 
+amr-hex-bwe2oa:	${BWE2OA_OBJS} ${LIBTEST}
+	${CC} ${CFLAGS} -o $@ ${BWE2OA_OBJS} ${LIBTEST}
+
 amr-hex-oa2bwe:	${OA2BWE_OBJS} ${LIBTEST}
 	${CC} ${CFLAGS} -o $@ ${OA2BWE_OBJS} ${LIBTEST}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/amrconv/bwe2oa.c	Thu Nov 06 23:19:57 2025 +0000
@@ -0,0 +1,94 @@
+/*
+ * This program reads a TW-TS-005 hexadecimal file, expects each record to be
+ * AMR in BWE format, converts it to OA format, and writes the output into
+ * a new TW-TS-005 hex file.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "../libtest/tw5reader.h"
+#include "../libtest/tw5writer.h"
+#include "amr_defs.h"
+
+extern const uint8_t pl_total_bytes_oa[9], pl_total_bytes_bwe[9];
+
+main(argc, argv)
+	char **argv;
+{
+	FILE *inf, *outf;
+	unsigned lineno;
+	uint8_t frm_in[TWTS005_MAX_FRAME], frm_out[MAX_IF1_BYTES + 2];
+	unsigned frm_in_len, cmr, ft, expect_len, out_len, n;
+	int rc;
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s amr-bwe.hex amr-oa.hex\n", argv[0]);
+		exit(1);
+	}
+	inf = fopen(argv[1], "r");
+	if (!inf) {
+		perror(argv[1]);
+		exit(1);
+	}
+	lineno = 0;
+	outf = fopen(argv[2], "w");
+	if (!outf) {
+		perror(argv[2]);
+		exit(1);
+	}
+	for (;;) {
+		rc = twts005_read_frame(inf, &lineno, frm_in, &frm_in_len);
+		if (rc < 0) {
+			fprintf(stderr, "%s line %u: not valid TW-TS-005\n",
+				argv[1], lineno);
+			exit(1);
+		}
+		if (!rc)
+			break;
+		if (frm_in_len == 0) {
+			fputs("NULL\n", outf);
+			continue;
+		}
+		if (frm_in_len < 2) {
+inv_payload:		fprintf(stderr,
+				"%s line %u: payload is not valid AMR BWE\n",
+				argv[1], lineno);
+			exit(1);
+		}
+		cmr = frm_in[0] >> 4;
+		if (cmr > MR122 && cmr != MODE_NO_DATA)
+			goto inv_payload;
+		if (frm_in[0] & 0x08)
+			goto inv_payload;
+		ft = ((frm_in[0] & 0x07) << 1) | ((frm_in[1] & 0x80) >> 7);
+		if (ft <= MRDTX)
+			expect_len = pl_total_bytes_bwe[ft];
+		else if (ft == MODE_NO_DATA)
+			expect_len = 2;
+		else
+			goto inv_payload;
+		if (frm_in_len != expect_len)
+			goto inv_payload;
+		/* With some frame types, the number of padding bits in OA
+		 * output will be greater than the number of padding bits in
+		 * BWE input.  The easiest way to ensure zero fill in those bits
+		 * is to zero the byte just past the input.  Because our frm_in
+		 * buffer is sized for TW-TS-005, we have room.
+		 */
+		frm_in[expect_len] = 0;
+		/* proceed to output */
+		if (ft != MODE_NO_DATA)
+			out_len = pl_total_bytes_oa[ft];
+		else
+			out_len = 2;
+		frm_out[0] = cmr << 4;
+		frm_out[1] = (ft << 3) | ((frm_in[1] & 0x40) >> 4);
+		for (n = 2; n < out_len; n++)
+			frm_out[n] = (frm_in[n-1] << 2) | (frm_in[n] >> 6);
+		emit_hex_frame(outf, frm_out, out_len);
+	}
+	exit(0);
+}