Kill AC_END_OF_BLOCK as well.
authorSteinar H. Gunderson <sesse@debian.org>
Mon, 1 Jun 2009 18:37:35 +0000 (20:37 +0200)
committerSteinar H. Gunderson <sesse@debian.org>
Mon, 1 Jun 2009 18:37:35 +0000 (20:37 +0200)
dehuff.c
dehuff.h
driver.c
zigzag.h

index f08dd88..5574e27 100644 (file)
--- a/dehuff.c
+++ b/dehuff.c
@@ -133,9 +133,9 @@ void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void*
                        }
                        if (rs == 0x00) {
                                // End of block.
-                               tbl->ac_table_codes[i] = AC_END_OF_BLOCK;
+                               tbl->ac_table_codes[i] = 0;
                                tbl->ac_table_length[i] = length;
-                               tbl->ac_table_skip[i] = 1;
+                               tbl->ac_table_skip[i] = 64;
                                continue;
                        }
                        if (rs == 0xf0) {
index 92c2aff..c54f68a 100644 (file)
--- a/dehuff.h
+++ b/dehuff.h
@@ -20,7 +20,6 @@ static const int DEHUF_SLOW_PATH = -1;
 #define DEHUF_AC_TABLE_BITS 10
 #define DEHUF_AC_TABLE_SIZE (1 << DEHUF_AC_TABLE_BITS)
 static const int AC_DEHUF_SLOW_PATH = 0xf0000000;
-static const int AC_END_OF_BLOCK = 0xf0000001;
 
 struct huffman_table {
        unsigned num_codes[17];     // BITS
index f24b0f5..f07e63e 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -116,38 +116,33 @@ void read_sof(struct byte_source* source, struct jpeg_image* image)
 
 void decode_ac_coefficients(const struct huffman_table* tbl, struct bit_source* bits, int16_t* coeff)
 {
+       possibly_refill(bits, DEHUF_AC_TABLE_BITS);
        for (unsigned i = 0; i < DCTSIZE2 - 1; ) {
-               possibly_refill(bits, DEHUF_AC_TABLE_BITS);
                unsigned lookup = peek_bits(bits, DEHUF_AC_TABLE_BITS);
                int code = tbl->ac_table_codes[lookup];
+               unsigned length = tbl->ac_table_length[lookup];
+               unsigned r = tbl->ac_table_skip[lookup];
 
                if (__builtin_expect(code == AC_DEHUF_SLOW_PATH, 0)) {
                        unsigned rs = read_huffman_symbol_no_refill(tbl, bits);
-                       unsigned r = rs >> 4;
-                       unsigned s = rs & 0xf;
-                       i += r + 1;
-                       possibly_refill(bits, s);
-
                        if (rs == 0x00) {
                                /* end of block */
                                break;
                        }
-                       if (rs == 0xf0) {
-                               /* 16 zero coefficients */
-                               continue;
-                       }
 
+                       unsigned r = rs >> 4;
+                       unsigned s = rs & 0xf;
+                       i += r + 1;
+                       possibly_refill(bits, s);
                        coeff[unzigzag[i]] = extend(read_bits(bits, s), s);
+                       possibly_refill(bits, DEHUF_AC_TABLE_BITS);
                } else {
-                       unsigned length = tbl->ac_table_length[lookup];
-                       int r = tbl->ac_table_skip[lookup];
-                       assert(r >= 1);
-                       i += r;
                        assert(bits->bits_available >= length);
                        read_bits(bits, length);
-                       if (code == AC_END_OF_BLOCK) {
-                               break;
-                       }
+                       possibly_refill(bits, DEHUF_AC_TABLE_BITS);
+
+                       assert(r >= 1);
+                       i += r;
                        coeff[unzigzag[i]] = code;
                }
        }
index 3fc1377..5d9df28 100644 (file)
--- a/zigzag.h
+++ b/zigzag.h
@@ -4,10 +4,10 @@
 #include "idct.h"
 
 // Table for transforming from zig-zag order to natural order.
-// We use the same trick as libjpeg here; there are 16 extra entries
+// We use the same trick as libjpeg here; there are 64 extra entries
 // after the end of the table, since the run-length decoder could
 // potentially cause entries indices >= 64 to be decoded.
-static const unsigned unzigzag[DCTSIZE2 + 16] = {
+static const unsigned unzigzag[DCTSIZE2 * 2] = {
        // Regular entries.
         0,  1,  8, 16,  9,  2,  3, 10,
        17, 24, 32, 25, 18, 11,  4,  5,
@@ -18,7 +18,14 @@ static const unsigned unzigzag[DCTSIZE2 + 16] = {
        58, 59, 52, 45, 38, 31, 39, 46,
        53, 60, 61, 54, 47, 55, 62, 63,
 
-       // Extra padding entries; should never be referenced in well-formed data.
+       // Extra padding entries.
+       // May get referenced by malformed data or during end-of-block processing.
+       63, 63, 63, 63, 63, 63, 63, 63,
+       63, 63, 63, 63, 63, 63, 63, 63,
+       63, 63, 63, 63, 63, 63, 63, 63,
+       63, 63, 63, 63, 63, 63, 63, 63,
+       63, 63, 63, 63, 63, 63, 63, 63,
+       63, 63, 63, 63, 63, 63, 63, 63,
        63, 63, 63, 63, 63, 63, 63, 63,
        63, 63, 63, 63, 63, 63, 63, 63,
 };