view amrconv/ietf2cod.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 1a7d659a952f
children
line wrap: on
line source

/*
 * This program converts an AMR-encoded speech recording from the common
 * IETF RFC 4867 .amr format into the 3GPP test sequence .cod format.
 */

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

extern unsigned amr_bit_lengths[9];
extern const char amr_file_hdr[IETF_HDR_LEN];
extern const uint8_t extra_bytes_per_ft[9];

static void
bits2words(ser_bits, cod_words, nbits)
	uint8_t *ser_bits;
	uint16_t *cod_words;
	unsigned nbits;
{
	uint8_t *sp = ser_bits;
	uint16_t *dp = cod_words;
	unsigned n;

	for (n = 0; n < nbits; n++)
		*dp++ = *sp++;
	for (; n < MAX_SERIAL_SIZE; n++)
		*dp++ = 0;
}

main(argc, argv)
	char **argv;
{
	char *infname, *outfname;
	FILE *inf, *outf;
	uint8_t frm_in[MAX_IF1_BYTES];
	unsigned frame_no, mode, qbit, sti, sid_mode;
	uint8_t ser_bits[MAX_SERIAL_SIZE];
	uint16_t cod_words[COD_FORMAT_NWORDS];
	int rc;

	if (argc != 3) {
		fprintf(stderr, "usage: %s input.amr output.cod\n", argv[0]);
		exit(1);
	}
	infname = argv[1];
	outfname = argv[2];
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	if (fread(frm_in, 1, IETF_HDR_LEN, inf) != IETF_HDR_LEN ||
	    bcmp(frm_in, amr_file_hdr, IETF_HDR_LEN)) {
		fprintf(stderr, "error: %s is not in IETF AMR format\n",
			infname);
		exit(1);
	}
	outf = fopen(outfname, "w");
	if (!outf) {
		perror(outfname);
		exit(1);
	}
	/* padding words which will never be filled */
	bzero(cod_words + MAX_SERIAL_SIZE + 2, sizeof(uint16_t) * 4);
	for (frame_no = 0; ; frame_no++) {
		rc = getc(inf);
		if (rc < 0)
			break;
		mode = (rc & 0x78) >> 3;
		qbit = (rc & 4) >> 2;
		if (mode == MODE_NO_DATA) {
			cod_words[0] = TX_NO_DATA;
			bzero(cod_words+1, sizeof(uint16_t) * MAX_SERIAL_SIZE);
			cod_words[245] = 0xFFFF;
			goto output;
		}
		if (mode > MRDTX) {
			fprintf(stderr, "error in frame #%u: invalid FT=%u\n",
				frame_no, mode);
			exit(1);
		}
		rc = fread(frm_in, 1, extra_bytes_per_ft[mode], inf);
		if (rc != extra_bytes_per_ft[mode]) {
			fprintf(stderr,
				"error: short read from %s on frame #%u\n",
				infname, frame_no);
			exit(1);
		}
		amr_if1_unpack(frm_in, ser_bits, mode);
		if (mode == MRDTX) {
			sti = (frm_in[4] & 0x10) >> 4;
			sid_mode = 0;
			if (frm_in[4] & 0x08)
				sid_mode |= 1;
			if (frm_in[4] & 0x04)
				sid_mode |= 2;
			if (frm_in[4] & 0x02)
				sid_mode |= 4;
		}
		bits2words(ser_bits, cod_words + 1, amr_bit_lengths[mode]);
		if (mode == MRDTX) {
			if (qbit)
				cod_words[0] = sti ? TX_SID_UPDATE
						   : TX_SID_FIRST;
			else
				cod_words[0] = TX_SID_BAD;
			cod_words[245] = sid_mode;
		} else {
			cod_words[0] = qbit ? TX_SPEECH_GOOD : TX_SPEECH_BAD;
			cod_words[245] = mode;
		}
output:		fwrite(cod_words, 2, COD_FORMAT_NWORDS, outf);
	}
	fclose(outf);
	exit(0);
}