Merge branch 'lane-txrd' (48fcabf8): an inter block's chroma transform type is derived from the luma type; the encoder now re-codes chroma at the inherited type
Browse files at this commit4 changed files +467 -9
- crates/ec-av1/src/decode.rs +1 -1
- crates/ec-av1/src/encode.rs +288 -8
- crates/ec-av1/src/transform.rs +36 -0
- lanes/txrd.report.md +142 -0
crates/ec-av1/src/decode.rsmodified+1 -1
| @@ -18428,7 +18428,7 @@ fn read_inter_luma8( | ||
| 18428 | 18428 | /// both `TX_CLASS_2D`, so that mistake left the entropy stream perfectly in |
| 18429 | 18429 | /// sync and only corrupted the inverse transform (one 64x64 superblock of |
| 18430 | 18430 | /// high-frequency chroma noise, luma bit-exact). |
| 18431 | -fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize, fctx: &crate::decode::FrameCtx) -> TxType { | |
| 18431 | +pub(crate) fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize, fctx: &crate::decode::FrameCtx) -> TxType { | |
| 18432 | 18432 | use TxType::*; |
| 18433 | 18433 | let sqr_up = w.max(h); |
| 18434 | 18434 | let sqr = w.min(h); |
crates/ec-av1/src/encode.rsmodified+288 -8
| @@ -3839,6 +3839,42 @@ fn tx_depth_bits(side: usize, depth: usize) -> f64 { | ||
| 3839 | 3839 | pub(crate) static INTER_TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] = |
| 3840 | 3840 | [const { std::sync::atomic::AtomicUsize::new(0) }; 16]; |
| 3841 | 3841 | |
| 3842 | +/// lane-txrd: a multiplier on the rate term of the TRANSFORM-TYPE decision | |
| 3843 | +/// only (`EC_AV1_TXRD_LAMBDA`, default 1.0 = the mode search's own weight). | |
| 3844 | +/// `0` compares types on reconstruction error alone, which is what separates | |
| 3845 | +/// a rate-term calibration (class `rd-rate-term-calibration`) from a | |
| 3846 | +/// distortion the trial mis-measures. | |
| 3847 | +pub(crate) fn tx_type_lambda_mult() -> f64 { | |
| 3848 | + static M: std::sync::LazyLock<f64> = std::sync::LazyLock::new(|| { | |
| 3849 | + crate::envflags::var("EC_AV1_TXRD_LAMBDA") | |
| 3850 | + .ok() | |
| 3851 | + .and_then(|v| v.parse().ok()) | |
| 3852 | + .unwrap_or(1.0) | |
| 3853 | + }); | |
| 3854 | + *M | |
| 3855 | +} | |
| 3856 | + | |
| 3857 | +/// lane-txrd instrument: over the FLAT inter luma decision, the summed | |
| 3858 | +/// reconstruction error and priced bits of the type the search chose against | |
| 3859 | +/// the `DCT_DCT` trial it was compared with. Both are `* 64` so they fit an | |
| 3860 | +/// integer counter. `EC_AV1_TXRD_CENSUS`. | |
| 3861 | +pub(crate) static TXRD_STATS: [std::sync::atomic::AtomicU64; 7] = [ | |
| 3862 | + std::sync::atomic::AtomicU64::new(0), | |
| 3863 | + std::sync::atomic::AtomicU64::new(0), | |
| 3864 | + std::sync::atomic::AtomicU64::new(0), | |
| 3865 | + std::sync::atomic::AtomicU64::new(0), | |
| 3866 | + std::sync::atomic::AtomicU64::new(0), | |
| 3867 | + std::sync::atomic::AtomicU64::new(0), | |
| 3868 | + std::sync::atomic::AtomicU64::new(0), | |
| 3869 | +]; | |
| 3870 | + | |
| 3871 | +/// Reads [`TXRD_STATS`] and zeroes it. Only the gate reads it, so the lib | |
| 3872 | +/// build would warn `dead_code` (class from lane-txset2's `coeff_bits`). | |
| 3873 | +#[cfg(test)] | |
| 3874 | +pub(crate) fn take_txrd_stats() -> [u64; 7] { | |
| 3875 | + std::array::from_fn(|i| TXRD_STATS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 3876 | +} | |
| 3877 | + | |
| 3842 | 3878 | /// Reads [`INTER_TX_TYPE_HITS`] and zeroes it. |
| 3843 | 3879 | #[cfg(test)] |
| 3844 | 3880 | pub(crate) fn take_inter_tx_type_hits() -> [usize; 16] { |
| @@ -3964,6 +4000,52 @@ fn txfm_split_bits(side: usize, depth: usize) -> f64 { | ||
| 3964 | 4000 | + 4.0 * symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx / 2)], 0) |
| 3965 | 4001 | } |
| 3966 | 4002 | |
| 4003 | +/// spec 5.11.40 / libaom `av1_get_tx_type`: an INTER block's chroma transform | |
| 4004 | +/// type is never coded -- the decoder takes the LUMA type of the block's | |
| 4005 | +/// chroma-reference position and reduces it to the set the chroma size allows | |
| 4006 | +/// ([`crate::decode::reduce_inherited_chroma_tx_type`]). This encoder | |
| 4007 | +/// quantised chroma under a `DCT_DCT` basis whatever luma chose, so every | |
| 4008 | +/// non-`DCT_DCT` luma winner had its chroma levels inverse-transformed on a | |
| 4009 | +/// basis they were never produced for: on the bars gate row the luma of the | |
| 4010 | +/// unconditional tx-type arm came out BETTER on every frame (53.66 -> 53.70 | |
| 4011 | +/// dB) while V lost seven decibels (52.52 -> 45.53) -- the whole 20-point BD | |
| 4012 | +/// collapse two lanes attributed to the type PRICE (class | |
| 4013 | +/// `parsed then discarded` / `override slot on one arm`: a decoder-derived | |
| 4014 | +/// field the encoder never mirrored). | |
| 4015 | +/// | |
| 4016 | +/// Re-codes the two chroma planes of a committed inter block at the type the | |
| 4017 | +/// decoder will use, `None` when the inherited type reduces to `DCT_DCT` and | |
| 4018 | +/// the trials the caller already holds are right. | |
| 4019 | +#[allow(clippy::too_many_arguments)] | |
| 4020 | +fn recode_inter_chroma( | |
| 4021 | + chroma: &[Plane; 2], | |
| 4022 | + (x, y): (usize, usize), | |
| 4023 | + side: usize, | |
| 4024 | + mv: (i32, i32), | |
| 4025 | + refs: [(&[u16], usize, usize, usize); 2], | |
| 4026 | + second: Option<((i32, i32), [(&[u16], usize, usize, usize); 2])>, | |
| 4027 | + search: &Search, | |
| 4028 | + set: TxbSet, | |
| 4029 | + luma_tx_type: TxType, | |
| 4030 | + fctx: &crate::decode::FrameCtx, | |
| 4031 | +) -> Option<[Trial; 2]> { | |
| 4032 | + let c = side / 2; | |
| 4033 | + let tx = crate::decode::reduce_inherited_chroma_tx_type(luma_tx_type, c.min(32), c.min(32), fctx); | |
| 4034 | + if tx == TxType::DctDct { | |
| 4035 | + return None; | |
| 4036 | + } | |
| 4037 | + Some([0usize, 1].map(|p| match second { | |
| 4038 | + Some((mv1, r1)) => mc_trial_compound_typed( | |
| 4039 | + &chroma[p], x / 2, y / 2, c, (mv, mv1), false, refs[p], r1[p], search.base_q_idx, | |
| 4040 | + search.deadzone, set, tx, fctx, | |
| 4041 | + ), | |
| 4042 | + None => mc_trial_typed( | |
| 4043 | + &chroma[p], x / 2, y / 2, c, mv, false, refs[p].0, refs[p].1, refs[p].2, refs[p].3, | |
| 4044 | + false, search.base_q_idx, search.deadzone, set, tx, fctx, | |
| 4045 | + ), | |
| 4046 | + })) | |
| 4047 | +} | |
| 4048 | + | |
| 3967 | 4049 | /// The inter block's own var-tx decision, once its motion vector is settled: |
| 3968 | 4050 | /// its residual as ONE transform over the whole block, or as the four |
| 3969 | 4051 | /// transforms of half the side the tree's first halving names. The prediction |
| @@ -4003,7 +4085,8 @@ fn commit_inter_luma( | ||
| 4003 | 4085 | let mut flat_type = TxType::DctDct; |
| 4004 | 4086 | if !skip { |
| 4005 | 4087 | if let Some(fset) = inter_luma_set(side) { |
| 4006 | - let cost = |t: &Trial| t.sse + search.lambda * t.bits; | |
| 4088 | + let lambda = search.lambda * tx_type_lambda_mult(); | |
| 4089 | + let cost = |t: &Trial| t.sse + lambda * t.bits; | |
| 4007 | 4090 | for &tx_type in inter_tx_type_candidates(fset, search.screen) { |
| 4008 | 4091 | if tx_type == TxType::DctDct { |
| 4009 | 4092 | continue; |
| @@ -4035,6 +4118,45 @@ fn commit_inter_luma( | ||
| 4035 | 4118 | } |
| 4036 | 4119 | }; |
| 4037 | 4120 | let flat_ref: &Trial = flat_typed.as_ref().unwrap_or(flat); |
| 4121 | + if crate::envflags::env_flag!("EC_AV1_TXRD_CENSUS") && !skip { | |
| 4122 | + use std::sync::atomic::Ordering::Relaxed; | |
| 4123 | + TXRD_STATS[0].fetch_add(1, Relaxed); | |
| 4124 | + TXRD_STATS[1].fetch_add(flat_ref.sse as u64, Relaxed); | |
| 4125 | + TXRD_STATS[2].fetch_add(flat.sse as u64, Relaxed); | |
| 4126 | + TXRD_STATS[3].fetch_add((flat_ref.bits * 64.0) as u64, Relaxed); | |
| 4127 | + TXRD_STATS[4].fetch_add((flat.bits * 64.0) as u64, Relaxed); | |
| 4128 | + // Is the `DCT_DCT` baseline the search compares against the trial | |
| 4129 | + // THIS function would build for the same block? A stale or | |
| 4130 | + // differently-predicted baseline is what would let a candidate win on | |
| 4131 | + // both terms at once (class `local-rd-on-references` vs a real | |
| 4132 | + // pricing defect); re-predicting it here is the only way to tell. | |
| 4133 | + if let Some(fset) = inter_luma_set(side) { | |
| 4134 | + let here = match second { | |
| 4135 | + Some((mv1, ref1)) => mc_trial_compound_typed( | |
| 4136 | + luma, x, y, side, (mv, mv1), true, reference, ref1, search.base_q_idx, | |
| 4137 | + search.deadzone, fset, TxType::DctDct, fctx, | |
| 4138 | + ), | |
| 4139 | + None => mc_trial_typed( | |
| 4140 | + luma, x, y, side, mv, true, reference.0, reference.1, reference.2, | |
| 4141 | + reference.3, false, search.base_q_idx, search.deadzone, fset, | |
| 4142 | + TxType::DctDct, fctx, | |
| 4143 | + ), | |
| 4144 | + }; | |
| 4145 | + TXRD_STATS[5].fetch_add(here.sse as u64, Relaxed); | |
| 4146 | + TXRD_STATS[6].fetch_add((here.bits * 64.0) as u64, Relaxed); | |
| 4147 | + static DUMPED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); | |
| 4148 | + let n = TXRD_STATS[0].load(Relaxed); | |
| 4149 | + if here.sse * 2.0 < flat.sse && DUMPED.fetch_add(1, Relaxed) < 30 { | |
| 4150 | + eprintln!( | |
| 4151 | + "TXRD block {n} at ({x},{y}) side={side} compound={} mv={mv:?} \ | |
| 4152 | + flat sse={:.0} bits={:.1} | here sse={:.0} bits={:.1} | chosen {flat_type:?} \ | |
| 4153 | + sse={:.0} bits={:.1}", | |
| 4154 | + second.is_some(), flat.sse, flat.bits, here.sse, here.bits, | |
| 4155 | + flat_ref.sse, flat_ref.bits, | |
| 4156 | + ); | |
| 4157 | + } | |
| 4158 | + } | |
| 4159 | + } | |
| 4038 | 4160 | // What the caller's already-counted `flat` cost has to be corrected by |
| 4039 | 4161 | // when a non-`DCT_DCT` type won it, the same way the split delta below is. |
| 4040 | 4162 | let flat_gain = (flat_ref.sse + search.lambda * flat_ref.bits) |
| @@ -4087,7 +4209,7 @@ fn commit_inter_luma( | ||
| 4087 | 4209 | let mut best: Option<(f64, TxType, Trial)> = None; |
| 4088 | 4210 | for &tx_type in inter_tx_type_candidates(set, search.screen) { |
| 4089 | 4211 | let candidate = unit(tx_type); |
| 4090 | - let cost = candidate.sse + search.lambda * candidate.bits; | |
| 4212 | + let cost = candidate.sse + search.lambda * tx_type_lambda_mult() * candidate.bits; | |
| 4091 | 4213 | if best.as_ref().is_none_or(|(b, _, _)| cost < *b) { |
| 4092 | 4214 | best = Some((cost, tx_type, candidate)); |
| 4093 | 4215 | } |
| @@ -6177,6 +6299,30 @@ fn code_square_inter( | ||
| 6177 | 6299 | search, &luma_new, skip_new, second, fctx, |
| 6178 | 6300 | ) |
| 6179 | 6301 | }; |
| 6302 | + // lane-txrd: chroma follows the luma type the search just chose. | |
| 6303 | + let [u_new, v_new] = match luma_tx_types.first().copied() { | |
| 6304 | + Some(t) => recode_inter_chroma( | |
| 6305 | + chroma, (x, y), side, info.mv, | |
| 6306 | + [ | |
| 6307 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | |
| 6308 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | |
| 6309 | + ], | |
| 6310 | + info.ref1.filter(|_| motion_won == 0).and_then(|r| { | |
| 6311 | + refs[r as usize].map(|g| { | |
| 6312 | + ( | |
| 6313 | + info.mv1, | |
| 6314 | + [ | |
| 6315 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | |
| 6316 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | |
| 6317 | + ], | |
| 6318 | + ) | |
| 6319 | + }) | |
| 6320 | + }), | |
| 6321 | + search, chroma_set, t, fctx, | |
| 6322 | + ) | |
| 6323 | + .unwrap_or([u_new, v_new]), | |
| 6324 | + None => [u_new, v_new], | |
| 6325 | + }; | |
| 6180 | 6326 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); |
| 6181 | 6327 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); |
| 6182 | 6328 | return ( |
| @@ -6215,6 +6361,20 @@ fn code_square_inter( | ||
| 6215 | 6361 | let (levels, tx_depth, dcost, luma_tx_types) = commit_inter_luma( |
| 6216 | 6362 | luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, None, fctx, |
| 6217 | 6363 | ); |
| 6364 | + // lane-txrd: chroma follows the luma type (this tail's block is | |
| 6365 | + // single-reference `NEARESTMV`, so no compound second half). | |
| 6366 | + let [u, v] = match luma_tx_types.first().copied() { | |
| 6367 | + Some(t) => recode_inter_chroma( | |
| 6368 | + chroma, (x, y), side, mv, | |
| 6369 | + [ | |
| 6370 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | |
| 6371 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | |
| 6372 | + ], | |
| 6373 | + None, search, chroma_set, t, fctx, | |
| 6374 | + ) | |
| 6375 | + .unwrap_or([u, v]), | |
| 6376 | + None => [u, v], | |
| 6377 | + }; | |
| 6218 | 6378 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 6219 | 6379 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 6220 | 6380 | (BlockCoeffs { |
| @@ -9522,6 +9682,38 @@ fn search_inter_block( | ||
| 9522 | 9682 | (best.luma.levels.clone(), 0, 0.0, Vec::new()) |
| 9523 | 9683 | } |
| 9524 | 9684 | }; |
| 9685 | + // lane-txrd: chroma follows the luma type the var-tx/type search chose. | |
| 9686 | + let [bu, bv] = match (best.inter.filter(|_| motion_won == 0), luma_tx_types.first().copied()) { | |
| 9687 | + (Some(info), Some(t)) => { | |
| 9688 | + let (ou, ov) = match extra.iter().find(|(r, _, _)| *r == info.ref_frame) { | |
| 9689 | + Some(&(_, g, _)) => ( | |
| 9690 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | |
| 9691 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | |
| 9692 | + ), | |
| 9693 | + None => ( | |
| 9694 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | |
| 9695 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | |
| 9696 | + ), | |
| 9697 | + }; | |
| 9698 | + let sec = info.ref1.and_then(|r| { | |
| 9699 | + compound.iter().find(|(c, _, _)| *c == r).map(|&(_, g, _)| { | |
| 9700 | + ( | |
| 9701 | + info.mv1, | |
| 9702 | + [ | |
| 9703 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | |
| 9704 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | |
| 9705 | + ], | |
| 9706 | + ) | |
| 9707 | + }) | |
| 9708 | + }); | |
| 9709 | + recode_inter_chroma( | |
| 9710 | + chroma, (x, y), BLOCK, info.mv, [ou, ov], sec, search, chroma_set, t, fctx, | |
| 9711 | + ) | |
| 9712 | + .unwrap_or([best.u, best.v]) | |
| 9713 | + } | |
| 9714 | + _ => [best.u, best.v], | |
| 9715 | + }; | |
| 9716 | + let best = Candidate { u: bu, v: bv, ..best }; | |
| 9525 | 9717 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 9526 | 9718 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| 9527 | 9719 | ( |
| @@ -11200,6 +11392,41 @@ pub(crate) fn encode_inter_frame( | ||
| 11200 | 11392 | refs[4] = trial_golden.as_ref(); |
| 11201 | 11393 | refs[7] = trial_altref.as_ref(); |
| 11202 | 11394 | let refpix = crate::decode::RefPix::ready(refs); |
| 11395 | + // lane-txrd: the frame's luma error BEFORE the loop filters, i.e. the | |
| 11396 | + // sum the block search believed it was minimising. Against the | |
| 11397 | + // post-filter per-frame PSNR the gate prints, this separates a trial | |
| 11398 | + // whose reconstruction is a lie from a filter stage that undoes the win. | |
| 11399 | + if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { | |
| 11400 | + let sse: u64 = luma | |
| 11401 | + .reconstruction | |
| 11402 | + .chunks(luma.width) | |
| 11403 | + .take(luma.true_height) | |
| 11404 | + .zip(picture_y8.chunks(luma.width)) | |
| 11405 | + .flat_map(|(r, s)| r[..luma.true_width].iter().zip(&s[..luma.true_width])) | |
| 11406 | + .map(|(&r, &s)| { | |
| 11407 | + let d = i64::from(r) - i64::from(s); | |
| 11408 | + (d * d) as u64 | |
| 11409 | + }) | |
| 11410 | + .sum(); | |
| 11411 | + eprintln!("EC_PREFILTER q={base_q_idx} luma sse={sse}"); | |
| 11412 | + } | |
| 11413 | + let post = |luma: &Plane<'_>, tag: &str| { | |
| 11414 | + if !crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { | |
| 11415 | + return; | |
| 11416 | + } | |
| 11417 | + let sse: u64 = luma | |
| 11418 | + .reconstruction | |
| 11419 | + .chunks(luma.width) | |
| 11420 | + .take(luma.true_height) | |
| 11421 | + .zip(picture_y8.chunks(luma.width)) | |
| 11422 | + .flat_map(|(r, s)| r[..luma.true_width].iter().zip(&s[..luma.true_width])) | |
| 11423 | + .map(|(&r, &s)| { | |
| 11424 | + let d = i64::from(r) - i64::from(s); | |
| 11425 | + (d * d) as u64 | |
| 11426 | + }) | |
| 11427 | + .sum(); | |
| 11428 | + eprintln!("{tag} q={base_q_idx} luma sse={sse}"); | |
| 11429 | + }; | |
| 11203 | 11430 | pick_and_apply_filters( |
| 11204 | 11431 | &mut header, |
| 11205 | 11432 | &mut luma, |
| @@ -11255,6 +11482,7 @@ pub(crate) fn encode_inter_frame( | ||
| 11255 | 11482 | ) |
| 11256 | 11483 | }, |
| 11257 | 11484 | )?; |
| 11485 | + post(&luma, "EC_POSTFILTER"); | |
| 11258 | 11486 | let mut cdfs = end_cdfs |
| 11259 | 11487 | .into_inner() |
| 11260 | 11488 | .unwrap_or_else(|| start_cdfs.0.clone()); |
| @@ -14892,12 +15120,28 @@ mod tests { | ||
| 14892 | 15120 | ); |
| 14893 | 15121 | } |
| 14894 | 15122 | } |
| 14895 | - let mean: f64 = decoded | |
| 14896 | - .iter() | |
| 14897 | - .zip(source) | |
| 14898 | - .map(|(d, s)| psnr_all(d, s)) | |
| 14899 | - .sum::<f64>() | |
| 14900 | - / source.len() as f64; | |
| 15123 | + let per_frame: Vec<f64> = decoded.iter().zip(source).map(|(d, s)| psnr_all(d, s)).collect(); | |
| 15124 | + // lane-txrd: does a tool's loss start at the FIRST inter frame | |
| 15125 | + // (a local mispricing) or grow down the GOP (propagation into | |
| 15126 | + // the reference chain)? One line per q point answers it. | |
| 15127 | + if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { | |
| 15128 | + let row: Vec<String> = per_frame.iter().map(|p| format!("{p:.2}")).collect(); | |
| 15129 | + eprintln!("{name}: q={q} per-frame PSNR {}", row.join(" ")); | |
| 15130 | + // lane-txrd: per PLANE, because an all-plane mean hides a | |
| 15131 | + // collapse in one of them (class `metric blind to a plane`). | |
| 15132 | + let plane = |pick: fn(&Picture) -> &Vec<u16>| -> String { | |
| 15133 | + let v: Vec<String> = decoded | |
| 15134 | + .iter() | |
| 15135 | + .zip(source) | |
| 15136 | + .map(|(d, s)| format!("{:.2}", psnr(pick(d), pick(s)))) | |
| 15137 | + .collect(); | |
| 15138 | + v.join(" ") | |
| 15139 | + }; | |
| 15140 | + eprintln!("{name}: q={q} Y {}", plane(|p| &p.y)); | |
| 15141 | + eprintln!("{name}: q={q} U {}", plane(|p| &p.u)); | |
| 15142 | + eprintln!("{name}: q={q} V {}", plane(|p| &p.v)); | |
| 15143 | + } | |
| 15144 | + let mean: f64 = per_frame.iter().sum::<f64>() / source.len() as f64; | |
| 14901 | 15145 | ladder.push((mean, (encoded.stream.len() as f64).log10())); |
| 14902 | 15146 | } |
| 14903 | 15147 | // What this clip asked for and what the content gate left it coding |
| @@ -15981,6 +16225,42 @@ mod tests { | ||
| 15981 | 16225 | row("32x32 single", 4), |
| 15982 | 16226 | row("32x32 compound", 6), |
| 15983 | 16227 | ); |
| 16228 | + // lane-txrd: which types the two searches actually COMMITTED over | |
| 16229 | + // this row's four encodes -- the gate could not say whether a type | |
| 16230 | + // search fired at all (class `gate-blind-to-feature`). | |
| 16231 | + const NAMES: [&str; 16] = [ | |
| 16232 | + "IDTX", "DCT_DCT", "ADST_ADST", "ADST_DCT", "DCT_ADST", "V_DCT", "H_DCT", | |
| 16233 | + "FLIPADST_DCT", "DCT_FLIPADST", "FLIPADST_FLIPADST", "ADST_FLIPADST", | |
| 16234 | + "FLIPADST_ADST", "V_ADST", "H_ADST", "V_FLIPADST", "H_FLIPADST", | |
| 16235 | + ]; | |
| 16236 | + let name_of = |t: usize| NAMES[t]; | |
| 16237 | + for (label, hits) in [("inter", take_inter_tx_type_hits()), ("intra", take_tx_type_hits())] { | |
| 16238 | + let total: usize = hits.iter().sum(); | |
| 16239 | + let live: Vec<String> = hits | |
| 16240 | + .iter() | |
| 16241 | + .enumerate() | |
| 16242 | + .filter(|&(_, &n)| n > 0) | |
| 16243 | + .map(|(t, &n)| format!("{}={n} ({:.1}%)", name_of(t), 100.0 * n as f64 / total.max(1) as f64)) | |
| 16244 | + .collect(); | |
| 16245 | + eprintln!("{name}: {label} tx_type of {total} units -- {}", live.join(" ")); | |
| 16246 | + } | |
| 16247 | + let t = take_txrd_stats(); | |
| 16248 | + if t[0] > 0 { | |
| 16249 | + eprintln!( | |
| 16250 | + "{name}: txrd flat decisions={} chosen sse={} bits={:.0} | DCT_DCT sse={} \ | |
| 16251 | + bits={:.0} (dsse {:+.2}%, dbits {:+.2}%)", | |
| 16252 | + t[0], t[1], t[3] as f64 / 64.0, t[2], t[4] as f64 / 64.0, | |
| 16253 | + 100.0 * (t[1] as f64 - t[2] as f64) / t[2].max(1) as f64, | |
| 16254 | + 100.0 * (t[3] as f64 - t[4] as f64) / t[4].max(1) as f64, | |
| 16255 | + ); | |
| 16256 | + eprintln!( | |
| 16257 | + "{name}: txrd DCT_DCT re-predicted HERE sse={} bits={:.0} (vs the passed-in \ | |
| 16258 | + flat: dsse {:+.2}%, dbits {:+.2}%)", | |
| 16259 | + t[5], t[6] as f64 / 64.0, | |
| 16260 | + 100.0 * (t[5] as f64 - t[2] as f64) / t[2].max(1) as f64, | |
| 16261 | + 100.0 * (t[6] as f64 - t[4] as f64) / t[4].max(1) as f64, | |
| 16262 | + ); | |
| 16263 | + } | |
| 15984 | 16264 | } |
| 15985 | 16265 | |
| 15986 | 16266 | /// The video stream's coded size, from ffprobe. |
crates/ec-av1/src/transform.rsmodified+36 -0
| @@ -2152,6 +2152,42 @@ mod tests { | ||
| 2152 | 2152 | } |
| 2153 | 2153 | } |
| 2154 | 2154 | |
| 2155 | + #[test] | |
| 2156 | + #[ignore] | |
| 2157 | + fn txrd_gain_probe() { | |
| 2158 | + for side in [4usize, 8, 16, 32] { | |
| 2159 | + let residual: Vec<i32> = | |
| 2160 | + noise(side * side, 7 + side as u64).iter().map(|&v| v / 4).collect(); | |
| 2161 | + let all = [ | |
| 2162 | + TxType::DctDct, | |
| 2163 | + TxType::AdstDct, | |
| 2164 | + TxType::DctAdst, | |
| 2165 | + TxType::AdstAdst, | |
| 2166 | + TxType::Idtx, | |
| 2167 | + TxType::VDct, | |
| 2168 | + TxType::HDct, | |
| 2169 | + TxType::VAdst, | |
| 2170 | + TxType::HAdst, | |
| 2171 | + ]; | |
| 2172 | + for &tx in &all { | |
| 2173 | + if side > 16 && !matches!(tx, TxType::DctDct | TxType::Idtx) { | |
| 2174 | + continue; | |
| 2175 | + } | |
| 2176 | + let levels = forward_and_quantize_typed(&residual, side, 8, 20, 0.5, tx); | |
| 2177 | + let back = dequant_and_inverse_typed(&levels, side, 8, 20, 0, 0, tx); | |
| 2178 | + let num: f64 = | |
| 2179 | + residual.iter().zip(&back).map(|(&a, &b)| f64::from(a) * f64::from(b)).sum(); | |
| 2180 | + let den: f64 = residual.iter().map(|&a| f64::from(a) * f64::from(a)).sum(); | |
| 2181 | + let nz = levels.iter().filter(|&&l| l != 0).count(); | |
| 2182 | + let energy: f64 = levels.iter().map(|&l| f64::from(l.abs())).sum(); | |
| 2183 | + println!( | |
| 2184 | + "{side}x{side} {tx:?} alpha={:.4} nz={nz} sum|l|={energy}", | |
| 2185 | + num / den | |
| 2186 | + ); | |
| 2187 | + } | |
| 2188 | + } | |
| 2189 | + } | |
| 2190 | + | |
| 2155 | 2191 | /// The typed forward transform is the decoder's exact inverse undone: |
| 2156 | 2192 | /// quantized and taken back, an ADST-typed residual returns with the same |
| 2157 | 2193 | /// error a `DCT_DCT` one does. This is what pins the *gain* of the ADST |
lanes/txrd.report.mdadded+142 -0
| @@ -0,0 +1,142 @@ | ||
| 1 | +# lane-txrd -- why the unconditional transform-type search collapsed | |
| 2 | + | |
| 3 | +Branch `lane-txrd` off `01fa1fd5`. Predecessors: `lanes/txset.report.md` | |
| 4 | +(intra 5-type set), `lanes/txset2.report.md` (inter `DCT_IDTX`), both of | |
| 5 | +which shipped their search SCREEN-GATED because turning it on for all | |
| 6 | +content lost ~20 BD points on the two synthetic bars rows. | |
| 7 | + | |
| 8 | +## The finding | |
| 9 | + | |
| 10 | +The type search was never mispriced. **An inter block's chroma transform | |
| 11 | +type is not coded: the decoder derives it from the LUMA type at the block's | |
| 12 | +chroma-reference position** (spec 5.11.40, libaom `av1_get_tx_type`; our own | |
| 13 | +reader does it at `decode.rs:18234`, `reduce_inherited_chroma_tx_type`). | |
| 14 | +The encoder produced its chroma levels under a `DCT_DCT` basis whatever the | |
| 15 | +luma type search chose, so every non-`DCT_DCT` luma winner had its chroma | |
| 16 | +levels inverse-transformed on a basis they were never made for. | |
| 17 | + | |
| 18 | +Per-plane PSNR, bars 1080p, q=60, control vs the unconditional arm | |
| 19 | +(`EC_AV1_FRAME_PSNR=1`, frames 0..11): | |
| 20 | + | |
| 21 | +| plane | control frame 1..11 | unconditional arm | | |
| 22 | +|---|---|---| | |
| 23 | +| Y | 53.66 53.44 53.30 53.24 ... | **53.70 53.52 53.40 53.32 ...** (BETTER) | | |
| 24 | +| U | 51.97 51.89 51.78 51.55 ... | 49.15 48.19 47.74 47.57 ... | | |
| 25 | +| V | 52.52 52.35 52.25 52.14 ... | **45.53 44.19 44.16 42.82 ...** (-7 dB) | | |
| 26 | + | |
| 27 | +Luma is better on every frame -- the search's own win is real -- and the | |
| 28 | +all-plane mean the gate ranks on (`psnr_all` concatenates Y, U and V) reads | |
| 29 | +53.13 -> 50.20. Class `metric blind to a plane`, on top of | |
| 30 | +`parsed then discarded` / `override slot on one arm`: a decoder-DERIVED | |
| 31 | +field the encoder never mirrored. | |
| 32 | + | |
| 33 | +## The three chartered hypotheses, all refuted first | |
| 34 | + | |
| 35 | +1. **RDOQ's transform-domain distortion / typed gain.** The forward-inverse | |
| 36 | + gain was MEASURED per type and size (`alpha = <res, rec> / <res, res>` at | |
| 37 | + q_idx 20): 0.977 .. 1.030 over all sixteen types at 4/8/16 and both types | |
| 38 | + at 32. No type is mis-scaled, so the `(coeff - level*q)/8` model is as | |
| 39 | + valid for `IDTX` and the ADST kernels as for `DCT_DCT`. | |
| 40 | +2. **The rate term.** `EC_AV1_TXRD_LAMBDA=0` (types compared on | |
| 41 | + reconstruction error alone, rate weight zero) reproduces the collapse to | |
| 42 | + the digit: bars 1080p +24.2/+4.2 against the arm's +23.1/+3.3 and the | |
| 43 | + control's -1.0/-16.8. A rate-term calibration cannot be the cause of a | |
| 44 | + loss that survives the rate term being switched off. | |
| 45 | +3. **The loop filters.** Pre-filter frame luma SSE (`EC_PREFILTER`) is LOWER | |
| 46 | + in the arm on every frame at every quantizer (q=60 leaves: 26.02M vs the | |
| 47 | + control's 26.21M), and so is the post-filter one (25.47M vs 25.60M). The | |
| 48 | + filters were never the loss; the loss was in a plane nobody was reading. | |
| 49 | + | |
| 50 | +The census also showed the chosen types dominating the `DCT_DCT` trial on | |
| 51 | +BOTH terms (sse -5.5%, priced bits -19.1%) -- which is what a defect that | |
| 52 | +lives outside luma looks like from inside the luma search. | |
| 53 | + | |
| 54 | +## The fix | |
| 55 | + | |
| 56 | +`encode::recode_inter_chroma`: once `commit_inter_luma` has chosen the | |
| 57 | +block's luma type, the two chroma planes are re-coded at | |
| 58 | +`reduce_inherited_chroma_tx_type(luma_type, chroma_w, chroma_h)` -- the | |
| 59 | +decoder's own rule -- before they are committed. Wired at all three inter | |
| 60 | +commit sites that can carry a non-`DCT_DCT` luma type (the `code_square_inter` | |
| 61 | +winner and its intra-vs-inter tail, and `search_inter_block`'s 32x32 path); | |
| 62 | +a split block hands over `luma_tx_types[0]`, the top-left unit, which is the | |
| 63 | +position `xd->tx_type_map` is indexed at. An OBMC/warp winner commits the | |
| 64 | +flat trial with no type search, so its `luma_tx_types` is empty and nothing | |
| 65 | +is re-coded. | |
| 66 | + | |
| 67 | +SWEEP (same shape elsewhere): intra chroma derives its type from the UV MODE, | |
| 68 | +not from luma (`Intra_Mode_To_Tx_Type`), and the encoder already codes it that | |
| 69 | +way -- so the intra 5-type search of lane-txset is NOT affected. The only | |
| 70 | +other inheritance is intrabc's (`fctx.intrabc_chroma_tx`), whose luma type is | |
| 71 | +always `DCT_DCT` in this encoder. | |
| 72 | + | |
| 73 | +## Gates | |
| 74 | + | |
| 75 | +See the table appended below; the arms are running at the time of writing. | |
| 76 | + | |
| 77 | +### The 12-frame native gate (`bd_rate_screen_native`, 12 frames, gop 12) | |
| 78 | + | |
| 79 | +BD-rate vs libaom `cpu-used 6` / rav1e `speed 6`; lower is better. | |
| 80 | + | |
| 81 | +| clip | control (pre-lane) | unconditional BEFORE the fix | unconditional AFTER the fix | shipped (screen-gated) AFTER | | |
| 82 | +|---|---|---|---|---| | |
| 83 | +| bars 1080p | -1.0% / -16.8% | +23.1% / +3.3% | **-0.6% / -16.2%** | -1.0% / -16.8% | | |
| 84 | +| bars 2160p | +9.4% / -12.7% | +31.2% / +3.7% | **+10.3% / -11.7%** | +9.4% / -12.7% | | |
| 85 | +| film A | +21.7% / -4.4% | +21.5% / -4.5% | +21.6% / -4.5% | +21.7% / -4.4% | | |
| 86 | +| film B | +26.9% / -0.6% | +27.2% / -0.3% | +27.2% / -0.3% | +26.9% / -0.6% | | |
| 87 | +| screen capture | +20.1% / -30.4% (shipped) | +20.1% / -30.4% | +19.8% / -30.5% | **+19.8% / -30.5%** | | |
| 88 | + | |
| 89 | +The twenty-point collapse is GONE: the two bars rows come back to within | |
| 90 | +0.4/0.6 and 0.9/1.0 of the control instead of 24/20 points off it. | |
| 91 | + | |
| 92 | +DECISION: the unconditional arm still does NOT pass the keep rule (bars | |
| 93 | +1080p 0.4/0.6 down, bars 2160p 0.9/1.0 down, film B 0.3/0.3 down -- the | |
| 94 | +charter's bound was 0.3), so the inter search STAYS screen-gated and the | |
| 95 | +default is unchanged on every non-screen row: the four non-screen rows print | |
| 96 | +the control's own byte counts to the digit (194497/299067/419042/567789, | |
| 97 | +205409/321883/435765/564573, 64392/102508/170169/413430, | |
| 98 | +27442/50374/105485/296813). What the fix does change is the row the search | |
| 99 | +already ships on: the screen capture goes +20.1/-30.4 -> **+19.8/-30.5**, | |
| 100 | +i.e. the shipped screen-gated search was carrying this defect and is 0.3 | |
| 101 | +better against libaom with it repaired. | |
| 102 | + | |
| 103 | +What is left on the bars rows is now the real `local-rd-on-references` | |
| 104 | +residue, not a correctness bug -- a whole order of magnitude smaller than | |
| 105 | +what was attributed to the price. | |
| 106 | + | |
| 107 | +### Invariants | |
| 108 | + | |
| 109 | +* `cargo test -p ec-av1 --release --lib` (detached): **569 passed, 0 failed, | |
| 110 | + 46 ignored** (45 + this lane's `txrd_gain_probe` measurement). | |
| 111 | +* `cargo check --workspace --all-targets -j4`: 0 errors, 0 ec-av1 warnings. | |
| 112 | +* The BD gate itself asserts, at all four quantizers of all five rows, that | |
| 113 | + ffmpeg's decode AND our decoder's both equal the encoder's reconstruction | |
| 114 | + sample for sample -- three-way exact under the fix, in both arms. | |
| 115 | + | |
| 116 | +### Instruments this lane leaves | |
| 117 | + | |
| 118 | +* `EC_AV1_TXRD_LAMBDA=<f>` -- rate weight of the transform-TYPE decision | |
| 119 | + only (`0` = compare types on reconstruction error alone). | |
| 120 | +* `EC_AV1_TXRD_CENSUS=1` -- chosen vs `DCT_DCT` summed sse/bits over the flat | |
| 121 | + inter luma decisions, printed by the gate. | |
| 122 | +* `EC_AV1_FRAME_PSNR=1` -- per-frame all-plane AND per-plane (Y/U/V) PSNR per | |
| 123 | + ladder point, plus `EC_PREFILTER`/`EC_POSTFILTER` frame luma SSE around | |
| 124 | + `pick_and_apply_filters`, which is what separates a bad trial from a bad | |
| 125 | + filter choice. | |
| 126 | +* `transform::tests::txrd_gain_probe` (`--ignored`) -- the measured | |
| 127 | + forward-inverse gain per type and size. | |
| 128 | +* `every_speed_preset_decodes_sample_exact_through_both_decoders --ignored`: | |
| 129 | + PASS (presets 0..6, 27.7s). | |
| 130 | +* `an_inter_clip_codes_both_inter_set_tx_types_both_decoders_read_exactly` | |
| 131 | + under `EC_COMP_MISMATCH=1`: PASS, no mismatch line. | |
| 132 | + | |
| 133 | +### Deferred | |
| 134 | + | |
| 135 | +* `deferred: the unconditional inter type search -- still 0.4-1.0 BD points | |
| 136 | + short of the control on the two synthetic bars rows once the chroma defect | |
| 137 | + is out -- what unblocks it: the residual is `local-rd-on-references` (a | |
| 138 | + block whose type wins locally is a worse reference), so the lever is a | |
| 139 | + propagation-aware type cost (tpl weight per block), not another pricing fix.` | |
| 140 | +* `deferred: the intra 5-type search's own unconditional arm was not re-run | |
| 141 | + under this fix -- intra chroma does not inherit from luma, so nothing this | |
| 142 | + lane found applies to it; its screen gate stands on lane-txset's own table.` |