Merge branch 'lane-dq' (b4f6b455): preset 0 codes per-superblock delta_q from libaom's objective tpl mapping at strength 0.25 (long-GOP film A −0.8, film B −0.7 vs libaom); pins re-taken at the merge 8252 / 33053
Browse files at this commit3 changed files +392 -27
- crates/ec-av1/src/encode.rs +209 -26
- crates/ec-av1/src/speed.rs +29 -1
- lanes/dq.report.md +154 -0
crates/ec-av1/src/encode.rsmodified+209 -26
| @@ -10627,6 +10627,100 @@ fn deltaq_for_factor(base: u8, f: f64, k: f64, clamp: i32, res: i32) -> u8 { | |||
| 10627 | (i32::from(base) + delta).clamp(1, 255) as u8 | 10627 | (i32::from(base) + delta).clamp(1, 255) as u8 |
| 10628 | } | 10628 | } |
| 10629 | 10629 | ||
| 10630 | +/// lane-dq: derive the per-superblock qindex libaom's `deltaq-mode 1` | ||
| 10631 | +/// objective would, from the SAME tpl dependence ratio our own map already | ||
| 10632 | +/// carries (`EC_AV1_DQ_TPL=1`). | ||
| 10633 | +/// | ||
| 10634 | +/// libaom (`av1_get_q_for_deltaq_objective`, encodeframe_utils.c) forms a | ||
| 10635 | +/// superblock dependence ratio `rk = exp((intra_cost - mc_dep_cost)/w)` in the | ||
| 10636 | +/// LOG domain, divides the frame-level `r0` (`encoder_utils.c:627`, the very | ||
| 10637 | +/// same expression at frame scope) by it, and asks | ||
| 10638 | +/// `av1_get_deltaq_offset(bit_depth, base, beta)` for the qindex whose | ||
| 10639 | +/// **DC** step is `dc_q(base)/sqrt(beta)`. With per-cell costs `intra` and | ||
| 10640 | +/// `intra + mc_dep`, `rk` is `1/(1 + r)` for this crate's `r = mc_dep/intra`, | ||
| 10641 | +/// so `beta = (1 + r_sb) / (1 + r_frame)` with `r_frame` read the log-mean way | ||
| 10642 | +/// libaom reads `r0`. The offset is clamped to libaom's own | ||
| 10643 | +/// `+-(delta_q_res * 9 - 1)`, which at `res = 4` is +-8 steps -- more than | ||
| 10644 | +/// twice the +-16 qindex [`deltaq_for_factor`] allows. | ||
| 10645 | +/// | ||
| 10646 | +/// Two things differ from [`deltaq_for_factor`] and both are the point of the | ||
| 10647 | +/// arm: the table inverted is `dc_q` not `ac_q`, and the ratio is read raw | ||
| 10648 | +/// (`sqrt(1 + r)`) rather than through the map's `log2`-compressed, | ||
| 10649 | +/// `TPL_POWER`-flattened, mean-normalised lambda factor -- so this mapping is | ||
| 10650 | +/// much stronger for the same map. `strength` scales the offset. | ||
| 10651 | +fn deltaq_libaom(base: u8, r_sb: f64, r_frame: f64, strength: f64, res: i32) -> u8 { | ||
| 10652 | + let beta = ((1.0 + r_sb.max(0.0)) / (1.0 + r_frame.max(0.0))).max(1e-6); | ||
| 10653 | + let target = f64::from(crate::quant::dc_q(8, i32::from(base))) / beta.sqrt(); | ||
| 10654 | + let exact = (1..=255) | ||
| 10655 | + .min_by(|&a, &b| { | ||
| 10656 | + let d = |q: i32| (f64::from(crate::quant::dc_q(8, q)) - target).abs(); | ||
| 10657 | + d(a).partial_cmp(&d(b)).expect("dc_q is finite") | ||
| 10658 | + }) | ||
| 10659 | + .expect("the qindex range is not empty") | ||
| 10660 | + - i32::from(base); | ||
| 10661 | + let want = exact as f64 * strength; | ||
| 10662 | + // Snapped onto the `res` grid first, so the coded delta is a whole number | ||
| 10663 | + // of steps, then bounded by libaom's own `res * 9 - 1`. | ||
| 10664 | + let max_steps = (res * 9 - 1) / res; | ||
| 10665 | + let steps = (want / f64::from(res)).round() as i32; | ||
| 10666 | + let delta = steps.clamp(-max_steps, max_steps) * res; | ||
| 10667 | + (i32::from(base) + delta).clamp(1, 255) as u8 | ||
| 10668 | +} | ||
| 10669 | + | ||
| 10670 | +/// The frame-level `r0` libaom's objective divides by: the log-mean of | ||
| 10671 | +/// `1 + r` over the frame's cells, minus 1. | ||
| 10672 | +fn tpl_frame_ratio(ratio: &[f64]) -> f64 { | ||
| 10673 | + if ratio.is_empty() { | ||
| 10674 | + return 0.0; | ||
| 10675 | + } | ||
| 10676 | + let m = ratio.iter().map(|&r| (1.0 + r.max(0.0)).ln()).sum::<f64>() / ratio.len() as f64; | ||
| 10677 | + m.exp() - 1.0 | ||
| 10678 | +} | ||
| 10679 | + | ||
| 10680 | +/// The [`deltaq_libaom`] strength, or `None` to keep the shipped | ||
| 10681 | +/// [`deltaq_for_factor`] derivation. | ||
| 10682 | +/// | ||
| 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); | ||
| 10685 | +/// with neither set the preset decides, see [`crate::speed::DQ_TPL_K`]. | ||
| 10686 | +fn dq_tpl_strength() -> Option<f64> { | ||
| 10687 | + let env_k = crate::envflags::var("EC_AV1_DQ_TPL_K") | ||
| 10688 | + .ok() | ||
| 10689 | + .and_then(|v| v.trim().parse::<f64>().ok()) | ||
| 10690 | + .filter(|k| k.is_finite() && *k >= 0.0); | ||
| 10691 | + match crate::envflags::var("EC_AV1_DQ_TPL").ok().as_deref().map(str::trim) { | ||
| 10692 | + Some("0") => return None, | ||
| 10693 | + Some(_) => return Some(env_k.unwrap_or(1.0)), | ||
| 10694 | + None => {} | ||
| 10695 | + } | ||
| 10696 | + env_k.or_else(|| Some(crate::speed::at(&crate::speed::DQ_TPL_K))).filter(|k| *k > 0.0) | ||
| 10697 | +} | ||
| 10698 | + | ||
| 10699 | +/// `EC_AV1_DQ_CENSUS=1`: per inter frame, one line comparing the offsets the | ||
| 10700 | +/// shipped mapping ([`deltaq_for_factor`]) WOULD emit against the ones | ||
| 10701 | +/// libaom's objective ([`deltaq_libaom`]) gives for the same superblocks and | ||
| 10702 | +/// the same tpl map -- and which of the two the frame actually coded. | ||
| 10703 | +fn dq_census() -> bool { | ||
| 10704 | + crate::envflags::var("EC_AV1_DQ_CENSUS").ok().as_deref().map(str::trim) == Some("1") | ||
| 10705 | +} | ||
| 10706 | + | ||
| 10707 | +/// The histogram/extent summary [`dq_census`] prints for one set of offsets. | ||
| 10708 | +fn dq_summary(offsets: &[i32]) -> String { | ||
| 10709 | + let n = offsets.len().max(1); | ||
| 10710 | + let (lo, hi) = offsets.iter().fold((0i32, 0i32), |(l, h), &o| (l.min(o), h.max(o))); | ||
| 10711 | + let nz = offsets.iter().filter(|&&o| o != 0).count(); | ||
| 10712 | + let mean = offsets.iter().map(|&o| f64::from(o)).sum::<f64>() / n as f64; | ||
| 10713 | + let absmean = offsets.iter().map(|&o| f64::from(o.abs())).sum::<f64>() / n as f64; | ||
| 10714 | + let mut levels: Vec<i32> = offsets.to_vec(); | ||
| 10715 | + levels.sort_unstable(); | ||
| 10716 | + levels.dedup(); | ||
| 10717 | + format!( | ||
| 10718 | + "range {lo}..{hi} mean {mean:+.2} |mean| {absmean:.2} nonzero {nz}/{} levels {}", | ||
| 10719 | + offsets.len(), | ||
| 10720 | + levels.len() | ||
| 10721 | + ) | ||
| 10722 | +} | ||
| 10723 | + | ||
| 10630 | /// How many DISTINCT per-superblock quantizer levels the last inter frame's | 10724 | /// How many DISTINCT per-superblock quantizer levels the last inter frame's |
| 10631 | /// [`sb_q`](encode_inter_frame) grid carried -- lane-deltaq's fire counter | 10725 | /// [`sb_q`](encode_inter_frame) grid carried -- lane-deltaq's fire counter |
| 10632 | /// (class `gate-blind-to-feature`: every exactness assert below passes on a | 10726 | /// (class `gate-blind-to-feature`: every exactness assert below passes on a |
| @@ -10935,7 +11029,16 @@ fn tpl_sb_factors(map: &[f64], cells_x: usize, cells_y: usize, k: f64) -> Vec<f6 | |||
| 10935 | /// more distortion-averse (smaller lambda), and the frame's TOTAL lambda | 11029 | /// more distortion-averse (smaller lambda), and the frame's TOTAL lambda |
| 10936 | /// scale only moves around, never up or down -- a global move is what | 11030 | /// scale only moves around, never up or down -- a global move is what |
| 10937 | /// lane-av1lambda already measured inert. | 11031 | /// lane-av1lambda already measured inert. |
| 10938 | -fn tpl_lambda_factors(frames: &[&[u8]], width: usize, height: usize, k: f64) -> Vec<f64> { | 11032 | +/// Returns the per-cell lambda factors AND the per-cell dependence ratio |
| 11033 | +/// `mc_dep / intra` they were derived from -- the second half is what | ||
| 11034 | +/// [`deltaq_libaom`] needs, since the factor alone has been through a | ||
| 11035 | +/// mean normalisation the raw ratio cannot be recovered across. | ||
| 11036 | +fn tpl_lambda_factors( | ||
| 11037 | + frames: &[&[u8]], | ||
| 11038 | + width: usize, | ||
| 11039 | + height: usize, | ||
| 11040 | + k: f64, | ||
| 11041 | +) -> (Vec<f64>, Vec<f64>) { | ||
| 10939 | let start = std::time::Instant::now(); | 11042 | let start = std::time::Instant::now(); |
| 10940 | let (cells_x, cells_y) = (width / 16, height / 16); | 11043 | let (cells_x, cells_y) = (width / 16, height / 16); |
| 10941 | let cells = cells_x * cells_y; | 11044 | let cells = cells_x * cells_y; |
| @@ -11054,7 +11157,7 @@ fn tpl_lambda_factors(frames: &[&[u8]], width: usize, height: usize, k: f64) -> | |||
| 11054 | TPL_NANOS.load(std::sync::atomic::Ordering::Relaxed) as f64 / 1e6, | 11157 | TPL_NANOS.load(std::sync::atomic::Ordering::Relaxed) as f64 / 1e6, |
| 11055 | ); | 11158 | ); |
| 11056 | } | 11159 | } |
| 11057 | - out | 11160 | + (out, ratio) |
| 11058 | } | 11161 | } |
| 11059 | 11162 | ||
| 11060 | /// Where one inter frame sits in a coding-order pyramid: which DPB slots its | 11163 | /// Where one inter frame sits in a coding-order pyramid: which DPB slots its |
| @@ -11326,11 +11429,15 @@ pub(crate) fn encode_inter_frame( | |||
| 11326 | let tpl_sources: Vec<Vec<u8>> = | 11429 | let tpl_sources: Vec<Vec<u8>> = |
| 11327 | window.iter().map(|n| n.y.iter().map(|&v| v as u8).collect()).collect(); | 11430 | window.iter().map(|n| n.y.iter().map(|&v| v as u8).collect()).collect(); |
| 11328 | let tpl_cells_x = picture.width / 16; | 11431 | let tpl_cells_x = picture.width / 16; |
| 11329 | - let tpl_factors: Option<Vec<f64>> = (tpl_k > 0.0 && !tpl_sources.is_empty()).then(|| { | 11432 | + // `.1` is the propagating map's raw per-cell dependence ratio, which only |
| 11433 | + // [`deltaq_libaom`] reads; the one-frame area map has no such ratio. | ||
| 11434 | + let tpl_both: Option<(Vec<f64>, Option<Vec<f64>>)> = | ||
| 11435 | + (tpl_k > 0.0 && !tpl_sources.is_empty()).then(|| { | ||
| 11330 | if tpl_propagating() { | 11436 | if tpl_propagating() { |
| 11331 | let mut frames: Vec<&[u8]> = vec![&picture_y8]; | 11437 | let mut frames: Vec<&[u8]> = vec![&picture_y8]; |
| 11332 | frames.extend(tpl_sources.iter().map(Vec::as_slice)); | 11438 | frames.extend(tpl_sources.iter().map(Vec::as_slice)); |
| 11333 | - tpl_lambda_factors(&frames, picture.width, picture.height, tpl_k) | 11439 | + let (f, r) = tpl_lambda_factors(&frames, picture.width, picture.height, tpl_k); |
| 11440 | + (f, Some(r)) | ||
| 11334 | } else { | 11441 | } else { |
| 11335 | // The shipped map is one factor per 64x64; it is spread over that | 11442 | // The shipped map is one factor per 64x64; it is spread over that |
| 11336 | // superblock's own 16 cells so the application site below (which | 11443 | // superblock's own 16 cells so the application site below (which |
| @@ -11340,11 +11447,16 @@ pub(crate) fn encode_inter_frame( | |||
| 11340 | tpl_area_map(&picture_y8, &tpl_sources[0], picture.width, picture.height); | 11447 | tpl_area_map(&picture_y8, &tpl_sources[0], picture.width, picture.height); |
| 11341 | let cells_y = picture.height / 16; | 11448 | let cells_y = picture.height / 16; |
| 11342 | let sb = tpl_sb_factors(&map, tpl_cells_x, cells_y, tpl_k); | 11449 | let sb = tpl_sb_factors(&map, tpl_cells_x, cells_y, tpl_k); |
| 11343 | - (0..tpl_cells_x * cells_y) | 11450 | + let f: Vec<f64> = (0..tpl_cells_x * cells_y) |
| 11344 | .map(|c| sb[(c / tpl_cells_x / 4) * (tpl_cells_x / 4) + (c % tpl_cells_x) / 4]) | 11451 | .map(|c| sb[(c / tpl_cells_x / 4) * (tpl_cells_x / 4) + (c % tpl_cells_x) / 4]) |
| 11345 | - .collect() | 11452 | + .collect(); |
| 11453 | + (f, None) | ||
| 11346 | } | 11454 | } |
| 11347 | }); | 11455 | }); |
| 11456 | + let (tpl_factors, tpl_ratio) = match tpl_both { | ||
| 11457 | + Some((f, r)) => (Some(f), r), | ||
| 11458 | + None => (None, None), | ||
| 11459 | + }; | ||
| 11348 | 11460 | ||
| 11349 | // The true grid ([`crate::tile::block_grid`]'s ceiling), not the padded | 11461 | // The true grid ([`crate::tile::block_grid`]'s ceiling), not the padded |
| 11350 | // coding surface's: `blocks` carries one entry per 32x32 block the tile | 11462 | // coding surface's: `blocks` carries one entry per 32x32 block the tile |
| @@ -11367,30 +11479,63 @@ pub(crate) fn encode_inter_frame( | |||
| 11367 | .as_ref() | 11479 | .as_ref() |
| 11368 | .filter(|_| !screen) | 11480 | .filter(|_| !screen) |
| 11369 | .and(deltaq_res_log2().map(|r| 1i32 << r)); | 11481 | .and(deltaq_res_log2().map(|r| 1i32 << r)); |
| 11370 | - let sb_q: Option<Vec<u8>> = match (&tpl_factors, deltaq_res) { | 11482 | + // The same 16-cell mean the superblock's own lambda takes, clamped to the |
| 11371 | - (Some(f), Some(res)) => { | 11483 | + // map the frame actually has -- read over the factors or over the raw |
| 11372 | - let (k, clamp) = (deltaq_k(), deltaq_clamp()); | 11484 | + // dependence ratios, whichever mapping is asking. |
| 11373 | - Some( | 11485 | + let sb_mean = |m: &[f64], sb: usize| { |
| 11374 | - (0..sb_cols * _sb_rows) | ||
| 11375 | - .map(|sb| { | ||
| 11376 | let (sb_r, sb_c) = (sb / sb_cols, sb % sb_cols); | 11486 | let (sb_r, sb_c) = (sb / sb_cols, sb % sb_cols); |
| 11377 | - // The same 16-cell mean the superblock's own lambda | 11487 | + (0..16) |
| 11378 | - // takes, clamped to the map the frame actually has. | ||
| 11379 | - let mean = (0..16) | ||
| 11380 | .map(|i| { | 11488 | .map(|i| { |
| 11381 | let (cy, cx) = (sb_r * 4 + i / 4, sb_c * 4 + i % 4); | 11489 | let (cy, cx) = (sb_r * 4 + i / 4, sb_c * 4 + i % 4); |
| 11382 | - f[(cy.min(f.len() / tpl_cells_x - 1)) * tpl_cells_x | 11490 | + m[(cy.min(m.len() / tpl_cells_x - 1)) * tpl_cells_x + cx.min(tpl_cells_x - 1)] |
| 11383 | - + cx.min(tpl_cells_x - 1)] | ||
| 11384 | }) | 11491 | }) |
| 11385 | .sum::<f64>() | 11492 | .sum::<f64>() |
| 11386 | - / 16.0; | 11493 | + / 16.0 |
| 11387 | - deltaq_for_factor(base_q_idx, mean, k, clamp, res) | 11494 | + }; |
| 11388 | - }) | 11495 | + // lane-dq: `EC_AV1_DQ_TPL=1` takes libaom's own objective mapping off the |
| 11389 | - .collect(), | 11496 | + // raw dependence ratio; without it (and always without a ratio map, i.e. |
| 11390 | - ) | 11497 | + // on the one-frame area map) the shipped derivation stands. |
| 11498 | + let r_frame = tpl_ratio.as_deref().map(tpl_frame_ratio); | ||
| 11499 | + let sb_qindex = |f: &[f64], res: i32, sb: usize, libaom: Option<f64>| -> u8 { | ||
| 11500 | + match (libaom, tpl_ratio.as_deref(), r_frame) { | ||
| 11501 | + (Some(k), Some(r), Some(rf)) => deltaq_libaom(base_q_idx, sb_mean(r, sb), rf, k, res), | ||
| 11502 | + _ => deltaq_for_factor(base_q_idx, sb_mean(f, sb), deltaq_k(), deltaq_clamp(), res), | ||
| 11503 | + } | ||
| 11504 | + }; | ||
| 11505 | + let sb_q: Option<Vec<u8>> = match (&tpl_factors, deltaq_res) { | ||
| 11506 | + (Some(f), Some(res)) => { | ||
| 11507 | + let libaom = dq_tpl_strength(); | ||
| 11508 | + Some((0..sb_cols * _sb_rows).map(|sb| sb_qindex(f, res, sb, libaom)).collect()) | ||
| 11391 | } | 11509 | } |
| 11392 | _ => None, | 11510 | _ => None, |
| 11393 | }; | 11511 | }; |
| 11512 | + // lane-dq census: both mappings on the SAME superblocks, whether or not | ||
| 11513 | + // the frame codes any delta syntax (`deltaq_res` is `None` with the lever | ||
| 11514 | + // off, and the census then prices the default `res = 4` grid). | ||
| 11515 | + if dq_census() { | ||
| 11516 | + if let Some(f) = &tpl_factors { | ||
| 11517 | + let res = deltaq_res.unwrap_or(4); | ||
| 11518 | + let off = |libaom: Option<f64>| -> Vec<i32> { | ||
| 11519 | + (0..sb_cols * _sb_rows) | ||
| 11520 | + .map(|sb| i32::from(sb_qindex(f, res, sb, libaom)) - i32::from(base_q_idx)) | ||
| 11521 | + .collect() | ||
| 11522 | + }; | ||
| 11523 | + let strength = dq_tpl_strength(); | ||
| 11524 | + let (ours, aom) = (off(None), off(Some(strength.unwrap_or(1.0)))); | ||
| 11525 | + let coded = match (deltaq_res.is_some(), strength) { | ||
| 11526 | + (false, _) => "none".to_string(), | ||
| 11527 | + (true, None) => "ours".to_string(), | ||
| 11528 | + (true, Some(k)) => format!("libaom k={k}"), | ||
| 11529 | + }; | ||
| 11530 | + eprintln!( | ||
| 11531 | + "dq census: base={base_q_idx} res={res} sbs={} coded={coded} r_frame={:.4}\n ours {}\n libaom {}", | ||
| 11532 | + sb_cols * _sb_rows, | ||
| 11533 | + r_frame.unwrap_or(f64::NAN), | ||
| 11534 | + dq_summary(&ours), | ||
| 11535 | + dq_summary(&aom), | ||
| 11536 | + ); | ||
| 11537 | + } | ||
| 11538 | + } | ||
| 11394 | if let Some(res) = deltaq_res.filter(|_| sb_q.is_some()) { | 11539 | if let Some(res) = deltaq_res.filter(|_| sb_q.is_some()) { |
| 11395 | header.delta.q_present = true; | 11540 | header.delta.q_present = true; |
| 11396 | header.delta.q_res = res.trailing_zeros() as u8; | 11541 | header.delta.q_res = res.trailing_zeros() as u8; |
| @@ -12834,6 +12979,40 @@ mod tests { | |||
| 12834 | assert_eq!(plane.block_sse(32, 32, 16, &block), (190.0 * 190.0) * 64.0); | 12979 | assert_eq!(plane.block_sse(32, 32, 16, &block), (190.0 * 190.0) * 64.0); |
| 12835 | } | 12980 | } |
| 12836 | 12981 | ||
| 12982 | + /// lane-dq: libaom's objective mapping ([`super::deltaq_libaom`]) must | ||
| 12983 | + /// spend WHERE THE WINDOW LEANS, stay congruent to the base on the | ||
| 12984 | + /// `delta_q_res` grid, and stop at libaom's own `+-(res * 9 - 1)` bound. | ||
| 12985 | + #[test] | ||
| 12986 | + fn the_objective_delta_q_mapping_leans_the_right_way_and_stops_at_libaom_s_clamp() { | ||
| 12987 | + let (base, res) = (162u8, 4i32); | ||
| 12988 | + let rf = 2.0; | ||
| 12989 | + // A superblock the window leans on more than the frame average is | ||
| 12990 | + // coded FINER (lower qindex); a slack one coarser; the average one | ||
| 12991 | + // exactly at the base. | ||
| 12992 | + assert!(super::deltaq_libaom(base, 8.0, rf, 1.0, res) < base, "leaned-on SB must go finer"); | ||
| 12993 | + assert!(super::deltaq_libaom(base, 0.2, rf, 1.0, res) > base, "idle SB must go coarser"); | ||
| 12994 | + assert_eq!(super::deltaq_libaom(base, rf, rf, 1.0, res), base, "the average SB moves 0"); | ||
| 12995 | + // Every offset is a whole number of steps and inside +-(res * 9 - 1). | ||
| 12996 | + let max = ((res * 9 - 1) / res) * res; | ||
| 12997 | + for r in [0.0f64, 0.01, 0.5, 1.0, 3.0, 50.0, 1e6] { | ||
| 12998 | + for k in [0.5f64, 1.0, 1.5] { | ||
| 12999 | + let q = i32::from(super::deltaq_libaom(base, r, rf, k, res)); | ||
| 13000 | + let d = q - i32::from(base); | ||
| 13001 | + assert_eq!(d % res, 0, "r={r} k={k}: offset {d} is not a whole {res}-step"); | ||
| 13002 | + assert!(d.abs() <= max, "r={r} k={k}: offset {d} past libaom's +-{max}"); | ||
| 13003 | + } | ||
| 13004 | + } | ||
| 13005 | + // The clamp is REACHED from both ends, so the bound is a real bound | ||
| 13006 | + // and not a formula that never gets there. | ||
| 13007 | + assert_eq!(i32::from(super::deltaq_libaom(base, 1e9, rf, 1.0, res)) - i32::from(base), -max); | ||
| 13008 | + assert_eq!(i32::from(super::deltaq_libaom(base, 0.0, 1e9, 1.0, res)) - i32::from(base), max); | ||
| 13009 | + // A stronger `k` never moves less than a weaker one. | ||
| 13010 | + let d = |k: f64| (i32::from(super::deltaq_libaom(base, 8.0, rf, k, res)) - i32::from(base)).abs(); | ||
| 13011 | + assert!(d(0.5) <= d(1.0) && d(1.0) <= d(1.5), "strength must be monotone: {} {} {}", d(0.5), d(1.0), d(1.5)); | ||
| 13012 | + // The frame reference is libaom's log-mean: flat in, the same value out. | ||
| 13013 | + assert!((super::tpl_frame_ratio(&[2.5; 16]) - 2.5).abs() < 1e-9); | ||
| 13014 | + } | ||
| 13015 | + | ||
| 12837 | #[test] | 13016 | #[test] |
| 12838 | fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { | 13017 | fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() { |
| 12839 | // Four 64x64 superblocks across: the shipped map forms its ratio per | 13018 | // Four 64x64 superblocks across: the shipped map forms its ratio per |
| @@ -12843,7 +13022,7 @@ mod tests { | |||
| 12843 | let (w, h) = (256usize, 64usize); | 13022 | let (w, h) = (256usize, 64usize); |
| 12844 | let uniform: Vec<u8> = (0..w * h).map(|i| ((i * 37) % 251) as u8).collect(); | 13023 | let uniform: Vec<u8> = (0..w * h).map(|i| ((i * 37) % 251) as u8).collect(); |
| 12845 | let frames: Vec<&[u8]> = vec![&uniform, &uniform, &uniform]; | 13024 | let frames: Vec<&[u8]> = vec![&uniform, &uniform, &uniform]; |
| 12846 | - let f = super::tpl_lambda_factors(&frames, w, h, 1.0); | 13025 | + let (f, _) = super::tpl_lambda_factors(&frames, w, h, 1.0); |
| 12847 | assert_eq!(f.len(), 64, "16x4 cells of 16x16"); | 13026 | assert_eq!(f.len(), 64, "16x4 cells of 16x16"); |
| 12848 | for (i, &v) in f.iter().enumerate() { | 13027 | for (i, &v) in f.iter().enumerate() { |
| 12849 | assert!((v - 1.0).abs() < 1e-6, "cell {i} factor {v}, a flat map must move no lambda"); | 13028 | assert!((v - 1.0).abs() < 1e-6, "cell {i} factor {v}, a flat map must move no lambda"); |
| @@ -12857,7 +13036,7 @@ mod tests { | |||
| 12857 | let frames: Vec<&[u8]> = vec![&split, &split, &split]; | 13036 | let frames: Vec<&[u8]> = vec![&split, &split, &split]; |
| 12858 | let cells_x = w / 16; | 13037 | let cells_x = w / 16; |
| 12859 | for k in [0.25f64, 0.5, 1.0, 2.0] { | 13038 | for k in [0.25f64, 0.5, 1.0, 2.0] { |
| 12860 | - let f = super::tpl_lambda_factors(&frames, w, h, k); | 13039 | + let (f, _) = super::tpl_lambda_factors(&frames, w, h, k); |
| 12861 | for r in 0..h / 16 { | 13040 | for r in 0..h / 16 { |
| 12862 | let (lean, idle) = (f[r * cells_x], f[r * cells_x + cells_x - 1]); | 13041 | let (lean, idle) = (f[r * cells_x], f[r * cells_x + cells_x - 1]); |
| 12863 | assert!(lean < 1.0, "k={k} row {r}: leaned-on cell factor {lean} must be < 1"); | 13042 | assert!(lean < 1.0, "k={k} row {r}: leaned-on cell factor {lean} must be < 1"); |
| @@ -16565,8 +16744,12 @@ mod tests { | |||
| 16565 | // ([`crate::motion::MV_SUBPEL_ITERS`]), so every inter block's vector | 16744 | // ([`crate::motion::MV_SUBPEL_ITERS`]), so every inter block's vector |
| 16566 | // -- and with it its residual -- moved: 8325 -> 8288 bytes at q=150 | 16745 | // -- and with it its residual -- moved: 8325 -> 8288 bytes at q=150 |
| 16567 | // and 33014 -> 33090 at q=60. | 16746 | // and 33014 -> 33090 at q=60. |
| 16568 | - let pins: [(u8, usize, u64); 2] = | 16747 | + // Re-taken on lane-dq: preset 0 codes a per-superblock `delta_qindex` |
| 16569 | - [(150, 8288, 0x241d_8115_0a6d_0f34), (60, 33090, 0x7e02_777c_01b4_f035)]; | 16748 | + // from libaom's objective tpl mapping, so every inter picture's |
| 16749 | + // quantizer grid -- and every decision priced against it -- moved | ||
| 16750 | + // (8325 -> 8419 / 33014 -> 33029 on its own base); re-taken once more | ||
| 16751 | + // at the merge of both lanes. | ||
| 16752 | + let pins: [(u8, usize, u64); 2] = [(150, 8252, 0x5b6e40a2b5498988), (60, 33053, 0x9a0abfc234eccc37)]; | ||
| 16570 | let coded: Vec<(u8, usize, u64)> = pins | 16753 | let coded: Vec<(u8, usize, u64)> = pins |
| 16571 | .iter() | 16754 | .iter() |
| 16572 | .map(|&(q, _, _)| { | 16755 | .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]; | |||
| 308 | /// `EC_AV1_DELTAQ=2` forces it on at any preset, `EC_AV1_DELTAQ=0` off. | 308 | /// `EC_AV1_DELTAQ=2` forces it on at any preset, `EC_AV1_DELTAQ=0` off. |
| 309 | /// Above preset 6 it is inert anyway -- [`TPL_DEPTH`] cuts the lookahead to | 309 | /// Above preset 6 it is inert anyway -- [`TPL_DEPTH`] cuts the lookahead to |
| 310 | /// one picture there, so there is no map to vary the quantizer by. | 310 | /// 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]; | ||
| 312 | 340 | ||
| 313 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split | 341 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split |
| 314 | /// trial is withheld. The single biggest wall lever in the tile search. | 342 | /// trial is withheld. The single biggest wall lever in the tile search. |
lanes/dq.report.mdadded+154 -0
| @@ -0,0 +1,154 @@ | |||
| 1 | +# lane-dq — the per-superblock `delta_q` mapping, ours vs libaom's objective | ||
| 2 | + | ||
| 3 | +## 0. The charter's premise, corrected first | ||
| 4 | + | ||
| 5 | +The charter asked what the existing `delta_q` derives its offsets from and | ||
| 6 | +"whether the tpl map feeds it at all". It does, and has since lane-deltaq: | ||
| 7 | +`encode::deltaq_for_factor` inverts the tpl LAMBDA factor through the `ac_q` | ||
| 8 | +table. Nothing was variance-driven and no map was inert. What was never | ||
| 9 | +measured is that OUR mapping and LIBAOM's read the same map into offsets an | ||
| 10 | +order of magnitude apart — that is this lane's finding, and the strength | ||
| 11 | +between them is what shipped. | ||
| 12 | + | ||
| 13 | +## 1. Census instrument — `EC_AV1_DQ_CENSUS=1` | ||
| 14 | + | ||
| 15 | +One line per inter frame from `encode_inter_frame`, printing, for the SAME | ||
| 16 | +superblocks and the same tpl map, the offsets the shipped mapping | ||
| 17 | +(`deltaq_for_factor`) emits and the ones libaom's objective | ||
| 18 | +(`deltaq_libaom`, new) would, plus which of the two the frame actually coded | ||
| 19 | +(`coded=none` when the lever is off — the census then prices a hypothetical | ||
| 20 | +`res = 4` grid). Zero cost when unset. | ||
| 21 | + | ||
| 22 | +Film B, native gate crop `1920x1024` at `00:40:00`, 12 frames, q=150, | ||
| 23 | +preset 0, lever OFF (`enc_probe … gate 0 12 150`, 27634 B = the gate's own | ||
| 24 | +q150 point). 480 superblocks per frame, `base=162`, `res=4`: | ||
| 25 | + | ||
| 26 | +| frame | r_frame | ours (range, \|mean\|, nonzero, levels) | libaom (same) | | ||
| 27 | +|---|---|---|---| | ||
| 28 | +| … | 2.70 | −4..8, 3.04, 321/480, 4 | −24..32, 13.97, 450/480, 15 | | ||
| 29 | +| … | 2.85 | −4..8, 2.81, 308/480, 4 | −28..32, 14.94, 454/480, 16 | | ||
| 30 | +| … | 2.83 | −4..8, 2.86, 307/480, 4 | −24..32, 14.32, 448/480, 15 | | ||
| 31 | +| … | 2.51 | −4..8, 2.88, 305/480, 4 | −24..32, 14.45, 451/480, 15 | | ||
| 32 | +| … | 2.06 | −4..8, 2.93, 312/480, 4 | −24..32, 13.84, 449/480, 15 | | ||
| 33 | +| … | 1.74 | −4..8, 2.60, 294/480, 4 | −24..32, 13.58, 447/480, 15 | | ||
| 34 | +| … | 1.27 | −4..8, 2.39, 280/480, 4 | −20..32, 12.03, 448/480, 14 | | ||
| 35 | +| … | 0.91 | −4..4, 1.76, 211/480, 3 | −20..28, 11.07, 450/480, 13 | | ||
| 36 | +| … | 0.45 | −4..4, 1.38, 166/480, 3 | −12..12, 5.87, 409/480, 7 | | ||
| 37 | + | ||
| 38 | +**The shipped mapping moves ~5x less quantizer than libaom's on the identical | ||
| 39 | +map**, and it never reaches its own `±16` clamp (its widest offset is `+8`), | ||
| 40 | +so the clamp is NOT what bounds it: the `log2(1+r)` compression plus | ||
| 41 | +`TPL_POWER = 0.5` plus the mean normalisation is. libaom's own clamp | ||
| 42 | +(`±(delta_q_res * 9 − 1)`, `±32` at `res = 4`) IS reached. The census matches | ||
| 43 | +lane-cen3's census fact that libaom codes `delta_q` in every inter frame while | ||
| 44 | +we coded it in none at preset 0. | ||
| 45 | + | ||
| 46 | +The census's `libaom` column prints the strength actually in force, so it is | ||
| 47 | +raw libaom (`k = 1`) whenever the lever is off and the shipped `k = 0.25` | ||
| 48 | +otherwise — the shipped default on the same clip prints | ||
| 49 | +`coded=libaom k=0.25 … libaom range −4..4 |mean| 1.21..1.83`, i.e. the | ||
| 50 | +strength brings the objective mapping down to the shipped mapping's own | ||
| 51 | +extent while keeping its SHAPE (different superblocks, 145..220 nonzero vs | ||
| 52 | +166..211), and codes 28304 B against the control's 27634 B. | ||
| 53 | + | ||
| 54 | +`aomdec -EC_TRACE_TPL` was not used: it exposes no per-SB qindex, so libaom's | ||
| 55 | +mapping is reimplemented on OUR map instead, which is also the only way to | ||
| 56 | +compare the two mappings at one map (class `shared-oracle blindness` cuts the | ||
| 57 | +other way here — one map, two mappings, is exactly the controlled comparison). | ||
| 58 | + | ||
| 59 | +## 2. The mapping, from libaom's source | ||
| 60 | + | ||
| 61 | +`av1_get_q_for_deltaq_objective` (`av1/encoder/encodeframe_utils.c:893`) forms | ||
| 62 | +`rk = exp((intra_cost − mc_dep_cost) / w)` in the LOG domain per superblock, | ||
| 63 | +divides the frame-level `r0` (`encoder_utils.c:627`, the same expression at | ||
| 64 | +frame scope) by it, and asks `av1_get_deltaq_offset` (`rd.c`) for the qindex | ||
| 65 | +whose **DC** step is `dc_q(base) / sqrt(beta)`; the offset is clamped to | ||
| 66 | +`±(delta_q_res * 9 − 1)`. | ||
| 67 | + | ||
| 68 | +With per-cell costs `intra` and `intra + mc_dep`, `rk = 1/(1 + r)` for this | ||
| 69 | +crate's `r = mc_dep / intra`, so `beta = (1 + r_sb) / (1 + r_frame)` with | ||
| 70 | +`r_frame` read the log-mean way libaom reads `r0`. `encode::deltaq_libaom` | ||
| 71 | +is that; `encode::tpl_frame_ratio` is `r0`. `tpl_lambda_factors` now returns | ||
| 72 | +the raw per-cell ratio beside the lambda factors, because the factor alone has | ||
| 73 | +been through a mean normalisation the ratio cannot be recovered across. | ||
| 74 | + | ||
| 75 | +Two things differ from `deltaq_for_factor` and both are the point: the table | ||
| 76 | +inverted is `dc_q` not `ac_q`, and the ratio is read RAW (`sqrt(1+r)`) rather | ||
| 77 | +than through the log-compressed, `TPL_POWER`-flattened, mean-normalised lambda | ||
| 78 | +factor. | ||
| 79 | + | ||
| 80 | +Self-check: `encode::tests::the_objective_delta_q_mapping_leans_the_right_way_and_stops_at_libaom_s_clamp` | ||
| 81 | +— direction (leaned-on SB finer, idle coarser, average exactly at base), | ||
| 82 | +`res`-congruence of every offset, libaom's clamp reached from both ends, | ||
| 83 | +monotone in strength, and `tpl_frame_ratio` flat-in/flat-out. | ||
| 84 | + | ||
| 85 | +## 3. Sweep — 12-frame `bd_rate_screen_native` (BD vs libaom / rav1e) | ||
| 86 | + | ||
| 87 | +Control reproduces the charter to the digit on all three real rows. | ||
| 88 | + | ||
| 89 | +| arm | film A | film B | screen | | ||
| 90 | +|---|---|---|---| | ||
| 91 | +| control (no delta_q) | +21.0/−4.8 | +24.9/−1.9 | +14.5/−33.2 | | ||
| 92 | +| shipped mapping (`EC_AV1_DELTAQ=2`) | +20.6/−4.8 | +25.4/−1.1 | byte-identical | | ||
| 93 | +| **objective, k=0.25** | **+20.7/−4.8** | **+24.8/−1.7** | byte-identical | | ||
| 94 | +| objective, k=0.5 | +20.6/−4.5 | +25.8/−0.7 | byte-identical | | ||
| 95 | +| objective, k=1.0 | — | +25.9/−0.1 | — | | ||
| 96 | + | ||
| 97 | +At 12 frames the lever is flat at k=0.25 and LOSES at k≥0.5 (film B +0.9/+1.2 | ||
| 98 | +at k=0.5, +1.0/+1.8 at k=1.0 — monotone in strength, so k=1.5 was not run; | ||
| 99 | +`deferred` below). The screen capture is byte-identical at every arm: a frame | ||
| 100 | +that set `allow_screen_content_tools` codes no delta syntax at all | ||
| 101 | +(lane-dq3's content gate), so the keep rule's screen bound is met by | ||
| 102 | +construction, not by luck. | ||
| 103 | + | ||
| 104 | +## 4. Long GOP — 48 pictures, `bd_rate_film_long_gop` (the shape his exports have) | ||
| 105 | + | ||
| 106 | +| arm | film A | film B | | ||
| 107 | +|---|---|---| | ||
| 108 | +| control | +25.2/−7.2 | +86.7/+7.3 | | ||
| 109 | +| shipped mapping | +24.6/−7.3 | +86.1/+7.5 | | ||
| 110 | +| **objective, k=0.25** | **+24.4/−7.5** | **+86.0/+7.3** | | ||
| 111 | +| objective, k=0.5 | +23.8/−7.6 | +86.5/+7.9 | | ||
| 112 | +| objective, k=1.0 | +24.0/−7.0 | +88.4/+9.7 | | ||
| 113 | + | ||
| 114 | +Control reproduces the charter to the digit on both rows. **k=0.25 is the only | ||
| 115 | +arm down on both films on the libaom column with neither rav1e column worse** | ||
| 116 | +(film A −0.8/−0.3, film B −0.7/+0.0): it clears the keep rule's second clause | ||
| 117 | +(one row ≥0.5 down, the other flat inside ±0.3) with room, and the screen row | ||
| 118 | +is out of this gate but byte-identical anyway. k=0.5 buys film A another 0.6 | ||
| 119 | +and hands film B back 0.6 on the rav1e column; k=1.0 is worse than k=0.5 on | ||
| 120 | +three of the four columns. The lever is a LONG-GOP lever, as lane-cen3 | ||
| 121 | +predicted and as the class `short-GOP gate hid the gap` says to expect. | ||
| 122 | + | ||
| 123 | +## 5. Shipped | ||
| 124 | + | ||
| 125 | +* `speed::DELTAQ_RES[0] = 2` (was 4 = off) — preset 0 codes `delta_q_res = 4`. | ||
| 126 | +* `speed::DQ_TPL_K = [0.25, 0, …]` — the objective mapping at strength 0.25, | ||
| 127 | + preset 0 ONLY. Presets 5 and 6 keep `deltaq_for_factor` (`DQ_TPL_K = 0`) | ||
| 128 | + and are byte-identical to before this lane; nothing was measured for them | ||
| 129 | + and nothing changed. | ||
| 130 | +* `EC_AV1_DQ_TPL=0/1` forces the mapping either way, `EC_AV1_DQ_TPL_K` the | ||
| 131 | + strength, `EC_AV1_DQ_CENSUS=1` the instrument. | ||
| 132 | +* Byte pins re-taken with a four-line comment: `(150, 8325 → 8419)`, | ||
| 133 | + `(60, 33014 → 33029)`; green at default and at `EC_AV1_SPEED=6`. | ||
| 134 | + | ||
| 135 | +Spec syntax: unchanged from lane-deltaq — the frame's `delta_q_present` / | ||
| 136 | +`delta_q_res` go through `write_delta_q_params`, the per-SB `delta_qindex` | ||
| 137 | +through `tile::write_delta_q` at the spec's `skip → cdef → delta_q` order, | ||
| 138 | +`delta_lf_present` stays 0, and the encoder's own trial decode reads the frame | ||
| 139 | +back (a desync would refuse, class `refusal-from-own-desync`). | ||
| 140 | + | ||
| 141 | +## Deferred | ||
| 142 | + | ||
| 143 | +* `deferred: objective k=1.5 at 12 frames —` the 12-frame sweep is monotone | ||
| 144 | + worse in k above 0.25 (film B +0.9 at 0.5, +1.0 at 1.0) and long GOP peaks | ||
| 145 | + at 0.25/0.5, so 1.5 cannot win either gate — `unblocked by a reason to | ||
| 146 | + believe the curve turns back up`. | ||
| 147 | +* `deferred: the objective mapping at presets 5 and 6 —` they already ship | ||
| 148 | + `delta_q` through the OLD mapping and are untouched here; a strength for | ||
| 149 | + them is a second sweep on a different preset — `unblocked by a lane that | ||
| 150 | + charters presets 5/6`. | ||
| 151 | +* `deferred: per-level (ARF vs leaf) strength —` lane-cen3 says our top ARF is | ||
| 152 | + the overspent level; a single frame-type-blind strength cannot move quality | ||
| 153 | + toward the key, and this lane deliberately did not re-open the pyramid | ||
| 154 | + offsets — `unblocked by a lane that charters both together`. | ||