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;
517 /// Side of the smallest block the writer codes, in samples, which is the grid517 /// Side of the smallest block the writer codes, in samples, which is the grid
518 /// the neighbour bookkeeping is kept on.518 /// the neighbour bookkeeping is kept on.
519 const SUB: usize = 16;519 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;
520 524
521 /// What one coded block leaves behind for the blocks that read it as a525 /// What one coded block leaves behind for the blocks that read it as a
522 /// neighbour: whether it coded anything at all, and the sign of its DC.526 /// neighbour: whether it coded anything at all, and the sign of its DC.
@@ -612,6 +616,13 @@ struct Neighbours {
612 above_inter: Vec<bool>,616 above_inter: Vec<bool>,
613 /// The same, down the left edge.617 /// The same, down the left edge.
614 left_inter: Vec<bool>,618 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,
615 }626 }
616 627
617 /// What one plane's neighbours leave for a block, gathered across every cell628 /// What one plane's neighbours leave for a block, gathered across every cell
@@ -631,11 +642,13 @@ struct Around {
631 642
632 impl Neighbours {643 impl Neighbours {
633 /// The state a tile starts from: a block outside it reads as `DC_PRED`,644 /// The state a tile starts from: a block outside it reads as `DC_PRED`,
634- /// as having coded nothing, and as unsplit.645+ /// as having coded nothing, and as unsplit. `cols`/`rows` are in [`SUB`]
635- fn new(cols: usize, rows: usize) -> Self {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 {
636 Self {649 Self {
637- above: vec![[Neighbour::default(); 3]; cols],650+ above: vec![[Neighbour::default(); 3]; cols * (SUB / MI)],
638- left: vec![[Neighbour::default(); 3]; rows],651+ left: vec![[Neighbour::default(); 3]; rows * (SUB / MI)],
639 above_mode: vec![DC_PRED; cols],652 above_mode: vec![DC_PRED; cols],
640 left_mode: vec![DC_PRED; rows],653 left_mode: vec![DC_PRED; rows],
641 above_side: vec![SB; cols],654 above_side: vec![SB; cols],
@@ -644,6 +657,8 @@ impl Neighbours {
644 left_skip: vec![false; rows],657 left_skip: vec![false; rows],
645 above_inter: vec![false; cols],658 above_inter: vec![false; cols],
646 left_inter: vec![false; rows],659 left_inter: vec![false; rows],
660+ mi_cols,
661+ mi_rows,
647 }662 }
648 }663 }
649 664
@@ -656,18 +671,37 @@ impl Neighbours {
656 self.left_inter.iter_mut().for_each(|i| *i = false);671 self.left_inter.iter_mut().for_each(|i| *i = false);
657 }672 }
658 673
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).
660 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {681 fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) {
661 let (r, c) = at;682 let (r, c) = at;
662 let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));683 let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
663 for cell in 0..side / SUB {684 for cell in 0..side / SUB {
664- self.above[c + cell] = states;
665- self.left[r + cell] = states;
666 self.above_mode[c + cell] = mode;685 self.above_mode[c + cell] = mode;
667 self.left_mode[r + cell] = mode;686 self.left_mode[r + cell] = mode;
668 self.above_side[c + cell] = side;687 self.above_side[c + cell] = side;
669 self.left_side[r + cell] = side;688 self.left_side[r + cell] = side;
670 }689 }
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+ }
671 }705 }
672 706
673 /// Writes one inter-frame block's skip flag and inter/intra state into707 /// Writes one inter-frame block's skip flag and inter/intra state into
@@ -688,10 +722,14 @@ impl Neighbours {
688 /// The gathered state of the blocks above and to the left of one block,722 /// The gathered state of the blocks above and to the left of one block,
689 /// per plane.723 /// per plane.
690 fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] {724 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);
691 std::array::from_fn(|plane| {726 std::array::from_fn(|plane| {
692 let mut around = Around::default();727 let mut around = Around::default();
693- for cell in 0..side / SUB {728+ for cell in 0..side_mi {
694- let (above, left) = (&self.above[c + cell][plane], &self.left[r + cell][plane]);729+ let (above, left) = (
730+ &self.above[mi_c + cell][plane],
731+ &self.left[mi_r + cell][plane],
732+ );
695 around.above_coded |= above.coded;733 around.above_coded |= above.coded;
696 around.left_coded |= left.coded;734 around.left_coded |= left.coded;
697 around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc);735 around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc);
@@ -767,7 +805,12 @@ pub fn sb_coeff_key_frame_tile(
767 // The blocks above and to the left, on the 16x16 grid. The left edge is805 // The blocks above and to the left, on the 16x16 grid. The left edge is
768 // reset at every superblock row because a tile starts each row with no806 // reset at every superblock row because a tile starts each row with no
769 // left neighbour.807 // 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+ );
771 814
772 // The tile adapts every non-literal CDF it writes, exactly as the decoder815 // The tile adapts every non-literal CDF it writes, exactly as the decoder
773 // adapts the ones it reads, so the frame header leaves `disable_cdf_update`816 // adapts the ones it reads, so the frame header leaves `disable_cdf_update`
@@ -1631,10 +1674,12 @@ fn write_dc_coeffs(
1631 enc.symbol_fixed(usize::from(dc_level < 0), &cdf::DC_SIGN_LUMA[sign_ctx]);1674 enc.symbol_fixed(usize::from(dc_level < 0), &cdf::DC_SIGN_LUMA[sign_ctx]);
1632 }1675 }
1633 1676
1634-/// One neighbour cell's vote in `Dc_Sign_Contexts` (spec 8.3.2): plus one for1677+/// One 4x4 unit's vote in `Dc_Sign_Contexts` (spec 8.3.2): plus one for a
1635-/// a positive DC, minus one for a negative one, nothing for a cell whose block1678+/// positive DC, minus one for a negative one, nothing for a unit whose block
1636-/// carried no DC. Every 4x4 unit of a cell votes the same way, so counting1679+/// carried no DC or that sits past the frame's true edge (spec
1637-/// cells and counting units differ only by a factor the sign is blind to.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.
1638 fn dc_vote(dc: Option<bool>) -> i32 {1683 fn dc_vote(dc: Option<bool>) -> i32 {
1639 match dc {1684 match dc {
1640 None => 0,1685 None => 0,
@@ -1824,7 +1869,12 @@ pub fn sb_coeff_inter_frame_tile(
1824 const LAST_FRAME: i8 = 1;1869 const LAST_FRAME: i8 = 1;
1825 1870
1826 let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI);1871 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+ );
1828 let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize);1878 let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize);
1829 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));1879 let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx));
1830 let mut enc = SymbolEncoder::new();1880 let mut enc = SymbolEncoder::new();
@@ -2995,7 +3045,7 @@ mod tests {
2995 /// appeared beside a 32x32 one.3045 /// appeared beside a 32x32 one.
2996 #[test]3046 #[test]
2997 fn a_block_gathers_the_cells_its_neighbours_cover() {3047 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);
2999 let quiet = [3049 let quiet = [
3000 vec![0i32; TX16 * TX16],3050 vec![0i32; TX16 * TX16],
3001 vec![0; TX8 * TX8],3051 vec![0; TX8 * TX8],
@@ -3018,8 +3068,9 @@ mod tests {
3018 "the one negative DC above the block is what it votes"3068 "the one negative DC above the block is what it votes"
3019 );3069 );
3020 // The same read one cell at a time misses it, which is the bug this3070 // The same read one cell at a time misses it, which is the bug this
3021- // gate is here for.3071+ // gate is here for. `above[8]` is the first 4x4 unit the quiet block
3022- assert!(!neighbours.above[2][1].coded);3072+ // at `(0, 2)` wrote (2 SUB units * 4 4x4-units-per-SUB).
3073+ assert!(!neighbours.above[8][1].coded);
3023 }3074 }
3024 3075
3025 /// samples.3076 /// samples.