]> git.sesse.net Git - fjl/blob - dehuff.h
Fix some errors/warnings in driver.c.
[fjl] / dehuff.h
1 #ifndef _DEHUFF_H
2 #define _DEHUFF_H 1
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <sys/types.h>
7
8 #include "bytesource.h"
9 #include "bitsource.h"
10
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;
16
17 // About 98% of all AC coefficients (control byte + coefficient) are <= 10 bits
18 // long; again, see codelen.txt. This will cost us about 6 kB of data to store
19 // in L1 cache.
20 #define DEHUF_AC_TABLE_BITS 10
21 #define DEHUF_AC_TABLE_SIZE (1 << DEHUF_AC_TABLE_BITS)
22 static const int AC_DEHUF_SLOW_PATH = 0xf0000000;
23 static const int AC_END_OF_BLOCK = 0xf0000001;
24 static const int AC_SIXTEEN_ZEROS = 0xf0000002;
25
26 struct huffman_table {
27         unsigned num_codes[17];     // BITS
28         unsigned char codes[256];   // HUFFVAL
29         
30         // Derived values.
31         unsigned huffsize[256];
32         unsigned huffcode[256];
33         int maxcode[16];
34         int mincode[16];
35         unsigned valptr[16];
36
37         // Lookup table for fast decoding; given eight bits,
38         // return the symbol and length in bits. For longer codes,
39         // DEHUF_SLOW_PATH is returned.
40
41         // Note that the codes we return are 8-bit, but the type of
42         // the lookup tables is int to avoid extra zero extending. 
43         int lookup_table_codes[DEHUF_TABLE_SIZE]; 
44         int lookup_table_length[DEHUF_TABLE_SIZE]; 
45         
46         // Further lookup tables for decoding AC coefficients.
47         // (Generated but obviously not used for DC coefficients.)
48         // Maps from 10-bit lookahead values to the signed coeffient (_codes),
49         // number of bits to skip (_length) and the number of zero coefficients
50         // after this one (_skip).
51         int ac_table_codes[DEHUF_AC_TABLE_SIZE]; 
52         uint8_t ac_table_length[DEHUF_AC_TABLE_SIZE]; 
53         uint8_t ac_table_skip[DEHUF_AC_TABLE_SIZE]; 
54 };
55
56 enum coefficient_class {
57         DC_CLASS = 0,
58         AC_CLASS,
59         NUM_COEFF_CLASSES
60 };
61 typedef struct huffman_table huffman_tables_t[NUM_COEFF_CLASSES][4];
62
63 // Read Huffman tables from a stream, and compute the derived values.
64 void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* userdata);
65
66 unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
67                                        struct bit_source* source);
68
69 static inline unsigned read_huffman_symbol_no_refill(
70         const struct huffman_table* table,
71         struct bit_source* source)
72 {
73         assert(source->bits_available >= DEHUF_TABLE_BITS);
74         unsigned lookup = peek_bits(source, DEHUF_TABLE_BITS);
75         int code = table->lookup_table_codes[lookup];
76         int length = table->lookup_table_length[lookup];
77
78         if (code == DEHUF_SLOW_PATH) {
79                 return read_huffman_symbol_slow_path(table, source);
80         }
81                 
82         read_bits(source, length);
83         return code;
84 }
85
86 static inline unsigned read_huffman_symbol(const struct huffman_table* table,
87                                            struct bit_source* source)
88 {
89         possibly_refill(source, DEHUF_TABLE_BITS);
90         return read_huffman_symbol_no_refill(table, source);
91 }
92
93 // procedure EXTEND (figure F.12)
94
95 // Fast lookup table for (1 << (bits - 1)).
96 // The table actually helps, since the load can go in parallel with the shift
97 // operation below.
98 static const int bit_thresholds[16] = {
99         0, 1 << 0, 1 << 1, 1 << 2, 1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 7, 1 << 8, 1 << 9, 1 << 10, 1 << 11, 1 << 12, 1 << 13, 1 << 14
100 };
101
102 static inline unsigned extend(int val, unsigned bits)
103 {
104 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
105         // GCC should ideally be able to figure out that the conditional move is better, but
106         // it doesn't for various reasons, and this is pretty important for speed, so we hardcode.
107         asm("cmp %1, %0 ; cmovl %2, %0"
108                 : "+r" (val)
109                 : "g" (bit_thresholds[bits]),
110                   "r" (val + (-1 << bits) + 1)
111                 : "cc");
112         return val;
113 #else
114         if (val < bit_thresholds[bits]) {
115                 return val + (-1 << bits) + 1;
116         } else {
117                 return val;
118         }
119 #endif
120 }
121
122 #endif /* !defined(_DEHUFF_H) */