altos: improve FEC apis to reduce data copying
[fw/altos] / src / core / ao_fec_tx.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao_fec.h>
19 #include <stdio.h>
20
21 void
22 ao_fec_dump_bytes(uint8_t *bytes, uint16_t len, char *name)
23 {
24         uint16_t        i;
25
26         printf ("%s (%d):", name, len);
27         for (i = 0; i < len; i++) {
28                 if ((i & 7) == 0)
29                         printf ("\n\t%02x:", i);
30                 printf(" %02x", bytes[i]);
31         }
32         printf ("\n");
33 }
34
35 static uint16_t inline
36 crc_byte(uint8_t byte, uint16_t crc)
37 {
38         uint8_t bit;
39
40         for (bit = 0; bit < 8; bit++) {
41                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
42                         crc = (crc << 1) ^ 0x8005;
43                 else
44                         crc = (crc << 1);
45                 byte <<= 1;
46         }
47         return crc;
48 }
49
50 uint16_t
51 ao_fec_crc(uint8_t *bytes, uint8_t len)
52 {
53         uint16_t        crc = AO_FEC_CRC_INIT;
54
55         while (len--)
56                 crc = crc_byte(*bytes++, crc);
57         return crc;
58 }
59
60 /*
61  * len is the length of the data; the crc will be
62  * the fist two bytes after that
63  */
64
65 uint8_t
66 ao_fec_check_crc(uint8_t *bytes, uint8_t len)
67 {
68         uint16_t        computed_crc = ao_fec_crc(bytes, len);
69         uint16_t        received_crc = (bytes[len] << 8) | (bytes[len+1]);
70
71         return computed_crc == received_crc;
72 }
73
74 uint8_t
75 ao_fec_prepare(uint8_t *in, uint8_t len, uint8_t *extra)
76 {
77         uint16_t        crc = ao_fec_crc (in, len);
78         uint8_t         i = 0;
79         uint8_t         num_fec;
80
81         /* Append CRC */
82         extra[i++] = crc >> 8;
83         extra[i++] = crc;
84
85         /* Append FEC -- 1 byte if odd, two bytes if even */
86         num_fec = 2 - (i & 1);
87         while (num_fec--)
88                 extra[i++] = AO_FEC_TRELLIS_TERMINATOR;
89         return i;
90 }
91
92 const uint8_t ao_fec_whiten_table[] = {
93 #include "ao_whiten.h"
94 };
95
96 #if 0
97 void
98 ao_fec_whiten(uint8_t *in, uint8_t len, uint8_t *out)
99 {
100         const uint8_t   *w = ao_fec_whiten_table;
101
102         while (len--)
103                 *out++ = *in++ ^ *w++;
104 }
105
106 /*
107  * Unused as interleaving is now built in to ao_fec_encode
108  */
109
110 static void
111 ao_fec_interleave(uint8_t *d, uint8_t len)
112 {
113         uint8_t i, j;
114
115         for (i = 0; i < len; i += 4) {
116                 uint32_t        interleaved = 0;
117
118                 for (j = 0; j < 4 * 4; j++) {
119                         interleaved <<= 2;
120                         interleaved |= (d[i + (~j & 0x3)] >> (2 * ((j & 0xc) >> 2))) & 0x03;
121                 }
122                 d[i+0] = interleaved >> 24;
123                 d[i+1] = interleaved >> 16;
124                 d[i+2] = interleaved >> 8;
125                 d[i+3] = interleaved;
126         }
127 }
128 #endif
129
130 static const uint8_t ao_fec_encode_table[16] = {
131 /* next 0  1      state */
132         0, 3,   /* 000 */
133         1, 2,   /* 001 */
134         3, 0,   /* 010 */
135         2, 1,   /* 011 */
136         3, 0,   /* 100 */
137         2, 1,   /* 101 */
138         0, 3,   /* 110 */
139         1, 2    /* 111 */
140 };
141
142 uint8_t
143 ao_fec_encode(uint8_t *in, uint8_t len, uint8_t *out)
144 {
145         uint8_t         extra[AO_FEC_PREPARE_EXTRA];
146         uint8_t         extra_len;
147         uint32_t        encode, interleave;
148         uint8_t         pair, byte, bit;
149         uint16_t        fec = 0;
150         const uint8_t   *whiten = ao_fec_whiten_table;
151
152         extra_len = ao_fec_prepare(in, len, extra);
153         for (pair = 0; pair < len + extra_len; pair += 2) {
154                 encode = 0;
155                 for (byte = 0; byte < 2; byte++) {
156                         if (pair + byte == len)
157                                 in = extra;
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;
162                         }
163                 }
164
165                 interleave = 0;
166                 for (bit = 0; bit < 4 * 4; bit++) {
167                         uint8_t byte_shift = (bit & 0x3) << 3;
168                         uint8_t bit_shift = (bit & 0xc) >> 1;
169
170                         interleave = (interleave << 2) | ((encode >> (byte_shift + bit_shift)) & 0x3);
171                 }
172                 *out++ = interleave >> 24;
173                 *out++ = interleave >> 16;
174                 *out++ = interleave >> 8;
175                 *out++ = interleave >> 0;
176         }
177         return (len + extra_len) * 2;
178 }