blob: b42f242d53d99b522de50a3a3ef00b28483350e7 [file] [log] [blame]
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001/*
2 * (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
3 * (C) 2016 by Tom Tsou <tom.tsou@ettus.com>
Harald Weltec6636782017-06-12 14:59:37 +02004 * (C) 2017 by Hrald Welte <laforge@gnumonks.org>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <stdint.h>
24#include <string.h>
25
26#include <osmocom/core/bits.h>
27#include <osmocom/coding/gsm0503_tables.h>
28#include <osmocom/coding/gsm0503_interleaving.h>
29
Harald Weltec6636782017-06-12 14:59:37 +020030/*! \addtogroup interleaving
31 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020032 * GSM TS 05.03 interleaving
Harald Weltec6636782017-06-12 14:59:37 +020033 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020034 * This module contains interleaving / de-interleaving routines for
35 * various channel types, as defined in 3GPP TS 05.03 / 45.003.
36 *
37 * GSM xCCH interleaving and burst mapping:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070038 *
39 * Interleaving:
40 *
41 * Given 456 coded input bits, form 4 blocks of 114 bits:
42 *
43 * i(B, j) = c(n, k) k = 0, ..., 455
44 * n = 0, ..., N, N + 1, ...
45 * B = B_0 + 4n + (k mod 4)
46 * j = 2(49k mod 57) + ((k mod 8) div 4)
47 *
48 * Mapping on Burst:
49 *
50 * e(B, j) = i(B, j)
51 * e(B, 59 + j) = i(B, 57 + j) j = 0, ..., 56
52 * e(B, 57) = h_l(B)
53 * e(B, 58) = h_n(B)
54 *
55 * Where hl(B) and hn(B) are bits in burst B indicating flags.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020056 *
57 * GSM TCH HR/AHS interleaving and burst mapping:
58 *
59 * Interleaving:
60 *
61 * Given 288 coded input bits, form 4 blocks of 114 bits,
62 * where even bits of the first 2 blocks and odd bits of the last 2 blocks
63 * are used:
64 *
65 * i(B, j) = c(n, k) k = 0, ..., 227
66 * n = 0, ..., N, N + 1, ...
67 * B = B_0 + 2n + b
68 * j, b = table[k];
69 *
70 * Mapping on Burst:
71 *
72 * e(B, j) = i(B, j)
73 * e(B, 59 + j) = i(B, 57 + j) j = 0, ..., 56
74 * e(B, 57) = h_l(B)
75 * e(B, 58) = h_n(B)
76 *
77 * Where hl(B) and hn(B) are bits in burst B indicating flags.
78 *
79 * \file gsm0503_interleaving.c */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070080
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081/*! De-Interleave burst bits according to TS 05.03 4.1.4
Harald Weltec6636782017-06-12 14:59:37 +020082 * \param[out] cB caller-allocated output buffer for 456 soft coded bits
83 * \param[in] iB 456 soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070084void gsm0503_xcch_deinterleave(sbit_t *cB, const sbit_t *iB)
85{
86 int j, k, B;
87
88 for (k = 0; k < 456; k++) {
89 B = k & 3;
90 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
91 cB[k] = iB[B * 114 + j];
92 }
93}
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! Interleave burst bits according to TS 05.03 4.1.4
Harald Weltec6636782017-06-12 14:59:37 +020096 * \param[out] iB caller-allocated output buffer for 456 soft interleaved bits
97 * \param[in] cB 456 soft input coded bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +020098void gsm0503_xcch_interleave(const ubit_t *cB, ubit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070099{
100 int j, k, B;
101
102 for (k = 0; k < 456; k++) {
103 B = k & 3;
104 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
105 iB[B * 114 + j] = cB[k];
106 }
107}
108
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200109/*! De-Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200110 * \param[out] u caller-allocated output buffer for 12 soft coded bits
111 * \param[out] hc caller-allocated output buffer for 68 soft coded bits
112 * \param[out] dc caller-allocated output buffer for 372 soft coded bits
113 * \param[in] iB 452 interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700114void gsm0503_mcs1_dl_deinterleave(sbit_t *u, sbit_t *hc,
115 sbit_t *dc, const sbit_t *iB)
116{
117 int k;
118 sbit_t c[452];
119 sbit_t cp[456];
120
121 gsm0503_xcch_deinterleave(cp, iB);
122
123 for (k = 0; k < 25; k++)
124 c[k] = cp[k];
125 for (k = 26; k < 82; k++)
126 c[k - 1] = cp[k];
127 for (k = 83; k < 139; k++)
128 c[k - 2] = cp[k];
129 for (k = 140; k < 424; k++)
130 c[k - 3] = cp[k];
131 for (k = 425; k < 456; k++)
132 c[k - 4] = cp[k];
133
134 if (u) {
135 for (k = 0; k < 12; k++)
136 u[k] = c[k];
137 }
138
139 if (hc) {
140 for (k = 12; k < 80; k++)
141 hc[k - 12] = c[k];
142 }
143
144 if (dc) {
145 for (k = 80; k < 452; k++)
146 dc[k - 80] = c[k];
147 }
148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200151 * \param[in] up 12 input soft coded bits (usf)
152 * \param[in] hc 68 input soft coded bits (header)
153 * \param[in] dc 372 input soft bits (data)
154 * \param[out] iB 456 interleaved soft output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700155void gsm0503_mcs1_dl_interleave(const ubit_t *up, const ubit_t *hc,
156 const ubit_t *dc, ubit_t *iB)
157{
158 int k;
159 ubit_t c[452];
160 ubit_t cp[456];
161
162 for (k = 0; k < 12; k++)
163 c[k] = up[k];
164 for (k = 12; k < 80; k++)
165 c[k] = hc[k - 12];
166 for (k = 80; k < 452; k++)
167 c[k] = dc[k - 80];
168
169 for (k = 0; k < 25; k++)
170 cp[k] = c[k];
171 for (k = 26; k < 82; k++)
172 cp[k] = c[k - 1];
173 for (k = 83; k < 139; k++)
174 cp[k] = c[k - 2];
175 for (k = 140; k < 424; k++)
176 cp[k] = c[k - 3];
177 for (k = 425; k < 456; k++)
178 cp[k] = c[k - 4];
179
180 cp[25] = 0;
181 cp[82] = 0;
182 cp[139] = 0;
183 cp[424] = 0;
184
185 gsm0503_xcch_interleave(cp, iB);
186}
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/*! Interleave MCS1 UL burst bits according to TS 05.03 5.1.5.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200189 * \param[out] hc caller-allocated output buffer for 80 soft coded header bits
190 * \param[out] dc caller-allocated output buffer for 372 soft coded data bits
191 * \param[in] iB 456 interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700192void gsm0503_mcs1_ul_deinterleave(sbit_t *hc, sbit_t *dc, const sbit_t *iB)
193{
194 int k;
195 sbit_t c[452];
196 sbit_t cp[456];
197
198 gsm0503_xcch_deinterleave(cp, iB);
199
200 for (k = 0; k < 25; k++)
201 c[k] = cp[k];
202 for (k = 26; k < 82; k++)
203 c[k - 1] = cp[k];
204 for (k = 83; k < 139; k++)
205 c[k - 2] = cp[k];
206 for (k = 140; k < 424; k++)
207 c[k - 3] = cp[k];
208 for (k = 425; k < 456; k++)
209 c[k - 4] = cp[k];
210
211 if (hc) {
212 for (k = 0; k < 80; k++)
213 hc[k] = c[k];
214 }
215
216 if (dc) {
217 for (k = 80; k < 452; k++)
218 dc[k - 80] = c[k];
219 }
220}
221
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200222/*! Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200223 * \param[in] hc 80 input coded bits (header)
224 * \param[in] dc 372 input bits (data)
225 * \param[out] iB 456 interleaved output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700226void gsm0503_mcs1_ul_interleave(const ubit_t *hc, const ubit_t *dc, ubit_t *iB)
227{
228 int k;
229 ubit_t c[452];
230 ubit_t cp[456];
231
232 for (k = 0; k < 80; k++)
233 c[k] = hc[k];
234 for (k = 80; k < 452; k++)
235 c[k] = dc[k - 80];
236
237 for (k = 0; k < 25; k++)
238 cp[k] = c[k];
239 for (k = 26; k < 82; k++)
240 cp[k] = c[k - 1];
241 for (k = 83; k < 139; k++)
242 cp[k] = c[k - 2];
243 for (k = 140; k < 424; k++)
244 cp[k] = c[k - 3];
245 for (k = 425; k < 456; k++)
246 cp[k] = c[k - 4];
247
248 cp[25] = 0;
249 cp[82] = 0;
250 cp[139] = 0;
251 cp[424] = 0;
252
253 gsm0503_xcch_interleave(cp, iB);
254}
255
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200256/*! Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200257 * \param[in] hc 136 soft coded header input bits
258 * \param[in] dc 1248 soft coded data input bits
259 * \param[out] hi 136 interleaved header output bits
260 * \param[out] di 1248 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700261void gsm0503_mcs5_ul_interleave(const ubit_t *hc, const ubit_t *dc,
262 ubit_t *hi, ubit_t *di)
263{
264 int j, k;
265
266 /* Header */
267 for (k = 0; k < 136; k++) {
268 j = 34 * (k % 4) + 2 * (11 * k % 17) + k % 8 / 4;
269 hi[j] = hc[k];
270 }
271
272 /* Data */
273 for (k = 0; k < 1248; k++) {
274 j = gsm0503_interleave_mcs5[k];
275 di[j] = dc[k];
276 }
277}
278
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200279/*! De-Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200280 * \param[out] hc caller-allocated output buffer for 136 soft coded header bits
281 * \param[out] dc caller-allocated output buffer for 1248 soft coded data bits
282 * \param[in] iB interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700283void gsm0503_mcs5_ul_deinterleave(sbit_t *hc, sbit_t *dc,
284 const sbit_t *hi, const sbit_t *di)
285{
286 int j, k;
287
288 /* Header */
289 if (hc) {
290 for (k = 0; k < 136; k++) {
291 j = 34 * (k % 4) + 2 * (11 * k % 17) + k % 8 / 4;
292 hc[k] = hi[j];
293 }
294 }
295
296 /* Data */
297 if (dc) {
298 for (k = 0; k < 1248; k++) {
299 j = gsm0503_interleave_mcs5[k];
300 dc[k] = di[j];
301 }
302 }
303}
304
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200305/*! Interleave MCS5 DL burst bits according to TS 05.03 5.1.9.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200306 * \param[in] hc 100 soft coded header input bits
307 * \param[in] dc 1248 soft coded data input bits
308 * \param[out] hi 100 interleaved header output bits
309 * \param[out] di 1248 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700310void gsm0503_mcs5_dl_interleave(const ubit_t *hc, const ubit_t *dc,
311 ubit_t *hi, ubit_t *di)
312{
313 int j, k;
314
315 /* Header */
316 for (k = 0; k < 100; k++) {
317 j = 25 * (k % 4) + ((17 * k) % 25);
318 hi[j] = hc[k];
319 }
320
321 /* Data */
322 for (k = 0; k < 1248; k++) {
323 j = gsm0503_interleave_mcs5[k];
324 di[j] = dc[k];
325 }
326}
327
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200328/*! De-Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200329 * \param[out] hc caller-allocated output buffer for 100 soft coded header bits
330 * \param[out] dc caller-allocated output buffer for 1248 soft coded data bits
331 * \param[in] iB interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700332void gsm0503_mcs5_dl_deinterleave(sbit_t *hc, sbit_t *dc,
333 const sbit_t *hi, const sbit_t *di)
334{
335 int j, k;
336
337 /* Header */
338 if (hc) {
339 for (k = 0; k < 100; k++) {
340 j = 25 * (k % 4) + ((17 * k) % 25);
341 hc[k] = hi[j];
342 }
343 }
344
345 /* Data */
346 if (dc) {
347 for (k = 0; k < 1248; k++) {
348 j = gsm0503_interleave_mcs5[k];
349 dc[k] = di[j];
350 }
351 }
352}
353
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200354/*! Interleave MCS7 DL burst bits according to TS 05.03 5.1.11.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200355 * \param[in] hc 124 soft coded header input bits
356 * \param[in] c1 612 soft coded data input bits
357 * \param[in] c2 612 soft coded data input bits
358 * \param[out] hi 124 interleaved header output bits
359 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700360void gsm0503_mcs7_dl_interleave(const ubit_t *hc, const ubit_t *c1,
361 const ubit_t *c2, ubit_t *hi, ubit_t *di)
362{
363 int j, k;
364 ubit_t dc[1224];
365
366 /* Header */
367 for (k = 0; k < 124; k++) {
368 j = 31 * (k % 4) + ((17 * k) % 31);
369 hi[j] = hc[k];
370 }
371
372 memcpy(&dc[0], c1, 612);
373 memcpy(&dc[612], c2, 612);
374
375 /* Data */
376 for (k = 0; k < 1224; k++) {
377 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
378 (k + 2 - k / 408) % 3;
379 di[j] = dc[k];
380 }
381}
382
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200383/*! De-Interleave MCS7 DL burst bits according to TS 05.03 5.1.11.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200384 * \param[out] hc caller-allocated output buffer for 124 soft coded header bits
385 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
386 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
387 * \param[in] hi interleaved soft input header bits
388 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700389void gsm0503_mcs7_dl_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
390 const sbit_t *hi, const sbit_t *di)
391{
392 int j, k;
393 ubit_t dc[1224];
394
395 /* Header */
396 if (hc) {
397 for (k = 0; k < 124; k++) {
398 j = 31 * (k % 4) + ((17 * k) % 31);
399 hc[k] = hi[j];
400 }
401 }
402
403 /* Data */
404 if (c1 && c2) {
405 for (k = 0; k < 1224; k++) {
406 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
407 (k + 2 - k / 408) % 3;
408 dc[k] = di[j];
409 }
410
411 memcpy(c1, &dc[0], 612);
412 memcpy(c2, &dc[612], 612);
413 }
414}
415
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200416/*! Interleave MCS7 UL burst bits according to TS 05.03 5.1.11.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200417 * \param[in] hc 124 soft coded header input bits
418 * \param[in] c1 612 soft coded data input bits
419 * \param[in] c2 612 soft coded data input bits
420 * \param[out] hi 124 interleaved header output bits
421 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700422void gsm0503_mcs7_ul_interleave(const ubit_t *hc, const ubit_t *c1,
423 const ubit_t *c2, ubit_t *hi, ubit_t *di)
424{
425 int j, k;
426 ubit_t dc[1224];
427
428 /* Header */
429 for (k = 0; k < 160; k++) {
430 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
431 hi[j] = hc[k];
432 }
433
434 memcpy(&dc[0], c1, 612);
435 memcpy(&dc[612], c2, 612);
436
437 /* Data */
438 for (k = 0; k < 1224; k++) {
439 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
440 (k + 2 - k / 408) % 3;
441 di[j] = dc[k];
442 }
443}
444
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200445/*! De-Interleave MCS7 UL burst bits according to TS 05.03 5.1.11.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200446 * \param[out] hc caller-allocated output buffer for 160 soft coded header bits
447 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
448 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
449 * \param[in] hi interleaved soft input header bits
450 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700451void gsm0503_mcs7_ul_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
452 const sbit_t *hi, const sbit_t *di)
453{
454 int j, k;
455 ubit_t dc[1224];
456
457 /* Header */
458 if (hc) {
459 for (k = 0; k < 160; k++) {
460 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
461 hc[k] = hi[j];
462 }
463 }
464
465 /* Data */
466 if (c1 && c2) {
467 for (k = 0; k < 1224; k++) {
468 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
469 (k + 2 - k / 408) % 3;
470 dc[k] = di[j];
471 }
472
473 memcpy(c1, &dc[0], 612);
474 memcpy(c2, &dc[612], 612);
475 }
476}
477
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200478/*! Interleave MCS8 UL burst bits according to TS 05.03 5.1.12.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200479 * \param[in] hc 160 soft coded header input bits
480 * \param[in] c1 612 soft coded data input bits
481 * \param[in] c2 612 soft coded data input bits
482 * \param[out] hi 160 interleaved header output bits
483 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700484void gsm0503_mcs8_ul_interleave(const ubit_t *hc, const ubit_t *c1,
485 const ubit_t *c2, ubit_t *hi, ubit_t *di)
486{
487 int j, k;
488 ubit_t dc[1224];
489
490 /* Header */
491 for (k = 0; k < 160; k++) {
492 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
493 hi[j] = hc[k];
494 }
495
496 memcpy(&dc[0], c1, 612);
497 memcpy(&dc[612], c2, 612);
498
499 /* Data */
500 for (k = 0; k < 1224; k++) {
501 j = 306 * (2 * (k / 612) + (k % 2)) +
502 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
503 di[j] = dc[k];
504 }
505}
506
Harald Weltec6636782017-06-12 14:59:37 +0200507
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200508/*! De-Interleave MCS8 UL burst bits according to TS 05.03 5.1.12.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200509 * \param[out] hc caller-allocated output buffer for 160 soft coded header bits
510 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
511 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
512 * \param[in] hi interleaved soft input header bits
513 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700514void gsm0503_mcs8_ul_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
515 const sbit_t *hi, const sbit_t *di)
516{
517 int j, k;
518 ubit_t dc[1224];
519
520 /* Header */
521 if (hc) {
522 for (k = 0; k < 160; k++) {
523 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
524 hc[k] = hi[j];
525 }
526 }
527
528 /* Data */
529 if (c1 && c2) {
530 for (k = 0; k < 1224; k++) {
531 j = 306 * (2 * (k / 612) + (k % 2)) +
532 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
533 dc[k] = di[j];
534 }
535
536 memcpy(c1, &dc[0], 612);
537 memcpy(c2, &dc[612], 612);
538 }
539}
540
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200541/*! Interleave MCS8 DL burst bits according to TS 05.03 5.1.12.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200542 * \param[in] hc 124 soft coded header input bits
543 * \param[in] c1 612 soft coded data input bits
544 * \param[in] c2 612 soft coded data input bits
545 * \param[out] hi 124 interleaved header output bits
546 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700547void gsm0503_mcs8_dl_interleave(const ubit_t *hc, const ubit_t *c1,
548 const ubit_t *c2, ubit_t *hi, ubit_t *di)
549{
550 int j, k;
551 ubit_t dc[1224];
552
553 /* Header */
554 for (k = 0; k < 124; k++) {
555 j = 31 * (k % 4) + ((17 * k) % 31);
556 hi[j] = hc[k];
557 }
558
559 memcpy(&dc[0], c1, 612);
560 memcpy(&dc[612], c2, 612);
561
562 /* Data */
563 for (k = 0; k < 1224; k++) {
564 j = 306 * (2 * (k / 612) + (k % 2)) +
565 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
566 di[j] = dc[k];
567 }
568}
569
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200570/*! De-Interleave MCS8 DL burst bits according to TS 05.03 5.1.12.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200571 * \param[out] hc caller-allocated output buffer for 124 soft coded header bits
572 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
573 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
574 * \param[in] hi interleaved soft input header bits
575 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700576void gsm0503_mcs8_dl_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
577 const sbit_t *hi, const sbit_t *di)
578{
579 int j, k;
580 ubit_t dc[1224];
581
582 /* Header */
583 if (hc) {
584 for (k = 0; k < 124; k++) {
585 j = 31 * (k % 4) + ((17 * k) % 31);
586 hc[k] = hi[j];
587 }
588 }
589
590 /* Data */
591 if (c1 && c2) {
592 for (k = 0; k < 1224; k++) {
593 j = 306 * (2 * (k / 612) + (k % 2)) +
594 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
595 dc[k] = di[j];
596 }
597
598 memcpy(c1, &dc[0], 612);
599 memcpy(c2, &dc[612], 612);
600 }
601}
602
603/*
604 * GSM TCH FR/EFR/AFS interleaving and burst mapping
605 *
606 * Interleaving:
607 *
608 * Given 456 coded input bits, form 8 blocks of 114 bits,
609 * where even bits of the first 4 blocks and odd bits of the last 4 blocks
610 * are used:
611 *
612 * i(B, j) = c(n, k) k = 0, ..., 455
613 * n = 0, ..., N, N + 1, ...
614 * B = B_0 + 4n + (k mod 8)
615 * j = 2(49k mod 57) + ((k mod 8) div 4)
616 *
617 * Mapping on Burst:
618 *
619 * e(B, j) = i(B, j)
620 * e(B, 59 + j) = i(B, 57 + j) j = 0, ..., 56
621 * e(B, 57) = h_l(B)
622 * e(B, 58) = h_n(B)
623 *
624 * Where hl(B) and hn(B) are bits in burst B indicating flags.
625 */
626
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200627/*! GSM TCH FR/EFR/AFS De-Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200628 * \param[out] cB caller-allocated buffer for 456 unpacked output bits
629 * \param[in] iB 456 unpacked interleaved input bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200630void gsm0503_tch_fr_deinterleave(sbit_t *cB, const sbit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700631{
632 int j, k, B;
633
634 for (k = 0; k < 456; k++) {
635 B = k & 7;
636 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
637 cB[k] = iB[B * 114 + j];
638 }
639}
640
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200641/*! GSM TCH FR/EFR/AFS Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200642 * \param[in] cB caller-allocated buffer for 456 unpacked input bits
643 * \param[out] iB 456 unpacked interleaved output bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200644void gsm0503_tch_fr_interleave(const ubit_t *cB, ubit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700645{
646 int j, k, B;
647
648 for (k = 0; k < 456; k++) {
649 B = k & 7;
650 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
651 iB[B * 114 + j] = cB[k];
652 }
653}
654
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200655/*! GSM TCH HR/AHS De-Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200656 * \param[out] cB caller-allocated buffer for 228 unpacked output bits
657 * \param[in] iB 228 unpacked interleaved input bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200658void gsm0503_tch_hr_deinterleave(sbit_t *cB, const sbit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700659{
660 int j, k, B;
661
662 for (k = 0; k < 228; k++) {
663 B = gsm0503_tch_hr_interleaving[k][1];
664 j = gsm0503_tch_hr_interleaving[k][0];
665 cB[k] = iB[B * 114 + j];
666 }
667}
668
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200669/*! GSM TCH HR/AHS Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200670 * \param[in] cB caller-allocated buffer for 228 unpacked input bits
671 * \param[out] iB 228 unpacked interleaved output bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200672void gsm0503_tch_hr_interleave(const ubit_t *cB, ubit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700673{
674 int j, k, B;
675
676 for (k = 0; k < 228; k++) {
677 B = gsm0503_tch_hr_interleaving[k][1];
678 j = gsm0503_tch_hr_interleaving[k][0];
679 iB[B * 114 + j] = cB[k];
680 }
681}
Harald Weltec6636782017-06-12 14:59:37 +0200682
683/*! @} */