changeset 590:6b2900fe20f4

amrconv: new program amr-hex-oa2bwe
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Nov 2025 22:42:09 +0000
parents e414d138c607
children 2f1e91dd552b
files .hgignore amrconv/Makefile amrconv/bwe_conv_common.c amrconv/oa2bwe.c
diffstat 4 files changed, 112 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Nov 06 21:18:59 2025 +0000
+++ b/.hgignore	Thu Nov 06 22:42:09 2025 +0000
@@ -5,6 +5,7 @@
 
 ^amrconv/amr-cod-parse$
 ^amrconv/amr-cod2ietf$
+^amrconv/amr-hex-oa2bwe$
 ^amrconv/amr-hexoa2ietf$
 ^amrconv/amr-ietf-parse$
 ^amrconv/amr-ietf2cod$
--- a/amrconv/Makefile	Thu Nov 06 21:18:59 2025 +0000
+++ b/amrconv/Makefile	Thu Nov 06 22:42:09 2025 +0000
@@ -1,5 +1,5 @@
-PROGS=	amr-cod-parse amr-cod2ietf amr-hexoa2ietf amr-ietf-parse amr-ietf2cod \
-	amr-ietf2hexoa gsm-amr2efr gsm-efr2amr
+PROGS=	amr-cod-parse amr-cod2ietf 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,8 @@
 HEX2IETF_OBJS=	hex2ietf.o ietf_common.o
 IETF2HEX_OBJS=	ietf2hex.o ietf_common.o
 
+OA2BWE_OBJS=	oa2bwe.o bwe_conv_common.o
+
 all:	${PROGS}
 
 amr-cod-parse:	${COD_PARSE_OBJS}
@@ -27,6 +29,9 @@
 amr-cod2ietf:	${COD2IETF_OBJS}
 	${CC} ${CFLAGS} -o $@ ${COD2IETF_OBJS}
 
+amr-hex-oa2bwe:	${OA2BWE_OBJS} ${LIBTEST}
+	${CC} ${CFLAGS} -o $@ ${OA2BWE_OBJS} ${LIBTEST}
+
 amr-hexoa2ietf:	${HEX2IETF_OBJS} ${LIBTEST}
 	${CC} ${CFLAGS} -o $@ ${HEX2IETF_OBJS} ${LIBTEST}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/amrconv/bwe_conv_common.c	Thu Nov 06 22:42:09 2025 +0000
@@ -0,0 +1,10 @@
+/*
+ * This C module holds some common data items for conversion between
+ * OA and BWE formats of AMR RTP payload.
+ */
+
+#include <stdint.h>
+#include "amr_defs.h"
+
+const uint8_t pl_total_bytes_oa[9]  = {14, 15, 17, 19, 21, 22, 28, 33, 7};
+const uint8_t pl_total_bytes_bwe[9] = {14, 15, 16, 18, 20, 22, 27, 32, 7};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/amrconv/oa2bwe.c	Thu Nov 06 22:42:09 2025 +0000
@@ -0,0 +1,94 @@
+/*
+ * This program reads a TW-TS-005 hexadecimal file, expects each record to be
+ * AMR in OA/OAX format, converts it to BWE 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-oa.hex amr-bwe.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 OA\n",
+				argv[1], lineno);
+			exit(1);
+		}
+		cmr = frm_in[0] >> 4;
+		if (cmr > MR122 && cmr != MODE_NO_DATA)
+			goto inv_payload;
+		if (frm_in[1] & 0x80)
+			goto inv_payload;
+		ft = (frm_in[1] & 0x78) >> 3;
+		if (ft <= MRDTX)
+			expect_len = pl_total_bytes_oa[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 BWE
+		 * output will be greater than the number of padding bits in
+		 * OA 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_bwe[ft];
+		else
+			out_len = 2;
+		frm_out[0] = (cmr << 4) | (ft >> 1);
+		frm_out[1] = ((frm_in[1] & 0x0C) << 4) | (frm_in[2] >> 2);
+		for (n = 2; n < out_len; n++)
+			frm_out[n] = (frm_in[n] << 6) | (frm_in[n+1] >> 2);
+		emit_hex_frame(outf, frm_out, out_len);
+	}
+	exit(0);
+}