Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 4 | * @author (C) 2015 by Roman Khassraf <rkhassraf@gmail.com> |
| 5 | * (C) 2017 by Piotr Krysik <ptrkrysik@gmail.com> |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 6 | * @section LICENSE |
| 7 | * |
| 8 | * Gr-gsm is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 3, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * Gr-gsm is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with gr-gsm; see the file COPYING. If not, write to |
| 20 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 21 | * Boston, MA 02110-1301, USA. |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
| 28 | #include <gnuradio/io_signature.h> |
| 29 | #include <grgsm/gsmtap.h> |
| 30 | #include "stdio.h" |
| 31 | #include "tch_f_decoder_impl.h" |
| 32 | |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 33 | extern "C" { |
Piotr Krysik | 70c25a1 | 2017-01-03 08:01:23 +0100 | [diff] [blame] | 34 | #include "osmocom/coding/gsm0503_coding.h" |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 37 | #define DATA_BYTES 23 |
| 38 | |
| 39 | namespace gr { |
| 40 | namespace gsm { |
| 41 | |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 42 | static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count) |
| 43 | { |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0; i < count; i++) { |
| 47 | if (*ubits == 0x23) { |
| 48 | ubits++; |
| 49 | sbits++; |
| 50 | continue; |
| 51 | } |
| 52 | if ((*ubits++) & 1) |
| 53 | *sbits++ = -127; |
| 54 | else |
| 55 | *sbits++ = 127; |
| 56 | } |
| 57 | |
| 58 | return count; |
| 59 | } |
| 60 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 61 | tch_f_decoder::sptr |
Piotr Krysik | 0a60e7a | 2016-06-29 22:22:27 +0200 | [diff] [blame] | 62 | tch_f_decoder::make(tch_mode mode, bool boundary_check) |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 63 | { |
| 64 | return gnuradio::get_initial_sptr |
Piotr Krysik | 0a60e7a | 2016-06-29 22:22:27 +0200 | [diff] [blame] | 65 | (new tch_f_decoder_impl(mode, boundary_check)); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Constructor |
| 70 | */ |
Piotr Krysik | 0a60e7a | 2016-06-29 22:22:27 +0200 | [diff] [blame] | 71 | tch_f_decoder_impl::tch_f_decoder_impl(tch_mode mode, bool boundary_check) |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 72 | : gr::block("tch_f_decoder", |
| 73 | gr::io_signature::make(0, 0, 0), |
| 74 | gr::io_signature::make(0, 0, 0)), |
| 75 | d_tch_mode(mode), |
Roman Khassraf | e159333 | 2015-06-02 13:19:01 +0200 | [diff] [blame] | 76 | d_collected_bursts_num(0), |
Roman Khassraf | 23c8d8a | 2015-08-11 11:52:15 +0200 | [diff] [blame] | 77 | d_boundary_check(boundary_check), |
| 78 | d_boundary_decode(!boundary_check), |
Piotr Krysik | 2b97cb1 | 2016-06-29 15:00:07 +0200 | [diff] [blame] | 79 | d_header_sent(false), |
Roman Khassraf | e159333 | 2015-06-02 13:19:01 +0200 | [diff] [blame] | 80 | mBlockCoder(0x10004820009ULL, 40, 224), |
| 81 | mU(228), |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 82 | mP(mU.segment(184,40)), |
| 83 | mD(mU.head(184)), |
| 84 | mDP(mU.head(224)), |
Roman Khassraf | e159333 | 2015-06-02 13:19:01 +0200 | [diff] [blame] | 85 | mC(CONV_SIZE), |
| 86 | mClass1_c(mC.head(378)), |
| 87 | mClass2_c(mC.segment(378, 78)), |
| 88 | mTCHU(189), |
| 89 | mTCHD(260), |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 90 | mClass1A_d(mTCHD.head(50)), |
| 91 | mTCHParity(0x0b, 3, 50) |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 92 | { |
Piotr Krysik | 2b97cb1 | 2016-06-29 15:00:07 +0200 | [diff] [blame] | 93 | //setup input/output ports |
| 94 | message_port_register_in(pmt::mp("bursts")); |
| 95 | set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1)); |
| 96 | message_port_register_out(pmt::mp("msgs")); |
| 97 | message_port_register_out(pmt::mp("voice")); |
Roman Khassraf | 091a80f | 2015-05-22 10:39:16 +0200 | [diff] [blame] | 98 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 99 | int j, k, B; |
| 100 | for (k = 0; k < CONV_SIZE; k++) |
| 101 | { |
| 102 | B = k % 8; |
| 103 | j = 2 * ((49 * k) % 57) + ((k % 8) / 4); |
| 104 | interleave_trans[k] = B * 114 + j; |
| 105 | } |
| 106 | |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 107 | setCodingMode(mode); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | tch_f_decoder_impl::~tch_f_decoder_impl() |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | void tch_f_decoder_impl::decode(pmt::pmt_t msg) |
| 115 | { |
Piotr Krysik | 2b97cb1 | 2016-06-29 15:00:07 +0200 | [diff] [blame] | 116 | if(!d_header_sent) |
| 117 | { |
| 118 | if (d_tch_mode != TCH_FS) |
| 119 | { |
| 120 | const unsigned char amr_nb_magic[7] = "#!AMR\n"; |
| 121 | message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(amr_nb_magic,6))); |
| 122 | } |
| 123 | d_header_sent = true; |
| 124 | } |
| 125 | |
| 126 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 127 | d_bursts[d_collected_bursts_num] = msg; |
| 128 | d_collected_bursts_num++; |
Piotr Krysik | 2b97cb1 | 2016-06-29 15:00:07 +0200 | [diff] [blame] | 129 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 130 | bool stolen = false; |
| 131 | |
| 132 | if (d_collected_bursts_num == 8) |
| 133 | { |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 134 | ubit_t bursts_u[116 * 8]; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 135 | d_collected_bursts_num = 0; |
| 136 | |
| 137 | // reorganize data |
| 138 | for (int ii = 0; ii < 8; ii++) |
| 139 | { |
| 140 | pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]); |
| 141 | int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr); |
| 142 | |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 143 | memcpy(&bursts_u[ii*116], &burst_bits[3],58); |
| 144 | memcpy(&bursts_u[ii*116+58], &burst_bits[3+57+1+26],58); |
| 145 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 146 | for (int jj = 0; jj < 57; jj++) |
| 147 | { |
| 148 | iBLOCK[ii*114+jj] = burst_bits[jj + 3]; |
| 149 | iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1 |
| 150 | } |
| 151 | |
| 152 | if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1)) |
| 153 | { |
| 154 | stolen = true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // deinterleave |
| 159 | for (int k = 0; k < CONV_SIZE; k++) |
| 160 | { |
| 161 | mC[k] = iBLOCK[interleave_trans[k]]; |
| 162 | } |
| 163 | |
| 164 | // Decode stolen frames as FACCH/F |
| 165 | if (stolen) |
| 166 | { |
Roman Khassraf | e159333 | 2015-06-02 13:19:01 +0200 | [diff] [blame] | 167 | mVR204Coder.decode(mC, mU); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 168 | mP.invert(); |
| 169 | |
| 170 | unsigned syndrome = mBlockCoder.syndrome(mDP); |
| 171 | |
| 172 | if (syndrome == 0) |
| 173 | { |
Steve Glass | 6b20e1f | 2016-02-27 19:47:00 +1000 | [diff] [blame] | 174 | unsigned char outmsg[28]; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 175 | unsigned char sbuf_len=224; |
| 176 | int i, j, c, pos=0; |
| 177 | for(i = 0; i < sbuf_len; i += 8) { |
| 178 | for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){ |
| 179 | c |= (!!mU.bit(i + j)) << j; |
| 180 | } |
| 181 | outmsg[pos++] = c & 0xff; |
| 182 | } |
| 183 | |
| 184 | pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]); |
| 185 | gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 186 | int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES]; |
iZsh | 6698793 | 2015-08-16 14:42:18 +0200 | [diff] [blame] | 187 | memcpy(header_plus_data, header, sizeof(gsmtap_hdr)); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 188 | memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES); |
iZsh | 6698793 | 2015-08-16 14:42:18 +0200 | [diff] [blame] | 189 | ((gsmtap_hdr*)header_plus_data)->type = GSMTAP_TYPE_UM; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 190 | |
| 191 | pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr)); |
| 192 | pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob); |
| 193 | |
| 194 | message_port_pub(pmt::mp("msgs"), msg_out); |
Roman Khassraf | 23c8d8a | 2015-08-11 11:52:15 +0200 | [diff] [blame] | 195 | |
| 196 | // if d_boundary_check is enabled, we set d_boundary_decode to true, when a |
| 197 | // "Connect" or "Connect Acknowledge" message is received, and |
| 198 | // we set d_boundary_decode back to false, when "Release" message is received |
| 199 | if (d_boundary_check) |
| 200 | { |
| 201 | // check if this is a call control message |
| 202 | if ((outmsg[3] & 0x0f) == 0x03) |
| 203 | { |
| 204 | // Connect specified in GSM 04.08, 9.3.5 |
| 205 | if ((outmsg[4] & 0x3f) == 0x07) |
| 206 | { |
| 207 | d_boundary_decode = true; |
| 208 | } |
| 209 | // Connect Acknowledge specified in GSM 04.08, 9.3.6 |
| 210 | else if ((outmsg[4] & 0x3f) == 0x0f) |
| 211 | { |
| 212 | d_boundary_decode = true; |
| 213 | } |
| 214 | // Release specified in GSM 04.08, 9.3.18 |
| 215 | else if ((outmsg[4] & 0x3f) == 0x2d) |
| 216 | { |
| 217 | d_boundary_decode = false; |
| 218 | } |
| 219 | } |
| 220 | } |
Roman Khassraf | b868367 | 2015-07-11 15:08:01 +0200 | [diff] [blame] | 221 | |
| 222 | // if we are in an AMR-mode and we receive a channel mode modify message, |
| 223 | // we set the mode according to the multirate configuration from the message |
| 224 | // see GSM 04.18, section 9.1.5 and 10.5.2.21aa |
| 225 | if (d_tch_mode != TCH_FS && d_tch_mode != TCH_EFR) |
| 226 | { |
| 227 | if (outmsg[3] == 0x06 && outmsg[4] == 0x10) |
| 228 | { |
| 229 | // Verify that multirate version 1 is set |
| 230 | if ((outmsg[11] >> 5) == 1) |
| 231 | { |
| 232 | // the set of active codecs, max 4 modes |
| 233 | // active_codec_set[0] corresponds to CODEC_MODE_1 with lowest bit rate |
| 234 | // active_codec_set[3] corresponds to CODEC_MODE_4 with highest bit rate |
| 235 | tch_mode active_codec_set[4]; |
| 236 | uint8_t mode_count = 0; |
| 237 | for (i = 0; i<8; i++) |
| 238 | { |
| 239 | if (((outmsg[12] >> i) & 0x1) == 1 && mode_count < 4) |
| 240 | { |
| 241 | active_codec_set[mode_count++] = static_cast<tch_mode>(7-i); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // check Initial Codec Mode Indicator ICMI |
| 246 | // if ICMI == 1, then use the one defined in start mode field |
| 247 | // else use implicit rule defined in GSM 05.09, section 3.4.3 |
| 248 | if (((outmsg[11] >> 3) & 0x1) == 1) |
| 249 | { |
| 250 | // from start field |
| 251 | setCodingMode(active_codec_set[ (outmsg[11] & 0x3) ]); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | // implicit mode |
| 256 | // if the set contains only 1 codec, we use that one |
| 257 | // else if there are 2 or 3 codecs in the set, we use the one with lowest bitrate |
| 258 | if (mode_count >= 1 && mode_count <= 3) |
| 259 | { |
| 260 | setCodingMode(active_codec_set[0]); |
| 261 | } |
| 262 | // if there are 4 codecs in the set, we use the second lowest bitrate |
| 263 | else if (mode_count == 4) |
| 264 | { |
| 265 | setCodingMode(active_codec_set[1]); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 271 | } |
| 272 | } |
Roman Khassraf | 23c8d8a | 2015-08-11 11:52:15 +0200 | [diff] [blame] | 273 | |
Roman Khassraf | d36123d | 2015-08-11 12:44:56 +0200 | [diff] [blame] | 274 | // if voice boundary_check is enabled and d_boundary_decode is false, we are done |
Roman Khassraf | 23c8d8a | 2015-08-11 11:52:15 +0200 | [diff] [blame] | 275 | if (d_boundary_check && !d_boundary_decode) |
| 276 | { |
| 277 | return; |
| 278 | } |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 279 | |
Piotr Krysik | 0a60e7a | 2016-06-29 22:22:27 +0200 | [diff] [blame] | 280 | // Decode voice frames and send to the output |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 281 | if (d_tch_mode == TCH_FS || d_tch_mode == TCH_EFR) |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 282 | { |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 283 | mVR204Coder.decode(mClass1_c, mTCHU); |
| 284 | mClass2_c.sliced().copyToSegment(mTCHD, 182); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 285 | |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 286 | // 3.1.2.1 |
| 287 | // copy class 1 bits u[] to d[] |
| 288 | for (unsigned k = 0; k <= 90; k++) { |
| 289 | mTCHD[2*k] = mTCHU[k]; |
| 290 | mTCHD[2*k+1] = mTCHU[184-k]; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 291 | } |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 292 | |
| 293 | // 3.1.2.1 |
| 294 | // check parity of class 1A |
| 295 | unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07; |
| 296 | unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07; |
Piotr Krysik | 5c9afd3 | 2016-08-18 18:57:02 +0200 | [diff] [blame] | 297 | unsigned tail = mTCHU.peekField(185, 4); |
| 298 | bool good = (sentParity == calcParity) && (tail == 0); |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 299 | |
| 300 | if (good) |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 301 | { |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 302 | uint8_t frameBuffer[33]; |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 303 | sbit_t bursts_s[116 * 8]; |
| 304 | int n_errors, n_bits_total; |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 305 | unsigned int mTCHFrameLength; |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 306 | ubits2sbits(bursts_u, bursts_s, 116 * 8); |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 307 | |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 308 | if (d_tch_mode == TCH_FS) // GSM-FR |
| 309 | { |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 310 | mTCHFrameLength = 33; |
Piotr Krysik | 694ed81 | 2016-10-02 18:56:04 +0200 | [diff] [blame] | 311 | gsm0503_tch_fr_decode(frameBuffer, bursts_s, 1, 0, &n_errors, &n_bits_total); |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 312 | //std::cout << "Errors: " << n_errors << std::endl; |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 313 | } |
| 314 | else if (d_tch_mode == TCH_EFR) // GSM-EFR |
| 315 | { |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 316 | unsigned char mFrameHeader = 0x3c; |
| 317 | |
| 318 | // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding |
| 319 | BitVector amrFrame(244 + 8); // Same output length as AMR 12.2 |
| 320 | BitVector payload = amrFrame.tail(8); |
| 321 | |
| 322 | BitVector TCHW(260), EFRBits(244); |
| 323 | |
| 324 | // write frame header |
| 325 | amrFrame.fillField(0, mFrameHeader, 8); |
| 326 | |
| 327 | // Undo Um's EFR bit ordering. |
| 328 | mTCHD.unmap(GSM::g660BitOrder, 260, TCHW); |
| 329 | |
| 330 | // Remove repeating bits and CRC to get raw EFR frame (244 bits) |
| 331 | for (unsigned k=0; k<71; k++) |
| 332 | EFRBits[k] = TCHW[k] & 1; |
| 333 | |
| 334 | for (unsigned k=73; k<123; k++) |
| 335 | EFRBits[k-2] = TCHW[k] & 1; |
| 336 | |
| 337 | for (unsigned k=125; k<178; k++) |
| 338 | EFRBits[k-4] = TCHW[k] & 1; |
| 339 | |
| 340 | for (unsigned k=180; k<230; k++) |
| 341 | EFRBits[k-6] = TCHW[k] & 1; |
| 342 | |
| 343 | for (unsigned k=232; k<252; k++) |
| 344 | EFRBits[k-8] = TCHW[k] & 1; |
| 345 | |
| 346 | // Map bits as AMR 12.2k |
| 347 | EFRBits.map(GSM::gAMRBitOrderTCH_AFS12_2, 244, payload); |
| 348 | |
| 349 | // Put the whole frame (hdr + payload) |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 350 | mTCHFrameLength = 32; |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 351 | amrFrame.pack(frameBuffer); |
| 352 | //when itegrating with libosmocore lines above can be removed and line below uncommented, efr decoding with libosmocore need to be tested however |
| 353 | //gsm0503_tch_fr_decode(frameBuffer, bursts_s, 1, 1, &n_errors, &n_bits_total); |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 354 | } |
Piotr Krysik | 2b97cb1 | 2016-06-29 15:00:07 +0200 | [diff] [blame] | 355 | message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer,mTCHFrameLength))); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 356 | } |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 357 | } |
| 358 | else |
| 359 | { |
| 360 | // Handle inband bits, see 3.9.4.1 |
| 361 | // OpenBTS source takes last 8 bits as inband bits for some reason. This may be either a |
| 362 | // divergence between their implementation and GSM specification, which works because |
| 363 | // both their encoder and decoder do it same way, or they handle the issue at some other place |
| 364 | // SoftVector cMinus8 = mC.segment(0, mC.size() - 8); |
| 365 | SoftVector cMinus8 = mC.segment(8, mC.size()); |
| 366 | cMinus8.copyUnPunctured(mTCHUC, mPuncture, mPunctureLth); |
| 367 | |
| 368 | // 3.9.4.4 |
| 369 | // decode from uc[] to u[] |
| 370 | mViterbi->decode(mTCHUC, mTCHU); |
| 371 | |
| 372 | // 3.9.4.3 -- class 1a bits in u[] to d[] |
| 373 | for (unsigned k=0; k < mClass1ALth; k++) { |
| 374 | mTCHD[k] = mTCHU[k]; |
| 375 | } |
| 376 | |
| 377 | // 3.9.4.3 -- class 1b bits in u[] to d[] |
| 378 | for (unsigned k=0; k < mClass1BLth; k++) { |
| 379 | mTCHD[k+mClass1ALth] = mTCHU[k+mClass1ALth+6]; |
| 380 | } |
| 381 | |
| 382 | // Check parity |
| 383 | unsigned sentParity = (~mTCHU.peekField(mClass1ALth,6)) & 0x3f; |
| 384 | BitVector class1A = mTCHU.segment(0, mClass1ALth); |
| 385 | unsigned calcParity = class1A.parity(mTCHParity) & 0x3f; |
| 386 | |
| 387 | bool good = (sentParity == calcParity); |
| 388 | |
| 389 | if (good) |
| 390 | { |
Piotr Krysik | 7923307 | 2018-02-27 08:34:03 +0100 | [diff] [blame] | 391 | unsigned char * frameBuffer = new unsigned char [mAMRFrameLth]; |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 392 | // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding |
| 393 | BitVector amrFrame(mKd + 8); |
| 394 | BitVector payload = amrFrame.tail(8); |
| 395 | |
| 396 | // write frame header |
| 397 | amrFrame.fillField(0, mAMRFrameHeader, 8); |
| 398 | |
| 399 | // We don't unmap here, but copy the decoded bits directly |
| 400 | // Decoder already delivers correct bit order |
| 401 | // mTCHD.unmap(mAMRBitOrder, payload.size(), payload); |
| 402 | mTCHD.copyTo(payload); |
| 403 | amrFrame.pack(frameBuffer); |
Piotr Krysik | 2b97cb1 | 2016-06-29 15:00:07 +0200 | [diff] [blame] | 404 | message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer,mAMRFrameLth))); |
Piotr Krysik | 7923307 | 2018-02-27 08:34:03 +0100 | [diff] [blame] | 405 | delete[] frameBuffer; |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void tch_f_decoder_impl::setCodingMode(tch_mode mode) |
| 412 | { |
Roman Khassraf | b868367 | 2015-07-11 15:08:01 +0200 | [diff] [blame] | 413 | if (mode != TCH_FS && d_tch_mode != TCH_EFR) |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 414 | { |
Roman Khassraf | b868367 | 2015-07-11 15:08:01 +0200 | [diff] [blame] | 415 | d_tch_mode = mode; |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 416 | mKd = GSM::gAMRKd[d_tch_mode]; |
| 417 | mTCHD.resize(mKd); |
| 418 | mTCHU.resize(mKd+6); |
| 419 | mTCHParity = Parity(0x06f,6, GSM::gAMRClass1ALth[d_tch_mode]); |
| 420 | mAMRBitOrder = GSM::gAMRBitOrder[d_tch_mode]; |
| 421 | mClass1ALth = GSM::gAMRClass1ALth[d_tch_mode]; |
| 422 | mClass1BLth = GSM::gAMRKd[d_tch_mode] - GSM::gAMRClass1ALth[d_tch_mode]; |
| 423 | mTCHUC.resize(GSM::gAMRTCHUCLth[d_tch_mode]); |
| 424 | mPuncture = GSM::gAMRPuncture[d_tch_mode]; |
| 425 | mPunctureLth = GSM::gAMRPunctureLth[d_tch_mode]; |
| 426 | mClass1A_d.dup(mTCHD.head(mClass1ALth)); |
| 427 | |
| 428 | switch (d_tch_mode) |
| 429 | { |
| 430 | case TCH_AFS12_2: |
| 431 | mViterbi = new ViterbiTCH_AFS12_2(); |
| 432 | mAMRFrameLth = 32; |
| 433 | mAMRFrameHeader = 0x3c; |
| 434 | break; |
| 435 | case TCH_AFS10_2: |
| 436 | mViterbi = new ViterbiTCH_AFS10_2(); |
| 437 | mAMRFrameLth = 27; |
| 438 | mAMRFrameHeader = 0x3c; |
| 439 | break; |
| 440 | case TCH_AFS7_95: |
| 441 | mViterbi = new ViterbiTCH_AFS7_95(); |
| 442 | mAMRFrameLth = 21; |
| 443 | mAMRFrameHeader = 0x3c; |
| 444 | break; |
| 445 | case TCH_AFS7_4: |
| 446 | mViterbi = new ViterbiTCH_AFS7_4(); |
| 447 | mAMRFrameLth = 20; |
| 448 | mAMRFrameHeader = 0x3c; |
| 449 | break; |
| 450 | case TCH_AFS6_7: |
| 451 | mViterbi = new ViterbiTCH_AFS6_7(); |
| 452 | mAMRFrameLth = 18; |
| 453 | mAMRFrameHeader = 0x3c; |
| 454 | break; |
| 455 | case TCH_AFS5_9: |
| 456 | mViterbi = new ViterbiTCH_AFS5_9(); |
| 457 | mAMRFrameLth = 16; |
| 458 | mAMRFrameHeader = 0x14; |
| 459 | break; |
| 460 | case TCH_AFS5_15: |
| 461 | mViterbi = new ViterbiTCH_AFS5_15(); |
| 462 | mAMRFrameLth = 14; |
| 463 | mAMRFrameHeader = 0x3c; |
| 464 | break; |
| 465 | case TCH_AFS4_75: |
| 466 | mViterbi = new ViterbiTCH_AFS4_75(); |
| 467 | mAMRFrameLth = 13; |
| 468 | mAMRFrameHeader = 0x3c; |
| 469 | break; |
| 470 | default: |
| 471 | mViterbi = new ViterbiTCH_AFS12_2(); |
| 472 | mAMRFrameLth = 32; |
| 473 | mAMRFrameHeader = 0x3c; |
| 474 | break; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | } /* namespace gsm */ |
| 479 | } /* namespace gr */ |
| 480 | |