diff --git a/crates/captions/src/mcc.rs b/crates/captions/src/mcc.rs index 9c685a4e7c..7169a4ec72 100644 --- a/crates/captions/src/mcc.rs +++ b/crates/captions/src/mcc.rs @@ -23,9 +23,9 @@ //! 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 -//! DisplayWindows / HideWindows pair switches them on the caption's first frame; HideWindows -//! clears it at its end. +//! caption goes into one window, deleted and defined anew first (hidden, empty, the pen at its +//! top left), while the other shows, then a DisplayWindows / HideWindows pair switches them on +//! the caption's first frame; HideWindows clears it at its end. //! - **Reading:** every CDP's `cc_data`; channel 1 CEA-608 field-1 data is decoded with the SCC //! decoder when present, otherwise CEA-708 service 1 (windows, text, CR, clear / display / //! hide / toggle / delete windows, DefineWindow visibility); a cue starts whenever the visible @@ -240,15 +240,18 @@ pub fn shown_as_708(c: char) -> char { if char_atom(c).is_some() { c } else { '?' } } -/// Atoms that define window `win` hidden and load `rows` into it. +/// Atoms that make window `win` anew, hidden, and load `rows` into it. fn load_atoms(win: u8, rows: &[String]) -> Vec> { let rc = rows.len().clamp(1, 12) as u8 - 1; let cols = rows.iter().map(|r| r.chars().count()).max().unwrap_or(1).clamp(1, 42) as u8 - 1; let mut v = vec![ + // DeleteWindows: the window holds the caption before last. Decoders differ on whether + // redefining it or ClearWindows moves the pen back (CCExtractor keeps it), but a new + // window starts empty with the pen at its top left, for the same bytes as ClearWindows. + vec![0x8c, 1 << win], // DefineWindow: hidden, row/column lock, relative position 90 % down / 50 % across, // anchored at its bottom centre, rows / columns, window style 1, pen style 1 vec![0x98 + win, 0x18, 0x80 | 90, 50, 7 << 4 | rc, cols, 1 << 3 | 1], - vec![0x88, 1 << win], // ClearWindows ]; for (i, r) in rows.iter().enumerate() { if i > 0 { @@ -898,4 +901,92 @@ mod tests { assert!(decoder.cues.iter().map(|c| c.2.len()).sum::() <= MAX_708_TEXT_BYTES); assert!(decoder.open.is_none()); } + + /// Start every CEA-708 caption at the top left of its window, also for a decoder that sets + /// the pen only when a window is created and moves it only on SetPenLocation, carriage return + /// and text. CCExtractor keeps the pen through ClearWindows and a redefined window, so the + /// third caption below once started at the end of the first one, past its smaller window. + #[test] + fn each_708_caption_starts_at_its_window_top_left() { + let doc = Document { + cues: vec![ + cue(60, 120, "Por que você não admite que\nminha mão robótica te assusta?"), + cue(150, 200, "Você partiu meu coração!"), + cue(230, 250, "Eu sei."), + cue(260, 300, "Você me esqueceu na Terra."), + ], + ..Default::default() + }; + // Service 1 bytes in order, out of the scheduled DTVCC packets. + let mut packets: Vec> = Vec::new(); + for (start, a, b) in schedule_708(&doc).into_values().flatten() { + if start { + packets.push(Vec::new()); + } + packets.last_mut().unwrap().extend([a, b]); + } + let mut s = Vec::new(); + for p in &packets { + let mut body = &p[1..]; + while let [h, rest @ ..] = body { + let n = (h & 0x1f) as usize; + if h >> 5 == 1 { + s.extend_from_slice(&rest[..n]); + } + body = &rest[n..]; + } + } + // Pen (row, column), size (rows, columns) and whether defined, per window; where each + // caption's text starts (its first character after a DefineWindow). + let (mut pen, mut size, mut defined, mut cur) = ([(0usize, 0usize); 8], [(0usize, 0usize); 8], [false; 8], 0usize); + let (mut starts, mut loaded, mut i) = (Vec::new(), false, 0); + while let Some(&c) = s.get(i) { + let mut text = false; + i += match c { + 0x80..=0x87 => { + cur = (c - 0x80) as usize; + 1 + } + 0x8c => { + (0..8).filter(|w| s[i + 1] & 1 << w != 0).for_each(|w| defined[w] = false); + 2 + } + 0x88..=0x8b => 2, + 0x92 => { + pen[cur] = (s[i + 1] as usize, s[i + 2] as usize); + 3 + } + 0x98..=0x9f => { + cur = (c - 0x98) as usize; + if !std::mem::replace(&mut defined[cur], true) { + pen[cur] = (0, 0); + } + size[cur] = ((s[i + 4] & 0x0f) as usize + 1, (s[i + 5] & 0x3f) as usize + 1); + loaded = true; + 7 + } + 0x0d => { + pen[cur] = (pen[cur].0 + 1, 0); + 1 + } + 0x10 => { + text = true; + 2 + } + 0x20..=0x7f | 0xa0..=0xff => { + text = true; + 1 + } + _ => 1, + }; + if text { + if std::mem::take(&mut loaded) { + starts.push((cur, pen[cur])); + } + assert!(pen[cur].0 < size[cur].0 && pen[cur].1 < size[cur].1, "window {cur} {:?}: text at {:?}", size[cur], pen[cur]); + pen[cur].1 += 1; + } + } + assert_eq!(starts, [(0, (0, 0)), (1, (0, 0)), (0, (0, 0)), (1, (0, 0))]); + } } diff --git a/crates/captions/src/ttml.rs b/crates/captions/src/ttml.rs index f1b8fb05b1..3755202db9 100644 --- a/crates/captions/src/ttml.rs +++ b/crates/captions/src/ttml.rs @@ -769,8 +769,7 @@ mod tests { #[test] fn only_xml_whitespace_collapses_in_cue_text() { - let xml = - "

