forge
Explore
Log in

Don't you dare go Hollow

The writer records a rectangular block's two neighbour bands, and codes a 128-root HORZ/VERT half

Browse files at this commit

b52bd176bb79806982546274e6e627649966b95fAhmet Kaan GümüşParent: 53e1991
  • feat
    • the writer records a rectangular block's two neighbour bands, and codes a 128-root HORZ/VERT half
Show full message
feat(av1): the writer records a rectangular block's two neighbour bands, and codes a 128-root HORZ/VERT half

The tile writer had no rect path at all: record/record_inter/record_planes/
record_txfm and the palette bands each took one side and stamped it into both
the above and the left band. Each now has an (above = width, left = height)
twin, the square call delegating to it, and write_inter_block_128_rect codes
one 128x64 / 64x128 half at the root's own square CDF context, skip only.
Quadrant::Rect128 carries the pair. The search side is unchanged, so the pins
are byte-identical.
crates/ec-av1/src/encode.rsmodified+68 -42
@@ -3079,6 +3079,17 @@ impl Plane<'_> {
30793079 rows_of(self.source, self.width, x, y, side)
30803080 }
30813081
3082+ /// lane-b128hv: [`Self::source_block`] over a rectangle, for the 128
3083+ /// root's own HORZ/VERT motion search.
3084+ fn source_block_rect(&self, x: usize, y: usize, w: usize, h: usize) -> Vec<u8> {
3085+ let mut out = vec![0u8; w * h];
3086+ for row in 0..h {
3087+ out[row * w..][..w]
3088+ .copy_from_slice(&self.source[(y + row) * self.width + x..][..w]);
3089+ }
3090+ out
3091+ }
3092+
30823093 /// The reconstructed samples of one square, so that a partition trial can
30833094 /// be undone.
30843095 /// The reconstructed samples of one square AND the coefficient contexts
@@ -9549,7 +9560,7 @@ fn search_root_128(
95499560 luma,
95509561 chroma,
95519562 (x128, y128),
9552- SIDE,
9563+ (SIDE, SIDE),
95539564 &root_search,
95549565 false,
95559566 reference,
@@ -9662,12 +9673,14 @@ fn search_skip_64(
96629673 luma: &mut Plane,
96639674 chroma: &mut [Plane; 2],
96649675 (x, y): (usize, usize),
9665- // The root's side in samples: [`SUPERBLOCK`] at a 64 root, twice that at
9666- // the 128 one [`search_root_128`] drives (lane-b128). Every trial below
9667- // is a square of this side and its half in chroma, and the residual arm
9668- // is only ever asked for at 64 -- a 128 block's residual is four
9676+ // The root's footprint in samples: [`SUPERBLOCK`] square at a 64 root,
9677+ // twice that at the 128 one [`search_root_128`] drives (lane-b128), and
9678+ // 128x64 / 64x128 at the rect halves of [`search_root_128_rect`]
9679+ // (lane-b128hv). Every trial below tiles it into squares of its SHORTER
9680+ // side (one square when it is square, two otherwise), and the residual
9681+ // arm is only ever asked for at 64 -- a 128 block's residual is four
96699682 // TX_64X64 units the writer has no path for.
9670- side: usize,
9683+ (bw, bh): (usize, usize),
96719684 search: &Search,
96729685 // Whether the non-skip arm is priced at all ([`b64_residual`], and off on
96739686 // screen content -- see that function's doc).
@@ -9680,7 +9693,23 @@ fn search_skip_64(
96809693 compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)],
96819694 fctx: &crate::decode::FrameCtx,
96829695 ) -> Option<(f64, BlockCoeffs)> {
9683- debug_assert!(side == SUPERBLOCK || !residual, "a 128 root has no residual arm");
9696+ debug_assert!(
9697+ (bw, bh) == (SUPERBLOCK, SUPERBLOCK) || !residual,
9698+ "only a 64 root has a residual arm"
9699+ );
9700+ // The square units every trial below is taken on: the block itself when
9701+ // it is square, its two halves otherwise. Luma first (in raster order),
9702+ // then each chroma plane over the same tiling at half the side -- which is
9703+ // exactly `[(0, x, y, side), (1, ..), (2, ..)]` at bw == bh.
9704+ let sq = bw.min(bh);
9705+ let units: Vec<(usize, usize, usize, usize, bool)> = (0..3usize)
9706+ .flat_map(|plane| {
9707+ let (ox, oy, s) = if plane == 0 { (x, y, sq) } else { (x / 2, y / 2, sq / 2) };
9708+ (0..bh / sq).flat_map(move |j| {
9709+ (0..bw / sq).map(move |i| (plane, ox + i * s, oy + j * s, s, plane == 0))
9710+ })
9711+ })
9712+ .collect();
96849713 let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1);
96859714 let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1);
96869715 let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack);
@@ -9718,7 +9747,7 @@ fn search_skip_64(
97189747 // candidate below to pair with the second reference's NEAREST half.
97199748 let mut last_new_mv: Option<(i32, i32)> = None;
97209749 if leaf_new_mv() {
9721- let source_block = luma.source_block(x, y, side);
9750+ let source_block = luma.source_block_rect(x, y, bw, bh);
97229751 let (seeds, seed_n) = mv_seeds(stack);
97239752 let found = motion::search(
97249753 &reference.y,
@@ -9728,8 +9757,8 @@ fn search_skip_64(
97289757 &source_block,
97299758 x,
97309759 y,
9731- side,
9732- side,
9760+ bw,
9761+ bh,
97339762 stack.pred_mv,
97349763 &seeds[..seed_n],
97359764 search.lambda,
@@ -9746,14 +9775,11 @@ fn search_skip_64(
97469775 }
97479776 }
97489777
9749- let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None;
9778+ let mut best: Option<(f64, f64, InterInfo, Vec<Trial>)> = None;
97509779 for (bits, info) in cands {
9751- let trials = [
9752- (0usize, x, y, side, true),
9753- (1, x / 2, y / 2, side / 2, false),
9754- (2, x / 2, y / 2, side / 2, false),
9755- ]
9756- .map(|(plane, px, py, s, is_luma)| {
9780+ let trials: Vec<Trial> = units
9781+ .iter()
9782+ .map(|&(plane, px, py, s, is_luma)| {
97579783 let (samples, stride, w, h) = match plane {
97589784 0 => (&reference.y, reference.width, luma.true_width, luma.true_height),
97599785 1 => (
@@ -9784,11 +9810,10 @@ fn search_skip_64(
97849810 search.deadzone,
97859811 TxbSet::Luma32Inter,
97869812 )
9787- });
9788- let cost = trials[0].sse
9789- + trials[1].sse
9790- + trials[2].sse
9791- + search.lambda * (fixed + bits);
9813+ })
9814+ .collect();
9815+ let cost =
9816+ trials.iter().map(|t| t.sse).sum::<f64>() + search.lambda * (fixed + bits);
97929817 if best.as_ref().is_none_or(|b| cost < b.0) {
97939818 best = Some((cost, fixed + bits, info, trials));
97949819 }
@@ -9847,12 +9872,9 @@ fn search_skip_64(
98479872 ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1)));
98489873 ccands.dedup_by_key(|c| c.0);
98499874 for (mvs, bits, info) in ccands {
9850- let trials = [
9851- (0usize, x, y, side, true),
9852- (1, x / 2, y / 2, side / 2, false),
9853- (2, x / 2, y / 2, side / 2, false),
9854- ]
9855- .map(|(plane, px, py, s, is_luma)| {
9875+ let trials: Vec<Trial> = units
9876+ .iter()
9877+ .map(|&(plane, px, py, s, is_luma)| {
98569878 let (r0, r1) = match plane {
98579879 0 => (
98589880 (reference.y.as_slice(), reference.width, luma.true_width, luma.true_height),
@@ -9890,10 +9912,10 @@ fn search_skip_64(
98909912 search.deadzone,
98919913 TxbSet::Luma32Inter,
98929914 )
9893- });
9915+ })
9916+ .collect();
98949917 let syntax = fixed - ref_bits + bits;
9895- let cost =
9896- trials[0].sse + trials[1].sse + trials[2].sse + search.lambda * syntax;
9918+ let cost = trials.iter().map(|t| t.sse).sum::<f64>() + search.lambda * syntax;
98979919 if best.as_ref().is_none_or(|b| cost < b.0) {
98989920 best = Some((cost, syntax, info, trials));
98999921 }
@@ -9923,11 +9945,13 @@ fn search_skip_64(
99239945 let mut luma_levels: Option<(Vec<i32>, u8, Vec<TxType>)> = None;
99249946 if residual {
99259947 let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32];
9948+ // Square by construction (the `debug_assert` above), so `units` is
9949+ // one entry per plane and `chosen.2[plane]` is that plane's trial.
99269950 let residual_trials = [0usize, 1, 2].map(|plane| {
99279951 let (target, px, py, s) = if plane == 0 {
9928- (&*luma, x, y, side)
9952+ (&*luma, x, y, bw)
99299953 } else {
9930- (&chroma[plane - 1], x / 2, y / 2, side / 2)
9954+ (&chroma[plane - 1], x / 2, y / 2, bw / 2)
99319955 };
99329956 target.code_from_prediction(
99339957 px,
@@ -9959,7 +9983,7 @@ fn search_skip_64(
99599983 commit_inter_luma(
99609984 luma,
99619985 (x, y),
9962- side,
9986+ bw,
99639987 info.mv,
99649988 (&reference.y, reference.width, luma.true_width, luma.true_height),
99659989 search,
@@ -9980,18 +10004,20 @@ fn search_skip_64(
998010004 B64_VAR_TX_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
998110005 }
998210006 luma_levels = Some((levels, depth, tx_types));
9983- chosen = (cost, false, residual_trials);
10007+ chosen = (cost, false, residual_trials.to_vec());
998410008 }
998510009 }
998610010
998710011 let (cost, skip, trials) = chosen;
998810012 // `commit_inter_luma` already wrote the residual arm's luma (its four
998910013 // split units are not one 64-sided trial); a skip winner overwrites it.
9990- if skip || !luma_committed {
9991- luma.commit(x, y, side, &trials[0]);
10014+ for (&(plane, px, py, s, _), trial) in units.iter().zip(&trials) {
10015+ match plane {
10016+ 0 if skip || !luma_committed => luma.commit(px, py, s, trial),
10017+ 0 => {}
10018+ _ => chroma[plane - 1].commit(px, py, s, trial),
10019+ }
999210020 }
9993- chroma[0].commit(x / 2, y / 2, side / 2, &trials[1]);
9994- chroma[1].commit(x / 2, y / 2, side / 2, &trials[2]);
999510021 Some((
999610022 cost,
999710023 BlockCoeffs {
@@ -10011,8 +10037,8 @@ fn search_skip_64(
1001110037 (false, Some((levels, _, _))) => coeffs(levels, BLOCK),
1001210038 (false, None) => coeffs(&trials[0].levels, BLOCK),
1001310039 },
10014- u: if skip { Vec::new() } else { coeffs(&trials[1].levels, side / 2) },
10015- v: if skip { Vec::new() } else { coeffs(&trials[2].levels, side / 2) },
10040+ u: if skip { Vec::new() } else { coeffs(&trials[1].levels, bw / 2) },
10041+ v: if skip { Vec::new() } else { coeffs(&trials[2].levels, bw / 2) },
1001610042 ..BlockCoeffs::default()
1001710043 },
1001810044 ))
@@ -12462,7 +12488,7 @@ pub(crate) fn encode_inter_frame(
1246212488 &mut luma,
1246312489 &mut chroma,
1246412490 (x64, y64),
12465- SUPERBLOCK,
12491+ (SUPERBLOCK, SUPERBLOCK),
1246612492 &sb_search,
1246712493 b64_residual() && !screen,
1246812494 reference,
crates/ec-av1/src/tile.rsmodified+395 -30
@@ -464,6 +464,184 @@ fn write_inter_block_128(
464464 Ok(())
465465 }
466466
467+/// lane-b128hv: one 128x64 (HORZ) or 64x128 (VERT) inter block, the two
468+/// halves a `PARTITION_HORZ`/`PARTITION_VERT` 128 superblock root carries --
469+/// the syntax `decode_inter_block` reads for `sb128_rect` (decode.rs ~35343):
470+/// the same chain [`write_inter_block_128`] writes, at the block's own
471+/// rectangular window. Every CDF/context decision is taken at the enclosing
472+/// square `side` of 128 (what the decoder's `inter_piece!` passes), and the
473+/// block's true footprint governs only its mv-stack window, its mi/grid stamp
474+/// and its neighbour bands (decode.rs' own `write_w`/`write_h` split).
475+///
476+/// SKIP only: the decoder reads a rect half's residual through the mu-chunk
477+/// loop, but the 128 NONE residual arm it would share is itself parked
478+/// (`EC_AV1_B128RES`), so offering one here would buy the same open filter
479+/// mismatch for a candidate the RD takes 0-2 times a frame.
480+///
481+/// # Errors
482+/// A block that is intra or that is not skipped.
483+#[allow(clippy::too_many_arguments)]
484+fn write_inter_block_128_rect(
485+ enc: &mut SymbolEncoder,
486+ cdfs: &mut Cdfs,
487+ neighbours: &mut Neighbours,
488+ grid: &mut MiGrid,
489+ // The half's own top-left position in mode-info (4x4) units.
490+ at_mi: (usize, usize),
491+ // Its true footprint in samples: (128, 64) at HORZ, (64, 128) at VERT.
492+ (w, h): (usize, usize),
493+ block: &BlockCoeffs,
494+ mi_cols: u32,
495+ mi_rows: u32,
496+ tx_select: bool,
497+) -> Result<()> {
498+ let info = block.inter.ok_or_else(|| {
499+ Error::unsupported("AV1 tile", "a 128-root rectangular half is coded inter")
500+ })?;
501+ if !block.skip {
502+ return Err(Error::unsupported(
503+ "AV1 tile",
504+ "a 128-root rectangular half carries a residual",
505+ ));
506+ }
507+ let (mi_r, mi_c) = at_mi;
508+ let (w_mi, h_mi) = (w / MI, h / MI);
509+ let has_above = neighbours.has_above(mi_r);
510+ let has_left = neighbours.has_left(mi_c);
511+ let skip_ctx =
512+ usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]);
513+ enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
514+ write_cdef_idx(enc, (mi_r, mi_c), block.skip);
515+ // NOT a whole superblock: decode.rs' `whole_sb` is `write_w == write_h ==
516+ // sb_px`, so a rect half reads its `delta_q` group even when skipped.
517+ write_delta_q(enc, cdfs, (mi_r, mi_c), false, block.skip);
518+ let ii_ctx = intra_inter_ctx(
519+ has_above,
520+ has_left,
521+ neighbours.above_inter[mi_c],
522+ neighbours.left_inter[mi_r],
523+ );
524+ enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]);
525+ write_comp_mode(
526+ enc,
527+ cdfs,
528+ neighbours,
529+ (mi_r, mi_c),
530+ (has_above, has_left),
531+ info.ref1.is_some(),
532+ );
533+ let (mv, mv1, is_new_mv) = if info.ref1.is_some() {
534+ write_compound_block(
535+ enc,
536+ cdfs,
537+ neighbours,
538+ grid,
539+ (mi_r, mi_c),
540+ (w_mi, h_mi),
541+ (has_above, has_left),
542+ info,
543+ (mi_cols as usize, mi_rows as usize),
544+ )?
545+ } else {
546+ write_single_ref(
547+ enc,
548+ cdfs,
549+ info.ref_frame,
550+ neighbours.above_ref[mi_c],
551+ neighbours.above_ref1[mi_c],
552+ neighbours.left_ref[mi_r],
553+ neighbours.left_ref1[mi_r],
554+ );
555+ let stack = find_mv_stack(
556+ grid,
557+ mi_r,
558+ mi_c,
559+ w_mi,
560+ h_mi,
561+ info.ref_frame,
562+ mi_cols as usize,
563+ mi_rows as usize,
564+ );
565+ let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;
566+ (mv, (0, 0), is_new_mv)
567+ };
568+ for dr in 0..h_mi {
569+ for dc in 0..w_mi {
570+ grid.set(
571+ mi_r + dr,
572+ mi_c + dc,
573+ MiInfo {
574+ is_inter: true,
575+ ref_frame: info.ref_frame,
576+ ref_frame1: info.ref1.unwrap_or(NO_REF1),
577+ mv1: mv16(mv1),
578+ mv: mv16(mv),
579+ is_new_mv,
580+ size: w_mi as u8,
581+ size_h: h_mi as u8,
582+ is_global_mv0: false,
583+ is_global_mv1: false,
584+ },
585+ );
586+ }
587+ }
588+ if info.ref1.is_none() {
589+ write_motion_mode(
590+ enc,
591+ cdfs,
592+ grid,
593+ (mi_r, mi_c),
594+ (w_mi, h_mi),
595+ (w, h),
596+ (mi_cols as usize, mi_rows as usize),
597+ info.ref_frame,
598+ block.motion_mode,
599+ )?;
600+ }
601+ if tx_select {
602+ // `read_block_tx_size_rect`'s skip arm: `txfm_partition_update_rect`
603+ // with the BLOCK's own size in both bands, no symbol.
604+ neighbours.record_txfm_rect((mi_r, mi_c), (w, h), (w, h));
605+ }
606+ let zero_grids = [
607+ vec![0i32; TX32 * TX32],
608+ vec![0i32; TX32 * TX32],
609+ vec![0i32; TX32 * TX32],
610+ ];
611+ // A 128 root's half is always SUB (16 px) aligned, so the coarse mode /
612+ // side bands are named by the same SUB-grid origin every square block
613+ // uses.
614+ let at_sub = (mi_r / (SUB / MI), mi_c / (SUB / MI));
615+ neighbours.record_planes_rect(at_sub, (w, h), 0, &zero_grids, true);
616+ neighbours.record_inter_rect_mi((mi_r, mi_c), (w, h), block.skip, true, block_ref(block));
617+ if let Some(r1) = info.ref1 {
618+ neighbours.record_compound_rect_mi((mi_r, mi_c), (w, h), r1, 0, 1);
619+ }
620+ if w > h {
621+ SB128_HORZ_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
622+ } else {
623+ SB128_VERT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
624+ }
625+ Ok(())
626+}
627+
628+/// lane-b128hv: how many 128x64 / 64x128 halves the inter writer has coded
629+/// since the last [`take_sb128_horz_hits`] / [`take_sb128_vert_hits`] (class
630+/// `gate-blind-to-feature`).
631+static SB128_HORZ_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
632+/// The VERT twin of [`SB128_HORZ_HITS`].
633+static SB128_VERT_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
634+
635+/// The 128x64-half count since the last call, and zero it.
636+pub fn take_sb128_horz_hits() -> usize {
637+ SB128_HORZ_HITS.swap(0, std::sync::atomic::Ordering::Relaxed)
638+}
639+
640+/// The 64x128-half count since the last call, and zero it.
641+pub fn take_sb128_vert_hits() -> usize {
642+ SB128_VERT_HITS.swap(0, std::sync::atomic::Ordering::Relaxed)
643+}
644+
467645 /// lane-b128r: how many of those blocks carried a REAL residual (four
468646 /// TX_64X64 luma units plus their per-chunk chroma pair) rather than being
469647 /// skipped, since the last [`take_sb128_residual_hits`].
@@ -1634,6 +1812,10 @@ fn ec_rng_trace(msg: impl FnOnce() -> String) {
16341812
16351813 /// `PARTITION_NONE` (spec 6.10.4): the whole block, undivided.
16361814 const PARTITION_NONE: usize = 0;
1815+/// `PARTITION_HORZ` (spec 6.10.4): the block cut into a top and a bottom half.
1816+const PARTITION_HORZ: usize = 1;
1817+/// `PARTITION_VERT` (spec 6.10.4): the block cut into a left and a right half.
1818+const PARTITION_VERT: usize = 2;
16371819 /// `PARTITION_SPLIT` (spec 6.10.4): the block cut into four quadrants.
16381820 const PARTITION_SPLIT: usize = 3;
16391821
@@ -2365,6 +2547,15 @@ pub enum Quadrant {
23652547 /// interleaved with its chroma per 64x64 mu chunk, which this writer has
23662548 /// no path for; [`crate::tile::write_inter_block_128`] refuses one.
23672549 Whole128(BlockCoeffs),
2550+ /// lane-b128hv: the two 128x64 (`true`) or 64x128 (`false`) inter blocks a
2551+ /// `PARTITION_HORZ` / `PARTITION_VERT` 128 superblock root carries, in the
2552+ /// order the decoder reads them (top then bottom, left then right).
2553+ /// Carried by the top-left 32x32 entry of the root's sixteen, exactly as
2554+ /// [`Whole128`](Quadrant::Whole128) is.
2555+ ///
2556+ /// SKIP only ([`crate::tile::write_inter_block_128_rect`] refuses a
2557+ /// residual), single reference or compound.
2558+ Rect128(bool, Vec<BlockCoeffs>),
23682559 /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's
23692560 /// top-left already coded; it carries no block of its own.
23702561 Covered,
@@ -2377,7 +2568,7 @@ impl Quadrant {
23772568 Quadrant::Whole(block) | Quadrant::Whole64(block) | Quadrant::Whole128(block) => {
23782569 std::slice::from_ref(block)
23792570 }
2380- Quadrant::Split(blocks) => blocks.as_slice(),
2571+ Quadrant::Split(blocks) | Quadrant::Rect128(_, blocks) => blocks.as_slice(),
23812572 Quadrant::Covered => &[],
23822573 }
23832574 }
@@ -2722,14 +2913,18 @@ impl Neighbours {
27222913 /// [`Self::record_palette_y`] for the chroma bands (decode.rs
27232914 /// `record_palette_uv_rect`) -- `size == 0` clears stale state the same
27242915 /// way, and the span is in LUMA pixels for both planes' bands alike.
2725- fn record_palette_uv(&mut self, (r, c): (usize, usize), side: usize, size: u8, colors: [u16; 8]) {
2726- for cell in 0..(side / MI).max(1) {
2916+ fn record_palette_uv(&mut self, (r, c): (usize, usize), (w, h): (usize, usize), size: u8, colors: [u16; 8]) {
2917+ // lane-b128hv: the above band spans the block's WIDTH and the left
2918+ // band its HEIGHT; identical to the one loop this replaces at w == h.
2919+ for cell in 0..(w / MI).max(1) {
27272920 if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {
27282921 *e = size;
27292922 if size > 0 {
27302923 self.above_palette_uv_colors[c + cell] = colors;
27312924 }
27322925 }
2926+ }
2927+ for cell in 0..(h / MI).max(1) {
27332928 if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {
27342929 *e = size;
27352930 if size > 0 {
@@ -2742,14 +2937,18 @@ impl Neighbours {
27422937 /// What a just-written block leaves in the palette bands (decode.rs
27432938 /// `record_palette_y_rect`): its size and colours over every 4x4 unit it
27442939 /// spans, `0` for a block that took no palette.
2745- fn record_palette_y(&mut self, (r, c): (usize, usize), side: usize, size: u8, colors: [u16; 8]) {
2746- for cell in 0..(side / MI).max(1) {
2940+ fn record_palette_y(&mut self, (r, c): (usize, usize), (w, h): (usize, usize), size: u8, colors: [u16; 8]) {
2941+ // lane-b128hv: the above band spans the block's WIDTH and the left
2942+ // band its HEIGHT; identical to the one loop this replaces at w == h.
2943+ for cell in 0..(w / MI).max(1) {
27472944 if let Some(e) = self.above_palette_size.get_mut(c + cell) {
27482945 *e = size;
27492946 if size > 0 {
27502947 self.above_palette_colors[c + cell] = colors;
27512948 }
27522949 }
2950+ }
2951+ for cell in 0..(h / MI).max(1) {
27532952 if let Some(e) = self.left_palette_size.get_mut(r + cell) {
27542953 *e = size;
27552954 if size > 0 {
@@ -2776,6 +2975,27 @@ impl Neighbours {
27762975 }
27772976 }
27782977
2978+ /// lane-b128hv: decode.rs `txfm_partition_update_rect` -- the two bands
2979+ /// take the transform's own HEIGHT (left) and WIDTH (above), over the
2980+ /// txb's own rect span. [`Self::record_txfm`] is this at `tx_w == tx_h`.
2981+ fn record_txfm_rect(
2982+ &mut self,
2983+ (mi_r, mi_c): (usize, usize),
2984+ (tx_w, tx_h): (usize, usize),
2985+ (txb_w, txb_h): (usize, usize),
2986+ ) {
2987+ for i in 0..txb_h / MI {
2988+ if let Some(cell) = self.left_txfm.get_mut(mi_r + i) {
2989+ *cell = tx_h as u8;
2990+ }
2991+ }
2992+ for i in 0..txb_w / MI {
2993+ if let Some(cell) = self.above_txfm.get_mut(mi_c + i) {
2994+ *cell = tx_w as u8;
2995+ }
2996+ }
2997+ }
2998+
27792999 /// `get_tx_size_context` as libaom writes it for a block inside an INTER
27803000 /// frame (decode.rs `tx_size_context_txfm_rect`): the `TXFM_CONTEXT`
27813001 /// bands, except that an *inter* neighbour contributes its own BLOCK size.
@@ -2897,15 +3117,31 @@ impl Neighbours {
28973117 mode: usize,
28983118 grids: &[Vec<i32>; 3],
28993119 luma: bool,
3120+ ) {
3121+ self.record_planes_rect(at, (side, side), mode, grids, luma);
3122+ }
3123+
3124+ /// lane-b128hv: [`Self::record_planes`] with independent above (width) /
3125+ /// left (height) extents, decode.rs `record_rect_mi`'s own split -- the
3126+ /// 128 root's HORZ/VERT halves are the writer's first rect blocks.
3127+ fn record_planes_rect(
3128+ &mut self,
3129+ at: (usize, usize),
3130+ (w, h): (usize, usize),
3131+ mode: usize,
3132+ grids: &[Vec<i32>; 3],
3133+ luma: bool,
29003134 ) {
29013135 let (r, c) = at;
2902- for cell in 0..side / SUB {
3136+ for cell in 0..w / SUB {
29033137 self.above_mode[c + cell] = mode;
3138+ self.above_side[c + cell] = w;
3139+ }
3140+ for cell in 0..h / SUB {
29043141 self.left_mode[r + cell] = mode;
2905- self.above_side[c + cell] = side;
2906- self.left_side[r + cell] = side;
3142+ self.left_side[r + cell] = h;
29073143 }
2908- self.record_mi_planes((r * (SUB / MI), c * (SUB / MI)), side, grids, luma);
3144+ self.record_mi_planes_rect((r * (SUB / MI), c * (SUB / MI)), (w, h), grids, luma);
29093145 }
29103146
29113147 /// The coefficient-context half of [`Self::record`], taking the block's
@@ -2926,19 +3162,33 @@ impl Neighbours {
29263162 side: usize,
29273163 grids: &[Vec<i32>; 3],
29283164 luma: bool,
3165+ ) {
3166+ self.record_mi_planes_rect(at_mi, (side, side), grids, luma);
3167+ }
3168+
3169+ /// [`Self::record_mi_planes`] with independent above (width) / left
3170+ /// (height) extents (lane-b128hv, decode.rs `record_mi_rect`).
3171+ fn record_mi_planes_rect(
3172+ &mut self,
3173+ at_mi: (usize, usize),
3174+ (w, h): (usize, usize),
3175+ grids: &[Vec<i32>; 3],
3176+ luma: bool,
29293177 ) {
29303178 let (mi_r, mi_c) = at_mi;
29313179 if screen_armed() {
29323180 let (size, colors) = self.pending_palette.take().unwrap_or((0, [0u16; 8]));
2933- self.record_palette_y(at_mi, side, size, colors);
3181+ self.record_palette_y(at_mi, (w, h), size, colors);
29343182 let (uv_size, uv_colors) = self.pending_palette_uv.take().unwrap_or((0, [0u16; 8]));
2935- self.record_palette_uv(at_mi, side, uv_size, uv_colors);
3183+ self.record_palette_uv(at_mi, (w, h), uv_size, uv_colors);
29363184 }
29373185 let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
2938- let side_mi = side / MI;
2939- for cell in 0..side_mi {
2940- self.left_side_mi[mi_r + cell] = side;
2941- self.above_side_mi[mi_c + cell] = side;
3186+ let (w_mi, h_mi) = (w / MI, h / MI);
3187+ for cell in 0..h_mi {
3188+ self.left_side_mi[mi_r + cell] = h;
3189+ }
3190+ for cell in 0..w_mi {
3191+ self.above_side_mi[mi_c + cell] = w;
29423192 }
29433193 // libaom rounds the luma edge up to the plane's own 4x4 unit before
29443194 // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high,
@@ -2957,25 +3207,29 @@ impl Neighbours {
29573207 round_up_even(self.mi_cols),
29583208 round_up_even(self.mi_cols),
29593209 ];
2960- for cell in 0..side_mi {
3210+ for cell in 0..h_mi {
29613211 let keep_left = self.left[mi_r + cell][0];
2962- let keep_above = self.above[mi_c + cell][0];
29633212 self.left[mi_r + cell] = std::array::from_fn(|plane| {
2964- if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) {
3213+ if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) {
29653214 states[plane]
29663215 } else {
29673216 Default::default()
29683217 }
29693218 });
3219+ if !luma {
3220+ self.left[mi_r + cell][0] = keep_left;
3221+ }
3222+ }
3223+ for cell in 0..w_mi {
3224+ let keep_above = self.above[mi_c + cell][0];
29703225 self.above[mi_c + cell] = std::array::from_fn(|plane| {
2971- if cell < side_mi.min(bound_w[plane].saturating_sub(mi_c)) {
3226+ if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) {
29723227 states[plane]
29733228 } else {
29743229 Default::default()
29753230 }
29763231 });
29773232 if !luma {
2978- self.left[mi_r + cell][0] = keep_left;
29793233 self.above[mi_c + cell][0] = keep_above;
29803234 }
29813235 }
@@ -2989,6 +3243,56 @@ impl Neighbours {
29893243 self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame);
29903244 }
29913245
3246+ /// lane-b128hv: [`Self::record_inter_mi`] with independent above (width)
3247+ /// / left (height) spans, decode.rs `record_inter_rect_mi`'s own split.
3248+ fn record_inter_rect_mi(
3249+ &mut self,
3250+ (mi_r, mi_c): (usize, usize),
3251+ (w, h): (usize, usize),
3252+ skip: bool,
3253+ is_inter: bool,
3254+ ref_frame: i8,
3255+ ) {
3256+ let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME;
3257+ for cell in 0..h / MI {
3258+ self.left_skip[mi_r + cell] = skip;
3259+ self.left_inter[mi_r + cell] = is_inter;
3260+ self.left_ref[mi_r + cell] = ref_frame;
3261+ self.left_ref1[mi_r + cell] = None;
3262+ self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 };
3263+ self.left_compound_idx[mi_r + cell] = u8::from(altref);
3264+ }
3265+ for cell in 0..w / MI {
3266+ self.above_skip[mi_c + cell] = skip;
3267+ self.above_inter[mi_c + cell] = is_inter;
3268+ self.above_ref[mi_c + cell] = ref_frame;
3269+ self.above_ref1[mi_c + cell] = None;
3270+ self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 };
3271+ self.above_compound_idx[mi_c + cell] = u8::from(altref);
3272+ }
3273+ }
3274+
3275+ /// [`Self::record_compound_mi`] over a rect block's own two spans.
3276+ fn record_compound_rect_mi(
3277+ &mut self,
3278+ (mi_r, mi_c): (usize, usize),
3279+ (w, h): (usize, usize),
3280+ ref1: i8,
3281+ comp_group_idx: u8,
3282+ compound_idx: u8,
3283+ ) {
3284+ for cell in 0..h / MI {
3285+ self.left_ref1[mi_r + cell] = Some(ref1);
3286+ self.left_comp_group_idx[mi_r + cell] = comp_group_idx;
3287+ self.left_compound_idx[mi_r + cell] = compound_idx;
3288+ }
3289+ for cell in 0..w / MI {
3290+ self.above_ref1[mi_c + cell] = Some(ref1);
3291+ self.above_comp_group_idx[mi_c + cell] = comp_group_idx;
3292+ self.above_compound_idx[mi_c + cell] = compound_idx;
3293+ }
3294+ }
3295+
29923296 /// [`Self::record_inter_mi`]'s compound half (decode.rs
29933297 /// `Neighbours::record_compound_ctx_rect_mi`): overwrites the ALTREF
29943298 /// single-reference default with a real compound block's own second
@@ -3397,7 +3701,10 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs(
33973701 match quadrant {
33983702 // A 64x64 root is an INTER-frame partition; a
33993703 // key frame codes one as `Superblock::Whole`.
3400- Quadrant::Whole64(_) | Quadrant::Whole128(_) | Quadrant::Covered => {
3704+ Quadrant::Whole64(_)
3705+ | Quadrant::Whole128(_)
3706+ | Quadrant::Rect128(..)
3707+ | Quadrant::Covered => {
34013708 return Err(Error::unsupported(
34023709 "AV1 tile",
34033710 "a key frame codes a 64x64 block as `Superblock::Whole`",
@@ -5065,7 +5372,7 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 {
50655372 // lane-b128: a 128x128 root is SKIP by construction (the
50665373 // writer refuses a residual at that size), so it names no
50675374 // coefficients on any plane.
5068- Quadrant::Whole128(_) => return 0.0,
5375+ Quadrant::Whole128(_) | Quadrant::Rect128(..) => return 0.0,
50695376 Quadrant::Whole(_) => 32,
50705377 Quadrant::Split(_) => 16,
50715378 // lane-tx64: a 64x64 root that codes a residual pays for one
@@ -5687,6 +5994,22 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 {
56875994 crate::encode::symbol_bits(cdf, symbol)
56885995 }
56895996
5997+/// lane-b128hv: [`partition_bits`] for any partition symbol, at the same
5998+/// fixed (round-2) tables -- what the 128 root's HORZ/VERT candidate is priced
5999+/// with (`PARTITION_HORZ` = 1, `PARTITION_VERT` = 2).
6000+pub(crate) fn partition_bits_sym(side: usize, symbol: usize) -> f64 {
6001+ static CDFS: LazyLock<Cdfs> = LazyLock::new(|| Cdfs::new(2));
6002+ let cdfs = &*CDFS;
6003+ let cdf: &[u16] = match side {
6004+ 128 => &cdfs.partition_w128[0],
6005+ SB => &cdfs.partition_w64[0],
6006+ BLOCK => &cdfs.partition_w32[0],
6007+ TX8 => &cdfs.partition_w8[0],
6008+ _ => &cdfs.partition_w16[0],
6009+ };
6010+ crate::encode::symbol_bits(cdf, symbol)
6011+}
6012+
56906013 /// coeffs() for one plane's transform block of any coefficients the base and
56916014 /// base-range syntax reach (spec 5.11.39).
56926015 ///
@@ -6769,12 +7092,17 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
67697092 if sb128 && sb_start {
67707093 // The 128x128 block a `PARTITION_NONE` root carries, held by the
67717094 // top-left 32x32 entry of the root's sixteen.
6772- let root_block = ((sb_r & !1) * 2 < rows && (sb_c & !1) * 2 < cols)
6773- .then(|| &blocks[(((sb_r & !1) * 2) * cols + (sb_c & !1) * 2) as usize])
6774- .and_then(|q| match q {
6775- Quadrant::Whole128(block) => Some(block),
6776- _ => None,
6777- });
7095+ let root_entry = ((sb_r & !1) * 2 < rows && (sb_c & !1) * 2 < cols)
7096+ .then(|| &blocks[(((sb_r & !1) * 2) * cols + (sb_c & !1) * 2) as usize]);
7097+ let root_block = root_entry.and_then(|q| match q {
7098+ Quadrant::Whole128(block) => Some(block),
7099+ _ => None,
7100+ });
7101+ // lane-b128hv: the two rectangular halves a HORZ/VERT root carries.
7102+ let root_rect = root_entry.and_then(|q| match q {
7103+ Quadrant::Rect128(horz, halves) => Some((*horz, halves)),
7104+ _ => None,
7105+ });
67787106 write_sb128_root(
67797107 &mut enc,
67807108 &mut cdfs,
@@ -6783,7 +7111,12 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
67837111 sb_c,
67847112 mi_cols,
67857113 mi_rows,
6786- if root_block.is_some() { PARTITION_NONE } else { PARTITION_SPLIT },
7114+ match (root_block.is_some(), root_rect) {
7115+ (true, _) => PARTITION_NONE,
7116+ (_, Some((true, _))) => PARTITION_HORZ,
7117+ (_, Some((false, _))) => PARTITION_VERT,
7118+ _ => PARTITION_SPLIT,
7119+ },
67877120 );
67887121 if let Some(block) = root_block {
67897122 let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4);
@@ -6801,6 +7134,35 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
68017134 sb128_none = true;
68027135 continue;
68037136 }
7137+ if let Some((horz, halves)) = root_rect {
7138+ // libaom `decode_partition` at BLOCK_128X128: `subsize` at
7139+ // (mi_row, mi_col) and a second one at + hbs (16 mi) along the
7140+ // cut axis, the second only when it is inside the frame -- the
7141+ // search only offers a root wholly inside, so both are.
7142+ let base_mi = ((sb_r & !1) as usize * SB_MI as usize, (sb_c & !1) as usize * SB_MI as usize);
7143+ let (w, h) = if horz { (128usize, 64usize) } else { (64, 128) };
7144+ for (i, half) in halves.iter().enumerate() {
7145+ let at_mi = if horz {
7146+ (base_mi.0 + i * 16, base_mi.1)
7147+ } else {
7148+ (base_mi.0, base_mi.1 + i * 16)
7149+ };
7150+ write_inter_block_128_rect(
7151+ &mut enc,
7152+ &mut cdfs,
7153+ &mut neighbours,
7154+ &mut grid,
7155+ at_mi,
7156+ (w, h),
7157+ half,
7158+ mi_cols,
7159+ mi_rows,
7160+ tx_select,
7161+ )?;
7162+ }
7163+ sb128_none = true;
7164+ continue;
7165+ }
68047166 }
68057167 {
68067168 if !sb128 {
@@ -7046,7 +7408,10 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
70467408 let block = match site {
70477409 // Already coded by the `Whole64` above -- but only inside
70487410 // a superblock that really carried one.
7049- Quadrant::Whole64(_) | Quadrant::Whole128(_) | Quadrant::Covered => {
7411+ Quadrant::Whole64(_)
7412+ | Quadrant::Whole128(_)
7413+ | Quadrant::Rect128(..)
7414+ | Quadrant::Covered => {
70507415 return Err(Error::unsupported(
70517416 "AV1 tile",
70527417 "a `Whole64`/`Covered` quadrant needs its superblock's \