summaryrefslogtreecommitdiffstats
path: root/src/coding/gsm0503_interleaving.c
blob: d5008d0753e597886f5c9ee4c8e767eca0e40391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
 * (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
 * (C) 2016 by Tom Tsou <tom.tsou@ettus.com>
 * (C) 2017 by Hrald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdint.h>
#include <string.h>

#include <osmocom/core/bits.h>
#include <osmocom/coding/gsm0503_tables.h>
#include <osmocom/coding/gsm0503_interleaving.h>

/*! \addtogroup interleaving
 *  @{
 * GSM TS 05.03 interleaving
 *
 * This module contains interleaving / de-interleaving routines for
 * various channel types, as defined in 3GPP TS 05.03 / 45.003.
 *
 * GSM xCCH interleaving and burst mapping:
 *
 * Interleaving:
 *
 * Given 456 coded input bits, form 4 blocks of 114 bits:
 *
 *      i(B, j) = c(n, k)       k = 0, ..., 455
 *                              n = 0, ..., N, N + 1, ...
 *                              B = B_0 + 4n + (k mod 4)
 *                              j = 2(49k mod 57) + ((k mod 8) div 4)
 *
 * Mapping on Burst:
 *
 *      e(B, j) = i(B, j)
 *      e(B, 59 + j) = i(B, 57 + j)     j = 0, ..., 56
 *      e(B, 57) = h_l(B)
 *      e(B, 58) = h_n(B)
 *
 * Where hl(B) and hn(B) are bits in burst B indicating flags.
 *
 * GSM TCH HR/AHS interleaving and burst mapping:
 *
 * Interleaving:
 *
 * Given 288 coded input bits, form 4 blocks of 114 bits,
 * where even bits of the first 2 blocks and odd bits of the last 2 blocks
 * are used:
 *
 *      i(B, j) = c(n, k)       k = 0, ..., 227
 *                              n = 0, ..., N, N + 1, ...
 *                              B = B_0 + 2n + b
 *                              j, b = table[k];
 *
 * Mapping on Burst:
 *
 *      e(B, j) = i(B, j)
 *      e(B, 59 + j) = i(B, 57 + j)     j = 0, ..., 56
 *      e(B, 57) = h_l(B)
 *      e(B, 58) = h_n(B)
 *
 * Where hl(B) and hn(B) are bits in burst B indicating flags.
 *
 * \file gsm0503_interleaving.c */

/*! De-Interleave burst bits according to TS 05.03 4.1.4
 *  \param[out] cB caller-allocated output buffer for 456 soft coded bits
 *  \param[in] iB 456 soft input bits */
void gsm0503_xcch_deinterleave(sbit_t *cB, const sbit_t *iB)
{
	int j, k, B;

	for (k = 0; k < 456; k++) {
		B = k & 3;
		j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
		cB[k] = iB[B * 114 + j];
	}
}

/*! Interleave burst bits according to TS 05.03 4.1.4
 *  \param[out] iB caller-allocated output buffer for 456 soft interleaved bits
 *  \param[in] cB 456 soft input coded bits */
void gsm0503_xcch_interleave(const ubit_t *cB, ubit_t *iB)
{
	int j, k, B;

	for (k = 0; k < 456; k++) {
		B = k & 3;
		j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
		iB[B * 114 + j] = cB[k];
	}
}

/*! De-Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.1.5
 *  \param[out] u caller-allocated output buffer for 12 soft coded bits
 *  \param[out] hc caller-allocated output buffer for 68 soft coded bits
 *  \param[out] dc caller-allocated output buffer for 372 soft coded bits
 *  \param[in] iB 452 interleaved soft input bits */
void gsm0503_mcs1_dl_deinterleave(sbit_t *u, sbit_t *hc,
	sbit_t *dc, const sbit_t *iB)
{
	int k;
	sbit_t c[452];
	sbit_t cp[456];

	gsm0503_xcch_deinterleave(cp, iB);

	for (k = 0; k < 25; k++)
		c[k] = cp[k];
	for (k = 26; k < 82; k++)
		c[k - 1] = cp[k];
	for (k = 83; k < 139; k++)
		c[k - 2] = cp[k];
	for (k = 140; k < 424; k++)
		c[k - 3] = cp[k];
	for (k = 425; k < 456; k++)
		c[k - 4] = cp[k];

	if (u) {
		for (k = 0; k < 12; k++)
			u[k] = c[k];
	}

	if (hc) {
		for (k = 12; k < 80; k++)
			hc[k - 12] = c[k];
	}

	if (dc) {
		for (k = 80; k < 452; k++)
			dc[k - 80] = c[k];
	}
}

/*! Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.1.5
 *  \param[in] up 12 input soft coded bits (usf)
 *  \param[in] hc 68 input soft coded bits (header)
 *  \param[in] dc 372 input soft bits (data)
 *  \param[out] iB 456 interleaved soft output bits */
void gsm0503_mcs1_dl_interleave(const ubit_t *up, const ubit_t *hc,
	const ubit_t *dc, ubit_t *iB)
{
	int k;
	ubit_t c[452];
	ubit_t cp[456];

	for (k = 0; k < 12; k++)
		c[k] = up[k];
	for (k = 12; k < 80; k++)
		c[k] = hc[k - 12];
	for (k = 80; k < 452; k++)
		c[k] = dc[k - 80];

	for (k = 0; k < 25; k++)
		cp[k] = c[k];
	for (k = 26; k < 82; k++)
		cp[k] = c[k - 1];
	for (k = 83; k < 139; k++)
		cp[k] = c[k - 2];
	for (k = 140; k < 424; k++)
		cp[k] = c[k - 3];
	for (k = 425; k < 456; k++)
		cp[k] = c[k - 4];

	cp[25] = 0;
	cp[82] = 0;
	cp[139] = 0;
	cp[424] = 0;

	gsm0503_xcch_interleave(cp, iB);
}

/*! Interleave MCS1 UL burst bits according to TS 05.03 5.1.5.2.4
 *  \param[out] hc caller-allocated output buffer for 80 soft coded header bits
 *  \param[out] dc caller-allocated output buffer for 372 soft coded data bits
 *  \param[in] iB 456 interleaved soft input bits */
void gsm0503_mcs1_ul_deinterleave(sbit_t *hc, sbit_t *dc, const sbit_t *iB)
{
	int k;
	sbit_t c[452];
	sbit_t cp[456];

	gsm0503_xcch_deinterleave(cp, iB);

	for (k = 0; k < 25; k++)
		c[k] = cp[k];
	for (k = 26; k < 82; k++)
		c[k - 1] = cp[k];
	for (k = 83; k < 139; k++)
		c[k - 2] = cp[k];
	for (k = 140; k < 424; k++)
		c[k - 3] = cp[k];
	for (k = 425; k < 456; k++)
		c[k - 4] = cp[k];

	if (hc) {
		for (k = 0; k < 80; k++)
			hc[k] = c[k];
	}

	if (dc) {
		for (k = 80; k < 452; k++)
			dc[k - 80] = c[k];
	}
}

/*! Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.2.4
 *  \param[in] hc 80 input coded bits (header)
 *  \param[in] dc 372 input bits (data)
 *  \param[out] iB 456 interleaved output bits */
void gsm0503_mcs1_ul_interleave(const ubit_t *hc, const ubit_t *dc, ubit_t *iB)
{
	int k;
	ubit_t c[452];
	ubit_t cp[456];

	for (k = 0; k < 80; k++)
		c[k] = hc[k];
	for (k = 80; k < 452; k++)
		c[k] = dc[k - 80];

	for (k = 0; k < 25; k++)
		cp[k] = c[k];
	for (k = 26; k < 82; k++)
		cp[k] = c[k - 1];
	for (k = 83; k < 139; k++)
		cp[k] = c[k - 2];
	for (k = 140; k < 424; k++)
		cp[k] = c[k - 3];
	for (k = 425; k < 456; k++)
		cp[k] = c[k - 4];

	cp[25] = 0;
	cp[82] = 0;
	cp[139] = 0;
	cp[424] = 0;

	gsm0503_xcch_interleave(cp, iB);
}

/*! Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.2.4
 *  \param[in] hc 136 soft coded header input bits
 *  \param[in] dc 1248 soft coded data input bits
 *  \param[out] hi 136 interleaved header output bits
 *  \param[out] di 1248 interleaved data output bits */
void gsm0503_mcs5_ul_interleave(const ubit_t *hc, const ubit_t *dc,
	ubit_t *hi, ubit_t *di)
{
	int j, k;

	/* Header */
	for (k = 0; k < 136; k++) {
		j = 34 * (k % 4) + 2 * (11 * k % 17) + k % 8 / 4;
		hi[j] = hc[k];
	}

	/* Data */
	for (k = 0; k < 1248; k++) {
		j = gsm0503_interleave_mcs5[k];
		di[j] = dc[k];
	}
}

/*! De-Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.2.4
 *  \param[out] hc caller-allocated output buffer for 136 soft coded header bits
 *  \param[out] dc caller-allocated output buffer for 1248 soft coded data bits
 *  \param[in] iB interleaved soft input bits */
void gsm0503_mcs5_ul_deinterleave(sbit_t *hc, sbit_t *dc,
	const sbit_t *hi, const sbit_t *di)
{
	int j, k;

	/* Header */
	if (hc) {
		for (k = 0; k < 136; k++) {
			j = 34 * (k % 4) + 2 * (11 * k % 17) + k % 8 / 4;
			hc[k] = hi[j];
		}
	}

	/* Data */
	if (dc) {
		for (k = 0; k < 1248; k++) {
			j = gsm0503_interleave_mcs5[k];
			dc[k] = di[j];
		}
	}
}

/*! Interleave MCS5 DL burst bits according to TS 05.03 5.1.9.1.5
 *  \param[in] hc 100 soft coded header input bits
 *  \param[in] dc 1248 soft coded data input bits
 *  \param[out] hi 100 interleaved header output bits
 *  \param[out] di 1248 interleaved data output bits */
void gsm0503_mcs5_dl_interleave(const ubit_t *hc, const ubit_t *dc,
	ubit_t *hi, ubit_t *di)
{
	int j, k;

	/* Header */
	for (k = 0; k < 100; k++) {
		j = 25 * (k % 4) + ((17 * k) % 25);
		hi[j] = hc[k];
	}

	/* Data */
	for (k = 0; k < 1248; k++) {
		j = gsm0503_interleave_mcs5[k];
		di[j] = dc[k];
	}
}

/*! De-Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.1.5
 *  \param[out] hc caller-allocated output buffer for 100 soft coded header bits
 *  \param[out] dc caller-allocated output buffer for 1248 soft coded data bits
 *  \param[in] iB interleaved soft input bits */
void gsm0503_mcs5_dl_deinterleave(sbit_t *hc, sbit_t *dc,
	const sbit_t *hi, const sbit_t *di)
{
	int j, k;

	/* Header */
	if (hc) {
		for (k = 0; k < 100; k++) {
			j = 25 * (k % 4) + ((17 * k) % 25);
			hc[k] = hi[j];
		}
	}

	/* Data */
	if (dc) {
		for (k = 0; k < 1248; k++) {
			j = gsm0503_interleave_mcs5[k];
			dc[k] = di[j];
		}
	}
}

/*! Interleave MCS7 DL burst bits according to TS 05.03 5.1.11.1.5
 *  \param[in] hc 124 soft coded header input bits
 *  \param[in] c1 612 soft coded data input bits
 *  \param[in] c2 612 soft coded data input bits
 *  \param[out] hi 124 interleaved header output bits
 *  \param[out] di 1224 interleaved data output bits */
void gsm0503_mcs7_dl_interleave(const ubit_t *hc, const ubit_t *c1,
	const ubit_t *c2, ubit_t *hi, ubit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	for (k = 0; k < 124; k++) {
		j = 31 * (k % 4) + ((17 * k) % 31);
		hi[j] = hc[k];
	}

	memcpy(&dc[0], c1, 612);
	memcpy(&dc[612], c2, 612);

	/* Data */
	for (k = 0; k < 1224; k++) {
		j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
			(k + 2 - k / 408) % 3;
		di[j] = dc[k];
	}
}

/*! De-Interleave MCS7 DL burst bits according to TS 05.03 5.1.11.1.5
 *  \param[out] hc caller-allocated output buffer for 124 soft coded header bits
 *  \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
 *  \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
 *  \param[in] hi interleaved soft input header bits
 *  \param[in] di interleaved soft input data bits */
void gsm0503_mcs7_dl_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
	const sbit_t *hi, const sbit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	if (hc) {
		for (k = 0; k < 124; k++) {
			j = 31 * (k % 4) + ((17 * k) % 31);
			hc[k] = hi[j];
		}
	}

	/* Data */
	if (c1 && c2) {
		for (k = 0; k < 1224; k++) {
			j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
	(k + 2 - k / 408) % 3;
			dc[k] = di[j];
		}

		memcpy(c1, &dc[0], 612);
		memcpy(c2, &dc[612], 612);
	}
}

