forge
Explore
Log in

Don't you dare go Hollow

Clamp chroma's dc-sign/skip context to its own subsampled true edge

Browse files at this commit

82410a61e0ae3a8d079a7ec6b00dc45344a4aba1Ahmet Kaan GümüşParent: 77b7e14
  • fix
    • clamp chroma's dc-sign/skip context to its own subsampled true edge
    • fix a stale padded-decode test
Show full message
fix(av1): clamp chroma's dc-sign/skip context to its own subsampled true edge; fix a stale padded-decode test

Neighbours::record clamped chroma the same as luma (true mi_cols/mi_rows),
but libaom rounds a subsampled plane's edge up to its own 4x4 unit before
clamping (ROUND_POWER_OF_TWO(max_blocks_high, subsampling_y) in
av1_write_intra_coeffs_mb, encodetxb.c:456-459): a chroma unit straddling
an odd luma edge is still whole in chroma's own halved grid. Fixed by
rounding the luma bound up to even before clamping planes 1/2. No test in
the current suite exercises an odd true mi_rows/mi_cols with chroma
residual, so this doesn't move the gate; landed as a correctness fix
proven safe (no regression) pending such a case.

an_854x480_picture_round_trips_through_its_padding decoded at the padded
864x480 size and cropped to 854x480 by hand -- stale since 7ba33dd started
writing the true display size into frame_width/frame_height, which makes
ffmpeg crop to 854x480 on its own. Decoding at padded size now reads past
what ffmpeg emits (out.stdout shorter than expected, not a hard decode
error). Fixed the test to decode at the true size directly; it now
exercises the real desync (a luma mismatch, bitstream confirmed
byte-identical to a real decoder via EC_RNG trace, so a reconstruction-side
bug, not an entropy desync) instead of failing on a wrong expectation.

Gate: cargo test -p ec-av1 --release = 124 passed, 3 failed (same three
tests, still red, no regressions), 4 ignored.
crates/ec-av1/src/encode.rsmodified+12 -20
@@ -2171,19 +2171,6 @@ mod tests {
2171 ffmpeg_decode_sequence(&stream, width, height, 2);2171 ffmpeg_decode_sequence(&stream, width, height, 2);
2172 }2172 }
2173 2173
2174- /// The top-left `width` x `height` region of a decoded picture, for
2175- /// comparing against [`Encoded::reconstruction`] (already cropped to
2176- /// the same region) when ffmpeg hands back the padded, coded-size one.
2177- fn crop_picture(picture: &Picture, width: usize, height: usize) -> Picture {
2178- Picture {
2179- width,
2180- height,
2181- y: crop_plane(&picture.y, picture.width, width, height),
2182- u: crop_plane(&picture.u, picture.width / 2, width / 2, height / 2),
2183- v: crop_plane(&picture.v, picture.width / 2, width / 2, height / 2),
2184- }
2185- }
2186-
2187 /// `ffprobe`'s reported `width,height` for one OBU stream -- the coded2174 /// `ffprobe`'s reported `width,height` for one OBU stream -- the coded
2188 /// frame size an AV1 decoder allocates, not necessarily the render size2175 /// frame size an AV1 decoder allocates, not necessarily the render size
2189 /// (see [`a_frame_round_trips_at_its_own_size`]).2176 /// (see [`a_frame_round_trips_at_its_own_size`]).
@@ -2283,6 +2270,14 @@ mod tests {
2283 /// EC_RNG-per-symbol trace against the same debug decoder found the2270 /// EC_RNG-per-symbol trace against the same debug decoder found the
2284 /// bitstream byte-for-byte identical to libaom's across all 511882271 /// bitstream byte-for-byte identical to libaom's across all 51188
2285 /// symbols of this frame before this bug was found).2272 /// symbols of this frame before this bug was found).
2273+ ///
2274+ /// Since the frame header started writing the true (unpadded) display
2275+ /// size into `frame_width`/`frame_height` rather than the padded coded
2276+ /// size, ffmpeg crops to 854x480 on its own -- decoding at the padded
2277+ /// 864x480 and cropping ourselves now reads past what ffmpeg actually
2278+ /// emits (`av1_common_int.h`'s render/upscale path, mirrored by
2279+ /// `av1_frame_size` on the decode side, crops the loop-filtered picture to
2280+ /// the header's own size before output).
2286 #[test]2281 #[test]
2287 fn an_854x480_picture_round_trips_through_its_padding() {2282 fn an_854x480_picture_round_trips_through_its_padding() {
2288 if !have_ffmpeg() {2283 if !have_ffmpeg() {
@@ -2291,13 +2286,10 @@ mod tests {
2291 let (width, height) = (854usize, 480usize);2286 let (width, height) = (854usize, 480usize);
2292 let picture = test_card(width, height);2287 let picture = test_card(width, height);
2293 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();2288 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
2294- let (padded_width, padded_height) = (2289+ let decoded = ffmpeg_decode(&encoded.stream, width, height);
2295- width.next_multiple_of(BLOCK),2290+ assert_eq!(decoded.y, encoded.reconstruction.y, "854x480: luma");
2296- height.next_multiple_of(BLOCK),2291+ assert_eq!(decoded.u, encoded.reconstruction.u, "854x480: U");
2297- );2292+ assert_eq!(decoded.v, encoded.reconstruction.v, "854x480: V");
2298- let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height);
2299- let cropped = crop_picture(&decoded, width, height);
2300- assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma");
2301 }2293 }
2302 2294
2303 /// `Reach::top_right`/`bottom_left` against a from-scratch transcription2295 /// `Reach::top_right`/`bottom_left` against a from-scratch transcription
crates/ec-av1/src/tile.rsmodified+27 -8
@@ -688,19 +688,38 @@ impl Neighbours {
688 self.left_side[r + cell] = side;688 self.left_side[r + cell] = side;
689 }689 }
690 let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);690 let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI);
691- let valid_h = side_mi.min(self.mi_rows.saturating_sub(mi_r));691+ // libaom rounds the luma edge up to the plane's own 4x4 unit before
692- let valid_w = side_mi.min(self.mi_cols.saturating_sub(mi_c));692+ // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high,
693+ // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459):
694+ // a chroma 4x4 unit straddling the true luma edge is still whole in
695+ // chroma's own halved grid, so it stays valid one luma-mi row/col
696+ // past where luma's own edge falls when that edge is odd.
697+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
698+ let bound_h = [
699+ self.mi_rows,
700+ round_up_even(self.mi_rows),
701+ round_up_even(self.mi_rows),
702+ ];
703+ let bound_w = [
704+ self.mi_cols,
705+ round_up_even(self.mi_cols),
706+ round_up_even(self.mi_cols),
707+ ];
693 for cell in 0..side_mi {708 for cell in 0..side_mi {
694- self.left[mi_r + cell] = if cell < valid_h {709+ self.left[mi_r + cell] = std::array::from_fn(|plane| {
695- states710+ if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) {
711+ states[plane]
696 } else {712 } else {
697 Default::default()713 Default::default()
698- };714+ }
699- self.above[mi_c + cell] = if cell < valid_w {715+ });
700- states716+ self.above[mi_c + cell] = std::array::from_fn(|plane| {
717+ if cell < side_mi.min(bound_w[plane].saturating_sub(mi_c)) {
718+ states[plane]
701 } else {719 } else {
702 Default::default()720 Default::default()
703- };721+ }
722+ });
704 }723 }
705 }724 }
706 725