changeset 151:a13b1605142b

gsmefr-etsi-enc: add BE support
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 14 Dec 2022 22:25:31 +0000
parents da17c7f02c6c
children a217a6eacbad
files efrtest/etsi-enc.c
diffstat 1 files changed, 38 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/efrtest/etsi-enc.c	Wed Dec 14 22:04:39 2022 +0000
+++ b/efrtest/etsi-enc.c	Wed Dec 14 22:25:31 2022 +0000
@@ -13,10 +13,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
+#include <unistd.h>
 #include "../libgsmefr/gsm_efr.h"
 
 static int
-read_input(inf, pcm, filename_for_errs)
+read_input(inf, pcm, filename_for_errs, big_endian)
 	FILE *inf;
 	int16_t *pcm;
 	char *filename_for_errs;
@@ -35,7 +36,10 @@
 	}
 	sp = file_bytes;
 	for (n = 0; n < 160; n++) {
-		pcm[n] = sp[0] | (sp[1] << 8);
+		if (big_endian)
+			pcm[n] = (sp[0] << 8) | sp[1];
+		else
+			pcm[n] = sp[0] | (sp[1] << 8);
 		sp += 2;
 	}
 	return 1;
@@ -60,7 +64,7 @@
 }
 
 static void
-emit_output(outf, bits, nbits)
+emit_output(outf, bits, nbits, big_endian)
 	FILE *outf;
 	uint8_t *bits;
 	unsigned nbits;
@@ -68,8 +72,13 @@
 	unsigned n;
 
 	for (n = 0; n < nbits; n++) {
-		putc(bits[n], outf);
-		putc(0, outf);
+		if (big_endian) {
+			putc(0, outf);
+			putc(bits[n], outf);
+		} else {
+			putc(bits[n], outf);
+			putc(0, outf);
+		}
 	}
 }
 
@@ -81,21 +90,30 @@
 	struct EFR_encoder_state *state;
 	int16_t pcm[160];
 	uint8_t frame[EFR_RTP_FRAME_LEN], bits[250];
-	int dtx, rc, sp, vad;
+	int opt, dtx = 0, rc, sp, vad, big_endian = 0;
+	extern int optind;
 
-	if (argc == 3 && argv[1][0] != '-') {
-		dtx = 0;
-		infname = argv[1];
-		outfname = argv[2];
-	} else if (argc == 4 && !strcmp(argv[1], "-d")) {
-		dtx = 1;
-		infname = argv[2];
-		outfname = argv[3];
-	} else {
-		fprintf(stderr, "usage: %s [-d] input.inp output.cod\n",
-			argv[0]);
-		exit(1);
+	while ((opt = getopt(argc, argv, "bd")) != EOF) {
+		switch (opt) {
+		case 'b':
+			big_endian = 1;
+			continue;
+		case 'd':
+			dtx = 1;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+				"usage: %s [-b] [-d] input.inp output.cod\n",
+				argv[0]);
+			exit(1);
+		}
 	}
+	if (argc != optind + 2)
+		goto usage;
+	infname = argv[optind];
+	outfname = argv[optind+1];
+
 	inf = fopen(infname, "r");
 	if (!inf) {
 		perror(infname);
@@ -112,14 +130,14 @@
 		exit(1);
 	}
 	for (;;) {
-		rc = read_input(inf, pcm, argv[1]);
+		rc = read_input(inf, pcm, infname, big_endian);
 		if (!rc)
 			break;
 		EFR_encode_frame(state, pcm, frame, &sp, &vad);
 		frame2bits(frame, bits);
 		bits[248] = vad;
 		bits[249] = sp;
-		emit_output(outf, bits + 4, 246);
+		emit_output(outf, bits + 4, 246, big_endian);
 	}
 	fclose(outf);
 	exit(0);