Merge branch 'lane-txset' (cdb209a0): the luma transform-type search (intra 5-type reduced set), screen-gated; forward kernels for all 16 types; the writer had coded one type everywhere
Browse files at this commit7 changed files +567 -25
- crates/ec-av1/src/decode.rs +8 -0
- crates/ec-av1/src/encode.rs +128 -9
- crates/ec-av1/src/encoder.rs +112 -0
- crates/ec-av1/src/speed.rs +8 -0
- crates/ec-av1/src/tile.rs +77 -10
- crates/ec-av1/src/transform.rs +40 -6
- lanes/txset.report.md +194 -0
crates/ec-av1/src/decode.rsmodified+8 -0
| @@ -5997,6 +5997,14 @@ pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option<TxType> { | |||
| 5997 | } | 5997 | } |
| 5998 | } | 5998 | } |
| 5999 | 5999 | ||
| 6000 | +/// [`tx_type_from_symbol`] inverted: which symbol of a `cdf_len`-wide | ||
| 6001 | +/// `tx_type` CDF names `tx_type`, or `None` when that set does not hold it | ||
| 6002 | +/// (class `table-and-reader-move-together` -- the writer picks its symbol | ||
| 6003 | +/// through the reader's own map, so the two cannot drift). | ||
| 6004 | +pub(crate) fn tx_type_symbol(cdf_len: usize, tx_type: TxType) -> Option<usize> { | ||
| 6005 | + (0..cdf_len.saturating_sub(1)).find(|&t| tx_type_from_symbol(cdf_len, t) == Some(tx_type)) | ||
| 6006 | +} | ||
| 6007 | + | ||
| 6000 | /// The remainder of a level the base and base-range syntax could not reach | 6008 | /// The remainder of a level the base and base-range syntax could not reach |
| 6001 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, | 6009 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 6002 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. | 6010 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
crates/ec-av1/src/encode.rsmodified+128 -9
| @@ -252,6 +252,48 @@ fn tx_select_inter() -> bool { | |||
| 252 | ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_SELECT_INTER)) | 252 | ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_SELECT_INTER)) |
| 253 | } | 253 | } |
| 254 | 254 | ||
| 255 | +/// SCREEN CONTENT ONLY, measured (12-frame `bd_rate_screen_native`, control | ||
| 256 | +/// vs arm): the capture row goes +27.8/-26.0 -> +20.8/-30.1 -- seven points | ||
| 257 | +/// against libaom -- while BOTH film rows LOSE (film A +21.7/-4.4 -> | ||
| 258 | +/// +22.1/-4.0, film B +26.9/-0.6 -> +28.1/+0.7) and 2160p bars lose 0.8/1.1. | ||
| 259 | +/// The keep rule wants both films down, so this takes the same content gate | ||
| 260 | +/// palette, intrabc and the angle-delta refinement already stand behind: a | ||
| 261 | +/// non-screen frame is byte-identical to the encoder before the lane. | ||
| 262 | +/// | ||
| 263 | +/// The transform types an intra luma transform unit of `set` may be coded | ||
| 264 | +/// with -- exactly the alphabet the writer's `tx_type` symbol names for that | ||
| 265 | +/// set, so a searched type is always one the tile can express (lane-txset). | ||
| 266 | +/// `DCT_DCT` first: it is the incumbent every other candidate has to beat. | ||
| 267 | +/// | ||
| 268 | +/// Under this encoder's `reduced_tx_set` header bit every intra set at 16x16 | ||
| 269 | +/// and below is `TX_SET_INTRA_2` (spec `get_tx_set`), the five types | ||
| 270 | +/// [`TxType::from_symbol`] names; 32x32 and 64x64 luma code no symbol at all, | ||
| 271 | +/// and inter luma reads the two-type `TX_SET_INTER_3`, left at `DCT_DCT` | ||
| 272 | +/// here. | ||
| 273 | +pub(crate) fn tx_type_candidates(set: TxbSet, screen: bool) -> &'static [TxType] { | ||
| 274 | + const DCT: [TxType; 1] = [TxType::DctDct]; | ||
| 275 | + const INTRA2: [TxType; 5] = [ | ||
| 276 | + TxType::DctDct, | ||
| 277 | + TxType::AdstAdst, | ||
| 278 | + TxType::AdstDct, | ||
| 279 | + TxType::DctAdst, | ||
| 280 | + TxType::Idtx, | ||
| 281 | + ]; | ||
| 282 | + match set { | ||
| 283 | + TxbSet::Luma16 | TxbSet::Luma8 | TxbSet::Luma4 if screen && tx_type_search() => &INTRA2, | ||
| 284 | + _ => &DCT, | ||
| 285 | + } | ||
| 286 | +} | ||
| 287 | + | ||
| 288 | +fn tx_type_search() -> bool { | ||
| 289 | + static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | ||
| 290 | + crate::envflags::var("EC_AV1_TXSET") | ||
| 291 | + .ok() | ||
| 292 | + .map(|v| !matches!(v.as_str(), "0" | "off")) | ||
| 293 | + }); | ||
| 294 | + ENV.unwrap_or_else(|| crate::speed::at(&crate::speed::TX_TYPE_SEARCH)) | ||
| 295 | +} | ||
| 296 | + | ||
| 255 | fn tx_select() -> bool { | 297 | fn tx_select() -> bool { |
| 256 | static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { | 298 | static ENV: std::sync::LazyLock<Option<bool>> = std::sync::LazyLock::new(|| { |
| 257 | crate::envflags::var("EC_AV1_TX_SELECT") | 299 | crate::envflags::var("EC_AV1_TX_SELECT") |
| @@ -2128,6 +2170,7 @@ impl Plane<'_> { | |||
| 2128 | let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); | 2170 | let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); |
| 2129 | let bits = crate::tile::rdoq( | 2171 | let bits = crate::tile::rdoq( |
| 2130 | &mut levels, &scaled, side, base_q_idx, at.set, skip_ctx, sign_ctx, rdoq_lambda(), | 2172 | &mut levels, &scaled, side, base_q_idx, at.set, skip_ctx, sign_ctx, rdoq_lambda(), |
| 2173 | + tx_type, | ||
| 2131 | ); | 2174 | ); |
| 2132 | (levels, Some(bits)) | 2175 | (levels, Some(bits)) |
| 2133 | } else { | 2176 | } else { |
| @@ -2182,12 +2225,13 @@ impl Plane<'_> { | |||
| 2182 | } else { | 2225 | } else { |
| 2183 | { | 2226 | { |
| 2184 | let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); | 2227 | let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, at.set); |
| 2185 | - crate::tile::coeff_bits( | 2228 | + crate::tile::coeff_bits_typed( |
| 2186 | &levels, | 2229 | &levels, |
| 2187 | at.set, | 2230 | at.set, |
| 2188 | crate::decode::q_ctx_of(base_q_idx), | 2231 | crate::decode::q_ctx_of(base_q_idx), |
| 2189 | skip_ctx, | 2232 | skip_ctx, |
| 2190 | sign_ctx, | 2233 | sign_ctx, |
| 2234 | + tx_type, | ||
| 2191 | ) | 2235 | ) |
| 2192 | } | 2236 | } |
| 2193 | }; | 2237 | }; |
| @@ -2258,6 +2302,7 @@ impl Plane<'_> { | |||
| 2258 | let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, set); | 2302 | let (skip_ctx, sign_ctx) = self.coef_ctx(x, y, side, set); |
| 2259 | let bits = crate::tile::rdoq( | 2303 | let bits = crate::tile::rdoq( |
| 2260 | &mut levels, &scaled, side, base_q_idx, set, skip_ctx, sign_ctx, rdoq_lambda(), | 2304 | &mut levels, &scaled, side, base_q_idx, set, skip_ctx, sign_ctx, rdoq_lambda(), |
| 2305 | + TxType::DctDct, | ||
| 2261 | ); | 2306 | ); |
| 2262 | (levels, Some(bits)) | 2307 | (levels, Some(bits)) |
| 2263 | } else { | 2308 | } else { |
| @@ -2353,7 +2398,7 @@ impl Plane<'_> { | |||
| 2353 | // `filter_intra`), not once over the whole block. | 2398 | // `filter_intra`), not once over the whole block. |
| 2354 | filter_intra: Option<u8>, | 2399 | filter_intra: Option<u8>, |
| 2355 | search: &Search, fctx: &crate::decode::FrameCtx, | 2400 | search: &Search, fctx: &crate::decode::FrameCtx, |
| 2356 | - ) -> (Vec<i32>, f64, f64) { | 2401 | + ) -> (Vec<i32>, f64, f64, Vec<TxType>) { |
| 2357 | let At { x, y, side, reach, .. } = at; | 2402 | let At { x, y, side, reach, .. } = at; |
| 2358 | let tx = side >> depth; | 2403 | let tx = side >> depth; |
| 2359 | let n = 1usize << depth; | 2404 | let n = 1usize << depth; |
| @@ -2369,6 +2414,11 @@ impl Plane<'_> { | |||
| 2369 | // for its `txb_skip` context, not context 0 ([`Plane::coef_ctx`]). | 2414 | // for its `txb_skip` context, not context 0 ([`Plane::coef_ctx`]). |
| 2370 | self.ctx.tu_split = n > 1; | 2415 | self.ctx.tu_split = n > 1; |
| 2371 | let (mut sse, mut bits) = (0.0, 0.0); | 2416 | let (mut sse, mut bits) = (0.0, 0.0); |
| 2417 | + // One entry per unit at its own `tu_row * n + tu_col`, which is how | ||
| 2418 | + // the writer indexes them (tile.rs `write_luma_tus`) -- a unit the | ||
| 2419 | + // frame edge cuts away is never coded there, so a compacted list | ||
| 2420 | + // would slide every later unit's type onto the wrong transform. | ||
| 2421 | + let mut tx_types = vec![TxType::DctDct; n * n]; | ||
| 2372 | for tu_row in 0..n { | 2422 | for tu_row in 0..n { |
| 2373 | for tu_col in 0..n { | 2423 | for tu_col in 0..n { |
| 2374 | let (col_off, row_off) = (tu_col * tx, tu_row * tx); | 2424 | let (col_off, row_off) = (tu_col * tx, tu_row * tx); |
| @@ -2406,7 +2456,27 @@ impl Plane<'_> { | |||
| 2406 | ) | 2456 | ) |
| 2407 | } | 2457 | } |
| 2408 | None => { | 2458 | None => { |
| 2409 | - self.trial(tu, mode, angle_delta, search.base_q_idx, search.deadzone, fctx) | 2459 | + // lane-txset: the unit picks its own transform type |
| 2460 | + // out of the set the writer can name it in, priced | ||
| 2461 | + // through the same RDOQ'd trial path (its `bits` | ||
| 2462 | + // already carry the `tx_type` symbol itself). | ||
| 2463 | + let mut best: Option<(f64, TxType, Trial)> = None; | ||
| 2464 | + for &tx_type in tx_type_candidates(set, search.screen) { | ||
| 2465 | + let candidate = self.trial_typed( | ||
| 2466 | + tu, mode, angle_delta, search.base_q_idx, search.deadzone, | ||
| 2467 | + tx_type, fctx, | ||
| 2468 | + ); | ||
| 2469 | + let cost = candidate.sse + search.lambda * candidate.bits; | ||
| 2470 | + if best.as_ref().is_none_or(|(best, _, _)| cost < *best) { | ||
| 2471 | + best = Some((cost, tx_type, candidate)); | ||
| 2472 | + } | ||
| 2473 | + } | ||
| 2474 | + let (_, tx_type, trial) = | ||
| 2475 | + best.expect("the candidate list is never empty"); | ||
| 2476 | + tx_types[tu_row * n + tu_col] = tx_type; | ||
| 2477 | + TX_TYPE_HITS[tx_type as usize] | ||
| 2478 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 2479 | + trial | ||
| 2410 | } | 2480 | } |
| 2411 | }; | 2481 | }; |
| 2412 | sse += trial.sse; | 2482 | sse += trial.sse; |
| @@ -2419,7 +2489,7 @@ impl Plane<'_> { | |||
| 2419 | } | 2489 | } |
| 2420 | } | 2490 | } |
| 2421 | self.ctx.tu_split = false; | 2491 | self.ctx.tu_split = false; |
| 2422 | - (levels, sse, bits) | 2492 | + (levels, sse, bits, tx_types) |
| 2423 | } | 2493 | } |
| 2424 | 2494 | ||
| 2425 | /// Writes a trial's reconstruction back into the plane, and the | 2495 | /// Writes a trial's reconstruction back into the plane, and the |
| @@ -2637,7 +2707,7 @@ impl Plane<'_> { | |||
| 2637 | at: At, | 2707 | at: At, |
| 2638 | search: &Search, | 2708 | search: &Search, |
| 2639 | mode_bits: &[f64; 13], fctx: &crate::decode::FrameCtx, | 2709 | mode_bits: &[f64; 13], fctx: &crate::decode::FrameCtx, |
| 2640 | - ) -> (Vec<Coeff>, u8, i32, f64) { | 2710 | + ) -> (Vec<Coeff>, u8, i32, f64, TxType) { |
| 2641 | let At { | 2711 | let At { |
| 2642 | x, y, side, reach, .. | 2712 | x, y, side, reach, .. |
| 2643 | } = at; | 2713 | } = at; |
| @@ -2777,6 +2847,23 @@ impl Plane<'_> { | |||
| 2777 | ANGLE_DELTA_HITS[(crate::tile::ANGLE_DELTA_ZERO as i32 + angle_delta) as usize] | 2847 | ANGLE_DELTA_HITS[(crate::tile::ANGLE_DELTA_ZERO as i32 + angle_delta) as usize] |
| 2778 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 2848 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2779 | } | 2849 | } |
| 2850 | + // lane-txset: the block's own transform type, searched on the mode | ||
| 2851 | + // and angle the trials above settled -- the same ordering the angle | ||
| 2852 | + // refinement uses, and the same RD terms. | ||
| 2853 | + let mut tx_type = TxType::DctDct; | ||
| 2854 | + for &candidate_type in tx_type_candidates(at.set, search.screen).iter().skip(1) { | ||
| 2855 | + let candidate = self.trial_typed( | ||
| 2856 | + at, mode, angle_delta, search.base_q_idx, search.deadzone, candidate_type, fctx, | ||
| 2857 | + ); | ||
| 2858 | + let cost_t = | ||
| 2859 | + candidate.sse + search.lambda * (candidate.bits + mode_bits[usize::from(mode)]); | ||
| 2860 | + if cost_t < cost { | ||
| 2861 | + cost = cost_t; | ||
| 2862 | + tx_type = candidate_type; | ||
| 2863 | + trial = candidate; | ||
| 2864 | + } | ||
| 2865 | + } | ||
| 2866 | + TX_TYPE_HITS[tx_type as usize].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 2780 | if census_on() { | 2867 | if census_on() { |
| 2781 | LUMA_RANK[sad_rank(&ranking, mode).min(12)] | 2868 | LUMA_RANK[sad_rank(&ranking, mode).min(12)] |
| 2782 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 2869 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| @@ -2784,7 +2871,7 @@ impl Plane<'_> { | |||
| 2784 | self.commit(x, y, side, &trial); | 2871 | self.commit(x, y, side, &trial); |
| 2785 | // A 64-point transform codes only its top-left 32x32 corner, which is | 2872 | // A 64-point transform codes only its top-left 32x32 corner, which is |
| 2786 | // the grid `code_from_prediction` hands back (lane-i64). | 2873 | // the grid `code_from_prediction` hands back (lane-i64). |
| 2787 | - (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost) | 2874 | + (coeffs(&trial.levels, side.min(BLOCK)), mode, angle_delta, cost, tx_type) |
| 2788 | } | 2875 | } |
| 2789 | } | 2876 | } |
| 2790 | 2877 | ||
| @@ -3586,6 +3673,19 @@ fn tx_depth_bits(side: usize, depth: usize) -> f64 { | |||
| 3586 | } | 3673 | } |
| 3587 | } | 3674 | } |
| 3588 | 3675 | ||
| 3676 | +/// How many luma transform units resolved to each [`TxType`], indexed by the | ||
| 3677 | +/// enum's own discriminant (lane-txset): the fire count a witness asserts on, | ||
| 3678 | +/// and the instrument that says which types the search actually reaches. | ||
| 3679 | +pub(crate) static TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] = | ||
| 3680 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 16]; | ||
| 3681 | + | ||
| 3682 | +/// Reads [`TX_TYPE_HITS`] and zeroes it, so a gate or a test attributes the | ||
| 3683 | +/// counts to its own encode. | ||
| 3684 | +#[cfg(test)] | ||
| 3685 | +pub(crate) fn take_tx_type_hits() -> [usize; 16] { | ||
| 3686 | + std::array::from_fn(|t| TX_TYPE_HITS[t].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 3687 | +} | ||
| 3688 | + | ||
| 3589 | /// How many blocks resolved to each `tx_depth`, the fire count that says | 3689 | /// How many blocks resolved to each `tx_depth`, the fire count that says |
| 3590 | /// whether the depth search below reaches its candidates at all. | 3690 | /// whether the depth search below reaches its candidates at all. |
| 3591 | static TX_DEPTH_HITS: [std::sync::atomic::AtomicUsize; 3] = | 3691 | static TX_DEPTH_HITS: [std::sync::atomic::AtomicUsize; 3] = |
| @@ -4377,8 +4477,9 @@ fn code_square( | |||
| 4377 | reach: Reach::of(side, x, y, luma.true_width, luma.true_height, fctx), | 4477 | reach: Reach::of(side, x, y, luma.true_width, luma.true_height, fctx), |
| 4378 | set: luma_set, | 4478 | set: luma_set, |
| 4379 | }; | 4479 | }; |
| 4380 | - let (mut luma_coeffs, mode, angle_delta, mut cost) = | 4480 | + let (mut luma_coeffs, mode, angle_delta, mut cost, tx_type) = |
| 4381 | luma.search_block(at, search, mode_bits, fctx); | 4481 | luma.search_block(at, search, mode_bits, fctx); |
| 4482 | + let mut luma_tx_types = vec![tx_type]; | ||
| 4382 | // The depth search runs on the mode the whole-block transform picked -- | 4483 | // The depth search runs on the mode the whole-block transform picked -- |
| 4383 | // libaom's own ordering, and the reason it costs one extra pass per depth | 4484 | // libaom's own ordering, and the reason it costs one extra pass per depth |
| 4384 | // rather than one per (mode, depth) pair. | 4485 | // rather than one per (mode, depth) pair. |
| @@ -4398,15 +4499,19 @@ fn code_square( | |||
| 4398 | luma.snapshot(x, y, side), | 4499 | luma.snapshot(x, y, side), |
| 4399 | luma_coeffs, | 4500 | luma_coeffs, |
| 4400 | ); | 4501 | ); |
| 4502 | + let mut best_types = std::mem::take(&mut luma_tx_types); | ||
| 4401 | for depth in 1..=max_tx_depth(side) { | 4503 | 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); | 4504 | + let (levels, sse, bits, types) = |
| 4505 | + luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx); | ||
| 4403 | let cost_d = sse | 4506 | let cost_d = sse |
| 4404 | + search.lambda | 4507 | + search.lambda |
| 4405 | * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth)); | 4508 | * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth)); |
| 4406 | if cost_d < best.0 { | 4509 | if cost_d < best.0 { |
| 4407 | best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side)); | 4510 | best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side)); |
| 4511 | + best_types = types; | ||
| 4408 | } | 4512 | } |
| 4409 | } | 4513 | } |
| 4514 | + luma_tx_types = best_types; | ||
| 4410 | luma.restore(x, y, side, &best.2); | 4515 | luma.restore(x, y, side, &best.2); |
| 4411 | cost = best.0; | 4516 | cost = best.0; |
| 4412 | tx_depth = best.1 as u8; | 4517 | tx_depth = best.1 as u8; |
| @@ -4503,7 +4608,10 @@ fn code_square( | |||
| 4503 | let mut best: Option<(f64, u8, usize, (Vec<u8>, Vec<CoefCtx>), Vec<Coeff>)> = None; | 4608 | let mut best: Option<(f64, u8, usize, (Vec<u8>, Vec<CoefCtx>), Vec<Coeff>)> = None; |
| 4504 | for fi in 0..5u8 { | 4609 | for fi in 0..5u8 { |
| 4505 | for depth in 0..=max_depth { | 4610 | for depth in 0..=max_depth { |
| 4506 | - let (levels, sse, bits) = | 4611 | + // A filter-intra block's units are coded through |
| 4612 | + // `code_from_prediction`, which is `DCT_DCT` throughout, so | ||
| 4613 | + // this arm keeps the empty (all-`DCT_DCT`) type list. | ||
| 4614 | + let (levels, sse, bits, _) = | ||
| 4507 | luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx); | 4615 | luma.code_tx_depth(at, DC_PRED, 0, depth, Some(fi), search, fctx); |
| 4508 | let cost_f = sse | 4616 | let cost_f = sse |
| 4509 | + search.lambda | 4617 | + search.lambda |
| @@ -4564,6 +4672,12 @@ fn code_square( | |||
| 4564 | } | 4672 | } |
| 4565 | } | 4673 | } |
| 4566 | } | 4674 | } |
| 4675 | + // A palette or filter-intra block's luma was recoded through `DCT_DCT` | ||
| 4676 | + // (its levels came from `code_from_prediction`), so the searched types | ||
| 4677 | + // belong to a block this one no longer is. | ||
| 4678 | + if palette.is_some() || filter_intra.is_some() { | ||
| 4679 | + luma_tx_types = Vec::new(); | ||
| 4680 | + } | ||
| 4567 | ( | 4681 | ( |
| 4568 | BlockCoeffs { | 4682 | BlockCoeffs { |
| 4569 | u, | 4683 | u, |
| @@ -4577,6 +4691,7 @@ fn code_square( | |||
| 4577 | filter_intra, | 4691 | filter_intra, |
| 4578 | cfl_alphas, | 4692 | cfl_alphas, |
| 4579 | angle_delta_y: angle_delta as i8, | 4693 | angle_delta_y: angle_delta as i8, |
| 4694 | + luma_tx_types, | ||
| 4580 | ..BlockCoeffs::default() | 4695 | ..BlockCoeffs::default() |
| 4581 | }, | 4696 | }, |
| 4582 | cost, | 4697 | cost, |
| @@ -5847,6 +5962,7 @@ fn code_square_inter( | |||
| 5847 | palette: None, | 5962 | palette: None, |
| 5848 | palette_uv: None, | 5963 | palette_uv: None, |
| 5849 | tx_depth, | 5964 | tx_depth, |
| 5965 | + luma_tx_types: Vec::new(), | ||
| 5850 | inter: Some(info), | 5966 | inter: Some(info), |
| 5851 | motion_mode: motion_won, | 5967 | motion_mode: motion_won, |
| 5852 | }, | 5968 | }, |
| @@ -5869,6 +5985,7 @@ fn code_square_inter( | |||
| 5869 | chroma[0].commit(x / 2, y / 2, side / 2, &u); | 5985 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 5870 | chroma[1].commit(x / 2, y / 2, side / 2, &v); | 5986 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 5871 | (BlockCoeffs { | 5987 | (BlockCoeffs { |
| 5988 | + luma_tx_types: Vec::new(), | ||
| 5872 | angle_delta_y: 0, | 5989 | angle_delta_y: 0, |
| 5873 | cfl_alphas: None, | 5990 | cfl_alphas: None, |
| 5874 | filter_intra: None, | 5991 | filter_intra: None, |
| @@ -9117,6 +9234,7 @@ fn search_inter_block( | |||
| 9117 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); | 9234 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| 9118 | ( | 9235 | ( |
| 9119 | BlockCoeffs { | 9236 | BlockCoeffs { |
| 9237 | + luma_tx_types: Vec::new(), | ||
| 9120 | angle_delta_y: 0, | 9238 | angle_delta_y: 0, |
| 9121 | cfl_alphas: None, | 9239 | cfl_alphas: None, |
| 9122 | filter_intra: None, | 9240 | filter_intra: None, |
| @@ -11802,6 +11920,7 @@ mod tests { | |||
| 11802 | let (_, inter_header) = inter_frame_headers(width, height, 100, 1, 0).unwrap(); | 11920 | let (_, inter_header) = inter_frame_headers(width, height, 100, 1, 0).unwrap(); |
| 11803 | 11921 | ||
| 11804 | let residual_block = BlockCoeffs { | 11922 | let residual_block = BlockCoeffs { |
| 11923 | + luma_tx_types: Vec::new(), | ||
| 11805 | angle_delta_y: 0, | 11924 | angle_delta_y: 0, |
| 11806 | cfl_alphas: None, | 11925 | cfl_alphas: None, |
| 11807 | filter_intra: None, | 11926 | filter_intra: None, |
crates/ec-av1/src/encoder.rsmodified+112 -0
| @@ -3615,6 +3615,118 @@ mod tests { | |||
| 3615 | eprintln!("delta_q witness: {levels} quantizer levels, {} bytes", stream.len()); | 3615 | eprintln!("delta_q witness: {levels} quantizer levels, {} bytes", stream.len()); |
| 3616 | } | 3616 | } |
| 3617 | 3617 | ||
| 3618 | + /// lane-txset's witness: a clip of hard directional edges makes the intra | ||
| 3619 | + /// luma search take EVERY type of the five-type reduced set | ||
| 3620 | + /// (`TX_SET_INTRA_2`: `DCT_DCT`, `ADST_ADST`, `ADST_DCT`, `DCT_ADST`, | ||
| 3621 | + /// `IDTX`) at least once -- the fire count, class `gate-blind-to-feature` | ||
| 3622 | + /// -- and both decoders reconstruct every frame sample-exact from the | ||
| 3623 | + /// `tx_type` symbols that name them. Before the lane the writer coded | ||
| 3624 | + /// `DCT_DCT` for every luma transform unit ever written, so this is also | ||
| 3625 | + /// the statement that the other four are REACHABLE at all. | ||
| 3626 | + #[test] | ||
| 3627 | + fn an_edge_clip_codes_every_reduced_set_tx_type_both_decoders_read_exactly() { | ||
| 3628 | + // `knob_write`: the screen override below is process-global, and the | ||
| 3629 | + // type search is SCREEN-ONLY (`encode::tx_type_candidates`). | ||
| 3630 | + let _knobs = crate::speed::knob_write(); | ||
| 3631 | + crate::encode::force_screen(Some(true)); | ||
| 3632 | + let _gate_lock = crate::stream::tests::lock_gate_counters(); | ||
| 3633 | + let (width, height) = (320usize, 192usize); | ||
| 3634 | + // Sharp horizontal and vertical steps: a vertical edge is what an | ||
| 3635 | + // `ADST` column pass beats a `DCT` one on, a horizontal edge the | ||
| 3636 | + // mirror, and the flat/ramp areas between them are where `IDTX` wins. | ||
| 3637 | + let sources: Vec<Picture> = (0..2) | ||
| 3638 | + .map(|t| { | ||
| 3639 | + let mut p = test_card(width, height, t * 3); | ||
| 3640 | + for y in 0..height { | ||
| 3641 | + for x in 0..width { | ||
| 3642 | + let v = match ((x + t * 4) / 16 % 2, y / 16 % 2) { | ||
| 3643 | + (0, 0) => 16u16, | ||
| 3644 | + (1, 0) => 235, | ||
| 3645 | + (0, 1) => (x % 64 * 3) as u16, | ||
| 3646 | + _ => (y % 64 * 3) as u16, | ||
| 3647 | + }; | ||
| 3648 | + p.y[y * width + x] = v; | ||
| 3649 | + } | ||
| 3650 | + } | ||
| 3651 | + p | ||
| 3652 | + }) | ||
| 3653 | + .collect(); | ||
| 3654 | + let config = EncoderConfig { | ||
| 3655 | + width, | ||
| 3656 | + height, | ||
| 3657 | + base_q_idx: 90, | ||
| 3658 | + gop: 2, | ||
| 3659 | + colour: Colour::Bt709Limited, | ||
| 3660 | + tile_cols_log2: 0, | ||
| 3661 | + tile_rows_log2: 0, | ||
| 3662 | + }; | ||
| 3663 | + let mut enc = Av1Encoder::new(config).unwrap(); | ||
| 3664 | + let _ = crate::encode::take_tx_type_hits(); | ||
| 3665 | + let mut stream = Vec::new(); | ||
| 3666 | + for packet in encode_all(&mut enc, &sources) { | ||
| 3667 | + stream.extend_from_slice(&packet.data); | ||
| 3668 | + } | ||
| 3669 | + let hits = crate::encode::take_tx_type_hits(); | ||
| 3670 | + // The type search is a speed lever (`speed::TX_TYPE_SEARCH`); above | ||
| 3671 | + // the presets that carry it only `DCT_DCT` is offered, and the | ||
| 3672 | + // exactness half below is what still runs there. The FULL five-type | ||
| 3673 | + // fire count is preset 0's to make: a faster preset prunes modes and | ||
| 3674 | + // partitions before a type is ever priced on them, so `ADST_ADST` and | ||
| 3675 | + // `IDTX` stop winning on this clip at preset 6 (measured: hits | ||
| 3676 | + // `[0, 6192, 0, 79, 284, ..]`). Every preset that carries the lever | ||
| 3677 | + // still has to reach SOME non-`DCT_DCT` type -- that is the | ||
| 3678 | + // "reachable at all" statement, class `gate-blind-to-feature`. | ||
| 3679 | + if crate::speed::at(&crate::speed::TX_TYPE_SEARCH) { | ||
| 3680 | + let non_dct: usize = hits.iter().sum::<usize>() | ||
| 3681 | + - hits[crate::transform::TxType::DctDct as usize]; | ||
| 3682 | + assert!( | ||
| 3683 | + non_dct > 0, | ||
| 3684 | + "every luma transform unit took DCT_DCT: {hits:?}" | ||
| 3685 | + ); | ||
| 3686 | + } | ||
| 3687 | + if crate::speed::at(&crate::speed::TX_TYPE_SEARCH) && crate::speed::speed() == 0 { | ||
| 3688 | + for (name, ty) in [ | ||
| 3689 | + ("DCT_DCT", crate::transform::TxType::DctDct), | ||
| 3690 | + ("ADST_ADST", crate::transform::TxType::AdstAdst), | ||
| 3691 | + ("ADST_DCT", crate::transform::TxType::AdstDct), | ||
| 3692 | + ("DCT_ADST", crate::transform::TxType::DctAdst), | ||
| 3693 | + ("IDTX", crate::transform::TxType::Idtx), | ||
| 3694 | + ] { | ||
| 3695 | + assert!( | ||
| 3696 | + hits[ty as usize] > 0, | ||
| 3697 | + "no luma transform unit took {name}: {hits:?}" | ||
| 3698 | + ); | ||
| 3699 | + } | ||
| 3700 | + } else { | ||
| 3701 | + eprintln!( | ||
| 3702 | + "SKIP the five-type fire count at preset {}: {hits:?}", | ||
| 3703 | + crate::speed::speed() | ||
| 3704 | + ); | ||
| 3705 | + } | ||
| 3706 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | ||
| 3707 | + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); | ||
| 3708 | + if have_ffmpeg() { | ||
| 3709 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | ||
| 3710 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); | ||
| 3711 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 3712 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 3713 | + if let Some(at) = got.iter().zip(b).position(|(x, y)| x != y) { | ||
| 3714 | + panic!( | ||
| 3715 | + "frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", | ||
| 3716 | + at % width, | ||
| 3717 | + at / width, | ||
| 3718 | + got[at], | ||
| 3719 | + b[at], | ||
| 3720 | + ); | ||
| 3721 | + } | ||
| 3722 | + } | ||
| 3723 | + } else { | ||
| 3724 | + eprintln!("SKIP the ffmpeg half: no ffmpeg"); | ||
| 3725 | + } | ||
| 3726 | + crate::encode::force_screen(None); | ||
| 3727 | + eprintln!("tx_type witness: hits {hits:?}, {} bytes", stream.len()); | ||
| 3728 | + } | ||
| 3729 | + | ||
| 3618 | /// Every SHIPPED SPEED PRESET codes a stream both decoders reconstruct | 3730 | /// Every SHIPPED SPEED PRESET codes a stream both decoders reconstruct |
| 3619 | /// sample-exact -- ours and ffmpeg's -- and codes a DIFFERENT stream from | 3731 | /// sample-exact -- ours and ffmpeg's -- and codes a DIFFERENT stream from |
| 3620 | /// its neighbour (a preset that changes no byte is a preset that buys no | 3732 | /// its neighbour (a preset that changes no byte is a preset that buys no |
crates/ec-av1/src/speed.rsmodified+8 -0
| @@ -483,6 +483,14 @@ pub(crate) const RESTORATION: [bool; 11] = [ | |||
| 483 | pub(crate) const TPL_DEPTH: [usize; 11] = | 483 | pub(crate) const TPL_DEPTH: [usize; 11] = |
| 484 | [crate::encode::TPL_DEPTH, 8, 8, 4, 4, 4, 4, 1, 1, 1, 1]; | 484 | [crate::encode::TPL_DEPTH, 8, 8, 4, 4, 4, 4, 1, 1, 1, 1]; |
| 485 | 485 | ||
| 486 | +/// `encode::tx_type_search`: whether an intra luma transform unit searches | ||
| 487 | +/// its own `tx_type` instead of coding `DCT_DCT` (lane-txset). The five-type | ||
| 488 | +/// reduced-set alphabet the writer already codes is what it picks from, so a | ||
| 489 | +/// preset that turns it off is byte-identical to the encoder before the lane. | ||
| 490 | +pub(crate) const TX_TYPE_SEARCH: [bool; 11] = [ | ||
| 491 | + true, true, true, true, true, true, true, false, false, false, false, | ||
| 492 | +]; | ||
| 493 | + | ||
| 486 | /// `filter_search`: whether the deblock ladder's +-1/+-2 refinement stage runs. | 494 | /// `filter_search`: whether the deblock ladder's +-1/+-2 refinement stage runs. |
| 487 | pub(crate) const DEBLOCK_REFINE: [bool; 11] = [ | 495 | pub(crate) const DEBLOCK_REFINE: [bool; 11] = [ |
| 488 | true, true, true, true, true, true, true, false, false, false, false, | 496 | true, true, true, true, true, true, true, false, false, false, false, |
crates/ec-av1/src/tile.rsmodified+77 -10
| @@ -21,6 +21,7 @@ use crate::cdf; | |||
| 21 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; | 21 | use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables}; |
| 22 | use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect}; | 22 | use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect}; |
| 23 | use crate::msac::SymbolEncoder; | 23 | use crate::msac::SymbolEncoder; |
| 24 | +use crate::transform::TxType; | ||
| 24 | use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16}; | 25 | use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16}; |
| 25 | 26 | ||
| 26 | // --------------------------------------------------------------------------- | 27 | // --------------------------------------------------------------------------- |
| @@ -1674,6 +1675,10 @@ pub struct Coeff { | |||
| 1674 | pub struct BlockCoeffs { | 1675 | pub struct BlockCoeffs { |
| 1675 | /// The luma transform's coefficients. | 1676 | /// The luma transform's coefficients. |
| 1676 | pub luma: Vec<Coeff>, | 1677 | 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>, | ||
| 1677 | /// The U plane's. | 1682 | /// The U plane's. |
| 1678 | pub u: Vec<Coeff>, | 1683 | pub u: Vec<Coeff>, |
| 1679 | /// The V plane's. | 1684 | /// The V plane's. |
| @@ -3994,6 +3999,7 @@ fn write_block( | |||
| 3994 | &grids[0], | 3999 | &grids[0], |
| 3995 | tx_mode, | 4000 | tx_mode, |
| 3996 | all_scans, | 4001 | all_scans, |
| 4002 | + &block.luma_tx_types, | ||
| 3997 | )?; | 4003 | )?; |
| 3998 | tx < side | 4004 | tx < side |
| 3999 | } else { | 4005 | } else { |
| @@ -4008,6 +4014,7 @@ fn write_block( | |||
| 4008 | &neighbours.around(at, side), | 4014 | &neighbours.around(at, side), |
| 4009 | tx_mode, | 4015 | tx_mode, |
| 4010 | tx_select, | 4016 | tx_select, |
| 4017 | + block_tx_type(block), | ||
| 4011 | ); | 4018 | ); |
| 4012 | neighbours.record_planes(at, side, mode, grids, !split); | 4019 | neighbours.record_planes(at, side, mode, grids, !split); |
| 4013 | clear_skip_band(neighbours, at_mi, side); | 4020 | clear_skip_band(neighbours, at_mi, side); |
| @@ -4092,6 +4099,7 @@ fn write_leaf8( | |||
| 4092 | &grids[0], | 4099 | &grids[0], |
| 4093 | tx_mode, | 4100 | tx_mode, |
| 4094 | all_scans, | 4101 | all_scans, |
| 4102 | + &block.luma_tx_types, | ||
| 4095 | )?; | 4103 | )?; |
| 4096 | tx < 8 | 4104 | tx < 8 |
| 4097 | } else { | 4105 | } else { |
| @@ -4106,6 +4114,7 @@ fn write_leaf8( | |||
| 4106 | &neighbours.around_mi(leaf_mi, 8), | 4114 | &neighbours.around_mi(leaf_mi, 8), |
| 4107 | tx_mode, | 4115 | tx_mode, |
| 4108 | tx_select, | 4116 | tx_select, |
| 4117 | + block_tx_type(block), | ||
| 4109 | ); | 4118 | ); |
| 4110 | neighbours.record_mi_planes(leaf_mi, 8, grids, !split); | 4119 | neighbours.record_mi_planes(leaf_mi, 8, grids, !split); |
| 4111 | clear_skip_band(neighbours, leaf_mi, 8); | 4120 | clear_skip_band(neighbours, leaf_mi, 8); |
| @@ -4127,6 +4136,9 @@ fn write_block_planes( | |||
| 4127 | // A block whose luma was already written as several transform units | 4136 | // A block whose luma was already written as several transform units |
| 4128 | // ([`write_luma_select`]) has only its chroma planes left here. | 4137 | // ([`write_luma_select`]) has only its chroma planes left here. |
| 4129 | skip_luma: bool, | 4138 | 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, | ||
| 4130 | ) { | 4142 | ) { |
| 4131 | for (plane, (grid, scan)) in grids.iter().zip(scans.iter()).enumerate() { | 4143 | for (plane, (grid, scan)) in grids.iter().zip(scans.iter()).enumerate() { |
| 4132 | if plane == 0 && skip_luma { | 4144 | if plane == 0 && skip_luma { |
| @@ -4153,6 +4165,7 @@ fn write_block_planes( | |||
| 4153 | skip_ctx, | 4165 | skip_ctx, |
| 4154 | dc_sign_ctx(around[plane].dc_vote), | 4166 | dc_sign_ctx(around[plane].dc_vote), |
| 4155 | Some(plane), | 4167 | Some(plane), |
| 4168 | + if plane == 0 { luma_tx_type } else { TxType::DctDct }, | ||
| 4156 | ); | 4169 | ); |
| 4157 | #[cfg(test)] | 4170 | #[cfg(test)] |
| 4158 | census_record( | 4171 | census_record( |
| @@ -4173,6 +4186,12 @@ fn write_block_planes( | |||
| 4173 | } | 4186 | } |
| 4174 | } | 4187 | } |
| 4175 | 4188 | ||
| 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 | + | ||
| 4176 | /// The `(side / tx)^2` transform units of one block's luma residual, raster | 4195 | /// The `(side / tx)^2` transform units of one block's luma residual, raster |
| 4177 | /// order, each with its own coefficient context and each published before the | 4196 | /// order, each with its own coefficient context and each published before the |
| 4178 | /// next one reads it (decode.rs `decode_block`'s multi-transform-unit branch). | 4197 | /// next one reads it (decode.rs `decode_block`'s multi-transform-unit branch). |
| @@ -4192,6 +4211,10 @@ fn write_luma_tus( | |||
| 4192 | // lane-av1tx2 r2: an INTER block's split units read the inter coefficient | 4211 | // lane-av1tx2 r2: an INTER block's split units read the inter coefficient |
| 4193 | // tables, not the intra ones (`TxbSet::Luma*Inter`). | 4212 | // tables, not the intra ones (`TxbSet::Luma*Inter`). |
| 4194 | inter: bool, | 4213 | 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], | ||
| 4195 | ) -> Result<()> { | 4218 | ) -> Result<()> { |
| 4196 | if tx < side && side > 32 && tx < 32 { | 4219 | if tx < side && side > 32 && tx < 32 { |
| 4197 | // A 64x64 block's own levels reach the writer as the 32x32 corner a | 4220 | // A 64x64 block's own levels reach the writer as the 32x32 corner a |
| @@ -4268,6 +4291,10 @@ fn write_luma_tus( | |||
| 4268 | skip_ctx, | 4291 | skip_ctx, |
| 4269 | dc_sign_ctx(around.dc_vote), | 4292 | dc_sign_ctx(around.dc_vote), |
| 4270 | Some(0), | 4293 | Some(0), |
| 4294 | + tx_types | ||
| 4295 | + .get(tu_row * n + tu_col) | ||
| 4296 | + .copied() | ||
| 4297 | + .unwrap_or(TxType::DctDct), | ||
| 4271 | ); | 4298 | ); |
| 4272 | #[cfg(test)] | 4299 | #[cfg(test)] |
| 4273 | census_record( | 4300 | census_record( |
| @@ -4308,6 +4335,7 @@ fn write_luma_select( | |||
| 4308 | grid: &[i32], | 4335 | grid: &[i32], |
| 4309 | mode: usize, | 4336 | mode: usize, |
| 4310 | scans: &[Vec<u16>; 4], | 4337 | scans: &[Vec<u16>; 4], |
| 4338 | + tx_types: &[TxType], | ||
| 4311 | ) -> Result<usize> { | 4339 | ) -> Result<usize> { |
| 4312 | let max_tx = side.min(64); | 4340 | let max_tx = side.min(64); |
| 4313 | let ctx = neighbours.tx_size_ctx(at_mi, max_tx); | 4341 | let ctx = neighbours.tx_size_ctx(at_mi, max_tx); |
| @@ -4324,7 +4352,7 @@ fn write_luma_select( | |||
| 4324 | } | 4352 | } |
| 4325 | } | 4353 | } |
| 4326 | let tx = max_tx >> depth; | 4354 | 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)?; |
| 4328 | neighbours.record_tx(at_mi, side, tx); | 4356 | neighbours.record_tx(at_mi, side, tx); |
| 4329 | Ok(tx) | 4357 | Ok(tx) |
| 4330 | } | 4358 | } |
| @@ -4697,6 +4725,19 @@ fn scan_of(side: usize) -> &'static Vec<u16> { | |||
| 4697 | } | 4725 | } |
| 4698 | 4726 | ||
| 4699 | pub(crate) fn coeff_bits( | 4727 | 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( | ||
| 4700 | grid: &[i32], | 4741 | grid: &[i32], |
| 4701 | set: TxbSet, | 4742 | set: TxbSet, |
| 4702 | q_ctx: usize, | 4743 | q_ctx: usize, |
| @@ -4708,6 +4749,7 @@ pub(crate) fn coeff_bits( | |||
| 4708 | // largest remaining pricer error for every other block. | 4749 | // largest remaining pricer error for every other block. |
| 4709 | skip_ctx: usize, | 4750 | skip_ctx: usize, |
| 4710 | sign_ctx: usize, | 4751 | sign_ctx: usize, |
| 4752 | + tx_type: TxType, | ||
| 4711 | ) -> f64 { | 4753 | ) -> f64 { |
| 4712 | // `Cdfs::new` builds every table a tile writer adapts -- tens of | 4754 | // `Cdfs::new` builds every table a tile writer adapts -- tens of |
| 4713 | // kilobytes -- and this function is called once per priced candidate: | 4755 | // kilobytes -- and this function is called once per priced candidate: |
| @@ -4743,7 +4785,7 @@ pub(crate) fn coeff_bits( | |||
| 4743 | } | 4785 | } |
| 4744 | let scan = scan_of(coding.side); | 4786 | let scan = scan_of(coding.side); |
| 4745 | let mut enc = SymbolEncoder::pricer(); | 4787 | 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); |
| 4747 | enc.bits() | 4789 | enc.bits() |
| 4748 | }; | 4790 | }; |
| 4749 | // lane-av1speed2: 60% of the transform units of an inter stream hold no | 4791 | // lane-av1speed2: 60% of the transform units of an inter stream hold no |
| @@ -4842,6 +4884,7 @@ pub(crate) fn rdoq( | |||
| 4842 | skip_ctx: usize, | 4884 | skip_ctx: usize, |
| 4843 | sign_ctx: usize, | 4885 | sign_ctx: usize, |
| 4844 | lambda: f64, | 4886 | lambda: f64, |
| 4887 | + tx_type: TxType, | ||
| 4845 | ) -> f64 { | 4888 | ) -> f64 { |
| 4846 | // A 64-point transform carries its top-left 32x32 alone, and that corner | 4889 | // A 64-point transform carries its top-left 32x32 alone, and that corner |
| 4847 | // is what the writer and the pricer take (`coded_corner`). | 4890 | // is what the writer and the pricer take (`coded_corner`). |
| @@ -4857,16 +4900,17 @@ pub(crate) fn rdoq( | |||
| 4857 | q_ctx: usize, | 4900 | q_ctx: usize, |
| 4858 | skip_ctx: usize, | 4901 | skip_ctx: usize, |
| 4859 | sign_ctx: usize, | 4902 | sign_ctx: usize, |
| 4903 | + tx_type: TxType, | ||
| 4860 | ) -> f64 { | 4904 | ) -> f64 { |
| 4861 | if dense.is_empty() { | 4905 | 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); |
| 4863 | } | 4907 | } |
| 4864 | for row in 0..coded { | 4908 | for row in 0..coded { |
| 4865 | dense[row * coded..][..coded].copy_from_slice(&levels[row * side..][..coded]); | 4909 | dense[row * coded..][..coded].copy_from_slice(&levels[row * side..][..coded]); |
| 4866 | } | 4910 | } |
| 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) |
| 4868 | } | 4912 | } |
| 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); |
| 4870 | // The DC has its own quantiser, so its squared error per unit of level is | 4914 | // The DC has its own quantiser, so its squared error per unit of level is |
| 4871 | // `(dc_q / ac_q)^2` of an AC coefficient's. | 4915 | // `(dc_q / ac_q)^2` of an AC coefficient's. |
| 4872 | let dc = f64::from(crate::quant::dc_q(8, i32::from(base_q_idx))); | 4916 | let dc = f64::from(crate::quant::dc_q(8, i32::from(base_q_idx))); |
| @@ -4905,7 +4949,7 @@ pub(crate) fn rdoq( | |||
| 4905 | let candidate = level - level.signum(); | 4949 | let candidate = level - level.signum(); |
| 4906 | levels[i] = candidate; | 4950 | levels[i] = candidate; |
| 4907 | budget -= 1; | 4951 | 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); |
| 4909 | let distortion = weight | 4953 | let distortion = weight |
| 4910 | * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2)); | 4954 | * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2)); |
| 4911 | if distortion + lambda * (after - bits) < 0.0 { | 4955 | if distortion + lambda * (after - bits) < 0.0 { |
| @@ -5177,6 +5221,10 @@ fn write_coeffs( | |||
| 5177 | skip_ctx: usize, | 5221 | skip_ctx: usize, |
| 5178 | sign_ctx: usize, | 5222 | sign_ctx: usize, |
| 5179 | plane: Option<usize>, | 5223 | 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, | ||
| 5180 | ) { | 5228 | ) { |
| 5181 | if let Some(plane) = plane { | 5229 | if let Some(plane) = plane { |
| 5182 | ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); | 5230 | ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); |
| @@ -5212,8 +5260,18 @@ fn write_coeffs( | |||
| 5212 | // A luma transform whose type set holds more than one type codes which it | 5260 | // A luma transform whose type set holds more than one type codes which it |
| 5213 | // is, right after the all-zero flag (spec 5.11.39). The writer only ever | 5261 | // is, right after the all-zero flag (spec 5.11.39). The writer only ever |
| 5214 | // uses DCT_DCT, which is index one of `Tx_Type_Intra_Inv_Set2`. | 5262 | // 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() { | 5263 | + if let Some(cdf) = coding.tx_type.as_deref_mut() { |
| 5216 | - enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); | 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); | ||
| 5217 | if plane.is_some() { | 5275 | if plane.is_some() { |
| 5218 | ec_rng_trace(|| format!("EC_TXTYPE tell={} rng={}", enc.tell(), enc.rng())); | 5276 | ec_rng_trace(|| format!("EC_TXTYPE tell={} rng={}", enc.tell(), enc.rng())); |
| 5219 | } | 5277 | } |
| @@ -6242,6 +6300,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6242 | 0, | 6300 | 0, |
| 6243 | &all_scans, | 6301 | &all_scans, |
| 6244 | true, | 6302 | true, |
| 6303 | + &block.luma_tx_types, | ||
| 6245 | )?; | 6304 | )?; |
| 6246 | } | 6305 | } |
| 6247 | write_block_planes( | 6306 | write_block_planes( |
| @@ -6253,6 +6312,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6253 | &neighbours.around(sb_at, SB), | 6312 | &neighbours.around(sb_at, SB), |
| 6254 | 0, | 6313 | 0, |
| 6255 | split, | 6314 | split, |
| 6315 | + block_tx_type(block), | ||
| 6256 | ); | 6316 | ); |
| 6257 | neighbours.record_planes(sb_at, SB, 0, &grids, !split); | 6317 | neighbours.record_planes(sb_at, SB, 0, &grids, !split); |
| 6258 | } | 6318 | } |
| @@ -6690,6 +6750,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6690 | mode_for_tx, | 6750 | mode_for_tx, |
| 6691 | &all_scans, | 6751 | &all_scans, |
| 6692 | is_inter, | 6752 | is_inter, |
| 6753 | + &block.luma_tx_types, | ||
| 6693 | )?; | 6754 | )?; |
| 6694 | } | 6755 | } |
| 6695 | write_block_planes( | 6756 | write_block_planes( |
| @@ -6705,6 +6766,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 6705 | &neighbours.around(at, BLOCK), | 6766 | &neighbours.around(at, BLOCK), |
| 6706 | mode_for_tx, | 6767 | mode_for_tx, |
| 6707 | split, | 6768 | split, |
| 6769 | + block_tx_type(block), | ||
| 6708 | ); | 6770 | ); |
| 6709 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); | 6771 | neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); |
| 6710 | } | 6772 | } |
| @@ -6929,6 +6991,7 @@ fn write_inter_frame_leaf( | |||
| 6929 | if split { | 6991 | if split { |
| 6930 | write_luma_tus( | 6992 | write_luma_tus( |
| 6931 | enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter, | 6993 | enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter, |
| 6994 | + &block.luma_tx_types, | ||
| 6932 | )?; | 6995 | )?; |
| 6933 | } | 6996 | } |
| 6934 | write_block_planes( | 6997 | write_block_planes( |
| @@ -6944,6 +7007,7 @@ fn write_inter_frame_leaf( | |||
| 6944 | &neighbours.around(at, SUB), | 7007 | &neighbours.around(at, SUB), |
| 6945 | mode_for_tx, | 7008 | mode_for_tx, |
| 6946 | split, | 7009 | split, |
| 7010 | + block_tx_type(block), | ||
| 6947 | ); | 7011 | ); |
| 6948 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); | 7012 | neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); |
| 6949 | } | 7013 | } |
| @@ -7184,6 +7248,7 @@ fn write_inter_frame_leaf8( | |||
| 7184 | if split { | 7248 | if split { |
| 7185 | write_luma_tus( | 7249 | write_luma_tus( |
| 7186 | enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter, | 7250 | enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter, |
| 7251 | + &block.luma_tx_types, | ||
| 7187 | )?; | 7252 | )?; |
| 7188 | } | 7253 | } |
| 7189 | write_block_planes( | 7254 | write_block_planes( |
| @@ -7195,6 +7260,7 @@ fn write_inter_frame_leaf8( | |||
| 7195 | &neighbours.around_mi(leaf_mi, 8), | 7260 | &neighbours.around_mi(leaf_mi, 8), |
| 7196 | mode_for_tx, | 7261 | mode_for_tx, |
| 7197 | split, | 7262 | split, |
| 7263 | + block_tx_type(block), | ||
| 7198 | ); | 7264 | ); |
| 7199 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); | 7265 | neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); |
| 7200 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); | 7266 | neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block)); |
| @@ -7939,6 +8005,7 @@ mod tests { | |||
| 7939 | 0, | 8005 | 0, |
| 7940 | 0, | 8006 | 0, |
| 7941 | None, | 8007 | None, |
| 8008 | + TxType::DctDct, | ||
| 7942 | ); | 8009 | ); |
| 7943 | priced += luma_32_coeff_bits(grid); | 8010 | priced += luma_32_coeff_bits(grid); |
| 7944 | } | 8011 | } |
| @@ -8887,7 +8954,7 @@ mod tests { | |||
| 8887 | .sum(); | 8954 | .sum(); |
| 8888 | d + lambda * coeff_bits(grid, set, crate::decode::q_ctx_of(q), 0, 0) | 8955 | d + lambda * coeff_bits(grid, set, crate::decode::q_ctx_of(q), 0, 0) |
| 8889 | }; | 8956 | }; |
| 8890 | - let bits = rdoq(&mut levels, &scaled, side, q, set, 0, 0, lambda); | 8957 | + let bits = rdoq(&mut levels, &scaled, side, q, set, 0, 0, lambda, TxType::DctDct); |
| 8891 | assert_eq!(bits, coeff_bits(&levels, set, crate::decode::q_ctx_of(q), 0, 0)); | 8958 | assert_eq!(bits, coeff_bits(&levels, set, crate::decode::q_ctx_of(q), 0, 0)); |
| 8892 | assert!(cost(&levels) <= cost(&before), "{:?} vs {:?}", cost(&levels), cost(&before)); | 8959 | assert!(cost(&levels) <= cost(&before), "{:?} vs {:?}", cost(&levels), cost(&before)); |
| 8893 | // The tail is what it is for: the last coded position moves earlier. | 8960 | // The tail is what it is for: the last coded position moves earlier. |
| @@ -8908,7 +8975,7 @@ mod tests { | |||
| 8908 | if v < 0.0 { -l } else { l } | 8975 | if v < 0.0 { -l } else { l } |
| 8909 | }) | 8976 | }) |
| 8910 | .collect(); | 8977 | .collect(); |
| 8911 | - let bits = rdoq(&mut levels, &scaled, side, q, set, 0, 0, lambda); | 8978 | + let bits = rdoq(&mut levels, &scaled, side, q, set, 0, 0, lambda, TxType::DctDct); |
| 8912 | let corner: Vec<i32> = (0..32) | 8979 | let corner: Vec<i32> = (0..32) |
| 8913 | .flat_map(|row| levels[row * side..][..32].to_vec()) | 8980 | .flat_map(|row| levels[row * side..][..32].to_vec()) |
| 8914 | .collect(); | 8981 | .collect(); |
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 | |||
| 1483 | if tx_type == TxType::DctDct { | 1483 | if tx_type == TxType::DctDct { |
| 1484 | return forward_transform_2d(residual, side); | 1484 | return forward_transform_2d(residual, side); |
| 1485 | } | 1485 | } |
| 1486 | - debug_assert_eq!( | 1486 | + // A FLIPADST axis is the plain ADST kernel with the DECODER's output |
| 1487 | - tx_type.flip(), | 1487 | + // mirrored (`dequant_and_inverse_typed_wh`'s `ud_flip`/`lr_flip`: row |
| 1488 | - (false, false), | 1488 | + // `h - 1 - i`, column `w - 1 - j`). A mirror is its own inverse, so the |
| 1489 | - "a flipped ADST needs its residual mirrored before this basis applies" | 1489 | + // analysis half is the same plain basis over the mirrored residual -- |
| 1490 | - ); | 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 }; | ||
| 1491 | let (row_kind, col_kind) = tx_type.axes(); | 1505 | let (row_kind, col_kind) = tx_type.axes(); |
| 1492 | let (hb, vb) = (axis_basis_t(side, row_kind), axis_basis_t(side, col_kind)); | 1506 | let (hb, vb) = (axis_basis_t(side, row_kind), axis_basis_t(side, col_kind)); |
| 1493 | let mut rows = vec![0.0f64; side * side]; | 1507 | let mut rows = vec![0.0f64; side * side]; |
| @@ -2162,7 +2176,27 @@ mod tests { | |||
| 2162 | (sum / (side * side) as f64).sqrt() | 2176 | (sum / (side * side) as f64).sqrt() |
| 2163 | }; | 2177 | }; |
| 2164 | let dct = rmse(TxType::DctDct); | 2178 | 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 | + ] { | ||
| 2166 | let e = rmse(tx); | 2200 | let e = rmse(tx); |
| 2167 | assert!( | 2201 | assert!( |
| 2168 | e < 2.0 * dct, | 2202 | e < 2.0 * dct, |
lanes/txset.report.mdadded+194 -0
| @@ -0,0 +1,194 @@ | |||
| 1 | +# lane-txset -- the transform-type search | ||
| 2 | + | ||
| 3 | +Branch `lane-txset` off main `3ebe0de9`. Scope: which `tx_type` an intra luma | ||
| 4 | +transform unit is coded with, the forward kernels the missing types need, and | ||
| 5 | +the writer symbol that names them. | ||
| 6 | + | ||
| 7 | +## 1. Instrument: what was coded, what was searched (read off the code) | ||
| 8 | + | ||
| 9 | +The writer coded ONE transform type, everywhere, always: | ||
| 10 | + | ||
| 11 | + tile.rs:5216 enc.symbol(TX_TYPE_DCT_DCT_SET2, tx_type); // a constant | ||
| 12 | + | ||
| 13 | +and the search never varied a luma type either -- `search_block` and | ||
| 14 | +`code_tx_depth` both go through `code_from_prediction`/`trial`, which pass | ||
| 15 | +`TxType::DctDct` (`encode.rs:1993`, `:2278`). The only non-DCT types this | ||
| 16 | +encoder has ever produced are CHROMA's, which code no symbol at all: the | ||
| 17 | +decoder derives them from the chroma mode (`search_chroma` -> | ||
| 18 | +`default_intra_tx_type`). | ||
| 19 | + | ||
| 20 | +STALE PREMISE IN THE CHARTER, corrected: this encoder writes | ||
| 21 | +`reduced_tx_set: true` (`encode.rs:1660`, `:1760`) -- not 0. Under that bit | ||
| 22 | +`get_tx_set` gives, per block class: | ||
| 23 | + | ||
| 24 | +| block class | tx size | set the WRITER uses | alphabet | types the search tried (before) | now | | ||
| 25 | +|---|---|---|---|---|---| | ||
| 26 | +| intra luma | 4x4, 8x8, 16x16 | `TX_SET_INTRA_2` (`intra_tx_type_{4,8,16}`) | 5 | DCT_DCT only | all 5 | | ||
| 27 | +| intra luma | 32x32, 64x64 | none (`TxbSet::Luma{32,64}.tx_type == None`) | 1 | -- | -- | | ||
| 28 | +| inter luma | 4x4..32x32 | `TX_SET_INTER_3` (`inter_tx_type_*`) | 2 (IDTX, DCT_DCT) | DCT_DCT only | DCT_DCT only | | ||
| 29 | +| chroma | any | none, derived from uv_mode | -- | mode's default | unchanged | | ||
| 30 | + | ||
| 31 | +So the bytes reachable WITHOUT touching the frame header were the four | ||
| 32 | +non-DCT members of `TX_SET_INTRA_2` on every intra luma unit at 16x16 and | ||
| 33 | +below -- an alphabet the writer was already coding into, one symbol wide, and | ||
| 34 | +never using. That is what this lane spends. | ||
| 35 | + | ||
| 36 | +Sets 13/17 (`DTT9_IDTX_1DDCT`, `ALL16`) that the charter names are NOT | ||
| 37 | +reachable from here: they need `reduced_tx_set == 0` in the frame header, | ||
| 38 | +which changes the alphabet of EVERY luma `tx_type` symbol in the stream | ||
| 39 | +(`TxbSet::Luma*Set1`/`Set2` exist on the decode side with their CDFs; the | ||
| 40 | +writer's set map at `tile.rs:4208` would have to take the bit). Deferred -- | ||
| 41 | +see below. | ||
| 42 | + | ||
| 43 | +## 2. Forward kernels | ||
| 44 | + | ||
| 45 | +`forward_transform_2d_typed` is basis-driven (`TxType::axes`), so | ||
| 46 | +`V_DCT`/`H_DCT`/`V_ADST`/`H_ADST` and the plain ADST family already had | ||
| 47 | +kernels; the FLIPADST family was refused by a `debug_assert`. A flipped axis | ||
| 48 | +is the plain ADST kernel with the DECODER's output mirrored (`ud_flip` -> | ||
| 49 | +row `h-1-i`, `lr_flip` -> column `w-1-j`), and a mirror is its own inverse, | ||
| 50 | +so the analysis half is the same basis over the MIRRORED residual -- 12 lines, | ||
| 51 | +no new kernel. | ||
| 52 | + | ||
| 53 | +Check: `transform::tests::a_typed_forward_round_trips_like_the_dct_one`, now | ||
| 54 | +over all SIXTEEN types at 4x4/8x8/16x16 (was 3), each round trip's rmse under | ||
| 55 | +2x `DCT_DCT`'s. PASS. | ||
| 56 | + | ||
| 57 | +## 3. The search + the writer | ||
| 58 | + | ||
| 59 | +* `encode::tx_type_candidates(set)` returns exactly the alphabet the writer | ||
| 60 | + can name for that set, so a searched type is always codable. | ||
| 61 | +* `code_tx_depth` picks a type PER TRANSFORM UNIT (RD: sse + lambda * bits, | ||
| 62 | + the trial's bits carrying the `tx_type` symbol itself through | ||
| 63 | + `coeff_bits_typed`/`rdoq`), stored at `tu_row * n + tu_col` -- the writer's | ||
| 64 | + own index, so a unit the frame edge cuts away cannot slide the rest. | ||
| 65 | +* `search_block` picks one for a whole-block transform, on the mode and angle | ||
| 66 | + already settled (libaom's ordering, the same shape the angle refinement has). | ||
| 67 | +* `BlockCoeffs::luma_tx_types` carries them to `write_luma_tus` / | ||
| 68 | + `write_block_planes`; empty = `DCT_DCT` throughout, which is every inter, | ||
| 69 | + palette and filter-intra block. | ||
| 70 | +* The symbol comes from the DECODER's own map (`decode::tx_type_symbol`, | ||
| 71 | + keyed by the CDF's alphabet width), so writer and reader cannot drift -- | ||
| 72 | + and it already answers for the 7/12/16-symbol sets a `reduced_tx_set == 0` | ||
| 73 | + lane would turn on. | ||
| 74 | +* Lever: `speed::TX_TYPE_SEARCH` (presets 0-6 on, 7-10 off) AND a content | ||
| 75 | + gate -- SCREEN FRAMES ONLY, see the gate table below. `EC_AV1_TXSET=0` | ||
| 76 | + forces it off entirely. | ||
| 77 | + | ||
| 78 | +## 4. Checks | ||
| 79 | + | ||
| 80 | +| check | result | | ||
| 81 | +|---|---| | ||
| 82 | +| `transform::tests::a_typed_forward_round_trips_like_the_dct_one` (16 types) | PASS | | ||
| 83 | +| `encoder::tests::an_edge_clip_codes_every_reduced_set_tx_type_both_decoders_read_exactly` | PASS -- hits `[IDTX 12, DCT_DCT 15445, ADST_ADST 70, ADST_DCT 161, DCT_ADST 467]`, ours == ffmpeg every sample | | ||
| 84 | +| `cargo test -p ec-av1 --lib --release round_trip` (19 tests) | PASS | | ||
| 85 | +| `predicted_coeff_bits_track_the_tile_the_writer_wrote`, `tile_bytes_do_not_depend_on_the_thread_count --include-ignored`, `the_facade_codes_the_same_bytes_as_encode_sequence` | PASS | | ||
| 86 | +| `cargo check -p ec-av1 --all-targets` | 0 errors, 0 warnings | | ||
| 87 | + | ||
| 88 | + | ||
| 89 | +## 5. The 12-frame gate (`encode::tests::bd_rate_screen_native`, 12 frames, gop 12) | ||
| 90 | + | ||
| 91 | +Control re-run on this head first (`EC_AV1_TXSET=0`), arm = the search on for | ||
| 92 | +every frame. BD-rate vs libaom cpu-used 6 / vs rav1e speed 6, lower is better; | ||
| 93 | +wall is ours per row. | ||
| 94 | + | ||
| 95 | +| clip | control | arm (all frames) | delta | wall ctl -> arm | | ||
| 96 | +|---|---|---|---|---| | ||
| 97 | +| bars 1080p | -1.0% / -16.8% | -2.3% / -17.8% | -1.3 / -1.0 | 247.2s -> 280.4s | | ||
| 98 | +| bars 2160p | +9.4% / -12.7% | +10.2% / -11.6% | +0.8 / +1.1 | 168.8s -> 180.7s | | ||
| 99 | +| film A | +21.7% / -4.4% | +22.1% / -4.0% | +0.4 / +0.4 | 153.9s -> 203.9s | | ||
| 100 | +| film B | +26.9% / -0.6% | +28.1% / +0.7% | +1.2 / +1.3 | 133.5s -> 164.0s | | ||
| 101 | +| screen capture | +27.8% / -26.0% | **+20.8% / -30.1%** | **-7.0 / -4.1** | 83.7s -> 120.6s | | ||
| 102 | + | ||
| 103 | +KEEP RULE VERDICT on the unconditional arm: FAIL. The rule wants both film | ||
| 104 | +rows down; both go UP (film A +0.4/+0.4, film B +1.2/+1.3). The capture wins | ||
| 105 | +seven points against libaom and four against rav1e -- the largest single | ||
| 106 | +screen move this lane has on record -- so the search ships behind the SAME | ||
| 107 | +content gate palette, intrabc and the angle-delta refinement already stand | ||
| 108 | +behind (`search.screen`): a non-screen frame is byte-identical to the encoder | ||
| 109 | +before the lane. | ||
| 110 | + | ||
| 111 | +Class note for the film loss: an ADST/IDTX unit is a locally cheaper | ||
| 112 | +reconstruction that the next picture then predicts from -- the same | ||
| 113 | +`local-rd-on-references` shape lane-fitu measured for filter intra, which is | ||
| 114 | +why the film rows lose while the intra-heavy capture wins. | ||
| 115 | + | ||
| 116 | +### Shipped state (screen gate on), confirm run | ||
| 117 | + | ||
| 118 | +| clip | control | shipped | note | | ||
| 119 | +|---|---|---|---| | ||
| 120 | +| bars 1080p | -1.0% / -16.8% | -1.0% / -16.8% | byte-identical (194497/299067/419042/567789 B) | | ||
| 121 | +| bars 2160p | +9.4% / -12.7% | +9.4% / -12.7% | byte-identical (205409/321883/435765/564573 B) | | ||
| 122 | +| film A | +21.7% / -4.4% | +21.7% / -4.4% | byte-identical (64392/102508/170169/413430 B) | | ||
| 123 | +| film B | +26.9% / -0.6% | +26.9% / -0.6% | byte-identical (27442/50374/105485/296813 B) | | ||
| 124 | +| screen capture | +27.8% / -26.0% | **+20.8% / -30.1%** | 36775/45758/57095/71711 B, wall 83.7s -> 96.2s (+15%) | | ||
| 125 | + | ||
| 126 | +Every non-screen row lands on the control's own byte counts to the digit -- | ||
| 127 | +the gate is the frame classifier, and the four non-capture clips classify 0 | ||
| 128 | +screen frames of 4 -- so nothing outside screen content moves, pins included. | ||
| 129 | + | ||
| 130 | +Pins: `encode::tests::the_encoders_own_streams_are_byte_identical_to_their_pins` | ||
| 131 | +PASSES UNCHANGED at 8562 / 33357 -- the pin clip is not screen, so nothing to | ||
| 132 | +re-pin. | ||
| 133 | + | ||
| 134 | +## 6. Preset lever and invariants | ||
| 135 | + | ||
| 136 | +`speed::TX_TYPE_SEARCH = [true x7, false x4]` (presets 0-6 on) on top of the | ||
| 137 | +screen content gate. The FIVE-type fire count is preset 0's: at preset 6 the | ||
| 138 | +mode/partition pruning removes the blocks `ADST_ADST` and `IDTX` were winning | ||
| 139 | +on (hits `[0, 6192, 0, 79, 284, ..]`), so the witness asserts the full set at | ||
| 140 | +preset 0 and "some non-`DCT_DCT` type fires" at every preset that carries the | ||
| 141 | +lever. | ||
| 142 | + | ||
| 143 | +| invariant | preset 0 | preset 6 | | ||
| 144 | +|---|---|---| | ||
| 145 | +| `an_edge_clip_codes_every_reduced_set_tx_type_both_decoders_read_exactly` | PASS | PASS | | ||
| 146 | +| `tile_bytes_do_not_depend_on_the_thread_count --include-ignored` | PASS | PASS | | ||
| 147 | +| `the_facade_codes_the_same_bytes_as_encode_sequence` | PASS | PASS | | ||
| 148 | +| `predicted_coeff_bits_track_the_tile_the_writer_wrote` | PASS | PASS | | ||
| 149 | +| all four above with `EC_COMP_MISMATCH=1` | 0 mismatches | 0 mismatches | | ||
| 150 | + | ||
| 151 | +`every_speed_preset_decodes_sample_exact_through_both_decoders --ignored` | ||
| 152 | +PASS (with `EC_COMP_MISMATCH=1`). | ||
| 153 | +`the_encoders_own_streams_are_byte_identical_to_their_pins`: PASS at the | ||
| 154 | +UNCHANGED 8562 / 33357 -- no re-pin, the pin clip is not screen content. | ||
| 155 | + | ||
| 156 | +## 7. The long-GOP gate at what ships, and the suite | ||
| 157 | + | ||
| 158 | +`encode::tests::bd_rate_film_long_gop` (48 pictures, gop 48) on the shipped | ||
| 159 | +state -- both rows are non-screen and therefore byte-identical to the encoder | ||
| 160 | +before the lane, which the 12-frame confirm run proves clip by clip: | ||
| 161 | + | ||
| 162 | +| clip | shipped | wall ours:libaom:rav1e | | ||
| 163 | +|---|---|---| | ||
| 164 | +| film A | +26.4% / -6.6% | 713.0s:38.4s:60.4s | | ||
| 165 | +| film B | +89.8% / +9.1% | 459.0s:71.9s:59.0s | | ||
| 166 | + | ||
| 167 | +Full `ec-av1` suite, detached: **567 passed / 1 failed / 45 ignored** | ||
| 168 | +(943s). The one failure is `stream::tests::no_per_frame_state_is_thread_local_any_more` | ||
| 169 | +-- `["encode.rs: SB128"]`, main's own red from the sb128 merge that a fix lane | ||
| 170 | +owns, not this lane's. `timeout 900 cargo check --workspace --all-targets -j4`: | ||
| 171 | +0 errors, 0 `ec-av1` warnings. | ||
| 172 | + | ||
| 173 | +## 8. Deferred | ||
| 174 | + | ||
| 175 | +* `deferred: inter luma IDTX vs DCT_DCT (the two-type TX_SET_INTER_3 the | ||
| 176 | + writer already codes) -- the plumbing is done (an inter block's | ||
| 177 | + BlockCoeffs carries luma_tx_types through the same writer path), what is | ||
| 178 | + missing is the candidate loop in code_from_prediction/commit_inter_luma and | ||
| 179 | + a gate round -- unblocked by ~40 min of gate wall; the screen row is where | ||
| 180 | + it would pay, same as the intra half.` | ||
| 181 | +* `deferred: reduced_tx_set = 0 and the 13/17 alphabets (DTT9_IDTX_1DDCT, | ||
| 182 | + ALL16) -- the FORWARD kernels are DONE and round-trip tested (all sixteen | ||
| 183 | + types), decode::tx_type_symbol already answers for the 7/12/16-symbol sets, | ||
| 184 | + and the decoder reads them; what is missing is the frame-header bit plus | ||
| 185 | + the writer's set map (tile.rs:4208 -> the Luma*Set1/Set2 variants) and a | ||
| 186 | + rav1e-speed-6-shaped candidate list per class -- unblocked by a lane of its | ||
| 187 | + own. Note the shape this lane measured first: the wider a set gets, the | ||
| 188 | + more the FILM rows lose (this five-type set already costs film A +0.4 and | ||
| 189 | + film B +1.2), so that lane should gate by content from the start rather | ||
| 190 | + than measure an unconditional arm.` | ||
| 191 | +* `deferred: libaom's own tx_type histogram on film B (syntax_census) -- the | ||
| 192 | + charter's instrument half. Not run: the two gate slots were the budget, and | ||
| 193 | + the decision it would have informed (which types to offer) was settled by | ||
| 194 | + the gate itself. Unblocked by one census run.` | ||