/*! Interleave MCS7 UL burst bits according to TS 05.03 5.1.11.2.4
 *  \param[in] hc 124 soft coded header input bits
 *  \param[in] c1 612 soft coded data input bits
 *  \param[in] c2 612 soft coded data input bits
 *  \param[out] hi 124 interleaved header output bits
 *  \param[out] di 1224 interleaved data output bits */
void gsm0503_mcs7_ul_interleave(const ubit_t *hc, const ubit_t *c1,
	const ubit_t *c2, ubit_t *hi, ubit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	for (k = 0; k < 160; k++) {
		j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
		hi[j] = hc[k];
	}

	memcpy(&dc[0], c1, 612);
	memcpy(&dc[612], c2, 612);

	/* Data */
	for (k = 0; k < 1224; k++) {
		j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
			(k + 2 - k / 408) % 3;
		di[j] = dc[k];
	}
}

/*! De-Interleave MCS7 UL burst bits according to TS 05.03 5.1.11.2.4
 *  \param[out] hc caller-allocated output buffer for 160 soft coded header bits
 *  \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
 *  \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
 *  \param[in] hi interleaved soft input header bits
 *  \param[in] di interleaved soft input data bits */
void gsm0503_mcs7_ul_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
	const sbit_t *hi, const sbit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	if (hc) {
		for (k = 0; k < 160; k++) {
			j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
			hc[k] = hi[j];
		}
	}

	/* Data */
	if (c1 && c2) {
		for (k = 0; k < 1224; k++) {
			j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
	(k + 2 - k / 408) % 3;
			dc[k] = di[j];
		}

		memcpy(c1, &dc[0], 612);
		memcpy(c2, &dc[612], 612);
	}
}

