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};
14 use crate::convert::{i420_to_bgra, i420_to_bgra_with};14 use crate::convert::{i420_to_bgra, i420_to_bgra_with};
15 use crate::demux::{Codec, Demuxer, VideoMeta};15 use crate::demux::{Codec, Demuxer, VideoMeta};
16 use crate::hw::HwSession;16 use crate::hw::HwSession;
17+use crate::project::Speed;
17 use crate::scale::Composer;18 use crate::scale::Composer;
18 use crate::tonemap::{self, ToneMapper};19 use crate::tonemap::{self, ToneMapper};
19 20
@@ -177,6 +178,12 @@ struct SpanCmd {
177 color: ColorParams,178 color: ColorParams,
178 canvas: Composer,179 canvas: Composer,
179 tone: tonemap::Preset,180 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,
180 tx: SyncSender<Frame>,187 tx: SyncSender<Frame>,
181 }188 }
182 189
@@ -213,6 +220,33 @@ impl Abort<'_> {
213 }220 }
214 }221 }
215 222
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+
216 impl Worker {250 impl Worker {
217 /// Stops the worker *and its thread* at their next check, without waiting.251 /// Stops the worker *and its thread* at their next check, without waiting.
218 /// Callers that replace a worker should do this first and drop the receiver252 /// Callers that replace a worker should do this first and drop the receiver
@@ -267,6 +301,7 @@ impl Worker {
267 /// in `send`, and only then does it reach the command below. So the order at301 /// in `send`, and only then does it reach the command below. So the order at
268 /// the call site is unchanged -- install the new receiver, and the old302 /// the call site is unchanged -- install the new receiver, and the old
269 /// worker finds out by losing its consumer.303 /// worker finds out by losing its consumer.
304+ #[allow(clippy::too_many_arguments)]
270 pub(crate) fn reseek(305 pub(crate) fn reseek(
271 &mut self,306 &mut self,
272 path: &Path,307 path: &Path,
@@ -275,6 +310,7 @@ impl Worker {
275 color: ColorParams,310 color: ColorParams,
276 canvas: Composer,311 canvas: Composer,
277 tone: tonemap::Preset,312 tone: tonemap::Preset,
313+ speed: Speed,
278 ) -> Option<(Receiver<Frame>, BackendCell)> {314 ) -> Option<(Receiver<Frame>, BackendCell)> {
279 let reuse = self.reuse.as_ref()?;315 let reuse = self.reuse.as_ref()?;
280 if reuse.path != path || self.is_finished() {316 if reuse.path != path || self.is_finished() {
@@ -297,6 +333,7 @@ impl Worker {
297 color,333 color,
298 canvas,334 canvas,
299 tone,335 tone,
336+ speed,
300 tx,337 tx,
301 })338 })
302 .ok()?;339 .ok()?;
@@ -615,6 +652,9 @@ impl DecodeSession {
615 color,652 color,
616 canvas,653 canvas,
617 tone,654 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,
618 ),658 ),
619 ))659 ))
620 }660 }
@@ -631,6 +671,7 @@ impl DecodeSession {
631 /// carries a session on to the next span, which is exactly what a failed671 /// carries a session on to the next span, which is exactly what a failed
632 /// open did on this path before. Callers that must *tell* the user why --672 /// open did on this path before. Callers that must *tell* the user why --
633 /// an import at the door -- keep the sync opener.673 /// an import at the door -- keep the sync opener.
674+ #[allow(clippy::too_many_arguments)]
634 pub(crate) fn open_worker_deferred(675 pub(crate) fn open_worker_deferred(
635 path: impl AsRef<Path>,676 path: impl AsRef<Path>,
636 start_frame: u32,677 start_frame: u32,
@@ -638,6 +679,7 @@ impl DecodeSession {
638 color: ColorParams,679 color: ColorParams,
639 canvas: Composer,680 canvas: Composer,
640 tone: tonemap::Preset,681 tone: tonemap::Preset,
682+ speed: Speed,
641 ) -> FrameStream {683 ) -> FrameStream {
642 span_worker(684 span_worker(
643 path.as_ref().to_path_buf(),685 path.as_ref().to_path_buf(),
@@ -647,6 +689,7 @@ impl DecodeSession {
647 color,689 color,
648 canvas,690 canvas,
649 tone,691 tone,
692+ speed,
650 )693 )
651 }694 }
652 }695 }
@@ -669,6 +712,7 @@ fn span_worker(
669 color: ColorParams,712 color: ColorParams,
670 canvas: Composer,713 canvas: Composer,
671 tone: tonemap::Preset,714 tone: tonemap::Preset,
715+ speed: Speed,
672 ) -> FrameStream {716 ) -> FrameStream {
673 // The depth is the size of the pictures this worker will *emit*, which for a717 // The depth is the size of the pictures this worker will *emit*, which for a
674 // pass-through canvas is the stream's own -- and the sync opener has already718 // pass-through canvas is the stream's own -- and the sync opener has already
@@ -698,6 +742,7 @@ fn span_worker(
698 color,742 color,
699 canvas,743 canvas,
700 tone,744 tone,
745+ speed,
701 tx,746 tx,
702 };747 };
703 let worker_cancel = Arc::clone(&cancel);748 let worker_cancel = Arc::clone(&cancel);
@@ -876,6 +921,7 @@ fn run_span(
876 color,921 color,
877 canvas,922 canvas,
878 tone,923 tone,
924+ speed,
879 tx,925 tx,
880 ..926 ..
881 } = cmd;927 } = cmd;
@@ -915,6 +961,7 @@ fn run_span(
915 end,961 end,
916 &mut render,962 &mut render,
917 abort,963 abort,
964+ speed,
918 );965 );
919 // A *reused* session that produced nothing may simply be one this966 // A *reused* session that produced nothing may simply be one this
920 // reseek left where the driver would not follow; a session opened fresh967 // reseek left where the driver would not follow; a session opened fresh
@@ -925,7 +972,7 @@ fn run_span(
925 eprintln!("hardware decode produced nothing after a reseek; reopening at frame {start}");972 eprintln!("hardware decode produced nothing after a reseek; reopening at frame {start}");
926 opened.hw = open_hw(path, start);973 opened.hw = open_hw(path, start);
927 if let Some(hw) = opened.hw.as_mut() {974 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);
929 }976 }
930 }977 }
931 if decoded {978 if decoded {
@@ -944,7 +991,7 @@ fn run_span(
944 }991 }
945 eprintln!("decode backend: software (rusty_h264)");992 eprintln!("decode backend: software (rusty_h264)");
946 backend.set(Backend::Software);993 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)
948 }995 }
949 996
950 /// One clip's pictures on their way to the renderer: graded, placed on the997 /// One clip's pictures on their way to the renderer: graded, placed on the
@@ -1242,6 +1289,7 @@ fn run_hw(
1242 end_frame: u32,1289 end_frame: u32,
1243 render: &mut Render,1290 render: &mut Render,
1244 abort: &Abort,1291 abort: &Abort,
1292+ speed: Speed,
1245 ) -> bool {1293 ) -> bool {
1246 let mut index = start_frame;1294 let mut index = start_frame;
1247 // Pictures dropped for being late since the last one handed over; see1295 // Pictures dropped for being late since the last one handed over; see
@@ -1255,8 +1303,19 @@ fn run_hw(
1255 Ok(Some((y, u, v, width, height))) => {1303 Ok(Some((y, u, v, width, height))) => {
1256 let due = index;1304 let due = index;
1257 index += 1;1305 index += 1;
1258- if abort.late(due) && skipped < LATE_RUN {1306+ if abort.late(due) {
1259- skipped += 1;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`).
1260 } else {1319 } else {
1261 skipped = 0;1320 skipped = 0;
1262 let frame = render.frame(due, y, u, v, width, height);1321 let frame = render.frame(due, y, u, v, width, height);
@@ -1327,6 +1386,7 @@ mod tests {
1327 ColorParams::default(),1386 ColorParams::default(),
1328 Composer::passthrough(),1387 Composer::passthrough(),
1329 tonemap::Preset::default(),1388 tonemap::Preset::default(),
1389+ Speed::NORMAL,
1330 );1390 );
1331 let returned = start.elapsed();1391 let returned = start.elapsed();
1332 let frame = stream1392 let frame = stream
@@ -1393,6 +1453,7 @@ mod tests {
1393 ColorParams::default(),1453 ColorParams::default(),
1394 Composer::passthrough(),1454 Composer::passthrough(),
1395 tonemap::Preset::default(),1455 tonemap::Preset::default(),
1456+ Speed::NORMAL,
1396 );1457 );
1397 stream.worker.playhead(floor);1458 stream.worker.playhead(floor);
1398 let mut indices = Vec::new();1459 let mut indices = Vec::new();
@@ -1427,6 +1488,36 @@ mod tests {
1427 );1488 );
1428 }1489 }
1429 1490
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+
1430 /// Dropping a [`FrameStream`] whose frames nobody ever took must not wait.1521 /// Dropping a [`FrameStream`] whose frames nobody ever took must not wait.
1431 /// The channel is bounded, so such a worker is parked in `send` within a1522 /// The channel is bounded, so such a worker is parked in `send` within a
1432 /// few frames, and the only thing that can wake it is its receiver going1523 /// few frames, and the only thing that can wake it is its receiver going
@@ -1471,6 +1562,7 @@ fn run(
1471 end_frame: u32,1562 end_frame: u32,
1472 render: &mut Render,1563 render: &mut Render,
1473 abort: &Abort,1564 abort: &Abort,
1565+ speed: Speed,
1474 ) {1566 ) {
1475 // A decoder per span, and cheap: it is a parameter-set map and a picture1567 // A decoder per span, and cheap: it is a parameter-set map and a picture
1476 // buffer, while the *demuxer* -- whose index cost seconds to build -- is the1568 // buffer, while the *demuxer* -- whose index cost seconds to build -- is the
@@ -1511,8 +1603,26 @@ fn run(
1511 }1603 }
1512 let due = index as u32;1604 let due = index as u32;
1513 index += 1;1605 index += 1;
1514- if abort.late(due) && skipped < LATE_RUN {1606+ if abort.late(due) {
1515- skipped += 1;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`).
1516 } else {1626 } else {
1517 skipped = 0;1627 skipped = 0;
1518 let frame = render.frame(1628 let frame = render.frame(
crates/engine/src/playback.rsmodified+28 -4
@@ -1561,6 +1561,7 @@ impl PlaybackSession {
1561 Some(Span {1561 Some(Span {
1562 start,1562 start,
1563 from: Some((source, in_frame)),1563 from: Some((source, in_frame)),
1564+ speed,
1564 ..1565 ..
1565 }) => {1566 }) => {
1566 // The stand-in where there is one and the switch is on, the1567 // The stand-in where there is one and the switch is on, the
@@ -1603,15 +1604,37 @@ impl PlaybackSession {
1603 // the project's own and constant across the span for that1604 // the project's own and constant across the span for that
1604 // reason too.1605 // reason too.
1605 let tone = self.project.tone();1606 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+ };
1606 // The worker already decoding this very file takes the new span1624 // The worker already decoding this very file takes the new span
1607 // itself: no thread, no container parse, no VA-API init -- the1625 // itself: no thread, no container parse, no VA-API init -- the
1608 // whole of what a seek used to cost. It answers `None` for any1626 // whole of what a seek used to cost. It answers `None` for any
1609 // other file (and for a worker whose thread has ended), and the1627 // other file (and for a worker whose thread has ended), and the
1610 // deferred opener below is then exactly the path it always was.1628 // deferred opener below is then exactly the path it always was.
1611- if let Some((frames, backend)) =1629+ if let Some((frames, backend)) = self.worker.reseek(
1612- self.worker1630+ &path,
1613- .reseek(&path, start_frame, end_frame, color, canvas(), tone)1631+ start_frame,
1614- {1632+ end_frame,
1633+ color,
1634+ canvas(),
1635+ tone,
1636+ decode_speed,
1637+ ) {
1615 self.frames = frames;1638 self.frames = frames;
1616 self.backend = backend;1639 self.backend = backend;
1617 self.span = span;1640 self.span = span;
@@ -1624,6 +1647,7 @@ impl PlaybackSession {
1624 color,1647 color,
1625 canvas(),1648 canvas(),
1626 tone,1649 tone,
1650+ decode_speed,
1627 ))1651 ))
1628 }1652 }
1629 // A gap: black for as long as it runs. An emptied timeline has no1653 // A gap: black for as long as it runs. An emptied timeline has no