wip(av1): txrd lane snapshot at the turn cap (orchestrator)
Browse files at this commit888d58d6a1f93c66f764b026f10139d89728dc30Parent: f1e61202 changed files +193 -8
- crates/ec-av1/src/encode.rs +157 -8
- crates/ec-av1/src/transform.rs +36 -0
crates/ec-av1/src/encode.rsmodified+157 -8
| @@ -3802,6 +3802,40 @@ fn tx_depth_bits(side: usize, depth: usize) -> f64 { | |||
| 3802 | pub(crate) static INTER_TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] = | 3802 | pub(crate) static INTER_TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] = |
| 3803 | [const { std::sync::atomic::AtomicUsize::new(0) }; 16]; | 3803 | [const { std::sync::atomic::AtomicUsize::new(0) }; 16]; |
| 3804 | 3804 | ||
| 3805 | +/// lane-txrd: a multiplier on the rate term of the TRANSFORM-TYPE decision | ||
| 3806 | +/// only (`EC_AV1_TXRD_LAMBDA`, default 1.0 = the mode search's own weight). | ||
| 3807 | +/// `0` compares types on reconstruction error alone, which is what separates | ||
| 3808 | +/// a rate-term calibration (class `rd-rate-term-calibration`) from a | ||
| 3809 | +/// distortion the trial mis-measures. | ||
| 3810 | +pub(crate) fn tx_type_lambda_mult() -> f64 { | ||
| 3811 | + static M: std::sync::LazyLock<f64> = std::sync::LazyLock::new(|| { | ||
| 3812 | + crate::envflags::var("EC_AV1_TXRD_LAMBDA") | ||
| 3813 | + .ok() | ||
| 3814 | + .and_then(|v| v.parse().ok()) | ||
| 3815 | + .unwrap_or(1.0) | ||
| 3816 | + }); | ||
| 3817 | + *M | ||
| 3818 | +} | ||
| 3819 | + | ||
| 3820 | +/// lane-txrd instrument: over the FLAT inter luma decision, the summed | ||
| 3821 | +/// reconstruction error and priced bits of the type the search chose against | ||
| 3822 | +/// the `DCT_DCT` trial it was compared with. Both are `* 64` so they fit an | ||
| 3823 | +/// integer counter. `EC_AV1_TXRD_CENSUS`. | ||
| 3824 | +pub(crate) static TXRD_STATS: [std::sync::atomic::AtomicU64; 7] = [ | ||
| 3825 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3826 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3827 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3828 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3829 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3830 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3831 | + std::sync::atomic::AtomicU64::new(0), | ||
| 3832 | +]; | ||
| 3833 | + | ||
| 3834 | +/// Reads [`TXRD_STATS`] and zeroes it. | ||
| 3835 | +pub(crate) fn take_txrd_stats() -> [u64; 7] { | ||
| 3836 | + std::array::from_fn(|i| TXRD_STATS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | ||
| 3837 | +} | ||
| 3838 | + | ||
| 3805 | /// Reads [`INTER_TX_TYPE_HITS`] and zeroes it. | 3839 | /// Reads [`INTER_TX_TYPE_HITS`] and zeroes it. |
| 3806 | #[cfg(test)] | 3840 | #[cfg(test)] |
| 3807 | pub(crate) fn take_inter_tx_type_hits() -> [usize; 16] { | 3841 | pub(crate) fn take_inter_tx_type_hits() -> [usize; 16] { |
| @@ -3966,7 +4000,8 @@ fn commit_inter_luma( | |||
| 3966 | let mut flat_type = TxType::DctDct; | 4000 | let mut flat_type = TxType::DctDct; |
| 3967 | if !skip { | 4001 | if !skip { |
| 3968 | if let Some(fset) = inter_luma_set(side) { | 4002 | if let Some(fset) = inter_luma_set(side) { |
| 3969 | - let cost = |t: &Trial| t.sse + search.lambda * t.bits; | 4003 | + let lambda = search.lambda * tx_type_lambda_mult(); |
| 4004 | + let cost = |t: &Trial| t.sse + lambda * t.bits; | ||
| 3970 | for &tx_type in inter_tx_type_candidates(fset, search.screen) { | 4005 | for &tx_type in inter_tx_type_candidates(fset, search.screen) { |
| 3971 | if tx_type == TxType::DctDct { | 4006 | if tx_type == TxType::DctDct { |
| 3972 | continue; | 4007 | continue; |
| @@ -3998,6 +4033,45 @@ fn commit_inter_luma( | |||
| 3998 | } | 4033 | } |
| 3999 | }; | 4034 | }; |
| 4000 | let flat_ref: &Trial = flat_typed.as_ref().unwrap_or(flat); | 4035 | let flat_ref: &Trial = flat_typed.as_ref().unwrap_or(flat); |
| 4036 | + if crate::envflags::env_flag!("EC_AV1_TXRD_CENSUS") && !skip { | ||
| 4037 | + use std::sync::atomic::Ordering::Relaxed; | ||
| 4038 | + TXRD_STATS[0].fetch_add(1, Relaxed); | ||
| 4039 | + TXRD_STATS[1].fetch_add(flat_ref.sse as u64, Relaxed); | ||
| 4040 | + TXRD_STATS[2].fetch_add(flat.sse as u64, Relaxed); | ||
| 4041 | + TXRD_STATS[3].fetch_add((flat_ref.bits * 64.0) as u64, Relaxed); | ||
| 4042 | + TXRD_STATS[4].fetch_add((flat.bits * 64.0) as u64, Relaxed); | ||
| 4043 | + // Is the `DCT_DCT` baseline the search compares against the trial | ||
| 4044 | + // THIS function would build for the same block? A stale or | ||
| 4045 | + // differently-predicted baseline is what would let a candidate win on | ||
| 4046 | + // both terms at once (class `local-rd-on-references` vs a real | ||
| 4047 | + // pricing defect); re-predicting it here is the only way to tell. | ||
| 4048 | + if let Some(fset) = inter_luma_set(side) { | ||
| 4049 | + let here = match second { | ||
| 4050 | + Some((mv1, ref1)) => mc_trial_compound_typed( | ||
| 4051 | + luma, x, y, side, (mv, mv1), true, reference, ref1, search.base_q_idx, | ||
| 4052 | + search.deadzone, fset, TxType::DctDct, fctx, | ||
| 4053 | + ), | ||
| 4054 | + None => mc_trial_typed( | ||
| 4055 | + luma, x, y, side, mv, true, reference.0, reference.1, reference.2, | ||
| 4056 | + reference.3, false, search.base_q_idx, search.deadzone, fset, | ||
| 4057 | + TxType::DctDct, fctx, | ||
| 4058 | + ), | ||
| 4059 | + }; | ||
| 4060 | + TXRD_STATS[5].fetch_add(here.sse as u64, Relaxed); | ||
| 4061 | + TXRD_STATS[6].fetch_add((here.bits * 64.0) as u64, Relaxed); | ||
| 4062 | + static DUMPED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); | ||
| 4063 | + let n = TXRD_STATS[0].load(Relaxed); | ||
| 4064 | + if here.sse * 2.0 < flat.sse && DUMPED.fetch_add(1, Relaxed) < 30 { | ||
| 4065 | + eprintln!( | ||
| 4066 | + "TXRD block {n} at ({x},{y}) side={side} compound={} mv={mv:?} \ | ||
| 4067 | + flat sse={:.0} bits={:.1} | here sse={:.0} bits={:.1} | chosen {flat_type:?} \ | ||
| 4068 | + sse={:.0} bits={:.1}", | ||
| 4069 | + second.is_some(), flat.sse, flat.bits, here.sse, here.bits, | ||
| 4070 | + flat_ref.sse, flat_ref.bits, | ||
| 4071 | + ); | ||
| 4072 | + } | ||
| 4073 | + } | ||
| 4074 | + } | ||
| 4001 | // What the caller's already-counted `flat` cost has to be corrected by | 4075 | // What the caller's already-counted `flat` cost has to be corrected by |
| 4002 | // when a non-`DCT_DCT` type won it, the same way the split delta below is. | 4076 | // when a non-`DCT_DCT` type won it, the same way the split delta below is. |
| 4003 | let flat_gain = (flat_ref.sse + search.lambda * flat_ref.bits) | 4077 | let flat_gain = (flat_ref.sse + search.lambda * flat_ref.bits) |
| @@ -4050,7 +4124,7 @@ fn commit_inter_luma( | |||
| 4050 | let mut best: Option<(f64, TxType, Trial)> = None; | 4124 | let mut best: Option<(f64, TxType, Trial)> = None; |
| 4051 | for &tx_type in inter_tx_type_candidates(set, search.screen) { | 4125 | for &tx_type in inter_tx_type_candidates(set, search.screen) { |
| 4052 | let candidate = unit(tx_type); | 4126 | let candidate = unit(tx_type); |
| 4053 | - let cost = candidate.sse + search.lambda * candidate.bits; | 4127 | + let cost = candidate.sse + search.lambda * tx_type_lambda_mult() * candidate.bits; |
| 4054 | if best.as_ref().is_none_or(|(b, _, _)| cost < *b) { | 4128 | if best.as_ref().is_none_or(|(b, _, _)| cost < *b) { |
| 4055 | best = Some((cost, tx_type, candidate)); | 4129 | best = Some((cost, tx_type, candidate)); |
| 4056 | } | 4130 | } |
| @@ -11157,6 +11231,41 @@ pub(crate) fn encode_inter_frame( | |||
| 11157 | refs[4] = trial_golden.as_ref(); | 11231 | refs[4] = trial_golden.as_ref(); |
| 11158 | refs[7] = trial_altref.as_ref(); | 11232 | refs[7] = trial_altref.as_ref(); |
| 11159 | let refpix = crate::decode::RefPix::ready(refs); | 11233 | let refpix = crate::decode::RefPix::ready(refs); |
| 11234 | + // lane-txrd: the frame's luma error BEFORE the loop filters, i.e. the | ||
| 11235 | + // sum the block search believed it was minimising. Against the | ||
| 11236 | + // post-filter per-frame PSNR the gate prints, this separates a trial | ||
| 11237 | + // whose reconstruction is a lie from a filter stage that undoes the win. | ||
| 11238 | + if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { | ||
| 11239 | + let sse: u64 = luma | ||
| 11240 | + .reconstruction | ||
| 11241 | + .chunks(luma.width) | ||
| 11242 | + .take(luma.true_height) | ||
| 11243 | + .zip(picture_y8.chunks(luma.width)) | ||
| 11244 | + .flat_map(|(r, s)| r[..luma.true_width].iter().zip(&s[..luma.true_width])) | ||
| 11245 | + .map(|(&r, &s)| { | ||
| 11246 | + let d = i64::from(r) - i64::from(s); | ||
| 11247 | + (d * d) as u64 | ||
| 11248 | + }) | ||
| 11249 | + .sum(); | ||
| 11250 | + eprintln!("EC_PREFILTER q={base_q_idx} luma sse={sse}"); | ||
| 11251 | + } | ||
| 11252 | + let post = |luma: &Plane<'_>, tag: &str| { | ||
| 11253 | + if !crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { | ||
| 11254 | + return; | ||
| 11255 | + } | ||
| 11256 | + let sse: u64 = luma | ||
| 11257 | + .reconstruction | ||
| 11258 | + .chunks(luma.width) | ||
| 11259 | + .take(luma.true_height) | ||
| 11260 | + .zip(picture_y8.chunks(luma.width)) | ||
| 11261 | + .flat_map(|(r, s)| r[..luma.true_width].iter().zip(&s[..luma.true_width])) | ||
| 11262 | + .map(|(&r, &s)| { | ||
| 11263 | + let d = i64::from(r) - i64::from(s); | ||
| 11264 | + (d * d) as u64 | ||
| 11265 | + }) | ||
| 11266 | + .sum(); | ||
| 11267 | + eprintln!("{tag} q={base_q_idx} luma sse={sse}"); | ||
| 11268 | + }; | ||
| 11160 | pick_and_apply_filters( | 11269 | pick_and_apply_filters( |
| 11161 | &mut header, | 11270 | &mut header, |
| 11162 | &mut luma, | 11271 | &mut luma, |
| @@ -11212,6 +11321,7 @@ pub(crate) fn encode_inter_frame( | |||
| 11212 | ) | 11321 | ) |
| 11213 | }, | 11322 | }, |
| 11214 | )?; | 11323 | )?; |
| 11324 | + post(&luma, "EC_POSTFILTER"); | ||
| 11215 | let mut cdfs = end_cdfs | 11325 | let mut cdfs = end_cdfs |
| 11216 | .into_inner() | 11326 | .into_inner() |
| 11217 | .unwrap_or_else(|| start_cdfs.0.clone()); | 11327 | .unwrap_or_else(|| start_cdfs.0.clone()); |
| @@ -14849,12 +14959,15 @@ mod tests { | |||
| 14849 | ); | 14959 | ); |
| 14850 | } | 14960 | } |
| 14851 | } | 14961 | } |
| 14852 | - let mean: f64 = decoded | 14962 | + let per_frame: Vec<f64> = decoded.iter().zip(source).map(|(d, s)| psnr_all(d, s)).collect(); |
| 14853 | - .iter() | 14963 | + // lane-txrd: does a tool's loss start at the FIRST inter frame |
| 14854 | - .zip(source) | 14964 | + // (a local mispricing) or grow down the GOP (propagation into |
| 14855 | - .map(|(d, s)| psnr_all(d, s)) | 14965 | + // the reference chain)? One line per q point answers it. |
| 14856 | - .sum::<f64>() | 14966 | + if crate::envflags::env_flag!("EC_AV1_FRAME_PSNR") { |
| 14857 | - / source.len() as f64; | 14967 | + let row: Vec<String> = per_frame.iter().map(|p| format!("{p:.2}")).collect(); |
| 14968 | + eprintln!("{name}: q={q} per-frame PSNR {}", row.join(" ")); | ||
| 14969 | + } | ||
| 14970 | + let mean: f64 = per_frame.iter().sum::<f64>() / source.len() as f64; | ||
| 14858 | ladder.push((mean, (encoded.stream.len() as f64).log10())); | 14971 | ladder.push((mean, (encoded.stream.len() as f64).log10())); |
| 14859 | } | 14972 | } |
| 14860 | // What this clip asked for and what the content gate left it coding | 14973 | // What this clip asked for and what the content gate left it coding |
| @@ -15938,6 +16051,42 @@ mod tests { | |||
| 15938 | row("32x32 single", 4), | 16051 | row("32x32 single", 4), |
| 15939 | row("32x32 compound", 6), | 16052 | row("32x32 compound", 6), |
| 15940 | ); | 16053 | ); |
| 16054 | + // lane-txrd: which types the two searches actually COMMITTED over | ||
| 16055 | + // this row's four encodes -- the gate could not say whether a type | ||
| 16056 | + // search fired at all (class `gate-blind-to-feature`). | ||
| 16057 | + const NAMES: [&str; 16] = [ | ||
| 16058 | + "IDTX", "DCT_DCT", "ADST_ADST", "ADST_DCT", "DCT_ADST", "V_DCT", "H_DCT", | ||
| 16059 | + "FLIPADST_DCT", "DCT_FLIPADST", "FLIPADST_FLIPADST", "ADST_FLIPADST", | ||
| 16060 | + "FLIPADST_ADST", "V_ADST", "H_ADST", "V_FLIPADST", "H_FLIPADST", | ||
| 16061 | + ]; | ||
| 16062 | + let name_of = |t: usize| NAMES[t]; | ||
| 16063 | + for (label, hits) in [("inter", take_inter_tx_type_hits()), ("intra", take_tx_type_hits())] { | ||
| 16064 | + let total: usize = hits.iter().sum(); | ||
| 16065 | + let live: Vec<String> = hits | ||
| 16066 | + .iter() | ||
| 16067 | + .enumerate() | ||
| 16068 | + .filter(|&(_, &n)| n > 0) | ||
| 16069 | + .map(|(t, &n)| format!("{}={n} ({:.1}%)", name_of(t), 100.0 * n as f64 / total.max(1) as f64)) | ||
| 16070 | + .collect(); | ||
| 16071 | + eprintln!("{name}: {label} tx_type of {total} units -- {}", live.join(" ")); | ||
| 16072 | + } | ||
| 16073 | + let t = take_txrd_stats(); | ||
| 16074 | + if t[0] > 0 { | ||
| 16075 | + eprintln!( | ||
| 16076 | + "{name}: txrd flat decisions={} chosen sse={} bits={:.0} | DCT_DCT sse={} \ | ||
| 16077 | + bits={:.0} (dsse {:+.2}%, dbits {:+.2}%)", | ||
| 16078 | + t[0], t[1], t[3] as f64 / 64.0, t[2], t[4] as f64 / 64.0, | ||
| 16079 | + 100.0 * (t[1] as f64 - t[2] as f64) / t[2].max(1) as f64, | ||
| 16080 | + 100.0 * (t[3] as f64 - t[4] as f64) / t[4].max(1) as f64, | ||
| 16081 | + ); | ||
| 16082 | + eprintln!( | ||
| 16083 | + "{name}: txrd DCT_DCT re-predicted HERE sse={} bits={:.0} (vs the passed-in \ | ||
| 16084 | + flat: dsse {:+.2}%, dbits {:+.2}%)", | ||
| 16085 | + t[5], t[6] as f64 / 64.0, | ||
| 16086 | + 100.0 * (t[5] as f64 - t[2] as f64) / t[2].max(1) as f64, | ||
| 16087 | + 100.0 * (t[6] as f64 - t[4] as f64) / t[4].max(1) as f64, | ||
| 16088 | + ); | ||
| 16089 | + } | ||
| 15941 | } | 16090 | } |
| 15942 | 16091 | ||
| 15943 | /// The video stream's coded size, from ffprobe. | 16092 | /// 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 | /// The typed forward transform is the decoder's exact inverse undone: | 2191 | /// The typed forward transform is the decoder's exact inverse undone: |
| 2156 | /// quantized and taken back, an ADST-typed residual returns with the same | 2192 | /// quantized and taken back, an ADST-typed residual returns with the same |
| 2157 | /// error a `DCT_DCT` one does. This is what pins the *gain* of the ADST | 2193 | /// error a `DCT_DCT` one does. This is what pins the *gain* of the ADST |