view amrconv/ietf2cod.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 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);
}