blob: 1f409d68ef6375a0369b217cc5370fa06be85935 [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{
539 int res, i, coded_len;
540 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;
553 for (i = 0; i < coded_len; i++) {
Alexander Chemeris147051f2018-07-14 21:02:29 +0200554 if (((!data_punc) || (data_punc && !data_punc[i])) &&
555 !((recoded[i] && input[i] < 0) ||
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700556 (!recoded[i] && input[i] > 0)) )
557 *n_errors += 1;
558 }
559 }
560
561 if (n_bits_total)
562 *n_bits_total = coded_len;
563
564 return res;
565}
566
Alexander Chemeris147051f2018-07-14 21:02:29 +0200567/*! Convolutional Decode + compute BER for non-punctured codes
568 * \param[in] code Description of Convolutional Code
569 * \param[in] input Input soft-bits (-127...127)
570 * \param[out] output bits
571 * \param[out] n_errors Number of bit-errors
572 * \param[out] n_bits_total Number of bits
573 */
574static int osmo_conv_decode_ber(const struct osmo_conv_code *code,
575 const sbit_t *input, ubit_t *output,
576 int *n_errors, int *n_bits_total)
577{
578 return osmo_conv_decode_ber_punctured(code, input, output,
579 n_errors, n_bits_total, NULL);
580}
581
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200582/*! convenience wrapper for decoding coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200583 * \param[out] l2_data caller-allocated buffer for L2 Frame
584 * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3
585 * \param[out] n_errors Number of detected errors
586 * \param[out] n_bits_total Number of total coded bits
587 * \returns 0 on success; -1 on CRC error */
Harald Welteb9946d32017-06-12 09:40:16 +0200588static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700589 int *n_errors, int *n_bits_total)
590{
591 ubit_t conv[224];
592 int rv;
593
594 osmo_conv_decode_ber(&gsm0503_xcch, cB,
595 conv, n_errors, n_bits_total);
596
597 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
598 conv, 184, conv + 184);
599 if (rv)
600 return -1;
601
602 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
603
604 return 0;
605}
606
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200607/*! convenience wrapper for encoding to coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200608 * \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 +0700609 * \param[in] l2_data to-be-encoded L2 Frame
Harald Weltec6636782017-06-12 14:59:37 +0200610 * \returns 0 */
Harald Welteb9946d32017-06-12 09:40:16 +0200611static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700612{
613 ubit_t conv[224];
614
615 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
616
617 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
618
619 osmo_conv_encode(&gsm0503_xcch, conv, cB);
620
621 return 0;
622}
623
624/*
625 * GSM xCCH block transcoding
626 */
Harald Weltec6636782017-06-12 14:59:37 +0200627
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200628/*! Decoding of xCCH data from bursts to L2 frame
Harald Weltec6636782017-06-12 14:59:37 +0200629 * \param[out] l2_data caller-allocated output data buffer
630 * \param[in] bursts four GSM bursts in soft-bits
631 * \param[out] n_errors Number of detected errors
632 * \param[out] n_bits_total Number of total coded bits
633 */
Harald Welteb9946d32017-06-12 09:40:16 +0200634int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700635 int *n_errors, int *n_bits_total)
636{
637 sbit_t iB[456], cB[456];
638 int i;
639
640 for (i = 0; i < 4; i++)
641 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL);
642
643 gsm0503_xcch_deinterleave(cB, iB);
644
645 return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total);
646}
647
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200648/*! Encoding of xCCH data from L2 frame to bursts
Harald Weltec6636782017-06-12 14:59:37 +0200649 * \param[out] bursts caller-allocated burst data (unpacked bits)
650 * \param[in] l2_data L2 input data (MAC block)
651 * \returns 0
652 */
Harald Welteb9946d32017-06-12 09:40:16 +0200653int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700654{
655 ubit_t iB[456], cB[456], hl = 1, hn = 1;
656 int i;
657
658 _xcch_encode_cB(cB, l2_data);
659
660 gsm0503_xcch_interleave(cB, iB);
661
662 for (i = 0; i < 4; i++)
663 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn);
664
665 return 0;
666}
667
668/*
669 * EGPRS PDTCH UL block decoding
670 */
671
672/*
673 * Type 3 - MCS-1,2,3,4
674 * Unmapping and deinterleaving
675 */
676static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
677{
678 int i;
679 sbit_t iB[456], q[8];
680
681 for (i = 0; i < 4; i++) {
682 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
683 q + i * 2, q + i * 2 + 1);
684 }
685
686 gsm0503_mcs1_ul_deinterleave(hc, dc, iB);
687
688 return 0;
689}
690
691/*
692 * Type 2 - MCS-5,6
693 * Unmapping and deinterleaving
694 */
695static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
696{
697 int i;
698 sbit_t burst[348];
699 sbit_t hi[EGPRS_HDR_HC_MAX];
700 sbit_t di[EGPRS_DATA_DC_MAX];
701
702 for (i = 0; i < 4; i++) {
703 memcpy(burst, &bursts[i * 348], 348);
704
705 gsm0503_mcs5_burst_swap(burst);
706 gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i);
707 }
708
709 gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di);
710
711 return 0;
712}
713
714/*
715 * Type 1 - MCS-7,8,9
716 * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique
717 */
718static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc,
719 sbit_t *c1, sbit_t *c2, int msc)
720{
721 int i;
722 sbit_t burst[348];
723 sbit_t hi[EGPRS_HDR_HC_MAX];
724 sbit_t di[EGPRS_DATA_C1 * 2];
725
726 for (i = 0; i < 4; i++) {
727 memcpy(burst, &bursts[i * 348], 348);
728
729 gsm0503_mcs5_burst_swap(burst);
730 gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i);
731 }
732
733 if (msc == EGPRS_MCS7)
734 gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di);
735 else
736 gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di);
737
738 return 0;
739}
740
741/*
742 * Decode EGPRS UL header section
743 *
744 * 1. Depuncture
745 * 2. Convolutional decoding
746 * 3. CRC check
747 */
748static int _egprs_decode_hdr(const sbit_t *hc, int mcs,
749 union gprs_rlc_ul_hdr_egprs *hdr)
750{
751 sbit_t C[EGPRS_HDR_C_MAX];
752 ubit_t upp[EGPRS_HDR_UPP_MAX];
753 int i, j, rc;
Harald Welte2f984ea2017-06-12 15:05:21 +0200754 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700755
756 code = &gsm0503_mcs_ul_codes[mcs];
757
758 /* Skip depuncturing on MCS-5,6 header */
759 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
760 memcpy(C, hc, code->hdr_code_len);
761 goto hdr_conv_decode;
762 }
763
764 if (!code->hdr_punc) {
765 /* Invalid MCS-X header puncture matrix */
766 return -1;
767 }
768
769 i = code->hdr_code_len - 1;
770 j = code->hdr_punc_len - 1;
771
772 for (; i >= 0; i--) {
773 if (!code->hdr_punc[i])
774 C[i] = hc[j--];
775 else
776 C[i] = 0;
777 }
778
779hdr_conv_decode:
780 osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL);
781 rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp,
782 code->hdr_len, upp + code->hdr_len);
783 if (rc)
784 return -1;
785
786 osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1);
787
788 return 0;
789}
790
791/*
792 * Blind MCS header decoding based on burst length and CRC validation.
793 * Ignore 'q' value coding identification. This approach provides
794 * the strongest chance of header recovery.
795 */
796static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr,
797 const sbit_t *bursts, uint16_t nbits)
798{
799 int rc;
800 sbit_t hc[EGPRS_HDR_HC_MAX];
801
802 if (nbits == GSM0503_GPRS_BURSTS_NBITS) {
803 /* MCS-1,2,3,4 */
804 egprs_type3_unmap(bursts, hc, NULL);
805 rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr);
806 if (!rc)
807 return EGPRS_HDR_TYPE3;
808 } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) {
809 /* MCS-5,6 */
810 egprs_type2_unmap(bursts, hc, NULL);
811 rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr);
812 if (!rc)
813 return EGPRS_HDR_TYPE2;
814
815 /* MCS-7,8,9 */
816 egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7);
817 rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr);
818 if (!rc)
819 return EGPRS_HDR_TYPE1;
820 }
821
822 return -1;
823}
824
825/*
826 * Parse EGPRS UL header for coding and puncturing scheme (CPS)
827 *
828 * Type 1 - MCS-7,8,9
829 * Type 2 - MCS-5,6
830 * Type 3 - MCS-1,2,3,4
831 */
832static int egprs_parse_ul_cps(struct egprs_cps *cps,
833 union gprs_rlc_ul_hdr_egprs *hdr, int type)
834{
835 uint8_t bits;
836
837 switch (type) {
838 case EGPRS_HDR_TYPE1:
839 bits = hdr->type1.cps;
840 break;
841 case EGPRS_HDR_TYPE2:
842 bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi;
843 break;
844 case EGPRS_HDR_TYPE3:
845 bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi;
846 break;
847 default:
848 return -1;
849 }
850
851 return egprs_get_cps(cps, type, bits);
852}
853
854/*
855 * Decode EGPRS UL data section
856 *
857 * 1. Depuncture
858 * 2. Convolutional decoding
859 * 3. CRC check
860 * 4. Block combining (MCS-7,8,9 only)
861 */
Harald Welteb9946d32017-06-12 09:40:16 +0200862static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700863 int mcs, int p, int blk, int *n_errors, int *n_bits_total)
864{
865 ubit_t u[EGPRS_DATA_U_MAX];
866 sbit_t C[EGPRS_DATA_C_MAX];
867
868 int i, j, rc, data_len;
Harald Welte2f984ea2017-06-12 15:05:21 +0200869 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700870
871 if (blk && mcs < EGPRS_MCS7) {
872 /* Invalid MCS-X block state */
873 return -1;
874 }
875
876 code = &gsm0503_mcs_ul_codes[mcs];
877 if (!code->data_punc[p]) {
878 /* Invalid MCS-X data puncture matrix */
879 return -1;
880 }
881
882 /*
883 * MCS-1,6 - single block processing
884 * MCS-7,9 - dual block processing
885 */
886 if (mcs >= EGPRS_MCS7)
887 data_len = code->data_len / 2;
888 else
889 data_len = code->data_len;
890
891 i = code->data_code_len - 1;
892 j = code->data_punc_len - 1;
893
894 for (; i >= 0; i--) {
895 if (!code->data_punc[p][i])
896 C[i] = c[j--];
897 else
898 C[i] = 0;
899 }
900
Alexander Chemeris147051f2018-07-14 21:02:29 +0200901 osmo_conv_decode_ber_punctured(code->data_conv, C, u,
902 n_errors, n_bits_total, code->data_punc[p]);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700903 rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u,
904 data_len, u + data_len);
905 if (rc)
906 return -1;
907
908 /* Offsets output pointer on the second block of Type 1 MCS */
909 osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len,
910 u, 0, data_len, 1);
911
912 /* Return the number of bytes required for the bit message */
Maxdd75bac2017-06-13 15:07:01 +0200913 return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700914}
915
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200916/*! Decode EGPRS UL message
Harald Weltec6636782017-06-12 14:59:37 +0200917 * 1. Header section decoding
918 * 2. Extract CPS settings
919 * 3. Burst unmapping and deinterleaving
920 * 4. Data section decoding
921 * \param[out] l2_data caller-allocated buffer for L2 Frame
922 * \param[in] bursts burst input data as soft unpacked bits
923 * \param[in] nbits number of bits in \a bursts
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +0700924 * \param usf_p Uplink State Flag, FIXME: not implemented
Harald Weltec6636782017-06-12 14:59:37 +0200925 * \param[out] n_errors number of detected bit-errors
Alexander Chemerised7d2dd2018-07-14 21:06:27 +0200926 * \param[out] n_bits_total total number of decoded bits
Vadim Yanitskiyccf9aad2023-12-08 01:11:40 +0700927 * \returns number of bytes decoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +0200928int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700929 uint8_t *usf_p, int *n_errors, int *n_bits_total)
930{
931 sbit_t dc[EGPRS_DATA_DC_MAX];
932 sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
933 int type, rc;
934 struct egprs_cps cps;
935 union gprs_rlc_ul_hdr_egprs *hdr;
936
Alexander Chemeris50f7d742018-07-14 21:07:27 +0200937 if (n_errors)
938 *n_errors = 0;
939 if (n_bits_total)
940 *n_bits_total = 0;
941
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700942 if ((nbits != GSM0503_GPRS_BURSTS_NBITS) &&
943 (nbits != GSM0503_EGPRS_BURSTS_NBITS)) {
944 /* Invalid EGPRS bit length */
Maxc8cf8202017-05-22 16:07:04 +0200945 return -EOVERFLOW;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700946 }
947
948 hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data;
949 type = egprs_decode_hdr(hdr, bursts, nbits);
950 if (egprs_parse_ul_cps(&cps, hdr, type) < 0)
Maxc8cf8202017-05-22 16:07:04 +0200951 return -EIO;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700952
953 switch (cps.mcs) {
Maxc8cf8202017-05-22 16:07:04 +0200954 case EGPRS_MCS0:
955 return -ENOTSUP;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700956 case EGPRS_MCS1:
957 case EGPRS_MCS2:
958 case EGPRS_MCS3:
959 case EGPRS_MCS4:
960 egprs_type3_unmap(bursts, NULL, dc);
961 break;
962 case EGPRS_MCS5:
963 case EGPRS_MCS6:
964 egprs_type2_unmap(bursts, NULL, dc);
965 break;
966 case EGPRS_MCS7:
967 case EGPRS_MCS8:
968 case EGPRS_MCS9:
969 egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs);
970 break;
971 default:
972 /* Invalid MCS-X */
Maxc8cf8202017-05-22 16:07:04 +0200973 return -EINVAL;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700974 }
975
976 /* Decode MCS-X block, where X = cps.mcs */
977 if (cps.mcs < EGPRS_MCS7) {
978 rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0],
979 0, n_errors, n_bits_total);
980 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200981 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700982 } else {
Alexander Chemeris94443262018-07-14 21:09:54 +0200983 /* Bit counters for the second block */
984 int n_errors2, n_bits_total2;
985
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700986 /* MCS-7,8,9 block 1 */
987 rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0],
988 0, n_errors, n_bits_total);
989 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200990 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700991
992 /* MCS-7,8,9 block 2 */
993 rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1],
Alexander Chemeris94443262018-07-14 21:09:54 +0200994 1, &n_errors2, &n_bits_total2);
995 if (n_errors)
996 *n_errors += n_errors2;
997 if (n_bits_total)
998 *n_bits_total += n_bits_total2;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700999 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +02001000 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001001 }
1002
1003 return rc;
1004}
1005
1006/*
1007 * GSM PDTCH block transcoding
1008 */
1009
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001010/*! Decode GPRS PDTCH
Harald Weltec6636782017-06-12 14:59:37 +02001011 * \param[out] l2_data caller-allocated buffer for L2 Frame
1012 * \param[in] bursts burst input data as soft unpacked bits
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001013 * \param[out] usf_p Uplink State Flag, only relevant for DL blocks
Harald Weltec6636782017-06-12 14:59:37 +02001014 * \param[out] n_errors number of detected bit-errors
1015 * \param[out] n_bits_total total number of dcoded bits
Vadim Yanitskiyccf9aad2023-12-08 01:11:40 +07001016 * \returns number of bytes decoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001017int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001018 int *n_errors, int *n_bits_total)
1019{
1020 sbit_t iB[456], cB[676], hl_hn[8];
1021 ubit_t conv[456];
1022 int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */
1023
1024 for (i = 0; i < 4; i++)
1025 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
1026 hl_hn + i * 2, hl_hn + i * 2 + 1);
1027
1028 for (i = 0; i < 4; i++) {
1029 for (j = 0, k = 0; j < 8; j++)
1030 k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j]));
1031
1032 if (i == 0 || k < best) {
1033 best = k;
1034 cs = i + 1;
1035 }
1036 }
1037
1038 gsm0503_xcch_deinterleave(cB, iB);
1039
1040 switch (cs) {
1041 case 1:
1042 osmo_conv_decode_ber(&gsm0503_xcch, cB,
1043 conv, n_errors, n_bits_total);
1044
Vadim Yanitskiyc58e4852023-12-08 03:35:50 +07001045 /* the three USF bits d(0),d(1),d(2) are *not* precoded */
1046 if (usf_p)
1047 *usf_p = (conv[0] << 2) | (conv[1] << 1) | (conv[2] << 0);
1048
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001049 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
1050 conv, 184, conv + 184);
1051 if (rv)
1052 return -1;
1053
1054 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
1055
1056 return 23;
1057 case 2:
1058 for (i = 587, j = 455; i >= 0; i--) {
1059 if (!gsm0503_puncture_cs2[i])
1060 cB[i] = cB[j--];
1061 else
1062 cB[i] = 0;
1063 }
1064
1065 osmo_conv_decode_ber(&gsm0503_cs2_np, cB,
1066 conv, n_errors, n_bits_total);
1067
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001068 /* 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 +07001069 for (i = 0; i < 8; i++) {
1070 for (j = 0, k = 0; j < 6; j++)
1071 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1072
1073 if (i == 0 || k < best) {
1074 best = k;
1075 usf = i;
1076 }
1077 }
1078
1079 conv[3] = usf & 1;
1080 conv[4] = (usf >> 1) & 1;
1081 conv[5] = (usf >> 2) & 1;
1082 if (usf_p)
1083 *usf_p = usf;
1084
1085 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1086 conv + 3, 271, conv + 3 + 271);
1087 if (rv)
1088 return -1;
1089
1090 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1091
1092 return 34;
1093 case 3:
1094 for (i = 675, j = 455; i >= 0; i--) {
1095 if (!gsm0503_puncture_cs3[i])
1096 cB[i] = cB[j--];
1097 else
1098 cB[i] = 0;
1099 }
1100
1101 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
1102 conv, n_errors, n_bits_total);
1103
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001104 /* 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 +07001105 for (i = 0; i < 8; i++) {
1106 for (j = 0, k = 0; j < 6; j++)
1107 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1108
1109 if (i == 0 || k < best) {
1110 best = k;
1111 usf = i;
1112 }
1113 }
1114
1115 conv[3] = usf & 1;
1116 conv[4] = (usf >> 1) & 1;
1117 conv[5] = (usf >> 2) & 1;
1118 if (usf_p)
1119 *usf_p = usf;
1120
1121 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1122 conv + 3, 315, conv + 3 + 315);
1123 if (rv)
1124 return -1;
1125
1126 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
1127
1128 return 40;
1129 case 4:
1130 for (i = 12; i < 456; i++)
1131 conv[i] = (cB[i] < 0) ? 1 : 0;
1132
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001133 /* 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 +07001134 for (i = 0; i < 8; i++) {
1135 for (j = 0, k = 0; j < 12; j++)
1136 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1137
1138 if (i == 0 || k < best) {
1139 best = k;
1140 usf = i;
1141 }
1142 }
1143
1144 conv[9] = usf & 1;
1145 conv[10] = (usf >> 1) & 1;
1146 conv[11] = (usf >> 2) & 1;
1147 if (usf_p)
1148 *usf_p = usf;
1149
1150 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1151 conv + 9, 431, conv + 9 + 431);
1152 if (rv) {
1153 *n_bits_total = 456 - 12;
1154 *n_errors = *n_bits_total;
1155 return -1;
1156 }
1157
1158 *n_bits_total = 456 - 12;
1159 *n_errors = 0;
1160
1161 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1162
1163 return 54;
1164 default:
1165 *n_bits_total = 0;
1166 *n_errors = 0;
1167 break;
1168 }
1169
1170 return -1;
1171}
1172
1173/*
Pau Espin Pedrolf62f0732020-04-07 13:12:11 +02001174 * EGPRS PDTCH DL block encoding
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001175 */
Harald Welteb9946d32017-06-12 09:40:16 +02001176static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001177{
1178 int i;
1179 ubit_t iB[456];
1180 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1181
Pau Espin Pedrol63ebc362020-04-07 13:15:36 +02001182 gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001183
1184 for (i = 0; i < 4; i++) {
1185 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1186 hl_hn + i * 2, hl_hn + i * 2 + 1);
1187 }
1188
1189 return 0;
1190}
1191
Harald Welteb9946d32017-06-12 09:40:16 +02001192static int egprs_type2_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 const ubit_t *up;
1196 ubit_t hi[EGPRS_HDR_HC_MAX];
1197 ubit_t di[EGPRS_DATA_DC_MAX];
1198
1199 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1200 up = gsm0503_mcs5_usf_precode_table[usf];
1201
1202 for (i = 0; i < 4; i++) {
1203 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1204 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1205 }
1206
1207 return 0;
1208}
1209
Harald Welteb9946d32017-06-12 09:40:16 +02001210static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1211 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001212{
1213 int i;
1214 const ubit_t *up;
1215 ubit_t hi[EGPRS_HDR_HC_MAX];
1216 ubit_t di[EGPRS_DATA_C1 * 2];
1217
1218 if (mcs == EGPRS_MCS7)
1219 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1220 else
1221 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1222
1223 up = gsm0503_mcs5_usf_precode_table[usf];
1224
1225 for (i = 0; i < 4; i++) {
1226 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1227 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1228 }
1229
1230 return 0;
1231}
1232
Harald Welteb9946d32017-06-12 09:40:16 +02001233static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001234{
1235 int i, j;
1236 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001237 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001238
1239 code = &gsm0503_mcs_dl_codes[mcs];
1240
1241 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1242 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1243 code->hdr_len, upp + code->hdr_len);
1244
1245 osmo_conv_encode(code->hdr_conv, upp, C);
1246
1247 /* MCS-5,6 header direct puncture instead of table */
1248 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1249 memcpy(hc, C, code->hdr_code_len);
1250 hc[99] = hc[98];
1251 return 0;
1252 }
1253
1254 if (!code->hdr_punc) {
1255 /* Invalid MCS-X header puncture matrix */
1256 return -1;
1257 }
1258
1259 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1260 if (!code->hdr_punc[i])
1261 hc[j++] = C[i];
1262 }
1263
1264 return 0;
1265}
1266
Harald Welteb9946d32017-06-12 09:40:16 +02001267static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001268 int mcs, int p, int blk)
1269{
1270 int i, j, data_len;
1271 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001272 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001273
1274 code = &gsm0503_mcs_dl_codes[mcs];
1275
1276 /*
1277 * Dual block - MCS-7,8,9
1278 * Single block - MCS-1,2,3,4,5,6
1279 */
1280 if (mcs >= EGPRS_MCS7)
1281 data_len = code->data_len / 2;
1282 else
1283 data_len = code->data_len;
1284
1285 osmo_pbit2ubit_ext(u, 0, l2_data,
1286 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1287
1288 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1289
1290 osmo_conv_encode(code->data_conv, u, C);
1291
1292 if (!code->data_punc[p]) {
1293 /* Invalid MCS-X data puncture matrix */
1294 return -1;
1295 }
1296
1297 for (i = 0, j = 0; i < code->data_code_len; i++) {
1298 if (!code->data_punc[p][i])
1299 c[j++] = C[i];
1300 }
1301
1302 return 0;
1303}
1304
1305/*
1306 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1307 *
1308 * Type 1 - MCS-7,8,9
1309 * Type 2 - MCS-5,6
1310 * Type 3 - MCS-1,2,3,4
1311 */
1312static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001313 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001314{
1315 uint8_t bits;
1316
1317 switch (type) {
1318 case EGPRS_HDR_TYPE1:
1319 bits = hdr->type1.cps;
1320 break;
1321 case EGPRS_HDR_TYPE2:
1322 bits = hdr->type2.cps;
1323 break;
1324 case EGPRS_HDR_TYPE3:
1325 bits = hdr->type3.cps;
1326 break;
1327 default:
1328 return -1;
1329 }
1330
1331 return egprs_get_cps(cps, type, bits);
1332}
1333
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001334/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001335 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1336 * \param[in] l2_data L2 (MAC) block to be encoded
1337 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001338 * \returns number of bits encoded; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001339int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001340 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001341{
1342 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1343 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1344 uint8_t mcs;
1345 struct egprs_cps cps;
1346 union gprs_rlc_dl_hdr_egprs *hdr;
1347
1348 switch (l2_len) {
1349 case 27:
1350 mcs = EGPRS_MCS1;
1351 break;
1352 case 33:
1353 mcs = EGPRS_MCS2;
1354 break;
1355 case 42:
1356 mcs = EGPRS_MCS3;
1357 break;
1358 case 49:
1359 mcs = EGPRS_MCS4;
1360 break;
1361 case 60:
1362 mcs = EGPRS_MCS5;
1363 break;
1364 case 78:
1365 mcs = EGPRS_MCS6;
1366 break;
1367 case 118:
1368 mcs = EGPRS_MCS7;
1369 break;
1370 case 142:
1371 mcs = EGPRS_MCS8;
1372 break;
1373 case 154:
1374 mcs = EGPRS_MCS9;
1375 break;
1376 default:
1377 return -1;
1378 }
1379
1380 /* Read header for USF and puncturing matrix selection. */
1381 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1382
1383 switch (mcs) {
1384 case EGPRS_MCS1:
1385 case EGPRS_MCS2:
1386 case EGPRS_MCS3:
1387 case EGPRS_MCS4:
1388 /* Check for valid CPS and matching MCS to message size */
1389 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1390 (cps.mcs != mcs))
1391 goto bad_header;
1392
1393 egprs_encode_hdr(hc, l2_data, mcs);
1394 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1395 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1396 break;
1397 case EGPRS_MCS5:
1398 case EGPRS_MCS6:
1399 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1400 (cps.mcs != mcs))
1401 goto bad_header;
1402
1403 egprs_encode_hdr(hc, l2_data, mcs);
1404 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1405 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1406 break;
1407 case EGPRS_MCS7:
1408 case EGPRS_MCS8:
1409 case EGPRS_MCS9:
1410 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1411 (cps.mcs != mcs))
1412 goto bad_header;
1413
1414 egprs_encode_hdr(hc, l2_data, mcs);
1415 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1416 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1417 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1418 break;
1419 }
1420
1421 return mcs >= EGPRS_MCS5 ?
1422 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1423
1424bad_header:
1425 /* Invalid EGPRS MCS-X header */
1426 return -1;
1427}
1428
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001429/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001430 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1431 * \param[in] l2_data L2 (MAC) block to be encoded
1432 * \param[in] l2_len length of l2_data in bytes, used to determine CS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001433 * \returns number of bits encoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001434int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001435{
1436 ubit_t iB[456], cB[676];
1437 const ubit_t *hl_hn;
1438 ubit_t conv[334];
1439 int i, j, usf;
1440
1441 switch (l2_len) {
1442 case 23:
1443 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1444
1445 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1446
1447 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1448
1449 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1450
1451 break;
1452 case 34:
1453 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1454 usf = l2_data[0] & 0x7;
1455
1456 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1457 271, conv + 3 + 271);
1458
1459 memcpy(conv, gsm0503_usf2six[usf], 6);
1460
1461 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1462
1463 for (i = 0, j = 0; i < 588; i++)
1464 if (!gsm0503_puncture_cs2[i])
1465 cB[j++] = cB[i];
1466
1467 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1468
1469 break;
1470 case 40:
1471 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1472 usf = l2_data[0] & 0x7;
1473
1474 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1475 315, conv + 3 + 315);
1476
1477 memcpy(conv, gsm0503_usf2six[usf], 6);
1478
1479 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1480
1481 for (i = 0, j = 0; i < 676; i++)
1482 if (!gsm0503_puncture_cs3[i])
1483 cB[j++] = cB[i];
1484
1485 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1486
1487 break;
1488 case 54:
1489 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1490 usf = l2_data[0] & 0x7;
1491
1492 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1493 431, cB + 9 + 431);
1494
1495 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1496
1497 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1498
1499 break;
1500 default:
1501 return -1;
1502 }
1503
1504 gsm0503_xcch_interleave(cB, iB);
1505
1506 for (i = 0; i < 4; i++) {
1507 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1508 hl_hn + i * 2, hl_hn + i * 2 + 1);
1509 }
1510
1511 return GSM0503_GPRS_BURSTS_NBITS;
1512}
1513
1514/*
1515 * GSM TCH/F FR/EFR transcoding
1516 */
1517
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001518/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001519 * \param[out] tch_data Codec frame in RTP format
1520 * \param[in] b_bits Codec frame in 'native' format
1521 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001522static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001523 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001524{
1525 int i, j, k, l, o;
1526
1527 tch_data[0] = 0xd << 4;
1528 memset(tch_data + 1, 0, 32);
1529
1530 if (net_order) {
1531 for (i = 0, j = 4; i < 260; i++, j++)
1532 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1533
1534 return;
1535 }
1536
1537 /* reassemble d-bits */
1538 i = 0; /* counts bits */
1539 j = 4; /* counts output bits */
1540 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1541 l = 0; /* counts element bits */
1542 o = 0; /* offset input bits */
1543 while (i < 260) {
1544 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1545 if (--k < 0) {
1546 o += gsm0503_gsm_fr_map[l];
1547 k = gsm0503_gsm_fr_map[++l]-1;
1548 }
1549 i++;
1550 j++;
1551 }
1552}
1553
1554static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001555 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001556{
1557 int i, j, k, l, o;
1558
1559 if (net_order) {
1560 for (i = 0, j = 4; i < 260; i++, j++)
1561 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1562
1563 return;
1564 }
1565
1566 i = 0; /* counts bits */
1567 j = 4; /* counts input bits */
1568 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1569 l = 0; /* counts element bits */
1570 o = 0; /* offset output bits */
1571 while (i < 260) {
1572 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1573 if (--k < 0) {
1574 o += gsm0503_gsm_fr_map[l];
1575 k = gsm0503_gsm_fr_map[++l] - 1;
1576 }
1577 i++;
1578 j++;
1579 }
1580}
1581
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001582/* assemble a HR codec frame in the canonical format of ETSI TS 101 318 */
Harald Welteb9946d32017-06-12 09:40:16 +02001583static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001584{
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001585 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001586
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001587 memset(tch_data, 0, GSM_HR_BYTES);
1588 for (i = 0; i < 112; i++)
1589 tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7)));
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001590}
1591
Harald Welteb9946d32017-06-12 09:40:16 +02001592static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001593{
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001594 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001595
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001596 for (i = 0; i < 112; i++)
1597 b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001598}
1599
Harald Weltec6636782017-06-12 14:59:37 +02001600/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001601static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001602{
1603 int i, j;
1604
1605 tch_data[0] = 0xc << 4;
1606 memset(tch_data + 1, 0, 30);
1607
1608 for (i = 0, j = 4; i < 244; i++, j++)
1609 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1610}
1611
Harald Welteb9946d32017-06-12 09:40:16 +02001612static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001613{
1614 int i, j;
1615
1616 for (i = 0, j = 4; i < 244; i++, j++)
1617 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1618}
1619
Harald Weltec6636782017-06-12 14:59:37 +02001620/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001621static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001622{
1623 int i, j;
1624
1625 memset(tch_data, 0, (len + 7) >> 3);
1626
1627 for (i = 0, j = 0; i < len; i++, j++)
1628 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1629}
1630
Harald Welteb9946d32017-06-12 09:40:16 +02001631static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001632{
1633 int i, j;
1634
1635 for (i = 0, j = 0; i < len; i++, j++)
1636 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1637}
1638
Philipp Maier898c9c62020-02-06 14:25:01 +01001639/* Append STI and MI bits to the SID_UPDATE frame, see also
1640 * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */
1641static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi)
1642{
1643 /* Zero out the space that had been used by the CRC14 */
1644 memset(sid_update + 35, 0, 14);
1645
1646 /* Append STI and MI parameters */
1647 sid_update[35] = sti & 1;
1648 sid_update[36] = mi & 1;
1649 sid_update[37] = mi >> 1 & 1;
1650 sid_update[38] = mi >> 2 & 1;
1651}
1652
1653/* Extract a SID UPDATE fram the sbits of an FR AMR frame */
1654static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits)
1655{
1656
1657 unsigned int i;
1658
1659 sbits += 32;
1660
1661 for (i = 0; i < 53; i++) {
1662 sid_update[0] = sbits[0];
1663 sid_update[1] = sbits[1];
1664 sid_update[2] = sbits[2];
1665 sid_update[3] = sbits[3];
1666 sid_update += 4;
1667 sbits += 8;
1668 }
1669
1670}
1671
Harald Weltec6636782017-06-12 14:59:37 +02001672/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001673static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001674{
1675 int i;
1676
1677 for (i = 0; i < 260; i++)
1678 b_bits[gsm610_bitorder[i]] = d_bits[i];
1679}
1680
Harald Weltec6636782017-06-12 14:59:37 +02001681/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001682static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001683{
1684 int i;
1685
1686 for (i = 0; i < 260; i++)
1687 d_bits[i] = b_bits[gsm610_bitorder[i]];
1688}
1689
Harald Weltec6636782017-06-12 14:59:37 +02001690/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001691static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001692{
1693 int i;
1694
1695 const uint16_t *map;
1696
1697 if (!d_bits[93] && !d_bits[94])
1698 map = gsm620_unvoiced_bitorder;
1699 else
1700 map = gsm620_voiced_bitorder;
1701
1702 for (i = 0; i < 112; i++)
1703 b_bits[map[i]] = d_bits[i];
1704}
1705
Harald Weltec6636782017-06-12 14:59:37 +02001706/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001707static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001708{
1709 int i;
1710 const uint16_t *map;
1711
1712 if (!b_bits[34] && !b_bits[35])
1713 map = gsm620_unvoiced_bitorder;
1714 else
1715 map = gsm620_voiced_bitorder;
1716
1717 for (i = 0; i < 112; i++)
1718 d_bits[i] = b_bits[map[i]];
1719}
1720
Harald Weltec6636782017-06-12 14:59:37 +02001721/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001722static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001723{
1724 int i;
1725
1726 for (i = 0; i < 260; i++)
1727 b_bits[gsm660_bitorder[i]] = d_bits[i];
1728}
1729
Harald Weltec6636782017-06-12 14:59:37 +02001730/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001731static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001732{
1733 int i;
1734
1735 for (i = 0; i < 260; i++)
1736 d_bits[i] = b_bits[gsm660_bitorder[i]];
1737}
1738
Harald Weltec6636782017-06-12 14:59:37 +02001739/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001740static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001741{
1742 int i;
1743
1744 for (i = 0; i < 65; i++)
1745 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1746}
1747
Harald Welteb9946d32017-06-12 09:40:16 +02001748static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001749{
1750 int i;
1751
1752 for (i = 0; i < 91; i++) {
1753 d[i << 1] = u[i];
1754 d[(i << 1) + 1] = u[184 - i];
1755 }
1756
1757 for (i = 0; i < 3; i++)
1758 p[i] = u[91 + i];
1759}
1760
Harald Welteb9946d32017-06-12 09:40:16 +02001761static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001762{
1763 int i;
1764
1765 for (i = 0; i < 91; i++) {
1766 u[i] = d[i << 1];
1767 u[184 - i] = d[(i << 1) + 1];
1768 }
1769
1770 for (i = 0; i < 3; i++)
1771 u[91 + i] = p[i];
1772}
1773
Harald Welteb9946d32017-06-12 09:40:16 +02001774static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001775{
1776 memcpy(d, u, 95);
1777 memcpy(p, u + 95, 3);
1778}
1779
Harald Welteb9946d32017-06-12 09:40:16 +02001780static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001781{
1782 memcpy(u, d, 95);
1783 memcpy(u + 95, p, 3);
1784}
1785
Harald Welteb9946d32017-06-12 09:40:16 +02001786static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001787{
1788 memcpy(w, s, 71);
1789 w[71] = w[72] = s[69];
1790 memcpy(w + 73, s + 71, 50);
1791 w[123] = w[124] = s[119];
1792 memcpy(w + 125, s + 121, 53);
1793 w[178] = w[179] = s[172];
1794 memcpy(w + 180, s + 174, 50);
1795 w[230] = w[231] = s[222];
1796 memcpy(w + 232, s + 224, 20);
1797 memcpy(w + 252, p, 8);
1798}
1799
Harald Welteb9946d32017-06-12 09:40:16 +02001800static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001801{
1802 int sum;
1803
1804 memcpy(s, w, 71);
1805 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001806 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001807 memcpy(s + 71, w + 73, 50);
1808 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001809 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001810 memcpy(s + 121, w + 125, 53);
1811 sum = s[172] + w[178] + w[179];
Mychaela N. Falconiaf3e47452023-05-17 18:58:40 +00001812 s[172] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001813 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001814 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001815 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001816 memcpy(s + 224, w + 232, 20);
1817 memcpy(p, w + 252, 8);
1818}
1819
Harald Welteb9946d32017-06-12 09:40:16 +02001820static 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 +07001821{
1822 memcpy(u, d, prot);
1823 memcpy(u + prot, p, 6);
1824 memcpy(u + prot + 6, d + prot, len - prot);
1825}
1826
Harald Welteb9946d32017-06-12 09:40:16 +02001827static 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 +07001828{
1829 memcpy(d, u, prot);
1830 memcpy(p, u + prot, 6);
1831 memcpy(d + prot, u + prot + 6, len - prot);
1832}
1833
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001834/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001835 * \param[out] tch_data Codec frame in RTP payload format
1836 * \param[in] bursts buffer containing the symbols of 8 bursts
1837 * \param[in] net_order FIXME
1838 * \param[in] efr Is this channel using EFR (1) or FR (0)
1839 * \param[out] n_errors Number of detected bit errors
1840 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001841 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001842int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001843 int net_order, int efr, int *n_errors, int *n_bits_total)
1844{
1845 sbit_t iB[912], cB[456], h;
1846 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1847 int i, rv, len, steal = 0;
1848
Harald Weltec6636782017-06-12 14:59:37 +02001849 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001850 for (i = 0; i < 8; i++) {
1851 gsm0503_tch_burst_unmap(&iB[i * 114],
1852 &bursts[i * 116], &h, i >> 2);
1853 steal -= h;
1854 }
Harald Weltec6636782017-06-12 14:59:37 +02001855 /* we now have the bits of the four bursts (interface 4 in
1856 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001857
1858 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001859 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001860
1861 if (steal > 0) {
1862 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1863 if (rv) {
1864 /* Error decoding FACCH frame */
1865 return -1;
1866 }
1867
Vadim Yanitskiyc0305b62023-05-20 16:33:38 +07001868 return GSM_MACBLOCK_LEN;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001869 }
1870
1871 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001872 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001873
Harald Weltec6636782017-06-12 14:59:37 +02001874 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001875 tch_fr_unreorder(d, p, conv);
1876
1877 for (i = 0; i < 78; i++)
1878 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1879
Harald Weltec6636782017-06-12 14:59:37 +02001880 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001881 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1882 if (rv) {
1883 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1884 return -1;
1885 }
1886
1887 if (efr) {
1888 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001889 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001890
1891 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001892 /* we now have the data delivered to the preliminary
1893 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001894
Harald Weltec6636782017-06-12 14:59:37 +02001895 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001896 tch_efr_protected(s, b);
1897
Harald Weltec6636782017-06-12 14:59:37 +02001898 /* perform CRC-8 on 65 most important bits (50 bits of
1899 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001900 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1901 if (rv) {
1902 /* Error checking CRC8 for the EFR part of an EFR frame */
1903 return -1;
1904 }
1905
1906 tch_efr_reassemble(tch_data, s);
1907
1908 len = GSM_EFR_BYTES;
1909 } else {
1910 tch_fr_d_to_b(w, d);
1911
1912 tch_fr_reassemble(tch_data, w, net_order);
1913
1914 len = GSM_FR_BYTES;
1915 }
1916
1917 return len;
1918}
1919
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001920/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001921 * \param[out] bursts caller-allocated output buffer for bursts bits
1922 * \param[in] tch_data Codec input data in RTP payload format
1923 * \param[in] len Length of \a tch_data in bytes
1924 * \param[in] net_order FIXME
1925 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001926int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001927 int len, int net_order)
1928{
1929 ubit_t iB[912], cB[456], h;
1930 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1931 int i;
1932
1933 switch (len) {
1934 case GSM_EFR_BYTES: /* TCH EFR */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001935 tch_efr_disassemble(s, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001936 tch_efr_protected(s, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001937 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001938 tch_efr_reorder(w, s, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001939 tch_efr_w_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001940 goto coding_efr_fr;
1941 case GSM_FR_BYTES: /* TCH FR */
1942 tch_fr_disassemble(w, tch_data, net_order);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001943 tch_fr_b_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001944coding_efr_fr:
1945 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001946 tch_fr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001947 memcpy(cB + 378, d + 182, 78);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001948 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001949 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001950 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00001951 case 0: /* no data, induce BFI in the receiver */
1952 /* Do the same thing that sysmoBTS PHY does when fed a 0-length
1953 * payload for DL: set all u(k) bits to 0, and do the same
1954 * with all class 2 bits. This operation is NOT the same as
1955 * an FR codec frame of all zero bits: with all-zeros d(k) input
1956 * the CRC3 function will produce 111 output, whereas we
1957 * transmit 000 in those parity bits too. The result will be
1958 * an induced BFI (bad frame indication) condition in the
1959 * receiver, for both TCH/FS and TCH/EFS decoders. */
1960 memset(conv, 0, sizeof(conv));
1961 memset(cB + 378, 0, 78);
1962 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1963 h = 0;
1964 break;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001965 case GSM_MACBLOCK_LEN: /* FACCH */
1966 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001967 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001968 break;
1969 default:
1970 return -1;
1971 }
1972
1973 gsm0503_tch_fr_interleave(cB, iB);
1974
1975 for (i = 0; i < 8; i++) {
1976 gsm0503_tch_burst_map(&iB[i * 114],
1977 &bursts[i * 116], &h, i >> 2);
1978 }
1979
1980 return 0;
1981}
1982
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001983/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001984 * \param[out] tch_data Codec frame in TS 101 318 canonical format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07001985 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02001986 * \param[in] odd Odd (1) or even (0) frame number
1987 * \param[out] n_errors Number of detected bit errors
1988 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001989 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001990int gsm0503_tch_hr_decode2(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001991 int *n_errors, int *n_bits_total)
1992{
1993 sbit_t iB[912], cB[456], h;
1994 ubit_t conv[98], b[112], d[112], p[3];
1995 int i, rv, steal = 0;
1996
1997 /* Only unmap the stealing bits */
1998 if (!odd) {
1999 for (i = 0; i < 4; i++) {
2000 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2001 steal -= h;
2002 }
2003
Mychaela N. Falconia920c0ed2023-05-23 14:06:53 +00002004 for (i = 2; i < 6; i++) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002005 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2006 steal -= h;
2007 }
2008 }
2009
2010 /* If we found a stole FACCH, but only at correct alignment */
2011 if (steal > 0) {
2012 for (i = 0; i < 6; i++) {
2013 gsm0503_tch_burst_unmap(&iB[i * 114],
2014 &bursts[i * 116], NULL, i >> 2);
2015 }
2016
2017 for (i = 2; i < 4; i++) {
2018 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2019 &bursts[i * 116], NULL, 1);
2020 }
2021
2022 gsm0503_tch_fr_deinterleave(cB, iB);
2023
2024 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2025 if (rv) {
2026 /* Error decoding FACCH frame */
2027 return -1;
2028 }
2029
2030 return GSM_MACBLOCK_LEN;
2031 }
2032
2033 for (i = 0; i < 4; i++) {
2034 gsm0503_tch_burst_unmap(&iB[i * 114],
2035 &bursts[i * 116], NULL, i >> 1);
2036 }
2037
2038 gsm0503_tch_hr_deinterleave(cB, iB);
2039
2040 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
2041
2042 tch_hr_unreorder(d, p, conv);
2043
2044 for (i = 0; i < 17; i++)
2045 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
2046
2047 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2048 if (rv) {
2049 /* Error checking CRC8 for an HR frame */
2050 return -1;
2051 }
2052
2053 tch_hr_d_to_b(b, d);
2054
2055 tch_hr_reassemble(tch_data, b);
2056
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002057 return GSM_HR_BYTES;
2058}
2059
2060/*! Perform channel decoding of a HR(v1) channel according TS 05.03,
2061 * deprecated legacy API.
2062 * \param[out] tch_data Codec frame in pseudo-RFC5993 format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002063 * \param[in] bursts buffer containing the symbols of 6 bursts
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002064 * \param[in] odd Odd (1) or even (0) frame number
2065 * \param[out] n_errors Number of detected bit errors
2066 * \param[out] n_bits_total Total number of bits
2067 * \returns length of bytes used in \a tch_data output buffer; negative on error
2068 *
2069 * The HR1 codec frame format returned by this function is pseudo-RFC5993,
2070 * not true RFC 5993, as there is no SID classification being done
2071 * and the FT bits in the ToC octet are always set to 0 - but this
2072 * arguably-bogus format is the legacy public API.
2073 */
2074int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
2075 int *n_errors, int *n_bits_total)
2076{
2077 int rc;
2078
2079 rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors,
2080 n_bits_total);
2081 if (rc != GSM_HR_BYTES)
2082 return rc;
2083 memmove(tch_data + 1, tch_data, GSM_HR_BYTES);
2084 tch_data[0] = 0x00; /* FT=0, note absence of SID classification */
2085 return GSM_HR_BYTES_RTP_RFC5993;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002086}
2087
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002088/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002089 * \param[out] bursts caller-allocated output buffer for bursts bits
2090 * \param[in] tch_data Codec input data in RTP payload format
2091 * \param[in] len Length of \a tch_data in bytes
2092 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002093int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002094{
2095 ubit_t iB[912], cB[456], h;
2096 ubit_t conv[98], b[112], d[112], p[3];
2097 int i;
2098
2099 switch (len) {
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00002100 case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */
2101 tch_data++;
2102 /* fall-through */
2103 case GSM_HR_BYTES: /* TCH HR in "pure" form */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002104 tch_hr_disassemble(b, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002105 tch_hr_b_to_d(d, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002106 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002107 tch_hr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002108 memcpy(cB + 211, d + 95, 17);
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002109hr_conv_coding:
2110 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002111 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002112 gsm0503_tch_hr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002113 for (i = 0; i < 4; i++) {
2114 gsm0503_tch_burst_map(&iB[i * 114],
2115 &bursts[i * 116], &h, i >> 1);
2116 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002117 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002118 case 0: /* no data, induce BFI in the receiver */
2119 /* see comments in gsm0503_tch_fr_encode() - same deal here */
2120 memset(conv, 0, sizeof(conv));
2121 memset(cB + 211, 0, 17);
2122 goto hr_conv_coding;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002123 case GSM_MACBLOCK_LEN: /* FACCH */
2124 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002125 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002126 gsm0503_tch_fr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002127 for (i = 0; i < 6; i++) {
2128 gsm0503_tch_burst_map(&iB[i * 114],
2129 &bursts[i * 116], &h, i >> 2);
2130 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002131 for (i = 2; i < 4; i++) {
2132 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2133 &bursts[i * 116], &h, 1);
2134 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002135 break;
2136 default:
2137 return -1;
2138 }
2139
2140 return 0;
2141}
2142
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002143/* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2144static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB)
2145{
2146 unsigned int id = 0, best = 0;
2147 unsigned int i, j, k;
2148
2149 for (i = 0; i < 4; i++) {
2150 /* FIXME: why not using remaining (16 - 8) soft-bits here? */
2151 for (j = 0, k = 0; j < 8; j++)
2152 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2153
2154 if (i == 0 || k < best) {
2155 best = k;
2156 id = i;
2157 }
2158 }
2159
2160 return id;
2161}
2162
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002163/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002164 * \param[out] tch_data Codec frame in RTP payload format
2165 * \param[in] bursts buffer containing the symbols of 8 bursts
2166 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2167 * \param[in] codec array of active codecs (active codec set)
2168 * \param[in] codecs number of codecs in \a codec
2169 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2170 * \param[out] cmr Output in \a codec_mode_req = 1
2171 * \param[out] n_errors Number of detected bit errors
2172 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002173 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2174 * codec out of range; negative on error
2175 */
Harald Welteb9946d32017-06-12 09:40:16 +02002176int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002177 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2178 uint8_t *cmr, int *n_errors, int *n_bits_total)
2179{
Philipp Maier898c9c62020-02-06 14:25:01 +01002180 return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req,
2181 codec, codecs, ft, cmr, n_errors,
2182 n_bits_total, NULL);
2183}
2184
2185/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2186 * \param[out] tch_data Codec frame in RTP payload format
2187 * \param[in] bursts buffer containing the symbols of 8 bursts
2188 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2189 * \param[in] codec array of active codecs (active codec set)
2190 * \param[in] codecs number of codecs in \a codec
2191 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2192 * \param[out] cmr Output in \a codec_mode_req = 1
2193 * \param[out] n_errors Number of detected bit errors
2194 * \param[out] n_bits_total Total number of bits
2195 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2196 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2197 * codec out of range; negative on error
2198 */
2199int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts,
2200 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2201 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2202{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002203 sbit_t iB[912], cB[456], h;
2204 ubit_t d[244], p[6], conv[250];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002205 int i, rv, len, steal = 0, id = -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002206 *n_errors = 0; *n_bits_total = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002207 static ubit_t sid_first_dummy[64] = { 0 };
2208 sbit_t sid_update_enc[256];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002209
2210 for (i=0; i<8; i++) {
2211 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2212 steal -= h;
2213 }
2214
2215 gsm0503_tch_fr_deinterleave(cB, iB);
2216
2217 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002218 /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002219 * It's normally updated by gsm0503_detect_afs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002220 * reached in case of FACCH. Reset it here to avoid FACCH/F frames being
2221 * misinterpreted as AMR's special DTX frames. */
2222 if (dtx != NULL)
2223 *dtx = AMR_OTHER;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002224 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2225 if (rv) {
2226 /* Error decoding FACCH frame */
2227 return -1;
2228 }
2229
2230 return GSM_MACBLOCK_LEN;
2231 }
2232
Philipp Maier898c9c62020-02-06 14:25:01 +01002233 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2234 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002235 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2236
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002237 *dtx = gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Philipp Maier898c9c62020-02-06 14:25:01 +01002238
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002239 switch (*dtx) {
2240 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002241 /* NOTE: The AFS_SID_UPDATE frame is splitted into
2242 * two half rate frames. If the id marker frame
2243 * (AFS_SID_UPDATE) is detected the following frame
2244 * contains the actual comfort noised data part of
2245 * (AFS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002246 if (dtx_prev != AFS_SID_UPDATE)
2247 break;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002248 /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002249 *dtx = AFS_SID_UPDATE_CN;
2250
2251 extract_afs_sid_update(sid_update_enc, cB);
2252 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2253 sid_update_enc, conv, n_errors,
2254 n_bits_total);
2255 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2256 35, conv + 35);
2257 if (rv != 0) {
2258 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2259 return -1;
2260 }
2261
2262 tch_amr_sid_update_append(conv, 1,
2263 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002264 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002265 tch_amr_reassemble(tch_data, conv, 39);
2266 len = 5;
2267 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002268 case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002269 tch_amr_sid_update_append(sid_first_dummy, 0,
2270 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002271 : codec[id > 0 ? id : 0]);
Vadim Yanitskiy7d121cb2023-06-23 16:57:48 +07002272 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
Philipp Maier898c9c62020-02-06 14:25:01 +01002273 len = 5;
2274 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002275 case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002276 case AFS_ONSET:
Philipp Maier898c9c62020-02-06 14:25:01 +01002277 len = 0;
2278 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002279 default:
2280 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002281 }
2282 }
2283
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002284 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2285 if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002286 /* Codec mode out of range, return id */
2287 return id;
2288 }
2289
2290 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2291 case 7: /* TCH/AFS12.2 */
2292 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2293 conv, n_errors, n_bits_total);
2294
2295 tch_amr_unmerge(d, p, conv, 244, 81);
2296
2297 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2298 if (rv) {
2299 /* Error checking CRC8 for an AMR 12.2 frame */
2300 return -1;
2301 }
2302
2303 tch_amr_reassemble(tch_data, d, 244);
2304
2305 len = 31;
2306
2307 break;
2308 case 6: /* TCH/AFS10.2 */
2309 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2310 conv, n_errors, n_bits_total);
2311
2312 tch_amr_unmerge(d, p, conv, 204, 65);
2313
2314 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2315 if (rv) {
2316 /* Error checking CRC8 for an AMR 10.2 frame */
2317 return -1;
2318 }
2319
2320 tch_amr_reassemble(tch_data, d, 204);
2321
2322 len = 26;
2323
2324 break;
2325 case 5: /* TCH/AFS7.95 */
2326 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2327 conv, n_errors, n_bits_total);
2328
2329 tch_amr_unmerge(d, p, conv, 159, 75);
2330
2331 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2332 if (rv) {
2333 /* Error checking CRC8 for an AMR 7.95 frame */
2334 return -1;
2335 }
2336
2337 tch_amr_reassemble(tch_data, d, 159);
2338
2339 len = 20;
2340
2341 break;
2342 case 4: /* TCH/AFS7.4 */
2343 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2344 conv, n_errors, n_bits_total);
2345
2346 tch_amr_unmerge(d, p, conv, 148, 61);
2347
2348 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2349 if (rv) {
2350 /* Error checking CRC8 for an AMR 7.4 frame */
2351 return -1;
2352 }
2353
2354 tch_amr_reassemble(tch_data, d, 148);
2355
2356 len = 19;
2357
2358 break;
2359 case 3: /* TCH/AFS6.7 */
2360 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2361 conv, n_errors, n_bits_total);
2362
2363 tch_amr_unmerge(d, p, conv, 134, 55);
2364
2365 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2366 if (rv) {
2367 /* Error checking CRC8 for an AMR 6.7 frame */
2368 return -1;
2369 }
2370
2371 tch_amr_reassemble(tch_data, d, 134);
2372
2373 len = 17;
2374
2375 break;
2376 case 2: /* TCH/AFS5.9 */
2377 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2378 conv, n_errors, n_bits_total);
2379
2380 tch_amr_unmerge(d, p, conv, 118, 55);
2381
2382 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2383 if (rv) {
2384 /* Error checking CRC8 for an AMR 5.9 frame */
2385 return -1;
2386 }
2387
2388 tch_amr_reassemble(tch_data, d, 118);
2389
2390 len = 15;
2391
2392 break;
2393 case 1: /* TCH/AFS5.15 */
2394 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2395 conv, n_errors, n_bits_total);
2396
2397 tch_amr_unmerge(d, p, conv, 103, 49);
2398
2399 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2400 if (rv) {
2401 /* Error checking CRC8 for an AMR 5.15 frame */
2402 return -1;
2403 }
2404
2405 tch_amr_reassemble(tch_data, d, 103);
2406
2407 len = 13;
2408
2409 break;
2410 case 0: /* TCH/AFS4.75 */
2411 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2412 conv, n_errors, n_bits_total);
2413
2414 tch_amr_unmerge(d, p, conv, 95, 39);
2415
2416 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2417 if (rv) {
2418 /* Error checking CRC8 for an AMR 4.75 frame */
2419 return -1;
2420 }
2421
2422 tch_amr_reassemble(tch_data, d, 95);
2423
2424 len = 12;
2425
2426 break;
2427 default:
2428 /* Unknown frame type */
2429 *n_bits_total = 448;
2430 *n_errors = *n_bits_total;
2431 return -1;
2432 }
2433
Philipp Maier898c9c62020-02-06 14:25:01 +01002434out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002435 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002436 if (id != -1) {
2437 if (codec_mode_req)
2438 *cmr = id;
2439 else
2440 *ft = id;
2441 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002442
2443 return len;
2444}
2445
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002446/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002447 * \param[out] bursts caller-allocated output buffer for bursts bits
2448 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002449 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002450 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2451 * \param[in] codec Array of codecs (active codec set)
2452 * \param[in] codecs Number of entries in \a codec
2453 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2454 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2455 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002456int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002457 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002458 uint8_t cmr)
2459{
2460 ubit_t iB[912], cB[456], h;
2461 ubit_t d[244], p[6], conv[250];
2462 int i;
2463 uint8_t id;
2464
2465 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2466 _xcch_encode_cB(cB, tch_data);
2467
2468 h = 1;
2469
2470 goto facch;
2471 }
2472
2473 h = 0;
2474
Vadim Yanitskiya9952692023-11-10 03:31:55 +07002475 id = codec_mode_req ? cmr : ft;
2476 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit)))
2477 return -1;
2478 if (OSMO_UNLIKELY(ft >= codecs))
2479 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002480
2481 switch (codec[ft]) {
2482 case 7: /* TCH/AFS12.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002483 if (!len) {
2484 /* No data, induce BFI in the receiver by inverted CRC bits.
2485 * The data bit are all 0, so the correct parity bits would be 111111. */
2486 memset(d, 0, 244);
2487 memset(p, 0, 6);
2488 } else {
2489 if (len != 31)
2490 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002491
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002492 tch_amr_disassemble(d, tch_data, 244);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002493
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002494 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2495 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002496
2497 tch_amr_merge(conv, d, p, 244, 81);
2498
2499 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2500
2501 break;
2502 case 6: /* TCH/AFS10.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002503 if (!len) {
2504 /* See comment above. */
2505 memset(d, 0, 204);
2506 memset(p, 0, 6);
2507 } else {
2508 if (len != 26)
2509 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002510
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002511 tch_amr_disassemble(d, tch_data, 204);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002512
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002513 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2514 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002515
2516 tch_amr_merge(conv, d, p, 204, 65);
2517
2518 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2519
2520 break;
2521 case 5: /* TCH/AFS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002522 if (!len) {
2523 /* See comment above. */
2524 memset(d, 0, 159);
2525 memset(p, 0, 6);
2526 } else {
2527 if (len != 20)
2528 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002529
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002530 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002531
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002532 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2533 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002534
2535 tch_amr_merge(conv, d, p, 159, 75);
2536
2537 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2538
2539 break;
2540 case 4: /* TCH/AFS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002541 if (!len) {
2542 /* See comment above. */
2543 memset(d, 0, 148);
2544 memset(p, 0, 6);
2545 } else {
2546 if (len != 19)
2547 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002548
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002549 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002550
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002551 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2552 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002553
2554 tch_amr_merge(conv, d, p, 148, 61);
2555
2556 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2557
2558 break;
2559 case 3: /* TCH/AFS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002560 if (!len) {
2561 /* See comment above. */
2562 memset(d, 0, 134);
2563 memset(p, 0, 6);
2564 } else {
2565 if (len != 17)
2566 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002567
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002568 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002569
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002570 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2571 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002572
2573 tch_amr_merge(conv, d, p, 134, 55);
2574
2575 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2576
2577 break;
2578 case 2: /* TCH/AFS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002579 if (!len) {
2580 /* See comment above. */
2581 memset(d, 0, 118);
2582 memset(p, 0, 6);
2583 } else {
2584 if (len != 15)
2585 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002586
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002587 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002588
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002589 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2590 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002591
2592 tch_amr_merge(conv, d, p, 118, 55);
2593
2594 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2595
2596 break;
2597 case 1: /* TCH/AFS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002598 if (!len) {
2599 /* See comment above. */
2600 memset(d, 0, 103);
2601 memset(p, 0, 6);
2602 } else {
2603 if (len != 13)
2604 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002605
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002606 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002607
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002608 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2609 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002610
2611 tch_amr_merge(conv, d, p, 103, 49);
2612
2613 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2614
2615 break;
2616 case 0: /* TCH/AFS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002617 if (!len) {
2618 /* See comment above. */
2619 memset(d, 0, 95);
2620 memset(p, 0, 6);
2621 } else {
2622 if (len != 12)
2623 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002624
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002625 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002626
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002627 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2628 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002629
2630 tch_amr_merge(conv, d, p, 95, 39);
2631
2632 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2633
2634 break;
2635 default:
2636 /* FIXME: FT %ft is not supported */
2637 return -1;
2638 }
2639
2640 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2641
2642facch:
2643 gsm0503_tch_fr_interleave(cB, iB);
2644
2645 for (i = 0; i < 8; i++) {
2646 gsm0503_tch_burst_map(&iB[i * 114],
2647 &bursts[i * 116], &h, i >> 2);
2648 }
2649
2650 return 0;
2651
2652invalid_length:
2653 /* FIXME: payload length %len does not comply with codec type %ft */
2654 return -1;
2655}
2656
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002657/* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2658static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB)
2659{
2660 unsigned int id = 0, best = 0;
2661 unsigned int i, j, k;
2662
2663 for (i = 0, k = 0; i < 4; i++) {
2664 /* FIXME: why not using remaining (16 - 4) soft-bits here? */
2665 for (j = 0, k = 0; j < 4; j++)
2666 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2667
2668 if (i == 0 || k < best) {
2669 best = k;
2670 id = i;
2671 }
2672 }
2673
2674 return id;
2675}
2676
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002677/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002678 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002679 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02002680 * \param[in] odd Is this an odd (1) or even (0) frame number?
2681 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2682 * \param[in] codec array of active codecs (active codec set)
2683 * \param[in] codecs number of codecs in \a codec
2684 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2685 * \param[out] cmr Output in \a codec_mode_req = 1
2686 * \param[out] n_errors Number of detected bit errors
2687 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002688 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2689 * codec out of range; negative on error
2690 */
Harald Welteb9946d32017-06-12 09:40:16 +02002691int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002692 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2693 uint8_t *cmr, int *n_errors, int *n_bits_total)
2694{
Philipp Maier898c9c62020-02-06 14:25:01 +01002695 return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req,
2696 codec, codecs, ft, cmr, n_errors,
2697 n_bits_total, NULL);
2698}
2699
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002700/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Philipp Maier898c9c62020-02-06 14:25:01 +01002701 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002702 * \param[in] bursts buffer containing the symbols of 6 bursts
Philipp Maier898c9c62020-02-06 14:25:01 +01002703 * \param[in] odd Is this an odd (1) or even (0) frame number?
2704 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2705 * \param[in] codec array of active codecs (active codec set)
2706 * \param[in] codecs number of codecs in \a codec
2707 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2708 * \param[out] cmr Output in \a codec_mode_req = 1
2709 * \param[out] n_errors Number of detected bit errors
2710 * \param[out] n_bits_total Total number of bits
2711 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2712 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2713 * codec out of range; negative on error
2714 */
2715int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd,
2716 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2717 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2718{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002719 sbit_t iB[912], cB[456], h;
2720 ubit_t d[244], p[6], conv[135];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002721 int i, rv, len, steal = 0, id = -1;
Philipp Maier898c9c62020-02-06 14:25:01 +01002722 static ubit_t sid_first_dummy[64] = { 0 };
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002723
2724 /* only unmap the stealing bits */
2725 if (!odd) {
2726 for (i = 0; i < 4; i++) {
2727 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2728 steal -= h;
2729 }
2730 for (i = 2; i < 5; i++) {
2731 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2732 steal -= h;
2733 }
2734 }
2735
2736 /* if we found a stole FACCH, but only at correct alignment */
2737 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002738 /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002739 * It's normally updated by gsm0503_detect_ahs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002740 * reached in case of FACCH. Reset it here to avoid FACCH/H frames being
2741 * misinterpreted as AMR's special DTX frames. */
2742 if (dtx != NULL)
2743 *dtx = AMR_OTHER;
2744
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002745 for (i = 0; i < 6; i++) {
2746 gsm0503_tch_burst_unmap(&iB[i * 114],
2747 &bursts[i * 116], NULL, i >> 2);
2748 }
2749
2750 for (i = 2; i < 4; i++) {
2751 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2752 &bursts[i * 116], NULL, 1);
2753 }
2754
2755 gsm0503_tch_fr_deinterleave(cB, iB);
2756
2757 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2758 if (rv) {
2759 /* Error decoding FACCH frame */
2760 return -1;
2761 }
2762
2763 return GSM_MACBLOCK_LEN;
2764 }
2765
2766 for (i = 0; i < 4; i++) {
2767 gsm0503_tch_burst_unmap(&iB[i * 114],
2768 &bursts[i * 116], NULL, i >> 1);
2769 }
2770
2771 gsm0503_tch_hr_deinterleave(cB, iB);
2772
Philipp Maier898c9c62020-02-06 14:25:01 +01002773 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2774 if (dtx) {
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002775 int n_bits_total_sid;
2776 int n_errors_sid;
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002777
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002778 *dtx = gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002779 /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */
Philipp Maier898c9c62020-02-06 14:25:01 +01002780
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002781 switch (*dtx) {
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002782 case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002783 /* cB[] contains 16 bits of coded in-band data and 212 bits containing
2784 * the identification marker. We need to unmap/deinterleave 114 odd
2785 * bits from the last two blocks, 114 even bits from the first two
2786 * blocks and combine them together. */
2787 gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0);
2788 gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0);
2789 gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1);
2790 gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1);
2791 gsm0503_tch_hr_deinterleave(cB, iB);
2792
2793 /* cB[] is expected to contain 16 bits of coded in-band data and
2794 * 212 bits containing the coded data (53 bits coded at 1/4 rate). */
Philipp Maier898c9c62020-02-06 14:25:01 +01002795 *dtx = AHS_SID_UPDATE_CN;
2796
2797 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002798 cB + 16, conv, &n_errors_sid,
2799 &n_bits_total_sid);
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002800 /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002801 * osmo_conv_decode_ber() calculates BER for the coded data. */
2802 if (n_errors != NULL)
2803 *n_errors += n_errors_sid;
2804 if (n_bits_total != NULL)
2805 *n_bits_total += n_bits_total_sid;
Philipp Maier898c9c62020-02-06 14:25:01 +01002806 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2807 35, conv + 35);
2808 if (rv != 0) {
2809 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2810 return -1;
2811 }
2812
2813 tch_amr_sid_update_append(conv, 1,
2814 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002815 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002816 tch_amr_reassemble(tch_data, conv, 39);
2817 len = 5;
2818 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002819 case AHS_SID_FIRST_P2:
Philipp Maier898c9c62020-02-06 14:25:01 +01002820 tch_amr_sid_update_append(sid_first_dummy, 0,
2821 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002822 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002823 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
2824 len = 5;
2825 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002826 case AHS_ONSET:
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002827 case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2828 case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2829 case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002830 len = 0;
2831 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002832 default:
2833 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002834 }
2835 }
2836
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002837 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2838 if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002839 /* Codec mode out of range, return id */
2840 return id;
2841 }
2842
2843 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2844 case 5: /* TCH/AHS7.95 */
2845 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2846 conv, n_errors, n_bits_total);
2847
2848 tch_amr_unmerge(d, p, conv, 123, 67);
2849
2850 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2851 if (rv) {
2852 /* Error checking CRC8 for an AMR 7.95 frame */
2853 return -1;
2854 }
2855
2856 for (i = 0; i < 36; i++)
2857 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2858
2859 tch_amr_reassemble(tch_data, d, 159);
2860
2861 len = 20;
2862
2863 break;
2864 case 4: /* TCH/AHS7.4 */
2865 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2866 conv, n_errors, n_bits_total);
2867
2868 tch_amr_unmerge(d, p, conv, 120, 61);
2869
2870 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2871 if (rv) {
2872 /* Error checking CRC8 for an AMR 7.4 frame */
2873 return -1;
2874 }
2875
2876 for (i = 0; i < 28; i++)
2877 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2878
2879 tch_amr_reassemble(tch_data, d, 148);
2880
2881 len = 19;
2882
2883 break;
2884 case 3: /* TCH/AHS6.7 */
2885 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2886 conv, n_errors, n_bits_total);
2887
2888 tch_amr_unmerge(d, p, conv, 110, 55);
2889
2890 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2891 if (rv) {
2892 /* Error checking CRC8 for an AMR 6.7 frame */
2893 return -1;
2894 }
2895
2896 for (i = 0; i < 24; i++)
2897 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2898
2899 tch_amr_reassemble(tch_data, d, 134);
2900
2901 len = 17;
2902
2903 break;
2904 case 2: /* TCH/AHS5.9 */
2905 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2906 conv, n_errors, n_bits_total);
2907
2908 tch_amr_unmerge(d, p, conv, 102, 55);
2909
2910 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2911 if (rv) {
2912 /* Error checking CRC8 for an AMR 5.9 frame */
2913 return -1;
2914 }
2915
2916 for (i = 0; i < 16; i++)
2917 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2918
2919 tch_amr_reassemble(tch_data, d, 118);
2920
2921 len = 15;
2922
2923 break;
2924 case 1: /* TCH/AHS5.15 */
2925 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2926 conv, n_errors, n_bits_total);
2927
2928 tch_amr_unmerge(d, p, conv, 91, 49);
2929
2930 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2931 if (rv) {
2932 /* Error checking CRC8 for an AMR 5.15 frame */
2933 return -1;
2934 }
2935
2936 for (i = 0; i < 12; i++)
2937 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2938
2939 tch_amr_reassemble(tch_data, d, 103);
2940
2941 len = 13;
2942
2943 break;
2944 case 0: /* TCH/AHS4.75 */
2945 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2946 conv, n_errors, n_bits_total);
2947
2948 tch_amr_unmerge(d, p, conv, 83, 39);
2949
2950 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2951 if (rv) {
2952 /* Error checking CRC8 for an AMR 4.75 frame */
2953 return -1;
2954 }
2955
2956 for (i = 0; i < 12; i++)
2957 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2958
2959 tch_amr_reassemble(tch_data, d, 95);
2960
2961 len = 12;
2962
2963 break;
2964 default:
2965 /* Unknown frame type */
2966 *n_bits_total = 159;
2967 *n_errors = *n_bits_total;
2968 return -1;
2969 }
2970
Philipp Maier898c9c62020-02-06 14:25:01 +01002971out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002972 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002973 if (id != -1) {
2974 if (codec_mode_req)
2975 *cmr = id;
2976 else
2977 *ft = id;
2978 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002979
2980 return len;
2981}
2982
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002983/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002984 * \param[out] bursts caller-allocated output buffer for bursts bits
2985 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002986 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002987 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2988 * \param[in] codec Array of codecs (active codec set)
2989 * \param[in] codecs Number of entries in \a codec
2990 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2991 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2992 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002993int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002994 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002995 uint8_t cmr)
2996{
2997 ubit_t iB[912], cB[456], h;
2998 ubit_t d[244], p[6], conv[135];
2999 int i;
3000 uint8_t id;
3001
3002 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
3003 _xcch_encode_cB(cB, tch_data);
3004
3005 h = 1;
3006
3007 gsm0503_tch_fr_interleave(cB, iB);
3008
3009 for (i = 0; i < 6; i++)
3010 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
3011 &h, i >> 2);
3012 for (i = 2; i < 4; i++)
3013 gsm0503_tch_burst_map(&iB[i * 114 + 456],
3014 &bursts[i * 116], &h, 1);
3015
3016 return 0;
3017 }
3018
3019 h = 0;
3020
Vadim Yanitskiya9952692023-11-10 03:31:55 +07003021 id = codec_mode_req ? cmr : ft;
3022 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit)))
3023 return -1;
3024 if (OSMO_UNLIKELY(ft >= codecs))
3025 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003026
3027 switch (codec[ft]) {
3028 case 5: /* TCH/AHS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003029 if (!len) {
3030 /* No data, induce BFI in the receiver by inverted CRC bits.
3031 * The data bit are all 0, so the correct parity bits would be 111111. */
3032 memset(d, 0, 159);
3033 memset(p, 0, 6);
3034 } else {
3035 if (len != 20)
3036 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003037
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003038 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003039
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003040 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
3041 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003042
3043 tch_amr_merge(conv, d, p, 123, 67);
3044
3045 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
3046
3047 memcpy(cB + 192, d + 123, 36);
3048
3049 break;
3050 case 4: /* TCH/AHS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003051 if (!len) {
3052 /* See comment above. */
3053 memset(d, 0, 148);
3054 memset(p, 0, 6);
3055 } else {
3056 if (len != 19)
3057 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003058
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003059 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003060
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003061 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
3062 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003063
3064 tch_amr_merge(conv, d, p, 120, 61);
3065
3066 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
3067
3068 memcpy(cB + 200, d + 120, 28);
3069
3070 break;
3071 case 3: /* TCH/AHS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003072 if (!len) {
3073 /* See comment above. */
3074 memset(d, 0, 134);
3075 memset(p, 0, 6);
3076 } else {
3077 if (len != 17)
3078 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003079
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003080 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003081
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003082 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3083 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003084
3085 tch_amr_merge(conv, d, p, 110, 55);
3086
3087 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
3088
3089 memcpy(cB + 204, d + 110, 24);
3090
3091 break;
3092 case 2: /* TCH/AHS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003093 if (!len) {
3094 /* See comment above. */
3095 memset(d, 0, 118);
3096 memset(p, 0, 6);
3097 } else {
3098 if (len != 15)
3099 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003100
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003101 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003102
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003103 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3104 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003105
3106 tch_amr_merge(conv, d, p, 102, 55);
3107
3108 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
3109
3110 memcpy(cB + 212, d + 102, 16);
3111
3112 break;
3113 case 1: /* TCH/AHS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003114 if (!len) {
3115 /* See comment above. */
3116 memset(d, 0, 103);
3117 memset(p, 0, 6);
3118 } else {
3119 if (len != 13)
3120 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003121
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003122 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003123
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003124 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
3125 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003126
3127 tch_amr_merge(conv, d, p, 91, 49);
3128
3129 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
3130
3131 memcpy(cB + 216, d + 91, 12);
3132
3133 break;
3134 case 0: /* TCH/AHS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003135 if (!len) {
3136 /* See comment above. */
3137 memset(d, 0, 95);
3138 memset(p, 0, 6);
3139 } else {
3140 if (len != 12)
3141 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003142
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003143 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003144
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003145 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
3146 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003147
3148 tch_amr_merge(conv, d, p, 83, 39);
3149
3150 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
3151
3152 memcpy(cB + 216, d + 83, 12);
3153
3154 break;
3155 default:
3156 /* FIXME: FT %ft is not supported */
3157 return -1;
3158 }
3159
Philipp Maiercfea39b2021-08-31 16:02:31 +02003160 memcpy(cB, gsm0503_ahs_ic_ubit[id], 4);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003161
3162 gsm0503_tch_hr_interleave(cB, iB);
3163
3164 for (i = 0; i < 4; i++)
3165 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
3166
3167 return 0;
3168
3169invalid_length:
3170 /* FIXME: payload length %len does not comply with codec type %ft */
3171 return -1;
3172}
3173
3174/*
3175 * GSM RACH transcoding
3176 */
3177
3178/*
3179 * GSM RACH apply BSIC to parity
3180 *
3181 * p(j) = p(j) xor b(j) j = 0, ..., 5
3182 * b(0) = MSB of PLMN colour code
3183 * b(5) = LSB of BS colour code
3184 */
Max32e56412017-10-16 14:58:00 +02003185static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003186{
3187 int i;
3188
3189 /* Apply it */
3190 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02003191 d[start + i] ^= ((bsic >> (5 - i)) & 1);
3192}
3193
Harald Welte6950b192018-02-26 11:48:00 +01003194static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
3195 int *n_errors, int *n_bits_total)
Max32e56412017-10-16 14:58:00 +02003196{
3197 ubit_t conv[17];
3198 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
3199 int rv;
3200
Harald Welte6950b192018-02-26 11:48:00 +01003201 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
3202 n_errors, n_bits_total);
Max32e56412017-10-16 14:58:00 +02003203
3204 rach_apply_bsic(conv, bsic, nbits);
3205
3206 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3207 if (rv)
3208 return -1;
3209
3210 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
3211
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003212 return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0];
Max32e56412017-10-16 14:58:00 +02003213}
3214
3215/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3216 * \param[out] ra output buffer for RACH data
3217 * \param[in] burst Input burst data
3218 * \param[in] bsic BSIC used in this cell
3219 * \returns 0 on success; negative on error (e.g. CRC error) */
3220int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
3221{
Harald Welte6950b192018-02-26 11:48:00 +01003222 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
Max32e56412017-10-16 14:58:00 +02003223
3224 if (r < 0)
3225 return r;
3226
3227 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003228
3229 return 0;
3230}
3231
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003232/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003233 * \param[out] ra output buffer for RACH data
3234 * \param[in] burst Input burst data
3235 * \param[in] bsic BSIC used in this cell
3236 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003237int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003238{
Harald Welte6950b192018-02-26 11:48:00 +01003239 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
3240 if (r < 0)
3241 return r;
3242
3243 *ra = r;
3244 return 0;
3245}
3246
3247/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3248 * \param[out] ra output buffer for RACH data
3249 * \param[in] burst Input burst data
3250 * \param[in] bsic BSIC used in this cell
3251 * \param[out] n_errors Number of detected bit errors
3252 * \param[out] n_bits_total Total number of bits
3253 * \returns 0 on success; negative on error (e.g. CRC error) */
3254int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
3255 int *n_errors, int *n_bits_total)
3256{
3257 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
3258 if (r < 0)
3259 return r;
3260
3261 *ra = r;
3262 return 0;
3263}
3264
3265/*! Decode the (8-bit) RACH according to TS 05.03
3266 * \param[out] ra output buffer for RACH data
3267 * \param[in] burst Input burst data
3268 * \param[in] bsic BSIC used in this cell
3269 * \param[out] n_errors Number of detected bit errors
3270 * \param[out] n_bits_total Total number of bits
3271 * \returns 0 on success; negative on error (e.g. CRC error) */
3272int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
3273 int *n_errors, int *n_bits_total)
3274{
3275 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003276
Max32e56412017-10-16 14:58:00 +02003277 if (r < 0)
3278 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003279
Max32e56412017-10-16 14:58:00 +02003280 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003281
3282 return 0;
3283}
3284
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003285/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003286 * \param[out] burst Caller-allocated output burst buffer
3287 * \param[in] ra Input RACH data
3288 * \param[in] bsic BSIC used in this cell
3289 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003290int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003291{
Max32e56412017-10-16 14:58:00 +02003292 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
3293}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003294
Max32e56412017-10-16 14:58:00 +02003295/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
3296 * \param[out] burst Caller-allocated output burst buffer
3297 * \param[in] ra11 Input RACH data
3298 * \param[in] bsic BSIC used in this cell
3299 * \param[in] is_11bit whether given RA is 11 bit or not
3300 * \returns 0 on success; negative on error */
3301int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
3302{
3303 ubit_t conv[17];
3304 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003305
Max32e56412017-10-16 14:58:00 +02003306 if (is_11bit) {
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003307 ra[0] = (uint8_t) (ra11 >> 3);
3308 ra[1] = (uint8_t) (ra11 & 0x07);
Max32e56412017-10-16 14:58:00 +02003309 nbits = 11;
3310 } else
3311 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003312
Max32e56412017-10-16 14:58:00 +02003313 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003314
Max32e56412017-10-16 14:58:00 +02003315 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3316
3317 rach_apply_bsic(conv, bsic, nbits);
3318
3319 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003320
3321 return 0;
3322}
3323
3324/*
3325 * GSM SCH transcoding
3326 */
Harald Weltec6636782017-06-12 14:59:37 +02003327
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003328/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003329 * \param[out] sb_info output buffer for SCH data
3330 * \param[in] burst Input burst data
3331 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003332int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003333{
3334 ubit_t conv[35];
3335 int rv;
3336
3337 osmo_conv_decode(&gsm0503_sch, burst, conv);
3338
3339 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3340 if (rv)
3341 return -1;
3342
3343 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
3344
3345 return 0;
3346}
3347
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003348/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003349 * \param[out] burst Caller-allocated output burst buffer
3350 * \param[in] sb_info Input SCH data
3351 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003352int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003353{
3354 ubit_t conv[35];
3355
3356 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
3357
3358 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3359
3360 osmo_conv_encode(&gsm0503_sch, conv, burst);
3361
3362 return 0;
3363}
Harald Weltec6636782017-06-12 14:59:37 +02003364
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003365/*
3366 * GSM CSD transcoding
3367 */
3368
3369static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB)
3370{
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003371 unsigned int i;
3372
3373 /* hu(B): copy *even* numbered bits if not stolen by FACCH */
3374 if (burst[58] == 0) {
3375 for (i = 0; i < 57; i += 2)
3376 burst[i] |= iB[i];
3377 for (i = 58; i < 114; i += 2)
3378 burst[i + 2] |= iB[i];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003379 }
3380
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003381 /* hl(B): copy *odd* numbered bits if not stolen by FACCH */
3382 if (burst[57] == 0) {
3383 for (i = 1; i < 57; i += 2)
3384 burst[i] |= iB[i];
3385 for (i = 57; i < 114; i += 2)
3386 burst[i + 2] |= iB[i];
3387 }
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003388}
3389
3390/*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3.
3391 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3392 * 22 * 2 * 58 == 2552 bits total.
3393 * \param[in] data Data to be encoded (240 unpacked bits).
3394 * \returns 0 in case of success; negative on error. */
3395int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data)
3396{
3397 ubit_t iB[22 * 114], cB[4 * 114];
3398 ubit_t conv[4 * 60 + 4];
3399
3400 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3401 memcpy(&conv[0], &data[0], 4 * 60);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003402 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003403
3404 /* 3.3.3 Convolutional encoder */
3405 osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]);
3406
3407 /* 3.3.4 Interleaving */
3408 memset(&iB[0], 0, sizeof(iB));
3409 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3410
3411 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3412 for (unsigned int i = 0; i < 22; i++)
3413 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3414
3415 return 0;
3416}
3417
3418/*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3.
3419 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3420 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3421 * 22 * 2 * 58 == 2552 bits total.
3422 * \param[out] n_errors Number of detected bit errors.
3423 * \param[out] n_bits_total Total number of bits.
3424 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3425int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts,
3426 int *n_errors, int *n_bits_total)
3427{
3428 sbit_t iB[22 * 114], cB[4 * 114];
3429 ubit_t conv[4 * 60 + 4];
3430
3431 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3432 for (unsigned int i = 0; i < 22; i++) {
3433 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3434 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3435 }
3436
3437 /* 3.3.4 Interleaving */
3438 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3439
3440 /* 3.3.3 Convolutional encoder */
3441 osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total);
3442
3443 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3444 memcpy(&data[0], &conv[0], 4 * 60);
3445
3446 return 4 * 60;
3447}
3448
3449/*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4.
3450 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3451 * 22 * 2 * 58 == 2552 bits total.
3452 * \param[in] data Data to be encoded (120 unpacked bits).
3453 * \returns 0 in case of success; negative on error */
3454int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data)
3455{
3456 ubit_t iB[22 * 114], cB[4 * 114];
3457 ubit_t conv[2 * 60 + 32];
3458
3459 /* 3.4.2 Block code:
3460 *
3461 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3462 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3463 *
3464 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3465 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3466 *
3467 * Two such blocks forming a block of 152 bits: u1 + u2. */
3468 for (unsigned int k = 0; k < 2 * 4; k++) {
3469 memcpy(&conv[19 * k], &data[15 * k], 15);
3470 memset(&conv[19 * k + 15], 0, 4);
3471 }
3472
3473 /* 3.4.3 Convolutional encoder */
3474 osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]);
3475
3476 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3477 memset(&iB[0], 0, sizeof(iB));
3478 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3479
3480 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3481 for (unsigned int i = 0; i < 22; i++)
3482 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3483
3484 return 0;
3485}
3486
3487/*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4.
3488 * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits).
3489 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3490 * 22 * 2 * 58 == 2552 bits total.
3491 * \param[out] n_errors Number of detected bit errors.
3492 * \param[out] n_bits_total Total number of bits.
3493 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3494int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts,
3495 int *n_errors, int *n_bits_total)
3496{
3497 sbit_t iB[22 * 114], cB[4 * 114];
3498 ubit_t conv[2 * 60 + 32];
3499
3500 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3501 for (unsigned int i = 0; i < 22; i++) {
3502 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3503 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3504 }
3505
3506 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3507 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3508
3509 /* 3.4.3 Convolutional encoder */
3510 osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total);
3511
3512 /* 3.4.2 Block code:
3513 *
3514 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3515 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3516 *
3517 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3518 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3519 *
3520 * Two such blocks forming a block of 152 bits: u1 + u2. */
3521 for (unsigned int k = 0; k < 2 * 4; k++)
3522 memcpy(&data[15 * k], &conv[19 * k], 15);
3523
3524 return 2 * 60;
3525}
3526
3527/*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5.
3528 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3529 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3530 * 22 * 2 * 58 == 2552 bits total.
3531 * \param[in] data Data to be encoded (240 unpacked bits).
3532 * \returns 0 in case of success; negative on error */
3533int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data)
3534{
3535 return gsm0503_tch_fr96_encode(bursts, data);
3536}
3537
3538/*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5.
3539 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3540 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3541 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3542 * 22 * 2 * 58 == 2552 bits total.
3543 * \param[out] n_errors Number of detected bit errors.
3544 * \param[out] n_bits_total Total number of bits.
3545 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3546int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts,
3547 int *n_errors, int *n_bits_total)
3548{
3549 return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total);
3550}
3551
Vadim Yanitskiyfadda012023-07-08 06:30:55 +07003552/*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6.
3553 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3554 * 8 * 2 * 58 == 928 bits total.
3555 * \param[in] data Data to be encoded (72 unpacked bits).
3556 * \returns 0 in case of success; negative on error */
3557int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data)
3558{
3559 ubit_t iB[8 * 114], cB[4 * 114];
3560 const ubit_t h = 0;
3561
3562 /* 3.6.{1-3} Block code and Convolutional encoder */
3563 osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]);
3564
3565 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3566 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3567
3568 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3569 for (unsigned int i = 0; i < 8; i++)
3570 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3571
3572 return 0;
3573}
3574
3575/*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6.
3576 * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits).
3577 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3578 * 8 * 2 * 58 == 928 bits total.
3579 * \param[out] n_errors Number of detected bit errors.
3580 * \param[out] n_bits_total Total number of bits.
3581 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3582int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts,
3583 int *n_errors, int *n_bits_total)
3584{
3585 sbit_t iB[8 * 114], cB[4 * 114];
3586
3587 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3588 for (unsigned int i = 0; i < 8; i++)
3589 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3590
3591 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3592 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3593
3594 /* 3.6.{1-3} Block code and Convolutional encoder */
3595 osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total);
3596
3597 return 72;
3598}
3599
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003600/*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7.
3601 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3602 * 22 * 2 * 58 == 2552 bits total.
3603 * \param[in] data Data to be encoded (144 unpacked bits).
3604 * \returns 0 in case of success; negative on error */
3605int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data)
3606{
3607 ubit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003608
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003609 /* 3.7.{1-3} Block code and Convolutional encoder */
3610 osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]);
3611 osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]);
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003612
3613 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3614 memset(&iB[0], 0, sizeof(iB));
3615 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3616
3617 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3618 for (unsigned int i = 0; i < 22; i++)
3619 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3620
3621 return 0;
3622}
3623
3624/*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7.
3625 * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits).
3626 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3627 * 22 * 2 * 58 == 2552 bits total.
3628 * \param[out] n_errors Number of detected bit errors.
3629 * \param[out] n_bits_total Total number of bits.
3630 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3631int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts,
3632 int *n_errors, int *n_bits_total)
3633{
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003634 int n_errors_l[2], n_bits_total_l[2];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003635 sbit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003636
3637 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3638 for (unsigned int i = 0; i < 22; i++) {
3639 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3640 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3641 }
3642
3643 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3644 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3645
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003646 /* 3.7.{1-3} Block code and Convolutional encoder */
3647 osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]);
3648 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 +07003649
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003650 if (n_errors)
3651 *n_errors = n_errors_l[0] + n_errors_l[1];
3652
3653 if (n_bits_total)
3654 *n_bits_total = n_bits_total_l[0] + n_bits_total_l[1];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003655
3656 return 2 * 72;
3657}
3658
3659/*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8.
3660 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3661 * 22 * 2 * 58 == 2552 bits total.
3662 * \param[in] data Data to be encoded (290 unpacked bits).
3663 * \returns 0 in case of success; negative on error */
3664int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data)
3665{
3666 ubit_t iB[22 * 114], cB[4 * 114];
3667 ubit_t conv[290 + 4];
3668
3669 /* 3.8.2 Block code: b(290) + pad(4) */
3670 memcpy(&conv[0], &data[0], 290);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003671 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003672
3673 /* 3.8.3 Convolutional encoder */
3674 osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]);
3675
3676 /* 3.8.4 Interleaving */
3677 memset(&iB[0], 0, sizeof(iB));
3678 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3679
3680 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3681 for (unsigned int i = 0; i < 22; i++)
3682 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3683
3684 return 0;
3685}
3686
3687/*! Perform channel decoding of a TCH/14.4 channel as per section 3.8.
3688 * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits).
3689 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3690 * 22 * 2 * 58 == 2552 bits total.
3691 * \param[out] n_errors Number of detected bit errors.
3692 * \param[out] n_bits_total Total number of bits.
3693 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3694int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts,
3695 int *n_errors, int *n_bits_total)
3696{
3697 sbit_t iB[22 * 114], cB[4 * 114];
3698 ubit_t conv[294];
3699
3700 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3701 for (unsigned int i = 0; i < 22; i++) {
3702 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3703 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3704 }
3705
3706 /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3707 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3708
3709 /* 3.8.3 Convolutional encoder */
3710 osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total);
3711
3712 /* 3.8.2 Block code: b(290) + pad(4) */
3713 memcpy(&data[0], &conv[0], 290);
3714
3715 return 290;
3716}
3717
Vadim Yanitskiyb3340222023-05-26 01:07:18 +07003718/*
3719 * FACCH/[FH] transcoding
3720 */
3721
3722/*! Perform channel encoding of a FACCH/F data as per section 4.2.
3723 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3724 * 8 * 2 * 58 == 928 bits total.
3725 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3726 * \returns 0 in case of success; negative on error */
3727int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data)
3728{
3729 ubit_t iB[8 * 114], cB[4 * 114];
3730 const ubit_t h = 1;
3731
3732 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3733 _xcch_encode_cB(&cB[0], &data[0]);
3734
3735 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3736 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3737
3738 /* 4.2.5 Mapping on a Burst:
3739 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3740 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3741 for (unsigned int i = 0; i < 8; i++)
3742 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3743
3744 return 0;
3745}
3746
3747/*! Perform channel decoding of a FACCH/F data as per section 4.2.
3748 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3749 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3750 * 8 * 2 * 58 == 928 bits total.
3751 * \param[out] n_errors Number of detected bit errors.
3752 * \param[out] n_bits_total Total number of bits.
3753 * \returns Number of bytes used in the output buffer; negative on error. */
3754int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts,
3755 int *n_errors, int *n_bits_total)
3756{
3757 sbit_t iB[8 * 114], cB[4 * 114];
3758 int steal = 0;
3759
3760 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3761 for (unsigned int i = 0; i < 4; i++)
3762 steal -= bursts[i * 116 + 58]; /* hu(B) */
3763 for (unsigned int i = 4; i < 8; i++)
3764 steal -= bursts[i * 116 + 57]; /* hl(B) */
3765 if (steal <= 0)
3766 return -1;
3767
3768 /* 4.2.5 Mapping on a Burst:
3769 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3770 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3771 for (unsigned int i = 0; i < 8; i++)
3772 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3773
3774 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3775 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3776
3777 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3778 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3779 return -1;
3780
3781 return GSM_MACBLOCK_LEN;
3782}
3783
3784/*! Perform channel encoding of a FACCH/H data as per section 4.3.
3785 * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts,
3786 * 6 * 2 * 58 == 696 bits total.
3787 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3788 * \returns 0 in case of success; negative on error */
3789int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data)
3790{
3791 ubit_t iB[8 * 114], cB[4 * 114];
3792 const ubit_t h = 1;
3793
3794 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3795 _xcch_encode_cB(&cB[0], &data[0]);
3796
3797 /* 4.3.4 Interleaving */
3798 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3799
3800 /* 4.3.5 Mapping on a Burst:
3801 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3802 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3803 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3804 for (unsigned int i = 0; i < 6; i++)
3805 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3806 for (unsigned int i = 2; i < 4; i++)
3807 gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1);
3808
3809 return 0;
3810}
3811
3812/*! Perform channel decoding of a FACCH/H data as per section 4.3.
3813 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3814 * \param[in] bursts Buffer containing the symbols of 6 bursts,
3815 * 6 * 2 * 58 == 696 bits total.
3816 * \param[out] n_errors Number of detected bit errors.
3817 * \param[out] n_bits_total Total number of bits.
3818 * \returns Number of bytes used in the output buffer; negative on error. */
3819int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts,
3820 int *n_errors, int *n_bits_total)
3821{
3822 sbit_t iB[8 * 114], cB[4 * 114];
3823 int steal = 0;
3824
3825 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3826 for (unsigned int i = 0; i < 4; i++)
3827 steal -= bursts[i * 116 + 58]; /* hu(B) */
3828 for (unsigned int i = 2; i < 6; i++)
3829 steal -= bursts[i * 116 + 57]; /* hl(B) */
3830 if (steal <= 0)
3831 return -1;
3832
3833 /* 4.3.5 Mapping on a Burst:
3834 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3835 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3836 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3837 for (unsigned int i = 0; i < 6; i++)
3838 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3839 for (unsigned int i = 2; i < 4; i++)
3840 gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1);
3841
3842 /* 4.3.4 Interleaving */
3843 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3844
3845 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3846 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3847 return -1;
3848
3849 return GSM_MACBLOCK_LEN;
3850}
3851
Harald Weltec6636782017-06-12 14:59:37 +02003852/*! @} */