diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index eef6469c..048abfc0 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1264,9 +1264,20 @@ pub(crate) fn encode_key_frame_inner( let mut split = Vec::with_capacity(4); let mut cost_split = search.lambda * (partition_bits(BLOCK, true) + 4.0 * partition_bits(SUB, false)); - let mut split_modes = [DC_PRED; 4]; - for (sub, sub_mode) in split_modes.iter_mut().enumerate() { + let mut split_modes = Vec::with_capacity(4); + for sub in 0..4 { let (sc, sr) = (c0 + sub % 2, r0 + sub / 2); + // Same filter as the writer's `sub_positions` (spec + // `decode_partition`'s `r >= MiRows || c >= MiCols` early + // return): a sub-block whose own origin sits past the + // true frame is never coded, so it must not be searched + // or pushed onto `split` either, or the writer's block + // count will not match what it is prepared to name. + if (sr as u32) * crate::tile::SUB_MI >= header.mi_rows + || (sc as u32) * crate::tile::SUB_MI >= header.mi_cols + { + continue; + } let (block, cost) = code_square( &mut luma, &mut chroma, @@ -1276,7 +1287,7 @@ pub(crate) fn encode_key_frame_inner( &mode_bits(above_mode[sc], left_mode[sr]), ); cost_split += cost; - *sub_mode = block.mode; + split_modes.push(block.mode); above_mode[sc] = block.mode; left_mode[sr] = block.mode; split.push(block); @@ -1833,13 +1844,14 @@ pub(crate) fn encode_inter_frame( // overwrites slot 0, so the frame just coded is always what the next one // predicts against. const LAST_SLOT: u8 = 0; - let (seq, mut header) = inter_frame_headers( - picture.width, - picture.height, - base_q_idx, - order_hint, - LAST_SLOT, - )?; + // `render`, not `picture.width`/`picture.height`: `picture` here is + // already padded to a whole number of superblocks (`encode_sequence`'s + // `padded_to(SUPERBLOCK)`), and the header's `mi_cols`/`mi_rows` (and so + // every true-edge clamp downstream) must come from the frame's real, + // unpadded size, same as `key_frame_headers_colour` takes `render` and + // not the padded picture in `encode_key_frame_inner`. + let (seq, mut header) = + inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?; header.render_width = render.0 as u32; header.render_height = render.1 as u32; @@ -1880,8 +1892,14 @@ pub(crate) fn encode_inter_frame( }; let mode_bits_table = inter_mode_bits(); - let (cols, rows) = (picture.width / BLOCK, picture.height / BLOCK); - let (sb_cols, sb_rows) = (cols / 2, rows / 2); + // The true grid ([`crate::tile::block_grid`]'s ceiling), not the padded + // coding surface's: `blocks` carries one entry per 32x32 block the tile + // writer will actually visit, same count it checks `blocks.len()` + // against, which is fewer than the padded surface's own block count + // whenever the true size is not a 64x64 multiple. + let (cols, rows) = crate::tile::block_grid(header.mi_cols, header.mi_rows); + let (cols, rows) = (cols as usize, rows as usize); + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); let (mi_cols, mi_rows) = (header.mi_cols as usize, header.mi_rows as usize); let mut grid = MiGrid::new(mi_cols, mi_rows); let mut blocks = vec![BlockCoeffs::default(); cols * rows]; @@ -1890,6 +1908,11 @@ pub(crate) fn encode_inter_frame( for sb_c in 0..sb_cols { for quadrant in 0..4 { let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); + // Same filter as the tile writer's: a quadrant whose own mi + // origin is not inside the true frame is never coded. + if r32 >= rows || c32 >= cols { + continue; + } let (x, y) = (c32 * BLOCK, r32 * BLOCK); let (mi_row, mi_col) = (r32 * 8, c32 * 8); let stack = @@ -2140,7 +2163,7 @@ mod tests { eprintln!("SKIP ffmpeg_decodes_exactly_what_the_encoder_reconstructed: no ffmpeg"); return; } - for &(width, height) in &[(64usize, 64usize), (96, 64), (160, 96)] { + for &(width, height) in &[(64usize, 64usize), (96, 64), (160, 96), (32, 48)] { let picture = test_card(width, height); let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); let decoded = ffmpeg_decode(&encoded.stream, width, height); @@ -3036,7 +3059,11 @@ mod tests { msg.contains("true frame") }; assert!(refused(40, 32)); - assert!(refused(32, 48)); + // 32x48 was refused here through round 9: its split quadrant only + // ever has two legal 16x16 subs (the other two sit past the true + // 48-row edge, block-aligned, no 8x8 split needed), but the writer + // demanded four. See `ffmpeg_decodes_exactly_what_the_encoder_ + // reconstructed`, which now round-trips it pixel-exact instead. let mut short = Picture::grey(64, 64); short.u.truncate(10); assert!(encode_key_frame(&short, 100, 0.5).is_err()); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 1133a43a..efec65a0 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1859,12 +1859,16 @@ pub fn sb_coeff_inter_frame_tile( base_q_idx: u8, blocks: &[BlockCoeffs], ) -> Result> { - check_superblocks(mi_cols, mi_rows)?; - let (cols, rows) = (mi_cols / BLOCK_MI, mi_rows / BLOCK_MI); + check_blocks(mi_cols, mi_rows)?; + // `block_grid`'s ceiling, not a plain division: a true frame size that is + // not a whole number of 32x32 blocks (or of 64x64 superblocks) still has + // one more block/superblock whose own origin is inside the true frame, + // same as `sb_coeff_key_frame_tile`. + let (cols, rows) = block_grid(mi_cols, mi_rows); if blocks.len() != (cols * rows) as usize { return Err(Error::unsupported( "AV1 tile", - "an inter frame needs one entry per 32x32 block", + "an inter frame needs one entry per 32x32 block inside the true frame", )); } if let Some(bad) = blocks @@ -1887,7 +1891,7 @@ pub fn sb_coeff_inter_frame_tile( /// writer's single-reference chain ever names. const LAST_FRAME: i8 = 1; - let (sb_cols, sb_rows) = (mi_cols / SB_MI, mi_rows / SB_MI); + let (sb_cols, sb_rows) = (cols.div_ceil(2), rows.div_ceil(2)); let mut neighbours = Neighbours::new( cols as usize * 2, rows as usize * 2, @@ -1916,14 +1920,57 @@ pub fn sb_coeff_inter_frame_tile( for sb_c in 0..sb_cols { let sb_at = (sb_r as usize * 4, sb_c as usize * 4); let sb_ctx = neighbours.partition_ctx(sb_at, SB); - enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]); + // spec `decode_partition`'s hasRows/hasCols (5.11.4): a superblock + // whose bottom or right half falls outside the true frame cannot + // be left whole, but this writer only ever splits a superblock + // into its four 32x32 quadrants (never `PARTITION_NONE` at 64x64), + // so the only question is which of the three partition symbols + // that split takes — same three-way signaling as + // `sb_coeff_key_frame_tile`'s superblock level. + let (has_cols, has_rows) = ( + sb_c * SB_MI + SB_MI / 2 < mi_cols, + sb_r * SB_MI + SB_MI / 2 < mi_rows, + ); + match (has_cols, has_rows) { + (true, true) => enc.symbol(PARTITION_SPLIT, &mut cdfs.partition_w64[sb_ctx]), + (true, false) => { + enc.symbol_fixed(1, &gather(&cdfs.partition_w64[sb_ctx], VERT_ALIKE)); + } + (false, true) => { + enc.symbol_fixed(1, &gather(&cdfs.partition_w64[sb_ctx], HORZ_ALIKE)); + } + (false, false) => {} + } for quadrant in 0..4 { let (r32, c32) = (sb_r * 2 + quadrant / 2, sb_c * 2 + quadrant % 2); + // Only a quadrant whose own mi origin is inside the true + // frame is coded at all (spec `decode_partition`'s + // `r >= MiRows || c >= MiCols` early return), same filter as + // `sb_coeff_key_frame_tile`'s `quadrant_positions`. + if r32 >= rows || c32 >= cols { + continue; + } let block = &blocks[(r32 * cols + c32) as usize]; let at = (r32 as usize * 2, c32 as usize * 2); let (r, c) = at; let ctx32 = neighbours.partition_ctx(at, BLOCK); + // This writer has no rectangular (HORZ/VERT) or further-split + // representation for a 32x32 block, so one that itself + // straddles the true edge is refused rather than miscoded -- + // mirroring `sb_coeff_key_frame_tile`'s "cannot be left whole" + // refusal, just with no split arm to fall back to here. + let (has_cols32, has_rows32) = ( + has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols), + has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows), + ); + if !has_cols32 || !has_rows32 { + return Err(Error::unsupported( + "AV1 tile", + "a 32x32 inter-frame block that straddles the true frame edge \ + needs an 8x8 split this writer does not code", + )); + } enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[ctx32]); let has_above = r32 > 0;