1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: shc.h 8022 2007-06-05 22:23:38Z giles $ */
15
/* Common definitions for filters using Huffman coding */
24
* These definitions are valid for code lengths up to 16 bits
25
* and non-negative decoded values up to 15 bits.
27
* We define 3 different representations of the code: encoding tables,
28
* decoding tables, and a definition table which can be generated easily
29
* from frequency information and which in turn can easily generate
30
* the encoding and decoding tables.
32
* The definition table has two parts: a list of the number of i-bit
33
* codes for each i >= 1, and the decoded values corresponding to
34
* the code values in increasing lexicographic order (which will also
35
* normally be decreasing code frequency). Calling these two lists
36
* L[1..M] and V[0..N-1] respectively, we have the following invariants:
37
* - 1 <= M <= max_hc_length, N >= 2.
39
* - for i=1..M, L[i] >= 0.
40
* - sum(i=1..M: L[i]) = N.
41
* - sum(i=1..M: L[i] * 2^-i) = 1.
42
* - V[0..N-1] are a permutation of the integers 0..N-1.
44
#define max_hc_length 16
45
typedef struct hc_definition_s {
46
ushort *counts; /* [0..M] */
47
uint num_counts; /* M */
48
ushort *values; /* [0..N-1] */
49
uint num_values; /* N */
52
/* ------ Common state ------ */
55
* Define the common stream state for Huffman-coded filters.
56
* Invariants when writing:
57
* 0 <= bits_left <= hc_bits_size;
58
* Only the leftmost (hc_bits_size - bits_left) bits of bits
61
#define stream_hc_state_common\
63
/* The client sets the following before initialization. */\
64
bool FirstBitLowOrder;\
65
/* The following are updated dynamically. */\
66
uint bits; /* most recent bits of input or */\
67
/* current bits of output */\
68
int bits_left /* # of valid low bits (input) or */\
69
/* unused low bits (output) in above, */\
70
/* 0 <= bits_left <= 7 */
71
typedef struct stream_hc_state_s {
72
stream_hc_state_common;
75
#define hc_bits_size (arch_sizeof_int * 8)
76
#define s_hce_init_inline(ss)\
77
((ss)->bits = 0, (ss)->bits_left = hc_bits_size)
78
#define s_hcd_init_inline(ss)\
79
((ss)->bits = 0, (ss)->bits_left = 0)
81
/* ------ Encoding tables ------ */
83
/* Define the structure for the encoding tables. */
84
typedef struct hce_code_s {
89
#define hce_entry(c, len) { c, len }
91
typedef struct hce_table_s {
96
#define hce_bits_available(n)\
97
(ss->bits_left >= (n) || wlimit - q > ((n) - ss->bits_left - 1) >> 3)
99
/* ------ Encoding utilities ------ */
102
* Put a code on the output. The client is responsible for ensuring
103
* that q does not exceed pw->limit.
107
# define hc_print_value(code, clen)\
109
(dlprintf2("[W]0x%x,%d\n", code, clen), 0) : 0)
110
# define hc_print_value_then(code, clen) hc_print_value(code, clen),
112
# define hc_print_value(code, clen) 0
113
# define hc_print_value_then(code, clen) /* */
115
#define hc_print_code(rp) hc_print_value((rp)->code, (rp)->code_length)
117
/* Declare variables that hold the encoder state. */
118
#define hce_declare_state\
120
register int bits_left
122
/* Load the state from the stream. */
123
/* Free variables: ss, bits, bits_left. */
124
#define hce_load_state()\
125
bits = ss->bits, bits_left = ss->bits_left
127
/* Store the state back in the stream. */
128
/* Free variables: ss, bits, bits_left. */
129
#define hce_store_state()\
130
ss->bits = bits, ss->bits_left = bits_left
132
/* Put a code on the stream. */
133
void hc_put_code_proc(bool, byte *, uint);
135
#define hc_put_value(ss, q, code, clen)\
136
(hc_print_value_then(code, clen)\
137
((bits_left -= (clen)) >= 0 ?\
138
(bits += (code) << bits_left) :\
139
(hc_put_code_proc((ss)->FirstBitLowOrder,\
140
q += hc_bits_size >> 3,\
141
(bits + ((code) >> -bits_left))),\
142
bits = (code) << (bits_left += hc_bits_size))))
143
#define hc_put_code(ss, q, cp)\
144
hc_put_value(ss, q, (cp)->code, (cp)->code_length)
147
* Force out the final bits to the output.
148
* Note that this does a store_state, but not a load_state.
150
byte *hc_put_last_bits_proc(stream_hc_state *, byte *, uint, int);
152
#define hc_put_last_bits(ss, q)\
153
hc_put_last_bits_proc(ss, q, bits, bits_left)
155
/* ------ Decoding tables ------ */
158
* Define the structure for the decoding tables.
159
* First-level nodes are either leaves, which have
160
* value = decoded value
161
* code_length <= initial_bits
162
* or non-leaves, which have
163
* value = the index of a sub-table
164
* code_length = initial_bits + the number of additional dispatch bits
165
* Second-level nodes are always leaves, with
166
* code_length = the actual number of bits in the code - initial_bits.
169
typedef struct hcd_code_s {
174
typedef struct hcd_table_s {
180
/* Declare variables that hold the decoder state. */
181
#define hcd_declare_state\
182
register const byte *p;\
187
/* Load the state from the stream. */
188
/* Free variables: pr, ss, p, rlimit, bits, bits_left. */
189
#define hcd_load_state()\
193
bits_left = ss->bits_left
195
/* Store the state back in the stream. */
196
/* Put back any complete bytes into the input buffer. */
197
/* Free variables: pr, ss, p, bits, bits_left. */
198
#define hcd_store_state()\
199
pr->ptr = p -= (bits_left >> 3),\
200
ss->bits = bits >>= (bits_left & ~7),\
201
ss->bits_left = bits_left &= 7
203
/* Macros to get blocks of bits from the input stream. */
204
/* Invariants: 0 <= bits_left <= bits_size; */
205
/* bits [bits_left-1..0] contain valid data. */
207
#define hcd_bits_available(n)\
208
(bits_left >= (n) || rlimit - p > ((n) - bits_left - 1) >> 3)
209
/* For hcd_ensure_bits, n must not be greater than 8. */
210
#define HCD_ENSURE_BITS_ELSE(n)\
213
else HCD_MORE_BITS_ELSE
214
#define hcd_ensure_bits(n, outl)\
215
BEGIN HCD_ENSURE_BITS_ELSE(n) goto outl; END
217
/* Load more bits into the buffer. */
218
#define HCD_MORE_BITS_1_ELSE\
222
if (ss->FirstBitLowOrder)\
223
c = byte_reverse_bits[c];\
224
bits = (bits << 8) + c, bits_left += 8;\
226
#if hc_bits_size == 16
227
# define HCD_MORE_BITS_ELSE HCD_MORE_BITS_1_ELSE
228
#else /* hc_bits_size >= 32 */
229
# define HCD_MORE_BITS_ELSE\
230
if (rlimit - p >= 3) {\
231
if (ss->FirstBitLowOrder)\
232
bits = (bits << 24) + ((uint)byte_reverse_bits[p[1]] << 16) + ((uint)byte_reverse_bits[p[2]] << 8) + byte_reverse_bits[p[3]];\
234
bits = (bits << 24) + ((uint)p[1] << 16) + ((uint)p[2] << 8) + p[3];\
235
bits_left += 24, p += 3;\
236
} else HCD_MORE_BITS_1_ELSE
238
#define hcd_more_bits(outl)\
239
BEGIN HCD_MORE_BITS_ELSE goto outl; END
241
#define hcd_peek_bits(n) ((bits >> (bits_left - (n))) & ((1 << (n)) - 1))
243
/* hcd_peek_var_bits requires bits_left <= 8. */
244
#define hcd_peek_var_bits(n)\
245
((bits >> (bits_left - (n))) & byte_right_mask[n])
247
/* hcd_peek_bits_left requires bits_left <= 8. */
248
#define hcd_peek_bits_left()\
249
(bits & byte_right_mask[bits_left])
251
#define hcd_skip_bits(n) (bits_left -= (n))
253
#endif /* shc_INCLUDED */