forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

matrix_render.rs91 lines, 3.5 KiBHistoryRawDownload
1//! The stream's matrix reaching the picture a player shows, through the door
2//! the window itself opens ([`PlaybackSession`]) rather than through the
3//! converter alone.
4//!
5//! Two fixtures, one picture: `test_bt601.mkv` and `test_h264.mkv` are the same
6//! `testsrc2` at 1280x720 through the same encoder, and their first frames were
7//! byte-identical while every file was converted as BT.601 -- the FNV constant
8//! below is that shared frame, measured before this change. What separates them
9//! now is only what they *say*: the first tags SMPTE 170M in its container, the
10//! second tags nothing and is 720 lines, so the heuristic calls it BT.709.
11//!
12//! ```text
13//! cargo test -p engine --release --test matrix_render -- --test-threads=1
14//! ```
15
16use std::path::PathBuf;
17use std::time::{Duration, Instant};
18
19use engine::PlaybackSession;
20use engine::colorspace::Matrix;
21
22/// Frame 0 of either fixture as the engine converted it before it read a single
23/// colour tag: BT.601 limited, both files.
24const LEGACY_601_FRAME: u64 = 0x872e_4f9c_4165_0c9e;
25
26fn asset(name: &str) -> PathBuf {
27 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
28 .join("../../assets")
29 .join(name)
30}
31
32fn fnv(b: &[u8]) -> u64 {
33 b.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, &x| {
34 (h ^ u64::from(x)).wrapping_mul(0x100_0000_01b3)
35 })
36}
37
38/// The first frame of `name`, and the matrix the engine resolved for it. Pinned
39/// to the software decoder so the bytes are the same on a machine with a VA-API
40/// plugin and one without -- hence `--test-threads=1`.
41fn frame0(name: &str) -> (Matrix, Vec<u8>) {
42 unsafe { std::env::set_var("VE_SW", "1") };
43 let mut session = PlaybackSession::open(asset(name)).expect("open");
44 session.set_gain(0.0);
45 let matrix = session.meta().color.matrix;
46 let deadline = Instant::now() + Duration::from_secs(60);
47 loop {
48 if let Some(frame) = session.try_frame() {
49 assert_eq!(frame.index, 0, "{name}: not the first frame");
50 assert_eq!((frame.width, frame.height), (1280, 720), "{name}");
51 return (matrix, frame.bgra);
52 }
53 assert!(Instant::now() < deadline, "no frame for {name} in 60 s");
54 std::thread::sleep(Duration::from_millis(10));
55 }
56}
57
58#[test]
59fn the_streams_matrix_reaches_the_rendered_frame() {
60 let (sd_matrix, sd) = frame0("test_bt601.mkv");
61 let (hd_matrix, hd) = frame0("test_h264.mkv");
62 assert_eq!(sd_matrix, Matrix::Bt601, "the tagged fixture");
63 assert_eq!(hd_matrix, Matrix::Bt709, "the untagged 720-line fixture");
64
65 // The half that must not move: BT.601 content is the picture it always was,
66 // to the byte (`convert::bt601_limited_is_the_legacy_matrix` is why).
67 assert_eq!(fnv(&sd), LEGACY_601_FRAME, "the BT.601 fixture shifted");
68
69 // ...and the half that must: the same picture, read as HD, is not it.
70 assert_ne!(
71 fnv(&hd),
72 LEGACY_601_FRAME,
73 "the BT.709 stream is still being converted as BT.601"
74 );
75
76 // A matrix, not a mangling: every pixel moves a little and none of it moves
77 // far. (BT.709's red weight is 0.2126 against BT.601's 0.299, so the
78 // saturated bars of `testsrc2` are where the shift lands.)
79 let moved = sd.iter().zip(&hd).filter(|(a, b)| a != b).count();
80 let worst = sd
81 .iter()
82 .zip(&hd)
83 .map(|(a, b)| a.abs_diff(*b))
84 .max()
85 .expect("a frame");
86 assert!(
87 (0.05 * sd.len() as f64) < moved as f64 && worst <= 60,
88 "{moved} of {} bytes moved, worst by {worst}",
89 sd.len()
90 );
91}