GNU Radio 3.5.1 C++ API
atsci_randomizer.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2001 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef _ATSC_RANDOMIZER_H_
00024 #define _ATSC_RANDOMIZER_H_
00025 
00026 #include <atsc_api.h>
00027 #include <atsc_types.h>
00028 
00029 /*! 
00030  * \brief ATSC data "whitener"
00031  *
00032  * The data randomizer described in ATSC standard A/53B.  
00033  * See figure D4 on page 54.
00034  */
00035 
00036 class ATSC_API atsci_randomizer {
00037   friend class qa_atsci_randomizer;
00038   
00039  public:
00040   atsci_randomizer();
00041 
00042   /*! \brief reset randomizer LFSR
00043    *
00044    * must be called during the Data Segment Sync interval prior to the
00045    * first data segment.  I.e., the LFSR is reset prior to the first
00046    * field of each VSB data frame.
00047    */
00048   void reset ();
00049 
00050   //! randomize (whiten) mpeg packet and remove leading MPEG-2 sync byte
00051   void randomize (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet &in);
00052 
00053   //! derandomize (de-whiten) mpeg packet and add leading MPEG-2 sync byte
00054   void derandomize (atsc_mpeg_packet &out, const atsc_mpeg_packet_no_sync &in);
00055 
00056   unsigned int state() const { return d_state; }
00057 
00058  private:
00059   static void initialize_output_map ();
00060   static unsigned char slow_output_map (int st);
00061 
00062   static unsigned char fast_output_map (int st){
00063     return s_output_map[(st & 0xb23c) >> 2]; // Magic const with 8 bits set improves cache 
00064                                              // utilization.  The bits correspond to the taps
00065                                              // used in output calculation.  Others may be 
00066                                              // safely ignored.
00067   }
00068     
00069   //! return current output value
00070   unsigned char output (){
00071     return fast_output_map (d_state);
00072   }
00073 
00074   //! clock LFSR; advance to next state.
00075   void clk (){
00076     if (d_state & 0x1)
00077       d_state = ((d_state ^ MASK) >> 1) | 0x8000;
00078     else
00079       d_state = d_state >> 1;
00080   }
00081 
00082   //! return current output value and advance to next state
00083   unsigned char output_and_clk (){
00084     unsigned char r = output ();
00085     clk ();
00086     return r;
00087   }
00088 
00089   unsigned int              d_state;
00090 
00091   static const unsigned int PRELOAD_VALUE = 0x018f; /* 0xf180 bit reversed */
00092   static const unsigned int MASK = 0xa638;
00093   static unsigned char      s_output_map[1 << 14];
00094   static bool               s_output_map_initialized_p;
00095 };
00096 
00097 #endif /* _ATSC_RANDOMIZER_H_ */