From ccbcc73581abe2bfbdcb608249f4e03c6ac87b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 17:00:09 +0300 Subject: [PATCH] feat(av1): the temporal lambda map runs on the pyramid path, which is every default stream encode_pyramid_inter passed an empty lookahead window, so the propagating per-superblock lambda map lane-av1tpl2/3 shipped has been inert on every stream the encoder writes by default since the pyramid became the default -- it only ever ran on the flat A/B arm. The window is the group's own buffered sources: display-order successors for a frame inside the leaf run, and the group's leaves in reverse display order for its top ARF, whose successors are not buffered yet and which those leaves predict backward from. Measured through enc_probe on film B against the control's own rate/quality slope: 1.9% fewer bytes at the q=150 point's PSNR and 0.2% at q=90, wall unchanged. EC_AV1_TPL_PYRAMID=0 restores the old behaviour. Also from the census lane: EC_AV1_LAMBDA_HIDDEN, a rate weight for hidden frames alone (default 1.0 = unchanged), which is the knob the pyramid's q offsets cannot express; the sweep says 1.0 is already the optimum. --- crates/ec-av1/src/encode.rs | 28 ++++++++++++++++- crates/ec-av1/src/encoder.rs | 51 +++++++++++++++++++++++++++--- lanes/arfcen.report.md | 60 ++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 5 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index d55c563b..4b88f984 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -137,6 +137,26 @@ fn reach_sb_px(fctx: &crate::decode::FrameCtx) -> usize { /// defaults below are on: +50.1 / -15.4). It spends ~5% more encode wall. const LAMBDA_SCALE: f64 = 0.0275; +/// The rate weight a HIDDEN frame (a pyramid ARF, coded but not shown) codes +/// under, as a multiple of [`LAMBDA_SCALE`] (lane-arfcen). +/// +/// The ARF-vs-ARF census says our top ARF codes residual over HALF its area +/// (rav1e's, at the same display position, over 26-30%) and comes out 1.4-2.5x +/// its bytes at the same PSNR, while every leaf that predicts from it is +/// still 0.4 dB behind rav1e's -- i.e. the ARF's extra fidelity is bought at +/// a rate weight fitted on shown frames and does not propagate. This is the +/// one knob that prices a hidden frame's rate differently from its q, which +/// the pyramid's q offsets cannot do (they move lambda and step together). +const HIDDEN_LAMBDA_FACTOR: f64 = 1.0; + +/// [`HIDDEN_LAMBDA_FACTOR`], swept by `EC_AV1_LAMBDA_HIDDEN` in any build. +fn hidden_lambda_factor() -> f64 { + match std::env::var("EC_AV1_LAMBDA_HIDDEN").ok().and_then(|v| v.parse().ok()) { + Some(f) => f, + None => HIDDEN_LAMBDA_FACTOR, + } +} + /// [`LAMBDA_SCALE`], or whatever `EC_AV1_LAMBDA` names when the sweep that /// picks it is the thing running. A release build has no such knob. fn lambda_scale() -> f64 { @@ -9040,7 +9060,13 @@ pub(crate) fn encode_inter_frame( let search = Search { base_q_idx, deadzone, - lambda: lambda_scale() * step * step, + lambda: lambda_scale() + * match pyramid { + Some(p) if !p.show_frame => hidden_lambda_factor(), + _ => 1.0, + } + * step + * step, modes: &KEY_FRAME_MODES, top_k: prune_top_k_inter(), screen, diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index a4ba46ae..942b1d9c 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -886,6 +886,23 @@ const MID_DEFAULT: Option = Some(-8); /// is gated off). const KEY_DEFAULT: i16 = -48; +/// Whether the pyramid path hands each frame a lookahead window, i.e. +/// whether the temporal lambda map (`encode::tpl_lambda_factors`) runs on +/// the DEFAULT stream at all. It did not until lane-arfcen: the pyramid +/// reorders pictures, so this path passed `&[]` and the map -- measured and +/// shipped on the flat path -- was inert on every frame of every pyramid +/// stream, including the ARFs that carry 54% of a long-GOP stream's bytes +/// (class `tool disabled in every gate recipe`). +fn tpl_pyramid() -> bool { + match std::env::var("EC_AV1_TPL_PYRAMID").ok() { + Some(v) => v != "0", + None => TPL_PYRAMID, + } +} + +/// [`tpl_pyramid`]'s default. +const TPL_PYRAMID: bool = true; + /// The AV1 software encoder: [`EncoderConfig`] in, one [`Packet`] out per /// [`Av1Encoder::encode`] call — or, with a [`Pyramid`] configured, a /// coding-order burst of them per [`Av1Encoder::encode_frames`] call plus a @@ -1464,6 +1481,10 @@ impl Av1Encoder { // ARF and the mid one) at different offsets, and they share the // rate-control slot but not the offset. offset: i16, + // The lookahead window this frame's temporal lambda map is built + // from, already padded (lane-arfcen). Empty codes the frame at one + // lambda, which is what the whole pyramid path used to do. + lookahead: &[Picture], ) -> Result { let render = (self.config.width, self.config.height); let order_hint = (order & 0x7f) as u32; @@ -1529,10 +1550,10 @@ impl Av1Encoder { show_frame, sign_bias, }), - // The pyramid path reorders pictures, so its own buffer is not - // a lookahead window: the temporal lambda weighting is off on - // this path (lane-av1tpl). - &[], + // lane-arfcen: the group's own sources ARE this frame's window + // (`tpl_window`); it was `&[]` here, which switched the temporal + // lambda map off on every frame the default stream codes. + lookahead, ) }(); // Name the pyramid position in any failure: an error out of the tile // writer or the filter search is otherwise indistinguishable between @@ -1568,6 +1589,23 @@ impl Av1Encoder { } let group: Vec<(u64, Picture)> = std::mem::take(&mut self.pending); let mut packets = Vec::with_capacity(group.len() + 1); + // The lookahead window of the group's picture at index `pos`: the + // display-order successors inside this group, or -- for the group's + // top ARF, the last picture, whose successors are not buffered yet -- + // the leaves BEHIND it in reverse display order, which are exactly + // the pictures that predict backward from it. Padded like the frame + // itself, as `encode_inter_frame` wants it. + let tpl_window = |pos: usize, back: bool| -> Vec { + let depth = crate::encode::tpl_depth(); + if !tpl_pyramid() || depth < 2 { + return Vec::new(); + } + let window: Vec<&Picture> = match back { + false => group[pos + 1..].iter().map(|(_, p)| p).collect(), + true => group[..pos].iter().rev().map(|(_, p)| p).collect(), + }; + window.into_iter().take(depth - 1).map(|p| p.padded_to(SUPERBLOCK)).collect() + }; let anchor_slot = ANCHOR_SLOTS[self.anchor]; let next_anchor_slot = ANCHOR_SLOTS[1 - self.anchor]; let (arf_order, arf_picture) = group.last().cloned().expect("non-empty"); @@ -1584,6 +1622,7 @@ impl Av1Encoder { true, Level::Leaf, pyramid.leaf_q_offset, + &tpl_window(0, false), )?); self.anchor = 1 - self.anchor; return Ok(packets); @@ -1598,6 +1637,7 @@ impl Av1Encoder { false, Level::Arf, pyramid.arf_q_offset, + &tpl_window(group.len() - 1, true), )?); let leaves = group.len() - 1; // THE THIRD LEVEL: a second hidden frame at the middle of the leaf @@ -1630,6 +1670,7 @@ impl Av1Encoder { false, Level::Arf, offset, + &tpl_window(at, false), )?); } // THE FOURTH LEVEL: one hidden frame at the middle of each half of @@ -1665,6 +1706,7 @@ impl Av1Encoder { false, Level::Arf, offset, + &tpl_window(pos, false), )?); } } @@ -1721,6 +1763,7 @@ impl Av1Encoder { true, Level::Leaf, pyramid.leaf_q_offset, + &tpl_window(i, false), )?); } let packet = show_existing(self, next_anchor_slot, arf_order)?; diff --git a/lanes/arfcen.report.md b/lanes/arfcen.report.md index 5222bf40..27201544 100644 --- a/lanes/arfcen.report.md +++ b/lanes/arfcen.report.md @@ -61,3 +61,63 @@ the same six positions in rav1e's stream, and everything else: So the WHOLE +15.4 kB gap (and 6 kB more) sits in the six pictures we code at prediction distance 8; our non-anchor pictures are already 23% CHEAPER than rav1e's -- though 0.3-0.4 dB worse (leaves 45.26 vs 45.69 dB). + +## 4. Ranked mechanisms, and what each one measured + +Every arm below is `enc_probe`'s q=150 and q=90 points on film B (48 frames, +the gate's window), read against the control's own rate/quality slope +(3.46 dB per decade of bytes between its two points), so "better" means +fewer bytes AT the arm's own PSNR. + +| # | mechanism | bytes at stake | verdict | +|---|---|---|---| +| 1 | anchor density (rav1e anchors every 2 pictures, ours every 4) | 22.1 kB of leaf+mid | **REFUTED** | +| 2 | hidden-frame rate weight (ARF codes residual on 50% of area, rav1e 26-30%) | 48.1 kB of ARF | **REFUTED** | +| 3 | motion search widening at distance 8 (`MV_DIST_SCALE`, its 4x cap) | 48.1 kB of ARF | **INERT** | +| 4 | the temporal lambda map never ran on the pyramid path | the whole stream | **KEPT, small** | +| 5 | ARF q offset / key allocation | — | already swept (lane-arfq, lane-keyq) | + +1. `EC_AV1_PYRAMID` density arms: `4:-32:12:-8:-48` 108567 B/45.809 dB and + 613170 B/48.283 (3.8% better at q150, 3.8% WORSE at q90), `4:-24:12:-8:-48` + 92200/45.504 and 535188/48.102 (0.4% better, 2.7% worse), `2:-32:12:-8:-48` + 127348/45.982 and 778014/48.492 (tie, 15% worse). Denser anchors pay only + at the low-rate end and lose at the rates the gate is scored on -- the same + verdict lane-pyr6 got for a fourth level, now with the mini-GOP-4 shape + (rav1e's exact structure) measured too. +2. `EC_AV1_LAMBDA_HIDDEN` (a rate weight for hidden frames only, on top of + the q-derived lambda -- the one knob the q offsets cannot express): + 1.5 -> 84513/45.297 (4.8% WORSE), 2.0 -> 81454/45.160, 3.0 -> 75372/44.910, + 0.5 -> 106859/45.733 (0.9% better at q150, 5.5% worse at q90). 1.0 is the + optimum; our ARF's operating point is not mispriced. +3. `EC_AV1_MV_DIST_SCALE` x `EC_AV1_MV_DIST_CAP`: (0.5,8) 90534/45.471, + (1.0,8) 90531/45.481, (2.0,8) 90526/45.473, (1.0,16) 90650/45.474 against + the control's 90674/45.473 -- +-0.2% bytes, +-0.01 dB. The distance-8 ARF + is NOT search-range-bound (and its coded mv histogram, 0.0% over 64 px, is + the content, not a clamp). + +## 5. The lever: the temporal lambda map was inert on every default stream + +`Av1Encoder::encode_pyramid_inter` passed `&[]` as the lookahead window with +the comment "the pyramid path reorders pictures, so its own buffer is not a +lookahead window: the temporal lambda weighting is off on this path". The +pyramid has been the DEFAULT since lane-av1pyrdef, so the propagating tpl map +lane-av1tpl2/3 measured and shipped (`TPL_DEPTH` 8, `TPL_STRENGTH` 0.5) has +been running on nothing but the flat A/B arm ever since -- class +`tool disabled in every gate recipe`. + +The fix is the window the group already holds: for a frame inside the leaf +run, its display-order successors in `pending`; for the group's top ARF (the +last picture of the group, whose successors are not buffered yet) the group's +own leaves in REVERSE display order, which are exactly the pictures that +predict backward from it. `EC_AV1_TPL_PYRAMID=0` restores the old behaviour. + +Probe arms on film B (control = the old `&[]`): + +| arm | q150 | q90 | vs control at equal PSNR | +|---|---|---|---| +| off (control) | 90674 B / 45.473 dB | 479686 B / 47.978 dB | — | +| on, depth 8 | 91206 / 45.511 | 482161 / 47.989 | -1.9% / -0.2% | +| on, depth 4 | 91453 / 45.506 | 481555 / 47.991 | -1.4% / -0.3% | +| on, depth 2 | 91328 / 45.489 | 481497 / 47.990 | -0.6% / -0.3% | + +Wall is unchanged (101.5 s vs 101.2 s for the two-point ladder).