The preview paints at its own size, not the project's
Browse files at this commit62b680ed5b9d1857431de4842aea05dfaa26809eParent: 72e32b8- perf
- the preview paints at its own size, not the project's
7 changed files +408 -92
- crates/app/src/main.rs +20 -0
- crates/app/src/player/library.rs +4 -0
- crates/app/src/render.rs +57 -0
- crates/app/src/ui/preview.rs +112 -22
- crates/engine/src/decode.rs +94 -59
- crates/engine/src/export.rs +14 -2
- crates/engine/src/playback.rs +107 -9
crates/app/src/main.rsmodified+20 -0
| @@ -761,6 +761,23 @@ struct Player { | |||
| 761 | viz_hs_dragging: bool, | 761 | viz_hs_dragging: bool, |
| 762 | /// The wheel's box, recorded at prepaint for a drag's window position. | 762 | /// The wheel's box, recorded at prepaint for a drag's window position. |
| 763 | viz_wheel: Rc<Cell<Bounds<Pixels>>>, | 763 | viz_wheel: Rc<Cell<Bounds<Pixels>>>, |
| 764 | + /// Where the picture actually lands on screen, probed by the picture area | ||
| 765 | + /// (`render.rs`): what tells the session to paint at the size the preview | ||
| 766 | + /// is *watched* at rather than at the project's own | ||
| 767 | + /// ([`engine::PlaybackSession::set_view_size`]). | ||
| 768 | + picture_box: Rc<Cell<Bounds<Pixels>>>, | ||
| 769 | + /// A screenshot asked for while the picture was painted at the preview's | ||
| 770 | + /// own size: `(session, stem, timecode)`, written by [`Player::flush_shot`] | ||
| 771 | + /// once the seat has painted one frame at the project's size. Taken at the | ||
| 772 | + /// ask so a shot mid-playback is named for the frame it was asked at, not | ||
| 773 | + /// for the one it landed on -- and *keyed to the session that asked*, so a | ||
| 774 | + /// swap mid-flight (a preview opened, another file opened, a session | ||
| 775 | + /// closed) cannot have the new session's frame written under the old name | ||
| 776 | + /// ([`Player::session_swapped`]). | ||
| 777 | + shot_pending: Option<(u64, String, String)>, | ||
| 778 | + /// The active session's identity, for the above: bumped by every door that | ||
| 779 | + /// swaps `session` or `preview_session`. | ||
| 780 | + session_gen: u64, | ||
| 764 | /// Which of the clip's two inks the look section's wheel and colour field | 781 | /// Which of the clip's two inks the look section's wheel and colour field |
| 765 | /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half. | 782 | /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half. |
| 766 | /// The pick stays picked across clips, the way the dock tab's does. | 783 | /// The pick stays picked across clips, the way the dock tab's does. |
| @@ -1086,6 +1103,9 @@ fn main() { | |||
| 1086 | color_dragging: false, | 1103 | color_dragging: false, |
| 1087 | viz_hs_dragging: false, | 1104 | viz_hs_dragging: false, |
| 1088 | viz_wheel: Rc::default(), | 1105 | viz_wheel: Rc::default(), |
| 1106 | + picture_box: Rc::default(), | ||
| 1107 | + shot_pending: None, | ||
| 1108 | + session_gen: 0, | ||
| 1089 | viz_slot: VizSlot::Main, | 1109 | viz_slot: VizSlot::Main, |
| 1090 | viz_hex_edit: None, | 1110 | viz_hex_edit: None, |
| 1091 | pending_viz: None, | 1111 | pending_viz: None, |
crates/app/src/player/library.rsmodified+4 -0
| @@ -569,6 +569,7 @@ impl Player { | |||
| 569 | /// again. | 569 | /// again. |
| 570 | pub(crate) fn close_session(&mut self) { | 570 | pub(crate) fn close_session(&mut self) { |
| 571 | self.session = None; | 571 | self.session = None; |
| 572 | + self.session_swapped(); | ||
| 572 | // The picture goes with it, or the empty window would keep showing the | 573 | // The picture goes with it, or the empty window would keep showing the |
| 573 | // last frame of a timeline that no longer exists. | 574 | // last frame of a timeline that no longer exists. |
| 574 | // | 575 | // |
| @@ -677,6 +678,7 @@ impl Player { | |||
| 677 | // reads as a frozen picture, not as a player awaiting a click. | 678 | // reads as a frozen picture, not as a player awaiting a click. |
| 678 | session.play(); | 679 | session.play(); |
| 679 | self.preview_session = Some(session); | 680 | self.preview_session = Some(session); |
| 681 | + self.session_swapped(); | ||
| 680 | self.notify_user( | 682 | self.notify_user( |
| 681 | format!( | 683 | format!( |
| 682 | "PREVIEWING {} — not on the timeline; esc stops it", | 684 | "PREVIEWING {} — not on the timeline; esc stops it", |
| @@ -1477,6 +1479,7 @@ impl Player { | |||
| 1477 | // is not track 3 of this. | 1479 | // is not track 3 of this. |
| 1478 | self.sub_track = 0; | 1480 | self.sub_track = 0; |
| 1479 | self.session = Some(session); | 1481 | self.session = Some(session); |
| 1482 | + self.session_swapped(); | ||
| 1480 | // A fresh session comes up at full volume; the player's own | 1483 | // A fresh session comes up at full volume; the player's own |
| 1481 | // setting outlives the file, so it is pushed at every new one. | 1484 | // setting outlives the file, so it is pushed at every new one. |
| 1482 | self.apply_volume(); | 1485 | self.apply_volume(); |
| @@ -1790,6 +1793,7 @@ impl Player { | |||
| 1790 | // media: that is the only place an export has ever landed. | 1793 | // media: that is the only place an export has ever landed. |
| 1791 | self.export_path = retarget(&export_path(&session.sources()[0].path), self.format); | 1794 | self.export_path = retarget(&export_path(&session.sources()[0].path), self.format); |
| 1792 | self.session = Some(session); | 1795 | self.session = Some(session); |
| 1796 | + self.session_swapped(); | ||
| 1793 | self.apply_volume(); | 1797 | self.apply_volume(); |
| 1794 | // The other way round from every other door: a project carries | 1798 | // The other way round from every other door: a project carries |
| 1795 | // its own two switches ([`engine::edith`]) and they are what it | 1799 | // its own two switches ([`engine::edith`]) and they are what it |
crates/app/src/render.rsmodified+57 -0
| @@ -76,6 +76,9 @@ impl Player { | |||
| 76 | // bottom of the picture region, which is the one box that is the | 76 | // bottom of the picture region, which is the one box that is the |
| 77 | // picture and nothing else. | 77 | // picture and nothing else. |
| 78 | .relative() | 78 | .relative() |
| 79 | + // ...and the box the frame is *watched* in, which is what the | ||
| 80 | + // session paints at ([`Player::push_view_size`]). | ||
| 81 | + .child(bounds_probe(self.picture_box.clone())) | ||
| 79 | .flex() | 82 | .flex() |
| 80 | .justify_center() | 83 | .justify_center() |
| 81 | .items_center() | 84 | .items_center() |
| @@ -125,6 +128,51 @@ impl Player { | |||
| 125 | } | 128 | } |
| 126 | } | 129 | } |
| 127 | 130 | ||
| 131 | +impl Player { | ||
| 132 | + /// Tells the active session where the picture actually lands, in device | ||
| 133 | + /// pixels, so its seats can paint at that size instead of the project's | ||
| 134 | + /// ([`engine::PlaybackSession::set_view_size`]). Read off the picture | ||
| 135 | + /// area's own bounds -- the box `letterboxed_image` fits the frame into -- | ||
| 136 | + /// and the project's aspect, which is the aspect of every frame it hands | ||
| 137 | + /// over. | ||
| 138 | + /// | ||
| 139 | + /// A size that moved enough for the eye to see gets a fresh picture: the | ||
| 140 | + /// canvas a span paints at is decided when it opens, so a resized window | ||
| 141 | + /// would otherwise sharpen on its next seek and not before. | ||
| 142 | + fn push_view_size(&mut self, window: &mut Window) { | ||
| 143 | + if self.shot_pending.is_some() { | ||
| 144 | + // A screenshot in flight asked for the project's own size and is | ||
| 145 | + // waiting on that frame: pushing the preview's size back now would | ||
| 146 | + // cancel the very picture it is waiting for. | ||
| 147 | + return; | ||
| 148 | + } | ||
| 149 | + let region = self.picture_box.get(); | ||
| 150 | + if region.size.width <= px(1.) || region.size.height <= px(1.) { | ||
| 151 | + // Before the first paint there is no box to read, and a floor is | ||
| 152 | + // not a measurement. | ||
| 153 | + return; | ||
| 154 | + } | ||
| 155 | + let scale = window.scale_factor(); | ||
| 156 | + let Some(session) = self.active_session_mut() else { | ||
| 157 | + return; | ||
| 158 | + }; | ||
| 159 | + // Device pixels, the unit `Contain` measures its fitting size in. | ||
| 160 | + let meta = session.meta(); | ||
| 161 | + let fitted = gpui::ObjectFit::Contain.get_bounds( | ||
| 162 | + region, | ||
| 163 | + size( | ||
| 164 | + gpui::DevicePixels(meta.width as i32), | ||
| 165 | + gpui::DevicePixels(meta.height as i32), | ||
| 166 | + ), | ||
| 167 | + ); | ||
| 168 | + let w = (f32::from(fitted.size.width) * scale).round() as u32; | ||
| 169 | + let h = (f32::from(fitted.size.height) * scale).round() as u32; | ||
| 170 | + if session.set_view_size(w, h) { | ||
| 171 | + session.resync_picture(); | ||
| 172 | + } | ||
| 173 | + } | ||
| 174 | +} | ||
| 175 | + | ||
| 128 | impl Render for Player { | 176 | impl Render for Player { |
| 129 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { | 177 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 130 | // A file that has just been opened sits on its first frame with the | 178 | // A file that has just been opened sits on its first frame with the |
| @@ -134,6 +182,15 @@ impl Render for Player { | |||
| 134 | session.tick(); | 182 | session.tick(); |
| 135 | } | 183 | } |
| 136 | self.pump(window); | 184 | self.pump(window); |
| 185 | + // A screenshot waiting for its project-sized frame is written on the | ||
| 186 | + // repaint that lands it -- and before the size push below, which is | ||
| 187 | + // what gives the preview its own size back. | ||
| 188 | + self.flush_shot(cx); | ||
| 189 | + // ...at the size it is watched at, not the project's: a visualizer | ||
| 190 | + // seat redrawing 1920x1080 for a 600 px preview is nine times the | ||
| 191 | + // pixels for nothing the eye can see. An export has a seat of its own | ||
| 192 | + // and never comes through here. | ||
| 193 | + self.push_view_size(window); | ||
| 137 | // What every hover label asks before it paints: a card or a menu is | 194 | // What every hover label asks before it paints: a card or a menu is |
| 138 | // drawn over whatever the pointer is resting on. | 195 | // drawn over whatever the pointer is resting on. |
| 139 | OVERLAID.store(self.overlaid(), Ordering::Relaxed); | 196 | OVERLAID.store(self.overlaid(), Ordering::Relaxed); |
crates/app/src/ui/preview.rsmodified+112 -22
| @@ -123,44 +123,134 @@ impl Player { | |||
| 123 | ) | 123 | ) |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | - /// The frame on screen right now, written to a PNG: `image` and its | 126 | + /// The frame on screen right now, written to a PNG -- at the size the |
| 127 | - /// `as_bytes` are gpui's own cached copy of what was last handed to the | 127 | + /// **project** is, not at the size the preview shows it. |
| 128 | - /// atlas ([`Player::pump`]), so this reads no decoder and races nothing -- | 128 | + /// |
| 129 | - /// works during preview or on the timeline, whichever is showing. | 129 | + /// The picture area asks the session for frames the size of its own box |
| 130 | + /// ([`PlaybackSession::set_view_size`]), so the atlas holds a preview-sized | ||
| 131 | + /// picture: saving that would cost the user the resolution they asked for | ||
| 132 | + /// (a still of a 1080p project, an 11th of it). So the seat is asked for | ||
| 133 | + /// one picture at the project's own size and the file is written when it | ||
| 134 | + /// lands ([`Player::flush_shot`], off `pump`'s frame-owed signal). Every | ||
| 135 | + /// other part of the gesture is what it always was: the same directory, | ||
| 136 | + /// the same name, the same notice. | ||
| 137 | + /// | ||
| 138 | + /// A library preview is the same rule about *its* file: the seat paints at | ||
| 139 | + /// the file's own resolution for it, which is the full size a still of that | ||
| 140 | + /// file wants. | ||
| 130 | pub(crate) fn take_screenshot(&mut self, cx: &mut Context<Self>) { | 141 | pub(crate) fn take_screenshot(&mut self, cx: &mut Context<Self>) { |
| 131 | - let no_frame = || "NO FRAME TO SAVE — nothing is showing yet".to_string(); | 142 | + if self.shot_pending.is_some() { |
| 132 | - let Some(image) = self.image.clone() else { | 143 | + return; // one at a time: the picture is on its way |
| 133 | - self.notify_user(no_frame().into()); | 144 | + } |
| 134 | - cx.notify(); | 145 | + let Some(session) = self.active_session() else { |
| 135 | - return; | 146 | + self.notify_user("NO FRAME TO SAVE — nothing is showing yet".into()); |
| 136 | - }; | ||
| 137 | - let Some(bytes) = image.as_bytes(0) else { | ||
| 138 | - self.notify_user(no_frame().into()); | ||
| 139 | cx.notify(); | 147 | cx.notify(); |
| 140 | return; | 148 | return; |
| 141 | }; | 149 | }; |
| 142 | - let size = image.size(0); | 150 | + let full = (session.meta().width, session.meta().height); |
| 143 | - let stem = self | 151 | + let stem = session |
| 144 | - .active_session() | 152 | + .sources() |
| 145 | - .and_then(|s| s.sources().first()) | 153 | + .first() |
| 146 | .map_or_else(|| self.name.to_string(), |s| file_name(&s.path)); | 154 | .map_or_else(|| self.name.to_string(), |s| file_name(&s.path)); |
| 147 | let stem = std::path::Path::new(&stem) | 155 | let stem = std::path::Path::new(&stem) |
| 148 | .file_stem() | 156 | .file_stem() |
| 149 | .map_or(stem.clone(), |s| s.to_string_lossy().into_owned()); | 157 | .map_or(stem.clone(), |s| s.to_string_lossy().into_owned()); |
| 150 | - let tc = timecode( | 158 | + let tc = timecode(session.now(), self.active_fps()).replace(':', "-"); |
| 151 | - self.active_session().map_or(0., PlaybackSession::now), | 159 | + // The *exact* setter, not the deadbanded push: a view within 8 px of |
| 152 | - self.active_fps(), | 160 | + // the project is a still of the wrong size. `false` says the view is |
| 153 | - ) | 161 | + // already at it -- and then the frame in hand *is* the project's frame, |
| 154 | - .replace(':', "-"); | 162 | + // so waiting for a picture that would never come (nothing moved, so |
| 163 | + // nothing reseeks) is exactly the trap this avoids. | ||
| 164 | + let moved = self | ||
| 165 | + .active_session_mut() | ||
| 166 | + .is_some_and(|s| s.set_view_size_exact(full.0, full.1)); | ||
| 167 | + if moved { | ||
| 168 | + // One frame at the project's own size, which is a reseek like any | ||
| 169 | + // other edit: the frame it owes is what the flush waits on. | ||
| 170 | + if let Some(session) = self.active_session_mut() { | ||
| 171 | + session.resync_picture(); | ||
| 172 | + } | ||
| 173 | + self.shot_pending = Some((self.session_gen, stem, tc)); | ||
| 174 | + self.reset_after_reseek(); | ||
| 175 | + } else if !self.write_shot(&stem, &tc) { | ||
| 176 | + self.notify_user("NO FRAME TO SAVE — nothing is showing yet".into()); | ||
| 177 | + } | ||
| 178 | + cx.notify(); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + /// Writes the picture the atlas is holding, at the size it holds it, and | ||
| 182 | + /// says whether there was one. The tail [`Player::take_screenshot`] and | ||
| 183 | + /// [`Player::flush_shot`] share, so the two doors cannot write differently. | ||
| 184 | + fn write_shot(&mut self, stem: &str, tc: &str) -> bool { | ||
| 185 | + let Some(image) = self.image.clone() else { | ||
| 186 | + return false; | ||
| 187 | + }; | ||
| 188 | + let Some(bytes) = image.as_bytes(0) else { | ||
| 189 | + return false; | ||
| 190 | + }; | ||
| 191 | + let size = image.size(0); | ||
| 155 | let text = | 192 | let text = |
| 156 | - match save_screenshot(bytes, size.width.0 as u32, size.height.0 as u32, &stem, &tc) { | 193 | + match save_screenshot(bytes, size.width.0 as u32, size.height.0 as u32, stem, tc) { |
| 157 | Ok(path) => format!("SAVED {}", path.display()), | 194 | Ok(path) => format!("SAVED {}", path.display()), |
| 158 | Err(e) => format!("SCREENSHOT FAILED: {e}"), | 195 | Err(e) => format!("SCREENSHOT FAILED: {e}"), |
| 159 | }; | 196 | }; |
| 160 | self.notify_user(text.into()); | 197 | self.notify_user(text.into()); |
| 198 | + true | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + /// The screenshot a preview-sized picture had to wait for: written once the | ||
| 202 | + /// seat's project-sized frame has landed, then the preview takes its own | ||
| 203 | + /// size back ([`Player::push_view_size`] pushes it on the next repaint). | ||
| 204 | + /// | ||
| 205 | + /// A picture that never lands -- the timeline emptied, a source that died | ||
| 206 | + /// -- gives up rather than leaving the preview stuck at the project's size: | ||
| 207 | + /// the wait is bounded, and the notice names what happened. | ||
| 208 | + pub(crate) fn flush_shot(&mut self, cx: &mut Context<Self>) { | ||
| 209 | + let Some((asked, stem, tc)) = self.shot_pending.clone() else { | ||
| 210 | + return; | ||
| 211 | + }; | ||
| 212 | + if asked != self.session_gen { | ||
| 213 | + // The session that asked is gone (a preview opened, another file | ||
| 214 | + // opened, a session closed): its picture is not coming, and the new | ||
| 215 | + // session's frame must not be written under its name. | ||
| 216 | + self.shot_pending = None; | ||
| 217 | + self.notify_user("NO FRAME TO SAVE — the session changed meanwhile".into()); | ||
| 218 | + cx.notify(); | ||
| 219 | + return; | ||
| 220 | + } | ||
| 221 | + if self | ||
| 222 | + .seek_since | ||
| 223 | + .is_some_and(|since| since.elapsed() < std::time::Duration::from_secs(2)) | ||
| 224 | + { | ||
| 225 | + return; // the frame is still being painted | ||
| 226 | + } | ||
| 227 | + self.shot_pending = None; | ||
| 228 | + // The seat's own clamp is the truth about the size it can paint, which | ||
| 229 | + // is what the ask settled on ([`PlaybackSession::set_view_size_exact`]), | ||
| 230 | + // and with the push held while the shot is in flight | ||
| 231 | + // ([`Player::push_view_size`]) it is still holding it here. | ||
| 232 | + let want = self.active_session().map(PlaybackSession::view_size); | ||
| 233 | + let landed = self | ||
| 234 | + .image | ||
| 235 | + .as_ref() | ||
| 236 | + .map(|i| (i.size(0).width.0 as u32, i.size(0).height.0 as u32)) | ||
| 237 | + == want; | ||
| 238 | + if !landed || !self.write_shot(&stem, &tc) { | ||
| 239 | + self.notify_user("NO FRAME TO SAVE — the picture could not be redrawn".into()); | ||
| 240 | + } | ||
| 161 | cx.notify(); | 241 | cx.notify(); |
| 162 | } | 242 | } |
| 163 | 243 | ||
| 244 | + /// The active session has been swapped (`session` or `preview_session`): | ||
| 245 | + /// what anything keyed to a session has to hear. A screenshot in flight is | ||
| 246 | + /// the one such thing ([`Player::shot_pending`]), and it is *keyed* rather | ||
| 247 | + /// than cleared here because a door that forgot to call this would still be | ||
| 248 | + /// caught by the key -- the picture it is holding came from a session that | ||
| 249 | + /// is no longer the one on screen. | ||
| 250 | + pub(crate) fn session_swapped(&mut self) { | ||
| 251 | + self.session_gen += 1; | ||
| 252 | + } | ||
| 253 | + | ||
| 164 | /// The cues the *subtitle lanes* put on screen at `at`, over the picture and | 254 | /// The cues the *subtitle lanes* put on screen at `at`, over the picture and |
| 165 | /// nothing else: bottom-centred where every player puts them, white on a | 255 | /// nothing else: bottom-centred where every player puts them, white on a |
| 166 | /// plate so the film underneath cannot swallow them, and each cue its own | 256 | /// plate so the film underneath cannot swallow them, and each cue its own |
crates/engine/src/decode.rsmodified+94 -59
| @@ -668,6 +668,9 @@ impl DecodeSession { | |||
| 668 | return; | 668 | return; |
| 669 | } | 669 | } |
| 670 | let fps = if fps.is_finite() && fps > 0.0 { fps } else { 30.0 }; | 670 | let fps = if fps.is_finite() && fps > 0.0 { fps } else { 30.0 }; |
| 671 | + // One frame's painting no longer allocates per strand per | ||
| 672 | + // frame: this thread lives for the span. | ||
| 673 | + let mut scratch = VizScratch::default(); | ||
| 671 | let mut render = Render::new( | 674 | let mut render = Render::new( |
| 672 | color, | 675 | color, |
| 673 | transform, | 676 | transform, |
| @@ -681,7 +684,8 @@ impl DecodeSession { | |||
| 681 | return; | 684 | return; |
| 682 | } | 685 | } |
| 683 | let t = f64::from(start_frame + index) / fps; | 686 | let t = f64::from(start_frame + index) / fps; |
| 684 | - let (y, u, v) = visualizer_i420(&peaks, t, width, height, flags, paint); | 687 | + let (y, u, v) = |
| 688 | + visualizer_i420(&mut scratch, &peaks, t, width, height, flags, paint); | ||
| 685 | let frame = render.frame(start_frame + index, &y, &u, &v, width, height); | 689 | let frame = render.frame(start_frame + index, &y, &u, &v, width, height); |
| 686 | if tx.send(frame).is_err() { | 690 | if tx.send(frame).is_err() { |
| 687 | return; // caller moved on | 691 | return; // caller moved on |
| @@ -1735,6 +1739,26 @@ fn viz_span( | |||
| 1735 | } | 1739 | } |
| 1736 | } | 1740 | } |
| 1737 | 1741 | ||
| 1742 | +/// What one frame's painting borrows instead of allocating, owned by whoever | ||
| 1743 | +/// paints a *stream* of frames -- the visualizer worker's own thread, an | ||
| 1744 | +/// export seat, a test -- and reused for every strand of every frame. Eight | ||
| 1745 | +/// `Vec`s, against the twenty-seven a fresh set per strand cost a ring at | ||
| 1746 | +/// eight strands. | ||
| 1747 | +#[derive(Default)] | ||
| 1748 | +pub(crate) struct VizScratch { | ||
| 1749 | + /// The frame's own columns, kept across frames to the widest picture yet. | ||
| 1750 | + lo: Vec<f32>, | ||
| 1751 | + hi: Vec<f32>, | ||
| 1752 | + sig: Vec<f32>, | ||
| 1753 | + /// One strand's sampled positions, the cells its Bresenham runs cover, | ||
| 1754 | + /// the chain's rows, the row band's candidates and their lower hull. | ||
| 1755 | + pts: Vec<(i32, i32)>, | ||
| 1756 | + chain: Vec<(i32, i32)>, | ||
| 1757 | + rows: Vec<(i32, usize, usize)>, | ||
| 1758 | + cand: Vec<(i32, i32)>, | ||
| 1759 | + hull: Vec<usize>, | ||
| 1760 | +} | ||
| 1761 | + | ||
| 1738 | /// One segment's glow as its own disc, stamped at every Bresenham cell: what | 1762 | /// One segment's glow as its own disc, stamped at every Bresenham cell: what |
| 1739 | /// a chain was painted by before [`viz_chain`], and still the cheaper of the | 1763 | /// a chain was painted by before [`viz_chain`], and still the cheaper of the |
| 1740 | /// two when the chain is long ([`viz_discs_are_cheaper`]). | 1764 | /// two when the chain is long ([`viz_discs_are_cheaper`]). |
| @@ -1807,6 +1831,18 @@ fn viz_discs_are_cheaper(samples: usize, cells: usize, glow: u8) -> bool { | |||
| 1807 | (samples as u64) * (2 * u64::from(glow) + 1) < cells as u64 | 1831 | (samples as u64) * (2 * u64::from(glow) + 1) < cells as u64 |
| 1808 | } | 1832 | } |
| 1809 | 1833 | ||
| 1834 | +/// How many cells [`viz_walk`] would push for these samples, without walking | ||
| 1835 | +/// them: one per iteration of that loop, and it iterates `max(dx, dy) + 1` | ||
| 1836 | +/// times a segment. | ||
| 1837 | +fn chain_cells(pts: &[(i32, i32)]) -> usize { | ||
| 1838 | + pts.windows(2) | ||
| 1839 | + .map(|w| { | ||
| 1840 | + let (dx, dy) = ((w[1].0 - w[0].0).abs(), (w[1].1 - w[0].1).abs()); | ||
| 1841 | + dx.max(dy) as usize + 1 | ||
| 1842 | + }) | ||
| 1843 | + .sum() | ||
| 1844 | +} | ||
| 1845 | + | ||
| 1810 | /// The Bresenham cells of one segment, collected rather than stamped: the same | 1846 | /// The Bresenham cells of one segment, collected rather than stamped: the same |
| 1811 | /// walk [`viz_line`] used to rasterise, kept for [`viz_chain`] to paint the | 1847 | /// walk [`viz_line`] used to rasterise, kept for [`viz_chain`] to paint the |
| 1812 | /// whole chain from. | 1848 | /// whole chain from. |
| @@ -1855,13 +1891,18 @@ fn viz_chain( | |||
| 1855 | h: usize, | 1891 | h: usize, |
| 1856 | cw: usize, | 1892 | cw: usize, |
| 1857 | ch: usize, | 1893 | ch: usize, |
| 1858 | - chain: &mut Vec<(i32, i32)>, | 1894 | + scratch: &mut VizScratch, |
| 1859 | bg_y: u8, | 1895 | bg_y: u8, |
| 1860 | ink_y: u8, | 1896 | ink_y: u8, |
| 1861 | ink_u: u8, | 1897 | ink_u: u8, |
| 1862 | ink_v: u8, | 1898 | ink_v: u8, |
| 1863 | glow: u8, | 1899 | glow: u8, |
| 1864 | ) { | 1900 | ) { |
| 1901 | + let VizScratch { pts, chain, rows, cand, hull, .. } = scratch; | ||
| 1902 | + chain.clear(); | ||
| 1903 | + for pair in pts.windows(2) { | ||
| 1904 | + viz_walk(pair[0].0, pair[0].1, pair[1].0, pair[1].1, chain); | ||
| 1905 | + } | ||
| 1865 | if chain.is_empty() { | 1906 | if chain.is_empty() { |
| 1866 | return; | 1907 | return; |
| 1867 | } | 1908 | } |
| @@ -1880,7 +1921,7 @@ fn viz_chain( | |||
| 1880 | // a pixel) and a repeat is the same candidate. | 1921 | // a pixel) and a repeat is the same candidate. |
| 1881 | chain.sort_unstable_by_key(|&(x, row)| (row, x)); | 1922 | chain.sort_unstable_by_key(|&(x, row)| (row, x)); |
| 1882 | chain.dedup(); | 1923 | chain.dedup(); |
| 1883 | - let mut rows: Vec<(i32, usize, usize)> = Vec::new(); | 1924 | + rows.clear(); |
| 1884 | let mut i = 0; | 1925 | let mut i = 0; |
| 1885 | while i < chain.len() { | 1926 | while i < chain.len() { |
| 1886 | let row = chain[i].1; | 1927 | let row = chain[i].1; |
| @@ -1895,10 +1936,8 @@ fn viz_chain( | |||
| 1895 | let mut band = 0usize; | 1936 | let mut band = 0usize; |
| 1896 | // `(x, dy^2)` -- a candidate's horizontal offset is left to the cell that | 1937 | // `(x, dy^2)` -- a candidate's horizontal offset is left to the cell that |
| 1897 | // asks, its vertical one is its own. | 1938 | // asks, its vertical one is its own. |
| 1898 | - let mut cand: Vec<(i32, i32)> = Vec::new(); | 1939 | + // ...and the candidates a run's cells are decided by, once the ones that |
| 1899 | - // The candidates a run's cells are decided by, once the ones that can never | 1940 | + // can never be nearest are off ([`viz_chain`]'s own note). |
| 1900 | - // be nearest are off ([`viz_chain`]'s own note). | ||
| 1901 | - let mut hull: Vec<usize> = Vec::new(); | ||
| 1902 | for row in first..=last { | 1941 | for row in first..=last { |
| 1903 | while band < rows.len() && rows[band].0 < row - g { | 1942 | while band < rows.len() && rows[band].0 < row - g { |
| 1904 | band += 1; | 1943 | band += 1; |
| @@ -1981,25 +2020,25 @@ fn viz_chain( | |||
| 1981 | } | 2020 | } |
| 1982 | hull.push(c); | 2021 | hull.push(c); |
| 1983 | } | 2022 | } |
| 2023 | + // Only the hull's last two entries can change places as x moves | ||
| 2024 | + // forward, so this is a walk and not a search -- and `value` is | ||
| 2025 | + // asked of those two alone. | ||
| 1984 | let mut hp = 0usize; | 2026 | let mut hp = 0usize; |
| 1985 | for x in x_lo..=x_hi { | 2027 | for x in x_lo..=x_hi { |
| 1986 | - while hp + 1 < hull.len() | 2028 | + while hp + 1 < hull.len() && value(hull[hp + 1], x) <= value(hull[hp], x) { |
| 1987 | - && value(hull[hp + 1], x) <= value(hull[hp], x) | ||
| 1988 | - { | ||
| 1989 | hp += 1; | 2029 | hp += 1; |
| 1990 | } | 2030 | } |
| 1991 | let (cx, dy2) = cand[hull[hp]]; | 2031 | let (cx, dy2) = cand[hull[hp]]; |
| 1992 | let d = x - cx; | 2032 | let d = x - cx; |
| 1993 | let d2 = d * d + dy2; | 2033 | let d2 = d * d + dy2; |
| 1994 | - if d2 > g * g { | 2034 | + if d2 <= g * g { |
| 1995 | - continue; | 2035 | + let fall = if d2 == 0 { |
| 2036 | + 1.0 | ||
| 2037 | + } else { | ||
| 2038 | + (-0.45 * d2 as f32 / (g * g).max(1) as f32).exp() | ||
| 2039 | + }; | ||
| 2040 | + viz_stamp(y, u, v, w, h, cw, ch, x, row, fall, bg_y, ink_y, ink_u, ink_v); | ||
| 1996 | } | 2041 | } |
| 1997 | - let fall = if d2 == 0 { | ||
| 1998 | - 1.0 | ||
| 1999 | - } else { | ||
| 2000 | - (-0.45 * d2 as f32 / (g * g).max(1) as f32).exp() | ||
| 2001 | - }; | ||
| 2002 | - viz_stamp(y, u, v, w, h, cw, ch, x, row, fall, bg_y, ink_y, ink_u, ink_v); | ||
| 2003 | } | 2042 | } |
| 2004 | } | 2043 | } |
| 2005 | } | 2044 | } |
| @@ -2010,6 +2049,7 @@ fn viz_chain( | |||
| 2010 | /// envelope at pixel resolution, or a multi-strand ribbon. Ink is | 2049 | /// envelope at pixel resolution, or a multi-strand ribbon. Ink is |
| 2011 | /// [`viz_ink_from_paint`]. A frame always exists; silence is the zero line. | 2050 | /// [`viz_ink_from_paint`]. A frame always exists; silence is the zero line. |
| 2012 | pub(crate) fn visualizer_i420( | 2051 | pub(crate) fn visualizer_i420( |
| 2052 | + scratch: &mut VizScratch, | ||
| 2013 | peaks: &[(f32, f32)], | 2053 | peaks: &[(f32, f32)], |
| 2014 | t_secs: f64, | 2054 | t_secs: f64, |
| 2015 | width: u32, | 2055 | width: u32, |
| @@ -2036,9 +2076,12 @@ pub(crate) fn visualizer_i420( | |||
| 2036 | y[z * w + col] = y[z * w + col].max(bg_y.saturating_add(18)); | 2076 | y[z * w + col] = y[z * w + col].max(bg_y.saturating_add(18)); |
| 2037 | } | 2077 | } |
| 2038 | 2078 | ||
| 2039 | - let mut lo = vec![0.0f32; w]; | 2079 | + scratch.lo.clear(); |
| 2040 | - let mut hi = vec![0.0f32; w]; | 2080 | + scratch.lo.resize(w, 0.0); |
| 2041 | - let mut sig = vec![0.0f32; w]; | 2081 | + scratch.hi.clear(); |
| 2082 | + scratch.hi.resize(w, 0.0); | ||
| 2083 | + scratch.sig.clear(); | ||
| 2084 | + scratch.sig.resize(w, 0.0); | ||
| 2042 | if !peaks.is_empty() { | 2085 | if !peaks.is_empty() { |
| 2043 | let duration = peaks.len() as f64 / f64::from(VIZ_BUCKETS_PER_SEC); | 2086 | let duration = peaks.len() as f64 / f64::from(VIZ_BUCKETS_PER_SEC); |
| 2044 | let per_sec = f64::from(VIZ_BUCKETS_PER_SEC); | 2087 | let per_sec = f64::from(VIZ_BUCKETS_PER_SEC); |
| @@ -2081,9 +2124,9 @@ pub(crate) fn visualizer_i420( | |||
| 2081 | mn = mn.min(l); | 2124 | mn = mn.min(l); |
| 2082 | mx = mx.max(h); | 2125 | mx = mx.max(h); |
| 2083 | } | 2126 | } |
| 2084 | - lo[col] = mn; | 2127 | + scratch.lo[col] = mn; |
| 2085 | - hi[col] = mx; | 2128 | + scratch.hi[col] = mx; |
| 2086 | - sig[col] = signed_at(origin + (col as f64 + 0.5) / w as f64 * window); | 2129 | + scratch.sig[col] = signed_at(origin + (col as f64 + 0.5) / w as f64 * window); |
| 2087 | } | 2130 | } |
| 2088 | // Whole-file peak, not the window's: a 2s window that gains or loses | 2131 | // Whole-file peak, not the window's: a 2s window that gains or loses |
| 2089 | // a loud hit used to rescale every column, so the same bump changed | 2132 | // a loud hit used to rescale every column, so the same bump changed |
| @@ -2094,9 +2137,9 @@ pub(crate) fn visualizer_i420( | |||
| 2094 | .fold(0.0f32, f32::max); | 2137 | .fold(0.0f32, f32::max); |
| 2095 | if peak > 1e-4 { | 2138 | if peak > 1e-4 { |
| 2096 | for i in 0..w { | 2139 | for i in 0..w { |
| 2097 | - lo[i] /= peak; | 2140 | + scratch.lo[i] /= peak; |
| 2098 | - hi[i] /= peak; | 2141 | + scratch.hi[i] /= peak; |
| 2099 | - sig[i] /= peak; | 2142 | + scratch.sig[i] /= peak; |
| 2100 | } | 2143 | } |
| 2101 | } | 2144 | } |
| 2102 | } | 2145 | } |
| @@ -2106,7 +2149,7 @@ pub(crate) fn visualizer_i420( | |||
| 2106 | let tau = std::f64::consts::TAU; | 2149 | let tau = std::f64::consts::TAU; |
| 2107 | let travel = t_secs * 1.35; | 2150 | let travel = t_secs * 1.35; |
| 2108 | for col in 0..w { | 2151 | for col in 0..w { |
| 2109 | - let env = lo[col].abs().max(hi[col]).max(0.06); | 2152 | + let env = scratch.lo[col].abs().max(scratch.hi[col]).max(0.06); |
| 2110 | let x_phase = (col as f64 / w.max(1) as f64) * 2.15 * tau; | 2153 | let x_phase = (col as f64 / w.max(1) as f64) * 2.15 * tau; |
| 2111 | for s in 0..u32::from(viz_strands_of(paint)).max(1) { | 2154 | for s in 0..u32::from(viz_strands_of(paint)).max(1) { |
| 2112 | let frac = s as f32 / (viz_strands_of(paint).max(2) as f32 - 1.0); | 2155 | let frac = s as f32 / (viz_strands_of(paint).max(2) as f32 - 1.0); |
| @@ -2141,8 +2184,6 @@ pub(crate) fn visualizer_i420( | |||
| 2141 | let steps = w.max(h) * 4; | 2184 | let steps = w.max(h) * 4; |
| 2142 | let strands = viz_strands_of(paint); | 2185 | let strands = viz_strands_of(paint); |
| 2143 | let glow = viz_glow_of(paint); | 2186 | let glow = viz_glow_of(paint); |
| 2144 | - let mut chain: Vec<(i32, i32)> = Vec::new(); | ||
| 2145 | - let mut pts: Vec<(i32, i32)> = Vec::new(); | ||
| 2146 | for strand in 0..strands { | 2187 | for strand in 0..strands { |
| 2147 | let dual = strands >= 2 && strand >= strands / 2; | 2188 | let dual = strands >= 2 && strand >= strands / 2; |
| 2148 | // The dual half takes the clip's own strand colour when it | 2189 | // The dual half takes the clip's own strand colour when it |
| @@ -2164,24 +2205,20 @@ pub(crate) fn visualizer_i420( | |||
| 2164 | let (iy, iu, iv) = rgb_yuv(ir as i32, ig as i32, ib as i32); | 2205 | let (iy, iu, iv) = rgb_yuv(ir as i32, ig as i32, ib as i32); |
| 2165 | let scale = 0.65 + 0.35 * f32::from(strand % 4) / 3.0; | 2206 | let scale = 0.65 + 0.35 * f32::from(strand % 4) / 3.0; |
| 2166 | let phase = f32::from(strand) * 0.35; | 2207 | let phase = f32::from(strand) * 0.35; |
| 2167 | - pts.clear(); | 2208 | + scratch.pts.clear(); |
| 2168 | for i in 0..=steps { | 2209 | for i in 0..=steps { |
| 2169 | let t = i as f32 / steps as f32; | 2210 | let t = i as f32 / steps as f32; |
| 2170 | let theta = t * tau + t_secs as f32 * 1.1 + phase; | 2211 | let theta = t * tau + t_secs as f32 * 1.1 + phase; |
| 2171 | let col = ((t * w as f32) as usize).min(w.saturating_sub(1)); | 2212 | let col = ((t * w as f32) as usize).min(w.saturating_sub(1)); |
| 2172 | - let env = lo[col].abs().max(hi[col]); | 2213 | + let env = scratch.lo[col].abs().max(scratch.hi[col]); |
| 2173 | let wobble = (theta * 3.0 + phase).sin() * 0.12 * env; | 2214 | let wobble = (theta * 3.0 + phase).sin() * 0.12 * env; |
| 2174 | let r = r0 + amp * env * scale + amp * wobble; | 2215 | let r = r0 + amp * env * scale + amp * wobble; |
| 2175 | let x = (cx + r * theta.cos()).round() as i32; | 2216 | let x = (cx + r * theta.cos()).round() as i32; |
| 2176 | let row = (cy + r * theta.sin()).round() as i32; | 2217 | let row = (cy + r * theta.sin()).round() as i32; |
| 2177 | - pts.push((x, row)); | 2218 | + scratch.pts.push((x, row)); |
| 2178 | - } | ||
| 2179 | - chain.clear(); | ||
| 2180 | - for pair in pts.windows(2) { | ||
| 2181 | - viz_walk(pair[0].0, pair[0].1, pair[1].0, pair[1].1, &mut chain); | ||
| 2182 | } | 2219 | } |
| 2183 | - if viz_discs_are_cheaper(pts.len(), chain.len(), glow) { | 2220 | + if viz_discs_are_cheaper(scratch.pts.len(), chain_cells(&scratch.pts), glow) { |
| 2184 | - for pair in pts.windows(2) { | 2221 | + for pair in scratch.pts.windows(2) { |
| 2185 | viz_line( | 2222 | viz_line( |
| 2186 | &mut y, &mut u, &mut v, w, h, cw, ch, pair[0].0, pair[0].1, pair[1].0, | 2223 | &mut y, &mut u, &mut v, w, h, cw, ch, pair[0].0, pair[0].1, pair[1].0, |
| 2187 | pair[1].1, bg_y, iy, iu, iv, glow, | 2224 | pair[1].1, bg_y, iy, iu, iv, glow, |
| @@ -2189,23 +2226,21 @@ pub(crate) fn visualizer_i420( | |||
| 2189 | } | 2226 | } |
| 2190 | } else { | 2227 | } else { |
| 2191 | viz_chain( | 2228 | viz_chain( |
| 2192 | - &mut y, &mut u, &mut v, w, h, cw, ch, &mut chain, bg_y, iy, iu, iv, glow, | 2229 | + &mut y, &mut u, &mut v, w, h, cw, ch, scratch, bg_y, iy, iu, iv, glow, |
| 2193 | ); | 2230 | ); |
| 2194 | } | 2231 | } |
| 2195 | } | 2232 | } |
| 2196 | } | 2233 | } |
| 2197 | VIZ_STYLE_WAVE => { | 2234 | VIZ_STYLE_WAVE => { |
| 2198 | - let mut chain: Vec<(i32, i32)> = Vec::new(); | 2235 | + scratch.pts.clear(); |
| 2199 | - let mut pts: Vec<(i32, i32)> = Vec::new(); | ||
| 2200 | for col in 0..w { | 2236 | for col in 0..w { |
| 2201 | - pts.push((col as i32, (center - sig[col] * half).round() as i32)); | 2237 | + scratch |
| 2202 | - } | 2238 | + .pts |
| 2203 | - for pair in pts.windows(2) { | 2239 | + .push((col as i32, (center - scratch.sig[col] * half).round() as i32)); |
| 2204 | - viz_walk(pair[0].0, pair[0].1, pair[1].0, pair[1].1, &mut chain); | ||
| 2205 | } | 2240 | } |
| 2206 | let glow = viz_glow_of(paint); | 2241 | let glow = viz_glow_of(paint); |
| 2207 | - if viz_discs_are_cheaper(pts.len(), chain.len(), glow) { | 2242 | + if viz_discs_are_cheaper(scratch.pts.len(), chain_cells(&scratch.pts), glow) { |
| 2208 | - for pair in pts.windows(2) { | 2243 | + for pair in scratch.pts.windows(2) { |
| 2209 | viz_line( | 2244 | viz_line( |
| 2210 | &mut y, &mut u, &mut v, w, h, cw, ch, pair[0].0, pair[0].1, pair[1].0, | 2245 | &mut y, &mut u, &mut v, w, h, cw, ch, pair[0].0, pair[0].1, pair[1].0, |
| 2211 | pair[1].1, bg_y, ink_y, ink_u, ink_v, glow, | 2246 | pair[1].1, bg_y, ink_y, ink_u, ink_v, glow, |
| @@ -2213,7 +2248,7 @@ pub(crate) fn visualizer_i420( | |||
| 2213 | } | 2248 | } |
| 2214 | } else { | 2249 | } else { |
| 2215 | viz_chain( | 2250 | viz_chain( |
| 2216 | - &mut y, &mut u, &mut v, w, h, cw, ch, &mut chain, bg_y, ink_y, ink_u, ink_v, | 2251 | + &mut y, &mut u, &mut v, w, h, cw, ch, scratch, bg_y, ink_y, ink_u, ink_v, |
| 2217 | glow, | 2252 | glow, |
| 2218 | ); | 2253 | ); |
| 2219 | } | 2254 | } |
| @@ -2223,8 +2258,8 @@ pub(crate) fn visualizer_i420( | |||
| 2223 | // the fold+smooth above. Default look. | 2258 | // the fold+smooth above. Default look. |
| 2224 | let glow = i32::from(viz_glow_of(paint)); | 2259 | let glow = i32::from(viz_glow_of(paint)); |
| 2225 | for col in 0..w { | 2260 | for col in 0..w { |
| 2226 | - let top = (center - hi[col] * half).round() as i32; | 2261 | + let top = (center - scratch.hi[col] * half).round() as i32; |
| 2227 | - let bot = (center - lo[col] * half).round() as i32; | 2262 | + let bot = (center - scratch.lo[col] * half).round() as i32; |
| 2228 | viz_span( | 2263 | viz_span( |
| 2229 | &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top, bot, bg_y, ink_y, ink_u, | 2264 | &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top, bot, bg_y, ink_y, ink_u, |
| 2230 | ink_v, | 2265 | ink_v, |
| @@ -2570,7 +2605,7 @@ mod tests { | |||
| 2570 | *p = (-a.abs(), a.abs()); | 2605 | *p = (-a.abs(), a.abs()); |
| 2571 | } | 2606 | } |
| 2572 | let flags = 1u8; | 2607 | let flags = 1u8; |
| 2573 | - let (y, u, v) = visualizer_i420(&peaks, 1.0, 320, 180, flags, 0); | 2608 | + let (y, u, v) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 320, 180, flags, 0); |
| 2574 | let ink = y.iter().filter(|&&p| p > 40).count(); | 2609 | let ink = y.iter().filter(|&&p| p > 40).count(); |
| 2575 | assert!(ink > 200, "envelope missing: {ink}"); | 2610 | assert!(ink > 200, "envelope missing: {ink}"); |
| 2576 | assert!(ink < y.len() * 9 / 10, "solid frame, not an envelope: {ink}"); | 2611 | assert!(ink < y.len() * 9 / 10, "solid frame, not an envelope: {ink}"); |
| @@ -2583,13 +2618,13 @@ mod tests { | |||
| 2583 | assert!(blue > 10, "default ink was not timeline blue: {blue}"); | 2618 | assert!(blue > 10, "default ink was not timeline blue: {blue}"); |
| 2584 | 2619 | ||
| 2585 | let ring = with_viz_style(flags, VIZ_STYLE_RING); | 2620 | let ring = with_viz_style(flags, VIZ_STYLE_RING); |
| 2586 | - let (yr, _, _) = visualizer_i420(&peaks, 1.0, 320, 180, ring, 0); | 2621 | + let (yr, _, _) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 320, 180, ring, 0); |
| 2587 | let ring_ink = yr.iter().filter(|&&p| p > 40).count(); | 2622 | let ring_ink = yr.iter().filter(|&&p| p > 40).count(); |
| 2588 | assert!(ring_ink > 80, "ring missing: {ring_ink}"); | 2623 | assert!(ring_ink > 80, "ring missing: {ring_ink}"); |
| 2589 | assert!(ring_ink < ink, "ring should be a stroke, not a fill"); | 2624 | assert!(ring_ink < ink, "ring should be a stroke, not a fill"); |
| 2590 | 2625 | ||
| 2591 | let hue_paint = with_viz_paint_hue(0, 40); | 2626 | let hue_paint = with_viz_paint_hue(0, 40); |
| 2592 | - let (_, u2, v2) = visualizer_i420(&peaks, 1.0, 64, 32, flags, hue_paint); | 2627 | + let (_, u2, v2) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 64, 32, flags, hue_paint); |
| 2593 | let mut shifted = 0usize; | 2628 | let mut shifted = 0usize; |
| 2594 | for i in 0..u2.len() { | 2629 | for i in 0..u2.len() { |
| 2595 | if u2[i] < 120 && v2[i] > 130 { | 2630 | if u2[i] < 120 && v2[i] > 130 { |
| @@ -2686,7 +2721,7 @@ mod tests { | |||
| 2686 | } | 2721 | } |
| 2687 | let ring = with_viz_style(1, VIZ_STYLE_RING); | 2722 | let ring = with_viz_style(1, VIZ_STYLE_RING); |
| 2688 | let auto = VIZ_PAINT_AZURE; | 2723 | let auto = VIZ_PAINT_AZURE; |
| 2689 | - let (y0, u0, v0) = visualizer_i420(&peaks, 1.0, 320, 180, ring, auto); | 2724 | + let (y0, u0, v0) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 320, 180, ring, auto); |
| 2690 | 2725 | ||
| 2691 | // The automatic dual is half a wheel from the clip's hue at the | 2726 | // The automatic dual is half a wheel from the clip's hue at the |
| 2692 | // clip's own saturation; choosing exactly that colour changes | 2727 | // clip's own saturation; choosing exactly that colour changes |
| @@ -2696,14 +2731,14 @@ mod tests { | |||
| 2696 | viz_sat_ui(auto), | 2731 | viz_sat_ui(auto), |
| 2697 | ); | 2732 | ); |
| 2698 | assert!(viz_strand_set(same)); | 2733 | assert!(viz_strand_set(same)); |
| 2699 | - let (ys, us, vs) = visualizer_i420(&peaks, 1.0, 320, 180, ring, same); | 2734 | + let (ys, us, vs) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 320, 180, ring, same); |
| 2700 | assert_eq!(ys, y0, "the automatic complement, spelled out, is the same frame"); | 2735 | assert_eq!(ys, y0, "the automatic complement, spelled out, is the same frame"); |
| 2701 | assert_eq!(us, u0); | 2736 | assert_eq!(us, u0); |
| 2702 | assert_eq!(vs, v0); | 2737 | assert_eq!(vs, v0); |
| 2703 | 2738 | ||
| 2704 | // A colour of its own repaints the dual strands in it. | 2739 | // A colour of its own repaints the dual strands in it. |
| 2705 | let red = with_viz_paint_strand_sat(with_viz_paint_strand_hue(auto, 0), 255); | 2740 | let red = with_viz_paint_strand_sat(with_viz_paint_strand_hue(auto, 0), 255); |
| 2706 | - let (_, u1, v1) = visualizer_i420(&peaks, 1.0, 320, 180, ring, red); | 2741 | + let (_, u1, v1) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 320, 180, ring, red); |
| 2707 | let red_px = (0..u1.len()).filter(|&i| u1[i] < 110 && v1[i] > 200).count(); | 2742 | let red_px = (0..u1.len()).filter(|&i| u1[i] < 110 && v1[i] > 200).count(); |
| 2708 | let auto_px = (0..u0.len()).filter(|&i| u0[i] < 110 && v0[i] > 200).count(); | 2743 | let auto_px = (0..u0.len()).filter(|&i| u0[i] < 110 && v0[i] > 200).count(); |
| 2709 | assert!(red_px > 20, "the chosen colour did not reach the frame: {red_px}"); | 2744 | assert!(red_px > 20, "the chosen colour did not reach the frame: {red_px}"); |
| @@ -2713,7 +2748,7 @@ mod tests { | |||
| 2713 | // the unset picture, byte for byte. | 2748 | // the unset picture, byte for byte. |
| 2714 | let stale = auto | (77u64 << 32) | (9u64 << 40); | 2749 | let stale = auto | (77u64 << 32) | (9u64 << 40); |
| 2715 | assert!(!viz_strand_set(stale)); | 2750 | assert!(!viz_strand_set(stale)); |
| 2716 | - let (yt, ut, vt) = visualizer_i420(&peaks, 1.0, 320, 180, ring, stale); | 2751 | + let (yt, ut, vt) = visualizer_i420(&mut VizScratch::default(), &peaks, 1.0, 320, 180, ring, stale); |
| 2717 | assert_eq!(yt, y0, "an unset strand colour is the frame it always was"); | 2752 | assert_eq!(yt, y0, "an unset strand colour is the frame it always was"); |
| 2718 | assert_eq!(ut, u0); | 2753 | assert_eq!(ut, u0); |
| 2719 | assert_eq!(vt, v0); | 2754 | assert_eq!(vt, v0); |
| @@ -2761,7 +2796,7 @@ mod tests { | |||
| 2761 | }; | 2796 | }; |
| 2762 | let flags = with_viz_style(1, style); | 2797 | let flags = with_viz_style(1, style); |
| 2763 | let paint = with_viz_paint_glow(VIZ_PAINT_AZURE, glow); | 2798 | let paint = with_viz_paint_glow(VIZ_PAINT_AZURE, glow); |
| 2764 | - let (y, u, v) = visualizer_i420(&peaks, 37.5, 1920, 1080, flags, paint); | 2799 | + let (y, u, v) = visualizer_i420(&mut VizScratch::default(), &peaks, 37.5, 1920, 1080, flags, paint); |
| 2765 | let mut hash = 0xcbf2_9ce4_8422_2325u64; | 2800 | let mut hash = 0xcbf2_9ce4_8422_2325u64; |
| 2766 | for buf in [&y, &u, &v] { | 2801 | for buf in [&y, &u, &v] { |
| 2767 | for &b in buf.iter() { | 2802 | for &b in buf.iter() { |
crates/engine/src/export.rsmodified+14 -2
| @@ -4427,6 +4427,9 @@ enum ClipDecoder { | |||
| 4427 | next: u32, | 4427 | next: u32, |
| 4428 | flags: u8, | 4428 | flags: u8, |
| 4429 | paint: u64, | 4429 | paint: u64, |
| 4430 | + /// This seat's own painting scratch, kept for the whole export: the | ||
| 4431 | + /// same reuse the playback worker's thread does. | ||
| 4432 | + scratch: crate::decode::VizScratch, | ||
| 4430 | y: Vec<u8>, | 4433 | y: Vec<u8>, |
| 4431 | u: Vec<u8>, | 4434 | u: Vec<u8>, |
| 4432 | v: Vec<u8>, | 4435 | v: Vec<u8>, |
| @@ -4555,6 +4558,7 @@ impl ClipDecoder { | |||
| 4555 | next: start_frame, | 4558 | next: start_frame, |
| 4556 | flags, | 4559 | flags, |
| 4557 | paint, | 4560 | paint, |
| 4561 | + scratch: crate::decode::VizScratch::default(), | ||
| 4558 | y: Vec::new(), | 4562 | y: Vec::new(), |
| 4559 | u: Vec::new(), | 4563 | u: Vec::new(), |
| 4560 | v: Vec::new(), | 4564 | v: Vec::new(), |
| @@ -4577,12 +4581,20 @@ impl ClipDecoder { | |||
| 4577 | next, | 4581 | next, |
| 4578 | flags, | 4582 | flags, |
| 4579 | paint, | 4583 | paint, |
| 4584 | + scratch, | ||
| 4580 | y, | 4585 | y, |
| 4581 | u, | 4586 | u, |
| 4582 | v, | 4587 | v, |
| 4583 | } => { | 4588 | } => { |
| 4584 | - let (yy, uu, vv) = | 4589 | + let (yy, uu, vv) = crate::decode::visualizer_i420( |
| 4585 | - crate::decode::visualizer_i420(peaks, f64::from(*next) / *fps, *width, *height, *flags, *paint); | 4590 | + scratch, |
| 4591 | + peaks, | ||
| 4592 | + f64::from(*next) / *fps, | ||
| 4593 | + *width, | ||
| 4594 | + *height, | ||
| 4595 | + *flags, | ||
| 4596 | + *paint, | ||
| 4597 | + ); | ||
| 4586 | *y = yy; | 4598 | *y = yy; |
| 4587 | *u = uu; | 4599 | *u = uu; |
| 4588 | *v = vv; | 4600 | *v = vv; |
crates/engine/src/playback.rsmodified+107 -9
| @@ -431,6 +431,11 @@ pub struct PlaybackSession { | |||
| 431 | /// span most of the time. | 431 | /// span most of the time. |
| 432 | dissolve: Option<Dissolve>, | 432 | dissolve: Option<Dissolve>, |
| 433 | /// The last clip has been played out; see [`PlaybackSession::is_eos`]. | 433 | /// The last clip has been played out; see [`PlaybackSession::is_eos`]. |
| 434 | + /// The size a front-end showing this session *on screen* wants its | ||
| 435 | + /// pictures at, in device pixels ([`Self::set_view_size`]). `None` is the | ||
| 436 | + /// project's own -- every file-level caller, every test, and an export, | ||
| 437 | + /// which has a seat of its own ([`crate::export`]) and never comes here. | ||
| 438 | + view: Option<(u32, u32)>, | ||
| 434 | eos: bool, | 439 | eos: bool, |
| 435 | /// The mix the running mixer is reading, when there is one: what lets a | 440 | /// The mix the running mixer is reading, when there is one: what lets a |
| 436 | /// fader and the master ceiling move without rebuilding the audio pipeline | 441 | /// fader and the master ceiling move without rebuilding the audio pipeline |
| @@ -632,6 +637,7 @@ impl PlaybackSession { | |||
| 632 | span, | 637 | span, |
| 633 | span_priming: true, | 638 | span_priming: true, |
| 634 | dissolve: None, | 639 | dissolve: None, |
| 640 | + view: None, | ||
| 635 | eos: false, | 641 | eos: false, |
| 636 | mix: None, | 642 | mix: None, |
| 637 | priming: false, | 643 | priming: false, |
| @@ -761,6 +767,7 @@ impl PlaybackSession { | |||
| 761 | span, | 767 | span, |
| 762 | span_priming: true, | 768 | span_priming: true, |
| 763 | dissolve: None, | 769 | dissolve: None, |
| 770 | + view: None, | ||
| 764 | eos: false, | 771 | eos: false, |
| 765 | mix: None, | 772 | mix: None, |
| 766 | priming: false, | 773 | priming: false, |
| @@ -854,6 +861,7 @@ impl PlaybackSession { | |||
| 854 | span, | 861 | span, |
| 855 | span_priming: true, | 862 | span_priming: true, |
| 856 | dissolve: None, | 863 | dissolve: None, |
| 864 | + view: None, | ||
| 857 | eos: false, | 865 | eos: false, |
| 858 | mix: None, | 866 | mix: None, |
| 859 | priming: false, | 867 | priming: false, |
| @@ -1207,6 +1215,7 @@ impl PlaybackSession { | |||
| 1207 | span, | 1215 | span, |
| 1208 | span_priming: true, | 1216 | span_priming: true, |
| 1209 | dissolve: None, | 1217 | dissolve: None, |
| 1218 | + view: None, | ||
| 1210 | eos: false, | 1219 | eos: false, |
| 1211 | mix: None, | 1220 | mix: None, |
| 1212 | priming: false, | 1221 | priming: false, |
| @@ -1749,6 +1758,10 @@ impl PlaybackSession { | |||
| 1749 | /// video that is now the worker's own doing -- it opens the file, so it is | 1758 | /// video that is now the worker's own doing -- it opens the file, so it is |
| 1750 | /// the one that finds out -- and nothing on this thread waits to hear it. | 1759 | /// the one that finds out -- and nothing on this thread waits to hear it. |
| 1751 | fn start_span(&mut self, span: Option<Span>) { | 1760 | fn start_span(&mut self, span: Option<Span>) { |
| 1761 | + // The viewer's own size, once one has said it ([`Self::set_view_size`]): | ||
| 1762 | + // every picture below is painted at *this*, and the project's | ||
| 1763 | + // resolution otherwise. | ||
| 1764 | + let (canvas_w, canvas_h) = self.view_size(); | ||
| 1752 | // The outgoing span's own dissolve, if it ever opened B's decoder: | 1765 | // The outgoing span's own dissolve, if it ever opened B's decoder: |
| 1753 | // that decoder's pictures were for a window this span is leaving | 1766 | // that decoder's pictures were for a window this span is leaving |
| 1754 | // (played through, paused before, or seeked away from), and never | 1767 | // (played through, paused before, or seeked away from), and never |
| @@ -1828,8 +1841,8 @@ impl PlaybackSession { | |||
| 1828 | .copied() | 1841 | .copied() |
| 1829 | .unwrap_or_default(), | 1842 | .unwrap_or_default(), |
| 1830 | Composer::new( | 1843 | Composer::new( |
| 1831 | - self.meta.width, | 1844 | + canvas_w, |
| 1832 | - self.meta.height, | 1845 | + canvas_h, |
| 1833 | self.project.composite_fit_at(start), | 1846 | self.project.composite_fit_at(start), |
| 1834 | ), | 1847 | ), |
| 1835 | ) | 1848 | ) |
| @@ -1861,8 +1874,8 @@ impl PlaybackSession { | |||
| 1861 | .copied() | 1874 | .copied() |
| 1862 | .unwrap_or_default(), | 1875 | .unwrap_or_default(), |
| 1863 | Composer::new( | 1876 | Composer::new( |
| 1864 | - self.meta.width, | 1877 | + canvas_w, |
| 1865 | - self.meta.height, | 1878 | + canvas_h, |
| 1866 | self.project.composite_fit_at(start), | 1879 | self.project.composite_fit_at(start), |
| 1867 | ), | 1880 | ), |
| 1868 | self.project.composite_visualizer_at(start), | 1881 | self.project.composite_visualizer_at(start), |
| @@ -1918,7 +1931,7 @@ impl PlaybackSession { | |||
| 1918 | // than kept in a local, because a `Composer` owns the scratch | 1931 | // than kept in a local, because a `Composer` owns the scratch |
| 1919 | // buffers it places through -- the value is not copied about. | 1932 | // buffers it places through -- the value is not copied about. |
| 1920 | let fit = self.project.composite_fit_at(start); | 1933 | let fit = self.project.composite_fit_at(start); |
| 1921 | - let canvas = || Composer::new(self.meta.width, self.meta.height, fit); | 1934 | + let canvas = || Composer::new(canvas_w, canvas_h, fit); |
| 1922 | // ...and the rendition an HDR source among them is mapped to, | 1935 | // ...and the rendition an HDR source among them is mapped to, |
| 1923 | // the project's own and constant across the span for that | 1936 | // the project's own and constant across the span for that |
| 1924 | // reason too. | 1937 | // reason too. |
| @@ -1975,8 +1988,8 @@ impl PlaybackSession { | |||
| 1975 | // span at all and gets one frame of it -- enough to put black on | 1988 | // span at all and gets one frame of it -- enough to put black on |
| 1976 | // screen, and it ends where the timeline does, at once. | 1989 | // screen, and it ends where the timeline does, at once. |
| 1977 | gap => Ok(DecodeSession::open_black( | 1990 | gap => Ok(DecodeSession::open_black( |
| 1978 | - self.meta.width, | 1991 | + canvas_w, |
| 1979 | - self.meta.height, | 1992 | + canvas_h, |
| 1980 | gap.map_or(1, |s| s.len), | 1993 | gap.map_or(1, |s| s.len), |
| 1981 | )), | 1994 | )), |
| 1982 | }; | 1995 | }; |
| @@ -2018,6 +2031,7 @@ impl PlaybackSession { | |||
| 2018 | /// (still inside A's span) -- the same asymmetry | 2031 | /// (still inside A's span) -- the same asymmetry |
| 2019 | /// [`crate::export`]'s own `Dissolve` is built with. | 2032 | /// [`crate::export`]'s own `Dissolve` is built with. |
| 2020 | fn open_dissolve_b(&mut self) { | 2033 | fn open_dissolve_b(&mut self) { |
| 2034 | + let (canvas_w, canvas_h) = self.view_size(); | ||
| 2021 | let Some(d) = &self.dissolve else { return }; | 2035 | let Some(d) = &self.dissolve else { return }; |
| 2022 | if d.b.is_some() { | 2036 | if d.b.is_some() { |
| 2023 | return; | 2037 | return; |
| @@ -2038,8 +2052,8 @@ impl PlaybackSession { | |||
| 2038 | .copied() | 2052 | .copied() |
| 2039 | .unwrap_or_default(); | 2053 | .unwrap_or_default(); |
| 2040 | let canvas = Composer::new( | 2054 | let canvas = Composer::new( |
| 2041 | - self.meta.width, | 2055 | + canvas_w, |
| 2042 | - self.meta.height, | 2056 | + canvas_h, |
| 2043 | self.project.composite_fit_at(b_start), | 2057 | self.project.composite_fit_at(b_start), |
| 2044 | ); | 2058 | ); |
| 2045 | let tone = self.project.tone(); | 2059 | let tone = self.project.tone(); |
| @@ -3567,6 +3581,55 @@ impl PlaybackSession { | |||
| 3567 | } | 3581 | } |
| 3568 | } | 3582 | } |
| 3569 | 3583 | ||
| 3584 | + /// Says how big the picture is on screen, in device pixels -- what a | ||
| 3585 | + /// front-end showing this session pushes once it knows where the picture | ||
| 3586 | + /// actually lands. Rendering a 1080p project's visualizer for a 600 px | ||
| 3587 | + /// preview costs nine times the pixels for nothing the eye can see; a | ||
| 3588 | + /// window that is fullscreen gets the project's own size back by pushing | ||
| 3589 | + /// it. `true` when the size moved enough to be worth a new picture: under | ||
| 3590 | + /// 8 px on both axes is a resize nobody can see, and a window being | ||
| 3591 | + /// dragged would otherwise restart the picture every tick. | ||
| 3592 | + pub fn set_view_size(&mut self, width: u32, height: u32) -> bool { | ||
| 3593 | + let want = self.view_want(width, height); | ||
| 3594 | + match self.view { | ||
| 3595 | + Some(now) if now.0.abs_diff(want.0) < 8 && now.1.abs_diff(want.1) < 8 => false, | ||
| 3596 | + _ => { | ||
| 3597 | + self.view = Some(want); | ||
| 3598 | + true | ||
| 3599 | + } | ||
| 3600 | + } | ||
| 3601 | + } | ||
| 3602 | + | ||
| 3603 | + /// [`Self::set_view_size`] with no deadband: what asks for the *exact* | ||
| 3604 | + /// size rather than one within 8 px of it. A still saved at the project's | ||
| 3605 | + /// own pixels is a file whose dimensions are read back, so "close enough | ||
| 3606 | + /// to be invisible" is not close enough to be right. `true` when the view | ||
| 3607 | + /// moved, `false` when it was already there -- which is also the answer | ||
| 3608 | + /// that lets a caller skip waiting for a frame it already has. | ||
| 3609 | + pub fn set_view_size_exact(&mut self, width: u32, height: u32) -> bool { | ||
| 3610 | + let want = self.view_want(width, height); | ||
| 3611 | + match self.view { | ||
| 3612 | + Some(now) if now == want => false, | ||
| 3613 | + _ => { | ||
| 3614 | + self.view = Some(want); | ||
| 3615 | + true | ||
| 3616 | + } | ||
| 3617 | + } | ||
| 3618 | + } | ||
| 3619 | + | ||
| 3620 | + /// The size a push of `(width, height)` settles on: even on both axes | ||
| 3621 | + /// (YUV 4:2:0 has no odd chroma) and never past the project's own. | ||
| 3622 | + fn view_want(&self, width: u32, height: u32) -> (u32, u32) { | ||
| 3623 | + let fit = |n: u32, cap: u32| (n & !1).clamp(2, cap.max(2)); | ||
| 3624 | + (fit(width, self.meta.width), fit(height, self.meta.height)) | ||
| 3625 | + } | ||
| 3626 | + | ||
| 3627 | + /// The canvas a span's pictures are painted at: [`Self::set_view_size`]'s | ||
| 3628 | + /// size once one has been pushed, the project's own otherwise. | ||
| 3629 | + pub fn view_size(&self) -> (u32, u32) { | ||
| 3630 | + self.view.unwrap_or((self.meta.width, self.meta.height)) | ||
| 3631 | + } | ||
| 3632 | + | ||
| 3570 | /// Current timeline position in seconds. | 3633 | /// Current timeline position in seconds. |
| 3571 | pub fn now(&self) -> f64 { | 3634 | pub fn now(&self) -> f64 { |
| 3572 | self.clock.now() | 3635 | self.clock.now() |
| @@ -4453,6 +4516,41 @@ mod tests { | |||
| 4453 | assert_eq!(blend_bgra(&a, &b, 0.5), [60, 70, 80, 255]); | 4516 | assert_eq!(blend_bgra(&a, &b, 0.5), [60, 70, 80, 255]); |
| 4454 | } | 4517 | } |
| 4455 | 4518 | ||
| 4519 | + /// A still wants the project's own pixels, not a view near them. | ||
| 4520 | + /// [`PlaybackSession::set_view_size`] calls under 8 px no change at all (a | ||
| 4521 | + /// window being dragged must not restart the picture every tick), so a | ||
| 4522 | + /// caller that means "paint at the project's size" has to say it through | ||
| 4523 | + /// [`PlaybackSession::set_view_size_exact`] -- and read its `false` as "the | ||
| 4524 | + /// view is already there, the frame in hand is the one wanted", which is | ||
| 4525 | + /// what stops it waiting for a picture nothing will reseek into existence. | ||
| 4526 | + #[test] | ||
| 4527 | + fn a_still_gets_the_exact_view_size_where_a_push_settles_for_near() { | ||
| 4528 | + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | ||
| 4529 | + assert!(session.set_resolution(1920, 1080)); | ||
| 4530 | + let (w, h) = session.resolution(); | ||
| 4531 | + assert_eq!((w, h), (1920, 1080)); | ||
| 4532 | + // A preview-sized push, then the ask a still makes: two pixels short on | ||
| 4533 | + // each axis, which the deadbanded push calls no change at all. | ||
| 4534 | + assert!(session.set_view_size(1918, 1078)); | ||
| 4535 | + assert!( | ||
| 4536 | + std::ops::Not::not(session.set_view_size(w, h)), | ||
| 4537 | + "the push's deadband is exactly what a still cannot use" | ||
| 4538 | + ); | ||
| 4539 | + assert_eq!( | ||
| 4540 | + session.view_size(), | ||
| 4541 | + (1918, 1078), | ||
| 4542 | + "the push left the view near, not at, full -- so a still written now would be short" | ||
| 4543 | + ); | ||
| 4544 | + assert!( | ||
| 4545 | + session.set_view_size_exact(w, h), | ||
| 4546 | + "the exact setter must move a view the push called close enough" | ||
| 4547 | + ); | ||
| 4548 | + assert_eq!(session.view_size(), (w, h)); | ||
| 4549 | + // ...and at the size already it says so rather than repainting: that | ||
| 4550 | + // `false` is the caller's "write the frame you have". | ||
| 4551 | + assert!(std::ops::Not::not(session.set_view_size_exact(w, h))); | ||
| 4552 | + } | ||
| 4553 | + | ||
| 4456 | /// The dissolve window's own edges, in the units [`PlaybackSession::start_span`] | 4554 | /// The dissolve window's own edges, in the units [`PlaybackSession::start_span`] |
| 4457 | /// computes them in: a `tail_start` of `span.start + span.len - window` | 4555 | /// computes them in: a `tail_start` of `span.start + span.len - window` |
| 4458 | /// puts the very first output frame of the window at `idx_in_window == 0` | 4556 | /// puts the very first output frame of the window at `idx_in_window == 0` |