Merge branch 'lane-av1-partial': partial superblocks at the frame edge
Browse files at this commit1 changed file +181 -9
- crates/ec-av1/src/tile.rs +181 -9
crates/ec-av1/src/tile.rsmodified+181 -9
| @@ -18,6 +18,19 @@ use crate::msac::SymbolEncoder; | |||
| 18 | const PARTITION_NONE: usize = 0; | 18 | const PARTITION_NONE: usize = 0; |
| 19 | /// `PARTITION_SPLIT` (spec 6.10.4): the block cut into four quadrants. | 19 | /// `PARTITION_SPLIT` (spec 6.10.4): the block cut into four quadrants. |
| 20 | const PARTITION_SPLIT: usize = 3; | 20 | const PARTITION_SPLIT: usize = 3; |
| 21 | + | ||
| 22 | +/// The partition types whose probability mass the split-or-horizontal flag of | ||
| 23 | +/// a superblock hanging off the bottom of the frame gathers (spec 9.3, | ||
| 24 | +/// `partition_gather_vert_alike`): the flag says split, so everything vertical | ||
| 25 | +/// or split lands on it. | ||
| 26 | +const VERT_ALIKE: [usize; 6] = [2, PARTITION_SPLIT, 4, 6, 7, 9]; | ||
| 27 | + | ||
| 28 | +/// The same for a superblock hanging off the right-hand edge | ||
| 29 | +/// (`partition_gather_horz_alike`). | ||
| 30 | +const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; | ||
| 31 | + | ||
| 32 | +/// Mode-info units across a 32x32 block. | ||
| 33 | +const BLOCK_MI: u32 = SB_MI / 2; | ||
| 21 | /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. | 34 | /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. |
| 22 | const DC_PRED: usize = 0; | 35 | const DC_PRED: usize = 0; |
| 23 | 36 | ||
| @@ -61,6 +74,19 @@ const Q_CTX_2: std::ops::RangeInclusive<u8> = 61..=120; | |||
| 61 | 74 | ||
| 62 | /// Both writers here code whole superblocks only: a partial one forces the | 75 | /// Both writers here code whole superblocks only: a partial one forces the |
| 63 | /// partition syntax down the block tree, which they do not code yet. | 76 | /// partition syntax down the block tree, which they do not code yet. |
| 77 | +/// The probability the CDF gives one symbol on its own. | ||
| 78 | +fn element_prob(cdf: &[u16], element: usize) -> u16 { | ||
| 79 | + cdf[element] - if element > 0 { cdf[element - 1] } else { 0 } | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +/// The two-symbol CDF a partial superblock's partition flag is coded with: the | ||
| 83 | +/// mass of the listed partition types becomes the probability of a split, and | ||
| 84 | +/// the rest is the one partition the frame edge still allows. | ||
| 85 | +fn gather(cdf: &[u16], elements: [usize; 6]) -> [u16; 3] { | ||
| 86 | + let split: u16 = elements.iter().map(|&e| element_prob(cdf, e)).sum(); | ||
| 87 | + [32768 - split, 32768, 0] | ||
| 88 | +} | ||
| 89 | + | ||
| 64 | fn check_superblocks(mi_cols: u32, mi_rows: u32) -> Result<()> { | 90 | fn check_superblocks(mi_cols: u32, mi_rows: u32) -> Result<()> { |
| 65 | if mi_cols == 0 | 91 | if mi_cols == 0 |
| 66 | || mi_rows == 0 | 92 | || mi_rows == 0 |
| @@ -391,21 +417,43 @@ struct Neighbour { | |||
| 391 | dc: Option<bool>, | 417 | dc: Option<bool>, |
| 392 | } | 418 | } |
| 393 | 419 | ||
| 420 | +/// Rejects a frame the 32x32 block grid cannot tile. | ||
| 421 | +/// | ||
| 422 | +/// The frame need not be a whole number of superblocks — a superblock at the | ||
| 423 | +/// right-hand or bottom edge may be half outside it — but every 32x32 block | ||
| 424 | +/// that is coded has to be wholly inside, because a block that hangs over the | ||
| 425 | +/// edge has to be coded as a rectangle this writer has no transform for. | ||
| 426 | +fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { | ||
| 427 | + if mi_cols == 0 | ||
| 428 | + || mi_rows == 0 | ||
| 429 | + || !mi_cols.is_multiple_of(BLOCK_MI) | ||
| 430 | + || !mi_rows.is_multiple_of(BLOCK_MI) | ||
| 431 | + { | ||
| 432 | + return Err(Error::unsupported( | ||
| 433 | + "AV1 tile", | ||
| 434 | + "a coefficient key frame is written only for frames that are a \ | ||
| 435 | + whole number of 32x32 blocks", | ||
| 436 | + )); | ||
| 437 | + } | ||
| 438 | + Ok(()) | ||
| 439 | +} | ||
| 440 | + | ||
| 394 | /// Writes the payload of a one-tile key frame in which every superblock is | 441 | /// Writes the payload of a one-tile key frame in which every superblock is |
| 395 | -/// split into four 32x32 DC-predicted blocks, each carrying the coefficients | 442 | +/// split into 32x32 DC-predicted blocks, each carrying the coefficients |
| 396 | /// `blocks` gives it. | 443 | /// `blocks` gives it. |
| 397 | /// | 444 | /// |
| 398 | /// `blocks` gives one coefficient set per 32x32 block in raster order across | 445 | /// `blocks` gives one coefficient set per 32x32 block in raster order across |
| 399 | -/// the frame, so the grid is twice as wide and twice as tall as the superblock | 446 | +/// the frame. The frame need not be a whole number of superblocks: a |
| 400 | -/// grid. Each set carries a 32x32 luma transform and the 16x16 transform of | 447 | +/// superblock at the right-hand or bottom edge may be half outside it, and the |
| 448 | +/// blocks that would sit in the half outside are not coded. Each set carries a 32x32 luma transform and the 16x16 transform of | ||
| 401 | /// each chroma plane; an empty list writes an all-zero transform block. Unlike | 449 | /// each chroma plane; an empty list writes an all-zero transform block. Unlike |
| 402 | /// the DC writers, the coefficients may sit anywhere in their transform, so a | 450 | /// the DC writers, the coefficients may sit anywhere in their transform, so a |
| 403 | /// block here is a picture rather than a flat grey: a single coefficient off | 451 | /// block here is a picture rather than a flat grey: a single coefficient off |
| 404 | /// the origin decodes to the basis function it selects. | 452 | /// the origin decodes to the basis function it selects. |
| 405 | /// | 453 | /// |
| 406 | /// # Errors | 454 | /// # Errors |
| 407 | -/// Returns an error when the frame is not a whole number of 64x64 | 455 | +/// Returns an error when the frame is not a whole number of 32x32 blocks, |
| 408 | -/// superblocks, when `blocks` does not carry exactly one set per 32x32 block, | 456 | +/// when `blocks` does not carry exactly one set per 32x32 block, |
| 409 | /// when a coefficient sits outside its transform, repeats a position, carries | 457 | /// when a coefficient sits outside its transform, repeats a position, carries |
| 410 | /// a zero level or one wider than the base and base-range syntax reach, or | 458 | /// a zero level or one wider than the base and base-range syntax reach, or |
| 411 | /// when `base_q_idx` is outside the q-context band whose default CDFs this | 459 | /// when `base_q_idx` is outside the q-context band whose default CDFs this |
| @@ -416,9 +464,9 @@ pub fn split_coeff_key_frame_tile( | |||
| 416 | base_q_idx: u8, | 464 | base_q_idx: u8, |
| 417 | blocks: &[BlockCoeffs], | 465 | blocks: &[BlockCoeffs], |
| 418 | ) -> Result<Vec<u8>> { | 466 | ) -> Result<Vec<u8>> { |
| 419 | - check_superblocks(mi_cols, mi_rows)?; | 467 | + check_blocks(mi_cols, mi_rows)?; |
| 420 | - let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); | 468 | + let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); |
| 421 | - let (cols, rows) = (sb_cols * 2, sb_rows * 2); | 469 | + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 422 | if blocks.len() != (cols * rows) as usize { | 470 | if blocks.len() != (cols * rows) as usize { |
| 423 | return Err(Error::unsupported( | 471 | return Err(Error::unsupported( |
| 424 | "AV1 tile", | 472 | "AV1 tile", |
| @@ -473,10 +521,28 @@ pub fn split_coeff_key_frame_tile( | |||
| 473 | left_mode.iter_mut().for_each(|m| *m = DC_PRED); | 521 | left_mode.iter_mut().for_each(|m| *m = DC_PRED); |
| 474 | for sb_c in 0..sb_cols { | 522 | for sb_c in 0..sb_cols { |
| 475 | let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); | 523 | let ctx = 2 * usize::from(sb_c > 0) + usize::from(sb_r > 0); |
| 476 | - enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]); | 524 | + // A superblock whose bottom or right half is outside the frame |
| 525 | + // cannot be left unsplit, so the decoder reads a flag instead of | ||
| 526 | + // the partition symbol — and reads nothing at all when both halves | ||
| 527 | + // are outside, where the split is the only partition left. | ||
| 528 | + let (has_cols, has_rows) = ( | ||
| 529 | + sb_c * SB_MI + SB_MI / 2 < mi_cols, | ||
| 530 | + sb_r * SB_MI + SB_MI / 2 < mi_rows, | ||
| 531 | + ); | ||
| 532 | + match (has_cols, has_rows) { | ||
| 533 | + (true, true) => enc.symbol_fixed(PARTITION_SPLIT, &cdf::PARTITION_W64[ctx]), | ||
| 534 | + (true, false) => enc.symbol_fixed(1, &gather(&cdf::PARTITION_W64[ctx], VERT_ALIKE)), | ||
| 535 | + (false, true) => enc.symbol_fixed(1, &gather(&cdf::PARTITION_W64[ctx], HORZ_ALIKE)), | ||
| 536 | + (false, false) => {} | ||
| 537 | + } | ||
| 477 | 538 | ||
| 478 | for quadrant in 0..4 { | 539 | for quadrant in 0..4 { |
| 479 | let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); | 540 | let (r, c) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); |
| 541 | + if r >= rows || c >= cols { | ||
| 542 | + // The quadrant is outside the frame: the decoder never | ||
| 543 | + // walks into it. | ||
| 544 | + continue; | ||
| 545 | + } | ||
| 480 | let (r, c) = (r as usize, c as usize); | 546 | let (r, c) = (r as usize, c as usize); |
| 481 | let block = &grids[r * cols as usize + c]; | 547 | let block = &grids[r * cols as usize + c]; |
| 482 | let mode = usize::from(blocks[r * cols as usize + c].mode); | 548 | let mode = usize::from(blocks[r * cols as usize + c].mode); |
| @@ -1735,6 +1801,112 @@ mod tests { | |||
| 1735 | .collect() | 1801 | .collect() |
| 1736 | } | 1802 | } |
| 1737 | 1803 | ||
| 1804 | + /// Encodes a key frame of the given size, whose 32x32 blocks carry | ||
| 1805 | + /// `blocks` in raster order, and hands back its luma plane as rows of | ||
| 1806 | + /// samples. | ||
| 1807 | + fn decode_luma_at(width: usize, height: usize, blocks: &[BlockCoeffs]) -> Vec<Vec<u8>> { | ||
| 1808 | + let (seq, header) = frame_of(width as u32, height as u32); | ||
| 1809 | + let tile = | ||
| 1810 | + split_coeff_key_frame_tile(header.mi_cols, header.mi_rows, Q_IDX, blocks).unwrap(); | ||
| 1811 | + let mut stream = temporal_delimiter(); | ||
| 1812 | + stream.extend_from_slice(&sequence_header_obu(&seq).unwrap()); | ||
| 1813 | + stream.extend_from_slice(&frame_obu(&seq, &header, &tile).unwrap()); | ||
| 1814 | + let planes = ffmpeg_decode(&stream, width, height); | ||
| 1815 | + planes[..width * height] | ||
| 1816 | + .chunks_exact(width) | ||
| 1817 | + .map(<[u8]>::to_vec) | ||
| 1818 | + .collect() | ||
| 1819 | + } | ||
| 1820 | + | ||
| 1821 | + /// Every row of a block is the same row, and that row falls from left to | ||
| 1822 | + /// right — the shape of a coefficient at row 0, column 1. | ||
| 1823 | + fn assert_falls_across(block: &[Vec<u8>], name: &str) { | ||
| 1824 | + for row in block { | ||
| 1825 | + assert_eq!( | ||
| 1826 | + row, &block[0], | ||
| 1827 | + "{name}: every row of the block is the same row" | ||
| 1828 | + ); | ||
| 1829 | + } | ||
| 1830 | + assert!( | ||
| 1831 | + block[0][0] > block[0][31], | ||
| 1832 | + "{name}: the row falls from left to right, got {} then {}", | ||
| 1833 | + block[0][0], | ||
| 1834 | + block[0][31] | ||
| 1835 | + ); | ||
| 1836 | + } | ||
| 1837 | + | ||
| 1838 | + /// Every column of a block is constant across it and falls down it — the | ||
| 1839 | + /// shape of a coefficient at row 1, column 0. | ||
| 1840 | + fn assert_falls_down(block: &[Vec<u8>], name: &str) { | ||
| 1841 | + for (y, row) in block.iter().enumerate() { | ||
| 1842 | + assert!( | ||
| 1843 | + row.iter().all(|&s| s == row[0]), | ||
| 1844 | + "{name}: row {y} of the block is one constant" | ||
| 1845 | + ); | ||
| 1846 | + } | ||
| 1847 | + assert!( | ||
| 1848 | + block[0][0] > block[31][0], | ||
| 1849 | + "{name}: the column falls down the block, got {} then {}", | ||
| 1850 | + block[0][0], | ||
| 1851 | + block[31][0] | ||
| 1852 | + ); | ||
| 1853 | + } | ||
| 1854 | + | ||
| 1855 | + /// A frame need not be a whole number of superblocks. A 96x64 frame is a | ||
| 1856 | + /// superblock and a half across: the second superblock has no right-hand | ||
| 1857 | + /// half, so its partition is a gathered flag rather than the full symbol, | ||
| 1858 | + /// and the two blocks that would sit in that half are never coded at all. | ||
| 1859 | + /// Each of the six blocks that do exist carries one of two basis functions, | ||
| 1860 | + /// alternating, so a block written into the wrong place or a flag the | ||
| 1861 | + /// decoder reads as a different number of bits shows up as a block with the | ||
| 1862 | + /// other block's shape — or as a decode failure. | ||
| 1863 | + #[test] | ||
| 1864 | + fn a_frame_that_is_not_a_whole_number_of_superblocks_codes_every_block() { | ||
| 1865 | + if !have_ffmpeg() { | ||
| 1866 | + eprintln!( | ||
| 1867 | + "SKIP a_frame_that_is_not_a_whole_number_of_superblocks_codes_every_block: no \ | ||
| 1868 | + ffmpeg" | ||
| 1869 | + ); | ||
| 1870 | + return; | ||
| 1871 | + } | ||
| 1872 | + let blocks: Vec<BlockCoeffs> = (0..6) | ||
| 1873 | + .map(|i| { | ||
| 1874 | + let (row, col) = if i % 2 == 0 { (0, 1) } else { (1, 0) }; | ||
| 1875 | + BlockCoeffs::from(vec![Coeff { | ||
| 1876 | + row, | ||
| 1877 | + col, | ||
| 1878 | + level: 12, | ||
| 1879 | + }]) | ||
| 1880 | + }) | ||
| 1881 | + .collect(); | ||
| 1882 | + let rows = decode_luma_at(96, 64, &blocks); | ||
| 1883 | + for block_row in 0..2 { | ||
| 1884 | + for block_col in 0..3 { | ||
| 1885 | + let name = format!("block ({block_row},{block_col})"); | ||
| 1886 | + let block = block_at(&rows, block_row, block_col); | ||
| 1887 | + if (block_row * 3 + block_col) % 2 == 0 { | ||
| 1888 | + assert_falls_across(&block, &name); | ||
| 1889 | + } else { | ||
| 1890 | + assert_falls_down(&block, &name); | ||
| 1891 | + } | ||
| 1892 | + } | ||
| 1893 | + } | ||
| 1894 | + } | ||
| 1895 | + | ||
| 1896 | + /// A frame whose blocks do not tile it is refused rather than written as a | ||
| 1897 | + /// stream a decoder walks off the end of. | ||
| 1898 | + #[test] | ||
| 1899 | + fn a_frame_that_is_not_a_whole_number_of_blocks_is_refused() { | ||
| 1900 | + let (_, header) = frame_of(80, 64); | ||
| 1901 | + let blocks = vec![BlockCoeffs::default(); 4]; | ||
| 1902 | + let err = split_coeff_key_frame_tile(header.mi_cols, header.mi_rows, Q_IDX, &blocks) | ||
| 1903 | + .expect_err("80 is not a whole number of 32x32 blocks"); | ||
| 1904 | + assert!( | ||
| 1905 | + format!("{err}").contains("32x32"), | ||
| 1906 | + "the refusal names the block size, got {err}" | ||
| 1907 | + ); | ||
| 1908 | + } | ||
| 1909 | + | ||
| 1738 | /// A directional intra mode says where its prediction comes from, and with | 1910 | /// A directional intra mode says where its prediction comes from, and with |
| 1739 | /// no residual of its own a block must be exactly that prediction. The | 1911 | /// no residual of its own a block must be exactly that prediction. The |
| 1740 | /// top-left block is given a basis function that is flat across it and | 1912 | /// top-left block is given a basis function that is flat across it and |