Houston, we have a trunk.
[debian/gnuradio] / gr-atsc / src / lib / atsci_randomizer.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio 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 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <atsci_randomizer.h>
24 #include <assert.h>
25
26 unsigned char atsci_randomizer::s_output_map[1 << 14];
27 bool atsci_randomizer::s_output_map_initialized_p = false;
28
29 atsci_randomizer::atsci_randomizer ()
30 {
31   d_state = PRELOAD_VALUE;
32
33   if (!s_output_map_initialized_p)
34     initialize_output_map ();
35 }
36
37 /*!
38  * \brief Generate the table used in the fast_output_map function.
39  *
40  * The table has 16K byte entries, but because of how is is used, only
41  * 256 entries end up being resident in the cache.  This seems
42  * like a good use of memory.  We can get away with a 16K table
43  * because the low two bits of the state do not affect the output
44  * function.  By shifting right those two bits we shrink the table,
45  * and also get better cache line utilization.
46  */
47 void
48 atsci_randomizer::initialize_output_map ()
49 {
50   s_output_map_initialized_p = true;
51   
52   for (int i = 0; i < (1 << 14); i++)
53     s_output_map[i] = slow_output_map (i << 2);
54 }
55
56
57 void 
58 atsci_randomizer::reset ()
59 {
60   d_state = PRELOAD_VALUE;
61 }
62
63 void 
64 atsci_randomizer::randomize (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet &in)
65 {
66   assert (in.data[0] == MPEG_SYNC_BYTE);        // confirm it's there, then drop
67
68   for (int i = 0; i < ATSC_MPEG_DATA_LENGTH; i++)
69     out.data[i] = in.data[i + 1] ^ output_and_clk ();
70 }
71
72 void
73 atsci_randomizer::derandomize (atsc_mpeg_packet &out, const atsc_mpeg_packet_no_sync &in)
74 {
75   out.data[0] = MPEG_SYNC_BYTE;         // add sync byte to beginning of packet
76
77   for (int i = 0; i < ATSC_MPEG_DATA_LENGTH; i++)
78     out.data[i + 1] = in.data[i] ^ output_and_clk ();
79 }
80
81
82 unsigned char
83 atsci_randomizer::slow_output_map (int st)
84 {
85   int   output = 0;
86
87   if (st & 0x8000)
88     output |= 0x01;
89
90   if (st & 0x2000)
91     output |= 0x02;
92
93   if (st & 0x1000)
94     output |= 0x04;
95
96   if (st & 0x0200)
97     output |= 0x08;
98
99   if (st & 0x0020)
100     output |= 0x10;
101
102   if (st & 0x0010)
103     output |= 0x20;
104
105   if (st & 0x0008)
106     output |= 0x40;
107
108   if (st & 0x0004)
109     output |= 0x80;
110
111   return output;
112 }