\t 10\u{a0}km\u{2009}a\u{3000}b \t\r\n c \n

"; + let xml = "

\t 10\u{a0}km\u{2009}a\u{3000}b \t\r\n c \n

"; let doc = parse(xml).unwrap(); assert_eq!(doc.cues[0].text, "10\u{a0}km\u{2009}a\u{3000}b c"); let xml = "

\u{a0}x
y\u{a0}

"; diff --git a/crates/captions/tests/it/main.rs b/crates/captions/tests/it/main.rs index dca828132e..2efd793cb0 100644 --- a/crates/captions/tests/it/main.rs +++ b/crates/captions/tests/it/main.rs @@ -1,4 +1,5 @@ mod limits; +mod mcc_708_timing; mod roundtrip; mod ttml_body_timing; mod ttml_clock_overflow; diff --git a/crates/captions/tests/it/mcc_708_timing.rs b/crates/captions/tests/it/mcc_708_timing.rs new file mode 100644 index 0000000000..77289f0b08 --- /dev/null +++ b/crates/captions/tests/it/mcc_708_timing.rs @@ -0,0 +1,27 @@ +//! CEA-708 timing of back-to-back one-frame captions in MCC. + +use craft_captions::{Cue, Document, Format, mcc, parse}; + +/// A frame carries 18 DTVCC pairs, and with one-frame captions back to back it holds one +/// caption's display (3 pairs) and the next one's load. A load of up to 19 characters (15 pairs) +/// fits beside it. A SetPenLocation in every load made it 16 pairs from 17 characters on, and +/// the display a frame late. +#[test] +fn back_to_back_one_frame_708_captions_keep_their_frames() { + for len in [1, 16, 17, 18, 19] { + let cues: Vec = (0..4i64) + .map(|k| Cue { + start: mcc::RATE.tick_of(60 + k), + end: mcc::RATE.tick_of(61 + k), + // a different text each time, so the reader sees every change + text: ('a'..='z').cycle().skip(k as usize).take(len).collect(), + ..Default::default() + }) + .collect(); + let doc = Document { cues, ..Default::default() }; + let back = parse(mcc::write_with(&doc, true, false, true, "test").as_bytes(), Format::Mcc).unwrap(); + let frames: Vec<(i64, i64)> = back.cues.iter().map(|c| (mcc::RATE.frame_at(c.start), mcc::RATE.frame_at(c.end))).collect(); + assert_eq!(frames, [(60, 61), (61, 62), (62, 63), (63, 64)], "{len} characters"); + assert_eq!(back.cues, doc.cues, "{len} characters"); + } +} diff --git a/crates/captions/tests/it/roundtrip.rs b/crates/captions/tests/it/roundtrip.rs index 2371441663..effb81e9af 100644 --- a/crates/captions/tests/it/roundtrip.rs +++ b/crates/captions/tests/it/roundtrip.rs @@ -282,7 +282,8 @@ fn tight_pop_on_load_can_cross_the_previous_caption_end() { #[test] fn interrupted_load_keeps_extended_characters_and_repeated_basic_pairs() { - for text in ["A second caption that needs more time to load", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ\nÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ"] + for text in + ["A second caption that needs more time to load", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ\nÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ"] { let doc = Document { cues: vec![