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
|
/*
* Copyright © 2012 Keith Packard <keithp@keithp.com>
*
* 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; version 2 of the License.
*
* 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <ao_fec.h>
#include <stdio.h>
/*
* 'input' is 8-bits per symbol soft decision data
* 'len' is output byte length
*/
struct ao_soft_sym {
uint8_t a, b;
};
static const struct ao_soft_sym ao_fec_decode_table[16] = {
/* next 0 1 state */
{ 0x00, 0x00 }, { 0xff, 0xff }, /* 000 */
{ 0x00, 0xff }, { 0xff, 0x00 }, /* 001 */
{ 0xff, 0xff }, { 0x00, 0x00 }, /* 010 */
{ 0xff, 0x00 }, { 0x00, 0xff }, /* 011 */
{ 0xff, 0xff }, { 0x00, 0x00 }, /* 100 */
{ 0xff, 0x00 }, { 0x00, 0xff }, /* 101 */
{ 0x00, 0x00 }, { 0xff, 0xff }, /* 110 */
{ 0x00, 0xff }, { 0xff, 0x00 } /* 111 */
};
struct ao_soft_sym
ao_soft_sym(uint8_t bits)
{
struct ao_soft_sym s;
s.a = ((bits & 2) >> 1) * 0xff;
s.b = (bits & 1) * 0xff;
return s;
}
uint8_t
ao_next_state(uint8_t state, uint8_t bit)
{
return ((state << 1) | bit) & 0x7;
}
static inline abs(int x) { return x < 0 ? -x : x; }
static inline uint16_t
ao_cost(struct ao_soft_sym a, struct ao_soft_sym b)
{
return abs(a.a - b.a) + abs(a.b - b.b);
}
#define NUM_STATE 8
uint8_t
ao_fec_decode(uint8_t *in, int len, uint8_t *out)
{
uint16_t cost[2][NUM_STATE];
uint8_t prev[len/2 + 1][NUM_STATE];
int c;
int i, b;
uint8_t p, n;
uint8_t state = 0, min_state;
uint8_t bits[len/2];
p = 0;
for (c = 0; c < NUM_STATE; c++)
cost[0][c] = 0xffff;
cost[0][0] = 0;
for (i = 0; i < len; i += 2) {
b = i/2;
n = p ^ 1;
struct ao_soft_sym s = { .a = in[i], .b = in[i+1] };
for (state = 0; state < NUM_STATE; state++)
cost[n][state] = 0xffff;
for (state = 0; state < NUM_STATE; state++) {
int zero_cost = ao_cost(s, ao_fec_decode_table[state * 2 + 0]);
int one_cost = ao_cost(s, ao_fec_decode_table[state * 2 + 1]);
uint8_t zero_state = ao_next_state(state, 0);
uint8_t one_state = ao_next_state(state, 1);
zero_cost += cost[p][state];
one_cost += cost[p][state];
if (zero_cost < cost[n][zero_state]) {
prev[b+1][zero_state] = state;
cost[n][zero_state] = zero_cost;
}
if (one_cost < cost[n][one_state]) {
prev[b+1][one_state] = state;
cost[n][one_state] = one_cost;
}
}
printf ("bit %3d symbol %2x %2x:", i/2, s.a, s.b);
for (state = 0; state < NUM_STATE; state++) {
printf (" %5d", cost[n][state]);
}
printf ("\n");
p = n;
}
c = cost[p][0];
min_state = 0;
for (state = 1; state < NUM_STATE; state++) {
if (cost[p][state] < c) {
c = cost[p][state];
min_state = state;
}
}
for (b = len/2; b > 0; b--) {
bits[b-1] = min_state & 1;
min_state = prev[b][min_state];
}
for (i = 0; i < len/2; i += 8) {
uint8_t byte;
byte = 0;
for (b = 0; b < 8; b++)
byte = (byte << 1) | bits[i + b];
out[i/8] = byte;
}
return len/16;
}
|