forge
Explore
Log in

Don't you dare go Hollow

The top ARF's leaf offer set behind EC_AV1_ARFMODE -- 1 no intra candidate at the 16x16/8x8 leaves (the intra trial's commit was always overwritten by the inter winner's, so the planes never change), 2 no RD-optional 8x8 split (16x16 is the leaf floor

Browse files at this commit

5e19f1af98c5783008c159f89691b4e995665189Ahmet Kaan GümüşParent: 20925bd
  • perf
    • the top ARF's leaf offer set behind EC_AV1_ARFMODE -- 1 no intra candidate at the 16x16/8x8 leaves (the intra trial's commit was always overwritten by the inter winner's, so the planes never change), 2 no RD-optional 8x8 split (16x16 is the leaf floor
    • the spec-forced 8x8s at straddling 16x16s are a different site), 4 no compound candidates at the leaves
    • the 32x32 whole keeps its full set as the scene-cut hatch
    • default 0 is bit-exact (pins 8291/33227 hold at the default and EC_AV1_SPEED=6)
    • witness proves per-arm engagement counters, stream movement on a level-ramp fixture (middle group's ARF sits between its two anchors), coding-order stability and sample-exact decode through decode_stream AND ffmpeg
Show full message
perf(av1): the top ARF's leaf offer set behind EC_AV1_ARFMODE -- 1 no intra candidate at the 16x16/8x8 leaves (the intra trial's commit was always overwritten by the inter winner's, so the planes never change), 2 no RD-optional 8x8 split (16x16 is the leaf floor; the spec-forced 8x8s at straddling 16x16s are a different site), 4 no compound candidates at the leaves; the 32x32 whole keeps its full set as the scene-cut hatch; default 0 is bit-exact (pins 8291/33227 hold at the default and EC_AV1_SPEED=6); witness proves per-arm engagement counters, stream movement on a level-ramp fixture (middle group's ARF sits between its two anchors), coding-order stability and sample-exact decode through decode_stream AND ffmpeg
crates/ec-av1/src/encode.rsmodified+113 -4
@@ -6862,7 +6862,14 @@ fn code_square_inter(
6862 refs: &[Option<&Picture>; 8],6862 refs: &[Option<&Picture>; 8],
6863 fctx: &crate::decode::FrameCtx,6863 fctx: &crate::decode::FrameCtx,
6864 ) -> (BlockCoeffs, f64) {6864 ) -> (BlockCoeffs, f64) {
6865- let (intra_block, intra_cost) =6865+ // lane-arfmode: at the top ARF (arm 1) the leaf offers no intra
6866+ // candidate at all -- `intra_cost` stays at infinity, so every
6867+ // comparison below takes an inter candidate and the fallback arm at the
6868+ // tail is unreachable. The planes hold what the caller snapshotted until
6869+ // the winner commits: the intra trial used to commit here and be
6870+ // overwritten by the inter commit, so skipping it changes no committed
6871+ // sample, only work.
6872+ let (intra_block, intra_cost) = if search.arf_leaf_intra {
6866 code_square(6873 code_square(
6867 luma,6874 luma,
6868 chroma,6875 chroma,
@@ -6874,7 +6881,11 @@ fn code_square_inter(
6874 None,6881 None,
6875 false,6882 false,
6876 fctx,6883 fctx,
6877- );6884+ )
6885+ } else {
6886+ arfmode_hit(0);
6887+ (BlockCoeffs::default(), f64::INFINITY)
6888+ };
6878 let luma_set = if side == 8 {6889 let luma_set = if side == 8 {
6879 TxbSet::Luma8Inter6890 TxbSet::Luma8Inter
6880 } else {6891 } else {
@@ -7129,7 +7140,13 @@ fn code_square_inter(
7129 // SECOND-reference search of its own -- `NEAREST_NEWMV` and `NEW_NEWMV`7140 // SECOND-reference search of its own -- `NEAREST_NEWMV` and `NEW_NEWMV`
7130 // too. Priced with the same static-CDF approximation, plus the7141 // too. Priced with the same static-CDF approximation, plus the
7131 // compound-only syntax the writer emits.7142 // compound-only syntax the writer emits.
7132- for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) {7143+ if leaf_compound() && !search.arf_leaf_compound && !compound.is_empty() {
7144+ // lane-arfmode: arm 4 drops this leaf's compound set (counted once
7145+ // per leaf, not once per pair); the single-reference candidates
7146+ // above stay.
7147+ arfmode_hit(2);
7148+ }
7149+ for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound() && search.arf_leaf_compound) {
7133 let (ref1, g) = (*ref1, *g);7150 let (ref1, g) = (*ref1, *g);
7134 // The whole `comp_mode` + reference-pair tree at the writer's own7151 // The whole `comp_mode` + reference-pair tree at the writer's own
7135 // contexts, for THIS pair (lane-ctx).7152 // contexts, for THIS pair (lane-ctx).
@@ -7762,6 +7779,72 @@ struct Search<'a> {
7762 /// `intra_pruning_with_hog` -- never prunes it away). `None` runs every7779 /// `intra_pruning_with_hog` -- never prunes it away). `None` runs every
7763 /// mode through full RD, unchanged from before this lever.7780 /// mode through full RD, unchanged from before this lever.
7764 top_k: Option<usize>,7781 top_k: Option<usize>,
7782+ /// lane-arfmode: the top ARF's leaf offer set ([`crate::speed::ARFMODE`])
7783+ /// -- `false` cuts the named candidate group from the 16x16/8x8 LEAF
7784+ /// search ([`code_square_inter`]) and the 8x8 split offer at the top ARF
7785+ /// ([`DqLevel::TopArf`] frames only). `true` everywhere else, which is
7786+ /// the pre-lane behaviour: the key frame never reaches
7787+ /// [`code_square_inter`], the 32x32 whole keeps its full set, and the
7788+ /// straddling-edge 8x8s are only mode-gated, never partition-gated.
7789+ arf_leaf_intra: bool,
7790+ arf_leaf8: bool,
7791+ arf_leaf_compound: bool,
7792+}
7793+
7794+/// [`Search`]'s ARF fields' source: [`crate::speed::ARFMODE`], or what
7795+/// `EC_AV1_ARFMODE` names on one build (a missing or broken value keeps the
7796+/// shipped table -- 0, bit-exact with the pre-lane encoder).
7797+fn arfmode() -> u8 {
7798+ match ARFMODE_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) {
7799+ u8::MAX => {}
7800+ n => return n,
7801+ }
7802+ static ENV: std::sync::LazyLock<Option<u8>> = std::sync::LazyLock::new(|| {
7803+ crate::envflags::var("EC_AV1_ARFMODE").ok().and_then(|v| v.parse().ok())
7804+ });
7805+ ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::ARFMODE))
7806+}
7807+
7808+/// [`ARFMODE_OVERRIDE`]: `u8::MAX` clears it.
7809+static ARFMODE_OVERRIDE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(u8::MAX);
7810+
7811+/// Sets [`ARFMODE_OVERRIDE`]; `None` clears it. Test-only: the env is a
7812+/// process constant ([`crate::envflags`]), so an in-process witness needs
7813+/// this to arm the lever.
7814+#[allow(dead_code)] // set only from the `#[cfg(test)]` witness
7815+pub(crate) fn set_arfmode(n: Option<u8>) {
7816+ ARFMODE_OVERRIDE.store(n.unwrap_or(u8::MAX), std::sync::atomic::Ordering::Relaxed);
7817+}
7818+
7819+/// How often each armed [`crate::speed::ARFMODE`] arm actually fired since
7820+/// the last take (class `gate-blind-to-feature`): 0 = a top-ARF leaf coded
7821+/// with its intra candidate skipped, 1 = a top-ARF 16x16 leaf whose 8x8
7822+/// split the search would have offered and the arm refused, 2 = a top-ARF
7823+/// leaf whose compound set was dropped. A green gate over counts of zero
7824+/// proves nothing about the lever.
7825+static ARFMODE_HITS: [std::sync::atomic::AtomicUsize; 3] = [
7826+ std::sync::atomic::AtomicUsize::new(0),
7827+ std::sync::atomic::AtomicUsize::new(0),
7828+ std::sync::atomic::AtomicUsize::new(0),
7829+];
7830+
7831+/// Records one such hit.
7832+fn arfmode_hit(which: usize) {
7833+ ARFMODE_HITS[which].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7834+}
7835+
7836+/// [`arfmode_hit`]`(1)` and "no": the `||` arm of the top ARF's 8x8-offer
7837+/// condition, so the counter only runs when every other prune let the split
7838+/// through and the arm alone refuses it.
7839+fn arfmode_hit_and_no() -> bool {
7840+ arfmode_hit(1);
7841+ false
7842+}
7843+
7844+/// Takes (and clears) the counts.
7845+#[cfg(test)]
7846+pub(crate) fn take_arfmode_hits() -> [usize; 3] {
7847+ [0, 1, 2].map(|i| ARFMODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
7765 }7848 }
7766 7849
7767 /// [`Search::top_k`]'s value: `None` keeps every mode's search unchanged7850 /// [`Search::top_k`]'s value: `None` keeps every mode's search unchanged
@@ -8419,6 +8502,9 @@ pub(crate) fn encode_key_frame_inner(
8419 modes,8502 modes,
8420 top_k: prune_top_k(),8503 top_k: prune_top_k(),
8421 screen,8504 screen,
8505+ arf_leaf_intra: true,
8506+ arf_leaf8: true,
8507+ arf_leaf_compound: true,
8422 };8508 };
8423 8509
8424 // The block grid this frame is coded over: [`crate::tile::block_grid`]'s8510 // The block grid this frame is coded over: [`crate::tile::block_grid`]'s
@@ -12863,6 +12949,11 @@ pub(crate) fn encode_inter_frame(
12863 ];12949 ];
12864 12950
12865 let step = f64::from(ac_q(8, i32::from(base_q_idx))) / 8.0;12951 let step = f64::from(ac_q(8, i32::from(base_q_idx))) / 8.0;
12952+ let top_arf = matches!(
12953+ pyramid,
12954+ Some(p) if p.dq_level == DqLevel::TopArf
12955+ );
12956+ let arfmode_v = arfmode();
12866 let search = Search {12957 let search = Search {
12867 base_q_idx,12958 base_q_idx,
12868 deadzone,12959 deadzone,
@@ -12876,6 +12967,14 @@ pub(crate) fn encode_inter_frame(
12876 modes: &KEY_FRAME_MODES,12967 modes: &KEY_FRAME_MODES,
12877 top_k: prune_top_k_inter(),12968 top_k: prune_top_k_inter(),
12878 screen,12969 screen,
12970+ // lane-arfmode: the group's own anchor ([`DqLevel::TopArf`], whose
12971+ // offset is the pyramid's own `arf_q_offset`) reads its leaf offer
12972+ // set from [`crate::speed::ARFMODE`]; every other frame -- the mid
12973+ // and quarter hidden frames, the leaves, the flat path -- keeps the
12974+ // full set.
12975+ arf_leaf_intra: !top_arf || arfmode_v & 1 == 0,
12976+ arf_leaf8: !top_arf || arfmode_v & 2 == 0,
12977+ arf_leaf_compound: !top_arf || arfmode_v & 4 == 0,
12879 };12978 };
12880 let mode_bits_table = inter_mode_bits();12979 let mode_bits_table = inter_mode_bits();
12881 // lane-av1tpl: one lambda factor per 64x64 superblock, from how much of12980 // lane-av1tpl: one lambda factor per 64x64 superblock, from how much of
@@ -13586,7 +13685,17 @@ pub(crate) fn encode_inter_frame(
13586 let eight = (!leaf.skip13685 let eight = (!leaf.skip
13587 && !split_rd_breakout(leaf_cost, SUB, search.lambda)13686 && !split_rd_breakout(leaf_cost, SUB, search.lambda)
13588 && (breakout == 0 || leaf.luma.len() > breakout)13687 && (breakout == 0 || leaf.luma.len() > breakout)
13589- && split_inter_8())13688+ && split_inter_8()
13689+ // lane-arfmode: arm 2 makes 16x16 the top
13690+ // ARF's leaf floor -- the RD-optional 8x8
13691+ // comparison is not offered. Counted only
13692+ // when every other prune let it through,
13693+ // so the counter means "refused", not
13694+ // "never considered". The spec-forced 8x8s
13695+ // at a straddling 16x16 (the
13696+ // `sub_positions` path below) are a
13697+ // different site and stay.
13698+ && (search.arf_leaf8 || arfmode_hit_and_no()))
13590 .then(|| {13699 .then(|| {
13591 restore(13700 restore(
13592 &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base,13701 &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base,
crates/ec-av1/src/encoder.rsmodified+93 -0
@@ -3971,6 +3971,99 @@ mod tests {
3971 crate::speed::set_speed(was);3971 crate::speed::set_speed(was);
3972 }3972 }
3973 3973
3974+ /// lane-arfmode: the top ARF's leaf offer set. Three guards in one
3975+ /// witness, over 17 pictures at gop 32 (three groups, so several top
3976+ /// ARFs) at q=60 -- the lookahead witness's lesson: at a fine q the
3977+ /// anchor carries real coefficients, so a cut that moves only the
3978+ /// hidden frame's internal decisions cannot hide behind "everything
3979+ /// quantized to the same bytes":
3980+ ///
3981+ /// 1. OFF (default and explicit `Some(0)`) is byte-identical -- the
3982+ /// shipped table is 0 and the lever is bit-exact when off;
3983+ /// 2. each armed mode (1 no intra, 2 no 8x8, 4 no compound) really
3984+ /// FIRES -- its own [`crate::encode::take_arfmode_hits`] counter
3985+ /// moves (class `gate-blind-to-feature`) -- and MOVES THE STREAM;
3986+ /// 3. the coding order and levels never change, and every armed stream
3987+ /// decodes sample-exact through `decode_stream` AND ffmpeg.
3988+ #[test]
3989+ fn the_arf_leaf_offer_set_fires_moves_the_stream_and_decodes_sample_exact() {
3990+ let _knobs = crate::speed::knob_write();
3991+ let was = crate::speed::speed();
3992+ crate::speed::set_speed(0);
3993+ crate::encode::force_screen(Some(false));
3994+ let (width, height) = (128usize, 128usize);
3995+ let run = |mode: Option<u8>| -> (Vec<u8>, Vec<Packet>) {
3996+ crate::encode::set_arfmode(mode);
3997+ let _ = crate::encode::take_arfmode_hits();
3998+ let config = EncoderConfig {
3999+ base_q_idx: 60,
4000+ width,
4001+ height,
4002+ gop: 32,
4003+ colour: Colour::Bt709Limited,
4004+ tile_cols_log2: 0,
4005+ tile_rows_log2: 0,
4006+ };
4007+ let mut enc = Av1Encoder::with_pyramid(config, Pyramid::default()).unwrap();
4008+ // Groups at three luma LEVELS (the base ramp 40/110/180): the
4009+ // middle group's top ARF sits halfway between the two dark/light
4010+ // anchors its slots hold, which is exactly where neither
4011+ // single-reference prediction nor intra wins by default -- the
4012+ // AVERAGE of two references (compound) and the DC of the new
4013+ // level (intra) do. Without this the synthetic cards' noise
4014+ // quantizes to skip and no arm can move anything (measured:
4015+ // 516 intra trials skipped, zero won, stream identical).
4016+ let sources: Vec<Picture> = (0..17)
4017+ .map(|t| {
4018+ let mut p = noisy_card(width, height, t);
4019+ let shift = 70i32 * (t / 8) as i32;
4020+ for v in p.y.iter_mut() {
4021+ *v = (i32::from(*v) + shift).clamp(0, 255) as u16;
4022+ }
4023+ p
4024+ })
4025+ .collect();
4026+ let packets = encode_all(&mut enc, &sources);
4027+ (concat(&packets), packets)
4028+ };
4029+ let (off, off_packets) = run(None);
4030+ let (explicit_off, _) = run(Some(0));
4031+ assert_eq!(off, explicit_off, "mode 0 is not bit-exact with the default");
4032+ for (mode, arm) in [(1u8, 0usize), (2, 1), (4, 2)] {
4033+ let (on, packets) = run(Some(mode));
4034+ let hits = crate::encode::take_arfmode_hits();
4035+ assert_eq!(
4036+ packets.iter().map(|p| (p.order, p.key, p.level)).collect::<Vec<_>>(),
4037+ off_packets.iter().map(|p| (p.order, p.key, p.level)).collect::<Vec<_>>(),
4038+ "mode {mode}: coding order/levels moved"
4039+ );
4040+ assert!(
4041+ hits[arm] > 0,
4042+ "mode {mode}: its counter never fired ({hits:?}) -- the lever is \
4043+ gate-blind on this fixture",
4044+ );
4045+ assert_ne!(
4046+ off, on,
4047+ "mode {mode} left the stream byte-identical despite {} firing hits",
4048+ hits[arm],
4049+ );
4050+ eprintln!("mode {mode}: {} -> {} B, hits {hits:?}", off.len(), on.len());
4051+ let ours = crate::stream::decode_stream(&on).expect("our decoder");
4052+ assert_eq!(ours.len(), 17, "mode {mode}: our decoder's picture count");
4053+ if !have_ffmpeg() {
4054+ continue;
4055+ }
4056+ let theirs = ffmpeg_decode_luma(&on, width, height);
4057+ assert_eq!(theirs.len(), 17, "mode {mode}: ffmpeg's picture count");
4058+ for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() {
4059+ let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect();
4060+ assert!(got == *b, "mode {mode}, frame {i}: ours differs from ffmpeg");
4061+ }
4062+ }
4063+ crate::encode::set_arfmode(None);
4064+ crate::speed::set_speed(was);
4065+ }
4066+
3974 fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) {4067 fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) {
3975 let (width, height) = (128usize, 128usize);4068 let (width, height) = (128usize, 128usize);
3976 let config = EncoderConfig {4069 let config = EncoderConfig {
crates/ec-av1/src/speed.rsmodified+20 -0
@@ -460,6 +460,26 @@ pub(crate) const ARF_TF_WIN: [usize; 11] = [3; 11];
460 /// so [`ARF_TF`] stays where lane-arftf put it.460 /// so [`ARF_TF`] stays where lane-arftf put it.
461 pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [3; 11];461 pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [3; 11];
462 462
463+/// lane-arfmode: the top ARF's LEAF offer set, as a bitmask -- `1` the 16x16
464+/// and 8x8 leaves of the group's own anchor ([`crate::encode::DqLevel::
465+/// TopArf`]) offer no INTRA candidate, `2` a 16x16 leaf under it is not
466+/// offered the 8x8 split, `4` the leaves offer no compound candidates;
467+/// combinations OR, `0` (the default) is bit-exact with the pre-lane
468+/// encoder. `EC_AV1_ARFMODE=<n>` overrides it. The 32x32 whole keeps its
469+/// full offer set (the scene-cut hatch), and the spec-forced 8x8s at
470+/// straddling edges are untouched -- only RD-optional offers are cut.
471+///
472+/// The census it answers to (lanes/arfmode.charter.md, at 0d81d6c5): the top
473+/// ARF's syntax gap against rav1e's own ARF at the same position is
474+/// film A +1337 / film B +971 B a frame across mode+mv+partition, and the
475+/// mode family is carried by syntax rav1e does not spend there -- an intra
476+/// fallback whose 4-15% area share de-optimizes the shared `intra_inter`
477+/// CDF (ours 138 B/frame against its 10 on film B), compound at 19-24% of
478+/// the area against its 0%, and an 8x8 split rate 2.4x its own. CAUTION the
479+/// same census records: on the grain film even rav1e keeps 12.3% intra area
480+/// at its ARF, so arm 1's residual risk is concentrated exactly there.
481+pub(crate) const ARFMODE: [u8; 11] = [0; 11];
482+
463 /// lane-lookahead: whether [`crate::encoder::Av1Encoder`] holds one whole483 /// lane-lookahead: whether [`crate::encoder::Av1Encoder`] holds one whole
464 /// mini-GOP of lookahead -- the next group's sources are buffered before the484 /// mini-GOP of lookahead -- the next group's sources are buffered before the
465 /// current group's top ARF is coded, so that ARF has display-FUTURE pictures485 /// current group's top ARF is coded, so that ARF has display-FUTURE pictures