blob: 022f4656b21ca98db8c42500a9e157516b9e781e [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:
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001062 /* reorder, set punctured bits to 0 (unknown state) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001063 for (i = 587, j = 455; i >= 0; i--) {
1064 if (!gsm0503_puncture_cs2[i])
1065 cB[i] = cB[j--];
1066 else
1067 cB[i] = 0;
1068 }
1069
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001070 /* decode as if puncturing was not employed (note '_np') */
Vadim Yanitskiy6c4be022024-01-31 20:59:04 +07001071 osmo_conv_decode_ber_punctured(&gsm0503_cs2_np, cB, conv,
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001072 n_errors, NULL,
Vadim Yanitskiy6c4be022024-01-31 20:59:04 +07001073 gsm0503_puncture_cs2);
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001074 /* indicate the actual amount of coded bits (excluding punctured ones) */
1075 if (n_bits_total != NULL)
1076 *n_bits_total = 456;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001077
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001078 /* 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 +07001079 for (i = 0; i < 8; i++) {
1080 for (j = 0, k = 0; j < 6; j++)
1081 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1082
1083 if (i == 0 || k < best) {
1084 best = k;
1085 usf = i;
1086 }
1087 }
1088
1089 conv[3] = usf & 1;
1090 conv[4] = (usf >> 1) & 1;
1091 conv[5] = (usf >> 2) & 1;
1092 if (usf_p)
1093 *usf_p = usf;
1094
1095 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1096 conv + 3, 271, conv + 3 + 271);
1097 if (rv)
1098 return -1;
1099
1100 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1101
1102 return 34;
1103 case 3:
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001104 /* reorder, set punctured bits to 0 (unknown state) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001105 for (i = 675, j = 455; i >= 0; i--) {
1106 if (!gsm0503_puncture_cs3[i])
1107 cB[i] = cB[j--];
1108 else
1109 cB[i] = 0;
1110 }
1111
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001112 /* decode as if puncturing was not employed (note '_np') */
Vadim Yanitskiy6c4be022024-01-31 20:59:04 +07001113 osmo_conv_decode_ber_punctured(&gsm0503_cs3_np, cB, conv,
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001114 n_errors, NULL,
Vadim Yanitskiy6c4be022024-01-31 20:59:04 +07001115 gsm0503_puncture_cs3);
Vadim Yanitskiy3f4c6a82024-01-31 22:48:32 +07001116 /* indicate the actual amount of coded bits (excluding punctured ones) */
1117 if (n_bits_total != NULL)
1118 *n_bits_total = 456;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001119
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001120 /* 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 +07001121 for (i = 0; i < 8; i++) {
1122 for (j = 0, k = 0; j < 6; j++)
1123 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1124
1125 if (i == 0 || k < best) {
1126 best = k;
1127 usf = i;
1128 }
1129 }
1130
1131 conv[3] = usf & 1;
1132 conv[4] = (usf >> 1) & 1;
1133 conv[5] = (usf >> 2) & 1;
1134 if (usf_p)
1135 *usf_p = usf;
1136
1137 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1138 conv + 3, 315, conv + 3 + 315);
1139 if (rv)
1140 return -1;
1141
1142 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
1143
1144 return 40;
1145 case 4:
1146 for (i = 12; i < 456; i++)
1147 conv[i] = (cB[i] < 0) ? 1 : 0;
1148
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001149 /* 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 +07001150 for (i = 0; i < 8; i++) {
1151 for (j = 0, k = 0; j < 12; j++)
1152 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1153
1154 if (i == 0 || k < best) {
1155 best = k;
1156 usf = i;
1157 }
1158 }
1159
1160 conv[9] = usf & 1;
1161 conv[10] = (usf >> 1) & 1;
1162 conv[11] = (usf >> 2) & 1;
1163 if (usf_p)
1164 *usf_p = usf;
1165
1166 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1167 conv + 9, 431, conv + 9 + 431);
1168 if (rv) {
1169 *n_bits_total = 456 - 12;
1170 *n_errors = *n_bits_total;
1171 return -1;
1172 }
1173
1174 *n_bits_total = 456 - 12;
1175 *n_errors = 0;
1176
1177 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1178
1179 return 54;
1180 default:
1181 *n_bits_total = 0;
1182 *n_errors = 0;
1183 break;
1184 }
1185
1186 return -1;
1187}
1188
1189/*
Pau Espin Pedrolf62f0732020-04-07 13:12:11 +02001190 * EGPRS PDTCH DL block encoding
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001191 */
Harald Welteb9946d32017-06-12 09:40:16 +02001192static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001193{
1194 int i;
1195 ubit_t iB[456];
1196 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1197
Pau Espin Pedrol63ebc362020-04-07 13:15:36 +02001198 gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001199
1200 for (i = 0; i < 4; i++) {
1201 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1202 hl_hn + i * 2, hl_hn + i * 2 + 1);
1203 }
1204
1205 return 0;
1206}
1207
Harald Welteb9946d32017-06-12 09:40:16 +02001208static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001209{
1210 int i;
1211 const ubit_t *up;
1212 ubit_t hi[EGPRS_HDR_HC_MAX];
1213 ubit_t di[EGPRS_DATA_DC_MAX];
1214
1215 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1216 up = gsm0503_mcs5_usf_precode_table[usf];
1217
1218 for (i = 0; i < 4; i++) {
1219 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1220 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1221 }
1222
1223 return 0;
1224}
1225
Harald Welteb9946d32017-06-12 09:40:16 +02001226static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1227 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001228{
1229 int i;
1230 const ubit_t *up;
1231 ubit_t hi[EGPRS_HDR_HC_MAX];
1232 ubit_t di[EGPRS_DATA_C1 * 2];
1233
1234 if (mcs == EGPRS_MCS7)
1235 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1236 else
1237 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1238
1239 up = gsm0503_mcs5_usf_precode_table[usf];
1240
1241 for (i = 0; i < 4; i++) {
1242 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1243 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1244 }
1245
1246 return 0;
1247}
1248
Harald Welteb9946d32017-06-12 09:40:16 +02001249static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001250{
1251 int i, j;
1252 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001253 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001254
1255 code = &gsm0503_mcs_dl_codes[mcs];
1256
1257 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1258 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1259 code->hdr_len, upp + code->hdr_len);
1260
1261 osmo_conv_encode(code->hdr_conv, upp, C);
1262
1263 /* MCS-5,6 header direct puncture instead of table */
1264 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1265 memcpy(hc, C, code->hdr_code_len);
1266 hc[99] = hc[98];
1267 return 0;
1268 }
1269
1270 if (!code->hdr_punc) {
1271 /* Invalid MCS-X header puncture matrix */
1272 return -1;
1273 }
1274
1275 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1276 if (!code->hdr_punc[i])
1277 hc[j++] = C[i];
1278 }
1279
1280 return 0;
1281}
1282
Harald Welteb9946d32017-06-12 09:40:16 +02001283static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001284 int mcs, int p, int blk)
1285{
1286 int i, j, data_len;
1287 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001288 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001289
1290 code = &gsm0503_mcs_dl_codes[mcs];
1291
1292 /*
1293 * Dual block - MCS-7,8,9
1294 * Single block - MCS-1,2,3,4,5,6
1295 */
1296 if (mcs >= EGPRS_MCS7)
1297 data_len = code->data_len / 2;
1298 else
1299 data_len = code->data_len;
1300
1301 osmo_pbit2ubit_ext(u, 0, l2_data,
1302 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1303
1304 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1305
1306 osmo_conv_encode(code->data_conv, u, C);
1307
1308 if (!code->data_punc[p]) {
1309 /* Invalid MCS-X data puncture matrix */
1310 return -1;
1311 }
1312
1313 for (i = 0, j = 0; i < code->data_code_len; i++) {
1314 if (!code->data_punc[p][i])
1315 c[j++] = C[i];
1316 }
1317
1318 return 0;
1319}
1320
1321/*
1322 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1323 *
1324 * Type 1 - MCS-7,8,9
1325 * Type 2 - MCS-5,6
1326 * Type 3 - MCS-1,2,3,4
1327 */
1328static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001329 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001330{
1331 uint8_t bits;
1332
1333 switch (type) {
1334 case EGPRS_HDR_TYPE1:
1335 bits = hdr->type1.cps;
1336 break;
1337 case EGPRS_HDR_TYPE2:
1338 bits = hdr->type2.cps;
1339 break;
1340 case EGPRS_HDR_TYPE3:
1341 bits = hdr->type3.cps;
1342 break;
1343 default:
1344 return -1;
1345 }
1346
1347 return egprs_get_cps(cps, type, bits);
1348}
1349
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001350/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001351 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1352 * \param[in] l2_data L2 (MAC) block to be encoded
1353 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001354 * \returns number of bits encoded; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001355int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001356 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001357{
1358 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1359 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1360 uint8_t mcs;
1361 struct egprs_cps cps;
1362 union gprs_rlc_dl_hdr_egprs *hdr;
1363
1364 switch (l2_len) {
1365 case 27:
1366 mcs = EGPRS_MCS1;
1367 break;
1368 case 33:
1369 mcs = EGPRS_MCS2;
1370 break;
1371 case 42:
1372 mcs = EGPRS_MCS3;
1373 break;
1374 case 49:
1375 mcs = EGPRS_MCS4;
1376 break;
1377 case 60:
1378 mcs = EGPRS_MCS5;
1379 break;
1380 case 78:
1381 mcs = EGPRS_MCS6;
1382 break;
1383 case 118:
1384 mcs = EGPRS_MCS7;
1385 break;
1386 case 142:
1387 mcs = EGPRS_MCS8;
1388 break;
1389 case 154:
1390 mcs = EGPRS_MCS9;
1391 break;
1392 default:
1393 return -1;
1394 }
1395
1396 /* Read header for USF and puncturing matrix selection. */
1397 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1398
1399 switch (mcs) {
1400 case EGPRS_MCS1:
1401 case EGPRS_MCS2:
1402 case EGPRS_MCS3:
1403 case EGPRS_MCS4:
1404 /* Check for valid CPS and matching MCS to message size */
1405 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1406 (cps.mcs != mcs))
1407 goto bad_header;
1408
1409 egprs_encode_hdr(hc, l2_data, mcs);
1410 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1411 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1412 break;
1413 case EGPRS_MCS5:
1414 case EGPRS_MCS6:
1415 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1416 (cps.mcs != mcs))
1417 goto bad_header;
1418
1419 egprs_encode_hdr(hc, l2_data, mcs);
1420 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1421 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1422 break;
1423 case EGPRS_MCS7:
1424 case EGPRS_MCS8:
1425 case EGPRS_MCS9:
1426 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1427 (cps.mcs != mcs))
1428 goto bad_header;
1429
1430 egprs_encode_hdr(hc, l2_data, mcs);
1431 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1432 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1433 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1434 break;
1435 }
1436
1437 return mcs >= EGPRS_MCS5 ?
1438 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1439
1440bad_header:
1441 /* Invalid EGPRS MCS-X header */
1442 return -1;
1443}
1444
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001445/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001446 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1447 * \param[in] l2_data L2 (MAC) block to be encoded
1448 * \param[in] l2_len length of l2_data in bytes, used to determine CS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001449 * \returns number of bits encoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001450int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001451{
1452 ubit_t iB[456], cB[676];
1453 const ubit_t *hl_hn;
1454 ubit_t conv[334];
1455 int i, j, usf;
1456
1457 switch (l2_len) {
1458 case 23:
1459 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1460
1461 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1462
1463 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1464
1465 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1466
1467 break;
1468 case 34:
1469 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1470 usf = l2_data[0] & 0x7;
1471
1472 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1473 271, conv + 3 + 271);
1474
1475 memcpy(conv, gsm0503_usf2six[usf], 6);
1476
1477 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1478
1479 for (i = 0, j = 0; i < 588; i++)
1480 if (!gsm0503_puncture_cs2[i])
1481 cB[j++] = cB[i];
1482
1483 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1484
1485 break;
1486 case 40:
1487 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1488 usf = l2_data[0] & 0x7;
1489
1490 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1491 315, conv + 3 + 315);
1492
1493 memcpy(conv, gsm0503_usf2six[usf], 6);
1494
1495 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1496
1497 for (i = 0, j = 0; i < 676; i++)
1498 if (!gsm0503_puncture_cs3[i])
1499 cB[j++] = cB[i];
1500
1501 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1502
1503 break;
1504 case 54:
1505 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1506 usf = l2_data[0] & 0x7;
1507
1508 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1509 431, cB + 9 + 431);
1510
1511 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1512
1513 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1514
1515 break;
1516 default:
1517 return -1;
1518 }
1519
1520 gsm0503_xcch_interleave(cB, iB);
1521
1522 for (i = 0; i < 4; i++) {
1523 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1524 hl_hn + i * 2, hl_hn + i * 2 + 1);
1525 }
1526
1527 return GSM0503_GPRS_BURSTS_NBITS;
1528}
1529
1530/*
1531 * GSM TCH/F FR/EFR transcoding
1532 */
1533
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001534/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001535 * \param[out] tch_data Codec frame in RTP format
1536 * \param[in] b_bits Codec frame in 'native' format
1537 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001538static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001539 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001540{
1541 int i, j, k, l, o;
1542
1543 tch_data[0] = 0xd << 4;
1544 memset(tch_data + 1, 0, 32);
1545
1546 if (net_order) {
1547 for (i = 0, j = 4; i < 260; i++, j++)
1548 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1549
1550 return;
1551 }
1552
1553 /* reassemble d-bits */
1554 i = 0; /* counts bits */
1555 j = 4; /* counts output bits */
1556 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1557 l = 0; /* counts element bits */
1558 o = 0; /* offset input bits */
1559 while (i < 260) {
1560 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1561 if (--k < 0) {
1562 o += gsm0503_gsm_fr_map[l];
1563 k = gsm0503_gsm_fr_map[++l]-1;
1564 }
1565 i++;
1566 j++;
1567 }
1568}
1569
1570static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001571 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001572{
1573 int i, j, k, l, o;
1574
1575 if (net_order) {
1576 for (i = 0, j = 4; i < 260; i++, j++)
1577 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1578
1579 return;
1580 }
1581
1582 i = 0; /* counts bits */
1583 j = 4; /* counts input bits */
1584 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1585 l = 0; /* counts element bits */
1586 o = 0; /* offset output bits */
1587 while (i < 260) {
1588 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1589 if (--k < 0) {
1590 o += gsm0503_gsm_fr_map[l];
1591 k = gsm0503_gsm_fr_map[++l] - 1;
1592 }
1593 i++;
1594 j++;
1595 }
1596}
1597
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001598/* assemble a HR codec frame in the canonical format of ETSI TS 101 318 */
Harald Welteb9946d32017-06-12 09:40:16 +02001599static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001600{
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001601 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001602
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001603 memset(tch_data, 0, GSM_HR_BYTES);
1604 for (i = 0; i < 112; i++)
1605 tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7)));
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001606}
1607
Harald Welteb9946d32017-06-12 09:40:16 +02001608static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001609{
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001610 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001611
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001612 for (i = 0; i < 112; i++)
1613 b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001614}
1615
Harald Weltec6636782017-06-12 14:59:37 +02001616/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001617static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001618{
1619 int i, j;
1620
1621 tch_data[0] = 0xc << 4;
1622 memset(tch_data + 1, 0, 30);
1623
1624 for (i = 0, j = 4; i < 244; i++, j++)
1625 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1626}
1627
Harald Welteb9946d32017-06-12 09:40:16 +02001628static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001629{
1630 int i, j;
1631
1632 for (i = 0, j = 4; i < 244; i++, j++)
1633 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1634}
1635
Harald Weltec6636782017-06-12 14:59:37 +02001636/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001637static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001638{
1639 int i, j;
1640
1641 memset(tch_data, 0, (len + 7) >> 3);
1642
1643 for (i = 0, j = 0; i < len; i++, j++)
1644 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1645}
1646
Harald Welteb9946d32017-06-12 09:40:16 +02001647static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001648{
1649 int i, j;
1650
1651 for (i = 0, j = 0; i < len; i++, j++)
1652 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1653}
1654
Philipp Maier898c9c62020-02-06 14:25:01 +01001655/* Append STI and MI bits to the SID_UPDATE frame, see also
1656 * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */
1657static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi)
1658{
1659 /* Zero out the space that had been used by the CRC14 */
1660 memset(sid_update + 35, 0, 14);
1661
1662 /* Append STI and MI parameters */
1663 sid_update[35] = sti & 1;
1664 sid_update[36] = mi & 1;
1665 sid_update[37] = mi >> 1 & 1;
1666 sid_update[38] = mi >> 2 & 1;
1667}
1668
1669/* Extract a SID UPDATE fram the sbits of an FR AMR frame */
1670static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits)
1671{
1672
1673 unsigned int i;
1674
1675 sbits += 32;
1676
1677 for (i = 0; i < 53; i++) {
1678 sid_update[0] = sbits[0];
1679 sid_update[1] = sbits[1];
1680 sid_update[2] = sbits[2];
1681 sid_update[3] = sbits[3];
1682 sid_update += 4;
1683 sbits += 8;
1684 }
1685
1686}
1687
Harald Weltec6636782017-06-12 14:59:37 +02001688/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001689static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001690{
1691 int i;
1692
1693 for (i = 0; i < 260; i++)
1694 b_bits[gsm610_bitorder[i]] = d_bits[i];
1695}
1696
Harald Weltec6636782017-06-12 14:59:37 +02001697/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001698static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001699{
1700 int i;
1701
1702 for (i = 0; i < 260; i++)
1703 d_bits[i] = b_bits[gsm610_bitorder[i]];
1704}
1705
Harald Weltec6636782017-06-12 14:59:37 +02001706/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001707static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001708{
1709 int i;
1710
1711 const uint16_t *map;
1712
1713 if (!d_bits[93] && !d_bits[94])
1714 map = gsm620_unvoiced_bitorder;
1715 else
1716 map = gsm620_voiced_bitorder;
1717
1718 for (i = 0; i < 112; i++)
1719 b_bits[map[i]] = d_bits[i];
1720}
1721
Harald Weltec6636782017-06-12 14:59:37 +02001722/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001723static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001724{
1725 int i;
1726 const uint16_t *map;
1727
1728 if (!b_bits[34] && !b_bits[35])
1729 map = gsm620_unvoiced_bitorder;
1730 else
1731 map = gsm620_voiced_bitorder;
1732
1733 for (i = 0; i < 112; i++)
1734 d_bits[i] = b_bits[map[i]];
1735}
1736
Harald Weltec6636782017-06-12 14:59:37 +02001737/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001738static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001739{
1740 int i;
1741
1742 for (i = 0; i < 260; i++)
1743 b_bits[gsm660_bitorder[i]] = d_bits[i];
1744}
1745
Harald Weltec6636782017-06-12 14:59:37 +02001746/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001747static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001748{
1749 int i;
1750
1751 for (i = 0; i < 260; i++)
1752 d_bits[i] = b_bits[gsm660_bitorder[i]];
1753}
1754
Harald Weltec6636782017-06-12 14:59:37 +02001755/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001756static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001757{
1758 int i;
1759
1760 for (i = 0; i < 65; i++)
1761 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1762}
1763
Harald Welteb9946d32017-06-12 09:40:16 +02001764static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001765{
1766 int i;
1767
1768 for (i = 0; i < 91; i++) {
1769 d[i << 1] = u[i];
1770 d[(i << 1) + 1] = u[184 - i];
1771 }
1772
1773 for (i = 0; i < 3; i++)
1774 p[i] = u[91 + i];
1775}
1776
Harald Welteb9946d32017-06-12 09:40:16 +02001777static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001778{
1779 int i;
1780
1781 for (i = 0; i < 91; i++) {
1782 u[i] = d[i << 1];
1783 u[184 - i] = d[(i << 1) + 1];
1784 }
1785
1786 for (i = 0; i < 3; i++)
1787 u[91 + i] = p[i];
1788}
1789
Harald Welteb9946d32017-06-12 09:40:16 +02001790static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001791{
1792 memcpy(d, u, 95);
1793 memcpy(p, u + 95, 3);
1794}
1795
Harald Welteb9946d32017-06-12 09:40:16 +02001796static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001797{
1798 memcpy(u, d, 95);
1799 memcpy(u + 95, p, 3);
1800}
1801
Harald Welteb9946d32017-06-12 09:40:16 +02001802static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001803{
1804 memcpy(w, s, 71);
1805 w[71] = w[72] = s[69];
1806 memcpy(w + 73, s + 71, 50);
1807 w[123] = w[124] = s[119];
1808 memcpy(w + 125, s + 121, 53);
1809 w[178] = w[179] = s[172];
1810 memcpy(w + 180, s + 174, 50);
1811 w[230] = w[231] = s[222];
1812 memcpy(w + 232, s + 224, 20);
1813 memcpy(w + 252, p, 8);
1814}
1815
Harald Welteb9946d32017-06-12 09:40:16 +02001816static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001817{
1818 int sum;
1819
1820 memcpy(s, w, 71);
1821 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001822 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001823 memcpy(s + 71, w + 73, 50);
1824 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001825 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001826 memcpy(s + 121, w + 125, 53);
1827 sum = s[172] + w[178] + w[179];
Mychaela N. Falconiaf3e47452023-05-17 18:58:40 +00001828 s[172] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001829 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001830 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001831 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001832 memcpy(s + 224, w + 232, 20);
1833 memcpy(p, w + 252, 8);
1834}
1835
Harald Welteb9946d32017-06-12 09:40:16 +02001836static 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 +07001837{
1838 memcpy(u, d, prot);
1839 memcpy(u + prot, p, 6);
1840 memcpy(u + prot + 6, d + prot, len - prot);
1841}
1842
Harald Welteb9946d32017-06-12 09:40:16 +02001843static 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 +07001844{
1845 memcpy(d, u, prot);
1846 memcpy(p, u + prot, 6);
1847 memcpy(d + prot, u + prot + 6, len - prot);
1848}
1849
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001850/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001851 * \param[out] tch_data Codec frame in RTP payload format
1852 * \param[in] bursts buffer containing the symbols of 8 bursts
1853 * \param[in] net_order FIXME
1854 * \param[in] efr Is this channel using EFR (1) or FR (0)
1855 * \param[out] n_errors Number of detected bit errors
1856 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001857 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001858int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001859 int net_order, int efr, int *n_errors, int *n_bits_total)
1860{
1861 sbit_t iB[912], cB[456], h;
1862 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1863 int i, rv, len, steal = 0;
1864
Harald Weltec6636782017-06-12 14:59:37 +02001865 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001866 for (i = 0; i < 8; i++) {
1867 gsm0503_tch_burst_unmap(&iB[i * 114],
1868 &bursts[i * 116], &h, i >> 2);
1869 steal -= h;
1870 }
Harald Weltec6636782017-06-12 14:59:37 +02001871 /* we now have the bits of the four bursts (interface 4 in
1872 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001873
1874 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001875 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001876
1877 if (steal > 0) {
1878 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1879 if (rv) {
1880 /* Error decoding FACCH frame */
1881 return -1;
1882 }
1883
Vadim Yanitskiyc0305b62023-05-20 16:33:38 +07001884 return GSM_MACBLOCK_LEN;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001885 }
1886
1887 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001888 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001889
Harald Weltec6636782017-06-12 14:59:37 +02001890 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001891 tch_fr_unreorder(d, p, conv);
1892
1893 for (i = 0; i < 78; i++)
1894 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1895
Harald Weltec6636782017-06-12 14:59:37 +02001896 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001897 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1898 if (rv) {
1899 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1900 return -1;
1901 }
1902
1903 if (efr) {
1904 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001905 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001906
1907 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001908 /* we now have the data delivered to the preliminary
1909 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001910
Harald Weltec6636782017-06-12 14:59:37 +02001911 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001912 tch_efr_protected(s, b);
1913
Harald Weltec6636782017-06-12 14:59:37 +02001914 /* perform CRC-8 on 65 most important bits (50 bits of
1915 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001916 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1917 if (rv) {
1918 /* Error checking CRC8 for the EFR part of an EFR frame */
1919 return -1;
1920 }
1921
1922 tch_efr_reassemble(tch_data, s);
1923
1924 len = GSM_EFR_BYTES;
1925 } else {
1926 tch_fr_d_to_b(w, d);
1927
1928 tch_fr_reassemble(tch_data, w, net_order);
1929
1930 len = GSM_FR_BYTES;
1931 }
1932
1933 return len;
1934}
1935
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001936/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001937 * \param[out] bursts caller-allocated output buffer for bursts bits
1938 * \param[in] tch_data Codec input data in RTP payload format
1939 * \param[in] len Length of \a tch_data in bytes
1940 * \param[in] net_order FIXME
1941 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001942int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001943 int len, int net_order)
1944{
1945 ubit_t iB[912], cB[456], h;
1946 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1947 int i;
1948
1949 switch (len) {
1950 case GSM_EFR_BYTES: /* TCH EFR */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001951 tch_efr_disassemble(s, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001952 tch_efr_protected(s, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001953 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001954 tch_efr_reorder(w, s, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001955 tch_efr_w_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001956 goto coding_efr_fr;
1957 case GSM_FR_BYTES: /* TCH FR */
1958 tch_fr_disassemble(w, tch_data, net_order);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001959 tch_fr_b_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001960coding_efr_fr:
1961 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001962 tch_fr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001963 memcpy(cB + 378, d + 182, 78);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001964 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001965 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001966 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00001967 case 0: /* no data, induce BFI in the receiver */
1968 /* Do the same thing that sysmoBTS PHY does when fed a 0-length
1969 * payload for DL: set all u(k) bits to 0, and do the same
1970 * with all class 2 bits. This operation is NOT the same as
1971 * an FR codec frame of all zero bits: with all-zeros d(k) input
1972 * the CRC3 function will produce 111 output, whereas we
1973 * transmit 000 in those parity bits too. The result will be
1974 * an induced BFI (bad frame indication) condition in the
1975 * receiver, for both TCH/FS and TCH/EFS decoders. */
1976 memset(conv, 0, sizeof(conv));
1977 memset(cB + 378, 0, 78);
1978 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1979 h = 0;
1980 break;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001981 case GSM_MACBLOCK_LEN: /* FACCH */
1982 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001983 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001984 break;
1985 default:
1986 return -1;
1987 }
1988
1989 gsm0503_tch_fr_interleave(cB, iB);
1990
1991 for (i = 0; i < 8; i++) {
1992 gsm0503_tch_burst_map(&iB[i * 114],
1993 &bursts[i * 116], &h, i >> 2);
1994 }
1995
1996 return 0;
1997}
1998
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001999/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002000 * \param[out] tch_data Codec frame in TS 101 318 canonical format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002001 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02002002 * \param[in] odd Odd (1) or even (0) frame number
2003 * \param[out] n_errors Number of detected bit errors
2004 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002005 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002006int gsm0503_tch_hr_decode2(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002007 int *n_errors, int *n_bits_total)
2008{
2009 sbit_t iB[912], cB[456], h;
2010 ubit_t conv[98], b[112], d[112], p[3];
2011 int i, rv, steal = 0;
2012
2013 /* Only unmap the stealing bits */
2014 if (!odd) {
2015 for (i = 0; i < 4; i++) {
2016 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2017 steal -= h;
2018 }
2019
Mychaela N. Falconia920c0ed2023-05-23 14:06:53 +00002020 for (i = 2; i < 6; i++) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002021 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2022 steal -= h;
2023 }
2024 }
2025
2026 /* If we found a stole FACCH, but only at correct alignment */
2027 if (steal > 0) {
2028 for (i = 0; i < 6; i++) {
2029 gsm0503_tch_burst_unmap(&iB[i * 114],
2030 &bursts[i * 116], NULL, i >> 2);
2031 }
2032
2033 for (i = 2; i < 4; i++) {
2034 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2035 &bursts[i * 116], NULL, 1);
2036 }
2037
2038 gsm0503_tch_fr_deinterleave(cB, iB);
2039
2040 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2041 if (rv) {
2042 /* Error decoding FACCH frame */
2043 return -1;
2044 }
2045
2046 return GSM_MACBLOCK_LEN;
2047 }
2048
2049 for (i = 0; i < 4; i++) {
2050 gsm0503_tch_burst_unmap(&iB[i * 114],
2051 &bursts[i * 116], NULL, i >> 1);
2052 }
2053
2054 gsm0503_tch_hr_deinterleave(cB, iB);
2055
2056 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
2057
2058 tch_hr_unreorder(d, p, conv);
2059
2060 for (i = 0; i < 17; i++)
2061 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
2062
2063 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2064 if (rv) {
2065 /* Error checking CRC8 for an HR frame */
2066 return -1;
2067 }
2068
2069 tch_hr_d_to_b(b, d);
2070
2071 tch_hr_reassemble(tch_data, b);
2072
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002073 return GSM_HR_BYTES;
2074}
2075
2076/*! Perform channel decoding of a HR(v1) channel according TS 05.03,
2077 * deprecated legacy API.
2078 * \param[out] tch_data Codec frame in pseudo-RFC5993 format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002079 * \param[in] bursts buffer containing the symbols of 6 bursts
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002080 * \param[in] odd Odd (1) or even (0) frame number
2081 * \param[out] n_errors Number of detected bit errors
2082 * \param[out] n_bits_total Total number of bits
2083 * \returns length of bytes used in \a tch_data output buffer; negative on error
2084 *
2085 * The HR1 codec frame format returned by this function is pseudo-RFC5993,
2086 * not true RFC 5993, as there is no SID classification being done
2087 * and the FT bits in the ToC octet are always set to 0 - but this
2088 * arguably-bogus format is the legacy public API.
2089 */
2090int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
2091 int *n_errors, int *n_bits_total)
2092{
2093 int rc;
2094
2095 rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors,
2096 n_bits_total);
2097 if (rc != GSM_HR_BYTES)
2098 return rc;
2099 memmove(tch_data + 1, tch_data, GSM_HR_BYTES);
2100 tch_data[0] = 0x00; /* FT=0, note absence of SID classification */
2101 return GSM_HR_BYTES_RTP_RFC5993;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002102}
2103
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002104/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002105 * \param[out] bursts caller-allocated output buffer for bursts bits
2106 * \param[in] tch_data Codec input data in RTP payload format
2107 * \param[in] len Length of \a tch_data in bytes
2108 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002109int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002110{
2111 ubit_t iB[912], cB[456], h;
2112 ubit_t conv[98], b[112], d[112], p[3];
2113 int i;
2114
2115 switch (len) {
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00002116 case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */
2117 tch_data++;
2118 /* fall-through */
2119 case GSM_HR_BYTES: /* TCH HR in "pure" form */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002120 tch_hr_disassemble(b, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002121 tch_hr_b_to_d(d, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002122 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002123 tch_hr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002124 memcpy(cB + 211, d + 95, 17);
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002125hr_conv_coding:
2126 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002127 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002128 gsm0503_tch_hr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002129 for (i = 0; i < 4; i++) {
2130 gsm0503_tch_burst_map(&iB[i * 114],
2131 &bursts[i * 116], &h, i >> 1);
2132 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002133 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002134 case 0: /* no data, induce BFI in the receiver */
2135 /* see comments in gsm0503_tch_fr_encode() - same deal here */
2136 memset(conv, 0, sizeof(conv));
2137 memset(cB + 211, 0, 17);
2138 goto hr_conv_coding;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002139 case GSM_MACBLOCK_LEN: /* FACCH */
2140 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002141 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002142 gsm0503_tch_fr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002143 for (i = 0; i < 6; i++) {
2144 gsm0503_tch_burst_map(&iB[i * 114],
2145 &bursts[i * 116], &h, i >> 2);
2146 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002147 for (i = 2; i < 4; i++) {
2148 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2149 &bursts[i * 116], &h, 1);
2150 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002151 break;
2152 default:
2153 return -1;
2154 }
2155
2156 return 0;
2157}
2158
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002159/* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2160static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB)
2161{
2162 unsigned int id = 0, best = 0;
2163 unsigned int i, j, k;
2164
2165 for (i = 0; i < 4; i++) {
2166 /* FIXME: why not using remaining (16 - 8) soft-bits here? */
2167 for (j = 0, k = 0; j < 8; j++)
2168 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2169
2170 if (i == 0 || k < best) {
2171 best = k;
2172 id = i;
2173 }
2174 }
2175
2176 return id;
2177}
2178
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002179/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002180 * \param[out] tch_data Codec frame in RTP payload format
2181 * \param[in] bursts buffer containing the symbols of 8 bursts
2182 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2183 * \param[in] codec array of active codecs (active codec set)
2184 * \param[in] codecs number of codecs in \a codec
2185 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2186 * \param[out] cmr Output in \a codec_mode_req = 1
2187 * \param[out] n_errors Number of detected bit errors
2188 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002189 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2190 * codec out of range; negative on error
2191 */
Harald Welteb9946d32017-06-12 09:40:16 +02002192int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002193 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2194 uint8_t *cmr, int *n_errors, int *n_bits_total)
2195{
Philipp Maier898c9c62020-02-06 14:25:01 +01002196 return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req,
2197 codec, codecs, ft, cmr, n_errors,
2198 n_bits_total, NULL);
2199}
2200
2201/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2202 * \param[out] tch_data Codec frame in RTP payload format
2203 * \param[in] bursts buffer containing the symbols of 8 bursts
2204 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2205 * \param[in] codec array of active codecs (active codec set)
2206 * \param[in] codecs number of codecs in \a codec
2207 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2208 * \param[out] cmr Output in \a codec_mode_req = 1
2209 * \param[out] n_errors Number of detected bit errors
2210 * \param[out] n_bits_total Total number of bits
2211 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2212 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2213 * codec out of range; negative on error
2214 */
2215int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts,
2216 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2217 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2218{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002219 sbit_t iB[912], cB[456], h;
2220 ubit_t d[244], p[6], conv[250];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002221 int i, rv, len, steal = 0, id = -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002222 *n_errors = 0; *n_bits_total = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002223 static ubit_t sid_first_dummy[64] = { 0 };
2224 sbit_t sid_update_enc[256];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002225
2226 for (i=0; i<8; i++) {
2227 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2228 steal -= h;
2229 }
2230
2231 gsm0503_tch_fr_deinterleave(cB, iB);
2232
2233 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002234 /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002235 * It's normally updated by gsm0503_detect_afs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002236 * reached in case of FACCH. Reset it here to avoid FACCH/F frames being
2237 * misinterpreted as AMR's special DTX frames. */
2238 if (dtx != NULL)
2239 *dtx = AMR_OTHER;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002240 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2241 if (rv) {
2242 /* Error decoding FACCH frame */
2243 return -1;
2244 }
2245
2246 return GSM_MACBLOCK_LEN;
2247 }
2248
Philipp Maier898c9c62020-02-06 14:25:01 +01002249 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2250 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002251 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2252
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002253 *dtx = gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Philipp Maier898c9c62020-02-06 14:25:01 +01002254
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002255 switch (*dtx) {
2256 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002257 /* NOTE: The AFS_SID_UPDATE frame is splitted into
2258 * two half rate frames. If the id marker frame
2259 * (AFS_SID_UPDATE) is detected the following frame
2260 * contains the actual comfort noised data part of
2261 * (AFS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002262 if (dtx_prev != AFS_SID_UPDATE)
2263 break;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002264 /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002265 *dtx = AFS_SID_UPDATE_CN;
2266
2267 extract_afs_sid_update(sid_update_enc, cB);
2268 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2269 sid_update_enc, conv, n_errors,
2270 n_bits_total);
2271 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2272 35, conv + 35);
2273 if (rv != 0) {
2274 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2275 return -1;
2276 }
2277
2278 tch_amr_sid_update_append(conv, 1,
2279 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002280 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002281 tch_amr_reassemble(tch_data, conv, 39);
2282 len = 5;
2283 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002284 case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002285 tch_amr_sid_update_append(sid_first_dummy, 0,
2286 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002287 : codec[id > 0 ? id : 0]);
Vadim Yanitskiy7d121cb2023-06-23 16:57:48 +07002288 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
Philipp Maier898c9c62020-02-06 14:25:01 +01002289 len = 5;
2290 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002291 case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002292 case AFS_ONSET:
Philipp Maier898c9c62020-02-06 14:25:01 +01002293 len = 0;
2294 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002295 default:
2296 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002297 }
2298 }
2299
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002300 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2301 if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002302 /* Codec mode out of range, return id */
2303 return id;
2304 }
2305
2306 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2307 case 7: /* TCH/AFS12.2 */
2308 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2309 conv, n_errors, n_bits_total);
2310
2311 tch_amr_unmerge(d, p, conv, 244, 81);
2312
2313 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2314 if (rv) {
2315 /* Error checking CRC8 for an AMR 12.2 frame */
2316 return -1;
2317 }
2318
2319 tch_amr_reassemble(tch_data, d, 244);
2320
2321 len = 31;
2322
2323 break;
2324 case 6: /* TCH/AFS10.2 */
2325 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2326 conv, n_errors, n_bits_total);
2327
2328 tch_amr_unmerge(d, p, conv, 204, 65);
2329
2330 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2331 if (rv) {
2332 /* Error checking CRC8 for an AMR 10.2 frame */
2333 return -1;
2334 }
2335
2336 tch_amr_reassemble(tch_data, d, 204);
2337
2338 len = 26;
2339
2340 break;
2341 case 5: /* TCH/AFS7.95 */
2342 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2343 conv, n_errors, n_bits_total);
2344
2345 tch_amr_unmerge(d, p, conv, 159, 75);
2346
2347 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2348 if (rv) {
2349 /* Error checking CRC8 for an AMR 7.95 frame */
2350 return -1;
2351 }
2352
2353 tch_amr_reassemble(tch_data, d, 159);
2354
2355 len = 20;
2356
2357 break;
2358 case 4: /* TCH/AFS7.4 */
2359 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2360 conv, n_errors, n_bits_total);
2361
2362 tch_amr_unmerge(d, p, conv, 148, 61);
2363
2364 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2365 if (rv) {
2366 /* Error checking CRC8 for an AMR 7.4 frame */
2367 return -1;
2368 }
2369
2370 tch_amr_reassemble(tch_data, d, 148);
2371
2372 len = 19;
2373
2374 break;
2375 case 3: /* TCH/AFS6.7 */
2376 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2377 conv, n_errors, n_bits_total);
2378
2379 tch_amr_unmerge(d, p, conv, 134, 55);
2380
2381 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2382 if (rv) {
2383 /* Error checking CRC8 for an AMR 6.7 frame */
2384 return -1;
2385 }
2386
2387 tch_amr_reassemble(tch_data, d, 134);
2388
2389 len = 17;
2390
2391 break;
2392 case 2: /* TCH/AFS5.9 */
2393 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2394 conv, n_errors, n_bits_total);
2395
2396 tch_amr_unmerge(d, p, conv, 118, 55);
2397
2398 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2399 if (rv) {
2400 /* Error checking CRC8 for an AMR 5.9 frame */
2401 return -1;
2402 }
2403
2404 tch_amr_reassemble(tch_data, d, 118);
2405
2406 len = 15;
2407
2408 break;
2409 case 1: /* TCH/AFS5.15 */
2410 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2411 conv, n_errors, n_bits_total);
2412
2413 tch_amr_unmerge(d, p, conv, 103, 49);
2414
2415 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2416 if (rv) {
2417 /* Error checking CRC8 for an AMR 5.15 frame */
2418 return -1;
2419 }
2420
2421 tch_amr_reassemble(tch_data, d, 103);
2422
2423 len = 13;
2424
2425 break;
2426 case 0: /* TCH/AFS4.75 */
2427 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2428 conv, n_errors, n_bits_total);
2429
2430 tch_amr_unmerge(d, p, conv, 95, 39);
2431
2432 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2433 if (rv) {
2434 /* Error checking CRC8 for an AMR 4.75 frame */
2435 return -1;
2436 }
2437
2438 tch_amr_reassemble(tch_data, d, 95);
2439
2440 len = 12;
2441
2442 break;
2443 default:
2444 /* Unknown frame type */
2445 *n_bits_total = 448;
2446 *n_errors = *n_bits_total;
2447 return -1;
2448 }
2449
Philipp Maier898c9c62020-02-06 14:25:01 +01002450out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002451 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002452 if (id != -1) {
2453 if (codec_mode_req)
2454 *cmr = id;
2455 else
2456 *ft = id;
2457 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002458
2459 return len;
2460}
2461
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002462/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002463 * \param[out] bursts caller-allocated output buffer for bursts bits
2464 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002465 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002466 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2467 * \param[in] codec Array of codecs (active codec set)
2468 * \param[in] codecs Number of entries in \a codec
2469 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2470 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2471 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002472int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002473 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002474 uint8_t cmr)
2475{
2476 ubit_t iB[912], cB[456], h;
2477 ubit_t d[244], p[6], conv[250];
2478 int i;
2479 uint8_t id;
2480
2481 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2482 _xcch_encode_cB(cB, tch_data);
2483
2484 h = 1;
2485
2486 goto facch;
2487 }
2488
2489 h = 0;
2490
Vadim Yanitskiya9952692023-11-10 03:31:55 +07002491 id = codec_mode_req ? cmr : ft;
2492 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit)))
2493 return -1;
2494 if (OSMO_UNLIKELY(ft >= codecs))
2495 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002496
2497 switch (codec[ft]) {
2498 case 7: /* TCH/AFS12.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002499 if (!len) {
2500 /* No data, induce BFI in the receiver by inverted CRC bits.
2501 * The data bit are all 0, so the correct parity bits would be 111111. */
2502 memset(d, 0, 244);
2503 memset(p, 0, 6);
2504 } else {
2505 if (len != 31)
2506 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002507
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002508 tch_amr_disassemble(d, tch_data, 244);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002509
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002510 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2511 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002512
2513 tch_amr_merge(conv, d, p, 244, 81);
2514
2515 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2516
2517 break;
2518 case 6: /* TCH/AFS10.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002519 if (!len) {
2520 /* See comment above. */
2521 memset(d, 0, 204);
2522 memset(p, 0, 6);
2523 } else {
2524 if (len != 26)
2525 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002526
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002527 tch_amr_disassemble(d, tch_data, 204);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002528
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002529 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2530 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002531
2532 tch_amr_merge(conv, d, p, 204, 65);
2533
2534 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2535
2536 break;
2537 case 5: /* TCH/AFS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002538 if (!len) {
2539 /* See comment above. */
2540 memset(d, 0, 159);
2541 memset(p, 0, 6);
2542 } else {
2543 if (len != 20)
2544 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002545
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002546 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002547
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002548 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2549 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002550
2551 tch_amr_merge(conv, d, p, 159, 75);
2552
2553 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2554
2555 break;
2556 case 4: /* TCH/AFS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002557 if (!len) {
2558 /* See comment above. */
2559 memset(d, 0, 148);
2560 memset(p, 0, 6);
2561 } else {
2562 if (len != 19)
2563 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002564
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002565 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002566
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002567 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2568 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002569
2570 tch_amr_merge(conv, d, p, 148, 61);
2571
2572 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2573
2574 break;
2575 case 3: /* TCH/AFS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002576 if (!len) {
2577 /* See comment above. */
2578 memset(d, 0, 134);
2579 memset(p, 0, 6);
2580 } else {
2581 if (len != 17)
2582 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002583
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002584 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002585
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002586 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2587 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002588
2589 tch_amr_merge(conv, d, p, 134, 55);
2590
2591 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2592
2593 break;
2594 case 2: /* TCH/AFS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002595 if (!len) {
2596 /* See comment above. */
2597 memset(d, 0, 118);
2598 memset(p, 0, 6);
2599 } else {
2600 if (len != 15)
2601 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002602
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002603 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002604
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002605 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2606 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002607
2608 tch_amr_merge(conv, d, p, 118, 55);
2609
2610 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2611
2612 break;
2613 case 1: /* TCH/AFS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002614 if (!len) {
2615 /* See comment above. */
2616 memset(d, 0, 103);
2617 memset(p, 0, 6);
2618 } else {
2619 if (len != 13)
2620 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002621
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002622 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002623
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002624 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2625 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002626
2627 tch_amr_merge(conv, d, p, 103, 49);
2628
2629 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2630
2631 break;
2632 case 0: /* TCH/AFS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002633 if (!len) {
2634 /* See comment above. */
2635 memset(d, 0, 95);
2636 memset(p, 0, 6);
2637 } else {
2638 if (len != 12)
2639 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002640
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002641 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002642
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002643 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2644 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002645
2646 tch_amr_merge(conv, d, p, 95, 39);
2647
2648 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2649
2650 break;
2651 default:
2652 /* FIXME: FT %ft is not supported */
2653 return -1;
2654 }
2655
2656 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2657
2658facch:
2659 gsm0503_tch_fr_interleave(cB, iB);
2660
2661 for (i = 0; i < 8; i++) {
2662 gsm0503_tch_burst_map(&iB[i * 114],
2663 &bursts[i * 116], &h, i >> 2);
2664 }
2665
2666 return 0;
2667
2668invalid_length:
2669 /* FIXME: payload length %len does not comply with codec type %ft */
2670 return -1;
2671}
2672
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002673/* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2674static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB)
2675{
2676 unsigned int id = 0, best = 0;
2677 unsigned int i, j, k;
2678
2679 for (i = 0, k = 0; i < 4; i++) {
2680 /* FIXME: why not using remaining (16 - 4) soft-bits here? */
2681 for (j = 0, k = 0; j < 4; j++)
2682 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2683
2684 if (i == 0 || k < best) {
2685 best = k;
2686 id = i;
2687 }
2688 }
2689
2690 return id;
2691}
2692
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002693/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002694 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002695 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02002696 * \param[in] odd Is this an odd (1) or even (0) frame number?
2697 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2698 * \param[in] codec array of active codecs (active codec set)
2699 * \param[in] codecs number of codecs in \a codec
2700 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2701 * \param[out] cmr Output in \a codec_mode_req = 1
2702 * \param[out] n_errors Number of detected bit errors
2703 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002704 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2705 * codec out of range; negative on error
2706 */
Harald Welteb9946d32017-06-12 09:40:16 +02002707int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002708 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2709 uint8_t *cmr, int *n_errors, int *n_bits_total)
2710{
Philipp Maier898c9c62020-02-06 14:25:01 +01002711 return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req,
2712 codec, codecs, ft, cmr, n_errors,
2713 n_bits_total, NULL);
2714}
2715
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002716/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Philipp Maier898c9c62020-02-06 14:25:01 +01002717 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002718 * \param[in] bursts buffer containing the symbols of 6 bursts
Philipp Maier898c9c62020-02-06 14:25:01 +01002719 * \param[in] odd Is this an odd (1) or even (0) frame number?
2720 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2721 * \param[in] codec array of active codecs (active codec set)
2722 * \param[in] codecs number of codecs in \a codec
2723 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2724 * \param[out] cmr Output in \a codec_mode_req = 1
2725 * \param[out] n_errors Number of detected bit errors
2726 * \param[out] n_bits_total Total number of bits
2727 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2728 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2729 * codec out of range; negative on error
2730 */
2731int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd,
2732 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2733 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2734{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002735 sbit_t iB[912], cB[456], h;
2736 ubit_t d[244], p[6], conv[135];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002737 int i, rv, len, steal = 0, id = -1;
Philipp Maier898c9c62020-02-06 14:25:01 +01002738 static ubit_t sid_first_dummy[64] = { 0 };
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002739
2740 /* only unmap the stealing bits */
2741 if (!odd) {
2742 for (i = 0; i < 4; i++) {
2743 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2744 steal -= h;
2745 }
2746 for (i = 2; i < 5; i++) {
2747 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2748 steal -= h;
2749 }
2750 }
2751
2752 /* if we found a stole FACCH, but only at correct alignment */
2753 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002754 /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002755 * It's normally updated by gsm0503_detect_ahs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002756 * reached in case of FACCH. Reset it here to avoid FACCH/H frames being
2757 * misinterpreted as AMR's special DTX frames. */
2758 if (dtx != NULL)
2759 *dtx = AMR_OTHER;
2760
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002761 for (i = 0; i < 6; i++) {
2762 gsm0503_tch_burst_unmap(&iB[i * 114],
2763 &bursts[i * 116], NULL, i >> 2);
2764 }
2765
2766 for (i = 2; i < 4; i++) {
2767 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2768 &bursts[i * 116], NULL, 1);
2769 }
2770
2771 gsm0503_tch_fr_deinterleave(cB, iB);
2772
2773 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2774 if (rv) {
2775 /* Error decoding FACCH frame */
2776 return -1;
2777 }
2778
2779 return GSM_MACBLOCK_LEN;
2780 }
2781
2782 for (i = 0; i < 4; i++) {
2783 gsm0503_tch_burst_unmap(&iB[i * 114],
2784 &bursts[i * 116], NULL, i >> 1);
2785 }
2786
2787 gsm0503_tch_hr_deinterleave(cB, iB);
2788
Philipp Maier898c9c62020-02-06 14:25:01 +01002789 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2790 if (dtx) {
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002791 int n_bits_total_sid;
2792 int n_errors_sid;
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002793
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002794 *dtx = gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002795 /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */
Philipp Maier898c9c62020-02-06 14:25:01 +01002796
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002797 switch (*dtx) {
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002798 case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002799 /* cB[] contains 16 bits of coded in-band data and 212 bits containing
2800 * the identification marker. We need to unmap/deinterleave 114 odd
2801 * bits from the last two blocks, 114 even bits from the first two
2802 * blocks and combine them together. */
2803 gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0);
2804 gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0);
2805 gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1);
2806 gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1);
2807 gsm0503_tch_hr_deinterleave(cB, iB);
2808
2809 /* cB[] is expected to contain 16 bits of coded in-band data and
2810 * 212 bits containing the coded data (53 bits coded at 1/4 rate). */
Philipp Maier898c9c62020-02-06 14:25:01 +01002811 *dtx = AHS_SID_UPDATE_CN;
2812
2813 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002814 cB + 16, conv, &n_errors_sid,
2815 &n_bits_total_sid);
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002816 /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002817 * osmo_conv_decode_ber() calculates BER for the coded data. */
2818 if (n_errors != NULL)
2819 *n_errors += n_errors_sid;
2820 if (n_bits_total != NULL)
2821 *n_bits_total += n_bits_total_sid;
Philipp Maier898c9c62020-02-06 14:25:01 +01002822 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2823 35, conv + 35);
2824 if (rv != 0) {
2825 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2826 return -1;
2827 }
2828
2829 tch_amr_sid_update_append(conv, 1,
2830 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002831 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002832 tch_amr_reassemble(tch_data, conv, 39);
2833 len = 5;
2834 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002835 case AHS_SID_FIRST_P2:
Philipp Maier898c9c62020-02-06 14:25:01 +01002836 tch_amr_sid_update_append(sid_first_dummy, 0,
2837 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002838 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002839 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
2840 len = 5;
2841 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002842 case AHS_ONSET:
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002843 case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2844 case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2845 case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002846 len = 0;
2847 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002848 default:
2849 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002850 }
2851 }
2852
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002853 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2854 if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002855 /* Codec mode out of range, return id */
2856 return id;
2857 }
2858
2859 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2860 case 5: /* TCH/AHS7.95 */
2861 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2862 conv, n_errors, n_bits_total);
2863
2864 tch_amr_unmerge(d, p, conv, 123, 67);
2865
2866 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2867 if (rv) {
2868 /* Error checking CRC8 for an AMR 7.95 frame */
2869 return -1;
2870 }
2871
2872 for (i = 0; i < 36; i++)
2873 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2874
2875 tch_amr_reassemble(tch_data, d, 159);
2876
2877 len = 20;
2878
2879 break;
2880 case 4: /* TCH/AHS7.4 */
2881 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2882 conv, n_errors, n_bits_total);
2883
2884 tch_amr_unmerge(d, p, conv, 120, 61);
2885
2886 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2887 if (rv) {
2888 /* Error checking CRC8 for an AMR 7.4 frame */
2889 return -1;
2890 }
2891
2892 for (i = 0; i < 28; i++)
2893 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2894
2895 tch_amr_reassemble(tch_data, d, 148);
2896
2897 len = 19;
2898
2899 break;
2900 case 3: /* TCH/AHS6.7 */
2901 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2902 conv, n_errors, n_bits_total);
2903
2904 tch_amr_unmerge(d, p, conv, 110, 55);
2905
2906 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2907 if (rv) {
2908 /* Error checking CRC8 for an AMR 6.7 frame */
2909 return -1;
2910 }
2911
2912 for (i = 0; i < 24; i++)
2913 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2914
2915 tch_amr_reassemble(tch_data, d, 134);
2916
2917 len = 17;
2918
2919 break;
2920 case 2: /* TCH/AHS5.9 */
2921 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2922 conv, n_errors, n_bits_total);
2923
2924 tch_amr_unmerge(d, p, conv, 102, 55);
2925
2926 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2927 if (rv) {
2928 /* Error checking CRC8 for an AMR 5.9 frame */
2929 return -1;
2930 }
2931
2932 for (i = 0; i < 16; i++)
2933 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2934
2935 tch_amr_reassemble(tch_data, d, 118);
2936
2937 len = 15;
2938
2939 break;
2940 case 1: /* TCH/AHS5.15 */
2941 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2942 conv, n_errors, n_bits_total);
2943
2944 tch_amr_unmerge(d, p, conv, 91, 49);
2945
2946 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2947 if (rv) {
2948 /* Error checking CRC8 for an AMR 5.15 frame */
2949 return -1;
2950 }
2951
2952 for (i = 0; i < 12; i++)
2953 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2954
2955 tch_amr_reassemble(tch_data, d, 103);
2956
2957 len = 13;
2958
2959 break;
2960 case 0: /* TCH/AHS4.75 */
2961 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2962 conv, n_errors, n_bits_total);
2963
2964 tch_amr_unmerge(d, p, conv, 83, 39);
2965
2966 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2967 if (rv) {
2968 /* Error checking CRC8 for an AMR 4.75 frame */
2969 return -1;
2970 }
2971
2972 for (i = 0; i < 12; i++)
2973 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2974
2975 tch_amr_reassemble(tch_data, d, 95);
2976
2977 len = 12;
2978
2979 break;
2980 default:
2981 /* Unknown frame type */
2982 *n_bits_total = 159;
2983 *n_errors = *n_bits_total;
2984 return -1;
2985 }
2986
Philipp Maier898c9c62020-02-06 14:25:01 +01002987out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002988 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002989 if (id != -1) {
2990 if (codec_mode_req)
2991 *cmr = id;
2992 else
2993 *ft = id;
2994 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002995
2996 return len;
2997}
2998
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002999/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003000 * \param[out] bursts caller-allocated output buffer for bursts bits
3001 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003002 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02003003 * \param[in] codec_mode_req Use CMR (1) or FT (0)
3004 * \param[in] codec Array of codecs (active codec set)
3005 * \param[in] codecs Number of entries in \a codec
3006 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
3007 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
3008 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003009int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07003010 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003011 uint8_t cmr)
3012{
3013 ubit_t iB[912], cB[456], h;
3014 ubit_t d[244], p[6], conv[135];
3015 int i;
3016 uint8_t id;
3017
3018 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
3019 _xcch_encode_cB(cB, tch_data);
3020
3021 h = 1;
3022
3023 gsm0503_tch_fr_interleave(cB, iB);
3024
3025 for (i = 0; i < 6; i++)
3026 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
3027 &h, i >> 2);
3028 for (i = 2; i < 4; i++)
3029 gsm0503_tch_burst_map(&iB[i * 114 + 456],
3030 &bursts[i * 116], &h, 1);
3031
3032 return 0;
3033 }
3034
3035 h = 0;
3036
Vadim Yanitskiya9952692023-11-10 03:31:55 +07003037 id = codec_mode_req ? cmr : ft;
3038 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit)))
3039 return -1;
3040 if (OSMO_UNLIKELY(ft >= codecs))
3041 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003042
3043 switch (codec[ft]) {
3044 case 5: /* TCH/AHS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003045 if (!len) {
3046 /* No data, induce BFI in the receiver by inverted CRC bits.
3047 * The data bit are all 0, so the correct parity bits would be 111111. */
3048 memset(d, 0, 159);
3049 memset(p, 0, 6);
3050 } else {
3051 if (len != 20)
3052 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003053
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003054 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003055
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003056 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
3057 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003058
3059 tch_amr_merge(conv, d, p, 123, 67);
3060
3061 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
3062
3063 memcpy(cB + 192, d + 123, 36);
3064
3065 break;
3066 case 4: /* TCH/AHS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003067 if (!len) {
3068 /* See comment above. */
3069 memset(d, 0, 148);
3070 memset(p, 0, 6);
3071 } else {
3072 if (len != 19)
3073 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003074
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003075 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003076
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003077 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
3078 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003079
3080 tch_amr_merge(conv, d, p, 120, 61);
3081
3082 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
3083
3084 memcpy(cB + 200, d + 120, 28);
3085
3086 break;
3087 case 3: /* TCH/AHS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003088 if (!len) {
3089 /* See comment above. */
3090 memset(d, 0, 134);
3091 memset(p, 0, 6);
3092 } else {
3093 if (len != 17)
3094 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003095
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003096 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003097
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003098 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3099 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003100
3101 tch_amr_merge(conv, d, p, 110, 55);
3102
3103 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
3104
3105 memcpy(cB + 204, d + 110, 24);
3106
3107 break;
3108 case 2: /* TCH/AHS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003109 if (!len) {
3110 /* See comment above. */
3111 memset(d, 0, 118);
3112 memset(p, 0, 6);
3113 } else {
3114 if (len != 15)
3115 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003116
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003117 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003118
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003119 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3120 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003121
3122 tch_amr_merge(conv, d, p, 102, 55);
3123
3124 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
3125
3126 memcpy(cB + 212, d + 102, 16);
3127
3128 break;
3129 case 1: /* TCH/AHS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003130 if (!len) {
3131 /* See comment above. */
3132 memset(d, 0, 103);
3133 memset(p, 0, 6);
3134 } else {
3135 if (len != 13)
3136 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003137
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003138 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003139
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003140 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
3141 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003142
3143 tch_amr_merge(conv, d, p, 91, 49);
3144
3145 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
3146
3147 memcpy(cB + 216, d + 91, 12);
3148
3149 break;
3150 case 0: /* TCH/AHS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003151 if (!len) {
3152 /* See comment above. */
3153 memset(d, 0, 95);
3154 memset(p, 0, 6);
3155 } else {
3156 if (len != 12)
3157 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003158
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003159 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003160
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003161 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
3162 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003163
3164 tch_amr_merge(conv, d, p, 83, 39);
3165
3166 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
3167
3168 memcpy(cB + 216, d + 83, 12);
3169
3170 break;
3171 default:
3172 /* FIXME: FT %ft is not supported */
3173 return -1;
3174 }
3175
Philipp Maiercfea39b2021-08-31 16:02:31 +02003176 memcpy(cB, gsm0503_ahs_ic_ubit[id], 4);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003177
3178 gsm0503_tch_hr_interleave(cB, iB);
3179
3180 for (i = 0; i < 4; i++)
3181 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
3182
3183 return 0;
3184
3185invalid_length:
3186 /* FIXME: payload length %len does not comply with codec type %ft */
3187 return -1;
3188}
3189
3190/*
3191 * GSM RACH transcoding
3192 */
3193
3194/*
3195 * GSM RACH apply BSIC to parity
3196 *
3197 * p(j) = p(j) xor b(j) j = 0, ..., 5
3198 * b(0) = MSB of PLMN colour code
3199 * b(5) = LSB of BS colour code
3200 */
Max32e56412017-10-16 14:58:00 +02003201static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003202{
3203 int i;
3204
3205 /* Apply it */
3206 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02003207 d[start + i] ^= ((bsic >> (5 - i)) & 1);
3208}
3209
Harald Welte6950b192018-02-26 11:48:00 +01003210static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
3211 int *n_errors, int *n_bits_total)
Max32e56412017-10-16 14:58:00 +02003212{
3213 ubit_t conv[17];
3214 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
3215 int rv;
3216
Harald Welte6950b192018-02-26 11:48:00 +01003217 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
3218 n_errors, n_bits_total);
Max32e56412017-10-16 14:58:00 +02003219
3220 rach_apply_bsic(conv, bsic, nbits);
3221
3222 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3223 if (rv)
3224 return -1;
3225
3226 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
3227
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003228 return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0];
Max32e56412017-10-16 14:58:00 +02003229}
3230
3231/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3232 * \param[out] ra output buffer for RACH data
3233 * \param[in] burst Input burst data
3234 * \param[in] bsic BSIC used in this cell
3235 * \returns 0 on success; negative on error (e.g. CRC error) */
3236int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
3237{
Harald Welte6950b192018-02-26 11:48:00 +01003238 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
Max32e56412017-10-16 14:58:00 +02003239
3240 if (r < 0)
3241 return r;
3242
3243 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003244
3245 return 0;
3246}
3247
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003248/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003249 * \param[out] ra output buffer for RACH data
3250 * \param[in] burst Input burst data
3251 * \param[in] bsic BSIC used in this cell
3252 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003253int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003254{
Harald Welte6950b192018-02-26 11:48:00 +01003255 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
3256 if (r < 0)
3257 return r;
3258
3259 *ra = r;
3260 return 0;
3261}
3262
3263/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3264 * \param[out] ra output buffer for RACH data
3265 * \param[in] burst Input burst data
3266 * \param[in] bsic BSIC used in this cell
3267 * \param[out] n_errors Number of detected bit errors
3268 * \param[out] n_bits_total Total number of bits
3269 * \returns 0 on success; negative on error (e.g. CRC error) */
3270int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
3271 int *n_errors, int *n_bits_total)
3272{
3273 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
3274 if (r < 0)
3275 return r;
3276
3277 *ra = r;
3278 return 0;
3279}
3280
3281/*! Decode the (8-bit) RACH according to TS 05.03
3282 * \param[out] ra output buffer for RACH data
3283 * \param[in] burst Input burst data
3284 * \param[in] bsic BSIC used in this cell
3285 * \param[out] n_errors Number of detected bit errors
3286 * \param[out] n_bits_total Total number of bits
3287 * \returns 0 on success; negative on error (e.g. CRC error) */
3288int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
3289 int *n_errors, int *n_bits_total)
3290{
3291 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003292
Max32e56412017-10-16 14:58:00 +02003293 if (r < 0)
3294 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003295
Max32e56412017-10-16 14:58:00 +02003296 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003297
3298 return 0;
3299}
3300
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003301/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003302 * \param[out] burst Caller-allocated output burst buffer
3303 * \param[in] ra Input RACH data
3304 * \param[in] bsic BSIC used in this cell
3305 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003306int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003307{
Max32e56412017-10-16 14:58:00 +02003308 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
3309}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003310
Max32e56412017-10-16 14:58:00 +02003311/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
3312 * \param[out] burst Caller-allocated output burst buffer
3313 * \param[in] ra11 Input RACH data
3314 * \param[in] bsic BSIC used in this cell
3315 * \param[in] is_11bit whether given RA is 11 bit or not
3316 * \returns 0 on success; negative on error */
3317int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
3318{
3319 ubit_t conv[17];
3320 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003321
Max32e56412017-10-16 14:58:00 +02003322 if (is_11bit) {
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003323 ra[0] = (uint8_t) (ra11 >> 3);
3324 ra[1] = (uint8_t) (ra11 & 0x07);
Max32e56412017-10-16 14:58:00 +02003325 nbits = 11;
3326 } else
3327 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003328
Max32e56412017-10-16 14:58:00 +02003329 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003330
Max32e56412017-10-16 14:58:00 +02003331 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3332
3333 rach_apply_bsic(conv, bsic, nbits);
3334
3335 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003336
3337 return 0;
3338}
3339
3340/*
3341 * GSM SCH transcoding
3342 */
Harald Weltec6636782017-06-12 14:59:37 +02003343
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003344/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003345 * \param[out] sb_info output buffer for SCH data
3346 * \param[in] burst Input burst data
3347 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003348int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003349{
3350 ubit_t conv[35];
3351 int rv;
3352
3353 osmo_conv_decode(&gsm0503_sch, burst, conv);
3354
3355 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3356 if (rv)
3357 return -1;
3358
3359 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
3360
3361 return 0;
3362}
3363
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003364/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003365 * \param[out] burst Caller-allocated output burst buffer
3366 * \param[in] sb_info Input SCH data
3367 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003368int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003369{
3370 ubit_t conv[35];
3371
3372 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
3373
3374 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3375
3376 osmo_conv_encode(&gsm0503_sch, conv, burst);
3377
3378 return 0;
3379}
Harald Weltec6636782017-06-12 14:59:37 +02003380
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003381/*
3382 * GSM CSD transcoding
3383 */
3384
3385static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB)
3386{
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003387 unsigned int i;
3388
3389 /* hu(B): copy *even* numbered bits if not stolen by FACCH */
3390 if (burst[58] == 0) {
3391 for (i = 0; i < 57; i += 2)
3392 burst[i] |= iB[i];
3393 for (i = 58; i < 114; i += 2)
3394 burst[i + 2] |= iB[i];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003395 }
3396
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003397 /* hl(B): copy *odd* numbered bits if not stolen by FACCH */
3398 if (burst[57] == 0) {
3399 for (i = 1; i < 57; i += 2)
3400 burst[i] |= iB[i];
3401 for (i = 57; i < 114; i += 2)
3402 burst[i + 2] |= iB[i];
3403 }
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003404}
3405
3406/*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3.
3407 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3408 * 22 * 2 * 58 == 2552 bits total.
3409 * \param[in] data Data to be encoded (240 unpacked bits).
3410 * \returns 0 in case of success; negative on error. */
3411int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data)
3412{
3413 ubit_t iB[22 * 114], cB[4 * 114];
3414 ubit_t conv[4 * 60 + 4];
3415
3416 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3417 memcpy(&conv[0], &data[0], 4 * 60);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003418 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003419
3420 /* 3.3.3 Convolutional encoder */
3421 osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]);
3422
3423 /* 3.3.4 Interleaving */
3424 memset(&iB[0], 0, sizeof(iB));
3425 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3426
3427 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3428 for (unsigned int i = 0; i < 22; i++)
3429 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3430
3431 return 0;
3432}
3433
3434/*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3.
3435 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3436 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3437 * 22 * 2 * 58 == 2552 bits total.
3438 * \param[out] n_errors Number of detected bit errors.
3439 * \param[out] n_bits_total Total number of bits.
3440 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3441int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts,
3442 int *n_errors, int *n_bits_total)
3443{
3444 sbit_t iB[22 * 114], cB[4 * 114];
3445 ubit_t conv[4 * 60 + 4];
3446
3447 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3448 for (unsigned int i = 0; i < 22; i++) {
3449 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3450 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3451 }
3452
3453 /* 3.3.4 Interleaving */
3454 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3455
3456 /* 3.3.3 Convolutional encoder */
3457 osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total);
3458
3459 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3460 memcpy(&data[0], &conv[0], 4 * 60);
3461
3462 return 4 * 60;
3463}
3464
3465/*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4.
3466 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3467 * 22 * 2 * 58 == 2552 bits total.
3468 * \param[in] data Data to be encoded (120 unpacked bits).
3469 * \returns 0 in case of success; negative on error */
3470int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data)
3471{
3472 ubit_t iB[22 * 114], cB[4 * 114];
3473 ubit_t conv[2 * 60 + 32];
3474
3475 /* 3.4.2 Block code:
3476 *
3477 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3478 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3479 *
3480 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3481 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3482 *
3483 * Two such blocks forming a block of 152 bits: u1 + u2. */
3484 for (unsigned int k = 0; k < 2 * 4; k++) {
3485 memcpy(&conv[19 * k], &data[15 * k], 15);
3486 memset(&conv[19 * k + 15], 0, 4);
3487 }
3488
3489 /* 3.4.3 Convolutional encoder */
3490 osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]);
3491
3492 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3493 memset(&iB[0], 0, sizeof(iB));
3494 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3495
3496 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3497 for (unsigned int i = 0; i < 22; i++)
3498 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3499
3500 return 0;
3501}
3502
3503/*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4.
3504 * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits).
3505 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3506 * 22 * 2 * 58 == 2552 bits total.
3507 * \param[out] n_errors Number of detected bit errors.
3508 * \param[out] n_bits_total Total number of bits.
3509 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3510int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts,
3511 int *n_errors, int *n_bits_total)
3512{
3513 sbit_t iB[22 * 114], cB[4 * 114];
3514 ubit_t conv[2 * 60 + 32];
3515
3516 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3517 for (unsigned int i = 0; i < 22; i++) {
3518 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3519 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3520 }
3521
3522 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3523 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3524
3525 /* 3.4.3 Convolutional encoder */
3526 osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total);
3527
3528 /* 3.4.2 Block code:
3529 *
3530 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3531 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3532 *
3533 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3534 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3535 *
3536 * Two such blocks forming a block of 152 bits: u1 + u2. */
3537 for (unsigned int k = 0; k < 2 * 4; k++)
3538 memcpy(&data[15 * k], &conv[19 * k], 15);
3539
3540 return 2 * 60;
3541}
3542
3543/*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5.
3544 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3545 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3546 * 22 * 2 * 58 == 2552 bits total.
3547 * \param[in] data Data to be encoded (240 unpacked bits).
3548 * \returns 0 in case of success; negative on error */
3549int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data)
3550{
3551 return gsm0503_tch_fr96_encode(bursts, data);
3552}
3553
3554/*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5.
3555 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3556 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3557 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3558 * 22 * 2 * 58 == 2552 bits total.
3559 * \param[out] n_errors Number of detected bit errors.
3560 * \param[out] n_bits_total Total number of bits.
3561 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3562int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts,
3563 int *n_errors, int *n_bits_total)
3564{
3565 return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total);
3566}
3567
Vadim Yanitskiyfadda012023-07-08 06:30:55 +07003568/*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6.
3569 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3570 * 8 * 2 * 58 == 928 bits total.
3571 * \param[in] data Data to be encoded (72 unpacked bits).
3572 * \returns 0 in case of success; negative on error */
3573int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data)
3574{
3575 ubit_t iB[8 * 114], cB[4 * 114];
3576 const ubit_t h = 0;
3577
3578 /* 3.6.{1-3} Block code and Convolutional encoder */
3579 osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]);
3580
3581 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3582 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3583
3584 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3585 for (unsigned int i = 0; i < 8; i++)
3586 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3587
3588 return 0;
3589}
3590
3591/*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6.
3592 * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits).
3593 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3594 * 8 * 2 * 58 == 928 bits total.
3595 * \param[out] n_errors Number of detected bit errors.
3596 * \param[out] n_bits_total Total number of bits.
3597 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3598int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts,
3599 int *n_errors, int *n_bits_total)
3600{
3601 sbit_t iB[8 * 114], cB[4 * 114];
3602
3603 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3604 for (unsigned int i = 0; i < 8; i++)
3605 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3606
3607 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3608 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3609
3610 /* 3.6.{1-3} Block code and Convolutional encoder */
3611 osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total);
3612
3613 return 72;
3614}
3615
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003616/*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7.
3617 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3618 * 22 * 2 * 58 == 2552 bits total.
3619 * \param[in] data Data to be encoded (144 unpacked bits).
3620 * \returns 0 in case of success; negative on error */
3621int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data)
3622{
3623 ubit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003624
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003625 /* 3.7.{1-3} Block code and Convolutional encoder */
3626 osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]);
3627 osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]);
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003628
3629 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3630 memset(&iB[0], 0, sizeof(iB));
3631 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3632
3633 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3634 for (unsigned int i = 0; i < 22; i++)
3635 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3636
3637 return 0;
3638}
3639
3640/*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7.
3641 * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits).
3642 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3643 * 22 * 2 * 58 == 2552 bits total.
3644 * \param[out] n_errors Number of detected bit errors.
3645 * \param[out] n_bits_total Total number of bits.
3646 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3647int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts,
3648 int *n_errors, int *n_bits_total)
3649{
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003650 int n_errors_l[2], n_bits_total_l[2];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003651 sbit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003652
3653 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3654 for (unsigned int i = 0; i < 22; i++) {
3655 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3656 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3657 }
3658
3659 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3660 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3661
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003662 /* 3.7.{1-3} Block code and Convolutional encoder */
3663 osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]);
3664 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 +07003665
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003666 if (n_errors)
3667 *n_errors = n_errors_l[0] + n_errors_l[1];
3668
3669 if (n_bits_total)
3670 *n_bits_total = n_bits_total_l[0] + n_bits_total_l[1];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003671
3672 return 2 * 72;
3673}
3674
3675/*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8.
3676 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3677 * 22 * 2 * 58 == 2552 bits total.
3678 * \param[in] data Data to be encoded (290 unpacked bits).
3679 * \returns 0 in case of success; negative on error */
3680int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data)
3681{
3682 ubit_t iB[22 * 114], cB[4 * 114];
3683 ubit_t conv[290 + 4];
3684
3685 /* 3.8.2 Block code: b(290) + pad(4) */
3686 memcpy(&conv[0], &data[0], 290);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003687 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003688
3689 /* 3.8.3 Convolutional encoder */
3690 osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]);
3691
3692 /* 3.8.4 Interleaving */
3693 memset(&iB[0], 0, sizeof(iB));
3694 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3695
3696 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3697 for (unsigned int i = 0; i < 22; i++)
3698 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3699
3700 return 0;
3701}
3702
3703/*! Perform channel decoding of a TCH/14.4 channel as per section 3.8.
3704 * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits).
3705 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3706 * 22 * 2 * 58 == 2552 bits total.
3707 * \param[out] n_errors Number of detected bit errors.
3708 * \param[out] n_bits_total Total number of bits.
3709 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3710int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts,
3711 int *n_errors, int *n_bits_total)
3712{
3713 sbit_t iB[22 * 114], cB[4 * 114];
3714 ubit_t conv[294];
3715
3716 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3717 for (unsigned int i = 0; i < 22; i++) {
3718 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3719 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3720 }
3721
3722 /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3723 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3724
3725 /* 3.8.3 Convolutional encoder */
3726 osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total);
3727
3728 /* 3.8.2 Block code: b(290) + pad(4) */
3729 memcpy(&data[0], &conv[0], 290);
3730
3731 return 290;
3732}
3733
Vadim Yanitskiyb3340222023-05-26 01:07:18 +07003734/*
3735 * FACCH/[FH] transcoding
3736 */
3737
3738/*! Perform channel encoding of a FACCH/F data as per section 4.2.
3739 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3740 * 8 * 2 * 58 == 928 bits total.
3741 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3742 * \returns 0 in case of success; negative on error */
3743int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data)
3744{
3745 ubit_t iB[8 * 114], cB[4 * 114];
3746 const ubit_t h = 1;
3747
3748 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3749 _xcch_encode_cB(&cB[0], &data[0]);
3750
3751 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3752 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3753
3754 /* 4.2.5 Mapping on a Burst:
3755 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3756 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3757 for (unsigned int i = 0; i < 8; i++)
3758 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3759
3760 return 0;
3761}
3762
3763/*! Perform channel decoding of a FACCH/F data as per section 4.2.
3764 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3765 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3766 * 8 * 2 * 58 == 928 bits total.
3767 * \param[out] n_errors Number of detected bit errors.
3768 * \param[out] n_bits_total Total number of bits.
3769 * \returns Number of bytes used in the output buffer; negative on error. */
3770int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts,
3771 int *n_errors, int *n_bits_total)
3772{
3773 sbit_t iB[8 * 114], cB[4 * 114];
3774 int steal = 0;
3775
3776 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3777 for (unsigned int i = 0; i < 4; i++)
3778 steal -= bursts[i * 116 + 58]; /* hu(B) */
3779 for (unsigned int i = 4; i < 8; i++)
3780 steal -= bursts[i * 116 + 57]; /* hl(B) */
3781 if (steal <= 0)
3782 return -1;
3783
3784 /* 4.2.5 Mapping on a Burst:
3785 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3786 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3787 for (unsigned int i = 0; i < 8; i++)
3788 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3789
3790 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3791 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3792
3793 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3794 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3795 return -1;
3796
3797 return GSM_MACBLOCK_LEN;
3798}
3799
3800/*! Perform channel encoding of a FACCH/H data as per section 4.3.
3801 * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts,
3802 * 6 * 2 * 58 == 696 bits total.
3803 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3804 * \returns 0 in case of success; negative on error */
3805int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data)
3806{
3807 ubit_t iB[8 * 114], cB[4 * 114];
3808 const ubit_t h = 1;
3809
3810 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3811 _xcch_encode_cB(&cB[0], &data[0]);
3812
3813 /* 4.3.4 Interleaving */
3814 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3815
3816 /* 4.3.5 Mapping on a Burst:
3817 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3818 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3819 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3820 for (unsigned int i = 0; i < 6; i++)
3821 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3822 for (unsigned int i = 2; i < 4; i++)
3823 gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1);
3824
3825 return 0;
3826}
3827
3828/*! Perform channel decoding of a FACCH/H data as per section 4.3.
3829 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3830 * \param[in] bursts Buffer containing the symbols of 6 bursts,
3831 * 6 * 2 * 58 == 696 bits total.
3832 * \param[out] n_errors Number of detected bit errors.
3833 * \param[out] n_bits_total Total number of bits.
3834 * \returns Number of bytes used in the output buffer; negative on error. */
3835int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts,
3836 int *n_errors, int *n_bits_total)
3837{
3838 sbit_t iB[8 * 114], cB[4 * 114];
3839 int steal = 0;
3840
3841 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3842 for (unsigned int i = 0; i < 4; i++)
3843 steal -= bursts[i * 116 + 58]; /* hu(B) */
3844 for (unsigned int i = 2; i < 6; i++)
3845 steal -= bursts[i * 116 + 57]; /* hl(B) */
3846 if (steal <= 0)
3847 return -1;
3848
3849 /* 4.3.5 Mapping on a Burst:
3850 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3851 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3852 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3853 for (unsigned int i = 0; i < 6; i++)
3854 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3855 for (unsigned int i = 2; i < 4; i++)
3856 gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1);
3857
3858 /* 4.3.4 Interleaving */
3859 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3860
3861 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3862 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3863 return -1;
3864
3865 return GSM_MACBLOCK_LEN;
3866}
3867
Harald Weltec6636782017-06-12 14:59:37 +02003868/*! @} */