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, uint8_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 ao_fec_prepare(uint8_t *in, uint8_t len, uint8_t *out)
63 uint16_t crc = ao_fec_crc (in, len);
68 for (i = 0; i < len; i++)
75 /* Append FEC -- 1 byte if odd, two bytes if even */
76 num_fec = 2 - (i & 1);
78 out[i++] = AO_FEC_TRELLIS_TERMINATOR;
82 static const uint8_t whiten[] = {
83 #include "ao_whiten.h"
87 ao_fec_whiten(uint8_t *in, uint8_t len, uint8_t *out)
89 const uint8_t *w = whiten;
92 *out++ = *in++ ^ *w++;
95 static const uint8_t ao_fec_encode_table[16] = {
103 ao_fec_encode(uint8_t *in, uint8_t len, uint8_t *out)
105 uint16_t fec = 0, output;
108 for (byte = 0; byte < len; byte++) {
109 fec = (fec & 0x700) | in[byte];
111 for (bit = 0; bit < 8; bit++) {
112 output = output << 2 | ao_fec_encode_table[fec >> 7];
113 fec = (fec << 1) & 0x7ff;
115 out[byte * 2] = output >> 8;
116 out[byte * 2 + 1] = output;
122 ao_fec_interleave(uint8_t *in, uint8_t len, uint8_t *out)
126 for (i = 0; i < len; i += 4) {
127 uint32_t interleaved = 0;
129 for (j = 0; j < 4 * 4; j++) {
131 interleaved |= (in[i + (~j & 0x3)] >> (2 * ((j & 0xc) >> 2))) & 0x03;
133 out[i+0] = interleaved >> 24;
134 out[i+1] = interleaved >> 16;
135 out[i+2] = interleaved >> 8;
136 out[i+3] = interleaved;