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 {
36366 mi_cols,36366 mi_cols,
36367 mi_rows,36367 mi_rows,
36368 base_q_idx,36368 base_q_idx,
36369+ tx_select,
36369 ..36370 ..
36370 }: Encoded = crate::encode::encode_key_frame_with_modes_with_ctx(&picture, 40, 0.0, modes, fctx).unwrap();36371 }: Encoded = crate::encode::encode_key_frame_with_modes_with_ctx(&picture, 40, 0.0, modes, fctx).unwrap();
36371 let decoded = decode_key_frame_tile(36372 let decoded = decode_key_frame_tile(
@@ -36378,7 +36379,7 @@ mod tests {
36378 false,36379 false,
36379 &CdefParams::default(),36380 &CdefParams::default(),
36380 &LoopFilterParams::default(),36381 &LoopFilterParams::default(),
36381- false,36382+ tx_select,
36382 true,36383 true,
36383 false,36384 false,
36384 false, fctx,36385 false, fctx,
@@ -36743,7 +36744,7 @@ mod tests {
36743 false,36744 false,
36744 &CdefParams::default(),36745 &CdefParams::default(),
36745 &LoopFilterParams::default(),36746 &LoopFilterParams::default(),
36746- false,36747+ encoded.tx_select,
36747 true,36748 true,
36748 false,36749 false,
36749 false, fctx,36750 false, fctx,
@@ -36755,6 +36756,59 @@ mod tests {
36755 }36756 }
36756 }36757 }
36757 36758
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+
36758 fn round_trip_test_card(width: usize, height: usize) -> crate::encode::Picture {36812 fn round_trip_test_card(width: usize, height: usize) -> crate::encode::Picture {
36759 let mut picture = crate::encode::Picture::grey(width, height);36813 let mut picture = crate::encode::Picture::grey(width, height);
36760 for row in 0..height {36814 for row in 0..height {
@@ -36817,7 +36871,7 @@ mod tests {
36817 false,36871 false,
36818 &CdefParams::default(),36872 &CdefParams::default(),
36819 &LoopFilterParams::default(),36873 &LoopFilterParams::default(),
36820- false,36874+ key.tx_select,
36821 true,36875 true,
36822 false,36876 false,
36823 false, fctx,36877 false, fctx,
@@ -37137,7 +37191,7 @@ mod tests {
37137 false,37191 false,
37138 &CdefParams::default(),37192 &CdefParams::default(),
37139 &LoopFilterParams::default(),37193 &LoopFilterParams::default(),
37140- false,37194+ key.tx_select,
37141 true,37195 true,
37142 false,37196 false,
37143 false, fctx,37197 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
449 mi_cols: encoded.mi_cols,449 mi_cols: encoded.mi_cols,
450 mi_rows: encoded.mi_rows,450 mi_rows: encoded.mi_rows,
451 base_q_idx: encoded.base_q_idx,451 base_q_idx: encoded.base_q_idx,
452+ tx_select: encoded.tx_select,
452 }453 }
453 }454 }
454 455
@@ -478,6 +479,13 @@ pub struct Encoded {
478 pub(crate) mi_cols: u32,479 pub(crate) mi_cols: u32,
479 pub(crate) mi_rows: u32,480 pub(crate) mi_rows: u32,
480 pub(crate) base_q_idx: u8,481 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,
481 }489 }
482 490
483 /// The sequence and frame headers a picture of this size is coded under: one491 /// 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(
2869 mi_cols: header.mi_cols,2877 mi_cols: header.mi_cols,
2870 mi_rows: header.mi_rows,2878 mi_rows: header.mi_rows,
2871 base_q_idx,2879 base_q_idx,
2880+ tx_select,
2872 })2881 })
2873 }2882 }
2874 2883
@@ -3933,6 +3942,7 @@ pub(crate) fn encode_inter_frame(
3933 mi_cols: header.mi_cols,3942 mi_cols: header.mi_cols,
3934 mi_rows: header.mi_rows,3943 mi_rows: header.mi_rows,
3935 base_q_idx,3944 base_q_idx,
3945+ tx_select,
3936 })3946 })
3937 }3947 }
3938 3948
crates/ec-av1/src/stream.rsmodified+1 -1
@@ -2996,7 +2996,7 @@ mod tests {
2996 false,2996 false,
2997 &ec_av1_syntax::CdefParams::default(),2997 &ec_av1_syntax::CdefParams::default(),
2998 &ec_av1_syntax::LoopFilterParams::default(),2998 &ec_av1_syntax::LoopFilterParams::default(),
2999- false,2999+ encoded.tx_select,
3000 // Our own encoder always writes `reduced_tx_set: true`.3000 // Our own encoder always writes `reduced_tx_set: true`.
3001 true,3001 true,
3002 false,3002 false,