forge
Explore
Log in

Don't you dare go Hollow

A key frame test decoded its own tile as TxMode::Largest

Browse files at this commit

787a14a5ca4c4b6522070d85655f3035db19971bAhmet Kaan GümüşParent: 6397783
  • fix
    • a key frame test decoded its own tile as TxMode::Largest
Show full message
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.rsmodified+58 -4
@@ -36366,6 +36366,7 @@ mod tests {
3636636366 mi_cols,
3636736367 mi_rows,
3636836368 base_q_idx,
36369+ tx_select,
3636936370 ..
3637036371 }: Encoded = crate::encode::encode_key_frame_with_modes_with_ctx(&picture, 40, 0.0, modes, fctx).unwrap();
3637136372 let decoded = decode_key_frame_tile(
@@ -36378,7 +36379,7 @@ mod tests {
3637836379 false,
3637936380 &CdefParams::default(),
3638036381 &LoopFilterParams::default(),
36381- false,
36382+ tx_select,
3638236383 true,
3638336384 false,
3638436385 false, fctx,
@@ -36743,7 +36744,7 @@ mod tests {
3674336744 false,
3674436745 &CdefParams::default(),
3674536746 &LoopFilterParams::default(),
36746- false,
36747+ encoded.tx_select,
3674736748 true,
3674836749 false,
3674936750 false, fctx,
@@ -36755,6 +36756,59 @@ mod tests {
3675536756 }
3675636757 }
3675736758
36759+ /// The key frame the encoder writes carries `tx_mode == TxMode::Select`,
36760+ /// and that header bit is load-bearing for a decode of its tile: read as a
36761+ /// `TxMode::Largest` frame the very first block misses its `tx_depth`
36762+ /// symbol and the whole tile desyncs. Pins the shape that broke nine gates
36763+ /// when `Encoded` had no `tx_select` for them to pass along — a test that
36764+ /// hardcoded the bit was decoding a different frame header than the one
36765+ /// the encoder wrote (and than the one `decode_stream`/ffmpeg read off the
36766+ /// wire).
36767+ #[test]
36768+ fn a_key_frames_tile_needs_the_headers_own_tx_select_bit() {
36769+ let fctx = &crate::decode::FrameCtx::new();
36770+ use crate::encode::encode_key_frame_with_ctx;
36771+ let (width, height) = (64usize, 64usize);
36772+ let picture = round_trip_test_card(width, height);
36773+ let encoded = encode_key_frame_with_ctx(&picture, 100, 0.5, fctx).unwrap();
36774+ assert!(
36775+ encoded.tx_select,
36776+ "the encoder's key frame no longer codes TxMode::Select -- this \
36777+ gate's premise moved, not the decoder"
36778+ );
36779+ let decode = |tx_select: bool| {
36780+ decode_key_frame_tile(
36781+ &encoded.tile,
36782+ encoded.mi_cols,
36783+ encoded.mi_rows,
36784+ encoded.base_q_idx,
36785+ width as u32,
36786+ height as u32,
36787+ false,
36788+ &CdefParams::default(),
36789+ &LoopFilterParams::default(),
36790+ tx_select,
36791+ true,
36792+ false,
36793+ false,
36794+ fctx,
36795+ )
36796+ };
36797+ let right = decode(true).expect("the header's own tx_mode decodes");
36798+ assert_eq!(right.y, encoded.reconstruction.y, "luma under tx_select");
36799+ assert_eq!(right.u, encoded.reconstruction.u, "U under tx_select");
36800+ assert_eq!(right.v, encoded.reconstruction.v, "V under tx_select");
36801+ // The wrong bit either refuses or reconstructs something else; what it
36802+ // must never do is agree, which would mean the bit codes nothing.
36803+ if let Ok(wrong) = decode(false) {
36804+ assert_ne!(
36805+ wrong.y, encoded.reconstruction.y,
36806+ "reading a TxMode::Select tile as Largest agreed -- the \
36807+ tx_depth symbols are not being coded"
36808+ );
36809+ }
36810+ }
36811+
3675836812 fn round_trip_test_card(width: usize, height: usize) -> crate::encode::Picture {
3675936813 let mut picture = crate::encode::Picture::grey(width, height);
3676036814 for row in 0..height {
@@ -36817,7 +36871,7 @@ mod tests {
3681736871 false,
3681836872 &CdefParams::default(),
3681936873 &LoopFilterParams::default(),
36820- false,
36874+ key.tx_select,
3682136875 true,
3682236876 false,
3682336877 false, fctx,
@@ -37137,7 +37191,7 @@ mod tests {
3713737191 false,
3713837192 &CdefParams::default(),
3713937193 &LoopFilterParams::default(),
37140- false,
37194+ key.tx_select,
3714137195 true,
3714237196 false,
3714337197 false, fctx,
crates/ec-av1/src/encode.rsmodified+10 -0
@@ -449,6 +449,7 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En
449449 mi_cols: encoded.mi_cols,
450450 mi_rows: encoded.mi_rows,
451451 base_q_idx: encoded.base_q_idx,
452+ tx_select: encoded.tx_select,
452453 }
453454 }
454455
@@ -478,6 +479,13 @@ pub struct Encoded {
478479 pub(crate) mi_cols: u32,
479480 pub(crate) mi_rows: u32,
480481 pub(crate) base_q_idx: u8,
482+ /// This frame header's `tx_mode == TxMode::Select` ([`tx_select`], and
483+ /// [`tx_select_inter`] on an inter frame) — a decoder of `tile` needs it,
484+ /// and reading it off `stream`'s header is what `decode_stream` does. A
485+ /// test that decodes `tile` directly must pass this one along, or it reads
486+ /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first
487+ /// block.
488+ pub(crate) tx_select: bool,
481489 }
482490
483491 /// 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(
28692877 mi_cols: header.mi_cols,
28702878 mi_rows: header.mi_rows,
28712879 base_q_idx,
2880+ tx_select,
28722881 })
28732882 }
28742883
@@ -3933,6 +3942,7 @@ pub(crate) fn encode_inter_frame(
39333942 mi_cols: header.mi_cols,
39343943 mi_rows: header.mi_rows,
39353944 base_q_idx,
3945+ tx_select,
39363946 })
39373947 }
39383948
crates/ec-av1/src/stream.rsmodified+1 -1
@@ -2996,7 +2996,7 @@ mod tests {
29962996 false,
29972997 &ec_av1_syntax::CdefParams::default(),
29982998 &ec_av1_syntax::LoopFilterParams::default(),
2999- false,
2999+ encoded.tx_select,
30003000 // Our own encoder always writes `reduced_tx_set: true`.
30013001 true,
30023002 false,