From 76cf6e952339c2877b7f9bfe1f57df82d65247a9 Mon Sep 17 00:00:00 2001 From: Philippe Normand Date: Thu, 21 Mar 2024 15:35:10 +0000 Subject: [PATCH 1/2] Add colorimetry information API in Picture Fixes #93 --- Cargo.toml | 1 + src/lib.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 03fedbb..70588f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ version = "0.10.2" [dependencies] bitflags = "2" dav1d-sys = { version = "0.8.2", path = "dav1d-sys" } +av-data = "0.4.2" [features] diff --git a/src/lib.rs b/src/lib.rs index 7bf93db..38fd798 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ use dav1d_sys::*; +pub use av_data::pixel; use std::ffi::c_void; use std::fmt; use std::i64; @@ -640,6 +641,97 @@ impl Picture { pub fn offset(&self) -> i64 { self.inner.pic.m.offset } + + /// Chromaticity coordinates of the source colour primaries. + pub fn color_primaries(&self) -> pixel::ColorPrimaries { + unsafe { + #[allow(non_upper_case_globals)] + match (*self.inner.pic.seq_hdr).pri { + DAV1D_COLOR_PRI_BT709 => pixel::ColorPrimaries::BT709, + DAV1D_COLOR_PRI_UNKNOWN => pixel::ColorPrimaries::Unspecified, + DAV1D_COLOR_PRI_BT470M => pixel::ColorPrimaries::BT470M, + DAV1D_COLOR_PRI_BT470BG => pixel::ColorPrimaries::BT470BG, + DAV1D_COLOR_PRI_BT601 => pixel::ColorPrimaries::BT470BG, + DAV1D_COLOR_PRI_SMPTE240 => pixel::ColorPrimaries::ST240M, + DAV1D_COLOR_PRI_FILM => pixel::ColorPrimaries::Film, + DAV1D_COLOR_PRI_BT2020 => pixel::ColorPrimaries::BT2020, + DAV1D_COLOR_PRI_XYZ => pixel::ColorPrimaries::ST428, + DAV1D_COLOR_PRI_SMPTE431 => pixel::ColorPrimaries::P3DCI, + DAV1D_COLOR_PRI_SMPTE432 => pixel::ColorPrimaries::P3Display, + DAV1D_COLOR_PRI_EBU3213 => pixel::ColorPrimaries::Tech3213, + 23..=DAV1D_COLOR_PRI_RESERVED => pixel::ColorPrimaries::Unspecified, + _ => unreachable!(), + } + } + } + + /// Transfer characteristics function. + pub fn transfer_characteristic(&self) -> pixel::TransferCharacteristic { + unsafe { + #[allow(non_upper_case_globals)] + match (*self.inner.pic.seq_hdr).trc { + DAV1D_TRC_BT709 => pixel::TransferCharacteristic::BT1886, + DAV1D_TRC_UNKNOWN => pixel::TransferCharacteristic::Unspecified, + DAV1D_TRC_BT470M => pixel::TransferCharacteristic::BT470M, + DAV1D_TRC_BT470BG => pixel::TransferCharacteristic::BT470BG, + DAV1D_TRC_BT601 => pixel::TransferCharacteristic::ST170M, + DAV1D_TRC_SMPTE240 => pixel::TransferCharacteristic::ST240M, + DAV1D_TRC_LINEAR => pixel::TransferCharacteristic::Linear, + DAV1D_TRC_LOG100 => pixel::TransferCharacteristic::Logarithmic100, + DAV1D_TRC_LOG100_SQRT10 => pixel::TransferCharacteristic::Logarithmic316, + DAV1D_TRC_IEC61966 => pixel::TransferCharacteristic::SRGB, + DAV1D_TRC_BT1361 => pixel::TransferCharacteristic::BT1886, + DAV1D_TRC_SRGB => pixel::TransferCharacteristic::SRGB, + DAV1D_TRC_BT2020_10BIT => pixel::TransferCharacteristic::BT2020Ten, + DAV1D_TRC_BT2020_12BIT => pixel::TransferCharacteristic::BT2020Twelve, + DAV1D_TRC_SMPTE2084 => pixel::TransferCharacteristic::PerceptualQuantizer, + DAV1D_TRC_SMPTE428 => pixel::TransferCharacteristic::ST428, + DAV1D_TRC_HLG => pixel::TransferCharacteristic::HybridLogGamma, + 19..=DAV1D_TRC_RESERVED => pixel::TransferCharacteristic::Unspecified, + _ => unreachable!(), + } + } + } + + /// Matrix coefficients used in deriving luma and chroma signals from the + /// green, blue and red or X, Y and Z primaries. + pub fn matrix_coefficients(&self) -> pixel::MatrixCoefficients { + unsafe { + #[allow(non_upper_case_globals)] + match (*self.inner.pic.seq_hdr).mtrx { + DAV1D_MC_IDENTITY => pixel::MatrixCoefficients::Identity, + DAV1D_MC_BT709 => pixel::MatrixCoefficients::BT709, + DAV1D_MC_UNKNOWN => pixel::MatrixCoefficients::Unspecified, + DAV1D_MC_FCC => pixel::MatrixCoefficients::BT470M, + DAV1D_MC_BT470BG => pixel::MatrixCoefficients::BT470BG, + DAV1D_MC_BT601 => pixel::MatrixCoefficients::BT470BG, + DAV1D_MC_SMPTE240 => pixel::MatrixCoefficients::ST240M, + DAV1D_MC_SMPTE_YCGCO => pixel::MatrixCoefficients::YCgCo, + DAV1D_MC_BT2020_NCL => pixel::MatrixCoefficients::BT2020NonConstantLuminance, + DAV1D_MC_BT2020_CL => pixel::MatrixCoefficients::BT2020ConstantLuminance, + DAV1D_MC_SMPTE2085 => pixel::MatrixCoefficients::ST2085, + DAV1D_MC_CHROMAT_NCL => { + pixel::MatrixCoefficients::ChromaticityDerivedNonConstantLuminance + } + DAV1D_MC_CHROMAT_CL => { + pixel::MatrixCoefficients::ChromaticityDerivedConstantLuminance + } + DAV1D_MC_ICTCP => pixel::MatrixCoefficients::ICtCp, + 15..=DAV1D_MC_RESERVED => pixel::MatrixCoefficients::Unspecified, + _ => unreachable!(), + } + } + } + + /// YUV color range. + pub fn color_range(&self) -> pixel::YUVRange { + unsafe { + match (*self.inner.pic.seq_hdr).color_range { + 0 => pixel::YUVRange::Limited, + _ => pixel::YUVRange::Full, + } + } + } } unsafe impl Send for Picture {} From c2f30220cd87bc3e60beba7f04cc007c3b2fb3e9 Mon Sep 17 00:00:00 2001 From: Philippe Normand Date: Fri, 22 Mar 2024 17:32:02 +0000 Subject: [PATCH 2/2] Fix clippy warnings --- dav1d-sys/build.rs | 2 +- src/lib.rs | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/dav1d-sys/build.rs b/dav1d-sys/build.rs index d228929..f5f9bbc 100644 --- a/dav1d-sys/build.rs +++ b/dav1d-sys/build.rs @@ -67,7 +67,7 @@ mod build { runner!("meson", "install", "-C", build_path.to_str().unwrap()); let pkg_dir = build_path.join("meson-private"); - system_deps::Library::from_internal_pkg_config(&pkg_dir, lib, TAG) + system_deps::Library::from_internal_pkg_config(pkg_dir, lib, TAG) } } diff --git a/src/lib.rs b/src/lib.rs index 38fd798..7db0e84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,20 +189,15 @@ bitflags::bitflags! { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)] pub enum DecodeFrameType { + #[default] All, Reference, Intra, Key, } -impl Default for DecodeFrameType { - fn default() -> Self { - DecodeFrameType::All - } -} - impl TryFrom for DecodeFrameType { type Error = TryFromEnumError; @@ -418,6 +413,8 @@ impl Decoder { } else { let inner = InnerPicture { pic }; Ok(Picture { + // https://github.com/rust-av/dav1d-rs/issues/95 + #[allow(clippy::arc_with_non_send_sync)] inner: Arc::new(inner), }) } @@ -561,7 +558,7 @@ impl Picture { PixelLayout::I400 | PixelLayout::I422 | PixelLayout::I444 => self.height(), }, }; - (self.stride(component) as u32, height) + (self.stride(component), height) } /// Plane data of the `component` for the decoded frame. @@ -631,7 +628,7 @@ impl Picture { /// This is the same duration as the one provided to [`Decoder::send_data`] or `0` if none was /// provided. pub fn duration(&self) -> i64 { - self.inner.pic.m.duration as i64 + self.inner.pic.m.duration } /// Offset of the frame.