blob: b9aa057b8d6bb751af4292de48c45df702888863 [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
32#define DATA_BYTES 23
33
34namespace gr {
35 namespace gsm {
36
37 tch_f_decoder::sptr
Roman Khassraf23c8d8a2015-08-11 11:52:15 +020038 tch_f_decoder::make(tch_mode mode, const std::string &file, bool boundary_check)
Roman Khassraf059bab92015-05-20 12:49:46 +020039 {
40 return gnuradio::get_initial_sptr
Roman Khassraf23c8d8a2015-08-11 11:52:15 +020041 (new tch_f_decoder_impl(mode, file, boundary_check));
Roman Khassraf059bab92015-05-20 12:49:46 +020042 }
43
44 /*
45 * Constructor
46 */
Roman Khassraf23c8d8a2015-08-11 11:52:15 +020047 tch_f_decoder_impl::tch_f_decoder_impl(tch_mode mode, const std::string &file, bool boundary_check)
Roman Khassraf059bab92015-05-20 12:49:46 +020048 : 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 Khassrafe1593332015-06-02 13:19:01 +020052 d_collected_bursts_num(0),
Roman Khassraf23c8d8a2015-08-11 11:52:15 +020053 d_boundary_check(boundary_check),
54 d_boundary_decode(!boundary_check),
Piotr Krysik2b97cb12016-06-29 15:00:07 +020055 d_header_sent(false),
Roman Khassrafe1593332015-06-02 13:19:01 +020056 mBlockCoder(0x10004820009ULL, 40, 224),
57 mU(228),
Roman Khassrafd38206c2015-06-07 16:26:29 +020058 mP(mU.segment(184,40)),
59 mD(mU.head(184)),
60 mDP(mU.head(224)),
Roman Khassrafe1593332015-06-02 13:19:01 +020061 mC(CONV_SIZE),
62 mClass1_c(mC.head(378)),
63 mClass2_c(mC.segment(378, 78)),
64 mTCHU(189),
65 mTCHD(260),
Roman Khassrafd38206c2015-06-07 16:26:29 +020066 mClass1A_d(mTCHD.head(50)),
67 mTCHParity(0x0b, 3, 50)
Roman Khassraf059bab92015-05-20 12:49:46 +020068 {
Piotr Krysik2b97cb12016-06-29 15:00:07 +020069 //setup input/output ports
70 message_port_register_in(pmt::mp("bursts"));
71 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
72 message_port_register_out(pmt::mp("msgs"));
73 message_port_register_out(pmt::mp("voice"));
Roman Khassraf091a80f2015-05-22 10:39:16 +020074
Roman Khassraf059bab92015-05-20 12:49:46 +020075 int j, k, B;
76 for (k = 0; k < CONV_SIZE; k++)
77 {
78 B = k % 8;
79 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
80 interleave_trans[k] = B * 114 + j;
81 }
82
Roman Khassrafd38206c2015-06-07 16:26:29 +020083 setCodingMode(mode);
Roman Khassraf059bab92015-05-20 12:49:46 +020084 }
85
86 tch_f_decoder_impl::~tch_f_decoder_impl()
87 {
88 }
89
90 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
91 {
Piotr Krysik2b97cb12016-06-29 15:00:07 +020092 if(!d_header_sent)
93 {
94 if (d_tch_mode != TCH_FS)
95 {
96 const unsigned char amr_nb_magic[7] = "#!AMR\n";
97 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(amr_nb_magic,6)));
98 }
99 d_header_sent = true;
100 }
101
102
Roman Khassraf059bab92015-05-20 12:49:46 +0200103 d_bursts[d_collected_bursts_num] = msg;
104 d_collected_bursts_num++;
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200105
Roman Khassraf059bab92015-05-20 12:49:46 +0200106 bool stolen = false;
107
108 if (d_collected_bursts_num == 8)
109 {
Roman Khassraf059bab92015-05-20 12:49:46 +0200110 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 Khassrafe1593332015-06-02 13:19:01 +0200139 mVR204Coder.decode(mC, mU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200140 mP.invert();
141
142 unsigned syndrome = mBlockCoder.syndrome(mDP);
143
144 if (syndrome == 0)
145 {
Steve Glass6b20e1f2016-02-27 19:47:00 +1000146 unsigned char outmsg[28];
Roman Khassraf059bab92015-05-20 12:49:46 +0200147 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 Khassraf059bab92015-05-20 12:49:46 +0200158 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
iZsh66987932015-08-16 14:42:18 +0200159 memcpy(header_plus_data, header, sizeof(gsmtap_hdr));
Roman Khassraf059bab92015-05-20 12:49:46 +0200160 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
iZsh66987932015-08-16 14:42:18 +0200161 ((gsmtap_hdr*)header_plus_data)->type = GSMTAP_TYPE_UM;
Roman Khassraf059bab92015-05-20 12:49:46 +0200162
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 Khassraf23c8d8a2015-08-11 11:52:15 +0200167
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 Khassrafb8683672015-07-11 15:08:01 +0200193
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 Khassraf059bab92015-05-20 12:49:46 +0200243 }
244 }
Roman Khassraf23c8d8a2015-08-11 11:52:15 +0200245
Roman Khassrafd36123d2015-08-11 12:44:56 +0200246 // if voice boundary_check is enabled and d_boundary_decode is false, we are done
Roman Khassraf23c8d8a2015-08-11 11:52:15 +0200247 if (d_boundary_check && !d_boundary_decode)
248 {
249 return;
250 }
Roman Khassraf059bab92015-05-20 12:49:46 +0200251
Roman Khassrafd38206c2015-06-07 16:26:29 +0200252 // Decode voice frames and write to file
253 if (d_tch_mode == TCH_FS || d_tch_mode == TCH_EFR)
Roman Khassraf059bab92015-05-20 12:49:46 +0200254 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200255 mVR204Coder.decode(mClass1_c, mTCHU);
256 mClass2_c.sliced().copyToSegment(mTCHD, 182);
Roman Khassraf059bab92015-05-20 12:49:46 +0200257
Roman Khassrafd38206c2015-06-07 16:26:29 +0200258 // 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 Khassraf059bab92015-05-20 12:49:46 +0200263 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200264
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 Khassraf059bab92015-05-20 12:49:46 +0200273 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200274 unsigned char frameBuffer[33];
275 unsigned int mTCHFrameLength;
Roman Khassraf059bab92015-05-20 12:49:46 +0200276
Roman Khassrafd38206c2015-06-07 16:26:29 +0200277 if (d_tch_mode == TCH_FS) // GSM-FR
278 {
279 unsigned char mFrameHeader = 0x0d;
280 mTCHFrameLength = 33;
Roman Khassraf059bab92015-05-20 12:49:46 +0200281
Roman Khassrafd38206c2015-06-07 16:26:29 +0200282 // 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 Khassrafd36123d2015-08-11 12:44:56 +0200285 frFrame.fillField(0, mFrameHeader, 4);
Roman Khassrafd38206c2015-06-07 16:26:29 +0200286 BitVector payload = frFrame.tail(4);
Roman Khassraf059bab92015-05-20 12:49:46 +0200287
Roman Khassrafd38206c2015-06-07 16:26:29 +0200288 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
289 frFrame.pack(frameBuffer);
Roman Khassraf059bab92015-05-20 12:49:46 +0200290
Roman Khassrafd38206c2015-06-07 16:26:29 +0200291 }
292 else if (d_tch_mode == TCH_EFR) // GSM-EFR
293 {
294 unsigned char mFrameHeader = 0x3c;
Roman Khassraf059bab92015-05-20 12:49:46 +0200295
Roman Khassrafd38206c2015-06-07 16:26:29 +0200296 // 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 Khassraf059bab92015-05-20 12:49:46 +0200299
Roman Khassrafd38206c2015-06-07 16:26:29 +0200300 BitVector TCHW(260), EFRBits(244);
Roman Khassraf059bab92015-05-20 12:49:46 +0200301
Roman Khassrafd38206c2015-06-07 16:26:29 +0200302 // write frame header
303 amrFrame.fillField(0, mFrameHeader, 8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200304
Roman Khassrafd38206c2015-06-07 16:26:29 +0200305 // Undo Um's EFR bit ordering.
306 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
Roman Khassraf059bab92015-05-20 12:49:46 +0200307
Roman Khassrafd38206c2015-06-07 16:26:29 +0200308 // 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 Khassraf8851d662015-06-07 16:42:37 +0200325 EFRBits.map(GSM::gAMRBitOrderTCH_AFS12_2, 244, payload);
Roman Khassrafd38206c2015-06-07 16:26:29 +0200326
327 // Put the whole frame (hdr + payload)
328 mTCHFrameLength = 32;
329 amrFrame.pack(frameBuffer);
330
331 }
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200332 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer,mTCHFrameLength)));
Roman Khassraf059bab92015-05-20 12:49:46 +0200333 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200334 }
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);
Piotr Krysik2b97cb12016-06-29 15:00:07 +0200381 message_port_pub(pmt::mp("voice"), pmt::cons(pmt::PMT_NIL, pmt::make_blob(frameBuffer,mAMRFrameLth)));
Roman Khassrafd38206c2015-06-07 16:26:29 +0200382 }
383 }
384 }
385 }
386
387 void tch_f_decoder_impl::setCodingMode(tch_mode mode)
388 {
Roman Khassrafb8683672015-07-11 15:08:01 +0200389 if (mode != TCH_FS && d_tch_mode != TCH_EFR)
Roman Khassrafd38206c2015-06-07 16:26:29 +0200390 {
Roman Khassrafb8683672015-07-11 15:08:01 +0200391 d_tch_mode = mode;
Roman Khassrafd38206c2015-06-07 16:26:29 +0200392 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 Khassraf059bab92015-05-20 12:49:46 +0200451 }
452 }
453 }
454 } /* namespace gsm */
455} /* namespace gr */
456