X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=driver.c;h=94cce90332e7df8186c83b0aa311ef3afec61d6f;hp=20d64f2cd0c9c062e775fb0c66aad4dd6be4b797;hb=HEAD;hpb=90cfe68276c3639bb340956754dd726284f7b822 diff --git a/driver.c b/driver.c index 20d64f2..94cce90 100644 --- a/driver.c +++ b/driver.c @@ -5,19 +5,11 @@ #include "bytesource.h" #include "choice.h" #include "dehuff.h" +#include "driver.h" #include "idct.h" #include "input.h" #include "zigzag.h" -struct jpeg_image { - unsigned precision; - unsigned width, height; - 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) { return fread(buf, 1, count, (FILE*)userdata); @@ -62,8 +54,8 @@ void read_sof(struct byte_source* source, struct jpeg_image* image) assert(len >= 8); image->precision = read_uint8(byte_source_input_func, source); assert(image->precision == 8); - image->width = read_uint16(byte_source_input_func, source); image->height = read_uint16(byte_source_input_func, source); + image->width = read_uint16(byte_source_input_func, source); image->num_components = read_uint8(byte_source_input_func, source); len -= 8; @@ -79,9 +71,68 @@ void read_sof(struct byte_source* source, struct jpeg_image* image) image->qtable[c] = read_uint8(byte_source_input_func, source); len -= 3; + if (image->hsample[c] > image->max_hsample) { + image->max_hsample = image->hsample[c]; + } + if (image->vsample[c] > image->max_vsample) { + image->max_vsample = image->vsample[c]; + } + fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n", c, image->hsample[c], image->vsample[c], image->qtable[c]); } + + image->num_blocks_horizontal = (image->width + image->max_hsample * DCTSIZE - 1) / (image->max_hsample * DCTSIZE); + image->num_blocks_vertical = (image->height + image->max_vsample * DCTSIZE - 1) / (image->max_vsample * DCTSIZE); + + for (unsigned c = 0; c < 256; ++c) { + if (image->hsample[c] == 0) { + continue; + } + + unsigned width = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE; + unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE; + image->stride[c] = width; + image->pixel_data[c] = (uint8_t*)malloc(width * height); + assert(image->pixel_data[c] != NULL); + image->pixel_write_pointer[c] = image->pixel_data[c]; + + fprintf(stderr, "Component %u: allocating %d x %d\n", c, width, height); + } +} + +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; ) { + 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 (code == AC_DEHUF_SLOW_PATH) { + unsigned rs = read_huffman_symbol_no_refill(tbl, bits); + if (rs == 0x00) { + /* end of block */ + break; + } + + 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 { + assert(bits->bits_available >= length); + read_bits(bits, length); + possibly_refill(bits, DEHUF_AC_TABLE_BITS); + + assert(r >= 1); + i += r; + coeff[unzigzag[i]] = code; + } + } } void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables) @@ -121,22 +172,19 @@ 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); + init_bit_source(&bits, byte_source_input_func, 8, 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; + unsigned mcu_x = 0, mcu_y = 0; - for ( ;; ) { + while (!bits.source_eof) { for (unsigned c = 0; c < num_components; ++c) { unsigned cn = component_num[c]; 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) { + + uint8_t* pixel_write_pointer_y = image->pixel_write_pointer[cn]; + for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb, pixel_write_pointer_y += image->stride[cn] * DCTSIZE) { + uint8_t* pixel_write_pointer = pixel_write_pointer_y; + for (unsigned local_xb = 0; local_xb < image->hsample[cn]; ++local_xb, pixel_write_pointer += DCTSIZE) { 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]]); @@ -147,52 +195,30 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab 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(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); - } - uint8_t pixdata[DCTSIZE2]; idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata); - if (cn != 1) { - continue; - } - - 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); + uint8_t* dest_pixdata = pixel_write_pointer; + for (unsigned y = 0; y < DCTSIZE; ++y, dest_pixdata += image->stride[cn]) { + memcpy(dest_pixdata, pixdata + y * DCTSIZE, DCTSIZE); } } } - - if (cn != 1) { - continue; + image->pixel_write_pointer[cn] += DCTSIZE * image->hsample[cn]; + } + + if (++mcu_x == image->num_blocks_horizontal) { + ++mcu_y; + mcu_x = 0; + + for (unsigned c = 0; c < num_components; ++c) { + unsigned cn = component_num[c]; + image->pixel_write_pointer[cn] += (image->vsample[cn] * DCTSIZE - 1) * image->stride[cn]; } - - xb += image->hsample[cn]; - if (xb * DCTSIZE == y_stride) { - fwrite(y_row_data, y_stride * DCTSIZE * image->vsample[cn], 1, stdout); - xb = 0; + if (mcu_y == image->num_blocks_vertical) { + return; } } } @@ -216,14 +242,12 @@ void skip_segment(struct byte_source* source) } } -int main(void) +void read_jpeg(struct jpeg_image* jpeg, FILE *input) { - struct jpeg_image jpeg; - memset(&jpeg, 0, sizeof(jpeg)); - init_choices(); - + memset(jpeg, 0, sizeof(*jpeg)); + struct byte_source source; - init_byte_source(&source, stdio_read, stdin); + init_byte_source(&source, stdio_read, input); huffman_tables_t tables; @@ -261,22 +285,25 @@ int main(void) break; case 0xdb: /* DQT */ - read_dqt(&source, &jpeg); + read_dqt(&source, jpeg); break; case 0xc0: /* SOF0 (baseline DCT, Huffman encoded) */ - read_sof(&source, &jpeg); + read_sof(&source, jpeg); break; case 0xd8: /* SOI */ break; + case 0xd9: + /* EOI */ + return; case 0xc4: /* DHT (define Huffman tables) */ read_huffman_tables(&tables, byte_source_input_func, &source); break; case 0xda: /* SOS (start of scan) */ - read_scan(&source, &jpeg, &tables); + read_scan(&source, jpeg, &tables); break; default: fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2); @@ -284,3 +311,18 @@ int main(void) } } } + +int main(void) +{ + init_choices(); + + struct jpeg_image jpeg; + read_jpeg(&jpeg, stdin); + + // Some debug code. + const int c = 1; + unsigned stride = jpeg.num_blocks_horizontal * jpeg.hsample[c] * DCTSIZE; + unsigned height = jpeg.num_blocks_vertical * jpeg.vsample[c] * DCTSIZE; + printf("P5\n%u %u\n255\n", stride, height); + fwrite(jpeg.pixel_data[c], stride * height, 1, stdout); +}