3 * Copyright 2002 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
24 #include <GrAtscFieldSyncDemux.h>
25 #include <atsc_consts.h>
26 #include <atsc_types.h>
27 #include <atsci_syminfo.h>
33 static const int DEC = ATSC_DATA_SEGMENT_LENGTH; // nominal decimation factor
35 GrAtscFieldSyncDemux::GrAtscFieldSyncDemux ()
36 : VrDecimatingSigProc<float,atsc_soft_data_segment> (1, DEC),
37 d_locked (false), d_in_field2(true), d_segment_number(0), d_next_input(0),
40 history = 2 * ATSC_DATA_SEGMENT_LENGTH; // spare input samples in case we need them.
43 GrAtscFieldSyncDemux::~GrAtscFieldSyncDemux ()
48 GrAtscFieldSyncDemux::forecast (VrSampleRange output,
49 VrSampleRange inputs[]) {
50 /* dec:1 ratio with history */
52 assert (numberInputs == 2);
54 for (unsigned int i = 0; i < numberInputs; i++) {
55 inputs[i].index = d_next_input;
56 inputs[i].size = output.size * decimation + history - 1;
62 tag_is_seg_sync_or_field_sync (atsc::syminfo tag)
64 return tag.symbol_num == 0 && tag.valid;
68 GrAtscFieldSyncDemux::work (VrSampleRange output, void *ao[],
69 VrSampleRange inputs[], void *ai[])
71 float *input_samples = (float *) ai[0];
72 atsc::syminfo *input_tags = (atsc::syminfo *) ai[1];
73 atsc_soft_data_segment *out = ((atsc_soft_data_segment **)ao)[0];
77 unsigned int ii = 0; // input index
80 if (!tag_is_seg_sync_or_field_sync (input_tags[0])){ // No ...
84 d_lost_index = inputs[0].index + ii;
85 cerr << "GrAtscFieldSyncDemux: lost sync at "
86 << d_lost_index << endl;
89 // ... search for beginning of a field sync
91 // cerr << "GrAtscFieldSyncDemux: searching for sync at "
92 // << inputs[0].index + ii << endl;
94 for (ii = 1; ii < inputs[0].size; ii++){
95 if (atsc::tag_is_start_field_sync (input_tags[ii])){
100 if (atsc::tag_is_start_field_sync_1 (input_tags[ii]))
102 else if (atsc::tag_is_start_field_sync_2 (input_tags[ii]))
107 cerr << "GrAtscFieldSyncDemux: synced (" << str << ") at "
108 << inputs[0].index + ii
109 << " [delta = " << inputs[0].index + ii - d_lost_index
112 d_next_input += ii; // update for forecast
113 return 0; // no work completed so far
116 // no non-NORMAL tag found
117 d_next_input += ii; // update for forecast
118 return 0; // no work completed so far
121 // We are in sync. Produce output...
123 unsigned int k = 0; // output index
125 while (k < output.size){
127 if (inputs[0].size - ii < (unsigned) ATSC_DATA_SEGMENT_LENGTH){
128 // We're out of input data.
129 cerr << "GrAtscFieldSyncDemux: ran out of input data\n";
130 d_next_input += ii; // update for forecast
131 return k; // return amount of work completed so far
134 if (!tag_is_seg_sync_or_field_sync (input_tags[ii])){
136 // cerr << "GrAtscFieldSyncDemux: lost sync at "
137 // << inputs[0].index + ii << endl;
139 d_next_input += ii; // update for forecast
140 return k; // return amount of work completed so far
143 if (atsc::tag_is_start_field_sync_1 (input_tags[ii])){
145 d_segment_number = 0;
146 ii += ATSC_DATA_SEGMENT_LENGTH; // skip over field sync
150 if (atsc::tag_is_start_field_sync_2 (input_tags[ii])){
152 d_segment_number = 0;
153 ii += ATSC_DATA_SEGMENT_LENGTH; // skip over field sync
157 if (d_segment_number >= ATSC_DSEGS_PER_FIELD){
158 // something's wrong...
159 cerr << "GrAtscFieldSyncDemux: segment number overflow\n";
160 d_segment_number = 0;
163 out[k].pli.set_regular_seg (d_in_field2, d_segment_number++);
164 for (int jj = 0; jj < ATSC_DATA_SEGMENT_LENGTH; jj++)
165 out[k].data[jj] = input_samples[ii + jj];
166 ii += ATSC_DATA_SEGMENT_LENGTH;
170 d_next_input += ii; // update for forecast
171 return k; // return amount of work completed