blob: 570d65aa7e89526d23c0e47addbd621a6ac83790 [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 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070019 */
20
21#include <stdint.h>
22#include <string.h>
23
24#include <osmocom/core/bits.h>
25#include <osmocom/coding/gsm0503_tables.h>
26#include <osmocom/coding/gsm0503_interleaving.h>
27
Harald Weltec6636782017-06-12 14:59:37 +020028/*! \addtogroup interleaving
29 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020030 * GSM TS 05.03 interleaving
Harald Weltec6636782017-06-12 14:59:37 +020031 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020032 * This module contains interleaving / de-interleaving routines for
33 * various channel types, as defined in 3GPP TS 05.03 / 45.003.
34 *
35 * GSM xCCH interleaving and burst mapping:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070036 *
37 * Interleaving:
38 *
39 * Given 456 coded input bits, form 4 blocks of 114 bits:
40 *
41 * i(B, j) = c(n, k) k = 0, ..., 455
42 * n = 0, ..., N, N + 1, ...
43 * B = B_0 + 4n + (k mod 4)
44 * j = 2(49k mod 57) + ((k mod 8) div 4)
45 *
46 * Mapping on Burst:
47 *
48 * e(B, j) = i(B, j)
49 * e(B, 59 + j) = i(B, 57 + j) j = 0, ..., 56
50 * e(B, 57) = h_l(B)
51 * e(B, 58) = h_n(B)
52 *
53 * Where hl(B) and hn(B) are bits in burst B indicating flags.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020054 *
55 * GSM TCH HR/AHS interleaving and burst mapping:
56 *
57 * Interleaving:
58 *
59 * Given 288 coded input bits, form 4 blocks of 114 bits,
60 * where even bits of the first 2 blocks and odd bits of the last 2 blocks
61 * are used:
62 *
63 * i(B, j) = c(n, k) k = 0, ..., 227
64 * n = 0, ..., N, N + 1, ...
65 * B = B_0 + 2n + b
66 * j, b = table[k];
67 *
68 * Mapping on Burst:
69 *
70 * e(B, j) = i(B, j)
71 * e(B, 59 + j) = i(B, 57 + j) j = 0, ..., 56
72 * e(B, 57) = h_l(B)
73 * e(B, 58) = h_n(B)
74 *
75 * Where hl(B) and hn(B) are bits in burst B indicating flags.
76 *
77 * \file gsm0503_interleaving.c */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070078
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079/*! De-Interleave burst bits according to TS 05.03 4.1.4
Harald Weltec6636782017-06-12 14:59:37 +020080 * \param[out] cB caller-allocated output buffer for 456 soft coded bits
81 * \param[in] iB 456 soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070082void gsm0503_xcch_deinterleave(sbit_t *cB, const sbit_t *iB)
83{
84 int j, k, B;
85
86 for (k = 0; k < 456; k++) {
87 B = k & 3;
88 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
89 cB[k] = iB[B * 114 + j];
90 }
91}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! Interleave burst bits according to TS 05.03 4.1.4
Harald Weltec6636782017-06-12 14:59:37 +020094 * \param[out] iB caller-allocated output buffer for 456 soft interleaved bits
95 * \param[in] cB 456 soft input coded bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +020096void gsm0503_xcch_interleave(const ubit_t *cB, ubit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070097{
98 int j, k, B;
99
100 for (k = 0; k < 456; k++) {
101 B = k & 3;
102 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
103 iB[B * 114 + j] = cB[k];
104 }
105}
106
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107/*! De-Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200108 * \param[out] u caller-allocated output buffer for 12 soft coded bits
109 * \param[out] hc caller-allocated output buffer for 68 soft coded bits
110 * \param[out] dc caller-allocated output buffer for 372 soft coded bits
111 * \param[in] iB 452 interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700112void gsm0503_mcs1_dl_deinterleave(sbit_t *u, sbit_t *hc,
113 sbit_t *dc, const sbit_t *iB)
114{
115 int k;
116 sbit_t c[452];
117 sbit_t cp[456];
118
119 gsm0503_xcch_deinterleave(cp, iB);
120
121 for (k = 0; k < 25; k++)
122 c[k] = cp[k];
123 for (k = 26; k < 82; k++)
124 c[k - 1] = cp[k];
125 for (k = 83; k < 139; k++)
126 c[k - 2] = cp[k];
127 for (k = 140; k < 424; k++)
128 c[k - 3] = cp[k];
129 for (k = 425; k < 456; k++)
130 c[k - 4] = cp[k];
131
132 if (u) {
133 for (k = 0; k < 12; k++)
134 u[k] = c[k];
135 }
136
137 if (hc) {
138 for (k = 12; k < 80; k++)
139 hc[k - 12] = c[k];
140 }
141
142 if (dc) {
143 for (k = 80; k < 452; k++)
144 dc[k - 80] = c[k];
145 }
146}
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148/*! Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200149 * \param[in] up 12 input soft coded bits (usf)
150 * \param[in] hc 68 input soft coded bits (header)
151 * \param[in] dc 372 input soft bits (data)
152 * \param[out] iB 456 interleaved soft output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700153void gsm0503_mcs1_dl_interleave(const ubit_t *up, const ubit_t *hc,
154 const ubit_t *dc, ubit_t *iB)
155{
156 int k;
157 ubit_t c[452];
158 ubit_t cp[456];
159
160 for (k = 0; k < 12; k++)
161 c[k] = up[k];
162 for (k = 12; k < 80; k++)
163 c[k] = hc[k - 12];
164 for (k = 80; k < 452; k++)
165 c[k] = dc[k - 80];
166
167 for (k = 0; k < 25; k++)
168 cp[k] = c[k];
169 for (k = 26; k < 82; k++)
170 cp[k] = c[k - 1];
171 for (k = 83; k < 139; k++)
172 cp[k] = c[k - 2];
173 for (k = 140; k < 424; k++)
174 cp[k] = c[k - 3];
175 for (k = 425; k < 456; k++)
176 cp[k] = c[k - 4];
177
178 cp[25] = 0;
179 cp[82] = 0;
180 cp[139] = 0;
181 cp[424] = 0;
182
183 gsm0503_xcch_interleave(cp, iB);
184}
185
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200186/*! Interleave MCS1 UL burst bits according to TS 05.03 5.1.5.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200187 * \param[out] hc caller-allocated output buffer for 80 soft coded header bits
188 * \param[out] dc caller-allocated output buffer for 372 soft coded data bits
189 * \param[in] iB 456 interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700190void gsm0503_mcs1_ul_deinterleave(sbit_t *hc, sbit_t *dc, const sbit_t *iB)
191{
192 int k;
193 sbit_t c[452];
194 sbit_t cp[456];
195
196 gsm0503_xcch_deinterleave(cp, iB);
197
198 for (k = 0; k < 25; k++)
199 c[k] = cp[k];
200 for (k = 26; k < 82; k++)
201 c[k - 1] = cp[k];
202 for (k = 83; k < 139; k++)
203 c[k - 2] = cp[k];
204 for (k = 140; k < 424; k++)
205 c[k - 3] = cp[k];
206 for (k = 425; k < 456; k++)
207 c[k - 4] = cp[k];
208
209 if (hc) {
210 for (k = 0; k < 80; k++)
211 hc[k] = c[k];
212 }
213
214 if (dc) {
215 for (k = 80; k < 452; k++)
216 dc[k - 80] = c[k];
217 }
218}
219
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200220/*! Interleave MCS1 DL burst bits according to TS 05.03 5.1.5.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200221 * \param[in] hc 80 input coded bits (header)
222 * \param[in] dc 372 input bits (data)
223 * \param[out] iB 456 interleaved output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700224void gsm0503_mcs1_ul_interleave(const ubit_t *hc, const ubit_t *dc, ubit_t *iB)
225{
226 int k;
227 ubit_t c[452];
228 ubit_t cp[456];
229
230 for (k = 0; k < 80; k++)
231 c[k] = hc[k];
232 for (k = 80; k < 452; k++)
233 c[k] = dc[k - 80];
234
235 for (k = 0; k < 25; k++)
236 cp[k] = c[k];
237 for (k = 26; k < 82; k++)
238 cp[k] = c[k - 1];
239 for (k = 83; k < 139; k++)
240 cp[k] = c[k - 2];
241 for (k = 140; k < 424; k++)
242 cp[k] = c[k - 3];
243 for (k = 425; k < 456; k++)
244 cp[k] = c[k - 4];
245
246 cp[25] = 0;
247 cp[82] = 0;
248 cp[139] = 0;
249 cp[424] = 0;
250
251 gsm0503_xcch_interleave(cp, iB);
252}
253
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200254/*! Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200255 * \param[in] hc 136 soft coded header input bits
256 * \param[in] dc 1248 soft coded data input bits
257 * \param[out] hi 136 interleaved header output bits
258 * \param[out] di 1248 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700259void gsm0503_mcs5_ul_interleave(const ubit_t *hc, const ubit_t *dc,
260 ubit_t *hi, ubit_t *di)
261{
262 int j, k;
263
264 /* Header */
265 for (k = 0; k < 136; k++) {
266 j = 34 * (k % 4) + 2 * (11 * k % 17) + k % 8 / 4;
267 hi[j] = hc[k];
268 }
269
270 /* Data */
271 for (k = 0; k < 1248; k++) {
272 j = gsm0503_interleave_mcs5[k];
273 di[j] = dc[k];
274 }
275}
276
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200277/*! De-Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200278 * \param[out] hc caller-allocated output buffer for 136 soft coded header bits
279 * \param[out] dc caller-allocated output buffer for 1248 soft coded data bits
280 * \param[in] iB interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700281void gsm0503_mcs5_ul_deinterleave(sbit_t *hc, sbit_t *dc,
282 const sbit_t *hi, const sbit_t *di)
283{
284 int j, k;
285
286 /* Header */
287 if (hc) {
288 for (k = 0; k < 136; k++) {
289 j = 34 * (k % 4) + 2 * (11 * k % 17) + k % 8 / 4;
290 hc[k] = hi[j];
291 }
292 }
293
294 /* Data */
295 if (dc) {
296 for (k = 0; k < 1248; k++) {
297 j = gsm0503_interleave_mcs5[k];
298 dc[k] = di[j];
299 }
300 }
301}
302
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200303/*! Interleave MCS5 DL burst bits according to TS 05.03 5.1.9.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200304 * \param[in] hc 100 soft coded header input bits
305 * \param[in] dc 1248 soft coded data input bits
306 * \param[out] hi 100 interleaved header output bits
307 * \param[out] di 1248 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700308void gsm0503_mcs5_dl_interleave(const ubit_t *hc, const ubit_t *dc,
309 ubit_t *hi, ubit_t *di)
310{
311 int j, k;
312
313 /* Header */
314 for (k = 0; k < 100; k++) {
315 j = 25 * (k % 4) + ((17 * k) % 25);
316 hi[j] = hc[k];
317 }
318
319 /* Data */
320 for (k = 0; k < 1248; k++) {
321 j = gsm0503_interleave_mcs5[k];
322 di[j] = dc[k];
323 }
324}
325
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200326/*! De-Interleave MCS5 UL burst bits according to TS 05.03 5.1.9.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200327 * \param[out] hc caller-allocated output buffer for 100 soft coded header bits
328 * \param[out] dc caller-allocated output buffer for 1248 soft coded data bits
329 * \param[in] iB interleaved soft input bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700330void gsm0503_mcs5_dl_deinterleave(sbit_t *hc, sbit_t *dc,
331 const sbit_t *hi, const sbit_t *di)
332{
333 int j, k;
334
335 /* Header */
336 if (hc) {
337 for (k = 0; k < 100; k++) {
338 j = 25 * (k % 4) + ((17 * k) % 25);
339 hc[k] = hi[j];
340 }
341 }
342
343 /* Data */
344 if (dc) {
345 for (k = 0; k < 1248; k++) {
346 j = gsm0503_interleave_mcs5[k];
347 dc[k] = di[j];
348 }
349 }
350}
351
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200352/*! Interleave MCS7 DL burst bits according to TS 05.03 5.1.11.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200353 * \param[in] hc 124 soft coded header input bits
354 * \param[in] c1 612 soft coded data input bits
355 * \param[in] c2 612 soft coded data input bits
356 * \param[out] hi 124 interleaved header output bits
357 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700358void gsm0503_mcs7_dl_interleave(const ubit_t *hc, const ubit_t *c1,
359 const ubit_t *c2, ubit_t *hi, ubit_t *di)
360{
361 int j, k;
362 ubit_t dc[1224];
363
364 /* Header */
365 for (k = 0; k < 124; k++) {
366 j = 31 * (k % 4) + ((17 * k) % 31);
367 hi[j] = hc[k];
368 }
369
370 memcpy(&dc[0], c1, 612);
371 memcpy(&dc[612], c2, 612);
372
373 /* Data */
374 for (k = 0; k < 1224; k++) {
375 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
376 (k + 2 - k / 408) % 3;
377 di[j] = dc[k];
378 }
379}
380
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200381/*! De-Interleave MCS7 DL burst bits according to TS 05.03 5.1.11.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200382 * \param[out] hc caller-allocated output buffer for 124 soft coded header bits
383 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
384 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
385 * \param[in] hi interleaved soft input header bits
386 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700387void gsm0503_mcs7_dl_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
388 const sbit_t *hi, const sbit_t *di)
389{
390 int j, k;
391 ubit_t dc[1224];
392
393 /* Header */
394 if (hc) {
395 for (k = 0; k < 124; k++) {
396 j = 31 * (k % 4) + ((17 * k) % 31);
397 hc[k] = hi[j];
398 }
399 }
400
401 /* Data */
402 if (c1 && c2) {
403 for (k = 0; k < 1224; k++) {
404 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
405 (k + 2 - k / 408) % 3;
406 dc[k] = di[j];
407 }
408
409 memcpy(c1, &dc[0], 612);
410 memcpy(c2, &dc[612], 612);
411 }
412}
413
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200414/*! Interleave MCS7 UL burst bits according to TS 05.03 5.1.11.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200415 * \param[in] hc 124 soft coded header input bits
416 * \param[in] c1 612 soft coded data input bits
417 * \param[in] c2 612 soft coded data input bits
418 * \param[out] hi 124 interleaved header output bits
419 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700420void gsm0503_mcs7_ul_interleave(const ubit_t *hc, const ubit_t *c1,
421 const ubit_t *c2, ubit_t *hi, ubit_t *di)
422{
423 int j, k;
424 ubit_t dc[1224];
425
426 /* Header */
427 for (k = 0; k < 160; k++) {
428 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
429 hi[j] = hc[k];
430 }
431
432 memcpy(&dc[0], c1, 612);
433 memcpy(&dc[612], c2, 612);
434
435 /* Data */
436 for (k = 0; k < 1224; k++) {
437 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
438 (k + 2 - k / 408) % 3;
439 di[j] = dc[k];
440 }
441}
442
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200443/*! De-Interleave MCS7 UL burst bits according to TS 05.03 5.1.11.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200444 * \param[out] hc caller-allocated output buffer for 160 soft coded header bits
445 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
446 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
447 * \param[in] hi interleaved soft input header bits
448 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700449void gsm0503_mcs7_ul_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
450 const sbit_t *hi, const sbit_t *di)
451{
452 int j, k;
453 ubit_t dc[1224];
454
455 /* Header */
456 if (hc) {
457 for (k = 0; k < 160; k++) {
458 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
459 hc[k] = hi[j];
460 }
461 }
462
463 /* Data */
464 if (c1 && c2) {
465 for (k = 0; k < 1224; k++) {
466 j = 306 * (k % 4) + 3 * (44 * k % 102 + k / 4 % 2) +
467 (k + 2 - k / 408) % 3;
468 dc[k] = di[j];
469 }
470
471 memcpy(c1, &dc[0], 612);
472 memcpy(c2, &dc[612], 612);
473 }
474}
475
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200476/*! Interleave MCS8 UL burst bits according to TS 05.03 5.1.12.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200477 * \param[in] hc 160 soft coded header input bits
478 * \param[in] c1 612 soft coded data input bits
479 * \param[in] c2 612 soft coded data input bits
480 * \param[out] hi 160 interleaved header output bits
481 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700482void gsm0503_mcs8_ul_interleave(const ubit_t *hc, const ubit_t *c1,
483 const ubit_t *c2, ubit_t *hi, ubit_t *di)
484{
485 int j, k;
486 ubit_t dc[1224];
487
488 /* Header */
489 for (k = 0; k < 160; k++) {
490 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
491 hi[j] = hc[k];
492 }
493
494 memcpy(&dc[0], c1, 612);
495 memcpy(&dc[612], c2, 612);
496
497 /* Data */
498 for (k = 0; k < 1224; k++) {
499 j = 306 * (2 * (k / 612) + (k % 2)) +
500 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
501 di[j] = dc[k];
502 }
503}
504
Harald Weltec6636782017-06-12 14:59:37 +0200505
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200506/*! De-Interleave MCS8 UL burst bits according to TS 05.03 5.1.12.2.4
Harald Weltec6636782017-06-12 14:59:37 +0200507 * \param[out] hc caller-allocated output buffer for 160 soft coded header bits
508 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
509 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
510 * \param[in] hi interleaved soft input header bits
511 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700512void gsm0503_mcs8_ul_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
513 const sbit_t *hi, const sbit_t *di)
514{
515 int j, k;
516 ubit_t dc[1224];
517
518 /* Header */
519 if (hc) {
520 for (k = 0; k < 160; k++) {
521 j = 40 * (k % 4) + 2 * (13 * (k / 8) % 20) + k % 8 / 4;
522 hc[k] = hi[j];
523 }
524 }
525
526 /* Data */
527 if (c1 && c2) {
528 for (k = 0; k < 1224; k++) {
529 j = 306 * (2 * (k / 612) + (k % 2)) +
530 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
531 dc[k] = di[j];
532 }
533
534 memcpy(c1, &dc[0], 612);
535 memcpy(c2, &dc[612], 612);
536 }
537}
538
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200539/*! Interleave MCS8 DL burst bits according to TS 05.03 5.1.12.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200540 * \param[in] hc 124 soft coded header input bits
541 * \param[in] c1 612 soft coded data input bits
542 * \param[in] c2 612 soft coded data input bits
543 * \param[out] hi 124 interleaved header output bits
544 * \param[out] di 1224 interleaved data output bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700545void gsm0503_mcs8_dl_interleave(const ubit_t *hc, const ubit_t *c1,
546 const ubit_t *c2, ubit_t *hi, ubit_t *di)
547{
548 int j, k;
549 ubit_t dc[1224];
550
551 /* Header */
552 for (k = 0; k < 124; k++) {
553 j = 31 * (k % 4) + ((17 * k) % 31);
554 hi[j] = hc[k];
555 }
556
557 memcpy(&dc[0], c1, 612);
558 memcpy(&dc[612], c2, 612);
559
560 /* Data */
561 for (k = 0; k < 1224; k++) {
562 j = 306 * (2 * (k / 612) + (k % 2)) +
563 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
564 di[j] = dc[k];
565 }
566}
567
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200568/*! De-Interleave MCS8 DL burst bits according to TS 05.03 5.1.12.1.5
Harald Weltec6636782017-06-12 14:59:37 +0200569 * \param[out] hc caller-allocated output buffer for 124 soft coded header bits
570 * \param[out] c1 caller-allocated output buffer for 612 soft coded data bits
571 * \param[out] c2 caller-allocated output buffer for 612 soft coded data bits
572 * \param[in] hi interleaved soft input header bits
573 * \param[in] di interleaved soft input data bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700574void gsm0503_mcs8_dl_deinterleave(sbit_t *hc, sbit_t *c1, sbit_t *c2,
575 const sbit_t *hi, const sbit_t *di)
576{
577 int j, k;
578 ubit_t dc[1224];
579
580 /* Header */
581 if (hc) {
582 for (k = 0; k < 124; k++) {
583 j = 31 * (k % 4) + ((17 * k) % 31);
584 hc[k] = hi[j];
585 }
586 }
587
588 /* Data */
589 if (c1 && c2) {
590 for (k = 0; k < 1224; k++) {
591 j = 306 * (2 * (k / 612) + (k % 2)) +
592 3 * (74 * k % 102 + k / 2 % 2) + (k + 2 - k / 204) % 3;
593 dc[k] = di[j];
594 }
595
596 memcpy(c1, &dc[0], 612);
597 memcpy(c2, &dc[612], 612);
598 }
599}
600
601/*
602 * GSM TCH FR/EFR/AFS interleaving and burst mapping
603 *
604 * Interleaving:
605 *
606 * Given 456 coded input bits, form 8 blocks of 114 bits,
607 * where even bits of the first 4 blocks and odd bits of the last 4 blocks
608 * are used:
609 *
610 * i(B, j) = c(n, k) k = 0, ..., 455
611 * n = 0, ..., N, N + 1, ...
612 * B = B_0 + 4n + (k mod 8)
613 * j = 2(49k mod 57) + ((k mod 8) div 4)
614 *
615 * Mapping on Burst:
616 *
617 * e(B, j) = i(B, j)
618 * e(B, 59 + j) = i(B, 57 + j) j = 0, ..., 56
619 * e(B, 57) = h_l(B)
620 * e(B, 58) = h_n(B)
621 *
622 * Where hl(B) and hn(B) are bits in burst B indicating flags.
623 */
624
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200625/*! GSM TCH FR/EFR/AFS De-Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200626 * \param[out] cB caller-allocated buffer for 456 unpacked output bits
627 * \param[in] iB 456 unpacked interleaved input bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200628void gsm0503_tch_fr_deinterleave(sbit_t *cB, const sbit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700629{
630 int j, k, B;
631
632 for (k = 0; k < 456; k++) {
633 B = k & 7;
634 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
635 cB[k] = iB[B * 114 + j];
636 }
637}
638
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200639/*! GSM TCH FR/EFR/AFS Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200640 * \param[in] cB caller-allocated buffer for 456 unpacked input bits
641 * \param[out] iB 456 unpacked interleaved output bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200642void gsm0503_tch_fr_interleave(const ubit_t *cB, ubit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700643{
644 int j, k, B;
645
646 for (k = 0; k < 456; k++) {
647 B = k & 7;
648 j = 2 * ((49 * k) % 57) + ((k & 7) >> 2);
649 iB[B * 114 + j] = cB[k];
650 }
651}
652
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200653/*! GSM TCH HR/AHS De-Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200654 * \param[out] cB caller-allocated buffer for 228 unpacked output bits
655 * \param[in] iB 228 unpacked interleaved input bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200656void gsm0503_tch_hr_deinterleave(sbit_t *cB, const sbit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700657{
658 int j, k, B;
659
660 for (k = 0; k < 228; k++) {
661 B = gsm0503_tch_hr_interleaving[k][1];
662 j = gsm0503_tch_hr_interleaving[k][0];
663 cB[k] = iB[B * 114 + j];
664 }
665}
666
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200667/*! GSM TCH HR/AHS Interleaving and burst mapping
Harald Weltec6636782017-06-12 14:59:37 +0200668 * \param[in] cB caller-allocated buffer for 228 unpacked input bits
669 * \param[out] iB 228 unpacked interleaved output bits */
Harald Welte0eb2c5d2017-06-12 15:03:11 +0200670void gsm0503_tch_hr_interleave(const ubit_t *cB, ubit_t *iB)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700671{
672 int j, k, B;
673
674 for (k = 0; k < 228; k++) {
675 B = gsm0503_tch_hr_interleaving[k][1];
676 j = gsm0503_tch_hr_interleaving[k][0];
677 iB[B * 114 + j] = cB[k];
678 }
679}
Harald Weltec6636782017-06-12 14:59:37 +0200680
Harald Welte63453332020-02-13 10:55:25 +0100681/* 3GPP TS 45.003 Section 3.3.4
682 * The coded bits are reordered and interleaved according to the following rule:
683 * i(B,j) = c(n,k) for k = 0,1,...,455
684 * n = 0,1,...,N,N + 1,...
685 * B = B0 +4n + (k mod 19) + (k div 114)
686 * j = (k mod 19) + 19 (k mod 6)
687 *
688 * The result of the interleaving is a distribution of the reordered 114
689 * bit of a given data block, n = N, over 19 blocks, 6 bits equally
690 * distributed in each block, in a diagonal way over consecutive blocks.
691 *
692 * Or in other words the interleaving is a distribution of the encoded,
693 * reordered 456 bits from four given input data blocks, which taken
694 * together give n = N, over 22 bursts, 6 bits equally distributed in
695 * the first and 22 nd bursts, 12 bits distributed in the second and 21
696 * st bursts, 18 bits distributed in the third and 20 th bursts and 24
697 * bits distributed in the other 16 bursts.
698 *
699 * The block of coded data is interleaved "diagonal", where a new block
700 * of coded data starts with every fourth burst and is distributed over
701 * 22 bursts.
702 *
Vadim Yanitskiy548990b2023-05-20 18:02:32 +0700703 * Also used for TCH/F4.8, TCH/H4.8, and TCH/H2.4 and TCH/F14.4 */
Harald Welte63453332020-02-13 10:55:25 +0100704void gsm0503_tch_f96_interleave(const ubit_t *cB, ubit_t *iB)
705{
706 int j, k, B;
707
708 for (k = 0; k < 456; k++) {
709 /* upper bound for B: 4*n + 18 + 4 = 4*n + 22 */
710 B = /* B0 + 4n + */ (k % 19) + (k / 114);
711 /* upper bound for j: 18 + 19*5 = 113 */
712 j = (k % 19) + 19 * (k % 6);
713 /* upper iB index: 4*n+23*114-1 */
714 iB[B * 114 + j] = cB[k];
715 }
716}
717
718void gsm0503_tch_f96_deinterleave(sbit_t *cB, const sbit_t *iB)
719{
720 int j, k, B;
721
722 for (k = 0; k < 456; k++) {
723 /* upper bound for B: 4*n + 18 + 4 = 4*n + 22 */
724 B = /* B0 + 4n + */ (k % 19) + (k / 114);
725 /* upper bound for j: 18 + 19*5 = 113 */
726 j = (k % 19) + 19 * (k % 6);
727 /* upper iB index: 4*n+23*114-1 */
728 cB[k] = iB[B * 114 + j];
729 }
730}
731
732
Harald Weltec6636782017-06-12 14:59:37 +0200733/*! @} */