mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:22:39 +00:00
fix(media): custom ftyp compatible brands and real ReferenceBlock deltas (#2486)
* fix(media): support custom ftyp compatible brands and real ReferenceBlock deltas * fix(media): satisfy clippy::chunks_exact_to_as_chunks in ftyp test
This commit is contained in:
@@ -28,11 +28,13 @@ pub struct WriterOptions {
|
||||
/// chapter track (`tref/chap` → disabled text track) and as a Nero `udta/chpl` list;
|
||||
/// [`FragmentedWriter`] writes only `chpl`. See [`crate::isobmff::chapters`] for the formats and limits.
|
||||
pub chapters: Vec<Chapter>,
|
||||
/// Additional compatible brands appended to the `ftyp` box (for example `*b"av01"`).
|
||||
pub compatible_brands: Vec<[u8; 4]>,
|
||||
}
|
||||
|
||||
impl WriterOptions {
|
||||
pub fn new(brand: Brand) -> Self {
|
||||
WriterOptions { brand, movie_timescale: 1000, metadata: Vec::new(), chapters: Vec::new() }
|
||||
WriterOptions { brand, movie_timescale: 1000, metadata: Vec::new(), chapters: Vec::new(), compatible_brands: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,20 +215,26 @@ fn rescale(v: u64, from: u32, to: u32) -> u64 {
|
||||
|
||||
const MATRIX: [i32; 9] = [0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x4000_0000];
|
||||
|
||||
fn write_ftyp(b: &mut BoxBuf, brand: Brand) {
|
||||
fn write_ftyp(b: &mut BoxBuf, brand: Brand, compatible_brands: &[[u8; 4]]) {
|
||||
let m = b.start(b"ftyp");
|
||||
match brand {
|
||||
let base: &[[u8; 4]] = match brand {
|
||||
Brand::Mp4 => {
|
||||
b.bytes(b"isom");
|
||||
b.u32(0x200);
|
||||
for c in [b"isom", b"iso2", b"avc1", b"mp41", b"mp42"] {
|
||||
b.bytes(c);
|
||||
}
|
||||
&[*b"isom", *b"iso2", *b"avc1", *b"mp41", *b"mp42"]
|
||||
}
|
||||
Brand::Mov => {
|
||||
b.bytes(b"qt ");
|
||||
b.u32(0x200);
|
||||
b.bytes(b"qt ");
|
||||
&[*b"qt "]
|
||||
}
|
||||
};
|
||||
for c in base {
|
||||
b.bytes(c);
|
||||
}
|
||||
for c in compatible_brands {
|
||||
if !base.contains(c) {
|
||||
b.bytes(c);
|
||||
}
|
||||
}
|
||||
b.end(m);
|
||||
@@ -645,7 +653,7 @@ impl<W: Write + Seek> Mp4Writer<W> {
|
||||
pub fn new(mut w: W, opts: WriterOptions) -> Result<Self> {
|
||||
let start = w.stream_position()?;
|
||||
let mut b = BoxBuf::new();
|
||||
write_ftyp(&mut b, opts.brand);
|
||||
write_ftyp(&mut b, opts.brand, &opts.compatible_brands);
|
||||
let pad = b.len() as u64;
|
||||
b.leaf(if opts.brand == Brand::Mov { b"wide" } else { b"free" }, &[]);
|
||||
let mdat = b.len() as u64;
|
||||
@@ -977,7 +985,7 @@ impl<W: Write> FragmentedWriter<W> {
|
||||
let qt = self.opts.brand == Brand::Mov;
|
||||
let ts = self.opts.movie_timescale.max(1);
|
||||
let mut b = BoxBuf::new();
|
||||
write_ftyp(&mut b, self.opts.brand);
|
||||
write_ftyp(&mut b, self.opts.brand, &self.opts.compatible_brands);
|
||||
let m = b.start(b"moov");
|
||||
write_mvhd(&mut b, ts, 0, self.tracks.len() as u32 + 1);
|
||||
for t in &self.tracks {
|
||||
@@ -1478,4 +1486,20 @@ mod tests {
|
||||
assert_eq!(f.read_sample(plain.as_slice(), 1, 0).unwrap(), 7u32.to_be_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ftyp_appends_custom_compatible_brands_without_duplicates() {
|
||||
let mut opts = WriterOptions::new(Brand::Mp4);
|
||||
opts.compatible_brands = vec![*b"av01", *b"isom", *b"av01"];
|
||||
let mut w = Mp4Writer::new(Cursor::new(Vec::new()), opts).unwrap();
|
||||
let v = w.add_track(TrackConfig::new(avc_entry(), 1000)).unwrap();
|
||||
w.write_sample(v, WriteSample { data: &[1; 4], duration: 40, composition_offset: 0, is_sync: true }).unwrap();
|
||||
let out = w.finish().unwrap().into_inner();
|
||||
let size = u32::from_be_bytes(out[0..4].try_into().unwrap()) as usize;
|
||||
assert_eq!(&out[4..8], b"ftyp");
|
||||
let (brands, rest) = out[16..size].as_chunks::<4>();
|
||||
assert!(rest.is_empty());
|
||||
assert!(brands.contains(b"av01"), "missing av01 in {brands:?}");
|
||||
assert_eq!(brands.iter().filter(|&&b| b == *b"isom").count(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,20 @@ pub(crate) fn el_uint(out: &mut Vec<u8>, id: u32, v: u64) {
|
||||
el(out, id, &v.to_be_bytes()[8 - n..]);
|
||||
}
|
||||
|
||||
pub(crate) fn el_sint(out: &mut Vec<u8>, id: u32, v: i64) {
|
||||
let bytes = v.to_be_bytes();
|
||||
let mut start = 0;
|
||||
while start < 7 {
|
||||
let sign_bit = (bytes[start + 1] & 0x80) != 0;
|
||||
if (bytes[start] == 0x00 && !sign_bit) || (bytes[start] == 0xFF && sign_bit) {
|
||||
start += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
el(out, id, &bytes[start..]);
|
||||
}
|
||||
|
||||
pub(crate) fn el_float(out: &mut Vec<u8>, id: u32, v: f64) {
|
||||
el(out, id, &v.to_be_bytes());
|
||||
}
|
||||
@@ -198,6 +212,7 @@ pub struct MkvWriter<W: Write + Seek> {
|
||||
/// The cluster number in which each track last had a cue. Starting a cluster bumps
|
||||
/// `cluster_number` instead of clearing an entry for every declared track.
|
||||
track_cued_in: Vec<u64>,
|
||||
last_ts: Vec<Option<i64>>,
|
||||
cluster_number: u64,
|
||||
end_ns: i64,
|
||||
has_video: bool,
|
||||
@@ -321,6 +336,7 @@ impl<W: Write + Seek> MkvWriter<W> {
|
||||
pending: None,
|
||||
cues: Vec::new(),
|
||||
track_cued_in: vec![u64::MAX; n],
|
||||
last_ts: vec![None; n],
|
||||
cluster_number: 0,
|
||||
end_ns: 0,
|
||||
has_video,
|
||||
@@ -386,6 +402,7 @@ impl<W: Write + Seek> MkvWriter<W> {
|
||||
Some(p) => p.frames.push(data.to_vec()),
|
||||
None => self.pending = Some(Pending { track, ts: ts - cts, frames: vec![data.to_vec()] }),
|
||||
}
|
||||
self.last_ts[track] = Some(ts);
|
||||
return Ok(());
|
||||
}
|
||||
self.flush_pending();
|
||||
@@ -399,7 +416,8 @@ impl<W: Write + Seek> MkvWriter<W> {
|
||||
el(&mut g, BLOCK, &block);
|
||||
el_uint(&mut g, BLOCK_DURATION, ((u128::from(d) + scale as u128 / 2) / scale as u128) as u64);
|
||||
if !keyframe {
|
||||
el(&mut g, REFERENCE_BLOCK, &[0xFF]);
|
||||
let ref_delta = self.last_ts[track].map(|prev| prev - ts).unwrap_or(0);
|
||||
el_sint(&mut g, REFERENCE_BLOCK, ref_delta);
|
||||
}
|
||||
el(&mut self.cluster, BLOCK_GROUP, &g);
|
||||
} else {
|
||||
@@ -407,6 +425,7 @@ impl<W: Write + Seek> MkvWriter<W> {
|
||||
block.extend_from_slice(data);
|
||||
el(&mut self.cluster, SIMPLE_BLOCK, &block);
|
||||
}
|
||||
self.last_ts[track] = Some(ts);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -723,4 +742,24 @@ mod tests {
|
||||
seen.dedup();
|
||||
assert_eq!(seen.len(), 12, "{:?}", m.cues);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependent_block_group_writes_actual_reference_block_delta() {
|
||||
let mut v = TrackSpec::new(TrackKind::Video, "V_AV1");
|
||||
v.video_size = Some((16, 16));
|
||||
v.default_duration_ns = Some(33_333_333);
|
||||
let mut m = MkvWriter::new(Cursor::new(Vec::new()), vec![v], MuxOptions::default()).unwrap();
|
||||
m.write_frame(0, 0, true, &[1; 4], Some(33_000_000)).unwrap();
|
||||
m.write_frame(0, 33_000_000, true, &[2; 4], Some(33_000_000)).unwrap();
|
||||
m.write_frame(0, 66_000_000, false, &[3; 4], Some(67_000_000)).unwrap();
|
||||
let bytes = m.finish().unwrap().into_inner();
|
||||
// ReferenceBlock (0xFB, len 1 = 0x81, value -33i8 = 0xDF).
|
||||
assert!(bytes.windows(3).any(|w| w == [0xFB, 0x81, (-33i8) as u8]));
|
||||
let mut d = Demuxer::from_slice(&bytes).unwrap();
|
||||
let mut frames = Vec::new();
|
||||
while let Some(p) = d.next_packet().unwrap() {
|
||||
frames.push((p.pts_ns, p.keyframe, p.duration));
|
||||
}
|
||||
assert_eq!(frames, vec![(0, true, 33), (33_000_000, true, 33), (66_000_000, false, 67)]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user