Rebuild the 32x32 inter tx-depth search and the compound var-tx trial set behind knobs
Browse files at this commit7d0a80d5f5cb82632222fa05af59f44530242eb1Parent: 4cfdfb5- feat
- rebuild the 32x32 inter tx-depth search and the compound var-tx trial set behind knobs
1 changed file +135 -36
- crates/ec-av1/src/encode.rs +135 -36
crates/ec-av1/src/encode.rsmodified+135 -36
| @@ -2835,9 +2835,28 @@ pub(crate) fn take_tx_depth_hits() -> [usize; 3] { | |||
| 2835 | 2835 | ||
| 2836 | /// How many inter blocks resolved to each var-tx depth (0 = one transform | 2836 | /// How many inter blocks resolved to each var-tx depth (0 = one transform |
| 2837 | /// over the whole block, 1 = its four halves), the fire count a gate prints | 2837 | /// 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. | 2838 | +/// to see the split search reach its candidates at all. Indexed |
| 2839 | -static INTER_TX_SPLIT_HITS: [std::sync::atomic::AtomicUsize; 2] = | 2839 | +/// `(block is 32x32) * 4 + (block is compound) * 2 + depth`, so one census |
| 2840 | - [const { std::sync::atomic::AtomicUsize::new(0) }; 2]; | 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 | +} | ||
| 2841 | 2860 | ||
| 2842 | /// How many 32x32-and-below inter blocks each reference frame won, indexed by | 2861 | /// How many 32x32-and-below inter blocks each reference frame won, indexed by |
| 2843 | /// `ref_frame` (`crate::mvstack::LAST_FRAME` = 1 .. `ALTREF_FRAME` = 7), the | 2862 | /// `ref_frame` (`crate::mvstack::LAST_FRAME` = 1 .. `ALTREF_FRAME` = 7), the |
| @@ -2855,7 +2874,7 @@ pub(crate) fn take_ref_frame_hits() -> [usize; 8] { | |||
| 2855 | 2874 | ||
| 2856 | /// Reads [`INTER_TX_SPLIT_HITS`] and zeroes it. | 2875 | /// Reads [`INTER_TX_SPLIT_HITS`] and zeroes it. |
| 2857 | #[cfg(test)] | 2876 | #[cfg(test)] |
| 2858 | -pub(crate) fn take_inter_tx_split_hits() -> [usize; 2] { | 2877 | +pub(crate) fn take_inter_tx_split_hits() -> [usize; 8] { |
| 2859 | std::array::from_fn(|d| INTER_TX_SPLIT_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed)) | 2878 | std::array::from_fn(|d| INTER_TX_SPLIT_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed)) |
| 2860 | } | 2879 | } |
| 2861 | 2880 | ||
| @@ -2905,12 +2924,19 @@ fn commit_inter_luma( | |||
| 2905 | reference: (&[u16], usize, usize, usize), | 2924 | reference: (&[u16], usize, usize, usize), |
| 2906 | search: &Search, | 2925 | search: &Search, |
| 2907 | flat: &Trial, | 2926 | 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, | ||
| 2909 | ) -> (Vec<i32>, u8, f64) { | 2933 | ) -> (Vec<i32>, u8, f64) { |
| 2910 | let eligible = tx_select() | 2934 | let eligible = tx_select() |
| 2911 | && tx_select_inter() | 2935 | && tx_select_inter() |
| 2912 | && !skip | 2936 | && !skip |
| 2913 | && side >= 16 | 2937 | && side >= 16 |
| 2938 | + && (second.is_none() || compound_var_tx()) | ||
| 2939 | + && (side < BLOCK || tx32_depth_search()) | ||
| 2914 | && x + side <= luma.true_width | 2940 | && x + side <= luma.true_width |
| 2915 | && y + side <= luma.true_height; | 2941 | && y + side <= luma.true_height; |
| 2916 | if !eligible { | 2942 | if !eligible { |
| @@ -2929,10 +2955,16 @@ fn commit_inter_luma( | |||
| 2929 | for tu_row in 0..2 { | 2955 | for tu_row in 0..2 { |
| 2930 | for tu_col in 0..2 { | 2956 | for tu_col in 0..2 { |
| 2931 | let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx); | 2957 | let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx); |
| 2932 | - let trial = mc_trial( | 2958 | + let trial = match second { |
| 2933 | - luma, tu_x, tu_y, tx, mv, true, reference.0, reference.1, reference.2, | 2959 | + Some((mv1, ref1)) => mc_trial_compound( |
| 2934 | - reference.3, false, search.base_q_idx, search.deadzone, set, fctx, | 2960 | + luma, tu_x, tu_y, tx, (mv, mv1), true, reference, ref1, |
| 2935 | - ); | 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 | + }; | ||
| 2936 | sse += trial.sse; | 2968 | sse += trial.sse; |
| 2937 | bits += trial.bits; | 2969 | bits += trial.bits; |
| 2938 | for row in 0..tx { | 2970 | for row in 0..tx { |
| @@ -2945,14 +2977,15 @@ fn commit_inter_luma( | |||
| 2945 | luma.ctx.tu_split = false; | 2977 | luma.ctx.tu_split = false; |
| 2946 | let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1)); | 2978 | let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1)); |
| 2947 | let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0)); | 2979 | 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; | ||
| 2948 | if split_cost < flat_cost { | 2981 | if split_cost < flat_cost { |
| 2949 | for (tu_x, tu_y, trial) in &units { | 2982 | for (tu_x, tu_y, trial) in &units { |
| 2950 | luma.commit(*tu_x, *tu_y, tx, trial); | 2983 | luma.commit(*tu_x, *tu_y, tx, trial); |
| 2951 | } | 2984 | } |
| 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); |
| 2953 | return (levels, 1, split_cost - flat_cost); | 2986 | return (levels, 1, split_cost - flat_cost); |
| 2954 | } | 2987 | } |
| 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); |
| 2956 | luma.commit(x, y, side, flat); | 2989 | luma.commit(x, y, side, flat); |
| 2957 | (flat.levels.clone(), 0, 0.0) | 2990 | (flat.levels.clone(), 0, 0.0) |
| 2958 | } | 2991 | } |
| @@ -4600,14 +4633,30 @@ fn code_square_inter( | |||
| 4600 | // An OBMC winner commits the flat trial too: `commit_inter_luma` | 4633 | // An OBMC winner commits the flat trial too: `commit_inter_luma` |
| 4601 | // re-predicts each var-tx unit by plain translation, which is not | 4634 | // re-predicts each var-tx unit by plain translation, which is not |
| 4602 | // the prediction this candidate was priced from. | 4635 | // 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 | + { | ||
| 4604 | luma.commit(x, y, side, &luma_new); | 4653 | luma.commit(x, y, side, &luma_new); |
| 4605 | (luma_new.levels.clone(), 0, 0.0) | 4654 | (luma_new.levels.clone(), 0, 0.0) |
| 4606 | } else { | 4655 | } else { |
| 4607 | commit_inter_luma( | 4656 | commit_inter_luma( |
| 4608 | luma, (x, y), side, info.mv, | 4657 | luma, (x, y), side, info.mv, |
| 4609 | (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), | 4658 | (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, |
| 4611 | ) | 4660 | ) |
| 4612 | }; | 4661 | }; |
| 4613 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); | 4662 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); |
| @@ -4642,7 +4691,7 @@ fn code_square_inter( | |||
| 4642 | } | 4691 | } |
| 4643 | if inter_cost < intra_cost { | 4692 | if inter_cost < intra_cost { |
| 4644 | let (levels, tx_depth, dcost) = commit_inter_luma( | 4693 | 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, |
| 4646 | ); | 4695 | ); |
| 4647 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 4696 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 4648 | chroma[1].commit(x / 2, y / 2, side / 2, &v); | 4697 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| @@ -7165,12 +7214,46 @@ fn search_inter_block( | |||
| 7165 | // the film's inter blocks and 17.6% of the screen's. The same shape as | 7214 | // the film's inter blocks and 17.6% of the screen's. The same shape as |
| 7166 | // the compound trial set below: a local RD win that does not convert into | 7215 | // the compound trial set below: a local RD win that does not convert into |
| 7167 | // ladder bytes. The flat 32x32 transform stays. | 7216 | // 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 | + }; | ||
| 7169 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); | 7252 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 7170 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); | 7253 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| 7171 | ( | 7254 | ( |
| 7172 | BlockCoeffs { | 7255 | BlockCoeffs { |
| 7173 | - luma: coeffs(&best.luma.levels, BLOCK), | 7256 | + luma: coeffs(&levels, BLOCK), |
| 7174 | u: coeffs(&best.u.levels, BLOCK / 2), | 7257 | u: coeffs(&best.u.levels, BLOCK / 2), |
| 7175 | v: coeffs(&best.v.levels, BLOCK / 2), | 7258 | v: coeffs(&best.v.levels, BLOCK / 2), |
| 7176 | mode: best.mode, | 7259 | mode: best.mode, |
| @@ -7182,9 +7265,9 @@ fn search_inter_block( | |||
| 7182 | dv: None, | 7265 | dv: None, |
| 7183 | palette: None, | 7266 | palette: None, |
| 7184 | palette_uv: None, | 7267 | palette_uv: None, |
| 7185 | - tx_depth: 0, | 7268 | + tx_depth, |
| 7186 | }, | 7269 | }, |
| 7187 | - best.cost, | 7270 | + best.cost + dcost, |
| 7188 | ) | 7271 | ) |
| 7189 | } | 7272 | } |
| 7190 | 7273 | ||
| @@ -10359,7 +10442,13 @@ mod tests { | |||
| 10359 | (0..5).map(|i| panned_test_card(width, height, i * 3)).collect(); | 10442 | (0..5).map(|i| panned_test_card(width, height, i * 3)).collect(); |
| 10360 | let _ = take_inter_tx_split_hits(); | 10443 | let _ = take_inter_tx_split_hits(); |
| 10361 | let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); | 10444 | 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 | + ]; | ||
| 10363 | eprintln!("inter var-tx: {} blocks flat, {} split", split[0], split[1]); | 10452 | eprintln!("inter var-tx: {} blocks flat, {} split", split[0], split[1]); |
| 10364 | // [[gate-blind-to-feature]]: with inter `TxMode::Select` on, this | 10453 | // [[gate-blind-to-feature]]: with inter `TxMode::Select` on, this |
| 10365 | // content splits -- a green round trip in which the split never fires | 10454 | // content splits -- a green round trip in which the split never fires |
| @@ -11991,23 +12080,7 @@ mod tests { | |||
| 11991 | .collect::<Vec<_>>() | 12080 | .collect::<Vec<_>>() |
| 11992 | .join(" ") | 12081 | .join(" ") |
| 11993 | ); | 12082 | ); |
| 11994 | - // And which var-tx depth this clip's INTER blocks resolved to | 12083 | + print_inter_tx_census(name); |
| 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 | - ); | ||
| 12011 | // lane-av1obmc: how many blocks actually CODED a motion_mode | 12084 | // lane-av1obmc: how many blocks actually CODED a motion_mode |
| 12012 | // symbol and which value they took, by footprint -- "OBMC is on" | 12085 | // symbol and which value they took, by footprint -- "OBMC is on" |
| 12013 | // as a measurement rather than a claim (class | 12086 | // as a measurement rather than a claim (class |
| @@ -12053,6 +12126,31 @@ mod tests { | |||
| 12053 | ); | 12126 | ); |
| 12054 | } | 12127 | } |
| 12055 | } | 12128 | } |
| 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 | + | ||
| 12056 | /// The video stream's coded size, from ffprobe. | 12154 | /// The video stream's coded size, from ffprobe. |
| 12057 | fn probe_dims(clip: &str) -> Option<(usize, usize)> { | 12155 | fn probe_dims(clip: &str) -> Option<(usize, usize)> { |
| 12058 | let out = Command::new("ffprobe") | 12156 | let out = Command::new("ffprobe") |
| @@ -12244,6 +12342,7 @@ mod tests { | |||
| 12244 | 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | 12342 | 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64, |
| 12245 | mm.iter().sum::<usize>(), | 12343 | mm.iter().sum::<usize>(), |
| 12246 | ); | 12344 | ); |
| 12345 | + print_inter_tx_census(name); | ||
| 12247 | let palette = crate::tile::take_palette_hits(); | 12346 | let palette = crate::tile::take_palette_hits(); |
| 12248 | let palette_uv = crate::tile::take_palette_uv_hits(); | 12347 | let palette_uv = crate::tile::take_palette_uv_hits(); |
| 12249 | let ibc = crate::tile::take_intrabc_hits(); | 12348 | let ibc = crate::tile::take_intrabc_hits(); |