FreeCalypso > hg > gsm-net-reveng
view trau-decode/parse-amr.c @ 83:5e2ecfd45fc3 default tip
trau-parse: AMR 7k40 CRC
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 09 Feb 2025 03:22:47 +0000 |
parents | 31a8d34b71ed |
children |
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; { ubit_t crc_collect[59]; int crc1, crc2, crc3, crc4; bcopy(c_bits, crc_collect, 25); bcopy(d_bits + 34, crc_collect + 25, 20); bcopy(d_bits + 55, crc_collect + 45, 3); bcopy(d_bits + 60, crc_collect + 48, 6); bcopy(d_bits + 85, crc_collect + 54, 2); bcopy(d_bits + 88, crc_collect + 56, 3); crc1 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 59, d_bits + 92); bcopy(d_bits + 95, crc_collect, 3); bcopy(d_bits + 117, crc_collect + 3, 2); bcopy(d_bits + 120, crc_collect + 5, 3); crc2 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 8, d_bits + 124); bcopy(d_bits + 127, crc_collect, 6); bcopy(d_bits + 152, crc_collect + 6, 2); bcopy(d_bits + 155, crc_collect + 8, 3); crc3 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 11, d_bits + 159); bcopy(d_bits + 162, crc_collect, 3); bcopy(d_bits + 184, crc_collect + 3, 2); bcopy(d_bits + 187, crc_collect + 5, 2); crc4 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 7, d_bits + 191); printf(" CRC %s %s %s %s\n", crc1 ? "bad" : "good", crc2 ? "bad" : "good", crc3 ? "bad" : "good", crc4 ? "bad" : "good"); } static void decode_mode_5(c_bits, d_bits) ubit_t *c_bits, *d_bits; { ubit_t crc_collect[64]; int crc1, crc2, crc3, crc4; bcopy(c_bits, crc_collect, 25); bcopy(d_bits + 31, crc_collect + 25, 35); bcopy(d_bits + 83, crc_collect + 60, 2); crc_collect[62] = d_bits[87]; crc_collect[63] = d_bits[90]; crc1 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 64, d_bits + 92); bcopy(d_bits + 95, crc_collect, 4); bcopy(d_bits + 118, crc_collect + 4, 2); bcopy(d_bits + 122, crc_collect + 6, 4); crc2 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 10, d_bits + 127); bcopy(d_bits + 130, crc_collect, 8); bcopy(d_bits + 155, crc_collect + 8, 2); bcopy(d_bits + 159, crc_collect + 10, 4); crc3 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 14, d_bits + 164); bcopy(d_bits + 167, crc_collect, 4); bcopy(d_bits + 190, crc_collect + 4, 2); bcopy(d_bits + 194, crc_collect + 6, 4); crc4 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 10, d_bits + 199); printf(" CRC %s %s %s %s\n", crc1 ? "bad" : "good", crc2 ? "bad" : "good", crc3 ? "bad" : "good", crc4 ? "bad" : "good"); } static void decode_mode_6(c_bits, d_bits) ubit_t *c_bits, *d_bits; { ubit_t crc_collect[82]; int crc1, crc2, crc3, crc4; bcopy(c_bits, crc_collect, 25); bcopy(d_bits, crc_collect + 25, 45); bcopy(d_bits + 46, crc_collect + 70, 8); bcopy(d_bits + 85, crc_collect + 78, 2); bcopy(d_bits + 88, crc_collect + 80, 2); crc1 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 82, d_bits + 92); bcopy(d_bits + 95, crc_collect, 4); bcopy(d_bits + 131, crc_collect + 4, 2); bcopy(d_bits + 134, crc_collect + 6, 2); crc2 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 8, d_bits + 138); bcopy(d_bits + 141, crc_collect, 8); bcopy(d_bits + 180, crc_collect + 8, 2); bcopy(d_bits + 183, crc_collect + 10, 2); crc3 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 12, d_bits + 187); bcopy(d_bits + 190, crc_collect, 4); bcopy(d_bits + 226, crc_collect + 4, 2); bcopy(d_bits + 229, crc_collect + 6, 2); bcopy(d_bits + 233, crc_collect + 8, 20); crc4 = osmo_crc8gen_check_bits(&gsm0860_amr_crc3, crc_collect, 28, d_bits + 253); printf(" CRC %s %s %s %s\n", crc1 ? "bad" : "good", crc2 ? "bad" : "good", crc3 ? "bad" : "good", crc4 ? "bad" : "good"); } 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]); }