FreeCalypso > hg > freecalypso-tools
comparison miscutil/gsm0610.c @ 5:7eaa3307e5df
fc-fr2tch utility written, added under miscutil
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 14 Jun 2016 01:40:14 +0000 |
parents | rvinterf/asyncshell/gsm0610.c@e7502631a0f9 |
children | 08804864172a |
comparison
equal
deleted
inserted
replaced
4:971906d7763d | 5:7eaa3307e5df |
---|---|
1 /* | |
2 * TI's DSP has API words from which GSM 06.10 codec bits that have been | |
3 * demodulated from the downlink may be read, FreeCalypso firmware has | |
4 * an experimental feature by way of which these bits may be forwarded | |
5 * to the external host, and fc-shell provides a mechanism to record | |
6 * this downlink speech stream in a file. We would like to record these | |
7 * files in the libgsm format which the FLOSS world has adopted as the | |
8 * standard, so that they can be played with the SoX play command, | |
9 * for example. But the bit order is different between what TI's DSP | |
10 * gives us and libgsm, hence we need to reshuffle the bits here. | |
11 * | |
12 * Adapted from OsmocomBB. | |
13 */ | |
14 | |
15 #include <sys/types.h> | |
16 #include <string.h> | |
17 #include <strings.h> | |
18 | |
19 /* GSM FR - subjective importance bit ordering */ | |
20 /* This array encodes GSM 05.03 Table 2. | |
21 * It's also GSM 06.10 Table A.2.1a | |
22 * | |
23 * It converts between serial parameter output by the encoder and the | |
24 * order needed before channel encoding. | |
25 */ | |
26 const u_short gsm0610_bitorder[260] = { | |
27 0, /* LARc0:5 */ | |
28 47, /* Xmaxc0:5 */ | |
29 103, /* Xmaxc1:5 */ | |
30 159, /* Xmaxc2:5 */ | |
31 215, /* Xmaxc3:5 */ | |
32 1, /* LARc0:4 */ | |
33 6, /* LARc1:5 */ | |
34 12, /* LARc2:4 */ | |
35 2, /* LARc0:3 */ | |
36 7, /* LARc1:4 */ | |
37 13, /* LARc2:3 */ | |
38 17, /* LARc3:4 */ | |
39 36, /* Nc0:6 */ | |
40 92, /* Nc1:6 */ | |
41 148, /* Nc2:6 */ | |
42 204, /* Nc3:6 */ | |
43 48, /* Xmaxc0:4 */ | |
44 104, /* Xmaxc1:4 */ | |
45 160, /* Xmaxc2:4 */ | |
46 216, /* Xmaxc3:4 */ | |
47 8, /* LARc1:3 */ | |
48 22, /* LARc4:3 */ | |
49 26, /* LARc5:3 */ | |
50 37, /* Nc0:5 */ | |
51 93, /* Nc1:5 */ | |
52 149, /* Nc2:5 */ | |
53 205, /* Nc3:5 */ | |
54 38, /* Nc0:4 */ | |
55 94, /* Nc1:4 */ | |
56 150, /* Nc2:4 */ | |
57 206, /* Nc3:4 */ | |
58 39, /* Nc0:3 */ | |
59 95, /* Nc1:3 */ | |
60 151, /* Nc2:3 */ | |
61 207, /* Nc3:3 */ | |
62 40, /* Nc0:2 */ | |
63 96, /* Nc1:2 */ | |
64 152, /* Nc2:2 */ | |
65 208, /* Nc3:2 */ | |
66 49, /* Xmaxc0:3 */ | |
67 105, /* Xmaxc1:3 */ | |
68 161, /* Xmaxc2:3 */ | |
69 217, /* Xmaxc3:3 */ | |
70 3, /* LARc0:2 */ | |
71 18, /* LARc3:3 */ | |
72 30, /* LARc6:2 */ | |
73 41, /* Nc0:1 */ | |
74 97, /* Nc1:1 */ | |
75 153, /* Nc2:1 */ | |
76 209, /* Nc3:1 */ | |
77 23, /* LARc4:2 */ | |
78 27, /* LARc5:2 */ | |
79 43, /* bc0:1 */ | |
80 99, /* bc1:1 */ | |
81 155, /* bc2:1 */ | |
82 211, /* bc3:1 */ | |
83 42, /* Nc0:0 */ | |
84 98, /* Nc1:0 */ | |
85 154, /* Nc2:0 */ | |
86 210, /* Nc3:0 */ | |
87 45, /* Mc0:1 */ | |
88 101, /* Mc1:1 */ | |
89 157, /* Mc2:1 */ | |
90 213, /* Mc3:1 */ | |
91 4, /* LARc0:1 */ | |
92 9, /* LARc1:2 */ | |
93 14, /* LARc2:2 */ | |
94 33, /* LARc7:2 */ | |
95 19, /* LARc3:2 */ | |
96 24, /* LARc4:1 */ | |
97 31, /* LARc6:1 */ | |
98 44, /* bc0:0 */ | |
99 100, /* bc1:0 */ | |
100 156, /* bc2:0 */ | |
101 212, /* bc3:0 */ | |
102 50, /* Xmaxc0:2 */ | |
103 106, /* Xmaxc1:2 */ | |
104 162, /* Xmaxc2:2 */ | |
105 218, /* Xmaxc3:2 */ | |
106 53, /* xmc0_0:2 */ | |
107 56, /* xmc0_1:2 */ | |
108 59, /* xmc0_2:2 */ | |
109 62, /* xmc0_3:2 */ | |
110 65, /* xmc0_4:2 */ | |
111 68, /* xmc0_5:2 */ | |
112 71, /* xmc0_6:2 */ | |
113 74, /* xmc0_7:2 */ | |
114 77, /* xmc0_8:2 */ | |
115 80, /* xmc0_9:2 */ | |
116 83, /* xmc0_10:2 */ | |
117 86, /* xmc0_11:2 */ | |
118 89, /* xmc0_12:2 */ | |
119 109, /* xmc1_0:2 */ | |
120 112, /* xmc1_1:2 */ | |
121 115, /* xmc1_2:2 */ | |
122 118, /* xmc1_3:2 */ | |
123 121, /* xmc1_4:2 */ | |
124 124, /* xmc1_5:2 */ | |
125 127, /* xmc1_6:2 */ | |
126 130, /* xmc1_7:2 */ | |
127 133, /* xmc1_8:2 */ | |
128 136, /* xmc1_9:2 */ | |
129 139, /* xmc1_10:2 */ | |
130 142, /* xmc1_11:2 */ | |
131 145, /* xmc1_12:2 */ | |
132 165, /* xmc2_0:2 */ | |
133 168, /* xmc2_1:2 */ | |
134 171, /* xmc2_2:2 */ | |
135 174, /* xmc2_3:2 */ | |
136 177, /* xmc2_4:2 */ | |
137 180, /* xmc2_5:2 */ | |
138 183, /* xmc2_6:2 */ | |
139 186, /* xmc2_7:2 */ | |
140 189, /* xmc2_8:2 */ | |
141 192, /* xmc2_9:2 */ | |
142 195, /* xmc2_10:2 */ | |
143 198, /* xmc2_11:2 */ | |
144 201, /* xmc2_12:2 */ | |
145 221, /* xmc3_0:2 */ | |
146 224, /* xmc3_1:2 */ | |
147 227, /* xmc3_2:2 */ | |
148 230, /* xmc3_3:2 */ | |
149 233, /* xmc3_4:2 */ | |
150 236, /* xmc3_5:2 */ | |
151 239, /* xmc3_6:2 */ | |
152 242, /* xmc3_7:2 */ | |
153 245, /* xmc3_8:2 */ | |
154 248, /* xmc3_9:2 */ | |
155 251, /* xmc3_10:2 */ | |
156 254, /* xmc3_11:2 */ | |
157 257, /* xmc3_12:2 */ | |
158 46, /* Mc0:0 */ | |
159 102, /* Mc1:0 */ | |
160 158, /* Mc2:0 */ | |
161 214, /* Mc3:0 */ | |
162 51, /* Xmaxc0:1 */ | |
163 107, /* Xmaxc1:1 */ | |
164 163, /* Xmaxc2:1 */ | |
165 219, /* Xmaxc3:1 */ | |
166 54, /* xmc0_0:1 */ | |
167 57, /* xmc0_1:1 */ | |
168 60, /* xmc0_2:1 */ | |
169 63, /* xmc0_3:1 */ | |
170 66, /* xmc0_4:1 */ | |
171 69, /* xmc0_5:1 */ | |
172 72, /* xmc0_6:1 */ | |
173 75, /* xmc0_7:1 */ | |
174 78, /* xmc0_8:1 */ | |
175 81, /* xmc0_9:1 */ | |
176 84, /* xmc0_10:1 */ | |
177 87, /* xmc0_11:1 */ | |
178 90, /* xmc0_12:1 */ | |
179 110, /* xmc1_0:1 */ | |
180 113, /* xmc1_1:1 */ | |
181 116, /* xmc1_2:1 */ | |
182 119, /* xmc1_3:1 */ | |
183 122, /* xmc1_4:1 */ | |
184 125, /* xmc1_5:1 */ | |
185 128, /* xmc1_6:1 */ | |
186 131, /* xmc1_7:1 */ | |
187 134, /* xmc1_8:1 */ | |
188 137, /* xmc1_9:1 */ | |
189 140, /* xmc1_10:1 */ | |
190 143, /* xmc1_11:1 */ | |
191 146, /* xmc1_12:1 */ | |
192 166, /* xmc2_0:1 */ | |
193 169, /* xmc2_1:1 */ | |
194 172, /* xmc2_2:1 */ | |
195 175, /* xmc2_3:1 */ | |
196 178, /* xmc2_4:1 */ | |
197 181, /* xmc2_5:1 */ | |
198 184, /* xmc2_6:1 */ | |
199 187, /* xmc2_7:1 */ | |
200 190, /* xmc2_8:1 */ | |
201 193, /* xmc2_9:1 */ | |
202 196, /* xmc2_10:1 */ | |
203 199, /* xmc2_11:1 */ | |
204 202, /* xmc2_12:1 */ | |
205 222, /* xmc3_0:1 */ | |
206 225, /* xmc3_1:1 */ | |
207 228, /* xmc3_2:1 */ | |
208 231, /* xmc3_3:1 */ | |
209 234, /* xmc3_4:1 */ | |
210 237, /* xmc3_5:1 */ | |
211 240, /* xmc3_6:1 */ | |
212 243, /* xmc3_7:1 */ | |
213 246, /* xmc3_8:1 */ | |
214 249, /* xmc3_9:1 */ | |
215 252, /* xmc3_10:1 */ | |
216 255, /* xmc3_11:1 */ | |
217 258, /* xmc3_12:1 */ | |
218 5, /* LARc0:0 */ | |
219 10, /* LARc1:1 */ | |
220 15, /* LARc2:1 */ | |
221 28, /* LARc5:1 */ | |
222 32, /* LARc6:0 */ | |
223 34, /* LARc7:1 */ | |
224 35, /* LARc7:0 */ | |
225 16, /* LARc2:0 */ | |
226 20, /* LARc3:1 */ | |
227 21, /* LARc3:0 */ | |
228 25, /* LARc4:0 */ | |
229 52, /* Xmaxc0:0 */ | |
230 108, /* Xmaxc1:0 */ | |
231 164, /* Xmaxc2:0 */ | |
232 220, /* Xmaxc3:0 */ | |
233 55, /* xmc0_0:0 */ | |
234 58, /* xmc0_1:0 */ | |
235 61, /* xmc0_2:0 */ | |
236 64, /* xmc0_3:0 */ | |
237 67, /* xmc0_4:0 */ | |
238 70, /* xmc0_5:0 */ | |
239 73, /* xmc0_6:0 */ | |
240 76, /* xmc0_7:0 */ | |
241 79, /* xmc0_8:0 */ | |
242 82, /* xmc0_9:0 */ | |
243 85, /* xmc0_10:0 */ | |
244 88, /* xmc0_11:0 */ | |
245 91, /* xmc0_12:0 */ | |
246 111, /* xmc1_0:0 */ | |
247 114, /* xmc1_1:0 */ | |
248 117, /* xmc1_2:0 */ | |
249 120, /* xmc1_3:0 */ | |
250 123, /* xmc1_4:0 */ | |
251 126, /* xmc1_5:0 */ | |
252 129, /* xmc1_6:0 */ | |
253 132, /* xmc1_7:0 */ | |
254 135, /* xmc1_8:0 */ | |
255 138, /* xmc1_9:0 */ | |
256 141, /* xmc1_10:0 */ | |
257 144, /* xmc1_11:0 */ | |
258 147, /* xmc1_12:0 */ | |
259 167, /* xmc2_0:0 */ | |
260 170, /* xmc2_1:0 */ | |
261 173, /* xmc2_2:0 */ | |
262 176, /* xmc2_3:0 */ | |
263 179, /* xmc2_4:0 */ | |
264 182, /* xmc2_5:0 */ | |
265 185, /* xmc2_6:0 */ | |
266 188, /* xmc2_7:0 */ | |
267 191, /* xmc2_8:0 */ | |
268 194, /* xmc2_9:0 */ | |
269 197, /* xmc2_10:0 */ | |
270 200, /* xmc2_11:0 */ | |
271 203, /* xmc2_12:0 */ | |
272 223, /* xmc3_0:0 */ | |
273 226, /* xmc3_1:0 */ | |
274 229, /* xmc3_2:0 */ | |
275 232, /* xmc3_3:0 */ | |
276 235, /* xmc3_4:0 */ | |
277 238, /* xmc3_5:0 */ | |
278 241, /* xmc3_6:0 */ | |
279 244, /* xmc3_7:0 */ | |
280 247, /* xmc3_8:0 */ | |
281 250, /* xmc3_9:0 */ | |
282 253, /* xmc3_10:0 */ | |
283 256, /* xmc3_11:0 */ | |
284 259, /* xmc3_12:0 */ | |
285 11, /* LARc1:0 */ | |
286 29, /* LARc5:0 */ | |
287 }; | |
288 | |
289 static int | |
290 msb_get_bit(buf, bn) | |
291 u_char *buf; | |
292 { | |
293 int pos_byte = bn >> 3; | |
294 int pos_bit = 7 - (bn & 7); | |
295 | |
296 return (buf[pos_byte] >> pos_bit) & 1; | |
297 } | |
298 | |
299 static void | |
300 msb_set_bit(buf, bn, bit) | |
301 u_char *buf; | |
302 { | |
303 int pos_byte = bn >> 3; | |
304 int pos_bit = 7 - (bn & 7); | |
305 | |
306 buf[pos_byte] |= (bit << pos_bit); | |
307 } | |
308 | |
309 void | |
310 gsm0610_tidsp_to_libgsm(inbytes, outbytes) | |
311 u_char *inbytes, *outbytes; | |
312 { | |
313 int i, di, si; | |
314 | |
315 bzero(outbytes, 33); | |
316 outbytes[0] = 0xD0; | |
317 for (i = 0; i < 260; i++) { | |
318 di = gsm0610_bitorder[i]; | |
319 si = (i > 181) ? i + 4 : i; | |
320 msb_set_bit(outbytes, 4 + di, msb_get_bit(inbytes, si)); | |
321 } | |
322 } | |
323 | |
324 void | |
325 gsm0610_libgsm_to_tidsp(inbytes, outbytes) | |
326 u_char *inbytes, *outbytes; | |
327 { | |
328 int i, di, si; | |
329 | |
330 bzero(outbytes, 33); | |
331 for (i = 0; i < 260; i++) { | |
332 si = gsm0610_bitorder[i]; | |
333 di = (i > 181) ? i + 4 : i; | |
334 msb_set_bit(outbytes, di, msb_get_bit(inbytes, 4 + si)); | |
335 } | |
336 } |