view trau-decode/parse-amr.c @ 80:58dfdb2fc6df

trau-parse: AMR 12k2 CRC
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Feb 2025 18:26:48 +0000
parents 8c1f20e845a1
children 9eff2af191d5
line wrap: on
line source

/*
 * Here we implement parsing/decoding of AMR frames as in 3GPP TS 48.060
 * section 5.5.1.2, striving to decode and display as much as we can.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "osmo_bits.h"

/*
 * AMR TRAU parity (same as EFR)
 *
 * g(x) = x^3 + x^1 + 1
 */
static const struct osmo_crc8gen_code gsm0860_amr_crc3 = {
	.bits = 3,
	.poly = 0x3,
	.init = 0x0,
	.remainder = 0x7,
};

static int saved_mode, saved_mode_valid;

static char *fclass_names[4] = {
	"No_Speech", "Speech_Bad", "Speech_Degraded", "Speech_Good"
};

static char *nospch_class_names[8] = {
	"No_Data", "invalid", "invalid", "invalid",
	"Sid_Bad", "Sid_Update", "Onset", "Sid_First"
};

static unsigned
bits_to_num(bits, nbits)
	ubit_t *bits;
	unsigned nbits;
{
	unsigned accum;
	unsigned n;

	accum = 0;
	for (n = 0; n < nbits; n++) {
		accum <<= 1;
		if (*bits)
			accum |= 1;
		bits++;
	}
	return accum;
}

static void
decode_mode_0(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_1(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_2(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_3(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_4(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_5(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_6(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
}

static void
decode_mode_7(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
	ubit_t crc_collect[69];
	int crc1, crc2, crc3, crc4;

	bcopy(c_bits, crc_collect, 25);
	bcopy(d_bits, crc_collect + 25, 29);
	bcopy(d_bits + 38, crc_collect + 54, 12);
	bcopy(d_bits + 86, crc_collect + 66, 3);
	crc1 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 69,
					d_bits + 91);
	bcopy(d_bits + 94, crc_collect, 9);
	bcopy(d_bits + 139, crc_collect + 9, 3);
	crc2 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 12,
					d_bits + 144);
	bcopy(d_bits + 147, crc_collect, 12);
	bcopy(d_bits + 195, crc_collect + 12, 3);
	crc3 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 15,
					d_bits + 200);
	bcopy(d_bits + 203, crc_collect, 5);
	bcopy(d_bits + 209, crc_collect + 5, 3);
	bcopy(d_bits + 248, crc_collect + 8, 3);
	crc4 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 11,
					d_bits + 253);
	printf("    CRC %s %s %s %s\n",
		crc1 ? "bad" : "good", crc2 ? "bad" : "good",
		crc3 ? "bad" : "good", crc4 ? "bad" : "good");
}

static void (*per_mode_decode[8])() = {
	decode_mode_0,
	decode_mode_1,
	decode_mode_2,
	decode_mode_3,
	decode_mode_4,
	decode_mode_5,
	decode_mode_6,
	decode_mode_7
};

static void
decode_speech_frame(c_bits, d_bits, cm_hdr)
	ubit_t *c_bits, *d_bits;
	unsigned cm_hdr;
{
	unsigned mode;

	if (c_bits[11]) {
		if (!saved_mode_valid) {
			printf("  Mode unknown, cannot decode\n");
			return;
		}
		mode = saved_mode;
		saved_mode_valid = 0;
	} else {
		mode = cm_hdr;
		saved_mode = mode;
		saved_mode_valid = 1;
	}
	printf("  Decoding per speech mode %u\n", mode);
	per_mode_decode[mode](c_bits, d_bits);
}

static void
decode_nospeech_frame(c_bits, d_bits)
	ubit_t *c_bits, *d_bits;
{
	ubit_t crc_collect[25 + 61];
	int crc_stat;

	printf("  No_Spch_Class=%u%u%u (%s) CMI=%u CMR=%u\n",
		d_bits[31], d_bits[32], d_bits[33],
		nospch_class_names[bits_to_num(d_bits + 31, 3)],
		bits_to_num(d_bits + 34, 3), bits_to_num(d_bits + 37, 3));
	printf("  PAB=%u TAE=%u%u\n", d_bits[40], d_bits[41], d_bits[42]);
	bcopy(c_bits, crc_collect, 25);
	bcopy(d_bits + 31, crc_collect + 25, 61);
	crc_stat = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect,
					   25 + 61, d_bits + 92);
	printf("  CRC %s\n", crc_stat ? "bad" : "good");
	if (crc_stat) {
		saved_mode_valid = 0;
	} else {
		saved_mode = bits_to_num(d_bits + 34, 3);
		saved_mode_valid = 1;
	}
}

void
print_amr_frame(frame_bits)
	uint8_t *frame_bits;
{
	ubit_t c_bits[25], d_bits[256];
	uint8_t fclass, cm_hdr;

	bcopy(frame_bits + 17, c_bits, 15);
	bcopy(frame_bits + 33, c_bits + 15, 10);
	bcopy(frame_bits + 43, d_bits, 5);
	bcopy(frame_bits + 49, d_bits + 5, 15);
	bcopy(frame_bits + 65, d_bits + 20, 15);
	bcopy(frame_bits + 81, d_bits + 35, 15);
	bcopy(frame_bits + 97, d_bits + 50, 15);
	bcopy(frame_bits + 113, d_bits + 65, 15);
	bcopy(frame_bits + 129, d_bits + 80, 15);
	bcopy(frame_bits + 145, d_bits + 95, 15);
	bcopy(frame_bits + 161, d_bits + 110, 15);
	bcopy(frame_bits + 177, d_bits + 125, 15);
	bcopy(frame_bits + 193, d_bits + 140, 15);
	bcopy(frame_bits + 209, d_bits + 155, 15);
	bcopy(frame_bits + 225, d_bits + 170, 15);
	bcopy(frame_bits + 241, d_bits + 185, 15);
	bcopy(frame_bits + 257, d_bits + 200, 15);
	bcopy(frame_bits + 273, d_bits + 215, 15);
	bcopy(frame_bits + 289, d_bits + 230, 15);
	bcopy(frame_bits + 305, d_bits + 245, 11);

	printf("  C6-C11: %u\n", bits_to_num(c_bits + 5, 6));
	printf("  RIF=%u C13=%u Config_Prot=%u%u%u Msg_No=%u%u\n",
		c_bits[11], c_bits[12], c_bits[13], c_bits[14], c_bits[15],
		c_bits[16], c_bits[17]);
	fclass = bits_to_num(c_bits + 20, 2);
	cm_hdr = bits_to_num(c_bits + 22, 3);
	printf("  DTXd=%u TFOE=%u FClass=%u%u (%s) %s=%u\n",
		c_bits[18], c_bits[19], c_bits[20], c_bits[21],
		fclass_names[fclass], c_bits[11] ? "CMR" : "CMI", cm_hdr);
	if (fclass)
		decode_speech_frame(c_bits, d_bits, cm_hdr);
	else
		decode_nospeech_frame(c_bits, d_bits);
	printf("  T1=%u T2=%u T3=%u T4=%u\n", frame_bits[316],
		frame_bits[317], frame_bits[318], frame_bits[319]);
}