blob: 3ec720535e003cb2ce130e24eeedbcb2a57e7ab9 [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 }
ptrkrysikd1312132015-07-06 18:48:13 +020071
72 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);
166 }
167 }
168
Roman Khassrafd38206c2015-06-07 16:26:29 +0200169 // Decode voice frames and write to file
170 if (d_tch_mode == TCH_FS || d_tch_mode == TCH_EFR)
Roman Khassraf059bab92015-05-20 12:49:46 +0200171 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200172 mVR204Coder.decode(mClass1_c, mTCHU);
173 mClass2_c.sliced().copyToSegment(mTCHD, 182);
Roman Khassraf059bab92015-05-20 12:49:46 +0200174
Roman Khassrafd38206c2015-06-07 16:26:29 +0200175 // 3.1.2.1
176 // copy class 1 bits u[] to d[]
177 for (unsigned k = 0; k <= 90; k++) {
178 mTCHD[2*k] = mTCHU[k];
179 mTCHD[2*k+1] = mTCHU[184-k];
Roman Khassraf059bab92015-05-20 12:49:46 +0200180 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200181
182 // 3.1.2.1
183 // check parity of class 1A
184 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
185 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
186
187 bool good = (sentParity == calcParity);
188
189 if (good)
Roman Khassraf059bab92015-05-20 12:49:46 +0200190 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200191 unsigned char frameBuffer[33];
192 unsigned int mTCHFrameLength;
Roman Khassraf059bab92015-05-20 12:49:46 +0200193
Roman Khassrafd38206c2015-06-07 16:26:29 +0200194 if (d_tch_mode == TCH_FS) // GSM-FR
195 {
196 unsigned char mFrameHeader = 0x0d;
197 mTCHFrameLength = 33;
Roman Khassraf059bab92015-05-20 12:49:46 +0200198
Roman Khassrafd38206c2015-06-07 16:26:29 +0200199 // Undo Um's importance-sorted bit ordering.
200 // See GSM 05.03 3.1 and Table 2.
201 BitVector frFrame(260 + 4); // FR has a frameheader of 4 bits only
202 BitVector payload = frFrame.tail(4);
Roman Khassraf059bab92015-05-20 12:49:46 +0200203
Roman Khassrafd38206c2015-06-07 16:26:29 +0200204 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
205 frFrame.pack(frameBuffer);
Roman Khassraf059bab92015-05-20 12:49:46 +0200206
Roman Khassrafd38206c2015-06-07 16:26:29 +0200207 }
208 else if (d_tch_mode == TCH_EFR) // GSM-EFR
209 {
210 unsigned char mFrameHeader = 0x3c;
Roman Khassraf059bab92015-05-20 12:49:46 +0200211
Roman Khassrafd38206c2015-06-07 16:26:29 +0200212 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
213 BitVector amrFrame(244 + 8); // Same output length as AMR 12.2
214 BitVector payload = amrFrame.tail(8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200215
Roman Khassrafd38206c2015-06-07 16:26:29 +0200216 BitVector TCHW(260), EFRBits(244);
Roman Khassraf059bab92015-05-20 12:49:46 +0200217
Roman Khassrafd38206c2015-06-07 16:26:29 +0200218 // write frame header
219 amrFrame.fillField(0, mFrameHeader, 8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200220
Roman Khassrafd38206c2015-06-07 16:26:29 +0200221 // Undo Um's EFR bit ordering.
222 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
Roman Khassraf059bab92015-05-20 12:49:46 +0200223
Roman Khassrafd38206c2015-06-07 16:26:29 +0200224 // Remove repeating bits and CRC to get raw EFR frame (244 bits)
225 for (unsigned k=0; k<71; k++)
226 EFRBits[k] = TCHW[k] & 1;
227
228 for (unsigned k=73; k<123; k++)
229 EFRBits[k-2] = TCHW[k] & 1;
230
231 for (unsigned k=125; k<178; k++)
232 EFRBits[k-4] = TCHW[k] & 1;
233
234 for (unsigned k=180; k<230; k++)
235 EFRBits[k-6] = TCHW[k] & 1;
236
237 for (unsigned k=232; k<252; k++)
238 EFRBits[k-8] = TCHW[k] & 1;
239
240 // Map bits as AMR 12.2k
Roman Khassraf8851d662015-06-07 16:42:37 +0200241 EFRBits.map(GSM::gAMRBitOrderTCH_AFS12_2, 244, payload);
Roman Khassrafd38206c2015-06-07 16:26:29 +0200242
243 // Put the whole frame (hdr + payload)
244 mTCHFrameLength = 32;
245 amrFrame.pack(frameBuffer);
246
247 }
248 fwrite(frameBuffer, 1 , mTCHFrameLength, d_speech_file);
Roman Khassraf059bab92015-05-20 12:49:46 +0200249 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200250 }
251 else
252 {
253 // Handle inband bits, see 3.9.4.1
254 // OpenBTS source takes last 8 bits as inband bits for some reason. This may be either a
255 // divergence between their implementation and GSM specification, which works because
256 // both their encoder and decoder do it same way, or they handle the issue at some other place
257 // SoftVector cMinus8 = mC.segment(0, mC.size() - 8);
258 SoftVector cMinus8 = mC.segment(8, mC.size());
259 cMinus8.copyUnPunctured(mTCHUC, mPuncture, mPunctureLth);
260
261 // 3.9.4.4
262 // decode from uc[] to u[]
263 mViterbi->decode(mTCHUC, mTCHU);
264
265 // 3.9.4.3 -- class 1a bits in u[] to d[]
266 for (unsigned k=0; k < mClass1ALth; k++) {
267 mTCHD[k] = mTCHU[k];
268 }
269
270 // 3.9.4.3 -- class 1b bits in u[] to d[]
271 for (unsigned k=0; k < mClass1BLth; k++) {
272 mTCHD[k+mClass1ALth] = mTCHU[k+mClass1ALth+6];
273 }
274
275 // Check parity
276 unsigned sentParity = (~mTCHU.peekField(mClass1ALth,6)) & 0x3f;
277 BitVector class1A = mTCHU.segment(0, mClass1ALth);
278 unsigned calcParity = class1A.parity(mTCHParity) & 0x3f;
279
280 bool good = (sentParity == calcParity);
281
282 if (good)
283 {
284 unsigned char frameBuffer[mAMRFrameLth];
285 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
286 BitVector amrFrame(mKd + 8);
287 BitVector payload = amrFrame.tail(8);
288
289 // write frame header
290 amrFrame.fillField(0, mAMRFrameHeader, 8);
291
292 // We don't unmap here, but copy the decoded bits directly
293 // Decoder already delivers correct bit order
294 // mTCHD.unmap(mAMRBitOrder, payload.size(), payload);
295 mTCHD.copyTo(payload);
296 amrFrame.pack(frameBuffer);
297 fwrite(frameBuffer, 1 , mAMRFrameLth, d_speech_file);
298 }
299 }
300 }
301 }
302
303 void tch_f_decoder_impl::setCodingMode(tch_mode mode)
304 {
305 if (d_tch_mode != TCH_FS && d_tch_mode != TCH_EFR)
306 {
307 mKd = GSM::gAMRKd[d_tch_mode];
308 mTCHD.resize(mKd);
309 mTCHU.resize(mKd+6);
310 mTCHParity = Parity(0x06f,6, GSM::gAMRClass1ALth[d_tch_mode]);
311 mAMRBitOrder = GSM::gAMRBitOrder[d_tch_mode];
312 mClass1ALth = GSM::gAMRClass1ALth[d_tch_mode];
313 mClass1BLth = GSM::gAMRKd[d_tch_mode] - GSM::gAMRClass1ALth[d_tch_mode];
314 mTCHUC.resize(GSM::gAMRTCHUCLth[d_tch_mode]);
315 mPuncture = GSM::gAMRPuncture[d_tch_mode];
316 mPunctureLth = GSM::gAMRPunctureLth[d_tch_mode];
317 mClass1A_d.dup(mTCHD.head(mClass1ALth));
318
319 switch (d_tch_mode)
320 {
321 case TCH_AFS12_2:
322 mViterbi = new ViterbiTCH_AFS12_2();
323 mAMRFrameLth = 32;
324 mAMRFrameHeader = 0x3c;
325 break;
326 case TCH_AFS10_2:
327 mViterbi = new ViterbiTCH_AFS10_2();
328 mAMRFrameLth = 27;
329 mAMRFrameHeader = 0x3c;
330 break;
331 case TCH_AFS7_95:
332 mViterbi = new ViterbiTCH_AFS7_95();
333 mAMRFrameLth = 21;
334 mAMRFrameHeader = 0x3c;
335 break;
336 case TCH_AFS7_4:
337 mViterbi = new ViterbiTCH_AFS7_4();
338 mAMRFrameLth = 20;
339 mAMRFrameHeader = 0x3c;
340 break;
341 case TCH_AFS6_7:
342 mViterbi = new ViterbiTCH_AFS6_7();
343 mAMRFrameLth = 18;
344 mAMRFrameHeader = 0x3c;
345 break;
346 case TCH_AFS5_9:
347 mViterbi = new ViterbiTCH_AFS5_9();
348 mAMRFrameLth = 16;
349 mAMRFrameHeader = 0x14;
350 break;
351 case TCH_AFS5_15:
352 mViterbi = new ViterbiTCH_AFS5_15();
353 mAMRFrameLth = 14;
354 mAMRFrameHeader = 0x3c;
355 break;
356 case TCH_AFS4_75:
357 mViterbi = new ViterbiTCH_AFS4_75();
358 mAMRFrameLth = 13;
359 mAMRFrameHeader = 0x3c;
360 break;
361 default:
362 mViterbi = new ViterbiTCH_AFS12_2();
363 mAMRFrameLth = 32;
364 mAMRFrameHeader = 0x3c;
365 break;
Roman Khassraf059bab92015-05-20 12:49:46 +0200366 }
367 }
368 }
369 } /* namespace gsm */
370} /* namespace gr */
371