forge
Explore
Log in

Don't you dare go Hollow

Port partial-superblock support to the inter tile writer

Browse files at this commit

68e9a25f55cc1d3a1061cbcbc09ad08ae9c798afAhmet Kaan GümüşParent: ad1509f
  • fix
    • port partial-superblock support to the inter tile writer
Show full message
fix(av1): port partial-superblock support to the inter tile writer

Three fixes closing the remaining true-size reds:

1. a_frame_round_trips_at_its_own_size (1920x1080): encode.rs's split-32x32
   search unconditionally pushed all 4 16x16 sub-block trials into
   Quadrant::Split, but tile.rs's writer (mirroring spec decode_partition's
   r>=MiRows||c>=MiCols early return) only expects entries for sub-blocks
   whose own mi origin is inside the true frame. Filter the search loop to
   match, same predicate both sides.

2. a_picture_off_the_block_grid_is_refused: the 32x48 case in this test was
   a symptom of bug 1 (a block-aligned 48-row true height was refused only
   because the split path demanded 4 subs where just 2 are legal), not a
   genuine 8x8-split requirement -- confirmed by adding it to
   ffmpeg_decodes_exactly_what_the_encoder_reconstructed, where it now
   round-trips pixel-exact. Updated the test's expectations to match.

3. sequence_round_trips_at_a_non_multiple_size (160x96, inter): three
   compounding bugs in the inter path, which had zero partial-superblock
   support before this round:
   - sb_coeff_inter_frame_tile checked check_superblocks (whole-SB-multiple
     only) and used mi_cols/mi_rows / SB_MI, dropping any partial trailing
     superblock; switched to check_blocks and the same block_grid ceiling
     sb_coeff_key_frame_tile uses, plus its three-way has_cols/has_rows
     superblock-level signaling (PARTITION_SPLIT / gathered VERT_ALIKE or
     HORZ_ALIKE / nothing) and quadrant-position filtering. A 32x32 quadrant
     that itself straddles the true edge is refused (this writer has no
     rectangular/further-split representation at that level), mirroring the
     key frame writer's own "cannot be left whole" refusal.
   - encode_inter_frame's block-search loop used picture.width/BLOCK (the
     padded coding surface) for its block grid instead of the true,
     mi_cols-derived grid, overrunning the tile writer's expected block
     count whenever the true size is not a 64-multiple.
   - encode_inter_frame built its FrameHeader from the padded picture's own
     width/height instead of `render` (the true size), the same render vs.
     padded-picture split encode_key_frame_inner already makes for
     key_frame_headers_colour -- this alone produced a bitstream dav1d
     rejected outright ("Invalid data") since inter frame_width/mi_cols
     disagreed with the true size the key frame declared.

