blob: 75d2402d2f58f754d37dade1fe36a5f801aae9c5 [file] [log] [blame]
Roman Khassraf059bab92015-05-20 12:49:46 +02001/* -*- c++ -*- */
2/*
3 * @file
4 * @author Piotr Krysik <ptrkrysik@gmail.com>
5 * @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
60 int j, k, B;
61 for (k = 0; k < CONV_SIZE; k++)
62 {
63 B = k % 8;
64 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
65 interleave_trans[k] = B * 114 + j;
66 }
67
68 //setup input/output ports
69 message_port_register_in(pmt::mp("bursts"));
70 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
71 message_port_register_out(pmt::mp("msgs"));
72 }
73
74 tch_f_decoder_impl::~tch_f_decoder_impl()
75 {
76 }
77
78 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
79 {
80 d_bursts[d_collected_bursts_num] = msg;
81 d_collected_bursts_num++;
82
83 bool stolen = false;
84
85 if (d_collected_bursts_num == 8)
86 {
87 unsigned char iBLOCK[2*BLOCKS*iBLOCK_SIZE];
88 SoftVector mC(CONV_SIZE);
89 SoftVector mClass1_c(mC.head(378));
90 SoftVector mClass2_c(mC.segment(378, 78));
91 BitVector mTCHU(189);
92 BitVector mTCHD(260);
93 BitVector mClass1A_d(mTCHD.head(50));
94 ViterbiR2O4 mVCoder;
95
96 d_collected_bursts_num = 0;
97
98 // reorganize data
99 for (int ii = 0; ii < 8; ii++)
100 {
101 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
102 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
103
104 for (int jj = 0; jj < 57; jj++)
105 {
106 iBLOCK[ii*114+jj] = burst_bits[jj + 3];
107 iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
108 }
109
110 if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1))
111 {
112 stolen = true;
113 }
114 }
115
116 // deinterleave
117 for (int k = 0; k < CONV_SIZE; k++)
118 {
119 mC[k] = iBLOCK[interleave_trans[k]];
120 }
121
122 // Decode stolen frames as FACCH/F
123 if (stolen)
124 {
125 BitVector mU(228);
126 BitVector mP(mU.segment(184,40));
127 BitVector mD(mU.head(184));
128 BitVector mDP(mU.head(224));
129 Parity mBlockCoder(0x10004820009ULL, 40, 224);
130
131 mC.decode(mVCoder, mU);
132 mP.invert();
133
134 unsigned syndrome = mBlockCoder.syndrome(mDP);
135
136 if (syndrome == 0)
137 {
138 unsigned char outmsg[27];
139 unsigned char sbuf_len=224;
140 int i, j, c, pos=0;
141 for(i = 0; i < sbuf_len; i += 8) {
142 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
143 c |= (!!mU.bit(i + j)) << j;
144 }
145 outmsg[pos++] = c & 0xff;
146 }
147
148 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
149 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
150 header->type = GSMTAP_TYPE_UM;
151 int8_t * header_content = (int8_t *)header;
152 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
153 memcpy(header_plus_data, header_content, sizeof(gsmtap_hdr));
154 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
155
156 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
157 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
158
159 message_port_pub(pmt::mp("msgs"), msg_out);
160 }
161 }
162
163 mClass1_c.decode(mVCoder, mTCHU);
164 mClass2_c.sliced().copyToSegment(mTCHD, 182);
165
166 // 3.1.2.1
167 // copy class 1 bits u[] to d[]
168 for (unsigned k = 0; k <= 90; k++) {
169 mTCHD[2*k] = mTCHU[k];
170 mTCHD[2*k+1] = mTCHU[184-k];
171 }
172
173 Parity mTCHParity(0x0b, 3, 50);
174
175 // 3.1.2.1
176 // check parity of class 1A
177 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
178 //unsigned calcParity = mTCHD.head(50).parity(mTCHParity) & 0x07;
179 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
180
181 // 3.1.2.2
182 // Check the tail bits, too.
183 unsigned tail = mTCHU.peekField(185, 4);
184
185 bool good = (sentParity == calcParity) && (tail == 0);
186
187 if (good)
188 {
189 unsigned char mTCHFrame[33];
190 unsigned int mTCHFrameLength;
191
192 if (d_tch_mode == MODE_SPEECH_FR) // GSM-FR
193 {
194 // Undo Um's importance-sorted bit ordering.
195 // See GSM 05.03 3.1 and Tablee 2.
196 VocoderFrame mVFrame;
197
198 BitVector payload = mVFrame.payload();
199 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
200 mVFrame.pack(mTCHFrame);
201 mTCHFrameLength = 33;
202 }
203 else if (d_tch_mode == MODE_SPEECH_EFR) // GSM-EFR / AMR 12.2
204 {
205 VocoderAMRFrame mVFrameAMR;
206
207 BitVector payload = mVFrameAMR.payload();
208 BitVector TCHW(260), EFRBits(244);
209
210 // Undo Um's EFR bit ordering.
211 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
212
213 // Remove repeating bits and CRC to get raw EFR frame (244 bits)
214 for (unsigned k=0; k<71; k++)
215 EFRBits[k] = TCHW[k] & 1;
216
217 for (unsigned k=73; k<123; k++)
218 EFRBits[k-2] = TCHW[k] & 1;
219
220 for (unsigned k=125; k<178; k++)
221 EFRBits[k-4] = TCHW[k] & 1;
222
223 for (unsigned k=180; k<230; k++)
224 EFRBits[k-6] = TCHW[k] & 1;
225
226 for (unsigned k=232; k<252; k++)
227 EFRBits[k-8] = TCHW[k] & 1;
228
229 // Map bits as AMR 12.2k
230 EFRBits.map(GSM::g690_12_2_BitOrder, 244, payload);
231
232 // Put the whole frame (hdr + payload)
233 mVFrameAMR.pack(mTCHFrame);
234 mTCHFrameLength = 32;
235 }
236 fwrite(mTCHFrame, 1 , mTCHFrameLength, d_speech_file);
237 }
238 }
239 }
240 } /* namespace gsm */
241} /* namespace gr */
242