diff options
author | Kirill Zakharenko <earwin@gmail.com> | 2020-05-01 18:21:58 +0300 |
---|---|---|
committer | Kirill Zakharenko <earwin@gmail.com> | 2020-05-01 18:21:58 +0300 |
commit | 357ab3abba5a1be0a66f8ae6c6a0460ddb4b7c06 (patch) | |
tree | ad0979169f0d7911bb503466036f36f1489bc714 /src/coding | |
parent | 459224a99391fa9adf22a45cb6a63d56703aa3a8 (diff) | |
parent | d87d6f177837c848381e362d3880509ba275cac8 (diff) |
Merge master into fairwaves/production
Diffstat (limited to 'src/coding')
-rw-r--r-- | src/coding/Makefile.am | 3 | ||||
-rw-r--r-- | src/coding/gsm0503_amr_dtx.c | 314 | ||||
-rw-r--r-- | src/coding/gsm0503_coding.c | 193 | ||||
-rw-r--r-- | src/coding/gsm0503_parity.c | 11 | ||||
-rw-r--r-- | src/coding/gsm0503_tables.c | 6 | ||||
-rw-r--r-- | src/coding/libosmocoding.map | 7 |
6 files changed, 527 insertions, 7 deletions
diff --git a/src/coding/Makefile.am b/src/coding/Makefile.am index f47fe457..b023668e 100644 --- a/src/coding/Makefile.am +++ b/src/coding/Makefile.am @@ -20,7 +20,8 @@ libosmocoding_la_SOURCES = \ gsm0503_mapping.c \ gsm0503_tables.c \ gsm0503_parity.c \ - gsm0503_coding.c + gsm0503_coding.c \ + gsm0503_amr_dtx.c libosmocoding_la_LDFLAGS = \ $(LTLDFLAGS_OSMOCODING) \ -version-info \ diff --git a/src/coding/gsm0503_amr_dtx.c b/src/coding/gsm0503_amr_dtx.c new file mode 100644 index 00000000..724cf091 --- /dev/null +++ b/src/coding/gsm0503_amr_dtx.c @@ -0,0 +1,314 @@ +/* + * (C) 2020 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> + +#include <osmocom/core/bits.h> +#include <osmocom/core/conv.h> +#include <osmocom/core/utils.h> +#include <osmocom/coding/gsm0503_amr_dtx.h> +#include <osmocom/coding/gsm0503_parity.h> +#include <osmocom/gsm/gsm0503.h> + +/* See also: 3GPP TS 05.03, chapter 3.10.1.3, 3.10.5.2 Identification marker */ +static const ubit_t id_marker_1[] = { 1, 0, 1, 1, 0, 0, 0, 0, 1 }; + +/* See also: 3GPP TS 05.03, chapter 3.9.1.3, 3.10.2.2, 3.10.2.2 Identification marker */ +static const ubit_t id_marker_0[] = { 0, 1, 0, 0, 1, 1, 1, 1, 0 }; + +/* See also: 3GPP TS 05.03, chapter 3.9 Adaptive multi rate speech channel at full rate (TCH/AFS) */ +static const ubit_t codec_mode_1_sid[] = { 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0 }; +static const ubit_t codec_mode_2_sid[] = { 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0 }; +static const ubit_t codec_mode_3_sid[] = { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }; +static const ubit_t codec_mode_4_sid[] = { 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1 }; + +const struct value_string gsm0503_amr_dtx_frame_names[] = { + { AFS_SID_FIRST, "AFS_SID_FIRST" }, + { AFS_SID_UPDATE, "AFS_SID_UPDATE" }, + { AFS_ONSET, "AFS_ONSET" }, + { AHS_SID_UPDATE, "AHS_SID_UPDATE" }, + { AHS_SID_FIRST_P1, "AHS_SID_FIRST_P1" }, + { AHS_SID_FIRST_P2, "AHS_SID_FIRST_P2" }, + { AHS_ONSET, "AHS_ONSET" }, + { AHS_SID_FIRST_INH, "AHS_SID_FIRST_INH" }, + { AHS_SID_UPDATE_INH, "AHS_SID_UPDATE_INH" }, + { AMR_OTHER, "NON DTX FRAME (OTHER)" }, + { 0, NULL } +}; + +static bool detect_afs_id_marker(int *n_errors, int *n_bits_total, const ubit_t * ubits, uint8_t offset, uint8_t count, + const ubit_t * id_marker, uint8_t id_marker_len) +{ + unsigned int i, k; + unsigned int id_bit_nr = 0; + int errors = 0; + int bits = 0; + + /* Override coded in-band data */ + ubits += offset; + + /* Check for identification marker bits */ + for (i = 0; i < count; i++) { + for (k = 0; k < 4; k++) { + if (id_marker[id_bit_nr % id_marker_len] != *ubits) + errors++; + id_bit_nr++; + ubits++; + bits++; + } + + /* Jump to the next block of 4 bits */ + ubits += 4; + } + + *n_errors = errors; + *n_bits_total = bits; + + /* Tolerate up to 1/8 errornous bits */ + return *n_errors < *n_bits_total / 8; +} + +static bool detect_ahs_id_marker(int *n_errors, int *n_bits_total, const ubit_t * ubits, const ubit_t * id_marker) +{ + unsigned int i, k; + int errors = 0; + int bits = 0; + + /* Override coded in-band data */ + ubits += 16; + + /* Check first identification marker bits (23*9 bits) */ + for (i = 0; i < 23; i++) { + for (k = 0; k < 9; k++) { + if (id_marker[k] != *ubits) + errors++; + ubits++; + bits++; + } + } + + /* Check remaining identification marker bits (5 bits) */ + for (k = 0; k < 5; k++) { + if (id_marker[k] != *ubits) + errors++; + ubits++; + bits++; + } + + *n_errors = errors; + *n_bits_total = bits; + + /* Tolerate up to 1/8 errornous bits */ + return *n_errors < *n_bits_total / 8; +} + +static bool detect_interleaved_ahs_id_marker(int *n_errors, int *n_bits_total, const ubit_t * ubits, uint8_t offset, + uint8_t n_bits, const ubit_t * id_marker, uint8_t id_marker_len) +{ + unsigned int i, k; + int errors = 0; + int bits = 0; + uint8_t full_rounds = n_bits / id_marker_len; + uint8_t remainder = n_bits % id_marker_len; + + /* Override coded in-band data */ + ubits += offset; + + /* Check first identification marker bits (23*9 bits) */ + for (i = 0; i < full_rounds; i++) { + for (k = 0; k < id_marker_len; k++) { + if (id_marker[k] != *ubits) + errors++; + ubits += 2; + bits++; + } + } + + /* Check remaining identification marker bits (5 bits) */ + for (k = 0; k < remainder; k++) { + if (id_marker[k] != *ubits) + errors++; + ubits += 2; + bits++; + } + + *n_errors = errors; + *n_bits_total = bits; + + /* Tolerate up to 1/8 errornous bits */ + return *n_errors < *n_bits_total / 8; +} + +/* Detect a an FR AMR SID_FIRST frame by its identifcation marker */ +static bool detect_afs_sid_first(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + return detect_afs_id_marker(n_errors, n_bits_total, ubits, 32, 53, id_marker_0, 9); +} + +/* Detect an FR AMR SID_FIRST frame by its identification marker */ +static bool detect_afs_sid_update(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + return detect_afs_id_marker(n_errors, n_bits_total, ubits, 36, 53, id_marker_0, 9); +} + +/* Detect an FR AMR SID_FIRST frame by its repeating coded inband data */ +static bool detect_afs_onset(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + bool rc; + + rc = detect_afs_id_marker(n_errors, n_bits_total, ubits, 4, 57, codec_mode_1_sid, 16); + if (rc) + return true; + + rc = detect_afs_id_marker(n_errors, n_bits_total, ubits, 4, 57, codec_mode_2_sid, 16); + if (rc) + return true; + + rc = detect_afs_id_marker(n_errors, n_bits_total, ubits, 4, 57, codec_mode_3_sid, 16); + if (rc) + return true; + + rc = detect_afs_id_marker(n_errors, n_bits_total, ubits, 4, 57, codec_mode_4_sid, 16); + if (rc) + return true; + + return false; +} + +/* Detect an HR AMR SID UPDATE frame by its identification marker */ +static bool detect_ahs_sid_update(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + return detect_ahs_id_marker(n_errors, n_bits_total, ubits, id_marker_1); +} + +/* Detect an HR AMR SID FIRST (part 1) frame by its identification marker */ +static bool detect_ahs_sid_first_p1(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + return detect_ahs_id_marker(n_errors, n_bits_total, ubits, id_marker_0); +} + +/* Detect an HR AMR SID FIRST (part 2) frame by its repeating coded inband data */ +static bool detect_ahs_sid_first_p2(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + bool rc; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 0, 114, codec_mode_1_sid, 16); + if (rc) + return true; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 0, 114, codec_mode_2_sid, 16); + if (rc) + return true; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 0, 114, codec_mode_3_sid, 16); + if (rc) + return true; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 0, 114, codec_mode_4_sid, 16); + if (rc) + return true; + + return false; +} + +/* Detect an HR AMR ONSET frame by its repeating coded inband data */ +static bool detect_ahs_onset(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + bool rc; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 1, 114, codec_mode_1_sid, 16); + if (rc) + return true; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 1, 114, codec_mode_2_sid, 16); + if (rc) + return true; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 1, 114, codec_mode_3_sid, 16); + if (rc) + return true; + + rc = detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 1, 114, codec_mode_4_sid, 16); + if (rc) + return true; + + return false; +} + +/* Detect an HR AMR SID FIRST INHIBIT frame by its identification marker */ +static bool detect_ahs_sid_first_inh(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + return detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 33, 212, id_marker_1, 9); +} + +/* Detect an HR AMR SID UPDATE INHIBIT frame by its identification marker */ +static bool detect_ahs_sid_update_inh(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + return detect_interleaved_ahs_id_marker(n_errors, n_bits_total, ubits, 33, 212, id_marker_0, 9); +} + +/*! Detect FR AMR DTX frame in unmapped, deinterleaved frame bits. + * \param[in] ubits input bits (456 bit). + * \param[out] n_errors number of errornous bits. + * \param[out] n_bits_total number of checked bits. + * \returns dtx frame type. */ +enum gsm0503_amr_dtx_frames gsm0503_detect_afs_dtx_frame(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + if (detect_afs_sid_first(n_errors, n_bits_total, ubits)) + return AFS_SID_FIRST; + if (detect_afs_sid_update(n_errors, n_bits_total, ubits)) + return AFS_SID_UPDATE; + if (detect_afs_onset(n_errors, n_bits_total, ubits)) + return AFS_ONSET; + + *n_errors = 0; + *n_bits_total = 0; + return AMR_OTHER; +} + +/*! Detect HR AMR DTX frame in unmapped, deinterleaved frame bits. + * \param[in] ubits input bits (456 bit). + * \param[out] n_errors number of errornous bits. + * \param[out] n_bits_total number of checked bits. + * \returns dtx frame type, */ +enum gsm0503_amr_dtx_frames gsm0503_detect_ahs_dtx_frame(int *n_errors, int *n_bits_total, const ubit_t * ubits) +{ + if (detect_ahs_sid_update(n_errors, n_bits_total, ubits)) + return AHS_SID_UPDATE; + if (detect_ahs_sid_first_inh(n_errors, n_bits_total, ubits)) + return AHS_SID_FIRST_INH; + if (detect_ahs_sid_update_inh(n_errors, n_bits_total, ubits)) + return AHS_SID_UPDATE_INH; + if (detect_ahs_sid_first_p1(n_errors, n_bits_total, ubits)) + return AHS_SID_FIRST_P1; + if (detect_ahs_sid_first_p2(n_errors, n_bits_total, ubits)) + return AHS_SID_FIRST_P2; + if (detect_ahs_onset(n_errors, n_bits_total, ubits)) + return AHS_ONSET; + + *n_errors = 0; + *n_bits_total = 0; + return AMR_OTHER; +} diff --git a/src/coding/gsm0503_coding.c b/src/coding/gsm0503_coding.c index 7385d233..1bec56ea 100644 --- a/src/coding/gsm0503_coding.c +++ b/src/coding/gsm0503_coding.c @@ -47,6 +47,7 @@ #include <osmocom/coding/gsm0503_tables.h> #include <osmocom/coding/gsm0503_coding.h> #include <osmocom/coding/gsm0503_parity.h> +#include <osmocom/coding/gsm0503_amr_dtx.h> /*! \mainpage libosmocoding Documentation * @@ -1168,7 +1169,7 @@ int gsm0503_pdtch_decode(uint8_t *l2_data, const sbit_t *bursts, uint8_t *usf_p, } /* - * EGPRS PDTCH UL block encoding + * EGPRS PDTCH DL block encoding */ static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, int usf) { @@ -1176,7 +1177,7 @@ static int egprs_type3_map(ubit_t *bursts, const ubit_t *hc, const ubit_t *dc, i ubit_t iB[456]; const ubit_t *hl_hn = gsm0503_pdtch_hl_hn_ubit[3]; - gsm0503_mcs1_dl_interleave(gsm0503_usf2six[usf], hc, dc, iB); + gsm0503_mcs1_dl_interleave(gsm0503_usf2twelve_ubit[usf], hc, dc, iB); for (i = 0; i < 4; i++) { gsm0503_xcch_burst_map(&iB[i * 114], &bursts[i * 116], @@ -1332,7 +1333,7 @@ static int egprs_parse_dl_cps(struct egprs_cps *cps, * \param[out] bursts caller-allocated buffer for unpacked burst bits * \param[in] l2_data L2 (MAC) block to be encoded * \param[in] l2_len length of l2_data in bytes, used to determine MCS - * \returns 0 on success; negative on error */ + * \returns number of bits encoded; negative on error */ int gsm0503_pdtch_egprs_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len) { @@ -1427,7 +1428,7 @@ bad_header: * \param[out] bursts caller-allocated buffer for unpacked burst bits * \param[in] l2_data L2 (MAC) block to be encoded * \param[in] l2_len length of l2_data in bytes, used to determine CS - * \returns 0 on success; negative on error */ + * \returns number of bits encoded; negative on error */ int gsm0503_pdtch_encode(ubit_t *bursts, const uint8_t *l2_data, uint8_t l2_len) { ubit_t iB[456], cB[676]; @@ -1635,6 +1636,39 @@ static void tch_amr_disassemble(ubit_t *d_bits, const uint8_t *tch_data, int len d_bits[i] = (tch_data[j >> 3] >> (7 - (j & 7))) & 1; } +/* Append STI and MI bits to the SID_UPDATE frame, see also + * 3GPP TS 26.101, chapter 4.2.3 AMR Core Frame with comfort noise bits */ +static void tch_amr_sid_update_append(ubit_t *sid_update, uint8_t sti, uint8_t mi) +{ + /* Zero out the space that had been used by the CRC14 */ + memset(sid_update + 35, 0, 14); + + /* Append STI and MI parameters */ + sid_update[35] = sti & 1; + sid_update[36] = mi & 1; + sid_update[37] = mi >> 1 & 1; + sid_update[38] = mi >> 2 & 1; +} + +/* Extract a SID UPDATE fram the sbits of an FR AMR frame */ +static void extract_afs_sid_update(sbit_t *sid_update, const sbit_t *sbits) +{ + + unsigned int i; + + sbits += 32; + + for (i = 0; i < 53; i++) { + sid_update[0] = sbits[0]; + sid_update[1] = sbits[1]; + sid_update[2] = sbits[2]; + sid_update[3] = sbits[3]; + sid_update += 4; + sbits += 8; + } + +} + /* re-arrange according to TS 05.03 Table 2 (receiver) */ static void tch_fr_d_to_b(ubit_t *b_bits, const ubit_t *d_bits) { @@ -2101,10 +2135,37 @@ int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts, int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, uint8_t *cmr, int *n_errors, int *n_bits_total) { + return gsm0503_tch_afs_decode_dtx(tch_data, bursts, codec_mode_req, + codec, codecs, ft, cmr, n_errors, + n_bits_total, NULL); +} + +/*! Perform channel decoding of a TCH/AFS channel according TS 05.03 + * \param[out] tch_data Codec frame in RTP payload format + * \param[in] bursts buffer containing the symbols of 8 bursts + * \param[in] codec_mode_req is this CMR (1) or CMC (0) + * \param[in] codec array of active codecs (active codec set) + * \param[in] codecs number of codecs in \a codec + * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise + * \param[out] cmr Output in \a codec_mode_req = 1 + * \param[out] n_errors Number of detected bit errors + * \param[out] n_bits_total Total number of bits + * \param[inout] dtx DTX frame type output, previous DTX frame type input + * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) + * codec out of range; negative on error + */ +int gsm0503_tch_afs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, + int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, + uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) +{ sbit_t iB[912], cB[456], h; ubit_t d[244], p[6], conv[250]; int i, j, k, best = 0, rv, len, steal = 0, id = 0; + ubit_t cBd[456]; *n_errors = 0; *n_bits_total = 0; + static ubit_t sid_first_dummy[64] = { 0 }; + sbit_t sid_update_enc[256]; + uint8_t dtx_prev; for (i=0; i<8; i++) { gsm0503_tch_burst_unmap(&iB[i * 114], &bursts[i * 116], &h, i >> 2); @@ -2123,6 +2184,50 @@ int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts, return GSM_MACBLOCK_LEN; } + /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ + if (dtx) { + osmo_sbit2ubit(cBd, cB, 456); + dtx_prev = *dtx; + *dtx = gsm0503_detect_afs_dtx_frame(n_errors, n_bits_total, cBd); + + if (dtx_prev == AFS_SID_UPDATE && *dtx == AMR_OTHER) { + /* NOTE: The AFS_SID_UPDATE frame is splitted into + * two half rate frames. If the id marker frame + * (AFS_SID_UPDATE) is detected the following frame + * contains the actual comfort noised data part of + * (AFS_SID_UPDATE_CN). */ + *dtx = AFS_SID_UPDATE_CN; + + extract_afs_sid_update(sid_update_enc, cB); + osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, + sid_update_enc, conv, n_errors, + n_bits_total); + rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, + 35, conv + 35); + if (rv != 0) { + /* Error checking CRC14 for an AMR SID_UPDATE frame */ + return -1; + } + + tch_amr_sid_update_append(conv, 1, + (codec_mode_req) ? codec[*ft] + : codec[id]); + tch_amr_reassemble(tch_data, conv, 39); + len = 5; + goto out; + } else if (*dtx == AFS_SID_FIRST) { + tch_amr_sid_update_append(sid_first_dummy, 0, + (codec_mode_req) ? codec[*ft] + : codec[id]); + tch_amr_reassemble(tch_data, conv, 39); + len = 5; + goto out; + } else if (*dtx == AFS_ONSET) { + len = 0; + goto out; + } + } + for (i = 0; i < 4; i++) { for (j = 0, k = 0; j < 8; j++) k += abs(((int)gsm0503_afs_ic_sbit[i][j]) - ((int)cB[j])); @@ -2283,6 +2388,7 @@ int gsm0503_tch_afs_decode(uint8_t *tch_data, const sbit_t *bursts, return -1; } +out: /* Change codec request / indication, if frame is valid */ if (codec_mode_req) *cmr = id; @@ -2480,9 +2586,36 @@ int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, uint8_t *cmr, int *n_errors, int *n_bits_total) { + return gsm0503_tch_ahs_decode_dtx(tch_data, bursts, odd, codec_mode_req, + codec, codecs, ft, cmr, n_errors, + n_bits_total, NULL); +} + +/*! Perform channel decoding of a TCH/AFS channel according TS 05.03 + * \param[out] tch_data Codec frame in RTP payload format + * \param[in] bursts buffer containing the symbols of 8 bursts + * \param[in] odd Is this an odd (1) or even (0) frame number? + * \param[in] codec_mode_req is this CMR (1) or CMC (0) + * \param[in] codec array of active codecs (active codec set) + * \param[in] codecs number of codecs in \a codec + * \param ft Frame Type; Input if \a codec_mode_req = 1, Output * otherwise + * \param[out] cmr Output in \a codec_mode_req = 1 + * \param[out] n_errors Number of detected bit errors + * \param[out] n_bits_total Total number of bits + * \param[inout] dtx DTX frame type output, previous DTX frame type input + * \returns (>=4) length of bytes used in \a tch_data output buffer; ([0,3]) + * codec out of range; negative on error + */ +int gsm0503_tch_ahs_decode_dtx(uint8_t *tch_data, const sbit_t *bursts, int odd, + int codec_mode_req, uint8_t *codec, int codecs, uint8_t *ft, + uint8_t *cmr, int *n_errors, int *n_bits_total, uint8_t *dtx) +{ sbit_t iB[912], cB[456], h; ubit_t d[244], p[6], conv[135]; int i, j, k, best = 0, rv, len, steal = 0, id = 0; + ubit_t cBd[456]; + static ubit_t sid_first_dummy[64] = { 0 }; + uint8_t dtx_prev; /* only unmap the stealing bits */ if (!odd) { @@ -2526,6 +2659,52 @@ int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, gsm0503_tch_hr_deinterleave(cB, iB); + /* Determine the DTX frame type (SID_UPDATE, ONSET etc...) */ + if (dtx) { + osmo_sbit2ubit(cBd, cB, 456); + dtx_prev = *dtx; + *dtx = gsm0503_detect_ahs_dtx_frame(n_errors, n_bits_total, cBd); + + if (dtx_prev == AHS_SID_UPDATE && *dtx == AMR_OTHER) { + /* NOTE: The AHS_SID_UPDATE frame is splitted into + * two half rate frames. If the id marker frame + * (AHS_SID_UPDATE) is detected the following frame + * contains the actual comfort noised data part of + * (AHS_SID_UPDATE_CN). */ + *dtx = AHS_SID_UPDATE_CN; + + osmo_conv_decode_ber(&gsm0503_tch_axs_sid_update, + cB + 16, conv, n_errors, + n_bits_total); + rv = osmo_crc16gen_check_bits(&gsm0503_amr_crc14, conv, + 35, conv + 35); + if (rv != 0) { + /* Error checking CRC14 for an AMR SID_UPDATE frame */ + return -1; + } + + tch_amr_sid_update_append(conv, 1, + (codec_mode_req) ? codec[*ft] + : codec[id]); + tch_amr_reassemble(tch_data, conv, 39); + len = 5; + goto out; + } else if (*dtx == AHS_SID_FIRST_P2) { + tch_amr_sid_update_append(sid_first_dummy, 0, + (codec_mode_req) ? codec[*ft] + : codec[id]); + tch_amr_reassemble(tch_data, sid_first_dummy, 39); + len = 5; + goto out; + } else if (*dtx == AHS_SID_UPDATE || *dtx == AHS_ONSET + || *dtx == AHS_SID_FIRST_INH + || *dtx == AHS_SID_UPDATE_INH + || *dtx == AHS_SID_FIRST_P1) { + len = 0; + goto out; + } + } + for (i = 0; i < 4; i++) { for (j = 0, k = 0; j < 4; j++) k += abs(((int)gsm0503_ahs_ic_sbit[i][j]) - ((int)cB[j])); @@ -2670,6 +2849,7 @@ int gsm0503_tch_ahs_decode(uint8_t *tch_data, const sbit_t *bursts, int odd, return -1; } +out: /* Change codec request / indication, if frame is valid */ if (codec_mode_req) *cmr = id; @@ -2879,7 +3059,7 @@ static inline int16_t rach_decode_ber(const sbit_t *burst, uint8_t bsic, bool is osmo_ubit2pbit_ext(ra, 0, conv, 0, nbits, 1); - return is_11bit ? osmo_load16le(ra) : ra[0]; + return is_11bit ? ((ra[0] << 3) | (ra[1] & 0x07)) : ra[0]; } /*! Decode the Extended (11-bit) RACH according to 3GPP TS 45.003 @@ -2974,7 +3154,8 @@ int gsm0503_rach_ext_encode(ubit_t *burst, uint16_t ra11, uint8_t bsic, bool is_ uint8_t ra[2] = { 0 }, nbits = 8; if (is_11bit) { - osmo_store16le(ra11, ra); + ra[0] = (uint8_t) (ra11 >> 3); + ra[1] = (uint8_t) (ra11 & 0x07); nbits = 11; } else ra[0] = (uint8_t)ra11; diff --git a/src/coding/gsm0503_parity.c b/src/coding/gsm0503_parity.c index 874114ff..a8daacc7 100644 --- a/src/coding/gsm0503_parity.c +++ b/src/coding/gsm0503_parity.c @@ -134,4 +134,15 @@ const struct osmo_crc8gen_code gsm0503_amr_crc6 = { .remainder = 0x3f, }; +/*! GSM AMR parity (SID_UPDATE) + * + * g(x) = x^14 + x^13 + x^5 + x^3 + x^2 + 1 + */ +const struct osmo_crc16gen_code gsm0503_amr_crc14 = { + .bits = 14, + .poly = 0x202d, + .init = 0x0000, + .remainder = 0x3fff, +}; + /*! @} */ diff --git a/src/coding/gsm0503_tables.c b/src/coding/gsm0503_tables.c index 5fe634bf..df0abeed 100644 --- a/src/coding/gsm0503_tables.c +++ b/src/coding/gsm0503_tables.c @@ -63,6 +63,9 @@ const sbit_t gsm0503_pdtch_edge_hl_hn_sbit[3][8] = { { -127,-127, -127, 127, 127,-127, -127,-127 }, }; +/* + * 3GPP TS 05.03 sec 5.1.2.2 "Block code". Rows re-ordered to be indxed by USF in host bit order. + */ const ubit_t gsm0503_usf2six[8][6] = { { 0,0,0, 0,0,0 }, { 1,0,0, 1,0,1 }, @@ -74,6 +77,9 @@ const ubit_t gsm0503_usf2six[8][6] = { { 1,1,1, 0,0,0 }, }; +/* + * 3GPP TS 05.03 sec 5.1.4.2 "Block code". Rows re-ordered to be indxed by USF in host bit order. + */ const ubit_t gsm0503_usf2twelve_ubit[8][12] = { { 0,0,0, 0,0,0, 0,0,0, 0,0,0 }, { 1,1,0, 1,0,0, 0,0,1, 0,1,1 }, diff --git a/src/coding/libosmocoding.map b/src/coding/libosmocoding.map index 87b38864..325b6d80 100644 --- a/src/coding/libosmocoding.map +++ b/src/coding/libosmocoding.map @@ -56,6 +56,7 @@ gsm0503_sch_crc10; gsm0503_tch_fr_crc3; gsm0503_tch_efr_crc8; gsm0503_amr_crc6; +gsm0503_amr_crc14; gsm0503_xcch_burst_unmap; gsm0503_xcch_burst_map; @@ -106,8 +107,10 @@ gsm0503_tch_hr_encode; gsm0503_tch_hr_decode; gsm0503_tch_afs_encode; gsm0503_tch_afs_decode; +gsm0503_tch_afs_decode_dtx; gsm0503_tch_ahs_encode; gsm0503_tch_ahs_decode; +gsm0503_tch_ahs_decode_dtx; gsm0503_rach_ext_encode; gsm0503_rach_ext_decode; gsm0503_rach_ext_decode_ber; @@ -116,6 +119,10 @@ gsm0503_rach_decode; gsm0503_rach_decode_ber; gsm0503_sch_encode; gsm0503_sch_decode; +gsm0503_amr_dtx_frame_names; +gsm0503_amr_dtx_frame_name; +gsm0503_detect_afs_dtx_frame; +gsm0503_detect_ahs_dtx_frame; local: *; }; |