Skip to content

av1enc: specialise the inverse transform per size, size the forward tmp buffer - #2396

Merged
bflatastic merged 3 commits into
storytold:mainfrom
vangel76:av1enc-transform-specialise
Oct 29, 2026
Merged

bflatastic merged 3 commits into
storytold:mainfrom
vangel76:av1enc-transform-specialise

Conversation

@vangel76

@vangel76 vangel76 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2372

What

  • inverse_dct, the ADST permutations and inverse_2d are const generic over the transform size and #[inline(always)], so the bit-reversal permutations and loop bounds fold at compile time (they were about 6 % of the encode by themselves).
  • forward_2d no longer zeroes a 4 KiB tmp on every call: 4x4, 8x8 and 16x16 use a fixed-size version with tmp: [f32; N*N]. 32x32 keeps the run-time version (a const 32 variant measured slower). Float operation order is unchanged.
  • Only crates/av1enc/src/transform.rs changes.

Output is identical

md5 of the encoded stream, base vs this branch, all equal:

input md5
1080p, Q76, 8 frames, keyint 30 06cc3ba1...
1080p, 10000 kbit/s, 60 frames, keyint 60 e82e4415...
noise sweep (NOISE=3), 6 frames, q50 / q90 / q130 / q170 identical at all four

cargo test --release -p effectcraft-av1enc passes (roundtrip, ffmpeg/dav1d oracle), cargo fmt --all --check and cargo clippy -p effectcraft-av1enc --all-targets -- -D warnings are clean.

Speed

1080p synthetic sequence, 10 frames, one thread pinned (taskset -c 1), perf stat -e instructions:u,cycles:u, base and branch alternating, median of 5 rounds, baseline x86-64 build:

config instructions before after cycles before after wall before after
Q76, keyint 30 134.83e9 112.81e9 (-16.3 %) 33.43e9 29.04e9 (-13.1 %) 6.94 s 6.06 s
10000 kbit/s, keyint 60 244.72e9 197.93e9 (-19.1 %) 60.99e9 52.38e9 (-14.1 %) 12.71 s 10.93 s

Not included: lane-parallel column pass (B6)

The optional second step of the issue (inverse column pass over whole rows) was measured on top of this on the baseline build: instructions another -1.5 % / -1.4 %, but cycles +0.9 % / +1.3 % and wall unchanged (Q76 6.11 s, BR 10.99 s). Without a wider ISA there is no signed 32x32->64 multiply to vectorise with, so there is no measurable gain here and it is left out. It would only pay with -C target-cpu=x86-64-v3 (#2377).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Updated image transform processing for commonly used block sizes. The resulting transforms and externally visible behavior remain unchanged. No new user-facing features or behavior changes are included, and existing workflows continue as before. There are no new settings or visible interface changes.

…mp buffer

The inverse transform computed its bit-reversal permutations and loop
bounds at run time and went through one generic path for every size.
inverse_dct, the ADST permutations and inverse_2d are now const generic
over the size and inlined, so the permutations fold at compile time.

forward_2d zeroed a 4 KiB tmp buffer on every call, even for 4x4. It now
dispatches to a fixed-size version for 4, 8 and 16 (tmp is exactly n*n
floats); 32 keeps the run-time version, which measured faster than a
const 32 variant. The float operation order is unchanged.

Output is bit-identical.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 18ce00d and c99b20c.

Files selected for processing: 1
  • crates/av1enc/src/transform.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

Walkthrough

Inverse transforms now use const-generic implementations selected by transform size. Forward transforms use fixed-size implementations for 4×4, 8×8, and 16×16 blocks. Other forward-transform sizes retain the dynamic implementation.

Changes: Transform specialization

Layer / File(s) Summary
Inverse transform specialization
crates/av1enc/src/transform.rs
Inverse DCT and ADST helpers use const-generic sizes. The inverse transform entry points dispatch to size-specific implementations.
Forward transform specialization
crates/av1enc/src/transform.rs
forward_2d uses fixed-size implementations with sized temporary arrays for 4×4, 8×8, and 16×16 blocks. Other sizes, including 32×32, use the dynamic implementation.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor · Severity of issue fixed: Medium

Possibly related issues

  • storytold/effectcraft#2376 — This issue proposes a butterfly forward DCT/ADST implementation. This pull request specializes the existing forward transform by block size.

Suggested reviewers: echelon

Merge Risk: Minimal · up to c99b2

No merge-blocking risk is established for the transform specialization; normal checks remain appropriate.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main changes: inverse-transform specialization by size and forward-transform temporary-buffer sizing.
Linked Issues check Passed Issue #2372 requires inverse-transform specialization by size and a forward-transform temporary buffer sized to the block. The diff adds const-generic inverse_dct_n, ADST permutation helpers, and `i…
Out of Scope Changes check Passed The whole-PR diff changes only crates/av1enc/src/transform.rs. All changes support issue #2372 by specializing transform code or sizing the forward temporary buffer. No unrelated behavior or file ch…
Full details: Linked Issues check

Explanation

Issue #2372 requires inverse-transform specialization by size and a forward-transform temporary buffer sized to the block. The diff adds const-generic inverse_dct_n, ADST permutation helpers, and inverse_2d_n dispatch for sizes 4 through 32. forward_2d uses fixed-size buffers for 4×4, 8×8, and 16×16. The 32×32 path remains dynamic, as allowed by the issue proposal and PR description. The optional inverse column pass is not required. The PR reports unchanged output and passing encoder tests.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff changes only crates/av1enc/src/transform.rs. All changes support issue #2372 by specializing transform code or sizing the forward temporary buffer. No unrelated behavior or file changes are present.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 16 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@vangel76

Copy link
Copy Markdown
Contributor Author

This PR is one of five output-identical speed PRs for the AV1 encoder (effectcraft-av1enc): #2396 (transform specialisation), #2401 (half-pel planes in one pass), #2400 (quantiser tables), #2405 (scratch buffers), #2406 (motion search, SATD, decoded-flag snapshot). Each PR has its own table; this is the measurement of all five merged together (upstream/main + the five branches; the decide.rs conflicts are trivial) against upstream/main.

1080p, one thread base all five merged
Q76, 8 frames: instructions 107.3 G 64.6 G -39.7 %
Q76: cycles (median) 42.8 G 27.5 G -35.8 %
Q76: frames/s (best of 5 rounds) 1.55 2.20 +42 %
bitrate 10000 kbit/s, keyint 60, 60 frames: instructions 1133 G 670 G -40.9 %
bitrate: cycles (median) 351 G 237 G -32.4 %
bitrate: frames/s (best of 5 rounds) 0.91 1.39 +53 %

bflatastic and others added 2 commits October 29, 2026 23:15
…ugh them

The strict-clippy split of inverse_dct (storytold#2496) passes n at run time; inlining keeps
inverse_dct_n's compile-time sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bflatastic
bflatastic merged commit 6e3c864 into storytold:main Oct 29, 2026
6 checks passed
@bflatastic

Copy link
Copy Markdown
Member

Merged, thank you @vangel76! It landed on main with a batch of community PRs, after a full workspace build and test run on Windows against the refactored main (fmt, strict clippy, about 4,100 tests, layers, assets and wasm). Before landing, I added #[inline(always)] to inverse_dct_initial_stages / inverse_dct_final_stages: #2496 split inverse_dct and passes n at run time, so without inlining most of the const-folding win was lost.

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.

av1enc: inverse and forward transform run-time setup costs about 17 % of the encode

2 participants