| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use std::path::Path; |
| 14 | use std::process::Command; |
| 15 | use std::sync::Mutex; |
| 16 | use std::time::{Duration, Instant}; |
| 17 | |
| 18 | use engine::PlaybackSession; |
| 19 | use engine::colorspace::Chroma; |
| 20 | use engine::decode::{DecodeSession, probe}; |
| 21 | use engine::demux::{Codec, Demuxer}; |
| 22 | use engine::export::ExportSettings; |
| 23 | use engine::scratch::Scratch; |
| 24 | |
| 25 | const FRAMES: u32 = 30; |
| 26 | |
| 27 | |
| 28 | const 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 | |
| 39 | static SW: Mutex<()> = Mutex::new(()); |
| 40 | |
| 41 | fn 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 | |
| 50 | fn 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] |
| 65 | fn 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 | |
| 74 | |
| 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] |
| 81 | fn 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] |
| 113 | fn 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 | } |