File: reed_solomon_decode_bb_impl.cc

package info (click to toggle)
gr-dab 0.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,280 kB
  • sloc: python: 14,976; cpp: 6,738; ansic: 547; makefile: 17; sh: 11
file content (148 lines) | stat: -rw-r--r-- 4,817 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- c++ -*- */
/* 
 * Reed-Solomon decoder for DAB+
 * Copyright 2002 Phil Karn, KA9Q
 * May be used under the terms of the GNU General Public License (GPL)
 *
 * Rewritten into a GNU Radio block for gr-dab
 * Copyright 2017 Moritz Luca Schmid, Communications Engineering Lab (CEL) / Karlsruhe Institute of Technology (KIT).

 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdexcept>
#include <stdio.h>
#include <sstream>
#include <boost/format.hpp>
#include <gnuradio/io_signature.h>
#include "reed_solomon_decode_bb_impl.h"

using namespace boost;

namespace gr {
  namespace dab {

    reed_solomon_decode_bb::sptr
    reed_solomon_decode_bb::make(int bit_rate_n)
    {
      return gnuradio::get_initial_sptr
              (new reed_solomon_decode_bb_impl(bit_rate_n));
    }

    /*
     * The private constructor
     */
    reed_solomon_decode_bb_impl::reed_solomon_decode_bb_impl(int bit_rate_n)
            : gr::block("reed_solomon_decode_bb",
                        gr::io_signature::make(1, 1, sizeof(unsigned char)),
                        gr::io_signature::make(1, 1, sizeof(unsigned char))),
              d_bit_rate_n(bit_rate_n)
    {
      rs_handle = init_rs_char(8, 0x11D, 0, 1, 10, 135);
      if (!rs_handle) {
        GR_LOG_DEBUG(d_logger, "RS init failed");
      } else {
        GR_LOG_DEBUG(d_logger, "RS init succeeded");
      }
      d_superframe_size = bit_rate_n * 120;
      d_superframe_size_rs = bit_rate_n * 110;
      set_output_multiple(d_superframe_size_rs);
      d_corrected_errors = 0;
    }

    /*
     * Our virtual destructor.
     */
    reed_solomon_decode_bb_impl::~reed_solomon_decode_bb_impl()
    {
      free_rs_char(rs_handle);
    }

    void reed_solomon_decode_bb_impl::DecodeSuperframe(uint8_t *sf, size_t sf_len)
    {
//	// insert errors for test
//	sf[0] ^= 0xFF;
//	sf[10] ^= 0xFF;
//	sf[20] ^= 0xFF;

      int subch_index = sf_len / 120;
      int total_corr_count = 0;
      bool uncorr_errors = false;

      // process all RS packets
      for (int i = 0; i < subch_index; i++) {
        for (int pos = 0; pos < 120; pos++) {
          rs_packet[pos] = sf[pos * subch_index + i];
        }
        // detect errors
        int corr_count = decode_rs_char(rs_handle, rs_packet, corr_pos, 0);
        if (corr_count == -1) {
          uncorr_errors = true;
          GR_LOG_DEBUG(d_logger, "uncorrectable error");
        } else
          total_corr_count += corr_count;

        // correct errors
        for (int j = 0; j < corr_count; j++) {

          int pos = corr_pos[j] - 135;
          if (pos < 0)
            continue;
          sf[pos * subch_index + i] = rs_packet[pos];
        }
      }
      //GR_LOG_DEBUG(d_logger, format("RS corrected %d errors in superframe") % total_corr_count);
      d_corrected_errors = total_corr_count;
    }


    void
    reed_solomon_decode_bb_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required)
    {
      ninput_items_required[0] = noutput_items*120/110;
    }

    int
    reed_solomon_decode_bb_impl::general_work(int noutput_items,
                                              gr_vector_int &ninput_items,
                                              gr_vector_const_void_star &input_items,
                                              gr_vector_void_star &output_items)
    {
      const unsigned char *in = (const unsigned char *) input_items[0];
      unsigned char *out = (unsigned char *) output_items[0];

      for (int n = 0; n < noutput_items / d_superframe_size_rs; n++) {
        uint8_t superframe[d_superframe_size];
        memcpy(superframe, &in[n * d_superframe_size], d_superframe_size);
        DecodeSuperframe(superframe, d_superframe_size);
        memcpy(&out[n * d_superframe_size_rs], superframe, d_superframe_size_rs);
      }
      // Tell runtime system how many input items we consumed on
      // each input stream.
      consume_each(noutput_items * 120 / 110);

      // Tell runtime system how many output items we produced.
      return noutput_items;
    }

  } /* namespace dab */
} /* namespace gr */