# h264rd r2 — deblock-aware intra trial distortion Lane `h264rd2`, worktree `ec-wt-h264rd2`. Goal inherited from r1: the opt-in `transform_8x8` flag lost ~0.1 dB BD-PSNR on natural content (won on synthetic). r1 (`3165e8c`) re-priced the 8x8 RD by exact syntax bit delta; r2 makes the intra trial distortion itself deblock-aware. ## Harness `real_clip_t8x8_bd_psnr` in `crates/ec-h264/tests/encode.rs` (`#[ignore]`d, existing test binary). `EC_H264_CLIP` names a file; ec-mp4 + this crate's decoder yield the first 30 pictures 1 s apart; a q22/26/30/34 sweep (gop 10, CABAC, full size, framerate 1.0) prints per-qp points, BD-PSNR flag-on vs flag-off, and the 8x8 MB share. Asserts nothing — the number gates the lane, not the suite. EC_H264_CLIP= cargo test --release -p ec-h264 --test encode \ real_clip_t8x8_bd_psnr -- --ignored --nocapture ## Baselines (harness commit `d5b0525`, before the r2 change) **natural 720p film** (1280x720, 30 pictures, 8x8 share 13.5 % avg): | qp | off bits | off dB | on bits | on dB | 8x8 share | |----|----------|--------|---------|-------|-----------| | 22 | 9538320 | 50.06 | 9345832 | 50.87 | 14.6 % | | 26 | 6220816 | 47.73 | 6105104 | 48.17 | 13.5 % | | 30 | 3975904 | 44.14 | 3911680 | 45.21 | 14.7 % | | 34 | 2474616 | 42.36 | 2442024 | 42.45 | 11.3 % | **BD-PSNR +0.796 dB.** The r1 loss on this class is already closed by the exact-bits pricing — the r1-era -0.102 dB was measured under the old rough-bit cost model. r2's job became: don't regress it, push it. **obs 1440p capture** (2560x1440, 19 pictures, 8x8 share 1.9 % avg): q22 52.41/52.44, q26 49.01/49.00, q30 45.32/45.19, q34 41.28/41.43 dB — **BD-PSNR -0.007 dB.** ## Change (`da6a6bb`) `ssd_mb_deblocked` in `crates/ec-h264/src/enc/mb.rs`, swapped into the three `t8x8_intra` trial costs (cost4/cost8/cost16) of `encode_intra_mb`: - Copy the candidate's 16x16 luma recon plus 4-px left/top aprons (gated on `nbr.a`/`nbr.b`, the same availability intra prediction uses) into a 24x24 scratch; the apron feeds the bS-4 p3 read / p2 rewrite. - Filter exactly as `deblock_picture` will: bS 4 on the MB edges, bS 3 on internal edges, verticals left-to-right then horizontals, `edge_params(qp, 0, 0, bs)`; the 8x8 candidate skips luma edges 4/12 — the decoder's `internal()` gate — so each candidate meets its own true edge set. - Luma SSD on the filtered scratch vs source, composited with `ssd_mb - ssd_luma` for the untouched chroma half (chroma never takes the 8x8 transform; it cannot separate the candidates). - Flag-off path untouched: default output stays byte-identical (structural; only the three `e.t8x8_intra(mb_y)` branches changed). ## After (feature commit `da6a6bb`) **natural 720p film** (8x8 share 13.5 % avg): | qp | off bits | off dB | on bits | on dB | 8x8 share | |----|----------|--------|---------|-------|-----------| | 22 | 9538320 | 50.06 | 9349104 | 50.87 | 14.7 % | | 26 | 6220816 | 47.73 | 6098024 | 48.18 | 13.3 % | | 30 | 3975904 | 44.14 | 3888736 | 45.25 | 14.7 % | | 34 | 2474616 | 42.36 | 2426344 | 42.35 | 11.4 % | **BD-PSNR +0.827 dB** (was +0.796; +0.031). **obs 1440p capture** (8x8 share 1.8 % avg): q22 52.41/52.44, q26 49.01/49.02, q30 45.32/45.20, q34 41.28/41.46 dB — **BD-PSNR -0.000 dB** (was -0.007; +0.007). Mechanism visible in the points: same or better PSNR at fewer bits where 8x8 competes (720p q30: 45.21 dB @ 3912 kb → 45.25 dB @ 3889 kb); the share is stable, so the gain is truer candidate pricing, not mode flipping. ## Variants measured / rejected - None needed: the primary design passed both clip gates on the first measurement. Deliberate scope cuts (chroma half pre-filter; aprons pre-filter; no right/bottom edges — those belong to MBs coded later; neighbours at slice qp rather than tracked per-MB) stayed as designed. ## Gates 1. `cargo test --release -p ec-h264` — **75+23+9+1 passed, 0 failed** (includes the synthetic gate `eight_by_eight_gains_on_flat_and_text` pinning BD ≥ 0.255347 - 0.02 dB and the byte-identity determinism tests). 2. Harness both clips: 720p +0.827 ≥ -0.02; obs -0.000, not below its -0.007 baseline — **pass**. 3. `cargo test --release -p ec-h264 --test conformance` — **23 passed, 0 failed**. Out of scope by charter: the inter trial (`encode_inter_mb` cost4/cost8).