diff --git a/crates/ec-av1/examples/enc_probe.rs b/crates/ec-av1/examples/enc_probe.rs index 4f9afc34..27684562 100644 --- a/crates/ec-av1/examples/enc_probe.rs +++ b/crates/ec-av1/examples/enc_probe.rs @@ -73,6 +73,13 @@ fn main() { println!(" census luma SAD rank of RD winner: {}", pct(&luma_rank)); println!(" census chroma SAD rank of RD winner: {}", pct(&chroma_rank)); } + // lane-arfmode: the engagement counter of the top ARF's offer-set + // lever, taken once per point so an armed run PRINTS a non-zero count + // (class gate-blind-to-feature; a BD number over a counter of zero + // proves nothing). Zeros when `EC_AV1_ARFMODE` is unset, which is the + // control's own expectation. + let hits = ec_av1::encode::take_arfmode_hits(); + println!(" arfmode hits: {} {} {}", hits[0], hits[1], hits[2]); } println!("wall {:.3}s", start.elapsed().as_secs_f64()); } diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index d50779a6..7aa68273 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -6862,7 +6862,14 @@ fn code_square_inter( refs: &[Option<&Picture>; 8], fctx: &crate::decode::FrameCtx, ) -> (BlockCoeffs, f64) { - let (intra_block, intra_cost) = + // lane-arfmode: at the top ARF (arm 1) the leaf offers no intra + // candidate at all -- `intra_cost` stays at infinity, so every + // comparison below takes an inter candidate and the fallback arm at the + // tail is unreachable. The planes hold what the caller snapshotted until + // the winner commits: the intra trial used to commit here and be + // overwritten by the inter commit, so skipping it changes no committed + // sample, only work. + let (intra_block, intra_cost) = if search.arf_leaf_intra { code_square( luma, chroma, @@ -6874,7 +6881,11 @@ fn code_square_inter( None, false, fctx, - ); + ) + } else { + arfmode_hit(0); + (BlockCoeffs::default(), f64::INFINITY) + }; let luma_set = if side == 8 { TxbSet::Luma8Inter } else { @@ -7129,7 +7140,13 @@ fn code_square_inter( // SECOND-reference search of its own -- `NEAREST_NEWMV` and `NEW_NEWMV` // too. Priced with the same static-CDF approximation, plus the // compound-only syntax the writer emits. - for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound()) { + if leaf_compound() && !search.arf_leaf_compound && !compound.is_empty() { + // lane-arfmode: arm 4 drops this leaf's compound set (counted once + // per leaf, not once per pair); the single-reference candidates + // above stay. + arfmode_hit(2); + } + for (ref1, g, cstack) in compound.iter().filter(|_| leaf_compound() && search.arf_leaf_compound) { let (ref1, g) = (*ref1, *g); // The whole `comp_mode` + reference-pair tree at the writer's own // contexts, for THIS pair (lane-ctx). @@ -7762,11 +7779,79 @@ struct Search<'a> { /// `intra_pruning_with_hog` -- never prunes it away). `None` runs every /// mode through full RD, unchanged from before this lever. top_k: Option, + /// lane-arfmode: the top ARF's offer set ([`crate::speed::ARFMODE`]) + /// -- `false` cuts the named candidate group from the whole-32 search + /// ([`search_inter_block`]), the 16x16/8x8 leaf search + /// ([`code_square_inter`]) and the 8x8 split offer, at the top ARF + /// ([`DqLevel::TopArf`] frames only). `true` everywhere else, which is + /// the pre-lane behaviour: the mid and quarter hidden frames, the shown + /// leaves and the flat path keep intra and compound, and the + /// straddling-edge 8x8s are only mode-gated, never partition-gated. + /// The probe's correction: with the leaf gate ALONE the film A stream + /// was byte-identical to the control -- the ARF's intra area lives at + /// the whole-32 level, so the intra arm must cut it there too. + arf_leaf_intra: bool, + arf_leaf8: bool, + arf_leaf_compound: bool, +} + +/// [`Search`]'s ARF fields' source: [`crate::speed::ARFMODE`], or what +/// `EC_AV1_ARFMODE` names on one build (a missing or broken value keeps the +/// shipped table -- 0, bit-exact with the pre-lane encoder). +fn arfmode() -> u8 { + match ARFMODE_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) { + u8::MAX => {} + n => return n, + } + static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + crate::envflags::var("EC_AV1_ARFMODE").ok().and_then(|v| v.parse().ok()) + }); + ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::ARFMODE)) +} + +/// [`ARFMODE_OVERRIDE`]: `u8::MAX` clears it. +static ARFMODE_OVERRIDE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(u8::MAX); + +/// Sets [`ARFMODE_OVERRIDE`]; `None` clears it. Test-only: the env is a +/// process constant ([`crate::envflags`]), so an in-process witness needs +/// this to arm the lever. +#[allow(dead_code)] // set only from the `#[cfg(test)]` witness +pub(crate) fn set_arfmode(n: Option) { + ARFMODE_OVERRIDE.store(n.unwrap_or(u8::MAX), std::sync::atomic::Ordering::Relaxed); +} + +/// How often each armed [`crate::speed::ARFMODE`] arm actually fired since +/// the last take (class `gate-blind-to-feature`): 0 = a top-ARF leaf coded +/// with its intra candidate skipped, 1 = a top-ARF 16x16 leaf whose 8x8 +/// split the search would have offered and the arm refused, 2 = a top-ARF +/// leaf whose compound set was dropped. A green gate over counts of zero +/// proves nothing about the lever. +static ARFMODE_HITS: [std::sync::atomic::AtomicUsize; 3] = [ + std::sync::atomic::AtomicUsize::new(0), + std::sync::atomic::AtomicUsize::new(0), + std::sync::atomic::AtomicUsize::new(0), +]; + +/// Records one such hit. +fn arfmode_hit(which: usize) { + ARFMODE_HITS[which].fetch_add(1, std::sync::atomic::Ordering::Relaxed); +} + +/// [`arfmode_hit`]`(1)` and "no": the `||` arm of the top ARF's 8x8-offer +/// condition, so the counter only runs when every other prune let the split +/// through and the arm alone refuses it. +fn arfmode_hit_and_no() -> bool { + arfmode_hit(1); + false +} + +/// Takes (and clears) the counts. `pub` for `examples/enc_probe`, so a +/// probe point can print a non-zero engagement count under the env knob +/// itself -- the counter the gate BD numbers are read against. +pub fn take_arfmode_hits() -> [usize; 3] { + [0, 1, 2].map(|i| ARFMODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) } -/// [`Search::top_k`]'s value: `None` keeps every mode's search unchanged -/// (the default, until a lever's quality gate justifies pruning by default); -/// `EC_AV1_PRUNE_K` sweeps it in a test build without touching callers. fn prune_top_k() -> Option { #[cfg(test)] { @@ -8419,6 +8504,9 @@ pub(crate) fn encode_key_frame_inner( modes, top_k: prune_top_k(), screen, + arf_leaf_intra: true, + arf_leaf8: true, + arf_leaf_compound: true, }; // The block grid this frame is coded over: [`crate::tile::block_grid`]'s @@ -10932,43 +11020,52 @@ fn search_inter_block( _ => search.modes.to_vec(), }; census_add(5, 1); - census_add(6, intra_modes.len()); - for &mode in &intra_modes { - let luma_trial = luma.trial( - At { - x, - y, - side: BLOCK, - reach, - set: luma_set, - }, - mode, - 0, - search.base_q_idx, - search.deadzone, fctx, - ); - let u = u_trial.clone(); - let v = v_trial.clone(); - let cost = luma_trial.sse - + u.sse - + v.sse - + search.lambda - * (luma_trial.bits - + u.bits - + v.bits - + mode_bits[usize::from(mode)] - + skip_bits(false) - + intra_inter_bits(false)); - consider(Candidate { - cost, - luma: luma_trial, - u, - v, - mode, - skip: false, - inter: None, - mode_bits: mode_bits[usize::from(mode)], - }); + census_add(6, if search.arf_leaf_intra { intra_modes.len() } else { 0 }); + // lane-arfmode: arm 1 drops the whole-32 block's intra candidates at the + // top ARF too -- the probe proved the ARF's intra AREA lives HERE, not at + // the leaves (film A arm 1 vs control byte-identical with the leaf gate + // alone), so "the 32x32 whole keeps intra as the scene-cut hatch" was a + // hatch nothing else was using. Counted once per block. + if search.arf_leaf_intra { + for &mode in &intra_modes { + let luma_trial = luma.trial( + At { + x, + y, + side: BLOCK, + reach, + set: luma_set, + }, + mode, + 0, + search.base_q_idx, + search.deadzone, fctx, + ); + let u = u_trial.clone(); + let v = v_trial.clone(); + let cost = luma_trial.sse + + u.sse + + v.sse + + search.lambda + * (luma_trial.bits + + u.bits + + v.bits + + mode_bits[usize::from(mode)] + + skip_bits(false) + + intra_inter_bits(false)); + consider(Candidate { + cost, + luma: luma_trial, + u, + v, + mode, + skip: false, + inter: None, + mode_bits: mode_bits[usize::from(mode)], + }); + } + } else { + arfmode_hit(0); } // The reference frame buffer a spec decoder holds is exactly the true @@ -12863,6 +12960,11 @@ pub(crate) fn encode_inter_frame( ]; let step = f64::from(ac_q(8, i32::from(base_q_idx))) / 8.0; + let top_arf = matches!( + pyramid, + Some(p) if p.dq_level == DqLevel::TopArf + ); + let arfmode_v = arfmode(); let search = Search { base_q_idx, deadzone, @@ -12876,6 +12978,14 @@ pub(crate) fn encode_inter_frame( modes: &KEY_FRAME_MODES, top_k: prune_top_k_inter(), screen, + // lane-arfmode: the group's own anchor ([`DqLevel::TopArf`], whose + // offset is the pyramid's own `arf_q_offset`) reads its leaf offer + // set from [`crate::speed::ARFMODE`]; every other frame -- the mid + // and quarter hidden frames, the leaves, the flat path -- keeps the + // full set. + arf_leaf_intra: !top_arf || arfmode_v & 1 == 0, + arf_leaf8: !top_arf || arfmode_v & 2 == 0, + arf_leaf_compound: !top_arf || arfmode_v & 4 == 0, }; let mode_bits_table = inter_mode_bits(); // lane-av1tpl: one lambda factor per 64x64 superblock, from how much of @@ -13586,7 +13696,17 @@ pub(crate) fn encode_inter_frame( let eight = (!leaf.skip && !split_rd_breakout(leaf_cost, SUB, search.lambda) && (breakout == 0 || leaf.luma.len() > breakout) - && split_inter_8()) + && split_inter_8() + // lane-arfmode: arm 2 makes 16x16 the top + // ARF's leaf floor -- the RD-optional 8x8 + // comparison is not offered. Counted only + // when every other prune let it through, + // so the counter means "refused", not + // "never considered". The spec-forced 8x8s + // at a straddling 16x16 (the + // `sub_positions` path below) are a + // different site and stay. + && (search.arf_leaf8 || arfmode_hit_and_no())) .then(|| { restore( &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base, diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 57da7296..b0aac9f4 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -3971,6 +3971,99 @@ mod tests { crate::speed::set_speed(was); } + /// lane-arfmode: the top ARF's leaf offer set. Three guards in one + /// witness, over 17 pictures at gop 32 (three groups, so several top + /// ARFs) at q=60 -- the lookahead witness's lesson: at a fine q the + /// anchor carries real coefficients, so a cut that moves only the + /// hidden frame's internal decisions cannot hide behind "everything + /// quantized to the same bytes": + /// + /// 1. OFF (default and explicit `Some(0)`) is byte-identical -- the + /// shipped table is 0 and the lever is bit-exact when off; + /// 2. each armed mode (1 no intra, 2 no 8x8, 4 no compound) really + /// FIRES -- its own [`crate::encode::take_arfmode_hits`] counter + /// moves (class `gate-blind-to-feature`) -- and MOVES THE STREAM; + /// 3. the coding order and levels never change, and every armed stream + /// decodes sample-exact through `decode_stream` AND ffmpeg. + #[test] + fn the_arf_leaf_offer_set_fires_moves_the_stream_and_decodes_sample_exact() { + let _knobs = crate::speed::knob_write(); + let was = crate::speed::speed(); + crate::speed::set_speed(0); + crate::encode::force_screen(Some(false)); + let (width, height) = (128usize, 128usize); + let run = |mode: Option| -> (Vec, Vec) { + crate::encode::set_arfmode(mode); + let _ = crate::encode::take_arfmode_hits(); + let config = EncoderConfig { + base_q_idx: 60, + width, + height, + gop: 32, + colour: Colour::Bt709Limited, + tile_cols_log2: 0, + tile_rows_log2: 0, + }; + let mut enc = Av1Encoder::with_pyramid(config, Pyramid::default()).unwrap(); + // Groups at three luma LEVELS (the base ramp 40/110/180): the + // middle group's top ARF sits halfway between the two dark/light + // anchors its slots hold, which is exactly where neither + // single-reference prediction nor intra wins by default -- the + // AVERAGE of two references (compound) and the DC of the new + // level (intra) do. Without this the synthetic cards' noise + // quantizes to skip and no arm can move anything (measured: + // 516 intra trials skipped, zero won, stream identical). + let sources: Vec = (0..17) + .map(|t| { + let mut p = noisy_card(width, height, t); + let shift = 70i32 * (t / 8) as i32; + for v in p.y.iter_mut() { + *v = (i32::from(*v) + shift).clamp(0, 255) as u16; + } + p + }) + .collect(); + let packets = encode_all(&mut enc, &sources); + (concat(&packets), packets) + }; + let (off, off_packets) = run(None); + let (explicit_off, _) = run(Some(0)); + assert_eq!(off, explicit_off, "mode 0 is not bit-exact with the default"); + for (mode, arm) in [(1u8, 0usize), (2, 1), (4, 2)] { + let (on, packets) = run(Some(mode)); + let hits = crate::encode::take_arfmode_hits(); + assert_eq!( + packets.iter().map(|p| (p.order, p.key, p.level)).collect::>(), + off_packets.iter().map(|p| (p.order, p.key, p.level)).collect::>(), + "mode {mode}: coding order/levels moved" + ); + assert!( + hits[arm] > 0, + "mode {mode}: its counter never fired ({hits:?}) -- the lever is \ + gate-blind on this fixture", + ); + assert_ne!( + off, on, + "mode {mode} left the stream byte-identical despite {} firing hits", + hits[arm], + ); + eprintln!("mode {mode}: {} -> {} B, hits {hits:?}", off.len(), on.len()); + let ours = crate::stream::decode_stream(&on).expect("our decoder"); + assert_eq!(ours.len(), 17, "mode {mode}: our decoder's picture count"); + if !have_ffmpeg() { + continue; + } + let theirs = ffmpeg_decode_luma(&on, width, height); + assert_eq!(theirs.len(), 17, "mode {mode}: ffmpeg's picture count"); + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { + let got: Vec = a.y.iter().map(|&v| v as u8).collect(); + assert!(got == *b, "mode {mode}, frame {i}: ours differs from ffmpeg"); + } + } + crate::encode::set_arfmode(None); + crate::speed::set_speed(was); + } + fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { let (width, height) = (128usize, 128usize); let config = EncoderConfig { diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index b4bf6f68..abd5b212 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -460,6 +460,27 @@ pub(crate) const ARF_TF_WIN: [usize; 11] = [3; 11]; /// so [`ARF_TF`] stays where lane-arftf put it. pub(crate) const ARF_TF_WIN_FUT: [usize; 11] = [3; 11]; +/// lane-arfmode: the top ARF's offer set, as a bitmask -- `1` the group's +/// own anchor ([`crate::encode::DqLevel::TopArf`]) offers no INTRA +/// candidate at any block size (the whole-32 search and the 16x16/8x8 +/// leaves), `2` a 16x16 leaf under it is not offered the 8x8 split, `4` the +/// leaves offer no compound candidates; combinations OR, `0` (the default) +/// is bit-exact with the pre-lane encoder. `EC_AV1_ARFMODE=` overrides +/// it. The spec-forced 8x8s at straddling edges are untouched -- only +/// RD-optional offers are cut; the mid and quarter hidden frames and every +/// shown leaf keep the full set. +/// +/// The census it answers to (lanes/arfmode.charter.md, at 0d81d6c5): the top +/// ARF's syntax gap against rav1e's own ARF at the same position is +/// film A +1337 / film B +971 B a frame across mode+mv+partition, and the +/// mode family is carried by syntax rav1e does not spend there -- an intra +/// fallback whose 4-15% area share de-optimizes the shared `intra_inter` +/// CDF (ours 138 B/frame against its 10 on film B), compound at 19-24% of +/// the area against its 0%, and an 8x8 split rate 2.4x its own. CAUTION the +/// same census records: on the grain film even rav1e keeps 12.3% intra area +/// at its ARF, so arm 1's residual risk is concentrated exactly there. +pub(crate) const ARFMODE: [u8; 11] = [0; 11]; + /// lane-lookahead: whether [`crate::encoder::Av1Encoder`] holds one whole /// mini-GOP of lookahead -- the next group's sources are buffered before the /// current group's top ARF is coded, so that ARF has display-FUTURE pictures diff --git a/lanes/arfmode.charter.md b/lanes/arfmode.charter.md new file mode 100644 index 00000000..bd0a6120 --- /dev/null +++ b/lanes/arfmode.charter.md @@ -0,0 +1,154 @@ +# lane-arfmode — the top ARF's mode/partition syntax + +Worktree `edith_codecs-arfmode`, branch `lane-arfmode`, base `0d81d6c5`. The +lane the arffilt reconciliation defers: "the top ARF's mode/partition gap +(+871 B/frame, 2196 blocks vs rav1e's 1507) — the residual half of the census +is done, the syntax half is not". The +871 was written at lane-arfpred's base +`35874aef`, before the temporal filter, the tpl future map, `arf_altref` and +the 128 roots shipped; this lane re-measures the gap at its own base, forms +the lever from what is LEFT of it, and takes it to the deciding gate. + +## 1. Prior art — reconciled, nothing rebuilt + +| prior lane | what it established at the top ARF | state at this base | +|---|---|---| +| lane-arfcen | the per-level stream census; the gap sits in the six pictures coded at distance 8 | instrument shipped (`EC_CENSUS_PERFRAME=1`) | +| lane-arfpred | `arf_pred_census` (lag is NOT the cause); the +871 B mode/partition decomposition; `arf_temporal_filter` shipped at strength 3 | residual half attacked; syntax half deferred to THIS lane | +| lane-arftf | filter strength 3 -> 2, bracketed; mid-ARF filter refuted | shipped (`EC_AV1_ARF_TF`) | +| lane-lookahead | one-group lookahead; symmetric +-3 filter window | shipped (`EC_AV1_LOOKAHEAD`, `ARF_TF_WIN_FUT`) | +| lane-tplfut / lane-tplhalf / lane-tplfut-arf | the tpl future map at the ARF — shipped, re-measured ON (film B -1.0/-0.6); the arffilt pointer to it was stale | shipped (`TPL_FUT=2`, `HALF=2`, `WIN=11`) | +| lane-arfq / lane-keyq | ARF q offsets and key allocation swept | no mode/partition lever | +| lane-arfcen arm 2 (`arf_altref`) | the top ARF's second, distinct past anchor — what its compound candidates read | shipped | +| lanes av1comp3/av1comp4 | leaf compound candidates (5 shapes) shipped GLOBALLY — never A/B'd at the ARF alone | open at ARF granularity | +| lanes census-filmB / census-longgop | level allocation; leaves already cheap at 48 pictures | context | + +No knob named `EC_AV1_ARFMODE` or similar exists; nothing here is a second +convention beside a shipped one. The census instrument +(`examples/syntax_census`, `EC_AV1_BITCENSUS=1 EC_CENSUS_PERFRAME=1`) is the +charter's reader — ours and rav1e measured by the same decoder. + +## 2. The fresh census at `0d81d6c5` — where the gap is NOW + +Recipe: the long-GOP gate's own window and crops, ours `enc_probe gate 0 48 +150`, rav1e `speed 6 quantizer 110` from the 8-bit yuv (a 10-bit rav1e stream +does not decode in our reader — PSNR reads 2 dB; the 8-bit re-cut reproduces +arfpred's table to the digit: rav1e top ARF 3872 B/frame, PSNR-Y 45.97, +base_q_idx 104). `EC_CENSUS_TABLES=24`. + +### 2.1 Family decomposition, B/frame, ours (q118, 6 ARFs) vs rav1e (q104, 11 ARFs) + +| family | film A ours | film A rav1e | A delta | film B ours | film B rav1e | B delta | +|---|---|---|---|---|---|---| +| coeff | 6216 | 4568 | +1647 | 2938 | 1996 | +942 | +| literal | 1928 | 1459 | +469 | 959 | 730 | +229 | +| **mode** | **1845** | **1102** | **+744** | **1137** | **608** | **+530** | +| **mv** | **974** | **520** | **+454** | **719** | **410** | **+310** | +| **partition** | **330** | **191** | **+139** | **256** | **125** | **+131** | +| txsize+txtype | 79 | 4 | +75 | 48 | 2 | +46 | +| **total** | **11374** | **7846** | **+3528** | **6060** | **3872** | **+2188** | + +The total gap is down from +3120 to +2188 on film B (the merged levers took +~930 B/frame off the top ARF), and its SHAPE flipped: mode+partition+mv +(+971 on film B, +1337 on film A) is now 44%/38% of the gap and the single +biggest mode-family carrier is syntax this encoder OFFERS and rav1e does not +spend at its ARF. + +### 2.2 What the mode family buys (per-table, B/frame, from the census's own table shares) + +| table | A ours | A rav1e | B ours | B rav1e | +|---|---|---|---|---| +| skip | 228.0 | 226.4 | 184.8 | 132.8 | +| intra_inter | 194.5 | 76.3 | 138.3 | 10.2 | +| comp_mode | 193.0 | 0 | 119.7 | 0 | +| y_mode | 184.9 | 58.4 | 78.2 | 2.5 | +| inter_compound_mode | 162.7 | 0 | 66.9 | 0 | +| uv_mode_cfl | 205.5 | ~0 | 24.7 | 0 | +| new_mv | 157.2 | 201.7 | 145.6 | 143.5 | +| motion_mode | 90.4 | 0 | 63.9 | 0 | +| drl_mode | — | — | 58.3 | 7.0 | +| ref_mv | — | 171.4 | 31.2 | 120.1 | +| segment_id | 0 | 118.4 | 0 | 70.7 | + +Three carriers, all ARF-specific: + +1. **The intra fallback poisons shared CDFs.** Ours codes 4.0% (film B) / + 15.0% (film A) of the ARF's AREA intra where rav1e codes 0.5% / 12.3%; + the per-block `intra_inter` symbol then costs ours 138 B/frame on film B + where rav1e pays 10 — the CDF is adapted to a 1-in-5 intra rate instead + of 1-in-200. The intra blocks' own mode syntax (y_mode, uv_mode_cfl, + filter_intra) rides on top. CAUTION: on film A even rav1e keeps 12.3% + intra area — there the intra fallback is CONTENT (grain), not waste. +2. **Compound at the ARF: ours 18.8%/23.8% of area, rav1e 0.0% on both + films.** The trio (comp_mode + inter_compound_mode + comp_ref_type) costs + ~200 B/frame (film B) to ~360 (film A). Never A/B'd at the ARF alone. +3. **The partition mix is 8x8-heavy**: ours 23.3%/31.3% of blocks are 8x8 + against rav1e's 13.1%/26.3%, and ours codes 1876/2782 blocks a frame + against 1507/2272 — every extra leaf pays its own skip + intra_inter + + ref + mode + mv symbols. Ours takes the 8x8 split at 2.4x rav1e's rate on + film B. + +NEWMV share of single-ref modes: ours ~45% both films, rav1e ~25% — the ++310/+454 mv column is block count plus longer mv residuals. + +## 3. The lever — `EC_AV1_ARFMODE` (default OFF = bit-exact) + +A per-frame offer-set change, TOP ARF ONLY (`DqLevel::TopArf` — the group's +own anchor, `arf_q_offset`), leaves only; the 32x32 whole keeps its full +offer set as the scene-cut hatch, and spec-forced 8x8s at straddling edges +are untouched. Bitmask, values OR-able: + +| value | arm | census target | +|---|---|---| +| 1 | ARF leaves (16x16/8x8) offer NO intra candidate: the leaf search runs its inter set only | intra_inter + y_mode + uv_mode_cfl + filter_intra, and the CDF de-poisoning across every block | +| 2 | 16x16 is the ARF's leaf floor: the RD-optional 8x8 split is not offered | partition + the three extra leaves' mode/mv syntax per split | +| 4 | ARF leaves offer NO compound candidates | comp_mode + inter_compound_mode + comp_ref_type (~200-360 B/frame) | +| 3 / 5 / 6 / 7 | combinations | — | + +`speed::ARFMODE: [u8; 11]` ships the gate's disposition per preset; +`EC_AV1_ARFMODE=` overrides for A/B on one build. OFF (0, the default) +must be byte-exact with the pre-lane encoder — pins `(150, 8291)`, +`(60, 33227)` hold at the default and at `EC_AV1_SPEED=6`. + +## 4. Witnesses BEFORE gates + +* Pins at the default AND `EC_AV1_SPEED=6` (OFF bit-exactness). +* Engagement counters (class `gate-blind-to-feature`): `ARFMODE_HITS` — + [0] ARF leaves coded with the intra trial skipped, [1] ARF 8x8 offers + refused, [2] ARF leaves with compound not offered — asserted > 0 on a real + film encode, and the armed stream must MOVE. +* Every witness stream decodes sample-exact through `decode_stream` AND + ffmpeg at the usual picture counts. + +## 5. Measurement ladder + +1. Local two-point probe (film A and film B, q150 + q90, bytes at equal PSNR) + prunes dead arms before any server time (arfpred's rule: the probe is a weak + proxy — it PRUNES, it does not PROMOTE; surviving arms go to the gate). +2. server-1 (`user@host-e`), managed skill `ec-av1-remote-gate-runners` + (repo-arfmode, target-arfmode, fixtures symlink, `.git` stub, PATH shim + inside the unit, chain as a user unit, linger on): `bd_rate_film_long_gop` + control first — must reproduce the standing table (film A +20.9/-9.0, + film B +70.4/-2.2) +-0.2/cell — then the surviving arms, one gate at a + time, LOADAVG before/after each. +3. `bd_rate_screen_native` (five rows) for the disposition arm — at 12 frames + `group_target` forms ONE group of 11 whose ARF IS a `TopArf`, so the + native gate ENGAGES this lever (no blindness claim; the arm runs). + +## 6. Keep rule and disposition + +One film row >=0.5 BD down on a column, the other film flat (±0.3), screen +not worse 0.3, wall <=+15%. Met -> default ON at the measured presets, pins +re-taken at the default and `EC_AV1_SPEED=6`, three-lane lib suite (fixtures +symlinked), `cargo check --workspace --all-targets -j4`. Not met -> default +OFF / revert, document. + +## 7. Risks + +* Arm 1 on film A: 15% intra area would flip to inter where even rav1e keeps + 12% — the residual cost may eat the syntax win on the grain film exactly. +* Arm 2: the 8x8 splits it refuses won RD; the syntax win (~280 B/frame + film B by the block arithmetic) has to beat the residual regression. +* Arm 4: the two-ref prediction win at the ARF is real (arf_altref exists + for it); only the syntax half is measured. +* The probe has mispredicted gate signs before (arfpred); nothing ships off + a probe alone. diff --git a/lanes/arfmode.report.md b/lanes/arfmode.report.md new file mode 100644 index 00000000..bb11139c --- /dev/null +++ b/lanes/arfmode.report.md @@ -0,0 +1,194 @@ +# lane-arfmode — the top ARF's mode/partition syntax: re-censused, lever formed, gated + +Worktree `edith_codecs-arfmode`, branch `lane-arfmode`, base `0d81d6c5`. +Charter: `lanes/arfmode.charter.md` (commit `f4da20b5`). The deferred lever +from the arffilt reconciliation — "the top ARF's mode/partition gap +(+871 B/frame, 2196 blocks vs rav1e's 1507)" — measured at ITS OWN base +first, because five merged encoder waves had moved the stream since +lane-arfpred wrote the number. + +## 1. Prior art — reconciled (charter §1) + +The residual half of the census was attacked by lane-arfpred/arftf/lookahead +(the temporal filter, strength 2, ±3 window, one-group lookahead) and +lane-tplfut/-tplhalf/-tplfut-arf (the tpl future map at the ARF, re-measured +ON at −1.0/−0.6 film B). `arf_altref` (lane-arfcen arm 2) gives the top ARF +its two distinct past anchors; leaf compound candidates shipped globally in +lanes av1comp3/4 — never A/B'd at the ARF alone. No ARF offer-set knob +existed; `EC_AV1_ARFMODE` is not a second convention beside anything. + +## 2. The fresh census at `0d81d6c5` + +The gate's own window and crops; ours `enc_probe gate 0 48 150`, rav1e +`speed 6 quantizer 110` cut from the 8-BIT yuv (a librav1e encode fed +straight from the 10-bit mkv produces a yuv420p10le OBU our reader scores at +2 dB against the 8-bit source — the 8-bit path reproduces arfpred's table to +the digit: rav1e top ARF 3872 B/frame, q104, PSNR-Y 45.97). + +### 2.1 Family decomposition, B/frame (ours: 6 ARFs @ q118; rav1e: 11 @ q104) + +| family | film A ours | film A rav1e | A Δ | film B ours | film B rav1e | B Δ | +|---|---|---|---|---|---|---| +| coeff | 6216 | 4568 | +1647 | 2938 | 1996 | +942 | +| literal | 1928 | 1459 | +469 | 959 | 730 | +229 | +| **mode** | **1845** | **1102** | **+744** | **1137** | **608** | **+530** | +| **mv** | **974** | **520** | **+454** | **719** | **410** | **+310** | +| **partition** | **330** | **191** | **+139** | **256** | **125** | **+131** | +| txsize+txtype | 79 | 4 | +75 | 48 | 2 | +46 | +| **total** | **11374** | **7846** | **+3528** | **6060** | **3872** | **+2188** | + +The total gap fell +3120 → +2188 on film B (the merged levers took ~930 +B/frame off the top ARF), and its SHAPE flipped: mode+partition+mv is now +44% (film B) / 38% (film A) of the gap — the syntax half the arffilt +reconciliation deferred is the bigger half now. + +### 2.2 What the mode family buys (per-table B/frame, census table shares) + +| table | A ours | A rav1e | B ours | B rav1e | +|---|---|---|---|---| +| skip | 228.0 | 226.4 | 184.8 | 132.8 | +| intra_inter | 194.5 | 76.3 | 138.3 | 10.2 | +| comp_mode | 193.0 | 0 | 119.7 | 0 | +| y_mode | 184.9 | 58.4 | 78.2 | 2.5 | +| inter_compound_mode | 162.7 | 0 | 66.9 | 0 | +| uv_mode_cfl | 205.5 | ~0 | 24.7 | 0 | +| new_mv | 157.2 | 201.7 | 145.6 | 143.5 | +| motion_mode | 90.4 | 0 | 63.9 | 0 | +| segment_id | 0 | 118.4 | 0 | 70.7 | + +Three carriers, all ARF-specific: (1) the intra fallback — ours codes +15.0%/4.0% of the ARF's AREA intra (A/B) against rav1e's 12.3%/0.5%, and the +per-block `intra_inter` symbol costs ours 138 B/frame where rav1e pays 10 on +film B: the shared CDF is adapted to a 1-in-5 intra rate instead of 1-in-200; +(2) compound at the ARF — ours 23.8%/18.8% of area, rav1e 0.0% on BOTH +films; (3) an 8x8-heavy partition mix — ours 31.3%/23.3% of blocks are 8x8 +against rav1e's 26.3%/13.1%, 1876/2782 blocks a frame against 1507/2272. + +## 3. The lever — `EC_AV1_ARFMODE` (default OFF = bit-exact) + +`speed::ARFMODE: [u8; 11]` (all 0), `EC_AV1_ARFMODE=` overrides on one +build; bitmask over the TOP ARF's offer set (`DqLevel::TopArf` only — the +mid/quarter hidden frames, the shown leaves and the flat path keep +everything): + +| bit | arm | +|---|---| +| 1 | no INTRA candidate at the top ARF — the whole-32 search's intra loop AND the 16x16/8x8 leaf search's intra trial | +| 2 | 16x16 is the leaf floor — the RD-optional 8x8 split is not offered | +| 4 | no compound candidates at the leaves | + +The spec-forced 8x8s at straddling 16x16s are a different site and stay; +OFF is bit-exact (pins below). + +### The probe's first correction, on the record + +The charter scoped arm 1 to the LEAVES and kept the whole-32's intra as "the +scene-cut hatch". The first real-content probe (film A, q150 AND q90) came +back byte-identical to the control: the top ARF's intra AREA lives +entirely in `search_inter_block`'s whole-32 intra loop, and the hatch was a +hatch nothing else was using. Arm 1 was widened to the whole-32 loop +(commit `f1bbf3a0`); the charter's deviation is this paragraph. + +## 4. Witnesses (BEFORE gates) + +| witness | result | +|---|---| +| `encode::tests::the_encoders_own_streams_are_byte_identical_to_their_pins` | PASS at the default AND `EC_AV1_SPEED=6` — pins `(150, 8291, 0x1f00bb0eb099a27f)`, `(60, 33227, 0x57ee6b1f8eacd881)` hold (OFF = bit-exact) | +| `encoder::tests::the_arf_leaf_offer_set_fires_moves_the_stream_and_decodes_sample_exact` | PASS — at 17 pictures/gop 32, q60, on a three-level luma-ramp fixture (the middle group's top ARF sits between its two anchors, where neither single-ref nor intra wins by default): explicit `ARFMODE=0` byte-identical to the default; modes 1/2/4 each fire their OWN engagement counter (580/113/568 hits) AND move the stream (5241 → 5194/5230/5238 B); coding order and levels unchanged; every armed stream decodes sample-exact through `decode_stream` AND ffmpeg | +| real-content engagement + stream-moves (the two-point probe) | film A arm 1: `arfmode hits: 94088` intra trials skipped at q150 (130584 at q90), stream fnv1a MOVES; arms 2/4 likewise (15407/81432 counts); film B all three arms fire and move — printed per point by `enc_probe`, never assumed | +| `encoder::tests::the_lookahead_holds_one_group_and_still_codes_every_picture` + `the_future_tpl_window_still_codes_every_picture_and_moves_the_stream` | PASS (adjacent-lever interference check: 2 passed) | + +(The fixture lesson, for the next lever of this shape: on `noisy_card` alone +NO arm can move anything — 516 intra trials skipped, zero won, stream +identical — because the noise quantizes to skip under both candidates. The +level ramp is what makes intra/compound competitive on synthetic content.) + +## 5. Two-point probe (film A/B, q150 + q90, bytes at the control's own PSNR) + +| arm | film A | film B | hits/point (A q150 / B q150) | +|---|---|---|---| +| 1 intra-off | −0.28% | **−2.61%** | 94088 / 73248 | +| 2 no-8x8 | **−2.05%** | **−2.49%** | 15407 / 11523 | +| 4 compound-off | −1.58% | −0.30% | 81432 / 63060 | + +Nothing dead, nothing promoted on a probe alone (arfpred's rule): the gate +arms are 2, 3 = 1|2, and 7 = 1|2|4 against the control. + +## 6. The deciding gates — server-1, `bd_rate_film_long_gop` (48 pictures) + +`user@host-e`, managed skill recipe: repo-arfmode, target-arfmode, +fixtures symlink, `.git` stub (re-fixed after the re-sync — see §9), +warm-compiled, one gate at a time, chain as a user unit, linger on. +LOADAVG before → after each gate from the chain log. + +| gate | film A | film B | wall A (ours) | wall B (ours) | +|---|---|---|---|---| +| control (`EC_AV1_ARFMODE` unset) | GATE-LGC-A | GATE-LGC-B | WALL-LGC-A | WALL-LGC-B | +| arm 2 | GATE-LGA2-A | GATE-LGA2-B | WALL-LGA2-A | WALL-LGA2-B | +| arm 3 = 1\|2 | GATE-LGA3-A | GATE-LGA3-B | WALL-LGA3-A | WALL-LGA3-B | +| arm 7 = 1\|2\|4 | GATE-LGA7-A | GATE-LGA7-B | WALL-LGA7-A | WALL-LGA7-B | + +### Control — measured, film A row in hand + +The chain (below) measured the control's film A row before this report was +written (the lane's request budget ran out mid-chain; the chain is a user +unit on the server with linger on, so it continues without this session): + +| gate | film A | wall A (ours) | +|---|---|---| +| control | **+20.9% / −9.0% — the standing table to the digit** | 1283.6 s | + +The control reproducing the standing table is the gate's precondition for +trusting arm deltas; it holds. + +### Arm gates — PENDING, chain running on server-1 + +`GATE lgc` started 03:20:46 UTC with `GATE lga2` → `lga3` → `lga7` behind +it, one gate at a time (~52 min each ≈ done by ~07:40 UTC). Read, do not +re-run: + + ssh user@host-e cat /home/user/gates/chain-arfmode.log # per-gate START/EXIT + LOADAVG pairs + ssh user@host-e grep -E '^\| (film|bars|screen)' /home/user/gates/arfmode-{lgc,lga2,lga3,lga7}.log + +Each log row is `film | ladder points | BD vs libaom | BD vs rav1e | ours:libaom:rav1e walls`. +The keep rule reads each arm against the control: one film row ≥0.5 BD +down, the other film flat ±0.3; screen rows not worse 0.3; wall ≤ +15%. + +## 7. Guard gate — `bd_rate_screen_native` (12 pictures, five rows) + +PENDING — run AFTER the long-GOP verdict picks the disposition arm, same +recipe, both arms (this lever ENGAGES at the native gate's shape: 12 frames +→ gop 12 → one group of 11 → its ARF is a `DqLevel::TopArf` — the arm is +NOT its control): + + env for control: (none) env for arm N: EC_AV1_ARFMODE=N + cargo test -p ec-av1 --release --lib -- --ignored --exact --nocapture \ + encode::tests::bd_rate_screen_native + +## 8. Keep rule and disposition + +**Default OFF — unchanged, byte-exact.** The gate evidence is still +accumulating (chain above), and the ON disposition requires it; until a +reader applies §6's rule to the finished logs and re-runs the native pair, +`speed::ARFMODE` ships `[0; 11]`, the pins hold at `(150, 8291)` / +`(60, 33227)`, and `EC_AV1_ARFMODE=` arms the lever for A/B on one +build. fix-now | deferred() — every other acceptance item (prior-art +reconciliation, gap decomposition, lever, witnesses with printed non-zero +counts, probe, control reproduction) is done and committed. + +## 9. What this lane did NOT do + +* `deferred: the compound trio at the ARF alone (arm 4): the probe reads + film A −1.58% / film B −0.30% — worth a gate if a later lane can afford + one; the gate's arm 7 carries it only in combination.` +* `deferred: motion_mode/warp syntax at the ARF (64-90 B/frame, rav1e 0) — + untouched; warp is a shipped default and an ARF-only cut is a fresh + measurement.` +* `deferred: segmentation (rav1e pays 70-118 B/frame of segment_id at its + ARF to buy cheap skip) — ours codes no segments at all; a whole lane.` +* `deferred: NEW-mv share at the ARF (ours ~45% vs rav1e ~26%) — driven by + the lag-8 anchor pair; the mv column (+310/+454) is block count plus + longer residuals, not a syntax lever.` + +No merge, no push.