Repository navigation
av1enc: truncate instead of floor, log2 lookup table, skip division in the dead zone - #2400
Conversation
…n the dead zone The quantiser in try_tx called libm floorf for every coefficient, estimate_bits and the motion-vector cost called f64::log2, and coefficients that quantise to zero still went through an f32 division. - (x / q + rnd).floor() as i32 becomes (x / q + rnd) as i32: the operand is never negative, so truncation equals floor. - log2 of small integers comes from a table built with the same f64::log2 call (estimate_bits and the MV cost in motion_search), so the values are bit-identical. - Coefficients below an exact per-(q, rounding offset) threshold are zero without dividing. The threshold is found once per Decider by a binary search over f32 bit patterns of the monotone quantiser expression. The encoded stream is unchanged. Unit tests compare the new quantiser with the old expression at the threshold boundary and on random values for every quantiser index, and the table with f64::log2. Fixes storytold#2373 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Zero-threshold quantization crates/av1enc/src/decide.rs |
Decider stores inter/intra DC/AC thresholds. try_tx skips coefficients below the selected threshold and truncates the rounded quotient. Tests compare the threshold quantizer with the previous formula. |
Logarithm-based bit estimates crates/av1enc/src/decide.rs |
Motion-vector and coefficient bit estimates use the shared logarithm lookup. Tests compare lookup results with direct log2 values. |
Priority:
Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Refactor
Suggested reviewers: echelon
Merge Risk: 
Minimal · up to 88c02
-
No merge-blocking risk was identified in the quantization or bit-estimation changes.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Linked Issues check
-
Explanation
Issue
#2373requires truncation where it matchesfloor(), exactlog2lookup values with fallback, and an exact dead-zone threshold. The reviewed changes implement these items incrates/av1enc/src/decide.rs. The changes add DC/AC inter/intra thresholds, skip division below the applicable threshold, use truncating conversion, and use a shared 1025-entry table with directf64::log2fallback. Unit tests cover quantiser equivalence at threshold boundaries and sampled values, plus bit-exact table values. The PR reports bit-identical output validation and passing encoder tests.
Full details: Out of Scope Changes check
-
Explanation
The reviewed change is limited to
crates/av1enc/src/decide.rs. The implementation and tests directly support issue#2373performance and output-preservation requirements. No unrelated change is evident.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 44.44% 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 9 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.
|
This PR is one of five output-identical speed PRs for the AV1 encoder (
|
# Conflicts: # crates/av1enc/src/decide.rs
|
Merged current main into the branch. The only conflict was in decide.rs, next to the TransformBlock refactor; the quantiser changes are intact and the av1enc tests pass, including the dead-zone and log2 table equality tests. |
|
Merged, thank you @vangel76! It landed on |
Fixes #2373
What
In
crates/av1enc/src/decide.rs:(x / q + rnd).floor() as i32, a libmfloorfcall per coefficient on baseline x86-64. The operand is never negative, soas i32(truncation) gives the same value, with the same saturation.estimate_bits(eob and level magnitudes) and the motion-vector cost inmotion_searchcalledf64::log2. They now read a 1025-entry table built with the samef64::log2call, so the values are bit-identical. Larger arguments fall back to the direct call.Decider(DC and AC, inter and intra rounding offset) by a binary search over the f32 bit patterns of the monotone expression, so the shortcut is exact.Output
Bit-identical. md5 of the encoded stream equal to upstream/main (9919048) at 5 operating points (1080p synthetic pan + noise):
cargo test --release -p effectcraft-av1encpasses (roundtrip decoder == reconstruction, ffmpeg/libdav1d oracle, sizes 1x1..4K, 8 and 10 bit). Two new unit tests: the new quantiser equals the old expression at the threshold boundary (+-3 ulp) and on random values for every quantiser index of all three bit-depth tables and both rounding offsets; the table equalsf64::log2bit for bit.Speed
perf stat -e instructions:u,cycles:u, one thread pinned to one core, base and branch alternating, 5 rounds, medians, 1080p synthetic sequence, 8 frames:🤖 Generated with Claude Code
Summary by CodeRabbit