blob: a3b09fb285eb7c75855834e839d03031d47d139d [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
38 tch_f_decoder::make(tch_mode mode, const std::string &file)
39 {
40 return gnuradio::get_initial_sptr
41 (new tch_f_decoder_impl(mode, file));
42 }
43
44 /*
45 * Constructor
46 */
47 tch_f_decoder_impl::tch_f_decoder_impl(tch_mode mode, const std::string &file)
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 Khassrafe1593332015-06-02 13:19:01 +020052 d_collected_bursts_num(0),
53 mBlockCoder(0x10004820009ULL, 40, 224),
54 mU(228),
Roman Khassrafd38206c2015-06-07 16:26:29 +020055 mP(mU.segment(184,40)),
56 mD(mU.head(184)),
57 mDP(mU.head(224)),
Roman Khassrafe1593332015-06-02 13:19:01 +020058 mC(CONV_SIZE),
59 mClass1_c(mC.head(378)),
60 mClass2_c(mC.segment(378, 78)),
61 mTCHU(189),
62 mTCHD(260),
Roman Khassrafd38206c2015-06-07 16:26:29 +020063 mClass1A_d(mTCHD.head(50)),
64 mTCHParity(0x0b, 3, 50)
Roman Khassraf059bab92015-05-20 12:49:46 +020065 {
66 d_speech_file = fopen( file.c_str(), "wb" );
67 if (d_speech_file == NULL)
68 {
69 throw std::runtime_error("TCH/F Decoder: can't open file\n");
70 }
Roman Khassrafb8683672015-07-11 15:08:01 +020071
ptrkrysikd1312132015-07-06 18:48:13 +020072 const unsigned char amr_nb_magic[6] = { 0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a };
Roman Khassraf059bab92015-05-20 12:49:46 +020073
Roman Khassraffa7058b2015-06-02 09:21:11 +020074 if (d_tch_mode != TCH_FS)
Roman Khassraf091a80f2015-05-22 10:39:16 +020075 {
76 fwrite(amr_nb_magic, 1, 6, d_speech_file);
77 }
78
Roman Khassraf059bab92015-05-20 12:49:46 +020079 int j, k, B;
80 for (k = 0; k < CONV_SIZE; k++)
81 {
82 B = k % 8;
83 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
84 interleave_trans[k] = B * 114 + j;
85 }
86
87 //setup input/output ports
88 message_port_register_in(pmt::mp("bursts"));
89 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
90 message_port_register_out(pmt::mp("msgs"));
Roman Khassrafd38206c2015-06-07 16:26:29 +020091
92 setCodingMode(mode);
Roman Khassraf059bab92015-05-20 12:49:46 +020093 }
94
95 tch_f_decoder_impl::~tch_f_decoder_impl()
96 {
97 }
98
99 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
100 {
101 d_bursts[d_collected_bursts_num] = msg;
102 d_collected_bursts_num++;
103
104 bool stolen = false;
105
106 if (d_collected_bursts_num == 8)
107 {
Roman Khassraf059bab92015-05-20 12:49:46 +0200108 d_collected_bursts_num = 0;
109
110 // reorganize data
111 for (int ii = 0; ii < 8; ii++)
112 {
113 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
114 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
115
116 for (int jj = 0; jj < 57; jj++)
117 {
118 iBLOCK[ii*114+jj] = burst_bits[jj + 3];
119 iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
120 }
121
122 if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1))
123 {
124 stolen = true;
125 }
126 }
127
128 // deinterleave
129 for (int k = 0; k < CONV_SIZE; k++)
130 {
131 mC[k] = iBLOCK[interleave_trans[k]];
132 }
133
134 // Decode stolen frames as FACCH/F
135 if (stolen)
136 {
Roman Khassrafe1593332015-06-02 13:19:01 +0200137 mVR204Coder.decode(mC, mU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200138 mP.invert();
139
140 unsigned syndrome = mBlockCoder.syndrome(mDP);
141
142 if (syndrome == 0)
143 {
144 unsigned char outmsg[27];
145 unsigned char sbuf_len=224;
146 int i, j, c, pos=0;
147 for(i = 0; i < sbuf_len; i += 8) {
148 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
149 c |= (!!mU.bit(i + j)) << j;
150 }
151 outmsg[pos++] = c & 0xff;
152 }
153
154 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
155 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
156 header->type = GSMTAP_TYPE_UM;
157 int8_t * header_content = (int8_t *)header;
158 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
159 memcpy(header_plus_data, header_content, sizeof(gsmtap_hdr));
160 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
161
162 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
163 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
164
165 message_port_pub(pmt::mp("msgs"), msg_out);
Roman Khassrafb8683672015-07-11 15:08:01 +0200166
167 // if we are in an AMR-mode and we receive a channel mode modify message,
168 // we set the mode according to the multirate configuration from the message
169 // see GSM 04.18, section 9.1.5 and 10.5.2.21aa
170 if (d_tch_mode != TCH_FS && d_tch_mode != TCH_EFR)
171 {
172 if (outmsg[3] == 0x06 && outmsg[4] == 0x10)
173 {
174 // Verify that multirate version 1 is set
175 if ((outmsg[11] >> 5) == 1)
176 {
177 // the set of active codecs, max 4 modes
178 // active_codec_set[0] corresponds to CODEC_MODE_1 with lowest bit rate
179 // active_codec_set[3] corresponds to CODEC_MODE_4 with highest bit rate
180 tch_mode active_codec_set[4];
181 uint8_t mode_count = 0;
182 for (i = 0; i<8; i++)
183 {
184 if (((outmsg[12] >> i) & 0x1) == 1 && mode_count < 4)
185 {
186 active_codec_set[mode_count++] = static_cast<tch_mode>(7-i);
187 }
188 }
189
190 // check Initial Codec Mode Indicator ICMI
191 // if ICMI == 1, then use the one defined in start mode field
192 // else use implicit rule defined in GSM 05.09, section 3.4.3
193 if (((outmsg[11] >> 3) & 0x1) == 1)
194 {
195 // from start field
196 setCodingMode(active_codec_set[ (outmsg[11] & 0x3) ]);
197 }
198 else
199 {
200 // implicit mode
201 // if the set contains only 1 codec, we use that one
202 // else if there are 2 or 3 codecs in the set, we use the one with lowest bitrate
203 if (mode_count >= 1 && mode_count <= 3)
204 {
205 setCodingMode(active_codec_set[0]);
206 }
207 // if there are 4 codecs in the set, we use the second lowest bitrate
208 else if (mode_count == 4)
209 {
210 setCodingMode(active_codec_set[1]);
211 }
212 }
213 }
214 }
215 }
Roman Khassraf059bab92015-05-20 12:49:46 +0200216 }
217 }
218
Roman Khassrafd38206c2015-06-07 16:26:29 +0200219 // Decode voice frames and write to file
220 if (d_tch_mode == TCH_FS || d_tch_mode == TCH_EFR)
Roman Khassraf059bab92015-05-20 12:49:46 +0200221 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200222 mVR204Coder.decode(mClass1_c, mTCHU);
223 mClass2_c.sliced().copyToSegment(mTCHD, 182);
Roman Khassraf059bab92015-05-20 12:49:46 +0200224
Roman Khassrafd38206c2015-06-07 16:26:29 +0200225 // 3.1.2.1
226 // copy class 1 bits u[] to d[]
227 for (unsigned k = 0; k <= 90; k++) {
228 mTCHD[2*k] = mTCHU[k];
229 mTCHD[2*k+1] = mTCHU[184-k];
Roman Khassraf059bab92015-05-20 12:49:46 +0200230 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200231
232 // 3.1.2.1
233 // check parity of class 1A
234 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
235 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
236
237 bool good = (sentParity == calcParity);
238
239 if (good)
Roman Khassraf059bab92015-05-20 12:49:46 +0200240 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200241 unsigned char frameBuffer[33];
242 unsigned int mTCHFrameLength;
Roman Khassraf059bab92015-05-20 12:49:46 +0200243
Roman Khassrafd38206c2015-06-07 16:26:29 +0200244 if (d_tch_mode == TCH_FS) // GSM-FR
245 {
246 unsigned char mFrameHeader = 0x0d;
247 mTCHFrameLength = 33;
Roman Khassraf059bab92015-05-20 12:49:46 +0200248
Roman Khassrafd38206c2015-06-07 16:26:29 +0200249 // Undo Um's importance-sorted bit ordering.
250 // See GSM 05.03 3.1 and Table 2.
251 BitVector frFrame(260 + 4); // FR has a frameheader of 4 bits only
252 BitVector payload = frFrame.tail(4);
Roman Khassraf059bab92015-05-20 12:49:46 +0200253
Roman Khassrafd38206c2015-06-07 16:26:29 +0200254 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
255 frFrame.pack(frameBuffer);
Roman Khassraf059bab92015-05-20 12:49:46 +0200256
Roman Khassrafd38206c2015-06-07 16:26:29 +0200257 }
258 else if (d_tch_mode == TCH_EFR) // GSM-EFR
259 {
260 unsigned char mFrameHeader = 0x3c;
Roman Khassraf059bab92015-05-20 12:49:46 +0200261
Roman Khassrafd38206c2015-06-07 16:26:29 +0200262 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
263 BitVector amrFrame(244 + 8); // Same output length as AMR 12.2
264 BitVector payload = amrFrame.tail(8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200265
Roman Khassrafd38206c2015-06-07 16:26:29 +0200266 BitVector TCHW(260), EFRBits(244);
Roman Khassraf059bab92015-05-20 12:49:46 +0200267
Roman Khassrafd38206c2015-06-07 16:26:29 +0200268 // write frame header
269 amrFrame.fillField(0, mFrameHeader, 8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200270
Roman Khassrafd38206c2015-06-07 16:26:29 +0200271 // Undo Um's EFR bit ordering.
272 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
Roman Khassraf059bab92015-05-20 12:49:46 +0200273
Roman Khassrafd38206c2015-06-07 16:26:29 +0200274 // Remove repeating bits and CRC to get raw EFR frame (244 bits)
275 for (unsigned k=0; k<71; k++)
276 EFRBits[k] = TCHW[k] & 1;
277
278 for (unsigned k=73; k<123; k++)
279 EFRBits[k-2] = TCHW[k] & 1;
280
281 for (unsigned k=125; k<178; k++)
282 EFRBits[k-4] = TCHW[k] & 1;
283
284 for (unsigned k=180; k<230; k++)
285 EFRBits[k-6] = TCHW[k] & 1;
286
287 for (unsigned k=232; k<252; k++)
288 EFRBits[k-8] = TCHW[k] & 1;
289
290 // Map bits as AMR 12.2k
Roman Khassraf8851d662015-06-07 16:42:37 +0200291 EFRBits.map(GSM::gAMRBitOrderTCH_AFS12_2, 244, payload);
Roman Khassrafd38206c2015-06-07 16:26:29 +0200292
293 // Put the whole frame (hdr + payload)
294 mTCHFrameLength = 32;
295 amrFrame.pack(frameBuffer);
296
297 }
298 fwrite(frameBuffer, 1 , mTCHFrameLength, d_speech_file);
Roman Khassraf059bab92015-05-20 12:49:46 +0200299 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200300 }
301 else
302 {
303 // Handle inband bits, see 3.9.4.1
304 // OpenBTS source takes last 8 bits as inband bits for some reason. This may be either a
305 // divergence between their implementation and GSM specification, which works because
306 // both their encoder and decoder do it same way, or they handle the issue at some other place
307 // SoftVector cMinus8 = mC.segment(0, mC.size() - 8);
308 SoftVector cMinus8 = mC.segment(8, mC.size());
309 cMinus8.copyUnPunctured(mTCHUC, mPuncture, mPunctureLth);
310
311 // 3.9.4.4
312 // decode from uc[] to u[]
313 mViterbi->decode(mTCHUC, mTCHU);
314
315 // 3.9.4.3 -- class 1a bits in u[] to d[]
316 for (unsigned k=0; k < mClass1ALth; k++) {
317 mTCHD[k] = mTCHU[k];
318 }
319
320 // 3.9.4.3 -- class 1b bits in u[] to d[]
321 for (unsigned k=0; k < mClass1BLth; k++) {
322 mTCHD[k+mClass1ALth] = mTCHU[k+mClass1ALth+6];
323 }
324
325 // Check parity
326 unsigned sentParity = (~mTCHU.peekField(mClass1ALth,6)) & 0x3f;
327 BitVector class1A = mTCHU.segment(0, mClass1ALth);
328 unsigned calcParity = class1A.parity(mTCHParity) & 0x3f;
329
330 bool good = (sentParity == calcParity);
331
332 if (good)
333 {
334 unsigned char frameBuffer[mAMRFrameLth];
335 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
336 BitVector amrFrame(mKd + 8);
337 BitVector payload = amrFrame.tail(8);
338
339 // write frame header
340 amrFrame.fillField(0, mAMRFrameHeader, 8);
341
342 // We don't unmap here, but copy the decoded bits directly
343 // Decoder already delivers correct bit order
344 // mTCHD.unmap(mAMRBitOrder, payload.size(), payload);
345 mTCHD.copyTo(payload);
346 amrFrame.pack(frameBuffer);
347 fwrite(frameBuffer, 1 , mAMRFrameLth, d_speech_file);
348 }
349 }
350 }
351 }
352
353 void tch_f_decoder_impl::setCodingMode(tch_mode mode)
354 {
Roman Khassrafb8683672015-07-11 15:08:01 +0200355 if (mode != TCH_FS && d_tch_mode != TCH_EFR)
Roman Khassrafd38206c2015-06-07 16:26:29 +0200356 {
Roman Khassrafb8683672015-07-11 15:08:01 +0200357 d_tch_mode = mode;
Roman Khassrafd38206c2015-06-07 16:26:29 +0200358 mKd = GSM::gAMRKd[d_tch_mode];
359 mTCHD.resize(mKd);
360 mTCHU.resize(mKd+6);
361 mTCHParity = Parity(0x06f,6, GSM::gAMRClass1ALth[d_tch_mode]);
362 mAMRBitOrder = GSM::gAMRBitOrder[d_tch_mode];
363 mClass1ALth = GSM::gAMRClass1ALth[d_tch_mode];
364 mClass1BLth = GSM::gAMRKd[d_tch_mode] - GSM::gAMRClass1ALth[d_tch_mode];
365 mTCHUC.resize(GSM::gAMRTCHUCLth[d_tch_mode]);
366 mPuncture = GSM::gAMRPuncture[d_tch_mode];
367 mPunctureLth = GSM::gAMRPunctureLth[d_tch_mode];
368 mClass1A_d.dup(mTCHD.head(mClass1ALth));
369
370 switch (d_tch_mode)
371 {
372 case TCH_AFS12_2:
373 mViterbi = new ViterbiTCH_AFS12_2();
374 mAMRFrameLth = 32;
375 mAMRFrameHeader = 0x3c;
376 break;
377 case TCH_AFS10_2:
378 mViterbi = new ViterbiTCH_AFS10_2();
379 mAMRFrameLth = 27;
380 mAMRFrameHeader = 0x3c;
381 break;
382 case TCH_AFS7_95:
383 mViterbi = new ViterbiTCH_AFS7_95();
384 mAMRFrameLth = 21;
385 mAMRFrameHeader = 0x3c;
386 break;
387 case TCH_AFS7_4:
388 mViterbi = new ViterbiTCH_AFS7_4();
389 mAMRFrameLth = 20;
390 mAMRFrameHeader = 0x3c;
391 break;
392 case TCH_AFS6_7:
393 mViterbi = new ViterbiTCH_AFS6_7();
394 mAMRFrameLth = 18;
395 mAMRFrameHeader = 0x3c;
396 break;
397 case TCH_AFS5_9:
398 mViterbi = new ViterbiTCH_AFS5_9();
399 mAMRFrameLth = 16;
400 mAMRFrameHeader = 0x14;
401 break;
402 case TCH_AFS5_15:
403 mViterbi = new ViterbiTCH_AFS5_15();
404 mAMRFrameLth = 14;
405 mAMRFrameHeader = 0x3c;
406 break;
407 case TCH_AFS4_75:
408 mViterbi = new ViterbiTCH_AFS4_75();
409 mAMRFrameLth = 13;
410 mAMRFrameHeader = 0x3c;
411 break;
412 default:
413 mViterbi = new ViterbiTCH_AFS12_2();
414 mAMRFrameLth = 32;
415 mAMRFrameHeader = 0x3c;
416 break;
Roman Khassraf059bab92015-05-20 12:49:46 +0200417 }
418 }
419 }
420 } /* namespace gsm */
421} /* namespace gr */
422