Skip to content

Build optimized Huffman tables for any code depth - #46

Open
sunsided wants to merge 1 commit into
vstroebel:mainfrom
sunsided:feature/huffman-code-depth
Open

sunsided wants to merge 1 commit into
vstroebel:mainfrom
sunsided:feature/huffman-code-depth

Conversation

@sunsided

Copy link
Copy Markdown

HuffmanTable::new_optimized follows T.81 K.2–K.4 literally: the code-length histogram bits has 33 entries, and the K.4 sort only visits sizes 1..=32. Skewed symbol counts build deeper trees than that. With 33 symbols whose counts follow a tie-free near-Fibonacci sequence (f(n) = f(n-1) + f(n-2) + 1, about 18.5M symbols in total, i.e. a large image with adversarial content), K.1 produces codes longer than 32 bits and bits[size] += 1 indexes out of bounds. That panics in both debug and release builds. libjpeg reports an error in this case.

An exact Fibonacci sequence does not trigger it, because the K.1 tie-breaking rebalances the tree.

Separately, freq[v1] += freq[v2] overflows (debug panic) once a table's total count exceeds u32::MAX.

Change

  • Merge counts in u64 (freq.map(u64::from)).
  • Size bits for the deepest tree 257 symbols can form ([0u16; 257]), start K.3 at the last index and sort over every size in K.4. K.3 then limits any depth to 16 bits, as intended.

Tests

This adds proptest as a dev-dependency (it builds and passes on 1.89). The new optimized_props module checks every generated table for:

  • sum(length) == values.len()
  • Kraft sum strictly below 1, so the all-ones code stays reserved
  • every symbol with a nonzero count has a code of 1..=16 bits that is not all ones
  • the codes are prefix-free

These are run over uniform and heavily skewed AC counts and arbitrary DC counts. Two fixed cases, fibonacci_depth and total_over_u32, fail on main and pass with the fix.

cargo fmt --check and cargo test pass with --features simd, with --features use_wide, with --no-default-features --tests, and in --release, also on 1.89.

`HuffmanTable::new_optimized` follows T.81 K.2-K.4 literally, which
sizes the code length histogram at 33 and sorts symbols by sizes 1..=32.
Skewed counts build deeper trees than that: 33 symbols with tie-free
near-Fibonacci counts (f(n) = f(n-1) + f(n-2) + 1, ~18.5M total) index
past `bits[33]` and panic in debug and release builds. Exact Fibonacci
counts do not trigger it, because the K.1 tie-breaking rebalances them.

Size the histogram for the deepest tree 257 symbols can form and sort
over every size, so K.3 limits any depth to 16 bits. Also merge counts
in u64: `freq[v1] += freq[v2]` overflowed (debug panic) once a table's
total count exceeded u32::MAX.

Adds proptest as a dev-dependency for property tests of the generated
tables (lengths sum to the value count, Kraft sum below 1 so the all-ones
code stays reserved, every used symbol has a code of 1..=16 bits, codes
are prefix-free), plus the two failing cases as fixed tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant