8 #include "bytesource.h"
11 // About 99% of all Huffman codes are <= 8 bits long (see codelen.txt),
12 // and it's what libjpeg uses. Thus, it seems like a reasonable size.
13 #define DEHUF_TABLE_BITS 8
14 #define DEHUF_TABLE_SIZE (1 << DEHUF_TABLE_BITS)
15 static const int DEHUF_SLOW_PATH = -1;
17 struct huffman_table {
18 unsigned num_codes[17]; // BITS
19 unsigned char codes[256]; // HUFFVAL
22 unsigned huffsize[256];
23 unsigned huffcode[256];
28 // Lookup table for fast decoding; given eight bits,
29 // return the symbol and length in bits. For longer codes,
30 // DEHUF_SLOW_PATH is returned.
32 // Note that the codes we return are 8-bit, but the type of
33 // the lookup tables is int to avoid extra zero extending.
34 int lookup_table_codes[DEHUF_TABLE_SIZE];
35 int lookup_table_length[DEHUF_TABLE_SIZE];
38 enum coefficient_class {
43 typedef struct huffman_table huffman_tables_t[NUM_COEFF_CLASSES][4];
45 // Read Huffman tables from a stream, and compute the derived values.
46 void read_huffman_tables(huffman_tables_t* dst, raw_input_func_t* input_func, void* userdata);
48 unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
49 struct bit_source* source);
53 static inline unsigned read_huffman_symbol(const struct huffman_table* table,
54 struct bit_source* source)
56 // FIXME: We can read past the end of the stream here in some edge
57 // cases. We need to define some guarantees in the layers above.
58 possibly_refill(source, DEHUF_TABLE_BITS);
59 unsigned lookup = peek_bits(source, DEHUF_TABLE_BITS);
60 int code = table->lookup_table_codes[lookup];
61 int length = table->lookup_table_length[lookup];
63 if (code == DEHUF_SLOW_PATH) {
64 return read_huffman_symbol_slow_path(table, source);
67 read_bits(source, length);
71 #endif /* !defined(_DEHUFF_H) */