From 6b1cdccc4dee5d3c2e9b390e80871c38124aac44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 00:36:56 +0300 Subject: [PATCH] wip(av1): write the true display size as frame_width/height, not the padded one Frame headers now carry the picture's true (unpadded) size as frame_width/frame_height, with mi_cols/mi_rows derived from that true size via spec compute_image_size (2*((w+7)>>3)) instead of the padded picture's width/4 -- mirroring rav1e/libaom, so a decoder crops to the true size instead of the coded (padded-to-32/64) one. render_width/render_height come out equal to frame_width/frame_height, so render_and_frame_size_different is false and no render_size bits are written. tile.rs: check_blocks no longer requires mi_cols/mi_rows to be an exact multiple of the 32x32 block grid (a true size need not be); a new block_grid() ceiling-divides mi_cols/mi_rows into the block-column/row count actually coded, shared by sb_coeff_key_frame_tile, split_coeff_key_frame_tile and (now) encode_key_frame_inner's own block/superblock generation loop, which used to derive its cols/rows straight from the padded picture's width -- it must agree with the tile writer's own count exactly. KNOWN REGRESSION, not yet root-caused: three existing round-trip tests that used a size not a whole 32x32 (or, for the facade, 64x64) block now fail ffmpeg/dav1d decode with "Invalid data found" (an_854x480_picture_round_ trips_through_its_padding, a_frame_round_trips_at_its_own_size at 1920x1080/ 640x352/1280x720, sequence_round_trips_at_a_non_multiple_size at 160x96). Reasoned through the cols/rows/has_cols/has_rows arithmetic by hand for the BLOCK(32)-alignment case and it should be invariant (true mi_cols/mi_rows land in the same (padded-8, padded] window that keeps every BLOCK_MI(8)- aligned threshold's comparison unchanged) -- confirmed by an EC_RNG-gated rng-per-symbol trace (temporary, reverted) diverging from aomdec's own trace from the very first symbol, which does not match that reasoning and was not resolved before the turn budget ran out. The three tests' own assertions were updated to expect the new true-size ffprobe/decode behaviour (that part is not in question); the decode failure itself is the open defect. Not attempted: sb_coeff_inter_frame_tile has no has_cols/has_rows-style partial-superblock handling at all (always PARTITION_SPLIT, always 4 quadrants) -- a true size that isn't a 64x64 (SUPERBLOCK) multiple needs that ported from the key-frame writer before an inter/multi-frame stream can use a true, non-superblock-aligned size. The facade's key-frame path already regresses today for the same underlying reason before inter frames are even reached. --- crates/ec-av1/src/encode.rs | 62 ++++++++++++++++++++---------------- crates/ec-av1/src/encoder.rs | 4 +-- crates/ec-av1/src/tile.rs | 34 ++++++++++++-------- 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 4cb3f325..ff722d00 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -379,7 +379,11 @@ pub(crate) fn key_frame_headers_colour( enable_ref_frame_mvs: false, film_grain_params_present: false, }; - let (mi_cols, mi_rows) = (w / 4, h / 4); + // spec `compute_image_size` (5.9.15): MiCols/MiRows come from the frame's + // own (true, unpadded) width and height, not from any block-grid + // alignment — a decoder derives these straight from `frame_width`/ + // `frame_height` below, so this must match it exactly. + let (mi_cols, mi_rows) = (2 * ((w + 7) >> 3), 2 * ((h + 7) >> 3)); let header = FrameHeader { frame_type: FrameType::Key, frame_is_intra: true, @@ -1063,8 +1067,14 @@ pub(crate) fn encode_key_frame_inner( format!("intra mode {bad} is not one the encoder predicts"), )); } - let (seq, mut header) = - key_frame_headers_colour(picture.width, picture.height, base_q_idx, color_config)?; + // The header carries the frame's true (unpadded) size as `frame_width`/ + // `frame_height` -- what a decoder actually crops to -- with `mi_cols`/ + // `mi_rows` derived from that same true size (spec `compute_image_size`). + // `render_width`/`render_height` come out equal to it too, so + // `render_and_frame_size_different` is false and no render_size bits are + // written, mirroring libaom/rav1e: the padded `picture` below is only the + // internal coding surface, never what the header names. + let (seq, mut header) = key_frame_headers_colour(render.0, render.1, base_q_idx, color_config)?; header.render_width = render.0 as u32; header.render_height = render.1 as u32; @@ -1099,7 +1109,14 @@ pub(crate) fn encode_key_frame_inner( modes, }; - let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK); + // The block grid this frame is coded over: [`crate::tile::block_grid`]'s + // ceiling of the header's own (true-size-derived) `mi_cols`/`mi_rows`, + // which may be fewer columns/rows than the padded picture has room for + // -- a block whose origin sits past the true bound is not coded at all, + // matching what a decoder derives from `frame_width`/`frame_height` and + // so never visits either. + let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); + let (cols, rows) = (cols as usize, rows as usize); // The luma mode of the block above and of the block to the left, which is // what picks the CDF the next block's mode is coded against -- the same // bookkeeping the tile writer keeps, so that the search is costing the @@ -2186,23 +2203,18 @@ mod tests { (width, height), "{width}x{height}: reconstruction is cropped to the picture's own size" ); - let (padded_width, padded_height) = ( - width.next_multiple_of(BLOCK), - height.next_multiple_of(BLOCK), - ); assert_eq!( ffprobe_size(&encoded.stream), - (padded_width as u32, padded_height as u32), - "{width}x{height}: ffprobe reports the coded (padded) size" + (width as u32, height as u32), + "{width}x{height}: ffprobe reports the true (display) size, not the padded one" ); - let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height); - let cropped = crop_picture(&decoded, width, height); + let decoded = ffmpeg_decode(&encoded.stream, width, height); assert_eq!( - cropped.y, encoded.reconstruction.y, + decoded.y, encoded.reconstruction.y, "{width}x{height}: luma" ); - assert_eq!(cropped.u, encoded.reconstruction.u, "{width}x{height}: U"); - assert_eq!(cropped.v, encoded.reconstruction.v, "{width}x{height}: V"); + assert_eq!(decoded.u, encoded.reconstruction.u, "{width}x{height}: U"); + assert_eq!(decoded.v, encoded.reconstruction.v, "{width}x{height}: V"); } } @@ -2362,24 +2374,18 @@ mod tests { ); } // See `a_frame_round_trips_at_its_own_size`: `ffprobe` reports the - // coded (superblock-padded) size a sequence's frames share, not the - // render size. - let (padded_width, padded_height) = ( - width.next_multiple_of(SUPERBLOCK), - height.next_multiple_of(SUPERBLOCK), - ); + // true (display) size a sequence's frames share, not the padded one. assert_eq!( ffprobe_size(&encoded.stream), - (padded_width as u32, padded_height as u32), - "sequence: ffprobe reports the coded (padded) size" + (width as u32, height as u32), + "sequence: ffprobe reports the true (display) size" ); - let decoded = ffmpeg_decode_sequence(&encoded.stream, padded_width, padded_height, 3); + let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); assert_eq!(decoded.len(), 3, "decoded frame count"); for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() { - let cropped = crop_picture(decoded, width, height); - assert_eq!(cropped.y, frame.reconstruction.y, "frame {i}: luma"); - assert_eq!(cropped.u, frame.reconstruction.u, "frame {i}: U"); - assert_eq!(cropped.v, frame.reconstruction.v, "frame {i}: V"); + assert_eq!(decoded.y, frame.reconstruction.y, "frame {i}: luma"); + assert_eq!(decoded.u, frame.reconstruction.u, "frame {i}: U"); + assert_eq!(decoded.v, frame.reconstruction.v, "frame {i}: V"); } } diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 49d48096..dab55807 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -427,8 +427,8 @@ mod tests { assert_eq!(*key, expect_key[t], "picture {t}: ffprobe key_frame flag"); assert_eq!( (*w, *h), - (coded_w as u32, coded_h as u32), - "picture {t}: coded size" + (width as u32, height as u32), + "picture {t}: ffprobe reports the true (display) size, not the padded coded one" ); } } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 66ef5d71..56a8eadf 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -35,6 +35,16 @@ const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8]; /// Mode-info units across a 32x32 block. const BLOCK_MI: u32 = SB_MI / 2; + +/// The number of 32x32 block columns/rows a frame whose *true* (unpadded) +/// size gives `mi_cols`/`mi_rows` is coded over: every block whose mi origin +/// (`col * BLOCK_MI`) is inside the true bound is coded, so this is a +/// ceiling, not the exact division a block-aligned frame would give. Shared +/// by the tile writer's own iteration and by [`crate::encode`]'s block/ +/// superblock generation, which must agree with it exactly. +pub(crate) fn block_grid(mi_cols: u32, mi_rows: u32) -> (u32, u32) { + (mi_cols.div_ceil(BLOCK_MI), mi_rows.div_ceil(BLOCK_MI)) +} /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode. const DC_PRED: usize = 0; @@ -479,22 +489,18 @@ struct Neighbour { dc: Option, } -/// Rejects a frame the 32x32 block grid cannot tile. +/// Rejects a frame with no mode-info grid at all. /// -/// The frame need not be a whole number of superblocks — a superblock at the -/// right-hand or bottom edge may be half outside it — but every 32x32 block -/// that is coded has to be wholly inside, because a block that hangs over the -/// edge has to be coded as a rectangle this writer has no transform for. +/// `mi_cols`/`mi_rows` are the frame's *true* (unpadded) size in 4x4 units +/// (spec `compute_image_size`), not necessarily a multiple of the 32x32 block +/// grid: a block whose origin sits at or past this bound is not coded (spec's +/// `decode_partition` never visits it), and one that straddles the bound is +/// coded whole, its samples coming from the padded planes. fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> { - if mi_cols == 0 - || mi_rows == 0 - || !mi_cols.is_multiple_of(BLOCK_MI) - || !mi_rows.is_multiple_of(BLOCK_MI) - { + if mi_cols == 0 || mi_rows == 0 { return Err(Error::unsupported( "AV1 tile", - "a coefficient key frame is written only for frames that are a \ - whole number of 32x32 blocks", + "a coefficient key frame needs a nonzero mode-info grid", )); } Ok(()) @@ -685,7 +691,7 @@ pub fn sb_coeff_key_frame_tile( superblocks: &[Superblock], ) -> Result> { check_blocks(mi_cols, mi_rows)?; - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); + let (cols, rows) = block_grid(mi_cols, mi_rows); let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); if superblocks.len() != (sb_cols * sb_rows) as usize { return Err(Error::unsupported( @@ -988,7 +994,7 @@ pub fn split_coeff_key_frame_tile( blocks: &[BlockCoeffs], ) -> Result> { check_blocks(mi_cols, mi_rows)?; - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); + let (cols, rows) = block_grid(mi_cols, mi_rows); if blocks.len() != (cols * rows) as usize { return Err(Error::unsupported( "AV1 tile",