summaryrefslogtreecommitdiff
path: root/gr-atsc/src/lib/GrAtscFieldSyncMux.cc
blob: 42921952ae744d4206937d35337d03c31cbafa62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- c++ -*- */
/*
 * Copyright 2002 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <GrAtscFieldSyncMux.h>
#include <atsci_pnXXX.h>


// typedefs for fundamental i/o types

typedef atsc_data_segment	iType;
typedef atsc_data_segment	oType;

static const int NUMBER_OF_OUTPUTS = 1;	// # of output streams (almost always one)

static const int N_SAVED_SYMBOLS = GrAtscFieldSyncMux::N_SAVED_SYMBOLS;

static void
init_field_sync_common (unsigned char *p, int mask,
			const unsigned char saved_symbols[N_SAVED_SYMBOLS])
{
  static const unsigned char bin_map[2] = { 1, 6 };  // map binary values to 1 of 8 levels

  int  i = 0;

  p[i++] = bin_map[1];			// data segment sync pulse
  p[i++] = bin_map[0];
  p[i++] = bin_map[0];
  p[i++] = bin_map[1];

  for (int j = 0; j < 511; j++)		// PN511
    p[i++] = bin_map[atsc_pn511[j]];

  for (int j = 0; j < 63; j++)		// PN63
    p[i++] = bin_map[atsc_pn63[j]];

  for (int j = 0; j < 63; j++)		// PN63, toggled on field 2
    p[i++] = bin_map[atsc_pn63[j] ^ mask];
  
  for (int j = 0; j < 63; j++)		// PN63
    p[i++] = bin_map[atsc_pn63[j]];

  p[i++] = bin_map[0];			// 24 bits of VSB8 mode identifiera
  p[i++] = bin_map[0];
  p[i++] = bin_map[0];
  p[i++] = bin_map[0];

  p[i++] = bin_map[1];
  p[i++] = bin_map[0];
  p[i++] = bin_map[1];
  p[i++] = bin_map[0];

  p[i++] = bin_map[0];
  p[i++] = bin_map[1];
  p[i++] = bin_map[0];
  p[i++] = bin_map[1];

  p[i++] = bin_map[1];
  p[i++] = bin_map[1];
  p[i++] = bin_map[1];
  p[i++] = bin_map[1];

  p[i++] = bin_map[0];
  p[i++] = bin_map[1];
  p[i++] = bin_map[0];
  p[i++] = bin_map[1];

  p[i++] = bin_map[1];
  p[i++] = bin_map[0];
  p[i++] = bin_map[1];
  p[i++] = bin_map[0];


  for (int j = 0; j < 92; j++)		// 92 more bits
    p[i++] = bin_map[atsc_pn63[j % 63]];

  // now copy the last 12 symbols of the previous segment

  for (int j = 0; j < N_SAVED_SYMBOLS; j++)
    p[i++] = saved_symbols[j];

  assert (i == ATSC_DATA_SEGMENT_LENGTH);
}

inline static void
init_field_sync_1 (atsc_data_segment *s,
		   const unsigned char saved_symbols[N_SAVED_SYMBOLS])
{
  init_field_sync_common (&s->data[0], 0, saved_symbols);
}

inline static void
init_field_sync_2 (atsc_data_segment *s,
		   const unsigned char saved_symbols[N_SAVED_SYMBOLS])

{
  init_field_sync_common (&s->data[0], 1, saved_symbols);
}

static void
save_last_symbols (unsigned char saved_symbols[N_SAVED_SYMBOLS],
		   const atsc_data_segment &seg)
{
  for (int i = 0; i < N_SAVED_SYMBOLS; i++)
    saved_symbols[i] = seg.data[i + ATSC_DATA_SEGMENT_LENGTH - N_SAVED_SYMBOLS];
}


inline static bool 
last_regular_seg_p (const plinfo &pli)
{
  return pli.regular_seg_p () && (pli.segno () == ATSC_DSEGS_PER_FIELD - 1);
}


GrAtscFieldSyncMux::GrAtscFieldSyncMux ()
  : VrHistoryProc<iType,oType> (NUMBER_OF_OUTPUTS),
    d_current_index (0), d_already_output_field_sync (false)
{
  // 1 + number of extra input elements at which we look.  This is
  // used by the superclass's forecast routine to get us the correct
  // range on our inputs.
  history = 1;	

  // any other init here.
}

GrAtscFieldSyncMux::~GrAtscFieldSyncMux ()
{
  // Anything that isn't automatically cleaned up...
}

void
GrAtscFieldSyncMux::pre_initialize ()
{
  // we jack our output sampling frequency up to account for inserted field syncs
  setSamplingFrequency (getInputSamplingFrequencyN (0) * 313./312.);
}

/*
 * we need a non-standard version of forecast because our output isn't
 * exactly 1:1 with our input.
 */

int 
GrAtscFieldSyncMux::forecast (VrSampleRange output, VrSampleRange inputs[])
{
  for(unsigned int i = 0; i < numberInputs; i++) {
    inputs[i].index = d_current_index;
    inputs[i].size = output.size;
  }
  return 0;
}

/*
 * This is the real work horse.  In general this interface can handle
 * multiple streams of input and output, but we almost always
 * use a single input and output stream.
 */
int 
GrAtscFieldSyncMux::work (VrSampleRange output, void *ao[],
			  VrSampleRange inputs[], void *ai[])
{
  // If we have state that persists across invocations (e.g., we have
  // instance variables that we modify), we must use the sync method
  // to indicate to the scheduler that our output must be computed in
  // order.  This doesn't keep other things from being run in
  // parallel, it just means that at any given time, there is only a
  // single thread working this code, and that the scheduler will
  // ensure that we are asked to produce output that is contiguous and
  // that will be presented to us in order of increasing time.

  // We have state, hence we must use sync.

  sync (output.index);

  // construct some nicer i/o pointers to work with.

  iType *in  = ((iType **) ai)[0];
  oType *out = ((oType **) ao)[0];


  // We must produce output.size units of output.

  unsigned int index = 0;
  for (unsigned int outdex = 0; outdex < output.size; outdex++){

    assert (in[index].pli.regular_seg_p ());
    
    if (!in[index].pli.first_regular_seg_p ()){
      out[outdex] = in[index];			// just copy in to out

      if (last_regular_seg_p (in[index].pli))
	save_last_symbols (d_saved_symbols, in[index]);

      index++;
    }
    else {					// first_regular_seg_p
      if (!d_already_output_field_sync){
	// write out field sync...
	atsc_data_segment	field_sync;

	if (in[index].pli.in_field1_p ())
	  init_field_sync_1 (&field_sync, d_saved_symbols);
	else
	  init_field_sync_2 (&field_sync, d_saved_symbols);

	// note that index doesn't advance in this branch
	out[outdex] = field_sync;
	d_already_output_field_sync = true;
      }
      else {
	// already output field sync, now output first regular segment
	out[outdex] = in[index];
	index++;
	d_already_output_field_sync = false;
      }
    }
  }

  d_current_index += index;

  // Return the number of units we produced.
  // Note that for all intents and purposes, it is an error to
  // produce less than you are asked for.

  return output.size;	
}