From 787a14a5ca4c4b6522070d85655f3035db19971b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 06:22:08 +0300 Subject: [PATCH] fix(av1): a key frame test decoded its own tile as TxMode::Largest The nine failing gates fed decode_key_frame_tile a hardcoded tx_select = false while the encoder had moved its key frame header to TxMode::Select, so the decoder missed the first block tx_depth symbol and desynced at pixel (0,0). Encoded now carries the header bit and the encoder-driven decode sites pass it along. --- crates/ec-av1/src/decode.rs | 62 ++++++++++++++++++++++++++++++++++--- crates/ec-av1/src/encode.rs | 10 ++++++ crates/ec-av1/src/stream.rs | 2 +- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 3577a290..6c82aa95 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -36366,6 +36366,7 @@ mod tests { mi_cols, mi_rows, base_q_idx, + tx_select, .. }: Encoded = crate::encode::encode_key_frame_with_modes_with_ctx(&picture, 40, 0.0, modes, fctx).unwrap(); let decoded = decode_key_frame_tile( @@ -36378,7 +36379,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + tx_select, true, false, false, fctx, @@ -36743,7 +36744,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + encoded.tx_select, true, false, false, fctx, @@ -36755,6 +36756,59 @@ mod tests { } } + /// The key frame the encoder writes carries `tx_mode == TxMode::Select`, + /// and that header bit is load-bearing for a decode of its tile: read as a + /// `TxMode::Largest` frame the very first block misses its `tx_depth` + /// symbol and the whole tile desyncs. Pins the shape that broke nine gates + /// when `Encoded` had no `tx_select` for them to pass along — a test that + /// hardcoded the bit was decoding a different frame header than the one + /// the encoder wrote (and than the one `decode_stream`/ffmpeg read off the + /// wire). + #[test] + fn a_key_frames_tile_needs_the_headers_own_tx_select_bit() { + let fctx = &crate::decode::FrameCtx::new(); + use crate::encode::encode_key_frame_with_ctx; + let (width, height) = (64usize, 64usize); + let picture = round_trip_test_card(width, height); + let encoded = encode_key_frame_with_ctx(&picture, 100, 0.5, fctx).unwrap(); + assert!( + encoded.tx_select, + "the encoder's key frame no longer codes TxMode::Select -- this \ + gate's premise moved, not the decoder" + ); + let decode = |tx_select: bool| { + decode_key_frame_tile( + &encoded.tile, + encoded.mi_cols, + encoded.mi_rows, + encoded.base_q_idx, + width as u32, + height as u32, + false, + &CdefParams::default(), + &LoopFilterParams::default(), + tx_select, + true, + false, + false, + fctx, + ) + }; + let right = decode(true).expect("the header's own tx_mode decodes"); + assert_eq!(right.y, encoded.reconstruction.y, "luma under tx_select"); + assert_eq!(right.u, encoded.reconstruction.u, "U under tx_select"); + assert_eq!(right.v, encoded.reconstruction.v, "V under tx_select"); + // The wrong bit either refuses or reconstructs something else; what it + // must never do is agree, which would mean the bit codes nothing. + if let Ok(wrong) = decode(false) { + assert_ne!( + wrong.y, encoded.reconstruction.y, + "reading a TxMode::Select tile as Largest agreed -- the \ + tx_depth symbols are not being coded" + ); + } + } + fn round_trip_test_card(width: usize, height: usize) -> crate::encode::Picture { let mut picture = crate::encode::Picture::grey(width, height); for row in 0..height { @@ -36817,7 +36871,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + key.tx_select, true, false, false, fctx, @@ -37137,7 +37191,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + key.tx_select, true, false, false, fctx, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 94d09e9b..c992a5ea 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -449,6 +449,7 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En mi_cols: encoded.mi_cols, mi_rows: encoded.mi_rows, base_q_idx: encoded.base_q_idx, + tx_select: encoded.tx_select, } } @@ -478,6 +479,13 @@ pub struct Encoded { pub(crate) mi_cols: u32, pub(crate) mi_rows: u32, pub(crate) base_q_idx: u8, + /// This frame header's `tx_mode == TxMode::Select` ([`tx_select`], and + /// [`tx_select_inter`] on an inter frame) — a decoder of `tile` needs it, + /// and reading it off `stream`'s header is what `decode_stream` does. A + /// test that decodes `tile` directly must pass this one along, or it reads + /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first + /// block. + pub(crate) tx_select: bool, } /// The sequence and frame headers a picture of this size is coded under: one @@ -2869,6 +2877,7 @@ pub(crate) fn encode_key_frame_inner( mi_cols: header.mi_cols, mi_rows: header.mi_rows, base_q_idx, + tx_select, }) } @@ -3933,6 +3942,7 @@ pub(crate) fn encode_inter_frame( mi_cols: header.mi_cols, mi_rows: header.mi_rows, base_q_idx, + tx_select, }) } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 4e746aa1..f7ee04fb 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -2996,7 +2996,7 @@ mod tests { false, &ec_av1_syntax::CdefParams::default(), &ec_av1_syntax::LoopFilterParams::default(), - false, + encoded.tx_select, // Our own encoder always writes `reduced_tx_set: true`. true, false,