blob: e83e536c168f98eb7087830e86836c58c690d9e5 [file] [log] [blame]
Piotr Krysik70c25a12017-01-03 08:01:23 +01001/*
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>
Piotr Krysik9e2e8352018-02-27 12:16:25 +01005 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
Piotr Krysik70c25a12017-01-03 08:01:23 +01006 *
7 * All Rights Reserved
8 *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01009 * SPDX-License-Identifier: GPL-2.0+
10 *
Piotr Krysik70c25a12017-01-03 08:01:23 +010011 * 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.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include <stdio.h>
27#include <stdint.h>
28#include <string.h>
29#include <stdlib.h>
Piotr Krysik9e2e8352018-02-27 12:16:25 +010030#include <errno.h>
Piotr Krysik70c25a12017-01-03 08:01:23 +010031
Piotr Krysikb3bd68e2018-02-27 14:37:01 +010032#define GSM_MACBLOCK_LEN 23
33
Piotr Krysik70c25a12017-01-03 08:01:23 +010034#include <osmocom/core/bits.h>
35#include <osmocom/core/conv.h>
Piotr Krysik9e2e8352018-02-27 12:16:25 +010036//#include <osmocom/core/utils.h>
Piotr Krysik70c25a12017-01-03 08:01:23 +010037#include <osmocom/core/crcgen.h>
38#include <osmocom/core/endian.h>
39
Piotr Krysik9e2e8352018-02-27 12:16:25 +010040//#include <osmocom/gsm/protocol/gsm_04_08.h>
Piotr Krysikb3bd68e2018-02-27 14:37:01 +010041/*#include <osmocom/gprs/protocol/gsm_04_60.h>*/
42/*#include <osmocom/gprs/gprs_rlc.h>*/
Piotr Krysik9e2e8352018-02-27 12:16:25 +010043
44#include <osmocom/gsm/gsm0503.h>
Piotr Krysik70c25a12017-01-03 08:01:23 +010045#include <osmocom/codec/codec.h>
46
Piotr Krysik9e2e8352018-02-27 12:16:25 +010047#include <osmocom/coding/gsm0503_interleaving.h>
48#include <osmocom/coding/gsm0503_mapping.h>
49#include <osmocom/coding/gsm0503_tables.h>
50#include <osmocom/coding/gsm0503_coding.h>
51#include <osmocom/coding/gsm0503_parity.h>
Piotr Krysik70c25a12017-01-03 08:01:23 +010052
Piotr Krysik9e2e8352018-02-27 12:16:25 +010053/*! \mainpage libosmocoding Documentation
54 *
55 * \section sec_intro Introduction
56 * This library is a collection of definitions, tables and functions
57 * implementing the GSM/GPRS/EGPRS channel coding (and decoding) as
58 * specified in 3GPP TS 05.03 / 45.003.
59 *
60 * libosmocoding is developed as part of the Osmocom (Open Source Mobile
61 * Communications) project, a community-based, collaborative development
62 * project to create Free and Open Source implementations of mobile
63 * communications systems. For more information about Osmocom, please
64 * see https://osmocom.org/
65 *
66 * \section sec_copyright Copyright and License
67 * Copyright © 2013 by Andreas Eversberg\n
68 * Copyright © 2015 by Alexander Chemeris\n
69 * Copyright © 2016 by Tom Tsou\n
70 * Documentation Copyright © 2017 by Harald Welte\n
71 * All rights reserved. \n\n
72 * The source code of libosmocoding is licensed under the terms of the GNU
73 * General Public License as published by the Free Software Foundation;
74 * either version 2 of the License, or (at your option) any later
75 * version.\n
76 * See <http://www.gnu.org/licenses/> or COPYING included in the source
77 * code package istelf.\n
78 * The information detailed here is provided AS IS with NO WARRANTY OF
79 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
80 * FITNESS FOR A PARTICULAR PURPOSE.
81 * \n\n
82 *
83 * \section sec_tracker Homepage + Issue Tracker
84 * libosmocoding is distributed as part of libosmocore and shares its
85 * project page at http://osmocom.org/projects/libosmocore
86 *
87 * An Issue Tracker can be found at
88 * https://osmocom.org/projects/libosmocore/issues
89 *
90 * \section sec_contact Contact and Support
91 * Community-based support is available at the OpenBSC mailing list
92 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
93 * Commercial support options available upon request from
94 * <http://sysmocom.de/>
95 */
Piotr Krysik70c25a12017-01-03 08:01:23 +010096
97
Piotr Krysik9e2e8352018-02-27 12:16:25 +010098/*! \addtogroup coding
99 * @{
100 *
101 * GSM TS 05.03 coding
102 *
103 * This module is the "master module" of libosmocoding. It uses the
104 * various other modules (mapping, parity, interleaving) in order to
105 * implement the complete channel coding (and decoding) chain for the
106 * various channel types as defined in TS 05.03 / 45.003.
107 *
108 * \file gsm0503_coding.c */
109
Piotr Krysik70c25a12017-01-03 08:01:23 +0100110/*
111 * EGPRS coding limits
112 */
113
114/* Max header size with parity bits */
115#define EGPRS_HDR_UPP_MAX 54
116
117/* Max encoded header size */
118#define EGPRS_HDR_C_MAX 162
119
120/* Max punctured header size */
121#define EGPRS_HDR_HC_MAX 160
122
123/* Max data block size with parity bits */
124#define EGPRS_DATA_U_MAX 612
125
126/* Max encoded data block size */
127#define EGPRS_DATA_C_MAX 1836
128
129/* Max single block punctured data size */
130#define EGPRS_DATA_DC_MAX 1248
131
132/* Dual block punctured data size */
133#define EGPRS_DATA_C1 612
134#define EGPRS_DATA_C2 EGPRS_DATA_C1
135
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100136/*! union across the three different EGPRS Uplink header types *
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100137union gprs_rlc_ul_hdr_egprs {
138 struct gprs_rlc_ul_header_egprs_1 type1;
139 struct gprs_rlc_ul_header_egprs_2 type2;
140 struct gprs_rlc_ul_header_egprs_3 type3;
141};
Piotr Krysik70c25a12017-01-03 08:01:23 +0100142
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100143/*! union across the three different EGPRS Downlink header types *
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100144union gprs_rlc_dl_hdr_egprs {
145 struct gprs_rlc_dl_header_egprs_1 type1;
146 struct gprs_rlc_dl_header_egprs_2 type2;
147 struct gprs_rlc_dl_header_egprs_3 type3;
148};
149
150/*! Structure describing a Modulation and Coding Scheme */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100151struct gsm0503_mcs_code {
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100152 /*! Modulation and Coding Scheme (MSC) number */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100153 uint8_t mcs;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100154 /*! Length of Uplink Stealing Flag (USF) in bits */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100155 uint8_t usf_len;
156
157 /* Header coding */
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100158 /*! Length of header (bits) */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100159 uint8_t hdr_len;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100160 /*! Length of header convolutional code */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100161 uint8_t hdr_code_len;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100162 /*! Length of header code puncturing sequence */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100163 uint8_t hdr_punc_len;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100164 /*! header convolutional code */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100165 const struct osmo_conv_code *hdr_conv;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100166 /*! header puncturing sequence */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100167 const uint8_t *hdr_punc;
168
169 /* Data coding */
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100170 /*! length of data (bits) */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100171 uint16_t data_len;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100172 /*! length of data convolutional code */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100173 uint16_t data_code_len;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100174 /*! length of data code puncturing sequence */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100175 uint16_t data_punc_len;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100176 /*! data convolutional code */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100177 const struct osmo_conv_code *data_conv;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100178 /*! data puncturing sequences */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100179 const uint8_t *data_punc[3];
180};
181
182static int osmo_conv_decode_ber(const struct osmo_conv_code *code,
183 const sbit_t *input, ubit_t *output,
184 int *n_errors, int *n_bits_total)
185{
186 int res, i, coded_len;
187 ubit_t recoded[EGPRS_DATA_C_MAX];
188
189 res = osmo_conv_decode(code, input, output);
190
191 if (n_bits_total || n_errors) {
192 coded_len = osmo_conv_encode(code, output, recoded);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100193 //OSMO_ASSERT(sizeof(recoded) / sizeof(recoded[0]) >= coded_len);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100194 }
195
196 /* Count bit errors */
197 if (n_errors) {
198 *n_errors = 0;
199 for (i = 0; i < coded_len; i++) {
200 if (!((recoded[i] && input[i] < 0) ||
201 (!recoded[i] && input[i] > 0)) )
202 *n_errors += 1;
203 }
204 }
205
206 if (n_bits_total)
207 *n_bits_total = coded_len;
208
209 return res;
210}
211
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100212/*! convenience wrapper for decoding coded bits
213 * \param[out] l2_data caller-allocated buffer for L2 Frame
214 * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3
215 * \param[out] n_errors Number of detected errors
216 * \param[out] n_bits_total Number of total coded bits
217 * \returns 0 on success; -1 on CRC error */
218static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100219 int *n_errors, int *n_bits_total)
220{
221 ubit_t conv[224];
222 int rv;
223
224 osmo_conv_decode_ber(&gsm0503_xcch, cB,
225 conv, n_errors, n_bits_total);
226
227 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
228 conv, 184, conv + 184);
229 if (rv)
230 return -1;
231
232 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
233
234 return 0;
235}
236
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100237/*! convenience wrapper for encoding to coded bits
238 * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3
239 * \param[out] l2_data to-be-encoded L2 Frame
240 * \returns 0 */
241static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100242{
243 ubit_t conv[224];
244
245 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
246
247 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
248
249 osmo_conv_encode(&gsm0503_xcch, conv, cB);
250
251 return 0;
252}
253
254/*
255 * GSM xCCH block transcoding
256 */
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100257
258/*! Decoding of xCCH data from bursts to L2 frame
259 * \param[out] l2_data caller-allocated output data buffer
260 * \param[in] bursts four GSM bursts in soft-bits
261 * \param[out] n_errors Number of detected errors
262 * \param[out] n_bits_total Number of total coded bits
263 */
264int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100265 int *n_errors, int *n_bits_total)
266{
267 sbit_t iB[456], cB[456];
268 int i;
269
270 for (i = 0; i < 4; i++)
271 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL);
272
273 gsm0503_xcch_deinterleave(cB, iB);
274
275 return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total);
276}
277
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100278/*! Encoding of xCCH data from L2 frame to bursts
279 * \param[out] bursts caller-allocated burst data (unpacked bits)
280 * \param[in] l2_data L2 input data (MAC block)
281 * \returns 0
282 */
283int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100284{
285 ubit_t iB[456], cB[456], hl = 1, hn = 1;
286 int i;
287
288 _xcch_encode_cB(cB, l2_data);
289
290 gsm0503_xcch_interleave(cB, iB);
291
292 for (i = 0; i < 4; i++)
293 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn);
294
295 return 0;
296}
297
298
299
300/*
301 * GSM PDTCH block transcoding
302 */
303
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100304/*! Decode GPRS PDTCH
305 * \param[out] l2_data caller-allocated buffer for L2 Frame
306 * \param[in] bursts burst input data as soft unpacked bits
307 * \param[out] usf_p uplink stealing flag
308 * \param[out] n_errors number of detected bit-errors
309 * \param[out] n_bits_total total number of dcoded bits
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100310 * \returns 0 on success; negative on error *
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100311int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100312 int *n_errors, int *n_bits_total)
313{
314 sbit_t iB[456], cB[676], hl_hn[8];
315 ubit_t conv[456];
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100316 int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy *
Piotr Krysik70c25a12017-01-03 08:01:23 +0100317
318 for (i = 0; i < 4; i++)
319 gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116],
320 hl_hn + i * 2, hl_hn + i * 2 + 1);
321
322 for (i = 0; i < 4; i++) {
323 for (j = 0, k = 0; j < 8; j++)
324 k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j]));
325
326 if (i == 0 || k < best) {
327 best = k;
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100328 cs = i + 1;
Piotr Krysik70c25a12017-01-03 08:01:23 +0100329 }
330 }
331
332 gsm0503_xcch_deinterleave(cB, iB);
333
334 switch (cs) {
335 case 1:
336 osmo_conv_decode_ber(&gsm0503_xcch, cB,
337 conv, n_errors, n_bits_total);
338
339 rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40,
340 conv, 184, conv + 184);
341 if (rv)
342 return -1;
343
344 osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1);
345
346 return 23;
347 case 2:
348 for (i = 587, j = 455; i >= 0; i--) {
349 if (!gsm0503_puncture_cs2[i])
350 cB[i] = cB[j--];
351 else
352 cB[i] = 0;
353 }
354
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100355 osmo_conv_decode_ber(&gsm0503_cs2_np, cB,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100356 conv, n_errors, n_bits_total);
357
358 for (i = 0; i < 8; i++) {
359 for (j = 0, k = 0; j < 6; j++)
360 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
361
362 if (i == 0 || k < best) {
363 best = k;
364 usf = i;
365 }
366 }
367
368 conv[3] = usf & 1;
369 conv[4] = (usf >> 1) & 1;
370 conv[5] = (usf >> 2) & 1;
371 if (usf_p)
372 *usf_p = usf;
373
374 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
375 conv + 3, 271, conv + 3 + 271);
376 if (rv)
377 return -1;
378
379 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1);
380
381 return 34;
382 case 3:
383 for (i = 675, j = 455; i >= 0; i--) {
384 if (!gsm0503_puncture_cs3[i])
385 cB[i] = cB[j--];
386 else
387 cB[i] = 0;
388 }
389
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100390 osmo_conv_decode_ber(&gsm0503_cs3_np, cB,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100391 conv, n_errors, n_bits_total);
392
393 for (i = 0; i < 8; i++) {
394 for (j = 0, k = 0; j < 6; j++)
395 k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j]));
396
397 if (i == 0 || k < best) {
398 best = k;
399 usf = i;
400 }
401 }
402
403 conv[3] = usf & 1;
404 conv[4] = (usf >> 1) & 1;
405 conv[5] = (usf >> 2) & 1;
406 if (usf_p)
407 *usf_p = usf;
408
409 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
410 conv + 3, 315, conv + 3 + 315);
411 if (rv)
412 return -1;
413
414 osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1);
415
416 return 40;
417 case 4:
418 for (i = 12; i < 456; i++)
419 conv[i] = (cB[i] < 0) ? 1 : 0;
420
421 for (i = 0; i < 8; i++) {
422 for (j = 0, k = 0; j < 12; j++)
423 k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j]));
424
425 if (i == 0 || k < best) {
426 best = k;
427 usf = i;
428 }
429 }
430
431 conv[9] = usf & 1;
432 conv[10] = (usf >> 1) & 1;
433 conv[11] = (usf >> 2) & 1;
434 if (usf_p)
435 *usf_p = usf;
436
437 rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16,
438 conv + 9, 431, conv + 9 + 431);
439 if (rv) {
440 *n_bits_total = 456 - 12;
441 *n_errors = *n_bits_total;
442 return -1;
443 }
444
445 *n_bits_total = 456 - 12;
446 *n_errors = 0;
447
448 osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1);
449
450 return 54;
451 default:
452 *n_bits_total = 0;
453 *n_errors = 0;
454 break;
455 }
456
457 return -1;
458}
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100459*/
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100460
461/*! GPRS DL message encoding
462 * \param[out] bursts caller-allocated buffer for unpacked burst bits
463 * \param[in] l2_data L2 (MAC) block to be encoded
464 * \param[in] l2_len length of l2_data in bytes, used to determine CS
465 * \returns 0 on success; negative on error */
466int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100467{
468 ubit_t iB[456], cB[676];
469 const ubit_t *hl_hn;
470 ubit_t conv[334];
471 int i, j, usf;
472
473 switch (l2_len) {
474 case 23:
475 osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1);
476
477 osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184);
478
479 osmo_conv_encode(&gsm0503_xcch, conv, cB);
480
481 hl_hn = gsm0503_pdtch_hl_hn_ubit[0];
482
483 break;
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100484 /*case 34:
Piotr Krysik70c25a12017-01-03 08:01:23 +0100485 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1);
486 usf = l2_data[0] & 0x7;
487
488 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
489 271, conv + 3 + 271);
490
491 memcpy(conv, gsm0503_usf2six[usf], 6);
492
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100493 osmo_conv_encode(&gsm0503_cs2_np, conv, cB);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100494
495 for (i = 0, j = 0; i < 588; i++)
496 if (!gsm0503_puncture_cs2[i])
497 cB[j++] = cB[i];
498
499 hl_hn = gsm0503_pdtch_hl_hn_ubit[1];
500
501 break;
502 case 40:
503 osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1);
504 usf = l2_data[0] & 0x7;
505
506 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3,
507 315, conv + 3 + 315);
508
509 memcpy(conv, gsm0503_usf2six[usf], 6);
510
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100511 osmo_conv_encode(&gsm0503_cs3_np, conv, cB);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100512
513 for (i = 0, j = 0; i < 676; i++)
514 if (!gsm0503_puncture_cs3[i])
515 cB[j++] = cB[i];
516
517 hl_hn = gsm0503_pdtch_hl_hn_ubit[2];
518
Piotr Krysikb3bd68e2018-02-27 14:37:01 +0100519 break;*/
Piotr Krysik70c25a12017-01-03 08:01:23 +0100520 case 54:
521 osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1);
522 usf = l2_data[0] & 0x7;
523
524 osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9,
525 431, cB + 9 + 431);
526
527 memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12);
528
529 hl_hn = gsm0503_pdtch_hl_hn_ubit[3];
530
531 break;
532 default:
533 return -1;
534 }
535
536 gsm0503_xcch_interleave(cB, iB);
537
538 for (i = 0; i < 4; i++) {
539 gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116],
540 hl_hn + i * 2, hl_hn + i * 2 + 1);
541 }
542
543 return GSM0503_GPRS_BURSTS_NBITS;
544}
545
546/*
547 * GSM TCH/F FR/EFR transcoding
548 */
549
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100550/*! assemble a FR codec frame in format as used inside RTP
551 * \param[out] tch_data Codec frame in RTP format
552 * \param[in] b_bits Codec frame in 'native' format
553 * \param[in] net_order FIXME */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100554static void tch_fr_reassemble(uint8_t *tch_data,
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100555 const ubit_t *b_bits, int net_order)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100556{
557 int i, j, k, l, o;
558
559 tch_data[0] = 0xd << 4;
560 memset(tch_data + 1, 0, 32);
561
562 if (net_order) {
563 for (i = 0, j = 4; i < 260; i++, j++)
564 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
565
566 return;
567 }
568
569 /* reassemble d-bits */
570 i = 0; /* counts bits */
571 j = 4; /* counts output bits */
572 k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */
573 l = 0; /* counts element bits */
574 o = 0; /* offset input bits */
575 while (i < 260) {
576 tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7)));
577 if (--k < 0) {
578 o += gsm0503_gsm_fr_map[l];
579 k = gsm0503_gsm_fr_map[++l]-1;
580 }
581 i++;
582 j++;
583 }
584}
585
586static void tch_fr_disassemble(ubit_t *b_bits,
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100587 const uint8_t *tch_data, int net_order)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100588{
589 int i, j, k, l, o;
590
591 if (net_order) {
592 for (i = 0, j = 4; i < 260; i++, j++)
593 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
594
595 return;
596 }
597
598 i = 0; /* counts bits */
599 j = 4; /* counts input bits */
600 k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */
601 l = 0; /* counts element bits */
602 o = 0; /* offset output bits */
603 while (i < 260) {
604 b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
605 if (--k < 0) {
606 o += gsm0503_gsm_fr_map[l];
607 k = gsm0503_gsm_fr_map[++l] - 1;
608 }
609 i++;
610 j++;
611 }
612}
613
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100614/* assemble a HR codec frame in format as used inside RTP */
615static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100616{
617 int i, j;
618
619 tch_data[0] = 0x00; /* F = 0, FT = 000 */
620 memset(tch_data + 1, 0, 14);
621
622 for (i = 0, j = 8; i < 112; i++, j++)
623 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
624}
625
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100626static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100627{
628 int i, j;
629
630 for (i = 0, j = 8; i < 112; i++, j++)
631 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
632}
633
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100634/* assemble a EFR codec frame in format as used inside RTP */
635static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100636{
637 int i, j;
638
639 tch_data[0] = 0xc << 4;
640 memset(tch_data + 1, 0, 30);
641
642 for (i = 0, j = 4; i < 244; i++, j++)
643 tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7)));
644}
645
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100646static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100647{
648 int i, j;
649
650 for (i = 0, j = 4; i < 244; i++, j++)
651 b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
652}
653
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100654/* assemble a AMR codec frame in format as used inside RTP */
655static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100656{
657 int i, j;
658
659 memset(tch_data, 0, (len + 7) >> 3);
660
661 for (i = 0, j = 0; i < len; i++, j++)
662 tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7)));
663}
664
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100665static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100666{
667 int i, j;
668
669 for (i = 0, j = 0; i < len; i++, j++)
670 d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1;
671}
672
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100673/* re-arrange according to TS 05.03 Table 2 (receiver) */
674static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100675{
676 int i;
677
678 for (i = 0; i < 260; i++)
679 b_bits[gsm610_bitorder[i]] = d_bits[i];
680}
681
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100682/* re-arrange according to TS 05.03 Table 2 (transmitter) */
683static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100684{
685 int i;
686
687 for (i = 0; i < 260; i++)
688 d_bits[i] = b_bits[gsm610_bitorder[i]];
689}
690
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100691/* re-arrange according to TS 05.03 Table 3a (receiver) */
692static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100693{
694 int i;
695
696 const uint16_t *map;
697
698 if (!d_bits[93] && !d_bits[94])
699 map = gsm620_unvoiced_bitorder;
700 else
701 map = gsm620_voiced_bitorder;
702
703 for (i = 0; i < 112; i++)
704 b_bits[map[i]] = d_bits[i];
705}
706
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100707/* re-arrange according to TS 05.03 Table 3a (transmitter) */
708static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100709{
710 int i;
711 const uint16_t *map;
712
713 if (!b_bits[34] && !b_bits[35])
714 map = gsm620_unvoiced_bitorder;
715 else
716 map = gsm620_voiced_bitorder;
717
718 for (i = 0; i < 112; i++)
719 d_bits[i] = b_bits[map[i]];
720}
721
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100722/* re-arrange according to TS 05.03 Table 6 (receiver) */
723static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100724{
725 int i;
726
727 for (i = 0; i < 260; i++)
728 b_bits[gsm660_bitorder[i]] = d_bits[i];
729}
730
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100731/* re-arrange according to TS 05.03 Table 6 (transmitter) */
732static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100733{
734 int i;
735
736 for (i = 0; i < 260; i++)
737 d_bits[i] = b_bits[gsm660_bitorder[i]];
738}
739
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100740/* extract the 65 protected class1a+1b bits */
741static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100742{
743 int i;
744
745 for (i = 0; i < 65; i++)
746 b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1];
747}
748
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100749static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100750{
751 int i;
752
753 for (i = 0; i < 91; i++) {
754 d[i << 1] = u[i];
755 d[(i << 1) + 1] = u[184 - i];
756 }
757
758 for (i = 0; i < 3; i++)
759 p[i] = u[91 + i];
760}
761
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100762static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100763{
764 int i;
765
766 for (i = 0; i < 91; i++) {
767 u[i] = d[i << 1];
768 u[184 - i] = d[(i << 1) + 1];
769 }
770
771 for (i = 0; i < 3; i++)
772 u[91 + i] = p[i];
773}
774
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100775static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100776{
777 memcpy(d, u, 95);
778 memcpy(p, u + 95, 3);
779}
780
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100781static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100782{
783 memcpy(u, d, 95);
784 memcpy(u + 95, p, 3);
785}
786
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100787static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100788{
789 memcpy(w, s, 71);
790 w[71] = w[72] = s[69];
791 memcpy(w + 73, s + 71, 50);
792 w[123] = w[124] = s[119];
793 memcpy(w + 125, s + 121, 53);
794 w[178] = w[179] = s[172];
795 memcpy(w + 180, s + 174, 50);
796 w[230] = w[231] = s[222];
797 memcpy(w + 232, s + 224, 20);
798 memcpy(w + 252, p, 8);
799}
800
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100801static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100802{
803 int sum;
804
805 memcpy(s, w, 71);
806 sum = s[69] + w[71] + w[72];
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100807 s[69] = (sum >= 2);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100808 memcpy(s + 71, w + 73, 50);
809 sum = s[119] + w[123] + w[124];
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100810 s[119] = (sum >= 2);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100811 memcpy(s + 121, w + 125, 53);
812 sum = s[172] + w[178] + w[179];
813 s[172] = (sum > 2);
814 memcpy(s + 174, w + 180, 50);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100815 sum = s[222] + w[230] + w[231];
816 s[222] = (sum >= 2);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100817 memcpy(s + 224, w + 232, 20);
818 memcpy(p, w + 252, 8);
819}
820
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100821static void tch_amr_merge(ubit_t *u, const ubit_t *d, const ubit_t *p, int len, int prot)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100822{
823 memcpy(u, d, prot);
824 memcpy(u + prot, p, 6);
825 memcpy(u + prot + 6, d + prot, len - prot);
826}
827
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100828static void tch_amr_unmerge(ubit_t *d, ubit_t *p, const ubit_t *u, int len, int prot)
Piotr Krysik70c25a12017-01-03 08:01:23 +0100829{
830 memcpy(d, u, prot);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100831 memcpy(p, u + prot, 6);
Piotr Krysik70c25a12017-01-03 08:01:23 +0100832 memcpy(d + prot, u + prot + 6, len - prot);
833}
834
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100835/*! Perform channel decoding of a FR/EFR channel according TS 05.03
836 * \param[out] tch_data Codec frame in RTP payload format
837 * \param[in] bursts buffer containing the symbols of 8 bursts
838 * \param[in] net_order FIXME
839 * \param[in] efr Is this channel using EFR (1) or FR (0)
840 * \param[out] n_errors Number of detected bit errors
841 * \param[out] n_bits_total Total number of bits
842 * \returns length of bytes used in \a tch_data output buffer */
843int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100844 int net_order, int efr, int *n_errors, int *n_bits_total)
845{
846 sbit_t iB[912], cB[456], h;
847 ubit_t conv[185], s[244], w[260], b[65], d[260], p[8];
848 int i, rv, len, steal = 0;
849
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100850 /* map from 8 bursts to interleaved data bits (iB) */
851 for (i = 0; i < 8; i++) {
Piotr Krysik70c25a12017-01-03 08:01:23 +0100852 gsm0503_tch_burst_unmap(&iB[i * 114],
853 &bursts[i * 116], &h, i >> 2);
854 steal -= h;
855 }
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100856 /* we now have the bits of the four bursts (interface 4 in
857 * Figure 1a of TS 05.03 */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100858
859 gsm0503_tch_fr_deinterleave(cB, iB);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100860 /* we now have the coded bits c(B): interface 3 in Fig. 1a */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100861
862 if (steal > 0) {
863 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
864 if (rv) {
865 /* Error decoding FACCH frame */
866 return -1;
867 }
868
869 return 23;
870 }
871
872 osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100873 /* we now have the data bits 'u': interface 2 in Fig. 1a */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100874
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100875 /* input: 'conv', output: d[ata] + p[arity] */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100876 tch_fr_unreorder(d, p, conv);
877
878 for (i = 0; i < 78; i++)
879 d[i + 182] = (cB[i + 378] < 0) ? 1 : 0;
880
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100881 /* check if parity of first 50 (class 1) 'd'-bits match 'p' */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100882 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p);
883 if (rv) {
884 /* Error checking CRC8 for the FR part of an EFR/FR frame */
885 return -1;
886 }
887
888 if (efr) {
889 tch_efr_d_to_w(w, d);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100890 /* we now have the preliminary-coded bits w(k) */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100891
892 tch_efr_unreorder(s, p, w);
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100893 /* we now have the data delivered to the preliminary
894 * channel encoding unit s(k) */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100895
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100896 /* extract the 65 most important bits according TS 05.03 3.1.1.1 */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100897 tch_efr_protected(s, b);
898
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100899 /* perform CRC-8 on 65 most important bits (50 bits of
900 * class 1a + 15 bits of class 1b) */
Piotr Krysik70c25a12017-01-03 08:01:23 +0100901 rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p);
902 if (rv) {
903 /* Error checking CRC8 for the EFR part of an EFR frame */
904 return -1;
905 }
906
907 tch_efr_reassemble(tch_data, s);
908
909 len = GSM_EFR_BYTES;
910 } else {
911 tch_fr_d_to_b(w, d);
912
913 tch_fr_reassemble(tch_data, w, net_order);
914
915 len = GSM_FR_BYTES;
916 }
917
918 return len;
919}
920
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100921/*! Perform channel encoding on a TCH/FS channel according to TS 05.03
922 * \param[out] bursts caller-allocated output buffer for bursts bits
923 * \param[in] tch_data Codec input data in RTP payload format
924 * \param[in] len Length of \a tch_data in bytes
925 * \param[in] net_order FIXME
926 * \returns 0 in case of success; negative on error */
927int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100928 int len, int net_order)
929{
930 ubit_t iB[912], cB[456], h;
931 ubit_t conv[185], w[260], b[65], s[244], d[260], p[8];
932 int i;
933
934 switch (len) {
935 case GSM_EFR_BYTES: /* TCH EFR */
936
937 tch_efr_disassemble(s, tch_data);
938
939 tch_efr_protected(s, b);
940
941 osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p);
942
943 tch_efr_reorder(w, s, p);
944
945 tch_efr_w_to_d(d, w);
946
947 goto coding_efr_fr;
948 case GSM_FR_BYTES: /* TCH FR */
949 tch_fr_disassemble(w, tch_data, net_order);
950
951 tch_fr_b_to_d(d, w);
952
953coding_efr_fr:
954 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p);
955
956 tch_fr_reorder(conv, d, p);
957
958 memcpy(cB + 378, d + 182, 78);
959
960 osmo_conv_encode(&gsm0503_tch_fr, conv, cB);
961
962 h = 0;
963
964 break;
965 case GSM_MACBLOCK_LEN: /* FACCH */
966 _xcch_encode_cB(cB, tch_data);
967
968 h = 1;
969
970 break;
971 default:
972 return -1;
973 }
974
975 gsm0503_tch_fr_interleave(cB, iB);
976
977 for (i = 0; i < 8; i++) {
978 gsm0503_tch_burst_map(&iB[i * 114],
979 &bursts[i * 116], &h, i >> 2);
980 }
981
982 return 0;
983}
984
Piotr Krysik9e2e8352018-02-27 12:16:25 +0100985/*! Perform channel decoding of a HR(v1) channel according TS 05.03
986 * \param[out] tch_data Codec frame in RTP payload format
987 * \param[in] bursts buffer containing the symbols of 8 bursts
988 * \param[in] odd Odd (1) or even (0) frame number
989 * \param[out] n_errors Number of detected bit errors
990 * \param[out] n_bits_total Total number of bits
991 * \returns length of bytes used in \a tch_data output buffer */
992int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Piotr Krysik70c25a12017-01-03 08:01:23 +0100993 int *n_errors, int *n_bits_total)
994{
995 sbit_t iB[912], cB[456], h;
996 ubit_t conv[98], b[112], d[112], p[3];
997 int i, rv, steal = 0;
998
999 /* Only unmap the stealing bits */
1000 if (!odd) {
1001 for (i = 0; i < 4; i++) {
1002 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
1003 steal -= h;
1004 }
1005
1006 for (i = 2; i < 5; i++) {
1007 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
1008 steal -= h;
1009 }
1010 }
1011
1012 /* If we found a stole FACCH, but only at correct alignment */
1013 if (steal > 0) {
1014 for (i = 0; i < 6; i++) {
1015 gsm0503_tch_burst_unmap(&iB[i * 114],
1016 &bursts[i * 116], NULL, i >> 2);
1017 }
1018
1019 for (i = 2; i < 4; i++) {
1020 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
1021 &bursts[i * 116], NULL, 1);
1022 }
1023
1024 gsm0503_tch_fr_deinterleave(cB, iB);
1025
1026 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1027 if (rv) {
1028 /* Error decoding FACCH frame */
1029 return -1;
1030 }
1031
1032 return GSM_MACBLOCK_LEN;
1033 }
1034
1035 for (i = 0; i < 4; i++) {
1036 gsm0503_tch_burst_unmap(&iB[i * 114],
1037 &bursts[i * 116], NULL, i >> 1);
1038 }
1039
1040 gsm0503_tch_hr_deinterleave(cB, iB);
1041
1042 osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total);
1043
1044 tch_hr_unreorder(d, p, conv);
1045
1046 for (i = 0; i < 17; i++)
1047 d[i + 95] = (cB[i + 211] < 0) ? 1 : 0;
1048
1049 rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
1050 if (rv) {
1051 /* Error checking CRC8 for an HR frame */
1052 return -1;
1053 }
1054
1055 tch_hr_d_to_b(b, d);
1056
1057 tch_hr_reassemble(tch_data, b);
1058
1059 return 15;
1060}
1061
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001062/*! Perform channel encoding on a TCH/HS channel according to TS 05.03
1063 * \param[out] bursts caller-allocated output buffer for bursts bits
1064 * \param[in] tch_data Codec input data in RTP payload format
1065 * \param[in] len Length of \a tch_data in bytes
1066 * \returns 0 in case of success; negative on error */
1067int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len)
Piotr Krysik70c25a12017-01-03 08:01:23 +01001068{
1069 ubit_t iB[912], cB[456], h;
1070 ubit_t conv[98], b[112], d[112], p[3];
1071 int i;
1072
1073 switch (len) {
1074 case 15: /* TCH HR */
1075 tch_hr_disassemble(b, tch_data);
1076
1077 tch_hr_b_to_d(d, b);
1078
1079 osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p);
1080
1081 tch_hr_reorder(conv, d, p);
1082
1083 osmo_conv_encode(&gsm0503_tch_hr, conv, cB);
1084
1085 memcpy(cB + 211, d + 95, 17);
1086
1087 h = 0;
1088
1089 gsm0503_tch_hr_interleave(cB, iB);
1090
1091 for (i = 0; i < 4; i++) {
1092 gsm0503_tch_burst_map(&iB[i * 114],
1093 &bursts[i * 116], &h, i >> 1);
1094 }
1095
1096 break;
1097 case GSM_MACBLOCK_LEN: /* FACCH */
1098 _xcch_encode_cB(cB, tch_data);
1099
1100 h = 1;
1101
1102 gsm0503_tch_fr_interleave(cB, iB);
1103
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001104 for (i = 0; i < 6; i++) {
Piotr Krysik70c25a12017-01-03 08:01:23 +01001105 gsm0503_tch_burst_map(&iB[i * 114],
1106 &bursts[i * 116], &h, i >> 2);
1107 }
1108
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001109 for (i = 2; i < 4; i++) {
Piotr Krysik70c25a12017-01-03 08:01:23 +01001110 gsm0503_tch_burst_map(&iB[i * 114 + 456],
1111 &bursts[i * 116], &h, 1);
1112 }
1113
1114 break;
1115 default:
1116 return -1;
1117 }
1118
1119 return 0;
1120}
1121
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001122/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
1123 * \param[out] tch_data Codec frame in RTP payload format
1124 * \param[in] bursts buffer containing the symbols of 8 bursts
1125 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
1126 * \param[in] codec array of active codecs (active codec set)
1127 * \param[in] codecs number of codecs in \a codec
1128 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
1129 * \param[out] cmr Output in \a codec_mode_req = 1
1130 * \param[out] n_errors Number of detected bit errors
1131 * \param[out] n_bits_total Total number of bits
1132 * \returns length of bytes used in \a tch_data output buffer */
1133int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts,
Piotr Krysik70c25a12017-01-03 08:01:23 +01001134 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
1135 uint8_t *cmr, int *n_errors, int *n_bits_total)
1136{
1137 sbit_t iB[912], cB[456], h;
1138 ubit_t d[244], p[6], conv[250];
1139 int i, j, k, best = 0, rv, len, steal = 0, id = 0;
1140 *n_errors = 0; *n_bits_total = 0;
1141
1142 for (i=0; i<8; i++) {
1143 gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2);
1144 steal -= h;
1145 }
1146
1147 gsm0503_tch_fr_deinterleave(cB, iB);
1148
1149 if (steal > 0) {
1150 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1151 if (rv) {
1152 /* Error decoding FACCH frame */
1153 return -1;
1154 }
1155
1156 return GSM_MACBLOCK_LEN;
1157 }
1158
1159 for (i = 0; i < 4; i++) {
1160 for (j = 0, k = 0; j < 8; j++)
1161 k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j]));
1162
1163 if (i == 0 || k < best) {
1164 best = k;
1165 id = i;
1166 }
1167 }
1168
1169 /* Check if indicated codec fits into range of codecs */
1170 if (id >= codecs) {
1171 /* Codec mode out of range, return id */
1172 return id;
1173 }
1174
1175 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
1176 case 7: /* TCH/AFS12.2 */
1177 osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8,
1178 conv, n_errors, n_bits_total);
1179
1180 tch_amr_unmerge(d, p, conv, 244, 81);
1181
1182 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p);
1183 if (rv) {
1184 /* Error checking CRC8 for an AMR 12.2 frame */
1185 return -1;
1186 }
1187
1188 tch_amr_reassemble(tch_data, d, 244);
1189
1190 len = 31;
1191
1192 break;
1193 case 6: /* TCH/AFS10.2 */
1194 osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8,
1195 conv, n_errors, n_bits_total);
1196
1197 tch_amr_unmerge(d, p, conv, 204, 65);
1198
1199 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p);
1200 if (rv) {
1201 /* Error checking CRC8 for an AMR 10.2 frame */
1202 return -1;
1203 }
1204
1205 tch_amr_reassemble(tch_data, d, 204);
1206
1207 len = 26;
1208
1209 break;
1210 case 5: /* TCH/AFS7.95 */
1211 osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8,
1212 conv, n_errors, n_bits_total);
1213
1214 tch_amr_unmerge(d, p, conv, 159, 75);
1215
1216 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p);
1217 if (rv) {
1218 /* Error checking CRC8 for an AMR 7.95 frame */
1219 return -1;
1220 }
1221
1222 tch_amr_reassemble(tch_data, d, 159);
1223
1224 len = 20;
1225
1226 break;
1227 case 4: /* TCH/AFS7.4 */
1228 osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8,
1229 conv, n_errors, n_bits_total);
1230
1231 tch_amr_unmerge(d, p, conv, 148, 61);
1232
1233 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
1234 if (rv) {
1235 /* Error checking CRC8 for an AMR 7.4 frame */
1236 return -1;
1237 }
1238
1239 tch_amr_reassemble(tch_data, d, 148);
1240
1241 len = 19;
1242
1243 break;
1244 case 3: /* TCH/AFS6.7 */
1245 osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8,
1246 conv, n_errors, n_bits_total);
1247
1248 tch_amr_unmerge(d, p, conv, 134, 55);
1249
1250 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
1251 if (rv) {
1252 /* Error checking CRC8 for an AMR 6.7 frame */
1253 return -1;
1254 }
1255
1256 tch_amr_reassemble(tch_data, d, 134);
1257
1258 len = 17;
1259
1260 break;
1261 case 2: /* TCH/AFS5.9 */
1262 osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8,
1263 conv, n_errors, n_bits_total);
1264
1265 tch_amr_unmerge(d, p, conv, 118, 55);
1266
1267 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
1268 if (rv) {
1269 /* Error checking CRC8 for an AMR 5.9 frame */
1270 return -1;
1271 }
1272
1273 tch_amr_reassemble(tch_data, d, 118);
1274
1275 len = 15;
1276
1277 break;
1278 case 1: /* TCH/AFS5.15 */
1279 osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8,
1280 conv, n_errors, n_bits_total);
1281
1282 tch_amr_unmerge(d, p, conv, 103, 49);
1283
1284 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
1285 if (rv) {
1286 /* Error checking CRC8 for an AMR 5.15 frame */
1287 return -1;
1288 }
1289
1290 tch_amr_reassemble(tch_data, d, 103);
1291
1292 len = 13;
1293
1294 break;
1295 case 0: /* TCH/AFS4.75 */
1296 osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8,
1297 conv, n_errors, n_bits_total);
1298
1299 tch_amr_unmerge(d, p, conv, 95, 39);
1300
1301 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
1302 if (rv) {
1303 /* Error checking CRC8 for an AMR 4.75 frame */
1304 return -1;
1305 }
1306
1307 tch_amr_reassemble(tch_data, d, 95);
1308
1309 len = 12;
1310
1311 break;
1312 default:
1313 /* Unknown frame type */
1314 *n_bits_total = 448;
1315 *n_errors = *n_bits_total;
1316 return -1;
1317 }
1318
1319 /* Change codec request / indication, if frame is valid */
1320 if (codec_mode_req)
1321 *cmr = id;
1322 else
1323 *ft = id;
1324
1325 return len;
1326}
1327
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001328/*! Perform channel encoding on a TCH/AFS channel according to TS 05.03
1329 * \param[out] bursts caller-allocated output buffer for bursts bits
1330 * \param[in] tch_data Codec input data in RTP payload format
1331 * \param[in] len Length of \a tch_data in bytes
1332 * \param[in] codec_mode_req Use CMR (1) or FT (0)
1333 * \param[in] codec Array of codecs (active codec set)
1334 * \param[in] codecs Number of entries in \a codec
1335 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
1336 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
1337 * \returns 0 in case of success; negative on error */
1338int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Piotr Krysik70c25a12017-01-03 08:01:23 +01001339 int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft,
1340 uint8_t cmr)
1341{
1342 ubit_t iB[912], cB[456], h;
1343 ubit_t d[244], p[6], conv[250];
1344 int i;
1345 uint8_t id;
1346
1347 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
1348 _xcch_encode_cB(cB, tch_data);
1349
1350 h = 1;
1351
1352 goto facch;
1353 }
1354
1355 h = 0;
1356
1357 if (codec_mode_req) {
1358 if (cmr >= codecs) {
1359 /* FIXME: CMR ID is not in codec list! */
1360 return -1;
1361 }
1362 id = cmr;
1363 } else {
1364 if (ft >= codecs) {
1365 /* FIXME: FT ID is not in codec list! */
1366 return -1;
1367 }
1368 id = ft;
1369 }
1370
1371 switch (codec[ft]) {
1372 case 7: /* TCH/AFS12.2 */
1373 if (len != 31)
1374 goto invalid_length;
1375
1376 tch_amr_disassemble(d, tch_data, 244);
1377
1378 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p);
1379
1380 tch_amr_merge(conv, d, p, 244, 81);
1381
1382 osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8);
1383
1384 break;
1385 case 6: /* TCH/AFS10.2 */
1386 if (len != 26)
1387 goto invalid_length;
1388
1389 tch_amr_disassemble(d, tch_data, 204);
1390
1391 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p);
1392
1393 tch_amr_merge(conv, d, p, 204, 65);
1394
1395 osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8);
1396
1397 break;
1398 case 5: /* TCH/AFS7.95 */
1399 if (len != 20)
1400 goto invalid_length;
1401
1402 tch_amr_disassemble(d, tch_data, 159);
1403
1404 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p);
1405
1406 tch_amr_merge(conv, d, p, 159, 75);
1407
1408 osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8);
1409
1410 break;
1411 case 4: /* TCH/AFS7.4 */
1412 if (len != 19)
1413 goto invalid_length;
1414
1415 tch_amr_disassemble(d, tch_data, 148);
1416
1417 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
1418
1419 tch_amr_merge(conv, d, p, 148, 61);
1420
1421 osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8);
1422
1423 break;
1424 case 3: /* TCH/AFS6.7 */
1425 if (len != 17)
1426 goto invalid_length;
1427
1428 tch_amr_disassemble(d, tch_data, 134);
1429
1430 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
1431
1432 tch_amr_merge(conv, d, p, 134, 55);
1433
1434 osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8);
1435
1436 break;
1437 case 2: /* TCH/AFS5.9 */
1438 if (len != 15)
1439 goto invalid_length;
1440
1441 tch_amr_disassemble(d, tch_data, 118);
1442
1443 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
1444
1445 tch_amr_merge(conv, d, p, 118, 55);
1446
1447 osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8);
1448
1449 break;
1450 case 1: /* TCH/AFS5.15 */
1451 if (len != 13)
1452 goto invalid_length;
1453
1454 tch_amr_disassemble(d, tch_data, 103);
1455
1456 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
1457
1458 tch_amr_merge(conv, d, p, 103, 49);
1459
1460 osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8);
1461
1462 break;
1463 case 0: /* TCH/AFS4.75 */
1464 if (len != 12)
1465 goto invalid_length;
1466
1467 tch_amr_disassemble(d, tch_data, 95);
1468
1469 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
1470
1471 tch_amr_merge(conv, d, p, 95, 39);
1472
1473 osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8);
1474
1475 break;
1476 default:
1477 /* FIXME: FT %ft is not supported */
1478 return -1;
1479 }
1480
1481 memcpy(cB, gsm0503_afs_ic_ubit[id], 8);
1482
1483facch:
1484 gsm0503_tch_fr_interleave(cB, iB);
1485
1486 for (i = 0; i < 8; i++) {
1487 gsm0503_tch_burst_map(&iB[i * 114],
1488 &bursts[i * 116], &h, i >> 2);
1489 }
1490
1491 return 0;
1492
1493invalid_length:
1494 /* FIXME: payload length %len does not comply with codec type %ft */
1495 return -1;
1496}
1497
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001498/*! Perform channel decoding of a TCH/AFS channel according TS 05.03
1499 * \param[out] tch_data Codec frame in RTP payload format
1500 * \param[in] bursts buffer containing the symbols of 8 bursts
1501 * \param[in] odd Is this an odd (1) or even (0) frame number?
1502 * \param[in] codec_mode_req is this CMR (1) or CMC (0)
1503 * \param[in] codec array of active codecs (active codec set)
1504 * \param[in] codecs number of codecs in \a codec
1505 * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise
1506 * \param[out] cmr Output in \a codec_mode_req = 1
1507 * \param[out] n_errors Number of detected bit errors
1508 * \param[out] n_bits_total Total number of bits
1509 * \returns length of bytes used in \a tch_data output buffer */
1510int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd,
Piotr Krysik70c25a12017-01-03 08:01:23 +01001511 int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft,
1512 uint8_t *cmr, int *n_errors, int *n_bits_total)
1513{
1514 sbit_t iB[912], cB[456], h;
1515 ubit_t d[244], p[6], conv[135];
1516 int i, j, k, best = 0, rv, len, steal = 0, id = 0;
1517
1518 /* only unmap the stealing bits */
1519 if (!odd) {
1520 for (i = 0; i < 4; i++) {
1521 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0);
1522 steal -= h;
1523 }
1524 for (i = 2; i < 5; i++) {
1525 gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1);
1526 steal -= h;
1527 }
1528 }
1529
1530 /* if we found a stole FACCH, but only at correct alignment */
1531 if (steal > 0) {
1532 for (i = 0; i < 6; i++) {
1533 gsm0503_tch_burst_unmap(&iB[i * 114],
1534 &bursts[i * 116], NULL, i >> 2);
1535 }
1536
1537 for (i = 2; i < 4; i++) {
1538 gsm0503_tch_burst_unmap(&iB[i * 114 + 456],
1539 &bursts[i * 116], NULL, 1);
1540 }
1541
1542 gsm0503_tch_fr_deinterleave(cB, iB);
1543
1544 rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total);
1545 if (rv) {
1546 /* Error decoding FACCH frame */
1547 return -1;
1548 }
1549
1550 return GSM_MACBLOCK_LEN;
1551 }
1552
1553 for (i = 0; i < 4; i++) {
1554 gsm0503_tch_burst_unmap(&iB[i * 114],
1555 &bursts[i * 116], NULL, i >> 1);
1556 }
1557
1558 gsm0503_tch_hr_deinterleave(cB, iB);
1559
1560 for (i = 0; i < 4; i++) {
1561 for (j = 0, k = 0; j < 4; j++)
1562 k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j]));
1563
1564 if (i == 0 || k < best) {
1565 best = k;
1566 id = i;
1567 }
1568 }
1569
1570 /* Check if indicated codec fits into range of codecs */
1571 if (id >= codecs) {
1572 /* Codec mode out of range, return id */
1573 return id;
1574 }
1575
1576 switch ((codec_mode_req) ? codec[*ft] : codec[id]) {
1577 case 5: /* TCH/AHS7.95 */
1578 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4,
1579 conv, n_errors, n_bits_total);
1580
1581 tch_amr_unmerge(d, p, conv, 123, 67);
1582
1583 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p);
1584 if (rv) {
1585 /* Error checking CRC8 for an AMR 7.95 frame */
1586 return -1;
1587 }
1588
1589 for (i = 0; i < 36; i++)
1590 d[i + 123] = (cB[i + 192] < 0) ? 1 : 0;
1591
1592 tch_amr_reassemble(tch_data, d, 159);
1593
1594 len = 20;
1595
1596 break;
1597 case 4: /* TCH/AHS7.4 */
1598 osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4,
1599 conv, n_errors, n_bits_total);
1600
1601 tch_amr_unmerge(d, p, conv, 120, 61);
1602
1603 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p);
1604 if (rv) {
1605 /* Error checking CRC8 for an AMR 7.4 frame */
1606 return -1;
1607 }
1608
1609 for (i = 0; i < 28; i++)
1610 d[i + 120] = (cB[i + 200] < 0) ? 1 : 0;
1611
1612 tch_amr_reassemble(tch_data, d, 148);
1613
1614 len = 19;
1615
1616 break;
1617 case 3: /* TCH/AHS6.7 */
1618 osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4,
1619 conv, n_errors, n_bits_total);
1620
1621 tch_amr_unmerge(d, p, conv, 110, 55);
1622
1623 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
1624 if (rv) {
1625 /* Error checking CRC8 for an AMR 6.7 frame */
1626 return -1;
1627 }
1628
1629 for (i = 0; i < 24; i++)
1630 d[i + 110] = (cB[i + 204] < 0) ? 1 : 0;
1631
1632 tch_amr_reassemble(tch_data, d, 134);
1633
1634 len = 17;
1635
1636 break;
1637 case 2: /* TCH/AHS5.9 */
1638 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4,
1639 conv, n_errors, n_bits_total);
1640
1641 tch_amr_unmerge(d, p, conv, 102, 55);
1642
1643 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p);
1644 if (rv) {
1645 /* Error checking CRC8 for an AMR 5.9 frame */
1646 return -1;
1647 }
1648
1649 for (i = 0; i < 16; i++)
1650 d[i + 102] = (cB[i + 212] < 0) ? 1 : 0;
1651
1652 tch_amr_reassemble(tch_data, d, 118);
1653
1654 len = 15;
1655
1656 break;
1657 case 1: /* TCH/AHS5.15 */
1658 osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4,
1659 conv, n_errors, n_bits_total);
1660
1661 tch_amr_unmerge(d, p, conv, 91, 49);
1662
1663 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p);
1664 if (rv) {
1665 /* Error checking CRC8 for an AMR 5.15 frame */
1666 return -1;
1667 }
1668
1669 for (i = 0; i < 12; i++)
1670 d[i + 91] = (cB[i + 216] < 0) ? 1 : 0;
1671
1672 tch_amr_reassemble(tch_data, d, 103);
1673
1674 len = 13;
1675
1676 break;
1677 case 0: /* TCH/AHS4.75 */
1678 osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4,
1679 conv, n_errors, n_bits_total);
1680
1681 tch_amr_unmerge(d, p, conv, 83, 39);
1682
1683 rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p);
1684 if (rv) {
1685 /* Error checking CRC8 for an AMR 4.75 frame */
1686 return -1;
1687 }
1688
1689 for (i = 0; i < 12; i++)
1690 d[i + 83] = (cB[i + 216] < 0) ? 1 : 0;
1691
1692 tch_amr_reassemble(tch_data, d, 95);
1693
1694 len = 12;
1695
1696 break;
1697 default:
1698 /* Unknown frame type */
1699 *n_bits_total = 159;
1700 *n_errors = *n_bits_total;
1701 return -1;
1702 }
1703
1704 /* Change codec request / indication, if frame is valid */
1705 if (codec_mode_req)
1706 *cmr = id;
1707 else
1708 *ft = id;
1709
1710 return len;
1711}
1712
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001713/*! Perform channel encoding on a TCH/AHS channel according to TS 05.03
1714 * \param[out] bursts caller-allocated output buffer for bursts bits
1715 * \param[in] tch_data Codec input data in RTP payload format
1716 * \param[in] len Length of \a tch_data in bytes
1717 * \param[in] codec_mode_req Use CMR (1) or FT (0)
1718 * \param[in] codec Array of codecs (active codec set)
1719 * \param[in] codecs Number of entries in \a codec
1720 * \param[in] ft Frame Type to be used for encoding (index to \a codec)
1721 * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only)
1722 * \returns 0 in case of success; negative on error */
1723int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len,
Piotr Krysik70c25a12017-01-03 08:01:23 +01001724 int codec_mode_req, uint8_t *codec, int codecs, uint8_t ft,
1725 uint8_t cmr)
1726{
1727 ubit_t iB[912], cB[456], h;
1728 ubit_t d[244], p[6], conv[135];
1729 int i;
1730 uint8_t id;
1731
1732 if (len == GSM_MACBLOCK_LEN) { /* FACCH */
1733 _xcch_encode_cB(cB, tch_data);
1734
1735 h = 1;
1736
1737 gsm0503_tch_fr_interleave(cB, iB);
1738
1739 for (i = 0; i < 6; i++)
1740 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116],
1741 &h, i >> 2);
1742 for (i = 2; i < 4; i++)
1743 gsm0503_tch_burst_map(&iB[i * 114 + 456],
1744 &bursts[i * 116], &h, 1);
1745
1746 return 0;
1747 }
1748
1749 h = 0;
1750
1751 if (codec_mode_req) {
1752 if (cmr >= codecs) {
1753 /* FIXME: CMR ID %d not in codec list */
1754 return -1;
1755 }
1756 id = cmr;
1757 } else {
1758 if (ft >= codecs) {
1759 /* FIXME: FT ID %d not in codec list */
1760 return -1;
1761 }
1762 id = ft;
1763 }
1764
1765 switch (codec[ft]) {
1766 case 5: /* TCH/AHS7.95 */
1767 if (len != 20)
1768 goto invalid_length;
1769
1770 tch_amr_disassemble(d, tch_data, 159);
1771
1772 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p);
1773
1774 tch_amr_merge(conv, d, p, 123, 67);
1775
1776 osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4);
1777
1778 memcpy(cB + 192, d + 123, 36);
1779
1780 break;
1781 case 4: /* TCH/AHS7.4 */
1782 if (len != 19)
1783 goto invalid_length;
1784
1785 tch_amr_disassemble(d, tch_data, 148);
1786
1787 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p);
1788
1789 tch_amr_merge(conv, d, p, 120, 61);
1790
1791 osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4);
1792
1793 memcpy(cB + 200, d + 120, 28);
1794
1795 break;
1796 case 3: /* TCH/AHS6.7 */
1797 if (len != 17)
1798 goto invalid_length;
1799
1800 tch_amr_disassemble(d, tch_data, 134);
1801
1802 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
1803
1804 tch_amr_merge(conv, d, p, 110, 55);
1805
1806 osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4);
1807
1808 memcpy(cB + 204, d + 110, 24);
1809
1810 break;
1811 case 2: /* TCH/AHS5.9 */
1812 if (len != 15)
1813 goto invalid_length;
1814
1815 tch_amr_disassemble(d, tch_data, 118);
1816
1817 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p);
1818
1819 tch_amr_merge(conv, d, p, 102, 55);
1820
1821 osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4);
1822
1823 memcpy(cB + 212, d + 102, 16);
1824
1825 break;
1826 case 1: /* TCH/AHS5.15 */
1827 if (len != 13)
1828 goto invalid_length;
1829
1830 tch_amr_disassemble(d, tch_data, 103);
1831
1832 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p);
1833
1834 tch_amr_merge(conv, d, p, 91, 49);
1835
1836 osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4);
1837
1838 memcpy(cB + 216, d + 91, 12);
1839
1840 break;
1841 case 0: /* TCH/AHS4.75 */
1842 if (len != 12)
1843 goto invalid_length;
1844
1845 tch_amr_disassemble(d, tch_data, 95);
1846
1847 osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p);
1848
1849 tch_amr_merge(conv, d, p, 83, 39);
1850
1851 osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4);
1852
1853 memcpy(cB + 216, d + 83, 12);
1854
1855 break;
1856 default:
1857 /* FIXME: FT %ft is not supported */
1858 return -1;
1859 }
1860
1861 memcpy(cB, gsm0503_afs_ic_ubit[id], 4);
1862
1863 gsm0503_tch_hr_interleave(cB, iB);
1864
1865 for (i = 0; i < 4; i++)
1866 gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1);
1867
1868 return 0;
1869
1870invalid_length:
1871 /* FIXME: payload length %len does not comply with codec type %ft */
1872 return -1;
1873}
1874
1875/*
1876 * GSM RACH transcoding
1877 */
1878
1879/*
1880 * GSM RACH apply BSIC to parity
1881 *
1882 * p(j) = p(j) xor b(j) j = 0, ..., 5
1883 * b(0) = MSB of PLMN colour code
1884 * b(5) = LSB of BS colour code
1885 */
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001886static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start)
Piotr Krysik70c25a12017-01-03 08:01:23 +01001887{
1888 int i;
1889
1890 /* Apply it */
1891 for (i = 0; i < 6; i++)
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001892 d[start + i] ^= ((bsic >> (5 - i)) & 1);
Piotr Krysik70c25a12017-01-03 08:01:23 +01001893}
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001894/*
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001895static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit,
1896 int *n_errors, int *n_bits_total)
Piotr Krysik70c25a12017-01-03 08:01:23 +01001897{
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001898 ubit_t conv[17];
1899 uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8;
Piotr Krysik70c25a12017-01-03 08:01:23 +01001900 int rv;
1901
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001902 osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv,
1903 n_errors, n_bits_total);
Piotr Krysik70c25a12017-01-03 08:01:23 +01001904
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001905 rach_apply_bsic(conv, bsic, nbits);
Piotr Krysik70c25a12017-01-03 08:01:23 +01001906
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001907 rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
Piotr Krysik70c25a12017-01-03 08:01:23 +01001908 if (rv)
1909 return -1;
1910
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001911 osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1);
1912
1913 return is_11bit ? osmo_load16le(ra) : ra[0];
1914}
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001915*/
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001916
1917/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
1918 * \param[out] ra output buffer for RACH data
1919 * \param[in] burst Input burst data
1920 * \param[in] bsic BSIC used in this cell
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001921 * \returns 0 on success; negative on error (e.g. CRC error) *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001922int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic)
1923{
1924 int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL);
1925
1926 if (r < 0)
1927 return r;
1928
1929 *ra = r;
Piotr Krysik70c25a12017-01-03 08:01:23 +01001930
1931 return 0;
1932}
1933
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001934/*! Decode the (8-bit) RACH according to TS 05.03
1935 * \param[out] ra output buffer for RACH data
1936 * \param[in] burst Input burst data
1937 * \param[in] bsic BSIC used in this cell
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001938 * \returns 0 on success; negative on error (e.g. CRC error) *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001939int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic)
Piotr Krysik70c25a12017-01-03 08:01:23 +01001940{
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001941 int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL);
1942 if (r < 0)
1943 return r;
Piotr Krysik70c25a12017-01-03 08:01:23 +01001944
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001945 *ra = r;
1946 return 0;
1947}
Piotr Krysik70c25a12017-01-03 08:01:23 +01001948
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001949/*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003
1950 * \param[out] ra output buffer for RACH data
1951 * \param[in] burst Input burst data
1952 * \param[in] bsic BSIC used in this cell
1953 * \param[out] n_errors Number of detected bit errors
1954 * \param[out] n_bits_total Total number of bits
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001955 * \returns 0 on success; negative on error (e.g. CRC error) *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001956int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic,
1957 int *n_errors, int *n_bits_total)
1958{
1959 int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total);
1960 if (r < 0)
1961 return r;
Piotr Krysik70c25a12017-01-03 08:01:23 +01001962
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001963 *ra = r;
1964 return 0;
1965}
Piotr Krysik70c25a12017-01-03 08:01:23 +01001966
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001967/*! Decode the (8-bit) RACH according to TS 05.03
1968 * \param[out] ra output buffer for RACH data
1969 * \param[in] burst Input burst data
1970 * \param[in] bsic BSIC used in this cell
1971 * \param[out] n_errors Number of detected bit errors
1972 * \param[out] n_bits_total Total number of bits
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001973 * \returns 0 on success; negative on error (e.g. CRC error) *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001974int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic,
1975 int *n_errors, int *n_bits_total)
1976{
1977 int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total);
1978
1979 if (r < 0)
1980 return r;
1981
1982 *ra = r;
1983
1984 return 0;
1985}
1986
1987/*! Encode the (8-bit) RACH according to TS 05.03
1988 * \param[out] burst Caller-allocated output burst buffer
1989 * \param[in] ra Input RACH data
1990 * \param[in] bsic BSIC used in this cell
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01001991 * \returns 0 on success; negative on error *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001992int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic)
1993{
1994 return gsm0503_rach_ext_encode(burst, *ra, bsic, false);
1995}
1996
1997/*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003
1998 * \param[out] burst Caller-allocated output burst buffer
1999 * \param[in] ra11 Input RACH data
2000 * \param[in] bsic BSIC used in this cell
2001 * \param[in] is_11bit whether given RA is 11 bit or not
Piotr Krysikb3bd68e2018-02-27 14:37:01 +01002002 * \returns 0 on success; negative on error *
Piotr Krysik9e2e8352018-02-27 12:16:25 +01002003int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit)
2004{
2005 ubit_t conv[17];
2006 uint8_t ra[2] = { 0 }, nbits = 8;
2007
2008 if (is_11bit) {
2009 osmo_store16le(ra11, ra);
2010 nbits = 11;
2011 } else
2012 ra[0] = (uint8_t)ra11;
2013
2014 osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1);
2015
2016 osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits);
2017
2018 rach_apply_bsic(conv, bsic, nbits);
2019
2020 osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst);
Piotr Krysik70c25a12017-01-03 08:01:23 +01002021
2022 return 0;
2023}
2024
2025/*
2026 * GSM SCH transcoding
2027 */
Piotr Krysik9e2e8352018-02-27 12:16:25 +01002028
2029/*! Decode the SCH according to TS 05.03
2030 * \param[out] sb_info output buffer for SCH data
2031 * \param[in] burst Input burst data
2032 * \returns 0 on success; negative on error (e.g. CRC error) */
2033int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst)
Piotr Krysik70c25a12017-01-03 08:01:23 +01002034{
2035 ubit_t conv[35];
2036 int rv;
2037
2038 osmo_conv_decode(&gsm0503_sch, burst, conv);
2039
2040 rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
2041 if (rv)
2042 return -1;
2043
2044 osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1);
2045
2046 return 0;
2047}
2048
Piotr Krysik9e2e8352018-02-27 12:16:25 +01002049/*! Encode the SCH according to TS 05.03
2050 * \param[out] burst Caller-allocated output burst buffer
2051 * \param[in] sb_info Input SCH data
2052 * \returns 0 on success; negative on error */
2053int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info)
Piotr Krysik70c25a12017-01-03 08:01:23 +01002054{
2055 ubit_t conv[35];
2056
2057 osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1);
2058
2059 osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25);
2060
2061 osmo_conv_encode(&gsm0503_sch, conv, burst);
2062
2063 return 0;
2064}
Piotr Krysik9e2e8352018-02-27 12:16:25 +01002065
2066/*! @} */