X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=dehuff.c;h=695da0bdc78ee2d863cbb0fa547b628619ffc4ab;hp=c4a65eea8a0619153bb9910cd5421db53bd01193;hb=1cfb04470add4e31406785980e7ce14a8e9d0672;hpb=03497d9a2c6de19c6b8a0761b2703ca61b174e2a diff --git a/dehuff.c b/dehuff.c index c4a65ee..695da0b 100644 --- a/dehuff.c +++ b/dehuff.c @@ -119,6 +119,50 @@ void read_huffman_tables(huffman_tables_t* dst, input_func_t* input_func, void* } } } + + // Generate the AC lookup tables. + for (unsigned i = 0; i < DEHUF_AC_TABLE_SIZE; ++i) { + tbl->ac_table_codes[i] = AC_DEHUF_SLOW_PATH; + tbl->ac_table_length[i] = AC_DEHUF_SLOW_PATH; + tbl->ac_table_skip[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] = 0; + continue; + } + if (rs == 0xf0) { + // 16 zero coefficients. + tbl->ac_table_codes[i] = AC_SIXTEEN_ZEROS; + tbl->ac_table_length[i] = length; + tbl->ac_table_skip[i] = 15; + 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; + + assert(tbl->ac_table_length[i] <= DEHUF_AC_TABLE_BITS); + } } free(buf);