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