forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1rect' (91654d7a): pictures whose last block is cut on both frame edges encode; the refusal was a misreading of spec 5.11.4

Browse files at this commit

0e3a352b7beb5793abcfd40bd44a52835736d79dAhmet Kaan GümüşParent: 0ebeaad0e8b39f
When hasRows and hasCols are both false no partition symbol is read and
PARTITION_SPLIT is inferred down to the in-frame 8x8 leaves, so no
rectangular transform is involved. 16 doubly-straddling sizes round-trip
three-way exact through ffmpeg; byte pins and BD unchanged.
crates/ec-av1/src/encode.rsmodified+28 -51
@@ -5368,11 +5368,13 @@ pub(crate) fn encode_key_frame_inner(
5368 );5368 );
5369 let whole_legal = has_cols32 && has_rows32;5369 let whole_legal = has_cols32 && has_rows32;
5370 // A 16x16 sub-block's own hasCols/hasRows (spec5370 // A 16x16 sub-block's own hasCols/hasRows (spec
5371- // `decode_partition`, recomputed at this leaf's own half): one5371+ // `decode_partition`, recomputed at this leaf's own half):
5372- // axis false is a straddling leaf this writer codes as two5372+ // either axis false is a straddling leaf this writer codes as
5373- // 8x8 leaves (`crate::tile::write_leaf8`, lane-av1-rect r7);5373+ // the 8x8 leaves that are inside it
5374- // both axes false needs a rectangular transform this writer5374+ // (`crate::tile::write_leaf8`, lane-av1-rect r7). Both axes
5375- // has no size for at all, whether whole or split.5375+ // false is the same split with the partition symbol inferred
5376+ // rather than coded -- no rectangular transform is needed
5377+ // (lane-av1rect).
5376 let sub_half = |sr: usize, sc: usize| {5378 let sub_half = |sr: usize, sc: usize| {
5377 (5379 (
5378 crate::tile::has_half(5380 crate::tile::has_half(
@@ -5387,27 +5389,6 @@ pub(crate) fn encode_key_frame_inner(
5387 ),5389 ),
5388 )5390 )
5389 };5391 };
5390- let both_axes_cut = (0..4)
5391- .map(|i| (r0 + i / 2, c0 + i % 2))
5392- .filter(|&(sr, sc)| {
5393- (sr as u32) * crate::tile::SUB_MI < header.mi_rows
5394- && (sc as u32) * crate::tile::SUB_MI < header.mi_cols
5395- })
5396- .any(|(sr, sc)| {
5397- let (has_cols16, has_rows16) = sub_half(sr, sc);
5398- !has_cols16 && !has_rows16
5399- });
5400- if !whole_legal && both_axes_cut {
5401- return Err(Error::unsupported(
5402- "AV1 encode",
5403- format!(
5404- "the 32x32 block at ({col},{row}) straddles the true frame edge \
5405- in a way that needs a rectangular (HORZ/VERT) transform this \
5406- encoder does not code yet"
5407- ),
5408- ));
5409- }
5410-
5411 // What the whole 32x32 costs, including the partition symbol5392 // What the whole 32x32 costs, including the partition symbol
5412 // that says it is not split.5393 // that says it is not split.
5413 let (whole, mut cost_whole) = code_square(5394 let (whole, mut cost_whole) = code_square(
@@ -7643,16 +7624,6 @@ pub(crate) fn encode_inter_frame(
7643 header.mi_rows,7624 header.mi_rows,
7644 ),7625 ),
7645 );7626 );
7646- if !has_cols16 && !has_rows16 {
7647- return Err(Error::unsupported(
7648- "AV1 encode",
7649- format!(
7650- "the 16x16 block at ({sc},{sr}) straddles the true \
7651- frame edge in a way that needs a rectangular \
7652- (HORZ/VERT) transform this encoder does not code yet"
7653- ),
7654- ));
7655- }
7656 if has_cols16 && has_rows16 {7627 if has_cols16 && has_rows16 {
7657 let (x, y) = (sc * SUB, sr * SUB);7628 let (x, y) = (sc * SUB, sr * SUB);
7658 let (mi_row, mi_col) = (sr * 4, sc * 4);7629 let (mi_row, mi_col) = (sr * 4, sc * 4);
@@ -9915,24 +9886,30 @@ mod tests {
9915 }9886 }
9916 9887
9917 /// The sizes the encoder refuses, refused for a reason rather than by9888 /// The sizes the encoder refuses, refused for a reason rather than by
9918- /// panicking somewhere below. Most sizes off the 32x32 block grid encode9889+ /// panicking somewhere below. Sizes off the 32x32 block grid encode fine
9919- /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame9890+ /// (see `a_frame_round_trips_at_its_own_size`) -- the true frame edge
9920- /// edge lands past the halfway point of whichever block it falls in, so9891+ /// lands past the halfway point of whichever block it falls in, so
9921 /// `PARTITION_NONE` or a single gathered split flag still says everything9892 /// `PARTITION_NONE` or a single gathered split flag still says everything
9922- /// the spec needs. lane-av1-rect r7 wires the 8x8-leaf path live, so a9893+ /// the spec needs. lane-av1-rect r7 wired the 8x8-leaf path for a 16x16
9923- /// 16x16 leaf straddling on exactly one axis (e.g. 40x32, cols mod 32 ==9894+ /// leaf straddling on exactly one axis (e.g. 40x32), and lane-av1rect the
9924- /// 8) now encodes too (see `a_frame_round_trips_at_its_own_size`'s9895+ /// both-axes cut (e.g. 40x40, both dims mod 32 == 8), which spec 5.11.4
9925- /// sibling below). Only a leaf whose true edge cuts *both* axes (e.g.9896+ /// codes with NO partition symbol at all -- `PARTITION_SPLIT` inferred,
9926- /// 40x40, both dims mod 32 == 8) needs the rectangular transform neither9897+ /// down to the in-frame 8x8s, no rectangular transform anywhere (the old
9927- /// writer has, so that class is still refused, by name rather than by9898+ /// refusal here claimed otherwise and was wrong; see
9928- /// corrupting the stream.9899+ /// `crate::stream::tests::a_sweep_of_doubly_straddling_sizes_round_trips_
9900+ /// through_ffmpeg`). No size refusal is left: an 8x8 leaf can never
9901+ /// straddle either, because `MiCols`/`MiRows` are always EVEN (spec
9902+ /// 5.9.5 `compute_image_size`: `2 * ((frame_width + 7) >> 3)`), so the
9903+ /// true edge always lands on an 8-luma-sample boundary in mi terms and a
9904+ /// 4x4 leaf -- which neither writer has -- is never asked for. What is
9905+ /// still refused is a malformed picture.
9929 #[test]9906 #[test]
9930- fn a_picture_off_the_block_grid_is_refused() {9907+ fn a_picture_off_the_block_grid_encodes_and_a_malformed_one_is_refused() {
9931 let fctx = &crate::decode::FrameCtx::new();9908 let fctx = &crate::decode::FrameCtx::new();
9932- let msg = encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx)9909+ encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx)
9933- .expect_err("40x40 straddles both axes of a 16x16 leaf, needs a rectangular transform")9910+ .expect("40x40 cuts a 16x16 leaf on both axes, which is an inferred split");
9934- .to_string();9911+ encode_key_frame_with_ctx(&Picture::grey(36, 40), 100, 0.5, fctx)
9935- assert!(msg.contains("rectangular"), "refusal: {msg}");9912+ .expect("36 luma columns still round up to an even MiCols");
9936 9913
9937 let mut short = Picture::grey(64, 64);9914 let mut short = Picture::grey(64, 64);
9938 short.u.truncate(10);9915 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
@@ -2786,30 +2786,26 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs(
2786 continue;2786 continue;
2787 }2787 }
2788 // The true edge falls inside this 16x162788 // The true edge falls inside this 16x16
2789- // leaf itself (mod-32==8 target sizes):2789+ // leaf itself: the block splits into the
2790- // one axis only, per r6's charter -- an2790+ // 8x8s that are inside, each its own
2791- // 8x8 leaf never itself straddles, so the2791+ // leaf. Same three-way spec signaling as
2792- // block splits cleanly along whichever2792+ // every level above -- a single gathered
2793- // axis is short and only the in-frame2793+ // bit when one half is outside, and
2794- // 8x8s are coded, each its own leaf.2794+ // nothing at all when BOTH are, where
2795- if !has_cols16 && !has_rows16 {2795+ // `decode_partition` infers the split
2796- return Err(Error::unsupported(2796+ // (lane-av1rect: no rectangular
2797- "AV1 tile",2797+ // transform is needed for this, the
2798- "a 16x16 block whose true edge cuts through both \2798+ // earlier refusal here misread 5.11.4).
2799- axes needs a rectangular transform this writer \2799+ match (has_cols16, has_rows16) {
2800- does not code yet",2800+ (true, false) => enc.symbol_fixed(
2801- ));
2802- }
2803- if has_cols16 {
2804- enc.symbol_fixed(
2805 1,2801 1,
2806 &gather(&cdfs.partition_w16[ctx], VERT_ALIKE),2802 &gather(&cdfs.partition_w16[ctx], VERT_ALIKE),
2807- );2803+ ),
2808- } else {2804+ (false, true) => enc.symbol_fixed(
2809- enc.symbol_fixed(
2810 1,2805 1,
2811 &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE),2806 &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE),
2812- );2807+ ),
2808+ _ => {}
2813 }2809 }
2814 let leaves = block.eight.as_ref().ok_or_else(|| {2810 let leaves = block.eight.as_ref().ok_or_else(|| {
2815 Error::unsupported(2811 Error::unsupported(
@@ -5439,23 +5435,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
5439 }5435 }
5440 for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) {5436 for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) {
5441 // A 16x16 leaf whose own half straddles the true5437 // A 16x16 leaf whose own half straddles the true
5442- // frame edge on both axes needs a rectangular5438+ // frame edge splits into the 8x8 leaves that are
5443- // transform this writer does not code yet; one5439+ // inside it (lane-av1inter8), same split the key
5444- // axis only is two (or, at a true corner, one)5440+ // frame search takes at this geometry -- on both
5445- // 8x8 leaves (lane-av1inter8), same split the key5441+ // axes too, where `decode_partition` infers the
5446- // frame search takes at this geometry.5442+ // split and reads no symbol at all
5443+ // (lane-av1rect).
5447 let (has_cols16, has_rows16) = (5444 let (has_cols16, has_rows16) = (
5448 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),5445 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
5449 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),5446 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
5450 );5447 );
5451- if !has_cols16 && !has_rows16 {
5452- return Err(Error::unsupported(
5453- "AV1 tile",
5454- "a 16x16 inter-frame block whose true edge cuts through \
5455- both axes needs a rectangular transform this writer \
5456- does not code yet",
5457- ));
5458- }
5459 let at16 = (sr, sc);5448 let at16 = (sr, sc);
5460 if has_cols16 && has_rows16 {5449 if has_cols16 && has_rows16 {
5461 let ctx16 = neighbours.partition_ctx(at16, SUB);5450 let ctx16 = neighbours.partition_ctx(at16, SUB);
@@ -5522,16 +5511,17 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
5522 )?;5511 )?;
5523 } else {5512 } else {
5524 let ctx16 = neighbours.partition_ctx(at16, SUB);5513 let ctx16 = neighbours.partition_ctx(at16, SUB);
5525- if has_cols16 {5514+ match (has_cols16, has_rows16) {
5526- enc.symbol_fixed(5515+ (true, false) => enc.symbol_fixed(
5527 1,5516 1,
5528 &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE),5517 &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE),
5529- );5518+ ),
5530- } else {5519+ (false, true) => enc.symbol_fixed(
5531- enc.symbol_fixed(
5532 1,5520 1,
5533 &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE),5521 &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE),
5534- );5522+ ),
5523+ // Both halves outside: SPLIT is inferred.
5524+ _ => {}
5535 }5525 }
5536 let leaves = leaf.eight.as_ref().ok_or_else(|| {5526 let leaves = leaf.eight.as_ref().ok_or_else(|| {
5537 Error::unsupported(5527 Error::unsupported(