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",