Validate frame alpha depth and correct color metadata contracts

This commit is contained in:
FredApps committed 2026-11-16 17:41:25 +01:00
1 parent d839db7d4a
commit cc63a31309
2 files changed
+46 -5

No files matched your search

+8 -4
View File
@@ -39,7 +39,8 @@ impl Matrix {
match c {
1 => Some(Matrix::Bt709),
5 | 6 => Some(Matrix::Bt601),
9 | 10 => Some(Matrix::Bt2020Ncl),
// H.273 table 4: code 10 is constant luminance, not this matrix.
9 => Some(Matrix::Bt2020Ncl),
_ => None,
}
}
@@ -48,7 +49,8 @@ impl Matrix {
/// Transfer characteristics.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Transfer {
/// BT.709 / BT.1886 display (pure 2.4 gamma is what NLEs use for display-referred decode).
/// BT.709 display, approximated with the sRGB curve by this slim conversion layer.
/// This is not a pure-gamma BT.1886 decode.
#[default]
Bt709,
Srgb,
@@ -126,8 +128,7 @@ impl HdrMetadata {
pub fn peak_nits(&self) -> Option<f32> {
let known = |v: Option<f32>| v.filter(|n| *n > 0.0);
let p = match (known(self.max_cll), known(self.mastering_max_nits)) {
(Some(c), Some(m)) => c.min(m),
(Some(c), None) => c,
(Some(c), _) => c,
(None, m) => m?,
};
Some(p.clamp(REFERENCE_WHITE_NITS as f32, 10_000.0))
@@ -420,6 +421,7 @@ mod tests {
#[test]
fn hdr_peak_nits() {
assert_eq!(HdrMetadata::default().peak_nits(), None);
assert_eq!(HdrMetadata { max_cll: Some(4000.0), mastering_max_nits: Some(1000.0), ..HdrMetadata::default() }.peak_nits(), Some(4000.0));
assert_eq!(HdrMetadata { max_cll: Some(1000.0), mastering_max_nits: Some(4000.0), ..HdrMetadata::default() }.peak_nits(), Some(1000.0));
assert_eq!(HdrMetadata { max_cll: Some(0.0), mastering_max_nits: Some(400.0), ..HdrMetadata::default() }.peak_nits(), Some(400.0));
// never below reference white
@@ -428,6 +430,8 @@ mod tests {
#[test]
fn codes() {
assert_eq!(Matrix::from_code(9), Some(Matrix::Bt2020Ncl));
assert_eq!(Matrix::from_code(10), None);
assert_eq!(Matrix::from_code(1), Some(Matrix::Bt709));
assert_eq!(Matrix::from_code(6), Some(Matrix::Bt601));
assert_eq!(Transfer::from_code(16), Some(Transfer::Pq));
+38 -1
View File
@@ -31,6 +31,7 @@ pub const DEFAULT_MAX_FRAME_BYTES: usize = 1 << 30;
pub enum FrameError {
InvalidLayout,
InvalidBitDepth,
InvalidSample,
TooLarge,
Allocation,
}
@@ -39,6 +40,7 @@ impl std::fmt::Display for FrameError {
f.write_str(match self {
Self::InvalidLayout => "frame storage does not match its dimensions",
Self::InvalidBitDepth => "unsupported frame sample bit depth",
Self::InvalidSample => "frame sample exceeds its declared bit depth",
Self::TooLarge => "frame exceeds the allocation limit",
Self::Allocation => "unable to allocate frame buffer",
})
@@ -191,7 +193,14 @@ impl VideoFrame {
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()))
planar(planes.each_ref().map(|p| p.len()), *chroma, alpha.as_ref().map(|p| p.len()))?;
let max = (1u32 << bits) - 1;
// Alpha is full-range at the declared depth. Out-of-range values would produce
// opacity above one and corrupt premultiplied colors; unused padding is ignored.
if alpha.as_ref().is_some_and(|a| a.iter().take(pixels).any(|&v| u32::from(v) > max)) {
return Err(FrameError::InvalidSample);
}
Ok(())
}
_ => Err(FrameError::InvalidLayout),
}
@@ -470,6 +479,34 @@ mod tests {
assert_eq!(fast.to_rgba8(), Err(FrameError::InvalidLayout));
}
#[test]
fn yuv16_alpha_respects_declared_bit_depth() {
let make = |alpha, bits| VideoFrame {
width: 1,
height: 1,
data: PixelData::Yuv16 {
planes: [Arc::new(vec![128]), Arc::new(vec![128]), Arc::new(vec![128])],
chroma: Chroma::C444,
bits,
alpha: Some(Arc::new(vec![alpha])),
},
color: ColorInfo::REC709,
par: (1, 1),
pts: Tick::ZERO,
};
for bits in [9, 10, 12, 16] {
let max = ((1u32 << bits) - 1) as u16;
let valid = make(max, bits);
assert_eq!(valid.to_linear_f32().unwrap()[3], 1.0);
assert_eq!(valid.to_rgba8().unwrap()[3], 255);
if bits < 16 {
let invalid = make(max + 1, bits);
assert!(invalid.to_linear_f32().is_err());
assert!(invalid.to_rgba8().is_err());
assert!(make(u16::MAX, bits).validate().is_err());
}
}
}
#[test]
fn frame_allocations_are_checked_before_reserving() {
assert!(matches!(VideoFrame::transparent_f32(u32::MAX, u32::MAX), Err(FrameError::TooLarge)));