forge
Explore
Log in

Don't you dare go Hollow

Interintra built a square intra predictor for rectangular blocks

Browse files at this commit

9a73c3092d9bf0e6fe9e80d539c4a0916ed6f6d7Ahmet Kaan GümüşParent: 3e2b507
  • fix
    • interintra built a square intra predictor for rectangular blocks
Show full message
fix(av1): interintra built a square intra predictor for rectangular blocks

`interintra_blend` took one `side` and used it for the edge fetch, the intra
predictor's `(bw, bh)`, `ii_size_scales` and the blend bounds. On a
rectangular block (`write_w != write_h`) `side` is the LONGER dimension, so a
32x16 block got a 32x32 SMOOTH/DC predictor built off 32 rows of left edge
(16 of which are not yet decoded) instead of libaom's 32x16 one. The mask
scale happened to come out right (`128 / max(bw,bh)` == `ii_size_scales`),
the predictor did not: the blend error is zero on the block's first row and
grows downward, exactly the `sm_weights_h` divergence between a height-32 and
a height-16 SMOOTH_PRED.

Blend now takes `(bw, bh, stride)`: `edges_rect`/`predict` at the true
footprint, mask scale off `max(bw, bh)`, loop over `bh x bw`, `pred` indexed
at the buffer's own stride. New `interintra_rect_hits()` counts the shape
(every existing interintra gate passed with the bug, so the rect arm was
vacuous) and `decode_probe` reports it.

Also `EC_AV1_NO_GRAIN=1` in `decode_stream`, which splits a mismatch into
reconstruction vs film-grain synthesis in one run (it is what ruled grain out
here).

