forge
Explore
Log in

Don't you dare go Hollow

Code the both-axes frame-edge cut as the spec's inferred split

Browse files at this commit

0e8b39f1daf5e74ba24eca80db067b3057c226baAhmet Kaan GümüşParent: 16ecf06
  • feat
    • code the both-axes frame-edge cut as the spec's inferred split
Show full message
feat(av1): code the both-axes frame-edge cut as the spec's inferred split

A 32x32 or 16x16 block whose true frame edge cuts BOTH axes reads no
partition symbol at all (spec 5.11.4: hasRows and hasCols both false =>
PARTITION_SPLIT inferred), down to the 8x8 leaves whose own mi origin is
inside the frame -- the same leaves the one-axis case already coded. The
encoder refused that geometry by name, claiming it needed a rectangular
HORZ/VERT transform; it needs none, and the refusal was a misreading of
the rule. Both writers now emit the inferred split, and the searches drop
their matching refusals.
crates/ec-av1/src/encode.rsmodified+28 -51
@@ -4981,11 +4981,13 @@ pub(crate) fn encode_key_frame_inner(
4981 );4981 );
4982 let whole_legal = has_cols32 && has_rows32;4982 let whole_legal = has_cols32 && has_rows32;
4983 // A 16x16 sub-block's own hasCols/hasRows (spec4983 // A 16x16 sub-block's own hasCols/hasRows (spec
4984- // `decode_partition`, recomputed at this leaf's own half): one4984+ // `decode_partition`, recomputed at this leaf's own half):
4985- // axis false is a straddling leaf this writer codes as two4985+ // either axis false is a straddling leaf this writer codes as
4986- // 8x8 leaves (`crate::tile::write_leaf8`, lane-av1-rect r7);4986+ // the 8x8 leaves that are inside it
4987- // both axes false needs a rectangular transform this writer4987+ // (`crate::tile::write_leaf8`, lane-av1-rect r7). Both axes
4988- // has no size for at all, whether whole or split.4988+ // false is the same split with the partition symbol inferred
4989+ // rather than coded -- no rectangular transform is needed
4990+ // (lane-av1rect).
4989 let sub_half = |sr: usize, sc: usize| {4991 let sub_half = |sr: usize, sc: usize| {
4990 (4992 (
4991 crate::tile::has_half(4993 crate::tile::has_half(
@@ -5000,27 +5002,6 @@ pub(crate) fn encode_key_frame_inner(
5000 ),5002 ),
5001 )5003 )
5002 };5004 };
5003- let both_axes_cut = (0..4)
5004- .map(|i| (r0 + i / 2, c0 + i % 2))
5005- .filter(|&(sr, sc)| {
5006- (sr as u32) * crate::tile::SUB_MI < header.mi_rows
5007- && (sc as u32) * crate::tile::SUB_MI < header.mi_cols
5008- })
5009- .any(|(sr, sc)| {
5010- let (has_cols16, has_rows16) = sub_half(sr, sc);
5011- !has_cols16 && !has_rows16
5012- });
5013- if !whole_legal && both_axes_cut {
5014- return Err(Error::unsupported(
5015- "AV1 encode",
5016- format!(
5017- "the 32x32 block at ({col},{row}) straddles the true frame edge \
5018- in a way that needs a rectangular (HORZ/VERT) transform this \
5019- encoder does not code yet"
5020- ),
5021- ));
5022- }
5023-
5024 // What the whole 32x32 costs, including the partition symbol5005 // What the whole 32x32 costs, including the partition symbol
5025 // that says it is not split.5006 // that says it is not split.
5026 let (whole, mut cost_whole) = code_square(5007 let (whole, mut cost_whole) = code_square(
@@ -7238,16 +7219,6 @@ pub(crate) fn encode_inter_frame(
7238 header.mi_rows,7219 header.mi_rows,
7239 ),7220 ),
7240 );7221 );
7241- if !has_cols16 && !has_rows16 {
7242- return Err(Error::unsupported(
7243- "AV1 encode",
7244- format!(
7245- "the 16x16 block at ({sc},{sr}) straddles the true \
7246- frame edge in a way that needs a rectangular \
7247- (HORZ/VERT) transform this encoder does not code yet"
7248- ),
7249- ));
7250- }
7251 if has_cols16 && has_rows16 {7222 if has_cols16 && has_rows16 {
7252 let (x, y) = (sc * SUB, sr * SUB);7223 let (x, y) = (sc * SUB, sr * SUB);
7253 let (mi_row, mi_col) = (sr * 4, sc * 4);7224 let (mi_row, mi_col) = (sr * 4, sc * 4);
@@ -9494,24 +9465,30 @@ mod tests {
9494 }9465 }
9495 9466
9496 /// The sizes the encoder refuses, refused for a reason rather than by9467 /// The sizes the encoder refuses, refused for a reason rather than by
9497- /// panicking somewhere below. Most sizes off the 32x32 block grid encode9468+ /// panicking somewhere below. Sizes off the 32x32 block grid encode fine
9498- /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame9469+ /// (see `a_frame_round_trips_at_its_own_size`) -- the true frame edge
9499- /// edge lands past the halfway point of whichever block it falls in, so9470+ /// lands past the halfway point of whichever block it falls in, so
9500 /// `PARTITION_NONE` or a single gathered split flag still says everything9471 /// `PARTITION_NONE` or a single gathered split flag still says everything
9501- /// the spec needs. lane-av1-rect r7 wires the 8x8-leaf path live, so a9472+ /// the spec needs. lane-av1-rect r7 wired the 8x8-leaf path for a 16x16
9502- /// 16x16 leaf straddling on exactly one axis (e.g. 40x32, cols mod 32 ==9473+ /// leaf straddling on exactly one axis (e.g. 40x32), and lane-av1rect the
9503- /// 8) now encodes too (see `a_frame_round_trips_at_its_own_size`'s9474+ /// both-axes cut (e.g. 40x40, both dims mod 32 == 8), which spec 5.11.4
9504- /// sibling below). Only a leaf whose true edge cuts *both* axes (e.g.9475+ /// codes with NO partition symbol at all -- `PARTITION_SPLIT` inferred,
9505- /// 40x40, both dims mod 32 == 8) needs the rectangular transform neither9476+ /// down to the in-frame 8x8s, no rectangular transform anywhere (the old
9506- /// writer has, so that class is still refused, by name rather than by9477+ /// refusal here claimed otherwise and was wrong; see
9507- /// corrupting the stream.9478+ /// `crate::stream::tests::a_sweep_of_doubly_straddling_sizes_round_trips_
9479+ /// through_ffmpeg`). No size refusal is left: an 8x8 leaf can never
9480+ /// straddle either, because `MiCols`/`MiRows` are always EVEN (spec
9481+ /// 5.9.5 `compute_image_size`: `2 * ((frame_width + 7) >> 3)`), so the
9482+ /// true edge always lands on an 8-luma-sample boundary in mi terms and a
9483+ /// 4x4 leaf -- which neither writer has -- is never asked for. What is
9484+ /// still refused is a malformed picture.
9508 #[test]9485 #[test]
9509- fn a_picture_off_the_block_grid_is_refused() {9486+ fn a_picture_off_the_block_grid_encodes_and_a_malformed_one_is_refused() {
9510 let fctx = &crate::decode::FrameCtx::new();9487 let fctx = &crate::decode::FrameCtx::new();
9511- let msg = encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx)9488+ encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx)
9512- .expect_err("40x40 straddles both axes of a 16x16 leaf, needs a rectangular transform")9489+ .expect("40x40 cuts a 16x16 leaf on both axes, which is an inferred split");
9513- .to_string();9490+ encode_key_frame_with_ctx(&Picture::grey(36, 40), 100, 0.5, fctx)
9514- assert!(msg.contains("rectangular"), "refusal: {msg}");9491+ .expect("36 luma columns still round up to an even MiCols");
9515 9492
9516 let mut short = Picture::grey(64, 64);9493 let mut short = Picture::grey(64, 64);
9517 short.u.truncate(10);9494 short.u.truncate(10);
crates/ec-av1/src/stream.rsmodified+67 -0
@@ -3348,6 +3348,73 @@ pub(crate) mod tests {
3348 }3348 }
3349 }3349 }
3350 3350
3351+ /// lane-av1rect: the encoder used to REFUSE every size where a 32x32
3352+ /// block straddles BOTH true frame edges at once ("straddles the true
3353+ /// frame edge in a way that needs a rectangular (HORZ/VERT) transform
3354+ /// this encoder does not code yet"). That refusal was a misreading of
3355+ /// spec 5.11.4: a block whose `hasRows` and `hasCols` are BOTH false
3356+ /// reads no partition symbol at all and is `PARTITION_SPLIT` by
3357+ /// inference, down to the 8x8 leaves whose own mi origin is inside the
3358+ /// frame -- exactly the leaves this writer already codes for the
3359+ /// one-axis case, and no rectangular transform anywhere. The sweep walks
3360+ /// the corners around the 32-multiples (each width has a 16x16 column
3361+ /// whose right half is outside, each height a row whose bottom half is,
3362+ /// so the bottom-right 16x16 of every size here is cut on both axes) and
3363+ /// three-way compares: our decoder, the encoder's own reconstruction and
3364+ /// ffmpeg, over a 4-frame GOP so the inter writer's copy of the same
3365+ /// signaling is covered too. `edge_both_cut_hits` proves the decoder
3366+ /// really took the inferred-split path rather than the gate passing on
3367+ /// geometry that never fires it.
3368+ #[test]
3369+ fn a_sweep_of_doubly_straddling_sizes_round_trips_through_ffmpeg() {
3370+ let fctx = &crate::decode::FrameCtx::new();
3371+ if !have_ffmpeg() {
3372+ eprintln!("SKIP a_sweep_of_doubly_straddling_sizes_round_trips_through_ffmpeg: no ffmpeg");
3373+ return;
3374+ }
3375+ const FRAMES: usize = 4;
3376+ let mut both_cut = 0usize;
3377+ for &width in &[200usize, 216, 232, 248] {
3378+ for &height in &[104usize, 120, 136, 152] {
3379+ let pictures: Vec<_> = (0..FRAMES as i64)
3380+ .map(|i| panned_test_card(width, height, i * 3))
3381+ .collect();
3382+ let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx)
3383+ .unwrap_or_else(|e| panic!("{width}x{height}: encoder refused: {e}"));
3384+ // Reset per attempt, never a delta across sizes (class
3385+ // counter-from-refused-stream).
3386+ crate::decode::reset_edge_both_cut_hits();
3387+ let decoded = decode_stream(&encoded.stream)
3388+ .unwrap_or_else(|e| panic!("{width}x{height}: our decoder refused: {e}"));
3389+ both_cut += crate::decode::edge_both_cut_hits().iter().sum::<usize>();
3390+ let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, FRAMES);
3391+ assert_eq!(
3392+ ffmpeg_frames.len(),
3393+ FRAMES,
3394+ "{width}x{height}: ffmpeg decoded {} of {FRAMES} frames",
3395+ ffmpeg_frames.len()
3396+ );
3397+ assert_eq!(decoded.len(), FRAMES, "{width}x{height}: frame count");
3398+ for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() {
3399+ assert_eq!(got.y, want.y, "{width}x{height} frame {i} luma vs ffmpeg");
3400+ assert_eq!(got.u, want.u, "{width}x{height} frame {i} U vs ffmpeg");
3401+ assert_eq!(got.v, want.v, "{width}x{height} frame {i} V vs ffmpeg");
3402+ }
3403+ for (i, (frame, want)) in encoded.frames.iter().zip(&ffmpeg_frames).enumerate() {
3404+ let rec = &frame.reconstruction;
3405+ assert_eq!(rec.y, want.y, "{width}x{height} frame {i} luma: recon vs ffmpeg");
3406+ assert_eq!(rec.u, want.u, "{width}x{height} frame {i} U: recon vs ffmpeg");
3407+ assert_eq!(rec.v, want.v, "{width}x{height} frame {i} V: recon vs ffmpeg");
3408+ }
3409+ }
3410+ }
3411+ assert!(
3412+ both_cut > 0,
3413+ "no partition node in the whole sweep was coded as the both-axes-cut \
3414+ inferred split -- the gate is blind to the feature it pins"
3415+ );
3416+ }
3417+
3351 /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME`3418 /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME`
3352 /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s3419 /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s
3353 /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc3420 /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc
crates/ec-av1/src/tile.rsmodified+29 -39
@@ -2706,30 +2706,26 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs(
2706 continue;2706 continue;
2707 }2707 }
2708 // The true edge falls inside this 16x162708 // The true edge falls inside this 16x16
2709- // leaf itself (mod-32==8 target sizes):2709+ // leaf itself: the block splits into the
2710- // one axis only, per r6's charter -- an2710+ // 8x8s that are inside, each its own
2711- // 8x8 leaf never itself straddles, so the2711+ // leaf. Same three-way spec signaling as
2712- // block splits cleanly along whichever2712+ // every level above -- a single gathered
2713- // axis is short and only the in-frame2713+ // bit when one half is outside, and
2714- // 8x8s are coded, each its own leaf.2714+ // nothing at all when BOTH are, where
2715- if !has_cols16 && !has_rows16 {2715+ // `decode_partition` infers the split
2716- return Err(Error::unsupported(2716+ // (lane-av1rect: no rectangular
2717- "AV1 tile",2717+ // transform is needed for this, the
2718- "a 16x16 block whose true edge cuts through both \2718+ // earlier refusal here misread 5.11.4).
2719- axes needs a rectangular transform this writer \2719+ match (has_cols16, has_rows16) {
2720- does not code yet",2720+ (true, false) => enc.symbol_fixed(
2721- ));
2722- }
2723- if has_cols16 {
2724- enc.symbol_fixed(
2725 1,2721 1,
2726 &gather(&cdfs.partition_w16[ctx], VERT_ALIKE),2722 &gather(&cdfs.partition_w16[ctx], VERT_ALIKE),
2727- );2723+ ),
2728- } else {2724+ (false, true) => enc.symbol_fixed(
2729- enc.symbol_fixed(
2730 1,2725 1,
2731 &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE),2726 &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE),
2732- );2727+ ),
2728+ _ => {}
2733 }2729 }
2734 let leaves = block.eight.as_ref().ok_or_else(|| {2730 let leaves = block.eight.as_ref().ok_or_else(|| {
2735 Error::unsupported(2731 Error::unsupported(
@@ -5359,23 +5355,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
5359 }5355 }
5360 for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) {5356 for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) {
5361 // A 16x16 leaf whose own half straddles the true5357 // A 16x16 leaf whose own half straddles the true
5362- // frame edge on both axes needs a rectangular5358+ // frame edge splits into the 8x8 leaves that are
5363- // transform this writer does not code yet; one5359+ // inside it (lane-av1inter8), same split the key
5364- // axis only is two (or, at a true corner, one)5360+ // frame search takes at this geometry -- on both
5365- // 8x8 leaves (lane-av1inter8), same split the key5361+ // axes too, where `decode_partition` infers the
5366- // frame search takes at this geometry.5362+ // split and reads no symbol at all
5363+ // (lane-av1rect).
5367 let (has_cols16, has_rows16) = (5364 let (has_cols16, has_rows16) = (
5368 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),5365 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
5369 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),5366 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
5370 );5367 );
5371- if !has_cols16 && !has_rows16 {
5372- return Err(Error::unsupported(
5373- "AV1 tile",
5374- "a 16x16 inter-frame block whose true edge cuts through \
5375- both axes needs a rectangular transform this writer \
5376- does not code yet",
5377- ));
5378- }
5379 let at16 = (sr, sc);5368 let at16 = (sr, sc);
5380 if has_cols16 && has_rows16 {5369 if has_cols16 && has_rows16 {
5381 let ctx16 = neighbours.partition_ctx(at16, SUB);5370 let ctx16 = neighbours.partition_ctx(at16, SUB);
@@ -5442,16 +5431,17 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
5442 )?;5431 )?;
5443 } else {5432 } else {
5444 let ctx16 = neighbours.partition_ctx(at16, SUB);5433 let ctx16 = neighbours.partition_ctx(at16, SUB);
5445- if has_cols16 {5434+ match (has_cols16, has_rows16) {
5446- enc.symbol_fixed(5435+ (true, false) => enc.symbol_fixed(
5447 1,5436 1,
5448 &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE),5437 &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE),
5449- );5438+ ),
5450- } else {5439+ (false, true) => enc.symbol_fixed(
5451- enc.symbol_fixed(
5452 1,5440 1,
5453 &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE),5441 &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE),
5454- );5442+ ),
5443+ // Both halves outside: SPLIT is inferred.
5444+ _ => {}
5455 }5445 }
5456 let leaves = leaf.eight.as_ref().ok_or_else(|| {5446 let leaves = leaf.eight.as_ref().ok_or_else(|| {
5457 Error::unsupported(5447 Error::unsupported(