Houston, we have a trunk.
[debian/gnuradio] / gr-atsc / src / lib / atsci_reed_solomon.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 2, 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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <atsci_reed_solomon.h>
24 #include <assert.h>
25
26 extern "C" {
27 #include "rs.h"
28 }
29
30 static const int rs_init_symsize =     8;
31 static const int rs_init_gfpoly  = 0x11d;
32 static const int rs_init_fcr     =     0;       // first consecutive root
33 static const int rs_init_prim    =     1;       // primitive is 1 (alpha)
34 static const int rs_init_nroots  =    20;
35
36 static const int N = (1 << rs_init_symsize) - 1;        // 255
37 static const int K = N - rs_init_nroots;                // 235
38
39 static const int amount_of_pad   = N - ATSC_MPEG_RS_ENCODED_LENGTH;       // 48
40
41 atsci_reed_solomon::atsci_reed_solomon ()
42 {
43   d_rs = init_rs_char (rs_init_symsize, rs_init_gfpoly,
44                        rs_init_fcr, rs_init_prim, rs_init_nroots);
45
46   assert (d_rs != 0);
47 }
48
49 atsci_reed_solomon::~atsci_reed_solomon ()
50 {
51   if (d_rs)
52     free_rs_char (d_rs);
53   d_rs = 0;
54 }
55
56 void
57 atsci_reed_solomon::encode (atsc_mpeg_packet_rs_encoded &out, const atsc_mpeg_packet_no_sync &in)
58 {
59   unsigned char tmp[K];
60
61   assert ((int)(amount_of_pad + sizeof (in.data)) == K);
62   
63   // add missing prefix zero padding to message
64   memset (tmp, 0, amount_of_pad);
65   memcpy (&tmp[amount_of_pad], in.data, sizeof (in.data));
66
67   // copy message portion to output packet
68   memcpy (out.data, in.data, sizeof (in.data));
69
70   // now compute parity bytes and add them to tail end of output packet
71   encode_rs_char (d_rs, tmp, &out.data[sizeof (in.data)]);
72 }
73
74 int
75 atsci_reed_solomon::decode (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet_rs_encoded &in)
76 {
77   unsigned char tmp[N];
78   int           ncorrections;
79
80   assert ((int)(amount_of_pad + sizeof (in.data)) == N);
81   
82   // add missing prefix zero padding to message
83   memset (tmp, 0, amount_of_pad);
84   memcpy (&tmp[amount_of_pad], in.data, sizeof (in.data));
85
86   // correct message... 
87   ncorrections = decode_rs_char (d_rs, tmp, 0, 0);
88   
89   // copy corrected message to output, skipping prefix zero padding
90   memcpy (out.data, &tmp[amount_of_pad], sizeof (out.data));
91
92   return ncorrections;
93 }