Try to dequantize and IDCT data in the driver.
authorsgunderson@bigfoot.com <>
Sun, 31 May 2009 00:26:38 +0000 (02:26 +0200)
committersgunderson@bigfoot.com <>
Sun, 31 May 2009 00:26:38 +0000 (02:26 +0200)
driver.c

index bbc569e5d143e1b41c35b460f53f1b1c25721863..1411b51311e7efca363b88b038a1c9a634173455 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -1,9 +1,11 @@
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 
 #include "bytesource.h"
 #include "choice.h"
 #include "dehuff.h"
+#include "idct.h"
 #include "input.h"
 #include "zigzag.h"
 
@@ -13,6 +15,7 @@ struct jpeg_image {
        unsigned num_components;
        unsigned hsample[256], vsample[256], qtable[256];
        uint32_t qvalues[256][DCTSIZE2];
+       void* idct_data[256];
 };
 
 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count) 
@@ -28,6 +31,10 @@ void read_dqt(struct byte_source* source, struct jpeg_image* image)
        int precision = precision_table >> 4;  // 0 = 8 bits, otherwise 16 bits.
        int table = precision_table & 0x0f;
 
+       if (image->idct_data[table] != NULL) {
+               idct_choice_free(image->idct_data[table]);
+       }
+
        if (precision != 0) {
                assert(len == 131);
                fprintf(stderr, "Quantization table %u: 16 bits/entry\n", table);
@@ -45,6 +52,8 @@ void read_dqt(struct byte_source* source, struct jpeg_image* image)
                                read_uint8(byte_source_input_func, source);
                }       
        }
+
+       image->idct_data[table] = idct_choice_alloc(image->qvalues[table]);
 }
 
 void read_sof(struct byte_source* source, struct jpeg_image* image)
@@ -113,51 +122,72 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
 
        struct bit_source bits;
        init_bit_source(&bits, byte_source_input_func, source);
+               
+       // Some debugging code.
+       assert(image->width % 8 == 0);
+       int y_stride = image->width;
+       uint8_t* y_row_data = (uint8_t*)malloc(y_stride * DCTSIZE * image->vsample[1]);
+       assert(y_row_data != NULL);
+       int xb = 0;
 
        for ( ;; ) {
                for (unsigned c = 0; c < num_components; ++c) {
                        unsigned cn = component_num[c];
-                       unsigned nc = image->vsample[cn] * image->hsample[cn];
-                       for (unsigned n = 0; n < nc; ++n) {
-                               const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
-                               const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
-
-                               // decode DC component
-                               unsigned dc_category = read_huffman_symbol(dc_table, &bits);
-                               possibly_refill(&bits, dc_category);
-                               last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
-
-               //              printf("dc=%d ac=", last_dc[c]);
-                               putchar(last_dc[c]);
-
-                               // decode AC components
-                               int zz[63] = { 0 };
-                               for (unsigned i = 0; i < 63; ++i) {
-                                       unsigned rs = read_huffman_symbol(ac_table, &bits);
-                                       unsigned r = rs >> 4;
-                                       unsigned s = rs & 0xf;
-
-                                       if (rs == 0x00) {
-                                               /* end of block */
-                                               break;
+                       assert(image->idct_data[image->qtable[cn]] != NULL);
+                       
+                       for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb) {
+                               for (unsigned local_xb = 0; local_xb < image->hsample[cn]; ++local_xb) {
+                                       const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
+                                       const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
+
+                                       // decode DC component
+                                       unsigned dc_category = read_huffman_symbol(dc_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 components
+                                       for (unsigned i = 1; i < DCTSIZE2; ++i) {
+                                               unsigned rs = read_huffman_symbol(ac_table, &bits);
+                                               unsigned r = rs >> 4;
+                                               unsigned s = rs & 0xf;
+
+                                               if (rs == 0x00) {
+                                                       /* end of block */
+                                                       break;
+                                               }
+                                               if (rs == 0xf0) {
+                                                       /* 16 zero coefficients */
+                                                       i += 15;
+                                                       continue;
+                                               }
+
+                                               possibly_refill(&bits, s);
+
+                                               i += r;
+                                               coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
                                        }
-                                       if (rs == 0xf0) {
-                                               /* 16 zero coefficients */
-                                               i += 15;
+                       
+                                       uint8_t pixdata[DCTSIZE2];      
+                                       idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
+
+                                       if (cn != 1) {
                                                continue;
                                        }
 
-                                       possibly_refill(&bits, s);
-
-                                       i += r;
-                                       zz[unzigzag[i]] = extend(read_bits(&bits, s), s);
-                               }
-                               
-                               for (unsigned i = 0; i < 63; ++i) {
-                                       putchar(zz[i]);
-                                       //printf("%d ", zz[i]);
+                                       for (int y = 0; y < DCTSIZE; ++y) {
+                                               memcpy(y_row_data + (local_yb * DCTSIZE + y) * y_stride + (xb + local_xb) * DCTSIZE,
+                                                      pixdata + y * DCTSIZE,
+                                                      DCTSIZE);
+                                       }
                                }
-                               //printf("\n");
+                       }
+
+                       if (cn == 1 && ++xb * DCTSIZE == y_stride) {
+                               fwrite(y_row_data, y_stride * DCTSIZE * image->vsample[1], 1, stdout);
+                               xb = 0;
                        }
                }
        }
@@ -184,6 +214,7 @@ void skip_segment(struct byte_source* source)
 int main(void)
 {
        struct jpeg_image jpeg;
+       memset(&jpeg, 0, sizeof(jpeg));
        init_choices();
 
        struct byte_source source;