FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#924: decoded H.264 / HEVC frames keep the signalled colour primaries
JordyUSA0166dc1d6c7c
diff --git a/crates/codecs/src/hw.rs b/crates/codecs/src/hw.rsindex 771de6476..b73b963c4 100644--- a/crates/codecs/src/hw.rs+++ b/crates/codecs/src/hw.rs@@ -258,7 +258,7 @@ impl NalStreamInfo {bit_depth_chroma: sps.bit_depth_chroma,interlaced: !sps.frame_mbs_only,profile_idc: sps.profile_idc,- color: vui_color(crop.2, crop.3, vui.matrix_coefficients, vui.transfer_characteristics, vui.full_range),+ color: vui_color(crop.2, crop.3, vui.colour_primaries, vui.matrix_coefficients, vui.transfer_characteristics, vui.full_range),par: sar_par(vui.sar),// as the software decoder's DPB (`max_reorder`)reorder: sps.max_num_reorder_frames().min(sps.max_dpb_frames()),@@ -304,7 +304,7 @@ impl NalStreamInfo {bit_depth_chroma: sps.bit_depth_chroma,interlaced: false,profile_idc: sps.ptl.profile_idc,- color: vui_color(crop.2, crop.3, vui.matrix_coefficients, vui.transfer_characteristics, vui.full_range),+ color: vui_color(crop.2, crop.3, vui.colour_primaries, vui.matrix_coefficients, vui.transfer_characteristics, vui.full_range),par: sar_par(vui.sar),reorder: sps.max_num_reorder as usize,})diff --git a/crates/codecs/src/video.rs b/crates/codecs/src/video.rsindex fa8eb9de3..e646c4744 100644--- a/crates/codecs/src/video.rs+++ b/crates/codecs/src/video.rs@@ -140,11 +140,14 @@ pub(crate) fn tight_plane<T: Copy>(src: Vec<T>, stride: usize, w: usize, h: usiz}/// Colour of an H.264 / HEVC picture from its VUI code points (ITU-T H.273), as the decoders-/// report it: BT.709 by default with the matrix guessed from the size, the signalled matrix and-/// transfer when known, and full range when flagged. Shared by the software and hardware-/// decoders so the two are interchangeable.-pub fn vui_color(width: u32, height: u32, matrix: u8, transfer: u8, full_range: bool) -> filmcraft_color::ColorInfo {+/// report it: BT.709 by default with the matrix guessed from the size, the signalled primaries,+/// matrix and transfer when known, and full range when flagged. Shared by the software and+/// hardware decoders so the two are interchangeable.+pub fn vui_color(width: u32, height: u32, primaries: u8, matrix: u8, transfer: u8, full_range: bool) -> filmcraft_color::ColorInfo {let mut color = filmcraft_color::ColorInfo { matrix: filmcraft_frame::default_matrix(width, height), ..filmcraft_color::ColorInfo::REC709 };+ if let Some(p) = primaries_from_code(primaries) {+ color.primaries = p;+ }if let Some(m) = filmcraft_color::Matrix::from_code(matrix) {color.matrix = m;}@@ -250,7 +253,7 @@ impl H264Decoder {let y = tight_plane(p.y, p.y_stride, w, h);let u = tight_plane(p.u, p.uv_stride, cw, ch);let v = tight_plane(p.v, p.uv_stride, cw, ch);- let color = vui_color(p.width, p.height, p.color.matrix, p.color.transfer, p.color.full_range);+ let color = vui_color(p.width, p.height, p.color.primaries, p.color.matrix, p.color.transfer, p.color.full_range);let par = sar_par(p.sar);let frame = VideoFrame {width: p.width,@@ -371,7 +374,7 @@ impl HevcDecoder {alpha: None,},};- let color = vui_color(p.width, p.height, p.color.matrix, p.color.transfer, p.color.full_range);+ let color = vui_color(p.width, p.height, p.color.primaries, p.color.matrix, p.color.transfer, p.color.full_range);let par = sar_par(p.sar);let frame = VideoFrame { width: p.width, height: p.height, data, color, par, pts: filmcraft_time::Tick::ZERO };DecodedFrame { pts: p.pts, frame, draft: p.draft }diff --git a/crates/codecs/tests/frame_color_oracle.rs b/crates/codecs/tests/frame_color_oracle.rsnew file mode 100644index 000000000..7da3baf09--- /dev/null+++ b/crates/codecs/tests/frame_color_oracle.rs@@ -0,0 +1,61 @@+//! Decoded H.264 / HEVC frames carry the colour primaries their VUI signals (#924). ffmpeg writes+//! MP4 without a `colr` box unless asked, so for most files the bitstream is the only place the+//! primaries are described: stream info already read them, the decoded frames reported BT.709.++mod common;++use common::*;+use filmcraft_color::{ColorInfo, ColorSpace, Matrix, Primaries, Range, Transfer};+use filmcraft_media::FrameRequest;+use filmcraft_time::Tick;++/// One 64×64 frame of `codec` with the colour `params` (x264 / x265 syntax), and our stream and+/// frame colour for it.+fn colors(ff: &std::path::Path, name: &str, codec: &[&str]) -> Option<(ColorInfo, ColorInfo)> {+ let mut args = vec!["-f", "lavfi", "-i", "testsrc2=size=64x64:rate=1", "-frames:v", "1"];+ args.extend_from_slice(codec);+ let path = fixture(ff, name, &args)?;+ let isobmff = filmcraft_isobmff::open(bytes(&path)).unwrap();+ let track = isobmff.tracks.iter().find(|t| t.kind == filmcraft_isobmff::TrackKind::Video).unwrap();+ assert!(track.entries[0].video.as_ref().unwrap().color.is_none(), "{name}: the fixture has a colr box");+ let src = filmcraft_codecs::open_bytes(name, bytes(&path)).unwrap();+ let stream = src.info().video.as_ref().unwrap().color;+ let frame = src.video_frame(FrameRequest::full(Tick::ZERO)).unwrap().color;+ Some((stream, frame))+}++#[test]+fn decoded_frames_keep_the_signalled_primaries() {+ let ff = filmcraft_testkit::require_ffmpeg!();+ let x264 = |p: &'static str| ["-c:v", "libx264", "-pix_fmt", "yuv420p", "-x264-params", p];+ let x265 = |p: &'static str, pix: &'static str| ["-c:v", "libx265", "-pix_fmt", pix, "-tag:v", "hvc1", "-x265-params", p];+ let sdr2020 = ColorInfo { matrix: Matrix::Bt709, transfer: Transfer::Bt709, primaries: Primaries::Bt2020, range: Range::Limited };+ let cases: [(&str, Vec<&str>, ColorInfo, ColorSpace); 4] = [+ ("prim2020_h264.mp4", x264("colorprim=bt2020:transfer=bt709:colormatrix=bt709").to_vec(), sdr2020, ColorSpace::Rec2020),+ ("prim2020_hevc.mp4", x265("log-level=error:colorprim=bt2020:transfer=bt709:colormatrix=bt709", "yuv420p").to_vec(), sdr2020, ColorSpace::Rec2020),+ // control: BT.709 throughout+ (+ "prim709_h264.mp4",+ x264("colorprim=bt709:transfer=bt709:colormatrix=bt709").to_vec(),+ ColorInfo { primaries: Primaries::Bt709, ..sdr2020 },+ ColorSpace::Rec709,+ ),+ // HDR keeps its transfer and now its primaries too+ (+ "prim2020_pq_hevc.mp4",+ x265("log-level=error:colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc", "yuv420p10le").to_vec(),+ ColorInfo { matrix: Matrix::Bt2020Ncl, transfer: Transfer::Pq, primaries: Primaries::Bt2020, range: Range::Limited },+ ColorSpace::Rec2100Pq,+ ),+ ];+ for (name, codec, want, space) in cases {+ let Some((stream, frame)) = colors(&ff, name, &codec) else {+ eprintln!("SKIPPED ({name}): this ffmpeg can't encode it");+ continue;+ };+ println!("{name}: stream {stream:?}, frame {frame:?}");+ assert_eq!(stream, want, "{name}: stream");+ assert_eq!(frame, want, "{name}: decoded frame");+ assert_eq!(ColorSpace::from_info(&frame), space, "{name}");+ }+}