diff --git a/crates/matroska/README.md b/crates/matroska/README.md new file mode 100644 index 00000000..a4805b6f --- /dev/null +++ b/crates/matroska/README.md @@ -0,0 +1,115 @@ +# filmcraft-matroska + +A clean-room Matroska (MKV) and WebM demuxer, plus a minimal muxer, written from the public +specifications: RFC 8794 (EBML), RFC 9559 (Matroska), the Matroska codec mappings and the WebM +container guidelines. Layer L0: it depends only on `std`, has no `unsafe`, and builds for +`wasm32-unknown-unknown`. No GPL/LGPL code (FFmpeg, libmatroska, mkvtoolnix) was consulted; +FFmpeg is used only as an external test oracle. + +## Demuxing + +```rust +use filmcraft_matroska::{Demuxer, TrackKind, open}; + +// Sample tables, same shape as filmcraft-isobmff (any ByteSource: &[u8], Vec, Arc<[u8]>, File…) +let file = std::fs::File::open("clip.mkv")?; +let mkv = open(&file)?; +let v = mkv.track_of_kind(TrackKind::Video).unwrap(); +let k = mkv.keyframe_before(v, 2_000_000_000).unwrap(); // sample index +let frame = mkv.read_sample(&file, v, k)?; + +// Streaming packets + seeking (from a ByteSource, any Read + Seek, or a byte slice) +let mut d = Demuxer::from_reader(std::io::BufReader::new(std::fs::File::open("clip.webm")?))?; +let sp = d.seek(v, 2_000_000_000)?; // lands on the keyframe with pts ≤ 2 s +while let Some(p) = d.next_packet()? { // file order, all tracks + // p.track, p.pts (ticks), p.pts_ns, p.duration, p.keyframe, p.data +} +``` + +- **Open** (`open`, `open_with(src, &OpenOptions { index, verify_crc })`): EBML header (DocType + `matroska`/`webm`), the first Segment, and its level-1 elements: SeekHead (followed for elements + after the clusters), Info, Tracks, Cues, Chapters, Tags, Attachments (listed; payload located by + offset/size, never read). With `index: true` (default) every Cluster is scanned — element and block + headers only, never frame payloads — to build per-track `Sample` tables (`offset`, `size`, `pts`, + `duration`, `keyframe`, cluster, block offset, lace index) and keyframe lists. With `index: false` + open stops at the first Cluster. +- **Timestamps.** Matroska ticks are `TimestampScale` ns (usually 1 ms); `Track::timebase` is that + as a reduced fraction of a second. `pts` is in ticks, `pts_ns` in exact nanoseconds. `CodecDelay` + is subtracted (RFC 9559), as FFmpeg does (Opus/Vorbis priming gives negative pts). Laced frames get + `block ts + i × (BlockDuration or DefaultDuration)/n`; if neither is known, later laces repeat the + block timestamp and have `duration == 0` (`Packet::lace` tells which). +- **Keyframes:** SimpleBlock keyframe flag; BlockGroup = keyframe iff it has no ReferenceBlock. +- **Lacing:** Xiph, fixed-size, EBML. **Unknown sizes:** Segment (live/streamed output) and + Clusters end at the next top-level element. **CRC-32:** optional verification of level-1 + elements and clusters; mismatches go to `MkvFile::crc_errors` (not fatal). **Void** skipped. +- **Damage:** undecodable/oversized elements trigger a byte scan for the next plausible level-1 + element (Cluster whose first child is Timestamp, or a sized metadata element); a truncated final + block is dropped. Problems are listed in `MkvFile::warnings`. +- **Seeking** (`Demuxer::seek(track, time_ns)`): uses the sample index when present; otherwise Cues + (refined by scanning the cued cluster for the exact keyframe); otherwise builds the index by + scanning all clusters. `Demuxer::keyframe_index(track)` returns the keyframe list. +- **Tracks:** number/UID/type/flags, `DefaultDuration`, name, language (+BCP 47), CodecID, + CodecPrivate, CodecName, `CodecDelay`, `SeekPreRoll`; `VideoInfo` (pixel/display size, crop, + interlacing, stereo, alpha mode, `pixel_aspect()`), `Colour` (matrix, range, transfer, primaries, + subsampling/siting, MaxCLL/MaxFALL, SMPTE 2086 mastering metadata); `AudioInfo` (rate, output + rate, channels, bit depth); `ContentEncoding`s. +- **Content encodings:** header stripping is undone in every packet / `read_sample`. zlib/bzlib/ + lzo compression and encryption are reported (`Track::frames_readable() == false`); packets are + then returned as stored and `read_sample` fails with `Unsupported`. + +### Codec mapping (`Codec`) + +| CodecID | `Codec` | payload | +|---|---|---| +| `V_MPEG4/ISO/AVC` | `Avc` | avcC (CodecPrivate) | +| `V_MPEGH/ISO/HEVC` | `Hevc` | hvcC | +| `V_VP8`, `V_VP9` | `Vp8`, `Vp9` | VP9 CodecPrivate features | +| `V_AV1` | `Av1` | av1C | +| `V_PRORES` | `ProRes` | FourCC; frames get the 8-byte `size`+`icpf` header re-added | +| `V_MJPEG`, `V_MS/VFW/FOURCC` (MJPG) | `Mjpeg` | | +| `V_MS/VFW/FOURCC` (other) | `VfwFourcc` | BITMAPINFOHEADER | +| `A_AAC`, legacy `A_AAC/MPEG{2,4}/…` | `Aac` | AudioSpecificConfig (synthesised for legacy IDs, incl. SBR) | +| `A_OPUS` | `Opus` | OpusHead | +| `A_VORBIS` | `Vorbis` | three headers split from Xiph-laced CodecPrivate | +| `A_FLAC` | `Flac` | `fLaC` + metadata blocks | +| `A_PCM/INT/LIT`, `A_PCM/INT/BIG`, `A_PCM/FLOAT/IEEE` | `Pcm` | bit depth from Audio | +| `A_AC3`, `A_EAC3`, `A_MPEG/L3`, `A_MPEG/L2` | `Ac3`, `Eac3`, `Mp3`, `Mp2` | | +| `S_TEXT/UTF8` | `SubRip` | | +| `S_TEXT/WEBVTT`, `D_WEBVTT/*` (WebM) | `WebVtt` | WebM blocks keep `id\nsettings\npayload` | +| `S_TEXT/ASS`, `S_TEXT/SSA` | `Ass` | script header | +| anything else | `Other(id)` | `Track::codec_private` | + +`Codec::name()` matches FFmpeg's `codec_name` for these. + +## Muxing (minimal) + +`MkvWriter::new(w: Write + Seek, Vec, MuxOptions)` → `write_frame(track, pts_ns, +keyframe, data, duration_ns)` → `finish()`. Writes the EBML header, Segment, SeekHead, Info (with +Duration), Tracks, Clusters of SimpleBlocks (new cluster at a video keyframe after +`cluster_duration_ns`), BlockGroups for explicit durations, optional audio lacing (Xiph, EBML, +fixed-size), and Cues (video keyframes, or the first frame of each track per cluster). No +CRC-32, Chapters, Tags, Attachments, or content encodings. + +## Tests + +- Unit: EBML VINTs (known/unknown sizes, IDs, signed lace deltas), typed values, CRC-32, all lacing + modes, codec mapping, a hand-built file (unknown-size Segment/Clusters, header stripping, + BlockGroups, Colour + mastering metadata, Chapters, Tags, Attachments, CRC-32 pass/fail, Cue-less + seek), and mux → demux round trips for every lacing mode. +- Oracle (`tests/oracle.rs`, skipped without ffmpeg/ffprobe; fixtures in `target/fixtures/matroska`): + H.264+AAC, VP9+Opus WebM, FLAC, Vorbis, SRT+ASS subtitles, WebVTT WebM, Cues at front, a + streamed (pipe) file without Cues, HEVC with HDR colour, ProRes+PCM, MJPEG+AC-3, AV1. Every packet + is compared with `ffprobe -show_packets` (stream, size, keyframe flag, pts, duration), plus codec + names, time bases, dimensions, audio params and duration; the sample index must equal the packet + stream. Seeks (indexed, Cues-only, and index-on-demand) must land on the latest keyframe ≤ target. + Our muxer's laced output (Xiph/EBML/fixed) is read back by ffprobe and compared the same way. + A damaged file must resync and deliver the packets after the damage; truncated files must not fail. + +## Limitations + +- Only the first Segment is read (no linked/chained segments, no ordered-chapter editions playback). +- zlib/bzlib/lzo compressed and encrypted tracks are detected but their frames are not decoded. +- BlockAdditions (e.g. VP9 alpha, WebVTT settings in Matroska) are skipped; `EncryptedBlock` ignored. +- `TrackTimestampScale` (deprecated) is ignored. +- Lace timestamps without any duration information repeat the block timestamp (FFmpeg derives them + from codec parsers). diff --git a/crates/matroska/src/codec.rs b/crates/matroska/src/codec.rs index 75b7a7e2..e44f47ea 100644 --- a/crates/matroska/src/codec.rs +++ b/crates/matroska/src/codec.rs @@ -45,7 +45,7 @@ pub enum Codec { Mp2, /// `S_TEXT/UTF8` (SubRip-style plain text). SubRip, - /// `S_TEXT/WEBVTT`. + /// `S_TEXT/WEBVTT`, or WebM's `D_WEBVTT/{SUBTITLES,CAPTIONS,DESCRIPTIONS,METADATA}`. WebVtt, /// `S_TEXT/ASS` / `S_TEXT/SSA`; `header` is the script header from `CodecPrivate`. Ass { header: Vec, ssa: bool }, @@ -169,7 +169,7 @@ pub(crate) fn map_codec(id: &str, private: &[u8], audio: Option<&crate::AudioInf "A_MPEG/L3" => Codec::Mp3, "A_MPEG/L2" => Codec::Mp2, "S_TEXT/UTF8" => Codec::SubRip, - "S_TEXT/WEBVTT" => Codec::WebVtt, + "S_TEXT/WEBVTT" | "D_WEBVTT/SUBTITLES" | "D_WEBVTT/CAPTIONS" | "D_WEBVTT/DESCRIPTIONS" | "D_WEBVTT/METADATA" => Codec::WebVtt, "S_TEXT/ASS" | "S_ASS" => Codec::Ass { header: p, ssa: false }, "S_TEXT/SSA" | "S_SSA" => Codec::Ass { header: p, ssa: true }, _ if id.starts_with("A_AAC/") => { diff --git a/crates/matroska/src/demux.rs b/crates/matroska/src/demux.rs index b4e03cde..808b45b7 100644 --- a/crates/matroska/src/demux.rs +++ b/crates/matroska/src/demux.rs @@ -91,6 +91,10 @@ pub struct Packet { pub discard_padding_ns: Option, /// Absolute offset of the stored frame data. pub offset: u64, + /// Index of the frame within a laced block (0 when not laced). Laced frames after the first + /// only have exact timestamps when the block or track carries a duration; otherwise they + /// repeat the block timestamp and `duration` is 0. + pub lace: u16, /// Frame bytes with header stripping undone. Frames of tracks with other content encodings /// (zlib, encryption) are returned as stored; see [`Track::frames_readable`]. pub data: Vec, @@ -419,19 +423,24 @@ fn expand(b: &BlockRef, t: &Track, scale: u64, skip: u16, out: &mut impl Extend< let scale_i = scale as i128; // round half away from zero let to_ticks = |ns: i128| -> i64 { (if ns >= 0 { (ns + scale_i / 2) / scale_i } else { (ns - scale_i / 2) / scale_i }) as i64 }; - let base_ns = b.ts as i128 * scale_i - t.codec_delay_ns as i128; + // CodecDelay is subtracted from every timestamp (RFC 9559 §5.1.4.1.25); in ticks it is rounded + // once, so tick timestamps stay on the block grid. + let delay_ns = t.codec_delay_ns as i128; + let delay_ticks = to_ticks(delay_ns); + let base_ns = b.ts as i128 * scale_i; out.extend(b.frames.iter().enumerate().skip(skip as usize).map(|(i, &(offset, size))| { let (start, end) = match block_ns { Some(total) => ((total * i as u128 / n as u128) as i128, Some((total * (i as u128 + 1) / n as u128) as i128)), None => (0, None), }; - let pts_ns = base_ns + start; + let pts_ns = base_ns + start - delay_ns; + let pts = to_ticks(base_ns + start) - delay_ticks; let duration = end.map_or(0, |e| (to_ticks(e) - to_ticks(start)).max(0) as u64); FrameRef { track: b.track, offset, size, - pts: to_ticks(pts_ns), + pts, pts_ns: pts_ns as i64, duration, duration_ns: end.map_or(0, |e| (e - start).max(0) as u64), @@ -824,7 +833,7 @@ impl MkvFile { if !t.frames_readable() { return Err(Error::Unsupported(format!("content encoding of track {} (compression/encryption)", t.number))); } - let mut data = t.stripped_header().map(<[u8]>::to_vec).unwrap_or_default(); + let mut data = t.frame_prefix(s.size as u64); let at = data.len(); data.resize(at + s.size as usize, 0); src.read_at(s.offset, &mut data[at..])?; @@ -1023,7 +1032,7 @@ impl Demuxer { fn materialize(&mut self, f: FrameRef) -> Result { let t = &self.file.tracks[f.track]; - let mut data = if t.frames_readable() { t.stripped_header().map(<[u8]>::to_vec).unwrap_or_default() } else { Vec::new() }; + let mut data = if t.frames_readable() { t.frame_prefix(f.size as u64) } else { Vec::new() }; let at = data.len(); data.resize(at + f.size as usize, 0); let mut io = Io::new(&self.src, &mut self.cache); @@ -1048,6 +1057,7 @@ impl Demuxer { invisible: f.invisible, discard_padding_ns: f.discard_padding, offset: f.offset, + lace: f.lace, data, }) } diff --git a/crates/matroska/src/ebml.rs b/crates/matroska/src/ebml.rs index 9fe599ca..cccfdcf3 100644 --- a/crates/matroska/src/ebml.rs +++ b/crates/matroska/src/ebml.rs @@ -47,7 +47,8 @@ pub fn read_size(data: &[u8]) -> Option<(Size, usize)> { Some((if ones { Size::Unknown } else { Size::Known(v) }, len)) } -/// Decode an element ID (marker bits kept, 1..=4 bytes). Rejects all-zero / all-ones value bits. +/// Decode an element ID (marker bits kept, 1..=4 bytes). Rejects all-ones value bits (reserved). +/// All-zero value bits are accepted, because Matroska's `ChapterDisplay` uses ID `0x80`. pub fn read_id(data: &[u8]) -> Option<(u32, usize)> { let first = *data.first()?; let len = vint_len(first)?; @@ -55,7 +56,7 @@ pub fn read_id(data: &[u8]) -> Option<(u32, usize)> { return None; } let (v, _, ones) = read_vint(&data[..len])?; - if ones || v == 0 { + if ones || (v == 0 && len > 1) { return None; } let mut id = 0u32; @@ -364,7 +365,8 @@ mod tests { assert_eq!(read_id(&[0xA3]).unwrap(), (0xA3, 1)); assert_eq!(read_id(&[0x42, 0x86]).unwrap(), (0x4286, 2)); assert!(read_id(&[0xFF]).is_none()); // reserved - assert!(read_id(&[0x80]).is_none()); // zero value + assert_eq!(read_id(&[0x80]).unwrap(), (0x80, 1)); // ChapterDisplay + assert!(read_id(&[0x40, 0x00]).is_none()); // zero value assert!(read_id(&[0x08, 0, 0, 0, 1]).is_none()); // 5-byte ID for id in [0xA3u32, 0x4286, 0x2AD7B1, 0x1F43B675] { let mut b = Vec::new(); diff --git a/crates/matroska/src/lib.rs b/crates/matroska/src/lib.rs index c2ada3e6..2e280200 100644 --- a/crates/matroska/src/lib.rs +++ b/crates/matroska/src/lib.rs @@ -16,13 +16,17 @@ pub mod ebml; mod error; mod ids; mod meta; +mod mux; mod source; +#[cfg(test)] +mod synthetic_tests; mod track; pub use codec::Codec; pub use demux::{Demuxer, Keyframe, MkvFile, OpenOptions, Packet, SeekPoint, open, open_with}; pub use error::{Error, Result}; pub use meta::{Attachment, Chapter, ChapterDisplay, ClusterInfo, CuePoint, CuePosition, Edition, SeekEntry, SegmentInfo, SimpleTag, Tag, TagTargets}; +pub use mux::{LacingMode, MkvWriter, MuxOptions, TrackSpec}; pub use source::{ByteSource, ReadSeekSource}; pub use track::{AudioInfo, Colour, ContentEncoding, MasteringMetadata, Sample, Track, TrackKind, VideoInfo}; diff --git a/crates/matroska/src/mux.rs b/crates/matroska/src/mux.rs new file mode 100644 index 00000000..f809b1be --- /dev/null +++ b/crates/matroska/src/mux.rs @@ -0,0 +1,539 @@ +//! Minimal Matroska / WebM muxer: SimpleBlocks (optionally laced), BlockGroups for explicit +//! durations, Cues, SeekHead, Info with Duration. Output must be `Write + Seek` (sizes and the +//! SeekHead are back-patched on [`MkvWriter::finish`]). + +use std::io::{Seek, SeekFrom, Write}; + +use crate::ebml::{write_id, write_size, write_unknown_size}; +use crate::error::{Error, Result}; +use crate::ids::*; +use crate::track::TrackKind; + +/// Lacing used for audio tracks. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum LacingMode { + None, + Xiph, + Ebml, + /// Fixed-size lacing when consecutive frames have equal sizes, otherwise EBML lacing. + FixedOrEbml, +} + +/// Track description for [`MkvWriter`]. +#[derive(Clone, Debug)] +pub struct TrackSpec { + pub kind: TrackKind, + pub codec_id: String, + pub codec_private: Vec, + pub default_duration_ns: Option, + pub language: Option, + pub name: Option, + /// Pixel width/height for video tracks. + pub video_size: Option<(u32, u32)>, + /// (sampling frequency, channels, bit depth) for audio tracks. + pub audio: Option<(f64, u64, Option)>, + pub codec_delay_ns: u64, + pub seek_pre_roll_ns: u64, +} + +impl TrackSpec { + pub fn new(kind: TrackKind, codec_id: impl Into) -> Self { + Self { + kind, + codec_id: codec_id.into(), + codec_private: Vec::new(), + default_duration_ns: None, + language: None, + name: None, + video_size: None, + audio: None, + codec_delay_ns: 0, + seek_pre_roll_ns: 0, + } + } +} + +/// Muxer options. +#[derive(Clone, Debug)] +pub struct MuxOptions { + /// `webm` or `matroska`. + pub doc_type: String, + /// Nanoseconds per tick (default 1 000 000). + pub timestamp_scale: u64, + /// Start a new cluster at the next video keyframe (or any frame without video) after this long. + pub cluster_duration_ns: u64, + pub lacing: LacingMode, + /// Maximum frames per laced block. + pub max_lace_frames: usize, + pub title: Option, + pub writing_app: String, +} + +impl Default for MuxOptions { + fn default() -> Self { + Self { + doc_type: "matroska".into(), + timestamp_scale: 1_000_000, + cluster_duration_ns: 1_000_000_000, + lacing: LacingMode::None, + max_lace_frames: 8, + title: None, + writing_app: "filmcraft".into(), + } + } +} + +pub(crate) fn el(out: &mut Vec, id: u32, data: &[u8]) { + write_id(out, id); + write_size(out, data.len() as u64, 1); + out.extend_from_slice(data); +} + +pub(crate) fn el_uint(out: &mut Vec, id: u32, v: u64) { + let n = (8 - v.leading_zeros() as usize / 8).max(1); + el(out, id, &v.to_be_bytes()[8 - n..]); +} + +pub(crate) fn el_float(out: &mut Vec, id: u32, v: f64) { + el(out, id, &v.to_be_bytes()); +} + +pub(crate) fn el_str(out: &mut Vec, id: u32, s: &str) { + el(out, id, s.as_bytes()); +} + +fn void(out: &mut Vec, total: usize) { + debug_assert!(total >= 2); + let sl = if total - 2 <= 126 { 1 } else { 8 }; + write_id(out, VOID); + write_size(out, (total - 1 - sl) as u64, sl); + out.resize(out.len() + total - 1 - sl, 0); +} + +#[derive(Clone, Debug)] +struct Pending { + track: usize, + ts: i64, + frames: Vec>, +} + +const SEEK_HEAD_SPACE: usize = 128; + +/// Streaming Matroska/WebM writer. +pub struct MkvWriter { + w: W, + opts: MuxOptions, + tracks: Vec, + segment_size_pos: u64, + segment_data_start: u64, + seek_head_pos: u64, + duration_pos: u64, + info_pos: u64, + tracks_pos: u64, + cluster: Vec, + cluster_ts: Option, + cluster_start_ns: i64, + pending: Option, + cues: Vec<(u64, u64, u64)>, // (time ticks, track number, cluster position) + cued_tracks_in_cluster: Vec, + end_ns: i64, + has_video: bool, +} + +impl MkvWriter { + pub fn new(mut w: W, tracks: Vec, opts: MuxOptions) -> Result { + if tracks.is_empty() || opts.timestamp_scale == 0 { + return Err(Error::Invalid("muxer needs at least one track and a non-zero timestamp scale".into())); + } + let mut out = Vec::new(); + let mut h = Vec::new(); + el_uint(&mut h, EBML_VERSION, 1); + el_uint(&mut h, EBML_READ_VERSION, 1); + el_uint(&mut h, EBML_MAX_ID_LENGTH, 4); + el_uint(&mut h, EBML_MAX_SIZE_LENGTH, 8); + el_str(&mut h, DOC_TYPE, &opts.doc_type); + el_uint(&mut h, DOC_TYPE_VERSION, 4); + el_uint(&mut h, DOC_TYPE_READ_VERSION, 2); + el(&mut out, EBML, &h); + write_id(&mut out, SEGMENT); + let base = w.stream_position()?; + let segment_size_pos = base + out.len() as u64; + write_unknown_size(&mut out, 8); + let segment_data_start = base + out.len() as u64; + let seek_head_pos = segment_data_start; + void(&mut out, SEEK_HEAD_SPACE); + // Info + let info_pos = base + out.len() as u64; + let mut info = Vec::new(); + el_uint(&mut info, TIMESTAMP_SCALE, opts.timestamp_scale); + el_str(&mut info, MUXING_APP, "filmcraft-matroska"); + el_str(&mut info, WRITING_APP, &opts.writing_app); + if let Some(t) = &opts.title { + el_str(&mut info, TITLE, t); + } + let dur_off_in_info = info.len() + 2 + 1; // ID (2) + size (1) + el_float(&mut info, DURATION, 0.0); + write_id(&mut out, INFO); + write_size(&mut out, info.len() as u64, 1); + let duration_pos = base + out.len() as u64 + dur_off_in_info as u64; + out.extend_from_slice(&info); + // Tracks + let tracks_pos = base + out.len() as u64; + let mut tr = Vec::new(); + for (i, t) in tracks.iter().enumerate() { + let mut e = Vec::new(); + el_uint(&mut e, TRACK_NUMBER, i as u64 + 1); + el_uint(&mut e, TRACK_UID, 0x1000 + i as u64 + 1); + let ty = match t.kind { + TrackKind::Video => 1, + TrackKind::Audio => 2, + TrackKind::Subtitle => 0x11, + TrackKind::Complex => 3, + TrackKind::Logo => 0x10, + TrackKind::Buttons => 0x12, + TrackKind::Control => 0x20, + TrackKind::Metadata => 0x21, + TrackKind::Other(n) => n as u64, + }; + el_uint(&mut e, TRACK_TYPE, ty); + el_uint(&mut e, FLAG_LACING, (t.kind == TrackKind::Audio && opts.lacing != LacingMode::None) as u64); + if let Some(l) = &t.language { + el_str(&mut e, LANGUAGE, l); + } + if let Some(n) = &t.name { + el_str(&mut e, NAME, n); + } + el_str(&mut e, CODEC_ID, &t.codec_id); + if !t.codec_private.is_empty() { + el(&mut e, CODEC_PRIVATE, &t.codec_private); + } + if let Some(d) = t.default_duration_ns { + el_uint(&mut e, DEFAULT_DURATION, d); + } + if t.codec_delay_ns > 0 { + el_uint(&mut e, CODEC_DELAY, t.codec_delay_ns); + } + if t.seek_pre_roll_ns > 0 { + el_uint(&mut e, SEEK_PRE_ROLL, t.seek_pre_roll_ns); + } + if let Some((pw, ph)) = t.video_size { + let mut v = Vec::new(); + el_uint(&mut v, PIXEL_WIDTH, pw as u64); + el_uint(&mut v, PIXEL_HEIGHT, ph as u64); + el(&mut e, VIDEO, &v); + } + if let Some((rate, ch, bits)) = t.audio { + let mut a = Vec::new(); + el_float(&mut a, SAMPLING_FREQUENCY, rate); + el_uint(&mut a, CHANNELS, ch); + if let Some(b) = bits { + el_uint(&mut a, BIT_DEPTH, b); + } + el(&mut e, AUDIO, &a); + } + el(&mut tr, TRACK_ENTRY, &e); + } + el(&mut out, TRACKS, &tr); + w.write_all(&out)?; + let has_video = tracks.iter().any(|t| t.kind == TrackKind::Video); + let n = tracks.len(); + Ok(Self { + w, + opts, + tracks, + segment_size_pos, + segment_data_start, + seek_head_pos, + duration_pos, + info_pos, + tracks_pos, + cluster: Vec::new(), + cluster_ts: None, + cluster_start_ns: 0, + pending: None, + cues: Vec::new(), + cued_tracks_in_cluster: vec![false; n], + end_ns: 0, + has_video, + }) + } + + /// Add one frame. `pts_ns` is the presentation time (codec delay is added back for storage); + /// frames must be supplied in the desired file order. `duration_ns` forces a BlockGroup with a + /// BlockDuration when it differs from the track's DefaultDuration. + pub fn write_frame(&mut self, track: usize, pts_ns: i64, keyframe: bool, data: &[u8], duration_ns: Option) -> Result<()> { + let spec = self.tracks.get(track).ok_or(Error::NoSuchTrack(track))?; + // the Matroska ProRes mapping stores frames without the 8-byte `size`+`icpf` header + let data = if spec.codec_id == "V_PRORES" && data.get(4..8) == Some(b"icpf") { &data[8..] } else { data }; + let scale = self.opts.timestamp_scale as i64; + let stored_ns = pts_ns + spec.codec_delay_ns as i64; + if stored_ns < 0 { + return Err(Error::Invalid("negative timestamp".into())); + } + let ts = (stored_ns + scale / 2) / scale; + let dur_end = pts_ns + duration_ns.or(spec.default_duration_ns).unwrap_or(0) as i64; + self.end_ns = self.end_ns.max(dur_end).max(pts_ns); + let is_video = spec.kind == TrackKind::Video; + let lacing = spec.kind == TrackKind::Audio && self.opts.lacing != LacingMode::None; + let explicit_dur = duration_ns.filter(|&d| Some(d) != spec.default_duration_ns); + // new cluster? + let start_new = match self.cluster_ts { + None => true, + Some(cts) => { + let long = stored_ns - self.cluster_start_ns >= self.opts.cluster_duration_ns as i64; + (long && (!self.has_video || (is_video && keyframe))) || ts - cts > 30_000 || ts < cts + } + }; + if start_new { + self.flush_cluster()?; + self.cluster_ts = Some(ts); + self.cluster_start_ns = stored_ns; + el_uint(&mut self.cluster, TIMESTAMP, ts as u64); + } + let cts = self.cluster_ts.unwrap_or(0); + // cue: video keyframes, or the first frame of each track per cluster when there is no video + if (is_video && keyframe) || (!self.has_video && !self.cued_tracks_in_cluster[track]) { + self.cued_tracks_in_cluster[track] = true; + let pos = self.w.stream_position()? - self.segment_data_start; + if self.cues.last().is_none_or(|c| c.2 != pos || c.1 != track as u64 + 1) { + self.cues.push((ts as u64, track as u64 + 1, pos)); + } + } + if lacing && explicit_dur.is_none() { + let same = self.pending.as_ref().is_some_and(|p| p.track == track && p.frames.len() < self.opts.max_lace_frames); + if !same { + self.flush_pending(); + } + match &mut self.pending { + Some(p) => p.frames.push(data.to_vec()), + None => self.pending = Some(Pending { track, ts: ts - cts, frames: vec![data.to_vec()] }), + } + return Ok(()); + } + self.flush_pending(); + let mut block = Vec::new(); + write_size(&mut block, track as u64 + 1, 1); + block.extend_from_slice(&((ts - cts) as i16).to_be_bytes()); + if let Some(d) = explicit_dur { + block.push(0); + block.extend_from_slice(data); + let mut g = Vec::new(); + el(&mut g, BLOCK, &block); + el_uint(&mut g, BLOCK_DURATION, (d as i64 / scale) as u64); + if !keyframe { + el(&mut g, REFERENCE_BLOCK, &[0xFF]); + } + el(&mut self.cluster, BLOCK_GROUP, &g); + } else { + block.push(if keyframe { 0x80 } else { 0 }); + block.extend_from_slice(data); + el(&mut self.cluster, SIMPLE_BLOCK, &block); + } + Ok(()) + } + + fn flush_pending(&mut self) { + let Some(p) = self.pending.take() else { return }; + let mut block = Vec::new(); + write_size(&mut block, p.track as u64 + 1, 1); + block.extend_from_slice(&(p.ts as i16).to_be_bytes()); + let n = p.frames.len(); + let mode = if n == 1 { LacingMode::None } else { self.opts.lacing }; + let fixed = p.frames.iter().all(|f| f.len() == p.frames[0].len()); + match mode { + LacingMode::None => block.push(0x80), + LacingMode::Xiph => { + block.push(0x80 | 0x02); + block.push((n - 1) as u8); + for f in &p.frames[..n - 1] { + let mut s = f.len(); + while s >= 255 { + block.push(255); + s -= 255; + } + block.push(s as u8); + } + } + LacingMode::FixedOrEbml if fixed => { + block.push(0x80 | 0x04); + block.push((n - 1) as u8); + } + LacingMode::Ebml | LacingMode::FixedOrEbml => { + block.push(0x80 | 0x06); + block.push((n - 1) as u8); + write_size(&mut block, p.frames[0].len() as u64, 1); + for i in 1..n - 1 { + let d = p.frames[i].len() as i64 - p.frames[i - 1].len() as i64; + // signed VINT: smallest length whose range holds d + let mut len = 1; + while len < 8 && d.unsigned_abs() >= (1u64 << (7 * len - 1)) - 1 { + len += 1; + } + let v = (d + (1i64 << (7 * len - 1)) - 1) as u64 | (1u64 << (7 * len)); + block.extend_from_slice(&v.to_be_bytes()[8 - len..]); + } + } + } + for f in &p.frames { + block.extend_from_slice(f); + } + el(&mut self.cluster, SIMPLE_BLOCK, &block); + } + + fn flush_cluster(&mut self) -> Result<()> { + self.flush_pending(); + if self.cluster_ts.is_some() { + let mut out = Vec::new(); + el(&mut out, CLUSTER, &self.cluster); + self.w.write_all(&out)?; + } + self.cluster.clear(); + self.cluster_ts = None; + self.cued_tracks_in_cluster.iter_mut().for_each(|c| *c = false); + Ok(()) + } + + /// Write Cues, back-patch SeekHead / Segment size / Duration, and return the writer. + pub fn finish(mut self) -> Result { + self.flush_cluster()?; + let cues_pos = self.w.stream_position()?; + let mut cues = Vec::new(); + for &(time, track, pos) in &self.cues { + let mut ctp = Vec::new(); + el_uint(&mut ctp, CUE_TRACK, track); + el_uint(&mut ctp, CUE_CLUSTER_POSITION, pos); + let mut cp = Vec::new(); + el_uint(&mut cp, CUE_TIME, time); + el(&mut cp, CUE_TRACK_POSITIONS, &ctp); + el(&mut cues, CUE_POINT, &cp); + } + let has_cues = !cues.is_empty(); + if has_cues { + let mut out = Vec::new(); + el(&mut out, CUES, &cues); + self.w.write_all(&out)?; + } + let end = self.w.stream_position()?; + // SeekHead + let mut sh = Vec::new(); + let mut entries = vec![(INFO, self.info_pos), (TRACKS, self.tracks_pos)]; + if has_cues { + entries.push((CUES, cues_pos)); + } + for (id, pos) in entries { + let mut s = Vec::new(); + let mut idb = Vec::new(); + write_id(&mut idb, id); + el(&mut s, SEEK_ID, &idb); + el_uint(&mut s, SEEK_POSITION, pos - self.segment_data_start); + el(&mut sh, SEEK, &s); + } + let mut out = Vec::new(); + el(&mut out, SEEK_HEAD, &sh); + if out.len() + 2 > SEEK_HEAD_SPACE { + return Err(Error::Invalid("SeekHead does not fit its reserved space".into())); + } + let rest = SEEK_HEAD_SPACE - out.len(); + void(&mut out, rest); + self.w.seek(SeekFrom::Start(self.seek_head_pos))?; + self.w.write_all(&out)?; + // Duration + let dur = self.end_ns as f64 / self.opts.timestamp_scale as f64; + self.w.seek(SeekFrom::Start(self.duration_pos))?; + self.w.write_all(&dur.to_be_bytes())?; + // Segment size + let mut sz = Vec::new(); + write_size(&mut sz, end - self.segment_data_start, 8); + self.w.seek(SeekFrom::Start(self.segment_size_pos))?; + self.w.write_all(&sz)?; + self.w.seek(SeekFrom::Start(end))?; + self.w.flush()?; + Ok(self.w) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Demuxer, TrackKind}; + use std::io::Cursor; + + fn roundtrip(lacing: LacingMode) { + let mut v = TrackSpec::new(TrackKind::Video, "V_MJPEG"); + v.video_size = Some((16, 16)); + v.default_duration_ns = Some(40_000_000); + let mut a = TrackSpec::new(TrackKind::Audio, "A_PCM/INT/LIT"); + a.audio = Some((48000.0, 1, Some(16))); + a.default_duration_ns = Some(10_000_000); + let mut s = TrackSpec::new(TrackKind::Subtitle, "S_TEXT/UTF8"); + s.language = Some("fre".into()); + let opts = MuxOptions { lacing, ..Default::default() }; + let mut m = MkvWriter::new(Cursor::new(Vec::new()), vec![v, a, s], opts).unwrap(); + let mut expect = Vec::new(); + for i in 0..50i64 { + let vf = vec![i as u8; 100 + i as usize]; + m.write_frame(0, i * 40_000_000, i % 10 == 0, &vf, None).unwrap(); + expect.push((0usize, i * 40, vf, i % 10 == 0)); + for k in 0..4 { + let n = if lacing == LacingMode::FixedOrEbml { 960 } else { 900 + (i as usize * 7 + k * 13) % 300 }; + let af = vec![(i * 4 + k as i64) as u8; n]; + let t = i * 40 + k as i64 * 10; + m.write_frame(1, t * 1_000_000, true, &af, None).unwrap(); + expect.push((1, t, af, true)); + } + if i % 20 == 5 { + m.write_frame(2, i * 40_000_000, true, b"hello", Some(500_000_000)).unwrap(); + expect.push((2, i * 40, b"hello".to_vec(), true)); + } + } + let bytes = m.finish().unwrap().into_inner(); + let mut d = Demuxer::from_slice(&bytes).unwrap(); + assert!(d.file().warnings.is_empty(), "{:?}", d.file().warnings); + assert_eq!(d.tracks()[2].language, "fre"); + assert!(!d.file().cues.is_empty()); + assert_eq!(d.duration_ns(), Some(2_300_000_000)); + let got: Vec<_> = d.by_ref().map(|p| p.unwrap()).collect(); + assert_eq!(got.len(), expect.len()); + for (g, e) in got.iter().zip(&expect) { + assert_eq!((g.track, g.pts, &g.data, g.keyframe), (e.0, e.1, &e.2, e.3)); + } + let sub = got.iter().find(|p| p.track == 2).unwrap(); + assert_eq!(sub.duration, 500); + // the index agrees with the packet stream + let f = d.file(); + for (ti, t) in f.tracks.iter().enumerate() { + let pk: Vec<_> = got.iter().filter(|p| p.track == ti).collect(); + assert_eq!(t.samples.len(), pk.len()); + for (i, p) in pk.iter().enumerate() { + assert_eq!(f.read_sample(&bytes[..], ti, i).unwrap(), p.data); + assert_eq!(t.samples[i].pts, p.pts); + } + } + // seeking + let sp = d.seek(0, 1_230_000_000).unwrap(); + assert_eq!(sp.pts, 1200); + let p = d.next_packet().unwrap().unwrap(); + assert_eq!((p.track, p.pts, p.keyframe), (0, 1200, true)); + let sp = d.seek(1, 1_235_000_000).unwrap(); + assert_eq!(sp.pts, 1230); + let p = d.next_packet().unwrap().unwrap(); + assert_eq!((p.track, p.pts), (1, 1230)); + } + + #[test] + fn roundtrip_unlaced() { + roundtrip(LacingMode::None); + } + #[test] + fn roundtrip_xiph() { + roundtrip(LacingMode::Xiph); + } + #[test] + fn roundtrip_ebml() { + roundtrip(LacingMode::Ebml); + } + #[test] + fn roundtrip_fixed() { + roundtrip(LacingMode::FixedOrEbml); + } +} diff --git a/crates/matroska/src/synthetic_tests.rs b/crates/matroska/src/synthetic_tests.rs new file mode 100644 index 00000000..e1c604c0 --- /dev/null +++ b/crates/matroska/src/synthetic_tests.rs @@ -0,0 +1,203 @@ +//! Hand-built files exercising features FFmpeg does not write: unknown-size Segment/Clusters, +//! header stripping, BlockGroups, Colour + mastering metadata, Chapters, Tags, Attachments, CRC-32. + +use crate::codec::Codec; +use crate::ebml::{self, write_id, write_unknown_size}; +use crate::ids::*; +use crate::mux::{el, el_float, el_str, el_uint}; +use crate::track::ContentEncoding; +use crate::{Demuxer, OpenOptions, Packet, open_with}; + +fn block(track: u8, rel: i16, flags: u8, data: &[u8]) -> Vec { + let mut b = vec![0x80 | track]; + b.extend_from_slice(&rel.to_be_bytes()); + b.push(flags); + b.extend_from_slice(data); + b +} + +fn synthetic() -> Vec { + let mut f = Vec::new(); + let mut h = Vec::new(); + el_str(&mut h, DOC_TYPE, "matroska"); + el_uint(&mut h, DOC_TYPE_VERSION, 4); + el(&mut f, EBML, &h); + write_id(&mut f, SEGMENT); + write_unknown_size(&mut f, 8); + // Info with a correct CRC-32 and a Void + let mut info = Vec::new(); + el_uint(&mut info, TIMESTAMP_SCALE, 1_000_000); + el_str(&mut info, TITLE, "Synthetic"); + el(&mut info, VOID, &[0; 3]); + let mut crc_info = Vec::new(); + el(&mut crc_info, CRC32, &ebml::crc32(&info).to_le_bytes()); + crc_info.extend_from_slice(&info); + el(&mut f, INFO, &crc_info); + // Tracks + let mut v = Vec::new(); + el_uint(&mut v, TRACK_NUMBER, 1); + el_uint(&mut v, TRACK_UID, 11); + el_uint(&mut v, TRACK_TYPE, 1); + el_str(&mut v, CODEC_ID, "V_MPEG4/ISO/AVC"); + el(&mut v, CODEC_PRIVATE, &[1, 0x64, 0, 0x1F]); + el_uint(&mut v, DEFAULT_DURATION, 40_000_000); + let mut mm = Vec::new(); + for (id, val) in [(PRIMARY_R_X, 0.708), (PRIMARY_R_Y, 0.292), (PRIMARY_G_X, 0.17), (PRIMARY_G_Y, 0.797), (PRIMARY_B_X, 0.131), (PRIMARY_B_Y, 0.046)] { + el_float(&mut mm, id, val); + } + el_float(&mut mm, WHITE_POINT_X, 0.3127); + el_float(&mut mm, WHITE_POINT_Y, 0.329); + el_float(&mut mm, LUMINANCE_MAX, 1000.0); + el_float(&mut mm, LUMINANCE_MIN, 0.0001); + let mut col = Vec::new(); + el_uint(&mut col, MATRIX_COEFFICIENTS, 9); + el_uint(&mut col, RANGE, 2); + el_uint(&mut col, TRANSFER_CHARACTERISTICS, 16); + el_uint(&mut col, PRIMARIES, 9); + el_uint(&mut col, MAX_CLL, 1000); + el_uint(&mut col, MAX_FALL, 400); + el(&mut col, MASTERING_METADATA, &mm); + let mut vid = Vec::new(); + el_uint(&mut vid, PIXEL_WIDTH, 1440); + el_uint(&mut vid, PIXEL_HEIGHT, 1080); + el_uint(&mut vid, DISPLAY_WIDTH, 1920); + el_uint(&mut vid, DISPLAY_HEIGHT, 1080); + el(&mut vid, COLOUR, &col); + el(&mut v, VIDEO, &vid); + // header stripping: every frame starts with 00 00 00 01 + let mut comp = Vec::new(); + el_uint(&mut comp, CONTENT_COMP_ALGO, 3); + el(&mut comp, CONTENT_COMP_SETTINGS, &[0, 0, 0, 1]); + let mut enc = Vec::new(); + el(&mut enc, CONTENT_COMPRESSION, &comp); + let mut encs = Vec::new(); + el(&mut encs, CONTENT_ENCODING, &enc); + el(&mut v, CONTENT_ENCODINGS, &encs); + let mut a = Vec::new(); + el_uint(&mut a, TRACK_NUMBER, 2); + el_uint(&mut a, TRACK_TYPE, 2); + el_str(&mut a, CODEC_ID, "A_AAC/MPEG4/LC/SBR"); + el_str(&mut a, LANGUAGE, "ger"); + let mut au = Vec::new(); + el_float(&mut au, SAMPLING_FREQUENCY, 24000.0); + el_float(&mut au, OUTPUT_SAMPLING_FREQUENCY, 48000.0); + el_uint(&mut au, CHANNELS, 2); + el(&mut a, AUDIO, &au); + let mut tr = Vec::new(); + el(&mut tr, TRACK_ENTRY, &v); + el(&mut tr, TRACK_ENTRY, &a); + el(&mut f, TRACKS, &tr); + // Chapters + let mut disp = Vec::new(); + el_str(&mut disp, CHAP_STRING, "Intro"); + let mut atom = Vec::new(); + el_uint(&mut atom, CHAPTER_UID, 5); + el_uint(&mut atom, CHAPTER_TIME_START, 0); + el_uint(&mut atom, CHAPTER_TIME_END, 40_000_000); + el(&mut atom, CHAPTER_DISPLAY, &disp); + let mut ed = Vec::new(); + el(&mut ed, CHAPTER_ATOM, &atom); + let mut ch = Vec::new(); + el(&mut ch, EDITION_ENTRY, &ed); + el(&mut f, CHAPTERS, &ch); + // Attachments + let mut af = Vec::new(); + el_str(&mut af, FILE_NAME, "font.ttf"); + el_str(&mut af, FILE_MEDIA_TYPE, "font/ttf"); + el_uint(&mut af, FILE_UID, 77); + el(&mut af, FILE_DATA, &[9; 100]); + let mut att = Vec::new(); + el(&mut att, ATTACHED_FILE, &af); + el(&mut f, ATTACHMENTS, &att); + // Cluster 1 (unknown size): keyframe SimpleBlock, BlockGroup with ReferenceBlock + duration + write_id(&mut f, CLUSTER); + write_unknown_size(&mut f, 1); + el_uint(&mut f, TIMESTAMP, 1000); + el(&mut f, SIMPLE_BLOCK, &block(1, 0, 0x80, &[0x65, 1, 2])); + el(&mut f, SIMPLE_BLOCK, &block(2, 5, 0x80, &[0xAA; 10])); + let mut g = Vec::new(); + el(&mut g, BLOCK, &block(1, 40, 0, &[0x41, 3])); + el(&mut g, REFERENCE_BLOCK, &[0xD8]); + el_uint(&mut g, BLOCK_DURATION, 80); + el(&mut f, BLOCK_GROUP, &g); + el(&mut f, SIMPLE_BLOCK, &block(9, 0, 0x80, &[1])); // unknown track: ignored + // Cluster 2 (unknown size), ended by Tags + write_id(&mut f, CLUSTER); + write_unknown_size(&mut f, 8); + el_uint(&mut f, TIMESTAMP, 1120); + el(&mut f, SIMPLE_BLOCK, &block(1, 0, 0x80, &[0x65, 4])); + let mut st = Vec::new(); + el_str(&mut st, TAG_NAME, "ARTIST"); + el_str(&mut st, TAG_STRING, "Nobody"); + let mut tg = Vec::new(); + let mut tgt = Vec::new(); + el_uint(&mut tgt, TAG_TRACK_UID, 11); + el(&mut tg, TARGETS, &tgt); + el(&mut tg, SIMPLE_TAG, &st); + let mut tags = Vec::new(); + el(&mut tags, TAG, &tg); + el(&mut f, TAGS, &tags); + f +} + +#[test] +fn synthetic_file() { + let bytes = synthetic(); + let f = open_with(&bytes[..], &OpenOptions { index: true, verify_crc: true }).unwrap(); + assert!(f.live); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + assert_eq!((f.crc_checked, f.crc_errors.len()), (1, 0)); + assert_eq!(f.info.title.as_deref(), Some("Synthetic")); + let v = &f.tracks[0]; + assert!(matches!(&v.codec, Codec::Avc { avcc } if avcc[1] == 0x64)); + let vi = v.video.as_ref().unwrap(); + assert_eq!(vi.pixel_aspect(), (4, 3)); + let c = vi.colour.as_ref().unwrap(); + assert!(c.full_range()); + assert_eq!((c.matrix_coefficients, c.transfer_characteristics, c.primaries, c.max_cll, c.max_fall), (Some(9), Some(16), Some(9), Some(1000), Some(400))); + let m = c.mastering.as_ref().unwrap(); + assert_eq!(m.primaries[1], (0.17, 0.797)); + assert_eq!(m.white_point, (0.3127, 0.329)); + assert_eq!((m.luminance_max, m.luminance_min), (1000.0, 0.0001)); + assert_eq!(v.encodings, vec![ContentEncoding::HeaderStripping { bytes: vec![0, 0, 0, 1], scope: 1 }]); + // legacy AAC ID: ASC synthesised with explicit SBR signalling (LC, 24 kHz, stereo → 48 kHz) + assert_eq!(f.tracks[1].codec, Codec::Aac { asc: vec![0x13, 0x10, 0x56, 0xE5, 0x98] }); + assert_eq!(f.tracks[1].language, "ger"); + assert_eq!(f.chapters[0].chapters[0].title(), Some("Intro")); + assert_eq!(f.chapters[0].chapters[0].end_ns, Some(40_000_000)); + assert_eq!(f.attachments.len(), 1); + let at = &f.attachments[0]; + assert_eq!((at.name.as_str(), at.media_type.as_str(), at.uid), ("font.ttf", "font/ttf", 77)); + assert_eq!(&bytes[at.data_offset as usize..][..100], &[9; 100]); + assert_eq!(f.tags[0].targets.track_uids, vec![11]); + assert_eq!(f.tags[0].simple[0].string.as_deref(), Some("Nobody")); + assert_eq!(f.clusters.len(), 2); + assert_eq!(v.samples.len(), 3); + assert_eq!(v.keyframes(), &[0, 2]); + + let d = Demuxer::from_slice(&bytes).unwrap(); + let p: Vec = d.map(|p| p.unwrap()).collect(); + assert_eq!(p.len(), 4); + assert_eq!((p[0].track, p[0].pts, p[0].keyframe, p[0].duration), (0, 1000, true, 40)); + assert_eq!(p[0].data, [0, 0, 0, 1, 0x65, 1, 2]); + assert_eq!((p[1].track, p[1].pts, p[1].data.len()), (1, 1005, 10)); + assert_eq!((p[2].pts, p[2].keyframe, p[2].duration), (1040, false, 80)); + assert_eq!(p[2].data, [0, 0, 0, 1, 0x41, 3]); + assert_eq!((p[3].pts, p[3].pts_ns), (1120, 1_120_000_000)); + assert_eq!(f.read_sample(&bytes[..], 0, 1).unwrap(), p[2].data); + + // CRC mismatch is reported, not fatal + let mut bad = bytes.clone(); + let title = bad.windows(9).position(|w| w == b"Synthetic").unwrap(); + bad[title] = b's'; + let f = open_with(&bad[..], &OpenOptions { index: true, verify_crc: true }).unwrap(); + assert_eq!(f.crc_errors.len(), 1); + + // seeking without index or cues builds the index + let mut d = Demuxer::with_options(&bytes[..], OpenOptions { index: false, verify_crc: false }).unwrap(); + assert_eq!(d.file().tracks.len(), 2); + assert_eq!(d.seek(0, 1_100_000_000).unwrap().pts, 1000); + assert_eq!(d.seek(0, 1_130_000_000).unwrap().pts, 1120); + assert_eq!(d.next_packet().unwrap().unwrap().pts, 1120); + assert!(d.file().indexed); +} diff --git a/crates/matroska/src/track.rs b/crates/matroska/src/track.rs index 25c0267b..79fcc3c2 100644 --- a/crates/matroska/src/track.rs +++ b/crates/matroska/src/track.rs @@ -220,10 +220,25 @@ impl Track { }) } - /// Decoded frame size of sample `i` (stored size plus stripped header). + /// Bytes to put in front of a stored frame of `stored_size` bytes to get the codec frame: + /// header-stripped bytes, or for `V_PRORES` the 8-byte frame header (`size` + `icpf`) that the + /// Matroska ProRes mapping omits. + pub fn frame_prefix(&self, stored_size: u64) -> Vec { + let mut p = self.stripped_header().map(<[u8]>::to_vec).unwrap_or_default(); + if matches!(self.codec, Codec::ProRes { .. }) { + let total = stored_size + p.len() as u64 + 8; + let mut h = (total as u32).to_be_bytes().to_vec(); + h.extend_from_slice(b"icpf"); + h.extend_from_slice(&p); + p = h; + } + p + } + + /// Frame size of sample `i` as returned by `read_sample` / packets (stored size plus prefix). pub fn sample_size(&self, i: usize) -> Option { let s = self.samples.get(i)?; - Some(s.size as u64 + self.stripped_header().map_or(0, |h| h.len() as u64)) + Some(s.size as u64 + self.frame_prefix(s.size as u64).len() as u64) } /// Indices of keyframe samples, ascending by file order. diff --git a/crates/matroska/tests/common/mod.rs b/crates/matroska/tests/common/mod.rs new file mode 100644 index 00000000..2e0fd872 --- /dev/null +++ b/crates/matroska/tests/common/mod.rs @@ -0,0 +1,137 @@ +//! Shared helpers for oracle tests: ffmpeg/ffprobe discovery and fixture generation. +//! ffmpeg/ffprobe are used only as external fixture generators and oracles (never linked). +#![allow(dead_code)] + +use serde_json::Value; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +pub fn tool(name: &str) -> Option { + for dir in ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"] { + let p = Path::new(dir).join(name); + if p.exists() { + return Some(p); + } + } + let ok = Command::new(name).arg("-version").output().map(|o| o.status.success()).unwrap_or(false); + ok.then(|| PathBuf::from(name)) +} + +/// (ffmpeg, ffprobe), or `None` (test skipped) if either is missing. +pub fn tools() -> Option<(PathBuf, PathBuf)> { + match (tool("ffmpeg"), tool("ffprobe")) { + (Some(a), Some(b)) => Some((a, b)), + _ => { + eprintln!("ffmpeg/ffprobe not found; skipping oracle test"); + None + } + } +} + +pub fn fixture_dir() -> PathBuf { + let d = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/fixtures/matroska"); + std::fs::create_dir_all(&d).unwrap(); + d +} + +const V: &[&str] = &["-f", "lavfi", "-i", "testsrc2=size=160x120:rate=25"]; +const A: &[&str] = &["-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000"]; + +fn write_subs(dir: &Path) { + let srt = "1\n00:00:00,200 --> 00:00:00,900\nHello\n\n2\n00:00:01,000 --> 00:00:01,500\nWorld two\n\n"; + std::fs::write(dir.join("in.srt"), srt).unwrap(); + let vtt = "WEBVTT\n\n00:00:00.200 --> 00:00:00.900\nHello\n\n00:00:01.000 --> 00:00:01.500\nWorld\n\n"; + std::fs::write(dir.join("in.vtt"), vtt).unwrap(); +} + +/// Fixture name → ffmpeg arguments (after `-y -v error`, before the output path). +/// `None` for fixtures written through a pipe (no Cues, unknown sizes). +pub fn spec(name: &str) -> Option> { + let cat = |parts: &[&[&str]]| parts.concat().into_iter().map(String::from).collect::>(); + let x264: &[&str] = &["-c:v", "libx264", "-bf", "2", "-g", "12", "-pix_fmt", "yuv420p"]; + Some(match name { + "h264_aac.mkv" => cat(&[V, A, &["-t", "2"], x264, &["-c:a", "aac"]]), + "vp9_opus.webm" => cat(&[V, A, &["-t", "2", "-c:v", "libvpx-vp9", "-deadline", "realtime", "-g", "10", "-c:a", "libopus"]]), + "flac.mkv" => cat(&[A, &["-t", "2", "-c:a", "flac"]]), + "vorbis.mkv" => cat(&[A, &["-t", "2", "-ac", "2", "-c:a", "vorbis", "-strict", "-2"]]), + "subs.mkv" => cat(&[ + V, + &["-i", "in.srt", "-i", "in.srt", "-t", "2", "-map", "0:v", "-map", "1:s", "-map", "2:s"], + x264, + &["-c:s:0", "srt", "-c:s:1", "ass", "-metadata:s:s:0", "language=fre", "-metadata:s:s:1", "title=Styled"], + ]), + "webvtt.webm" => cat(&[V, &["-i", "in.vtt", "-t", "2", "-map", "0:v", "-map", "1:s", "-c:v", "libvpx", "-deadline", "realtime", "-c:s", "webvtt"]]), + "cues_front.mkv" => cat(&[V, A, &["-t", "3"], x264, &["-c:a", "aac", "-cues_to_front", "1", "-cluster_time_limit", "500"]]), + "hevc_hdr.mkv" => cat(&[ + V, + &[ + "-t", + "1", + "-c:v", + "libx265", + "-pix_fmt", + "yuv420p10le", + "-x265-params", + "log-level=error:bframes=3:keyint=10:hdr10=1:master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1):max-cll=1000,400", + "-vf", + "setparams=color_primaries=bt2020:color_trc=smpte2084:colorspace=bt2020nc:range=tv", + ], + ]), + "prores_pcm.mkv" => cat(&[V, A, &["-t", "1", "-c:v", "prores_ks", "-profile:v", "2", "-c:a", "pcm_s24le"]]), + "mjpeg_ac3.mkv" => cat(&[V, A, &["-t", "1", "-c:v", "mjpeg", "-q:v", "5", "-c:a", "ac3", "-metadata", "title=FilmCraft test"]]), + "av1.mkv" => cat(&[V, &["-t", "1", "-c:v", "libsvtav1", "-preset", "12", "-g", "10"]]), + "live.mkv" => return None, + _ => panic!("unknown fixture {name}"), + }) +} + +/// Generate (once) and return the path of a fixture; `None` if generation failed (encoder missing). +pub fn fixture(ffmpeg: &Path, name: &str) -> Option { + let dir = fixture_dir(); + let out = dir.join(name); + if out.exists() && std::fs::metadata(&out).map(|m| m.len() > 0).unwrap_or(false) { + return Some(out); + } + write_subs(&dir); + let tid = format!("{:?}", std::thread::current().id()).replace(|c: char| !c.is_ascii_alphanumeric(), ""); + let tmp = dir.join(format!("{name}.{}.{tid}.tmp", std::process::id())); + let ok = match spec(name) { + Some(args) => { + let mut c = Command::new(ffmpeg); + c.current_dir(&dir).args(["-y", "-v", "error"]).args(&args); + let fmt = if name.ends_with(".webm") { "webm" } else { "matroska" }; + c.args(["-f", fmt]).arg(&tmp); + c.status().map(|s| s.success()).unwrap_or(false) + } + None => { + // streamed to a pipe: unknown-size Segment, no Cues, no Duration + let f = std::fs::File::create(&tmp).unwrap(); + let mut c = Command::new(ffmpeg); + c.current_dir(&dir).args(["-y", "-v", "error"]).args(V).args(A); + c.args(["-t", "2", "-c:v", "libx264", "-bf", "2", "-g", "12", "-pix_fmt", "yuv420p", "-c:a", "aac", "-f", "matroska", "pipe:1"]); + c.stdout(Stdio::from(f)); + c.status().map(|s| s.success()).unwrap_or(false) + } + }; + if !ok { + let _ = std::fs::remove_file(&tmp); + eprintln!("could not generate {name} (encoder unavailable?); skipping"); + return None; + } + std::fs::rename(&tmp, &out).unwrap(); // atomic: concurrent generators race harmlessly + Some(out) +} + +pub fn ffprobe_json(ffprobe: &Path, path: &Path) -> Value { + let out = Command::new(ffprobe) + .args(["-v", "error", "-show_packets", "-show_streams", "-show_format", "-show_chapters", "-of", "json"]) + .arg(path) + .output() + .expect("run ffprobe"); + assert!(out.status.success(), "ffprobe failed on {}: {}", path.display(), String::from_utf8_lossy(&out.stderr)); + serde_json::from_slice(&out.stdout).expect("ffprobe json") +} + +pub fn int(v: &Value) -> Option { + v.as_i64().or_else(|| v.as_str().and_then(|s| s.parse().ok())) +} diff --git a/crates/matroska/tests/oracle.rs b/crates/matroska/tests/oracle.rs new file mode 100644 index 00000000..bc278ac8 --- /dev/null +++ b/crates/matroska/tests/oracle.rs @@ -0,0 +1,362 @@ +//! Oracle tests: compare our demuxer's packets, timestamps, keyframes and track parameters with +//! ffprobe on ffmpeg-generated fixtures (skipped when ffmpeg/ffprobe are not installed). + +mod common; + +use common::*; +use filmcraft_matroska::*; +use serde_json::Value; +use std::path::Path; + +/// Compare every packet (file order) and stream parameter against ffprobe. +fn compare(ffprobe: &Path, path: &Path) -> (MkvFile, Vec) { + let json = ffprobe_json(ffprobe, path); + let bytes = std::fs::read(path).unwrap(); + let mut d = Demuxer::from_slice(&bytes).unwrap(); + let name = path.file_name().unwrap().to_string_lossy().to_string(); + let file = d.file().clone(); + let streams = json["streams"].as_array().unwrap(); + assert_eq!(streams.len(), file.tracks.len(), "{name}: stream count"); + for (i, st) in streams.iter().enumerate() { + let t = &file.tracks[i]; + assert_eq!(st["codec_name"].as_str().unwrap_or(""), t.codec.name(), "{name}: track {i} codec ({})", t.codec_id); + assert_eq!(st["time_base"].as_str(), Some(format!("{}/{}", t.timebase.0, t.timebase.1).as_str()), "{name}: time_base"); + match t.kind { + TrackKind::Video => { + let v = t.video.as_ref().unwrap(); + assert_eq!(int(&st["width"]), Some(v.pixel_width as i64), "{name}: width"); + assert_eq!(int(&st["height"]), Some(v.pixel_height as i64), "{name}: height"); + } + TrackKind::Audio => { + let a = t.audio.as_ref().unwrap(); + assert_eq!(int(&st["sample_rate"]), Some(a.sampling_frequency as i64), "{name}: sample_rate"); + assert_eq!(int(&st["channels"]), Some(a.channels as i64), "{name}: channels"); + } + _ => {} + } + } + let packets: Vec = d.by_ref().collect::>().unwrap(); + let ff = json["packets"].as_array().unwrap(); + assert_eq!(packets.len(), ff.len(), "{name}: packet count"); + let (mut pts_checked, mut dur_checked) = (0, 0); + for (i, (p, f)) in packets.iter().zip(ff).enumerate() { + let ctx = format!("{name}: packet {i} (track {})", p.track); + assert_eq!(int(&f["stream_index"]), Some(p.track as i64), "{ctx}: stream"); + if file.tracks[p.track].codec_id.starts_with("D_WEBVTT") { + // WebM WebVTT blocks carry "id\nsettings\npayload"; FFmpeg moves id/settings to side data + assert!(p.data.len() as i64 >= int(&f["size"]).unwrap(), "{ctx}: size"); + } else { + assert_eq!(int(&f["size"]), Some(p.data.len() as i64), "{ctx}: size"); + } + let key = f["flags"].as_str().unwrap_or("").starts_with('K'); + assert_eq!(key, p.keyframe, "{ctx}: keyframe"); + if let Some(pts) = int(&f["pts"]) + && !(p.lace > 0 && p.duration == 0) + { + // FFmpeg truncates the per-lace duration to whole ticks; we place laces exactly + // (rounded once), so later laces may differ by up to one tick per lace. + assert!((pts - p.pts).abs() <= p.lace as i64, "{ctx}: pts ffprobe {pts} vs {}", p.pts); + pts_checked += 1; + } + if let Some(dur) = int(&f["duration"]) + && p.duration != 0 + { + assert!((dur - p.duration as i64).abs() <= 1, "{ctx}: duration ffprobe {dur} vs {}", p.duration); + dur_checked += 1; + } + } + assert!(pts_checked > 0, "{name}: no pts compared"); + eprintln!("{name}: {} packets, {pts_checked} pts and {dur_checked} durations compared", packets.len()); + // the sample index agrees with the packet stream + for (ti, t) in file.tracks.iter().enumerate() { + let pk: Vec<&Packet> = packets.iter().filter(|p| p.track == ti).collect(); + assert_eq!(t.samples.len(), pk.len(), "{name}: index size track {ti}"); + for (i, p) in pk.iter().enumerate() { + assert_eq!(t.samples[i].pts, p.pts); + assert_eq!(t.samples[i].keyframe, p.keyframe); + assert_eq!(t.sample_size(i), Some(p.data.len() as u64)); + } + if let Some(i) = pk.len().checked_sub(1) { + assert_eq!(file.read_sample(&bytes[..], ti, i).unwrap(), pk[i].data); + } + } + // duration + if let Some(fd) = json["format"]["duration"].as_str().and_then(|s| s.parse::().ok()) + && let Some(ours) = file.duration_ns() + { + assert!((fd - ours as f64 / 1e9).abs() < 0.05, "{name}: duration {fd} vs {ours}"); + } + (file, packets) +} + +/// Seek to a range of times on `track` and check we land on the latest keyframe ≤ target. +fn check_seeks(bytes: &[u8], opts: OpenOptions, track: usize, packets: &[Packet]) { + let mut d = Demuxer::with_options(bytes, opts).unwrap(); + let keys: Vec<(i64, i64)> = packets.iter().filter(|p| p.track == track && p.keyframe).map(|p| (p.pts, p.pts_ns)).collect(); + let last = packets.iter().filter(|p| p.track == track).map(|p| p.pts_ns).max().unwrap(); + let mut t = 0i64; + while t <= last + 100_000_000 { + let sp = d.seek(track, t).unwrap(); + let expect = keys.iter().filter(|k| k.1 <= t).map(|k| k.0).max().unwrap_or(keys[0].0); + assert_eq!(sp.pts, expect, "seek to {t}ns"); + assert!(sp.pts_ns <= t || sp.pts == keys[0].0); + // the first packet of that track after seeking is the keyframe + let p = loop { + let p = d.next_packet().unwrap().expect("packet after seek"); + if p.track == track { + break p; + } + }; + assert!(p.keyframe); + assert_eq!(p.pts, sp.pts); + t += 137_000_000; + } +} + +fn run(name: &str) -> Option<(Vec, MkvFile, Vec)> { + let (ffmpeg, ffprobe) = tools()?; + let path = fixture(&ffmpeg, name)?; + let (file, packets) = compare(&ffprobe, &path); + let bytes = std::fs::read(&path).unwrap(); + Some((bytes, file, packets)) +} + +#[test] +fn h264_aac() { + let Some((bytes, f, packets)) = run("h264_aac.mkv") else { return }; + assert_eq!(f.doc_type, "matroska"); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + let v = f.track_of_kind(TrackKind::Video).unwrap(); + let Codec::Avc { avcc } = &f.tracks[v].codec else { panic!("not avc") }; + assert_eq!(avcc[0], 1, "avcC version"); + let a = f.track_of_kind(TrackKind::Audio).unwrap(); + assert!(matches!(&f.tracks[a].codec, Codec::Aac { asc } if asc.len() >= 2)); + assert!(!f.cues.is_empty()); + assert!(!f.seek_head.is_empty()); + check_seeks(&bytes, OpenOptions::default(), v, &packets); + check_seeks(&bytes, OpenOptions { index: false, verify_crc: false }, v, &packets); + check_seeks(&bytes, OpenOptions::default(), a, &packets); + // Read + Seek entry point and CRC verification (ffmpeg writes CRC-32 in level-1 elements) + let d = Demuxer::from_reader(std::io::Cursor::new(bytes.clone())).unwrap(); + assert_eq!(d.file().tracks.len(), 2); + let crc = open_with(&bytes[..], &OpenOptions { index: true, verify_crc: true }).unwrap(); + assert!(crc.crc_checked > 0); + assert!(crc.crc_errors.is_empty()); + let kf = Demuxer::from_slice(&bytes).unwrap().keyframe_index(v).unwrap(); + assert_eq!(kf.len(), packets.iter().filter(|p| p.track == v && p.keyframe).count()); +} + +#[test] +fn vp9_opus_webm() { + let Some((bytes, f, packets)) = run("vp9_opus.webm") else { return }; + assert!(f.is_webm()); + let a = f.track_of_kind(TrackKind::Audio).unwrap(); + let t = &f.tracks[a]; + assert!(matches!(&t.codec, Codec::Opus { head } if head.starts_with(b"OpusHead"))); + assert!(t.codec_delay_ns > 0 && t.seek_pre_roll_ns > 0); + assert!(packets.iter().any(|p| p.track == a && p.pts < 0), "codec delay applied"); + let v = f.track_of_kind(TrackKind::Video).unwrap(); + assert!(matches!(f.tracks[v].codec, Codec::Vp9 { .. })); + check_seeks(&bytes, OpenOptions::default(), v, &packets); + check_seeks(&bytes, OpenOptions { index: false, verify_crc: false }, v, &packets); +} + +#[test] +fn flac() { + let Some((_, f, _)) = run("flac.mkv") else { return }; + assert!(matches!(&f.tracks[0].codec, Codec::Flac { private } if private.starts_with(b"fLaC"))); +} + +#[test] +fn vorbis() { + let Some((_, f, _)) = run("vorbis.mkv") else { return }; + let Codec::Vorbis { headers } = &f.tracks[0].codec else { panic!("not vorbis") }; + assert_eq!(headers.len(), 3); + assert_eq!(&headers[0][..7], b"\x01vorbis"); + assert_eq!(&headers[1][..7], b"\x03vorbis"); + assert_eq!(&headers[2][..7], b"\x05vorbis"); +} + +#[test] +fn subtitles() { + let Some((_, f, packets)) = run("subs.mkv") else { return }; + let subs: Vec = (0..f.tracks.len()).filter(|&i| f.tracks[i].kind == TrackKind::Subtitle).collect(); + assert_eq!(subs.len(), 2); + assert_eq!(f.tracks[subs[0]].codec, Codec::SubRip); + assert_eq!(f.tracks[subs[0]].language, "fre"); + assert!(matches!(&f.tracks[subs[1]].codec, Codec::Ass { header, ssa: false } if !header.is_empty())); + assert_eq!(f.tracks[subs[1]].name, "Styled"); + let first = packets.iter().find(|p| p.track == subs[0]).unwrap(); + assert_eq!(first.data, b"Hello"); + assert_eq!((first.pts, first.duration), (200, 700)); +} + +#[test] +fn webvtt_webm() { + let Some((_, f, packets)) = run("webvtt.webm") else { return }; + let s = f.track_of_kind(TrackKind::Subtitle).unwrap(); + assert_eq!(f.tracks[s].codec, Codec::WebVtt); + let cues: Vec<&Packet> = packets.iter().filter(|p| p.track == s).collect(); + assert_eq!(cues.len(), 2); + assert!(cues[0].data.ends_with(b"Hello")); +} + +#[test] +fn cues_at_front() { + let Some((bytes, f, packets)) = run("cues_front.mkv") else { return }; + assert!(!f.cues.is_empty()); + assert!(f.clusters.len() >= 5); + let v = f.track_of_kind(TrackKind::Video).unwrap(); + check_seeks(&bytes, OpenOptions { index: false, verify_crc: false }, v, &packets); +} + +#[test] +fn live_stream_without_cues() { + let Some((bytes, f, packets)) = run("live.mkv") else { return }; + assert!(f.cues.is_empty(), "streamed output has no Cues"); + assert!(f.live || f.info.duration.is_none()); + assert!(f.duration_ns().unwrap() > 1_900_000_000); + let v = f.track_of_kind(TrackKind::Video).unwrap(); + // no cues: seeking builds the index by scanning clusters + check_seeks(&bytes, OpenOptions { index: false, verify_crc: false }, v, &packets); + let mut d = Demuxer::with_options(&bytes[..], OpenOptions { index: false, verify_crc: false }).unwrap(); + assert!(!d.file().indexed); + assert!(!d.keyframe_index(v).unwrap().is_empty()); + assert!(d.file().indexed); +} + +#[test] +fn hevc_hdr_colour() { + let Some((_, f, _)) = run("hevc_hdr.mkv") else { return }; + let t = &f.tracks[0]; + assert!(matches!(&t.codec, Codec::Hevc { hvcc } if hvcc.first() == Some(&1))); + let c = t.video.as_ref().unwrap().colour.as_ref().expect("Colour element"); + assert_eq!(c.primaries, Some(9)); + assert_eq!(c.transfer_characteristics, Some(16)); + assert_eq!(c.matrix_coefficients, Some(9)); + assert_eq!(c.range, Some(1)); + // mastering metadata / CLL are written by ffmpeg from stream side data when available + if let Some(m) = &c.mastering { + assert!((m.primaries[1].0 - 0.265).abs() < 1e-3, "{m:?}"); + assert!((m.luminance_max - 1000.0).abs() < 1e-3); + } + if let Some(cll) = c.max_cll { + assert_eq!(cll, 1000); + } +} + +#[test] +fn prores_pcm() { + let Some((_, f, _)) = run("prores_pcm.mkv") else { return }; + let a = f.track_of_kind(TrackKind::Audio).unwrap(); + assert_eq!(f.tracks[a].codec, Codec::Pcm { float: false, big_endian: false, bits: 24 }); +} + +#[test] +fn mjpeg_ac3() { + let Some((_, f, _)) = run("mjpeg_ac3.mkv") else { return }; + assert_eq!(f.info.title.as_deref(), Some("FilmCraft test")); + assert!(f.info.muxing_app.starts_with("Lavf")); + assert!(f.tags.iter().flat_map(|t| &t.simple).any(|s| s.name == "ENCODER")); +} + +#[test] +fn av1() { + let Some((_, f, _)) = run("av1.mkv") else { return }; + assert!(matches!(&f.tracks[0].codec, Codec::Av1 { av1c } if av1c.first() == Some(&0x81))); +} + +/// Re-mux a fixture with our writer using each lacing mode; ffprobe must read the result identically +/// to our demuxer (and to the source packets). +#[test] +fn laced_remux() { + let Some((ffmpeg, ffprobe)) = tools() else { return }; + for (src, modes) in [ + ("vorbis.mkv", &[LacingMode::Xiph, LacingMode::Ebml][..]), + ("h264_aac.mkv", &[LacingMode::Ebml, LacingMode::Xiph][..]), + ("prores_pcm.mkv", &[LacingMode::FixedOrEbml][..]), + ("flac.mkv", &[LacingMode::Ebml][..]), + ] { + let Some(path) = fixture(&ffmpeg, src) else { continue }; + let bytes = std::fs::read(&path).unwrap(); + let d = Demuxer::from_slice(&bytes).unwrap(); + let file = d.file().clone(); + let packets: Vec = d.collect::>().unwrap(); + for &mode in modes { + let specs: Vec = file + .tracks + .iter() + .map(|t| { + let mut s = TrackSpec::new(t.kind, t.codec_id.clone()); + s.codec_private = t.codec_private.clone(); + s.default_duration_ns = t.default_duration_ns.or_else(|| { + // give AAC a DefaultDuration so every lace gets a timestamp + matches!(t.codec, Codec::Aac { .. }).then(|| (1024.0 * 1e9 / t.audio.as_ref().unwrap().sampling_frequency) as u64) + }); + s.video_size = t.video.as_ref().map(|v| (v.pixel_width, v.pixel_height)); + s.audio = t.audio.as_ref().map(|a| (a.sampling_frequency, a.channels, a.bit_depth)); + s.codec_delay_ns = t.codec_delay_ns; + s + }) + .collect(); + let opts = MuxOptions { lacing: mode, ..Default::default() }; + let mut w = MkvWriter::new(std::io::Cursor::new(Vec::new()), specs, opts).unwrap(); + for p in &packets { + w.write_frame(p.track, p.pts_ns, p.keyframe, &p.data, None).unwrap(); + } + let out = w.finish().unwrap().into_inner(); + let laced = { + let f = open(&out[..]).unwrap(); + f.tracks.iter().any(|t| t.samples.iter().any(|s| s.lace > 0)) + }; + assert!(laced, "{src} {mode:?}: nothing was laced"); + let outp = fixture_dir().join(format!("remux_{mode:?}_{src}")); + std::fs::write(&outp, &out).unwrap(); + let (_, got) = compare(&ffprobe, &outp); + assert_eq!(got.len(), packets.len()); + for (a, b) in got.iter().zip(&packets) { + assert_eq!(a.data, b.data, "{src} {mode:?}: payload"); + assert_eq!(a.track, b.track); + } + std::fs::remove_file(&outp).unwrap(); + } + } +} + +/// Damaged file: overwrite bytes in the middle of the clusters; the demuxer must not fail and must +/// resync on a later cluster. +#[test] +fn damaged_resync() { + let Some((mut bytes, f, packets)) = run("h264_aac.mkv") else { return }; + let mid = f.clusters[f.clusters.len() / 2]; + let at = mid.data_start as usize + 40; + for b in &mut bytes[at..at + 200] { + *b = 0x00; + } + let d = Demuxer::from_slice(&bytes).unwrap(); + assert!(!d.file().warnings.is_empty()); + let got: Vec = d.collect::>().unwrap(); + assert!(got.len() < packets.len()); + assert!(got.len() > packets.len() / 2); + // packets after the damaged cluster are intact + let last = packets.last().unwrap(); + assert_eq!(got.last().unwrap(), last); + // truncated file + let cut = &bytes[..bytes.len() * 2 / 3]; + let d = Demuxer::from_slice(cut).unwrap(); + let got: Vec = d.collect::>().unwrap(); + assert!(!got.is_empty()); + // garbage never panics + for n in [0usize, 10, 100, 1000] { + let _ = Demuxer::from_slice(&bytes[..n.min(bytes.len())]).map(|d| d.count()); + } +} + +#[test] +fn not_matroska() { + assert!(matches!(open(&b"hello world"[..]), Err(Error::NotMatroska(_)))); + let mut ebml = vec![0x1A, 0x45, 0xDF, 0xA3, 0x84, 0x42, 0x82, 0x81, b'x']; + ebml.extend_from_slice(&[0; 8]); + assert!(matches!(open(&ebml[..]), Err(Error::NotMatroska(_)))); + let _ = Value::Null; +}