diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index ac37c260..a4ba46ae 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -234,13 +234,21 @@ impl RateLoop { false => target_bytes, }; // A group of `m` pictures gets `m * rest`, split one `ARF_WEIGHT` - // share to EACH of its `h` hidden frames and one each to the `m - h` - // leaves, so the group's own average is `rest` exactly. `h` is the - // pyramid's depth, not 1: a four-level group of eight codes four - // hidden frames, and pricing that group as one ARF plus seven leaves - // gave the ARF slot a target its four frames could not share. - let h = p.hidden_per_group(gop); - let share = rest * m / (m - h + h * ARF_WEIGHT); + // share to the hidden frame and one each to the `m - 1` leaves, so + // the group's own average is `rest` exactly. + // + // ONE hidden frame, deliberately, even though the group has coded two + // since the mid level landed (and four with + // [`Pyramid::quarter_q_offset`] on). Pricing the real count + // (`m / (m - h + h * ARF_WEIGHT)`, h = 2) is the truthful model and + // it lands the 48-frame bitrate gate -9.4% / -14.9% under target + // against this line's -1.8% / -6.3%: the h = 1 split over-allocates + // the group by exactly the amount `ARF_WEIGHT` under-prices a hidden + // frame, and the two errors cancel. Fixing the split alone therefore + // makes the loop WORSE; `ARF_WEIGHT` has to be recalibrated in the + // same diff, which is a rate-control lane, not a pyramid-shape one + // (lane-pyr6, `lanes/pyr6.sweep.txt`). + let share = rest * m / (m - 1.0 + ARF_WEIGHT); Self { target: [target_bytes * key, share * ARF_WEIGHT, share], q: [start_q; 3], @@ -503,18 +511,6 @@ impl Pyramid { }, }) } - - /// How many hidden frames a FULL mini-GOP of this shape codes, in a run - /// of `gop` pictures: the top ARF, plus the mid one and the two - /// quarter-point ones when they are switched on. Mirrors the gating in - /// [`Av1Encoder::drain_pending`] -- the rate loop prices the group from - /// it ([`RateLoop::new`]), so the two must move together. - fn hidden_per_group(&self, gop: usize) -> f64 { - let long_run = gop > self.mini_gop + self.mini_gop / 2; - let mid = long_run && self.mid_q_offset.is_some() && self.mini_gop >= 4; - let quarters = mid && self.quarter_q_offset.is_some() && self.mini_gop >= 8; - 1.0 + f64::from(mid) + 2.0 * f64::from(quarters) - } } impl Default for Pyramid { diff --git a/lanes/pyr6.sweep.txt b/lanes/pyr6.sweep.txt index c930ff66..daba9640 100644 --- a/lanes/pyr6.sweep.txt +++ b/lanes/pyr6.sweep.txt @@ -57,3 +57,21 @@ So the depth lever is spent at THIS group size: our per-hidden-frame overhead, n level count, is what separates us from rav1e's 23-hidden-per-48 shape. The code stays in (six-field EC_AV1_PYRAMID, slot map documented, round trip gated) so the next lane can re-measure it the moment a hidden frame gets cheaper. + +## the rate loop: the truthful group split makes it WORSE (kept at h = 1) +`RateLoop::new` splits a group's byte budget as one `ARF_WEIGHT` share plus `m - 1` +leaf shares -- a model from when the pyramid had ONE hidden frame per group. Extending +it to the real count (`m / (m - h + h * ARF_WEIGHT)`, h = 2 with the mid level, 4 with +quarters) is the truthful model and it FAILS the 48-frame bitrate gate: + +| model | 768 kbps, pyramid | 1536 kbps, pyramid | +|---|---|---| +| h = 1 (main, kept) | -1.8% | -6.3% | +| h = real hidden count | -9.4% | -14.9% (gate limit 10%) | + +The h = 1 split over-allocates a group by exactly what `ARF_WEIGHT` = 2.0 under-prices a +hidden frame at, and the two errors cancel; fixing one alone un-cancels them. So the +extension was REVERTED and the coupling documented at the line -- recalibrating +`ARF_WEIGHT` belongs to a rate-control lane, not to a pyramid-shape one. deferred: +truthful group split + ARF_WEIGHT recalibration -- needs the bitrate gate as its own +keep rule -- unblocked by a rate lane.