From 8369d8eed8c7d1003865fb9f6f2d893fa5e32ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 02:26:25 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(engine):=20clips=20can=20name?= =?UTF-8?q?=20a=20dissolve=20into=20their=20neighbor=20(edith=2019,=20stor?= =?UTF-8?q?age+format)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit transition_out frames on a video clip dissolve it into the adjacent next clip; split keeps the tail's transition clamped and zeroes the head; v18 files load with hard cuts, v19 round-trips bit-exactly. Blending (playback and export) lands separately. --- crates/engine/examples/bench.rs | 1 + crates/engine/src/edith.rs | 169 ++++++++++++++++++++-------- crates/engine/src/playback.rs | 3 + crates/engine/src/project.rs | 136 ++++++++++++++++++++-- crates/engine/src/proxy.rs | 1 + crates/engine/src/scale.rs | 37 ++++++ crates/engine/src/silence.rs | 1 + crates/engine/src/sweep.rs | 1 + crates/engine/tests/audio_export.rs | 9 ++ crates/engine/tests/eq_audio.rs | 1 + crates/engine/tests/export.rs | 2 + crates/engine/tests/lanes.rs | 2 + crates/engine/tests/opus_export.rs | 2 + crates/engine/tests/silence.rs | 2 + 14 files changed, 312 insertions(+), 55 deletions(-) diff --git a/crates/engine/examples/bench.rs b/crates/engine/examples/bench.rs index 0959178..d3dd28b 100644 --- a/crates/engine/examples/bench.rs +++ b/crates/engine/examples/bench.rs @@ -862,6 +862,7 @@ fn export_bench(path: &Path, seat: &str, out_dir: &Path) { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame, out_frame: in_frame + span, diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index e0ecc9c..1edc073 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -110,7 +110,7 @@ //! -- the first colour line is color 0 -- printed and refused by the same rules. //! //! A lane line is ` -//! `: which lane the clip is on -- its kind and its 1-based number +//! `: which lane the clip is on -- its kind and its 1-based number //! among the lanes of that kind, the [`crate::project::Lane::label`] a header //! column shows -- then where the clip sits on the timeline, the half-open //! source range it plays, the file it plays from, its group id, the eq line it @@ -120,7 +120,9 @@ //! in thousandths of real time ([`crate::project::Speed`], `1000` for a clip //! nobody has speeded), and finally its ramp up from silence and ramp down to //! it, in timeline frames ([`crate::project::Clip::fade_in`], -//! [`crate::project::Clip::fade_out`], `0 0` for a clip nobody has faded). +//! [`crate::project::Clip::fade_out`], `0 0` for a clip nobody has faded), and +//! finally its cross-dissolve into whatever abuts its end +//! ([`crate::project::Clip::transition_out`], `0` for a hard cut). //! Timeline placement is //! explicit, so a *gap* is simply a stretch no line covers -- there is nothing //! to write for one, and nothing that can disagree about its length. The `` @@ -221,7 +223,8 @@ 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 18"; +const MAGIC: &[u8] = b"edith 19"; +const MAGIC_V18: &[u8] = b"edith 18"; const MAGIC_V17: &[u8] = b"edith 17"; const MAGIC_V16: &[u8] = b"edith 16"; const MAGIC_V15: &[u8] = b"edith 15"; @@ -555,7 +558,7 @@ fn emit( let color = c.color.map_or("-".to_string(), |e| e.to_string()); out.extend_from_slice( format!( - "{keyword} {ord} {} {} {} {} {link} {eq} {color} {} {} {} {}\n", + "{keyword} {ord} {} {} {} {} {link} {eq} {color} {} {} {} {} {}\n", c.start, c.in_frame, c.out_frame, @@ -564,6 +567,7 @@ fn emit( c.speed.permille(), c.fade_in, c.fade_out, + c.transition_out, ) .as_bytes(), ); @@ -606,8 +610,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 carries a per-clip fade envelope... - let v18 = first == MAGIC; + // The one that carries a per-clip cross-dissolve into its successor... + let v19 = first == MAGIC; + // ...the one that carries a per-clip fade envelope... + let v18 = v19 || first == MAGIC_V18; // ...the one that carries an explicit sound rate... let v17 = v18 || first == MAGIC_V17; // ...the one whose captions may carry a group id... @@ -975,6 +981,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: queued, in_frame: number(f[0], n)?, out_frame: number(f[1], n)?, @@ -1093,15 +1100,16 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { if numbered && rest.is_empty() { continue; } - // v18 grew the two fade fields, v8 the speed one, v7 the fit - // one, v6 the colour one and v5 the eq one; every older - // dialect ends at the link. + // v18 grew the two fade fields, v19 the transition one, v8 + // the speed one, v7 the fit one, v6 the colour one and v5 the + // eq one; every older dialect ends at the link. let want = 5 + usize::from(v5) + usize::from(v6) + usize::from(v7) + usize::from(v8) - + 2 * usize::from(v18); + + 2 * usize::from(v18) + + usize::from(v19); let f = fields(rest, want, "clip", n)?; let in_frame = number(f[1], n)?; let out_frame = number(f[2], n)?; @@ -1115,6 +1123,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { Clip { fade_in: number(f.get(9).copied().unwrap_or(b"0"), n)?.min(frames), fade_out: number(f.get(10).copied().unwrap_or(b"0"), n)?.min(frames), + transition_out: number(f.get(11).copied().unwrap_or(b"0"), n)?.min(frames), start: number(f[0], n)?, in_frame, out_frame, @@ -1536,7 +1545,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 18\n")); + assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 19\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() @@ -1948,6 +1957,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame, out_frame, @@ -2006,10 +2016,10 @@ mod tests { let bytes = flat(&dir, &sources, &lanes, 12); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 19\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 0 0\nvideo 1 30 10 20 1 1 - - fit 1000 0 0\n\ - audio 1 0 0 30 0 0 - - fit 1000 0 0\n", + video 1 0 0 30 0 0 - - fit 1000 0 0 0\nvideo 1 30 10 20 1 1 - - fit 1000 0 0 0\n\ + audio 1 0 0 30 0 0 - - fit 1000 0 0 0\n", "the file under the project directory is written relative to it, \ each with the audio stream it plays" ); @@ -2043,9 +2053,9 @@ mod tests { let bytes = flat(&dir, &sources, &lanes, 7); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ - video 1 0 0 30 0 4 - - fit 1000 0 0\naudio 1\n\ - video 2 40 0 10 0 - - - fit 1000 0 0\naudio 2 0 0 30 0 4 - - fit 1000 0 0\n", + "edith 19\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 4 - - fit 1000 0 0 0\naudio 1\n\ + video 2 40 0 10 0 - - - fit 1000 0 0 0\naudio 2 0 0 30 0 4 - - fit 1000 0 0 0\n", "an empty lane is a line of its own; everything else is its clips" ); let back = parse(&bytes, &dir).expect("parse"); @@ -2131,12 +2141,14 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, eq: Some(0), ..clip(0, 0, 30, 0, Some(0)) }, Clip { fade_in: 0, fade_out: 0, + transition_out: 0, eq: Some(1), ..clip(30, 10, 20, 0, None) }, @@ -2145,12 +2157,14 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, eq: Some(0), ..clip(0, 0, 30, 0, Some(0)) }, Clip { fade_in: 0, fade_out: 0, + transition_out: 0, eq: Some(2), ..clip(30, 0, 10, 0, None) }, @@ -2159,12 +2173,12 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &eq, &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 19\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\ - video 1 0 0 30 0 0 0 - fit 1000 0 0\nvideo 1 30 10 20 0 - 1 - fit 1000 0 0\n\ - audio 1 0 0 30 0 0 0 - fit 1000 0 0\naudio 1 30 0 10 0 - 2 - fit 1000 0 0\n", + video 1 0 0 30 0 0 0 - fit 1000 0 0 0\nvideo 1 30 10 20 0 - 1 - fit 1000 0 0 0\n\ + audio 1 0 0 30 0 0 0 - fit 1000 0 0 0\naudio 1 30 0 10 0 - 2 - fit 1000 0 0 0\n", "the table comes before the clips, and a clip names a line of it" ); @@ -2235,6 +2249,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, color: Some(0), fit: FitPolicy::default(), speed: Speed::NORMAL, @@ -2243,6 +2258,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, color: Some(1), fit: FitPolicy::default(), speed: Speed::NORMAL, @@ -2253,6 +2269,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, color: Some(0), fit: FitPolicy::default(), speed: Speed::NORMAL, @@ -2261,6 +2278,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, color: Some(2), fit: FitPolicy::default(), speed: Speed::NORMAL, @@ -2271,12 +2289,12 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &color, (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 19\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\ - video 1 0 0 30 0 0 - 0 fit 1000 0 0\nvideo 1 30 10 20 0 - - 1 fit 1000 0 0\n\ - audio 1 0 0 30 0 0 - 0 fit 1000 0 0\naudio 1 30 0 10 0 - - 2 fit 1000 0 0\n", + video 1 0 0 30 0 0 - 0 fit 1000 0 0 0\nvideo 1 30 10 20 0 - - 1 fit 1000 0 0 0\n\ + audio 1 0 0 30 0 0 - 0 fit 1000 0 0 0\naudio 1 30 0 10 0 - - 2 fit 1000 0 0 0\n", "the table comes before the clips, and a clip names a line of it" ); @@ -2336,9 +2354,9 @@ mod tests { (1280, 720), old.playhead )), - "edith 18\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 19\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 0 0\naudio 1 0 0 30 0 0 - - fit 1000 0 0\n" + video 1 0 0 30 0 0 0 - fit 1000 0 0 0\naudio 1 0 0 30 0 0 - - fit 1000 0 0 0\n" ); } @@ -2353,6 +2371,7 @@ mod tests { let speeded = |start, in_frame, out_frame, permille| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame, out_frame, @@ -2376,9 +2395,9 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ - video 1 0 0 30 0 - - - fit 2000 0 0\nvideo 1 15 30 40 0 - - - fit 250 0 0\n\ - audio 1 0 0 30 0 - - - fit 2000 0 0\n", + "edith 19\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 - - - fit 2000 0 0 0\nvideo 1 15 30 40 0 - - - fit 250 0 0 0\n\ + audio 1 0 0 30 0 - - - fit 2000 0 0 0\n", "the rate is the clip line's last field, in thousandths" ); let back = parse(&bytes, &dir).expect("parse"); @@ -2405,8 +2424,8 @@ mod tests { (1280, 720), old.playhead )), - "edith 18\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ - video 1 0 0 30 0 0 - - fit 1000 0 0\naudio 1 0 0 30 0 0 - - fit 1000 0 0\n" + "edith 19\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 0 - - fit 1000 0 0 0\naudio 1 0 0 30 0 0 - - fit 1000 0 0 0\n" ); // A rate outside what the editor can set is a corrupt line, by name. let bad = b"edith 8\nsource 0 a.mp4\nvideo 1 0 0 30 0 - - - fit 9000\n"; @@ -2438,6 +2457,7 @@ mod tests { let faded = Clip { fade_in: 5, fade_out: 7, + transition_out: 0, start: 0, in_frame: 0, out_frame: 30, @@ -2452,14 +2472,67 @@ mod tests { let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ - video 1 0 0 30 0 - - - fit 1000 5 7\n", + "edith 19\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 - - - fit 1000 5 7 0\n", "the fades are the clip line's last two fields" ); let back = parse(&bytes, &dir).expect("parse"); assert_eq!(back.lanes, lanes, "the fades round trip as the very numbers"); } + /// The whole of the v19 bump, the fade test's twin: a clip's dissolve into + /// its successor is written as the clip line's own last field, comes back + /// as the very number that was set, and a v18 file -- which has no such + /// field -- loads with every clip's [`Clip::transition_out`] at zero and + /// re-saves saying so. + #[test] + fn a_v18_file_loads_with_no_transition_and_transitions_round_trip_bit_exactly() { + let dir = PathBuf::from("/proj"); + // The v18 dialect: eleven clip fields, the last the fade-out -- no + // transition field after it at all, not even a zero. + let v18 = b"edith 18\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 - - - fit 1000 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0\n"; + let old = parse(v18, &dir).expect("v18 parses"); + assert!( + old.lanes + .iter() + .flat_map(|(_, clips)| clips) + .all(|c| c.transition_out == 0), + "nothing before v19 had a dissolve to name" + ); + + // ...and the dialect that does: a nonzero dissolve, written and read + // back as the very number. + let sources = vec![Source::new("/proj/a.mp4", 0)]; + let dissolving = Clip { + fade_in: 0, + fade_out: 0, + transition_out: 9, + start: 0, + in_frame: 0, + out_frame: 30, + source: 0, + link: None, + eq: None, + color: None, + fit: FitPolicy::default(), + speed: Speed::NORMAL, + }; + let lanes = vec![(LaneKind::Video, vec![dissolving])]; + let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); + assert_eq!( + String::from_utf8_lossy(&bytes), + "edith 19\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 - - - fit 1000 0 0 9\n", + "the dissolve is the clip line's own last field" + ); + let back = parse(&bytes, &dir).expect("parse"); + assert_eq!( + back.lanes, lanes, + "the dissolve round trips as the very number" + ); + } + /// The whole of the v9 bump, the speed test's twin: the mix a project was /// left at -- every track's own volume and the master limiter -- plus the /// rate it was cut at, all written, all read back as the very numbers, and @@ -2500,10 +2573,10 @@ mod tests { ); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\ + "edith 19\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 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0\n\ - audio 2 0 0 30 0 - - - fit 1000 0 0\n\ + video 1 0 0 30 0 - - - fit 1000 0 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0 0\n\ + audio 2 0 0 30 0 - - - fit 1000 0 0 0\n\ gain audio 1 3.0\ngain audio 2 -6.5\n", "the mix is lines of its own, the gains after the lanes they name" ); @@ -2532,8 +2605,8 @@ mod tests { (1280, 720), old.playhead )), - "edith 18\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ - video 1 0 0 30 0 - - - fit 1000 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0\n" + "edith 19\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ + video 1 0 0 30 0 - - - fit 1000 0 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0 0\n" ); // Each of the three refuses by name, on its own line. @@ -2696,10 +2769,10 @@ 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 18\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 19\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 0 0\nvideo 1 30 10 20 1 1 - - fit 1000 0 0\n\ - audio 1 0 0 30 0 0 - - fit 1000 0 0\n" + video 1 0 0 30 0 0 - - fit 1000 0 0 0\nvideo 1 30 10 20 1 1 - - fit 1000 0 0 0\n\ + audio 1 0 0 30 0 0 - - fit 1000 0 0 0\n" ); // An empty lane's line is still the whole of that lane, eq or no eq. let empty = parse( @@ -2749,10 +2822,10 @@ mod tests { let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); assert_eq!( String::from_utf8_lossy(&v5), - "edith 18\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ + "edith 19\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 0 0\nvideo 1 30 10 20 1 1 - - fit 1000 0 0\n\ - audio 1 0 0 30 0 0 - - fit 1000 0 0\n", + video 1 0 0 30 0 0 - - fit 1000 0 0 0\nvideo 1 30 10 20 1 1 - - fit 1000 0 0 0\n\ + audio 1 0 0 30 0 0 - - fit 1000 0 0 0\n", "a re-saved v2 project differs only by its version, the lane \ numbers, the streams it always meant and an equalizer it has none of" ); @@ -2781,7 +2854,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 18\n")); + assert!(v5.starts_with(b"edith 19\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. @@ -2891,8 +2964,8 @@ mod tests { let bytes = std::fs::read(&path).expect("read back"); assert_eq!( String::from_utf8_lossy(&bytes), - "edith 18\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ - video 1 0 0 30 0 - - - fit 1000 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0\n" + "edith 19\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ + video 1 0 0 30 0 - - - fit 1000 0 0 0\naudio 1 0 0 30 0 - - - fit 1000 0 0 0\n" ); // Loading rejoins the *given* directory, so the file is reached by the // way the project was opened -- the same file, through the link, still @@ -2936,10 +3009,10 @@ mod tests { #[test] fn a_wrong_first_line_is_refused_by_name() { let dir = PathBuf::from("/proj"); - let err = parse(b"edith 19\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) + let err = parse(b"edith 20\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) .unwrap_err() .to_string(); - assert_eq!(err, "line 1: unsupported version 19"); + assert_eq!(err, "line 1: unsupported version 20"); 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 fa16859..e4e0879 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -620,6 +620,7 @@ impl PlaybackSession { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: meta.frame_count, @@ -705,6 +706,7 @@ impl PlaybackSession { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: place_frames(meta.frame_count, IMAGE_ONLY_RATE), @@ -2631,6 +2633,7 @@ impl PlaybackSession { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: match image { diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 5ec406e..ce3a325 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -439,6 +439,17 @@ pub struct Clip { /// Timeline frames of ramp-down to silence at the clip's own end. Same /// promises as [`Clip::fade_in`]. pub fade_out: u32, + /// Timeline frames of cross-dissolve into the clip immediately after this + /// one on the same video lane, at this clip's own end -- `0` for a hard + /// cut. Inline like [`Clip::fade_in`]/[`Clip::fade_out`] for the same + /// reason, and clamped the same way by its setter + /// ([`Project::set_transition_out`]): never wider than this clip's own + /// length, nor than the successor it dissolves into. Meaningless -- and + /// left as whatever it was -- the moment the next clip stops abutting + /// this one; a reader that cares checks adjacency itself rather than + /// trusting this field alone, exactly as a stored fade is trusted only + /// because a clip's own length is checked beside it. + pub transition_out: u32, } impl Clip { @@ -881,6 +892,7 @@ impl Project { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: frame_count.max(1), @@ -1263,6 +1275,7 @@ impl Project { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame: 0, out_frame: frame_count.max(1), @@ -2509,6 +2522,37 @@ impl Project { true } + /// Timeline frames of cross-dissolve the clip at `idx` of `lane` plays + /// into its successor, at its own end. `0` for an index that is not + /// there, same as a clip that has none. + pub fn transition_out_of(&self, lane: Lane, idx: usize) -> u32 { + self.lane(lane).get(idx).map_or(0, |c| c.transition_out) + } + + /// Sets it, clamped to the clip's own length and to how many frames its + /// successor on `lane` actually offers -- a dissolve can shrink a clip's + /// visible middle to nothing but never ask for more than the two clips + /// between them have. `false` -- no history -- unless `lane` is video, + /// `idx + 1` is in bounds, and the two clips are adjacent: the second's + /// [`Clip::start`] is exactly the first's [`Clip::end`], no gap between + /// them. One undo step like [`Project::set_fade_out`]. + pub fn set_transition_out(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { + if lane.kind != LaneKind::Video { + return false; + } + let clips = self.lane(lane); + let (Some(a), Some(b)) = (clips.get(idx), clips.get(idx + 1)) else { + return false; + }; + if a.end() != b.start { + return false; + } + let cap = a.frames().min(b.frames()); + self.snapshot(); + self.lane_mut(lane).expect("checked above")[idx].transition_out = frames.min(cap); + true + } + /// Crossfades the audio clip at `idx` of `lane` into the one immediately /// after it, over `frames` timeline frames: sets `idx`'s /// [`Clip::fade_out`] and its neighbour's [`Clip::fade_in`] to `frames`, @@ -2957,9 +3001,16 @@ impl Project { // ends, flat -- there was no silence there to ramp out of. tail.fade_in = 0; tail.fade_out = tail.fade_out.min(tail.frames()); + // A dissolve is a promise about the *end* edge, same as + // `fade_out`: only the half that kept the clip's original end + // (the tail) keeps it, re-clamped to its own now-shorter length; + // the head's new end is the cut the razor just made, which starts + // flat -- there was no successor to dissolve into there. + tail.transition_out = tail.transition_out.min(tail.frames()); data.clips[idx].out_frame = tail.in_frame; data.clips[idx].link = orig[data_i]; data.clips[idx].fade_out = 0; + data.clips[idx].transition_out = 0; data.clips[idx].fade_in = data.clips[idx].fade_in.min(data.clips[idx].frames()); data.clips.insert(idx + 1, tail); } @@ -3294,6 +3345,7 @@ impl Project { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: timeline_frame, link: None, ..clip @@ -3335,6 +3387,7 @@ impl Project { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: timeline_frame, link: Some(self.new_link()), ..clip @@ -3912,6 +3965,7 @@ impl Project { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: at, link: Some(self.new_link()), ..clip @@ -4987,6 +5041,7 @@ fn clear(clips: &mut Vec, start: u32, end: u32) { out.push(Clip { fade_in: 0, fade_out: 0, + transition_out: 0, out_frame: c.in_frame + keep.min(c.len()), link: None, ..c @@ -5002,6 +5057,7 @@ fn clear(clips: &mut Vec, start: u32, end: u32) { out.push(Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: end, in_frame: c.out_frame - keep.min(c.len()), link: None, @@ -5110,6 +5166,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame, out_frame, @@ -5321,6 +5378,62 @@ mod tests { assert_eq!(v[1].fade_out, 15, "the right half's kept fade-out, unclamped: 15 frames fit in 15"); } + /// [`Project::set_transition_out`] refuses a non-video lane, a clip with + /// no successor, and two clips that do not touch, and otherwise clamps + /// the dissolve to the shorter of the two clips it spans. + #[test] + fn set_transition_out_only_dissolves_adjacent_video_clips() { + let mut p = Project::single(FILE, 20); + assert!(p.split(15), "a short 5-frame tail to clamp against"); + assert!( + !p.set_transition_out(Lane::A1, 0, 5), + "audio is not a video lane" + ); + assert!( + !p.set_transition_out(Lane::V1, 1, 5), + "there is no clip after idx 1" + ); + assert!( + p.set_transition_out(Lane::V1, 0, 100), + "the two halves are adjacent" + ); + assert_eq!( + p.transition_out_of(Lane::V1, 0), + 5, + "clamped to the shorter neighbour's 5 frames" + ); + + // A gap between two clips is not adjacency, even on the video lane. + let mut p2 = Project::single(FILE, 1); + let v2 = p2.add_lane(LaneKind::Video); + assert!(p2.place(v2, 0, clip(0, 0, 3, 0))); + assert!(p2.place(v2, 5, clip(5, 3, 6, 0)), "a gap before this one"); + assert!(!p2.set_transition_out(v2, 0, 5), "a gap is not adjacent"); + } + + /// The rule [`Project::write_split`] follows for [`Clip::transition_out`], + /// same shape as [`a_split_clamps_the_kept_fade_and_zeroes_the_cut_edge`]: + /// only the half that kept the clip's original *end* keeps the dissolve, + /// re-clamped to its own now-shorter length; the head the cut just made + /// has no successor to dissolve into and starts flat. + #[test] + fn a_split_clamps_the_tail_transition_and_zeroes_the_head() { + let mut p = Project::single(FILE, 20); + assert!( + !p.set_transition_out(Lane::V1, 0, 0), + "one clip on the lane, no successor to dissolve into yet" + ); + // Give the clip a dissolve by hand, then cut it in two. + p.lane_mut(Lane::V1).unwrap()[0].transition_out = 15; + assert!(p.split(5)); + let v = p.lane(Lane::V1); + assert_eq!(v[0].transition_out, 0, "the cut edge it made has no successor"); + assert_eq!( + v[1].transition_out, 15, + "the tail's kept dissolve, unclamped: 15 frames fit in 15" + ); + } + /// The music video's own path: the take comes apart into halves that carry /// ids of their own, one undo puts it back, and two clips over the same /// frames become one take again by hand. @@ -6501,15 +6614,18 @@ mod tests { /// table like the eq and the grade: there is nothing for a table to share. /// It costs the clip nothing at all -- the fields before it (a `usize` /// source and two `Option`s) already left the struct padded to 40 - /// bytes, and the byte landed in that padding. `fade_in`/`fade_out` are the - /// two `u32`s after it, so those cost the clip a full word each: 48 bytes - /// total. This is the assert that says so: a clip that grows a word grows - /// every undo snapshot and every clipboard copy with it. + /// bytes, and the byte landed in that padding. `fade_in`/`fade_out` are two + /// `u32`s after it (48 bytes), and `transition_out` is a third word -- + /// 52 bytes of fields, but the struct's own alignment is 8 (from the + /// `usize` source and the 8-byte `Option` link), so an odd number of + /// words after it pads out to the next multiple of 8: 56 bytes total. + /// This is the assert that says so: a clip that grows a word grows every + /// undo snapshot and every clipboard copy with it. #[test] fn a_fit_policy_costs_the_clip_no_word() { assert_eq!( std::mem::size_of::(), - 48, + 56, "Clip changed size: {} bytes", std::mem::size_of::() ); @@ -6520,6 +6636,7 @@ mod tests { const PASTED: Clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 100, out_frame: 102, @@ -6594,6 +6711,7 @@ mod tests { let copied = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, source: wav, ..PASTED }; @@ -7860,8 +7978,8 @@ mod tests { fn a_clip_is_still_a_small_copy() { assert_eq!( std::mem::size_of::(), - 48, - "Clip changed size -- 32 before the colour index, 40 before the fades, 48 after" + 56, + "Clip changed size -- 32 before the colour index, 40 before the fades, 48 after, 56 with the transition" ); } @@ -7945,6 +8063,7 @@ mod tests { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, eq: Some(i), ..video[0] }] @@ -7983,6 +8102,7 @@ mod tests { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, color: Some(i), fit: FitPolicy::default(), speed: Speed::NORMAL, @@ -8043,6 +8163,7 @@ mod tests { let linked = |start, in_frame, out_frame, link| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame, out_frame, @@ -8822,6 +8943,7 @@ mod tests { let one = |start: u32, end: u32, link| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame: start, out_frame: end, diff --git a/crates/engine/src/proxy.rs b/crates/engine/src/proxy.rs index ad018bc..eb30564 100644 --- a/crates/engine/src/proxy.rs +++ b/crates/engine/src/proxy.rs @@ -489,6 +489,7 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result (Vec, Vec, Vec) { + let t = t.clamp(0.0, 1.0); + let plane = |a: &[u8], b: &[u8]| -> Vec { + assert_eq!(a.len(), b.len(), "blend_i420: mismatched plane sizes"); + a.iter() + .zip(b) + .map(|(&av, &bv)| (av as f32 + (bv as f32 - av as f32) * t).round() as u8) + .collect() + }; + (plane(a.0, b.0), plane(a.1, b.1), plane(a.2, b.2)) +} + /// Resizes `plane` to `len` and sets every byte to `value` -- a canvas reused /// across frames is refilled, not reallocated. fn fill(plane: &mut Vec, len: usize, value: u8) { @@ -1097,4 +1115,23 @@ mod tests { let ms = t.elapsed().as_secs_f64() * 1000.0 / f64::from(runs); println!("scale::Composer::place 1280x720 onto 1920x1080 Fit: {ms:.3} ms/frame"); } + + /// [`blend_i420`] at the midpoint of two solid-colour frames lands exactly + /// on the 50/50 average, on every plane -- the synthetic stand-in for "the + /// dissolve's midpoint frame looks like the average colour" the export + /// path is built on. + #[test] + fn blend_i420_is_50_50_at_the_midpoint() { + let a = (vec![0u8; 16], vec![10u8; 4], vec![20u8; 4]); + let b = (vec![100u8; 16], vec![50u8; 4], vec![120u8; 4]); + let (y, u, v) = blend_i420((&a.0, &a.1, &a.2), (&b.0, &b.1, &b.2), 0.5); + assert!(y.iter().all(|&p| p == 50), "Y midpoint: {y:?}"); + assert!(u.iter().all(|&p| p == 30), "U midpoint: {u:?}"); + assert!(v.iter().all(|&p| p == 70), "V midpoint: {v:?}"); + // The edges hand back each frame untouched. + let (y0, _, _) = blend_i420((&a.0, &a.1, &a.2), (&b.0, &b.1, &b.2), 0.0); + assert_eq!(y0, a.0); + let (y1, _, _) = blend_i420((&a.0, &a.1, &a.2), (&b.0, &b.1, &b.2), 1.0); + assert_eq!(y1, b.0); + } } diff --git a/crates/engine/src/silence.rs b/crates/engine/src/silence.rs index 80c35ff..b2ae6b5 100644 --- a/crates/engine/src/silence.rs +++ b/crates/engine/src/silence.rs @@ -297,6 +297,7 @@ mod tests { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame, out_frame, diff --git a/crates/engine/src/sweep.rs b/crates/engine/src/sweep.rs index 360f1ca..12c8c2f 100644 --- a/crates/engine/src/sweep.rs +++ b/crates/engine/src/sweep.rs @@ -68,6 +68,7 @@ fn clip(start: u32, in_frame: u32, out_frame: u32) -> Clip { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame, out_frame, diff --git a/crates/engine/tests/audio_export.rs b/crates/engine/tests/audio_export.rs index de965ac..495d32b 100644 --- a/crates/engine/tests/audio_export.rs +++ b/crates/engine/tests/audio_export.rs @@ -63,6 +63,7 @@ fn mixed_project() -> Project { let clip = |start, source| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame: 0, out_frame: 30, @@ -87,6 +88,7 @@ fn mixed_project() -> Project { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 90, @@ -333,6 +335,7 @@ fn a_still_and_a_song_export_as_an_mp4_with_sound() { let clip = |source, frames| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: frames, @@ -399,6 +402,7 @@ fn a_fader_is_one_tracks_own_and_the_limiter_holds_the_sum() { let clip = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 30, @@ -637,6 +641,7 @@ fn the_sound_is_written_at_the_rate_that_was_asked_for() { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 60, @@ -653,6 +658,7 @@ fn the_sound_is_written_at_the_rate_that_was_asked_for() { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 60, @@ -710,6 +716,7 @@ fn a_silent_clip_exports_as_silence_over_its_own_span() { let clip = |start, source| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame: 0, out_frame: 30, @@ -804,6 +811,7 @@ fn a_51_ac3_source_round_trips_through_a_wav() { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 30, @@ -933,6 +941,7 @@ fn a_mono_timeline_exports_as_dual_mono_ogg() { vec![Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 30, diff --git a/crates/engine/tests/eq_audio.rs b/crates/engine/tests/eq_audio.rs index 780c91e..5c654bb 100644 --- a/crates/engine/tests/eq_audio.rs +++ b/crates/engine/tests/eq_audio.rs @@ -56,6 +56,7 @@ fn clip(start: u32, source: usize, frames: u32) -> Clip { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame: 0, out_frame: frames, diff --git a/crates/engine/tests/export.rs b/crates/engine/tests/export.rs index d433f99..69cb986 100644 --- a/crates/engine/tests/export.rs +++ b/crates/engine/tests/export.rs @@ -236,6 +236,7 @@ fn exports_at_the_project_resolution_with_the_watched_geometry() { let clip = |source: usize, start: u32| engine::Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start, in_frame: 0, out_frame: 10, @@ -320,6 +321,7 @@ fn exports_the_audio_stream_the_timeline_plays() { let whole = engine::Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: meta.frame_count, diff --git a/crates/engine/tests/lanes.rs b/crates/engine/tests/lanes.rs index b1fb59b..c412caf 100644 --- a/crates/engine/tests/lanes.rs +++ b/crates/engine/tests/lanes.rs @@ -236,6 +236,7 @@ fn the_mp4_copy_follows_the_lane_that_holds_the_sound() { let source = asset("test_av.mp4"); let clip = Clip { fade_in: 0, + transition_out: 0, fade_out: 0, start: 0, in_frame: 0, @@ -300,6 +301,7 @@ fn two_audio_lanes_are_summed() { let doubled = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: TOP_IN, in_frame: TOP_IN, out_frame: TOP_OUT, diff --git a/crates/engine/tests/opus_export.rs b/crates/engine/tests/opus_export.rs index 2affe6a..dc450fa 100644 --- a/crates/engine/tests/opus_export.rs +++ b/crates/engine/tests/opus_export.rs @@ -82,6 +82,7 @@ fn project(video: &Path, sound: &Path, sound_in: u32) -> Project { let clip = |source, in_frame, out_frame| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame, out_frame, @@ -425,6 +426,7 @@ fn a_copied_picture_names_the_sound_it_was_written_with() { let clip = |source, out_frame| Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame, diff --git a/crates/engine/tests/silence.rs b/crates/engine/tests/silence.rs index 9c349fe..0b034d4 100644 --- a/crates/engine/tests/silence.rs +++ b/crates/engine/tests/silence.rs @@ -140,6 +140,7 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() { let broll = Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: 20, @@ -177,6 +178,7 @@ fn whole(source: usize, frames: u32) -> Clip { Clip { fade_in: 0, fade_out: 0, + transition_out: 0, start: 0, in_frame: 0, out_frame: frames,