Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gr-atsc / src / lib / GrAtscTrellisEncoder.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 <GrAtscTrellisEncoder.h>
24
25 // typedefs for fundamental i/o types
26
27 typedef atsc_mpeg_packet_rs_encoded     iType;
28 typedef atsc_data_segment               oType;
29
30 static const int NUMBER_OF_OUTPUTS = 1; // # of output streams (almost always one)
31
32
33 GrAtscTrellisEncoder::GrAtscTrellisEncoder ()
34   : VrHistoryProc<iType,oType> (NUMBER_OF_OUTPUTS), last_start(-1)
35 {
36   // 1 + number of extra input elements at which we look.  This is
37   // used by the superclass's forecast routine to get us the correct
38   // range on our inputs.
39   //
40   // We need our input to be aligned on a 12-segment boundary,
41   // to ensure satisfaction, ask for 11 more
42   history = 1 + (atsci_trellis_encoder::NCODERS - 1);
43
44   // any other init here.
45
46   // Let the bottom end know we must produce output in multiples of 12 segments.  
47   setOutputSize (atsci_trellis_encoder::NCODERS);
48 }
49
50 GrAtscTrellisEncoder::~GrAtscTrellisEncoder ()
51 {
52   // Anything that isn't automatically cleaned up...
53 }
54
55 /*
56  * This is the real work horse.  In general this interface can handle
57  * multiple streams of input and output, but we almost always
58  * use a single input and output stream.
59  */
60
61 int 
62 GrAtscTrellisEncoder::work (VrSampleRange output, void *ao[],
63                             VrSampleRange inputs[], void *ai[])
64 {
65   // If we have state that persists across invocations (e.g., we have
66   // instance variables that we modify), we must use the sync method
67   // to indicate to the scheduler that our output must be computed in
68   // order.  This doesn't keep other things from being run in
69   // parallel, it just means that at any given time, there is only a
70   // single thread working this code, and that the scheduler will
71   // ensure that we are asked to produce output that is contiguous and
72   // that will be presented to us in order of increasing time.
73
74   // We have state, the current state of the encoders, hence
75   // we must use sync.
76
77   sync (output.index);
78
79   // construct some nicer i/o pointers to work with.
80
81   iType *in  = ((iType **) ai)[0];
82   oType *out = ((oType **) ao)[0];
83
84
85 #if 0
86   cerr << "@@@ GrAtscTrellisEncoder: output.index = " << output.index
87        << " output.size = " << output.size
88        << " sum = " << output.index + output.size
89        << " \t[in  = " << in << "]"
90        << endl;
91 #endif  
92
93   assert (output.size % atsci_trellis_encoder::NCODERS == 0);
94
95
96   // find the first mod 12 boundary to begin decoding
97   int start;
98   for (start = 0; start < atsci_trellis_encoder::NCODERS; start++){
99     plinfo::sanity_check (in[start].pli);
100     assert (in[start].pli.regular_seg_p ());
101     if ((in[start].pli.segno () % atsci_trellis_encoder::NCODERS) == 0)
102       break;
103   }
104
105   if (start == atsci_trellis_encoder::NCODERS){
106     // we didn't find a mod 12 boundary.  There's some kind of problem
107     // upstream of us (not yet sync'd??)
108     cerr << "!!!GrAtscTrellisEncoder: no mod-12 boundary found\7\n";
109     start = 0;
110   }
111   else if (start != last_start){
112     cerr << "GrAtscTrellisEncoder: new starting offset = " << start
113          << " output.index = " << output.index << endl;
114     last_start = start;
115   }
116
117
118   // FIXME paranoid check for problem
119   for (unsigned int i = 0; i < output.size; i++){
120     plinfo::sanity_check (in[i + start].pli);
121   }
122
123   // We must produce output.size units of output.
124
125   for (unsigned int i = 0; i < output.size; i += atsci_trellis_encoder::NCODERS){
126     // primitive does 12 segments at a time.
127     // pipeline info is handled in the primitive.
128     encoder.encode (&out[i], &in[i + start]);
129   }
130
131 #if 0
132   // FIXME paranoid check for problem
133   for (unsigned int i = 0; i < output.size; i++){
134     plinfo::sanity_check (out[i].pli);
135     assert (out[i].pli.regular_seg_p ());
136   }
137 #endif
138
139   // Return the number of units we produced.
140   // Note that for all intents and purposes, it is an error to
141   // produce less than you are asked for.
142
143   return output.size;   
144 }