diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 42410141..f0d956a2 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -10485,20 +10485,23 @@ fn tpl_frame_ratio(ratio: &[f64]) -> f64 { m.exp() - 1.0 } -/// `EC_AV1_DQ_TPL=1`: take the per-superblock qindex from [`deltaq_libaom`] -/// instead of [`deltaq_for_factor`]. Off by default. -fn dq_tpl() -> bool { - crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) == Some("1") -} - -/// The [`deltaq_libaom`] strength multiplier (`EC_AV1_DQ_TPL_K`, default 1.0 -/// = libaom's own offset). -fn dq_tpl_k() -> f64 { - crate::envflags::var("EC_AV1_DQ_TPL_K") +/// The [`deltaq_libaom`] strength, or `None` to keep the shipped +/// [`deltaq_for_factor`] derivation. +/// +/// `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 { + 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) - .unwrap_or(1.0) + .filter(|k| k.is_finite() && *k >= 0.0); + 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 => {} + } + env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))).filter(|k| *k > 0.0) } /// `EC_AV1_DQ_CENSUS=1`: per inter frame, one line comparing the offsets the @@ -11301,17 +11304,15 @@ pub(crate) fn encode_inter_frame( // raw dependence ratio; without it (and always without a ratio map, i.e. // on the one-frame area map) the shipped derivation stands. let r_frame = tpl_ratio.as_deref().map(tpl_frame_ratio); - let sb_qindex = |f: &[f64], res: i32, sb: usize, libaom: bool| -> u8 { + let sb_qindex = |f: &[f64], res: i32, sb: usize, libaom: Option| -> u8 { match (libaom, tpl_ratio.as_deref(), r_frame) { - (true, Some(r), Some(rf)) => { - deltaq_libaom(base_q_idx, sb_mean(r, sb), rf, dq_tpl_k(), res) - } + (Some(k), Some(r), Some(rf)) => deltaq_libaom(base_q_idx, sb_mean(r, sb), rf, k, res), _ => deltaq_for_factor(base_q_idx, sb_mean(f, sb), deltaq_k(), deltaq_clamp(), res), } }; let sb_q: Option> = match (&tpl_factors, deltaq_res) { (Some(f), Some(res)) => { - let libaom = dq_tpl(); + let libaom = dq_tpl_strength(); Some((0..sb_cols * _sb_rows).map(|sb| sb_qindex(f, res, sb, libaom)).collect()) } _ => None, @@ -11322,16 +11323,17 @@ pub(crate) fn encode_inter_frame( if dq_census() { if let Some(f) = &tpl_factors { let res = deltaq_res.unwrap_or(4); - let off = |libaom: bool| -> Vec { + let off = |libaom: Option| -> Vec { (0..sb_cols * _sb_rows) .map(|sb| i32::from(sb_qindex(f, res, sb, libaom)) - i32::from(base_q_idx)) .collect() }; - let (ours, aom) = (off(false), off(true)); - let coded = if deltaq_res.is_some() { - if dq_tpl() { "libaom" } else { "ours" } - } else { - "none" + let strength = dq_tpl_strength(); + let (ours, aom) = (off(None), off(Some(strength.unwrap_or(1.0)))); + let coded = match (deltaq_res.is_some(), strength) { + (false, _) => "none".to_string(), + (true, None) => "ours".to_string(), + (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 {}", @@ -16473,8 +16475,12 @@ mod tests { // pays the `comp_mode = 0` symbol the writer codes ahead of it, so // single and compound are priced against the same syntax -- 8311 -> // 8325 bytes at q=150 and 33087 -> 33014 at q=60. + // Re-taken on lane-dq: preset 0 codes a per-superblock `delta_qindex` + // from libaom's objective tpl mapping, so every inter picture's + // quantizer grid -- and every decision priced against it -- moved: + // 8325 -> 8419 bytes at q=150 and 33014 -> 33029 at q=60. let pins: [(u8, usize, u64); 2] = - [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)]; + [(150, 8419, 0x123c_501c_9753_1347), (60, 33029, 0x8178_2340_81cd_6630)]; let coded: Vec<(u8, usize, u64)> = pins .iter() .map(|&(q, _, _)| { diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index a5b5b19d..8f766189 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -308,7 +308,35 @@ pub(crate) const I64_ROOT: [bool; 11] = [crate::encode::I64_ROOT; 11]; /// `EC_AV1_DELTAQ=2` forces it on at any preset, `EC_AV1_DELTAQ=0` off. /// Above preset 6 it is inert anyway -- [`TPL_DEPTH`] cuts the lookahead to /// one picture there, so there is no map to vary the quantizer by. -pub(crate) const DELTAQ_RES: [u8; 11] = [4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4]; +pub(crate) const DELTAQ_RES: [u8; 11] = [2, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4]; + +/// lane-dq: the strength of libaom's OBJECTIVE per-superblock mapping +/// ([`crate::encode::deltaq_libaom`]) -- `0` keeps the shipped +/// `deltaq_for_factor` derivation, anything above it takes that fraction of +/// libaom's own offset. Only meaningful where [`DELTAQ_RES`] is on. +/// +/// Preset 0 only, at 0.25. The census (`EC_AV1_DQ_CENSUS=1`) says the two +/// mappings read the SAME tpl map into offsets an order of magnitude apart -- +/// on film B's 12-frame window, ours is `-4..8` with `|mean| 3.0` while +/// libaom's is `-24..32` with `|mean| 14` -- so a strength is the only honest +/// knob between them. Measured on the 48-picture long-GOP gate (BD vs libaom +/// / rav1e), which is the shape his exports have: +/// +/// | arm | film A | film B | +/// |---|---|---| +/// | control (no delta_q) | +25.2/-7.2 | +86.7/+7.3 | +/// | shipped mapping | +24.6/-7.3 | +86.1/+7.5 | +/// | **objective, 0.25** | **+24.4/-7.5** | **+86.0/+7.3** | +/// | objective, 0.5 | +23.8/-7.6 | +86.5/+7.9 | +/// | objective, 1.0 | +24.0/-7.0 | +88.4/+9.7 | +/// +/// 0.25 is the only arm down on both films on the libaom column with neither +/// rav1e column worse; 0.5 buys film A another 0.6 and gives film B back 0.6. +/// The 12-frame gate reads the same arm as flat (film A +21.0/-4.8 -> +/// +20.7/-4.8, film B +24.9/-1.9 -> +24.8/-1.7) and the screen capture is +/// BYTE-IDENTICAL at every arm -- `encode_inter_frame` codes no delta syntax +/// 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]; /// `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.