Skip to content

Replace the libjpeg-derived forward DCT so the crate is MIT OR Apache-2.0 - #43

Draft
nisunze wants to merge 1 commit into
vstroebel:mainfrom
nisunze:clean-room-fdct
Draft

nisunze wants to merge 1 commit into
vstroebel:mainfrom
nisunze:clean-room-fdct

Conversation

@nisunze

@nisunze nisunze commented Oct 30, 2026

Copy link
Copy Markdown

The forward DCT is the only part of the crate derived from libjpeg, and it is why the crate's license is (MIT OR Apache-2.0) AND IJG (#15, #34, #37). Downstream packagers have had to account for the extra IJG terms (Fedora, Chromium). This PR replaces the scalar, wide and AVX2 forward DCTs with an implementation written from the transform's definition in ITU-T T.81 (A.3.3), and drops IJG from the crate's license.

Approach

  • Algorithm. A separable integer transform: a vertical then a horizontal 8-point pass.
    • Each pass splits into an even half over x(n) + x(7-n) and a 4×4 odd half over x(n) - x(7-n).
    • The constants are round(2^13·√2·cos(mπ/16)), with 3 extra fraction bits between the passes.
    • All intermediates fit in an i32.
  • Variants. One generic pass serves the scalar and wide versions. The AVX2 version does the same arithmetic on rows of eight i32 lanes, with two 8×8 transposes. All three round at the same points, so their coefficients are identical; the tests check this.
  • Output. The existing 8·F(u,v) scale and layout are kept, so quantization and everything after it are unchanged.
  • Provenance. I did not use the previous implementation's code. Its scale and layout were established with black-box tests only.

Results

Accuracy against the exact transform (f64), over 50,000 blocks, a quarter of them at the sample extremes:

before after
worst error 1.334 0.937
coefficients not the nearest integer 13.66% 7.36%

Image quality on a 512×512 synthetic RGB image (gradients, texture, edges), encoded and then decoded with jpeg-decoder:

quality size before size after PSNR before PSNR after
50 25,535 25,555 32.807 dB 32.805 dB
75 36,696 36,701 33.663 dB 33.664 dB
90 94,056 94,039 40.954 dB 40.955 dB

Speed: per-block DCT time in a release build, two interleaved runs on a busy machine, so treat it as indicative:

before after
scalar 70.4 / 81.2 ns 70.7 / 58.5 ns
AVX2 33.1 / 33.6 ns 31.3 / 20.6 ns

Tests

  • New tests.
    • The constants follow from the definition.
    • The scalar transform is within 1 of the exact transform on extreme and random blocks.
    • wide and AVX2 match scalar exactly.
  • Existing checks.
    • cargo test passes with default features, simd, use_wide and --no-default-features.
    • Clippy adds no warnings.
    • wasm32-unknown-unknown builds.

Notes

🤖 Generated with Claude Code

…-2.0

The forward DCT (scalar, `wide` and AVX2) was ported from libjpeg, which is
why the crate carries the IJG license next to MIT/Apache-2.0 (vstroebel#15, vstroebel#34, vstroebel#37).
This replaces all three with an implementation written from the transform's
definition in ITU-T T.81 (A.3.3), and drops IJG from the crate's license.

- One separable integer algorithm: a vertical then a horizontal 8-point
  pass, each an even half over x(n) + x(7-n) and a 4x4 odd half over
  x(n) - x(7-n), with constants round(2^13 * sqrt(2) * cos(m*pi/16)) and 3
  extra fraction bits between the passes. Every intermediate fits in an i32.
- The scalar, `wide` and AVX2 versions round at the same points and produce
  identical coefficients (tested). The output keeps the 8*F(u,v) scale the
  quantization tables expect.

Accuracy against the exact transform over 50,000 blocks (a quarter at the
sample extremes): worst error 0.937 (was 1.334); coefficients that are not
the nearest integer 7.36% (was 13.66%). A 512x512 test image encodes with
PSNR within 0.002 dB and size within 0.08% at qualities 50, 75 and 90.
Speed is unchanged or better.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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