forked from gnuradio/gnuradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc_encoder_impl.cc
206 lines (173 loc) · 5.59 KB
/
cc_encoder_impl.cc
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
/* -*- c++ -*- */
/*
* Copyright 2013-2014 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cc_encoder_impl.h"
#include <gnuradio/fec/cc_common.h>
#include <gnuradio/fec/generic_encoder.h>
#include <volk/volk.h>
#include <volk/volk_typedefs.h>
#include <boost/assign/list_of.hpp>
#include <cmath>
#include <cstdio>
#include <sstream>
#include <vector>
namespace gr {
namespace fec {
namespace code {
generic_encoder::sptr cc_encoder::make(int frame_size,
int k,
int rate,
std::vector<int> polys,
int start_state,
cc_mode_t mode,
bool padded)
{
return generic_encoder::sptr(
new cc_encoder_impl(frame_size, k, rate, polys, start_state, mode, padded));
}
cc_encoder_impl::cc_encoder_impl(int frame_size,
int k,
int rate,
std::vector<int> polys,
int start_state,
cc_mode_t mode,
bool padded)
: generic_encoder("cc_encoder"),
d_rate(rate),
d_k(k),
d_polys(polys),
d_start_state(start_state),
d_mode(mode),
d_padding(0)
{
if (static_cast<size_t>(d_rate) != d_polys.size()) {
throw std::runtime_error(
"cc_encoder: Number of polynomials must be the same as the value of rate");
}
if (d_rate < 2) {
throw std::runtime_error("cc_encoder: inverse rate r must be > 2");
}
if (k < 2 || k > 31) {
throw std::runtime_error(
"cc_encoder: constraint length K must in be the range [2, 31]");
}
if (d_start_state >= (1u << (d_k - 1))) {
throw std::runtime_error("cc_encoder: start state is invalid; must be in range "
"[0, 2^(K-1)-1] where K is the constraint length");
}
if (frame_size < 1) {
throw std::runtime_error("cc_encoder: frame_size must be > 0");
}
if (mode != CC_STREAMING && mode != CC_TRUNCATED && mode != CC_TAILBITING &&
mode != CC_TERMINATED) {
throw std::runtime_error("cc_encoder: invalid mode passed");
}
partab_init();
// set up a padding factor. If padding, extends the encoding
// by this many bits to fit into a full byte.
if (padded && (mode == CC_TERMINATED)) {
d_padding = static_cast<int>(8.0f * ceilf(d_rate * (d_k - 1) / 8.0f) -
(d_rate * (d_k - 1)));
}
d_max_frame_size = frame_size;
set_frame_size(frame_size);
}
cc_encoder_impl::~cc_encoder_impl() {}
int cc_encoder_impl::get_output_size() { return d_output_size; }
int cc_encoder_impl::get_input_size() { return d_frame_size; }
bool cc_encoder_impl::set_frame_size(unsigned int frame_size)
{
bool ret = true;
if (frame_size > d_max_frame_size) {
GR_LOG_INFO(d_logger,
boost::format("tried to set frame to %1%; max possible is %2%") %
frame_size % d_max_frame_size);
frame_size = d_max_frame_size;
ret = false;
}
d_frame_size = frame_size;
if (d_mode == CC_TERMINATED) {
d_output_size = d_rate * (d_frame_size + d_k - 1) + d_padding;
}
/*
else if(d_trunc_intrinsic) {
int cnt = 0;
for(int i = 0; i < d_rate; ++i) {
if (d_polys[i] != 1) {
cnt++;
}
}
d_output_size = (d_rate * (d_frame_size)) + (cnt * (d_k - 1));
}
*/
else {
d_output_size = d_rate * d_frame_size;
}
return ret;
}
double cc_encoder_impl::rate() { return static_cast<double>(d_rate); }
int cc_encoder_impl::parity(int x)
{
x ^= (x >> 16);
x ^= (x >> 8);
return parityb(x);
}
int cc_encoder_impl::parityb(unsigned char x) { return Partab[x]; }
void cc_encoder_impl::partab_init(void)
{
int i, cnt, ti;
/* Initialize parity lookup table */
for (i = 0; i < 256; i++) {
cnt = 0;
ti = i;
while (ti) {
if (ti & 1)
cnt++;
ti >>= 1;
}
Partab[i] = cnt & 1;
}
}
void cc_encoder_impl::generic_work(void* in_buffer, void* out_buffer)
{
const unsigned char* in = (const unsigned char*)in_buffer;
unsigned char* out = (unsigned char*)out_buffer;
unsigned int my_state = d_start_state;
if (d_mode == CC_TAILBITING) {
for (unsigned int i = 0; i < d_k - 1; ++i) {
my_state = (my_state << 1) | (in[d_frame_size - (d_k - 1) + i] & 1);
}
}
for (unsigned int i = 0; i < d_frame_size; ++i) {
my_state = (my_state << 1) | (in[i] & 1);
for (unsigned int j = 0; j < d_rate; ++j) {
out[i * d_rate + j] =
(d_polys[j] < 0) ^ parity(my_state & abs(d_polys[j])) ? 1 : 0;
}
}
if (d_mode == CC_TERMINATED) {
for (unsigned int i = 0; i < d_k - 1; ++i) {
my_state = (my_state << 1) | ((d_start_state >> (d_k - 2 - i)) & 1);
for (unsigned int j = 0; j < d_rate; ++j) {
out[(i + d_frame_size) * d_rate + j] =
(d_polys[j] < 0) ^ parity(my_state & abs(d_polys[j])) ? 1 : 0;
}
}
}
if (d_mode == CC_TRUNCATED) {
my_state = d_start_state;
}
d_start_state = my_state;
}
} /* namespace code */
} /* namespace fec */
} /* namespace gr */