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.
22 ao_fec_dump_bytes(uint8_t *bytes, uint16_t len, char *name)
26 printf ("%s (%d):", name, len);
27 for (i = 0; i < len; i++) {
29 printf ("\n\t%02x:", i);
30 printf(" %02x", bytes[i]);
35 static uint16_t inline
36 crc_byte(uint8_t byte, uint16_t crc)
40 for (bit = 0; bit < 8; bit++) {
41 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
42 crc = (crc << 1) ^ 0x8005;
51 ao_fec_crc(uint8_t *bytes, uint8_t len)
53 uint16_t crc = AO_FEC_CRC_INIT;
56 crc = crc_byte(*bytes++, crc);
61 * len is the length of the data; the crc will be
62 * the fist two bytes after that
66 ao_fec_check_crc(uint8_t *bytes, uint8_t len)
68 uint16_t computed_crc = ao_fec_crc(bytes, len);
69 uint16_t received_crc = (bytes[len] << 8) | (bytes[len+1]);
71 return computed_crc == received_crc;
75 ao_fec_prepare(uint8_t *in, uint8_t len, uint8_t *extra)
77 uint16_t crc = ao_fec_crc (in, len);
82 extra[i++] = crc >> 8;
85 /* Append FEC -- 1 byte if odd, two bytes if even */
86 num_fec = 2 - (i & 1);
88 extra[i++] = AO_FEC_TRELLIS_TERMINATOR;
92 const uint8_t ao_fec_whiten_table[] = {
93 #include "ao_whiten.h"
98 ao_fec_whiten(uint8_t *in, uint8_t len, uint8_t *out)
100 const uint8_t *w = ao_fec_whiten_table;
103 *out++ = *in++ ^ *w++;
107 * Unused as interleaving is now built in to ao_fec_encode
111 ao_fec_interleave(uint8_t *d, uint8_t len)
115 for (i = 0; i < len; i += 4) {
116 uint32_t interleaved = 0;
118 for (j = 0; j < 4 * 4; j++) {
120 interleaved |= (d[i + (~j & 0x3)] >> (2 * ((j & 0xc) >> 2))) & 0x03;
122 d[i+0] = interleaved >> 24;
123 d[i+1] = interleaved >> 16;
124 d[i+2] = interleaved >> 8;
125 d[i+3] = interleaved;
130 static const uint8_t ao_fec_encode_table[16] = {
143 ao_fec_encode(uint8_t *in, uint8_t len, uint8_t *out)
145 uint8_t extra[AO_FEC_PREPARE_EXTRA];
147 uint32_t encode, interleave;
148 uint8_t pair, byte, bit;
150 const uint8_t *whiten = ao_fec_whiten_table;
152 extra_len = ao_fec_prepare(in, len, extra);
153 for (pair = 0; pair < len + extra_len; pair += 2) {
155 for (byte = 0; byte < 2; byte++) {
156 if (pair + byte == len)
158 fec |= *in++ ^ *whiten++;
159 for (bit = 0; bit < 8; bit++) {
160 encode = encode << 2 | ao_fec_encode_table[fec >> 7];
161 fec = (fec << 1) & 0x7ff;
166 for (bit = 0; bit < 4 * 4; bit++) {
167 uint8_t byte_shift = (bit & 0x3) << 3;
168 uint8_t bit_shift = (bit & 0xc) >> 1;
170 interleave = (interleave << 2) | ((encode >> (byte_shift + bit_shift)) & 0x3);
172 *out++ = interleave >> 24;
173 *out++ = interleave >> 16;
174 *out++ = interleave >> 8;
175 *out++ = interleave >> 0;
177 return (len + extra_len) * 2;