aboutsummaryrefslogtreecommitdiffstats
path: root/src/rsdecoder.cpp
blob: 5d01dcf46fab32567de19dcce95bd702e54370d0 (plain)
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
/*
   Copyright (C) 2015 Stefan Pöschel
   Copyright (C) 2016 Matthias P. Braendli (http://www.opendigitalradio.org)

   This program 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 of the License, or
   (at your option) any later version.

   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
   */

#include <stdexcept>
#include "rsdecoder.hpp"

#define RSDEC_DEBUG 0

RSDecoder::RSDecoder()
{
    rs_handle = init_rs_char(8, 0x11D, 0, 1, 10, 135);
    if(!rs_handle)
        throw std::runtime_error("RSDecoder: error while init_rs_char");
}

RSDecoder::~RSDecoder()
{
    free_rs_char(rs_handle);
}

int RSDecoder::DecodeSuperframe(std::vector<uint8_t> &sf, int subch_index)
{
    int total_corr_count = 0;
    bool uncorr_errors = false;

    std::vector<int> errors_per_index(subch_index);

    // 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);
        errors_per_index[i] = corr_count;

        if(corr_count == -1) {
            uncorr_errors = true;
        }
        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;

            //			fprintf(stderr, "j: %d, pos: %d, sf-index: %d\n", j, pos, pos * subch_index + i);
            sf[pos * subch_index + i] = rs_packet[pos];
        }
    }

#if RSDEC_DEBUG
    // output statistics
    if (total_corr_count || uncorr_errors) {
        printf("RS uncorrected errors:\n");
        for (size_t i = 0; i < errors_per_index.size(); i++) {
            int e = errors_per_index[i];
            printf(" (%zu: %d)", i, e);
        }
        printf("\n");
    }
#endif

    return uncorr_errors ? -1 : total_corr_count;
}