view amrconv/cod2ietf.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 4c4649a5fec3
children
line wrap: on
line source

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

#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];

main(argc, argv)
	char **argv;
{
	char *infname, *outfname;
	FILE *inf, *outf;
	int big_endian;
	unsigned frame_no, type, mode, outlen;
	uint8_t input_bits[COD_FORMAT_NWORDS], frm_out[MAX_IF1_BYTES+1];
	int rc;

	if (argc == 3 && argv[1][0] != '-') {
		big_endian = 0;
		infname = argv[1];
		outfname = argv[2];
	} else if (argc == 4 && !strcmp(argv[1], "-b")) {
		big_endian = 1;
		infname = argv[2];
		outfname = argv[3];
	} else {
		fprintf(stderr, "usage: %s [-b] input.cod 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, IETF_HDR_LEN, outf);
	for (frame_no = 0; ; frame_no++) {
		rc = read_cod_bits(inf, big_endian, input_bits, infname);
		if (!rc)
			break;
		type = input_bits[0];
		mode = input_bits[245];
		switch (type) {
		case TX_SPEECH_GOOD:
			if (mode > MR122) {
invalid_mode:			fprintf(stderr,
					"error in %s frame #%u: invalid mode\n",
					infname, frame_no);
				exit(1);
			}
			preen_frame_bits(input_bits+1, amr_bit_lengths[mode],
					 infname, frame_no);
			frm_out[0] = (mode << 3) | 0x04;
			amr_if1_pack(frm_out+1, input_bits+1, mode);
			outlen = extra_bytes_per_ft[mode] + 1;
			break;
		case TX_SID_FIRST:
		case TX_SID_UPDATE:
			if (mode > MR122)
				goto invalid_mode;
			preen_frame_bits(input_bits+1, AMR_NBITS_SID, infname,
					 frame_no);
			frm_out[0] = 0x44;
			amr_if1_pack(frm_out+1, input_bits+1, MRDTX);
			if (type == TX_SID_UPDATE)
				frm_out[5] |= 0x10;
			if (mode & 1)
				frm_out[5] |= 0x08;
			if (mode & 2)
				frm_out[5] |= 0x04;
			if (mode & 4)
				frm_out[5] |= 0x02;
			outlen = 6;
			break;
		case TX_NO_DATA:
			frm_out[0] = 0x7C;
			outlen = 1;
			break;
		default:
			fprintf(stderr, "error in %s frame #%u: invalid type\n",
				infname, frame_no);
			exit(1);
		}
		fwrite(frm_out, 1, outlen, outf);
	}
	fclose(outf);
	exit(0);
}