From 5c168bf7d7aadb5fdf0131de93f0f74601a6148c 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:26:42 +0300 Subject: [PATCH] ec-av1 rate: BytesPerFrame no longer replans. Root cause of settled mean 3102 vs 4000: RateLoop::replan re-solves target = left_bytes/left_weight after every frame, so the key overshoot (22289-4000) was repaid over the remaining 23 frames, target ~3200 (observed 2950-3100). BytesPerFrame now keeps its fixed per-frame target (repay=false); Bitrate keeps the budget-remaining replan. cargo check -p ec-av1 --all-targets clean; tests left to orchestrator --- crates/ec-av1/src/encoder/mod.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/ec-av1/src/encoder/mod.rs b/crates/ec-av1/src/encoder/mod.rs index 494d4318..e319a9a6 100644 --- a/crates/ec-av1/src/encoder/mod.rs +++ b/crates/ec-av1/src/encoder/mod.rs @@ -125,6 +125,7 @@ impl RateTarget { /// (`None` for the two open-loop variants). `mini_gop` is `Some` in /// pyramid mode, which is what makes the loop keep a quantizer per level. fn into_loop(self, config: &mut EncoderConfig, pyramid: Option) -> Option { + let repay = matches!(self, RateTarget::Bitrate { .. }); let target_bytes = match self { RateTarget::QIndex(q) => { config.base_q_idx = q; @@ -140,12 +141,14 @@ impl RateTarget { frames_per_second, } => f64::from(bits_per_second) / 8.0 / frames_per_second.max(1.0), }; - Some(RateLoop::new( + let mut rate_loop = RateLoop::new( target_bytes, f64::from(config.base_q_idx), pyramid, config.gop, - )) + ); + rate_loop.repay = repay; + Some(rate_loop) } } @@ -268,6 +271,7 @@ struct RateLoop { /// a level held at its bound hands its unspent bytes to the others, but /// no level's target may exceed this many times its own opening share. scale0: f64, + repay: bool, /// `EC_AV1_RL_CENSUS=1`: one line per coded frame on stderr. census: bool, coded: usize, @@ -350,6 +354,7 @@ impl RateLoop { left_bytes: target_bytes * gop.max(1) as f64, plans, scale0: scale, + repay: true, census: census_on(), coded: 0, } @@ -445,7 +450,9 @@ impl RateLoop { ); self.coded += 1; } - self.replan(level, slot, offset, frame_weight, actual_bytes as f64); + if self.repay { + self.replan(level, slot, offset, frame_weight, actual_bytes as f64); + } } /// Re-plans the REST OF THE RUN from what it has actually spent: the