X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=dehuff.c;h=f08dd881adee0bab2256f8b171f1423821a1f331;hp=9b296a798075bf6f6f01e2bd439c8c4b7da1bba2;hb=8326dff1cbad02c5d660696648e15a0f575c9afd;hpb=cc0c0e56d8269e91f23739b57faf67d5b5cc0794 diff --git a/dehuff.c b/dehuff.c index 9b296a7..f08dd88 100644 --- a/dehuff.c +++ b/dehuff.c @@ -4,40 +4,11 @@ #include "bytesource.h" #include "dehuff.h" - -void reliable_read(raw_input_func_t* input_func, void* userdata, uint8_t* buf, size_t len) -{ - while (len > 0) { - ssize_t bytes_read = input_func(userdata, buf, len); - assert(bytes_read <= (ssize_t)len); - - // TODO: We need better error handling here. setjmp()/longjmp() - // should hopefully do the trick, but we need to take care for - // suspension. - if (bytes_read == (ssize_t)-1) { - fprintf(stderr, "Input function returned error\n"); - exit(1); - } - if (bytes_read == 0) { - fprintf(stderr, "Premature EOF\n"); - exit(1); - } - - buf += bytes_read; - len -= bytes_read; - } -} - -uint16_t read_length(raw_input_func_t* input_func, void* userdata) -{ - uint8_t buf[2]; - reliable_read(input_func, userdata, buf, 2); - return (buf[0] << 8) | buf[1]; -} +#include "input.h" void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* userdata) { - size_t len = read_length(input_func, userdata); + size_t len = read_uint16(input_func, userdata); assert(len > 2); len -= 2; @@ -61,7 +32,7 @@ void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* exit(1); } - struct huffman_table* tbl = dst[table_class][table_dest]; + struct huffman_table* tbl = &((*dst)[table_class][table_dest]); if (len < 16) { fprintf(stderr, "Short read for num_codes\n"); exit(1); @@ -143,11 +114,53 @@ void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* for (unsigned elem = prefix_min; elem < prefix_max; ++elem) { assert(tbl->lookup_table_codes[elem] == DEHUF_SLOW_PATH); assert(tbl->lookup_table_length[elem] == DEHUF_SLOW_PATH); - tbl->lookup_table_codes[elem] = k; + tbl->lookup_table_codes[elem] = tbl->codes[k]; tbl->lookup_table_length[elem] = length; } } } + + // Generate the AC lookup tables. + for (unsigned i = 0; i < DEHUF_AC_TABLE_SIZE; ++i) { + tbl->ac_table_codes[i] = AC_DEHUF_SLOW_PATH; + + int lookup = i >> (DEHUF_AC_TABLE_BITS - DEHUF_TABLE_BITS); + int rs = tbl->lookup_table_codes[lookup]; + unsigned length = tbl->lookup_table_length[lookup]; + if (rs == DEHUF_SLOW_PATH) { + // Not enough bits to decode even the length. + continue; + } + if (rs == 0x00) { + // End of block. + tbl->ac_table_codes[i] = AC_END_OF_BLOCK; + tbl->ac_table_length[i] = length; + tbl->ac_table_skip[i] = 1; + continue; + } + if (rs == 0xf0) { + // 16 zero coefficients. + tbl->ac_table_codes[i] = 0; + tbl->ac_table_length[i] = length; + tbl->ac_table_skip[i] = 16; + continue; + } + + unsigned r = rs >> 4; + unsigned s = rs & 0xf; + if (s > DEHUF_AC_TABLE_BITS - length) { + // Not enough bits to decode this coefficient. + continue; + } + + unsigned bits = (i >> (DEHUF_AC_TABLE_BITS - length - s)) & ((1 << s) - 1); + + tbl->ac_table_codes[i] = extend(bits, s); + tbl->ac_table_length[i] = length + s; + tbl->ac_table_skip[i] = r + 1; + + assert(tbl->ac_table_length[i] <= DEHUF_AC_TABLE_BITS); + } } free(buf);