Gate 122/3/4 (round start) -> 126/1/4: one chroma-content mismatch remains
on the first inter frame at 160x96, unrelated to true-size handling (all
quadrants there are already block-aligned, no partial-block path involved)
-- a pre-existing inter-frame defect this size never exercised before,
left for its own round.
crates/ec-av1/src/encode.rsmodified+41 -14
@@ -1264,9 +1264,20 @@ pub(crate) fn encode_key_frame_inner(
1264 let mut split = Vec::with_capacity(4);1264 let mut split = Vec::with_capacity(4);
1265 let mut cost_split = search.lambda1265 let mut cost_split = search.lambda
1266 * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false));1266 * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false));
1267- let mut split_modes = [DC_PRED; 4];1267+ let mut split_modes = Vec::with_capacity(4);
1268- for (sub, sub_mode) in split_modes.iter_mut().enumerate() {1268+ for sub in 0..4 {
1269 let (sc, sr) = (c0 + sub % 2, r0 + sub / 2);1269 let (sc, sr) = (c0 + sub % 2, r0 + sub / 2);
1270+ // Same filter as the writer's `sub_positions` (spec
1271+ // `decode_partition`'s `r >= MiRows || c >= MiCols` early
1272+ // return): a sub-block whose own origin sits past the
1273+ // true frame is never coded, so it must not be searched
1274+ // or pushed onto `split` either, or the writer's block
1275+ // count will not match what it is prepared to name.
1276+ if (sr as u32) * crate::tile::SUB_MI >= header.mi_rows
1277+ || (sc as u32) * crate::tile::SUB_MI >= header.mi_cols
1278+ {
1279+ continue;
1280+ }
1270 let (block, cost) = code_square(1281 let (block, cost) = code_square(
1271 &mut luma,1282 &mut luma,
1272 &mut chroma,1283 &mut chroma,
@@ -1276,7 +1287,7 @@ pub(crate) fn encode_key_frame_inner(
1276 &mode_bits(above_mode[sc], left_mode[sr]),1287 &mode_bits(above_mode[sc], left_mode[sr]),
1277 );1288 );
1278 cost_split += cost;1289 cost_split += cost;
1279- *sub_mode = block.mode;1290+ split_modes.push(block.mode);
1280 above_mode[sc] = block.mode;1291 above_mode[sc] = block.mode;
1281 left_mode[sr] = block.mode;1292 left_mode[sr] = block.mode;
1282 split.push(block);1293 split.push(block);
@@ -1833,13 +1844,14 @@ pub(crate) fn encode_inter_frame(
1833 // overwrites slot 0, so the frame just coded is always what the next one1844 // overwrites slot 0, so the frame just coded is always what the next one
1834 // predicts against.1845 // predicts against.
1835 const LAST_SLOT: u8 = 0;1846 const LAST_SLOT: u8 = 0;
1836- let (seq, mut header) = inter_frame_headers(1847+ // `render`, not `picture.width`/`picture.height`: `picture` here is
1837- picture.width,1848+ // already padded to a whole number of superblocks (`encode_sequence`'s
1838- picture.height,1849+ // `padded_to(SUPERBLOCK)`), and the header's `mi_cols`/`mi_rows` (and so
1839- base_q_idx,1850+ // every true-edge clamp downstream) must come from the frame's real,
1840- order_hint,1851+ // unpadded size, same as `key_frame_headers_colour` takes `render` and
1841- LAST_SLOT,1852+ // not the padded picture in `encode_key_frame_inner`.
1842- )?;1853+ let (seq, mut header) =
1854+ inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?;
1843 header.render_width = render.0 as u32;1855 header.render_width = render.0 as u32;
1844 header.render_height = render.1 as u32;1856 header.render_height = render.1 as u32;
1845 1857
@@ -1880,8 +1892,14 @@ pub(crate) fn encode_inter_frame(
1880 };1892 };
1881 let mode_bits_table = inter_mode_bits();1893 let mode_bits_table = inter_mode_bits();
1882 1894
1883- let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK);1895+ // The true grid ([`crate::tile::block_grid`]'s ceiling), not the padded
1884- let (sb_cols, sb_rows) = (cols / 2, rows / 2);1896+ // coding surface's: `blocks` carries one entry per 32x32 block the tile
1897+ // writer will actually visit, same count it checks `blocks.len()`
1898+ // against, which is fewer than the padded surface's own block count
1899+ // whenever the true size is not a 64x64 multiple.
1900+ let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows);
1901+ let (cols, rows) = (cols as usize, rows as usize);
1902+ let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2));
1885 let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize);1903 let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize);
1886 let mut grid = MiGrid::new(mi_cols, mi_rows);1904 let mut grid = MiGrid::new(mi_cols, mi_rows);
1887 let mut blocks = vec![BlockCoeffs::default(); cols * rows];1905 let mut blocks = vec![BlockCoeffs::default(); cols * rows];
@@ -1890,6 +1908,11 @@ pub(crate) fn encode_inter_frame(
1890 for sb_c in 0..sb_cols {1908 for sb_c in 0..sb_cols {
1891 for quadrant in 0..4 {1909 for quadrant in 0..4 {
1892 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);1910 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);
1911+ // Same filter as the tile writer's: a quadrant whose own mi
1912+ // origin is not inside the true frame is never coded.
1913+ if r32 >= rows || c32 >= cols {
1914+ continue;
1915+ }
1893 let (x, y) = (c32 * BLOCK, r32 * BLOCK);1916 let (x, y) = (c32 * BLOCK, r32 * BLOCK);
1894 let (mi_row, mi_col) = (r32 * 8, c32 * 8);1917 let (mi_row, mi_col) = (r32 * 8, c32 * 8);
1895 let stack =1918 let stack =
@@ -2140,7 +2163,7 @@ mod tests {
2140 eprintln!("SKIP ffmpeg_decodes_exactly_what_the_encoder_reconstructed: no ffmpeg");2163 eprintln!("SKIP ffmpeg_decodes_exactly_what_the_encoder_reconstructed: no ffmpeg");
2141 return;2164 return;
2142 }2165 }
2143- for &(width, height) in &[(64usize, 64usize), (96, 64), (160, 96)] {2166+ for &(width, height) in &[(64usize, 64usize), (96, 64), (160, 96), (32, 48)] {
2144 let picture = test_card(width, height);2167 let picture = test_card(width, height);
2145 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();2168 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
2146 let decoded = ffmpeg_decode(&encoded.stream, width, height);2169 let decoded = ffmpeg_decode(&encoded.stream, width, height);
@@ -3036,7 +3059,11 @@ mod tests {
3036 msg.contains("true frame")3059 msg.contains("true frame")
3037 };3060 };
3038 assert!(refused(40, 32));3061 assert!(refused(40, 32));
3039- assert!(refused(32, 48));3062+ // 32x48 was refused here through round 9: its split quadrant only
3063+ // ever has two legal 16x16 subs (the other two sit past the true
3064+ // 48-row edge, block-aligned, no 8x8 split needed), but the writer
3065+ // demanded four. See `ffmpeg_decodes_exactly_what_the_encoder_
3066+ // reconstructed`, which now round-trips it pixel-exact instead.
3040 let mut short = Picture::grey(64, 64);3067 let mut short = Picture::grey(64, 64);
3041 short.u.truncate(10);3068 short.u.truncate(10);
3042 assert!(encode_key_frame(&short, 100, 0.5).is_err());3069 assert!(encode_key_frame(&short, 100, 0.5).is_err());
crates/ec-av1/src/tile.rsmodified+52 -5
@@ -1859,12 +1859,16 @@ pub fn sb_coeff_inter_frame_tile(
1859 base_q_idx: u8,1859 base_q_idx: u8,
1860 blocks: &[BlockCoeffs],1860 blocks: &[BlockCoeffs],
1861 ) -> Result<Vec<u8>> {1861 ) -> Result<Vec<u8>> {
1862- check_superblocks(mi_cols, mi_rows)?;1862+ check_blocks(mi_cols, mi_rows)?;
1863- let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI);1863+ // `block_grid`'s ceiling, not a plain division: a true frame size that is
1864+ // not a whole number of 32x32 blocks (or of 64x64 superblocks) still has
1865+ // one more block/superblock whose own origin is inside the true frame,
1866+ // same as `sb_coeff_key_frame_tile`.
1867+ let (cols, rows) = block_grid(mi_cols, mi_rows);
1864 if blocks.len() != (cols * rows) as usize {1868 if blocks.len() != (cols * rows) as usize {
1865 return Err(Error::unsupported(1869 return Err(Error::unsupported(
1866 "AV1 tile",1870 "AV1 tile",
1867- "an inter frame needs one entry per 32x32 block",1871+ "an inter frame needs one entry per 32x32 block inside the true frame",
1868 ));1872 ));
1869 }1873 }
1870 if let Some(bad) = blocks1874 if let Some(bad) = blocks
@@ -1887,7 +1891,7 @@ pub fn sb_coeff_inter_frame_tile(
1887 /// writer's single-reference chain ever names.1891 /// writer's single-reference chain ever names.
1888 const LAST_FRAME: i8 = 1;1892 const LAST_FRAME: i8 = 1;
1889 1893
1890- let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI);1894+ let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2));
1891 let mut neighbours = Neighbours::new(1895 let mut neighbours = Neighbours::new(
1892 cols as usize * 2,1896 cols as usize * 2,
1893 rows as usize * 2,1897 rows as usize * 2,
@@ -1916,14 +1920,57 @@ pub fn sb_coeff_inter_frame_tile(
1916 for sb_c in 0..sb_cols {1920 for sb_c in 0..sb_cols {
1917 let sb_at = (sb_r as usize * 4, sb_c as usize * 4);1921 let sb_at = (sb_r as usize * 4, sb_c as usize * 4);
1918 let sb_ctx = neighbours.partition_ctx(sb_at, SB);1922 let sb_ctx = neighbours.partition_ctx(sb_at, SB);
1919- enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]);1923+ // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock
1924+ // whose bottom or right half falls outside the true frame cannot
1925+ // be left whole, but this writer only ever splits a superblock
1926+ // into its four 32x32 quadrants (never `PARTITION_NONE` at 64x64),
1927+ // so the only question is which of the three partition symbols
1928+ // that split takes — same three-way signaling as
1929+ // `sb_coeff_key_frame_tile`'s superblock level.
1930+ let (has_cols, has_rows) = (
1931+ sb_c * SB_MI + SB_MI / 2 < mi_cols,
1932+ sb_r * SB_MI + SB_MI / 2 < mi_rows,
1933+ );
1934+ match (has_cols, has_rows) {
1935+ (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]),
1936+ (true, false) => {
1937+ enc.symbol_fixed(1, &gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE));
1938+ }
1939+ (false, true) => {
1940+ enc.symbol_fixed(1, &gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE));
1941+ }
1942+ (false, false) => {}
1943+ }
1920 1944
1921 for quadrant in 0..4 {1945 for quadrant in 0..4 {
1922 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);1946 let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2);
1947+ // Only a quadrant whose own mi origin is inside the true
1948+ // frame is coded at all (spec `decode_partition`'s
1949+ // `r >= MiRows || c >= MiCols` early return), same filter as
1950+ // `sb_coeff_key_frame_tile`'s `quadrant_positions`.
1951+ if r32 >= rows || c32 >= cols {
1952+ continue;
1953+ }
1923 let block = &blocks[(r32 * cols + c32) as usize];1954 let block = &blocks[(r32 * cols + c32) as usize];
1924 let at = (r32 as usize * 2, c32 as usize * 2);1955 let at = (r32 as usize * 2, c32 as usize * 2);
1925 let (r, c) = at;1956 let (r, c) = at;
1926 let ctx32 = neighbours.partition_ctx(at, BLOCK);1957 let ctx32 = neighbours.partition_ctx(at, BLOCK);
1958+ // This writer has no rectangular (HORZ/VERT) or further-split
1959+ // representation for a 32x32 block, so one that itself
1960+ // straddles the true edge is refused rather than miscoded --
1961+ // mirroring `sb_coeff_key_frame_tile`'s "cannot be left whole"
1962+ // refusal, just with no split arm to fall back to here.
1963+ let (has_cols32, has_rows32) = (
1964+ has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols),
1965+ has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows),
1966+ );
1967+ if !has_cols32 || !has_rows32 {
1968+ return Err(Error::unsupported(
1969+ "AV1 tile",
1970+ "a 32x32 inter-frame block that straddles the true frame edge \
1971+ needs an 8x8 split this writer does not code",
1972+ ));
1973+ }
1927 enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx32]);1974 enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx32]);
1928 1975
1929 let has_above = r32 > 0;1976 let has_above = r32 > 0;