]> git.sesse.net Git - fjl/blobdiff - driver.c
Fix some errors/warnings in driver.c.
[fjl] / driver.c
index 5b4545343ddcc4504a09b51d8dc27a4a225bd19c..6dcc8b3da676161ecd60e05898a27ebe3d19bf3f 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -114,6 +114,50 @@ 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)
+{
+       for (unsigned i = 1; i < DCTSIZE2; ++i) {
+               possibly_refill(bits, DEHUF_AC_TABLE_BITS);
+               unsigned lookup = peek_bits(bits, DEHUF_AC_TABLE_BITS);
+               int code = tbl->ac_table_codes[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;
+                       possibly_refill(bits, s);
+
+                       if (rs == 0x00) {
+                               assert(code == AC_DEHUF_SLOW_PATH || code == AC_END_OF_BLOCK);
+                               /* end of block */
+                               break;
+                       }
+                       if (rs == 0xf0) {
+                               assert(code == AC_DEHUF_SLOW_PATH || code == AC_SIXTEEN_ZEROS);
+                               /* 16 zero coefficients */
+                               continue;
+                       }
+
+                       coeff[unzigzag[i]] = extend(read_bits(bits, s), s);
+               } else {
+                       unsigned length = tbl->ac_table_length[lookup];
+                       int r = tbl->ac_table_skip[lookup];
+                       assert(r >= 0);
+                       i += r;
+                       assert(bits->bits_available >= length);
+                       read_bits(bits, length);
+                       if (code == AC_END_OF_BLOCK) {
+                               break;
+                       }
+                       if (code == AC_SIXTEEN_ZEROS) {
+                               continue;
+                       }
+                       coeff[unzigzag[i]] = code;
+               }
+       }
+}
+
 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
 {
        unsigned len = read_uint16(byte_source_input_func, source);
@@ -169,32 +213,13 @@ 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 + DEHUF_TABLE_BITS);
+                                       possibly_refill(&bits, dc_category);
                                        last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
                                        
                                        int16_t coeff[DCTSIZE2] = { 0 };
                                        coeff[0] = last_dc[c];
+                                       decode_ac_coefficients(ac_table, &bits, coeff);
 
-                                       // decode AC components
-                                       for (unsigned i = 1; i < DCTSIZE2; ++i) {
-                                               unsigned rs = read_huffman_symbol_no_refill(ac_table, &bits);
-                                               unsigned r = rs >> 4;
-                                               unsigned s = rs & 0xf;
-                                               i += r;
-                                               possibly_refill(&bits, s + DEHUF_TABLE_BITS);
-
-                                               if (rs == 0x00) {
-                                                       /* end of block */
-                                                       break;
-                                               }
-                                               if (rs == 0xf0) {
-                                                       /* 16 zero coefficients */
-                                                       continue;
-                                               }
-
-                                               coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
-                                       }
-                       
                                        uint8_t pixdata[DCTSIZE2];      
                                        idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);