1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015,2016,2019 Free Software Foundation, Inc.
4  *
5  * This is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3, or (at your option)
8  * any later version.
9  *
10  * This software is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this software; see the file COPYING.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "dvb_bbscrambler_bb_impl.h"
26 #include <gnuradio/io_signature.h>
27 
28 namespace gr {
29 namespace dtv {
30 
make(dvb_standard_t standard,dvb_framesize_t framesize,dvb_code_rate_t rate)31 dvb_bbscrambler_bb::sptr dvb_bbscrambler_bb::make(dvb_standard_t standard,
32                                                   dvb_framesize_t framesize,
33                                                   dvb_code_rate_t rate)
34 {
35     return gnuradio::get_initial_sptr(
36         new dvb_bbscrambler_bb_impl(standard, framesize, rate));
37 }
38 
39 /*
40  * The private constructor
41  */
dvb_bbscrambler_bb_impl(dvb_standard_t standard,dvb_framesize_t framesize,dvb_code_rate_t rate)42 dvb_bbscrambler_bb_impl::dvb_bbscrambler_bb_impl(dvb_standard_t standard,
43                                                  dvb_framesize_t framesize,
44                                                  dvb_code_rate_t rate)
45     : gr::sync_block("dvb_bbscrambler_bb",
46                      gr::io_signature::make(1, 1, sizeof(unsigned char)),
47                      gr::io_signature::make(1, 1, sizeof(unsigned char)))
48 {
49     if (framesize == FECFRAME_NORMAL) {
50         switch (rate) {
51         case C1_4:
52             kbch = 16008;
53             break;
54         case C1_3:
55             kbch = 21408;
56             break;
57         case C2_5:
58             kbch = 25728;
59             break;
60         case C1_2:
61             kbch = 32208;
62             break;
63         case C3_5:
64             kbch = 38688;
65             break;
66         case C2_3:
67             kbch = 43040;
68             break;
69         case C3_4:
70             kbch = 48408;
71             break;
72         case C4_5:
73             kbch = 51648;
74             break;
75         case C5_6:
76             kbch = 53840;
77             break;
78         case C8_9:
79             kbch = 57472;
80             break;
81         case C9_10:
82             kbch = 58192;
83             break;
84         case C2_9_VLSNR:
85             kbch = 14208;
86             break;
87         case C13_45:
88             kbch = 18528;
89             break;
90         case C9_20:
91             kbch = 28968;
92             break;
93         case C90_180:
94             kbch = 32208;
95             break;
96         case C96_180:
97             kbch = 34368;
98             break;
99         case C11_20:
100             kbch = 35448;
101             break;
102         case C100_180:
103             kbch = 35808;
104             break;
105         case C104_180:
106             kbch = 37248;
107             break;
108         case C26_45:
109             kbch = 37248;
110             break;
111         case C18_30:
112             kbch = 38688;
113             break;
114         case C28_45:
115             kbch = 40128;
116             break;
117         case C23_36:
118             kbch = 41208;
119             break;
120         case C116_180:
121             kbch = 41568;
122             break;
123         case C20_30:
124             kbch = 43008;
125             break;
126         case C124_180:
127             kbch = 44448;
128             break;
129         case C25_36:
130             kbch = 44808;
131             break;
132         case C128_180:
133             kbch = 45888;
134             break;
135         case C13_18:
136             kbch = 46608;
137             break;
138         case C132_180:
139             kbch = 47328;
140             break;
141         case C22_30:
142             kbch = 47328;
143             break;
144         case C135_180:
145             kbch = 48408;
146             break;
147         case C140_180:
148             kbch = 50208;
149             break;
150         case C7_9:
151             kbch = 50208;
152             break;
153         case C154_180:
154             kbch = 55248;
155             break;
156         default:
157             kbch = 0;
158             break;
159         }
160     } else if (framesize == FECFRAME_SHORT) {
161         switch (rate) {
162         case C1_4:
163             kbch = 3072;
164             break;
165         case C1_3:
166             kbch = 5232;
167             break;
168         case C2_5:
169             kbch = 6312;
170             break;
171         case C1_2:
172             kbch = 7032;
173             break;
174         case C3_5:
175             kbch = 9552;
176             break;
177         case C2_3:
178             kbch = 10632;
179             break;
180         case C3_4:
181             kbch = 11712;
182             break;
183         case C4_5:
184             kbch = 12432;
185             break;
186         case C5_6:
187             kbch = 13152;
188             break;
189         case C8_9:
190             kbch = 14232;
191             break;
192         case C11_45:
193             kbch = 3792;
194             break;
195         case C4_15:
196             kbch = 4152;
197             break;
198         case C14_45:
199             kbch = 4872;
200             break;
201         case C7_15:
202             kbch = 7392;
203             break;
204         case C8_15:
205             kbch = 8472;
206             break;
207         case C26_45:
208             kbch = 9192;
209             break;
210         case C32_45:
211             kbch = 11352;
212             break;
213         case C1_5_VLSNR_SF2:
214             kbch = 2512;
215             break;
216         case C11_45_VLSNR_SF2:
217             kbch = 3792;
218             break;
219         case C1_5_VLSNR:
220             kbch = 3072;
221             break;
222         case C4_15_VLSNR:
223             kbch = 4152;
224             break;
225         case C1_3_VLSNR:
226             kbch = 5232;
227             break;
228         default:
229             kbch = 0;
230             break;
231         }
232     } else {
233         switch (rate) {
234         case C1_5_MEDIUM:
235             kbch = 5660;
236             break;
237         case C11_45_MEDIUM:
238             kbch = 7740;
239             break;
240         case C1_3_MEDIUM:
241             kbch = 10620;
242             break;
243         default:
244             kbch = 0;
245             break;
246         }
247     }
248 
249     init_bb_randomiser();
250     frame_size = framesize;
251     set_output_multiple(kbch);
252 }
253 
254 /*
255  * Our virtual destructor.
256  */
~dvb_bbscrambler_bb_impl()257 dvb_bbscrambler_bb_impl::~dvb_bbscrambler_bb_impl() {}
258 
init_bb_randomiser(void)259 void dvb_bbscrambler_bb_impl::init_bb_randomiser(void)
260 {
261     int sr = 0x4A80;
262     for (int i = 0; i < FRAME_SIZE_NORMAL; i++) {
263         int b = ((sr) ^ (sr >> 1)) & 1;
264         bb_randomise[i] = b;
265         sr >>= 1;
266         if (b) {
267             sr |= 0x4000;
268         }
269     }
270     bb_randomize32 = (uint32_t*)&bb_randomise[0];
271     bb_randomize64 = (uint64_t*)&bb_randomise[0];
272 }
273 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)274 int dvb_bbscrambler_bb_impl::work(int noutput_items,
275                                   gr_vector_const_void_star& input_items,
276                                   gr_vector_void_star& output_items)
277 {
278     const uint64_t* in = (const uint64_t*)input_items[0];
279     uint64_t* out = (uint64_t*)output_items[0];
280     const uint32_t* inm = (const uint32_t*)input_items[0];
281     uint32_t* outm = (uint32_t*)output_items[0];
282 
283     if (frame_size != FECFRAME_MEDIUM) {
284         for (int i = 0; i < noutput_items; i += kbch) {
285             for (int j = 0; j < kbch / 8; ++j) {
286                 *out++ = *in++ ^ bb_randomize64[j];
287             }
288         }
289     } else {
290         for (int i = 0; i < noutput_items; i += kbch) {
291             for (int j = 0; j < kbch / 4; ++j) {
292                 *outm++ = *inm++ ^ bb_randomize32[j];
293             }
294         }
295     }
296 
297     // Tell runtime system how many output items we produced.
298     return noutput_items;
299 }
300 
301 } /* namespace dtv */
302 } /* namespace gr */
303