summaryrefslogtreecommitdiffstats
path: root/src/PuncturingEncoder.cpp
blob: 9bd90049490cf1a5ffa325357ae18bf05a2fa200 (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty
   the Queen in Right of Canada (Communications Research Center Canada)

   Copyright (C) 2016
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://opendigitalradio.org
 */
/*
   This file is part of ODR-DabMod.

   ODR-DabMod 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.

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

#include "PuncturingEncoder.h"
#include "PcDebug.h"

#include <stdio.h>
#include <stdexcept>
#include <stdint.h>
#include <assert.h>


PuncturingEncoder::PuncturingEncoder() :
    ModCodec(),
    d_num_cu(0),
    d_in_block_size(0),
    d_out_block_size(0),
    d_tail_rule()
{
    PDEBUG("PuncturingEncoder() @ %p\n", this);
}

PuncturingEncoder::PuncturingEncoder(
        size_t num_cu) :
    ModCodec(),
    d_num_cu(num_cu),
    d_in_block_size(0),
    d_out_block_size(0),
    d_tail_rule()
{
    PDEBUG("PuncturingEncoder(%zu) @ %p\n", num_cu, this);
}

void PuncturingEncoder::adjust_item_size()
{
    PDEBUG("PuncturingEncoder::adjust_item_size()\n");
    int in_size = 0;
    int out_size = 0;
    int length = 0;

    for (const auto& rule : d_rules) {
        for (length = rule.length(); length > 0; length -= 4) {
            out_size += rule.bit_size();
            in_size += 4;
        }
    }

    if (d_tail_rule) {
        in_size += d_tail_rule->length();
        out_size += d_tail_rule->bit_size();
    }

    d_in_block_size = in_size;
    d_out_block_size = (out_size + 7) / 8;

    PDEBUG(" Puncturing encoder ratio (out/in): %zu / %zu\n",
            d_out_block_size, d_in_block_size);
}


void PuncturingEncoder::append_rule(const PuncturingRule& rule)
{
    PDEBUG("append_rule(rule(%zu, 0x%x))\n", rule.length(), rule.pattern());
    d_rules.push_back(rule);

    adjust_item_size();
}


void PuncturingEncoder::append_tail_rule(const PuncturingRule& rule)
{
    PDEBUG("append_tail_rule(rule(%zu, 0x%x))\n", rule.length(), rule.pattern());
    d_tail_rule = rule;

    adjust_item_size();
}


int PuncturingEncoder::process(Buffer* const dataIn, Buffer* dataOut)
{
    PDEBUG("PuncturingEncoder::process"
            "(dataIn: %p, dataOut: %p)\n",
            dataIn, dataOut);
    size_t in_count = 0;
    size_t out_count = 0;
    size_t bit_count = 0;
    size_t length;
    unsigned char data;
    uint32_t mask;
    uint32_t pattern;
    auto rule_it = d_rules.begin();
    PDEBUG(" in block size: %zu\n", d_in_block_size);
    PDEBUG(" out block size: %zu\n", d_out_block_size);

    if (d_num_cu > 0) {
        if (d_num_cu * 8 == d_out_block_size + 1) {
            /* EN 300 401 Table 31 in 11.3.1 UEP coding specifies
             * that we need one byte of padding
             */
            d_out_block_size = d_num_cu * 8;
        }

        if (d_num_cu * 8 != d_out_block_size) {
            throw std::runtime_error(
                    "PuncturingEncoder encoder initialisation failed. "
                    " CU: " + std::to_string(d_num_cu) +
                    " block_size: " + std::to_string(d_out_block_size));
        }
    }

    dataOut->setLength(d_out_block_size);
    const unsigned char* in = reinterpret_cast<const unsigned char*>(dataIn->getData());
    unsigned char* out = reinterpret_cast<unsigned char*>(dataOut->getData());

    if (dataIn->getLength() != d_in_block_size) {
        throw std::runtime_error(
                "PuncturingEncoder::process wrong input size");
    }

    if (d_tail_rule) {
        d_in_block_size -= d_tail_rule->length();
    }
    while (in_count < d_in_block_size) {
        for (length = rule_it->length(); length > 0; length -= 4) {
            mask = 0x80000000;
            pattern = rule_it->pattern();
            for (int i = 0; i < 4; ++i) {
                data = in[in_count++];
                for (int j = 0; j < 8; ++j) {
                    if (pattern & mask) {
                        out[out_count] <<= 1;
                        out[out_count] |= data >> 7;
                        if (++bit_count == 8) {
                            bit_count = 0;
                            ++out_count;
                        }
                    }
                    data <<= 1;
                    mask >>= 1;
                }
            }
        }
        if (++rule_it == d_rules.end()) {
            rule_it = d_rules.begin();
        }
    }
    if (d_tail_rule) {
        d_in_block_size += d_tail_rule->length();
        mask = 0x800000;
        pattern = d_tail_rule->pattern();
        length = d_tail_rule->length();
        for (size_t i = 0; i < length; ++i) {
            data = in[in_count++];
            for (int j = 0; j < 8; ++j) {
                if (pattern & mask) {
                    out[out_count] <<= 1;
                    out[out_count] |= data >> 7;
                    if (++bit_count == 8) {
                        bit_count = 0;
                        ++out_count;
                    }
                }
                data <<= 1;
                mask >>= 1;
            }
        }
    }
    while (bit_count) {
        out[out_count] <<= 1;
        if (++bit_count == 8) {
            bit_count = 0;
            ++out_count;
        }
    }

    for (size_t i = out_count; i < dataOut->getLength(); ++i) {
        out[out_count++] = 0;
    }

    assert(out_count == dataOut->getLength());
    if (out_count != dataOut->getLength()) {
        throw std::runtime_error("PuncturingEncoder::process output size "
                "does not correspond!");
    }

    return d_out_block_size;
}