forge
Explore
Log in

Don't you dare go Hollow

Rebuild the 32x32 inter tx-depth search and the compound var-tx trial set behind knobs

Browse files at this commit

7d0a80d5f5cb82632222fa05af59f44530242eb1Ahmet Kaan GümüşParent: 4cfdfb5
  • feat
    • rebuild the 32x32 inter tx-depth search and the compound var-tx trial set behind knobs
Show full message
feat(av1): rebuild the 32x32 inter tx-depth search and the compound var-tx trial set behind knobs

Both were measured and removed at LAMBDA_SCALE 0.1; the lambda lane halved
the rate weight, so they need rebuilding to be re-judged. commit_inter_luma
now takes an optional second reference (mc_trial_compound predicts each
half-side unit from both), and search_inter_block routes its 32x32 winner
through it instead of coding tx_depth 0 blind. EC_AV1_TX32_DEPTH and
EC_AV1_COMP_VARTX, both off; the var-tx census is keyed by block class.
crates/ec-av1/src/encode.rsmodified+135 -36
@@ -2835,9 +2835,28 @@ pub(crate) fn take_tx_depth_hits() -> [usize; 3] {
28352835
28362836 /// How many inter blocks resolved to each var-tx depth (0 = one transform
28372837 /// over the whole block, 1 = its four halves), the fire count a gate prints
2838-/// to see the split search reach its candidates at all.
2839-static INTER_TX_SPLIT_HITS: [std::sync::atomic::AtomicUsize; 2] =
2840- [const { std::sync::atomic::AtomicUsize::new(0) }; 2];
2838+/// to see the split search reach its candidates at all. Indexed
2839+/// `(block is 32x32) * 4 + (block is compound) * 2 + depth`, so one census
2840+/// says which of the four block classes the split reaches -- the 32x32 root
2841+/// and the compound blocks are this lane's two new ones.
2842+static INTER_TX_SPLIT_HITS: [std::sync::atomic::AtomicUsize; 8] =
2843+ [const { std::sync::atomic::AtomicUsize::new(0) }; 8];
2844+
2845+/// Whether a 32x32 inter winner searches its own var-tx depth (the four
2846+/// 16x16 units against the flat transform) instead of coding depth 0 blind.
2847+/// `EC_AV1_TX32_DEPTH`.
2848+fn tx32_depth_search() -> bool {
2849+ crate::envflags::env_flag!("EC_AV1_TX32_DEPTH")
2850+}
2851+
2852+/// Whether a COMPOUND inter winner is offered the var-tx split at all: its
2853+/// trial units have to be predicted from BOTH references
2854+/// ([`mc_trial_compound`]) or the split would re-predict them by single-
2855+/// reference translation, which is not what the decoder reconstructs.
2856+/// `EC_AV1_COMP_VARTX`.
2857+fn compound_var_tx() -> bool {
2858+ crate::envflags::env_flag!("EC_AV1_COMP_VARTX")
2859+}
28412860
28422861 /// How many 32x32-and-below inter blocks each reference frame won, indexed by
28432862 /// `ref_frame` (`crate::mvstack::LAST_FRAME` = 1 .. `ALTREF_FRAME` = 7), the
@@ -2855,7 +2874,7 @@ pub(crate) fn take_ref_frame_hits() -> [usize; 8] {
28552874
28562875 /// Reads [`INTER_TX_SPLIT_HITS`] and zeroes it.
28572876 #[cfg(test)]
2858-pub(crate) fn take_inter_tx_split_hits() -> [usize; 2] {
2877+pub(crate) fn take_inter_tx_split_hits() -> [usize; 8] {
28592878 std::array::from_fn(|d| INTER_TX_SPLIT_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed))
28602879 }
28612880
@@ -2905,12 +2924,19 @@ fn commit_inter_luma(
29052924 reference: (&[u16], usize, usize, usize),
29062925 search: &Search,
29072926 flat: &Trial,
2908- skip: bool, fctx: &crate::decode::FrameCtx,
2927+ skip: bool,
2928+ // A COMPOUND winner's second half: the vector and reference planes the
2929+ // trial units are blended from ([`mc_trial_compound`], the decoder's own
2930+ // `comp_group_idx == 0` average). `None` is the single-reference block.
2931+ second: Option<((i32, i32), (&[u16], usize, usize, usize))>,
2932+ fctx: &crate::decode::FrameCtx,
29092933 ) -> (Vec<i32>, u8, f64) {
29102934 let eligible = tx_select()
29112935 && tx_select_inter()
29122936 && !skip
29132937 && side >= 16
2938+ && (second.is_none() || compound_var_tx())
2939+ && (side < BLOCK || tx32_depth_search())
29142940 && x + side <= luma.true_width
29152941 && y + side <= luma.true_height;
29162942 if !eligible {
@@ -2929,10 +2955,16 @@ fn commit_inter_luma(
29292955 for tu_row in 0..2 {
29302956 for tu_col in 0..2 {
29312957 let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx);
2932- let trial = mc_trial(
2933- luma, tu_x, tu_y, tx, mv, true, reference.0, reference.1, reference.2,
2934- reference.3, false, search.base_q_idx, search.deadzone, set, fctx,
2935- );
2958+ let trial = match second {
2959+ Some((mv1, ref1)) => mc_trial_compound(
2960+ luma, tu_x, tu_y, tx, (mv, mv1), true, reference, ref1,
2961+ search.base_q_idx, search.deadzone, set, fctx,
2962+ ),
2963+ None => mc_trial(
2964+ luma, tu_x, tu_y, tx, mv, true, reference.0, reference.1, reference.2,
2965+ reference.3, false, search.base_q_idx, search.deadzone, set, fctx,
2966+ ),
2967+ };
29362968 sse += trial.sse;
29372969 bits += trial.bits;
29382970 for row in 0..tx {
@@ -2945,14 +2977,15 @@ fn commit_inter_luma(
29452977 luma.ctx.tu_split = false;
29462978 let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1));
29472979 let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0));
2980+ let class = usize::from(side == BLOCK) * 4 + usize::from(second.is_some()) * 2;
29482981 if split_cost < flat_cost {
29492982 for (tu_x, tu_y, trial) in &units {
29502983 luma.commit(*tu_x, *tu_y, tx, trial);
29512984 }
2952- INTER_TX_SPLIT_HITS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2985+ INTER_TX_SPLIT_HITS[class + 1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
29532986 return (levels, 1, split_cost - flat_cost);
29542987 }
2955- INTER_TX_SPLIT_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2988+ INTER_TX_SPLIT_HITS[class].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
29562989 luma.commit(x, y, side, flat);
29572990 (flat.levels.clone(), 0, 0.0)
29582991 }
@@ -4600,14 +4633,30 @@ fn code_square_inter(
46004633 // An OBMC winner commits the flat trial too: `commit_inter_luma`
46014634 // re-predicts each var-tx unit by plain translation, which is not
46024635 // the prediction this candidate was priced from.
4603- let (levels, tx_depth, dcost) = if info.ref1.is_some() || motion_won != 0 {
4636+ // lane-av1txdepth REBUILT the compound trial set at LAMBDA_SCALE
4637+ // 0.05 (both rejections above were measured at 0.1, the weight
4638+ // the lambda lane showed was 2x too heavy): a compound winner is
4639+ // offered the same var-tx split, its units predicted from BOTH
4640+ // references. Behind `EC_AV1_COMP_VARTX` until the native table
4641+ // says keep.
4642+ let second = info.ref1.filter(|_| motion_won == 0).and_then(|r| {
4643+ refs[r as usize].map(|g| {
4644+ (
4645+ info.mv1,
4646+ (g.y.as_slice(), g.width, luma.true_width, luma.true_height),
4647+ )
4648+ })
4649+ });
4650+ let (levels, tx_depth, dcost) = if motion_won != 0
4651+ || (info.ref1.is_some() && second.is_none())
4652+ {
46044653 luma.commit(x, y, side, &luma_new);
46054654 (luma_new.levels.clone(), 0, 0.0)
46064655 } else {
46074656 commit_inter_luma(
46084657 luma, (x, y), side, info.mv,
46094658 (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3),
4610- search, &luma_new, skip_new, fctx,
4659+ search, &luma_new, skip_new, second, fctx,
46114660 )
46124661 };
46134662 chroma[0].commit(x / 2, y / 2, side / 2, &u_new);
@@ -4642,7 +4691,7 @@ fn code_square_inter(
46424691 }
46434692 if inter_cost < intra_cost {
46444693 let (levels, tx_depth, dcost) = commit_inter_luma(
4645- luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, fctx,
4694+ luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, None, fctx,
46464695 );
46474696 chroma[0].commit(x / 2, y / 2, side / 2, &u);
46484697 chroma[1].commit(x / 2, y / 2, side / 2, &v);
@@ -7165,12 +7214,46 @@ fn search_inter_block(
71657214 // the film's inter blocks and 17.6% of the screen's. The same shape as
71667215 // the compound trial set below: a local RD win that does not convert into
71677216 // ladder bytes. The flat 32x32 transform stays.
7168- luma.commit(x, y, BLOCK, &best.luma);
7217+ // lane-av1txdepth REBUILT that search at LAMBDA_SCALE 0.05 (the rejection
7218+ // above was measured at 0.1), behind `EC_AV1_TX32_DEPTH`, and gave the
7219+ // compound winner the same offer behind `EC_AV1_COMP_VARTX`. An OBMC/warp
7220+ // winner still commits flat: `commit_inter_luma` re-predicts each unit by
7221+ // plain translation, which is not the prediction it was priced from.
7222+ let (levels, tx_depth, dcost) = match best.inter.filter(|_| motion_won == 0) {
7223+ Some(info) => {
7224+ let own = match extra.iter().find(|(r, _, _)| *r == info.ref_frame) {
7225+ Some(&(_, g, _)) => (g.y.as_slice(), g.width, luma.true_width, luma.true_height),
7226+ None => (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3),
7227+ };
7228+ let second = info.ref1.and_then(|r| {
7229+ compound.iter().find(|(c, _, _)| *c == r).map(|&(_, g, _)| {
7230+ (
7231+ info.mv1,
7232+ (g.y.as_slice(), g.width, luma.true_width, luma.true_height),
7233+ )
7234+ })
7235+ });
7236+ match info.ref1.is_some() && second.is_none() {
7237+ true => {
7238+ luma.commit(x, y, BLOCK, &best.luma);
7239+ (best.luma.levels.clone(), 0, 0.0)
7240+ }
7241+ false => commit_inter_luma(
7242+ luma, (x, y), BLOCK, info.mv, own, search, &best.luma, best.skip, second,
7243+ fctx,
7244+ ),
7245+ }
7246+ }
7247+ None => {
7248+ luma.commit(x, y, BLOCK, &best.luma);
7249+ (best.luma.levels.clone(), 0, 0.0)
7250+ }
7251+ };
71697252 chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u);
71707253 chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v);
71717254 (
71727255 BlockCoeffs {
7173- luma: coeffs(&best.luma.levels, BLOCK),
7256+ luma: coeffs(&levels, BLOCK),
71747257 u: coeffs(&best.u.levels, BLOCK / 2),
71757258 v: coeffs(&best.v.levels, BLOCK / 2),
71767259 mode: best.mode,
@@ -7182,9 +7265,9 @@ fn search_inter_block(
71827265 dv: None,
71837266 palette: None,
71847267 palette_uv: None,
7185- tx_depth: 0,
7268+ tx_depth,
71867269 },
7187- best.cost,
7270+ best.cost + dcost,
71887271 )
71897272 }
71907273
@@ -10359,7 +10442,13 @@ mod tests {
1035910442 (0..5).map(|i| panned_test_card(width, height, i * 3)).collect();
1036010443 let _ = take_inter_tx_split_hits();
1036110444 let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap();
10362- let split = take_inter_tx_split_hits();
10445+ let raw = take_inter_tx_split_hits();
10446+ // Summed over the four block classes the census keys on (leaf/32x32 x
10447+ // single/compound): this assert is about the split firing at all.
10448+ let split = [
10449+ raw.iter().step_by(2).sum::<usize>(),
10450+ raw.iter().skip(1).step_by(2).sum::<usize>(),
10451+ ];
1036310452 eprintln!("inter var-tx: {} blocks flat, {} split", split[0], split[1]);
1036410453 // [[gate-blind-to-feature]]: with inter `TxMode::Select` on, this
1036510454 // content splits -- a green round trip in which the split never fires
@@ -11991,23 +12080,7 @@ mod tests {
1199112080 .collect::<Vec<_>>()
1199212081 .join(" ")
1199312082 );
11994- // And which var-tx depth this clip's INTER blocks resolved to
11995- // (lane-av1tx2): 0 is one transform over the whole block, 1 its
11996- // four halves.
11997- let splits = take_inter_tx_split_hits();
11998- let split_total: usize = splits.iter().sum();
11999- eprintln!(
12000- "{name}: inter var-tx depths {}",
12001- splits
12002- .iter()
12003- .enumerate()
12004- .map(|(d, &n)| format!(
12005- "{d}={n} ({:.1}%)",
12006- 100.0 * n as f64 / split_total.max(1) as f64
12007- ))
12008- .collect::<Vec<_>>()
12009- .join(" ")
12010- );
12083+ print_inter_tx_census(name);
1201112084 // lane-av1obmc: how many blocks actually CODED a motion_mode
1201212085 // symbol and which value they took, by footprint -- "OBMC is on"
1201312086 // as a measurement rather than a claim (class
@@ -12053,6 +12126,31 @@ mod tests {
1205312126 );
1205412127 }
1205512128 }
12129+ /// Which var-tx depth this clip's INTER blocks resolved to (lane-av1tx2,
12130+ /// split by block class in lane-av1txdepth): depth 0 is one transform over
12131+ /// the whole block, 1 its four halves; the classes are the 16x16/8x8
12132+ /// leaves and the 32x32 root, each single-reference or compound. A class
12133+ /// with no counts at all never reached `commit_inter_luma` -- which is
12134+ /// what its knob being off looks like (class `gate-blind-to-feature`).
12135+ fn print_inter_tx_census(name: &str) {
12136+ let splits = take_inter_tx_split_hits();
12137+ let total: usize = splits.iter().sum();
12138+ let row = |label: &str, base: usize| {
12139+ let (n0, n1) = (splits[base], splits[base + 1]);
12140+ format!(
12141+ "{label} 0={n0} 1={n1} ({:.1}% split)",
12142+ 100.0 * n1 as f64 / (n0 + n1).max(1) as f64
12143+ )
12144+ };
12145+ eprintln!(
12146+ "{name}: inter var-tx depths of {total} blocks -- {} | {} | {} | {}",
12147+ row("leaf single", 0),
12148+ row("leaf compound", 2),
12149+ row("32x32 single", 4),
12150+ row("32x32 compound", 6),
12151+ );
12152+ }
12153+
1205612154 /// The video stream's coded size, from ffprobe.
1205712155 fn probe_dims(clip: &str) -> Option<(usize, usize)> {
1205812156 let out = Command::new("ffprobe")
@@ -12244,6 +12342,7 @@ mod tests {
1224412342 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
1224512343 mm.iter().sum::<usize>(),
1224612344 );
12345+ print_inter_tx_census(name);
1224712346 let palette = crate::tile::take_palette_hits();
1224812347 let palette_uv = crate::tile::take_palette_uv_hits();
1224912348 let ibc = crate::tile::take_intrabc_hits();