diff miscutil/efr2amr.c @ 106:e7c8d739c4c8

gsm-efr2amr: implement -w option like in gsm-amr2efr
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 27 Nov 2022 06:25:06 +0000
parents 7f43089e9a7a
children 41f1ae68d253
line wrap: on
line diff
--- a/miscutil/efr2amr.c	Sun Nov 27 05:59:10 2022 +0000
+++ b/miscutil/efr2amr.c	Sun Nov 27 06:25:06 2022 +0000
@@ -20,30 +20,41 @@
 main(argc, argv)
 	char **argv;
 {
+	char *infname, *outfname;
 	FILE *inf, *outf;
+	int wrong_flag;
 	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]);
+	if (argc == 3 && argv[1][0] != '-') {
+		wrong_flag = 0;
+		infname = argv[1];
+		outfname = argv[2];
+	} else if (argc == 4 && !strcmp(argv[1], "-w")) {
+		wrong_flag = 1;
+		infname = argv[2];
+		outfname = argv[3];
+	} else {
+		fprintf(stderr, "usage: %s [-w] input.gsmx output.amr\n",
+			argv[0]);
 		exit(1);
 	}
-	inf = fopen(argv[1], "r");
+	inf = fopen(infname, "r");
 	if (!inf) {
-		perror(argv[1]);
+		perror(infname);
 		exit(1);
 	}
-	outf = fopen(argv[2], "w");
+	outf = fopen(outfname, "w");
 	if (!outf) {
-		perror(argv[2]);
+		perror(outfname);
 		exit(1);
 	}
 	fwrite(amr_file_hdr, 1, 6, outf);
 	for (;;) {
 		rc = binfile_read_frame(inf, frm_in);
 		if (rc < 0) {
-			fprintf(stderr, "error: garbage in %s\n", argv[1]);
+			fprintf(stderr, "error: garbage in %s\n", infname);
 			exit(1);
 		}
 		if (!rc)
@@ -55,7 +66,7 @@
 		else {
 			fprintf(stderr,
 				"error: %s is not in EFR codec format\n",
-				argv[1]);
+				infname);
 			exit(1);
 		}
 		if (bfi) {
@@ -65,7 +76,10 @@
 			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];
+				if (wrong_flag)
+					bit_n = bit_a;
+				else
+					bit_n = amr_122_bit_order[bit_a];
 				msb_set_bit(frm_out + 1, bit_a,
 					    msb_get_bit(frm_in, 4 + bit_n));
 			}