blob: b4ff24fb108232e52d23f0c885b7c1c7b48ff6ef [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 }
71
Roman Khassraffa7058b2015-06-02 09:21:11 +020072 if (d_tch_mode != TCH_FS)
Roman Khassraf091a80f2015-05-22 10:39:16 +020073 {
74 fwrite(amr_nb_magic, 1, 6, d_speech_file);
75 }
76
Roman Khassraf059bab92015-05-20 12:49:46 +020077 int j, k, B;
78 for (k = 0; k < CONV_SIZE; k++)
79 {
80 B = k % 8;
81 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
82 interleave_trans[k] = B * 114 + j;
83 }
84
85 //setup input/output ports
86 message_port_register_in(pmt::mp("bursts"));
87 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
88 message_port_register_out(pmt::mp("msgs"));
Roman Khassrafd38206c2015-06-07 16:26:29 +020089
90 setCodingMode(mode);
Roman Khassraf059bab92015-05-20 12:49:46 +020091 }
92
93 tch_f_decoder_impl::~tch_f_decoder_impl()
94 {
95 }
96
97 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
98 {
99 d_bursts[d_collected_bursts_num] = msg;
100 d_collected_bursts_num++;
101
102 bool stolen = false;
103
104 if (d_collected_bursts_num == 8)
105 {
Roman Khassraf059bab92015-05-20 12:49:46 +0200106 d_collected_bursts_num = 0;
107
108 // reorganize data
109 for (int ii = 0; ii < 8; ii++)
110 {
111 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
112 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
113
114 for (int jj = 0; jj < 57; jj++)
115 {
116 iBLOCK[ii*114+jj] = burst_bits[jj + 3];
117 iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
118 }
119
120 if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1))
121 {
122 stolen = true;
123 }
124 }
125
126 // deinterleave
127 for (int k = 0; k < CONV_SIZE; k++)
128 {
129 mC[k] = iBLOCK[interleave_trans[k]];
130 }
131
132 // Decode stolen frames as FACCH/F
133 if (stolen)
134 {
Roman Khassrafe1593332015-06-02 13:19:01 +0200135 mVR204Coder.decode(mC, mU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200136 mP.invert();
137
138 unsigned syndrome = mBlockCoder.syndrome(mDP);
139
140 if (syndrome == 0)
141 {
142 unsigned char outmsg[27];
143 unsigned char sbuf_len=224;
144 int i, j, c, pos=0;
145 for(i = 0; i < sbuf_len; i += 8) {
146 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
147 c |= (!!mU.bit(i + j)) << j;
148 }
149 outmsg[pos++] = c & 0xff;
150 }
151
152 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
153 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
154 header->type = GSMTAP_TYPE_UM;
155 int8_t * header_content = (int8_t *)header;
156 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
157 memcpy(header_plus_data, header_content, sizeof(gsmtap_hdr));
158 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
159
160 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
161 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
162
163 message_port_pub(pmt::mp("msgs"), msg_out);
164 }
165 }
166
Roman Khassrafd38206c2015-06-07 16:26:29 +0200167 // Decode voice frames and write to file
168 if (d_tch_mode == TCH_FS || d_tch_mode == TCH_EFR)
Roman Khassraf059bab92015-05-20 12:49:46 +0200169 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200170 mVR204Coder.decode(mClass1_c, mTCHU);
171 mClass2_c.sliced().copyToSegment(mTCHD, 182);
Roman Khassraf059bab92015-05-20 12:49:46 +0200172
Roman Khassrafd38206c2015-06-07 16:26:29 +0200173 // 3.1.2.1
174 // copy class 1 bits u[] to d[]
175 for (unsigned k = 0; k <= 90; k++) {
176 mTCHD[2*k] = mTCHU[k];
177 mTCHD[2*k+1] = mTCHU[184-k];
Roman Khassraf059bab92015-05-20 12:49:46 +0200178 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200179
180 // 3.1.2.1
181 // check parity of class 1A
182 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
183 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
184
185 bool good = (sentParity == calcParity);
186
187 if (good)
Roman Khassraf059bab92015-05-20 12:49:46 +0200188 {
Roman Khassrafd38206c2015-06-07 16:26:29 +0200189 unsigned char frameBuffer[33];
190 unsigned int mTCHFrameLength;
Roman Khassraf059bab92015-05-20 12:49:46 +0200191
Roman Khassrafd38206c2015-06-07 16:26:29 +0200192 if (d_tch_mode == TCH_FS) // GSM-FR
193 {
194 unsigned char mFrameHeader = 0x0d;
195 mTCHFrameLength = 33;
Roman Khassraf059bab92015-05-20 12:49:46 +0200196
Roman Khassrafd38206c2015-06-07 16:26:29 +0200197 // Undo Um's importance-sorted bit ordering.
198 // See GSM 05.03 3.1 and Table 2.
199 BitVector frFrame(260 + 4); // FR has a frameheader of 4 bits only
200 BitVector payload = frFrame.tail(4);
Roman Khassraf059bab92015-05-20 12:49:46 +0200201
Roman Khassrafd38206c2015-06-07 16:26:29 +0200202 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
203 frFrame.pack(frameBuffer);
Roman Khassraf059bab92015-05-20 12:49:46 +0200204
Roman Khassrafd38206c2015-06-07 16:26:29 +0200205 }
206 else if (d_tch_mode == TCH_EFR) // GSM-EFR
207 {
208 unsigned char mFrameHeader = 0x3c;
Roman Khassraf059bab92015-05-20 12:49:46 +0200209
Roman Khassrafd38206c2015-06-07 16:26:29 +0200210 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
211 BitVector amrFrame(244 + 8); // Same output length as AMR 12.2
212 BitVector payload = amrFrame.tail(8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200213
Roman Khassrafd38206c2015-06-07 16:26:29 +0200214 BitVector TCHW(260), EFRBits(244);
Roman Khassraf059bab92015-05-20 12:49:46 +0200215
Roman Khassrafd38206c2015-06-07 16:26:29 +0200216 // write frame header
217 amrFrame.fillField(0, mFrameHeader, 8);
Roman Khassraf059bab92015-05-20 12:49:46 +0200218
Roman Khassrafd38206c2015-06-07 16:26:29 +0200219 // Undo Um's EFR bit ordering.
220 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
Roman Khassraf059bab92015-05-20 12:49:46 +0200221
Roman Khassrafd38206c2015-06-07 16:26:29 +0200222 // Remove repeating bits and CRC to get raw EFR frame (244 bits)
223 for (unsigned k=0; k<71; k++)
224 EFRBits[k] = TCHW[k] & 1;
225
226 for (unsigned k=73; k<123; k++)
227 EFRBits[k-2] = TCHW[k] & 1;
228
229 for (unsigned k=125; k<178; k++)
230 EFRBits[k-4] = TCHW[k] & 1;
231
232 for (unsigned k=180; k<230; k++)
233 EFRBits[k-6] = TCHW[k] & 1;
234
235 for (unsigned k=232; k<252; k++)
236 EFRBits[k-8] = TCHW[k] & 1;
237
238 // Map bits as AMR 12.2k
239 EFRBits.map(GSM::g690_12_2_BitOrder, 244, payload);
240
241 // Put the whole frame (hdr + payload)
242 mTCHFrameLength = 32;
243 amrFrame.pack(frameBuffer);
244
245 }
246 fwrite(frameBuffer, 1 , mTCHFrameLength, d_speech_file);
Roman Khassraf059bab92015-05-20 12:49:46 +0200247 }
Roman Khassrafd38206c2015-06-07 16:26:29 +0200248 }
249 else
250 {
251 // Handle inband bits, see 3.9.4.1
252 // OpenBTS source takes last 8 bits as inband bits for some reason. This may be either a
253 // divergence between their implementation and GSM specification, which works because
254 // both their encoder and decoder do it same way, or they handle the issue at some other place
255 // SoftVector cMinus8 = mC.segment(0, mC.size() - 8);
256 SoftVector cMinus8 = mC.segment(8, mC.size());
257 cMinus8.copyUnPunctured(mTCHUC, mPuncture, mPunctureLth);
258
259 // 3.9.4.4
260 // decode from uc[] to u[]
261 mViterbi->decode(mTCHUC, mTCHU);
262
263 // 3.9.4.3 -- class 1a bits in u[] to d[]
264 for (unsigned k=0; k < mClass1ALth; k++) {
265 mTCHD[k] = mTCHU[k];
266 }
267
268 // 3.9.4.3 -- class 1b bits in u[] to d[]
269 for (unsigned k=0; k < mClass1BLth; k++) {
270 mTCHD[k+mClass1ALth] = mTCHU[k+mClass1ALth+6];
271 }
272
273 // Check parity
274 unsigned sentParity = (~mTCHU.peekField(mClass1ALth,6)) & 0x3f;
275 BitVector class1A = mTCHU.segment(0, mClass1ALth);
276 unsigned calcParity = class1A.parity(mTCHParity) & 0x3f;
277
278 bool good = (sentParity == calcParity);
279
280 if (good)
281 {
282 unsigned char frameBuffer[mAMRFrameLth];
283 // AMR Frame, consisting of a 8 bit frame header, plus the payload from decoding
284 BitVector amrFrame(mKd + 8);
285 BitVector payload = amrFrame.tail(8);
286
287 // write frame header
288 amrFrame.fillField(0, mAMRFrameHeader, 8);
289
290 // We don't unmap here, but copy the decoded bits directly
291 // Decoder already delivers correct bit order
292 // mTCHD.unmap(mAMRBitOrder, payload.size(), payload);
293 mTCHD.copyTo(payload);
294 amrFrame.pack(frameBuffer);
295 fwrite(frameBuffer, 1 , mAMRFrameLth, d_speech_file);
296 }
297 }
298 }
299 }
300
301 void tch_f_decoder_impl::setCodingMode(tch_mode mode)
302 {
303 if (d_tch_mode != TCH_FS && d_tch_mode != TCH_EFR)
304 {
305 mKd = GSM::gAMRKd[d_tch_mode];
306 mTCHD.resize(mKd);
307 mTCHU.resize(mKd+6);
308 mTCHParity = Parity(0x06f,6, GSM::gAMRClass1ALth[d_tch_mode]);
309 mAMRBitOrder = GSM::gAMRBitOrder[d_tch_mode];
310 mClass1ALth = GSM::gAMRClass1ALth[d_tch_mode];
311 mClass1BLth = GSM::gAMRKd[d_tch_mode] - GSM::gAMRClass1ALth[d_tch_mode];
312 mTCHUC.resize(GSM::gAMRTCHUCLth[d_tch_mode]);
313 mPuncture = GSM::gAMRPuncture[d_tch_mode];
314 mPunctureLth = GSM::gAMRPunctureLth[d_tch_mode];
315 mClass1A_d.dup(mTCHD.head(mClass1ALth));
316
317 switch (d_tch_mode)
318 {
319 case TCH_AFS12_2:
320 mViterbi = new ViterbiTCH_AFS12_2();
321 mAMRFrameLth = 32;
322 mAMRFrameHeader = 0x3c;
323 break;
324 case TCH_AFS10_2:
325 mViterbi = new ViterbiTCH_AFS10_2();
326 mAMRFrameLth = 27;
327 mAMRFrameHeader = 0x3c;
328 break;
329 case TCH_AFS7_95:
330 mViterbi = new ViterbiTCH_AFS7_95();
331 mAMRFrameLth = 21;
332 mAMRFrameHeader = 0x3c;
333 break;
334 case TCH_AFS7_4:
335 mViterbi = new ViterbiTCH_AFS7_4();
336 mAMRFrameLth = 20;
337 mAMRFrameHeader = 0x3c;
338 break;
339 case TCH_AFS6_7:
340 mViterbi = new ViterbiTCH_AFS6_7();
341 mAMRFrameLth = 18;
342 mAMRFrameHeader = 0x3c;
343 break;
344 case TCH_AFS5_9:
345 mViterbi = new ViterbiTCH_AFS5_9();
346 mAMRFrameLth = 16;
347 mAMRFrameHeader = 0x14;
348 break;
349 case TCH_AFS5_15:
350 mViterbi = new ViterbiTCH_AFS5_15();
351 mAMRFrameLth = 14;
352 mAMRFrameHeader = 0x3c;
353 break;
354 case TCH_AFS4_75:
355 mViterbi = new ViterbiTCH_AFS4_75();
356 mAMRFrameLth = 13;
357 mAMRFrameHeader = 0x3c;
358 break;
359 default:
360 mViterbi = new ViterbiTCH_AFS12_2();
361 mAMRFrameLth = 32;
362 mAMRFrameHeader = 0x3c;
363 break;
Roman Khassraf059bab92015-05-20 12:49:46 +0200364 }
365 }
366 }
367 } /* namespace gsm */
368} /* namespace gr */
369