diff amrconv/oa2bwe.c @ 590:6b2900fe20f4

amrconv: new program amr-hex-oa2bwe
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Nov 2025 22:42:09 +0000
parents amrconv/hex2ietf.c@4d6ccca0c687
children
line wrap: on
line diff
--- /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);
+}