| 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 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | use std::path::{Path, PathBuf}; |
| 66 | |
| 67 | use crate::color::ColorParams; |
| 68 | use crate::eq::EqParams; |
| 69 | use crate::scale::FitPolicy; |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 75 | pub struct Clip { |
| 76 | |
| 77 | |
| 78 | |
| 79 | pub start: u32, |
| 80 | pub in_frame: u32, |
| 81 | pub out_frame: u32, |
| 82 | |
| 83 | pub source: usize, |
| 84 | |
| 85 | |
| 86 | pub link: Option<u32>, |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | pub eq: Option<u16>, |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub color: Option<u16>, |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | pub fit: FitPolicy, |
| 106 | } |
| 107 | |
| 108 | impl Clip { |
| 109 | |
| 110 | pub fn len(&self) -> u32 { |
| 111 | self.out_frame - self.in_frame |
| 112 | } |
| 113 | |
| 114 | |
| 115 | pub fn end(&self) -> u32 { |
| 116 | self.start + self.len() |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 128 | pub struct Source { |
| 129 | pub path: PathBuf, |
| 130 | |
| 131 | |
| 132 | |
| 133 | pub audio_stream: usize, |
| 134 | } |
| 135 | |
| 136 | impl Source { |
| 137 | pub fn new(path: impl AsRef<Path>, audio_stream: usize) -> Self { |
| 138 | Self { |
| 139 | path: canonical(path.as_ref()), |
| 140 | audio_stream, |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 148 | pub enum LaneKind { |
| 149 | Video, |
| 150 | Audio, |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 162 | pub struct Lane { |
| 163 | pub kind: LaneKind, |
| 164 | pub ord: usize, |
| 165 | } |
| 166 | |
| 167 | impl Lane { |
| 168 | |
| 169 | pub const V1: Lane = Lane::new(LaneKind::Video, 0); |
| 170 | |
| 171 | pub const A1: Lane = Lane::new(LaneKind::Audio, 0); |
| 172 | |
| 173 | pub const fn new(kind: LaneKind, ord: usize) -> Self { |
| 174 | Self { kind, ord } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | pub fn label(self) -> String { |
| 180 | let kind = match self.kind { |
| 181 | LaneKind::Video => 'V', |
| 182 | LaneKind::Audio => 'A', |
| 183 | }; |
| 184 | format!("{kind}{}", self.ord + 1) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | #[derive(Clone, Debug)] |
| 192 | struct LaneData { |
| 193 | kind: LaneKind, |
| 194 | |
| 195 | clips: Vec<Clip>, |
| 196 | } |
| 197 | |
| 198 | impl LaneData { |
| 199 | |
| 200 | |
| 201 | fn two_lanes(video: Vec<Clip>, audio: Vec<Clip>) -> Vec<LaneData> { |
| 202 | vec![ |
| 203 | LaneData { |
| 204 | kind: LaneKind::Video, |
| 205 | clips: video, |
| 206 | }, |
| 207 | LaneData { |
| 208 | kind: LaneKind::Audio, |
| 209 | clips: audio, |
| 210 | }, |
| 211 | ] |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 219 | pub struct Span { |
| 220 | |
| 221 | pub start: u32, |
| 222 | |
| 223 | pub len: u32, |
| 224 | |
| 225 | |
| 226 | pub from: Option<(usize, u32)>, |
| 227 | } |
| 228 | |
| 229 | impl Span { |
| 230 | |
| 231 | pub fn end(&self) -> u32 { |
| 232 | self.start + self.len |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | pub type Parts = ( |
| 241 | Vec<Source>, |
| 242 | Vec<(LaneKind, Vec<Clip>)>, |
| 243 | Vec<EqParams>, |
| 244 | Vec<ColorParams>, |
| 245 | ); |
| 246 | |
| 247 | |
| 248 | #[derive(Clone, Debug)] |
| 249 | pub struct Project { |
| 250 | |
| 251 | sources: Vec<Source>, |
| 252 | |
| 253 | |
| 254 | lanes: Vec<LaneData>, |
| 255 | |
| 256 | eq: Vec<EqParams>, |
| 257 | |
| 258 | color: Vec<ColorParams>, |
| 259 | |
| 260 | |
| 261 | history: Vec<Vec<LaneData>>, |
| 262 | |
| 263 | |
| 264 | next_link: u32, |
| 265 | } |
| 266 | |
| 267 | impl Project { |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | pub fn single(path: impl AsRef<Path>, frame_count: u32) -> Self { |
| 273 | let clip = Clip { |
| 274 | start: 0, |
| 275 | in_frame: 0, |
| 276 | out_frame: frame_count.max(1), |
| 277 | source: 0, |
| 278 | link: Some(0), |
| 279 | eq: None, |
| 280 | color: None, |
| 281 | fit: FitPolicy::default(), |
| 282 | }; |
| 283 | Self { |
| 284 | |
| 285 | |
| 286 | sources: vec![Source::new(path, 0)], |
| 287 | lanes: LaneData::two_lanes(vec![clip], vec![clip]), |
| 288 | eq: Vec::new(), |
| 289 | color: Vec::new(), |
| 290 | history: Vec::new(), |
| 291 | next_link: 1, |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | pub fn from_parts( |
| 307 | sources: Vec<Source>, |
| 308 | lanes: Vec<(LaneKind, Vec<Clip>)>, |
| 309 | eq: Vec<EqParams>, |
| 310 | color: Vec<ColorParams>, |
| 311 | ) -> crate::Result<Self> { |
| 312 | if let Some(bad) = eq.iter().position(|p| !finite(p)) { |
| 313 | return Err(format!("eq {bad} holds a band that is not a finite number").into()); |
| 314 | } |
| 315 | if let Some(bad) = color.iter().position(|p| !color_finite(p)) { |
| 316 | return Err(format!("color {bad} holds a value that is not a finite number").into()); |
| 317 | } |
| 318 | if lanes.iter().all(|(_, clips)| clips.is_empty()) { |
| 319 | return Err("every lane is empty: that is not a project".into()); |
| 320 | } |
| 321 | let lanes: Vec<LaneData> = lanes |
| 322 | .into_iter() |
| 323 | .map(|(kind, clips)| LaneData { kind, clips }) |
| 324 | .collect(); |
| 325 | for (data, lane) in lanes.iter().zip(handles(&lanes)) { |
| 326 | let name = lane.label(); |
| 327 | for c in &data.clips { |
| 328 | if c.out_frame <= c.in_frame { |
| 329 | return Err(format!( |
| 330 | "{name} clip at {} plays the empty range [{}, {})", |
| 331 | c.start, c.in_frame, c.out_frame |
| 332 | ) |
| 333 | .into()); |
| 334 | } |
| 335 | if c.source >= sources.len() { |
| 336 | return Err(format!( |
| 337 | "{name} clip at {} names source {} of {}", |
| 338 | c.start, |
| 339 | c.source, |
| 340 | sources.len() |
| 341 | ) |
| 342 | .into()); |
| 343 | } |
| 344 | if c.start.checked_add(c.len()).is_none() { |
| 345 | return Err(format!( |
| 346 | "{name} clip at {} runs past the last frame there is", |
| 347 | c.start |
| 348 | ) |
| 349 | .into()); |
| 350 | } |
| 351 | if c.eq.is_some_and(|i| usize::from(i) >= eq.len()) { |
| 352 | return Err(format!( |
| 353 | "{name} clip at {} names eq {} of {}", |
| 354 | c.start, |
| 355 | c.eq.unwrap_or_default(), |
| 356 | eq.len() |
| 357 | ) |
| 358 | .into()); |
| 359 | } |
| 360 | if c.color.is_some_and(|i| usize::from(i) >= color.len()) { |
| 361 | return Err(format!( |
| 362 | "{name} clip at {} names color {} of {}", |
| 363 | c.start, |
| 364 | c.color.unwrap_or_default(), |
| 365 | color.len() |
| 366 | ) |
| 367 | .into()); |
| 368 | } |
| 369 | } |
| 370 | if !sorted_disjoint(&data.clips) { |
| 371 | return Err(format!("the {name} lane is out of order or overlaps itself").into()); |
| 372 | } |
| 373 | } |
| 374 | links_are_consistent(&lanes)?; |
| 375 | let next_link = lanes |
| 376 | .iter() |
| 377 | .flat_map(|l| &l.clips) |
| 378 | .filter_map(|c| c.link) |
| 379 | .max() |
| 380 | |
| 381 | |
| 382 | .map_or(0, |m| m.saturating_add(1)); |
| 383 | Ok(Self { |
| 384 | sources: sources |
| 385 | .iter() |
| 386 | .map(|s| Source::new(&s.path, s.audio_stream)) |
| 387 | .collect(), |
| 388 | lanes, |
| 389 | eq, |
| 390 | color, |
| 391 | history: Vec::new(), |
| 392 | next_link, |
| 393 | }) |
| 394 | } |
| 395 | |
| 396 | |
| 397 | |
| 398 | pub fn clips(&self) -> &[Clip] { |
| 399 | self.lane(Lane::V1) |
| 400 | } |
| 401 | |
| 402 | |
| 403 | pub fn lane(&self, lane: Lane) -> &[Clip] { |
| 404 | self.index(lane).map_or(&[][..], |i| &self.lanes[i].clips) |
| 405 | } |
| 406 | |
| 407 | |
| 408 | |
| 409 | pub fn lanes(&self) -> Vec<Lane> { |
| 410 | handles(&self.lanes) |
| 411 | } |
| 412 | |
| 413 | |
| 414 | |
| 415 | pub fn lane_count(&self, kind: LaneKind) -> usize { |
| 416 | self.lanes.iter().filter(|l| l.kind == kind).count() |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | pub fn add_lane(&mut self, kind: LaneKind) -> Lane { |
| 428 | self.snapshot(); |
| 429 | self.lanes.push(LaneData { |
| 430 | kind, |
| 431 | clips: Vec::new(), |
| 432 | }); |
| 433 | Lane::new(kind, self.lane_count(kind) - 1) |
| 434 | } |
| 435 | |
| 436 | |
| 437 | |
| 438 | fn index(&self, lane: Lane) -> Option<usize> { |
| 439 | self.lanes |
| 440 | .iter() |
| 441 | .enumerate() |
| 442 | .filter(|(_, l)| l.kind == lane.kind) |
| 443 | .map(|(i, _)| i) |
| 444 | .nth(lane.ord) |
| 445 | } |
| 446 | |
| 447 | fn lane_mut(&mut self, lane: Lane) -> Option<&mut Vec<Clip>> { |
| 448 | let i = self.index(lane)?; |
| 449 | Some(&mut self.lanes[i].clips) |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |
| 454 | pub fn sources(&self) -> &[Source] { |
| 455 | &self.sources |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | pub fn audio_sources(&self) -> Vec<(PathBuf, usize)> { |
| 463 | self.sources |
| 464 | .iter() |
| 465 | .map(|s| (s.path.clone(), s.audio_stream)) |
| 466 | .collect() |
| 467 | } |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | pub fn import(&mut self, path: impl AsRef<Path>, audio_stream: usize) -> usize { |
| 475 | let source = Source::new(path, audio_stream); |
| 476 | match self.sources.iter().position(|s| *s == source) { |
| 477 | Some(idx) => idx, |
| 478 | None => { |
| 479 | self.sources.push(source); |
| 480 | self.sources.len() - 1 |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | pub fn append_clip(&mut self, source: usize, frame_count: u32) -> bool { |
| 490 | if source >= self.sources.len() { |
| 491 | return false; |
| 492 | } |
| 493 | self.snapshot(); |
| 494 | let start = self.timeline_frames(); |
| 495 | let clip = Clip { |
| 496 | start, |
| 497 | in_frame: 0, |
| 498 | out_frame: frame_count.max(1), |
| 499 | source, |
| 500 | link: Some(self.new_link()), |
| 501 | eq: None, |
| 502 | color: None, |
| 503 | fit: FitPolicy::default(), |
| 504 | }; |
| 505 | for data in &mut self.lanes { |
| 506 | data.clips.push(clip); |
| 507 | } |
| 508 | true |
| 509 | } |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | pub fn without_orphan_sources(&self) -> Parts { |
| 529 | let mut moved = vec![None; self.sources.len()]; |
| 530 | let mut sources = Vec::new(); |
| 531 | let mut moved_eq = vec![None; self.eq.len()]; |
| 532 | let mut eq = Vec::new(); |
| 533 | let mut moved_color = vec![None; self.color.len()]; |
| 534 | let mut color = Vec::new(); |
| 535 | let mut lanes = self.lanes.clone(); |
| 536 | for c in lanes.iter_mut().flat_map(|l| &mut l.clips) { |
| 537 | let old = c.source; |
| 538 | c.source = match moved[old] { |
| 539 | Some(new) => new, |
| 540 | None => { |
| 541 | sources.push(self.sources[old].clone()); |
| 542 | moved[old] = Some(sources.len() - 1); |
| 543 | sources.len() - 1 |
| 544 | } |
| 545 | }; |
| 546 | if let Some(old) = c.eq.map(usize::from) { |
| 547 | c.eq = Some(match moved_eq[old] { |
| 548 | Some(new) => new, |
| 549 | None => { |
| 550 | eq.push(self.eq[old].clone()); |
| 551 | let new = (eq.len() - 1) as u16; |
| 552 | moved_eq[old] = Some(new); |
| 553 | new |
| 554 | } |
| 555 | }); |
| 556 | } |
| 557 | if let Some(old) = c.color.map(usize::from) { |
| 558 | c.color = Some(match moved_color[old] { |
| 559 | Some(new) => new, |
| 560 | None => { |
| 561 | color.push(self.color[old]); |
| 562 | let new = (color.len() - 1) as u16; |
| 563 | moved_color[old] = Some(new); |
| 564 | new |
| 565 | } |
| 566 | }); |
| 567 | } |
| 568 | } |
| 569 | ( |
| 570 | sources, |
| 571 | lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), |
| 572 | eq, |
| 573 | color, |
| 574 | ) |
| 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> { |
| 581 | self.eq.get(usize::from(self.lane(lane).get(idx)?.eq?)) |
| 582 | } |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | pub fn set_eq(&mut self, lane: Lane, idx: usize, params: Option<EqParams>) -> bool { |
| 593 | if idx >= self.lane(lane).len() { |
| 594 | return false; |
| 595 | } |
| 596 | let slot = match params { |
| 597 | None => None, |
| 598 | Some(params) => { |
| 599 | if !finite(¶ms) { |
| 600 | return false; |
| 601 | } |
| 602 | Some(match self.eq.iter().position(|p| *p == params) { |
| 603 | Some(at) => at as u16, |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | None if self.eq.len() >= usize::from(u16::MAX) => return false, |
| 611 | None => { |
| 612 | self.eq.push(params); |
| 613 | (self.eq.len() - 1) as u16 |
| 614 | } |
| 615 | }) |
| 616 | } |
| 617 | }; |
| 618 | self.snapshot(); |
| 619 | self.lane_mut(lane).expect("checked above")[idx].eq = slot; |
| 620 | true |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | pub fn color_of(&self, lane: Lane, idx: usize) -> Option<&ColorParams> { |
| 627 | self.color |
| 628 | .get(usize::from(self.lane(lane).get(idx)?.color?)) |
| 629 | } |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | pub fn set_color(&mut self, lane: Lane, idx: usize, params: Option<ColorParams>) -> bool { |
| 636 | if idx >= self.lane(lane).len() { |
| 637 | return false; |
| 638 | } |
| 639 | let slot = match params { |
| 640 | None => None, |
| 641 | Some(params) => { |
| 642 | if !color_finite(¶ms) { |
| 643 | return false; |
| 644 | } |
| 645 | Some(match self.color.iter().position(|p| *p == params) { |
| 646 | Some(at) => at as u16, |
| 647 | |
| 648 | |
| 649 | |
| 650 | None if self.color.len() >= usize::from(u16::MAX) => return false, |
| 651 | None => { |
| 652 | self.color.push(params); |
| 653 | (self.color.len() - 1) as u16 |
| 654 | } |
| 655 | }) |
| 656 | } |
| 657 | }; |
| 658 | self.snapshot(); |
| 659 | self.lane_mut(lane).expect("checked above")[idx].color = slot; |
| 660 | true |
| 661 | } |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | pub fn fit_of(&self, lane: Lane, idx: usize) -> FitPolicy { |
| 667 | self.lane(lane) |
| 668 | .get(idx) |
| 669 | .map_or(FitPolicy::default(), |c| c.fit) |
| 670 | } |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | pub fn set_fit(&mut self, lane: Lane, idx: usize, fit: FitPolicy) -> bool { |
| 676 | if idx >= self.lane(lane).len() { |
| 677 | return false; |
| 678 | } |
| 679 | self.snapshot(); |
| 680 | self.lane_mut(lane).expect("checked above")[idx].fit = fit; |
| 681 | true |
| 682 | } |
| 683 | |
| 684 | |
| 685 | |
| 686 | pub fn timeline_frames(&self) -> u32 { |
| 687 | self.lanes |
| 688 | .iter() |
| 689 | .filter_map(|l| l.clips.last().map(Clip::end)) |
| 690 | .max() |
| 691 | .unwrap_or(0) |
| 692 | } |
| 693 | |
| 694 | |
| 695 | |
| 696 | pub fn clip_spans(&self) -> Vec<(u32, u32)> { |
| 697 | self.lane_spans(Lane::V1) |
| 698 | } |
| 699 | |
| 700 | pub fn lane_spans(&self, lane: Lane) -> Vec<(u32, u32)> { |
| 701 | self.lane(lane).iter().map(|c| (c.start, c.len())).collect() |
| 702 | } |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | pub fn map(&self, lane: Lane, timeline_frame: u32) -> Option<(usize, u32)> { |
| 708 | let clips = self.lane(lane); |
| 709 | let idx = at(clips, timeline_frame)?; |
| 710 | Some(( |
| 711 | idx, |
| 712 | clips[idx].in_frame + (timeline_frame - clips[idx].start), |
| 713 | )) |
| 714 | } |
| 715 | |
| 716 | |
| 717 | pub fn map_timeline(&self, timeline_frame: u32) -> Option<(usize, u32)> { |
| 718 | self.map(Lane::V1, timeline_frame) |
| 719 | } |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | pub fn span_at(&self, lane: Lane, timeline_frame: u32) -> Option<Span> { |
| 727 | let total = self.timeline_frames(); |
| 728 | if timeline_frame >= total { |
| 729 | return None; |
| 730 | } |
| 731 | let clips = self.lane(lane); |
| 732 | Some(match at(clips, timeline_frame) { |
| 733 | Some(idx) => Span { |
| 734 | start: timeline_frame, |
| 735 | len: clips[idx].end() - timeline_frame, |
| 736 | from: Some(( |
| 737 | clips[idx].source, |
| 738 | clips[idx].in_frame + (timeline_frame - clips[idx].start), |
| 739 | )), |
| 740 | }, |
| 741 | None => Span { |
| 742 | start: timeline_frame, |
| 743 | |
| 744 | |
| 745 | len: clips |
| 746 | .iter() |
| 747 | .find(|c| c.start > timeline_frame) |
| 748 | .map_or(total, |c| c.start) |
| 749 | - timeline_frame, |
| 750 | from: None, |
| 751 | }, |
| 752 | }) |
| 753 | } |
| 754 | |
| 755 | |
| 756 | |
| 757 | pub fn spans_from(&self, lane: Lane, timeline_frame: u32) -> Vec<Span> { |
| 758 | let mut out = Vec::new(); |
| 759 | let mut t = timeline_frame; |
| 760 | while let Some(span) = self.span_at(lane, t) { |
| 761 | t = span.end(); |
| 762 | out.push(span); |
| 763 | } |
| 764 | out |
| 765 | } |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | pub fn composite_span_at(&self, timeline_frame: u32) -> Option<Span> { |
| 787 | let total = self.timeline_frames(); |
| 788 | if timeline_frame >= total { |
| 789 | return None; |
| 790 | } |
| 791 | let video = self.video_lanes(); |
| 792 | |
| 793 | |
| 794 | let winner = video |
| 795 | .iter() |
| 796 | .rposition(|&lane| at(self.lane(lane), timeline_frame).is_some()); |
| 797 | let (from, until) = match winner { |
| 798 | Some(i) => { |
| 799 | let clips = self.lane(video[i]); |
| 800 | let idx = at(clips, timeline_frame).expect("just found above"); |
| 801 | ( |
| 802 | Some(( |
| 803 | clips[idx].source, |
| 804 | clips[idx].in_frame + (timeline_frame - clips[idx].start), |
| 805 | )), |
| 806 | clips[idx].end(), |
| 807 | ) |
| 808 | } |
| 809 | |
| 810 | None => (None, total), |
| 811 | }; |
| 812 | let takeover = video[winner.map_or(0, |i| i + 1)..] |
| 813 | .iter() |
| 814 | .filter_map(|&lane| self.lane(lane).iter().find(|c| c.start > timeline_frame)) |
| 815 | .map(|c| c.start) |
| 816 | .min(); |
| 817 | Some(Span { |
| 818 | start: timeline_frame, |
| 819 | len: takeover.map_or(until, |t| t.min(until)) - timeline_frame, |
| 820 | from, |
| 821 | }) |
| 822 | } |
| 823 | |
| 824 | |
| 825 | |
| 826 | fn video_lanes(&self) -> Vec<Lane> { |
| 827 | self.lanes() |
| 828 | .into_iter() |
| 829 | .filter(|l| l.kind == LaneKind::Video) |
| 830 | .collect() |
| 831 | } |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | pub fn composite_clip_at(&self, timeline_frame: u32) -> Option<(Lane, usize)> { |
| 838 | let video = self.video_lanes(); |
| 839 | let winner = video |
| 840 | .iter() |
| 841 | .rposition(|&lane| at(self.lane(lane), timeline_frame).is_some())?; |
| 842 | Some((video[winner], at(self.lane(video[winner]), timeline_frame)?)) |
| 843 | } |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | pub fn lane_clip_at(&self, lane: Lane, timeline_frame: u32) -> Option<usize> { |
| 850 | at(self.lane(lane), timeline_frame) |
| 851 | } |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | pub fn composite_color_at(&self, timeline_frame: u32) -> Option<&ColorParams> { |
| 858 | let (lane, idx) = self.composite_clip_at(timeline_frame)?; |
| 859 | self.color_of(lane, idx) |
| 860 | } |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | pub fn composite_fit_at(&self, timeline_frame: u32) -> FitPolicy { |
| 866 | self.composite_clip_at(timeline_frame) |
| 867 | .map_or(FitPolicy::default(), |(lane, idx)| self.fit_of(lane, idx)) |
| 868 | } |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | pub fn composite_spans_from(&self, timeline_frame: u32) -> Vec<Span> { |
| 874 | let mut out = Vec::new(); |
| 875 | let mut t = timeline_frame; |
| 876 | while let Some(span) = self.composite_span_at(t) { |
| 877 | t = span.end(); |
| 878 | out.push(span); |
| 879 | } |
| 880 | out |
| 881 | } |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | pub fn split(&mut self, timeline_frame: u32) -> bool { |
| 897 | let cut: Vec<Option<Clip>> = self |
| 898 | .lanes |
| 899 | .iter() |
| 900 | .map(|l| splittable(&l.clips, timeline_frame).map(|idx| l.clips[idx])) |
| 901 | .collect(); |
| 902 | if cut.iter().all(Option::is_none) { |
| 903 | return false; |
| 904 | } |
| 905 | self.snapshot(); |
| 906 | |
| 907 | |
| 908 | let starts: Vec<Option<u32>> = cut.iter().map(|c| c.map(|c| c.start)).collect(); |
| 909 | let ends: Vec<Option<u32>> = cut.iter().map(|c| c.map(|c| c.end())).collect(); |
| 910 | let left = self.group_ids(&starts); |
| 911 | let right = self.group_ids(&ends); |
| 912 | for ((data, left), right) in self.lanes.iter_mut().zip(left).zip(right) { |
| 913 | let Some(idx) = splittable(&data.clips, timeline_frame) else { |
| 914 | continue; |
| 915 | }; |
| 916 | let mut tail = data.clips[idx]; |
| 917 | tail.in_frame += timeline_frame - tail.start; |
| 918 | tail.start = timeline_frame; |
| 919 | tail.link = right; |
| 920 | data.clips[idx].out_frame = tail.in_frame; |
| 921 | data.clips[idx].link = left; |
| 922 | data.clips.insert(idx + 1, tail); |
| 923 | } |
| 924 | true |
| 925 | } |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | pub fn regroup(&mut self, timeline_frame: u32) -> bool { |
| 936 | |
| 937 | let joined: Vec<Option<(u32, u32)>> = self |
| 938 | .lanes |
| 939 | .iter() |
| 940 | .map(|l| { |
| 941 | joinable(&l.clips, timeline_frame) |
| 942 | .map(|idx| (l.clips[idx].start, l.clips[idx + 1].end())) |
| 943 | }) |
| 944 | .collect(); |
| 945 | if joined.iter().all(Option::is_none) { |
| 946 | return false; |
| 947 | } |
| 948 | self.snapshot(); |
| 949 | let ids = self.group_ids(&joined); |
| 950 | for (data, link) in self.lanes.iter_mut().zip(ids) { |
| 951 | let Some(idx) = joinable(&data.clips, timeline_frame) else { |
| 952 | continue; |
| 953 | }; |
| 954 | data.clips[idx].out_frame = data.clips[idx + 1].out_frame; |
| 955 | data.clips[idx].link = link; |
| 956 | data.clips.remove(idx + 1); |
| 957 | } |
| 958 | true |
| 959 | } |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | pub fn place(&mut self, lane: Lane, timeline_frame: u32, clip: Clip) -> bool { |
| 978 | if clip.out_frame <= clip.in_frame || self.index(lane).is_none() { |
| 979 | return false; |
| 980 | } |
| 981 | self.snapshot(); |
| 982 | let clip = Clip { |
| 983 | start: timeline_frame, |
| 984 | link: None, |
| 985 | ..clip |
| 986 | }; |
| 987 | let clips = self.lane_mut(lane).expect("checked above"); |
| 988 | clear(clips, clip.start, clip.end()); |
| 989 | let idx = clips.partition_point(|c| c.start < clip.start); |
| 990 | clips.insert(idx, clip); |
| 991 | debug_assert!(sorted_disjoint(clips)); |
| 992 | true |
| 993 | } |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | pub fn paste(&mut self, timeline_frame: u32, clip: Clip) -> bool { |
| 1018 | let Some(source) = self.sources.get(clip.source) else { |
| 1019 | return false; |
| 1020 | }; |
| 1021 | if clip.out_frame <= clip.in_frame { |
| 1022 | return false; |
| 1023 | } |
| 1024 | let audio_only = crate::is_audio(&source.path); |
| 1025 | let at = timeline_frame.min(self.timeline_frames()); |
| 1026 | let len = clip.len(); |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | if at.checked_add(len).is_none() |
| 1033 | || self |
| 1034 | .lanes |
| 1035 | .iter() |
| 1036 | .flat_map(|l| &l.clips) |
| 1037 | .any(|c| c.end() > at && c.end().checked_add(len).is_none()) |
| 1038 | { |
| 1039 | return false; |
| 1040 | } |
| 1041 | self.snapshot(); |
| 1042 | let clip = Clip { |
| 1043 | start: at, |
| 1044 | link: Some(self.new_link()), |
| 1045 | ..clip |
| 1046 | }; |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | let kinds: &[LaneKind] = if audio_only { |
| 1052 | &[LaneKind::Audio] |
| 1053 | } else { |
| 1054 | &[LaneKind::Video, LaneKind::Audio] |
| 1055 | }; |
| 1056 | let takes: Vec<usize> = kinds |
| 1057 | .iter() |
| 1058 | .filter_map(|&kind| self.index(Lane::new(kind, 0))) |
| 1059 | .collect(); |
| 1060 | for (i, data) in self.lanes.iter_mut().enumerate() { |
| 1061 | open_room(&mut data.clips, at, clip.len()); |
| 1062 | if takes.contains(&i) { |
| 1063 | let idx = data.clips.partition_point(|c| c.start < at); |
| 1064 | data.clips.insert(idx, clip); |
| 1065 | } |
| 1066 | debug_assert!(sorted_disjoint(&data.clips)); |
| 1067 | } |
| 1068 | true |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | pub fn lift(&mut self, lane: Lane, idx: usize) -> bool { |
| 1077 | let placed: usize = self.lanes.iter().map(|l| l.clips.len()).sum(); |
| 1078 | if idx >= self.lane(lane).len() || placed == 1 { |
| 1079 | return false; |
| 1080 | } |
| 1081 | self.snapshot(); |
| 1082 | self.lane_mut(lane).expect("checked above").remove(idx); |
| 1083 | true |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool { |
| 1091 | if len == 0 { |
| 1092 | return false; |
| 1093 | } |
| 1094 | let survivors: usize = self |
| 1095 | .lanes |
| 1096 | .iter() |
| 1097 | .map(|l| { |
| 1098 | l.clips |
| 1099 | .iter() |
| 1100 | .filter(|c| c.start < at || c.end() > at + len) |
| 1101 | .count() |
| 1102 | }) |
| 1103 | .sum(); |
| 1104 | if survivors == 0 { |
| 1105 | return false; |
| 1106 | } |
| 1107 | self.snapshot(); |
| 1108 | for data in &mut self.lanes { |
| 1109 | clear(&mut data.clips, at, at + len); |
| 1110 | for c in data.clips.iter_mut().filter(|c| c.start >= at) { |
| 1111 | c.start -= len; |
| 1112 | } |
| 1113 | debug_assert!(sorted_disjoint(&data.clips)); |
| 1114 | } |
| 1115 | true |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | pub fn delete(&mut self, idx: usize) -> bool { |
| 1122 | self.delete_in(Lane::V1, idx) |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | pub fn delete_in(&mut self, lane: Lane, idx: usize) -> bool { |
| 1130 | let Some(clip) = self.lane(lane).get(idx).copied() else { |
| 1131 | return false; |
| 1132 | }; |
| 1133 | self.ripple_delete(clip.start, clip.len()) |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | pub fn undo(&mut self) -> bool { |
| 1139 | match self.history.pop() { |
| 1140 | Some(prev) => { |
| 1141 | self.lanes = prev; |
| 1142 | true |
| 1143 | } |
| 1144 | None => false, |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | pub fn segments_from(&self, timeline_frame: u32, fps: f64) -> Vec<(Option<usize>, f64, f64)> { |
| 1156 | self.lane_segments_from(Lane::A1, timeline_frame, fps) |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | pub fn lane_segments_from( |
| 1161 | &self, |
| 1162 | lane: Lane, |
| 1163 | timeline_frame: u32, |
| 1164 | fps: f64, |
| 1165 | ) -> Vec<(Option<usize>, f64, f64)> { |
| 1166 | if !(fps.is_finite() && fps > 0.0) { |
| 1167 | return Vec::new(); |
| 1168 | } |
| 1169 | self.spans_from(lane, timeline_frame) |
| 1170 | .iter() |
| 1171 | .map(|span| match span.from { |
| 1172 | |
| 1173 | |
| 1174 | Some((source, in_frame)) => ( |
| 1175 | Some(source), |
| 1176 | f64::from(in_frame) / fps, |
| 1177 | f64::from(in_frame + span.len) / fps, |
| 1178 | ), |
| 1179 | |
| 1180 | None => (None, 0.0, f64::from(span.len) / fps), |
| 1181 | }) |
| 1182 | .collect() |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | pub fn audio_segments_from( |
| 1195 | &self, |
| 1196 | timeline_frame: u32, |
| 1197 | fps: f64, |
| 1198 | ) -> Vec<Vec<(Option<usize>, f64, f64)>> { |
| 1199 | self.audio_lanes() |
| 1200 | .into_iter() |
| 1201 | .map(|lane| self.lane_segments_from(lane, timeline_frame, fps)) |
| 1202 | .collect() |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | pub fn audio_lanes(&self) -> Vec<Lane> { |
| 1212 | let lanes: Vec<Lane> = self |
| 1213 | .lanes() |
| 1214 | .into_iter() |
| 1215 | .filter(|l| l.kind == LaneKind::Audio && !self.lane(*l).is_empty()) |
| 1216 | .collect(); |
| 1217 | match lanes.is_empty() { |
| 1218 | |
| 1219 | |
| 1220 | true => vec![Lane::A1], |
| 1221 | false => lanes, |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | pub fn audio_eqs_from(&self, timeline_frame: u32, fps: f64) -> Vec<Vec<Option<EqParams>>> { |
| 1235 | self.audio_lanes() |
| 1236 | .into_iter() |
| 1237 | .map(|lane| self.lane_eqs_from(lane, timeline_frame, fps)) |
| 1238 | .collect() |
| 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | pub fn lane_eqs_from( |
| 1246 | &self, |
| 1247 | lane: Lane, |
| 1248 | timeline_frame: u32, |
| 1249 | fps: f64, |
| 1250 | ) -> Vec<Option<EqParams>> { |
| 1251 | if !(fps.is_finite() && fps > 0.0) { |
| 1252 | return Vec::new(); |
| 1253 | } |
| 1254 | self.spans_from(lane, timeline_frame) |
| 1255 | .iter() |
| 1256 | |
| 1257 | |
| 1258 | .map(|span| { |
| 1259 | self.map(lane, span.start) |
| 1260 | .and_then(|(idx, _)| self.eq_of(lane, idx).cloned()) |
| 1261 | }) |
| 1262 | .collect() |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | fn snapshot(&mut self) { |
| 1268 | self.history.push(self.lanes.clone()); |
| 1269 | } |
| 1270 | |
| 1271 | fn new_link(&mut self) -> u32 { |
| 1272 | let id = self.next_link; |
| 1273 | self.next_link = self.next_link.saturating_add(1); |
| 1274 | id |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | fn group_ids<K: PartialEq>(&mut self, keys: &[Option<K>]) -> Vec<Option<u32>> { |
| 1283 | |
| 1284 | |
| 1285 | let mut seen: Vec<(&K, u32)> = Vec::new(); |
| 1286 | let mut out = Vec::with_capacity(keys.len()); |
| 1287 | for key in keys { |
| 1288 | out.push( |
| 1289 | key.as_ref() |
| 1290 | .map(|k| match seen.iter().find(|(other, _)| *other == k) { |
| 1291 | Some(&(_, id)) => id, |
| 1292 | None => { |
| 1293 | let id = self.new_link(); |
| 1294 | seen.push((k, id)); |
| 1295 | id |
| 1296 | } |
| 1297 | }), |
| 1298 | ); |
| 1299 | } |
| 1300 | out |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | fn handles(lanes: &[LaneData]) -> Vec<Lane> { |
| 1307 | let (mut video, mut audio) = (0, 0); |
| 1308 | lanes |
| 1309 | .iter() |
| 1310 | .map(|l| match l.kind { |
| 1311 | LaneKind::Video => { |
| 1312 | video += 1; |
| 1313 | Lane::new(LaneKind::Video, video - 1) |
| 1314 | } |
| 1315 | LaneKind::Audio => { |
| 1316 | audio += 1; |
| 1317 | Lane::new(LaneKind::Audio, audio - 1) |
| 1318 | } |
| 1319 | }) |
| 1320 | .collect() |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | fn at(clips: &[Clip], frame: u32) -> Option<usize> { |
| 1326 | let idx = clips.partition_point(|c| c.start <= frame).checked_sub(1)?; |
| 1327 | (frame < clips[idx].end()).then_some(idx) |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | fn splittable(clips: &[Clip], frame: u32) -> Option<usize> { |
| 1333 | let idx = at(clips, frame)?; |
| 1334 | (frame > clips[idx].start).then_some(idx) |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | fn joinable(clips: &[Clip], frame: u32) -> Option<usize> { |
| 1340 | let idx = clips.iter().position(|c| c.end() == frame)?; |
| 1341 | let next = clips.get(idx + 1)?; |
| 1342 | (next.start == frame |
| 1343 | && next.source == clips[idx].source |
| 1344 | && next.in_frame == clips[idx].out_frame) |
| 1345 | .then_some(idx) |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | fn finite(params: &EqParams) -> bool { |
| 1355 | params |
| 1356 | .bands |
| 1357 | .iter() |
| 1358 | .all(|b| b.freq_hz.is_finite() && b.gain_db.is_finite() && b.q.is_finite()) |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | |
| 1363 | |
| 1364 | fn color_finite(p: &ColorParams) -> bool { |
| 1365 | p.brightness.is_finite() |
| 1366 | && p.contrast.is_finite() |
| 1367 | && p.saturation.is_finite() |
| 1368 | && p.tint.is_finite() |
| 1369 | } |
| 1370 | |
| 1371 | fn sorted_disjoint(clips: &[Clip]) -> bool { |
| 1372 | clips.iter().all(|c| c.out_frame > c.in_frame) |
| 1373 | && clips.windows(2).all(|w| w[0].end() <= w[1].start) |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | fn links_are_consistent(lanes: &[LaneData]) -> crate::Result<()> { |
| 1390 | let handles = handles(lanes); |
| 1391 | for (data, lane) in lanes.iter().zip(&handles) { |
| 1392 | for (i, c) in data.clips.iter().enumerate() { |
| 1393 | let Some(id) = c.link else { continue }; |
| 1394 | if data.clips[..i].iter().any(|prev| prev.link == Some(id)) { |
| 1395 | return Err( |
| 1396 | format!("link {id} names two clips in the {} lane", lane.label()).into(), |
| 1397 | ); |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | let mut seen: Vec<(u32, Lane, u32, u32)> = Vec::new(); |
| 1403 | for (data, &lane) in lanes.iter().zip(&handles) { |
| 1404 | for c in &data.clips { |
| 1405 | let Some(id) = c.link else { continue }; |
| 1406 | match seen.iter().find(|(other, ..)| *other == id) { |
| 1407 | Some(&(_, first, start, end)) => { |
| 1408 | if (start, end) != (c.start, c.end()) { |
| 1409 | return Err(format!( |
| 1410 | "link {id} covers [{start}, {end}) in {} and [{}, {}) in {}", |
| 1411 | first.label(), |
| 1412 | c.start, |
| 1413 | c.end(), |
| 1414 | lane.label() |
| 1415 | ) |
| 1416 | .into()); |
| 1417 | } |
| 1418 | } |
| 1419 | None => seen.push((id, lane, c.start, c.end())), |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | Ok(()) |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | fn clear(clips: &mut Vec<Clip>, start: u32, end: u32) { |
| 1435 | let mut out = Vec::with_capacity(clips.len() + 1); |
| 1436 | for c in clips.drain(..) { |
| 1437 | |
| 1438 | if c.end() <= start || c.start >= end { |
| 1439 | out.push(c); |
| 1440 | continue; |
| 1441 | } |
| 1442 | |
| 1443 | if c.start < start { |
| 1444 | out.push(Clip { |
| 1445 | out_frame: c.in_frame + (start - c.start), |
| 1446 | link: None, |
| 1447 | ..c |
| 1448 | }); |
| 1449 | } |
| 1450 | |
| 1451 | if c.end() > end { |
| 1452 | out.push(Clip { |
| 1453 | start: end, |
| 1454 | in_frame: c.in_frame + (end - c.start), |
| 1455 | link: None, |
| 1456 | ..c |
| 1457 | }); |
| 1458 | } |
| 1459 | } |
| 1460 | *clips = out; |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | fn open_room(clips: &mut Vec<Clip>, at: u32, len: u32) { |
| 1468 | if let Some(idx) = splittable(clips, at) { |
| 1469 | let mut tail = clips[idx]; |
| 1470 | tail.in_frame += at - tail.start; |
| 1471 | tail.start = at; |
| 1472 | tail.link = None; |
| 1473 | clips[idx].out_frame = tail.in_frame; |
| 1474 | clips[idx].link = None; |
| 1475 | clips.insert(idx + 1, tail); |
| 1476 | } |
| 1477 | for c in clips.iter_mut().filter(|c| c.start >= at) { |
| 1478 | c.start += len; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | fn canonical(path: &Path) -> PathBuf { |
| 1486 | std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) |
| 1487 | } |
| 1488 | |
| 1489 | #[cfg(test)] |
| 1490 | mod tests { |
| 1491 | use super::*; |
| 1492 | |
| 1493 | const FPS: f64 = 30.0; |
| 1494 | |
| 1495 | |
| 1496 | const FILE: &str = "/nonexistent/a.mp4"; |
| 1497 | const FILE2: &str = "/nonexistent/b.mp4"; |
| 1498 | |
| 1499 | |
| 1500 | fn clip(start: u32, in_frame: u32, out_frame: u32, source: usize) -> Clip { |
| 1501 | Clip { |
| 1502 | start, |
| 1503 | in_frame, |
| 1504 | out_frame, |
| 1505 | source, |
| 1506 | link: None, |
| 1507 | eq: None, |
| 1508 | color: None, |
| 1509 | fit: FitPolicy::default(), |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | fn band_at(n: u32) -> EqParams { |
| 1516 | EqParams { |
| 1517 | bands: vec![crate::eq::Band { |
| 1518 | freq_hz: 1000.0, |
| 1519 | gain_db: f64::from(n) as f32 * 1.5, |
| 1520 | q: 0.707, |
| 1521 | kind: crate::eq::BandKind::Peak, |
| 1522 | }], |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | fn grade_at(n: u32) -> ColorParams { |
| 1529 | ColorParams { |
| 1530 | brightness: f64::from(n) as f32 * 0.05, |
| 1531 | ..Default::default() |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | fn two(video: Vec<Clip>, audio: Vec<Clip>) -> Vec<(LaneKind, Vec<Clip>)> { |
| 1537 | vec![(LaneKind::Video, video), (LaneKind::Audio, audio)] |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | fn shape(p: &Project) -> Vec<Vec<Clip>> { |
| 1542 | p.lanes() |
| 1543 | .into_iter() |
| 1544 | .map(|l| { |
| 1545 | p.lane(l) |
| 1546 | .iter() |
| 1547 | .map(|c| Clip { link: None, ..*c }) |
| 1548 | .collect() |
| 1549 | }) |
| 1550 | .collect() |
| 1551 | } |
| 1552 | |
| 1553 | |
| 1554 | |
| 1555 | |
| 1556 | fn traversal(p: &Project) -> Vec<Vec<Option<u32>>> { |
| 1557 | (0..p.timeline_frames() + 2) |
| 1558 | .map(|f| { |
| 1559 | p.lanes() |
| 1560 | .into_iter() |
| 1561 | .map(|l| p.map(l, f).map(|(_, source)| source)) |
| 1562 | .collect() |
| 1563 | }) |
| 1564 | .collect() |
| 1565 | } |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | fn three() -> Project { |
| 1570 | let mut p = Project::single(FILE, 9); |
| 1571 | assert!(p.split(3)); |
| 1572 | assert!(p.split(5)); |
| 1573 | assert_eq!( |
| 1574 | shape(&p), |
| 1575 | [ |
| 1576 | vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 9, 0)], |
| 1577 | vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 9, 0)], |
| 1578 | ] |
| 1579 | ); |
| 1580 | p |
| 1581 | } |
| 1582 | |
| 1583 | #[test] |
| 1584 | fn single_is_the_whole_file_on_both_lanes() { |
| 1585 | let p = Project::single(FILE, 150); |
| 1586 | assert_eq!(p.clips().len(), 1); |
| 1587 | assert_eq!(p.lane(Lane::A1).len(), 1); |
| 1588 | assert_eq!(p.timeline_frames(), 150); |
| 1589 | assert_eq!(p.clip_spans(), vec![(0, 150)]); |
| 1590 | |
| 1591 | assert_eq!(p.clips()[0].link, p.lane(Lane::A1)[0].link); |
| 1592 | assert!(p.clips()[0].link.is_some()); |
| 1593 | |
| 1594 | assert_eq!(p.map_timeline(0), Some((0, 0))); |
| 1595 | assert_eq!(p.map_timeline(149), Some((0, 149))); |
| 1596 | assert_eq!(p.map_timeline(150), None); |
| 1597 | |
| 1598 | assert_eq!(Project::single(FILE, 0).timeline_frames(), 1); |
| 1599 | } |
| 1600 | |
| 1601 | #[test] |
| 1602 | fn split_cuts_every_lane_and_regroups_it_back() { |
| 1603 | let mut p = Project::single(FILE, 9); |
| 1604 | let before = traversal(&p); |
| 1605 | let before_shape = shape(&p); |
| 1606 | |
| 1607 | assert!(p.split(4)); |
| 1608 | for l in p.lanes() { |
| 1609 | assert_eq!(p.lane(l).len(), 2, "{} split", l.label()); |
| 1610 | } |
| 1611 | |
| 1612 | let (v, a) = (p.lane(Lane::V1), p.lane(Lane::A1)); |
| 1613 | assert_eq!(v[0].link, a[0].link); |
| 1614 | assert_eq!(v[1].link, a[1].link); |
| 1615 | assert_ne!(v[0].link, v[1].link, "the halves are no longer one take"); |
| 1616 | |
| 1617 | assert_eq!(traversal(&p), before, "a split moves nothing"); |
| 1618 | |
| 1619 | assert!(p.regroup(4), "the inverse rejoins them"); |
| 1620 | assert_eq!(shape(&p), before_shape, "bit-exact back to one clip"); |
| 1621 | assert_eq!(traversal(&p), before); |
| 1622 | assert_eq!( |
| 1623 | p.lane(Lane::V1)[0].link, |
| 1624 | p.lane(Lane::A1)[0].link, |
| 1625 | "and the rejoined clip is one group again" |
| 1626 | ); |
| 1627 | } |
| 1628 | |
| 1629 | #[test] |
| 1630 | fn split_refused_at_zero_boundary_end_and_in_a_gap() { |
| 1631 | let mut p = three(); |
| 1632 | let before = shape(&p); |
| 1633 | assert!(!p.split(0), "timeline 0 has nothing before it"); |
| 1634 | assert!(!p.split(3), "existing boundary"); |
| 1635 | assert!(!p.split(5), "existing boundary"); |
| 1636 | assert!(!p.split(9), "one past the last frame"); |
| 1637 | assert!(!p.split(1_000)); |
| 1638 | assert_eq!(shape(&p), before); |
| 1639 | |
| 1640 | assert!(p.undo()); |
| 1641 | assert_eq!(p.clips().len(), 2, "refused splits push no history"); |
| 1642 | |
| 1643 | |
| 1644 | let mut p = three(); |
| 1645 | assert!(p.lift(Lane::V1, 1)); |
| 1646 | assert!(p.lift(Lane::A1, 1)); |
| 1647 | assert!(!p.split(4), "a gap is not a clip"); |
| 1648 | } |
| 1649 | |
| 1650 | #[test] |
| 1651 | fn regroup_refuses_what_a_split_could_not_have_made() { |
| 1652 | let mut p = three(); |
| 1653 | |
| 1654 | assert!(p.regroup(5)); |
| 1655 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 9, 0)]); |
| 1656 | |
| 1657 | |
| 1658 | let mut p = three(); |
| 1659 | assert!(p.lift(Lane::V1, 1)); |
| 1660 | assert!(p.lift(Lane::A1, 1)); |
| 1661 | assert!(!p.regroup(3), "nothing starts at 3 any more"); |
| 1662 | let mut p = three(); |
| 1663 | for l in p.lanes() { |
| 1664 | assert!(p.place(l, 3, clip(0, 100, 102, 0))); |
| 1665 | } |
| 1666 | assert!(!p.regroup(3), "source 100 does not follow source 3"); |
| 1667 | assert!(!p.regroup(0), "nothing ends at 0"); |
| 1668 | assert!(!p.regroup(1_000)); |
| 1669 | } |
| 1670 | |
| 1671 | #[test] |
| 1672 | fn map_and_spans_sweep_every_boundary() { |
| 1673 | let p = three(); |
| 1674 | assert_eq!(p.timeline_frames(), 9); |
| 1675 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4)]); |
| 1676 | |
| 1677 | let expect = [ |
| 1678 | (0, (0, 0)), |
| 1679 | (1, (0, 1)), |
| 1680 | (2, (0, 2)), |
| 1681 | (3, (1, 3)), |
| 1682 | (4, (1, 4)), |
| 1683 | (5, (2, 5)), |
| 1684 | (6, (2, 6)), |
| 1685 | (7, (2, 7)), |
| 1686 | (8, (2, 8)), |
| 1687 | ]; |
| 1688 | for (t, want) in expect { |
| 1689 | assert_eq!(p.map_timeline(t), Some(want), "timeline frame {t}"); |
| 1690 | } |
| 1691 | assert_eq!(p.map_timeline(9), None); |
| 1692 | |
| 1693 | assert_eq!( |
| 1694 | p.span_at(Lane::V1, 6), |
| 1695 | Some(Span { |
| 1696 | start: 6, |
| 1697 | len: 3, |
| 1698 | from: Some((0, 6)) |
| 1699 | }) |
| 1700 | ); |
| 1701 | assert_eq!(p.span_at(Lane::V1, 9), None); |
| 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | #[test] |
| 1712 | fn a_fit_policy_costs_the_clip_no_word() { |
| 1713 | assert_eq!( |
| 1714 | std::mem::size_of::<Clip>(), |
| 1715 | 40, |
| 1716 | "Clip changed size: {} bytes", |
| 1717 | std::mem::size_of::<Clip>() |
| 1718 | ); |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | |
| 1723 | const PASTED: Clip = Clip { |
| 1724 | start: 0, |
| 1725 | in_frame: 100, |
| 1726 | out_frame: 102, |
| 1727 | source: 0, |
| 1728 | link: None, |
| 1729 | eq: None, |
| 1730 | color: None, |
| 1731 | fit: FitPolicy::Fit, |
| 1732 | }; |
| 1733 | |
| 1734 | #[test] |
| 1735 | fn paste_mid_clip_splits_around_it() { |
| 1736 | let mut p = three(); |
| 1737 | assert!(p.paste(6, PASTED)); |
| 1738 | assert_eq!(p.timeline_frames(), 11); |
| 1739 | assert_eq!( |
| 1740 | shape(&p)[0], |
| 1741 | vec![ |
| 1742 | clip(0, 0, 3, 0), |
| 1743 | clip(3, 3, 5, 0), |
| 1744 | clip(5, 5, 6, 0), |
| 1745 | clip(6, 100, 102, 0), |
| 1746 | clip(8, 6, 9, 0), |
| 1747 | ] |
| 1748 | ); |
| 1749 | assert_eq!(shape(&p)[1], shape(&p)[0], "a paste lands on both lanes"); |
| 1750 | |
| 1751 | |
| 1752 | assert_eq!(p.map_timeline(6), Some((3, 100))); |
| 1753 | assert_eq!(p.map_timeline(7), Some((3, 101))); |
| 1754 | assert_eq!(p.map_timeline(8), Some((4, 6))); |
| 1755 | } |
| 1756 | |
| 1757 | #[test] |
| 1758 | fn paste_at_boundary_zero_and_end() { |
| 1759 | let mut p = three(); |
| 1760 | assert!(p.paste(3, PASTED), "existing boundary: no split"); |
| 1761 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 2), (7, 4)]); |
| 1762 | assert_eq!(p.map_timeline(3), Some((1, 100))); |
| 1763 | |
| 1764 | let mut p = three(); |
| 1765 | assert!(p.paste(0, PASTED)); |
| 1766 | assert_eq!(p.map_timeline(0), Some((0, 100))); |
| 1767 | assert_eq!(p.map_timeline(2), Some((1, 0))); |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | let mut p = three(); |
| 1772 | assert!(p.paste(9, PASTED)); |
| 1773 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4), (9, 2)]); |
| 1774 | assert!(p.paste(1_000, PASTED)); |
| 1775 | assert_eq!(p.timeline_frames(), 13); |
| 1776 | |
| 1777 | |
| 1778 | let mut p = three(); |
| 1779 | assert!(!p.paste(4, clip(0, 7, 7, 0))); |
| 1780 | assert_eq!(p.clips().len(), 3); |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | #[test] |
| 1787 | fn a_paste_of_a_source_with_no_picture_stays_off_the_video_lane() { |
| 1788 | let mut p = three(); |
| 1789 | let wav = p.import("/nonexistent/song.wav", 0); |
| 1790 | let copied = Clip { |
| 1791 | source: wav, |
| 1792 | ..PASTED |
| 1793 | }; |
| 1794 | assert!(p.paste(3, copied)); |
| 1795 | |
| 1796 | assert_eq!(p.timeline_frames(), 11); |
| 1797 | assert_eq!( |
| 1798 | shape(&p)[0], |
| 1799 | vec![clip(0, 0, 3, 0), clip(5, 3, 5, 0), clip(7, 5, 9, 0)], |
| 1800 | "the video lane got room and nothing else" |
| 1801 | ); |
| 1802 | |
| 1803 | assert_eq!( |
| 1804 | shape(&p)[1], |
| 1805 | vec![ |
| 1806 | clip(0, 0, 3, 0), |
| 1807 | clip(3, 100, 102, wav), |
| 1808 | clip(5, 3, 5, 0), |
| 1809 | clip(7, 5, 9, 0) |
| 1810 | ] |
| 1811 | ); |
| 1812 | |
| 1813 | let mut p = three(); |
| 1814 | assert!(p.paste(3, PASTED)); |
| 1815 | assert_eq!(shape(&p)[0], shape(&p)[1]); |
| 1816 | } |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | #[test] |
| 1823 | fn a_paste_that_would_run_past_the_last_frame_is_refused() { |
| 1824 | let far = clip(u32::MAX - 90, 0, 90, 0); |
| 1825 | let parts = |c: Clip| { |
| 1826 | Project::from_parts( |
| 1827 | vec![Source::new(FILE, 0)], |
| 1828 | two(vec![c], vec![c]), |
| 1829 | Vec::new(), |
| 1830 | Vec::new(), |
| 1831 | ) |
| 1832 | .expect("valid parts") |
| 1833 | }; |
| 1834 | let mut p = parts(far); |
| 1835 | assert!(!p.paste(0, PASTED), "no room for the two frames it adds"); |
| 1836 | assert_eq!(shape(&p)[0], vec![far], "a refusal changes nothing"); |
| 1837 | assert!(!p.undo(), "...and pushes no history either"); |
| 1838 | assert!( |
| 1839 | !p.paste(u32::MAX, PASTED), |
| 1840 | "appending has nowhere to go either: the timeline ends at the last frame" |
| 1841 | ); |
| 1842 | |
| 1843 | let mut p = parts(clip(u32::MAX - 92, 0, 90, 0)); |
| 1844 | assert!(p.paste(0, PASTED)); |
| 1845 | assert_eq!(p.lane(Lane::V1)[1].end(), u32::MAX); |
| 1846 | } |
| 1847 | |
| 1848 | #[test] |
| 1849 | fn paste_undoes_in_one_step() { |
| 1850 | let mut p = three(); |
| 1851 | let before = shape(&p); |
| 1852 | assert!(p.paste(6, PASTED)); |
| 1853 | assert!(p.undo()); |
| 1854 | assert_eq!(shape(&p), before, "a paste is one undo step"); |
| 1855 | } |
| 1856 | |
| 1857 | #[test] |
| 1858 | fn a_copy_outlives_the_clip_it_came_from() { |
| 1859 | let mut p = three(); |
| 1860 | let copied = p.clips()[1]; |
| 1861 | assert!(p.delete(1)); |
| 1862 | |
| 1863 | assert!(p.paste(0, copied)); |
| 1864 | assert_eq!(p.map_timeline(0), Some((0, 3))); |
| 1865 | assert_eq!(p.timeline_frames(), 9); |
| 1866 | } |
| 1867 | |
| 1868 | #[test] |
| 1869 | fn delete_closes_the_gap_on_both_lanes() { |
| 1870 | let mut p = three(); |
| 1871 | assert!(p.delete(1)); |
| 1872 | assert_eq!(p.timeline_frames(), 7); |
| 1873 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 4)]); |
| 1874 | assert_eq!(p.lane_spans(Lane::A1), vec![(0, 3), (3, 4)]); |
| 1875 | |
| 1876 | assert_eq!(p.map_timeline(3), Some((1, 5))); |
| 1877 | assert_eq!(p.map_timeline(6), Some((1, 8))); |
| 1878 | assert_eq!(p.map_timeline(7), None); |
| 1879 | } |
| 1880 | |
| 1881 | #[test] |
| 1882 | fn delete_first_last_and_only() { |
| 1883 | let mut p = three(); |
| 1884 | assert!(p.delete(0)); |
| 1885 | assert_eq!(p.map_timeline(0), Some((0, 3))); |
| 1886 | assert_eq!(p.timeline_frames(), 6); |
| 1887 | |
| 1888 | let mut p = three(); |
| 1889 | assert!(p.delete(2)); |
| 1890 | assert_eq!(p.timeline_frames(), 5); |
| 1891 | assert_eq!(p.map_timeline(4), Some((1, 4))); |
| 1892 | |
| 1893 | assert!(!p.delete(2), "index past the end"); |
| 1894 | assert!(p.delete(1)); |
| 1895 | assert!(!p.delete(0), "the last remaining clip stays"); |
| 1896 | assert_eq!(p.clips().len(), 1); |
| 1897 | } |
| 1898 | |
| 1899 | |
| 1900 | #[test] |
| 1901 | fn a_lift_leaves_a_gap_and_moves_nothing() { |
| 1902 | let mut p = three(); |
| 1903 | let video_before = shape(&p)[0].clone(); |
| 1904 | assert!(p.lift(Lane::A1, 1)); |
| 1905 | assert_eq!(shape(&p)[0], video_before, "the picture never moved"); |
| 1906 | assert_eq!(p.timeline_frames(), 9, "nor did the timeline get shorter"); |
| 1907 | assert_eq!(p.lane_spans(Lane::A1), vec![(0, 3), (5, 4)]); |
| 1908 | |
| 1909 | assert_eq!(p.map(Lane::A1, 4), None); |
| 1910 | assert_eq!( |
| 1911 | p.span_at(Lane::A1, 3), |
| 1912 | Some(Span { |
| 1913 | start: 3, |
| 1914 | len: 2, |
| 1915 | from: None |
| 1916 | }) |
| 1917 | ); |
| 1918 | assert_eq!(p.map(Lane::V1, 4), Some((1, 4)), "video plays on"); |
| 1919 | assert!(p.undo()); |
| 1920 | assert_eq!(p.lane_spans(Lane::A1), vec![(0, 3), (3, 2), (5, 4)]); |
| 1921 | } |
| 1922 | |
| 1923 | #[test] |
| 1924 | fn a_trailing_gap_is_a_gap_to_the_end_of_the_timeline() { |
| 1925 | let mut p = three(); |
| 1926 | assert!(p.lift(Lane::A1, 2)); |
| 1927 | assert_eq!(p.timeline_frames(), 9, "video still runs to 9"); |
| 1928 | assert_eq!( |
| 1929 | p.span_at(Lane::A1, 5), |
| 1930 | Some(Span { |
| 1931 | start: 5, |
| 1932 | len: 4, |
| 1933 | from: None |
| 1934 | }), |
| 1935 | "the audio lane holds silence to the end of the picture" |
| 1936 | ); |
| 1937 | assert_eq!(p.span_at(Lane::A1, 9), None); |
| 1938 | |
| 1939 | for l in p.lanes() { |
| 1940 | let spans = p.spans_from(l, 0); |
| 1941 | assert_eq!(spans.iter().map(|s| s.len).sum::<u32>(), 9, "{}", l.label()); |
| 1942 | assert_eq!(spans[0].start, 0); |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | #[test] |
| 1947 | fn the_last_clip_of_the_last_lane_cannot_be_lifted() { |
| 1948 | let mut p = Project::single(FILE, 9); |
| 1949 | assert!(p.lift(Lane::A1, 0), "a silent timeline is fine"); |
| 1950 | assert!(!p.lift(Lane::V1, 0), "an empty one is not"); |
| 1951 | assert!(!p.lift(Lane::A1, 0), "index past the end"); |
| 1952 | assert_eq!(p.timeline_frames(), 9); |
| 1953 | } |
| 1954 | |
| 1955 | |
| 1956 | |
| 1957 | |
| 1958 | #[test] |
| 1959 | fn lane_clip_at_answers_for_each_lane_separately() { |
| 1960 | let mut p = Project::single(FILE, 9); |
| 1961 | assert!(p.split(3), "both lanes split"); |
| 1962 | assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0)); |
| 1963 | assert_eq!(p.lane_clip_at(Lane::V1, 3), Some(1)); |
| 1964 | assert_eq!(p.lane_clip_at(Lane::A1, 3), Some(1)); |
| 1965 | assert_eq!(p.lane_clip_at(Lane::V1, 9), None, "past the end"); |
| 1966 | |
| 1967 | assert!(p.lift(Lane::A1, 0)); |
| 1968 | assert_eq!(p.lane_clip_at(Lane::A1, 0), None, "the gap holds nothing"); |
| 1969 | assert_eq!(p.lane_clip_at(Lane::A1, 3), Some(0), "indices moved with it"); |
| 1970 | assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0)); |
| 1971 | |
| 1972 | assert_eq!(p.lane_clip_at(Lane::new(LaneKind::Video, 1), 0), None); |
| 1973 | } |
| 1974 | |
| 1975 | #[test] |
| 1976 | fn place_overwrites_only_its_own_lane() { |
| 1977 | let mut p = three(); |
| 1978 | let audio_before = shape(&p)[1].clone(); |
| 1979 | |
| 1980 | |
| 1981 | assert!(p.place(Lane::V1, 4, clip(0, 100, 102, 0))); |
| 1982 | assert_eq!( |
| 1983 | shape(&p)[0], |
| 1984 | vec![ |
| 1985 | clip(0, 0, 3, 0), |
| 1986 | clip(3, 3, 4, 0), |
| 1987 | clip(4, 100, 102, 0), |
| 1988 | clip(6, 6, 9, 0), |
| 1989 | ] |
| 1990 | ); |
| 1991 | assert_eq!(shape(&p)[1], audio_before, "the audio lane is untouched"); |
| 1992 | assert_eq!(p.timeline_frames(), 9, "an overwrite is not an insert"); |
| 1993 | |
| 1994 | |
| 1995 | let mut p = Project::single(FILE, 9); |
| 1996 | assert!(p.place(Lane::V1, 4, clip(0, 100, 101, 0))); |
| 1997 | assert_eq!( |
| 1998 | shape(&p)[0], |
| 1999 | vec![clip(0, 0, 4, 0), clip(4, 100, 101, 0), clip(5, 5, 9, 0)] |
| 2000 | ); |
| 2001 | |
| 2002 | assert!(p.place(Lane::V1, 20, clip(0, 100, 102, 0))); |
| 2003 | assert_eq!(p.timeline_frames(), 22); |
| 2004 | assert_eq!( |
| 2005 | p.span_at(Lane::V1, 9), |
| 2006 | Some(Span { |
| 2007 | start: 9, |
| 2008 | len: 11, |
| 2009 | from: None |
| 2010 | }) |
| 2011 | ); |
| 2012 | assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip"); |
| 2013 | } |
| 2014 | |
| 2015 | #[test] |
| 2016 | fn ripple_delete_spans_partial_clips() { |
| 2017 | let mut p = three(); |
| 2018 | |
| 2019 | assert!(p.ripple_delete(2, 4)); |
| 2020 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 2, 0), clip(2, 6, 9, 0)]); |
| 2021 | assert_eq!(shape(&p)[1], shape(&p)[0]); |
| 2022 | assert_eq!(p.timeline_frames(), 5); |
| 2023 | assert!(!p.ripple_delete(0, 0), "an empty range is not a delete"); |
| 2024 | assert!(!p.ripple_delete(0, 100), "and one that empties every lane"); |
| 2025 | assert!(p.undo()); |
| 2026 | assert_eq!(shape(&p), shape(&three())); |
| 2027 | } |
| 2028 | |
| 2029 | #[test] |
| 2030 | fn undo_restores_exactly() { |
| 2031 | let mut p = three(); |
| 2032 | let after_splits = shape(&p); |
| 2033 | assert!(p.delete(1)); |
| 2034 | assert!(p.undo()); |
| 2035 | assert_eq!(shape(&p), after_splits); |
| 2036 | |
| 2037 | |
| 2038 | assert!(p.undo()); |
| 2039 | assert_eq!(p.clips().len(), 2); |
| 2040 | assert!(p.undo()); |
| 2041 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); |
| 2042 | assert!(!p.undo(), "empty history"); |
| 2043 | assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); |
| 2044 | } |
| 2045 | |
| 2046 | |
| 2047 | |
| 2048 | #[test] |
| 2049 | fn group_ids_are_never_reused() { |
| 2050 | let mut p = Project::single(FILE, 9); |
| 2051 | assert!(p.split(3)); |
| 2052 | let first = p.clips()[1].link; |
| 2053 | assert!(p.undo()); |
| 2054 | assert!(p.split(5)); |
| 2055 | assert_ne!(p.clips()[1].link, first, "a retired id must not come back"); |
| 2056 | } |
| 2057 | |
| 2058 | #[test] |
| 2059 | fn segments_from_reads_the_audio_lane() { |
| 2060 | let p = three(); |
| 2061 | |
| 2062 | assert_eq!( |
| 2063 | p.segments_from(4, FPS), |
| 2064 | vec![ |
| 2065 | (Some(0), 4.0 / 30.0, 5.0 / 30.0), |
| 2066 | (Some(0), 5.0 / 30.0, 9.0 / 30.0) |
| 2067 | ] |
| 2068 | ); |
| 2069 | |
| 2070 | assert_eq!( |
| 2071 | p.segments_from(3, FPS), |
| 2072 | vec![ |
| 2073 | (Some(0), 3.0 / 30.0, 5.0 / 30.0), |
| 2074 | (Some(0), 5.0 / 30.0, 9.0 / 30.0) |
| 2075 | ] |
| 2076 | ); |
| 2077 | |
| 2078 | assert_eq!(p.segments_from(0, FPS).len(), 3); |
| 2079 | |
| 2080 | assert_eq!(p.segments_from(8, FPS), vec![(Some(0), 8.0 / 30.0, 0.3)]); |
| 2081 | |
| 2082 | assert!(p.segments_from(9, FPS).is_empty()); |
| 2083 | assert!(p.segments_from(0, 0.0).is_empty()); |
| 2084 | assert!(p.segments_from(0, f64::NAN).is_empty()); |
| 2085 | } |
| 2086 | |
| 2087 | #[test] |
| 2088 | fn a_gap_in_the_audio_lane_is_a_silent_segment_of_its_own_length() { |
| 2089 | let mut p = three(); |
| 2090 | assert!(p.lift(Lane::A1, 1)); |
| 2091 | assert_eq!( |
| 2092 | p.segments_from(0, FPS), |
| 2093 | vec![ |
| 2094 | (Some(0), 0.0, 3.0 / 30.0), |
| 2095 | (None, 0.0, 2.0 / 30.0), |
| 2096 | (Some(0), 5.0 / 30.0, 9.0 / 30.0), |
| 2097 | ] |
| 2098 | ); |
| 2099 | |
| 2100 | |
| 2101 | let played: f64 = p.segments_from(0, FPS).iter().map(|(_, a, b)| b - a).sum(); |
| 2102 | assert!((played - 9.0 / 30.0).abs() < 1e-9, "{played}"); |
| 2103 | |
| 2104 | assert_eq!( |
| 2105 | p.segments_from(4, FPS), |
| 2106 | vec![(None, 0.0, 1.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0)] |
| 2107 | ); |
| 2108 | |
| 2109 | assert!(p.lift(Lane::A1, 0)); |
| 2110 | assert!(p.lift(Lane::A1, 0)); |
| 2111 | assert_eq!(p.segments_from(0, FPS), vec![(None, 0.0, 0.3)]); |
| 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | fn two_sources() -> Project { |
| 2117 | let mut p = three(); |
| 2118 | let s = p.import(FILE2, 0); |
| 2119 | assert_eq!(s, 1); |
| 2120 | assert!(p.append_clip(s, 4)); |
| 2121 | p |
| 2122 | } |
| 2123 | |
| 2124 | #[test] |
| 2125 | fn import_dedups_and_appends() { |
| 2126 | let mut p = Project::single(FILE, 9); |
| 2127 | assert_eq!(p.sources().len(), 1, "the opened file is source 0"); |
| 2128 | assert_eq!(p.import(FILE, 0), 0, "reimporting the open file reuses 0"); |
| 2129 | assert_eq!(p.import(FILE2, 0), 1); |
| 2130 | assert_eq!(p.import(FILE2, 0), 1, "second import of the same path"); |
| 2131 | |
| 2132 | |
| 2133 | assert_eq!(p.import(FILE2, 1), 2, "a second stream is a second source"); |
| 2134 | assert_eq!(p.import(FILE2, 1), 2, "and dedups on the pair"); |
| 2135 | assert_eq!(p.sources()[2].audio_stream, 1); |
| 2136 | assert!(p.append_clip(2, 4), "a stream entry is placeable"); |
| 2137 | assert!(p.undo()); |
| 2138 | assert_eq!(p.sources().len(), 3); |
| 2139 | assert!(!p.append_clip(3, 5), "unknown source index"); |
| 2140 | assert_eq!(p.clips().len(), 1, "a refusal changes nothing"); |
| 2141 | |
| 2142 | |
| 2143 | |
| 2144 | let here = concat!(env!("CARGO_MANIFEST_DIR"), "/src/project.rs"); |
| 2145 | let detour = concat!(env!("CARGO_MANIFEST_DIR"), "/src/../src/project.rs"); |
| 2146 | let mut p = Project::single(here, 9); |
| 2147 | assert_eq!(p.import(detour, 0), 0); |
| 2148 | assert_eq!(p.sources().len(), 1); |
| 2149 | } |
| 2150 | |
| 2151 | #[test] |
| 2152 | fn append_clip_is_one_undo_step() { |
| 2153 | let mut p = two_sources(); |
| 2154 | assert_eq!(p.timeline_frames(), 13); |
| 2155 | assert_eq!(shape(&p)[0][3], clip(9, 0, 4, 1)); |
| 2156 | assert_eq!(shape(&p)[1][3], clip(9, 0, 4, 1), "and on the audio lane"); |
| 2157 | assert_eq!(p.map_timeline(9), Some((3, 0)), "source 1 starts at 9"); |
| 2158 | assert!(p.undo()); |
| 2159 | assert_eq!(shape(&p), shape(&three()), "one step back to one source"); |
| 2160 | assert_eq!( |
| 2161 | p.sources().len(), |
| 2162 | 2, |
| 2163 | "the orphan source entry stays -- indexes are forever" |
| 2164 | ); |
| 2165 | |
| 2166 | assert!(p.append_clip(1, 0)); |
| 2167 | assert_eq!(p.clips()[3].len(), 1, "clamped like `single`"); |
| 2168 | } |
| 2169 | |
| 2170 | #[test] |
| 2171 | fn edits_carry_the_source_along() { |
| 2172 | let mut p = two_sources(); |
| 2173 | |
| 2174 | assert!(p.split(11)); |
| 2175 | assert_eq!(shape(&p)[0][3], clip(9, 0, 2, 1)); |
| 2176 | assert_eq!(shape(&p)[0][4], clip(11, 2, 4, 1)); |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | let copied = p.clips()[4]; |
| 2181 | assert!(p.paste(1, copied)); |
| 2182 | assert_eq!(p.clips()[1].source, 1); |
| 2183 | assert_eq!(p.clips()[1].in_frame, 2); |
| 2184 | assert_eq!(p.clips()[2].source, 0); |
| 2185 | assert!(p.undo()); |
| 2186 | assert_eq!(p.clips()[1].source, 0, "undo restores sources too"); |
| 2187 | } |
| 2188 | |
| 2189 | #[test] |
| 2190 | fn segments_from_names_the_source() { |
| 2191 | let p = two_sources(); |
| 2192 | |
| 2193 | assert_eq!( |
| 2194 | p.segments_from(4, FPS), |
| 2195 | vec![ |
| 2196 | (Some(0), 4.0 / 30.0, 5.0 / 30.0), |
| 2197 | (Some(0), 5.0 / 30.0, 9.0 / 30.0), |
| 2198 | (Some(1), 0.0, 4.0 / 30.0), |
| 2199 | ] |
| 2200 | ); |
| 2201 | |
| 2202 | assert_eq!( |
| 2203 | p.segments_from(10, FPS), |
| 2204 | vec![(Some(1), 1.0 / 30.0, 4.0 / 30.0)] |
| 2205 | ); |
| 2206 | assert!(p.segments_from(13, FPS).is_empty()); |
| 2207 | |
| 2208 | let played: f64 = p.segments_from(0, FPS).iter().map(|(_, a, b)| b - a).sum(); |
| 2209 | assert!((played - f64::from(p.timeline_frames()) / FPS).abs() < 1e-9); |
| 2210 | } |
| 2211 | |
| 2212 | #[test] |
| 2213 | fn segments_follow_deletes_and_fps() { |
| 2214 | let mut p = three(); |
| 2215 | assert!(p.delete(0)); |
| 2216 | |
| 2217 | let segs = p.segments_from(0, 25.0); |
| 2218 | assert_eq!( |
| 2219 | segs, |
| 2220 | vec![(Some(0), 3.0 / 25.0, 0.2), (Some(0), 0.2, 9.0 / 25.0)] |
| 2221 | ); |
| 2222 | let played: f64 = segs.iter().map(|(_, a, b)| b - a).sum(); |
| 2223 | assert!( |
| 2224 | (played - p.timeline_frames() as f64 / 25.0).abs() < 1e-9, |
| 2225 | "segments must cover exactly the timeline: {played}" |
| 2226 | ); |
| 2227 | } |
| 2228 | |
| 2229 | #[test] |
| 2230 | fn orphan_sources_are_pruned_and_the_clips_reindexed() { |
| 2231 | |
| 2232 | let mut p = two_sources(); |
| 2233 | assert!(p.undo()); |
| 2234 | let (sources, lanes, eq, color) = p.without_orphan_sources(); |
| 2235 | assert_eq!(sources, vec![Source::new(FILE, 0)], "the orphan is gone"); |
| 2236 | assert!(eq.is_empty(), "and a project with no equalizer writes none"); |
| 2237 | assert!(color.is_empty(), "nor a colour it never graded with"); |
| 2238 | assert_eq!( |
| 2239 | lanes, |
| 2240 | two(three().clips().to_vec(), three().lane(Lane::A1).to_vec()), |
| 2241 | "the clips are untouched" |
| 2242 | ); |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | let mut p = Project::single(FILE, 9); |
| 2247 | assert_eq!(p.import(FILE2, 0), 1); |
| 2248 | assert_eq!(p.import("/nonexistent/c.mp4", 0), 2); |
| 2249 | assert!(p.append_clip(2, 4)); |
| 2250 | let (sources, lanes, eq, color) = p.without_orphan_sources(); |
| 2251 | assert_eq!( |
| 2252 | sources, |
| 2253 | vec![Source::new(FILE, 0), Source::new("/nonexistent/c.mp4", 0)] |
| 2254 | ); |
| 2255 | assert_eq!( |
| 2256 | lanes[0].1.iter().map(|c| c.source).collect::<Vec<_>>(), |
| 2257 | [0, 1] |
| 2258 | ); |
| 2259 | |
| 2260 | let reloaded = Project::from_parts(sources, lanes, eq, color).expect("from_parts"); |
| 2261 | assert_eq!(reloaded.timeline_frames(), p.timeline_frames()); |
| 2262 | assert_eq!(reloaded.sources().len(), 2); |
| 2263 | } |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | #[test] |
| 2268 | fn an_equalizer_follows_the_clip_through_split_copy_and_undo() { |
| 2269 | let mut p = three(); |
| 2270 | assert!(p.eq_of(Lane::V1, 0).is_none(), "a fresh clip plays flat"); |
| 2271 | assert!(p.set_eq(Lane::A1, 0, Some(band_at(2)))); |
| 2272 | assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); |
| 2273 | assert!( |
| 2274 | p.eq_of(Lane::V1, 0).is_none(), |
| 2275 | "one clip, not the lane above" |
| 2276 | ); |
| 2277 | |
| 2278 | |
| 2279 | assert!(p.split(1)); |
| 2280 | assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); |
| 2281 | assert_eq!(p.eq_of(Lane::A1, 1), Some(&band_at(2))); |
| 2282 | assert!(p.eq_of(Lane::A1, 2).is_none(), "the clip after it is flat"); |
| 2283 | |
| 2284 | |
| 2285 | let copied = p.lane(Lane::A1)[1]; |
| 2286 | assert!(p.place(Lane::V1, 20, copied)); |
| 2287 | assert_eq!(p.eq_of(Lane::V1, 4), Some(&band_at(2))); |
| 2288 | assert!(p.undo()); |
| 2289 | assert_eq!(p.lane(Lane::V1).len(), 4, "the placement came back off"); |
| 2290 | |
| 2291 | |
| 2292 | assert!(p.set_eq(Lane::A1, 0, Some(band_at(5)))); |
| 2293 | assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(5))); |
| 2294 | assert!(p.undo()); |
| 2295 | assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); |
| 2296 | assert!(p.set_eq(Lane::A1, 0, None), "and taking it off is an edit"); |
| 2297 | assert!(p.eq_of(Lane::A1, 0).is_none()); |
| 2298 | assert!(p.undo()); |
| 2299 | assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); |
| 2300 | |
| 2301 | |
| 2302 | let mut nan = band_at(1); |
| 2303 | nan.bands[0].q = f32::INFINITY; |
| 2304 | assert!(!p.set_eq(Lane::A1, 99, Some(band_at(1))), "no clip there"); |
| 2305 | assert!(!p.set_eq(Lane::A1, 0, Some(nan)), "not a finite band"); |
| 2306 | assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); |
| 2307 | assert!(p.undo()); |
| 2308 | assert_eq!( |
| 2309 | p.lane(Lane::A1).len(), |
| 2310 | 3, |
| 2311 | "one step back is before the split" |
| 2312 | ); |
| 2313 | assert!(p.undo()); |
| 2314 | assert!( |
| 2315 | p.eq_of(Lane::A1, 0).is_none(), |
| 2316 | "and the next is before the setting: the refusals pushed none" |
| 2317 | ); |
| 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | |
| 2322 | #[test] |
| 2323 | fn an_undone_equalizer_is_pruned_when_the_project_is_saved() { |
| 2324 | let mut p = three(); |
| 2325 | assert!(p.set_eq(Lane::V1, 0, Some(band_at(1)))); |
| 2326 | assert!(p.set_eq(Lane::V1, 0, Some(band_at(2)))); |
| 2327 | assert!(p.undo(), "back to the first setting"); |
| 2328 | assert_eq!(p.eq_of(Lane::V1, 0), Some(&band_at(1))); |
| 2329 | |
| 2330 | let (_, lanes, eq, _) = p.without_orphan_sources(); |
| 2331 | assert_eq!(eq, vec![band_at(1)], "what nothing plays is not written"); |
| 2332 | assert_eq!(lanes[0].1[0].eq, Some(0), "and the survivor renumbers"); |
| 2333 | assert_eq!(lanes[0].1[1].eq, None); |
| 2334 | |
| 2335 | |
| 2336 | assert!(p.set_eq(Lane::V1, 1, Some(band_at(1)))); |
| 2337 | assert!(p.set_eq(Lane::A1, 2, Some(band_at(1)))); |
| 2338 | let (_, lanes, eq, _) = p.without_orphan_sources(); |
| 2339 | assert_eq!(eq.len(), 1, "equal settings share their entry"); |
| 2340 | assert_eq!(lanes[1].1[2].eq, Some(0)); |
| 2341 | reloads(&p, "an equalizer that outlived an undo"); |
| 2342 | } |
| 2343 | |
| 2344 | |
| 2345 | |
| 2346 | #[test] |
| 2347 | fn a_colour_follows_the_clip_through_split_copy_and_undo() { |
| 2348 | let mut p = three(); |
| 2349 | assert!( |
| 2350 | p.color_of(Lane::V1, 0).is_none(), |
| 2351 | "a fresh clip is ungraded" |
| 2352 | ); |
| 2353 | assert!(p.set_color(Lane::V1, 0, Some(grade_at(2)))); |
| 2354 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); |
| 2355 | assert!( |
| 2356 | p.color_of(Lane::A1, 0).is_none(), |
| 2357 | "one clip, not the lane below" |
| 2358 | ); |
| 2359 | assert!( |
| 2360 | p.eq_of(Lane::V1, 0).is_none(), |
| 2361 | "and a grade is not an equalizer" |
| 2362 | ); |
| 2363 | |
| 2364 | |
| 2365 | assert!(p.split(1)); |
| 2366 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); |
| 2367 | assert_eq!(p.color_of(Lane::V1, 1), Some(&grade_at(2))); |
| 2368 | assert!( |
| 2369 | p.color_of(Lane::V1, 2).is_none(), |
| 2370 | "the clip after it is ungraded" |
| 2371 | ); |
| 2372 | |
| 2373 | |
| 2374 | let copied = p.lane(Lane::V1)[1]; |
| 2375 | assert!(p.place(Lane::A1, 20, copied)); |
| 2376 | assert_eq!(p.color_of(Lane::A1, 4), Some(&grade_at(2))); |
| 2377 | assert!(p.undo()); |
| 2378 | assert_eq!(p.lane(Lane::A1).len(), 4, "the placement came back off"); |
| 2379 | |
| 2380 | |
| 2381 | assert!(p.set_color(Lane::V1, 0, Some(grade_at(5)))); |
| 2382 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(5))); |
| 2383 | assert!(p.undo()); |
| 2384 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); |
| 2385 | assert!( |
| 2386 | p.set_color(Lane::V1, 0, None), |
| 2387 | "and taking it off is an edit" |
| 2388 | ); |
| 2389 | assert!(p.color_of(Lane::V1, 0).is_none()); |
| 2390 | assert!(p.undo()); |
| 2391 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); |
| 2392 | |
| 2393 | |
| 2394 | let nan = ColorParams { |
| 2395 | contrast: f32::NAN, |
| 2396 | ..grade_at(1) |
| 2397 | }; |
| 2398 | assert!( |
| 2399 | !p.set_color(Lane::V1, 99, Some(grade_at(1))), |
| 2400 | "no clip there" |
| 2401 | ); |
| 2402 | assert!(!p.set_color(Lane::V1, 0, Some(nan)), "not a finite value"); |
| 2403 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); |
| 2404 | assert!(p.undo()); |
| 2405 | assert_eq!( |
| 2406 | p.lane(Lane::V1).len(), |
| 2407 | 3, |
| 2408 | "one step back is before the split" |
| 2409 | ); |
| 2410 | assert!(p.undo()); |
| 2411 | assert!( |
| 2412 | p.color_of(Lane::V1, 0).is_none(), |
| 2413 | "and the next is before the grade: the refusals pushed none" |
| 2414 | ); |
| 2415 | } |
| 2416 | |
| 2417 | |
| 2418 | |
| 2419 | #[test] |
| 2420 | fn an_undone_colour_is_pruned_when_the_project_is_saved() { |
| 2421 | let mut p = three(); |
| 2422 | assert!(p.set_color(Lane::V1, 0, Some(grade_at(1)))); |
| 2423 | assert!(p.set_color(Lane::V1, 0, Some(grade_at(2)))); |
| 2424 | assert!(p.undo(), "back to the first grade"); |
| 2425 | assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(1))); |
| 2426 | |
| 2427 | let (_, lanes, _, color) = p.without_orphan_sources(); |
| 2428 | assert_eq!( |
| 2429 | color, |
| 2430 | vec![grade_at(1)], |
| 2431 | "what nothing plays is not written" |
| 2432 | ); |
| 2433 | assert_eq!(lanes[0].1[0].color, Some(0), "and the survivor renumbers"); |
| 2434 | assert_eq!(lanes[0].1[1].color, None); |
| 2435 | |
| 2436 | |
| 2437 | assert!(p.set_color(Lane::V1, 1, Some(grade_at(1)))); |
| 2438 | assert!(p.set_color(Lane::A1, 2, Some(grade_at(1)))); |
| 2439 | let (_, lanes, _, color) = p.without_orphan_sources(); |
| 2440 | assert_eq!(color.len(), 1, "equal grades share their entry"); |
| 2441 | assert_eq!(lanes[1].1[2].color, Some(0)); |
| 2442 | reloads(&p, "a colour that outlived an undo"); |
| 2443 | |
| 2444 | |
| 2445 | assert!(p.set_eq(Lane::V1, 0, Some(band_at(3)))); |
| 2446 | let (_, lanes, eq, color) = p.without_orphan_sources(); |
| 2447 | assert_eq!((eq.len(), color.len()), (1, 1)); |
| 2448 | assert_eq!((lanes[0].1[0].eq, lanes[0].1[0].color), (Some(0), Some(0))); |
| 2449 | } |
| 2450 | |
| 2451 | |
| 2452 | |
| 2453 | |
| 2454 | |
| 2455 | #[test] |
| 2456 | fn a_clip_is_still_a_small_copy() { |
| 2457 | assert_eq!( |
| 2458 | std::mem::size_of::<Clip>(), |
| 2459 | 40, |
| 2460 | "Clip changed size -- 32 before the colour index, 40 after" |
| 2461 | ); |
| 2462 | } |
| 2463 | |
| 2464 | #[test] |
| 2465 | fn from_parts_has_no_history_and_checks_the_invariants() { |
| 2466 | let (sources, lanes, eq, color) = three().without_orphan_sources(); |
| 2467 | let (video, audio) = (lanes[0].1.clone(), lanes[1].1.clone()); |
| 2468 | let mut p = Project::from_parts(sources.clone(), lanes.clone(), eq.clone(), color.clone()) |
| 2469 | .expect("valid parts"); |
| 2470 | assert_eq!(p.clips(), three().clips()); |
| 2471 | assert!(!p.undo(), "a loaded project has nothing to undo"); |
| 2472 | assert!(p.split(4), "...and is editable from there"); |
| 2473 | assert!(p.undo()); |
| 2474 | |
| 2475 | |
| 2476 | assert!( |
| 2477 | Project::from_parts( |
| 2478 | sources.clone(), |
| 2479 | two(video.clone(), Vec::new()), |
| 2480 | Vec::new(), |
| 2481 | Vec::new(), |
| 2482 | ) |
| 2483 | .is_ok() |
| 2484 | ); |
| 2485 | assert!( |
| 2486 | Project::from_parts( |
| 2487 | sources.clone(), |
| 2488 | two(Vec::new(), Vec::new()), |
| 2489 | Vec::new(), |
| 2490 | Vec::new(), |
| 2491 | ) |
| 2492 | .is_err() |
| 2493 | ); |
| 2494 | assert!(Project::from_parts(sources.clone(), Vec::new(), Vec::new(), Vec::new()).is_err()); |
| 2495 | let bad: [Vec<Clip>; 5] = [ |
| 2496 | vec![clip(0, 0, 3, 1)], |
| 2497 | vec![clip(0, 3, 3, 0)], |
| 2498 | vec![clip(3, 0, 3, 0), clip(0, 3, 5, 0)], |
| 2499 | vec![clip(0, 0, 5, 0), clip(3, 3, 5, 0)], |
| 2500 | vec![clip(u32::MAX - 1, 0, 3, 0)], |
| 2501 | ]; |
| 2502 | for clips in bad { |
| 2503 | assert!( |
| 2504 | Project::from_parts( |
| 2505 | sources.clone(), |
| 2506 | two(clips.clone(), Vec::new()), |
| 2507 | Vec::new(), |
| 2508 | Vec::new(), |
| 2509 | ) |
| 2510 | .is_err(), |
| 2511 | "{clips:?}" |
| 2512 | ); |
| 2513 | assert!( |
| 2514 | Project::from_parts( |
| 2515 | sources.clone(), |
| 2516 | two(video.clone(), clips.clone()), |
| 2517 | Vec::new(), |
| 2518 | Vec::new(), |
| 2519 | ) |
| 2520 | .is_err() |
| 2521 | ); |
| 2522 | |
| 2523 | assert!( |
| 2524 | Project::from_parts( |
| 2525 | sources.clone(), |
| 2526 | vec![ |
| 2527 | (LaneKind::Video, video.clone()), |
| 2528 | (LaneKind::Audio, audio.clone()), |
| 2529 | (LaneKind::Video, clips), |
| 2530 | ], |
| 2531 | Vec::new(), |
| 2532 | Vec::new(), |
| 2533 | ) |
| 2534 | .is_err() |
| 2535 | ); |
| 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | let eqd = |i: u16| { |
| 2540 | vec![Clip { |
| 2541 | eq: Some(i), |
| 2542 | ..video[0] |
| 2543 | }] |
| 2544 | }; |
| 2545 | assert!( |
| 2546 | Project::from_parts( |
| 2547 | sources.clone(), |
| 2548 | two(eqd(0), Vec::new()), |
| 2549 | Vec::new(), |
| 2550 | Vec::new(), |
| 2551 | ) |
| 2552 | .is_err() |
| 2553 | ); |
| 2554 | let mut nan = band_at(1); |
| 2555 | nan.bands[0].freq_hz = f32::NAN; |
| 2556 | assert!( |
| 2557 | Project::from_parts( |
| 2558 | sources.clone(), |
| 2559 | two(eqd(0), Vec::new()), |
| 2560 | vec![nan], |
| 2561 | Vec::new(), |
| 2562 | ) |
| 2563 | .is_err() |
| 2564 | ); |
| 2565 | let loaded = Project::from_parts( |
| 2566 | sources.clone(), |
| 2567 | two(eqd(0), Vec::new()), |
| 2568 | vec![band_at(1)], |
| 2569 | Vec::new(), |
| 2570 | ) |
| 2571 | .expect("an eq the table holds"); |
| 2572 | assert_eq!(loaded.eq_of(Lane::V1, 0), Some(&band_at(1))); |
| 2573 | |
| 2574 | |
| 2575 | let graded = |i: u16| { |
| 2576 | vec![Clip { |
| 2577 | color: Some(i), |
| 2578 | fit: FitPolicy::default(), |
| 2579 | ..video[0] |
| 2580 | }] |
| 2581 | }; |
| 2582 | assert!( |
| 2583 | Project::from_parts( |
| 2584 | sources.clone(), |
| 2585 | two(graded(0), Vec::new()), |
| 2586 | Vec::new(), |
| 2587 | Vec::new(), |
| 2588 | ) |
| 2589 | .is_err(), |
| 2590 | "a colour index no entry answers" |
| 2591 | ); |
| 2592 | assert!( |
| 2593 | Project::from_parts( |
| 2594 | sources.clone(), |
| 2595 | two(graded(0), Vec::new()), |
| 2596 | Vec::new(), |
| 2597 | vec![ColorParams { |
| 2598 | tint: f32::NAN, |
| 2599 | ..grade_at(1) |
| 2600 | }], |
| 2601 | ) |
| 2602 | .is_err(), |
| 2603 | "a value the file format could not have written" |
| 2604 | ); |
| 2605 | let loaded = Project::from_parts( |
| 2606 | sources.clone(), |
| 2607 | two(graded(0), Vec::new()), |
| 2608 | Vec::new(), |
| 2609 | vec![grade_at(1)], |
| 2610 | ) |
| 2611 | .expect("a colour the table holds"); |
| 2612 | assert_eq!(loaded.color_of(Lane::V1, 0), Some(&grade_at(1))); |
| 2613 | |
| 2614 | |
| 2615 | let mut p = Project::from_parts(sources, lanes, eq, color).expect("valid parts"); |
| 2616 | assert!(p.split(4)); |
| 2617 | assert!(p.clips().iter().all(|c| c.link.is_some())); |
| 2618 | } |
| 2619 | |
| 2620 | |
| 2621 | |
| 2622 | |
| 2623 | |
| 2624 | |
| 2625 | |
| 2626 | |
| 2627 | |
| 2628 | #[test] |
| 2629 | fn a_link_id_is_never_two_clips_of_one_lane() { |
| 2630 | let sources = vec![Source::new(FILE, 0)]; |
| 2631 | let linked = |start, in_frame, out_frame, link| Clip { |
| 2632 | start, |
| 2633 | in_frame, |
| 2634 | out_frame, |
| 2635 | source: 0, |
| 2636 | link: Some(link), |
| 2637 | eq: None, |
| 2638 | color: None, |
| 2639 | fit: FitPolicy::default(), |
| 2640 | }; |
| 2641 | |
| 2642 | |
| 2643 | let err = |video: Vec<Clip>, audio: Vec<Clip>| { |
| 2644 | Project::from_parts(sources.clone(), two(video, audio), Vec::new(), Vec::new()) |
| 2645 | .expect_err("refused") |
| 2646 | .to_string() |
| 2647 | }; |
| 2648 | assert!( |
| 2649 | err(vec![linked(0, 0, 3, 7), linked(3, 3, 5, 7)], Vec::new()) |
| 2650 | .contains("link 7 names two clips in the V1 lane"), |
| 2651 | "a duplicate id inside one lane is refused by name" |
| 2652 | ); |
| 2653 | assert!( |
| 2654 | err(vec![linked(0, 0, 5, 2)], vec![linked(0, 0, 3, 2)]) |
| 2655 | .contains("link 2 covers [0, 5) in V1 and [0, 3) in A1"), |
| 2656 | "a pair that does not cover one span is refused by name" |
| 2657 | ); |
| 2658 | |
| 2659 | let mut p = Project::single(FILE, 9); |
| 2660 | assert!(p.lift(Lane::A1, 0)); |
| 2661 | let (sources, lanes, eq, color) = p.clone().without_orphan_sources(); |
| 2662 | assert!( |
| 2663 | Project::from_parts(sources, lanes, eq, color).is_ok(), |
| 2664 | "a lifted lane's project has to load again" |
| 2665 | ); |
| 2666 | |
| 2667 | |
| 2668 | let consistent = |p: &Project, what: &str| { |
| 2669 | assert!( |
| 2670 | links_are_consistent(&p.lanes).is_ok(), |
| 2671 | "{what}: {:?}", |
| 2672 | p.lanes |
| 2673 | ); |
| 2674 | }; |
| 2675 | let mut p = three(); |
| 2676 | let copied = p.clips()[0]; |
| 2677 | assert!( |
| 2678 | copied.link.is_some(), |
| 2679 | "the clipboard clip carries its group" |
| 2680 | ); |
| 2681 | assert!(p.lift(Lane::V1, 0)); |
| 2682 | assert!(p.place(Lane::V1, 0, copied)); |
| 2683 | assert!(p.place(Lane::V1, 20, copied), "and again, further out"); |
| 2684 | assert_eq!( |
| 2685 | p.clips().iter().filter(|c| c.link == copied.link).count(), |
| 2686 | 0, |
| 2687 | "a placement belongs to no group, so it cannot duplicate one" |
| 2688 | ); |
| 2689 | consistent(&p, "lift + place twice"); |
| 2690 | assert!(p.place(Lane::A1, 4, copied), "over a linked clip's middle"); |
| 2691 | consistent(&p, "place through a grouped clip"); |
| 2692 | assert!(p.paste(6, copied), "a rippling paste through another"); |
| 2693 | consistent(&p, "paste"); |
| 2694 | assert!(p.split(2)); |
| 2695 | consistent(&p, "split"); |
| 2696 | assert!(p.ripple_delete(1, 3)); |
| 2697 | consistent(&p, "ripple_delete"); |
| 2698 | assert!(p.undo()); |
| 2699 | consistent(&p, "undo"); |
| 2700 | } |
| 2701 | |
| 2702 | |
| 2703 | |
| 2704 | |
| 2705 | |
| 2706 | #[test] |
| 2707 | fn every_reachable_state_reloads() { |
| 2708 | let mut p = Project::single(FILE, 150); |
| 2709 | assert!(p.place(Lane::A1, 3, clip(0, 0, 50, 0))); |
| 2710 | assert!(p.split(10), "the lanes are cut apart at 10"); |
| 2711 | reloads(&p, "place on one lane, then split"); |
| 2712 | |
| 2713 | let mut p = Project::single(FILE, 150); |
| 2714 | assert!(p.split(30)); |
| 2715 | assert!(p.lift(Lane::V1, 0), "the picture goes, the sound stays"); |
| 2716 | assert!(p.regroup(30), "only the audio lane can rejoin"); |
| 2717 | assert!(p.split(60)); |
| 2718 | reloads(&p, "lift, regroup one lane, split"); |
| 2719 | |
| 2720 | |
| 2721 | |
| 2722 | |
| 2723 | let mut p = Project::single(FILE, 150); |
| 2724 | let second = p.import(FILE, 1); |
| 2725 | assert_eq!(second, 1, "the same file on another stream is a new source"); |
| 2726 | assert!(p.append_clip(second, 150)); |
| 2727 | assert!(p.split(200)); |
| 2728 | reloads( |
| 2729 | &p, |
| 2730 | "a second audio stream of the same file, placed and split", |
| 2731 | ); |
| 2732 | let (sources, ..) = p.without_orphan_sources(); |
| 2733 | assert_eq!( |
| 2734 | sources.iter().map(|s| s.audio_stream).collect::<Vec<_>>(), |
| 2735 | [0, 1], |
| 2736 | "both entries survive the orphan prune: both are played" |
| 2737 | ); |
| 2738 | } |
| 2739 | |
| 2740 | |
| 2741 | |
| 2742 | |
| 2743 | #[test] |
| 2744 | fn random_edit_sequences_reload() { |
| 2745 | for seed in 0..200u64 { |
| 2746 | let mut rng = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); |
| 2747 | let mut next = move |n: u32| { |
| 2748 | |
| 2749 | rng ^= rng << 13; |
| 2750 | rng ^= rng >> 7; |
| 2751 | rng ^= rng << 17; |
| 2752 | (rng >> 33) as u32 % n |
| 2753 | }; |
| 2754 | let mut p = Project::single(FILE, 40); |
| 2755 | for step in 0..24 { |
| 2756 | let frame = next(44); |
| 2757 | |
| 2758 | |
| 2759 | let copied = *p |
| 2760 | .lane(if next(2) == 0 { Lane::V1 } else { Lane::A1 }) |
| 2761 | .get(next(4) as usize) |
| 2762 | .unwrap_or(&clip(0, 0, 5, 0)); |
| 2763 | let lane = if next(2) == 0 { Lane::V1 } else { Lane::A1 }; |
| 2764 | let idx = next(4) as usize; |
| 2765 | let _ = match next(13) { |
| 2766 | 0 => p.split(frame), |
| 2767 | 1 => p.regroup(frame), |
| 2768 | 2 => p.lift(lane, idx), |
| 2769 | 3 => p.place(lane, frame, copied), |
| 2770 | 4 => p.paste(frame, copied), |
| 2771 | 5 => p.ripple_delete(frame, next(9)), |
| 2772 | 6 => p.delete(idx), |
| 2773 | 7 => p.undo(), |
| 2774 | |
| 2775 | |
| 2776 | |
| 2777 | 8 => p.set_eq(lane, idx, Some(band_at(next(4)))), |
| 2778 | 9 => p.set_eq(lane, idx, None), |
| 2779 | |
| 2780 | |
| 2781 | 10 => p.set_color(lane, idx, Some(grade_at(next(4)))), |
| 2782 | 11 => p.set_color(lane, idx, None), |
| 2783 | _ => p.append_clip(0, 1 + next(9)), |
| 2784 | }; |
| 2785 | reloads(&p, &format!("seed {seed}, step {step}")); |
| 2786 | } |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | |
| 2791 | |
| 2792 | |
| 2793 | fn reloads(p: &Project, what: &str) { |
| 2794 | let (sources, lanes, eq, color) = p.clone().without_orphan_sources(); |
| 2795 | match Project::from_parts(sources, lanes, eq, color) { |
| 2796 | Err(e) => panic!("{what}: saved but would not load: {e}\n{:?}", p.lanes), |
| 2797 | Ok(back) => { |
| 2798 | assert_eq!(back.lanes(), p.lanes(), "{what}: the lane list changed"); |
| 2799 | |
| 2800 | |
| 2801 | let spans = |p: &Project| -> Vec<Vec<(u32, u32)>> { |
| 2802 | p.lanes().into_iter().map(|l| p.lane_spans(l)).collect() |
| 2803 | }; |
| 2804 | assert_eq!(spans(&back), spans(p), "{what}: the placements changed"); |
| 2805 | |
| 2806 | |
| 2807 | let eqs = |p: &Project| -> Vec<Vec<Option<EqParams>>> { |
| 2808 | p.lanes() |
| 2809 | .into_iter() |
| 2810 | .map(|l| { |
| 2811 | (0..p.lane(l).len()) |
| 2812 | .map(|i| p.eq_of(l, i).cloned()) |
| 2813 | .collect() |
| 2814 | }) |
| 2815 | .collect() |
| 2816 | }; |
| 2817 | assert_eq!(eqs(&back), eqs(p), "{what}: the equalizers changed"); |
| 2818 | |
| 2819 | |
| 2820 | let colors = |p: &Project| -> Vec<Vec<Option<ColorParams>>> { |
| 2821 | p.lanes() |
| 2822 | .into_iter() |
| 2823 | .map(|l| { |
| 2824 | (0..p.lane(l).len()) |
| 2825 | .map(|i| p.color_of(l, i).copied()) |
| 2826 | .collect() |
| 2827 | }) |
| 2828 | .collect() |
| 2829 | }; |
| 2830 | assert_eq!(colors(&back), colors(p), "{what}: the colours changed"); |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | |
| 2837 | |
| 2838 | fn invariants_hold(p: &Project, what: &str) { |
| 2839 | for lane in p.lanes() { |
| 2840 | assert!( |
| 2841 | sorted_disjoint(p.lane(lane)), |
| 2842 | "{what}: {} is out of order or overlaps itself: {:?}", |
| 2843 | lane.label(), |
| 2844 | p.lane(lane) |
| 2845 | ); |
| 2846 | } |
| 2847 | if let Err(e) = links_are_consistent(&p.lanes) { |
| 2848 | panic!("{what}: {e}\n{:?}", p.lanes); |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | |
| 2853 | |
| 2854 | |
| 2855 | #[test] |
| 2856 | fn a_third_lane_is_a_peer() { |
| 2857 | let mut p = three(); |
| 2858 | assert_eq!(p.lane_count(LaneKind::Video), 1); |
| 2859 | assert_eq!(p.lane_count(LaneKind::Audio), 1); |
| 2860 | let v2 = p.add_lane(LaneKind::Video); |
| 2861 | assert_eq!(v2, Lane::new(LaneKind::Video, 1)); |
| 2862 | assert_eq!(v2.label(), "V2"); |
| 2863 | assert_eq!(p.lane_count(LaneKind::Video), 2); |
| 2864 | assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); |
| 2865 | assert!(p.lane(v2).is_empty(), "a new lane holds nothing"); |
| 2866 | |
| 2867 | |
| 2868 | let v3 = Lane::new(LaneKind::Video, 2); |
| 2869 | assert!(p.lane(v3).is_empty()); |
| 2870 | assert!(!p.place(v3, 0, clip(0, 0, 3, 0)), "no lane, no placement"); |
| 2871 | assert!(!p.lift(v3, 0)); |
| 2872 | assert_eq!(p.lane_count(LaneKind::Video), 2, "and none was made"); |
| 2873 | |
| 2874 | |
| 2875 | let before = shape(&p); |
| 2876 | assert!(p.place(v2, 4, clip(0, 20, 32, 0))); |
| 2877 | assert_eq!(shape(&p)[0], before[0], "V1 never moved"); |
| 2878 | assert_eq!(shape(&p)[1], before[1], "A1 never moved"); |
| 2879 | assert_eq!(p.lane_spans(v2), vec![(4, 12)]); |
| 2880 | assert_eq!( |
| 2881 | p.timeline_frames(), |
| 2882 | 16, |
| 2883 | "the last lane to end sets the length" |
| 2884 | ); |
| 2885 | assert_eq!(p.map(v2, 5), Some((0, 21))); |
| 2886 | assert_eq!(p.map(v2, 3), None, "V2's own leading gap"); |
| 2887 | assert_eq!( |
| 2888 | p.span_at(v2, 0), |
| 2889 | Some(Span { |
| 2890 | start: 0, |
| 2891 | len: 4, |
| 2892 | from: None |
| 2893 | }) |
| 2894 | ); |
| 2895 | |
| 2896 | for l in p.lanes() { |
| 2897 | let spans = p.spans_from(l, 0); |
| 2898 | assert_eq!( |
| 2899 | spans.iter().map(|s| s.len).sum::<u32>(), |
| 2900 | 16, |
| 2901 | "{}", |
| 2902 | l.label() |
| 2903 | ); |
| 2904 | } |
| 2905 | |
| 2906 | assert_eq!(p.clips(), p.lane(Lane::V1)); |
| 2907 | assert_eq!(p.map_timeline(4), p.map(Lane::V1, 4)); |
| 2908 | let segments = p.segments_from(0, FPS); |
| 2909 | assert_eq!(segments[..3], three().segments_from(0, FPS)[..]); |
| 2910 | assert_eq!( |
| 2911 | segments[3], |
| 2912 | (None, 0.0, 7.0 / FPS), |
| 2913 | "A1 keeps the clock counting over the picture V2 added" |
| 2914 | ); |
| 2915 | invariants_hold(&p, "place on V2"); |
| 2916 | |
| 2917 | |
| 2918 | assert!(p.lift(v2, 0)); |
| 2919 | assert!(p.lane(v2).is_empty()); |
| 2920 | assert_eq!(p.timeline_frames(), 9, "V2 no longer runs the longest"); |
| 2921 | assert!(p.undo()); |
| 2922 | assert_eq!(p.lane_spans(v2), vec![(4, 12)]); |
| 2923 | assert!(p.undo()); |
| 2924 | assert!(p.lane(v2).is_empty(), "the placement came off V2"); |
| 2925 | assert!(p.undo(), "and one more step takes the lane itself back"); |
| 2926 | assert_eq!(p.lane_count(LaneKind::Video), 1); |
| 2927 | assert_eq!(shape(&p), shape(&three())); |
| 2928 | } |
| 2929 | |
| 2930 | |
| 2931 | |
| 2932 | |
| 2933 | |
| 2934 | #[test] |
| 2935 | fn a_group_id_is_one_span_across_every_lane() { |
| 2936 | let mut p = Project::single(FILE, 9); |
| 2937 | let v2 = p.add_lane(LaneKind::Video); |
| 2938 | assert!(p.place(v2, 0, clip(0, 0, 9, 0)), "three lanes, one span"); |
| 2939 | assert!(p.split(4)); |
| 2940 | let side = |p: &Project, i: usize| -> Vec<Option<u32>> { |
| 2941 | p.lanes().into_iter().map(|l| p.lane(l)[i].link).collect() |
| 2942 | }; |
| 2943 | let (left, right) = (side(&p, 0), side(&p, 1)); |
| 2944 | assert!( |
| 2945 | left[0].is_some() && left.iter().all(|id| *id == left[0]), |
| 2946 | "one id for three left halves: {left:?}" |
| 2947 | ); |
| 2948 | assert!(right.iter().all(|id| *id == right[0]), "{right:?}"); |
| 2949 | assert_ne!(left[0], right[0], "the halves are no longer one take"); |
| 2950 | invariants_hold(&p, "split across three lanes"); |
| 2951 | |
| 2952 | |
| 2953 | |
| 2954 | let mut p = Project::single(FILE, 9); |
| 2955 | let v2 = p.add_lane(LaneKind::Video); |
| 2956 | assert!(p.place(v2, 0, clip(0, 0, 6, 0))); |
| 2957 | assert!(p.split(3)); |
| 2958 | assert_eq!(p.lane(Lane::V1)[0].link, p.lane(v2)[0].link, "same [0, 3)"); |
| 2959 | assert_ne!( |
| 2960 | p.lane(Lane::V1)[1].link, |
| 2961 | p.lane(v2)[1].link, |
| 2962 | "[3,9) vs [3,6)" |
| 2963 | ); |
| 2964 | assert_eq!(p.lane(Lane::V1)[1].link, p.lane(Lane::A1)[1].link); |
| 2965 | invariants_hold(&p, "split where one lane ends early"); |
| 2966 | |
| 2967 | |
| 2968 | assert!(p.regroup(3)); |
| 2969 | assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link); |
| 2970 | assert_ne!( |
| 2971 | p.lane(Lane::V1)[0].link, |
| 2972 | p.lane(v2)[0].link, |
| 2973 | "V2 rejoins into a span of its own" |
| 2974 | ); |
| 2975 | invariants_hold(&p, "regroup across three lanes"); |
| 2976 | |
| 2977 | |
| 2978 | |
| 2979 | let linked = |out_frame, link| Clip { |
| 2980 | start: 0, |
| 2981 | in_frame: 0, |
| 2982 | out_frame, |
| 2983 | source: 0, |
| 2984 | link: Some(link), |
| 2985 | eq: None, |
| 2986 | color: None, |
| 2987 | fit: FitPolicy::default(), |
| 2988 | }; |
| 2989 | let lanes = vec![ |
| 2990 | LaneData { |
| 2991 | kind: LaneKind::Video, |
| 2992 | clips: vec![linked(3, 4)], |
| 2993 | }, |
| 2994 | LaneData { |
| 2995 | kind: LaneKind::Video, |
| 2996 | clips: vec![linked(5, 4)], |
| 2997 | }, |
| 2998 | ]; |
| 2999 | assert_eq!( |
| 3000 | links_are_consistent(&lanes).unwrap_err().to_string(), |
| 3001 | "link 4 covers [0, 3) in V1 and [0, 5) in V2" |
| 3002 | ); |
| 3003 | } |
| 3004 | |
| 3005 | |
| 3006 | |
| 3007 | |
| 3008 | |
| 3009 | |
| 3010 | #[test] |
| 3011 | fn a_group_may_span_any_lane() { |
| 3012 | let lane = |kind, clips: Vec<Clip>| LaneData { kind, clips }; |
| 3013 | let one = |start: u32, end: u32, link| Clip { |
| 3014 | start, |
| 3015 | in_frame: start, |
| 3016 | out_frame: end, |
| 3017 | source: 0, |
| 3018 | link: Some(link), |
| 3019 | eq: None, |
| 3020 | color: None, |
| 3021 | fit: FitPolicy::default(), |
| 3022 | }; |
| 3023 | |
| 3024 | let kinds = [ |
| 3025 | LaneKind::Video, |
| 3026 | LaneKind::Audio, |
| 3027 | LaneKind::Video, |
| 3028 | LaneKind::Audio, |
| 3029 | ]; |
| 3030 | let build = |clips: [Vec<Clip>; 4]| -> Vec<LaneData> { |
| 3031 | kinds.iter().zip(clips).map(|(&k, c)| lane(k, c)).collect() |
| 3032 | }; |
| 3033 | let take = || vec![one(0, 4, 7)]; |
| 3034 | assert!(links_are_consistent(&build([take(), take(), take(), take()])).is_ok()); |
| 3035 | |
| 3036 | |
| 3037 | let across = build([take(), Vec::new(), Vec::new(), take()]); |
| 3038 | assert!(links_are_consistent(&across).is_ok(), "V1 grouped with A2"); |
| 3039 | |
| 3040 | let apart = build([take(), Vec::new(), Vec::new(), vec![one(0, 6, 7)]]); |
| 3041 | assert_eq!( |
| 3042 | links_are_consistent(&apart).unwrap_err().to_string(), |
| 3043 | "link 7 covers [0, 4) in V1 and [0, 6) in A2" |
| 3044 | ); |
| 3045 | |
| 3046 | let twice = build([ |
| 3047 | Vec::new(), |
| 3048 | Vec::new(), |
| 3049 | vec![one(0, 4, 7), one(4, 8, 7)], |
| 3050 | Vec::new(), |
| 3051 | ]); |
| 3052 | assert_eq!( |
| 3053 | links_are_consistent(&twice).unwrap_err().to_string(), |
| 3054 | "link 7 names two clips in the V2 lane" |
| 3055 | ); |
| 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | |
| 3060 | |
| 3061 | #[test] |
| 3062 | fn a_save_writes_every_lane_it_has() { |
| 3063 | let mut p = three(); |
| 3064 | let before = p.without_orphan_sources(); |
| 3065 | assert_eq!(before.1.len(), 2, "V1 and A1"); |
| 3066 | let v2 = p.add_lane(LaneKind::Video); |
| 3067 | assert!(p.place(v2, 4, clip(0, 20, 32, 0))); |
| 3068 | let a2 = p.add_lane(LaneKind::Audio); |
| 3069 | let (sources, lanes, eq, color) = p.without_orphan_sources(); |
| 3070 | assert_eq!( |
| 3071 | lanes.iter().map(|(k, _)| *k).collect::<Vec<_>>(), |
| 3072 | [ |
| 3073 | LaneKind::Video, |
| 3074 | LaneKind::Audio, |
| 3075 | LaneKind::Video, |
| 3076 | LaneKind::Audio |
| 3077 | ], |
| 3078 | "display order, not video-then-audio" |
| 3079 | ); |
| 3080 | assert_eq!(lanes[2].1, p.lane(v2), "V2's clips are written"); |
| 3081 | assert!(lanes[3].1.is_empty(), "and the empty A2 is still a lane"); |
| 3082 | |
| 3083 | let back = Project::from_parts(sources, lanes, eq, color).expect("four lanes load"); |
| 3084 | assert_eq!(back.lanes(), p.lanes()); |
| 3085 | assert_eq!(back.lane(a2), p.lane(a2)); |
| 3086 | |
| 3087 | |
| 3088 | for _ in 0..3 { |
| 3089 | assert!(p.undo()); |
| 3090 | } |
| 3091 | assert_eq!( |
| 3092 | p.without_orphan_sources(), |
| 3093 | before, |
| 3094 | "byte for byte the same parts a two-lane save always wrote" |
| 3095 | ); |
| 3096 | } |
| 3097 | |
| 3098 | |
| 3099 | |
| 3100 | |
| 3101 | |
| 3102 | |
| 3103 | #[test] |
| 3104 | fn random_edit_sequences_keep_many_lanes_consistent() { |
| 3105 | for seed in 0..200u64 { |
| 3106 | let mut rng = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); |
| 3107 | let mut next = move |n: u32| { |
| 3108 | rng ^= rng << 13; |
| 3109 | rng ^= rng >> 7; |
| 3110 | rng ^= rng << 17; |
| 3111 | (rng >> 33) as u32 % n |
| 3112 | }; |
| 3113 | let mut p = Project::single(FILE, 40); |
| 3114 | let v2 = p.add_lane(LaneKind::Video); |
| 3115 | assert!(p.place(v2, 5, clip(0, 0, 20, 0))); |
| 3116 | let lanes = [Lane::V1, Lane::A1, v2]; |
| 3117 | for step in 0..24 { |
| 3118 | let frame = next(44); |
| 3119 | let copied = *p |
| 3120 | .lane(lanes[next(3) as usize]) |
| 3121 | .get(next(4) as usize) |
| 3122 | .unwrap_or(&clip(0, 0, 5, 0)); |
| 3123 | let lane = lanes[next(3) as usize]; |
| 3124 | let idx = next(4) as usize; |
| 3125 | let _ = match next(12) { |
| 3126 | 10 => p.set_eq(lane, idx, Some(band_at(next(4)))), |
| 3127 | 11 => p.set_eq(lane, idx, None), |
| 3128 | 0 => p.split(frame), |
| 3129 | 1 => p.regroup(frame), |
| 3130 | 2 => p.lift(lane, idx), |
| 3131 | 3 => p.place(lane, frame, copied), |
| 3132 | 4 => p.paste(frame, copied), |
| 3133 | 5 => p.ripple_delete(frame, next(9)), |
| 3134 | 6 => p.delete(idx), |
| 3135 | 7 => p.undo(), |
| 3136 | 8 => { |
| 3137 | p.add_lane(if next(2) == 0 { |
| 3138 | LaneKind::Video |
| 3139 | } else { |
| 3140 | LaneKind::Audio |
| 3141 | }); |
| 3142 | true |
| 3143 | } |
| 3144 | _ => p.append_clip(0, 1 + next(9)), |
| 3145 | }; |
| 3146 | invariants_hold(&p, &format!("seed {seed}, step {step}")); |
| 3147 | reloads(&p, &format!("seed {seed}, step {step}")); |
| 3148 | } |
| 3149 | } |
| 3150 | } |
| 3151 | |
| 3152 | |
| 3153 | |
| 3154 | |
| 3155 | #[test] |
| 3156 | fn the_composite_is_the_topmost_lane_with_a_clip() { |
| 3157 | let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; |
| 3158 | let lanes = vec![ |
| 3159 | |
| 3160 | (LaneKind::Video, vec![clip(0, 0, 30, 0)]), |
| 3161 | |
| 3162 | (LaneKind::Video, vec![clip(10, 5, 15, 1)]), |
| 3163 | (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), |
| 3164 | ]; |
| 3165 | let p = Project::from_parts(sources, lanes, vec![], vec![]).expect("valid parts"); |
| 3166 | assert_eq!( |
| 3167 | p.composite_spans_from(0), |
| 3168 | vec![ |
| 3169 | Span { |
| 3170 | start: 0, |
| 3171 | len: 10, |
| 3172 | from: Some((0, 0)) |
| 3173 | }, |
| 3174 | Span { |
| 3175 | start: 10, |
| 3176 | len: 10, |
| 3177 | from: Some((1, 5)) |
| 3178 | }, |
| 3179 | Span { |
| 3180 | start: 20, |
| 3181 | len: 10, |
| 3182 | from: Some((0, 20)) |
| 3183 | }, |
| 3184 | ], |
| 3185 | "V2 takes over for its own span and hands V1 back" |
| 3186 | ); |
| 3187 | |
| 3188 | assert_eq!( |
| 3189 | p.composite_span_at(15), |
| 3190 | Some(Span { |
| 3191 | start: 15, |
| 3192 | len: 5, |
| 3193 | from: Some((1, 10)) |
| 3194 | }) |
| 3195 | ); |
| 3196 | assert_eq!(p.composite_span_at(30), None, "past the end"); |
| 3197 | |
| 3198 | |
| 3199 | |
| 3200 | let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; |
| 3201 | let holed = vec![ |
| 3202 | (LaneKind::Video, vec![clip(0, 0, 5, 0)]), |
| 3203 | (LaneKind::Video, vec![clip(10, 0, 10, 1)]), |
| 3204 | (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), |
| 3205 | ]; |
| 3206 | let p = Project::from_parts(sources, holed, vec![], vec![]).expect("valid parts"); |
| 3207 | assert_eq!( |
| 3208 | p.composite_spans_from(0), |
| 3209 | vec![ |
| 3210 | Span { |
| 3211 | start: 0, |
| 3212 | len: 5, |
| 3213 | from: Some((0, 0)) |
| 3214 | }, |
| 3215 | Span { |
| 3216 | start: 5, |
| 3217 | len: 5, |
| 3218 | from: None |
| 3219 | }, |
| 3220 | Span { |
| 3221 | start: 10, |
| 3222 | len: 10, |
| 3223 | from: Some((1, 0)) |
| 3224 | }, |
| 3225 | Span { |
| 3226 | start: 20, |
| 3227 | len: 10, |
| 3228 | from: None |
| 3229 | }, |
| 3230 | ] |
| 3231 | ); |
| 3232 | |
| 3233 | |
| 3234 | |
| 3235 | let p = Project::single(FILE, 30); |
| 3236 | assert_eq!(p.composite_spans_from(0), p.spans_from(Lane::V1, 0)); |
| 3237 | assert_eq!(p.composite_span_at(7), p.span_at(Lane::V1, 7)); |
| 3238 | } |
| 3239 | |
| 3240 | |
| 3241 | #[test] |
| 3242 | fn audio_segments_cover_every_lane_that_holds_something() { |
| 3243 | let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; |
| 3244 | let lanes = vec![ |
| 3245 | (LaneKind::Video, vec![clip(0, 0, 30, 0)]), |
| 3246 | (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), |
| 3247 | (LaneKind::Audio, vec![clip(15, 0, 15, 1)]), |
| 3248 | (LaneKind::Audio, Vec::new()), |
| 3249 | ]; |
| 3250 | let p = Project::from_parts(sources, lanes, vec![], vec![]).expect("valid parts"); |
| 3251 | let lists = p.audio_segments_from(0, FPS); |
| 3252 | assert_eq!(lists.len(), 2, "the empty lane is not mixed in"); |
| 3253 | assert_eq!(lists[0], p.segments_from(0, FPS), "A1 is unchanged"); |
| 3254 | |
| 3255 | assert_eq!(lists[1], vec![(None, 0.0, 0.5), (Some(1), 0.0, 0.5)]); |
| 3256 | |
| 3257 | |
| 3258 | |
| 3259 | let p = Project::from_parts( |
| 3260 | vec![Source::new(FILE, 0)], |
| 3261 | vec![ |
| 3262 | (LaneKind::Video, vec![clip(0, 0, 30, 0)]), |
| 3263 | (LaneKind::Audio, Vec::new()), |
| 3264 | ], |
| 3265 | vec![], |
| 3266 | vec![], |
| 3267 | ) |
| 3268 | .expect("valid parts"); |
| 3269 | assert_eq!(p.audio_segments_from(0, FPS), vec![vec![(None, 0.0, 1.0)]]); |
| 3270 | } |
| 3271 | } |