diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 994dbf34..6e89746e 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -386,10 +386,16 @@ fn split_inter_blocks() -> bool { /// 32x32 or smaller, which put our mode+mv+partition+tx_size spend at 162,669 /// bits against rav1e's 61,988, about 46% of the film B gap. /// -/// SKIP-only: a 64x64 residual needs a forward TX_64X64 the encoder does not -/// have (`transform.rs` tops out at 32x32), and the writer refuses any other -/// shape (`crate::tile::Quadrant::Whole64`). Switched off by `EC_AV1_B64=0` -/// in any build. +/// lane-tx64: no longer skip-only. The root also prices a REAL residual -- +/// one TX_64X64 luma transform (`transform.rs`'s forward network has always +/// reached 64 points; what was missing was the block coder and the writer +/// around it) and one TX_32X32 per chroma plane -- see [`b64_residual`], +/// which carries that arm's own gate table. Still refused at the 64 root: +/// compound references, a depth-1 4x32x32 var-tx tree, intra, and a +/// superblock the true frame edge cuts through. +/// +/// Switched off by `EC_AV1_B64=0` in any build; `EC_AV1_B64RES=0` keeps the +/// root and restores the skip-only shape lane-b64 shipped. pub(crate) const B64_ROOT: bool = true; /// How many superblocks took the 64x64 root since the last @@ -410,6 +416,57 @@ fn b64_root() -> bool { ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::B64_ROOT)) } +/// Whether the 64x64 root also prices a NON-skip candidate (lane-tx64): the +/// winner's prediction through one TX_64X64 luma transform and two TX_32X32 +/// chroma ones. Off by `EC_AV1_B64RES=0`, which restores the skip-only root +/// lane-b64 shipped, so the two can be measured against each other on the +/// same build. +/// +/// MEASURED on the native gate (two arms of one build, baseline reproduced to +/// the tenth): both film rows improve on both columns -- film A +/// +43.0/+15.0 -> +42.9/+14.8, film B +64.7/+34.0 -> +63.3/+31.5 -- and +/// SCREEN CAPTURE loses +33.4/-23.8 -> +34.3/-23.4, outside the keep rule's +/// +-0.3 screen clause. So the caller gates this off on a screen frame +/// (`allow_screen_content_tools`), the same content gate the coding pyramid +/// takes: a desktop capture's 64x64 superblocks are flat runs whose skip arm +/// already codes them for nothing, and a residual there only spends bits the +/// intra/palette tools would have spent better. +/// +/// CENSUS on film B (12 frames, q120, `EC_AV1_BITCENSUS=1` + +/// `examples/syntax_census.rs`, both streams read by OUR decoder), off vs on: +/// +/// | | skip-only | + residual | +/// |---|---|---| +/// | 64x64 share, all frames | 36.4% | 38.3% | +/// | 64x64 share, leaf frames | 53.3% | 56.0% | +/// | skip area, all frames | 80.4% | 78.8% | +/// | skip area, leaf frames | 90.2% | 88.5% | +/// | stream bytes | 34,366 | 34,295 | +/// | encoder wall | 13.4s | 9.6s | +/// +/// `base_luma_64` enters the leaf frames' top tables at 5.7%, i.e. the +/// TX_64X64 coefficients are really being coded. The root wins MORE often +/// with a residual to offer (rav1e speed 6's own 64x64 share on this clip is +/// 45.2%), which is also why the arm is 28% FASTER despite costing three more +/// transforms per superblock: a root that wins is four quadrant searches that +/// never run. +fn b64_residual() -> bool { + static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + crate::envflags::var("EC_AV1_B64RES").ok().map(|v| v != "0") + }); + ENV.unwrap_or(true) && b64_root() +} + +/// How many 64x64 roots came out cheaper with a residual than skipped, since +/// the last [`take_b64_residual_hits`] -- so a gate reports the fire rate +/// rather than assuming one (class `gate-blind-to-feature`). +static B64_RESIDUAL_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// The non-skip 64x64-root count since the last call, and zero it. +pub fn take_b64_residual_hits() -> usize { + B64_RESIDUAL_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + /// Whether the encoder searches loop restoration at all (spec 7.17): one /// Wiener filter per 64x64 luma restoration unit, picked from /// [`crate::filter_search::WIENER_CANDIDATES`]. Swept by `EC_AV1_LR` in a @@ -476,10 +533,29 @@ fn split_rd_threshold() -> f64 { /// Whether `cost` over a `side` x `side` block is cheap enough per pixel that /// [`split_rd_threshold`] withholds the split trial. fn split_rd_breakout(cost: f64, side: usize, lambda: f64) -> bool { - let t = split_rd_threshold(); + split_rd_breakout_at(split_rd_threshold(), cost, side, lambda) +} + +/// [`split_rd_breakout`] at an explicit threshold -- what the 64x64 root's own +/// early-out reads. +fn split_rd_breakout_at(t: f64, cost: f64, side: usize, lambda: f64) -> bool { t > 0.0 && cost < t * lambda * (side * side) as f64 } +/// The threshold the 64x64 root's early-out uses: [`split_rd_threshold`] +/// CLAMPED at the shipped [`SPLIT_RD_THRESHOLD`] (lane-tx64). +/// +/// The 32x32 level is allowed to loosen its breakout with the speed preset +/// (`crate::speed::SPLIT_RD` steps to 0.5 at preset 6), but lane-b64 measured +/// what that does one size up: at 0.5 a whole superblock is taken at 64x64 +/// without its four quadrants ever being searched, and film B goes +/// +91.5/+53.4 -> +114.6/+71.5. A 64x64 block is four times the area, so the +/// same per-pixel threshold withholds four times as much search -- the +/// preset's step is priced for the 32 level and does not carry up here. +fn b64_breakout_threshold() -> f64 { + split_rd_threshold().min(SPLIT_RD_THRESHOLD) +} + /// [`SPLIT_BREAKOUT_COEFFS`], swept by `EC_AV1_SPLIT_BREAKOUT` in a test /// build. fn split_breakout_coeffs() -> usize { @@ -1829,8 +1905,20 @@ impl Plane<'_> { bits: 0.0, }; } - let mut residual = [0i32; BLOCK * BLOCK]; - let residual = &mut residual[..side * side]; + // A 64x64 root's residual is four times the scratch every other + // block coder here sizes for [`BLOCK`]; only that one case pays for + // a heap buffer, so the 4x4..32x32 path keeps its stack array. + let mut small = [0i32; BLOCK * BLOCK]; + let mut big = if side > BLOCK { + vec![0i32; side * side] + } else { + Vec::new() + }; + let residual: &mut [i32] = if side > BLOCK { + &mut big + } else { + &mut small[..side * side] + }; for row in 0..side { for col in 0..side { residual[row * side + col] = @@ -1864,6 +1952,16 @@ impl Plane<'_> { .collect() }; let sse = self.block_sse(x, y, side, &reconstruction); + // A 64-point transform codes only its top-left 32x32 corner (spec + // 5.11.39's zero-out; `TxbSet::Luma64`'s tables are 32-sided and the + // key frame writer takes a 32x32 level grid for one). The inverse + // above reads the whole 64x64 array, but the price below and the + // levels the writer takes are that corner. + let levels = if side > BLOCK { + coded_corner(&levels, side, BLOCK) + } else { + levels + }; #[cfg(test)] let t = stage_start(); let bits = if cfg!(test) && std::env::var_os("EC_AV1_ESTIMATE").is_some() { @@ -5657,6 +5755,17 @@ struct Trial { bits: f64, } +/// The top-left `to` x `to` corner of a `from`-sided level grid -- the part +/// of a 64-point transform that is actually coded (everything outside it is +/// zeroed by [`crate::transform::quantize`] already, so this only reshapes). +fn coded_corner(levels: &[i32], from: usize, to: usize) -> Vec { + let mut out = vec![0i32; to * to]; + for row in 0..to { + out[row * to..][..to].copy_from_slice(&levels[row * from..][..to]); + } + out +} + /// The non-zero levels of a block, as the tile writer takes them. fn coeffs(levels: &[i32], side: usize) -> Vec { levels @@ -7214,6 +7323,9 @@ fn search_skip_64( chroma: &mut [Plane; 2], (x, y): (usize, usize), search: &Search, + // Whether the non-skip arm is priced at all ([`b64_residual`], and off on + // screen content -- see that function's doc). + residual: bool, reference: &Picture, stack: &MvStack, fctx: &crate::decode::FrameCtx, @@ -7282,7 +7394,7 @@ fn search_skip_64( } } - let mut best: Option<(f64, InterInfo, [Trial; 3])> = None; + let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; for (bits, info) in cands { let trials = [ (0usize, x, y, side, true), @@ -7326,18 +7438,66 @@ fn search_skip_64( + trials[2].sse + search.lambda * (fixed + bits); if best.as_ref().is_none_or(|b| cost < b.0) { - best = Some((cost, info, trials)); + best = Some((cost, bits, info, trials)); + } + } + let (skip_cost, bits, info, skip_trials) = best?; + + // The winner's prediction coded with a REAL residual (lane-tx64): one + // TX_64X64 luma transform -- `TxbSet::Luma64`, whose coded quarter is the + // 32x32 corner `code_from_prediction` hands back -- and one TX_32X32 per + // chroma plane, since a 64x64 root's chroma block IS 32x32. Only the mv + // the skip arm already chose is re-coded: the prediction with the lowest + // SSE is also the cheapest residual to code, and a second 64x64 motion + // search per superblock is the wall this root was taken to SAVE. + // + // Priced against the skip arm through the same `symbol_bits` path, so the + // two numbers are comparable: `fixed` charged `skip` coded 1, and this + // arm charges 0 instead and adds its three planes' coefficient bits. + let mut chosen = (skip_cost, true, skip_trials); + if residual { + let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; + let residual_trials = [0usize, 1, 2].map(|plane| { + let (target, px, py, s) = if plane == 0 { + (&*luma, x, y, side) + } else { + (&chroma[plane - 1], x / 2, y / 2, side / 2) + }; + target.code_from_prediction( + px, + py, + s, + &chosen.2[plane].reconstruction, + false, + search.base_q_idx, + search.deadzone, + sets[plane], + ) + }); + let unskip = symbol_bits(&cdf::SKIP[0], 0) - symbol_bits(&cdf::SKIP[0], 1); + let coeff_bits: f64 = residual_trials.iter().map(|t| t.bits).sum(); + let cost = residual_trials.iter().map(|t| t.sse).sum::() + + search.lambda * (fixed + bits + unskip + coeff_bits); + if cost < skip_cost { + B64_RESIDUAL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + chosen = (cost, false, residual_trials); } } - let (cost, info, trials) = best?; + + let (cost, skip, trials) = chosen; luma.commit(x, y, side, &trials[0]); chroma[0].commit(x / 2, y / 2, side / 2, &trials[1]); chroma[1].commit(x / 2, y / 2, side / 2, &trials[2]); Some(( cost, BlockCoeffs { - skip: true, + skip, inter: Some(info), + // Both grids are 32-sided: the luma one is the 64-point + // transform's coded corner, the chroma ones are whole TX_32X32. + luma: if skip { Vec::new() } else { coeffs(&trials[0].levels, BLOCK) }, + u: if skip { Vec::new() } else { coeffs(&trials[1].levels, side / 2) }, + v: if skip { Vec::new() } else { coeffs(&trials[2].levels, side / 2) }, ..BlockCoeffs::default() }, )) @@ -9027,6 +9187,7 @@ pub(crate) fn encode_inter_frame( &mut chroma, (x64, y64), &sb_search, + b64_residual() && !screen, reference, &stack, fctx, @@ -9042,9 +9203,14 @@ pub(crate) fn encode_inter_frame( // pixel is taken outright and its quadrants are never searched -- // the same `split_rd_breakout` shape the 32x32 level uses one // size up, and where this lever BUYS wall instead of spending it. - let early64 = sb64 - .as_ref() - .is_some_and(|(c, _, _)| split_rd_breakout(*c, SUPERBLOCK, sb_search.lambda)); + let early64 = sb64.as_ref().is_some_and(|(c, _, _)| { + split_rd_breakout_at( + b64_breakout_threshold(), + *c, + SUPERBLOCK, + sb_search.lambda, + ) + }); for quadrant in 0..if early64 { 0 } else { 4 } { let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); // lane-av1tpl2: this 32x32 block's own lambda -- every RD @@ -13348,14 +13514,14 @@ mod tests { // the blocks that take one code a filter-intra mode, a different // prediction and different coefficients -- 7321 -> 7373 at q=150 and // 27285 -> 27074 at q=60. `EC_AV1_FILTER_INTRA=0` restores these. - // Re-taken on lane-b64 (64x64 skip root, `EC_AV1_B64=0` restores - // 8076 / 27585 under the 16:-24:16 pyramid) and again at the lane-pyr4 - // merge: the default pyramid is `8:-32:16` now, the long-GOP sweep's - // shape (`lanes/pyr4.sweep.txt`), so these four pictures code as one - // truncated group of 8 with its hidden ALTREF at `q-32`. - // `EC_AV1_PYRAMID=16:-24:16` restores 7291 / 27466. + // Re-taken on lane-b64 (64x64 skip root), at the lane-pyr4 merge + // (default pyramid `8:-32:16`, one truncated group of 8 with its + // hidden ALTREF at `q-32`), and at the lane-tx64 merge: the 64x64 + // root can code a REAL residual now (one TX_64X64 luma transform and + // two TX_32X32 chroma ones). `EC_AV1_B64RES=0` restores 8446 / 28548; + // `EC_AV1_B64=0` gives the pre-root bytes under this pyramid. let pins: [(u8, usize, u64); 2] = - [(150, 8446, 0xaf24_d4bc_c9a3_9819), (60, 28548, 0x1459_61a1_f2b6_0f05)]; + [(150, 8590, 0x407d_4c27_d24f_d830), (60, 28535, 0xf033_c999_93a3_5b27)]; for (q, bytes, hash) in pins { let encoded = encode_sequence(&source, q, 0.5).unwrap(); assert_eq!( diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 36b81f83..69a6f898 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -2413,6 +2413,75 @@ mod tests { } } + /// The same clip with a DC step between frames (lane-tx64): the 64x64 + /// root still predicts the whole superblock from the last frame, but the + /// prediction is uniformly off, so the root's NON-skip arm -- one + /// TX_64X64 luma transform and two TX_32X32 chroma ones -- prices below + /// its skip arm and a 64x64 residual is really written. The counter is + /// the gate (class `gate-blind-to-feature`; the static clip above cannot + /// reach this arm at all because its prediction is exact), and both + /// decoders have to reconstruct those coefficients sample-exact -- a + /// writer that names the wrong coefficient set or the wrong `txfm_split` + /// desyncs the whole tile here. + #[test] + fn a_dc_stepped_clip_codes_a_64x64_residual_and_decodes_sample_exact() { + let _gate_lock = crate::stream::tests::lock_gate_counters(); + let (width, height) = (256usize, 128usize); + let still = test_card(width, height, 0); + let sources: Vec = (0..3) + .map(|t| { + let mut p = still.clone(); + let step = (t * 6) as u16; + for v in &mut p.y { + *v = (*v + step).min(255); + } + p + }) + .collect(); + let config = EncoderConfig { + width, + height, + base_q_idx: 120, + gop: 8, + colour: Colour::Bt709Limited, + tile_cols_log2: 0, + tile_rows_log2: 0, + }; + let mut enc = Av1Encoder::new(config).unwrap(); + let mut stream = Vec::new(); + let _ = crate::encode::take_b64_residual_hits(); + for packet in encode_all(&mut enc, &sources) { + stream.extend_from_slice(&packet.data); + } + let residuals = crate::encode::take_b64_residual_hits(); + assert!( + residuals > 0, + "a DC-stepped clip coded no 64x64 root with a residual at all" + ); + eprintln!("64x64 roots with a residual: {residuals} of 16 inter superblocks"); + + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); + if !have_ffmpeg() { + eprintln!("SKIP the ffmpeg half: no ffmpeg"); + return; + } + let theirs = ffmpeg_decode_luma(&stream, width, height); + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { + let got: Vec = a.y.iter().map(|&v| v as u8).collect(); + if let Some(at) = got.iter().zip(b).position(|(x, y)| x != y) { + panic!( + "frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", + at % width, + at / width, + got[at], + b[at], + ); + } + } + } + #[test] fn every_tile_layout_decodes_sample_exact_through_both_decoders() { let _knobs = crate::speed::knob_read(); diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index 8040c7a2..e772520f 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -149,27 +149,25 @@ pub(crate) fn at(table: &[T; 11]) -> T { /// film B +70.5/+35.2 -> +64.7/+34.0, screen +48.2/-16.4 -> +33.4/-23.8) at /// -4% to -20% wall on four of the five rows. /// -/// MEASURED OFF from preset 4 up (lane-b64, native gate at `EC_AV1_SPEED=6`, -/// two arms side by side): ON costs film A +55.1/+24.8 -> +64.5/+37.5 and -/// film B +91.5/+53.4 -> +114.6/+71.5 for -16%/-36% wall. The cause is the -/// trial's own early-out, which rides [`SPLIT_RD`]: at 0.5 (preset 6) a -/// superblock is taken whole without its quadrants ever being searched, and -/// film B's q150 point loses 1.2 dB. Presets 1..=3 keep it because they keep -/// `SPLIT_RD` at 0.125, i.e. the early-out they get is the measured one; -/// 4 and up switch it off with `SPLIT_RD`'s first step. -pub(crate) const B64_ROOT: [bool; 11] = [ - crate::encode::B64_ROOT, - true, - true, - true, - false, - false, - false, - false, - false, - false, - false, -]; +/// lane-b64 MEASURED it off from preset 4 up: at `EC_AV1_SPEED=6` ON cost +/// film A +55.1/+24.8 -> +64.5/+37.5 and film B +91.5/+53.4 -> +114.6/+71.5. +/// The cause was named there and FIXED in lane-tx64: the trial's early-out +/// rode [`SPLIT_RD`], so at 0.5 (preset 6) a superblock was taken whole with +/// its quadrants never searched. `encode::b64_breakout_threshold` clamps that +/// early-out at the shipped 0.125 whatever the preset, and the same gate at +/// `EC_AV1_SPEED=6` now reads, every row better on both columns: +/// +/// | row | B64 off | B64 on, clamped | +/// |---|---|---| +/// | bars 1080p | +88.9 / +54.8 | +79.5 / +47.2 | +/// | bars 2160p | +91.6 / +49.8 | +72.6 / +35.1 | +/// | film A | +55.1 / +24.8 | +51.0 / +21.7 | +/// | film B | +91.5 / +53.4 | +77.9 / +44.3 | +/// | screen | +57.7 / -11.7 | +40.3 / -20.0 | +/// +/// at 28.1s vs 30.8s (film A), 31.3s vs 28.0s (film B), 14.3s vs 15.2s +/// (screen). So it is on at every preset. +pub(crate) const B64_ROOT: [bool; 11] = [crate::encode::B64_ROOT; 11]; /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split /// trial is withheld. The single biggest wall lever in the tile search. diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 00fe1130..0bd17b4c 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1770,9 +1770,10 @@ pub enum Quadrant { /// and the other three carry [`Covered`](Quadrant::Covered), so the /// per-32x32-entry shape the inter writer takes is unchanged. /// - /// The block is coded SKIP and single-reference: a 64x64 residual needs - /// the forward TX_64X64 the encoder does not have (`transform.rs` tops - /// out at 32x32), and the writer refuses anything else. + /// The block is single-reference; the writer refuses a compound one. It + /// may be skipped or carry a real residual -- one TX_64X64 luma transform + /// whose coded quarter is a 32x32 level grid, and one TX_32X32 per chroma + /// plane (lane-tx64, `crate::encode::b64_residual`). Whole64(BlockCoeffs), /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's /// top-left already coded; it carries no block of its own. @@ -4365,9 +4366,24 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { let side = match q { Quadrant::Whole(_) => 32, Quadrant::Split(_) => 16, - // A 64x64 root is skip-only, so it carries no coefficient - // bits to predict at all (`Quadrant::Whole64`). - Quadrant::Whole64(_) | Quadrant::Covered => return 0.0, + // lane-tx64: a 64x64 root that codes a residual pays for one + // TX_64X64 luma transform (only the top-left 32x32 carries + // coefficients) and one TX_32X32 per chroma plane -- the same + // three prices `predicted_coeff_bits_sb` takes for a key + // frame's whole superblock, and the same sets the writer's + // `Whole64` arm codes. `block_bits`'s table has no 64 row, so + // it is spelled out here. + Quadrant::Whole64(block) => { + return match block.skip { + true => 0.0, + false => { + coeff_bits(&dense(&block.luma, 32), TxbSet::Luma64, q_ctx, 0, 0) + + coeff_bits(&dense(&block.u, 32), TxbSet::Chroma32, q_ctx, 0, 0) + + coeff_bits(&dense(&block.v, 32), TxbSet::Chroma32, q_ctx, 0, 0) + } + }; + } + Quadrant::Covered => return 0.0, }; q.blocks().iter().map(|b| block_bits(b, side, q_ctx)).sum::() }) @@ -5558,6 +5574,12 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( // derived from luma's, not coded, so it never differs between the two. let intra_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; let inter_planes = [TxbSet::Luma32Inter, TxbSet::Chroma16, TxbSet::Chroma16]; + // A 64x64 root's planes (lane-tx64): TX_64X64 luma -- DCT-only at this + // size for an inter block too (spec `av1_get_ext_tx_set_type` returns + // `TX_SET_DCTONLY` for `tx_size_sqr_up == TX_64X64`), so `Luma64` carries + // no `tx_type` symbol and there is no `Luma64Inter` to write -- and one + // TX_32X32 per chroma plane. + let sb64_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; let scan32 = default_scan(TX32); let scan16 = default_scan(TX16); let scan8 = default_scan(TX8); @@ -5593,8 +5615,9 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( ); // lane-b64: a superblock the search left WHOLE — one 64x64 block // at `PARTITION_NONE`, carried by its top-left quadrant's entry - // ([`Quadrant::Whole64`]). Coded skip and single-reference: no - // residual, so no TX_64X64 coefficients and no `cdef_idx`. + // ([`Quadrant::Whole64`]). Single-reference; skipped, or with a + // real residual through `TxbSet::Luma64` + two `Chroma32` + // (lane-tx64). let sb64 = ((sb_r * 2) < rows && (sb_c * 2) < cols) .then(|| &blocks[((sb_r * 2) * cols + sb_c * 2) as usize]) .and_then(|q| match q { @@ -5611,11 +5634,10 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( let info = block.inter.ok_or_else(|| { Error::unsupported("AV1 tile", "a 64x64 root block is coded inter") })?; - if !block.skip || info.ref1.is_some() { + if info.ref1.is_some() { return Err(Error::unsupported( "AV1 tile", - "a 64x64 root block is coded skip and single-reference \ - (a 64x64 residual needs a forward TX_64X64)", + "a 64x64 root block is coded single-reference", )); } enc.symbol(PARTITION_NONE, &mut cdfs.partition_w64[sb_ctx]); @@ -5624,8 +5646,8 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( let has_left = neighbours.has_left(mi_c); let skip_ctx = usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]); - enc.symbol(1, &mut cdfs.skip[skip_ctx]); - write_cdef_idx(&mut enc, (mi_r, mi_c), true); + enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); + write_cdef_idx(&mut enc, (mi_r, mi_c), block.skip); let ii_ctx = intra_inter_ctx( has_above, has_left, @@ -5694,8 +5716,11 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( )?; if tx_select { // A skipped inter block writes no `txfm_split` at all -- - // this only publishes the max transform size the reader - // infers for it (`write_tx_syntax_inter`). + // that call only publishes the max transform size the + // reader infers for it; an unskipped one writes the + // `txfm_split` flag, 0 here because a 64x64 root's luma + // is one whole TX_64X64 (depth 1 is lane-tx64's deferred + // var-tx arm). write_tx_syntax_inter( &mut enc, &mut cdfs, @@ -5703,12 +5728,37 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( (mi_r, mi_c), SB, true, - true, + block.skip, 0, ); } - neighbours.record(sb_at, SB, 0, &zero_grids); - neighbours.record_inter(sb_at, SB, true, true, block_ref(block)); + if block.skip { + neighbours.record(sb_at, SB, 0, &zero_grids); + } else { + // lane-tx64: the 64x64 root's real residual. Luma is one + // TX_64X64 whose coded quarter is a 32x32 level grid + // (`TxbSet::Luma64`, `TX32`'s own scan -- the same pair + // `sb_coeff_key_frame_tile`'s `Superblock::Whole` writes), + // chroma one whole TX_32X32 per plane, and `split` is + // false because the luma transform covers the block. + let grids = [ + level_grid(&block.luma, TX32)?, + level_grid(&block.u, TX32)?, + level_grid(&block.v, TX32)?, + ]; + write_block_planes( + &mut enc, + &mut cdfs, + &sb64_planes, + &grids, + &[&scan32, &scan32, &scan32], + &neighbours.around(sb_at, SB), + 0, + false, + ); + neighbours.record_planes(sb_at, SB, 0, &grids, true); + } + neighbours.record_inter(sb_at, SB, block.skip, true, block_ref(block)); continue; } match (has_cols, has_rows) {