forge
Explore
Log in

Don't you dare go Hollow

Gather dc-sign context per 4x4 unit, clamped to the true edge

Browse files at this commit

77b7e148bdb4c08a2ab6dca16d6cbf7a026fd475Ahmet Kaan GümüşParent: dcd1644
  • fix
    • gather dc-sign context per 4x4 unit, clamped to the true edge
Show full message
fix(av1): gather dc-sign context per 4x4 unit, clamped to the true edge

get_txb_ctx_general (libaom av1/common/txb_common.h) sums the DC-sign vote
over every 4x4 mode-info unit a transform spans, not once per coded
neighbour cell; av1_set_entropy_contexts (av1/common/blockd.c) clamps that
per-unit state to the frame's true (unpadded) mi_cols/mi_rows, zeroing
units past the edge even mid-cell. Our writer tracked one vote per 16x16
neighbour cell, so a 32x32 transform straddling the true bottom edge
(mi_rows=30, block rows 24..31) summed 2 cells instead of libaom's 8, and
never zeroed the two invisible mi-rows within the last cell -- producing
dc_sign_ctx=0 where the real decoder reads 2 (traced at mi(24,8), tell
2969, in a 192x120 key frame).

Widens Neighbours' above/left tracking from the 16x16 (SUB) grid to the
4x4 (MI) grid libaom actually keeps it on, and clamps record() to the
frame's true mi_cols/mi_rows the same way av1_set_entropy_contexts does.
mode/side/skip/inter tracking (single-cell lookups, not summed) stays on
the coarser grid.

