Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1 | /* |
| 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 Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 5 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 11 | * 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 Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 26 | #include <errno.h> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 27 | |
| 28 | #include <osmocom/core/bits.h> |
| 29 | #include <osmocom/core/conv.h> |
| 30 | #include <osmocom/core/utils.h> |
| 31 | #include <osmocom/core/crcgen.h> |
| 32 | #include <osmocom/core/endian.h> |
| 33 | |
Pau Espin Pedrol | 0158b05 | 2023-02-17 12:30:57 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/protocol/gsm_44_060.h> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 35 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 36 | #include <osmocom/gsm/gsm0503.h> |
| 37 | #include <osmocom/codec/codec.h> |
| 38 | |
| 39 | #include <osmocom/coding/gsm0503_interleaving.h> |
| 40 | #include <osmocom/coding/gsm0503_mapping.h> |
| 41 | #include <osmocom/coding/gsm0503_tables.h> |
| 42 | #include <osmocom/coding/gsm0503_coding.h> |
| 43 | #include <osmocom/coding/gsm0503_parity.h> |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 44 | #include <osmocom/coding/gsm0503_amr_dtx.h> |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 45 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 46 | /*! \mainpage libosmocoding Documentation |
| 47 | * |
| 48 | * \section sec_intro Introduction |
| 49 | * This library is a collection of definitions, tables and functions |
| 50 | * implementing the GSM/GPRS/EGPRS channel coding (and decoding) as |
| 51 | * specified in 3GPP TS 05.03 / 45.003. |
| 52 | * |
Vadim Yanitskiy | 9a232fd | 2018-01-19 03:05:32 +0600 | [diff] [blame] | 53 | * libosmocoding is developed as part of the Osmocom (Open Source Mobile |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 54 | * Communications) project, a community-based, collaborative development |
| 55 | * project to create Free and Open Source implementations of mobile |
| 56 | * communications systems. For more information about Osmocom, please |
| 57 | * see https://osmocom.org/ |
| 58 | * |
| 59 | * \section sec_copyright Copyright and License |
| 60 | * Copyright © 2013 by Andreas Eversberg\n |
| 61 | * Copyright © 2015 by Alexander Chemeris\n |
| 62 | * Copyright © 2016 by Tom Tsou\n |
| 63 | * Documentation Copyright © 2017 by Harald Welte\n |
| 64 | * All rights reserved. \n\n |
| 65 | * The source code of libosmocoding is licensed under the terms of the GNU |
| 66 | * General Public License as published by the Free Software Foundation; |
| 67 | * either version 2 of the License, or (at your option) any later |
| 68 | * version.\n |
| 69 | * See <http://www.gnu.org/licenses/> or COPYING included in the source |
| 70 | * code package istelf.\n |
| 71 | * The information detailed here is provided AS IS with NO WARRANTY OF |
| 72 | * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND |
| 73 | * FITNESS FOR A PARTICULAR PURPOSE. |
| 74 | * \n\n |
| 75 | * |
| 76 | * \section sec_tracker Homepage + Issue Tracker |
| 77 | * libosmocoding is distributed as part of libosmocore and shares its |
| 78 | * project page at http://osmocom.org/projects/libosmocore |
| 79 | * |
| 80 | * An Issue Tracker can be found at |
| 81 | * https://osmocom.org/projects/libosmocore/issues |
| 82 | * |
| 83 | * \section sec_contact Contact and Support |
| 84 | * Community-based support is available at the OpenBSC mailing list |
| 85 | * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n |
| 86 | * Commercial support options available upon request from |
| 87 | * <http://sysmocom.de/> |
| 88 | */ |
| 89 | |
| 90 | |
| 91 | /*! \addtogroup coding |
| 92 | * @{ |
| 93 | * |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 94 | * GSM TS 05.03 coding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 95 | * |
| 96 | * This module is the "master module" of libosmocoding. It uses the |
| 97 | * various other modules (mapping, parity, interleaving) in order to |
| 98 | * implement the complete channel coding (and decoding) chain for the |
| 99 | * various channel types as defined in TS 05.03 / 45.003. |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 100 | * |
| 101 | * \file gsm0503_coding.c */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 102 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 103 | /* |
| 104 | * EGPRS coding limits |
| 105 | */ |
| 106 | |
| 107 | /* Max header size with parity bits */ |
| 108 | #define EGPRS_HDR_UPP_MAX 54 |
| 109 | |
| 110 | /* Max encoded header size */ |
| 111 | #define EGPRS_HDR_C_MAX 162 |
| 112 | |
| 113 | /* Max punctured header size */ |
| 114 | #define EGPRS_HDR_HC_MAX 160 |
| 115 | |
| 116 | /* Max data block size with parity bits */ |
| 117 | #define EGPRS_DATA_U_MAX 612 |
| 118 | |
| 119 | /* Max encoded data block size */ |
| 120 | #define EGPRS_DATA_C_MAX 1836 |
| 121 | |
| 122 | /* Max single block punctured data size */ |
| 123 | #define EGPRS_DATA_DC_MAX 1248 |
| 124 | |
| 125 | /* Dual block punctured data size */ |
| 126 | #define EGPRS_DATA_C1 612 |
| 127 | #define EGPRS_DATA_C2 EGPRS_DATA_C1 |
| 128 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 129 | /*! union across the three different EGPRS Uplink header types */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 130 | union gprs_rlc_ul_hdr_egprs { |
| 131 | struct gprs_rlc_ul_header_egprs_1 type1; |
| 132 | struct gprs_rlc_ul_header_egprs_2 type2; |
| 133 | struct gprs_rlc_ul_header_egprs_3 type3; |
| 134 | }; |
| 135 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 136 | /*! union across the three different EGPRS Downlink header types */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 137 | union gprs_rlc_dl_hdr_egprs { |
| 138 | struct gprs_rlc_dl_header_egprs_1 type1; |
| 139 | struct gprs_rlc_dl_header_egprs_2 type2; |
| 140 | struct gprs_rlc_dl_header_egprs_3 type3; |
| 141 | }; |
| 142 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 143 | /*! Structure describing a Modulation and Coding Scheme */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 144 | struct gsm0503_mcs_code { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 145 | /*! Modulation and Coding Scheme (MSC) number */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 146 | uint8_t mcs; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 147 | /*! Length of Uplink Stealing Flag (USF) in bits */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 148 | uint8_t usf_len; |
| 149 | |
| 150 | /* Header coding */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 151 | /*! Length of header (bits) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 152 | uint8_t hdr_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 153 | /*! Length of header convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 154 | uint8_t hdr_code_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 155 | /*! Length of header code puncturing sequence */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 156 | uint8_t hdr_punc_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 157 | /*! header convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 158 | const struct osmo_conv_code *hdr_conv; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 159 | /*! header puncturing sequence */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 160 | const uint8_t *hdr_punc; |
| 161 | |
| 162 | /* Data coding */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 163 | /*! length of data (bits) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 164 | uint16_t data_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 165 | /*! length of data convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 166 | uint16_t data_code_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 167 | /*! length of data code puncturing sequence */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 168 | uint16_t data_punc_len; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 169 | /*! data convolutional code */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 170 | const struct osmo_conv_code *data_conv; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 171 | /*! data puncturing sequences */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 172 | const uint8_t *data_punc[3]; |
| 173 | }; |
| 174 | |
| 175 | /* |
| 176 | * EGPRS UL coding parameters |
| 177 | */ |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 178 | const struct gsm0503_mcs_code gsm0503_mcs_ul_codes[EGPRS_NUM_MCS] = { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 179 | { |
| 180 | .mcs = EGPRS_MCS0, |
| 181 | }, |
| 182 | { |
| 183 | .mcs = EGPRS_MCS1, |
| 184 | .hdr_len = 31, |
| 185 | .hdr_code_len = 117, |
| 186 | .hdr_punc_len = 80, |
| 187 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 188 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 189 | |
| 190 | .data_len = 178, |
| 191 | .data_code_len = 588, |
| 192 | .data_punc_len = 372, |
| 193 | .data_conv = &gsm0503_mcs1, |
| 194 | .data_punc = { |
| 195 | gsm0503_puncture_mcs1_p1, |
| 196 | gsm0503_puncture_mcs1_p2, |
| 197 | NULL, |
| 198 | }, |
| 199 | }, |
| 200 | { |
| 201 | .mcs = EGPRS_MCS2, |
| 202 | .hdr_len = 31, |
| 203 | .hdr_code_len = 117, |
| 204 | .hdr_punc_len = 80, |
| 205 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 206 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 207 | |
| 208 | .data_len = 226, |
| 209 | .data_code_len = 732, |
| 210 | .data_punc_len = 372, |
| 211 | .data_conv = &gsm0503_mcs2, |
| 212 | .data_punc = { |
| 213 | gsm0503_puncture_mcs2_p1, |
| 214 | gsm0503_puncture_mcs2_p2, |
| 215 | NULL, |
| 216 | }, |
| 217 | }, |
| 218 | { |
| 219 | .mcs = EGPRS_MCS3, |
| 220 | .hdr_len = 31, |
| 221 | .hdr_code_len = 117, |
| 222 | .hdr_punc_len = 80, |
| 223 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 224 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 225 | |
| 226 | .data_len = 298, |
| 227 | .data_code_len = 948, |
| 228 | .data_punc_len = 372, |
| 229 | .data_conv = &gsm0503_mcs3, |
| 230 | .data_punc = { |
| 231 | gsm0503_puncture_mcs3_p1, |
| 232 | gsm0503_puncture_mcs3_p2, |
| 233 | gsm0503_puncture_mcs3_p3, |
| 234 | }, |
| 235 | }, |
| 236 | { |
| 237 | .mcs = EGPRS_MCS4, |
| 238 | .hdr_len = 31, |
| 239 | .hdr_code_len = 117, |
| 240 | .hdr_punc_len = 80, |
| 241 | .hdr_conv = &gsm0503_mcs1_ul_hdr, |
| 242 | .hdr_punc = gsm0503_puncture_mcs1_ul_hdr, |
| 243 | |
| 244 | .data_len = 354, |
| 245 | .data_code_len = 1116, |
| 246 | .data_punc_len = 372, |
| 247 | .data_conv = &gsm0503_mcs4, |
| 248 | .data_punc = { |
| 249 | gsm0503_puncture_mcs4_p1, |
| 250 | gsm0503_puncture_mcs4_p2, |
| 251 | gsm0503_puncture_mcs4_p3, |
| 252 | }, |
| 253 | }, |
| 254 | { |
| 255 | .mcs = EGPRS_MCS5, |
| 256 | .hdr_len = 37, |
| 257 | .hdr_code_len = 135, |
| 258 | .hdr_punc_len = 136, |
| 259 | .hdr_conv = &gsm0503_mcs5_ul_hdr, |
| 260 | .hdr_punc = NULL, |
| 261 | |
| 262 | .data_len = 450, |
| 263 | .data_code_len = 1404, |
| 264 | .data_punc_len = 1248, |
| 265 | .data_conv = &gsm0503_mcs5, |
| 266 | .data_punc = { |
| 267 | gsm0503_puncture_mcs5_p1, |
| 268 | gsm0503_puncture_mcs5_p2, |
| 269 | NULL, |
| 270 | }, |
| 271 | }, |
| 272 | { |
| 273 | .mcs = EGPRS_MCS6, |
| 274 | .hdr_len = 37, |
| 275 | .hdr_code_len = 135, |
| 276 | .hdr_punc_len = 136, |
| 277 | .hdr_conv = &gsm0503_mcs5_ul_hdr, |
| 278 | .hdr_punc = NULL, |
| 279 | |
| 280 | .data_len = 594, |
| 281 | .data_code_len = 1836, |
| 282 | .data_punc_len = 1248, |
| 283 | .data_conv = &gsm0503_mcs6, |
| 284 | .data_punc = { |
| 285 | gsm0503_puncture_mcs6_p1, |
| 286 | gsm0503_puncture_mcs6_p2, |
| 287 | NULL, |
| 288 | }, |
| 289 | }, |
| 290 | { |
| 291 | .mcs = EGPRS_MCS7, |
| 292 | .hdr_len = 46, |
| 293 | .hdr_code_len = 162, |
| 294 | .hdr_punc_len = 160, |
| 295 | .hdr_conv = &gsm0503_mcs7_ul_hdr, |
| 296 | .hdr_punc = gsm0503_puncture_mcs7_ul_hdr, |
| 297 | |
| 298 | .data_len = 900, |
| 299 | .data_code_len = 1404, |
| 300 | .data_punc_len = 612, |
| 301 | .data_conv = &gsm0503_mcs7, |
| 302 | .data_punc = { |
| 303 | gsm0503_puncture_mcs7_p1, |
| 304 | gsm0503_puncture_mcs7_p2, |
| 305 | gsm0503_puncture_mcs7_p3, |
| 306 | } |
| 307 | }, |
| 308 | { |
| 309 | .mcs = EGPRS_MCS8, |
| 310 | .hdr_len = 46, |
| 311 | .hdr_code_len = 162, |
| 312 | .hdr_punc_len = 160, |
| 313 | .hdr_conv = &gsm0503_mcs7_ul_hdr, |
| 314 | .hdr_punc = gsm0503_puncture_mcs7_ul_hdr, |
| 315 | |
| 316 | .data_len = 1092, |
| 317 | .data_code_len = 1692, |
| 318 | .data_punc_len = 612, |
| 319 | .data_conv = &gsm0503_mcs8, |
| 320 | .data_punc = { |
| 321 | gsm0503_puncture_mcs8_p1, |
| 322 | gsm0503_puncture_mcs8_p2, |
| 323 | gsm0503_puncture_mcs8_p3, |
| 324 | } |
| 325 | }, |
| 326 | { |
| 327 | .mcs = EGPRS_MCS9, |
| 328 | .hdr_len = 46, |
| 329 | .hdr_code_len = 162, |
| 330 | .hdr_punc_len = 160, |
| 331 | .hdr_conv = &gsm0503_mcs7_ul_hdr, |
| 332 | .hdr_punc = gsm0503_puncture_mcs7_ul_hdr, |
| 333 | |
| 334 | .data_len = 1188, |
| 335 | .data_code_len = 1836, |
| 336 | .data_punc_len = 612, |
| 337 | .data_conv = &gsm0503_mcs9, |
| 338 | .data_punc = { |
| 339 | gsm0503_puncture_mcs9_p1, |
| 340 | gsm0503_puncture_mcs9_p2, |
| 341 | gsm0503_puncture_mcs9_p3, |
| 342 | } |
| 343 | }, |
| 344 | }; |
| 345 | |
| 346 | /* |
| 347 | * EGPRS DL coding parameters |
| 348 | */ |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 349 | const struct gsm0503_mcs_code gsm0503_mcs_dl_codes[EGPRS_NUM_MCS] = { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 350 | { |
| 351 | .mcs = EGPRS_MCS0, |
| 352 | }, |
| 353 | { |
| 354 | .mcs = EGPRS_MCS1, |
| 355 | .usf_len = 3, |
| 356 | .hdr_len = 28, |
| 357 | .hdr_code_len = 108, |
| 358 | .hdr_punc_len = 68, |
| 359 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 360 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 361 | |
| 362 | .data_len = 178, |
| 363 | .data_code_len = 588, |
| 364 | .data_punc_len = 372, |
| 365 | .data_conv = &gsm0503_mcs1, |
| 366 | .data_punc = { |
| 367 | gsm0503_puncture_mcs1_p1, |
| 368 | gsm0503_puncture_mcs1_p2, |
| 369 | NULL, |
| 370 | }, |
| 371 | }, |
| 372 | { |
| 373 | .mcs = EGPRS_MCS2, |
| 374 | .usf_len = 3, |
| 375 | .hdr_len = 28, |
| 376 | .hdr_code_len = 108, |
| 377 | .hdr_punc_len = 68, |
| 378 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 379 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 380 | |
| 381 | .data_len = 226, |
| 382 | .data_code_len = 732, |
| 383 | .data_punc_len = 372, |
| 384 | .data_conv = &gsm0503_mcs2, |
| 385 | .data_punc = { |
| 386 | gsm0503_puncture_mcs2_p1, |
| 387 | gsm0503_puncture_mcs2_p2, |
| 388 | NULL, |
| 389 | }, |
| 390 | }, |
| 391 | { |
| 392 | .mcs = EGPRS_MCS3, |
| 393 | .usf_len = 3, |
| 394 | .hdr_len = 28, |
| 395 | .hdr_code_len = 108, |
| 396 | .hdr_punc_len = 68, |
| 397 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 398 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 399 | |
| 400 | .data_len = 298, |
| 401 | .data_code_len = 948, |
| 402 | .data_punc_len = 372, |
| 403 | .data_conv = &gsm0503_mcs3, |
| 404 | .data_punc = { |
| 405 | gsm0503_puncture_mcs3_p1, |
| 406 | gsm0503_puncture_mcs3_p2, |
| 407 | gsm0503_puncture_mcs3_p3, |
| 408 | }, |
| 409 | }, |
| 410 | { |
| 411 | .mcs = EGPRS_MCS4, |
| 412 | .usf_len = 3, |
| 413 | .hdr_len = 28, |
| 414 | .hdr_code_len = 108, |
| 415 | .hdr_punc_len = 68, |
| 416 | .hdr_conv = &gsm0503_mcs1_dl_hdr, |
| 417 | .hdr_punc = gsm0503_puncture_mcs1_dl_hdr, |
| 418 | |
| 419 | .data_len = 354, |
| 420 | .data_code_len = 1116, |
| 421 | .data_punc_len = 372, |
| 422 | .data_conv = &gsm0503_mcs4, |
| 423 | .data_punc = { |
| 424 | gsm0503_puncture_mcs4_p1, |
| 425 | gsm0503_puncture_mcs4_p2, |
| 426 | gsm0503_puncture_mcs4_p3, |
| 427 | }, |
| 428 | }, |
| 429 | { |
| 430 | .mcs = EGPRS_MCS5, |
| 431 | .usf_len = 3, |
| 432 | .hdr_len = 25, |
| 433 | .hdr_code_len = 99, |
| 434 | .hdr_punc_len = 100, |
| 435 | .hdr_conv = &gsm0503_mcs5_dl_hdr, |
| 436 | .hdr_punc = NULL, |
| 437 | |
| 438 | .data_len = 450, |
| 439 | .data_code_len = 1404, |
| 440 | .data_punc_len = 1248, |
| 441 | .data_conv = &gsm0503_mcs5, |
| 442 | .data_punc = { |
| 443 | gsm0503_puncture_mcs5_p1, |
| 444 | gsm0503_puncture_mcs5_p2, |
| 445 | NULL, |
| 446 | }, |
| 447 | }, |
| 448 | { |
| 449 | .mcs = EGPRS_MCS6, |
| 450 | .usf_len = 3, |
| 451 | .hdr_len = 25, |
| 452 | .hdr_code_len = 99, |
| 453 | .hdr_punc_len = 100, |
| 454 | .hdr_conv = &gsm0503_mcs5_dl_hdr, |
| 455 | .hdr_punc = NULL, |
| 456 | |
| 457 | .data_len = 594, |
| 458 | .data_code_len = 1836, |
| 459 | .data_punc_len = 1248, |
| 460 | .data_conv = &gsm0503_mcs6, |
| 461 | .data_punc = { |
| 462 | gsm0503_puncture_mcs6_p1, |
| 463 | gsm0503_puncture_mcs6_p2, |
| 464 | NULL, |
| 465 | }, |
| 466 | }, |
| 467 | { |
| 468 | .mcs = EGPRS_MCS7, |
| 469 | .usf_len = 3, |
| 470 | .hdr_len = 37, |
| 471 | .hdr_code_len = 135, |
| 472 | .hdr_punc_len = 124, |
| 473 | .hdr_conv = &gsm0503_mcs7_dl_hdr, |
| 474 | .hdr_punc = gsm0503_puncture_mcs7_dl_hdr, |
| 475 | |
| 476 | .data_len = 900, |
| 477 | .data_code_len = 1404, |
| 478 | .data_punc_len = 612, |
| 479 | .data_conv = &gsm0503_mcs7, |
| 480 | .data_punc = { |
| 481 | gsm0503_puncture_mcs7_p1, |
| 482 | gsm0503_puncture_mcs7_p2, |
| 483 | gsm0503_puncture_mcs7_p3, |
| 484 | } |
| 485 | }, |
| 486 | { |
| 487 | .mcs = EGPRS_MCS8, |
| 488 | .usf_len = 3, |
| 489 | .hdr_len = 37, |
| 490 | .hdr_code_len = 135, |
| 491 | .hdr_punc_len = 124, |
| 492 | .hdr_conv = &gsm0503_mcs7_dl_hdr, |
| 493 | .hdr_punc = gsm0503_puncture_mcs7_dl_hdr, |
| 494 | |
| 495 | .data_len = 1092, |
| 496 | .data_code_len = 1692, |
| 497 | .data_punc_len = 612, |
| 498 | .data_conv = &gsm0503_mcs8, |
| 499 | .data_punc = { |
| 500 | gsm0503_puncture_mcs8_p1, |
| 501 | gsm0503_puncture_mcs8_p2, |
| 502 | gsm0503_puncture_mcs8_p3, |
| 503 | } |
| 504 | }, |
| 505 | { |
| 506 | .mcs = EGPRS_MCS9, |
| 507 | .usf_len = 3, |
| 508 | .hdr_len = 37, |
| 509 | .hdr_code_len = 135, |
| 510 | .hdr_punc_len = 124, |
| 511 | .hdr_conv = &gsm0503_mcs7_dl_hdr, |
| 512 | .hdr_punc = gsm0503_puncture_mcs7_dl_hdr, |
| 513 | |
| 514 | .data_len = 1188, |
| 515 | .data_code_len = 1836, |
| 516 | .data_punc_len = 612, |
| 517 | .data_conv = &gsm0503_mcs9, |
| 518 | .data_punc = { |
| 519 | gsm0503_puncture_mcs9_p1, |
| 520 | gsm0503_puncture_mcs9_p2, |
| 521 | gsm0503_puncture_mcs9_p3, |
| 522 | } |
| 523 | }, |
| 524 | }; |
| 525 | |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 526 | /*! Convolutional Decode + compute BER for punctured codes |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 527 | * \param[in] code Description of Convolutional Code |
| 528 | * \param[in] input Input soft-bits (-127...127) |
| 529 | * \param[out] output bits |
| 530 | * \param[out] n_errors Number of bit-errors |
| 531 | * \param[out] n_bits_total Number of bits |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 532 | * \param[in] data_punc Puncturing mask array. Can be NULL. |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 533 | */ |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 534 | static int osmo_conv_decode_ber_punctured(const struct osmo_conv_code *code, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 535 | const sbit_t *input, ubit_t *output, |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 536 | int *n_errors, int *n_bits_total, |
| 537 | const uint8_t *data_punc) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 538 | { |
Vadim Yanitskiy | 2a72d68 | 2024-01-31 20:56:53 +0700 | [diff] [blame] | 539 | int res, coded_len; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 540 | ubit_t recoded[EGPRS_DATA_C_MAX]; |
| 541 | |
| 542 | res = osmo_conv_decode(code, input, output); |
| 543 | |
Pau Espin Pedrol | d2737e6 | 2022-05-13 13:07:21 +0200 | [diff] [blame] | 544 | if (!n_bits_total && !n_errors) |
| 545 | return res; |
| 546 | |
| 547 | coded_len = osmo_conv_encode(code, output, recoded); |
Pau Espin Pedrol | 6c58d15 | 2022-05-13 14:03:27 +0200 | [diff] [blame] | 548 | OSMO_ASSERT(ARRAY_SIZE(recoded) >= coded_len); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 549 | |
| 550 | /* Count bit errors */ |
| 551 | if (n_errors) { |
| 552 | *n_errors = 0; |
Vadim Yanitskiy | 2a72d68 | 2024-01-31 20:56:53 +0700 | [diff] [blame] | 553 | for (unsigned int i = 0; i < coded_len; i++) { |
| 554 | /* punctured bits do not count as bit errors */ |
| 555 | if (data_punc != NULL && data_punc[i]) |
| 556 | continue; |
| 557 | if (recoded[i] == 1 && input[i] < 0) |
| 558 | continue; |
| 559 | if (recoded[i] == 0 && input[i] > 0) |
| 560 | continue; |
| 561 | *n_errors += 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | if (n_bits_total) |
| 566 | *n_bits_total = coded_len; |
| 567 | |
| 568 | return res; |
| 569 | } |
| 570 | |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 571 | /*! Convolutional Decode + compute BER for non-punctured codes |
| 572 | * \param[in] code Description of Convolutional Code |
| 573 | * \param[in] input Input soft-bits (-127...127) |
| 574 | * \param[out] output bits |
| 575 | * \param[out] n_errors Number of bit-errors |
| 576 | * \param[out] n_bits_total Number of bits |
| 577 | */ |
| 578 | static int osmo_conv_decode_ber(const struct osmo_conv_code *code, |
| 579 | const sbit_t *input, ubit_t *output, |
| 580 | int *n_errors, int *n_bits_total) |
| 581 | { |
| 582 | return osmo_conv_decode_ber_punctured(code, input, output, |
| 583 | n_errors, n_bits_total, NULL); |
| 584 | } |
| 585 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 586 | /*! convenience wrapper for decoding coded bits |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 587 | * \param[out] l2_data caller-allocated buffer for L2 Frame |
| 588 | * \param[in] cB 456 coded (soft) bits as per TS 05.03 4.1.3 |
| 589 | * \param[out] n_errors Number of detected errors |
| 590 | * \param[out] n_bits_total Number of total coded bits |
| 591 | * \returns 0 on success; -1 on CRC error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 592 | static int _xcch_decode_cB(uint8_t *l2_data, const sbit_t *cB, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 593 | int *n_errors, int *n_bits_total) |
| 594 | { |
| 595 | ubit_t conv[224]; |
| 596 | int rv; |
| 597 | |
| 598 | osmo_conv_decode_ber(&gsm0503_xcch, cB, |
| 599 | conv, n_errors, n_bits_total); |
| 600 | |
| 601 | rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40, |
| 602 | conv, 184, conv + 184); |
| 603 | if (rv) |
| 604 | return -1; |
| 605 | |
| 606 | osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 611 | /*! convenience wrapper for encoding to coded bits |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 612 | * \param[out] cB caller-allocated buffer for 456 coded bits as per TS 05.03 4.1.3 |
Vadim Yanitskiy | fed02e0 | 2023-05-15 23:25:05 +0700 | [diff] [blame] | 613 | * \param[in] l2_data to-be-encoded L2 Frame |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 614 | * \returns 0 */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 615 | static int _xcch_encode_cB(ubit_t *cB, const uint8_t *l2_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 616 | { |
| 617 | ubit_t conv[224]; |
| 618 | |
| 619 | osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1); |
| 620 | |
| 621 | osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184); |
| 622 | |
| 623 | osmo_conv_encode(&gsm0503_xcch, conv, cB); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * GSM xCCH block transcoding |
| 630 | */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 631 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 632 | /*! Decoding of xCCH data from bursts to L2 frame |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 633 | * \param[out] l2_data caller-allocated output data buffer |
| 634 | * \param[in] bursts four GSM bursts in soft-bits |
| 635 | * \param[out] n_errors Number of detected errors |
| 636 | * \param[out] n_bits_total Number of total coded bits |
| 637 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 638 | int gsm0503_xcch_decode(uint8_t *l2_data, const sbit_t *bursts, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 639 | int *n_errors, int *n_bits_total) |
| 640 | { |
| 641 | sbit_t iB[456], cB[456]; |
| 642 | int i; |
| 643 | |
| 644 | for (i = 0; i < 4; i++) |
| 645 | gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, NULL); |
| 646 | |
| 647 | gsm0503_xcch_deinterleave(cB, iB); |
| 648 | |
| 649 | return _xcch_decode_cB(l2_data, cB, n_errors, n_bits_total); |
| 650 | } |
| 651 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 652 | /*! Encoding of xCCH data from L2 frame to bursts |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 653 | * \param[out] bursts caller-allocated burst data (unpacked bits) |
| 654 | * \param[in] l2_data L2 input data (MAC block) |
| 655 | * \returns 0 |
| 656 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 657 | int gsm0503_xcch_encode(ubit_t *bursts, const uint8_t *l2_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 658 | { |
| 659 | ubit_t iB[456], cB[456], hl = 1, hn = 1; |
| 660 | int i; |
| 661 | |
| 662 | _xcch_encode_cB(cB, l2_data); |
| 663 | |
| 664 | gsm0503_xcch_interleave(cB, iB); |
| 665 | |
| 666 | for (i = 0; i < 4; i++) |
| 667 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], &hl, &hn); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * EGPRS PDTCH UL block decoding |
| 674 | */ |
| 675 | |
| 676 | /* |
| 677 | * Type 3 - MCS-1,2,3,4 |
| 678 | * Unmapping and deinterleaving |
| 679 | */ |
| 680 | static int egprs_type3_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc) |
| 681 | { |
| 682 | int i; |
| 683 | sbit_t iB[456], q[8]; |
| 684 | |
| 685 | for (i = 0; i < 4; i++) { |
| 686 | gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], |
| 687 | q + i * 2, q + i * 2 + 1); |
| 688 | } |
| 689 | |
| 690 | gsm0503_mcs1_ul_deinterleave(hc, dc, iB); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Type 2 - MCS-5,6 |
| 697 | * Unmapping and deinterleaving |
| 698 | */ |
| 699 | static int egprs_type2_unmap(const sbit_t *bursts, sbit_t *hc, sbit_t *dc) |
| 700 | { |
| 701 | int i; |
| 702 | sbit_t burst[348]; |
| 703 | sbit_t hi[EGPRS_HDR_HC_MAX]; |
| 704 | sbit_t di[EGPRS_DATA_DC_MAX]; |
| 705 | |
| 706 | for (i = 0; i < 4; i++) { |
| 707 | memcpy(burst, &bursts[i * 348], 348); |
| 708 | |
| 709 | gsm0503_mcs5_burst_swap(burst); |
| 710 | gsm0503_mcs5_ul_burst_unmap(di, burst, hi, i); |
| 711 | } |
| 712 | |
| 713 | gsm0503_mcs5_ul_deinterleave(hc, dc, hi, di); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Type 1 - MCS-7,8,9 |
| 720 | * Unmapping and deinterleaving - Note that MCS-7 interleaver is unique |
| 721 | */ |
| 722 | static int egprs_type1_unmap(const sbit_t *bursts, sbit_t *hc, |
| 723 | sbit_t *c1, sbit_t *c2, int msc) |
| 724 | { |
| 725 | int i; |
| 726 | sbit_t burst[348]; |
| 727 | sbit_t hi[EGPRS_HDR_HC_MAX]; |
| 728 | sbit_t di[EGPRS_DATA_C1 * 2]; |
| 729 | |
| 730 | for (i = 0; i < 4; i++) { |
| 731 | memcpy(burst, &bursts[i * 348], 348); |
| 732 | |
| 733 | gsm0503_mcs5_burst_swap(burst); |
| 734 | gsm0503_mcs7_ul_burst_unmap(di, burst, hi, i); |
| 735 | } |
| 736 | |
| 737 | if (msc == EGPRS_MCS7) |
| 738 | gsm0503_mcs7_ul_deinterleave(hc, c1, c2, hi, di); |
| 739 | else |
| 740 | gsm0503_mcs8_ul_deinterleave(hc, c1, c2, hi, di); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Decode EGPRS UL header section |
| 747 | * |
| 748 | * 1. Depuncture |
| 749 | * 2. Convolutional decoding |
| 750 | * 3. CRC check |
| 751 | */ |
| 752 | static int _egprs_decode_hdr(const sbit_t *hc, int mcs, |
| 753 | union gprs_rlc_ul_hdr_egprs *hdr) |
| 754 | { |
| 755 | sbit_t C[EGPRS_HDR_C_MAX]; |
| 756 | ubit_t upp[EGPRS_HDR_UPP_MAX]; |
| 757 | int i, j, rc; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 758 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 759 | |
| 760 | code = &gsm0503_mcs_ul_codes[mcs]; |
| 761 | |
| 762 | /* Skip depuncturing on MCS-5,6 header */ |
| 763 | if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) { |
| 764 | memcpy(C, hc, code->hdr_code_len); |
| 765 | goto hdr_conv_decode; |
| 766 | } |
| 767 | |
| 768 | if (!code->hdr_punc) { |
| 769 | /* Invalid MCS-X header puncture matrix */ |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | i = code->hdr_code_len - 1; |
| 774 | j = code->hdr_punc_len - 1; |
| 775 | |
| 776 | for (; i >= 0; i--) { |
| 777 | if (!code->hdr_punc[i]) |
| 778 | C[i] = hc[j--]; |
| 779 | else |
| 780 | C[i] = 0; |
| 781 | } |
| 782 | |
| 783 | hdr_conv_decode: |
| 784 | osmo_conv_decode_ber(code->hdr_conv, C, upp, NULL, NULL); |
| 785 | rc = osmo_crc8gen_check_bits(&gsm0503_mcs_crc8_hdr, upp, |
| 786 | code->hdr_len, upp + code->hdr_len); |
| 787 | if (rc) |
| 788 | return -1; |
| 789 | |
| 790 | osmo_ubit2pbit_ext((pbit_t *) hdr, 0, upp, 0, code->hdr_len, 1); |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Blind MCS header decoding based on burst length and CRC validation. |
| 797 | * Ignore 'q' value coding identification. This approach provides |
| 798 | * the strongest chance of header recovery. |
| 799 | */ |
| 800 | static int egprs_decode_hdr(union gprs_rlc_ul_hdr_egprs *hdr, |
| 801 | const sbit_t *bursts, uint16_t nbits) |
| 802 | { |
| 803 | int rc; |
| 804 | sbit_t hc[EGPRS_HDR_HC_MAX]; |
| 805 | |
| 806 | if (nbits == GSM0503_GPRS_BURSTS_NBITS) { |
| 807 | /* MCS-1,2,3,4 */ |
| 808 | egprs_type3_unmap(bursts, hc, NULL); |
| 809 | rc = _egprs_decode_hdr(hc, EGPRS_MCS1, hdr); |
| 810 | if (!rc) |
| 811 | return EGPRS_HDR_TYPE3; |
| 812 | } else if (nbits == GSM0503_EGPRS_BURSTS_NBITS) { |
| 813 | /* MCS-5,6 */ |
| 814 | egprs_type2_unmap(bursts, hc, NULL); |
| 815 | rc = _egprs_decode_hdr(hc, EGPRS_MCS5, hdr); |
| 816 | if (!rc) |
| 817 | return EGPRS_HDR_TYPE2; |
| 818 | |
| 819 | /* MCS-7,8,9 */ |
| 820 | egprs_type1_unmap(bursts, hc, NULL, NULL, EGPRS_MCS7); |
| 821 | rc = _egprs_decode_hdr(hc, EGPRS_MCS7, hdr); |
| 822 | if (!rc) |
| 823 | return EGPRS_HDR_TYPE1; |
| 824 | } |
| 825 | |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Parse EGPRS UL header for coding and puncturing scheme (CPS) |
| 831 | * |
| 832 | * Type 1 - MCS-7,8,9 |
| 833 | * Type 2 - MCS-5,6 |
| 834 | * Type 3 - MCS-1,2,3,4 |
| 835 | */ |
| 836 | static int egprs_parse_ul_cps(struct egprs_cps *cps, |
| 837 | union gprs_rlc_ul_hdr_egprs *hdr, int type) |
| 838 | { |
| 839 | uint8_t bits; |
| 840 | |
| 841 | switch (type) { |
| 842 | case EGPRS_HDR_TYPE1: |
| 843 | bits = hdr->type1.cps; |
| 844 | break; |
| 845 | case EGPRS_HDR_TYPE2: |
| 846 | bits = (hdr->type2.cps_lo << 2) | hdr->type2.cps_hi; |
| 847 | break; |
| 848 | case EGPRS_HDR_TYPE3: |
| 849 | bits = (hdr->type3.cps_lo << 2) | hdr->type3.cps_hi; |
| 850 | break; |
| 851 | default: |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | return egprs_get_cps(cps, type, bits); |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Decode EGPRS UL data section |
| 860 | * |
| 861 | * 1. Depuncture |
| 862 | * 2. Convolutional decoding |
| 863 | * 3. CRC check |
| 864 | * 4. Block combining (MCS-7,8,9 only) |
| 865 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 866 | static int egprs_decode_data(uint8_t *l2_data, const sbit_t *c, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 867 | int mcs, int p, int blk, int *n_errors, int *n_bits_total) |
| 868 | { |
| 869 | ubit_t u[EGPRS_DATA_U_MAX]; |
| 870 | sbit_t C[EGPRS_DATA_C_MAX]; |
| 871 | |
| 872 | int i, j, rc, data_len; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 873 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 874 | |
| 875 | if (blk && mcs < EGPRS_MCS7) { |
| 876 | /* Invalid MCS-X block state */ |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | code = &gsm0503_mcs_ul_codes[mcs]; |
| 881 | if (!code->data_punc[p]) { |
| 882 | /* Invalid MCS-X data puncture matrix */ |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * MCS-1,6 - single block processing |
| 888 | * MCS-7,9 - dual block processing |
| 889 | */ |
| 890 | if (mcs >= EGPRS_MCS7) |
| 891 | data_len = code->data_len / 2; |
| 892 | else |
| 893 | data_len = code->data_len; |
| 894 | |
| 895 | i = code->data_code_len - 1; |
| 896 | j = code->data_punc_len - 1; |
| 897 | |
| 898 | for (; i >= 0; i--) { |
| 899 | if (!code->data_punc[p][i]) |
| 900 | C[i] = c[j--]; |
| 901 | else |
| 902 | C[i] = 0; |
| 903 | } |
| 904 | |
Alexander Chemeris | 147051f | 2018-07-14 21:02:29 +0200 | [diff] [blame] | 905 | osmo_conv_decode_ber_punctured(code->data_conv, C, u, |
| 906 | n_errors, n_bits_total, code->data_punc[p]); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 907 | rc = osmo_crc16gen_check_bits(&gsm0503_mcs_crc12, u, |
| 908 | data_len, u + data_len); |
| 909 | if (rc) |
| 910 | return -1; |
| 911 | |
| 912 | /* Offsets output pointer on the second block of Type 1 MCS */ |
| 913 | osmo_ubit2pbit_ext(l2_data, code->hdr_len + blk * data_len, |
| 914 | u, 0, data_len, 1); |
| 915 | |
| 916 | /* Return the number of bytes required for the bit message */ |
Max | dd75bac | 2017-06-13 15:07:01 +0200 | [diff] [blame] | 917 | return OSMO_BYTES_FOR_BITS(code->hdr_len + code->data_len); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 918 | } |
| 919 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 920 | /*! Decode EGPRS UL message |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 921 | * 1. Header section decoding |
| 922 | * 2. Extract CPS settings |
| 923 | * 3. Burst unmapping and deinterleaving |
| 924 | * 4. Data section decoding |
| 925 | * \param[out] l2_data caller-allocated buffer for L2 Frame |
| 926 | * \param[in] bursts burst input data as soft unpacked bits |
| 927 | * \param[in] nbits number of bits in \a bursts |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 928 | * \param usf_p Uplink State Flag, FIXME: not implemented |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 929 | * \param[out] n_errors number of detected bit-errors |
Alexander Chemeris | ed7d2dd | 2018-07-14 21:06:27 +0200 | [diff] [blame] | 930 | * \param[out] n_bits_total total number of decoded bits |
Vadim Yanitskiy | ccf9aad | 2023-12-08 01:11:40 +0700 | [diff] [blame] | 931 | * \returns number of bytes decoded; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 932 | int gsm0503_pdtch_egprs_decode(uint8_t *l2_data, const sbit_t *bursts, uint16_t nbits, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 933 | uint8_t *usf_p, int *n_errors, int *n_bits_total) |
| 934 | { |
| 935 | sbit_t dc[EGPRS_DATA_DC_MAX]; |
| 936 | sbit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2]; |
| 937 | int type, rc; |
| 938 | struct egprs_cps cps; |
| 939 | union gprs_rlc_ul_hdr_egprs *hdr; |
| 940 | |
Alexander Chemeris | 50f7d74 | 2018-07-14 21:07:27 +0200 | [diff] [blame] | 941 | if (n_errors) |
| 942 | *n_errors = 0; |
| 943 | if (n_bits_total) |
| 944 | *n_bits_total = 0; |
| 945 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 946 | if ((nbits != GSM0503_GPRS_BURSTS_NBITS) && |
| 947 | (nbits != GSM0503_EGPRS_BURSTS_NBITS)) { |
| 948 | /* Invalid EGPRS bit length */ |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 949 | return -EOVERFLOW; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | hdr = (union gprs_rlc_ul_hdr_egprs *) l2_data; |
| 953 | type = egprs_decode_hdr(hdr, bursts, nbits); |
| 954 | if (egprs_parse_ul_cps(&cps, hdr, type) < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 955 | return -EIO; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 956 | |
| 957 | switch (cps.mcs) { |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 958 | case EGPRS_MCS0: |
| 959 | return -ENOTSUP; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 960 | case EGPRS_MCS1: |
| 961 | case EGPRS_MCS2: |
| 962 | case EGPRS_MCS3: |
| 963 | case EGPRS_MCS4: |
| 964 | egprs_type3_unmap(bursts, NULL, dc); |
| 965 | break; |
| 966 | case EGPRS_MCS5: |
| 967 | case EGPRS_MCS6: |
| 968 | egprs_type2_unmap(bursts, NULL, dc); |
| 969 | break; |
| 970 | case EGPRS_MCS7: |
| 971 | case EGPRS_MCS8: |
| 972 | case EGPRS_MCS9: |
| 973 | egprs_type1_unmap(bursts, NULL, c1, c2, cps.mcs); |
| 974 | break; |
| 975 | default: |
| 976 | /* Invalid MCS-X */ |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 977 | return -EINVAL; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* Decode MCS-X block, where X = cps.mcs */ |
| 981 | if (cps.mcs < EGPRS_MCS7) { |
| 982 | rc = egprs_decode_data(l2_data, dc, cps.mcs, cps.p[0], |
| 983 | 0, n_errors, n_bits_total); |
| 984 | if (rc < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 985 | return -EFAULT; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 986 | } else { |
Alexander Chemeris | 9444326 | 2018-07-14 21:09:54 +0200 | [diff] [blame] | 987 | /* Bit counters for the second block */ |
| 988 | int n_errors2, n_bits_total2; |
| 989 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 990 | /* MCS-7,8,9 block 1 */ |
| 991 | rc = egprs_decode_data(l2_data, c1, cps.mcs, cps.p[0], |
| 992 | 0, n_errors, n_bits_total); |
| 993 | if (rc < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 994 | return -EFAULT; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 995 | |
| 996 | /* MCS-7,8,9 block 2 */ |
| 997 | rc = egprs_decode_data(l2_data, c2, cps.mcs, cps.p[1], |
Alexander Chemeris | 9444326 | 2018-07-14 21:09:54 +0200 | [diff] [blame] | 998 | 1, &n_errors2, &n_bits_total2); |
| 999 | if (n_errors) |
| 1000 | *n_errors += n_errors2; |
| 1001 | if (n_bits_total) |
| 1002 | *n_bits_total += n_bits_total2; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1003 | if (rc < 0) |
Max | c8cf820 | 2017-05-22 16:07:04 +0200 | [diff] [blame] | 1004 | return -EFAULT; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | return rc; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * GSM PDTCH block transcoding |
| 1012 | */ |
| 1013 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1014 | /*! Decode GPRS PDTCH |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1015 | * \param[out] l2_data caller-allocated buffer for L2 Frame |
| 1016 | * \param[in] bursts burst input data as soft unpacked bits |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1017 | * \param[out] usf_p Uplink State Flag, only relevant for DL blocks |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1018 | * \param[out] n_errors number of detected bit-errors |
Vadim Yanitskiy | 9737451 | 2024-01-31 20:56:28 +0700 | [diff] [blame] | 1019 | * \param[out] n_bits_total total number of decoded bits |
Vadim Yanitskiy | ccf9aad | 2023-12-08 01:11:40 +0700 | [diff] [blame] | 1020 | * \returns number of bytes decoded; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1021 | int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1022 | int *n_errors, int *n_bits_total) |
| 1023 | { |
| 1024 | sbit_t iB[456], cB[676], hl_hn[8]; |
| 1025 | ubit_t conv[456]; |
| 1026 | int i, j, k, rv, best = 0, cs = 0, usf = 0; /* make GCC happy */ |
| 1027 | |
| 1028 | for (i = 0; i < 4; i++) |
| 1029 | gsm0503_xcch_burst_unmap(&iB[i * 114], &bursts[i * 116], |
| 1030 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1031 | |
| 1032 | for (i = 0; i < 4; i++) { |
| 1033 | for (j = 0, k = 0; j < 8; j++) |
| 1034 | k += abs(((int)gsm0503_pdtch_hl_hn_sbit[i][j]) - ((int)hl_hn[j])); |
| 1035 | |
| 1036 | if (i == 0 || k < best) { |
| 1037 | best = k; |
| 1038 | cs = i + 1; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | gsm0503_xcch_deinterleave(cB, iB); |
| 1043 | |
| 1044 | switch (cs) { |
| 1045 | case 1: |
| 1046 | osmo_conv_decode_ber(&gsm0503_xcch, cB, |
| 1047 | conv, n_errors, n_bits_total); |
| 1048 | |
Vadim Yanitskiy | c58e485 | 2023-12-08 03:35:50 +0700 | [diff] [blame] | 1049 | /* the three USF bits d(0),d(1),d(2) are *not* precoded */ |
| 1050 | if (usf_p) |
| 1051 | *usf_p = (conv[0] << 2) | (conv[1] << 1) | (conv[2] << 0); |
| 1052 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1053 | rv = osmo_crc64gen_check_bits(&gsm0503_fire_crc40, |
| 1054 | conv, 184, conv + 184); |
| 1055 | if (rv) |
| 1056 | return -1; |
| 1057 | |
| 1058 | osmo_ubit2pbit_ext(l2_data, 0, conv, 0, 184, 1); |
| 1059 | |
| 1060 | return 23; |
| 1061 | case 2: |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1062 | /* reorder, set punctured bits to 0 (unknown state) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1063 | for (i = 587, j = 455; i >= 0; i--) { |
| 1064 | if (!gsm0503_puncture_cs2[i]) |
| 1065 | cB[i] = cB[j--]; |
| 1066 | else |
| 1067 | cB[i] = 0; |
| 1068 | } |
| 1069 | |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1070 | /* decode as if puncturing was not employed (note '_np') */ |
Vadim Yanitskiy | 6c4be02 | 2024-01-31 20:59:04 +0700 | [diff] [blame] | 1071 | osmo_conv_decode_ber_punctured(&gsm0503_cs2_np, cB, conv, |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1072 | n_errors, NULL, |
Vadim Yanitskiy | 6c4be02 | 2024-01-31 20:59:04 +0700 | [diff] [blame] | 1073 | gsm0503_puncture_cs2); |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1074 | /* indicate the actual amount of coded bits (excluding punctured ones) */ |
| 1075 | if (n_bits_total != NULL) |
| 1076 | *n_bits_total = 456; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1077 | |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1078 | /* 5.1.2.2 a) the three USF bits d(0),d(1),d(2) are precoded into six bits */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1079 | for (i = 0; i < 8; i++) { |
| 1080 | for (j = 0, k = 0; j < 6; j++) |
| 1081 | k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j])); |
| 1082 | |
| 1083 | if (i == 0 || k < best) { |
| 1084 | best = k; |
| 1085 | usf = i; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | conv[3] = usf & 1; |
| 1090 | conv[4] = (usf >> 1) & 1; |
| 1091 | conv[5] = (usf >> 2) & 1; |
| 1092 | if (usf_p) |
| 1093 | *usf_p = usf; |
| 1094 | |
| 1095 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1096 | conv + 3, 271, conv + 3 + 271); |
| 1097 | if (rv) |
| 1098 | return -1; |
| 1099 | |
| 1100 | osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1); |
| 1101 | |
| 1102 | return 34; |
| 1103 | case 3: |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1104 | /* reorder, set punctured bits to 0 (unknown state) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1105 | for (i = 675, j = 455; i >= 0; i--) { |
| 1106 | if (!gsm0503_puncture_cs3[i]) |
| 1107 | cB[i] = cB[j--]; |
| 1108 | else |
| 1109 | cB[i] = 0; |
| 1110 | } |
| 1111 | |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1112 | /* decode as if puncturing was not employed (note '_np') */ |
Vadim Yanitskiy | 6c4be02 | 2024-01-31 20:59:04 +0700 | [diff] [blame] | 1113 | osmo_conv_decode_ber_punctured(&gsm0503_cs3_np, cB, conv, |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1114 | n_errors, NULL, |
Vadim Yanitskiy | 6c4be02 | 2024-01-31 20:59:04 +0700 | [diff] [blame] | 1115 | gsm0503_puncture_cs3); |
Vadim Yanitskiy | 3f4c6a8 | 2024-01-31 22:48:32 +0700 | [diff] [blame] | 1116 | /* indicate the actual amount of coded bits (excluding punctured ones) */ |
| 1117 | if (n_bits_total != NULL) |
| 1118 | *n_bits_total = 456; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1119 | |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1120 | /* 5.1.3.2 a) the three USF bits d(0),d(1),d(2) are precoded into six bits */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1121 | for (i = 0; i < 8; i++) { |
| 1122 | for (j = 0, k = 0; j < 6; j++) |
| 1123 | k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j])); |
| 1124 | |
| 1125 | if (i == 0 || k < best) { |
| 1126 | best = k; |
| 1127 | usf = i; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | conv[3] = usf & 1; |
| 1132 | conv[4] = (usf >> 1) & 1; |
| 1133 | conv[5] = (usf >> 2) & 1; |
| 1134 | if (usf_p) |
| 1135 | *usf_p = usf; |
| 1136 | |
| 1137 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1138 | conv + 3, 315, conv + 3 + 315); |
| 1139 | if (rv) |
| 1140 | return -1; |
| 1141 | |
| 1142 | osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1); |
| 1143 | |
| 1144 | return 40; |
| 1145 | case 4: |
| 1146 | for (i = 12; i < 456; i++) |
| 1147 | conv[i] = (cB[i] < 0) ? 1 : 0; |
| 1148 | |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1149 | /* 5.1.4.2 a) the three USF bits d(0),d(1),d(2) are precoded into twelve bits */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1150 | for (i = 0; i < 8; i++) { |
| 1151 | for (j = 0, k = 0; j < 12; j++) |
| 1152 | k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j])); |
| 1153 | |
| 1154 | if (i == 0 || k < best) { |
| 1155 | best = k; |
| 1156 | usf = i; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | conv[9] = usf & 1; |
| 1161 | conv[10] = (usf >> 1) & 1; |
| 1162 | conv[11] = (usf >> 2) & 1; |
| 1163 | if (usf_p) |
| 1164 | *usf_p = usf; |
| 1165 | |
| 1166 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1167 | conv + 9, 431, conv + 9 + 431); |
| 1168 | if (rv) { |
| 1169 | *n_bits_total = 456 - 12; |
| 1170 | *n_errors = *n_bits_total; |
| 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | *n_bits_total = 456 - 12; |
| 1175 | *n_errors = 0; |
| 1176 | |
| 1177 | osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1); |
| 1178 | |
| 1179 | return 54; |
| 1180 | default: |
| 1181 | *n_bits_total = 0; |
| 1182 | *n_errors = 0; |
| 1183 | break; |
| 1184 | } |
| 1185 | |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
Pau Espin Pedrol | f62f073 | 2020-04-07 13:12:11 +0200 | [diff] [blame] | 1190 | * EGPRS PDTCH DL block encoding |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1191 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1192 | static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1193 | { |
| 1194 | int i; |
| 1195 | ubit_t iB[456]; |
| 1196 | const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; |
| 1197 | |
Pau Espin Pedrol | 63ebc36 | 2020-04-07 13:15:36 +0200 | [diff] [blame] | 1198 | gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1199 | |
| 1200 | for (i = 0; i < 4; i++) { |
| 1201 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 1202 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1203 | } |
| 1204 | |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1208 | static int egprs_type2_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1209 | { |
| 1210 | int i; |
| 1211 | const ubit_t *up; |
| 1212 | ubit_t hi[EGPRS_HDR_HC_MAX]; |
| 1213 | ubit_t di[EGPRS_DATA_DC_MAX]; |
| 1214 | |
| 1215 | gsm0503_mcs5_dl_interleave(hc, dc, hi, di); |
| 1216 | up = gsm0503_mcs5_usf_precode_table[usf]; |
| 1217 | |
| 1218 | for (i = 0; i < 4; i++) { |
| 1219 | gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i); |
| 1220 | gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]); |
| 1221 | } |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1226 | static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc, |
| 1227 | const ubit_t *c1, const ubit_t *c2, int usf, int mcs) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1228 | { |
| 1229 | int i; |
| 1230 | const ubit_t *up; |
| 1231 | ubit_t hi[EGPRS_HDR_HC_MAX]; |
| 1232 | ubit_t di[EGPRS_DATA_C1 * 2]; |
| 1233 | |
| 1234 | if (mcs == EGPRS_MCS7) |
| 1235 | gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di); |
| 1236 | else |
| 1237 | gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di); |
| 1238 | |
| 1239 | up = gsm0503_mcs5_usf_precode_table[usf]; |
| 1240 | |
| 1241 | for (i = 0; i < 4; i++) { |
| 1242 | gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i); |
| 1243 | gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]); |
| 1244 | } |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1249 | static int egprs_encode_hdr(ubit_t *hc, const uint8_t *l2_data, int mcs) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1250 | { |
| 1251 | int i, j; |
| 1252 | ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX]; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 1253 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1254 | |
| 1255 | code = &gsm0503_mcs_dl_codes[mcs]; |
| 1256 | |
| 1257 | osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1); |
| 1258 | osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp, |
| 1259 | code->hdr_len, upp + code->hdr_len); |
| 1260 | |
| 1261 | osmo_conv_encode(code->hdr_conv, upp, C); |
| 1262 | |
| 1263 | /* MCS-5,6 header direct puncture instead of table */ |
| 1264 | if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) { |
| 1265 | memcpy(hc, C, code->hdr_code_len); |
| 1266 | hc[99] = hc[98]; |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | if (!code->hdr_punc) { |
| 1271 | /* Invalid MCS-X header puncture matrix */ |
| 1272 | return -1; |
| 1273 | } |
| 1274 | |
| 1275 | for (i = 0, j = 0; i < code->hdr_code_len; i++) { |
| 1276 | if (!code->hdr_punc[i]) |
| 1277 | hc[j++] = C[i]; |
| 1278 | } |
| 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1283 | static int egprs_encode_data(ubit_t *c, const uint8_t *l2_data, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1284 | int mcs, int p, int blk) |
| 1285 | { |
| 1286 | int i, j, data_len; |
| 1287 | ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX]; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 1288 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1289 | |
| 1290 | code = &gsm0503_mcs_dl_codes[mcs]; |
| 1291 | |
| 1292 | /* |
| 1293 | * Dual block - MCS-7,8,9 |
| 1294 | * Single block - MCS-1,2,3,4,5,6 |
| 1295 | */ |
| 1296 | if (mcs >= EGPRS_MCS7) |
| 1297 | data_len = code->data_len / 2; |
| 1298 | else |
| 1299 | data_len = code->data_len; |
| 1300 | |
| 1301 | osmo_pbit2ubit_ext(u, 0, l2_data, |
| 1302 | code->usf_len + code->hdr_len + blk * data_len, data_len, 1); |
| 1303 | |
| 1304 | osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len); |
| 1305 | |
| 1306 | osmo_conv_encode(code->data_conv, u, C); |
| 1307 | |
| 1308 | if (!code->data_punc[p]) { |
| 1309 | /* Invalid MCS-X data puncture matrix */ |
| 1310 | return -1; |
| 1311 | } |
| 1312 | |
| 1313 | for (i = 0, j = 0; i < code->data_code_len; i++) { |
| 1314 | if (!code->data_punc[p][i]) |
| 1315 | c[j++] = C[i]; |
| 1316 | } |
| 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | /* |
| 1322 | * Parse EGPRS DL header for coding and puncturing scheme (CPS) |
| 1323 | * |
| 1324 | * Type 1 - MCS-7,8,9 |
| 1325 | * Type 2 - MCS-5,6 |
| 1326 | * Type 3 - MCS-1,2,3,4 |
| 1327 | */ |
| 1328 | static int egprs_parse_dl_cps(struct egprs_cps *cps, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1329 | const union gprs_rlc_dl_hdr_egprs *hdr, int type) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1330 | { |
| 1331 | uint8_t bits; |
| 1332 | |
| 1333 | switch (type) { |
| 1334 | case EGPRS_HDR_TYPE1: |
| 1335 | bits = hdr->type1.cps; |
| 1336 | break; |
| 1337 | case EGPRS_HDR_TYPE2: |
| 1338 | bits = hdr->type2.cps; |
| 1339 | break; |
| 1340 | case EGPRS_HDR_TYPE3: |
| 1341 | bits = hdr->type3.cps; |
| 1342 | break; |
| 1343 | default: |
| 1344 | return -1; |
| 1345 | } |
| 1346 | |
| 1347 | return egprs_get_cps(cps, type, bits); |
| 1348 | } |
| 1349 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1350 | /*! EGPRS DL message encoding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1351 | * \param[out] bursts caller-allocated buffer for unpacked burst bits |
| 1352 | * \param[in] l2_data L2 (MAC) block to be encoded |
| 1353 | * \param[in] l2_len length of l2_data in bytes, used to determine MCS |
Vadim Yanitskiy | 8055cdd | 2020-03-30 18:16:38 +0700 | [diff] [blame] | 1354 | * \returns number of bits encoded; negative on error */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1355 | int gsm0503_pdtch_egprs_encode(ubit_t *bursts, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1356 | const uint8_t *l2_data, uint8_t l2_len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1357 | { |
| 1358 | ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX]; |
| 1359 | ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2]; |
| 1360 | uint8_t mcs; |
| 1361 | struct egprs_cps cps; |
| 1362 | union gprs_rlc_dl_hdr_egprs *hdr; |
| 1363 | |
| 1364 | switch (l2_len) { |
| 1365 | case 27: |
| 1366 | mcs = EGPRS_MCS1; |
| 1367 | break; |
| 1368 | case 33: |
| 1369 | mcs = EGPRS_MCS2; |
| 1370 | break; |
| 1371 | case 42: |
| 1372 | mcs = EGPRS_MCS3; |
| 1373 | break; |
| 1374 | case 49: |
| 1375 | mcs = EGPRS_MCS4; |
| 1376 | break; |
| 1377 | case 60: |
| 1378 | mcs = EGPRS_MCS5; |
| 1379 | break; |
| 1380 | case 78: |
| 1381 | mcs = EGPRS_MCS6; |
| 1382 | break; |
| 1383 | case 118: |
| 1384 | mcs = EGPRS_MCS7; |
| 1385 | break; |
| 1386 | case 142: |
| 1387 | mcs = EGPRS_MCS8; |
| 1388 | break; |
| 1389 | case 154: |
| 1390 | mcs = EGPRS_MCS9; |
| 1391 | break; |
| 1392 | default: |
| 1393 | return -1; |
| 1394 | } |
| 1395 | |
| 1396 | /* Read header for USF and puncturing matrix selection. */ |
| 1397 | hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data; |
| 1398 | |
| 1399 | switch (mcs) { |
| 1400 | case EGPRS_MCS1: |
| 1401 | case EGPRS_MCS2: |
| 1402 | case EGPRS_MCS3: |
| 1403 | case EGPRS_MCS4: |
| 1404 | /* Check for valid CPS and matching MCS to message size */ |
| 1405 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) || |
| 1406 | (cps.mcs != mcs)) |
| 1407 | goto bad_header; |
| 1408 | |
| 1409 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1410 | egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0); |
| 1411 | egprs_type3_map(bursts, hc, dc, hdr->type3.usf); |
| 1412 | break; |
| 1413 | case EGPRS_MCS5: |
| 1414 | case EGPRS_MCS6: |
| 1415 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) || |
| 1416 | (cps.mcs != mcs)) |
| 1417 | goto bad_header; |
| 1418 | |
| 1419 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1420 | egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0); |
| 1421 | egprs_type2_map(bursts, hc, dc, hdr->type2.usf); |
| 1422 | break; |
| 1423 | case EGPRS_MCS7: |
| 1424 | case EGPRS_MCS8: |
| 1425 | case EGPRS_MCS9: |
| 1426 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) || |
| 1427 | (cps.mcs != mcs)) |
| 1428 | goto bad_header; |
| 1429 | |
| 1430 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1431 | egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0); |
| 1432 | egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1); |
| 1433 | egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs); |
| 1434 | break; |
| 1435 | } |
| 1436 | |
| 1437 | return mcs >= EGPRS_MCS5 ? |
| 1438 | GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS; |
| 1439 | |
| 1440 | bad_header: |
| 1441 | /* Invalid EGPRS MCS-X header */ |
| 1442 | return -1; |
| 1443 | } |
| 1444 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1445 | /*! GPRS DL message encoding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1446 | * \param[out] bursts caller-allocated buffer for unpacked burst bits |
| 1447 | * \param[in] l2_data L2 (MAC) block to be encoded |
| 1448 | * \param[in] l2_len length of l2_data in bytes, used to determine CS |
Vadim Yanitskiy | 8055cdd | 2020-03-30 18:16:38 +0700 | [diff] [blame] | 1449 | * \returns number of bits encoded; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1450 | int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1451 | { |
| 1452 | ubit_t iB[456], cB[676]; |
| 1453 | const ubit_t *hl_hn; |
| 1454 | ubit_t conv[334]; |
| 1455 | int i, j, usf; |
| 1456 | |
| 1457 | switch (l2_len) { |
| 1458 | case 23: |
| 1459 | osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1); |
| 1460 | |
| 1461 | osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184); |
| 1462 | |
| 1463 | osmo_conv_encode(&gsm0503_xcch, conv, cB); |
| 1464 | |
| 1465 | hl_hn = gsm0503_pdtch_hl_hn_ubit[0]; |
| 1466 | |
| 1467 | break; |
| 1468 | case 34: |
| 1469 | osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1); |
| 1470 | usf = l2_data[0] & 0x7; |
| 1471 | |
| 1472 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3, |
| 1473 | 271, conv + 3 + 271); |
| 1474 | |
| 1475 | memcpy(conv, gsm0503_usf2six[usf], 6); |
| 1476 | |
| 1477 | osmo_conv_encode(&gsm0503_cs2_np, conv, cB); |
| 1478 | |
| 1479 | for (i = 0, j = 0; i < 588; i++) |
| 1480 | if (!gsm0503_puncture_cs2[i]) |
| 1481 | cB[j++] = cB[i]; |
| 1482 | |
| 1483 | hl_hn = gsm0503_pdtch_hl_hn_ubit[1]; |
| 1484 | |
| 1485 | break; |
| 1486 | case 40: |
| 1487 | osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1); |
| 1488 | usf = l2_data[0] & 0x7; |
| 1489 | |
| 1490 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3, |
| 1491 | 315, conv + 3 + 315); |
| 1492 | |
| 1493 | memcpy(conv, gsm0503_usf2six[usf], 6); |
| 1494 | |
| 1495 | osmo_conv_encode(&gsm0503_cs3_np, conv, cB); |
| 1496 | |
| 1497 | for (i = 0, j = 0; i < 676; i++) |
| 1498 | if (!gsm0503_puncture_cs3[i]) |
| 1499 | cB[j++] = cB[i]; |
| 1500 | |
| 1501 | hl_hn = gsm0503_pdtch_hl_hn_ubit[2]; |
| 1502 | |
| 1503 | break; |
| 1504 | case 54: |
| 1505 | osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1); |
| 1506 | usf = l2_data[0] & 0x7; |
| 1507 | |
| 1508 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9, |
| 1509 | 431, cB + 9 + 431); |
| 1510 | |
| 1511 | memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12); |
| 1512 | |
| 1513 | hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; |
| 1514 | |
| 1515 | break; |
| 1516 | default: |
| 1517 | return -1; |
| 1518 | } |
| 1519 | |
| 1520 | gsm0503_xcch_interleave(cB, iB); |
| 1521 | |
| 1522 | for (i = 0; i < 4; i++) { |
| 1523 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 1524 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1525 | } |
| 1526 | |
| 1527 | return GSM0503_GPRS_BURSTS_NBITS; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * GSM TCH/F FR/EFR transcoding |
| 1532 | */ |
| 1533 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1534 | /*! assemble a FR codec frame in format as used inside RTP |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1535 | * \param[out] tch_data Codec frame in RTP format |
| 1536 | * \param[in] b_bits Codec frame in 'native' format |
| 1537 | * \param[in] net_order FIXME */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1538 | static void tch_fr_reassemble(uint8_t *tch_data, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1539 | const ubit_t *b_bits, int net_order) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1540 | { |
| 1541 | int i, j, k, l, o; |
| 1542 | |
| 1543 | tch_data[0] = 0xd << 4; |
| 1544 | memset(tch_data + 1, 0, 32); |
| 1545 | |
| 1546 | if (net_order) { |
| 1547 | for (i = 0, j = 4; i < 260; i++, j++) |
| 1548 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1549 | |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | /* reassemble d-bits */ |
| 1554 | i = 0; /* counts bits */ |
| 1555 | j = 4; /* counts output bits */ |
| 1556 | k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */ |
| 1557 | l = 0; /* counts element bits */ |
| 1558 | o = 0; /* offset input bits */ |
| 1559 | while (i < 260) { |
| 1560 | tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7))); |
| 1561 | if (--k < 0) { |
| 1562 | o += gsm0503_gsm_fr_map[l]; |
| 1563 | k = gsm0503_gsm_fr_map[++l]-1; |
| 1564 | } |
| 1565 | i++; |
| 1566 | j++; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | static void tch_fr_disassemble(ubit_t *b_bits, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1571 | const uint8_t *tch_data, int net_order) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1572 | { |
| 1573 | int i, j, k, l, o; |
| 1574 | |
| 1575 | if (net_order) { |
| 1576 | for (i = 0, j = 4; i < 260; i++, j++) |
| 1577 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1578 | |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | i = 0; /* counts bits */ |
| 1583 | j = 4; /* counts input bits */ |
| 1584 | k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */ |
| 1585 | l = 0; /* counts element bits */ |
| 1586 | o = 0; /* offset output bits */ |
| 1587 | while (i < 260) { |
| 1588 | b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1589 | if (--k < 0) { |
| 1590 | o += gsm0503_gsm_fr_map[l]; |
| 1591 | k = gsm0503_gsm_fr_map[++l] - 1; |
| 1592 | } |
| 1593 | i++; |
| 1594 | j++; |
| 1595 | } |
| 1596 | } |
| 1597 | |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 1598 | /* assemble a HR codec frame in the canonical format of ETSI TS 101 318 */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1599 | static void tch_hr_reassemble(uint8_t *tch_data, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1600 | { |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 1601 | int i; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1602 | |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 1603 | memset(tch_data, 0, GSM_HR_BYTES); |
| 1604 | for (i = 0; i < 112; i++) |
| 1605 | tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7))); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1608 | static void tch_hr_disassemble(ubit_t *b_bits, const uint8_t *tch_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1609 | { |
Mychaela N. Falconia | 8d8d5af | 2023-05-23 16:41:05 +0000 | [diff] [blame] | 1610 | int i; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1611 | |
Mychaela N. Falconia | 8d8d5af | 2023-05-23 16:41:05 +0000 | [diff] [blame] | 1612 | for (i = 0; i < 112; i++) |
| 1613 | b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1616 | /* assemble a EFR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1617 | static void tch_efr_reassemble(uint8_t *tch_data, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1618 | { |
| 1619 | int i, j; |
| 1620 | |
| 1621 | tch_data[0] = 0xc << 4; |
| 1622 | memset(tch_data + 1, 0, 30); |
| 1623 | |
| 1624 | for (i = 0, j = 4; i < 244; i++, j++) |
| 1625 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1626 | } |
| 1627 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1628 | static void tch_efr_disassemble(ubit_t *b_bits, const uint8_t *tch_data) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1629 | { |
| 1630 | int i, j; |
| 1631 | |
| 1632 | for (i = 0, j = 4; i < 244; i++, j++) |
| 1633 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1634 | } |
| 1635 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1636 | /* assemble a AMR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1637 | static void tch_amr_reassemble(uint8_t *tch_data, const ubit_t *d_bits, int len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1638 | { |
| 1639 | int i, j; |
| 1640 | |
| 1641 | memset(tch_data, 0, (len + 7) >> 3); |
| 1642 | |
| 1643 | for (i = 0, j = 0; i < len; i++, j++) |
| 1644 | tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7))); |
| 1645 | } |
| 1646 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1647 | static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1648 | { |
| 1649 | int i, j; |
| 1650 | |
| 1651 | for (i = 0, j = 0; i < len; i++, j++) |
| 1652 | d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1653 | } |
| 1654 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 1655 | /* Append STI and MI bits to the SID_UPDATE frame, see also |
| 1656 | * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */ |
| 1657 | static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi) |
| 1658 | { |
| 1659 | /* Zero out the space that had been used by the CRC14 */ |
| 1660 | memset(sid_update + 35, 0, 14); |
| 1661 | |
| 1662 | /* Append STI and MI parameters */ |
| 1663 | sid_update[35] = sti & 1; |
| 1664 | sid_update[36] = mi & 1; |
| 1665 | sid_update[37] = mi >> 1 & 1; |
| 1666 | sid_update[38] = mi >> 2 & 1; |
| 1667 | } |
| 1668 | |
| 1669 | /* Extract a SID UPDATE fram the sbits of an FR AMR frame */ |
| 1670 | static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits) |
| 1671 | { |
| 1672 | |
| 1673 | unsigned int i; |
| 1674 | |
| 1675 | sbits += 32; |
| 1676 | |
| 1677 | for (i = 0; i < 53; i++) { |
| 1678 | sid_update[0] = sbits[0]; |
| 1679 | sid_update[1] = sbits[1]; |
| 1680 | sid_update[2] = sbits[2]; |
| 1681 | sid_update[3] = sbits[3]; |
| 1682 | sid_update += 4; |
| 1683 | sbits += 8; |
| 1684 | } |
| 1685 | |
| 1686 | } |
| 1687 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1688 | /* re-arrange according to TS 05.03 Table 2 (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1689 | static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1690 | { |
| 1691 | int i; |
| 1692 | |
| 1693 | for (i = 0; i < 260; i++) |
| 1694 | b_bits[gsm610_bitorder[i]] = d_bits[i]; |
| 1695 | } |
| 1696 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1697 | /* re-arrange according to TS 05.03 Table 2 (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1698 | static void tch_fr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1699 | { |
| 1700 | int i; |
| 1701 | |
| 1702 | for (i = 0; i < 260; i++) |
| 1703 | d_bits[i] = b_bits[gsm610_bitorder[i]]; |
| 1704 | } |
| 1705 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1706 | /* re-arrange according to TS 05.03 Table 3a (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1707 | static void tch_hr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1708 | { |
| 1709 | int i; |
| 1710 | |
| 1711 | const uint16_t *map; |
| 1712 | |
| 1713 | if (!d_bits[93] && !d_bits[94]) |
| 1714 | map = gsm620_unvoiced_bitorder; |
| 1715 | else |
| 1716 | map = gsm620_voiced_bitorder; |
| 1717 | |
| 1718 | for (i = 0; i < 112; i++) |
| 1719 | b_bits[map[i]] = d_bits[i]; |
| 1720 | } |
| 1721 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1722 | /* re-arrange according to TS 05.03 Table 3a (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1723 | static void tch_hr_b_to_d(ubit_t *d_bits, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1724 | { |
| 1725 | int i; |
| 1726 | const uint16_t *map; |
| 1727 | |
| 1728 | if (!b_bits[34] && !b_bits[35]) |
| 1729 | map = gsm620_unvoiced_bitorder; |
| 1730 | else |
| 1731 | map = gsm620_voiced_bitorder; |
| 1732 | |
| 1733 | for (i = 0; i < 112; i++) |
| 1734 | d_bits[i] = b_bits[map[i]]; |
| 1735 | } |
| 1736 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1737 | /* re-arrange according to TS 05.03 Table 6 (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1738 | static void tch_efr_d_to_w(ubit_t *b_bits, const ubit_t *d_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1739 | { |
| 1740 | int i; |
| 1741 | |
| 1742 | for (i = 0; i < 260; i++) |
| 1743 | b_bits[gsm660_bitorder[i]] = d_bits[i]; |
| 1744 | } |
| 1745 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1746 | /* re-arrange according to TS 05.03 Table 6 (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1747 | static void tch_efr_w_to_d(ubit_t *d_bits, const ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1748 | { |
| 1749 | int i; |
| 1750 | |
| 1751 | for (i = 0; i < 260; i++) |
| 1752 | d_bits[i] = b_bits[gsm660_bitorder[i]]; |
| 1753 | } |
| 1754 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1755 | /* extract the 65 protected class1a+1b bits */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1756 | static void tch_efr_protected(const ubit_t *s_bits, ubit_t *b_bits) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1757 | { |
| 1758 | int i; |
| 1759 | |
| 1760 | for (i = 0; i < 65; i++) |
| 1761 | b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1]; |
| 1762 | } |
| 1763 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1764 | static void tch_fr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1765 | { |
| 1766 | int i; |
| 1767 | |
| 1768 | for (i = 0; i < 91; i++) { |
| 1769 | d[i << 1] = u[i]; |
| 1770 | d[(i << 1) + 1] = u[184 - i]; |
| 1771 | } |
| 1772 | |
| 1773 | for (i = 0; i < 3; i++) |
| 1774 | p[i] = u[91 + i]; |
| 1775 | } |
| 1776 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1777 | static void tch_fr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1778 | { |
| 1779 | int i; |
| 1780 | |
| 1781 | for (i = 0; i < 91; i++) { |
| 1782 | u[i] = d[i << 1]; |
| 1783 | u[184 - i] = d[(i << 1) + 1]; |
| 1784 | } |
| 1785 | |
| 1786 | for (i = 0; i < 3; i++) |
| 1787 | u[91 + i] = p[i]; |
| 1788 | } |
| 1789 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1790 | static void tch_hr_unreorder(ubit_t *d, ubit_t *p, const ubit_t *u) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1791 | { |
| 1792 | memcpy(d, u, 95); |
| 1793 | memcpy(p, u + 95, 3); |
| 1794 | } |
| 1795 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1796 | static void tch_hr_reorder(ubit_t *u, const ubit_t *d, const ubit_t *p) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1797 | { |
| 1798 | memcpy(u, d, 95); |
| 1799 | memcpy(u + 95, p, 3); |
| 1800 | } |
| 1801 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1802 | static void tch_efr_reorder(ubit_t *w, const ubit_t *s, const ubit_t *p) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1803 | { |
| 1804 | memcpy(w, s, 71); |
| 1805 | w[71] = w[72] = s[69]; |
| 1806 | memcpy(w + 73, s + 71, 50); |
| 1807 | w[123] = w[124] = s[119]; |
| 1808 | memcpy(w + 125, s + 121, 53); |
| 1809 | w[178] = w[179] = s[172]; |
| 1810 | memcpy(w + 180, s + 174, 50); |
| 1811 | w[230] = w[231] = s[222]; |
| 1812 | memcpy(w + 232, s + 224, 20); |
| 1813 | memcpy(w + 252, p, 8); |
| 1814 | } |
| 1815 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1816 | static void tch_efr_unreorder(ubit_t *s, ubit_t *p, const ubit_t *w) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1817 | { |
| 1818 | int sum; |
| 1819 | |
| 1820 | memcpy(s, w, 71); |
| 1821 | sum = s[69] + w[71] + w[72]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1822 | s[69] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1823 | memcpy(s + 71, w + 73, 50); |
| 1824 | sum = s[119] + w[123] + w[124]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1825 | s[119] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1826 | memcpy(s + 121, w + 125, 53); |
| 1827 | sum = s[172] + w[178] + w[179]; |
Mychaela N. Falconia | f3e4745 | 2023-05-17 18:58:40 +0000 | [diff] [blame] | 1828 | s[172] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1829 | memcpy(s + 174, w + 180, 50); |
Niro Mahasinghe | 834e2ac | 2017-11-03 12:22:34 +0100 | [diff] [blame] | 1830 | sum = s[222] + w[230] + w[231]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1831 | s[222] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1832 | memcpy(s + 224, w + 232, 20); |
| 1833 | memcpy(p, w + 252, 8); |
| 1834 | } |
| 1835 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1836 | static void tch_amr_merge(ubit_t *u, const ubit_t *d, const ubit_t *p, int len, int prot) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1837 | { |
| 1838 | memcpy(u, d, prot); |
| 1839 | memcpy(u + prot, p, 6); |
| 1840 | memcpy(u + prot + 6, d + prot, len - prot); |
| 1841 | } |
| 1842 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1843 | static void tch_amr_unmerge(ubit_t *d, ubit_t *p, const ubit_t *u, int len, int prot) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1844 | { |
| 1845 | memcpy(d, u, prot); |
| 1846 | memcpy(p, u + prot, 6); |
| 1847 | memcpy(d + prot, u + prot + 6, len - prot); |
| 1848 | } |
| 1849 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1850 | /*! Perform channel decoding of a FR/EFR channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1851 | * \param[out] tch_data Codec frame in RTP payload format |
| 1852 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 1853 | * \param[in] net_order FIXME |
| 1854 | * \param[in] efr Is this channel using EFR (1) or FR (0) |
| 1855 | * \param[out] n_errors Number of detected bit errors |
| 1856 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 1857 | * \returns length of bytes used in \a tch_data output buffer; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1858 | int gsm0503_tch_fr_decode(uint8_t *tch_data, const sbit_t *bursts, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1859 | int net_order, int efr, int *n_errors, int *n_bits_total) |
| 1860 | { |
| 1861 | sbit_t iB[912], cB[456], h; |
| 1862 | ubit_t conv[185], s[244], w[260], b[65], d[260], p[8]; |
| 1863 | int i, rv, len, steal = 0; |
| 1864 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1865 | /* map from 8 bursts to interleaved data bits (iB) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1866 | for (i = 0; i < 8; i++) { |
| 1867 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 1868 | &bursts[i * 116], &h, i >> 2); |
| 1869 | steal -= h; |
| 1870 | } |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1871 | /* we now have the bits of the four bursts (interface 4 in |
| 1872 | * Figure 1a of TS 05.03 */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1873 | |
| 1874 | gsm0503_tch_fr_deinterleave(cB, iB); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1875 | /* we now have the coded bits c(B): interface 3 in Fig. 1a */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1876 | |
| 1877 | if (steal > 0) { |
| 1878 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 1879 | if (rv) { |
| 1880 | /* Error decoding FACCH frame */ |
| 1881 | return -1; |
| 1882 | } |
| 1883 | |
Vadim Yanitskiy | c0305b6 | 2023-05-20 16:33:38 +0700 | [diff] [blame] | 1884 | return GSM_MACBLOCK_LEN; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | osmo_conv_decode_ber(&gsm0503_tch_fr, cB, conv, n_errors, n_bits_total); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1888 | /* we now have the data bits 'u': interface 2 in Fig. 1a */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1889 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1890 | /* input: 'conv', output: d[ata] + p[arity] */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1891 | tch_fr_unreorder(d, p, conv); |
| 1892 | |
| 1893 | for (i = 0; i < 78; i++) |
| 1894 | d[i + 182] = (cB[i + 378] < 0) ? 1 : 0; |
| 1895 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1896 | /* check if parity of first 50 (class 1) 'd'-bits match 'p' */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1897 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p); |
| 1898 | if (rv) { |
| 1899 | /* Error checking CRC8 for the FR part of an EFR/FR frame */ |
| 1900 | return -1; |
| 1901 | } |
| 1902 | |
| 1903 | if (efr) { |
| 1904 | tch_efr_d_to_w(w, d); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1905 | /* we now have the preliminary-coded bits w(k) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1906 | |
| 1907 | tch_efr_unreorder(s, p, w); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1908 | /* we now have the data delivered to the preliminary |
| 1909 | * channel encoding unit s(k) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1910 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1911 | /* extract the 65 most important bits according TS 05.03 3.1.1.1 */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1912 | tch_efr_protected(s, b); |
| 1913 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1914 | /* perform CRC-8 on 65 most important bits (50 bits of |
| 1915 | * class 1a + 15 bits of class 1b) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1916 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p); |
| 1917 | if (rv) { |
| 1918 | /* Error checking CRC8 for the EFR part of an EFR frame */ |
| 1919 | return -1; |
| 1920 | } |
| 1921 | |
| 1922 | tch_efr_reassemble(tch_data, s); |
| 1923 | |
| 1924 | len = GSM_EFR_BYTES; |
| 1925 | } else { |
| 1926 | tch_fr_d_to_b(w, d); |
| 1927 | |
| 1928 | tch_fr_reassemble(tch_data, w, net_order); |
| 1929 | |
| 1930 | len = GSM_FR_BYTES; |
| 1931 | } |
| 1932 | |
| 1933 | return len; |
| 1934 | } |
| 1935 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1936 | /*! Perform channel encoding on a TCH/FS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1937 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 1938 | * \param[in] tch_data Codec input data in RTP payload format |
| 1939 | * \param[in] len Length of \a tch_data in bytes |
| 1940 | * \param[in] net_order FIXME |
| 1941 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1942 | int gsm0503_tch_fr_encode(ubit_t *bursts, const uint8_t *tch_data, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1943 | int len, int net_order) |
| 1944 | { |
| 1945 | ubit_t iB[912], cB[456], h; |
| 1946 | ubit_t conv[185], w[260], b[65], s[244], d[260], p[8]; |
| 1947 | int i; |
| 1948 | |
| 1949 | switch (len) { |
| 1950 | case GSM_EFR_BYTES: /* TCH EFR */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1951 | tch_efr_disassemble(s, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1952 | tch_efr_protected(s, b); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1953 | osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1954 | tch_efr_reorder(w, s, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1955 | tch_efr_w_to_d(d, w); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1956 | goto coding_efr_fr; |
| 1957 | case GSM_FR_BYTES: /* TCH FR */ |
| 1958 | tch_fr_disassemble(w, tch_data, net_order); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1959 | tch_fr_b_to_d(d, w); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1960 | coding_efr_fr: |
| 1961 | osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1962 | tch_fr_reorder(conv, d, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1963 | memcpy(cB + 378, d + 182, 78); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1964 | osmo_conv_encode(&gsm0503_tch_fr, conv, cB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1965 | h = 0; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1966 | break; |
Mychaela N. Falconia | 6e529a2 | 2023-05-27 11:06:57 +0000 | [diff] [blame] | 1967 | case 0: /* no data, induce BFI in the receiver */ |
| 1968 | /* Do the same thing that sysmoBTS PHY does when fed a 0-length |
| 1969 | * payload for DL: set all u(k) bits to 0, and do the same |
| 1970 | * with all class 2 bits. This operation is NOT the same as |
| 1971 | * an FR codec frame of all zero bits: with all-zeros d(k) input |
| 1972 | * the CRC3 function will produce 111 output, whereas we |
| 1973 | * transmit 000 in those parity bits too. The result will be |
| 1974 | * an induced BFI (bad frame indication) condition in the |
| 1975 | * receiver, for both TCH/FS and TCH/EFS decoders. */ |
| 1976 | memset(conv, 0, sizeof(conv)); |
| 1977 | memset(cB + 378, 0, 78); |
| 1978 | osmo_conv_encode(&gsm0503_tch_fr, conv, cB); |
| 1979 | h = 0; |
| 1980 | break; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1981 | case GSM_MACBLOCK_LEN: /* FACCH */ |
| 1982 | _xcch_encode_cB(cB, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1983 | h = 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1984 | break; |
| 1985 | default: |
| 1986 | return -1; |
| 1987 | } |
| 1988 | |
| 1989 | gsm0503_tch_fr_interleave(cB, iB); |
| 1990 | |
| 1991 | for (i = 0; i < 8; i++) { |
| 1992 | gsm0503_tch_burst_map(&iB[i * 114], |
| 1993 | &bursts[i * 116], &h, i >> 2); |
| 1994 | } |
| 1995 | |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1999 | /*! Perform channel decoding of a HR(v1) channel according TS 05.03 |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 2000 | * \param[out] tch_data Codec frame in TS 101 318 canonical format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2001 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2002 | * \param[in] odd Odd (1) or even (0) frame number |
| 2003 | * \param[out] n_errors Number of detected bit errors |
| 2004 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2005 | * \returns length of bytes used in \a tch_data output buffer; negative on error */ |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 2006 | int gsm0503_tch_hr_decode2(uint8_t *tch_data, const sbit_t *bursts, int odd, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2007 | int *n_errors, int *n_bits_total) |
| 2008 | { |
| 2009 | sbit_t iB[912], cB[456], h; |
| 2010 | ubit_t conv[98], b[112], d[112], p[3]; |
| 2011 | int i, rv, steal = 0; |
| 2012 | |
| 2013 | /* Only unmap the stealing bits */ |
| 2014 | if (!odd) { |
| 2015 | for (i = 0; i < 4; i++) { |
| 2016 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0); |
| 2017 | steal -= h; |
| 2018 | } |
| 2019 | |
Mychaela N. Falconia | 920c0ed | 2023-05-23 14:06:53 +0000 | [diff] [blame] | 2020 | for (i = 2; i < 6; i++) { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2021 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1); |
| 2022 | steal -= h; |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | /* If we found a stole FACCH, but only at correct alignment */ |
| 2027 | if (steal > 0) { |
| 2028 | for (i = 0; i < 6; i++) { |
| 2029 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2030 | &bursts[i * 116], NULL, i >> 2); |
| 2031 | } |
| 2032 | |
| 2033 | for (i = 2; i < 4; i++) { |
| 2034 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], |
| 2035 | &bursts[i * 116], NULL, 1); |
| 2036 | } |
| 2037 | |
| 2038 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2039 | |
| 2040 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2041 | if (rv) { |
| 2042 | /* Error decoding FACCH frame */ |
| 2043 | return -1; |
| 2044 | } |
| 2045 | |
| 2046 | return GSM_MACBLOCK_LEN; |
| 2047 | } |
| 2048 | |
| 2049 | for (i = 0; i < 4; i++) { |
| 2050 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2051 | &bursts[i * 116], NULL, i >> 1); |
| 2052 | } |
| 2053 | |
| 2054 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2055 | |
| 2056 | osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total); |
| 2057 | |
| 2058 | tch_hr_unreorder(d, p, conv); |
| 2059 | |
| 2060 | for (i = 0; i < 17; i++) |
| 2061 | d[i + 95] = (cB[i + 211] < 0) ? 1 : 0; |
| 2062 | |
| 2063 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p); |
| 2064 | if (rv) { |
| 2065 | /* Error checking CRC8 for an HR frame */ |
| 2066 | return -1; |
| 2067 | } |
| 2068 | |
| 2069 | tch_hr_d_to_b(b, d); |
| 2070 | |
| 2071 | tch_hr_reassemble(tch_data, b); |
| 2072 | |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 2073 | return GSM_HR_BYTES; |
| 2074 | } |
| 2075 | |
| 2076 | /*! Perform channel decoding of a HR(v1) channel according TS 05.03, |
| 2077 | * deprecated legacy API. |
| 2078 | * \param[out] tch_data Codec frame in pseudo-RFC5993 format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2079 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 2080 | * \param[in] odd Odd (1) or even (0) frame number |
| 2081 | * \param[out] n_errors Number of detected bit errors |
| 2082 | * \param[out] n_bits_total Total number of bits |
| 2083 | * \returns length of bytes used in \a tch_data output buffer; negative on error |
| 2084 | * |
| 2085 | * The HR1 codec frame format returned by this function is pseudo-RFC5993, |
| 2086 | * not true RFC 5993, as there is no SID classification being done |
| 2087 | * and the FT bits in the ToC octet are always set to 0 - but this |
| 2088 | * arguably-bogus format is the legacy public API. |
| 2089 | */ |
| 2090 | int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, |
| 2091 | int *n_errors, int *n_bits_total) |
| 2092 | { |
| 2093 | int rc; |
| 2094 | |
| 2095 | rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors, |
| 2096 | n_bits_total); |
| 2097 | if (rc != GSM_HR_BYTES) |
| 2098 | return rc; |
| 2099 | memmove(tch_data + 1, tch_data, GSM_HR_BYTES); |
| 2100 | tch_data[0] = 0x00; /* FT=0, note absence of SID classification */ |
| 2101 | return GSM_HR_BYTES_RTP_RFC5993; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2102 | } |
| 2103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2104 | /*! Perform channel encoding on a TCH/HS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2105 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2106 | * \param[in] tch_data Codec input data in RTP payload format |
| 2107 | * \param[in] len Length of \a tch_data in bytes |
| 2108 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2109 | int gsm0503_tch_hr_encode(ubit_t *bursts, const uint8_t *tch_data, int len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2110 | { |
| 2111 | ubit_t iB[912], cB[456], h; |
| 2112 | ubit_t conv[98], b[112], d[112], p[3]; |
| 2113 | int i; |
| 2114 | |
| 2115 | switch (len) { |
Mychaela N. Falconia | 8d8d5af | 2023-05-23 16:41:05 +0000 | [diff] [blame] | 2116 | case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */ |
| 2117 | tch_data++; |
| 2118 | /* fall-through */ |
| 2119 | case GSM_HR_BYTES: /* TCH HR in "pure" form */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2120 | tch_hr_disassemble(b, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2121 | tch_hr_b_to_d(d, b); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2122 | osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2123 | tch_hr_reorder(conv, d, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2124 | memcpy(cB + 211, d + 95, 17); |
Mychaela N. Falconia | 6e529a2 | 2023-05-27 11:06:57 +0000 | [diff] [blame] | 2125 | hr_conv_coding: |
| 2126 | osmo_conv_encode(&gsm0503_tch_hr, conv, cB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2127 | h = 0; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2128 | gsm0503_tch_hr_interleave(cB, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2129 | for (i = 0; i < 4; i++) { |
| 2130 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2131 | &bursts[i * 116], &h, i >> 1); |
| 2132 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2133 | break; |
Mychaela N. Falconia | 6e529a2 | 2023-05-27 11:06:57 +0000 | [diff] [blame] | 2134 | case 0: /* no data, induce BFI in the receiver */ |
| 2135 | /* see comments in gsm0503_tch_fr_encode() - same deal here */ |
| 2136 | memset(conv, 0, sizeof(conv)); |
| 2137 | memset(cB + 211, 0, 17); |
| 2138 | goto hr_conv_coding; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2139 | case GSM_MACBLOCK_LEN: /* FACCH */ |
| 2140 | _xcch_encode_cB(cB, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2141 | h = 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2142 | gsm0503_tch_fr_interleave(cB, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2143 | for (i = 0; i < 6; i++) { |
| 2144 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2145 | &bursts[i * 116], &h, i >> 2); |
| 2146 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2147 | for (i = 2; i < 4; i++) { |
| 2148 | gsm0503_tch_burst_map(&iB[i * 114 + 456], |
| 2149 | &bursts[i * 116], &h, 1); |
| 2150 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2151 | break; |
| 2152 | default: |
| 2153 | return -1; |
| 2154 | } |
| 2155 | |
| 2156 | return 0; |
| 2157 | } |
| 2158 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2159 | /* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */ |
| 2160 | static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB) |
| 2161 | { |
| 2162 | unsigned int id = 0, best = 0; |
| 2163 | unsigned int i, j, k; |
| 2164 | |
| 2165 | for (i = 0; i < 4; i++) { |
| 2166 | /* FIXME: why not using remaining (16 - 8) soft-bits here? */ |
| 2167 | for (j = 0, k = 0; j < 8; j++) |
| 2168 | k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j])); |
| 2169 | |
| 2170 | if (i == 0 || k < best) { |
| 2171 | best = k; |
| 2172 | id = i; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | return id; |
| 2177 | } |
| 2178 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2179 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2180 | * \param[out] tch_data Codec frame in RTP payload format |
| 2181 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2182 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2183 | * \param[in] codec array of active codecs (active codec set) |
| 2184 | * \param[in] codecs number of codecs in \a codec |
| 2185 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2186 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2187 | * \param[out] n_errors Number of detected bit errors |
| 2188 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2189 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2190 | * codec out of range; negative on error |
| 2191 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2192 | int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2193 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2194 | uint8_t *cmr, int *n_errors, int *n_bits_total) |
| 2195 | { |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2196 | return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req, |
| 2197 | codec, codecs, ft, cmr, n_errors, |
| 2198 | n_bits_total, NULL); |
| 2199 | } |
| 2200 | |
| 2201 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
| 2202 | * \param[out] tch_data Codec frame in RTP payload format |
| 2203 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2204 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2205 | * \param[in] codec array of active codecs (active codec set) |
| 2206 | * \param[in] codecs number of codecs in \a codec |
| 2207 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2208 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2209 | * \param[out] n_errors Number of detected bit errors |
| 2210 | * \param[out] n_bits_total Total number of bits |
| 2211 | * \param[inout] dtx DTX frame type output, previous DTX frame type input |
| 2212 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2213 | * codec out of range; negative on error |
| 2214 | */ |
| 2215 | int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, |
| 2216 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2217 | uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) |
| 2218 | { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2219 | sbit_t iB[912], cB[456], h; |
| 2220 | ubit_t d[244], p[6], conv[250]; |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2221 | int i, rv, len, steal = 0, id = -1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2222 | *n_errors = 0; *n_bits_total = 0; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2223 | static ubit_t sid_first_dummy[64] = { 0 }; |
| 2224 | sbit_t sid_update_enc[256]; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2225 | |
| 2226 | for (i=0; i<8; i++) { |
| 2227 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 2228 | steal -= h; |
| 2229 | } |
| 2230 | |
| 2231 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2232 | |
| 2233 | if (steal > 0) { |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2234 | /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame. |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2235 | * It's normally updated by gsm0503_detect_afs_dtx_frame2(), which is not |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2236 | * reached in case of FACCH. Reset it here to avoid FACCH/F frames being |
| 2237 | * misinterpreted as AMR's special DTX frames. */ |
| 2238 | if (dtx != NULL) |
| 2239 | *dtx = AMR_OTHER; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2240 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2241 | if (rv) { |
| 2242 | /* Error decoding FACCH frame */ |
| 2243 | return -1; |
| 2244 | } |
| 2245 | |
| 2246 | return GSM_MACBLOCK_LEN; |
| 2247 | } |
| 2248 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2249 | /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ |
| 2250 | if (dtx) { |
Vadim Yanitskiy | bf2d5e9 | 2022-05-15 15:58:10 +0300 | [diff] [blame] | 2251 | const enum gsm0503_amr_dtx_frames dtx_prev = *dtx; |
| 2252 | |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2253 | *dtx = gsm0503_detect_afs_dtx_frame2(n_errors, n_bits_total, &id, cB); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2254 | |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2255 | switch (*dtx) { |
| 2256 | case AMR_OTHER: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2257 | /* NOTE: The AFS_SID_UPDATE frame is splitted into |
| 2258 | * two half rate frames. If the id marker frame |
| 2259 | * (AFS_SID_UPDATE) is detected the following frame |
| 2260 | * contains the actual comfort noised data part of |
| 2261 | * (AFS_SID_UPDATE_CN). */ |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2262 | if (dtx_prev != AFS_SID_UPDATE) |
| 2263 | break; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2264 | /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2265 | *dtx = AFS_SID_UPDATE_CN; |
| 2266 | |
| 2267 | extract_afs_sid_update(sid_update_enc, cB); |
| 2268 | osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, |
| 2269 | sid_update_enc, conv, n_errors, |
| 2270 | n_bits_total); |
| 2271 | rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, |
| 2272 | 35, conv + 35); |
| 2273 | if (rv != 0) { |
| 2274 | /* Error checking CRC14 for an AMR SID_UPDATE frame */ |
| 2275 | return -1; |
| 2276 | } |
| 2277 | |
| 2278 | tch_amr_sid_update_append(conv, 1, |
| 2279 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2280 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2281 | tch_amr_reassemble(tch_data, conv, 39); |
| 2282 | len = 5; |
| 2283 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2284 | case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2285 | tch_amr_sid_update_append(sid_first_dummy, 0, |
| 2286 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2287 | : codec[id > 0 ? id : 0]); |
Vadim Yanitskiy | 7d121cb | 2023-06-23 16:57:48 +0700 | [diff] [blame] | 2288 | tch_amr_reassemble(tch_data, sid_first_dummy, 39); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2289 | len = 5; |
| 2290 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2291 | case AFS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2292 | case AFS_ONSET: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2293 | len = 0; |
| 2294 | goto out; |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2295 | default: |
| 2296 | break; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2300 | /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */ |
| 2301 | if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2302 | /* Codec mode out of range, return id */ |
| 2303 | return id; |
| 2304 | } |
| 2305 | |
| 2306 | switch ((codec_mode_req) ? codec[*ft] : codec[id]) { |
| 2307 | case 7: /* TCH/AFS12.2 */ |
| 2308 | osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8, |
| 2309 | conv, n_errors, n_bits_total); |
| 2310 | |
| 2311 | tch_amr_unmerge(d, p, conv, 244, 81); |
| 2312 | |
| 2313 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p); |
| 2314 | if (rv) { |
| 2315 | /* Error checking CRC8 for an AMR 12.2 frame */ |
| 2316 | return -1; |
| 2317 | } |
| 2318 | |
| 2319 | tch_amr_reassemble(tch_data, d, 244); |
| 2320 | |
| 2321 | len = 31; |
| 2322 | |
| 2323 | break; |
| 2324 | case 6: /* TCH/AFS10.2 */ |
| 2325 | osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8, |
| 2326 | conv, n_errors, n_bits_total); |
| 2327 | |
| 2328 | tch_amr_unmerge(d, p, conv, 204, 65); |
| 2329 | |
| 2330 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p); |
| 2331 | if (rv) { |
| 2332 | /* Error checking CRC8 for an AMR 10.2 frame */ |
| 2333 | return -1; |
| 2334 | } |
| 2335 | |
| 2336 | tch_amr_reassemble(tch_data, d, 204); |
| 2337 | |
| 2338 | len = 26; |
| 2339 | |
| 2340 | break; |
| 2341 | case 5: /* TCH/AFS7.95 */ |
| 2342 | osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8, |
| 2343 | conv, n_errors, n_bits_total); |
| 2344 | |
| 2345 | tch_amr_unmerge(d, p, conv, 159, 75); |
| 2346 | |
| 2347 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p); |
| 2348 | if (rv) { |
| 2349 | /* Error checking CRC8 for an AMR 7.95 frame */ |
| 2350 | return -1; |
| 2351 | } |
| 2352 | |
| 2353 | tch_amr_reassemble(tch_data, d, 159); |
| 2354 | |
| 2355 | len = 20; |
| 2356 | |
| 2357 | break; |
| 2358 | case 4: /* TCH/AFS7.4 */ |
| 2359 | osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8, |
| 2360 | conv, n_errors, n_bits_total); |
| 2361 | |
| 2362 | tch_amr_unmerge(d, p, conv, 148, 61); |
| 2363 | |
| 2364 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2365 | if (rv) { |
| 2366 | /* Error checking CRC8 for an AMR 7.4 frame */ |
| 2367 | return -1; |
| 2368 | } |
| 2369 | |
| 2370 | tch_amr_reassemble(tch_data, d, 148); |
| 2371 | |
| 2372 | len = 19; |
| 2373 | |
| 2374 | break; |
| 2375 | case 3: /* TCH/AFS6.7 */ |
| 2376 | osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8, |
| 2377 | conv, n_errors, n_bits_total); |
| 2378 | |
| 2379 | tch_amr_unmerge(d, p, conv, 134, 55); |
| 2380 | |
| 2381 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2382 | if (rv) { |
| 2383 | /* Error checking CRC8 for an AMR 6.7 frame */ |
| 2384 | return -1; |
| 2385 | } |
| 2386 | |
| 2387 | tch_amr_reassemble(tch_data, d, 134); |
| 2388 | |
| 2389 | len = 17; |
| 2390 | |
| 2391 | break; |
| 2392 | case 2: /* TCH/AFS5.9 */ |
| 2393 | osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8, |
| 2394 | conv, n_errors, n_bits_total); |
| 2395 | |
| 2396 | tch_amr_unmerge(d, p, conv, 118, 55); |
| 2397 | |
| 2398 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2399 | if (rv) { |
| 2400 | /* Error checking CRC8 for an AMR 5.9 frame */ |
| 2401 | return -1; |
| 2402 | } |
| 2403 | |
| 2404 | tch_amr_reassemble(tch_data, d, 118); |
| 2405 | |
| 2406 | len = 15; |
| 2407 | |
| 2408 | break; |
| 2409 | case 1: /* TCH/AFS5.15 */ |
| 2410 | osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8, |
| 2411 | conv, n_errors, n_bits_total); |
| 2412 | |
| 2413 | tch_amr_unmerge(d, p, conv, 103, 49); |
| 2414 | |
| 2415 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2416 | if (rv) { |
| 2417 | /* Error checking CRC8 for an AMR 5.15 frame */ |
| 2418 | return -1; |
| 2419 | } |
| 2420 | |
| 2421 | tch_amr_reassemble(tch_data, d, 103); |
| 2422 | |
| 2423 | len = 13; |
| 2424 | |
| 2425 | break; |
| 2426 | case 0: /* TCH/AFS4.75 */ |
| 2427 | osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8, |
| 2428 | conv, n_errors, n_bits_total); |
| 2429 | |
| 2430 | tch_amr_unmerge(d, p, conv, 95, 39); |
| 2431 | |
| 2432 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2433 | if (rv) { |
| 2434 | /* Error checking CRC8 for an AMR 4.75 frame */ |
| 2435 | return -1; |
| 2436 | } |
| 2437 | |
| 2438 | tch_amr_reassemble(tch_data, d, 95); |
| 2439 | |
| 2440 | len = 12; |
| 2441 | |
| 2442 | break; |
| 2443 | default: |
| 2444 | /* Unknown frame type */ |
| 2445 | *n_bits_total = 448; |
| 2446 | *n_errors = *n_bits_total; |
| 2447 | return -1; |
| 2448 | } |
| 2449 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2450 | out: |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2451 | /* Change codec request / indication, if frame is valid */ |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2452 | if (id != -1) { |
| 2453 | if (codec_mode_req) |
| 2454 | *cmr = id; |
| 2455 | else |
| 2456 | *ft = id; |
| 2457 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2458 | |
| 2459 | return len; |
| 2460 | } |
| 2461 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2462 | /*! Perform channel encoding on a TCH/AFS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2463 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2464 | * \param[in] tch_data Codec input data in RTP payload format |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2465 | * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2466 | * \param[in] codec_mode_req Use CMR (1) or FT (0) |
| 2467 | * \param[in] codec Array of codecs (active codec set) |
| 2468 | * \param[in] codecs Number of entries in \a codec |
| 2469 | * \param[in] ft Frame Type to be used for encoding (index to \a codec) |
| 2470 | * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only) |
| 2471 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2472 | int gsm0503_tch_afs_encode(ubit_t *bursts, const uint8_t *tch_data, int len, |
Vadim Yanitskiy | d739e25 | 2023-11-10 04:22:32 +0700 | [diff] [blame] | 2473 | int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2474 | uint8_t cmr) |
| 2475 | { |
| 2476 | ubit_t iB[912], cB[456], h; |
| 2477 | ubit_t d[244], p[6], conv[250]; |
| 2478 | int i; |
| 2479 | uint8_t id; |
| 2480 | |
| 2481 | if (len == GSM_MACBLOCK_LEN) { /* FACCH */ |
| 2482 | _xcch_encode_cB(cB, tch_data); |
| 2483 | |
| 2484 | h = 1; |
| 2485 | |
| 2486 | goto facch; |
| 2487 | } |
| 2488 | |
| 2489 | h = 0; |
| 2490 | |
Vadim Yanitskiy | a995269 | 2023-11-10 03:31:55 +0700 | [diff] [blame] | 2491 | id = codec_mode_req ? cmr : ft; |
| 2492 | if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit))) |
| 2493 | return -1; |
| 2494 | if (OSMO_UNLIKELY(ft >= codecs)) |
| 2495 | return -1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2496 | |
| 2497 | switch (codec[ft]) { |
| 2498 | case 7: /* TCH/AFS12.2 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2499 | if (!len) { |
| 2500 | /* No data, induce BFI in the receiver by inverted CRC bits. |
| 2501 | * The data bit are all 0, so the correct parity bits would be 111111. */ |
| 2502 | memset(d, 0, 244); |
| 2503 | memset(p, 0, 6); |
| 2504 | } else { |
| 2505 | if (len != 31) |
| 2506 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2507 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2508 | tch_amr_disassemble(d, tch_data, 244); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2509 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2510 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p); |
| 2511 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2512 | |
| 2513 | tch_amr_merge(conv, d, p, 244, 81); |
| 2514 | |
| 2515 | osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8); |
| 2516 | |
| 2517 | break; |
| 2518 | case 6: /* TCH/AFS10.2 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2519 | if (!len) { |
| 2520 | /* See comment above. */ |
| 2521 | memset(d, 0, 204); |
| 2522 | memset(p, 0, 6); |
| 2523 | } else { |
| 2524 | if (len != 26) |
| 2525 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2526 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2527 | tch_amr_disassemble(d, tch_data, 204); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2528 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2529 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p); |
| 2530 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2531 | |
| 2532 | tch_amr_merge(conv, d, p, 204, 65); |
| 2533 | |
| 2534 | osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8); |
| 2535 | |
| 2536 | break; |
| 2537 | case 5: /* TCH/AFS7.95 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2538 | if (!len) { |
| 2539 | /* See comment above. */ |
| 2540 | memset(d, 0, 159); |
| 2541 | memset(p, 0, 6); |
| 2542 | } else { |
| 2543 | if (len != 20) |
| 2544 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2545 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2546 | tch_amr_disassemble(d, tch_data, 159); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2547 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2548 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p); |
| 2549 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2550 | |
| 2551 | tch_amr_merge(conv, d, p, 159, 75); |
| 2552 | |
| 2553 | osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8); |
| 2554 | |
| 2555 | break; |
| 2556 | case 4: /* TCH/AFS7.4 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2557 | if (!len) { |
| 2558 | /* See comment above. */ |
| 2559 | memset(d, 0, 148); |
| 2560 | memset(p, 0, 6); |
| 2561 | } else { |
| 2562 | if (len != 19) |
| 2563 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2564 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2565 | tch_amr_disassemble(d, tch_data, 148); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2566 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2567 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2568 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2569 | |
| 2570 | tch_amr_merge(conv, d, p, 148, 61); |
| 2571 | |
| 2572 | osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8); |
| 2573 | |
| 2574 | break; |
| 2575 | case 3: /* TCH/AFS6.7 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2576 | if (!len) { |
| 2577 | /* See comment above. */ |
| 2578 | memset(d, 0, 134); |
| 2579 | memset(p, 0, 6); |
| 2580 | } else { |
| 2581 | if (len != 17) |
| 2582 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2583 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2584 | tch_amr_disassemble(d, tch_data, 134); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2585 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2586 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2587 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2588 | |
| 2589 | tch_amr_merge(conv, d, p, 134, 55); |
| 2590 | |
| 2591 | osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8); |
| 2592 | |
| 2593 | break; |
| 2594 | case 2: /* TCH/AFS5.9 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2595 | if (!len) { |
| 2596 | /* See comment above. */ |
| 2597 | memset(d, 0, 118); |
| 2598 | memset(p, 0, 6); |
| 2599 | } else { |
| 2600 | if (len != 15) |
| 2601 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2602 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2603 | tch_amr_disassemble(d, tch_data, 118); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2604 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2605 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2606 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2607 | |
| 2608 | tch_amr_merge(conv, d, p, 118, 55); |
| 2609 | |
| 2610 | osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8); |
| 2611 | |
| 2612 | break; |
| 2613 | case 1: /* TCH/AFS5.15 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2614 | if (!len) { |
| 2615 | /* See comment above. */ |
| 2616 | memset(d, 0, 103); |
| 2617 | memset(p, 0, 6); |
| 2618 | } else { |
| 2619 | if (len != 13) |
| 2620 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2621 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2622 | tch_amr_disassemble(d, tch_data, 103); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2623 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2624 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2625 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2626 | |
| 2627 | tch_amr_merge(conv, d, p, 103, 49); |
| 2628 | |
| 2629 | osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8); |
| 2630 | |
| 2631 | break; |
| 2632 | case 0: /* TCH/AFS4.75 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2633 | if (!len) { |
| 2634 | /* See comment above. */ |
| 2635 | memset(d, 0, 95); |
| 2636 | memset(p, 0, 6); |
| 2637 | } else { |
| 2638 | if (len != 12) |
| 2639 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2640 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2641 | tch_amr_disassemble(d, tch_data, 95); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2642 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2643 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2644 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2645 | |
| 2646 | tch_amr_merge(conv, d, p, 95, 39); |
| 2647 | |
| 2648 | osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8); |
| 2649 | |
| 2650 | break; |
| 2651 | default: |
| 2652 | /* FIXME: FT %ft is not supported */ |
| 2653 | return -1; |
| 2654 | } |
| 2655 | |
| 2656 | memcpy(cB, gsm0503_afs_ic_ubit[id], 8); |
| 2657 | |
| 2658 | facch: |
| 2659 | gsm0503_tch_fr_interleave(cB, iB); |
| 2660 | |
| 2661 | for (i = 0; i < 8; i++) { |
| 2662 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2663 | &bursts[i * 116], &h, i >> 2); |
| 2664 | } |
| 2665 | |
| 2666 | return 0; |
| 2667 | |
| 2668 | invalid_length: |
| 2669 | /* FIXME: payload length %len does not comply with codec type %ft */ |
| 2670 | return -1; |
| 2671 | } |
| 2672 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2673 | /* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */ |
| 2674 | static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB) |
| 2675 | { |
| 2676 | unsigned int id = 0, best = 0; |
| 2677 | unsigned int i, j, k; |
| 2678 | |
| 2679 | for (i = 0, k = 0; i < 4; i++) { |
| 2680 | /* FIXME: why not using remaining (16 - 4) soft-bits here? */ |
| 2681 | for (j = 0, k = 0; j < 4; j++) |
| 2682 | k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j])); |
| 2683 | |
| 2684 | if (i == 0 || k < best) { |
| 2685 | best = k; |
| 2686 | id = i; |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | return id; |
| 2691 | } |
| 2692 | |
Vadim Yanitskiy | 8fb5647 | 2023-05-24 01:26:02 +0700 | [diff] [blame] | 2693 | /*! Perform channel decoding of a TCH/AHS channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2694 | * \param[out] tch_data Codec frame in RTP payload format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2695 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2696 | * \param[in] odd Is this an odd (1) or even (0) frame number? |
| 2697 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2698 | * \param[in] codec array of active codecs (active codec set) |
| 2699 | * \param[in] codecs number of codecs in \a codec |
| 2700 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2701 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2702 | * \param[out] n_errors Number of detected bit errors |
| 2703 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2704 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2705 | * codec out of range; negative on error |
| 2706 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2707 | int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2708 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2709 | uint8_t *cmr, int *n_errors, int *n_bits_total) |
| 2710 | { |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2711 | return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req, |
| 2712 | codec, codecs, ft, cmr, n_errors, |
| 2713 | n_bits_total, NULL); |
| 2714 | } |
| 2715 | |
Vadim Yanitskiy | 8fb5647 | 2023-05-24 01:26:02 +0700 | [diff] [blame] | 2716 | /*! Perform channel decoding of a TCH/AHS channel according TS 05.03 |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2717 | * \param[out] tch_data Codec frame in RTP payload format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2718 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2719 | * \param[in] odd Is this an odd (1) or even (0) frame number? |
| 2720 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2721 | * \param[in] codec array of active codecs (active codec set) |
| 2722 | * \param[in] codecs number of codecs in \a codec |
| 2723 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2724 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2725 | * \param[out] n_errors Number of detected bit errors |
| 2726 | * \param[out] n_bits_total Total number of bits |
| 2727 | * \param[inout] dtx DTX frame type output, previous DTX frame type input |
| 2728 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2729 | * codec out of range; negative on error |
| 2730 | */ |
| 2731 | int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd, |
| 2732 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2733 | uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) |
| 2734 | { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2735 | sbit_t iB[912], cB[456], h; |
| 2736 | ubit_t d[244], p[6], conv[135]; |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2737 | int i, rv, len, steal = 0, id = -1; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2738 | static ubit_t sid_first_dummy[64] = { 0 }; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2739 | |
| 2740 | /* only unmap the stealing bits */ |
| 2741 | if (!odd) { |
| 2742 | for (i = 0; i < 4; i++) { |
| 2743 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0); |
| 2744 | steal -= h; |
| 2745 | } |
| 2746 | for (i = 2; i < 5; i++) { |
| 2747 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1); |
| 2748 | steal -= h; |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | /* if we found a stole FACCH, but only at correct alignment */ |
| 2753 | if (steal > 0) { |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2754 | /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame. |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2755 | * It's normally updated by gsm0503_detect_ahs_dtx_frame2(), which is not |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2756 | * reached in case of FACCH. Reset it here to avoid FACCH/H frames being |
| 2757 | * misinterpreted as AMR's special DTX frames. */ |
| 2758 | if (dtx != NULL) |
| 2759 | *dtx = AMR_OTHER; |
| 2760 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2761 | for (i = 0; i < 6; i++) { |
| 2762 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2763 | &bursts[i * 116], NULL, i >> 2); |
| 2764 | } |
| 2765 | |
| 2766 | for (i = 2; i < 4; i++) { |
| 2767 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], |
| 2768 | &bursts[i * 116], NULL, 1); |
| 2769 | } |
| 2770 | |
| 2771 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2772 | |
| 2773 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2774 | if (rv) { |
| 2775 | /* Error decoding FACCH frame */ |
| 2776 | return -1; |
| 2777 | } |
| 2778 | |
| 2779 | return GSM_MACBLOCK_LEN; |
| 2780 | } |
| 2781 | |
| 2782 | for (i = 0; i < 4; i++) { |
| 2783 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2784 | &bursts[i * 116], NULL, i >> 1); |
| 2785 | } |
| 2786 | |
| 2787 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2788 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2789 | /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ |
| 2790 | if (dtx) { |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2791 | int n_bits_total_sid; |
| 2792 | int n_errors_sid; |
Vadim Yanitskiy | bf2d5e9 | 2022-05-15 15:58:10 +0300 | [diff] [blame] | 2793 | |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2794 | *dtx = gsm0503_detect_ahs_dtx_frame2(n_errors, n_bits_total, &id, cB); |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2795 | /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2796 | |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2797 | switch (*dtx) { |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2798 | case AHS_SID_UPDATE: /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2799 | /* cB[] contains 16 bits of coded in-band data and 212 bits containing |
| 2800 | * the identification marker. We need to unmap/deinterleave 114 odd |
| 2801 | * bits from the last two blocks, 114 even bits from the first two |
| 2802 | * blocks and combine them together. */ |
| 2803 | gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0); |
| 2804 | gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0); |
| 2805 | gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1); |
| 2806 | gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1); |
| 2807 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2808 | |
| 2809 | /* cB[] is expected to contain 16 bits of coded in-band data and |
| 2810 | * 212 bits containing the coded data (53 bits coded at 1/4 rate). */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2811 | *dtx = AHS_SID_UPDATE_CN; |
| 2812 | |
| 2813 | osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2814 | cB + 16, conv, &n_errors_sid, |
| 2815 | &n_bits_total_sid); |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2816 | /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker, |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2817 | * osmo_conv_decode_ber() calculates BER for the coded data. */ |
| 2818 | if (n_errors != NULL) |
| 2819 | *n_errors += n_errors_sid; |
| 2820 | if (n_bits_total != NULL) |
| 2821 | *n_bits_total += n_bits_total_sid; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2822 | rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, |
| 2823 | 35, conv + 35); |
| 2824 | if (rv != 0) { |
| 2825 | /* Error checking CRC14 for an AMR SID_UPDATE frame */ |
| 2826 | return -1; |
| 2827 | } |
| 2828 | |
| 2829 | tch_amr_sid_update_append(conv, 1, |
| 2830 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2831 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2832 | tch_amr_reassemble(tch_data, conv, 39); |
| 2833 | len = 5; |
| 2834 | goto out; |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2835 | case AHS_SID_FIRST_P2: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2836 | tch_amr_sid_update_append(sid_first_dummy, 0, |
| 2837 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2838 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2839 | tch_amr_reassemble(tch_data, sid_first_dummy, 39); |
| 2840 | len = 5; |
| 2841 | goto out; |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2842 | case AHS_ONSET: |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2843 | case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
| 2844 | case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
| 2845 | case AHS_SID_FIRST_P1: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2846 | len = 0; |
| 2847 | goto out; |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2848 | default: |
| 2849 | break; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2853 | /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */ |
| 2854 | if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2855 | /* Codec mode out of range, return id */ |
| 2856 | return id; |
| 2857 | } |
| 2858 | |
| 2859 | switch ((codec_mode_req) ? codec[*ft] : codec[id]) { |
| 2860 | case 5: /* TCH/AHS7.95 */ |
| 2861 | osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4, |
| 2862 | conv, n_errors, n_bits_total); |
| 2863 | |
| 2864 | tch_amr_unmerge(d, p, conv, 123, 67); |
| 2865 | |
| 2866 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p); |
| 2867 | if (rv) { |
| 2868 | /* Error checking CRC8 for an AMR 7.95 frame */ |
| 2869 | return -1; |
| 2870 | } |
| 2871 | |
| 2872 | for (i = 0; i < 36; i++) |
| 2873 | d[i + 123] = (cB[i + 192] < 0) ? 1 : 0; |
| 2874 | |
| 2875 | tch_amr_reassemble(tch_data, d, 159); |
| 2876 | |
| 2877 | len = 20; |
| 2878 | |
| 2879 | break; |
| 2880 | case 4: /* TCH/AHS7.4 */ |
| 2881 | osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4, |
| 2882 | conv, n_errors, n_bits_total); |
| 2883 | |
| 2884 | tch_amr_unmerge(d, p, conv, 120, 61); |
| 2885 | |
| 2886 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2887 | if (rv) { |
| 2888 | /* Error checking CRC8 for an AMR 7.4 frame */ |
| 2889 | return -1; |
| 2890 | } |
| 2891 | |
| 2892 | for (i = 0; i < 28; i++) |
| 2893 | d[i + 120] = (cB[i + 200] < 0) ? 1 : 0; |
| 2894 | |
| 2895 | tch_amr_reassemble(tch_data, d, 148); |
| 2896 | |
| 2897 | len = 19; |
| 2898 | |
| 2899 | break; |
| 2900 | case 3: /* TCH/AHS6.7 */ |
| 2901 | osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4, |
| 2902 | conv, n_errors, n_bits_total); |
| 2903 | |
| 2904 | tch_amr_unmerge(d, p, conv, 110, 55); |
| 2905 | |
| 2906 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2907 | if (rv) { |
| 2908 | /* Error checking CRC8 for an AMR 6.7 frame */ |
| 2909 | return -1; |
| 2910 | } |
| 2911 | |
| 2912 | for (i = 0; i < 24; i++) |
| 2913 | d[i + 110] = (cB[i + 204] < 0) ? 1 : 0; |
| 2914 | |
| 2915 | tch_amr_reassemble(tch_data, d, 134); |
| 2916 | |
| 2917 | len = 17; |
| 2918 | |
| 2919 | break; |
| 2920 | case 2: /* TCH/AHS5.9 */ |
| 2921 | osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4, |
| 2922 | conv, n_errors, n_bits_total); |
| 2923 | |
| 2924 | tch_amr_unmerge(d, p, conv, 102, 55); |
| 2925 | |
| 2926 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2927 | if (rv) { |
| 2928 | /* Error checking CRC8 for an AMR 5.9 frame */ |
| 2929 | return -1; |
| 2930 | } |
| 2931 | |
| 2932 | for (i = 0; i < 16; i++) |
| 2933 | d[i + 102] = (cB[i + 212] < 0) ? 1 : 0; |
| 2934 | |
| 2935 | tch_amr_reassemble(tch_data, d, 118); |
| 2936 | |
| 2937 | len = 15; |
| 2938 | |
| 2939 | break; |
| 2940 | case 1: /* TCH/AHS5.15 */ |
| 2941 | osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4, |
| 2942 | conv, n_errors, n_bits_total); |
| 2943 | |
| 2944 | tch_amr_unmerge(d, p, conv, 91, 49); |
| 2945 | |
| 2946 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2947 | if (rv) { |
| 2948 | /* Error checking CRC8 for an AMR 5.15 frame */ |
| 2949 | return -1; |
| 2950 | } |
| 2951 | |
| 2952 | for (i = 0; i < 12; i++) |
| 2953 | d[i + 91] = (cB[i + 216] < 0) ? 1 : 0; |
| 2954 | |
| 2955 | tch_amr_reassemble(tch_data, d, 103); |
| 2956 | |
| 2957 | len = 13; |
| 2958 | |
| 2959 | break; |
| 2960 | case 0: /* TCH/AHS4.75 */ |
| 2961 | osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4, |
| 2962 | conv, n_errors, n_bits_total); |
| 2963 | |
| 2964 | tch_amr_unmerge(d, p, conv, 83, 39); |
| 2965 | |
| 2966 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2967 | if (rv) { |
| 2968 | /* Error checking CRC8 for an AMR 4.75 frame */ |
| 2969 | return -1; |
| 2970 | } |
| 2971 | |
| 2972 | for (i = 0; i < 12; i++) |
| 2973 | d[i + 83] = (cB[i + 216] < 0) ? 1 : 0; |
| 2974 | |
| 2975 | tch_amr_reassemble(tch_data, d, 95); |
| 2976 | |
| 2977 | len = 12; |
| 2978 | |
| 2979 | break; |
| 2980 | default: |
| 2981 | /* Unknown frame type */ |
| 2982 | *n_bits_total = 159; |
| 2983 | *n_errors = *n_bits_total; |
| 2984 | return -1; |
| 2985 | } |
| 2986 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2987 | out: |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2988 | /* Change codec request / indication, if frame is valid */ |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2989 | if (id != -1) { |
| 2990 | if (codec_mode_req) |
| 2991 | *cmr = id; |
| 2992 | else |
| 2993 | *ft = id; |
| 2994 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2995 | |
| 2996 | return len; |
| 2997 | } |
| 2998 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2999 | /*! Perform channel encoding on a TCH/AHS channel according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3000 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 3001 | * \param[in] tch_data Codec input data in RTP payload format |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3002 | * \param[in] len Length of \a tch_data in bytes or 0 to generate a bad frame |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3003 | * \param[in] codec_mode_req Use CMR (1) or FT (0) |
| 3004 | * \param[in] codec Array of codecs (active codec set) |
| 3005 | * \param[in] codecs Number of entries in \a codec |
| 3006 | * \param[in] ft Frame Type to be used for encoding (index to \a codec) |
| 3007 | * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only) |
| 3008 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3009 | int gsm0503_tch_ahs_encode(ubit_t *bursts, const uint8_t *tch_data, int len, |
Vadim Yanitskiy | d739e25 | 2023-11-10 04:22:32 +0700 | [diff] [blame] | 3010 | int codec_mode_req, const uint8_t *codec, int codecs, uint8_t ft, |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3011 | uint8_t cmr) |
| 3012 | { |
| 3013 | ubit_t iB[912], cB[456], h; |
| 3014 | ubit_t d[244], p[6], conv[135]; |
| 3015 | int i; |
| 3016 | uint8_t id; |
| 3017 | |
| 3018 | if (len == GSM_MACBLOCK_LEN) { /* FACCH */ |
| 3019 | _xcch_encode_cB(cB, tch_data); |
| 3020 | |
| 3021 | h = 1; |
| 3022 | |
| 3023 | gsm0503_tch_fr_interleave(cB, iB); |
| 3024 | |
| 3025 | for (i = 0; i < 6; i++) |
| 3026 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 3027 | &h, i >> 2); |
| 3028 | for (i = 2; i < 4; i++) |
| 3029 | gsm0503_tch_burst_map(&iB[i * 114 + 456], |
| 3030 | &bursts[i * 116], &h, 1); |
| 3031 | |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | h = 0; |
| 3036 | |
Vadim Yanitskiy | a995269 | 2023-11-10 03:31:55 +0700 | [diff] [blame] | 3037 | id = codec_mode_req ? cmr : ft; |
| 3038 | if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit))) |
| 3039 | return -1; |
| 3040 | if (OSMO_UNLIKELY(ft >= codecs)) |
| 3041 | return -1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3042 | |
| 3043 | switch (codec[ft]) { |
| 3044 | case 5: /* TCH/AHS7.95 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3045 | if (!len) { |
| 3046 | /* No data, induce BFI in the receiver by inverted CRC bits. |
| 3047 | * The data bit are all 0, so the correct parity bits would be 111111. */ |
| 3048 | memset(d, 0, 159); |
| 3049 | memset(p, 0, 6); |
| 3050 | } else { |
| 3051 | if (len != 20) |
| 3052 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3053 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3054 | tch_amr_disassemble(d, tch_data, 159); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3055 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3056 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p); |
| 3057 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3058 | |
| 3059 | tch_amr_merge(conv, d, p, 123, 67); |
| 3060 | |
| 3061 | osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4); |
| 3062 | |
| 3063 | memcpy(cB + 192, d + 123, 36); |
| 3064 | |
| 3065 | break; |
| 3066 | case 4: /* TCH/AHS7.4 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3067 | if (!len) { |
| 3068 | /* See comment above. */ |
| 3069 | memset(d, 0, 148); |
| 3070 | memset(p, 0, 6); |
| 3071 | } else { |
| 3072 | if (len != 19) |
| 3073 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3074 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3075 | tch_amr_disassemble(d, tch_data, 148); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3076 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3077 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p); |
| 3078 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3079 | |
| 3080 | tch_amr_merge(conv, d, p, 120, 61); |
| 3081 | |
| 3082 | osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4); |
| 3083 | |
| 3084 | memcpy(cB + 200, d + 120, 28); |
| 3085 | |
| 3086 | break; |
| 3087 | case 3: /* TCH/AHS6.7 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3088 | if (!len) { |
| 3089 | /* See comment above. */ |
| 3090 | memset(d, 0, 134); |
| 3091 | memset(p, 0, 6); |
| 3092 | } else { |
| 3093 | if (len != 17) |
| 3094 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3095 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3096 | tch_amr_disassemble(d, tch_data, 134); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3097 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3098 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 3099 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3100 | |
| 3101 | tch_amr_merge(conv, d, p, 110, 55); |
| 3102 | |
| 3103 | osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4); |
| 3104 | |
| 3105 | memcpy(cB + 204, d + 110, 24); |
| 3106 | |
| 3107 | break; |
| 3108 | case 2: /* TCH/AHS5.9 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3109 | if (!len) { |
| 3110 | /* See comment above. */ |
| 3111 | memset(d, 0, 118); |
| 3112 | memset(p, 0, 6); |
| 3113 | } else { |
| 3114 | if (len != 15) |
| 3115 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3116 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3117 | tch_amr_disassemble(d, tch_data, 118); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3118 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3119 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 3120 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3121 | |
| 3122 | tch_amr_merge(conv, d, p, 102, 55); |
| 3123 | |
| 3124 | osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4); |
| 3125 | |
| 3126 | memcpy(cB + 212, d + 102, 16); |
| 3127 | |
| 3128 | break; |
| 3129 | case 1: /* TCH/AHS5.15 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3130 | if (!len) { |
| 3131 | /* See comment above. */ |
| 3132 | memset(d, 0, 103); |
| 3133 | memset(p, 0, 6); |
| 3134 | } else { |
| 3135 | if (len != 13) |
| 3136 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3137 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3138 | tch_amr_disassemble(d, tch_data, 103); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3139 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3140 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p); |
| 3141 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3142 | |
| 3143 | tch_amr_merge(conv, d, p, 91, 49); |
| 3144 | |
| 3145 | osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4); |
| 3146 | |
| 3147 | memcpy(cB + 216, d + 91, 12); |
| 3148 | |
| 3149 | break; |
| 3150 | case 0: /* TCH/AHS4.75 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3151 | if (!len) { |
| 3152 | /* See comment above. */ |
| 3153 | memset(d, 0, 95); |
| 3154 | memset(p, 0, 6); |
| 3155 | } else { |
| 3156 | if (len != 12) |
| 3157 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3158 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3159 | tch_amr_disassemble(d, tch_data, 95); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3160 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3161 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p); |
| 3162 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3163 | |
| 3164 | tch_amr_merge(conv, d, p, 83, 39); |
| 3165 | |
| 3166 | osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4); |
| 3167 | |
| 3168 | memcpy(cB + 216, d + 83, 12); |
| 3169 | |
| 3170 | break; |
| 3171 | default: |
| 3172 | /* FIXME: FT %ft is not supported */ |
| 3173 | return -1; |
| 3174 | } |
| 3175 | |
Philipp Maier | cfea39b | 2021-08-31 16:02:31 +0200 | [diff] [blame] | 3176 | memcpy(cB, gsm0503_ahs_ic_ubit[id], 4); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3177 | |
| 3178 | gsm0503_tch_hr_interleave(cB, iB); |
| 3179 | |
| 3180 | for (i = 0; i < 4; i++) |
| 3181 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1); |
| 3182 | |
| 3183 | return 0; |
| 3184 | |
| 3185 | invalid_length: |
| 3186 | /* FIXME: payload length %len does not comply with codec type %ft */ |
| 3187 | return -1; |
| 3188 | } |
| 3189 | |
| 3190 | /* |
| 3191 | * GSM RACH transcoding |
| 3192 | */ |
| 3193 | |
| 3194 | /* |
| 3195 | * GSM RACH apply BSIC to parity |
| 3196 | * |
| 3197 | * p(j) = p(j) xor b(j) j = 0, ..., 5 |
| 3198 | * b(0) = MSB of PLMN colour code |
| 3199 | * b(5) = LSB of BS colour code |
| 3200 | */ |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3201 | static inline void rach_apply_bsic(ubit_t *d, uint8_t bsic, uint8_t start) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3202 | { |
| 3203 | int i; |
| 3204 | |
| 3205 | /* Apply it */ |
| 3206 | for (i = 0; i < 6; i++) |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3207 | d[start + i] ^= ((bsic >> (5 - i)) & 1); |
| 3208 | } |
| 3209 | |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3210 | static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit, |
| 3211 | int *n_errors, int *n_bits_total) |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3212 | { |
| 3213 | ubit_t conv[17]; |
| 3214 | uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8; |
| 3215 | int rv; |
| 3216 | |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3217 | osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv, |
| 3218 | n_errors, n_bits_total); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3219 | |
| 3220 | rach_apply_bsic(conv, bsic, nbits); |
| 3221 | |
| 3222 | rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits); |
| 3223 | if (rv) |
| 3224 | return -1; |
| 3225 | |
| 3226 | osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1); |
| 3227 | |
Vadim Yanitskiy | 9e713f3 | 2020-03-31 19:40:09 +0700 | [diff] [blame] | 3228 | return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0]; |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 |
| 3232 | * \param[out] ra output buffer for RACH data |
| 3233 | * \param[in] burst Input burst data |
| 3234 | * \param[in] bsic BSIC used in this cell |
| 3235 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3236 | int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic) |
| 3237 | { |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3238 | int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3239 | |
| 3240 | if (r < 0) |
| 3241 | return r; |
| 3242 | |
| 3243 | *ra = r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3244 | |
| 3245 | return 0; |
| 3246 | } |
| 3247 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3248 | /*! Decode the (8-bit) RACH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3249 | * \param[out] ra output buffer for RACH data |
| 3250 | * \param[in] burst Input burst data |
| 3251 | * \param[in] bsic BSIC used in this cell |
| 3252 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3253 | int gsm0503_rach_decode(uint8_t *ra, const sbit_t *burst, uint8_t bsic) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3254 | { |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3255 | int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL); |
| 3256 | if (r < 0) |
| 3257 | return r; |
| 3258 | |
| 3259 | *ra = r; |
| 3260 | return 0; |
| 3261 | } |
| 3262 | |
| 3263 | /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 |
| 3264 | * \param[out] ra output buffer for RACH data |
| 3265 | * \param[in] burst Input burst data |
| 3266 | * \param[in] bsic BSIC used in this cell |
| 3267 | * \param[out] n_errors Number of detected bit errors |
| 3268 | * \param[out] n_bits_total Total number of bits |
| 3269 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3270 | int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic, |
| 3271 | int *n_errors, int *n_bits_total) |
| 3272 | { |
| 3273 | int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total); |
| 3274 | if (r < 0) |
| 3275 | return r; |
| 3276 | |
| 3277 | *ra = r; |
| 3278 | return 0; |
| 3279 | } |
| 3280 | |
| 3281 | /*! Decode the (8-bit) RACH according to TS 05.03 |
| 3282 | * \param[out] ra output buffer for RACH data |
| 3283 | * \param[in] burst Input burst data |
| 3284 | * \param[in] bsic BSIC used in this cell |
| 3285 | * \param[out] n_errors Number of detected bit errors |
| 3286 | * \param[out] n_bits_total Total number of bits |
| 3287 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3288 | int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic, |
| 3289 | int *n_errors, int *n_bits_total) |
| 3290 | { |
| 3291 | int16_t r = rach_decode_ber(burst, bsic, false, n_errors, n_bits_total); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3292 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3293 | if (r < 0) |
| 3294 | return r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3295 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3296 | *ra = r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3297 | |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3301 | /*! Encode the (8-bit) RACH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3302 | * \param[out] burst Caller-allocated output burst buffer |
| 3303 | * \param[in] ra Input RACH data |
| 3304 | * \param[in] bsic BSIC used in this cell |
| 3305 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3306 | int gsm0503_rach_encode(ubit_t *burst, const uint8_t *ra, uint8_t bsic) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3307 | { |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3308 | return gsm0503_rach_ext_encode(burst, *ra, bsic, false); |
| 3309 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3310 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3311 | /*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003 |
| 3312 | * \param[out] burst Caller-allocated output burst buffer |
| 3313 | * \param[in] ra11 Input RACH data |
| 3314 | * \param[in] bsic BSIC used in this cell |
| 3315 | * \param[in] is_11bit whether given RA is 11 bit or not |
| 3316 | * \returns 0 on success; negative on error */ |
| 3317 | int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit) |
| 3318 | { |
| 3319 | ubit_t conv[17]; |
| 3320 | uint8_t ra[2] = { 0 }, nbits = 8; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3321 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3322 | if (is_11bit) { |
Vadim Yanitskiy | 9e713f3 | 2020-03-31 19:40:09 +0700 | [diff] [blame] | 3323 | ra[0] = (uint8_t) (ra11 >> 3); |
| 3324 | ra[1] = (uint8_t) (ra11 & 0x07); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3325 | nbits = 11; |
| 3326 | } else |
| 3327 | ra[0] = (uint8_t)ra11; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3328 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3329 | osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3330 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3331 | osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits); |
| 3332 | |
| 3333 | rach_apply_bsic(conv, bsic, nbits); |
| 3334 | |
| 3335 | osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3336 | |
| 3337 | return 0; |
| 3338 | } |
| 3339 | |
| 3340 | /* |
| 3341 | * GSM SCH transcoding |
| 3342 | */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3343 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3344 | /*! Decode the SCH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3345 | * \param[out] sb_info output buffer for SCH data |
| 3346 | * \param[in] burst Input burst data |
| 3347 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3348 | int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3349 | { |
| 3350 | ubit_t conv[35]; |
| 3351 | int rv; |
| 3352 | |
| 3353 | osmo_conv_decode(&gsm0503_sch, burst, conv); |
| 3354 | |
| 3355 | rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25); |
| 3356 | if (rv) |
| 3357 | return -1; |
| 3358 | |
| 3359 | osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1); |
| 3360 | |
| 3361 | return 0; |
| 3362 | } |
| 3363 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3364 | /*! Encode the SCH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3365 | * \param[out] burst Caller-allocated output burst buffer |
| 3366 | * \param[in] sb_info Input SCH data |
| 3367 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3368 | int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3369 | { |
| 3370 | ubit_t conv[35]; |
| 3371 | |
| 3372 | osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1); |
| 3373 | |
| 3374 | osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25); |
| 3375 | |
| 3376 | osmo_conv_encode(&gsm0503_sch, conv, burst); |
| 3377 | |
| 3378 | return 0; |
| 3379 | } |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3380 | |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3381 | /* |
| 3382 | * GSM CSD transcoding |
| 3383 | */ |
| 3384 | |
| 3385 | static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB) |
| 3386 | { |
Vadim Yanitskiy | b04d59e | 2023-05-26 01:38:23 +0700 | [diff] [blame] | 3387 | unsigned int i; |
| 3388 | |
| 3389 | /* hu(B): copy *even* numbered bits if not stolen by FACCH */ |
| 3390 | if (burst[58] == 0) { |
| 3391 | for (i = 0; i < 57; i += 2) |
| 3392 | burst[i] |= iB[i]; |
| 3393 | for (i = 58; i < 114; i += 2) |
| 3394 | burst[i + 2] |= iB[i]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3395 | } |
| 3396 | |
Vadim Yanitskiy | b04d59e | 2023-05-26 01:38:23 +0700 | [diff] [blame] | 3397 | /* hl(B): copy *odd* numbered bits if not stolen by FACCH */ |
| 3398 | if (burst[57] == 0) { |
| 3399 | for (i = 1; i < 57; i += 2) |
| 3400 | burst[i] |= iB[i]; |
| 3401 | for (i = 57; i < 114; i += 2) |
| 3402 | burst[i + 2] |= iB[i]; |
| 3403 | } |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | /*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3. |
| 3407 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3408 | * 22 * 2 * 58 == 2552 bits total. |
| 3409 | * \param[in] data Data to be encoded (240 unpacked bits). |
| 3410 | * \returns 0 in case of success; negative on error. */ |
| 3411 | int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data) |
| 3412 | { |
| 3413 | ubit_t iB[22 * 114], cB[4 * 114]; |
| 3414 | ubit_t conv[4 * 60 + 4]; |
| 3415 | |
| 3416 | /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */ |
| 3417 | memcpy(&conv[0], &data[0], 4 * 60); |
Vadim Yanitskiy | f8e570a | 2023-07-08 06:05:12 +0700 | [diff] [blame] | 3418 | /* pad(4) is set to 0 by osmo_conv_encode() below */ |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3419 | |
| 3420 | /* 3.3.3 Convolutional encoder */ |
| 3421 | osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]); |
| 3422 | |
| 3423 | /* 3.3.4 Interleaving */ |
| 3424 | memset(&iB[0], 0, sizeof(iB)); |
| 3425 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3426 | |
| 3427 | /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3428 | for (unsigned int i = 0; i < 22; i++) |
| 3429 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3430 | |
| 3431 | return 0; |
| 3432 | } |
| 3433 | |
| 3434 | /*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3. |
| 3435 | * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits). |
| 3436 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3437 | * 22 * 2 * 58 == 2552 bits total. |
| 3438 | * \param[out] n_errors Number of detected bit errors. |
| 3439 | * \param[out] n_bits_total Total number of bits. |
| 3440 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3441 | int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts, |
| 3442 | int *n_errors, int *n_bits_total) |
| 3443 | { |
| 3444 | sbit_t iB[22 * 114], cB[4 * 114]; |
| 3445 | ubit_t conv[4 * 60 + 4]; |
| 3446 | |
| 3447 | /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3448 | for (unsigned int i = 0; i < 22; i++) { |
| 3449 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3450 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3451 | } |
| 3452 | |
| 3453 | /* 3.3.4 Interleaving */ |
| 3454 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3455 | |
| 3456 | /* 3.3.3 Convolutional encoder */ |
| 3457 | osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total); |
| 3458 | |
| 3459 | /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */ |
| 3460 | memcpy(&data[0], &conv[0], 4 * 60); |
| 3461 | |
| 3462 | return 4 * 60; |
| 3463 | } |
| 3464 | |
| 3465 | /*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4. |
| 3466 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3467 | * 22 * 2 * 58 == 2552 bits total. |
| 3468 | * \param[in] data Data to be encoded (120 unpacked bits). |
| 3469 | * \returns 0 in case of success; negative on error */ |
| 3470 | int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data) |
| 3471 | { |
| 3472 | ubit_t iB[22 * 114], cB[4 * 114]; |
| 3473 | ubit_t conv[2 * 60 + 32]; |
| 3474 | |
| 3475 | /* 3.4.2 Block code: |
| 3476 | * |
| 3477 | * Sixteen bits equal to 0 are added to the 60 information bits, the result |
| 3478 | * being a block of 76 bits, {u(0),u(1),...,u(75)}, with: |
| 3479 | * |
| 3480 | * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14; |
| 3481 | * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18. |
| 3482 | * |
| 3483 | * Two such blocks forming a block of 152 bits: u1 + u2. */ |
| 3484 | for (unsigned int k = 0; k < 2 * 4; k++) { |
| 3485 | memcpy(&conv[19 * k], &data[15 * k], 15); |
| 3486 | memset(&conv[19 * k + 15], 0, 4); |
| 3487 | } |
| 3488 | |
| 3489 | /* 3.4.3 Convolutional encoder */ |
| 3490 | osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]); |
| 3491 | |
| 3492 | /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3493 | memset(&iB[0], 0, sizeof(iB)); |
| 3494 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3495 | |
| 3496 | /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3497 | for (unsigned int i = 0; i < 22; i++) |
| 3498 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3499 | |
| 3500 | return 0; |
| 3501 | } |
| 3502 | |
| 3503 | /*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4. |
| 3504 | * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits). |
| 3505 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3506 | * 22 * 2 * 58 == 2552 bits total. |
| 3507 | * \param[out] n_errors Number of detected bit errors. |
| 3508 | * \param[out] n_bits_total Total number of bits. |
| 3509 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3510 | int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts, |
| 3511 | int *n_errors, int *n_bits_total) |
| 3512 | { |
| 3513 | sbit_t iB[22 * 114], cB[4 * 114]; |
| 3514 | ubit_t conv[2 * 60 + 32]; |
| 3515 | |
| 3516 | /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3517 | for (unsigned int i = 0; i < 22; i++) { |
| 3518 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3519 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3520 | } |
| 3521 | |
| 3522 | /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3523 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3524 | |
| 3525 | /* 3.4.3 Convolutional encoder */ |
| 3526 | osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total); |
| 3527 | |
| 3528 | /* 3.4.2 Block code: |
| 3529 | * |
| 3530 | * Sixteen bits equal to 0 are added to the 60 information bits, the result |
| 3531 | * being a block of 76 bits, {u(0),u(1),...,u(75)}, with: |
| 3532 | * |
| 3533 | * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14; |
| 3534 | * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18. |
| 3535 | * |
| 3536 | * Two such blocks forming a block of 152 bits: u1 + u2. */ |
| 3537 | for (unsigned int k = 0; k < 2 * 4; k++) |
| 3538 | memcpy(&data[15 * k], &conv[19 * k], 15); |
| 3539 | |
| 3540 | return 2 * 60; |
| 3541 | } |
| 3542 | |
| 3543 | /*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5. |
| 3544 | * The algorithm is identical to TCH/F9.6, so it's just a wrapper. |
| 3545 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3546 | * 22 * 2 * 58 == 2552 bits total. |
| 3547 | * \param[in] data Data to be encoded (240 unpacked bits). |
| 3548 | * \returns 0 in case of success; negative on error */ |
| 3549 | int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data) |
| 3550 | { |
| 3551 | return gsm0503_tch_fr96_encode(bursts, data); |
| 3552 | } |
| 3553 | |
| 3554 | /*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5. |
| 3555 | * The algorithm is identical to TCH/F9.6, so it's just a wrapper. |
| 3556 | * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits). |
| 3557 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3558 | * 22 * 2 * 58 == 2552 bits total. |
| 3559 | * \param[out] n_errors Number of detected bit errors. |
| 3560 | * \param[out] n_bits_total Total number of bits. |
| 3561 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3562 | int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts, |
| 3563 | int *n_errors, int *n_bits_total) |
| 3564 | { |
| 3565 | return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total); |
| 3566 | } |
| 3567 | |
Vadim Yanitskiy | fadda01 | 2023-07-08 06:30:55 +0700 | [diff] [blame] | 3568 | /*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6. |
| 3569 | * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts, |
| 3570 | * 8 * 2 * 58 == 928 bits total. |
| 3571 | * \param[in] data Data to be encoded (72 unpacked bits). |
| 3572 | * \returns 0 in case of success; negative on error */ |
| 3573 | int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data) |
| 3574 | { |
| 3575 | ubit_t iB[8 * 114], cB[4 * 114]; |
| 3576 | const ubit_t h = 0; |
| 3577 | |
| 3578 | /* 3.6.{1-3} Block code and Convolutional encoder */ |
| 3579 | osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]); |
| 3580 | |
| 3581 | /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3582 | gsm0503_tch_fr_interleave(&cB[0], &iB[0]); |
| 3583 | |
| 3584 | /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3585 | for (unsigned int i = 0; i < 8; i++) |
| 3586 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 3587 | |
| 3588 | return 0; |
| 3589 | } |
| 3590 | |
| 3591 | /*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6. |
| 3592 | * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits). |
| 3593 | * \param[in] bursts Buffer containing the symbols of 8 bursts, |
| 3594 | * 8 * 2 * 58 == 928 bits total. |
| 3595 | * \param[out] n_errors Number of detected bit errors. |
| 3596 | * \param[out] n_bits_total Total number of bits. |
| 3597 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3598 | int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts, |
| 3599 | int *n_errors, int *n_bits_total) |
| 3600 | { |
| 3601 | sbit_t iB[8 * 114], cB[4 * 114]; |
| 3602 | |
| 3603 | /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3604 | for (unsigned int i = 0; i < 8; i++) |
| 3605 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2); |
| 3606 | |
| 3607 | /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3608 | gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]); |
| 3609 | |
| 3610 | /* 3.6.{1-3} Block code and Convolutional encoder */ |
| 3611 | osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total); |
| 3612 | |
| 3613 | return 72; |
| 3614 | } |
| 3615 | |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3616 | /*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7. |
| 3617 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3618 | * 22 * 2 * 58 == 2552 bits total. |
| 3619 | * \param[in] data Data to be encoded (144 unpacked bits). |
| 3620 | * \returns 0 in case of success; negative on error */ |
| 3621 | int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data) |
| 3622 | { |
| 3623 | ubit_t iB[22 * 114], cB[4 * 114]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3624 | |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3625 | /* 3.7.{1-3} Block code and Convolutional encoder */ |
| 3626 | osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]); |
| 3627 | osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]); |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3628 | |
| 3629 | /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3630 | memset(&iB[0], 0, sizeof(iB)); |
| 3631 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3632 | |
| 3633 | /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3634 | for (unsigned int i = 0; i < 22; i++) |
| 3635 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3636 | |
| 3637 | return 0; |
| 3638 | } |
| 3639 | |
| 3640 | /*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7. |
| 3641 | * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits). |
| 3642 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3643 | * 22 * 2 * 58 == 2552 bits total. |
| 3644 | * \param[out] n_errors Number of detected bit errors. |
| 3645 | * \param[out] n_bits_total Total number of bits. |
| 3646 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3647 | int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts, |
| 3648 | int *n_errors, int *n_bits_total) |
| 3649 | { |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3650 | int n_errors_l[2], n_bits_total_l[2]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3651 | sbit_t iB[22 * 114], cB[4 * 114]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3652 | |
| 3653 | /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3654 | for (unsigned int i = 0; i < 22; i++) { |
| 3655 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3656 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3657 | } |
| 3658 | |
| 3659 | /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3660 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3661 | |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3662 | /* 3.7.{1-3} Block code and Convolutional encoder */ |
| 3663 | osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]); |
| 3664 | osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[228], &data[72], &n_errors_l[1], &n_bits_total_l[1]); |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3665 | |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3666 | if (n_errors) |
| 3667 | *n_errors = n_errors_l[0] + n_errors_l[1]; |
| 3668 | |
| 3669 | if (n_bits_total) |
| 3670 | *n_bits_total = n_bits_total_l[0] + n_bits_total_l[1]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3671 | |
| 3672 | return 2 * 72; |
| 3673 | } |
| 3674 | |
| 3675 | /*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8. |
| 3676 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3677 | * 22 * 2 * 58 == 2552 bits total. |
| 3678 | * \param[in] data Data to be encoded (290 unpacked bits). |
| 3679 | * \returns 0 in case of success; negative on error */ |
| 3680 | int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data) |
| 3681 | { |
| 3682 | ubit_t iB[22 * 114], cB[4 * 114]; |
| 3683 | ubit_t conv[290 + 4]; |
| 3684 | |
| 3685 | /* 3.8.2 Block code: b(290) + pad(4) */ |
| 3686 | memcpy(&conv[0], &data[0], 290); |
Vadim Yanitskiy | f8e570a | 2023-07-08 06:05:12 +0700 | [diff] [blame] | 3687 | /* pad(4) is set to 0 by osmo_conv_encode() below */ |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3688 | |
| 3689 | /* 3.8.3 Convolutional encoder */ |
| 3690 | osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]); |
| 3691 | |
| 3692 | /* 3.8.4 Interleaving */ |
| 3693 | memset(&iB[0], 0, sizeof(iB)); |
| 3694 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3695 | |
| 3696 | /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3697 | for (unsigned int i = 0; i < 22; i++) |
| 3698 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3699 | |
| 3700 | return 0; |
| 3701 | } |
| 3702 | |
| 3703 | /*! Perform channel decoding of a TCH/14.4 channel as per section 3.8. |
| 3704 | * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits). |
| 3705 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3706 | * 22 * 2 * 58 == 2552 bits total. |
| 3707 | * \param[out] n_errors Number of detected bit errors. |
| 3708 | * \param[out] n_bits_total Total number of bits. |
| 3709 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3710 | int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts, |
| 3711 | int *n_errors, int *n_bits_total) |
| 3712 | { |
| 3713 | sbit_t iB[22 * 114], cB[4 * 114]; |
| 3714 | ubit_t conv[294]; |
| 3715 | |
| 3716 | /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3717 | for (unsigned int i = 0; i < 22; i++) { |
| 3718 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3719 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3720 | } |
| 3721 | |
| 3722 | /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3723 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3724 | |
| 3725 | /* 3.8.3 Convolutional encoder */ |
| 3726 | osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total); |
| 3727 | |
| 3728 | /* 3.8.2 Block code: b(290) + pad(4) */ |
| 3729 | memcpy(&data[0], &conv[0], 290); |
| 3730 | |
| 3731 | return 290; |
| 3732 | } |
| 3733 | |
Vadim Yanitskiy | b334022 | 2023-05-26 01:07:18 +0700 | [diff] [blame] | 3734 | /* |
| 3735 | * FACCH/[FH] transcoding |
| 3736 | */ |
| 3737 | |
| 3738 | /*! Perform channel encoding of a FACCH/F data as per section 4.2. |
| 3739 | * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts, |
| 3740 | * 8 * 2 * 58 == 928 bits total. |
| 3741 | * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN). |
| 3742 | * \returns 0 in case of success; negative on error */ |
| 3743 | int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data) |
| 3744 | { |
| 3745 | ubit_t iB[8 * 114], cB[4 * 114]; |
| 3746 | const ubit_t h = 1; |
| 3747 | |
| 3748 | /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3749 | _xcch_encode_cB(&cB[0], &data[0]); |
| 3750 | |
| 3751 | /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3752 | gsm0503_tch_fr_interleave(&cB[0], &iB[0]); |
| 3753 | |
| 3754 | /* 4.2.5 Mapping on a Burst: |
| 3755 | * - hu(B)=1 the even numbered bits in the first 4 bursts and |
| 3756 | * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */ |
| 3757 | for (unsigned int i = 0; i < 8; i++) |
| 3758 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 3759 | |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
| 3763 | /*! Perform channel decoding of a FACCH/F data as per section 4.2. |
| 3764 | * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN). |
| 3765 | * \param[in] bursts Buffer containing the symbols of 8 bursts, |
| 3766 | * 8 * 2 * 58 == 928 bits total. |
| 3767 | * \param[out] n_errors Number of detected bit errors. |
| 3768 | * \param[out] n_bits_total Total number of bits. |
| 3769 | * \returns Number of bytes used in the output buffer; negative on error. */ |
| 3770 | int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts, |
| 3771 | int *n_errors, int *n_bits_total) |
| 3772 | { |
| 3773 | sbit_t iB[8 * 114], cB[4 * 114]; |
| 3774 | int steal = 0; |
| 3775 | |
| 3776 | /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */ |
| 3777 | for (unsigned int i = 0; i < 4; i++) |
| 3778 | steal -= bursts[i * 116 + 58]; /* hu(B) */ |
| 3779 | for (unsigned int i = 4; i < 8; i++) |
| 3780 | steal -= bursts[i * 116 + 57]; /* hl(B) */ |
| 3781 | if (steal <= 0) |
| 3782 | return -1; |
| 3783 | |
| 3784 | /* 4.2.5 Mapping on a Burst: |
| 3785 | * - hu(B)=1 the even numbered bits in the first 4 bursts and |
| 3786 | * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */ |
| 3787 | for (unsigned int i = 0; i < 8; i++) |
| 3788 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2); |
| 3789 | |
| 3790 | /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3791 | gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]); |
| 3792 | |
| 3793 | /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3794 | if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0) |
| 3795 | return -1; |
| 3796 | |
| 3797 | return GSM_MACBLOCK_LEN; |
| 3798 | } |
| 3799 | |
| 3800 | /*! Perform channel encoding of a FACCH/H data as per section 4.3. |
| 3801 | * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts, |
| 3802 | * 6 * 2 * 58 == 696 bits total. |
| 3803 | * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN). |
| 3804 | * \returns 0 in case of success; negative on error */ |
| 3805 | int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data) |
| 3806 | { |
| 3807 | ubit_t iB[8 * 114], cB[4 * 114]; |
| 3808 | const ubit_t h = 1; |
| 3809 | |
| 3810 | /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3811 | _xcch_encode_cB(&cB[0], &data[0]); |
| 3812 | |
| 3813 | /* 4.3.4 Interleaving */ |
| 3814 | gsm0503_tch_fr_interleave(&cB[0], &iB[0]); |
| 3815 | |
| 3816 | /* 4.3.5 Mapping on a Burst: |
| 3817 | * - hu(B)=1 the even numbered bits of the first 2 bursts, |
| 3818 | * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and |
| 3819 | * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */ |
| 3820 | for (unsigned int i = 0; i < 6; i++) |
| 3821 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 3822 | for (unsigned int i = 2; i < 4; i++) |
| 3823 | gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1); |
| 3824 | |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
| 3828 | /*! Perform channel decoding of a FACCH/H data as per section 4.3. |
| 3829 | * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN). |
| 3830 | * \param[in] bursts Buffer containing the symbols of 6 bursts, |
| 3831 | * 6 * 2 * 58 == 696 bits total. |
| 3832 | * \param[out] n_errors Number of detected bit errors. |
| 3833 | * \param[out] n_bits_total Total number of bits. |
| 3834 | * \returns Number of bytes used in the output buffer; negative on error. */ |
| 3835 | int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts, |
| 3836 | int *n_errors, int *n_bits_total) |
| 3837 | { |
| 3838 | sbit_t iB[8 * 114], cB[4 * 114]; |
| 3839 | int steal = 0; |
| 3840 | |
| 3841 | /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */ |
| 3842 | for (unsigned int i = 0; i < 4; i++) |
| 3843 | steal -= bursts[i * 116 + 58]; /* hu(B) */ |
| 3844 | for (unsigned int i = 2; i < 6; i++) |
| 3845 | steal -= bursts[i * 116 + 57]; /* hl(B) */ |
| 3846 | if (steal <= 0) |
| 3847 | return -1; |
| 3848 | |
| 3849 | /* 4.3.5 Mapping on a Burst: |
| 3850 | * - hu(B)=1 the even numbered bits of the first 2 bursts, |
| 3851 | * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and |
| 3852 | * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */ |
| 3853 | for (unsigned int i = 0; i < 6; i++) |
| 3854 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2); |
| 3855 | for (unsigned int i = 2; i < 4; i++) |
| 3856 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1); |
| 3857 | |
| 3858 | /* 4.3.4 Interleaving */ |
| 3859 | gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]); |
| 3860 | |
| 3861 | /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3862 | if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0) |
| 3863 | return -1; |
| 3864 | |
| 3865 | return GSM_MACBLOCK_LEN; |
| 3866 | } |
| 3867 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3868 | /*! @} */ |