Asterisk - The Open Source Telephony Project  18.5.0
gsm_decode.c
Go to the documentation of this file.
1 /*
2  * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3  * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
4  * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5  */
6 
7 /* $Header$ */
8 
9 #include "private.h"
10 
11 #include "gsm.h"
12 #include "proto.h"
13 
14 int gsm_decode P3((s, c, target), gsm s, gsm_byte * c, gsm_signal * target)
15 {
16  word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4];
17 
18 #ifdef WAV49
19  if (s->wav_fmt) {
20 
21  uword sr = 0;
22 
23  s->frame_index = !s->frame_index;
24  if (s->frame_index) {
25 
26  sr = *c++;
27  LARc[0] = sr & 0x3f; sr >>= 6;
28  sr |= (uword)*c++ << 2;
29  LARc[1] = sr & 0x3f; sr >>= 6;
30  sr |= (uword)*c++ << 4;
31  LARc[2] = sr & 0x1f; sr >>= 5;
32  LARc[3] = sr & 0x1f; sr >>= 5;
33  sr |= (uword)*c++ << 2;
34  LARc[4] = sr & 0xf; sr >>= 4;
35  LARc[5] = sr & 0xf; sr >>= 4;
36  sr |= (uword)*c++ << 2; /* 5 */
37  LARc[6] = sr & 0x7; sr >>= 3;
38  LARc[7] = sr & 0x7; sr >>= 3;
39  sr |= (uword)*c++ << 4;
40  Nc[0] = sr & 0x7f; sr >>= 7;
41  bc[0] = sr & 0x3; sr >>= 2;
42  Mc[0] = sr & 0x3; sr >>= 2;
43  sr |= (uword)*c++ << 1;
44  xmaxc[0] = sr & 0x3f; sr >>= 6;
45  xmc[0] = sr & 0x7; sr >>= 3;
46  sr = *c++;
47  xmc[1] = sr & 0x7; sr >>= 3;
48  xmc[2] = sr & 0x7; sr >>= 3;
49  sr |= (uword)*c++ << 2;
50  xmc[3] = sr & 0x7; sr >>= 3;
51  xmc[4] = sr & 0x7; sr >>= 3;
52  xmc[5] = sr & 0x7; sr >>= 3;
53  sr |= (uword)*c++ << 1; /* 10 */
54  xmc[6] = sr & 0x7; sr >>= 3;
55  xmc[7] = sr & 0x7; sr >>= 3;
56  xmc[8] = sr & 0x7; sr >>= 3;
57  sr = *c++;
58  xmc[9] = sr & 0x7; sr >>= 3;
59  xmc[10] = sr & 0x7; sr >>= 3;
60  sr |= (uword)*c++ << 2;
61  xmc[11] = sr & 0x7; sr >>= 3;
62  xmc[12] = sr & 0x7; sr >>= 3;
63  sr |= (uword)*c++ << 4;
64  Nc[1] = sr & 0x7f; sr >>= 7;
65  bc[1] = sr & 0x3; sr >>= 2;
66  Mc[1] = sr & 0x3; sr >>= 2;
67  sr |= (uword)*c++ << 1;
68  xmaxc[1] = sr & 0x3f; sr >>= 6;
69  xmc[13] = sr & 0x7; sr >>= 3;
70  sr = *c++; /* 15 */
71  xmc[14] = sr & 0x7; sr >>= 3;
72  xmc[15] = sr & 0x7; sr >>= 3;
73  sr |= (uword)*c++ << 2;
74  xmc[16] = sr & 0x7; sr >>= 3;
75  xmc[17] = sr & 0x7; sr >>= 3;
76  xmc[18] = sr & 0x7; sr >>= 3;
77  sr |= (uword)*c++ << 1;
78  xmc[19] = sr & 0x7; sr >>= 3;
79  xmc[20] = sr & 0x7; sr >>= 3;
80  xmc[21] = sr & 0x7; sr >>= 3;
81  sr = *c++;
82  xmc[22] = sr & 0x7; sr >>= 3;
83  xmc[23] = sr & 0x7; sr >>= 3;
84  sr |= (uword)*c++ << 2;
85  xmc[24] = sr & 0x7; sr >>= 3;
86  xmc[25] = sr & 0x7; sr >>= 3;
87  sr |= (uword)*c++ << 4; /* 20 */
88  Nc[2] = sr & 0x7f; sr >>= 7;
89  bc[2] = sr & 0x3; sr >>= 2;
90  Mc[2] = sr & 0x3; sr >>= 2;
91  sr |= (uword)*c++ << 1;
92  xmaxc[2] = sr & 0x3f; sr >>= 6;
93  xmc[26] = sr & 0x7; sr >>= 3;
94  sr = *c++;
95  xmc[27] = sr & 0x7; sr >>= 3;
96  xmc[28] = sr & 0x7; sr >>= 3;
97  sr |= (uword)*c++ << 2;
98  xmc[29] = sr & 0x7; sr >>= 3;
99  xmc[30] = sr & 0x7; sr >>= 3;
100  xmc[31] = sr & 0x7; sr >>= 3;
101  sr |= (uword)*c++ << 1;
102  xmc[32] = sr & 0x7; sr >>= 3;
103  xmc[33] = sr & 0x7; sr >>= 3;
104  xmc[34] = sr & 0x7; sr >>= 3;
105  sr = *c++; /* 25 */
106  xmc[35] = sr & 0x7; sr >>= 3;
107  xmc[36] = sr & 0x7; sr >>= 3;
108  sr |= (uword)*c++ << 2;
109  xmc[37] = sr & 0x7; sr >>= 3;
110  xmc[38] = sr & 0x7; sr >>= 3;
111  sr |= (uword)*c++ << 4;
112  Nc[3] = sr & 0x7f; sr >>= 7;
113  bc[3] = sr & 0x3; sr >>= 2;
114  Mc[3] = sr & 0x3; sr >>= 2;
115  sr |= (uword)*c++ << 1;
116  xmaxc[3] = sr & 0x3f; sr >>= 6;
117  xmc[39] = sr & 0x7; sr >>= 3;
118  sr = *c++;
119  xmc[40] = sr & 0x7; sr >>= 3;
120  xmc[41] = sr & 0x7; sr >>= 3;
121  sr |= (uword)*c++ << 2; /* 30 */
122  xmc[42] = sr & 0x7; sr >>= 3;
123  xmc[43] = sr & 0x7; sr >>= 3;
124  xmc[44] = sr & 0x7; sr >>= 3;
125  sr |= (uword)*c++ << 1;
126  xmc[45] = sr & 0x7; sr >>= 3;
127  xmc[46] = sr & 0x7; sr >>= 3;
128  xmc[47] = sr & 0x7; sr >>= 3;
129  sr = *c++;
130  xmc[48] = sr & 0x7; sr >>= 3;
131  xmc[49] = sr & 0x7; sr >>= 3;
132  sr |= (uword)*c++ << 2;
133  xmc[50] = sr & 0x7; sr >>= 3;
134  xmc[51] = sr & 0x7; sr >>= 3;
135 
136  s->frame_chain = sr & 0xf;
137  }
138  else {
139  sr = s->frame_chain;
140  sr |= (uword)*c++ << 4; /* 1 */
141  LARc[0] = sr & 0x3f; sr >>= 6;
142  LARc[1] = sr & 0x3f; sr >>= 6;
143  sr = *c++;
144  LARc[2] = sr & 0x1f; sr >>= 5;
145  sr |= (uword)*c++ << 3;
146  LARc[3] = sr & 0x1f; sr >>= 5;
147  LARc[4] = sr & 0xf; sr >>= 4;
148  sr |= (uword)*c++ << 2;
149  LARc[5] = sr & 0xf; sr >>= 4;
150  LARc[6] = sr & 0x7; sr >>= 3;
151  LARc[7] = sr & 0x7; sr >>= 3;
152  sr = *c++; /* 5 */
153  Nc[0] = sr & 0x7f; sr >>= 7;
154  sr |= (uword)*c++ << 1;
155  bc[0] = sr & 0x3; sr >>= 2;
156  Mc[0] = sr & 0x3; sr >>= 2;
157  sr |= (uword)*c++ << 5;
158  xmaxc[0] = sr & 0x3f; sr >>= 6;
159  xmc[0] = sr & 0x7; sr >>= 3;
160  xmc[1] = sr & 0x7; sr >>= 3;
161  sr |= (uword)*c++ << 1;
162  xmc[2] = sr & 0x7; sr >>= 3;
163  xmc[3] = sr & 0x7; sr >>= 3;
164  xmc[4] = sr & 0x7; sr >>= 3;
165  sr = *c++;
166  xmc[5] = sr & 0x7; sr >>= 3;
167  xmc[6] = sr & 0x7; sr >>= 3;
168  sr |= (uword)*c++ << 2; /* 10 */
169  xmc[7] = sr & 0x7; sr >>= 3;
170  xmc[8] = sr & 0x7; sr >>= 3;
171  xmc[9] = sr & 0x7; sr >>= 3;
172  sr |= (uword)*c++ << 1;
173  xmc[10] = sr & 0x7; sr >>= 3;
174  xmc[11] = sr & 0x7; sr >>= 3;
175  xmc[12] = sr & 0x7; sr >>= 3;
176  sr = *c++;
177  Nc[1] = sr & 0x7f; sr >>= 7;
178  sr |= (uword)*c++ << 1;
179  bc[1] = sr & 0x3; sr >>= 2;
180  Mc[1] = sr & 0x3; sr >>= 2;
181  sr |= (uword)*c++ << 5;
182  xmaxc[1] = sr & 0x3f; sr >>= 6;
183  xmc[13] = sr & 0x7; sr >>= 3;
184  xmc[14] = sr & 0x7; sr >>= 3;
185  sr |= (uword)*c++ << 1; /* 15 */
186  xmc[15] = sr & 0x7; sr >>= 3;
187  xmc[16] = sr & 0x7; sr >>= 3;
188  xmc[17] = sr & 0x7; sr >>= 3;
189  sr = *c++;
190  xmc[18] = sr & 0x7; sr >>= 3;
191  xmc[19] = sr & 0x7; sr >>= 3;
192  sr |= (uword)*c++ << 2;
193  xmc[20] = sr & 0x7; sr >>= 3;
194  xmc[21] = sr & 0x7; sr >>= 3;
195  xmc[22] = sr & 0x7; sr >>= 3;
196  sr |= (uword)*c++ << 1;
197  xmc[23] = sr & 0x7; sr >>= 3;
198  xmc[24] = sr & 0x7; sr >>= 3;
199  xmc[25] = sr & 0x7; sr >>= 3;
200  sr = *c++;
201  Nc[2] = sr & 0x7f; sr >>= 7;
202  sr |= (uword)*c++ << 1; /* 20 */
203  bc[2] = sr & 0x3; sr >>= 2;
204  Mc[2] = sr & 0x3; sr >>= 2;
205  sr |= (uword)*c++ << 5;
206  xmaxc[2] = sr & 0x3f; sr >>= 6;
207  xmc[26] = sr & 0x7; sr >>= 3;
208  xmc[27] = sr & 0x7; sr >>= 3;
209  sr |= (uword)*c++ << 1;
210  xmc[28] = sr & 0x7; sr >>= 3;
211  xmc[29] = sr & 0x7; sr >>= 3;
212  xmc[30] = sr & 0x7; sr >>= 3;
213  sr = *c++;
214  xmc[31] = sr & 0x7; sr >>= 3;
215  xmc[32] = sr & 0x7; sr >>= 3;
216  sr |= (uword)*c++ << 2;
217  xmc[33] = sr & 0x7; sr >>= 3;
218  xmc[34] = sr & 0x7; sr >>= 3;
219  xmc[35] = sr & 0x7; sr >>= 3;
220  sr |= (uword)*c++ << 1; /* 25 */
221  xmc[36] = sr & 0x7; sr >>= 3;
222  xmc[37] = sr & 0x7; sr >>= 3;
223  xmc[38] = sr & 0x7; sr >>= 3;
224  sr = *c++;
225  Nc[3] = sr & 0x7f; sr >>= 7;
226  sr |= (uword)*c++ << 1;
227  bc[3] = sr & 0x3; sr >>= 2;
228  Mc[3] = sr & 0x3; sr >>= 2;
229  sr |= (uword)*c++ << 5;
230  xmaxc[3] = sr & 0x3f; sr >>= 6;
231  xmc[39] = sr & 0x7; sr >>= 3;
232  xmc[40] = sr & 0x7; sr >>= 3;
233  sr |= (uword)*c++ << 1;
234  xmc[41] = sr & 0x7; sr >>= 3;
235  xmc[42] = sr & 0x7; sr >>= 3;
236  xmc[43] = sr & 0x7; sr >>= 3;
237  sr = *c++; /* 30 */
238  xmc[44] = sr & 0x7; sr >>= 3;
239  xmc[45] = sr & 0x7; sr >>= 3;
240  sr |= (uword)*c++ << 2;
241  xmc[46] = sr & 0x7; sr >>= 3;
242  xmc[47] = sr & 0x7; sr >>= 3;
243  xmc[48] = sr & 0x7; sr >>= 3;
244  sr |= (uword)*c++ << 1;
245  xmc[49] = sr & 0x7; sr >>= 3;
246  xmc[50] = sr & 0x7; sr >>= 3;
247  xmc[51] = sr & 0x7; sr >>= 3;
248  }
249  }
250  else
251 #endif
252  {
253  /* GSM_MAGIC = (*c >> 4) & 0xF; */
254 
255  if (((*c >> 4) & 0x0F) != GSM_MAGIC) return -1;
256 
257  LARc[0] = (*c++ & 0xF) << 2; /* 1 */
258  LARc[0] |= (*c >> 6) & 0x3;
259  LARc[1] = *c++ & 0x3F;
260  LARc[2] = (*c >> 3) & 0x1F;
261  LARc[3] = (*c++ & 0x7) << 2;
262  LARc[3] |= (*c >> 6) & 0x3;
263  LARc[4] = (*c >> 2) & 0xF;
264  LARc[5] = (*c++ & 0x3) << 2;
265  LARc[5] |= (*c >> 6) & 0x3;
266  LARc[6] = (*c >> 3) & 0x7;
267  LARc[7] = *c++ & 0x7;
268  Nc[0] = (*c >> 1) & 0x7F;
269  bc[0] = (*c++ & 0x1) << 1;
270  bc[0] |= (*c >> 7) & 0x1;
271  Mc[0] = (*c >> 5) & 0x3;
272  xmaxc[0] = (*c++ & 0x1F) << 1;
273  xmaxc[0] |= (*c >> 7) & 0x1;
274  xmc[0] = (*c >> 4) & 0x7;
275  xmc[1] = (*c >> 1) & 0x7;
276  xmc[2] = (*c++ & 0x1) << 2;
277  xmc[2] |= (*c >> 6) & 0x3;
278  xmc[3] = (*c >> 3) & 0x7;
279  xmc[4] = *c++ & 0x7;
280  xmc[5] = (*c >> 5) & 0x7;
281  xmc[6] = (*c >> 2) & 0x7;
282  xmc[7] = (*c++ & 0x3) << 1; /* 10 */
283  xmc[7] |= (*c >> 7) & 0x1;
284  xmc[8] = (*c >> 4) & 0x7;
285  xmc[9] = (*c >> 1) & 0x7;
286  xmc[10] = (*c++ & 0x1) << 2;
287  xmc[10] |= (*c >> 6) & 0x3;
288  xmc[11] = (*c >> 3) & 0x7;
289  xmc[12] = *c++ & 0x7;
290  Nc[1] = (*c >> 1) & 0x7F;
291  bc[1] = (*c++ & 0x1) << 1;
292  bc[1] |= (*c >> 7) & 0x1;
293  Mc[1] = (*c >> 5) & 0x3;
294  xmaxc[1] = (*c++ & 0x1F) << 1;
295  xmaxc[1] |= (*c >> 7) & 0x1;
296  xmc[13] = (*c >> 4) & 0x7;
297  xmc[14] = (*c >> 1) & 0x7;
298  xmc[15] = (*c++ & 0x1) << 2;
299  xmc[15] |= (*c >> 6) & 0x3;
300  xmc[16] = (*c >> 3) & 0x7;
301  xmc[17] = *c++ & 0x7;
302  xmc[18] = (*c >> 5) & 0x7;
303  xmc[19] = (*c >> 2) & 0x7;
304  xmc[20] = (*c++ & 0x3) << 1;
305  xmc[20] |= (*c >> 7) & 0x1;
306  xmc[21] = (*c >> 4) & 0x7;
307  xmc[22] = (*c >> 1) & 0x7;
308  xmc[23] = (*c++ & 0x1) << 2;
309  xmc[23] |= (*c >> 6) & 0x3;
310  xmc[24] = (*c >> 3) & 0x7;
311  xmc[25] = *c++ & 0x7;
312  Nc[2] = (*c >> 1) & 0x7F;
313  bc[2] = (*c++ & 0x1) << 1; /* 20 */
314  bc[2] |= (*c >> 7) & 0x1;
315  Mc[2] = (*c >> 5) & 0x3;
316  xmaxc[2] = (*c++ & 0x1F) << 1;
317  xmaxc[2] |= (*c >> 7) & 0x1;
318  xmc[26] = (*c >> 4) & 0x7;
319  xmc[27] = (*c >> 1) & 0x7;
320  xmc[28] = (*c++ & 0x1) << 2;
321  xmc[28] |= (*c >> 6) & 0x3;
322  xmc[29] = (*c >> 3) & 0x7;
323  xmc[30] = *c++ & 0x7;
324  xmc[31] = (*c >> 5) & 0x7;
325  xmc[32] = (*c >> 2) & 0x7;
326  xmc[33] = (*c++ & 0x3) << 1;
327  xmc[33] |= (*c >> 7) & 0x1;
328  xmc[34] = (*c >> 4) & 0x7;
329  xmc[35] = (*c >> 1) & 0x7;
330  xmc[36] = (*c++ & 0x1) << 2;
331  xmc[36] |= (*c >> 6) & 0x3;
332  xmc[37] = (*c >> 3) & 0x7;
333  xmc[38] = *c++ & 0x7;
334  Nc[3] = (*c >> 1) & 0x7F;
335  bc[3] = (*c++ & 0x1) << 1;
336  bc[3] |= (*c >> 7) & 0x1;
337  Mc[3] = (*c >> 5) & 0x3;
338  xmaxc[3] = (*c++ & 0x1F) << 1;
339  xmaxc[3] |= (*c >> 7) & 0x1;
340  xmc[39] = (*c >> 4) & 0x7;
341  xmc[40] = (*c >> 1) & 0x7;
342  xmc[41] = (*c++ & 0x1) << 2;
343  xmc[41] |= (*c >> 6) & 0x3;
344  xmc[42] = (*c >> 3) & 0x7;
345  xmc[43] = *c++ & 0x7; /* 30 */
346  xmc[44] = (*c >> 5) & 0x7;
347  xmc[45] = (*c >> 2) & 0x7;
348  xmc[46] = (*c++ & 0x3) << 1;
349  xmc[46] |= (*c >> 7) & 0x1;
350  xmc[47] = (*c >> 4) & 0x7;
351  xmc[48] = (*c >> 1) & 0x7;
352  xmc[49] = (*c++ & 0x1) << 2;
353  xmc[49] |= (*c >> 6) & 0x3;
354  xmc[50] = (*c >> 3) & 0x7;
355  xmc[51] = *c & 0x7; /* 33 */
356  }
357 
358  Gsm_Decoder(s, LARc, Nc, bc, Mc, xmaxc, xmc, target);
359 
360  return 0;
361 }
unsigned char frame_index
#define bc
#define Mc
#define xmc
unsigned char gsm_byte
Definition: gsm.h:41
static struct test_val c
#define GSM_MAGIC
Definition: gsm.h:44
int gsm_decode P3((s, c, target), gsm s, gsm_byte *c, gsm_signal *target)
Definition: gsm_decode.c:14
unsigned short uword
short gsm_signal
Definition: gsm.h:40
#define xmaxc
unsigned char frame_chain
#define Nc
#define LARc
short word