Skip to content

Deduplicate restart, buffer and Huffman counting code in the encoder - #49

Draft
sunsided wants to merge 2 commits into
vstroebel:mainfrom
sunsided:refactor/encoder-dedup
Draft

sunsided wants to merge 2 commits into
vstroebel:mainfrom
sunsided:refactor/encoder-dedup

Conversation

@sunsided

Copy link
Copy Markdown

Builds on #47. The first commit here is #47's. Only 91f4489 is new. I'll rebase once #47 merges.

This is a refactor with no behavior change. Over a grid of 768 combinations (4 color types × 4 sampling factors × {sequential, 2, 4, 64 scans} × optimized on/off × restart {off, 1, 5} × nearest/average subsampling), the output bytes are identical to #47.

  • RestartState replaces the restart-marker bookkeeping (restarts_to_go, restarts &= 7, …) that was pasted into all four scan loops. before_mcu writes the marker when one is due and returns whether the caller must reset its DC prediction.
  • init_buffers<T> replaces init_rows and init_block_buffers, which were identical apart from the element type.
  • optimize_huffman_table loops over the same (start, end) ranges the scans are written with: [(1, 64)] for sequential, ac_scan_ranges for progressive. Before, it had two near-copies of the AC counting loop. count_ac_symbols mirrors JfifWriter::write_ac_block.
  • get_num_bits is removed in favor of get_code, which it duplicated. Its only test checked that the two agree.

encoder.rs: +98 / −216.

Note: this conflicts with #42, which fixes the DC counting at restart markers inside optimize_huffman_table. The refactor keeps today's behavior on purpose. If #42 lands first, I'll rebase and carry its reset over (n % restart_interval == 0 → prev_dc = 0 in the DC loop).

With set_progressive_scans(n) for n in 34..=64, each AC scan was given
64 / (n - 1) = 1 coefficient, and the first scan started at
max(0, 1) = 1 but ended at 1. Its header was written with Ss=1, Se=0,
which decoders reject ("invalid spectral selection parameters"), so the
whole file failed to decode even though the setter documents 2..=64 as
valid.

The ranges are now computed once in ac_scan_ranges, which splits the 63
AC coefficients evenly over n - 1 scans: [1 + k*63/(n-1),
1 + (k+1)*63/(n-1)). The writer and the optimized Huffman counting both
use it, so they cannot drift apart. The default 4 scans now split at
1/22/43 instead of 1/21/42.
No behavior change; output is byte-identical across a grid of 768
color type / sampling / progressive / optimized / restart / averaging
combinations.

- RestartState replaces the restart-marker bookkeeping that was pasted
  into all four scan loops.
- init_buffers<T> replaces the identical init_rows and
  init_block_buffers.
- optimize_huffman_table iterates the same (start, end) ranges the
  scans are written with ([(1, 64)] for sequential, ac_scan_ranges for
  progressive) instead of two near-copies of the AC counting loop, and
  count_ac_symbols mirrors JfifWriter::write_ac_block.
- get_num_bits is removed in favor of get_code, which it duplicated (its
  only test asserted the two agree).
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