Merge branch 'lane-av1rd2' (27b5fb12): inter-frame RD quadtree 32->16->8 with per-leaf NEWMV search and a skipped-block prune; writer codes PARTITION_SPLIT at 16x16 into four 8x8 inter leaves with skip/intra-inter contexts at mi granularity; BD-rate vs rav1e speed 6 +365/+401/+207% -> +172/+203/+155%, vs libaom +502/+502/+440% -> +234/+260/+319% (1080p/2160p/screen); encode wall 3.8x rav1e; every stream ffmpeg-decoded sample-exact vs reconstruction
Browse files at this commit2 changed files +474 -113
- crates/ec-av1/src/encode.rs +390 -76
- crates/ec-av1/src/tile.rs +84 -37
crates/ec-av1/src/encode.rsmodified+390 -76
| @@ -110,6 +110,69 @@ pub(crate) fn take_predicted_bits() -> Vec<f64> { | |||
| 110 | PREDICTED_BITS.with(|p| std::mem::take(&mut *p.borrow_mut())) | 110 | PREDICTED_BITS.with(|p| std::mem::take(&mut *p.borrow_mut())) |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | +/// Whether a 16x16 inter leaf may split into four 8x8 ones of its own. | ||
| 114 | +const SPLIT_INTER_8: bool = true; | ||
| 115 | + | ||
| 116 | +/// [`SPLIT_INTER_8`], or what `EC_AV1_SPLIT_INTER8` names in a test build. | ||
| 117 | +fn split_inter_8() -> bool { | ||
| 118 | + match std::env::var("EC_AV1_SPLIT_INTER8").ok() { | ||
| 119 | + Some(v) if cfg!(test) => v != "0", | ||
| 120 | + _ => SPLIT_INTER_8, | ||
| 121 | + } | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +/// Whether a 16x16 (or 8x8) leaf runs a motion search of its own beside the | ||
| 125 | +/// NEARESTMV candidate it always had. | ||
| 126 | +const LEAF_NEW_MV: bool = true; | ||
| 127 | + | ||
| 128 | +/// [`LEAF_NEW_MV`], or what `EC_AV1_LEAF_NEWMV` names in a test build. | ||
| 129 | +fn leaf_new_mv() -> bool { | ||
| 130 | + match std::env::var("EC_AV1_LEAF_NEWMV").ok() { | ||
| 131 | + Some(v) if cfg!(test) => v != "0", | ||
| 132 | + _ => LEAF_NEW_MV, | ||
| 133 | + } | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +/// Whether an INTER frame's 32x32 block may be split into four 16x16 ones | ||
| 137 | +/// when the trial says four cost less. Before lane-av1rd2 only a quadrant | ||
| 138 | +/// the true frame edge cut through was ever split there; inside the frame | ||
| 139 | +/// every block was coded whole, which left the partition decision the key | ||
| 140 | +/// frame already makes unmade for eleven frames out of twelve. | ||
| 141 | +const SPLIT_INTER_BLOCKS: bool = true; | ||
| 142 | + | ||
| 143 | +/// [`SPLIT_INTER_BLOCKS`], or what `EC_AV1_SPLIT_INTER` names when the | ||
| 144 | +/// measurement that keeps it is running (test builds only, like | ||
| 145 | +/// [`lambda_scale`]). | ||
| 146 | +fn split_inter_blocks() -> bool { | ||
| 147 | + match std::env::var("EC_AV1_SPLIT_INTER").ok() { | ||
| 148 | + Some(v) if cfg!(test) => v != "0", | ||
| 149 | + _ => SPLIT_INTER_BLOCKS, | ||
| 150 | + } | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +/// How often each partition decision was taken since the last | ||
| 154 | +/// [`take_partition_hits`], so a gate can report how often a new partition | ||
| 155 | +/// fires rather than assume it does (gate-blind-to-feature). Index 0 is an | ||
| 156 | +/// inter 32x32 block left whole, 1 one split into four 16x16 leaves; 2 is | ||
| 157 | +/// such a leaf left whole, 3 one split into four 8x8 leaves of its own. | ||
| 158 | +static PARTITION_HITS: [std::sync::atomic::AtomicUsize; 4] = [ | ||
| 159 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 160 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 161 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 162 | + std::sync::atomic::AtomicUsize::new(0), | ||
| 163 | +]; | ||
| 164 | + | ||
| 165 | +/// Records one such decision. | ||
| 166 | +fn partition_hit(which: usize) { | ||
| 167 | + PARTITION_HITS[which].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 168 | +} | ||
| 169 | + | ||
| 170 | +/// Takes (and clears) the counts. | ||
| 171 | +#[cfg(test)] | ||
| 172 | +pub(crate) fn take_partition_hits() -> [usize; 4] { | ||
| 173 | + [0, 1, 2, 3].map(|i| PARTITION_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 174 | +} | ||
| 175 | + | ||
| 113 | /// Whether a 32x32 block may be split into four 16x16 ones when the trial says | 176 | /// Whether a 32x32 block may be split into four 16x16 ones when the trial says |
| 114 | /// four cost less. Set from the measurement in the lane report. | 177 | /// four cost less. Set from the measurement in the lane report. |
| 115 | const SPLIT_BLOCKS: bool = true; | 178 | const SPLIT_BLOCKS: bool = true; |
| @@ -1648,8 +1711,9 @@ fn code_square_inter( | |||
| 1648 | search: &Search, | 1711 | search: &Search, |
| 1649 | mode_bits: &[f64; 13], | 1712 | mode_bits: &[f64; 13], |
| 1650 | reference: &Picture, | 1713 | reference: &Picture, |
| 1714 | + ref_y8: &[u8], | ||
| 1651 | stack: &MvStack, fctx: &crate::decode::FrameCtx, | 1715 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 1652 | -) -> BlockCoeffs { | 1716 | +) -> (BlockCoeffs, f64) { |
| 1653 | let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); | 1717 | let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); |
| 1654 | let luma_set = if side == 8 { | 1718 | let luma_set = if side == 8 { |
| 1655 | TxbSet::Luma8Inter | 1719 | TxbSet::Luma8Inter |
| @@ -1769,11 +1833,98 @@ fn code_square_inter( | |||
| 1769 | // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` | 1833 | // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` |
| 1770 | // branches), which is what made 8-mi geometry a true no-op instead of | 1834 | // branches), which is what made 8-mi geometry a true no-op instead of |
| 1771 | // one that only held when every neighbour happened to be inter. | 1835 | // one that only held when every neighbour happened to be inter. |
| 1836 | + // A motion search of this leaf's own (lane-av1rd2): before it, a leaf | ||
| 1837 | + // only ever took the stack's NEARESTMV candidate or went intra, so a | ||
| 1838 | + // 32x32 block splitting because its four quarters MOVE differently could | ||
| 1839 | + // not code the movement it split for. Same candidate `search_inter_block` | ||
| 1840 | + // prices for a whole block -- `motion::search` from `pred_mv`, rounded to | ||
| 1841 | + // a vector the residual syntax can name -- at this leaf's own side. | ||
| 1842 | + let mut best = (inter_cost, None); | ||
| 1843 | + if leaf_new_mv() { | ||
| 1844 | + let source_block = luma.source_block(x, y, side); | ||
| 1845 | + let found = motion::search( | ||
| 1846 | + ref_y8, | ||
| 1847 | + ref_luma.1, | ||
| 1848 | + ref_luma.2, | ||
| 1849 | + ref_luma.3, | ||
| 1850 | + &source_block, | ||
| 1851 | + x, | ||
| 1852 | + y, | ||
| 1853 | + side, | ||
| 1854 | + side, | ||
| 1855 | + stack.pred_mv, | ||
| 1856 | + search.lambda, fctx, | ||
| 1857 | + ); | ||
| 1858 | + let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); | ||
| 1859 | + if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { | ||
| 1860 | + let luma_new = mc_trial( | ||
| 1861 | + luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3, | ||
| 1862 | + false, search.base_q_idx, search.deadzone, luma_set, fctx, | ||
| 1863 | + ); | ||
| 1864 | + let u_new = mc_trial( | ||
| 1865 | + &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1, ref_u.2, | ||
| 1866 | + ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 1867 | + ); | ||
| 1868 | + let v_new = mc_trial( | ||
| 1869 | + &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1, ref_v.2, | ||
| 1870 | + ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 1871 | + ); | ||
| 1872 | + let skip_new = luma_new.levels.iter().all(|&l| l == 0) | ||
| 1873 | + && u_new.levels.iter().all(|&l| l == 0) | ||
| 1874 | + && v_new.levels.iter().all(|&l| l == 0); | ||
| 1875 | + let drl_bits = if stack.entries.len() > 1 { | ||
| 1876 | + symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0) | ||
| 1877 | + } else { | ||
| 1878 | + 0.0 | ||
| 1879 | + }; | ||
| 1880 | + let cost_new = luma_new.sse | ||
| 1881 | + + u_new.sse | ||
| 1882 | + + v_new.sse | ||
| 1883 | + + search.lambda | ||
| 1884 | + * (skip_bits(skip_new) | ||
| 1885 | + + intra_inter_bits(true) | ||
| 1886 | + + single_ref_bits | ||
| 1887 | + + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) | ||
| 1888 | + + drl_bits | ||
| 1889 | + + mv_bits | ||
| 1890 | + + if skip_new { | ||
| 1891 | + 0.0 | ||
| 1892 | + } else { | ||
| 1893 | + luma_new.bits + u_new.bits + v_new.bits | ||
| 1894 | + }); | ||
| 1895 | + if cost_new < best.0 { | ||
| 1896 | + best = (cost_new, Some((luma_new, u_new, v_new, skip_new, new_mv))); | ||
| 1897 | + } | ||
| 1898 | + } | ||
| 1899 | + } | ||
| 1900 | + if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best { | ||
| 1901 | + if cost < intra_cost { | ||
| 1902 | + luma.commit(x, y, side, &luma_new); | ||
| 1903 | + chroma[0].commit(x / 2, y / 2, side / 2, &u_new); | ||
| 1904 | + chroma[1].commit(x / 2, y / 2, side / 2, &v_new); | ||
| 1905 | + return ( | ||
| 1906 | + BlockCoeffs { | ||
| 1907 | + luma: coeffs(&luma_new.levels, side), | ||
| 1908 | + u: coeffs(&u_new.levels, side / 2), | ||
| 1909 | + v: coeffs(&v_new.levels, side / 2), | ||
| 1910 | + mode: DC_PRED as u8, | ||
| 1911 | + skip: skip_new, | ||
| 1912 | + eight: None, | ||
| 1913 | + inter: Some(InterInfo { | ||
| 1914 | + mode: InterMode::NewMv, | ||
| 1915 | + mv: new_mv, | ||
| 1916 | + }), | ||
| 1917 | + }, | ||
| 1918 | + cost, | ||
| 1919 | + ); | ||
| 1920 | + } | ||
| 1921 | + } | ||
| 1922 | + | ||
| 1772 | if inter_cost < intra_cost { | 1923 | if inter_cost < intra_cost { |
| 1773 | luma.commit(x, y, side, &luma_trial); | 1924 | luma.commit(x, y, side, &luma_trial); |
| 1774 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 1925 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 1775 | chroma[1].commit(x / 2, y / 2, side / 2, &v); | 1926 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 1776 | - BlockCoeffs { | 1927 | + (BlockCoeffs { |
| 1777 | luma: coeffs(&luma_trial.levels, side), | 1928 | luma: coeffs(&luma_trial.levels, side), |
| 1778 | u: coeffs(&u.levels, side / 2), | 1929 | u: coeffs(&u.levels, side / 2), |
| 1779 | v: coeffs(&v.levels, side / 2), | 1930 | v: coeffs(&v.levels, side / 2), |
| @@ -1784,19 +1935,62 @@ fn code_square_inter( | |||
| 1784 | mode: InterMode::NearestMv, | 1935 | mode: InterMode::NearestMv, |
| 1785 | mv, | 1936 | mv, |
| 1786 | }), | 1937 | }), |
| 1787 | - } | 1938 | + }, inter_cost) |
| 1788 | } else { | 1939 | } else { |
| 1789 | - intra_block | 1940 | + (intra_block, intra_cost) |
| 1941 | + } | ||
| 1942 | +} | ||
| 1943 | + | ||
| 1944 | +/// Publishes one coded inter-frame block into the `mi` grid the next block's | ||
| 1945 | +/// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra | ||
| 1946 | +/// block casts no vote but is still a coded cell (see `code_square_inter`'s | ||
| 1947 | +/// note on `processed_rows`/`processed_cols`). | ||
| 1948 | +fn record_mi(grid: &mut MiGrid, mi_row: usize, mi_col: usize, size: u8, inter: Option<InterInfo>) { | ||
| 1949 | + let info = match inter { | ||
| 1950 | + Some(info) => MiInfo { | ||
| 1951 | + is_inter: true, | ||
| 1952 | + ref_frame: LAST_FRAME, | ||
| 1953 | + ref_frame1: NO_REF1, | ||
| 1954 | + mv1: (0, 0), | ||
| 1955 | + mv: mv16(info.mv), | ||
| 1956 | + is_new_mv: matches!(info.mode, InterMode::NewMv), | ||
| 1957 | + size, | ||
| 1958 | + size_h: size, | ||
| 1959 | + is_global_mv0: false, | ||
| 1960 | + is_global_mv1: false, | ||
| 1961 | + }, | ||
| 1962 | + None => MiInfo { | ||
| 1963 | + is_inter: false, | ||
| 1964 | + ref_frame: -1, | ||
| 1965 | + ref_frame1: NO_REF1, | ||
| 1966 | + mv1: (0, 0), | ||
| 1967 | + mv: (0, 0), | ||
| 1968 | + is_new_mv: false, | ||
| 1969 | + size, | ||
| 1970 | + size_h: size, | ||
| 1971 | + is_global_mv0: false, | ||
| 1972 | + is_global_mv1: false, | ||
| 1973 | + }, | ||
| 1974 | + }; | ||
| 1975 | + for dr in 0..usize::from(size) { | ||
| 1976 | + for dc in 0..usize::from(size) { | ||
| 1977 | + grid.set(mi_row + dr, mi_col + dc, info); | ||
| 1978 | + } | ||
| 1790 | } | 1979 | } |
| 1791 | } | 1980 | } |
| 1792 | 1981 | ||
| 1793 | /// The reconstructed samples one 32x32 square covers in all three planes, so | 1982 | /// The reconstructed samples one 32x32 square covers in all three planes, so |
| 1794 | /// that a partition trial can be undone. | 1983 | /// that a partition trial can be undone. |
| 1795 | -fn snapshot(luma: &Plane, chroma: &[Plane; 2], (x, y): (usize, usize)) -> [Vec<u8>; 3] { | 1984 | +fn snapshot( |
| 1985 | + luma: &Plane, | ||
| 1986 | + chroma: &[Plane; 2], | ||
| 1987 | + (x, y): (usize, usize), | ||
| 1988 | + side: usize, | ||
| 1989 | +) -> [Vec<u8>; 3] { | ||
| 1796 | [ | 1990 | [ |
| 1797 | - luma.snapshot(x, y, BLOCK), | 1991 | + luma.snapshot(x, y, side), |
| 1798 | - chroma[0].snapshot(x / 2, y / 2, BLOCK / 2), | 1992 | + chroma[0].snapshot(x / 2, y / 2, side / 2), |
| 1799 | - chroma[1].snapshot(x / 2, y / 2, BLOCK / 2), | 1993 | + chroma[1].snapshot(x / 2, y / 2, side / 2), |
| 1800 | ] | 1994 | ] |
| 1801 | } | 1995 | } |
| 1802 | 1996 | ||
| @@ -1805,11 +1999,12 @@ fn restore( | |||
| 1805 | luma: &mut Plane, | 1999 | luma: &mut Plane, |
| 1806 | chroma: &mut [Plane; 2], | 2000 | chroma: &mut [Plane; 2], |
| 1807 | (x, y): (usize, usize), | 2001 | (x, y): (usize, usize), |
| 2002 | + side: usize, | ||
| 1808 | saved: &[Vec<u8>; 3], | 2003 | saved: &[Vec<u8>; 3], |
| 1809 | ) { | 2004 | ) { |
| 1810 | - luma.restore(x, y, BLOCK, &saved[0]); | 2005 | + luma.restore(x, y, side, &saved[0]); |
| 1811 | - chroma[0].restore(x / 2, y / 2, BLOCK / 2, &saved[1]); | 2006 | + chroma[0].restore(x / 2, y / 2, side / 2, &saved[1]); |
| 1812 | - chroma[1].restore(x / 2, y / 2, BLOCK / 2, &saved[2]); | 2007 | + chroma[1].restore(x / 2, y / 2, side / 2, &saved[2]); |
| 1813 | } | 2008 | } |
| 1814 | 2009 | ||
| 1815 | /// Keeps the DC candidate plus the cheapest `top_k` (or fewer, once DC is set | 2010 | /// Keeps the DC candidate plus the cheapest `top_k` (or fewer, once DC is set |
| @@ -2212,7 +2407,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 2212 | } | 2407 | } |
| 2213 | let (x, y) = (col * BLOCK, row * BLOCK); | 2408 | let (x, y) = (col * BLOCK, row * BLOCK); |
| 2214 | let (c0, r0) = (col * 2, row * 2); | 2409 | let (c0, r0) = (col * 2, row * 2); |
| 2215 | - let base = snapshot(&luma, &chroma, (x, y)); | 2410 | + let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 2216 | 2411 | ||
| 2217 | // spec `decode_partition`'s hasRows/hasCols recomputed at this | 2412 | // spec `decode_partition`'s hasRows/hasCols recomputed at this |
| 2218 | // 32x32 block's own half (see `crate::tile::has_half`): the | 2413 | // 32x32 block's own half (see `crate::tile::has_half`): the |
| @@ -2286,11 +2481,11 @@ pub(crate) fn encode_key_frame_inner( | |||
| 2286 | &mode_bits(above_mode[c0], left_mode[r0]), fctx, | 2481 | &mode_bits(above_mode[c0], left_mode[r0]), fctx, |
| 2287 | ); | 2482 | ); |
| 2288 | cost_whole += search.lambda * partition_bits(BLOCK, false); | 2483 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 2289 | - let after_whole = snapshot(&luma, &chroma, (x, y)); | 2484 | + let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 2290 | 2485 | ||
| 2291 | // What four 16x16 blocks cost instead, each searched against | 2486 | // What four 16x16 blocks cost instead, each searched against |
| 2292 | // the reconstruction the ones before it left. | 2487 | // the reconstruction the ones before it left. |
| 2293 | - restore(&mut luma, &mut chroma, (x, y), &base); | 2488 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &base); |
| 2294 | let mut split = Vec::with_capacity(4); | 2489 | let mut split = Vec::with_capacity(4); |
| 2295 | let mut cost_split = search.lambda | 2490 | let mut cost_split = search.lambda |
| 2296 | * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false)); | 2491 | * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false)); |
| @@ -2372,7 +2567,7 @@ pub(crate) fn encode_key_frame_inner( | |||
| 2372 | modes.extend_from_slice(&split_modes); | 2567 | modes.extend_from_slice(&split_modes); |
| 2373 | blocks.push(Quadrant::Split(split)); | 2568 | blocks.push(Quadrant::Split(split)); |
| 2374 | } else { | 2569 | } else { |
| 2375 | - restore(&mut luma, &mut chroma, (x, y), &after_whole); | 2570 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); |
| 2376 | for cell in 0..2 { | 2571 | for cell in 0..2 { |
| 2377 | above_mode[c0 + cell] = whole.mode; | 2572 | above_mode[c0 + cell] = whole.mode; |
| 2378 | left_mode[r0 + cell] = whole.mode; | 2573 | left_mode[r0 + cell] = whole.mode; |
| @@ -2614,8 +2809,9 @@ fn search_inter_block( | |||
| 2614 | search: &Search, | 2809 | search: &Search, |
| 2615 | mode_bits: &[f64; 13], | 2810 | mode_bits: &[f64; 13], |
| 2616 | reference: &Picture, | 2811 | reference: &Picture, |
| 2812 | + ref_y8: &[u8], | ||
| 2617 | stack: &MvStack, fctx: &crate::decode::FrameCtx, | 2813 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 2618 | -) -> BlockCoeffs { | 2814 | +) -> (BlockCoeffs, f64) { |
| 2619 | // `luma_set` is the INTRA candidates' table; the two inter candidates | 2815 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 2620 | // below price their coefficients through `TxbSet::Luma32Inter`, which is | 2816 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| 2621 | // what `sb_coeff_inter_frame_tile` codes an inter block with (it carries | 2817 | // what `sb_coeff_inter_frame_tile` codes an inter block with (it carries |
| @@ -2850,13 +3046,10 @@ fn search_inter_block( | |||
| 2850 | } | 3046 | } |
| 2851 | 3047 | ||
| 2852 | let source_block = luma.source_block(x, y, BLOCK); | 3048 | let source_block = luma.source_block(x, y, BLOCK); |
| 2853 | - // lane-hbd r4: `motion::search` is 8-bit only (its SAD/cost math is | ||
| 2854 | - // integer-`u8`-scale); narrow the DPB reference plane locally. | ||
| 2855 | - let ref_luma_8: Vec<u8> = ref_luma.0.iter().map(|&v| v as u8).collect(); | ||
| 2856 | #[cfg(test)] | 3049 | #[cfg(test)] |
| 2857 | let t = std::time::Instant::now(); | 3050 | let t = std::time::Instant::now(); |
| 2858 | let found = motion::search( | 3051 | let found = motion::search( |
| 2859 | - &ref_luma_8, | 3052 | + ref_y8, |
| 2860 | ref_luma.1, | 3053 | ref_luma.1, |
| 2861 | ref_luma.2, | 3054 | ref_luma.2, |
| 2862 | ref_luma.3, | 3055 | ref_luma.3, |
| @@ -2961,15 +3154,18 @@ fn search_inter_block( | |||
| 2961 | luma.commit(x, y, BLOCK, &best.luma); | 3154 | luma.commit(x, y, BLOCK, &best.luma); |
| 2962 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); | 3155 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 2963 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); | 3156 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| 2964 | - BlockCoeffs { | 3157 | + ( |
| 2965 | - luma: coeffs(&best.luma.levels, BLOCK), | 3158 | + BlockCoeffs { |
| 2966 | - u: coeffs(&best.u.levels, BLOCK / 2), | 3159 | + luma: coeffs(&best.luma.levels, BLOCK), |
| 2967 | - v: coeffs(&best.v.levels, BLOCK / 2), | 3160 | + u: coeffs(&best.u.levels, BLOCK / 2), |
| 2968 | - mode: best.mode, | 3161 | + v: coeffs(&best.v.levels, BLOCK / 2), |
| 2969 | - skip: best.skip, | 3162 | + mode: best.mode, |
| 2970 | - inter: best.inter, | 3163 | + skip: best.skip, |
| 2971 | - eight: None, | 3164 | + inter: best.inter, |
| 2972 | - } | 3165 | + eight: None, |
| 3166 | + }, | ||
| 3167 | + best.cost, | ||
| 3168 | + ) | ||
| 2973 | } | 3169 | } |
| 2974 | 3170 | ||
| 2975 | /// Encodes one picture as an inter frame predicting from `reference`'s | 3171 | /// Encodes one picture as an inter frame predicting from `reference`'s |
| @@ -3067,6 +3263,11 @@ pub(crate) fn encode_inter_frame( | |||
| 3067 | let (cols, rows) = (cols as usize, rows as usize); | 3263 | let (cols, rows) = (cols as usize, rows as usize); |
| 3068 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 3264 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 3069 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); | 3265 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); |
| 3266 | + // lane-hbd r4: `motion::search` is 8-bit only (its SAD/cost math is | ||
| 3267 | + // integer-`u8`-scale), so the DPB reference plane is narrowed for it -- | ||
| 3268 | + // once per frame (lane-av1rd2), not once per block, which is a whole | ||
| 3269 | + // plane's map per 32x32 block and now per leaf too. | ||
| 3270 | + let ref_y8: Vec<u8> = reference.y.iter().map(|&v| v as u8).collect(); | ||
| 3070 | let mut grid = MiGrid::new(mi_cols, mi_rows); | 3271 | let mut grid = MiGrid::new(mi_cols, mi_rows); |
| 3071 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; | 3272 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; |
| 3072 | 3273 | ||
| @@ -3105,54 +3306,145 @@ pub(crate) fn encode_inter_frame( | |||
| 3105 | let stack = | 3306 | let stack = |
| 3106 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); | 3307 | find_mv_stack(&grid, mi_row, mi_col, 8, 8, LAST_FRAME, mi_cols, mi_rows); |
| 3107 | 3308 | ||
| 3108 | - let block = search_inter_block( | 3309 | + let base = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 3310 | + let (block, mut cost_whole) = search_inter_block( | ||
| 3109 | &mut luma, | 3311 | &mut luma, |
| 3110 | &mut chroma, | 3312 | &mut chroma, |
| 3111 | (x, y), | 3313 | (x, y), |
| 3112 | &search, | 3314 | &search, |
| 3113 | &mode_bits_table, | 3315 | &mode_bits_table, |
| 3114 | reference, | 3316 | reference, |
| 3317 | + &ref_y8, | ||
| 3115 | &stack, fctx, | 3318 | &stack, fctx, |
| 3116 | ); | 3319 | ); |
| 3117 | - | 3320 | + cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 3118 | - for dr in 0..8 { | 3321 | + let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| 3119 | - for dc in 0..8 { | 3322 | + |
| 3120 | - grid.set( | 3323 | + // What four 16x16 leaves cost instead (spec |
| 3121 | - mi_row + dr, | 3324 | + // PARTITION_SPLIT at BLOCK_32X32, which |
| 3122 | - mi_col + dc, | 3325 | + // `sb_coeff_inter_frame_tile`'s `Quadrant::Split` arm |
| 3123 | - match block.inter { | 3326 | + // already writes for a straddling quadrant). Each leaf is |
| 3124 | - Some(info) => MiInfo { | 3327 | + // searched against the reconstruction -- and the `mi` |
| 3125 | - is_inter: true, | 3328 | + // grid -- the ones before it left, exactly as the writer |
| 3126 | - ref_frame: LAST_FRAME, | 3329 | + // and the decoder will read them. |
| 3127 | - ref_frame1: NO_REF1, | 3330 | + // |
| 3128 | - mv1: (0, 0), | 3331 | + // A block the whole-32 search left skipped (no residual |
| 3129 | - mv: mv16(info.mv), | 3332 | + // at all) is not offered the split: libaom prunes the |
| 3130 | - is_new_mv: matches!(info.mode, InterMode::NewMv), | 3333 | + // same way at cpu-used 6, and it is where the wall would |
| 3131 | - size: 8, | 3334 | + // otherwise go on static content. |
| 3132 | - size_h: 8, | 3335 | + let leaf_positions: Vec<(usize, usize)> = (0..4) |
| 3133 | - is_global_mv0: false, | 3336 | + .map(|i| (r32 * 2 + i / 2, c32 * 2 + i % 2)) |
| 3134 | - is_global_mv1: false, | 3337 | + .collect(); |
| 3135 | - }, | 3338 | + let leaves_legal = leaf_positions.iter().all(|&(sr, sc)| { |
| 3136 | - // Intra: no vote, but still a coded cell | 3339 | + crate::tile::has_half( |
| 3137 | - // -- mvstack's extended-scan coverage | 3340 | + sc as u32 * crate::tile::SUB_MI, |
| 3138 | - // (`processed_rows`/`processed_cols`) | 3341 | + crate::tile::SUB_MI, |
| 3139 | - // must see it (module doc). | 3342 | + header.mi_cols, |
| 3140 | - None => MiInfo { | 3343 | + ) && crate::tile::has_half( |
| 3141 | - is_inter: false, | 3344 | + sr as u32 * crate::tile::SUB_MI, |
| 3142 | - ref_frame: -1, | 3345 | + crate::tile::SUB_MI, |
| 3143 | - ref_frame1: NO_REF1, | 3346 | + header.mi_rows, |
| 3144 | - mv1: (0, 0), | 3347 | + ) |
| 3145 | - mv: (0, 0), | 3348 | + }); |
| 3146 | - is_new_mv: false, | 3349 | + let split = (!block.skip && leaves_legal && split_inter_blocks()) |
| 3147 | - size: 8, | 3350 | + .then(|| { |
| 3148 | - size_h: 8, | 3351 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &base); |
| 3149 | - is_global_mv0: false, | 3352 | + let mut cost = search.lambda * partition_bits(BLOCK, true); |
| 3150 | - is_global_mv1: false, | 3353 | + let mut leaves = Vec::with_capacity(4); |
| 3151 | - }, | 3354 | + for &(sr, sc) in &leaf_positions { |
| 3152 | - }, | 3355 | + let (mi_row, mi_col) = (sr * 4, sc * 4); |
| 3153 | - ); | 3356 | + let (lx, ly) = (sc * SUB, sr * SUB); |
| 3357 | + let leaf_base = snapshot(&luma, &chroma, (lx, ly), SUB); | ||
| 3358 | + let stack = find_mv_stack( | ||
| 3359 | + &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, | ||
| 3360 | + ); | ||
| 3361 | + let (leaf, leaf_cost) = code_square_inter( | ||
| 3362 | + &mut luma, | ||
| 3363 | + &mut chroma, | ||
| 3364 | + (lx, ly), | ||
| 3365 | + SUB, | ||
| 3366 | + &search, | ||
| 3367 | + &mode_bits_table, | ||
| 3368 | + reference, | ||
| 3369 | + &ref_y8, | ||
| 3370 | + &stack, fctx, | ||
| 3371 | + ); | ||
| 3372 | + let leaf_cost = leaf_cost | ||
| 3373 | + + search.lambda * partition_bits(SUB, false); | ||
| 3374 | + let after_leaf = snapshot(&luma, &chroma, (lx, ly), SUB); | ||
| 3375 | + | ||
| 3376 | + // And what four 8x8 leaves cost instead -- | ||
| 3377 | + // the same tree one step further down, which | ||
| 3378 | + // the writer codes as a real PARTITION_SPLIT | ||
| 3379 | + // at BLOCK_16X16 (lane-av1rd2). Same | ||
| 3380 | + // skipped-block prune as the level above. | ||
| 3381 | + let eight = (!leaf.skip && split_inter_8()) | ||
| 3382 | + .then(|| { | ||
| 3383 | + restore( | ||
| 3384 | + &mut luma, &mut chroma, (lx, ly), SUB, &leaf_base, | ||
| 3385 | + ); | ||
| 3386 | + let mut cost8 = search.lambda | ||
| 3387 | + * (partition_bits(SUB, true) | ||
| 3388 | + + 4.0 * partition_bits(8, false)); | ||
| 3389 | + let mut eight = Vec::with_capacity(4); | ||
| 3390 | + for i in 0..4 { | ||
| 3391 | + let (mr, mc) = | ||
| 3392 | + (mi_row + (i / 2) * 2, mi_col + (i % 2) * 2); | ||
| 3393 | + let stack8 = find_mv_stack( | ||
| 3394 | + &grid, mr, mc, 2, 2, LAST_FRAME, mi_cols, mi_rows, | ||
| 3395 | + ); | ||
| 3396 | + let (leaf8, cost) = code_square_inter( | ||
| 3397 | + &mut luma, | ||
| 3398 | + &mut chroma, | ||
| 3399 | + (lx + (i % 2) * 8, ly + (i / 2) * 8), | ||
| 3400 | + 8, | ||
| 3401 | + &search, | ||
| 3402 | + &mode_bits_table, | ||
| 3403 | + reference, | ||
| 3404 | + &ref_y8, | ||
| 3405 | + &stack8, fctx, | ||
| 3406 | + ); | ||
| 3407 | + cost8 += cost; | ||
| 3408 | + record_mi(&mut grid, mr, mc, 2, leaf8.inter); | ||
| 3409 | + eight.push(leaf8); | ||
| 3410 | + } | ||
| 3411 | + (eight, cost8) | ||
| 3412 | + }); | ||
| 3413 | + match eight { | ||
| 3414 | + Some((eight, cost8)) if cost8 < leaf_cost => { | ||
| 3415 | + partition_hit(3); | ||
| 3416 | + cost += cost8; | ||
| 3417 | + leaves.push(BlockCoeffs { | ||
| 3418 | + eight: Some(eight), | ||
| 3419 | + ..BlockCoeffs::default() | ||
| 3420 | + }); | ||
| 3421 | + continue; | ||
| 3422 | + } | ||
| 3423 | + Some(_) => restore( | ||
| 3424 | + &mut luma, &mut chroma, (lx, ly), SUB, &after_leaf, | ||
| 3425 | + ), | ||
| 3426 | + None => {} | ||
| 3427 | + } | ||
| 3428 | + partition_hit(2); | ||
| 3429 | + cost += leaf_cost; | ||
| 3430 | + record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter); | ||
| 3431 | + leaves.push(leaf); | ||
| 3432 | + } | ||
| 3433 | + (leaves, cost) | ||
| 3434 | + }); | ||
| 3435 | + if let Some((leaves, cost)) = split { | ||
| 3436 | + if cost < cost_whole { | ||
| 3437 | + // The leaves' own `mi` cells are already in the | ||
| 3438 | + // grid, and their reconstruction is what the | ||
| 3439 | + // planes hold. | ||
| 3440 | + partition_hit(1); | ||
| 3441 | + blocks[r32 * cols + c32] = Quadrant::Split(leaves); | ||
| 3442 | + continue; | ||
| 3154 | } | 3443 | } |
| 3444 | + restore(&mut luma, &mut chroma, (x, y), BLOCK, &after_whole); | ||
| 3155 | } | 3445 | } |
| 3446 | + partition_hit(0); | ||
| 3447 | + record_mi(&mut grid, mi_row, mi_col, 8, block.inter); | ||
| 3156 | blocks[r32 * cols + c32] = Quadrant::Whole(block); | 3448 | blocks[r32 * cols + c32] = Quadrant::Whole(block); |
| 3157 | } else { | 3449 | } else { |
| 3158 | // The sub-positions actually inside the true frame, same | 3450 | // The sub-positions actually inside the true frame, same |
| @@ -3203,7 +3495,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3203 | let stack = find_mv_stack( | 3495 | let stack = find_mv_stack( |
| 3204 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, | 3496 | &grid, mi_row, mi_col, 4, 4, LAST_FRAME, mi_cols, mi_rows, |
| 3205 | ); | 3497 | ); |
| 3206 | - let block = code_square_inter( | 3498 | + let (block, _) = code_square_inter( |
| 3207 | &mut luma, | 3499 | &mut luma, |
| 3208 | &mut chroma, | 3500 | &mut chroma, |
| 3209 | (x, y), | 3501 | (x, y), |
| @@ -3211,6 +3503,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3211 | &search, | 3503 | &search, |
| 3212 | &mode_bits_table, | 3504 | &mode_bits_table, |
| 3213 | reference, | 3505 | reference, |
| 3506 | + &ref_y8, | ||
| 3214 | &stack, fctx, | 3507 | &stack, fctx, |
| 3215 | ); | 3508 | ); |
| 3216 | for dr in 0..4 { | 3509 | for dr in 0..4 { |
| @@ -3263,7 +3556,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3263 | let stack = find_mv_stack( | 3556 | let stack = find_mv_stack( |
| 3264 | &grid, mi_row, mi_col, 2, 2, LAST_FRAME, mi_cols, mi_rows, | 3557 | &grid, mi_row, mi_col, 2, 2, LAST_FRAME, mi_cols, mi_rows, |
| 3265 | ); | 3558 | ); |
| 3266 | - let leaf = code_square_inter( | 3559 | + let (leaf, _) = code_square_inter( |
| 3267 | &mut luma, | 3560 | &mut luma, |
| 3268 | &mut chroma, | 3561 | &mut chroma, |
| 3269 | (leaf_x, leaf_y), | 3562 | (leaf_x, leaf_y), |
| @@ -3271,6 +3564,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3271 | &search, | 3564 | &search, |
| 3272 | &mode_bits_table, | 3565 | &mode_bits_table, |
| 3273 | reference, | 3566 | reference, |
| 3567 | + &ref_y8, | ||
| 3274 | &stack, fctx, | 3568 | &stack, fctx, |
| 3275 | ); | 3569 | ); |
| 3276 | for dr in 0..2 { | 3570 | for dr in 0..2 { |
| @@ -6022,17 +6316,24 @@ mod tests { | |||
| 6022 | /// ours: `base_q_idx {60,90,120,150}`. Every PSNR is all-plane, on | 6316 | /// ours: `base_q_idx {60,90,120,150}`. Every PSNR is all-plane, on |
| 6023 | /// ffmpeg-decoded frames of each encoder's own stream. | 6317 | /// ffmpeg-decoded frames of each encoder's own stream. |
| 6024 | /// | 6318 | /// |
| 6025 | - /// Measured baseline 2026-09-06, 12 frames, this box (wall = the four | 6319 | + /// Measured 2026-09-06 after the inter partition search (lane-av1rd2), |
| 6026 | - /// encodes of that ladder together): | 6320 | + /// 12 frames, this box (wall = the four encodes of that ladder |
| 6321 | + /// together): | ||
| 6027 | /// | 6322 | /// |
| 6028 | /// | clip | ours (q 150/120/90/60) | BD-rate vs libaom | vs rav1e | wall ours:aom:rav1e | | 6323 | /// | clip | ours (q 150/120/90/60) | BD-rate vs libaom | vs rav1e | wall ours:aom:rav1e | |
| 6029 | /// |---|---|---|---|---| | 6324 | /// |---|---|---|---|---| |
| 6030 | - /// | 1080p fixture | 40.36 dB/26824 B .. 48.24 dB/120942 B | +501.4% | +364.4% | 5.1s:1.9s:3.8s | | 6325 | + /// | 1080p fixture | 41.16 dB/22349 B .. 49.97 dB/83178 B | +234.0% | +171.9% | 9.8s:1.1s:2.6s | |
| 6031 | - /// | 2160p fixture | 41.36 dB/17608 B .. 49.27 dB/76978 B | +504.4% | +401.0% | 4.6s:1.5s:3.3s | | 6326 | + /// | 2160p fixture | 42.01 dB/14687 B .. 50.82 dB/52415 B | +260.3% | +203.2% | 9.3s:1.0s:2.2s | |
| 6032 | - /// | screen capture | 37.32 dB/13053 B .. 45.39 dB/59744 B | +440.9% | +206.0% | 5.9s:1.7s:3.0s | | 6327 | + /// | screen capture | 37.49 dB/12797 B .. 46.19 dB/50643 B | +318.5% | +155.3% | 9.1s:1.0s:2.1s | |
| 6328 | + /// | ||
| 6329 | + /// The baseline it replaced, before an inter frame's blocks chose their | ||
| 6330 | + /// own partition, was +501.4/+504.4/+440.9 against libaom and | ||
| 6331 | + /// +364.4/+401.0/+206.0 against rav1e at 5.1/4.6/5.9 s. | ||
| 6033 | /// | 6332 | /// |
| 6034 | - /// So: roughly 5x libaom's rate and 3-4x rav1e's at matched fidelity, | 6333 | + /// So: roughly 2.5-3x libaom's rate and 1.5-2x rav1e's at matched |
| 6035 | - /// while spending 2.7x libaom's and 1.4x rav1e's encode time. No | 6334 | + /// fidelity, while spending far more time than either (the partition |
| 6335 | + /// search's own cost, uncut: see the two intra-pruning variants measured | ||
| 6336 | + /// and rejected in the lane report). No | ||
| 6036 | /// threshold is asserted yet -- this run sets the baseline; the gate | 6337 | /// threshold is asserted yet -- this run sets the baseline; the gate |
| 6037 | /// asserts only four monotone points per ladder and that ffmpeg decodes | 6338 | /// asserts only four monotone points per ladder and that ffmpeg decodes |
| 6038 | /// our stream sample-exact against our reconstruction. | 6339 | /// our stream sample-exact against our reconstruction. |
| @@ -6104,7 +6405,20 @@ mod tests { | |||
| 6104 | for (name, path) in &clips { | 6405 | for (name, path) in &clips { |
| 6105 | let fctx = &crate::decode::FrameCtx::new(); | 6406 | let fctx = &crate::decode::FrameCtx::new(); |
| 6106 | let source = clip_frames(path.to_str().unwrap(), "0", width, height, frames); | 6407 | let source = clip_frames(path.to_str().unwrap(), "0", width, height, frames); |
| 6408 | + let _ = take_partition_hits(); | ||
| 6107 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); | 6409 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); |
| 6410 | + // How often the inter split actually fired over this clip's four | ||
| 6411 | + // encodes, against the whole 32x32 that was the only outcome | ||
| 6412 | + // before (gate-blind-to-feature). | ||
| 6413 | + let [whole32, split32, whole16, split16] = take_partition_hits(); | ||
| 6414 | + eprintln!( | ||
| 6415 | + "{name}: inter 32x32 split into 16s {split32} of {} blocks ({:.1}%); \ | ||
| 6416 | + 16x16 leaves split into 8s {split16} of {} ({:.1}%)", | ||
| 6417 | + whole32 + split32, | ||
| 6418 | + 100.0 * split32 as f64 / (whole32 + split32).max(1) as f64, | ||
| 6419 | + whole16 + split16, | ||
| 6420 | + 100.0 * split16 as f64 / (whole16 + split16).max(1) as f64 | ||
| 6421 | + ); | ||
| 6108 | let (aom, aom_wall) = | 6422 | let (aom, aom_wall) = |
| 6109 | external_ladder(&source, width, height, "libaom-av1", &aom_points); | 6423 | external_ladder(&source, width, height, "libaom-av1", &aom_points); |
| 6110 | let (rav1e, rav1e_wall) = | 6424 | let (rav1e, rav1e_wall) = |
crates/ec-av1/src/tile.rsmodified+84 -37
| @@ -629,6 +629,9 @@ struct Neighbours { | |||
| 629 | left_side_mi: Vec<usize>, | 629 | left_side_mi: Vec<usize>, |
| 630 | /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s | 630 | /// Whether the neighbour carried no residual, for [`sb_coeff_inter_frame_tile`]'s |
| 631 | /// skip context. Unused (and left at its default) by the key frame writers. | 631 | /// skip context. Unused (and left at its default) by the key frame writers. |
| 632 | + /// Kept per 4x4 mode-info unit, like [`Self::above_side_mi`] and for the | ||
| 633 | + /// same reason: under a real PARTITION_SPLIT to 8x8 the four leaves of a | ||
| 634 | + /// 16x16 read (and leave) different contexts inside one [`SUB`] slot. | ||
| 632 | above_skip: Vec<bool>, | 635 | above_skip: Vec<bool>, |
| 633 | /// The same, down the left edge. | 636 | /// The same, down the left edge. |
| 634 | left_skip: Vec<bool>, | 637 | left_skip: Vec<bool>, |
| @@ -676,10 +679,10 @@ impl Neighbours { | |||
| 676 | left_side: vec![SB; rows], | 679 | left_side: vec![SB; rows], |
| 677 | above_side_mi: vec![SB; cols * (SUB / MI)], | 680 | above_side_mi: vec![SB; cols * (SUB / MI)], |
| 678 | left_side_mi: vec![SB; rows * (SUB / MI)], | 681 | left_side_mi: vec![SB; rows * (SUB / MI)], |
| 679 | - above_skip: vec![false; cols], | 682 | + above_skip: vec![false; cols * (SUB / MI)], |
| 680 | - left_skip: vec![false; rows], | 683 | + left_skip: vec![false; rows * (SUB / MI)], |
| 681 | - above_inter: vec![false; cols], | 684 | + above_inter: vec![false; cols * (SUB / MI)], |
| 682 | - left_inter: vec![false; rows], | 685 | + left_inter: vec![false; rows * (SUB / MI)], |
| 683 | mi_cols, | 686 | mi_cols, |
| 684 | mi_rows, | 687 | mi_rows, |
| 685 | } | 688 | } |
| @@ -768,11 +771,17 @@ impl Neighbours { | |||
| 768 | /// fills for the coefficient and mode state. | 771 | /// fills for the coefficient and mode state. |
| 769 | fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) { | 772 | fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool) { |
| 770 | let (r, c) = at; | 773 | let (r, c) = at; |
| 771 | - for cell in 0..side / SUB { | 774 | + self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter); |
| 772 | - self.above_skip[c + cell] = skip; | 775 | + } |
| 773 | - self.left_skip[r + cell] = skip; | 776 | + |
| 774 | - self.above_inter[c + cell] = is_inter; | 777 | + /// [`Self::record_inter`] taking the block's position directly in 4x4 |
| 775 | - self.left_inter[r + cell] = is_inter; | 778 | + /// mode-info units, for a block finer than one [`SUB`] slot. |
| 779 | + fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool) { | ||
| 780 | + for cell in 0..side / MI { | ||
| 781 | + self.above_skip[mi_c + cell] = skip; | ||
| 782 | + self.left_skip[mi_r + cell] = skip; | ||
| 783 | + self.above_inter[mi_c + cell] = is_inter; | ||
| 784 | + self.left_inter[mi_r + cell] = is_inter; | ||
| 776 | } | 785 | } |
| 777 | } | 786 | } |
| 778 | 787 | ||
| @@ -1682,6 +1691,7 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 { | |||
| 1682 | let cdf: &[u16] = match side { | 1691 | let cdf: &[u16] = match side { |
| 1683 | SB => &cdfs.partition_w64[0], | 1692 | SB => &cdfs.partition_w64[0], |
| 1684 | BLOCK => &cdfs.partition_w32[0], | 1693 | BLOCK => &cdfs.partition_w32[0], |
| 1694 | + TX8 => &cdfs.partition_w8[0], | ||
| 1685 | _ => &cdfs.partition_w16[0], | 1695 | _ => &cdfs.partition_w16[0], |
| 1686 | }; | 1696 | }; |
| 1687 | let symbol = if split { | 1697 | let symbol = if split { |
| @@ -2368,6 +2378,50 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2368 | let at16 = (sr, sc); | 2378 | let at16 = (sr, sc); |
| 2369 | if has_cols16 && has_rows16 { | 2379 | if has_cols16 && has_rows16 { |
| 2370 | let ctx16 = neighbours.partition_ctx(at16, SUB); | 2380 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| 2381 | + // A 16x16 leaf wholly inside the frame that | ||
| 2382 | + // carries `eight` split itself further, on | ||
| 2383 | + // cost rather than on geometry (lane-av1rd2): | ||
| 2384 | + // spec PARTITION_SPLIT at BLOCK_16X16, then | ||
| 2385 | + // the same four 8x8 leaves the straddling | ||
| 2386 | + // branch below writes -- all four inside, so | ||
| 2387 | + // the partition symbol is a real one rather | ||
| 2388 | + // than the edge's gathered flag. | ||
| 2389 | + if let Some(leaves) = &leaf.eight { | ||
| 2390 | + if leaves.len() != 4 { | ||
| 2391 | + return Err(Error::unsupported( | ||
| 2392 | + "AV1 tile", | ||
| 2393 | + "a 16x16 inter-frame block inside the frame that splits needs all four 8x8 leaves", | ||
| 2394 | + )); | ||
| 2395 | + } | ||
| 2396 | + enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w16[ctx16]); | ||
| 2397 | + let (mi_row0, mi_col0) = | ||
| 2398 | + (sr as u32 * SUB_MI, sc as u32 * SUB_MI); | ||
| 2399 | + for (i, leaf8) in leaves.iter().enumerate() { | ||
| 2400 | + let leaf_mi = ( | ||
| 2401 | + (mi_row0 + (i as u32 / 2) * 2) as usize, | ||
| 2402 | + (mi_col0 + (i as u32 % 2) * 2) as usize, | ||
| 2403 | + ); | ||
| 2404 | + let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); | ||
| 2405 | + enc.symbol( | ||
| 2406 | + PARTITION_NONE, | ||
| 2407 | + &mut cdfs.partition_w8[leaf_ctx], | ||
| 2408 | + ); | ||
| 2409 | + let (skip, is_inter) = write_inter_frame_leaf8( | ||
| 2410 | + &mut enc, | ||
| 2411 | + &mut cdfs, | ||
| 2412 | + &mut neighbours, | ||
| 2413 | + &mut grid, | ||
| 2414 | + mi_cols, | ||
| 2415 | + mi_rows, | ||
| 2416 | + leaf8, | ||
| 2417 | + leaf_mi, | ||
| 2418 | + &scan8, | ||
| 2419 | + &scan4, | ||
| 2420 | + )?; | ||
| 2421 | + let _ = (skip, is_inter); | ||
| 2422 | + } | ||
| 2423 | + continue; | ||
| 2424 | + } | ||
| 2371 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); | 2425 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx16]); |
| 2372 | write_inter_frame_leaf( | 2426 | write_inter_frame_leaf( |
| 2373 | &mut enc, | 2427 | &mut enc, |
| @@ -2413,7 +2467,6 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2413 | `eight` entry per 8x8 leaf inside the true frame", | 2467 | `eight` entry per 8x8 leaf inside the true frame", |
| 2414 | )); | 2468 | )); |
| 2415 | } | 2469 | } |
| 2416 | - let mut prev_leaf: Option<((usize, usize), bool, bool)> = None; | ||
| 2417 | for (leaf8, (mr, mc)) in leaves.iter().zip(leaf_positions) { | 2470 | for (leaf8, (mr, mc)) in leaves.iter().zip(leaf_positions) { |
| 2418 | let leaf_mi = (mr as usize, mc as usize); | 2471 | let leaf_mi = (mr as usize, mc as usize); |
| 2419 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); | 2472 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| @@ -2426,22 +2479,18 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2426 | mi_cols, | 2479 | mi_cols, |
| 2427 | mi_rows, | 2480 | mi_rows, |
| 2428 | leaf8, | 2481 | leaf8, |
| 2429 | - at16, | ||
| 2430 | leaf_mi, | 2482 | leaf_mi, |
| 2431 | &scan8, | 2483 | &scan8, |
| 2432 | &scan4, | 2484 | &scan4, |
| 2433 | - prev_leaf, | ||
| 2434 | )?; | 2485 | )?; |
| 2435 | - prev_leaf = Some((leaf_mi, skip, is_inter)); | 2486 | + let _ = (skip, is_inter); |
| 2436 | } | 2487 | } |
| 2437 | // Same write-back-once-from-the-last-leaf rule | 2488 | // Same write-back-once-from-the-last-leaf rule |
| 2438 | // as the key frame search's `write_leaf8` | 2489 | // as the key frame search's `write_leaf8` |
| 2439 | // caller (r15): the SUB-grid skip/inter arrays | 2490 | // caller (r15): the SUB-grid skip/inter arrays |
| 2440 | // are otherwise left stale for the next | 2491 | // are otherwise left stale for the next |
| 2441 | // 16x16 slot. | 2492 | // 16x16 slot. |
| 2442 | - if let Some((_, skip, is_inter)) = prev_leaf { | 2493 | + |
| 2443 | - neighbours.record_inter(at16, SUB, skip, is_inter); | ||
| 2444 | - } | ||
| 2445 | } | 2494 | } |
| 2446 | } | 2495 | } |
| 2447 | continue; | 2496 | continue; |
| @@ -2451,13 +2500,14 @@ pub fn sb_coeff_inter_frame_tile( | |||
| 2451 | let (r, c) = at; | 2500 | let (r, c) = at; |
| 2452 | let has_above = r32 > 0; | 2501 | let has_above = r32 > 0; |
| 2453 | let has_left = c32 > 0; | 2502 | let has_left = c32 > 0; |
| 2454 | - let skip_ctx = | 2503 | + let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 2455 | - usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | 2504 | + let skip_ctx = usize::from(neighbours.above_skip[mi_c]) |
| 2505 | + + usize::from(neighbours.left_skip[mi_r]); | ||
| 2456 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); | 2506 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2457 | 2507 | ||
| 2458 | let is_inter = block.inter.is_some(); | 2508 | let is_inter = block.inter.is_some(); |
| 2459 | let (above_inter, left_inter) = | 2509 | let (above_inter, left_inter) = |
| 2460 | - (neighbours.above_inter[c], neighbours.left_inter[r]); | 2510 | + (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]); |
| 2461 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); | 2511 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 2462 | enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); | 2512 | enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); |
| 2463 | 2513 | ||
| @@ -2649,11 +2699,12 @@ fn write_inter_frame_leaf( | |||
| 2649 | const LAST_FRAME: i8 = 1; | 2699 | const LAST_FRAME: i8 = 1; |
| 2650 | 2700 | ||
| 2651 | let (r, c) = at; | 2701 | let (r, c) = at; |
| 2652 | - let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]); | 2702 | + let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); |
| 2703 | + let skip_ctx = usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]); | ||
| 2653 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); | 2704 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2654 | 2705 | ||
| 2655 | let (has_above, has_left) = (r > 0, c > 0); | 2706 | let (has_above, has_left) = (r > 0, c > 0); |
| 2656 | - let (above_inter, left_inter) = (neighbours.above_inter[c], neighbours.left_inter[r]); | 2707 | + let (above_inter, left_inter) = (neighbours.above_inter[mi_c], neighbours.left_inter[mi_r]); |
| 2657 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); | 2708 | let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter); |
| 2658 | let is_inter = block.inter.is_some(); | 2709 | let is_inter = block.inter.is_some(); |
| 2659 | enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); | 2710 | enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]); |
| @@ -2812,11 +2863,9 @@ fn write_inter_frame_leaf8( | |||
| 2812 | mi_cols: u32, | 2863 | mi_cols: u32, |
| 2813 | mi_rows: u32, | 2864 | mi_rows: u32, |
| 2814 | block: &BlockCoeffs, | 2865 | block: &BlockCoeffs, |
| 2815 | - outer_at: (usize, usize), | ||
| 2816 | leaf_mi: (usize, usize), | 2866 | leaf_mi: (usize, usize), |
| 2817 | scan8: &Vec<u16>, | 2867 | scan8: &Vec<u16>, |
| 2818 | scan4: &Vec<u16>, | 2868 | scan4: &Vec<u16>, |
| 2819 | - prev_leaf: Option<((usize, usize), bool, bool)>, | ||
| 2820 | ) -> Result<(bool, bool)> { | 2869 | ) -> Result<(bool, bool)> { |
| 2821 | if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES { | 2870 | if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES { |
| 2822 | return Err(Error::unsupported( | 2871 | return Err(Error::unsupported( |
| @@ -2834,20 +2883,16 @@ fn write_inter_frame_leaf8( | |||
| 2834 | /// branches. | 2883 | /// branches. |
| 2835 | const LAST_FRAME: i8 = 1; | 2884 | const LAST_FRAME: i8 = 1; |
| 2836 | 2885 | ||
| 2837 | - let (r, c) = outer_at; | 2886 | + // This leaf's own 4x4 mode-info cells, not the enclosing 16x16 slot's: |
| 2838 | - let mut above_skip = neighbours.above_skip[c]; | 2887 | + // under a real PARTITION_SPLIT all four leaves are coded, so the |
| 2839 | - let mut left_skip = neighbours.left_skip[r]; | 2888 | + // bottom-left leaf's true above neighbour is the FIRST leaf and the |
| 2840 | - let mut above_inter = neighbours.above_inter[c]; | 2889 | + // bottom-right's above and left are two DIFFERENT ones (lane-av1rd2 -- |
| 2841 | - let mut left_inter = neighbours.left_inter[r]; | 2890 | + // reading the coarse slot desynced the stream, and so did an override |
| 2842 | - if let Some(((pr, pc), pskip, pinter)) = prev_leaf { | 2891 | + // that remembered only the previous leaf). |
| 2843 | - if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 { | 2892 | + let above_skip = neighbours.above_skip[leaf_mi.1]; |
| 2844 | - above_skip = pskip; | 2893 | + let left_skip = neighbours.left_skip[leaf_mi.0]; |
| 2845 | - above_inter = pinter; | 2894 | + let above_inter = neighbours.above_inter[leaf_mi.1]; |
| 2846 | - } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 { | 2895 | + let left_inter = neighbours.left_inter[leaf_mi.0]; |
| 2847 | - left_skip = pskip; | ||
| 2848 | - left_inter = pinter; | ||
| 2849 | - } | ||
| 2850 | - } | ||
| 2851 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); | 2896 | let skip_ctx = usize::from(above_skip) + usize::from(left_skip); |
| 2852 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); | 2897 | enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); |
| 2853 | 2898 | ||
| @@ -2964,6 +3009,7 @@ fn write_inter_frame_leaf8( | |||
| 2964 | vec![0i32; TX4 * TX4], | 3009 | vec![0i32; TX4 * TX4], |
| 2965 | ]; | 3010 | ]; |
| 2966 | neighbours.record_mi(leaf_mi, 8, &zero_grids); | 3011 | neighbours.record_mi(leaf_mi, 8, &zero_grids); |
| 3012 | + neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some()); | ||
| 2967 | } else { | 3013 | } else { |
| 2968 | let grids = [ | 3014 | let grids = [ |
| 2969 | level_grid(&block.luma, TX8)?, | 3015 | level_grid(&block.luma, TX8)?, |
| @@ -2980,6 +3026,7 @@ fn write_inter_frame_leaf8( | |||
| 2980 | mode_for_tx, | 3026 | mode_for_tx, |
| 2981 | ); | 3027 | ); |
| 2982 | neighbours.record_mi(leaf_mi, 8, &grids); | 3028 | neighbours.record_mi(leaf_mi, 8, &grids); |
| 3029 | + neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter); | ||
| 2983 | } | 3030 | } |
| 2984 | Ok((block.skip, is_inter)) | 3031 | Ok((block.skip, is_inter)) |
| 2985 | } | 3032 | } |