view amrconv/amr2efr.c @ 242:f081a6850fb5

libgsmfrp: new refined implementation The previous implementation exhibited the following defects, which are now fixed: 1) The last received valid SID was cached forever for the purpose of handling future invalid SIDs - we could have received some valid SID ages ago, then lots of speech or NO_DATA, and if we then get an invalid SID, we would resurrect the last valid SID from ancient history - a bad design. In our new design, we handle invalid SID based on the current state, much like BFI. 2) GSM 06.11 spec says clearly that after the second lost SID (received BFI=1 && TAF=1 in CN state) we need to gradually decrease the output level, rather than jump directly to emitting silence frames - we previously failed to implement such logic. 3) Per GSM 06.12 section 5.2, Xmaxc should be the same in all 4 subframes in a SID frame. What should we do if we receive an otherwise valid SID frame with different Xmaxc? Our previous approach would replicate this Xmaxc oddity in every subsequent generated CN frame, which is rather bad. In our new design, the very first CN frame (which can be seen as a transformation of the SID frame itself) retains the original 4 distinct Xmaxc, but all subsequent CN frames are based on the Xmaxc from the last subframe of the most recent SID.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 09 May 2023 05:16:31 +0000
parents b092a510141e
children
line wrap: on
line source

/*
 * Our gsm-amr2efr utility reads in a speech recording in .amr format
 * (the mode must be MR122 for every frame, no DTX) and converts it into
 * our ad hoc .gsmx format for EFR.  The conversion is a bit reshuffling
 * only, no transcoding takes place.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

extern const uint8_t amr_122_bit_order[244];

static const char amr_file_hdr[] = "#!AMR\n";

main(argc, argv)
	char **argv;
{
	char *infname, *outfname;
	FILE *inf, *outf;
	int wrong_flag, hdrb;
	uint8_t frm_in[31], frm_out[31];
	unsigned bit_a, bit_n;

	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.amr output.gsmx\n",
			argv[0]);
		exit(1);
	}
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	if (fread(frm_in, 1, 6, inf) != 6 || bcmp(frm_in, amr_file_hdr, 6)) {
		fprintf(stderr, "error: %s is not in AMR format\n", infname);
		exit(1);
	}
	outf = fopen(outfname, "w");
	if (!outf) {
		perror(outfname);
		exit(1);
	}
	for (;;) {
		hdrb = getc(inf);
		if (hdrb < 0)
			break;
		if ((hdrb & 0x78) != 0x38) {
			fprintf(stderr, "error: unexpected content in %s\n",
				infname);
			exit(1);
		}
		if (fread(frm_in, 1, 31, inf) != 31) {
			fprintf(stderr, "error: short read from %s\n", infname);
			exit(1);
		}
		frm_out[0] = 0xC0;
		for (bit_a = 0; bit_a < 244; bit_a++) {
			if (wrong_flag)
				bit_n = bit_a;
			else
				bit_n = amr_122_bit_order[bit_a];
			msb_set_bit(frm_out, 4 + bit_n,
				    msb_get_bit(frm_in, bit_a));
		}
		fwrite(frm_out, 1, 31, outf);
	}
	fclose(outf);
	exit(0);
}