/*! Interleave MCS8 UL burst bits according to TS 05.03 5.1.12.2.4
 *  \param[in] hc 160 soft coded header input bits
 *  \param[in] c1 612 soft coded data input bits
 *  \param[in] c2 612 soft coded data input bits
 *  \param[out] hi 160 interleaved header output bits
 *  \param[out] di 1224 interleaved data output bits */
void gsm0503_mcs8_ul_interleave(const ubit_t *hc, const ubit_t *c1,
	const ubit_t *c2, ubit_t *hi, ubit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	for (k = 0; k < 160; k++) {
		j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
		hi[j] = hc[k];
	}

	memcpy(&dc[0], c1, 612);
	memcpy(&dc[612], c2, 612);

	/* Data */
	for (k = 0; k < 1224; k++) {
		j = 306 * (2 * (k / 612) + (k % 2)) +
			3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
		di[j] = dc[k];
	}
}


/*! De-Interleave MCS8 UL burst bits according to TS 05.03 5.1.12.2.4
 *  \param[out] hc caller-allocated output buffer for 160 soft coded header bits
 *  \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
 *  \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
 *  \param[in] hi interleaved soft input header bits
 *  \param[in] di interleaved soft input data bits */
void gsm0503_mcs8_ul_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
	const sbit_t *hi, const sbit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	if (hc) {
		for (k = 0; k < 160; k++) {
			j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
			hc[k] = hi[j];
		}
	}

	/* Data */
	if (c1 && c2) {
		for (k = 0; k < 1224; k++) {
			j = 306 * (2 * (k / 612) + (k % 2)) +
	3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
			dc[k] = di[j];
		}

		memcpy(c1, &dc[0], 612);
		memcpy(c2, &dc[612], 612);
	}
}

