Save one refill from inner coefficient-reading loop.
authorsgunderson@bigfoot.com <>
Sun, 31 May 2009 22:38:50 +0000 (00:38 +0200)
committersgunderson@bigfoot.com <>
Sun, 31 May 2009 22:38:50 +0000 (00:38 +0200)
dehuff.h
driver.c

index a6a431a..9243e10 100644 (file)
--- a/dehuff.h
+++ b/dehuff.h
@@ -48,10 +48,10 @@ void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void*
 unsigned read_huffman_symbol_slow_path(const struct huffman_table* table,
                                        struct bit_source* source);
 
-static inline unsigned read_huffman_symbol(const struct huffman_table* table,
-                                           struct bit_source* source)
+static inline unsigned read_huffman_symbol_no_refill(
+       const struct huffman_table* table,
+       struct bit_source* source)
 {
-       possibly_refill(source, DEHUF_TABLE_BITS);
        assert(source->bits_available >= DEHUF_TABLE_BITS);
        unsigned lookup = peek_bits(source, DEHUF_TABLE_BITS);
        int code = table->lookup_table_codes[lookup];
@@ -65,6 +65,13 @@ static inline unsigned read_huffman_symbol(const struct huffman_table* table,
        return code;
 }
 
+static inline unsigned read_huffman_symbol(const struct huffman_table* table,
+                                           struct bit_source* source)
+{
+       possibly_refill(source, DEHUF_TABLE_BITS);
+       return read_huffman_symbol_no_refill(table, source);
+}
+
 // procedure EXTEND (figure F.12)
 
 // Fast lookup table for (1 << (bits - 1)).
index 04e2369..6db737f 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -164,7 +164,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
 
                                        // decode DC component
                                        unsigned dc_category = read_huffman_symbol(dc_table, &bits);
-                                       possibly_refill(&bits, dc_category);
+                                       possibly_refill(&bits, dc_category + DEHUF_TABLE_BITS);
                                        last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
                                        
                                        int16_t coeff[DCTSIZE2] = { 0 };
@@ -172,7 +172,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
 
                                        // decode AC components
                                        for (unsigned i = 1; i < DCTSIZE2; ++i) {
-                                               unsigned rs = read_huffman_symbol(ac_table, &bits);
+                                               unsigned rs = read_huffman_symbol_no_refill(ac_table, &bits);
                                                unsigned r = rs >> 4;
                                                unsigned s = rs & 0xf;
 
@@ -182,12 +182,13 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                                                }
                                                if (rs == 0xf0) {
                                                        /* 16 zero coefficients */
+                                                       possibly_refill(&bits, DEHUF_TABLE_BITS);
                                                        i += 15;
                                                        continue;
                                                }
                                                i += r;
 
-                                               possibly_refill(&bits, s);
+                                               possibly_refill(&bits, s + DEHUF_TABLE_BITS);
                                                coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
                                        }