Merge branch 'worktree-agent-a7066fb8aeac02d29'
Browse files at this commit7 changed files +656 -25
- crates/app/src/keymap.rs +35 -5
- crates/app/src/main.rs +310 -2
- crates/engine/src/decode.rs +29 -10
- crates/engine/src/export.rs +25 -0
- crates/engine/src/playback.rs +43 -3
- crates/engine/src/project.rs +31 -5
- crates/engine/tests/color.rs +183 -0
crates/app/src/keymap.rsmodified+35 -5
| @@ -47,6 +47,7 @@ pub enum ActionId { | ||
| 47 | 47 | Regroup, |
| 48 | 48 | Delete, |
| 49 | 49 | Lift, |
| 50 | + Color, | |
| 50 | 51 | Undo, |
| 51 | 52 | AddVideoLane, |
| 52 | 53 | AddAudioLane, |
| @@ -60,7 +61,7 @@ pub enum ActionId { | ||
| 60 | 61 | impl ActionId { |
| 61 | 62 | /// Display order everywhere -- the editor lists them in it and |
| 62 | 63 | /// [`Keymap::defaults`] binds them in it. |
| 63 | - pub const ALL: [ActionId; 17] = [ | |
| 64 | + pub const ALL: [ActionId; 18] = [ | |
| 64 | 65 | ActionId::Play, |
| 65 | 66 | ActionId::Export, |
| 66 | 67 | ActionId::Save, |
| @@ -70,6 +71,7 @@ impl ActionId { | ||
| 70 | 71 | ActionId::Regroup, |
| 71 | 72 | ActionId::Delete, |
| 72 | 73 | ActionId::Lift, |
| 74 | + ActionId::Color, | |
| 73 | 75 | ActionId::Undo, |
| 74 | 76 | ActionId::AddVideoLane, |
| 75 | 77 | ActionId::AddAudioLane, |
| @@ -92,6 +94,7 @@ impl ActionId { | ||
| 92 | 94 | ActionId::Regroup => "Regroup", |
| 93 | 95 | ActionId::Delete => "Delete", |
| 94 | 96 | ActionId::Lift => "Lift (leave a gap)", |
| 97 | + ActionId::Color => "Colour…", | |
| 95 | 98 | ActionId::Undo => "Undo", |
| 96 | 99 | ActionId::AddVideoLane => "Add a video track", |
| 97 | 100 | ActionId::AddAudioLane => "Add an audio track", |
| @@ -116,6 +119,7 @@ impl ActionId { | ||
| 116 | 119 | ActionId::Regroup => "regroup", |
| 117 | 120 | ActionId::Delete => "delete", |
| 118 | 121 | ActionId::Lift => "lift", |
| 122 | + ActionId::Color => "color", | |
| 119 | 123 | ActionId::Undo => "undo", |
| 120 | 124 | ActionId::AddVideoLane => "add-video-lane", |
| 121 | 125 | ActionId::AddAudioLane => "add-audio-lane", |
| @@ -137,7 +141,11 @@ impl ActionId { | ||
| 137 | 141 | pub fn category(self) -> Category { |
| 138 | 142 | match self { |
| 139 | 143 | ActionId::Play => Category::Playback, |
| 140 | - ActionId::Copy | ActionId::Paste | ActionId::Delete | ActionId::Lift => Category::Clips, | |
| 144 | + ActionId::Copy | |
| 145 | + | ActionId::Paste | |
| 146 | + | ActionId::Delete | |
| 147 | + | ActionId::Lift | |
| 148 | + | ActionId::Color => Category::Clips, | |
| 141 | 149 | ActionId::Cut |
| 142 | 150 | | ActionId::Regroup |
| 143 | 151 | | ActionId::Undo |
| @@ -198,7 +206,7 @@ pub struct Fixed { | ||
| 198 | 206 | pub category: Category, |
| 199 | 207 | } |
| 200 | 208 | |
| 201 | -pub const FIXED: [Fixed; 9] = [ | |
| 209 | +pub const FIXED: [Fixed; 12] = [ | |
| 202 | 210 | Fixed { |
| 203 | 211 | chord: "esc", |
| 204 | 212 | label: "Close this card or clip menu, or cancel a capture", |
| @@ -247,6 +255,23 @@ pub const FIXED: [Fixed; 9] = [ | ||
| 247 | 255 | label: "Flatten every band", |
| 248 | 256 | category: Category::Audio, |
| 249 | 257 | }, |
| 258 | + // The colour card's own three, which mean nothing outside it -- the same | |
| 259 | + // card-local input the export card's digits are. | |
| 260 | + Fixed { | |
| 261 | + chord: "↑ / ↓", | |
| 262 | + label: "Pick a colour slider", | |
| 263 | + category: Category::Clips, | |
| 264 | + }, | |
| 265 | + Fixed { | |
| 266 | + chord: "← / →", | |
| 267 | + label: "Move the picked colour slider", | |
| 268 | + category: Category::Clips, | |
| 269 | + }, | |
| 270 | + Fixed { | |
| 271 | + chord: "r", | |
| 272 | + label: "Take the colour grade off the clip", | |
| 273 | + category: Category::Clips, | |
| 274 | + }, | |
| 250 | 275 | ]; |
| 251 | 276 | |
| 252 | 277 | /// A stroke as the key handler sees it: gpui's key name plus the one modifier |
| @@ -350,6 +375,10 @@ impl Keymap { | ||
| 350 | 375 | b(ActionId::Delete, "x", false), |
| 351 | 376 | b(ActionId::Delete, "delete", false), |
| 352 | 377 | b(ActionId::Lift, "l", false), |
| 378 | + // The c of colour is Cut and ctrl+c is Copy, so the grade takes | |
| 379 | + // k: free, next to nothing that edits, and one press like the | |
| 380 | + // rest of the clip keys. | |
| 381 | + b(ActionId::Color, "k", false), | |
| 353 | 382 | b(ActionId::Undo, "z", false), |
| 354 | 383 | b(ActionId::Undo, "z", true), |
| 355 | 384 | // The unshifted initials of what they add. Both were free -- |
| @@ -592,7 +621,7 @@ mod tests { | ||
| 592 | 621 | #[test] |
| 593 | 622 | fn every_default_stroke_reaches_its_action() { |
| 594 | 623 | let k = Keymap::defaults(); |
| 595 | - assert_eq!(k.entries().len(), 19); | |
| 624 | + assert_eq!(k.entries().len(), 20); | |
| 596 | 625 | assert_eq!(k.lookup("space", false), Some(ActionId::Play)); |
| 597 | 626 | assert_eq!(k.lookup("e", false), Some(ActionId::Export)); |
| 598 | 627 | assert_eq!(k.lookup("s", true), Some(ActionId::Save)); |
| @@ -603,6 +632,7 @@ mod tests { | ||
| 603 | 632 | assert_eq!(k.lookup("x", false), Some(ActionId::Delete)); |
| 604 | 633 | assert_eq!(k.lookup("delete", false), Some(ActionId::Delete)); |
| 605 | 634 | assert_eq!(k.lookup("l", false), Some(ActionId::Lift)); |
| 635 | + assert_eq!(k.lookup("k", false), Some(ActionId::Color)); | |
| 606 | 636 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); |
| 607 | 637 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); |
| 608 | 638 | // The track keys are the bare letters; the ctrl ones stay copy/paste. |
| @@ -743,7 +773,7 @@ mod tests { | ||
| 743 | 773 | ); |
| 744 | 774 | // The label is the editor's column, and never the file's word for it. |
| 745 | 775 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); |
| 746 | - assert_eq!(ActionId::ALL.len(), 17); | |
| 776 | + assert_eq!(ActionId::ALL.len(), 18); | |
| 747 | 777 | } |
| 748 | 778 | |
| 749 | 779 | #[test] |
crates/app/src/main.rsmodified+310 -2
| @@ -10,6 +10,7 @@ use std::sync::Arc; | ||
| 10 | 10 | use std::time::{Duration, Instant}; |
| 11 | 11 | |
| 12 | 12 | use engine::audio::StreamInfo; |
| 13 | +use engine::color::ColorParams; | |
| 13 | 14 | use engine::eq::{Band, BandKind, EqParams}; |
| 14 | 15 | use engine::export::{ExportSettings, Format}; |
| 15 | 16 | use engine::project::{Lane, LaneKind, Source}; |
| @@ -220,12 +221,13 @@ struct ContextMenu { | ||
| 220 | 221 | /// action a stroke already reaches -- the menu is a second way *to* the actions |
| 221 | 222 | /// and never a second version of them -- so both the label and the hint come |
| 222 | 223 | /// out of the keymap registry and the two can never disagree. |
| 223 | -const MENU_ITEMS: [ActionId; 6] = [ | |
| 224 | +const MENU_ITEMS: [ActionId; 7] = [ | |
| 224 | 225 | ActionId::Cut, |
| 225 | 226 | ActionId::Delete, |
| 226 | 227 | ActionId::Lift, |
| 227 | 228 | ActionId::Regroup, |
| 228 | 229 | ActionId::Equalizer, |
| 230 | + ActionId::Color, | |
| 229 | 231 | ActionId::ToggleMute, |
| 230 | 232 | ]; |
| 231 | 233 | |
| @@ -242,6 +244,27 @@ const EQ_STEP: f32 = 1.; | ||
| 242 | 244 | /// The gain bar's height inside a band row. |
| 243 | 245 | const EQ_BAR_H: f32 = 6.; |
| 244 | 246 | |
| 247 | +/// The colour card's four controls, in the order it lists them: what each is | |
| 248 | +/// called and the range it moves in. The order is `ColorParams`' own, which is | |
| 249 | +/// what [`color_band`] indexes. | |
| 250 | +const COLOR_BANDS: [(&str, f32, f32); 4] = [ | |
| 251 | + ("Brightness", -1., 1.), | |
| 252 | + ("Contrast", 0., 2.), | |
| 253 | + ("Saturation", 0., 2.), | |
| 254 | + ("Tint (cool–warm)", -1., 1.), | |
| 255 | +]; | |
| 256 | + | |
| 257 | +/// A press of a nudge key or button: a fortieth of a band's range, so a slider | |
| 258 | +/// crosses it in forty presses and every stop is a number the file can write. | |
| 259 | +const COLOR_STEP: f32 = 0.05; | |
| 260 | + | |
| 261 | +/// The card's width: a slider row is a label, two buttons, the bar and the | |
| 262 | +/// value, and the longest label has to fit beside all four without truncating | |
| 263 | +/// (measured against "Tint (cool–warm)" at 460). | |
| 264 | +const COLOR_W: f32 = 460.; | |
| 265 | +/// How much of a slider row the bar itself gets. | |
| 266 | +const COLOR_BAR_W: f32 = 140.; | |
| 267 | + | |
| 245 | 268 | struct Player { |
| 246 | 269 | /// The timeline, once there is one. A run with no file opens without it and |
| 247 | 270 | /// waits: the first media import or project load is what fills it, and |
| @@ -356,6 +379,13 @@ struct Player { | ||
| 356 | 379 | /// they are laid out identically and only the *x* of a click is read |
| 357 | 380 | /// ([`frac_along`]), so whichever row painted last answers for all of them. |
| 358 | 381 | eq_bar: Rc<Cell<Bounds<Pixels>>>, |
| 382 | + /// The colour card is up on this clip -- the lane it is on and its index | |
| 383 | + /// there. `None` when it is closed, which is the only place that state | |
| 384 | + /// lives: the grade itself is the project's. | |
| 385 | + color_open: Option<(Lane, usize)>, | |
| 386 | + /// Which of the card's four sliders the arrow keys and the buttons move. | |
| 387 | + /// The card's own focus, since nothing in it takes gpui's (ledger:182). | |
| 388 | + color_band: usize, | |
| 359 | 389 | /// The action whose row is waiting for a stroke. The next key that is |
| 360 | 390 | /// neither escape nor a lone modifier becomes the whole of what reaches it. |
| 361 | 391 | rebinding: Option<ActionId>, |
| @@ -466,6 +496,7 @@ impl Player { | ||
| 466 | 496 | ActionId::Regroup => self.regroup(cx), |
| 467 | 497 | ActionId::Delete => self.delete_selected(cx), |
| 468 | 498 | ActionId::Lift => self.lift_selected(cx), |
| 499 | + ActionId::Color => self.open_color(cx), | |
| 469 | 500 | ActionId::Undo => self.undo(cx), |
| 470 | 501 | ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx), |
| 471 | 502 | ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx), |
| @@ -479,11 +510,87 @@ impl Player { | ||
| 479 | 510 | } |
| 480 | 511 | } |
| 481 | 512 | |
| 513 | + /// Opens the colour card on the clip a grade would go on: the clip that was | |
| 514 | + /// clicked when it is a video one, and otherwise the clip the picture is | |
| 515 | + /// coming from -- the one the engine's own compositing rule picks, which is | |
| 516 | + /// what a person means by "this shot". That fallback is also what makes the | |
| 517 | + /// card reachable with no pointer at all: clip selection is a click and | |
| 518 | + /// nothing else (ledger:182). | |
| 519 | + fn open_color(&mut self, cx: &mut Context<Self>) { | |
| 520 | + if self.exporting().is_some() { | |
| 521 | + return; | |
| 522 | + } | |
| 523 | + let Some(session) = &self.session else { | |
| 524 | + self.notice = Some("no timeline to grade — open a file first".into()); | |
| 525 | + cx.notify(); | |
| 526 | + return; | |
| 527 | + }; | |
| 528 | + let target = self | |
| 529 | + .selected | |
| 530 | + .filter(|(lane, _)| lane.kind == LaneKind::Video) | |
| 531 | + .or_else(|| session.video_clip_at(session.now())); | |
| 532 | + match target { | |
| 533 | + Some(clip) => { | |
| 534 | + self.color_open = Some(clip); | |
| 535 | + self.color_band = 0; | |
| 536 | + // One card at a time, the rule both the others already follow. | |
| 537 | + self.keys_open = false; | |
| 538 | + self.export_open = false; | |
| 539 | + self.context_menu = None; | |
| 540 | + } | |
| 541 | + None => self.notice = Some("no clip under the playhead to grade".into()), | |
| 542 | + } | |
| 543 | + cx.notify(); | |
| 544 | + } | |
| 545 | + | |
| 546 | + /// What the card's clip is graded by right now -- the identity for one | |
| 547 | + /// nobody has graded, which is what the sliders start at. | |
| 548 | + fn color_params(&self) -> ColorParams { | |
| 549 | + self.color_open | |
| 550 | + .zip(self.session.as_ref()) | |
| 551 | + .and_then(|((lane, idx), session)| session.color_of(lane, idx).copied()) | |
| 552 | + .unwrap_or_default() | |
| 553 | + } | |
| 554 | + | |
| 555 | + /// Puts `params` on the card's clip, or takes the grade off when they are | |
| 556 | + /// the identity -- a slider walked back to the middle leaves the clip | |
| 557 | + /// ungraded rather than carrying a do-nothing entry, which is what keeps an | |
| 558 | + /// untouched project byte-identical. The engine reseeks on the edit, so the | |
| 559 | + /// frame on screen repaints through the new grade; this only owes the flags | |
| 560 | + /// that reseek clears. | |
| 561 | + fn set_color(&mut self, params: ColorParams, cx: &mut Context<Self>) { | |
| 562 | + let Some((lane, idx)) = self.color_open else { | |
| 563 | + return; | |
| 564 | + }; | |
| 565 | + let Some(session) = &mut self.session else { | |
| 566 | + return; | |
| 567 | + }; | |
| 568 | + let grade = Some(params).filter(|p| !p.is_identity()); | |
| 569 | + if session.set_color(lane, idx, grade) { | |
| 570 | + self.reset_after_reseek(); | |
| 571 | + } | |
| 572 | + cx.notify(); | |
| 573 | + } | |
| 574 | + | |
| 575 | + /// Moves the picked slider by `steps` of [`COLOR_STEP`], clamped to that | |
| 576 | + /// band's range. One edit, so one undo step per press. | |
| 577 | + fn nudge_color(&mut self, steps: f32, cx: &mut Context<Self>) { | |
| 578 | + let mut params = self.color_params(); | |
| 579 | + let (_, low, high) = COLOR_BANDS[self.color_band]; | |
| 580 | + let value = band_mut(&mut params, self.color_band); | |
| 581 | + *value = (*value + steps * COLOR_STEP).clamp(low, high); | |
| 582 | + self.set_color(params, cx); | |
| 583 | + } | |
| 584 | + | |
| 482 | 585 | /// Whether a card owns the window. While one does the timeline under it is |
| 483 | 586 | /// out of reach, so a right-click there opens no menu -- the same rule the |
| 484 | 587 | /// key handler and the drop target already follow. |
| 485 | 588 | fn modal(&self) -> bool { |
| 486 | - self.keys_open || self.export_open || self.eq_open.is_some() || self.exporting().is_some() | |
| 589 | + self.keys_open | |
| 590 | + || self.export_open | |
| 591 | + || self.eq_open.is_some() | |
| 592 | + || self.color_open.is_some() | |
| 593 | + || self.exporting().is_some() | |
| 487 | 594 | } |
| 488 | 595 | |
| 489 | 596 | /// Opens the equalizer on the selected clip. Audio only, and it says so |
| @@ -1423,6 +1530,26 @@ impl Render for Player { | ||
| 1423 | 1530 | cx.notify(); |
| 1424 | 1531 | return; |
| 1425 | 1532 | } |
| 1533 | + // The colour card owns the keyboard the same way the export | |
| 1534 | + // card does, and its keys mean nothing outside it: the arrows | |
| 1535 | + // pick a slider and move it, and `r` takes the grade off. Not | |
| 1536 | + // keymap bindings for exactly that reason -- see `FIXED`, where | |
| 1537 | + // the keys menu still lists them. | |
| 1538 | + if this.color_open.is_some() { | |
| 1539 | + match color_key(key) { | |
| 1540 | + Some(ColorKey::Close) => this.color_open = None, | |
| 1541 | + Some(ColorKey::Band(step)) => { | |
| 1542 | + this.color_band = (this.color_band + step) % COLOR_BANDS.len(); | |
| 1543 | + } | |
| 1544 | + Some(ColorKey::Nudge(steps)) => this.nudge_color(steps, cx), | |
| 1545 | + Some(ColorKey::Reset) => { | |
| 1546 | + this.set_color(ColorParams::default(), cx); | |
| 1547 | + } | |
| 1548 | + None => {} | |
| 1549 | + } | |
| 1550 | + cx.notify(); | |
| 1551 | + return; | |
| 1552 | + } | |
| 1426 | 1553 | // A clip menu names an index, and every edit below moves |
| 1427 | 1554 | // indices -- so a stroke closes it before it acts. Escape means |
| 1428 | 1555 | // that and nothing else, which is the `esc` the keys menu |
| @@ -1573,6 +1700,7 @@ impl Render for Player { | ||
| 1573 | 1700 | .children(self.keys_overlay(cx)) |
| 1574 | 1701 | .children(self.export_card(cx)) |
| 1575 | 1702 | .children(self.eq_card(cx)) |
| 1703 | + .children(self.color_card(cx)) | |
| 1576 | 1704 | } |
| 1577 | 1705 | } |
| 1578 | 1706 | |
| @@ -2599,6 +2727,141 @@ impl Player { | ||
| 2599 | 2727 | ) |
| 2600 | 2728 | } |
| 2601 | 2729 | |
| 2730 | + /// The colour card: a row per control, each showing where in its range it | |
| 2731 | + /// stands and carrying the pair of buttons that move it -- the same move the | |
| 2732 | + /// arrow keys make, because a control only the pointer can reach is not one | |
| 2733 | + /// everyone can reach. Same scrim, surface and row shape as the other two | |
| 2734 | + /// cards, and the same plain divs, so the root keeps the keyboard. | |
| 2735 | + /// | |
| 2736 | + /// The values are read from the project every render: what is drawn is what | |
| 2737 | + /// the decoder is grading with, never a copy that could drift from it. | |
| 2738 | + fn color_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { | |
| 2739 | + let (lane, idx) = self.color_open?; | |
| 2740 | + let params = self.color_params(); | |
| 2741 | + let rows: Vec<_> = COLOR_BANDS | |
| 2742 | + .iter() | |
| 2743 | + .enumerate() | |
| 2744 | + .map(|(i, &(label, low, high))| { | |
| 2745 | + let mut read = params; | |
| 2746 | + let value = *band_mut(&mut read, i); | |
| 2747 | + let frac = ((value - low) / (high - low)).clamp(0., 1.); | |
| 2748 | + let picked = i == self.color_band; | |
| 2749 | + // Both buttons of a row do what the arrows do, so there is one | |
| 2750 | + // rule for what a press is worth and one place it is clamped. | |
| 2751 | + let step = |name: &'static str, steps: f32| { | |
| 2752 | + div() | |
| 2753 | + .id((name, i)) | |
| 2754 | + .w(px(CONTROL_H)) | |
| 2755 | + .h(px(CONTROL_H)) | |
| 2756 | + .flex() | |
| 2757 | + .items_center() | |
| 2758 | + .justify_center() | |
| 2759 | + .rounded(px(3.)) | |
| 2760 | + .bg(rgb(CHROME)) | |
| 2761 | + .cursor_pointer() | |
| 2762 | + .hover(|s| s.bg(rgb(HOVER))) | |
| 2763 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | |
| 2764 | + this.color_band = i; | |
| 2765 | + this.nudge_color(steps, cx); | |
| 2766 | + })) | |
| 2767 | + .child(if steps < 0. { "–" } else { "+" }) | |
| 2768 | + }; | |
| 2769 | + div() | |
| 2770 | + .id(("color-row", i)) | |
| 2771 | + .flex() | |
| 2772 | + .min_h(px(KEYS_ROW_H)) | |
| 2773 | + .items_center() | |
| 2774 | + .gap(px(8.)) | |
| 2775 | + .px(px(6.)) | |
| 2776 | + .rounded(px(3.)) | |
| 2777 | + .cursor_pointer() | |
| 2778 | + .when(picked, |d| d.bg(rgb(SELECTED))) | |
| 2779 | + .hover(|s| s.bg(rgb(HOVER))) | |
| 2780 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | |
| 2781 | + this.color_band = i; | |
| 2782 | + cx.notify(); | |
| 2783 | + })) | |
| 2784 | + .child(div().flex_1().min_w(px(0.)).truncate().child(label)) | |
| 2785 | + .child(step("color-down", -1.)) | |
| 2786 | + .child( | |
| 2787 | + div() | |
| 2788 | + .w(px(COLOR_BAR_W)) | |
| 2789 | + .h(px(4.)) | |
| 2790 | + .rounded(px(2.)) | |
| 2791 | + .bg(rgb(CHROME)) | |
| 2792 | + .child( | |
| 2793 | + div() | |
| 2794 | + .h_full() | |
| 2795 | + .w(relative(frac)) | |
| 2796 | + .rounded(px(2.)) | |
| 2797 | + .bg(rgb(ACCENT)), | |
| 2798 | + ), | |
| 2799 | + ) | |
| 2800 | + .child(step("color-up", 1.)) | |
| 2801 | + .child( | |
| 2802 | + div() | |
| 2803 | + .w(px(44.)) | |
| 2804 | + .text_size(px(11.)) | |
| 2805 | + .text_color(rgb(INK_DIM)) | |
| 2806 | + .child(format!("{value:.2}")), | |
| 2807 | + ) | |
| 2808 | + }) | |
| 2809 | + .collect(); | |
| 2810 | + Some( | |
| 2811 | + div() | |
| 2812 | + .absolute() | |
| 2813 | + .inset_0() | |
| 2814 | + .flex() | |
| 2815 | + .justify_center() | |
| 2816 | + .items_center() | |
| 2817 | + .bg(rgba(0x101010cc)) | |
| 2818 | + .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { | |
| 2819 | + cx.stop_propagation() | |
| 2820 | + }) | |
| 2821 | + .child( | |
| 2822 | + div() | |
| 2823 | + .w(px(COLOR_W)) | |
| 2824 | + .flex() | |
| 2825 | + .flex_col() | |
| 2826 | + .gap(px(2.)) | |
| 2827 | + .p(px(12.)) | |
| 2828 | + .rounded(px(6.)) | |
| 2829 | + .bg(rgb(SURFACE)) | |
| 2830 | + .child(div().flex_none().px(px(6.)).child(format!( | |
| 2831 | + "Colour — {} clip {}", | |
| 2832 | + lane.label(), | |
| 2833 | + idx + 1 | |
| 2834 | + ))) | |
| 2835 | + .child( | |
| 2836 | + div() | |
| 2837 | + .flex_none() | |
| 2838 | + .px(px(6.)) | |
| 2839 | + .text_size(px(11.)) | |
| 2840 | + .text_color(rgb(INK_DIM)) | |
| 2841 | + .child("↑↓ picks a slider, ←→ moves it, r resets — esc closes"), | |
| 2842 | + ) | |
| 2843 | + .children(rows) | |
| 2844 | + .child( | |
| 2845 | + div() | |
| 2846 | + .id("color-reset") | |
| 2847 | + .mt(px(4.)) | |
| 2848 | + .flex() | |
| 2849 | + .h(px(CONTROL_H)) | |
| 2850 | + .items_center() | |
| 2851 | + .justify_center() | |
| 2852 | + .rounded(px(3.)) | |
| 2853 | + .bg(rgb(SELECTED)) | |
| 2854 | + .cursor_pointer() | |
| 2855 | + .hover(|s| s.bg(rgb(HOVER))) | |
| 2856 | + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { | |
| 2857 | + this.set_color(ColorParams::default(), cx); | |
| 2858 | + })) | |
| 2859 | + .child("Reset"), | |
| 2860 | + ), | |
| 2861 | + ), | |
| 2862 | + ) | |
| 2863 | + } | |
| 2864 | + | |
| 2602 | 2865 | /// The menu a right-click on a clip opens: what that clip can be given, |
| 2603 | 2866 | /// each item beside the stroke that does the very same thing, and a |
| 2604 | 2867 | /// turn-over side that says what the clip *is*. An item that would do |
| @@ -3232,6 +3495,9 @@ fn applicable(clip: &Clip, lane: Lane, action: ActionId, playhead: u32) -> bool | ||
| 3232 | 3495 | // The equalizer filters samples, and a video clip has none of its own |
| 3233 | 3496 | // here: the sound is the audio lane's, clip for clip. |
| 3234 | 3497 | ActionId::Equalizer => lane.kind == LaneKind::Audio, |
| 3498 | + // A grade is a picture setting and an audio clip has no picture: the | |
| 3499 | + // item is there on every clip, and dimmed where it would mean nothing. | |
| 3500 | + ActionId::Color => lane.kind == LaneKind::Video, | |
| 3235 | 3501 | // Splits this clip only from inside it: at either edge there is nothing |
| 3236 | 3502 | // to split off (project.rs `splittable`). |
| 3237 | 3503 | ActionId::Cut => clip.start < playhead && playhead < clip.end(), |
| @@ -3522,6 +3788,42 @@ fn format_key(key: &str) -> Option<Format> { | ||
| 3522 | 3788 | .map(|(format, _)| format) |
| 3523 | 3789 | } |
| 3524 | 3790 | |
| 3791 | +/// What one of the colour card's own strokes does. Its keys are card-local -- | |
| 3792 | +/// they mean nothing outside it -- so they are a table here rather than keymap | |
| 3793 | +/// bindings, exactly as the export card's format initials are. Listed in | |
| 3794 | +/// `keymap::FIXED` all the same, which is how the keys menu still says so. | |
| 3795 | +enum ColorKey { | |
| 3796 | + Close, | |
| 3797 | + /// Steps down the four sliders, wrapping. | |
| 3798 | + Band(usize), | |
| 3799 | + /// Moves the picked slider, in [`COLOR_STEP`]s. | |
| 3800 | + Nudge(f32), | |
| 3801 | + Reset, | |
| 3802 | +} | |
| 3803 | + | |
| 3804 | +fn color_key(key: &str) -> Option<ColorKey> { | |
| 3805 | + Some(match key { | |
| 3806 | + ESCAPE => ColorKey::Close, | |
| 3807 | + "down" => ColorKey::Band(1), | |
| 3808 | + "up" => ColorKey::Band(COLOR_BANDS.len() - 1), | |
| 3809 | + "right" => ColorKey::Nudge(1.), | |
| 3810 | + "left" => ColorKey::Nudge(-1.), | |
| 3811 | + "r" => ColorKey::Reset, | |
| 3812 | + _ => return None, | |
| 3813 | + }) | |
| 3814 | +} | |
| 3815 | + | |
| 3816 | +/// The band'th control of a grade, to read or to write. The order is | |
| 3817 | +/// [`COLOR_BANDS`]', which is the order the card lists them in. | |
| 3818 | +fn band_mut(params: &mut ColorParams, band: usize) -> &mut f32 { | |
| 3819 | + match band { | |
| 3820 | + 0 => &mut params.brightness, | |
| 3821 | + 1 => &mut params.contrast, | |
| 3822 | + 2 => &mut params.saturation, | |
| 3823 | + _ => &mut params.tint, | |
| 3824 | + } | |
| 3825 | +} | |
| 3826 | + | |
| 3525 | 3827 | /// The line under the rows: what the picked format really writes, in the terms |
| 3526 | 3828 | /// a file is judged by afterwards. |
| 3527 | 3829 | fn format_line(format: Format) -> &'static str { |
| @@ -4202,6 +4504,10 @@ mod tests { | ||
| 4202 | 4504 | assert!(applicable(&clip, v1, action, 0)); |
| 4203 | 4505 | assert!(applicable(&clip, a1, action, 60)); |
| 4204 | 4506 | } |
| 4507 | + // Except the grade, which is a picture setting: offered on a video | |
| 4508 | + // clip wherever the playhead is, dimmed on a waveform. | |
| 4509 | + assert!(applicable(&clip, v1, ActionId::Color, 0)); | |
| 4510 | + assert!(!applicable(&clip, a1, ActionId::Color, 60)); | |
| 4205 | 4511 | // The playhead frame is the engine's own rule, boundary included. |
| 4206 | 4512 | assert_eq!(frame_at(1.0, 30.), 30); |
| 4207 | 4513 | assert_eq!(frame_at(0.0, 30.), 0); |
| @@ -5291,6 +5597,8 @@ fn main() { | ||
| 5291 | 5597 | eq_band: 0, |
| 5292 | 5598 | eq_dragging: false, |
| 5293 | 5599 | eq_bar: Rc::default(), |
| 5600 | + color_open: None, | |
| 5601 | + color_band: 0, | |
| 5294 | 5602 | // What an export is until someone says otherwise: the |
| 5295 | 5603 | // bitrate the picture asks for. |
| 5296 | 5604 | quality: Quality::Auto, |
crates/engine/src/decode.rsmodified+29 -10
| @@ -9,7 +9,8 @@ use std::thread; | ||
| 9 | 9 | |
| 10 | 10 | use rusty_h264::Decoder; |
| 11 | 11 | |
| 12 | -use crate::convert::i420_to_bgra; | |
| 12 | +use crate::color::ColorParams; | |
| 13 | +use crate::convert::i420_to_bgra_with; | |
| 13 | 14 | use crate::demux::{Codec, Demuxer, VideoMeta}; |
| 14 | 15 | use crate::hw::HwSession; |
| 15 | 16 | |
| @@ -119,7 +120,10 @@ impl DecodeSession { | ||
| 119 | 120 | start_frame: u32, |
| 120 | 121 | end_frame: u32, |
| 121 | 122 | ) -> crate::Result<(VideoMeta, Receiver<Frame>, Arc<AtomicBool>)> { |
| 122 | - let (meta, stream) = Self::open_worker(path, start_frame, end_frame)?; | |
| 123 | + // Ungraded: the public API opens a *file*, and a colour grade belongs to | |
| 124 | + // a clip of a timeline. `PlaybackSession` is what has one to pass. | |
| 125 | + let (meta, stream) = | |
| 126 | + Self::open_worker(path, start_frame, end_frame, ColorParams::default())?; | |
| 123 | 127 | // The public API hands out the flag alone, so the worker is detached and |
| 124 | 128 | // the receiver goes to the caller: nothing here joins anything. |
| 125 | 129 | Ok((meta, stream.frames, stream.worker.detach())) |
| @@ -172,12 +176,20 @@ impl DecodeSession { | ||
| 172 | 176 | } |
| 173 | 177 | |
| 174 | 178 | /// As [`DecodeSession::open_range`], but the caller gets the whole |
| 175 | - /// [`Worker`] and can therefore *wait* for it. In-process only: a caller | |
| 176 | - /// that outlives its workers has to be able to join them at exit. | |
| 179 | + /// [`Worker`] and can therefore *wait* for it, and the range is graded by | |
| 180 | + /// `color` -- the clip's own setting, folded into the conversion the frames | |
| 181 | + /// go through anyway (see [`i420_to_bgra_with`]). It is captured here rather | |
| 182 | + /// than read per frame because a worker *is* one clip's range: a grade | |
| 183 | + /// changed while this one runs reaches the picture when the session reseeks | |
| 184 | + /// onto the new one, which every edit does. | |
| 185 | + /// | |
| 186 | + /// In-process only: a caller that outlives its workers has to be able to | |
| 187 | + /// join them at exit. | |
| 177 | 188 | pub(crate) fn open_worker( |
| 178 | 189 | path: impl AsRef<Path>, |
| 179 | 190 | start_frame: u32, |
| 180 | 191 | end_frame: u32, |
| 192 | + color: ColorParams, | |
| 181 | 193 | ) -> crate::Result<(VideoMeta, FrameStream)> { |
| 182 | 194 | let path = path.as_ref().to_path_buf(); |
| 183 | 195 | let (meta, demuxer) = Demuxer::open(&path)?; |
| @@ -228,7 +240,7 @@ impl DecodeSession { | ||
| 228 | 240 | return; |
| 229 | 241 | } |
| 230 | 242 | eprintln!("decode backend: hardware (VA-API plugin)"); |
| 231 | - if run_hw(hw, &tx, start_frame, end_frame, &worker_cancel) { | |
| 243 | + if run_hw(hw, &tx, start_frame, end_frame, &color, &worker_cancel) { | |
| 232 | 244 | return; |
| 233 | 245 | } |
| 234 | 246 | // A driver that opens but cannot decode a single frame is |
| @@ -242,7 +254,7 @@ impl DecodeSession { | ||
| 242 | 254 | return; |
| 243 | 255 | } |
| 244 | 256 | eprintln!("decode backend: software (rusty_h264)"); |
| 245 | - run(demuxer, tx, start_frame, end_frame, &worker_cancel) | |
| 257 | + run(demuxer, tx, start_frame, end_frame, &color, &worker_cancel) | |
| 246 | 258 | })?; |
| 247 | 259 | Ok(( |
| 248 | 260 | meta, |
| @@ -277,6 +289,7 @@ fn run_hw( | ||
| 277 | 289 | tx: &SyncSender<Frame>, |
| 278 | 290 | start_frame: u32, |
| 279 | 291 | end_frame: u32, |
| 292 | + color: &ColorParams, | |
| 280 | 293 | cancel: &AtomicBool, |
| 281 | 294 | ) -> bool { |
| 282 | 295 | let mut index = start_frame; |
| @@ -290,7 +303,7 @@ fn run_hw( | ||
| 290 | 303 | index, |
| 291 | 304 | width, |
| 292 | 305 | height, |
| 293 | - bgra: i420_to_bgra(y, u, v, width as usize, height as usize), | |
| 306 | + bgra: i420_to_bgra_with(color, y, u, v, width as usize, height as usize), | |
| 294 | 307 | }; |
| 295 | 308 | index += 1; |
| 296 | 309 | if tx.send(frame).is_err() { |
| @@ -335,8 +348,13 @@ mod tests { | ||
| 335 | 348 | /// regression fails in ten seconds instead of hanging the suite forever. |
| 336 | 349 | #[test] |
| 337 | 350 | fn dropping_a_frame_stream_never_waits_for_an_undrained_channel() { |
| 338 | - let (_meta, stream) = | |
| 339 | - DecodeSession::open_worker(&asset("test_baseline.mp4"), 0, u32::MAX).expect("open"); | |
| 351 | + let (_meta, stream) = DecodeSession::open_worker( | |
| 352 | + &asset("test_baseline.mp4"), | |
| 353 | + 0, | |
| 354 | + u32::MAX, | |
| 355 | + ColorParams::default(), | |
| 356 | + ) | |
| 357 | + .expect("open"); | |
| 340 | 358 | // Nothing is ever received: two frames fill the channel and the next |
| 341 | 359 | // send parks the decode thread, exactly as a refused project load does. |
| 342 | 360 | thread::sleep(Duration::from_millis(500)); |
| @@ -359,6 +377,7 @@ fn run( | ||
| 359 | 377 | tx: SyncSender<Frame>, |
| 360 | 378 | start_frame: u32, |
| 361 | 379 | end_frame: u32, |
| 380 | + color: &ColorParams, | |
| 362 | 381 | cancel: &AtomicBool, |
| 363 | 382 | ) { |
| 364 | 383 | let mut decoder = Decoder::new(); |
| @@ -398,7 +417,7 @@ fn run( | ||
| 398 | 417 | index: index as u32, |
| 399 | 418 | width: yuv.width as u32, |
| 400 | 419 | height: yuv.height as u32, |
| 401 | - bgra: i420_to_bgra(&yuv.y, &yuv.u, &yuv.v, yuv.width, yuv.height), | |
| 420 | + bgra: i420_to_bgra_with(color, &yuv.y, &yuv.u, &yuv.v, yuv.width, yuv.height), | |
| 402 | 421 | }; |
| 403 | 422 | index += 1; |
| 404 | 423 | if tx.send(frame).is_err() { |
crates/engine/src/export.rsmodified+25 -0
| @@ -286,6 +286,13 @@ fn run( | ||
| 286 | 286 | } |
| 287 | 287 | None => None, |
| 288 | 288 | }; |
| 289 | + // What this span is graded by -- the same answer playback's decoder | |
| 290 | + // carries, so the export is the picture that was watched. `None` for a | |
| 291 | + // gap and for an ungraded clip, and that is the path where not a byte | |
| 292 | + // is touched. Scratch outside the frame loop: it is refilled per frame | |
| 293 | + // and its allocation survives the whole span. | |
| 294 | + let grade = project.composite_color_at(span.start).copied(); | |
| 295 | + let mut graded = (Vec::new(), Vec::new(), Vec::new()); | |
| 289 | 296 | for _ in 0..span.len { |
| 290 | 297 | cancelled(shared)?; |
| 291 | 298 | let picture = match &mut pictures { |
| @@ -295,6 +302,24 @@ fn run( | ||
| 295 | 302 | let Some((y, u, v, width, height)) = picture else { |
| 296 | 303 | break; // source ran out early; the clip list outlives the file |
| 297 | 304 | }; |
| 305 | + // The planes are borrowed from the decoder (and `Black::picture` | |
| 306 | + // hands the same slice as both u and v), so a grade cannot be | |
| 307 | + // applied in place: it goes onto a copy, which the encoder then | |
| 308 | + // reads instead. | |
| 309 | + let (y, u, v) = match grade.filter(|p| !p.is_identity()) { | |
| 310 | + Some(params) => { | |
| 311 | + let (gy, gu, gv) = &mut graded; | |
| 312 | + gy.clear(); | |
| 313 | + gy.extend_from_slice(y); | |
| 314 | + gu.clear(); | |
| 315 | + gu.extend_from_slice(u); | |
| 316 | + gv.clear(); | |
| 317 | + gv.extend_from_slice(v); | |
| 318 | + crate::color::apply_yuv(¶ms, gy, gu, gv); | |
| 319 | + (&gy[..], &gu[..], &gv[..]) | |
| 320 | + } | |
| 321 | + None => (y, u, v), | |
| 322 | + }; | |
| 298 | 323 | if let Some(au) = encoder.encode(y, u, v, width, height)? { |
| 299 | 324 | write_video(&mut muxer, out, meta, audio_params.as_ref(), au)?; |
| 300 | 325 | } |
crates/engine/src/playback.rsmodified+43 -3
| @@ -21,6 +21,7 @@ use std::time::Duration; | ||
| 21 | 21 | use crate::ao::AoSession; |
| 22 | 22 | use crate::audio::{AudioChunk, AudioSession}; |
| 23 | 23 | use crate::clock::{ClockSource, PlaybackClock}; |
| 24 | +use crate::color::ColorParams; | |
| 24 | 25 | use crate::decode::{DecodeSession, Frame, Worker}; |
| 25 | 26 | use crate::demux::{Demuxer, VideoMeta}; |
| 26 | 27 | use crate::eq::EqParams; |
| @@ -139,7 +140,11 @@ impl PlaybackSession { | ||
| 139 | 140 | } |
| 140 | 141 | // `open_worker` rather than `open` purely for the worker handle: the |
| 141 | 142 | // field has to exist from the start for the first seek to use it. |
| 142 | - let (meta, stream) = DecodeSession::open_worker(&path, 0, u32::MAX)?; | |
| 143 | + // Ungraded: a file just opened has one clip per lane and nothing has | |
| 144 | + // graded it yet. Every later span is opened by `start_span`, which asks | |
| 145 | + // the project. | |
| 146 | + let (meta, stream) = | |
| 147 | + DecodeSession::open_worker(&path, 0, u32::MAX, ColorParams::default())?; | |
| 143 | 148 | // A file is opened on its first audio stream, like `Project::single` |
| 144 | 149 | // names it: nothing has picked one yet. |
| 145 | 150 | let (audio, audio_disabled) = open_audio(&path, 0); |
| @@ -195,8 +200,12 @@ impl PlaybackSession { | ||
| 195 | 200 | // locals drop in reverse declaration order -- a bare worker would join |
| 196 | 201 | // its decode thread while the receiver next to it was still holding |
| 197 | 202 | // that thread parked in `send`, which is a hang (see [`FrameStream`]). |
| 198 | - let (meta, stream) = DecodeSession::open_worker(&first.path, 0, u32::MAX) | |
| 199 | - .map_err(|e| format!("source {}: {e}", first.path.display()))?; | |
| 203 | + // Ungraded, and superseded before a frame of it is shown: the `seek` at | |
| 204 | + // the end of this function reopens the playhead's span through | |
| 205 | + // `start_span`, which is where a saved grade reaches the picture. | |
| 206 | + let (meta, stream) = | |
| 207 | + DecodeSession::open_worker(&first.path, 0, u32::MAX, ColorParams::default()) | |
| 208 | + .map_err(|e| format!("source {}: {e}", first.path.display()))?; | |
| 200 | 209 | let first_audio = AudioSession::probe(&first.path, first.audio_stream)?; |
| 201 | 210 | |
| 202 | 211 | let mut counts = vec![meta.frame_count]; |
| @@ -384,10 +393,17 @@ impl PlaybackSession { | ||
| 384 | 393 | /// disconnected receiver carries the session on to the next span. |
| 385 | 394 | fn start_span(&mut self, span: Span) { |
| 386 | 395 | let opened = match span.from { |
| 396 | + // The grade is the composite's at this frame -- the same clip the | |
| 397 | + // span itself came from -- and it is constant across the span, so | |
| 398 | + // the worker carries it and every frame it converts wears it. | |
| 387 | 399 | Some((source, in_frame)) => DecodeSession::open_worker( |
| 388 | 400 | &self.project.sources()[source].path, |
| 389 | 401 | in_frame, |
| 390 | 402 | in_frame + span.len, |
| 403 | + self.project | |
| 404 | + .composite_color_at(span.start) | |
| 405 | + .copied() | |
| 406 | + .unwrap_or_default(), | |
| 391 | 407 | ) |
| 392 | 408 | .map(|(_, stream)| stream) |
| 393 | 409 | .inspect_err(|e| eprintln!("timeline frame {}: video open failed: {e}", span.start)), |
| @@ -515,6 +531,30 @@ impl PlaybackSession { | ||
| 515 | 531 | self.edit(|p| p.lift(lane, idx)) |
| 516 | 532 | } |
| 517 | 533 | |
| 534 | + /// The clip the picture is coming from at `timeline_secs`: the lane it sits | |
| 535 | + /// on and its index there, by the same topmost-lane-wins rule the decoder | |
| 536 | + /// follows. `None` over a gap and past the end. What a card that grades | |
| 537 | + /// "this clip" opens on when nothing has been clicked. | |
| 538 | + pub fn video_clip_at(&self, timeline_secs: f64) -> Option<(Lane, usize)> { | |
| 539 | + self.project | |
| 540 | + .composite_clip_at(secs_to_frame(timeline_secs, self.meta.frame_rate)) | |
| 541 | + } | |
| 542 | + | |
| 543 | + /// How the clip at `idx` of `lane` is graded, or `None` for one that plays | |
| 544 | + /// as it was shot -- what a colour card reads when it opens. | |
| 545 | + pub fn color_of(&self, lane: Lane, idx: usize) -> Option<&ColorParams> { | |
| 546 | + self.project.color_of(lane, idx) | |
| 547 | + } | |
| 548 | + | |
| 549 | + /// Grades that clip, or takes the grade off with `None`. One undo step like | |
| 550 | + /// every other edit, and it reseeks like one too -- which is what puts the | |
| 551 | + /// new grade on the frame that is already on screen, paused or playing, | |
| 552 | + /// without the caller having to nudge the playhead itself. `false` for an | |
| 553 | + /// index that is not there and for a value that is not finite. | |
| 554 | + pub fn set_color(&mut self, lane: Lane, idx: usize, params: Option<ColorParams>) -> bool { | |
| 555 | + self.edit(|p| p.set_color(lane, idx, params)) | |
| 556 | + } | |
| 557 | + | |
| 518 | 558 | /// The clip at `idx` -- what a caller copies. It is a pair of source frame |
| 519 | 559 | /// numbers and nothing else, so a copy stays valid after the clip it came |
| 520 | 560 | /// from is deleted. `None` past the end. |
crates/engine/src/project.rsmodified+31 -5
| @@ -757,11 +757,7 @@ impl Project { | ||
| 757 | 757 | if timeline_frame >= total { |
| 758 | 758 | return None; |
| 759 | 759 | } |
| 760 | - let video: Vec<Lane> = self | |
| 761 | - .lanes() | |
| 762 | - .into_iter() | |
| 763 | - .filter(|l| l.kind == LaneKind::Video) | |
| 764 | - .collect(); | |
| 760 | + let video = self.video_lanes(); | |
| 765 | 761 | // The last covering lane is the winner; only the lanes above it can |
| 766 | 762 | // interrupt what it shows. |
| 767 | 763 | let winner = video |
| @@ -794,6 +790,36 @@ impl Project { | ||
| 794 | 790 | }) |
| 795 | 791 | } |
| 796 | 792 | |
| 793 | + /// The video lanes in display order, topmost last -- the order the composite | |
| 794 | + /// rule counts in. | |
| 795 | + fn video_lanes(&self) -> Vec<Lane> { | |
| 796 | + self.lanes() | |
| 797 | + .into_iter() | |
| 798 | + .filter(|l| l.kind == LaneKind::Video) | |
| 799 | + .collect() | |
| 800 | + } | |
| 801 | + | |
| 802 | + /// *Which* clip the composite shows at `timeline_frame`, named: the winner | |
| 803 | + /// of [`composite_span_at`](Project::composite_span_at)'s own rule and its | |
| 804 | + /// index on that lane. `None` over a gap and past the end. What a front-end | |
| 805 | + /// asks to act on "the clip on screen" without re-deriving the rule. | |
| 806 | + pub fn composite_clip_at(&self, timeline_frame: u32) -> Option<(Lane, usize)> { | |
| 807 | + let video = self.video_lanes(); | |
| 808 | + let winner = video | |
| 809 | + .iter() | |
| 810 | + .rposition(|&lane| at(self.lane(lane), timeline_frame).is_some())?; | |
| 811 | + Some((video[winner], at(self.lane(video[winner]), timeline_frame)?)) | |
| 812 | + } | |
| 813 | + | |
| 814 | + /// How the composite is graded at `timeline_frame`, so the picture playback | |
| 815 | + /// converts and the one an export encodes are graded from one answer. | |
| 816 | + /// `None` over a gap (black is black) and for a clip nobody has graded, | |
| 817 | + /// which is the byte-identical path everywhere. | |
| 818 | + pub fn composite_color_at(&self, timeline_frame: u32) -> Option<&ColorParams> { | |
| 819 | + let (lane, idx) = self.composite_clip_at(timeline_frame)?; | |
| 820 | + self.color_of(lane, idx) | |
| 821 | + } | |
| 822 | + | |
| 797 | 823 | /// [`spans_from`](Project::spans_from) over the composite: every stretch the |
| 798 | 824 | /// viewer sees from `timeline_frame` to the end, in order. What an export |
| 799 | 825 | /// encodes and what playback walks at a clip boundary. |
crates/engine/tests/color.rsadded+183 -0
| @@ -0,0 +1,183 @@ | ||
| 1 | +//! A per-clip colour grade, end to end: what the renderer shows and what the | |
| 2 | +//! export writes have to be the same picture, and a clip nobody graded has to | |
| 3 | +//! come out of both untouched. | |
| 4 | +//! | |
| 5 | +//! `test_baseline.mp4` is colour bars (mean channel spread 249 of 255), so | |
| 6 | +//! saturation 0 is not a subtle claim: every pixel of a graded span is grey to | |
| 7 | +//! within the converter's own rounding, and every pixel outside it is not. | |
| 8 | +//! | |
| 9 | +//! ```text | |
| 10 | +//! cargo test -p engine --release --test color -- --nocapture --test-threads=1 | |
| 11 | +//! ``` | |
| 12 | + | |
| 13 | +use std::path::{Path, PathBuf}; | |
| 14 | +use std::thread::sleep; | |
| 15 | +use std::time::{Duration, Instant}; | |
| 16 | + | |
| 17 | +use engine::color::ColorParams; | |
| 18 | +use engine::export::ExportSettings; | |
| 19 | +use engine::project::Lane; | |
| 20 | +use engine::{DecodeSession, ExportHandle, PlaybackSession}; | |
| 21 | + | |
| 22 | +const FPS: f64 = 30.0; | |
| 23 | +/// Where the first clip ends and the ungraded one begins. | |
| 24 | +const CUT: u32 = 30; | |
| 25 | + | |
| 26 | +fn asset(name: &str) -> PathBuf { | |
| 27 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 28 | + .join("../../assets") | |
| 29 | + .join(name) | |
| 30 | +} | |
| 31 | + | |
| 32 | +/// Silent, like every other suite that plays a real file: the grade is a | |
| 33 | +/// picture setting and nothing here listens. | |
| 34 | +fn open(path: &Path) -> PlaybackSession { | |
| 35 | + let session = PlaybackSession::open(path).expect("open"); | |
| 36 | + session.set_gain(0.0); | |
| 37 | + session | |
| 38 | +} | |
| 39 | + | |
| 40 | +/// Two clips on `V1`, the first of them drained of all colour. | |
| 41 | +fn graded() -> PlaybackSession { | |
| 42 | + let mut session = open(&asset("test_baseline.mp4")); | |
| 43 | + assert!(session.cut_at(f64::from(CUT) / FPS), "cut at frame 30"); | |
| 44 | + assert!( | |
| 45 | + session.set_color( | |
| 46 | + Lane::V1, | |
| 47 | + 0, | |
| 48 | + Some(ColorParams { | |
| 49 | + saturation: 0.0, | |
| 50 | + ..Default::default() | |
| 51 | + }) | |
| 52 | + ), | |
| 53 | + "grade the first clip" | |
| 54 | + ); | |
| 55 | + session | |
| 56 | +} | |
| 57 | + | |
| 58 | +/// How far a BGRA frame is from grey: the mean spread between its channels, | |
| 59 | +/// which is 0 for a greyscale picture and ~250 for these colour bars. | |
| 60 | +fn spread(bgra: &[u8]) -> f64 { | |
| 61 | + let total: u64 = bgra | |
| 62 | + .chunks_exact(4) | |
| 63 | + .map(|px| u64::from(px[..3].iter().max().unwrap() - px[..3].iter().min().unwrap())) | |
| 64 | + .sum(); | |
| 65 | + total as f64 / (bgra.len() / 4) as f64 | |
| 66 | +} | |
| 67 | + | |
| 68 | +/// The next frame off a freshly seeked channel. | |
| 69 | +fn next_frame(session: &mut PlaybackSession, what: &str) -> engine::Frame { | |
| 70 | + let deadline = Instant::now() + Duration::from_secs(10); | |
| 71 | + loop { | |
| 72 | + if let Some(frame) = session.try_frame() { | |
| 73 | + return frame; | |
| 74 | + } | |
| 75 | + assert!(Instant::now() < deadline, "no frame after {what}"); | |
| 76 | + sleep(Duration::from_millis(4)); | |
| 77 | + } | |
| 78 | +} | |
| 79 | + | |
| 80 | +fn seek_and_show(session: &mut PlaybackSession, frame: u32) -> Vec<u8> { | |
| 81 | + session.seek(f64::from(frame) / FPS); | |
| 82 | + let shown = next_frame(session, "a seek"); | |
| 83 | + assert_eq!(shown.index, frame, "the seek landed elsewhere"); | |
| 84 | + shown.bgra | |
| 85 | +} | |
| 86 | + | |
| 87 | +/// The render half: the grade reaches the picture, and it stops at the clip's | |
| 88 | +/// own edge rather than colouring the whole timeline. | |
| 89 | +#[test] | |
| 90 | +fn a_graded_clip_renders_grey_and_the_next_one_does_not() { | |
| 91 | + let mut session = graded(); | |
| 92 | + for frame in [0, CUT - 1] { | |
| 93 | + let inside = spread(&seek_and_show(&mut session, frame)); | |
| 94 | + assert!( | |
| 95 | + inside <= 2.0, | |
| 96 | + "frame {frame} is graded and not grey: {inside}" | |
| 97 | + ); | |
| 98 | + } | |
| 99 | + for frame in [CUT, CUT + 15] { | |
| 100 | + let outside = spread(&seek_and_show(&mut session, frame)); | |
| 101 | + assert!( | |
| 102 | + outside > 100.0, | |
| 103 | + "frame {frame} is outside the graded clip and lost its colour: {outside}" | |
| 104 | + ); | |
| 105 | + } | |
| 106 | +} | |
| 107 | + | |
| 108 | +/// Live: a grade set while the session is up repaints what is on screen. The | |
| 109 | +/// engine reseeks on the edit, so the very next frame is the graded one -- no | |
| 110 | +/// scrub, no reopen by the caller. | |
| 111 | +#[test] | |
| 112 | +fn setting_a_grade_repaints_the_frame_that_is_already_up() { | |
| 113 | + let mut session = open(&asset("test_baseline.mp4")); | |
| 114 | + let before = spread(&seek_and_show(&mut session, 10)); | |
| 115 | + assert!(before > 100.0, "the fixture is not colour bars: {before}"); | |
| 116 | + | |
| 117 | + assert!(session.set_color( | |
| 118 | + Lane::V1, | |
| 119 | + 0, | |
| 120 | + Some(ColorParams { | |
| 121 | + saturation: 0.0, | |
| 122 | + ..Default::default() | |
| 123 | + }) | |
| 124 | + )); | |
| 125 | + let after = spread(&next_frame(&mut session, "a grade").bgra); | |
| 126 | + assert!(after <= 2.0, "the frame on screen kept its colour: {after}"); | |
| 127 | + | |
| 128 | + // And taking it off puts the colour back, on the same terms. | |
| 129 | + assert!(session.set_color(Lane::V1, 0, None)); | |
| 130 | + let back = spread(&next_frame(&mut session, "an ungrade").bgra); | |
| 131 | + assert!(back > 100.0, "ungrading left the picture grey: {back}"); | |
| 132 | +} | |
| 133 | + | |
| 134 | +fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> { | |
| 135 | + let started = Instant::now(); | |
| 136 | + while !handle.is_finished() { | |
| 137 | + assert!( | |
| 138 | + started.elapsed() < limit, | |
| 139 | + "export did not finish in {limit:?}" | |
| 140 | + ); | |
| 141 | + sleep(Duration::from_millis(20)); | |
| 142 | + } | |
| 143 | + handle.result().expect("a finished export has an outcome") | |
| 144 | +} | |
| 145 | + | |
| 146 | +/// The export half, decoded back off disk: the same picture the renderer | |
| 147 | +/// showed, graded span for graded span. Software encoder, so it needs nothing | |
| 148 | +/// installed -- the same pin `tests/export.rs`'s default suite uses. | |
| 149 | +#[test] | |
| 150 | +fn an_exported_grade_is_the_one_that_was_watched() { | |
| 151 | + unsafe { | |
| 152 | + std::env::set_var("VE_SW", "1"); | |
| 153 | + std::env::set_var("VE_SW_ENC", "1"); | |
| 154 | + } | |
| 155 | + let mut session = graded(); | |
| 156 | + session.pause(); | |
| 157 | + let out = std::env::temp_dir().join(format!("ve_color_{}.mp4", std::process::id())); | |
| 158 | + let _ = std::fs::remove_file(&out); | |
| 159 | + let handle = session.export_to_with(&out, &ExportSettings::default()); | |
| 160 | + wait(&handle, Duration::from_secs(180)).expect("export"); | |
| 161 | + | |
| 162 | + let (_, frames) = DecodeSession::open(&out).expect("decode the export"); | |
| 163 | + let mut seen = 0; | |
| 164 | + for frame in frames { | |
| 165 | + let spread = spread(&frame.bgra); | |
| 166 | + if frame.index < CUT { | |
| 167 | + assert!( | |
| 168 | + spread <= 4.0, | |
| 169 | + "exported frame {} is inside the graded clip and kept its colour: {spread}", | |
| 170 | + frame.index | |
| 171 | + ); | |
| 172 | + } else { | |
| 173 | + assert!( | |
| 174 | + spread > 100.0, | |
| 175 | + "exported frame {} is outside the graded clip and lost its colour: {spread}", | |
| 176 | + frame.index | |
| 177 | + ); | |
| 178 | + } | |
| 179 | + seen += 1; | |
| 180 | + } | |
| 181 | + assert!(seen > CUT, "the export is shorter than the graded clip"); | |
| 182 | + let _ = std::fs::remove_file(&out); | |
| 183 | +} |