Houston, we have a trunk.
[debian/gnuradio] / gr-atsc / src / lib / GrAtscFieldSyncMux.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 <GrAtscFieldSyncMux.h>
24 #include <atsci_pnXXX.h>
25
26
27 // typedefs for fundamental i/o types
28
29 typedef atsc_data_segment       iType;
30 typedef atsc_data_segment       oType;
31
32 static const int NUMBER_OF_OUTPUTS = 1; // # of output streams (almost always one)
33
34 static const int N_SAVED_SYMBOLS = GrAtscFieldSyncMux::N_SAVED_SYMBOLS;
35
36 static void
37 init_field_sync_common (unsigned char *p, int mask,
38                         const unsigned char saved_symbols[N_SAVED_SYMBOLS])
39 {
40   static const unsigned char bin_map[2] = { 1, 6 };  // map binary values to 1 of 8 levels
41
42   int  i = 0;
43
44   p[i++] = bin_map[1];                  // data segment sync pulse
45   p[i++] = bin_map[0];
46   p[i++] = bin_map[0];
47   p[i++] = bin_map[1];
48
49   for (int j = 0; j < 511; j++)         // PN511
50     p[i++] = bin_map[atsc_pn511[j]];
51
52   for (int j = 0; j < 63; j++)          // PN63
53     p[i++] = bin_map[atsc_pn63[j]];
54
55   for (int j = 0; j < 63; j++)          // PN63, toggled on field 2
56     p[i++] = bin_map[atsc_pn63[j] ^ mask];
57   
58   for (int j = 0; j < 63; j++)          // PN63
59     p[i++] = bin_map[atsc_pn63[j]];
60
61   p[i++] = bin_map[0];                  // 24 bits of VSB8 mode identifiera
62   p[i++] = bin_map[0];
63   p[i++] = bin_map[0];
64   p[i++] = bin_map[0];
65
66   p[i++] = bin_map[1];
67   p[i++] = bin_map[0];
68   p[i++] = bin_map[1];
69   p[i++] = bin_map[0];
70
71   p[i++] = bin_map[0];
72   p[i++] = bin_map[1];
73   p[i++] = bin_map[0];
74   p[i++] = bin_map[1];
75
76   p[i++] = bin_map[1];
77   p[i++] = bin_map[1];
78   p[i++] = bin_map[1];
79   p[i++] = bin_map[1];
80
81   p[i++] = bin_map[0];
82   p[i++] = bin_map[1];
83   p[i++] = bin_map[0];
84   p[i++] = bin_map[1];
85
86   p[i++] = bin_map[1];
87   p[i++] = bin_map[0];
88   p[i++] = bin_map[1];
89   p[i++] = bin_map[0];
90
91
92   for (int j = 0; j < 92; j++)          // 92 more bits
93     p[i++] = bin_map[atsc_pn63[j % 63]];
94
95   // now copy the last 12 symbols of the previous segment
96
97   for (int j = 0; j < N_SAVED_SYMBOLS; j++)
98     p[i++] = saved_symbols[j];
99
100   assert (i == ATSC_DATA_SEGMENT_LENGTH);
101 }
102
103 inline static void
104 init_field_sync_1 (atsc_data_segment *s,
105                    const unsigned char saved_symbols[N_SAVED_SYMBOLS])
106 {
107   init_field_sync_common (&s->data[0], 0, saved_symbols);
108 }
109
110 inline static void
111 init_field_sync_2 (atsc_data_segment *s,
112                    const unsigned char saved_symbols[N_SAVED_SYMBOLS])
113
114 {
115   init_field_sync_common (&s->data[0], 1, saved_symbols);
116 }
117
118 static void
119 save_last_symbols (unsigned char saved_symbols[N_SAVED_SYMBOLS],
120                    const atsc_data_segment &seg)
121 {
122   for (int i = 0; i < N_SAVED_SYMBOLS; i++)
123     saved_symbols[i] = seg.data[i + ATSC_DATA_SEGMENT_LENGTH - N_SAVED_SYMBOLS];
124 }
125
126
127 inline static bool 
128 last_regular_seg_p (const plinfo &pli)
129 {
130   return pli.regular_seg_p () && (pli.segno () == ATSC_DSEGS_PER_FIELD - 1);
131 }
132
133
134 GrAtscFieldSyncMux::GrAtscFieldSyncMux ()
135   : VrHistoryProc<iType,oType> (NUMBER_OF_OUTPUTS),
136     d_current_index (0), d_already_output_field_sync (false)
137 {
138   // 1 + number of extra input elements at which we look.  This is
139   // used by the superclass's forecast routine to get us the correct
140   // range on our inputs.
141   history = 1;  
142
143   // any other init here.
144 }
145
146 GrAtscFieldSyncMux::~GrAtscFieldSyncMux ()
147 {
148   // Anything that isn't automatically cleaned up...
149 }
150
151 void
152 GrAtscFieldSyncMux::pre_initialize ()
153 {
154   // we jack our output sampling frequency up to account for inserted field syncs
155   setSamplingFrequency (getInputSamplingFrequencyN (0) * 313./312.);
156 }
157
158 /*
159  * we need a non-standard version of forecast because our output isn't
160  * exactly 1:1 with our input.
161  */
162
163 int 
164 GrAtscFieldSyncMux::forecast (VrSampleRange output, VrSampleRange inputs[])
165 {
166   for(unsigned int i = 0; i < numberInputs; i++) {
167     inputs[i].index = d_current_index;
168     inputs[i].size = output.size;
169   }
170   return 0;
171 }
172
173 /*
174  * This is the real work horse.  In general this interface can handle
175  * multiple streams of input and output, but we almost always
176  * use a single input and output stream.
177  */
178 int 
179 GrAtscFieldSyncMux::work (VrSampleRange output, void *ao[],
180                           VrSampleRange inputs[], void *ai[])
181 {
182   // If we have state that persists across invocations (e.g., we have
183   // instance variables that we modify), we must use the sync method
184   // to indicate to the scheduler that our output must be computed in
185   // order.  This doesn't keep other things from being run in
186   // parallel, it just means that at any given time, there is only a
187   // single thread working this code, and that the scheduler will
188   // ensure that we are asked to produce output that is contiguous and
189   // that will be presented to us in order of increasing time.
190
191   // We have state, hence we must use sync.
192
193   sync (output.index);
194
195   // construct some nicer i/o pointers to work with.
196
197   iType *in  = ((iType **) ai)[0];
198   oType *out = ((oType **) ao)[0];
199
200
201   // We must produce output.size units of output.
202
203   unsigned int index = 0;
204   for (unsigned int outdex = 0; outdex < output.size; outdex++){
205
206     assert (in[index].pli.regular_seg_p ());
207     
208     if (!in[index].pli.first_regular_seg_p ()){
209       out[outdex] = in[index];                  // just copy in to out
210
211       if (last_regular_seg_p (in[index].pli))
212         save_last_symbols (d_saved_symbols, in[index]);
213
214       index++;
215     }
216     else {                                      // first_regular_seg_p
217       if (!d_already_output_field_sync){
218         // write out field sync...
219         atsc_data_segment       field_sync;
220
221         if (in[index].pli.in_field1_p ())
222           init_field_sync_1 (&field_sync, d_saved_symbols);
223         else
224           init_field_sync_2 (&field_sync, d_saved_symbols);
225
226         // note that index doesn't advance in this branch
227         out[outdex] = field_sync;
228         d_already_output_field_sync = true;
229       }
230       else {
231         // already output field sync, now output first regular segment
232         out[outdex] = in[index];
233         index++;
234         d_already_output_field_sync = false;
235       }
236     }
237   }
238
239   d_current_index += index;
240
241   // Return the number of units we produced.
242   // Note that for all intents and purposes, it is an error to
243   // produce less than you are asked for.
244
245   return output.size;   
246 }