forge
Explore
Log in

Don't you dare go Hollow

Search an intra luma transform unit's own tx_type

Browse files at this commit

2fd2ac4f3565bc832ab1230e8c904cb24eafaa41Ahmet Kaan GümüşParent: ad81365
  • feat
    • search an intra luma transform unit's own tx_type
Show full message
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.rsmodified+8 -0
@@ -5988,6 +5988,14 @@ pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option<TxType> {
59885988 }
59895989 }
59905990
5991+/// [`tx_type_from_symbol`] inverted: which symbol of a `cdf_len`-wide
5992+/// `tx_type` CDF names `tx_type`, or `None` when that set does not hold it
5993+/// (class `table-and-reader-move-together` -- the writer picks its symbol
5994+/// through the reader's own map, so the two cannot drift).
5995+pub(crate) fn tx_type_symbol(cdf_len: usize, tx_type: TxType) -> Option<usize> {
5996+ (0..cdf_len.saturating_sub(1)).find(|&t| tx_type_from_symbol(cdf_len, t) == Some(tx_type))
5997+}
5998+
59915999 /// The remainder of a level the base and base-range syntax could not reach
59926000 /// (spec 5.11.40): its bit length in unary, then that many of its own bits,
59936001 /// most significant first — the exact inverse of [`crate::tile::write_golomb`].
crates/ec-av1/src/encode.rsmodified+118 -9
@@ -252,6 +252,40 @@ fn tx_select_inter() -> bool {
252252 ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_SELECT_INTER))
253253 }
254254
255+/// The transform types an intra luma transform unit of `set` may be coded
256+/// with -- exactly the alphabet the writer's `tx_type` symbol names for that
257+/// set, so a searched type is always one the tile can express (lane-txset).
258+/// `DCT_DCT` first: it is the incumbent every other candidate has to beat.
259+///
260+/// Under this encoder's `reduced_tx_set` header bit every intra set at 16x16
261+/// and below is `TX_SET_INTRA_2` (spec `get_tx_set`), the five types
262+/// [`TxType::from_symbol`] names; 32x32 and 64x64 luma code no symbol at all,
263+/// and inter luma reads the two-type `TX_SET_INTER_3`, left at `DCT_DCT`
264+/// here.
265+pub(crate) fn tx_type_candidates(set: TxbSet) -> &'static [TxType] {
266+ const DCT: [TxType; 1] = [TxType::DctDct];
267+ const INTRA2: [TxType; 5] = [
268+ TxType::DctDct,
269+ TxType::AdstAdst,
270+ TxType::AdstDct,
271+ TxType::DctAdst,
272+ TxType::Idtx,
273+ ];
274+ match set {
275+ TxbSet::Luma16 | TxbSet::Luma8 | TxbSet::Luma4 if tx_type_search() => &INTRA2,
276+ _ => &DCT,
277+ }
278+}
279+
280+fn tx_type_search() -> bool {
281+ static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| {
282+ crate::envflags::var("EC_AV1_TXSET")
283+ .ok()
284+ .map(|v| !matches!(v.as_str(), "0" | "off"))
285+ });
286+ ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_TYPE_SEARCH))
287+}
288+
255289 fn tx_select() -> bool {
256290 static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| {
257291 crate::envflags::var("EC_AV1_TX_SELECT")
@@ -2128,6 +2162,7 @@ impl Plane<'_> {
21282162 let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set);
21292163 let bits = crate::tile::rdoq(
21302164 &mut levels, &scaled, side, base_q_idx, at.set, skip_ctx, sign_ctx, rdoq_lambda(),
2165+ tx_type,
21312166 );
21322167 (levels, Some(bits))
21332168 } else {
@@ -2182,12 +2217,13 @@ impl Plane<'_> {
21822217 } else {
21832218 {
21842219 let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set);
2185- crate::tile::coeff_bits(
2220+ crate::tile::coeff_bits_typed(
21862221 &levels,
21872222 at.set,
21882223 crate::decode::q_ctx_of(base_q_idx),
21892224 skip_ctx,
21902225 sign_ctx,
2226+ tx_type,
21912227 )
21922228 }
21932229 };
@@ -2258,6 +2294,7 @@ impl Plane<'_> {
22582294 let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, set);
22592295 let bits = crate::tile::rdoq(
22602296 &mut levels, &scaled, side, base_q_idx, set, skip_ctx, sign_ctx, rdoq_lambda(),
2297+ TxType::DctDct,
22612298 );
22622299 (levels, Some(bits))
22632300 } else {
@@ -2353,7 +2390,7 @@ impl Plane<'_> {
23532390 // `filter_intra`), not once over the whole block.
23542391 filter_intra: Option<u8>,
23552392 search: &Search, fctx: &crate::decode::FrameCtx,
2356- ) -> (Vec<i32>, f64, f64) {
2393+ ) -> (Vec<i32>, f64, f64, Vec<TxType>) {
23572394 let At { x, y, side, reach, .. } = at;
23582395 let tx = side >> depth;
23592396 let n = 1usize << depth;
@@ -2369,6 +2406,11 @@ impl Plane<'_> {
23692406 // for its `txb_skip` context, not context 0 ([`Plane::coef_ctx`]).
23702407 self.ctx.tu_split = n > 1;
23712408 let (mut sse, mut bits) = (0.0, 0.0);
2409+ // One entry per unit at its own `tu_row * n + tu_col`, which is how
2410+ // the writer indexes them (tile.rs `write_luma_tus`) -- a unit the
2411+ // frame edge cuts away is never coded there, so a compacted list
2412+ // would slide every later unit's type onto the wrong transform.
2413+ let mut tx_types = vec![TxType::DctDct; n * n];
23722414 for tu_row in 0..n {
23732415 for tu_col in 0..n {
23742416 let (col_off, row_off) = (tu_col * tx, tu_row * tx);
@@ -2406,7 +2448,27 @@ impl Plane<'_> {
24062448 )
24072449 }
24082450 None => {
2409- self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx)
2451+ // lane-txset: the unit picks its own transform type
2452+ // out of the set the writer can name it in, priced
2453+ // through the same RDOQ'd trial path (its `bits`
2454+ // already carry the `tx_type` symbol itself).
2455+ let mut best: Option<(f64, TxType, Trial)> = None;
2456+ for &tx_type in tx_type_candidates(set) {
2457+ let candidate = self.trial_typed(
2458+ tu, mode, angle_delta, search.base_q_idx, search.deadzone,
2459+ tx_type, fctx,
2460+ );
2461+ let cost = candidate.sse + search.lambda * candidate.bits;
2462+ if best.as_ref().is_none_or(|(best, _, _)| cost < *best) {
2463+ best = Some((cost, tx_type, candidate));
2464+ }
2465+ }
2466+ let (_, tx_type, trial) =
2467+ best.expect("the candidate list is never empty");
2468+ tx_types[tu_row * n + tu_col] = tx_type;
2469+ TX_TYPE_HITS[tx_type as usize]
2470+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2471+ trial
24102472 }
24112473 };
24122474 sse += trial.sse;
@@ -2419,7 +2481,7 @@ impl Plane<'_> {
24192481 }
24202482 }
24212483 self.ctx.tu_split = false;
2422- (levels, sse, bits)
2484+ (levels, sse, bits, tx_types)
24232485 }
24242486
24252487 /// Writes a trial's reconstruction back into the plane, and the
@@ -2637,7 +2699,7 @@ impl Plane<'_> {
26372699 at: At,
26382700 search: &Search,
26392701 mode_bits: &[f64; 13], fctx: &crate::decode::FrameCtx,
2640- ) -> (Vec<Coeff>, u8, i32, f64) {
2702+ ) -> (Vec<Coeff>, u8, i32, f64, TxType) {
26412703 let At {
26422704 x, y, side, reach, ..
26432705 } = at;
@@ -2777,6 +2839,23 @@ impl Plane<'_> {
27772839 ANGLE_DELTA_HITS[(crate::tile::ANGLE_DELTA_ZERO as i32 + angle_delta) as usize]
27782840 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
27792841 }
2842+ // lane-txset: the block's own transform type, searched on the mode
2843+ // and angle the trials above settled -- the same ordering the angle
2844+ // refinement uses, and the same RD terms.
2845+ let mut tx_type = TxType::DctDct;
2846+ for &candidate_type in tx_type_candidates(at.set).iter().skip(1) {
2847+ let candidate = self.trial_typed(
2848+ at, mode, angle_delta, search.base_q_idx, search.deadzone, candidate_type, fctx,
2849+ );
2850+ let cost_t =
2851+ candidate.sse + search.lambda * (candidate.bits + mode_bits[usize::from(mode)]);
2852+ if cost_t < cost {
2853+ cost = cost_t;
2854+ tx_type = candidate_type;
2855+ trial = candidate;
2856+ }
2857+ }
2858+ TX_TYPE_HITS[tx_type as usize].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
27802859 if census_on() {
27812860 LUMA_RANK[sad_rank(&ranking, mode).min(12)]
27822861 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -2784,7 +2863,7 @@ impl Plane<'_> {
27842863 self.commit(x, y, side, &trial);
27852864 // A 64-point transform codes only its top-left 32x32 corner, which is
27862865 // the grid `code_from_prediction` hands back (lane-i64).
2787- (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost)
2866+ (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost, tx_type)
27882867 }
27892868 }
27902869
@@ -3586,6 +3665,18 @@ fn tx_depth_bits(side: usize, depth: usize) -> f64 {
35863665 }
35873666 }
35883667
3668+/// How many luma transform units resolved to each [`TxType`], indexed by the
3669+/// enum's own discriminant (lane-txset): the fire count a witness asserts on,
3670+/// and the instrument that says which types the search actually reaches.
3671+pub(crate) static TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] =
3672+ [const { std::sync::atomic::AtomicUsize::new(0) }; 16];
3673+
3674+/// Reads [`TX_TYPE_HITS`] and zeroes it, so a gate or a test attributes the
3675+/// counts to its own encode.
3676+pub(crate) fn take_tx_type_hits() -> [usize; 16] {
3677+ std::array::from_fn(|t| TX_TYPE_HITS[t].swap(0, std::sync::atomic::Ordering::Relaxed))
3678+}
3679+
35893680 /// How many blocks resolved to each `tx_depth`, the fire count that says
35903681 /// whether the depth search below reaches its candidates at all.
35913682 static TX_DEPTH_HITS: [std::sync::atomic::AtomicUsize; 3] =
@@ -4377,8 +4468,9 @@ fn code_square(
43774468 reach: Reach::of(side, x, y, luma.true_width, luma.true_height, fctx),
43784469 set: luma_set,
43794470 };
4380- let (mut luma_coeffs, mode, angle_delta, mut cost) =
4471+ let (mut luma_coeffs, mode, angle_delta, mut cost, tx_type) =
43814472 luma.search_block(at, search, mode_bits, fctx);
4473+ let mut luma_tx_types = vec![tx_type];
43824474 // The depth search runs on the mode the whole-block transform picked --
43834475 // libaom's own ordering, and the reason it costs one extra pass per depth
43844476 // rather than one per (mode, depth) pair.
@@ -4398,15 +4490,19 @@ fn code_square(
43984490 luma.snapshot(x, y, side),
43994491 luma_coeffs,
44004492 );
4493+ let mut best_types = std::mem::take(&mut luma_tx_types);
44014494 for depth in 1..=max_tx_depth(side) {
4402- let (levels, sse, bits) = luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);
4495+ let (levels, sse, bits, types) =
4496+ luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);
44034497 let cost_d = sse
44044498 + search.lambda
44054499 * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth));
44064500 if cost_d < best.0 {
44074501 best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side));
4502+ best_types = types;
44084503 }
44094504 }
4505+ luma_tx_types = best_types;
44104506 luma.restore(x, y, side, &best.2);
44114507 cost = best.0;
44124508 tx_depth = best.1 as u8;
@@ -4503,7 +4599,10 @@ fn code_square(
45034599 let mut best: Option<(f64, u8, usize, (Vec<u8>, Vec<CoefCtx>), Vec<Coeff>)> = None;
45044600 for fi in 0..5u8 {
45054601 for depth in 0..=max_depth {
4506- let (levels, sse, bits) =
4602+ // A filter-intra block's units are coded through
4603+ // `code_from_prediction`, which is `DCT_DCT` throughout, so
4604+ // this arm keeps the empty (all-`DCT_DCT`) type list.
4605+ let (levels, sse, bits, _) =
45074606 luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx);
45084607 let cost_f = sse
45094608 + search.lambda
@@ -4564,6 +4663,12 @@ fn code_square(
45644663 }
45654664 }
45664665 }
4666+ // A palette or filter-intra block's luma was recoded through `DCT_DCT`
4667+ // (its levels came from `code_from_prediction`), so the searched types
4668+ // belong to a block this one no longer is.
4669+ if palette.is_some() || filter_intra.is_some() {
4670+ luma_tx_types = Vec::new();
4671+ }
45674672 (
45684673 BlockCoeffs {
45694674 u,
@@ -4577,6 +4682,7 @@ fn code_square(
45774682 filter_intra,
45784683 cfl_alphas,
45794684 angle_delta_y: angle_delta as i8,
4685+ luma_tx_types,
45804686 ..BlockCoeffs::default()
45814687 },
45824688 cost,
@@ -5846,6 +5952,7 @@ fn code_square_inter(
58465952 palette: None,
58475953 palette_uv: None,
58485954 tx_depth,
5955+ luma_tx_types: Vec::new(),
58495956 inter: Some(info),
58505957 motion_mode: motion_won,
58515958 },
@@ -5868,6 +5975,7 @@ fn code_square_inter(
58685975 chroma[0].commit(x / 2, y / 2, side / 2, &u);
58695976 chroma[1].commit(x / 2, y / 2, side / 2, &v);
58705977 (BlockCoeffs {
5978+ luma_tx_types: Vec::new(),
58715979 angle_delta_y: 0,
58725980 cfl_alphas: None,
58735981 filter_intra: None,
@@ -9116,6 +9224,7 @@ fn search_inter_block(
91169224 chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v);
91179225 (
91189226 BlockCoeffs {
9227+ luma_tx_types: Vec::new(),
91199228 angle_delta_y: 0,
91209229 cfl_alphas: None,
91219230 filter_intra: None,
crates/ec-av1/src/speed.rsmodified+8 -0
@@ -437,6 +437,14 @@ pub(crate) const RESTORATION: [bool; 11] = [
437437 /// `encode::TPL_DEPTH`: lookahead pictures the lambda map reads (1 = off).
438438 pub(crate) const TPL_DEPTH: [usize; 11] = [crate::encode::TPL_DEPTH, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
439439
440+/// `encode::tx_type_search`: whether an intra luma transform unit searches
441+/// its own `tx_type` instead of coding `DCT_DCT` (lane-txset). The five-type
442+/// reduced-set alphabet the writer already codes is what it picks from, so a
443+/// preset that turns it off is byte-identical to the encoder before the lane.
444+pub(crate) const TX_TYPE_SEARCH: [bool; 11] = [
445+ true, true, true, true, true, true, true, false, false, false, false,
446+];
447+
440448 /// `filter_search`: whether the deblock ladder's +-1/+-2 refinement stage runs.
441449 pub(crate) const DEBLOCK_REFINE: [bool; 11] = [
442450 true, true, true, true, true, true, true, false, false, false, false,
crates/ec-av1/src/tile.rsmodified+74 -8
@@ -21,6 +21,7 @@ use crate::cdf;
2121 use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables};
2222 use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect};
2323 use crate::msac::SymbolEncoder;
24+use crate::transform::TxType;
2425 use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16};
2526
2627 // ---------------------------------------------------------------------------
@@ -1674,6 +1675,10 @@ pub struct Coeff {
16741675 pub struct BlockCoeffs {
16751676 /// The luma transform's coefficients.
16761677 pub luma: Vec<Coeff>,
1678+ /// One transform type per luma transform unit, raster order (lane-txset).
1679+ /// Empty is `DCT_DCT` throughout -- what every block coded before the
1680+ /// search offered a type, and what an inter block still carries.
1681+ pub luma_tx_types: Vec<TxType>,
16771682 /// The U plane's.
16781683 pub u: Vec<Coeff>,
16791684 /// The V plane's.
@@ -3994,6 +3999,7 @@ fn write_block(
39943999 &grids[0],
39954000 tx_mode,
39964001 all_scans,
4002+ &block.luma_tx_types,
39974003 )?;
39984004 tx < side
39994005 } else {
@@ -4008,6 +4014,7 @@ fn write_block(
40084014 &neighbours.around(at, side),
40094015 tx_mode,
40104016 tx_select,
4017+ block_tx_type(block),
40114018 );
40124019 neighbours.record_planes(at, side, mode, grids, !split);
40134020 clear_skip_band(neighbours, at_mi, side);
@@ -4092,6 +4099,7 @@ fn write_leaf8(
40924099 &grids[0],
40934100 tx_mode,
40944101 all_scans,
4102+ &block.luma_tx_types,
40954103 )?;
40964104 tx < 8
40974105 } else {
@@ -4106,6 +4114,7 @@ fn write_leaf8(
41064114 &neighbours.around_mi(leaf_mi, 8),
41074115 tx_mode,
41084116 tx_select,
4117+ block_tx_type(block),
41094118 );
41104119 neighbours.record_mi_planes(leaf_mi, 8, grids, !split);
41114120 clear_skip_band(neighbours, leaf_mi, 8);
@@ -4127,6 +4136,9 @@ fn write_block_planes(
41274136 // A block whose luma was already written as several transform units
41284137 // ([`write_luma_select`]) has only its chroma planes left here.
41294138 skip_luma: bool,
4139+ // This block's single luma transform type (lane-txset); chroma's is
4140+ // derived from its mode by the decoder and codes no symbol.
4141+ luma_tx_type: TxType,
41304142 ) {
41314143 for (plane, (grid, scan)) in grids.iter().zip(scans.iter()).enumerate() {
41324144 if plane == 0 && skip_luma {
@@ -4153,6 +4165,7 @@ fn write_block_planes(
41534165 skip_ctx,
41544166 dc_sign_ctx(around[plane].dc_vote),
41554167 Some(plane),
4168+ if plane == 0 { luma_tx_type } else { TxType::DctDct },
41564169 );
41574170 #[cfg(test)]
41584171 census_record(
@@ -4173,6 +4186,12 @@ fn write_block_planes(
41734186 }
41744187 }
41754188
4189+/// The single luma transform type a block carries, `DCT_DCT` for one that
4190+/// never searched one (lane-txset).
4191+fn block_tx_type(block: &BlockCoeffs) -> TxType {
4192+ block.luma_tx_types.first().copied().unwrap_or(TxType::DctDct)
4193+}
4194+
41764195 /// The `(side / tx)^2` transform units of one block's luma residual, raster
41774196 /// order, each with its own coefficient context and each published before the
41784197 /// next one reads it (decode.rs `decode_block`'s multi-transform-unit branch).
@@ -4192,6 +4211,10 @@ fn write_luma_tus(
41924211 // lane-av1tx2 r2: an INTER block's split units read the inter coefficient
41934212 // tables, not the intra ones (`TxbSet::Luma*Inter`).
41944213 inter: bool,
4214+ // One transform type per unit in this same raster order (lane-txset).
4215+ // Empty -- what every caller that never searched a type passes -- is
4216+ // `DCT_DCT` throughout, the one type this writer used to code.
4217+ tx_types: &[TxType],
41954218 ) -> Result<()> {
41964219 if tx < side && side > 32 && tx < 32 {
41974220 // A 64x64 block's own levels reach the writer as the 32x32 corner a
@@ -4268,6 +4291,10 @@ fn write_luma_tus(
42684291 skip_ctx,
42694292 dc_sign_ctx(around.dc_vote),
42704293 Some(0),
4294+ tx_types
4295+ .get(tu_row * n + tu_col)
4296+ .copied()
4297+ .unwrap_or(TxType::DctDct),
42714298 );
42724299 #[cfg(test)]
42734300 census_record(
@@ -4308,6 +4335,7 @@ fn write_luma_select(
43084335 grid: &[i32],
43094336 mode: usize,
43104337 scans: &[Vec<u16>; 4],
4338+ tx_types: &[TxType],
43114339 ) -> Result<usize> {
43124340 let max_tx = side.min(64);
43134341 let ctx = neighbours.tx_size_ctx(at_mi, max_tx);
@@ -4324,7 +4352,7 @@ fn write_luma_select(
43244352 }
43254353 }
43264354 let tx = max_tx >> depth;
4327- write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false)?;
4355+ write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false, tx_types)?;
43284356 neighbours.record_tx(at_mi, side, tx);
43294357 Ok(tx)
43304358 }
@@ -4697,6 +4725,19 @@ fn scan_of(side: usize) -> &'static Vec<u16> {
46974725 }
46984726
46994727 pub(crate) fn coeff_bits(
4728+ grid: &[i32],
4729+ set: TxbSet,
4730+ q_ctx: usize,
4731+ skip_ctx: usize,
4732+ sign_ctx: usize,
4733+) -> f64 {
4734+ coeff_bits_typed(grid, set, q_ctx, skip_ctx, sign_ctx, TxType::DctDct)
4735+}
4736+
4737+/// [`coeff_bits`] for levels a named transform type produced: the `tx_type`
4738+/// symbol is part of what the block spends, so a search comparing types has
4739+/// to see each one's own symbol priced (lane-txset).
4740+pub(crate) fn coeff_bits_typed(
47004741 grid: &[i32],
47014742 set: TxbSet,
47024743 q_ctx: usize,
@@ -4708,6 +4749,7 @@ pub(crate) fn coeff_bits(
47084749 // largest remaining pricer error for every other block.
47094750 skip_ctx: usize,
47104751 sign_ctx: usize,
4752+ tx_type: TxType,
47114753 ) -> f64 {
47124754 // `Cdfs::new` builds every table a tile writer adapts -- tens of
47134755 // kilobytes -- and this function is called once per priced candidate:
@@ -4743,7 +4785,7 @@ pub(crate) fn coeff_bits(
47434785 }
47444786 let scan = scan_of(coding.side);
47454787 let mut enc = SymbolEncoder::pricer();
4746- write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None);
4788+ write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None, tx_type);
47474789 enc.bits()
47484790 };
47494791 // lane-av1speed2: 60% of the transform units of an inter stream hold no
@@ -4842,6 +4884,7 @@ pub(crate) fn rdoq(
48424884 skip_ctx: usize,
48434885 sign_ctx: usize,
48444886 lambda: f64,
4887+ tx_type: TxType,
48454888 ) -> f64 {
48464889 // A 64-point transform carries its top-left 32x32 alone, and that corner
48474890 // is what the writer and the pricer take (`coded_corner`).
@@ -4857,16 +4900,17 @@ pub(crate) fn rdoq(
48574900 q_ctx: usize,
48584901 skip_ctx: usize,
48594902 sign_ctx: usize,
4903+ tx_type: TxType,
48604904 ) -> f64 {
48614905 if dense.is_empty() {
4862- return coeff_bits(levels, set, q_ctx, skip_ctx, sign_ctx);
4906+ return coeff_bits_typed(levels, set, q_ctx, skip_ctx, sign_ctx, tx_type);
48634907 }
48644908 for row in 0..coded {
48654909 dense[row * coded..][..coded].copy_from_slice(&levels[row * side..][..coded]);
48664910 }
4867- coeff_bits(dense, set, q_ctx, skip_ctx, sign_ctx)
4911+ coeff_bits_typed(dense, set, q_ctx, skip_ctx, sign_ctx, tx_type)
48684912 }
4869- let mut bits = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx);
4913+ let mut bits = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type);
48704914 // The DC has its own quantiser, so its squared error per unit of level is
48714915 // `(dc_q / ac_q)^2` of an AC coefficient's.
48724916 let dc = f64::from(crate::quant::dc_q(8, i32::from(base_q_idx)));
@@ -4905,7 +4949,7 @@ pub(crate) fn rdoq(
49054949 let candidate = level - level.signum();
49064950 levels[i] = candidate;
49074951 budget -= 1;
4908- let after = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx);
4952+ let after = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type);
49094953 let distortion = weight
49104954 * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2));
49114955 if distortion + lambda * (after - bits) < 0.0 {
@@ -5177,6 +5221,10 @@ fn write_coeffs(
51775221 skip_ctx: usize,
51785222 sign_ctx: usize,
51795223 plane: Option<usize>,
5224+ // Which transform type these levels were produced with (lane-txset).
5225+ // Only a luma set whose alphabet holds more than one type codes it;
5226+ // chroma never codes a `tx_type` symbol at all.
5227+ tx_type: TxType,
51805228 ) {
51815229 if let Some(plane) = plane {
51825230 ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell()));
@@ -5212,8 +5260,18 @@ fn write_coeffs(
52125260 // A luma transform whose type set holds more than one type codes which it
52135261 // is, right after the all-zero flag (spec 5.11.39). The writer only ever
52145262 // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`.
5215- if let Some(tx_type) = coding.tx_type.as_deref_mut() {
5216- enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type);
5263+ if let Some(cdf) = coding.tx_type.as_deref_mut() {
5264+ // The symbol comes from the DECODER's own map, keyed by this set's
5265+ // alphabet width, so a type this set cannot express is caught here
5266+ // rather than desyncing the tile. The search only ever offers types
5267+ // the set holds ([`crate::encode::tx_type_candidates`]).
5268+ let symbol = crate::decode::tx_type_symbol(cdf.len(), tx_type);
5269+ debug_assert!(
5270+ symbol.is_some(),
5271+ "{tx_type:?} is not in the {}-symbol tx_type set",
5272+ cdf.len() - 1
5273+ );
5274+ enc.symbol(symbol.unwrap_or(TX_TYPE_DCT_DCT_SET2), cdf);
52175275 if plane.is_some() {
52185276 ec_rng_trace(|| format!("EC_TXTYPE tell={} rng={}", enc.tell(), enc.rng()));
52195277 }
@@ -6242,6 +6300,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
62426300 0,
62436301 &all_scans,
62446302 true,
6303+ &block.luma_tx_types,
62456304 )?;
62466305 }
62476306 write_block_planes(
@@ -6253,6 +6312,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
62536312 &neighbours.around(sb_at, SB),
62546313 0,
62556314 split,
6315+ block_tx_type(block),
62566316 );
62576317 neighbours.record_planes(sb_at, SB, 0, &grids, !split);
62586318 }
@@ -6690,6 +6750,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
66906750 mode_for_tx,
66916751 &all_scans,
66926752 is_inter,
6753+ &block.luma_tx_types,
66936754 )?;
66946755 }
66956756 write_block_planes(
@@ -6705,6 +6766,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
67056766 &neighbours.around(at, BLOCK),
67066767 mode_for_tx,
67076768 split,
6769+ block_tx_type(block),
67086770 );
67096771 neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split);
67106772 }
@@ -6929,6 +6991,7 @@ fn write_inter_frame_leaf(
69296991 if split {
69306992 write_luma_tus(
69316993 enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter,
6994+ &block.luma_tx_types,
69326995 )?;
69336996 }
69346997 write_block_planes(
@@ -6944,6 +7007,7 @@ fn write_inter_frame_leaf(
69447007 &neighbours.around(at, SUB),
69457008 mode_for_tx,
69467009 split,
7010+ block_tx_type(block),
69477011 );
69487012 neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split);
69497013 }
@@ -7184,6 +7248,7 @@ fn write_inter_frame_leaf8(
71847248 if split {
71857249 write_luma_tus(
71867250 enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter,
7251+ &block.luma_tx_types,
71877252 )?;
71887253 }
71897254 write_block_planes(
@@ -7195,6 +7260,7 @@ fn write_inter_frame_leaf8(
71957260 &neighbours.around_mi(leaf_mi, 8),
71967261 mode_for_tx,
71977262 split,
7263+ block_tx_type(block),
71987264 );
71997265 neighbours.record_mi_planes(leaf_mi, 8, &grids, !split);
72007266 neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block));
crates/ec-av1/src/transform.rsmodified+40 -6
@@ -1483,11 +1483,25 @@ pub fn forward_transform_2d_typed(residual: &[i32], side: usize, tx_type: TxType
14831483 if tx_type == TxType::DctDct {
14841484 return forward_transform_2d(residual, side);
14851485 }
1486- debug_assert_eq!(
1487- tx_type.flip(),
1488- (false, false),
1489- "a flipped ADST needs its residual mirrored before this basis applies"
1490- );
1486+ // A FLIPADST axis is the plain ADST kernel with the DECODER's output
1487+ // mirrored (`dequant_and_inverse_typed_wh`'s `ud_flip`/`lr_flip`: row
1488+ // `h - 1 - i`, column `w - 1 - j`). A mirror is its own inverse, so the
1489+ // analysis half is the same plain basis over the mirrored residual --
1490+ // lane-txset, which is what lets every FLIPADST-bearing type share the
1491+ // kernels below instead of needing its own.
1492+ let (ud_flip, lr_flip) = tx_type.flip();
1493+ let mirrored: Vec<i32> = if ud_flip || lr_flip {
1494+ (0..side)
1495+ .flat_map(|row| {
1496+ let src = if ud_flip { side - 1 - row } else { row };
1497+ (0..side).map(move |col| (src, if lr_flip { side - 1 - col } else { col }))
1498+ })
1499+ .map(|(r, c)| residual[r * side + c])
1500+ .collect()
1501+ } else {
1502+ Vec::new()
1503+ };
1504+ let residual: &[i32] = if mirrored.is_empty() { residual } else { &mirrored };
14911505 let (row_kind, col_kind) = tx_type.axes();
14921506 let (hb, vb) = (axis_basis_t(side, row_kind), axis_basis_t(side, col_kind));
14931507 let mut rows = vec![0.0f64; side * side];
@@ -2162,7 +2176,27 @@ mod tests {
21622176 (sum / (side * side) as f64).sqrt()
21632177 };
21642178 let dct = rmse(TxType::DctDct);
2165- for tx in [TxType::AdstDct, TxType::DctAdst, TxType::AdstAdst] {
2179+ // lane-txset: every one of the sixteen types, not just the four
2180+ // the reduced set names -- the FLIPADST and 1D-ADST/DCT families
2181+ // reach the forward side through the mirrored residual, and a
2182+ // mirror on the wrong axis reads exactly as a blown-up rmse here.
2183+ for tx in [
2184+ TxType::AdstDct,
2185+ TxType::DctAdst,
2186+ TxType::AdstAdst,
2187+ TxType::Idtx,
2188+ TxType::VDct,
2189+ TxType::HDct,
2190+ TxType::VAdst,
2191+ TxType::HAdst,
2192+ TxType::VFlipAdst,
2193+ TxType::HFlipAdst,
2194+ TxType::FlipAdstDct,
2195+ TxType::DctFlipAdst,
2196+ TxType::FlipAdstFlipAdst,
2197+ TxType::AdstFlipAdst,
2198+ TxType::FlipAdstAdst,
2199+ ] {
21662200 let e = rmse(tx);
21672201 assert!(
21682202 e < 2.0 * dct,