FreeCalypso > hg > gsm-net-reveng
diff trau-decode/amr8-common.c @ 101:625be4b2922f
trau-decode: start framework for TRAU-AMR-8k decoding
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Thu, 20 Nov 2025 16:52:21 +0000 |
| parents | trau-decode/parse-amr.c@fa80ce9b076c |
| children | b59a61446c34 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trau-decode/amr8-common.c Thu Nov 20 16:52:21 2025 +0000 @@ -0,0 +1,214 @@ +/* + * Here we implement parsing/decoding of TRAU-AMR-8k frames per 3GPP TS 48.061, + * striving to decode and display as much as we can. + */ + +#include <stdio.h> +#include <stdint.h> +#include <stdbool.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 const uint8_t params_lsf_475_515[] = {8, 8, 7, 0}; +static const uint8_t params_lsf_59_67_74[] = {8, 9, 9, 0}; + +static const uint8_t params_475_sf1[] = {8, 7, 2, 8, 0}; +static const uint8_t params_475_sf3[] = {4, 7, 2, 8, 0}; +static const uint8_t params_475_sf24[] = {4, 7, 2, 0}; + +static const uint8_t params_515_sf1[] = {8, 7, 2, 6, 0}; +static const uint8_t params_515_sf234[] = {4, 7, 2, 6, 0}; + +static const uint8_t params_59_sf13[] = {8, 9, 2, 6, 0}; +static const uint8_t params_59_sf24[] = {4, 9, 2, 6, 0}; + +static const uint8_t params_67_sf13[] = {8, 11, 3, 7, 0}; +static const uint8_t params_67_sf24[] = {4, 11, 3, 7, 0}; + +static const uint8_t params_74_sf13[] = {8, 13, 4, 7, 0}; +static const uint8_t params_74_sf24[] = {5, 13, 4, 7, 0}; + +static const uint8_t params_sid[] = {3, 8, 9, 9, 6, 0}; + +static const ubit_t bit8_0[8] = { 0, }; + +/*!< check sync pattern for AMR No_Speech + low bit rate */ +static bool is_amr_low(const ubit_t *bits) +{ + int i; + + /* TS 08.61 Section 6.8.2.1.2 */ + if (memcmp(bits, bit8_0, sizeof(bit8_0))) + return false; + if (bits[8] != 1) + return false; + if (bits[16] != 1) + return false; + if (bits[24] != 0 || bits[25] != 1) + return false; + for (i = 32; i < 20 * 8; i += 8) { + if (bits[i] != 1) + return false; + } + return true; +} + +/*!< check sync pattern for AMR 6.7kBit/s */ +static bool is_amr_67(const ubit_t *bits) +{ + int i; + + /* TS 08.61 Section 6.8.2.1.3 */ + if (memcmp(bits, bit8_0, sizeof(bit8_0))) + return false; + if (bits[8] != 1) + return false; + if (bits[16] != 1) + return false; + if (bits[24] != 1) + return false; + if (bits[32] != 1) + return false; + if (bits[40] != 0) + return false; + for (i = 48; i < 20 * 8; i += 16) { + if (bits[i] != 1) + return false; + } + return true; +} + +/*!< check sync pattern for AMR 7.4kBit/s */ +static bool is_amr_74(const ubit_t *bits) +{ + if (bits[0] != 0 || bits[1] != 0 || bits[2] != 1) + return false; + if (bits[8] != 0) + return false; + if (bits[16] != 1) + return false; + if (bits[24] != 0) + return false; + + return true; +} + +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 +print_generic_params(bits, ltab) + ubit_t *bits; + uint8_t *ltab; +{ + uint8_t *tp; + unsigned n, p; + + for (tp = ltab; n = *tp; tp++) { + p = bits_to_num(bits, n); + bits += n; + printf(" %u", p); + } +} + +static void +print_speech_params(bits, ltab) + ubit_t *bits; + uint8_t *ltab; +{ + fputs(" ", stdout); + print_generic_params(bits, ltab); + putchar('\n'); +} + +static void +check_spare_bits(bits, nbits, desc) + ubit_t *bits; + unsigned nbits; + char *desc; +{ + while (nbits) { + if (*bits == 0) + break; + bits++; + nbits--; + } + if (!nbits) + return; + printf(" Extra data in bits %s\n", desc); +} + +static void +handle_amr8_low(frame_bits) + ubit_t *frame_bits; +{ + puts(" TRAU-AMR-8k Low format, decoding to be implemented"); +} + +static void +handle_amr8_6k7(frame_bits) + ubit_t *frame_bits; +{ + puts(" TRAU-AMR-8k MR67 format, decoding to be implemented"); +} + +static void +handle_amr8_7k4(frame_bits) + ubit_t *frame_bits; +{ + puts(" TRAU-AMR-8k MR74 format, decoding to be implemented"); +} + +void +print_amr8_frame(frame_bits) + ubit_t *frame_bits; +{ + if (is_amr_low(frame_bits)) + handle_amr8_low(frame_bits); + else if (is_amr_67(frame_bits)) + handle_amr8_6k7(frame_bits); + else if (is_amr_74(frame_bits)) + handle_amr8_7k4(frame_bits); + else + puts(" Unrecognized format by sync pattern"); +}
