blob: 9272fdbc5d5e9e82432ceec378c459c15366b1e2 [file] [log] [blame]
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001/*
2 * (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
3 * (C) 2015 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co>
4 * (C) 2016 by Tom Tsou <tom.tsou@ettus.com>
Harald Weltec6636782017-06-12 14:59:37 +02005 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070020 */
21
22#include <stdio.h>
23#include <stdint.h>
24#include <string.h>
25#include <stdlib.h>
Maxc8cf8202017-05-22 16:07:04 +020026#include <errno.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070027
28#include <osmocom/core/bits.h>
29#include <osmocom/core/conv.h>
30#include <osmocom/core/utils.h>
31#include <osmocom/core/crcgen.h>
32#include <osmocom/core/endian.h>
33
Pau Espin Pedrol0158b052023-02-17 12:30:57 +010034#include <osmocom/gsm/protocol/gsm_44_060.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070035#include <osmocom/gsm/protocol/gsm_04_08.h>
36#include <osmocom/gsm/gsm0503.h>
37#include <osmocom/codec/codec.h>
38
39#include <osmocom/coding/gsm0503_interleaving.h>
40#include <osmocom/coding/gsm0503_mapping.h>
41#include <osmocom/coding/gsm0503_tables.h>
42#include <osmocom/coding/gsm0503_coding.h>
43#include <osmocom/coding/gsm0503_parity.h>
Philipp Maier898c9c62020-02-06 14:25:01 +010044#include <osmocom/coding/gsm0503_amr_dtx.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070045
Harald Weltec6636782017-06-12 14:59:37 +020046/*! \mainpage libosmocoding Documentation
47 *
48 * \section sec_intro Introduction
49 * This library is a collection of definitions, tables and functions
50 * implementing the GSM/GPRS/EGPRS channel coding (and decoding) as
51 * specified in 3GPP TS 05.03 / 45.003.
52 *
Vadim Yanitskiy9a232fd2018-01-19 03:05:32 +060053 * libosmocoding is developed as part of the Osmocom (Open Source Mobile
Harald Weltec6636782017-06-12 14:59:37 +020054 * Communications) project, a community-based, collaborative development
55 * project to create Free and Open Source implementations of mobile
56 * communications systems. For more information about Osmocom, please
57 * see https://osmocom.org/
58 *
59 * \section sec_copyright Copyright and License
60 * Copyright © 2013 by Andreas Eversberg\n
61 * Copyright © 2015 by Alexander Chemeris\n
62 * Copyright © 2016 by Tom Tsou\n
63 * Documentation Copyright © 2017 by Harald Welte\n
64 * All rights reserved. \n\n
65 * The source code of libosmocoding is licensed under the terms of the GNU
66 * General Public License as published by the Free Software Foundation;
67 * either version 2 of the License, or (at your option) any later
68 * version.\n
69 * See <http://www.gnu.org/licenses/> or COPYING included in the source
70 * code package istelf.\n
71 * The information detailed here is provided AS IS with NO WARRANTY OF
72 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
73 * FITNESS FOR A PARTICULAR PURPOSE.
74 * \n\n
75 *
76 * \section sec_tracker Homepage + Issue Tracker
77 * libosmocoding is distributed as part of libosmocore and shares its
78 * project page at http://osmocom.org/projects/libosmocore
79 *
80 * An Issue Tracker can be found at
81 * https://osmocom.org/projects/libosmocore/issues
82 *
83 * \section sec_contact Contact and Support
84 * Community-based support is available at the OpenBSC mailing list
85 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
86 * Commercial support options available upon request from
87 * <http://sysmocom.de/>
88 */
89
90
91/*! \addtogroup coding
92 * @{
93 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094 * GSM TS 05.03 coding
Harald Weltec6636782017-06-12 14:59:37 +020095 *
96 * This module is the "master module" of libosmocoding. It uses the
97 * various other modules (mapping, parity, interleaving) in order to
98 * implement the complete channel coding (and decoding) chain for the
99 * various channel types as defined in TS 05.03 / 45.003.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +0200100 *
101 * \file gsm0503_coding.c */
Harald Weltec6636782017-06-12 14:59:37 +0200102
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700103/*
104 * EGPRS coding limits
105 */
106
107/* Max header size with parity bits */
108#define EGPRS_HDR_UPP_MAX 54
109
110/* Max encoded header size */
111#define EGPRS_HDR_C_MAX 162
112
113/* Max punctured header size */
114#define EGPRS_HDR_HC_MAX 160
115
116/* Max data block size with parity bits */
117#define EGPRS_DATA_U_MAX 612
118
119/* Max encoded data block size */
120#define EGPRS_DATA_C_MAX 1836
121
122/* Max single block punctured data size */
123#define EGPRS_DATA_DC_MAX 1248
124
125/* Dual block punctured data size */
126#define EGPRS_DATA_C1 612
127#define EGPRS_DATA_C2 EGPRS_DATA_C1
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! union across the three different EGPRS Uplink header types */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700130union gprs_rlc_ul_hdr_egprs {
131 struct gprs_rlc_ul_header_egprs_1 type1;
132 struct gprs_rlc_ul_header_egprs_2 type2;
133 struct gprs_rlc_ul_header_egprs_3 type3;
134};
135
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136/*! union across the three different EGPRS Downlink header types */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700137union gprs_rlc_dl_hdr_egprs {
138 struct gprs_rlc_dl_header_egprs_1 type1;
139 struct gprs_rlc_dl_header_egprs_2 type2;
140 struct gprs_rlc_dl_header_egprs_3 type3;
141};
142
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143/*! Structure describing a Modulation and Coding Scheme */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700144struct gsm0503_mcs_code {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200145 /*! Modulation and Coding Scheme (MSC) number */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700146 uint8_t mcs;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200147 /*! Length of Uplink Stealing Flag (USF) in bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700148 uint8_t usf_len;
149
150 /* Header coding */
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151 /*! Length of header (bits) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700152 uint8_t hdr_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153 /*! Length of header convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700154 uint8_t hdr_code_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155 /*! Length of header code puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700156 uint8_t hdr_punc_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157 /*! header convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700158 const struct osmo_conv_code *hdr_conv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159 /*! header puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700160 const uint8_t *hdr_punc;
161
162 /* Data coding */
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163 /*! length of data (bits) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700164 uint16_t data_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200165 /*! length of data convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700166 uint16_t data_code_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167 /*! length of data code puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700168 uint16_t data_punc_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200169 /*! data convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700170 const struct osmo_conv_code *data_conv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200171 /*! data puncturing sequences */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700172 const uint8_t *data_punc[3];
173};
174
175/*
176 * EGPRS UL coding parameters
177 */
Harald Welte2f984ea2017-06-12 15:05:21 +0200178const struct gsm0503_mcs_code gsm0503_mcs_ul_codes[EGPRS_NUM_MCS] = {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700179 {
180 .mcs = EGPRS_MCS0,
181 },
182 {
183 .mcs = EGPRS_MCS1,
184 .hdr_len = 31,
185 .hdr_code_len = 117,
186 .hdr_punc_len = 80,
187 .hdr_conv = &gsm0503_mcs1_ul_hdr,
188 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
189
190 .data_len = 178,
191 .data_code_len = 588,
192 .data_punc_len = 372,
193 .data_conv = &gsm0503_mcs1,
194 .data_punc = {
195 gsm0503_puncture_mcs1_p1,
196 gsm0503_puncture_mcs1_p2,
197 NULL,
198 },
199 },
200 {
201 .mcs = EGPRS_MCS2,
202 .hdr_len = 31,
203 .hdr_code_len = 117,
204 .hdr_punc_len = 80,
205 .hdr_conv = &gsm0503_mcs1_ul_hdr,
206 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
207
208 .data_len = 226,
209 .data_code_len = 732,
210 .data_punc_len = 372,
211 .data_conv = &gsm0503_mcs2,
212 .data_punc = {
213 gsm0503_puncture_mcs2_p1,
214 gsm0503_puncture_mcs2_p2,
215 NULL,
216 },
217 },
218 {
219 .mcs = EGPRS_MCS3,
220 .hdr_len = 31,
221 .hdr_code_len = 117,
222 .hdr_punc_len = 80,
223 .hdr_conv = &gsm0503_mcs1_ul_hdr,
224 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
225
226 .data_len = 298,
227 .data_code_len = 948,
228 .data_punc_len = 372,
229 .data_conv = &gsm0503_mcs3,
230 .data_punc = {
231 gsm0503_puncture_mcs3_p1,
232 gsm0503_puncture_mcs3_p2,
233 gsm0503_puncture_mcs3_p3,
234 },
235 },
236 {
237 .mcs = EGPRS_MCS4,
238 .hdr_len = 31,
239 .hdr_code_len = 117,
240 .hdr_punc_len = 80,
241 .hdr_conv = &gsm0503_mcs1_ul_hdr,
242 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
243
244 .data_len = 354,
245 .data_code_len = 1116,
246 .data_punc_len = 372,
247 .data_conv = &gsm0503_mcs4,
248 .data_punc = {
249 gsm0503_puncture_mcs4_p1,
250 gsm0503_puncture_mcs4_p2,
251 gsm0503_puncture_mcs4_p3,
252 },
253 },
254 {
255 .mcs = EGPRS_MCS5,
256 .hdr_len = 37,
257 .hdr_code_len = 135,
258 .hdr_punc_len = 136,
259 .hdr_conv = &gsm0503_mcs5_ul_hdr,
260 .hdr_punc = NULL,
261
262 .data_len = 450,
263 .data_code_len = 1404,
264 .data_punc_len = 1248,
265 .data_conv = &gsm0503_mcs5,
266 .data_punc = {
267 gsm0503_puncture_mcs5_p1,
268 gsm0503_puncture_mcs5_p2,
269 NULL,
270 },
271 },
272 {
273 .mcs = EGPRS_MCS6,
274 .hdr_len = 37,
275 .hdr_code_len = 135,
276 .hdr_punc_len = 136,
277 .hdr_conv = &gsm0503_mcs5_ul_hdr,
278 .hdr_punc = NULL,
279
280 .data_len = 594,
281 .data_code_len = 1836,
282 .data_punc_len = 1248,
283 .data_conv = &gsm0503_mcs6,
284 .data_punc = {
285 gsm0503_puncture_mcs6_p1,
286 gsm0503_puncture_mcs6_p2,
287 NULL,
288 },
289 },
290 {
291 .mcs = EGPRS_MCS7,
292 .hdr_len = 46,
293 .hdr_code_len = 162,
294 .hdr_punc_len = 160,
295 .hdr_conv = &gsm0503_mcs7_ul_hdr,
296 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
297
298 .data_len = 900,
299 .data_code_len = 1404,
300 .data_punc_len = 612,
301 .data_conv = &gsm0503_mcs7,
302 .data_punc = {
303 gsm0503_puncture_mcs7_p1,
304 gsm0503_puncture_mcs7_p2,
305 gsm0503_puncture_mcs7_p3,
306 }
307 },
308 {
309 .mcs = EGPRS_MCS8,
310 .hdr_len = 46,
311 .hdr_code_len = 162,
312 .hdr_punc_len = 160,
313 .hdr_conv = &gsm0503_mcs7_ul_hdr,
314 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
315
316 .data_len = 1092,
317 .data_code_len = 1692,
318 .data_punc_len = 612,
319 .data_conv = &gsm0503_mcs8,
320 .data_punc = {
321 gsm0503_puncture_mcs8_p1,
322 gsm0503_puncture_mcs8_p2,
323 gsm0503_puncture_mcs8_p3,
324 }
325 },
326 {
327 .mcs = EGPRS_MCS9,
328 .hdr_len = 46,
329 .hdr_code_len = 162,
330 .hdr_punc_len = 160,
331 .hdr_conv = &gsm0503_mcs7_ul_hdr,
332 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
333
334 .data_len = 1188,
335 .data_code_len = 1836,
336 .data_punc_len = 612,
337 .data_conv = &gsm0503_mcs9,
338 .data_punc = {
339 gsm0503_puncture_mcs9_p1,
340 gsm0503_puncture_mcs9_p2,
341 gsm0503_puncture_mcs9_p3,
342 }
343 },
344};
345
346/*
347 * EGPRS DL coding parameters
348 */
Harald Welte2f984ea2017-06-12 15:05:21 +0200349const struct gsm0503_mcs_code gsm0503_mcs_dl_codes[EGPRS_NUM_MCS] = {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700350 {
351 .mcs = EGPRS_MCS0,
352 },
353 {
354 .mcs = EGPRS_MCS1,
355 .usf_len = 3,
356 .hdr_len = 28,
357 .hdr_code_len = 108,
358 .hdr_punc_len = 68,
359 .hdr_conv = &gsm0503_mcs1_dl_hdr,
360 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
361
362 .data_len = 178,
363 .data_code_len = 588,
364 .data_punc_len = 372,
365 .data_conv = &gsm0503_mcs1,
366 .data_punc = {
367 gsm0503_puncture_mcs1_p1,
368 gsm0503_puncture_mcs1_p2,
369 NULL,
370 },
371 },
372 {
373 .mcs = EGPRS_MCS2,
374 .usf_len = 3,
375 .hdr_len = 28,
376 .hdr_code_len = 108,
377 .hdr_punc_len = 68,
378 .hdr_conv = &gsm0503_mcs1_dl_hdr,
379 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
380
381 .data_len = 226,
382 .data_code_len = 732,
383 .data_punc_len = 372,
384 .data_conv = &gsm0503_mcs2,
385 .data_punc = {
386 gsm0503_puncture_mcs2_p1,
387 gsm0503_puncture_mcs2_p2,
388 NULL,
389 },
390 },
391 {
392 .mcs = EGPRS_MCS3,
393 .usf_len = 3,
394 .hdr_len = 28,
395 .hdr_code_len = 108,
396 .hdr_punc_len = 68,
397 .hdr_conv = &gsm0503_mcs1_dl_hdr,
398 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
399
400 .data_len = 298,
401 .data_code_len = 948,
402 .data_punc_len = 372,
403 .data_conv = &gsm0503_mcs3,
404 .data_punc = {
405 gsm0503_puncture_mcs3_p1,
406 gsm0503_puncture_mcs3_p2,
407 gsm0503_puncture_mcs3_p3,
408 },
409 },
410 {
411 .mcs = EGPRS_MCS4,
412 .usf_len = 3,
413 .hdr_len = 28,
414 .hdr_code_len = 108,
415 .hdr_punc_len = 68,
416 .hdr_conv = &gsm0503_mcs1_dl_hdr,
417 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
418
419 .data_len = 354,
420 .data_code_len = 1116,
421 .data_punc_len = 372,
422 .data_conv = &gsm0503_mcs4,
423 .data_punc = {
424 gsm0503_puncture_mcs4_p1,
425 gsm0503_puncture_mcs4_p2,
426 gsm0503_puncture_mcs4_p3,
427 },
428 },
429 {
430 .mcs = EGPRS_MCS5,
431 .usf_len = 3,
432 .hdr_len = 25,
433 .hdr_code_len = 99,
434 .hdr_punc_len = 100,
435 .hdr_conv = &gsm0503_mcs5_dl_hdr,
436 .hdr_punc = NULL,
437
438 .data_len = 450,
439 .data_code_len = 1404,
440 .data_punc_len = 1248,
441 .data_conv = &gsm0503_mcs5,
442 .data_punc = {
443 gsm0503_puncture_mcs5_p1,
444 gsm0503_puncture_mcs5_p2,
445 NULL,
446 },
447 },
448 {
449 .mcs = EGPRS_MCS6,
450 .usf_len = 3,
451 .hdr_len = 25,
452 .hdr_code_len = 99,
453 .hdr_punc_len = 100,
454 .hdr_conv = &gsm0503_mcs5_dl_hdr,
455 .hdr_punc = NULL,
456
457 .data_len = 594,
458 .data_code_len = 1836,
459 .data_punc_len = 1248,
460 .data_conv = &gsm0503_mcs6,
461 .data_punc = {
462 gsm0503_puncture_mcs6_p1,
463 gsm0503_puncture_mcs6_p2,
464 NULL,
465 },
466 },
467 {
468 .mcs = EGPRS_MCS7,
469 .usf_len = 3,
470 .hdr_len = 37,
471 .hdr_code_len = 135,
472 .hdr_punc_len = 124,
473 .hdr_conv = &gsm0503_mcs7_dl_hdr,
474 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
475
476 .data_len = 900,
477 .data_code_len = 1404,
478 .data_punc_len = 612,
479 .data_conv = &gsm0503_mcs7,
480 .data_punc = {
481 gsm0503_puncture_mcs7_p1,
482 gsm0503_puncture_mcs7_p2,
483 gsm0503_puncture_mcs7_p3,
484 }
485 },
486 {
487 .mcs = EGPRS_MCS8,
488 .usf_len = 3,
489 .hdr_len = 37,
490 .hdr_code_len = 135,
491 .hdr_punc_len = 124,
492 .hdr_conv = &gsm0503_mcs7_dl_hdr,
493 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
494
495 .data_len = 1092,
496 .data_code_len = 1692,
497 .data_punc_len = 612,
498 .data_conv = &gsm0503_mcs8,
499 .data_punc = {
500 gsm0503_puncture_mcs8_p1,
501 gsm0503_puncture_mcs8_p2,
502 gsm0503_puncture_mcs8_p3,
503 }
504 },
505 {
506 .mcs = EGPRS_MCS9,
507 .usf_len = 3,
508 .hdr_len = 37,
509 .hdr_code_len = 135,
510 .hdr_punc_len = 124,
511 .hdr_conv = &gsm0503_mcs7_dl_hdr,
512 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
513
514 .data_len = 1188,
515 .data_code_len = 1836,
516 .data_punc_len = 612,
517 .data_conv = &gsm0503_mcs9,
518 .data_punc = {
519 gsm0503_puncture_mcs9_p1,
520 gsm0503_puncture_mcs9_p2,
521 gsm0503_puncture_mcs9_p3,
522 }
523 },
524};
525
Alexander Chemeris147051f2018-07-14 21:02:29 +0200526/*! Convolutional Decode + compute BER for punctured codes
Harald Weltec6636782017-06-12 14:59:37 +0200527 * \param[in] code Description of Convolutional Code
528 * \param[in] input Input soft-bits (-127...127)
529 * \param[out] output bits
530 * \param[out] n_errors Number of bit-errors
531 * \param[out] n_bits_total Number of bits
Alexander Chemeris147051f2018-07-14 21:02:29 +0200532 * \param[in] data_punc Puncturing mask array. Can be NULL.
Harald Weltec6636782017-06-12 14:59:37 +0200533 */
Alexander Chemeris147051f2018-07-14 21:02:29 +0200534static int osmo_conv_decode_ber_punctured(const struct osmo_conv_code *code,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700535 const sbit_t *input, ubit_t *output,
Alexander Chemeris147051f2018-07-14 21:02:29 +0200536 int *n_errors, int *n_bits_total,
537 const uint8_t *data_punc)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700538{
Vadim Yanitskiy2a72d682024-01-31 20:56:53 +0700539 int res, coded_len;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700540 ubit_t recoded[EGPRS_DATA_C_MAX];
541
542 res = osmo_conv_decode(code, input, output);
543
Pau Espin Pedrold2737e62022-05-13 13:07:21 +0200544 if (!n_bits_total && !n_errors)
545 return res;
546
547 coded_len = osmo_conv_encode(code, output, recoded);
Pau Espin Pedrol6c58d152022-05-13 14:03:27 +0200548 OSMO_ASSERT(ARRAY_SIZE(recoded) >= coded_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700549
550 /* Count bit errors */
551 if (n_errors) {
552 *n_errors = 0;
Vadim Yanitskiy2a72d682024-01-31 20:56:53 +0700553 for (unsigned int i = 0; i < coded_len; i++) {
554 /* punctured bits do not count as bit errors */
555 if (data_punc != NULL && data_punc[i])
556 continue;
557 if (recoded[i] == 1 && input[i] < 0)
558 continue;
559 if (recoded[i] == 0 && input[i] > 0)
560 continue;
561 *n_errors += 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700562 }
563 }
564
565 if (n_bits_total)
566 *n_bits_total = coded_len;
567
568 return res;
569}
570
Alexander Chemeris147051f2018-07-14 21:02:29 +0200571/*! Convolutional Decode + compute BER for non-punctured codes
572 * \param[in] code Description of Convolutional Code
573 * \param[in] input Input soft-bits (-127...127)
574 * \param[out] output bits
575 * \param[out] n_errors Number of bit-errors
576 * \param[out] n_bits_total Number of bits
577 */
578static int osmo_conv_decode_ber(const struct osmo_conv_code *code,
579 const sbit_t *input, ubit_t *output,
580 int *n_errors, int *n_bits_total)
581{
582 return osmo_conv_decode_ber_punctured(code, input, output,
583 n_errors, n_bits_total, NULL);
584}
585
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200586/*! convenience wrapper for decoding coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200587 * \param[out] l2_data caller-allocated buffer for L2 Frame
588 * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3
589 * \param[out] n_errors Number of detected errors
590 * \param[out] n_bits_total Number of total coded bits
591 * \returns 0 on success; -1 on CRC error */
Harald Welteb9946d32017-06-12 09:40:16 +0200592static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700593 int *n_errors, int *n_bits_total)
594{
595 ubit_t conv[224];
596 int rv;
597
598 osmo_conv_decode_ber(&gsm0503_xcch, cB,
599 conv, n_errors, n_bits_total);
600
601 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
602 conv, 184, conv + 184);
603 if (rv)
604 return -1;
605
606 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
607
608 return 0;
609}
610
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200611/*! convenience wrapper for encoding to coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200612 * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3
Vadim Yanitskiyfed02e02023-05-15 23:25:05 +0700613 * \param[in] l2_data to-be-encoded L2 Frame
Harald Weltec6636782017-06-12 14:59:37 +0200614 * \returns 0 */
Harald Welteb9946d32017-06-12 09:40:16 +0200615static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700616{
617 ubit_t conv[224];
618
619 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
620
621 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
622
623 osmo_conv_encode(&gsm0503_xcch, conv, cB);
624
625 return 0;
626}
627
628/*
629 * GSM xCCH block transcoding
630 */
Harald Weltec6636782017-06-12 14:59:37 +0200631
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200632/*! Decoding of xCCH data from bursts to L2 frame
Harald Weltec6636782017-06-12 14:59:37 +0200633 * \param[out] l2_data caller-allocated output data buffer
634 * \param[in] bursts four GSM bursts in soft-bits
635 * \param[out] n_errors Number of detected errors
636 * \param[out] n_bits_total Number of total coded bits
637 */
Harald Welteb9946d32017-06-12 09:40:16 +0200638int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700639 int *n_errors, int *n_bits_total)
640{
641 sbit_t iB[456], cB[456];
642 int i;
643
644 for (i = 0; i < 4; i++)
645 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL);
646
647 gsm0503_xcch_deinterleave(cB, iB);
648
649 return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total);
650}
651
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200652/*! Encoding of xCCH data from L2 frame to bursts
Harald Weltec6636782017-06-12 14:59:37 +0200653 * \param[out] bursts caller-allocated burst data (unpacked bits)
654 * \param[in] l2_data L2 input data (MAC block)
655 * \returns 0
656 */
Harald Welteb9946d32017-06-12 09:40:16 +0200657int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700658{
659 ubit_t iB[456], cB[456], hl = 1, hn = 1;
660 int i;
661
662 _xcch_encode_cB(cB, l2_data);
663
664 gsm0503_xcch_interleave(cB, iB);
665
666 for (i = 0; i < 4; i++)
667 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn);
668
669 return 0;
670}
671
672/*
673 * EGPRS PDTCH UL block decoding
674 */
675
676/*
677 * Type 3 - MCS-1,2,3,4
678 * Unmapping and deinterleaving
679 */
680static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
681{
682 int i;
683 sbit_t iB[456], q[8];
684
685 for (i = 0; i < 4; i++) {
686 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
687 q + i * 2, q + i * 2 + 1);
688 }
689
690 gsm0503_mcs1_ul_deinterleave(hc, dc, iB);
691
692 return 0;
693}
694
695/*
696 * Type 2 - MCS-5,6
697 * Unmapping and deinterleaving
698 */
699static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
700{
701 int i;
702 sbit_t burst[348];
703 sbit_t hi[EGPRS_HDR_HC_MAX];
704 sbit_t di[EGPRS_DATA_DC_MAX];
705
706 for (i = 0; i < 4; i++) {
707 memcpy(burst, &bursts[i * 348], 348);
708
709 gsm0503_mcs5_burst_swap(burst);
710 gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i);
711 }
712
713 gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di);
714
715 return 0;
716}
717
718/*
719 * Type 1 - MCS-7,8,9
720 * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique
721 */
722static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc,
723 sbit_t *c1, sbit_t *c2, int msc)
724{
725 int i;
726 sbit_t burst[348];
727 sbit_t hi[EGPRS_HDR_HC_MAX];
728 sbit_t di[EGPRS_DATA_C1 * 2];
729
730 for (i = 0; i < 4; i++) {
731 memcpy(burst, &bursts[i * 348], 348);
732
733 gsm0503_mcs5_burst_swap(burst);
734 gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i);
735 }
736
737 if (msc == EGPRS_MCS7)
738 gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di);
739 else
740 gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di);
741
742 return 0;
743}
744
745/*
746 * Decode EGPRS UL header section
747 *
748 * 1. Depuncture
749 * 2. Convolutional decoding
750 * 3. CRC check
751 */
752static int _egprs_decode_hdr(const sbit_t *hc, int mcs,
753 union gprs_rlc_ul_hdr_egprs *hdr)
754{
755 sbit_t C[EGPRS_HDR_C_MAX];
756 ubit_t upp[EGPRS_HDR_UPP_MAX];
757 int i, j, rc;
Harald Welte2f984ea2017-06-12 15:05:21 +0200758 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700759
760 code = &gsm0503_mcs_ul_codes[mcs];
761
762 /* Skip depuncturing on MCS-5,6 header */
763 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
764 memcpy(C, hc, code->hdr_code_len);
765 goto hdr_conv_decode;
766 }
767
768 if (!code->hdr_punc) {
769 /* Invalid MCS-X header puncture matrix */
770 return -1;
771 }
772
773 i = code->hdr_code_len - 1;
774 j = code->hdr_punc_len - 1;
775
776 for (; i >= 0; i--) {
777 if (!code->hdr_punc[i])
778 C[i] = hc[j--];
779 else
780 C[i] = 0;
781 }
782
783hdr_conv_decode:
784 osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL);
785 rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp,
786 code->hdr_len, upp + code->hdr_len);
787 if (rc)
788 return -1;
789
790 osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1);
791
792 return 0;
793}
794
795/*
796 * Blind MCS header decoding based on burst length and CRC validation.
797 * Ignore 'q' value coding identification. This approach provides
798 * the strongest chance of header recovery.
799 */
800static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr,
801 const sbit_t *bursts, uint16_t nbits)
802{
803 int rc;
804 sbit_t hc[EGPRS_HDR_HC_MAX];
805
806 if (nbits == GSM0503_GPRS_BURSTS_NBITS) {
807 /* MCS-1,2,3,4 */
808 egprs_type3_unmap(bursts, hc, NULL);
809 rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr);
810 if (!rc)
811 return EGPRS_HDR_TYPE3;
812 } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) {
813 /* MCS-5,6 */
814 egprs_type2_unmap(bursts, hc, NULL);
815 rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr);
816 if (!rc)
817 return EGPRS_HDR_TYPE2;
818
819 /* MCS-7,8,9 */
820 egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7);
821 rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr);
822 if (!rc)
823 return EGPRS_HDR_TYPE1;
824 }
825
826 return -1;
827}
828
829/*
830 * Parse EGPRS UL header for coding and puncturing scheme (CPS)
831 *
832 * Type 1 - MCS-7,8,9
833 * Type 2 - MCS-5,6
834 * Type 3 - MCS-1,2,3,4
835 */
836static int egprs_parse_ul_cps(struct egprs_cps *cps,
837 union gprs_rlc_ul_hdr_egprs *hdr, int type)
838{
839 uint8_t bits;
840
841 switch (type) {
842 case EGPRS_HDR_TYPE1:
843 bits = hdr->type1.cps;
844 break;
845 case EGPRS_HDR_TYPE2:
846 bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi;
847 break;
848 case EGPRS_HDR_TYPE3:
849 bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi;
850 break;
851 default:
852 return -1;
853 }
854
855 return egprs_get_cps(cps, type, bits);
856}
857
858/*
859 * Decode EGPRS UL data section
860 *
861 * 1. Depuncture
862 * 2. Convolutional decoding
863 * 3. CRC check
864 * 4. Block combining (MCS-7,8,9 only)
865 */
Harald Welteb9946d32017-06-12 09:40:16 +0200866static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700867 int mcs, int p, int blk, int *n_errors, int *n_bits_total)
868{
869 ubit_t u[EGPRS_DATA_U_MAX];
870 sbit_t C[EGPRS_DATA_C_MAX];
871
872 int i, j, rc, data_len;
Harald Welte2f984ea2017-06-12 15:05:21 +0200873 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700874
875 if (blk && mcs < EGPRS_MCS7) {
876 /* Invalid MCS-X block state */
877 return -1;
878 }
879
880 code = &gsm0503_mcs_ul_codes[mcs];
881 if (!code->data_punc[p]) {
882 /* Invalid MCS-X data puncture matrix */
883 return -1;
884 }
885
886 /*
887 * MCS-1,6 - single block processing
888 * MCS-7,9 - dual block processing
889 */
890 if (mcs >= EGPRS_MCS7)
891 data_len = code->data_len / 2;
892 else
893 data_len = code->data_len;
894
895 i = code->data_code_len - 1;
896 j = code->data_punc_len - 1;
897
898 for (; i >= 0; i--) {
899 if (!code->data_punc[p][i])
900 C[i] = c[j--];
901 else
902 C[i] = 0;
903 }
904
Alexander Chemeris147051f2018-07-14 21:02:29 +0200905 osmo_conv_decode_ber_punctured(code->data_conv, C, u,
906 n_errors, n_bits_total, code->data_punc[p]);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700907 rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u,
908 data_len, u + data_len);
909 if (rc)
910 return -1;
911
912 /* Offsets output pointer on the second block of Type 1 MCS */
913 osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len,
914 u, 0, data_len, 1);
915
916 /* Return the number of bytes required for the bit message */
Maxdd75bac2017-06-13 15:07:01 +0200917 return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700918}
919
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200920/*! Decode EGPRS UL message
Harald Weltec6636782017-06-12 14:59:37 +0200921 * 1. Header section decoding
922 * 2. Extract CPS settings
923 * 3. Burst unmapping and deinterleaving
924 * 4. Data section decoding
925 * \param[out] l2_data caller-allocated buffer for L2 Frame
926 * \param[in] bursts burst input data as soft unpacked bits
927 * \param[in] nbits number of bits in \a bursts
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +0700928 * \param usf_p Uplink State Flag, FIXME: not implemented
Harald Weltec6636782017-06-12 14:59:37 +0200929 * \param[out] n_errors number of detected bit-errors
Alexander Chemerised7d2dd2018-07-14 21:06:27 +0200930 * \param[out] n_bits_total total number of decoded bits
Vadim Yanitskiyccf9aad2023-12-08 01:11:40 +0700931 * \returns number of bytes decoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +0200932int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700933 uint8_t *usf_p, int *n_errors, int *n_bits_total)
934{
935 sbit_t dc[EGPRS_DATA_DC_MAX];
936 sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
937 int type, rc;
938 struct egprs_cps cps;
939 union gprs_rlc_ul_hdr_egprs *hdr;
940
Alexander Chemeris50f7d742018-07-14 21:07:27 +0200941 if (n_errors)
942 *n_errors = 0;
943 if (n_bits_total)
944 *n_bits_total = 0;
945
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700946 if ((nbits != GSM0503_GPRS_BURSTS_NBITS) &&
947 (nbits != GSM0503_EGPRS_BURSTS_NBITS)) {
948 /* Invalid EGPRS bit length */
Maxc8cf8202017-05-22 16:07:04 +0200949 return -EOVERFLOW;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700950 }
951
952 hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data;
953 type = egprs_decode_hdr(hdr, bursts, nbits);
954 if (egprs_parse_ul_cps(&cps, hdr, type) < 0)
Maxc8cf8202017-05-22 16:07:04 +0200955 return -EIO;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700956
957 switch (cps.mcs) {
Maxc8cf8202017-05-22 16:07:04 +0200958 case EGPRS_MCS0:
959 return -ENOTSUP;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700960 case EGPRS_MCS1:
961 case EGPRS_MCS2:
962 case EGPRS_MCS3:
963 case EGPRS_MCS4:
964 egprs_type3_unmap(bursts, NULL, dc);
965 break;
966 case EGPRS_MCS5:
967 case EGPRS_MCS6:
968 egprs_type2_unmap(bursts, NULL, dc);
969 break;
970 case EGPRS_MCS7:
971 case EGPRS_MCS8:
972 case EGPRS_MCS9:
973 egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs);
974 break;
975 default:
976 /* Invalid MCS-X */
Maxc8cf8202017-05-22 16:07:04 +0200977 return -EINVAL;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700978 }
979
980 /* Decode MCS-X block, where X = cps.mcs */
981 if (cps.mcs < EGPRS_MCS7) {
982 rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0],
983 0, n_errors, n_bits_total);
984 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200985 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700986 } else {
Alexander Chemeris94443262018-07-14 21:09:54 +0200987 /* Bit counters for the second block */
988 int n_errors2, n_bits_total2;
989
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700990 /* MCS-7,8,9 block 1 */
991 rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0],
992 0, n_errors, n_bits_total);
993 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200994 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700995
996 /* MCS-7,8,9 block 2 */
997 rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1],
Alexander Chemeris94443262018-07-14 21:09:54 +0200998 1, &n_errors2, &n_bits_total2);
999 if (n_errors)
1000 *n_errors += n_errors2;
1001 if (n_bits_total)
1002 *n_bits_total += n_bits_total2;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001003 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +02001004 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001005 }
1006
1007 return rc;
1008}
1009
1010/*
1011 * GSM PDTCH block transcoding
1012 */
1013
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001014/*! Decode GPRS PDTCH
Harald Weltec6636782017-06-12 14:59:37 +02001015 * \param[out] l2_data caller-allocated buffer for L2 Frame
1016 * \param[in] bursts burst input data as soft unpacked bits
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001017 * \param[out] usf_p Uplink State Flag, only relevant for DL blocks
Harald Weltec6636782017-06-12 14:59:37 +02001018 * \param[out] n_errors number of detected bit-errors
Vadim Yanitskiy97374512024-01-31 20:56:28 +07001019 * \param[out] n_bits_total total number of decoded bits
Vadim Yanitskiyccf9aad2023-12-08 01:11:40 +07001020 * \returns number of bytes decoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001021int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001022 int *n_errors, int *n_bits_total)
1023{
1024 sbit_t iB[456], cB[676], hl_hn[8];
1025 ubit_t conv[456];
1026 int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */
1027
1028 for (i = 0; i < 4; i++)
1029 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
1030 hl_hn + i * 2, hl_hn + i * 2 + 1);
1031
1032 for (i = 0; i < 4; i++) {
1033 for (j = 0, k = 0; j < 8; j++)
1034 k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j]));
1035
1036 if (i == 0 || k < best) {
1037 best = k;
1038 cs = i + 1;
1039 }
1040 }
1041
1042 gsm0503_xcch_deinterleave(cB, iB);
1043
1044 switch (cs) {
1045 case 1:
1046 osmo_conv_decode_ber(&gsm0503_xcch, cB,
1047 conv, n_errors, n_bits_total);
1048
Vadim Yanitskiyc58e4852023-12-08 03:35:50 +07001049 /* the three USF bits d(0),d(1),d(2) are *not* precoded */
1050 if (usf_p)
1051 *usf_p = (conv[0] << 2) | (conv[1] << 1) | (conv[2] << 0);
1052
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001053 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
1054 conv, 184, conv + 184);
1055 if (rv)
1056 return -1;
1057
1058 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
1059
1060 return 23;
1061 case 2:
1062 for (i = 587, j = 455; i >= 0; i--) {
1063 if (!gsm0503_puncture_cs2[i])
1064 cB[i] = cB[j--];
1065 else
1066 cB[i] = 0;
1067 }
1068
1069 osmo_conv_decode_ber(&gsm0503_cs2_np, cB,
1070 conv, n_errors, n_bits_total);
1071
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001072 /* 5.1.2.2 a) the three USF bits d(0),d(1),d(2) are precoded into six bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001073 for (i = 0; i < 8; i++) {
1074 for (j = 0, k = 0; j < 6; j++)
1075 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1076
1077 if (i == 0 || k < best) {
1078 best = k;
1079 usf = i;
1080 }
1081 }
1082
1083 conv[3] = usf & 1;
1084 conv[4] = (usf >> 1) & 1;
1085 conv[5] = (usf >> 2) & 1;
1086 if (usf_p)
1087 *usf_p = usf;
1088
1089 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1090 conv + 3, 271, conv + 3 + 271);
1091 if (rv)
1092 return -1;
1093
1094 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1095
1096 return 34;
1097 case 3:
1098 for (i = 675, j = 455; i >= 0; i--) {
1099 if (!gsm0503_puncture_cs3[i])
1100 cB[i] = cB[j--];
1101 else
1102 cB[i] = 0;
1103 }
1104
1105 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
1106 conv, n_errors, n_bits_total);
1107
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001108 /* 5.1.3.2 a) the three USF bits d(0),d(1),d(2) are precoded into six bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001109 for (i = 0; i < 8; i++) {
1110 for (j = 0, k = 0; j < 6; j++)
1111 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1112
1113 if (i == 0 || k < best) {
1114 best = k;
1115 usf = i;
1116 }
1117 }
1118
1119 conv[3] = usf & 1;
1120 conv[4] = (usf >> 1) & 1;
1121 conv[5] = (usf >> 2) & 1;
1122 if (usf_p)
1123 *usf_p = usf;
1124
1125 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1126 conv + 3, 315, conv + 3 + 315);
1127 if (rv)
1128 return -1;
1129
1130 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
1131
1132 return 40;
1133 case 4:
1134 for (i = 12; i < 456; i++)
1135 conv[i] = (cB[i] < 0) ? 1 : 0;
1136
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001137 /* 5.1.4.2 a) the three USF bits d(0),d(1),d(2) are precoded into twelve bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001138 for (i = 0; i < 8; i++) {
1139 for (j = 0, k = 0; j < 12; j++)
1140 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1141
1142 if (i == 0 || k < best) {
1143 best = k;
1144 usf = i;
1145 }
1146 }
1147
1148 conv[9] = usf & 1;
1149 conv[10] = (usf >> 1) & 1;
1150 conv[11] = (usf >> 2) & 1;
1151 if (usf_p)
1152 *usf_p = usf;
1153
1154 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1155 conv + 9, 431, conv + 9 + 431);
1156 if (rv) {
1157 *n_bits_total = 456 - 12;
1158 *n_errors = *n_bits_total;
1159 return -1;
1160 }
1161
1162 *n_bits_total = 456 - 12;
1163 *n_errors = 0;
1164
1165 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1166
1167 return 54;
1168 default:
1169 *n_bits_total = 0;
1170 *n_errors = 0;
1171 break;
1172 }
1173
1174 return -1;
1175}
1176
1177/*
Pau Espin Pedrolf62f0732020-04-07 13:12:11 +02001178 * EGPRS PDTCH DL block encoding
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001179 */
Harald Welteb9946d32017-06-12 09:40:16 +02001180static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001181{
1182 int i;
1183 ubit_t iB[456];
1184 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1185
Pau Espin Pedrol63ebc362020-04-07 13:15:36 +02001186 gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001187
1188 for (i = 0; i < 4; i++) {
1189 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1190 hl_hn + i * 2, hl_hn + i * 2 + 1);
1191 }
1192
1193 return 0;
1194}
1195
Harald Welteb9946d32017-06-12 09:40:16 +02001196static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001197{
1198 int i;
1199 const ubit_t *up;
1200 ubit_t hi[EGPRS_HDR_HC_MAX];
1201 ubit_t di[EGPRS_DATA_DC_MAX];
1202
1203 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1204 up = gsm0503_mcs5_usf_precode_table[usf];
1205
1206 for (i = 0; i < 4; i++) {
1207 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1208 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1209 }
1210
1211 return 0;
1212}
1213
Harald Welteb9946d32017-06-12 09:40:16 +02001214static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1215 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001216{
1217 int i;
1218 const ubit_t *up;
1219 ubit_t hi[EGPRS_HDR_HC_MAX];
1220 ubit_t di[EGPRS_DATA_C1 * 2];
1221
1222 if (mcs == EGPRS_MCS7)
1223 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1224 else
1225 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1226
1227 up = gsm0503_mcs5_usf_precode_table[usf];
1228
1229 for (i = 0; i < 4; i++) {
1230 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1231 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1232 }
1233
1234 return 0;
1235}
1236
Harald Welteb9946d32017-06-12 09:40:16 +02001237static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001238{
1239 int i, j;
1240 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001241 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001242
1243 code = &gsm0503_mcs_dl_codes[mcs];
1244
1245 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1246 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1247 code->hdr_len, upp + code->hdr_len);
1248
1249 osmo_conv_encode(code->hdr_conv, upp, C);
1250
1251 /* MCS-5,6 header direct puncture instead of table */
1252 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1253 memcpy(hc, C, code->hdr_code_len);
1254 hc[99] = hc[98];
1255 return 0;
1256 }
1257
1258 if (!code->hdr_punc) {
1259 /* Invalid MCS-X header puncture matrix */
1260 return -1;
1261 }
1262
1263 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1264 if (!code->hdr_punc[i])
1265 hc[j++] = C[i];
1266 }
1267
1268 return 0;
1269}
1270
Harald Welteb9946d32017-06-12 09:40:16 +02001271static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001272 int mcs, int p, int blk)
1273{
1274 int i, j, data_len;
1275 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001276 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001277
1278 code = &gsm0503_mcs_dl_codes[mcs];
1279
1280 /*
1281 * Dual block - MCS-7,8,9
1282 * Single block - MCS-1,2,3,4,5,6
1283 */
1284 if (mcs >= EGPRS_MCS7)
1285 data_len = code->data_len / 2;
1286 else
1287 data_len = code->data_len;
1288
1289 osmo_pbit2ubit_ext(u, 0, l2_data,
1290 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1291
1292 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1293
1294 osmo_conv_encode(code->data_conv, u, C);
1295
1296 if (!code->data_punc[p]) {
1297 /* Invalid MCS-X data puncture matrix */
1298 return -1;
1299 }
1300
1301 for (i = 0, j = 0; i < code->data_code_len; i++) {
1302 if (!code->data_punc[p][i])
1303 c[j++] = C[i];
1304 }
1305
1306 return 0;
1307}
1308
1309/*
1310 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1311 *
1312 * Type 1 - MCS-7,8,9
1313 * Type 2 - MCS-5,6
1314 * Type 3 - MCS-1,2,3,4
1315 */
1316static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001317 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001318{
1319 uint8_t bits;
1320
1321 switch (type) {
1322 case EGPRS_HDR_TYPE1:
1323 bits = hdr->type1.cps;
1324 break;
1325 case EGPRS_HDR_TYPE2:
1326 bits = hdr->type2.cps;
1327 break;
1328 case EGPRS_HDR_TYPE3:
1329 bits = hdr->type3.cps;
1330 break;
1331 default:
1332 return -1;
1333 }
1334
1335 return egprs_get_cps(cps, type, bits);
1336}
1337
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001338/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001339 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1340 * \param[in] l2_data L2 (MAC) block to be encoded
1341 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001342 * \returns number of bits encoded; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001343int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001344 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001345{
1346 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1347 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1348 uint8_t mcs;
1349 struct egprs_cps cps;
1350 union gprs_rlc_dl_hdr_egprs *hdr;
1351
1352 switch (l2_len) {
1353 case 27:
1354 mcs = EGPRS_MCS1;
1355 break;
1356 case 33:
1357 mcs = EGPRS_MCS2;
1358 break;
1359 case 42:
1360 mcs = EGPRS_MCS3;
1361 break;
1362 case 49:
1363 mcs = EGPRS_MCS4;
1364 break;
1365 case 60:
1366 mcs = EGPRS_MCS5;
1367 break;
1368 case 78:
1369 mcs = EGPRS_MCS6;
1370 break;
1371 case 118:
1372 mcs = EGPRS_MCS7;
1373 break;
1374 case 142:
1375 mcs = EGPRS_MCS8;
1376 break;
1377 case 154:
1378 mcs = EGPRS_MCS9;
1379 break;
1380 default:
1381 return -1;
1382 }
1383
1384 /* Read header for USF and puncturing matrix selection. */
1385 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1386
1387 switch (mcs) {
1388 case EGPRS_MCS1:
1389 case EGPRS_MCS2:
1390 case EGPRS_MCS3:
1391 case EGPRS_MCS4:
1392 /* Check for valid CPS and matching MCS to message size */
1393 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1394 (cps.mcs != mcs))
1395 goto bad_header;
1396
1397 egprs_encode_hdr(hc, l2_data, mcs);
1398 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1399 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1400 break;
1401 case EGPRS_MCS5:
1402 case EGPRS_MCS6:
1403 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1404 (cps.mcs != mcs))
1405 goto bad_header;
1406
1407 egprs_encode_hdr(hc, l2_data, mcs);
1408 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1409 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1410 break;
1411 case EGPRS_MCS7:
1412 case EGPRS_MCS8:
1413 case EGPRS_MCS9:
1414 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1415 (cps.mcs != mcs))
1416 goto bad_header;
1417
1418 egprs_encode_hdr(hc, l2_data, mcs);
1419 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1420 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1421 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1422 break;
1423 }
1424
1425 return mcs >= EGPRS_MCS5 ?
1426 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1427
1428bad_header:
1429 /* Invalid EGPRS MCS-X header */
1430 return -1;
1431}
1432
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001433/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001434 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1435 * \param[in] l2_data L2 (MAC) block to be encoded
1436 * \param[in] l2_len length of l2_data in bytes, used to determine CS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001437 * \returns number of bits encoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001438int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001439{
1440 ubit_t iB[456], cB[676];
1441 const ubit_t *hl_hn;
1442 ubit_t conv[334];
1443 int i, j, usf;
1444
1445 switch (l2_len) {
1446 case 23:
1447 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1448
1449 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1450
1451 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1452
1453 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1454
1455 break;
1456 case 34:
1457 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1458 usf = l2_data[0] & 0x7;
1459
1460 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1461 271, conv + 3 + 271);
1462
1463 memcpy(conv, gsm0503_usf2six[usf], 6);
1464
1465 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1466
1467 for (i = 0, j = 0; i < 588; i++)
1468 if (!gsm0503_puncture_cs2[i])
1469 cB[j++] = cB[i];
1470
1471 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1472
1473 break;
1474 case 40:
1475 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1476 usf = l2_data[0] & 0x7;
1477
1478 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1479 315, conv + 3 + 315);
1480
1481 memcpy(conv, gsm0503_usf2six[usf], 6);
1482
1483 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1484
1485 for (i = 0, j = 0; i < 676; i++)
1486 if (!gsm0503_puncture_cs3[i])
1487 cB[j++] = cB[i];
1488
1489 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1490
1491 break;
1492 case 54:
1493 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1494 usf = l2_data[0] & 0x7;
1495
1496 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1497 431, cB + 9 + 431);
1498
1499 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1500
1501 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1502
1503 break;
1504 default:
1505 return -1;
1506 }
1507
1508 gsm0503_xcch_interleave(cB, iB);
1509
1510 for (i = 0; i < 4; i++) {
1511 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1512 hl_hn + i * 2, hl_hn + i * 2 + 1);
1513 }
1514
1515 return GSM0503_GPRS_BURSTS_NBITS;
1516}
1517
1518/*
1519 * GSM TCH/F FR/EFR transcoding
1520 */
1521
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001522/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001523 * \param[out] tch_data Codec frame in RTP format
1524 * \param[in] b_bits Codec frame in 'native' format
1525 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001526static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001527 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001528{
1529 int i, j, k, l, o;
1530
1531 tch_data[0] = 0xd << 4;
1532 memset(tch_data + 1, 0, 32);
1533
1534 if (net_order) {
1535 for (i = 0, j = 4; i < 260; i++, j++)
1536 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1537
1538 return;
1539 }
1540
1541 /* reassemble d-bits */
1542 i = 0; /* counts bits */
1543 j = 4; /* counts output bits */
1544 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1545 l = 0; /* counts element bits */
1546 o = 0; /* offset input bits */
1547 while (i < 260) {
1548 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1549 if (--k < 0) {
1550 o += gsm0503_gsm_fr_map[l];
1551 k = gsm0503_gsm_fr_map[++l]-1;
1552 }
1553 i++;
1554 j++;
1555 }
1556}
1557
1558static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001559 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001560{
1561 int i, j, k, l, o;
1562
1563 if (net_order) {
1564 for (i = 0, j = 4; i < 260; i++, j++)
1565 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1566
1567 return;
1568 }
1569
1570 i = 0; /* counts bits */
1571 j = 4; /* counts input bits */
1572 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1573 l = 0; /* counts element bits */
1574 o = 0; /* offset output bits */
1575 while (i < 260) {
1576 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1577 if (--k < 0) {
1578 o += gsm0503_gsm_fr_map[l];
1579 k = gsm0503_gsm_fr_map[++l] - 1;
1580 }
1581 i++;
1582 j++;
1583 }
1584}
1585
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001586/* assemble a HR codec frame in the canonical format of ETSI TS 101 318 */
Harald Welteb9946d32017-06-12 09:40:16 +02001587static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001588{
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001589 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001590
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001591 memset(tch_data, 0, GSM_HR_BYTES);
1592 for (i = 0; i < 112; i++)
1593 tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7)));
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001594}
1595
Harald Welteb9946d32017-06-12 09:40:16 +02001596static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001597{
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001598 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001599
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001600 for (i = 0; i < 112; i++)
1601 b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001602}
1603
Harald Weltec6636782017-06-12 14:59:37 +02001604/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001605static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001606{
1607 int i, j;
1608
1609 tch_data[0] = 0xc << 4;
1610 memset(tch_data + 1, 0, 30);
1611
1612 for (i = 0, j = 4; i < 244; i++, j++)
1613 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1614}
1615
Harald Welteb9946d32017-06-12 09:40:16 +02001616static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001617{
1618 int i, j;
1619
1620 for (i = 0, j = 4; i < 244; i++, j++)
1621 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1622}
1623
Harald Weltec6636782017-06-12 14:59:37 +02001624/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001625static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001626{
1627 int i, j;
1628
1629 memset(tch_data, 0, (len + 7) >> 3);
1630
1631 for (i = 0, j = 0; i < len; i++, j++)
1632 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1633}
1634
Harald Welteb9946d32017-06-12 09:40:16 +02001635static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001636{
1637 int i, j;
1638
1639 for (i = 0, j = 0; i < len; i++, j++)
1640 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1641}
1642
Philipp Maier898c9c62020-02-06 14:25:01 +01001643/* Append STI and MI bits to the SID_UPDATE frame, see also
1644 * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */
1645static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi)
1646{
1647 /* Zero out the space that had been used by the CRC14 */
1648 memset(sid_update + 35, 0, 14);
1649
1650 /* Append STI and MI parameters */
1651 sid_update[35] = sti & 1;
1652 sid_update[36] = mi & 1;
1653 sid_update[37] = mi >> 1 & 1;
1654 sid_update[38] = mi >> 2 & 1;
1655}
1656
1657/* Extract a SID UPDATE fram the sbits of an FR AMR frame */
1658static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits)
1659{
1660
1661 unsigned int i;
1662
1663 sbits += 32;
1664
1665 for (i = 0; i < 53; i++) {
1666 sid_update[0] = sbits[0];
1667 sid_update[1] = sbits[1];
1668 sid_update[2] = sbits[2];
1669 sid_update[3] = sbits[3];
1670 sid_update += 4;
1671 sbits += 8;
1672 }
1673
1674}
1675
Harald Weltec6636782017-06-12 14:59:37 +02001676/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001677static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001678{
1679 int i;
1680
1681 for (i = 0; i < 260; i++)
1682 b_bits[gsm610_bitorder[i]] = d_bits[i];
1683}
1684
Harald Weltec6636782017-06-12 14:59:37 +02001685/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001686static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001687{
1688 int i;
1689
1690 for (i = 0; i < 260; i++)
1691 d_bits[i] = b_bits[gsm610_bitorder[i]];
1692}
1693
Harald Weltec6636782017-06-12 14:59:37 +02001694/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001695static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001696{
1697 int i;
1698
1699 const uint16_t *map;
1700
1701 if (!d_bits[93] && !d_bits[94])
1702 map = gsm620_unvoiced_bitorder;
1703 else
1704 map = gsm620_voiced_bitorder;
1705
1706 for (i = 0; i < 112; i++)
1707 b_bits[map[i]] = d_bits[i];
1708}
1709
Harald Weltec6636782017-06-12 14:59:37 +02001710/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001711static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001712{
1713 int i;
1714 const uint16_t *map;
1715
1716 if (!b_bits[34] && !b_bits[35])
1717 map = gsm620_unvoiced_bitorder;
1718 else
1719 map = gsm620_voiced_bitorder;
1720
1721 for (i = 0; i < 112; i++)
1722 d_bits[i] = b_bits[map[i]];
1723}
1724
Harald Weltec6636782017-06-12 14:59:37 +02001725/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001726static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001727{
1728 int i;
1729
1730 for (i = 0; i < 260; i++)
1731 b_bits[gsm660_bitorder[i]] = d_bits[i];
1732}
1733
Harald Weltec6636782017-06-12 14:59:37 +02001734/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001735static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001736{
1737 int i;
1738
1739 for (i = 0; i < 260; i++)
1740 d_bits[i] = b_bits[gsm660_bitorder[i]];
1741}
1742
Harald Weltec6636782017-06-12 14:59:37 +02001743/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001744static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001745{
1746 int i;
1747
1748 for (i = 0; i < 65; i++)
1749 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1750}
1751
Harald Welteb9946d32017-06-12 09:40:16 +02001752static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001753{
1754 int i;
1755
1756 for (i = 0; i < 91; i++) {
1757 d[i << 1] = u[i];
1758 d[(i << 1) + 1] = u[184 - i];
1759 }
1760
1761 for (i = 0; i < 3; i++)
1762 p[i] = u[91 + i];
1763}
1764
Harald Welteb9946d32017-06-12 09:40:16 +02001765static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001766{
1767 int i;
1768
1769 for (i = 0; i < 91; i++) {
1770 u[i] = d[i << 1];
1771 u[184 - i] = d[(i << 1) + 1];
1772 }
1773
1774 for (i = 0; i < 3; i++)
1775 u[91 + i] = p[i];
1776}
1777
Harald Welteb9946d32017-06-12 09:40:16 +02001778static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001779{
1780 memcpy(d, u, 95);
1781 memcpy(p, u + 95, 3);
1782}
1783
Harald Welteb9946d32017-06-12 09:40:16 +02001784static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001785{
1786 memcpy(u, d, 95);
1787 memcpy(u + 95, p, 3);
1788}
1789
Harald Welteb9946d32017-06-12 09:40:16 +02001790static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001791{
1792 memcpy(w, s, 71);
1793 w[71] = w[72] = s[69];
1794 memcpy(w + 73, s + 71, 50);
1795 w[123] = w[124] = s[119];
1796 memcpy(w + 125, s + 121, 53);
1797 w[178] = w[179] = s[172];
1798 memcpy(w + 180, s + 174, 50);
1799 w[230] = w[231] = s[222];
1800 memcpy(w + 232, s + 224, 20);
1801 memcpy(w + 252, p, 8);
1802}
1803
Harald Welteb9946d32017-06-12 09:40:16 +02001804static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001805{
1806 int sum;
1807
1808 memcpy(s, w, 71);
1809 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001810 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001811 memcpy(s + 71, w + 73, 50);
1812 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001813 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001814 memcpy(s + 121, w + 125, 53);
1815 sum = s[172] + w[178] + w[179];
Mychaela N. Falconiaf3e47452023-05-17 18:58:40 +00001816 s[172] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001817 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001818 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001819 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001820 memcpy(s + 224, w + 232, 20);
1821 memcpy(p, w + 252, 8);
1822}
1823
Harald Welteb9946d32017-06-12 09:40:16 +02001824static void tch_amr_merge(ubit_t *u, const ubit_t *d, const ubit_t *p, int len, int prot)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001825{
1826 memcpy(u, d, prot);
1827 memcpy(u + prot, p, 6);
1828 memcpy(u + prot + 6, d + prot, len - prot);
1829}
1830
Harald Welteb9946d32017-06-12 09:40:16 +02001831static void tch_amr_unmerge(ubit_t *d, ubit_t *p, const ubit_t *u, int len, int prot)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001832{
1833 memcpy(d, u, prot);
1834 memcpy(p, u + prot, 6);
1835 memcpy(d + prot, u + prot + 6, len - prot);
1836}
1837
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001838/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001839 * \param[out] tch_data Codec frame in RTP payload format
1840 * \param[in] bursts buffer containing the symbols of 8 bursts
1841 * \param[in] net_order FIXME
1842 * \param[in] efr Is this channel using EFR (1) or FR (0)
1843 * \param[out] n_errors Number of detected bit errors
1844 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001845 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001846int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001847 int net_order, int efr, int *n_errors, int *n_bits_total)
1848{
1849 sbit_t iB[912], cB[456], h;
1850 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1851 int i, rv, len, steal = 0;
1852
Harald Weltec6636782017-06-12 14:59:37 +02001853 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001854 for (i = 0; i < 8; i++) {
1855 gsm0503_tch_burst_unmap(&iB[i * 114],
1856 &bursts[i * 116], &h, i >> 2);
1857 steal -= h;
1858 }
Harald Weltec6636782017-06-12 14:59:37 +02001859 /* we now have the bits of the four bursts (interface 4 in
1860 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001861
1862 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001863 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001864
1865 if (steal > 0) {
1866 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1867 if (rv) {
1868 /* Error decoding FACCH frame */
1869 return -1;
1870 }
1871
Vadim Yanitskiyc0305b62023-05-20 16:33:38 +07001872 return GSM_MACBLOCK_LEN;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001873 }
1874
1875 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001876 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001877
Harald Weltec6636782017-06-12 14:59:37 +02001878 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001879 tch_fr_unreorder(d, p, conv);
1880
1881 for (i = 0; i < 78; i++)
1882 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1883
Harald Weltec6636782017-06-12 14:59:37 +02001884 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001885 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1886 if (rv) {
1887 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1888 return -1;
1889 }
1890
1891 if (efr) {
1892 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001893 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001894
1895 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001896 /* we now have the data delivered to the preliminary
1897 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001898
Harald Weltec6636782017-06-12 14:59:37 +02001899 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001900 tch_efr_protected(s, b);
1901
Harald Weltec6636782017-06-12 14:59:37 +02001902 /* perform CRC-8 on 65 most important bits (50 bits of
1903 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001904 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1905 if (rv) {
1906 /* Error checking CRC8 for the EFR part of an EFR frame */
1907 return -1;
1908 }
1909
1910 tch_efr_reassemble(tch_data, s);
1911
1912 len = GSM_EFR_BYTES;
1913 } else {
1914 tch_fr_d_to_b(w, d);
1915
1916 tch_fr_reassemble(tch_data, w, net_order);
1917
1918 len = GSM_FR_BYTES;
1919 }
1920
1921 return len;
1922}
1923
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001924/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001925 * \param[out] bursts caller-allocated output buffer for bursts bits
1926 * \param[in] tch_data Codec input data in RTP payload format
1927 * \param[in] len Length of \a tch_data in bytes
1928 * \param[in] net_order FIXME
1929 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001930int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001931 int len, int net_order)
1932{
1933 ubit_t iB[912], cB[456], h;
1934 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1935 int i;
1936
1937 switch (len) {
1938 case GSM_EFR_BYTES: /* TCH EFR */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001939 tch_efr_disassemble(s, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001940 tch_efr_protected(s, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001941 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001942 tch_efr_reorder(w, s, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001943 tch_efr_w_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001944 goto coding_efr_fr;
1945 case GSM_FR_BYTES: /* TCH FR */
1946 tch_fr_disassemble(w, tch_data, net_order);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001947 tch_fr_b_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001948coding_efr_fr:
1949 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001950 tch_fr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001951 memcpy(cB + 378, d + 182, 78);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001952 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001953 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001954 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00001955 case 0: /* no data, induce BFI in the receiver */
1956 /* Do the same thing that sysmoBTS PHY does when fed a 0-length
1957 * payload for DL: set all u(k) bits to 0, and do the same
1958 * with all class 2 bits. This operation is NOT the same as
1959 * an FR codec frame of all zero bits: with all-zeros d(k) input
1960 * the CRC3 function will produce 111 output, whereas we
1961 * transmit 000 in those parity bits too. The result will be
1962 * an induced BFI (bad frame indication) condition in the
1963 * receiver, for both TCH/FS and TCH/EFS decoders. */
1964 memset(conv, 0, sizeof(conv));
1965 memset(cB + 378, 0, 78);
1966 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1967 h = 0;
1968 break;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001969 case GSM_MACBLOCK_LEN: /* FACCH */
1970 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001971 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001972 break;
1973 default:
1974 return -1;
1975 }
1976
1977 gsm0503_tch_fr_interleave(cB, iB);
1978
1979 for (i = 0; i < 8; i++) {
1980 gsm0503_tch_burst_map(&iB[i * 114],
1981 &bursts[i * 116], &h, i >> 2);
1982 }
1983
1984 return 0;
1985}
1986
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001987/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001988 * \param[out] tch_data Codec frame in TS 101 318 canonical format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07001989 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02001990 * \param[in] odd Odd (1) or even (0) frame number
1991 * \param[out] n_errors Number of detected bit errors
1992 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001993 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001994int gsm0503_tch_hr_decode2(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001995 int *n_errors, int *n_bits_total)
1996{
1997 sbit_t iB[912], cB[456], h;
1998 ubit_t conv[98], b[112], d[112], p[3];
1999 int i, rv, steal = 0;
2000
2001 /* Only unmap the stealing bits */
2002 if (!odd) {
2003 for (i = 0; i < 4; i++) {
2004 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2005 steal -= h;
2006 }
2007
Mychaela N. Falconia920c0ed2023-05-23 14:06:53 +00002008 for (i = 2; i < 6; i++) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002009 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2010 steal -= h;
2011 }
2012 }
2013
2014 /* If we found a stole FACCH, but only at correct alignment */
2015 if (steal > 0) {
2016 for (i = 0; i < 6; i++) {
2017 gsm0503_tch_burst_unmap(&iB[i * 114],
2018 &bursts[i * 116], NULL, i >> 2);
2019 }
2020
2021 for (i = 2; i < 4; i++) {
2022 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2023 &bursts[i * 116], NULL, 1);
2024 }
2025
2026 gsm0503_tch_fr_deinterleave(cB, iB);
2027
2028 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2029 if (rv) {
2030 /* Error decoding FACCH frame */
2031 return -1;
2032 }
2033
2034 return GSM_MACBLOCK_LEN;
2035 }
2036
2037 for (i = 0; i < 4; i++) {
2038 gsm0503_tch_burst_unmap(&iB[i * 114],
2039 &bursts[i * 116], NULL, i >> 1);
2040 }
2041
2042 gsm0503_tch_hr_deinterleave(cB, iB);
2043
2044 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
2045
2046 tch_hr_unreorder(d, p, conv);
2047
2048 for (i = 0; i < 17; i++)
2049 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
2050
2051 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2052 if (rv) {
2053 /* Error checking CRC8 for an HR frame */
2054 return -1;
2055 }
2056
2057 tch_hr_d_to_b(b, d);
2058
2059 tch_hr_reassemble(tch_data, b);
2060
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002061 return GSM_HR_BYTES;
2062}
2063
2064/*! Perform channel decoding of a HR(v1) channel according TS 05.03,
2065 * deprecated legacy API.
2066 * \param[out] tch_data Codec frame in pseudo-RFC5993 format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002067 * \param[in] bursts buffer containing the symbols of 6 bursts
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002068 * \param[in] odd Odd (1) or even (0) frame number
2069 * \param[out] n_errors Number of detected bit errors
2070 * \param[out] n_bits_total Total number of bits
2071 * \returns length of bytes used in \a tch_data output buffer; negative on error
2072 *
2073 * The HR1 codec frame format returned by this function is pseudo-RFC5993,
2074 * not true RFC 5993, as there is no SID classification being done
2075 * and the FT bits in the ToC octet are always set to 0 - but this
2076 * arguably-bogus format is the legacy public API.
2077 */
2078int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
2079 int *n_errors, int *n_bits_total)
2080{
2081 int rc;
2082
2083 rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors,
2084 n_bits_total);
2085 if (rc != GSM_HR_BYTES)
2086 return rc;
2087 memmove(tch_data + 1, tch_data, GSM_HR_BYTES);
2088 tch_data[0] = 0x00; /* FT=0, note absence of SID classification */
2089 return GSM_HR_BYTES_RTP_RFC5993;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002090}
2091
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002092/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002093 * \param[out] bursts caller-allocated output buffer for bursts bits
2094 * \param[in] tch_data Codec input data in RTP payload format
2095 * \param[in] len Length of \a tch_data in bytes
2096 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002097int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002098{
2099 ubit_t iB[912], cB[456], h;
2100 ubit_t conv[98], b[112], d[112], p[3];
2101 int i;
2102
2103 switch (len) {
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00002104 case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */
2105 tch_data++;
2106 /* fall-through */
2107 case GSM_HR_BYTES: /* TCH HR in "pure" form */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002108 tch_hr_disassemble(b, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002109 tch_hr_b_to_d(d, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002110 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002111 tch_hr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002112 memcpy(cB + 211, d + 95, 17);
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002113hr_conv_coding:
2114 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002115 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002116 gsm0503_tch_hr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002117 for (i = 0; i < 4; i++) {
2118 gsm0503_tch_burst_map(&iB[i * 114],
2119 &bursts[i * 116], &h, i >> 1);
2120 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002121 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002122 case 0: /* no data, induce BFI in the receiver */
2123 /* see comments in gsm0503_tch_fr_encode() - same deal here */
2124 memset(conv, 0, sizeof(conv));
2125 memset(cB + 211, 0, 17);
2126 goto hr_conv_coding;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002127 case GSM_MACBLOCK_LEN: /* FACCH */
2128 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002129 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002130 gsm0503_tch_fr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002131 for (i = 0; i < 6; i++) {
2132 gsm0503_tch_burst_map(&iB[i * 114],
2133 &bursts[i * 116], &h, i >> 2);
2134 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002135 for (i = 2; i < 4; i++) {
2136 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2137 &bursts[i * 116], &h, 1);
2138 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002139 break;
2140 default:
2141 return -1;
2142 }
2143
2144 return 0;
2145}
2146
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002147/* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2148static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB)
2149{
2150 unsigned int id = 0, best = 0;
2151 unsigned int i, j, k;
2152
2153 for (i = 0; i < 4; i++) {
2154 /* FIXME: why not using remaining (16 - 8) soft-bits here? */
2155 for (j = 0, k = 0; j < 8; j++)
2156 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2157
2158 if (i == 0 || k < best) {
2159 best = k;
2160 id = i;
2161 }
2162 }
2163
2164 return id;
2165}
2166
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002167/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002168 * \param[out] tch_data Codec frame in RTP payload format
2169 * \param[in] bursts buffer containing the symbols of 8 bursts
2170 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2171 * \param[in] codec array of active codecs (active codec set)
2172 * \param[in] codecs number of codecs in \a codec
2173 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2174 * \param[out] cmr Output in \a codec_mode_req = 1
2175 * \param[out] n_errors Number of detected bit errors
2176 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002177 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2178 * codec out of range; negative on error
2179 */
Harald Welteb9946d32017-06-12 09:40:16 +02002180int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002181 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2182 uint8_t *cmr, int *n_errors, int *n_bits_total)
2183{
Philipp Maier898c9c62020-02-06 14:25:01 +01002184 return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req,
2185 codec, codecs, ft, cmr, n_errors,
2186 n_bits_total, NULL);
2187}
2188
2189/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2190 * \param[out] tch_data Codec frame in RTP payload format
2191 * \param[in] bursts buffer containing the symbols of 8 bursts
2192 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2193 * \param[in] codec array of active codecs (active codec set)
2194 * \param[in] codecs number of codecs in \a codec
2195 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2196 * \param[out] cmr Output in \a codec_mode_req = 1
2197 * \param[out] n_errors Number of detected bit errors
2198 * \param[out] n_bits_total Total number of bits
2199 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2200 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2201 * codec out of range; negative on error
2202 */
2203int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts,
2204 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2205 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2206{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002207 sbit_t iB[912], cB[456], h;
2208 ubit_t d[244], p[6], conv[250];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002209 int i, rv, len, steal = 0, id = -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002210 *n_errors = 0; *n_bits_total = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002211 static ubit_t sid_first_dummy[64] = { 0 };
2212 sbit_t sid_update_enc[256];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002213
2214 for (i=0; i<8; i++) {
2215 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2216 steal -= h;
2217 }
2218
2219 gsm0503_tch_fr_deinterleave(cB, iB);
2220
2221 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002222 /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002223 * It's normally updated by gsm0503_detect_afs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002224 * reached in case of FACCH. Reset it here to avoid FACCH/F frames being
2225 * misinterpreted as AMR's special DTX frames. */
2226 if (dtx != NULL)
2227 *dtx = AMR_OTHER;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002228 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2229 if (rv) {
2230 /* Error decoding FACCH frame */
2231 return -1;
2232 }
2233
2234 return GSM_MACBLOCK_LEN;
2235 }
2236
Philipp Maier898c9c62020-02-06 14:25:01 +01002237 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2238 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002239 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2240
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002241 *dtx = gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Philipp Maier898c9c62020-02-06 14:25:01 +01002242
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002243 switch (*dtx) {
2244 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002245 /* NOTE: The AFS_SID_UPDATE frame is splitted into
2246 * two half rate frames. If the id marker frame
2247 * (AFS_SID_UPDATE) is detected the following frame
2248 * contains the actual comfort noised data part of
2249 * (AFS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002250 if (dtx_prev != AFS_SID_UPDATE)
2251 break;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002252 /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002253 *dtx = AFS_SID_UPDATE_CN;
2254
2255 extract_afs_sid_update(sid_update_enc, cB);
2256 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2257 sid_update_enc, conv, n_errors,
2258 n_bits_total);
2259 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2260 35, conv + 35);
2261 if (rv != 0) {
2262 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2263 return -1;
2264 }
2265
2266 tch_amr_sid_update_append(conv, 1,
2267 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002268 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002269 tch_amr_reassemble(tch_data, conv, 39);
2270 len = 5;
2271 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002272 case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002273 tch_amr_sid_update_append(sid_first_dummy, 0,
2274 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002275 : codec[id > 0 ? id : 0]);
Vadim Yanitskiy7d121cb2023-06-23 16:57:48 +07002276 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
Philipp Maier898c9c62020-02-06 14:25:01 +01002277 len = 5;
2278 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002279 case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002280 case AFS_ONSET:
Philipp Maier898c9c62020-02-06 14:25:01 +01002281 len = 0;
2282 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002283 default:
2284 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002285 }
2286 }
2287
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002288 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2289 if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002290 /* Codec mode out of range, return id */
2291 return id;
2292 }
2293
2294 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2295 case 7: /* TCH/AFS12.2 */
2296 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2297 conv, n_errors, n_bits_total);
2298
2299 tch_amr_unmerge(d, p, conv, 244, 81);
2300
2301 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2302 if (rv) {
2303 /* Error checking CRC8 for an AMR 12.2 frame */
2304 return -1;
2305 }
2306
2307 tch_amr_reassemble(tch_data, d, 244);
2308
2309 len = 31;
2310
2311 break;
2312 case 6: /* TCH/AFS10.2 */
2313 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2314 conv, n_errors, n_bits_total);
2315
2316 tch_amr_unmerge(d, p, conv, 204, 65);
2317
2318 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2319 if (rv) {
2320 /* Error checking CRC8 for an AMR 10.2 frame */
2321 return -1;
2322 }
2323
2324 tch_amr_reassemble(tch_data, d, 204);
2325
2326 len = 26;
2327
2328 break;
2329 case 5: /* TCH/AFS7.95 */
2330 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2331 conv, n_errors, n_bits_total);
2332
2333 tch_amr_unmerge(d, p, conv, 159, 75);
2334
2335 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2336 if (rv) {
2337 /* Error checking CRC8 for an AMR 7.95 frame */
2338 return -1;
2339 }
2340
2341 tch_amr_reassemble(tch_data, d, 159);
2342
2343 len = 20;
2344
2345 break;
2346 case 4: /* TCH/AFS7.4 */
2347 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2348 conv, n_errors, n_bits_total);
2349
2350 tch_amr_unmerge(d, p, conv, 148, 61);
2351
2352 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2353 if (rv) {
2354 /* Error checking CRC8 for an AMR 7.4 frame */
2355 return -1;
2356 }
2357
2358 tch_amr_reassemble(tch_data, d, 148);
2359
2360 len = 19;
2361
2362 break;
2363 case 3: /* TCH/AFS6.7 */
2364 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2365 conv, n_errors, n_bits_total);
2366
2367 tch_amr_unmerge(d, p, conv, 134, 55);
2368
2369 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2370 if (rv) {
2371 /* Error checking CRC8 for an AMR 6.7 frame */
2372 return -1;
2373 }
2374
2375 tch_amr_reassemble(tch_data, d, 134);
2376
2377 len = 17;
2378
2379 break;
2380 case 2: /* TCH/AFS5.9 */
2381 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2382 conv, n_errors, n_bits_total);
2383
2384 tch_amr_unmerge(d, p, conv, 118, 55);
2385
2386 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2387 if (rv) {
2388 /* Error checking CRC8 for an AMR 5.9 frame */
2389 return -1;
2390 }
2391
2392 tch_amr_reassemble(tch_data, d, 118);
2393
2394 len = 15;
2395
2396 break;
2397 case 1: /* TCH/AFS5.15 */
2398 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2399 conv, n_errors, n_bits_total);
2400
2401 tch_amr_unmerge(d, p, conv, 103, 49);
2402
2403 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2404 if (rv) {
2405 /* Error checking CRC8 for an AMR 5.15 frame */
2406 return -1;
2407 }
2408
2409 tch_amr_reassemble(tch_data, d, 103);
2410
2411 len = 13;
2412
2413 break;
2414 case 0: /* TCH/AFS4.75 */
2415 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2416 conv, n_errors, n_bits_total);
2417
2418 tch_amr_unmerge(d, p, conv, 95, 39);
2419
2420 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2421 if (rv) {
2422 /* Error checking CRC8 for an AMR 4.75 frame */
2423 return -1;
2424 }
2425
2426 tch_amr_reassemble(tch_data, d, 95);
2427
2428 len = 12;
2429
2430 break;
2431 default:
2432 /* Unknown frame type */
2433 *n_bits_total = 448;
2434 *n_errors = *n_bits_total;
2435 return -1;
2436 }
2437
Philipp Maier898c9c62020-02-06 14:25:01 +01002438out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002439 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002440 if (id != -1) {
2441 if (codec_mode_req)
2442 *cmr = id;
2443 else
2444 *ft = id;
2445 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002446
2447 return len;
2448}
2449
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002450/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002451 * \param[out] bursts caller-allocated output buffer for bursts bits
2452 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002453 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002454 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2455 * \param[in] codec Array of codecs (active codec set)
2456 * \param[in] codecs Number of entries in \a codec
2457 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2458 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2459 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002460int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002461 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002462 uint8_t cmr)
2463{
2464 ubit_t iB[912], cB[456], h;
2465 ubit_t d[244], p[6], conv[250];
2466 int i;
2467 uint8_t id;
2468
2469 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2470 _xcch_encode_cB(cB, tch_data);
2471
2472 h = 1;
2473
2474 goto facch;
2475 }
2476
2477 h = 0;
2478
Vadim Yanitskiya9952692023-11-10 03:31:55 +07002479 id = codec_mode_req ? cmr : ft;
2480 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit)))
2481 return -1;
2482 if (OSMO_UNLIKELY(ft >= codecs))
2483 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002484
2485 switch (codec[ft]) {
2486 case 7: /* TCH/AFS12.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002487 if (!len) {
2488 /* No data, induce BFI in the receiver by inverted CRC bits.
2489 * The data bit are all 0, so the correct parity bits would be 111111. */
2490 memset(d, 0, 244);
2491 memset(p, 0, 6);
2492 } else {
2493 if (len != 31)
2494 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002495
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002496 tch_amr_disassemble(d, tch_data, 244);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002497
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002498 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2499 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002500
2501 tch_amr_merge(conv, d, p, 244, 81);
2502
2503 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2504
2505 break;
2506 case 6: /* TCH/AFS10.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002507 if (!len) {
2508 /* See comment above. */
2509 memset(d, 0, 204);
2510 memset(p, 0, 6);
2511 } else {
2512 if (len != 26)
2513 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002514
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002515 tch_amr_disassemble(d, tch_data, 204);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002516
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002517 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2518 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002519
2520 tch_amr_merge(conv, d, p, 204, 65);
2521
2522 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2523
2524 break;
2525 case 5: /* TCH/AFS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002526 if (!len) {
2527 /* See comment above. */
2528 memset(d, 0, 159);
2529 memset(p, 0, 6);
2530 } else {
2531 if (len != 20)
2532 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002533
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002534 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002535
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002536 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2537 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002538
2539 tch_amr_merge(conv, d, p, 159, 75);
2540
2541 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2542
2543 break;
2544 case 4: /* TCH/AFS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002545 if (!len) {
2546 /* See comment above. */
2547 memset(d, 0, 148);
2548 memset(p, 0, 6);
2549 } else {
2550 if (len != 19)
2551 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002552
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002553 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002554
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002555 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2556 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002557
2558 tch_amr_merge(conv, d, p, 148, 61);
2559
2560 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2561
2562 break;
2563 case 3: /* TCH/AFS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002564 if (!len) {
2565 /* See comment above. */
2566 memset(d, 0, 134);
2567 memset(p, 0, 6);
2568 } else {
2569 if (len != 17)
2570 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002571
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002572 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002573
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002574 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2575 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002576
2577 tch_amr_merge(conv, d, p, 134, 55);
2578
2579 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2580
2581 break;
2582 case 2: /* TCH/AFS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002583 if (!len) {
2584 /* See comment above. */
2585 memset(d, 0, 118);
2586 memset(p, 0, 6);
2587 } else {
2588 if (len != 15)
2589 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002590
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002591 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002592
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002593 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2594 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002595
2596 tch_amr_merge(conv, d, p, 118, 55);
2597
2598 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2599
2600 break;
2601 case 1: /* TCH/AFS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002602 if (!len) {
2603 /* See comment above. */
2604 memset(d, 0, 103);
2605 memset(p, 0, 6);
2606 } else {
2607 if (len != 13)
2608 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002609
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002610 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002611
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002612 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2613 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002614
2615 tch_amr_merge(conv, d, p, 103, 49);
2616
2617 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2618
2619 break;
2620 case 0: /* TCH/AFS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002621 if (!len) {
2622 /* See comment above. */
2623 memset(d, 0, 95);
2624 memset(p, 0, 6);
2625 } else {
2626 if (len != 12)
2627 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002628
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002629 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002630
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002631 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2632 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002633
2634 tch_amr_merge(conv, d, p, 95, 39);
2635
2636 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2637
2638 break;
2639 default:
2640 /* FIXME: FT %ft is not supported */
2641 return -1;
2642 }
2643
2644 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2645
2646facch:
2647 gsm0503_tch_fr_interleave(cB, iB);
2648
2649 for (i = 0; i < 8; i++) {
2650 gsm0503_tch_burst_map(&iB[i * 114],
2651 &bursts[i * 116], &h, i >> 2);
2652 }
2653
2654 return 0;
2655
2656invalid_length:
2657 /* FIXME: payload length %len does not comply with codec type %ft */
2658 return -1;
2659}
2660
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002661/* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2662static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB)
2663{
2664 unsigned int id = 0, best = 0;
2665 unsigned int i, j, k;
2666
2667 for (i = 0, k = 0; i < 4; i++) {
2668 /* FIXME: why not using remaining (16 - 4) soft-bits here? */
2669 for (j = 0, k = 0; j < 4; j++)
2670 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2671
2672 if (i == 0 || k < best) {
2673 best = k;
2674 id = i;
2675 }
2676 }
2677
2678 return id;
2679}
2680
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002681/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002682 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002683 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02002684 * \param[in] odd Is this an odd (1) or even (0) frame number?
2685 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2686 * \param[in] codec array of active codecs (active codec set)
2687 * \param[in] codecs number of codecs in \a codec
2688 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2689 * \param[out] cmr Output in \a codec_mode_req = 1
2690 * \param[out] n_errors Number of detected bit errors
2691 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002692 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2693 * codec out of range; negative on error
2694 */
Harald Welteb9946d32017-06-12 09:40:16 +02002695int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002696 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2697 uint8_t *cmr, int *n_errors, int *n_bits_total)
2698{
Philipp Maier898c9c62020-02-06 14:25:01 +01002699 return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req,
2700 codec, codecs, ft, cmr, n_errors,
2701 n_bits_total, NULL);
2702}
2703
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002704/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Philipp Maier898c9c62020-02-06 14:25:01 +01002705 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002706 * \param[in] bursts buffer containing the symbols of 6 bursts
Philipp Maier898c9c62020-02-06 14:25:01 +01002707 * \param[in] odd Is this an odd (1) or even (0) frame number?
2708 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2709 * \param[in] codec array of active codecs (active codec set)
2710 * \param[in] codecs number of codecs in \a codec
2711 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2712 * \param[out] cmr Output in \a codec_mode_req = 1
2713 * \param[out] n_errors Number of detected bit errors
2714 * \param[out] n_bits_total Total number of bits
2715 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2716 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2717 * codec out of range; negative on error
2718 */
2719int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd,
2720 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2721 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2722{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002723 sbit_t iB[912], cB[456], h;
2724 ubit_t d[244], p[6], conv[135];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002725 int i, rv, len, steal = 0, id = -1;
Philipp Maier898c9c62020-02-06 14:25:01 +01002726 static ubit_t sid_first_dummy[64] = { 0 };
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002727
2728 /* only unmap the stealing bits */
2729 if (!odd) {
2730 for (i = 0; i < 4; i++) {
2731 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2732 steal -= h;
2733 }
2734 for (i = 2; i < 5; i++) {
2735 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2736 steal -= h;
2737 }
2738 }
2739
2740 /* if we found a stole FACCH, but only at correct alignment */
2741 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002742 /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002743 * It's normally updated by gsm0503_detect_ahs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002744 * reached in case of FACCH. Reset it here to avoid FACCH/H frames being
2745 * misinterpreted as AMR's special DTX frames. */
2746 if (dtx != NULL)
2747 *dtx = AMR_OTHER;
2748
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002749 for (i = 0; i < 6; i++) {
2750 gsm0503_tch_burst_unmap(&iB[i * 114],
2751 &bursts[i * 116], NULL, i >> 2);
2752 }
2753
2754 for (i = 2; i < 4; i++) {
2755 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2756 &bursts[i * 116], NULL, 1);
2757 }
2758
2759 gsm0503_tch_fr_deinterleave(cB, iB);
2760
2761 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2762 if (rv) {
2763 /* Error decoding FACCH frame */
2764 return -1;
2765 }
2766
2767 return GSM_MACBLOCK_LEN;
2768 }
2769
2770 for (i = 0; i < 4; i++) {
2771 gsm0503_tch_burst_unmap(&iB[i * 114],
2772 &bursts[i * 116], NULL, i >> 1);
2773 }
2774
2775 gsm0503_tch_hr_deinterleave(cB, iB);
2776
Philipp Maier898c9c62020-02-06 14:25:01 +01002777 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2778 if (dtx) {
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002779 int n_bits_total_sid;
2780 int n_errors_sid;
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002781
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002782 *dtx = gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002783 /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */
Philipp Maier898c9c62020-02-06 14:25:01 +01002784
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002785 switch (*dtx) {
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002786 case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002787 /* cB[] contains 16 bits of coded in-band data and 212 bits containing
2788 * the identification marker. We need to unmap/deinterleave 114 odd
2789 * bits from the last two blocks, 114 even bits from the first two
2790 * blocks and combine them together. */
2791 gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0);
2792 gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0);
2793 gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1);
2794 gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1);
2795 gsm0503_tch_hr_deinterleave(cB, iB);
2796
2797 /* cB[] is expected to contain 16 bits of coded in-band data and
2798 * 212 bits containing the coded data (53 bits coded at 1/4 rate). */
Philipp Maier898c9c62020-02-06 14:25:01 +01002799 *dtx = AHS_SID_UPDATE_CN;
2800
2801 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002802 cB + 16, conv, &n_errors_sid,
2803 &n_bits_total_sid);
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002804 /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002805 * osmo_conv_decode_ber() calculates BER for the coded data. */
2806 if (n_errors != NULL)
2807 *n_errors += n_errors_sid;
2808 if (n_bits_total != NULL)
2809 *n_bits_total += n_bits_total_sid;
Philipp Maier898c9c62020-02-06 14:25:01 +01002810 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2811 35, conv + 35);
2812 if (rv != 0) {
2813 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2814 return -1;
2815 }
2816
2817 tch_amr_sid_update_append(conv, 1,
2818 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002819 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002820 tch_amr_reassemble(tch_data, conv, 39);
2821 len = 5;
2822 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002823 case AHS_SID_FIRST_P2:
Philipp Maier898c9c62020-02-06 14:25:01 +01002824 tch_amr_sid_update_append(sid_first_dummy, 0,
2825 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002826 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002827 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
2828 len = 5;
2829 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002830 case AHS_ONSET:
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002831 case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2832 case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2833 case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002834 len = 0;
2835 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002836 default:
2837 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002838 }
2839 }
2840
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002841 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2842 if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002843 /* Codec mode out of range, return id */
2844 return id;
2845 }
2846
2847 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2848 case 5: /* TCH/AHS7.95 */
2849 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2850 conv, n_errors, n_bits_total);
2851
2852 tch_amr_unmerge(d, p, conv, 123, 67);
2853
2854 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2855 if (rv) {
2856 /* Error checking CRC8 for an AMR 7.95 frame */
2857 return -1;
2858 }
2859
2860 for (i = 0; i < 36; i++)
2861 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2862
2863 tch_amr_reassemble(tch_data, d, 159);
2864
2865 len = 20;
2866
2867 break;
2868 case 4: /* TCH/AHS7.4 */
2869 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2870 conv, n_errors, n_bits_total);
2871
2872 tch_amr_unmerge(d, p, conv, 120, 61);
2873
2874 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2875 if (rv) {
2876 /* Error checking CRC8 for an AMR 7.4 frame */
2877 return -1;
2878 }
2879
2880 for (i = 0; i < 28; i++)
2881 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2882
2883 tch_amr_reassemble(tch_data, d, 148);
2884
2885 len = 19;
2886
2887 break;
2888 case 3: /* TCH/AHS6.7 */
2889 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2890 conv, n_errors, n_bits_total);
2891
2892 tch_amr_unmerge(d, p, conv, 110, 55);
2893
2894 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2895 if (rv) {
2896 /* Error checking CRC8 for an AMR 6.7 frame */
2897 return -1;
2898 }
2899
2900 for (i = 0; i < 24; i++)
2901 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2902
2903 tch_amr_reassemble(tch_data, d, 134);
2904
2905 len = 17;
2906
2907 break;
2908 case 2: /* TCH/AHS5.9 */
2909 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2910 conv, n_errors, n_bits_total);
2911
2912 tch_amr_unmerge(d, p, conv, 102, 55);
2913
2914 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2915 if (rv) {
2916 /* Error checking CRC8 for an AMR 5.9 frame */
2917 return -1;
2918 }
2919
2920 for (i = 0; i < 16; i++)
2921 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2922
2923 tch_amr_reassemble(tch_data, d, 118);
2924
2925 len = 15;
2926
2927 break;
2928 case 1: /* TCH/AHS5.15 */
2929 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2930 conv, n_errors, n_bits_total);
2931
2932 tch_amr_unmerge(d, p, conv, 91, 49);
2933
2934 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2935 if (rv) {
2936 /* Error checking CRC8 for an AMR 5.15 frame */
2937 return -1;
2938 }
2939
2940 for (i = 0; i < 12; i++)
2941 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2942
2943 tch_amr_reassemble(tch_data, d, 103);
2944
2945 len = 13;
2946
2947 break;
2948 case 0: /* TCH/AHS4.75 */
2949 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2950 conv, n_errors, n_bits_total);
2951
2952 tch_amr_unmerge(d, p, conv, 83, 39);
2953
2954 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2955 if (rv) {
2956 /* Error checking CRC8 for an AMR 4.75 frame */
2957 return -1;
2958 }
2959
2960 for (i = 0; i < 12; i++)
2961 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2962
2963 tch_amr_reassemble(tch_data, d, 95);
2964
2965 len = 12;
2966
2967 break;
2968 default:
2969 /* Unknown frame type */
2970 *n_bits_total = 159;
2971 *n_errors = *n_bits_total;
2972 return -1;
2973 }
2974
Philipp Maier898c9c62020-02-06 14:25:01 +01002975out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002976 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002977 if (id != -1) {
2978 if (codec_mode_req)
2979 *cmr = id;
2980 else
2981 *ft = id;
2982 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002983
2984 return len;
2985}
2986
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002987/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002988 * \param[out] bursts caller-allocated output buffer for bursts bits
2989 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002990 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002991 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2992 * \param[in] codec Array of codecs (active codec set)
2993 * \param[in] codecs Number of entries in \a codec
2994 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2995 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2996 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002997int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002998 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002999 uint8_t cmr)
3000{
3001 ubit_t iB[912], cB[456], h;
3002 ubit_t d[244], p[6], conv[135];
3003 int i;
3004 uint8_t id;
3005
3006 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
3007 _xcch_encode_cB(cB, tch_data);
3008
3009 h = 1;
3010
3011 gsm0503_tch_fr_interleave(cB, iB);
3012
3013 for (i = 0; i < 6; i++)
3014 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
3015 &h, i >> 2);
3016 for (i = 2; i < 4; i++)
3017 gsm0503_tch_burst_map(&iB[i * 114 + 456],
3018 &bursts[i * 116], &h, 1);
3019
3020 return 0;
3021 }
3022
3023 h = 0;
3024
Vadim Yanitskiya9952692023-11-10 03:31:55 +07003025 id = codec_mode_req ? cmr : ft;
3026 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit)))
3027 return -1;
3028 if (OSMO_UNLIKELY(ft >= codecs))
3029 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003030
3031 switch (codec[ft]) {
3032 case 5: /* TCH/AHS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003033 if (!len) {
3034 /* No data, induce BFI in the receiver by inverted CRC bits.
3035 * The data bit are all 0, so the correct parity bits would be 111111. */
3036 memset(d, 0, 159);
3037 memset(p, 0, 6);
3038 } else {
3039 if (len != 20)
3040 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003041
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003042 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003043
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003044 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
3045 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003046
3047 tch_amr_merge(conv, d, p, 123, 67);
3048
3049 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
3050
3051 memcpy(cB + 192, d + 123, 36);
3052
3053 break;
3054 case 4: /* TCH/AHS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003055 if (!len) {
3056 /* See comment above. */
3057 memset(d, 0, 148);
3058 memset(p, 0, 6);
3059 } else {
3060 if (len != 19)
3061 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003062
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003063 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003064
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003065 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
3066 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003067
3068 tch_amr_merge(conv, d, p, 120, 61);
3069
3070 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
3071
3072 memcpy(cB + 200, d + 120, 28);
3073
3074 break;
3075 case 3: /* TCH/AHS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003076 if (!len) {
3077 /* See comment above. */
3078 memset(d, 0, 134);
3079 memset(p, 0, 6);
3080 } else {
3081 if (len != 17)
3082 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003083
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003084 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003085
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003086 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3087 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003088
3089 tch_amr_merge(conv, d, p, 110, 55);
3090
3091 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
3092
3093 memcpy(cB + 204, d + 110, 24);
3094
3095 break;
3096 case 2: /* TCH/AHS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003097 if (!len) {
3098 /* See comment above. */
3099 memset(d, 0, 118);
3100 memset(p, 0, 6);
3101 } else {
3102 if (len != 15)
3103 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003104
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003105 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003106
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003107 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3108 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003109
3110 tch_amr_merge(conv, d, p, 102, 55);
3111
3112 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
3113
3114 memcpy(cB + 212, d + 102, 16);
3115
3116 break;
3117 case 1: /* TCH/AHS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003118 if (!len) {
3119 /* See comment above. */
3120 memset(d, 0, 103);
3121 memset(p, 0, 6);
3122 } else {
3123 if (len != 13)
3124 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003125
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003126 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003127
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003128 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
3129 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003130
3131 tch_amr_merge(conv, d, p, 91, 49);
3132
3133 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
3134
3135 memcpy(cB + 216, d + 91, 12);
3136
3137 break;
3138 case 0: /* TCH/AHS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003139 if (!len) {
3140 /* See comment above. */
3141 memset(d, 0, 95);
3142 memset(p, 0, 6);
3143 } else {
3144 if (len != 12)
3145 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003146
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003147 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003148
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003149 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
3150 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003151
3152 tch_amr_merge(conv, d, p, 83, 39);
3153
3154 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
3155
3156 memcpy(cB + 216, d + 83, 12);
3157
3158 break;
3159 default:
3160 /* FIXME: FT %ft is not supported */
3161 return -1;
3162 }
3163
Philipp Maiercfea39b2021-08-31 16:02:31 +02003164 memcpy(cB, gsm0503_ahs_ic_ubit[id], 4);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003165
3166 gsm0503_tch_hr_interleave(cB, iB);
3167
3168 for (i = 0; i < 4; i++)
3169 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
3170
3171 return 0;
3172
3173invalid_length:
3174 /* FIXME: payload length %len does not comply with codec type %ft */
3175 return -1;
3176}
3177
3178/*
3179 * GSM RACH transcoding
3180 */
3181
3182/*
3183 * GSM RACH apply BSIC to parity
3184 *
3185 * p(j) = p(j) xor b(j) j = 0, ..., 5
3186 * b(0) = MSB of PLMN colour code
3187 * b(5) = LSB of BS colour code
3188 */
Max32e56412017-10-16 14:58:00 +02003189static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003190{
3191 int i;
3192
3193 /* Apply it */
3194 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02003195 d[start + i] ^= ((bsic >> (5 - i)) & 1);
3196}
3197
Harald Welte6950b192018-02-26 11:48:00 +01003198static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
3199 int *n_errors, int *n_bits_total)
Max32e56412017-10-16 14:58:00 +02003200{
3201 ubit_t conv[17];
3202 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
3203 int rv;
3204
Harald Welte6950b192018-02-26 11:48:00 +01003205 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
3206 n_errors, n_bits_total);
Max32e56412017-10-16 14:58:00 +02003207
3208 rach_apply_bsic(conv, bsic, nbits);
3209
3210 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3211 if (rv)
3212 return -1;
3213
3214 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
3215
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003216 return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0];
Max32e56412017-10-16 14:58:00 +02003217}
3218
3219/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3220 * \param[out] ra output buffer for RACH data
3221 * \param[in] burst Input burst data
3222 * \param[in] bsic BSIC used in this cell
3223 * \returns 0 on success; negative on error (e.g. CRC error) */
3224int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
3225{
Harald Welte6950b192018-02-26 11:48:00 +01003226 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
Max32e56412017-10-16 14:58:00 +02003227
3228 if (r < 0)
3229 return r;
3230
3231 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003232
3233 return 0;
3234}
3235
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003236/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003237 * \param[out] ra output buffer for RACH data
3238 * \param[in] burst Input burst data
3239 * \param[in] bsic BSIC used in this cell
3240 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003241int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003242{
Harald Welte6950b192018-02-26 11:48:00 +01003243 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
3244 if (r < 0)
3245 return r;
3246
3247 *ra = r;
3248 return 0;
3249}
3250
3251/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3252 * \param[out] ra output buffer for RACH data
3253 * \param[in] burst Input burst data
3254 * \param[in] bsic BSIC used in this cell
3255 * \param[out] n_errors Number of detected bit errors
3256 * \param[out] n_bits_total Total number of bits
3257 * \returns 0 on success; negative on error (e.g. CRC error) */
3258int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
3259 int *n_errors, int *n_bits_total)
3260{
3261 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
3262 if (r < 0)
3263 return r;
3264
3265 *ra = r;
3266 return 0;
3267}
3268
3269/*! Decode the (8-bit) RACH according to TS 05.03
3270 * \param[out] ra output buffer for RACH data
3271 * \param[in] burst Input burst data
3272 * \param[in] bsic BSIC used in this cell
3273 * \param[out] n_errors Number of detected bit errors
3274 * \param[out] n_bits_total Total number of bits
3275 * \returns 0 on success; negative on error (e.g. CRC error) */
3276int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
3277 int *n_errors, int *n_bits_total)
3278{
3279 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003280
Max32e56412017-10-16 14:58:00 +02003281 if (r < 0)
3282 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003283
Max32e56412017-10-16 14:58:00 +02003284 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003285
3286 return 0;
3287}
3288
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003289/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003290 * \param[out] burst Caller-allocated output burst buffer
3291 * \param[in] ra Input RACH data
3292 * \param[in] bsic BSIC used in this cell
3293 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003294int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003295{
Max32e56412017-10-16 14:58:00 +02003296 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
3297}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003298
Max32e56412017-10-16 14:58:00 +02003299/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
3300 * \param[out] burst Caller-allocated output burst buffer
3301 * \param[in] ra11 Input RACH data
3302 * \param[in] bsic BSIC used in this cell
3303 * \param[in] is_11bit whether given RA is 11 bit or not
3304 * \returns 0 on success; negative on error */
3305int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
3306{
3307 ubit_t conv[17];
3308 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003309
Max32e56412017-10-16 14:58:00 +02003310 if (is_11bit) {
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003311 ra[0] = (uint8_t) (ra11 >> 3);
3312 ra[1] = (uint8_t) (ra11 & 0x07);
Max32e56412017-10-16 14:58:00 +02003313 nbits = 11;
3314 } else
3315 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003316
Max32e56412017-10-16 14:58:00 +02003317 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003318
Max32e56412017-10-16 14:58:00 +02003319 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3320
3321 rach_apply_bsic(conv, bsic, nbits);
3322
3323 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003324
3325 return 0;
3326}
3327
3328/*
3329 * GSM SCH transcoding
3330 */
Harald Weltec6636782017-06-12 14:59:37 +02003331
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003332/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003333 * \param[out] sb_info output buffer for SCH data
3334 * \param[in] burst Input burst data
3335 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003336int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003337{
3338 ubit_t conv[35];
3339 int rv;
3340
3341 osmo_conv_decode(&gsm0503_sch, burst, conv);
3342
3343 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3344 if (rv)
3345 return -1;
3346
3347 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
3348
3349 return 0;
3350}
3351
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003352/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003353 * \param[out] burst Caller-allocated output burst buffer
3354 * \param[in] sb_info Input SCH data
3355 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003356int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003357{
3358 ubit_t conv[35];
3359
3360 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
3361
3362 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3363
3364 osmo_conv_encode(&gsm0503_sch, conv, burst);
3365
3366 return 0;
3367}
Harald Weltec6636782017-06-12 14:59:37 +02003368
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003369/*
3370 * GSM CSD transcoding
3371 */
3372
3373static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB)
3374{
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003375 unsigned int i;
3376
3377 /* hu(B): copy *even* numbered bits if not stolen by FACCH */
3378 if (burst[58] == 0) {
3379 for (i = 0; i < 57; i += 2)
3380 burst[i] |= iB[i];
3381 for (i = 58; i < 114; i += 2)
3382 burst[i + 2] |= iB[i];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003383 }
3384
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003385 /* hl(B): copy *odd* numbered bits if not stolen by FACCH */
3386 if (burst[57] == 0) {
3387 for (i = 1; i < 57; i += 2)
3388 burst[i] |= iB[i];
3389 for (i = 57; i < 114; i += 2)
3390 burst[i + 2] |= iB[i];
3391 }
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003392}
3393
3394/*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3.
3395 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3396 * 22 * 2 * 58 == 2552 bits total.
3397 * \param[in] data Data to be encoded (240 unpacked bits).
3398 * \returns 0 in case of success; negative on error. */
3399int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data)
3400{
3401 ubit_t iB[22 * 114], cB[4 * 114];
3402 ubit_t conv[4 * 60 + 4];
3403
3404 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3405 memcpy(&conv[0], &data[0], 4 * 60);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003406 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003407
3408 /* 3.3.3 Convolutional encoder */
3409 osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]);
3410
3411 /* 3.3.4 Interleaving */
3412 memset(&iB[0], 0, sizeof(iB));
3413 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3414
3415 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3416 for (unsigned int i = 0; i < 22; i++)
3417 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3418
3419 return 0;
3420}
3421
3422/*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3.
3423 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3424 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3425 * 22 * 2 * 58 == 2552 bits total.
3426 * \param[out] n_errors Number of detected bit errors.
3427 * \param[out] n_bits_total Total number of bits.
3428 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3429int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts,
3430 int *n_errors, int *n_bits_total)
3431{
3432 sbit_t iB[22 * 114], cB[4 * 114];
3433 ubit_t conv[4 * 60 + 4];
3434
3435 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3436 for (unsigned int i = 0; i < 22; i++) {
3437 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3438 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3439 }
3440
3441 /* 3.3.4 Interleaving */
3442 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3443
3444 /* 3.3.3 Convolutional encoder */
3445 osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total);
3446
3447 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3448 memcpy(&data[0], &conv[0], 4 * 60);
3449
3450 return 4 * 60;
3451}
3452
3453/*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4.
3454 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3455 * 22 * 2 * 58 == 2552 bits total.
3456 * \param[in] data Data to be encoded (120 unpacked bits).
3457 * \returns 0 in case of success; negative on error */
3458int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data)
3459{
3460 ubit_t iB[22 * 114], cB[4 * 114];
3461 ubit_t conv[2 * 60 + 32];
3462
3463 /* 3.4.2 Block code:
3464 *
3465 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3466 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3467 *
3468 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3469 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3470 *
3471 * Two such blocks forming a block of 152 bits: u1 + u2. */
3472 for (unsigned int k = 0; k < 2 * 4; k++) {
3473 memcpy(&conv[19 * k], &data[15 * k], 15);
3474 memset(&conv[19 * k + 15], 0, 4);
3475 }
3476
3477 /* 3.4.3 Convolutional encoder */
3478 osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]);
3479
3480 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3481 memset(&iB[0], 0, sizeof(iB));
3482 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3483
3484 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3485 for (unsigned int i = 0; i < 22; i++)
3486 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3487
3488 return 0;
3489}
3490
3491/*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4.
3492 * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits).
3493 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3494 * 22 * 2 * 58 == 2552 bits total.
3495 * \param[out] n_errors Number of detected bit errors.
3496 * \param[out] n_bits_total Total number of bits.
3497 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3498int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts,
3499 int *n_errors, int *n_bits_total)
3500{
3501 sbit_t iB[22 * 114], cB[4 * 114];
3502 ubit_t conv[2 * 60 + 32];
3503
3504 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3505 for (unsigned int i = 0; i < 22; i++) {
3506 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3507 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3508 }
3509
3510 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3511 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3512
3513 /* 3.4.3 Convolutional encoder */
3514 osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total);
3515
3516 /* 3.4.2 Block code:
3517 *
3518 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3519 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3520 *
3521 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3522 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3523 *
3524 * Two such blocks forming a block of 152 bits: u1 + u2. */
3525 for (unsigned int k = 0; k < 2 * 4; k++)
3526 memcpy(&data[15 * k], &conv[19 * k], 15);
3527
3528 return 2 * 60;
3529}
3530
3531/*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5.
3532 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3533 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3534 * 22 * 2 * 58 == 2552 bits total.
3535 * \param[in] data Data to be encoded (240 unpacked bits).
3536 * \returns 0 in case of success; negative on error */
3537int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data)
3538{
3539 return gsm0503_tch_fr96_encode(bursts, data);
3540}
3541
3542/*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5.
3543 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3544 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3545 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3546 * 22 * 2 * 58 == 2552 bits total.
3547 * \param[out] n_errors Number of detected bit errors.
3548 * \param[out] n_bits_total Total number of bits.
3549 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3550int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts,
3551 int *n_errors, int *n_bits_total)
3552{
3553 return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total);
3554}
3555
Vadim Yanitskiyfadda012023-07-08 06:30:55 +07003556/*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6.
3557 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3558 * 8 * 2 * 58 == 928 bits total.
3559 * \param[in] data Data to be encoded (72 unpacked bits).
3560 * \returns 0 in case of success; negative on error */
3561int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data)
3562{
3563 ubit_t iB[8 * 114], cB[4 * 114];
3564 const ubit_t h = 0;
3565
3566 /* 3.6.{1-3} Block code and Convolutional encoder */
3567 osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]);
3568
3569 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3570 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3571
3572 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3573 for (unsigned int i = 0; i < 8; i++)
3574 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3575
3576 return 0;
3577}
3578
3579/*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6.
3580 * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits).
3581 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3582 * 8 * 2 * 58 == 928 bits total.
3583 * \param[out] n_errors Number of detected bit errors.
3584 * \param[out] n_bits_total Total number of bits.
3585 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3586int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts,
3587 int *n_errors, int *n_bits_total)
3588{
3589 sbit_t iB[8 * 114], cB[4 * 114];
3590
3591 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3592 for (unsigned int i = 0; i < 8; i++)
3593 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3594
3595 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3596 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3597
3598 /* 3.6.{1-3} Block code and Convolutional encoder */
3599 osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total);
3600
3601 return 72;
3602}
3603
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003604/*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7.
3605 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3606 * 22 * 2 * 58 == 2552 bits total.
3607 * \param[in] data Data to be encoded (144 unpacked bits).
3608 * \returns 0 in case of success; negative on error */
3609int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data)
3610{
3611 ubit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003612
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003613 /* 3.7.{1-3} Block code and Convolutional encoder */
3614 osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]);
3615 osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]);
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003616
3617 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3618 memset(&iB[0], 0, sizeof(iB));
3619 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3620
3621 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3622 for (unsigned int i = 0; i < 22; i++)
3623 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3624
3625 return 0;
3626}
3627
3628/*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7.
3629 * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits).
3630 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3631 * 22 * 2 * 58 == 2552 bits total.
3632 * \param[out] n_errors Number of detected bit errors.
3633 * \param[out] n_bits_total Total number of bits.
3634 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3635int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts,
3636 int *n_errors, int *n_bits_total)
3637{
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003638 int n_errors_l[2], n_bits_total_l[2];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003639 sbit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003640
3641 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3642 for (unsigned int i = 0; i < 22; i++) {
3643 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3644 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3645 }
3646
3647 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3648 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3649
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003650 /* 3.7.{1-3} Block code and Convolutional encoder */
3651 osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]);
3652 osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[228], &data[72], &n_errors_l[1], &n_bits_total_l[1]);
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003653
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003654 if (n_errors)
3655 *n_errors = n_errors_l[0] + n_errors_l[1];
3656
3657 if (n_bits_total)
3658 *n_bits_total = n_bits_total_l[0] + n_bits_total_l[1];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003659
3660 return 2 * 72;
3661}
3662
3663/*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8.
3664 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3665 * 22 * 2 * 58 == 2552 bits total.
3666 * \param[in] data Data to be encoded (290 unpacked bits).
3667 * \returns 0 in case of success; negative on error */
3668int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data)
3669{
3670 ubit_t iB[22 * 114], cB[4 * 114];
3671 ubit_t conv[290 + 4];
3672
3673 /* 3.8.2 Block code: b(290) + pad(4) */
3674 memcpy(&conv[0], &data[0], 290);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003675 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003676
3677 /* 3.8.3 Convolutional encoder */
3678 osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]);
3679
3680 /* 3.8.4 Interleaving */
3681 memset(&iB[0], 0, sizeof(iB));
3682 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3683
3684 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3685 for (unsigned int i = 0; i < 22; i++)
3686 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3687
3688 return 0;
3689}
3690
3691/*! Perform channel decoding of a TCH/14.4 channel as per section 3.8.
3692 * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits).
3693 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3694 * 22 * 2 * 58 == 2552 bits total.
3695 * \param[out] n_errors Number of detected bit errors.
3696 * \param[out] n_bits_total Total number of bits.
3697 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3698int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts,
3699 int *n_errors, int *n_bits_total)
3700{
3701 sbit_t iB[22 * 114], cB[4 * 114];
3702 ubit_t conv[294];
3703
3704 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3705 for (unsigned int i = 0; i < 22; i++) {
3706 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3707 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3708 }
3709
3710 /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3711 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3712
3713 /* 3.8.3 Convolutional encoder */
3714 osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total);
3715
3716 /* 3.8.2 Block code: b(290) + pad(4) */
3717 memcpy(&data[0], &conv[0], 290);
3718
3719 return 290;
3720}
3721
Vadim Yanitskiyb3340222023-05-26 01:07:18 +07003722/*
3723 * FACCH/[FH] transcoding
3724 */
3725
3726/*! Perform channel encoding of a FACCH/F data as per section 4.2.
3727 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3728 * 8 * 2 * 58 == 928 bits total.
3729 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3730 * \returns 0 in case of success; negative on error */
3731int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data)
3732{
3733 ubit_t iB[8 * 114], cB[4 * 114];
3734 const ubit_t h = 1;
3735
3736 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3737 _xcch_encode_cB(&cB[0], &data[0]);
3738
3739 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3740 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3741
3742 /* 4.2.5 Mapping on a Burst:
3743 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3744 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3745 for (unsigned int i = 0; i < 8; i++)
3746 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3747
3748 return 0;
3749}
3750
3751/*! Perform channel decoding of a FACCH/F data as per section 4.2.
3752 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3753 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3754 * 8 * 2 * 58 == 928 bits total.
3755 * \param[out] n_errors Number of detected bit errors.
3756 * \param[out] n_bits_total Total number of bits.
3757 * \returns Number of bytes used in the output buffer; negative on error. */
3758int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts,
3759 int *n_errors, int *n_bits_total)
3760{
3761 sbit_t iB[8 * 114], cB[4 * 114];
3762 int steal = 0;
3763
3764 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3765 for (unsigned int i = 0; i < 4; i++)
3766 steal -= bursts[i * 116 + 58]; /* hu(B) */
3767 for (unsigned int i = 4; i < 8; i++)
3768 steal -= bursts[i * 116 + 57]; /* hl(B) */
3769 if (steal <= 0)
3770 return -1;
3771
3772 /* 4.2.5 Mapping on a Burst:
3773 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3774 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3775 for (unsigned int i = 0; i < 8; i++)
3776 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3777
3778 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3779 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3780
3781 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3782 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3783 return -1;
3784
3785 return GSM_MACBLOCK_LEN;
3786}
3787
3788/*! Perform channel encoding of a FACCH/H data as per section 4.3.
3789 * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts,
3790 * 6 * 2 * 58 == 696 bits total.
3791 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3792 * \returns 0 in case of success; negative on error */
3793int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data)
3794{
3795 ubit_t iB[8 * 114], cB[4 * 114];
3796 const ubit_t h = 1;
3797
3798 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3799 _xcch_encode_cB(&cB[0], &data[0]);
3800
3801 /* 4.3.4 Interleaving */
3802 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3803
3804 /* 4.3.5 Mapping on a Burst:
3805 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3806 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3807 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3808 for (unsigned int i = 0; i < 6; i++)
3809 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3810 for (unsigned int i = 2; i < 4; i++)
3811 gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1);
3812
3813 return 0;
3814}
3815
3816/*! Perform channel decoding of a FACCH/H data as per section 4.3.
3817 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3818 * \param[in] bursts Buffer containing the symbols of 6 bursts,
3819 * 6 * 2 * 58 == 696 bits total.
3820 * \param[out] n_errors Number of detected bit errors.
3821 * \param[out] n_bits_total Total number of bits.
3822 * \returns Number of bytes used in the output buffer; negative on error. */
3823int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts,
3824 int *n_errors, int *n_bits_total)
3825{
3826 sbit_t iB[8 * 114], cB[4 * 114];
3827 int steal = 0;
3828
3829 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3830 for (unsigned int i = 0; i < 4; i++)
3831 steal -= bursts[i * 116 + 58]; /* hu(B) */
3832 for (unsigned int i = 2; i < 6; i++)
3833 steal -= bursts[i * 116 + 57]; /* hl(B) */
3834 if (steal <= 0)
3835 return -1;
3836
3837 /* 4.3.5 Mapping on a Burst:
3838 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3839 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3840 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3841 for (unsigned int i = 0; i < 6; i++)
3842 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3843 for (unsigned int i = 2; i < 4; i++)
3844 gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1);
3845
3846 /* 4.3.4 Interleaving */
3847 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3848
3849 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3850 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3851 return -1;
3852
3853 return GSM_MACBLOCK_LEN;
3854}
3855
Harald Weltec6636782017-06-12 14:59:37 +02003856/*! @} */