Measured on a 2-frame cut of the 10-bit 3840x1608 stream (key frame + first
hidden alt-ref, 59 rect interintra blocks): the alt-ref's pre-filter dump vs
the instrumented aomdec goes from a first mismatch at luma (17,1088) to that
superblock exact; first remaining mismatch moves to (448,1152), a separate
NEARMV mv-stack defect (ours (-40,-340), aomdec (-32,-350)).
crates/ec-av1/examples/decode_probe.rsmodified+1 -0
@@ -43,6 +43,7 @@ fn main() {
4343 let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters();
4444 println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}");
4545 println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits());
46+ println!("interintra_rect: {}", ec_av1::decode::interintra_rect_hits());
4647 let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits();
4748 println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}");
4849 let si = ec_av1::stream::sub8_intra_rect_hits();
crates/ec-av1/src/decode.rsmodified+52 -19
@@ -2075,6 +2075,20 @@ pub(crate) fn interintra_hits() -> usize {
20752075 INTERINTRA_HITS.with(|c| c.get())
20762076 }
20772077
2078+// lane-midcut r1: of those, how many were RECTANGULAR (`write_w != write_h`)
2079+// -- the shape `interintra_blend` used to build a square `side x side` intra
2080+// predictor for, with `ii_size_scales`/`edges` taken at the square. Every
2081+// pre-existing interintra gate passed with that bug, so the rect arm needs
2082+// its own counter to stop being vacuous.
2083+thread_local! {
2084+ static INTERINTRA_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
2085+}
2086+
2087+/// Current value of [`INTERINTRA_RECT_HITS`].
2088+pub fn interintra_rect_hits() -> usize {
2089+ INTERINTRA_RECT_HITS.with(|c| c.get())
2090+}
2091+
20782092 // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE,
20792093 // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/
20802094 // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a
@@ -18941,7 +18955,9 @@ fn interintra_blend(
1894118955 plane: &PlaneBuf,
1894218956 x: usize,
1894318957 y: usize,
18944- side: usize,
18958+ bw: usize,
18959+ bh: usize,
18960+ stride: usize,
1894518961 ii_mode: u8,
1894618962 wedge: Option<(&'static [u8], usize)>,
1894718963 pred: &mut [u16],
@@ -18953,16 +18969,17 @@ fn interintra_blend(
1895318969 2 => crate::intra::H_PRED,
1895418970 _ => crate::intra::SMOOTH_PRED,
1895518971 };
18956- let (above, left, corner) = plane.edges(
18972+ let (above, left, corner) = plane.edges_rect(
1895718973 x,
1895818974 y,
18959- side,
18975+ bw,
18976+ bh,
1896018977 crate::encode::Reach {
1896118978 above_right: false,
1896218979 below_left: false,
1896318980 },
1896418981 );
18965- let mut intra = vec![0u16; side * side];
18982+ let mut intra = vec![0u16; bw * bh];
1896618983 // Edge filtering never applies here: V/H at delta 0 are plain edge
1896718984 // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no
1896818985 // angle at all.
@@ -18972,22 +18989,25 @@ fn interintra_blend(
1897218989 above.as_deref(),
1897318990 left.as_deref(),
1897418991 corner,
18975- side,
18976- side,
18992+ bw,
18993+ bh,
1897718994 false,
1897818995 false,
1897918996 &mut intra,
1898018997 );
18981- let scale = 128 / side;
18982- for i in 0..side {
18983- for j in 0..side {
18998+ // `ii_size_scales[bsize]` is keyed on the block's LONGER side
18999+ // (`MAX_SB_SIZE >> max(bw,bh)` order), not on the square this decoder
19000+ // builds the predictor buffer at.
19001+ let scale = 128 / bw.max(bh);
19002+ for i in 0..bh {
19003+ for j in 0..bw {
1898419004 let m = u32::from(match wedge {
1898519005 // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask
1898619006 // over the wedge codebook, intra as src0, fixed sign 0).
1898719007 // Luma plane: mask rows are luma-resolution, ms == side.
1898819008 // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 --
1898919009 // subw == subh == 1).
18990- Some((mask, ms)) if ms == side => mask[i * side + j],
19010+ Some((mask, ms)) if ms == stride => mask[i * stride + j],
1899119011 Some((mask, ms)) => {
1899219012 let t = 2 * i * ms + 2 * j;
1899319013 ((u32::from(mask[t])
@@ -19004,9 +19024,9 @@ fn interintra_blend(
1900419024 _ => II_WEIGHTS_1D[i.min(j) * scale],
1900519025 },
1900619026 });
19007- let idx = i * side + j;
19008- pred[idx] =
19009- ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u16;
19027+ let idx = i * stride + j;
19028+ pred[idx] = ((m * u32::from(intra[i * bw + j]) + (64 - m) * u32::from(pred[idx]) + 32)
19029+ >> 6) as u16;
1901019030 }
1901119031 }
1901219032 }
@@ -21873,9 +21893,18 @@ fn decode_inter_block(
2187321893 // result, all planes (reconinter.c av1_build_interintra_predictor
2187421894 // plane loop).
2187521895 if let Some(ii) = interintra_mode {
21876- interintra_blend(y, px, py, side, ii, wedge_mask, &mut pred_y);
21877- interintra_blend(u, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_u);
21878- interintra_blend(v, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_v);
21896+ if write_w != write_h {
21897+ INTERINTRA_RECT_HITS.with(|c| c.set(c.get() + 1));
21898+ }
21899+ interintra_blend(y, px, py, write_w, write_h, side, ii, wedge_mask, &mut pred_y);
21900+ interintra_blend(
21901+ u, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask,
21902+ &mut pred_u,
21903+ );
21904+ interintra_blend(
21905+ v, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask,
21906+ &mut pred_v,
21907+ );
2187921908 }
2188021909
2188121910 // lane-inter16ab r2: libaom `build_inter_predictors_sub8x8`
@@ -25935,9 +25964,13 @@ fn decode_inter_block8(
2593525964 // Non-wedge interintra blend, mutually exclusive with OBMC (the
2593625965 // motion_mode symbol is not read for an interintra block).
2593725966 if let Some(ii) = interintra_mode {
25938- interintra_blend(y, px, py, SIDE, ii, wedge_mask, &mut pred_y);
25939- interintra_blend(u, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_u);
25940- interintra_blend(v, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_v);
25967+ interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y);
25968+ interintra_blend(
25969+ u, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_u,
25970+ );
25971+ interintra_blend(
25972+ v, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_v,
25973+ );
2594125974 }
2594225975
2594325976 split8 = read_block_tx_size(
crates/ec-av1/src/stream.rsmodified+9 -2
@@ -29,6 +29,13 @@ use crate::decode::{
2929 };
3030 use crate::encode::Picture;
3131
32+/// lane-midcut r1: `EC_AV1_NO_GRAIN=1` skips film grain synthesis at output, so a
33+/// mid-film mismatch splits into "reconstruction" (compare against
34+/// `ffmpeg -c:v libdav1d -filmgrain 0`) and "grain synthesis" in one run.
35+fn no_grain() -> bool {
36+ std::env::var_os("EC_AV1_NO_GRAIN").is_some()
37+}
38+
3239 /// lane-hidden r1: where `EC_AV1_FINAL_DUMP`'s per-frame dump goes for the
3340 /// current thread, overriding the environment variable. Every ffmpeg/aomdec
3441 /// pixel gate in this repo compares SHOWN frames only (class
@@ -297,7 +304,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
297304 order_hint_slots[i] = order_hint_slots[slot];
298305 }
299306 }
300- let output = if header.film_grain.apply_grain {
307+ let output = if header.film_grain.apply_grain && !no_grain() {
301308 // lane-hbd10 r1: grain synthesis is bit-depth generic
302309 // (`film_grain.rs`'s `grain_range`/`scale_lut`, spec 7.18.3).
303310 let bit_depth = parser
@@ -967,7 +974,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
967974 decode::note_fwd_kf();
968975 }
969976 if header.show_frame {
970- let output = if header.film_grain.apply_grain {
977+ let output = if header.film_grain.apply_grain && !no_grain() {
971978 let mc_identity = parser
972979 .sequence_header()
973980 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);