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