feat: add regenerate trace thumbnail endpoint with bbox clamping

- validator::clamp_crop: clamp crop rect to frame dims (fixes OOB shift)
- tkg.rs: use clamp_crop in key_face crop paths (root-cause fix)
- trace_agent_api: POST /api/v1/file/:file_uuid/trace/:trace_id/regenerate-thumbnail
- docs: 26_trace_thumbnail_regenerate.md for Studio team
This commit is contained in:
Accusys
2026-08-12 03:04:19 +08:00
parent aa28bf93fb
commit 59ca6cdf83
5 changed files with 553 additions and 25 deletions
+64
View File
@@ -91,6 +91,31 @@ pub fn validate_crop(
Ok(())
}
/// Clamp a crop rectangle so it fully fits inside the frame dimensions.
///
/// `x`/`y` are clamped into `[0, frame_size)`, and `w`/`h` are reduced so the
/// rectangle never extends past the frame boundary. The result always has
/// positive dimensions. Out-of-bounds bboxes that previously made ffmpeg
/// silently shift the crop (root cause of the "key frame OK, key face broken"
/// bug) now produce a correctly-aligned crop.
pub fn clamp_crop(
x: i32,
y: i32,
w: i32,
h: i32,
video_width: i32,
video_height: i32,
) -> (i32, i32, i32, i32) {
if video_width <= 0 || video_height <= 0 {
return (x.max(0), y.max(0), w.max(1), h.max(1));
}
let cx = x.clamp(0, video_width - 1);
let cy = y.clamp(0, video_height - 1);
let cw = w.clamp(1, (video_width - cx).max(1));
let ch = h.clamp(1, (video_height - cy).max(1));
(cx, cy, cw, ch)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -167,6 +192,45 @@ mod tests {
assert!(validate_crop(100, -1, 200, 200, 1920, 1080).is_err());
}
#[test]
fn test_clamp_crop_in_bounds_unchanged() {
assert_eq!(
clamp_crop(100, 100, 200, 200, 1920, 1080),
(100, 100, 200, 200)
);
assert_eq!(clamp_crop(0, 0, 1920, 1080, 1920, 1080), (0, 0, 1920, 1080));
}
#[test]
fn test_clamp_crop_exceeds_right_bottom() {
// Regression for trace 355b: bbox 11px past the frame edge used to
// make ffmpeg silently shift the crop content.
assert_eq!(clamp_crop(65, 301, 70, 70, 1280, 360), (65, 301, 70, 59));
assert_eq!(
clamp_crop(1800, 900, 200, 200, 1920, 1080),
(1800, 900, 120, 180)
);
}
#[test]
fn test_clamp_crop_negative_origin() {
assert_eq!(clamp_crop(-10, -20, 200, 100, 1920, 1080), (0, 0, 200, 100));
}
#[test]
fn test_clamp_crop_fully_outside() {
assert_eq!(
clamp_crop(3000, 3000, 50, 50, 1920, 1080),
(1919, 1079, 1, 1)
);
}
#[test]
fn test_clamp_crop_zero_frame_dimensions() {
// Unknown frame size: fall back to legacy max(0)/max(1) behavior.
assert_eq!(clamp_crop(-10, -20, 0, 0, 0, 0), (0, 0, 1, 1));
}
#[test]
fn test_is_valid_jpeg() {
let valid_jpeg = vec![