FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
render: advance Noise grain from exact frame ticks
Nigel Ihza Firdausy4969601e9ac4
diff --git a/crates/render/src/effects.rs b/crates/render/src/effects.rsindex f4eeb3ba0..b8dfce28b 100644--- a/crates/render/src/effects.rs+++ b/crates/render/src/effects.rs@@ -77,6 +77,13 @@ pub(crate) fn dec(c: [f32; 3]) -> [f32; 3] {[srgb_to_linear(c[0].clamp(0.0, 1.0)), srgb_to_linear(c[1].clamp(0.0, 1.0)), srgb_to_linear(c[2].clamp(0.0, 1.0))]}+/// Stable seed for the *exact* frame timestamp. A fixed 30 Hz quantizer+/// would reuse the same noise field at 60/120 fps and can repeat at 30 fps+/// through floating-point rounding of seconds.+pub(crate) fn noise_frame_seed(cx: &FxCtx) -> u64 {+ cx.t.0 as u64+}+pub(crate) fn hash3(x: usize, y: usize, z: u64) -> f32 {let mut h = (x as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ (y as u64).wrapping_mul(0xC2B2_AE3D_27D4_EB4F) ^ z.wrapping_mul(0x1656_67B1_9E37_79F9);h ^= h >> 31;@@ -110,7 +117,7 @@ pub fn apply(img: &mut Image, e: &EffectInstance, cx: &FxCtx) {let amt = f(e, "amount", cx) / 100.0;let colored = b(e, "color");let clip = b(e, "clip");- let frame = (cx.seconds * 30.0) as u64;+ let frame = noise_frame_seed(cx);let w = img.w;img.px.par_chunks_mut(w * 4).enumerate().for_each(|(y, row)| {for x in 0..w {diff --git a/crates/render/src/vfx/color.rs b/crates/render/src/vfx/color.rsindex 309e6d729..5bff8ff82 100644--- a/crates/render/src/vfx/color.rs+++ b/crates/render/src/vfx/color.rs@@ -337,7 +337,7 @@ pub fn noise(img: &mut Image, e: &EffectInstance, cx: &FxCtx) {let colored = bv(e, "color");let clip = bv(e, "clip");let grain = (fv(e, "grain_size", cx).max(1.0) * cx.px_scale).max(1.0);- let frame = (cx.seconds * 30.0) as u64;+ let frame = crate::effects::noise_frame_seed(cx);let w = img.w;img.px.par_chunks_mut(w * 4).enumerate().for_each(|(y, row)| {for x in 0..w {diff --git a/crates/render/tests/noise_frame_cadence.rs b/crates/render/tests/noise_frame_cadence.rsnew file mode 100644index 000000000..57a6a7e18--- /dev/null+++ b/crates/render/tests/noise_frame_cadence.rs@@ -0,0 +1,39 @@+use filmcraft_project::{ParamValue, find_effect};+use filmcraft_render::{Image, effects::{FxCtx, apply}};+use filmcraft_time::{FrameRate, Tick};++fn render_noise(effect_id: &str, rate: FrameRate, frame: i64, amount: f64) -> Vec<f32> {+ let mut image = Image::filled(16, 16, [0.5, 0.5, 0.5, 1.0]);+ let mut effect = find_effect(effect_id).unwrap().instance();+ effect.param_mut("amount").unwrap().value = ParamValue::Float(amount);+ let tick = rate.tick_of(frame);+ let context = FxCtx {+ t: tick,+ px_scale: 1.0,+ seconds: tick.seconds(),+ timecode: "00:00:00:00",+ clip_name: "generated matte",+ project: None,+ env: None,+ working: filmcraft_color::WorkingSpace::Rec709,+ };+ apply(&mut image, &effect, &context);+ image.px+}++#[test]+fn both_noise_variants_change_on_every_sequence_frame() {+ for effect_id in ["noise", "noise_legacy"] {+ for rate in [FrameRate::FPS_24, FrameRate::FPS_30, FrameRate::FPS_60, FrameRate::FPS_120] {+ for frame in 0..5 {+ let this = render_noise(effect_id, rate, frame, 100.0);+ let next = render_noise(effect_id, rate, frame + 1, 100.0);+ assert_ne!(this, next, "{effect_id} {rate:?} repeated frames {frame} and {}", frame + 1);+ assert_eq!(this, render_noise(effect_id, rate, frame, 100.0), "same timestamp must be deterministic");+ }+ }+ // Floating-point second-to-30Hz conversion also repeated frames 122/123 at 30 fps.+ assert_ne!(render_noise(effect_id, FrameRate::FPS_30, 122, 100.0), render_noise(effect_id, FrameRate::FPS_30, 123, 100.0));+ assert_eq!(render_noise(effect_id, FrameRate::FPS_60, 0, 0.0), vec![0.5, 0.5, 0.5, 1.0].repeat(16 * 16));+ }+}