view miscutil/tw5a-dump.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 86d4ec69b36c
children
line wrap: on
line source

/*
 * This program reads a TW-TS-005 Annex A hexadecimal file
 * and dumps all frames in human-readable form.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../libgsmfr2/tw_gsmfr.h"
#include "../libgsmefr/gsm_efr.h"
#include "../libtest/tw5reader.h"

static void
dump_fr_frame(frame)
	uint8_t *frame;
{
	int16_t params[GSMFR_NUM_PARAMS];
	int i, j, n;

	printf("  SID=%d LARc", gsmfr_preproc_sid_classify(frame));
	gsmfr_unpack_to_array(frame, params);
	n = 0;
	for (i = 0; i < 8; i++)
		printf(" %d", params[n++]);
	if (!bcmp(frame, gsmfr_decoder_homing_frame, GSMFR_RTP_FRAME_LEN))
		fputs(" (DHF)", stdout);
	putchar('\n');
	for (i = 0; i < 4; i++) {
		putchar(' ');
		for (j = 0; j < 17; j++)
			printf(" %d", params[n++]);
		putchar('\n');
	}
}

static void
dump_efr_frame(frame)
	uint8_t *frame;
{
	int16_t params[EFR_NUM_PARAMS];
	int i, j, n;

	printf("  SID=%d LPC", EFR_sid_classify(frame));
	EFR_frame2params(frame, params);
	n = 0;
	for (i = 0; i < 5; i++)
		printf(" %d", params[n++]);
	if (!bcmp(frame, EFR_decoder_homing_frame, EFR_RTP_FRAME_LEN))
		fputs(" (DHF)", stdout);
	putchar('\n');
	for (i = 0; i < 4; i++) {
		putchar(' ');
		for (j = 0; j < 13; j++)
			printf(" %d", params[n++]);
		putchar('\n');
	}
}

main(argc, argv)
	char **argv;
{
	FILE *hexf;
	unsigned lineno;
	uint8_t frame[TWTS005_MAX_FRAME];
	unsigned frame_len, bfi;
	int rc;

	if (argc != 2) {
		fprintf(stderr, "usage: %s hex-file\n", argv[0]);
		exit(1);
	}
	hexf = fopen(argv[1], "r");
	if (!hexf) {
		perror(argv[1]);
		exit(1);
	}
	lineno = 0;
	for (;;) {
		rc = twts005_read_frame(hexf, &lineno, frame, &frame_len);
		if (rc < 0) {
			fprintf(stderr, "%s line %u: not valid TW-TS-005\n",
				argv[1], lineno);
			exit(1);
		}
		if (!rc)
			break;
		switch (frame_len) {
		case 0:
			printf("line %u: NULL frame\n", lineno);
			break;
		case 1:
			if ((frame[0] & 0xF6) != 0xE6)
				goto invalid;
			printf("line %u: No_Data frame\n", lineno);
			printf("  TEH=%02X (DTXd=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, frame[0] & 1);
			break;
		case GSMFR_RTP_FRAME_LEN:
			if ((frame[0] & 0xF0) != 0xD0)
				goto invalid;
			printf("line %u: FR basic frame\n", lineno);
			dump_fr_frame(frame);
			break;
		case GSMFR_RTP_FRAME_LEN+1:
			if ((frame[0] & 0xF4) != 0xE0)
				goto invalid;
			if ((frame[1] & 0xF0) != 0xD0)
				goto invalid;
			bfi = (frame[0] >> 1) & 1;
			printf("line %u: FR %s frame\n", lineno,
				bfi ? "bad" : "extended");
			printf("  TEH=%02X (DTXd=%u BFI=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, bfi, frame[0] & 1);
			dump_fr_frame(frame + 1);
			break;
		case EFR_RTP_FRAME_LEN:
			if ((frame[0] & 0xF0) != 0xC0)
				goto invalid;
			printf("line %u: EFR basic frame\n", lineno);
			dump_efr_frame(frame);
			break;
		case EFR_RTP_FRAME_LEN+1:
			if ((frame[0] & 0xF4) != 0xE0)
				goto invalid;
			if ((frame[1] & 0xF0) != 0xC0)
				goto invalid;
			bfi = (frame[0] >> 1) & 1;
			printf("line %u: EFR %s frame\n", lineno,
				bfi ? "bad" : "extended");
			printf("  TEH=%02X (DTXd=%u BFI=%u TAF=%u)\n", frame[0],
				(frame[0] >> 3) & 1, bfi, frame[0] & 1);
			dump_efr_frame(frame + 1);
			break;
		default:
		invalid:
			fprintf(stderr,
				"%s line %u: not a valid FR or EFR frame\n",
				argv[1], lineno);
			exit(1);
		}
	}
	exit(0);
}