forge
Explore
Log in

Don't you dare go Hollow

Playback above 1x skips the pictures nobody will see

Browse files at this commit

490568c8afe894802afda0d59913ecba2c4f3ddeAhmet Kaan GümüşParent: e6a4f4543ff2f2
  • perf
    • playback above 1x skips the pictures nobody will see
Show full message
🚀 perf(engine): playback above 1x skips the pictures nobody will see

Clip speed threads into the decode worker; frames a faster playhead
supersedes skip tone-map/convert/send while still bitstream-decoding
for references. The late-drop escape valve always fires. Export path
unchanged (always Speed::NORMAL). Measured: 4K SW decode at 1x p99
frame gap 1131ms->76ms, resyncs 5->2; OBS-class stays at 0 resyncs.
crates/engine/src/decode.rsmodified+116 -6
@@ -14,6 +14,7 @@ use crate::colorspace::{ColorDescription, Matrix, Transfer};
1414 use crate::convert::{i420_to_bgra, i420_to_bgra_with};
1515 use crate::demux::{Codec, Demuxer, VideoMeta};
1616 use crate::hw::HwSession;
17+use crate::project::Speed;
1718 use crate::scale::Composer;
1819 use crate::tonemap::{self, ToneMapper};
1920
@@ -177,6 +178,12 @@ struct SpanCmd {
177178 color: ColorParams,
178179 canvas: Composer,
179180 tone: tonemap::Preset,
181+ /// The clip's own playback speed ([`crate::project::Clip::speed`]), for
182+ /// [`skip_for_speed`]: at faster than real time several source frames in a
183+ /// row land on the same timeline frame, and only the last of a run is ever
184+ /// shown. [`Speed::NORMAL`] for a file-level open ([`DecodeSession::open`]
185+ /// and friends), which has no clip and skips nothing.
186+ speed: Speed,
180187 tx: SyncSender<Frame>,
181188 }
182189
@@ -213,6 +220,33 @@ impl Abort<'_> {
213220 }
214221 }
215222
223+/// Whether the picture at source frame `due` (of a span starting at `start` and
224+/// ending at `end`) is redundant *by construction* at `speed`, and its convert
225+/// and send can be skipped without ever waiting on the playhead
226+/// ([`Abort::late`]): at faster than real time several source frames in a row
227+/// map to the same timeline frame ([`Speed::timeline_at`]), and only the last
228+/// of such a run is ever shown -- the newest one always lands after the others
229+/// in `try_frame`'s stamp comparison. Unlike a late drop this needs no run
230+/// limit: the gap between two kept frames is at most `speed` source frames, so
231+/// the picture stream this leaves is never silent, only sparser.
232+///
233+/// The last frame of the whole span is never skipped by this: it is the
234+/// picture [`crate::project::Span::source_len`] was built to land exactly on,
235+/// and this is what proves that promise here instead of trusting it blind.
236+///
237+/// corner-cut: `due` and `start` are the file's own frame numbers, so this
238+/// is only correct when the file plays at the timeline's own rate
239+/// ([`crate::project::Rate::is_real_time`]) -- callers pass [`Speed::NORMAL`]
240+/// (never skip) otherwise. Upgrade path is composing `Rate` into this the way
241+/// [`crate::project::Span::timeline_at`] does.
242+fn skip_for_speed(speed: Speed, start: u32, due: u32, end: u32) -> bool {
243+ if speed.is_normal() || due + 1 >= end {
244+ return false;
245+ }
246+ let rel = due - start;
247+ speed.timeline_at(rel) == speed.timeline_at(rel + 1)
248+}
249+
216250 impl Worker {
217251 /// Stops the worker *and its thread* at their next check, without waiting.
218252 /// Callers that replace a worker should do this first and drop the receiver
@@ -267,6 +301,7 @@ impl Worker {
267301 /// in `send`, and only then does it reach the command below. So the order at
268302 /// the call site is unchanged -- install the new receiver, and the old
269303 /// worker finds out by losing its consumer.
304+ #[allow(clippy::too_many_arguments)]
270305 pub(crate) fn reseek(
271306 &mut self,
272307 path: &Path,
@@ -275,6 +310,7 @@ impl Worker {
275310 color: ColorParams,
276311 canvas: Composer,
277312 tone: tonemap::Preset,
313+ speed: Speed,
278314 ) -> Option<(Receiver<Frame>, BackendCell)> {
279315 let reuse = self.reuse.as_ref()?;
280316 if reuse.path != path || self.is_finished() {
@@ -297,6 +333,7 @@ impl Worker {
297333 color,
298334 canvas,
299335 tone,
336+ speed,
300337 tx,
301338 })
302339 .ok()?;
@@ -615,6 +652,9 @@ impl DecodeSession {
615652 color,
616653 canvas,
617654 tone,
655+ // A file-level open has no clip and no speed to skip frames
656+ // for: every picture in range is delivered, as it always was.
657+ Speed::NORMAL,
618658 ),
619659 ))
620660 }
@@ -631,6 +671,7 @@ impl DecodeSession {
631671 /// carries a session on to the next span, which is exactly what a failed
632672 /// open did on this path before. Callers that must *tell* the user why --
633673 /// an import at the door -- keep the sync opener.
674+ #[allow(clippy::too_many_arguments)]
634675 pub(crate) fn open_worker_deferred(
635676 path: impl AsRef<Path>,
636677 start_frame: u32,
@@ -638,6 +679,7 @@ impl DecodeSession {
638679 color: ColorParams,
639680 canvas: Composer,
640681 tone: tonemap::Preset,
682+ speed: Speed,
641683 ) -> FrameStream {
642684 span_worker(
643685 path.as_ref().to_path_buf(),
@@ -647,6 +689,7 @@ impl DecodeSession {
647689 color,
648690 canvas,
649691 tone,
692+ speed,
650693 )
651694 }
652695 }
@@ -669,6 +712,7 @@ fn span_worker(
669712 color: ColorParams,
670713 canvas: Composer,
671714 tone: tonemap::Preset,
715+ speed: Speed,
672716 ) -> FrameStream {
673717 // The depth is the size of the pictures this worker will *emit*, which for a
674718 // pass-through canvas is the stream's own -- and the sync opener has already
@@ -698,6 +742,7 @@ fn span_worker(
698742 color,
699743 canvas,
700744 tone,
745+ speed,
701746 tx,
702747 };
703748 let worker_cancel = Arc::clone(&cancel);
@@ -876,6 +921,7 @@ fn run_span(
876921 color,
877922 canvas,
878923 tone,
924+ speed,
879925 tx,
880926 ..
881927 } = cmd;
@@ -915,6 +961,7 @@ fn run_span(
915961 end,
916962 &mut render,
917963 abort,
964+ speed,
918965 );
919966 // A *reused* session that produced nothing may simply be one this
920967 // reseek left where the driver would not follow; a session opened fresh
@@ -925,7 +972,7 @@ fn run_span(
925972 eprintln!("hardware decode produced nothing after a reseek; reopening at frame {start}");
926973 opened.hw = open_hw(path, start);
927974 if let Some(hw) = opened.hw.as_mut() {
928- decoded = run_hw(hw, &tx, start, end, &mut render, abort);
975+ decoded = run_hw(hw, &tx, start, end, &mut render, abort, speed);
929976 }
930977 }
931978 if decoded {
@@ -944,7 +991,7 @@ fn run_span(
944991 }
945992 eprintln!("decode backend: software (rusty_h264)");
946993 backend.set(Backend::Software);
947- run(&mut opened.demuxer, &tx, start, end, &mut render, abort)
994+ run(&mut opened.demuxer, &tx, start, end, &mut render, abort, speed)
948995 }
949996
950997 /// One clip's pictures on their way to the renderer: graded, placed on the
@@ -1242,6 +1289,7 @@ fn run_hw(
12421289 end_frame: u32,
12431290 render: &mut Render,
12441291 abort: &Abort,
1292+ speed: Speed,
12451293 ) -> bool {
12461294 let mut index = start_frame;
12471295 // Pictures dropped for being late since the last one handed over; see
@@ -1255,8 +1303,19 @@ fn run_hw(
12551303 Ok(Some((y, u, v, width, height))) => {
12561304 let due = index;
12571305 index += 1;
1258- if abort.late(due) && skipped < LATE_RUN {
1259- skipped += 1;
1306+ if abort.late(due) {
1307+ if skipped < LATE_RUN {
1308+ skipped += 1;
1309+ } else {
1310+ skipped = 0;
1311+ let frame = render.frame(due, y, u, v, width, height);
1312+ if tx.send(frame).is_err() {
1313+ return true; // consumer went away
1314+ }
1315+ }
1316+ } else if skip_for_speed(speed, start_frame, due, end_frame) {
1317+ // Redundant by construction, not by lateness: no run limit
1318+ // needed (see `skip_for_speed`).
12601319 } else {
12611320 skipped = 0;
12621321 let frame = render.frame(due, y, u, v, width, height);
@@ -1327,6 +1386,7 @@ mod tests {
13271386 ColorParams::default(),
13281387 Composer::passthrough(),
13291388 tonemap::Preset::default(),
1389+ Speed::NORMAL,
13301390 );
13311391 let returned = start.elapsed();
13321392 let frame = stream
@@ -1393,6 +1453,7 @@ mod tests {
13931453 ColorParams::default(),
13941454 Composer::passthrough(),
13951455 tonemap::Preset::default(),
1456+ Speed::NORMAL,
13961457 );
13971458 stream.worker.playhead(floor);
13981459 let mut indices = Vec::new();
@@ -1427,6 +1488,36 @@ mod tests {
14271488 );
14281489 }
14291490
1491+ /// [`skip_for_speed`] on its own, with no worker or channel involved: at
1492+ /// 2x every other source frame maps onto the same timeline frame
1493+ /// ([`Speed::timeline_at`]), so `skip`/`keep` must alternate exactly, and
1494+ /// the last frame of the span (the one [`crate::project::Span::source_len`]
1495+ /// is built to land on) must never be skipped regardless of where the
1496+ /// alternation lands.
1497+ #[test]
1498+ fn skip_for_speed_alternates_at_2x_and_never_drops_the_last_frame() {
1499+ let speed = Speed::from_permille(2000);
1500+ let start = 100;
1501+ let end = 110;
1502+
1503+ let skips: Vec<bool> = (start..end)
1504+ .map(|due| skip_for_speed(speed, start, due, end))
1505+ .collect();
1506+ // Alternation holds up to (but not including) the span's last frame,
1507+ // which is special-cased below.
1508+ for pair in (start..end - 2).zip(skips.windows(2)) {
1509+ let (due, w) = pair;
1510+ assert_eq!(
1511+ w[0], !w[1],
1512+ "frame {due} and its successor should alternate skip/keep at 2x: {skips:?}"
1513+ );
1514+ }
1515+ assert!(
1516+ !skip_for_speed(speed, start, end - 1, end),
1517+ "the last frame of the span must never be skipped"
1518+ );
1519+ }
1520+
14301521 /// Dropping a [`FrameStream`] whose frames nobody ever took must not wait.
14311522 /// The channel is bounded, so such a worker is parked in `send` within a
14321523 /// few frames, and the only thing that can wake it is its receiver going
@@ -1471,6 +1562,7 @@ fn run(
14711562 end_frame: u32,
14721563 render: &mut Render,
14731564 abort: &Abort,
1565+ speed: Speed,
14741566 ) {
14751567 // A decoder per span, and cheap: it is a parameter-set map and a picture
14761568 // buffer, while the *demuxer* -- whose index cost seconds to build -- is the
@@ -1511,8 +1603,26 @@ fn run(
15111603 }
15121604 let due = index as u32;
15131605 index += 1;
1514- if abort.late(due) && skipped < LATE_RUN {
1515- skipped += 1;
1606+ if abort.late(due) {
1607+ if skipped < LATE_RUN {
1608+ skipped += 1;
1609+ } else {
1610+ skipped = 0;
1611+ let frame = render.frame(
1612+ due,
1613+ &yuv.y,
1614+ &yuv.u,
1615+ &yuv.v,
1616+ yuv.width as u32,
1617+ yuv.height as u32,
1618+ );
1619+ if tx.send(frame).is_err() {
1620+ break; // consumer went away
1621+ }
1622+ }
1623+ } else if skip_for_speed(speed, start_frame, due, end_frame) {
1624+ // Redundant by construction, not by lateness: no run limit needed
1625+ // (see `skip_for_speed`).
15161626 } else {
15171627 skipped = 0;
15181628 let frame = render.frame(
crates/engine/src/playback.rsmodified+28 -4
@@ -1561,6 +1561,7 @@ impl PlaybackSession {
15611561 Some(Span {
15621562 start,
15631563 from: Some((source, in_frame)),
1564+ speed,
15641565 ..
15651566 }) => {
15661567 // The stand-in where there is one and the switch is on, the
@@ -1603,15 +1604,37 @@ impl PlaybackSession {
16031604 // the project's own and constant across the span for that
16041605 // reason too.
16051606 let tone = self.project.tone();
1607+ // At faster than real time several source frames decode to the
1608+ // same timeline frame ([`Speed::timeline_at`]) and only the last
1609+ // of a run is ever shown; the worker skips converting the rest
1610+ // ([`crate::decode::skip_for_speed`]) rather than paying a full
1611+ // colour convert and scale for a picture nothing displays.
1612+ //
1613+ // corner-cut: correct only when this file plays at the
1614+ // timeline's own rate -- `skip_for_speed`'s own math is in the
1615+ // file's frame numbers, which a `Rate` conversion would make a
1616+ // different sequence than the timeline's. `Speed::NORMAL` (never
1617+ // skip) covers that rarer case exactly as every frame was
1618+ // delivered before this.
1619+ let decode_speed = if self.span_rate.is_real_time() {
1620+ speed
1621+ } else {
1622+ Speed::NORMAL
1623+ };
16061624 // The worker already decoding this very file takes the new span
16071625 // itself: no thread, no container parse, no VA-API init -- the
16081626 // whole of what a seek used to cost. It answers `None` for any
16091627 // other file (and for a worker whose thread has ended), and the
16101628 // deferred opener below is then exactly the path it always was.
1611- if let Some((frames, backend)) =
1612- self.worker
1613- .reseek(&path, start_frame, end_frame, color, canvas(), tone)
1614- {
1629+ if let Some((frames, backend)) = self.worker.reseek(
1630+ &path,
1631+ start_frame,
1632+ end_frame,
1633+ color,
1634+ canvas(),
1635+ tone,
1636+ decode_speed,
1637+ ) {
16151638 self.frames = frames;
16161639 self.backend = backend;
16171640 self.span = span;
@@ -1624,6 +1647,7 @@ impl PlaybackSession {
16241647 color,
16251648 canvas(),
16261649 tone,
1650+ decode_speed,
16271651 ))
16281652 }
16291653 // A gap: black for as long as it runs. An emptied timeline has no