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