FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
captions: bound MCC export work to scheduled data (#2811) (#3307)
Michael Butlercbf75114aff2
diff --git a/crates/captions/README.md b/crates/captions/README.mdindex 8618cadac..661d8fb82 100644--- a/crates/captions/README.md+++ b/crates/captions/README.md@@ -14,7 +14,7 @@ and writes caption files, converts them to and from caption tracks, and draws ca| SubRip `.srt` | yes | yes | BOM, UTF-16, Windows-1252 fallback, CRLF/CR, missing or wrong indexes, `,` or `.` milliseconds, 1–9 fraction digits, missing hours, SSA coordinates, cues not separated by blank lines. Inline tags are kept as text. || WebVTT `.vtt` | yes | yes | Cue identifiers, cue settings (verbatim), a leading `<v Speaker>` voice span (as the caption's speaker) and STYLE / REGION / NOTE blocks are kept. A missing `WEBVTT` signature is tolerated. || Scenarist SCC `.scc` | yes | yes | CEA-608 at 29.97 fps, drop-frame or not. Reads pop-on, roll-up and paint-on (channel 1); writes pop-on. |-| MacCaption MCC `.mcc` | yes | yes | One SMPTE ST 334-2 caption distribution packet (CDP) per frame in MacCaption's hex text with letter abbreviations. Writes 29.97 fps (`30DF` / `30`) with CEA-608 field 1 (the SCC schedule) **and** CEA-708 service 1 (two-window pop-on emulation). Reads CEA-608 field 1 when present, else CEA-708 service 1; time code rates 30DF, 30, 60DF, 60, 25, 24, 50. |+| MacCaption MCC `.mcc` | yes | yes | One SMPTE ST 334-2 caption distribution packet (CDP) per frame carrying caption data, omitting empty frames, in MacCaption's hex text with letter abbreviations. Writes 29.97 fps (`30DF` / `30`) with CEA-608 field 1 (the SCC schedule) **and** CEA-708 service 1 (two-window pop-on emulation). Reads CEA-608 field 1 when present, else CEA-708 service 1; time code rates 30DF, 30, 60DF, 60, 25, 24, 50. || EBU STL `.stl` | yes | yes | EBU Tech 3264-E: GSI block + 128-byte TTI blocks, `STL25.01` (25 fps) or `STL30.01` (read/written as 29.97 fps, non-drop labels), ISO/IEC 6937 Latin text (accents as diacritic + letter), italics / underline codes as `<i>` / `<u>`, extension blocks for long subtitles, start-of-programme (TCP) subtracted. || TTML `.ttml` (IMSC1) | yes | yes | Writes the IMSC1 Text profile (TTML2 namespaces): `<p>` per cue, `<br/>`, styled `<span>`s for `<i>` / `<b>` / `<u>`, `ttm:agent` speakers, exact frame times (`123f`) at the sequence rate. Reads any TTML (TTML1, TTML2, the 2006 `ttaf1` drafts): clock / frame / offset / tick times, `dur`, nested time containers (`body` / `div` begins offset and ends clip their contents; `seq` containers are read as `par` with a warning), timed spans, referenced styles. || DFXP `.dfxp` | yes | yes | TTML1 with the DFXP presentation profile; clock times in milliseconds (frame-exact after `snap_to_frames`). |@@ -64,6 +64,22 @@ Text is wrapped at 32 columns, bottom-aligned (last row 15) and centred. Charactcharacter, as the standard requires); others are dropped. If there is no room to load a captionbefore its in point (for example a caption at 00:00:00:00), it appears as soon as it is loaded.+### MCC writer++The writer merges the scheduled CEA-608 and CEA-708 frames in timestamp order. Frames without+caption data are omitted, including during a displayed caption; its explicit erase/hide command+still lands at its out point. CDP sequence counters advance per emitted packet, wrapping at 16 bits.+Export work and output size therefore follow the scheduled caption data, not the time between+cues or their duration (#2811). Empty-frame insertion is an optional MCC export behavior; see+[EZTitles' MCC export documentation, "Write empty frames"](https://eztitles.com/Webhelp/EZTitles/export_subtitles_cc_mcc.htm).+FilmCraft does not offer continuous padding for consumers that require a packet on every video frame.++Regression tests cover sparse output, both caption streams individually and together, drop-frame+and non-drop timecodes, exact cue round trips, and timestamps near the tick storage limit.+An external FFmpeg 9.0.2 decode of the two-cue regression fixture preserved text and all four+timestamps (2.002–4.004 s and 120.120–122.122 s); the combined-stream export has 29 data lines,+down from 3,614. No media fixtures are committed.+## Burn-in`burn::render_caption` lays out a caption with the track style (font size relative to a 1080-linediff --git a/crates/captions/src/mcc.rs b/crates/captions/src/mcc.rsindex ffc155df9..b5ecb243c 100644--- a/crates/captions/src/mcc.rs+++ b/crates/captions/src/mcc.rs@@ -1,4 +1,4 @@-//! MacCaption MCC (`.mcc`): closed-caption data as text, one line per video frame, each line a+//! MacCaption MCC (`.mcc`): closed-caption data as text, one line per frame carrying data, each line a//! SMPTE ST 334-2 caption distribution packet (CDP) carrying CEA-608 byte pairs and CEA-708//! (DTVCC) packets, written in hexadecimal with MacCaption's letter abbreviations.//!@@ -19,8 +19,8 @@//! bytes)), optional time code / service info sections, footer `74`, the counter again and a//! checksum that makes the packet's byte sum 0 mod 256. The ancillary packet then ends with one ST 291 checksum byte//! (low 8 bits of DID + SDID + data count + CDP bytes).-//! - **Writing** (29.97 fps, `30DF` or `30` timecode labels): every frame from the first caption-//! data to one frame after the last carries a CDP with 20 cc_data triplets: field 1 CEA-608+//! - **Writing** (29.97 fps, `30DF` or `30` timecode labels): each scheduled caption-data frame+//! carries a CDP with 20 cc_data triplets; empty frames are omitted. Each CDP contains field 1 CEA-608//! pop-on data (the same schedule as [SCC](crate::scc)), a null field-2 pair, CEA-708//! service 1 data and padding. The CEA-708 captions emulate pop-on with two windows: each//! caption is defined (hidden) and loaded into one window while the other shows, then a@@ -542,14 +542,19 @@ pub fn write(doc: &Document, drop_frame: bool) -> String {write_with(doc, drop_frame, true, true)}-/// [`write`] choosing which caption streams to include.+/// Write MCC with independently selected CEA-608 field 1 and CEA-708 service 1 streams.+///+/// Emit only scheduled data frames, preserving their timestamps and explicit erase/hide+/// commands. Empty time spans add no packets; CDP counters advance per emitted packet and+/// wrap at 16 bits. `drop_frame` selects 30DF rather than 30 timecode labels at 29.97 fps.+/// An empty document or two disabled streams produces a header with no data lines.pub fn write_with(doc: &Document, drop_frame: bool, cea608: bool, cea708: bool) -> String {let f1 = if cea608 { scc::schedule(doc) } else { BTreeMap::new() };let dtv = if cea708 { schedule_708(doc) } else { BTreeMap::new() };let mut out = String::from("File Format=MacCaption_MCC V1.0\n\n");out.push_str("///////////////////////////////////////////////////////////////////////////////////\n");out.push_str("// Closed captions written by FilmCraft: SMPTE ST 334-2 caption distribution packets\n");- out.push_str("// carrying CEA-608 field 1 and CEA-708 service 1 data, one packet per frame.\n");+ out.push_str("// carrying CEA-608 field 1 and CEA-708 service 1 data; empty frames omitted.\n");out.push_str("///////////////////////////////////////////////////////////////////////////////////\n\n");let n = doc.cues.len() as u64;let h = doc.cues.iter().fold(0xcbf2_9ce4_8422_2325u64 ^ n, |h, c| (h ^ c.start.0 as u64 ^ (c.end.0 as u64).rotate_left(17)).wrapping_mul(0x100_0000_01b3));@@ -563,15 +568,20 @@ pub fn write_with(doc: &Document, drop_frame: bool, cea608: bool, cea708: bool)));out.push_str("Creation Program=FilmCraft\n");out.push_str(&format!("Time Code Rate={}\n\n", if drop_frame { "30DF" } else { "30" }));- let first = f1.keys().next().copied().into_iter().chain(dtv.keys().next().copied()).min();- let last = f1.keys().next_back().copied().into_iter().chain(dtv.keys().next_back().copied()).max();- if let (Some(a), Some(b)) = (first, last) {- for (seq, f) in (a..=b + 1).enumerate() {- let data = FrameData { f1: f1.get(&f).copied(), dtvcc: dtv.get(&f).cloned().unwrap_or_default() };- let (_, hh, mm, ss, ff) = frames_to_fields(f, RATE, drop_frame);- let sep = if drop_frame { ';' } else { ':' };- out.push_str(&format!("{hh:02}:{mm:02}:{ss:02}{sep}{ff:02}\t{}\n", compress(&build_cdp(seq as u16, &data))));- }+ // Merge the sparse schedules without walking (or allocating for) input-derived time gaps.+ // Erase/hide commands are scheduled data too, so omitting padding preserves cue end times.+ let mut f1 = f1.into_iter().peekable();+ let mut dtv = dtv.into_iter().peekable();+ let mut seq = 0u16;+ while let Some(f) = f1.peek().map(|(f, _)| *f).into_iter().chain(dtv.peek().map(|(f, _)| *f)).min() {+ let data = FrameData {+ f1: f1.next_if(|(frame, _)| *frame == f).map(|(_, pair)| pair),+ dtvcc: dtv.next_if(|(frame, _)| *frame == f).map(|(_, pairs)| pairs).unwrap_or_default(),+ };+ let (_, hh, mm, ss, ff) = frames_to_fields(f, RATE, drop_frame);+ let sep = if drop_frame { ';' } else { ':' };+ out.push_str(&format!("{hh:02}:{mm:02}:{ss:02}{sep}{ff:02}\t{}\n", compress(&build_cdp(seq, &data))));+ seq = seq.wrapping_add(1);}out}@@ -664,10 +674,12 @@ pub fn parse(text: &str) -> Result<Document> {mod tests {use super::*;+ /// Build a caption fixture from start/end frame indices on the MCC 29.97 fps grid.fn cue(s: i64, e: i64, text: &str) -> Cue {Cue { start: RATE.tick_of(s), end: RATE.tick_of(e), text: text.into(), ..Default::default() }}+ /// Check that MCC abbreviations round-trip bytes and reject an incomplete hex pair.#[test]fn abbreviations_round_trip() {let b = [0x61, 0x01, 0x49, 0x96, 0x69, 0xfa, 0, 0, 0xfa, 0, 0, 0xfc, 0x80, 0x80, 0x00, 0x12, 0xe1, 0, 0, 0, 0xfb, 0x80, 0x80, 0xfd, 0x80, 0x80];@@ -677,6 +689,7 @@ mod tests {assert_eq!(expand("T52S5"), None);}+ /// Verify CDP framing, caption triplets, sequence fields, and both packet checksums.#[test]fn cdp_structure_and_checksum() {let d = FrameData { f1: Some((0x14, 0x20)), dtvcc: vec![(true, 0x02, 0x21), (false, 0x89, 0x01)] };@@ -697,6 +710,7 @@ mod tests {assert_eq!(t[19], [0xfa, 0, 0]);}+ /// Preserve caption text and timing through combined-stream and CEA-708-only exports.#[test]fn writes_and_reads_608_and_708() {let doc = Document {@@ -717,6 +731,55 @@ mod tests {assert!(parse("garbage").is_err());}+ /// Keep separated short captions compact while preserving cues and valid CDP packets.+ /// Exercise each stream combination with both drop-frame and non-drop timecode labels.+ #[test]+ fn export_size_depends_on_caption_data_not_empty_frames() {+ // A small reproducer: the old writer emits thousands of padding-only lines.+ let doc = Document { cues: vec![cue(60, 120, "First"), cue(3_600, 3_660, "Last")], ..Default::default() };+ for drop_frame in [false, true] {+ for (cea608, cea708) in [(true, true), (true, false), (false, true)] {+ let text = std::panic::catch_unwind(|| write_with(&doc, drop_frame, cea608, cea708)).unwrap();+ let lines: Vec<_> = text.lines().filter_map(|line| line.split_once('\t')).collect();+ assert!(lines.len() < 100, "{} lines for two short captions", lines.len());+ assert!(text.len() < 16_384);+ assert_eq!(parse(&text).unwrap().cues, doc.cues);+ for (seq, (_, data)) in lines.iter().enumerate() {+ let bytes = expand(data).unwrap();+ assert_eq!(u16::from_be_bytes([bytes[8], bytes[9]]), seq as u16);+ assert_eq!(bytes[3..76].iter().map(|&b| b as u32).sum::<u32>() % 256, 0);+ let trips = cc_triplets(&bytes).unwrap();+ assert!(trips.iter().any(|&[m, a, b]| m & 4 != 0 && (m & 3 >= 2 || a & 0x7f != 0 || b & 0x7f != 0)));+ }+ }+ }+ }++ /// Bound output for distant or long cues near the tick limit without changing their timing.+ #[test]+ fn distant_and_long_cues_round_trip_near_tick_limit() {+ let last = RATE.frame_at(crate::Tick(i64::MAX)) - 10;+ for cues in [vec![cue(60, 120, "First"), cue(last - 60, last, "Last")], vec![cue(60, last, "Long")]] {+ let doc = Document { cues, ..Default::default() };+ for drop_frame in [false, true] {+ for (cea608, cea708) in [(true, true), (true, false), (false, true)] {+ let text = std::panic::catch_unwind(|| write_with(&doc, drop_frame, cea608, cea708)).unwrap();+ assert!(text.len() < 16_384);+ assert_eq!(parse(&text).unwrap().cues, doc.cues);+ }+ }+ }+ }++ /// Emit only the MCC header when the document is empty or both streams are disabled.+ #[test]+ fn empty_schedules_have_no_data_lines() {+ let doc = Document { cues: vec![cue(60, 120, "Caption")], ..Default::default() };+ assert!(!write_with(&doc, true, false, false).contains('\t'));+ assert!(!write(&Document::default(), true).contains('\t'));+ }++ /// Prevent CEA-708 text and line breaks from overflowing a window's declared dimensions.#[test]fn decoder_keeps_708_text_within_declared_window_dimensions() {let mut decoder = Dec708::new();