blob: 612e0ad0ab1fe789859f8606b487ad694b4485cd [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
924 * \param usf_p unused argument ?!?
925 * \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
1013 * \param[out] usf_p uplink stealing flag
1014 * \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
1045 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
1046 conv, 184, conv + 184);
1047 if (rv)
1048 return -1;
1049
1050 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
1051
1052 return 23;
1053 case 2:
1054 for (i = 587, j = 455; i >= 0; i--) {
1055 if (!gsm0503_puncture_cs2[i])
1056 cB[i] = cB[j--];
1057 else
1058 cB[i] = 0;
1059 }
1060
1061 osmo_conv_decode_ber(&gsm0503_cs2_np, cB,
1062 conv, n_errors, n_bits_total);
1063
1064 for (i = 0; i < 8; i++) {
1065 for (j = 0, k = 0; j < 6; j++)
1066 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1067
1068 if (i == 0 || k < best) {
1069 best = k;
1070 usf = i;
1071 }
1072 }
1073
1074 conv[3] = usf & 1;
1075 conv[4] = (usf >> 1) & 1;
1076 conv[5] = (usf >> 2) & 1;
1077 if (usf_p)
1078 *usf_p = usf;
1079
1080 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1081 conv + 3, 271, conv + 3 + 271);
1082 if (rv)
1083 return -1;
1084
1085 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1086
1087 return 34;
1088 case 3:
1089 for (i = 675, j = 455; i >= 0; i--) {
1090 if (!gsm0503_puncture_cs3[i])
1091 cB[i] = cB[j--];
1092 else
1093 cB[i] = 0;
1094 }
1095
1096 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
1097 conv, n_errors, n_bits_total);
1098
1099 for (i = 0; i < 8; i++) {
1100 for (j = 0, k = 0; j < 6; j++)
1101 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1102
1103 if (i == 0 || k < best) {
1104 best = k;
1105 usf = i;
1106 }
1107 }
1108
1109 conv[3] = usf & 1;
1110 conv[4] = (usf >> 1) & 1;
1111 conv[5] = (usf >> 2) & 1;
1112 if (usf_p)
1113 *usf_p = usf;
1114
1115 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1116 conv + 3, 315, conv + 3 + 315);
1117 if (rv)
1118 return -1;
1119
1120 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
1121
1122 return 40;
1123 case 4:
1124 for (i = 12; i < 456; i++)
1125 conv[i] = (cB[i] < 0) ? 1 : 0;
1126
1127 for (i = 0; i < 8; i++) {
1128 for (j = 0, k = 0; j < 12; j++)
1129 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1130
1131 if (i == 0 || k < best) {
1132 best = k;
1133 usf = i;
1134 }
1135 }
1136
1137 conv[9] = usf & 1;
1138 conv[10] = (usf >> 1) & 1;
1139 conv[11] = (usf >> 2) & 1;
1140 if (usf_p)
1141 *usf_p = usf;
1142
1143 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1144 conv + 9, 431, conv + 9 + 431);
1145 if (rv) {
1146 *n_bits_total = 456 - 12;
1147 *n_errors = *n_bits_total;
1148 return -1;
1149 }
1150
1151 *n_bits_total = 456 - 12;
1152 *n_errors = 0;
1153
1154 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1155
1156 return 54;
1157 default:
1158 *n_bits_total = 0;
1159 *n_errors = 0;
1160 break;
1161 }
1162
1163 return -1;
1164}
1165
1166/*
Pau Espin Pedrolf62f0732020-04-07 13:12:11 +02001167 * EGPRS PDTCH DL block encoding
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001168 */
Harald Welteb9946d32017-06-12 09:40:16 +02001169static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001170{
1171 int i;
1172 ubit_t iB[456];
1173 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1174
Pau Espin Pedrol63ebc362020-04-07 13:15:36 +02001175 gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001176
1177 for (i = 0; i < 4; i++) {
1178 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1179 hl_hn + i * 2, hl_hn + i * 2 + 1);
1180 }
1181
1182 return 0;
1183}
1184
Harald Welteb9946d32017-06-12 09:40:16 +02001185static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001186{
1187 int i;
1188 const ubit_t *up;
1189 ubit_t hi[EGPRS_HDR_HC_MAX];
1190 ubit_t di[EGPRS_DATA_DC_MAX];
1191
1192 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1193 up = gsm0503_mcs5_usf_precode_table[usf];
1194
1195 for (i = 0; i < 4; i++) {
1196 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1197 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1198 }
1199
1200 return 0;
1201}
1202
Harald Welteb9946d32017-06-12 09:40:16 +02001203static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1204 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001205{
1206 int i;
1207 const ubit_t *up;
1208 ubit_t hi[EGPRS_HDR_HC_MAX];
1209 ubit_t di[EGPRS_DATA_C1 * 2];
1210
1211 if (mcs == EGPRS_MCS7)
1212 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1213 else
1214 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1215
1216 up = gsm0503_mcs5_usf_precode_table[usf];
1217
1218 for (i = 0; i < 4; i++) {
1219 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1220 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1221 }
1222
1223 return 0;
1224}
1225
Harald Welteb9946d32017-06-12 09:40:16 +02001226static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001227{
1228 int i, j;
1229 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001230 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001231
1232 code = &gsm0503_mcs_dl_codes[mcs];
1233
1234 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1235 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1236 code->hdr_len, upp + code->hdr_len);
1237
1238 osmo_conv_encode(code->hdr_conv, upp, C);
1239
1240 /* MCS-5,6 header direct puncture instead of table */
1241 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1242 memcpy(hc, C, code->hdr_code_len);
1243 hc[99] = hc[98];
1244 return 0;
1245 }
1246
1247 if (!code->hdr_punc) {
1248 /* Invalid MCS-X header puncture matrix */
1249 return -1;
1250 }
1251
1252 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1253 if (!code->hdr_punc[i])
1254 hc[j++] = C[i];
1255 }
1256
1257 return 0;
1258}
1259
Harald Welteb9946d32017-06-12 09:40:16 +02001260static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001261 int mcs, int p, int blk)
1262{
1263 int i, j, data_len;
1264 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001265 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001266
1267 code = &gsm0503_mcs_dl_codes[mcs];
1268
1269 /*
1270 * Dual block - MCS-7,8,9
1271 * Single block - MCS-1,2,3,4,5,6
1272 */
1273 if (mcs >= EGPRS_MCS7)
1274 data_len = code->data_len / 2;
1275 else
1276 data_len = code->data_len;
1277
1278 osmo_pbit2ubit_ext(u, 0, l2_data,
1279 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1280
1281 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1282
1283 osmo_conv_encode(code->data_conv, u, C);
1284
1285 if (!code->data_punc[p]) {
1286 /* Invalid MCS-X data puncture matrix */
1287 return -1;
1288 }
1289
1290 for (i = 0, j = 0; i < code->data_code_len; i++) {
1291 if (!code->data_punc[p][i])
1292 c[j++] = C[i];
1293 }
1294
1295 return 0;
1296}
1297
1298/*
1299 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1300 *
1301 * Type 1 - MCS-7,8,9
1302 * Type 2 - MCS-5,6
1303 * Type 3 - MCS-1,2,3,4
1304 */
1305static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001306 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001307{
1308 uint8_t bits;
1309
1310 switch (type) {
1311 case EGPRS_HDR_TYPE1:
1312 bits = hdr->type1.cps;
1313 break;
1314 case EGPRS_HDR_TYPE2:
1315 bits = hdr->type2.cps;
1316 break;
1317 case EGPRS_HDR_TYPE3:
1318 bits = hdr->type3.cps;
1319 break;
1320 default:
1321 return -1;
1322 }
1323
1324 return egprs_get_cps(cps, type, bits);
1325}
1326
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001327/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001328 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1329 * \param[in] l2_data L2 (MAC) block to be encoded
1330 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001331 * \returns number of bits encoded; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001332int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001333 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001334{
1335 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1336 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1337 uint8_t mcs;
1338 struct egprs_cps cps;
1339 union gprs_rlc_dl_hdr_egprs *hdr;
1340
1341 switch (l2_len) {
1342 case 27:
1343 mcs = EGPRS_MCS1;
1344 break;
1345 case 33:
1346 mcs = EGPRS_MCS2;
1347 break;
1348 case 42:
1349 mcs = EGPRS_MCS3;
1350 break;
1351 case 49:
1352 mcs = EGPRS_MCS4;
1353 break;
1354 case 60:
1355 mcs = EGPRS_MCS5;
1356 break;
1357 case 78:
1358 mcs = EGPRS_MCS6;
1359 break;
1360 case 118:
1361 mcs = EGPRS_MCS7;
1362 break;
1363 case 142:
1364 mcs = EGPRS_MCS8;
1365 break;
1366 case 154:
1367 mcs = EGPRS_MCS9;
1368 break;
1369 default:
1370 return -1;
1371 }
1372
1373 /* Read header for USF and puncturing matrix selection. */
1374 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1375
1376 switch (mcs) {
1377 case EGPRS_MCS1:
1378 case EGPRS_MCS2:
1379 case EGPRS_MCS3:
1380 case EGPRS_MCS4:
1381 /* Check for valid CPS and matching MCS to message size */
1382 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1383 (cps.mcs != mcs))
1384 goto bad_header;
1385
1386 egprs_encode_hdr(hc, l2_data, mcs);
1387 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1388 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1389 break;
1390 case EGPRS_MCS5:
1391 case EGPRS_MCS6:
1392 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1393 (cps.mcs != mcs))
1394 goto bad_header;
1395
1396 egprs_encode_hdr(hc, l2_data, mcs);
1397 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1398 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1399 break;
1400 case EGPRS_MCS7:
1401 case EGPRS_MCS8:
1402 case EGPRS_MCS9:
1403 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1404 (cps.mcs != mcs))
1405 goto bad_header;
1406
1407 egprs_encode_hdr(hc, l2_data, mcs);
1408 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1409 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1410 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1411 break;
1412 }
1413
1414 return mcs >= EGPRS_MCS5 ?
1415 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1416
1417bad_header:
1418 /* Invalid EGPRS MCS-X header */
1419 return -1;
1420}
1421
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001422/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001423 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1424 * \param[in] l2_data L2 (MAC) block to be encoded
1425 * \param[in] l2_len length of l2_data in bytes, used to determine CS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001426 * \returns number of bits encoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001427int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001428{
1429 ubit_t iB[456], cB[676];
1430 const ubit_t *hl_hn;
1431 ubit_t conv[334];
1432 int i, j, usf;
1433
1434 switch (l2_len) {
1435 case 23:
1436 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1437
1438 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1439
1440 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1441
1442 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1443
1444 break;
1445 case 34:
1446 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1447 usf = l2_data[0] & 0x7;
1448
1449 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1450 271, conv + 3 + 271);
1451
1452 memcpy(conv, gsm0503_usf2six[usf], 6);
1453
1454 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1455
1456 for (i = 0, j = 0; i < 588; i++)
1457 if (!gsm0503_puncture_cs2[i])
1458 cB[j++] = cB[i];
1459
1460 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1461
1462 break;
1463 case 40:
1464 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1465 usf = l2_data[0] & 0x7;
1466
1467 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1468 315, conv + 3 + 315);
1469
1470 memcpy(conv, gsm0503_usf2six[usf], 6);
1471
1472 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1473
1474 for (i = 0, j = 0; i < 676; i++)
1475 if (!gsm0503_puncture_cs3[i])
1476 cB[j++] = cB[i];
1477
1478 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1479
1480 break;
1481 case 54:
1482 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1483 usf = l2_data[0] & 0x7;
1484
1485 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1486 431, cB + 9 + 431);
1487
1488 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1489
1490 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1491
1492 break;
1493 default:
1494 return -1;
1495 }
1496
1497 gsm0503_xcch_interleave(cB, iB);
1498
1499 for (i = 0; i < 4; i++) {
1500 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1501 hl_hn + i * 2, hl_hn + i * 2 + 1);
1502 }
1503
1504 return GSM0503_GPRS_BURSTS_NBITS;
1505}
1506
1507/*
1508 * GSM TCH/F FR/EFR transcoding
1509 */
1510
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001511/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001512 * \param[out] tch_data Codec frame in RTP format
1513 * \param[in] b_bits Codec frame in 'native' format
1514 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001515static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001516 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001517{
1518 int i, j, k, l, o;
1519
1520 tch_data[0] = 0xd << 4;
1521 memset(tch_data + 1, 0, 32);
1522
1523 if (net_order) {
1524 for (i = 0, j = 4; i < 260; i++, j++)
1525 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1526
1527 return;
1528 }
1529
1530 /* reassemble d-bits */
1531 i = 0; /* counts bits */
1532 j = 4; /* counts output bits */
1533 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1534 l = 0; /* counts element bits */
1535 o = 0; /* offset input bits */
1536 while (i < 260) {
1537 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1538 if (--k < 0) {
1539 o += gsm0503_gsm_fr_map[l];
1540 k = gsm0503_gsm_fr_map[++l]-1;
1541 }
1542 i++;
1543 j++;
1544 }
1545}
1546
1547static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001548 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001549{
1550 int i, j, k, l, o;
1551
1552 if (net_order) {
1553 for (i = 0, j = 4; i < 260; i++, j++)
1554 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1555
1556 return;
1557 }
1558
1559 i = 0; /* counts bits */
1560 j = 4; /* counts input bits */
1561 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1562 l = 0; /* counts element bits */
1563 o = 0; /* offset output bits */
1564 while (i < 260) {
1565 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1566 if (--k < 0) {
1567 o += gsm0503_gsm_fr_map[l];
1568 k = gsm0503_gsm_fr_map[++l] - 1;
1569 }
1570 i++;
1571 j++;
1572 }
1573}
1574
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001575/* assemble a HR codec frame in the canonical format of ETSI TS 101 318 */
Harald Welteb9946d32017-06-12 09:40:16 +02001576static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001577{
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001578 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001579
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001580 memset(tch_data, 0, GSM_HR_BYTES);
1581 for (i = 0; i < 112; i++)
1582 tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7)));
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001583}
1584
Harald Welteb9946d32017-06-12 09:40:16 +02001585static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001586{
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001587 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001588
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001589 for (i = 0; i < 112; i++)
1590 b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001591}
1592
Harald Weltec6636782017-06-12 14:59:37 +02001593/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001594static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001595{
1596 int i, j;
1597
1598 tch_data[0] = 0xc << 4;
1599 memset(tch_data + 1, 0, 30);
1600
1601 for (i = 0, j = 4; i < 244; i++, j++)
1602 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1603}
1604
Harald Welteb9946d32017-06-12 09:40:16 +02001605static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001606{
1607 int i, j;
1608
1609 for (i = 0, j = 4; i < 244; i++, j++)
1610 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1611}
1612
Harald Weltec6636782017-06-12 14:59:37 +02001613/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001614static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001615{
1616 int i, j;
1617
1618 memset(tch_data, 0, (len + 7) >> 3);
1619
1620 for (i = 0, j = 0; i < len; i++, j++)
1621 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1622}
1623
Harald Welteb9946d32017-06-12 09:40:16 +02001624static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001625{
1626 int i, j;
1627
1628 for (i = 0, j = 0; i < len; i++, j++)
1629 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1630}
1631
Philipp Maier898c9c62020-02-06 14:25:01 +01001632/* Append STI and MI bits to the SID_UPDATE frame, see also
1633 * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */
1634static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi)
1635{
1636 /* Zero out the space that had been used by the CRC14 */
1637 memset(sid_update + 35, 0, 14);
1638
1639 /* Append STI and MI parameters */
1640 sid_update[35] = sti & 1;
1641 sid_update[36] = mi & 1;
1642 sid_update[37] = mi >> 1 & 1;
1643 sid_update[38] = mi >> 2 & 1;
1644}
1645
1646/* Extract a SID UPDATE fram the sbits of an FR AMR frame */
1647static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits)
1648{
1649
1650 unsigned int i;
1651
1652 sbits += 32;
1653
1654 for (i = 0; i < 53; i++) {
1655 sid_update[0] = sbits[0];
1656 sid_update[1] = sbits[1];
1657 sid_update[2] = sbits[2];
1658 sid_update[3] = sbits[3];
1659 sid_update += 4;
1660 sbits += 8;
1661 }
1662
1663}
1664
Harald Weltec6636782017-06-12 14:59:37 +02001665/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001666static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001667{
1668 int i;
1669
1670 for (i = 0; i < 260; i++)
1671 b_bits[gsm610_bitorder[i]] = d_bits[i];
1672}
1673
Harald Weltec6636782017-06-12 14:59:37 +02001674/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001675static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001676{
1677 int i;
1678
1679 for (i = 0; i < 260; i++)
1680 d_bits[i] = b_bits[gsm610_bitorder[i]];
1681}
1682
Harald Weltec6636782017-06-12 14:59:37 +02001683/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001684static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001685{
1686 int i;
1687
1688 const uint16_t *map;
1689
1690 if (!d_bits[93] && !d_bits[94])
1691 map = gsm620_unvoiced_bitorder;
1692 else
1693 map = gsm620_voiced_bitorder;
1694
1695 for (i = 0; i < 112; i++)
1696 b_bits[map[i]] = d_bits[i];
1697}
1698
Harald Weltec6636782017-06-12 14:59:37 +02001699/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001700static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001701{
1702 int i;
1703 const uint16_t *map;
1704
1705 if (!b_bits[34] && !b_bits[35])
1706 map = gsm620_unvoiced_bitorder;
1707 else
1708 map = gsm620_voiced_bitorder;
1709
1710 for (i = 0; i < 112; i++)
1711 d_bits[i] = b_bits[map[i]];
1712}
1713
Harald Weltec6636782017-06-12 14:59:37 +02001714/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001715static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001716{
1717 int i;
1718
1719 for (i = 0; i < 260; i++)
1720 b_bits[gsm660_bitorder[i]] = d_bits[i];
1721}
1722
Harald Weltec6636782017-06-12 14:59:37 +02001723/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001724static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001725{
1726 int i;
1727
1728 for (i = 0; i < 260; i++)
1729 d_bits[i] = b_bits[gsm660_bitorder[i]];
1730}
1731
Harald Weltec6636782017-06-12 14:59:37 +02001732/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001733static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001734{
1735 int i;
1736
1737 for (i = 0; i < 65; i++)
1738 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1739}
1740
Harald Welteb9946d32017-06-12 09:40:16 +02001741static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001742{
1743 int i;
1744
1745 for (i = 0; i < 91; i++) {
1746 d[i << 1] = u[i];
1747 d[(i << 1) + 1] = u[184 - i];
1748 }
1749
1750 for (i = 0; i < 3; i++)
1751 p[i] = u[91 + i];
1752}
1753
Harald Welteb9946d32017-06-12 09:40:16 +02001754static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001755{
1756 int i;
1757
1758 for (i = 0; i < 91; i++) {
1759 u[i] = d[i << 1];
1760 u[184 - i] = d[(i << 1) + 1];
1761 }
1762
1763 for (i = 0; i < 3; i++)
1764 u[91 + i] = p[i];
1765}
1766
Harald Welteb9946d32017-06-12 09:40:16 +02001767static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001768{
1769 memcpy(d, u, 95);
1770 memcpy(p, u + 95, 3);
1771}
1772
Harald Welteb9946d32017-06-12 09:40:16 +02001773static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001774{
1775 memcpy(u, d, 95);
1776 memcpy(u + 95, p, 3);
1777}
1778
Harald Welteb9946d32017-06-12 09:40:16 +02001779static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001780{
1781 memcpy(w, s, 71);
1782 w[71] = w[72] = s[69];
1783 memcpy(w + 73, s + 71, 50);
1784 w[123] = w[124] = s[119];
1785 memcpy(w + 125, s + 121, 53);
1786 w[178] = w[179] = s[172];
1787 memcpy(w + 180, s + 174, 50);
1788 w[230] = w[231] = s[222];
1789 memcpy(w + 232, s + 224, 20);
1790 memcpy(w + 252, p, 8);
1791}
1792
Harald Welteb9946d32017-06-12 09:40:16 +02001793static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001794{
1795 int sum;
1796
1797 memcpy(s, w, 71);
1798 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001799 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001800 memcpy(s + 71, w + 73, 50);
1801 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001802 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001803 memcpy(s + 121, w + 125, 53);
1804 sum = s[172] + w[178] + w[179];
Mychaela N. Falconiaf3e47452023-05-17 18:58:40 +00001805 s[172] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001806 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001807 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001808 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001809 memcpy(s + 224, w + 232, 20);
1810 memcpy(p, w + 252, 8);
1811}
1812
Harald Welteb9946d32017-06-12 09:40:16 +02001813static 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 +07001814{
1815 memcpy(u, d, prot);
1816 memcpy(u + prot, p, 6);
1817 memcpy(u + prot + 6, d + prot, len - prot);
1818}
1819
Harald Welteb9946d32017-06-12 09:40:16 +02001820static 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 +07001821{
1822 memcpy(d, u, prot);
1823 memcpy(p, u + prot, 6);
1824 memcpy(d + prot, u + prot + 6, len - prot);
1825}
1826
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001827/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001828 * \param[out] tch_data Codec frame in RTP payload format
1829 * \param[in] bursts buffer containing the symbols of 8 bursts
1830 * \param[in] net_order FIXME
1831 * \param[in] efr Is this channel using EFR (1) or FR (0)
1832 * \param[out] n_errors Number of detected bit errors
1833 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001834 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001835int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001836 int net_order, int efr, int *n_errors, int *n_bits_total)
1837{
1838 sbit_t iB[912], cB[456], h;
1839 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1840 int i, rv, len, steal = 0;
1841
Harald Weltec6636782017-06-12 14:59:37 +02001842 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001843 for (i = 0; i < 8; i++) {
1844 gsm0503_tch_burst_unmap(&iB[i * 114],
1845 &bursts[i * 116], &h, i >> 2);
1846 steal -= h;
1847 }
Harald Weltec6636782017-06-12 14:59:37 +02001848 /* we now have the bits of the four bursts (interface 4 in
1849 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001850
1851 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001852 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001853
1854 if (steal > 0) {
1855 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1856 if (rv) {
1857 /* Error decoding FACCH frame */
1858 return -1;
1859 }
1860
Vadim Yanitskiyc0305b62023-05-20 16:33:38 +07001861 return GSM_MACBLOCK_LEN;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001862 }
1863
1864 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001865 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001866
Harald Weltec6636782017-06-12 14:59:37 +02001867 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001868 tch_fr_unreorder(d, p, conv);
1869
1870 for (i = 0; i < 78; i++)
1871 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1872
Harald Weltec6636782017-06-12 14:59:37 +02001873 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001874 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1875 if (rv) {
1876 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1877 return -1;
1878 }
1879
1880 if (efr) {
1881 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001882 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001883
1884 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001885 /* we now have the data delivered to the preliminary
1886 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001887
Harald Weltec6636782017-06-12 14:59:37 +02001888 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001889 tch_efr_protected(s, b);
1890
Harald Weltec6636782017-06-12 14:59:37 +02001891 /* perform CRC-8 on 65 most important bits (50 bits of
1892 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001893 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1894 if (rv) {
1895 /* Error checking CRC8 for the EFR part of an EFR frame */
1896 return -1;
1897 }
1898
1899 tch_efr_reassemble(tch_data, s);
1900
1901 len = GSM_EFR_BYTES;
1902 } else {
1903 tch_fr_d_to_b(w, d);
1904
1905 tch_fr_reassemble(tch_data, w, net_order);
1906
1907 len = GSM_FR_BYTES;
1908 }
1909
1910 return len;
1911}
1912
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001913/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001914 * \param[out] bursts caller-allocated output buffer for bursts bits
1915 * \param[in] tch_data Codec input data in RTP payload format
1916 * \param[in] len Length of \a tch_data in bytes
1917 * \param[in] net_order FIXME
1918 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001919int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001920 int len, int net_order)
1921{
1922 ubit_t iB[912], cB[456], h;
1923 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1924 int i;
1925
1926 switch (len) {
1927 case GSM_EFR_BYTES: /* TCH EFR */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001928 tch_efr_disassemble(s, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001929 tch_efr_protected(s, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001930 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001931 tch_efr_reorder(w, s, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001932 tch_efr_w_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001933 goto coding_efr_fr;
1934 case GSM_FR_BYTES: /* TCH FR */
1935 tch_fr_disassemble(w, tch_data, net_order);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001936 tch_fr_b_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001937coding_efr_fr:
1938 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001939 tch_fr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001940 memcpy(cB + 378, d + 182, 78);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001941 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001942 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001943 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00001944 case 0: /* no data, induce BFI in the receiver */
1945 /* Do the same thing that sysmoBTS PHY does when fed a 0-length
1946 * payload for DL: set all u(k) bits to 0, and do the same
1947 * with all class 2 bits. This operation is NOT the same as
1948 * an FR codec frame of all zero bits: with all-zeros d(k) input
1949 * the CRC3 function will produce 111 output, whereas we
1950 * transmit 000 in those parity bits too. The result will be
1951 * an induced BFI (bad frame indication) condition in the
1952 * receiver, for both TCH/FS and TCH/EFS decoders. */
1953 memset(conv, 0, sizeof(conv));
1954 memset(cB + 378, 0, 78);
1955 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1956 h = 0;
1957 break;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001958 case GSM_MACBLOCK_LEN: /* FACCH */
1959 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001960 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001961 break;
1962 default:
1963 return -1;
1964 }
1965
1966 gsm0503_tch_fr_interleave(cB, iB);
1967
1968 for (i = 0; i < 8; i++) {
1969 gsm0503_tch_burst_map(&iB[i * 114],
1970 &bursts[i * 116], &h, i >> 2);
1971 }
1972
1973 return 0;
1974}
1975
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001976/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001977 * \param[out] tch_data Codec frame in TS 101 318 canonical format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07001978 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02001979 * \param[in] odd Odd (1) or even (0) frame number
1980 * \param[out] n_errors Number of detected bit errors
1981 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001982 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001983int gsm0503_tch_hr_decode2(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001984 int *n_errors, int *n_bits_total)
1985{
1986 sbit_t iB[912], cB[456], h;
1987 ubit_t conv[98], b[112], d[112], p[3];
1988 int i, rv, steal = 0;
1989
1990 /* Only unmap the stealing bits */
1991 if (!odd) {
1992 for (i = 0; i < 4; i++) {
1993 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
1994 steal -= h;
1995 }
1996
Mychaela N. Falconia920c0ed2023-05-23 14:06:53 +00001997 for (i = 2; i < 6; i++) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001998 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
1999 steal -= h;
2000 }
2001 }
2002
2003 /* If we found a stole FACCH, but only at correct alignment */
2004 if (steal > 0) {
2005 for (i = 0; i < 6; i++) {
2006 gsm0503_tch_burst_unmap(&iB[i * 114],
2007 &bursts[i * 116], NULL, i >> 2);
2008 }
2009
2010 for (i = 2; i < 4; i++) {
2011 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2012 &bursts[i * 116], NULL, 1);
2013 }
2014
2015 gsm0503_tch_fr_deinterleave(cB, iB);
2016
2017 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2018 if (rv) {
2019 /* Error decoding FACCH frame */
2020 return -1;
2021 }
2022
2023 return GSM_MACBLOCK_LEN;
2024 }
2025
2026 for (i = 0; i < 4; i++) {
2027 gsm0503_tch_burst_unmap(&iB[i * 114],
2028 &bursts[i * 116], NULL, i >> 1);
2029 }
2030
2031 gsm0503_tch_hr_deinterleave(cB, iB);
2032
2033 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
2034
2035 tch_hr_unreorder(d, p, conv);
2036
2037 for (i = 0; i < 17; i++)
2038 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
2039
2040 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2041 if (rv) {
2042 /* Error checking CRC8 for an HR frame */
2043 return -1;
2044 }
2045
2046 tch_hr_d_to_b(b, d);
2047
2048 tch_hr_reassemble(tch_data, b);
2049
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002050 return GSM_HR_BYTES;
2051}
2052
2053/*! Perform channel decoding of a HR(v1) channel according TS 05.03,
2054 * deprecated legacy API.
2055 * \param[out] tch_data Codec frame in pseudo-RFC5993 format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002056 * \param[in] bursts buffer containing the symbols of 6 bursts
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002057 * \param[in] odd Odd (1) or even (0) frame number
2058 * \param[out] n_errors Number of detected bit errors
2059 * \param[out] n_bits_total Total number of bits
2060 * \returns length of bytes used in \a tch_data output buffer; negative on error
2061 *
2062 * The HR1 codec frame format returned by this function is pseudo-RFC5993,
2063 * not true RFC 5993, as there is no SID classification being done
2064 * and the FT bits in the ToC octet are always set to 0 - but this
2065 * arguably-bogus format is the legacy public API.
2066 */
2067int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
2068 int *n_errors, int *n_bits_total)
2069{
2070 int rc;
2071
2072 rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors,
2073 n_bits_total);
2074 if (rc != GSM_HR_BYTES)
2075 return rc;
2076 memmove(tch_data + 1, tch_data, GSM_HR_BYTES);
2077 tch_data[0] = 0x00; /* FT=0, note absence of SID classification */
2078 return GSM_HR_BYTES_RTP_RFC5993;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002079}
2080
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002081/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002082 * \param[out] bursts caller-allocated output buffer for bursts bits
2083 * \param[in] tch_data Codec input data in RTP payload format
2084 * \param[in] len Length of \a tch_data in bytes
2085 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002086int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002087{
2088 ubit_t iB[912], cB[456], h;
2089 ubit_t conv[98], b[112], d[112], p[3];
2090 int i;
2091
2092 switch (len) {
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00002093 case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */
2094 tch_data++;
2095 /* fall-through */
2096 case GSM_HR_BYTES: /* TCH HR in "pure" form */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002097 tch_hr_disassemble(b, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002098 tch_hr_b_to_d(d, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002099 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002100 tch_hr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002101 memcpy(cB + 211, d + 95, 17);
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002102hr_conv_coding:
2103 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002104 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002105 gsm0503_tch_hr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002106 for (i = 0; i < 4; i++) {
2107 gsm0503_tch_burst_map(&iB[i * 114],
2108 &bursts[i * 116], &h, i >> 1);
2109 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002110 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002111 case 0: /* no data, induce BFI in the receiver */
2112 /* see comments in gsm0503_tch_fr_encode() - same deal here */
2113 memset(conv, 0, sizeof(conv));
2114 memset(cB + 211, 0, 17);
2115 goto hr_conv_coding;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002116 case GSM_MACBLOCK_LEN: /* FACCH */
2117 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002118 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002119 gsm0503_tch_fr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002120 for (i = 0; i < 6; i++) {
2121 gsm0503_tch_burst_map(&iB[i * 114],
2122 &bursts[i * 116], &h, i >> 2);
2123 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002124 for (i = 2; i < 4; i++) {
2125 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2126 &bursts[i * 116], &h, 1);
2127 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002128 break;
2129 default:
2130 return -1;
2131 }
2132
2133 return 0;
2134}
2135
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002136/* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2137static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB)
2138{
2139 unsigned int id = 0, best = 0;
2140 unsigned int i, j, k;
2141
2142 for (i = 0; i < 4; i++) {
2143 /* FIXME: why not using remaining (16 - 8) soft-bits here? */
2144 for (j = 0, k = 0; j < 8; j++)
2145 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2146
2147 if (i == 0 || k < best) {
2148 best = k;
2149 id = i;
2150 }
2151 }
2152
2153 return id;
2154}
2155
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002156/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002157 * \param[out] tch_data Codec frame in RTP payload format
2158 * \param[in] bursts buffer containing the symbols of 8 bursts
2159 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2160 * \param[in] codec array of active codecs (active codec set)
2161 * \param[in] codecs number of codecs in \a codec
2162 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2163 * \param[out] cmr Output in \a codec_mode_req = 1
2164 * \param[out] n_errors Number of detected bit errors
2165 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002166 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2167 * codec out of range; negative on error
2168 */
Harald Welteb9946d32017-06-12 09:40:16 +02002169int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002170 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2171 uint8_t *cmr, int *n_errors, int *n_bits_total)
2172{
Philipp Maier898c9c62020-02-06 14:25:01 +01002173 return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req,
2174 codec, codecs, ft, cmr, n_errors,
2175 n_bits_total, NULL);
2176}
2177
2178/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2179 * \param[out] tch_data Codec frame in RTP payload format
2180 * \param[in] bursts buffer containing the symbols of 8 bursts
2181 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2182 * \param[in] codec array of active codecs (active codec set)
2183 * \param[in] codecs number of codecs in \a codec
2184 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2185 * \param[out] cmr Output in \a codec_mode_req = 1
2186 * \param[out] n_errors Number of detected bit errors
2187 * \param[out] n_bits_total Total number of bits
2188 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2189 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2190 * codec out of range; negative on error
2191 */
2192int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts,
2193 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2194 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2195{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002196 sbit_t iB[912], cB[456], h;
2197 ubit_t d[244], p[6], conv[250];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002198 int i, rv, len, steal = 0, id = -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002199 *n_errors = 0; *n_bits_total = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002200 static ubit_t sid_first_dummy[64] = { 0 };
2201 sbit_t sid_update_enc[256];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002202
2203 for (i=0; i<8; i++) {
2204 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2205 steal -= h;
2206 }
2207
2208 gsm0503_tch_fr_deinterleave(cB, iB);
2209
2210 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002211 /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002212 * It's normally updated by gsm0503_detect_afs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002213 * reached in case of FACCH. Reset it here to avoid FACCH/F frames being
2214 * misinterpreted as AMR's special DTX frames. */
2215 if (dtx != NULL)
2216 *dtx = AMR_OTHER;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002217 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2218 if (rv) {
2219 /* Error decoding FACCH frame */
2220 return -1;
2221 }
2222
2223 return GSM_MACBLOCK_LEN;
2224 }
2225
Philipp Maier898c9c62020-02-06 14:25:01 +01002226 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2227 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002228 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2229
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002230 *dtx = gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Philipp Maier898c9c62020-02-06 14:25:01 +01002231
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002232 switch (*dtx) {
2233 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002234 /* NOTE: The AFS_SID_UPDATE frame is splitted into
2235 * two half rate frames. If the id marker frame
2236 * (AFS_SID_UPDATE) is detected the following frame
2237 * contains the actual comfort noised data part of
2238 * (AFS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002239 if (dtx_prev != AFS_SID_UPDATE)
2240 break;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002241 /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002242 *dtx = AFS_SID_UPDATE_CN;
2243
2244 extract_afs_sid_update(sid_update_enc, cB);
2245 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2246 sid_update_enc, conv, n_errors,
2247 n_bits_total);
2248 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2249 35, conv + 35);
2250 if (rv != 0) {
2251 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2252 return -1;
2253 }
2254
2255 tch_amr_sid_update_append(conv, 1,
2256 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002257 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002258 tch_amr_reassemble(tch_data, conv, 39);
2259 len = 5;
2260 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002261 case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002262 tch_amr_sid_update_append(sid_first_dummy, 0,
2263 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002264 : codec[id > 0 ? id : 0]);
Vadim Yanitskiy7d121cb2023-06-23 16:57:48 +07002265 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
Philipp Maier898c9c62020-02-06 14:25:01 +01002266 len = 5;
2267 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002268 case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002269 case AFS_ONSET:
Philipp Maier898c9c62020-02-06 14:25:01 +01002270 len = 0;
2271 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002272 default:
2273 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002274 }
2275 }
2276
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002277 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2278 if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002279 /* Codec mode out of range, return id */
2280 return id;
2281 }
2282
2283 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2284 case 7: /* TCH/AFS12.2 */
2285 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2286 conv, n_errors, n_bits_total);
2287
2288 tch_amr_unmerge(d, p, conv, 244, 81);
2289
2290 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2291 if (rv) {
2292 /* Error checking CRC8 for an AMR 12.2 frame */
2293 return -1;
2294 }
2295
2296 tch_amr_reassemble(tch_data, d, 244);
2297
2298 len = 31;
2299
2300 break;
2301 case 6: /* TCH/AFS10.2 */
2302 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2303 conv, n_errors, n_bits_total);
2304
2305 tch_amr_unmerge(d, p, conv, 204, 65);
2306
2307 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2308 if (rv) {
2309 /* Error checking CRC8 for an AMR 10.2 frame */
2310 return -1;
2311 }
2312
2313 tch_amr_reassemble(tch_data, d, 204);
2314
2315 len = 26;
2316
2317 break;
2318 case 5: /* TCH/AFS7.95 */
2319 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2320 conv, n_errors, n_bits_total);
2321
2322 tch_amr_unmerge(d, p, conv, 159, 75);
2323
2324 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2325 if (rv) {
2326 /* Error checking CRC8 for an AMR 7.95 frame */
2327 return -1;
2328 }
2329
2330 tch_amr_reassemble(tch_data, d, 159);
2331
2332 len = 20;
2333
2334 break;
2335 case 4: /* TCH/AFS7.4 */
2336 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2337 conv, n_errors, n_bits_total);
2338
2339 tch_amr_unmerge(d, p, conv, 148, 61);
2340
2341 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2342 if (rv) {
2343 /* Error checking CRC8 for an AMR 7.4 frame */
2344 return -1;
2345 }
2346
2347 tch_amr_reassemble(tch_data, d, 148);
2348
2349 len = 19;
2350
2351 break;
2352 case 3: /* TCH/AFS6.7 */
2353 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2354 conv, n_errors, n_bits_total);
2355
2356 tch_amr_unmerge(d, p, conv, 134, 55);
2357
2358 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2359 if (rv) {
2360 /* Error checking CRC8 for an AMR 6.7 frame */
2361 return -1;
2362 }
2363
2364 tch_amr_reassemble(tch_data, d, 134);
2365
2366 len = 17;
2367
2368 break;
2369 case 2: /* TCH/AFS5.9 */
2370 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2371 conv, n_errors, n_bits_total);
2372
2373 tch_amr_unmerge(d, p, conv, 118, 55);
2374
2375 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2376 if (rv) {
2377 /* Error checking CRC8 for an AMR 5.9 frame */
2378 return -1;
2379 }
2380
2381 tch_amr_reassemble(tch_data, d, 118);
2382
2383 len = 15;
2384
2385 break;
2386 case 1: /* TCH/AFS5.15 */
2387 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2388 conv, n_errors, n_bits_total);
2389
2390 tch_amr_unmerge(d, p, conv, 103, 49);
2391
2392 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2393 if (rv) {
2394 /* Error checking CRC8 for an AMR 5.15 frame */
2395 return -1;
2396 }
2397
2398 tch_amr_reassemble(tch_data, d, 103);
2399
2400 len = 13;
2401
2402 break;
2403 case 0: /* TCH/AFS4.75 */
2404 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2405 conv, n_errors, n_bits_total);
2406
2407 tch_amr_unmerge(d, p, conv, 95, 39);
2408
2409 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2410 if (rv) {
2411 /* Error checking CRC8 for an AMR 4.75 frame */
2412 return -1;
2413 }
2414
2415 tch_amr_reassemble(tch_data, d, 95);
2416
2417 len = 12;
2418
2419 break;
2420 default:
2421 /* Unknown frame type */
2422 *n_bits_total = 448;
2423 *n_errors = *n_bits_total;
2424 return -1;
2425 }
2426
Philipp Maier898c9c62020-02-06 14:25:01 +01002427out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002428 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002429 if (id != -1) {
2430 if (codec_mode_req)
2431 *cmr = id;
2432 else
2433 *ft = id;
2434 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002435
2436 return len;
2437}
2438
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002439/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002440 * \param[out] bursts caller-allocated output buffer for bursts bits
2441 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002442 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002443 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2444 * \param[in] codec Array of codecs (active codec set)
2445 * \param[in] codecs Number of entries in \a codec
2446 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2447 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2448 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002449int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002450 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002451 uint8_t cmr)
2452{
2453 ubit_t iB[912], cB[456], h;
2454 ubit_t d[244], p[6], conv[250];
2455 int i;
2456 uint8_t id;
2457
2458 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2459 _xcch_encode_cB(cB, tch_data);
2460
2461 h = 1;
2462
2463 goto facch;
2464 }
2465
2466 h = 0;
2467
Vadim Yanitskiya9952692023-11-10 03:31:55 +07002468 id = codec_mode_req ? cmr : ft;
2469 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit)))
2470 return -1;
2471 if (OSMO_UNLIKELY(ft >= codecs))
2472 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002473
2474 switch (codec[ft]) {
2475 case 7: /* TCH/AFS12.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002476 if (!len) {
2477 /* No data, induce BFI in the receiver by inverted CRC bits.
2478 * The data bit are all 0, so the correct parity bits would be 111111. */
2479 memset(d, 0, 244);
2480 memset(p, 0, 6);
2481 } else {
2482 if (len != 31)
2483 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002484
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002485 tch_amr_disassemble(d, tch_data, 244);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002486
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002487 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2488 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002489
2490 tch_amr_merge(conv, d, p, 244, 81);
2491
2492 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2493
2494 break;
2495 case 6: /* TCH/AFS10.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002496 if (!len) {
2497 /* See comment above. */
2498 memset(d, 0, 204);
2499 memset(p, 0, 6);
2500 } else {
2501 if (len != 26)
2502 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002503
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002504 tch_amr_disassemble(d, tch_data, 204);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002505
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002506 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2507 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002508
2509 tch_amr_merge(conv, d, p, 204, 65);
2510
2511 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2512
2513 break;
2514 case 5: /* TCH/AFS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002515 if (!len) {
2516 /* See comment above. */
2517 memset(d, 0, 159);
2518 memset(p, 0, 6);
2519 } else {
2520 if (len != 20)
2521 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002522
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002523 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002524
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002525 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2526 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002527
2528 tch_amr_merge(conv, d, p, 159, 75);
2529
2530 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2531
2532 break;
2533 case 4: /* TCH/AFS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002534 if (!len) {
2535 /* See comment above. */
2536 memset(d, 0, 148);
2537 memset(p, 0, 6);
2538 } else {
2539 if (len != 19)
2540 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002541
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002542 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002543
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002544 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2545 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002546
2547 tch_amr_merge(conv, d, p, 148, 61);
2548
2549 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2550
2551 break;
2552 case 3: /* TCH/AFS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002553 if (!len) {
2554 /* See comment above. */
2555 memset(d, 0, 134);
2556 memset(p, 0, 6);
2557 } else {
2558 if (len != 17)
2559 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002560
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002561 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002562
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002563 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2564 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002565
2566 tch_amr_merge(conv, d, p, 134, 55);
2567
2568 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2569
2570 break;
2571 case 2: /* TCH/AFS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002572 if (!len) {
2573 /* See comment above. */
2574 memset(d, 0, 118);
2575 memset(p, 0, 6);
2576 } else {
2577 if (len != 15)
2578 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002579
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002580 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002581
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002582 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2583 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002584
2585 tch_amr_merge(conv, d, p, 118, 55);
2586
2587 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2588
2589 break;
2590 case 1: /* TCH/AFS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002591 if (!len) {
2592 /* See comment above. */
2593 memset(d, 0, 103);
2594 memset(p, 0, 6);
2595 } else {
2596 if (len != 13)
2597 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002598
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002599 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002600
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002601 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2602 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002603
2604 tch_amr_merge(conv, d, p, 103, 49);
2605
2606 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2607
2608 break;
2609 case 0: /* TCH/AFS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002610 if (!len) {
2611 /* See comment above. */
2612 memset(d, 0, 95);
2613 memset(p, 0, 6);
2614 } else {
2615 if (len != 12)
2616 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002617
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002618 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002619
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002620 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2621 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002622
2623 tch_amr_merge(conv, d, p, 95, 39);
2624
2625 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2626
2627 break;
2628 default:
2629 /* FIXME: FT %ft is not supported */
2630 return -1;
2631 }
2632
2633 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2634
2635facch:
2636 gsm0503_tch_fr_interleave(cB, iB);
2637
2638 for (i = 0; i < 8; i++) {
2639 gsm0503_tch_burst_map(&iB[i * 114],
2640 &bursts[i * 116], &h, i >> 2);
2641 }
2642
2643 return 0;
2644
2645invalid_length:
2646 /* FIXME: payload length %len does not comply with codec type %ft */
2647 return -1;
2648}
2649
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002650/* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2651static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB)
2652{
2653 unsigned int id = 0, best = 0;
2654 unsigned int i, j, k;
2655
2656 for (i = 0, k = 0; i < 4; i++) {
2657 /* FIXME: why not using remaining (16 - 4) soft-bits here? */
2658 for (j = 0, k = 0; j < 4; j++)
2659 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2660
2661 if (i == 0 || k < best) {
2662 best = k;
2663 id = i;
2664 }
2665 }
2666
2667 return id;
2668}
2669
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002670/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002671 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002672 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02002673 * \param[in] odd Is this an odd (1) or even (0) frame number?
2674 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2675 * \param[in] codec array of active codecs (active codec set)
2676 * \param[in] codecs number of codecs in \a codec
2677 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2678 * \param[out] cmr Output in \a codec_mode_req = 1
2679 * \param[out] n_errors Number of detected bit errors
2680 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002681 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2682 * codec out of range; negative on error
2683 */
Harald Welteb9946d32017-06-12 09:40:16 +02002684int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002685 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2686 uint8_t *cmr, int *n_errors, int *n_bits_total)
2687{
Philipp Maier898c9c62020-02-06 14:25:01 +01002688 return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req,
2689 codec, codecs, ft, cmr, n_errors,
2690 n_bits_total, NULL);
2691}
2692
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002693/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Philipp Maier898c9c62020-02-06 14:25:01 +01002694 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002695 * \param[in] bursts buffer containing the symbols of 6 bursts
Philipp Maier898c9c62020-02-06 14:25:01 +01002696 * \param[in] odd Is this an odd (1) or even (0) frame number?
2697 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2698 * \param[in] codec array of active codecs (active codec set)
2699 * \param[in] codecs number of codecs in \a codec
2700 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2701 * \param[out] cmr Output in \a codec_mode_req = 1
2702 * \param[out] n_errors Number of detected bit errors
2703 * \param[out] n_bits_total Total number of bits
2704 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2705 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2706 * codec out of range; negative on error
2707 */
2708int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd,
2709 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2710 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2711{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002712 sbit_t iB[912], cB[456], h;
2713 ubit_t d[244], p[6], conv[135];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002714 int i, rv, len, steal = 0, id = -1;
Philipp Maier898c9c62020-02-06 14:25:01 +01002715 static ubit_t sid_first_dummy[64] = { 0 };
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002716
2717 /* only unmap the stealing bits */
2718 if (!odd) {
2719 for (i = 0; i < 4; i++) {
2720 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2721 steal -= h;
2722 }
2723 for (i = 2; i < 5; i++) {
2724 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2725 steal -= h;
2726 }
2727 }
2728
2729 /* if we found a stole FACCH, but only at correct alignment */
2730 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002731 /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002732 * It's normally updated by gsm0503_detect_ahs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002733 * reached in case of FACCH. Reset it here to avoid FACCH/H frames being
2734 * misinterpreted as AMR's special DTX frames. */
2735 if (dtx != NULL)
2736 *dtx = AMR_OTHER;
2737
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002738 for (i = 0; i < 6; i++) {
2739 gsm0503_tch_burst_unmap(&iB[i * 114],
2740 &bursts[i * 116], NULL, i >> 2);
2741 }
2742
2743 for (i = 2; i < 4; i++) {
2744 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2745 &bursts[i * 116], NULL, 1);
2746 }
2747
2748 gsm0503_tch_fr_deinterleave(cB, iB);
2749
2750 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2751 if (rv) {
2752 /* Error decoding FACCH frame */
2753 return -1;
2754 }
2755
2756 return GSM_MACBLOCK_LEN;
2757 }
2758
2759 for (i = 0; i < 4; i++) {
2760 gsm0503_tch_burst_unmap(&iB[i * 114],
2761 &bursts[i * 116], NULL, i >> 1);
2762 }
2763
2764 gsm0503_tch_hr_deinterleave(cB, iB);
2765
Philipp Maier898c9c62020-02-06 14:25:01 +01002766 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2767 if (dtx) {
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002768 int n_bits_total_sid;
2769 int n_errors_sid;
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002770
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002771 *dtx = gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002772 /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */
Philipp Maier898c9c62020-02-06 14:25:01 +01002773
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002774 switch (*dtx) {
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002775 case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002776 /* cB[] contains 16 bits of coded in-band data and 212 bits containing
2777 * the identification marker. We need to unmap/deinterleave 114 odd
2778 * bits from the last two blocks, 114 even bits from the first two
2779 * blocks and combine them together. */
2780 gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0);
2781 gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0);
2782 gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1);
2783 gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1);
2784 gsm0503_tch_hr_deinterleave(cB, iB);
2785
2786 /* cB[] is expected to contain 16 bits of coded in-band data and
2787 * 212 bits containing the coded data (53 bits coded at 1/4 rate). */
Philipp Maier898c9c62020-02-06 14:25:01 +01002788 *dtx = AHS_SID_UPDATE_CN;
2789
2790 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002791 cB + 16, conv, &n_errors_sid,
2792 &n_bits_total_sid);
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002793 /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002794 * osmo_conv_decode_ber() calculates BER for the coded data. */
2795 if (n_errors != NULL)
2796 *n_errors += n_errors_sid;
2797 if (n_bits_total != NULL)
2798 *n_bits_total += n_bits_total_sid;
Philipp Maier898c9c62020-02-06 14:25:01 +01002799 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2800 35, conv + 35);
2801 if (rv != 0) {
2802 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2803 return -1;
2804 }
2805
2806 tch_amr_sid_update_append(conv, 1,
2807 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002808 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002809 tch_amr_reassemble(tch_data, conv, 39);
2810 len = 5;
2811 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002812 case AHS_SID_FIRST_P2:
Philipp Maier898c9c62020-02-06 14:25:01 +01002813 tch_amr_sid_update_append(sid_first_dummy, 0,
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, sid_first_dummy, 39);
2817 len = 5;
2818 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002819 case AHS_ONSET:
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002820 case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2821 case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2822 case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002823 len = 0;
2824 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002825 default:
2826 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002827 }
2828 }
2829
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002830 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2831 if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002832 /* Codec mode out of range, return id */
2833 return id;
2834 }
2835
2836 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2837 case 5: /* TCH/AHS7.95 */
2838 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2839 conv, n_errors, n_bits_total);
2840
2841 tch_amr_unmerge(d, p, conv, 123, 67);
2842
2843 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2844 if (rv) {
2845 /* Error checking CRC8 for an AMR 7.95 frame */
2846 return -1;
2847 }
2848
2849 for (i = 0; i < 36; i++)
2850 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2851
2852 tch_amr_reassemble(tch_data, d, 159);
2853
2854 len = 20;
2855
2856 break;
2857 case 4: /* TCH/AHS7.4 */
2858 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2859 conv, n_errors, n_bits_total);
2860
2861 tch_amr_unmerge(d, p, conv, 120, 61);
2862
2863 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2864 if (rv) {
2865 /* Error checking CRC8 for an AMR 7.4 frame */
2866 return -1;
2867 }
2868
2869 for (i = 0; i < 28; i++)
2870 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2871
2872 tch_amr_reassemble(tch_data, d, 148);
2873
2874 len = 19;
2875
2876 break;
2877 case 3: /* TCH/AHS6.7 */
2878 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2879 conv, n_errors, n_bits_total);
2880
2881 tch_amr_unmerge(d, p, conv, 110, 55);
2882
2883 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2884 if (rv) {
2885 /* Error checking CRC8 for an AMR 6.7 frame */
2886 return -1;
2887 }
2888
2889 for (i = 0; i < 24; i++)
2890 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2891
2892 tch_amr_reassemble(tch_data, d, 134);
2893
2894 len = 17;
2895
2896 break;
2897 case 2: /* TCH/AHS5.9 */
2898 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2899 conv, n_errors, n_bits_total);
2900
2901 tch_amr_unmerge(d, p, conv, 102, 55);
2902
2903 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2904 if (rv) {
2905 /* Error checking CRC8 for an AMR 5.9 frame */
2906 return -1;
2907 }
2908
2909 for (i = 0; i < 16; i++)
2910 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2911
2912 tch_amr_reassemble(tch_data, d, 118);
2913
2914 len = 15;
2915
2916 break;
2917 case 1: /* TCH/AHS5.15 */
2918 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2919 conv, n_errors, n_bits_total);
2920
2921 tch_amr_unmerge(d, p, conv, 91, 49);
2922
2923 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2924 if (rv) {
2925 /* Error checking CRC8 for an AMR 5.15 frame */
2926 return -1;
2927 }
2928
2929 for (i = 0; i < 12; i++)
2930 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2931
2932 tch_amr_reassemble(tch_data, d, 103);
2933
2934 len = 13;
2935
2936 break;
2937 case 0: /* TCH/AHS4.75 */
2938 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2939 conv, n_errors, n_bits_total);
2940
2941 tch_amr_unmerge(d, p, conv, 83, 39);
2942
2943 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2944 if (rv) {
2945 /* Error checking CRC8 for an AMR 4.75 frame */
2946 return -1;
2947 }
2948
2949 for (i = 0; i < 12; i++)
2950 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2951
2952 tch_amr_reassemble(tch_data, d, 95);
2953
2954 len = 12;
2955
2956 break;
2957 default:
2958 /* Unknown frame type */
2959 *n_bits_total = 159;
2960 *n_errors = *n_bits_total;
2961 return -1;
2962 }
2963
Philipp Maier898c9c62020-02-06 14:25:01 +01002964out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002965 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002966 if (id != -1) {
2967 if (codec_mode_req)
2968 *cmr = id;
2969 else
2970 *ft = id;
2971 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002972
2973 return len;
2974}
2975
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002976/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002977 * \param[out] bursts caller-allocated output buffer for bursts bits
2978 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002979 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002980 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2981 * \param[in] codec Array of codecs (active codec set)
2982 * \param[in] codecs Number of entries in \a codec
2983 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2984 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2985 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002986int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002987 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002988 uint8_t cmr)
2989{
2990 ubit_t iB[912], cB[456], h;
2991 ubit_t d[244], p[6], conv[135];
2992 int i;
2993 uint8_t id;
2994
2995 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2996 _xcch_encode_cB(cB, tch_data);
2997
2998 h = 1;
2999
3000 gsm0503_tch_fr_interleave(cB, iB);
3001
3002 for (i = 0; i < 6; i++)
3003 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
3004 &h, i >> 2);
3005 for (i = 2; i < 4; i++)
3006 gsm0503_tch_burst_map(&iB[i * 114 + 456],
3007 &bursts[i * 116], &h, 1);
3008
3009 return 0;
3010 }
3011
3012 h = 0;
3013
Vadim Yanitskiya9952692023-11-10 03:31:55 +07003014 id = codec_mode_req ? cmr : ft;
3015 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit)))
3016 return -1;
3017 if (OSMO_UNLIKELY(ft >= codecs))
3018 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003019
3020 switch (codec[ft]) {
3021 case 5: /* TCH/AHS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003022 if (!len) {
3023 /* No data, induce BFI in the receiver by inverted CRC bits.
3024 * The data bit are all 0, so the correct parity bits would be 111111. */
3025 memset(d, 0, 159);
3026 memset(p, 0, 6);
3027 } else {
3028 if (len != 20)
3029 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003030
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003031 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003032
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003033 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
3034 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003035
3036 tch_amr_merge(conv, d, p, 123, 67);
3037
3038 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
3039
3040 memcpy(cB + 192, d + 123, 36);
3041
3042 break;
3043 case 4: /* TCH/AHS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003044 if (!len) {
3045 /* See comment above. */
3046 memset(d, 0, 148);
3047 memset(p, 0, 6);
3048 } else {
3049 if (len != 19)
3050 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003051
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003052 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003053
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003054 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
3055 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003056
3057 tch_amr_merge(conv, d, p, 120, 61);
3058
3059 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
3060
3061 memcpy(cB + 200, d + 120, 28);
3062
3063 break;
3064 case 3: /* TCH/AHS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003065 if (!len) {
3066 /* See comment above. */
3067 memset(d, 0, 134);
3068 memset(p, 0, 6);
3069 } else {
3070 if (len != 17)
3071 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003072
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003073 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003074
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003075 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3076 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003077
3078 tch_amr_merge(conv, d, p, 110, 55);
3079
3080 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
3081
3082 memcpy(cB + 204, d + 110, 24);
3083
3084 break;
3085 case 2: /* TCH/AHS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003086 if (!len) {
3087 /* See comment above. */
3088 memset(d, 0, 118);
3089 memset(p, 0, 6);
3090 } else {
3091 if (len != 15)
3092 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003093
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003094 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003095
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003096 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3097 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003098
3099 tch_amr_merge(conv, d, p, 102, 55);
3100
3101 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
3102
3103 memcpy(cB + 212, d + 102, 16);
3104
3105 break;
3106 case 1: /* TCH/AHS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003107 if (!len) {
3108 /* See comment above. */
3109 memset(d, 0, 103);
3110 memset(p, 0, 6);
3111 } else {
3112 if (len != 13)
3113 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003114
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003115 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003116
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003117 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
3118 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003119
3120 tch_amr_merge(conv, d, p, 91, 49);
3121
3122 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
3123
3124 memcpy(cB + 216, d + 91, 12);
3125
3126 break;
3127 case 0: /* TCH/AHS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003128 if (!len) {
3129 /* See comment above. */
3130 memset(d, 0, 95);
3131 memset(p, 0, 6);
3132 } else {
3133 if (len != 12)
3134 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003135
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003136 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003137
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003138 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
3139 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003140
3141 tch_amr_merge(conv, d, p, 83, 39);
3142
3143 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
3144
3145 memcpy(cB + 216, d + 83, 12);
3146
3147 break;
3148 default:
3149 /* FIXME: FT %ft is not supported */
3150 return -1;
3151 }
3152
Philipp Maiercfea39b2021-08-31 16:02:31 +02003153 memcpy(cB, gsm0503_ahs_ic_ubit[id], 4);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003154
3155 gsm0503_tch_hr_interleave(cB, iB);
3156
3157 for (i = 0; i < 4; i++)
3158 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
3159
3160 return 0;
3161
3162invalid_length:
3163 /* FIXME: payload length %len does not comply with codec type %ft */
3164 return -1;
3165}
3166
3167/*
3168 * GSM RACH transcoding
3169 */
3170
3171/*
3172 * GSM RACH apply BSIC to parity
3173 *
3174 * p(j) = p(j) xor b(j) j = 0, ..., 5
3175 * b(0) = MSB of PLMN colour code
3176 * b(5) = LSB of BS colour code
3177 */
Max32e56412017-10-16 14:58:00 +02003178static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003179{
3180 int i;
3181
3182 /* Apply it */
3183 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02003184 d[start + i] ^= ((bsic >> (5 - i)) & 1);
3185}
3186
Harald Welte6950b192018-02-26 11:48:00 +01003187static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
3188 int *n_errors, int *n_bits_total)
Max32e56412017-10-16 14:58:00 +02003189{
3190 ubit_t conv[17];
3191 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
3192 int rv;
3193
Harald Welte6950b192018-02-26 11:48:00 +01003194 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
3195 n_errors, n_bits_total);
Max32e56412017-10-16 14:58:00 +02003196
3197 rach_apply_bsic(conv, bsic, nbits);
3198
3199 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3200 if (rv)
3201 return -1;
3202
3203 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
3204
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003205 return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0];
Max32e56412017-10-16 14:58:00 +02003206}
3207
3208/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3209 * \param[out] ra output buffer for RACH data
3210 * \param[in] burst Input burst data
3211 * \param[in] bsic BSIC used in this cell
3212 * \returns 0 on success; negative on error (e.g. CRC error) */
3213int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
3214{
Harald Welte6950b192018-02-26 11:48:00 +01003215 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
Max32e56412017-10-16 14:58:00 +02003216
3217 if (r < 0)
3218 return r;
3219
3220 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003221
3222 return 0;
3223}
3224
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003225/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003226 * \param[out] ra output buffer for RACH data
3227 * \param[in] burst Input burst data
3228 * \param[in] bsic BSIC used in this cell
3229 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003230int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003231{
Harald Welte6950b192018-02-26 11:48:00 +01003232 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
3233 if (r < 0)
3234 return r;
3235
3236 *ra = r;
3237 return 0;
3238}
3239
3240/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3241 * \param[out] ra output buffer for RACH data
3242 * \param[in] burst Input burst data
3243 * \param[in] bsic BSIC used in this cell
3244 * \param[out] n_errors Number of detected bit errors
3245 * \param[out] n_bits_total Total number of bits
3246 * \returns 0 on success; negative on error (e.g. CRC error) */
3247int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
3248 int *n_errors, int *n_bits_total)
3249{
3250 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
3251 if (r < 0)
3252 return r;
3253
3254 *ra = r;
3255 return 0;
3256}
3257
3258/*! Decode the (8-bit) RACH according to TS 05.03
3259 * \param[out] ra output buffer for RACH data
3260 * \param[in] burst Input burst data
3261 * \param[in] bsic BSIC used in this cell
3262 * \param[out] n_errors Number of detected bit errors
3263 * \param[out] n_bits_total Total number of bits
3264 * \returns 0 on success; negative on error (e.g. CRC error) */
3265int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
3266 int *n_errors, int *n_bits_total)
3267{
3268 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003269
Max32e56412017-10-16 14:58:00 +02003270 if (r < 0)
3271 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003272
Max32e56412017-10-16 14:58:00 +02003273 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003274
3275 return 0;
3276}
3277
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003278/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003279 * \param[out] burst Caller-allocated output burst buffer
3280 * \param[in] ra Input RACH data
3281 * \param[in] bsic BSIC used in this cell
3282 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003283int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003284{
Max32e56412017-10-16 14:58:00 +02003285 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
3286}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003287
Max32e56412017-10-16 14:58:00 +02003288/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
3289 * \param[out] burst Caller-allocated output burst buffer
3290 * \param[in] ra11 Input RACH data
3291 * \param[in] bsic BSIC used in this cell
3292 * \param[in] is_11bit whether given RA is 11 bit or not
3293 * \returns 0 on success; negative on error */
3294int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
3295{
3296 ubit_t conv[17];
3297 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003298
Max32e56412017-10-16 14:58:00 +02003299 if (is_11bit) {
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003300 ra[0] = (uint8_t) (ra11 >> 3);
3301 ra[1] = (uint8_t) (ra11 & 0x07);
Max32e56412017-10-16 14:58:00 +02003302 nbits = 11;
3303 } else
3304 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003305
Max32e56412017-10-16 14:58:00 +02003306 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003307
Max32e56412017-10-16 14:58:00 +02003308 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3309
3310 rach_apply_bsic(conv, bsic, nbits);
3311
3312 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003313
3314 return 0;
3315}
3316
3317/*
3318 * GSM SCH transcoding
3319 */
Harald Weltec6636782017-06-12 14:59:37 +02003320
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003321/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003322 * \param[out] sb_info output buffer for SCH data
3323 * \param[in] burst Input burst data
3324 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003325int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003326{
3327 ubit_t conv[35];
3328 int rv;
3329
3330 osmo_conv_decode(&gsm0503_sch, burst, conv);
3331
3332 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3333 if (rv)
3334 return -1;
3335
3336 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
3337
3338 return 0;
3339}
3340
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003341/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003342 * \param[out] burst Caller-allocated output burst buffer
3343 * \param[in] sb_info Input SCH data
3344 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003345int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003346{
3347 ubit_t conv[35];
3348
3349 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
3350
3351 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3352
3353 osmo_conv_encode(&gsm0503_sch, conv, burst);
3354
3355 return 0;
3356}
Harald Weltec6636782017-06-12 14:59:37 +02003357
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003358/*
3359 * GSM CSD transcoding
3360 */
3361
3362static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB)
3363{
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003364 unsigned int i;
3365
3366 /* hu(B): copy *even* numbered bits if not stolen by FACCH */
3367 if (burst[58] == 0) {
3368 for (i = 0; i < 57; i += 2)
3369 burst[i] |= iB[i];
3370 for (i = 58; i < 114; i += 2)
3371 burst[i + 2] |= iB[i];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003372 }
3373
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003374 /* hl(B): copy *odd* numbered bits if not stolen by FACCH */
3375 if (burst[57] == 0) {
3376 for (i = 1; i < 57; i += 2)
3377 burst[i] |= iB[i];
3378 for (i = 57; i < 114; i += 2)
3379 burst[i + 2] |= iB[i];
3380 }
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003381}
3382
3383/*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3.
3384 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3385 * 22 * 2 * 58 == 2552 bits total.
3386 * \param[in] data Data to be encoded (240 unpacked bits).
3387 * \returns 0 in case of success; negative on error. */
3388int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data)
3389{
3390 ubit_t iB[22 * 114], cB[4 * 114];
3391 ubit_t conv[4 * 60 + 4];
3392
3393 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3394 memcpy(&conv[0], &data[0], 4 * 60);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003395 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003396
3397 /* 3.3.3 Convolutional encoder */
3398 osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]);
3399
3400 /* 3.3.4 Interleaving */
3401 memset(&iB[0], 0, sizeof(iB));
3402 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3403
3404 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3405 for (unsigned int i = 0; i < 22; i++)
3406 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3407
3408 return 0;
3409}
3410
3411/*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3.
3412 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3413 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3414 * 22 * 2 * 58 == 2552 bits total.
3415 * \param[out] n_errors Number of detected bit errors.
3416 * \param[out] n_bits_total Total number of bits.
3417 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3418int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts,
3419 int *n_errors, int *n_bits_total)
3420{
3421 sbit_t iB[22 * 114], cB[4 * 114];
3422 ubit_t conv[4 * 60 + 4];
3423
3424 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3425 for (unsigned int i = 0; i < 22; i++) {
3426 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3427 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3428 }
3429
3430 /* 3.3.4 Interleaving */
3431 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3432
3433 /* 3.3.3 Convolutional encoder */
3434 osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total);
3435
3436 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3437 memcpy(&data[0], &conv[0], 4 * 60);
3438
3439 return 4 * 60;
3440}
3441
3442/*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4.
3443 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3444 * 22 * 2 * 58 == 2552 bits total.
3445 * \param[in] data Data to be encoded (120 unpacked bits).
3446 * \returns 0 in case of success; negative on error */
3447int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data)
3448{
3449 ubit_t iB[22 * 114], cB[4 * 114];
3450 ubit_t conv[2 * 60 + 32];
3451
3452 /* 3.4.2 Block code:
3453 *
3454 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3455 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3456 *
3457 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3458 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3459 *
3460 * Two such blocks forming a block of 152 bits: u1 + u2. */
3461 for (unsigned int k = 0; k < 2 * 4; k++) {
3462 memcpy(&conv[19 * k], &data[15 * k], 15);
3463 memset(&conv[19 * k + 15], 0, 4);
3464 }
3465
3466 /* 3.4.3 Convolutional encoder */
3467 osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]);
3468
3469 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3470 memset(&iB[0], 0, sizeof(iB));
3471 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3472
3473 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3474 for (unsigned int i = 0; i < 22; i++)
3475 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3476
3477 return 0;
3478}
3479
3480/*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4.
3481 * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits).
3482 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3483 * 22 * 2 * 58 == 2552 bits total.
3484 * \param[out] n_errors Number of detected bit errors.
3485 * \param[out] n_bits_total Total number of bits.
3486 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3487int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts,
3488 int *n_errors, int *n_bits_total)
3489{
3490 sbit_t iB[22 * 114], cB[4 * 114];
3491 ubit_t conv[2 * 60 + 32];
3492
3493 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3494 for (unsigned int i = 0; i < 22; i++) {
3495 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3496 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3497 }
3498
3499 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3500 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3501
3502 /* 3.4.3 Convolutional encoder */
3503 osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total);
3504
3505 /* 3.4.2 Block code:
3506 *
3507 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3508 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3509 *
3510 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3511 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3512 *
3513 * Two such blocks forming a block of 152 bits: u1 + u2. */
3514 for (unsigned int k = 0; k < 2 * 4; k++)
3515 memcpy(&data[15 * k], &conv[19 * k], 15);
3516
3517 return 2 * 60;
3518}
3519
3520/*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5.
3521 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3522 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3523 * 22 * 2 * 58 == 2552 bits total.
3524 * \param[in] data Data to be encoded (240 unpacked bits).
3525 * \returns 0 in case of success; negative on error */
3526int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data)
3527{
3528 return gsm0503_tch_fr96_encode(bursts, data);
3529}
3530
3531/*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5.
3532 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3533 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3534 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3535 * 22 * 2 * 58 == 2552 bits total.
3536 * \param[out] n_errors Number of detected bit errors.
3537 * \param[out] n_bits_total Total number of bits.
3538 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3539int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts,
3540 int *n_errors, int *n_bits_total)
3541{
3542 return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total);
3543}
3544
Vadim Yanitskiyfadda012023-07-08 06:30:55 +07003545/*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6.
3546 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3547 * 8 * 2 * 58 == 928 bits total.
3548 * \param[in] data Data to be encoded (72 unpacked bits).
3549 * \returns 0 in case of success; negative on error */
3550int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data)
3551{
3552 ubit_t iB[8 * 114], cB[4 * 114];
3553 const ubit_t h = 0;
3554
3555 /* 3.6.{1-3} Block code and Convolutional encoder */
3556 osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]);
3557
3558 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3559 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3560
3561 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3562 for (unsigned int i = 0; i < 8; i++)
3563 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3564
3565 return 0;
3566}
3567
3568/*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6.
3569 * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits).
3570 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3571 * 8 * 2 * 58 == 928 bits total.
3572 * \param[out] n_errors Number of detected bit errors.
3573 * \param[out] n_bits_total Total number of bits.
3574 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3575int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts,
3576 int *n_errors, int *n_bits_total)
3577{
3578 sbit_t iB[8 * 114], cB[4 * 114];
3579
3580 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3581 for (unsigned int i = 0; i < 8; i++)
3582 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3583
3584 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3585 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3586
3587 /* 3.6.{1-3} Block code and Convolutional encoder */
3588 osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total);
3589
3590 return 72;
3591}
3592
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003593/*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7.
3594 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3595 * 22 * 2 * 58 == 2552 bits total.
3596 * \param[in] data Data to be encoded (144 unpacked bits).
3597 * \returns 0 in case of success; negative on error */
3598int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data)
3599{
3600 ubit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003601
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003602 /* 3.7.{1-3} Block code and Convolutional encoder */
3603 osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]);
3604 osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]);
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003605
3606 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3607 memset(&iB[0], 0, sizeof(iB));
3608 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3609
3610 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3611 for (unsigned int i = 0; i < 22; i++)
3612 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3613
3614 return 0;
3615}
3616
3617/*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7.
3618 * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits).
3619 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3620 * 22 * 2 * 58 == 2552 bits total.
3621 * \param[out] n_errors Number of detected bit errors.
3622 * \param[out] n_bits_total Total number of bits.
3623 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3624int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts,
3625 int *n_errors, int *n_bits_total)
3626{
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003627 int n_errors_l[2], n_bits_total_l[2];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003628 sbit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003629
3630 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3631 for (unsigned int i = 0; i < 22; i++) {
3632 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3633 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3634 }
3635
3636 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3637 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3638
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003639 /* 3.7.{1-3} Block code and Convolutional encoder */
3640 osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]);
3641 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 +07003642
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003643 if (n_errors)
3644 *n_errors = n_errors_l[0] + n_errors_l[1];
3645
3646 if (n_bits_total)
3647 *n_bits_total = n_bits_total_l[0] + n_bits_total_l[1];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003648
3649 return 2 * 72;
3650}
3651
3652/*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8.
3653 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3654 * 22 * 2 * 58 == 2552 bits total.
3655 * \param[in] data Data to be encoded (290 unpacked bits).
3656 * \returns 0 in case of success; negative on error */
3657int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data)
3658{
3659 ubit_t iB[22 * 114], cB[4 * 114];
3660 ubit_t conv[290 + 4];
3661
3662 /* 3.8.2 Block code: b(290) + pad(4) */
3663 memcpy(&conv[0], &data[0], 290);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003664 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003665
3666 /* 3.8.3 Convolutional encoder */
3667 osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]);
3668
3669 /* 3.8.4 Interleaving */
3670 memset(&iB[0], 0, sizeof(iB));
3671 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3672
3673 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3674 for (unsigned int i = 0; i < 22; i++)
3675 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3676
3677 return 0;
3678}
3679
3680/*! Perform channel decoding of a TCH/14.4 channel as per section 3.8.
3681 * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits).
3682 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3683 * 22 * 2 * 58 == 2552 bits total.
3684 * \param[out] n_errors Number of detected bit errors.
3685 * \param[out] n_bits_total Total number of bits.
3686 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3687int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts,
3688 int *n_errors, int *n_bits_total)
3689{
3690 sbit_t iB[22 * 114], cB[4 * 114];
3691 ubit_t conv[294];
3692
3693 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3694 for (unsigned int i = 0; i < 22; i++) {
3695 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3696 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3697 }
3698
3699 /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3700 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3701
3702 /* 3.8.3 Convolutional encoder */
3703 osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total);
3704
3705 /* 3.8.2 Block code: b(290) + pad(4) */
3706 memcpy(&data[0], &conv[0], 290);
3707
3708 return 290;
3709}
3710
Vadim Yanitskiyb3340222023-05-26 01:07:18 +07003711/*
3712 * FACCH/[FH] transcoding
3713 */
3714
3715/*! Perform channel encoding of a FACCH/F data as per section 4.2.
3716 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3717 * 8 * 2 * 58 == 928 bits total.
3718 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3719 * \returns 0 in case of success; negative on error */
3720int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data)
3721{
3722 ubit_t iB[8 * 114], cB[4 * 114];
3723 const ubit_t h = 1;
3724
3725 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3726 _xcch_encode_cB(&cB[0], &data[0]);
3727
3728 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3729 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3730
3731 /* 4.2.5 Mapping on a Burst:
3732 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3733 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3734 for (unsigned int i = 0; i < 8; i++)
3735 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3736
3737 return 0;
3738}
3739
3740/*! Perform channel decoding of a FACCH/F data as per section 4.2.
3741 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3742 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3743 * 8 * 2 * 58 == 928 bits total.
3744 * \param[out] n_errors Number of detected bit errors.
3745 * \param[out] n_bits_total Total number of bits.
3746 * \returns Number of bytes used in the output buffer; negative on error. */
3747int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts,
3748 int *n_errors, int *n_bits_total)
3749{
3750 sbit_t iB[8 * 114], cB[4 * 114];
3751 int steal = 0;
3752
3753 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3754 for (unsigned int i = 0; i < 4; i++)
3755 steal -= bursts[i * 116 + 58]; /* hu(B) */
3756 for (unsigned int i = 4; i < 8; i++)
3757 steal -= bursts[i * 116 + 57]; /* hl(B) */
3758 if (steal <= 0)
3759 return -1;
3760
3761 /* 4.2.5 Mapping on a Burst:
3762 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3763 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3764 for (unsigned int i = 0; i < 8; i++)
3765 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3766
3767 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3768 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3769
3770 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3771 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3772 return -1;
3773
3774 return GSM_MACBLOCK_LEN;
3775}
3776
3777/*! Perform channel encoding of a FACCH/H data as per section 4.3.
3778 * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts,
3779 * 6 * 2 * 58 == 696 bits total.
3780 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3781 * \returns 0 in case of success; negative on error */
3782int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data)
3783{
3784 ubit_t iB[8 * 114], cB[4 * 114];
3785 const ubit_t h = 1;
3786
3787 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3788 _xcch_encode_cB(&cB[0], &data[0]);
3789
3790 /* 4.3.4 Interleaving */
3791 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3792
3793 /* 4.3.5 Mapping on a Burst:
3794 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3795 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3796 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3797 for (unsigned int i = 0; i < 6; i++)
3798 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3799 for (unsigned int i = 2; i < 4; i++)
3800 gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1);
3801
3802 return 0;
3803}
3804
3805/*! Perform channel decoding of a FACCH/H data as per section 4.3.
3806 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3807 * \param[in] bursts Buffer containing the symbols of 6 bursts,
3808 * 6 * 2 * 58 == 696 bits total.
3809 * \param[out] n_errors Number of detected bit errors.
3810 * \param[out] n_bits_total Total number of bits.
3811 * \returns Number of bytes used in the output buffer; negative on error. */
3812int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts,
3813 int *n_errors, int *n_bits_total)
3814{
3815 sbit_t iB[8 * 114], cB[4 * 114];
3816 int steal = 0;
3817
3818 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3819 for (unsigned int i = 0; i < 4; i++)
3820 steal -= bursts[i * 116 + 58]; /* hu(B) */
3821 for (unsigned int i = 2; i < 6; i++)
3822 steal -= bursts[i * 116 + 57]; /* hl(B) */
3823 if (steal <= 0)
3824 return -1;
3825
3826 /* 4.3.5 Mapping on a Burst:
3827 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3828 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3829 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3830 for (unsigned int i = 0; i < 6; i++)
3831 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3832 for (unsigned int i = 2; i < 4; i++)
3833 gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1);
3834
3835 /* 4.3.4 Interleaving */
3836 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3837
3838 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3839 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3840 return -1;
3841
3842 return GSM_MACBLOCK_LEN;
3843}
3844
Harald Weltec6636782017-06-12 14:59:37 +02003845/*! @} */