🔀 merge(C6): the frame rate is picked, and the whole edit list conforms to it exactly
Browse files at this commit5 changed files +418 -13
- crates/app/src/main.rs +144 -5
- crates/engine/src/edith.rs +7 -4
- crates/engine/src/playback.rs +93 -2
- crates/engine/src/project.rs +65 -2
- crates/engine/tests/mixed_fps.rs +109 -0
crates/app/src/main.rsmodified+144 -5
| @@ -430,6 +430,7 @@ struct Picker { | ||
| 430 | 430 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 431 | 431 | enum Pick { |
| 432 | 432 | Resolution, |
| 433 | + Fps, | |
| 433 | 434 | Fit(Lane, usize), |
| 434 | 435 | /// What the export's *sound* is coded at. Opened from the card's Sound row, |
| 435 | 436 | /// which is the only place it means anything. |
| @@ -439,9 +440,13 @@ enum Pick { | ||
| 439 | 440 | /// One value a list offers, carrying everything picking it needs -- so a click |
| 440 | 441 | /// goes straight to the value rather than to a position in a list that was |
| 441 | 442 | /// built somewhere else. |
| 442 | -#[derive(Clone, Copy, PartialEq, Eq, Debug)] | |
| 443 | +/// `Eq` is off it for the rate: a frame rate is the `f64` the engine is told, | |
| 444 | +/// bit for bit (23.976023976... is not 23.976), and nothing here keys a map on a | |
| 445 | +/// choice -- comparing two is all a list row ever does. | |
| 446 | +#[derive(Clone, Copy, PartialEq, Debug)] | |
| 443 | 447 | enum Choice { |
| 444 | 448 | Size(u32, u32), |
| 449 | + Fps(f64), | |
| 445 | 450 | Fit(Lane, usize, FitPolicy), |
| 446 | 451 | AudioRate(u32), |
| 447 | 452 | } |
| @@ -622,6 +627,23 @@ fn typed(key: &str) -> Option<char> { | ||
| 622 | 627 | /// them, which is what makes the trip round come back to where it started. |
| 623 | 628 | const RESOLUTIONS: [(u32, u32); 4] = [(3840, 2160), (1920, 1080), (1280, 720), (854, 480)]; |
| 624 | 629 | |
| 630 | +/// The project frame rates the list offers, slowest first: the rates footage is | |
| 631 | +/// actually shot and delivered at, the NTSC ones written as the ratios they are | |
| 632 | +/// (`24000/1001`, not `23.976`) -- the engine conforms the timeline to the very | |
| 633 | +/// number it is handed, so a rate rounded here would be a rate no timescale can | |
| 634 | +/// name. The media's own is cycled in beside them | |
| 635 | +/// ([`frame_rate_ladder`]), which is what keeps the way back on the list. | |
| 636 | +const FRAME_RATES: [f64; 8] = [ | |
| 637 | + 24_000. / 1001., | |
| 638 | + 24., | |
| 639 | + 25., | |
| 640 | + 30_000. / 1001., | |
| 641 | + 30., | |
| 642 | + 50., | |
| 643 | + 60_000. / 1001., | |
| 644 | + 60., | |
| 645 | +]; | |
| 646 | + | |
| 625 | 647 | /// How far a band may be pushed either way, in dB. The engine clamps nothing |
| 626 | 648 | /// here -- it will filter whatever it is given -- so this is a UI decision: |
| 627 | 649 | /// past about this a peaking band stops sounding like tone and starts sounding |
| @@ -1696,6 +1718,23 @@ impl Player { | ||
| 1696 | 1718 | cx.notify(); |
| 1697 | 1719 | } |
| 1698 | 1720 | |
| 1721 | + /// The project cut at another rate: the list names one and this is where it | |
| 1722 | + /// happens, the way [`apply_resolution`](Self::apply_resolution) is for a | |
| 1723 | + /// size. The whole timeline is conformed to it by the engine | |
| 1724 | + /// ([`PlaybackSession::set_frame_rate`]) -- same seconds, same footage -- | |
| 1725 | + /// and the rate the app itself counts frames in follows, since every | |
| 1726 | + /// timecode, ruler mark and step key here is measured in it. | |
| 1727 | + fn apply_frame_rate(&mut self, fps: f64, cx: &mut Context<Self>) { | |
| 1728 | + if let Some(session) = &mut self.session | |
| 1729 | + && session.set_frame_rate(fps) | |
| 1730 | + { | |
| 1731 | + self.fps = session.meta().frame_rate; | |
| 1732 | + self.notice = Some(format!("PROJECT: {} fps", fps_label(fps)).into()); | |
| 1733 | + self.reset_after_reseek(); | |
| 1734 | + } | |
| 1735 | + cx.notify(); | |
| 1736 | + } | |
| 1737 | + | |
| 1699 | 1738 | /// Opens a choice list on a setting, where it was asked for. One floating |
| 1700 | 1739 | /// thing at a time: the click that opens it is the click that closes |
| 1701 | 1740 | /// whatever menu it was opened from. |
| @@ -1713,6 +1752,7 @@ impl Player { | ||
| 1713 | 1752 | self.picker = None; |
| 1714 | 1753 | match choice { |
| 1715 | 1754 | Choice::Size(w, h) => self.apply_resolution(w, h, cx), |
| 1755 | + Choice::Fps(fps) => self.apply_frame_rate(fps, cx), | |
| 1716 | 1756 | Choice::Fit(lane, idx, fit) => self.apply_fit(lane, idx, fit, cx), |
| 1717 | 1757 | // The same field the row's key steps, set outright: a list picks a |
| 1718 | 1758 | // value, it does not step to one. |
| @@ -1734,6 +1774,7 @@ impl Player { | ||
| 1734 | 1774 | Pick::Resolution => { |
| 1735 | 1775 | resolution_choices(session.resolution(), session.native_resolution()) |
| 1736 | 1776 | } |
| 1777 | + Pick::Fps => fps_choices(session.meta().frame_rate, session.native_frame_rate()), | |
| 1737 | 1778 | Pick::Fit(lane, idx) => { |
| 1738 | 1779 | fit_choices(lane, idx, session.fit_of(lane, idx), session.resolution()) |
| 1739 | 1780 | } |
| @@ -5599,6 +5640,30 @@ impl Player { | ||
| 5599 | 5640 | this.open_picker(Pick::Resolution, event.position(), cx) |
| 5600 | 5641 | }), |
| 5601 | 5642 | )) |
| 5643 | + // The rate every clip is counted in, which is also the rate | |
| 5644 | + // the export is written at -- the other half of "the project | |
| 5645 | + // is not the media", and the one the panel could only ever | |
| 5646 | + // read out before. A click opens the rates with this one | |
| 5647 | + // marked; picking one conforms the whole timeline to it. | |
| 5648 | + .child(control( | |
| 5649 | + "fps", | |
| 5650 | + None, | |
| 5651 | + match self.session.is_some() { | |
| 5652 | + true => format!("{} fps", fps_label(self.fps)), | |
| 5653 | + false => "Rate".to_string(), | |
| 5654 | + }, | |
| 5655 | + match self.session.is_some() { | |
| 5656 | + true => format!( | |
| 5657 | + "click to pick a frame rate — the project is cut at {} fps", | |
| 5658 | + fps_label(self.fps) | |
| 5659 | + ), | |
| 5660 | + false => "the project's frame rate — open a file first".to_string(), | |
| 5661 | + }, | |
| 5662 | + live, | |
| 5663 | + cx.listener(|this, event: &ClickEvent, _, cx| { | |
| 5664 | + this.open_picker(Pick::Fps, event.position(), cx) | |
| 5665 | + }), | |
| 5666 | + )) | |
| 5602 | 5667 | // How much of the timeline the panel below is showing -- |
| 5603 | 5668 | // beside the resolution, since neither of them edits |
| 5604 | 5669 | // anything: they are both what is being looked at. The |
| @@ -10022,6 +10087,53 @@ fn resolution_choices(current: (u32, u32), native: (u32, u32)) -> Vec<ChoiceRow> | ||
| 10022 | 10087 | .collect() |
| 10023 | 10088 | } |
| 10024 | 10089 | |
| 10090 | +/// Every project frame rate on offer, slowest first: [`FRAME_RATES`] with the | |
| 10091 | +/// media's own cycled in at its place by speed, so a project already cut at a | |
| 10092 | +/// listed rate does not see it twice and the media's own rate -- the one a | |
| 10093 | +/// project moved off it has no other way back to -- is always there. | |
| 10094 | +/// [`resolution_ladder`]'s rule, for the other setting the project has of its | |
| 10095 | +/// own. | |
| 10096 | +fn frame_rate_ladder(native: f64) -> Vec<f64> { | |
| 10097 | + let mut rates = FRAME_RATES.to_vec(); | |
| 10098 | + // Bit for bit: 23.976023976... is not 23.976, and a rate that read as | |
| 10099 | + // "already listed" when it is not would take the media's own off the list. | |
| 10100 | + if !rates.contains(&native) { | |
| 10101 | + let at = rates | |
| 10102 | + .iter() | |
| 10103 | + .position(|&fps| fps > native) | |
| 10104 | + .unwrap_or(rates.len()); | |
| 10105 | + rates.insert(at, native); | |
| 10106 | + } | |
| 10107 | + rates | |
| 10108 | +} | |
| 10109 | + | |
| 10110 | +/// The rate list's rows: every rung of the ladder, the media's own said so, and | |
| 10111 | +/// the one in force marked. Named as a person writes a rate ([`fps_label`]), | |
| 10112 | +/// with what it is for beside it -- short, or the row loses its tail to the | |
| 10113 | +/// truncation the resolution list already met. | |
| 10114 | +fn fps_choices(current: f64, native: f64) -> Vec<ChoiceRow> { | |
| 10115 | + frame_rate_ladder(native) | |
| 10116 | + .into_iter() | |
| 10117 | + .map(|fps| { | |
| 10118 | + ( | |
| 10119 | + Choice::Fps(fps), | |
| 10120 | + format!("{} fps", fps_label(fps)).into(), | |
| 10121 | + match fps == native { | |
| 10122 | + true => "the media's own".to_string(), | |
| 10123 | + // The rates that are a ratio are the ones nobody can tell | |
| 10124 | + // from their neighbour by the label alone. | |
| 10125 | + false => match (fps - fps.round()).abs() < 0.001 { | |
| 10126 | + true => String::new(), | |
| 10127 | + false => "NTSC".to_string(), | |
| 10128 | + }, | |
| 10129 | + } | |
| 10130 | + .into(), | |
| 10131 | + fps == current, | |
| 10132 | + ) | |
| 10133 | + }) | |
| 10134 | + .collect() | |
| 10135 | +} | |
| 10136 | + | |
| 10025 | 10137 | /// The fit list's rows: all four policies against the canvas they place a |
| 10026 | 10138 | /// picture on, since the word alone ("fill") says nothing about the size it is |
| 10027 | 10139 | /// filling -- which is the very thing the notice says after a stroke. |
| @@ -11624,7 +11736,8 @@ mod tests { | ||
| 11624 | 11736 | containers, |
| 11625 | 11737 | enable, envelope, eq_card_w, eq_freq, eq_freq_label, eq_spectrum, eq_x, eq_y, |
| 11626 | 11738 | estimated_mb, export_path, export_settings, format_key, |
| 11627 | - format_line, format_refusal, fps_label, frac_along, frac_down, frame_at, histogram, | |
| 11739 | + format_line, format_refusal, fps_choices, fps_label, frac_along, frac_down, frame_at, | |
| 11740 | + frame_rate_ladder, histogram, | |
| 11628 | 11741 | inserted_band, is_bare_modifier, is_project, keymap, keys_filter, keys_rows, lanes_h, |
| 11629 | 11742 | marked, menu_at, next_audio_kbps, next_container, normalise, nothing_to_play, panel_h, project_path, |
| 11630 | 11743 | retarget, scrub_due, show_label, silence_rate, snap_cue, snap_marks, snapped, |
| @@ -11633,7 +11746,8 @@ mod tests { | ||
| 11633 | 11746 | whole_take, window_title, |
| 11634 | 11747 | }; |
| 11635 | 11748 | use super::{ |
| 11636 | - Choice, ETA_SPAN, Edge, FITS, LaneKind, PPS_DEFAULT, RESOLUTIONS, Repeat, Scale, View, | |
| 11749 | + Choice, ETA_SPAN, Edge, FITS, FRAME_RATES, LaneKind, PPS_DEFAULT, RESOLUTIONS, Repeat, | |
| 11750 | + Scale, View, | |
| 11637 | 11751 | ZOOM_MAX_SECONDS, ZOOM_MIN_FRAMES, ZOOM_STEP, audio_rate_choices, clock, eta_secs, file_name, file_uri, |
| 11638 | 11752 | fit_choices, landing, lane_refuses, library_rows, live_idx, next_fit, next_resolution, |
| 11639 | 11753 | note_progress, px_along, |
| @@ -14570,10 +14684,35 @@ mod tests { | ||
| 14570 | 14684 | assert!(fits[2].3, "the clip's own policy is not marked"); |
| 14571 | 14685 | assert!(fits[0].2.contains("1920x1080"), "{}", fits[0].2); |
| 14572 | 14686 | |
| 14687 | + // The rate list, the other setting the project has of its own: every | |
| 14688 | + // rate on offer, the media's own cycled in at its place by speed and | |
| 14689 | + // said so, and the one the timeline is cut at marked. The value carried | |
| 14690 | + // is the `f64` the engine conforms to, not the rounded label -- 23.976 | |
| 14691 | + // is not 24000/1001, and a rate the timescales cannot name is refused. | |
| 14692 | + let ntsc = 24_000. / 1001.; | |
| 14693 | + let rates = frame_rate_ladder(25.); | |
| 14694 | + assert_eq!(rates.len(), FRAME_RATES.len(), "25 is already on the list"); | |
| 14695 | + let odd = frame_rate_ladder(48.); | |
| 14696 | + assert_eq!(odd[5], 48., "the media's own, in its place by speed"); | |
| 14697 | + assert_eq!(odd.len(), FRAME_RATES.len() + 1); | |
| 14698 | + let fps = fps_choices(ntsc, 48.); | |
| 14699 | + assert_eq!(fps.len(), odd.len()); | |
| 14700 | + assert_eq!(fps.iter().filter(|(.., picked)| *picked).count(), 1); | |
| 14701 | + assert_eq!(fps[0].0, Choice::Fps(ntsc), "the ratio, not 23.976"); | |
| 14702 | + assert_eq!(fps[0].1.as_ref(), "23.976 fps"); | |
| 14703 | + assert!(fps[0].3, "the rate in force is not marked"); | |
| 14704 | + assert!(fps[5].2.contains("the media's own"), "{}", fps[5].2); | |
| 14705 | + for (.., detail, _) in &fps { | |
| 14706 | + assert!(detail.chars().count() < 26, "{detail} loses its tail"); | |
| 14707 | + } | |
| 14708 | + | |
| 14573 | 14709 | // The open list fits the floor the menus are measured against: the |
| 14574 | - // longest of them is the resolution ladder, and it hangs at the pointer | |
| 14575 | - // with every row on screen. Rows are click targets (WCAG 2.5.8). | |
| 14710 | + // longest of them is the rate ladder with an odd rate cycled in, and it | |
| 14711 | + // hangs at the pointer with every row on screen. Rows are click targets | |
| 14712 | + // (WCAG 2.5.8). | |
| 14576 | 14713 | assert!(MENU_ROW_H >= HIT_MIN); |
| 14714 | + assert!(odd.len() > ladder.len(), "the longest list moved"); | |
| 14715 | + assert!(MENU_PAD * 2. + odd.len() as f32 * MENU_ROW_H <= 360.); | |
| 14577 | 14716 | let tall = MENU_PAD * 2. + ladder.len() as f32 * MENU_ROW_H; |
| 14578 | 14717 | assert!(tall <= 360., "the list is taller than the floor"); |
| 14579 | 14718 | assert_eq!( |
crates/engine/src/edith.rsmodified+7 -4
| @@ -19,10 +19,13 @@ | ||
| 19 | 19 | //! gain audio 1 -3.0 |
| 20 | 20 | //! ``` |
| 21 | 21 | //! |
| 22 | -//! The `fps` line is the rate the timeline was cut at, printed so it reads back | |
| 23 | -//! bit-exactly. A file without one (every dialect before v9) means "whatever | |
| 24 | -//! the scaffolding source runs at", which is what such a project always was and | |
| 25 | -//! is an answer only a project holding a video has | |
| 22 | +//! The `fps` line is the rate the timeline was cut at -- the scaffolding | |
| 23 | +//! source's own unless a rate was picked | |
| 24 | +//! ([`crate::PlaybackSession::set_frame_rate`]), and the *only* thing that says | |
| 25 | +//! so once one was, since every clip number in the file is counted in it. | |
| 26 | +//! Printed so it reads back bit-exactly. A file without one (every dialect | |
| 27 | +//! before v9) means "whatever the scaffolding source runs at", which is what | |
| 28 | +//! such a project always was and is an answer only a project holding a video has | |
| 26 | 29 | //! ([`crate::PlaybackSession::open_project`]). |
| 27 | 30 | //! |
| 28 | 31 | //! A `subtitle` line is `subtitle <track> <path>`: which track of that file the |
crates/engine/src/playback.rsmodified+93 -2
| @@ -158,6 +158,11 @@ pub struct PlaybackSession { | ||
| 158 | 158 | /// starts here, and a caller offering sizes to pick from needs the media's |
| 159 | 159 | /// own among them or a project moved off it could never come back. |
| 160 | 160 | native: (u32, u32), |
| 161 | + /// ...and the scaffolding source's own frame rate, kept for exactly that | |
| 162 | + /// reason: the project rate starts here, and the list a caller offers rates | |
| 163 | + /// from needs the media's own on it or a project moved off it has no way | |
| 164 | + /// back ([`PlaybackSession::set_frame_rate`] is not an undo step either). | |
| 165 | + native_fps: f64, | |
| 161 | 166 | frames: Receiver<Frame>, |
| 162 | 167 | /// The current decode worker. |
| 163 | 168 | worker: Worker, |
| @@ -277,6 +282,7 @@ impl PlaybackSession { | ||
| 277 | 282 | Ok(Self { |
| 278 | 283 | meta, |
| 279 | 284 | native: (meta.width, meta.height), |
| 285 | + native_fps: meta.frame_rate, | |
| 280 | 286 | frames: stream.frames, |
| 281 | 287 | worker: stream.worker, |
| 282 | 288 | backend: stream.backend, |
| @@ -375,6 +381,7 @@ impl PlaybackSession { | ||
| 375 | 381 | Ok(Self { |
| 376 | 382 | meta, |
| 377 | 383 | native: (width, height), |
| 384 | + native_fps: frame_rate, | |
| 378 | 385 | frames: stream.frames, |
| 379 | 386 | worker: stream.worker, |
| 380 | 387 | backend: stream.backend, |
| @@ -451,6 +458,7 @@ impl PlaybackSession { | ||
| 451 | 458 | Ok(Self { |
| 452 | 459 | meta, |
| 453 | 460 | native: (meta.width, meta.height), |
| 461 | + native_fps: IMAGE_ONLY_RATE, | |
| 454 | 462 | frames: stream.frames, |
| 455 | 463 | worker: stream.worker, |
| 456 | 464 | backend: stream.backend, |
| @@ -585,6 +593,32 @@ impl PlaybackSession { | ||
| 585 | 593 | meta.width = width; |
| 586 | 594 | meta.height = height; |
| 587 | 595 | } |
| 596 | + // ...and the project's own *rate*, which is the scaffold's unless the | |
| 597 | + // file says otherwise. The two made-up rates above already took the | |
| 598 | + // saved one (their counts are computed from it); a scaffold with a | |
| 599 | + // picture keeps its own here, so a project cut at a rate nobody picked | |
| 600 | + // is the file it always was -- and one where a rate *was* picked | |
| 601 | + // ([`set_frame_rate`](Self::set_frame_rate)) comes back at it rather | |
| 602 | + // than at the media's, which would leave every clip number counted in | |
| 603 | + // frames the timeline no longer has. | |
| 604 | + let native_fps = match crate::is_audio(&first.path) { | |
| 605 | + true => AUDIO_ONLY_CANVAS.2, | |
| 606 | + false if crate::is_image(&first.path) => IMAGE_ONLY_RATE, | |
| 607 | + false => meta.frame_rate, | |
| 608 | + }; | |
| 609 | + if let Some(fps) = saved_fps { | |
| 610 | + meta.frame_rate = fps; | |
| 611 | + } | |
| 612 | + // A song and a still have no rate of their own to be conformed from: | |
| 613 | + // their length above was counted in the timeline's frames already, | |
| 614 | + // whatever those are. A file with pictures is read through [`Rate`] like | |
| 615 | + // any other source when the project is cut at another rate. | |
| 616 | + let made_up = crate::is_audio(&first.path) || crate::is_image(&first.path); | |
| 617 | + let scaffold_rate = match made_up || native_fps == meta.frame_rate { | |
| 618 | + true => Rate::REAL_TIME, | |
| 619 | + false => Rate::from_fps(native_fps, meta.frame_rate) | |
| 620 | + .map_err(|e| format!("source {}: {e}", first.path.display()))?, | |
| 621 | + }; | |
| 588 | 622 | let first_audio = first_audio_of(&doc.sources)?; |
| 589 | 623 | |
| 590 | 624 | // In source order, with the scaffold's own count in its own slot: the |
| @@ -599,8 +633,8 @@ impl PlaybackSession { | ||
| 599 | 633 | let mut rates = Vec::with_capacity(doc.sources.len()); |
| 600 | 634 | for (i, source) in doc.sources.iter().enumerate() { |
| 601 | 635 | if i == scaffold { |
| 602 | - counts.push(meta.frame_count); | |
| 603 | - rates.push(Rate::REAL_TIME); | |
| 636 | + counts.push(scaffold_rate.timeline_at(meta.frame_count)); | |
| 637 | + rates.push(scaffold_rate); | |
| 604 | 638 | continue; |
| 605 | 639 | } |
| 606 | 640 | // A still has neither a rate to match nor a track to match with: |
| @@ -704,6 +738,7 @@ impl PlaybackSession { | ||
| 704 | 738 | let mut session = Self { |
| 705 | 739 | meta, |
| 706 | 740 | native, |
| 741 | + native_fps, | |
| 707 | 742 | frames: stream.frames, |
| 708 | 743 | worker: stream.worker, |
| 709 | 744 | backend: stream.backend, |
| @@ -1449,6 +1484,62 @@ impl PlaybackSession { | ||
| 1449 | 1484 | true |
| 1450 | 1485 | } |
| 1451 | 1486 | |
| 1487 | + /// The scaffolding source's own frame rate -- what the project rate started | |
| 1488 | + /// as, and the one rate a caller offering a list of them must not leave out | |
| 1489 | + /// ([`native_resolution`](Self::native_resolution)'s reason). | |
| 1490 | + pub fn native_frame_rate(&self) -> f64 { | |
| 1491 | + self.native_fps | |
| 1492 | + } | |
| 1493 | + | |
| 1494 | + /// Cuts this timeline at `fps` from here on: every clip is *conformed* to | |
| 1495 | + /// the new rate ([`Project::retime`]), every source is read through a | |
| 1496 | + /// [`Rate`] against it, and an export written from this session comes out at | |
| 1497 | + /// it. The edit survives as the same seconds of the same footage on a finer | |
| 1498 | + /// or coarser grid -- a 30 fps take on a timeline moved to 24 keeps its | |
| 1499 | + /// length and its sync, it is simply counted in 24ths now. | |
| 1500 | + /// | |
| 1501 | + /// Reseeks like [`set_resolution`](Self::set_resolution), so the picture and | |
| 1502 | + /// the sound under the playhead are already the new rate's when this | |
| 1503 | + /// returns. `false` -- changing nothing -- for a rate that is not a rate, for | |
| 1504 | + /// the one already in force, and for one no timescale can name | |
| 1505 | + /// ([`crate::mux::frame_timing`], the same wall a file of an unnameable rate | |
| 1506 | + /// is refused at). | |
| 1507 | + /// | |
| 1508 | + /// ponytail: not an undo step, exactly as the project resolution is not -- | |
| 1509 | + /// and this one *moves the frame numbers*, so the way back is picking the | |
| 1510 | + /// old rate (or the media's, [`native_frame_rate`](Self::native_frame_rate)) | |
| 1511 | + /// rather than a `z`, and it lands within a frame of where it started rather | |
| 1512 | + /// than on it (see [`Project::retime`]). Upgrade path is the same one: the | |
| 1513 | + /// project's settings snapshotted beside the lanes. | |
| 1514 | + pub fn set_frame_rate(&mut self, fps: f64) -> bool { | |
| 1515 | + if !fps.is_finite() || fps <= 0.0 || fps == self.meta.frame_rate { | |
| 1516 | + return false; | |
| 1517 | + } | |
| 1518 | + // Old timeline frames per new one, exactly -- the one map every frame | |
| 1519 | + // number on every lane goes through. Built before anything is touched, | |
| 1520 | + // since a rate no timescale can name is a refusal and not a half-retimed | |
| 1521 | + // project. | |
| 1522 | + let Ok(k) = Rate::from_fps(self.meta.frame_rate, fps) else { | |
| 1523 | + return false; | |
| 1524 | + }; | |
| 1525 | + // Lengths first: the retime holds every clip inside its source's *new* | |
| 1526 | + // length, so a clip that played to the last frame of its file still | |
| 1527 | + // does rather than naming one past the end. | |
| 1528 | + for count in &mut self.counts { | |
| 1529 | + *count = k.timeline_at(*count).max(1); | |
| 1530 | + } | |
| 1531 | + self.project.retime(k, &self.counts); | |
| 1532 | + // A file's own rate has not changed; what it is *against* has. | |
| 1533 | + for rate in &mut self.rates { | |
| 1534 | + *rate = rate.then(k); | |
| 1535 | + } | |
| 1536 | + self.meta.frame_rate = fps; | |
| 1537 | + self.meta.frame_count = k.timeline_at(self.meta.frame_count).max(1); | |
| 1538 | + let now = self.now(); | |
| 1539 | + self.seek(now); | |
| 1540 | + true | |
| 1541 | + } | |
| 1542 | + | |
| 1452 | 1543 | /// How the clip at `idx` of `lane` meets a project canvas of another shape. |
| 1453 | 1544 | pub fn fit_of(&self, lane: Lane, idx: usize) -> FitPolicy { |
| 1454 | 1545 | self.project.fit_of(lane, idx) |
crates/engine/src/project.rsmodified+65 -2
| @@ -289,6 +289,11 @@ impl Rate { | ||
| 289 | 289 | // fps is scale/ticks, so the ratio is (src_scale/src_ticks) / (tl_scale/tl_ticks). |
| 290 | 290 | let num = u64::from(src_scale) * u64::from(tl_ticks); |
| 291 | 291 | let den = u64::from(src_ticks) * u64::from(tl_scale); |
| 292 | + Ok(Self::reduced(num, den)) | |
| 293 | + } | |
| 294 | + | |
| 295 | + /// `num` frames of a file per `den` frames of a timeline, in lowest terms. | |
| 296 | + fn reduced(num: u64, den: u64) -> Self { | |
| 292 | 297 | let g = gcd(num, den); |
| 293 | 298 | let (mut num, mut den) = (num / g, den / g); |
| 294 | 299 | // A ratio no pair of real frame rates produces, but the fields are `u32` |
| @@ -296,10 +301,22 @@ impl Rate { | ||
| 296 | 301 | while num > u64::from(u32::MAX) || den > u64::from(u32::MAX) { |
| 297 | 302 | (num, den) = (num.div_ceil(2), den.div_ceil(2)); |
| 298 | 303 | } |
| 299 | - Ok(Self { | |
| 304 | + Self { | |
| 300 | 305 | source: num.max(1) as u32, |
| 301 | 306 | timeline: den.max(1) as u32, |
| 302 | - }) | |
| 307 | + } | |
| 308 | + } | |
| 309 | + | |
| 310 | + /// The same file against a timeline that has itself been retimed: `self` is | |
| 311 | + /// its frames per frame of the old timeline and `k` is old frames per new | |
| 312 | + /// one, so this is its frames per frame of the *new* one -- exactly, without | |
| 313 | + /// going back through an `f64` fps that names neither rate | |
| 314 | + /// ([`crate::PlaybackSession::set_frame_rate`]). | |
| 315 | + pub fn then(self, k: Rate) -> Self { | |
| 316 | + Self::reduced( | |
| 317 | + u64::from(self.source) * u64::from(k.source), | |
| 318 | + u64::from(self.timeline) * u64::from(k.timeline), | |
| 319 | + ) | |
| 303 | 320 | } |
| 304 | 321 | |
| 305 | 322 | pub fn is_real_time(self) -> bool { |
| @@ -1423,6 +1440,52 @@ impl Project { | ||
| 1423 | 1440 | true |
| 1424 | 1441 | } |
| 1425 | 1442 | |
| 1443 | + /// Every frame number on every lane rewritten onto a timeline counted at | |
| 1444 | + /// another rate: `k` is old timeline frames per new one, `counts` is how | |
| 1445 | + /// long each source is on the *new* timeline (indexed as | |
| 1446 | + /// [`Project::sources`] is), and the edit survives as the same seconds of | |
| 1447 | + /// the same footage. What [`crate::PlaybackSession::set_frame_rate`] does to | |
| 1448 | + /// the edit list, and the only thing that touches the frame numbers without | |
| 1449 | + /// being an edit anyone made. | |
| 1450 | + /// | |
| 1451 | + /// Every position -- a clip's start and its end alike -- goes through the | |
| 1452 | + /// one map [`Rate::timeline_at`], so two clips that met still meet, a gap | |
| 1453 | + /// stays a gap, and a take's picture and its sound land on the same frame on | |
| 1454 | + /// their two lanes: no lane can drift against another. A clip is then only | |
| 1455 | + /// ever made to *fit* the room its own start and end mapped to, never to | |
| 1456 | + /// outgrow it, which is what keeps the lane sorted and disjoint (the cursor | |
| 1457 | + /// below is the backstop for the one case that could: a slow clip so short | |
| 1458 | + /// that no source range fits its new room at all). | |
| 1459 | + /// | |
| 1460 | + /// ponytail: not exact both ways. Each rate change re-rounds every boundary | |
| 1461 | + /// onto the new grid, so a rate picked, unpicked and picked again is within | |
| 1462 | + /// a frame of where it started rather than back at it. Upgrade path is | |
| 1463 | + /// keeping the edit list at one reference rate and conforming on the way | |
| 1464 | + /// out, which every frame number in this module would then be measured in. | |
| 1465 | + pub fn retime(&mut self, k: Rate, counts: &[u32]) { | |
| 1466 | + for lane in &mut self.lanes { | |
| 1467 | + let mut cursor = 0; | |
| 1468 | + for clip in &mut lane.clips { | |
| 1469 | + let start = k.timeline_at(clip.start).max(cursor); | |
| 1470 | + // The room its own end mapped to -- never zero, since a clip is | |
| 1471 | + // never empty even on a grid too coarse to hold it. | |
| 1472 | + let room = k.timeline_at(clip.end()).saturating_sub(start).max(1); | |
| 1473 | + let count = counts.get(clip.source).copied().unwrap_or(u32::MAX).max(1); | |
| 1474 | + // Its footage, held inside the file's new length: a clip whose | |
| 1475 | + // out-point was the last frame of its source must not come out | |
| 1476 | + // of this naming a frame past the end -- that is a project that | |
| 1477 | + // would not open again ([`crate::PlaybackSession::open_project`] | |
| 1478 | + // refuses one by name). | |
| 1479 | + let in_frame = k.timeline_at(clip.in_frame).min(count - 1); | |
| 1480 | + let len = clip.speed.fit(room).unwrap_or(1).clamp(1, count - in_frame); | |
| 1481 | + clip.start = start; | |
| 1482 | + clip.in_frame = in_frame; | |
| 1483 | + clip.out_frame = in_frame + len; | |
| 1484 | + cursor = clip.end(); | |
| 1485 | + } | |
| 1486 | + } | |
| 1487 | + } | |
| 1488 | + | |
| 1426 | 1489 | /// Length of the timeline in frames: where the *last* lane runs out. A lane |
| 1427 | 1490 | /// that ends early is a trailing gap in that lane, not a shorter timeline. |
| 1428 | 1491 | pub fn timeline_frames(&self) -> u32 { |
crates/engine/tests/mixed_fps.rsmodified+109 -0
| @@ -323,6 +323,115 @@ fn a_mixed_rate_project_reloads_unchanged() { | ||
| 323 | 323 | std::fs::remove_file(&path).ok(); |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | +/// The project's rate is the user's to pick, exactly as its resolution is. The | |
| 327 | +/// timeline is *conformed* to it -- the same seconds of the same footage, | |
| 328 | +/// counted on a new grid -- every source is read through a [`Rate`] against it, | |
| 329 | +/// the media's own rate stays reachable, and a save comes back at the rate it | |
| 330 | +/// was cut at rather than at the media's. | |
| 331 | +#[test] | |
| 332 | +fn the_project_rate_is_picked_and_the_timeline_is_conformed_to_it() { | |
| 333 | + let pal = asset("test_25fps.mp4"); | |
| 334 | + let mut session = open(asset("test_av.mp4")); | |
| 335 | + import_and_place(&mut session, &pal); | |
| 336 | + let duration = session.timeline_duration(); | |
| 337 | + assert_eq!(session.native_frame_rate(), FPS, "the media's own rate"); | |
| 338 | + assert!(!session.set_frame_rate(FPS), "the rate already in force"); | |
| 339 | + assert!(!session.set_frame_rate(0.0), "not a rate at all"); | |
| 340 | + | |
| 341 | + assert!( | |
| 342 | + session.set_frame_rate(24.0), | |
| 343 | + "24 is a rate a timescale can name" | |
| 344 | + ); | |
| 345 | + assert_eq!(session.meta().frame_rate, 24.0); | |
| 346 | + assert!( | |
| 347 | + (session.timeline_duration() - duration).abs() < 1.0 / 24.0, | |
| 348 | + "the timeline is {} s where it was {duration} s", | |
| 349 | + session.timeline_duration() | |
| 350 | + ); | |
| 351 | + // Both files are read through a rate against the new grid now: 5 s and 2 s, | |
| 352 | + // in 24ths. | |
| 353 | + assert_eq!(session.file_frames(&asset("test_av.mp4")), 120, "5 s at 24"); | |
| 354 | + assert_eq!(session.file_frames(&pal), 48, "2 s at 24"); | |
| 355 | + // ...and each take is still one take on both lanes: every frame number goes | |
| 356 | + // through one map, so a picture cannot drift off its sound. | |
| 357 | + let (video, audio) = (session.lane_clips(Lane::V1), session.lane_clips(Lane::A1)); | |
| 358 | + assert_eq!(video.len(), 2, "the 30 fps take and the 25 fps one"); | |
| 359 | + for (v, a) in video.iter().zip(audio) { | |
| 360 | + assert_eq!((v.start, v.len()), (a.start, a.len()), "{v:?} vs {a:?}"); | |
| 361 | + } | |
| 362 | + | |
| 363 | + let path = std::env::temp_dir().join(format!("edith-rate-{}.edith", std::process::id())); | |
| 364 | + session.save_project(&path).expect("save"); | |
| 365 | + let reloaded = PlaybackSession::open_project(&path).expect("a retimed project reopens"); | |
| 366 | + assert_eq!(reloaded.meta().frame_rate, 24.0, "the rate it was cut at"); | |
| 367 | + assert_eq!( | |
| 368 | + reloaded.clip_spans(), | |
| 369 | + session.clip_spans(), | |
| 370 | + "the same clips" | |
| 371 | + ); | |
| 372 | + assert_eq!(reloaded.file_frames(&pal), 48, "recomputed against 24"); | |
| 373 | + assert_eq!( | |
| 374 | + reloaded.native_frame_rate(), | |
| 375 | + FPS, | |
| 376 | + "the way back is still there" | |
| 377 | + ); | |
| 378 | + std::fs::remove_file(&path).ok(); | |
| 379 | + | |
| 380 | + // ...and that way back is a pick like any other: the media's own rate. | |
| 381 | + let native = session.native_frame_rate(); | |
| 382 | + assert!(session.set_frame_rate(native), "back to the media's own"); | |
| 383 | + assert_eq!(session.file_frames(&pal), 60, "2 s at 30 again"); | |
| 384 | + assert!( | |
| 385 | + (session.timeline_duration() - duration).abs() < 1.0 / 24.0, | |
| 386 | + "{} s after the round trip, {duration} s before", | |
| 387 | + session.timeline_duration() | |
| 388 | + ); | |
| 389 | +} | |
| 390 | + | |
| 391 | +/// ...and an export of it comes out at that rate: the file is written at 24 fps, | |
| 392 | +/// as many frames as the conformed timeline has, with its sound as long as its | |
| 393 | +/// picture. | |
| 394 | +#[test] | |
| 395 | +fn an_export_runs_at_the_rate_the_project_was_cut_at() { | |
| 396 | + let mut session = open(asset("test_av.mp4")); | |
| 397 | + assert!( | |
| 398 | + session.trim_clip(Lane::V1, 0, Edge::End, 30), | |
| 399 | + "one second of the 30 fps take" | |
| 400 | + ); | |
| 401 | + assert!(session.set_frame_rate(24.0), "cut it at 24 instead"); | |
| 402 | + let total = (session.timeline_duration() * 24.0).round() as u32; | |
| 403 | + assert_eq!(total, 24, "one second, in 24ths"); | |
| 404 | + | |
| 405 | + let out = std::env::temp_dir().join(format!("edith-rate-{}.mp4", std::process::id())); | |
| 406 | + wait(&session.export_to_with( | |
| 407 | + &out, | |
| 408 | + &ExportSettings { | |
| 409 | + format: Format::Mp4, | |
| 410 | + ..ExportSettings::default() | |
| 411 | + }, | |
| 412 | + )) | |
| 413 | + .expect("the export"); | |
| 414 | + | |
| 415 | + let (meta, _rx, _cancel) = DecodeSession::open_range(&out, 0, u32::MAX).expect("reopen"); | |
| 416 | + assert!( | |
| 417 | + (meta.frame_rate - 24.0).abs() < 0.01, | |
| 418 | + "the export runs at the picked rate: {}", | |
| 419 | + meta.frame_rate | |
| 420 | + ); | |
| 421 | + assert_eq!( | |
| 422 | + meta.frame_count, total, | |
| 423 | + "the export is the timeline's length" | |
| 424 | + ); | |
| 425 | + let secs = engine::AudioSession::duration_secs(&out) | |
| 426 | + .expect("probe the export") | |
| 427 | + .expect("the export has sound"); | |
| 428 | + assert!( | |
| 429 | + (secs - f64::from(total) / 24.0).abs() < 0.05, | |
| 430 | + "sound is {secs} s under a second of picture" | |
| 431 | + ); | |
| 432 | + std::fs::remove_file(&out).ok(); | |
| 433 | +} | |
| 434 | + | |
| 326 | 435 | /// The export is what was watched: as many frames as the timeline has, and the |
| 327 | 436 | /// 25 fps clip walked through its file at 25 fps -- 25 pictures over 30 frames, |
| 328 | 437 | /// not 30 of them and not 25 held to the end. |