| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use libwebrtc::video_frame::{I420Buffer, VideoBuffer as _}; |
| 4 | |
| 5 | use crate::blur::{MASK_HEIGHT, MASK_WIDTH, axis, smoothstep}; |
| 6 | use crate::blur::BlurSettings; |
| 7 | |
| 8 | const CUTOUT_WIDTH: u32 = 480; |
| 9 | const WIDTH_SHARE: f32 = 0.22; |
| 10 | const MARGIN_SHARE: f32 = 0.03; |
| 11 | const CORNER_SHARE: f32 = 0.1; |
| 12 | const MIN_TARGET_WIDTH: usize = 64; |
| 13 | |
| 14 | pub struct Cutout { |
| 15 | width: usize, |
| 16 | height: usize, |
| 17 | luma: Vec<u8>, |
| 18 | chroma_blue: Vec<u8>, |
| 19 | chroma_red: Vec<u8>, |
| 20 | alpha: Vec<u8>, |
| 21 | } |
| 22 | |
| 23 | fn corner_coverage(column: usize, row: usize, width: usize, height: usize, radius: f32) -> f32 { |
| 24 | let x = column as f32 + 0.5; |
| 25 | let y = row as f32 + 0.5; |
| 26 | let inner_x = x.clamp(radius, width as f32 - radius); |
| 27 | let inner_y = y.clamp(radius, height as f32 - radius); |
| 28 | let distance = ((x - inner_x).powi(2) + (y - inner_y).powi(2)).sqrt(); |
| 29 | (radius - distance + 0.5).clamp(0., 1.) |
| 30 | } |
| 31 | |
| 32 | impl Cutout { |
| 33 | pub fn from_camera(camera: &mut I420Buffer, mask: &[f32]) -> Option<Cutout> { |
| 34 | if mask.len() != MASK_WIDTH * MASK_HEIGHT || camera.width() < 2 || camera.height() < 2 { |
| 35 | return None; |
| 36 | } |
| 37 | let width = CUTOUT_WIDTH.min(camera.width()) & !1; |
| 38 | let height = (u64::from(camera.height()) * u64::from(width) / u64::from(camera.width())) as u32 & !1; |
| 39 | let scaled = camera.scale(width as i32, height as i32); |
| 40 | let (width, height) = (width as usize, height as usize); |
| 41 | let (stride_y, stride_u, stride_v) = scaled.strides(); |
| 42 | let (plane_y, plane_u, plane_v) = scaled.data(); |
| 43 | let mask_x = axis(width, MASK_WIDTH); |
| 44 | let mask_y = axis(height, MASK_HEIGHT); |
| 45 | let radius = width as f32 * CORNER_SHARE; |
| 46 | let mut cutout = Cutout { |
| 47 | width, |
| 48 | height, |
| 49 | luma: Vec::with_capacity(width * height), |
| 50 | chroma_blue: Vec::with_capacity(width * height), |
| 51 | chroma_red: Vec::with_capacity(width * height), |
| 52 | alpha: Vec::with_capacity(width * height), |
| 53 | }; |
| 54 | for row in 0..height { |
| 55 | let (top, bottom, vertical) = (mask_y.first[row] * MASK_WIDTH, mask_y.second[row] * MASK_WIDTH, mask_y.weight[row]); |
| 56 | for column in 0..width { |
| 57 | let (left, right, across) = (mask_x.first[column], mask_x.second[column], mask_x.weight[column]); |
| 58 | let upper = mask[top + left] * (1. - across) + mask[top + right] * across; |
| 59 | let lower = mask[bottom + left] * (1. - across) + mask[bottom + right] * across; |
| 60 | let person = smoothstep(upper * (1. - vertical) + lower * vertical); |
| 61 | let coverage = person * corner_coverage(column, row, width, height, radius); |
| 62 | cutout.luma.push(plane_y[row * stride_y as usize + column]); |
| 63 | cutout.chroma_blue.push(plane_u[(row / 2) * stride_u as usize + column / 2]); |
| 64 | cutout.chroma_red.push(plane_v[(row / 2) * stride_v as usize + column / 2]); |
| 65 | cutout.alpha.push((coverage * 255. + 0.5) as u8); |
| 66 | } |
| 67 | } |
| 68 | Some(cutout) |
| 69 | } |
| 70 | |
| 71 | fn scaled_to(&self, width: usize, height: usize) -> Cutout { |
| 72 | let across = axis(width, self.width); |
| 73 | let down = axis(height, self.height); |
| 74 | let resample = |plane: &[u8]| -> Vec<u8> { |
| 75 | let mut output = Vec::with_capacity(width * height); |
| 76 | for row in 0..height { |
| 77 | let (top, bottom, vertical) = (down.first[row] * self.width, down.second[row] * self.width, down.weight[row]); |
| 78 | for column in 0..width { |
| 79 | let (left, right, horizontal) = (across.first[column], across.second[column], across.weight[column]); |
| 80 | let sample = |index: usize| f32::from(plane[index]); |
| 81 | let upper = sample(top + left) * (1. - horizontal) + sample(top + right) * horizontal; |
| 82 | let lower = sample(bottom + left) * (1. - horizontal) + sample(bottom + right) * horizontal; |
| 83 | output.push((upper * (1. - vertical) + lower * vertical + 0.5) as u8); |
| 84 | } |
| 85 | } |
| 86 | output |
| 87 | }; |
| 88 | Cutout { |
| 89 | width, |
| 90 | height, |
| 91 | luma: resample(&self.luma), |
| 92 | chroma_blue: resample(&self.chroma_blue), |
| 93 | chroma_red: resample(&self.chroma_red), |
| 94 | alpha: resample(&self.alpha), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn blend_onto(&self, target: &mut I420Buffer, left: usize, top: usize) { |
| 99 | let (stride_y, stride_u, stride_v) = target.strides(); |
| 100 | let (plane_y, plane_u, plane_v) = target.data_mut(); |
| 101 | let mix = |below: u8, above: u8, alpha: u8| { |
| 102 | let weight = u32::from(alpha); |
| 103 | ((u32::from(below) * (255 - weight) + u32::from(above) * weight + 127) / 255) as u8 |
| 104 | }; |
| 105 | for row in 0..self.height { |
| 106 | for column in 0..self.width { |
| 107 | let alpha = self.alpha[row * self.width + column]; |
| 108 | if alpha == 0 { |
| 109 | continue; |
| 110 | } |
| 111 | let (x, y) = (left + column, top + row); |
| 112 | let source = row * self.width + column; |
| 113 | let luma = &mut plane_y[y * stride_y as usize + x]; |
| 114 | *luma = mix(*luma, self.luma[source], alpha); |
| 115 | if x % 2 == 0 && y % 2 == 0 { |
| 116 | let chroma_index = (y / 2) * stride_u as usize + x / 2; |
| 117 | plane_u[chroma_index] = mix(plane_u[chroma_index], self.chroma_blue[source], alpha); |
| 118 | let chroma_index = (y / 2) * stride_v as usize + x / 2; |
| 119 | plane_v[chroma_index] = mix(plane_v[chroma_index], self.chroma_red[source], alpha); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | pub struct StandoutOverlay { |
| 127 | settings: Arc<BlurSettings>, |
| 128 | scaled: Option<ScaledCutout>, |
| 129 | } |
| 130 | |
| 131 | struct ScaledCutout { |
| 132 | source: Arc<Cutout>, |
| 133 | frame: (usize, usize), |
| 134 | cutout: Cutout, |
| 135 | } |
| 136 | |
| 137 | impl StandoutOverlay { |
| 138 | pub fn new(settings: Arc<BlurSettings>) -> Self { |
| 139 | StandoutOverlay { settings, scaled: None } |
| 140 | } |
| 141 | |
| 142 | pub fn apply(&mut self, mut frame: I420Buffer) -> I420Buffer { |
| 143 | if !self.settings.standout() { |
| 144 | return frame; |
| 145 | } |
| 146 | let Some(source) = self.settings.cutout() else { return frame }; |
| 147 | let (frame_width, frame_height) = (frame.width() as usize, frame.height() as usize); |
| 148 | let width = (frame_width as f32 * WIDTH_SHARE) as usize & !1; |
| 149 | if width < MIN_TARGET_WIDTH { |
| 150 | return frame; |
| 151 | } |
| 152 | let height = (width * source.height / source.width) & !1; |
| 153 | let margin = (frame_width as f32 * MARGIN_SHARE) as usize; |
| 154 | if height + margin >= frame_height || width + margin >= frame_width { |
| 155 | return frame; |
| 156 | } |
| 157 | let fresh = self.scaled.as_ref().is_some_and(|scaled| Arc::ptr_eq(&scaled.source, &source) && scaled.frame == (frame_width, frame_height)); |
| 158 | if !fresh { |
| 159 | self.scaled = Some(ScaledCutout { cutout: source.scaled_to(width, height), source, frame: (frame_width, frame_height) }); |
| 160 | } |
| 161 | if let Some(scaled) = &self.scaled { |
| 162 | let left = (frame_width - width - margin) & !1; |
| 163 | let top = (frame_height - height - margin) & !1; |
| 164 | scaled.cutout.blend_onto(&mut frame, left, top); |
| 165 | } |
| 166 | frame |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #[cfg(test)] |
| 171 | mod tests { |
| 172 | use super::*; |
| 173 | |
| 174 | fn gray_frame(width: u32, height: u32, luma: u8) -> I420Buffer { |
| 175 | let mut buffer = I420Buffer::new(width, height); |
| 176 | let (stride_y, stride_u, stride_v) = buffer.strides(); |
| 177 | let (plane_y, plane_u, plane_v) = buffer.data_mut(); |
| 178 | for row in 0..height as usize { |
| 179 | plane_y[row * stride_y as usize..][..width as usize].fill(luma); |
| 180 | } |
| 181 | for row in 0..height as usize / 2 { |
| 182 | plane_u[row * stride_u as usize..][..width as usize / 2].fill(128); |
| 183 | plane_v[row * stride_v as usize..][..width as usize / 2].fill(128); |
| 184 | } |
| 185 | buffer |
| 186 | } |
| 187 | |
| 188 | fn full_mask(value: f32) -> Vec<f32> { |
| 189 | vec![value; MASK_WIDTH * MASK_HEIGHT] |
| 190 | } |
| 191 | |
| 192 | fn luma_at(buffer: &I420Buffer, x: usize, y: usize) -> u8 { |
| 193 | buffer.data().0[y * buffer.strides().0 as usize + x] |
| 194 | } |
| 195 | |
| 196 | fn overlay_with(person: f32) -> (StandoutOverlay, Arc<BlurSettings>) { |
| 197 | let settings = Arc::new(BlurSettings::default()); |
| 198 | settings.set_standout(true); |
| 199 | let mut camera = gray_frame(640, 360, 235); |
| 200 | settings.set_cutout(Cutout::from_camera(&mut camera, &full_mask(person)).map(Arc::new)); |
| 201 | (StandoutOverlay::new(settings.clone()), settings) |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn the_person_lands_bottom_right_and_the_rest_of_the_screen_stays() { |
| 206 | let (mut overlay, _) = overlay_with(1.); |
| 207 | let frame = overlay.apply(gray_frame(1280, 720, 16)); |
| 208 | assert_eq!(luma_at(&frame, 100, 100), 16); |
| 209 | assert!(luma_at(&frame, 1280 - 100, 720 - 100) > 200); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn corners_are_rounded() { |
| 214 | let (mut overlay, _) = overlay_with(1.); |
| 215 | let frame = overlay.apply(gray_frame(1280, 720, 16)); |
| 216 | let width = (1280. * WIDTH_SHARE) as usize & !1; |
| 217 | let left = (1280 - width - (1280. * MARGIN_SHARE) as usize) & !1; |
| 218 | let top = (720 - ((width * 9 / 16) & !1) - (1280. * MARGIN_SHARE) as usize) & !1; |
| 219 | assert_eq!(luma_at(&frame, left, top), 16); |
| 220 | assert!(luma_at(&frame, left + width / 2, top + 20) > 200); |
| 221 | } |
| 222 | |
| 223 | #[test] |
| 224 | fn no_person_in_the_mask_changes_nothing() { |
| 225 | let (mut overlay, _) = overlay_with(0.); |
| 226 | let frame = overlay.apply(gray_frame(1280, 720, 16)); |
| 227 | assert_eq!(luma_at(&frame, 1280 - 100, 720 - 100), 16); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn switched_off_or_without_a_cutout_the_frame_passes_through() { |
| 232 | let (mut overlay, settings) = overlay_with(1.); |
| 233 | settings.set_standout(false); |
| 234 | assert_eq!(luma_at(&overlay.apply(gray_frame(1280, 720, 16)), 1280 - 100, 720 - 100), 16); |
| 235 | settings.set_standout(true); |
| 236 | settings.set_cutout(None); |
| 237 | assert_eq!(luma_at(&overlay.apply(gray_frame(1280, 720, 16)), 1280 - 100, 720 - 100), 16); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn a_missing_mask_gives_no_cutout() { |
| 242 | let mut camera = gray_frame(640, 360, 100); |
| 243 | assert!(Cutout::from_camera(&mut camera, &[]).is_none()); |
| 244 | } |
| 245 | } |