3 * Copyright 2006 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 2, 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.
28 #include <atsc_field_sync_demux.h>
29 #include <gr_io_signature.h>
30 #include <atsc_types.h>
31 #include <atsc_consts.h>
32 #include <atsci_syminfo.h>
39 static const int DEC = ATSC_DATA_SEGMENT_LENGTH; // nominal decimation factor
42 atsc_field_sync_demux_sptr
43 atsc_make_field_sync_demux()
45 return atsc_field_sync_demux_sptr(new atsc_field_sync_demux());
48 atsc_field_sync_demux::atsc_field_sync_demux()
49 : gr_sync_decimator("atsc_field_sync_demux",
50 gr_make_io_signature(2, 2, sizeof(float)),
51 gr_make_io_signature(1, 1, sizeof(atsc_soft_data_segment)),DEC),
52 d_locked(false), d_in_field2(true), d_segment_number(0),
53 d_next_input(0), d_lost_index(0)
59 tag_is_seg_sync_or_field_sync (atsc::syminfo tag)
61 return tag.symbol_num == 0 && tag.valid;
65 atsc_field_sync_demux::forecast (int noutput_items, gr_vector_int &ninput_items_required)
67 unsigned ninputs = ninput_items_required.size();
68 for (unsigned i = 0; i < ninputs; i++) {
69 ninput_items_required[i] = noutput_items * DEC + 2 * DEC - 1;
71 inputs0_index = d_next_input;
72 inputs0_size = noutput_items * DEC + 2 * DEC - 1;
78 atsc_field_sync_demux::work (int noutput_items,
79 gr_vector_const_void_star &input_items,
80 gr_vector_void_star &output_items)
82 float *in = (float *) input_items[0];
83 atsc::syminfo *input_tags = (atsc::syminfo *) input_items[1];
84 atsc_soft_data_segment *out = (atsc_soft_data_segment *) output_items[0];
86 assert(sizeof(float) == sizeof(atsc::syminfo));
88 int ii = 0; // input index
91 if (!tag_is_seg_sync_or_field_sync (input_tags[0])){ // No ...
95 d_lost_index = inputs0_index + ii;
96 cerr << "atsc_field_sync_demux: lost sync at "
97 << d_lost_index << endl;
100 // ... search for beginning of a field sync
102 // cerr << "atsc_field_sync_demux: searching for sync at "
103 // << inputs0_index + ii << endl;
105 for (ii = 1; ii < inputs0_size; ii++){
106 if (atsc::tag_is_start_field_sync (input_tags[ii])){
111 if (atsc::tag_is_start_field_sync_1 (input_tags[ii]))
113 else if (atsc::tag_is_start_field_sync_2 (input_tags[ii]))
118 cerr << "atsc_field_sync_demux: synced (" << str << ") at "
119 << inputs0_index + ii
120 << " [delta = " << inputs0_index + ii - d_lost_index
123 d_next_input += ii; // update for forecast
124 return 0; // no work completed so far
127 // no non-NORMAL tag found
128 d_next_input += ii; // update for forecast
129 return 0; // no work completed so far
132 // We are in sync. Produce output...
134 int k = 0; // output index
136 while (k < noutput_items){
138 if (inputs0_size - ii < ATSC_DATA_SEGMENT_LENGTH){
139 // We're out of input data.
140 cerr << "atsc_field_sync_demux: ran out of input data\n";
141 d_next_input += ii; // update for forecast
142 return k; // return amount of work completed so far
145 if (!tag_is_seg_sync_or_field_sync (input_tags[ii])){
147 cerr << "atsc_field_sync_demux: lost sync at "
148 << inputs0_index + ii << endl;
150 d_next_input += ii; // update for forecast
151 return k; // return amount of work completed so far
154 if (atsc::tag_is_start_field_sync_1 (input_tags[ii])){
156 d_segment_number = 0;
157 ii += ATSC_DATA_SEGMENT_LENGTH; // skip over field sync
161 if (atsc::tag_is_start_field_sync_2 (input_tags[ii])){
163 d_segment_number = 0;
164 ii += ATSC_DATA_SEGMENT_LENGTH; // skip over field sync
168 if (d_segment_number >= ATSC_DSEGS_PER_FIELD){
169 // something's wrong...
170 cerr << "atsc_field_sync_demux: segment number overflow\n";
171 d_segment_number = 0;
174 out[k].pli.set_regular_seg (d_in_field2, d_segment_number++);
175 for (int jj = 0; jj < ATSC_DATA_SEGMENT_LENGTH; jj++)
176 out[k].data[jj] = in[ii + jj];
177 ii += ATSC_DATA_SEGMENT_LENGTH;
181 d_next_input += ii; // update for forecast
182 return k; // return amount of work completed