X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=dehuff.c;h=5574e27184b8de5961b01f1f76cbf0e27688c89b;hb=4adcb962d555cba7af8e1de42d203f03d28d81a5;hp=af16b512a83c0f796fd24ff8a499e15fe3719817;hpb=8a495c6c9b595e9f188ef703d71fa868cbb1605d;p=fjl diff --git a/dehuff.c b/dehuff.c index af16b51..5574e27 100644 --- a/dehuff.c +++ b/dehuff.c @@ -114,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] = 0; + tbl->ac_table_length[i] = length; + tbl->ac_table_skip[i] = 64; + 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);