36025d10b8b4f295b9d63d8f06aa7b1151c1ef9d
[debian/gnuradio] / gr-atsc / src / lib / atsci_randomizer.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2001 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 3, 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., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef _ATSC_RANDOMIZER_H_
24 #define _ATSC_RANDOMIZER_H_
25
26 #include <atsc_types.h>
27
28 /*! 
29  * \brief ATSC data "whitener"
30  *
31  * The data randomizer described in ATSC standard A/53B.  
32  * See figure D4 on page 54.
33  */
34
35 class atsci_randomizer {
36   friend class qa_atsci_randomizer;
37   
38  public:
39   atsci_randomizer();
40
41   /*! \brief reset randomizer LFSR
42    *
43    * must be called during the Data Segment Sync interval prior to the
44    * first data segment.  I.e., the LFSR is reset prior to the first
45    * field of each VSB data frame.
46    */
47   void reset ();
48
49   //! randomize (whiten) mpeg packet and remove leading MPEG-2 sync byte
50   void randomize (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet &in);
51
52   //! derandomize (de-whiten) mpeg packet and add leading MPEG-2 sync byte
53   void derandomize (atsc_mpeg_packet &out, const atsc_mpeg_packet_no_sync &in);
54
55   unsigned int state() const { return d_state; }
56
57  private:
58   static void initialize_output_map ();
59   static unsigned char slow_output_map (int st);
60
61   static unsigned char fast_output_map (int st){
62     return s_output_map[(st & 0xb23c) >> 2]; // Magic const with 8 bits set improves cache 
63                                              // utilization.  The bits correspond to the taps
64                                              // used in output calculation.  Others may be 
65                                              // safely ignored.
66   }
67     
68   //! return current output value
69   unsigned char output (){
70     return fast_output_map (d_state);
71   }
72
73   //! clock LFSR; advance to next state.
74   void clk (){
75     if (d_state & 0x1)
76       d_state = ((d_state ^ MASK) >> 1) | 0x8000;
77     else
78       d_state = d_state >> 1;
79   }
80
81   //! return current output value and advance to next state
82   unsigned char output_and_clk (){
83     unsigned char r = output ();
84     clk ();
85     return r;
86   }
87
88   unsigned int              d_state;
89
90   static const unsigned int PRELOAD_VALUE = 0x018f; /* 0xf180 bit reversed */
91   static const unsigned int MASK = 0xa638;
92   static unsigned char      s_output_map[1 << 14];
93   static bool               s_output_map_initialized_p;
94 };
95
96 #endif /* _ATSC_RANDOMIZER_H_ */