forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

hevc_rext.rs131 lines, 5.0 KiBHistoryRawDownload
1//! HEVC range extensions on the software seat: 4:0:0, 4:2:2 and 4:4:4 at 8, 10
2//! and 12 bits, made by ffmpeg's libx265 into the temp dir (never personal
3//! media). Each opens, probes with its real chroma and depth, plays every frame
4//! in order, seeks onto the bytes a linear decode gave, and exports.
5//!
6//! The plane-for-plane PSNR against ffmpeg lives beside the seat, in
7//! `decode/hevc.rs`.
8//!
9//! ```text
10//! cargo test -p engine --release --test hevc_rext
11//! ```
12
13use std::path::Path;
14use std::process::Command;
15use std::sync::Mutex;
16use std::time::{Duration, Instant};
17
18use engine::PlaybackSession;
19use engine::colorspace::Chroma;
20use engine::decode::{DecodeSession, probe};
21use engine::demux::{Codec, Demuxer};
22use engine::export::ExportSettings;
23use engine::scratch::Scratch;
24
25const FRAMES: u32 = 30;
26
27/// (pix_fmt, chroma, luma bit depth)
28const SHAPES: [(&str, Chroma, u8); 8] = [
29 ("yuv444p", Chroma::C444, 8),
30 ("yuv444p10le", Chroma::C444, 10),
31 ("yuv444p12le", Chroma::C444, 12),
32 ("yuv422p12le", Chroma::C422, 12),
33 ("yuv422p10le", Chroma::C422, 10),
34 ("yuv422p", Chroma::C422, 8),
35 ("gray", Chroma::Gray, 8),
36 ("gray10le", Chroma::Gray, 10),
37];
38
39static SW: Mutex<()> = Mutex::new(());
40
41fn software<T>(f: impl FnOnce() -> T) -> T {
42 let _guard = SW.lock().unwrap_or_else(|p| p.into_inner());
43 engine::envcfg::set("VE_SW", "1");
44 let out = f();
45 engine::envcfg::unset("VE_SW");
46 out
47}
48
49/// 30 frames of 352x288 with a sync point every ten.
50fn make_clip(pix_fmt: &str) -> Scratch {
51 let clip = Scratch::file(&format!("hevc_rext_{pix_fmt}"), "mp4");
52 let out = Command::new("ffmpeg")
53 .args(["-v", "error", "-y", "-f", "lavfi", "-i"])
54 .arg("testsrc2=size=352x288:rate=30:duration=1")
55 .args(["-frames:v", "30", "-c:v", "libx265", "-pix_fmt", pix_fmt])
56 .args(["-x265-params", "log-level=error:keyint=10:min-keyint=10"])
57 .arg(&*clip)
58 .output()
59 .expect("run ffmpeg");
60 assert!(out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr));
61 clip
62}
63
64#[test]
65fn every_rext_shape_opens_and_probes_with_its_real_chroma_and_depth() {
66 for (pix, chroma, depth) in SHAPES {
67 let clip = make_clip(pix);
68 let (meta, demuxer) = Demuxer::open(&clip).unwrap_or_else(|e| panic!("{pix}: {e}"));
69 assert_eq!(meta.codec, Codec::Hevc, "{pix}");
70 assert_eq!(demuxer.chroma(), Some(chroma), "{pix}");
71 assert_eq!(demuxer.bit_depth(), depth, "{pix}");
72 probe(&clip).unwrap_or_else(|e| panic!("{pix}: probe refused: {e}"));
73 // Hardware cannot decode these profiles, so the seat is software whatever
74 // the plugin is, and the span still delivers.
75 let (_, rx) = DecodeSession::open(&clip).unwrap_or_else(|e| panic!("{pix}: {e}"));
76 assert_eq!(rx.into_iter().count(), FRAMES as usize, "{pix}: unpinned seat decodes");
77 }
78}
79
80#[test]
81fn rext_streams_play_in_order_and_seeks_match_a_linear_decode() {
82 for (pix, _, _) in SHAPES {
83 let clip = make_clip(pix);
84 let frames: Vec<_> = software(|| {
85 let (_, rx) = DecodeSession::open(&clip).expect("open");
86 rx.into_iter().collect()
87 });
88 assert_eq!(
89 frames.iter().map(|f| f.index).collect::<Vec<_>>(),
90 (0..FRAMES).collect::<Vec<_>>(),
91 "{pix}: display order"
92 );
93 let first = &frames[0].bgra;
94 assert!(
95 first.as_chunks::<4>().0.iter().any(|px| px[..] != first[..4]),
96 "{pix}: frame 0 holds a picture"
97 );
98 for target in [0, 9, 10, 11, 20, FRAMES - 1] {
99 let seeked = software(|| {
100 let (_, rx, _cancel) = DecodeSession::open_at(&clip, target).expect("open_at");
101 rx.into_iter().next().expect("a frame after the seek")
102 });
103 assert_eq!(seeked.index, target, "{pix}");
104 assert!(
105 seeked.bgra == frames[target as usize].bgra,
106 "{pix}: seek to {target} differs from a linear decode"
107 );
108 }
109 }
110}
111
112#[test]
113fn rext_streams_export() {
114 for (pix, _, _) in SHAPES {
115 let clip = make_clip(pix);
116 let mut session = PlaybackSession::open(&*clip).unwrap_or_else(|e| panic!("{pix}: {e}"));
117 assert!(session.cut_at(5.0 / 30.0), "{pix}: cut");
118 assert!(session.delete_clip(engine::project::Lane::V1, 1));
119 let out = Scratch::file(&format!("hevc_rext_out_{pix}"), "mp4");
120 let handle = session.export_to_with(&out, &ExportSettings::default());
121 let started = Instant::now();
122 while !handle.is_finished() {
123 assert!(started.elapsed() < Duration::from_secs(300), "{pix}: export hung");
124 std::thread::sleep(Duration::from_millis(20));
125 }
126 handle.result().expect("an outcome").unwrap_or_else(|e| panic!("{pix}: export: {e}"));
127 let (meta, _) = Demuxer::open(Path::new(&*out)).expect("the export reopens");
128 assert_eq!(meta.frame_count, 5, "{pix}");
129 assert_eq!((meta.width, meta.height), (352, 288), "{pix}");
130 }
131}