blob: 6f19ed92c234ae76a31ec2dba203815118a728fd [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
34#include <osmocom/gprs/protocol/gsm_04_60.h>
35#include <osmocom/gprs/gprs_rlc.h>
36
37#include <osmocom/gsm/protocol/gsm_04_08.h>
38#include <osmocom/gsm/gsm0503.h>
39#include <osmocom/codec/codec.h>
40
41#include <osmocom/coding/gsm0503_interleaving.h>
42#include <osmocom/coding/gsm0503_mapping.h>
43#include <osmocom/coding/gsm0503_tables.h>
44#include <osmocom/coding/gsm0503_coding.h>
45#include <osmocom/coding/gsm0503_parity.h>
Philipp Maier898c9c62020-02-06 14:25:01 +010046#include <osmocom/coding/gsm0503_amr_dtx.h>
Vadim Yanitskiy3262f822016-09-23 01:48:59 +070047
Harald Weltec6636782017-06-12 14:59:37 +020048/*! \mainpage libosmocoding Documentation
49 *
50 * \section sec_intro Introduction
51 * This library is a collection of definitions, tables and functions
52 * implementing the GSM/GPRS/EGPRS channel coding (and decoding) as
53 * specified in 3GPP TS 05.03 / 45.003.
54 *
Vadim Yanitskiy9a232fd2018-01-19 03:05:32 +060055 * libosmocoding is developed as part of the Osmocom (Open Source Mobile
Harald Weltec6636782017-06-12 14:59:37 +020056 * Communications) project, a community-based, collaborative development
57 * project to create Free and Open Source implementations of mobile
58 * communications systems. For more information about Osmocom, please
59 * see https://osmocom.org/
60 *
61 * \section sec_copyright Copyright and License
62 * Copyright © 2013 by Andreas Eversberg\n
63 * Copyright © 2015 by Alexander Chemeris\n
64 * Copyright © 2016 by Tom Tsou\n
65 * Documentation Copyright © 2017 by Harald Welte\n
66 * All rights reserved. \n\n
67 * The source code of libosmocoding is licensed under the terms of the GNU
68 * General Public License as published by the Free Software Foundation;
69 * either version 2 of the License, or (at your option) any later
70 * version.\n
71 * See <http://www.gnu.org/licenses/> or COPYING included in the source
72 * code package istelf.\n
73 * The information detailed here is provided AS IS with NO WARRANTY OF
74 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
75 * FITNESS FOR A PARTICULAR PURPOSE.
76 * \n\n
77 *
78 * \section sec_tracker Homepage + Issue Tracker
79 * libosmocoding is distributed as part of libosmocore and shares its
80 * project page at http://osmocom.org/projects/libosmocore
81 *
82 * An Issue Tracker can be found at
83 * https://osmocom.org/projects/libosmocore/issues
84 *
85 * \section sec_contact Contact and Support
86 * Community-based support is available at the OpenBSC mailing list
87 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
88 * Commercial support options available upon request from
89 * <http://sysmocom.de/>
90 */
91
92
93/*! \addtogroup coding
94 * @{
95 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +020096 * GSM TS 05.03 coding
Harald Weltec6636782017-06-12 14:59:37 +020097 *
98 * This module is the "master module" of libosmocoding. It uses the
99 * various other modules (mapping, parity, interleaving) in order to
100 * implement the complete channel coding (and decoding) chain for the
101 * various channel types as defined in TS 05.03 / 45.003.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +0200102 *
103 * \file gsm0503_coding.c */
Harald Weltec6636782017-06-12 14:59:37 +0200104
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700105/*
106 * EGPRS coding limits
107 */
108
109/* Max header size with parity bits */
110#define EGPRS_HDR_UPP_MAX 54
111
112/* Max encoded header size */
113#define EGPRS_HDR_C_MAX 162
114
115/* Max punctured header size */
116#define EGPRS_HDR_HC_MAX 160
117
118/* Max data block size with parity bits */
119#define EGPRS_DATA_U_MAX 612
120
121/* Max encoded data block size */
122#define EGPRS_DATA_C_MAX 1836
123
124/* Max single block punctured data size */
125#define EGPRS_DATA_DC_MAX 1248
126
127/* Dual block punctured data size */
128#define EGPRS_DATA_C1 612
129#define EGPRS_DATA_C2 EGPRS_DATA_C1
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! union across the three different EGPRS Uplink header types */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700132union gprs_rlc_ul_hdr_egprs {
133 struct gprs_rlc_ul_header_egprs_1 type1;
134 struct gprs_rlc_ul_header_egprs_2 type2;
135 struct gprs_rlc_ul_header_egprs_3 type3;
136};
137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! union across the three different EGPRS Downlink header types */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700139union gprs_rlc_dl_hdr_egprs {
140 struct gprs_rlc_dl_header_egprs_1 type1;
141 struct gprs_rlc_dl_header_egprs_2 type2;
142 struct gprs_rlc_dl_header_egprs_3 type3;
143};
144
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200145/*! Structure describing a Modulation and Coding Scheme */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700146struct gsm0503_mcs_code {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200147 /*! Modulation and Coding Scheme (MSC) number */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700148 uint8_t mcs;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200149 /*! Length of Uplink Stealing Flag (USF) in bits */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700150 uint8_t usf_len;
151
152 /* Header coding */
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153 /*! Length of header (bits) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700154 uint8_t hdr_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155 /*! Length of header convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700156 uint8_t hdr_code_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157 /*! Length of header code puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700158 uint8_t hdr_punc_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159 /*! header convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700160 const struct osmo_conv_code *hdr_conv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161 /*! header puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700162 const uint8_t *hdr_punc;
163
164 /* Data coding */
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200165 /*! length of data (bits) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700166 uint16_t data_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167 /*! length of data convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700168 uint16_t data_code_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200169 /*! length of data code puncturing sequence */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700170 uint16_t data_punc_len;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200171 /*! data convolutional code */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700172 const struct osmo_conv_code *data_conv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173 /*! data puncturing sequences */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700174 const uint8_t *data_punc[3];
175};
176
177/*
178 * EGPRS UL coding parameters
179 */
Harald Welte2f984ea2017-06-12 15:05:21 +0200180const struct gsm0503_mcs_code gsm0503_mcs_ul_codes[EGPRS_NUM_MCS] = {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700181 {
182 .mcs = EGPRS_MCS0,
183 },
184 {
185 .mcs = EGPRS_MCS1,
186 .hdr_len = 31,
187 .hdr_code_len = 117,
188 .hdr_punc_len = 80,
189 .hdr_conv = &gsm0503_mcs1_ul_hdr,
190 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
191
192 .data_len = 178,
193 .data_code_len = 588,
194 .data_punc_len = 372,
195 .data_conv = &gsm0503_mcs1,
196 .data_punc = {
197 gsm0503_puncture_mcs1_p1,
198 gsm0503_puncture_mcs1_p2,
199 NULL,
200 },
201 },
202 {
203 .mcs = EGPRS_MCS2,
204 .hdr_len = 31,
205 .hdr_code_len = 117,
206 .hdr_punc_len = 80,
207 .hdr_conv = &gsm0503_mcs1_ul_hdr,
208 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
209
210 .data_len = 226,
211 .data_code_len = 732,
212 .data_punc_len = 372,
213 .data_conv = &gsm0503_mcs2,
214 .data_punc = {
215 gsm0503_puncture_mcs2_p1,
216 gsm0503_puncture_mcs2_p2,
217 NULL,
218 },
219 },
220 {
221 .mcs = EGPRS_MCS3,
222 .hdr_len = 31,
223 .hdr_code_len = 117,
224 .hdr_punc_len = 80,
225 .hdr_conv = &gsm0503_mcs1_ul_hdr,
226 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
227
228 .data_len = 298,
229 .data_code_len = 948,
230 .data_punc_len = 372,
231 .data_conv = &gsm0503_mcs3,
232 .data_punc = {
233 gsm0503_puncture_mcs3_p1,
234 gsm0503_puncture_mcs3_p2,
235 gsm0503_puncture_mcs3_p3,
236 },
237 },
238 {
239 .mcs = EGPRS_MCS4,
240 .hdr_len = 31,
241 .hdr_code_len = 117,
242 .hdr_punc_len = 80,
243 .hdr_conv = &gsm0503_mcs1_ul_hdr,
244 .hdr_punc = gsm0503_puncture_mcs1_ul_hdr,
245
246 .data_len = 354,
247 .data_code_len = 1116,
248 .data_punc_len = 372,
249 .data_conv = &gsm0503_mcs4,
250 .data_punc = {
251 gsm0503_puncture_mcs4_p1,
252 gsm0503_puncture_mcs4_p2,
253 gsm0503_puncture_mcs4_p3,
254 },
255 },
256 {
257 .mcs = EGPRS_MCS5,
258 .hdr_len = 37,
259 .hdr_code_len = 135,
260 .hdr_punc_len = 136,
261 .hdr_conv = &gsm0503_mcs5_ul_hdr,
262 .hdr_punc = NULL,
263
264 .data_len = 450,
265 .data_code_len = 1404,
266 .data_punc_len = 1248,
267 .data_conv = &gsm0503_mcs5,
268 .data_punc = {
269 gsm0503_puncture_mcs5_p1,
270 gsm0503_puncture_mcs5_p2,
271 NULL,
272 },
273 },
274 {
275 .mcs = EGPRS_MCS6,
276 .hdr_len = 37,
277 .hdr_code_len = 135,
278 .hdr_punc_len = 136,
279 .hdr_conv = &gsm0503_mcs5_ul_hdr,
280 .hdr_punc = NULL,
281
282 .data_len = 594,
283 .data_code_len = 1836,
284 .data_punc_len = 1248,
285 .data_conv = &gsm0503_mcs6,
286 .data_punc = {
287 gsm0503_puncture_mcs6_p1,
288 gsm0503_puncture_mcs6_p2,
289 NULL,
290 },
291 },
292 {
293 .mcs = EGPRS_MCS7,
294 .hdr_len = 46,
295 .hdr_code_len = 162,
296 .hdr_punc_len = 160,
297 .hdr_conv = &gsm0503_mcs7_ul_hdr,
298 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
299
300 .data_len = 900,
301 .data_code_len = 1404,
302 .data_punc_len = 612,
303 .data_conv = &gsm0503_mcs7,
304 .data_punc = {
305 gsm0503_puncture_mcs7_p1,
306 gsm0503_puncture_mcs7_p2,
307 gsm0503_puncture_mcs7_p3,
308 }
309 },
310 {
311 .mcs = EGPRS_MCS8,
312 .hdr_len = 46,
313 .hdr_code_len = 162,
314 .hdr_punc_len = 160,
315 .hdr_conv = &gsm0503_mcs7_ul_hdr,
316 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
317
318 .data_len = 1092,
319 .data_code_len = 1692,
320 .data_punc_len = 612,
321 .data_conv = &gsm0503_mcs8,
322 .data_punc = {
323 gsm0503_puncture_mcs8_p1,
324 gsm0503_puncture_mcs8_p2,
325 gsm0503_puncture_mcs8_p3,
326 }
327 },
328 {
329 .mcs = EGPRS_MCS9,
330 .hdr_len = 46,
331 .hdr_code_len = 162,
332 .hdr_punc_len = 160,
333 .hdr_conv = &gsm0503_mcs7_ul_hdr,
334 .hdr_punc = gsm0503_puncture_mcs7_ul_hdr,
335
336 .data_len = 1188,
337 .data_code_len = 1836,
338 .data_punc_len = 612,
339 .data_conv = &gsm0503_mcs9,
340 .data_punc = {
341 gsm0503_puncture_mcs9_p1,
342 gsm0503_puncture_mcs9_p2,
343 gsm0503_puncture_mcs9_p3,
344 }
345 },
346};
347
348/*
349 * EGPRS DL coding parameters
350 */
Harald Welte2f984ea2017-06-12 15:05:21 +0200351const struct gsm0503_mcs_code gsm0503_mcs_dl_codes[EGPRS_NUM_MCS] = {
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700352 {
353 .mcs = EGPRS_MCS0,
354 },
355 {
356 .mcs = EGPRS_MCS1,
357 .usf_len = 3,
358 .hdr_len = 28,
359 .hdr_code_len = 108,
360 .hdr_punc_len = 68,
361 .hdr_conv = &gsm0503_mcs1_dl_hdr,
362 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
363
364 .data_len = 178,
365 .data_code_len = 588,
366 .data_punc_len = 372,
367 .data_conv = &gsm0503_mcs1,
368 .data_punc = {
369 gsm0503_puncture_mcs1_p1,
370 gsm0503_puncture_mcs1_p2,
371 NULL,
372 },
373 },
374 {
375 .mcs = EGPRS_MCS2,
376 .usf_len = 3,
377 .hdr_len = 28,
378 .hdr_code_len = 108,
379 .hdr_punc_len = 68,
380 .hdr_conv = &gsm0503_mcs1_dl_hdr,
381 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
382
383 .data_len = 226,
384 .data_code_len = 732,
385 .data_punc_len = 372,
386 .data_conv = &gsm0503_mcs2,
387 .data_punc = {
388 gsm0503_puncture_mcs2_p1,
389 gsm0503_puncture_mcs2_p2,
390 NULL,
391 },
392 },
393 {
394 .mcs = EGPRS_MCS3,
395 .usf_len = 3,
396 .hdr_len = 28,
397 .hdr_code_len = 108,
398 .hdr_punc_len = 68,
399 .hdr_conv = &gsm0503_mcs1_dl_hdr,
400 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
401
402 .data_len = 298,
403 .data_code_len = 948,
404 .data_punc_len = 372,
405 .data_conv = &gsm0503_mcs3,
406 .data_punc = {
407 gsm0503_puncture_mcs3_p1,
408 gsm0503_puncture_mcs3_p2,
409 gsm0503_puncture_mcs3_p3,
410 },
411 },
412 {
413 .mcs = EGPRS_MCS4,
414 .usf_len = 3,
415 .hdr_len = 28,
416 .hdr_code_len = 108,
417 .hdr_punc_len = 68,
418 .hdr_conv = &gsm0503_mcs1_dl_hdr,
419 .hdr_punc = gsm0503_puncture_mcs1_dl_hdr,
420
421 .data_len = 354,
422 .data_code_len = 1116,
423 .data_punc_len = 372,
424 .data_conv = &gsm0503_mcs4,
425 .data_punc = {
426 gsm0503_puncture_mcs4_p1,
427 gsm0503_puncture_mcs4_p2,
428 gsm0503_puncture_mcs4_p3,
429 },
430 },
431 {
432 .mcs = EGPRS_MCS5,
433 .usf_len = 3,
434 .hdr_len = 25,
435 .hdr_code_len = 99,
436 .hdr_punc_len = 100,
437 .hdr_conv = &gsm0503_mcs5_dl_hdr,
438 .hdr_punc = NULL,
439
440 .data_len = 450,
441 .data_code_len = 1404,
442 .data_punc_len = 1248,
443 .data_conv = &gsm0503_mcs5,
444 .data_punc = {
445 gsm0503_puncture_mcs5_p1,
446 gsm0503_puncture_mcs5_p2,
447 NULL,
448 },
449 },
450 {
451 .mcs = EGPRS_MCS6,
452 .usf_len = 3,
453 .hdr_len = 25,
454 .hdr_code_len = 99,
455 .hdr_punc_len = 100,
456 .hdr_conv = &gsm0503_mcs5_dl_hdr,
457 .hdr_punc = NULL,
458
459 .data_len = 594,
460 .data_code_len = 1836,
461 .data_punc_len = 1248,
462 .data_conv = &gsm0503_mcs6,
463 .data_punc = {
464 gsm0503_puncture_mcs6_p1,
465 gsm0503_puncture_mcs6_p2,
466 NULL,
467 },
468 },
469 {
470 .mcs = EGPRS_MCS7,
471 .usf_len = 3,
472 .hdr_len = 37,
473 .hdr_code_len = 135,
474 .hdr_punc_len = 124,
475 .hdr_conv = &gsm0503_mcs7_dl_hdr,
476 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
477
478 .data_len = 900,
479 .data_code_len = 1404,
480 .data_punc_len = 612,
481 .data_conv = &gsm0503_mcs7,
482 .data_punc = {
483 gsm0503_puncture_mcs7_p1,
484 gsm0503_puncture_mcs7_p2,
485 gsm0503_puncture_mcs7_p3,
486 }
487 },
488 {
489 .mcs = EGPRS_MCS8,
490 .usf_len = 3,
491 .hdr_len = 37,
492 .hdr_code_len = 135,
493 .hdr_punc_len = 124,
494 .hdr_conv = &gsm0503_mcs7_dl_hdr,
495 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
496
497 .data_len = 1092,
498 .data_code_len = 1692,
499 .data_punc_len = 612,
500 .data_conv = &gsm0503_mcs8,
501 .data_punc = {
502 gsm0503_puncture_mcs8_p1,
503 gsm0503_puncture_mcs8_p2,
504 gsm0503_puncture_mcs8_p3,
505 }
506 },
507 {
508 .mcs = EGPRS_MCS9,
509 .usf_len = 3,
510 .hdr_len = 37,
511 .hdr_code_len = 135,
512 .hdr_punc_len = 124,
513 .hdr_conv = &gsm0503_mcs7_dl_hdr,
514 .hdr_punc = gsm0503_puncture_mcs7_dl_hdr,
515
516 .data_len = 1188,
517 .data_code_len = 1836,
518 .data_punc_len = 612,
519 .data_conv = &gsm0503_mcs9,
520 .data_punc = {
521 gsm0503_puncture_mcs9_p1,
522 gsm0503_puncture_mcs9_p2,
523 gsm0503_puncture_mcs9_p3,
524 }
525 },
526};
527
Alexander Chemeris147051f2018-07-14 21:02:29 +0200528/*! Convolutional Decode + compute BER for punctured codes
Harald Weltec6636782017-06-12 14:59:37 +0200529 * \param[in] code Description of Convolutional Code
530 * \param[in] input Input soft-bits (-127...127)
531 * \param[out] output bits
532 * \param[out] n_errors Number of bit-errors
533 * \param[out] n_bits_total Number of bits
Alexander Chemeris147051f2018-07-14 21:02:29 +0200534 * \param[in] data_punc Puncturing mask array. Can be NULL.
Harald Weltec6636782017-06-12 14:59:37 +0200535 */
Alexander Chemeris147051f2018-07-14 21:02:29 +0200536static int osmo_conv_decode_ber_punctured(const struct osmo_conv_code *code,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700537 const sbit_t *input, ubit_t *output,
Alexander Chemeris147051f2018-07-14 21:02:29 +0200538 int *n_errors, int *n_bits_total,
539 const uint8_t *data_punc)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700540{
541 int res, i, coded_len;
542 ubit_t recoded[EGPRS_DATA_C_MAX];
543
544 res = osmo_conv_decode(code, input, output);
545
Pau Espin Pedrold2737e62022-05-13 13:07:21 +0200546 if (!n_bits_total && !n_errors)
547 return res;
548
549 coded_len = osmo_conv_encode(code, output, recoded);
Pau Espin Pedrol6c58d152022-05-13 14:03:27 +0200550 OSMO_ASSERT(ARRAY_SIZE(recoded) >= coded_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700551
552 /* Count bit errors */
553 if (n_errors) {
554 *n_errors = 0;
555 for (i = 0; i < coded_len; i++) {
Alexander Chemeris147051f2018-07-14 21:02:29 +0200556 if (((!data_punc) || (data_punc && !data_punc[i])) &&
557 !((recoded[i] && input[i] < 0) ||
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700558 (!recoded[i] && input[i] > 0)) )
559 *n_errors += 1;
560 }
561 }
562
563 if (n_bits_total)
564 *n_bits_total = coded_len;
565
566 return res;
567}
568
Alexander Chemeris147051f2018-07-14 21:02:29 +0200569/*! Convolutional Decode + compute BER for non-punctured codes
570 * \param[in] code Description of Convolutional Code
571 * \param[in] input Input soft-bits (-127...127)
572 * \param[out] output bits
573 * \param[out] n_errors Number of bit-errors
574 * \param[out] n_bits_total Number of bits
575 */
576static int osmo_conv_decode_ber(const struct osmo_conv_code *code,
577 const sbit_t *input, ubit_t *output,
578 int *n_errors, int *n_bits_total)
579{
580 return osmo_conv_decode_ber_punctured(code, input, output,
581 n_errors, n_bits_total, NULL);
582}
583
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200584/*! convenience wrapper for decoding coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200585 * \param[out] l2_data caller-allocated buffer for L2 Frame
586 * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3
587 * \param[out] n_errors Number of detected errors
588 * \param[out] n_bits_total Number of total coded bits
589 * \returns 0 on success; -1 on CRC error */
Harald Welteb9946d32017-06-12 09:40:16 +0200590static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700591 int *n_errors, int *n_bits_total)
592{
593 ubit_t conv[224];
594 int rv;
595
596 osmo_conv_decode_ber(&gsm0503_xcch, cB,
597 conv, n_errors, n_bits_total);
598
599 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
600 conv, 184, conv + 184);
601 if (rv)
602 return -1;
603
604 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
605
606 return 0;
607}
608
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200609/*! convenience wrapper for encoding to coded bits
Harald Weltec6636782017-06-12 14:59:37 +0200610 * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3
611 * \param[out] l2_data to-be-encoded L2 Frame
612 * \returns 0 */
Harald Welteb9946d32017-06-12 09:40:16 +0200613static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700614{
615 ubit_t conv[224];
616
617 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
618
619 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
620
621 osmo_conv_encode(&gsm0503_xcch, conv, cB);
622
623 return 0;
624}
625
626/*
627 * GSM xCCH block transcoding
628 */
Harald Weltec6636782017-06-12 14:59:37 +0200629
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200630/*! Decoding of xCCH data from bursts to L2 frame
Harald Weltec6636782017-06-12 14:59:37 +0200631 * \param[out] l2_data caller-allocated output data buffer
632 * \param[in] bursts four GSM bursts in soft-bits
633 * \param[out] n_errors Number of detected errors
634 * \param[out] n_bits_total Number of total coded bits
635 */
Harald Welteb9946d32017-06-12 09:40:16 +0200636int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700637 int *n_errors, int *n_bits_total)
638{
639 sbit_t iB[456], cB[456];
640 int i;
641
642 for (i = 0; i < 4; i++)
643 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL);
644
645 gsm0503_xcch_deinterleave(cB, iB);
646
647 return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total);
648}
649
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200650/*! Encoding of xCCH data from L2 frame to bursts
Harald Weltec6636782017-06-12 14:59:37 +0200651 * \param[out] bursts caller-allocated burst data (unpacked bits)
652 * \param[in] l2_data L2 input data (MAC block)
653 * \returns 0
654 */
Harald Welteb9946d32017-06-12 09:40:16 +0200655int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700656{
657 ubit_t iB[456], cB[456], hl = 1, hn = 1;
658 int i;
659
660 _xcch_encode_cB(cB, l2_data);
661
662 gsm0503_xcch_interleave(cB, iB);
663
664 for (i = 0; i < 4; i++)
665 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn);
666
667 return 0;
668}
669
670/*
671 * EGPRS PDTCH UL block decoding
672 */
673
674/*
675 * Type 3 - MCS-1,2,3,4
676 * Unmapping and deinterleaving
677 */
678static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
679{
680 int i;
681 sbit_t iB[456], q[8];
682
683 for (i = 0; i < 4; i++) {
684 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
685 q + i * 2, q + i * 2 + 1);
686 }
687
688 gsm0503_mcs1_ul_deinterleave(hc, dc, iB);
689
690 return 0;
691}
692
693/*
694 * Type 2 - MCS-5,6
695 * Unmapping and deinterleaving
696 */
697static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc)
698{
699 int i;
700 sbit_t burst[348];
701 sbit_t hi[EGPRS_HDR_HC_MAX];
702 sbit_t di[EGPRS_DATA_DC_MAX];
703
704 for (i = 0; i < 4; i++) {
705 memcpy(burst, &bursts[i * 348], 348);
706
707 gsm0503_mcs5_burst_swap(burst);
708 gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i);
709 }
710
711 gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di);
712
713 return 0;
714}
715
716/*
717 * Type 1 - MCS-7,8,9
718 * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique
719 */
720static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc,
721 sbit_t *c1, sbit_t *c2, int msc)
722{
723 int i;
724 sbit_t burst[348];
725 sbit_t hi[EGPRS_HDR_HC_MAX];
726 sbit_t di[EGPRS_DATA_C1 * 2];
727
728 for (i = 0; i < 4; i++) {
729 memcpy(burst, &bursts[i * 348], 348);
730
731 gsm0503_mcs5_burst_swap(burst);
732 gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i);
733 }
734
735 if (msc == EGPRS_MCS7)
736 gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di);
737 else
738 gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di);
739
740 return 0;
741}
742
743/*
744 * Decode EGPRS UL header section
745 *
746 * 1. Depuncture
747 * 2. Convolutional decoding
748 * 3. CRC check
749 */
750static int _egprs_decode_hdr(const sbit_t *hc, int mcs,
751 union gprs_rlc_ul_hdr_egprs *hdr)
752{
753 sbit_t C[EGPRS_HDR_C_MAX];
754 ubit_t upp[EGPRS_HDR_UPP_MAX];
755 int i, j, rc;
Harald Welte2f984ea2017-06-12 15:05:21 +0200756 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700757
758 code = &gsm0503_mcs_ul_codes[mcs];
759
760 /* Skip depuncturing on MCS-5,6 header */
761 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
762 memcpy(C, hc, code->hdr_code_len);
763 goto hdr_conv_decode;
764 }
765
766 if (!code->hdr_punc) {
767 /* Invalid MCS-X header puncture matrix */
768 return -1;
769 }
770
771 i = code->hdr_code_len - 1;
772 j = code->hdr_punc_len - 1;
773
774 for (; i >= 0; i--) {
775 if (!code->hdr_punc[i])
776 C[i] = hc[j--];
777 else
778 C[i] = 0;
779 }
780
781hdr_conv_decode:
782 osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL);
783 rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp,
784 code->hdr_len, upp + code->hdr_len);
785 if (rc)
786 return -1;
787
788 osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1);
789
790 return 0;
791}
792
793/*
794 * Blind MCS header decoding based on burst length and CRC validation.
795 * Ignore 'q' value coding identification. This approach provides
796 * the strongest chance of header recovery.
797 */
798static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr,
799 const sbit_t *bursts, uint16_t nbits)
800{
801 int rc;
802 sbit_t hc[EGPRS_HDR_HC_MAX];
803
804 if (nbits == GSM0503_GPRS_BURSTS_NBITS) {
805 /* MCS-1,2,3,4 */
806 egprs_type3_unmap(bursts, hc, NULL);
807 rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr);
808 if (!rc)
809 return EGPRS_HDR_TYPE3;
810 } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) {
811 /* MCS-5,6 */
812 egprs_type2_unmap(bursts, hc, NULL);
813 rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr);
814 if (!rc)
815 return EGPRS_HDR_TYPE2;
816
817 /* MCS-7,8,9 */
818 egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7);
819 rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr);
820 if (!rc)
821 return EGPRS_HDR_TYPE1;
822 }
823
824 return -1;
825}
826
827/*
828 * Parse EGPRS UL header for coding and puncturing scheme (CPS)
829 *
830 * Type 1 - MCS-7,8,9
831 * Type 2 - MCS-5,6
832 * Type 3 - MCS-1,2,3,4
833 */
834static int egprs_parse_ul_cps(struct egprs_cps *cps,
835 union gprs_rlc_ul_hdr_egprs *hdr, int type)
836{
837 uint8_t bits;
838
839 switch (type) {
840 case EGPRS_HDR_TYPE1:
841 bits = hdr->type1.cps;
842 break;
843 case EGPRS_HDR_TYPE2:
844 bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi;
845 break;
846 case EGPRS_HDR_TYPE3:
847 bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi;
848 break;
849 default:
850 return -1;
851 }
852
853 return egprs_get_cps(cps, type, bits);
854}
855
856/*
857 * Decode EGPRS UL data section
858 *
859 * 1. Depuncture
860 * 2. Convolutional decoding
861 * 3. CRC check
862 * 4. Block combining (MCS-7,8,9 only)
863 */
Harald Welteb9946d32017-06-12 09:40:16 +0200864static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700865 int mcs, int p, int blk, int *n_errors, int *n_bits_total)
866{
867 ubit_t u[EGPRS_DATA_U_MAX];
868 sbit_t C[EGPRS_DATA_C_MAX];
869
870 int i, j, rc, data_len;
Harald Welte2f984ea2017-06-12 15:05:21 +0200871 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700872
873 if (blk && mcs < EGPRS_MCS7) {
874 /* Invalid MCS-X block state */
875 return -1;
876 }
877
878 code = &gsm0503_mcs_ul_codes[mcs];
879 if (!code->data_punc[p]) {
880 /* Invalid MCS-X data puncture matrix */
881 return -1;
882 }
883
884 /*
885 * MCS-1,6 - single block processing
886 * MCS-7,9 - dual block processing
887 */
888 if (mcs >= EGPRS_MCS7)
889 data_len = code->data_len / 2;
890 else
891 data_len = code->data_len;
892
893 i = code->data_code_len - 1;
894 j = code->data_punc_len - 1;
895
896 for (; i >= 0; i--) {
897 if (!code->data_punc[p][i])
898 C[i] = c[j--];
899 else
900 C[i] = 0;
901 }
902
Alexander Chemeris147051f2018-07-14 21:02:29 +0200903 osmo_conv_decode_ber_punctured(code->data_conv, C, u,
904 n_errors, n_bits_total, code->data_punc[p]);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700905 rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u,
906 data_len, u + data_len);
907 if (rc)
908 return -1;
909
910 /* Offsets output pointer on the second block of Type 1 MCS */
911 osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len,
912 u, 0, data_len, 1);
913
914 /* Return the number of bytes required for the bit message */
Maxdd75bac2017-06-13 15:07:01 +0200915 return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700916}
917
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200918/*! Decode EGPRS UL message
Harald Weltec6636782017-06-12 14:59:37 +0200919 * 1. Header section decoding
920 * 2. Extract CPS settings
921 * 3. Burst unmapping and deinterleaving
922 * 4. Data section decoding
923 * \param[out] l2_data caller-allocated buffer for L2 Frame
924 * \param[in] bursts burst input data as soft unpacked bits
925 * \param[in] nbits number of bits in \a bursts
926 * \param usf_p unused argument ?!?
927 * \param[out] n_errors number of detected bit-errors
Alexander Chemerised7d2dd2018-07-14 21:06:27 +0200928 * \param[out] n_bits_total total number of decoded bits
Harald Weltec6636782017-06-12 14:59:37 +0200929 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +0200930int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700931 uint8_t *usf_p, int *n_errors, int *n_bits_total)
932{
933 sbit_t dc[EGPRS_DATA_DC_MAX];
934 sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
935 int type, rc;
936 struct egprs_cps cps;
937 union gprs_rlc_ul_hdr_egprs *hdr;
938
Alexander Chemeris50f7d742018-07-14 21:07:27 +0200939 if (n_errors)
940 *n_errors = 0;
941 if (n_bits_total)
942 *n_bits_total = 0;
943
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700944 if ((nbits != GSM0503_GPRS_BURSTS_NBITS) &&
945 (nbits != GSM0503_EGPRS_BURSTS_NBITS)) {
946 /* Invalid EGPRS bit length */
Maxc8cf8202017-05-22 16:07:04 +0200947 return -EOVERFLOW;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700948 }
949
950 hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data;
951 type = egprs_decode_hdr(hdr, bursts, nbits);
952 if (egprs_parse_ul_cps(&cps, hdr, type) < 0)
Maxc8cf8202017-05-22 16:07:04 +0200953 return -EIO;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700954
955 switch (cps.mcs) {
Maxc8cf8202017-05-22 16:07:04 +0200956 case EGPRS_MCS0:
957 return -ENOTSUP;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700958 case EGPRS_MCS1:
959 case EGPRS_MCS2:
960 case EGPRS_MCS3:
961 case EGPRS_MCS4:
962 egprs_type3_unmap(bursts, NULL, dc);
963 break;
964 case EGPRS_MCS5:
965 case EGPRS_MCS6:
966 egprs_type2_unmap(bursts, NULL, dc);
967 break;
968 case EGPRS_MCS7:
969 case EGPRS_MCS8:
970 case EGPRS_MCS9:
971 egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs);
972 break;
973 default:
974 /* Invalid MCS-X */
Maxc8cf8202017-05-22 16:07:04 +0200975 return -EINVAL;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700976 }
977
978 /* Decode MCS-X block, where X = cps.mcs */
979 if (cps.mcs < EGPRS_MCS7) {
980 rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0],
981 0, n_errors, n_bits_total);
982 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200983 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700984 } else {
Alexander Chemeris94443262018-07-14 21:09:54 +0200985 /* Bit counters for the second block */
986 int n_errors2, n_bits_total2;
987
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700988 /* MCS-7,8,9 block 1 */
989 rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0],
990 0, n_errors, n_bits_total);
991 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +0200992 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +0700993
994 /* MCS-7,8,9 block 2 */
995 rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1],
Alexander Chemeris94443262018-07-14 21:09:54 +0200996 1, &n_errors2, &n_bits_total2);
997 if (n_errors)
998 *n_errors += n_errors2;
999 if (n_bits_total)
1000 *n_bits_total += n_bits_total2;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001001 if (rc < 0)
Maxc8cf8202017-05-22 16:07:04 +02001002 return -EFAULT;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001003 }
1004
1005 return rc;
1006}
1007
1008/*
1009 * GSM PDTCH block transcoding
1010 */
1011
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001012/*! Decode GPRS PDTCH
Harald Weltec6636782017-06-12 14:59:37 +02001013 * \param[out] l2_data caller-allocated buffer for L2 Frame
1014 * \param[in] bursts burst input data as soft unpacked bits
1015 * \param[out] usf_p uplink stealing flag
1016 * \param[out] n_errors number of detected bit-errors
1017 * \param[out] n_bits_total total number of dcoded bits
1018 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001019int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001020 int *n_errors, int *n_bits_total)
1021{
1022 sbit_t iB[456], cB[676], hl_hn[8];
1023 ubit_t conv[456];
1024 int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */
1025
1026 for (i = 0; i < 4; i++)
1027 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
1028 hl_hn + i * 2, hl_hn + i * 2 + 1);
1029
1030 for (i = 0; i < 4; i++) {
1031 for (j = 0, k = 0; j < 8; j++)
1032 k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j]));
1033
1034 if (i == 0 || k < best) {
1035 best = k;
1036 cs = i + 1;
1037 }
1038 }
1039
1040 gsm0503_xcch_deinterleave(cB, iB);
1041
1042 switch (cs) {
1043 case 1:
1044 osmo_conv_decode_ber(&gsm0503_xcch, cB,
1045 conv, n_errors, n_bits_total);
1046
1047 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
1048 conv, 184, conv + 184);
1049 if (rv)
1050 return -1;
1051
1052 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
1053
1054 return 23;
1055 case 2:
1056 for (i = 587, j = 455; i >= 0; i--) {
1057 if (!gsm0503_puncture_cs2[i])
1058 cB[i] = cB[j--];
1059 else
1060 cB[i] = 0;
1061 }
1062
1063 osmo_conv_decode_ber(&gsm0503_cs2_np, cB,
1064 conv, n_errors, n_bits_total);
1065
1066 for (i = 0; i < 8; i++) {
1067 for (j = 0, k = 0; j < 6; j++)
1068 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
1069
1070 if (i == 0 || k < best) {
1071 best = k;
1072 usf = i;
1073 }
1074 }
1075
1076 conv[3] = usf & 1;
1077 conv[4] = (usf >> 1) & 1;
1078 conv[5] = (usf >> 2) & 1;
1079 if (usf_p)
1080 *usf_p = usf;
1081
1082 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1083 conv + 3, 271, conv + 3 + 271);
1084 if (rv)
1085 return -1;
1086
1087 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
1088
1089 return 34;
1090 case 3:
1091 for (i = 675, j = 455; i >= 0; i--) {
1092 if (!gsm0503_puncture_cs3[i])
1093 cB[i] = cB[j--];
1094 else
1095 cB[i] = 0;
1096 }
1097
1098 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
1099 conv, n_errors, n_bits_total);
1100
1101 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
1129 for (i = 0; i < 8; i++) {
1130 for (j = 0, k = 0; j < 12; j++)
1131 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
1132
1133 if (i == 0 || k < best) {
1134 best = k;
1135 usf = i;
1136 }
1137 }
1138
1139 conv[9] = usf & 1;
1140 conv[10] = (usf >> 1) & 1;
1141 conv[11] = (usf >> 2) & 1;
1142 if (usf_p)
1143 *usf_p = usf;
1144
1145 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
1146 conv + 9, 431, conv + 9 + 431);
1147 if (rv) {
1148 *n_bits_total = 456 - 12;
1149 *n_errors = *n_bits_total;
1150 return -1;
1151 }
1152
1153 *n_bits_total = 456 - 12;
1154 *n_errors = 0;
1155
1156 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
1157
1158 return 54;
1159 default:
1160 *n_bits_total = 0;
1161 *n_errors = 0;
1162 break;
1163 }
1164
1165 return -1;
1166}
1167
1168/*
Pau Espin Pedrolf62f0732020-04-07 13:12:11 +02001169 * EGPRS PDTCH DL block encoding
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001170 */
Harald Welteb9946d32017-06-12 09:40:16 +02001171static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001172{
1173 int i;
1174 ubit_t iB[456];
1175 const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1176
Pau Espin Pedrol63ebc362020-04-07 13:15:36 +02001177 gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001178
1179 for (i = 0; i < 4; i++) {
1180 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1181 hl_hn + i * 2, hl_hn + i * 2 + 1);
1182 }
1183
1184 return 0;
1185}
1186
Harald Welteb9946d32017-06-12 09:40:16 +02001187static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001188{
1189 int i;
1190 const ubit_t *up;
1191 ubit_t hi[EGPRS_HDR_HC_MAX];
1192 ubit_t di[EGPRS_DATA_DC_MAX];
1193
1194 gsm0503_mcs5_dl_interleave(hc, dc, hi, di);
1195 up = gsm0503_mcs5_usf_precode_table[usf];
1196
1197 for (i = 0; i < 4; i++) {
1198 gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1199 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1200 }
1201
1202 return 0;
1203}
1204
Harald Welteb9946d32017-06-12 09:40:16 +02001205static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc,
1206 const ubit_t *c1, const ubit_t *c2, int usf, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001207{
1208 int i;
1209 const ubit_t *up;
1210 ubit_t hi[EGPRS_HDR_HC_MAX];
1211 ubit_t di[EGPRS_DATA_C1 * 2];
1212
1213 if (mcs == EGPRS_MCS7)
1214 gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di);
1215 else
1216 gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di);
1217
1218 up = gsm0503_mcs5_usf_precode_table[usf];
1219
1220 for (i = 0; i < 4; i++) {
1221 gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i);
1222 gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]);
1223 }
1224
1225 return 0;
1226}
1227
Harald Welteb9946d32017-06-12 09:40:16 +02001228static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001229{
1230 int i, j;
1231 ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001232 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001233
1234 code = &gsm0503_mcs_dl_codes[mcs];
1235
1236 osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1);
1237 osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp,
1238 code->hdr_len, upp + code->hdr_len);
1239
1240 osmo_conv_encode(code->hdr_conv, upp, C);
1241
1242 /* MCS-5,6 header direct puncture instead of table */
1243 if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) {
1244 memcpy(hc, C, code->hdr_code_len);
1245 hc[99] = hc[98];
1246 return 0;
1247 }
1248
1249 if (!code->hdr_punc) {
1250 /* Invalid MCS-X header puncture matrix */
1251 return -1;
1252 }
1253
1254 for (i = 0, j = 0; i < code->hdr_code_len; i++) {
1255 if (!code->hdr_punc[i])
1256 hc[j++] = C[i];
1257 }
1258
1259 return 0;
1260}
1261
Harald Welteb9946d32017-06-12 09:40:16 +02001262static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001263 int mcs, int p, int blk)
1264{
1265 int i, j, data_len;
1266 ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX];
Harald Welte2f984ea2017-06-12 15:05:21 +02001267 const struct gsm0503_mcs_code *code;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001268
1269 code = &gsm0503_mcs_dl_codes[mcs];
1270
1271 /*
1272 * Dual block - MCS-7,8,9
1273 * Single block - MCS-1,2,3,4,5,6
1274 */
1275 if (mcs >= EGPRS_MCS7)
1276 data_len = code->data_len / 2;
1277 else
1278 data_len = code->data_len;
1279
1280 osmo_pbit2ubit_ext(u, 0, l2_data,
1281 code->usf_len + code->hdr_len + blk * data_len, data_len, 1);
1282
1283 osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len);
1284
1285 osmo_conv_encode(code->data_conv, u, C);
1286
1287 if (!code->data_punc[p]) {
1288 /* Invalid MCS-X data puncture matrix */
1289 return -1;
1290 }
1291
1292 for (i = 0, j = 0; i < code->data_code_len; i++) {
1293 if (!code->data_punc[p][i])
1294 c[j++] = C[i];
1295 }
1296
1297 return 0;
1298}
1299
1300/*
1301 * Parse EGPRS DL header for coding and puncturing scheme (CPS)
1302 *
1303 * Type 1 - MCS-7,8,9
1304 * Type 2 - MCS-5,6
1305 * Type 3 - MCS-1,2,3,4
1306 */
1307static int egprs_parse_dl_cps(struct egprs_cps *cps,
Harald Welteb9946d32017-06-12 09:40:16 +02001308 const union gprs_rlc_dl_hdr_egprs *hdr, int type)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001309{
1310 uint8_t bits;
1311
1312 switch (type) {
1313 case EGPRS_HDR_TYPE1:
1314 bits = hdr->type1.cps;
1315 break;
1316 case EGPRS_HDR_TYPE2:
1317 bits = hdr->type2.cps;
1318 break;
1319 case EGPRS_HDR_TYPE3:
1320 bits = hdr->type3.cps;
1321 break;
1322 default:
1323 return -1;
1324 }
1325
1326 return egprs_get_cps(cps, type, bits);
1327}
1328
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001329/*! EGPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001330 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1331 * \param[in] l2_data L2 (MAC) block to be encoded
1332 * \param[in] l2_len length of l2_data in bytes, used to determine MCS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001333 * \returns number of bits encoded; negative on error */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001334int gsm0503_pdtch_egprs_encode(ubit_t *bursts,
Harald Welteb9946d32017-06-12 09:40:16 +02001335 const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001336{
1337 ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX];
1338 ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2];
1339 uint8_t mcs;
1340 struct egprs_cps cps;
1341 union gprs_rlc_dl_hdr_egprs *hdr;
1342
1343 switch (l2_len) {
1344 case 27:
1345 mcs = EGPRS_MCS1;
1346 break;
1347 case 33:
1348 mcs = EGPRS_MCS2;
1349 break;
1350 case 42:
1351 mcs = EGPRS_MCS3;
1352 break;
1353 case 49:
1354 mcs = EGPRS_MCS4;
1355 break;
1356 case 60:
1357 mcs = EGPRS_MCS5;
1358 break;
1359 case 78:
1360 mcs = EGPRS_MCS6;
1361 break;
1362 case 118:
1363 mcs = EGPRS_MCS7;
1364 break;
1365 case 142:
1366 mcs = EGPRS_MCS8;
1367 break;
1368 case 154:
1369 mcs = EGPRS_MCS9;
1370 break;
1371 default:
1372 return -1;
1373 }
1374
1375 /* Read header for USF and puncturing matrix selection. */
1376 hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data;
1377
1378 switch (mcs) {
1379 case EGPRS_MCS1:
1380 case EGPRS_MCS2:
1381 case EGPRS_MCS3:
1382 case EGPRS_MCS4:
1383 /* Check for valid CPS and matching MCS to message size */
1384 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) ||
1385 (cps.mcs != mcs))
1386 goto bad_header;
1387
1388 egprs_encode_hdr(hc, l2_data, mcs);
1389 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1390 egprs_type3_map(bursts, hc, dc, hdr->type3.usf);
1391 break;
1392 case EGPRS_MCS5:
1393 case EGPRS_MCS6:
1394 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) ||
1395 (cps.mcs != mcs))
1396 goto bad_header;
1397
1398 egprs_encode_hdr(hc, l2_data, mcs);
1399 egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0);
1400 egprs_type2_map(bursts, hc, dc, hdr->type2.usf);
1401 break;
1402 case EGPRS_MCS7:
1403 case EGPRS_MCS8:
1404 case EGPRS_MCS9:
1405 if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) ||
1406 (cps.mcs != mcs))
1407 goto bad_header;
1408
1409 egprs_encode_hdr(hc, l2_data, mcs);
1410 egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0);
1411 egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1);
1412 egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs);
1413 break;
1414 }
1415
1416 return mcs >= EGPRS_MCS5 ?
1417 GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS;
1418
1419bad_header:
1420 /* Invalid EGPRS MCS-X header */
1421 return -1;
1422}
1423
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001424/*! GPRS DL message encoding
Harald Weltec6636782017-06-12 14:59:37 +02001425 * \param[out] bursts caller-allocated buffer for unpacked burst bits
1426 * \param[in] l2_data L2 (MAC) block to be encoded
1427 * \param[in] l2_len length of l2_data in bytes, used to determine CS
Vadim Yanitskiy8055cdd2020-03-30 18:16:38 +07001428 * \returns number of bits encoded; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001429int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001430{
1431 ubit_t iB[456], cB[676];
1432 const ubit_t *hl_hn;
1433 ubit_t conv[334];
1434 int i, j, usf;
1435
1436 switch (l2_len) {
1437 case 23:
1438 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
1439
1440 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
1441
1442 osmo_conv_encode(&gsm0503_xcch, conv, cB);
1443
1444 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
1445
1446 break;
1447 case 34:
1448 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
1449 usf = l2_data[0] & 0x7;
1450
1451 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1452 271, conv + 3 + 271);
1453
1454 memcpy(conv, gsm0503_usf2six[usf], 6);
1455
1456 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
1457
1458 for (i = 0, j = 0; i < 588; i++)
1459 if (!gsm0503_puncture_cs2[i])
1460 cB[j++] = cB[i];
1461
1462 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
1463
1464 break;
1465 case 40:
1466 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
1467 usf = l2_data[0] & 0x7;
1468
1469 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
1470 315, conv + 3 + 315);
1471
1472 memcpy(conv, gsm0503_usf2six[usf], 6);
1473
1474 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
1475
1476 for (i = 0, j = 0; i < 676; i++)
1477 if (!gsm0503_puncture_cs3[i])
1478 cB[j++] = cB[i];
1479
1480 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
1481
1482 break;
1483 case 54:
1484 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
1485 usf = l2_data[0] & 0x7;
1486
1487 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
1488 431, cB + 9 + 431);
1489
1490 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
1491
1492 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
1493
1494 break;
1495 default:
1496 return -1;
1497 }
1498
1499 gsm0503_xcch_interleave(cB, iB);
1500
1501 for (i = 0; i < 4; i++) {
1502 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
1503 hl_hn + i * 2, hl_hn + i * 2 + 1);
1504 }
1505
1506 return GSM0503_GPRS_BURSTS_NBITS;
1507}
1508
1509/*
1510 * GSM TCH/F FR/EFR transcoding
1511 */
1512
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001513/*! assemble a FR codec frame in format as used inside RTP
Harald Weltec6636782017-06-12 14:59:37 +02001514 * \param[out] tch_data Codec frame in RTP format
1515 * \param[in] b_bits Codec frame in 'native' format
1516 * \param[in] net_order FIXME */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001517static void tch_fr_reassemble(uint8_t *tch_data,
Harald Welteb9946d32017-06-12 09:40:16 +02001518 const ubit_t *b_bits, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001519{
1520 int i, j, k, l, o;
1521
1522 tch_data[0] = 0xd << 4;
1523 memset(tch_data + 1, 0, 32);
1524
1525 if (net_order) {
1526 for (i = 0, j = 4; i < 260; i++, j++)
1527 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1528
1529 return;
1530 }
1531
1532 /* reassemble d-bits */
1533 i = 0; /* counts bits */
1534 j = 4; /* counts output bits */
1535 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
1536 l = 0; /* counts element bits */
1537 o = 0; /* offset input bits */
1538 while (i < 260) {
1539 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
1540 if (--k < 0) {
1541 o += gsm0503_gsm_fr_map[l];
1542 k = gsm0503_gsm_fr_map[++l]-1;
1543 }
1544 i++;
1545 j++;
1546 }
1547}
1548
1549static void tch_fr_disassemble(ubit_t *b_bits,
Harald Welteb9946d32017-06-12 09:40:16 +02001550 const uint8_t *tch_data, int net_order)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001551{
1552 int i, j, k, l, o;
1553
1554 if (net_order) {
1555 for (i = 0, j = 4; i < 260; i++, j++)
1556 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1557
1558 return;
1559 }
1560
1561 i = 0; /* counts bits */
1562 j = 4; /* counts input bits */
1563 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
1564 l = 0; /* counts element bits */
1565 o = 0; /* offset output bits */
1566 while (i < 260) {
1567 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1568 if (--k < 0) {
1569 o += gsm0503_gsm_fr_map[l];
1570 k = gsm0503_gsm_fr_map[++l] - 1;
1571 }
1572 i++;
1573 j++;
1574 }
1575}
1576
Harald Weltec6636782017-06-12 14:59:37 +02001577/* assemble a HR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001578static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001579{
1580 int i, j;
1581
1582 tch_data[0] = 0x00; /* F = 0, FT = 000 */
1583 memset(tch_data + 1, 0, 14);
1584
1585 for (i = 0, j = 8; i < 112; i++, j++)
1586 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1587}
1588
Harald Welteb9946d32017-06-12 09:40:16 +02001589static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001590{
1591 int i, j;
1592
1593 for (i = 0, j = 8; i < 112; i++, j++)
1594 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1595}
1596
Harald Weltec6636782017-06-12 14:59:37 +02001597/* assemble a EFR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001598static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001599{
1600 int i, j;
1601
1602 tch_data[0] = 0xc << 4;
1603 memset(tch_data + 1, 0, 30);
1604
1605 for (i = 0, j = 4; i < 244; i++, j++)
1606 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
1607}
1608
Harald Welteb9946d32017-06-12 09:40:16 +02001609static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001610{
1611 int i, j;
1612
1613 for (i = 0, j = 4; i < 244; i++, j++)
1614 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1615}
1616
Harald Weltec6636782017-06-12 14:59:37 +02001617/* assemble a AMR codec frame in format as used inside RTP */
Harald Welteb9946d32017-06-12 09:40:16 +02001618static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001619{
1620 int i, j;
1621
1622 memset(tch_data, 0, (len + 7) >> 3);
1623
1624 for (i = 0, j = 0; i < len; i++, j++)
1625 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
1626}
1627
Harald Welteb9946d32017-06-12 09:40:16 +02001628static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001629{
1630 int i, j;
1631
1632 for (i = 0, j = 0; i < len; i++, j++)
1633 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
1634}
1635
Philipp Maier898c9c62020-02-06 14:25:01 +01001636/* Append STI and MI bits to the SID_UPDATE frame, see also
1637 * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */
1638static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi)
1639{
1640 /* Zero out the space that had been used by the CRC14 */
1641 memset(sid_update + 35, 0, 14);
1642
1643 /* Append STI and MI parameters */
1644 sid_update[35] = sti & 1;
1645 sid_update[36] = mi & 1;
1646 sid_update[37] = mi >> 1 & 1;
1647 sid_update[38] = mi >> 2 & 1;
1648}
1649
1650/* Extract a SID UPDATE fram the sbits of an FR AMR frame */
1651static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits)
1652{
1653
1654 unsigned int i;
1655
1656 sbits += 32;
1657
1658 for (i = 0; i < 53; i++) {
1659 sid_update[0] = sbits[0];
1660 sid_update[1] = sbits[1];
1661 sid_update[2] = sbits[2];
1662 sid_update[3] = sbits[3];
1663 sid_update += 4;
1664 sbits += 8;
1665 }
1666
1667}
1668
Harald Weltec6636782017-06-12 14:59:37 +02001669/* re-arrange according to TS 05.03 Table 2 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001670static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001671{
1672 int i;
1673
1674 for (i = 0; i < 260; i++)
1675 b_bits[gsm610_bitorder[i]] = d_bits[i];
1676}
1677
Harald Weltec6636782017-06-12 14:59:37 +02001678/* re-arrange according to TS 05.03 Table 2 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001679static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001680{
1681 int i;
1682
1683 for (i = 0; i < 260; i++)
1684 d_bits[i] = b_bits[gsm610_bitorder[i]];
1685}
1686
Harald Weltec6636782017-06-12 14:59:37 +02001687/* re-arrange according to TS 05.03 Table 3a (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001688static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001689{
1690 int i;
1691
1692 const uint16_t *map;
1693
1694 if (!d_bits[93] && !d_bits[94])
1695 map = gsm620_unvoiced_bitorder;
1696 else
1697 map = gsm620_voiced_bitorder;
1698
1699 for (i = 0; i < 112; i++)
1700 b_bits[map[i]] = d_bits[i];
1701}
1702
Harald Weltec6636782017-06-12 14:59:37 +02001703/* re-arrange according to TS 05.03 Table 3a (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001704static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001705{
1706 int i;
1707 const uint16_t *map;
1708
1709 if (!b_bits[34] && !b_bits[35])
1710 map = gsm620_unvoiced_bitorder;
1711 else
1712 map = gsm620_voiced_bitorder;
1713
1714 for (i = 0; i < 112; i++)
1715 d_bits[i] = b_bits[map[i]];
1716}
1717
Harald Weltec6636782017-06-12 14:59:37 +02001718/* re-arrange according to TS 05.03 Table 6 (receiver) */
Harald Welteb9946d32017-06-12 09:40:16 +02001719static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001720{
1721 int i;
1722
1723 for (i = 0; i < 260; i++)
1724 b_bits[gsm660_bitorder[i]] = d_bits[i];
1725}
1726
Harald Weltec6636782017-06-12 14:59:37 +02001727/* re-arrange according to TS 05.03 Table 6 (transmitter) */
Harald Welteb9946d32017-06-12 09:40:16 +02001728static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001729{
1730 int i;
1731
1732 for (i = 0; i < 260; i++)
1733 d_bits[i] = b_bits[gsm660_bitorder[i]];
1734}
1735
Harald Weltec6636782017-06-12 14:59:37 +02001736/* extract the 65 protected class1a+1b bits */
Harald Welteb9946d32017-06-12 09:40:16 +02001737static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001738{
1739 int i;
1740
1741 for (i = 0; i < 65; i++)
1742 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
1743}
1744
Harald Welteb9946d32017-06-12 09:40:16 +02001745static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001746{
1747 int i;
1748
1749 for (i = 0; i < 91; i++) {
1750 d[i << 1] = u[i];
1751 d[(i << 1) + 1] = u[184 - i];
1752 }
1753
1754 for (i = 0; i < 3; i++)
1755 p[i] = u[91 + i];
1756}
1757
Harald Welteb9946d32017-06-12 09:40:16 +02001758static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001759{
1760 int i;
1761
1762 for (i = 0; i < 91; i++) {
1763 u[i] = d[i << 1];
1764 u[184 - i] = d[(i << 1) + 1];
1765 }
1766
1767 for (i = 0; i < 3; i++)
1768 u[91 + i] = p[i];
1769}
1770
Harald Welteb9946d32017-06-12 09:40:16 +02001771static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001772{
1773 memcpy(d, u, 95);
1774 memcpy(p, u + 95, 3);
1775}
1776
Harald Welteb9946d32017-06-12 09:40:16 +02001777static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001778{
1779 memcpy(u, d, 95);
1780 memcpy(u + 95, p, 3);
1781}
1782
Harald Welteb9946d32017-06-12 09:40:16 +02001783static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001784{
1785 memcpy(w, s, 71);
1786 w[71] = w[72] = s[69];
1787 memcpy(w + 73, s + 71, 50);
1788 w[123] = w[124] = s[119];
1789 memcpy(w + 125, s + 121, 53);
1790 w[178] = w[179] = s[172];
1791 memcpy(w + 180, s + 174, 50);
1792 w[230] = w[231] = s[222];
1793 memcpy(w + 232, s + 224, 20);
1794 memcpy(w + 252, p, 8);
1795}
1796
Harald Welteb9946d32017-06-12 09:40:16 +02001797static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001798{
1799 int sum;
1800
1801 memcpy(s, w, 71);
1802 sum = s[69] + w[71] + w[72];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001803 s[69] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001804 memcpy(s + 71, w + 73, 50);
1805 sum = s[119] + w[123] + w[124];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001806 s[119] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001807 memcpy(s + 121, w + 125, 53);
1808 sum = s[172] + w[178] + w[179];
1809 s[172] = (sum > 2);
1810 memcpy(s + 174, w + 180, 50);
Niro Mahasinghe834e2ac2017-11-03 12:22:34 +01001811 sum = s[222] + w[230] + w[231];
Niro Mahasinghec526dbc2017-11-03 12:24:30 +01001812 s[222] = (sum >= 2);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001813 memcpy(s + 224, w + 232, 20);
1814 memcpy(p, w + 252, 8);
1815}
1816
Harald Welteb9946d32017-06-12 09:40:16 +02001817static 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 +07001818{
1819 memcpy(u, d, prot);
1820 memcpy(u + prot, p, 6);
1821 memcpy(u + prot + 6, d + prot, len - prot);
1822}
1823
Harald Welteb9946d32017-06-12 09:40:16 +02001824static 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 +07001825{
1826 memcpy(d, u, prot);
1827 memcpy(p, u + prot, 6);
1828 memcpy(d + prot, u + prot + 6, len - prot);
1829}
1830
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001831/*! Perform channel decoding of a FR/EFR channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001832 * \param[out] tch_data Codec frame in RTP payload format
1833 * \param[in] bursts buffer containing the symbols of 8 bursts
1834 * \param[in] net_order FIXME
1835 * \param[in] efr Is this channel using EFR (1) or FR (0)
1836 * \param[out] n_errors Number of detected bit errors
1837 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001838 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001839int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001840 int net_order, int efr, int *n_errors, int *n_bits_total)
1841{
1842 sbit_t iB[912], cB[456], h;
1843 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
1844 int i, rv, len, steal = 0;
1845
Harald Weltec6636782017-06-12 14:59:37 +02001846 /* map from 8 bursts to interleaved data bits (iB) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001847 for (i = 0; i < 8; i++) {
1848 gsm0503_tch_burst_unmap(&iB[i * 114],
1849 &bursts[i * 116], &h, i >> 2);
1850 steal -= h;
1851 }
Harald Weltec6636782017-06-12 14:59:37 +02001852 /* we now have the bits of the four bursts (interface 4 in
1853 * Figure 1a of TS 05.03 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001854
1855 gsm0503_tch_fr_deinterleave(cB, iB);
Harald Weltec6636782017-06-12 14:59:37 +02001856 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001857
1858 if (steal > 0) {
1859 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1860 if (rv) {
1861 /* Error decoding FACCH frame */
1862 return -1;
1863 }
1864
1865 return 23;
1866 }
1867
1868 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Harald Weltec6636782017-06-12 14:59:37 +02001869 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001870
Harald Weltec6636782017-06-12 14:59:37 +02001871 /* input: 'conv', output: d[ata] + p[arity] */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001872 tch_fr_unreorder(d, p, conv);
1873
1874 for (i = 0; i < 78; i++)
1875 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
1876
Harald Weltec6636782017-06-12 14:59:37 +02001877 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001878 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1879 if (rv) {
1880 /* Error checking CRC8 for the FR part of an EFR/FR frame */
1881 return -1;
1882 }
1883
1884 if (efr) {
1885 tch_efr_d_to_w(w, d);
Harald Weltec6636782017-06-12 14:59:37 +02001886 /* we now have the preliminary-coded bits w(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001887
1888 tch_efr_unreorder(s, p, w);
Harald Weltec6636782017-06-12 14:59:37 +02001889 /* we now have the data delivered to the preliminary
1890 * channel encoding unit s(k) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001891
Harald Weltec6636782017-06-12 14:59:37 +02001892 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001893 tch_efr_protected(s, b);
1894
Harald Weltec6636782017-06-12 14:59:37 +02001895 /* perform CRC-8 on 65 most important bits (50 bits of
1896 * class 1a + 15 bits of class 1b) */
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001897 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1898 if (rv) {
1899 /* Error checking CRC8 for the EFR part of an EFR frame */
1900 return -1;
1901 }
1902
1903 tch_efr_reassemble(tch_data, s);
1904
1905 len = GSM_EFR_BYTES;
1906 } else {
1907 tch_fr_d_to_b(w, d);
1908
1909 tch_fr_reassemble(tch_data, w, net_order);
1910
1911 len = GSM_FR_BYTES;
1912 }
1913
1914 return len;
1915}
1916
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001917/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001918 * \param[out] bursts caller-allocated output buffer for bursts bits
1919 * \param[in] tch_data Codec input data in RTP payload format
1920 * \param[in] len Length of \a tch_data in bytes
1921 * \param[in] net_order FIXME
1922 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001923int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001924 int len, int net_order)
1925{
1926 ubit_t iB[912], cB[456], h;
1927 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
1928 int i;
1929
1930 switch (len) {
1931 case GSM_EFR_BYTES: /* TCH EFR */
1932
1933 tch_efr_disassemble(s, tch_data);
1934
1935 tch_efr_protected(s, b);
1936
1937 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
1938
1939 tch_efr_reorder(w, s, p);
1940
1941 tch_efr_w_to_d(d, w);
1942
1943 goto coding_efr_fr;
1944 case GSM_FR_BYTES: /* TCH FR */
1945 tch_fr_disassemble(w, tch_data, net_order);
1946
1947 tch_fr_b_to_d(d, w);
1948
1949coding_efr_fr:
1950 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
1951
1952 tch_fr_reorder(conv, d, p);
1953
1954 memcpy(cB + 378, d + 182, 78);
1955
1956 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
1957
1958 h = 0;
1959
1960 break;
1961 case GSM_MACBLOCK_LEN: /* FACCH */
1962 _xcch_encode_cB(cB, tch_data);
1963
1964 h = 1;
1965
1966 break;
1967 default:
1968 return -1;
1969 }
1970
1971 gsm0503_tch_fr_interleave(cB, iB);
1972
1973 for (i = 0; i < 8; i++) {
1974 gsm0503_tch_burst_map(&iB[i * 114],
1975 &bursts[i * 116], &h, i >> 2);
1976 }
1977
1978 return 0;
1979}
1980
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001981/*! Perform channel decoding of a HR(v1) channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02001982 * \param[out] tch_data Codec frame in RTP payload format
1983 * \param[in] bursts buffer containing the symbols of 8 bursts
1984 * \param[in] odd Odd (1) or even (0) frame number
1985 * \param[out] n_errors Number of detected bit errors
1986 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02001987 * \returns length of bytes used in \a tch_data output buffer; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02001988int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07001989 int *n_errors, int *n_bits_total)
1990{
1991 sbit_t iB[912], cB[456], h;
1992 ubit_t conv[98], b[112], d[112], p[3];
1993 int i, rv, steal = 0;
1994
1995 /* Only unmap the stealing bits */
1996 if (!odd) {
1997 for (i = 0; i < 4; i++) {
1998 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
1999 steal -= h;
2000 }
2001
2002 for (i = 2; i < 5; i++) {
2003 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2004 steal -= h;
2005 }
2006 }
2007
2008 /* If we found a stole FACCH, but only at correct alignment */
2009 if (steal > 0) {
2010 for (i = 0; i < 6; i++) {
2011 gsm0503_tch_burst_unmap(&iB[i * 114],
2012 &bursts[i * 116], NULL, i >> 2);
2013 }
2014
2015 for (i = 2; i < 4; i++) {
2016 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2017 &bursts[i * 116], NULL, 1);
2018 }
2019
2020 gsm0503_tch_fr_deinterleave(cB, iB);
2021
2022 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2023 if (rv) {
2024 /* Error decoding FACCH frame */
2025 return -1;
2026 }
2027
2028 return GSM_MACBLOCK_LEN;
2029 }
2030
2031 for (i = 0; i < 4; i++) {
2032 gsm0503_tch_burst_unmap(&iB[i * 114],
2033 &bursts[i * 116], NULL, i >> 1);
2034 }
2035
2036 gsm0503_tch_hr_deinterleave(cB, iB);
2037
2038 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
2039
2040 tch_hr_unreorder(d, p, conv);
2041
2042 for (i = 0; i < 17; i++)
2043 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
2044
2045 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2046 if (rv) {
2047 /* Error checking CRC8 for an HR frame */
2048 return -1;
2049 }
2050
2051 tch_hr_d_to_b(b, d);
2052
2053 tch_hr_reassemble(tch_data, b);
2054
2055 return 15;
2056}
2057
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002058/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002059 * \param[out] bursts caller-allocated output buffer for bursts bits
2060 * \param[in] tch_data Codec input data in RTP payload format
2061 * \param[in] len Length of \a tch_data in bytes
2062 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002063int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002064{
2065 ubit_t iB[912], cB[456], h;
2066 ubit_t conv[98], b[112], d[112], p[3];
2067 int i;
2068
2069 switch (len) {
2070 case 15: /* TCH HR */
2071 tch_hr_disassemble(b, tch_data);
2072
2073 tch_hr_b_to_d(d, b);
2074
2075 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
2076
2077 tch_hr_reorder(conv, d, p);
2078
2079 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
2080
2081 memcpy(cB + 211, d + 95, 17);
2082
2083 h = 0;
2084
2085 gsm0503_tch_hr_interleave(cB, iB);
2086
2087 for (i = 0; i < 4; i++) {
2088 gsm0503_tch_burst_map(&iB[i * 114],
2089 &bursts[i * 116], &h, i >> 1);
2090 }
2091
2092 break;
2093 case GSM_MACBLOCK_LEN: /* FACCH */
2094 _xcch_encode_cB(cB, tch_data);
2095
2096 h = 1;
2097
2098 gsm0503_tch_fr_interleave(cB, iB);
2099
2100 for (i = 0; i < 6; i++) {
2101 gsm0503_tch_burst_map(&iB[i * 114],
2102 &bursts[i * 116], &h, i >> 2);
2103 }
2104
2105 for (i = 2; i < 4; i++) {
2106 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2107 &bursts[i * 116], &h, 1);
2108 }
2109
2110 break;
2111 default:
2112 return -1;
2113 }
2114
2115 return 0;
2116}
2117
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002118/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002119 * \param[out] tch_data Codec frame in RTP payload format
2120 * \param[in] bursts buffer containing the symbols of 8 bursts
2121 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2122 * \param[in] codec array of active codecs (active codec set)
2123 * \param[in] codecs number of codecs in \a codec
2124 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2125 * \param[out] cmr Output in \a codec_mode_req = 1
2126 * \param[out] n_errors Number of detected bit errors
2127 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002128 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2129 * codec out of range; negative on error
2130 */
Harald Welteb9946d32017-06-12 09:40:16 +02002131int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002132 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2133 uint8_t *cmr, int *n_errors, int *n_bits_total)
2134{
Philipp Maier898c9c62020-02-06 14:25:01 +01002135 return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req,
2136 codec, codecs, ft, cmr, n_errors,
2137 n_bits_total, NULL);
2138}
2139
2140/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2141 * \param[out] tch_data Codec frame in RTP payload format
2142 * \param[in] bursts buffer containing the symbols of 8 bursts
2143 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2144 * \param[in] codec array of active codecs (active codec set)
2145 * \param[in] codecs number of codecs in \a codec
2146 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2147 * \param[out] cmr Output in \a codec_mode_req = 1
2148 * \param[out] n_errors Number of detected bit errors
2149 * \param[out] n_bits_total Total number of bits
2150 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2151 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2152 * codec out of range; negative on error
2153 */
2154int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts,
2155 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2156 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2157{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002158 sbit_t iB[912], cB[456], h;
2159 ubit_t d[244], p[6], conv[250];
2160 int i, j, k, best = 0, rv, len, steal = 0, id = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002161 ubit_t cBd[456];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002162 *n_errors = 0; *n_bits_total = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002163 static ubit_t sid_first_dummy[64] = { 0 };
2164 sbit_t sid_update_enc[256];
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002165
2166 for (i=0; i<8; i++) {
2167 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
2168 steal -= h;
2169 }
2170
2171 gsm0503_tch_fr_deinterleave(cB, iB);
2172
2173 if (steal > 0) {
2174 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2175 if (rv) {
2176 /* Error decoding FACCH frame */
2177 return -1;
2178 }
2179
2180 return GSM_MACBLOCK_LEN;
2181 }
2182
Philipp Maier898c9c62020-02-06 14:25:01 +01002183 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2184 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002185 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2186
Philipp Maier898c9c62020-02-06 14:25:01 +01002187 osmo_sbit2ubit(cBd, cB, 456);
Philipp Maier898c9c62020-02-06 14:25:01 +01002188 *dtx = gsm0503_detect_afs_dtx_frame(n_errors, n_bits_total, cBd);
2189
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002190 switch (*dtx) {
2191 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002192 /* NOTE: The AFS_SID_UPDATE frame is splitted into
2193 * two half rate frames. If the id marker frame
2194 * (AFS_SID_UPDATE) is detected the following frame
2195 * contains the actual comfort noised data part of
2196 * (AFS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002197 if (dtx_prev != AFS_SID_UPDATE)
2198 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002199 *dtx = AFS_SID_UPDATE_CN;
2200
2201 extract_afs_sid_update(sid_update_enc, cB);
2202 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2203 sid_update_enc, conv, n_errors,
2204 n_bits_total);
2205 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2206 35, conv + 35);
2207 if (rv != 0) {
2208 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2209 return -1;
2210 }
2211
2212 tch_amr_sid_update_append(conv, 1,
2213 (codec_mode_req) ? codec[*ft]
2214 : codec[id]);
2215 tch_amr_reassemble(tch_data, conv, 39);
2216 len = 5;
2217 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002218 case AFS_SID_FIRST:
Philipp Maier898c9c62020-02-06 14:25:01 +01002219 tch_amr_sid_update_append(sid_first_dummy, 0,
2220 (codec_mode_req) ? codec[*ft]
2221 : codec[id]);
2222 tch_amr_reassemble(tch_data, conv, 39);
2223 len = 5;
2224 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002225 case AFS_ONSET:
Philipp Maier898c9c62020-02-06 14:25:01 +01002226 len = 0;
2227 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002228 default:
2229 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002230 }
2231 }
2232
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002233 for (i = 0; i < 4; i++) {
2234 for (j = 0, k = 0; j < 8; j++)
2235 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
2236
2237 if (i == 0 || k < best) {
2238 best = k;
2239 id = i;
2240 }
2241 }
2242
2243 /* Check if indicated codec fits into range of codecs */
2244 if (id >= codecs) {
2245 /* Codec mode out of range, return id */
2246 return id;
2247 }
2248
2249 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2250 case 7: /* TCH/AFS12.2 */
2251 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
2252 conv, n_errors, n_bits_total);
2253
2254 tch_amr_unmerge(d, p, conv, 244, 81);
2255
2256 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
2257 if (rv) {
2258 /* Error checking CRC8 for an AMR 12.2 frame */
2259 return -1;
2260 }
2261
2262 tch_amr_reassemble(tch_data, d, 244);
2263
2264 len = 31;
2265
2266 break;
2267 case 6: /* TCH/AFS10.2 */
2268 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
2269 conv, n_errors, n_bits_total);
2270
2271 tch_amr_unmerge(d, p, conv, 204, 65);
2272
2273 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
2274 if (rv) {
2275 /* Error checking CRC8 for an AMR 10.2 frame */
2276 return -1;
2277 }
2278
2279 tch_amr_reassemble(tch_data, d, 204);
2280
2281 len = 26;
2282
2283 break;
2284 case 5: /* TCH/AFS7.95 */
2285 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
2286 conv, n_errors, n_bits_total);
2287
2288 tch_amr_unmerge(d, p, conv, 159, 75);
2289
2290 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
2291 if (rv) {
2292 /* Error checking CRC8 for an AMR 7.95 frame */
2293 return -1;
2294 }
2295
2296 tch_amr_reassemble(tch_data, d, 159);
2297
2298 len = 20;
2299
2300 break;
2301 case 4: /* TCH/AFS7.4 */
2302 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
2303 conv, n_errors, n_bits_total);
2304
2305 tch_amr_unmerge(d, p, conv, 148, 61);
2306
2307 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2308 if (rv) {
2309 /* Error checking CRC8 for an AMR 7.4 frame */
2310 return -1;
2311 }
2312
2313 tch_amr_reassemble(tch_data, d, 148);
2314
2315 len = 19;
2316
2317 break;
2318 case 3: /* TCH/AFS6.7 */
2319 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
2320 conv, n_errors, n_bits_total);
2321
2322 tch_amr_unmerge(d, p, conv, 134, 55);
2323
2324 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2325 if (rv) {
2326 /* Error checking CRC8 for an AMR 6.7 frame */
2327 return -1;
2328 }
2329
2330 tch_amr_reassemble(tch_data, d, 134);
2331
2332 len = 17;
2333
2334 break;
2335 case 2: /* TCH/AFS5.9 */
2336 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
2337 conv, n_errors, n_bits_total);
2338
2339 tch_amr_unmerge(d, p, conv, 118, 55);
2340
2341 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2342 if (rv) {
2343 /* Error checking CRC8 for an AMR 5.9 frame */
2344 return -1;
2345 }
2346
2347 tch_amr_reassemble(tch_data, d, 118);
2348
2349 len = 15;
2350
2351 break;
2352 case 1: /* TCH/AFS5.15 */
2353 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
2354 conv, n_errors, n_bits_total);
2355
2356 tch_amr_unmerge(d, p, conv, 103, 49);
2357
2358 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2359 if (rv) {
2360 /* Error checking CRC8 for an AMR 5.15 frame */
2361 return -1;
2362 }
2363
2364 tch_amr_reassemble(tch_data, d, 103);
2365
2366 len = 13;
2367
2368 break;
2369 case 0: /* TCH/AFS4.75 */
2370 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
2371 conv, n_errors, n_bits_total);
2372
2373 tch_amr_unmerge(d, p, conv, 95, 39);
2374
2375 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2376 if (rv) {
2377 /* Error checking CRC8 for an AMR 4.75 frame */
2378 return -1;
2379 }
2380
2381 tch_amr_reassemble(tch_data, d, 95);
2382
2383 len = 12;
2384
2385 break;
2386 default:
2387 /* Unknown frame type */
2388 *n_bits_total = 448;
2389 *n_errors = *n_bits_total;
2390 return -1;
2391 }
2392
Philipp Maier898c9c62020-02-06 14:25:01 +01002393out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002394 /* Change codec request / indication, if frame is valid */
2395 if (codec_mode_req)
2396 *cmr = id;
2397 else
2398 *ft = id;
2399
2400 return len;
2401}
2402
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002403/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002404 * \param[out] bursts caller-allocated output buffer for bursts bits
2405 * \param[in] tch_data Codec input data in RTP payload format
2406 * \param[in] len Length of \a tch_data in bytes
2407 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2408 * \param[in] codec Array of codecs (active codec set)
2409 * \param[in] codecs Number of entries in \a codec
2410 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2411 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2412 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002413int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002414 int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft,
2415 uint8_t cmr)
2416{
2417 ubit_t iB[912], cB[456], h;
2418 ubit_t d[244], p[6], conv[250];
2419 int i;
2420 uint8_t id;
2421
2422 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2423 _xcch_encode_cB(cB, tch_data);
2424
2425 h = 1;
2426
2427 goto facch;
2428 }
2429
2430 h = 0;
2431
2432 if (codec_mode_req) {
2433 if (cmr >= codecs) {
2434 /* FIXME: CMR ID is not in codec list! */
2435 return -1;
2436 }
2437 id = cmr;
2438 } else {
2439 if (ft >= codecs) {
2440 /* FIXME: FT ID is not in codec list! */
2441 return -1;
2442 }
2443 id = ft;
2444 }
2445
2446 switch (codec[ft]) {
2447 case 7: /* TCH/AFS12.2 */
2448 if (len != 31)
2449 goto invalid_length;
2450
2451 tch_amr_disassemble(d, tch_data, 244);
2452
2453 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
2454
2455 tch_amr_merge(conv, d, p, 244, 81);
2456
2457 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
2458
2459 break;
2460 case 6: /* TCH/AFS10.2 */
2461 if (len != 26)
2462 goto invalid_length;
2463
2464 tch_amr_disassemble(d, tch_data, 204);
2465
2466 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
2467
2468 tch_amr_merge(conv, d, p, 204, 65);
2469
2470 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
2471
2472 break;
2473 case 5: /* TCH/AFS7.95 */
2474 if (len != 20)
2475 goto invalid_length;
2476
2477 tch_amr_disassemble(d, tch_data, 159);
2478
2479 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
2480
2481 tch_amr_merge(conv, d, p, 159, 75);
2482
2483 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
2484
2485 break;
2486 case 4: /* TCH/AFS7.4 */
2487 if (len != 19)
2488 goto invalid_length;
2489
2490 tch_amr_disassemble(d, tch_data, 148);
2491
2492 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2493
2494 tch_amr_merge(conv, d, p, 148, 61);
2495
2496 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
2497
2498 break;
2499 case 3: /* TCH/AFS6.7 */
2500 if (len != 17)
2501 goto invalid_length;
2502
2503 tch_amr_disassemble(d, tch_data, 134);
2504
2505 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2506
2507 tch_amr_merge(conv, d, p, 134, 55);
2508
2509 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
2510
2511 break;
2512 case 2: /* TCH/AFS5.9 */
2513 if (len != 15)
2514 goto invalid_length;
2515
2516 tch_amr_disassemble(d, tch_data, 118);
2517
2518 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2519
2520 tch_amr_merge(conv, d, p, 118, 55);
2521
2522 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
2523
2524 break;
2525 case 1: /* TCH/AFS5.15 */
2526 if (len != 13)
2527 goto invalid_length;
2528
2529 tch_amr_disassemble(d, tch_data, 103);
2530
2531 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2532
2533 tch_amr_merge(conv, d, p, 103, 49);
2534
2535 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
2536
2537 break;
2538 case 0: /* TCH/AFS4.75 */
2539 if (len != 12)
2540 goto invalid_length;
2541
2542 tch_amr_disassemble(d, tch_data, 95);
2543
2544 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
2545
2546 tch_amr_merge(conv, d, p, 95, 39);
2547
2548 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
2549
2550 break;
2551 default:
2552 /* FIXME: FT %ft is not supported */
2553 return -1;
2554 }
2555
2556 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
2557
2558facch:
2559 gsm0503_tch_fr_interleave(cB, iB);
2560
2561 for (i = 0; i < 8; i++) {
2562 gsm0503_tch_burst_map(&iB[i * 114],
2563 &bursts[i * 116], &h, i >> 2);
2564 }
2565
2566 return 0;
2567
2568invalid_length:
2569 /* FIXME: payload length %len does not comply with codec type %ft */
2570 return -1;
2571}
2572
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002573/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002574 * \param[out] tch_data Codec frame in RTP payload format
2575 * \param[in] bursts buffer containing the symbols of 8 bursts
2576 * \param[in] odd Is this an odd (1) or even (0) frame number?
2577 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2578 * \param[in] codec array of active codecs (active codec set)
2579 * \param[in] codecs number of codecs in \a codec
2580 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2581 * \param[out] cmr Output in \a codec_mode_req = 1
2582 * \param[out] n_errors Number of detected bit errors
2583 * \param[out] n_bits_total Total number of bits
Pau Espin Pedrolf81d03f2018-07-19 13:49:41 +02002584 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2585 * codec out of range; negative on error
2586 */
Harald Welteb9946d32017-06-12 09:40:16 +02002587int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002588 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2589 uint8_t *cmr, int *n_errors, int *n_bits_total)
2590{
Philipp Maier898c9c62020-02-06 14:25:01 +01002591 return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req,
2592 codec, codecs, ft, cmr, n_errors,
2593 n_bits_total, NULL);
2594}
2595
2596/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
2597 * \param[out] tch_data Codec frame in RTP payload format
2598 * \param[in] bursts buffer containing the symbols of 8 bursts
2599 * \param[in] odd Is this an odd (1) or even (0) frame number?
2600 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
2601 * \param[in] codec array of active codecs (active codec set)
2602 * \param[in] codecs number of codecs in \a codec
2603 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
2604 * \param[out] cmr Output in \a codec_mode_req = 1
2605 * \param[out] n_errors Number of detected bit errors
2606 * \param[out] n_bits_total Total number of bits
2607 * \param[inout] dtx DTX frame type output, previous DTX frame type input
2608 * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3])
2609 * codec out of range; negative on error
2610 */
2611int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd,
2612 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
2613 uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx)
2614{
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002615 sbit_t iB[912], cB[456], h;
2616 ubit_t d[244], p[6], conv[135];
2617 int i, j, k, best = 0, rv, len, steal = 0, id = 0;
Philipp Maier898c9c62020-02-06 14:25:01 +01002618 ubit_t cBd[456];
2619 static ubit_t sid_first_dummy[64] = { 0 };
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002620
2621 /* only unmap the stealing bits */
2622 if (!odd) {
2623 for (i = 0; i < 4; i++) {
2624 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
2625 steal -= h;
2626 }
2627 for (i = 2; i < 5; i++) {
2628 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
2629 steal -= h;
2630 }
2631 }
2632
2633 /* if we found a stole FACCH, but only at correct alignment */
2634 if (steal > 0) {
2635 for (i = 0; i < 6; i++) {
2636 gsm0503_tch_burst_unmap(&iB[i * 114],
2637 &bursts[i * 116], NULL, i >> 2);
2638 }
2639
2640 for (i = 2; i < 4; i++) {
2641 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
2642 &bursts[i * 116], NULL, 1);
2643 }
2644
2645 gsm0503_tch_fr_deinterleave(cB, iB);
2646
2647 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
2648 if (rv) {
2649 /* Error decoding FACCH frame */
2650 return -1;
2651 }
2652
2653 return GSM_MACBLOCK_LEN;
2654 }
2655
2656 for (i = 0; i < 4; i++) {
2657 gsm0503_tch_burst_unmap(&iB[i * 114],
2658 &bursts[i * 116], NULL, i >> 1);
2659 }
2660
2661 gsm0503_tch_hr_deinterleave(cB, iB);
2662
Philipp Maier898c9c62020-02-06 14:25:01 +01002663 /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */
2664 if (dtx) {
Vadim Yanitskiybf2d5e92022-05-15 15:58:10 +03002665 const enum gsm0503_amr_dtx_frames dtx_prev = *dtx;
2666
Philipp Maier898c9c62020-02-06 14:25:01 +01002667 osmo_sbit2ubit(cBd, cB, 456);
Philipp Maier898c9c62020-02-06 14:25:01 +01002668 *dtx = gsm0503_detect_ahs_dtx_frame(n_errors, n_bits_total, cBd);
2669
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002670 switch (*dtx) {
2671 case AMR_OTHER:
Philipp Maier898c9c62020-02-06 14:25:01 +01002672 /* NOTE: The AHS_SID_UPDATE frame is splitted into
2673 * two half rate frames. If the id marker frame
2674 * (AHS_SID_UPDATE) is detected the following frame
2675 * contains the actual comfort noised data part of
2676 * (AHS_SID_UPDATE_CN). */
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002677 if (dtx_prev != AHS_SID_UPDATE)
2678 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002679 *dtx = AHS_SID_UPDATE_CN;
2680
2681 osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update,
2682 cB + 16, conv, n_errors,
2683 n_bits_total);
2684 rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv,
2685 35, conv + 35);
2686 if (rv != 0) {
2687 /* Error checking CRC14 for an AMR SID_UPDATE frame */
2688 return -1;
2689 }
2690
2691 tch_amr_sid_update_append(conv, 1,
2692 (codec_mode_req) ? codec[*ft]
2693 : codec[id]);
2694 tch_amr_reassemble(tch_data, conv, 39);
2695 len = 5;
2696 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002697 case AHS_SID_FIRST_P2:
Philipp Maier898c9c62020-02-06 14:25:01 +01002698 tch_amr_sid_update_append(sid_first_dummy, 0,
2699 (codec_mode_req) ? codec[*ft]
2700 : codec[id]);
2701 tch_amr_reassemble(tch_data, sid_first_dummy, 39);
2702 len = 5;
2703 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002704 case AHS_SID_UPDATE:
2705 case AHS_ONSET:
2706 case AHS_SID_FIRST_INH:
2707 case AHS_SID_UPDATE_INH:
2708 case AHS_SID_FIRST_P1:
Philipp Maier898c9c62020-02-06 14:25:01 +01002709 len = 0;
2710 goto out;
Vadim Yanitskiyeebaccd2022-05-15 14:46:08 +03002711 default:
2712 break;
Philipp Maier898c9c62020-02-06 14:25:01 +01002713 }
2714 }
2715
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002716 for (i = 0; i < 4; i++) {
2717 for (j = 0, k = 0; j < 4; j++)
2718 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
2719
2720 if (i == 0 || k < best) {
2721 best = k;
2722 id = i;
2723 }
2724 }
2725
2726 /* Check if indicated codec fits into range of codecs */
2727 if (id >= codecs) {
2728 /* Codec mode out of range, return id */
2729 return id;
2730 }
2731
2732 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
2733 case 5: /* TCH/AHS7.95 */
2734 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
2735 conv, n_errors, n_bits_total);
2736
2737 tch_amr_unmerge(d, p, conv, 123, 67);
2738
2739 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
2740 if (rv) {
2741 /* Error checking CRC8 for an AMR 7.95 frame */
2742 return -1;
2743 }
2744
2745 for (i = 0; i < 36; i++)
2746 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
2747
2748 tch_amr_reassemble(tch_data, d, 159);
2749
2750 len = 20;
2751
2752 break;
2753 case 4: /* TCH/AHS7.4 */
2754 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
2755 conv, n_errors, n_bits_total);
2756
2757 tch_amr_unmerge(d, p, conv, 120, 61);
2758
2759 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
2760 if (rv) {
2761 /* Error checking CRC8 for an AMR 7.4 frame */
2762 return -1;
2763 }
2764
2765 for (i = 0; i < 28; i++)
2766 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
2767
2768 tch_amr_reassemble(tch_data, d, 148);
2769
2770 len = 19;
2771
2772 break;
2773 case 3: /* TCH/AHS6.7 */
2774 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
2775 conv, n_errors, n_bits_total);
2776
2777 tch_amr_unmerge(d, p, conv, 110, 55);
2778
2779 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2780 if (rv) {
2781 /* Error checking CRC8 for an AMR 6.7 frame */
2782 return -1;
2783 }
2784
2785 for (i = 0; i < 24; i++)
2786 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
2787
2788 tch_amr_reassemble(tch_data, d, 134);
2789
2790 len = 17;
2791
2792 break;
2793 case 2: /* TCH/AHS5.9 */
2794 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
2795 conv, n_errors, n_bits_total);
2796
2797 tch_amr_unmerge(d, p, conv, 102, 55);
2798
2799 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
2800 if (rv) {
2801 /* Error checking CRC8 for an AMR 5.9 frame */
2802 return -1;
2803 }
2804
2805 for (i = 0; i < 16; i++)
2806 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
2807
2808 tch_amr_reassemble(tch_data, d, 118);
2809
2810 len = 15;
2811
2812 break;
2813 case 1: /* TCH/AHS5.15 */
2814 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
2815 conv, n_errors, n_bits_total);
2816
2817 tch_amr_unmerge(d, p, conv, 91, 49);
2818
2819 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
2820 if (rv) {
2821 /* Error checking CRC8 for an AMR 5.15 frame */
2822 return -1;
2823 }
2824
2825 for (i = 0; i < 12; i++)
2826 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
2827
2828 tch_amr_reassemble(tch_data, d, 103);
2829
2830 len = 13;
2831
2832 break;
2833 case 0: /* TCH/AHS4.75 */
2834 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
2835 conv, n_errors, n_bits_total);
2836
2837 tch_amr_unmerge(d, p, conv, 83, 39);
2838
2839 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
2840 if (rv) {
2841 /* Error checking CRC8 for an AMR 4.75 frame */
2842 return -1;
2843 }
2844
2845 for (i = 0; i < 12; i++)
2846 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
2847
2848 tch_amr_reassemble(tch_data, d, 95);
2849
2850 len = 12;
2851
2852 break;
2853 default:
2854 /* Unknown frame type */
2855 *n_bits_total = 159;
2856 *n_errors = *n_bits_total;
2857 return -1;
2858 }
2859
Philipp Maier898c9c62020-02-06 14:25:01 +01002860out:
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002861 /* Change codec request / indication, if frame is valid */
2862 if (codec_mode_req)
2863 *cmr = id;
2864 else
2865 *ft = id;
2866
2867 return len;
2868}
2869
Neels Hofmeyr87e45502017-06-20 00:17:59 +02002870/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02002871 * \param[out] bursts caller-allocated output buffer for bursts bits
2872 * \param[in] tch_data Codec input data in RTP payload format
2873 * \param[in] len Length of \a tch_data in bytes
2874 * \param[in] codec_mode_req Use CMR (1) or FT (0)
2875 * \param[in] codec Array of codecs (active codec set)
2876 * \param[in] codecs Number of entries in \a codec
2877 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
2878 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
2879 * \returns 0 in case of success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02002880int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07002881 int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft,
2882 uint8_t cmr)
2883{
2884 ubit_t iB[912], cB[456], h;
2885 ubit_t d[244], p[6], conv[135];
2886 int i;
2887 uint8_t id;
2888
2889 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
2890 _xcch_encode_cB(cB, tch_data);
2891
2892 h = 1;
2893
2894 gsm0503_tch_fr_interleave(cB, iB);
2895
2896 for (i = 0; i < 6; i++)
2897 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
2898 &h, i >> 2);
2899 for (i = 2; i < 4; i++)
2900 gsm0503_tch_burst_map(&iB[i * 114 + 456],
2901 &bursts[i * 116], &h, 1);
2902
2903 return 0;
2904 }
2905
2906 h = 0;
2907
2908 if (codec_mode_req) {
2909 if (cmr >= codecs) {
2910 /* FIXME: CMR ID %d not in codec list */
2911 return -1;
2912 }
2913 id = cmr;
2914 } else {
2915 if (ft >= codecs) {
2916 /* FIXME: FT ID %d not in codec list */
2917 return -1;
2918 }
2919 id = ft;
2920 }
2921
2922 switch (codec[ft]) {
2923 case 5: /* TCH/AHS7.95 */
2924 if (len != 20)
2925 goto invalid_length;
2926
2927 tch_amr_disassemble(d, tch_data, 159);
2928
2929 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
2930
2931 tch_amr_merge(conv, d, p, 123, 67);
2932
2933 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
2934
2935 memcpy(cB + 192, d + 123, 36);
2936
2937 break;
2938 case 4: /* TCH/AHS7.4 */
2939 if (len != 19)
2940 goto invalid_length;
2941
2942 tch_amr_disassemble(d, tch_data, 148);
2943
2944 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
2945
2946 tch_amr_merge(conv, d, p, 120, 61);
2947
2948 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
2949
2950 memcpy(cB + 200, d + 120, 28);
2951
2952 break;
2953 case 3: /* TCH/AHS6.7 */
2954 if (len != 17)
2955 goto invalid_length;
2956
2957 tch_amr_disassemble(d, tch_data, 134);
2958
2959 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2960
2961 tch_amr_merge(conv, d, p, 110, 55);
2962
2963 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
2964
2965 memcpy(cB + 204, d + 110, 24);
2966
2967 break;
2968 case 2: /* TCH/AHS5.9 */
2969 if (len != 15)
2970 goto invalid_length;
2971
2972 tch_amr_disassemble(d, tch_data, 118);
2973
2974 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
2975
2976 tch_amr_merge(conv, d, p, 102, 55);
2977
2978 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
2979
2980 memcpy(cB + 212, d + 102, 16);
2981
2982 break;
2983 case 1: /* TCH/AHS5.15 */
2984 if (len != 13)
2985 goto invalid_length;
2986
2987 tch_amr_disassemble(d, tch_data, 103);
2988
2989 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
2990
2991 tch_amr_merge(conv, d, p, 91, 49);
2992
2993 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
2994
2995 memcpy(cB + 216, d + 91, 12);
2996
2997 break;
2998 case 0: /* TCH/AHS4.75 */
2999 if (len != 12)
3000 goto invalid_length;
3001
3002 tch_amr_disassemble(d, tch_data, 95);
3003
3004 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
3005
3006 tch_amr_merge(conv, d, p, 83, 39);
3007
3008 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
3009
3010 memcpy(cB + 216, d + 83, 12);
3011
3012 break;
3013 default:
3014 /* FIXME: FT %ft is not supported */
3015 return -1;
3016 }
3017
Philipp Maiercfea39b2021-08-31 16:02:31 +02003018 memcpy(cB, gsm0503_ahs_ic_ubit[id], 4);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003019
3020 gsm0503_tch_hr_interleave(cB, iB);
3021
3022 for (i = 0; i < 4; i++)
3023 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
3024
3025 return 0;
3026
3027invalid_length:
3028 /* FIXME: payload length %len does not comply with codec type %ft */
3029 return -1;
3030}
3031
3032/*
3033 * GSM RACH transcoding
3034 */
3035
3036/*
3037 * GSM RACH apply BSIC to parity
3038 *
3039 * p(j) = p(j) xor b(j) j = 0, ..., 5
3040 * b(0) = MSB of PLMN colour code
3041 * b(5) = LSB of BS colour code
3042 */
Max32e56412017-10-16 14:58:00 +02003043static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003044{
3045 int i;
3046
3047 /* Apply it */
3048 for (i = 0; i < 6; i++)
Max32e56412017-10-16 14:58:00 +02003049 d[start + i] ^= ((bsic >> (5 - i)) & 1);
3050}
3051
Harald Welte6950b192018-02-26 11:48:00 +01003052static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
3053 int *n_errors, int *n_bits_total)
Max32e56412017-10-16 14:58:00 +02003054{
3055 ubit_t conv[17];
3056 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
3057 int rv;
3058
Harald Welte6950b192018-02-26 11:48:00 +01003059 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
3060 n_errors, n_bits_total);
Max32e56412017-10-16 14:58:00 +02003061
3062 rach_apply_bsic(conv, bsic, nbits);
3063
3064 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3065 if (rv)
3066 return -1;
3067
3068 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
3069
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003070 return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0];
Max32e56412017-10-16 14:58:00 +02003071}
3072
3073/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3074 * \param[out] ra output buffer for RACH data
3075 * \param[in] burst Input burst data
3076 * \param[in] bsic BSIC used in this cell
3077 * \returns 0 on success; negative on error (e.g. CRC error) */
3078int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
3079{
Harald Welte6950b192018-02-26 11:48:00 +01003080 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
Max32e56412017-10-16 14:58:00 +02003081
3082 if (r < 0)
3083 return r;
3084
3085 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003086
3087 return 0;
3088}
3089
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003090/*! Decode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003091 * \param[out] ra output buffer for RACH data
3092 * \param[in] burst Input burst data
3093 * \param[in] bsic BSIC used in this cell
3094 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003095int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003096{
Harald Welte6950b192018-02-26 11:48:00 +01003097 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
3098 if (r < 0)
3099 return r;
3100
3101 *ra = r;
3102 return 0;
3103}
3104
3105/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
3106 * \param[out] ra output buffer for RACH data
3107 * \param[in] burst Input burst data
3108 * \param[in] bsic BSIC used in this cell
3109 * \param[out] n_errors Number of detected bit errors
3110 * \param[out] n_bits_total Total number of bits
3111 * \returns 0 on success; negative on error (e.g. CRC error) */
3112int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
3113 int *n_errors, int *n_bits_total)
3114{
3115 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
3116 if (r < 0)
3117 return r;
3118
3119 *ra = r;
3120 return 0;
3121}
3122
3123/*! Decode the (8-bit) RACH according to TS 05.03
3124 * \param[out] ra output buffer for RACH data
3125 * \param[in] burst Input burst data
3126 * \param[in] bsic BSIC used in this cell
3127 * \param[out] n_errors Number of detected bit errors
3128 * \param[out] n_bits_total Total number of bits
3129 * \returns 0 on success; negative on error (e.g. CRC error) */
3130int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
3131 int *n_errors, int *n_bits_total)
3132{
3133 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003134
Max32e56412017-10-16 14:58:00 +02003135 if (r < 0)
3136 return r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003137
Max32e56412017-10-16 14:58:00 +02003138 *ra = r;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003139
3140 return 0;
3141}
3142
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003143/*! Encode the (8-bit) RACH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003144 * \param[out] burst Caller-allocated output burst buffer
3145 * \param[in] ra Input RACH data
3146 * \param[in] bsic BSIC used in this cell
3147 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003148int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003149{
Max32e56412017-10-16 14:58:00 +02003150 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
3151}
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003152
Max32e56412017-10-16 14:58:00 +02003153/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
3154 * \param[out] burst Caller-allocated output burst buffer
3155 * \param[in] ra11 Input RACH data
3156 * \param[in] bsic BSIC used in this cell
3157 * \param[in] is_11bit whether given RA is 11 bit or not
3158 * \returns 0 on success; negative on error */
3159int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
3160{
3161 ubit_t conv[17];
3162 uint8_t ra[2] = { 0 }, nbits = 8;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003163
Max32e56412017-10-16 14:58:00 +02003164 if (is_11bit) {
Vadim Yanitskiy9e713f32020-03-31 19:40:09 +07003165 ra[0] = (uint8_t) (ra11 >> 3);
3166 ra[1] = (uint8_t) (ra11 & 0x07);
Max32e56412017-10-16 14:58:00 +02003167 nbits = 11;
3168 } else
3169 ra[0] = (uint8_t)ra11;
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003170
Max32e56412017-10-16 14:58:00 +02003171 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003172
Max32e56412017-10-16 14:58:00 +02003173 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
3174
3175 rach_apply_bsic(conv, bsic, nbits);
3176
3177 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003178
3179 return 0;
3180}
3181
3182/*
3183 * GSM SCH transcoding
3184 */
Harald Weltec6636782017-06-12 14:59:37 +02003185
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003186/*! Decode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003187 * \param[out] sb_info output buffer for SCH data
3188 * \param[in] burst Input burst data
3189 * \returns 0 on success; negative on error (e.g. CRC error) */
Harald Welteb9946d32017-06-12 09:40:16 +02003190int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003191{
3192 ubit_t conv[35];
3193 int rv;
3194
3195 osmo_conv_decode(&gsm0503_sch, burst, conv);
3196
3197 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3198 if (rv)
3199 return -1;
3200
3201 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
3202
3203 return 0;
3204}
3205
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003206/*! Encode the SCH according to TS 05.03
Harald Weltec6636782017-06-12 14:59:37 +02003207 * \param[out] burst Caller-allocated output burst buffer
3208 * \param[in] sb_info Input SCH data
3209 * \returns 0 on success; negative on error */
Harald Welteb9946d32017-06-12 09:40:16 +02003210int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Vadim Yanitskiy3262f822016-09-23 01:48:59 +07003211{
3212 ubit_t conv[35];
3213
3214 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
3215
3216 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
3217
3218 osmo_conv_encode(&gsm0503_sch, conv, burst);
3219
3220 return 0;
3221}
Harald Weltec6636782017-06-12 14:59:37 +02003222
3223/*! @} */