forge
Explore
Log in

Don't you dare go Hollow

wip(av1): write the true display size as frame_width/height, not the padded one

Browse files at this commit

6b1cdccc4dee5d3c2e9b390e80871c38124aac44Ahmet Kaan GümüşParent: c47074a
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.rsmodified+34 -28
@@ -379,7 +379,11 @@ pub(crate) fn key_frame_headers_colour(
379379 enable_ref_frame_mvs: false,
380380 film_grain_params_present: false,
381381 };
382- let (mi_cols, mi_rows) = (w / 4, h / 4);
382+ // spec `compute_image_size` (5.9.15): MiCols/MiRows come from the frame's
383+ // own (true, unpadded) width and height, not from any block-grid
384+ // alignment — a decoder derives these straight from `frame_width`/
385+ // `frame_height` below, so this must match it exactly.
386+ let (mi_cols, mi_rows) = (2 * ((w + 7) >> 3), 2 * ((h + 7) >> 3));
383387 let header = FrameHeader {
384388 frame_type: FrameType::Key,
385389 frame_is_intra: true,
@@ -1063,8 +1067,14 @@ pub(crate) fn encode_key_frame_inner(
10631067 format!("intra mode {bad} is not one the encoder predicts"),
10641068 ));
10651069 }
1066- let (seq, mut header) =
1067- key_frame_headers_colour(picture.width, picture.height, base_q_idx, color_config)?;
1070+ // The header carries the frame's true (unpadded) size as `frame_width`/
1071+ // `frame_height` -- what a decoder actually crops to -- with `mi_cols`/
1072+ // `mi_rows` derived from that same true size (spec `compute_image_size`).
1073+ // `render_width`/`render_height` come out equal to it too, so
1074+ // `render_and_frame_size_different` is false and no render_size bits are
1075+ // written, mirroring libaom/rav1e: the padded `picture` below is only the
1076+ // internal coding surface, never what the header names.
1077+ let (seq, mut header) = key_frame_headers_colour(render.0, render.1, base_q_idx, color_config)?;
10681078 header.render_width = render.0 as u32;
10691079 header.render_height = render.1 as u32;
10701080
@@ -1099,7 +1109,14 @@ pub(crate) fn encode_key_frame_inner(
10991109 modes,
11001110 };
11011111
1102- let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK);
1112+ // The block grid this frame is coded over: [`crate::tile::block_grid`]'s
1113+ // ceiling of the header's own (true-size-derived) `mi_cols`/`mi_rows`,
1114+ // which may be fewer columns/rows than the padded picture has room for
1115+ // -- a block whose origin sits past the true bound is not coded at all,
1116+ // matching what a decoder derives from `frame_width`/`frame_height` and
1117+ // so never visits either.
1118+ let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows);
1119+ let (cols, rows) = (cols as usize, rows as usize);
11031120 // The luma mode of the block above and of the block to the left, which is
11041121 // what picks the CDF the next block's mode is coded against -- the same
11051122 // bookkeeping the tile writer keeps, so that the search is costing the
@@ -2186,23 +2203,18 @@ mod tests {
21862203 (width, height),
21872204 "{width}x{height}: reconstruction is cropped to the picture's own size"
21882205 );
2189- let (padded_width, padded_height) = (
2190- width.next_multiple_of(BLOCK),
2191- height.next_multiple_of(BLOCK),
2192- );
21932206 assert_eq!(
21942207 ffprobe_size(&encoded.stream),
2195- (padded_width as u32, padded_height as u32),
2196- "{width}x{height}: ffprobe reports the coded (padded) size"
2208+ (width as u32, height as u32),
2209+ "{width}x{height}: ffprobe reports the true (display) size, not the padded one"
21972210 );
2198- let decoded = ffmpeg_decode(&encoded.stream, padded_width, padded_height);
2199- let cropped = crop_picture(&decoded, width, height);
2211+ let decoded = ffmpeg_decode(&encoded.stream, width, height);
22002212 assert_eq!(
2201- cropped.y, encoded.reconstruction.y,
2213+ decoded.y, encoded.reconstruction.y,
22022214 "{width}x{height}: luma"
22032215 );
2204- assert_eq!(cropped.u, encoded.reconstruction.u, "{width}x{height}: U");
2205- assert_eq!(cropped.v, encoded.reconstruction.v, "{width}x{height}: V");
2216+ assert_eq!(decoded.u, encoded.reconstruction.u, "{width}x{height}: U");
2217+ assert_eq!(decoded.v, encoded.reconstruction.v, "{width}x{height}: V");
22062218 }
22072219 }
22082220
@@ -2362,24 +2374,18 @@ mod tests {
23622374 );
23632375 }
23642376 // See `a_frame_round_trips_at_its_own_size`: `ffprobe` reports the
2365- // coded (superblock-padded) size a sequence's frames share, not the
2366- // render size.
2367- let (padded_width, padded_height) = (
2368- width.next_multiple_of(SUPERBLOCK),
2369- height.next_multiple_of(SUPERBLOCK),
2370- );
2377+ // true (display) size a sequence's frames share, not the padded one.
23712378 assert_eq!(
23722379 ffprobe_size(&encoded.stream),
2373- (padded_width as u32, padded_height as u32),
2374- "sequence: ffprobe reports the coded (padded) size"
2380+ (width as u32, height as u32),
2381+ "sequence: ffprobe reports the true (display) size"
23752382 );
2376- let decoded = ffmpeg_decode_sequence(&encoded.stream, padded_width, padded_height, 3);
2383+ let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, 3);
23772384 assert_eq!(decoded.len(), 3, "decoded frame count");
23782385 for (i, (frame, decoded)) in encoded.frames.iter().zip(&decoded).enumerate() {
2379- let cropped = crop_picture(decoded, width, height);
2380- assert_eq!(cropped.y, frame.reconstruction.y, "frame {i}: luma");
2381- assert_eq!(cropped.u, frame.reconstruction.u, "frame {i}: U");
2382- assert_eq!(cropped.v, frame.reconstruction.v, "frame {i}: V");
2386+ assert_eq!(decoded.y, frame.reconstruction.y, "frame {i}: luma");
2387+ assert_eq!(decoded.u, frame.reconstruction.u, "frame {i}: U");
2388+ assert_eq!(decoded.v, frame.reconstruction.v, "frame {i}: V");
23832389 }
23842390 }
23852391
crates/ec-av1/src/encoder.rsmodified+2 -2
@@ -427,8 +427,8 @@ mod tests {
427427 assert_eq!(*key, expect_key[t], "picture {t}: ffprobe key_frame flag");
428428 assert_eq!(
429429 (*w, *h),
430- (coded_w as u32, coded_h as u32),
431- "picture {t}: coded size"
430+ (width as u32, height as u32),
431+ "picture {t}: ffprobe reports the true (display) size, not the padded coded one"
432432 );
433433 }
434434 }
crates/ec-av1/src/tile.rsmodified+20 -14
@@ -35,6 +35,16 @@ const HORZ_ALIKE: [usize; 6] = [1, PARTITION_SPLIT, 4, 5, 6, 8];
3535
3636 /// Mode-info units across a 32x32 block.
3737 const BLOCK_MI: u32 = SB_MI / 2;
38+
39+/// The number of 32x32 block columns/rows a frame whose *true* (unpadded)
40+/// size gives `mi_cols`/`mi_rows` is coded over: every block whose mi origin
41+/// (`col * BLOCK_MI`) is inside the true bound is coded, so this is a
42+/// ceiling, not the exact division a block-aligned frame would give. Shared
43+/// by the tile writer's own iteration and by [`crate::encode`]'s block/
44+/// superblock generation, which must agree with it exactly.
45+pub(crate) fn block_grid(mi_cols: u32, mi_rows: u32) -> (u32, u32) {
46+ (mi_cols.div_ceil(BLOCK_MI), mi_rows.div_ceil(BLOCK_MI))
47+}
3848 /// `DC_PRED` (spec 6.10.2), as both the luma and the chroma mode.
3949 const DC_PRED: usize = 0;
4050
@@ -479,22 +489,18 @@ struct Neighbour {
479489 dc: Option<bool>,
480490 }
481491
482-/// Rejects a frame the 32x32 block grid cannot tile.
492+/// Rejects a frame with no mode-info grid at all.
483493 ///
484-/// The frame need not be a whole number of superblocks — a superblock at the
485-/// right-hand or bottom edge may be half outside it — but every 32x32 block
486-/// that is coded has to be wholly inside, because a block that hangs over the
487-/// edge has to be coded as a rectangle this writer has no transform for.
494+/// `mi_cols`/`mi_rows` are the frame's *true* (unpadded) size in 4x4 units
495+/// (spec `compute_image_size`), not necessarily a multiple of the 32x32 block
496+/// grid: a block whose origin sits at or past this bound is not coded (spec's
497+/// `decode_partition` never visits it), and one that straddles the bound is
498+/// coded whole, its samples coming from the padded planes.
488499 fn check_blocks(mi_cols: u32, mi_rows: u32) -> Result<()> {
489- if mi_cols == 0
490- || mi_rows == 0
491- || !mi_cols.is_multiple_of(BLOCK_MI)
492- || !mi_rows.is_multiple_of(BLOCK_MI)
493- {
500+ if mi_cols == 0 || mi_rows == 0 {
494501 return Err(Error::unsupported(
495502 "AV1 tile",
496- "a coefficient key frame is written only for frames that are a \
497- whole number of 32x32 blocks",
503+ "a coefficient key frame needs a nonzero mode-info grid",
498504 ));
499505 }
500506 Ok(())
@@ -685,7 +691,7 @@ pub fn sb_coeff_key_frame_tile(
685691 superblocks: &[Superblock],
686692 ) -> Result<Vec<u8>> {
687693 check_blocks(mi_cols, mi_rows)?;
688- let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI);
694+ let (cols, rows) = block_grid(mi_cols, mi_rows);
689695 let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2));
690696 if superblocks.len() != (sb_cols * sb_rows) as usize {
691697 return Err(Error::unsupported(
@@ -988,7 +994,7 @@ pub fn split_coeff_key_frame_tile(
988994 blocks: &[BlockCoeffs],
989995 ) -> Result<Vec<u8>> {
990996 check_blocks(mi_cols, mi_rows)?;
991- let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI);
997+ let (cols, rows) = block_grid(mi_cols, mi_rows);
992998 if blocks.len() != (cols * rows) as usize {
993999 return Err(Error::unsupported(
9941000 "AV1 tile",