Three shape knobs for the propagating tpl map plus its denominator histogram
Browse files at this commitd30c5a4b9869f24285fdb5a522b27bfc160a7008Parent: 7169554- feat
- three shape knobs for the propagating tpl map plus its denominator histogram
1 changed file +117 -3
- crates/ec-av1/src/encode.rs +117 -3
crates/ec-av1/src/encode.rsmodified+117 -3
| @@ -7751,9 +7751,54 @@ fn tpl_lambda_factors(frames: &[&[u8]], width: usize, height: usize, k: f64) -> | ||
| 7751 | 7751 | .and_then(|v| v.parse::<f64>().ok()) |
| 7752 | 7752 | .filter(|p| p.is_finite() && *p > 0.0) |
| 7753 | 7753 | .unwrap_or(TPL_POWER); |
| 7754 | - let mut out: Vec<f64> = (0..cells) | |
| 7755 | - .map(|c| { | |
| 7756 | - let r = mc_dep[0][c] / f64::from(intra[0][c].max(1)); | |
| 7754 | + // lane-av1tpl3, the three shape fixes, each its own knob so the gate can | |
| 7755 | + // read them apart: | |
| 7756 | + // | |
| 7757 | + // * `EC_AV1_TPL_FLOOR=<f>` floors the denominator at `f` times the | |
| 7758 | + // frame's own mean intra cost. A flat, heavily filtered cell has a | |
| 7759 | + // near-zero MAD, so its ratio explodes exactly where coding is cheap. | |
| 7760 | + // * `EC_AV1_TPL_SB=1` sums intra and mc_dep over the 64x64 superblock | |
| 7761 | + // before the ratio is formed -- libaom's granularity (`mc_dep_cost` and | |
| 7762 | + // `intra_cost` are SB sums there, not per-16x16 numbers). | |
| 7763 | + // * `EC_AV1_TPL_LOG=1` reads the ratio through `log2(1 + r)` instead of | |
| 7764 | + // raw, libaom's log-compressed beta, which flattens the tail harder | |
| 7765 | + // than the `p` exponent alone can. | |
| 7766 | + let floor_frac = std::env::var("EC_AV1_TPL_FLOOR") | |
| 7767 | + .ok() | |
| 7768 | + .and_then(|v| v.parse::<f64>().ok()) | |
| 7769 | + .filter(|f| f.is_finite() && *f >= 0.0) | |
| 7770 | + .unwrap_or(0.0); | |
| 7771 | + let sb_agg = std::env::var("EC_AV1_TPL_SB").ok().as_deref() == Some("1"); | |
| 7772 | + let log_beta = std::env::var("EC_AV1_TPL_LOG").ok().as_deref() == Some("1"); | |
| 7773 | + let intra_mean = | |
| 7774 | + intra[0].iter().map(|&c| f64::from(c)).sum::<f64>() / cells.max(1) as f64; | |
| 7775 | + let floor = floor_frac * intra_mean; | |
| 7776 | + let denom = |c: usize| f64::from(intra[0][c].max(1)).max(floor).max(1.0); | |
| 7777 | + // Per-cell ratios, or one ratio per 64x64 superblock (4x4 cells) shared by | |
| 7778 | + // every cell under it. The edge groups are partial and summed as they are. | |
| 7779 | + let ratio: Vec<f64> = if sb_agg { | |
| 7780 | + let (sb_cols, sb_rows) = (cells_x.div_ceil(4), cells_y.div_ceil(4)); | |
| 7781 | + let mut sb = vec![(0.0f64, 0.0f64); sb_cols * sb_rows]; | |
| 7782 | + for cy in 0..cells_y { | |
| 7783 | + for cx in 0..cells_x { | |
| 7784 | + let e = &mut sb[(cy / 4) * sb_cols + cx / 4]; | |
| 7785 | + e.0 += mc_dep[0][cy * cells_x + cx]; | |
| 7786 | + e.1 += denom(cy * cells_x + cx); | |
| 7787 | + } | |
| 7788 | + } | |
| 7789 | + (0..cells) | |
| 7790 | + .map(|c| { | |
| 7791 | + let (dep, den) = sb[(c / cells_x / 4) * sb_cols + (c % cells_x) / 4]; | |
| 7792 | + dep / den.max(1.0) | |
| 7793 | + }) | |
| 7794 | + .collect() | |
| 7795 | + } else { | |
| 7796 | + (0..cells).map(|c| mc_dep[0][c] / denom(c)).collect() | |
| 7797 | + }; | |
| 7798 | + let mut out: Vec<f64> = ratio | |
| 7799 | + .iter() | |
| 7800 | + .map(|&r| { | |
| 7801 | + let r = if log_beta { (1.0 + r).log2() } else { r }; | |
| 7757 | 7802 | (1.0 + k * r).powf(-power) |
| 7758 | 7803 | }) |
| 7759 | 7804 | .collect(); |
| @@ -13015,4 +13060,73 @@ mod tests { | ||
| 13015 | 13060 | ); |
| 13016 | 13061 | } |
| 13017 | 13062 | } |
| 13063 | + | |
| 13064 | + /// lane-av1tpl3 step 1: the SHAPE of the propagating map's denominator | |
| 13065 | + /// ([`super::tpl_intra_costs`], the per-16x16 MAD from the cell's own DC) | |
| 13066 | + /// on the two film clips at their native gate crop. The 2160p clip loses | |
| 13067 | + /// at every arm of the propagating map and the named suspect was that its | |
| 13068 | + /// heavily filtered flat cells give a tiny denominator, so the dependence | |
| 13069 | + /// ratio explodes exactly where coding is cheap; this prints the numbers | |
| 13070 | + /// that confirm or refute it. | |
| 13071 | + #[test] | |
| 13072 | + #[ignore = "needs ffmpeg and the film fixtures"] | |
| 13073 | + fn tpl_intra_denominator_histogram() { | |
| 13074 | + if !have_ffmpeg() { | |
| 13075 | + eprintln!("SKIP tpl_intra_denominator_histogram: no ffmpeg"); | |
| 13076 | + return; | |
| 13077 | + } | |
| 13078 | + let fixtures = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures"); | |
| 13079 | + for (label, file) in [ | |
| 13080 | + ("film 1080p", "h264-1080p-23.976-8bit.mp4"), | |
| 13081 | + ("film 2160p", "h264-2160p-23.976-8bit.mp4"), | |
| 13082 | + ] { | |
| 13083 | + let path = fixtures.join("video").join(file); | |
| 13084 | + if !path.exists() { | |
| 13085 | + eprintln!("SKIP {label}: {file} missing"); | |
| 13086 | + continue; | |
| 13087 | + } | |
| 13088 | + let path = path.to_str().unwrap(); | |
| 13089 | + let Some((nw, nh)) = probe_dims(path) else { | |
| 13090 | + eprintln!("SKIP {label}: ffprobe gave no size"); | |
| 13091 | + continue; | |
| 13092 | + }; | |
| 13093 | + let (cw, ch) = (nw.min(1920) / 128 * 128, nh.min(1024) / 128 * 128); | |
| 13094 | + let (x, y) = ((nw - cw) / 2 & !1, (nh - ch) / 2 & !1); | |
| 13095 | + let source = | |
| 13096 | + clip_frames_vf(path, "0", &format!("crop={cw}:{ch}:{x}:{y}"), cw, ch, 8); | |
| 13097 | + let mut costs: Vec<f64> = Vec::new(); | |
| 13098 | + for pic in &source { | |
| 13099 | + let y8: Vec<u8> = pic.y.iter().map(|&v| v as u8).collect(); | |
| 13100 | + costs.extend( | |
| 13101 | + super::tpl_intra_costs(&y8, cw, ch).iter().map(|&c| f64::from(c)), | |
| 13102 | + ); | |
| 13103 | + } | |
| 13104 | + let mean = costs.iter().sum::<f64>() / costs.len() as f64; | |
| 13105 | + let mut sorted = costs.clone(); | |
| 13106 | + sorted.sort_by(f64::total_cmp); | |
| 13107 | + let q = |f: f64| sorted[((sorted.len() - 1) as f64 * f) as usize] / mean; | |
| 13108 | + let below = |f: f64| costs.iter().filter(|&&c| c < f * mean).count(); | |
| 13109 | + eprintln!( | |
| 13110 | + "{label} {cw}x{ch}: {} cells over 8 frames, mean intra MAD {mean:.0}; \ | |
| 13111 | + as a fraction of the mean p0={:.3} p1={:.3} p5={:.3} p25={:.3} p50={:.3} \ | |
| 13112 | + p75={:.3} p99={:.3} max={:.3}; below 0.25x {} ({:.1}%), below 0.5x {} ({:.1}%), \ | |
| 13113 | + below 1.0x {} ({:.1}%)", | |
| 13114 | + costs.len(), | |
| 13115 | + q(0.0), | |
| 13116 | + q(0.01), | |
| 13117 | + q(0.05), | |
| 13118 | + q(0.25), | |
| 13119 | + q(0.50), | |
| 13120 | + q(0.75), | |
| 13121 | + q(0.99), | |
| 13122 | + q(1.0), | |
| 13123 | + below(0.25), | |
| 13124 | + 100.0 * below(0.25) as f64 / costs.len() as f64, | |
| 13125 | + below(0.5), | |
| 13126 | + 100.0 * below(0.5) as f64 / costs.len() as f64, | |
| 13127 | + below(1.0), | |
| 13128 | + 100.0 * below(1.0) as f64 / costs.len() as f64, | |
| 13129 | + ); | |
| 13130 | + } | |
| 13131 | + } | |
| 13018 | 13132 | } |