2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 #include <ao_profile.h>
28 uint32_t ao_fec_decode_start, ao_fec_decode_end;
32 * byte order repeats through 3 2 1 0
34 * bit-pair order repeats through
38 * So, the over all order is:
40 * 3,1/0 2,1/0 1,1/0 0,1/0
41 * 3,3/2 2,3/2 1,3/2 0,3/2
42 * 3,5/4 2,5/4 1,5/4 0,5/4
43 * 3,7/6 2,7/6 1,7/6 0,7/6
45 * The raw bit order is thus
47 * 1e/1f 16/17 0e/0f 06/07
48 * 1c/1d 14/15 0c/0d 04/05
49 * 1a/1b 12/13 0a/0b 02/03
50 * 18/19 10/11 08/09 00/01
53 static const uint8_t ao_interleave_order[] = {
54 0x1e, 0x16, 0x0e, 0x06,
55 0x1c, 0x14, 0x0c, 0x04,
56 0x1a, 0x12, 0x0a, 0x02,
57 0x18, 0x10, 0x08, 0x00
60 static inline uint16_t ao_interleave_index(uint16_t i) {
61 return (i & ~0x1e) | ao_interleave_order[(i & 0x1e) >> 1];
67 typedef uint32_t bits_t;
73 * These are just the 'zero' states; the 'one' states mirror them
75 static const uint8_t ao_fec_decode_table[NUM_STATE*2] = {
87 ao_next_state(uint8_t state, uint8_t bit)
89 return ((state << 1) | bit) & 0x7;
93 * 'in' is 8-bits per symbol soft decision data
94 * 'len' is input byte length. 'out' must be
99 ao_fec_decode(const uint8_t *in, uint16_t len, uint8_t *out, uint8_t out_len, uint16_t (*callback)(void))
101 static uint32_t cost[2][NUM_STATE]; /* path cost */
102 static bits_t bits[2][NUM_STATE]; /* save bits to quickly output them */
104 uint16_t i; /* input byte index */
105 uint16_t b; /* encoded symbol index (bytes/2) */
106 uint16_t o; /* output bit index */
107 uint8_t p; /* previous cost/bits index */
108 uint8_t n; /* next cost/bits index */
109 uint8_t state; /* state index */
110 const uint8_t *whiten = ao_fec_whiten_table;
111 uint16_t interleave; /* input byte array index */
114 uint16_t crc = AO_FEC_CRC_INIT;
120 for (state = 0; state < NUM_STATE; state++) {
121 cost[0][state] = 0x7fffffff;
137 start_tick = ao_profile_tick();
140 for (i = 0; i < len; i += 2) {
150 /* Fetch one pair of input bytes, de-interleaving
153 interleave = ao_interleave_index(i);
155 s1 = in[interleave+1];
159 /* Compute path costs and accumulate output bit path
160 * for each state and encoded bit value. Unrolling
161 * this loop is worth about > 30% performance boost.
162 * Decoding 76-byte remote access packets is reduced
163 * from 14.700ms to 9.3ms. Redoing the loop to
164 * directly compare the two pasts for each future state
165 * reduces this down to 5.7ms
168 /* Ok, of course this is tricky, it's optimized.
170 * First, it's important to realize that we have 8
171 * states representing the combinations of the three
172 * most recent bits from the encoder. Flipping any
173 * of these three bits flips both output bits.
175 * 'state<<1' represents the target state for a new
176 * bit value of 0. '(state<<1)+1' represents the
177 * target state for a new bit value of 1.
179 * 'state' is the previous state with an oldest bit
180 * value of 0. 'state + 4' is the previous state with
181 * an oldest bit value of 1. These two states will
182 * either lead to 'state<<1' or '(state<<1)+1', depending
183 * on whether the next encoded bit was a zero or a one.
185 * m0 and m1 are the cost of coming to 'state<<1' from
186 * one of the two possible previous states 'state' and
189 * Because we know the expected values of each
190 * received bit are flipped between these two previous
193 * bitcost(state+4) = 510 - bitcost(state)
195 * With those two total costs in hand, we then pick
196 * the lower as the cost of the 'state<<1', and compute
197 * the path of bits leading to that state.
199 * Then, do the same for '(state<<1) + 1'. This time,
200 * instead of computing the m0 and m1 values from
201 * scratch, because the only difference is that we're
202 * expecting a one bit instead of a zero bit, we just
203 * flip the bitcost values around to match the
204 * expected transmitted bits with some tricky
205 * arithmetic which is equivalent to:
207 * m0 = cost[p][state] + (510 - bitcost);
208 * m1 = cost[p][state+4] + bitcost
210 * Then, the lowest cost and bit trace of the new state
214 #define DO_STATE(state) { \
221 bitcost = ((uint32_t) (s0 ^ ao_fec_decode_table[(state<<1)]) + \
222 (uint32_t) (s1 ^ ao_fec_decode_table[(state<<1)|1])); \
224 m0 = cost[p][state] + bitcost; \
225 m1 = cost[p][state+4] + (510 - bitcost); \
227 cost[n][state<<1] = bit ? m1 : m0; \
228 bits[n][state<<1] = (bits[p][state + (bit<<2)] << 1) | (state&1); \
230 m0 -= (bitcost+bitcost-510); \
231 m1 += (bitcost+bitcost-510); \
233 cost[n][(state<<1)+1] = bit ? m1 : m0; \
234 bits[n][(state<<1)+1] = (bits[p][state + (bit<<2)] << 1) | (state&1); \
243 printf ("bit %3d symbol %2x %2x:", i/2, s0, s1);
244 for (state = 0; state < NUM_STATE; state++) {
245 printf (" %8u(%08x)", cost[n][state], bits[n][state]);
251 /* A loop is needed to handle the last output byte. It
252 * won't have any bits of future data to perform full
253 * error correction, but we might as well give the
254 * best possible answer anyways.
256 while ((b - o) >= (8 + NUM_HIST) || (i + 2 >= len && b > o)) {
258 /* Compute number of bits to the end of the
259 * last full byte of data. This is generally
260 * NUM_HIST, unless we've reached
261 * the end of the input, in which case
264 int8_t dist = b - (o + 8); /* distance to last ready-for-writing bit */
265 uint32_t min_cost; /* lowest cost */
266 uint8_t min_state; /* lowest cost state */
269 /* Find the best fit at the current point
272 min_cost = cost[p][0];
274 for (state = 1; state < NUM_STATE; state++) {
275 if (cost[p][state] < min_cost) {
276 min_cost = cost[p][state];
281 /* The very last byte of data has the very last bit
282 * of data left in the state value; just smash the
283 * bits value in place and reset the 'dist' from
284 * -1 to 0 so that the full byte is read out
287 bits[p][min_state] = (bits[p][min_state] << 1) | (min_state & 1);
292 printf ("\tbit %3d min_cost %5d old bit %3d old_state %x bits %02x whiten %0x\n",
293 i/2, min_cost, o + 8, min_state, (bits[p][min_state] >> dist) & 0xff, *whiten);
295 byte = (bits[p][min_state] >> dist) ^ *whiten++;
298 crc = ao_fec_crc_byte(byte, crc);
301 if ((out[-2] == (uint8_t) (crc >> 8)) &&
302 out[-1] == (uint8_t) crc)
303 out[-1] = AO_FEC_DECODE_CRC_OK;
314 ao_fec_decode_start = start_tick;
315 ao_fec_decode_end = ao_profile_tick();