forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

dv.rs128 lines, 4.6 KiBHistoryRawDownload
1//! Raw DV (`.dv`, `.dif`) over the family's own [`ec_dv::DvDemuxer`], read by the very readers a
2//! transport stream is ([`super::ts`]): [`ts::TsDemuxer`](super::ts::TsDemuxer) for the picture,
3//! [`ts::TsAudio`](super::ts::TsAudio) for each sound track -- the way [`super::avi`] is.
4//!
5//! [`DvSrc`] is the adapter: it presents the file as the 90 kHz packet stream the readers expect
6//! (every stream's time base rescaled to ticks, a seek rescaled back), and its streams as the
7//! [`ec_mpegts::TrackInfo`] list `ts::describe_open` walks. Source time 0 is frame 0.
8//!
9//! **Picture.** One DV frame per packet (DV25 NTSC and PAL, DVCPRO25/50, DVCPRO HD), every frame a
10//! sync sample. **Sound.** The PCM in the audio DIF blocks, one 16-bit stereo stream per pair
11//! the first frame's audio source pack announces (12-bit non-linear already expanded).
12
13use std::io::BufReader;
14
15use ec_core::registry::SeekMode;
16use ec_core::timebase::{Rounding, TimeBase, Timestamp};
17use ec_core::{Packet, StreamInfo};
18use ec_dv::DvDemuxer;
19use ec_mpegts::{StreamKind as TsKind, TrackInfo};
20
21use super::*;
22
23type Reader = BufReader<File>;
24
25/// The ticks a second is cut into, as [`super::ts`] counts.
26fn tb() -> TimeBase {
27 TimeBase::new(1, 90_000)
28}
29
30/// Whether `path` is a raw DV file: its extension (`.dv`, `.dif`), or -- for a name that says
31/// nothing -- the DIF section headers in its first bytes. A container whose magic is Matroska's
32/// or an mp4's is never one, nor is a RIFF file (an AVI holds DV *inside*), a standalone sound
33/// file or a still.
34pub fn is_dv(path: &Path) -> bool {
35 if sniff_container(path).is_some() {
36 return false;
37 }
38 let named = path
39 .extension()
40 .and_then(|e| e.to_str())
41 .is_some_and(|e| matches!(e.to_ascii_lowercase().as_str(), "dv" | "dif"));
42 if !named && (crate::is_audio(path) || crate::is_image(path)) {
43 return false;
44 }
45 let mut head = vec![0u8; 64 << 10];
46 let read = File::open(path).and_then(|mut f| f.read(&mut head)).unwrap_or(0);
47 let head = &head[..read];
48 if head.starts_with(b"RIFF") {
49 return false;
50 }
51 ec_dv::is_dv(head)
52}
53
54pub(crate) fn dv_error(e: ec_core::Error) -> crate::Error {
55 format!("DV: {e}").into()
56}
57
58/// A raw DV file as the packet stream [`super::ts::Src`] speaks.
59pub(crate) struct DvSrc {
60 d: DvDemuxer<Reader>,
61 /// The streams in 90 kHz ticks.
62 streams: Vec<StreamInfo>,
63 /// Each stream's own time base, by stream index.
64 native: Vec<TimeBase>,
65}
66
67impl DvSrc {
68 pub(crate) fn open(path: &Path) -> crate::Result<Self> {
69 let file = File::open(path)?;
70 let d = DvDemuxer::new(BufReader::with_capacity(256 << 10, file)).map_err(dv_error)?;
71 let mut native = Vec::new();
72 let streams = ec_core::Demuxer::streams(&d)
73 .iter()
74 .map(|s| {
75 native.push(s.time_base);
76 let mut s = s.clone();
77 let to = |t: i64| Timestamp::new(t, s.time_base).rescale(tb(), Rounding::Nearest).ticks;
78 s.start_time = s.start_time.map(to);
79 s.duration = s.duration.map(to);
80 s.time_base = tb();
81 s
82 })
83 .collect();
84 Ok(Self { d, streams, native })
85 }
86
87 pub(crate) fn streams(&self) -> &[StreamInfo] {
88 &self.streams
89 }
90
91 pub(crate) fn tracks(&self) -> Vec<TrackInfo> {
92 self.streams
93 .iter()
94 .map(|s| TrackInfo {
95 pid: s.index as u16,
96 stream_type: 0,
97 kind: TsKind::Other(if s.index == 0 { "dvvideo".into() } else { "pcm_s16le".into() }),
98 stream: Some(s.index),
99 refusal: None,
100 language: None,
101 })
102 .collect()
103 }
104
105 pub(crate) fn next_packet(&mut self) -> ec_core::Result<Packet> {
106 let mut p = ec_core::Demuxer::next_packet(&mut self.d)?;
107 let from = self.native.get(p.stream as usize).copied().unwrap_or(p.time_base);
108 let to = |t: i64| Timestamp::new(t, from).rescale(tb(), Rounding::Nearest).ticks;
109 p.pts = p.pts.map(to);
110 p.dts = p.dts.map(to);
111 p.duration = p.duration.map(to);
112 p.time_base = tb();
113 Ok(p)
114 }
115
116 pub(crate) fn seek(&mut self, stream: u32, to: Timestamp, mode: SeekMode) -> ec_core::Result<()> {
117 let at = to.rescale(self.native.get(stream as usize).copied().unwrap_or(to.base), Rounding::Nearest);
118 ec_core::Demuxer::seek(&mut self.d, stream, at, mode)
119 }
120
121 pub(crate) fn duration_secs(&self) -> Option<f64> {
122 self.streams
123 .iter()
124 .filter_map(|s| s.duration)
125 .max()
126 .map(|t| t as f64 / 90_000.0)
127 }
128}