forge
Explore
Log in

Don't you dare go Hollow

A 16x16 inter leaf searches its own motion

Browse files at this commit

62967a8a246c61886c4b1df79c297c60d47132aaAhmet Kaan GümüşParent: e2e76c0
  • feat
    • a 16x16 inter leaf searches its own motion
Show full message
feat(av1): a 16x16 inter leaf searches its own motion

A leaf only ever took the MV stack's NEARESTMV candidate or went intra, so
a 32x32 block that split because its four quarters move differently could
not code the movement it split for. Each leaf now prices the same NEWMV
candidate a whole block does -- motion::search from pred_mv, rounded to a
vector the residual syntax can name -- at its own side.

The reference luma plane is narrowed to 8 bits once per frame rather than
once per searched block (a whole plane's map per 32x32 block before, and
per leaf after this), which is where the added search time comes back.

BD-rate vs libaom / rav1e, same 12-frame recipe:
  1080p  +321.7 -> +294.5  |  +235.4 -> +215.8
  2160p  +343.4 -> +320.9  |  +272.2 -> +253.2
  screen +396.1 -> +395.6  |  +181.2 -> +181.1
Wall 8.1/7.7/9.0 s -> 9.2/8.5/9.2 s. Splits fire on 14.6/12.3/23.7%.
crates/ec-av1/src/encode.rsmodified+111 -4
@@ -110,6 +110,18 @@ pub(crate) fn take_predicted_bits() -> Vec<f64> {
110110 PREDICTED_BITS.with(|p| std::mem::take(&mut *p.borrow_mut()))
111111 }
112112
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+
113125 /// Whether an INTER frame's 32x32 block may be split into four 16x16 ones
114126 /// when the trial says four cost less. Before lane-av1rd2 only a quadrant
115127 /// the true frame edge cut through was ever split there; inside the frame
@@ -1683,6 +1695,7 @@ fn code_square_inter(
16831695 search: &Search,
16841696 mode_bits: &[f64; 13],
16851697 reference: &Picture,
1698+ ref_y8: &[u8],
16861699 stack: &MvStack, fctx: &crate::decode::FrameCtx,
16871700 ) -> (BlockCoeffs, f64) {
16881701 let (intra_block, intra_cost) = code_square(luma, chroma, (x, y), side, search, mode_bits, fctx);
@@ -1804,6 +1817,93 @@ fn code_square_inter(
18041817 // `MiGrid` now records intra cells (`tile.rs`/`encode.rs`'s `else`
18051818 // branches), which is what made 8-mi geometry a true no-op instead of
18061819 // 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+
18071907 if inter_cost < intra_cost {
18081908 luma.commit(x, y, side, &luma_trial);
18091909 chroma[0].commit(x / 2, y / 2, side / 2, &u);
@@ -2687,6 +2787,7 @@ fn search_inter_block(
26872787 search: &Search,
26882788 mode_bits: &[f64; 13],
26892789 reference: &Picture,
2790+ ref_y8: &[u8],
26902791 stack: &MvStack, fctx: &crate::decode::FrameCtx,
26912792 ) -> (BlockCoeffs, f64) {
26922793 // `luma_set` is the INTRA candidates' table; the two inter candidates
@@ -2923,13 +3024,10 @@ fn search_inter_block(
29233024 }
29243025
29253026 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();
29293027 #[cfg(test)]
29303028 let t = std::time::Instant::now();
29313029 let found = motion::search(
2932- &ref_luma_8,
3030+ ref_y8,
29333031 ref_luma.1,
29343032 ref_luma.2,
29353033 ref_luma.3,
@@ -3143,6 +3241,11 @@ pub(crate) fn encode_inter_frame(
31433241 let (cols, rows) = (cols as usize, rows as usize);
31443242 let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2));
31453243 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();
31463249 let mut grid = MiGrid::new(mi_cols, mi_rows);
31473250 let mut blocks = vec![Quadrant::Whole(BlockCoeffs::default()); cols * rows];
31483251
@@ -3189,6 +3292,7 @@ pub(crate) fn encode_inter_frame(
31893292 &search,
31903293 &mode_bits_table,
31913294 reference,
3295+ &ref_y8,
31923296 &stack, fctx,
31933297 );
31943298 cost_whole += search.lambda * partition_bits(BLOCK, false);
@@ -3240,6 +3344,7 @@ pub(crate) fn encode_inter_frame(
32403344 &search,
32413345 &mode_bits_table,
32423346 reference,
3347+ &ref_y8,
32433348 &stack, fctx,
32443349 );
32453350 cost += leaf_cost;
@@ -3319,6 +3424,7 @@ pub(crate) fn encode_inter_frame(
33193424 &search,
33203425 &mode_bits_table,
33213426 reference,
3427+ &ref_y8,
33223428 &stack, fctx,
33233429 );
33243430 for dr in 0..4 {
@@ -3379,6 +3485,7 @@ pub(crate) fn encode_inter_frame(
33793485 &search,
33803486 &mode_bits_table,
33813487 reference,
3488+ &ref_y8,
33823489 &stack, fctx,
33833490 );
33843491 for dr in 0..2 {