blob: c989151c1ca03ae207c53143dc2bf322db93513d [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),
55 mP(mU.segment(184,40)), mD(mU.head(184)), mDP(mU.head(224)),
56 mC(CONV_SIZE),
57 mClass1_c(mC.head(378)),
58 mClass2_c(mC.segment(378, 78)),
59 mTCHU(189),
60 mTCHD(260),
61 mClass1A_d(mTCHD.head(50))
Roman Khassraf059bab92015-05-20 12:49:46 +020062 {
63 d_speech_file = fopen( file.c_str(), "wb" );
64 if (d_speech_file == NULL)
65 {
66 throw std::runtime_error("TCH/F Decoder: can't open file\n");
67 }
68
Roman Khassraffa7058b2015-06-02 09:21:11 +020069 if (d_tch_mode != TCH_FS)
Roman Khassraf091a80f2015-05-22 10:39:16 +020070 {
71 fwrite(amr_nb_magic, 1, 6, d_speech_file);
72 }
73
Roman Khassraf059bab92015-05-20 12:49:46 +020074 int j, k, B;
75 for (k = 0; k < CONV_SIZE; k++)
76 {
77 B = k % 8;
78 j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
79 interleave_trans[k] = B * 114 + j;
80 }
81
82 //setup input/output ports
83 message_port_register_in(pmt::mp("bursts"));
84 set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
85 message_port_register_out(pmt::mp("msgs"));
86 }
87
88 tch_f_decoder_impl::~tch_f_decoder_impl()
89 {
90 }
91
92 void tch_f_decoder_impl::decode(pmt::pmt_t msg)
93 {
94 d_bursts[d_collected_bursts_num] = msg;
95 d_collected_bursts_num++;
96
97 bool stolen = false;
98
99 if (d_collected_bursts_num == 8)
100 {
Roman Khassraf059bab92015-05-20 12:49:46 +0200101 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 {
Roman Khassrafe1593332015-06-02 13:19:01 +0200130 mVR204Coder.decode(mC, mU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200131 mP.invert();
132
133 unsigned syndrome = mBlockCoder.syndrome(mDP);
134
135 if (syndrome == 0)
136 {
137 unsigned char outmsg[27];
138 unsigned char sbuf_len=224;
139 int i, j, c, pos=0;
140 for(i = 0; i < sbuf_len; i += 8) {
141 for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
142 c |= (!!mU.bit(i + j)) << j;
143 }
144 outmsg[pos++] = c & 0xff;
145 }
146
147 pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
148 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
149 header->type = GSMTAP_TYPE_UM;
150 int8_t * header_content = (int8_t *)header;
151 int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
152 memcpy(header_plus_data, header_content, sizeof(gsmtap_hdr));
153 memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
154
155 pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
156 pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
157
158 message_port_pub(pmt::mp("msgs"), msg_out);
159 }
160 }
161
Roman Khassrafe1593332015-06-02 13:19:01 +0200162 mVR204Coder.decode(mClass1_c, mTCHU);
Roman Khassraf059bab92015-05-20 12:49:46 +0200163 mClass2_c.sliced().copyToSegment(mTCHD, 182);
164
165 // 3.1.2.1
166 // copy class 1 bits u[] to d[]
167 for (unsigned k = 0; k <= 90; k++) {
168 mTCHD[2*k] = mTCHU[k];
169 mTCHD[2*k+1] = mTCHU[184-k];
170 }
171
172 Parity mTCHParity(0x0b, 3, 50);
173
174 // 3.1.2.1
175 // check parity of class 1A
176 unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
177 //unsigned calcParity = mTCHD.head(50).parity(mTCHParity) & 0x07;
178 unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
179
180 // 3.1.2.2
181 // Check the tail bits, too.
182 unsigned tail = mTCHU.peekField(185, 4);
183
184 bool good = (sentParity == calcParity) && (tail == 0);
185
186 if (good)
187 {
188 unsigned char mTCHFrame[33];
189 unsigned int mTCHFrameLength;
190
Roman Khassraffa7058b2015-06-02 09:21:11 +0200191 if (d_tch_mode == TCH_FS) // GSM-FR
Roman Khassraf059bab92015-05-20 12:49:46 +0200192 {
193 // Undo Um's importance-sorted bit ordering.
194 // See GSM 05.03 3.1 and Tablee 2.
195 VocoderFrame mVFrame;
196
197 BitVector payload = mVFrame.payload();
198 mTCHD.unmap(GSM::g610BitOrder, 260, payload);
199 mVFrame.pack(mTCHFrame);
200 mTCHFrameLength = 33;
201 }
Roman Khassraffa7058b2015-06-02 09:21:11 +0200202 else if (d_tch_mode == TCH_EFR) // GSM-EFR / AMR 12.2
Roman Khassraf059bab92015-05-20 12:49:46 +0200203 {
204 VocoderAMRFrame mVFrameAMR;
205
206 BitVector payload = mVFrameAMR.payload();
207 BitVector TCHW(260), EFRBits(244);
208
209 // Undo Um's EFR bit ordering.
210 mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
211
212 // Remove repeating bits and CRC to get raw EFR frame (244 bits)
213 for (unsigned k=0; k<71; k++)
214 EFRBits[k] = TCHW[k] & 1;
215
216 for (unsigned k=73; k<123; k++)
217 EFRBits[k-2] = TCHW[k] & 1;
218
219 for (unsigned k=125; k<178; k++)
220 EFRBits[k-4] = TCHW[k] & 1;
221
222 for (unsigned k=180; k<230; k++)
223 EFRBits[k-6] = TCHW[k] & 1;
224
225 for (unsigned k=232; k<252; k++)
226 EFRBits[k-8] = TCHW[k] & 1;
227
228 // Map bits as AMR 12.2k
229 EFRBits.map(GSM::g690_12_2_BitOrder, 244, payload);
230
231 // Put the whole frame (hdr + payload)
232 mVFrameAMR.pack(mTCHFrame);
233 mTCHFrameLength = 32;
234 }
235 fwrite(mTCHFrame, 1 , mTCHFrameLength, d_speech_file);
236 }
237 }
238 }
239 } /* namespace gsm */
240} /* namespace gr */
241