From 657e0436e5adf318792904c6e3199e4529f6a2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 12:14:38 +0300 Subject: [PATCH] feat(av1): a per-pyramid-level strength for the objective delta_q lane-arfq: the objective per-superblock delta_q shipped one frame-type-blind strength (`speed::DQ_TPL_K`). The top ARF feeds a whole mini-GOP where a leaf feeds nothing, so the level is now a parameter: `encode::DqLevel` (top ARF / mid ARF / leaf) rides on `PyramidFrame`, the caller sets it from the offset it codes the frame at, and `speed::DQ_LEVEL_K` multiplies the preset's strength per level. `EC_AV1_DQ_K=::` sweeps it. Three levels, not four: the KEY frame codes no delta_q syntax at all (the lever lives in `encode_inter_frame`), so a strength cannot reach it -- the key's allocation stays `Pyramid::key_q_offset`'s job. The default table is flat, so every stream is byte-identical (`the_encoders_own_streams_are_byte_identical_to_their_pins` green, unchanged pins). The census line now names the level it prices. Self-check: encode::tests::the_per_level_delta_q_strength_indexes_the_levels_it_names --- crates/ec-av1/src/encode.rs | 80 ++++++++++++++++++++++++++++++++---- crates/ec-av1/src/encoder.rs | 11 +++++ crates/ec-av1/src/speed.rs | 8 ++++ 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index e66c67d8..b283cc97 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -10683,17 +10683,58 @@ fn tpl_frame_ratio(ratio: &[f64]) -> f64 { /// `EC_AV1_DQ_TPL=0`/`1` forces the mapping either way and `EC_AV1_DQ_TPL_K` /// the strength (default 1.0 = libaom's own offset when the flag armed it); /// with neither set the preset decides, see [`crate::speed::DQ_TPL_K`]. -fn dq_tpl_strength() -> Option { +fn dq_tpl_strength(level: DqLevel) -> Option { + let m = dq_level_k()[level as usize]; let env_k = crate::envflags::var("EC_AV1_DQ_TPL_K") .ok() .and_then(|v| v.trim().parse::().ok()) .filter(|k| k.is_finite() && *k >= 0.0); - match crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) { + let base = match crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) { Some("0") => return None, - Some(_) => return Some(env_k.unwrap_or(1.0)), - None => {} + Some(_) => Some(env_k.unwrap_or(1.0)), + None => env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))), + }; + base.map(|k| k * m).filter(|k| *k > 0.0) +} + +/// lane-arfq: which pyramid level the frame asking for a +/// [`dq_tpl_strength`] sits at. The KEY frame is not here on purpose: it +/// codes no `delta_q` syntax at all (the whole lever lives in +/// [`encode_inter_frame`]), so a per-level strength cannot touch it -- the +/// key's allocation is [`crate::encoder::Pyramid::key_q_offset`]'s job. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum DqLevel { + /// The mini-GOP's own hidden `ALTREF`, which every other frame of the + /// group predicts from (directly or through the mid level). + TopArf = 0, + /// A hidden mid/quarter-point `ALTREF`, read by the leaves around it. + MidArf = 1, + /// A shown leaf -- and every frame of the flat, non-pyramid path. + Leaf = 2, +} + +/// The per-level multiplier on the [`dq_tpl_strength`] the preset carries: +/// `EC_AV1_DQ_K=::`, else +/// [`crate::speed::DQ_LEVEL_K`]. Three fields, not four: the key frame codes +/// no `delta_q` (see [`DqLevel`]). +fn dq_level_k() -> [f64; 3] { + dq_level_k_of(crate::envflags::var("EC_AV1_DQ_K").ok().as_deref()) +} + +/// [`dq_level_k`]'s parse, pure: a missing spec, a missing field and an +/// unparseable one all keep [`crate::speed::DQ_LEVEL_K`]'s entry. +fn dq_level_k_of(spec: Option<&str>) -> [f64; 3] { + let mut k = crate::speed::DQ_LEVEL_K; + if let Some(spec) = spec { + for (slot, field) in k.iter_mut().zip(spec.split(':')) { + if let Some(v) = + field.trim().parse::().ok().filter(|v: &f64| v.is_finite() && *v >= 0.0) + { + *slot = v; + } + } } - env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))).filter(|k| *k > 0.0) + k } /// `EC_AV1_DQ_CENSUS=1`: per inter frame, one line comparing the offsets the @@ -11189,6 +11230,10 @@ pub(crate) struct PyramidFrame { /// `show_frame`: false for a hidden frame, output later by a /// `show_existing_frame` header. pub show_frame: bool, + /// lane-arfq: which level's `delta_q` strength this frame reads + /// ([`DqLevel`]) -- the caller knows it, since the top ARF and the mid + /// one share `Level::Arf` and differ only by their q offset. + pub dq_level: DqLevel, /// This frame's `ref_frame_sign_bias` (spec 5.9.2). pub sign_bias: crate::mvstack::SignBiasTable, } @@ -11475,6 +11520,9 @@ pub(crate) fn encode_inter_frame( // 0.5 loss vs libaom on the desktop capture, so a frame that set // `allow_screen_content_tools` codes no delta syntax at all and stays // byte-identical to the pre-lane encoder. + // lane-arfq: the level whose `delta_q` strength this frame reads; the + // flat path (no pyramid) is a leaf. + let dq_level = pyramid.map_or(DqLevel::Leaf, |p| p.dq_level); let deltaq_res: Option = tpl_factors .as_ref() .filter(|_| !screen) @@ -11504,7 +11552,7 @@ pub(crate) fn encode_inter_frame( }; let sb_q: Option> = match (&tpl_factors, deltaq_res) { (Some(f), Some(res)) => { - let libaom = dq_tpl_strength(); + let libaom = dq_tpl_strength(dq_level); Some((0..sb_cols * _sb_rows).map(|sb| sb_qindex(f, res, sb, libaom)).collect()) } _ => None, @@ -11520,7 +11568,7 @@ pub(crate) fn encode_inter_frame( .map(|sb| i32::from(sb_qindex(f, res, sb, libaom)) - i32::from(base_q_idx)) .collect() }; - let strength = dq_tpl_strength(); + let strength = dq_tpl_strength(dq_level); let (ours, aom) = (off(None), off(Some(strength.unwrap_or(1.0)))); let coded = match (deltaq_res.is_some(), strength) { (false, _) => "none".to_string(), @@ -11528,7 +11576,7 @@ pub(crate) fn encode_inter_frame( (true, Some(k)) => format!("libaom k={k}"), }; eprintln!( - "dq census: base={base_q_idx} res={res} sbs={} coded={coded} r_frame={:.4}\n ours {}\n libaom {}", + "dq census: level={dq_level:?} base={base_q_idx} res={res} sbs={} coded={coded} r_frame={:.4}\n ours {}\n libaom {}", sb_cols * _sb_rows, r_frame.unwrap_or(f64::NAN), dq_summary(&ours), @@ -13013,6 +13061,22 @@ mod tests { assert!((super::tpl_frame_ratio(&[2.5; 16]) - 2.5).abs() < 1e-9); } + /// lane-arfq: the per-level `delta_q` strength -- the spec parses onto + /// [`super::DqLevel`]'s own indices, a missing or broken field keeps the + /// shipped default, and the multiplier reaches the strength. + #[test] + fn the_per_level_delta_q_strength_indexes_the_levels_it_names() { + let d = crate::speed::DQ_LEVEL_K; + assert_eq!(super::dq_level_k_of(None), d, "no spec = the shipped table"); + assert_eq!(super::dq_level_k_of(Some("2:3:4")), [2.0, 3.0, 4.0]); + // Short and broken fields fall back per SLOT, not per spec. + assert_eq!(super::dq_level_k_of(Some("2")), [2.0, d[1], d[2]]); + assert_eq!(super::dq_level_k_of(Some("2:x:-1")), [2.0, d[1], d[2]]); + assert_eq!(super::DqLevel::TopArf as usize, 0); + assert_eq!(super::DqLevel::MidArf as usize, 1); + assert_eq!(super::DqLevel::Leaf as usize, 2); + } + #[test] fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { // Four 64x64 superblocks across: the shipped map forms its ratio per diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 7f935774..4ab4141b 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1724,6 +1724,16 @@ impl Av1Encoder { i16::from(r.q_idx(level)) }); let base_q_idx = (base + offset).clamp(1, 255) as u8; + // lane-arfq: the top ARF of a mini-GOP is the hidden frame coded at + // the pyramid's OWN `arf_q_offset` (the mid and quarter levels are + // `Level::Arf` too but sit at their own offsets), and it is the level + // the whole group predicts from -- so it reads its own `delta_q` + // strength (`crate::speed::DQ_LEVEL_K`). + let dq_level = match (show_frame, self.pyramid.map(|p| p.arf_q_offset)) { + (true, _) => crate::encode::DqLevel::Leaf, + (false, Some(arf)) if arf == offset => crate::encode::DqLevel::TopArf, + (false, _) => crate::encode::DqLevel::MidArf, + }; // spec 5.9.2's `ref_frame_sign_bias`, derived from what the slots // this frame names actually hold: a reference whose order hint is // AHEAD of this frame's is backward-biased, and the MV stack scans @@ -1792,6 +1802,7 @@ impl Av1Encoder { last2_slot, show_frame, sign_bias, + dq_level, }), // lane-arfcen: the group's own sources ARE this frame's window // (`tpl_window`); it was `&[]` here, which switched the temporal diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index 8f766189..2e21ed83 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -338,6 +338,14 @@ pub(crate) const DELTAQ_RES: [u8; 11] = [2, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4]; /// on a screen frame at all (lane-dq3's content gate). pub(crate) const DQ_TPL_K: [f64; 11] = [0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; +/// lane-arfq: a per-LEVEL multiplier on [`DQ_TPL_K`], indexed by +/// [`crate::encode::DqLevel`] -- `[top ARF, mid ARF, leaf]`. The key frame is +/// absent because it codes no `delta_q` syntax at all. +/// +/// `EC_AV1_DQ_K=::` overrides it for an A/B. The default is +/// flat: see `lanes/arfq.report.md` for the sweep this table records. +pub(crate) const DQ_LEVEL_K: [f64; 3] = [1.0, 1.0, 1.0]; + /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split /// trial is withheld. The single biggest wall lever in the tile search. pub(crate) const SPLIT_RD: [f64; 11] = [