]> git.sesse.net Git - fjl/blob - dehuff.h
Add optional padding data at the end to the bit source (is that the right place?...
[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 struct huffman_table {
18         unsigned num_codes[17];     // BITS
19         unsigned char codes[256];   // HUFFVAL
20         
21         // Derived values.
22         unsigned huffsize[256];
23         unsigned huffcode[256];
24         int maxcode[16];
25         int mincode[16];
26         unsigned valptr[16];
27
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.
31
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]; 
36 };
37
38 enum coefficient_class {
39         DC_CLASS = 0,
40         AC_CLASS,
41         NUM_COEFF_CLASSES
42 };
43 typedef struct huffman_table huffman_tables_t[NUM_COEFF_CLASSES][4];
44
45 // Read Huffman tables from a stream, and compute the derived values.
46 void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* userdata);
47
48 unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
49                                        struct bit_source* source);
50
51 static inline unsigned read_huffman_symbol(const struct huffman_table* table,
52                                            struct bit_source* source)
53 {
54         possibly_refill(source, DEHUF_TABLE_BITS);
55         assert(source->bits_available >= DEHUF_TABLE_BITS);
56         unsigned lookup = peek_bits(source, DEHUF_TABLE_BITS);
57         int code = table->lookup_table_codes[lookup];
58         int length = table->lookup_table_length[lookup];
59
60         if (code == DEHUF_SLOW_PATH) {
61                 return read_huffman_symbol_slow_path(table, source);
62         }
63                 
64         read_bits(source, length);
65         return code;
66 }
67
68 // procedure EXTEND (figure F.12)
69
70 // Fast lookup table for (1 << (bits - 1)).
71 // The table actually helps, since the load can go in parallel with the shift
72 // operation below.
73 static const int bit_thresholds[16] = {
74         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
75 };
76
77 static inline unsigned extend(int val, int bits)
78 {
79         if (val < bit_thresholds[bits]) {
80                 return val + (-1 << bits) + 1;
81         } else {
82                 return val;
83         }
84 }
85
86 #endif /* !defined(_DEHUFF_H) */