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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 ao_fec_dump_bytes(const uint8_t *bytes, uint16_t len, const char *name)
28 printf ("%s (%d):", name, len);
29 for (i = 0; i < len; i++) {
31 printf ("\n\t%02x:", i);
32 printf(" %02x", bytes[i]);
39 ao_fec_crc(const uint8_t *bytes, uint8_t len)
41 uint16_t crc = AO_FEC_CRC_INIT;
44 crc = ao_fec_crc_byte(*bytes++, crc);
49 * len is the length of the data; the crc will be
50 * the fist two bytes after that
54 ao_fec_check_crc(const uint8_t *bytes, uint8_t len)
56 uint16_t computed_crc = ao_fec_crc(bytes, len);
57 uint16_t received_crc = (bytes[len] << 8) | (bytes[len+1]);
59 return computed_crc == received_crc;
63 * Compute CRC and trellis-terminator/interleave-pad bytes
66 ao_fec_prepare(const uint8_t *in, uint8_t len, uint8_t *extra)
68 uint16_t crc = ao_fec_crc (in, len);
73 extra[i++] = crc >> 8;
76 /* Append FEC -- 1 byte if odd, two bytes if even */
77 num_fec = 2 - (i & 1);
79 extra[i++] = AO_FEC_TRELLIS_TERMINATOR;
83 const uint8_t ao_fec_whiten_table[] = {
84 #include "ao_whiten.h"
87 static const uint8_t ao_fec_encode_table[16] = {
100 ao_fec_encode(const uint8_t *in, uint8_t len, uint8_t *out)
102 uint8_t extra[AO_FEC_PREPARE_EXTRA];
104 uint32_t encode, interleave;
105 uint8_t pair, byte, bit;
107 const uint8_t *whiten = ao_fec_whiten_table;
109 extra_len = ao_fec_prepare(in, len, extra);
110 for (pair = 0; pair < len + extra_len; pair += 2) {
112 for (byte = 0; byte < 2; byte++) {
113 if (pair + byte == len)
115 fec |= *in++ ^ *whiten++;
116 for (bit = 0; bit < 8; bit++) {
117 encode = encode << 2 | ao_fec_encode_table[fec >> 7];
118 fec = (fec << 1) & 0x7ff;
123 for (bit = 0; bit < 4 * 4; bit++) {
124 uint8_t byte_shift = (bit & 0x3) << 3;
125 uint8_t bit_shift = (bit & 0xc) >> 1;
127 interleave = (interleave << 2) | ((encode >> (byte_shift + bit_shift)) & 0x3);
129 *out++ = interleave >> 24;
130 *out++ = interleave >> 16;
131 *out++ = interleave >> 8;
132 *out++ = interleave >> 0;
134 return (len + extra_len) * 2;