🔀 merge(T16): hw/sw backend indicator before and during ops
Browse files at this commit5 changed files +702 -57
- crates/app/src/main.rs +228 -35
- crates/engine/src/decode.rs +116 -1
- crates/engine/src/export.rs +142 -20
- crates/engine/src/playback.rs +29 -1
- crates/engine/tests/backends.rs +187 -0
crates/app/src/main.rsmodified+228 -35
| @@ -11,11 +11,12 @@ use std::time::{Duration, Instant}; | ||
| 11 | 11 | |
| 12 | 12 | use engine::audio::StreamInfo; |
| 13 | 13 | use engine::color::ColorParams; |
| 14 | +use engine::decode::Backend; | |
| 14 | 15 | use engine::eq::{Band, BandKind, EqParams}; |
| 15 | 16 | use engine::export::{ExportSettings, Format}; |
| 16 | 17 | use engine::project::{Edge, Lane, LaneKind, Source, Speed}; |
| 17 | 18 | use engine::scale::FitPolicy; |
| 18 | -use engine::{Clip, ExportHandle, Frame, PlaybackSession}; | |
| 19 | +use engine::{Clip, Codec, ExportHandle, Frame, PlaybackSession}; | |
| 19 | 20 | use gpui::{ |
| 20 | 21 | AnyElement, App, Application, Bounds, ClickEvent, Context, CursorStyle, Div, FocusHandle, |
| 21 | 22 | KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, |
| @@ -637,6 +638,19 @@ struct Player { | ||
| 637 | 638 | /// `None` is a file with no picture to report (every source that is not an |
| 638 | 639 | /// image, and one whose header would not read). |
| 639 | 640 | sizes: HashMap<PathBuf, Option<(u32, u32)>>, |
| 641 | + /// Which decoder each source will run on, probed once at import and kept: | |
| 642 | + /// the codec (`None` for a still) and the seat the engine picked for it. | |
| 643 | + /// What a library row says *before* anything plays; the running answer is | |
| 644 | + /// the session's own (`PlaybackSession::decode_backend`), which follows a | |
| 645 | + /// fallback this cannot. Filled like `sizes`: presence means "asked", and | |
| 646 | + /// `None` is a source with no decoder to name -- a song, or one the probe | |
| 647 | + /// refused -- which must stay in the map or every repaint would ask again. | |
| 648 | + decoders: HashMap<PathBuf, Option<(Option<Codec>, Backend)>>, | |
| 649 | + /// Which encoder an export of the picked settings would open, and what it | |
| 650 | + /// was asked about: the probe opens a real VA-API encoder (~100 ms), so it | |
| 651 | + /// runs off the render thread and only while the export card is up. The | |
| 652 | + /// inner `None` is "asked, not answered yet". | |
| 653 | + export_seat: Option<(ExportSettings, (u32, u32), Option<&'static str>)>, | |
| 640 | 654 | /// The copied clip. Frame ranges only, so it survives the clip it was taken |
| 641 | 655 | /// from being deleted -- and it outlives the selection. |
| 642 | 656 | clipboard: Option<Clip>, |
| @@ -1997,6 +2011,28 @@ impl Player { | ||
| 1997 | 2011 | }) |
| 1998 | 2012 | .detach(); |
| 1999 | 2013 | } |
| 2014 | + // Which decoder each file will run on, for the row that says so before | |
| 2015 | + // a frame of it plays. Off the render thread like the streams above: a | |
| 2016 | + // stream the plugin takes costs one VA-API init (~90 ms) to answer. | |
| 2017 | + for path in unseen_paths(session.sources(), &self.decoders) { | |
| 2018 | + self.decoders.insert(path.clone(), None); | |
| 2019 | + let probed = cx.background_executor().spawn({ | |
| 2020 | + let path = path.clone(); | |
| 2021 | + // A song and a source no decoder here takes are both `None`: | |
| 2022 | + // the row says nothing about them rather than guessing, and | |
| 2023 | + // import refused the second at the door anyway. | |
| 2024 | + async move { engine::decode::probe(&path).ok() } | |
| 2025 | + }); | |
| 2026 | + cx.spawn(async move |this, cx| { | |
| 2027 | + let probed = probed.await; | |
| 2028 | + this.update(cx, |this, cx| { | |
| 2029 | + this.decoders.insert(path, probed); | |
| 2030 | + cx.notify(); | |
| 2031 | + }) | |
| 2032 | + .ok(); | |
| 2033 | + }) | |
| 2034 | + .detach(); | |
| 2035 | + } | |
| 2000 | 2036 | for key in unseen_sources(session.sources(), &self.waves) { |
| 2001 | 2037 | self.waves.insert(key.clone(), Wave::Loading); |
| 2002 | 2038 | let decoded = cx.background_executor().spawn({ |
| @@ -2029,6 +2065,51 @@ impl Player { | ||
| 2029 | 2065 | } |
| 2030 | 2066 | } |
| 2031 | 2067 | |
| 2068 | + /// Probes the encoder an export would open, once per (settings, | |
| 2069 | + /// resolution) and only while the export card is up -- it opens the very | |
| 2070 | + /// VA-API encoder the export would, which is what makes the card's line a | |
| 2071 | + /// measurement instead of a promise, and also what makes it too slow for | |
| 2072 | + /// the render thread. Written before the spawn, like the probes above, so | |
| 2073 | + /// the repaints during it start no second one. | |
| 2074 | + fn cache_export_seat(&mut self, cx: &mut Context<Self>) { | |
| 2075 | + let Some(session) = &self.session else { | |
| 2076 | + return; | |
| 2077 | + }; | |
| 2078 | + let settings = export_settings(self.quality, self.custom_mbps, self.format); | |
| 2079 | + if !self.export_open || !settings.format.has_video() { | |
| 2080 | + return; | |
| 2081 | + } | |
| 2082 | + let meta = *session.meta(); | |
| 2083 | + let key = (settings, (meta.width, meta.height)); | |
| 2084 | + if self | |
| 2085 | + .export_seat | |
| 2086 | + .is_some_and(|(asked, size, _)| (asked, size) == key) | |
| 2087 | + { | |
| 2088 | + return; | |
| 2089 | + } | |
| 2090 | + self.export_seat = Some((key.0, key.1, None)); | |
| 2091 | + let probed = cx | |
| 2092 | + .background_executor() | |
| 2093 | + .spawn(async move { engine::export::planned_video(&meta, &settings) }); | |
| 2094 | + cx.spawn(async move |this, cx| { | |
| 2095 | + let probed = probed.await; | |
| 2096 | + this.update(cx, |this, cx| { | |
| 2097 | + // Only if the card is still asking the same question: a format | |
| 2098 | + // changed while the plugin opened has a probe of its own. | |
| 2099 | + if let Some(seat) = this | |
| 2100 | + .export_seat | |
| 2101 | + .as_mut() | |
| 2102 | + .filter(|(asked, size, _)| (*asked, *size) == key) | |
| 2103 | + { | |
| 2104 | + seat.2 = probed; | |
| 2105 | + } | |
| 2106 | + cx.notify(); | |
| 2107 | + }) | |
| 2108 | + .ok(); | |
| 2109 | + }) | |
| 2110 | + .detach(); | |
| 2111 | + } | |
| 2112 | + | |
| 2032 | 2113 | /// The group id of the clicked clip, which is what marks the other half. |
| 2033 | 2114 | fn selected_link(&self) -> Option<u32> { |
| 2034 | 2115 | let (lane, idx) = self.selected?; |
| @@ -3055,6 +3136,7 @@ impl Render for Player { | ||
| 3055 | 3136 | // has been through a repaint by the time its clips are drawn, so this |
| 3056 | 3137 | // is the one place that has to notice a new one. |
| 3057 | 3138 | self.cache_media(cx); |
| 3139 | + self.cache_export_seat(cx); | |
| 3058 | 3140 | // What the compositor calls this window. Pushed only when it changes: |
| 3059 | 3141 | // it is a protocol round trip and this runs at vsync. |
| 3060 | 3142 | let title = window_title(&self.name); |
| @@ -3545,11 +3627,17 @@ impl Player { | ||
| 3545 | 3627 | // Its own length, not one derived from what is on the lanes: a row |
| 3546 | 3628 | // imported and never placed is a row with a length, and it is the |
| 3547 | 3629 | // length a drag would put down. |
| 3548 | - let rows: Vec<_> = library_rows(sources, &self.streams, self.timeline_audio(), |path| { | |
| 3549 | - self.session | |
| 3550 | - .as_ref() | |
| 3551 | - .map_or(0, |session| session.file_frames(path)) | |
| 3552 | - }) | |
| 3630 | + let rows: Vec<_> = library_rows( | |
| 3631 | + sources, | |
| 3632 | + &self.streams, | |
| 3633 | + &self.decoders, | |
| 3634 | + self.timeline_audio(), | |
| 3635 | + |path| { | |
| 3636 | + self.session | |
| 3637 | + .as_ref() | |
| 3638 | + .map_or(0, |session| session.file_frames(path)) | |
| 3639 | + }, | |
| 3640 | + ) | |
| 3553 | 3641 | .into_iter() |
| 3554 | 3642 | .enumerate() |
| 3555 | 3643 | .map(|(i, row)| { |
| @@ -3786,6 +3874,30 @@ impl Player { | ||
| 3786 | 3874 | )) |
| 3787 | 3875 | } |
| 3788 | 3876 | |
| 3877 | + /// What is decoding the picture right now, for the transport line: the | |
| 3878 | + /// backend is the running worker's own (it is written where a hardware | |
| 3879 | + /// session falls back to software, so this follows reality), and the codec | |
| 3880 | + /// comes from the clip under the playhead. Empty when nothing is playing -- | |
| 3881 | + /// the question is about what is happening, not about what would. | |
| 3882 | + fn live_decode(&self, position: f64, playing: bool) -> String { | |
| 3883 | + let Some(session) = self.session.as_ref().filter(|_| playing) else { | |
| 3884 | + return String::new(); | |
| 3885 | + }; | |
| 3886 | + let backend = session.decode_backend(); | |
| 3887 | + let codec = session | |
| 3888 | + .video_clip_at(position) | |
| 3889 | + .and_then(|(lane, idx)| session.lane_clips(lane).get(idx).map(|clip| clip.source)) | |
| 3890 | + .and_then(|source| session.sources().get(source)) | |
| 3891 | + .and_then(|source| self.decoders.get(&source.path).copied().flatten()) | |
| 3892 | + .and_then(|(codec, _)| codec); | |
| 3893 | + match backend { | |
| 3894 | + // Neither is a decode, and saying "SW" of them would be a lie. | |
| 3895 | + Backend::Gap => "gap · nothing to decode".to_string(), | |
| 3896 | + Backend::Still => "still · one decode, held".to_string(), | |
| 3897 | + _ => format!("{} decode", decode_label(codec, backend)), | |
| 3898 | + } | |
| 3899 | + } | |
| 3900 | + | |
| 3789 | 3901 | /// Transport, edit and file buttons, timecode, playhead, clips lane. |
| 3790 | 3902 | fn panel( |
| 3791 | 3903 | &self, |
| @@ -3827,8 +3939,16 @@ impl Player { | ||
| 3827 | 3939 | let progress = export.progress(); |
| 3828 | 3940 | ( |
| 3829 | 3941 | format!( |
| 3830 | - "EXPORTING {}% — {} cancels", | |
| 3942 | + "EXPORTING {}% · {} · {} — {} cancels", | |
| 3831 | 3943 | (progress * 100.) as u32, |
| 3944 | + // The row that was picked; the engine's line below names the | |
| 3945 | + // seats alone, since the library is what identifies a codec. | |
| 3946 | + format_label(self.format), | |
| 3947 | + // What the worker actually opened, so a fallback to the | |
| 3948 | + // software encoder shows here rather than being invisible. | |
| 3949 | + export | |
| 3950 | + .encoders() | |
| 3951 | + .unwrap_or_else(|| "opening the encoder".to_string()), | |
| 3832 | 3952 | key(ActionId::CancelExport) |
| 3833 | 3953 | ), |
| 3834 | 3954 | progress, |
| @@ -3838,12 +3958,19 @@ impl Player { | ||
| 3838 | 3958 | // tooltips. Keys first: at a 640 px window the tail is what a |
| 3839 | 3959 | // truncation eats, and the two hints at the end are also on the |
| 3840 | 3960 | // ruler's and Import's tooltips. |
| 3961 | + // While it plays, what is decoding it goes first: it is the | |
| 3962 | + // answer that changes as the playhead crosses a cut, and the tail | |
| 3963 | + // of this line is what a narrow window truncates. | |
| 3841 | 3964 | ( |
| 3842 | - format!( | |
| 3843 | - "{} copy · {} paste · {} undo · click the bar to seek · drop a file to import", | |
| 3844 | - key(ActionId::Copy), | |
| 3845 | - key(ActionId::Paste), | |
| 3846 | - key(ActionId::Undo) | |
| 3965 | + join_detail( | |
| 3966 | + &self.live_decode(position, playing), | |
| 3967 | + &format!( | |
| 3968 | + "{} copy · {} paste · {} undo · click the bar to seek · drop a file to \ | |
| 3969 | + import", | |
| 3970 | + key(ActionId::Copy), | |
| 3971 | + key(ActionId::Paste), | |
| 3972 | + key(ActionId::Undo) | |
| 3973 | + ), | |
| 3847 | 3974 | ), |
| 3848 | 3975 | filled, |
| 3849 | 3976 | ) |
| @@ -4614,7 +4741,11 @@ impl Player { | ||
| 4614 | 4741 | .px(px(6.)) |
| 4615 | 4742 | .text_size(px(11.)) |
| 4616 | 4743 | .text_color(rgb(INK_DIM)) |
| 4617 | - .child(export_line(self.format, self.session.as_ref())), | |
| 4744 | + .child(export_line( | |
| 4745 | + self.format, | |
| 4746 | + self.session.as_ref(), | |
| 4747 | + self.export_seat.and_then(|(.., seat)| seat), | |
| 4748 | + )), | |
| 4618 | 4749 | ) |
| 4619 | 4750 | .child( |
| 4620 | 4751 | div() |
| @@ -6463,6 +6594,7 @@ struct Row { | ||
| 6463 | 6594 | fn library_rows( |
| 6464 | 6595 | sources: &[Source], |
| 6465 | 6596 | streams: &HashMap<PathBuf, Vec<StreamInfo>>, |
| 6597 | + decoders: &HashMap<PathBuf, Option<(Option<Codec>, Backend)>>, | |
| 6466 | 6598 | timeline_audio: Option<(u32, u16)>, |
| 6467 | 6599 | frames: impl Fn(&Path) -> u32, |
| 6468 | 6600 | ) -> Vec<Row> { |
| @@ -6478,12 +6610,22 @@ fn library_rows( | ||
| 6478 | 6610 | path: source.path.clone(), |
| 6479 | 6611 | stream: source.audio_stream, |
| 6480 | 6612 | name: row_name(&source.path, source.audio_stream, of_file.len() > 1), |
| 6481 | - // Only where there is a choice to describe: a file with one audio | |
| 6482 | - // track is the row it has always been, name and length, and the | |
| 6483 | - // length is what would be squeezed out at the panel's least width. | |
| 6484 | - detail: info | |
| 6485 | - .filter(|_| of_file.len() > 1) | |
| 6486 | - .map_or_else(String::new, stream_detail), | |
| 6613 | + // The decoder first: it is the same for every row of a file and | |
| 6614 | + // it is what a person opening the panel is asking about. The | |
| 6615 | + // stream half only where there is a choice to describe -- a file | |
| 6616 | + // with one audio track is the row it has always been, name and | |
| 6617 | + // length, and the length is what would be squeezed out at the | |
| 6618 | + // panel's least width. | |
| 6619 | + detail: join_detail( | |
| 6620 | + &decoders | |
| 6621 | + .get(&source.path) | |
| 6622 | + .copied() | |
| 6623 | + .flatten() | |
| 6624 | + .map_or_else(String::new, |(codec, backend)| decode_label(codec, backend)), | |
| 6625 | + &info | |
| 6626 | + .filter(|_| of_file.len() > 1) | |
| 6627 | + .map_or_else(String::new, stream_detail), | |
| 6628 | + ), | |
| 6487 | 6629 | // A stream already on the timeline is playing: whatever a probe |
| 6488 | 6630 | // would say about it now, it is usable by demonstration. |
| 6489 | 6631 | unusable: None, |
| @@ -6520,9 +6662,20 @@ fn library_rows( | ||
| 6520 | 6662 | /// why it cannot be used, with the separator only where both halves exist (a |
| 6521 | 6663 | /// single-stream file says nothing about its stream). |
| 6522 | 6664 | fn join_detail(detail: &str, tail: &str) -> String { |
| 6523 | - match detail.is_empty() { | |
| 6524 | - true => tail.to_string(), | |
| 6525 | - false => format!("{detail} · {tail}"), | |
| 6665 | + match (detail.is_empty(), tail.is_empty()) { | |
| 6666 | + (true, _) => tail.to_string(), | |
| 6667 | + (false, true) => detail.to_string(), | |
| 6668 | + (false, false) => format!("{detail} · {tail}"), | |
| 6669 | + } | |
| 6670 | +} | |
| 6671 | + | |
| 6672 | +/// How a source's decoder reads: the codec and which seat has it, or the seat | |
| 6673 | +/// alone for a still, which has no coded stream to name. The one place either | |
| 6674 | +/// answer is spelled, so a row, a transport line and a card cannot disagree. | |
| 6675 | +fn decode_label(codec: Option<Codec>, backend: Backend) -> String { | |
| 6676 | + match codec { | |
| 6677 | + Some(codec) => format!("{} · {}", codec.name(), backend.label()), | |
| 6678 | + None => backend.label().to_string(), | |
| 6526 | 6679 | } |
| 6527 | 6680 | } |
| 6528 | 6681 | |
| @@ -6840,14 +6993,32 @@ fn next_resolution(current: (u32, u32), native: (u32, u32)) -> (u32, u32) { | ||
| 6840 | 6993 | /// [`format_line`] plus the project's resolution, which is what a video export |
| 6841 | 6994 | /// is written at however many sizes the media on the timeline are. Only for the |
| 6842 | 6995 | /// formats that carry a picture -- a WAV has no resolution to state. |
| 6843 | -fn export_line(format: Format, session: Option<&PlaybackSession>) -> String { | |
| 6996 | +fn export_line( | |
| 6997 | + format: Format, | |
| 6998 | + session: Option<&PlaybackSession>, | |
| 6999 | + seat: Option<&'static str>, | |
| 7000 | +) -> String { | |
| 6844 | 7001 | let line = format_line(format); |
| 7002 | + // What it will really be encoded by, on the same line: the picture's seat | |
| 7003 | + // as the probe found it (`…` until it lands, never a guess) and the sound's, | |
| 7004 | + // which is decided without opening anything. Both are what a running export | |
| 7005 | + // then names on its progress line, from the same two functions. | |
| 7006 | + let encoders = match session { | |
| 7007 | + Some(session) => join_detail( | |
| 7008 | + seat.unwrap_or(match format.has_video() { | |
| 7009 | + true => "encoder …", | |
| 7010 | + false => "", | |
| 7011 | + }), | |
| 7012 | + session.planned_audio(format), | |
| 7013 | + ), | |
| 7014 | + None => String::new(), | |
| 7015 | + }; | |
| 6845 | 7016 | match session.filter(|_| format.has_video()) { |
| 6846 | 7017 | Some(session) => { |
| 6847 | 7018 | let (w, h) = session.resolution(); |
| 6848 | - format!("{line} · project {w}x{h}") | |
| 7019 | + format!("{line} · project {w}x{h} · {encoders}") | |
| 6849 | 7020 | } |
| 6850 | - None => line.to_string(), | |
| 7021 | + None => join_detail(line, &encoders), | |
| 6851 | 7022 | } |
| 6852 | 7023 | } |
| 6853 | 7024 | |
| @@ -7927,9 +8098,13 @@ mod tests { | ||
| 7927 | 8098 | ); |
| 7928 | 8099 | // The rows the panel draws: one per source, each its file's own length |
| 7929 | 8100 | // -- source 1 has no clip anywhere and is still 4 s at 30 fps. |
| 7930 | - let rows = library_rows(session.sources(), &HashMap::new(), None, |path| { | |
| 7931 | - session.file_frames(path) | |
| 7932 | - }); | |
| 8101 | + let rows = library_rows( | |
| 8102 | + session.sources(), | |
| 8103 | + &HashMap::new(), | |
| 8104 | + &HashMap::new(), | |
| 8105 | + None, | |
| 8106 | + |path| session.file_frames(path), | |
| 8107 | + ); | |
| 7933 | 8108 | assert_eq!(rows.len(), 2); |
| 7934 | 8109 | assert_eq!(rows[0].frames, 150, "5 s at 30 fps"); |
| 7935 | 8110 | assert_eq!(rows[1].frames, 120, "4 s at 30 fps, never placed"); |
| @@ -7968,7 +8143,9 @@ mod tests { | ||
| 7968 | 8143 | .find(|s| s.index == session.sources()[0].audio_stream) |
| 7969 | 8144 | .map(|s| (s.sample_rate, s.channels)); |
| 7970 | 8145 | let frames = session.file_frames(&path); |
| 7971 | - let rows = library_rows(session.sources(), &streams, rate, |_| frames); | |
| 8146 | + let rows = library_rows(session.sources(), &streams, &HashMap::new(), rate, |_| { | |
| 8147 | + frames | |
| 8148 | + }); | |
| 7972 | 8149 | assert_eq!(rows.len(), 1); |
| 7973 | 8150 | assert_eq!(rows[0].name, "test_tone.mp3"); |
| 7974 | 8151 | assert_eq!(rows[0].unusable, None, "the row is placeable"); |
| @@ -8035,7 +8212,13 @@ mod tests { | ||
| 8035 | 8212 | info(3, 0, 0, None, false), |
| 8036 | 8213 | ], |
| 8037 | 8214 | ); |
| 8038 | - let rows = library_rows(&sources, &streams, Some((44_100, 2)), |_| 90); | |
| 8215 | + let rows = library_rows( | |
| 8216 | + &sources, | |
| 8217 | + &streams, | |
| 8218 | + &HashMap::new(), | |
| 8219 | + Some((44_100, 2)), | |
| 8220 | + |_| 90, | |
| 8221 | + ); | |
| 8039 | 8222 | assert_eq!( |
| 8040 | 8223 | rows.iter().map(|r| r.stream).collect::<Vec<_>>(), |
| 8041 | 8224 | [0, 1, 2, 3], |
| @@ -8073,7 +8256,7 @@ mod tests { | ||
| 8073 | 8256 | PathBuf::from("/m/plain.mp4"), |
| 8074 | 8257 | vec![info(0, 44_100, 2, None, true)], |
| 8075 | 8258 | ); |
| 8076 | - let rows = library_rows(&plain, &one, Some((44_100, 2)), |_| 90); | |
| 8259 | + let rows = library_rows(&plain, &one, &HashMap::new(), Some((44_100, 2)), |_| 90); | |
| 8077 | 8260 | assert_eq!(rows.len(), 1); |
| 8078 | 8261 | assert_eq!(rows[0].name, "plain.mp4"); |
| 8079 | 8262 | assert_eq!( |
| @@ -8084,7 +8267,7 @@ mod tests { | ||
| 8084 | 8267 | let mut silent = HashMap::new(); |
| 8085 | 8268 | silent.insert(PathBuf::from("/m/plain.mp4"), Vec::new()); |
| 8086 | 8269 | for probe in [silent, HashMap::new()] { |
| 8087 | - let rows = library_rows(&plain, &probe, None, |_| 90); | |
| 8270 | + let rows = library_rows(&plain, &probe, &HashMap::new(), None, |_| 90); | |
| 8088 | 8271 | assert_eq!(rows.len(), 1); |
| 8089 | 8272 | assert_eq!(rows[0].name, "plain.mp4"); |
| 8090 | 8273 | assert_eq!(rows[0].detail, ""); |
| @@ -8102,7 +8285,13 @@ mod tests { | ||
| 8102 | 8285 | PathBuf::from("/m/other.mp4"), |
| 8103 | 8286 | vec![info(0, 44_100, 2, None, true)], |
| 8104 | 8287 | ); |
| 8105 | - let rows = library_rows(&placed, &streams, Some((44_100, 2)), |_| 90); | |
| 8288 | + let rows = library_rows( | |
| 8289 | + &placed, | |
| 8290 | + &streams, | |
| 8291 | + &HashMap::new(), | |
| 8292 | + Some((44_100, 2)), | |
| 8293 | + |_| 90, | |
| 8294 | + ); | |
| 8106 | 8295 | assert_eq!( |
| 8107 | 8296 | rows.iter() |
| 8108 | 8297 | .map(|r| (file_name(&r.path), r.stream)) |
| @@ -8505,7 +8694,7 @@ mod tests { | ||
| 8505 | 8694 | ); |
| 8506 | 8695 | let streams = HashMap::new(); |
| 8507 | 8696 | let rows = |session: &PlaybackSession| { |
| 8508 | - library_rows(session.sources(), &streams, None, |_| 0).len() | |
| 8697 | + library_rows(session.sources(), &streams, &HashMap::new(), None, |_| 0).len() | |
| 8509 | 8698 | }; |
| 8510 | 8699 | assert_eq!(rows(&session), 2, "a row per source"); |
| 8511 | 8700 | |
| @@ -9826,7 +10015,9 @@ mod tests { | ||
| 9826 | 10015 | // a title and a status line. |
| 9827 | 10016 | let title = 17.; |
| 9828 | 10017 | let status = 28.; |
| 9829 | - let fixed = 17.; | |
| 10018 | + // Two lines of 11 px: the format line carries the encoders an export | |
| 10019 | + // would open, which is what takes it past one at this width. | |
| 10020 | + let fixed = 30.; | |
| 9830 | 10021 | let gaps = 4. * 2.; |
| 9831 | 10022 | let padding = 24.; |
| 9832 | 10023 | assert!( |
| @@ -10371,6 +10562,8 @@ fn main() { | ||
| 10371 | 10562 | waves: HashMap::new(), |
| 10372 | 10563 | streams: HashMap::new(), |
| 10373 | 10564 | sizes: HashMap::new(), |
| 10565 | + decoders: HashMap::new(), | |
| 10566 | + export_seat: None, | |
| 10374 | 10567 | clipboard: None, |
| 10375 | 10568 | scrubbing: false, |
| 10376 | 10569 | trim: None, |
crates/engine/src/decode.rsmodified+116 -1
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | |
| 4 | 4 | use std::path::{Path, PathBuf}; |
| 5 | 5 | use std::sync::Arc; |
| 6 | -use std::sync::atomic::{AtomicBool, Ordering}; | |
| 6 | +use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; | |
| 7 | 7 | use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; |
| 8 | 8 | use std::thread; |
| 9 | 9 | |
| @@ -25,6 +25,106 @@ pub struct Frame { | ||
| 25 | 25 | pub bgra: Vec<u8>, |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | +/// Which decoder a source is really running on. Read-only introspection: it | |
| 29 | +/// decides nothing, it *reports* what [`DecodeSession::open_worker`] chose -- | |
| 30 | +/// a hardware session that opened and then failed before its first picture | |
| 31 | +/// falls back to software and this says so, because it is written where the | |
| 32 | +/// fallback happens rather than where it was hoped for. | |
| 33 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | |
| 34 | +pub enum Backend { | |
| 35 | + /// The worker has not reached its decoder yet: the plugin's VA-API init is | |
| 36 | + /// ~90 ms, and this is what a caller reads in that window. | |
| 37 | + #[default] | |
| 38 | + Opening, | |
| 39 | + /// The VA-API plugin (`libengine_hw.so`). | |
| 40 | + Hardware, | |
| 41 | + /// `rusty_h264`, in this process. | |
| 42 | + Software, | |
| 43 | + /// A still image: one `image` decode, no stream and no decoder to pick. | |
| 44 | + Still, | |
| 45 | + /// A gap: black frames, nothing decoded at all. | |
| 46 | + Gap, | |
| 47 | +} | |
| 48 | + | |
| 49 | +impl Backend { | |
| 50 | + /// Short enough for a library row and a transport line. | |
| 51 | + pub fn label(self) -> &'static str { | |
| 52 | + match self { | |
| 53 | + Self::Opening => "opening", | |
| 54 | + Self::Hardware => "HW", | |
| 55 | + Self::Software => "SW", | |
| 56 | + Self::Still => "still", | |
| 57 | + Self::Gap => "gap", | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + fn code(self) -> u8 { | |
| 62 | + match self { | |
| 63 | + Self::Opening => 0, | |
| 64 | + Self::Hardware => 1, | |
| 65 | + Self::Software => 2, | |
| 66 | + Self::Still => 3, | |
| 67 | + Self::Gap => 4, | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + fn from_code(code: u8) -> Self { | |
| 72 | + match code { | |
| 73 | + 1 => Self::Hardware, | |
| 74 | + 2 => Self::Software, | |
| 75 | + 3 => Self::Still, | |
| 76 | + 4 => Self::Gap, | |
| 77 | + _ => Self::Opening, | |
| 78 | + } | |
| 79 | + } | |
| 80 | +} | |
| 81 | + | |
| 82 | +/// Where a worker publishes the decoder it opened, for whoever holds the other | |
| 83 | +/// end of its channel. One atomic byte, written once at open (and once more if | |
| 84 | +/// hardware hands the range back to software), read whenever a caller repaints | |
| 85 | +/// -- never per frame on either side. | |
| 86 | +#[derive(Clone, Default)] | |
| 87 | +pub struct BackendCell(Arc<AtomicU8>); | |
| 88 | + | |
| 89 | +impl BackendCell { | |
| 90 | + fn new(backend: Backend) -> Self { | |
| 91 | + Self(Arc::new(AtomicU8::new(backend.code()))) | |
| 92 | + } | |
| 93 | + | |
| 94 | + fn set(&self, backend: Backend) { | |
| 95 | + self.0.store(backend.code(), Ordering::Relaxed); | |
| 96 | + } | |
| 97 | + | |
| 98 | + pub fn get(&self) -> Backend { | |
| 99 | + Backend::from_code(self.0.load(Ordering::Relaxed)) | |
| 100 | + } | |
| 101 | +} | |
| 102 | + | |
| 103 | +/// Which decoder `path` would open, decided exactly as [`DecodeSession::open_worker`] | |
| 104 | +/// decides it: the same `VE_SW` pin and the same plugin probe against this very | |
| 105 | +/// file. What lets a front-end name the decoder *before* anything plays. | |
| 106 | +/// | |
| 107 | +/// The codec is `None` for a still image, which has no coded stream. A stream | |
| 108 | +/// no decoder here can take is an `Err` naming the plugin, exactly as opening | |
| 109 | +/// it would be -- the same refusal, one VA-API init earlier. | |
| 110 | +/// | |
| 111 | +/// Costs that one init (~90 ms) for a stream the plugin takes: ask it once per | |
| 112 | +/// file, off a render thread, and keep the answer. | |
| 113 | +pub fn probe(path: &Path) -> crate::Result<(Option<Codec>, Backend)> { | |
| 114 | + if crate::is_image(path) { | |
| 115 | + return Ok((None, Backend::Still)); | |
| 116 | + } | |
| 117 | + let path = path.to_path_buf(); | |
| 118 | + let (meta, _demuxer) = Demuxer::open(&path)?; | |
| 119 | + if open_hw(&path, 0).is_some() { | |
| 120 | + return Ok((Some(meta.codec), Backend::Hardware)); | |
| 121 | + } | |
| 122 | + if meta.codec != Codec::H264 { | |
| 123 | + return Err(meta.codec.needs_plugin().into()); | |
| 124 | + } | |
| 125 | + Ok((Some(meta.codec), Backend::Software)) | |
| 126 | +} | |
| 127 | + | |
| 28 | 128 | /// A running decode worker: the flag that stops it, and the handle to wait on. |
| 29 | 129 | /// |
| 30 | 130 | /// Dropping one cancels *and joins*. A worker abandoned mid-`vaInitialize` |
| @@ -85,6 +185,9 @@ impl Drop for Worker { | ||
| 85 | 185 | pub(crate) struct FrameStream { |
| 86 | 186 | pub(crate) frames: Receiver<Frame>, |
| 87 | 187 | pub(crate) worker: Worker, |
| 188 | + /// What this stream's pictures are being decoded by. Dropped last and read | |
| 189 | + /// by the caller, so it outlives nothing and blocks nothing. | |
| 190 | + pub(crate) backend: BackendCell, | |
| 88 | 191 | } |
| 89 | 192 | |
| 90 | 193 | pub struct DecodeSession; |
| @@ -152,6 +255,7 @@ impl DecodeSession { | ||
| 152 | 255 | cancel, |
| 153 | 256 | handle: None, |
| 154 | 257 | }, |
| 258 | + backend: BackendCell::new(Backend::Gap), | |
| 155 | 259 | }; |
| 156 | 260 | } |
| 157 | 261 | let worker_cancel = Arc::clone(&cancel); |
| @@ -180,6 +284,7 @@ impl DecodeSession { | ||
| 180 | 284 | FrameStream { |
| 181 | 285 | frames: rx, |
| 182 | 286 | worker: Worker { cancel, handle }, |
| 287 | + backend: BackendCell::new(Backend::Gap), | |
| 183 | 288 | } |
| 184 | 289 | } |
| 185 | 290 | |
| @@ -209,6 +314,7 @@ impl DecodeSession { | ||
| 209 | 314 | cancel, |
| 210 | 315 | handle: None, |
| 211 | 316 | }, |
| 317 | + backend: BackendCell::new(Backend::Still), | |
| 212 | 318 | }); |
| 213 | 319 | } |
| 214 | 320 | // The one conversion, on this thread: it is a few milliseconds and the |
| @@ -245,6 +351,7 @@ impl DecodeSession { | ||
| 245 | 351 | Ok(FrameStream { |
| 246 | 352 | frames: rx, |
| 247 | 353 | worker: Worker { cancel, handle }, |
| 354 | + backend: BackendCell::new(Backend::Still), | |
| 248 | 355 | }) |
| 249 | 356 | } |
| 250 | 357 | |
| @@ -285,6 +392,9 @@ impl DecodeSession { | ||
| 285 | 392 | // decoder run ahead of the display without limit. |
| 286 | 393 | let (tx, rx) = sync_channel(2); |
| 287 | 394 | let cancel = Arc::new(AtomicBool::new(false)); |
| 395 | + // Written by the worker below the moment it knows, so a caller reading | |
| 396 | + // it sees what opened rather than what was hoped for. | |
| 397 | + let backend = BackendCell::default(); | |
| 288 | 398 | if end_frame <= start_frame { |
| 289 | 399 | // Nothing to decode: dropping `tx` here closes the channel cleanly, |
| 290 | 400 | // so the caller sees an immediate end of stream rather than an error. |
| @@ -296,10 +406,12 @@ impl DecodeSession { | ||
| 296 | 406 | cancel, |
| 297 | 407 | handle: None, |
| 298 | 408 | }, |
| 409 | + backend, | |
| 299 | 410 | }, |
| 300 | 411 | )); |
| 301 | 412 | } |
| 302 | 413 | let worker_cancel = Arc::clone(&cancel); |
| 414 | + let worker_backend = backend.clone(); | |
| 303 | 415 | let handle = thread::Builder::new() |
| 304 | 416 | .name("decode".into()) |
| 305 | 417 | .spawn(move || { |
| @@ -320,6 +432,7 @@ impl DecodeSession { | ||
| 320 | 432 | return; |
| 321 | 433 | } |
| 322 | 434 | eprintln!("decode backend: hardware (VA-API plugin)"); |
| 435 | + worker_backend.set(Backend::Hardware); | |
| 323 | 436 | if run_hw(hw, &tx, start_frame, end_frame, &mut render, &worker_cancel) { |
| 324 | 437 | return; |
| 325 | 438 | } |
| @@ -334,6 +447,7 @@ impl DecodeSession { | ||
| 334 | 447 | return; |
| 335 | 448 | } |
| 336 | 449 | eprintln!("decode backend: software (rusty_h264)"); |
| 450 | + worker_backend.set(Backend::Software); | |
| 337 | 451 | run( |
| 338 | 452 | demuxer, |
| 339 | 453 | tx, |
| @@ -351,6 +465,7 @@ impl DecodeSession { | ||
| 351 | 465 | cancel, |
| 352 | 466 | handle: Some(handle), |
| 353 | 467 | }, |
| 468 | + backend, | |
| 354 | 469 | }, |
| 355 | 470 | )) |
| 356 | 471 | } |
crates/engine/src/export.rsmodified+142 -20
| @@ -146,6 +146,10 @@ struct Shared { | ||
| 146 | 146 | cancel: AtomicBool, |
| 147 | 147 | finished: AtomicBool, |
| 148 | 148 | outcome: Mutex<Option<crate::Result<()>>>, |
| 149 | + /// The encoders this job really opened, published the moment they are | |
| 150 | + /// chosen and never changed after -- one seat for the whole file, which is | |
| 151 | + /// what [`Enc`] states. `None` until then. | |
| 152 | + encoders: Mutex<Option<String>>, | |
| 149 | 153 | } |
| 150 | 154 | |
| 151 | 155 | /// A running export. Poll [`is_finished`](ExportHandle::is_finished) once per |
| @@ -174,6 +178,13 @@ impl ExportHandle { | ||
| 174 | 178 | self.shared.finished.load(Ordering::Acquire) |
| 175 | 179 | } |
| 176 | 180 | |
| 181 | + /// What this export is encoding with -- the video seat and what the sound | |
| 182 | + /// is doing -- as the worker chose them, hardware fallback included. | |
| 183 | + /// `None` for the first instants of a job, before the encoder is open. | |
| 184 | + pub fn encoders(&self) -> Option<String> { | |
| 185 | + self.shared.encoders.lock().unwrap().clone() | |
| 186 | + } | |
| 187 | + | |
| 177 | 188 | /// The outcome, once — taken out of the handle, so a caller that already |
| 178 | 189 | /// reported it sees `None` afterwards. `None` while the export is running. |
| 179 | 190 | pub fn result(&self) -> Option<crate::Result<()>> { |
| @@ -197,6 +208,7 @@ pub fn start( | ||
| 197 | 208 | cancel: AtomicBool::new(false), |
| 198 | 209 | finished: AtomicBool::new(false), |
| 199 | 210 | outcome: Mutex::new(None), |
| 211 | + encoders: Mutex::new(None), | |
| 200 | 212 | }); |
| 201 | 213 | let worker = Arc::clone(&shared); |
| 202 | 214 | let out = out.to_path_buf(); |
| @@ -261,6 +273,103 @@ fn has_picture(project: &Project) -> bool { | ||
| 261 | 273 | .any(|lane| lane.kind == LaneKind::Video && !project.lane(lane).is_empty()) |
| 262 | 274 | } |
| 263 | 275 | |
| 276 | +/// Whether any audio lane holds anything, [`has_picture`]'s pair: what tells a | |
| 277 | +/// timeline whose export will carry sound from one whose file has none. | |
| 278 | +fn has_sound(project: &Project) -> bool { | |
| 279 | + project | |
| 280 | + .lanes() | |
| 281 | + .into_iter() | |
| 282 | + .any(|lane| lane.kind == LaneKind::Audio && !project.lane(lane).is_empty()) | |
| 283 | +} | |
| 284 | + | |
| 285 | +/// Whether any equalizer on the sound is doing something -- the one edit a | |
| 286 | +/// packet copy cannot carry, so an mp4 whose lane has one is decoded and | |
| 287 | +/// re-encoded ([`encode_audio`]) instead of copied. Asked of every audio lane: | |
| 288 | +/// the copy path has exactly one by the time it asks, and [`planned_audio`] is | |
| 289 | +/// asked before that is settled. | |
| 290 | +fn equalized(project: &Project) -> bool { | |
| 291 | + project.audio_lanes().into_iter().any(|lane| { | |
| 292 | + (0..project.lane(lane).len()) | |
| 293 | + .any(|idx| project.eq_of(lane, idx).is_some_and(|eq| !eq.is_identity())) | |
| 294 | + }) | |
| 295 | +} | |
| 296 | + | |
| 297 | +/// The bitrate an export codes at: the caller's number through the same clamp | |
| 298 | +/// as the computed one, for the reason [`Enc::open`] states. | |
| 299 | +fn bitrate_of(meta: &VideoMeta, settings: &ExportSettings) -> u64 { | |
| 300 | + settings | |
| 301 | + .bitrate | |
| 302 | + .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)) | |
| 303 | +} | |
| 304 | + | |
| 305 | +/// The hardware seat for these settings, opened exactly as the export opens it | |
| 306 | +/// -- the same pins, the same AV1 opt-in, the same dimensions a driver may | |
| 307 | +/// refuse -- or `None` where the software encoder takes the file. The single | |
| 308 | +/// place that choice is made, which is what lets [`planned_video`] *measure* it | |
| 309 | +/// instead of promising it. | |
| 310 | +fn hw_seat(meta: &VideoMeta, settings: &ExportSettings) -> Option<HwEncoder> { | |
| 311 | + if settings.force_sw || forced("VE_SW_ENC") { | |
| 312 | + return None; | |
| 313 | + } | |
| 314 | + let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate).ok()?; | |
| 315 | + let bitrate = bitrate_of(meta, settings); | |
| 316 | + match settings.format { | |
| 317 | + // Opt-in only, for the reason [`Enc::open_av1`] states in full. | |
| 318 | + Format::Av1 if !forced("VE_HW_AV1") => None, | |
| 319 | + Format::Av1 => HwEncoder::open_av1(meta.width, meta.height, fps_num, fps_den, bitrate), | |
| 320 | + _ => HwEncoder::open(meta.width, meta.height, fps_num, fps_den, bitrate), | |
| 321 | + } | |
| 322 | +} | |
| 323 | + | |
| 324 | +/// How a front-end names a video seat: which of the two encoders has the file, | |
| 325 | +/// and which library that is. Not the codec -- a caller shows this beside the | |
| 326 | +/// format it picked, and `rav1e` names AV1 as `rusty_h264` names H.264. One | |
| 327 | +/// place, so what a card says before an export and what its progress line says | |
| 328 | +/// during one cannot drift apart. | |
| 329 | +fn video_label(format: Format, hw: bool) -> &'static str { | |
| 330 | + match (format, hw) { | |
| 331 | + (Format::Av1, true) => "HW encode (VA-API)", | |
| 332 | + (Format::Av1, false) => "SW encode (rav1e)", | |
| 333 | + (_, true) => "HW encode (VA-API)", | |
| 334 | + (_, false) => "SW encode (rusty_h264)", | |
| 335 | + } | |
| 336 | +} | |
| 337 | + | |
| 338 | +/// What the sound is written by. Every audio encoder here is software -- there | |
| 339 | +/// is no hardware AAC, PCM or FLAC seat -- so this names *which* one, and | |
| 340 | +/// whether an mp4 escapes encoding altogether by copying the packets it was | |
| 341 | +/// given. Empty for AV1, which carries no audio at all: a caller joins this to | |
| 342 | +/// a line that already says so, and repeating it would cost a line of card. | |
| 343 | +fn audio_label(project: &Project, format: Format, sound: bool) -> &'static str { | |
| 344 | + match format { | |
| 345 | + Format::Av1 => "", | |
| 346 | + _ if !sound => "no sound to write", | |
| 347 | + Format::Mp4 if equalized(project) => "AAC · SW encode (rusty_aac)", | |
| 348 | + Format::Mp4 => "AAC copy", | |
| 349 | + Format::Wav => "PCM · SW (hound)", | |
| 350 | + Format::Flac => "FLAC · SW (flacenc)", | |
| 351 | + } | |
| 352 | +} | |
| 353 | + | |
| 354 | +/// What [`start`] would write this timeline's sound with, before one is | |
| 355 | +/// started. Pure: no probe and no file opened, so a card may ask it per repaint. | |
| 356 | +pub fn planned_audio(project: &Project, format: Format) -> &'static str { | |
| 357 | + audio_label(project, format, has_sound(project)) | |
| 358 | +} | |
| 359 | + | |
| 360 | +/// What [`start`] would encode the picture with, probed the way the export | |
| 361 | +/// probes it -- the very encoder is opened and closed again -- so this is a | |
| 362 | +/// measurement and not a promise. `None` for a format that carries no picture. | |
| 363 | +/// | |
| 364 | +/// Costs that open (~100 ms here): ask it off a render thread and keep the | |
| 365 | +/// answer until the format, the resolution or the bitrate changes. | |
| 366 | +pub fn planned_video(meta: &VideoMeta, settings: &ExportSettings) -> Option<&'static str> { | |
| 367 | + settings | |
| 368 | + .format | |
| 369 | + .has_video() | |
| 370 | + .then(|| video_label(settings.format, hw_seat(meta, settings).is_some())) | |
| 371 | +} | |
| 372 | + | |
| 264 | 373 | fn settle(shared: &Shared, result: crate::Result<()>) { |
| 265 | 374 | *shared.outcome.lock().unwrap() = Some(result); |
| 266 | 375 | // Published last: a caller that sees the flag is guaranteed the outcome. |
| @@ -367,9 +476,7 @@ fn copy_audio( | ||
| 367 | 476 | // only this case is: a timeline nobody has equalized still leaves through |
| 368 | 477 | // the copy below, packet for packet, so no passthrough quietly becomes a |
| 369 | 478 | // generation of loss. |
| 370 | - let equalized = (0..project.lane(lane).len()) | |
| 371 | - .any(|idx| project.eq_of(lane, idx).is_some_and(|eq| !eq.is_identity())); | |
| 372 | - if equalized { | |
| 479 | + if equalized(project) { | |
| 373 | 480 | return encode_audio(project, meta); |
| 374 | 481 | } |
| 375 | 482 | AudioSession::copy_multi_streams(&project.audio_sources(), segments) |
| @@ -495,6 +602,18 @@ fn run( | ||
| 495 | 602 | .collect(); |
| 496 | 603 | |
| 497 | 604 | let mut encoder = Enc::open(meta, settings)?; |
| 605 | + // What this file is really being written by, for a progress line to name. | |
| 606 | + // Published *after* the seat is open, so a hardware encoder the driver | |
| 607 | + // refused reads as the software one that took over rather than as the hope | |
| 608 | + // it replaced -- and the sound says whether it was copied or encoded, which | |
| 609 | + // `copy_audio` decided a few lines up. | |
| 610 | + *shared.encoders.lock().unwrap() = Some( | |
| 611 | + match audio_label(project, settings.format, audio.is_some()) { | |
| 612 | + // AV1: no sound in the file and none to name. | |
| 613 | + "" => encoder.label().to_string(), | |
| 614 | + sound => format!("{} · {sound}", encoder.label()), | |
| 615 | + }, | |
| 616 | + ); | |
| 498 | 617 | let mut muxer = None; |
| 499 | 618 | let mut done = 0u32; |
| 500 | 619 | let black = Black::new(meta); |
| @@ -734,6 +853,9 @@ fn run_audio( | ||
| 734 | 853 | else { |
| 735 | 854 | return Err("this timeline has no audio to export".into()); |
| 736 | 855 | }; |
| 856 | + // The picture path's line, for a file that is sound alone: there is sound | |
| 857 | + // by the line above, so this names the encoder writing it. | |
| 858 | + *shared.encoders.lock().unwrap() = Some(audio_label(project, format, true).to_string()); | |
| 737 | 859 | let channels = usize::from(audio.channels); |
| 738 | 860 | let frames = (f64::from(project.timeline_frames()) / meta.frame_rate |
| 739 | 861 | * f64::from(audio.sample_rate)) |
| @@ -988,6 +1110,18 @@ enum Enc { | ||
| 988 | 1110 | } |
| 989 | 1111 | |
| 990 | 1112 | impl Enc { |
| 1113 | + /// Which seat this *is*, said the way [`video_label`] says it -- so the | |
| 1114 | + /// name a running export shows is the name the card showed, or the honest | |
| 1115 | + /// difference where the probe and the open disagreed. | |
| 1116 | + fn label(&self) -> &'static str { | |
| 1117 | + match self { | |
| 1118 | + Self::Hw(_) => video_label(Format::Mp4, true), | |
| 1119 | + Self::Sw { .. } => video_label(Format::Mp4, false), | |
| 1120 | + Self::Av1Hw(_) => video_label(Format::Av1, true), | |
| 1121 | + Self::Av1Sw { .. } => video_label(Format::Av1, false), | |
| 1122 | + } | |
| 1123 | + } | |
| 1124 | + | |
| 991 | 1125 | fn open(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { |
| 992 | 1126 | if settings.format == Format::Av1 { |
| 993 | 1127 | return Self::open_av1(meta, settings); |
| @@ -995,18 +1129,13 @@ impl Enc { | ||
| 995 | 1129 | // A caller's number goes through the same clamp as the computed one: a |
| 996 | 1130 | // zero bitrate switches the software encoder's lookahead on, which would |
| 997 | 1131 | // break the one-picture-per-call contract `encode` documents below. |
| 998 | - let bitrate = settings | |
| 999 | - .bitrate | |
| 1000 | - .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)); | |
| 1132 | + let bitrate = bitrate_of(meta, settings); | |
| 1001 | 1133 | // The plugin wants an exact rational, and the muxer already picks one |
| 1002 | 1134 | // that is exact at every rate we can read -- `fps * 1000 / 1000` would |
| 1003 | 1135 | // hand 24000/1001 over as a rounded 23.976, which is the same |
| 1004 | 1136 | // truncation the container timing had. |
| 1005 | - let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate)?; | |
| 1006 | - if !settings.force_sw | |
| 1007 | - && !forced("VE_SW_ENC") | |
| 1008 | - && let Some(hw) = HwEncoder::open(meta.width, meta.height, fps_num, fps_den, bitrate) | |
| 1009 | - { | |
| 1137 | + crate::mux::frame_timing(meta.frame_rate)?; | |
| 1138 | + if let Some(hw) = hw_seat(meta, settings) { | |
| 1010 | 1139 | eprintln!("export encoder: hardware (VA-API plugin)"); |
| 1011 | 1140 | return Ok(Self::Hw(hw)); |
| 1012 | 1141 | } |
| @@ -1047,19 +1176,12 @@ impl Enc { | ||
| 1047 | 1176 | /// cros-codecs release that fixes it) plus a probe encode of one frame at |
| 1048 | 1177 | /// open, after which this can prefer hardware the way H.264 does. |
| 1049 | 1178 | fn open_av1(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { |
| 1050 | - let bitrate = settings | |
| 1051 | - .bitrate | |
| 1052 | - .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)); | |
| 1179 | + let bitrate = bitrate_of(meta, settings); | |
| 1053 | 1180 | let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate)?; |
| 1054 | 1181 | // Two seconds between keyframes, as the H.264 seat does: a seek may only |
| 1055 | 1182 | // land on one, and this is what a cluster of the Matroska file is. |
| 1056 | 1183 | let gop = (meta.frame_rate * 2.0).round().max(1.0) as u64; |
| 1057 | - if forced("VE_HW_AV1") | |
| 1058 | - && !settings.force_sw | |
| 1059 | - && !forced("VE_SW_ENC") | |
| 1060 | - && let Some(hw) = | |
| 1061 | - HwEncoder::open_av1(meta.width, meta.height, fps_num, fps_den, bitrate) | |
| 1062 | - { | |
| 1184 | + if let Some(hw) = hw_seat(meta, settings) { | |
| 1063 | 1185 | eprintln!("export encoder: hardware AV1 (VA-API plugin)"); |
| 1064 | 1186 | return Ok(Self::Av1Hw(hw)); |
| 1065 | 1187 | } |
crates/engine/src/playback.rsmodified+29 -1
| @@ -22,7 +22,7 @@ use crate::ao::AoSession; | ||
| 22 | 22 | use crate::audio::{AudioChunk, AudioSession}; |
| 23 | 23 | use crate::clock::{ClockSource, PlaybackClock}; |
| 24 | 24 | use crate::color::ColorParams; |
| 25 | -use crate::decode::{DecodeSession, Frame, Worker}; | |
| 25 | +use crate::decode::{Backend, BackendCell, DecodeSession, Frame, Worker}; | |
| 26 | 26 | use crate::demux::{Codec, Demuxer, VideoMeta}; |
| 27 | 27 | use crate::eq::EqParams; |
| 28 | 28 | use crate::project::{Clip, Edge, Lane, LaneKind, Project, Rate, Source, Span, Speed}; |
| @@ -161,6 +161,11 @@ pub struct PlaybackSession { | ||
| 161 | 161 | frames: Receiver<Frame>, |
| 162 | 162 | /// The current decode worker. |
| 163 | 163 | worker: Worker, |
| 164 | + /// What that worker opened -- hardware, software, a still, a gap -- written | |
| 165 | + /// by the worker itself and read by [`decode_backend`](Self::decode_backend). | |
| 166 | + /// Replaced with the stream at every seek, so it always describes the | |
| 167 | + /// pictures currently arriving and never the clip before them. | |
| 168 | + backend: BackendCell, | |
| 164 | 169 | /// Workers cancelled by a seek or a clip change, waiting to be reaped. |
| 165 | 170 | /// Joining one inline costs whatever VA-API init it is still inside (98 ms |
| 166 | 171 | /// measured worst case), which is a visible hitch on the caller's thread; |
| @@ -274,6 +279,7 @@ impl PlaybackSession { | ||
| 274 | 279 | native: (meta.width, meta.height), |
| 275 | 280 | frames: stream.frames, |
| 276 | 281 | worker: stream.worker, |
| 282 | + backend: stream.backend, | |
| 277 | 283 | retired: Vec::new(), |
| 278 | 284 | clock: PlaybackClock::new(source), |
| 279 | 285 | audio, |
| @@ -371,6 +377,7 @@ impl PlaybackSession { | ||
| 371 | 377 | native: (width, height), |
| 372 | 378 | frames: stream.frames, |
| 373 | 379 | worker: stream.worker, |
| 380 | + backend: stream.backend, | |
| 374 | 381 | retired: Vec::new(), |
| 375 | 382 | // The song keeps the clock, as a video's own sound does -- and wall |
| 376 | 383 | // time keeps it on a machine with no device, where the picture |
| @@ -446,6 +453,7 @@ impl PlaybackSession { | ||
| 446 | 453 | native: (meta.width, meta.height), |
| 447 | 454 | frames: stream.frames, |
| 448 | 455 | worker: stream.worker, |
| 456 | + backend: stream.backend, | |
| 449 | 457 | retired: Vec::new(), |
| 450 | 458 | clock: PlaybackClock::new(ClockSource::Wall), |
| 451 | 459 | audio: None, |
| @@ -681,6 +689,7 @@ impl PlaybackSession { | ||
| 681 | 689 | native, |
| 682 | 690 | frames: stream.frames, |
| 683 | 691 | worker: stream.worker, |
| 692 | + backend: stream.backend, | |
| 684 | 693 | retired: Vec::new(), |
| 685 | 694 | clock: PlaybackClock::new(match audio { |
| 686 | 695 | Some(_) => ClockSource::Audio, |
| @@ -708,6 +717,24 @@ impl PlaybackSession { | ||
| 708 | 717 | &self.meta |
| 709 | 718 | } |
| 710 | 719 | |
| 720 | + /// What the pictures now arriving are being decoded by: the clip under the | |
| 721 | + /// playhead, since one video worker feeds the composite at a time. An | |
| 722 | + /// atomic load -- a front-end may ask it every repaint -- and it is what | |
| 723 | + /// *opened*, so a clip whose hardware session fell back to software reads | |
| 724 | + /// [`Backend::Software`] from the frame that fallback happened. | |
| 725 | + pub fn decode_backend(&self) -> Backend { | |
| 726 | + self.backend.get() | |
| 727 | + } | |
| 728 | + | |
| 729 | + /// What an export of this timeline would encode the *sound* with, before | |
| 730 | + /// one is started: the copy an mp4 normally makes, or the software encoder | |
| 731 | + /// the format (or an equalized lane) forces. Pure -- no probe, no file | |
| 732 | + /// opened -- so a card may ask it per repaint; the picture's half costs a | |
| 733 | + /// plugin open and lives in [`crate::export::planned_video`]. | |
| 734 | + pub fn planned_audio(&self, format: crate::export::Format) -> &'static str { | |
| 735 | + crate::export::planned_audio(&self.project, format) | |
| 736 | + } | |
| 737 | + | |
| 711 | 738 | /// Why this session plays silent although the file has sound -- an audio |
| 712 | 739 | /// track in a codec we cannot decode, or one the decoder refused. `None` |
| 713 | 740 | /// when there is audio, and for a file that never had any. A front-end |
| @@ -932,6 +959,7 @@ impl PlaybackSession { | ||
| 932 | 959 | // then can it return and be reaped by a later sweep. Nothing here |
| 933 | 960 | // joins, so this ordering costs the seek nothing. |
| 934 | 961 | self.frames = stream.frames; |
| 962 | + self.backend = stream.backend; | |
| 935 | 963 | self.retire(stream.worker); |
| 936 | 964 | } |
| 937 | 965 | self.span = span; |
crates/engine/tests/backends.rsadded+187 -0
| @@ -0,0 +1,187 @@ | ||
| 1 | +//! Which decoder and which encoder a job really runs on -- the introspection a | |
| 2 | +//! front-end shows before an operation and while it runs. | |
| 3 | +//! | |
| 4 | +//! The point of every assertion here is *truthfulness*, not a particular seat: | |
| 5 | +//! this box may or may not have a working plugin, and a test that demanded | |
| 6 | +//! hardware would only be measuring the machine. What must hold is that the | |
| 7 | +//! answer given before an operation is the answer the operation then gives, and | |
| 8 | +//! that neither of them ever names a decoder that could not have run (software | |
| 9 | +//! HEVC, for one, does not exist here). | |
| 10 | + | |
| 11 | +use std::path::PathBuf; | |
| 12 | +use std::time::{Duration, Instant}; | |
| 13 | + | |
| 14 | +use engine::Codec; | |
| 15 | +use engine::decode::{Backend, probe}; | |
| 16 | +use engine::export::{ExportSettings, Format}; | |
| 17 | +use engine::{ExportHandle, PlaybackSession}; | |
| 18 | + | |
| 19 | +fn asset(name: &str) -> PathBuf { | |
| 20 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 21 | + .join("../../assets") | |
| 22 | + .join(name) | |
| 23 | +} | |
| 24 | + | |
| 25 | +fn out_path(name: &str, ext: &str) -> PathBuf { | |
| 26 | + let path = std::env::temp_dir().join(format!("ve_backend_{name}_{}.{ext}", std::process::id())); | |
| 27 | + let _ = std::fs::remove_file(&path); | |
| 28 | + let _ = std::fs::remove_file(format!("{}.part", path.display())); | |
| 29 | + path | |
| 30 | +} | |
| 31 | + | |
| 32 | +/// A still has no coded stream and no decoder to pick, and it must say so | |
| 33 | +/// rather than borrowing the picture path's answer. | |
| 34 | +#[test] | |
| 35 | +fn a_still_probes_as_a_still() { | |
| 36 | + let (codec, backend) = probe(&asset("test_still.png")).expect("probe the still"); | |
| 37 | + assert_eq!(codec, None, "an image has no coded stream to name"); | |
| 38 | + assert_eq!(backend, Backend::Still); | |
| 39 | +} | |
| 40 | + | |
| 41 | +/// H.264 has both seats, so the probe may legitimately answer either -- but it | |
| 42 | +/// must answer one of them, and it must name the codec. | |
| 43 | +#[test] | |
| 44 | +fn an_h264_source_probes_as_one_of_the_two_seats() { | |
| 45 | + let (codec, backend) = probe(&asset("test_baseline.mp4")).expect("probe the baseline fixture"); | |
| 46 | + assert_eq!(codec, Some(Codec::H264)); | |
| 47 | + assert!( | |
| 48 | + matches!(backend, Backend::Hardware | Backend::Software), | |
| 49 | + "a probe that returned must have picked a seat, got {backend:?}" | |
| 50 | + ); | |
| 51 | +} | |
| 52 | + | |
| 53 | +/// HEVC has no software decoder here at all. A box with the plugin answers | |
| 54 | +/// hardware; a box without it is refused by name -- and *never* told that | |
| 55 | +/// `rusty_h264` will take it, which is the one answer that would be a lie. | |
| 56 | +#[test] | |
| 57 | +fn hevc_probes_as_hardware_or_as_a_refusal() { | |
| 58 | + match probe(&asset("test_hevc.mkv")) { | |
| 59 | + Ok((codec, backend)) => { | |
| 60 | + assert_eq!(codec, Some(Codec::Hevc)); | |
| 61 | + assert_eq!(backend, Backend::Hardware, "there is no software HEVC here"); | |
| 62 | + } | |
| 63 | + Err(e) => assert!( | |
| 64 | + e.to_string().contains("plugin"), | |
| 65 | + "a refusal names the plugin, got {e}" | |
| 66 | + ), | |
| 67 | + } | |
| 68 | +} | |
| 69 | + | |
| 70 | +/// The running session reports the decoder its *worker* opened, and it agrees | |
| 71 | +/// with what the probe said before anything played. This is the whole point of | |
| 72 | +/// the cell: the worker writes it where the fallback happens, so a hardware | |
| 73 | +/// session that dropped to software could only be seen here. | |
| 74 | +#[test] | |
| 75 | +fn a_playing_session_reports_the_seat_the_probe_named() { | |
| 76 | + let path = asset("test_baseline.mp4"); | |
| 77 | + let (_codec, probed) = probe(&path).expect("probe the baseline fixture"); | |
| 78 | + let session = PlaybackSession::open(&path).expect("open the fixture"); | |
| 79 | + let started = Instant::now(); | |
| 80 | + loop { | |
| 81 | + let live = session.decode_backend(); | |
| 82 | + if live != Backend::Opening { | |
| 83 | + assert_eq!(live, probed, "the running seat is not the probed one"); | |
| 84 | + break; | |
| 85 | + } | |
| 86 | + assert!( | |
| 87 | + started.elapsed() < Duration::from_secs(20), | |
| 88 | + "the worker never published a decode backend" | |
| 89 | + ); | |
| 90 | + std::thread::sleep(Duration::from_millis(10)); | |
| 91 | + } | |
| 92 | +} | |
| 93 | + | |
| 94 | +/// What the export card says before the button and what the progress line says | |
| 95 | +/// during the job are the same two answers, from the same two functions: the | |
| 96 | +/// picture's seat is probed by opening the very encoder the export opens, and | |
| 97 | +/// the sound's is decided outright. A drift between them is a front-end lying | |
| 98 | +/// about the machine, which is exactly what this whole surface is for. | |
| 99 | +#[test] | |
| 100 | +fn the_planned_encoders_are_the_ones_the_job_opens() { | |
| 101 | + let settings = ExportSettings { | |
| 102 | + format: Format::Mp4, | |
| 103 | + ..ExportSettings::default() | |
| 104 | + }; | |
| 105 | + let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | |
| 106 | + let planned = format!( | |
| 107 | + "{} · {}", | |
| 108 | + engine::export::planned_video(session.meta(), &settings).expect("an mp4 carries picture"), | |
| 109 | + session.planned_audio(Format::Mp4) | |
| 110 | + ); | |
| 111 | + let out = out_path("planned", "mp4"); | |
| 112 | + let handle = session.export_to_with(&out, &settings); | |
| 113 | + let started = Instant::now(); | |
| 114 | + let opened = loop { | |
| 115 | + if let Some(line) = handle.encoders() { | |
| 116 | + break line; | |
| 117 | + } | |
| 118 | + assert!( | |
| 119 | + !handle.is_finished(), | |
| 120 | + "the export settled before it opened an encoder: {:?}", | |
| 121 | + handle.result().map(|r| r.err()) | |
| 122 | + ); | |
| 123 | + assert!( | |
| 124 | + started.elapsed() < Duration::from_secs(60), | |
| 125 | + "no encoder was published" | |
| 126 | + ); | |
| 127 | + std::thread::sleep(Duration::from_millis(10)); | |
| 128 | + }; | |
| 129 | + // Nothing here wants the file, only the seat it was going to be written | |
| 130 | + // with: cancelling deletes the `.part` the worker had started. | |
| 131 | + handle.cancel(); | |
| 132 | + wind_down(&handle); | |
| 133 | + assert_eq!(opened, planned); | |
| 134 | +} | |
| 135 | + | |
| 136 | +/// A WAV job has no picture at all and still names what writes it, so an audio | |
| 137 | +/// export is not a progress line with nothing to say. | |
| 138 | +#[test] | |
| 139 | +fn an_audio_only_job_names_its_encoder() { | |
| 140 | + let settings = ExportSettings { | |
| 141 | + format: Format::Wav, | |
| 142 | + ..ExportSettings::default() | |
| 143 | + }; | |
| 144 | + assert_eq!( | |
| 145 | + engine::export::planned_video( | |
| 146 | + &engine::demux::Demuxer::open(&asset("test_av.mp4")) | |
| 147 | + .expect("open the fixture") | |
| 148 | + .0, | |
| 149 | + &settings | |
| 150 | + ), | |
| 151 | + None, | |
| 152 | + "a WAV has no video seat to name" | |
| 153 | + ); | |
| 154 | + let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | |
| 155 | + let planned = session.planned_audio(Format::Wav); | |
| 156 | + let out = out_path("wav", "wav"); | |
| 157 | + let handle = session.export_to_with(&out, &settings); | |
| 158 | + let started = Instant::now(); | |
| 159 | + let opened = loop { | |
| 160 | + if let Some(line) = handle.encoders() { | |
| 161 | + break line; | |
| 162 | + } | |
| 163 | + assert!( | |
| 164 | + started.elapsed() < Duration::from_secs(60), | |
| 165 | + "no encoder was published" | |
| 166 | + ); | |
| 167 | + std::thread::sleep(Duration::from_millis(10)); | |
| 168 | + }; | |
| 169 | + handle.cancel(); | |
| 170 | + wind_down(&handle); | |
| 171 | + let _ = std::fs::remove_file(&out); | |
| 172 | + assert_eq!(opened, planned); | |
| 173 | +} | |
| 174 | + | |
| 175 | +/// Waits for a cancelled job to settle, so its `.part` is gone before the test | |
| 176 | +/// ends -- and so no worker outlives the process holding a VA-API session. | |
| 177 | +fn wind_down(handle: &ExportHandle) { | |
| 178 | + let started = Instant::now(); | |
| 179 | + while !handle.is_finished() { | |
| 180 | + assert!( | |
| 181 | + started.elapsed() < Duration::from_secs(60), | |
| 182 | + "a cancelled export did not settle" | |
| 183 | + ); | |
| 184 | + std::thread::sleep(Duration::from_millis(10)); | |
| 185 | + } | |
| 186 | + let _ = handle.result(); | |
| 187 | +} |