forge
Explore
Log in

Don't you dare go Hollow

Deblock-aware intra 4x4 trial distortion

Browse files at this commit

bb59e0bead14a9655cf31e83c8fb2d2ae2f53c38Ahmet Kaan GümüşParent: 04d77cb
  • feat
    • deblock-aware intra 4x4 trial distortion
Show full message
feat(h264rd/r2): deblock-aware intra 4x4 trial distortion

The transform_8x8 intra trial judged candidates by pre-filter SSD, which
prices the 4x4 candidate as if the bS-3 edges its extra transforms create
cost nothing under the loop filter every decoder runs. ssd_mb_deblocked
copies the candidate macroblock with 4-sample left/top aprons into a
scratch, filters it exactly as deblock_picture will - bS 4 on macroblock
edges when a neighbour exists, bS 3 on internal edges, the 4/12 luma
edges skipped for the 8x8 candidate as the decoder's internal() gate
does, verticals before horizontals, this qp, zero offsets - and SSDs that
instead. Chroma stays pre-filter (never 8x8-transformed, cannot separate
candidates); the flag-off path keeps ssd_luma + rough bits untouched.

Real clips, BD-PSNR flag-on vs flag-off q22/26/30/34:
  natural 720p film  +0.796 -> +0.827 dB
  obs 1440p capture  -0.007 -> -0.000 dB
