Merge branch 'lane-h265dec' (r4): HEVC decoder handles all odd sizes
Browse files at this commit3 changed files +54 -15
- crates/ec-h265/src/ctu.rs +9 -1
- crates/ec-h265/src/decode.rs +34 -6
- crates/ec-h265/tests/decode.rs +11 -8
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+34 -6
| @@ -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); |
| @@ -505,10 +512,25 @@ impl CtuDecoder { | |||
| 505 | mode | 512 | mode |
| 506 | }; | 513 | }; |
| 507 | modes[part] = mode; | 514 | modes[part] = mode; |
| 508 | - // Mode only, so the next partition's MPM sees it; pixels follow | 515 | + // Mode and availability, so the next partition's `mpm_at` sees |
| 509 | - // once the residual syntax below is read. | 516 | + // this one as a real coded neighbour — the encoder's own |
| 517 | + // mode-decide loop calls `mark_coded` (which flips availability) | ||
| 518 | + // immediately per partition for exactly this reason (8.4.2: an | ||
| 519 | + // NxN CU's later partitions see its earlier ones as available). | ||
| 520 | + // Marking modes here without also marking `decoded` left the | ||
| 521 | + // second/third/fourth partition's left/above neighbour reading | ||
| 522 | + // as unavailable (substituted DC) on the decoder side while the | ||
| 523 | + // encoder's MPM saw the real mode, decoding the wrong MPM triple | ||
| 524 | + // and desyncing every bin after it — this is what an NxN CU | ||
| 525 | + // forced into existence by a boundary split (rather than chosen | ||
| 526 | + // by `intra_nxn`'s RD search) actually exercises differently: | ||
| 527 | + // the RD search rarely keeps NxN once a real neighbour is | ||
| 528 | + // available on all four sides, so the bug went unseen until a | ||
| 529 | + // picture edge forced it. | ||
| 510 | let stride = self.width / 4; | 530 | let stride = self.width / 4; |
| 511 | - self.modes[(py / 4) * stride + px / 4] = mode; | 531 | + let idx = (py / 4) * stride + px / 4; |
| 532 | + self.modes[idx] = mode; | ||
| 533 | + self.decoded[idx] = true; | ||
| 512 | } | 534 | } |
| 513 | 535 | ||
| 514 | let chroma_mode = self.decode_chroma_mode(dec, modes[0]); | 536 | let chroma_mode = self.decode_chroma_mode(dec, modes[0]); |
| @@ -522,7 +544,13 @@ impl CtuDecoder { | |||
| 522 | self.mark_coded(px, py, 4, modes[part], depth); | 544 | self.mark_coded(px, py, 4, modes[part], depth); |
| 523 | self.mark_tu(px, py, 4); | 545 | self.mark_tu(px, py, 4); |
| 524 | if part == 3 { | 546 | if part == 3 { |
| 525 | - self.reconstruct_chroma(x, y, 8, chroma_mode, 2, cb_cbf, cr_cbf, dec); | 547 | + // `reconstruct_chroma`'s `log2_luma` is the covering luma |
| 548 | + // block's log2 (it derives `clog2 = log2_luma - 1` for the | ||
| 549 | + // transform-split convention); an NxN CU's covering block is | ||
| 550 | + // the whole 8x8 CU (`MIN_CB_LOG2`), not a 4x4 partition — | ||
| 551 | + // passing `2` here derived `clog2 = 1` and underflowed | ||
| 552 | + // `decode_residual`'s `log2_size - 2`. | ||
| 553 | + self.reconstruct_chroma(x, y, 8, chroma_mode, MIN_CB_LOG2, cb_cbf, cr_cbf, dec); | ||
| 526 | } | 554 | } |
| 527 | } | 555 | } |
| 528 | } | 556 | } |
crates/ec-h265/tests/decode.rsmodified+11 -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 the | 29 | // 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,16 @@ 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 \ | ||
| 63 | -row boundary' — the decoder's WPP substream handling at a sub-CTB-tail row is \ | ||
| 64 | -wrong or the single-slice no-WPP case is misdetected; whole-CTB sizes decode \ | ||
| 65 | -byte-exact. Next round fixes this, then un-ignores."] | ||
| 66 | fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() { | 59 | fn decode_matches_the_encoders_own_reconstruction_at_an_odd_padded_size() { |
| 67 | roundtrip_at(70, 50); | 60 | roundtrip_at(70, 50); |
| 68 | } | 61 | } |
| 62 | + | ||
| 63 | +#[test] | ||
| 64 | +fn decode_matches_the_encoders_own_reconstruction_at_another_odd_size() { | ||
| 65 | + roundtrip_at(150, 94); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +#[test] | ||
| 69 | +fn decode_matches_the_encoders_own_reconstruction_at_an_extreme_odd_size() { | ||
| 70 | + roundtrip_at(41, 33); | ||
| 71 | +} | ||