| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | use std::path::{Path, PathBuf}; |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 48 | pub struct Clip { |
| 49 | |
| 50 | |
| 51 | |
| 52 | pub start: u32, |
| 53 | pub in_frame: u32, |
| 54 | pub out_frame: u32, |
| 55 | |
| 56 | pub source: usize, |
| 57 | |
| 58 | |
| 59 | pub link: Option<u32>, |
| 60 | } |
| 61 | |
| 62 | impl Clip { |
| 63 | |
| 64 | pub fn len(&self) -> u32 { |
| 65 | self.out_frame - self.in_frame |
| 66 | } |
| 67 | |
| 68 | |
| 69 | pub fn end(&self) -> u32 { |
| 70 | self.start + self.len() |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 77 | pub enum Lane { |
| 78 | Video, |
| 79 | Audio, |
| 80 | } |
| 81 | |
| 82 | impl Lane { |
| 83 | pub const ALL: [Lane; 2] = [Lane::Video, Lane::Audio]; |
| 84 | |
| 85 | fn index(self) -> usize { |
| 86 | self as usize |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 94 | pub struct Span { |
| 95 | |
| 96 | pub start: u32, |
| 97 | |
| 98 | pub len: u32, |
| 99 | |
| 100 | |
| 101 | pub from: Option<(usize, u32)>, |
| 102 | } |
| 103 | |
| 104 | impl Span { |
| 105 | |
| 106 | pub fn end(&self) -> u32 { |
| 107 | self.start + self.len |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | #[derive(Clone, Debug)] |
| 113 | pub struct Project { |
| 114 | |
| 115 | sources: Vec<PathBuf>, |
| 116 | |
| 117 | lanes: [Vec<Clip>; 2], |
| 118 | |
| 119 | history: Vec<[Vec<Clip>; 2]>, |
| 120 | |
| 121 | |
| 122 | next_link: u32, |
| 123 | } |
| 124 | |
| 125 | impl Project { |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | pub fn single(path: impl AsRef<Path>, frame_count: u32) -> Self { |
| 131 | let clip = Clip { |
| 132 | start: 0, |
| 133 | in_frame: 0, |
| 134 | out_frame: frame_count.max(1), |
| 135 | source: 0, |
| 136 | link: Some(0), |
| 137 | }; |
| 138 | Self { |
| 139 | sources: vec![canonical(path.as_ref())], |
| 140 | lanes: [vec![clip], vec![clip]], |
| 141 | history: Vec::new(), |
| 142 | next_link: 1, |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | pub fn from_parts( |
| 155 | sources: Vec<PathBuf>, |
| 156 | video: Vec<Clip>, |
| 157 | audio: Vec<Clip>, |
| 158 | ) -> crate::Result<Self> { |
| 159 | if video.is_empty() && audio.is_empty() { |
| 160 | return Err("both lanes are empty: that is not a project".into()); |
| 161 | } |
| 162 | let lanes = [video, audio]; |
| 163 | for (lane, name) in lanes.iter().zip(["video", "audio"]) { |
| 164 | for c in lane { |
| 165 | if c.out_frame <= c.in_frame { |
| 166 | return Err(format!( |
| 167 | "{name} clip at {} plays the empty range [{}, {})", |
| 168 | c.start, c.in_frame, c.out_frame |
| 169 | ) |
| 170 | .into()); |
| 171 | } |
| 172 | if c.source >= sources.len() { |
| 173 | return Err(format!( |
| 174 | "{name} clip at {} names source {} of {}", |
| 175 | c.start, |
| 176 | c.source, |
| 177 | sources.len() |
| 178 | ) |
| 179 | .into()); |
| 180 | } |
| 181 | if c.start.checked_add(c.len()).is_none() { |
| 182 | return Err(format!( |
| 183 | "{name} clip at {} runs past the last frame there is", |
| 184 | c.start |
| 185 | ) |
| 186 | .into()); |
| 187 | } |
| 188 | } |
| 189 | if !sorted_disjoint(lane) { |
| 190 | return Err(format!("the {name} lane is out of order or overlaps itself").into()); |
| 191 | } |
| 192 | } |
| 193 | links_are_consistent(&lanes)?; |
| 194 | let next_link = lanes |
| 195 | .iter() |
| 196 | .flatten() |
| 197 | .filter_map(|c| c.link) |
| 198 | .max() |
| 199 | |
| 200 | |
| 201 | .map_or(0, |m| m.saturating_add(1)); |
| 202 | Ok(Self { |
| 203 | sources: sources.iter().map(|s| canonical(s)).collect(), |
| 204 | lanes, |
| 205 | history: Vec::new(), |
| 206 | next_link, |
| 207 | }) |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | pub fn clips(&self) -> &[Clip] { |
| 213 | self.lane(Lane::Video) |
| 214 | } |
| 215 | |
| 216 | pub fn lane(&self, lane: Lane) -> &[Clip] { |
| 217 | &self.lanes[lane.index()] |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | pub fn sources(&self) -> &[PathBuf] { |
| 223 | &self.sources |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | pub fn import(&mut self, path: impl AsRef<Path>) -> usize { |
| 231 | let path = canonical(path.as_ref()); |
| 232 | match self.sources.iter().position(|s| *s == path) { |
| 233 | Some(idx) => idx, |
| 234 | None => { |
| 235 | self.sources.push(path); |
| 236 | self.sources.len() - 1 |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | pub fn append_clip(&mut self, source: usize, frame_count: u32) -> bool { |
| 246 | if source >= self.sources.len() { |
| 247 | return false; |
| 248 | } |
| 249 | self.snapshot(); |
| 250 | let start = self.timeline_frames(); |
| 251 | let clip = Clip { |
| 252 | start, |
| 253 | in_frame: 0, |
| 254 | out_frame: frame_count.max(1), |
| 255 | source, |
| 256 | link: Some(self.new_link()), |
| 257 | }; |
| 258 | for lane in &mut self.lanes { |
| 259 | lane.push(clip); |
| 260 | } |
| 261 | true |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | pub fn without_orphan_sources(&self) -> (Vec<PathBuf>, Vec<Clip>, Vec<Clip>) { |
| 272 | let mut moved = vec![None; self.sources.len()]; |
| 273 | let mut sources = Vec::new(); |
| 274 | let mut lanes = self.lanes.clone(); |
| 275 | for c in lanes.iter_mut().flatten() { |
| 276 | let old = c.source; |
| 277 | c.source = match moved[old] { |
| 278 | Some(new) => new, |
| 279 | None => { |
| 280 | sources.push(self.sources[old].clone()); |
| 281 | moved[old] = Some(sources.len() - 1); |
| 282 | sources.len() - 1 |
| 283 | } |
| 284 | }; |
| 285 | } |
| 286 | let [video, audio] = lanes; |
| 287 | (sources, video, audio) |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | pub fn timeline_frames(&self) -> u32 { |
| 293 | self.lanes |
| 294 | .iter() |
| 295 | .filter_map(|lane| lane.last().map(Clip::end)) |
| 296 | .max() |
| 297 | .unwrap_or(0) |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
| 302 | pub fn clip_spans(&self) -> Vec<(u32, u32)> { |
| 303 | self.lane_spans(Lane::Video) |
| 304 | } |
| 305 | |
| 306 | pub fn lane_spans(&self, lane: Lane) -> Vec<(u32, u32)> { |
| 307 | self.lane(lane).iter().map(|c| (c.start, c.len())).collect() |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | pub fn map(&self, lane: Lane, timeline_frame: u32) -> Option<(usize, u32)> { |
| 314 | let clips = self.lane(lane); |
| 315 | let idx = at(clips, timeline_frame)?; |
| 316 | Some(( |
| 317 | idx, |
| 318 | clips[idx].in_frame + (timeline_frame - clips[idx].start), |
| 319 | )) |
| 320 | } |
| 321 | |
| 322 | |
| 323 | pub fn map_timeline(&self, timeline_frame: u32) -> Option<(usize, u32)> { |
| 324 | self.map(Lane::Video, timeline_frame) |
| 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | pub fn span_at(&self, lane: Lane, timeline_frame: u32) -> Option<Span> { |
| 333 | let total = self.timeline_frames(); |
| 334 | if timeline_frame >= total { |
| 335 | return None; |
| 336 | } |
| 337 | let clips = self.lane(lane); |
| 338 | Some(match at(clips, timeline_frame) { |
| 339 | Some(idx) => Span { |
| 340 | start: timeline_frame, |
| 341 | len: clips[idx].end() - timeline_frame, |
| 342 | from: Some(( |
| 343 | clips[idx].source, |
| 344 | clips[idx].in_frame + (timeline_frame - clips[idx].start), |
| 345 | )), |
| 346 | }, |
| 347 | None => Span { |
| 348 | start: timeline_frame, |
| 349 | |
| 350 | |
| 351 | len: clips |
| 352 | .iter() |
| 353 | .find(|c| c.start > timeline_frame) |
| 354 | .map_or(total, |c| c.start) |
| 355 | - timeline_frame, |
| 356 | from: None, |
| 357 | }, |
| 358 | }) |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | pub fn spans_from(&self, lane: Lane, timeline_frame: u32) -> Vec<Span> { |
| 364 | let mut out = Vec::new(); |
| 365 | let mut t = timeline_frame; |
| 366 | while let Some(span) = self.span_at(lane, t) { |
| 367 | t = span.end(); |
| 368 | out.push(span); |
| 369 | } |
| 370 | out |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | pub fn split(&mut self, timeline_frame: u32) -> bool { |
| 387 | let cut = Lane::ALL |
| 388 | .map(|l| splittable(self.lane(l), timeline_frame).map(|idx| self.lane(l)[idx])); |
| 389 | if cut.iter().all(Option::is_none) { |
| 390 | return false; |
| 391 | } |
| 392 | self.snapshot(); |
| 393 | let same = |f: fn(&Clip) -> u32| match cut { |
| 394 | [Some(a), Some(b)] => f(&a) == f(&b), |
| 395 | _ => false, |
| 396 | }; |
| 397 | let (left, right) = (self.new_link(), self.new_link()); |
| 398 | |
| 399 | |
| 400 | let ids = [ |
| 401 | (left, right), |
| 402 | ( |
| 403 | if same(|c| c.start) { |
| 404 | left |
| 405 | } else { |
| 406 | self.new_link() |
| 407 | }, |
| 408 | if same(Clip::end) { |
| 409 | right |
| 410 | } else { |
| 411 | self.new_link() |
| 412 | }, |
| 413 | ), |
| 414 | ]; |
| 415 | for (lane, (left, right)) in self.lanes.iter_mut().zip(ids) { |
| 416 | let Some(idx) = splittable(lane, timeline_frame) else { |
| 417 | continue; |
| 418 | }; |
| 419 | let mut tail = lane[idx]; |
| 420 | tail.in_frame += timeline_frame - tail.start; |
| 421 | tail.start = timeline_frame; |
| 422 | tail.link = Some(right); |
| 423 | lane[idx].out_frame = tail.in_frame; |
| 424 | lane[idx].link = Some(left); |
| 425 | lane.insert(idx + 1, tail); |
| 426 | } |
| 427 | true |
| 428 | } |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | pub fn regroup(&mut self, timeline_frame: u32) -> bool { |
| 439 | |
| 440 | let joined = Lane::ALL.map(|l| { |
| 441 | joinable(self.lane(l), timeline_frame) |
| 442 | .map(|idx| (self.lane(l)[idx].start, self.lane(l)[idx + 1].end())) |
| 443 | }); |
| 444 | if joined.iter().all(Option::is_none) { |
| 445 | return false; |
| 446 | } |
| 447 | self.snapshot(); |
| 448 | let link = self.new_link(); |
| 449 | let ids = [ |
| 450 | link, |
| 451 | match joined { |
| 452 | [Some(a), Some(b)] if a == b => link, |
| 453 | _ => self.new_link(), |
| 454 | }, |
| 455 | ]; |
| 456 | for (lane, link) in self.lanes.iter_mut().zip(ids) { |
| 457 | let Some(idx) = joinable(lane, timeline_frame) else { |
| 458 | continue; |
| 459 | }; |
| 460 | lane[idx].out_frame = lane[idx + 1].out_frame; |
| 461 | lane[idx].link = Some(link); |
| 462 | lane.remove(idx + 1); |
| 463 | } |
| 464 | true |
| 465 | } |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | pub fn place(&mut self, lane: Lane, timeline_frame: u32, clip: Clip) -> bool { |
| 482 | if clip.out_frame <= clip.in_frame { |
| 483 | return false; |
| 484 | } |
| 485 | self.snapshot(); |
| 486 | let clip = Clip { |
| 487 | start: timeline_frame, |
| 488 | link: None, |
| 489 | ..clip |
| 490 | }; |
| 491 | let lane = &mut self.lanes[lane.index()]; |
| 492 | clear(lane, clip.start, clip.end()); |
| 493 | let idx = lane.partition_point(|c| c.start < clip.start); |
| 494 | lane.insert(idx, clip); |
| 495 | debug_assert!(sorted_disjoint(lane)); |
| 496 | true |
| 497 | } |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | pub fn paste(&mut self, timeline_frame: u32, clip: Clip) -> bool { |
| 511 | if clip.out_frame <= clip.in_frame { |
| 512 | return false; |
| 513 | } |
| 514 | self.snapshot(); |
| 515 | let at = timeline_frame.min(self.timeline_frames()); |
| 516 | let clip = Clip { |
| 517 | start: at, |
| 518 | link: Some(self.new_link()), |
| 519 | ..clip |
| 520 | }; |
| 521 | for lane in &mut self.lanes { |
| 522 | open_room(lane, at, clip.len()); |
| 523 | let idx = lane.partition_point(|c| c.start < at); |
| 524 | lane.insert(idx, clip); |
| 525 | debug_assert!(sorted_disjoint(lane)); |
| 526 | } |
| 527 | true |
| 528 | } |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | pub fn lift(&mut self, lane: Lane, idx: usize) -> bool { |
| 535 | let placed: usize = self.lanes.iter().map(Vec::len).sum(); |
| 536 | if idx >= self.lane(lane).len() || placed == 1 { |
| 537 | return false; |
| 538 | } |
| 539 | self.snapshot(); |
| 540 | self.lanes[lane.index()].remove(idx); |
| 541 | true |
| 542 | } |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool { |
| 549 | if len == 0 { |
| 550 | return false; |
| 551 | } |
| 552 | let survivors: usize = self |
| 553 | .lanes |
| 554 | .iter() |
| 555 | .map(|lane| { |
| 556 | lane.iter() |
| 557 | .filter(|c| c.start < at || c.end() > at + len) |
| 558 | .count() |
| 559 | }) |
| 560 | .sum(); |
| 561 | if survivors == 0 { |
| 562 | return false; |
| 563 | } |
| 564 | self.snapshot(); |
| 565 | for lane in &mut self.lanes { |
| 566 | clear(lane, at, at + len); |
| 567 | for c in lane.iter_mut().filter(|c| c.start >= at) { |
| 568 | c.start -= len; |
| 569 | } |
| 570 | debug_assert!(sorted_disjoint(lane)); |
| 571 | } |
| 572 | true |
| 573 | } |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | pub fn delete(&mut self, idx: usize) -> bool { |
| 579 | let Some(clip) = self.clips().get(idx).copied() else { |
| 580 | return false; |
| 581 | }; |
| 582 | self.ripple_delete(clip.start, clip.len()) |
| 583 | } |
| 584 | |
| 585 | |
| 586 | |
| 587 | pub fn undo(&mut self) -> bool { |
| 588 | match self.history.pop() { |
| 589 | Some(prev) => { |
| 590 | self.lanes = prev; |
| 591 | true |
| 592 | } |
| 593 | None => false, |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | pub fn segments_from(&self, timeline_frame: u32, fps: f64) -> Vec<(Option<usize>, f64, f64)> { |
| 605 | if !(fps.is_finite() && fps > 0.0) { |
| 606 | return Vec::new(); |
| 607 | } |
| 608 | self.spans_from(Lane::Audio, timeline_frame) |
| 609 | .iter() |
| 610 | .map(|span| match span.from { |
| 611 | |
| 612 | |
| 613 | Some((source, in_frame)) => ( |
| 614 | Some(source), |
| 615 | f64::from(in_frame) / fps, |
| 616 | f64::from(in_frame + span.len) / fps, |
| 617 | ), |
| 618 | |
| 619 | None => (None, 0.0, f64::from(span.len) / fps), |
| 620 | }) |
| 621 | .collect() |
| 622 | } |
| 623 | |
| 624 | |
| 625 | |
| 626 | fn snapshot(&mut self) { |
| 627 | self.history.push(self.lanes.clone()); |
| 628 | } |
| 629 | |
| 630 | fn new_link(&mut self) -> u32 { |
| 631 | let id = self.next_link; |
| 632 | self.next_link = self.next_link.saturating_add(1); |
| 633 | id |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | |
| 638 | |
| 639 | fn at(clips: &[Clip], frame: u32) -> Option<usize> { |
| 640 | let idx = clips.partition_point(|c| c.start <= frame).checked_sub(1)?; |
| 641 | (frame < clips[idx].end()).then_some(idx) |
| 642 | } |
| 643 | |
| 644 | |
| 645 | |
| 646 | fn splittable(clips: &[Clip], frame: u32) -> Option<usize> { |
| 647 | let idx = at(clips, frame)?; |
| 648 | (frame > clips[idx].start).then_some(idx) |
| 649 | } |
| 650 | |
| 651 | |
| 652 | |
| 653 | fn joinable(clips: &[Clip], frame: u32) -> Option<usize> { |
| 654 | let idx = clips.iter().position(|c| c.end() == frame)?; |
| 655 | let next = clips.get(idx + 1)?; |
| 656 | (next.start == frame |
| 657 | && next.source == clips[idx].source |
| 658 | && next.in_frame == clips[idx].out_frame) |
| 659 | .then_some(idx) |
| 660 | } |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | fn sorted_disjoint(clips: &[Clip]) -> bool { |
| 666 | clips.iter().all(|c| c.out_frame > c.in_frame) |
| 667 | && clips.windows(2).all(|w| w[0].end() <= w[1].start) |
| 668 | } |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | fn links_are_consistent(lanes: &[Vec<Clip>; 2]) -> crate::Result<()> { |
| 678 | let find = |lane: &Vec<Clip>, id: u32| lane.iter().find(|c| c.link == Some(id)).copied(); |
| 679 | for (lane, name) in lanes.iter().zip(["video", "audio"]) { |
| 680 | for (i, c) in lane.iter().enumerate() { |
| 681 | let Some(id) = c.link else { continue }; |
| 682 | if lane[..i].iter().any(|prev| prev.link == Some(id)) { |
| 683 | return Err(format!("link {id} names two clips in the {name} lane").into()); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | for a in lanes[0].iter().filter(|c| c.link.is_some()) { |
| 688 | let id = a.link.expect("filtered"); |
| 689 | let Some(b) = find(&lanes[1], id) else { |
| 690 | continue; |
| 691 | }; |
| 692 | if (a.start, a.end()) != (b.start, b.end()) { |
| 693 | return Err(format!( |
| 694 | "link {id} covers [{}, {}) in video and [{}, {}) in audio", |
| 695 | a.start, |
| 696 | a.end(), |
| 697 | b.start, |
| 698 | b.end() |
| 699 | ) |
| 700 | .into()); |
| 701 | } |
| 702 | } |
| 703 | Ok(()) |
| 704 | } |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | fn clear(clips: &mut Vec<Clip>, start: u32, end: u32) { |
| 715 | let mut out = Vec::with_capacity(clips.len() + 1); |
| 716 | for c in clips.drain(..) { |
| 717 | |
| 718 | if c.end() <= start || c.start >= end { |
| 719 | out.push(c); |
| 720 | continue; |
| 721 | } |
| 722 | |
| 723 | if c.start < start { |
| 724 | out.push(Clip { |
| 725 | out_frame: c.in_frame + (start - c.start), |
| 726 | link: None, |
| 727 | ..c |
| 728 | }); |
| 729 | } |
| 730 | |
| 731 | if c.end() > end { |
| 732 | out.push(Clip { |
| 733 | start: end, |
| 734 | in_frame: c.in_frame + (end - c.start), |
| 735 | link: None, |
| 736 | ..c |
| 737 | }); |
| 738 | } |
| 739 | } |
| 740 | *clips = out; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | fn open_room(clips: &mut Vec<Clip>, at: u32, len: u32) { |
| 748 | if let Some(idx) = splittable(clips, at) { |
| 749 | let mut tail = clips[idx]; |
| 750 | tail.in_frame += at - tail.start; |
| 751 | tail.start = at; |
| 752 | tail.link = None; |
| 753 | clips[idx].out_frame = tail.in_frame; |
| 754 | clips[idx].link = None; |
| 755 | clips.insert(idx + 1, tail); |
| 756 | } |
| 757 | for c in clips.iter_mut().filter(|c| c.start >= at) { |
| 758 | c.start += len; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | fn canonical(path: &Path) -> PathBuf { |
| 766 | std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) |
| 767 | } |
| 768 | |
| 769 | #[cfg(test)] |
| 770 | mod tests { |
| 771 | use super::*; |
| 772 | |
| 773 | const FPS: f64 = 30.0; |
| 774 | |
| 775 | |
| 776 | const FILE: &str = "/nonexistent/a.mp4"; |
| 777 | const FILE2: &str = "/nonexistent/b.mp4"; |
| 778 | |
| 779 | |
| 780 | fn clip(start: u32, in_frame: u32, out_frame: u32, source: usize) -> Clip { |
| 781 | Clip { |
| 782 | start, |
| 783 | in_frame, |
| 784 | out_frame, |
| 785 | source, |
| 786 | link: None, |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | |
| 791 | fn shape(p: &Project) -> [Vec<Clip>; 2] { |
| 792 | Lane::ALL.map(|l| { |
| 793 | p.lane(l) |
| 794 | .iter() |
| 795 | .map(|c| Clip { link: None, ..*c }) |
| 796 | .collect() |
| 797 | }) |
| 798 | } |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | fn traversal(p: &Project) -> Vec<[Option<u32>; 2]> { |
| 804 | (0..p.timeline_frames() + 2) |
| 805 | .map(|f| Lane::ALL.map(|l| p.map(l, f).map(|(_, source)| source))) |
| 806 | .collect() |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | fn three() -> Project { |
| 812 | let mut p = Project::single(FILE, 9); |
| 813 | assert!(p.split(3)); |
| 814 | assert!(p.split(5)); |
| 815 | assert_eq!( |
| 816 | shape(&p), |
| 817 | [ |
| 818 | vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 9, 0)], |
| 819 | vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 9, 0)], |
| 820 | ] |
| 821 | ); |
| 822 | p |
| 823 | } |
| 824 | |
| 825 | #[test] |
| 826 | fn single_is_the_whole_file_on_both_lanes() { |
| 827 | let p = Project::single(FILE, 150); |
| 828 | assert_eq!(p.clips().len(), 1); |
| 829 | assert_eq!(p.lane(Lane::Audio).len(), 1); |
| 830 | assert_eq!(p.timeline_frames(), 150); |
| 831 | assert_eq!(p.clip_spans(), vec![(0, 150)]); |
| 832 | |
| 833 | assert_eq!(p.clips()[0].link, p.lane(Lane::Audio)[0].link); |
| 834 | assert!(p.clips()[0].link.is_some()); |
| 835 | |
| 836 | assert_eq!(p.map_timeline(0), Some((0, 0))); |
| 837 | assert_eq!(p.map_timeline(149), Some((0, 149))); |
| 838 | assert_eq!(p.map_timeline(150), None); |
| 839 | |
| 840 | assert_eq!(Project::single(FILE, 0).timeline_frames(), 1); |
| 841 | } |
| 842 | |
| 843 | #[test] |
| 844 | fn split_cuts_every_lane_and_regroups_it_back() { |
| 845 | let mut p = Project::single(FILE, 9); |
| 846 | let before = traversal(&p); |
| 847 | let before_shape = shape(&p); |
| 848 | |
| 849 | assert!(p.split(4)); |
| 850 | for l in Lane::ALL { |
| 851 | assert_eq!(p.lane(l).len(), 2, "{l:?} split"); |
| 852 | } |
| 853 | |
| 854 | let (v, a) = (p.lane(Lane::Video), p.lane(Lane::Audio)); |
| 855 | assert_eq!(v[0].link, a[0].link); |
| 856 | assert_eq!(v[1].link, a[1].link); |
| 857 | assert_ne!(v[0].link, v[1].link, "the halves are no longer one take"); |
| 858 | |
| 859 | assert_eq!(traversal(&p), before, "a split moves nothing"); |
| 860 | |
| 861 | assert!(p.regroup(4), "the inverse rejoins them"); |
| 862 | assert_eq!(shape(&p), before_shape, "bit-exact back to one clip"); |
| 863 | assert_eq!(traversal(&p), before); |
| 864 | assert_eq!( |
| 865 | p.lane(Lane::Video)[0].link, |
| 866 | p.lane(Lane::Audio)[0].link, |
| 867 | "and the rejoined clip is one group again" |
| 868 | ); |
| 869 | } |
| 870 | |
| 871 | #[test] |
| 872 | fn split_refused_at_zero_boundary_end_and_in_a_gap() { |
| 873 | let mut p = three(); |
| 874 | let before = shape(&p); |
| 875 | assert!(!p.split(0), "timeline 0 has nothing before it"); |
| 876 | assert!(!p.split(3), "existing boundary"); |
| 877 | assert!(!p.split(5), "existing boundary"); |
| 878 | assert!(!p.split(9), "one past the last frame"); |
| 879 | assert!(!p.split(1_000)); |
| 880 | assert_eq!(shape(&p), before); |
| 881 | |
| 882 | assert!(p.undo()); |
| 883 | assert_eq!(p.clips().len(), 2, "refused splits push no history"); |
| 884 | |
| 885 | |
| 886 | let mut p = three(); |
| 887 | assert!(p.lift(Lane::Video, 1)); |
| 888 | assert!(p.lift(Lane::Audio, 1)); |
| 889 | assert!(!p.split(4), "a gap is not a clip"); |
| 890 | } |
| 891 | |
| 892 | #[test] |
| 893 | fn regroup_refuses_what_a_split_could_not_have_made() { |
| 894 | let mut p = three(); |
| 895 | |
| 896 | assert!(p.regroup(5)); |
| 897 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 9, 0)]); |
| 898 | |
| 899 | |
| 900 | let mut p = three(); |
| 901 | assert!(p.lift(Lane::Video, 1)); |
| 902 | assert!(p.lift(Lane::Audio, 1)); |
| 903 | assert!(!p.regroup(3), "nothing starts at 3 any more"); |
| 904 | let mut p = three(); |
| 905 | for l in Lane::ALL { |
| 906 | assert!(p.place(l, 3, clip(0, 100, 102, 0))); |
| 907 | } |
| 908 | assert!(!p.regroup(3), "source 100 does not follow source 3"); |
| 909 | assert!(!p.regroup(0), "nothing ends at 0"); |
| 910 | assert!(!p.regroup(1_000)); |
| 911 | } |
| 912 | |
| 913 | #[test] |
| 914 | fn map_and_spans_sweep_every_boundary() { |
| 915 | let p = three(); |
| 916 | assert_eq!(p.timeline_frames(), 9); |
| 917 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4)]); |
| 918 | |
| 919 | let expect = [ |
| 920 | (0, (0, 0)), |
| 921 | (1, (0, 1)), |
| 922 | (2, (0, 2)), |
| 923 | (3, (1, 3)), |
| 924 | (4, (1, 4)), |
| 925 | (5, (2, 5)), |
| 926 | (6, (2, 6)), |
| 927 | (7, (2, 7)), |
| 928 | (8, (2, 8)), |
| 929 | ]; |
| 930 | for (t, want) in expect { |
| 931 | assert_eq!(p.map_timeline(t), Some(want), "timeline frame {t}"); |
| 932 | } |
| 933 | assert_eq!(p.map_timeline(9), None); |
| 934 | |
| 935 | assert_eq!( |
| 936 | p.span_at(Lane::Video, 6), |
| 937 | Some(Span { |
| 938 | start: 6, |
| 939 | len: 3, |
| 940 | from: Some((0, 6)) |
| 941 | }) |
| 942 | ); |
| 943 | assert_eq!(p.span_at(Lane::Video, 9), None); |
| 944 | } |
| 945 | |
| 946 | |
| 947 | |
| 948 | const PASTED: Clip = Clip { |
| 949 | start: 0, |
| 950 | in_frame: 100, |
| 951 | out_frame: 102, |
| 952 | source: 0, |
| 953 | link: None, |
| 954 | }; |
| 955 | |
| 956 | #[test] |
| 957 | fn paste_mid_clip_splits_around_it() { |
| 958 | let mut p = three(); |
| 959 | assert!(p.paste(6, PASTED)); |
| 960 | assert_eq!(p.timeline_frames(), 11); |
| 961 | assert_eq!( |
| 962 | shape(&p)[0], |
| 963 | vec![ |
| 964 | clip(0, 0, 3, 0), |
| 965 | clip(3, 3, 5, 0), |
| 966 | clip(5, 5, 6, 0), |
| 967 | clip(6, 100, 102, 0), |
| 968 | clip(8, 6, 9, 0), |
| 969 | ] |
| 970 | ); |
| 971 | assert_eq!(shape(&p)[1], shape(&p)[0], "a paste lands on both lanes"); |
| 972 | |
| 973 | |
| 974 | assert_eq!(p.map_timeline(6), Some((3, 100))); |
| 975 | assert_eq!(p.map_timeline(7), Some((3, 101))); |
| 976 | assert_eq!(p.map_timeline(8), Some((4, 6))); |
| 977 | } |
| 978 | |
| 979 | #[test] |
| 980 | fn paste_at_boundary_zero_and_end() { |
| 981 | let mut p = three(); |
| 982 | assert!(p.paste(3, PASTED), "existing boundary: no split"); |
| 983 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 2), (7, 4)]); |
| 984 | assert_eq!(p.map_timeline(3), Some((1, 100))); |
| 985 | |
| 986 | let mut p = three(); |
| 987 | assert!(p.paste(0, PASTED)); |
| 988 | assert_eq!(p.map_timeline(0), Some((0, 100))); |
| 989 | assert_eq!(p.map_timeline(2), Some((1, 0))); |
| 990 | |
| 991 | |
| 992 | |
| 993 | let mut p = three(); |
| 994 | assert!(p.paste(9, PASTED)); |
| 995 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4), (9, 2)]); |
| 996 | assert!(p.paste(1_000, PASTED)); |
| 997 | assert_eq!(p.timeline_frames(), 13); |
| 998 | |
| 999 | |
| 1000 | let mut p = three(); |
| 1001 | assert!(!p.paste(4, clip(0, 7, 7, 0))); |
| 1002 | assert_eq!(p.clips().len(), 3); |
| 1003 | } |
| 1004 | |
| 1005 | #[test] |
| 1006 | fn paste_undoes_in_one_step() { |
| 1007 | let mut p = three(); |
| 1008 | let before = shape(&p); |
| 1009 | assert!(p.paste(6, PASTED)); |
| 1010 | assert!(p.undo()); |
| 1011 | assert_eq!(shape(&p), before, "a paste is one undo step"); |
| 1012 | } |
| 1013 | |
| 1014 | #[test] |
| 1015 | fn a_copy_outlives_the_clip_it_came_from() { |
| 1016 | let mut p = three(); |
| 1017 | let copied = p.clips()[1]; |
| 1018 | assert!(p.delete(1)); |
| 1019 | |
| 1020 | assert!(p.paste(0, copied)); |
| 1021 | assert_eq!(p.map_timeline(0), Some((0, 3))); |
| 1022 | assert_eq!(p.timeline_frames(), 9); |
| 1023 | } |
| 1024 | |
| 1025 | #[test] |
| 1026 | fn delete_closes_the_gap_on_both_lanes() { |
| 1027 | let mut p = three(); |
| 1028 | assert!(p.delete(1)); |
| 1029 | assert_eq!(p.timeline_frames(), 7); |
| 1030 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 4)]); |
| 1031 | assert_eq!(p.lane_spans(Lane::Audio), vec![(0, 3), (3, 4)]); |
| 1032 | |
| 1033 | assert_eq!(p.map_timeline(3), Some((1, 5))); |
| 1034 | assert_eq!(p.map_timeline(6), Some((1, 8))); |
| 1035 | assert_eq!(p.map_timeline(7), None); |
| 1036 | } |
| 1037 | |
| 1038 | #[test] |
| 1039 | fn delete_first_last_and_only() { |
| 1040 | let mut p = three(); |
| 1041 | assert!(p.delete(0)); |
| 1042 | assert_eq!(p.map_timeline(0), Some((0, 3))); |
| 1043 | assert_eq!(p.timeline_frames(), 6); |
| 1044 | |
| 1045 | let mut p = three(); |
| 1046 | assert!(p.delete(2)); |
| 1047 | assert_eq!(p.timeline_frames(), 5); |
| 1048 | assert_eq!(p.map_timeline(4), Some((1, 4))); |
| 1049 | |
| 1050 | assert!(!p.delete(2), "index past the end"); |
| 1051 | assert!(p.delete(1)); |
| 1052 | assert!(!p.delete(0), "the last remaining clip stays"); |
| 1053 | assert_eq!(p.clips().len(), 1); |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | #[test] |
| 1058 | fn a_lift_leaves_a_gap_and_moves_nothing() { |
| 1059 | let mut p = three(); |
| 1060 | let video_before = shape(&p)[0].clone(); |
| 1061 | assert!(p.lift(Lane::Audio, 1)); |
| 1062 | assert_eq!(shape(&p)[0], video_before, "the picture never moved"); |
| 1063 | assert_eq!(p.timeline_frames(), 9, "nor did the timeline get shorter"); |
| 1064 | assert_eq!(p.lane_spans(Lane::Audio), vec![(0, 3), (5, 4)]); |
| 1065 | |
| 1066 | assert_eq!(p.map(Lane::Audio, 4), None); |
| 1067 | assert_eq!( |
| 1068 | p.span_at(Lane::Audio, 3), |
| 1069 | Some(Span { |
| 1070 | start: 3, |
| 1071 | len: 2, |
| 1072 | from: None |
| 1073 | }) |
| 1074 | ); |
| 1075 | assert_eq!(p.map(Lane::Video, 4), Some((1, 4)), "video plays on"); |
| 1076 | assert!(p.undo()); |
| 1077 | assert_eq!(p.lane_spans(Lane::Audio), vec![(0, 3), (3, 2), (5, 4)]); |
| 1078 | } |
| 1079 | |
| 1080 | #[test] |
| 1081 | fn a_trailing_gap_is_a_gap_to_the_end_of_the_timeline() { |
| 1082 | let mut p = three(); |
| 1083 | assert!(p.lift(Lane::Audio, 2)); |
| 1084 | assert_eq!(p.timeline_frames(), 9, "video still runs to 9"); |
| 1085 | assert_eq!( |
| 1086 | p.span_at(Lane::Audio, 5), |
| 1087 | Some(Span { |
| 1088 | start: 5, |
| 1089 | len: 4, |
| 1090 | from: None |
| 1091 | }), |
| 1092 | "the audio lane holds silence to the end of the picture" |
| 1093 | ); |
| 1094 | assert_eq!(p.span_at(Lane::Audio, 9), None); |
| 1095 | |
| 1096 | for l in Lane::ALL { |
| 1097 | let spans = p.spans_from(l, 0); |
| 1098 | assert_eq!(spans.iter().map(|s| s.len).sum::<u32>(), 9, "{l:?}"); |
| 1099 | assert_eq!(spans[0].start, 0); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | #[test] |
| 1104 | fn the_last_clip_of_the_last_lane_cannot_be_lifted() { |
| 1105 | let mut p = Project::single(FILE, 9); |
| 1106 | assert!(p.lift(Lane::Audio, 0), "a silent timeline is fine"); |
| 1107 | assert!(!p.lift(Lane::Video, 0), "an empty one is not"); |
| 1108 | assert!(!p.lift(Lane::Audio, 0), "index past the end"); |
| 1109 | assert_eq!(p.timeline_frames(), 9); |
| 1110 | } |
| 1111 | |
| 1112 | #[test] |
| 1113 | fn place_overwrites_only_its_own_lane() { |
| 1114 | let mut p = three(); |
| 1115 | let audio_before = shape(&p)[1].clone(); |
| 1116 | |
| 1117 | |
| 1118 | assert!(p.place(Lane::Video, 4, clip(0, 100, 102, 0))); |
| 1119 | assert_eq!( |
| 1120 | shape(&p)[0], |
| 1121 | vec![ |
| 1122 | clip(0, 0, 3, 0), |
| 1123 | clip(3, 3, 4, 0), |
| 1124 | clip(4, 100, 102, 0), |
| 1125 | clip(6, 6, 9, 0), |
| 1126 | ] |
| 1127 | ); |
| 1128 | assert_eq!(shape(&p)[1], audio_before, "the audio lane is untouched"); |
| 1129 | assert_eq!(p.timeline_frames(), 9, "an overwrite is not an insert"); |
| 1130 | |
| 1131 | |
| 1132 | let mut p = Project::single(FILE, 9); |
| 1133 | assert!(p.place(Lane::Video, 4, clip(0, 100, 101, 0))); |
| 1134 | assert_eq!( |
| 1135 | shape(&p)[0], |
| 1136 | vec![clip(0, 0, 4, 0), clip(4, 100, 101, 0), clip(5, 5, 9, 0)] |
| 1137 | ); |
| 1138 | |
| 1139 | assert!(p.place(Lane::Video, 20, clip(0, 100, 102, 0))); |
| 1140 | assert_eq!(p.timeline_frames(), 22); |
| 1141 | assert_eq!( |
| 1142 | p.span_at(Lane::Video, 9), |
| 1143 | Some(Span { |
| 1144 | start: 9, |
| 1145 | len: 11, |
| 1146 | from: None |
| 1147 | }) |
| 1148 | ); |
| 1149 | assert!(!p.place(Lane::Video, 0, clip(0, 7, 7, 0)), "empty clip"); |
| 1150 | } |
| 1151 | |
| 1152 | #[test] |
| 1153 | fn ripple_delete_spans_partial_clips() { |
| 1154 | let mut p = three(); |
| 1155 | |
| 1156 | assert!(p.ripple_delete(2, 4)); |
| 1157 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 2, 0), clip(2, 6, 9, 0)]); |
| 1158 | assert_eq!(shape(&p)[1], shape(&p)[0]); |
| 1159 | assert_eq!(p.timeline_frames(), 5); |
| 1160 | assert!(!p.ripple_delete(0, 0), "an empty range is not a delete"); |
| 1161 | assert!(!p.ripple_delete(0, 100), "and one that empties every lane"); |
| 1162 | assert!(p.undo()); |
| 1163 | assert_eq!(shape(&p), shape(&three())); |
| 1164 | } |
| 1165 | |
| 1166 | #[test] |
| 1167 | fn undo_restores_exactly() { |
| 1168 | let mut p = three(); |
| 1169 | let after_splits = shape(&p); |
| 1170 | assert!(p.delete(1)); |
| 1171 | assert!(p.undo()); |
| 1172 | assert_eq!(shape(&p), after_splits); |
| 1173 | |
| 1174 | |
| 1175 | assert!(p.undo()); |
| 1176 | assert_eq!(p.clips().len(), 2); |
| 1177 | assert!(p.undo()); |
| 1178 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); |
| 1179 | assert!(!p.undo(), "empty history"); |
| 1180 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); |
| 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | #[test] |
| 1186 | fn group_ids_are_never_reused() { |
| 1187 | let mut p = Project::single(FILE, 9); |
| 1188 | assert!(p.split(3)); |
| 1189 | let first = p.clips()[1].link; |
| 1190 | assert!(p.undo()); |
| 1191 | assert!(p.split(5)); |
| 1192 | assert_ne!(p.clips()[1].link, first, "a retired id must not come back"); |
| 1193 | } |
| 1194 | |
| 1195 | #[test] |
| 1196 | fn segments_from_reads_the_audio_lane() { |
| 1197 | let p = three(); |
| 1198 | |
| 1199 | assert_eq!( |
| 1200 | p.segments_from(4, FPS), |
| 1201 | vec![ |
| 1202 | (Some(0), 4.0 / 30.0, 5.0 / 30.0), |
| 1203 | (Some(0), 5.0 / 30.0, 9.0 / 30.0) |
| 1204 | ] |
| 1205 | ); |
| 1206 | |
| 1207 | assert_eq!( |
| 1208 | p.segments_from(3, FPS), |
| 1209 | vec![ |
| 1210 | (Some(0), 3.0 / 30.0, 5.0 / 30.0), |
| 1211 | (Some(0), 5.0 / 30.0, 9.0 / 30.0) |
| 1212 | ] |
| 1213 | ); |
| 1214 | |
| 1215 | assert_eq!(p.segments_from(0, FPS).len(), 3); |
| 1216 | |
| 1217 | assert_eq!(p.segments_from(8, FPS), vec![(Some(0), 8.0 / 30.0, 0.3)]); |
| 1218 | |
| 1219 | assert!(p.segments_from(9, FPS).is_empty()); |
| 1220 | assert!(p.segments_from(0, 0.0).is_empty()); |
| 1221 | assert!(p.segments_from(0, f64::NAN).is_empty()); |
| 1222 | } |
| 1223 | |
| 1224 | #[test] |
| 1225 | fn a_gap_in_the_audio_lane_is_a_silent_segment_of_its_own_length() { |
| 1226 | let mut p = three(); |
| 1227 | assert!(p.lift(Lane::Audio, 1)); |
| 1228 | assert_eq!( |
| 1229 | p.segments_from(0, FPS), |
| 1230 | vec![ |
| 1231 | (Some(0), 0.0, 3.0 / 30.0), |
| 1232 | (None, 0.0, 2.0 / 30.0), |
| 1233 | (Some(0), 5.0 / 30.0, 9.0 / 30.0), |
| 1234 | ] |
| 1235 | ); |
| 1236 | |
| 1237 | |
| 1238 | let played: f64 = p.segments_from(0, FPS).iter().map(|(_, a, b)| b - a).sum(); |
| 1239 | assert!((played - 9.0 / 30.0).abs() < 1e-9, "{played}"); |
| 1240 | |
| 1241 | assert_eq!( |
| 1242 | p.segments_from(4, FPS), |
| 1243 | vec![(None, 0.0, 1.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0)] |
| 1244 | ); |
| 1245 | |
| 1246 | assert!(p.lift(Lane::Audio, 0)); |
| 1247 | assert!(p.lift(Lane::Audio, 0)); |
| 1248 | assert_eq!(p.segments_from(0, FPS), vec![(None, 0.0, 0.3)]); |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | fn two_sources() -> Project { |
| 1254 | let mut p = three(); |
| 1255 | let s = p.import(FILE2); |
| 1256 | assert_eq!(s, 1); |
| 1257 | assert!(p.append_clip(s, 4)); |
| 1258 | p |
| 1259 | } |
| 1260 | |
| 1261 | #[test] |
| 1262 | fn import_dedups_and_appends() { |
| 1263 | let mut p = Project::single(FILE, 9); |
| 1264 | assert_eq!(p.sources().len(), 1, "the opened file is source 0"); |
| 1265 | assert_eq!(p.import(FILE), 0, "reimporting the open file reuses 0"); |
| 1266 | assert_eq!(p.import(FILE2), 1); |
| 1267 | assert_eq!(p.import(FILE2), 1, "second import of the same path"); |
| 1268 | assert_eq!(p.sources().len(), 2); |
| 1269 | assert!(!p.append_clip(2, 5), "unknown source index"); |
| 1270 | assert_eq!(p.clips().len(), 1, "a refusal changes nothing"); |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | let here = concat!(env!("CARGO_MANIFEST_DIR"), "/src/project.rs"); |
| 1275 | let detour = concat!(env!("CARGO_MANIFEST_DIR"), "/src/../src/project.rs"); |
| 1276 | let mut p = Project::single(here, 9); |
| 1277 | assert_eq!(p.import(detour), 0); |
| 1278 | assert_eq!(p.sources().len(), 1); |
| 1279 | } |
| 1280 | |
| 1281 | #[test] |
| 1282 | fn append_clip_is_one_undo_step() { |
| 1283 | let mut p = two_sources(); |
| 1284 | assert_eq!(p.timeline_frames(), 13); |
| 1285 | assert_eq!(shape(&p)[0][3], clip(9, 0, 4, 1)); |
| 1286 | assert_eq!(shape(&p)[1][3], clip(9, 0, 4, 1), "and on the audio lane"); |
| 1287 | assert_eq!(p.map_timeline(9), Some((3, 0)), "source 1 starts at 9"); |
| 1288 | assert!(p.undo()); |
| 1289 | assert_eq!(shape(&p), shape(&three()), "one step back to one source"); |
| 1290 | assert_eq!( |
| 1291 | p.sources().len(), |
| 1292 | 2, |
| 1293 | "the orphan source entry stays -- indexes are forever" |
| 1294 | ); |
| 1295 | |
| 1296 | assert!(p.append_clip(1, 0)); |
| 1297 | assert_eq!(p.clips()[3].len(), 1, "clamped like `single`"); |
| 1298 | } |
| 1299 | |
| 1300 | #[test] |
| 1301 | fn edits_carry_the_source_along() { |
| 1302 | let mut p = two_sources(); |
| 1303 | |
| 1304 | assert!(p.split(11)); |
| 1305 | assert_eq!(shape(&p)[0][3], clip(9, 0, 2, 1)); |
| 1306 | assert_eq!(shape(&p)[0][4], clip(11, 2, 4, 1)); |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | let copied = p.clips()[4]; |
| 1311 | assert!(p.paste(1, copied)); |
| 1312 | assert_eq!(p.clips()[1].source, 1); |
| 1313 | assert_eq!(p.clips()[1].in_frame, 2); |
| 1314 | assert_eq!(p.clips()[2].source, 0); |
| 1315 | assert!(p.undo()); |
| 1316 | assert_eq!(p.clips()[1].source, 0, "undo restores sources too"); |
| 1317 | } |
| 1318 | |
| 1319 | #[test] |
| 1320 | fn segments_from_names_the_source() { |
| 1321 | let p = two_sources(); |
| 1322 | |
| 1323 | assert_eq!( |
| 1324 | p.segments_from(4, FPS), |
| 1325 | vec![ |
| 1326 | (Some(0), 4.0 / 30.0, 5.0 / 30.0), |
| 1327 | (Some(0), 5.0 / 30.0, 9.0 / 30.0), |
| 1328 | (Some(1), 0.0, 4.0 / 30.0), |
| 1329 | ] |
| 1330 | ); |
| 1331 | |
| 1332 | assert_eq!( |
| 1333 | p.segments_from(10, FPS), |
| 1334 | vec![(Some(1), 1.0 / 30.0, 4.0 / 30.0)] |
| 1335 | ); |
| 1336 | assert!(p.segments_from(13, FPS).is_empty()); |
| 1337 | |
| 1338 | let played: f64 = p.segments_from(0, FPS).iter().map(|(_, a, b)| b - a).sum(); |
| 1339 | assert!((played - f64::from(p.timeline_frames()) / FPS).abs() < 1e-9); |
| 1340 | } |
| 1341 | |
| 1342 | #[test] |
| 1343 | fn segments_follow_deletes_and_fps() { |
| 1344 | let mut p = three(); |
| 1345 | assert!(p.delete(0)); |
| 1346 | |
| 1347 | let segs = p.segments_from(0, 25.0); |
| 1348 | assert_eq!( |
| 1349 | segs, |
| 1350 | vec![(Some(0), 3.0 / 25.0, 0.2), (Some(0), 0.2, 9.0 / 25.0)] |
| 1351 | ); |
| 1352 | let played: f64 = segs.iter().map(|(_, a, b)| b - a).sum(); |
| 1353 | assert!( |
| 1354 | (played - p.timeline_frames() as f64 / 25.0).abs() < 1e-9, |
| 1355 | "segments must cover exactly the timeline: {played}" |
| 1356 | ); |
| 1357 | } |
| 1358 | |
| 1359 | #[test] |
| 1360 | fn orphan_sources_are_pruned_and_the_clips_reindexed() { |
| 1361 | |
| 1362 | let mut p = two_sources(); |
| 1363 | assert!(p.undo()); |
| 1364 | let (sources, video, audio) = p.without_orphan_sources(); |
| 1365 | assert_eq!(sources, vec![PathBuf::from(FILE)], "the orphan is gone"); |
| 1366 | assert_eq!(video, three().clips(), "the clips are untouched"); |
| 1367 | assert_eq!(audio, three().lane(Lane::Audio)); |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | let mut p = Project::single(FILE, 9); |
| 1372 | assert_eq!(p.import(FILE2), 1); |
| 1373 | assert_eq!(p.import("/nonexistent/c.mp4"), 2); |
| 1374 | assert!(p.append_clip(2, 4)); |
| 1375 | let (sources, video, audio) = p.without_orphan_sources(); |
| 1376 | assert_eq!( |
| 1377 | sources, |
| 1378 | vec![PathBuf::from(FILE), PathBuf::from("/nonexistent/c.mp4")] |
| 1379 | ); |
| 1380 | assert_eq!(video.iter().map(|c| c.source).collect::<Vec<_>>(), [0, 1]); |
| 1381 | |
| 1382 | let reloaded = Project::from_parts(sources, video, audio).expect("from_parts"); |
| 1383 | assert_eq!(reloaded.timeline_frames(), p.timeline_frames()); |
| 1384 | assert_eq!(reloaded.sources().len(), 2); |
| 1385 | } |
| 1386 | |
| 1387 | #[test] |
| 1388 | fn from_parts_has_no_history_and_checks_the_invariants() { |
| 1389 | let (sources, video, audio) = three().without_orphan_sources(); |
| 1390 | let mut p = Project::from_parts(sources.clone(), video.clone(), audio.clone()) |
| 1391 | .expect("valid parts"); |
| 1392 | assert_eq!(p.clips(), three().clips()); |
| 1393 | assert!(!p.undo(), "a loaded project has nothing to undo"); |
| 1394 | assert!(p.split(4), "...and is editable from there"); |
| 1395 | assert!(p.undo()); |
| 1396 | |
| 1397 | |
| 1398 | assert!(Project::from_parts(sources.clone(), video.clone(), Vec::new()).is_ok()); |
| 1399 | assert!(Project::from_parts(sources.clone(), Vec::new(), Vec::new()).is_err()); |
| 1400 | let bad: [Vec<Clip>; 5] = [ |
| 1401 | vec![clip(0, 0, 3, 1)], |
| 1402 | vec![clip(0, 3, 3, 0)], |
| 1403 | vec![clip(3, 0, 3, 0), clip(0, 3, 5, 0)], |
| 1404 | vec![clip(0, 0, 5, 0), clip(3, 3, 5, 0)], |
| 1405 | vec![clip(u32::MAX - 1, 0, 3, 0)], |
| 1406 | ]; |
| 1407 | for clips in bad { |
| 1408 | assert!( |
| 1409 | Project::from_parts(sources.clone(), clips.clone(), Vec::new()).is_err(), |
| 1410 | "{clips:?}" |
| 1411 | ); |
| 1412 | assert!(Project::from_parts(sources.clone(), video.clone(), clips).is_err()); |
| 1413 | } |
| 1414 | |
| 1415 | let mut p = Project::from_parts(sources, video, audio).expect("valid parts"); |
| 1416 | assert!(p.split(4)); |
| 1417 | assert!(p.clips().iter().all(|c| c.link.is_some())); |
| 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | #[test] |
| 1425 | fn a_link_id_is_never_two_clips_of_one_lane() { |
| 1426 | let sources = vec![PathBuf::from(FILE)]; |
| 1427 | let linked = |start, in_frame, out_frame, link| Clip { |
| 1428 | start, |
| 1429 | in_frame, |
| 1430 | out_frame, |
| 1431 | source: 0, |
| 1432 | link: Some(link), |
| 1433 | }; |
| 1434 | |
| 1435 | |
| 1436 | let err = |video: Vec<Clip>, audio: Vec<Clip>| { |
| 1437 | Project::from_parts(sources.clone(), video, audio) |
| 1438 | .expect_err("refused") |
| 1439 | .to_string() |
| 1440 | }; |
| 1441 | assert!( |
| 1442 | err(vec![linked(0, 0, 3, 7), linked(3, 3, 5, 7)], Vec::new()) |
| 1443 | .contains("link 7 names two clips in the video lane"), |
| 1444 | "a duplicate id inside one lane is refused by name" |
| 1445 | ); |
| 1446 | assert!( |
| 1447 | err(vec![linked(0, 0, 5, 2)], vec![linked(0, 0, 3, 2)]) |
| 1448 | .contains("link 2 covers [0, 5) in video and [0, 3) in audio"), |
| 1449 | "a pair that does not cover one span is refused by name" |
| 1450 | ); |
| 1451 | |
| 1452 | let mut p = Project::single(FILE, 9); |
| 1453 | assert!(p.lift(Lane::Audio, 0)); |
| 1454 | let (sources, video, audio) = p.clone().without_orphan_sources(); |
| 1455 | assert!( |
| 1456 | Project::from_parts(sources, video, audio).is_ok(), |
| 1457 | "a lifted lane's project has to load again" |
| 1458 | ); |
| 1459 | |
| 1460 | |
| 1461 | let consistent = |p: &Project, what: &str| { |
| 1462 | let lanes = Lane::ALL.map(|l| p.lane(l).to_vec()); |
| 1463 | assert!(links_are_consistent(&lanes).is_ok(), "{what}: {lanes:?}"); |
| 1464 | }; |
| 1465 | let mut p = three(); |
| 1466 | let copied = p.clips()[0]; |
| 1467 | assert!( |
| 1468 | copied.link.is_some(), |
| 1469 | "the clipboard clip carries its group" |
| 1470 | ); |
| 1471 | assert!(p.lift(Lane::Video, 0)); |
| 1472 | assert!(p.place(Lane::Video, 0, copied)); |
| 1473 | assert!(p.place(Lane::Video, 20, copied), "and again, further out"); |
| 1474 | assert_eq!( |
| 1475 | p.clips().iter().filter(|c| c.link == copied.link).count(), |
| 1476 | 0, |
| 1477 | "a placement belongs to no group, so it cannot duplicate one" |
| 1478 | ); |
| 1479 | consistent(&p, "lift + place twice"); |
| 1480 | assert!( |
| 1481 | p.place(Lane::Audio, 4, copied), |
| 1482 | "over a linked clip's middle" |
| 1483 | ); |
| 1484 | consistent(&p, "place through a grouped clip"); |
| 1485 | assert!(p.paste(6, copied), "a rippling paste through another"); |
| 1486 | consistent(&p, "paste"); |
| 1487 | assert!(p.split(2)); |
| 1488 | consistent(&p, "split"); |
| 1489 | assert!(p.ripple_delete(1, 3)); |
| 1490 | consistent(&p, "ripple_delete"); |
| 1491 | assert!(p.undo()); |
| 1492 | consistent(&p, "undo"); |
| 1493 | } |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | |
| 1499 | #[test] |
| 1500 | fn every_reachable_state_reloads() { |
| 1501 | let mut p = Project::single(FILE, 150); |
| 1502 | assert!(p.place(Lane::Audio, 3, clip(0, 0, 50, 0))); |
| 1503 | assert!(p.split(10), "the lanes are cut apart at 10"); |
| 1504 | reloads(&p, "place on one lane, then split"); |
| 1505 | |
| 1506 | let mut p = Project::single(FILE, 150); |
| 1507 | assert!(p.split(30)); |
| 1508 | assert!(p.lift(Lane::Video, 0), "the picture goes, the sound stays"); |
| 1509 | assert!(p.regroup(30), "only the audio lane can rejoin"); |
| 1510 | assert!(p.split(60)); |
| 1511 | reloads(&p, "lift, regroup one lane, split"); |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | #[test] |
| 1518 | fn random_edit_sequences_reload() { |
| 1519 | for seed in 0..200u64 { |
| 1520 | let mut rng = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); |
| 1521 | let mut next = move |n: u32| { |
| 1522 | |
| 1523 | rng ^= rng << 13; |
| 1524 | rng ^= rng >> 7; |
| 1525 | rng ^= rng << 17; |
| 1526 | (rng >> 33) as u32 % n |
| 1527 | }; |
| 1528 | let mut p = Project::single(FILE, 40); |
| 1529 | for step in 0..24 { |
| 1530 | let frame = next(44); |
| 1531 | |
| 1532 | |
| 1533 | let copied = *p |
| 1534 | .lane(if next(2) == 0 { |
| 1535 | Lane::Video |
| 1536 | } else { |
| 1537 | Lane::Audio |
| 1538 | }) |
| 1539 | .get(next(4) as usize) |
| 1540 | .unwrap_or(&clip(0, 0, 5, 0)); |
| 1541 | let lane = if next(2) == 0 { |
| 1542 | Lane::Video |
| 1543 | } else { |
| 1544 | Lane::Audio |
| 1545 | }; |
| 1546 | let idx = next(4) as usize; |
| 1547 | let _ = match next(9) { |
| 1548 | 0 => p.split(frame), |
| 1549 | 1 => p.regroup(frame), |
| 1550 | 2 => p.lift(lane, idx), |
| 1551 | 3 => p.place(lane, frame, copied), |
| 1552 | 4 => p.paste(frame, copied), |
| 1553 | 5 => p.ripple_delete(frame, next(9)), |
| 1554 | 6 => p.delete(idx), |
| 1555 | 7 => p.undo(), |
| 1556 | _ => p.append_clip(0, 1 + next(9)), |
| 1557 | }; |
| 1558 | reloads(&p, &format!("seed {seed}, step {step}")); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | fn reloads(p: &Project, what: &str) { |
| 1566 | let (sources, video, audio) = p.clone().without_orphan_sources(); |
| 1567 | if let Err(e) = Project::from_parts(sources, video, audio) { |
| 1568 | panic!("{what}: saved but would not load: {e}\n{:?}", p.lanes); |
| 1569 | } |
| 1570 | } |
| 1571 | } |