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
1032 /// here: a trim or a split since it was set leaves the stored number in1032 /// here: a trim or a split since it was set leaves the stored number in
1033 /// place but not its meaning, so a stale one plays as the hard cut it now1033 /// place but not its meaning, so a stale one plays as the hard cut it now
1034 /// is rather than as a dissolve into a clip that no longer follows.1034 /// 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)> {
1036 let (source, _) = span.from?;1039 let (source, _) = span.from?;
1037 let (lane, idx) = project.composite_clip_at(span.start)?;1040 let (lane, idx) = project.composite_clip_at(span.start)?;
1038 let clips = project.lane(lane);1041 let clips = project.lane(lane);
@@ -1064,7 +1067,7 @@ fn dissolve_window(project: &Project, span: &crate::project::Span) -> Option<(us
1064 /// a trace of A and the last still a trace of B -- the cut a hard transition1067 /// a trace of A and the last still a trace of B -- the cut a hard transition
1065 /// would have made lands one frame *after* the window, at full B, rather than1068 /// would have made lands one frame *after* the window, at full B, rather than
1066 /// inside it.1069 /// 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 {
1068 (idx_in_window + 1) as f32 / (window + 1) as f321071 (idx_in_window + 1) as f32 / (window + 1) as f32
1069 }1072 }
1070 1073
crates/engine/src/playback.rsmodified+202 -1
@@ -257,6 +257,42 @@ impl Audio {
257 }257 }
258 }258 }
259 259
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+
260 /// A file opened for playback. Starts paused at t=0; call [`PlaybackSession::play`].296 /// A file opened for playback. Starts paused at t=0; call [`PlaybackSession::play`].
261 pub struct PlaybackSession {297 pub struct PlaybackSession {
262 /// The *timeline's* parameters, not any one file's: its frame rate and codec298 /// The *timeline's* parameters, not any one file's: its frame rate and codec
@@ -368,6 +404,13 @@ pub struct PlaybackSession {
368 /// is late by exactly this window's length with another restart of it404 /// is late by exactly this window's length with another restart of it
369 /// ([`picture_priming`](Self::picture_priming)).405 /// ([`picture_priming`](Self::picture_priming)).
370 span_priming: bool,406 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>,
371 /// The last clip has been played out; see [`PlaybackSession::is_eos`].414 /// The last clip has been played out; see [`PlaybackSession::is_eos`].
372 eos: bool,415 eos: bool,
373 /// The mix the running mixer is reading, when there is one: what lets a416 /// The mix the running mixer is reading, when there is one: what lets a
@@ -547,6 +590,7 @@ impl PlaybackSession {
547 span_rate: Rate::REAL_TIME,590 span_rate: Rate::REAL_TIME,
548 span,591 span,
549 span_priming: true,592 span_priming: true,
593+ dissolve: None,
550 eos: false,594 eos: false,
551 mix: None,595 mix: None,
552 priming: false,596 priming: false,
@@ -669,6 +713,7 @@ impl PlaybackSession {
669 span_rate: Rate::REAL_TIME,713 span_rate: Rate::REAL_TIME,
670 span,714 span,
671 span_priming: true,715 span_priming: true,
716+ dissolve: None,
672 eos: false,717 eos: false,
673 mix: None,718 mix: None,
674 priming: false,719 priming: false,
@@ -755,6 +800,7 @@ impl PlaybackSession {
755 span_rate: Rate::REAL_TIME,800 span_rate: Rate::REAL_TIME,
756 span,801 span,
757 span_priming: true,802 span_priming: true,
803+ dissolve: None,
758 eos: false,804 eos: false,
759 mix: None,805 mix: None,
760 priming: false,806 priming: false,
@@ -1059,6 +1105,7 @@ impl PlaybackSession {
1059 span_rate: Rate::REAL_TIME,1105 span_rate: Rate::REAL_TIME,
1060 span,1106 span,
1061 span_priming: true,1107 span_priming: true,
1108+ dissolve: None,
1062 eos: false,1109 eos: false,
1063 mix: None,1110 mix: None,
1064 priming: false,1111 priming: false,
@@ -1393,6 +1440,32 @@ impl PlaybackSession {
1393 // The first picture out of the span now decoding ends its1440 // The first picture out of the span now decoding ends its
1394 // prime ([`Self::span_priming`]).1441 // prime ([`Self::span_priming`]).
1395 self.span_priming = false;1442 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+ }
1396 return Some(frame);1469 return Some(frame);
1397 }1470 }
1398 Err(TryRecvError::Empty) => return None,1471 Err(TryRecvError::Empty) => return None,
@@ -1562,6 +1635,34 @@ impl PlaybackSession {
1562 /// video that is now the worker's own doing -- it opens the file, so it is1635 /// video that is now the worker's own doing -- it opens the file, so it is
1563 /// the one that finds out -- and nothing on this thread waits to hear it.1636 /// the one that finds out -- and nothing on this thread waits to hear it.
1564 fn start_span(&mut self, span: Option<Span>) {1637 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+ });
1565 // Which file's frames the worker about to be opened will number its1666 // Which file's frames the worker about to be opened will number its
1566 // pictures in: the span's own source, and real time for a gap, whose1667 // pictures in: the span's own source, and real time for a gap, whose
1567 // black worker counts timeline frames already. Set before the open,1668 // black worker counts timeline frames already. Set before the open,
@@ -1732,6 +1833,55 @@ impl PlaybackSession {
1732 self.span = span;1833 self.span = span;
1733 }1834 }
1734 1835
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+
1735 /// Length of the edited timeline in seconds -- what a ruler shows, and it1885 /// Length of the edited timeline in seconds -- what a ruler shows, and it
1736 /// shrinks with every delete.1886 /// shrinks with every delete.
1737 pub fn timeline_duration(&self) -> f64 {1887 pub fn timeline_duration(&self) -> f64 {
@@ -3872,9 +4022,25 @@ fn secs_to_frame(secs: f64, fps: f64) -> u32 {
3872 (secs * fps + 1e-6).floor().max(0.0) as u324022 (secs * fps + 1e-6).floor().max(0.0) as u32
3873 }4023 }
3874 4024
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+
3875 #[cfg(test)]4041 #[cfg(test)]
3876 mod tests {4042 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};
3878 use std::path::PathBuf;4044 use std::path::PathBuf;
3879 4045
3880 fn asset(name: &str) -> PathBuf {4046 fn asset(name: &str) -> PathBuf {
@@ -3883,6 +4049,41 @@ mod tests {
3883 .join(name)4049 .join(name)
3884 }4050 }
3885 4051
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+
3886 /// A picked project rate overrides the derived one, and its absence leaves4087 /// A picked project rate overrides the derived one, and its absence leaves
3887 /// the source's own -- the seam every explicit-sample-rate caller4088 /// the source's own -- the seam every explicit-sample-rate caller
3888 /// (playback rebuild, both export encoders) goes through4089 /// (playback rebuild, both export encoders) goes through