diff --git a/crates/video/src/lib.rs b/crates/video/src/lib.rs index 8202a93f8..17ca960bb 100644 --- a/crates/video/src/lib.rs +++ b/crates/video/src/lib.rs @@ -59,3 +59,6 @@ pub mod vp9; #[cfg(any(feature = "av1-decode", feature = "av1-encode"))] pub mod av1; + +/// Uncompressed AVI and QuickTime pixel layouts. +pub mod raw_video; diff --git a/crates/video/src/raw_video.rs b/crates/video/src/raw_video.rs new file mode 100644 index 000000000..8f6291e09 --- /dev/null +++ b/crates/video/src/raw_video.rs @@ -0,0 +1,228 @@ +//! Packed and planar uncompressed AVI and QuickTime sample decoding. + +/// A malformed uncompressed frame or an unsupported image size. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + UnsupportedDimensions, + Truncated, +} +/// Decoded top-down RGBA pixels or planar YUV samples. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum RawPixels { + Rgba(Vec), + Yuv420([Vec; 3]), + Yuv422([Vec; 3]), +} + +/// Uncompressed video layouts (AVI `BI_RGB` and YUV FourCCs). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RawFormat { + /// 24-bit B, G, R; rows padded to 4 bytes. + Bgr24, + /// 32-bit B, G, R, X. + Bgrx32, + /// Packed 4:2:2: Y0 U Y1 V (`YUY2`, `YUYV`). + Yuy2, + /// Packed 4:2:2: U Y0 V Y1 (`UYVY`, `2vuy`). + Uyvy, + /// Planar 4:2:0: Y, U, V (`I420`, `IYUV`). + I420, + /// Planar 4:2:0: Y, V, U (`YV12`). + Yv12, + /// Y plane, interleaved U V at half resolution (`NV12`). + Nv12, + /// Luma only (`Y800`, `GREY`, `Y8 `). + Gray8, + /// 24-bit R, G, B; rows unpadded (QuickTime `raw ` at depth 24). + Rgb24, + /// 10-bit 4:2:2 Y'CbCr, six pixels in four little-endian words, rows padded to 128 bytes + /// (QuickTime `v210`); shown at 8 bits. + V210, +} + +impl RawFormat { + /// The layout of an AVI video stream: `BI_RGB` with its bit count, or a YUV FourCC. + pub fn from_avi(compression: [u8; 4], bit_count: u16) -> Option { + if matches!(u32::from_le_bytes(compression), 0 | 3) { + return match bit_count { + 24 => Some(RawFormat::Bgr24), + 32 => Some(RawFormat::Bgrx32), + _ => None, + }; + } + let mut upper = compression; + upper.make_ascii_uppercase(); + Some(match &upper { + b"YUY2" | b"YUYV" | b"YUNV" | b"V422" => RawFormat::Yuy2, + b"UYVY" | b"2VUY" | b"UYNV" | b"HDYC" => RawFormat::Uyvy, + b"I420" | b"IYUV" => RawFormat::I420, + b"YV12" => RawFormat::Yv12, + b"NV12" => RawFormat::Nv12, + b"Y800" | b"GREY" | b"GRAY" | b"Y8 " => RawFormat::Gray8, + _ => return None, + }) + } + + /// The layout of a QuickTime / MP4 video sample entry (`raw ` RGB at depth 24, `2vuy`, `v210`). + pub fn from_mov(fourcc: [u8; 4], depth: u16) -> Option { + match (&fourcc, depth) { + (b"raw ", 24) => Some(RawFormat::Rgb24), + (b"2vuy", _) => Some(RawFormat::Uyvy), + (b"v210", _) => Some(RawFormat::V210), + _ => None, + } + } + + pub fn label(self) -> &'static str { + match self { + RawFormat::Bgr24 | RawFormat::Rgb24 => "Uncompressed RGB 24-bit", + RawFormat::Bgrx32 => "Uncompressed RGB 32-bit", + RawFormat::Yuy2 | RawFormat::Uyvy => "Uncompressed YUV 4:2:2", + RawFormat::I420 | RawFormat::Yv12 | RawFormat::Nv12 => "Uncompressed YUV 4:2:0", + RawFormat::Gray8 => "Uncompressed greyscale", + RawFormat::V210 => "Uncompressed YUV 4:2:2 10-bit", + } + } +} + +pub fn decode(format: RawFormat, width: u32, height: u32, bottom_up: bool, s: &[u8]) -> Result { + if width == 0 || height == 0 || width > 16_384 || height > 16_384 { + return Err(Error::UnsupportedDimensions); + } + let (w, h) = (width as usize, height as usize); + let short = || Error::Truncated; + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + Ok(match format { + RawFormat::Bgr24 | RawFormat::Bgrx32 => { + let bpp = if format == RawFormat::Bgr24 { 3 } else { 4 }; + let stride = (w * bpp).div_ceil(4) * 4; + if s.len() < stride * h { + return Err(short()); + } + let mut rgba = Vec::with_capacity(w * h * 4); + for row in 0..h { + let src = if bottom_up { h - 1 - row } else { row }; + for px in s.get(src * stride..src * stride + w * bpp).unwrap_or_default().chunks_exact(bpp) { + rgba.extend_from_slice(&[px[2], px[1], px[0], 255]); + } + } + RawPixels::Rgba(rgba) + } + RawFormat::Rgb24 => { + let stride = w * 3; + if s.len() < stride * h { + return Err(short()); + } + let mut rgba = Vec::with_capacity(w * h * 4); + for px in s.get(..stride * h).unwrap_or_default().as_chunks::<3>().0 { + rgba.extend_from_slice(&[px[0], px[1], px[2], 255]); + } + RawPixels::Rgba(rgba) + } + RawFormat::V210 => { + // 6 pixels per 16 bytes; each row padded to a multiple of 128 bytes (48 pixels) + let stride = w.div_ceil(48) * 128; + if s.len() < stride * h { + return Err(short()); + } + let (mut y, mut u, mut v) = (Vec::with_capacity(w * h), Vec::with_capacity(cw * h), Vec::with_capacity(cw * h)); + for row in s.chunks_exact(stride).take(h) { + // the row's 10-bit samples in order: Cb Y Cr Y Cb Y Cr Y …, kept at 8 bits + let mut comps = row.as_chunks::<4>().0.iter().flat_map(|b| { + let word = u32::from_le_bytes(*b); + [word & 0x3ff, (word >> 10) & 0x3ff, (word >> 20) & 0x3ff].map(|c| (c >> 2) as u8) + }); + for x in 0..cw { + let (Some(cb), Some(y0), Some(cr), Some(y1)) = (comps.next(), comps.next(), comps.next(), comps.next()) else { break }; + u.push(cb); + v.push(cr); + y.push(y0); + if 2 * x + 1 < w { + y.push(y1); + } + } + } + RawPixels::Yuv422([y, u, v]) + } + RawFormat::Yuy2 | RawFormat::Uyvy => { + let stride = cw * 4; + if s.len() < stride * h { + return Err(short()); + } + let (yo, uo, vo, y2) = if format == RawFormat::Yuy2 { (0, 1, 3, 2) } else { (1, 0, 2, 3) }; + let (mut y, mut u, mut v) = (Vec::with_capacity(w * h), Vec::with_capacity(cw * h), Vec::with_capacity(cw * h)); + for row in s.chunks_exact(stride).take(h) { + for (k, q) in row.as_chunks::<4>().0.iter().enumerate() { + y.push(q[yo]); + if 2 * k + 1 < w { + y.push(q[y2]); + } + u.push(q[uo]); + v.push(q[vo]); + } + } + RawPixels::Yuv422([y, u, v]) + } + RawFormat::I420 | RawFormat::Yv12 | RawFormat::Nv12 | RawFormat::Gray8 => { + let luma = w * h; + let need = if format == RawFormat::Gray8 { luma } else { luma + 2 * cw * ch }; + if s.len() < need { + return Err(short()); + } + let y = s.get(..luma).unwrap_or_default().to_vec(); + let (u, v) = match format { + RawFormat::Gray8 => (vec![128; cw * ch], vec![128; cw * ch]), + RawFormat::Nv12 => s.get(luma..luma + 2 * cw * ch).unwrap_or_default().as_chunks::<2>().0.iter().map(|p| (p[0], p[1])).unzip(), + _ => { + let a = s.get(luma..luma + cw * ch).unwrap_or_default().to_vec(); + let b = s.get(luma + cw * ch..luma + 2 * cw * ch).unwrap_or_default().to_vec(); + if format == RawFormat::Yv12 { (b, a) } else { (a, b) } + } + }; + RawPixels::Yuv420([y, u, v]) + } + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn avi_and_mov_layouts_and_bottom_up_rows() -> Result<(), Error> { + assert_eq!(RawFormat::from_avi(*b"yuyv", 16), Some(RawFormat::Yuy2)); + assert_eq!(RawFormat::from_avi([0; 4], 24), Some(RawFormat::Bgr24)); + assert_eq!(RawFormat::from_mov(*b"v210", 0), Some(RawFormat::V210)); + let pixels = decode(RawFormat::Bgr24, 1, 2, true, &[1, 2, 3, 0, 4, 5, 6, 0])?; + assert_eq!(pixels, RawPixels::Rgba(vec![6, 5, 4, 255, 3, 2, 1, 255])); + Ok(()) + } + + #[test] + fn odd_planar_and_packed_frames_preserve_chroma() -> Result<(), Error> { + let yuy = decode(RawFormat::Yuy2, 3, 1, false, &[10, 20, 11, 30, 12, 21, 99, 31])?; + assert_eq!(yuy, RawPixels::Yuv422([vec![10, 11, 12], vec![20, 21], vec![30, 31]])); + let nv = decode(RawFormat::Nv12, 3, 1, false, &[1, 2, 3, 10, 20, 11, 21])?; + assert_eq!(nv, RawPixels::Yuv420([vec![1, 2, 3], vec![10, 11], vec![20, 21]])); + Ok(()) + } + + #[test] + fn truncated_frames_and_hostile_dimensions_are_rejected() { + for format in [ + RawFormat::Bgr24, + RawFormat::Bgrx32, + RawFormat::Rgb24, + RawFormat::V210, + RawFormat::Yuy2, + RawFormat::Uyvy, + RawFormat::I420, + RawFormat::Yv12, + RawFormat::Nv12, + RawFormat::Gray8, + ] { + assert_eq!(decode(format, 3, 2, false, &[]), Err(Error::Truncated)); + } + assert_eq!(decode(RawFormat::Gray8, u32::MAX, 1, false, &[]), Err(Error::UnsupportedDimensions)); + } +}