/*! Interleave MCS8 DL burst bits according to TS 05.03 5.1.12.1.5
 *  \param[in] hc 124 soft coded header input bits
 *  \param[in] c1 612 soft coded data input bits
 *  \param[in] c2 612 soft coded data input bits
 *  \param[out] hi 124 interleaved header output bits
 *  \param[out] di 1224 interleaved data output bits */
void gsm0503_mcs8_dl_interleave(const ubit_t *hc, const ubit_t *c1,
	const ubit_t *c2, ubit_t *hi, ubit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	for (k = 0; k < 124; k++) {
		j = 31 * (k % 4) + ((17 * k) % 31);
		hi[j] = hc[k];
	}

	memcpy(&dc[0], c1, 612);
	memcpy(&dc[612], c2, 612);

	/* Data */
	for (k = 0; k < 1224; k++) {
		j = 306 * (2 * (k / 612) + (k % 2)) +
			3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
		di[j] = dc[k];
	}
}

/*! De-Interleave MCS8 DL burst bits according to TS 05.03 5.1.12.1.5
 *  \param[out] hc caller-allocated output buffer for 124 soft coded header bits
 *  \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
 *  \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
 *  \param[in] hi interleaved soft input header bits
 *  \param[in] di interleaved soft input data bits */
void gsm0503_mcs8_dl_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
	const sbit_t *hi, const sbit_t *di)
{
	int j, k;
	ubit_t dc[1224];

	/* Header */
	if (hc) {
		for (k = 0; k < 124; k++) {
			j = 31 * (k % 4) + ((17 * k) % 31);
			hc[k] = hi[j];
		}
	}

	/* Data */
	if (c1 && c2) {
		for (k = 0; k < 1224; k++) {
			j = 306 * (2 * (k / 612) + (k % 2)) +
	3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
			dc[k] = di[j];
		}

		memcpy(c1, &dc[0], 612);
		memcpy(c2, &dc[612], 612);
	}
}

