BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / examples / blur_bench.rs

blur_bench.rs

blur_bench.rs 4.9 KiB Download Raw
1use std::sync::Arc;
2use std::time::Instant;
3
4use calling::blur::{BlurSettings, MaskSource, Segmenter, compose};
5use calling::effects::{CameraEffects, EffectsStage};
6use calling::framing::{Crop, crop_and_scale};
7use calling::luma_effects::soften_luma;
8use libwebrtc::video_frame::I420Buffer;
9
10const WIDTH: usize = 640;
11const HEIGHT: usize = 360;
12const SEND_WIDTH: u32 = 1280;
13const SEND_HEIGHT: u32 = 720;
14const FRAMES: usize = 200;
15const WARMUP: usize = 10;
16
17fn synthetic() -> Vec<u8> {
18 (0..WIDTH * HEIGHT * 4).map(|index| if index % 4 == 3 { 255 } else { ((index / 4 % WIDTH + index / 4 / WIDTH) * 255 / (WIDTH + HEIGHT)) as u8 }).collect()
19}
20
21fn send_frame() -> I420Buffer {
22 let mut buffer = I420Buffer::new(SEND_WIDTH, SEND_HEIGHT);
23 let stride = buffer.strides().0 as usize;
24 let (plane_y, plane_u, plane_v) = buffer.data_mut();
25 for (index, value) in plane_y.iter_mut().enumerate() {
26 *value = (((index % stride) + index / stride) * 255 / (SEND_WIDTH as usize + SEND_HEIGHT as usize)) as u8;
27 }
28 plane_u.fill(128);
29 plane_v.fill(128);
30 buffer
31}
32
33fn milliseconds_per_frame(started: Instant) -> f64 {
34 started.elapsed().as_secs_f64() * 1000. / FRAMES as f64
35}
36
37fn stage_cost(label: &str, blur: bool, effects: CameraEffects) {
38 let settings = Arc::new(BlurSettings::default());
39 settings.set_enabled(blur);
40 settings.set_effects(effects);
41 let mut stage = EffectsStage::new(settings);
42 let source = send_frame();
43 let run = |stage: &mut EffectsStage| {
44 let mut frame = I420Buffer::new(SEND_WIDTH, SEND_HEIGHT);
45 let (source_y, source_u, source_v) = source.data();
46 let (target_y, target_u, target_v) = frame.data_mut();
47 target_y.copy_from_slice(source_y);
48 target_u.copy_from_slice(source_u);
49 target_v.copy_from_slice(source_v);
50 stage.apply_i420(frame)
51 };
52 for _ in 0..WARMUP {
53 run(&mut stage);
54 }
55 let started = Instant::now();
56 for _ in 0..FRAMES {
57 run(&mut stage);
58 }
59 println!("{label} {SEND_WIDTH}x{SEND_HEIGHT}: {:.2} ms/frame", milliseconds_per_frame(started));
60}
61
62fn main() {
63 let mut arguments = std::env::args().skip(1);
64 let input = arguments.next();
65 let output = arguments.next();
66 let frame = input.map_or_else(synthetic, |path| std::fs::read(path).expect("raw rgba 640x360 input"));
67 assert_eq!(frame.len(), WIDTH * HEIGHT * 4, "input must be raw RGBA {WIDTH}x{HEIGHT}");
68 let mut masks = MaskSource::new().expect("model");
69 let segmenter = Segmenter::new().expect("model");
70 for _ in 0..WARMUP {
71 masks.refresh(|| (frame.clone(), WIDTH, HEIGHT));
72 }
73 let started = Instant::now();
74 for _ in 0..FRAMES {
75 segmenter.mask(&frame, WIDTH, HEIGHT).expect("mask");
76 }
77 println!("segmentation only: {:.2} ms/frame", milliseconds_per_frame(started));
78 let started = Instant::now();
79 let mut result = frame.clone();
80 for _ in 0..FRAMES {
81 result.copy_from_slice(&frame);
82 masks.refresh(|| (frame.clone(), WIDTH, HEIGHT));
83 compose(&mut result, WIDTH, HEIGHT, masks.mask());
84 }
85 println!("segment + blur + compose {WIDTH}x{HEIGHT}: {:.2} ms/frame", milliseconds_per_frame(started));
86 if let Some(path) = output {
87 std::fs::write(path, &result).expect("output");
88 }
89 let off = CameraEffects::default();
90 let all = CameraEffects { auto_framing: true, adjust_brightness: true, soft_focus: true, ..off };
91 stage_cost("stage, brightness", false, CameraEffects { adjust_brightness: true, ..off });
92 stage_cost("stage, soft focus", false, CameraEffects { soft_focus: true, ..off });
93 stage_cost("stage, auto framing", false, CameraEffects { auto_framing: true, ..off });
94 stage_cost("stage, all three", false, all);
95 stage_cost("stage, all three + blur", true, all);
96 stage_cost("stage, blur only", true, off);
97 let mut frame = send_frame();
98 let stride = frame.strides().0 as usize;
99 let oval: Vec<f32> = (0..calling::blur::MASK_WIDTH * calling::blur::MASK_HEIGHT)
100 .map(|index| {
101 let (x, y) = ((index % calling::blur::MASK_WIDTH) as f32 / calling::blur::MASK_WIDTH as f32 - 0.5, (index / calling::blur::MASK_WIDTH) as f32 / calling::blur::MASK_HEIGHT as f32 - 0.5);
102 if (x * x) / 0.04 + (y * y) / 0.09 <= 1. { 1. } else { 0. }
103 })
104 .collect();
105 let started = Instant::now();
106 for _ in 0..FRAMES {
107 soften_luma(frame.data_mut().0, stride, SEND_WIDTH as usize, SEND_HEIGHT as usize, &oval, 100);
108 }
109 println!("soft focus only, oval person {SEND_WIDTH}x{SEND_HEIGHT}: {:.2} ms/frame", milliseconds_per_frame(started));
110 let source = send_frame();
111 let crop = Crop { center_x: 0.5, center_y: 0.5, zoom: 1.6 };
112 let started = Instant::now();
113 for _ in 0..FRAMES {
114 std::hint::black_box(crop_and_scale(&source, &crop));
115 }
116 println!("crop and scale {SEND_WIDTH}x{SEND_HEIGHT}: {:.2} ms/frame", milliseconds_per_frame(started));
117}