view amrconv/efr2amr.c @ 581:e2d5cad04cbf

libgsmhr1 RxFE: store CN R0+LPC separately from speech In the original GSM 06.06 code the ECU for speech mode is entirely separate from the CN generator, maintaining separate state. (The main intertie between them is the speech vs CN state variable, distinguishing between speech and CN BFIs, in addition to the CN-specific function of distinguishing between initial and update SIDs.) In the present RxFE implementation I initially thought that we could use the same saved_frame buffer for both ECU and CN, overwriting just the first 4 params (R0 and LPC) when a valid SID comes in. However, I now realize it was a bad idea: the original code has a corner case (long sequence of speech-mode BFIs to put the ECU in state 6, then SID and CN-mode BFIs, then a good speech frame) that would be broken by that buffer reuse approach. We could eliminate this corner case by resetting the ECU state when passing through a CN insertion period, but doing so would needlessly increase the behavioral diffs between GSM 06.06 and our version. Solution: use a separate CN-specific buffer for CN R0+LPC parameters, and match the behavior of GSM 06.06 code in this regard.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Feb 2025 10:02:45 +0000
parents b092a510141e
children
line wrap: on
line source

/*
 * 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;
{
	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 && 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(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	outf = fopen(outfname, "w");
	if (!outf) {
		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", infname);
			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",
				infname);
			exit(1);
		}
		if (bfi) {
			frm_out[0] = 0x78;
			outlen = 1;
		} else {
			frm_out[0] = 0x3C;
			frm_out[31] = 0;
			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 + 1, bit_a,
					    msb_get_bit(frm_in, 4 + bit_n));
			}
			outlen = 32;
		}
		fwrite(frm_out, 1, outlen, outf);
	}
	fclose(outf);
	exit(0);
}