Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gr-atsc / src / lib / GrAtscRandomizer.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 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 #include <GrAtscRandomizer.h>
24
25 // typedefs for fundamental i/o types
26
27 typedef atsc_mpeg_packet                iType;
28 typedef atsc_mpeg_packet_no_sync        oType;
29
30 static const int NUMBER_OF_OUTPUTS = 1; // # of output streams (almost always one)
31
32
33 GrAtscRandomizer::GrAtscRandomizer ()
34   : VrHistoryProc<iType,oType> (NUMBER_OF_OUTPUTS),
35     field2 (false), segno (0)
36 {
37   // 1 + number of extra input elements at which we look.  This is
38   // used by the superclass's forecast routine to get us the correct
39   // range on our inputs.
40   // We're one-to-one input-to-output so set it to 1.
41   history = 1;  
42
43   // any other init here.
44 }
45
46 GrAtscRandomizer::~GrAtscRandomizer ()
47 {
48   // Anything that isn't automatically cleaned up...
49 }
50
51 /*
52  * This is the real work horse.  In general this interface can handle
53  * multiple streams of input and output, but we almost always
54  * use a single input and output stream.
55  */
56
57 int 
58 GrAtscRandomizer::work (VrSampleRange output, void *ao[],
59                         VrSampleRange inputs[], void *ai[])
60 {
61   // If we have state that persists across invocations (e.g., we have
62   // instance variables that we modify), we must use the sync method
63   // to indicate to the scheduler that our output must be computed in
64   // order.  This doesn't keep other things from being run in
65   // parallel, it just means that at any given time, there is only a
66   // single thread working this code, and that the scheduler will
67   // ensure that we are asked to produce output that is contiguous and
68   // that will be presented to us in order of increasing time.
69
70   // We have state, the current contents of the LFSR in the randomizer, hence
71   // we must use sync.
72
73   sync (output.index);
74
75   // construct some nicer i/o pointers to work with.
76
77   iType *in  = ((iType **) ai)[0];
78   oType *out = ((oType **) ao)[0];
79
80
81   // We must produce output.size units of output.
82
83   for (unsigned int i = 0; i < output.size; i++){
84
85     // initialize plinfo for downstream
86     //
87     // We do this here because the randomizer is effectively
88     // the head of the tx processing chain
89     //
90     out[i].pli.set_regular_seg (field2, segno);
91     segno++;
92     if (segno == 312){
93       segno = 0;
94       field2 = !field2;
95     }
96
97     assert ((in[i].data[1] & MPEG_TRANSPORT_ERROR_BIT) == 0);
98
99     if (out[i].pli.first_regular_seg_p ())
100       rand.reset ();
101
102     rand.randomize (out[i], in[i]);
103   }
104
105   // Return the number of units we produced.
106   // Note that for all intents and purposes, it is an error to
107   // produce less than you are asked for.
108
109   return output.size;   
110 }