comparison 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
comparison
equal deleted inserted replaced
100:a1868d31ce7f 101:625be4b2922f
1 /*
2 * Here we implement parsing/decoding of TRAU-AMR-8k frames per 3GPP TS 48.061,
3 * striving to decode and display as much as we can.
4 */
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include "osmo_bits.h"
13
14 /*
15 * AMR TRAU parity (same as EFR)
16 *
17 * g(x) = x^3 + x^1 + 1
18 */
19 static const struct osmo_crc8gen_code gsm0860_amr_crc3 = {
20 .bits = 3,
21 .poly = 0x3,
22 .init = 0x0,
23 .remainder = 0x7,
24 };
25
26 static int saved_mode, saved_mode_valid;
27
28 static char *fclass_names[4] = {
29 "No_Speech", "Speech_Bad", "Speech_Degraded", "Speech_Good"
30 };
31
32 static char *nospch_class_names[8] = {
33 "No_Data", "invalid", "invalid", "invalid",
34 "Sid_Bad", "Sid_Update", "Onset", "Sid_First"
35 };
36
37 static const uint8_t params_lsf_475_515[] = {8, 8, 7, 0};
38 static const uint8_t params_lsf_59_67_74[] = {8, 9, 9, 0};
39
40 static const uint8_t params_475_sf1[] = {8, 7, 2, 8, 0};
41 static const uint8_t params_475_sf3[] = {4, 7, 2, 8, 0};
42 static const uint8_t params_475_sf24[] = {4, 7, 2, 0};
43
44 static const uint8_t params_515_sf1[] = {8, 7, 2, 6, 0};
45 static const uint8_t params_515_sf234[] = {4, 7, 2, 6, 0};
46
47 static const uint8_t params_59_sf13[] = {8, 9, 2, 6, 0};
48 static const uint8_t params_59_sf24[] = {4, 9, 2, 6, 0};
49
50 static const uint8_t params_67_sf13[] = {8, 11, 3, 7, 0};
51 static const uint8_t params_67_sf24[] = {4, 11, 3, 7, 0};
52
53 static const uint8_t params_74_sf13[] = {8, 13, 4, 7, 0};
54 static const uint8_t params_74_sf24[] = {5, 13, 4, 7, 0};
55
56 static const uint8_t params_sid[] = {3, 8, 9, 9, 6, 0};
57
58 static const ubit_t bit8_0[8] = { 0, };
59
60 /*!< check sync pattern for AMR No_Speech + low bit rate */
61 static bool is_amr_low(const ubit_t *bits)
62 {
63 int i;
64
65 /* TS 08.61 Section 6.8.2.1.2 */
66 if (memcmp(bits, bit8_0, sizeof(bit8_0)))
67 return false;
68 if (bits[8] != 1)
69 return false;
70 if (bits[16] != 1)
71 return false;
72 if (bits[24] != 0 || bits[25] != 1)
73 return false;
74 for (i = 32; i < 20 * 8; i += 8) {
75 if (bits[i] != 1)
76 return false;
77 }
78 return true;
79 }
80
81 /*!< check sync pattern for AMR 6.7kBit/s */
82 static bool is_amr_67(const ubit_t *bits)
83 {
84 int i;
85
86 /* TS 08.61 Section 6.8.2.1.3 */
87 if (memcmp(bits, bit8_0, sizeof(bit8_0)))
88 return false;
89 if (bits[8] != 1)
90 return false;
91 if (bits[16] != 1)
92 return false;
93 if (bits[24] != 1)
94 return false;
95 if (bits[32] != 1)
96 return false;
97 if (bits[40] != 0)
98 return false;
99 for (i = 48; i < 20 * 8; i += 16) {
100 if (bits[i] != 1)
101 return false;
102 }
103 return true;
104 }
105
106 /*!< check sync pattern for AMR 7.4kBit/s */
107 static bool is_amr_74(const ubit_t *bits)
108 {
109 if (bits[0] != 0 || bits[1] != 0 || bits[2] != 1)
110 return false;
111 if (bits[8] != 0)
112 return false;
113 if (bits[16] != 1)
114 return false;
115 if (bits[24] != 0)
116 return false;
117
118 return true;
119 }
120
121 static unsigned
122 bits_to_num(bits, nbits)
123 ubit_t *bits;
124 unsigned nbits;
125 {
126 unsigned accum;
127 unsigned n;
128
129 accum = 0;
130 for (n = 0; n < nbits; n++) {
131 accum <<= 1;
132 if (*bits)
133 accum |= 1;
134 bits++;
135 }
136 return accum;
137 }
138
139 static void
140 print_generic_params(bits, ltab)
141 ubit_t *bits;
142 uint8_t *ltab;
143 {
144 uint8_t *tp;
145 unsigned n, p;
146
147 for (tp = ltab; n = *tp; tp++) {
148 p = bits_to_num(bits, n);
149 bits += n;
150 printf(" %u", p);
151 }
152 }
153
154 static void
155 print_speech_params(bits, ltab)
156 ubit_t *bits;
157 uint8_t *ltab;
158 {
159 fputs(" ", stdout);
160 print_generic_params(bits, ltab);
161 putchar('\n');
162 }
163
164 static void
165 check_spare_bits(bits, nbits, desc)
166 ubit_t *bits;
167 unsigned nbits;
168 char *desc;
169 {
170 while (nbits) {
171 if (*bits == 0)
172 break;
173 bits++;
174 nbits--;
175 }
176 if (!nbits)
177 return;
178 printf(" Extra data in bits %s\n", desc);
179 }
180
181 static void
182 handle_amr8_low(frame_bits)
183 ubit_t *frame_bits;
184 {
185 puts(" TRAU-AMR-8k Low format, decoding to be implemented");
186 }
187
188 static void
189 handle_amr8_6k7(frame_bits)
190 ubit_t *frame_bits;
191 {
192 puts(" TRAU-AMR-8k MR67 format, decoding to be implemented");
193 }
194
195 static void
196 handle_amr8_7k4(frame_bits)
197 ubit_t *frame_bits;
198 {
199 puts(" TRAU-AMR-8k MR74 format, decoding to be implemented");
200 }
201
202 void
203 print_amr8_frame(frame_bits)
204 ubit_t *frame_bits;
205 {
206 if (is_amr_low(frame_bits))
207 handle_amr8_low(frame_bits);
208 else if (is_amr_67(frame_bits))
209 handle_amr8_6k7(frame_bits);
210 else if (is_amr_74(frame_bits))
211 handle_amr8_7k4(frame_bits);
212 else
213 puts(" Unrecognized format by sync pattern");
214 }