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