view test-voice/pcmu_catcher.c @ 16:4f81b959a5f5

sipout-test-voice: implement PCMU GSM uplink catcher
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 13 May 2024 22:10:25 -0800
parents
children
line wrap: on
line source

/*
 * This module implements the logic that scans received PCMU sample streams
 * for patterns that result from a GSM-FR or GSM-EFR decoder processing
 * uplink test frames constructed in vband-misc repository, ul-test
 * subdirectory.
 */

#include <stdio.h>
#include <stdint.h>

static const uint8_t match_frame_fr[160] = {
0xFF,0x0F,0x0E,0x1B,0x8D,0x8A,0x9C,0x23,0x1D,0x38,0x1C,0x1B,0x26,0x8A,0x80,0x92,
0x11,0x06,0x1E,0xA3,0x9B,0xBF,0x9B,0x91,0x9C,0x11,0x06,0x1C,0x98,0x87,0x8D,0xAF,
0x1D,0x16,0x13,0x1F,0xBE,0x8D,0x88,0x99,0x1D,0x07,0x05,0x12,0xAC,0x8B,0x87,0x95,
0x25,0x09,0x06,0x14,0xAA,0x8B,0x88,0x97,0x25,0x0A,0x09,0x1A,0xA4,0x8C,0x8A,0x99,
0x28,0x0C,0x09,0x1B,0xA5,0x8C,0x8B,0x9D,0x23,0x0C,0x0A,0x19,0xA4,0x8C,0x8A,0x9D,
0x1E,0x0A,0x0A,0x1C,0xA2,0x8C,0x8B,0x9B,0x25,0x0C,0x0A,0x19,0xA7,0x8C,0x8B,0x9B,
0x26,0x0D,0x0B,0x1A,0xAA,0x8D,0x8A,0x99,0x2A,0x0D,0x0B,0x1A,0xAB,0x8E,0x8C,0x99,
0x2F,0x0F,0x0B,0x19,0xAE,0x8E,0x8C,0x99,0x34,0x0F,0x0D,0x1A,0xB2,0x91,0x8D,0x99,
0x3B,0x12,0x0D,0x19,0xBA,0x92,0x8E,0x9B,0x38,0x14,0x0F,0x1B,0xBB,0x94,0x8E,0x9A,
0x3B,0x13,0x0E,0x1B,0xBA,0x95,0x90,0x9B,0x3F,0x16,0x0F,0x1B,0xBB,0x94,0x8F,0x9C,
};

static const uint8_t match_frame_efr_classic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x2D,0x1C,0x1C,0x35,0xA5,0x96,0x98,0xB4,0x1F,0x12,0x17,0x3D,0x9C,0x90,0x96,0xC1,
0x1A,0x0E,0x12,0x38,0x98,0x8C,0x8F,0xB4,0x16,0x0A,0x0D,0x32,0x94,0x88,0x8C,0xAE,
0x13,0x08,0x0C,0x35,0x92,0x88,0x8C,0xB3,0x13,0x08,0x0D,0x35,0x93,0x88,0x8D,0xB3,
0x13,0x09,0x0D,0x34,0x94,0x89,0x8D,0xB3,0x15,0x0A,0x0E,0x35,0x96,0x8B,0x8E,0xB5,
0x16,0x0B,0x0E,0x32,0x97,0x8B,0x8E,0xB3,0x16,0x0A,0x0E,0x32,0x96,0x8A,0x8E,0xB1,
0x16,0x0B,0x0E,0x31,0x96,0x8B,0x8E,0xB1,0x16,0x0B,0x0E,0x31,0x97,0x8B,0x8E,0xB1,
0x16,0x0A,0x0E,0x30,0x97,0x8A,0x8E,0xAF,0x17,0x0B,0x0E,0x2F,0x97,0x8B,0x8E,0xAE,
};

enum state {
	STATE_GROUND,
	STATE_FE,
	STATE_EFR_PRELIM,
	STATE_EFR_CLASSIC,
	STATE_EFR_AMR,
	STATE_FR,
};

static enum state state;
static unsigned fe_byte_count, match_pos;

void
pcmu_catcher_reset()
{
	state = STATE_GROUND;
}

static void
report_match(name, offset)
	char *name;
	unsigned offset;
{
	offset++;
	if (offset == 160)
		offset = 0;
	printf("Caught output of %s decoder at offset %u\n", name, offset);
}

static void
process_sample(samp, offset)
	unsigned samp, offset;
{
	if (samp == 0xFE) {
		if (state != STATE_FE) {
			state = STATE_FE;
			fe_byte_count = 1;
		} else if (fe_byte_count < 160)
			fe_byte_count++;
		return;
	}
	switch (state) {
	case STATE_GROUND:
		return;
	case STATE_FE:
		if (fe_byte_count < 160) {
			state = STATE_GROUND;
			return;
		}
		switch (samp) {
		case 0x6F:
			state = STATE_EFR_PRELIM;
			return;
		case 0xFF:
			state = STATE_FR;
			match_pos = 1;
			return;
		default:
			state = STATE_GROUND;
		}
		return;
	case STATE_EFR_PRELIM:
		switch (samp) {
		case 0x64:
			state = STATE_EFR_CLASSIC;
			match_pos = 2;
			return;
		case 0x66:
			state = STATE_EFR_AMR;
			match_pos = 2;
			return;
		default:
			state = STATE_GROUND;
		}
		return;
	case STATE_EFR_CLASSIC:
		if (samp == match_frame_efr_classic[match_pos])
			match_pos++;
		else {
			state = STATE_GROUND;
			return;
		}
		if (match_pos < 160)
			return;
		report_match("classic EFR", offset);
		state = STATE_GROUND;
		return;
	case STATE_EFR_AMR:
		if (samp == match_frame_amrefr_dec[match_pos])
			match_pos++;
		else {
			state = STATE_GROUND;
			return;
		}
		if (match_pos < 160)
			return;
		report_match("AMR-EFR", offset);
		state = STATE_GROUND;
		return;
	case STATE_FR:
		if (samp == match_frame_fr[match_pos])
			match_pos++;
		else {
			state = STATE_GROUND;
			return;
		}
		if (match_pos < 160)
			return;
		report_match("GSM-FR", offset);
		state = STATE_GROUND;
		return;
	default:
		state = STATE_GROUND;
	}
}

void
pcmu_catcher_process(inbuf)
	uint8_t *inbuf;
{
	unsigned n;

	for (n = 0; n < 160; n++)
		process_sample(inbuf[n], n);
}