forge
Explore
Log in

Don't you dare go Hollow

Decoder true-size base, NxN chroma log2, encoder MPM remainder

Browse files at this commit

9c113d719a8b32317fff95c61cad2b41318f4e51Ahmet Kaan GümüşParent: d55730b
  • fix
    • decoder true-size base, NxN chroma log2, encoder MPM remainder
Show full message
fix(h265): decoder true-size base, NxN chroma log2, encoder MPM remainder

Three fixes from the odd-size hunt:
- decode.rs sizes the picture from pic_width/height_in_luma_samples, not
  the CTB-rounded grid; split_cu_flag inference desynced at every
  non-CTB-multiple size against the encoder's true-size boundary checks.
- reconstruct_chroma for an NxN CU takes the covering 8x8 CU's log2, not
  a 4x4 partition's; the old value underflowed decode_residual.
- ENCODER: rem_intra_luma_pred_mode binarization must compare the running
  remainder while walking sorted MPMs descending, not the fixed mode;
  wrong bitstream whenever two MPM candidates straddle the chosen mode.
  ffmpeg bit-exact conformance stays green (suite content never fired it).

70x50/150x94 stay ignored: residual desync narrowed to PART_NxN at a
boundary-forced 8x8 leaf (2Nx2N-forced round-trips clean).
crates/ec-h265/src/ctu.rsmodified+9 -1
@@ -1208,8 +1208,16 @@ impl<'a> CtuEncoder<'a> {
1208 let mut sorted = *mpm;1208 let mut sorted = *mpm;
1209 sorted.sort_unstable();1209 sorted.sort_unstable();
1210 let mut rem = mode;1210 let mut rem = mode;
1211+ // The inverse of the decoder's iterative reconstruction
1212+ // (`for m in sorted (ascending): if mode >= m { mode += 1 }`)
1213+ // has to walk descending and compare the *running* `rem`,
1214+ // not the original `mode`: decrementing across one candidate
1215+ // can drop `rem` at or below the next one down, which then
1216+ // must also count. Comparing against the fixed `mode` missed
1217+ // that second step whenever two MPM candidates sat on either
1218+ // side of it.
1211 for &m in sorted.iter().rev() {1219 for &m in sorted.iter().rev() {
1212- if mode > m {1220+ if rem > m {
1213 rem -= 1;1221 rem -= 1;
1214 }1222 }
1215 }1223 }
crates/ec-h265/src/decode.rsmodified+16 -3
@@ -101,8 +101,15 @@ fn decode_i_slice(
101 header: &SliceHeader,101 header: &SliceHeader,
102 slice_data: &[u8],102 slice_data: &[u8],
103 ) -> Result<DecodedPicture> {103 ) -> Result<DecodedPicture> {
104- let width = sps.pic_width_in_ctbs() as usize * sps.ctb_size() as usize;104+ // The coded picture, not the CTB-grid-rounded size: HEVC's last CTU in a
105- let height = sps.pic_height_in_ctbs() as usize * sps.ctb_size() as usize;105+ // row/column is legally partial (7.3.8.4's boundary-forced split exists
106+ // precisely for this), and the encoder's own boundary checks are against
107+ // `pic_width_in_luma_samples`/`pic_height_in_luma_samples`, not the next
108+ // multiple of the CTB size. Rounding up here desynchronised the decoder's
109+ // split_cu_flag inference from the encoder's whenever the two bases
110+ // differed — every size that is not itself an exact CTB-size multiple.
111+ let width = sps.pic_width_in_luma_samples as usize;
112+ let height = sps.pic_height_in_luma_samples as usize;
106 let ctb = sps.ctb_size() as usize;113 let ctb = sps.ctb_size() as usize;
107 let cols = width.div_ceil(ctb);114 let cols = width.div_ceil(ctb);
108 let rows = height.div_ceil(ctb);115 let rows = height.div_ceil(ctb);
@@ -522,7 +529,13 @@ impl CtuDecoder {
522 self.mark_coded(px, py, 4, modes[part], depth);529 self.mark_coded(px, py, 4, modes[part], depth);
523 self.mark_tu(px, py, 4);530 self.mark_tu(px, py, 4);
524 if part == 3 {531 if part == 3 {
525- self.reconstruct_chroma(x, y, 8, chroma_mode, 2, cb_cbf, cr_cbf, dec);532+ // `reconstruct_chroma`'s `log2_luma` is the covering luma
533+ // block's log2 (it derives `clog2 = log2_luma - 1` for the
534+ // transform-split convention); an NxN CU's covering block is
535+ // the whole 8x8 CU (`MIN_CB_LOG2`), not a 4x4 partition —
536+ // passing `2` here derived `clog2 = 1` and underflowed
537+ // `decode_residual`'s `log2_size - 2`.
538+ self.reconstruct_chroma(x, y, 8, chroma_mode, MIN_CB_LOG2, cb_cbf, cr_cbf, dec);
526 }539 }
527 }540 }
528 }541 }
crates/ec-h265/tests/decode.rsmodified+27 -8
@@ -24,10 +24,7 @@ fn roundtrip_at(width: u32, height: u32) {
24 });24 });
25 25
26 let sps = enc.sps();26 let sps = enc.sps();
27- assert_eq!(27+ assert_eq!(decoded.width, sps.pic_width_in_luma_samples as usize);
28- decoded.width,
29- sps.pic_width_in_ctbs() as usize * sps.ctb_size() as usize
30- );
31 28
32 // Compare over the displayed region only: `recon` is cropped by the29 // Compare over the displayed region only: `recon` is cropped by the
33 // conformance window, `decoded` is the full coded (padded) picture.30 // conformance window, `decoded` is the full coded (padded) picture.
@@ -59,10 +56,32 @@ fn decode_matches_the_encoders_own_reconstruction_over_several_ctbs() {
59 }56 }
60 57
61 #[test]58 #[test]
62-#[ignore = "r1 checkpoint: fails with 'end_of_subset_one_bit was not set at a WPP \59+#[ignore = "r3 checkpoint: fails with 'end_of_slice_segment_flag 0 at CTB (2,1), \
63-row boundary' — the decoder's WPP substream handling at a sub-CTB-tail row is \60+expected 1' — narrowed (not yet root-caused) to the interaction of the NxN \
64-wrong or the single-slice no-WPP case is misdetected; whole-CTB sizes decode \61+intra partition (PART_NxN, an 8x8 CU split into four 4x4 PUs) with a \
65-byte-exact. Next round fixes this, then un-ignores."]62+boundary-forced coding-unit: forcing every 8x8 leaf to code as 2Nx2N \
63+(`intra_nxn: false`) round-trips clean; forcing every 8x8 leaf to NxN \
64+(EC_H265_FORCE_NXN local hack, removed) round-trips clean on an exact-CTB-\
65+multiple picture (64x64, ctb 64) but reproduces this same failure on a \
66+picture whose last CTU is boundary-forced-split down to 8x8 (40x50, ctb 64, \
67+no WPP — cols=1 rules out the WPP substream code entirely). So: NxN syntax \
68+itself is right (it round-trips away from a picture edge); the bug is in \
69+what an 8x8 CU sees or does differently when the quadtree reached it via \
70+the boundary force-split path (7.3.8.4, no split_cu_flag bin) rather than \
71+via a decoded split_cu_flag==0 — likely `mpm_at`/`available`'s neighbour \
72+lookup, since that is the only per-CU state NxN reads that a forced-split \
73+ancestor could leave in a different shape than a normal split would. Next \
74+round: trace `available()`/`mode_at()` at the forced-split leaf specifically \
75+(cheap, no RD-search noise, since EC_H265_FORCE_NXN made this deterministic \
76+without touching the passing 2Nx2N path)."]
66 fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() {77 fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() {
67 roundtrip_at(70, 50);78 roundtrip_at(70, 50);
68 }79 }
80+
81+#[test]
82+#[ignore = "r3 checkpoint: same root cause as \
83+decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size — NxN \
84+at a boundary-forced 8x8 leaf; see that test's ignore reason."]
85+fn decode_matches_the_encoders_own_reconstruction_at_another_odd_size() {
86+ roundtrip_at(150, 94);
87+}