diff --git a/crates/ec-av1/src/encoder/mod.rs b/crates/ec-av1/src/encoder/mod.rs index 8823dee8..dce7801a 100644 --- a/crates/ec-av1/src/encoder/mod.rs +++ b/crates/ec-av1/src/encoder/mod.rs @@ -236,10 +236,6 @@ struct RateLoop { target: [f64; 3], /// Quantizer per level, same index. q: [f64; 3], - /// Whether the three slots are actually distinct (pyramid mode) or all - /// one shared controller (the flat path, unchanged from before levels - /// existed). - per_level: bool, /// Whether each slot has already steered off a real coded frame. The /// FIRST update of a slot is the seeding one and takes the model's whole /// step ([`RateLoop::update`]); every later one is clamped. @@ -299,39 +295,30 @@ impl RateLoop { const REPLAN_FLOOR: f64 = 0.25; fn new(target_bytes: f64, start_q: f64, pyramid: Option, gop: usize) -> Self { - let Some(p) = pyramid else { - return Self { - target: [target_bytes; 3], - q: [start_q; 3], - per_level: false, - seeded: [false; 3], - leaf_q_offset: 0, - key_weight: 1.0, - run_weight: 0.0, - run_bytes: 0.0, - left_weight: 0.0, - left_bytes: 0.0, - plans: [None; 5], - scale0: target_bytes, - census: census_on(), - coded: 0, - }; - }; // THE LEVEL MODEL. Every coded frame of the run is priced by its own // q offset through `WEIGHT_LEVEL_GAIN`, in units of one leaf, and the - // run's `gop` frame targets are shared out in those units. The old - // model priced a group as "one hidden frame worth `ARF_WEIGHT` = 2 - // leaves plus `m - 1` leaves", which was wrong twice over and the two - // errors cancelled (lane-pyr6): the group has coded TWO hidden frames - // since the mid level landed (four with + // run's `gop` frame targets are shared out in those units. The flat + // path is the same run with no hidden levels and no offsets, so the + // key frame is budgeted against the GOP there too: its overshoot is + // re-planned over the leaves, never slammed into their shared `q`. + // The old pyramid model priced a group as "one hidden frame worth + // `ARF_WEIGHT` = 2 leaves plus `m - 1` leaves", which was wrong twice + // over and the two errors cancelled (lane-pyr6): the group has coded + // TWO hidden frames since the mid level landed (four with // [`Pyramid::quarter_q_offset`]), and a hidden frame is worth far // more than two leaves -- the census reads the top ARF at 24x a leaf // and the mid at 5x. Both are fixed here, in one diff. + let (key_q_offset, leaf_q_offset, hidden, leaves) = match pyramid { + Some(p) => { + let (hidden, leaves) = gop_shape(p, gop); + (p.key_q_offset, p.leaf_q_offset, hidden, leaves) + } + None => (0, 0, Vec::new(), gop.saturating_sub(1)), + }; let w = |offset: i16| { - ((f64::from(p.leaf_q_offset) - f64::from(offset)) * WEIGHT_LEVEL_GAIN).exp() + ((f64::from(leaf_q_offset) - f64::from(offset)) * WEIGHT_LEVEL_GAIN).exp() }; - let (hidden, leaves) = gop_shape(p, gop); - let key = KEY_WEIGHT * w(p.key_q_offset); + let key = KEY_WEIGHT * w(key_q_offset); let total: f64 = key + leaves as f64 + hidden.iter().map(|&(o, n)| n as f64 * w(o)).sum::(); // One leaf's byte target: the run is worth `gop` frame targets and @@ -347,19 +334,18 @@ impl RateLoop { // One plan per level, in coding order: key, hidden levels (at most // three distinct offsets -- top, mid, quarters), leaves. let mut plans = [None; 5]; - plans[0] = Some(LevelPlan::new(0, p.key_q_offset, key)); + plans[0] = Some(LevelPlan::new(0, key_q_offset, key)); let mut i = 1; for &(o, n) in hidden.iter().take(3) { plans[i] = Some(LevelPlan::new(1, o, n as f64 * w(o))); i += 1; } - plans[i] = Some(LevelPlan::new(2, p.leaf_q_offset, leaves as f64)); + plans[i] = Some(LevelPlan::new(2, leaf_q_offset, leaves as f64)); Self { target: [scale * key, scale, scale], q: [start_q; 3], - per_level: true, seeded: [false; 3], - leaf_q_offset: p.leaf_q_offset, + leaf_q_offset, key_weight: key, run_weight: total, run_bytes: target_bytes * gop.max(1) as f64, @@ -386,12 +372,9 @@ impl RateLoop { .position(|p| p.is_some_and(|p| p.slot == slot && (slot != 1 || p.offset == offset))) } - /// Which controller a level uses: one shared slot on the flat path, three - /// separate ones under a pyramid. + /// Which controller a level uses: the key, the hidden ARF levels and the + /// leaves each steer their own `q`. fn slot(&self, level: Level) -> usize { - if !self.per_level { - return 0; - } match level { Level::Key => 0, Level::Arf => 1, @@ -423,7 +406,7 @@ impl RateLoop { 1 => self.weight(offset), _ => 1.0, }; - let target = match self.per_level && slot == 1 { + let target = match slot == 1 { true => self.target[slot] * frame_weight, false => self.target[slot], }; @@ -495,9 +478,6 @@ impl RateLoop { frame_weight: f64, actual_bytes: f64, ) { - if !self.per_level { - return; - } if level == Level::Key { self.left_weight = self.run_weight; self.left_bytes = self.run_bytes;