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