diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 7092c231..44e040a1 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -2171,19 +2171,6 @@ mod tests { ffmpeg_decode_sequence(&stream, width, height, 2); } - /// The top-left `width` x `height` region of a decoded picture, for - /// comparing against [`Encoded::reconstruction`] (already cropped to - /// the same region) when ffmpeg hands back the padded, coded-size one. - fn crop_picture(picture: &Picture, width: usize, height: usize) -> Picture { - Picture { - width, - height, - y: crop_plane(&picture.y, picture.width, width, height), - u: crop_plane(&picture.u, picture.width / 2, width / 2, height / 2), - v: crop_plane(&picture.v, picture.width / 2, width / 2, height / 2), - } - } - /// `ffprobe`'s reported `width,height` for one OBU stream -- the coded /// frame size an AV1 decoder allocates, not necessarily the render size /// (see [`a_frame_round_trips_at_its_own_size`]). @@ -2283,6 +2270,14 @@ mod tests { /// EC_RNG-per-symbol trace against the same debug decoder found the /// bitstream byte-for-byte identical to libaom's across all 51188 /// symbols of this frame before this bug was found). + /// + /// Since the frame header started writing the true (unpadded) display + /// size into `frame_width`/`frame_height` rather than the padded coded + /// size, ffmpeg crops to 854x480 on its own -- decoding at the padded + /// 864x480 and cropping ourselves now reads past what ffmpeg actually + /// emits (`av1_common_int.h`'s render/upscale path, mirrored by + /// `av1_frame_size` on the decode side, crops the loop-filtered picture to + /// the header's own size before output). #[test] fn an_854x480_picture_round_trips_through_its_padding() { if !have_ffmpeg() { @@ -2291,13 +2286,10 @@ mod tests { let (width, height) = (854usize, 480usize); let picture = test_card(width, height); let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); - let (padded_width, padded_height) = ( - width.next_multiple_of(BLOCK), - height.next_multiple_of(BLOCK), - ); - let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height); - let cropped = crop_picture(&decoded, width, height); - assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma"); + let decoded = ffmpeg_decode(&encoded.stream, width, height); + assert_eq!(decoded.y, encoded.reconstruction.y, "854x480: luma"); + assert_eq!(decoded.u, encoded.reconstruction.u, "854x480: U"); + assert_eq!(decoded.v, encoded.reconstruction.v, "854x480: V"); } /// `Reach::top_right`/`bottom_left` against a from-scratch transcription diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index ec82c326..1133a43a 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -688,19 +688,38 @@ impl Neighbours { self.left_side[r + cell] = side; } let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); - let valid_h = side_mi.min(self.mi_rows.saturating_sub(mi_r)); - let valid_w = side_mi.min(self.mi_cols.saturating_sub(mi_c)); + // libaom rounds the luma edge up to the plane's own 4x4 unit before + // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high, + // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459): + // a chroma 4x4 unit straddling the true luma edge is still whole in + // chroma's own halved grid, so it stays valid one luma-mi row/col + // past where luma's own edge falls when that edge is odd. + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let bound_h = [ + self.mi_rows, + round_up_even(self.mi_rows), + round_up_even(self.mi_rows), + ]; + let bound_w = [ + self.mi_cols, + round_up_even(self.mi_cols), + round_up_even(self.mi_cols), + ]; for cell in 0..side_mi { - self.left[mi_r + cell] = if cell < valid_h { - states - } else { - Default::default() - }; - self.above[mi_c + cell] = if cell < valid_w { - states - } else { - Default::default() - }; + self.left[mi_r + cell] = std::array::from_fn(|plane| { + if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) { + states[plane] + } else { + Default::default() + } + }); + self.above[mi_c + cell] = std::array::from_fn(|plane| { + if cell < side_mi.min(bound_w[plane].saturating_sub(mi_c)) { + states[plane] + } else { + Default::default() + } + }); } }