voelin-media: stop copying an image backdrop every frame
Replacing a camera's background with an image cloned the whole scaled image (3.7 MB at 720p) for every frame, on the camera's thread, only to mix it and drop it: the mix only needs to read it. The image path had no test; one now checks the image shows behind the subject and the subject stays as it was. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c7c65b1596
commit
c41624e82d
1 file changed
+21
-2
@@ -404,8 +404,7 @@ impl BackgroundFilter {
|
|||||||
let FrameData::Rgba(behind) = &image.data else {
|
let FrameData::Rgba(behind) = &image.data else {
|
||||||
return Err(Error::InvalidFrame("the background image is not RGBA".into()));
|
return Err(Error::InvalidFrame("the background image is not RGBA".into()));
|
||||||
};
|
};
|
||||||
let behind = behind.data.clone();
|
mix(&mut self.workers, &mut plane.data, &behind.data, stride, width, height, &mask);
|
||||||
mix(&mut self.workers, &mut plane.data, &behind, stride, width, height, &mask);
|
|
||||||
}
|
}
|
||||||
Some(Backdrop::Colour(rgba)) => {
|
Some(Backdrop::Colour(rgba)) => {
|
||||||
let rgba = *rgba;
|
let rgba = *rgba;
|
||||||
@@ -693,6 +692,26 @@ mod tests {
|
|||||||
assert!(filter.apply(&mut VideoFrame::black_i420(8, 8)).is_err());
|
assert!(filter.apply(&mut VideoFrame::black_i420(8, 8)).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_image_background_shows_behind_the_subject() {
|
||||||
|
let dir = std::env::temp_dir().join(format!("voelin-backdrop-{}", std::process::id()));
|
||||||
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
|
let path = dir.join("behind.png");
|
||||||
|
image::RgbaImage::from_pixel(16, 16, image::Rgba([0, 0, 220, 255])).save(&path).unwrap();
|
||||||
|
let mut filter =
|
||||||
|
BackgroundFilter::with_default_segmenter(Background::Image { path: path.clone() });
|
||||||
|
let started = Instant::now();
|
||||||
|
let mut frame = flat_rgba(64, 64, [0, 255, 0, 255]);
|
||||||
|
while pixel(&frame, 0, 0) != [0, 0, 220, 255] {
|
||||||
|
assert!(started.elapsed() < Duration::from_secs(5), "the image never showed");
|
||||||
|
frame = flat_rgba(64, 64, [0, 255, 0, 255]);
|
||||||
|
filter.apply(&mut frame).unwrap();
|
||||||
|
std::thread::sleep(Duration::from_millis(10));
|
||||||
|
}
|
||||||
|
assert_eq!(pixel(&frame, 32, 35), [0, 255, 0, 255], "the subject is untouched");
|
||||||
|
std::fs::remove_dir_all(&dir).ok();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_blurred_background_keeps_the_subject_sharp() {
|
fn a_blurred_background_keeps_the_subject_sharp() {
|
||||||
let mut filter =
|
let mut filter =
|
||||||
|
|||||||
Reference in new issue
Block a user