9db04012166c8bd5dedc2680a0d9c303942ed142
[debian/gnuradio] / gr-atsc / src / lib / atsc_viterbi_decoder.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <atsc_viterbi_decoder.h>
28 #include <gr_io_signature.h>
29 #include <atsc_consts.h>
30 #include <iostream>
31
32 using std::cerr;
33 using std::endl;
34
35 atsc_viterbi_decoder_sptr
36 atsc_make_viterbi_decoder()
37 {
38   return atsc_viterbi_decoder_sptr(new atsc_viterbi_decoder());
39 }
40
41 atsc_viterbi_decoder::atsc_viterbi_decoder()
42   : gr_sync_block("atsc_viterbi_decoder",
43                   gr_make_io_signature(1, 1, sizeof(atsc_soft_data_segment)),
44                   gr_make_io_signature(1, 1, sizeof(atsc_mpeg_packet_rs_encoded))),
45                   last_start(-1)
46 {
47   set_output_multiple(atsci_viterbi_decoder::NCODERS);
48   reset();
49 }
50
51 int
52 atsc_viterbi_decoder::work (int noutput_items,
53                        gr_vector_const_void_star &input_items,
54                        gr_vector_void_star &output_items)
55 {
56   const atsc_soft_data_segment *in = (const atsc_soft_data_segment *) input_items[0];
57   atsc_mpeg_packet_rs_encoded *out = (atsc_mpeg_packet_rs_encoded *) output_items[0];
58
59   assert (noutput_items % atsci_viterbi_decoder::NCODERS == 0);
60
61   // find the first mod 12 boundary to begin decoding
62   int start;
63   for (start = 0; start < atsci_viterbi_decoder::NCODERS; start++){
64     assert (in[start].pli.regular_seg_p ());
65     if ((in[start].pli.segno () % atsci_viterbi_decoder::NCODERS) == 0)
66       break;
67   }
68
69   if (start == atsci_viterbi_decoder::NCODERS){
70     // we didn't find a mod 12 boundary.  There's some kind of problem
71     // upstream of us (not yet sync'd??)
72     cerr << "!!!atsc_viterbi_decoder: no mod-12 boundary found\7\n";
73     start = 0;
74   }
75   else if (start != last_start){
76     cerr << "atsc_viterbi_decoder: new starting offset = " << start
77          << endl;
78     last_start = start;
79   }
80
81   for (int i = 0; i < atsci_viterbi_decoder::NCODERS; i += atsci_viterbi_decoder::NCODERS){
82     d_viterbi_decoder.decode(&out[i], &in[i + start]);
83   }
84   return atsci_viterbi_decoder::NCODERS;
85 }
86