FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#1871: Lens Blur works out its disc offsets once per frame, not per pixel
Guilherme Barbosa Ferrarezi98f257f56e2c
diff --git a/crates/render/src/transitions/lights.rs b/crates/render/src/transitions/lights.rsindex a9930a72e..932f91162 100644--- a/crates/render/src/transitions/lights.rs+++ b/crates/render/src/transitions/lights.rs@@ -5,7 +5,10 @@ use std::f32::consts::PI;use super::dissolve::perceptual_luma;use super::wipe::angle_dir;-use super::{Tx, add_light, bell, blurred, disc_blur, ease_in, ease_out, hash, line_blur, luma, mid_mix, mix, paint, smoothstep, spin_blur, taps, zoom_blur};+use super::{+ Tx, add_light, bell, blurred, disc_blur_with, disc_offsets, ease_in, ease_out, hash, line_blur, luma, mid_mix, mix, paint, smoothstep, spin_blur, taps,+ zoom_blur,+};use crate::image::Image;/// Soft round light blob.@@ -142,9 +145,11 @@ pub(super) fn apply(id: &str, t: &Tx) -> Option<Image> {let ra = r * smoothstep(0.0, 0.55, p);let rb = r * (1.0 - smoothstep(0.45, 1.0, p));let n = ((r * 0.8) as usize).clamp(8, 48);+ // the disc's sample offsets once per frame, not per pixel (#1871)+ let (oa, ob) = (disc_offsets(ra, n), disc_offsets(rb, n));paint(t.w, t.h, |x, y| {- let a = if k < 1.0 { disc_blur(t.a, x, y, ra, n) } else { [0.0; 4] };- let b = if k > 0.0 { disc_blur(t.b, x, y, rb, n) } else { [0.0; 4] };+ let a = if k < 1.0 { disc_blur_with(t.a, x, y, &oa) } else { [0.0; 4] };+ let b = if k > 0.0 { disc_blur_with(t.b, x, y, &ob) } else { [0.0; 4] };mix(a, b, k)})}diff --git a/crates/render/src/transitions/mod.rs b/crates/render/src/transitions/mod.rsindex 27c26922e..651f8de4a 100644--- a/crates/render/src/transitions/mod.rs+++ b/crates/render/src/transitions/mod.rs@@ -337,21 +337,37 @@ pub(crate) fn spin_blur(img: &Image, x: f32, y: f32, cx: f32, cy: f32, ang: f32,}scale4(acc, 1.0 / n as f32)}-/// Disc (bokeh-like) blur of radius `r` with `n` golden-angle samples.-pub(crate) fn disc_blur(img: &Image, x: f32, y: f32, r: f32, n: usize) -> [f32; 4] {+/// The sample offsets of a disc (bokeh-like) blur of radius `r` with `n` golden-angle samples+/// (empty: no blur), for [`disc_blur_with`]. They depend+/// only on `r` and `n`, so a frame works them out once instead of for every pixel (#1871: the+/// square root, sine and cosine of every sample of every pixel were most of a Lens Blur frame).+pub(crate) fn disc_offsets(r: f32, n: usize) -> Vec<(f32, f32)> {if r < 0.5 || n <= 1 {+ return Vec::new();+ }+ (0..n)+ .map(|i| {+ let rr = r * ((i as f32 + 0.5) / n as f32).sqrt();+ let a = i as f32 * 2.399_963;+ (rr * a.cos(), rr * a.sin())+ })+ .collect()+}++/// Disc (bokeh-like) blur at `(x, y)` with `n` golden-angle samples, their offsets from+/// [`disc_offsets`].+pub(crate) fn disc_blur_with(img: &Image, x: f32, y: f32, offsets: &[(f32, f32)]) -> [f32; 4] {+ if offsets.is_empty() {return img.sample_bilinear_clamped(x, y);}let mut acc = [0.0; 4];- for i in 0..n {- let rr = r * ((i as f32 + 0.5) / n as f32).sqrt();- let a = i as f32 * 2.399_963;- let p = img.sample_bilinear_clamped(x + rr * a.cos(), y + rr * a.sin());+ for &(dx, dy) in offsets {+ let p = img.sample_bilinear_clamped(x + dx, y + dy);for k in 0..4 {acc[k] += p[k];}}- scale4(acc, 1.0 / n as f32)+ scale4(acc, 1.0 / offsets.len() as f32)}/// Sample count for a blur of extent `len` pixels.#[inline]diff --git a/crates/render/src/transitions/tests.rs b/crates/render/src/transitions/tests.rsindex a096c55fe..e2f25a184 100644--- a/crates/render/src/transitions/tests.rs+++ b/crates/render/src/transitions/tests.rs@@ -420,3 +420,62 @@ fn contact_sheets() {sheet.save(std::path::Path::new(&dir).join(format!("{f}.png"))).unwrap();}}++/// The disc blur as it was before #1871: offsets worked out for every pixel.+fn disc_blur_per_pixel(img: &Image, x: f32, y: f32, r: f32, n: usize) -> [f32; 4] {+ if r < 0.5 || n <= 1 {+ return img.sample_bilinear_clamped(x, y);+ }+ let mut acc = [0.0; 4];+ for i in 0..n {+ let rr = r * ((i as f32 + 0.5) / n as f32).sqrt();+ let a = i as f32 * 2.399_963;+ let p = img.sample_bilinear_clamped(x + rr * a.cos(), y + rr * a.sin());+ for k in 0..4 {+ acc[k] += p[k];+ }+ }+ scale4(acc, 1.0 / n as f32)+}++/// #1871: offsets worked out once per frame give exactly the pixels the per-pixel version gave.+#[test]+fn disc_blur_with_shared_offsets_is_bit_identical() {+ let img = frame_b();+ for (r, n) in [(0.0, 24), (0.4, 24), (3.0, 8), (30.0, 24), (75.5, 48), (200.0, 48), (12.0, 1)] {+ let offsets = disc_offsets(r, n);+ for y in (0..H).step_by(7) {+ for x in (0..W).step_by(5) {+ let (fx, fy) = (x as f32 + 0.5, y as f32 + 0.5);+ let a = disc_blur_per_pixel(&img, fx, fy, r, n);+ let b = disc_blur_with(&img, fx, fy, &offsets);+ assert_eq!(a.map(f32::to_bits), b.map(f32::to_bits), "r {r}, n {n}, at ({x}, {y})");+ }+ }+ }+}++/// `cargo test --release -p filmcraft-render --lib lens_blur_frame_time -- --ignored --nocapture`+#[test]+#[ignore]+fn lens_blur_frame_time() {+ let (w, h) = (1920, 1080);+ let img = paint(w, h, |x, y| [x / w as f32, y / h as f32, 0.5, 1.0]);+ let (r, n) = (30.0, 24);+ let time = |f: &dyn Fn() -> Image| {+ let t = std::time::Instant::now();+ let mut out = f();+ for _ in 0..4 {+ out = f();+ }+ (t.elapsed().as_secs_f64() * 1000.0 / 5.0, out)+ };+ let (before, a) = time(&|| paint(w, h, |x, y| disc_blur_per_pixel(&img, x, y, r, n)));+ let offsets = disc_offsets(r, n);+ let (after, b) = time(&|| paint(w, h, |x, y| disc_blur_with(&img, x, y, &offsets)));+ assert_eq!(a.px, b.px);+ eprintln!(+ "1080p disc blur of one picture, radius {r}, {n} samples: {before:.1} ms per pixel offsets, {after:.1} ms shared offsets ({:.2}x)",+ before / after+ );+}