From 2fd2ac4f3565bc832ab1230e8c904cb24eafaa41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 22:41:01 +0300 Subject: [PATCH] feat(av1): search an intra luma transform unit's own tx_type The writer coded DCT_DCT for every luma transform unit ever written -- the symbol was a constant -- while the five-type reduced-set alphabet (TX_SET_INTRA_2: IDTX, DCT_DCT, ADST_ADST, ADST_DCT, DCT_ADST) was already the CDF it wrote into. The search now offers that set per intra luma unit, priced through the same RDOQ'd trial path (the tx_type symbol included, coeff_bits_typed), and the writer names the chosen type through the decoder's own symbol map (decode::tx_type_symbol, keyed by the CDF's alphabet width, so the two cannot drift). The forward transform gains the FLIPADST family as well: a flipped axis is the plain ADST basis over the mirrored residual, which is what the decoder's ud_flip/lr_flip undoes. All sixteen types now round trip. --- crates/ec-av1/src/decode.rs | 8 +++ crates/ec-av1/src/encode.rs | 127 ++++++++++++++++++++++++++++++--- crates/ec-av1/src/speed.rs | 8 +++ crates/ec-av1/src/tile.rs | 82 ++++++++++++++++++--- crates/ec-av1/src/transform.rs | 46 ++++++++++-- 5 files changed, 248 insertions(+), 23 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index d9664710..1836be66 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -5988,6 +5988,14 @@ pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option { } } +/// [`tx_type_from_symbol`] inverted: which symbol of a `cdf_len`-wide +/// `tx_type` CDF names `tx_type`, or `None` when that set does not hold it +/// (class `table-and-reader-move-together` -- the writer picks its symbol +/// through the reader's own map, so the two cannot drift). +pub(crate) fn tx_type_symbol(cdf_len: usize, tx_type: TxType) -> Option { + (0..cdf_len.saturating_sub(1)).find(|&t| tx_type_from_symbol(cdf_len, t) == Some(tx_type)) +} + /// The remainder of a level the base and base-range syntax could not reach /// (spec 5.11.40): its bit length in unary, then that many of its own bits, /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 9b36a7d7..e4f87479 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -252,6 +252,40 @@ fn tx_select_inter() -> bool { ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_SELECT_INTER)) } +/// The transform types an intra luma transform unit of `set` may be coded +/// with -- exactly the alphabet the writer's `tx_type` symbol names for that +/// set, so a searched type is always one the tile can express (lane-txset). +/// `DCT_DCT` first: it is the incumbent every other candidate has to beat. +/// +/// Under this encoder's `reduced_tx_set` header bit every intra set at 16x16 +/// and below is `TX_SET_INTRA_2` (spec `get_tx_set`), the five types +/// [`TxType::from_symbol`] names; 32x32 and 64x64 luma code no symbol at all, +/// and inter luma reads the two-type `TX_SET_INTER_3`, left at `DCT_DCT` +/// here. +pub(crate) fn tx_type_candidates(set: TxbSet) -> &'static [TxType] { + const DCT: [TxType; 1] = [TxType::DctDct]; + const INTRA2: [TxType; 5] = [ + TxType::DctDct, + TxType::AdstAdst, + TxType::AdstDct, + TxType::DctAdst, + TxType::Idtx, + ]; + match set { + TxbSet::Luma16 | TxbSet::Luma8 | TxbSet::Luma4 if tx_type_search() => &INTRA2, + _ => &DCT, + } +} + +fn tx_type_search() -> bool { + static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + crate::envflags::var("EC_AV1_TXSET") + .ok() + .map(|v| !matches!(v.as_str(), "0" | "off")) + }); + ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_TYPE_SEARCH)) +} + fn tx_select() -> bool { static ENV: std::sync::LazyLock> = std::sync::LazyLock::new(|| { crate::envflags::var("EC_AV1_TX_SELECT") @@ -2128,6 +2162,7 @@ impl Plane<'_> { let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); let bits = crate::tile::rdoq( &mut levels, &scaled, side, base_q_idx, at.set, skip_ctx, sign_ctx, rdoq_lambda(), + tx_type, ); (levels, Some(bits)) } else { @@ -2182,12 +2217,13 @@ impl Plane<'_> { } else { { let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); - crate::tile::coeff_bits( + crate::tile::coeff_bits_typed( &levels, at.set, crate::decode::q_ctx_of(base_q_idx), skip_ctx, sign_ctx, + tx_type, ) } }; @@ -2258,6 +2294,7 @@ impl Plane<'_> { let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, set); let bits = crate::tile::rdoq( &mut levels, &scaled, side, base_q_idx, set, skip_ctx, sign_ctx, rdoq_lambda(), + TxType::DctDct, ); (levels, Some(bits)) } else { @@ -2353,7 +2390,7 @@ impl Plane<'_> { // `filter_intra`), not once over the whole block. filter_intra: Option, search: &Search, fctx: &crate::decode::FrameCtx, - ) -> (Vec, f64, f64) { + ) -> (Vec, f64, f64, Vec) { let At { x, y, side, reach, .. } = at; let tx = side >> depth; let n = 1usize << depth; @@ -2369,6 +2406,11 @@ impl Plane<'_> { // for its `txb_skip` context, not context 0 ([`Plane::coef_ctx`]). self.ctx.tu_split = n > 1; let (mut sse, mut bits) = (0.0, 0.0); + // One entry per unit at its own `tu_row * n + tu_col`, which is how + // the writer indexes them (tile.rs `write_luma_tus`) -- a unit the + // frame edge cuts away is never coded there, so a compacted list + // would slide every later unit's type onto the wrong transform. + let mut tx_types = vec![TxType::DctDct; n * n]; for tu_row in 0..n { for tu_col in 0..n { let (col_off, row_off) = (tu_col * tx, tu_row * tx); @@ -2406,7 +2448,27 @@ impl Plane<'_> { ) } None => { - self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx) + // lane-txset: the unit picks its own transform type + // out of the set the writer can name it in, priced + // through the same RDOQ'd trial path (its `bits` + // already carry the `tx_type` symbol itself). + let mut best: Option<(f64, TxType, Trial)> = None; + for &tx_type in tx_type_candidates(set) { + let candidate = self.trial_typed( + tu, mode, angle_delta, search.base_q_idx, search.deadzone, + tx_type, fctx, + ); + let cost = candidate.sse + search.lambda * candidate.bits; + if best.as_ref().is_none_or(|(best, _, _)| cost < *best) { + best = Some((cost, tx_type, candidate)); + } + } + let (_, tx_type, trial) = + best.expect("the candidate list is never empty"); + tx_types[tu_row * n + tu_col] = tx_type; + TX_TYPE_HITS[tx_type as usize] + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); + trial } }; sse += trial.sse; @@ -2419,7 +2481,7 @@ impl Plane<'_> { } } self.ctx.tu_split = false; - (levels, sse, bits) + (levels, sse, bits, tx_types) } /// Writes a trial's reconstruction back into the plane, and the @@ -2637,7 +2699,7 @@ impl Plane<'_> { at: At, search: &Search, mode_bits: &[f64; 13], fctx: &crate::decode::FrameCtx, - ) -> (Vec, u8, i32, f64) { + ) -> (Vec, u8, i32, f64, TxType) { let At { x, y, side, reach, .. } = at; @@ -2777,6 +2839,23 @@ impl Plane<'_> { ANGLE_DELTA_HITS[(crate::tile::ANGLE_DELTA_ZERO as i32 + angle_delta) as usize] .fetch_add(1, std::sync::atomic::Ordering::Relaxed); } + // lane-txset: the block's own transform type, searched on the mode + // and angle the trials above settled -- the same ordering the angle + // refinement uses, and the same RD terms. + let mut tx_type = TxType::DctDct; + for &candidate_type in tx_type_candidates(at.set).iter().skip(1) { + let candidate = self.trial_typed( + at, mode, angle_delta, search.base_q_idx, search.deadzone, candidate_type, fctx, + ); + let cost_t = + candidate.sse + search.lambda * (candidate.bits + mode_bits[usize::from(mode)]); + if cost_t < cost { + cost = cost_t; + tx_type = candidate_type; + trial = candidate; + } + } + TX_TYPE_HITS[tx_type as usize].fetch_add(1, std::sync::atomic::Ordering::Relaxed); if census_on() { LUMA_RANK[sad_rank(&ranking, mode).min(12)] .fetch_add(1, std::sync::atomic::Ordering::Relaxed); @@ -2784,7 +2863,7 @@ impl Plane<'_> { self.commit(x, y, side, &trial); // A 64-point transform codes only its top-left 32x32 corner, which is // the grid `code_from_prediction` hands back (lane-i64). - (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost) + (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost, tx_type) } } @@ -3586,6 +3665,18 @@ fn tx_depth_bits(side: usize, depth: usize) -> f64 { } } +/// How many luma transform units resolved to each [`TxType`], indexed by the +/// enum's own discriminant (lane-txset): the fire count a witness asserts on, +/// and the instrument that says which types the search actually reaches. +pub(crate) static TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] = + [const { std::sync::atomic::AtomicUsize::new(0) }; 16]; + +/// Reads [`TX_TYPE_HITS`] and zeroes it, so a gate or a test attributes the +/// counts to its own encode. +pub(crate) fn take_tx_type_hits() -> [usize; 16] { + std::array::from_fn(|t| TX_TYPE_HITS[t].swap(0, std::sync::atomic::Ordering::Relaxed)) +} + /// How many blocks resolved to each `tx_depth`, the fire count that says /// whether the depth search below reaches its candidates at all. static TX_DEPTH_HITS: [std::sync::atomic::AtomicUsize; 3] = @@ -4377,8 +4468,9 @@ fn code_square( reach: Reach::of(side, x, y, luma.true_width, luma.true_height, fctx), set: luma_set, }; - let (mut luma_coeffs, mode, angle_delta, mut cost) = + let (mut luma_coeffs, mode, angle_delta, mut cost, tx_type) = luma.search_block(at, search, mode_bits, fctx); + let mut luma_tx_types = vec![tx_type]; // The depth search runs on the mode the whole-block transform picked -- // libaom's own ordering, and the reason it costs one extra pass per depth // rather than one per (mode, depth) pair. @@ -4398,15 +4490,19 @@ fn code_square( luma.snapshot(x, y, side), luma_coeffs, ); + let mut best_types = std::mem::take(&mut luma_tx_types); for depth in 1..=max_tx_depth(side) { - let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx); + let (levels, sse, bits, types) = + luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx); let cost_d = sse + search.lambda * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth)); if cost_d < best.0 { best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side)); + best_types = types; } } + luma_tx_types = best_types; luma.restore(x, y, side, &best.2); cost = best.0; tx_depth = best.1 as u8; @@ -4503,7 +4599,10 @@ fn code_square( let mut best: Option<(f64, u8, usize, (Vec, Vec), Vec)> = None; for fi in 0..5u8 { for depth in 0..=max_depth { - let (levels, sse, bits) = + // A filter-intra block's units are coded through + // `code_from_prediction`, which is `DCT_DCT` throughout, so + // this arm keeps the empty (all-`DCT_DCT`) type list. + let (levels, sse, bits, _) = luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx); let cost_f = sse + search.lambda @@ -4564,6 +4663,12 @@ fn code_square( } } } + // A palette or filter-intra block's luma was recoded through `DCT_DCT` + // (its levels came from `code_from_prediction`), so the searched types + // belong to a block this one no longer is. + if palette.is_some() || filter_intra.is_some() { + luma_tx_types = Vec::new(); + } ( BlockCoeffs { u, @@ -4577,6 +4682,7 @@ fn code_square( filter_intra, cfl_alphas, angle_delta_y: angle_delta as i8, + luma_tx_types, ..BlockCoeffs::default() }, cost, @@ -5846,6 +5952,7 @@ fn code_square_inter( palette: None, palette_uv: None, tx_depth, + luma_tx_types: Vec::new(), inter: Some(info), motion_mode: motion_won, }, @@ -5868,6 +5975,7 @@ fn code_square_inter( chroma[0].commit(x / 2, y / 2, side / 2, &u); chroma[1].commit(x / 2, y / 2, side / 2, &v); (BlockCoeffs { + luma_tx_types: Vec::new(), angle_delta_y: 0, cfl_alphas: None, filter_intra: None, @@ -9116,6 +9224,7 @@ fn search_inter_block( chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); ( BlockCoeffs { + luma_tx_types: Vec::new(), angle_delta_y: 0, cfl_alphas: None, filter_intra: None, diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index 483d159a..00a08b14 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -437,6 +437,14 @@ pub(crate) const RESTORATION: [bool; 11] = [ /// `encode::TPL_DEPTH`: lookahead pictures the lambda map reads (1 = off). pub(crate) const TPL_DEPTH: [usize; 11] = [crate::encode::TPL_DEPTH, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; +/// `encode::tx_type_search`: whether an intra luma transform unit searches +/// its own `tx_type` instead of coding `DCT_DCT` (lane-txset). The five-type +/// reduced-set alphabet the writer already codes is what it picks from, so a +/// preset that turns it off is byte-identical to the encoder before the lane. +pub(crate) const TX_TYPE_SEARCH: [bool; 11] = [ + true, true, true, true, true, true, true, false, false, false, false, +]; + /// `filter_search`: whether the deblock ladder's +-1/+-2 refinement stage runs. pub(crate) const DEBLOCK_REFINE: [bool; 11] = [ true, true, true, true, true, true, true, false, false, false, false, diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index c9392f35..d69e61b0 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -21,6 +21,7 @@ use crate::cdf; use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect}; use crate::msac::SymbolEncoder; +use crate::transform::TxType; use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16}; // --------------------------------------------------------------------------- @@ -1674,6 +1675,10 @@ pub struct Coeff { pub struct BlockCoeffs { /// The luma transform's coefficients. pub luma: Vec, + /// One transform type per luma transform unit, raster order (lane-txset). + /// Empty is `DCT_DCT` throughout -- what every block coded before the + /// search offered a type, and what an inter block still carries. + pub luma_tx_types: Vec, /// The U plane's. pub u: Vec, /// The V plane's. @@ -3994,6 +3999,7 @@ fn write_block( &grids[0], tx_mode, all_scans, + &block.luma_tx_types, )?; tx < side } else { @@ -4008,6 +4014,7 @@ fn write_block( &neighbours.around(at, side), tx_mode, tx_select, + block_tx_type(block), ); neighbours.record_planes(at, side, mode, grids, !split); clear_skip_band(neighbours, at_mi, side); @@ -4092,6 +4099,7 @@ fn write_leaf8( &grids[0], tx_mode, all_scans, + &block.luma_tx_types, )?; tx < 8 } else { @@ -4106,6 +4114,7 @@ fn write_leaf8( &neighbours.around_mi(leaf_mi, 8), tx_mode, tx_select, + block_tx_type(block), ); neighbours.record_mi_planes(leaf_mi, 8, grids, !split); clear_skip_band(neighbours, leaf_mi, 8); @@ -4127,6 +4136,9 @@ fn write_block_planes( // A block whose luma was already written as several transform units // ([`write_luma_select`]) has only its chroma planes left here. skip_luma: bool, + // This block's single luma transform type (lane-txset); chroma's is + // derived from its mode by the decoder and codes no symbol. + luma_tx_type: TxType, ) { for (plane, (grid, scan)) in grids.iter().zip(scans.iter()).enumerate() { if plane == 0 && skip_luma { @@ -4153,6 +4165,7 @@ fn write_block_planes( skip_ctx, dc_sign_ctx(around[plane].dc_vote), Some(plane), + if plane == 0 { luma_tx_type } else { TxType::DctDct }, ); #[cfg(test)] census_record( @@ -4173,6 +4186,12 @@ fn write_block_planes( } } +/// The single luma transform type a block carries, `DCT_DCT` for one that +/// never searched one (lane-txset). +fn block_tx_type(block: &BlockCoeffs) -> TxType { + block.luma_tx_types.first().copied().unwrap_or(TxType::DctDct) +} + /// The `(side / tx)^2` transform units of one block's luma residual, raster /// order, each with its own coefficient context and each published before the /// next one reads it (decode.rs `decode_block`'s multi-transform-unit branch). @@ -4192,6 +4211,10 @@ fn write_luma_tus( // lane-av1tx2 r2: an INTER block's split units read the inter coefficient // tables, not the intra ones (`TxbSet::Luma*Inter`). inter: bool, + // One transform type per unit in this same raster order (lane-txset). + // Empty -- what every caller that never searched a type passes -- is + // `DCT_DCT` throughout, the one type this writer used to code. + tx_types: &[TxType], ) -> Result<()> { if tx < side && side > 32 && tx < 32 { // A 64x64 block's own levels reach the writer as the 32x32 corner a @@ -4268,6 +4291,10 @@ fn write_luma_tus( skip_ctx, dc_sign_ctx(around.dc_vote), Some(0), + tx_types + .get(tu_row * n + tu_col) + .copied() + .unwrap_or(TxType::DctDct), ); #[cfg(test)] census_record( @@ -4308,6 +4335,7 @@ fn write_luma_select( grid: &[i32], mode: usize, scans: &[Vec; 4], + tx_types: &[TxType], ) -> Result { let max_tx = side.min(64); let ctx = neighbours.tx_size_ctx(at_mi, max_tx); @@ -4324,7 +4352,7 @@ fn write_luma_select( } } let tx = max_tx >> depth; - write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false)?; + write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false, tx_types)?; neighbours.record_tx(at_mi, side, tx); Ok(tx) } @@ -4697,6 +4725,19 @@ fn scan_of(side: usize) -> &'static Vec { } pub(crate) fn coeff_bits( + grid: &[i32], + set: TxbSet, + q_ctx: usize, + skip_ctx: usize, + sign_ctx: usize, +) -> f64 { + coeff_bits_typed(grid, set, q_ctx, skip_ctx, sign_ctx, TxType::DctDct) +} + +/// [`coeff_bits`] for levels a named transform type produced: the `tx_type` +/// symbol is part of what the block spends, so a search comparing types has +/// to see each one's own symbol priced (lane-txset). +pub(crate) fn coeff_bits_typed( grid: &[i32], set: TxbSet, q_ctx: usize, @@ -4708,6 +4749,7 @@ pub(crate) fn coeff_bits( // largest remaining pricer error for every other block. skip_ctx: usize, sign_ctx: usize, + tx_type: TxType, ) -> f64 { // `Cdfs::new` builds every table a tile writer adapts -- tens of // kilobytes -- and this function is called once per priced candidate: @@ -4743,7 +4785,7 @@ pub(crate) fn coeff_bits( } let scan = scan_of(coding.side); let mut enc = SymbolEncoder::pricer(); - write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None); + write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None, tx_type); enc.bits() }; // lane-av1speed2: 60% of the transform units of an inter stream hold no @@ -4842,6 +4884,7 @@ pub(crate) fn rdoq( skip_ctx: usize, sign_ctx: usize, lambda: f64, + tx_type: TxType, ) -> f64 { // A 64-point transform carries its top-left 32x32 alone, and that corner // is what the writer and the pricer take (`coded_corner`). @@ -4857,16 +4900,17 @@ pub(crate) fn rdoq( q_ctx: usize, skip_ctx: usize, sign_ctx: usize, + tx_type: TxType, ) -> f64 { if dense.is_empty() { - return coeff_bits(levels, set, q_ctx, skip_ctx, sign_ctx); + return coeff_bits_typed(levels, set, q_ctx, skip_ctx, sign_ctx, tx_type); } for row in 0..coded { dense[row * coded..][..coded].copy_from_slice(&levels[row * side..][..coded]); } - coeff_bits(dense, set, q_ctx, skip_ctx, sign_ctx) + coeff_bits_typed(dense, set, q_ctx, skip_ctx, sign_ctx, tx_type) } - let mut bits = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx); + let mut bits = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type); // The DC has its own quantiser, so its squared error per unit of level is // `(dc_q / ac_q)^2` of an AC coefficient's. let dc = f64::from(crate::quant::dc_q(8, i32::from(base_q_idx))); @@ -4905,7 +4949,7 @@ pub(crate) fn rdoq( let candidate = level - level.signum(); levels[i] = candidate; budget -= 1; - let after = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx); + let after = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type); let distortion = weight * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2)); if distortion + lambda * (after - bits) < 0.0 { @@ -5177,6 +5221,10 @@ fn write_coeffs( skip_ctx: usize, sign_ctx: usize, plane: Option, + // Which transform type these levels were produced with (lane-txset). + // Only a luma set whose alphabet holds more than one type codes it; + // chroma never codes a `tx_type` symbol at all. + tx_type: TxType, ) { if let Some(plane) = plane { ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); @@ -5212,8 +5260,18 @@ fn write_coeffs( // A luma transform whose type set holds more than one type codes which it // is, right after the all-zero flag (spec 5.11.39). The writer only ever // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`. - if let Some(tx_type) = coding.tx_type.as_deref_mut() { - enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); + if let Some(cdf) = coding.tx_type.as_deref_mut() { + // The symbol comes from the DECODER's own map, keyed by this set's + // alphabet width, so a type this set cannot express is caught here + // rather than desyncing the tile. The search only ever offers types + // the set holds ([`crate::encode::tx_type_candidates`]). + let symbol = crate::decode::tx_type_symbol(cdf.len(), tx_type); + debug_assert!( + symbol.is_some(), + "{tx_type:?} is not in the {}-symbol tx_type set", + cdf.len() - 1 + ); + enc.symbol(symbol.unwrap_or(TX_TYPE_DCT_DCT_SET2), cdf); if plane.is_some() { ec_rng_trace(|| format!("EC_TXTYPE tell={} rng={}", enc.tell(), enc.rng())); } @@ -6242,6 +6300,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( 0, &all_scans, true, + &block.luma_tx_types, )?; } write_block_planes( @@ -6253,6 +6312,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( &neighbours.around(sb_at, SB), 0, split, + block_tx_type(block), ); neighbours.record_planes(sb_at, SB, 0, &grids, !split); } @@ -6690,6 +6750,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( mode_for_tx, &all_scans, is_inter, + &block.luma_tx_types, )?; } write_block_planes( @@ -6705,6 +6766,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( &neighbours.around(at, BLOCK), mode_for_tx, split, + block_tx_type(block), ); neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); } @@ -6929,6 +6991,7 @@ fn write_inter_frame_leaf( if split { write_luma_tus( enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter, + &block.luma_tx_types, )?; } write_block_planes( @@ -6944,6 +7007,7 @@ fn write_inter_frame_leaf( &neighbours.around(at, SUB), mode_for_tx, split, + block_tx_type(block), ); neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); } @@ -7184,6 +7248,7 @@ fn write_inter_frame_leaf8( if split { write_luma_tus( enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter, + &block.luma_tx_types, )?; } write_block_planes( @@ -7195,6 +7260,7 @@ fn write_inter_frame_leaf8( &neighbours.around_mi(leaf_mi, 8), mode_for_tx, split, + block_tx_type(block), ); neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index e6c056bf..bef434f3 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1483,11 +1483,25 @@ pub fn forward_transform_2d_typed(residual: &[i32], side: usize, tx_type: TxType if tx_type == TxType::DctDct { return forward_transform_2d(residual, side); } - debug_assert_eq!( - tx_type.flip(), - (false, false), - "a flipped ADST needs its residual mirrored before this basis applies" - ); + // A FLIPADST axis is the plain ADST kernel with the DECODER's output + // mirrored (`dequant_and_inverse_typed_wh`'s `ud_flip`/`lr_flip`: row + // `h - 1 - i`, column `w - 1 - j`). A mirror is its own inverse, so the + // analysis half is the same plain basis over the mirrored residual -- + // lane-txset, which is what lets every FLIPADST-bearing type share the + // kernels below instead of needing its own. + let (ud_flip, lr_flip) = tx_type.flip(); + let mirrored: Vec = if ud_flip || lr_flip { + (0..side) + .flat_map(|row| { + let src = if ud_flip { side - 1 - row } else { row }; + (0..side).map(move |col| (src, if lr_flip { side - 1 - col } else { col })) + }) + .map(|(r, c)| residual[r * side + c]) + .collect() + } else { + Vec::new() + }; + let residual: &[i32] = if mirrored.is_empty() { residual } else { &mirrored }; let (row_kind, col_kind) = tx_type.axes(); let (hb, vb) = (axis_basis_t(side, row_kind), axis_basis_t(side, col_kind)); let mut rows = vec![0.0f64; side * side]; @@ -2162,7 +2176,27 @@ mod tests { (sum / (side * side) as f64).sqrt() }; let dct = rmse(TxType::DctDct); - for tx in [TxType::AdstDct, TxType::DctAdst, TxType::AdstAdst] { + // lane-txset: every one of the sixteen types, not just the four + // the reduced set names -- the FLIPADST and 1D-ADST/DCT families + // reach the forward side through the mirrored residual, and a + // mirror on the wrong axis reads exactly as a blown-up rmse here. + for tx in [ + TxType::AdstDct, + TxType::DctAdst, + TxType::AdstAdst, + TxType::Idtx, + TxType::VDct, + TxType::HDct, + TxType::VAdst, + TxType::HAdst, + TxType::VFlipAdst, + TxType::HFlipAdst, + TxType::FlipAdstDct, + TxType::DctFlipAdst, + TxType::FlipAdstFlipAdst, + TxType::AdstFlipAdst, + TxType::FlipAdstAdst, + ] { let e = rmse(tx); assert!( e < 2.0 * dct,