diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 62dd16ba..e30bcee4 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -36618,6 +36618,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( @@ -36630,7 +36631,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + tx_select, true, false, false, fctx, @@ -36995,7 +36996,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + encoded.tx_select, true, false, false, fctx, @@ -37007,6 +37008,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 { @@ -37069,7 +37123,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), - false, + key.tx_select, true, false, false, fctx, @@ -37389,7 +37443,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 fed75396..f9030e40 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -473,6 +473,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, } } @@ -502,6 +503,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 @@ -2913,6 +2921,7 @@ pub(crate) fn encode_key_frame_inner( mi_cols: header.mi_cols, mi_rows: header.mi_rows, base_q_idx, + tx_select, }) } @@ -4002,6 +4011,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,