From d6f79ca28233001e125ebd153e3efb0704530258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 11:28:07 +0300 Subject: [PATCH] ec-av1 rate loop: steer on the geometric mean of the last two size ratios per slot. Evidence (c-round3-full.log sizes 4176,2073,4776,2183,3861,...): a 0.77 q step cannot move a frame 4176->2073, so the swing is inter-frame coupling (a big frame leaves a good reference, the next is small), not q slope; per-frame steering chases it as a decaying period-2 cycle that biases the mean low. Averaging log-ratios over two frames cancels the alternating component. BytesPerFrame repay=false kept; Bitrate +7.4% not separately root-caused (no census run permitted); replan still repays. cargo check -p ec-av1 --all-targets clean; tests left to orchestrator --- crates/ec-av1/src/encoder/mod.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/ec-av1/src/encoder/mod.rs b/crates/ec-av1/src/encoder/mod.rs index e319a9a6..0f8a17ab 100644 --- a/crates/ec-av1/src/encoder/mod.rs +++ b/crates/ec-av1/src/encoder/mod.rs @@ -243,6 +243,7 @@ struct RateLoop { /// FIRST update of a slot is the seeding one and takes the model's whole /// step ([`RateLoop::update`]); every later one is clamped. seeded: [bool; 3], + prev_ratio: [f64; 3], /// The leaf's own q offset: the zero of the level weight /// ([`WEIGHT_LEVEL_GAIN`]), so slot 1's target can be read in /// LEAF-EQUIVALENT bytes and scaled per frame by that frame's offset. @@ -355,6 +356,7 @@ impl RateLoop { plans, scale0: scale, repay: true, + prev_ratio: [1.0; 3], census: census_on(), coded: 0, } @@ -422,8 +424,15 @@ impl RateLoop { // step and seeds `q` where the log-linear slope says it belongs; from // the second frame on the clamp is back, and nothing is accumulated // between frames, so [[vorbis-rate-loop-windup]]'s bound still holds. - let raw = ratio.ln() * Self::GAIN; - let step = if std::mem::replace(&mut self.seeded[slot], true) { + let seeded = std::mem::replace(&mut self.seeded[slot], true); + let smoothed = if seeded { + (ratio * self.prev_ratio[slot]).sqrt() + } else { + ratio + }; + self.prev_ratio[slot] = ratio; + let raw = smoothed.ln() * Self::GAIN; + let step = if seeded { raw.clamp(-Self::STEP_CLAMP, Self::STEP_CLAMP) } else { raw