crates/ec-h264/src/enc/mb.rsmodified+93 -3
@@ -15,6 +15,7 @@
15 use ec_h264_syntax::SliceType;15 use ec_h264_syntax::SliceType;
16 use wide::i16x16;16 use wide::i16x16;
17 17
18+use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line};
18 use crate::decoder::{19 use crate::decoder::{
19 chroma_nz_pair, gather_nbr4, gather_nbr8, luma_nz_pair, mb_neighbors, MbNeighbors,20 chroma_nz_pair, gather_nbr4, gather_nbr8, luma_nz_pair, mb_neighbors, MbNeighbors,
20 };21 };
@@ -1607,7 +1608,7 @@ fn encode_intra_mb(
1607 cost.i16_mode,1608 cost.i16_mode,
1608 chroma_mode,1609 chroma_mode,
1609 );1610 );
1610- ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f641611+ ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, false) as f64 + e.lambda * bits as f64
1611 } else {1612 } else {
1612 ssd_luma(pic, e, mb_x, mb_y) as f641613 ssd_luma(pic, e, mb_x, mb_y) as f64
1613 + lambda_ssd(qp) * rough_intra_bits(&lv4, Some((&m4, &p4))) as f641614 + lambda_ssd(qp) * rough_intra_bits(&lv4, Some((&m4, &p4))) as f64
@@ -1639,7 +1640,7 @@ fn encode_intra_mb(
1639 cost.i16_mode,1640 cost.i16_mode,
1640 chroma_mode,1641 chroma_mode,
1641 );1642 );
1642- ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f641643+ ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, true) as f64 + e.lambda * bits as f64
1643 } else {1644 } else {
1644 f64::INFINITY1645 f64::INFINITY
1645 };1646 };
@@ -1658,7 +1659,7 @@ fn encode_intra_mb(
1658 cost.i16_mode,1659 cost.i16_mode,
1659 chroma_mode,1660 chroma_mode,
1660 );1661 );
1661- ssd_mb(pic, e, mb_x, mb_y) as f64 + e.lambda * bits as f641662+ ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, false) as f64 + e.lambda * bits as f64
1662 } else {1663 } else {
1663 ssd_luma(pic, e, mb_x, mb_y) as f641664 ssd_luma(pic, e, mb_x, mb_y) as f64
1664 + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f641665 + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f64
@@ -1783,6 +1784,95 @@ fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
1783 sum1784 sum
1784 }1785 }
1785 1786
1787+/// [`ssd_mb`] with the luma half measured after the loop filter this
1788+/// macroblock's reconstruction will get. An intra macroblock forces bS 4 on
1789+/// its left and top macroblock edges (8.7.2.1) and bS 3 on the internal luma
1790+/// edges; a transform-8x8 macroblock has no transform edge at luma offsets 4
1791+/// and 12, so those stay unfiltered, exactly as the decoder's `internal`
1792+/// gate decides it. Neighbours share this slice's qp with the zero offsets
1793+/// the encoder writes, so every edge is parameterised by the trial qp alone.
1794+/// The right and bottom edges belong to macroblocks coded later and cannot
1795+/// be known here; the aprons are the pre-filter neighbour samples, which is
1796+/// everything that exists at decision time. The chroma half is left
1797+/// pre-filter: chroma never takes the 8x8 transform, so it cannot separate
1798+/// the candidates.
1799+///
1800+/// The 8x8-vs-4x4 intra trial is judged by this and not by [`ssd_mb`]
1801+/// because the two candidates meet different edge sets under the same loop
1802+/// filter: a pre-filter SSD prices the 4x4 candidate as if its extra
1803+/// filtered edges cost nothing.
1804+fn ssd_mb_deblocked(
1805+ pic: &Picture,
1806+ e: &MbEnc<'_>,
1807+ mb_x: usize,
1808+ mb_y: usize,
1809+ nbr: &MbNeighbors,
1810+ trans8: bool,
1811+) -> i64 {
1812+ // The macroblock at (A, A) plus 4-sample left and top aprons: a bS-4
1813+ // edge reads p3 and rewrites p2 on its far side.
1814+ const A: usize = 4;
1815+ const S: usize = A + 16;
1816+ let mut scratch = [0u8; S * S];
1817+ let stride = pic.y.stride;
1818+ let origin = pic.y.at(mb_x * 16, mb_y * 16);
1819+ for row in 0..16 {
1820+ let src = origin + row * stride;
1821+ let dst = (A + row) * S + A;
1822+ scratch[dst..dst + 16].copy_from_slice(&pic.y.data[src..src + 16]);
1823+ }
1824+ if nbr.a {
1825+ for row in 0..16 {
1826+ let src = origin + row * stride - A;
1827+ let dst = (A + row) * S;
1828+ scratch[dst..dst + A].copy_from_slice(&pic.y.data[src..src + A]);
1829+ }
1830+ }
1831+ if nbr.b {
1832+ for row in 0..A {
1833+ let src = origin - (A - row) * stride;
1834+ let dst = row * S + A;
1835+ scratch[dst..dst + 16].copy_from_slice(&pic.y.data[src..src + 16]);
1836+ }
1837+ }
1838+ // Vertical edges left to right, then horizontal top to bottom, the order
1839+ // of 8.7.1: each edge filters what the earlier edges left behind.
1840+ let qp = e.target_qp;
1841+ let (e4, e3) = (edge_params(qp, 0, 0, 4), edge_params(qp, 0, 0, 3));
1842+ if nbr.a {
1843+ for row in 0..16 {
1844+ filter_luma_line(&mut scratch, (A + row) * S + A, 1, &e4);
1845+ }
1846+ }
1847+ for x in [4, 8, 12] {
1848+ if trans8 && x != 8 {
1849+ continue;
1850+ }
1851+ for row in 0..16 {
1852+ filter_luma_line(&mut scratch, (A + row) * S + A + x, 1, &e3);
1853+ }
1854+ }
1855+ if nbr.b {
1856+ filter_luma_h_edge16(&mut scratch, A * S + A, S, &e4);
1857+ }
1858+ for y in [4, 8, 12] {
1859+ if trans8 && y != 8 {
1860+ continue;
1861+ }
1862+ filter_luma_h_edge16(&mut scratch, (A + y) * S + A, S, &e3);
1863+ }
1864+ let mut sum = ssd_mb(pic, e, mb_x, mb_y) - ssd_luma(pic, e, mb_x, mb_y);
1865+ for row in 0..16 {
1866+ let src = (mb_y * 16 + row) * e.src.stride + mb_x * 16;
1867+ for x in 0..16 {
1868+ let d =
1869+ i64::from(e.src.y[src + x]) - i64::from(scratch[(A + row) * S + A + x]);
1870+ sum += d * d;
1871+ }
1872+ }
1873+ sum
1874+}
1875+
1786 /// Rough bit cost of a macroblock's luma residual.1876 /// Rough bit cost of a macroblock's luma residual.
1787 fn estimate_luma_bits(lv: &Levels) -> i64 {1877 fn estimate_luma_bits(lv: &Levels) -> i64 {
1788 let mut bits = 0;1878 let mut bits = 0;