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: |
| 1062 | for (i = 587, j = 455; i >= 0; i--) { |
| 1063 | if (!gsm0503_puncture_cs2[i]) |
| 1064 | cB[i] = cB[j--]; |
| 1065 | else |
| 1066 | cB[i] = 0; |
| 1067 | } |
| 1068 | |
| 1069 | osmo_conv_decode_ber(&gsm0503_cs2_np, cB, |
| 1070 | conv, n_errors, n_bits_total); |
| 1071 | |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1072 | /* 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] | 1073 | for (i = 0; i < 8; i++) { |
| 1074 | for (j = 0, k = 0; j < 6; j++) |
| 1075 | k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j])); |
| 1076 | |
| 1077 | if (i == 0 || k < best) { |
| 1078 | best = k; |
| 1079 | usf = i; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | conv[3] = usf & 1; |
| 1084 | conv[4] = (usf >> 1) & 1; |
| 1085 | conv[5] = (usf >> 2) & 1; |
| 1086 | if (usf_p) |
| 1087 | *usf_p = usf; |
| 1088 | |
| 1089 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1090 | conv + 3, 271, conv + 3 + 271); |
| 1091 | if (rv) |
| 1092 | return -1; |
| 1093 | |
| 1094 | osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 271, 1); |
| 1095 | |
| 1096 | return 34; |
| 1097 | case 3: |
| 1098 | for (i = 675, j = 455; i >= 0; i--) { |
| 1099 | if (!gsm0503_puncture_cs3[i]) |
| 1100 | cB[i] = cB[j--]; |
| 1101 | else |
| 1102 | cB[i] = 0; |
| 1103 | } |
| 1104 | |
| 1105 | osmo_conv_decode_ber(&gsm0503_cs3_np, cB, |
| 1106 | conv, n_errors, n_bits_total); |
| 1107 | |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1108 | /* 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] | 1109 | for (i = 0; i < 8; i++) { |
| 1110 | for (j = 0, k = 0; j < 6; j++) |
| 1111 | k += abs(((int)gsm0503_usf2six[i][j]) - ((int)conv[j])); |
| 1112 | |
| 1113 | if (i == 0 || k < best) { |
| 1114 | best = k; |
| 1115 | usf = i; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | conv[3] = usf & 1; |
| 1120 | conv[4] = (usf >> 1) & 1; |
| 1121 | conv[5] = (usf >> 2) & 1; |
| 1122 | if (usf_p) |
| 1123 | *usf_p = usf; |
| 1124 | |
| 1125 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1126 | conv + 3, 315, conv + 3 + 315); |
| 1127 | if (rv) |
| 1128 | return -1; |
| 1129 | |
| 1130 | osmo_ubit2pbit_ext(l2_data, 0, conv, 3, 315, 1); |
| 1131 | |
| 1132 | return 40; |
| 1133 | case 4: |
| 1134 | for (i = 12; i < 456; i++) |
| 1135 | conv[i] = (cB[i] < 0) ? 1 : 0; |
| 1136 | |
Vadim Yanitskiy | 93b8eb3 | 2023-12-08 02:52:12 +0700 | [diff] [blame] | 1137 | /* 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] | 1138 | for (i = 0; i < 8; i++) { |
| 1139 | for (j = 0, k = 0; j < 12; j++) |
| 1140 | k += abs(((int)gsm0503_usf2twelve_sbit[i][j]) - ((int)cB[j])); |
| 1141 | |
| 1142 | if (i == 0 || k < best) { |
| 1143 | best = k; |
| 1144 | usf = i; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | conv[9] = usf & 1; |
| 1149 | conv[10] = (usf >> 1) & 1; |
| 1150 | conv[11] = (usf >> 2) & 1; |
| 1151 | if (usf_p) |
| 1152 | *usf_p = usf; |
| 1153 | |
| 1154 | rv = osmo_crc16gen_check_bits(&gsm0503_cs234_crc16, |
| 1155 | conv + 9, 431, conv + 9 + 431); |
| 1156 | if (rv) { |
| 1157 | *n_bits_total = 456 - 12; |
| 1158 | *n_errors = *n_bits_total; |
| 1159 | return -1; |
| 1160 | } |
| 1161 | |
| 1162 | *n_bits_total = 456 - 12; |
| 1163 | *n_errors = 0; |
| 1164 | |
| 1165 | osmo_ubit2pbit_ext(l2_data, 0, conv, 9, 431, 1); |
| 1166 | |
| 1167 | return 54; |
| 1168 | default: |
| 1169 | *n_bits_total = 0; |
| 1170 | *n_errors = 0; |
| 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | return -1; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
Pau Espin Pedrol | f62f073 | 2020-04-07 13:12:11 +0200 | [diff] [blame] | 1178 | * EGPRS PDTCH DL block encoding |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1179 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1180 | 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] | 1181 | { |
| 1182 | int i; |
| 1183 | ubit_t iB[456]; |
| 1184 | const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; |
| 1185 | |
Pau Espin Pedrol | 63ebc36 | 2020-04-07 13:15:36 +0200 | [diff] [blame] | 1186 | gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1187 | |
| 1188 | for (i = 0; i < 4; i++) { |
| 1189 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 1190 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1191 | } |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1196 | 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] | 1197 | { |
| 1198 | int i; |
| 1199 | const ubit_t *up; |
| 1200 | ubit_t hi[EGPRS_HDR_HC_MAX]; |
| 1201 | ubit_t di[EGPRS_DATA_DC_MAX]; |
| 1202 | |
| 1203 | gsm0503_mcs5_dl_interleave(hc, dc, hi, di); |
| 1204 | up = gsm0503_mcs5_usf_precode_table[usf]; |
| 1205 | |
| 1206 | for (i = 0; i < 4; i++) { |
| 1207 | gsm0503_mcs5_dl_burst_map(di, &bursts[i * 348], hi, up, i); |
| 1208 | gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]); |
| 1209 | } |
| 1210 | |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1214 | static int egprs_type1_map(ubit_t *bursts, const ubit_t *hc, |
| 1215 | const ubit_t *c1, const ubit_t *c2, int usf, int mcs) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1216 | { |
| 1217 | int i; |
| 1218 | const ubit_t *up; |
| 1219 | ubit_t hi[EGPRS_HDR_HC_MAX]; |
| 1220 | ubit_t di[EGPRS_DATA_C1 * 2]; |
| 1221 | |
| 1222 | if (mcs == EGPRS_MCS7) |
| 1223 | gsm0503_mcs7_dl_interleave(hc, c1, c2, hi, di); |
| 1224 | else |
| 1225 | gsm0503_mcs8_dl_interleave(hc, c1, c2, hi, di); |
| 1226 | |
| 1227 | up = gsm0503_mcs5_usf_precode_table[usf]; |
| 1228 | |
| 1229 | for (i = 0; i < 4; i++) { |
| 1230 | gsm0503_mcs7_dl_burst_map(di, &bursts[i * 348], hi, up, i); |
| 1231 | gsm0503_mcs5_burst_swap((sbit_t *) &bursts[i * 348]); |
| 1232 | } |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1237 | 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] | 1238 | { |
| 1239 | int i, j; |
| 1240 | ubit_t upp[EGPRS_HDR_UPP_MAX], C[EGPRS_HDR_C_MAX]; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 1241 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1242 | |
| 1243 | code = &gsm0503_mcs_dl_codes[mcs]; |
| 1244 | |
| 1245 | osmo_pbit2ubit_ext(upp, 0, l2_data, code->usf_len, code->hdr_len, 1); |
| 1246 | osmo_crc8gen_set_bits(&gsm0503_mcs_crc8_hdr, upp, |
| 1247 | code->hdr_len, upp + code->hdr_len); |
| 1248 | |
| 1249 | osmo_conv_encode(code->hdr_conv, upp, C); |
| 1250 | |
| 1251 | /* MCS-5,6 header direct puncture instead of table */ |
| 1252 | if ((mcs == EGPRS_MCS5) || (mcs == EGPRS_MCS6)) { |
| 1253 | memcpy(hc, C, code->hdr_code_len); |
| 1254 | hc[99] = hc[98]; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | if (!code->hdr_punc) { |
| 1259 | /* Invalid MCS-X header puncture matrix */ |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
| 1263 | for (i = 0, j = 0; i < code->hdr_code_len; i++) { |
| 1264 | if (!code->hdr_punc[i]) |
| 1265 | hc[j++] = C[i]; |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1271 | 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] | 1272 | int mcs, int p, int blk) |
| 1273 | { |
| 1274 | int i, j, data_len; |
| 1275 | ubit_t u[EGPRS_DATA_U_MAX], C[EGPRS_DATA_C_MAX]; |
Harald Welte | 2f984ea | 2017-06-12 15:05:21 +0200 | [diff] [blame] | 1276 | const struct gsm0503_mcs_code *code; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1277 | |
| 1278 | code = &gsm0503_mcs_dl_codes[mcs]; |
| 1279 | |
| 1280 | /* |
| 1281 | * Dual block - MCS-7,8,9 |
| 1282 | * Single block - MCS-1,2,3,4,5,6 |
| 1283 | */ |
| 1284 | if (mcs >= EGPRS_MCS7) |
| 1285 | data_len = code->data_len / 2; |
| 1286 | else |
| 1287 | data_len = code->data_len; |
| 1288 | |
| 1289 | osmo_pbit2ubit_ext(u, 0, l2_data, |
| 1290 | code->usf_len + code->hdr_len + blk * data_len, data_len, 1); |
| 1291 | |
| 1292 | osmo_crc16gen_set_bits(&gsm0503_mcs_crc12, u, data_len, u + data_len); |
| 1293 | |
| 1294 | osmo_conv_encode(code->data_conv, u, C); |
| 1295 | |
| 1296 | if (!code->data_punc[p]) { |
| 1297 | /* Invalid MCS-X data puncture matrix */ |
| 1298 | return -1; |
| 1299 | } |
| 1300 | |
| 1301 | for (i = 0, j = 0; i < code->data_code_len; i++) { |
| 1302 | if (!code->data_punc[p][i]) |
| 1303 | c[j++] = C[i]; |
| 1304 | } |
| 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Parse EGPRS DL header for coding and puncturing scheme (CPS) |
| 1311 | * |
| 1312 | * Type 1 - MCS-7,8,9 |
| 1313 | * Type 2 - MCS-5,6 |
| 1314 | * Type 3 - MCS-1,2,3,4 |
| 1315 | */ |
| 1316 | static int egprs_parse_dl_cps(struct egprs_cps *cps, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1317 | const union gprs_rlc_dl_hdr_egprs *hdr, int type) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1318 | { |
| 1319 | uint8_t bits; |
| 1320 | |
| 1321 | switch (type) { |
| 1322 | case EGPRS_HDR_TYPE1: |
| 1323 | bits = hdr->type1.cps; |
| 1324 | break; |
| 1325 | case EGPRS_HDR_TYPE2: |
| 1326 | bits = hdr->type2.cps; |
| 1327 | break; |
| 1328 | case EGPRS_HDR_TYPE3: |
| 1329 | bits = hdr->type3.cps; |
| 1330 | break; |
| 1331 | default: |
| 1332 | return -1; |
| 1333 | } |
| 1334 | |
| 1335 | return egprs_get_cps(cps, type, bits); |
| 1336 | } |
| 1337 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1338 | /*! EGPRS DL message encoding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1339 | * \param[out] bursts caller-allocated buffer for unpacked burst bits |
| 1340 | * \param[in] l2_data L2 (MAC) block to be encoded |
| 1341 | * \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] | 1342 | * \returns number of bits encoded; negative on error */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1343 | int gsm0503_pdtch_egprs_encode(ubit_t *bursts, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1344 | const uint8_t *l2_data, uint8_t l2_len) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1345 | { |
| 1346 | ubit_t hc[EGPRS_DATA_C_MAX], dc[EGPRS_DATA_DC_MAX]; |
| 1347 | ubit_t c1[EGPRS_DATA_C1], c2[EGPRS_DATA_C2]; |
| 1348 | uint8_t mcs; |
| 1349 | struct egprs_cps cps; |
| 1350 | union gprs_rlc_dl_hdr_egprs *hdr; |
| 1351 | |
| 1352 | switch (l2_len) { |
| 1353 | case 27: |
| 1354 | mcs = EGPRS_MCS1; |
| 1355 | break; |
| 1356 | case 33: |
| 1357 | mcs = EGPRS_MCS2; |
| 1358 | break; |
| 1359 | case 42: |
| 1360 | mcs = EGPRS_MCS3; |
| 1361 | break; |
| 1362 | case 49: |
| 1363 | mcs = EGPRS_MCS4; |
| 1364 | break; |
| 1365 | case 60: |
| 1366 | mcs = EGPRS_MCS5; |
| 1367 | break; |
| 1368 | case 78: |
| 1369 | mcs = EGPRS_MCS6; |
| 1370 | break; |
| 1371 | case 118: |
| 1372 | mcs = EGPRS_MCS7; |
| 1373 | break; |
| 1374 | case 142: |
| 1375 | mcs = EGPRS_MCS8; |
| 1376 | break; |
| 1377 | case 154: |
| 1378 | mcs = EGPRS_MCS9; |
| 1379 | break; |
| 1380 | default: |
| 1381 | return -1; |
| 1382 | } |
| 1383 | |
| 1384 | /* Read header for USF and puncturing matrix selection. */ |
| 1385 | hdr = (union gprs_rlc_dl_hdr_egprs *) l2_data; |
| 1386 | |
| 1387 | switch (mcs) { |
| 1388 | case EGPRS_MCS1: |
| 1389 | case EGPRS_MCS2: |
| 1390 | case EGPRS_MCS3: |
| 1391 | case EGPRS_MCS4: |
| 1392 | /* Check for valid CPS and matching MCS to message size */ |
| 1393 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE3) < 0) || |
| 1394 | (cps.mcs != mcs)) |
| 1395 | goto bad_header; |
| 1396 | |
| 1397 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1398 | egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0); |
| 1399 | egprs_type3_map(bursts, hc, dc, hdr->type3.usf); |
| 1400 | break; |
| 1401 | case EGPRS_MCS5: |
| 1402 | case EGPRS_MCS6: |
| 1403 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE2) < 0) || |
| 1404 | (cps.mcs != mcs)) |
| 1405 | goto bad_header; |
| 1406 | |
| 1407 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1408 | egprs_encode_data(dc, l2_data, mcs, cps.p[0], 0); |
| 1409 | egprs_type2_map(bursts, hc, dc, hdr->type2.usf); |
| 1410 | break; |
| 1411 | case EGPRS_MCS7: |
| 1412 | case EGPRS_MCS8: |
| 1413 | case EGPRS_MCS9: |
| 1414 | if ((egprs_parse_dl_cps(&cps, hdr, EGPRS_HDR_TYPE1) < 0) || |
| 1415 | (cps.mcs != mcs)) |
| 1416 | goto bad_header; |
| 1417 | |
| 1418 | egprs_encode_hdr(hc, l2_data, mcs); |
| 1419 | egprs_encode_data(c1, l2_data, mcs, cps.p[0], 0); |
| 1420 | egprs_encode_data(c2, l2_data, mcs, cps.p[1], 1); |
| 1421 | egprs_type1_map(bursts, hc, c1, c2, hdr->type1.usf, mcs); |
| 1422 | break; |
| 1423 | } |
| 1424 | |
| 1425 | return mcs >= EGPRS_MCS5 ? |
| 1426 | GSM0503_EGPRS_BURSTS_NBITS : GSM0503_GPRS_BURSTS_NBITS; |
| 1427 | |
| 1428 | bad_header: |
| 1429 | /* Invalid EGPRS MCS-X header */ |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1433 | /*! GPRS DL message encoding |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1434 | * \param[out] bursts caller-allocated buffer for unpacked burst bits |
| 1435 | * \param[in] l2_data L2 (MAC) block to be encoded |
| 1436 | * \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] | 1437 | * \returns number of bits encoded; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1438 | 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] | 1439 | { |
| 1440 | ubit_t iB[456], cB[676]; |
| 1441 | const ubit_t *hl_hn; |
| 1442 | ubit_t conv[334]; |
| 1443 | int i, j, usf; |
| 1444 | |
| 1445 | switch (l2_len) { |
| 1446 | case 23: |
| 1447 | osmo_pbit2ubit_ext(conv, 0, l2_data, 0, 184, 1); |
| 1448 | |
| 1449 | osmo_crc64gen_set_bits(&gsm0503_fire_crc40, conv, 184, conv + 184); |
| 1450 | |
| 1451 | osmo_conv_encode(&gsm0503_xcch, conv, cB); |
| 1452 | |
| 1453 | hl_hn = gsm0503_pdtch_hl_hn_ubit[0]; |
| 1454 | |
| 1455 | break; |
| 1456 | case 34: |
| 1457 | osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 271, 1); |
| 1458 | usf = l2_data[0] & 0x7; |
| 1459 | |
| 1460 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3, |
| 1461 | 271, conv + 3 + 271); |
| 1462 | |
| 1463 | memcpy(conv, gsm0503_usf2six[usf], 6); |
| 1464 | |
| 1465 | osmo_conv_encode(&gsm0503_cs2_np, conv, cB); |
| 1466 | |
| 1467 | for (i = 0, j = 0; i < 588; i++) |
| 1468 | if (!gsm0503_puncture_cs2[i]) |
| 1469 | cB[j++] = cB[i]; |
| 1470 | |
| 1471 | hl_hn = gsm0503_pdtch_hl_hn_ubit[1]; |
| 1472 | |
| 1473 | break; |
| 1474 | case 40: |
| 1475 | osmo_pbit2ubit_ext(conv, 3, l2_data, 0, 315, 1); |
| 1476 | usf = l2_data[0] & 0x7; |
| 1477 | |
| 1478 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, conv + 3, |
| 1479 | 315, conv + 3 + 315); |
| 1480 | |
| 1481 | memcpy(conv, gsm0503_usf2six[usf], 6); |
| 1482 | |
| 1483 | osmo_conv_encode(&gsm0503_cs3_np, conv, cB); |
| 1484 | |
| 1485 | for (i = 0, j = 0; i < 676; i++) |
| 1486 | if (!gsm0503_puncture_cs3[i]) |
| 1487 | cB[j++] = cB[i]; |
| 1488 | |
| 1489 | hl_hn = gsm0503_pdtch_hl_hn_ubit[2]; |
| 1490 | |
| 1491 | break; |
| 1492 | case 54: |
| 1493 | osmo_pbit2ubit_ext(cB, 9, l2_data, 0, 431, 1); |
| 1494 | usf = l2_data[0] & 0x7; |
| 1495 | |
| 1496 | osmo_crc16gen_set_bits(&gsm0503_cs234_crc16, cB + 9, |
| 1497 | 431, cB + 9 + 431); |
| 1498 | |
| 1499 | memcpy(cB, gsm0503_usf2twelve_ubit[usf], 12); |
| 1500 | |
| 1501 | hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; |
| 1502 | |
| 1503 | break; |
| 1504 | default: |
| 1505 | return -1; |
| 1506 | } |
| 1507 | |
| 1508 | gsm0503_xcch_interleave(cB, iB); |
| 1509 | |
| 1510 | for (i = 0; i < 4; i++) { |
| 1511 | gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 1512 | hl_hn + i * 2, hl_hn + i * 2 + 1); |
| 1513 | } |
| 1514 | |
| 1515 | return GSM0503_GPRS_BURSTS_NBITS; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * GSM TCH/F FR/EFR transcoding |
| 1520 | */ |
| 1521 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1522 | /*! assemble a FR codec frame in format as used inside RTP |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1523 | * \param[out] tch_data Codec frame in RTP format |
| 1524 | * \param[in] b_bits Codec frame in 'native' format |
| 1525 | * \param[in] net_order FIXME */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1526 | static void tch_fr_reassemble(uint8_t *tch_data, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1527 | const ubit_t *b_bits, int net_order) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1528 | { |
| 1529 | int i, j, k, l, o; |
| 1530 | |
| 1531 | tch_data[0] = 0xd << 4; |
| 1532 | memset(tch_data + 1, 0, 32); |
| 1533 | |
| 1534 | if (net_order) { |
| 1535 | for (i = 0, j = 4; i < 260; i++, j++) |
| 1536 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1537 | |
| 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | /* reassemble d-bits */ |
| 1542 | i = 0; /* counts bits */ |
| 1543 | j = 4; /* counts output bits */ |
| 1544 | k = gsm0503_gsm_fr_map[0]-1; /* current number bit in element */ |
| 1545 | l = 0; /* counts element bits */ |
| 1546 | o = 0; /* offset input bits */ |
| 1547 | while (i < 260) { |
| 1548 | tch_data[j >> 3] |= (b_bits[k + o] << (7 - (j & 7))); |
| 1549 | if (--k < 0) { |
| 1550 | o += gsm0503_gsm_fr_map[l]; |
| 1551 | k = gsm0503_gsm_fr_map[++l]-1; |
| 1552 | } |
| 1553 | i++; |
| 1554 | j++; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | static void tch_fr_disassemble(ubit_t *b_bits, |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1559 | const uint8_t *tch_data, int net_order) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1560 | { |
| 1561 | int i, j, k, l, o; |
| 1562 | |
| 1563 | if (net_order) { |
| 1564 | for (i = 0, j = 4; i < 260; i++, j++) |
| 1565 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1566 | |
| 1567 | return; |
| 1568 | } |
| 1569 | |
| 1570 | i = 0; /* counts bits */ |
| 1571 | j = 4; /* counts input bits */ |
| 1572 | k = gsm0503_gsm_fr_map[0] - 1; /* current number bit in element */ |
| 1573 | l = 0; /* counts element bits */ |
| 1574 | o = 0; /* offset output bits */ |
| 1575 | while (i < 260) { |
| 1576 | b_bits[k + o] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1577 | if (--k < 0) { |
| 1578 | o += gsm0503_gsm_fr_map[l]; |
| 1579 | k = gsm0503_gsm_fr_map[++l] - 1; |
| 1580 | } |
| 1581 | i++; |
| 1582 | j++; |
| 1583 | } |
| 1584 | } |
| 1585 | |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 1586 | /* 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] | 1587 | 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] | 1588 | { |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 1589 | int i; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1590 | |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 1591 | memset(tch_data, 0, GSM_HR_BYTES); |
| 1592 | for (i = 0; i < 112; i++) |
| 1593 | tch_data[i >> 3] |= (b_bits[i] << (7 - (i & 7))); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1596 | 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] | 1597 | { |
Mychaela N. Falconia | 8d8d5af | 2023-05-23 16:41:05 +0000 | [diff] [blame] | 1598 | int i; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1599 | |
Mychaela N. Falconia | 8d8d5af | 2023-05-23 16:41:05 +0000 | [diff] [blame] | 1600 | for (i = 0; i < 112; i++) |
| 1601 | b_bits[i] = (tch_data[i >> 3] >> (7 - (i & 7))) & 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1604 | /* assemble a EFR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1605 | 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] | 1606 | { |
| 1607 | int i, j; |
| 1608 | |
| 1609 | tch_data[0] = 0xc << 4; |
| 1610 | memset(tch_data + 1, 0, 30); |
| 1611 | |
| 1612 | for (i = 0, j = 4; i < 244; i++, j++) |
| 1613 | tch_data[j >> 3] |= (b_bits[i] << (7 - (j & 7))); |
| 1614 | } |
| 1615 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1616 | 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] | 1617 | { |
| 1618 | int i, j; |
| 1619 | |
| 1620 | for (i = 0, j = 4; i < 244; i++, j++) |
| 1621 | b_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1622 | } |
| 1623 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1624 | /* assemble a AMR codec frame in format as used inside RTP */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1625 | 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] | 1626 | { |
| 1627 | int i, j; |
| 1628 | |
| 1629 | memset(tch_data, 0, (len + 7) >> 3); |
| 1630 | |
| 1631 | for (i = 0, j = 0; i < len; i++, j++) |
| 1632 | tch_data[j >> 3] |= (d_bits[i] << (7 - (j & 7))); |
| 1633 | } |
| 1634 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1635 | 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] | 1636 | { |
| 1637 | int i, j; |
| 1638 | |
| 1639 | for (i = 0, j = 0; i < len; i++, j++) |
| 1640 | d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; |
| 1641 | } |
| 1642 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 1643 | /* Append STI and MI bits to the SID_UPDATE frame, see also |
| 1644 | * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */ |
| 1645 | static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi) |
| 1646 | { |
| 1647 | /* Zero out the space that had been used by the CRC14 */ |
| 1648 | memset(sid_update + 35, 0, 14); |
| 1649 | |
| 1650 | /* Append STI and MI parameters */ |
| 1651 | sid_update[35] = sti & 1; |
| 1652 | sid_update[36] = mi & 1; |
| 1653 | sid_update[37] = mi >> 1 & 1; |
| 1654 | sid_update[38] = mi >> 2 & 1; |
| 1655 | } |
| 1656 | |
| 1657 | /* Extract a SID UPDATE fram the sbits of an FR AMR frame */ |
| 1658 | static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits) |
| 1659 | { |
| 1660 | |
| 1661 | unsigned int i; |
| 1662 | |
| 1663 | sbits += 32; |
| 1664 | |
| 1665 | for (i = 0; i < 53; i++) { |
| 1666 | sid_update[0] = sbits[0]; |
| 1667 | sid_update[1] = sbits[1]; |
| 1668 | sid_update[2] = sbits[2]; |
| 1669 | sid_update[3] = sbits[3]; |
| 1670 | sid_update += 4; |
| 1671 | sbits += 8; |
| 1672 | } |
| 1673 | |
| 1674 | } |
| 1675 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1676 | /* re-arrange according to TS 05.03 Table 2 (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1677 | 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] | 1678 | { |
| 1679 | int i; |
| 1680 | |
| 1681 | for (i = 0; i < 260; i++) |
| 1682 | b_bits[gsm610_bitorder[i]] = d_bits[i]; |
| 1683 | } |
| 1684 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1685 | /* re-arrange according to TS 05.03 Table 2 (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1686 | 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] | 1687 | { |
| 1688 | int i; |
| 1689 | |
| 1690 | for (i = 0; i < 260; i++) |
| 1691 | d_bits[i] = b_bits[gsm610_bitorder[i]]; |
| 1692 | } |
| 1693 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1694 | /* re-arrange according to TS 05.03 Table 3a (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1695 | 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] | 1696 | { |
| 1697 | int i; |
| 1698 | |
| 1699 | const uint16_t *map; |
| 1700 | |
| 1701 | if (!d_bits[93] && !d_bits[94]) |
| 1702 | map = gsm620_unvoiced_bitorder; |
| 1703 | else |
| 1704 | map = gsm620_voiced_bitorder; |
| 1705 | |
| 1706 | for (i = 0; i < 112; i++) |
| 1707 | b_bits[map[i]] = d_bits[i]; |
| 1708 | } |
| 1709 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1710 | /* re-arrange according to TS 05.03 Table 3a (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1711 | 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] | 1712 | { |
| 1713 | int i; |
| 1714 | const uint16_t *map; |
| 1715 | |
| 1716 | if (!b_bits[34] && !b_bits[35]) |
| 1717 | map = gsm620_unvoiced_bitorder; |
| 1718 | else |
| 1719 | map = gsm620_voiced_bitorder; |
| 1720 | |
| 1721 | for (i = 0; i < 112; i++) |
| 1722 | d_bits[i] = b_bits[map[i]]; |
| 1723 | } |
| 1724 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1725 | /* re-arrange according to TS 05.03 Table 6 (receiver) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1726 | 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] | 1727 | { |
| 1728 | int i; |
| 1729 | |
| 1730 | for (i = 0; i < 260; i++) |
| 1731 | b_bits[gsm660_bitorder[i]] = d_bits[i]; |
| 1732 | } |
| 1733 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1734 | /* re-arrange according to TS 05.03 Table 6 (transmitter) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1735 | 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] | 1736 | { |
| 1737 | int i; |
| 1738 | |
| 1739 | for (i = 0; i < 260; i++) |
| 1740 | d_bits[i] = b_bits[gsm660_bitorder[i]]; |
| 1741 | } |
| 1742 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1743 | /* extract the 65 protected class1a+1b bits */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1744 | 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] | 1745 | { |
| 1746 | int i; |
| 1747 | |
| 1748 | for (i = 0; i < 65; i++) |
| 1749 | b_bits[i] = s_bits[gsm0503_gsm_efr_protected_bits[i] - 1]; |
| 1750 | } |
| 1751 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1752 | 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] | 1753 | { |
| 1754 | int i; |
| 1755 | |
| 1756 | for (i = 0; i < 91; i++) { |
| 1757 | d[i << 1] = u[i]; |
| 1758 | d[(i << 1) + 1] = u[184 - i]; |
| 1759 | } |
| 1760 | |
| 1761 | for (i = 0; i < 3; i++) |
| 1762 | p[i] = u[91 + i]; |
| 1763 | } |
| 1764 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1765 | 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] | 1766 | { |
| 1767 | int i; |
| 1768 | |
| 1769 | for (i = 0; i < 91; i++) { |
| 1770 | u[i] = d[i << 1]; |
| 1771 | u[184 - i] = d[(i << 1) + 1]; |
| 1772 | } |
| 1773 | |
| 1774 | for (i = 0; i < 3; i++) |
| 1775 | u[91 + i] = p[i]; |
| 1776 | } |
| 1777 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1778 | 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] | 1779 | { |
| 1780 | memcpy(d, u, 95); |
| 1781 | memcpy(p, u + 95, 3); |
| 1782 | } |
| 1783 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1784 | 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] | 1785 | { |
| 1786 | memcpy(u, d, 95); |
| 1787 | memcpy(u + 95, p, 3); |
| 1788 | } |
| 1789 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1790 | 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] | 1791 | { |
| 1792 | memcpy(w, s, 71); |
| 1793 | w[71] = w[72] = s[69]; |
| 1794 | memcpy(w + 73, s + 71, 50); |
| 1795 | w[123] = w[124] = s[119]; |
| 1796 | memcpy(w + 125, s + 121, 53); |
| 1797 | w[178] = w[179] = s[172]; |
| 1798 | memcpy(w + 180, s + 174, 50); |
| 1799 | w[230] = w[231] = s[222]; |
| 1800 | memcpy(w + 232, s + 224, 20); |
| 1801 | memcpy(w + 252, p, 8); |
| 1802 | } |
| 1803 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1804 | 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] | 1805 | { |
| 1806 | int sum; |
| 1807 | |
| 1808 | memcpy(s, w, 71); |
| 1809 | sum = s[69] + w[71] + w[72]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1810 | s[69] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1811 | memcpy(s + 71, w + 73, 50); |
| 1812 | sum = s[119] + w[123] + w[124]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1813 | s[119] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1814 | memcpy(s + 121, w + 125, 53); |
| 1815 | sum = s[172] + w[178] + w[179]; |
Mychaela N. Falconia | f3e4745 | 2023-05-17 18:58:40 +0000 | [diff] [blame] | 1816 | s[172] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1817 | memcpy(s + 174, w + 180, 50); |
Niro Mahasinghe | 834e2ac | 2017-11-03 12:22:34 +0100 | [diff] [blame] | 1818 | sum = s[222] + w[230] + w[231]; |
Niro Mahasinghe | c526dbc | 2017-11-03 12:24:30 +0100 | [diff] [blame] | 1819 | s[222] = (sum >= 2); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1820 | memcpy(s + 224, w + 232, 20); |
| 1821 | memcpy(p, w + 252, 8); |
| 1822 | } |
| 1823 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | memcpy(u, d, prot); |
| 1827 | memcpy(u + prot, p, 6); |
| 1828 | memcpy(u + prot + 6, d + prot, len - prot); |
| 1829 | } |
| 1830 | |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1831 | 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] | 1832 | { |
| 1833 | memcpy(d, u, prot); |
| 1834 | memcpy(p, u + prot, 6); |
| 1835 | memcpy(d + prot, u + prot + 6, len - prot); |
| 1836 | } |
| 1837 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1838 | /*! Perform channel decoding of a FR/EFR channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1839 | * \param[out] tch_data Codec frame in RTP payload format |
| 1840 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 1841 | * \param[in] net_order FIXME |
| 1842 | * \param[in] efr Is this channel using EFR (1) or FR (0) |
| 1843 | * \param[out] n_errors Number of detected bit errors |
| 1844 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 1845 | * \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] | 1846 | 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] | 1847 | int net_order, int efr, int *n_errors, int *n_bits_total) |
| 1848 | { |
| 1849 | sbit_t iB[912], cB[456], h; |
| 1850 | ubit_t conv[185], s[244], w[260], b[65], d[260], p[8]; |
| 1851 | int i, rv, len, steal = 0; |
| 1852 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1853 | /* map from 8 bursts to interleaved data bits (iB) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1854 | for (i = 0; i < 8; i++) { |
| 1855 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 1856 | &bursts[i * 116], &h, i >> 2); |
| 1857 | steal -= h; |
| 1858 | } |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1859 | /* we now have the bits of the four bursts (interface 4 in |
| 1860 | * Figure 1a of TS 05.03 */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1861 | |
| 1862 | gsm0503_tch_fr_deinterleave(cB, iB); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1863 | /* 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] | 1864 | |
| 1865 | if (steal > 0) { |
| 1866 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 1867 | if (rv) { |
| 1868 | /* Error decoding FACCH frame */ |
| 1869 | return -1; |
| 1870 | } |
| 1871 | |
Vadim Yanitskiy | c0305b6 | 2023-05-20 16:33:38 +0700 | [diff] [blame] | 1872 | return GSM_MACBLOCK_LEN; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | 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] | 1876 | /* we now have the data bits 'u': interface 2 in Fig. 1a */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1877 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1878 | /* input: 'conv', output: d[ata] + p[arity] */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1879 | tch_fr_unreorder(d, p, conv); |
| 1880 | |
| 1881 | for (i = 0; i < 78; i++) |
| 1882 | d[i + 182] = (cB[i + 378] < 0) ? 1 : 0; |
| 1883 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1884 | /* check if parity of first 50 (class 1) 'd'-bits match 'p' */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1885 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d, 50, p); |
| 1886 | if (rv) { |
| 1887 | /* Error checking CRC8 for the FR part of an EFR/FR frame */ |
| 1888 | return -1; |
| 1889 | } |
| 1890 | |
| 1891 | if (efr) { |
| 1892 | tch_efr_d_to_w(w, d); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1893 | /* we now have the preliminary-coded bits w(k) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1894 | |
| 1895 | tch_efr_unreorder(s, p, w); |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1896 | /* we now have the data delivered to the preliminary |
| 1897 | * channel encoding unit s(k) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1898 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1899 | /* 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] | 1900 | tch_efr_protected(s, b); |
| 1901 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1902 | /* perform CRC-8 on 65 most important bits (50 bits of |
| 1903 | * class 1a + 15 bits of class 1b) */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1904 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_efr_crc8, b, 65, p); |
| 1905 | if (rv) { |
| 1906 | /* Error checking CRC8 for the EFR part of an EFR frame */ |
| 1907 | return -1; |
| 1908 | } |
| 1909 | |
| 1910 | tch_efr_reassemble(tch_data, s); |
| 1911 | |
| 1912 | len = GSM_EFR_BYTES; |
| 1913 | } else { |
| 1914 | tch_fr_d_to_b(w, d); |
| 1915 | |
| 1916 | tch_fr_reassemble(tch_data, w, net_order); |
| 1917 | |
| 1918 | len = GSM_FR_BYTES; |
| 1919 | } |
| 1920 | |
| 1921 | return len; |
| 1922 | } |
| 1923 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1924 | /*! 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] | 1925 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 1926 | * \param[in] tch_data Codec input data in RTP payload format |
| 1927 | * \param[in] len Length of \a tch_data in bytes |
| 1928 | * \param[in] net_order FIXME |
| 1929 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 1930 | 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] | 1931 | int len, int net_order) |
| 1932 | { |
| 1933 | ubit_t iB[912], cB[456], h; |
| 1934 | ubit_t conv[185], w[260], b[65], s[244], d[260], p[8]; |
| 1935 | int i; |
| 1936 | |
| 1937 | switch (len) { |
| 1938 | case GSM_EFR_BYTES: /* TCH EFR */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1939 | tch_efr_disassemble(s, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1940 | tch_efr_protected(s, b); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1941 | osmo_crc8gen_set_bits(&gsm0503_tch_efr_crc8, b, 65, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1942 | tch_efr_reorder(w, s, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1943 | tch_efr_w_to_d(d, w); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1944 | goto coding_efr_fr; |
| 1945 | case GSM_FR_BYTES: /* TCH FR */ |
| 1946 | tch_fr_disassemble(w, tch_data, net_order); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1947 | tch_fr_b_to_d(d, w); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1948 | coding_efr_fr: |
| 1949 | osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d, 50, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1950 | tch_fr_reorder(conv, d, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1951 | memcpy(cB + 378, d + 182, 78); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1952 | osmo_conv_encode(&gsm0503_tch_fr, conv, cB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1953 | h = 0; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1954 | break; |
Mychaela N. Falconia | 6e529a2 | 2023-05-27 11:06:57 +0000 | [diff] [blame] | 1955 | case 0: /* no data, induce BFI in the receiver */ |
| 1956 | /* Do the same thing that sysmoBTS PHY does when fed a 0-length |
| 1957 | * payload for DL: set all u(k) bits to 0, and do the same |
| 1958 | * with all class 2 bits. This operation is NOT the same as |
| 1959 | * an FR codec frame of all zero bits: with all-zeros d(k) input |
| 1960 | * the CRC3 function will produce 111 output, whereas we |
| 1961 | * transmit 000 in those parity bits too. The result will be |
| 1962 | * an induced BFI (bad frame indication) condition in the |
| 1963 | * receiver, for both TCH/FS and TCH/EFS decoders. */ |
| 1964 | memset(conv, 0, sizeof(conv)); |
| 1965 | memset(cB + 378, 0, 78); |
| 1966 | osmo_conv_encode(&gsm0503_tch_fr, conv, cB); |
| 1967 | h = 0; |
| 1968 | break; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1969 | case GSM_MACBLOCK_LEN: /* FACCH */ |
| 1970 | _xcch_encode_cB(cB, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1971 | h = 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 1972 | break; |
| 1973 | default: |
| 1974 | return -1; |
| 1975 | } |
| 1976 | |
| 1977 | gsm0503_tch_fr_interleave(cB, iB); |
| 1978 | |
| 1979 | for (i = 0; i < 8; i++) { |
| 1980 | gsm0503_tch_burst_map(&iB[i * 114], |
| 1981 | &bursts[i * 116], &h, i >> 2); |
| 1982 | } |
| 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1987 | /*! 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] | 1988 | * \param[out] tch_data Codec frame in TS 101 318 canonical format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 1989 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 1990 | * \param[in] odd Odd (1) or even (0) frame number |
| 1991 | * \param[out] n_errors Number of detected bit errors |
| 1992 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 1993 | * \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] | 1994 | 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] | 1995 | int *n_errors, int *n_bits_total) |
| 1996 | { |
| 1997 | sbit_t iB[912], cB[456], h; |
| 1998 | ubit_t conv[98], b[112], d[112], p[3]; |
| 1999 | int i, rv, steal = 0; |
| 2000 | |
| 2001 | /* Only unmap the stealing bits */ |
| 2002 | if (!odd) { |
| 2003 | for (i = 0; i < 4; i++) { |
| 2004 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0); |
| 2005 | steal -= h; |
| 2006 | } |
| 2007 | |
Mychaela N. Falconia | 920c0ed | 2023-05-23 14:06:53 +0000 | [diff] [blame] | 2008 | for (i = 2; i < 6; i++) { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2009 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1); |
| 2010 | steal -= h; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | /* If we found a stole FACCH, but only at correct alignment */ |
| 2015 | if (steal > 0) { |
| 2016 | for (i = 0; i < 6; i++) { |
| 2017 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2018 | &bursts[i * 116], NULL, i >> 2); |
| 2019 | } |
| 2020 | |
| 2021 | for (i = 2; i < 4; i++) { |
| 2022 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], |
| 2023 | &bursts[i * 116], NULL, 1); |
| 2024 | } |
| 2025 | |
| 2026 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2027 | |
| 2028 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2029 | if (rv) { |
| 2030 | /* Error decoding FACCH frame */ |
| 2031 | return -1; |
| 2032 | } |
| 2033 | |
| 2034 | return GSM_MACBLOCK_LEN; |
| 2035 | } |
| 2036 | |
| 2037 | for (i = 0; i < 4; i++) { |
| 2038 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2039 | &bursts[i * 116], NULL, i >> 1); |
| 2040 | } |
| 2041 | |
| 2042 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2043 | |
| 2044 | osmo_conv_decode_ber(&gsm0503_tch_hr, cB, conv, n_errors, n_bits_total); |
| 2045 | |
| 2046 | tch_hr_unreorder(d, p, conv); |
| 2047 | |
| 2048 | for (i = 0; i < 17; i++) |
| 2049 | d[i + 95] = (cB[i + 211] < 0) ? 1 : 0; |
| 2050 | |
| 2051 | rv = osmo_crc8gen_check_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p); |
| 2052 | if (rv) { |
| 2053 | /* Error checking CRC8 for an HR frame */ |
| 2054 | return -1; |
| 2055 | } |
| 2056 | |
| 2057 | tch_hr_d_to_b(b, d); |
| 2058 | |
| 2059 | tch_hr_reassemble(tch_data, b); |
| 2060 | |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 2061 | return GSM_HR_BYTES; |
| 2062 | } |
| 2063 | |
| 2064 | /*! Perform channel decoding of a HR(v1) channel according TS 05.03, |
| 2065 | * deprecated legacy API. |
| 2066 | * \param[out] tch_data Codec frame in pseudo-RFC5993 format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2067 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Mychaela N. Falconia | 57a3b3a | 2023-05-23 16:09:27 +0000 | [diff] [blame] | 2068 | * \param[in] odd Odd (1) or even (0) frame number |
| 2069 | * \param[out] n_errors Number of detected bit errors |
| 2070 | * \param[out] n_bits_total Total number of bits |
| 2071 | * \returns length of bytes used in \a tch_data output buffer; negative on error |
| 2072 | * |
| 2073 | * The HR1 codec frame format returned by this function is pseudo-RFC5993, |
| 2074 | * not true RFC 5993, as there is no SID classification being done |
| 2075 | * and the FT bits in the ToC octet are always set to 0 - but this |
| 2076 | * arguably-bogus format is the legacy public API. |
| 2077 | */ |
| 2078 | int gsm0503_tch_hr_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, |
| 2079 | int *n_errors, int *n_bits_total) |
| 2080 | { |
| 2081 | int rc; |
| 2082 | |
| 2083 | rc = gsm0503_tch_hr_decode2(tch_data, bursts, odd, n_errors, |
| 2084 | n_bits_total); |
| 2085 | if (rc != GSM_HR_BYTES) |
| 2086 | return rc; |
| 2087 | memmove(tch_data + 1, tch_data, GSM_HR_BYTES); |
| 2088 | tch_data[0] = 0x00; /* FT=0, note absence of SID classification */ |
| 2089 | return GSM_HR_BYTES_RTP_RFC5993; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2092 | /*! 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] | 2093 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2094 | * \param[in] tch_data Codec input data in RTP payload format |
| 2095 | * \param[in] len Length of \a tch_data in bytes |
| 2096 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2097 | 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] | 2098 | { |
| 2099 | ubit_t iB[912], cB[456], h; |
| 2100 | ubit_t conv[98], b[112], d[112], p[3]; |
| 2101 | int i; |
| 2102 | |
| 2103 | switch (len) { |
Mychaela N. Falconia | 8d8d5af | 2023-05-23 16:41:05 +0000 | [diff] [blame] | 2104 | case GSM_HR_BYTES_RTP_RFC5993: /* TCH HR with RFC 5993 prefix */ |
| 2105 | tch_data++; |
| 2106 | /* fall-through */ |
| 2107 | case GSM_HR_BYTES: /* TCH HR in "pure" form */ |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2108 | tch_hr_disassemble(b, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2109 | tch_hr_b_to_d(d, b); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2110 | osmo_crc8gen_set_bits(&gsm0503_tch_fr_crc3, d + 73, 22, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2111 | tch_hr_reorder(conv, d, p); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2112 | memcpy(cB + 211, d + 95, 17); |
Mychaela N. Falconia | 6e529a2 | 2023-05-27 11:06:57 +0000 | [diff] [blame] | 2113 | hr_conv_coding: |
| 2114 | osmo_conv_encode(&gsm0503_tch_hr, conv, cB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2115 | h = 0; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2116 | gsm0503_tch_hr_interleave(cB, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2117 | for (i = 0; i < 4; i++) { |
| 2118 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2119 | &bursts[i * 116], &h, i >> 1); |
| 2120 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2121 | break; |
Mychaela N. Falconia | 6e529a2 | 2023-05-27 11:06:57 +0000 | [diff] [blame] | 2122 | case 0: /* no data, induce BFI in the receiver */ |
| 2123 | /* see comments in gsm0503_tch_fr_encode() - same deal here */ |
| 2124 | memset(conv, 0, sizeof(conv)); |
| 2125 | memset(cB + 211, 0, 17); |
| 2126 | goto hr_conv_coding; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2127 | case GSM_MACBLOCK_LEN: /* FACCH */ |
| 2128 | _xcch_encode_cB(cB, tch_data); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2129 | h = 1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2130 | gsm0503_tch_fr_interleave(cB, iB); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2131 | for (i = 0; i < 6; i++) { |
| 2132 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2133 | &bursts[i * 116], &h, i >> 2); |
| 2134 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2135 | for (i = 2; i < 4; i++) { |
| 2136 | gsm0503_tch_burst_map(&iB[i * 114 + 456], |
| 2137 | &bursts[i * 116], &h, 1); |
| 2138 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2139 | break; |
| 2140 | default: |
| 2141 | return -1; |
| 2142 | } |
| 2143 | |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2147 | /* TCH/AFS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */ |
| 2148 | static uint8_t gsm0503_tch_afs_decode_inband(const sbit_t *cB) |
| 2149 | { |
| 2150 | unsigned int id = 0, best = 0; |
| 2151 | unsigned int i, j, k; |
| 2152 | |
| 2153 | for (i = 0; i < 4; i++) { |
| 2154 | /* FIXME: why not using remaining (16 - 8) soft-bits here? */ |
| 2155 | for (j = 0, k = 0; j < 8; j++) |
| 2156 | k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j])); |
| 2157 | |
| 2158 | if (i == 0 || k < best) { |
| 2159 | best = k; |
| 2160 | id = i; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | return id; |
| 2165 | } |
| 2166 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2167 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2168 | * \param[out] tch_data Codec frame in RTP payload format |
| 2169 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2170 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2171 | * \param[in] codec array of active codecs (active codec set) |
| 2172 | * \param[in] codecs number of codecs in \a codec |
| 2173 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2174 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2175 | * \param[out] n_errors Number of detected bit errors |
| 2176 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2177 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2178 | * codec out of range; negative on error |
| 2179 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2180 | 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] | 2181 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2182 | uint8_t *cmr, int *n_errors, int *n_bits_total) |
| 2183 | { |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2184 | return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req, |
| 2185 | codec, codecs, ft, cmr, n_errors, |
| 2186 | n_bits_total, NULL); |
| 2187 | } |
| 2188 | |
| 2189 | /*! Perform channel decoding of a TCH/AFS channel according TS 05.03 |
| 2190 | * \param[out] tch_data Codec frame in RTP payload format |
| 2191 | * \param[in] bursts buffer containing the symbols of 8 bursts |
| 2192 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2193 | * \param[in] codec array of active codecs (active codec set) |
| 2194 | * \param[in] codecs number of codecs in \a codec |
| 2195 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2196 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2197 | * \param[out] n_errors Number of detected bit errors |
| 2198 | * \param[out] n_bits_total Total number of bits |
| 2199 | * \param[inout] dtx DTX frame type output, previous DTX frame type input |
| 2200 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2201 | * codec out of range; negative on error |
| 2202 | */ |
| 2203 | int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, |
| 2204 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2205 | uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) |
| 2206 | { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2207 | sbit_t iB[912], cB[456], h; |
| 2208 | ubit_t d[244], p[6], conv[250]; |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2209 | int i, rv, len, steal = 0, id = -1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2210 | *n_errors = 0; *n_bits_total = 0; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2211 | static ubit_t sid_first_dummy[64] = { 0 }; |
| 2212 | sbit_t sid_update_enc[256]; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2213 | |
| 2214 | for (i=0; i<8; i++) { |
| 2215 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 2216 | steal -= h; |
| 2217 | } |
| 2218 | |
| 2219 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2220 | |
| 2221 | if (steal > 0) { |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2222 | /* If not NULL, dtx indicates type of previously decoded TCH/AFS frame. |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2223 | * 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] | 2224 | * reached in case of FACCH. Reset it here to avoid FACCH/F frames being |
| 2225 | * misinterpreted as AMR's special DTX frames. */ |
| 2226 | if (dtx != NULL) |
| 2227 | *dtx = AMR_OTHER; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2228 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2229 | if (rv) { |
| 2230 | /* Error decoding FACCH frame */ |
| 2231 | return -1; |
| 2232 | } |
| 2233 | |
| 2234 | return GSM_MACBLOCK_LEN; |
| 2235 | } |
| 2236 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2237 | /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ |
| 2238 | if (dtx) { |
Vadim Yanitskiy | bf2d5e9 | 2022-05-15 15:58:10 +0300 | [diff] [blame] | 2239 | const enum gsm0503_amr_dtx_frames dtx_prev = *dtx; |
| 2240 | |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2241 | *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] | 2242 | |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2243 | switch (*dtx) { |
| 2244 | case AMR_OTHER: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2245 | /* NOTE: The AFS_SID_UPDATE frame is splitted into |
| 2246 | * two half rate frames. If the id marker frame |
| 2247 | * (AFS_SID_UPDATE) is detected the following frame |
| 2248 | * contains the actual comfort noised data part of |
| 2249 | * (AFS_SID_UPDATE_CN). */ |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2250 | if (dtx_prev != AFS_SID_UPDATE) |
| 2251 | break; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2252 | /* TODO: parse CMI _and_ CMC/CMR (16 + 16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2253 | *dtx = AFS_SID_UPDATE_CN; |
| 2254 | |
| 2255 | extract_afs_sid_update(sid_update_enc, cB); |
| 2256 | osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, |
| 2257 | sid_update_enc, conv, n_errors, |
| 2258 | n_bits_total); |
| 2259 | rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, |
| 2260 | 35, conv + 35); |
| 2261 | if (rv != 0) { |
| 2262 | /* Error checking CRC14 for an AMR SID_UPDATE frame */ |
| 2263 | return -1; |
| 2264 | } |
| 2265 | |
| 2266 | tch_amr_sid_update_append(conv, 1, |
| 2267 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2268 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2269 | tch_amr_reassemble(tch_data, conv, 39); |
| 2270 | len = 5; |
| 2271 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2272 | case AFS_SID_FIRST: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2273 | tch_amr_sid_update_append(sid_first_dummy, 0, |
| 2274 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2275 | : codec[id > 0 ? id : 0]); |
Vadim Yanitskiy | 7d121cb | 2023-06-23 16:57:48 +0700 | [diff] [blame] | 2276 | tch_amr_reassemble(tch_data, sid_first_dummy, 39); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2277 | len = 5; |
| 2278 | goto out; |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2279 | 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] | 2280 | case AFS_ONSET: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2281 | len = 0; |
| 2282 | goto out; |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2283 | default: |
| 2284 | break; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2288 | /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */ |
| 2289 | if ((id = gsm0503_tch_afs_decode_inband(&cB[0])) >= codecs) { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2290 | /* Codec mode out of range, return id */ |
| 2291 | return id; |
| 2292 | } |
| 2293 | |
| 2294 | switch ((codec_mode_req) ? codec[*ft] : codec[id]) { |
| 2295 | case 7: /* TCH/AFS12.2 */ |
| 2296 | osmo_conv_decode_ber(&gsm0503_tch_afs_12_2, cB + 8, |
| 2297 | conv, n_errors, n_bits_total); |
| 2298 | |
| 2299 | tch_amr_unmerge(d, p, conv, 244, 81); |
| 2300 | |
| 2301 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 81, p); |
| 2302 | if (rv) { |
| 2303 | /* Error checking CRC8 for an AMR 12.2 frame */ |
| 2304 | return -1; |
| 2305 | } |
| 2306 | |
| 2307 | tch_amr_reassemble(tch_data, d, 244); |
| 2308 | |
| 2309 | len = 31; |
| 2310 | |
| 2311 | break; |
| 2312 | case 6: /* TCH/AFS10.2 */ |
| 2313 | osmo_conv_decode_ber(&gsm0503_tch_afs_10_2, cB + 8, |
| 2314 | conv, n_errors, n_bits_total); |
| 2315 | |
| 2316 | tch_amr_unmerge(d, p, conv, 204, 65); |
| 2317 | |
| 2318 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 65, p); |
| 2319 | if (rv) { |
| 2320 | /* Error checking CRC8 for an AMR 10.2 frame */ |
| 2321 | return -1; |
| 2322 | } |
| 2323 | |
| 2324 | tch_amr_reassemble(tch_data, d, 204); |
| 2325 | |
| 2326 | len = 26; |
| 2327 | |
| 2328 | break; |
| 2329 | case 5: /* TCH/AFS7.95 */ |
| 2330 | osmo_conv_decode_ber(&gsm0503_tch_afs_7_95, cB + 8, |
| 2331 | conv, n_errors, n_bits_total); |
| 2332 | |
| 2333 | tch_amr_unmerge(d, p, conv, 159, 75); |
| 2334 | |
| 2335 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 75, p); |
| 2336 | if (rv) { |
| 2337 | /* Error checking CRC8 for an AMR 7.95 frame */ |
| 2338 | return -1; |
| 2339 | } |
| 2340 | |
| 2341 | tch_amr_reassemble(tch_data, d, 159); |
| 2342 | |
| 2343 | len = 20; |
| 2344 | |
| 2345 | break; |
| 2346 | case 4: /* TCH/AFS7.4 */ |
| 2347 | osmo_conv_decode_ber(&gsm0503_tch_afs_7_4, cB + 8, |
| 2348 | conv, n_errors, n_bits_total); |
| 2349 | |
| 2350 | tch_amr_unmerge(d, p, conv, 148, 61); |
| 2351 | |
| 2352 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2353 | if (rv) { |
| 2354 | /* Error checking CRC8 for an AMR 7.4 frame */ |
| 2355 | return -1; |
| 2356 | } |
| 2357 | |
| 2358 | tch_amr_reassemble(tch_data, d, 148); |
| 2359 | |
| 2360 | len = 19; |
| 2361 | |
| 2362 | break; |
| 2363 | case 3: /* TCH/AFS6.7 */ |
| 2364 | osmo_conv_decode_ber(&gsm0503_tch_afs_6_7, cB + 8, |
| 2365 | conv, n_errors, n_bits_total); |
| 2366 | |
| 2367 | tch_amr_unmerge(d, p, conv, 134, 55); |
| 2368 | |
| 2369 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2370 | if (rv) { |
| 2371 | /* Error checking CRC8 for an AMR 6.7 frame */ |
| 2372 | return -1; |
| 2373 | } |
| 2374 | |
| 2375 | tch_amr_reassemble(tch_data, d, 134); |
| 2376 | |
| 2377 | len = 17; |
| 2378 | |
| 2379 | break; |
| 2380 | case 2: /* TCH/AFS5.9 */ |
| 2381 | osmo_conv_decode_ber(&gsm0503_tch_afs_5_9, cB + 8, |
| 2382 | conv, n_errors, n_bits_total); |
| 2383 | |
| 2384 | tch_amr_unmerge(d, p, conv, 118, 55); |
| 2385 | |
| 2386 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2387 | if (rv) { |
| 2388 | /* Error checking CRC8 for an AMR 5.9 frame */ |
| 2389 | return -1; |
| 2390 | } |
| 2391 | |
| 2392 | tch_amr_reassemble(tch_data, d, 118); |
| 2393 | |
| 2394 | len = 15; |
| 2395 | |
| 2396 | break; |
| 2397 | case 1: /* TCH/AFS5.15 */ |
| 2398 | osmo_conv_decode_ber(&gsm0503_tch_afs_5_15, cB + 8, |
| 2399 | conv, n_errors, n_bits_total); |
| 2400 | |
| 2401 | tch_amr_unmerge(d, p, conv, 103, 49); |
| 2402 | |
| 2403 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2404 | if (rv) { |
| 2405 | /* Error checking CRC8 for an AMR 5.15 frame */ |
| 2406 | return -1; |
| 2407 | } |
| 2408 | |
| 2409 | tch_amr_reassemble(tch_data, d, 103); |
| 2410 | |
| 2411 | len = 13; |
| 2412 | |
| 2413 | break; |
| 2414 | case 0: /* TCH/AFS4.75 */ |
| 2415 | osmo_conv_decode_ber(&gsm0503_tch_afs_4_75, cB + 8, |
| 2416 | conv, n_errors, n_bits_total); |
| 2417 | |
| 2418 | tch_amr_unmerge(d, p, conv, 95, 39); |
| 2419 | |
| 2420 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2421 | if (rv) { |
| 2422 | /* Error checking CRC8 for an AMR 4.75 frame */ |
| 2423 | return -1; |
| 2424 | } |
| 2425 | |
| 2426 | tch_amr_reassemble(tch_data, d, 95); |
| 2427 | |
| 2428 | len = 12; |
| 2429 | |
| 2430 | break; |
| 2431 | default: |
| 2432 | /* Unknown frame type */ |
| 2433 | *n_bits_total = 448; |
| 2434 | *n_errors = *n_bits_total; |
| 2435 | return -1; |
| 2436 | } |
| 2437 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2438 | out: |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2439 | /* Change codec request / indication, if frame is valid */ |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2440 | if (id != -1) { |
| 2441 | if (codec_mode_req) |
| 2442 | *cmr = id; |
| 2443 | else |
| 2444 | *ft = id; |
| 2445 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2446 | |
| 2447 | return len; |
| 2448 | } |
| 2449 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2450 | /*! 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] | 2451 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2452 | * \param[in] tch_data Codec input data in RTP payload format |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2453 | * \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] | 2454 | * \param[in] codec_mode_req Use CMR (1) or FT (0) |
| 2455 | * \param[in] codec Array of codecs (active codec set) |
| 2456 | * \param[in] codecs Number of entries in \a codec |
| 2457 | * \param[in] ft Frame Type to be used for encoding (index to \a codec) |
| 2458 | * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only) |
| 2459 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2460 | 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] | 2461 | 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] | 2462 | uint8_t cmr) |
| 2463 | { |
| 2464 | ubit_t iB[912], cB[456], h; |
| 2465 | ubit_t d[244], p[6], conv[250]; |
| 2466 | int i; |
| 2467 | uint8_t id; |
| 2468 | |
| 2469 | if (len == GSM_MACBLOCK_LEN) { /* FACCH */ |
| 2470 | _xcch_encode_cB(cB, tch_data); |
| 2471 | |
| 2472 | h = 1; |
| 2473 | |
| 2474 | goto facch; |
| 2475 | } |
| 2476 | |
| 2477 | h = 0; |
| 2478 | |
Vadim Yanitskiy | a995269 | 2023-11-10 03:31:55 +0700 | [diff] [blame] | 2479 | id = codec_mode_req ? cmr : ft; |
| 2480 | if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_afs_ic_ubit))) |
| 2481 | return -1; |
| 2482 | if (OSMO_UNLIKELY(ft >= codecs)) |
| 2483 | return -1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2484 | |
| 2485 | switch (codec[ft]) { |
| 2486 | case 7: /* TCH/AFS12.2 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2487 | if (!len) { |
| 2488 | /* No data, induce BFI in the receiver by inverted CRC bits. |
| 2489 | * The data bit are all 0, so the correct parity bits would be 111111. */ |
| 2490 | memset(d, 0, 244); |
| 2491 | memset(p, 0, 6); |
| 2492 | } else { |
| 2493 | if (len != 31) |
| 2494 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2495 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2496 | tch_amr_disassemble(d, tch_data, 244); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2497 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2498 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 81, p); |
| 2499 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2500 | |
| 2501 | tch_amr_merge(conv, d, p, 244, 81); |
| 2502 | |
| 2503 | osmo_conv_encode(&gsm0503_tch_afs_12_2, conv, cB + 8); |
| 2504 | |
| 2505 | break; |
| 2506 | case 6: /* TCH/AFS10.2 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2507 | if (!len) { |
| 2508 | /* See comment above. */ |
| 2509 | memset(d, 0, 204); |
| 2510 | memset(p, 0, 6); |
| 2511 | } else { |
| 2512 | if (len != 26) |
| 2513 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2514 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2515 | tch_amr_disassemble(d, tch_data, 204); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2516 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2517 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 65, p); |
| 2518 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2519 | |
| 2520 | tch_amr_merge(conv, d, p, 204, 65); |
| 2521 | |
| 2522 | osmo_conv_encode(&gsm0503_tch_afs_10_2, conv, cB + 8); |
| 2523 | |
| 2524 | break; |
| 2525 | case 5: /* TCH/AFS7.95 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2526 | if (!len) { |
| 2527 | /* See comment above. */ |
| 2528 | memset(d, 0, 159); |
| 2529 | memset(p, 0, 6); |
| 2530 | } else { |
| 2531 | if (len != 20) |
| 2532 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2533 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2534 | tch_amr_disassemble(d, tch_data, 159); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2535 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2536 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 75, p); |
| 2537 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2538 | |
| 2539 | tch_amr_merge(conv, d, p, 159, 75); |
| 2540 | |
| 2541 | osmo_conv_encode(&gsm0503_tch_afs_7_95, conv, cB + 8); |
| 2542 | |
| 2543 | break; |
| 2544 | case 4: /* TCH/AFS7.4 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2545 | if (!len) { |
| 2546 | /* See comment above. */ |
| 2547 | memset(d, 0, 148); |
| 2548 | memset(p, 0, 6); |
| 2549 | } else { |
| 2550 | if (len != 19) |
| 2551 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2552 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2553 | tch_amr_disassemble(d, tch_data, 148); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2554 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2555 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2556 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2557 | |
| 2558 | tch_amr_merge(conv, d, p, 148, 61); |
| 2559 | |
| 2560 | osmo_conv_encode(&gsm0503_tch_afs_7_4, conv, cB + 8); |
| 2561 | |
| 2562 | break; |
| 2563 | case 3: /* TCH/AFS6.7 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2564 | if (!len) { |
| 2565 | /* See comment above. */ |
| 2566 | memset(d, 0, 134); |
| 2567 | memset(p, 0, 6); |
| 2568 | } else { |
| 2569 | if (len != 17) |
| 2570 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2571 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2572 | tch_amr_disassemble(d, tch_data, 134); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2573 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2574 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2575 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2576 | |
| 2577 | tch_amr_merge(conv, d, p, 134, 55); |
| 2578 | |
| 2579 | osmo_conv_encode(&gsm0503_tch_afs_6_7, conv, cB + 8); |
| 2580 | |
| 2581 | break; |
| 2582 | case 2: /* TCH/AFS5.9 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2583 | if (!len) { |
| 2584 | /* See comment above. */ |
| 2585 | memset(d, 0, 118); |
| 2586 | memset(p, 0, 6); |
| 2587 | } else { |
| 2588 | if (len != 15) |
| 2589 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2590 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2591 | tch_amr_disassemble(d, tch_data, 118); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2592 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2593 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2594 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2595 | |
| 2596 | tch_amr_merge(conv, d, p, 118, 55); |
| 2597 | |
| 2598 | osmo_conv_encode(&gsm0503_tch_afs_5_9, conv, cB + 8); |
| 2599 | |
| 2600 | break; |
| 2601 | case 1: /* TCH/AFS5.15 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2602 | if (!len) { |
| 2603 | /* See comment above. */ |
| 2604 | memset(d, 0, 103); |
| 2605 | memset(p, 0, 6); |
| 2606 | } else { |
| 2607 | if (len != 13) |
| 2608 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2609 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2610 | tch_amr_disassemble(d, tch_data, 103); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2611 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2612 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2613 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2614 | |
| 2615 | tch_amr_merge(conv, d, p, 103, 49); |
| 2616 | |
| 2617 | osmo_conv_encode(&gsm0503_tch_afs_5_15, conv, cB + 8); |
| 2618 | |
| 2619 | break; |
| 2620 | case 0: /* TCH/AFS4.75 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2621 | if (!len) { |
| 2622 | /* See comment above. */ |
| 2623 | memset(d, 0, 95); |
| 2624 | memset(p, 0, 6); |
| 2625 | } else { |
| 2626 | if (len != 12) |
| 2627 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2628 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2629 | tch_amr_disassemble(d, tch_data, 95); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2630 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2631 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2632 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2633 | |
| 2634 | tch_amr_merge(conv, d, p, 95, 39); |
| 2635 | |
| 2636 | osmo_conv_encode(&gsm0503_tch_afs_4_75, conv, cB + 8); |
| 2637 | |
| 2638 | break; |
| 2639 | default: |
| 2640 | /* FIXME: FT %ft is not supported */ |
| 2641 | return -1; |
| 2642 | } |
| 2643 | |
| 2644 | memcpy(cB, gsm0503_afs_ic_ubit[id], 8); |
| 2645 | |
| 2646 | facch: |
| 2647 | gsm0503_tch_fr_interleave(cB, iB); |
| 2648 | |
| 2649 | for (i = 0; i < 8; i++) { |
| 2650 | gsm0503_tch_burst_map(&iB[i * 114], |
| 2651 | &bursts[i * 116], &h, i >> 2); |
| 2652 | } |
| 2653 | |
| 2654 | return 0; |
| 2655 | |
| 2656 | invalid_length: |
| 2657 | /* FIXME: payload length %len does not comply with codec type %ft */ |
| 2658 | return -1; |
| 2659 | } |
| 2660 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2661 | /* TCH/AHS: parse codec ID (CMI or CMC/CMR) from coded in-band data (16 bit) */ |
| 2662 | static uint8_t gsm0503_tch_ahs_decode_inband(const sbit_t *cB) |
| 2663 | { |
| 2664 | unsigned int id = 0, best = 0; |
| 2665 | unsigned int i, j, k; |
| 2666 | |
| 2667 | for (i = 0, k = 0; i < 4; i++) { |
| 2668 | /* FIXME: why not using remaining (16 - 4) soft-bits here? */ |
| 2669 | for (j = 0, k = 0; j < 4; j++) |
| 2670 | k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j])); |
| 2671 | |
| 2672 | if (i == 0 || k < best) { |
| 2673 | best = k; |
| 2674 | id = i; |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | return id; |
| 2679 | } |
| 2680 | |
Vadim Yanitskiy | 8fb5647 | 2023-05-24 01:26:02 +0700 | [diff] [blame] | 2681 | /*! Perform channel decoding of a TCH/AHS channel according TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2682 | * \param[out] tch_data Codec frame in RTP payload format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2683 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 2684 | * \param[in] odd Is this an odd (1) or even (0) frame number? |
| 2685 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2686 | * \param[in] codec array of active codecs (active codec set) |
| 2687 | * \param[in] codecs number of codecs in \a codec |
| 2688 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2689 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2690 | * \param[out] n_errors Number of detected bit errors |
| 2691 | * \param[out] n_bits_total Total number of bits |
Pau Espin Pedrol | f81d03f | 2018-07-19 13:49:41 +0200 | [diff] [blame] | 2692 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2693 | * codec out of range; negative on error |
| 2694 | */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2695 | 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] | 2696 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2697 | uint8_t *cmr, int *n_errors, int *n_bits_total) |
| 2698 | { |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2699 | return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req, |
| 2700 | codec, codecs, ft, cmr, n_errors, |
| 2701 | n_bits_total, NULL); |
| 2702 | } |
| 2703 | |
Vadim Yanitskiy | 8fb5647 | 2023-05-24 01:26:02 +0700 | [diff] [blame] | 2704 | /*! Perform channel decoding of a TCH/AHS channel according TS 05.03 |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2705 | * \param[out] tch_data Codec frame in RTP payload format |
Vadim Yanitskiy | 98431bb | 2023-05-24 01:21:46 +0700 | [diff] [blame] | 2706 | * \param[in] bursts buffer containing the symbols of 6 bursts |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2707 | * \param[in] odd Is this an odd (1) or even (0) frame number? |
| 2708 | * \param[in] codec_mode_req is this CMR (1) or CMC (0) |
| 2709 | * \param[in] codec array of active codecs (active codec set) |
| 2710 | * \param[in] codecs number of codecs in \a codec |
| 2711 | * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise |
| 2712 | * \param[out] cmr Output in \a codec_mode_req = 1 |
| 2713 | * \param[out] n_errors Number of detected bit errors |
| 2714 | * \param[out] n_bits_total Total number of bits |
| 2715 | * \param[inout] dtx DTX frame type output, previous DTX frame type input |
| 2716 | * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) |
| 2717 | * codec out of range; negative on error |
| 2718 | */ |
| 2719 | int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd, |
| 2720 | int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, |
| 2721 | uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) |
| 2722 | { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2723 | sbit_t iB[912], cB[456], h; |
| 2724 | ubit_t d[244], p[6], conv[135]; |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2725 | int i, rv, len, steal = 0, id = -1; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2726 | static ubit_t sid_first_dummy[64] = { 0 }; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2727 | |
| 2728 | /* only unmap the stealing bits */ |
| 2729 | if (!odd) { |
| 2730 | for (i = 0; i < 4; i++) { |
| 2731 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 0); |
| 2732 | steal -= h; |
| 2733 | } |
| 2734 | for (i = 2; i < 5; i++) { |
| 2735 | gsm0503_tch_burst_unmap(NULL, &bursts[i * 116], &h, 1); |
| 2736 | steal -= h; |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | /* if we found a stole FACCH, but only at correct alignment */ |
| 2741 | if (steal > 0) { |
Vadim Yanitskiy | a2bee8b | 2022-05-16 19:40:45 +0300 | [diff] [blame] | 2742 | /* If not NULL, dtx indicates type of previously decoded TCH/AHS frame. |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2743 | * 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] | 2744 | * reached in case of FACCH. Reset it here to avoid FACCH/H frames being |
| 2745 | * misinterpreted as AMR's special DTX frames. */ |
| 2746 | if (dtx != NULL) |
| 2747 | *dtx = AMR_OTHER; |
| 2748 | |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2749 | for (i = 0; i < 6; i++) { |
| 2750 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2751 | &bursts[i * 116], NULL, i >> 2); |
| 2752 | } |
| 2753 | |
| 2754 | for (i = 2; i < 4; i++) { |
| 2755 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], |
| 2756 | &bursts[i * 116], NULL, 1); |
| 2757 | } |
| 2758 | |
| 2759 | gsm0503_tch_fr_deinterleave(cB, iB); |
| 2760 | |
| 2761 | rv = _xcch_decode_cB(tch_data, cB, n_errors, n_bits_total); |
| 2762 | if (rv) { |
| 2763 | /* Error decoding FACCH frame */ |
| 2764 | return -1; |
| 2765 | } |
| 2766 | |
| 2767 | return GSM_MACBLOCK_LEN; |
| 2768 | } |
| 2769 | |
| 2770 | for (i = 0; i < 4; i++) { |
| 2771 | gsm0503_tch_burst_unmap(&iB[i * 114], |
| 2772 | &bursts[i * 116], NULL, i >> 1); |
| 2773 | } |
| 2774 | |
| 2775 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2776 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2777 | /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ |
| 2778 | if (dtx) { |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2779 | int n_bits_total_sid; |
| 2780 | int n_errors_sid; |
Vadim Yanitskiy | bf2d5e9 | 2022-05-15 15:58:10 +0300 | [diff] [blame] | 2781 | |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2782 | *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] | 2783 | /* TODO: detect and handle AHS_SID_UPDATE + AHS_SID_UPDATE_INH */ |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2784 | |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2785 | switch (*dtx) { |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2786 | 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] | 2787 | /* cB[] contains 16 bits of coded in-band data and 212 bits containing |
| 2788 | * the identification marker. We need to unmap/deinterleave 114 odd |
| 2789 | * bits from the last two blocks, 114 even bits from the first two |
| 2790 | * blocks and combine them together. */ |
| 2791 | gsm0503_tch_burst_unmap(&iB[0 * 114], &bursts[2 * 116], NULL, 0); |
| 2792 | gsm0503_tch_burst_unmap(&iB[1 * 114], &bursts[3 * 116], NULL, 0); |
| 2793 | gsm0503_tch_burst_unmap(&iB[2 * 114], &bursts[0 * 116], NULL, 1); |
| 2794 | gsm0503_tch_burst_unmap(&iB[3 * 114], &bursts[1 * 116], NULL, 1); |
| 2795 | gsm0503_tch_hr_deinterleave(cB, iB); |
| 2796 | |
| 2797 | /* cB[] is expected to contain 16 bits of coded in-band data and |
| 2798 | * 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] | 2799 | *dtx = AHS_SID_UPDATE_CN; |
| 2800 | |
| 2801 | osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2802 | cB + 16, conv, &n_errors_sid, |
| 2803 | &n_bits_total_sid); |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2804 | /* gsm0503_detect_ahs_dtx_frame2() calculates BER for the marker, |
Vadim Yanitskiy | 7790459 | 2022-05-19 17:43:57 +0300 | [diff] [blame] | 2805 | * osmo_conv_decode_ber() calculates BER for the coded data. */ |
| 2806 | if (n_errors != NULL) |
| 2807 | *n_errors += n_errors_sid; |
| 2808 | if (n_bits_total != NULL) |
| 2809 | *n_bits_total += n_bits_total_sid; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2810 | rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, |
| 2811 | 35, conv + 35); |
| 2812 | if (rv != 0) { |
| 2813 | /* Error checking CRC14 for an AMR SID_UPDATE frame */ |
| 2814 | return -1; |
| 2815 | } |
| 2816 | |
| 2817 | tch_amr_sid_update_append(conv, 1, |
| 2818 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2819 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2820 | tch_amr_reassemble(tch_data, conv, 39); |
| 2821 | len = 5; |
| 2822 | goto out; |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2823 | case AHS_SID_FIRST_P2: |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2824 | tch_amr_sid_update_append(sid_first_dummy, 0, |
| 2825 | (codec_mode_req) ? codec[*ft] |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2826 | : codec[id > 0 ? id : 0]); |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2827 | tch_amr_reassemble(tch_data, sid_first_dummy, 39); |
| 2828 | len = 5; |
| 2829 | goto out; |
Vadim Yanitskiy | cc4213e | 2022-05-24 04:17:23 +0700 | [diff] [blame] | 2830 | case AHS_ONSET: |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2831 | case AHS_SID_FIRST_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
| 2832 | case AHS_SID_UPDATE_INH: /* TODO: parse CMI or CMC/CMR (16 bit) */ |
| 2833 | 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] | 2834 | len = 0; |
| 2835 | goto out; |
Vadim Yanitskiy | eebaccd | 2022-05-15 14:46:08 +0300 | [diff] [blame] | 2836 | default: |
| 2837 | break; |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | |
Vadim Yanitskiy | b57e639 | 2022-05-24 02:51:50 +0700 | [diff] [blame] | 2841 | /* Parse codec ID (CMI or CMC/CMR) and check if it fits into range of codecs */ |
| 2842 | if ((id = gsm0503_tch_ahs_decode_inband(&cB[0])) >= codecs) { |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2843 | /* Codec mode out of range, return id */ |
| 2844 | return id; |
| 2845 | } |
| 2846 | |
| 2847 | switch ((codec_mode_req) ? codec[*ft] : codec[id]) { |
| 2848 | case 5: /* TCH/AHS7.95 */ |
| 2849 | osmo_conv_decode_ber(&gsm0503_tch_ahs_7_95, cB + 4, |
| 2850 | conv, n_errors, n_bits_total); |
| 2851 | |
| 2852 | tch_amr_unmerge(d, p, conv, 123, 67); |
| 2853 | |
| 2854 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 67, p); |
| 2855 | if (rv) { |
| 2856 | /* Error checking CRC8 for an AMR 7.95 frame */ |
| 2857 | return -1; |
| 2858 | } |
| 2859 | |
| 2860 | for (i = 0; i < 36; i++) |
| 2861 | d[i + 123] = (cB[i + 192] < 0) ? 1 : 0; |
| 2862 | |
| 2863 | tch_amr_reassemble(tch_data, d, 159); |
| 2864 | |
| 2865 | len = 20; |
| 2866 | |
| 2867 | break; |
| 2868 | case 4: /* TCH/AHS7.4 */ |
| 2869 | osmo_conv_decode_ber(&gsm0503_tch_ahs_7_4, cB + 4, |
| 2870 | conv, n_errors, n_bits_total); |
| 2871 | |
| 2872 | tch_amr_unmerge(d, p, conv, 120, 61); |
| 2873 | |
| 2874 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 61, p); |
| 2875 | if (rv) { |
| 2876 | /* Error checking CRC8 for an AMR 7.4 frame */ |
| 2877 | return -1; |
| 2878 | } |
| 2879 | |
| 2880 | for (i = 0; i < 28; i++) |
| 2881 | d[i + 120] = (cB[i + 200] < 0) ? 1 : 0; |
| 2882 | |
| 2883 | tch_amr_reassemble(tch_data, d, 148); |
| 2884 | |
| 2885 | len = 19; |
| 2886 | |
| 2887 | break; |
| 2888 | case 3: /* TCH/AHS6.7 */ |
| 2889 | osmo_conv_decode_ber(&gsm0503_tch_ahs_6_7, cB + 4, |
| 2890 | conv, n_errors, n_bits_total); |
| 2891 | |
| 2892 | tch_amr_unmerge(d, p, conv, 110, 55); |
| 2893 | |
| 2894 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2895 | if (rv) { |
| 2896 | /* Error checking CRC8 for an AMR 6.7 frame */ |
| 2897 | return -1; |
| 2898 | } |
| 2899 | |
| 2900 | for (i = 0; i < 24; i++) |
| 2901 | d[i + 110] = (cB[i + 204] < 0) ? 1 : 0; |
| 2902 | |
| 2903 | tch_amr_reassemble(tch_data, d, 134); |
| 2904 | |
| 2905 | len = 17; |
| 2906 | |
| 2907 | break; |
| 2908 | case 2: /* TCH/AHS5.9 */ |
| 2909 | osmo_conv_decode_ber(&gsm0503_tch_ahs_5_9, cB + 4, |
| 2910 | conv, n_errors, n_bits_total); |
| 2911 | |
| 2912 | tch_amr_unmerge(d, p, conv, 102, 55); |
| 2913 | |
| 2914 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 55, p); |
| 2915 | if (rv) { |
| 2916 | /* Error checking CRC8 for an AMR 5.9 frame */ |
| 2917 | return -1; |
| 2918 | } |
| 2919 | |
| 2920 | for (i = 0; i < 16; i++) |
| 2921 | d[i + 102] = (cB[i + 212] < 0) ? 1 : 0; |
| 2922 | |
| 2923 | tch_amr_reassemble(tch_data, d, 118); |
| 2924 | |
| 2925 | len = 15; |
| 2926 | |
| 2927 | break; |
| 2928 | case 1: /* TCH/AHS5.15 */ |
| 2929 | osmo_conv_decode_ber(&gsm0503_tch_ahs_5_15, cB + 4, |
| 2930 | conv, n_errors, n_bits_total); |
| 2931 | |
| 2932 | tch_amr_unmerge(d, p, conv, 91, 49); |
| 2933 | |
| 2934 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 49, p); |
| 2935 | if (rv) { |
| 2936 | /* Error checking CRC8 for an AMR 5.15 frame */ |
| 2937 | return -1; |
| 2938 | } |
| 2939 | |
| 2940 | for (i = 0; i < 12; i++) |
| 2941 | d[i + 91] = (cB[i + 216] < 0) ? 1 : 0; |
| 2942 | |
| 2943 | tch_amr_reassemble(tch_data, d, 103); |
| 2944 | |
| 2945 | len = 13; |
| 2946 | |
| 2947 | break; |
| 2948 | case 0: /* TCH/AHS4.75 */ |
| 2949 | osmo_conv_decode_ber(&gsm0503_tch_ahs_4_75, cB + 4, |
| 2950 | conv, n_errors, n_bits_total); |
| 2951 | |
| 2952 | tch_amr_unmerge(d, p, conv, 83, 39); |
| 2953 | |
| 2954 | rv = osmo_crc8gen_check_bits(&gsm0503_amr_crc6, d, 39, p); |
| 2955 | if (rv) { |
| 2956 | /* Error checking CRC8 for an AMR 4.75 frame */ |
| 2957 | return -1; |
| 2958 | } |
| 2959 | |
| 2960 | for (i = 0; i < 12; i++) |
| 2961 | d[i + 83] = (cB[i + 216] < 0) ? 1 : 0; |
| 2962 | |
| 2963 | tch_amr_reassemble(tch_data, d, 95); |
| 2964 | |
| 2965 | len = 12; |
| 2966 | |
| 2967 | break; |
| 2968 | default: |
| 2969 | /* Unknown frame type */ |
| 2970 | *n_bits_total = 159; |
| 2971 | *n_errors = *n_bits_total; |
| 2972 | return -1; |
| 2973 | } |
| 2974 | |
Philipp Maier | 898c9c6 | 2020-02-06 14:25:01 +0100 | [diff] [blame] | 2975 | out: |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2976 | /* Change codec request / indication, if frame is valid */ |
Vadim Yanitskiy | d6dca0c | 2022-05-21 18:36:54 +0300 | [diff] [blame] | 2977 | if (id != -1) { |
| 2978 | if (codec_mode_req) |
| 2979 | *cmr = id; |
| 2980 | else |
| 2981 | *ft = id; |
| 2982 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 2983 | |
| 2984 | return len; |
| 2985 | } |
| 2986 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2987 | /*! 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] | 2988 | * \param[out] bursts caller-allocated output buffer for bursts bits |
| 2989 | * \param[in] tch_data Codec input data in RTP payload format |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 2990 | * \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] | 2991 | * \param[in] codec_mode_req Use CMR (1) or FT (0) |
| 2992 | * \param[in] codec Array of codecs (active codec set) |
| 2993 | * \param[in] codecs Number of entries in \a codec |
| 2994 | * \param[in] ft Frame Type to be used for encoding (index to \a codec) |
| 2995 | * \param[in] cmr Codec Mode Request (used in codec_mode_req = 1 only) |
| 2996 | * \returns 0 in case of success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 2997 | 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] | 2998 | 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] | 2999 | uint8_t cmr) |
| 3000 | { |
| 3001 | ubit_t iB[912], cB[456], h; |
| 3002 | ubit_t d[244], p[6], conv[135]; |
| 3003 | int i; |
| 3004 | uint8_t id; |
| 3005 | |
| 3006 | if (len == GSM_MACBLOCK_LEN) { /* FACCH */ |
| 3007 | _xcch_encode_cB(cB, tch_data); |
| 3008 | |
| 3009 | h = 1; |
| 3010 | |
| 3011 | gsm0503_tch_fr_interleave(cB, iB); |
| 3012 | |
| 3013 | for (i = 0; i < 6; i++) |
| 3014 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], |
| 3015 | &h, i >> 2); |
| 3016 | for (i = 2; i < 4; i++) |
| 3017 | gsm0503_tch_burst_map(&iB[i * 114 + 456], |
| 3018 | &bursts[i * 116], &h, 1); |
| 3019 | |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | h = 0; |
| 3024 | |
Vadim Yanitskiy | a995269 | 2023-11-10 03:31:55 +0700 | [diff] [blame] | 3025 | id = codec_mode_req ? cmr : ft; |
| 3026 | if (OSMO_UNLIKELY(id >= ARRAY_SIZE(gsm0503_ahs_ic_ubit))) |
| 3027 | return -1; |
| 3028 | if (OSMO_UNLIKELY(ft >= codecs)) |
| 3029 | return -1; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3030 | |
| 3031 | switch (codec[ft]) { |
| 3032 | case 5: /* TCH/AHS7.95 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3033 | if (!len) { |
| 3034 | /* No data, induce BFI in the receiver by inverted CRC bits. |
| 3035 | * The data bit are all 0, so the correct parity bits would be 111111. */ |
| 3036 | memset(d, 0, 159); |
| 3037 | memset(p, 0, 6); |
| 3038 | } else { |
| 3039 | if (len != 20) |
| 3040 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3041 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3042 | tch_amr_disassemble(d, tch_data, 159); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3043 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3044 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 67, p); |
| 3045 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3046 | |
| 3047 | tch_amr_merge(conv, d, p, 123, 67); |
| 3048 | |
| 3049 | osmo_conv_encode(&gsm0503_tch_ahs_7_95, conv, cB + 4); |
| 3050 | |
| 3051 | memcpy(cB + 192, d + 123, 36); |
| 3052 | |
| 3053 | break; |
| 3054 | case 4: /* TCH/AHS7.4 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3055 | if (!len) { |
| 3056 | /* See comment above. */ |
| 3057 | memset(d, 0, 148); |
| 3058 | memset(p, 0, 6); |
| 3059 | } else { |
| 3060 | if (len != 19) |
| 3061 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3062 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3063 | tch_amr_disassemble(d, tch_data, 148); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3064 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3065 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 61, p); |
| 3066 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3067 | |
| 3068 | tch_amr_merge(conv, d, p, 120, 61); |
| 3069 | |
| 3070 | osmo_conv_encode(&gsm0503_tch_ahs_7_4, conv, cB + 4); |
| 3071 | |
| 3072 | memcpy(cB + 200, d + 120, 28); |
| 3073 | |
| 3074 | break; |
| 3075 | case 3: /* TCH/AHS6.7 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3076 | if (!len) { |
| 3077 | /* See comment above. */ |
| 3078 | memset(d, 0, 134); |
| 3079 | memset(p, 0, 6); |
| 3080 | } else { |
| 3081 | if (len != 17) |
| 3082 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3083 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3084 | tch_amr_disassemble(d, tch_data, 134); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3085 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3086 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 3087 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3088 | |
| 3089 | tch_amr_merge(conv, d, p, 110, 55); |
| 3090 | |
| 3091 | osmo_conv_encode(&gsm0503_tch_ahs_6_7, conv, cB + 4); |
| 3092 | |
| 3093 | memcpy(cB + 204, d + 110, 24); |
| 3094 | |
| 3095 | break; |
| 3096 | case 2: /* TCH/AHS5.9 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3097 | if (!len) { |
| 3098 | /* See comment above. */ |
| 3099 | memset(d, 0, 118); |
| 3100 | memset(p, 0, 6); |
| 3101 | } else { |
| 3102 | if (len != 15) |
| 3103 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3104 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3105 | tch_amr_disassemble(d, tch_data, 118); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3106 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3107 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 55, p); |
| 3108 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3109 | |
| 3110 | tch_amr_merge(conv, d, p, 102, 55); |
| 3111 | |
| 3112 | osmo_conv_encode(&gsm0503_tch_ahs_5_9, conv, cB + 4); |
| 3113 | |
| 3114 | memcpy(cB + 212, d + 102, 16); |
| 3115 | |
| 3116 | break; |
| 3117 | case 1: /* TCH/AHS5.15 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3118 | if (!len) { |
| 3119 | /* See comment above. */ |
| 3120 | memset(d, 0, 103); |
| 3121 | memset(p, 0, 6); |
| 3122 | } else { |
| 3123 | if (len != 13) |
| 3124 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3125 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3126 | tch_amr_disassemble(d, tch_data, 103); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3127 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3128 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 49, p); |
| 3129 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3130 | |
| 3131 | tch_amr_merge(conv, d, p, 91, 49); |
| 3132 | |
| 3133 | osmo_conv_encode(&gsm0503_tch_ahs_5_15, conv, cB + 4); |
| 3134 | |
| 3135 | memcpy(cB + 216, d + 91, 12); |
| 3136 | |
| 3137 | break; |
| 3138 | case 0: /* TCH/AHS4.75 */ |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3139 | if (!len) { |
| 3140 | /* See comment above. */ |
| 3141 | memset(d, 0, 95); |
| 3142 | memset(p, 0, 6); |
| 3143 | } else { |
| 3144 | if (len != 12) |
| 3145 | goto invalid_length; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3146 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3147 | tch_amr_disassemble(d, tch_data, 95); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3148 | |
Andreas Eversberg | ebdf3dc | 2023-11-24 16:52:21 +0100 | [diff] [blame] | 3149 | osmo_crc8gen_set_bits(&gsm0503_amr_crc6, d, 39, p); |
| 3150 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3151 | |
| 3152 | tch_amr_merge(conv, d, p, 83, 39); |
| 3153 | |
| 3154 | osmo_conv_encode(&gsm0503_tch_ahs_4_75, conv, cB + 4); |
| 3155 | |
| 3156 | memcpy(cB + 216, d + 83, 12); |
| 3157 | |
| 3158 | break; |
| 3159 | default: |
| 3160 | /* FIXME: FT %ft is not supported */ |
| 3161 | return -1; |
| 3162 | } |
| 3163 | |
Philipp Maier | cfea39b | 2021-08-31 16:02:31 +0200 | [diff] [blame] | 3164 | memcpy(cB, gsm0503_ahs_ic_ubit[id], 4); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3165 | |
| 3166 | gsm0503_tch_hr_interleave(cB, iB); |
| 3167 | |
| 3168 | for (i = 0; i < 4; i++) |
| 3169 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 1); |
| 3170 | |
| 3171 | return 0; |
| 3172 | |
| 3173 | invalid_length: |
| 3174 | /* FIXME: payload length %len does not comply with codec type %ft */ |
| 3175 | return -1; |
| 3176 | } |
| 3177 | |
| 3178 | /* |
| 3179 | * GSM RACH transcoding |
| 3180 | */ |
| 3181 | |
| 3182 | /* |
| 3183 | * GSM RACH apply BSIC to parity |
| 3184 | * |
| 3185 | * p(j) = p(j) xor b(j) j = 0, ..., 5 |
| 3186 | * b(0) = MSB of PLMN colour code |
| 3187 | * b(5) = LSB of BS colour code |
| 3188 | */ |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3189 | 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] | 3190 | { |
| 3191 | int i; |
| 3192 | |
| 3193 | /* Apply it */ |
| 3194 | for (i = 0; i < 6; i++) |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3195 | d[start + i] ^= ((bsic >> (5 - i)) & 1); |
| 3196 | } |
| 3197 | |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3198 | static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is_11bit, |
| 3199 | int *n_errors, int *n_bits_total) |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3200 | { |
| 3201 | ubit_t conv[17]; |
| 3202 | uint8_t ra[2] = { 0 }, nbits = is_11bit ? 11 : 8; |
| 3203 | int rv; |
| 3204 | |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3205 | osmo_conv_decode_ber(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, burst, conv, |
| 3206 | n_errors, n_bits_total); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3207 | |
| 3208 | rach_apply_bsic(conv, bsic, nbits); |
| 3209 | |
| 3210 | rv = osmo_crc8gen_check_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits); |
| 3211 | if (rv) |
| 3212 | return -1; |
| 3213 | |
| 3214 | osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1); |
| 3215 | |
Vadim Yanitskiy | 9e713f3 | 2020-03-31 19:40:09 +0700 | [diff] [blame] | 3216 | return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0]; |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 |
| 3220 | * \param[out] ra output buffer for RACH data |
| 3221 | * \param[in] burst Input burst data |
| 3222 | * \param[in] bsic BSIC used in this cell |
| 3223 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3224 | int gsm0503_rach_ext_decode(uint16_t *ra, const sbit_t *burst, uint8_t bsic) |
| 3225 | { |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3226 | int16_t r = rach_decode_ber(burst, bsic, true, NULL, NULL); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3227 | |
| 3228 | if (r < 0) |
| 3229 | return r; |
| 3230 | |
| 3231 | *ra = r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3232 | |
| 3233 | return 0; |
| 3234 | } |
| 3235 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3236 | /*! Decode the (8-bit) RACH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3237 | * \param[out] ra output buffer for RACH data |
| 3238 | * \param[in] burst Input burst data |
| 3239 | * \param[in] bsic BSIC used in this cell |
| 3240 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3241 | 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] | 3242 | { |
Harald Welte | 6950b19 | 2018-02-26 11:48:00 +0100 | [diff] [blame] | 3243 | int16_t r = rach_decode_ber(burst, bsic, false, NULL, NULL); |
| 3244 | if (r < 0) |
| 3245 | return r; |
| 3246 | |
| 3247 | *ra = r; |
| 3248 | return 0; |
| 3249 | } |
| 3250 | |
| 3251 | /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 |
| 3252 | * \param[out] ra output buffer for RACH data |
| 3253 | * \param[in] burst Input burst data |
| 3254 | * \param[in] bsic BSIC used in this cell |
| 3255 | * \param[out] n_errors Number of detected bit errors |
| 3256 | * \param[out] n_bits_total Total number of bits |
| 3257 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3258 | int gsm0503_rach_ext_decode_ber(uint16_t *ra, const sbit_t *burst, uint8_t bsic, |
| 3259 | int *n_errors, int *n_bits_total) |
| 3260 | { |
| 3261 | int16_t r = rach_decode_ber(burst, bsic, true, n_errors, n_bits_total); |
| 3262 | if (r < 0) |
| 3263 | return r; |
| 3264 | |
| 3265 | *ra = r; |
| 3266 | return 0; |
| 3267 | } |
| 3268 | |
| 3269 | /*! Decode the (8-bit) RACH according to TS 05.03 |
| 3270 | * \param[out] ra output buffer for RACH data |
| 3271 | * \param[in] burst Input burst data |
| 3272 | * \param[in] bsic BSIC used in this cell |
| 3273 | * \param[out] n_errors Number of detected bit errors |
| 3274 | * \param[out] n_bits_total Total number of bits |
| 3275 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
| 3276 | int gsm0503_rach_decode_ber(uint8_t *ra, const sbit_t *burst, uint8_t bsic, |
| 3277 | int *n_errors, int *n_bits_total) |
| 3278 | { |
| 3279 | 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] | 3280 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3281 | if (r < 0) |
| 3282 | return r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3283 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3284 | *ra = r; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3285 | |
| 3286 | return 0; |
| 3287 | } |
| 3288 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3289 | /*! Encode the (8-bit) RACH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3290 | * \param[out] burst Caller-allocated output burst buffer |
| 3291 | * \param[in] ra Input RACH data |
| 3292 | * \param[in] bsic BSIC used in this cell |
| 3293 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3294 | 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] | 3295 | { |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3296 | return gsm0503_rach_ext_encode(burst, *ra, bsic, false); |
| 3297 | } |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3298 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3299 | /*! Encode the Extended (11-bit) or regular (8-bit) RACH according to 3GPP TS 45.003 |
| 3300 | * \param[out] burst Caller-allocated output burst buffer |
| 3301 | * \param[in] ra11 Input RACH data |
| 3302 | * \param[in] bsic BSIC used in this cell |
| 3303 | * \param[in] is_11bit whether given RA is 11 bit or not |
| 3304 | * \returns 0 on success; negative on error */ |
| 3305 | int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_11bit) |
| 3306 | { |
| 3307 | ubit_t conv[17]; |
| 3308 | uint8_t ra[2] = { 0 }, nbits = 8; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3309 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3310 | if (is_11bit) { |
Vadim Yanitskiy | 9e713f3 | 2020-03-31 19:40:09 +0700 | [diff] [blame] | 3311 | ra[0] = (uint8_t) (ra11 >> 3); |
| 3312 | ra[1] = (uint8_t) (ra11 & 0x07); |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3313 | nbits = 11; |
| 3314 | } else |
| 3315 | ra[0] = (uint8_t)ra11; |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3316 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3317 | osmo_pbit2ubit_ext(conv, 0, ra, 0, nbits, 1); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3318 | |
Max | 32e5641 | 2017-10-16 14:58:00 +0200 | [diff] [blame] | 3319 | osmo_crc8gen_set_bits(&gsm0503_rach_crc6, conv, nbits, conv + nbits); |
| 3320 | |
| 3321 | rach_apply_bsic(conv, bsic, nbits); |
| 3322 | |
| 3323 | osmo_conv_encode(is_11bit ? &gsm0503_rach_ext : &gsm0503_rach, conv, burst); |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3324 | |
| 3325 | return 0; |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * GSM SCH transcoding |
| 3330 | */ |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3331 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3332 | /*! Decode the SCH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3333 | * \param[out] sb_info output buffer for SCH data |
| 3334 | * \param[in] burst Input burst data |
| 3335 | * \returns 0 on success; negative on error (e.g. CRC error) */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3336 | int gsm0503_sch_decode(uint8_t *sb_info, const sbit_t *burst) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3337 | { |
| 3338 | ubit_t conv[35]; |
| 3339 | int rv; |
| 3340 | |
| 3341 | osmo_conv_decode(&gsm0503_sch, burst, conv); |
| 3342 | |
| 3343 | rv = osmo_crc16gen_check_bits(&gsm0503_sch_crc10, conv, 25, conv + 25); |
| 3344 | if (rv) |
| 3345 | return -1; |
| 3346 | |
| 3347 | osmo_ubit2pbit_ext(sb_info, 0, conv, 0, 25, 1); |
| 3348 | |
| 3349 | return 0; |
| 3350 | } |
| 3351 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3352 | /*! Encode the SCH according to TS 05.03 |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3353 | * \param[out] burst Caller-allocated output burst buffer |
| 3354 | * \param[in] sb_info Input SCH data |
| 3355 | * \returns 0 on success; negative on error */ |
Harald Welte | b9946d3 | 2017-06-12 09:40:16 +0200 | [diff] [blame] | 3356 | int gsm0503_sch_encode(ubit_t *burst, const uint8_t *sb_info) |
Vadim Yanitskiy | 3262f82 | 2016-09-23 01:48:59 +0700 | [diff] [blame] | 3357 | { |
| 3358 | ubit_t conv[35]; |
| 3359 | |
| 3360 | osmo_pbit2ubit_ext(conv, 0, sb_info, 0, 25, 1); |
| 3361 | |
| 3362 | osmo_crc16gen_set_bits(&gsm0503_sch_crc10, conv, 25, conv + 25); |
| 3363 | |
| 3364 | osmo_conv_encode(&gsm0503_sch, conv, burst); |
| 3365 | |
| 3366 | return 0; |
| 3367 | } |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3368 | |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3369 | /* |
| 3370 | * GSM CSD transcoding |
| 3371 | */ |
| 3372 | |
| 3373 | static inline void _tch_csd_burst_map(ubit_t *burst, const ubit_t *iB) |
| 3374 | { |
Vadim Yanitskiy | b04d59e | 2023-05-26 01:38:23 +0700 | [diff] [blame] | 3375 | unsigned int i; |
| 3376 | |
| 3377 | /* hu(B): copy *even* numbered bits if not stolen by FACCH */ |
| 3378 | if (burst[58] == 0) { |
| 3379 | for (i = 0; i < 57; i += 2) |
| 3380 | burst[i] |= iB[i]; |
| 3381 | for (i = 58; i < 114; i += 2) |
| 3382 | burst[i + 2] |= iB[i]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3383 | } |
| 3384 | |
Vadim Yanitskiy | b04d59e | 2023-05-26 01:38:23 +0700 | [diff] [blame] | 3385 | /* hl(B): copy *odd* numbered bits if not stolen by FACCH */ |
| 3386 | if (burst[57] == 0) { |
| 3387 | for (i = 1; i < 57; i += 2) |
| 3388 | burst[i] |= iB[i]; |
| 3389 | for (i = 57; i < 114; i += 2) |
| 3390 | burst[i + 2] |= iB[i]; |
| 3391 | } |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /*! Perform channel encoding of a TCH/F9.6 channel as per section 3.3. |
| 3395 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3396 | * 22 * 2 * 58 == 2552 bits total. |
| 3397 | * \param[in] data Data to be encoded (240 unpacked bits). |
| 3398 | * \returns 0 in case of success; negative on error. */ |
| 3399 | int gsm0503_tch_fr96_encode(ubit_t *bursts, const ubit_t *data) |
| 3400 | { |
| 3401 | ubit_t iB[22 * 114], cB[4 * 114]; |
| 3402 | ubit_t conv[4 * 60 + 4]; |
| 3403 | |
| 3404 | /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */ |
| 3405 | memcpy(&conv[0], &data[0], 4 * 60); |
Vadim Yanitskiy | f8e570a | 2023-07-08 06:05:12 +0700 | [diff] [blame] | 3406 | /* pad(4) is set to 0 by osmo_conv_encode() below */ |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3407 | |
| 3408 | /* 3.3.3 Convolutional encoder */ |
| 3409 | osmo_conv_encode(&gsm0503_tch_f96, &conv[0], &cB[0]); |
| 3410 | |
| 3411 | /* 3.3.4 Interleaving */ |
| 3412 | memset(&iB[0], 0, sizeof(iB)); |
| 3413 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3414 | |
| 3415 | /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3416 | for (unsigned int i = 0; i < 22; i++) |
| 3417 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3418 | |
| 3419 | return 0; |
| 3420 | } |
| 3421 | |
| 3422 | /*! Perform channel decoding of a TCH/F9.6 channel as per section 3.3. |
| 3423 | * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits). |
| 3424 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3425 | * 22 * 2 * 58 == 2552 bits total. |
| 3426 | * \param[out] n_errors Number of detected bit errors. |
| 3427 | * \param[out] n_bits_total Total number of bits. |
| 3428 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3429 | int gsm0503_tch_fr96_decode(ubit_t *data, const sbit_t *bursts, |
| 3430 | int *n_errors, int *n_bits_total) |
| 3431 | { |
| 3432 | sbit_t iB[22 * 114], cB[4 * 114]; |
| 3433 | ubit_t conv[4 * 60 + 4]; |
| 3434 | |
| 3435 | /* 3.3.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3436 | for (unsigned int i = 0; i < 22; i++) { |
| 3437 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3438 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3439 | } |
| 3440 | |
| 3441 | /* 3.3.4 Interleaving */ |
| 3442 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3443 | |
| 3444 | /* 3.3.3 Convolutional encoder */ |
| 3445 | osmo_conv_decode_ber(&gsm0503_tch_f96, &cB[0], &conv[0], n_errors, n_bits_total); |
| 3446 | |
| 3447 | /* 3.3.2 Block code: b1(60) + b2(60) + b3(60) + b4(60) + pad(4) */ |
| 3448 | memcpy(&data[0], &conv[0], 4 * 60); |
| 3449 | |
| 3450 | return 4 * 60; |
| 3451 | } |
| 3452 | |
| 3453 | /*! Perform channel encoding of a TCH/F4.8 channel as per section 3.4. |
| 3454 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3455 | * 22 * 2 * 58 == 2552 bits total. |
| 3456 | * \param[in] data Data to be encoded (120 unpacked bits). |
| 3457 | * \returns 0 in case of success; negative on error */ |
| 3458 | int gsm0503_tch_fr48_encode(ubit_t *bursts, const ubit_t *data) |
| 3459 | { |
| 3460 | ubit_t iB[22 * 114], cB[4 * 114]; |
| 3461 | ubit_t conv[2 * 60 + 32]; |
| 3462 | |
| 3463 | /* 3.4.2 Block code: |
| 3464 | * |
| 3465 | * Sixteen bits equal to 0 are added to the 60 information bits, the result |
| 3466 | * being a block of 76 bits, {u(0),u(1),...,u(75)}, with: |
| 3467 | * |
| 3468 | * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14; |
| 3469 | * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18. |
| 3470 | * |
| 3471 | * Two such blocks forming a block of 152 bits: u1 + u2. */ |
| 3472 | for (unsigned int k = 0; k < 2 * 4; k++) { |
| 3473 | memcpy(&conv[19 * k], &data[15 * k], 15); |
| 3474 | memset(&conv[19 * k + 15], 0, 4); |
| 3475 | } |
| 3476 | |
| 3477 | /* 3.4.3 Convolutional encoder */ |
| 3478 | osmo_conv_encode(&gsm0503_tch_f48, &conv[0], &cB[0]); |
| 3479 | |
| 3480 | /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3481 | memset(&iB[0], 0, sizeof(iB)); |
| 3482 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3483 | |
| 3484 | /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3485 | for (unsigned int i = 0; i < 22; i++) |
| 3486 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3487 | |
| 3488 | return 0; |
| 3489 | } |
| 3490 | |
| 3491 | /*! Perform channel decoding of a TCH/F4.8 channel as per section 3.4. |
| 3492 | * \param[out] data Caller-allocated buffer for decoded data (120 unpacked bits). |
| 3493 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3494 | * 22 * 2 * 58 == 2552 bits total. |
| 3495 | * \param[out] n_errors Number of detected bit errors. |
| 3496 | * \param[out] n_bits_total Total number of bits. |
| 3497 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3498 | int gsm0503_tch_fr48_decode(ubit_t *data, const sbit_t *bursts, |
| 3499 | int *n_errors, int *n_bits_total) |
| 3500 | { |
| 3501 | sbit_t iB[22 * 114], cB[4 * 114]; |
| 3502 | ubit_t conv[2 * 60 + 32]; |
| 3503 | |
| 3504 | /* 3.4.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3505 | for (unsigned int i = 0; i < 22; i++) { |
| 3506 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3507 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3508 | } |
| 3509 | |
| 3510 | /* 3.4.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3511 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3512 | |
| 3513 | /* 3.4.3 Convolutional encoder */ |
| 3514 | osmo_conv_decode_ber(&gsm0503_tch_f48, &cB[0], &conv[0], n_errors, n_bits_total); |
| 3515 | |
| 3516 | /* 3.4.2 Block code: |
| 3517 | * |
| 3518 | * Sixteen bits equal to 0 are added to the 60 information bits, the result |
| 3519 | * being a block of 76 bits, {u(0),u(1),...,u(75)}, with: |
| 3520 | * |
| 3521 | * u(19k+p) = d(15k+p) for k = 0,1,2,3 and p = 0,1,...,14; |
| 3522 | * u(19k+p) = 0 for k = 0,1,2,3 and p = 15,16,17,18. |
| 3523 | * |
| 3524 | * Two such blocks forming a block of 152 bits: u1 + u2. */ |
| 3525 | for (unsigned int k = 0; k < 2 * 4; k++) |
| 3526 | memcpy(&data[15 * k], &conv[19 * k], 15); |
| 3527 | |
| 3528 | return 2 * 60; |
| 3529 | } |
| 3530 | |
| 3531 | /*! Perform channel encoding of a TCH/H4.8 channel as per section 3.5. |
| 3532 | * The algorithm is identical to TCH/F9.6, so it's just a wrapper. |
| 3533 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3534 | * 22 * 2 * 58 == 2552 bits total. |
| 3535 | * \param[in] data Data to be encoded (240 unpacked bits). |
| 3536 | * \returns 0 in case of success; negative on error */ |
| 3537 | int gsm0503_tch_hr48_encode(ubit_t *bursts, const ubit_t *data) |
| 3538 | { |
| 3539 | return gsm0503_tch_fr96_encode(bursts, data); |
| 3540 | } |
| 3541 | |
| 3542 | /*! Perform channel decoding of a TCH/H4.8 channel as per section 3.5. |
| 3543 | * The algorithm is identical to TCH/F9.6, so it's just a wrapper. |
| 3544 | * \param[out] data Caller-allocated buffer for decoded data (240 unpacked bits). |
| 3545 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3546 | * 22 * 2 * 58 == 2552 bits total. |
| 3547 | * \param[out] n_errors Number of detected bit errors. |
| 3548 | * \param[out] n_bits_total Total number of bits. |
| 3549 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3550 | int gsm0503_tch_hr48_decode(ubit_t *data, const sbit_t *bursts, |
| 3551 | int *n_errors, int *n_bits_total) |
| 3552 | { |
| 3553 | return gsm0503_tch_fr96_decode(data, bursts, n_errors, n_bits_total); |
| 3554 | } |
| 3555 | |
Vadim Yanitskiy | fadda01 | 2023-07-08 06:30:55 +0700 | [diff] [blame] | 3556 | /*! Perform channel encoding of a TCH/F2.4 channel as per section 3.6. |
| 3557 | * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts, |
| 3558 | * 8 * 2 * 58 == 928 bits total. |
| 3559 | * \param[in] data Data to be encoded (72 unpacked bits). |
| 3560 | * \returns 0 in case of success; negative on error */ |
| 3561 | int gsm0503_tch_fr24_encode(ubit_t *bursts, const ubit_t *data) |
| 3562 | { |
| 3563 | ubit_t iB[8 * 114], cB[4 * 114]; |
| 3564 | const ubit_t h = 0; |
| 3565 | |
| 3566 | /* 3.6.{1-3} Block code and Convolutional encoder */ |
| 3567 | osmo_conv_encode(&gsm0503_tch_f24, &data[0], &cB[0]); |
| 3568 | |
| 3569 | /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3570 | gsm0503_tch_fr_interleave(&cB[0], &iB[0]); |
| 3571 | |
| 3572 | /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3573 | for (unsigned int i = 0; i < 8; i++) |
| 3574 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 3575 | |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
| 3579 | /*! Perform channel decoding of a TCH/F2.4 channel as per section 3.6. |
| 3580 | * \param[out] data Caller-allocated buffer for decoded data (72 unpacked bits). |
| 3581 | * \param[in] bursts Buffer containing the symbols of 8 bursts, |
| 3582 | * 8 * 2 * 58 == 928 bits total. |
| 3583 | * \param[out] n_errors Number of detected bit errors. |
| 3584 | * \param[out] n_bits_total Total number of bits. |
| 3585 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3586 | int gsm0503_tch_fr24_decode(ubit_t *data, const sbit_t *bursts, |
| 3587 | int *n_errors, int *n_bits_total) |
| 3588 | { |
| 3589 | sbit_t iB[8 * 114], cB[4 * 114]; |
| 3590 | |
| 3591 | /* 3.6.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3592 | for (unsigned int i = 0; i < 8; i++) |
| 3593 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2); |
| 3594 | |
| 3595 | /* 3.6.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3596 | gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]); |
| 3597 | |
| 3598 | /* 3.6.{1-3} Block code and Convolutional encoder */ |
| 3599 | osmo_conv_decode_ber(&gsm0503_tch_f24, &cB[0], &data[0], n_errors, n_bits_total); |
| 3600 | |
| 3601 | return 72; |
| 3602 | } |
| 3603 | |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3604 | /*! Perform channel encoding of a TCH/H2.4 channel as per section 3.7. |
| 3605 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3606 | * 22 * 2 * 58 == 2552 bits total. |
| 3607 | * \param[in] data Data to be encoded (144 unpacked bits). |
| 3608 | * \returns 0 in case of success; negative on error */ |
| 3609 | int gsm0503_tch_hr24_encode(ubit_t *bursts, const ubit_t *data) |
| 3610 | { |
| 3611 | ubit_t iB[22 * 114], cB[4 * 114]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3612 | |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3613 | /* 3.7.{1-3} Block code and Convolutional encoder */ |
| 3614 | osmo_conv_encode(&gsm0503_tch_h24, &data[ 0], &cB[ 0]); |
| 3615 | osmo_conv_encode(&gsm0503_tch_h24, &data[72], &cB[228]); |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3616 | |
| 3617 | /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3618 | memset(&iB[0], 0, sizeof(iB)); |
| 3619 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3620 | |
| 3621 | /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3622 | for (unsigned int i = 0; i < 22; i++) |
| 3623 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3624 | |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | /*! Perform channel decoding of a TCH/H2.4 channel as per section 3.7. |
| 3629 | * \param[out] data Caller-allocated buffer for decoded data (144 unpacked bits). |
| 3630 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3631 | * 22 * 2 * 58 == 2552 bits total. |
| 3632 | * \param[out] n_errors Number of detected bit errors. |
| 3633 | * \param[out] n_bits_total Total number of bits. |
| 3634 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3635 | int gsm0503_tch_hr24_decode(ubit_t *data, const sbit_t *bursts, |
| 3636 | int *n_errors, int *n_bits_total) |
| 3637 | { |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3638 | int n_errors_l[2], n_bits_total_l[2]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3639 | sbit_t iB[22 * 114], cB[4 * 114]; |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3640 | |
| 3641 | /* 3.7.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3642 | for (unsigned int i = 0; i < 22; i++) { |
| 3643 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3644 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3645 | } |
| 3646 | |
| 3647 | /* 3.7.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3648 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3649 | |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3650 | /* 3.7.{1-3} Block code and Convolutional encoder */ |
| 3651 | osmo_conv_decode_ber(&gsm0503_tch_h24, &cB[ 0], &data[ 0], &n_errors_l[0], &n_bits_total_l[0]); |
| 3652 | 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] | 3653 | |
Sylvain Munaut | c87c6a3 | 2023-07-07 15:57:32 +0200 | [diff] [blame] | 3654 | if (n_errors) |
| 3655 | *n_errors = n_errors_l[0] + n_errors_l[1]; |
| 3656 | |
| 3657 | if (n_bits_total) |
| 3658 | *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] | 3659 | |
| 3660 | return 2 * 72; |
| 3661 | } |
| 3662 | |
| 3663 | /*! Perform channel encoding of a TCH/F14.4 channel as per section 3.8. |
| 3664 | * \param[out] bursts Caller-allocated buffer for symbols of 22 bursts, |
| 3665 | * 22 * 2 * 58 == 2552 bits total. |
| 3666 | * \param[in] data Data to be encoded (290 unpacked bits). |
| 3667 | * \returns 0 in case of success; negative on error */ |
| 3668 | int gsm0503_tch_fr144_encode(ubit_t *bursts, const ubit_t *data) |
| 3669 | { |
| 3670 | ubit_t iB[22 * 114], cB[4 * 114]; |
| 3671 | ubit_t conv[290 + 4]; |
| 3672 | |
| 3673 | /* 3.8.2 Block code: b(290) + pad(4) */ |
| 3674 | memcpy(&conv[0], &data[0], 290); |
Vadim Yanitskiy | f8e570a | 2023-07-08 06:05:12 +0700 | [diff] [blame] | 3675 | /* pad(4) is set to 0 by osmo_conv_encode() below */ |
Vadim Yanitskiy | 9a22827 | 2023-05-20 18:54:13 +0700 | [diff] [blame] | 3676 | |
| 3677 | /* 3.8.3 Convolutional encoder */ |
| 3678 | osmo_conv_encode(&gsm0503_tch_f144, &conv[0], &cB[0]); |
| 3679 | |
| 3680 | /* 3.8.4 Interleaving */ |
| 3681 | memset(&iB[0], 0, sizeof(iB)); |
| 3682 | gsm0503_tch_f96_interleave(&cB[0], &iB[0]); |
| 3683 | |
| 3684 | /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3685 | for (unsigned int i = 0; i < 22; i++) |
| 3686 | _tch_csd_burst_map(&bursts[i * 116], &iB[i * 114]); |
| 3687 | |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
| 3691 | /*! Perform channel decoding of a TCH/14.4 channel as per section 3.8. |
| 3692 | * \param[out] data Caller-allocated buffer for decoded data (290 unpacked bits). |
| 3693 | * \param[in] bursts Buffer containing the symbols of 22 bursts, |
| 3694 | * 22 * 2 * 58 == 2552 bits total. |
| 3695 | * \param[out] n_errors Number of detected bit errors. |
| 3696 | * \param[out] n_bits_total Total number of bits. |
| 3697 | * \returns Number of unpacked bits used in the output buffer; negative on error. */ |
| 3698 | int gsm0503_tch_fr144_decode(ubit_t *data, const sbit_t *bursts, |
| 3699 | int *n_errors, int *n_bits_total) |
| 3700 | { |
| 3701 | sbit_t iB[22 * 114], cB[4 * 114]; |
| 3702 | ubit_t conv[294]; |
| 3703 | |
| 3704 | /* 3.8.5 Mapping on a burst: as specified for TCH/FS in subclause 3.1.4 */ |
| 3705 | for (unsigned int i = 0; i < 22; i++) { |
| 3706 | memcpy(&iB[i * 114], &bursts[i * 116], 57); |
| 3707 | memcpy(&iB[i * 114 + 57], &bursts[i * 116 + 59], 57); |
| 3708 | } |
| 3709 | |
| 3710 | /* 3.8.4 Interleaving: as specified for the TCH/F9.6 in subclause 3.3.4 */ |
| 3711 | gsm0503_tch_f96_deinterleave(&cB[0], &iB[0]); |
| 3712 | |
| 3713 | /* 3.8.3 Convolutional encoder */ |
| 3714 | osmo_conv_decode_ber(&gsm0503_tch_f144, &cB[0], &conv[0], n_errors, n_bits_total); |
| 3715 | |
| 3716 | /* 3.8.2 Block code: b(290) + pad(4) */ |
| 3717 | memcpy(&data[0], &conv[0], 290); |
| 3718 | |
| 3719 | return 290; |
| 3720 | } |
| 3721 | |
Vadim Yanitskiy | b334022 | 2023-05-26 01:07:18 +0700 | [diff] [blame] | 3722 | /* |
| 3723 | * FACCH/[FH] transcoding |
| 3724 | */ |
| 3725 | |
| 3726 | /*! Perform channel encoding of a FACCH/F data as per section 4.2. |
| 3727 | * \param[out] bursts Caller-allocated buffer for symbols of 8 bursts, |
| 3728 | * 8 * 2 * 58 == 928 bits total. |
| 3729 | * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN). |
| 3730 | * \returns 0 in case of success; negative on error */ |
| 3731 | int gsm0503_tch_fr_facch_encode(ubit_t *bursts, const uint8_t *data) |
| 3732 | { |
| 3733 | ubit_t iB[8 * 114], cB[4 * 114]; |
| 3734 | const ubit_t h = 1; |
| 3735 | |
| 3736 | /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3737 | _xcch_encode_cB(&cB[0], &data[0]); |
| 3738 | |
| 3739 | /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3740 | gsm0503_tch_fr_interleave(&cB[0], &iB[0]); |
| 3741 | |
| 3742 | /* 4.2.5 Mapping on a Burst: |
| 3743 | * - hu(B)=1 the even numbered bits in the first 4 bursts and |
| 3744 | * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */ |
| 3745 | for (unsigned int i = 0; i < 8; i++) |
| 3746 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 3747 | |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
| 3751 | /*! Perform channel decoding of a FACCH/F data as per section 4.2. |
| 3752 | * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN). |
| 3753 | * \param[in] bursts Buffer containing the symbols of 8 bursts, |
| 3754 | * 8 * 2 * 58 == 928 bits total. |
| 3755 | * \param[out] n_errors Number of detected bit errors. |
| 3756 | * \param[out] n_bits_total Total number of bits. |
| 3757 | * \returns Number of bytes used in the output buffer; negative on error. */ |
| 3758 | int gsm0503_tch_fr_facch_decode(uint8_t *data, const sbit_t *bursts, |
| 3759 | int *n_errors, int *n_bits_total) |
| 3760 | { |
| 3761 | sbit_t iB[8 * 114], cB[4 * 114]; |
| 3762 | int steal = 0; |
| 3763 | |
| 3764 | /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */ |
| 3765 | for (unsigned int i = 0; i < 4; i++) |
| 3766 | steal -= bursts[i * 116 + 58]; /* hu(B) */ |
| 3767 | for (unsigned int i = 4; i < 8; i++) |
| 3768 | steal -= bursts[i * 116 + 57]; /* hl(B) */ |
| 3769 | if (steal <= 0) |
| 3770 | return -1; |
| 3771 | |
| 3772 | /* 4.2.5 Mapping on a Burst: |
| 3773 | * - hu(B)=1 the even numbered bits in the first 4 bursts and |
| 3774 | * - hl(B)=1 the odd numbered bits of the last 4 bursts are stolen. */ |
| 3775 | for (unsigned int i = 0; i < 8; i++) |
| 3776 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2); |
| 3777 | |
| 3778 | /* 4.2.4 Interleaving: as specified for the TCH/FS in subclause 3.1.3 */ |
| 3779 | gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]); |
| 3780 | |
| 3781 | /* 4.2.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3782 | if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0) |
| 3783 | return -1; |
| 3784 | |
| 3785 | return GSM_MACBLOCK_LEN; |
| 3786 | } |
| 3787 | |
| 3788 | /*! Perform channel encoding of a FACCH/H data as per section 4.3. |
| 3789 | * \param[out] bursts Caller-allocated buffer for symbols of 6 bursts, |
| 3790 | * 6 * 2 * 58 == 696 bits total. |
| 3791 | * \param[in] data FACCH MAC block to be encoded (GSM_MACBLOCK_LEN). |
| 3792 | * \returns 0 in case of success; negative on error */ |
| 3793 | int gsm0503_tch_hr_facch_encode(ubit_t *bursts, const uint8_t *data) |
| 3794 | { |
| 3795 | ubit_t iB[8 * 114], cB[4 * 114]; |
| 3796 | const ubit_t h = 1; |
| 3797 | |
| 3798 | /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3799 | _xcch_encode_cB(&cB[0], &data[0]); |
| 3800 | |
| 3801 | /* 4.3.4 Interleaving */ |
| 3802 | gsm0503_tch_fr_interleave(&cB[0], &iB[0]); |
| 3803 | |
| 3804 | /* 4.3.5 Mapping on a Burst: |
| 3805 | * - hu(B)=1 the even numbered bits of the first 2 bursts, |
| 3806 | * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and |
| 3807 | * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */ |
| 3808 | for (unsigned int i = 0; i < 6; i++) |
| 3809 | gsm0503_tch_burst_map(&iB[i * 114], &bursts[i * 116], &h, i >> 2); |
| 3810 | for (unsigned int i = 2; i < 4; i++) |
| 3811 | gsm0503_tch_burst_map(&iB[i * 114 + 456], &bursts[i * 116], &h, 1); |
| 3812 | |
| 3813 | return 0; |
| 3814 | } |
| 3815 | |
| 3816 | /*! Perform channel decoding of a FACCH/H data as per section 4.3. |
| 3817 | * \param[out] data Caller-allocated buffer for decoded FACCH (GSM_MACBLOCK_LEN). |
| 3818 | * \param[in] bursts Buffer containing the symbols of 6 bursts, |
| 3819 | * 6 * 2 * 58 == 696 bits total. |
| 3820 | * \param[out] n_errors Number of detected bit errors. |
| 3821 | * \param[out] n_bits_total Total number of bits. |
| 3822 | * \returns Number of bytes used in the output buffer; negative on error. */ |
| 3823 | int gsm0503_tch_hr_facch_decode(uint8_t *data, const sbit_t *bursts, |
| 3824 | int *n_errors, int *n_bits_total) |
| 3825 | { |
| 3826 | sbit_t iB[8 * 114], cB[4 * 114]; |
| 3827 | int steal = 0; |
| 3828 | |
| 3829 | /* FACCH decision: sum of 4 first hu(B) and 4 last hl(B) soft-bits */ |
| 3830 | for (unsigned int i = 0; i < 4; i++) |
| 3831 | steal -= bursts[i * 116 + 58]; /* hu(B) */ |
| 3832 | for (unsigned int i = 2; i < 6; i++) |
| 3833 | steal -= bursts[i * 116 + 57]; /* hl(B) */ |
| 3834 | if (steal <= 0) |
| 3835 | return -1; |
| 3836 | |
| 3837 | /* 4.3.5 Mapping on a Burst: |
| 3838 | * - hu(B)=1 the even numbered bits of the first 2 bursts, |
| 3839 | * - hu(B)=1 & hl(B)=1 all bits of the middle 2 bursts and |
| 3840 | * - hl(B)=1 the odd numbered bits of the last 2 bursts are stolen. */ |
| 3841 | for (unsigned int i = 0; i < 6; i++) |
| 3842 | gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], NULL, i >> 2); |
| 3843 | for (unsigned int i = 2; i < 4; i++) |
| 3844 | gsm0503_tch_burst_unmap(&iB[i * 114 + 456], &bursts[i * 116], NULL, 1); |
| 3845 | |
| 3846 | /* 4.3.4 Interleaving */ |
| 3847 | gsm0503_tch_fr_deinterleave(&cB[0], &iB[0]); |
| 3848 | |
| 3849 | /* 4.3.1-3 as specified for the SACCH in 4.1.1-3 */ |
| 3850 | if (_xcch_decode_cB(&data[0], &cB[0], n_errors, n_bits_total) != 0) |
| 3851 | return -1; |
| 3852 | |
| 3853 | return GSM_MACBLOCK_LEN; |
| 3854 | } |
| 3855 | |
Harald Welte | c663678 | 2017-06-12 14:59:37 +0200 | [diff] [blame] | 3856 | /*! @} */ |