Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colorimetry information API in Picture #94

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion dav1d-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
107 changes: 98 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dav1d_sys::*;

pub use av_data::pixel;
use std::ffi::c_void;
use std::fmt;
use std::i64;
Expand Down Expand Up @@ -188,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<u32> for DecodeFrameType {
type Error = TryFromEnumError;

Expand Down Expand Up @@ -417,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),
})
}
Expand Down Expand Up @@ -560,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.
Expand Down Expand Up @@ -630,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.
Expand All @@ -640,6 +638,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 {}
Expand Down
Loading