forge
Explore
Log in

Don't you dare go Hollow

Preset 0 codes a per-superblock delta_q from libaom's objective tpl mapping at strength 0.25

Browse files at this commit

7680c473aa639f5f17ddad33a46ec03ec9fa9a5eAhmet Kaan GümüşParent: 3676d05
  • feat
    • preset 0 codes a per-superblock delta_q from libaom's objective tpl mapping at strength 0.25
Show full message
feat(av1): preset 0 codes a per-superblock delta_q from libaom's objective tpl mapping at strength 0.25

Long-GOP (48 pictures) BD vs libaom/rav1e: film A +25.2/-7.2 -> +24.4/-7.5,
film B +86.7/+7.3 -> +86.0/+7.3; the 12-frame gate reads flat and the screen
capture stays byte-identical (no delta syntax on a screen frame). Byte pins
re-taken.
crates/ec-av1/src/encode.rsmodified+30 -24
@@ -10485,20 +10485,23 @@ fn tpl_frame_ratio(ratio: &[f64]) -> f64 {
1048510485 m.exp() - 1.0
1048610486 }
1048710487
10488-/// `EC_AV1_DQ_TPL=1`: take the per-superblock qindex from [`deltaq_libaom`]
10489-/// instead of [`deltaq_for_factor`]. Off by default.
10490-fn dq_tpl() -> bool {
10491- crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) == Some("1")
10492-}
10493-
10494-/// The [`deltaq_libaom`] strength multiplier (`EC_AV1_DQ_TPL_K`, default 1.0
10495-/// = libaom's own offset).
10496-fn dq_tpl_k() -> f64 {
10497- crate::envflags::var("EC_AV1_DQ_TPL_K")
10488+/// The [`deltaq_libaom`] strength, or `None` to keep the shipped
10489+/// [`deltaq_for_factor`] derivation.
10490+///
10491+/// `EC_AV1_DQ_TPL=0`/`1` forces the mapping either way and `EC_AV1_DQ_TPL_K`
10492+/// the strength (default 1.0 = libaom's own offset when the flag armed it);
10493+/// with neither set the preset decides, see [`crate::speed::DQ_TPL_K`].
10494+fn dq_tpl_strength() -> Option<f64> {
10495+ let env_k = crate::envflags::var("EC_AV1_DQ_TPL_K")
1049810496 .ok()
1049910497 .and_then(|v| v.trim().parse::<f64>().ok())
10500- .filter(|k| k.is_finite() && *k >= 0.0)
10501- .unwrap_or(1.0)
10498+ .filter(|k| k.is_finite() && *k >= 0.0);
10499+ match crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) {
10500+ Some("0") => return None,
10501+ Some(_) => return Some(env_k.unwrap_or(1.0)),
10502+ None => {}
10503+ }
10504+ env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))).filter(|k| *k > 0.0)
1050210505 }
1050310506
1050410507 /// `EC_AV1_DQ_CENSUS=1`: per inter frame, one line comparing the offsets the
@@ -11301,17 +11304,15 @@ pub(crate) fn encode_inter_frame(
1130111304 // raw dependence ratio; without it (and always without a ratio map, i.e.
1130211305 // on the one-frame area map) the shipped derivation stands.
1130311306 let r_frame = tpl_ratio.as_deref().map(tpl_frame_ratio);
11304- let sb_qindex = |f: &[f64], res: i32, sb: usize, libaom: bool| -> u8 {
11307+ let sb_qindex = |f: &[f64], res: i32, sb: usize, libaom: Option<f64>| -> u8 {
1130511308 match (libaom, tpl_ratio.as_deref(), r_frame) {
11306- (true, Some(r), Some(rf)) => {
11307- deltaq_libaom(base_q_idx, sb_mean(r, sb), rf, dq_tpl_k(), res)
11308- }
11309+ (Some(k), Some(r), Some(rf)) => deltaq_libaom(base_q_idx, sb_mean(r, sb), rf, k, res),
1130911310 _ => deltaq_for_factor(base_q_idx, sb_mean(f, sb), deltaq_k(), deltaq_clamp(), res),
1131011311 }
1131111312 };
1131211313 let sb_q: Option<Vec<u8>> = match (&tpl_factors, deltaq_res) {
1131311314 (Some(f), Some(res)) => {
11314- let libaom = dq_tpl();
11315+ let libaom = dq_tpl_strength();
1131511316 Some((0..sb_cols * _sb_rows).map(|sb| sb_qindex(f, res, sb, libaom)).collect())
1131611317 }
1131711318 _ => None,
@@ -11322,16 +11323,17 @@ pub(crate) fn encode_inter_frame(
1132211323 if dq_census() {
1132311324 if let Some(f) = &tpl_factors {
1132411325 let res = deltaq_res.unwrap_or(4);
11325- let off = |libaom: bool| -> Vec<i32> {
11326+ let off = |libaom: Option<f64>| -> Vec<i32> {
1132611327 (0..sb_cols * _sb_rows)
1132711328 .map(|sb| i32::from(sb_qindex(f, res, sb, libaom)) - i32::from(base_q_idx))
1132811329 .collect()
1132911330 };
11330- let (ours, aom) = (off(false), off(true));
11331- let coded = if deltaq_res.is_some() {
11332- if dq_tpl() { "libaom" } else { "ours" }
11333- } else {
11334- "none"
11331+ let strength = dq_tpl_strength();
11332+ let (ours, aom) = (off(None), off(Some(strength.unwrap_or(1.0))));
11333+ let coded = match (deltaq_res.is_some(), strength) {
11334+ (false, _) => "none".to_string(),
11335+ (true, None) => "ours".to_string(),
11336+ (true, Some(k)) => format!("libaom k={k}"),
1133511337 };
1133611338 eprintln!(
1133711339 "dq census: base={base_q_idx} res={res} sbs={} coded={coded} r_frame={:.4}\n ours {}\n libaom {}",
@@ -16473,8 +16475,12 @@ mod tests {
1647316475 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
1647416476 // single and compound are priced against the same syntax -- 8311 ->
1647516477 // 8325 bytes at q=150 and 33087 -> 33014 at q=60.
16478+ // Re-taken on lane-dq: preset 0 codes a per-superblock `delta_qindex`
16479+ // from libaom's objective tpl mapping, so every inter picture's
16480+ // quantizer grid -- and every decision priced against it -- moved:
16481+ // 8325 -> 8419 bytes at q=150 and 33014 -> 33029 at q=60.
1647616482 let pins: [(u8, usize, u64); 2] =
16477- [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)];
16483+ [(150, 8419, 0x123c_501c_9753_1347), (60, 33029, 0x8178_2340_81cd_6630)];
1647816484 let coded: Vec<(u8, usize, u64)> = pins
1647916485 .iter()
1648016486 .map(|&(q, _, _)| {
crates/ec-av1/src/speed.rsmodified+29 -1
@@ -308,7 +308,35 @@ pub(crate) const I64_ROOT: [bool; 11] = [crate::encode::I64_ROOT; 11];
308308 /// `EC_AV1_DELTAQ=2` forces it on at any preset, `EC_AV1_DELTAQ=0` off.
309309 /// Above preset 6 it is inert anyway -- [`TPL_DEPTH`] cuts the lookahead to
310310 /// one picture there, so there is no map to vary the quantizer by.
311-pub(crate) const DELTAQ_RES: [u8; 11] = [4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4];
311+pub(crate) const DELTAQ_RES: [u8; 11] = [2, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4];
312+
313+/// lane-dq: the strength of libaom's OBJECTIVE per-superblock mapping
314+/// ([`crate::encode::deltaq_libaom`]) -- `0` keeps the shipped
315+/// `deltaq_for_factor` derivation, anything above it takes that fraction of
316+/// libaom's own offset. Only meaningful where [`DELTAQ_RES`] is on.
317+///
318+/// Preset 0 only, at 0.25. The census (`EC_AV1_DQ_CENSUS=1`) says the two
319+/// mappings read the SAME tpl map into offsets an order of magnitude apart --
320+/// on film B's 12-frame window, ours is `-4..8` with `|mean| 3.0` while
321+/// libaom's is `-24..32` with `|mean| 14` -- so a strength is the only honest
322+/// knob between them. Measured on the 48-picture long-GOP gate (BD vs libaom
323+/// / rav1e), which is the shape his exports have:
324+///
325+/// | arm | film A | film B |
326+/// |---|---|---|
327+/// | control (no delta_q) | +25.2/-7.2 | +86.7/+7.3 |
328+/// | shipped mapping | +24.6/-7.3 | +86.1/+7.5 |
329+/// | **objective, 0.25** | **+24.4/-7.5** | **+86.0/+7.3** |
330+/// | objective, 0.5 | +23.8/-7.6 | +86.5/+7.9 |
331+/// | objective, 1.0 | +24.0/-7.0 | +88.4/+9.7 |
332+///
333+/// 0.25 is the only arm down on both films on the libaom column with neither
334+/// rav1e column worse; 0.5 buys film A another 0.6 and gives film B back 0.6.
335+/// The 12-frame gate reads the same arm as flat (film A +21.0/-4.8 ->
336+/// +20.7/-4.8, film B +24.9/-1.9 -> +24.8/-1.7) and the screen capture is
337+/// BYTE-IDENTICAL at every arm -- `encode_inter_frame` codes no delta syntax
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];
312340
313341 /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split
314342 /// trial is withheld. The single biggest wall lever in the tile search.