use gpu_handle_types::{PixelFormat, PlaneFormat};
use video_types::{ColorFamily, ColorRange, ResolvedRange};
#[test]
fn nv12_plane_sizes() {
let fmt = PixelFormat::NV12;
assert_eq!(fmt.plane_size(1920, 1080, 0), (1920, 1080));
assert_eq!(fmt.plane_size(1920, 1080, 1), (960, 540));
}
#[test]
fn yuv420p_plane_sizes() {
assert_eq!(PixelFormat::YUV420P.plane_size(1920, 1080, 0), (1920, 1080));
assert_eq!(PixelFormat::YUV420P.plane_size(1920, 1080, 1), (960, 540));
assert_eq!(PixelFormat::YUV420P.plane_size(1920, 1080, 2), (960, 540));
}
#[test]
fn hdr_native_classification() {
assert!(!PixelFormat::NV12.is_hdr_native());
assert!(!PixelFormat::RgbaU8.is_hdr_native());
assert!(PixelFormat::P010LE.is_hdr_native());
assert!(PixelFormat::RgbaF16.is_hdr_native());
assert!(PixelFormat::RgbaU16.is_hdr_native());
}
#[test]
fn bits_per_sample_spot_checks() {
assert_eq!(PixelFormat::NV12.bits_per_sample(), 8);
assert_eq!(PixelFormat::P010LE.bits_per_sample(), 10);
assert_eq!(PixelFormat::YUV422P12LE.bits_per_sample(), 12);
assert_eq!(PixelFormat::RgbaF16.bits_per_sample(), 16);
assert_eq!(PixelFormat::RgbaF32.bits_per_sample(), 32);
}
#[test]
fn storage_sample_scale_alignment_bridge() {
assert_eq!(PixelFormat::YUV420P10LE.storage_sample_scale(), 64.0); assert_eq!(PixelFormat::YUV422P12LE.storage_sample_scale(), 16.0); assert_eq!(PixelFormat::YUV444P14LE.storage_sample_scale(), 4.0); assert_eq!(PixelFormat::YUV420P10LE.storage_sample_scale_inv(), 1.0 / 64.0);
assert_eq!(PixelFormat::P010LE.storage_sample_scale(), 1.0);
assert_eq!(PixelFormat::YUV420P16LE.storage_sample_scale(), 1.0);
assert_eq!(PixelFormat::YUV420P.storage_sample_scale(), 1.0);
assert_eq!(PixelFormat::NV12.storage_sample_scale(), 1.0);
assert_eq!(PixelFormat::RgbaU16.storage_sample_scale(), 1.0);
assert_eq!(PixelFormat::RgbaU16.storage_sample_scale_inv(), 1.0);
}
#[test]
fn yuva444p10le_layout() {
let f = PixelFormat::YUVA444P10LE;
let l = f.plane_layout();
assert_eq!(l.count(), 4);
for i in 0..4u8 {
let info = l.plane(i).expect("YUVA444P10LE has 4 planes");
assert_eq!(info.format, PlaneFormat::R16, "plane {i} storage");
assert_eq!(info.size_scale, (1, 1), "plane {i} is full-res (4:4:4 + full-res alpha)");
}
assert_eq!(f.plane_size(1920, 1080, 3), (1920, 1080));
assert_eq!(f.bits_per_sample(), 10);
assert!(!f.is_biplanar());
assert!(f.is_hdr_native());
assert!(!f.is_msb_aligned_storage());
assert_eq!(f.storage_sample_scale(), 64.0);
assert_eq!(f.storage_sample_scale_inv(), 1.0 / 64.0);
let f12 = PixelFormat::YUVA444P12LE;
assert_eq!(f12.plane_layout(), l);
assert_eq!(f12.bits_per_sample(), 12);
assert!(!f12.is_msb_aligned_storage());
assert_eq!(f12.storage_sample_scale(), 16.0); }
#[test]
fn odd_dim_chroma_plane_size_rounds_up() {
fn ff_ceil(dim: u32, scale: u32) -> u32 {
let s = scale.trailing_zeros(); (dim + (1 << s) - 1) >> s
}
let (w, h) = (321u32, 241u32);
for fmt in [PixelFormat::NV12, PixelFormat::P010LE, PixelFormat::YUV420P, PixelFormat::YUV420P10LE] {
assert_eq!(fmt.plane_size(w, h, 0), (w, h), "{fmt:?} luma");
let chroma = fmt.plane_size(w, h, 1);
assert_eq!(chroma, (ff_ceil(w, 2), ff_ceil(h, 2)), "{fmt:?} chroma ceil");
assert_eq!(chroma, (161, 121), "{fmt:?} chroma 4:2:0 @ 321x241");
}
for fmt in [PixelFormat::P210LE, PixelFormat::YUV422P, PixelFormat::YUV422P10LE] {
let chroma = fmt.plane_size(w, h, 1);
assert_eq!(chroma, (ff_ceil(w, 2), ff_ceil(h, 1)), "{fmt:?} chroma ceil");
assert_eq!(chroma, (161, 241), "{fmt:?} chroma 4:2:2 @ 321x241");
}
assert_eq!(PixelFormat::NV12.plane_size(1920, 1080, 1), (960, 540));
assert_eq!(PixelFormat::YUV420P.plane_size(1920, 1080, 2), (960, 540));
}
#[test]
fn ayuv64le_layout() {
let l = PixelFormat::AYUV64LE.plane_layout();
assert_eq!(l.count(), 1);
assert_eq!(l.plane0.format, PlaneFormat::RGBA16);
assert!(l.extra.iter().all(Option::is_none));
assert_eq!(PixelFormat::AYUV64LE.bits_per_sample(), 16);
assert!(PixelFormat::AYUV64LE.is_hdr_native());
}
#[test]
fn color_family_drives_the_levels_guess() {
assert_eq!(PixelFormat::NV12.color_family(), ColorFamily::Yuv);
assert_eq!(PixelFormat::RgbaU8.color_family(), ColorFamily::Rgb);
assert_eq!(PixelFormat::R16U.color_family(), ColorFamily::Scalar);
assert_eq!(ColorRange::Unspecified.resolve(PixelFormat::NV12.color_family()), ResolvedRange::Limited);
assert_eq!(ColorRange::Unspecified.resolve(PixelFormat::RgbaU8.color_family()), ResolvedRange::Full);
assert_eq!(ColorRange::Unspecified.resolve(PixelFormat::R16U.color_family()), ResolvedRange::Full);
assert_eq!(ColorRange::Full.resolve(PixelFormat::NV12.color_family()), ResolvedRange::Full);
assert_eq!(ColorRange::Limited.resolve(PixelFormat::RgbaU8.color_family()), ResolvedRange::Limited);
}
#[test]
fn packed_expand_depth_owes_a_decode_only_for_limited_integer_rgb() {
use PixelFormat as P;
use ResolvedRange as R;
assert_eq!(P::RgbaU8.packed_expand_depth(R::Limited), 8);
assert_eq!(P::BgraU8.packed_expand_depth(R::Limited), 8);
assert_eq!(P::Rgb10A2.packed_expand_depth(R::Limited), 10);
assert_eq!(P::RgbaU16.packed_expand_depth(R::Limited), 16);
assert_eq!(P::BgraU16.packed_expand_depth(R::Limited), 16);
assert_eq!(P::RgbU16.packed_expand_depth(R::Limited), 16);
for fmt in [P::RgbaF16, P::BgraF16, P::RgbaF32, P::BgraF32] {
assert_eq!(fmt.packed_expand_depth(R::Limited), 0, "{fmt:?}");
}
for fmt in [P::R16U, P::R32F, P::Rgba16U] {
assert_eq!(fmt.packed_expand_depth(R::Limited), 0, "{fmt:?}");
}
for fmt in [P::NV12, P::P010LE, P::YUV420P, P::YUV444P16LE, P::AYUV64LE, P::V216, P::Y210LE, P::NV12A] {
assert_eq!(fmt.packed_expand_depth(R::Limited), 0, "{fmt:?}");
assert_eq!(fmt.packed_expand_depth(ColorRange::Unspecified.resolve(fmt.color_family())), 0, "{fmt:?}");
}
}
#[test]
fn packed_expand_depth_is_resolve_composed_with_the_code_space() {
use ColorRange as W;
use PixelFormat as P;
use ResolvedRange as R;
for fmt in [P::RgbaU8, P::Rgb10A2, P::RgbaU16, P::NV12, P::R16U] {
assert_eq!(fmt.packed_expand_depth(R::Full), 0, "{fmt:?}");
}
for fmt in [P::RgbaU8, P::BgraU8, P::Rgb10A2, P::RgbaU16, P::BgraU16, P::RgbU16] {
assert_eq!(fmt.packed_expand_depth(W::Unspecified.resolve(fmt.color_family())), 0, "{fmt:?}");
}
for fmt in [P::RgbaU8, P::BgraU8, P::Rgb10A2, P::RgbaU16, P::BgraU16, P::RgbU16] {
assert_ne!(fmt.packed_expand_depth(W::Limited.resolve(fmt.color_family())), 0, "{fmt:?}");
}
}