blob: 5b77be2b02c07884f9bb1f6b194fb6ba7c800cd2 [file] [log] [blame]
Roman Khassraf059bab92015-05-20 12:49:46 +02001/* -*- c++ -*- */
2/*
3 * @file
Roman Khassraf05bfb822015-05-22 11:46:00 +02004 * @author Roman Khassraf <rkhassraf@gmail.com>
Roman Khassraf059bab92015-05-20 12:49:46 +02005 * @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
Piotr Krysik694ed812016-10-02 18:56:04 +020032extern "C" {
Piotr Krysik70c25a12017-01-03 08:01:23 +010033 #include "osmocom/coding/gsm0503_coding.h"
Piotr Krysik694ed812016-10-02 18:56:04 +020034}
35
Roman Khassraf059bab92015-05-20 12:49:46 +020036#define DATA_BYTES 23
37
38namespace gr {
39 namespace gsm {
40
Piotr Krysik694ed812016-10-02 18:56:04 +020041 static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count)
42 {
43 int i;
44
45 for (i = 0; i < count; i++) {
46 if (*ubits == 0x23) {
47 ubits++;
48 sbits++;
49 continue;
50 }
51 if ((*ubits++) & 1)
52 *sbits++ = -127;
53 else
54 *sbits++ = 127;
55 }
56
57 return count;
58 }
59
Roman Khassraf059bab92015-05-20 12:49:46 +020060 tch_f_decoder::sptr
Piotr Krysik0a60e7a2016-06-29 22:22:27 +020061 tch_f_decoder::make(tch_mode mode, bool boundary_check)
Roman Khassraf059bab92015-05-20 12:49:46 +020062 {
63 return gnuradio::get_initial_sptr
Piotr Krysik0a60e7a2016-06-29 22:22:27 +020064 (new tch_f_decoder_impl(mode, boundary_check));
Roman Khassraf059bab92015-05-20 12:49:46 +020065 }
66
67 /*
68 * Constructor
69 */
Piotr Krysik0a60e7a2016-06-29 22:22:27 +020070 tch_f_decoder_impl::tch_f_decoder_impl(tch_mode mode, bool boundary_check)
Roman Khassraf059bab92015-05-20 12:49:46 +020071 : gr::block("tch_f_decoder",
72 gr::io_signature::make(0, 0, 0),
73 gr::io_signature::make(0, 0, 0)),
74 d_tch_mode(mode),
Roman Khassrafe1593332015-06-02 13:19:01 +020075 d_collected_bursts_num(0),
Roman Khassraf23c8d8a2015-08-11 11:52:15 +020076 d_boundary_check(boundary_check),
77 d_boundary_decode(!boundary_check),
Piotr Krysik2b97cb12016-06-29 15:00:07 +020078 d_header_sent(false),
Roman Khassrafe1593332015-06-02 13:19:01 +020079 mBlockCoder(0x10004820009ULL, 40, 224),
80 mU(228),
Roman Khassrafd38206c2015-06-07 16:26:29 +020081 mP(mU.segment(184,40)),
82 mD(mU.head(184)),
83 mDP(mU.head(224)),
Roman Khassrafe1593332015-06-02 13:19:01 +020084 mC(CONV_SIZE),
85 mClass1_c(mC.head(378)),
86 mClass2_c(mC.segment(378, 78)),
87 mTCHU(189),
88 mTCHD(260),
Roman Khassrafd38206c2015-06-07 16:26:29 +020089 mClass1A_d(mTCHD.head(50)),
90 mTCHParity(0x0b, 3, 50)
Roman Khassraf059bab92015-05-20 12:49:46 +020091 {
Piotr Krysik2b97cb12016-06-29 15:00:07 +020092 //setup input/output ports
93 message_port_register_in(pmt::mp("bursts"));
94 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
95 message_port_register_out(pmt::mp("msgs"));
96 message_port_register_out(pmt::mp("voice"));
Roman Khassraf091a80f2015-05-22 10:39:16 +020097
Roman Khassraf059bab92015-05-20 12:49:46 +020098 int j, k, B;
99 for (k = 0; k < CONV_SIZE; k++)
100 {
101 B = k % 8;
102 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
103 interleave_trans[k] = B * 114 + j;
104 }
105
Roman Khassrafd38206c2015-06-07 16:26:29 +0200106 setCodingMode(mode);
Roman Khassraf059bab92015-05-20 12:49:46 +0200107 }
108
109 tch_f_decoder_impl::~tch_f_decoder_impl()
110 {
111 }
112
113 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
114 {
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200115 if(!d_header_sent)
116 {
117 if (d_tch_mode != TCH_FS)
118 {
119 const unsigned char amr_nb_magic[7] = "#!AMR\n";
120 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(amr_nb_magic,6)));
121 }
122 d_header_sent = true;
123 }
124
125
Roman Khassraf059bab92015-05-20 12:49:46 +0200126 d_bursts[d_collected_bursts_num] = msg;
127 d_collected_bursts_num++;
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200128
Roman Khassraf059bab92015-05-20 12:49:46 +0200129 bool stolen = false;
130
131 if (d_collected_bursts_num == 8)
132 {
Piotr Krysik694ed812016-10-02 18:56:04 +0200133 ubit_t bursts_u[116 * 8];
Roman Khassraf059bab92015-05-20 12:49:46 +0200134 d_collected_bursts_num = 0;
135
136 // reorganize data
137 for (int ii = 0; ii < 8; ii++)
138 {
139 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
140 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
141
Piotr Krysik694ed812016-10-02 18:56:04 +0200142 memcpy(&bursts_u[ii*116], &burst_bits[3],58);
143 memcpy(&bursts_u[ii*116+58], &burst_bits[3+57+1+26],58);
144
Roman Khassraf059bab92015-05-20 12:49:46 +0200145 for (int jj = 0; jj < 57; jj++)
146 {
147 iBLOCK[ii*114+jj] = burst_bits[jj + 3];
148 iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
149 }
150
151 if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1))
152 {
153 stolen = true;
154 }
155 }
156
157 // deinterleave
158 for (int k = 0; k < CONV_SIZE; k++)
159 {
160 mC[k] = iBLOCK[interleave_trans[k]];
161 }
162
163 // Decode stolen frames as FACCH/F
164 if (stolen)
165 {
Roman Khassrafe1593332015-06-02 13:19:01 +0200166 mVR204Coder.decode(mC, mU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200167 mP.invert();
168
169 unsigned syndrome = mBlockCoder.syndrome(mDP);
170
171 if (syndrome == 0)
172 {
Steve Glass6b20e1f2016-02-27 19:47:00 +1000173 unsigned char outmsg[28];
Roman Khassraf059bab92015-05-20 12:49:46 +0200174 unsigned char sbuf_len=224;
175 int i, j, c, pos=0;
176 for(i = 0; i < sbuf_len; i += 8) {
177 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
178 c |= (!!mU.bit(i + j)) << j;
179 }
180 outmsg[pos++] = c & 0xff;
181 }
182
183 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
184 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
Roman Khassraf059bab92015-05-20 12:49:46 +0200185 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
iZsh66987932015-08-16 14:42:18 +0200186 memcpy(header_plus_data, header, sizeof(gsmtap_hdr));
Roman Khassraf059bab92015-05-20 12:49:46 +0200187 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
iZsh66987932015-08-16 14:42:18 +0200188 ((gsmtap_hdr*)header_plus_data)->type = GSMTAP_TYPE_UM;
Roman Khassraf059bab92015-05-20 12:49:46 +0200189
190 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
191 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
192
193 message_port_pub(pmt::mp("msgs"), msg_out);
Roman Khassraf23c8d8a2015-08-11 11:52:15 +0200194
195 // if d_boundary_check is enabled, we set d_boundary_decode to true, when a
196 // "Connect" or "Connect Acknowledge" message is received, and
197 // we set d_boundary_decode back to false, when "Release" message is received
198 if (d_boundary_check)
199 {
200 // check if this is a call control message
201 if ((outmsg[3] & 0x0f) == 0x03)
202 {
203 // Connect specified in GSM 04.08, 9.3.5
204 if ((outmsg[4] & 0x3f) == 0x07)
205 {
206 d_boundary_decode = true;
207 }
208 // Connect Acknowledge specified in GSM 04.08, 9.3.6
209 else if ((outmsg[4] & 0x3f) == 0x0f)
210 {
211 d_boundary_decode = true;
212 }
213 // Release specified in GSM 04.08, 9.3.18
214 else if ((outmsg[4] & 0x3f) == 0x2d)
215 {
216 d_boundary_decode = false;
217 }
218 }
219 }
Roman Khassrafb8683672015-07-11 15:08:01 +0200220
221 // if we are in an AMR-mode and we receive a channel mode modify message,
222 // we set the mode according to the multirate configuration from the message
223 // see GSM 04.18, section 9.1.5 and 10.5.2.21aa
224 if (d_tch_mode != TCH_FS && d_tch_mode != TCH_EFR)
225 {
226 if (outmsg[3] == 0x06 && outmsg[4] == 0x10)
227 {
228 // Verify that multirate version 1 is set
229 if ((outmsg[11] >> 5) == 1)
230 {
231 // the set of active codecs, max 4 modes
232 // active_codec_set[0] corresponds to CODEC_MODE_1 with lowest bit rate
233 // active_codec_set[3] corresponds to CODEC_MODE_4 with highest bit rate
234 tch_mode active_codec_set[4];
235 uint8_t mode_count = 0;
236 for (i = 0; i<8; i++)
237 {
238 if (((outmsg[12] >> i) & 0x1) == 1 && mode_count < 4)
239 {
240 active_codec_set[mode_count++] = static_cast<tch_mode>(7-i);
241 }
242 }
243
244 // check Initial Codec Mode Indicator ICMI
245 // if ICMI == 1, then use the one defined in start mode field
246 // else use implicit rule defined in GSM 05.09, section 3.4.3
247 if (((outmsg[11] >> 3) & 0x1) == 1)
248 {
249 // from start field
250 setCodingMode(active_codec_set[ (outmsg[11] & 0x3) ]);
251 }
252 else
253 {
254 // implicit mode
255 // if the set contains only 1 codec, we use that one
256 // else if there are 2 or 3 codecs in the set, we use the one with lowest bitrate
257 if (mode_count >= 1 && mode_count <= 3)
258 {
259 setCodingMode(active_codec_set[0]);
260 }
261 // if there are 4 codecs in the set, we use the second lowest bitrate
262 else if (mode_count == 4)
263 {
264 setCodingMode(active_codec_set[1]);
265 }
266 }
267 }
268 }
269 }
Roman Khassraf059bab92015-05-20 12:49:46 +0200270 }
271 }
Roman Khassraf23c8d8a2015-08-11 11:52:15 +0200272
Roman Khassrafd36123d2015-08-11 12:44:56 +0200273 // if voice boundary_check is enabled and d_boundary_decode is false, we are done
Roman Khassraf23c8d8a2015-08-11 11:52:15 +0200274 if (d_boundary_check && !d_boundary_decode)
275 {
276 return;
277 }
Roman Khassraf059bab92015-05-20 12:49:46 +0200278
Piotr Krysik0a60e7a2016-06-29 22:22:27 +0200279 // Decode voice frames and send to the output
Roman Khassrafd38206c2015-06-07 16:26:29 +0200280 if (d_tch_mode == TCH_FS || d_tch_mode == TCH_EFR)
Roman Khassraf059bab92015-05-20 12:49:46 +0200281 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200282 mVR204Coder.decode(mClass1_c, mTCHU);
283 mClass2_c.sliced().copyToSegment(mTCHD, 182);
Roman Khassraf059bab92015-05-20 12:49:46 +0200284
Roman Khassrafd38206c2015-06-07 16:26:29 +0200285 // 3.1.2.1
286 // copy class 1 bits u[] to d[]
287 for (unsigned k = 0; k <= 90; k++) {
288 mTCHD[2*k] = mTCHU[k];
289 mTCHD[2*k+1] = mTCHU[184-k];
Roman Khassraf059bab92015-05-20 12:49:46 +0200290 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200291
292 // 3.1.2.1
293 // check parity of class 1A
294 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
295 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
Piotr Krysik5c9afd32016-08-18 18:57:02 +0200296 unsigned tail = mTCHU.peekField(185, 4);
297 bool good = (sentParity == calcParity) && (tail == 0);
Roman Khassrafd38206c2015-06-07 16:26:29 +0200298
Piotr Krysik694ed812016-10-02 18:56:04 +0200299
Roman Khassrafd38206c2015-06-07 16:26:29 +0200300 if (good)
Roman Khassraf059bab92015-05-20 12:49:46 +0200301 {
Piotr Krysik694ed812016-10-02 18:56:04 +0200302 uint8_t frameBuffer[33];
303 sbit_t bursts_s[116 * 8];
304 int n_errors, n_bits_total;
Roman Khassrafd38206c2015-06-07 16:26:29 +0200305 unsigned int mTCHFrameLength;
Roman Khassraf059bab92015-05-20 12:49:46 +0200306
Piotr Krysik694ed812016-10-02 18:56:04 +0200307 ubits2sbits(bursts_u, bursts_s, 116 * 8);
308
Roman Khassrafd38206c2015-06-07 16:26:29 +0200309 if (d_tch_mode == TCH_FS) // GSM-FR
310 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200311 mTCHFrameLength = 33;
Piotr Krysik694ed812016-10-02 18:56:04 +0200312 gsm0503_tch_fr_decode(frameBuffer, bursts_s, 1, 0, &n_errors, &n_bits_total);
313 std::cout << "Errors: " << n_errors << std::endl;
Roman Khassrafd38206c2015-06-07 16:26:29 +0200314 }
315 else if (d_tch_mode == TCH_EFR) // GSM-EFR
316 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200317 mTCHFrameLength = 32;
Piotr Krysik694ed812016-10-02 18:56:04 +0200318 gsm0503_tch_fr_decode(frameBuffer, bursts_s, 1, 1, &n_errors, &n_bits_total);
Roman Khassrafd38206c2015-06-07 16:26:29 +0200319 }
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200320 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer,mTCHFrameLength)));
Roman Khassraf059bab92015-05-20 12:49:46 +0200321 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200322 }
323 else
324 {
325 // Handle inband bits, see 3.9.4.1
326 // OpenBTS source takes last 8 bits as inband bits for some reason. This may be either a
327 // divergence between their implementation and GSM specification, which works because
328 // both their encoder and decoder do it same way, or they handle the issue at some other place
329 // SoftVector cMinus8 = mC.segment(0, mC.size() - 8);
330 SoftVector cMinus8 = mC.segment(8, mC.size());
331 cMinus8.copyUnPunctured(mTCHUC, mPuncture, mPunctureLth);
332
333 // 3.9.4.4
334 // decode from uc[] to u[]
335 mViterbi->decode(mTCHUC, mTCHU);
336
337 // 3.9.4.3 -- class 1a bits in u[] to d[]
338 for (unsigned k=0; k < mClass1ALth; k++) {
339 mTCHD[k] = mTCHU[k];
340 }
341
342 // 3.9.4.3 -- class 1b bits in u[] to d[]
343 for (unsigned k=0; k < mClass1BLth; k++) {
344 mTCHD[k+mClass1ALth] = mTCHU[k+mClass1ALth+6];
345 }
346
347 // Check parity
348 unsigned sentParity = (~mTCHU.peekField(mClass1ALth,6)) & 0x3f;
349 BitVector class1A = mTCHU.segment(0, mClass1ALth);
350 unsigned calcParity = class1A.parity(mTCHParity) & 0x3f;
351
352 bool good = (sentParity == calcParity);
353
354 if (good)
355 {
356 unsigned char frameBuffer[mAMRFrameLth];
357 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
358 BitVector amrFrame(mKd + 8);
359 BitVector payload = amrFrame.tail(8);
360
361 // write frame header
362 amrFrame.fillField(0, mAMRFrameHeader, 8);
363
364 // We don't unmap here, but copy the decoded bits directly
365 // Decoder already delivers correct bit order
366 // mTCHD.unmap(mAMRBitOrder, payload.size(), payload);
367 mTCHD.copyTo(payload);
368 amrFrame.pack(frameBuffer);
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200369 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer,mAMRFrameLth)));
Roman Khassrafd38206c2015-06-07 16:26:29 +0200370 }
371 }
372 }
373 }
374
375 void tch_f_decoder_impl::setCodingMode(tch_mode mode)
376 {
Roman Khassrafb8683672015-07-11 15:08:01 +0200377 if (mode != TCH_FS && d_tch_mode != TCH_EFR)
Roman Khassrafd38206c2015-06-07 16:26:29 +0200378 {
Roman Khassrafb8683672015-07-11 15:08:01 +0200379 d_tch_mode = mode;
Roman Khassrafd38206c2015-06-07 16:26:29 +0200380 mKd = GSM::gAMRKd[d_tch_mode];
381 mTCHD.resize(mKd);
382 mTCHU.resize(mKd+6);
383 mTCHParity = Parity(0x06f,6, GSM::gAMRClass1ALth[d_tch_mode]);
384 mAMRBitOrder = GSM::gAMRBitOrder[d_tch_mode];
385 mClass1ALth = GSM::gAMRClass1ALth[d_tch_mode];
386 mClass1BLth = GSM::gAMRKd[d_tch_mode] - GSM::gAMRClass1ALth[d_tch_mode];
387 mTCHUC.resize(GSM::gAMRTCHUCLth[d_tch_mode]);
388 mPuncture = GSM::gAMRPuncture[d_tch_mode];
389 mPunctureLth = GSM::gAMRPunctureLth[d_tch_mode];
390 mClass1A_d.dup(mTCHD.head(mClass1ALth));
391
392 switch (d_tch_mode)
393 {
394 case TCH_AFS12_2:
395 mViterbi = new ViterbiTCH_AFS12_2();
396 mAMRFrameLth = 32;
397 mAMRFrameHeader = 0x3c;
398 break;
399 case TCH_AFS10_2:
400 mViterbi = new ViterbiTCH_AFS10_2();
401 mAMRFrameLth = 27;
402 mAMRFrameHeader = 0x3c;
403 break;
404 case TCH_AFS7_95:
405 mViterbi = new ViterbiTCH_AFS7_95();
406 mAMRFrameLth = 21;
407 mAMRFrameHeader = 0x3c;
408 break;
409 case TCH_AFS7_4:
410 mViterbi = new ViterbiTCH_AFS7_4();
411 mAMRFrameLth = 20;
412 mAMRFrameHeader = 0x3c;
413 break;
414 case TCH_AFS6_7:
415 mViterbi = new ViterbiTCH_AFS6_7();
416 mAMRFrameLth = 18;
417 mAMRFrameHeader = 0x3c;
418 break;
419 case TCH_AFS5_9:
420 mViterbi = new ViterbiTCH_AFS5_9();
421 mAMRFrameLth = 16;
422 mAMRFrameHeader = 0x14;
423 break;
424 case TCH_AFS5_15:
425 mViterbi = new ViterbiTCH_AFS5_15();
426 mAMRFrameLth = 14;
427 mAMRFrameHeader = 0x3c;
428 break;
429 case TCH_AFS4_75:
430 mViterbi = new ViterbiTCH_AFS4_75();
431 mAMRFrameLth = 13;
432 mAMRFrameHeader = 0x3c;
433 break;
434 default:
435 mViterbi = new ViterbiTCH_AFS12_2();
436 mAMRFrameLth = 32;
437 mAMRFrameHeader = 0x3c;
438 break;
Roman Khassraf059bab92015-05-20 12:49:46 +0200439 }
440 }
441 }
442 } /* namespace gsm */
443} /* namespace gr */
444