forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-h264rd3': bS derivation extracted, inter trial deblocking measured and rejected

Browse files at this commit

ba7f553d4f68bb3205d48159a6e130755ccd7726Ahmet Kaan GümüşParent: 5208ad8d3d7ad4
crates/ec-h264/src/decoder.rsmodified+71 -46
@@ -2624,61 +2624,36 @@ fn uniform_motion(pic: &Picture, mb_x: usize, mb_y: usize) -> bool {
26242624 })
26252625 }
26262626
2627-/// Boundary strength of one 4-sample luma edge segment (clause 8.7.2.1).
2628-///
2629-/// `edge` 0..4 selects the vertical (`vertical`) or horizontal edge inside the
2630-/// macroblock, `seg` the 4-sample run along it. Edge 0 is the macroblock edge.
2631-fn boundary_strength(
2632- pic: &Picture,
2633- mb_x: usize,
2634- mb_y: usize,
2635- edge: usize,
2636- seg: usize,
2637- vertical: bool,
2638-) -> u8 {
2639- let w4 = pic.mb_w * 4;
2640- let (qbx, qby) = if vertical {
2641- (mb_x * 4 + edge, mb_y * 4 + seg)
2642- } else {
2643- (mb_x * 4 + seg, mb_y * 4 + edge)
2644- };
2645- let (pbx, pby) = if vertical {
2646- (qbx - 1, qby)
2647- } else {
2648- (qbx, qby - 1)
2649- };
2650- let q = qby * w4 + qbx;
2651- let p = pby * w4 + pbx;
2652- let mb_edge = edge == 0;
2653- let q_mb = (qby / 4) * pic.mb_w + qbx / 4;
2654- let p_mb = (pby / 4) * pic.mb_w + pbx / 4;
2655- let intra = pic.mb_flags[q_mb] & FLAG_INTER == 0 || pic.mb_flags[p_mb] & FLAG_INTER == 0;
2656- if intra {
2627+/// What clause 8.7.2.1 needs from the 4x4 block on one side of a luma edge.
2628+#[derive(Clone, Copy)]
2629+pub(crate) struct BsSide {
2630+ /// The containing macroblock is intra: strength 4 on macroblock edges and
2631+ /// 3 on internal ones, regardless of motion or levels.
2632+ pub(crate) intra: bool,
2633+ /// The transform block containing this sample has non-zero levels.
2634+ pub(crate) coded: bool,
2635+ /// MvLX of the block, `[list][component]`.
2636+ pub(crate) mv: [[i16; 2]; 2],
2637+ /// RefIdxLX of the block as list identities (`Picture::id`), -1 unused.
2638+ pub(crate) refs: [i32; 2],
2639+}
2640+
2641+/// Boundary strength between the 4x4 blocks on the two sides of a luma edge
2642+/// (clause 8.7.2.1). `mb_edge` selects the macroblock-edge strengths 4/3.
2643+pub(crate) fn bs_between(p: &BsSide, q: &BsSide, mb_edge: bool) -> u8 {
2644+ if p.intra || q.intra {
26572645 return if mb_edge { 4 } else { 3 };
26582646 }
2659- // "the transform block containing the sample has non-zero levels": for an
2660- // 8x8-transform macroblock that is the whole 8x8, whose four 4x4 slots
2661- // carry per-slot counts under CAVLC (7.3.5.3.1 splits it into four blocks).
2662- let coded = |mb: usize, bx: usize, by: usize| -> bool {
2663- if pic.mb_flags[mb] & FLAG_TRANS8X8 == 0 {
2664- return pic.nz_y[by * w4 + bx] != 0;
2665- }
2666- let (ox, oy) = (bx & !1, by & !1);
2667- pic.nz_y[oy * w4 + ox] != 0
2668- || pic.nz_y[oy * w4 + ox + 1] != 0
2669- || pic.nz_y[(oy + 1) * w4 + ox] != 0
2670- || pic.nz_y[(oy + 1) * w4 + ox + 1] != 0
2671- };
2672- if coded(p_mb, pbx, pby) || coded(q_mb, qbx, qby) {
2647+ if p.coded || q.coded {
26732648 return 2;
26742649 }
2675- let (pr, qr) = (pic.ref_id[p], pic.ref_id[q]);
2650+ let (pr, qr) = (p.refs, q.refs);
26762651 let pn = u8::from(pr[0] >= 0) + u8::from(pr[1] >= 0);
26772652 let qn = u8::from(qr[0] >= 0) + u8::from(qr[1] >= 0);
26782653 if pn != qn {
26792654 return 1;
26802655 }
2681- let (pm, qm) = (pic.mv[p], pic.mv[q]);
2656+ let (pm, qm) = (p.mv, q.mv);
26822657 let differs = |a: [i16; 2], b: [i16; 2]| {
26832658 (i32::from(a[0]) - i32::from(b[0])).abs() >= 4
26842659 || (i32::from(a[1]) - i32::from(b[1])).abs() >= 4
@@ -2712,3 +2687,53 @@ fn boundary_strength(
27122687 let crossed = !differs(pm[0], qm[1]) && !differs(pm[1], qm[0]);
27132688 u8::from(!(straight || crossed))
27142689 }
2690+
2691+/// One side of a luma edge from picture state: the decoder path, both sides
2692+/// final macroblocks. The encoder's inter trial uses this for the
2693+/// already-coded neighbour and builds its candidate side itself.
2694+pub(crate) fn bs_side(pic: &Picture, bx: usize, by: usize) -> BsSide {
2695+ let w4 = pic.mb_w * 4;
2696+ let mb = (by / 4) * pic.mb_w + bx / 4;
2697+ BsSide {
2698+ intra: pic.mb_flags[mb] & FLAG_INTER == 0,
2699+ coded: bs_coded(pic, mb, bx, by),
2700+ mv: pic.mv[by * w4 + bx],
2701+ refs: pic.ref_id[by * w4 + bx],
2702+ }
2703+}
2704+
2705+/// "The transform block containing the sample has non-zero levels": for an
2706+/// 8x8-transform macroblock that is the whole 8x8, whose four 4x4 slots
2707+/// carry per-slot counts under CAVLC (7.3.5.3.1 splits it into four blocks).
2708+fn bs_coded(pic: &Picture, mb: usize, bx: usize, by: usize) -> bool {
2709+ let w4 = pic.mb_w * 4;
2710+ if pic.mb_flags[mb] & FLAG_TRANS8X8 == 0 {
2711+ return pic.nz_y[by * w4 + bx] != 0;
2712+ }
2713+ let (ox, oy) = (bx & !1, by & !1);
2714+ pic.nz_y[oy * w4 + ox] != 0
2715+ || pic.nz_y[oy * w4 + ox + 1] != 0
2716+ || pic.nz_y[(oy + 1) * w4 + ox] != 0
2717+ || pic.nz_y[(oy + 1) * w4 + ox + 1] != 0
2718+}
2719+
2720+/// Boundary strength of one 4-sample luma edge segment (clause 8.7.2.1).
2721+///
2722+/// `edge` 0..4 selects the vertical (`vertical`) or horizontal edge inside the
2723+/// macroblock, `seg` the 4-sample run along it. Edge 0 is the macroblock edge.
2724+fn boundary_strength(
2725+ pic: &Picture,
2726+ mb_x: usize,
2727+ mb_y: usize,
2728+ edge: usize,
2729+ seg: usize,
2730+ vertical: bool,
2731+) -> u8 {
2732+ let (qbx, qby) = if vertical {
2733+ (mb_x * 4 + edge, mb_y * 4 + seg)
2734+ } else {
2735+ (mb_x * 4 + seg, mb_y * 4 + edge)
2736+ };
2737+ let (pbx, pby) = if vertical { (qbx - 1, qby) } else { (qbx, qby - 1) };
2738+ bs_between(&bs_side(pic, pbx, pby), &bs_side(pic, qbx, qby), edge == 0)
2739+}
crates/ec-h264/src/enc/mb.rsmodified+73 -35
@@ -1608,7 +1608,8 @@ fn encode_intra_mb(
16081608 cost.i16_mode,
16091609 chroma_mode,
16101610 );
1611- ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, false) as f64 + e.lambda * bits as f64
1611+ ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, false, intra_bs) as f64
1612+ + e.lambda * bits as f64
16121613 } else {
16131614 ssd_luma(pic, e, mb_x, mb_y) as f64
16141615 + lambda_ssd(qp) * rough_intra_bits(&lv4, Some((&m4, &p4))) as f64
@@ -1640,7 +1641,8 @@ fn encode_intra_mb(
16401641 cost.i16_mode,
16411642 chroma_mode,
16421643 );
1643- ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, true) as f64 + e.lambda * bits as f64
1644+ ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, true, intra_bs) as f64
1645+ + e.lambda * bits as f64
16441646 } else {
16451647 f64::INFINITY
16461648 };
@@ -1659,7 +1661,8 @@ fn encode_intra_mb(
16591661 cost.i16_mode,
16601662 chroma_mode,
16611663 );
1662- ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, false) as f64 + e.lambda * bits as f64
1664+ ssd_mb_deblocked(pic, e, mb_x, mb_y, &nbr, false, intra_bs) as f64
1665+ + e.lambda * bits as f64
16631666 } else {
16641667 ssd_luma(pic, e, mb_x, mb_y) as f64
16651668 + lambda_ssd(qp) * rough_intra_bits(&lv16, None) as f64
@@ -1785,22 +1788,24 @@ fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
17851788 }
17861789
17871790 /// [`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.
1791+/// macroblock's reconstruction will get. `bs(edge, seg, vertical)` is the
1792+/// boundary strength of one 4-sample edge segment — edge 0 the macroblock
1793+/// edge, edges 1..3 the internal edges at offsets 4/8/12 — as the decoder
1794+/// will derive it, so an intra candidate passes its forced 4/3 and an inter
1795+/// candidate its own coded/motion strengths. A transform-8x8 macroblock has
1796+/// no transform edge at luma offsets 4 and 12, so those stay unfiltered,
1797+/// exactly as the decoder's `internal` gate decides it. Neighbours share
1798+/// this slice's qp with the zero offsets the encoder writes, so every edge
1799+/// is parameterised by the trial qp alone. The right and bottom edges
1800+/// belong to macroblocks coded later and cannot be known here; the aprons
1801+/// are the pre-filter neighbour samples, which is everything that exists at
1802+/// decision time. The chroma half is left pre-filter: chroma never takes
1803+/// the 8x8 transform, so it cannot separate the candidates.
17991804 ///
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.
1805+/// The 8x8-vs-4x4 trials are judged by this and not by [`ssd_mb`] because the
1806+/// two candidates meet different edge sets under the same loop filter: a
1807+/// pre-filter SSD prices the 4x4 candidate as if its extra filtered edges
1808+/// cost nothing.
18041809 fn ssd_mb_deblocked(
18051810 pic: &Picture,
18061811 e: &MbEnc<'_>,
@@ -1808,6 +1813,7 @@ fn ssd_mb_deblocked(
18081813 mb_y: usize,
18091814 nbr: &MbNeighbors,
18101815 trans8: bool,
1816+ bs: impl Fn(usize, usize, bool) -> u8,
18111817 ) -> i64 {
18121818 // The macroblock at (A, A) plus 4-sample left and top aprons: a bS-4
18131819 // edge reads p3 and rewrites p2 on its far side.
@@ -1836,30 +1842,52 @@ fn ssd_mb_deblocked(
18361842 }
18371843 }
18381844 // 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.
1845+ // of 8.7.1: each edge filters what the earlier edges left behind. Each
1846+ // 4-sample segment carries its own strength, as in the decoder.
18401847 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 {
1848+ for edge in 0..4 {
1849+ let x = edge * 4;
1850+ if (edge == 0 && !nbr.a) || (trans8 && x != 0 && x != 8) {
18491851 continue;
18501852 }
1851- for row in 0..16 {
1852- filter_luma_line(&mut scratch, (A + row) * S + A + x, 1, &e3);
1853+ for seg in 0..4 {
1854+ let s = bs(edge, seg, true);
1855+ if s == 0 {
1856+ continue;
1857+ }
1858+ let ep = edge_params(qp, 0, 0, s);
1859+ for row in seg * 4..seg * 4 + 4 {
1860+ filter_luma_line(&mut scratch, (A + row) * S + A + x, 1, &ep);
1861+ }
18531862 }
18541863 }
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 {
1864+ for edge in 0..4 {
1865+ let y = edge * 4;
1866+ if (edge == 0 && !nbr.b) || (trans8 && y != 0 && y != 8) {
1867+ continue;
1868+ }
1869+ let seg_bs = [
1870+ bs(edge, 0, false),
1871+ bs(edge, 1, false),
1872+ bs(edge, 2, false),
1873+ bs(edge, 3, false),
1874+ ];
1875+ if seg_bs[0] == seg_bs[1] && seg_bs[1] == seg_bs[2] && seg_bs[2] == seg_bs[3] {
1876+ if seg_bs[0] != 0 {
1877+ let ep = edge_params(qp, 0, 0, seg_bs[0]);
1878+ filter_luma_h_edge16(&mut scratch, (A + y) * S + A, S, &ep);
1879+ }
18601880 continue;
18611881 }
1862- filter_luma_h_edge16(&mut scratch, (A + y) * S + A, S, &e3);
1882+ for seg in 0..4 {
1883+ if seg_bs[seg] == 0 {
1884+ continue;
1885+ }
1886+ let ep = edge_params(qp, 0, 0, seg_bs[seg]);
1887+ for col in seg * 4..seg * 4 + 4 {
1888+ filter_luma_line(&mut scratch, (A + y) * S + A + col, S, &ep);
1889+ }
1890+ }
18631891 }
18641892 let mut sum = ssd_mb(pic, e, mb_x, mb_y) - ssd_luma(pic, e, mb_x, mb_y);
18651893 for row in 0..16 {
@@ -1873,6 +1901,16 @@ fn ssd_mb_deblocked(
18731901 sum
18741902 }
18751903
1904+/// Boundary strengths an intra candidate forces (8.7.2.1): 4 on its
1905+/// macroblock edges, 3 on the internal luma edges.
1906+fn intra_bs(edge: usize, _seg: usize, _vertical: bool) -> u8 {
1907+ if edge == 0 {
1908+ 4
1909+ } else {
1910+ 3
1911+ }
1912+}
1913+
18761914 /// Rough bit cost of a macroblock's luma residual.
18771915 fn estimate_luma_bits(lv: &Levels) -> i64 {
18781916 let mut bits = 0;
lanes/h264rd-r3.report.mdadded+91 -0
@@ -0,0 +1,91 @@
1+# h264rd r3 — deblock-aware inter trial distortion: rejected
2+
3+Lane `h264rd3`, worktree `ec-wt-h264rd3`. Charter: mirror r2's intra change
4+on the inter trial — price `encode_inter_mb`'s cost4/cost8 pair with a
5+deblocked SSD whose per-edge boundary strength is derived per 8.7.2.1
6+(coded → 2, mv/ref difference → 1, intra MB → 4/3), reusing the decoder's
7+own bS derivation rather than a second rule.
8+
9+**Verdict: every variant regressed the 720p clip; the encoder change is
10+reverted.** What ships is the behaviour-preserving extraction of the bS
11+derivation in `decoder.rs` (`BsSide`/`bs_side`/`bs_between`, `boundary_strength`
12+now composed from them) and the intra trial's `ssd_mb_deblocked` parametrised
13+by a `bs` closure (`intra_bs`) — output byte-identical to r2, confirmed by the
14+suite's byte-identity determinism tests and by the harness reproducing r2's
15+numbers exactly.
16+
17+## Variants measured (720p film / obs 1440p BD-PSNR, flag-on vs flag-off)
18+
19+Baseline = r2 (`da6a6bb`): **+0.827 dB** / **-0.000 dB**. Tolerance: no clip
20+below its baseline by > 0.005 dB; rise on 720p.
21+
22+| variant | 720p BD | Δ vs r2 | obs BD | verdict |
23+|---------|---------|---------|--------|---------|
24+| r2 baseline | +0.827 | — | -0.000 | — |
25+| V1 derived bS on cost4/cost8 | +0.818 | -0.009 | +0.001 | fail |
26+| V2 = V1 + deblocked skip re-check | +0.794 | -0.033 | +0.000 | fail |
27+| V3 = V1, weak edge-0 bS dropped | +0.821 | -0.006 | +0.000 | fail |
28+| reverted (shipped) | +0.827 | 0 | -0.000 | pass (=r2) |
29+
30+**V1 per-qp, 720p flag-on** (off rows identical to r2's, elided):
31+q22 9347720 bits 50.86 dB, q26 6095880 48.16, q30 3893464 45.24,
32+q34 2428872 42.36; 8x8 share 14.7/13.3/14.6/11.3 %.
33+**V2**: 9345712/50.86, 6087104/48.07, 3892752/45.24, 2429568/42.39.
34+**V3**: 9348064/50.86, 6097064/48.16, 3894768/45.24, 2428080/42.40.
35+obs per-qp rows moved by ≤ 0.01 dB in every variant; its BD stayed
36+within ±0.001 of flat the whole time.
37+
38+The four points are monotone: the more inter-edge filtering the trial
39+metric applies, the worse 720p gets. The deblocked-metric gain r2 found on
40+intra candidates does not transfer to inter candidates on this content.
41+
42+## Mechanism (what was built, why it loses)
43+
44+`inter_trial_bs(pic, mb_addr, mb_x, mb_y, lv)` answered `bs(edge, seg,
45+vertical)` with the decoder's own rule: the q side is always the candidate —
46+`BsSide { intra: false, coded: <lv.nz at that 4x4, or any-nonzero luma8 for
47+trans8>, mv/ref from pic (write_block already ran) }` — the p side is the
48+candidate again on internal edges, `bs_side(pic, …)` on macroblock edges.
49+`ssd_mb_deblocked` filtered its 24x24 scratch per (edge, seg) at that
50+strength.
51+
52+Why it loses: the trial's aprons are the neighbours' **pre-filter** samples —
53+the one state the encoder cannot know at decision time (the decoder filters
54+after the whole frame is coded, cascaded). For intra candidates (r2) the
55+forced bS 4/3 filtering dominates that staleness. For inter candidates the
56+derived strengths are mostly 1/2 — weak filters whose benefit is the same
57+order as the apron error — so pricing them adds model noise, not signal.
58+The variant ladder confirms the split: V3 (which dropped exactly the weak
59+macroblock-edge strengths against stale aprons, keeping the self-consistent
60+internal edges) recovered 0.003 of V1's 0.009 loss, and V2 (more deblocking
61+of the skip re-check, same stale aprons) lost a further 0.024. The
62+remaining -0.006 in V3 is the honest price of the internal bS-2 modelling:
63+on this film content even the self-consistent part does not pay for the inter
64+trial.
65+
66+## What ships
67+
68+- `crates/ec-h264/src/decoder.rs`: `bs_side`/`bs_between` extracted from
69+ `boundary_strength` (the 8.7.2.1 rule in one place, `pub(crate)` so the
70+ encoder can reach it); `boundary_strength` is now coords →
71+ `bs_between(&bs_side(p), &bs_side(q), mb_edge)`. No behaviour change.
72+- `crates/ec-h264/src/enc/mb.rs`: `ssd_mb_deblocked` takes a
73+ `bs(edge, seg, vertical)` closure instead of hard-coded 4/3; the three
74+ intra trial costs pass `intra_bs` (edge 0 → 4, else 3 — the same values).
75+ Per-segment strengths are now plumbed (uniform-segment fast path kept).
76+ Inter cost4/cost8 and the skip re-check stay pre-filter, exactly r2.
77+
78+## Gates (reverted state)
79+
80+1. `cargo test --release -p ec-h264` — **75+23+9+1 passed, 0 failed**
81+ (byte-identity determinism tests confirm flag-off/flag-on output matches
82+ r2 bit-for-bit).
83+2. Harness both clips: 720p **+0.827 dB**, obs **-0.000 dB** — identical to
84+ the r2 baselines (pass, no regression, no gain).
85+3. `cargo test --release -p ec-h264 --test conformance` — **23 passed,
86+ 0 failed**.
87+
88+Scope cuts (moot after the revert, but measured as V2): the skip re-check was
89+chartered as the fallback variant and made things worse, so it stays
90+pre-filter permanently on this evidence. Chroma half and right/bottom edges
91+remain r2's scope cuts.