diff --git a/crates/ec-av1/examples/syntax_census.rs b/crates/ec-av1/examples/syntax_census.rs index 86ebe6ae..fc018240 100644 --- a/crates/ec-av1/examples/syntax_census.rs +++ b/crates/ec-av1/examples/syntax_census.rs @@ -294,7 +294,7 @@ fn main() { // the frames above are coded in (a pyramid codes a hidden ARF ahead of the // leaves that predict from it, and re-outputs it later with // `show_existing_frame`) -- so it is its own table rather than a column. - let mut psnrs: Vec = Vec::new(); + let mut psnrs: Vec<(f64, f64, f64)> = Vec::new(); if let Some(src) = args.get(1).map(|p| std::fs::read(p).expect("source")) { let mut sum = 0.0; let mut rows = Vec::new(); @@ -303,8 +303,13 @@ fn main() { let frame_len = luma + 2 * chroma; let Some(b) = src.get(i * frame_len..(i + 1) * frame_len) else { break }; let want: Vec = b[..luma].iter().map(|&v| u16::from(v)).collect(); + // lane-arfpred: the chroma planes too -- an anchor's byte gap can + // sit in a plane the luma-only line cannot see (class + // `metric blind to a plane`). + let wu: Vec = b[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(); + let wv: Vec = b[luma + chroma..frame_len].iter().map(|&v| u16::from(v)).collect(); let p = psnr(&pic.y, &want); - psnrs.push(p); + psnrs.push((p, psnr(&pic.u, &wu), psnr(&pic.v, &wv))); sum += p; rows.push(format!("{i}:{p:.2}")); } @@ -321,7 +326,8 @@ fn main() { // put next to the reference encoder's ARF at the same display position. if std::env::var("EC_CENSUS_PERFRAME").as_deref() == Ok("1") { for f in &frames { - let p = psnrs.get(f.order_hint as usize).copied().unwrap_or(f64::NAN); + let (p, pu, pv) = + psnrs.get(f.order_hint as usize).copied().unwrap_or((f64::NAN, f64::NAN, f64::NAN)); let refs: Vec = (0..7) .filter(|i| f.refs[i + 1] > 0) .map(|i| { @@ -334,7 +340,7 @@ fn main() { .collect(); report( &format!( - "frame {} {} hint {} q {} PSNR-Y {p:.2} dB refs [{}]", + "frame {} {} hint {} q {} PSNR-Y {p:.2} U {pu:.2} V {pv:.2} dB refs [{}]", f.idx, f.kind, f.order_hint, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index fb5e0693..dd1f3d5e 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -11303,6 +11303,149 @@ pub(crate) struct PyramidFrame { pub sign_bias: crate::mvstack::SignBiasTable, } +/// lane-arfpred: the temporal filter of a top ARF's SOURCE picture, libaom's +/// `temporal_filter` in the small: every 16x16 luma block of `src` is +/// motion-compensated from each picture of `window` (the group's own display +/// neighbours, nearest first) and averaged in with weight +/// `exp(-mse / strength)`, so a neighbour that matches badly contributes +/// nothing and one that matches well cancels the grain the block's residual +/// would otherwise have to code. +/// +/// `strength` is [`crate::speed::ARF_TF`]; `0` never reaches here. Chroma +/// rides the luma block's own vector (halved), which is what libaom does at +/// this block size. The picture is the SUPERBLOCK-padded one, so the 16x16 +/// grid tiles it exactly. +/// +/// Priced before it was built by `encode::tests::arf_pred_census`: on film B +/// the filtered ARF's prediction SAD at lag 8 is 11.7% below the unfiltered +/// one's (film A 5.9%) with the two past neighbours at strength 30, and the +/// filtered picture is 50.1 dB from the source it replaces -- 4 dB above the +/// quality the ARF is coded at, so the substitution itself costs little. +pub(crate) fn arf_temporal_filter(src: &Picture, window: &[Picture], strength: f64) -> Picture { + const SIDE: usize = 16; + let neighbours: Vec<&Picture> = window + .iter() + .filter(|p| p.width == src.width && p.height == src.height) + .take(2) + .collect(); + if neighbours.is_empty() || strength <= 0.0 { + return src.clone(); + } + let mut out = src.clone(); + // SAD of `src`'s block at `(bx, by)` against `r` displaced by `mv`, or + // `None` when the displaced block leaves the picture. + let sad = |r: &Picture, bx: usize, by: usize, mv: (isize, isize)| { + let (sy, sx) = (by as isize + mv.0, bx as isize + mv.1); + if sy < 0 || sx < 0 || sy as usize + SIDE > r.height || sx as usize + SIDE > r.width { + return None; + } + let (sy, sx) = (sy as usize, sx as usize); + let mut acc = 0u64; + for row in 0..SIDE { + let c = &src.y[(by + row) * src.width + bx..][..SIDE]; + let p = &r.y[(sy + row) * r.width + sx..][..SIDE]; + acc += c.iter().zip(p).map(|(&a, &b)| u64::from(a.abs_diff(b))).sum::(); + } + Some((acc, (sy, sx))) + }; + // The log-step diamond `arf_pred_census` priced this with. + let search = |r: &Picture, bx: usize, by: usize| -> Option<(usize, usize)> { + let mut best = sad(r, bx, by, (0, 0))?; + let mut centre = (0isize, 0isize); + let mut step = 16isize; + while step >= 1 { + let from = centre; + for (dy, dx) in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] { + let mv = (from.0 + dy * step, from.1 + dx * step); + if let Some(s) = sad(r, bx, by, mv) { + if s.0 < best.0 { + best = s; + centre = mv; + } + } + } + step /= 2; + } + Some(best.1) + }; + let (cw, ch) = (src.width / 2, src.height / 2); + for by in (0..src.height).step_by(SIDE) { + for bx in (0..src.width).step_by(SIDE) { + if by + SIDE > src.height || bx + SIDE > src.width { + continue; + } + let mut y_acc = vec![0f64; SIDE * SIDE]; + let mut c_acc = [vec![0f64; SIDE * SIDE / 4], vec![0f64; SIDE * SIDE / 4]]; + let mut wsum = 1.0f64; + // Chroma keeps its OWN weight sum: an edge block whose halved + // vector leaves the chroma plane contributes to luma only, and + // dividing chroma by the luma sum would darken it (class + // `metric blind to a plane`, on the encoder side). + let mut cwsum = 1.0f64; + for row in 0..SIDE { + for col in 0..SIDE { + y_acc[row * SIDE + col] = f64::from(src.y[(by + row) * src.width + bx + col]); + } + } + for row in 0..SIDE / 2 { + for col in 0..SIDE / 2 { + let i = (by / 2 + row) * cw + bx / 2 + col; + c_acc[0][row * SIDE / 2 + col] = f64::from(src.u[i]); + c_acc[1][row * SIDE / 2 + col] = f64::from(src.v[i]); + } + } + for r in &neighbours { + let Some((sy, sx)) = search(r, bx, by) else { continue }; + let mut sse = 0f64; + for row in 0..SIDE { + for col in 0..SIDE { + let d = f64::from(src.y[(by + row) * src.width + bx + col]) + - f64::from(r.y[(sy + row) * r.width + sx + col]); + sse += d * d; + } + } + let w = (-sse / (SIDE * SIDE) as f64 / strength).exp(); + for row in 0..SIDE { + for col in 0..SIDE { + y_acc[row * SIDE + col] += + w * f64::from(r.y[(sy + row) * r.width + sx + col]); + } + } + // Chroma reads the luma vector halved; a block on the picture + // edge whose halved vector would leave the chroma plane is + // left to luma alone rather than clamped to a different + // displacement than the luma it must stay registered with. + let (cy, cx) = (sy / 2, sx / 2); + if cy + SIDE / 2 <= ch && cx + SIDE / 2 <= cw { + for row in 0..SIDE / 2 { + for col in 0..SIDE / 2 { + let i = (cy + row) * cw + cx + col; + c_acc[0][row * SIDE / 2 + col] += w * f64::from(r.u[i]); + c_acc[1][row * SIDE / 2 + col] += w * f64::from(r.v[i]); + } + } + cwsum += w; + } + wsum += w; + } + for row in 0..SIDE { + for col in 0..SIDE { + out.y[(by + row) * src.width + bx + col] = + (y_acc[row * SIDE + col] / wsum).round() as u16; + } + } + for row in 0..SIDE / 2 { + for col in 0..SIDE / 2 { + let i = (by / 2 + row) * cw + bx / 2 + col; + out.u[i] = (c_acc[0][row * SIDE / 2 + col] / cwsum).round() as u16; + out.v[i] = (c_acc[1][row * SIDE / 2 + col] / cwsum).round() as u16; + } + } + } + } + out +} + /// Encodes one picture as an inter frame predicting from `reference`'s /// reconstruction, which the caller decoded (or, for the frame right after /// the key frame, is the key frame's own reconstruction). @@ -18620,6 +18763,173 @@ mod tests { } } + /// lane-arfpred: WHAT THE TOP ARF'S PREDICTION IS WORTH, and what a + /// temporal filter of its SOURCE would take off it, priced before any + /// encoder change (class `gate-blind-to-feature`). + /// + /// The stream census says our top ARF costs 6992 B a frame against + /// rav1e's 3872 at a COARSER quantizer (118 vs 104) for the same picture + /// quality, and that 61% of the gap is residual, not syntax. Two + /// explanations are separable here without an encoder: our anchor + /// predicts across 8 pictures where rav1e's predicts across 4 (lag), and + /// the residual it codes at that lag is dominated by film grain, which no + /// motion search can predict (source noise). So, per ARF position of the + /// gate's own 48-picture window: + /// + /// * mean full-pel SAD per pixel at lag 8 (ours) and lag 4 (rav1e's), + /// * the same at lag 8 with the CURRENT picture temporally filtered -- + /// a libaom-shaped motion-compensated average of the +-2 display + /// neighbours, weights `exp(-mse / strength)` -- which is the residual + /// energy an ARF temporal filter would remove, and + /// * PSNR of that filtered picture against the unfiltered one, the + /// CEILING the filtered ARF's own PSNR is then measured under (the + /// BD gate scores against the unfiltered source). + /// + /// EC_ARF_TF_S= cargo test -p ec-av1 --release --lib -- \ + /// --ignored arf_pred_census --nocapture + #[test] + #[ignore = "reads the real films"] + fn arf_pred_census() { + if !have_ffmpeg() { + eprintln!("SKIP arf_pred_census: no ffmpeg"); + return; + } + const SIDE: usize = 16; + let strength: f64 = std::env::var("EC_ARF_TF_S") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(30.0); + let past = std::env::var("EC_ARF_TF_PAST").as_deref() == Ok("1"); + // SAD and the matching block of `r` at full-pel `(dy, dx)`, or `None` + // when the displaced block leaves the picture. + let at = |cur: &[u16], cw: usize, r: &Picture, bx: usize, by: usize, mv: (isize, isize)| { + let (sy, sx) = (by as isize + mv.0, bx as isize + mv.1); + if sy < 0 || sx < 0 { + return None; + } + let (sy, sx) = (sy as usize, sx as usize); + if sy + SIDE > r.height || sx + SIDE > r.width { + return None; + } + let mut acc = 0u64; + for row in 0..SIDE { + let c = &cur[row * cw..][..SIDE]; + let p = &r.y[(sy + row) * r.width + sx..][..SIDE]; + acc += c.iter().zip(p).map(|(&a, &b)| u64::from(a.abs_diff(b))).sum::(); + } + Some((acc, (sy, sx))) + }; + // The same log-step diamond `last2_census` uses, so the two censuses' + // SADs are comparable. + let search = |cur: &[u16], cw: usize, r: &Picture, bx: usize, by: usize| { + let mut best = match at(cur, cw, r, bx, by, (0, 0)) { + Some(v) => v, + None => return (u64::MAX, (by, bx)), + }; + let mut centre = (0isize, 0isize); + let mut step = 16isize; + while step >= 1 { + let from = centre; + for (dy, dx) in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] { + let mv = (from.0 + dy * step, from.1 + dx * step); + if let Some(s) = at(cur, cw, r, bx, by, mv) { + if s.0 < best.0 { + best = s; + centre = mv; + } + } + } + step /= 2; + } + best + }; + println!( + "| clip | ARFs | strength {strength} past {past} | lag8 SAD/px | lag4 SAD/px | tf lag8 SAD/px | tf removes | tf PSNR ceiling |" + ); + println!("|---|---|---|---|---|---|---|"); + for (name, path, seek) in native_gate_clips() { + if !name.starts_with("film ") { + continue; + } + let (vf, cw, ch) = crate::probe::gate_crop(&path); + let pictures = clip_frames_vf(&path, &seek, &vf, cw, ch, 48); + let (mut n8, mut n4, mut ntf, mut px, mut arfs) = (0u64, 0u64, 0u64, 0u64, 0usize); + let (mut tf_sse, mut tf_px) = (0f64, 0u64); + for i in (8..pictures.len()).step_by(8) { + arfs += 1; + let cur = &pictures[i]; + for by in (0..ch.saturating_sub(SIDE - 1)).step_by(SIDE) { + for bx in (0..cw.saturating_sub(SIDE - 1)).step_by(SIDE) { + let block: Vec = (0..SIDE) + .flat_map(|row| cur.y[(by + row) * cur.width + bx..][..SIDE].to_vec()) + .collect(); + n8 += search(&block, SIDE, &pictures[i - 8], bx, by).0; + n4 += search(&block, SIDE, &pictures[i - 4], bx, by).0; + // The temporal filter: each +-2 display neighbour is + // motion-compensated onto this block and averaged in + // with libaom's shape, a weight that falls with the + // match's own mean squared error. + let mut acc: Vec = block.iter().map(|&v| f64::from(v)).collect(); + let mut wsum = 1.0f64; + // `EC_ARF_TF_PAST=1` restricts the filter to the + // DISPLAY-PAST neighbours -- the only ones the + // encoder's own group buffer holds when it codes its + // top ARF (the group's future is not buffered yet). + let offs: &[isize] = match past { + true => &[-2, -1], + false => &[-2, -1, 1, 2], + }; + for &d in offs { + let j = i as isize + d; + let Some(nb) = usize::try_from(j).ok().and_then(|j| pictures.get(j)) + else { + continue; + }; + let (_, (sy, sx)) = search(&block, SIDE, nb, bx, by); + let mut sse = 0f64; + for row in 0..SIDE { + for col in 0..SIDE { + let d = f64::from(block[row * SIDE + col]) + - f64::from(nb.y[(sy + row) * nb.width + sx + col]); + sse += d * d; + } + } + let w = (-sse / ((SIDE * SIDE) as f64) / strength).exp(); + for row in 0..SIDE { + for col in 0..SIDE { + acc[row * SIDE + col] += + w * f64::from(nb.y[(sy + row) * nb.width + sx + col]); + } + } + wsum += w; + } + let filtered: Vec = acc + .iter() + .map(|v| (v / wsum).round().clamp(0.0, 1023.0) as u16) + .collect(); + for (a, b) in filtered.iter().zip(&block) { + let d = f64::from(*a) - f64::from(*b); + tf_sse += d * d; + } + tf_px += (SIDE * SIDE) as u64; + ntf += search(&filtered, SIDE, &pictures[i - 8], bx, by).0; + px += (SIDE * SIDE) as u64; + } + } + } + let per = |v: u64| v as f64 / px.max(1) as f64; + let mse = tf_sse / tf_px.max(1) as f64; + println!( + "| {name} | {arfs} | s{strength} p{past} | {:.3} | {:.3} | {:.3} | {:.1}% | {:.2} dB |", + per(n8), + per(n4), + per(ntf), + 100.0 * (n8 - ntf) as f64 / n8.max(1) as f64, + 10.0 * (255.0f64 * 255.0 / mse.max(1e-9)).log10(), + ); + } + } + /// lane-refs: THE FRAME FILTER REACHES THE PIXELS. `mc::predict` used to /// hardwire the REGULAR kernel, so the encoder could not have coded a /// SMOOTH or SHARP frame even if the header had said so -- the search, diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 4ab4141b..e17ab157 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1067,6 +1067,16 @@ const KEY_DEFAULT: i16 = -48; /// shipped on the flat path -- was inert on every frame of every pyramid /// stream, including the ARFs that carry 54% of a long-GOP stream's bytes /// (class `tool disabled in every gate recipe`). +/// lane-arfpred: the top ARF source temporal filter's strength +/// ([`crate::speed::ARF_TF`]), `EC_AV1_ARF_TF=` overriding it; +/// `0` is off. +fn arf_tf_strength() -> f64 { + std::env::var("EC_AV1_ARF_TF") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF)) +} + fn tpl_pyramid() -> bool { match std::env::var("EC_AV1_TPL_PYRAMID").ok() { Some(v) => v != "0", @@ -1782,8 +1792,20 @@ impl Av1Encoder { .then(|| self.dpb[altref_slot as usize].as_ref().map(|s| s.picture.clone())) .flatten(); let start_cdfs = self.dpb[last_slot as usize].as_ref().map(|s| s.cdfs.0.clone()); + // lane-arfpred: the top ARF is coded from a TEMPORALLY FILTERED + // source when `speed::ARF_TF` is non-zero -- the grain it would + // otherwise spend its residual on is averaged out against the + // group's own display neighbours (`lookahead`, already padded). + let source = picture.padded_to(SUPERBLOCK); + let tf = arf_tf_strength(); + let source = match (tf > 0.0, dq_level) { + (true, crate::encode::DqLevel::TopArf) => { + crate::encode::arf_temporal_filter(&source, lookahead, tf) + } + _ => source, + }; let encoded = || -> Result { encode_inter_frame( - &picture.padded_to(SUPERBLOCK), + &source, &reference, base_q_idx, DEADZONE, diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index d59d0df3..502e1dde 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -366,6 +366,20 @@ pub(crate) const DQ_TPL_K: [f64; 11] = [0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, /// `lanes/arfq.report.md`. pub(crate) const DQ_LEVEL_K: [f64; 3] = [1.5, 1.0, 1.0]; +/// lane-arfpred: the strength of the top ARF's SOURCE temporal filter +/// ([`crate::encode::arf_temporal_filter`]) -- `0` is off, higher filters +/// harder (the weight of a motion-compensated neighbour is +/// `exp(-mse / strength)`). `EC_AV1_ARF_TF=` overrides it. +/// +/// The top ARF carries 55% of a 48-picture film B stream at 6992 B a frame +/// against rav1e's 3872 at a FINER quantizer, and the per-frame census says +/// 61% of that gap is residual, not syntax -- while `arf_pred_census` says +/// the anchor's own lag is worth only 7% of its prediction SAD (lag 8 1.199 +/// vs lag 4 1.114 per pixel), so what the residual codes is mostly source +/// grain. Filtering it out of the picture the anchor is coded from is the +/// mechanism libaom uses (`arnr`) and rav1e 0.8.1 does not have at all. +pub(crate) const ARF_TF: [f64; 11] = [0.0; 11]; + /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split /// trial is withheld. The single biggest wall lever in the tile search. pub(crate) const SPLIT_RD: [f64; 11] = [