changeset 103:0123ca1f1402

gsm-efr2amr utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 27 Nov 2022 05:41:13 +0000
parents dd88f9312170
children 7f43089e9a7a
files .hgignore miscutil/Makefile miscutil/efr2amr.c
diffstat 3 files changed, 83 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Nov 27 05:20:08 2022 +0000
+++ b/.hgignore	Sun Nov 27 05:41:13 2022 +0000
@@ -21,6 +21,7 @@
 ^frtest/gsmfr-preproc$
 
 ^miscutil/gsm-amr2efr$
+^miscutil/gsm-efr2amr$
 ^miscutil/gsmrec-dump$
 
 ^pcap/rtp-gsmfr-extr$
--- a/miscutil/Makefile	Sun Nov 27 05:20:08 2022 +0000
+++ b/miscutil/Makefile	Sun Nov 27 05:41:13 2022 +0000
@@ -1,17 +1,21 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	gsm-amr2efr gsmrec-dump
+PROGS=	gsm-amr2efr gsm-efr2amr gsmrec-dump
 LIBEFR=	../libgsmefr/libgsmefr.a
 LIBTEST=../libtest/libtest.a
 INSTBIN=/opt/freecalypso/bin
 
 AMR2EFR_OBJS=	amr122bits.o bitmanip.o amr2efr.o
+EFR2AMR_OBJS=	amr122bits.o bitmanip.o efr2amr.o
 
 all:	${PROGS}
 
 gsm-amr2efr:	${AMR2EFR_OBJS}
 	${CC} ${CFLAGS} -o $@ ${AMR2EFR_OBJS}
 
+gsm-efr2amr:	${EFR2AMR_OBJS} ${LIBTEST}
+	${CC} ${CFLAGS} -o $@ ${EFR2AMR_OBJS} ${LIBTEST}
+
 gsmrec-dump:	gsmrec-dump.o ${LIBTEST} ${LIBEFR}
 	${CC} ${CFLAGS} -o $@ gsmrec-dump.o ${LIBTEST} ${LIBEFR} -lgsm
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/miscutil/efr2amr.c	Sun Nov 27 05:41:13 2022 +0000
@@ -0,0 +1,77 @@
+/*
+ * Our gsm-efr2amr utility reads in an EFR speech recording in our
+ * extended-libgsm file format and converts it to AMR, inserting
+ * an AMR NO_DATA frame in the place of every BFI in the input
+ * but NOT performing any special handling or even detection of
+ * EFR SID frames.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "../libtest/binreader.h"
+
+extern const uint8_t amr_122_bit_order[244];
+
+static const char amr_file_hdr[] = "#!AMR\n";
+
+main(argc, argv)
+	char **argv;
+{
+	FILE *inf, *outf;
+	uint8_t frm_in[BINFILE_MAX_FRAME], frm_out[32];
+	unsigned bit_a, bit_n, outlen;
+	int rc, bfi;
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s input.gsmx output.amr\n", argv[0]);
+		exit(1);
+	}
+	inf = fopen(argv[1], "r");
+	if (!inf) {
+		perror(argv[1]);
+		exit(1);
+	}
+	outf = fopen(argv[2], "w");
+	if (!outf) {
+		perror(argv[2]);
+		exit(1);
+	}
+	for (;;) {
+		rc = binfile_read_frame(inf, frm_in);
+		if (rc < 0) {
+			fprintf(stderr, "error: garbage in %s\n", argv[1]);
+			exit(1);
+		}
+		if (!rc)
+			break;
+		if (frm_in[0] == 0xBF)
+			bfi = 1;
+		else if ((frm_in[0] & 0xF0) == 0xC0)
+			bfi = 0;
+		else {
+			fprintf(stderr,
+				"error: %s is not in EFR codec format\n",
+				argv[1]);
+			exit(1);
+		}
+		if (bfi) {
+			frm_out[0] = 0x78;
+			outlen = 1;
+		} else {
+			frm_out[0] = 0x3C;
+			bzero(frm_out + 1, 31);
+			for (bit_a = 0; bit_a < 244; bit_a++) {
+				bit_n = amr_122_bit_order[bit_a];
+				msb_set_bit(frm_out + 1, bit_a,
+					    msb_get_bit(frm_in, 4 + bit_n));
+			}
+			outlen = 32;
+		}
+		fwrite(frm_out, 1, outlen, outf);
+	}
+	fclose(outf);
+	exit(0);
+}