Clamp chroma's dc-sign/skip context to its own subsampled true edge
Browse files at this commit82410a61e0ae3a8d079a7ec6b00dc45344a4aba1Parent: 77b7e14- fix
- clamp chroma's dc-sign/skip context to its own subsampled true edge
- fix a stale padded-decode test
2 changed files +43 -32
- crates/ec-av1/src/encode.rs +12 -20
- crates/ec-av1/src/tile.rs +31 -12
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 coded | 2174 | /// `ffprobe`'s reported `width,height` for one OBU stream -- the coded |
| 2188 | /// frame size an AV1 decoder allocates, not necessarily the render size | 2175 | /// 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 the | 2270 | /// EC_RNG-per-symbol trace against the same debug decoder found the |
| 2284 | /// bitstream byte-for-byte identical to libaom's across all 51188 | 2271 | /// 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 transcription | 2295 | /// `Reach::top_right`/`bottom_left` against a from-scratch transcription |
crates/ec-av1/src/tile.rsmodified+31 -12
| @@ -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 | - states | 710 | + if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) { |
| 696 | - } else { | 711 | + states[plane] |
| 697 | - Default::default() | 712 | + } else { |
| 698 | - }; | 713 | + Default::default() |
| 699 | - self.above[mi_c + cell] = if cell < valid_w { | 714 | + } |
| 700 | - states | 715 | + }); |
| 701 | - } else { | 716 | + self.above[mi_c + cell] = std::array::from_fn(|plane| { |
| 702 | - Default::default() | 717 | + if cell < side_mi.min(bound_w[plane].saturating_sub(mi_c)) { |
| 703 | - }; | 718 | + states[plane] |
| 719 | + } else { | ||
| 720 | + Default::default() | ||
| 721 | + } | ||
| 722 | + }); | ||
| 704 | } | 723 | } |
| 705 | } | 724 | } |
| 706 | 725 | ||