diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs index 5bf6426b8..01fbf3b18 100644 --- a/crates/color/src/lib.rs +++ b/crates/color/src/lib.rs @@ -154,24 +154,41 @@ pub fn rgb_to_ycbcr(r: f32, g: f32, b: f32, m: Matrix) -> [f32; 3] { [y, (b - y) / (2.0 * (1.0 - kb)), (r - y) / (2.0 * (1.0 - kr))] } +/// Unsupported integer sample depth. Normalization supports 8 through 16 bits. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct InvalidBitDepth(pub u32); + +impl std::fmt::Display for InvalidBitDepth { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "unsupported sample bit depth {}; expected 8..=16", self.0) + } +} +impl std::error::Error for InvalidBitDepth {} + /// Normalise an integer luma code value to 0..1 given bit depth and range. #[inline] -pub fn normalize_y(v: u32, bits: u32, range: Range) -> f32 { +pub fn normalize_y(v: u32, bits: u32, range: Range) -> Result { + if !(8..=16).contains(&bits) { + return Err(InvalidBitDepth(bits)); + } let scale = (1u32 << (bits - 8)) as f32; - match range { + Ok(match range { Range::Limited => (v as f32 - 16.0 * scale) / (219.0 * scale), Range::Full => v as f32 / ((1u32 << bits) - 1) as f32, - } + }) } /// Normalise an integer chroma code value to −0.5..0.5 given bit depth and range. #[inline] -pub fn normalize_c(v: u32, bits: u32, range: Range) -> f32 { +pub fn normalize_c(v: u32, bits: u32, range: Range) -> Result { + if !(8..=16).contains(&bits) { + return Err(InvalidBitDepth(bits)); + } let scale = (1u32 << (bits - 8)) as f32; - match range { + Ok(match range { Range::Limited => (v as f32 - 128.0 * scale) / (224.0 * scale), Range::Full => (v as f32 - (1u32 << (bits - 1)) as f32) / ((1u32 << bits) - 1) as f32, - } + }) } /// sRGB electro-optical transfer (encoded → linear). @@ -355,12 +372,20 @@ mod tests { } } + #[test] + fn invalid_bit_depths_are_errors_not_shifts() { + for bits in [0, 1, 7, 17, 31, 32, u32::MAX] { + assert_eq!(normalize_y(0, bits, Range::Full), Err(InvalidBitDepth(bits))); + assert_eq!(normalize_c(0, bits, Range::Limited), Err(InvalidBitDepth(bits))); + } + } + #[test] fn limited_range_levels() { - assert_eq!(normalize_y(16, 8, Range::Limited), 0.0); - assert_eq!(normalize_y(235, 8, Range::Limited), 1.0); - assert_eq!(normalize_y(940, 10, Range::Limited), 1.0); - assert_eq!(normalize_c(128, 8, Range::Limited), 0.0); + assert_eq!(normalize_y(16, 8, Range::Limited).unwrap(), 0.0); + assert_eq!(normalize_y(235, 8, Range::Limited).unwrap(), 1.0); + assert_eq!(normalize_y(940, 10, Range::Limited).unwrap(), 1.0); + assert_eq!(normalize_c(128, 8, Range::Limited).unwrap(), 0.0); } #[test] diff --git a/crates/frame/src/lib.rs b/crates/frame/src/lib.rs index 09f95e0b8..ad294b10f 100644 --- a/crates/frame/src/lib.rs +++ b/crates/frame/src/lib.rs @@ -23,6 +23,53 @@ use craft_time::Tick; use rayon::prelude::*; use serde::{Deserialize, Serialize}; +/// Default per-buffer allocation ceiling (1 GiB), permitting an 8K RGBA-f32 frame. +/// Callers with a smaller budget should use the `*_with_limit` conversions. +pub const DEFAULT_MAX_FRAME_BYTES: usize = 1 << 30; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FrameError { + InvalidLayout, + InvalidBitDepth, + TooLarge, + Allocation, +} +impl std::fmt::Display for FrameError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::InvalidLayout => "frame storage does not match its dimensions", + Self::InvalidBitDepth => "unsupported frame sample bit depth", + Self::TooLarge => "frame exceeds the allocation limit", + Self::Allocation => "unable to allocate frame buffer", + }) + } +} +impl std::error::Error for FrameError {} +impl From for FrameError { + fn from(_: craft_color::InvalidBitDepth) -> Self { + Self::InvalidBitDepth + } +} +pub type Result = std::result::Result; + +fn elements(width: u32, height: u32, channels: usize) -> Result { + usize::try_from(width) + .ok() + .and_then(|w| usize::try_from(height).ok().and_then(|h| w.checked_mul(h))) + .and_then(|n| n.checked_mul(channels)) + .ok_or(FrameError::TooLarge) +} + +fn buffer(len: usize, max_bytes: usize) -> Result> { + if len.checked_mul(std::mem::size_of::()).is_none_or(|bytes| bytes > max_bytes || bytes > isize::MAX as usize) { + return Err(FrameError::TooLarge); + } + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|_| FrameError::Allocation)?; + out.resize(len, T::default()); + Ok(out) +} + /// Chroma subsampling of planar Y'CbCr. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Chroma { @@ -103,20 +150,51 @@ impl Region { impl VideoFrame { /// An sRGB-full, 1:1, zero-pts RGBA8 frame. `data` is `width × height × 4` bytes. - pub fn rgba8(width: u32, height: u32, data: Vec) -> Self { - debug_assert_eq!(data.len(), (width * height * 4) as usize); - Self { width, height, data: PixelData::Rgba8(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO } + pub fn rgba8(width: u32, height: u32, data: Vec) -> Result { + let frame = Self { width, height, data: PixelData::Rgba8(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO }; + frame.validate()?; + Ok(frame) } /// A linear premultiplied RGBA f32 frame, sRGB-full tags, 1:1, zero pts. - pub fn rgba_f32(width: u32, height: u32, data: Vec) -> Self { - debug_assert_eq!(data.len(), (width * height * 4) as usize); - Self { width, height, data: PixelData::RgbaF32(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO } + pub fn rgba_f32(width: u32, height: u32, data: Vec) -> Result { + let frame = Self { width, height, data: PixelData::RgbaF32(Arc::new(data)), color: ColorInfo::SRGB_FULL, par: (1, 1), pts: Tick::ZERO }; + frame.validate()?; + Ok(frame) } /// A transparent linear RGBA f32 frame of the given size. - pub fn transparent_f32(width: u32, height: u32) -> Self { - Self::rgba_f32(width, height, vec![0.0; (width * height * 4) as usize]) + pub fn transparent_f32(width: u32, height: u32) -> Result { + Self::rgba_f32(width, height, buffer(elements(width, height, 4)?, DEFAULT_MAX_FRAME_BYTES)?) + } + + /// Validate all public fields before indexing pixel storage. Rechecked by conversions because + /// callers can mutate these fields after construction. Padding beyond each required plane is allowed. + pub fn validate(&self) -> Result<()> { + let pixels = elements(self.width, self.height, 1)?; + let rgba = pixels.checked_mul(4).ok_or(FrameError::TooLarge)?; + let planar = |lengths: [usize; 3], chroma: Chroma, alpha: Option| { + let (sx, sy) = chroma.shifts(); + let cw = (self.width as usize).div_ceil(1 << sx); + let ch = (self.height as usize).div_ceil(1 << sy); + let cp = cw.checked_mul(ch).ok_or(FrameError::TooLarge)?; + if lengths[0] < pixels || lengths[1] < cp || lengths[2] < cp || alpha.is_some_and(|n| n < pixels) { + return Err(FrameError::InvalidLayout); + } + Ok(()) + }; + match &self.data { + PixelData::Rgba8(d) if d.len() == rgba => Ok(()), + PixelData::RgbaF32(d) if d.len() == rgba => Ok(()), + PixelData::Yuv8 { planes, chroma, alpha } => planar(planes.each_ref().map(|p| p.len()), *chroma, alpha.as_ref().map(|p| p.len())), + PixelData::Yuv16 { planes, chroma, bits, alpha } => { + if !(9..=16).contains(bits) { + return Err(FrameError::InvalidBitDepth); + } + planar(planes.each_ref().map(|p| p.len()), *chroma, alpha.as_ref().map(|p| p.len())) + } + _ => Err(FrameError::InvalidLayout), + } } /// Sets the presentation timestamp and returns the frame. @@ -139,18 +217,27 @@ impl VideoFrame { pub fn byte_size(&self) -> usize { match &self.data { PixelData::Rgba8(d) => d.len(), - PixelData::RgbaF32(d) => d.len() * 4, - PixelData::Yuv8 { planes, alpha, .. } => planes.iter().map(|p| p.len()).sum::() + alpha.as_ref().map_or(0, |a| a.len()), - PixelData::Yuv16 { planes, alpha, .. } => (planes.iter().map(|p| p.len()).sum::() + alpha.as_ref().map_or(0, |a| a.len())) * 2, + PixelData::RgbaF32(d) => d.len().saturating_mul(4), + PixelData::Yuv8 { planes, alpha, .. } => { + planes.iter().fold(0usize, |n, p| n.saturating_add(p.len())).saturating_add(alpha.as_ref().map_or(0, |a| a.len())) + } + PixelData::Yuv16 { planes, alpha, .. } => { + planes.iter().fold(0usize, |n, p| n.saturating_add(p.len())).saturating_add(alpha.as_ref().map_or(0, |a| a.len())).saturating_mul(2) + } } } /// Premultiplied linear-light RGBA f32, one pixel per source pixel, row-major, 4 samples each. - pub fn to_linear_f32(&self) -> Vec { + pub fn to_linear_f32(&self) -> Result> { + self.to_linear_f32_with_limit(DEFAULT_MAX_FRAME_BYTES) + } + + pub fn to_linear_f32_with_limit(&self, max_bytes: usize) -> Result> { + self.validate()?; let (w, h) = (self.width as usize, self.height as usize); - let mut out = vec![0.0; w.saturating_mul(h).saturating_mul(4)]; + let mut out = buffer::(elements(self.width, self.height, 4)?, max_bytes)?; if w == 0 || h == 0 { - return out; + return Ok(out); } match &self.data { PixelData::RgbaF32(d) => { @@ -173,8 +260,8 @@ impl VideoFrame { let info = self.color; let lin: Vec = (0..4096).map(|i| to_linear(i as f32 / 4095.0, info.transfer)).collect(); let q = |v: f32| lin[(v.clamp(0.0, 1.0) * 4095.0 + 0.5) as usize]; - let ytab: Vec = (0..256).map(|v| normalize_y(v, 8, info.range)).collect(); - let ctab: Vec = (0..256).map(|v| normalize_c(v, 8, info.range)).collect(); + let ytab: Vec = (0..256).map(|v| normalize_y(v, 8, info.range)).collect::>()?; + let ctab: Vec = (0..256).map(|v| normalize_c(v, 8, info.range)).collect::>()?; let rgb_of = |y: usize, x: usize| { let yy = ytab[planes[0][y * w + x] as usize]; let u = ctab[planes[1][(y >> sy) * cw + (x >> sx)] as usize]; @@ -200,9 +287,9 @@ impl VideoFrame { let q = |v: f32| lin[(v.clamp(0.0, 1.0) * 4095.0 + 0.5) as usize]; for y in 0..h { for x in 0..w { - let yy = normalize_y(planes[0][y * w + x] as u32, bits, info.range); - let u = normalize_c(planes[1][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range); - let v = normalize_c(planes[2][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range); + let yy = normalize_y(planes[0][y * w + x] as u32, bits, info.range)?; + let u = normalize_c(planes[1][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range)?; + let v = normalize_c(planes[2][(y >> sy) * cw + (x >> sx)] as u32, bits, info.range)?; let rgb = ycbcr_to_rgb(yy, u, v, info.matrix); let a = alpha.as_ref().map_or(1.0, |al| al[y * w + x] as f32 / amax); let o = (y * w + x) * 4; @@ -214,19 +301,28 @@ impl VideoFrame { } } } - out + Ok(out) } /// Convert to straight-alpha sRGB RGBA8 for display (fast path for 8-bit Rec.709/sRGB Y'CbCr /// without alpha, and a copy for RGBA8). - pub fn to_rgba8(&self) -> Vec { + pub fn to_rgba8(&self) -> Result> { + self.to_rgba8_with_limit(DEFAULT_MAX_FRAME_BYTES) + } + + pub fn to_rgba8_with_limit(&self, max_bytes: usize) -> Result> { + self.validate()?; let (w, h) = (self.width as usize, self.height as usize); // Empty pictures have no rows; Rayon rejects a zero-sized row chunk. if w == 0 || h == 0 { - return Vec::new(); + return Ok(Vec::new()); } match &self.data { - PixelData::Rgba8(d) => d.as_ref().clone(), + PixelData::Rgba8(d) => { + let mut out = buffer::(d.len(), max_bytes)?; + out.copy_from_slice(d); + Ok(out) + } PixelData::Yuv8 { planes, chroma, alpha: None } if matches!(self.color.transfer, Transfer::Bt709 | Transfer::Srgb) => { let (sx, sy) = chroma.shifts(); let cw = w.div_ceil(1 << sx); @@ -238,7 +334,7 @@ impl VideoFrame { }; let (crr, cbb) = (2.0 * (1.0 - kr) * cs, 2.0 * (1.0 - kb) * cs); let (cgr, cgb) = (crr * kr / kg, cbb * kb / kg); - let mut out = vec![0u8; w * h * 4]; + let mut out = buffer::(elements(self.width, self.height, 4)?, max_bytes)?; out.par_chunks_mut(w * 4).enumerate().for_each(|(y, row)| { let cy = y >> sy; let yrow = &planes[0][y * w..y * w + w]; @@ -255,30 +351,33 @@ impl VideoFrame { o[3] = 255; } }); - out + Ok(out) } _ => { - let lin = self.to_linear_f32(); - let mut out = vec![0u8; w * h * 4]; + let lin = self.to_linear_f32_with_limit(max_bytes)?; + let mut out = buffer::(elements(self.width, self.height, 4)?, max_bytes)?; out.par_chunks_mut(w * 4).zip(lin.par_chunks(w * 4)).for_each(|(o, s)| linear_premul_to_srgb8(s, o)); - out + Ok(out) } } } /// Luma plane (8-bit, for scopes and thumbnails). Y'CbCr 8-bit returns the Y plane; others go /// through [`Self::to_rgba8`] and Rec.709 luma of the encoded RGB. - pub fn luma8(&self) -> Vec { + pub fn luma8(&self) -> Result> { + self.validate()?; + let pixels = elements(self.width, self.height, 1)?; + let mut out = buffer::(pixels, DEFAULT_MAX_FRAME_BYTES)?; match &self.data { - PixelData::Yuv8 { planes, .. } => planes[0].as_ref().clone(), - _ => self - .to_rgba8() - .as_chunks::<4>() - .0 - .iter() - .map(|p| (0.2126 * p[0] as f32 + 0.7152 * p[1] as f32 + 0.0722 * p[2] as f32) as u8) - .collect(), + PixelData::Yuv8 { planes, .. } => out.copy_from_slice(&planes[0][..pixels]), + _ => { + let rgba = self.to_rgba8()?; + for (o, p) in out.iter_mut().zip(rgba.as_chunks::<4>().0) { + *o = (0.2126 * p[0] as f32 + 0.7152 * p[1] as f32 + 0.0722 * p[2] as f32) as u8; + } + } } + Ok(out) } } @@ -307,22 +406,49 @@ pub fn default_matrix(width: u32, height: u32) -> Matrix { mod tests { use super::*; + #[test] + fn malformed_storage_and_bit_depth_are_errors() { + let make = |data| VideoFrame { width: 2, height: 2, data, color: ColorInfo::REC709, par: (1, 1), pts: Tick::ZERO }; + let short = make(PixelData::Yuv8 { planes: std::array::from_fn(|_| Arc::new(vec![0])), chroma: Chroma::C420, alpha: None }); + assert_eq!(short.to_rgba8(), Err(FrameError::InvalidLayout)); + assert_eq!(short.to_linear_f32(), Err(FrameError::InvalidLayout)); + assert_eq!(short.luma8(), Err(FrameError::InvalidLayout)); + for bits in [0, 8, 17, 32, u32::MAX] { + let frame = make(PixelData::Yuv16 { planes: std::array::from_fn(|_| Arc::new(vec![0; 4])), chroma: Chroma::C444, bits, alpha: None }); + assert_eq!(frame.to_linear_f32(), Err(FrameError::InvalidBitDepth)); + } + assert!(VideoFrame::rgba8(2, 2, vec![0; 15]).is_err()); + assert!(VideoFrame::rgba_f32(2, 2, vec![0.0; 15]).is_err()); + let mut valid = VideoFrame::rgba8(1, 1, vec![0; 4]).unwrap(); + valid.width = 2; // public mutations must be checked again, not just by constructors. + assert_eq!(valid.to_linear_f32(), Err(FrameError::InvalidLayout)); + } + + #[test] + fn frame_allocations_are_checked_before_reserving() { + assert!(matches!(VideoFrame::transparent_f32(u32::MAX, u32::MAX), Err(FrameError::TooLarge))); + let frame = VideoFrame::rgba8(1, 1, vec![0; 4]).unwrap(); + assert_eq!(frame.to_rgba8_with_limit(3), Err(FrameError::TooLarge)); + assert_eq!(frame.to_linear_f32_with_limit(15), Err(FrameError::TooLarge)); + assert_eq!(frame.to_linear_f32_with_limit(16).unwrap(), vec![0.0; 4]); + } + #[test] fn empty_frames_convert_without_zero_sized_chunks() { for (w, h) in [(0, 0), (0, 1), (1, 0)] { - let frame = VideoFrame::transparent_f32(w, h); - assert!(frame.to_rgba8().is_empty()); - assert!(frame.to_linear_f32().is_empty()); - assert!(frame.luma8().is_empty()); + let frame = VideoFrame::transparent_f32(w, h).unwrap(); + assert!(frame.to_rgba8().unwrap().is_empty()); + assert!(frame.to_linear_f32().unwrap().is_empty()); + assert!(frame.luma8().unwrap().is_empty()); } } #[test] fn rgba8_roundtrips_through_to_rgba8() { let px = vec![10u8, 20, 30, 255, 0, 0, 0, 0]; - let f = VideoFrame::rgba8(2, 1, px.clone()).with_pts(Tick(42)); + let f = VideoFrame::rgba8(2, 1, px.clone()).unwrap().with_pts(Tick(42)); assert_eq!(f.pts, Tick(42)); - assert_eq!(f.to_rgba8(), px); + assert_eq!(f.to_rgba8().unwrap(), px); assert_eq!(f.format_label(), "RGBA 8-bit"); assert_eq!(f.byte_size(), 8); assert_eq!(f.color, ColorInfo::SRGB_FULL); @@ -331,14 +457,14 @@ mod tests { #[test] fn rgba8_to_linear_premultiplies() { // 50% red, 50% alpha, sRGB encoded. Linear of 128/255, times 0.5 alpha. - let f = VideoFrame::rgba8(1, 1, vec![128, 0, 0, 128]); - let lin = f.to_linear_f32(); + let f = VideoFrame::rgba8(1, 1, vec![128, 0, 0, 128]).unwrap(); + let lin = f.to_linear_f32().unwrap(); let a = 128.0 / 255.0; let r = srgb_u8_to_linear_table()[128] * a; assert!((lin[0] - r).abs() < 1e-6); assert_eq!(lin[1], 0.0); assert!((lin[3] - a).abs() < 1e-6); - let back = f.to_rgba8(); + let back = f.to_rgba8().unwrap(); assert_eq!(back[0], 128); assert_eq!(back[3], 128); } @@ -356,14 +482,14 @@ mod tests { par: (1, 1), pts: Tick::ZERO, }; - let rgba = f.to_rgba8(); + let rgba = f.to_rgba8().unwrap(); for p in rgba.as_chunks::<4>().0 { assert!((p[0] as i16 - p[1] as i16).abs() <= 1, "{p:?}"); assert!((p[1] as i16 - p[2] as i16).abs() <= 1, "{p:?}"); assert!(p[0] > 90 && p[0] < 160, "{p:?}"); assert_eq!(p[3], 255); } - assert_eq!(f.luma8(), vec![126, 126, 126, 126]); + assert_eq!(f.luma8().unwrap(), vec![126, 126, 126, 126]); assert_eq!(f.format_label(), "YUV 4:4:4 8-bit"); } @@ -383,8 +509,8 @@ mod tests { #[test] fn transparent_float_encodes_as_zero_rgba8() { - let f = VideoFrame::transparent_f32(2, 1); - assert_eq!(f.to_rgba8(), vec![0; 8]); + let f = VideoFrame::transparent_f32(2, 1).unwrap(); + assert_eq!(f.to_rgba8().unwrap(), vec![0; 8]); assert_eq!(f.byte_size(), 32); // 2 pixels × 4 f32 channels × 4 bytes } }