Gather dc-sign context per 4x4 unit, clamped to the true edge
Browse files at this commit77b7e148bdb4c08a2ab6dca16d6cbf7a026fd475Parent: dcd1644- fix
- gather dc-sign context per 4x4 unit, clamped to the true edge
1 changed file +69 -18
- crates/ec-av1/src/tile.rs +69 -18
crates/ec-av1/src/tile.rsmodified+69 -18
| @@ -517,6 +517,10 @@ const SB: usize = 64; | ||
| 517 | 517 | /// Side of the smallest block the writer codes, in samples, which is the grid |
| 518 | 518 | /// the neighbour bookkeeping is kept on. |
| 519 | 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 | 525 | /// What one coded block leaves behind for the blocks that read it as a |
| 522 | 526 | /// neighbour: whether it coded anything at all, and the sign of its DC. |
| @@ -612,6 +616,13 @@ struct Neighbours { | ||
| 612 | 616 | above_inter: Vec<bool>, |
| 613 | 617 | /// The same, down the left edge. |
| 614 | 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 | 628 | /// What one plane's neighbours leave for a block, gathered across every cell |
| @@ -631,11 +642,13 @@ struct Around { | ||
| 631 | 642 | |
| 632 | 643 | impl Neighbours { |
| 633 | 644 | /// 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 { | |
| 636 | 649 | 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)], | |
| 639 | 652 | above_mode: vec![DC_PRED; cols], |
| 640 | 653 | left_mode: vec![DC_PRED; rows], |
| 641 | 654 | above_side: vec![SB; cols], |
| @@ -644,6 +657,8 @@ impl Neighbours { | ||
| 644 | 657 | left_skip: vec![false; rows], |
| 645 | 658 | above_inter: vec![false; cols], |
| 646 | 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 | 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 | 681 | fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec<i32>; 3]) { |
| 661 | 682 | let (r, c) = at; |
| 662 | 683 | let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); |
| 663 | 684 | for cell in 0..side / SUB { |
| 664 | - self.above[c + cell] = states; | |
| 665 | - self.left[r + cell] = states; | |
| 666 | 685 | self.above_mode[c + cell] = mode; |
| 667 | 686 | self.left_mode[r + cell] = mode; |
| 668 | 687 | self.above_side[c + cell] = side; |
| 669 | 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 | 707 | /// Writes one inter-frame block's skip flag and inter/intra state into |
| @@ -688,10 +722,14 @@ impl Neighbours { | ||
| 688 | 722 | /// The gathered state of the blocks above and to the left of one block, |
| 689 | 723 | /// per plane. |
| 690 | 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 | 726 | std::array::from_fn(|plane| { |
| 692 | 727 | 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 | + ); | |
| 695 | 733 | around.above_coded |= above.coded; |
| 696 | 734 | around.left_coded |= left.coded; |
| 697 | 735 | around.dc_vote += dc_vote(above.dc) + dc_vote(left.dc); |
| @@ -767,7 +805,12 @@ pub fn sb_coeff_key_frame_tile( | ||
| 767 | 805 | // The blocks above and to the left, on the 16x16 grid. The left edge is |
| 768 | 806 | // reset at every superblock row because a tile starts each row with no |
| 769 | 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 | 815 | // The tile adapts every non-literal CDF it writes, exactly as the decoder |
| 773 | 816 | // adapts the ones it reads, so the frame header leaves `disable_cdf_update` |
| @@ -1631,10 +1674,12 @@ fn write_dc_coeffs( | ||
| 1631 | 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 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. | |
| 1638 | 1683 | fn dc_vote(dc: Option<bool>) -> i32 { |
| 1639 | 1684 | match dc { |
| 1640 | 1685 | None => 0, |
| @@ -1824,7 +1869,12 @@ pub fn sb_coeff_inter_frame_tile( | ||
| 1824 | 1869 | const LAST_FRAME: i8 = 1; |
| 1825 | 1870 | |
| 1826 | 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 | 1878 | let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); |
| 1829 | 1879 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 1830 | 1880 | let mut enc = SymbolEncoder::new(); |
| @@ -2995,7 +3045,7 @@ mod tests { | ||
| 2995 | 3045 | /// appeared beside a 32x32 one. |
| 2996 | 3046 | #[test] |
| 2997 | 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 | 3049 | let quiet = [ |
| 3000 | 3050 | vec![0i32; TX16 * TX16], |
| 3001 | 3051 | vec![0; TX8 * TX8], |
| @@ -3018,8 +3068,9 @@ mod tests { | ||
| 3018 | 3068 | "the one negative DC above the block is what it votes" |
| 3019 | 3069 | ); |
| 3020 | 3070 | // 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); | |
| 3023 | 3074 | } |
| 3024 | 3075 | |
| 3025 | 3076 | /// samples. |