forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

color.rs181 lines, 6.1 KiBHistoryRawDownload
1//! A per-clip colour grade, end to end: what the renderer shows and what the
2//! export writes have to be the same picture, and a clip nobody graded has to
3//! come out of both untouched.
4//!
5//! `test_baseline.mp4` is colour bars (mean channel spread 249 of 255), so
6//! saturation 0 is not a subtle claim: every pixel of a graded span is grey to
7//! within the converter's own rounding, and every pixel outside it is not.
8//!
9//! ```text
10//! cargo test -p engine --release --test color -- --nocapture --test-threads=1
11//! ```
12
13use std::path::{Path, PathBuf};
14use std::thread::sleep;
15use std::time::{Duration, Instant};
16
17use engine::color::ColorParams;
18use engine::export::ExportSettings;
19use engine::project::Lane;
20use engine::scratch::Scratch;
21use engine::{DecodeSession, ExportHandle, PlaybackSession};
22
23const FPS: f64 = 30.0;
24/// Where the first clip ends and the ungraded one begins.
25const CUT: u32 = 30;
26
27fn asset(name: &str) -> PathBuf {
28 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
29 .join("../../assets")
30 .join(name)
31}
32
33/// Silent, like every other suite that plays a real file: the grade is a
34/// picture setting and nothing here listens.
35fn open(path: &Path) -> PlaybackSession {
36 let session = PlaybackSession::open(path).expect("open");
37 session.set_gain(0.0);
38 session
39}
40
41/// Two clips on `V1`, the first of them drained of all colour.
42fn graded() -> PlaybackSession {
43 let mut session = open(&asset("test_baseline.mp4"));
44 assert!(session.cut_at(f64::from(CUT) / FPS), "cut at frame 30");
45 assert!(
46 session.set_color(
47 Lane::V1,
48 0,
49 Some(ColorParams {
50 saturation: 0.0,
51 ..Default::default()
52 })
53 ),
54 "grade the first clip"
55 );
56 session
57}
58
59/// How far a BGRA frame is from grey: the mean spread between its channels,
60/// which is 0 for a greyscale picture and ~250 for these colour bars.
61fn spread(bgra: &[u8]) -> f64 {
62 let total: u64 = bgra
63 .as_chunks::<4>().0.iter()
64 .map(|px| u64::from(px[..3].iter().max().unwrap() - px[..3].iter().min().unwrap()))
65 .sum();
66 total as f64 / (bgra.len() / 4) as f64
67}
68
69/// The next frame off a freshly seeked channel.
70fn next_frame(session: &mut PlaybackSession, what: &str) -> engine::Frame {
71 let deadline = Instant::now() + Duration::from_secs(10);
72 loop {
73 if let Some(frame) = session.try_frame() {
74 return frame;
75 }
76 assert!(Instant::now() < deadline, "no frame after {what}");
77 sleep(Duration::from_millis(4));
78 }
79}
80
81fn seek_and_show(session: &mut PlaybackSession, frame: u32) -> Vec<u8> {
82 session.seek(f64::from(frame) / FPS);
83 let shown = next_frame(session, "a seek");
84 assert_eq!(shown.index, frame, "the seek landed elsewhere");
85 shown.bgra
86}
87
88/// The render half: the grade reaches the picture, and it stops at the clip's
89/// own edge rather than colouring the whole timeline.
90#[test]
91fn a_graded_clip_renders_grey_and_the_next_one_does_not() {
92 let mut session = graded();
93 for frame in [0, CUT - 1] {
94 let inside = spread(&seek_and_show(&mut session, frame));
95 assert!(
96 inside <= 2.0,
97 "frame {frame} is graded and not grey: {inside}"
98 );
99 }
100 for frame in [CUT, CUT + 15] {
101 let outside = spread(&seek_and_show(&mut session, frame));
102 assert!(
103 outside > 100.0,
104 "frame {frame} is outside the graded clip and lost its colour: {outside}"
105 );
106 }
107}
108
109/// Live: a grade set while the session is up repaints what is on screen. The
110/// engine reseeks on the edit, so the very next frame is the graded one -- no
111/// scrub, no reopen by the caller.
112#[test]
113fn setting_a_grade_repaints_the_frame_that_is_already_up() {
114 let mut session = open(&asset("test_baseline.mp4"));
115 let before = spread(&seek_and_show(&mut session, 10));
116 assert!(before > 100.0, "the fixture is not colour bars: {before}");
117
118 assert!(session.set_color(
119 Lane::V1,
120 0,
121 Some(ColorParams {
122 saturation: 0.0,
123 ..Default::default()
124 })
125 ));
126 let after = spread(&next_frame(&mut session, "a grade").bgra);
127 assert!(after <= 2.0, "the frame on screen kept its colour: {after}");
128
129 // And taking it off puts the colour back, on the same terms.
130 assert!(session.set_color(Lane::V1, 0, None));
131 let back = spread(&next_frame(&mut session, "an ungrade").bgra);
132 assert!(back > 100.0, "ungrading left the picture grey: {back}");
133}
134
135fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> {
136 let started = Instant::now();
137 while !handle.is_finished() {
138 assert!(
139 started.elapsed() < limit,
140 "export did not finish in {limit:?}"
141 );
142 sleep(Duration::from_millis(20));
143 }
144 handle.result().expect("a finished export has an outcome")
145}
146
147/// The export half, decoded back off disk: the same picture the renderer
148/// showed, graded span for graded span. Software encoder, so it needs nothing
149/// installed -- the same pin `tests/export.rs`'s default suite uses.
150#[test]
151fn an_exported_grade_is_the_one_that_was_watched() {
152 engine::envcfg::set("VE_SW", "1");
153 engine::envcfg::set("VE_SW_ENC", "1");
154 let mut session = graded();
155 session.pause();
156 let out = Scratch::file("ve_color", "mp4");
157 let handle = session.export_to_with(&out, &ExportSettings::default());
158 wait(&handle, Duration::from_secs(180)).expect("export");
159
160 let (_, frames) = DecodeSession::open(&out).expect("decode the export");
161 let mut seen = 0;
162 for frame in frames {
163 let spread = spread(&frame.bgra);
164 if frame.index < CUT {
165 assert!(
166 spread <= 4.0,
167 "exported frame {} is inside the graded clip and kept its colour: {spread}",
168 frame.index
169 );
170 } else {
171 assert!(
172 spread > 100.0,
173 "exported frame {} is outside the graded clip and lost its colour: {spread}",
174 frame.index
175 );
176 }
177 seen += 1;
178 }
179 assert!(seen > CUT, "the export is shorter than the graded clip");
180 let _ = std::fs::remove_file(&out);
181}