forge
Explore
Log in

Don't you dare go Hollow

Interintra built a square intra predictor for rectangular blocks

Browse files at this commit

f48ab1b9ede58111c5190b507fd481269024d437Ahmet Kaan GümüşParent: 663bb80
  • 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() {
43 let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters();43 let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters();
44 println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}");44 println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}");
45 println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits());45 println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits());
46+ println!("interintra_rect: {}", ec_av1::decode::interintra_rect_hits());
46 let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits();47 let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits();
47 println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}");48 println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}");
48 let si = ec_av1::stream::sub8_intra_rect_hits();49 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 {
2075 INTERINTRA_HITS.with(|c| c.get())2075 INTERINTRA_HITS.with(|c| c.get())
2076 }2076 }
2077 2077
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+
2078 // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE,2092 // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE,
2079 // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/2093 // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/
2080 // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a2094 // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a
@@ -18704,7 +18718,9 @@ fn interintra_blend(
18704 plane: &PlaneBuf,18718 plane: &PlaneBuf,
18705 x: usize,18719 x: usize,
18706 y: usize,18720 y: usize,
18707- side: usize,18721+ bw: usize,
18722+ bh: usize,
18723+ stride: usize,
18708 ii_mode: u8,18724 ii_mode: u8,
18709 wedge: Option<(&'static [u8], usize)>,18725 wedge: Option<(&'static [u8], usize)>,
18710 pred: &mut [u16],18726 pred: &mut [u16],
@@ -18716,16 +18732,17 @@ fn interintra_blend(
18716 2 => crate::intra::H_PRED,18732 2 => crate::intra::H_PRED,
18717 _ => crate::intra::SMOOTH_PRED,18733 _ => crate::intra::SMOOTH_PRED,
18718 };18734 };
18719- let (above, left, corner) = plane.edges(18735+ let (above, left, corner) = plane.edges_rect(
18720 x,18736 x,
18721 y,18737 y,
18722- side,18738+ bw,
18739+ bh,
18723 crate::encode::Reach {18740 crate::encode::Reach {
18724 above_right: false,18741 above_right: false,
18725 below_left: false,18742 below_left: false,
18726 },18743 },
18727 );18744 );
18728- let mut intra = vec![0u16; side * side];18745+ let mut intra = vec![0u16; bw * bh];
18729 // Edge filtering never applies here: V/H at delta 0 are plain edge18746 // Edge filtering never applies here: V/H at delta 0 are plain edge
18730 // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no18747 // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no
18731 // angle at all.18748 // angle at all.
@@ -18735,22 +18752,25 @@ fn interintra_blend(
18735 above.as_deref(),18752 above.as_deref(),
18736 left.as_deref(),18753 left.as_deref(),
18737 corner,18754 corner,
18738- side,18755+ bw,
18739- side,18756+ bh,
18740 false,18757 false,
18741 false,18758 false,
18742 &mut intra,18759 &mut intra,
18743 );18760 );
18744- let scale = 128 / side;18761+ // `ii_size_scales[bsize]` is keyed on the block's LONGER side
18745- for i in 0..side {18762+ // (`MAX_SB_SIZE >> max(bw,bh)` order), not on the square this decoder
18746- for j in 0..side {18763+ // builds the predictor buffer at.
18764+ let scale = 128 / bw.max(bh);
18765+ for i in 0..bh {
18766+ for j in 0..bw {
18747 let m = u32::from(match wedge {18767 let m = u32::from(match wedge {
18748 // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask18768 // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask
18749 // over the wedge codebook, intra as src0, fixed sign 0).18769 // over the wedge codebook, intra as src0, fixed sign 0).
18750 // Luma plane: mask rows are luma-resolution, ms == side.18770 // Luma plane: mask rows are luma-resolution, ms == side.
18751 // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 --18771 // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 --
18752 // subw == subh == 1).18772 // subw == subh == 1).
18753- Some((mask, ms)) if ms == side => mask[i * side + j],18773+ Some((mask, ms)) if ms == stride => mask[i * stride + j],
18754 Some((mask, ms)) => {18774 Some((mask, ms)) => {
18755 let t = 2 * i * ms + 2 * j;18775 let t = 2 * i * ms + 2 * j;
18756 ((u32::from(mask[t])18776 ((u32::from(mask[t])
@@ -18767,9 +18787,9 @@ fn interintra_blend(
18767 _ => II_WEIGHTS_1D[i.min(j) * scale],18787 _ => II_WEIGHTS_1D[i.min(j) * scale],
18768 },18788 },
18769 });18789 });
18770- let idx = i * side + j;18790+ let idx = i * stride + j;
18771- pred[idx] =18791+ pred[idx] = ((m * u32::from(intra[i * bw + j]) + (64 - m) * u32::from(pred[idx]) + 32)
18772- ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u16;18792+ >> 6) as u16;
18773 }18793 }
18774 }18794 }
18775 }18795 }
@@ -21636,9 +21656,18 @@ fn decode_inter_block(
21636 // result, all planes (reconinter.c av1_build_interintra_predictor21656 // result, all planes (reconinter.c av1_build_interintra_predictor
21637 // plane loop).21657 // plane loop).
21638 if let Some(ii) = interintra_mode {21658 if let Some(ii) = interintra_mode {
21639- interintra_blend(y, px, py, side, ii, wedge_mask, &mut pred_y);21659+ if write_w != write_h {
21640- interintra_blend(u, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_u);21660+ INTERINTRA_RECT_HITS.with(|c| c.set(c.get() + 1));
21641- interintra_blend(v, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_v);21661+ }
21662+ interintra_blend(y, px, py, write_w, write_h, side, ii, wedge_mask, &mut pred_y);
21663+ interintra_blend(
21664+ u, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask,
21665+ &mut pred_u,
21666+ );
21667+ interintra_blend(
21668+ v, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask,
21669+ &mut pred_v,
21670+ );
21642 }21671 }
21643 21672
21644 // lane-inter16ab r2: libaom `build_inter_predictors_sub8x8`21673 // lane-inter16ab r2: libaom `build_inter_predictors_sub8x8`
@@ -25640,9 +25669,13 @@ fn decode_inter_block8(
25640 // Non-wedge interintra blend, mutually exclusive with OBMC (the25669 // Non-wedge interintra blend, mutually exclusive with OBMC (the
25641 // motion_mode symbol is not read for an interintra block).25670 // motion_mode symbol is not read for an interintra block).
25642 if let Some(ii) = interintra_mode {25671 if let Some(ii) = interintra_mode {
25643- interintra_blend(y, px, py, SIDE, ii, wedge_mask, &mut pred_y);25672+ interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y);
25644- interintra_blend(u, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_u);25673+ interintra_blend(
25645- interintra_blend(v, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_v);25674+ u, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_u,
25675+ );
25676+ interintra_blend(
25677+ v, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_v,
25678+ );
25646 }25679 }
25647 25680
25648 split8 = read_block_tx_size(25681 split8 = read_block_tx_size(
crates/ec-av1/src/stream.rsmodified+9 -2
@@ -29,6 +29,13 @@ use crate::decode::{
29 };29 };
30 use crate::encode::Picture;30 use crate::encode::Picture;
31 31
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+
32 /// lane-hidden r1: where `EC_AV1_FINAL_DUMP`'s per-frame dump goes for the39 /// lane-hidden r1: where `EC_AV1_FINAL_DUMP`'s per-frame dump goes for the
33 /// current thread, overriding the environment variable. Every ffmpeg/aomdec40 /// current thread, overriding the environment variable. Every ffmpeg/aomdec
34 /// pixel gate in this repo compares SHOWN frames only (class41 /// pixel gate in this repo compares SHOWN frames only (class
@@ -291,7 +298,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
291 order_hint_slots[i] = order_hint_slots[slot];298 order_hint_slots[i] = order_hint_slots[slot];
292 }299 }
293 }300 }
294- let output = if header.film_grain.apply_grain {301+ let output = if header.film_grain.apply_grain && !no_grain() {
295 // lane-hbd10 r1: grain synthesis is bit-depth generic302 // lane-hbd10 r1: grain synthesis is bit-depth generic
296 // (`film_grain.rs`'s `grain_range`/`scale_lut`, spec 7.18.3).303 // (`film_grain.rs`'s `grain_range`/`scale_lut`, spec 7.18.3).
297 let bit_depth = parser304 let bit_depth = parser
@@ -961,7 +968,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> {
961 decode::note_fwd_kf();968 decode::note_fwd_kf();
962 }969 }
963 if header.show_frame {970 if header.show_frame {
964- let output = if header.film_grain.apply_grain {971+ let output = if header.film_grain.apply_grain && !no_grain() {
965 let mc_identity = parser972 let mc_identity = parser
966 .sequence_header()973 .sequence_header()
967 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);974 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);