//! Frames as BGRA, the byte order GPUI's `RenderImage` holds: the BGRA twin //! of `voelin_media::convert::to_rgba` (the same `yuv` calls with the //! channels swapped, BT.601 limited range), and the picture GPUI shows. use std::sync::Arc; use gpui_kit::RenderImage; use voelin_media::frame::{FrameRef, PixelsRef, VideoFrame}; use yuv::{YuvBiPlanarImage, YuvConversionMode, YuvPlanarImage, YuvRange, YuvStandardMatrix}; const RANGE: YuvRange = YuvRange::Limited; const MATRIX: YuvStandardMatrix = YuvStandardMatrix::Bt601; fn u32_of(n: usize) -> Result { u32::try_from(n).map_err(|_| format!("{n} does not fit a stride")) } /// Write `frame` as BGRA (alpha 255) into `out`, `stride` bytes per row. pub fn to_bgra(frame: &VideoFrame, out: &mut [u8], stride: usize) -> Result<(), String> { to_bgra_ref(&frame.view(), out, stride) } /// [`to_bgra`] of a borrowed frame; allocates nothing. pub fn to_bgra_ref(frame: &FrameRef<'_>, out: &mut [u8], stride: usize) -> Result<(), String> { frame.validate().map_err(|e| e.to_string())?; let (w, h) = (frame.width, frame.height); let row = w as usize * 4; if stride < row || out.len() < stride * (h as usize - 1) + row { return Err(format!("BGRA output of {} bytes is too small for {w}x{h}", out.len())); } let out_stride = u32_of(stride)?; match &frame.pixels { PixelsRef::I420 { y, u, v } => { let image = YuvPlanarImage { y_plane: y.data, y_stride: u32_of(y.stride)?, u_plane: u.data, u_stride: u32_of(u.stride)?, v_plane: v.data, v_stride: u32_of(v.stride)?, width: w, height: h, }; yuv::yuv420_to_bgra(&image, out, out_stride, RANGE, MATRIX).map_err(|e| e.to_string()) } PixelsRef::Nv12 { y, uv } => { let image = YuvBiPlanarImage { y_plane: y.data, y_stride: u32_of(y.stride)?, uv_plane: uv.data, uv_stride: u32_of(uv.stride)?, width: w, height: h, }; yuv::yuv_nv12_to_bgra( &image, out, out_stride, RANGE, MATRIX, YuvConversionMode::Balanced, ) .map_err(|e| e.to_string()) } PixelsRef::Bgra(p) => { for y in 0..h as usize { out[y * stride..y * stride + row].copy_from_slice(p.row(y, row)); } Ok(()) } PixelsRef::Rgba(p) => { for y in 0..h as usize { let (src, dst) = (p.row(y, row), &mut out[y * stride..y * stride + row]); // `row` is a whole number of pixels: there is no remainder. let (dst, src) = (dst.as_chunks_mut::<4>().0, src.as_chunks::<4>().0); for (d, s) in dst.iter_mut().zip(src) { *d = [s[2], s[1], s[0], 255]; } } Ok(()) } } } /// The pixels as GPUI's image: one frame, BGRA, `width * 4` bytes per row. pub fn render_image(width: u32, height: u32, bgra: Vec) -> Arc { let buffer = image::RgbaImage::from_raw(width, height, bgra).expect("a buffer of the size"); Arc::new(RenderImage::new([image::Frame::new(buffer)])) } /// `frame` converted into a new picture. pub fn picture(frame: &VideoFrame) -> Result, String> { let stride = frame.width as usize * 4; let mut bgra = vec![0u8; stride * frame.height as usize]; to_bgra(frame, &mut bgra, stride)?; Ok(render_image(frame.width, frame.height, bgra)) } #[cfg(test)] mod tests { use super::*; use voelin_media::capture::synthetic::{Pattern, SyntheticScreen}; use voelin_media::convert; /// BGRA is RGBA with red and blue swapped, pixel for pixel, for I420 and /// for packed input. #[test] fn bgra_is_rgba_swapped() { let picture = SyntheticScreen::with_pattern(64, 36, Pattern::Desktop).frame(3, 30); let i420 = convert::to_i420(&picture).unwrap().into_owned(); let rgba_in = VideoFrame::from_rgba(64, 36, 64 * 4, convert::to_rgba_vec(&picture).unwrap()).unwrap(); for frame in [&picture, &i420, &rgba_in] { let mut rgba = vec![0; 64 * 36 * 4]; let mut bgra = vec![0; 64 * 36 * 4]; convert::to_rgba(frame, &mut rgba, 64 * 4).unwrap(); to_bgra(frame, &mut bgra, 64 * 4).unwrap(); for (r, b) in rgba.as_chunks::<4>().0.iter().zip(bgra.as_chunks::<4>().0) { assert_eq!([r[2], r[1], r[0], r[3]], *b); } } } #[test] fn small_output_is_refused() { let picture = SyntheticScreen::new(32, 32).frame(0, 30); assert!(to_bgra(&picture, &mut [0; 16], 32 * 4).is_err()); } #[test] fn pictures_have_the_frame_size() { let picture = SyntheticScreen::new(40, 20).frame(0, 30); let image = super::picture(&picture).unwrap(); assert_eq!((image.size(0).width.0, image.size(0).height.0), (40, 20)); } }