forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

ps.rs104 lines, 4.5 KiBHistoryRawDownload
1//! MPEG program streams -- `.mpg`, `.mpeg`, `.vob`, `.m2p`, a camcorder's `.mod`/`.tod`
2//! -- and the raw MPEG-1/2 video streams `.m1v`, `.m2v`, `.mpv`, over the family's own
3//! [`ec_mpegts::PsDemuxer`].
4//!
5//! A program stream is read by the very readers a transport stream is ([`super::ts`]):
6//! [`ts::TsDemuxer`](super::ts::TsDemuxer) for the picture, [`ts::TsAudio`](super::ts::TsAudio)
7//! for each sound track, the same time model (source time 0 is the first picture's earliest
8//! presentation time), the same refusal wording. What differs lives here: how a file is
9//! recognised, how its container is named, and what is said about a track that is refused.
10//!
11//! Picture: MPEG-1 and MPEG-2 video (the `ec-mpeg2` software seat, deinterlace honoured),
12//! H.264 and HEVC. Sound: MPEG audio layers I-III, AC-3 (also a DVD's), DVD LPCM at 16, 20
13//! and 24 bits. Refused by name, with the ffmpeg line: DTS, DVD subtitles (VobSub) and any
14//! other private-stream substream.
15//!
16//! **Known limits.** A raw `.m2v` has no index, so `ec-mpegts` reads the whole file once at
17//! open: the picture is opened on the thread that asks, so a UI that opens a clip should
18//! do it off its render thread (every open in the engine already is). 20 and 24 bit DVD LPCM
19//! may seek to the middle of a two-frame group (a click, not drift). A `.mod` is only taken for
20//! a program stream if its bytes are one: the tracker-module spelling is never claimed.
21
22use super::*;
23
24/// The extensions that name a program stream or a raw MPEG video stream outright.
25const OWN_EXTENSIONS: &[&str] = &["m1v", "m2v", "mpv", "vob", "m2p", "tod"];
26
27/// `.mpg` and `.mpeg` are also the names of bare MPEG audio, and `.mod` of a tracker module:
28/// only the content decides these.
29const SHARED_EXTENSIONS: &[&str] = &["mpg", "mpeg", "mod"];
30
31pub(crate) fn ps_error(e: ec_core::Error) -> crate::Error {
32 format!("MPEG program stream: {e}").into()
33}
34
35fn head_of(path: &Path, len: usize) -> Vec<u8> {
36 let mut head = vec![0u8; len];
37 let read = File::open(path)
38 .and_then(|mut f| f.read(&mut head))
39 .unwrap_or(0);
40 head.truncate(read);
41 head
42}
43
44fn extension(path: &Path) -> Option<String> {
45 path.extension()
46 .and_then(|e| e.to_str())
47 .map(str::to_ascii_lowercase)
48}
49
50/// Whether `path` is an MPEG program stream or a raw MPEG video stream: its extension
51/// where that is unambiguous, its bytes otherwise (any name at all, `.mpg`/`.mpeg`/`.mod`
52/// included). A container whose magic is Matroska's or an mp4's, or a transport stream, is
53/// never one.
54pub fn is_mpegps(path: &Path) -> bool {
55 if sniff_container(path).is_some() {
56 return false;
57 }
58 let ext = extension(path);
59 let ext = ext.as_deref();
60 if matches!(ext, Some("ts" | "mts" | "m2ts" | "m2t" | "tp" | "trp")) {
61 return false;
62 }
63 if ext.is_some_and(|e| OWN_EXTENSIONS.contains(&e)) {
64 return true;
65 }
66 let shared = ext.is_some_and(|e| SHARED_EXTENSIONS.contains(&e));
67 if !shared && (crate::is_audio(path) || crate::is_image(path)) {
68 return false;
69 }
70 let head = head_of(path, 8 * 1024);
71 ec_mpegts::is_mpegps(&head) || ec_mpegts::is_mpeg_video_es(&head)
72}
73
74/// Whether `path` is a transport stream, a program stream, an AVI or a raw DV file: the files [`ts`] reads.
75pub fn is_mpeg_system(path: &Path) -> bool {
76 ts::is_mpegts(path) || is_mpegps(path) || super::avi::is_avi(path) || super::dv::is_dv(path)
77}
78
79/// Whether an `.mpg`/`.mpeg` file carries a *picture*, so that it belongs on a video lane and
80/// not with the MPEG audio streams those spellings also name. Looks at the first 64 KiB for a
81/// program stream with a video PES (or a raw video stream); a file that does not exist, or is
82/// anything else, is not one.
83pub fn carries_picture(path: &Path) -> bool {
84 let head = head_of(path, 64 * 1024);
85 if ec_mpegts::is_mpeg_video_es(&head) {
86 return true;
87 }
88 ec_mpegts::is_mpegps(&head)
89 && head
90 .windows(4)
91 .any(|w| w[..3] == [0, 0, 1] && (0xE0..=0xEF).contains(&w[3]))
92}
93
94/// ffprobe's name for the container: `mpeg` for a program stream, `mpegvideo` for a raw
95/// video stream; the camcorder's and the disc's own spellings are kept.
96pub(crate) fn container_name(path: &Path, ext: Option<&str>) -> String {
97 match ext {
98 Some("vob" | "mod" | "tod" | "m2p") => ext.unwrap_or_default().into(),
99 _ => match ec_mpegts::is_mpegps(&head_of(path, 8 * 1024)) {
100 true => "mpeg".into(),
101 false => "mpegvideo".into(),
102 },
103 }
104}