blob: 5b78502a59b764c65bcd5bf109251565725a4557 [file] [log] [blame]
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001/*
2 * (C) 2013 by Andreas Eversberg <jolly@eversberg.eu>
3 * (C) 2015 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co>
4 * (C) 2016 by Tom Tsou <tom.tsou@ettus.com>
Harald Weltec6636782017-06-12 14:59:37 +02005 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070020 */
21
22#include <stdio.h>
23#include <stdint.h>
24#include <string.h>
25#include <stdlib.h>
Maxc8cf8202017-05-22 16:07:04 +020026#include <errno.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070027
28#include <osmocom/core/bits.h>
29#include <osmocom/core/conv.h>
30#include <osmocom/core/utils.h>
31#include <osmocom/core/crcgen.h>
32#include <osmocom/core/endian.h>
33
Pau Espin Pedrol0158b052023-02-17 12:30:57 +010034#include <osmocom/gsm/protocol/gsm_44_060.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070035#include <osmocom/gsm/protocol/gsm_04_08.h>
36#include <osmocom/gsm/gsm0503.h>
37#include <osmocom/codec/codec.h>
38
39#include <osmocom/coding/gsm0503_interleaving.h>
40#include <osmocom/coding/gsm0503_mapping.h>
41#include <osmocom/coding/gsm0503_tables.h>
42#include <osmocom/coding/gsm0503_coding.h>
43#include <osmocom/coding/gsm0503_parity.h>
Philipp Maier898c9c62020-02-06 14:25:01 +010044#include <osmocom/coding/gsm0503_amr_dtx.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070045
Harald Weltec6636782017-06-12 14:59:37 +020046/*! \mainpage libosmocoding Documentation
47 *
48 * \section sec_intro Introduction
49 * This library is a collection of definitions, tables and functions
50 * implementing the GSM/GPRS/EGPRS channel coding (and decoding) as
51 * specified in 3GPP TS 05.03 / 45.003.
52 *
Vadim Yanitskiy9a232fd2018-01-19 03:05:32 +060053 * libosmocoding is developed as part of the Osmocom (Open Source Mobile
Harald Weltec6636782017-06-12 14:59:37 +020054 * Communications) project, a community-based, collaborative development
55 * project to create Free and Open Source implementations of mobile
56 * communications systems. For more information about Osmocom, please
57 * see https://osmocom.org/
58 *
59 * \section sec_copyright Copyright and License
60 * Copyright © 2013 by Andreas Eversberg\n
61 * Copyright © 2015 by Alexander Chemeris\n
62 * Copyright © 2016 by Tom Tsou\n
63 * Documentation Copyright © 2017 by Harald Welte\n
64 * All rights reserved. \n\n
65 * The source code of libosmocoding is licensed under the terms of the GNU
66 * General Public License as published by the Free Software Foundation;
67 * either version 2 of the License, or (at your option) any later
68 * version.\n
69 * See <http://www.gnu.org/licenses/> or COPYING included in the source
70 * code package istelf.\n
71 * The information detailed here is provided AS IS with NO WARRANTY OF
72 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
73 * FITNESS FOR A PARTICULAR PURPOSE.
74 * \n\n
75 *
76 * \section sec_tracker Homepage + Issue Tracker
77 * libosmocoding is distributed as part of libosmocore and shares its
78 * project page at http://osmocom.org/projects/libosmocore
79 *
80 * An Issue Tracker can be found at
81 * https://osmocom.org/projects/libosmocore/issues
82 *
83 * \section sec_contact Contact and Support
84 * Community-based support is available at the OpenBSC mailing list
85 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
86 * Commercial support options available upon request from
87 * <http://sysmocom.de/>
88 */
89
90
91/*! \addtogroup coding
92 * @{
93 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094 * GSM TS 05.03 coding
Harald Weltec6636782017-06-12 14:59:37 +020095 *
96 * This module is the "master module" of libosmocoding. It uses the
97 * various other modules (mapping, parity, interleaving) in order to
98 * implement the complete channel coding (and decoding) chain for the
99 * various channel types as defined in TS 05.03 / 45.003.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +0200100 *
101 * \file gsm0503_coding.c */
Harald Weltec6636782017-06-12 14:59:37 +0200102
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700103/*
104 * EGPRS coding limits
105 */
106
107/* Max header size with parity bits */
108#define EGPRS_HDR_UPP_MAX 54
109
110/* Max encoded header size */
111#define EGPRS_HDR_C_MAX 162
112
113/* Max punctured header size */
114#define EGPRS_HDR_HC_MAX 160
115
116/* Max data block size with parity bits */
117#define EGPRS_DATA_U_MAX 612
118
119/* Max encoded data block size */
120#define EGPRS_DATA_C_MAX 1836
121
122/* Max single block punctured data size */
123#define EGPRS_DATA_DC_MAX 1248
124
125/* Dual block punctured data size */
126#define EGPRS_DATA_C1 612
127#define EGPRS_DATA_C2 EGPRS_DATA_C1
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! union across the three different EGPRS Uplink header types */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700130union gprs_rlc_ul_hdr_egprs {
131 struct gprs_rlc_ul_header_egprs_1 type1;
132 struct gprs_rlc_ul_header_egprs_2 type2;
133 struct gprs_rlc_ul_header_egprs_3 type3;
134};
135
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136/*! union across the three different EGPRS Downlink header types */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700137union gprs_rlc_dl_hdr_egprs {
138 struct gprs_rlc_dl_header_egprs_1 type1;
139 struct gprs_rlc_dl_header_egprs_2 type2;
140 struct gprs_rlc_dl_header_egprs_3 type3;
141};
142
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143/*! Structure describing a Modulation and Coding Scheme */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700144struct gsm0503_mcs_code {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200145 /*! Modulation and Coding Scheme (MSC) number */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700146 uint8_t mcs;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200147 /*! Length of Uplink Stealing Flag (USF) in bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700148 uint8_t usf_len;
149
150 /* Header coding */
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151 /*! Length of header (bits) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700152 uint8_t hdr_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153 /*! Length of header convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700154 uint8_t hdr_code_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155 /*! Length of header code puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700156 uint8_t hdr_punc_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157 /*! header convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700158 const struct osmo_conv_code *hdr_conv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159 /*! header puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700160 const uint8_t *hdr_punc;
161
162 /* Data coding */
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163 /*! length of data (bits) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700164 uint16_t data_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200165 /*! length of data convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700166 uint16_t data_code_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167 /*! length of data code puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700168 uint16_t data_punc_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200169 /*! data convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700170 const struct osmo_conv_code *data_conv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200171 /*! data puncturing sequences */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700172 const uint8_t *data_punc[3];
173};
174
175/*
176 * EGPRS UL coding parameters
177 */
Harald Welte2f984ea2017-06-12 15:05:21 +0200178const struct gsm0503_mcs_code gsm0503_mcs_ul_codes[EGPRS_NUM_MCS] = {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700179 {
180 .mcs = EGPRS_MCS0,
181 },
182 {
183 .mcs = EGPRS_MCS1,
184 .hdr_len = 31,
185 .hdr_code_len = 117,
186 .hdr_punc_len = 80,
187 .hdr_conv = &gsm0503_mcs1_ul_hdr,
188 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
189
190 .data_len = 178,
191 .data_code_len = 588,
192 .data_punc_len = 372,
193 .data_conv = &gsm0503_mcs1,
194 .data_punc = {
195 gsm0503_puncture_mcs1_p1,
196 gsm0503_puncture_mcs1_p2,
197 NULL,
198 },
199 },
200 {
201 .mcs = EGPRS_MCS2,
202 .hdr_len = 31,
203 .hdr_code_len = 117,
204 .hdr_punc_len = 80,
205 .hdr_conv = &gsm0503_mcs1_ul_hdr,
206 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
207
208 .data_len = 226,
209 .data_code_len = 732,
210 .data_punc_len = 372,
211 .data_conv = &gsm0503_mcs2,
212 .data_punc = {
213 gsm0503_puncture_mcs2_p1,
214 gsm0503_puncture_mcs2_p2,
215 NULL,
216 },
217 },
218 {
219 .mcs = EGPRS_MCS3,
220 .hdr_len = 31,
221 .hdr_code_len = 117,
222 .hdr_punc_len = 80,
223 .hdr_conv = &gsm0503_mcs1_ul_hdr,
224 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
225
226 .data_len = 298,
227 .data_code_len = 948,
228 .data_punc_len = 372,
229 .data_conv = &gsm0503_mcs3,
230 .data_punc = {
231 gsm0503_puncture_mcs3_p1,
232 gsm0503_puncture_mcs3_p2,
233 gsm0503_puncture_mcs3_p3,
234 },
235 },
236 {
237 .mcs = EGPRS_MCS4,
238 .hdr_len = 31,
239 .hdr_code_len = 117,
240 .hdr_punc_len = 80,
241 .hdr_conv = &gsm0503_mcs1_ul_hdr,
242 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
243
244 .data_len = 354,
245 .data_code_len = 1116,
246 .data_punc_len = 372,
247 .data_conv = &gsm0503_mcs4,
248 .data_punc = {
249 gsm0503_puncture_mcs4_p1,
250 gsm0503_puncture_mcs4_p2,
251 gsm0503_puncture_mcs4_p3,
252 },
253 },
254 {
255 .mcs = EGPRS_MCS5,
256 .hdr_len = 37,
257 .hdr_code_len = 135,
258 .hdr_punc_len = 136,
259 .hdr_conv = &gsm0503_mcs5_ul_hdr,
260 .hdr_punc = NULL,
261
262 .data_len = 450,
263 .data_code_len = 1404,
264 .data_punc_len = 1248,
265 .data_conv = &gsm0503_mcs5,
266 .data_punc = {
267 gsm0503_puncture_mcs5_p1,
268 gsm0503_puncture_mcs5_p2,
269 NULL,
270 },
271 },
272 {
273 .mcs = EGPRS_MCS6,
274 .hdr_len = 37,
275 .hdr_code_len = 135,
276 .hdr_punc_len = 136,
277 .hdr_conv = &gsm0503_mcs5_ul_hdr,
278 .hdr_punc = NULL,
279
280 .data_len = 594,
281 .data_code_len = 1836,
282 .data_punc_len = 1248,
283 .data_conv = &gsm0503_mcs6,
284 .data_punc = {
285 gsm0503_puncture_mcs6_p1,
286 gsm0503_puncture_mcs6_p2,
287 NULL,
288 },
289 },
290 {
291 .mcs = EGPRS_MCS7,
292 .hdr_len = 46,
293 .hdr_code_len = 162,
294 .hdr_punc_len = 160,
295 .hdr_conv = &gsm0503_mcs7_ul_hdr,
296 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
297
298 .data_len = 900,
299 .data_code_len = 1404,
300 .data_punc_len = 612,
301 .data_conv = &gsm0503_mcs7,
302 .data_punc = {
303 gsm0503_puncture_mcs7_p1,
304 gsm0503_puncture_mcs7_p2,
305 gsm0503_puncture_mcs7_p3,
306 }
307 },
308 {
309 .mcs = EGPRS_MCS8,
310 .hdr_len = 46,
311 .hdr_code_len = 162,
312 .hdr_punc_len = 160,
313 .hdr_conv = &gsm0503_mcs7_ul_hdr,
314 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
315
316 .data_len = 1092,
317 .data_code_len = 1692,
318 .data_punc_len = 612,
319 .data_conv = &gsm0503_mcs8,
320 .data_punc = {
321 gsm0503_puncture_mcs8_p1,
322 gsm0503_puncture_mcs8_p2,
323 gsm0503_puncture_mcs8_p3,
324 }
325 },
326 {
327 .mcs = EGPRS_MCS9,
328 .hdr_len = 46,
329 .hdr_code_len = 162,
330 .hdr_punc_len = 160,
331 .hdr_conv = &gsm0503_mcs7_ul_hdr,
332 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
333
334 .data_len = 1188,
335 .data_code_len = 1836,
336 .data_punc_len = 612,
337 .data_conv = &gsm0503_mcs9,
338 .data_punc = {
339 gsm0503_puncture_mcs9_p1,
340 gsm0503_puncture_mcs9_p2,
341 gsm0503_puncture_mcs9_p3,
342 }
343 },
344};
345
346/*
347 * EGPRS DL coding parameters
348 */
Harald Welte2f984ea2017-06-12 15:05:21 +0200349const struct gsm0503_mcs_code gsm0503_mcs_dl_codes[EGPRS_NUM_MCS] = {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700350 {
351 .mcs = EGPRS_MCS0,
352 },
353 {
354 .mcs = EGPRS_MCS1,
355 .usf_len = 3,
356 .hdr_len = 28,
357 .hdr_code_len = 108,
358 .hdr_punc_len = 68,
359 .hdr_conv = &gsm0503_mcs1_dl_hdr,
360 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
361
362 .data_len = 178,
363 .data_code_len = 588,
364 .data_punc_len = 372,
365 .data_conv = &gsm0503_mcs1,
366 .data_punc = {
367 gsm0503_puncture_mcs1_p1,
368 gsm0503_puncture_mcs1_p2,
369 NULL,
370 },
371 },
372 {
373 .mcs = EGPRS_MCS2,
374 .usf_len = 3,
375 .hdr_len = 28,
376 .hdr_code_len = 108,
377 .hdr_punc_len = 68,
378 .hdr_conv = &gsm0503_mcs1_dl_hdr,
379 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
380
381 .data_len = 226,
382 .data_code_len = 732,
383 .data_punc_len = 372,
384 .data_conv = &gsm0503_mcs2,
385 .data_punc = {
386 gsm0503_puncture_mcs2_p1,
387 gsm0503_puncture_mcs2_p2,
388 NULL,
389 },
390 },
391 {
392 .mcs = EGPRS_MCS3,
393 .usf_len = 3,
394 .hdr_len = 28,
395 .hdr_code_len = 108,
396 .hdr_punc_len = 68,
397 .hdr_conv = &gsm0503_mcs1_dl_hdr,
398 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
399
400 .data_len = 298,
401 .data_code_len = 948,
402 .data_punc_len = 372,
403 .data_conv = &gsm0503_mcs3,
404 .data_punc = {
405 gsm0503_puncture_mcs3_p1,
406 gsm0503_puncture_mcs3_p2,
407 gsm0503_puncture_mcs3_p3,
408 },
409 },
410 {
411 .mcs = EGPRS_MCS4,
412 .usf_len = 3,
413 .hdr_len = 28,
414 .hdr_code_len = 108,
415 .hdr_punc_len = 68,
416 .hdr_conv = &gsm0503_mcs1_dl_hdr,
417 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
418
419 .data_len = 354,
420 .data_code_len = 1116,
421 .data_punc_len = 372,
422 .data_conv = &gsm0503_mcs4,
423 .data_punc = {
424 gsm0503_puncture_mcs4_p1,
425 gsm0503_puncture_mcs4_p2,
426 gsm0503_puncture_mcs4_p3,
427 },
428 },
429 {
430 .mcs = EGPRS_MCS5,
431 .usf_len = 3,
432 .hdr_len = 25,
433 .hdr_code_len = 99,
434 .hdr_punc_len = 100,
435 .hdr_conv = &gsm0503_mcs5_dl_hdr,
436 .hdr_punc = NULL,
437
438 .data_len = 450,
439 .data_code_len = 1404,
440 .data_punc_len = 1248,
441 .data_conv = &gsm0503_mcs5,
442 .data_punc = {
443 gsm0503_puncture_mcs5_p1,
444 gsm0503_puncture_mcs5_p2,
445 NULL,
446 },
447 },
448 {
449 .mcs = EGPRS_MCS6,
450 .usf_len = 3,
451 .hdr_len = 25,
452 .hdr_code_len = 99,
453 .hdr_punc_len = 100,
454 .hdr_conv = &gsm0503_mcs5_dl_hdr,
455 .hdr_punc = NULL,
456
457 .data_len = 594,
458 .data_code_len = 1836,
459 .data_punc_len = 1248,
460 .data_conv = &gsm0503_mcs6,
461 .data_punc = {
462 gsm0503_puncture_mcs6_p1,
463 gsm0503_puncture_mcs6_p2,
464 NULL,
465 },
466 },
467 {
468 .mcs = EGPRS_MCS7,
469 .usf_len = 3,
470 .hdr_len = 37,
471 .hdr_code_len = 135,
472 .hdr_punc_len = 124,
473 .hdr_conv = &gsm0503_mcs7_dl_hdr,
474 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
475
476 .data_len = 900,
477 .data_code_len = 1404,
478 .data_punc_len = 612,
479 .data_conv = &gsm0503_mcs7,
480 .data_punc = {
481 gsm0503_puncture_mcs7_p1,
482 gsm0503_puncture_mcs7_p2,
483 gsm0503_puncture_mcs7_p3,
484 }
485 },
486 {
487 .mcs = EGPRS_MCS8,
488 .usf_len = 3,
489 .hdr_len = 37,
490 .hdr_code_len = 135,
491 .hdr_punc_len = 124,
492 .hdr_conv = &gsm0503_mcs7_dl_hdr,
493 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
494
495 .data_len = 1092,
496 .data_code_len = 1692,
497 .data_punc_len = 612,
498 .data_conv = &gsm0503_mcs8,
499 .data_punc = {
500 gsm0503_puncture_mcs8_p1,
501 gsm0503_puncture_mcs8_p2,
502 gsm0503_puncture_mcs8_p3,
503 }
504 },
505 {
506 .mcs = EGPRS_MCS9,
507 .usf_len = 3,
508 .hdr_len = 37,
509 .hdr_code_len = 135,
510 .hdr_punc_len = 124,
511 .hdr_conv = &gsm0503_mcs7_dl_hdr,
512 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
513
514 .data_len = 1188,
515 .data_code_len = 1836,
516 .data_punc_len = 612,
517 .data_conv = &gsm0503_mcs9,
518 .data_punc = {
519 gsm0503_puncture_mcs9_p1,
520 gsm0503_puncture_mcs9_p2,
521 gsm0503_puncture_mcs9_p3,
522 }
523 },
524};
525
Alexander Chemeris147051f2018-07-14 21:02:29 +0200526/*! Convolutional Decode + compute BER for punctured codes
Harald Weltec6636782017-06-12 14:59:37 +0200527 * \param[in] code Description of Convolutional Code
528 * \param[in] input Input soft-bits (-127...127)
529 * \param[out] output bits
530 * \param[out] n_errors Number of bit-errors
531 * \param[out] n_bits_total Number of bits
Alexander Chemeris147051f2018-07-14 21:02:29 +0200532 * \param[in] data_punc Puncturing mask array. Can be NULL.
Harald Weltec6636782017-06-12 14:59:37 +0200533 */
Alexander Chemeris147051f2018-07-14 21:02:29 +0200534static int osmo_conv_decode_ber_punctured(const struct osmo_conv_code *code,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700535 const sbit_t *input, ubit_t *output,
Alexander Chemeris147051f2018-07-14 21:02:29 +0200536 int *n_errors, int *n_bits_total,
537 const uint8_t *data_punc)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700538{
539 int res, i, coded_len;
540 ubit_t recoded[EGPRS_DATA_C_MAX];
541
542 res = osmo_conv_decode(code, input, output);
543
Pau Espin Pedrold2737e62022-05-13 13:07:21 +0200544 if (!n_bits_total && !n_errors)
545 return res;
546
547 coded_len = osmo_conv_encode(code, output, recoded);
Pau Espin Pedrol6c58d152022-05-13 14:03:27 +0200548 OSMO_ASSERT(ARRAY_SIZE(recoded) >= coded_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700549
550 /* Count bit errors */
551 if (n_errors) {
552 *n_errors = 0;
553 for (i = 0; i < coded_len; i++) {
Alexander Chemeris147051f2018-07-14 21:02:29 +0200554 if (((!data_punc) || (data_punc && !data_punc[i])) &&
555 !((recoded[i] && input[i] < 0) ||
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700556 (!recoded[i] && input[i] > 0)) )
557 *n_errors += 1;
558 }
559 }
560
561 if (n_bits_total)
562 *n_bits_total = coded_len;
563
564 return res;
565}
566
Alexander Chemeris147051f2018-07-14 21:02:29 +0200567/*! Convolutional Decode + compute BER for non-punctured codes
568 * \param[in] code Description of Convolutional Code
569 * \param[in] input Input soft-bits (-127...127)
570 * \param[out] output bits
571 * \param[out] n_errors Number of bit-errors
572 * \param[out] n_bits_total Number of bits
573 */
574static int osmo_conv_decode_ber(const struct osmo_conv_code *code,
575 const sbit_t *input, ubit_t *output,
576 int *n_errors, int *n_bits_total)
577{
578 return osmo_conv_decode_ber_punctured(code, input, output,
579 n_errors, n_bits_total, NULL);
580}
581
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200582/*! convenience wrapper for decoding coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200583 * \param[out] l2_data caller-allocated buffer for L2 Frame
584 * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3
585 * \param[out] n_errors Number of detected errors
586 * \param[out] n_bits_total Number of total coded bits
587 * \returns 0 on success; -1 on CRC error */
Harald Welteb9946d32017-06-12 09:40:16 +0200588static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700589 int *n_errors, int *n_bits_total)
590{
591 ubit_t conv[224];
592 int rv;
593
594 osmo_conv_decode_ber(&gsm0503_xcch, cB,
595 conv, n_errors, n_bits_total);
596
597 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
598 conv, 184, conv + 184);
599 if (rv)
600 return -1;
601
602 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
603
604 return 0;
605}
606
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200607/*! convenience wrapper for encoding to coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200608 * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3
Vadim Yanitskiyfed02e02023-05-15 23:25:05 +0700609 * \param[in] l2_data to-be-encoded L2 Frame
Harald Weltec6636782017-06-12 14:59:37 +0200610 * \returns 0 */
Harald Welteb9946d32017-06-12 09:40:16 +0200611static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700612{
613 ubit_t conv[224];
614
615 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
616
617 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
618
619 osmo_conv_encode(&gsm0503_xcch, conv, cB);
620
621 return 0;
622}
623
624/*
625 * GSM xCCH block transcoding
626 */
Harald Weltec6636782017-06-12 14:59:37 +0200627
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200628/*! Decoding of xCCH data from bursts to L2 frame
Harald Weltec6636782017-06-12 14:59:37 +0200629 * \param[out] l2_data caller-allocated output data buffer
630 * \param[in] bursts four GSM bursts in soft-bits
631 * \param[out] n_errors Number of detected errors
632 * \param[out] n_bits_total Number of total coded bits
633 */
Harald Welteb9946d32017-06-12 09:40:16 +0200634int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700635 int *n_errors, int *n_bits_total)
636{
637 sbit_t iB[456], cB[456];
638 int i;
639
640 for (i = 0; i < 4; i++)
641 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL);
642
643 gsm0503_xcch_deinterleave(cB, iB);
644
645 return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total);
646}
647
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200648/*! Encoding of xCCH data from L2 frame to bursts
Harald Weltec6636782017-06-12 14:59:37 +0200649 * \param[out] bursts caller-allocated burst data (unpacked bits)
650 * \param[in] l2_data L2 input data (MAC block)
651 * \returns 0
652 */
Harald Welteb9946d32017-06-12 09:40:16 +0200653int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700654{
655 ubit_t iB[456], cB[456], hl = 1, hn = 1;
656 int i;
657
658 _xcch_encode_cB(cB, l2_data);
659
660 gsm0503_xcch_interleave(cB, iB);
661
662 for (i = 0; i < 4; i++)
663 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn);
664
665 return 0;
666}
667
668/*
669 * EGPRS PDTCH UL block decoding
670 */
671
672/*
673 * Type 3 - MCS-1,2,3,4
674 * Unmapping and deinterleaving
675 */
676static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
677{
678 int i;
679 sbit_t iB[456], q[8];
680
681 for (i = 0; i < 4; i++) {
682 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
683 q + i * 2, q + i * 2 + 1);
684 }
685
686 gsm0503_mcs1_ul_deinterleave(hc, dc, iB);
687
688 return 0;
689}
690
691/*
692 * Type 2 - MCS-5,6
693 * Unmapping and deinterleaving
694 */
695static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
696{
697 int i;
698 sbit_t burst[348];
699 sbit_t hi[EGPRS_HDR_HC_MAX];
700 sbit_t di[EGPRS_DATA_DC_MAX];
701
702 for (i = 0; i < 4; i++) {
703 memcpy(burst, &bursts[i * 348], 348);
704
705 gsm0503_mcs5_burst_swap(burst);
706 gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i);
707 }
708
709 gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di);
710
711 return 0;
712}
713
714/*
715 * Type 1 - MCS-7,8,9
716 * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique
717 */
718static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc,
719 sbit_t *c1, sbit_t *c2, int msc)
720{
721 int i;
722 sbit_t burst[348];
723 sbit_t hi[EGPRS_HDR_HC_MAX];
724 sbit_t di[EGPRS_DATA_C1 * 2];
725
726 for (i = 0; i < 4; i++) {
727 memcpy(burst, &bursts[i * 348], 348);
728
729 gsm0503_mcs5_burst_swap(burst);
730 gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i);
731 }
732
733 if (msc == EGPRS_MCS7)
734 gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di);
735 else
736 gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di);
737
738 return 0;
739}
740
741/*
742 * Decode EGPRS UL header section
743 *
744 * 1. Depuncture
745 * 2. Convolutional decoding
746 * 3. CRC check
747 */
748static int _egprs_decode_hdr(const sbit_t *hc, int mcs,
749 union gprs_rlc_ul_hdr_egprs *hdr)
750{
751 sbit_t C[EGPRS_HDR_C_MAX];
752 ubit_t upp[EGPRS_HDR_UPP_MAX];
753 int i, j, rc;
Harald Welte2f984ea2017-06-12 15:05:21 +0200754 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700755
756 code = &gsm0503_mcs_ul_codes[mcs];
757
758 /* Skip depuncturing on MCS-5,6 header */
759 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
760 memcpy(C, hc, code->hdr_code_len);
761 goto hdr_conv_decode;
762 }
763
764 if (!code->hdr_punc) {
765 /* Invalid MCS-X header puncture matrix */
766 return -1;
767 }
768
769 i = code->hdr_code_len - 1;
770 j = code->hdr_punc_len - 1;
771
772 for (; i >= 0; i--) {
773 if (!code->hdr_punc[i])
774 C[i] = hc[j--];
775 else
776 C[i] = 0;
777 }
778
779hdr_conv_decode:
780 osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL);
781 rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp,
782 code->hdr_len, upp + code->hdr_len);
783 if (rc)
784 return -1;
785
786 osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1);
787
788 return 0;
789}
790
791/*
792 * Blind MCS header decoding based on burst length and CRC validation.
793 * Ignore 'q' value coding identification. This approach provides
794 * the strongest chance of header recovery.
795 */
796static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr,
797 const sbit_t *bursts, uint16_t nbits)
798{
799 int rc;
800 sbit_t hc[EGPRS_HDR_HC_MAX];
801
802 if (nbits == GSM0503_GPRS_BURSTS_NBITS) {
803 /* MCS-1,2,3,4 */
804 egprs_type3_unmap(bursts, hc, NULL);
805 rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr);
806 if (!rc)
807 return EGPRS_HDR_TYPE3;
808 } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) {
809 /* MCS-5,6 */
810 egprs_type2_unmap(bursts, hc, NULL);
811 rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr);
812 if (!rc)
813 return EGPRS_HDR_TYPE2;
814
815 /* MCS-7,8,9 */
816 egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7);
817 rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr);
818 if (!rc)
819 return EGPRS_HDR_TYPE1;
820 }
821
822 return -1;
823}
824
825/*
826 * Parse EGPRS UL header for coding and puncturing scheme (CPS)
827 *
828 * Type 1 - MCS-7,8,9
829 * Type 2 - MCS-5,6
830 * Type 3 - MCS-1,2,3,4
831 */
832static int egprs_parse_ul_cps(struct egprs_cps *cps,
833 union gprs_rlc_ul_hdr_egprs *hdr, int type)
834{
835 uint8_t bits;
836
837 switch (type) {
838 case EGPRS_HDR_TYPE1:
839 bits = hdr->type1.cps;
840 break;
841 case EGPRS_HDR_TYPE2:
842 bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi;
843 break;
844 case EGPRS_HDR_TYPE3:
845 bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi;
846 break;
847 default:
848 return -1;
849 }
850
851 return egprs_get_cps(cps, type, bits);
852}
853
854/*
855 * Decode EGPRS UL data section
856 *
857 * 1. Depuncture
858 * 2. Convolutional decoding
859 * 3. CRC check
860 * 4. Block combining (MCS-7,8,9 only)
861 */
Harald Welteb9946d32017-06-12 09:40:16 +0200862static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700863 int mcs, int p, int blk, int *n_errors, int *n_bits_total)
864{
865 ubit_t u[EGPRS_DATA_U_MAX];
866 sbit_t C[EGPRS_DATA_C_MAX];
867
868 int i, j, rc, data_len;
Harald Welte2f984ea2017-06-12 15:05:21 +0200869 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700870
871 if (blk && mcs < EGPRS_MCS7) {
872 /* Invalid MCS-X block state */
873 return -1;
874 }
875
876 code = &gsm0503_mcs_ul_codes[mcs];
877 if (!code->data_punc[p]) {
878 /* Invalid MCS-X data puncture matrix */
879 return -1;
880 }
881
882 /*
883 * MCS-1,6 - single block processing
884 * MCS-7,9 - dual block processing
885 */
886 if (mcs >= EGPRS_MCS7)
887 data_len = code->data_len / 2;
888 else
889 data_len = code->data_len;
890
891 i = code->data_code_len - 1;
892 j = code->data_punc_len - 1;
893
894 for (; i >= 0; i--) {
895 if (!code->data_punc[p][i])
896 C[i] = c[j--];
897 else
898 C[i] = 0;
899 }
900
Alexander Chemeris147051f2018-07-14 21:02:29 +0200901 osmo_conv_decode_ber_punctured(code->data_conv, C, u,
902 n_errors, n_bits_total, code->data_punc[p]);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700903 rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u,
904 data_len, u + data_len);
905 if (rc)
906 return -1;
907
908 /* Offsets output pointer on the second block of Type 1 MCS */
909 osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len,
910 u, 0, data_len, 1);
911
912 /* Return the number of bytes required for the bit message */
Maxdd75bac2017-06-13 15:07:01 +0200913 return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700914}
915
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200916/*! Decode EGPRS UL message
Harald Weltec6636782017-06-12 14:59:37 +0200917 * 1. Header section decoding
918 * 2. Extract CPS settings
919 * 3. Burst unmapping and deinterleaving
920 * 4. Data section decoding
921 * \param[out] l2_data caller-allocated buffer for L2 Frame
922 * \param[in] bursts burst input data as soft unpacked bits
923 * \param[in] nbits number of bits in \a bursts
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +0700924 * \param usf_p Uplink State Flag, FIXME: not implemented
Harald Weltec6636782017-06-12 14:59:37 +0200925 * \param[out] n_errors number of detected bit-errors
Alexander Chemerised7d2dd2018-07-14 21:06:27 +0200926 * \param[out] n_bits_total total number of decoded bits
Vadim Yanitskiyccf9aad2023-12-08 01:11:40 +0700927 * \returns number of bytes decoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +0200928int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700929 uint8_t *usf_p, int *n_errors, int *n_bits_total)
930{
931 sbit_t dc[EGPRS_DATA_DC_MAX];
932 sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
933 int type, rc;
934 struct egprs_cps cps;
935 union gprs_rlc_ul_hdr_egprs *hdr;
936
Alexander Chemeris50f7d742018-07-14 21:07:27 +0200937 if (n_errors)
938 *n_errors = 0;
939 if (n_bits_total)
940 *n_bits_total = 0;
941
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700942 if ((nbits != GSM0503_GPRS_BURSTS_NBITS) &&
943 (nbits != GSM0503_EGPRS_BURSTS_NBITS)) {
944 /* Invalid EGPRS bit length */
Maxc8cf8202017-05-22 16:07:04 +0200945 return -EOVERFLOW;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700946 }
947
948 hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data;
949 type = egprs_decode_hdr(hdr, bursts, nbits);
950 if (egprs_parse_ul_cps(&cps, hdr, type) < 0)
Maxc8cf8202017-05-22 16:07:04 +0200951 return -EIO;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700952
953 switch (cps.mcs) {
Maxc8cf8202017-05-22 16:07:04 +0200954 case EGPRS_MCS0:
955 return -ENOTSUP;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700956 case EGPRS_MCS1:
957 case EGPRS_MCS2:
958 case EGPRS_MCS3:
959 case EGPRS_MCS4:
960 egprs_type3_unmap(bursts, NULL, dc);
961 break;
962 case EGPRS_MCS5:
963 case EGPRS_MCS6:
964 egprs_type2_unmap(bursts, NULL, dc);
965 break;
966 case EGPRS_MCS7:
967 case EGPRS_MCS8:
968 case EGPRS_MCS9:
969 egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs);
970 break;
971 default:
972 /* Invalid MCS-X */
Maxc8cf8202017-05-22 16:07:04 +0200973 return -EINVAL;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700974 }
975
976 /* Decode MCS-X block, where X = cps.mcs */
977 if (cps.mcs < EGPRS_MCS7) {
978 rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0],
979 0, n_errors, n_bits_total);
980 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200981 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700982 } else {
Alexander Chemeris94443262018-07-14 21:09:54 +0200983 /* Bit counters for the second block */
984 int n_errors2, n_bits_total2;
985
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700986 /* MCS-7,8,9 block 1 */
987 rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0],
988 0, n_errors, n_bits_total);
989 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200990 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700991
992 /* MCS-7,8,9 block 2 */
993 rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1],
Alexander Chemeris94443262018-07-14 21:09:54 +0200994 1, &n_errors2, &n_bits_total2);
995 if (n_errors)
996 *n_errors += n_errors2;
997 if (n_bits_total)
998 *n_bits_total += n_bits_total2;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700999 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +02001000 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001001 }
1002
1003 return rc;
1004}
1005
1006/*
1007 * GSM PDTCH block transcoding
1008 */
1009
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001010/*! Decode GPRS PDTCH
Harald Weltec6636782017-06-12 14:59:37 +02001011 * \param[out] l2_data caller-allocated buffer for L2 Frame
1012 * \param[in] bursts burst input data as soft unpacked bits
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001013 * \param[out] usf_p Uplink State Flag, only relevant for DL blocks
Harald Weltec6636782017-06-12 14:59:37 +02001014 * \param[out] n_errors number of detected bit-errors
1015 * \param[out] n_bits_total total number of dcoded bits
Vadim Yanitskiyccf9aad2023-12-08 01:11:40 +07001016 * \returns number of bytes decoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001017int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001018 int *n_errors, int *n_bits_total)
1019{
1020 sbit_t iB[456], cB[676], hl_hn[8];
1021 ubit_t conv[456];
1022 int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */
1023
1024 for (i = 0; i < 4; i++)
1025 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
1026 hl_hn + i * 2, hl_hn + i * 2 + 1);
1027
1028 for (i = 0; i < 4; i++) {
1029 for (j = 0, k = 0; j < 8; j++)
1030 k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j]));
1031
1032 if (i == 0 || k < best) {
1033 best = k;
1034 cs = i + 1;
1035 }
1036 }
1037
1038 gsm0503_xcch_deinterleave(cB, iB);
1039
1040 switch (cs) {
1041 case 1:
1042 osmo_conv_decode_ber(&gsm0503_xcch, cB,
1043 conv, n_errors, n_bits_total);
1044
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
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001064 /* 5.1.2.2 a) the three USF bits d(0),d(1),d(2) are precoded into six bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001065 for (i = 0; i < 8; i++) {
1066 for (j = 0, k = 0; j < 6; j++)
1067 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1068
1069 if (i == 0 || k < best) {
1070 best = k;
1071 usf = i;
1072 }
1073 }
1074
1075 conv[3] = usf & 1;
1076 conv[4] = (usf >> 1) & 1;
1077 conv[5] = (usf >> 2) & 1;
1078 if (usf_p)
1079 *usf_p = usf;
1080
1081 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1082 conv + 3, 271, conv + 3 + 271);
1083 if (rv)
1084 return -1;
1085
1086 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1087
1088 return 34;
1089 case 3:
1090 for (i = 675, j = 455; i >= 0; i--) {
1091 if (!gsm0503_puncture_cs3[i])
1092 cB[i] = cB[j--];
1093 else
1094 cB[i] = 0;
1095 }
1096
1097 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
1098 conv, n_errors, n_bits_total);
1099
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001100 /* 5.1.3.2 a) the three USF bits d(0),d(1),d(2) are precoded into six bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001101 for (i = 0; i < 8; i++) {
1102 for (j = 0, k = 0; j < 6; j++)
1103 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1104
1105 if (i == 0 || k < best) {
1106 best = k;
1107 usf = i;
1108 }
1109 }
1110
1111 conv[3] = usf & 1;
1112 conv[4] = (usf >> 1) & 1;
1113 conv[5] = (usf >> 2) & 1;
1114 if (usf_p)
1115 *usf_p = usf;
1116
1117 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1118 conv + 3, 315, conv + 3 + 315);
1119 if (rv)
1120 return -1;
1121
1122 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
1123
1124 return 40;
1125 case 4:
1126 for (i = 12; i < 456; i++)
1127 conv[i] = (cB[i] < 0) ? 1 : 0;
1128
Vadim Yanitskiy93b8eb32023-12-08 02:52:12 +07001129 /* 5.1.4.2 a) the three USF bits d(0),d(1),d(2) are precoded into twelve bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001130 for (i = 0; i < 8; i++) {
1131 for (j = 0, k = 0; j < 12; j++)
1132 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1133
1134 if (i == 0 || k < best) {
1135 best = k;
1136 usf = i;
1137 }
1138 }
1139
1140 conv[9] = usf & 1;
1141 conv[10] = (usf >> 1) & 1;
1142 conv[11] = (usf >> 2) & 1;
1143 if (usf_p)
1144 *usf_p = usf;
1145
1146 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1147 conv + 9, 431, conv + 9 + 431);
1148 if (rv) {
1149 *n_bits_total = 456 - 12;
1150 *n_errors = *n_bits_total;
1151 return -1;
1152 }
1153
1154 *n_bits_total = 456 - 12;
1155 *n_errors = 0;
1156
1157 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1158
1159 return 54;
1160 default:
1161 *n_bits_total = 0;
1162 *n_errors = 0;
1163 break;
1164 }
1165
1166 return -1;
1167}
1168
1169/*
Pau Espin Pedrolf62f0732020-04-07 13:12:11 +02001170 * EGPRS PDTCH DL block encoding
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001171 */
Harald Welteb9946d32017-06-12 09:40:16 +02001172static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001173{
1174 int i;
1175 ubit_t iB[456];
1176 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1177
Pau Espin Pedrol63ebc362020-04-07 13:15:36 +02001178 gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001179
1180 for (i = 0; i < 4; i++) {
1181 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1182 hl_hn + i * 2, hl_hn + i * 2 + 1);
1183 }
1184
1185 return 0;
1186}
1187
Harald Welteb9946d32017-06-12 09:40:16 +02001188static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001189{
1190 int i;
1191 const ubit_t *up;
1192 ubit_t hi[EGPRS_HDR_HC_MAX];
1193 ubit_t di[EGPRS_DATA_DC_MAX];
1194
1195 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1196 up = gsm0503_mcs5_usf_precode_table[usf];
1197
1198 for (i = 0; i < 4; i++) {
1199 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1200 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1201 }
1202
1203 return 0;
1204}
1205
Harald Welteb9946d32017-06-12 09:40:16 +02001206static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1207 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001208{
1209 int i;
1210 const ubit_t *up;
1211 ubit_t hi[EGPRS_HDR_HC_MAX];
1212 ubit_t di[EGPRS_DATA_C1 * 2];
1213
1214 if (mcs == EGPRS_MCS7)
1215 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1216 else
1217 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1218
1219 up = gsm0503_mcs5_usf_precode_table[usf];
1220
1221 for (i = 0; i < 4; i++) {
1222 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1223 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1224 }
1225
1226 return 0;
1227}
1228
Harald Welteb9946d32017-06-12 09:40:16 +02001229static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001230{
1231 int i, j;
1232 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001233 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001234
1235 code = &gsm0503_mcs_dl_codes[mcs];
1236
1237 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1238 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1239 code->hdr_len, upp + code->hdr_len);
1240
1241 osmo_conv_encode(code->hdr_conv, upp, C);
1242
1243 /* MCS-5,6 header direct puncture instead of table */
1244 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1245 memcpy(hc, C, code->hdr_code_len);
1246 hc[99] = hc[98];
1247 return 0;
1248 }
1249
1250 if (!code->hdr_punc) {
1251 /* Invalid MCS-X header puncture matrix */
1252 return -1;
1253 }
1254
1255 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1256 if (!code->hdr_punc[i])
1257 hc[j++] = C[i];
1258 }
1259
1260 return 0;
1261}
1262
Harald Welteb9946d32017-06-12 09:40:16 +02001263static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001264 int mcs, int p, int blk)
1265{
1266 int i, j, data_len;
1267 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001268 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001269
1270 code = &gsm0503_mcs_dl_codes[mcs];
1271
1272 /*
1273 * Dual block - MCS-7,8,9
1274 * Single block - MCS-1,2,3,4,5,6
1275 */
1276 if (mcs >= EGPRS_MCS7)
1277 data_len = code->data_len / 2;
1278 else
1279 data_len = code->data_len;
1280
1281 osmo_pbit2ubit_ext(u, 0, l2_data,
1282 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1283
1284 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1285
1286 osmo_conv_encode(code->data_conv, u, C);
1287
1288 if (!code->data_punc[p]) {
1289 /* Invalid MCS-X data puncture matrix */
1290 return -1;
1291 }
1292
1293 for (i = 0, j = 0; i < code->data_code_len; i++) {
1294 if (!code->data_punc[p][i])
1295 c[j++] = C[i];
1296 }
1297
1298 return 0;
1299}
1300
1301/*
1302 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1303 *
1304 * Type 1 - MCS-7,8,9
1305 * Type 2 - MCS-5,6
1306 * Type 3 - MCS-1,2,3,4
1307 */
1308static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001309 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001310{
1311 uint8_t bits;
1312
1313 switch (type) {
1314 case EGPRS_HDR_TYPE1:
1315 bits = hdr->type1.cps;
1316 break;
1317 case EGPRS_HDR_TYPE2:
1318 bits = hdr->type2.cps;
1319 break;
1320 case EGPRS_HDR_TYPE3:
1321 bits = hdr->type3.cps;
1322 break;
1323 default:
1324 return -1;
1325 }
1326
1327 return egprs_get_cps(cps, type, bits);
1328}
1329
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001330/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001331 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1332 * \param[in] l2_data L2 (MAC) block to be encoded
1333 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001334 * \returns number of bits encoded; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001335int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001336 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001337{
1338 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1339 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1340 uint8_t mcs;
1341 struct egprs_cps cps;
1342 union gprs_rlc_dl_hdr_egprs *hdr;
1343
1344 switch (l2_len) {
1345 case 27:
1346 mcs = EGPRS_MCS1;
1347 break;
1348 case 33:
1349 mcs = EGPRS_MCS2;
1350 break;
1351 case 42:
1352 mcs = EGPRS_MCS3;
1353 break;
1354 case 49:
1355 mcs = EGPRS_MCS4;
1356 break;
1357 case 60:
1358 mcs = EGPRS_MCS5;
1359 break;
1360 case 78:
1361 mcs = EGPRS_MCS6;
1362 break;
1363 case 118:
1364 mcs = EGPRS_MCS7;
1365 break;
1366 case 142:
1367 mcs = EGPRS_MCS8;
1368 break;
1369 case 154:
1370 mcs = EGPRS_MCS9;
1371 break;
1372 default:
1373 return -1;
1374 }
1375
1376 /* Read header for USF and puncturing matrix selection. */
1377 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1378
1379 switch (mcs) {
1380 case EGPRS_MCS1:
1381 case EGPRS_MCS2:
1382 case EGPRS_MCS3:
1383 case EGPRS_MCS4:
1384 /* Check for valid CPS and matching MCS to message size */
1385 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1386 (cps.mcs != mcs))
1387 goto bad_header;
1388
1389 egprs_encode_hdr(hc, l2_data, mcs);
1390 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1391 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1392 break;
1393 case EGPRS_MCS5:
1394 case EGPRS_MCS6:
1395 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1396 (cps.mcs != mcs))
1397 goto bad_header;
1398
1399 egprs_encode_hdr(hc, l2_data, mcs);
1400 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1401 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1402 break;
1403 case EGPRS_MCS7:
1404 case EGPRS_MCS8:
1405 case EGPRS_MCS9:
1406 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1407 (cps.mcs != mcs))
1408 goto bad_header;
1409
1410 egprs_encode_hdr(hc, l2_data, mcs);
1411 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1412 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1413 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1414 break;
1415 }
1416
1417 return mcs >= EGPRS_MCS5 ?
1418 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1419
1420bad_header:
1421 /* Invalid EGPRS MCS-X header */
1422 return -1;
1423}
1424
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001425/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001426 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1427 * \param[in] l2_data L2 (MAC) block to be encoded
1428 * \param[in] l2_len length of l2_data in bytes, used to determine CS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001429 * \returns number of bits encoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001430int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001431{
1432 ubit_t iB[456], cB[676];
1433 const ubit_t *hl_hn;
1434 ubit_t conv[334];
1435 int i, j, usf;
1436
1437 switch (l2_len) {
1438 case 23:
1439 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1440
1441 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1442
1443 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1444
1445 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1446
1447 break;
1448 case 34:
1449 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1450 usf = l2_data[0] & 0x7;
1451
1452 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1453 271, conv + 3 + 271);
1454
1455 memcpy(conv, gsm0503_usf2six[usf], 6);
1456
1457 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1458
1459 for (i = 0, j = 0; i < 588; i++)
1460 if (!gsm0503_puncture_cs2[i])
1461 cB[j++] = cB[i];
1462
1463 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1464
1465 break;
1466 case 40:
1467 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1468 usf = l2_data[0] & 0x7;
1469
1470 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1471 315, conv + 3 + 315);
1472
1473 memcpy(conv, gsm0503_usf2six[usf], 6);
1474
1475 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1476
1477 for (i = 0, j = 0; i < 676; i++)
1478 if (!gsm0503_puncture_cs3[i])
1479 cB[j++] = cB[i];
1480
1481 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1482
1483 break;
1484 case 54:
1485 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1486 usf = l2_data[0] & 0x7;
1487
1488 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1489 431, cB + 9 + 431);
1490
1491 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1492
1493 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1494
1495 break;
1496 default:
1497 return -1;
1498 }
1499
1500 gsm0503_xcch_interleave(cB, iB);
1501
1502 for (i = 0; i < 4; i++) {
1503 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1504 hl_hn + i * 2, hl_hn + i * 2 + 1);
1505 }
1506
1507 return GSM0503_GPRS_BURSTS_NBITS;
1508}
1509
1510/*
1511 * GSM TCH/F FR/EFR transcoding
1512 */
1513
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001514/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001515 * \param[out] tch_data Codec frame in RTP format
1516 * \param[in] b_bits Codec frame in 'native' format
1517 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001518static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001519 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001520{
1521 int i, j, k, l, o;
1522
1523 tch_data[0] = 0xd << 4;
1524 memset(tch_data + 1, 0, 32);
1525
1526 if (net_order) {
1527 for (i = 0, j = 4; i < 260; i++, j++)
1528 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1529
1530 return;
1531 }
1532
1533 /* reassemble d-bits */
1534 i = 0; /* counts bits */
1535 j = 4; /* counts output bits */
1536 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1537 l = 0; /* counts element bits */
1538 o = 0; /* offset input bits */
1539 while (i < 260) {
1540 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1541 if (--k < 0) {
1542 o += gsm0503_gsm_fr_map[l];
1543 k = gsm0503_gsm_fr_map[++l]-1;
1544 }
1545 i++;
1546 j++;
1547 }
1548}
1549
1550static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001551 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001552{
1553 int i, j, k, l, o;
1554
1555 if (net_order) {
1556 for (i = 0, j = 4; i < 260; i++, j++)
1557 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1558
1559 return;
1560 }
1561
1562 i = 0; /* counts bits */
1563 j = 4; /* counts input bits */
1564 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1565 l = 0; /* counts element bits */
1566 o = 0; /* offset output bits */
1567 while (i < 260) {
1568 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1569 if (--k < 0) {
1570 o += gsm0503_gsm_fr_map[l];
1571 k = gsm0503_gsm_fr_map[++l] - 1;
1572 }
1573 i++;
1574 j++;
1575 }
1576}
1577
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001578/* assemble a HR codec frame in the canonical format of ETSI TS 101 318 */
Harald Welteb9946d32017-06-12 09:40:16 +02001579static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001580{
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001581 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001582
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001583 memset(tch_data, 0, GSM_HR_BYTES);
1584 for (i = 0; i < 112; i++)
1585 tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7)));
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001586}
1587
Harald Welteb9946d32017-06-12 09:40:16 +02001588static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001589{
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001590 int i;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001591
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00001592 for (i = 0; i < 112; i++)
1593 b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001594}
1595
Harald Weltec6636782017-06-12 14:59:37 +02001596/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001597static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001598{
1599 int i, j;
1600
1601 tch_data[0] = 0xc << 4;
1602 memset(tch_data + 1, 0, 30);
1603
1604 for (i = 0, j = 4; i < 244; i++, j++)
1605 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1606}
1607
Harald Welteb9946d32017-06-12 09:40:16 +02001608static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001609{
1610 int i, j;
1611
1612 for (i = 0, j = 4; i < 244; i++, j++)
1613 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1614}
1615
Harald Weltec6636782017-06-12 14:59:37 +02001616/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001617static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001618{
1619 int i, j;
1620
1621 memset(tch_data, 0, (len + 7) >> 3);
1622
1623 for (i = 0, j = 0; i < len; i++, j++)
1624 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1625}
1626
Harald Welteb9946d32017-06-12 09:40:16 +02001627static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001628{
1629 int i, j;
1630
1631 for (i = 0, j = 0; i < len; i++, j++)
1632 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1633}
1634
Philipp Maier898c9c62020-02-06 14:25:01 +01001635/* Append STI and MI bits to the SID_UPDATE frame, see also
1636 * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */
1637static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi)
1638{
1639 /* Zero out the space that had been used by the CRC14 */
1640 memset(sid_update + 35, 0, 14);
1641
1642 /* Append STI and MI parameters */
1643 sid_update[35] = sti & 1;
1644 sid_update[36] = mi & 1;
1645 sid_update[37] = mi >> 1 & 1;
1646 sid_update[38] = mi >> 2 & 1;
1647}
1648
1649/* Extract a SID UPDATE fram the sbits of an FR AMR frame */
1650static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits)
1651{
1652
1653 unsigned int i;
1654
1655 sbits += 32;
1656
1657 for (i = 0; i < 53; i++) {
1658 sid_update[0] = sbits[0];
1659 sid_update[1] = sbits[1];
1660 sid_update[2] = sbits[2];
1661 sid_update[3] = sbits[3];
1662 sid_update += 4;
1663 sbits += 8;
1664 }
1665
1666}
1667
Harald Weltec6636782017-06-12 14:59:37 +02001668/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001669static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001670{
1671 int i;
1672
1673 for (i = 0; i < 260; i++)
1674 b_bits[gsm610_bitorder[i]] = d_bits[i];
1675}
1676
Harald Weltec6636782017-06-12 14:59:37 +02001677/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001678static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001679{
1680 int i;
1681
1682 for (i = 0; i < 260; i++)
1683 d_bits[i] = b_bits[gsm610_bitorder[i]];
1684}
1685
Harald Weltec6636782017-06-12 14:59:37 +02001686/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001687static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001688{
1689 int i;
1690
1691 const uint16_t *map;
1692
1693 if (!d_bits[93] && !d_bits[94])
1694 map = gsm620_unvoiced_bitorder;
1695 else
1696 map = gsm620_voiced_bitorder;
1697
1698 for (i = 0; i < 112; i++)
1699 b_bits[map[i]] = d_bits[i];
1700}
1701
Harald Weltec6636782017-06-12 14:59:37 +02001702/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001703static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001704{
1705 int i;
1706 const uint16_t *map;
1707
1708 if (!b_bits[34] && !b_bits[35])
1709 map = gsm620_unvoiced_bitorder;
1710 else
1711 map = gsm620_voiced_bitorder;
1712
1713 for (i = 0; i < 112; i++)
1714 d_bits[i] = b_bits[map[i]];
1715}
1716
Harald Weltec6636782017-06-12 14:59:37 +02001717/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001718static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001719{
1720 int i;
1721
1722 for (i = 0; i < 260; i++)
1723 b_bits[gsm660_bitorder[i]] = d_bits[i];
1724}
1725
Harald Weltec6636782017-06-12 14:59:37 +02001726/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001727static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001728{
1729 int i;
1730
1731 for (i = 0; i < 260; i++)
1732 d_bits[i] = b_bits[gsm660_bitorder[i]];
1733}
1734
Harald Weltec6636782017-06-12 14:59:37 +02001735/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001736static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001737{
1738 int i;
1739
1740 for (i = 0; i < 65; i++)
1741 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1742}
1743
Harald Welteb9946d32017-06-12 09:40:16 +02001744static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001745{
1746 int i;
1747
1748 for (i = 0; i < 91; i++) {
1749 d[i << 1] = u[i];
1750 d[(i << 1) + 1] = u[184 - i];
1751 }
1752
1753 for (i = 0; i < 3; i++)
1754 p[i] = u[91 + i];
1755}
1756
Harald Welteb9946d32017-06-12 09:40:16 +02001757static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001758{
1759 int i;
1760
1761 for (i = 0; i < 91; i++) {
1762 u[i] = d[i << 1];
1763 u[184 - i] = d[(i << 1) + 1];
1764 }
1765
1766 for (i = 0; i < 3; i++)
1767 u[91 + i] = p[i];
1768}
1769
Harald Welteb9946d32017-06-12 09:40:16 +02001770static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001771{
1772 memcpy(d, u, 95);
1773 memcpy(p, u + 95, 3);
1774}
1775
Harald Welteb9946d32017-06-12 09:40:16 +02001776static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001777{
1778 memcpy(u, d, 95);
1779 memcpy(u + 95, p, 3);
1780}
1781
Harald Welteb9946d32017-06-12 09:40:16 +02001782static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001783{
1784 memcpy(w, s, 71);
1785 w[71] = w[72] = s[69];
1786 memcpy(w + 73, s + 71, 50);
1787 w[123] = w[124] = s[119];
1788 memcpy(w + 125, s + 121, 53);
1789 w[178] = w[179] = s[172];
1790 memcpy(w + 180, s + 174, 50);
1791 w[230] = w[231] = s[222];
1792 memcpy(w + 232, s + 224, 20);
1793 memcpy(w + 252, p, 8);
1794}
1795
Harald Welteb9946d32017-06-12 09:40:16 +02001796static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001797{
1798 int sum;
1799
1800 memcpy(s, w, 71);
1801 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001802 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001803 memcpy(s + 71, w + 73, 50);
1804 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001805 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001806 memcpy(s + 121, w + 125, 53);
1807 sum = s[172] + w[178] + w[179];
Mychaela N. Falconiaf3e47452023-05-17 18:58:40 +00001808 s[172] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001809 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001810 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001811 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001812 memcpy(s + 224, w + 232, 20);
1813 memcpy(p, w + 252, 8);
1814}
1815
Harald Welteb9946d32017-06-12 09:40:16 +02001816static 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 +07001817{
1818 memcpy(u, d, prot);
1819 memcpy(u + prot, p, 6);
1820 memcpy(u + prot + 6, d + prot, len - prot);
1821}
1822
Harald Welteb9946d32017-06-12 09:40:16 +02001823static 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 +07001824{
1825 memcpy(d, u, prot);
1826 memcpy(p, u + prot, 6);
1827 memcpy(d + prot, u + prot + 6, len - prot);
1828}
1829
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001830/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001831 * \param[out] tch_data Codec frame in RTP payload format
1832 * \param[in] bursts buffer containing the symbols of 8 bursts
1833 * \param[in] net_order FIXME
1834 * \param[in] efr Is this channel using EFR (1) or FR (0)
1835 * \param[out] n_errors Number of detected bit errors
1836 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001837 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001838int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001839 int net_order, int efr, int *n_errors, int *n_bits_total)
1840{
1841 sbit_t iB[912], cB[456], h;
1842 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1843 int i, rv, len, steal = 0;
1844
Harald Weltec6636782017-06-12 14:59:37 +02001845 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001846 for (i = 0; i < 8; i++) {
1847 gsm0503_tch_burst_unmap(&iB[i * 114],
1848 &bursts[i * 116], &h, i >> 2);
1849 steal -= h;
1850 }
Harald Weltec6636782017-06-12 14:59:37 +02001851 /* we now have the bits of the four bursts (interface 4 in
1852 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001853
1854 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001855 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001856
1857 if (steal > 0) {
1858 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1859 if (rv) {
1860 /* Error decoding FACCH frame */
1861 return -1;
1862 }
1863
Vadim Yanitskiyc0305b62023-05-20 16:33:38 +07001864 return GSM_MACBLOCK_LEN;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001865 }
1866
1867 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001868 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001869
Harald Weltec6636782017-06-12 14:59:37 +02001870 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001871 tch_fr_unreorder(d, p, conv);
1872
1873 for (i = 0; i < 78; i++)
1874 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1875
Harald Weltec6636782017-06-12 14:59:37 +02001876 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001877 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1878 if (rv) {
1879 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1880 return -1;
1881 }
1882
1883 if (efr) {
1884 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001885 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001886
1887 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001888 /* we now have the data delivered to the preliminary
1889 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001890
Harald Weltec6636782017-06-12 14:59:37 +02001891 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001892 tch_efr_protected(s, b);
1893
Harald Weltec6636782017-06-12 14:59:37 +02001894 /* perform CRC-8 on 65 most important bits (50 bits of
1895 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001896 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1897 if (rv) {
1898 /* Error checking CRC8 for the EFR part of an EFR frame */
1899 return -1;
1900 }
1901
1902 tch_efr_reassemble(tch_data, s);
1903
1904 len = GSM_EFR_BYTES;
1905 } else {
1906 tch_fr_d_to_b(w, d);
1907
1908 tch_fr_reassemble(tch_data, w, net_order);
1909
1910 len = GSM_FR_BYTES;
1911 }
1912
1913 return len;
1914}
1915
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001916/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001917 * \param[out] bursts caller-allocated output buffer for bursts bits
1918 * \param[in] tch_data Codec input data in RTP payload format
1919 * \param[in] len Length of \a tch_data in bytes
1920 * \param[in] net_order FIXME
1921 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001922int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001923 int len, int net_order)
1924{
1925 ubit_t iB[912], cB[456], h;
1926 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1927 int i;
1928
1929 switch (len) {
1930 case GSM_EFR_BYTES: /* TCH EFR */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001931 tch_efr_disassemble(s, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001932 tch_efr_protected(s, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001933 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001934 tch_efr_reorder(w, s, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001935 tch_efr_w_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001936 goto coding_efr_fr;
1937 case GSM_FR_BYTES: /* TCH FR */
1938 tch_fr_disassemble(w, tch_data, net_order);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001939 tch_fr_b_to_d(d, w);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001940coding_efr_fr:
1941 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001942 tch_fr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001943 memcpy(cB + 378, d + 182, 78);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001944 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001945 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001946 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00001947 case 0: /* no data, induce BFI in the receiver */
1948 /* Do the same thing that sysmoBTS PHY does when fed a 0-length
1949 * payload for DL: set all u(k) bits to 0, and do the same
1950 * with all class 2 bits. This operation is NOT the same as
1951 * an FR codec frame of all zero bits: with all-zeros d(k) input
1952 * the CRC3 function will produce 111 output, whereas we
1953 * transmit 000 in those parity bits too. The result will be
1954 * an induced BFI (bad frame indication) condition in the
1955 * receiver, for both TCH/FS and TCH/EFS decoders. */
1956 memset(conv, 0, sizeof(conv));
1957 memset(cB + 378, 0, 78);
1958 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1959 h = 0;
1960 break;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001961 case GSM_MACBLOCK_LEN: /* FACCH */
1962 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001963 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001964 break;
1965 default:
1966 return -1;
1967 }
1968
1969 gsm0503_tch_fr_interleave(cB, iB);
1970
1971 for (i = 0; i < 8; i++) {
1972 gsm0503_tch_burst_map(&iB[i * 114],
1973 &bursts[i * 116], &h, i >> 2);
1974 }
1975
1976 return 0;
1977}
1978
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001979/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001980 * \param[out] tch_data Codec frame in TS 101 318 canonical format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07001981 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02001982 * \param[in] odd Odd (1) or even (0) frame number
1983 * \param[out] n_errors Number of detected bit errors
1984 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001985 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00001986int gsm0503_tch_hr_decode2(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001987 int *n_errors, int *n_bits_total)
1988{
1989 sbit_t iB[912], cB[456], h;
1990 ubit_t conv[98], b[112], d[112], p[3];
1991 int i, rv, steal = 0;
1992
1993 /* Only unmap the stealing bits */
1994 if (!odd) {
1995 for (i = 0; i < 4; i++) {
1996 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
1997 steal -= h;
1998 }
1999
Mychaela N. Falconia920c0ed2023-05-23 14:06:53 +00002000 for (i = 2; i < 6; i++) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002001 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2002 steal -= h;
2003 }
2004 }
2005
2006 /* If we found a stole FACCH, but only at correct alignment */
2007 if (steal > 0) {
2008 for (i = 0; i < 6; i++) {
2009 gsm0503_tch_burst_unmap(&iB[i * 114],
2010 &bursts[i * 116], NULL, i >> 2);
2011 }
2012
2013 for (i = 2; i < 4; i++) {
2014 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2015 &bursts[i * 116], NULL, 1);
2016 }
2017
2018 gsm0503_tch_fr_deinterleave(cB, iB);
2019
2020 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2021 if (rv) {
2022 /* Error decoding FACCH frame */
2023 return -1;
2024 }
2025
2026 return GSM_MACBLOCK_LEN;
2027 }
2028
2029 for (i = 0; i < 4; i++) {
2030 gsm0503_tch_burst_unmap(&iB[i * 114],
2031 &bursts[i * 116], NULL, i >> 1);
2032 }
2033
2034 gsm0503_tch_hr_deinterleave(cB, iB);
2035
2036 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
2037
2038 tch_hr_unreorder(d, p, conv);
2039
2040 for (i = 0; i < 17; i++)
2041 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
2042
2043 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2044 if (rv) {
2045 /* Error checking CRC8 for an HR frame */
2046 return -1;
2047 }
2048
2049 tch_hr_d_to_b(b, d);
2050
2051 tch_hr_reassemble(tch_data, b);
2052
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002053 return GSM_HR_BYTES;
2054}
2055
2056/*! Perform channel decoding of a HR(v1) channel according TS 05.03,
2057 * deprecated legacy API.
2058 * \param[out] tch_data Codec frame in pseudo-RFC5993 format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002059 * \param[in] bursts buffer containing the symbols of 6 bursts
Mychaela N. Falconia57a3b3a2023-05-23 16:09:27 +00002060 * \param[in] odd Odd (1) or even (0) frame number
2061 * \param[out] n_errors Number of detected bit errors
2062 * \param[out] n_bits_total Total number of bits
2063 * \returns length of bytes used in \a tch_data output buffer; negative on error
2064 *
2065 * The HR1 codec frame format returned by this function is pseudo-RFC5993,
2066 * not true RFC 5993, as there is no SID classification being done
2067 * and the FT bits in the ToC octet are always set to 0 - but this
2068 * arguably-bogus format is the legacy public API.
2069 */
2070int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
2071 int *n_errors, int *n_bits_total)
2072{
2073 int rc;
2074
2075 rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors,
2076 n_bits_total);
2077 if (rc != GSM_HR_BYTES)
2078 return rc;
2079 memmove(tch_data + 1, tch_data, GSM_HR_BYTES);
2080 tch_data[0] = 0x00; /* FT=0, note absence of SID classification */
2081 return GSM_HR_BYTES_RTP_RFC5993;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002082}
2083
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002084/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002085 * \param[out] bursts caller-allocated output buffer for bursts bits
2086 * \param[in] tch_data Codec input data in RTP payload format
2087 * \param[in] len Length of \a tch_data in bytes
2088 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002089int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002090{
2091 ubit_t iB[912], cB[456], h;
2092 ubit_t conv[98], b[112], d[112], p[3];
2093 int i;
2094
2095 switch (len) {
Mychaela N. Falconia8d8d5af2023-05-23 16:41:05 +00002096 case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */
2097 tch_data++;
2098 /* fall-through */
2099 case GSM_HR_BYTES: /* TCH HR in "pure" form */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002100 tch_hr_disassemble(b, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002101 tch_hr_b_to_d(d, b);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002102 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002103 tch_hr_reorder(conv, d, p);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002104 memcpy(cB + 211, d + 95, 17);
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002105hr_conv_coding:
2106 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002107 h = 0;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002108 gsm0503_tch_hr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002109 for (i = 0; i < 4; i++) {
2110 gsm0503_tch_burst_map(&iB[i * 114],
2111 &bursts[i * 116], &h, i >> 1);
2112 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002113 break;
Mychaela N. Falconia6e529a22023-05-27 11:06:57 +00002114 case 0: /* no data, induce BFI in the receiver */
2115 /* see comments in gsm0503_tch_fr_encode() - same deal here */
2116 memset(conv, 0, sizeof(conv));
2117 memset(cB + 211, 0, 17);
2118 goto hr_conv_coding;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002119 case GSM_MACBLOCK_LEN: /* FACCH */
2120 _xcch_encode_cB(cB, tch_data);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002121 h = 1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002122 gsm0503_tch_fr_interleave(cB, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002123 for (i = 0; i < 6; i++) {
2124 gsm0503_tch_burst_map(&iB[i * 114],
2125 &bursts[i * 116], &h, i >> 2);
2126 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002127 for (i = 2; i < 4; i++) {
2128 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2129 &bursts[i * 116], &h, 1);
2130 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002131 break;
2132 default:
2133 return -1;
2134 }
2135
2136 return 0;
2137}
2138
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002139/* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2140static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB)
2141{
2142 unsigned int id = 0, best = 0;
2143 unsigned int i, j, k;
2144
2145 for (i = 0; i < 4; i++) {
2146 /* FIXME: why not using remaining (16 - 8) soft-bits here? */
2147 for (j = 0, k = 0; j < 8; j++)
2148 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2149
2150 if (i == 0 || k < best) {
2151 best = k;
2152 id = i;
2153 }
2154 }
2155
2156 return id;
2157}
2158
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002159/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002160 * \param[out] tch_data Codec frame in RTP payload format
2161 * \param[in] bursts buffer containing the symbols of 8 bursts
2162 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2163 * \param[in] codec array of active codecs (active codec set)
2164 * \param[in] codecs number of codecs in \a codec
2165 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2166 * \param[out] cmr Output in \a codec_mode_req = 1
2167 * \param[out] n_errors Number of detected bit errors
2168 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002169 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2170 * codec out of range; negative on error
2171 */
Harald Welteb9946d32017-06-12 09:40:16 +02002172int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002173 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2174 uint8_t *cmr, int *n_errors, int *n_bits_total)
2175{
Philipp Maier898c9c62020-02-06 14:25:01 +01002176 return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req,
2177 codec, codecs, ft, cmr, n_errors,
2178 n_bits_total, NULL);
2179}
2180
2181/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2182 * \param[out] tch_data Codec frame in RTP payload format
2183 * \param[in] bursts buffer containing the symbols of 8 bursts
2184 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2185 * \param[in] codec array of active codecs (active codec set)
2186 * \param[in] codecs number of codecs in \a codec
2187 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2188 * \param[out] cmr Output in \a codec_mode_req = 1
2189 * \param[out] n_errors Number of detected bit errors
2190 * \param[out] n_bits_total Total number of bits
2191 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2192 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2193 * codec out of range; negative on error
2194 */
2195int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts,
2196 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2197 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2198{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002199 sbit_t iB[912], cB[456], h;
2200 ubit_t d[244], p[6], conv[250];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002201 int i, rv, len, steal = 0, id = -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002202 *n_errors = 0; *n_bits_total = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002203 static ubit_t sid_first_dummy[64] = { 0 };
2204 sbit_t sid_update_enc[256];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002205
2206 for (i=0; i<8; i++) {
2207 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2208 steal -= h;
2209 }
2210
2211 gsm0503_tch_fr_deinterleave(cB, iB);
2212
2213 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002214 /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002215 * It's normally updated by gsm0503_detect_afs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002216 * reached in case of FACCH. Reset it here to avoid FACCH/F frames being
2217 * misinterpreted as AMR's special DTX frames. */
2218 if (dtx != NULL)
2219 *dtx = AMR_OTHER;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002220 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2221 if (rv) {
2222 /* Error decoding FACCH frame */
2223 return -1;
2224 }
2225
2226 return GSM_MACBLOCK_LEN;
2227 }
2228
Philipp Maier898c9c62020-02-06 14:25:01 +01002229 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2230 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002231 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2232
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002233 *dtx = gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Philipp Maier898c9c62020-02-06 14:25:01 +01002234
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002235 switch (*dtx) {
2236 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002237 /* NOTE: The AFS_SID_UPDATE frame is splitted into
2238 * two half rate frames. If the id marker frame
2239 * (AFS_SID_UPDATE) is detected the following frame
2240 * contains the actual comfort noised data part of
2241 * (AFS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002242 if (dtx_prev != AFS_SID_UPDATE)
2243 break;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002244 /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002245 *dtx = AFS_SID_UPDATE_CN;
2246
2247 extract_afs_sid_update(sid_update_enc, cB);
2248 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2249 sid_update_enc, conv, n_errors,
2250 n_bits_total);
2251 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2252 35, conv + 35);
2253 if (rv != 0) {
2254 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2255 return -1;
2256 }
2257
2258 tch_amr_sid_update_append(conv, 1,
2259 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002260 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002261 tch_amr_reassemble(tch_data, conv, 39);
2262 len = 5;
2263 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002264 case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002265 tch_amr_sid_update_append(sid_first_dummy, 0,
2266 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002267 : codec[id > 0 ? id : 0]);
Vadim Yanitskiy7d121cb2023-06-23 16:57:48 +07002268 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
Philipp Maier898c9c62020-02-06 14:25:01 +01002269 len = 5;
2270 goto out;
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002271 case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002272 case AFS_ONSET:
Philipp Maier898c9c62020-02-06 14:25:01 +01002273 len = 0;
2274 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002275 default:
2276 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002277 }
2278 }
2279
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002280 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2281 if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002282 /* Codec mode out of range, return id */
2283 return id;
2284 }
2285
2286 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2287 case 7: /* TCH/AFS12.2 */
2288 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2289 conv, n_errors, n_bits_total);
2290
2291 tch_amr_unmerge(d, p, conv, 244, 81);
2292
2293 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2294 if (rv) {
2295 /* Error checking CRC8 for an AMR 12.2 frame */
2296 return -1;
2297 }
2298
2299 tch_amr_reassemble(tch_data, d, 244);
2300
2301 len = 31;
2302
2303 break;
2304 case 6: /* TCH/AFS10.2 */
2305 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2306 conv, n_errors, n_bits_total);
2307
2308 tch_amr_unmerge(d, p, conv, 204, 65);
2309
2310 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2311 if (rv) {
2312 /* Error checking CRC8 for an AMR 10.2 frame */
2313 return -1;
2314 }
2315
2316 tch_amr_reassemble(tch_data, d, 204);
2317
2318 len = 26;
2319
2320 break;
2321 case 5: /* TCH/AFS7.95 */
2322 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2323 conv, n_errors, n_bits_total);
2324
2325 tch_amr_unmerge(d, p, conv, 159, 75);
2326
2327 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2328 if (rv) {
2329 /* Error checking CRC8 for an AMR 7.95 frame */
2330 return -1;
2331 }
2332
2333 tch_amr_reassemble(tch_data, d, 159);
2334
2335 len = 20;
2336
2337 break;
2338 case 4: /* TCH/AFS7.4 */
2339 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2340 conv, n_errors, n_bits_total);
2341
2342 tch_amr_unmerge(d, p, conv, 148, 61);
2343
2344 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2345 if (rv) {
2346 /* Error checking CRC8 for an AMR 7.4 frame */
2347 return -1;
2348 }
2349
2350 tch_amr_reassemble(tch_data, d, 148);
2351
2352 len = 19;
2353
2354 break;
2355 case 3: /* TCH/AFS6.7 */
2356 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2357 conv, n_errors, n_bits_total);
2358
2359 tch_amr_unmerge(d, p, conv, 134, 55);
2360
2361 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2362 if (rv) {
2363 /* Error checking CRC8 for an AMR 6.7 frame */
2364 return -1;
2365 }
2366
2367 tch_amr_reassemble(tch_data, d, 134);
2368
2369 len = 17;
2370
2371 break;
2372 case 2: /* TCH/AFS5.9 */
2373 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2374 conv, n_errors, n_bits_total);
2375
2376 tch_amr_unmerge(d, p, conv, 118, 55);
2377
2378 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2379 if (rv) {
2380 /* Error checking CRC8 for an AMR 5.9 frame */
2381 return -1;
2382 }
2383
2384 tch_amr_reassemble(tch_data, d, 118);
2385
2386 len = 15;
2387
2388 break;
2389 case 1: /* TCH/AFS5.15 */
2390 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2391 conv, n_errors, n_bits_total);
2392
2393 tch_amr_unmerge(d, p, conv, 103, 49);
2394
2395 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2396 if (rv) {
2397 /* Error checking CRC8 for an AMR 5.15 frame */
2398 return -1;
2399 }
2400
2401 tch_amr_reassemble(tch_data, d, 103);
2402
2403 len = 13;
2404
2405 break;
2406 case 0: /* TCH/AFS4.75 */
2407 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2408 conv, n_errors, n_bits_total);
2409
2410 tch_amr_unmerge(d, p, conv, 95, 39);
2411
2412 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2413 if (rv) {
2414 /* Error checking CRC8 for an AMR 4.75 frame */
2415 return -1;
2416 }
2417
2418 tch_amr_reassemble(tch_data, d, 95);
2419
2420 len = 12;
2421
2422 break;
2423 default:
2424 /* Unknown frame type */
2425 *n_bits_total = 448;
2426 *n_errors = *n_bits_total;
2427 return -1;
2428 }
2429
Philipp Maier898c9c62020-02-06 14:25:01 +01002430out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002431 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002432 if (id != -1) {
2433 if (codec_mode_req)
2434 *cmr = id;
2435 else
2436 *ft = id;
2437 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002438
2439 return len;
2440}
2441
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002442/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002443 * \param[out] bursts caller-allocated output buffer for bursts bits
2444 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002445 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002446 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2447 * \param[in] codec Array of codecs (active codec set)
2448 * \param[in] codecs Number of entries in \a codec
2449 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2450 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2451 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002452int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002453 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002454 uint8_t cmr)
2455{
2456 ubit_t iB[912], cB[456], h;
2457 ubit_t d[244], p[6], conv[250];
2458 int i;
2459 uint8_t id;
2460
2461 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2462 _xcch_encode_cB(cB, tch_data);
2463
2464 h = 1;
2465
2466 goto facch;
2467 }
2468
2469 h = 0;
2470
Vadim Yanitskiya9952692023-11-10 03:31:55 +07002471 id = codec_mode_req ? cmr : ft;
2472 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit)))
2473 return -1;
2474 if (OSMO_UNLIKELY(ft >= codecs))
2475 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002476
2477 switch (codec[ft]) {
2478 case 7: /* TCH/AFS12.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002479 if (!len) {
2480 /* No data, induce BFI in the receiver by inverted CRC bits.
2481 * The data bit are all 0, so the correct parity bits would be 111111. */
2482 memset(d, 0, 244);
2483 memset(p, 0, 6);
2484 } else {
2485 if (len != 31)
2486 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002487
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002488 tch_amr_disassemble(d, tch_data, 244);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002489
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002490 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2491 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002492
2493 tch_amr_merge(conv, d, p, 244, 81);
2494
2495 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2496
2497 break;
2498 case 6: /* TCH/AFS10.2 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002499 if (!len) {
2500 /* See comment above. */
2501 memset(d, 0, 204);
2502 memset(p, 0, 6);
2503 } else {
2504 if (len != 26)
2505 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002506
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002507 tch_amr_disassemble(d, tch_data, 204);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002508
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002509 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2510 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002511
2512 tch_amr_merge(conv, d, p, 204, 65);
2513
2514 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2515
2516 break;
2517 case 5: /* TCH/AFS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002518 if (!len) {
2519 /* See comment above. */
2520 memset(d, 0, 159);
2521 memset(p, 0, 6);
2522 } else {
2523 if (len != 20)
2524 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002525
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002526 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002527
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002528 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2529 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002530
2531 tch_amr_merge(conv, d, p, 159, 75);
2532
2533 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2534
2535 break;
2536 case 4: /* TCH/AFS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002537 if (!len) {
2538 /* See comment above. */
2539 memset(d, 0, 148);
2540 memset(p, 0, 6);
2541 } else {
2542 if (len != 19)
2543 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002544
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002545 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002546
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002547 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2548 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002549
2550 tch_amr_merge(conv, d, p, 148, 61);
2551
2552 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2553
2554 break;
2555 case 3: /* TCH/AFS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002556 if (!len) {
2557 /* See comment above. */
2558 memset(d, 0, 134);
2559 memset(p, 0, 6);
2560 } else {
2561 if (len != 17)
2562 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002563
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002564 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002565
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002566 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2567 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002568
2569 tch_amr_merge(conv, d, p, 134, 55);
2570
2571 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2572
2573 break;
2574 case 2: /* TCH/AFS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002575 if (!len) {
2576 /* See comment above. */
2577 memset(d, 0, 118);
2578 memset(p, 0, 6);
2579 } else {
2580 if (len != 15)
2581 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002582
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002583 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002584
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002585 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2586 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002587
2588 tch_amr_merge(conv, d, p, 118, 55);
2589
2590 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2591
2592 break;
2593 case 1: /* TCH/AFS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002594 if (!len) {
2595 /* See comment above. */
2596 memset(d, 0, 103);
2597 memset(p, 0, 6);
2598 } else {
2599 if (len != 13)
2600 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002601
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002602 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002603
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002604 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2605 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002606
2607 tch_amr_merge(conv, d, p, 103, 49);
2608
2609 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2610
2611 break;
2612 case 0: /* TCH/AFS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002613 if (!len) {
2614 /* See comment above. */
2615 memset(d, 0, 95);
2616 memset(p, 0, 6);
2617 } else {
2618 if (len != 12)
2619 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002620
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002621 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002622
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002623 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2624 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002625
2626 tch_amr_merge(conv, d, p, 95, 39);
2627
2628 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2629
2630 break;
2631 default:
2632 /* FIXME: FT %ft is not supported */
2633 return -1;
2634 }
2635
2636 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2637
2638facch:
2639 gsm0503_tch_fr_interleave(cB, iB);
2640
2641 for (i = 0; i < 8; i++) {
2642 gsm0503_tch_burst_map(&iB[i * 114],
2643 &bursts[i * 116], &h, i >> 2);
2644 }
2645
2646 return 0;
2647
2648invalid_length:
2649 /* FIXME: payload length %len does not comply with codec type %ft */
2650 return -1;
2651}
2652
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002653/* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */
2654static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB)
2655{
2656 unsigned int id = 0, best = 0;
2657 unsigned int i, j, k;
2658
2659 for (i = 0, k = 0; i < 4; i++) {
2660 /* FIXME: why not using remaining (16 - 4) soft-bits here? */
2661 for (j = 0, k = 0; j < 4; j++)
2662 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2663
2664 if (i == 0 || k < best) {
2665 best = k;
2666 id = i;
2667 }
2668 }
2669
2670 return id;
2671}
2672
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002673/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002674 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002675 * \param[in] bursts buffer containing the symbols of 6 bursts
Harald Weltec6636782017-06-12 14:59:37 +02002676 * \param[in] odd Is this an odd (1) or even (0) frame number?
2677 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2678 * \param[in] codec array of active codecs (active codec set)
2679 * \param[in] codecs number of codecs in \a codec
2680 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2681 * \param[out] cmr Output in \a codec_mode_req = 1
2682 * \param[out] n_errors Number of detected bit errors
2683 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002684 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2685 * codec out of range; negative on error
2686 */
Harald Welteb9946d32017-06-12 09:40:16 +02002687int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002688 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2689 uint8_t *cmr, int *n_errors, int *n_bits_total)
2690{
Philipp Maier898c9c62020-02-06 14:25:01 +01002691 return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req,
2692 codec, codecs, ft, cmr, n_errors,
2693 n_bits_total, NULL);
2694}
2695
Vadim Yanitskiy8fb56472023-05-24 01:26:02 +07002696/*! Perform channel decoding of a TCH/AHS channel according TS 05.03
Philipp Maier898c9c62020-02-06 14:25:01 +01002697 * \param[out] tch_data Codec frame in RTP payload format
Vadim Yanitskiy98431bb2023-05-24 01:21:46 +07002698 * \param[in] bursts buffer containing the symbols of 6 bursts
Philipp Maier898c9c62020-02-06 14:25:01 +01002699 * \param[in] odd Is this an odd (1) or even (0) frame number?
2700 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2701 * \param[in] codec array of active codecs (active codec set)
2702 * \param[in] codecs number of codecs in \a codec
2703 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2704 * \param[out] cmr Output in \a codec_mode_req = 1
2705 * \param[out] n_errors Number of detected bit errors
2706 * \param[out] n_bits_total Total number of bits
2707 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2708 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2709 * codec out of range; negative on error
2710 */
2711int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd,
2712 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2713 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2714{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002715 sbit_t iB[912], cB[456], h;
2716 ubit_t d[244], p[6], conv[135];
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002717 int i, rv, len, steal = 0, id = -1;
Philipp Maier898c9c62020-02-06 14:25:01 +01002718 static ubit_t sid_first_dummy[64] = { 0 };
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002719
2720 /* only unmap the stealing bits */
2721 if (!odd) {
2722 for (i = 0; i < 4; i++) {
2723 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2724 steal -= h;
2725 }
2726 for (i = 2; i < 5; i++) {
2727 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2728 steal -= h;
2729 }
2730 }
2731
2732 /* if we found a stole FACCH, but only at correct alignment */
2733 if (steal > 0) {
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002734 /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame.
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002735 * It's normally updated by gsm0503_detect_ahs_dtx_frame2(), which is not
Vadim Yanitskiya2bee8b2022-05-16 19:40:45 +03002736 * reached in case of FACCH. Reset it here to avoid FACCH/H frames being
2737 * misinterpreted as AMR's special DTX frames. */
2738 if (dtx != NULL)
2739 *dtx = AMR_OTHER;
2740
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002741 for (i = 0; i < 6; i++) {
2742 gsm0503_tch_burst_unmap(&iB[i * 114],
2743 &bursts[i * 116], NULL, i >> 2);
2744 }
2745
2746 for (i = 2; i < 4; i++) {
2747 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2748 &bursts[i * 116], NULL, 1);
2749 }
2750
2751 gsm0503_tch_fr_deinterleave(cB, iB);
2752
2753 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2754 if (rv) {
2755 /* Error decoding FACCH frame */
2756 return -1;
2757 }
2758
2759 return GSM_MACBLOCK_LEN;
2760 }
2761
2762 for (i = 0; i < 4; i++) {
2763 gsm0503_tch_burst_unmap(&iB[i * 114],
2764 &bursts[i * 116], NULL, i >> 1);
2765 }
2766
2767 gsm0503_tch_hr_deinterleave(cB, iB);
2768
Philipp Maier898c9c62020-02-06 14:25:01 +01002769 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2770 if (dtx) {
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002771 int n_bits_total_sid;
2772 int n_errors_sid;
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002773
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002774 *dtx = gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &id, cB);
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002775 /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */
Philipp Maier898c9c62020-02-06 14:25:01 +01002776
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002777 switch (*dtx) {
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002778 case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002779 /* cB[] contains 16 bits of coded in-band data and 212 bits containing
2780 * the identification marker. We need to unmap/deinterleave 114 odd
2781 * bits from the last two blocks, 114 even bits from the first two
2782 * blocks and combine them together. */
2783 gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0);
2784 gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0);
2785 gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1);
2786 gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1);
2787 gsm0503_tch_hr_deinterleave(cB, iB);
2788
2789 /* cB[] is expected to contain 16 bits of coded in-band data and
2790 * 212 bits containing the coded data (53 bits coded at 1/4 rate). */
Philipp Maier898c9c62020-02-06 14:25:01 +01002791 *dtx = AHS_SID_UPDATE_CN;
2792
2793 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002794 cB + 16, conv, &n_errors_sid,
2795 &n_bits_total_sid);
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002796 /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker,
Vadim Yanitskiy77904592022-05-19 17:43:57 +03002797 * osmo_conv_decode_ber() calculates BER for the coded data. */
2798 if (n_errors != NULL)
2799 *n_errors += n_errors_sid;
2800 if (n_bits_total != NULL)
2801 *n_bits_total += n_bits_total_sid;
Philipp Maier898c9c62020-02-06 14:25:01 +01002802 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2803 35, conv + 35);
2804 if (rv != 0) {
2805 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2806 return -1;
2807 }
2808
2809 tch_amr_sid_update_append(conv, 1,
2810 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002811 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002812 tch_amr_reassemble(tch_data, conv, 39);
2813 len = 5;
2814 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002815 case AHS_SID_FIRST_P2:
Philipp Maier898c9c62020-02-06 14:25:01 +01002816 tch_amr_sid_update_append(sid_first_dummy, 0,
2817 (codec_mode_req) ? codec[*ft]
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002818 : codec[id > 0 ? id : 0]);
Philipp Maier898c9c62020-02-06 14:25:01 +01002819 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
2820 len = 5;
2821 goto out;
Vadim Yanitskiycc4213e2022-05-24 04:17:23 +07002822 case AHS_ONSET:
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002823 case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2824 case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */
2825 case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */
Philipp Maier898c9c62020-02-06 14:25:01 +01002826 len = 0;
2827 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002828 default:
2829 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002830 }
2831 }
2832
Vadim Yanitskiyb57e6392022-05-24 02:51:50 +07002833 /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */
2834 if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002835 /* Codec mode out of range, return id */
2836 return id;
2837 }
2838
2839 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2840 case 5: /* TCH/AHS7.95 */
2841 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2842 conv, n_errors, n_bits_total);
2843
2844 tch_amr_unmerge(d, p, conv, 123, 67);
2845
2846 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2847 if (rv) {
2848 /* Error checking CRC8 for an AMR 7.95 frame */
2849 return -1;
2850 }
2851
2852 for (i = 0; i < 36; i++)
2853 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2854
2855 tch_amr_reassemble(tch_data, d, 159);
2856
2857 len = 20;
2858
2859 break;
2860 case 4: /* TCH/AHS7.4 */
2861 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2862 conv, n_errors, n_bits_total);
2863
2864 tch_amr_unmerge(d, p, conv, 120, 61);
2865
2866 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2867 if (rv) {
2868 /* Error checking CRC8 for an AMR 7.4 frame */
2869 return -1;
2870 }
2871
2872 for (i = 0; i < 28; i++)
2873 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2874
2875 tch_amr_reassemble(tch_data, d, 148);
2876
2877 len = 19;
2878
2879 break;
2880 case 3: /* TCH/AHS6.7 */
2881 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2882 conv, n_errors, n_bits_total);
2883
2884 tch_amr_unmerge(d, p, conv, 110, 55);
2885
2886 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2887 if (rv) {
2888 /* Error checking CRC8 for an AMR 6.7 frame */
2889 return -1;
2890 }
2891
2892 for (i = 0; i < 24; i++)
2893 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2894
2895 tch_amr_reassemble(tch_data, d, 134);
2896
2897 len = 17;
2898
2899 break;
2900 case 2: /* TCH/AHS5.9 */
2901 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2902 conv, n_errors, n_bits_total);
2903
2904 tch_amr_unmerge(d, p, conv, 102, 55);
2905
2906 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2907 if (rv) {
2908 /* Error checking CRC8 for an AMR 5.9 frame */
2909 return -1;
2910 }
2911
2912 for (i = 0; i < 16; i++)
2913 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2914
2915 tch_amr_reassemble(tch_data, d, 118);
2916
2917 len = 15;
2918
2919 break;
2920 case 1: /* TCH/AHS5.15 */
2921 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2922 conv, n_errors, n_bits_total);
2923
2924 tch_amr_unmerge(d, p, conv, 91, 49);
2925
2926 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2927 if (rv) {
2928 /* Error checking CRC8 for an AMR 5.15 frame */
2929 return -1;
2930 }
2931
2932 for (i = 0; i < 12; i++)
2933 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2934
2935 tch_amr_reassemble(tch_data, d, 103);
2936
2937 len = 13;
2938
2939 break;
2940 case 0: /* TCH/AHS4.75 */
2941 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2942 conv, n_errors, n_bits_total);
2943
2944 tch_amr_unmerge(d, p, conv, 83, 39);
2945
2946 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2947 if (rv) {
2948 /* Error checking CRC8 for an AMR 4.75 frame */
2949 return -1;
2950 }
2951
2952 for (i = 0; i < 12; i++)
2953 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2954
2955 tch_amr_reassemble(tch_data, d, 95);
2956
2957 len = 12;
2958
2959 break;
2960 default:
2961 /* Unknown frame type */
2962 *n_bits_total = 159;
2963 *n_errors = *n_bits_total;
2964 return -1;
2965 }
2966
Philipp Maier898c9c62020-02-06 14:25:01 +01002967out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002968 /* Change codec request / indication, if frame is valid */
Vadim Yanitskiyd6dca0c2022-05-21 18:36:54 +03002969 if (id != -1) {
2970 if (codec_mode_req)
2971 *cmr = id;
2972 else
2973 *ft = id;
2974 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002975
2976 return len;
2977}
2978
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002979/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002980 * \param[out] bursts caller-allocated output buffer for bursts bits
2981 * \param[in] tch_data Codec input data in RTP payload format
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01002982 * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame
Harald Weltec6636782017-06-12 14:59:37 +02002983 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2984 * \param[in] codec Array of codecs (active codec set)
2985 * \param[in] codecs Number of entries in \a codec
2986 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2987 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2988 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002989int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiyd739e252023-11-10 04:22:32 +07002990 int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002991 uint8_t cmr)
2992{
2993 ubit_t iB[912], cB[456], h;
2994 ubit_t d[244], p[6], conv[135];
2995 int i;
2996 uint8_t id;
2997
2998 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2999 _xcch_encode_cB(cB, tch_data);
3000
3001 h = 1;
3002
3003 gsm0503_tch_fr_interleave(cB, iB);
3004
3005 for (i = 0; i < 6; i++)
3006 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
3007 &h, i >> 2);
3008 for (i = 2; i < 4; i++)
3009 gsm0503_tch_burst_map(&iB[i * 114 + 456],
3010 &bursts[i * 116], &h, 1);
3011
3012 return 0;
3013 }
3014
3015 h = 0;
3016
Vadim Yanitskiya9952692023-11-10 03:31:55 +07003017 id = codec_mode_req ? cmr : ft;
3018 if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit)))
3019 return -1;
3020 if (OSMO_UNLIKELY(ft >= codecs))
3021 return -1;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003022
3023 switch (codec[ft]) {
3024 case 5: /* TCH/AHS7.95 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003025 if (!len) {
3026 /* No data, induce BFI in the receiver by inverted CRC bits.
3027 * The data bit are all 0, so the correct parity bits would be 111111. */
3028 memset(d, 0, 159);
3029 memset(p, 0, 6);
3030 } else {
3031 if (len != 20)
3032 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003033
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003034 tch_amr_disassemble(d, tch_data, 159);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003035
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003036 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
3037 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003038
3039 tch_amr_merge(conv, d, p, 123, 67);
3040
3041 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
3042
3043 memcpy(cB + 192, d + 123, 36);
3044
3045 break;
3046 case 4: /* TCH/AHS7.4 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003047 if (!len) {
3048 /* See comment above. */
3049 memset(d, 0, 148);
3050 memset(p, 0, 6);
3051 } else {
3052 if (len != 19)
3053 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003054
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003055 tch_amr_disassemble(d, tch_data, 148);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003056
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003057 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
3058 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003059
3060 tch_amr_merge(conv, d, p, 120, 61);
3061
3062 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
3063
3064 memcpy(cB + 200, d + 120, 28);
3065
3066 break;
3067 case 3: /* TCH/AHS6.7 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003068 if (!len) {
3069 /* See comment above. */
3070 memset(d, 0, 134);
3071 memset(p, 0, 6);
3072 } else {
3073 if (len != 17)
3074 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003075
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003076 tch_amr_disassemble(d, tch_data, 134);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003077
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003078 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3079 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003080
3081 tch_amr_merge(conv, d, p, 110, 55);
3082
3083 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
3084
3085 memcpy(cB + 204, d + 110, 24);
3086
3087 break;
3088 case 2: /* TCH/AHS5.9 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003089 if (!len) {
3090 /* See comment above. */
3091 memset(d, 0, 118);
3092 memset(p, 0, 6);
3093 } else {
3094 if (len != 15)
3095 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003096
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003097 tch_amr_disassemble(d, tch_data, 118);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003098
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003099 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
3100 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003101
3102 tch_amr_merge(conv, d, p, 102, 55);
3103
3104 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
3105
3106 memcpy(cB + 212, d + 102, 16);
3107
3108 break;
3109 case 1: /* TCH/AHS5.15 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003110 if (!len) {
3111 /* See comment above. */
3112 memset(d, 0, 103);
3113 memset(p, 0, 6);
3114 } else {
3115 if (len != 13)
3116 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003117
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003118 tch_amr_disassemble(d, tch_data, 103);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003119
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003120 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
3121 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003122
3123 tch_amr_merge(conv, d, p, 91, 49);
3124
3125 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
3126
3127 memcpy(cB + 216, d + 91, 12);
3128
3129 break;
3130 case 0: /* TCH/AHS4.75 */
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003131 if (!len) {
3132 /* See comment above. */
3133 memset(d, 0, 95);
3134 memset(p, 0, 6);
3135 } else {
3136 if (len != 12)
3137 goto invalid_length;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003138
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003139 tch_amr_disassemble(d, tch_data, 95);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003140
Andreas Eversbergebdf3dc2023-11-24 16:52:21 +01003141 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
3142 }
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003143
3144 tch_amr_merge(conv, d, p, 83, 39);
3145
3146 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
3147
3148 memcpy(cB + 216, d + 83, 12);
3149
3150 break;
3151 default:
3152 /* FIXME: FT %ft is not supported */
3153 return -1;
3154 }
3155
Philipp Maiercfea39b2021-08-31 16:02:31 +02003156 memcpy(cB, gsm0503_ahs_ic_ubit[id], 4);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003157
3158 gsm0503_tch_hr_interleave(cB, iB);
3159
3160 for (i = 0; i < 4; i++)
3161 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
3162
3163 return 0;
3164
3165invalid_length:
3166 /* FIXME: payload length %len does not comply with codec type %ft */
3167 return -1;
3168}
3169
3170/*
3171 * GSM RACH transcoding
3172 */
3173
3174/*
3175 * GSM RACH apply BSIC to parity
3176 *
3177 * p(j) = p(j) xor b(j) j = 0, ..., 5
3178 * b(0) = MSB of PLMN colour code
3179 * b(5) = LSB of BS colour code
3180 */
Max32e56412017-10-16 14:58:00 +02003181static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003182{
3183 int i;
3184
3185 /* Apply it */
3186 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02003187 d[start + i] ^= ((bsic >> (5 - i)) & 1);
3188}
3189
Harald Welte6950b192018-02-26 11:48:00 +01003190static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
3191 int *n_errors, int *n_bits_total)
Max32e56412017-10-16 14:58:00 +02003192{
3193 ubit_t conv[17];
3194 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
3195 int rv;
3196
Harald Welte6950b192018-02-26 11:48:00 +01003197 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
3198 n_errors, n_bits_total);
Max32e56412017-10-16 14:58:00 +02003199
3200 rach_apply_bsic(conv, bsic, nbits);
3201
3202 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3203 if (rv)
3204 return -1;
3205
3206 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
3207
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003208 return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0];
Max32e56412017-10-16 14:58:00 +02003209}
3210
3211/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3212 * \param[out] ra output buffer for RACH data
3213 * \param[in] burst Input burst data
3214 * \param[in] bsic BSIC used in this cell
3215 * \returns 0 on success; negative on error (e.g. CRC error) */
3216int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
3217{
Harald Welte6950b192018-02-26 11:48:00 +01003218 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
Max32e56412017-10-16 14:58:00 +02003219
3220 if (r < 0)
3221 return r;
3222
3223 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003224
3225 return 0;
3226}
3227
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003228/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003229 * \param[out] ra output buffer for RACH data
3230 * \param[in] burst Input burst data
3231 * \param[in] bsic BSIC used in this cell
3232 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003233int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003234{
Harald Welte6950b192018-02-26 11:48:00 +01003235 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
3236 if (r < 0)
3237 return r;
3238
3239 *ra = r;
3240 return 0;
3241}
3242
3243/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3244 * \param[out] ra output buffer for RACH data
3245 * \param[in] burst Input burst data
3246 * \param[in] bsic BSIC used in this cell
3247 * \param[out] n_errors Number of detected bit errors
3248 * \param[out] n_bits_total Total number of bits
3249 * \returns 0 on success; negative on error (e.g. CRC error) */
3250int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
3251 int *n_errors, int *n_bits_total)
3252{
3253 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
3254 if (r < 0)
3255 return r;
3256
3257 *ra = r;
3258 return 0;
3259}
3260
3261/*! Decode the (8-bit) RACH according to TS 05.03
3262 * \param[out] ra output buffer for RACH data
3263 * \param[in] burst Input burst data
3264 * \param[in] bsic BSIC used in this cell
3265 * \param[out] n_errors Number of detected bit errors
3266 * \param[out] n_bits_total Total number of bits
3267 * \returns 0 on success; negative on error (e.g. CRC error) */
3268int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
3269 int *n_errors, int *n_bits_total)
3270{
3271 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003272
Max32e56412017-10-16 14:58:00 +02003273 if (r < 0)
3274 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003275
Max32e56412017-10-16 14:58:00 +02003276 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003277
3278 return 0;
3279}
3280
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003281/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003282 * \param[out] burst Caller-allocated output burst buffer
3283 * \param[in] ra Input RACH data
3284 * \param[in] bsic BSIC used in this cell
3285 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003286int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003287{
Max32e56412017-10-16 14:58:00 +02003288 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
3289}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003290
Max32e56412017-10-16 14:58:00 +02003291/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
3292 * \param[out] burst Caller-allocated output burst buffer
3293 * \param[in] ra11 Input RACH data
3294 * \param[in] bsic BSIC used in this cell
3295 * \param[in] is_11bit whether given RA is 11 bit or not
3296 * \returns 0 on success; negative on error */
3297int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
3298{
3299 ubit_t conv[17];
3300 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003301
Max32e56412017-10-16 14:58:00 +02003302 if (is_11bit) {
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003303 ra[0] = (uint8_t) (ra11 >> 3);
3304 ra[1] = (uint8_t) (ra11 & 0x07);
Max32e56412017-10-16 14:58:00 +02003305 nbits = 11;
3306 } else
3307 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003308
Max32e56412017-10-16 14:58:00 +02003309 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003310
Max32e56412017-10-16 14:58:00 +02003311 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3312
3313 rach_apply_bsic(conv, bsic, nbits);
3314
3315 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003316
3317 return 0;
3318}
3319
3320/*
3321 * GSM SCH transcoding
3322 */
Harald Weltec6636782017-06-12 14:59:37 +02003323
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003324/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003325 * \param[out] sb_info output buffer for SCH data
3326 * \param[in] burst Input burst data
3327 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003328int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003329{
3330 ubit_t conv[35];
3331 int rv;
3332
3333 osmo_conv_decode(&gsm0503_sch, burst, conv);
3334
3335 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3336 if (rv)
3337 return -1;
3338
3339 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
3340
3341 return 0;
3342}
3343
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003344/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003345 * \param[out] burst Caller-allocated output burst buffer
3346 * \param[in] sb_info Input SCH data
3347 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003348int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003349{
3350 ubit_t conv[35];
3351
3352 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
3353
3354 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3355
3356 osmo_conv_encode(&gsm0503_sch, conv, burst);
3357
3358 return 0;
3359}
Harald Weltec6636782017-06-12 14:59:37 +02003360
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003361/*
3362 * GSM CSD transcoding
3363 */
3364
3365static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB)
3366{
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003367 unsigned int i;
3368
3369 /* hu(B): copy *even* numbered bits if not stolen by FACCH */
3370 if (burst[58] == 0) {
3371 for (i = 0; i < 57; i += 2)
3372 burst[i] |= iB[i];
3373 for (i = 58; i < 114; i += 2)
3374 burst[i + 2] |= iB[i];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003375 }
3376
Vadim Yanitskiyb04d59e2023-05-26 01:38:23 +07003377 /* hl(B): copy *odd* numbered bits if not stolen by FACCH */
3378 if (burst[57] == 0) {
3379 for (i = 1; i < 57; i += 2)
3380 burst[i] |= iB[i];
3381 for (i = 57; i < 114; i += 2)
3382 burst[i + 2] |= iB[i];
3383 }
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003384}
3385
3386/*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3.
3387 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3388 * 22 * 2 * 58 == 2552 bits total.
3389 * \param[in] data Data to be encoded (240 unpacked bits).
3390 * \returns 0 in case of success; negative on error. */
3391int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data)
3392{
3393 ubit_t iB[22 * 114], cB[4 * 114];
3394 ubit_t conv[4 * 60 + 4];
3395
3396 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3397 memcpy(&conv[0], &data[0], 4 * 60);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003398 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003399
3400 /* 3.3.3 Convolutional encoder */
3401 osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]);
3402
3403 /* 3.3.4 Interleaving */
3404 memset(&iB[0], 0, sizeof(iB));
3405 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3406
3407 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3408 for (unsigned int i = 0; i < 22; i++)
3409 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3410
3411 return 0;
3412}
3413
3414/*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3.
3415 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3416 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3417 * 22 * 2 * 58 == 2552 bits total.
3418 * \param[out] n_errors Number of detected bit errors.
3419 * \param[out] n_bits_total Total number of bits.
3420 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3421int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts,
3422 int *n_errors, int *n_bits_total)
3423{
3424 sbit_t iB[22 * 114], cB[4 * 114];
3425 ubit_t conv[4 * 60 + 4];
3426
3427 /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3428 for (unsigned int i = 0; i < 22; i++) {
3429 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3430 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3431 }
3432
3433 /* 3.3.4 Interleaving */
3434 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3435
3436 /* 3.3.3 Convolutional encoder */
3437 osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total);
3438
3439 /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */
3440 memcpy(&data[0], &conv[0], 4 * 60);
3441
3442 return 4 * 60;
3443}
3444
3445/*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4.
3446 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3447 * 22 * 2 * 58 == 2552 bits total.
3448 * \param[in] data Data to be encoded (120 unpacked bits).
3449 * \returns 0 in case of success; negative on error */
3450int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data)
3451{
3452 ubit_t iB[22 * 114], cB[4 * 114];
3453 ubit_t conv[2 * 60 + 32];
3454
3455 /* 3.4.2 Block code:
3456 *
3457 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3458 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3459 *
3460 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3461 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3462 *
3463 * Two such blocks forming a block of 152 bits: u1 + u2. */
3464 for (unsigned int k = 0; k < 2 * 4; k++) {
3465 memcpy(&conv[19 * k], &data[15 * k], 15);
3466 memset(&conv[19 * k + 15], 0, 4);
3467 }
3468
3469 /* 3.4.3 Convolutional encoder */
3470 osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]);
3471
3472 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3473 memset(&iB[0], 0, sizeof(iB));
3474 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3475
3476 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3477 for (unsigned int i = 0; i < 22; i++)
3478 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3479
3480 return 0;
3481}
3482
3483/*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4.
3484 * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits).
3485 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3486 * 22 * 2 * 58 == 2552 bits total.
3487 * \param[out] n_errors Number of detected bit errors.
3488 * \param[out] n_bits_total Total number of bits.
3489 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3490int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts,
3491 int *n_errors, int *n_bits_total)
3492{
3493 sbit_t iB[22 * 114], cB[4 * 114];
3494 ubit_t conv[2 * 60 + 32];
3495
3496 /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3497 for (unsigned int i = 0; i < 22; i++) {
3498 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3499 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3500 }
3501
3502 /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3503 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3504
3505 /* 3.4.3 Convolutional encoder */
3506 osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total);
3507
3508 /* 3.4.2 Block code:
3509 *
3510 * Sixteen bits equal to 0 are added to the 60 information bits, the result
3511 * being a block of 76 bits, {u(0),u(1),...,u(75)}, with:
3512 *
3513 * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14;
3514 * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18.
3515 *
3516 * Two such blocks forming a block of 152 bits: u1 + u2. */
3517 for (unsigned int k = 0; k < 2 * 4; k++)
3518 memcpy(&data[15 * k], &conv[19 * k], 15);
3519
3520 return 2 * 60;
3521}
3522
3523/*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5.
3524 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3525 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3526 * 22 * 2 * 58 == 2552 bits total.
3527 * \param[in] data Data to be encoded (240 unpacked bits).
3528 * \returns 0 in case of success; negative on error */
3529int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data)
3530{
3531 return gsm0503_tch_fr96_encode(bursts, data);
3532}
3533
3534/*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5.
3535 * The algorithm is identical to TCH/F9.6, so it's just a wrapper.
3536 * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits).
3537 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3538 * 22 * 2 * 58 == 2552 bits total.
3539 * \param[out] n_errors Number of detected bit errors.
3540 * \param[out] n_bits_total Total number of bits.
3541 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3542int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts,
3543 int *n_errors, int *n_bits_total)
3544{
3545 return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total);
3546}
3547
Vadim Yanitskiyfadda012023-07-08 06:30:55 +07003548/*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6.
3549 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3550 * 8 * 2 * 58 == 928 bits total.
3551 * \param[in] data Data to be encoded (72 unpacked bits).
3552 * \returns 0 in case of success; negative on error */
3553int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data)
3554{
3555 ubit_t iB[8 * 114], cB[4 * 114];
3556 const ubit_t h = 0;
3557
3558 /* 3.6.{1-3} Block code and Convolutional encoder */
3559 osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]);
3560
3561 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3562 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3563
3564 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3565 for (unsigned int i = 0; i < 8; i++)
3566 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3567
3568 return 0;
3569}
3570
3571/*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6.
3572 * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits).
3573 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3574 * 8 * 2 * 58 == 928 bits total.
3575 * \param[out] n_errors Number of detected bit errors.
3576 * \param[out] n_bits_total Total number of bits.
3577 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3578int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts,
3579 int *n_errors, int *n_bits_total)
3580{
3581 sbit_t iB[8 * 114], cB[4 * 114];
3582
3583 /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3584 for (unsigned int i = 0; i < 8; i++)
3585 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3586
3587 /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3588 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3589
3590 /* 3.6.{1-3} Block code and Convolutional encoder */
3591 osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total);
3592
3593 return 72;
3594}
3595
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003596/*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7.
3597 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3598 * 22 * 2 * 58 == 2552 bits total.
3599 * \param[in] data Data to be encoded (144 unpacked bits).
3600 * \returns 0 in case of success; negative on error */
3601int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data)
3602{
3603 ubit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003604
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003605 /* 3.7.{1-3} Block code and Convolutional encoder */
3606 osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]);
3607 osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]);
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003608
3609 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3610 memset(&iB[0], 0, sizeof(iB));
3611 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3612
3613 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3614 for (unsigned int i = 0; i < 22; i++)
3615 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3616
3617 return 0;
3618}
3619
3620/*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7.
3621 * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits).
3622 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3623 * 22 * 2 * 58 == 2552 bits total.
3624 * \param[out] n_errors Number of detected bit errors.
3625 * \param[out] n_bits_total Total number of bits.
3626 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3627int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts,
3628 int *n_errors, int *n_bits_total)
3629{
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003630 int n_errors_l[2], n_bits_total_l[2];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003631 sbit_t iB[22 * 114], cB[4 * 114];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003632
3633 /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3634 for (unsigned int i = 0; i < 22; i++) {
3635 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3636 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3637 }
3638
3639 /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3640 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3641
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003642 /* 3.7.{1-3} Block code and Convolutional encoder */
3643 osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]);
3644 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 +07003645
Sylvain Munautc87c6a32023-07-07 15:57:32 +02003646 if (n_errors)
3647 *n_errors = n_errors_l[0] + n_errors_l[1];
3648
3649 if (n_bits_total)
3650 *n_bits_total = n_bits_total_l[0] + n_bits_total_l[1];
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003651
3652 return 2 * 72;
3653}
3654
3655/*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8.
3656 * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts,
3657 * 22 * 2 * 58 == 2552 bits total.
3658 * \param[in] data Data to be encoded (290 unpacked bits).
3659 * \returns 0 in case of success; negative on error */
3660int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data)
3661{
3662 ubit_t iB[22 * 114], cB[4 * 114];
3663 ubit_t conv[290 + 4];
3664
3665 /* 3.8.2 Block code: b(290) + pad(4) */
3666 memcpy(&conv[0], &data[0], 290);
Vadim Yanitskiyf8e570a2023-07-08 06:05:12 +07003667 /* pad(4) is set to 0 by osmo_conv_encode() below */
Vadim Yanitskiy9a228272023-05-20 18:54:13 +07003668
3669 /* 3.8.3 Convolutional encoder */
3670 osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]);
3671
3672 /* 3.8.4 Interleaving */
3673 memset(&iB[0], 0, sizeof(iB));
3674 gsm0503_tch_f96_interleave(&cB[0], &iB[0]);
3675
3676 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3677 for (unsigned int i = 0; i < 22; i++)
3678 _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]);
3679
3680 return 0;
3681}
3682
3683/*! Perform channel decoding of a TCH/14.4 channel as per section 3.8.
3684 * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits).
3685 * \param[in] bursts Buffer containing the symbols of 22 bursts,
3686 * 22 * 2 * 58 == 2552 bits total.
3687 * \param[out] n_errors Number of detected bit errors.
3688 * \param[out] n_bits_total Total number of bits.
3689 * \returns Number of unpacked bits used in the output buffer; negative on error. */
3690int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts,
3691 int *n_errors, int *n_bits_total)
3692{
3693 sbit_t iB[22 * 114], cB[4 * 114];
3694 ubit_t conv[294];
3695
3696 /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */
3697 for (unsigned int i = 0; i < 22; i++) {
3698 memcpy(&iB[i * 114], &bursts[i * 116], 57);
3699 memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57);
3700 }
3701
3702 /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */
3703 gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]);
3704
3705 /* 3.8.3 Convolutional encoder */
3706 osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total);
3707
3708 /* 3.8.2 Block code: b(290) + pad(4) */
3709 memcpy(&data[0], &conv[0], 290);
3710
3711 return 290;
3712}
3713
Vadim Yanitskiyb3340222023-05-26 01:07:18 +07003714/*
3715 * FACCH/[FH] transcoding
3716 */
3717
3718/*! Perform channel encoding of a FACCH/F data as per section 4.2.
3719 * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts,
3720 * 8 * 2 * 58 == 928 bits total.
3721 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3722 * \returns 0 in case of success; negative on error */
3723int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data)
3724{
3725 ubit_t iB[8 * 114], cB[4 * 114];
3726 const ubit_t h = 1;
3727
3728 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3729 _xcch_encode_cB(&cB[0], &data[0]);
3730
3731 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3732 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3733
3734 /* 4.2.5 Mapping on a Burst:
3735 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3736 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3737 for (unsigned int i = 0; i < 8; i++)
3738 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3739
3740 return 0;
3741}
3742
3743/*! Perform channel decoding of a FACCH/F data as per section 4.2.
3744 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3745 * \param[in] bursts Buffer containing the symbols of 8 bursts,
3746 * 8 * 2 * 58 == 928 bits total.
3747 * \param[out] n_errors Number of detected bit errors.
3748 * \param[out] n_bits_total Total number of bits.
3749 * \returns Number of bytes used in the output buffer; negative on error. */
3750int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts,
3751 int *n_errors, int *n_bits_total)
3752{
3753 sbit_t iB[8 * 114], cB[4 * 114];
3754 int steal = 0;
3755
3756 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3757 for (unsigned int i = 0; i < 4; i++)
3758 steal -= bursts[i * 116 + 58]; /* hu(B) */
3759 for (unsigned int i = 4; i < 8; i++)
3760 steal -= bursts[i * 116 + 57]; /* hl(B) */
3761 if (steal <= 0)
3762 return -1;
3763
3764 /* 4.2.5 Mapping on a Burst:
3765 * - hu(B)=1 the even numbered bits in the first 4 bursts and
3766 * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */
3767 for (unsigned int i = 0; i < 8; i++)
3768 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3769
3770 /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */
3771 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3772
3773 /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */
3774 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3775 return -1;
3776
3777 return GSM_MACBLOCK_LEN;
3778}
3779
3780/*! Perform channel encoding of a FACCH/H data as per section 4.3.
3781 * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts,
3782 * 6 * 2 * 58 == 696 bits total.
3783 * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN).
3784 * \returns 0 in case of success; negative on error */
3785int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data)
3786{
3787 ubit_t iB[8 * 114], cB[4 * 114];
3788 const ubit_t h = 1;
3789
3790 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3791 _xcch_encode_cB(&cB[0], &data[0]);
3792
3793 /* 4.3.4 Interleaving */
3794 gsm0503_tch_fr_interleave(&cB[0], &iB[0]);
3795
3796 /* 4.3.5 Mapping on a Burst:
3797 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3798 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3799 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3800 for (unsigned int i = 0; i < 6; i++)
3801 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
3802 for (unsigned int i = 2; i < 4; i++)
3803 gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1);
3804
3805 return 0;
3806}
3807
3808/*! Perform channel decoding of a FACCH/H data as per section 4.3.
3809 * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN).
3810 * \param[in] bursts Buffer containing the symbols of 6 bursts,
3811 * 6 * 2 * 58 == 696 bits total.
3812 * \param[out] n_errors Number of detected bit errors.
3813 * \param[out] n_bits_total Total number of bits.
3814 * \returns Number of bytes used in the output buffer; negative on error. */
3815int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts,
3816 int *n_errors, int *n_bits_total)
3817{
3818 sbit_t iB[8 * 114], cB[4 * 114];
3819 int steal = 0;
3820
3821 /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */
3822 for (unsigned int i = 0; i < 4; i++)
3823 steal -= bursts[i * 116 + 58]; /* hu(B) */
3824 for (unsigned int i = 2; i < 6; i++)
3825 steal -= bursts[i * 116 + 57]; /* hl(B) */
3826 if (steal <= 0)
3827 return -1;
3828
3829 /* 4.3.5 Mapping on a Burst:
3830 * - hu(B)=1 the even numbered bits of the first 2 bursts,
3831 * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and
3832 * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */
3833 for (unsigned int i = 0; i < 6; i++)
3834 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2);
3835 for (unsigned int i = 2; i < 4; i++)
3836 gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1);
3837
3838 /* 4.3.4 Interleaving */
3839 gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]);
3840
3841 /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */
3842 if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0)
3843 return -1;
3844
3845 return GSM_MACBLOCK_LEN;
3846}
3847
Harald Weltec6636782017-06-12 14:59:37 +02003848/*! @} */