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(
53685368 );
53695369 let whole_legal = has_cols32 && has_rows32;
53705370 // A 16x16 sub-block's own hasCols/hasRows (spec
5371- // `decode_partition`, recomputed at this leaf's own half): one
5372- // axis false is a straddling leaf this writer codes as two
5373- // 8x8 leaves (`crate::tile::write_leaf8`, lane-av1-rect r7);
5374- // both axes false needs a rectangular transform this writer
5375- // has no size for at all, whether whole or split.
5371+ // `decode_partition`, recomputed at this leaf's own half):
5372+ // either axis false is a straddling leaf this writer codes as
5373+ // the 8x8 leaves that are inside it
5374+ // (`crate::tile::write_leaf8`, lane-av1-rect r7). Both axes
5375+ // false is the same split with the partition symbol inferred
5376+ // rather than coded -- no rectangular transform is needed
5377+ // (lane-av1rect).
53765378 let sub_half = |sr: usize, sc: usize| {
53775379 (
53785380 crate::tile::has_half(
@@ -5387,27 +5389,6 @@ pub(crate) fn encode_key_frame_inner(
53875389 ),
53885390 )
53895391 };
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-
54115392 // What the whole 32x32 costs, including the partition symbol
54125393 // that says it is not split.
54135394 let (whole, mut cost_whole) = code_square(
@@ -7643,16 +7624,6 @@ pub(crate) fn encode_inter_frame(
76437624 header.mi_rows,
76447625 ),
76457626 );
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- }
76567627 if has_cols16 && has_rows16 {
76577628 let (x, y) = (sc * SUB, sr * SUB);
76587629 let (mi_row, mi_col) = (sr * 4, sc * 4);
@@ -9915,24 +9886,30 @@ mod tests {
99159886 }
99169887
99179888 /// The sizes the encoder refuses, refused for a reason rather than by
9918- /// panicking somewhere below. Most sizes off the 32x32 block grid encode
9919- /// fine now (see `a_frame_round_trips_at_its_own_size`) -- the true frame
9920- /// edge lands past the halfway point of whichever block it falls in, so
9889+ /// panicking somewhere below. Sizes off the 32x32 block grid encode fine
9890+ /// (see `a_frame_round_trips_at_its_own_size`) -- the true frame edge
9891+ /// lands past the halfway point of whichever block it falls in, so
99219892 /// `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 a
9923- /// 16x16 leaf straddling on exactly one axis (e.g. 40x32, cols mod 32 ==
9924- /// 8) now encodes too (see `a_frame_round_trips_at_its_own_size`'s
9925- /// sibling below). Only a leaf whose true edge cuts *both* axes (e.g.
9926- /// 40x40, both dims mod 32 == 8) needs the rectangular transform neither
9927- /// writer has, so that class is still refused, by name rather than by
9928- /// corrupting the stream.
9893+ /// the spec needs. lane-av1-rect r7 wired the 8x8-leaf path for a 16x16
9894+ /// leaf straddling on exactly one axis (e.g. 40x32), and lane-av1rect the
9895+ /// both-axes cut (e.g. 40x40, both dims mod 32 == 8), which spec 5.11.4
9896+ /// codes with NO partition symbol at all -- `PARTITION_SPLIT` inferred,
9897+ /// down to the in-frame 8x8s, no rectangular transform anywhere (the old
9898+ /// refusal here claimed otherwise and was wrong; see
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.
99299906 #[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() {
99319908 let fctx = &crate::decode::FrameCtx::new();
9932- let msg = 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")
9934- .to_string();
9935- assert!(msg.contains("rectangular"), "refusal: {msg}");
9909+ encode_key_frame_with_ctx(&Picture::grey(40, 40), 100, 0.5, fctx)
9910+ .expect("40x40 cuts a 16x16 leaf on both axes, which is an inferred split");
9911+ encode_key_frame_with_ctx(&Picture::grey(36, 40), 100, 0.5, fctx)
9912+ .expect("36 luma columns still round up to an even MiCols");
99369913
99379914 let mut short = Picture::grey(64, 64);
99389915 short.u.truncate(10);
crates/ec-av1/src/stream.rsmodified+67 -0
@@ -3348,6 +3348,73 @@ pub(crate) mod tests {
33483348 }
33493349 }
33503350
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+
33513418 /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME`
33523419 /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s
33533420 /// `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(
27862786 continue;
27872787 }
27882788 // The true edge falls inside this 16x16
2789- // leaf itself (mod-32==8 target sizes):
2790- // one axis only, per r6's charter -- an
2791- // 8x8 leaf never itself straddles, so the
2792- // block splits cleanly along whichever
2793- // axis is short and only the in-frame
2794- // 8x8s are coded, each its own leaf.
2795- if !has_cols16 && !has_rows16 {
2796- return Err(Error::unsupported(
2797- "AV1 tile",
2798- "a 16x16 block whose true edge cuts through both \
2799- axes needs a rectangular transform this writer \
2800- does not code yet",
2801- ));
2802- }
2803- if has_cols16 {
2804- enc.symbol_fixed(
2789+ // leaf itself: the block splits into the
2790+ // 8x8s that are inside, each its own
2791+ // leaf. Same three-way spec signaling as
2792+ // every level above -- a single gathered
2793+ // bit when one half is outside, and
2794+ // nothing at all when BOTH are, where
2795+ // `decode_partition` infers the split
2796+ // (lane-av1rect: no rectangular
2797+ // transform is needed for this, the
2798+ // earlier refusal here misread 5.11.4).
2799+ match (has_cols16, has_rows16) {
2800+ (true, false) => enc.symbol_fixed(
28052801 1,
28062802 &gather(&cdfs.partition_w16[ctx], VERT_ALIKE),
2807- );
2808- } else {
2809- enc.symbol_fixed(
2803+ ),
2804+ (false, true) => enc.symbol_fixed(
28102805 1,
28112806 &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE),
2812- );
2807+ ),
2808+ _ => {}
28132809 }
28142810 let leaves = block.eight.as_ref().ok_or_else(|| {
28152811 Error::unsupported(
@@ -5439,23 +5435,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
54395435 }
54405436 for (leaf, (sr, sc)) in sub_blocks.iter().zip(sub_positions) {
54415437 // A 16x16 leaf whose own half straddles the true
5442- // frame edge on both axes needs a rectangular
5443- // transform this writer does not code yet; one
5444- // axis only is two (or, at a true corner, one)
5445- // 8x8 leaves (lane-av1inter8), same split the key
5446- // frame search takes at this geometry.
5438+ // frame edge splits into the 8x8 leaves that are
5439+ // inside it (lane-av1inter8), same split the key
5440+ // frame search takes at this geometry -- on both
5441+ // axes too, where `decode_partition` infers the
5442+ // split and reads no symbol at all
5443+ // (lane-av1rect).
54475444 let (has_cols16, has_rows16) = (
54485445 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
54495446 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
54505447 );
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- }
54595448 let at16 = (sr, sc);
54605449 if has_cols16 && has_rows16 {
54615450 let ctx16 = neighbours.partition_ctx(at16, SUB);
@@ -5522,16 +5511,17 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
55225511 )?;
55235512 } else {
55245513 let ctx16 = neighbours.partition_ctx(at16, SUB);
5525- if has_cols16 {
5526- enc.symbol_fixed(
5514+ match (has_cols16, has_rows16) {
5515+ (true, false) => enc.symbol_fixed(
55275516 1,
55285517 &gather(&cdfs.partition_w16[ctx16], VERT_ALIKE),
5529- );
5530- } else {
5531- enc.symbol_fixed(
5518+ ),
5519+ (false, true) => enc.symbol_fixed(
55325520 1,
55335521 &gather(&cdfs.partition_w16[ctx16], HORZ_ALIKE),
5534- );
5522+ ),
5523+ // Both halves outside: SPLIT is inferred.
5524+ _ => {}
55355525 }
55365526 let leaves = leaf.eight.as_ref().ok_or_else(|| {
55375527 Error::unsupported(