instrument(av1): price the top ARF's prediction and a source temporal filter
Browse files at this commit56d80b67f496e69756f326cb5ca89c41e8af9613Parent: 7cc8fe14 changed files +357 -5
- crates/ec-av1/examples/syntax_census.rs +10 -4
- crates/ec-av1/src/encode.rs +310 -0
- crates/ec-av1/src/encoder.rs +23 -1
- crates/ec-av1/src/speed.rs +14 -0
crates/ec-av1/examples/syntax_census.rsmodified+10 -4
| @@ -294,7 +294,7 @@ fn main() { | ||
| 294 | 294 | // the frames above are coded in (a pyramid codes a hidden ARF ahead of the |
| 295 | 295 | // leaves that predict from it, and re-outputs it later with |
| 296 | 296 | // `show_existing_frame`) -- so it is its own table rather than a column. |
| 297 | - let mut psnrs: Vec<f64> = Vec::new(); | |
| 297 | + let mut psnrs: Vec<(f64, f64, f64)> = Vec::new(); | |
| 298 | 298 | if let Some(src) = args.get(1).map(|p| std::fs::read(p).expect("source")) { |
| 299 | 299 | let mut sum = 0.0; |
| 300 | 300 | let mut rows = Vec::new(); |
| @@ -303,8 +303,13 @@ fn main() { | ||
| 303 | 303 | let frame_len = luma + 2 * chroma; |
| 304 | 304 | let Some(b) = src.get(i * frame_len..(i + 1) * frame_len) else { break }; |
| 305 | 305 | let want: Vec<u16> = b[..luma].iter().map(|&v| u16::from(v)).collect(); |
| 306 | + // lane-arfpred: the chroma planes too -- an anchor's byte gap can | |
| 307 | + // sit in a plane the luma-only line cannot see (class | |
| 308 | + // `metric blind to a plane`). | |
| 309 | + let wu: Vec<u16> = b[luma..luma + chroma].iter().map(|&v| u16::from(v)).collect(); | |
| 310 | + let wv: Vec<u16> = b[luma + chroma..frame_len].iter().map(|&v| u16::from(v)).collect(); | |
| 306 | 311 | let p = psnr(&pic.y, &want); |
| 307 | - psnrs.push(p); | |
| 312 | + psnrs.push((p, psnr(&pic.u, &wu), psnr(&pic.v, &wv))); | |
| 308 | 313 | sum += p; |
| 309 | 314 | rows.push(format!("{i}:{p:.2}")); |
| 310 | 315 | } |
| @@ -321,7 +326,8 @@ fn main() { | ||
| 321 | 326 | // put next to the reference encoder's ARF at the same display position. |
| 322 | 327 | if std::env::var("EC_CENSUS_PERFRAME").as_deref() == Ok("1") { |
| 323 | 328 | for f in &frames { |
| 324 | - let p = psnrs.get(f.order_hint as usize).copied().unwrap_or(f64::NAN); | |
| 329 | + let (p, pu, pv) = | |
| 330 | + psnrs.get(f.order_hint as usize).copied().unwrap_or((f64::NAN, f64::NAN, f64::NAN)); | |
| 325 | 331 | let refs: Vec<String> = (0..7) |
| 326 | 332 | .filter(|i| f.refs[i + 1] > 0) |
| 327 | 333 | .map(|i| { |
| @@ -334,7 +340,7 @@ fn main() { | ||
| 334 | 340 | .collect(); |
| 335 | 341 | report( |
| 336 | 342 | &format!( |
| 337 | - "frame {} {} hint {} q {} PSNR-Y {p:.2} dB refs [{}]", | |
| 343 | + "frame {} {} hint {} q {} PSNR-Y {p:.2} U {pu:.2} V {pv:.2} dB refs [{}]", | |
| 338 | 344 | f.idx, |
| 339 | 345 | f.kind, |
| 340 | 346 | f.order_hint, |
crates/ec-av1/src/encode.rsmodified+310 -0
| @@ -11303,6 +11303,149 @@ pub(crate) struct PyramidFrame { | ||
| 11303 | 11303 | pub sign_bias: crate::mvstack::SignBiasTable, |
| 11304 | 11304 | } |
| 11305 | 11305 | |
| 11306 | +/// lane-arfpred: the temporal filter of a top ARF's SOURCE picture, libaom's | |
| 11307 | +/// `temporal_filter` in the small: every 16x16 luma block of `src` is | |
| 11308 | +/// motion-compensated from each picture of `window` (the group's own display | |
| 11309 | +/// neighbours, nearest first) and averaged in with weight | |
| 11310 | +/// `exp(-mse / strength)`, so a neighbour that matches badly contributes | |
| 11311 | +/// nothing and one that matches well cancels the grain the block's residual | |
| 11312 | +/// would otherwise have to code. | |
| 11313 | +/// | |
| 11314 | +/// `strength` is [`crate::speed::ARF_TF`]; `0` never reaches here. Chroma | |
| 11315 | +/// rides the luma block's own vector (halved), which is what libaom does at | |
| 11316 | +/// this block size. The picture is the SUPERBLOCK-padded one, so the 16x16 | |
| 11317 | +/// grid tiles it exactly. | |
| 11318 | +/// | |
| 11319 | +/// Priced before it was built by `encode::tests::arf_pred_census`: on film B | |
| 11320 | +/// the filtered ARF's prediction SAD at lag 8 is 11.7% below the unfiltered | |
| 11321 | +/// one's (film A 5.9%) with the two past neighbours at strength 30, and the | |
| 11322 | +/// filtered picture is 50.1 dB from the source it replaces -- 4 dB above the | |
| 11323 | +/// quality the ARF is coded at, so the substitution itself costs little. | |
| 11324 | +pub(crate) fn arf_temporal_filter(src: &Picture, window: &[Picture], strength: f64) -> Picture { | |
| 11325 | + const SIDE: usize = 16; | |
| 11326 | + let neighbours: Vec<&Picture> = window | |
| 11327 | + .iter() | |
| 11328 | + .filter(|p| p.width == src.width && p.height == src.height) | |
| 11329 | + .take(2) | |
| 11330 | + .collect(); | |
| 11331 | + if neighbours.is_empty() || strength <= 0.0 { | |
| 11332 | + return src.clone(); | |
| 11333 | + } | |
| 11334 | + let mut out = src.clone(); | |
| 11335 | + // SAD of `src`'s block at `(bx, by)` against `r` displaced by `mv`, or | |
| 11336 | + // `None` when the displaced block leaves the picture. | |
| 11337 | + let sad = |r: &Picture, bx: usize, by: usize, mv: (isize, isize)| { | |
| 11338 | + let (sy, sx) = (by as isize + mv.0, bx as isize + mv.1); | |
| 11339 | + if sy < 0 || sx < 0 || sy as usize + SIDE > r.height || sx as usize + SIDE > r.width { | |
| 11340 | + return None; | |
| 11341 | + } | |
| 11342 | + let (sy, sx) = (sy as usize, sx as usize); | |
| 11343 | + let mut acc = 0u64; | |
| 11344 | + for row in 0..SIDE { | |
| 11345 | + let c = &src.y[(by + row) * src.width + bx..][..SIDE]; | |
| 11346 | + let p = &r.y[(sy + row) * r.width + sx..][..SIDE]; | |
| 11347 | + acc += c.iter().zip(p).map(|(&a, &b)| u64::from(a.abs_diff(b))).sum::<u64>(); | |
| 11348 | + } | |
| 11349 | + Some((acc, (sy, sx))) | |
| 11350 | + }; | |
| 11351 | + // The log-step diamond `arf_pred_census` priced this with. | |
| 11352 | + let search = |r: &Picture, bx: usize, by: usize| -> Option<(usize, usize)> { | |
| 11353 | + let mut best = sad(r, bx, by, (0, 0))?; | |
| 11354 | + let mut centre = (0isize, 0isize); | |
| 11355 | + let mut step = 16isize; | |
| 11356 | + while step >= 1 { | |
| 11357 | + let from = centre; | |
| 11358 | + for (dy, dx) in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] { | |
| 11359 | + let mv = (from.0 + dy * step, from.1 + dx * step); | |
| 11360 | + if let Some(s) = sad(r, bx, by, mv) { | |
| 11361 | + if s.0 < best.0 { | |
| 11362 | + best = s; | |
| 11363 | + centre = mv; | |
| 11364 | + } | |
| 11365 | + } | |
| 11366 | + } | |
| 11367 | + step /= 2; | |
| 11368 | + } | |
| 11369 | + Some(best.1) | |
| 11370 | + }; | |
| 11371 | + let (cw, ch) = (src.width / 2, src.height / 2); | |
| 11372 | + for by in (0..src.height).step_by(SIDE) { | |
| 11373 | + for bx in (0..src.width).step_by(SIDE) { | |
| 11374 | + if by + SIDE > src.height || bx + SIDE > src.width { | |
| 11375 | + continue; | |
| 11376 | + } | |
| 11377 | + let mut y_acc = vec![0f64; SIDE * SIDE]; | |
| 11378 | + let mut c_acc = [vec![0f64; SIDE * SIDE / 4], vec![0f64; SIDE * SIDE / 4]]; | |
| 11379 | + let mut wsum = 1.0f64; | |
| 11380 | + // Chroma keeps its OWN weight sum: an edge block whose halved | |
| 11381 | + // vector leaves the chroma plane contributes to luma only, and | |
| 11382 | + // dividing chroma by the luma sum would darken it (class | |
| 11383 | + // `metric blind to a plane`, on the encoder side). | |
| 11384 | + let mut cwsum = 1.0f64; | |
| 11385 | + for row in 0..SIDE { | |
| 11386 | + for col in 0..SIDE { | |
| 11387 | + y_acc[row * SIDE + col] = f64::from(src.y[(by + row) * src.width + bx + col]); | |
| 11388 | + } | |
| 11389 | + } | |
| 11390 | + for row in 0..SIDE / 2 { | |
| 11391 | + for col in 0..SIDE / 2 { | |
| 11392 | + let i = (by / 2 + row) * cw + bx / 2 + col; | |
| 11393 | + c_acc[0][row * SIDE / 2 + col] = f64::from(src.u[i]); | |
| 11394 | + c_acc[1][row * SIDE / 2 + col] = f64::from(src.v[i]); | |
| 11395 | + } | |
| 11396 | + } | |
| 11397 | + for r in &neighbours { | |
| 11398 | + let Some((sy, sx)) = search(r, bx, by) else { continue }; | |
| 11399 | + let mut sse = 0f64; | |
| 11400 | + for row in 0..SIDE { | |
| 11401 | + for col in 0..SIDE { | |
| 11402 | + let d = f64::from(src.y[(by + row) * src.width + bx + col]) | |
| 11403 | + - f64::from(r.y[(sy + row) * r.width + sx + col]); | |
| 11404 | + sse += d * d; | |
| 11405 | + } | |
| 11406 | + } | |
| 11407 | + let w = (-sse / (SIDE * SIDE) as f64 / strength).exp(); | |
| 11408 | + for row in 0..SIDE { | |
| 11409 | + for col in 0..SIDE { | |
| 11410 | + y_acc[row * SIDE + col] += | |
| 11411 | + w * f64::from(r.y[(sy + row) * r.width + sx + col]); | |
| 11412 | + } | |
| 11413 | + } | |
| 11414 | + // Chroma reads the luma vector halved; a block on the picture | |
| 11415 | + // edge whose halved vector would leave the chroma plane is | |
| 11416 | + // left to luma alone rather than clamped to a different | |
| 11417 | + // displacement than the luma it must stay registered with. | |
| 11418 | + let (cy, cx) = (sy / 2, sx / 2); | |
| 11419 | + if cy + SIDE / 2 <= ch && cx + SIDE / 2 <= cw { | |
| 11420 | + for row in 0..SIDE / 2 { | |
| 11421 | + for col in 0..SIDE / 2 { | |
| 11422 | + let i = (cy + row) * cw + cx + col; | |
| 11423 | + c_acc[0][row * SIDE / 2 + col] += w * f64::from(r.u[i]); | |
| 11424 | + c_acc[1][row * SIDE / 2 + col] += w * f64::from(r.v[i]); | |
| 11425 | + } | |
| 11426 | + } | |
| 11427 | + cwsum += w; | |
| 11428 | + } | |
| 11429 | + wsum += w; | |
| 11430 | + } | |
| 11431 | + for row in 0..SIDE { | |
| 11432 | + for col in 0..SIDE { | |
| 11433 | + out.y[(by + row) * src.width + bx + col] = | |
| 11434 | + (y_acc[row * SIDE + col] / wsum).round() as u16; | |
| 11435 | + } | |
| 11436 | + } | |
| 11437 | + for row in 0..SIDE / 2 { | |
| 11438 | + for col in 0..SIDE / 2 { | |
| 11439 | + let i = (by / 2 + row) * cw + bx / 2 + col; | |
| 11440 | + out.u[i] = (c_acc[0][row * SIDE / 2 + col] / cwsum).round() as u16; | |
| 11441 | + out.v[i] = (c_acc[1][row * SIDE / 2 + col] / cwsum).round() as u16; | |
| 11442 | + } | |
| 11443 | + } | |
| 11444 | + } | |
| 11445 | + } | |
| 11446 | + out | |
| 11447 | +} | |
| 11448 | + | |
| 11306 | 11449 | /// Encodes one picture as an inter frame predicting from `reference`'s |
| 11307 | 11450 | /// reconstruction, which the caller decoded (or, for the frame right after |
| 11308 | 11451 | /// the key frame, is the key frame's own reconstruction). |
| @@ -18620,6 +18763,173 @@ mod tests { | ||
| 18620 | 18763 | } |
| 18621 | 18764 | } |
| 18622 | 18765 | |
| 18766 | + /// lane-arfpred: WHAT THE TOP ARF'S PREDICTION IS WORTH, and what a | |
| 18767 | + /// temporal filter of its SOURCE would take off it, priced before any | |
| 18768 | + /// encoder change (class `gate-blind-to-feature`). | |
| 18769 | + /// | |
| 18770 | + /// The stream census says our top ARF costs 6992 B a frame against | |
| 18771 | + /// rav1e's 3872 at a COARSER quantizer (118 vs 104) for the same picture | |
| 18772 | + /// quality, and that 61% of the gap is residual, not syntax. Two | |
| 18773 | + /// explanations are separable here without an encoder: our anchor | |
| 18774 | + /// predicts across 8 pictures where rav1e's predicts across 4 (lag), and | |
| 18775 | + /// the residual it codes at that lag is dominated by film grain, which no | |
| 18776 | + /// motion search can predict (source noise). So, per ARF position of the | |
| 18777 | + /// gate's own 48-picture window: | |
| 18778 | + /// | |
| 18779 | + /// * mean full-pel SAD per pixel at lag 8 (ours) and lag 4 (rav1e's), | |
| 18780 | + /// * the same at lag 8 with the CURRENT picture temporally filtered -- | |
| 18781 | + /// a libaom-shaped motion-compensated average of the +-2 display | |
| 18782 | + /// neighbours, weights `exp(-mse / strength)` -- which is the residual | |
| 18783 | + /// energy an ARF temporal filter would remove, and | |
| 18784 | + /// * PSNR of that filtered picture against the unfiltered one, the | |
| 18785 | + /// CEILING the filtered ARF's own PSNR is then measured under (the | |
| 18786 | + /// BD gate scores against the unfiltered source). | |
| 18787 | + /// | |
| 18788 | + /// EC_ARF_TF_S=<strength> cargo test -p ec-av1 --release --lib -- \ | |
| 18789 | + /// --ignored arf_pred_census --nocapture | |
| 18790 | + #[test] | |
| 18791 | + #[ignore = "reads the real films"] | |
| 18792 | + fn arf_pred_census() { | |
| 18793 | + if !have_ffmpeg() { | |
| 18794 | + eprintln!("SKIP arf_pred_census: no ffmpeg"); | |
| 18795 | + return; | |
| 18796 | + } | |
| 18797 | + const SIDE: usize = 16; | |
| 18798 | + let strength: f64 = std::env::var("EC_ARF_TF_S") | |
| 18799 | + .ok() | |
| 18800 | + .and_then(|v| v.parse().ok()) | |
| 18801 | + .unwrap_or(30.0); | |
| 18802 | + let past = std::env::var("EC_ARF_TF_PAST").as_deref() == Ok("1"); | |
| 18803 | + // SAD and the matching block of `r` at full-pel `(dy, dx)`, or `None` | |
| 18804 | + // when the displaced block leaves the picture. | |
| 18805 | + let at = |cur: &[u16], cw: usize, r: &Picture, bx: usize, by: usize, mv: (isize, isize)| { | |
| 18806 | + let (sy, sx) = (by as isize + mv.0, bx as isize + mv.1); | |
| 18807 | + if sy < 0 || sx < 0 { | |
| 18808 | + return None; | |
| 18809 | + } | |
| 18810 | + let (sy, sx) = (sy as usize, sx as usize); | |
| 18811 | + if sy + SIDE > r.height || sx + SIDE > r.width { | |
| 18812 | + return None; | |
| 18813 | + } | |
| 18814 | + let mut acc = 0u64; | |
| 18815 | + for row in 0..SIDE { | |
| 18816 | + let c = &cur[row * cw..][..SIDE]; | |
| 18817 | + let p = &r.y[(sy + row) * r.width + sx..][..SIDE]; | |
| 18818 | + acc += c.iter().zip(p).map(|(&a, &b)| u64::from(a.abs_diff(b))).sum::<u64>(); | |
| 18819 | + } | |
| 18820 | + Some((acc, (sy, sx))) | |
| 18821 | + }; | |
| 18822 | + // The same log-step diamond `last2_census` uses, so the two censuses' | |
| 18823 | + // SADs are comparable. | |
| 18824 | + let search = |cur: &[u16], cw: usize, r: &Picture, bx: usize, by: usize| { | |
| 18825 | + let mut best = match at(cur, cw, r, bx, by, (0, 0)) { | |
| 18826 | + Some(v) => v, | |
| 18827 | + None => return (u64::MAX, (by, bx)), | |
| 18828 | + }; | |
| 18829 | + let mut centre = (0isize, 0isize); | |
| 18830 | + let mut step = 16isize; | |
| 18831 | + while step >= 1 { | |
| 18832 | + let from = centre; | |
| 18833 | + for (dy, dx) in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] { | |
| 18834 | + let mv = (from.0 + dy * step, from.1 + dx * step); | |
| 18835 | + if let Some(s) = at(cur, cw, r, bx, by, mv) { | |
| 18836 | + if s.0 < best.0 { | |
| 18837 | + best = s; | |
| 18838 | + centre = mv; | |
| 18839 | + } | |
| 18840 | + } | |
| 18841 | + } | |
| 18842 | + step /= 2; | |
| 18843 | + } | |
| 18844 | + best | |
| 18845 | + }; | |
| 18846 | + println!( | |
| 18847 | + "| clip | ARFs | strength {strength} past {past} | lag8 SAD/px | lag4 SAD/px | tf lag8 SAD/px | tf removes | tf PSNR ceiling |" | |
| 18848 | + ); | |
| 18849 | + println!("|---|---|---|---|---|---|---|"); | |
| 18850 | + for (name, path, seek) in native_gate_clips() { | |
| 18851 | + if !name.starts_with("film ") { | |
| 18852 | + continue; | |
| 18853 | + } | |
| 18854 | + let (vf, cw, ch) = crate::probe::gate_crop(&path); | |
| 18855 | + let pictures = clip_frames_vf(&path, &seek, &vf, cw, ch, 48); | |
| 18856 | + let (mut n8, mut n4, mut ntf, mut px, mut arfs) = (0u64, 0u64, 0u64, 0u64, 0usize); | |
| 18857 | + let (mut tf_sse, mut tf_px) = (0f64, 0u64); | |
| 18858 | + for i in (8..pictures.len()).step_by(8) { | |
| 18859 | + arfs += 1; | |
| 18860 | + let cur = &pictures[i]; | |
| 18861 | + for by in (0..ch.saturating_sub(SIDE - 1)).step_by(SIDE) { | |
| 18862 | + for bx in (0..cw.saturating_sub(SIDE - 1)).step_by(SIDE) { | |
| 18863 | + let block: Vec<u16> = (0..SIDE) | |
| 18864 | + .flat_map(|row| cur.y[(by + row) * cur.width + bx..][..SIDE].to_vec()) | |
| 18865 | + .collect(); | |
| 18866 | + n8 += search(&block, SIDE, &pictures[i - 8], bx, by).0; | |
| 18867 | + n4 += search(&block, SIDE, &pictures[i - 4], bx, by).0; | |
| 18868 | + // The temporal filter: each +-2 display neighbour is | |
| 18869 | + // motion-compensated onto this block and averaged in | |
| 18870 | + // with libaom's shape, a weight that falls with the | |
| 18871 | + // match's own mean squared error. | |
| 18872 | + let mut acc: Vec<f64> = block.iter().map(|&v| f64::from(v)).collect(); | |
| 18873 | + let mut wsum = 1.0f64; | |
| 18874 | + // `EC_ARF_TF_PAST=1` restricts the filter to the | |
| 18875 | + // DISPLAY-PAST neighbours -- the only ones the | |
| 18876 | + // encoder's own group buffer holds when it codes its | |
| 18877 | + // top ARF (the group's future is not buffered yet). | |
| 18878 | + let offs: &[isize] = match past { | |
| 18879 | + true => &[-2, -1], | |
| 18880 | + false => &[-2, -1, 1, 2], | |
| 18881 | + }; | |
| 18882 | + for &d in offs { | |
| 18883 | + let j = i as isize + d; | |
| 18884 | + let Some(nb) = usize::try_from(j).ok().and_then(|j| pictures.get(j)) | |
| 18885 | + else { | |
| 18886 | + continue; | |
| 18887 | + }; | |
| 18888 | + let (_, (sy, sx)) = search(&block, SIDE, nb, bx, by); | |
| 18889 | + let mut sse = 0f64; | |
| 18890 | + for row in 0..SIDE { | |
| 18891 | + for col in 0..SIDE { | |
| 18892 | + let d = f64::from(block[row * SIDE + col]) | |
| 18893 | + - f64::from(nb.y[(sy + row) * nb.width + sx + col]); | |
| 18894 | + sse += d * d; | |
| 18895 | + } | |
| 18896 | + } | |
| 18897 | + let w = (-sse / ((SIDE * SIDE) as f64) / strength).exp(); | |
| 18898 | + for row in 0..SIDE { | |
| 18899 | + for col in 0..SIDE { | |
| 18900 | + acc[row * SIDE + col] += | |
| 18901 | + w * f64::from(nb.y[(sy + row) * nb.width + sx + col]); | |
| 18902 | + } | |
| 18903 | + } | |
| 18904 | + wsum += w; | |
| 18905 | + } | |
| 18906 | + let filtered: Vec<u16> = acc | |
| 18907 | + .iter() | |
| 18908 | + .map(|v| (v / wsum).round().clamp(0.0, 1023.0) as u16) | |
| 18909 | + .collect(); | |
| 18910 | + for (a, b) in filtered.iter().zip(&block) { | |
| 18911 | + let d = f64::from(*a) - f64::from(*b); | |
| 18912 | + tf_sse += d * d; | |
| 18913 | + } | |
| 18914 | + tf_px += (SIDE * SIDE) as u64; | |
| 18915 | + ntf += search(&filtered, SIDE, &pictures[i - 8], bx, by).0; | |
| 18916 | + px += (SIDE * SIDE) as u64; | |
| 18917 | + } | |
| 18918 | + } | |
| 18919 | + } | |
| 18920 | + let per = |v: u64| v as f64 / px.max(1) as f64; | |
| 18921 | + let mse = tf_sse / tf_px.max(1) as f64; | |
| 18922 | + println!( | |
| 18923 | + "| {name} | {arfs} | s{strength} p{past} | {:.3} | {:.3} | {:.3} | {:.1}% | {:.2} dB |", | |
| 18924 | + per(n8), | |
| 18925 | + per(n4), | |
| 18926 | + per(ntf), | |
| 18927 | + 100.0 * (n8 - ntf) as f64 / n8.max(1) as f64, | |
| 18928 | + 10.0 * (255.0f64 * 255.0 / mse.max(1e-9)).log10(), | |
| 18929 | + ); | |
| 18930 | + } | |
| 18931 | + } | |
| 18932 | + | |
| 18623 | 18933 | /// lane-refs: THE FRAME FILTER REACHES THE PIXELS. `mc::predict` used to |
| 18624 | 18934 | /// hardwire the REGULAR kernel, so the encoder could not have coded a |
| 18625 | 18935 | /// SMOOTH or SHARP frame even if the header had said so -- the search, |
crates/ec-av1/src/encoder.rsmodified+23 -1
| @@ -1067,6 +1067,16 @@ const KEY_DEFAULT: i16 = -48; | ||
| 1067 | 1067 | /// shipped on the flat path -- was inert on every frame of every pyramid |
| 1068 | 1068 | /// stream, including the ARFs that carry 54% of a long-GOP stream's bytes |
| 1069 | 1069 | /// (class `tool disabled in every gate recipe`). |
| 1070 | +/// lane-arfpred: the top ARF source temporal filter's strength | |
| 1071 | +/// ([`crate::speed::ARF_TF`]), `EC_AV1_ARF_TF=<strength>` overriding it; | |
| 1072 | +/// `0` is off. | |
| 1073 | +fn arf_tf_strength() -> f64 { | |
| 1074 | + std::env::var("EC_AV1_ARF_TF") | |
| 1075 | + .ok() | |
| 1076 | + .and_then(|v| v.parse().ok()) | |
| 1077 | + .unwrap_or_else(|| crate::speed::at(&crate::speed::ARF_TF)) | |
| 1078 | +} | |
| 1079 | + | |
| 1070 | 1080 | fn tpl_pyramid() -> bool { |
| 1071 | 1081 | match std::env::var("EC_AV1_TPL_PYRAMID").ok() { |
| 1072 | 1082 | Some(v) => v != "0", |
| @@ -1782,8 +1792,20 @@ impl Av1Encoder { | ||
| 1782 | 1792 | .then(|| self.dpb[altref_slot as usize].as_ref().map(|s| s.picture.clone())) |
| 1783 | 1793 | .flatten(); |
| 1784 | 1794 | let start_cdfs = self.dpb[last_slot as usize].as_ref().map(|s| s.cdfs.0.clone()); |
| 1795 | + // lane-arfpred: the top ARF is coded from a TEMPORALLY FILTERED | |
| 1796 | + // source when `speed::ARF_TF` is non-zero -- the grain it would | |
| 1797 | + // otherwise spend its residual on is averaged out against the | |
| 1798 | + // group's own display neighbours (`lookahead`, already padded). | |
| 1799 | + let source = picture.padded_to(SUPERBLOCK); | |
| 1800 | + let tf = arf_tf_strength(); | |
| 1801 | + let source = match (tf > 0.0, dq_level) { | |
| 1802 | + (true, crate::encode::DqLevel::TopArf) => { | |
| 1803 | + crate::encode::arf_temporal_filter(&source, lookahead, tf) | |
| 1804 | + } | |
| 1805 | + _ => source, | |
| 1806 | + }; | |
| 1785 | 1807 | let encoded = || -> Result<Encoded> { encode_inter_frame( |
| 1786 | - &picture.padded_to(SUPERBLOCK), | |
| 1808 | + &source, | |
| 1787 | 1809 | &reference, |
| 1788 | 1810 | base_q_idx, |
| 1789 | 1811 | DEADZONE, |
crates/ec-av1/src/speed.rsmodified+14 -0
| @@ -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, | ||
| 366 | 366 | /// `lanes/arfq.report.md`. |
| 367 | 367 | pub(crate) const DQ_LEVEL_K: [f64; 3] = [1.5, 1.0, 1.0]; |
| 368 | 368 | |
| 369 | +/// lane-arfpred: the strength of the top ARF's SOURCE temporal filter | |
| 370 | +/// ([`crate::encode::arf_temporal_filter`]) -- `0` is off, higher filters | |
| 371 | +/// harder (the weight of a motion-compensated neighbour is | |
| 372 | +/// `exp(-mse / strength)`). `EC_AV1_ARF_TF=<strength>` overrides it. | |
| 373 | +/// | |
| 374 | +/// The top ARF carries 55% of a 48-picture film B stream at 6992 B a frame | |
| 375 | +/// against rav1e's 3872 at a FINER quantizer, and the per-frame census says | |
| 376 | +/// 61% of that gap is residual, not syntax -- while `arf_pred_census` says | |
| 377 | +/// the anchor's own lag is worth only 7% of its prediction SAD (lag 8 1.199 | |
| 378 | +/// vs lag 4 1.114 per pixel), so what the residual codes is mostly source | |
| 379 | +/// grain. Filtering it out of the picture the anchor is coded from is the | |
| 380 | +/// mechanism libaom uses (`arnr`) and rav1e 0.8.1 does not have at all. | |
| 381 | +pub(crate) const ARF_TF: [f64; 11] = [0.0; 11]; | |
| 382 | + | |
| 369 | 383 | /// `encode::SPLIT_RD_THRESHOLD`: how cheap a block has to be before its split |
| 370 | 384 | /// trial is withheld. The single biggest wall lever in the tile search. |
| 371 | 385 | pub(crate) const SPLIT_RD: [f64; 11] = [ |