#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrackCodec {
H264,
Hevc,
Vp9,
Av1,
}
#[derive(Debug, Clone, Copy)]
pub struct Sample {
pub offset: u64,
pub size: u32,
pub duration: u32,
pub dts: i64,
pub pts: i64,
pub sync: bool,
}
#[derive(Debug, Clone)]
pub struct VideoTrack {
pub codec: TrackCodec,
pub width: u32,
pub height: u32,
pub config: Vec<u8>,
pub timescale: u32,
pub samples: Vec<Sample>,
}
impl VideoTrack {
pub fn presentation_order(&self) -> Vec<usize> {
let mut idx: Vec<usize> = (0..self.samples.len()).collect();
idx.sort_by_key(|&i| self.samples[i].pts);
idx
}
pub fn max_reorder(&self) -> usize {
let order = self.presentation_order();
order
.iter()
.enumerate()
.map(|(shown, &decoded)| decoded.saturating_sub(shown))
.max()
.unwrap_or(0)
}
pub fn fps(&self) -> Option<f64> {
let ticks: u64 = self.samples.iter().map(|s| s.duration as u64).sum();
if ticks == 0 || self.samples.is_empty() {
return None;
}
Some(self.samples.len() as f64 * self.timescale as f64 / ticks as f64)
}
}
pub(crate) fn boxes<'a>(data: &'a [u8], mut f: impl FnMut(&[u8; 4], &'a [u8])) {
let mut off = 0usize;
while off + 8 <= data.len() {
let size32 = u32::from_be_bytes(data[off..off + 4].try_into().unwrap()) as usize;
let ty: [u8; 4] = data[off + 4..off + 8].try_into().unwrap();
let (header, size) = match size32 {
0 => (8, data.len() - off),
1 => {
if off + 16 > data.len() {
return;
}
let s = u64::from_be_bytes(data[off + 8..off + 16].try_into().unwrap()) as usize;
(16, s)
}
n => (8, n),
};
if size < header || off + size > data.len() {
return;
}
f(&ty, &data[off + header..off + size]);
off += size;
}
}
pub(crate) fn find<'a>(data: &'a [u8], want: &[u8; 4]) -> Option<&'a [u8]> {
let mut hit = None;
boxes(data, |ty, body| {
if ty == want && hit.is_none() {
hit = Some(body);
}
});
hit
}
pub(crate) fn u32at(b: &[u8], i: usize) -> u32 {
u32::from_be_bytes(b[i..i + 4].try_into().unwrap())
}
pub fn demux_video(data: &[u8]) -> Result<VideoTrack, String> {
if find(data, b"moof").is_some() {
return Err("fragmented MP4 (moof) is not supported".into());
}
let moov = find(data, b"moov").ok_or("no moov box — not an MP4?")?;
let mut video_stbl: Option<&[u8]> = None;
let mut timescale = 0u32;
boxes(moov, |ty, trak| {
if ty != b"trak" || video_stbl.is_some() {
return;
}
let Some(mdia) = find(trak, b"mdia") else { return };
let is_video = find(mdia, b"hdlr")
.map(|h| h.len() >= 12 && &h[8..12] == b"vide")
.unwrap_or(false);
if !is_video {
return;
}
if let Some(mdhd) = find(mdia, b"mdhd") {
timescale = match mdhd.first() {
Some(1) if mdhd.len() >= 24 => u32at(mdhd, 20),
_ if mdhd.len() >= 16 => u32at(mdhd, 12),
_ => 0,
};
}
video_stbl = find(mdia, b"minf").and_then(|m| find(m, b"stbl"));
});
let stbl = video_stbl.ok_or("no video track")?;
if timescale == 0 {
return Err("video track has no timescale".into());
}
let stsd = find(stbl, b"stsd").ok_or("no stsd")?;
if stsd.len() < 8 {
return Err("stsd too short".into());
}
if u32at(stsd, 4) != 1 {
return Err("more than one sample description is not supported".into());
}
let entry = &stsd[8..];
if entry.len() < 86 {
return Err("sample entry too short".into());
}
let format: [u8; 4] = entry[4..8].try_into().unwrap();
let codec = match &format {
b"avc1" | b"avc3" => TrackCodec::H264,
b"hvc1" | b"hev1" => TrackCodec::Hevc,
b"vp09" => TrackCodec::Vp9,
b"av01" => TrackCodec::Av1,
other => {
return Err(format!(
"unsupported sample format {:?} — only H.264, HEVC, VP9 and AV1",
String::from_utf8_lossy(other)
))
}
};
let width = u16::from_be_bytes(entry[32..34].try_into().unwrap()) as u32;
let height = u16::from_be_bytes(entry[34..36].try_into().unwrap()) as u32;
let want_cfg: &[u8; 4] = match codec {
TrackCodec::H264 => b"avcC",
TrackCodec::Hevc => b"hvcC",
TrackCodec::Vp9 => b"vpcC",
TrackCodec::Av1 => b"av1C",
};
let config = find(&entry[86..], want_cfg)
.ok_or_else(|| format!("no {} in the sample entry", String::from_utf8_lossy(want_cfg)))?
.to_vec();
let stts = find(stbl, b"stts").ok_or("no stts")?;
let mut durations: Vec<u32> = Vec::new();
if stts.len() >= 8 {
let n = u32at(stts, 4) as usize;
for i in 0..n {
let o = 8 + i * 8;
if o + 8 > stts.len() {
break;
}
let count = u32at(stts, o);
let delta = u32at(stts, o + 4);
durations.extend(std::iter::repeat_n(delta, count as usize));
}
}
let mut offsets: Vec<i32> = Vec::new();
if let Some(ctts) = find(stbl, b"ctts") {
if ctts.len() >= 8 {
let n = u32at(ctts, 4) as usize;
for i in 0..n {
let o = 8 + i * 8;
if o + 8 > ctts.len() {
break;
}
let count = u32at(ctts, o) as usize;
let off = u32at(ctts, o + 4) as i32;
offsets.extend(std::iter::repeat_n(off, count));
}
}
}
let stsz = find(stbl, b"stsz").ok_or("no stsz")?;
if stsz.len() < 12 {
return Err("stsz too short".into());
}
let uniform = u32at(stsz, 4);
let count = u32at(stsz, 8) as usize;
let sizes: Vec<u32> = if uniform != 0 {
vec![uniform; count]
} else {
(0..count)
.map(|i| {
let o = 12 + i * 4;
if o + 4 <= stsz.len() {
u32at(stsz, o)
} else {
0
}
})
.collect()
};
let stsc = find(stbl, b"stsc").ok_or("no stsc")?;
let mut runs: Vec<(u32, u32)> = Vec::new(); if stsc.len() >= 8 {
let n = u32at(stsc, 4) as usize;
for i in 0..n {
let o = 8 + i * 12;
if o + 12 > stsc.len() {
break;
}
runs.push((u32at(stsc, o), u32at(stsc, o + 4)));
}
}
let chunk_offsets: Vec<u64> = if let Some(co64) = find(stbl, b"co64") {
let n = u32at(co64, 4) as usize;
(0..n)
.map(|i| {
let o = 8 + i * 8;
if o + 8 <= co64.len() {
u64::from_be_bytes(co64[o..o + 8].try_into().unwrap())
} else {
0
}
})
.collect()
} else {
let stco = find(stbl, b"stco").ok_or("no stco or co64")?;
let n = u32at(stco, 4) as usize;
(0..n)
.map(|i| {
let o = 8 + i * 4;
if o + 4 <= stco.len() {
u32at(stco, o) as u64
} else {
0
}
})
.collect()
};
let sync: Option<std::collections::HashSet<u32>> = find(stbl, b"stss").map(|stss| {
let n = if stss.len() >= 8 { u32at(stss, 4) as usize } else { 0 };
(0..n)
.filter_map(|i| {
let o = 8 + i * 4;
(o + 4 <= stss.len()).then(|| u32at(stss, o))
})
.collect()
});
let mut samples = Vec::with_capacity(sizes.len());
let mut idx = 0usize;
let mut dts = 0i64;
for (ci, &chunk_off) in chunk_offsets.iter().enumerate() {
let chunk = ci as u32 + 1;
let per = runs
.iter()
.rev()
.find(|(first, _)| *first <= chunk)
.map(|(_, n)| *n)
.unwrap_or(0);
let mut at = chunk_off;
for _ in 0..per {
if idx >= sizes.len() {
break;
}
let duration = durations.get(idx).copied().unwrap_or(0);
samples.push(Sample {
offset: at,
size: sizes[idx],
duration,
dts,
pts: dts + offsets.get(idx).copied().unwrap_or(0) as i64,
sync: sync
.as_ref()
.map(|s| s.contains(&(idx as u32 + 1)))
.unwrap_or(true),
});
at += sizes[idx] as u64;
dts += duration as i64;
idx += 1;
}
}
if samples.is_empty() {
return Err("the video track has no samples".into());
}
Ok(VideoTrack {
codec,
width,
height,
config,
timescale,
samples,
})
}