view efrtest/etsi-enc.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 a13b1605142b
children d4f47d0962e7
line wrap: on
line source

/*
 * gsmefr-etsi-enc is a test program for our EFR encoder: it reads raw
 * 16-bit PCM (matching ETSI's *.inp) as input and writes ETSI's *.cod
 * format as output, allowing our decoder to be tested with ETSI's
 * official test sequences.
 *
 * ETSI input and output files are read and written in LE byte order,
 * matching the official test sequences in ts_100725v050200p0.zip.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libgsmefr/gsm_efr.h"

static int
read_input(inf, pcm, filename_for_errs, big_endian)
	FILE *inf;
	int16_t *pcm;
	char *filename_for_errs;
{
	uint8_t file_bytes[320], *sp;
	int cc;
	unsigned n;

	cc = fread(file_bytes, 2, 160, inf);
	if (cc == 0)
		return 0;
	if (cc != 160) {
		fprintf(stderr, "error: short read from %s\n",
			filename_for_errs);
		exit(1);
	}
	sp = file_bytes;
	for (n = 0; n < 160; n++) {
		if (big_endian)
			pcm[n] = (sp[0] << 8) | sp[1];
		else
			pcm[n] = sp[0] | (sp[1] << 8);
		sp += 2;
	}
	return 1;
}

static void
frame2bits(frame, bits)
	uint8_t *frame, *bits;
{
	unsigned nb, byte, mask, bit;

	for (nb = 0; nb < EFR_RTP_FRAME_LEN; nb++) {
		byte = *frame++;
		for (mask = 0x80; mask; mask >>= 1) {
			if (byte & mask)
				bit = 1;
			else
				bit = 0;
			*bits++ = bit;
		}
	}
}

static void
emit_output(outf, bits, nbits, big_endian)
	FILE *outf;
	uint8_t *bits;
	unsigned nbits;
{
	unsigned n;

	for (n = 0; n < nbits; n++) {
		if (big_endian) {
			putc(0, outf);
			putc(bits[n], outf);
		} else {
			putc(bits[n], outf);
			putc(0, outf);
		}
	}
}

main(argc, argv)
	char **argv;
{
	char *infname, *outfname;
	FILE *inf, *outf;
	struct EFR_encoder_state *state;
	int16_t pcm[160];
	uint8_t frame[EFR_RTP_FRAME_LEN], bits[250];
	int opt, dtx = 0, rc, sp, vad, big_endian = 0;
	extern int optind;

	while ((opt = getopt(argc, argv, "bd")) != EOF) {
		switch (opt) {
		case 'b':
			big_endian = 1;
			continue;
		case 'd':
			dtx = 1;
			continue;
		default:
		usage:
			fprintf(stderr,
				"usage: %s [-b] [-d] input.inp output.cod\n",
				argv[0]);
			exit(1);
		}
	}
	if (argc != optind + 2)
		goto usage;
	infname = argv[optind];
	outfname = argv[optind+1];

	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	outf = fopen(outfname, "w");
	if (!outf) {
		perror(outfname);
		exit(1);
	}
	state = EFR_encoder_create(dtx);
	if (!state) {
		perror("EFR_encoder_create()");
		exit(1);
	}
	for (;;) {
		rc = read_input(inf, pcm, infname, big_endian);
		if (!rc)
			break;
		EFR_encode_frame(state, pcm, frame, &sp, &vad);
		frame2bits(frame, bits);
		bits[248] = vad;
		bits[249] = sp;
		emit_output(outf, bits + 4, 246, big_endian);
	}
	fclose(outf);
	exit(0);
}