From fc54b56b652d8b681ea74b371dd6d4c81de5f6f7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 11:17:54 -0500 Subject: [PATCH] Read CinemaDNG RGBA component depth and reference levels --- crates/media/src/mxf/file.rs | 53 ++++++++++++++++++++++++++--- crates/media/src/mxf/meta.rs | 2 ++ crates/media/src/mxf/write/tests.rs | 4 +++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/crates/media/src/mxf/file.rs b/crates/media/src/mxf/file.rs index 9064bcb83..9a0928077 100644 --- a/crates/media/src/mxf/file.rs +++ b/crates/media/src/mxf/file.rs @@ -571,8 +571,32 @@ fn descriptor_kind(s: &Set) -> Option { } } +// PictureInfo has one depth for all color components. Do not invent one for a +// malformed layout or unequal RGB precisions. PixelLayout is eight code/size pairs. +fn rgba_component_depth(layout: &[u8]) -> u32 { + if layout.len() != 16 { + return 0; + } + let mut depths = [0u8; 3]; + for pair in layout.chunks_exact(2) { + let index = match pair[0] { + b'R' => 0, + b'G' => 1, + b'B' => 2, + 0 => break, + _ => continue, + }; + if depths[index] != 0 || pair[1] == 0 { + return 0; + } + depths[index] = pair[1]; + } + if depths[0] != 0 && depths.iter().all(|&depth| depth == depths[0]) { u32::from(depths[0]) } else { 0 } +} + fn picture_info(d: &Set) -> PictureInfo { let u = |t| d.u(t).unwrap_or(0) as u32; + let rgba = d.kind() == Some(st::RGBA_DESCRIPTOR); PictureInfo { stored_width: u(tag::STORED_WIDTH), stored_height: u(tag::STORED_HEIGHT), @@ -581,13 +605,13 @@ fn picture_info(d: &Set) -> PictureInfo { frame_layout: u(tag::FRAME_LAYOUT) as u8, field_dominance: u(tag::FIELD_DOMINANCE) as u8, aspect_ratio: d.rational(tag::ASPECT_RATIO).unwrap_or_default(), - component_depth: u(tag::COMPONENT_DEPTH), + component_depth: if rgba { d.get(tag::PIXEL_LAYOUT).map_or(0, rgba_component_depth) } else { u(tag::COMPONENT_DEPTH) }, horizontal_subsampling: u(tag::HORIZONTAL_SUBSAMPLING), vertical_subsampling: u(tag::VERTICAL_SUBSAMPLING), - black_ref_level: d.u(tag::BLACK_REF_LEVEL).map(|v| v as u32), - white_ref_level: d.u(tag::WHITE_REF_LEVEL).map(|v| v as u32), + black_ref_level: d.u(if rgba { tag::COMPONENT_MIN_REF } else { tag::BLACK_REF_LEVEL }).map(|v| v as u32), + white_ref_level: d.u(if rgba { tag::COMPONENT_MAX_REF } else { tag::WHITE_REF_LEVEL }).map(|v| v as u32), alpha_depth: u(tag::ALPHA_SAMPLE_DEPTH), - rgba: d.kind() == Some(st::RGBA_DESCRIPTOR), + rgba, picture_coding: d.ul(tag::PICTURE_ESSENCE_CODING), transfer_characteristic: d.ul(tag::TRANSFER_CHARACTERISTIC), coding_equations: d.ul(tag::CODING_EQUATIONS), @@ -1343,3 +1367,24 @@ mod fallback_tracks_tests { assert_eq!(tracks.len(), 1); } } + +#[cfg(test)] +mod rgba_layout_tests { + use super::rgba_component_depth; + + #[test] + fn only_complete_uniform_rgb_layouts_have_one_depth() { + let mut layout = [0; 16]; + layout[..8].copy_from_slice(&[b'B', 16, b'G', 16, b'R', 16, b'A', 8]); + assert_eq!(rgba_component_depth(&layout), 16); + assert_eq!(rgba_component_depth(&layout[..15]), 0); + layout[3] = 8; + assert_eq!(rgba_component_depth(&layout), 0); + layout[3] = 16; + layout[4] = b'G'; + assert_eq!(rgba_component_depth(&layout), 0); + layout[4] = 0; + assert_eq!(rgba_component_depth(&layout), 0); + assert_eq!(rgba_component_depth(&[0; 16]), 0); + } +} diff --git a/crates/media/src/mxf/meta.rs b/crates/media/src/mxf/meta.rs index d02afdd71..2b649d099 100644 --- a/crates/media/src/mxf/meta.rs +++ b/crates/media/src/mxf/meta.rs @@ -204,6 +204,8 @@ pub mod tag { pub const WHITE_REF_LEVEL: u16 = 0x3305; pub const ALPHA_SAMPLE_DEPTH: u16 = 0x3309; pub const PIXEL_LAYOUT: u16 = 0x3401; + pub const COMPONENT_MAX_REF: u16 = 0x3406; + pub const COMPONENT_MIN_REF: u16 = 0x3407; pub const AUDIO_SAMPLING_RATE: u16 = 0x3D03; pub const LOCKED: u16 = 0x3D02; pub const CHANNEL_COUNT: u16 = 0x3D07; diff --git a/crates/media/src/mxf/write/tests.rs b/crates/media/src/mxf/write/tests.rs index eac50a0e9..bc60423e9 100644 --- a/crates/media/src/mxf/write/tests.rs +++ b/crates/media/src/mxf/write/tests.rs @@ -434,6 +434,10 @@ fn cinemadng_op1a_reads_back() { assert_eq!(t.samples.len(), 6); let p = t.picture.as_ref().unwrap(); assert!(p.rgba, "RGBA picture essence descriptor (ยง7.2.2)"); + assert_eq!(p.component_depth, 16); + assert_eq!(p.black_ref_level, Some(0)); + assert_eq!(p.white_ref_level, Some(65_535)); + assert!(p.full_range()); assert_eq!((p.frame_width(), p.frame_height()), (64, 36)); assert!(p.picture_coding.is_none()); for i in 0..6 {