A per-pyramid-level strength for the objective delta_q
Browse files at this commit657e0436e5adf318792904c6e3199e4529f6a2e3Parent: 1e4e107- feat
- a per-pyramid-level strength for the objective delta_q
3 changed files +91 -8
- crates/ec-av1/src/encode.rs +72 -8
- crates/ec-av1/src/encoder.rs +11 -0
- crates/ec-av1/src/speed.rs +8 -0
crates/ec-av1/src/encode.rsmodified+72 -8
| @@ -10683,17 +10683,58 @@ fn tpl_frame_ratio(ratio: &[f64]) -> f64 { | |||
| 10683 | /// `EC_AV1_DQ_TPL=0`/`1` forces the mapping either way and `EC_AV1_DQ_TPL_K` | 10683 | /// `EC_AV1_DQ_TPL=0`/`1` forces the mapping either way and `EC_AV1_DQ_TPL_K` |
| 10684 | /// the strength (default 1.0 = libaom's own offset when the flag armed it); | 10684 | /// the strength (default 1.0 = libaom's own offset when the flag armed it); |
| 10685 | /// with neither set the preset decides, see [`crate::speed::DQ_TPL_K`]. | 10685 | /// with neither set the preset decides, see [`crate::speed::DQ_TPL_K`]. |
| 10686 | -fn dq_tpl_strength() -> Option<f64> { | 10686 | +fn dq_tpl_strength(level: DqLevel) -> Option<f64> { |
| 10687 | + let m = dq_level_k()[level as usize]; | ||
| 10687 | let env_k = crate::envflags::var("EC_AV1_DQ_TPL_K") | 10688 | let env_k = crate::envflags::var("EC_AV1_DQ_TPL_K") |
| 10688 | .ok() | 10689 | .ok() |
| 10689 | .and_then(|v| v.trim().parse::<f64>().ok()) | 10690 | .and_then(|v| v.trim().parse::<f64>().ok()) |
| 10690 | .filter(|k| k.is_finite() && *k >= 0.0); | 10691 | .filter(|k| k.is_finite() && *k >= 0.0); |
| 10691 | - match crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) { | 10692 | + let base = match crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) { |
| 10692 | Some("0") => return None, | 10693 | Some("0") => return None, |
| 10693 | - Some(_) => return Some(env_k.unwrap_or(1.0)), | 10694 | + Some(_) => Some(env_k.unwrap_or(1.0)), |
| 10694 | - None => {} | 10695 | + None => env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))), |
| 10696 | + }; | ||
| 10697 | + base.map(|k| k * m).filter(|k| *k > 0.0) | ||
| 10698 | +} | ||
| 10699 | + | ||
| 10700 | +/// lane-arfq: which pyramid level the frame asking for a | ||
| 10701 | +/// [`dq_tpl_strength`] sits at. The KEY frame is not here on purpose: it | ||
| 10702 | +/// codes no `delta_q` syntax at all (the whole lever lives in | ||
| 10703 | +/// [`encode_inter_frame`]), so a per-level strength cannot touch it -- the | ||
| 10704 | +/// key's allocation is [`crate::encoder::Pyramid::key_q_offset`]'s job. | ||
| 10705 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 10706 | +pub(crate) enum DqLevel { | ||
| 10707 | + /// The mini-GOP's own hidden `ALTREF`, which every other frame of the | ||
| 10708 | + /// group predicts from (directly or through the mid level). | ||
| 10709 | + TopArf = 0, | ||
| 10710 | + /// A hidden mid/quarter-point `ALTREF`, read by the leaves around it. | ||
| 10711 | + MidArf = 1, | ||
| 10712 | + /// A shown leaf -- and every frame of the flat, non-pyramid path. | ||
| 10713 | + Leaf = 2, | ||
| 10714 | +} | ||
| 10715 | + | ||
| 10716 | +/// The per-level multiplier on the [`dq_tpl_strength`] the preset carries: | ||
| 10717 | +/// `EC_AV1_DQ_K=<top arf>:<mid arf>:<leaf>`, else | ||
| 10718 | +/// [`crate::speed::DQ_LEVEL_K`]. Three fields, not four: the key frame codes | ||
| 10719 | +/// no `delta_q` (see [`DqLevel`]). | ||
| 10720 | +fn dq_level_k() -> [f64; 3] { | ||
| 10721 | + dq_level_k_of(crate::envflags::var("EC_AV1_DQ_K").ok().as_deref()) | ||
| 10722 | +} | ||
| 10723 | + | ||
| 10724 | +/// [`dq_level_k`]'s parse, pure: a missing spec, a missing field and an | ||
| 10725 | +/// unparseable one all keep [`crate::speed::DQ_LEVEL_K`]'s entry. | ||
| 10726 | +fn dq_level_k_of(spec: Option<&str>) -> [f64; 3] { | ||
| 10727 | + let mut k = crate::speed::DQ_LEVEL_K; | ||
| 10728 | + if let Some(spec) = spec { | ||
| 10729 | + for (slot, field) in k.iter_mut().zip(spec.split(':')) { | ||
| 10730 | + if let Some(v) = | ||
| 10731 | + field.trim().parse::<f64>().ok().filter(|v: &f64| v.is_finite() && *v >= 0.0) | ||
| 10732 | + { | ||
| 10733 | + *slot = v; | ||
| 10734 | + } | ||
| 10735 | + } | ||
| 10695 | } | 10736 | } |
| 10696 | - env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))).filter(|k| *k > 0.0) | 10737 | + k |
| 10697 | } | 10738 | } |
| 10698 | 10739 | ||
| 10699 | /// `EC_AV1_DQ_CENSUS=1`: per inter frame, one line comparing the offsets the | 10740 | /// `EC_AV1_DQ_CENSUS=1`: per inter frame, one line comparing the offsets the |
| @@ -11189,6 +11230,10 @@ pub(crate) struct PyramidFrame { | |||
| 11189 | /// `show_frame`: false for a hidden frame, output later by a | 11230 | /// `show_frame`: false for a hidden frame, output later by a |
| 11190 | /// `show_existing_frame` header. | 11231 | /// `show_existing_frame` header. |
| 11191 | pub show_frame: bool, | 11232 | pub show_frame: bool, |
| 11233 | + /// lane-arfq: which level's `delta_q` strength this frame reads | ||
| 11234 | + /// ([`DqLevel`]) -- the caller knows it, since the top ARF and the mid | ||
| 11235 | + /// one share `Level::Arf` and differ only by their q offset. | ||
| 11236 | + pub dq_level: DqLevel, | ||
| 11192 | /// This frame's `ref_frame_sign_bias` (spec 5.9.2). | 11237 | /// This frame's `ref_frame_sign_bias` (spec 5.9.2). |
| 11193 | pub sign_bias: crate::mvstack::SignBiasTable, | 11238 | pub sign_bias: crate::mvstack::SignBiasTable, |
| 11194 | } | 11239 | } |
| @@ -11475,6 +11520,9 @@ pub(crate) fn encode_inter_frame( | |||
| 11475 | // 0.5 loss vs libaom on the desktop capture, so a frame that set | 11520 | // 0.5 loss vs libaom on the desktop capture, so a frame that set |
| 11476 | // `allow_screen_content_tools` codes no delta syntax at all and stays | 11521 | // `allow_screen_content_tools` codes no delta syntax at all and stays |
| 11477 | // byte-identical to the pre-lane encoder. | 11522 | // byte-identical to the pre-lane encoder. |
| 11523 | + // lane-arfq: the level whose `delta_q` strength this frame reads; the | ||
| 11524 | + // flat path (no pyramid) is a leaf. | ||
| 11525 | + let dq_level = pyramid.map_or(DqLevel::Leaf, |p| p.dq_level); | ||
| 11478 | let deltaq_res: Option<i32> = tpl_factors | 11526 | let deltaq_res: Option<i32> = tpl_factors |
| 11479 | .as_ref() | 11527 | .as_ref() |
| 11480 | .filter(|_| !screen) | 11528 | .filter(|_| !screen) |
| @@ -11504,7 +11552,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11504 | }; | 11552 | }; |
| 11505 | let sb_q: Option<Vec<u8>> = match (&tpl_factors, deltaq_res) { | 11553 | let sb_q: Option<Vec<u8>> = match (&tpl_factors, deltaq_res) { |
| 11506 | (Some(f), Some(res)) => { | 11554 | (Some(f), Some(res)) => { |
| 11507 | - let libaom = dq_tpl_strength(); | 11555 | + let libaom = dq_tpl_strength(dq_level); |
| 11508 | Some((0..sb_cols * _sb_rows).map(|sb| sb_qindex(f, res, sb, libaom)).collect()) | 11556 | Some((0..sb_cols * _sb_rows).map(|sb| sb_qindex(f, res, sb, libaom)).collect()) |
| 11509 | } | 11557 | } |
| 11510 | _ => None, | 11558 | _ => None, |
| @@ -11520,7 +11568,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11520 | .map(|sb| i32::from(sb_qindex(f, res, sb, libaom)) - i32::from(base_q_idx)) | 11568 | .map(|sb| i32::from(sb_qindex(f, res, sb, libaom)) - i32::from(base_q_idx)) |
| 11521 | .collect() | 11569 | .collect() |
| 11522 | }; | 11570 | }; |
| 11523 | - let strength = dq_tpl_strength(); | 11571 | + let strength = dq_tpl_strength(dq_level); |
| 11524 | let (ours, aom) = (off(None), off(Some(strength.unwrap_or(1.0)))); | 11572 | let (ours, aom) = (off(None), off(Some(strength.unwrap_or(1.0)))); |
| 11525 | let coded = match (deltaq_res.is_some(), strength) { | 11573 | let coded = match (deltaq_res.is_some(), strength) { |
| 11526 | (false, _) => "none".to_string(), | 11574 | (false, _) => "none".to_string(), |
| @@ -11528,7 +11576,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11528 | (true, Some(k)) => format!("libaom k={k}"), | 11576 | (true, Some(k)) => format!("libaom k={k}"), |
| 11529 | }; | 11577 | }; |
| 11530 | eprintln!( | 11578 | eprintln!( |
| 11531 | - "dq census: base={base_q_idx} res={res} sbs={} coded={coded} r_frame={:.4}\n ours {}\n libaom {}", | 11579 | + "dq census: level={dq_level:?} base={base_q_idx} res={res} sbs={} coded={coded} r_frame={:.4}\n ours {}\n libaom {}", |
| 11532 | sb_cols * _sb_rows, | 11580 | sb_cols * _sb_rows, |
| 11533 | r_frame.unwrap_or(f64::NAN), | 11581 | r_frame.unwrap_or(f64::NAN), |
| 11534 | dq_summary(&ours), | 11582 | dq_summary(&ours), |
| @@ -13013,6 +13061,22 @@ mod tests { | |||
| 13013 | assert!((super::tpl_frame_ratio(&[2.5; 16]) - 2.5).abs() < 1e-9); | 13061 | assert!((super::tpl_frame_ratio(&[2.5; 16]) - 2.5).abs() < 1e-9); |
| 13014 | } | 13062 | } |
| 13015 | 13063 | ||
| 13064 | + /// lane-arfq: the per-level `delta_q` strength -- the spec parses onto | ||
| 13065 | + /// [`super::DqLevel`]'s own indices, a missing or broken field keeps the | ||
| 13066 | + /// shipped default, and the multiplier reaches the strength. | ||
| 13067 | + #[test] | ||
| 13068 | + fn the_per_level_delta_q_strength_indexes_the_levels_it_names() { | ||
| 13069 | + let d = crate::speed::DQ_LEVEL_K; | ||
| 13070 | + assert_eq!(super::dq_level_k_of(None), d, "no spec = the shipped table"); | ||
| 13071 | + assert_eq!(super::dq_level_k_of(Some("2:3:4")), [2.0, 3.0, 4.0]); | ||
| 13072 | + // Short and broken fields fall back per SLOT, not per spec. | ||
| 13073 | + assert_eq!(super::dq_level_k_of(Some("2")), [2.0, d[1], d[2]]); | ||
| 13074 | + assert_eq!(super::dq_level_k_of(Some("2:x:-1")), [2.0, d[1], d[2]]); | ||
| 13075 | + assert_eq!(super::DqLevel::TopArf as usize, 0); | ||
| 13076 | + assert_eq!(super::DqLevel::MidArf as usize, 1); | ||
| 13077 | + assert_eq!(super::DqLevel::Leaf as usize, 2); | ||
| 13078 | + } | ||
| 13079 | + | ||
| 13016 | #[test] | 13080 | #[test] |
| 13017 | fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { | 13081 | fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { |
| 13018 | // Four 64x64 superblocks across: the shipped map forms its ratio per | 13082 | // Four 64x64 superblocks across: the shipped map forms its ratio per |
crates/ec-av1/src/encoder.rsmodified+11 -0
| @@ -1724,6 +1724,16 @@ impl Av1Encoder { | |||
| 1724 | i16::from(r.q_idx(level)) | 1724 | i16::from(r.q_idx(level)) |
| 1725 | }); | 1725 | }); |
| 1726 | let base_q_idx = (base + offset).clamp(1, 255) as u8; | 1726 | let base_q_idx = (base + offset).clamp(1, 255) as u8; |
| 1727 | + // lane-arfq: the top ARF of a mini-GOP is the hidden frame coded at | ||
| 1728 | + // the pyramid's OWN `arf_q_offset` (the mid and quarter levels are | ||
| 1729 | + // `Level::Arf` too but sit at their own offsets), and it is the level | ||
| 1730 | + // the whole group predicts from -- so it reads its own `delta_q` | ||
| 1731 | + // strength (`crate::speed::DQ_LEVEL_K`). | ||
| 1732 | + let dq_level = match (show_frame, self.pyramid.map(|p| p.arf_q_offset)) { | ||
| 1733 | + (true, _) => crate::encode::DqLevel::Leaf, | ||
| 1734 | + (false, Some(arf)) if arf == offset => crate::encode::DqLevel::TopArf, | ||
| 1735 | + (false, _) => crate::encode::DqLevel::MidArf, | ||
| 1736 | + }; | ||
| 1727 | // spec 5.9.2's `ref_frame_sign_bias`, derived from what the slots | 1737 | // spec 5.9.2's `ref_frame_sign_bias`, derived from what the slots |
| 1728 | // this frame names actually hold: a reference whose order hint is | 1738 | // this frame names actually hold: a reference whose order hint is |
| 1729 | // AHEAD of this frame's is backward-biased, and the MV stack scans | 1739 | // AHEAD of this frame's is backward-biased, and the MV stack scans |
| @@ -1792,6 +1802,7 @@ impl Av1Encoder { | |||
| 1792 | last2_slot, | 1802 | last2_slot, |
| 1793 | show_frame, | 1803 | show_frame, |
| 1794 | sign_bias, | 1804 | sign_bias, |
| 1805 | + dq_level, | ||
| 1795 | }), | 1806 | }), |
| 1796 | // lane-arfcen: the group's own sources ARE this frame's window | 1807 | // lane-arfcen: the group's own sources ARE this frame's window |
| 1797 | // (`tpl_window`); it was `&[]` here, which switched the temporal | 1808 | // (`tpl_window`); it was `&[]` here, which switched the temporal |
crates/ec-av1/src/speed.rsmodified+8 -0
| @@ -338,6 +338,14 @@ pub(crate) const DELTAQ_RES: [u8; 11] = [2, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4]; | |||
| 338 | /// on a screen frame at all (lane-dq3's content gate). | 338 | /// on a screen frame at all (lane-dq3's content gate). |
| 339 | 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]; | 339 | 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]; |
| 340 | 340 | ||
| 341 | +/// lane-arfq: a per-LEVEL multiplier on [`DQ_TPL_K`], indexed by | ||
| 342 | +/// [`crate::encode::DqLevel`] -- `[top ARF, mid ARF, leaf]`. The key frame is | ||
| 343 | +/// absent because it codes no `delta_q` syntax at all. | ||
| 344 | +/// | ||
| 345 | +/// `EC_AV1_DQ_K=<top>:<mid>:<leaf>` overrides it for an A/B. The default is | ||
| 346 | +/// flat: see `lanes/arfq.report.md` for the sweep this table records. | ||
| 347 | +pub(crate) const DQ_LEVEL_K: [f64; 3] = [1.0, 1.0, 1.0]; | ||
| 348 | + | ||
| 341 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split | 349 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split |
| 342 | /// trial is withheld. The single biggest wall lever in the tile search. | 350 | /// trial is withheld. The single biggest wall lever in the tile search. |
| 343 | pub(crate) const SPLIT_RD: [f64; 11] = [ | 351 | pub(crate) const SPLIT_RD: [f64; 11] = [ |