forge
Explore
Log in

Don't you dare go Hollow

The propagating tpl map ships on, formed per superblock and read through log2 beta

Browse files at this commit

853042ebc5a9bd3446258852432d5e94d8d543a2Ahmet Kaan GümüşParent: d30c5a4
  • feat
    • the propagating tpl map ships on, formed per superblock and read through log2 beta
Show full message
feat(av1): the propagating tpl map ships on, formed per superblock and read through log2 beta

The map's denominator is a 16x16 cell's MAD from its own DC, and on the
gate's synthetic film clips ~90% of all cells have a MAD of exactly zero,
so a per-cell dependence ratio is a large number over 1 wherever the
lookahead window leans on a flat area. Summing intra and mc_dep over the
64x64 superblock first (libaom's granularity) and reading the ratio
through log2(1+r) (libaom's compressed beta) is the only arm that is down
on two gate rows and flat on the third, so it becomes the default at
k=0.5, window 8, p=0.5. EC_AV1_TPL_PROP=0 restores the one-frame map;
EC_AV1_TPL_SB=0 / EC_AV1_TPL_LOG=0 take either half back out. Pins move.
crates/ec-av1/src/encode.rsmodified+67 -29
@@ -7471,16 +7471,18 @@ fn tpl_strength() -> f64 {
7471 /// lane-av1tpl2: how many SOURCE pictures the lambda map looks at, counting7471 /// lane-av1tpl2: how many SOURCE pictures the lambda map looks at, counting
7472 /// the frame being coded. 1 turns the weighting off; 2 is the shipped7472 /// the frame being coded. 1 turns the weighting off; 2 is the shipped
7473 /// one-frame area map ([`tpl_area_map`]); 3 and up only mean anything under7473 /// one-frame area map ([`tpl_area_map`]); 3 and up only mean anything under
7474-/// [`tpl_propagating`], which is the map that can actually use them. Swept7474+/// [`tpl_propagating`], which is the map that can actually use them and is
7475-/// by `EC_AV1_TPL_D=<d>`.7475+/// the default since lane-av1tpl3, so the default window is 8. Swept by
7476+/// `EC_AV1_TPL_D=<d>`.
7476 ///7477 ///
7477 /// The streaming facade holds `depth - 1` pictures back so it feeds7478 /// The streaming facade holds `depth - 1` pictures back so it feeds
7478 /// `encode_inter_frame` the same window `encode_sequence` does; both read7479 /// `encode_inter_frame` the same window `encode_sequence` does; both read
7479 /// THIS function, so the two paths cannot disagree about the window.7480 /// THIS function, so the two paths cannot disagree about the window.
7480-const TPL_DEPTH: usize = 2;7481+const TPL_DEPTH: usize = 8;
7481 7482
7482 /// The exponent the dependence ratio is read through ([`tpl_lambda_factors`]).7483 /// The exponent the dependence ratio is read through ([`tpl_lambda_factors`]).
7483-const TPL_POWER: f64 = 1.0;7484+/// 0.5 is libaom's own square-root compression of its dependence ratio.
7485+const TPL_POWER: f64 = 0.5;
7484 7486
7485 /// lane-av1tpl2: the propagating map ([`tpl_lambda_factors`]) instead of the7487 /// lane-av1tpl2: the propagating map ([`tpl_lambda_factors`]) instead of the
7486 /// one-frame area map ([`tpl_area_map`]), behind `EC_AV1_TPL_PROP=1`.7488 /// one-frame area map ([`tpl_area_map`]), behind `EC_AV1_TPL_PROP=1`.
@@ -7504,15 +7506,43 @@ const TPL_POWER: f64 = 1.0;
7504 /// | 0.5 | 4 | 0.5 | +15.6/-1.6 | +46.8/+19.0 | +51.0/-14.6 |7506 /// | 0.5 | 4 | 0.5 | +15.6/-1.6 | +46.8/+19.0 | +51.0/-14.6 |
7505 /// | 1 | 8 | 0.5 | +15.9/-1.5 | +47.0/+19.2 | +51.0/-14.5 |7507 /// | 1 | 8 | 0.5 | +15.9/-1.5 | +47.0/+19.2 | +51.0/-14.5 |
7506 ///7508 ///
7507-/// The map is genuinely non-flat now (0.25..1.12 across 7680 cells, only 587509+/// The map is genuinely non-flat (0.25..1.12 across 7680 cells, only 58 of
7508-/// of them within 0.9..1.1, where the one-frame map had 7127 of 7680 inside7510+/// them within 0.9..1.1, where the one-frame map had 7127 of 7680 inside
7509-/// 1..1.5x its own mean), and its best arm is 0.4 down on both film 1080p7511+/// 1..1.5x its own mean), and its best arm was 0.4 down on both film 1080p
7510-/// columns and 0.4/0.2 down on screen -- but EVERY arm is 0.1..0.4 UP on7512+/// columns and 0.4/0.2 down on screen -- but EVERY arm of that lane was
7511-/// film 2160p, including the vanishingly weak ones, so no arm is at least7513+/// 0.1..0.4 UP on film 2160p, so it shipped off.
7512-/// flat on every row. It therefore ships OFF: the default stays the7514+///
7513-/// one-frame map the gate's numbers were measured on.7515+/// lane-av1tpl3 found why and turned it on. The denominator is the cell's own
7516+/// MAD from its DC, and on the gate's film clips (ffmpeg `testsrc2`, colour
7517+/// bars) 89.4% (1080p) / 90.8% (2160p) of all 16x16 cells have a MAD of
7518+/// EXACTLY ZERO -- so a per-16x16 ratio is a huge number divided by 1
7519+/// wherever the window leans on a flat bar, which is exactly where coding is
7520+/// cheap. Summing intra and mc_dep over the 64x64 superblock before the ratio
7521+/// (libaom's own granularity) puts a busy cell's denominator next to the flat
7522+/// one's, and reading the ratio through `log2(1 + r)` (libaom's compressed
7523+/// beta) flattens what is left of the tail. Both on, at k=0.5 D=8 p=0.5, on
7524+/// the same native gate (baseline = the shipped one-frame map):
7525+///
7526+/// | arm | film 1080p | film 2160p | screen |
7527+/// |---|---|---|---|
7528+/// | one-frame map (was default) | +15.9/-1.3 | +46.5/+18.7 | +51.2/-14.4 |
7529+/// | propagating, per-16x16 | +15.5/-1.7 | +46.7/+19.0 | +50.8/-14.6 |
7530+/// | + denominator floor 0.25x mean | +15.7/-1.5 | +46.9/+19.1 | +50.9/-14.6 |
7531+/// | + floor 0.5x mean | +15.7/-1.5 | +46.8/+19.0 | +50.8/-14.5 |
7532+/// | + floor 1.0x mean | +15.8/-1.4 | +46.9/+19.1 | +51.0/-14.5 |
7533+/// | + 64x64 aggregation | +15.9/-1.3 | +46.6/+18.8 | +51.1/-14.6 |
7534+/// | + log2 beta | +16.0/-1.3 | +46.9/+19.0 | +51.0/-14.6 |
7535+/// | + aggregation + floor 0.25x | +15.7/-1.5 | +46.6/+18.9 | +50.9/-14.6 |
7536+/// | + aggregation + floor 0.5x | +16.0/-1.3 | +46.9/+19.1 | +50.8/-14.7 |
7537+/// | **+ aggregation + log2 beta** | **+15.6/-1.7** | **+46.5/+18.8** | **+51.0/-14.5** |
7538+///
7539+/// The last row is the only arm that is down on two rows (film 1080p 0.3/0.4,
7540+/// screen 0.2/0.1) and flat on the third (film 2160p 0.0/+0.1), so it ships
7541+/// as the default: `EC_AV1_TPL_PROP=0` goes back to the one-frame map. The
7542+/// floor is a real effect on the ratio and never a win once the aggregation
7543+/// is in -- the aggregation is the same fix, done where libaom does it.
7514 fn tpl_propagating() -> bool {7544 fn tpl_propagating() -> bool {
7515- std::env::var("EC_AV1_TPL_PROP").ok().as_deref() == Some("1")7545+ std::env::var("EC_AV1_TPL_PROP").ok().as_deref() != Some("0")
7516 }7546 }
7517 7547
7518 pub(crate) fn tpl_depth() -> usize {7548 pub(crate) fn tpl_depth() -> usize {
@@ -7752,24 +7782,27 @@ fn tpl_lambda_factors(frames: &[&[u8]], width: usize, height: usize, k: f64) ->
7752 .filter(|p| p.is_finite() && *p > 0.0)7782 .filter(|p| p.is_finite() && *p > 0.0)
7753 .unwrap_or(TPL_POWER);7783 .unwrap_or(TPL_POWER);
7754 // lane-av1tpl3, the three shape fixes, each its own knob so the gate can7784 // lane-av1tpl3, the three shape fixes, each its own knob so the gate can
7755- // read them apart:7785+ // read them apart. `SB` and `LOG` are the shipped shape (set the knob to
7786+ // `0` to take either back out); `FLOOR` defaults to off.
7756 //7787 //
7757 // * `EC_AV1_TPL_FLOOR=<f>` floors the denominator at `f` times the7788 // * `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 a7789+ // frame's own mean intra cost. A cell of flat content has a near-zero
7759- // near-zero MAD, so its ratio explodes exactly where coding is cheap.7790+ // MAD, so its ratio explodes exactly where coding is cheap.
7760- // * `EC_AV1_TPL_SB=1` sums intra and mc_dep over the 64x64 superblock7791+ // * `EC_AV1_TPL_SB=0` goes back to a per-16x16 ratio; by default intra
7761- // before the ratio is formed -- libaom's granularity (`mc_dep_cost` and7792+ // and mc_dep are summed over the 64x64 superblock first -- libaom's
7762- // `intra_cost` are SB sums there, not per-16x16 numbers).7793+ // granularity (`mc_dep_cost` and `intra_cost` are SB sums there, not
7763- // * `EC_AV1_TPL_LOG=1` reads the ratio through `log2(1 + r)` instead of7794+ // per-16x16 numbers), which is also what makes the ratio survive one
7764- // raw, libaom's log-compressed beta, which flattens the tail harder7795+ // flat cell next to a busy one.
7765- // than the `p` exponent alone can.7796+ // * `EC_AV1_TPL_LOG=0` reads the raw ratio; by default it goes through
7797+ // `log2(1 + r)`, libaom's log-compressed beta, which flattens the tail
7798+ // harder than the `p` exponent alone can.
7766 let floor_frac = std::env::var("EC_AV1_TPL_FLOOR")7799 let floor_frac = std::env::var("EC_AV1_TPL_FLOOR")
7767 .ok()7800 .ok()
7768 .and_then(|v| v.parse::<f64>().ok())7801 .and_then(|v| v.parse::<f64>().ok())
7769 .filter(|f| f.is_finite() && *f >= 0.0)7802 .filter(|f| f.is_finite() && *f >= 0.0)
7770 .unwrap_or(0.0);7803 .unwrap_or(0.0);
7771- let sb_agg = std::env::var("EC_AV1_TPL_SB").ok().as_deref() == Some("1");7804+ let sb_agg = std::env::var("EC_AV1_TPL_SB").ok().as_deref() != Some("0");
7772- let log_beta = std::env::var("EC_AV1_TPL_LOG").ok().as_deref() == Some("1");7805+ let log_beta = std::env::var("EC_AV1_TPL_LOG").ok().as_deref() != Some("0");
7773 let intra_mean =7806 let intra_mean =
7774 intra[0].iter().map(|&c| f64::from(c)).sum::<f64>() / cells.max(1) as f64;7807 intra[0].iter().map(|&c| f64::from(c)).sum::<f64>() / cells.max(1) as f64;
7775 let floor = floor_frac * intra_mean;7808 let floor = floor_frac * intra_mean;
@@ -8927,11 +8960,15 @@ mod tests {
8927 /// distortion-averse than the half nothing gains from.8960 /// distortion-averse than the half nothing gains from.
8928 #[test]8961 #[test]
8929 fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() {8962 fn tpl_factors_are_flat_on_a_uniform_repeat_and_fall_where_the_window_leans() {
8930- let (w, h) = (64usize, 64usize);8963+ // Four 64x64 superblocks across: the shipped map forms its ratio per
8964+ // SUPERBLOCK (`EC_AV1_TPL_SB`), so a frame one superblock wide could
8965+ // only ever produce one ratio -- and one ratio, mean-normalised, is
8966+ // the flat map, whatever the content underneath it.
8967+ let (w, h) = (256usize, 64usize);
8931 let uniform: Vec<u8> = (0..w * h).map(|i| ((i * 37) % 251) as u8).collect();8968 let uniform: Vec<u8> = (0..w * h).map(|i| ((i * 37) % 251) as u8).collect();
8932 let frames: Vec<&[u8]> = vec![&uniform, &uniform, &uniform];8969 let frames: Vec<&[u8]> = vec![&uniform, &uniform, &uniform];
8933 let f = super::tpl_lambda_factors(&frames, w, h, 1.0);8970 let f = super::tpl_lambda_factors(&frames, w, h, 1.0);
8934- assert_eq!(f.len(), 16, "4x4 cells of 16x16");8971+ assert_eq!(f.len(), 64, "16x4 cells of 16x16");
8935 for (i, &v) in f.iter().enumerate() {8972 for (i, &v) in f.iter().enumerate() {
8936 assert!((v - 1.0).abs() < 1e-6, "cell {i} factor {v}, a flat map must move no lambda");8973 assert!((v - 1.0).abs() < 1e-6, "cell {i} factor {v}, a flat map must move no lambda");
8937 }8974 }
@@ -8939,13 +8976,14 @@ mod tests {
8939 // Left half textured, right half constant: the window saves bits8976 // Left half textured, right half constant: the window saves bits
8940 // predicting the left half and nothing at all on the right.8977 // predicting the left half and nothing at all on the right.
8941 let split: Vec<u8> = (0..w * h)8978 let split: Vec<u8> = (0..w * h)
8942- .map(|i| if i % w < 32 { ((i * 37) % 251) as u8 } else { 128 })8979+ .map(|i| if i % w < w / 2 { ((i * 37) % 251) as u8 } else { 128 })
8943 .collect();8980 .collect();
8944 let frames: Vec<&[u8]> = vec![&split, &split, &split];8981 let frames: Vec<&[u8]> = vec![&split, &split, &split];
8982+ let cells_x = w / 16;
8945 for k in [0.25f64, 0.5, 1.0, 2.0] {8983 for k in [0.25f64, 0.5, 1.0, 2.0] {
8946 let f = super::tpl_lambda_factors(&frames, w, h, k);8984 let f = super::tpl_lambda_factors(&frames, w, h, k);
8947- for r in 0..4 {8985+ for r in 0..h / 16 {
8948- let (lean, idle) = (f[r * 4], f[r * 4 + 3]);8986+ let (lean, idle) = (f[r * cells_x], f[r * cells_x + cells_x - 1]);
8949 assert!(lean < 1.0, "k={k} row {r}: leaned-on cell factor {lean} must be < 1");8987 assert!(lean < 1.0, "k={k} row {r}: leaned-on cell factor {lean} must be < 1");
8950 assert!(idle > 1.0, "k={k} row {r}: idle cell factor {idle} must be > 1");8988 assert!(idle > 1.0, "k={k} row {r}: idle cell factor {idle} must be > 1");
8951 }8989 }
@@ -12307,7 +12345,7 @@ mod tests {
12307 })12345 })
12308 };12346 };
12309 let pins: [(u8, usize, u64); 2] =12347 let pins: [(u8, usize, u64); 2] =
12310- [(150, 7128, 0x8ee1_5c87_bdb4_a8c5), (60, 26353, 0x50fc_c10f_9eab_b638)];12348+ [(150, 7173, 0x3d5a_1440_7773_1816), (60, 26362, 0x2619_8199_39ad_828c)];
12311 for (q, bytes, hash) in pins {12349 for (q, bytes, hash) in pins {
12312 let encoded = encode_sequence(&source, q, 0.5).unwrap();12350 let encoded = encode_sequence(&source, q, 0.5).unwrap();
12313 assert_eq!(12351 assert_eq!(