forge
Explore
Log in

Don't you dare go Hollow

A dissolve renders in the playback preview too

Browse files at this commit

3e66b400e701a2812b4491dabd3fb8bf60ca082dAhmet Kaan GümüşParent: 77c6364
  • feat
    • a dissolve renders in the playback preview too
Show full message
✨ feat(engine): a dissolve renders in the playback preview too

The preview used to hard-cut where an export blends -- try_frame now opens
B's own decoder (graded and placed at B's own start, mirroring export's
Dissolve) once the playhead reaches the tail's dissolve window, and blends
its BGRA picture over A's with the same dissolve_weight ramp export uses.
One choke point covers both playing and scrubbed/seeked frames, since both
drain try_frame the same way.
crates/engine/src/export.rsmodified+5 -2
@@ -1032,7 +1032,10 @@ fn export_spans(project: &Project, range: Option<(u32, u32)>) -> Vec<crate::proj
10321032 /// here: a trim or a split since it was set leaves the stored number in
10331033 /// place but not its meaning, so a stale one plays as the hard cut it now
10341034 /// is rather than as a dissolve into a clip that no longer follows.
1035-fn dissolve_window(project: &Project, span: &crate::project::Span) -> Option<(usize, u32, u32, u32)> {
1035+pub(crate) fn dissolve_window(
1036+ project: &Project,
1037+ span: &crate::project::Span,
1038+) -> Option<(usize, u32, u32, u32)> {
10361039 let (source, _) = span.from?;
10371040 let (lane, idx) = project.composite_clip_at(span.start)?;
10381041 let clips = project.lane(lane);
@@ -1064,7 +1067,7 @@ fn dissolve_window(project: &Project, span: &crate::project::Span) -> Option<(us
10641067 /// a trace of A and the last still a trace of B -- the cut a hard transition
10651068 /// would have made lands one frame *after* the window, at full B, rather than
10661069 /// inside it.
1067-fn dissolve_weight(idx_in_window: u32, window: u32) -> f32 {
1070+pub(crate) fn dissolve_weight(idx_in_window: u32, window: u32) -> f32 {
10681071 (idx_in_window + 1) as f32 / (window + 1) as f32
10691072 }
10701073
crates/engine/src/playback.rsmodified+202 -1
@@ -257,6 +257,42 @@ impl Audio {
257257 }
258258 }
259259
260+/// The cross-dissolve a span's tail owes its successor, tracked from the
261+/// moment [`PlaybackSession::start_span`] opens A's own decoder until a later
262+/// `start_span` throws it away. What decides there *is* one mirrors
263+/// [`crate::export::dissolve_window`] exactly, on the very same span, so a
264+/// preview blends the window an export would render -- but B's own decoder
265+/// opens only once the playhead actually reaches it
266+/// ([`PlaybackSession::open_dissolve_b`]), rather than for the whole span:
267+/// most of a dissolving clip is never in the window, and a viewer who pauses
268+/// or seeks away before the tail never pays for a decoder nobody sees.
269+struct Dissolve {
270+ /// The first *timeline* frame inside the window.
271+ tail_start: u32,
272+ /// How many output frames the blend ramps across.
273+ window: u32,
274+ b_source: usize,
275+ b_in_frame: u32,
276+ /// Where B sits on the timeline -- what its own grade and fit are keyed
277+ /// on ([`Project::composite_color_at`], `composite_fit_at`), the same
278+ /// `b_start` [`crate::export`] grades B's side of the same dissolve by.
279+ b_start: u32,
280+ /// B's live decode, once open; `None` before the playhead reaches the
281+ /// window.
282+ b: Option<DissolveB>,
283+}
284+
285+/// B's own worker, decoding beside A's for the length of the window.
286+struct DissolveB {
287+ frames: Receiver<Frame>,
288+ worker: Worker,
289+ /// The last frame B handed over: a tick that lands between two of B's
290+ /// pictures still has one to blend A against, the same "keep showing the
291+ /// last one" policy A's own channel gets from the app's own pump -- done
292+ /// here instead, because B never leaves the engine to be pumped.
293+ held: Option<Frame>,
294+}
295+
260296 /// A file opened for playback. Starts paused at t=0; call [`PlaybackSession::play`].
261297 pub struct PlaybackSession {
262298 /// The *timeline's* parameters, not any one file's: its frame rate and codec
@@ -368,6 +404,13 @@ pub struct PlaybackSession {
368404 /// is late by exactly this window's length with another restart of it
369405 /// ([`picture_priming`](Self::picture_priming)).
370406 span_priming: bool,
407+ /// The cross-dissolve the current span's tail owes its successor, if any
408+ /// ([`crate::export::dissolve_window`], the same test an export makes of
409+ /// the same span) -- and, once the playhead has reached the window, B's
410+ /// own decoder, opened beside A's exactly as [`crate::export`] opens one
411+ /// for the render. `None` for a span with no dissolve out, which is every
412+ /// span most of the time.
413+ dissolve: Option<Dissolve>,
371414 /// The last clip has been played out; see [`PlaybackSession::is_eos`].
372415 eos: bool,
373416 /// The mix the running mixer is reading, when there is one: what lets a
@@ -547,6 +590,7 @@ impl PlaybackSession {
547590 span_rate: Rate::REAL_TIME,
548591 span,
549592 span_priming: true,
593+ dissolve: None,
550594 eos: false,
551595 mix: None,
552596 priming: false,
@@ -669,6 +713,7 @@ impl PlaybackSession {
669713 span_rate: Rate::REAL_TIME,
670714 span,
671715 span_priming: true,
716+ dissolve: None,
672717 eos: false,
673718 mix: None,
674719 priming: false,
@@ -755,6 +800,7 @@ impl PlaybackSession {
755800 span_rate: Rate::REAL_TIME,
756801 span,
757802 span_priming: true,
803+ dissolve: None,
758804 eos: false,
759805 mix: None,
760806 priming: false,
@@ -1059,6 +1105,7 @@ impl PlaybackSession {
10591105 span_rate: Rate::REAL_TIME,
10601106 span,
10611107 span_priming: true,
1108+ dissolve: None,
10621109 eos: false,
10631110 mix: None,
10641111 priming: false,
@@ -1393,6 +1440,32 @@ impl PlaybackSession {
13931440 // The first picture out of the span now decoding ends its
13941441 // prime ([`Self::span_priming`]).
13951442 self.span_priming = false;
1443+ // Inside the dissolve window this span's tail owes its
1444+ // successor, if any: B's own decoder, graded and placed
1445+ // exactly as an export would place it, blended over A's
1446+ // already-converted picture -- the one blend point every
1447+ // path to the screen goes through, playing or scrubbed,
1448+ // since both drain this same channel one frame at a time.
1449+ if self
1450+ .dissolve
1451+ .as_ref()
1452+ .is_some_and(|d| frame.index >= d.tail_start)
1453+ {
1454+ self.open_dissolve_b();
1455+ }
1456+ if let Some(d) = self.dissolve.as_mut()
1457+ && frame.index >= d.tail_start
1458+ && let Some(b) = d.b.as_mut()
1459+ {
1460+ while let Ok(bf) = b.frames.try_recv() {
1461+ b.held = Some(bf);
1462+ }
1463+ if let Some(bf) = &b.held {
1464+ let idx_in_window = (frame.index - d.tail_start).min(d.window - 1);
1465+ let t = crate::export::dissolve_weight(idx_in_window, d.window);
1466+ frame.bgra = blend_bgra(&frame.bgra, &bf.bgra, t);
1467+ }
1468+ }
13961469 return Some(frame);
13971470 }
13981471 Err(TryRecvError::Empty) => return None,
@@ -1562,6 +1635,34 @@ impl PlaybackSession {
15621635 /// video that is now the worker's own doing -- it opens the file, so it is
15631636 /// the one that finds out -- and nothing on this thread waits to hear it.
15641637 fn start_span(&mut self, span: Option<Span>) {
1638+ // The outgoing span's own dissolve, if it ever opened B's decoder:
1639+ // that decoder's pictures were for a window this span is leaving
1640+ // (played through, paused before, or seeked away from), and never
1641+ // for the one about to start -- parked in `retired` exactly like the
1642+ // picture worker below, so joining it costs this thread nothing.
1643+ if let Some(Dissolve { b: Some(b), .. }) = self.dissolve.take() {
1644+ let mut worker = b.worker;
1645+ worker.cancel();
1646+ self.retired.push(worker);
1647+ self.retired.retain(|w| !w.is_finished());
1648+ }
1649+ // What the *new* span's own tail owes its successor -- checked here,
1650+ // once, rather than every tick: adjacency can only change on an edit,
1651+ // and every edit that could touches this span starts it over
1652+ // ([`crate::export::dissolve_window`] makes the identical check for
1653+ // the same reason, on export).
1654+ self.dissolve = span.and_then(|s| {
1655+ crate::export::dissolve_window(&self.project, &s).map(
1656+ |(b_source, b_in_frame, window, b_start)| Dissolve {
1657+ tail_start: s.start + s.len - window,
1658+ window,
1659+ b_source,
1660+ b_in_frame,
1661+ b_start,
1662+ b: None,
1663+ },
1664+ )
1665+ });
15651666 // Which file's frames the worker about to be opened will number its
15661667 // pictures in: the span's own source, and real time for a gap, whose
15671668 // black worker counts timeline frames already. Set before the open,
@@ -1732,6 +1833,55 @@ impl PlaybackSession {
17321833 self.span = span;
17331834 }
17341835
1836+ /// Opens B's own decoder for the current span's dissolve, the first time
1837+ /// [`try_frame`](Self::try_frame) sees the playhead inside the window --
1838+ /// a no-op once it is already open, or if this span has no dissolve at
1839+ /// all. Everything [`start_span`](Self::start_span) works out for A's own
1840+ /// decoder, worked out again for B: the stand-in switch
1841+ /// ([`Self::picture_path`]), the file's own frame numbering
1842+ /// ([`Self::rates`]), and B's *own* grade and fit, asked at B's own start
1843+ /// on the timeline rather than at the position the blend writes to
1844+ /// (still inside A's span) -- the same asymmetry
1845+ /// [`crate::export`]'s own `Dissolve` is built with.
1846+ fn open_dissolve_b(&mut self) {
1847+ let Some(d) = &self.dissolve else { return };
1848+ if d.b.is_some() {
1849+ return;
1850+ }
1851+ let (source, in_frame, window, b_start) = (d.b_source, d.b_in_frame, d.window, d.b_start);
1852+ let path = self.picture_path(source);
1853+ let rate = self.rates.get(source).copied().unwrap_or(Rate::REAL_TIME);
1854+ let start_frame = rate.source_at(in_frame);
1855+ let end_frame = rate.source_at(in_frame + window);
1856+ let color = self
1857+ .project
1858+ .composite_color_at(b_start)
1859+ .copied()
1860+ .unwrap_or_default();
1861+ let canvas = Composer::new(
1862+ self.meta.width,
1863+ self.meta.height,
1864+ self.project.composite_fit_at(b_start),
1865+ );
1866+ let tone = self.project.tone();
1867+ let stream = DecodeSession::open_worker_deferred(
1868+ &path,
1869+ start_frame,
1870+ end_frame,
1871+ color,
1872+ canvas,
1873+ tone,
1874+ Speed::NORMAL,
1875+ );
1876+ if let Some(d) = self.dissolve.as_mut() {
1877+ d.b = Some(DissolveB {
1878+ frames: stream.frames,
1879+ worker: stream.worker,
1880+ held: None,
1881+ });
1882+ }
1883+ }
1884+
17351885 /// Length of the edited timeline in seconds -- what a ruler shows, and it
17361886 /// shrinks with every delete.
17371887 pub fn timeline_duration(&self) -> f64 {
@@ -3872,9 +4022,25 @@ fn secs_to_frame(secs: f64, fps: f64) -> u32 {
38724022 (secs * fps + 1e-6).floor().max(0.0) as u32
38734023 }
38744024
4025+/// Per-byte lerp of two same-sized BGRA8 frames -- [`crate::scale::blend_i420`]'s
4026+/// interleaved-buffer twin. By the time a picture reaches [`try_frame`]
4027+/// (`PlaybackSession::try_frame`) it is already colour-converted for the
4028+/// screen, so there are no separate Y/U/V planes left to blend, only the one
4029+/// buffer; straight alpha survives the lerp untouched (255 against 255 is
4030+/// still 255). `t` is B's weight, [`crate::export::dissolve_weight`]'s own
4031+/// convention.
4032+fn blend_bgra(a: &[u8], b: &[u8], t: f32) -> Vec<u8> {
4033+ let t = t.clamp(0.0, 1.0);
4034+ assert_eq!(a.len(), b.len(), "blend_bgra: mismatched frame sizes");
4035+ a.iter()
4036+ .zip(b)
4037+ .map(|(&av, &bv)| (av as f32 + (bv as f32 - av as f32) * t).round() as u8)
4038+ .collect()
4039+}
4040+
38754041 #[cfg(test)]
38764042 mod tests {
3877- use super::{Edge, Lane, PlaybackSession, Source, audio_source_of, secs_to_frame};
4043+ use super::{Edge, Lane, PlaybackSession, Source, audio_source_of, blend_bgra, secs_to_frame};
38784044 use std::path::PathBuf;
38794045
38804046 fn asset(name: &str) -> PathBuf {
@@ -3883,6 +4049,41 @@ mod tests {
38834049 .join(name)
38844050 }
38854051
4052+ /// `blend_bgra` at the two ends and the midpoint of its weight -- `t=0` is
4053+ /// pure A, `t=1` is pure B, `t=0.5` rounds each byte to its own midpoint.
4054+ /// Straight alpha (255 in both) survives every weight, which is the one
4055+ /// channel a wrong blend would otherwise fog.
4056+ #[test]
4057+ fn blend_bgra_interpolates_every_byte_including_alpha() {
4058+ let a = [10u8, 20, 30, 255];
4059+ let b = [110u8, 120, 130, 255];
4060+ assert_eq!(blend_bgra(&a, &b, 0.0), a);
4061+ assert_eq!(blend_bgra(&a, &b, 1.0), b);
4062+ assert_eq!(blend_bgra(&a, &b, 0.5), [60, 70, 80, 255]);
4063+ }
4064+
4065+ /// The dissolve window's own edges, in the units [`PlaybackSession::start_span`]
4066+ /// computes them in: a `tail_start` of `span.start + span.len - window`
4067+ /// puts the very first output frame of the window at `idx_in_window == 0`
4068+ /// and the very last at `window - 1`, clamped rather than overrun by a
4069+ /// frame that lands exactly on the span's own end -- the same off-by-one
4070+ /// a stray `%` or unclamped subtraction would have shown on screen as a
4071+ /// blend that never quite reached full B.
4072+ #[test]
4073+ fn dissolve_window_edges_index_zero_to_window_minus_one() {
4074+ let (span_start, span_len, window) = (100u32, 30u32, 10u32);
4075+ let tail_start = span_start + span_len - window;
4076+ assert_eq!(tail_start, 120);
4077+ let idx_in_window = |frame: u32| (frame - tail_start).min(window - 1);
4078+ assert_eq!(idx_in_window(120), 0, "first frame of the window");
4079+ assert_eq!(idx_in_window(129), 9, "last frame of the window");
4080+ assert_eq!(
4081+ idx_in_window(130),
4082+ 9,
4083+ "one past the span's own end clamps rather than panics"
4084+ );
4085+ }
4086+
38864087 /// A picked project rate overrides the derived one, and its absence leaves
38874088 /// the source's own -- the seam every explicit-sample-rate caller
38884089 /// (playback rebuild, both export encoders) goes through