2 * libzvbi -- Error correction functions
4 * Copyright (C) 2001, 2002, 2003, 2004, 2007 Michael H. Schimek
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
22 /* $Id: hamm.h,v 1.16 2013-07-10 11:37:13 mschimek Exp $ */
24 #ifndef __ZVBI_HAMM_H__
25 #define __ZVBI_HAMM_H__
27 #include <inttypes.h> /* uintN_t */
34 extern const uint8_t _vbi_bit_reverse [256];
35 extern const uint8_t _vbi_hamm8_fwd [16];
36 extern const int8_t _vbi_hamm8_inv [256];
37 extern const int8_t _vbi_hamm24_inv_par [3][256];
40 * @addtogroup Error Error correction functions
42 * @brief Helper functions to decode sliced VBI data.
47 * @param c Unsigned byte.
49 * Reverses the bits of the argument.
52 * Data bits 0 [msb] ... 7 [lsb].
56 _vbi_inline unsigned int
57 vbi_rev8 (unsigned int c)
59 return _vbi_bit_reverse[(uint8_t) c];
63 * @param c Unsigned 16 bit word.
65 * Reverses (or "reflects") the bits of the argument.
68 * Data bits 0 [msb] ... 15 [lsb].
72 _vbi_inline unsigned int
73 vbi_rev16 (unsigned int c)
75 return _vbi_bit_reverse[(uint8_t) c] * 256
76 + _vbi_bit_reverse[(uint8_t)(c >> 8)];
80 * @param p Pointer to a 16 bit word, last significant
83 * Reverses (or "reflects") the bits of the argument.
86 * Data bits 0 [msb] ... 15 [lsb].
90 _vbi_inline unsigned int
91 vbi_rev16p (const uint8_t * p)
93 return _vbi_bit_reverse[p[0]] * 256
94 + _vbi_bit_reverse[p[1]];
98 * @param c Unsigned byte.
101 * Changes the most significant bit of the byte
102 * to make the number of set bits odd.
106 _vbi_inline unsigned int
107 vbi_par8 (unsigned int c)
111 /* if 0 == (inv_par[] & 32) change bit 7 of c. */
112 c ^= 128 & ~(_vbi_hamm24_inv_par[0][c] << 2);
118 * @param c Unsigned byte.
121 * If the byte has odd parity (sum of bits modulo 2 is 1) the
122 * byte AND 127, otherwise a negative value.
127 vbi_unpar8 (unsigned int c)
129 /* Disabled until someone finds a reliable way
130 to test for cmov support at compile time. */
134 /* This saves cache flushes and an explicit branch. */
135 __asm__ (" testb %1,%1\n"
137 : "+&a" (r) : "c" (c), "rm" (-1));
140 if (_vbi_hamm24_inv_par[0][(uint8_t) c] & 32) {
143 /* The idea is to OR results together to find a parity
144 error in a sequence, rather than a test and branch on
151 vbi_par (uint8_t * p,
154 vbi_unpar (uint8_t * p,
158 * @param c Integer between 0 ... 15.
160 * Encodes a nibble with Hamming 8/4 protection
161 * as specified in EN 300 706, Section 8.2.
164 * Hamming encoded unsigned byte, lsb first transmitted.
168 _vbi_inline unsigned int
169 vbi_ham8 (unsigned int c)
171 return _vbi_hamm8_fwd[c & 15];
175 * @param c Hamming 8/4 protected byte, lsb first transmitted.
177 * Decodes a Hamming 8/4 protected byte
178 * as specified in EN 300 706, Section 8.2.
181 * Data bits (D4 [msb] ... D1 [lsb]) or a negative
182 * value if the byte contained uncorrectable errors.
187 vbi_unham8 (unsigned int c)
189 return _vbi_hamm8_inv[(uint8_t) c];
193 * @param p Pointer to a Hamming 8/4 protected 16 bit word,
194 * last significant byte first, lsb first transmitted.
196 * Decodes a Hamming 8/4 protected byte pair
197 * as specified in EN 300 706, Section 8.2.
200 * Data bits D4 [msb] ... D1 of first byte and D4 ... D1 [lsb]
201 * of second byte, or a negative value if any of the bytes
202 * contained uncorrectable errors.
207 vbi_unham16p (const uint8_t * p)
209 return ((int) _vbi_hamm8_inv[p[0]])
210 | (((int) _vbi_hamm8_inv[p[1]]) << 4);
214 vbi_ham24p (uint8_t * p,
217 vbi_unham24p (const uint8_t * p)
226 #endif /* __ZVBI_HAMM_H__ */