blob: c8265eacd00a8c307a346ce4e076862cf6ac4da5 [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),
52 d_collected_bursts_num(0)
53 {
54 d_speech_file = fopen( file.c_str(), "wb" );
55 if (d_speech_file == NULL)
56 {
57 throw std::runtime_error("TCH/F Decoder: can't open file\n");
58 }
59
Roman Khassraffa7058b2015-06-02 09:21:11 +020060 if (d_tch_mode != TCH_FS)
Roman Khassraf091a80f2015-05-22 10:39:16 +020061 {
62 fwrite(amr_nb_magic, 1, 6, d_speech_file);
63 }
64
Roman Khassraf059bab92015-05-20 12:49:46 +020065 int j, k, B;
66 for (k = 0; k < CONV_SIZE; k++)
67 {
68 B = k % 8;
69 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
70 interleave_trans[k] = B * 114 + j;
71 }
72
73 //setup input/output ports
74 message_port_register_in(pmt::mp("bursts"));
75 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
76 message_port_register_out(pmt::mp("msgs"));
77 }
78
79 tch_f_decoder_impl::~tch_f_decoder_impl()
80 {
81 }
82
83 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
84 {
85 d_bursts[d_collected_bursts_num] = msg;
86 d_collected_bursts_num++;
87
88 bool stolen = false;
89
90 if (d_collected_bursts_num == 8)
91 {
92 unsigned char iBLOCK[2*BLOCKS*iBLOCK_SIZE];
93 SoftVector mC(CONV_SIZE);
94 SoftVector mClass1_c(mC.head(378));
95 SoftVector mClass2_c(mC.segment(378, 78));
96 BitVector mTCHU(189);
97 BitVector mTCHD(260);
98 BitVector mClass1A_d(mTCHD.head(50));
99 ViterbiR2O4 mVCoder;
100
101 d_collected_bursts_num = 0;
102
103 // reorganize data
104 for (int ii = 0; ii < 8; ii++)
105 {
106 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
107 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
108
109 for (int jj = 0; jj < 57; jj++)
110 {
111 iBLOCK[ii*114+jj] = burst_bits[jj + 3];
112 iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
113 }
114
115 if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1))
116 {
117 stolen = true;
118 }
119 }
120
121 // deinterleave
122 for (int k = 0; k < CONV_SIZE; k++)
123 {
124 mC[k] = iBLOCK[interleave_trans[k]];
125 }
126
127 // Decode stolen frames as FACCH/F
128 if (stolen)
129 {
130 BitVector mU(228);
131 BitVector mP(mU.segment(184,40));
132 BitVector mD(mU.head(184));
133 BitVector mDP(mU.head(224));
134 Parity mBlockCoder(0x10004820009ULL, 40, 224);
135
Roman Khassrafd71e6582015-06-02 08:49:12 +0200136// mC.decode(mVCoder, mU);
137 mVCoder.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 Khassrafd71e6582015-06-02 08:49:12 +0200169 mVCoder.decode(mClass1_c, mTCHU);
170// mClass1_c.decode(mVCoder, mTCHU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200171 mClass2_c.sliced().copyToSegment(mTCHD, 182);
172
173 // 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];
178 }
179
180 Parity mTCHParity(0x0b, 3, 50);
181
182 // 3.1.2.1
183 // check parity of class 1A
184 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
185 //unsigned calcParity = mTCHD.head(50).parity(mTCHParity) & 0x07;
186 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
187
188 // 3.1.2.2
189 // Check the tail bits, too.
190 unsigned tail = mTCHU.peekField(185, 4);
191
192 bool good = (sentParity == calcParity) && (tail == 0);
193
194 if (good)
195 {
196 unsigned char mTCHFrame[33];
197 unsigned int mTCHFrameLength;
198
Roman Khassraffa7058b2015-06-02 09:21:11 +0200199 if (d_tch_mode == TCH_FS) // GSM-FR
Roman Khassraf059bab92015-05-20 12:49:46 +0200200 {
201 // Undo Um's importance-sorted bit ordering.
202 // See GSM 05.03 3.1 and Tablee 2.
203 VocoderFrame mVFrame;
204
205 BitVector payload = mVFrame.payload();
206 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
207 mVFrame.pack(mTCHFrame);
208 mTCHFrameLength = 33;
209 }
Roman Khassraffa7058b2015-06-02 09:21:11 +0200210 else if (d_tch_mode == TCH_EFR) // GSM-EFR / AMR 12.2
Roman Khassraf059bab92015-05-20 12:49:46 +0200211 {
212 VocoderAMRFrame mVFrameAMR;
213
214 BitVector payload = mVFrameAMR.payload();
215 BitVector TCHW(260), EFRBits(244);
216
217 // Undo Um's EFR bit ordering.
218 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
219
220 // Remove repeating bits and CRC to get raw EFR frame (244 bits)
221 for (unsigned k=0; k<71; k++)
222 EFRBits[k] = TCHW[k] & 1;
223
224 for (unsigned k=73; k<123; k++)
225 EFRBits[k-2] = TCHW[k] & 1;
226
227 for (unsigned k=125; k<178; k++)
228 EFRBits[k-4] = TCHW[k] & 1;
229
230 for (unsigned k=180; k<230; k++)
231 EFRBits[k-6] = TCHW[k] & 1;
232
233 for (unsigned k=232; k<252; k++)
234 EFRBits[k-8] = TCHW[k] & 1;
235
236 // Map bits as AMR 12.2k
237 EFRBits.map(GSM::g690_12_2_BitOrder, 244, payload);
238
239 // Put the whole frame (hdr + payload)
240 mVFrameAMR.pack(mTCHFrame);
241 mTCHFrameLength = 32;
242 }
243 fwrite(mTCHFrame, 1 , mTCHFrameLength, d_speech_file);
244 }
245 }
246 }
247 } /* namespace gsm */
248} /* namespace gr */
249