M13.4: av1enc sub-sample motion search, deblocking loop filter
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
646d3fa15f
commit
917e5f7ae8
@@ -0,0 +1,95 @@
|
||||
//! Speed / efficiency measurements on ffmpeg-generated test sources (ignored by default):
|
||||
//! `cargo test -p effectcraft-av1enc --test bench -- --ignored --nocapture`.
|
||||
|
||||
mod common;
|
||||
|
||||
use std::process::Command;
|
||||
use std::time::Instant;
|
||||
|
||||
use common::*;
|
||||
use effectcraft_av1enc::{Encoder, EncoderConfig, RateControl};
|
||||
|
||||
/// Raw 8-bit 4:2:0 frames of an ffmpeg lavfi source, or None without ffmpeg.
|
||||
fn lavfi(src: &str, w: usize, h: usize, frames: usize) -> Option<Vec<Pic>> {
|
||||
let out = std::env::temp_dir().join(format!("effectcraft-av1enc-bench-{}-{}.yuv", std::process::id(), src.split('=').next().unwrap_or("src")));
|
||||
let st = Command::new("ffmpeg")
|
||||
.args(["-v", "error", "-y", "-f", "lavfi", "-i"])
|
||||
.arg(format!("{src}=size={w}x{h}:rate=30"))
|
||||
.args(["-frames:v", &frames.to_string(), "-pix_fmt", "yuv420p", "-f", "rawvideo"])
|
||||
.arg(&out)
|
||||
.status()
|
||||
.ok()?;
|
||||
if !st.success() {
|
||||
return None;
|
||||
}
|
||||
let raw = std::fs::read(&out).ok()?;
|
||||
let _ = std::fs::remove_file(&out);
|
||||
let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
|
||||
let fs = w * h + 2 * cw * ch;
|
||||
Some(
|
||||
raw.chunks_exact(fs)
|
||||
.map(|f| Pic {
|
||||
w,
|
||||
h,
|
||||
y: f[..w * h].iter().map(|&b| b as u16).collect(),
|
||||
u: f[w * h..w * h + cw * ch].iter().map(|&b| b as u16).collect(),
|
||||
v: f[w * h + cw * ch..].iter().map(|&b| b as u16).collect(),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn run(name: &str, pics: &[Pic], rate: RateControl, keyint: u32) {
|
||||
let (w, h) = (pics[0].w, pics[0].h);
|
||||
let mut cfg = EncoderConfig::new(w as u32, h as u32, 30, 1);
|
||||
cfg.rate = rate;
|
||||
cfg.keyint = keyint;
|
||||
let mut enc = Encoder::new(cfg).unwrap();
|
||||
let mut bytes = 0;
|
||||
let mut psnr_sum = 0.0;
|
||||
let mut t_key = 0.0;
|
||||
let mut t_inter = 0.0;
|
||||
let mut sizes = Vec::new();
|
||||
for p in pics {
|
||||
let t = Instant::now();
|
||||
let pkt = enc.encode(&p.frame());
|
||||
let dt = t.elapsed().as_secs_f64();
|
||||
if pkt.keyframe {
|
||||
t_key += dt;
|
||||
} else {
|
||||
t_inter += dt;
|
||||
}
|
||||
bytes += pkt.data.len();
|
||||
sizes.push(pkt.data.len());
|
||||
let rec = enc.last_reconstruction().unwrap();
|
||||
psnr_sum += psnr(&p.y, &rec[0], 8);
|
||||
}
|
||||
let n = pics.len() as f64;
|
||||
let n_key = pics.len().div_ceil(keyint as usize) as f64;
|
||||
eprintln!(
|
||||
"{name} {w}x{h} {rate:?}: {:.0} kbit/s, PSNR-Y {:.2} dB, key {:.2} s/frame, inter {:.2} s/frame, sizes {:?}",
|
||||
bytes as f64 * 8.0 * 30.0 / n / 1000.0,
|
||||
psnr_sum / n,
|
||||
t_key / n_key,
|
||||
if n > n_key { t_inter / (n - n_key) } else { 0.0 },
|
||||
&sizes[..sizes.len().min(6)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn bench_lavfi() {
|
||||
if std::env::var("AV1ENC_BENCH_QUICK").is_err() {
|
||||
let Some(pics) = lavfi("testsrc2", 1920, 1080, 6) else {
|
||||
eprintln!("ffmpeg not available");
|
||||
return;
|
||||
};
|
||||
run("testsrc2", &pics, RateControl::ConstantQ(100), 30);
|
||||
}
|
||||
if let Some(pics) = lavfi("mandelbrot", 640, 360, 20) {
|
||||
for q in [40, 80, 120, 160] {
|
||||
run("mandelbrot", &pics, RateControl::ConstantQ(q), 30);
|
||||
}
|
||||
run("mandelbrot", &pics, RateControl::Bitrate { kbps: 1000 }, 30);
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,7 @@ pub struct Run {
|
||||
pub packets: Vec<Packet>,
|
||||
pub recons: Vec<[Vec<u16>; 3]>,
|
||||
pub psnr_y: Vec<f64>,
|
||||
pub lf_levels: Vec<u32>,
|
||||
}
|
||||
|
||||
/// Encodes `pics`, checks every packet decodes (filmcraft-av1) to exactly the encoder's
|
||||
@@ -91,7 +92,7 @@ pub fn encode_and_check(cfg: EncoderConfig, pics: &[Pic]) -> Run {
|
||||
let bd = cfg.bit_depth as u32;
|
||||
let mut enc = Encoder::new(cfg).expect("config");
|
||||
let mut dec = filmcraft_av1::Decoder::new();
|
||||
let mut run = Run { packets: Vec::new(), recons: Vec::new(), psnr_y: Vec::new() };
|
||||
let mut run = Run { packets: Vec::new(), recons: Vec::new(), psnr_y: Vec::new(), lf_levels: Vec::new() };
|
||||
for (i, p) in pics.iter().enumerate() {
|
||||
let pkt = enc.encode(&p.frame());
|
||||
let rec = enc.last_reconstruction().expect("reconstruction");
|
||||
@@ -107,6 +108,7 @@ pub fn encode_and_check(cfg: EncoderConfig, pics: &[Pic]) -> Run {
|
||||
}
|
||||
}
|
||||
run.psnr_y.push(psnr(&p.y, &rec[0], bd));
|
||||
run.lf_levels.push(enc.last_loop_filter_level().expect("level"));
|
||||
run.recons.push(rec);
|
||||
run.packets.push(pkt);
|
||||
}
|
||||
|
||||
@@ -45,14 +45,17 @@ fn ivf(w: usize, h: usize, packets: &[Packet]) -> Vec<u8> {
|
||||
|
||||
/// Encodes `pics`, decodes the IVF stream with `ffmpeg -c:v libdav1d` and compares every
|
||||
/// decoded frame with the encoder's reconstruction.
|
||||
fn check(name: &str, cfg: EncoderConfig, pics: &[Pic]) {
|
||||
/// Returns the loop filter levels used.
|
||||
fn check(name: &str, cfg: EncoderConfig, pics: &[Pic]) -> Vec<u32> {
|
||||
let (w, h, bd) = (cfg.width as usize, cfg.height as usize, cfg.bit_depth);
|
||||
let mut enc = Encoder::new(cfg).expect("config");
|
||||
let mut packets = Vec::new();
|
||||
let mut recons = Vec::new();
|
||||
let mut levels = Vec::new();
|
||||
for p in pics {
|
||||
packets.push(enc.encode(&p.frame()));
|
||||
recons.push(enc.last_reconstruction().expect("reconstruction"));
|
||||
levels.push(enc.last_loop_filter_level().expect("level"));
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("effectcraft-av1enc-{}-{name}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("temp dir");
|
||||
@@ -87,7 +90,12 @@ fn check(name: &str, cfg: EncoderConfig, pics: &[Pic]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
eprintln!("{name}: libdav1d bit-exact over {} frames ({} bytes)", pics.len(), packets.iter().map(|p| p.data.len()).sum::<usize>());
|
||||
eprintln!(
|
||||
"{name}: libdav1d bit-exact over {} frames ({} bytes, loop filter levels {levels:?})",
|
||||
pics.len(),
|
||||
packets.iter().map(|p| p.data.len()).sum::<usize>()
|
||||
);
|
||||
levels
|
||||
}
|
||||
|
||||
fn cfg(w: usize, h: usize, bd: u8, rate: RateControl, keyint: u32) -> EncoderConfig {
|
||||
@@ -116,7 +124,12 @@ fn libdav1d_decodes_bit_exact() {
|
||||
check("odd10", cfg(101, 75, 10, RateControl::ConstantQ(110), 4), &pics);
|
||||
// a noisy moving picture, bitrate mode
|
||||
let pics: Vec<Pic> = (0..6).map(|t| noisy(128, 80, t)).collect();
|
||||
check("bitrate", cfg(128, 80, 8, RateControl::Bitrate { kbps: 300 }, 10), &pics);
|
||||
let levels = check("bitrate", cfg(128, 80, 8, RateControl::Bitrate { kbps: 300 }, 10), &pics);
|
||||
assert!(levels.iter().any(|&l| l > 0), "the deblocking filter is exercised");
|
||||
// a coarse quantizer: strong deblocking, 10 bit
|
||||
let pics: Vec<Pic> = (0..4).map(|t| synth(120, 88, 10, t)).collect();
|
||||
let levels = check("deblock10", cfg(120, 88, 10, RateControl::ConstantQ(220), 2), &pics);
|
||||
assert!(levels.iter().any(|&l| l > 0), "the deblocking filter is exercised");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user