/*
 * GSM TCH FR/EFR/AFS interleaving and burst mapping
 *
 * Interleaving:
 *
 * Given 456 coded input bits, form 8 blocks of 114 bits,
 * where even bits of the first 4 blocks and odd bits of the last 4 blocks
 * are used:
 *
 *      i(B, j) = c(n, k)       k = 0, ..., 455
 *                              n = 0, ..., N, N + 1, ...
 *                              B = B_0 + 4n + (k mod 8)
 *                              j = 2(49k mod 57) + ((k mod 8) div 4)
 *
 * Mapping on Burst:
 *
 *      e(B, j) = i(B, j)
 *      e(B, 59 + j) = i(B, 57 + j)     j = 0, ..., 56
 *      e(B, 57) = h_l(B)
 *      e(B, 58) = h_n(B)
 *
 * Where hl(B) and hn(B) are bits in burst B indicating flags.
 */

/*! GSM TCH FR/EFR/AFS De-Interleaving and burst mapping
 *  \param[out] cB caller-allocated buffer for 456 unpacked output bits
 *  \param[in] iB 456 unpacked interleaved input bits */
void gsm0503_tch_fr_deinterleave(sbit_t *cB, const sbit_t *iB)
{
	int j, k, B;

	for (k = 0; k < 456; k++) {
		B = k & 7;
		j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
		cB[k] = iB[B * 114 + j];
	}
}

/*! GSM TCH FR/EFR/AFS Interleaving and burst mapping
 *  \param[in] cB caller-allocated buffer for 456 unpacked input bits
 *  \param[out] iB 456 unpacked interleaved output bits */
void gsm0503_tch_fr_interleave(const ubit_t *cB, ubit_t *iB)
{
	int j, k, B;

	for (k = 0; k < 456; k++) {
		B = k & 7;
		j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
		iB[B * 114 + j] = cB[k];
	}
}

/*! GSM TCH HR/AHS De-Interleaving and burst mapping
 *  \param[out] cB caller-allocated buffer for 228 unpacked output bits
 *  \param[in] iB 228 unpacked interleaved input bits */
void gsm0503_tch_hr_deinterleave(sbit_t *cB, const sbit_t *iB)
{
	int j, k, B;

	for (k = 0; k < 228; k++) {
		B = gsm0503_tch_hr_interleaving[k][1];
		j = gsm0503_tch_hr_interleaving[k][0];
		cB[k] = iB[B * 114 + j];
	}
}

/*! GSM TCH HR/AHS Interleaving and burst mapping
 *  \param[in] cB caller-allocated buffer for 228 unpacked input bits
 *  \param[out] iB 228 unpacked interleaved output bits */
void gsm0503_tch_hr_interleave(const ubit_t *cB, ubit_t *iB)
{
	int j, k, B;

	for (k = 0; k < 228; k++) {
		B = gsm0503_tch_hr_interleaving[k][1];
		j = gsm0503_tch_hr_interleaving[k][0];
		iB[B * 114 + j] = cB[k];
	}
}

/*! @} */