From df4020f31843d0e500ac67c0bd008b69cfd81f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 06:04:23 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(engine):=20the=20words=20a=20p?= =?UTF-8?q?roject=20places=20are=20in=20the=20file=20it=20saves?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `.edith` grows a `sub` line -- `sub ` -- and a bare `sub ` for a subtitle lane holding nothing, written in the lane's own place among the video and audio ones so the display order a load rebuilds is the one that was saved. Both of a caption's clocks are on the line: the placement in timeline frames like everything else, the window in the microseconds its cues are timed in. MAGIC is `edith 15`. `Project::without_orphan_sources` stops filtering the subtitle lanes out of its `Parts` and `Project::lane_gains` stops filtering them out of its gains -- the two are zipped by `edith::emit`, so they were only ever allowed to filter in step, and now neither does. What is *on* those lanes travels beside them in the new `Project::lane_subs`, one list per lane exactly as the gains are, and comes back through `Project::with_subs`, which is `from_parts` for words: it refuses a caption on a lane that is not a subtitle lane, an empty span or window, a track the palette does not have, a span past the last frame there is, and a lane that overlaps itself. The parser refuses the same file by line number. A v14 project is one that named subtitle files and could place none of them: it loads with its palette whole and no subtitle lane at all, which is what it always meant, and re-saves as v15 having invented neither. Also fixes a version-bump straggler in tests/silence.rs, which asserted `edith 13` against a `edith 14` writer and was red before this change. --- crates/engine/src/edith.rs | 217 +++++++++++++++++++++++++------ crates/engine/src/playback.rs | 9 +- crates/engine/src/project.rs | 105 +++++++++++---- crates/engine/tests/export.rs | 2 +- crates/engine/tests/lanes.rs | 5 +- crates/engine/tests/saveload.rs | 12 +- crates/engine/tests/silence.rs | 3 +- crates/engine/tests/subtitles.rs | 176 ++++++++++++++++++++++++- 8 files changed, 457 insertions(+), 72 deletions(-) diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index 7f39920..0074d50 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -1,7 +1,7 @@ //! The project file: a line of text per thing, and nothing else. //! //! ```text -//! edith 14 +//! edith 15 //! playhead 90 //! resolution 1920 1080 //! fps 30.0 @@ -20,6 +20,8 @@ //! audio 1 0 0 120 0 0 0 - fit 1000 //! video 2 120 0 120 1 - - - fill 2000 //! audio 2 +//! sub 1 0 60 0 500000 1500000 +//! sub 2 //! gain audio 1 -3.0 //! ``` //! @@ -127,7 +129,24 @@ //! is the only way an empty lane could still be there on the way back. A lane //! number may not skip one of its kind. //! -//! **Version 13** was this without the `encoder` line: an export of such a +//! A subtitle lane is a lane like the other two and is written in its place +//! among them, `sub` for its kind: `sub +//! ` ([`crate::project::SubClip`]) -- where the caption sits on the +//! timeline and how many frames it covers, then which `subtitle` line's words +//! it shows (by position, the first one is track 0) and the half-open window of +//! that track it shows, in *microseconds*, which is the clock the cues +//! themselves are timed in. Two clocks on one line because a subtitle has two: +//! a placement is in timeline frames like everything else, and a window is in +//! the file's own time whatever the timeline runs at. A lane holding nothing +//! has the bare `sub ` line for the reason a video one does, and the +//! track a caption names has to be one the file declared -- a `sub` line naming +//! track 3 of a two-track palette is refused here, as a clip naming a source +//! that is not there is. +//! +//! **Version 14** was this without the `sub` lines: such a project holds no +//! subtitle lane at all -- its `subtitle` lines are the palette, and where the +//! words of it were shown was nowhere in the file. +//! **Version 13** was that without the `encoder` line: an export of such a //! project takes the hardware seat where there is one and the software encoder //! everywhere else, which is the only choice those projects had. //! **Version 12** was that without the `autoproxy` line: such a project makes a @@ -158,7 +177,8 @@ //! what a v1 timeline meant, and an older file simply equalizes and grades //! nothing, and an older one plays everything at real time, and an older one //! mixes flat, and an older one shows no subtitles, and an older one is shown -//! in the reference rendition -- and saving any of them writes v14. An older +//! in the reference rendition, and an older one places none of the words it +//! names -- and saving any of them writes v15. An older //! reader refuses a newer file by name. //! //! Text because an edit list is a few integers and a path, and a path is @@ -188,13 +208,14 @@ use std::path::{Path, PathBuf}; use crate::color::ColorParams; use crate::eq::{Band, BandKind, EqParams}; use crate::limiter::Limiter; -use crate::project::{Clip, Lane, LaneKind, Source, Speed}; +use crate::project::{Clip, Lane, LaneKind, Source, Speed, SubClip}; use crate::scale::FitPolicy; use crate::subtitle::SubtitleTrack; /// What [`save`] writes. Read support goes back to `edith 1`; see the module /// docs for what those dialects looked like. -const MAGIC: &[u8] = b"edith 14"; +const MAGIC: &[u8] = b"edith 15"; +const MAGIC_V14: &[u8] = b"edith 14"; const MAGIC_V13: &[u8] = b"edith 13"; const MAGIC_V12: &[u8] = b"edith 12"; const MAGIC_V11: &[u8] = b"edith 11"; @@ -220,6 +241,11 @@ pub struct Document { /// [`crate::Project::from_parts`] takes them in. Exactly two -- `V1` then /// `A1` -- for every dialect before v4. pub lanes: Vec<(LaneKind, Vec)>, + /// What is placed on each lane, in the order `lanes` is in and as long as + /// it -- the `sub` lines. Empty for every video and audio lane, which hold + /// no [`SubClip`], and for every lane of every dialect before v15, which + /// placed no words anywhere ([`crate::Project::with_subs`] takes it back). + pub subs: Vec>, /// The equalizer table [`Clip::eq`] indexes into, in file order. Empty for /// every dialect before v5. pub eq: Vec, @@ -273,13 +299,15 @@ pub struct Document { /// Writes the project to `path`, atomically. `sources`, `eq` and `color` should /// already be orphan-free ([`crate::Project::without_orphan_sources`]); /// `gains` is one dB per lane in the same order the lanes are in -/// ([`crate::Project::lane_gains`]). +/// ([`crate::Project::lane_gains`]) and `subs` is one list of placements per +/// lane in that same order ([`crate::Project::lane_subs`]). #[allow(clippy::too_many_arguments)] pub fn save( path: &Path, sources: &[Source], lanes: &[(LaneKind, Vec)], gains: &[f32], + subs: &[Vec], subtitles: &[SubtitleTrack], eq: &[EqParams], color: &[ColorParams], @@ -303,8 +331,8 @@ pub fn save( let result = std::fs::File::create(&part) .and_then(|mut f| { f.write_all(&emit( - &dir, sources, lanes, gains, subtitles, eq, color, resolution, fps, tone, proxy, - auto_proxy, encoder, limiter, playhead, + &dir, sources, lanes, gains, subs, subtitles, eq, color, resolution, fps, tone, + proxy, auto_proxy, encoder, limiter, playhead, ))?; f.sync_all() }) @@ -341,6 +369,7 @@ fn emit( sources: &[Source], lanes: &[(LaneKind, Vec)], gains: &[f32], + subs: &[Vec], subtitles: &[SubtitleTrack], eq: &[EqParams], color: &[ColorParams], @@ -448,8 +477,8 @@ fn emit( // Lane by lane rather than interleaved by time: a lane reads as a list, the // parser gets its sortedness check for free from the order it is in, and // the lanes come back out in the order they are displayed in. - let (mut video, mut audio) = (0, 0); - for (kind, clips) in lanes { + let (mut video, mut audio, mut subtitle) = (0, 0, 0); + for (at, (kind, clips)) in lanes.iter().enumerate() { let (keyword, ord) = match kind { LaneKind::Video => { video += 1; @@ -459,13 +488,31 @@ fn emit( audio += 1; ("audio", audio) } - // Unreachable by construction: `Project::without_orphan_sources` - // leaves the subtitle lanes out of the [`Parts`] it hands a save, - // because this format has no line to write a `SubClip` on yet and a - // reader that met the keyword would refuse the file. Skipped rather - // than emitted, and the corner-cut is named at the filter. - LaneKind::Subtitle => continue, + LaneKind::Subtitle => { + subtitle += 1; + ("sub", subtitle) + } }; + // A subtitle lane holds no `Clip` at all ([`LaneKind::Subtitle`]): what + // is on it is in `subs`, one list per lane and in step with this one, + // and it is written here so the lane comes back in its place among the + // others. + if *kind == LaneKind::Subtitle { + let placed = subs.get(at).map_or(&[][..], Vec::as_slice); + if placed.is_empty() { + out.extend_from_slice(format!("{keyword} {ord}\n").as_bytes()); + } + for s in placed { + out.extend_from_slice( + format!( + "{keyword} {ord} {} {} {} {} {}\n", + s.start, s.frames, s.track, s.in_us, s.out_us + ) + .as_bytes(), + ); + } + continue; + } // A lane is declared by its clips; one holding nothing has to say so on // a line of its own, or the lane itself would not survive the round trip. if clips.is_empty() { @@ -503,8 +550,10 @@ fn emit( audio += 1; ("audio", audio) } - // Never here, for the reason above -- and a subtitle lane has no - // volume to write anyway (`Project::set_lane_gain_db` refuses one). + // In the list -- the two are zipped, so it has to be -- and never + // written: a subtitle lane has no volume at all + // (`Project::set_lane_gain_db` refuses one), so its entry is the + // `0.0` no lane writes a line for. LaneKind::Subtitle => continue, }; if db != 0.0 { @@ -524,8 +573,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { // The dialects that wrote a source line without its stream field. Reading // one is the whole of what "an old project still opens" means here. let streamless = v1 || first == MAGIC_V2; - // The one that says which encoder an export takes... - let v14 = first == MAGIC; + // The one that places the words it names... + let v15 = first == MAGIC; + // ...the ones that say which encoder an export takes... + let v14 = v15 || first == MAGIC_V14; // ...the ones that say whether the stand-ins are made by themselves... let v13 = v14 || first == MAGIC_V13; // ...the ones that say whether it is cut on stand-ins... @@ -564,6 +615,11 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { } else { vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, Vec::new())] }, + // Resized to the lanes as they arrive, and once more at the end, so a + // caller may zip the two exactly as it zips the gains. Nothing before + // v15 places a word anywhere: those projects have the palette and no + // subtitle lane to put any of it on. + subs: Vec::new(), // Nothing before v5 equalizes anything, and nothing before v6 grades. eq: Vec::new(), color: Vec::new(), @@ -877,6 +933,66 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { doc.lanes[0].1.push(clip); doc.lanes[1].1.push(clip); } + // A lane line like the two below it, in its place among them, and + // read into `subs` rather than into the clips: the lane holds + // words, and no media path may ever meet one + // ([`crate::project::LaneKind::Subtitle`]). + b"sub" if v15 => { + let at = rest.iter().position(|&b| b == b' '); + let ord = number(&rest[..at.unwrap_or(rest.len())], n)?; + let rest = &rest[at.map_or(rest.len(), |at| at + 1)..]; + let at = lane_of(&mut doc.lanes, LaneKind::Subtitle, ord, n)?; + doc.subs.resize(doc.lanes.len(), Vec::new()); + // The bare line is the empty lane's whole existence, exactly as + // it is for a video one. + if rest.is_empty() { + continue; + } + let f = fields(rest, 5, "sub", n)?; + let sub = SubClip { + start: number(f[0], n)?, + frames: number(f[1], n)?, + track: number(f[2], n)? as usize, + in_us: micros(f[3], n)?, + out_us: micros(f[4], n)?, + }; + // The never-empty invariant at both clocks, which is what + // [`crate::Project::place_sub`] refuses on the live timeline. + if sub.frames == 0 || sub.out_us <= sub.in_us || sub.in_us < 0 { + return Err(format!( + "line {n}: caption at {} is empty: {} frames of [{}, {})", + sub.start, sub.frames, sub.in_us, sub.out_us + ) + .into()); + } + // The palette bound, which is a clip's source bound for words: + // the `subtitle` lines are written before these and a caption + // names one of them by position. + if sub.track >= doc.subtitles.len() { + return Err(format!( + "line {n}: caption names subtitle track {} of {}", + sub.track, + doc.subtitles.len() + ) + .into()); + } + if sub.start.checked_add(sub.frames).is_none() { + return Err(format!( + "line {n}: caption at {} runs past the last frame there is", + sub.start + ) + .into()); + } + let lane = &mut doc.subs[at]; + if lane.last().is_some_and(|prev| prev.end() > sub.start) { + return Err(format!( + "line {n}: caption at {} overlaps the one before it, or comes before it", + sub.start + ) + .into()); + } + lane.push(sub); + } b"video" | b"audio" if !v1 => { let kind = match keyword { b"video" => LaneKind::Video, @@ -945,8 +1061,10 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { } } } - // One per lane whatever the file said, so a caller may zip the two. + // One per lane whatever the file said, so a caller may zip the two -- and + // the same for the placements, whose lanes are the very same list. doc.gains.resize(doc.lanes.len(), 0.0); + doc.subs.resize(doc.lanes.len(), Vec::new()); // A file whose lanes are all empty *is* a project -- the emptied timeline, // saved. What it may not be is laneless; [`crate::Project::from_parts`] // refuses that one, by the name it has there. @@ -1163,6 +1281,21 @@ fn number(field: &[u8], line: usize) -> crate::Result { } } +/// A `sub` line's window field: microseconds, which is the clock a cue is timed +/// in ([`crate::subtitle::Cue`]) and the one thing in this format counted in +/// something other than frames. Signed as the cues are, and the negative half +/// is refused by the never-empty check at the line, not here. +fn micros(field: &[u8], line: usize) -> crate::Result { + match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) { + Some(n) => Ok(n), + None => Err(format!( + "line {line}: {:?} is not a number", + String::from_utf8_lossy(field) + ) + .into()), + } +} + /// The two bytes a line-based format cannot hold, and the escape byte itself. fn escape(path: &Path, out: &mut Vec) { for &b in path.as_os_str().as_bytes() { @@ -1223,6 +1356,7 @@ mod tests { lanes, &[], &[], + &[], eq, color, resolution, @@ -1268,6 +1402,7 @@ mod tests { &sources, &lanes, &[], + &[], &tracks, &[], &[], @@ -1305,7 +1440,7 @@ mod tests { video 1 0 0 30 0 - - - fit 1000\n"; let old = parse(v9, &dir).expect("v9 still loads"); assert_eq!(old.subtitles, Vec::new()); - assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 14\n")); + assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 15\n")); // ...and the line itself is not a v9 line: a dialect may not be mixed. let mixed = parse(b"edith 9\nsource 0 a.mp4\nsubtitle - subs.srt\n", &dir) .unwrap_err() @@ -1337,6 +1472,7 @@ mod tests { &[], &[], &[], + &[], (1280, 720), None, crate::tonemap::Preset::default(), @@ -1399,6 +1535,7 @@ mod tests { &[], &[], &[], + &[], (1280, 720), None, crate::tonemap::Preset::default(), @@ -1463,6 +1600,7 @@ mod tests { &[], &[], &[], + &[], (1280, 720), None, crate::tonemap::Preset::default(), @@ -1510,7 +1648,7 @@ mod tests { assert_eq!(mixed, "line 3: unknown keyword \"encoder\""); // Anything but the three names is a corrupt line, by name. for line in ["encoder\n", "encoder gpu\n", "encoder Hardware\n"] { - let file = format!("edith 14\nsource 0 a.mp4\n{line}"); + let file = format!("edith 15\nsource 0 a.mp4\n{line}"); assert!( parse(file.as_bytes(), &dir).is_err(), "{line:?} is not an encoder line" @@ -1534,6 +1672,7 @@ mod tests { &[], &[], &[], + &[], (1280, 720), None, preset, @@ -1634,7 +1773,7 @@ mod tests { let bytes = flat(&dir, &sources, &lanes, 12); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ source 2 /elsewhere/b.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ audio 1 0 0 30 0 0 - - fit 1000\n", @@ -1671,7 +1810,7 @@ mod tests { let bytes = flat(&dir, &sources, &lanes, 7); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 4 - - fit 1000\naudio 1\n\ video 2 40 0 10 0 - - - fit 1000\naudio 2 0 0 30 0 4 - - fit 1000\n", "an empty lane is a line of its own; everything else is its clips" @@ -1779,7 +1918,7 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &eq, &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ eq 16777215.0:-0.1:3.918315e-39:hs\n\ eq\n\ @@ -1883,7 +2022,7 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &color, (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ color 0.1:1.2:0.9:-0.3\n\ color -1e-7:16777215.0:3.918315e-39:-0.0\n\ color 0.0:1.0:1.0:0.0\n\ @@ -1948,7 +2087,7 @@ mod tests { (1280, 720), old.playhead )), - "edith 14\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ eq 80.0:-3.0:0.707:ls\n\ video 1 0 0 30 0 0 0 - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" ); @@ -1986,7 +2125,7 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 - - - fit 2000\nvideo 1 15 30 40 0 - - - fit 250\n\ audio 1 0 0 30 0 - - - fit 2000\n", "the rate is the clip line's last field, in thousandths" @@ -2015,7 +2154,7 @@ mod tests { (1280, 720), old.playhead )), - "edith 14\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" ); // A rate outside what the editor can set is a corrupt line, by name. @@ -2053,6 +2192,7 @@ mod tests { &[], &[], &[], + &[], (1280, 720), Some(23.976023976023978), crate::tonemap::Preset::default(), @@ -2064,7 +2204,7 @@ mod tests { ); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\ limiter -1.5 on\nsource 0 a.mp4\n\ video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n\ audio 2 0 0 30 0 - - - fit 1000\n\ @@ -2096,7 +2236,7 @@ mod tests { (1280, 720), old.playhead )), - "edith 14\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" ); @@ -2260,7 +2400,7 @@ mod tests { assert!(old.eq.is_empty(), "nothing before v5 equalizes anything"); assert_eq!( String::from_utf8_lossy(&flat(&dir, &old.sources, &old.lanes, old.playhead)), - "edith 14\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ source 2 /elsewhere/b.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ audio 1 0 0 30 0 0 - - fit 1000\n" @@ -2313,7 +2453,7 @@ mod tests { let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); assert_eq!( String::from_utf8_lossy(&v5), - "edith 14\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 15\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ source 0 /elsewhere/b.mp4\n\ video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ audio 1 0 0 30 0 0 - - fit 1000\n", @@ -2345,7 +2485,7 @@ mod tests { // Saved again it is the current version, which round-trips to the // same document. let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); - assert!(v5.starts_with(b"edith 14\n")); + assert!(v5.starts_with(b"edith 15\n")); let again = parse(&v5, &dir).expect("v5 parses"); assert_eq!(again.lanes, back.lanes); // A dialect may not be mixed: lane lines under v1, `clip` under v2. @@ -2440,6 +2580,7 @@ mod tests { &[], &[], &[], + &[], (1280, 720), None, crate::tonemap::Preset::default(), @@ -2453,7 +2594,7 @@ mod tests { let bytes = std::fs::read(&path).expect("read back"); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 14\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" ); // Loading rejoins the *given* directory, so the file is reached by the @@ -2498,10 +2639,10 @@ mod tests { #[test] fn a_wrong_first_line_is_refused_by_name() { let dir = PathBuf::from("/proj"); - let err = parse(b"edith 15\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) + let err = parse(b"edith 16\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) .unwrap_err() .to_string(); - assert_eq!(err, "line 1: unsupported version 15"); + assert_eq!(err, "line 1: unsupported version 16"); for junk in [&b""[..], b"{}\n", b"source a.mp4\n"] { assert_eq!( parse(junk, &dir).unwrap_err().to_string(), diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 3e5225b..a9d8c0c 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -969,7 +969,11 @@ impl PlaybackSession { let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)? .with_mix(&doc.gains, doc.limiter) .with_tone(doc.tone) - .with_subtitles(subtitles); + // The palette before what is placed on it: a caption names a row of + // it, and [`Project::with_subs`] refuses one naming a row that is + // not there. + .with_subtitles(subtitles) + .with_subs(doc.subs)?; let span = project.composite_span_at(0); // Last, because it is the one thing here that cannot be taken back: the // feeder thread outlives the `Audio` value (it holds its own clones) and @@ -1260,6 +1264,9 @@ impl PlaybackSession { // project's, not this machine's (the monitoring volume is not // written and never will be). &self.project.lane_gains(), + // ...and what is placed on each lane, in the same order again: the + // captions, which are the only thing a subtitle lane holds. + &self.project.lane_subs(), // The subtitle tracks with them, by reference: which file the cues // are in, never the cues (see [`crate::edith`]). self.project.subtitles(), diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 50d95f7..e25f160 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1295,20 +1295,11 @@ impl Project { } ( sources, - lanes - .into_iter() - // corner-cut: the subtitle lanes are left out, because [`Parts`] - // is `(LaneKind, Vec)` and a subtitle lane's contents are - // [`SubClip`]s -- a `.edith` has no line to write one on yet, so - // a save that emitted the lane would write a track with nothing - // in it and a *reader* that has never seen the keyword would - // refuse the file. Upgrade path is the format slice that owns - // it: a `sub` line per placement (lane ord, start, frames, - // track, window) beside the `video`/`audio` ones, and this - // filter goes. - .filter(|l| l.kind != LaneKind::Subtitle) - .map(|l| (l.kind, l.clips)) - .collect(), + // Every lane, subtitle ones included: they hold no [`Clip`], so what + // comes out for one is an empty list, and its *place* in the display + // order is what a save writes it for. What is on it travels beside + // this, in [`lane_subs`](Project::lane_subs), as the gains do. + lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), eq, color, ) @@ -1352,15 +1343,85 @@ impl Project { /// Every lane's volume in dB, in display order -- what a save writes, /// beside the lanes [`Project::without_orphan_sources`] hands back. /// - /// The *same* lanes as that call, subtitle ones left out: a save zips the - /// two lists, so a lane that is in one and not the other would hand every - /// track below it the volume of the track above. + /// The *same* lanes as that call, subtitle ones included at the `0.0` they + /// are pinned to ([`set_lane_gain_db`](Project::set_lane_gain_db) refuses + /// one): a save zips the two lists, so a lane that is in one and not the + /// other would hand every track below it the volume of the track above. pub fn lane_gains(&self) -> Vec { - self.lanes - .iter() - .filter(|l| l.kind != LaneKind::Subtitle) - .map(|l| l.gain_db) - .collect() + self.lanes.iter().map(|l| l.gain_db).collect() + } + + /// What is placed on every lane, in display order and in step with + /// [`without_orphan_sources`](Project::without_orphan_sources) exactly as + /// [`lane_gains`](Project::lane_gains) is -- what a save writes as its `sub` + /// lines. Empty for every video and audio lane, which hold no [`SubClip`]. + /// + /// Nothing is renumbered on the way out: a [`SubClip`] names a *palette* + /// row ([`Project::subtitles`]) and the palette is saved whole, so unlike a + /// clip's source there is no orphan to prune and no index to move. + pub fn lane_subs(&self) -> Vec> { + self.lanes.iter().map(|l| l.subs.clone()).collect() + } + + /// The placements a *load* puts back, one list per lane in display order -- + /// the door [`with_mix`](Project::with_mix) is, and no undo step for the + /// same reason. Called *after* + /// [`with_subtitles`](Project::with_subtitles), whose palette a placement + /// names. + /// + /// The subtitle half of [`from_parts`](Project::from_parts) and checked + /// like it, by name and in release, because it is the same untrusted file: + /// a placement on a lane that is not a subtitle lane, an empty span or an + /// empty window, a track the palette does not have, a span running past the + /// last frame there is, and a lane that is out of order or overlaps itself + /// ([`place_sub`] refuses every one of those on the live timeline). + /// + /// [`place_sub`]: Project::place_sub + pub fn with_subs(mut self, subs: Vec>) -> crate::Result { + let names = handles(&self.lanes); + let palette = self.subtitles.len(); + for ((data, lane), list) in self.lanes.iter_mut().zip(names).zip(subs) { + let name = lane.label(); + if list.is_empty() { + continue; + } + if data.kind != LaneKind::Subtitle { + return Err(format!( + "{name} is not a subtitle track and holds {} caption(s): words go on a \ + subtitle track", + list.len() + ) + .into()); + } + for s in &list { + if s.frames == 0 || s.out_us <= s.in_us || s.in_us < 0 { + return Err(format!( + "{name} caption at {} is empty: {} frames of [{}, {})", + s.start, s.frames, s.in_us, s.out_us + ) + .into()); + } + if s.track >= palette { + return Err(format!( + "{name} caption at {} names subtitle track {} of {palette}", + s.start, s.track + ) + .into()); + } + if s.start.checked_add(s.frames).is_none() { + return Err(format!( + "{name} caption at {} runs past the last frame there is", + s.start + ) + .into()); + } + } + if !subs_sorted_disjoint(&list) { + return Err(format!("the {name} lane is out of order or overlaps itself").into()); + } + data.subs = list; + } + Ok(self) } /// The mix settings a *load* puts back: lane volumes in display order (a diff --git a/crates/engine/tests/export.rs b/crates/engine/tests/export.rs index 8290c10..fce57a4 100644 --- a/crates/engine/tests/export.rs +++ b/crates/engine/tests/export.rs @@ -985,7 +985,7 @@ fn a_proxy_is_picture_only_every_frame_a_starting_point_and_cached() { session.set_proxies(true); session.save_project(&project).expect("save"); let text = std::fs::read_to_string(&project).expect("read the project back"); - assert!(text.starts_with("edith 14\n"), "{text}"); + assert!(text.starts_with("edith 15\n"), "{text}"); assert!(text.contains("\nproxy on\n"), "{text}"); let reopened = PlaybackSession::open_project(&project).expect("load"); assert!(reopened.proxies(), "the switch came back on"); diff --git a/crates/engine/tests/lanes.rs b/crates/engine/tests/lanes.rs index c53784b..2609d15 100644 --- a/crates/engine/tests/lanes.rs +++ b/crates/engine/tests/lanes.rs @@ -463,8 +463,9 @@ fn a_subtitle_lane_is_a_peer_and_every_media_path_refuses_it() { assert_eq!(project.audio_gains().len(), 1); assert_eq!( project.lane_gains().len(), - 2, - "a save's gain list holds the two media lanes, in step with its lane list" + 3, + "a save's gain list holds one entry per lane, in step with its lane list -- \ + this one's at the 0.0 a subtitle lane is pinned to" ); // The clip-shaped calls: the lane holds no `Clip`, so each refuses by the diff --git a/crates/engine/tests/saveload.rs b/crates/engine/tests/saveload.rs index cd6d6cd..f75f311 100644 --- a/crates/engine/tests/saveload.rs +++ b/crates/engine/tests/saveload.rs @@ -291,7 +291,7 @@ fn a_version_1_project_loads_fully_grouped_and_saves_as_version_7() { let v2 = dir.join("new.edith"); loaded.save_project(&v2).expect("save"); let text = std::fs::read_to_string(&v2).expect("read back"); - assert!(text.starts_with("edith 14\n"), "{text}"); + assert!(text.starts_with("edith 15\n"), "{text}"); assert!( text.contains("\nresolution 1280 720\n"), "a project with no resolution of its own is saved at source 0's: {text}" @@ -379,7 +379,7 @@ fn a_version_4_project_holds_more_than_two_lanes() { loaded.save_project(&again).expect("save"); assert_eq!( std::fs::read_to_string(&again).expect("read back"), - "edith 14\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ video 1 0 0 30 0 3 - - fit 1000\naudio 1\n\ video 2 40 0 20 0 - - - fit 1000\naudio 2 0 0 30 0 3 - - fit 1000\n", "a four-lane project is written as it was read, three versions on" @@ -409,7 +409,7 @@ fn a_version_5_project_carries_per_clip_equalizers() { loaded.save_project(&again).expect("save"); assert_eq!( std::fs::read_to_string(&again).expect("read back"), - "edith 14\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ eq 12000.0:6.25:0.5:hs\n\ video 1 0 0 30 0 - 0 - fit 1000\nvideo 1 30 30 60 0 - 1 - fit 1000\n\ @@ -444,7 +444,7 @@ fn a_version_6_project_carries_per_clip_colours() { loaded.save_project(&again).expect("save"); assert_eq!( std::fs::read_to_string(&again).expect("read back"), - "edith 14\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ + "edith 15\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ eq 80.0:-3.0:0.707:ls\n\ color 0.1:1.2:0.9:-0.3\n\ color -0.25:1.0:0.0:0.5\n\ @@ -494,7 +494,7 @@ fn a_version_7_project_carries_a_resolution_and_fit_policies() { loaded.save_project(&again).expect("save"); assert_eq!( std::fs::read_to_string(&again).expect("read back"), - "edith 14\nplayhead 0\nresolution 960 720\nfps 30.0\nsource 0 test_av.mp4\n\ + "edith 15\nplayhead 0\nresolution 960 720\nfps 30.0\nsource 0 test_av.mp4\n\ video 1 0 0 30 0 - - - fill 1000\nvideo 1 30 30 60 0 - - - center 1000\n\ audio 1 0 0 60 0 - - - fit 1000\n", "a v7 project is written back as the v8 it now is: the same clips, each \ @@ -752,7 +752,7 @@ fn malformed_files_are_numbered_errors_and_never_panics() { for (text, want) in [ ( - "edith 15\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n", + "edith 16\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n", "line 1", ), // An eq index the table does not hold, and a band shape there is none. diff --git a/crates/engine/tests/silence.rs b/crates/engine/tests/silence.rs index d61285c..ccde715 100644 --- a/crates/engine/tests/silence.rs +++ b/crates/engine/tests/silence.rs @@ -370,6 +370,7 @@ fn scanning_a_clip_and_cutting_what_it_finds() { &sources, &lanes, &p.lane_gains(), + &p.lane_subs(), p.subtitles(), &eq, &color, @@ -385,7 +386,7 @@ fn scanning_a_clip_and_cutting_what_it_finds() { .expect("save"); let text = std::fs::read_to_string(&file).expect("read it back"); assert!( - text.starts_with("edith 13"), + text.starts_with("edith 15"), "{:?}", &text[..16.min(text.len())] ); diff --git a/crates/engine/tests/subtitles.rs b/crates/engine/tests/subtitles.rs index 69c0177..0a8d7df 100644 --- a/crates/engine/tests/subtitles.rs +++ b/crates/engine/tests/subtitles.rs @@ -260,7 +260,7 @@ fn a_project_keeps_its_subtitles_across_a_save() { let project = dir.join("cut.edith"); session.save_project(&project).expect("save"); let text = std::fs::read_to_string(&project).expect("read the project back"); - assert!(text.starts_with("edith 14\n"), "{text}"); + assert!(text.starts_with("edith 15\n"), "{text}"); assert!( text.contains("\nsubtitle - test_subs.srt\n"), "the reference is written relative, and no cue is: {text}" @@ -1211,3 +1211,177 @@ fn pgs_erase() -> Vec { set.extend(pgs_segment(0x80, &[])); set } + +/// The v15 lines: a subtitle lane is written in its place among the others, a +/// caption on it is one `sub` line -- both clocks and the palette row it names +/// -- and an empty one is the bare line a video lane's is. The whole of it +/// comes back the placement it was, and a re-save is the same bytes, which is +/// what "the file is the timeline" means for words as much as for pictures. +#[test] +fn subtitle_lanes_and_their_captions_survive_a_save() { + let dir = scratch("lanes_save"); + let media = dir.join("test_av.mp4"); + std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture"); + let subs = dir.join("test_subs.srt"); + std::fs::copy(data("test_subs.srt"), &subs).expect("copy the subtitle fixture"); + + let mut session = engine::PlaybackSession::open(&media).expect("open the fixture"); + session.set_gain(0.0); // silent like the rest of the suites + assert_eq!(session.import_subtitles(&subs).expect("the .srt imports"), 1); + + let s1 = session.add_lane(LaneKind::Subtitle); + let s2 = session.add_lane(LaneKind::Subtitle); + let caption = SubClip { + start: 0, + frames: 45, + track: 0, + in_us: 500_000, + out_us: 1_500_000, + }; + session.place_sub(s1, 10, caption).expect("a caption goes down"); + + let project = dir.join("cut.edith"); + session.save_project(&project).expect("save"); + let text = std::fs::read_to_string(&project).expect("read the project back"); + assert!(text.starts_with("edith 15\n"), "{text}"); + assert!( + text.contains("\nsub 1 10 45 0 500000 1500000\n"), + "the placement is one line: where it sits, how long, whose words, and \ + the window of them in microseconds: {text}" + ); + assert!( + text.contains("\nsub 2\n"), + "and an empty subtitle lane says so on a line of its own: {text}" + ); + + let back = engine::PlaybackSession::open_project(&project).expect("open the project"); + assert_eq!( + back.subtitle_lanes(), + [s1, s2], + "both lanes come back, in the order they were laid out" + ); + assert_eq!( + back.sub_lane(s1), + [SubClip { start: 10, ..caption }], + "and the caption is the very placement it was" + ); + assert!(back.sub_lane(s2).is_empty(), "the empty one is still empty"); + assert_eq!(back.subtitles()[0].cues, expected(), "palette intact"); + + // ...and saving what was loaded writes the file it was loaded from. + let again = dir.join("cut2.edith"); + back.save_project(&again).expect("save again"); + assert_eq!( + std::fs::read_to_string(&again).expect("read"), + text, + "a load and a re-save is the same file" + ); +} + +/// The dialect before the lanes: a v14 project names its subtitle files and +/// places none of them -- there was no line to place one on -- so it loads with +/// its palette whole and no subtitle lane at all, and re-saves as v15 with the +/// same one `subtitle` line and nothing on any lane. +#[test] +fn a_version_14_project_keeps_its_palette_and_places_nothing() { + let dir = scratch("v14"); + let media = dir.join("test_av.mp4"); + std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture"); + let srt = dir.join("test_subs.srt"); + std::fs::copy(data("test_subs.srt"), &srt).expect("copy the subtitle fixture"); + let path = dir.join("old.edith"); + std::fs::write( + &path, + "edith 14\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ + subtitle - test_subs.srt\nvideo 1 0 0 30 0 0 - - fit 1000\n\ + audio 1 0 0 30 0 0 - - fit 1000\n", + ) + .expect("write a v14 file"); + + let loaded = engine::PlaybackSession::open_project(&path).expect("a v14 file still opens"); + assert_eq!(loaded.subtitles().len(), 1, "the palette row is there"); + assert_eq!(loaded.subtitles()[0].cues, expected(), "cues and all"); + assert!( + loaded.subtitle_lanes().is_empty(), + "and nothing of it is placed: that dialect had nowhere to place it" + ); + + let now = dir.join("new.edith"); + loaded.save_project(&now).expect("save"); + let text = std::fs::read_to_string(&now).expect("read back"); + assert!(text.starts_with("edith 15\n"), "{text}"); + assert!( + text.contains("\nsubtitle - test_subs.srt\n"), + "the row survives the migration: {text}" + ); + assert!( + !text.contains("\nsub "), + "and no lane and no placement were invented for it: {text}" + ); +} + +/// The palette bound, which is a clip's source bound for words: a caption names +/// a `subtitle` line by position, so one naming a row that is not there is +/// refused -- at the parser, which names the line, and at +/// [`engine::Project::with_subs`], which names the lane. Neither is a file this +/// editor wrote; both are files it may be handed. +#[test] +fn a_caption_naming_a_track_the_palette_does_not_have_is_refused() { + let dir = scratch("badtrack"); + let media = dir.join("test_av.mp4"); + std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture"); + std::fs::copy(data("test_subs.srt"), dir.join("test_subs.srt")).expect("copy the subtitles"); + let path = dir.join("bad.edith"); + std::fs::write( + &path, + "edith 15\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ + subtitle - test_subs.srt\nvideo 1 0 0 30 0 0 - - fit 1000\n\ + audio 1 0 0 30 0 0 - - fit 1000\nsub 1 0 45 3 0 1000000\n", + ) + .expect("write it"); + let err = engine::PlaybackSession::open_project(&path) + .err() + .expect("a caption may not name a row that is not there") + .to_string(); + assert_eq!(err, "line 9: caption names subtitle track 3 of 1"); + + // An empty placement is refused by the same door, at either clock. + let empty = dir.join("empty.edith"); + std::fs::write( + &empty, + "edith 15\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ + subtitle - test_subs.srt\nvideo 1 0 0 30 0 0 - - fit 1000\n\ + audio 1 0 0 30 0 0 - - fit 1000\nsub 1 0 45 0 1000000 1000000\n", + ) + .expect("write it"); + let err = engine::PlaybackSession::open_project(&empty) + .err() + .expect("a window of nothing is not a caption") + .to_string(); + assert_eq!( + err, + "line 9: caption at 0 is empty: 45 frames of [1000000, 1000000)" + ); + + // ...and the load's own door says the same thing about the same file, by + // the lane rather than by the line: `from_parts` for words. + let (project, _lane) = with_subtitle_lane(); + let beyond = SubClip { track: 3, ..whole(0) }; + let refused = project + .clone() + .with_subs(vec![Vec::new(), Vec::new(), vec![beyond]]) + .expect_err("the palette holds one row") + .to_string(); + assert_eq!(refused, "S1 caption at 0 names subtitle track 3 of 1"); + + // A caption on a lane that is not a subtitle lane is refused there too -- + // the media lanes hold pictures and sound, and never words. + let wrong = project + .with_subs(vec![vec![whole(0)], Vec::new(), Vec::new()]) + .expect_err("V1 is not a subtitle track") + .to_string(); + assert!( + wrong.starts_with("V1 is not a subtitle track and holds 1 caption(s)"), + "{wrong}" + ); +}