192x120 (the established repro) now round-trips through ffmpeg bit-exact.
1920x1080 and the 854x480/non-multiple sequence tests still fail at other
positions -- a further defect of the same edge-clamping class, not yet
isolated; not chased further this round per budget.
crates/ec-av1/src/tile.rsmodified+69 -18
@@ -517,6 +517,10 @@ const SB: usize = 64;
517517 /// Side of the smallest block the writer codes, in samples, which is the grid
518518 /// the neighbour bookkeeping is kept on.
519519 const SUB: usize = 16;
520+/// Side of a 4x4 mode-info unit, in samples: the granularity libaom's above
521+/// and left entropy-context arrays are actually kept on (spec
522+/// `get_txb_ctx`/`av1_set_entropy_contexts`), finer than [`SUB`].
523+const MI: usize = 4;
520524
521525 /// What one coded block leaves behind for the blocks that read it as a
522526 /// neighbour: whether it coded anything at all, and the sign of its DC.
@@ -612,6 +616,13 @@ struct Neighbours {
612616 above_inter: Vec<bool>,
613617 /// The same, down the left edge.
614618 left_inter: Vec<bool>,
619+ /// The frame's true (unpadded) width and height, in 4x4 mode-info units,
620+ /// which is what clamps [`Self::above`]/[`Self::left`] at the edge (spec
621+ /// `av1_set_entropy_contexts`): a block whose row or column run spills
622+ /// past this bound leaves its trailing 4x4 units at their default,
623+ /// mid-cell if need be.
624+ mi_cols: usize,
625+ mi_rows: usize,
615626 }
616627
617628 /// What one plane's neighbours leave for a block, gathered across every cell
@@ -631,11 +642,13 @@ struct Around {
631642
632643 impl Neighbours {
633644 /// The state a tile starts from: a block outside it reads as `DC_PRED`,
634- /// as having coded nothing, and as unsplit.
635- fn new(cols: usize, rows: usize) -> Self {
645+ /// as having coded nothing, and as unsplit. `cols`/`rows` are in [`SUB`]
646+ /// units; `mi_cols`/`mi_rows` are the frame's true (unpadded) size in 4x4
647+ /// mode-info units.
648+ fn new(cols: usize, rows: usize, mi_cols: usize, mi_rows: usize) -> Self {
636649 Self {
637- above: vec![[Neighbour::default(); 3]; cols],
638- left: vec![[Neighbour::default(); 3]; rows],
650+ above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)],
651+ left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)],
639652 above_mode: vec![DC_PRED; cols],
640653 left_mode: vec![DC_PRED; rows],
641654 above_side: vec![SB; cols],
@@ -644,6 +657,8 @@ impl Neighbours {
644657 left_skip: vec![false; rows],
645658 above_inter: vec![false; cols],
646659 left_inter: vec![false; rows],
660+ mi_cols,
661+ mi_rows,
647662 }
648663 }
649664
@@ -656,18 +671,37 @@ impl Neighbours {
656671 self.left_inter.iter_mut().for_each(|i| *i = false);
657672 }
658673
659- /// Writes one coded block into every 16x16 column and row it covers.
674+ /// Writes one coded block into every 16x16 column and row it covers, and,
675+ /// on the finer 4x4 grid libaom's entropy context arrays are actually
676+ /// kept on, into every unit up to the true frame edge -- the units past
677+ /// it are left at their default (uncoded), even mid-16x16-cell (spec
678+ /// `av1_set_entropy_contexts`, which clamps to `blocks_wide`/`blocks_high`
679+ /// derived from the true `mi_cols`/`mi_rows`, not from this block's own
680+ /// side).
660681 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {
661682 let (r, c) = at;
662683 let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
663684 for cell in 0..side / SUB {
664- self.above[c + cell] = states;
665- self.left[r + cell] = states;
666685 self.above_mode[c + cell] = mode;
667686 self.left_mode[r + cell] = mode;
668687 self.above_side[c + cell] = side;
669688 self.left_side[r + cell] = side;
670689 }
690+ let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);
691+ let valid_h = side_mi.min(self.mi_rows.saturating_sub(mi_r));
692+ let valid_w = side_mi.min(self.mi_cols.saturating_sub(mi_c));
693+ for cell in 0..side_mi {
694+ self.left[mi_r + cell] = if cell < valid_h {
695+ states
696+ } else {
697+ Default::default()
698+ };
699+ self.above[mi_c + cell] = if cell < valid_w {
700+ states
701+ } else {
702+ Default::default()
703+ };
704+ }
671705 }
672706
673707 /// Writes one inter-frame block's skip flag and inter/intra state into
@@ -688,10 +722,14 @@ impl Neighbours {
688722 /// The gathered state of the blocks above and to the left of one block,
689723 /// per plane.
690724 fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] {
725+ let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);
691726 std::array::from_fn(|plane| {
692727 let mut around = Around::default();
693- for cell in 0..side / SUB {
694- let (above, left) = (&self.above[c + cell][plane], &self.left[r + cell][plane]);
728+ for cell in 0..side_mi {
729+ let (above, left) = (
730+ &self.above[mi_c + cell][plane],
731+ &self.left[mi_r + cell][plane],
732+ );
695733 around.above_coded |= above.coded;
696734 around.left_coded |= left.coded;
697735 around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc);
@@ -767,7 +805,12 @@ pub fn sb_coeff_key_frame_tile(
767805 // The blocks above and to the left, on the 16x16 grid. The left edge is
768806 // reset at every superblock row because a tile starts each row with no
769807 // left neighbour.
770- let mut neighbours = Neighbours::new(cols as usize * 2, rows as usize * 2);
808+ let mut neighbours = Neighbours::new(
809+ cols as usize * 2,
810+ rows as usize * 2,
811+ mi_cols as usize,
812+ mi_rows as usize,
813+ );
771814
772815 // The tile adapts every non-literal CDF it writes, exactly as the decoder
773816 // adapts the ones it reads, so the frame header leaves `disable_cdf_update`
@@ -1631,10 +1674,12 @@ fn write_dc_coeffs(
16311674 enc.symbol_fixed(usize::from(dc_level < 0), &cdf::DC_SIGN_LUMA[sign_ctx]);
16321675 }
16331676
1634-/// One neighbour cell's vote in `Dc_Sign_Contexts` (spec 8.3.2): plus one for
1635-/// a positive DC, minus one for a negative one, nothing for a cell whose block
1636-/// carried no DC. Every 4x4 unit of a cell votes the same way, so counting
1637-/// cells and counting units differ only by a factor the sign is blind to.
1677+/// One 4x4 unit's vote in `Dc_Sign_Contexts` (spec 8.3.2): plus one for a
1678+/// positive DC, minus one for a negative one, nothing for a unit whose block
1679+/// carried no DC or that sits past the frame's true edge (spec
1680+/// `av1_set_entropy_contexts`), which is why the vote is gathered per 4x4
1681+/// unit and not per coded cell -- a unit past the edge does not vote even
1682+/// when the rest of its cell does.
16381683 fn dc_vote(dc: Option<bool>) -> i32 {
16391684 match dc {
16401685 None => 0,
@@ -1824,7 +1869,12 @@ pub fn sb_coeff_inter_frame_tile(
18241869 const LAST_FRAME: i8 = 1;
18251870
18261871 let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI);
1827- let mut neighbours = Neighbours::new(cols as usize * 2, rows as usize * 2);
1872+ let mut neighbours = Neighbours::new(
1873+ cols as usize * 2,
1874+ rows as usize * 2,
1875+ mi_cols as usize,
1876+ mi_rows as usize,
1877+ );
18281878 let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize);
18291879 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));
18301880 let mut enc = SymbolEncoder::new();
@@ -2995,7 +3045,7 @@ mod tests {
29953045 /// appeared beside a 32x32 one.
29963046 #[test]
29973047 fn a_block_gathers_the_cells_its_neighbours_cover() {
2998- let mut neighbours = Neighbours::new(4, 4);
3048+ let mut neighbours = Neighbours::new(4, 4, 64, 64);
29993049 let quiet = [
30003050 vec![0i32; TX16 * TX16],
30013051 vec![0; TX8 * TX8],
@@ -3018,8 +3068,9 @@ mod tests {
30183068 "the one negative DC above the block is what it votes"
30193069 );
30203070 // The same read one cell at a time misses it, which is the bug this
3021- // gate is here for.
3022- assert!(!neighbours.above[2][1].coded);
3071+ // gate is here for. `above[8]` is the first 4x4 unit the quiet block
3072+ // at `(0, 2)` wrote (2 SUB units * 4 4x4-units-per-SUB).
3073+ assert!(!neighbours.above[8][1].coded);
30233074 }
30243075
30253076 /// samples.