]> git.sesse.net Git - fjl/blobdiff - dehuff.c
Add code for efficient (?) Huffman decoding.
[fjl] / dehuff.c
index 3e0fd6c4ec86ab441ca36c921cb134d1cf842228..0a9fbbcf1a4bcf1b2eb9f413eca7f1784503397e 100644 (file)
--- a/dehuff.c
+++ b/dehuff.c
@@ -121,7 +121,53 @@ void read_huffman_tables(huffman_tables_t* dst, raw_input_func_t* input_func, vo
                        j += tbl->num_codes[i];
                        tbl->maxcode[i] = tbl->huffcode[j - 1];
                }
+
+               // Generate the lookup tables
+               for (unsigned i = 0; i < DEHUF_TABLE_SIZE; ++i) {
+                       tbl->lookup_table_codes[i] = DEHUF_SLOW_PATH;
+                       tbl->lookup_table_length[i] = DEHUF_SLOW_PATH;
+               }
+       
+               unsigned k = 0; 
+               for (unsigned i = 0; i < 16; ++i) {
+                       for (unsigned j = 0; j < tbl->num_codes[i]; ++j, ++k) {
+                               const unsigned code = tbl->huffcode[k];
+                               const unsigned length = tbl->huffsize[k];
+                               if (length > DEHUF_TABLE_BITS) {
+                                       continue;
+                               }
+                               const unsigned prefix_min = code << (DEHUF_TABLE_BITS - length);
+                               const unsigned prefix_max = ((code + 1) << (DEHUF_TABLE_BITS - length));
+
+                               for (unsigned elem = prefix_min; elem < prefix_max; ++elem) {
+                                       assert(tbl->lookup_table_codes[elem] == DEHUF_SLOW_PATH);
+                                       assert(tbl->lookup_table_length[elem] == DEHUF_SLOW_PATH);
+                                       tbl->lookup_table_codes[elem] = k;
+                                       tbl->lookup_table_length[elem] = length;
+                               }
+                       }
+               }       
        }
 
        free(buf);
 }
+
+unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
+                                       struct bit_source* source) {
+       possibly_refill(source, 1);
+       unsigned code = read_bits(source, 1);
+       int i = 0;
+
+       while (code > table->maxcode[i] || table->maxcode[i] == -1) {
+               possibly_refill(source, 1);
+               code = (code << 1) | read_bits(source, 1);
+               ++i;
+
+               if (i > 15) {
+                       fprintf(stderr, "Error in Huffman decoding: Too long code (%x)\n", code);
+                       exit(1);
+               }
+       }
+
+       return table->codes[table->valptr[i] + code - table->mincode[i]];
+}