Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

style: rustfmt over the stabilization code (#253)

Jareer4e3102f19453
diff --git a/src/crates/concat-host/src/stabilize.rs b/src/crates/concat-host/src/stabilize.rs
index bd4d3c35..7350042e 100644
--- a/src/crates/concat-host/src/stabilize.rs
+++ b/src/crates/concat-host/src/stabilize.rs
@@ -239,8 +239,9 @@ fn analysis_size(request: &StabilizeRequest) -> Result<(u32, u32), String> {
let (width, height) = match request.media_size.filter(|(w, h)| *w > 0 && *h > 0) {
Some(size) => size,
None => {
- let decoder = Decoder::open(&request.media_path, &DecodeOptions::default().limited_to(1))
- .map_err(|error| format!("{}: {error}", request.media_path))?;
+ let decoder =
+ Decoder::open(&request.media_path, &DecodeOptions::default().limited_to(1))
+ .map_err(|error| format!("{}: {error}", request.media_path))?;
(decoder.width(), decoder.height())
}
};
diff --git a/src/crates/concat-host/tests/stabilize.rs b/src/crates/concat-host/tests/stabilize.rs
index e6654260..13411f0d 100644
--- a/src/crates/concat-host/tests/stabilize.rs
+++ b/src/crates/concat-host/tests/stabilize.rs
@@ -245,9 +245,14 @@ fn a_shaky_shot_is_measured_and_held_still() {
let (x0, y0) = camera(k - 1);
let (x1, y1) = camera(k);
let (dx, dy) = (frame.motion.tx * h, frame.motion.ty * h);
- worst = worst.max((dx - (x1 - x0)).abs()).max((dy - (y1 - y0)).abs());
+ worst = worst
+ .max((dx - (x1 - x0)).abs())
+ .max((dy - (y1 - y0)).abs());
assert!(frame.motion.angle().abs() < 0.002, "frame {k} turned");
- assert!((frame.motion.scale() - 1.0).abs() < 0.002, "frame {k} zoomed");
+ assert!(
+ (frame.motion.scale() - 1.0).abs() < 0.002,
+ "frame {k} zoomed"
+ );
}
eprintln!("worst step error: {worst:.3} px");
assert!(worst < 0.2, "a step measured {worst:.3} px off");
@@ -270,7 +275,10 @@ fn a_shaky_shot_is_measured_and_held_still() {
// little of the path through.
let (shaky, held) = (wobble(&plain[10..50]), wobble(&steady[10..50]));
eprintln!("frame-to-frame difference: as shot {shaky:.2}, stabilized {held:.2}");
- assert!(held < shaky * 0.35, "as shot {shaky:.2}, stabilized {held:.2}");
+ assert!(
+ held < shaky * 0.35,
+ "as shot {shaky:.2}, stabilized {held:.2}"
+ );
// The monitor draws what the export wrote: the same frame, to within
// what the encoder changed, and nowhere near the frame as shot.
diff --git a/src/crates/concat-render/src/gpu/tests.rs b/src/crates/concat-render/src/gpu/tests.rs
index 78684f83..100577b5 100644
--- a/src/crates/concat-render/src/gpu/tests.rs
+++ b/src/crates/concat-render/src/gpu/tests.rs
@@ -206,12 +206,18 @@ fn a_stabilized_frame_is_the_source_through_its_warp() {
};
let out = gpu.render(&plan(w, h, vec![turned.clone()]));
let probe = |x: f32, y: f32| {
- let (nx, ny) = ((x - w as f32 / 2.0) / h as f32, (y - h as f32 / 2.0) / h as f32);
+ let (nx, ny) = (
+ (x - w as f32 / 2.0) / h as f32,
+ (y - h as f32 / 2.0) / h as f32,
+ );
let (mx, my) = (
turned.warp.a * nx - turned.warp.b * ny,
turned.warp.b * nx + turned.warp.a * ny,
);
- (mx * h as f32 + w as f32 / 2.0, my * h as f32 + h as f32 / 2.0)
+ (
+ mx * h as f32 + w as f32 / 2.0,
+ my * h as f32 + h as f32 / 2.0,
+ )
};
for (x, y) in [(52u32, 10u32), (10, 36), (48, 40), (40, 6)] {
let (sx, sy) = probe(x as f32 + 0.5, y as f32 + 0.5);
@@ -231,7 +237,11 @@ fn a_stabilized_frame_is_the_source_through_its_warp() {
let mut cropped = turned.clone();
cropped.crop = Crop::of([0.1, 0.0, 0.2, 0.1]);
cropped.flip_h = true;
- assert_parity("stabilized and cropped", &plan(w, h, vec![cropped.clone()]), 0.99);
+ assert_parity(
+ "stabilized and cropped",
+ &plan(w, h, vec![cropped.clone()]),
+ 0.99,
+ );
let mut prepared = cropped;
prepared.effects = vec![package("test.invert", INVERT, "", &[], 1.0)];
let geometry = prepared.geometry(&source, w, h);
diff --git a/src/crates/concat-vision/src/stabilize/fit.rs b/src/crates/concat-vision/src/stabilize/fit.rs
index 86f0dea6..311b8429 100644
--- a/src/crates/concat-vision/src/stabilize/fit.rs
+++ b/src/crates/concat-vision/src/stabilize/fit.rs
@@ -253,7 +253,10 @@ pub fn ransac(pairs: &[Pair], threshold: f64, min_inliers: usize) -> Option<Robu
if !(0.75..=1.333).contains(&candidate.scale()) {
continue;
}
- let count = agreeing(&candidate).iter().filter(|&&inlier| inlier).count();
+ let count = agreeing(&candidate)
+ .iter()
+ .filter(|&&inlier| inlier)
+ .count();
if best.is_none_or(|(most, _)| count > most) {
best = Some((count, candidate));
// Enough draws that a clean pair was very likely among them:
diff --git a/src/crates/concat-vision/src/stabilize/path.rs b/src/crates/concat-vision/src/stabilize/path.rs
index 8bdb9ab1..3866c59b 100644
--- a/src/crates/concat-vision/src/stabilize/path.rs
+++ b/src/crates/concat-vision/src/stabilize/path.rs
@@ -275,7 +275,10 @@ fn box_min(values: &[f64], radius: usize) -> Vec<f64> {
.map(|i| {
let from = i.saturating_sub(radius);
let to = (i + radius + 1).min(n);
- values[from..to].iter().copied().fold(f64::INFINITY, f64::min)
+ values[from..to]
+ .iter()
+ .copied()
+ .fold(f64::INFINITY, f64::min)
})
.collect()
}
@@ -290,7 +293,11 @@ pub fn correct(
settings: &Settings,
) -> Correction {
let interval = motion.interval();
- let speed = if speed.is_finite() { speed.abs().max(1.0 / 16.0) } else { 1.0 };
+ let speed = if speed.is_finite() {
+ speed.abs().max(1.0 / 16.0)
+ } else {
+ 1.0
+ };
let sigma_source = sigma_seconds(settings.smoothness) * speed;
let sigma_frames = sigma_source / interval;
// The frames the clip shows, and the smoothing's reach either side,
@@ -485,7 +492,9 @@ mod tests {
for (a, b) in line.iter().zip(&smoothed) {
assert!((a - b).abs() < 1e-9, "a line through the ends stays a line");
}
- let wobble: Vec<f64> = (0..200).map(|i| if i % 2 == 0 { 1.0 } else { -1.0 }).collect();
+ let wobble: Vec<f64> = (0..200)
+ .map(|i| if i % 2 == 0 { 1.0 } else { -1.0 })
+ .collect();
let smoothed = gaussian(&wobble, 4.0);
assert!(smoothed[20..180].iter().all(|v| v.abs() < 0.02));
}
@@ -531,7 +540,11 @@ mod tests {
let (x, _) = correction.at(at).apply((0.0, 0.0));
assert!((x - offset).abs() < 0.2 / h, "frame {frame}: {} px", x * h);
}
- assert!((correction.shake * h - 2.0).abs() < 0.2, "{}", correction.shake * h);
+ assert!(
+ (correction.shake * h - 2.0).abs() < 0.2,
+ "{}",
+ correction.shake * h
+ );
}
/// A steady pan stays a pan: nothing to correct, nothing to zoom.
diff --git a/src/crates/concat-vision/src/stabilize/store.rs b/src/crates/concat-vision/src/stabilize/store.rs
index c28ef1cd..0fb3d6fb 100644
--- a/src/crates/concat-vision/src/stabilize/store.rs
+++ b/src/crates/concat-vision/src/stabilize/store.rs
@@ -120,7 +120,10 @@ impl Motion {
OnceLock::new();
let file = motion_file(dir);
let meta = std::fs::metadata(&file).ok()?;
- let stamp = (meta.modified().unwrap_or(SystemTime::UNIX_EPOCH), meta.len());
+ let stamp = (
+ meta.modified().unwrap_or(SystemTime::UNIX_EPOCH),
+ meta.len(),
+ );
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
if let Ok(cache) = cache.lock()
&& let Some((modified, len, motion)) = cache.get(&file)
@@ -185,7 +188,10 @@ impl Motion {
/// The text form.
pub fn text(&self) -> String {
- let mut out = format!("concat-stabilize {ANALYSIS_VERSION}\naspect {}\n", self.aspect);
+ let mut out = format!(
+ "concat-stabilize {ANALYSIS_VERSION}\naspect {}\n",
+ self.aspect
+ );
for (from, to) in &self.covered {
let _ = writeln!(out, "covered {from:.6} {to:.6}");
}
@@ -248,7 +254,9 @@ impl Motion {
/// and this one says how it was reached.
pub fn merge(&mut self, run: &[FrameMotion], span: (f64, f64)) {
for frame in run {
- let index = self.frames.partition_point(|f| f.at < frame.at - SAME_FRAME);
+ let index = self
+ .frames
+ .partition_point(|f| f.at < frame.at - SAME_FRAME);
match self.frames.get_mut(index) {
Some(existing) if (existing.at - frame.at).abs() <= SAME_FRAME => {
if existing.step == Step::Start && frame.step != Step::Start {
@@ -351,7 +359,10 @@ mod tests {
aspect: 1.0,
..Motion::default()
};
- motion.merge(&[frame(1.0, Step::Start), frame(1.1, Step::Tracked)], (1.0, 1.1));
+ motion.merge(
+ &[frame(1.0, Step::Start), frame(1.1, Step::Tracked)],
+ (1.0, 1.1),
+ );
motion.merge(
&[
frame(0.8, Step::Start),
diff --git a/src/crates/concat-vision/src/stabilize/track.rs b/src/crates/concat-vision/src/stabilize/track.rs
index bee2dc82..3c71e573 100644
--- a/src/crates/concat-vision/src/stabilize/track.rs
+++ b/src/crates/concat-vision/src/stabilize/track.rs
@@ -50,7 +50,9 @@ impl Gray {
let data = frame
.pixels()
.chunks_exact(4)
- .map(|px| 0.299 * f32::from(px[0]) + 0.587 * f32::from(px[1]) + 0.114 * f32::from(px[2]))
+ .map(|px| {
+ 0.299 * f32::from(px[0]) + 0.587 * f32::from(px[1]) + 0.114 * f32::from(px[2])
+ })
.collect();
Gray {
width,
@@ -74,8 +76,7 @@ impl Gray {
let y0 = y.floor();
let (fx, fy) = (x - x0, y - y0);
let (x0, y0) = (x0 as isize, y0 as isize);
- if x0 >= 0 && y0 >= 0 && (x0 as usize) + 1 < self.width && (y0 as usize) + 1 < self.height
- {
+ if x0 >= 0 && y0 >= 0 && (x0 as usize) + 1 < self.width && (y0 as usize) + 1 < self.height {
let at = y0 as usize * self.width + x0 as usize;
let (a, b) = (self.data[at], self.data[at + 1]);
let (c, d) = (self.data[at + self.width], self.data[at + self.width + 1]);
@@ -274,8 +275,7 @@ impl Pyramid {
let mut k = 0;
for dy in -RADIUS..=RADIUS {
for dx in -RADIUS..=RADIUS {
- let residual =
- window_i[k] - there.sample(qx + dx as f32, qy + dy as f32);
+ let residual = window_i[k] - there.sample(qx + dx as f32, qy + dy as f32);
bx += residual * window_x[k];
by += residual * window_y[k];
k += 1;
@@ -553,7 +553,9 @@ impl Tracker {
let handles: Vec<_> = self
.points
.chunks(chunk)
- .map(|part| scope.spawn(move || part.iter().filter_map(|&p| one(p)).collect::<Vec<_>>()))
+ .map(|part| {
+ scope.spawn(move || part.iter().filter_map(|&p| one(p)).collect::<Vec<_>>())
+ })
.collect();
handles
.into_iter()
@@ -586,7 +588,11 @@ mod tests {
}
// Soft-edged squares, for corners proper: a lens never draws an
// edge sharper than a pixel.
- v + 25.0 * (3.0 * (x * std::f64::consts::PI / 23.0).sin() * (y * std::f64::consts::PI / 19.0).sin()).tanh()
+ v + 25.0
+ * (3.0
+ * (x * std::f64::consts::PI / 23.0).sin()
+ * (y * std::f64::consts::PI / 19.0).sin())
+ .tanh()
}
/// The scene seen through `camera` (normalised units): each pixel of
@@ -656,7 +662,11 @@ mod tests {
"angle {}",
found.angle().to_degrees()
);
- assert!((found.scale() - 1.02).abs() < 0.002, "scale {}", found.scale());
+ assert!(
+ (found.scale() - 1.02).abs() < 0.002,
+ "scale {}",
+ found.scale()
+ );
assert!(((found.tx - camera.tx) * h as f64).abs() < 0.2);
assert!(((found.ty - camera.ty) * h as f64).abs() < 0.2);
}
@@ -701,7 +711,10 @@ mod tests {
tracker.push(frame);
}
let each = started.elapsed().as_secs_f64() * 1000.0 / 31.0;
- println!("{w}x{h}: {each:.2} ms a frame, {} tracks", tracker.points.len());
+ println!(
+ "{w}x{h}: {each:.2} ms a frame, {} tracks",
+ tracker.points.len()
+ );
}
#[test]