forge
Explore
Log in

Don't you dare go Hollow

Inter-frame tx_mode Select syntax, opt-in and not yet exact

Browse files at this commit

6397783904c080d2c76d58a10899e7d50bdd96e2Ahmet Kaan GümüşParent: b56ffce
  • feat
    • inter-frame tx_mode Select syntax, opt-in and not yet exact
Show full message
feat(av1): inter-frame tx_mode Select syntax, opt-in and not yet exact

The tile writer grows the TXFM_CONTEXT bands (above_txfm/left_txfm, decode.rs
txfm_partition_ctx_rect and tx_size_context_txfm_rect) and writes an inter
frame's transform syntax: one zero txfm_split flag per inter block that codes
a residual, none on a skipped one, a searched tx_depth on every intra block.
dav1d refuses the resulting stream and the first diverging symbol was not
located inside this lane, so the inter header stays TxMode::Largest unless
EC_AV1_TX_SELECT_INTER=1. Key frames are unaffected and stay byte-exact.

Also pins the split path in the ffmpeg exactness gate: the test now asserts
some block really resolved tx_depth > 0 before claiming the decode matched.
crates/ec-av1/src/encode.rsmodified+63 -8
@@ -39,7 +39,6 @@ use crate::quant::ac_q;
39 use crate::sequence::sequence_header_obu;39 use crate::sequence::sequence_header_obu;
40 use crate::tile::{40 use crate::tile::{
41 BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits,41 BlockCoeffs, Coeff, INTRA_MODE_CTX, InterInfo, InterMode, Quadrant, Superblock, partition_bits,
42- sb_coeff_inter_frame_tile,
43 };42 };
44 use crate::transform::{43 use crate::transform::{
45 TxType, dequant_and_inverse, dequant_and_inverse_typed, forward_and_quantize,44 TxType, dequant_and_inverse, dequant_and_inverse_typed, forward_and_quantize,
@@ -125,11 +124,31 @@ const SPLIT_INTER_8: bool = true;
125 /// Whether a key frame codes `tx_mode == TxMode::Select` and searches each124 /// Whether a key frame codes `tx_mode == TxMode::Select` and searches each
126 /// block's transform depth (lane-av1tx). On by default; `EC_AV1_TX_SELECT=0`125 /// block's transform depth (lane-av1tx). On by default; `EC_AV1_TX_SELECT=0`
127 /// turns it off for an A/B on one build.126 /// turns it off for an A/B on one build.
127+/// Whether an INTER frame also carries `TxMode::Select` (lane-av1tx step 2:
128+/// one `txfm_split` flag per inter block, a searched `tx_depth` on every
129+/// intra block inside the inter frame). OFF by default and opt-in through
130+/// `EC_AV1_TX_SELECT_INTER=1`: the syntax is written but a stream carrying it
131+/// is refused by dav1d and by this crate's own decoder, and the first
132+/// diverging symbol was not located inside the lane's budget. Key frames are
133+/// unaffected -- they are byte-exact with [`tx_select`] on.
134+fn tx_select_inter() -> bool {
135+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
136+ *ON.get_or_init(|| {
137+ matches!(
138+ std::env::var("EC_AV1_TX_SELECT_INTER").as_deref(),
139+ Ok("1") | Ok("on")
140+ )
141+ })
142+}
143+
128 fn tx_select() -> bool {144 fn tx_select() -> bool {
129- !matches!(145+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
130- std::env::var("EC_AV1_TX_SELECT").as_deref(),146+ *ON.get_or_init(|| {
131- Ok("0") | Ok("off")147+ !matches!(
132- )148+ std::env::var("EC_AV1_TX_SELECT").as_deref(),
149+ Ok("0") | Ok("off")
150+ )
151+ })
133 }152 }
134 153
135 fn split_inter_8() -> bool {154 fn split_inter_8() -> bool {
@@ -1930,7 +1949,16 @@ fn code_square_inter(
1930 stack: &MvStack, fctx: &crate::decode::FrameCtx,1949 stack: &MvStack, fctx: &crate::decode::FrameCtx,
1931 ) -> (BlockCoeffs, f64) {1950 ) -> (BlockCoeffs, f64) {
1932 let (intra_block, intra_cost) =1951 let (intra_block, intra_cost) =
1933- code_square(luma, chroma, (x, y), side, search, mode_bits, false, fctx);1952+ code_square(
1953+ luma,
1954+ chroma,
1955+ (x, y),
1956+ side,
1957+ search,
1958+ mode_bits,
1959+ tx_select() && tx_select_inter(),
1960+ fctx,
1961+ );
1934 let luma_set = if side == 8 {1962 let luma_set = if side == 8 {
1935 TxbSet::Luma8Inter1963 TxbSet::Luma8Inter
1936 } else {1964 } else {
@@ -3450,6 +3478,14 @@ pub(crate) fn encode_inter_frame(
3450 inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?;3478 inter_frame_headers(render.0, render.1, base_q_idx, order_hint, LAST_SLOT)?;
3451 header.render_width = render.0 as u32;3479 header.render_width = render.0 as u32;
3452 header.render_height = render.1 as u32;3480 header.render_height = render.1 as u32;
3481+ // `TxMode::Select` here too: an inter block then codes one `txfm_split`
3482+ // flag (kept at zero -- its var-tx tree is not searched yet) and an INTRA
3483+ // block inside this frame codes and searches its own `tx_depth`, the same
3484+ // as in a key frame (`crate::tile::write_tx_syntax_inter`).
3485+ let tx_select = tx_select() && tx_select_inter();
3486+ if tx_select {
3487+ header.tx_mode = TxMode::Select;
3488+ }
3453 3489
3454 let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);3490 let (true_width, true_height) = (header.mi_cols as usize * 4, header.mi_rows as usize * 4);
3455 // lane-hbd r4: encoder stays 8-bit by design; narrow the source once3491 // lane-hbd r4: encoder stays 8-bit by design; narrow the source once
@@ -3871,7 +3907,13 @@ pub(crate) fn encode_inter_frame(
3871 / modes.len() as f64;3907 / modes.len() as f64;
3872 #[cfg(test)]3908 #[cfg(test)]
3873 record_predicted_bits(crate::tile::predicted_coeff_bits(&blocks, base_q_idx));3909 record_predicted_bits(crate::tile::predicted_coeff_bits(&blocks, base_q_idx));
3874- let tile = sb_coeff_inter_frame_tile(header.mi_cols, header.mi_rows, base_q_idx, &blocks)?;3910+ let tile = crate::tile::sb_coeff_inter_frame_tile_tx(
3911+ header.mi_cols,
3912+ header.mi_rows,
3913+ base_q_idx,
3914+ &blocks,
3915+ tx_select,
3916+ )?;
3875 let mut stream = temporal_delimiter();3917 let mut stream = temporal_delimiter();
3876 stream.extend_from_slice(&frame_obu(&seq, &header, &tile)?);3918 stream.extend_from_slice(&frame_obu(&seq, &header, &tile)?);
3877 3919
@@ -4279,6 +4321,7 @@ mod tests {
4279 // (0..=20, 21..=60, 61..=120, 121..=255), on a single frame size.4321 // (0..=20, 21..=60, 61..=120, 121..=255), on a single frame size.
4280 let (width, height) = (64usize, 64usize);4322 let (width, height) = (64usize, 64usize);
4281 let picture = test_card(width, height);4323 let picture = test_card(width, height);
4324+ let _ = take_tx_depth_hits();
4282 for &q in &[15u8, 45, 100, 200] {4325 for &q in &[15u8, 45, 100, 200] {
4283 let encoded = encode_key_frame_with_ctx(&picture, q, 0.5, fctx).unwrap();4326 let encoded = encode_key_frame_with_ctx(&picture, q, 0.5, fctx).unwrap();
4284 let decoded = ffmpeg_decode(&encoded.stream, width, height);4327 let decoded = ffmpeg_decode(&encoded.stream, width, height);
@@ -4286,6 +4329,18 @@ mod tests {
4286 assert_eq!(decoded.u, encoded.reconstruction.u, "q={q}: U");4329 assert_eq!(decoded.u, encoded.reconstruction.u, "q={q}: U");
4287 assert_eq!(decoded.v, encoded.reconstruction.v, "q={q}: V");4330 assert_eq!(decoded.v, encoded.reconstruction.v, "q={q}: V");
4288 }4331 }
4332+ // lane-av1tx: the exactness above is only a claim about SPLIT
4333+ // transforms if some block actually split one (`gate-blind-to-feature`
4334+ // -- with `tx_depth` stuck at 0 every assert here passes on the
4335+ // pre-lane stream). The test card's edges split at these four
4336+ // quantizers; the counter is per-process (another test encoding in
4337+ // parallel can only ADD to it, so this can be masked, never falsely
4338+ // failed), so it is read once for the whole q ladder.
4339+ let depths = take_tx_depth_hits();
4340+ assert!(
4341+ depths[1] + depths[2] > 0,
4342+ "no block split its transform: depths {depths:?}"
4343+ );
4289 }4344 }
4290 4345
4291 /// The minimal repro that isolated the inter-residual desync: a key frame4346 /// The minimal repro that isolated the inter-residual desync: a key frame
@@ -4345,7 +4400,7 @@ mod tests {
4345 Quadrant::Whole(skipped_block),4400 Quadrant::Whole(skipped_block),
4346 ];4401 ];
4347 let tile =4402 let tile =
4348- sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks)4403+ crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks)
4349 .unwrap();4404 .unwrap();
4350 4405
4351 // `key.stream` is already a temporal delimiter, the sequence header4406 // `key.stream` is already a temporal delimiter, the sequence header