A 16x16 inter leaf searches its own motion
Browse files at this commit62967a8a246c61886c4b1df79c297c60d47132aaParent: e2e76c0- feat
- a 16x16 inter leaf searches its own motion
1 changed file +111 -4
- crates/ec-av1/src/encode.rs +111 -4
crates/ec-av1/src/encode.rsmodified+111 -4
| @@ -110,6 +110,18 @@ 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 (or 8x8) leaf runs a motion search of its own beside the | ||
| 114 | +/// NEARESTMV candidate it always had. | ||
| 115 | +const LEAF_NEW_MV: bool = true; | ||
| 116 | + | ||
| 117 | +/// [`LEAF_NEW_MV`], or what `EC_AV1_LEAF_NEWMV` names in a test build. | ||
| 118 | +fn leaf_new_mv() -> bool { | ||
| 119 | + match std::env::var("EC_AV1_LEAF_NEWMV").ok() { | ||
| 120 | + Some(v) if cfg!(test) => v != "0", | ||
| 121 | + _ => LEAF_NEW_MV, | ||
| 122 | + } | ||
| 123 | +} | ||
| 124 | + | ||
| 113 | /// Whether an INTER frame's 32x32 block may be split into four 16x16 ones | 125 | /// Whether an INTER frame's 32x32 block may be split into four 16x16 ones |
| 114 | /// when the trial says four cost less. Before lane-av1rd2 only a quadrant | 126 | /// when the trial says four cost less. Before lane-av1rd2 only a quadrant |
| 115 | /// the true frame edge cut through was ever split there; inside the frame | 127 | /// the true frame edge cut through was ever split there; inside the frame |
| @@ -1683,6 +1695,7 @@ fn code_square_inter( | |||
| 1683 | search: &Search, | 1695 | search: &Search, |
| 1684 | mode_bits: &[f64; 13], | 1696 | mode_bits: &[f64; 13], |
| 1685 | reference: &Picture, | 1697 | reference: &Picture, |
| 1698 | + ref_y8: &[u8], | ||
| 1686 | stack: &MvStack, fctx: &crate::decode::FrameCtx, | 1699 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 1687 | ) -> (BlockCoeffs, f64) { | 1700 | ) -> (BlockCoeffs, f64) { |
| 1688 | let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); | 1701 | let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx); |
| @@ -1804,6 +1817,93 @@ fn code_square_inter( | |||
| 1804 | // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` | 1817 | // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else` |
| 1805 | // branches), which is what made 8-mi geometry a true no-op instead of | 1818 | // branches), which is what made 8-mi geometry a true no-op instead of |
| 1806 | // one that only held when every neighbour happened to be inter. | 1819 | // one that only held when every neighbour happened to be inter. |
| 1820 | + // A motion search of this leaf's own (lane-av1rd2): before it, a leaf | ||
| 1821 | + // only ever took the stack's NEARESTMV candidate or went intra, so a | ||
| 1822 | + // 32x32 block splitting because its four quarters MOVE differently could | ||
| 1823 | + // not code the movement it split for. Same candidate `search_inter_block` | ||
| 1824 | + // prices for a whole block -- `motion::search` from `pred_mv`, rounded to | ||
| 1825 | + // a vector the residual syntax can name -- at this leaf's own side. | ||
| 1826 | + let mut best = (inter_cost, None); | ||
| 1827 | + if leaf_new_mv() { | ||
| 1828 | + let source_block = luma.source_block(x, y, side); | ||
| 1829 | + let found = motion::search( | ||
| 1830 | + ref_y8, | ||
| 1831 | + ref_luma.1, | ||
| 1832 | + ref_luma.2, | ||
| 1833 | + ref_luma.3, | ||
| 1834 | + &source_block, | ||
| 1835 | + x, | ||
| 1836 | + y, | ||
| 1837 | + side, | ||
| 1838 | + side, | ||
| 1839 | + stack.pred_mv, | ||
| 1840 | + search.lambda, fctx, | ||
| 1841 | + ); | ||
| 1842 | + let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); | ||
| 1843 | + if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { | ||
| 1844 | + let luma_new = mc_trial( | ||
| 1845 | + luma, x, y, side, new_mv, true, ref_luma.0, ref_luma.1, ref_luma.2, ref_luma.3, | ||
| 1846 | + false, search.base_q_idx, search.deadzone, luma_set, fctx, | ||
| 1847 | + ); | ||
| 1848 | + let u_new = mc_trial( | ||
| 1849 | + &chroma[0], x / 2, y / 2, side / 2, new_mv, false, ref_u.0, ref_u.1, ref_u.2, | ||
| 1850 | + ref_u.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 1851 | + ); | ||
| 1852 | + let v_new = mc_trial( | ||
| 1853 | + &chroma[1], x / 2, y / 2, side / 2, new_mv, false, ref_v.0, ref_v.1, ref_v.2, | ||
| 1854 | + ref_v.3, false, search.base_q_idx, search.deadzone, chroma_set, fctx, | ||
| 1855 | + ); | ||
| 1856 | + let skip_new = luma_new.levels.iter().all(|&l| l == 0) | ||
| 1857 | + && u_new.levels.iter().all(|&l| l == 0) | ||
| 1858 | + && v_new.levels.iter().all(|&l| l == 0); | ||
| 1859 | + let drl_bits = if stack.entries.len() > 1 { | ||
| 1860 | + symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[0]], 0) | ||
| 1861 | + } else { | ||
| 1862 | + 0.0 | ||
| 1863 | + }; | ||
| 1864 | + let cost_new = luma_new.sse | ||
| 1865 | + + u_new.sse | ||
| 1866 | + + v_new.sse | ||
| 1867 | + + search.lambda | ||
| 1868 | + * (skip_bits(skip_new) | ||
| 1869 | + + intra_inter_bits(true) | ||
| 1870 | + + single_ref_bits | ||
| 1871 | + + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) | ||
| 1872 | + + drl_bits | ||
| 1873 | + + mv_bits | ||
| 1874 | + + if skip_new { | ||
| 1875 | + 0.0 | ||
| 1876 | + } else { | ||
| 1877 | + luma_new.bits + u_new.bits + v_new.bits | ||
| 1878 | + }); | ||
| 1879 | + if cost_new < best.0 { | ||
| 1880 | + best = (cost_new, Some((luma_new, u_new, v_new, skip_new, new_mv))); | ||
| 1881 | + } | ||
| 1882 | + } | ||
| 1883 | + } | ||
| 1884 | + if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best { | ||
| 1885 | + if cost < intra_cost { | ||
| 1886 | + luma.commit(x, y, side, &luma_new); | ||
| 1887 | + chroma[0].commit(x / 2, y / 2, side / 2, &u_new); | ||
| 1888 | + chroma[1].commit(x / 2, y / 2, side / 2, &v_new); | ||
| 1889 | + return ( | ||
| 1890 | + BlockCoeffs { | ||
| 1891 | + luma: coeffs(&luma_new.levels, side), | ||
| 1892 | + u: coeffs(&u_new.levels, side / 2), | ||
| 1893 | + v: coeffs(&v_new.levels, side / 2), | ||
| 1894 | + mode: DC_PRED as u8, | ||
| 1895 | + skip: skip_new, | ||
| 1896 | + eight: None, | ||
| 1897 | + inter: Some(InterInfo { | ||
| 1898 | + mode: InterMode::NewMv, | ||
| 1899 | + mv: new_mv, | ||
| 1900 | + }), | ||
| 1901 | + }, | ||
| 1902 | + cost, | ||
| 1903 | + ); | ||
| 1904 | + } | ||
| 1905 | + } | ||
| 1906 | + | ||
| 1807 | if inter_cost < intra_cost { | 1907 | if inter_cost < intra_cost { |
| 1808 | luma.commit(x, y, side, &luma_trial); | 1908 | luma.commit(x, y, side, &luma_trial); |
| 1809 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 1909 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| @@ -2687,6 +2787,7 @@ fn search_inter_block( | |||
| 2687 | search: &Search, | 2787 | search: &Search, |
| 2688 | mode_bits: &[f64; 13], | 2788 | mode_bits: &[f64; 13], |
| 2689 | reference: &Picture, | 2789 | reference: &Picture, |
| 2790 | + ref_y8: &[u8], | ||
| 2690 | stack: &MvStack, fctx: &crate::decode::FrameCtx, | 2791 | stack: &MvStack, fctx: &crate::decode::FrameCtx, |
| 2691 | ) -> (BlockCoeffs, f64) { | 2792 | ) -> (BlockCoeffs, f64) { |
| 2692 | // `luma_set` is the INTRA candidates' table; the two inter candidates | 2793 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| @@ -2923,13 +3024,10 @@ fn search_inter_block( | |||
| 2923 | } | 3024 | } |
| 2924 | 3025 | ||
| 2925 | let source_block = luma.source_block(x, y, BLOCK); | 3026 | let source_block = luma.source_block(x, y, BLOCK); |
| 2926 | - // lane-hbd r4: `motion::search` is 8-bit only (its SAD/cost math is | ||
| 2927 | - // integer-`u8`-scale); narrow the DPB reference plane locally. | ||
| 2928 | - let ref_luma_8: Vec<u8> = ref_luma.0.iter().map(|&v| v as u8).collect(); | ||
| 2929 | #[cfg(test)] | 3027 | #[cfg(test)] |
| 2930 | let t = std::time::Instant::now(); | 3028 | let t = std::time::Instant::now(); |
| 2931 | let found = motion::search( | 3029 | let found = motion::search( |
| 2932 | - &ref_luma_8, | 3030 | + ref_y8, |
| 2933 | ref_luma.1, | 3031 | ref_luma.1, |
| 2934 | ref_luma.2, | 3032 | ref_luma.2, |
| 2935 | ref_luma.3, | 3033 | ref_luma.3, |
| @@ -3143,6 +3241,11 @@ pub(crate) fn encode_inter_frame( | |||
| 3143 | let (cols, rows) = (cols as usize, rows as usize); | 3241 | let (cols, rows) = (cols as usize, rows as usize); |
| 3144 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); | 3242 | let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); |
| 3145 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); | 3243 | let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); |
| 3244 | + // lane-hbd r4: `motion::search` is 8-bit only (its SAD/cost math is | ||
| 3245 | + // integer-`u8`-scale), so the DPB reference plane is narrowed for it -- | ||
| 3246 | + // once per frame (lane-av1rd2), not once per block, which is a whole | ||
| 3247 | + // plane's map per 32x32 block and now per leaf too. | ||
| 3248 | + let ref_y8: Vec<u8> = reference.y.iter().map(|&v| v as u8).collect(); | ||
| 3146 | let mut grid = MiGrid::new(mi_cols, mi_rows); | 3249 | let mut grid = MiGrid::new(mi_cols, mi_rows); |
| 3147 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; | 3250 | let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows]; |
| 3148 | 3251 | ||
| @@ -3189,6 +3292,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3189 | &search, | 3292 | &search, |
| 3190 | &mode_bits_table, | 3293 | &mode_bits_table, |
| 3191 | reference, | 3294 | reference, |
| 3295 | + &ref_y8, | ||
| 3192 | &stack, fctx, | 3296 | &stack, fctx, |
| 3193 | ); | 3297 | ); |
| 3194 | cost_whole += search.lambda * partition_bits(BLOCK, false); | 3298 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| @@ -3240,6 +3344,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3240 | &search, | 3344 | &search, |
| 3241 | &mode_bits_table, | 3345 | &mode_bits_table, |
| 3242 | reference, | 3346 | reference, |
| 3347 | + &ref_y8, | ||
| 3243 | &stack, fctx, | 3348 | &stack, fctx, |
| 3244 | ); | 3349 | ); |
| 3245 | cost += leaf_cost; | 3350 | cost += leaf_cost; |
| @@ -3319,6 +3424,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3319 | &search, | 3424 | &search, |
| 3320 | &mode_bits_table, | 3425 | &mode_bits_table, |
| 3321 | reference, | 3426 | reference, |
| 3427 | + &ref_y8, | ||
| 3322 | &stack, fctx, | 3428 | &stack, fctx, |
| 3323 | ); | 3429 | ); |
| 3324 | for dr in 0..4 { | 3430 | for dr in 0..4 { |
| @@ -3379,6 +3485,7 @@ pub(crate) fn encode_inter_frame( | |||
| 3379 | &search, | 3485 | &search, |
| 3380 | &mode_bits_table, | 3486 | &mode_bits_table, |
| 3381 | reference, | 3487 | reference, |
| 3488 | + &ref_y8, | ||
| 3382 | &stack, fctx, | 3489 | &stack, fctx, |
| 3383 | ); | 3490 | ); |
| 3384 | for dr in 0..2 { | 3491 | for dr in 0..2 { |