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