diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 5cd72b5e..c3e2fed2 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4981,11 +4981,13 @@ pub(crate) fn encode_key_frame_inner( ); let whole_legal = has_cols32 && has_rows32; // A 16x16 sub-block's own hasCols/hasRows (spec - // `decode_partition`, recomputed at this leaf's own half): one - // axis false is a straddling leaf this writer codes as two - // 8x8 leaves (`crate::tile::write_leaf8`, lane-av1-rect r7); - // both axes false needs a rectangular transform this writer - // has no size for at all, whether whole or split. + // `decode_partition`, recomputed at this leaf's own half): + // either axis false is a straddling leaf this writer codes as + // the 8x8 leaves that are inside it + // (`crate::tile::write_leaf8`, lane-av1-rect r7). Both axes + // false is the same split with the partition symbol inferred + // rather than coded -- no rectangular transform is needed + // (lane-av1rect). let sub_half = |sr: usize, sc: usize| { ( crate::tile::has_half( @@ -5000,27 +5002,6 @@ pub(crate) fn encode_key_frame_inner( ), ) }; - let both_axes_cut = (0..4) - .map(|i| (r0 + i / 2, c0 + i % 2)) - .filter(|&(sr, sc)| { - (sr as u32) * crate::tile::SUB_MI < header.mi_rows - && (sc as u32) * crate::tile::SUB_MI < header.mi_cols - }) - .any(|(sr, sc)| { - let (has_cols16, has_rows16) = sub_half(sr, sc); - !has_cols16 && !has_rows16 - }); - if !whole_legal && both_axes_cut { - return Err(Error::unsupported( - "AV1 encode", - format!( - "the 32x32 block at ({col},{row}) straddles the true frame edge \ - in a way that needs a rectangular (HORZ/VERT) transform this \ - encoder does not code yet" - ), - )); - } - // What the whole 32x32 costs, including the partition symbol // that says it is not split. let (whole, mut cost_whole) = code_square( @@ -7238,16 +7219,6 @@ pub(crate) fn encode_inter_frame( header.mi_rows, ), ); - if !has_cols16 && !has_rows16 { - return Err(Error::unsupported( - "AV1 encode", - format!( - "the 16x16 block at ({sc},{sr}) straddles the true \ - frame edge in a way that needs a rectangular \ - (HORZ/VERT) transform this encoder does not code yet" - ), - )); - } if has_cols16 && has_rows16 { let (x, y) = (sc * SUB, sr * SUB); let (mi_row, mi_col) = (sr * 4, sc * 4); @@ -9494,24 +9465,30 @@ mod tests { } /// The sizes the encoder refuses, refused for a reason rather than by - /// panicking somewhere below. Most sizes off the 32x32 block grid encode - /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame - /// edge lands past the halfway point of whichever block it falls in, so + /// panicking somewhere below. Sizes off the 32x32 block grid encode fine + /// (see `a_frame_round_trips_at_its_own_size`) -- the true frame edge + /// lands past the halfway point of whichever block it falls in, so /// `PARTITION_NONE` or a single gathered split flag still says everything - /// the spec needs. lane-av1-rect r7 wires the 8x8-leaf path live, so a - /// 16x16 leaf straddling on exactly one axis (e.g. 40x32, cols mod 32 == - /// 8) now encodes too (see `a_frame_round_trips_at_its_own_size`'s - /// sibling below). Only a leaf whose true edge cuts *both* axes (e.g. - /// 40x40, both dims mod 32 == 8) needs the rectangular transform neither - /// writer has, so that class is still refused, by name rather than by - /// corrupting the stream. + /// the spec needs. lane-av1-rect r7 wired the 8x8-leaf path for a 16x16 + /// leaf straddling on exactly one axis (e.g. 40x32), and lane-av1rect the + /// both-axes cut (e.g. 40x40, both dims mod 32 == 8), which spec 5.11.4 + /// codes with NO partition symbol at all -- `PARTITION_SPLIT` inferred, + /// down to the in-frame 8x8s, no rectangular transform anywhere (the old + /// refusal here claimed otherwise and was wrong; see + /// `crate::stream::tests::a_sweep_of_doubly_straddling_sizes_round_trips_ + /// through_ffmpeg`). No size refusal is left: an 8x8 leaf can never + /// straddle either, because `MiCols`/`MiRows` are always EVEN (spec + /// 5.9.5 `compute_image_size`: `2 * ((frame_width + 7) >> 3)`), so the + /// true edge always lands on an 8-luma-sample boundary in mi terms and a + /// 4x4 leaf -- which neither writer has -- is never asked for. What is + /// still refused is a malformed picture. #[test] - fn a_picture_off_the_block_grid_is_refused() { + fn a_picture_off_the_block_grid_encodes_and_a_malformed_one_is_refused() { let fctx = &crate::decode::FrameCtx::new(); - let msg = encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx) - .expect_err("40x40 straddles both axes of a 16x16 leaf, needs a rectangular transform") - .to_string(); - assert!(msg.contains("rectangular"), "refusal: {msg}"); + encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx) + .expect("40x40 cuts a 16x16 leaf on both axes, which is an inferred split"); + encode_key_frame_with_ctx(&Picture::grey(36, 40), 100, 0.5, fctx) + .expect("36 luma columns still round up to an even MiCols"); let mut short = Picture::grey(64, 64); short.u.truncate(10); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 2ef0a4bb..4f505cf9 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3348,6 +3348,73 @@ pub(crate) mod tests { } } + /// lane-av1rect: the encoder used to REFUSE every size where a 32x32 + /// block straddles BOTH true frame edges at once ("straddles the true + /// frame edge in a way that needs a rectangular (HORZ/VERT) transform + /// this encoder does not code yet"). That refusal was a misreading of + /// spec 5.11.4: a block whose `hasRows` and `hasCols` are BOTH false + /// reads no partition symbol at all and is `PARTITION_SPLIT` by + /// inference, down to the 8x8 leaves whose own mi origin is inside the + /// frame -- exactly the leaves this writer already codes for the + /// one-axis case, and no rectangular transform anywhere. The sweep walks + /// the corners around the 32-multiples (each width has a 16x16 column + /// whose right half is outside, each height a row whose bottom half is, + /// so the bottom-right 16x16 of every size here is cut on both axes) and + /// three-way compares: our decoder, the encoder's own reconstruction and + /// ffmpeg, over a 4-frame GOP so the inter writer's copy of the same + /// signaling is covered too. `edge_both_cut_hits` proves the decoder + /// really took the inferred-split path rather than the gate passing on + /// geometry that never fires it. + #[test] + fn a_sweep_of_doubly_straddling_sizes_round_trips_through_ffmpeg() { + let fctx = &crate::decode::FrameCtx::new(); + if !have_ffmpeg() { + eprintln!("SKIP a_sweep_of_doubly_straddling_sizes_round_trips_through_ffmpeg: no ffmpeg"); + return; + } + const FRAMES: usize = 4; + let mut both_cut = 0usize; + for &width in &[200usize, 216, 232, 248] { + for &height in &[104usize, 120, 136, 152] { + let pictures: Vec<_> = (0..FRAMES as i64) + .map(|i| panned_test_card(width, height, i * 3)) + .collect(); + let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx) + .unwrap_or_else(|e| panic!("{width}x{height}: encoder refused: {e}")); + // Reset per attempt, never a delta across sizes (class + // counter-from-refused-stream). + crate::decode::reset_edge_both_cut_hits(); + let decoded = decode_stream(&encoded.stream) + .unwrap_or_else(|e| panic!("{width}x{height}: our decoder refused: {e}")); + both_cut += crate::decode::edge_both_cut_hits().iter().sum::(); + let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, FRAMES); + assert_eq!( + ffmpeg_frames.len(), + FRAMES, + "{width}x{height}: ffmpeg decoded {} of {FRAMES} frames", + ffmpeg_frames.len() + ); + assert_eq!(decoded.len(), FRAMES, "{width}x{height}: frame count"); + for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{width}x{height} frame {i} luma vs ffmpeg"); + assert_eq!(got.u, want.u, "{width}x{height} frame {i} U vs ffmpeg"); + assert_eq!(got.v, want.v, "{width}x{height} frame {i} V vs ffmpeg"); + } + for (i, (frame, want)) in encoded.frames.iter().zip(&ffmpeg_frames).enumerate() { + let rec = &frame.reconstruction; + assert_eq!(rec.y, want.y, "{width}x{height} frame {i} luma: recon vs ffmpeg"); + assert_eq!(rec.u, want.u, "{width}x{height} frame {i} U: recon vs ffmpeg"); + assert_eq!(rec.v, want.v, "{width}x{height} frame {i} V: recon vs ffmpeg"); + } + } + } + assert!( + both_cut > 0, + "no partition node in the whole sweep was coded as the both-axes-cut \ + inferred split -- the gate is blind to the feature it pins" + ); + } + /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME` /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index f22967bc..910c671b 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2706,30 +2706,26 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( continue; } // The true edge falls inside this 16x16 - // leaf itself (mod-32==8 target sizes): - // one axis only, per r6's charter -- an - // 8x8 leaf never itself straddles, so the - // block splits cleanly along whichever - // axis is short and only the in-frame - // 8x8s are coded, each its own leaf. - if !has_cols16 && !has_rows16 { - return Err(Error::unsupported( - "AV1 tile", - "a 16x16 block whose true edge cuts through both \ - axes needs a rectangular transform this writer \ - does not code yet", - )); - } - if has_cols16 { - enc.symbol_fixed( + // leaf itself: the block splits into the + // 8x8s that are inside, each its own + // leaf. Same three-way spec signaling as + // every level above -- a single gathered + // bit when one half is outside, and + // nothing at all when BOTH are, where + // `decode_partition` infers the split + // (lane-av1rect: no rectangular + // transform is needed for this, the + // earlier refusal here misread 5.11.4). + match (has_cols16, has_rows16) { + (true, false) => enc.symbol_fixed( 1, &gather(&cdfs.partition_w16[ctx], VERT_ALIKE), - ); - } else { - enc.symbol_fixed( + ), + (false, true) => enc.symbol_fixed( 1, &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE), - ); + ), + _ => {} } let leaves = block.eight.as_ref().ok_or_else(|| { Error::unsupported( @@ -5359,23 +5355,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( } for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) { // A 16x16 leaf whose own half straddles the true - // frame edge on both axes needs a rectangular - // transform this writer does not code yet; one - // axis only is two (or, at a true corner, one) - // 8x8 leaves (lane-av1inter8), same split the key - // frame search takes at this geometry. + // frame edge splits into the 8x8 leaves that are + // inside it (lane-av1inter8), same split the key + // frame search takes at this geometry -- on both + // axes too, where `decode_partition` infers the + // split and reads no symbol at all + // (lane-av1rect). let (has_cols16, has_rows16) = ( has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), ); - if !has_cols16 && !has_rows16 { - return Err(Error::unsupported( - "AV1 tile", - "a 16x16 inter-frame block whose true edge cuts through \ - both axes needs a rectangular transform this writer \ - does not code yet", - )); - } let at16 = (sr, sc); if has_cols16 && has_rows16 { let ctx16 = neighbours.partition_ctx(at16, SUB); @@ -5442,16 +5431,17 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( )?; } else { let ctx16 = neighbours.partition_ctx(at16, SUB); - if has_cols16 { - enc.symbol_fixed( + match (has_cols16, has_rows16) { + (true, false) => enc.symbol_fixed( 1, &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE), - ); - } else { - enc.symbol_fixed( + ), + (false, true) => enc.symbol_fixed( 1, &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE), - ); + ), + // Both halves outside: SPLIT is inferred. + _ => {} } let leaves = leaf.eight.as_ref().ok_or_else(|| { Error::unsupported(