changeset 588:4d6ccca0c687

amrconv: new program amr-hexoa2ietf
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Nov 2025 20:12:15 +0000
parents 7bce90c844c2
children e414d138c607
files .hgignore amrconv/Makefile amrconv/hex2ietf.c
diffstat 3 files changed, 87 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Mar 12 20:38:23 2025 +0000
+++ b/.hgignore	Thu Nov 06 20:12:15 2025 +0000
@@ -5,6 +5,7 @@
 
 ^amrconv/amr-cod-parse$
 ^amrconv/amr-cod2ietf$
+^amrconv/amr-hexoa2ietf$
 ^amrconv/amr-ietf-parse$
 ^amrconv/amr-ietf2cod$
 ^amrconv/gsm-amr2efr$
--- a/amrconv/Makefile	Wed Mar 12 20:38:23 2025 +0000
+++ b/amrconv/Makefile	Thu Nov 06 20:12:15 2025 +0000
@@ -1,5 +1,5 @@
-PROGS=	amr-cod-parse amr-cod2ietf amr-ietf-parse amr-ietf2cod gsm-amr2efr \
-	gsm-efr2amr
+PROGS=	amr-cod-parse amr-cod2ietf amr-hexoa2ietf amr-ietf-parse amr-ietf2cod \
+	gsm-amr2efr gsm-efr2amr
 LIBTEST=../libtest/libtest.a
 
 include ../config.defs
@@ -16,6 +16,8 @@
 IETF2COD_OBJS=	amr122bits.o amr_bits.o amr_common_tbl.o bitmanip.o ietf2cod.o \
 		ietf_common.o if1_unpack.o
 
+HEX2IETF_OBJS=	hex2ietf.o ietf_common.o
+
 all:	${PROGS}
 
 amr-cod-parse:	${COD_PARSE_OBJS}
@@ -24,6 +26,9 @@
 amr-cod2ietf:	${COD2IETF_OBJS}
 	${CC} ${CFLAGS} -o $@ ${COD2IETF_OBJS}
 
+amr-hexoa2ietf:	${HEX2IETF_OBJS} ${LIBTEST}
+	${CC} ${CFLAGS} -o $@ ${HEX2IETF_OBJS} ${LIBTEST}
+
 amr-ietf-parse:	${IETF_PARSE_OBJS}
 	${CC} ${CFLAGS} -o $@ ${IETF_PARSE_OBJS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/amrconv/hex2ietf.c	Thu Nov 06 20:12:15 2025 +0000
@@ -0,0 +1,79 @@
+/*
+ * This program converts an AMR speech recording from TW-TS-005 Annex C OA/OAX
+ * hexadecimal format into the standard binary storage format of RFC 4867.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "../libtest/tw5reader.h"
+#include "amr_defs.h"
+
+extern const char amr_file_hdr[IETF_HDR_LEN];
+extern const uint8_t extra_bytes_per_ft[9];
+
+main(argc, argv)
+	char **argv;
+{
+	FILE *hexf, *outf;
+	unsigned lineno;
+	uint8_t frame[TWTS005_MAX_FRAME];
+	unsigned frame_len, cmr, ft, if1_len;
+	int rc;
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s input.hex output.amr\n", argv[0]);
+		exit(1);
+	}
+	hexf = fopen(argv[1], "r");
+	if (!hexf) {
+		perror(argv[1]);
+		exit(1);
+	}
+	lineno = 0;
+	outf = fopen(argv[2], "w");
+	if (!outf) {
+		perror(argv[2]);
+		exit(1);
+	}
+	fwrite(amr_file_hdr, 1, IETF_HDR_LEN, outf);
+	for (;;) {
+		rc = twts005_read_frame(hexf, &lineno, frame, &frame_len);
+		if (rc < 0) {
+			fprintf(stderr, "%s line %u: not valid TW-TS-005\n",
+				argv[1], lineno);
+			exit(1);
+		}
+		if (!rc)
+			break;
+		if (frame_len == 0) {
+			putc(0x7C, outf);
+			continue;
+		}
+		if (frame_len < 2) {
+inv_payload:		fprintf(stderr,
+				"%s line %u: payload is not valid AMR OA\n",
+				argv[1], lineno);
+			exit(1);
+		}
+		cmr = frame[0] >> 4;
+		if (cmr > MR122 && cmr != MODE_NO_DATA)
+			goto inv_payload;
+		if (frame[1] & 0x80)
+			goto inv_payload;
+		ft = (frame[1] & 0x78) >> 3;
+		if (ft <= MRDTX)
+			if1_len = extra_bytes_per_ft[ft];
+		else if (ft == MODE_NO_DATA)
+			if1_len = 0;
+		else
+			goto inv_payload;
+		if (frame_len < if1_len + 2)
+			goto inv_payload;
+		frame[1] &= 0xFC;
+		fwrite(frame + 1, 1, if1_len + 1, outf);
+	}
+	exit(0);
+}