| 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 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | use std::ffi::OsString; |
| 206 | use std::io::Write; |
| 207 | use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 208 | use std::path::{Path, PathBuf}; |
| 209 | |
| 210 | use crate::color::ColorParams; |
| 211 | use crate::eq::{Band, BandKind, EqParams}; |
| 212 | use crate::limiter::Limiter; |
| 213 | use crate::project::{Clip, Lane, LaneKind, Source, Speed, SubClip}; |
| 214 | use crate::scale::FitPolicy; |
| 215 | use crate::subtitle::SubtitleTrack; |
| 216 | |
| 217 | |
| 218 | |
| 219 | const MAGIC: &[u8] = b"edith 16"; |
| 220 | const MAGIC_V15: &[u8] = b"edith 15"; |
| 221 | const MAGIC_V14: &[u8] = b"edith 14"; |
| 222 | const MAGIC_V13: &[u8] = b"edith 13"; |
| 223 | const MAGIC_V12: &[u8] = b"edith 12"; |
| 224 | const MAGIC_V11: &[u8] = b"edith 11"; |
| 225 | const MAGIC_V10: &[u8] = b"edith 10"; |
| 226 | const MAGIC_V9: &[u8] = b"edith 9"; |
| 227 | const MAGIC_V8: &[u8] = b"edith 8"; |
| 228 | const MAGIC_V7: &[u8] = b"edith 7"; |
| 229 | const MAGIC_V6: &[u8] = b"edith 6"; |
| 230 | const MAGIC_V5: &[u8] = b"edith 5"; |
| 231 | const MAGIC_V4: &[u8] = b"edith 4"; |
| 232 | const MAGIC_V3: &[u8] = b"edith 3"; |
| 233 | const MAGIC_V2: &[u8] = b"edith 2"; |
| 234 | const MAGIC_V1: &[u8] = b"edith 1"; |
| 235 | |
| 236 | |
| 237 | |
| 238 | #[derive(Debug)] |
| 239 | pub struct Document { |
| 240 | |
| 241 | |
| 242 | pub sources: Vec<Source>, |
| 243 | |
| 244 | |
| 245 | |
| 246 | pub lanes: Vec<(LaneKind, Vec<Clip>)>, |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | pub subs: Vec<Vec<SubClip>>, |
| 252 | |
| 253 | |
| 254 | pub eq: Vec<EqParams>, |
| 255 | |
| 256 | |
| 257 | pub color: Vec<ColorParams>, |
| 258 | |
| 259 | |
| 260 | pub resolution: Option<(u32, u32)>, |
| 261 | |
| 262 | |
| 263 | |
| 264 | pub gains: Vec<f32>, |
| 265 | |
| 266 | |
| 267 | pub limiter: Limiter, |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | pub subtitles: Vec<(PathBuf, Option<u64>)>, |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | pub fps: Option<f64>, |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | pub tone: crate::tonemap::Preset, |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | pub proxy: bool, |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | pub auto_proxy: bool, |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | pub encoder: crate::export::EncoderSeat, |
| 299 | pub playhead: u32, |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | #[allow(clippy::too_many_arguments)] |
| 308 | pub fn save( |
| 309 | path: &Path, |
| 310 | sources: &[Source], |
| 311 | lanes: &[(LaneKind, Vec<Clip>)], |
| 312 | gains: &[f32], |
| 313 | subs: &[Vec<SubClip>], |
| 314 | subtitles: &[SubtitleTrack], |
| 315 | eq: &[EqParams], |
| 316 | color: &[ColorParams], |
| 317 | resolution: (u32, u32), |
| 318 | fps: Option<f64>, |
| 319 | tone: crate::tonemap::Preset, |
| 320 | proxy: bool, |
| 321 | auto_proxy: bool, |
| 322 | encoder: crate::export::EncoderSeat, |
| 323 | limiter: Limiter, |
| 324 | playhead: u32, |
| 325 | ) -> crate::Result<()> { |
| 326 | let dir = project_dir(path); |
| 327 | let mut part = path.to_path_buf().into_os_string(); |
| 328 | part.push(".part"); |
| 329 | let part = PathBuf::from(part); |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | let result = std::fs::File::create(&part) |
| 335 | .and_then(|mut f| { |
| 336 | f.write_all(&emit( |
| 337 | &dir, sources, lanes, gains, subs, subtitles, eq, color, resolution, fps, tone, |
| 338 | proxy, auto_proxy, encoder, limiter, playhead, |
| 339 | ))?; |
| 340 | f.sync_all() |
| 341 | }) |
| 342 | .and_then(|()| std::fs::rename(&part, path)) |
| 343 | .and_then(|()| std::fs::File::open(&dir)?.sync_all()); |
| 344 | if result.is_err() { |
| 345 | let _ = std::fs::remove_file(&part); |
| 346 | } |
| 347 | result.map_err(Into::into) |
| 348 | } |
| 349 | |
| 350 | pub fn load(path: &Path) -> crate::Result<Document> { |
| 351 | let data = std::fs::read(path)?; |
| 352 | parse(&data, path.parent().unwrap_or(Path::new(""))) |
| 353 | } |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | fn project_dir(path: &Path) -> PathBuf { |
| 362 | let dir = path |
| 363 | .parent() |
| 364 | .filter(|p| !p.as_os_str().is_empty()) |
| 365 | .unwrap_or(Path::new(".")); |
| 366 | dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf()) |
| 367 | } |
| 368 | |
| 369 | #[allow(clippy::too_many_arguments)] |
| 370 | fn emit( |
| 371 | dir: &Path, |
| 372 | sources: &[Source], |
| 373 | lanes: &[(LaneKind, Vec<Clip>)], |
| 374 | gains: &[f32], |
| 375 | subs: &[Vec<SubClip>], |
| 376 | subtitles: &[SubtitleTrack], |
| 377 | eq: &[EqParams], |
| 378 | color: &[ColorParams], |
| 379 | resolution: (u32, u32), |
| 380 | fps: Option<f64>, |
| 381 | tone: crate::tonemap::Preset, |
| 382 | proxy: bool, |
| 383 | auto_proxy: bool, |
| 384 | encoder: crate::export::EncoderSeat, |
| 385 | limiter: Limiter, |
| 386 | playhead: u32, |
| 387 | ) -> Vec<u8> { |
| 388 | let mut out = Vec::new(); |
| 389 | out.extend_from_slice(MAGIC); |
| 390 | out.push(b'\n'); |
| 391 | out.extend_from_slice(format!("playhead {playhead}\n").as_bytes()); |
| 392 | out.extend_from_slice(format!("resolution {} {}\n", resolution.0, resolution.1).as_bytes()); |
| 393 | |
| 394 | |
| 395 | if let Some(fps) = fps.filter(|f| f.is_finite() && *f > 0.0) { |
| 396 | out.extend_from_slice(format!("fps {fps:?}\n").as_bytes()); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | |
| 401 | if tone != crate::tonemap::Preset::default() { |
| 402 | out.extend_from_slice(format!("tonemap {}\n", tone.name()).as_bytes()); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | if proxy { |
| 407 | out.extend_from_slice(b"proxy on\n"); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | if !auto_proxy { |
| 413 | out.extend_from_slice(b"autoproxy off\n"); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | |
| 418 | if encoder != crate::export::EncoderSeat::default() { |
| 419 | out.extend_from_slice(format!("encoder {}\n", encoder.name()).as_bytes()); |
| 420 | } |
| 421 | |
| 422 | |
| 423 | if limiter != Limiter::default() { |
| 424 | out.extend_from_slice( |
| 425 | format!( |
| 426 | "limiter {:?} {}\n", |
| 427 | limiter.ceiling_db, |
| 428 | match limiter.on { |
| 429 | true => "on", |
| 430 | false => "off", |
| 431 | } |
| 432 | ) |
| 433 | .as_bytes(), |
| 434 | ); |
| 435 | } |
| 436 | for s in sources { |
| 437 | out.extend_from_slice(format!("source {} ", s.audio_stream).as_bytes()); |
| 438 | escape(s.path.strip_prefix(dir).unwrap_or(&s.path), &mut out); |
| 439 | out.push(b'\n'); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | for t in subtitles { |
| 446 | let track = t.track.map_or("-".to_string(), |n| n.to_string()); |
| 447 | out.extend_from_slice(format!("subtitle {track} ").as_bytes()); |
| 448 | escape(t.path.strip_prefix(dir).unwrap_or(&t.path), &mut out); |
| 449 | out.push(b'\n'); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | for params in eq { |
| 454 | out.extend_from_slice(b"eq"); |
| 455 | for b in ¶ms.bands { |
| 456 | |
| 457 | |
| 458 | |
| 459 | let kind = match b.kind { |
| 460 | BandKind::LowShelf => "ls", |
| 461 | BandKind::Peak => "pk", |
| 462 | BandKind::HighShelf => "hs", |
| 463 | }; |
| 464 | out.extend_from_slice( |
| 465 | format!(" {:?}:{:?}:{:?}:{kind}", b.freq_hz, b.gain_db, b.q).as_bytes(), |
| 466 | ); |
| 467 | } |
| 468 | out.push(b'\n'); |
| 469 | } |
| 470 | |
| 471 | for p in color { |
| 472 | out.extend_from_slice( |
| 473 | format!( |
| 474 | "color {:?}:{:?}:{:?}:{:?}\n", |
| 475 | p.brightness, p.contrast, p.saturation, p.tint |
| 476 | ) |
| 477 | .as_bytes(), |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | |
| 483 | let (mut video, mut audio, mut subtitle) = (0, 0, 0); |
| 484 | for (at, (kind, clips)) in lanes.iter().enumerate() { |
| 485 | let (keyword, ord) = match kind { |
| 486 | LaneKind::Video => { |
| 487 | video += 1; |
| 488 | ("video", video) |
| 489 | } |
| 490 | LaneKind::Audio => { |
| 491 | audio += 1; |
| 492 | ("audio", audio) |
| 493 | } |
| 494 | LaneKind::Subtitle => { |
| 495 | subtitle += 1; |
| 496 | ("sub", subtitle) |
| 497 | } |
| 498 | }; |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | if *kind == LaneKind::Subtitle { |
| 504 | let placed = subs.get(at).map_or(&[][..], Vec::as_slice); |
| 505 | if placed.is_empty() { |
| 506 | out.extend_from_slice(format!("{keyword} {ord}\n").as_bytes()); |
| 507 | } |
| 508 | for s in placed { |
| 509 | |
| 510 | |
| 511 | |
| 512 | let link = s.link.map_or(String::new(), |l| format!(" {l}")); |
| 513 | out.extend_from_slice( |
| 514 | format!( |
| 515 | "{keyword} {ord} {} {} {} {} {}{link}\n", |
| 516 | s.start, s.frames, s.track, s.in_us, s.out_us |
| 517 | ) |
| 518 | .as_bytes(), |
| 519 | ); |
| 520 | } |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | if clips.is_empty() { |
| 526 | out.extend_from_slice(format!("{keyword} {ord}\n").as_bytes()); |
| 527 | } |
| 528 | for c in clips { |
| 529 | let link = c.link.map_or("-".to_string(), |l| l.to_string()); |
| 530 | let eq = c.eq.map_or("-".to_string(), |e| e.to_string()); |
| 531 | let color = c.color.map_or("-".to_string(), |e| e.to_string()); |
| 532 | out.extend_from_slice( |
| 533 | format!( |
| 534 | "{keyword} {ord} {} {} {} {} {link} {eq} {color} {} {}\n", |
| 535 | c.start, |
| 536 | c.in_frame, |
| 537 | c.out_frame, |
| 538 | c.source, |
| 539 | fit_name(c.fit), |
| 540 | c.speed.permille() |
| 541 | ) |
| 542 | .as_bytes(), |
| 543 | ); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | let (mut video, mut audio) = (0, 0); |
| 550 | for ((kind, _), &db) in lanes.iter().zip(gains) { |
| 551 | let (keyword, ord) = match kind { |
| 552 | LaneKind::Video => { |
| 553 | video += 1; |
| 554 | ("video", video) |
| 555 | } |
| 556 | LaneKind::Audio => { |
| 557 | audio += 1; |
| 558 | ("audio", audio) |
| 559 | } |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | LaneKind::Subtitle => continue, |
| 565 | }; |
| 566 | if db != 0.0 { |
| 567 | out.extend_from_slice(format!("gain {keyword} {ord} {db:?}\n").as_bytes()); |
| 568 | } |
| 569 | } |
| 570 | out |
| 571 | } |
| 572 | |
| 573 | fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { |
| 574 | |
| 575 | |
| 576 | let body = data.strip_suffix(b"\n").unwrap_or(data); |
| 577 | let mut lines = body.split(|&b| b == b'\n').enumerate(); |
| 578 | let (_, first) = lines.next().unwrap_or((0, &[])); |
| 579 | let v1 = first == MAGIC_V1; |
| 580 | |
| 581 | |
| 582 | let streamless = v1 || first == MAGIC_V2; |
| 583 | |
| 584 | let v16 = first == MAGIC; |
| 585 | |
| 586 | let v15 = v16 || first == MAGIC_V15; |
| 587 | |
| 588 | let v14 = v15 || first == MAGIC_V14; |
| 589 | |
| 590 | let v13 = v14 || first == MAGIC_V13; |
| 591 | |
| 592 | let v12 = v13 || first == MAGIC_V12; |
| 593 | |
| 594 | let v11 = v12 || first == MAGIC_V11; |
| 595 | |
| 596 | let v10 = v11 || first == MAGIC_V10; |
| 597 | |
| 598 | |
| 599 | let v9 = v10 || first == MAGIC_V9; |
| 600 | |
| 601 | let v8 = v9 || first == MAGIC_V8; |
| 602 | |
| 603 | let v7 = v8 || first == MAGIC_V7; |
| 604 | |
| 605 | let v6 = v7 || first == MAGIC_V6; |
| 606 | |
| 607 | let v5 = v6 || first == MAGIC_V5; |
| 608 | |
| 609 | let numbered = v5 || first == MAGIC_V4; |
| 610 | if !numbered && !streamless && first != MAGIC_V3 { |
| 611 | return Err(match first.strip_prefix(b"edith ") { |
| 612 | Some(v) => format!("line 1: unsupported version {}", String::from_utf8_lossy(v)), |
| 613 | None => "line 1: not a edith file".to_string(), |
| 614 | } |
| 615 | .into()); |
| 616 | } |
| 617 | |
| 618 | let mut doc = Document { |
| 619 | sources: Vec::new(), |
| 620 | |
| 621 | |
| 622 | lanes: if numbered { |
| 623 | Vec::new() |
| 624 | } else { |
| 625 | vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, Vec::new())] |
| 626 | }, |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | subs: Vec::new(), |
| 632 | |
| 633 | eq: Vec::new(), |
| 634 | color: Vec::new(), |
| 635 | |
| 636 | |
| 637 | resolution: None, |
| 638 | |
| 639 | |
| 640 | gains: Vec::new(), |
| 641 | |
| 642 | |
| 643 | subtitles: Vec::new(), |
| 644 | limiter: Limiter::default(), |
| 645 | tone: crate::tonemap::Preset::default(), |
| 646 | proxy: false, |
| 647 | |
| 648 | |
| 649 | |
| 650 | auto_proxy: true, |
| 651 | |
| 652 | |
| 653 | |
| 654 | encoder: crate::export::EncoderSeat::default(), |
| 655 | fps: None, |
| 656 | playhead: 0, |
| 657 | }; |
| 658 | let mut playhead_seen = false; |
| 659 | |
| 660 | |
| 661 | let mut queued = 0u32; |
| 662 | for (i, line) in lines { |
| 663 | let n = i + 1; |
| 664 | let (keyword, rest) = match line.iter().position(|&b| b == b' ') { |
| 665 | Some(at) => (&line[..at], &line[at + 1..]), |
| 666 | None => (line, &line[line.len()..]), |
| 667 | }; |
| 668 | match keyword { |
| 669 | b"playhead" => { |
| 670 | if playhead_seen || !doc.sources.is_empty() { |
| 671 | return Err( |
| 672 | format!("line {n}: playhead belongs once, before the sources").into(), |
| 673 | ); |
| 674 | } |
| 675 | doc.playhead = number(rest, n)?; |
| 676 | playhead_seen = true; |
| 677 | } |
| 678 | b"resolution" if v7 => { |
| 679 | if doc.resolution.is_some() || !doc.sources.is_empty() { |
| 680 | return Err( |
| 681 | format!("line {n}: resolution belongs once, before the sources").into(), |
| 682 | ); |
| 683 | } |
| 684 | let f = fields(rest, 2, "resolution", n)?; |
| 685 | let (width, height) = (number(f[0], n)?, number(f[1], n)?); |
| 686 | |
| 687 | |
| 688 | |
| 689 | if !crate::is_resolution(width, height) { |
| 690 | return Err(format!("line {n}: {width}x{height} is not a picture").into()); |
| 691 | } |
| 692 | doc.resolution = Some((width, height)); |
| 693 | } |
| 694 | b"fps" if v9 => { |
| 695 | if doc.fps.is_some() || !doc.sources.is_empty() { |
| 696 | return Err(format!("line {n}: fps belongs once, before the sources").into()); |
| 697 | } |
| 698 | let f = fields(rest, 1, "fps", n)?; |
| 699 | let fps = std::str::from_utf8(f[0]) |
| 700 | .ok() |
| 701 | .and_then(|s| s.parse::<f64>().ok()) |
| 702 | .filter(|f| f.is_finite() && *f > 0.0 && *f <= 1000.0) |
| 703 | .ok_or_else(|| { |
| 704 | format!( |
| 705 | "line {n}: {:?} is not a frame rate", |
| 706 | String::from_utf8_lossy(f[0]) |
| 707 | ) |
| 708 | })?; |
| 709 | doc.fps = Some(fps); |
| 710 | } |
| 711 | b"tonemap" if v11 => { |
| 712 | if !doc.sources.is_empty() { |
| 713 | return Err( |
| 714 | format!("line {n}: tonemap belongs once, before the sources").into(), |
| 715 | ); |
| 716 | } |
| 717 | let f = fields(rest, 1, "tonemap", n)?; |
| 718 | doc.tone = crate::tonemap::Preset::from_name(f[0]).ok_or_else(|| { |
| 719 | format!( |
| 720 | "line {n}: {:?} is not a tone map", |
| 721 | String::from_utf8_lossy(f[0]) |
| 722 | ) |
| 723 | })?; |
| 724 | } |
| 725 | b"proxy" if v12 => { |
| 726 | if !doc.sources.is_empty() { |
| 727 | return Err( |
| 728 | format!("line {n}: proxy belongs once, before the sources").into() |
| 729 | ); |
| 730 | } |
| 731 | let f = fields(rest, 1, "proxy", n)?; |
| 732 | doc.proxy = match f[0] { |
| 733 | b"on" => true, |
| 734 | b"off" => false, |
| 735 | other => { |
| 736 | return Err(format!( |
| 737 | "line {n}: proxy is on or off, not {:?}", |
| 738 | String::from_utf8_lossy(other) |
| 739 | ) |
| 740 | .into()); |
| 741 | } |
| 742 | }; |
| 743 | } |
| 744 | b"autoproxy" if v13 => { |
| 745 | if !doc.sources.is_empty() { |
| 746 | return Err( |
| 747 | format!("line {n}: autoproxy belongs once, before the sources").into(), |
| 748 | ); |
| 749 | } |
| 750 | let f = fields(rest, 1, "autoproxy", n)?; |
| 751 | doc.auto_proxy = match f[0] { |
| 752 | b"on" => true, |
| 753 | b"off" => false, |
| 754 | other => { |
| 755 | return Err(format!( |
| 756 | "line {n}: autoproxy is on or off, not {:?}", |
| 757 | String::from_utf8_lossy(other) |
| 758 | ) |
| 759 | .into()); |
| 760 | } |
| 761 | }; |
| 762 | } |
| 763 | b"encoder" if v14 => { |
| 764 | if !doc.sources.is_empty() { |
| 765 | return Err( |
| 766 | format!("line {n}: encoder belongs once, before the sources").into() |
| 767 | ); |
| 768 | } |
| 769 | let f = fields(rest, 1, "encoder", n)?; |
| 770 | doc.encoder = crate::export::EncoderSeat::from_name(f[0]).ok_or_else(|| { |
| 771 | format!( |
| 772 | "line {n}: encoder is auto, hardware or software, not {:?}", |
| 773 | String::from_utf8_lossy(f[0]) |
| 774 | ) |
| 775 | })?; |
| 776 | } |
| 777 | b"limiter" if v9 => { |
| 778 | if !doc.sources.is_empty() { |
| 779 | return Err( |
| 780 | format!("line {n}: limiter belongs once, before the sources").into(), |
| 781 | ); |
| 782 | } |
| 783 | let f = fields(rest, 2, "limiter", n)?; |
| 784 | let on = match f[1] { |
| 785 | b"on" => true, |
| 786 | b"off" => false, |
| 787 | other => { |
| 788 | return Err(format!( |
| 789 | "line {n}: limiter is on or off, not {:?}", |
| 790 | String::from_utf8_lossy(other) |
| 791 | ) |
| 792 | .into()); |
| 793 | } |
| 794 | }; |
| 795 | |
| 796 | |
| 797 | doc.limiter = Limiter { |
| 798 | on, |
| 799 | ..Limiter::default() |
| 800 | } |
| 801 | .with_ceiling(float(f[0], n)?); |
| 802 | } |
| 803 | |
| 804 | |
| 805 | b"gain" if v9 => { |
| 806 | let f = fields(rest, 3, "gain", n)?; |
| 807 | let kind = match f[0] { |
| 808 | b"video" => LaneKind::Video, |
| 809 | b"audio" => LaneKind::Audio, |
| 810 | other => { |
| 811 | return Err(format!( |
| 812 | "line {n}: gain names {:?}, not a lane kind", |
| 813 | String::from_utf8_lossy(other) |
| 814 | ) |
| 815 | .into()); |
| 816 | } |
| 817 | }; |
| 818 | let ord = number(f[1], n)?; |
| 819 | let at = doc |
| 820 | .lanes |
| 821 | .iter() |
| 822 | .enumerate() |
| 823 | .filter(|(_, (k, _))| *k == kind) |
| 824 | .nth(ord.saturating_sub(1) as usize) |
| 825 | .map(|(at, _)| at) |
| 826 | .ok_or_else(|| format!("line {n}: gain names a lane that is not there"))?; |
| 827 | doc.gains.resize(doc.lanes.len(), 0.0); |
| 828 | doc.gains[at] = float(f[2], n)?; |
| 829 | } |
| 830 | |
| 831 | |
| 832 | b"subtitle" if v10 => { |
| 833 | let at = rest |
| 834 | .iter() |
| 835 | .position(|&b| b == b' ') |
| 836 | .ok_or_else(|| format!("line {n}: subtitle without a path"))?; |
| 837 | let track = match &rest[..at] { |
| 838 | b"-" => None, |
| 839 | field => Some(u64::from(number(field, n)?)), |
| 840 | }; |
| 841 | let path = unescape(&rest[at + 1..], n)?; |
| 842 | if path.as_os_str().is_empty() { |
| 843 | return Err(format!("line {n}: subtitle without a path").into()); |
| 844 | } |
| 845 | doc.subtitles.push(( |
| 846 | |
| 847 | if path.is_absolute() { |
| 848 | path |
| 849 | } else { |
| 850 | dir.join(path) |
| 851 | }, |
| 852 | track, |
| 853 | )); |
| 854 | } |
| 855 | b"source" => { |
| 856 | if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { |
| 857 | return Err(format!("line {n}: source after a clip").into()); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | let (audio_stream, rest) = if streamless { |
| 862 | (0, rest) |
| 863 | } else { |
| 864 | |
| 865 | |
| 866 | |
| 867 | let at = rest.iter().position(|&b| b == b' '); |
| 868 | ( |
| 869 | number(&rest[..at.unwrap_or(rest.len())], n)? as usize, |
| 870 | &rest[at.map_or(rest.len(), |at| at + 1)..], |
| 871 | ) |
| 872 | }; |
| 873 | if rest.is_empty() { |
| 874 | return Err(format!("line {n}: source without a path").into()); |
| 875 | } |
| 876 | let path = unescape(rest, n)?; |
| 877 | |
| 878 | |
| 879 | doc.sources.push(Source { |
| 880 | path: if path.is_absolute() { |
| 881 | path |
| 882 | } else { |
| 883 | dir.join(path) |
| 884 | }, |
| 885 | audio_stream, |
| 886 | }); |
| 887 | } |
| 888 | b"eq" if v5 => { |
| 889 | if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { |
| 890 | return Err(format!("line {n}: eq after a clip").into()); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | let bands = match rest.is_empty() { |
| 895 | true => Vec::new(), |
| 896 | false => rest |
| 897 | .split(|&b| b == b' ') |
| 898 | .map(|field| band(field, n)) |
| 899 | .collect::<crate::Result<Vec<Band>>>()?, |
| 900 | }; |
| 901 | doc.eq.push(EqParams { bands }); |
| 902 | } |
| 903 | b"color" if v6 => { |
| 904 | if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { |
| 905 | return Err(format!("line {n}: color after a clip").into()); |
| 906 | } |
| 907 | let f = fields(rest, 1, "color", n)?; |
| 908 | let parts: Vec<&[u8]> = f[0].split(|&b| b == b':').collect(); |
| 909 | if parts.len() != 4 { |
| 910 | return Err( |
| 911 | format!("line {n}: color wants 4 fields, found {}", parts.len()).into(), |
| 912 | ); |
| 913 | } |
| 914 | doc.color.push(ColorParams { |
| 915 | brightness: float(parts[0], n)?, |
| 916 | contrast: float(parts[1], n)?, |
| 917 | saturation: float(parts[2], n)?, |
| 918 | tint: float(parts[3], n)?, |
| 919 | }); |
| 920 | } |
| 921 | |
| 922 | |
| 923 | |
| 924 | b"clip" if v1 => { |
| 925 | let f = fields(rest, 3, "clip", n)?; |
| 926 | let clip = check( |
| 927 | Clip { |
| 928 | start: queued, |
| 929 | in_frame: number(f[0], n)?, |
| 930 | out_frame: number(f[1], n)?, |
| 931 | source: number(f[2], n)? as usize, |
| 932 | link: Some(doc.lanes[0].1.len() as u32), |
| 933 | eq: None, |
| 934 | color: None, |
| 935 | fit: FitPolicy::default(), |
| 936 | speed: Speed::NORMAL, |
| 937 | }, |
| 938 | &doc, |
| 939 | n, |
| 940 | )?; |
| 941 | queued = clip.end(); |
| 942 | doc.lanes[0].1.push(clip); |
| 943 | doc.lanes[1].1.push(clip); |
| 944 | } |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | b"sub" if v15 => { |
| 950 | let at = rest.iter().position(|&b| b == b' '); |
| 951 | let ord = number(&rest[..at.unwrap_or(rest.len())], n)?; |
| 952 | let rest = &rest[at.map_or(rest.len(), |at| at + 1)..]; |
| 953 | let at = lane_of(&mut doc.lanes, LaneKind::Subtitle, ord, n)?; |
| 954 | doc.subs.resize(doc.lanes.len(), Vec::new()); |
| 955 | |
| 956 | |
| 957 | if rest.is_empty() { |
| 958 | continue; |
| 959 | } |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | let all: Vec<&[u8]> = rest.split(|&b| b == b' ').collect(); |
| 965 | let ok = all.len() == 5 || (v16 && all.len() == 6); |
| 966 | if !ok { |
| 967 | return Err(format!( |
| 968 | "line {n}: sub wants {} fields, found {}", |
| 969 | if v16 { "5 or 6" } else { "5" }, |
| 970 | all.len() |
| 971 | ) |
| 972 | .into()); |
| 973 | } |
| 974 | let link = match all.get(5) { |
| 975 | None => None, |
| 976 | Some(f) if *f == b"-" => None, |
| 977 | Some(f) => Some(number(f, n)?), |
| 978 | }; |
| 979 | let f = &all; |
| 980 | let sub = SubClip { |
| 981 | start: number(f[0], n)?, |
| 982 | frames: number(f[1], n)?, |
| 983 | track: number(f[2], n)? as usize, |
| 984 | in_us: micros(f[3], n)?, |
| 985 | out_us: micros(f[4], n)?, |
| 986 | link, |
| 987 | }; |
| 988 | |
| 989 | |
| 990 | if sub.frames == 0 || sub.out_us <= sub.in_us || sub.in_us < 0 { |
| 991 | return Err(format!( |
| 992 | "line {n}: caption at {} is empty: {} frames of [{}, {})", |
| 993 | sub.start, sub.frames, sub.in_us, sub.out_us |
| 994 | ) |
| 995 | .into()); |
| 996 | } |
| 997 | |
| 998 | |
| 999 | |
| 1000 | if sub.track >= doc.subtitles.len() { |
| 1001 | return Err(format!( |
| 1002 | "line {n}: caption names subtitle track {} of {}", |
| 1003 | sub.track, |
| 1004 | doc.subtitles.len() |
| 1005 | ) |
| 1006 | .into()); |
| 1007 | } |
| 1008 | if sub.start.checked_add(sub.frames).is_none() { |
| 1009 | return Err(format!( |
| 1010 | "line {n}: caption at {} runs past the last frame there is", |
| 1011 | sub.start |
| 1012 | ) |
| 1013 | .into()); |
| 1014 | } |
| 1015 | let lane = &mut doc.subs[at]; |
| 1016 | if lane.last().is_some_and(|prev| prev.end() > sub.start) { |
| 1017 | return Err(format!( |
| 1018 | "line {n}: caption at {} overlaps the one before it, or comes before it", |
| 1019 | sub.start |
| 1020 | ) |
| 1021 | .into()); |
| 1022 | } |
| 1023 | lane.push(sub); |
| 1024 | } |
| 1025 | b"video" | b"audio" if !v1 => { |
| 1026 | let kind = match keyword { |
| 1027 | b"video" => LaneKind::Video, |
| 1028 | _ => LaneKind::Audio, |
| 1029 | }; |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | let (ord, rest) = if numbered { |
| 1034 | let at = rest.iter().position(|&b| b == b' '); |
| 1035 | ( |
| 1036 | number(&rest[..at.unwrap_or(rest.len())], n)?, |
| 1037 | &rest[at.map_or(rest.len(), |at| at + 1)..], |
| 1038 | ) |
| 1039 | } else { |
| 1040 | (1, rest) |
| 1041 | }; |
| 1042 | let at = lane_of(&mut doc.lanes, kind, ord, n)?; |
| 1043 | if numbered && rest.is_empty() { |
| 1044 | continue; |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | let want = |
| 1049 | 5 + usize::from(v5) + usize::from(v6) + usize::from(v7) + usize::from(v8); |
| 1050 | let f = fields(rest, want, "clip", n)?; |
| 1051 | let clip = check( |
| 1052 | Clip { |
| 1053 | start: number(f[0], n)?, |
| 1054 | in_frame: number(f[1], n)?, |
| 1055 | out_frame: number(f[2], n)?, |
| 1056 | source: number(f[3], n)? as usize, |
| 1057 | link: match f[4] { |
| 1058 | b"-" => None, |
| 1059 | field => Some(number(field, n)?), |
| 1060 | }, |
| 1061 | eq: table_index(f.get(5).copied(), doc.eq.len(), "eq", n)?, |
| 1062 | color: table_index(f.get(6).copied(), doc.color.len(), "color", n)?, |
| 1063 | fit: fit_policy(f.get(7).copied(), n)?, |
| 1064 | speed: speed_of(f.get(8).copied(), n)?, |
| 1065 | }, |
| 1066 | &doc, |
| 1067 | n, |
| 1068 | )?; |
| 1069 | let lane = &mut doc.lanes[at].1; |
| 1070 | |
| 1071 | |
| 1072 | if lane.last().is_some_and(|prev| prev.end() > clip.start) { |
| 1073 | return Err(format!( |
| 1074 | "line {n}: clip at {} overlaps the one before it, or comes before it", |
| 1075 | clip.start |
| 1076 | ) |
| 1077 | .into()); |
| 1078 | } |
| 1079 | lane.push(clip); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | _ => { |
| 1085 | return Err(format!( |
| 1086 | "line {n}: unknown keyword {:?}", |
| 1087 | String::from_utf8_lossy(keyword) |
| 1088 | ) |
| 1089 | .into()); |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | doc.gains.resize(doc.lanes.len(), 0.0); |
| 1096 | doc.subs.resize(doc.lanes.len(), Vec::new()); |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | Ok(doc) |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | fn lane_of( |
| 1107 | lanes: &mut Vec<(LaneKind, Vec<Clip>)>, |
| 1108 | kind: LaneKind, |
| 1109 | ord: u32, |
| 1110 | line: usize, |
| 1111 | ) -> crate::Result<usize> { |
| 1112 | let mut seen = 0; |
| 1113 | for (at, (k, _)) in lanes.iter().enumerate() { |
| 1114 | if *k == kind { |
| 1115 | seen += 1; |
| 1116 | if seen == ord { |
| 1117 | return Ok(at); |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | if ord != seen + 1 { |
| 1122 | return Err(match ord.checked_sub(1) { |
| 1123 | Some(prev) => format!( |
| 1124 | "line {line}: lane {} comes before {}", |
| 1125 | Lane::new(kind, prev as usize).label(), |
| 1126 | Lane::new(kind, seen as usize).label() |
| 1127 | ), |
| 1128 | None => format!("line {line}: lane numbers start at 1"), |
| 1129 | } |
| 1130 | .into()); |
| 1131 | } |
| 1132 | lanes.push((kind, Vec::new())); |
| 1133 | Ok(lanes.len() - 1) |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | fn fields<'a>( |
| 1138 | rest: &'a [u8], |
| 1139 | want: usize, |
| 1140 | what: &str, |
| 1141 | line: usize, |
| 1142 | ) -> crate::Result<Vec<&'a [u8]>> { |
| 1143 | let fields: Vec<&[u8]> = rest.split(|&b| b == b' ').collect(); |
| 1144 | if fields.len() != want { |
| 1145 | return Err(format!( |
| 1146 | "line {line}: {what} wants {want} fields, found {}", |
| 1147 | fields.len() |
| 1148 | ) |
| 1149 | .into()); |
| 1150 | } |
| 1151 | Ok(fields) |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | fn check(clip: Clip, doc: &Document, line: usize) -> crate::Result<Clip> { |
| 1156 | if clip.out_frame <= clip.in_frame { |
| 1157 | return Err(format!( |
| 1158 | "line {line}: clip [{}, {}) is empty", |
| 1159 | clip.in_frame, clip.out_frame |
| 1160 | ) |
| 1161 | .into()); |
| 1162 | } |
| 1163 | if clip.source >= doc.sources.len() { |
| 1164 | return Err(format!( |
| 1165 | "line {line}: clip names source {} of {}", |
| 1166 | clip.source, |
| 1167 | doc.sources.len() |
| 1168 | ) |
| 1169 | .into()); |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | if clip.start.checked_add(clip.frames()).is_none() { |
| 1175 | return Err(format!( |
| 1176 | "line {line}: clip at {} runs past the last frame there is", |
| 1177 | clip.start |
| 1178 | ) |
| 1179 | .into()); |
| 1180 | } |
| 1181 | Ok(clip) |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | fn speed_of(field: Option<&[u8]>, line: usize) -> crate::Result<Speed> { |
| 1191 | let Some(field) = field else { |
| 1192 | return Ok(Speed::NORMAL); |
| 1193 | }; |
| 1194 | let permille = number(field, line)?; |
| 1195 | let speed = Speed::from_permille(permille.min(u32::from(u16::MAX)) as u16); |
| 1196 | if u32::from(speed.permille()) != permille { |
| 1197 | return Err(format!( |
| 1198 | "line {line}: speed {permille} is outside {}-{} thousandths", |
| 1199 | Speed::MIN.permille(), |
| 1200 | Speed::MAX.permille() |
| 1201 | ) |
| 1202 | .into()); |
| 1203 | } |
| 1204 | Ok(speed) |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | fn fit_name(fit: FitPolicy) -> &'static str { |
| 1210 | match fit { |
| 1211 | FitPolicy::Fit => "fit", |
| 1212 | FitPolicy::Fill => "fill", |
| 1213 | FitPolicy::Stretch => "stretch", |
| 1214 | FitPolicy::Center => "center", |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | fn fit_policy(field: Option<&[u8]>, line: usize) -> crate::Result<FitPolicy> { |
| 1222 | let Some(field) = field else { |
| 1223 | return Ok(FitPolicy::default()); |
| 1224 | }; |
| 1225 | match field { |
| 1226 | b"fit" => Ok(FitPolicy::Fit), |
| 1227 | b"fill" => Ok(FitPolicy::Fill), |
| 1228 | b"stretch" => Ok(FitPolicy::Stretch), |
| 1229 | b"center" => Ok(FitPolicy::Center), |
| 1230 | other => Err(format!( |
| 1231 | "line {line}: {:?} is not a fit policy", |
| 1232 | String::from_utf8_lossy(other) |
| 1233 | ) |
| 1234 | .into()), |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | fn table_index( |
| 1242 | field: Option<&[u8]>, |
| 1243 | len: usize, |
| 1244 | what: &str, |
| 1245 | line: usize, |
| 1246 | ) -> crate::Result<Option<u16>> { |
| 1247 | let field = match field { |
| 1248 | None | Some(b"-") => return Ok(None), |
| 1249 | Some(field) => field, |
| 1250 | }; |
| 1251 | let i = number(field, line)?; |
| 1252 | if i as usize >= len || i > u32::from(u16::MAX) { |
| 1253 | return Err(format!("line {line}: clip names {what} {i} of {len}").into()); |
| 1254 | } |
| 1255 | Ok(Some(i as u16)) |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | fn band(field: &[u8], line: usize) -> crate::Result<Band> { |
| 1260 | let parts: Vec<&[u8]> = field.split(|&b| b == b':').collect(); |
| 1261 | if parts.len() != 4 { |
| 1262 | return Err(format!("line {line}: band wants 4 fields, found {}", parts.len()).into()); |
| 1263 | } |
| 1264 | Ok(Band { |
| 1265 | freq_hz: float(parts[0], line)?, |
| 1266 | gain_db: float(parts[1], line)?, |
| 1267 | q: float(parts[2], line)?, |
| 1268 | kind: match parts[3] { |
| 1269 | b"ls" => BandKind::LowShelf, |
| 1270 | b"pk" => BandKind::Peak, |
| 1271 | b"hs" => BandKind::HighShelf, |
| 1272 | other => { |
| 1273 | return Err(format!( |
| 1274 | "line {line}: {:?} is not a band shape", |
| 1275 | String::from_utf8_lossy(other) |
| 1276 | ) |
| 1277 | .into()); |
| 1278 | } |
| 1279 | }, |
| 1280 | }) |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | fn float(field: &[u8], line: usize) -> crate::Result<f32> { |
| 1288 | match std::str::from_utf8(field) |
| 1289 | .ok() |
| 1290 | .and_then(|s| s.parse::<f32>().ok()) |
| 1291 | .filter(|f| f.is_finite()) |
| 1292 | { |
| 1293 | Some(f) => Ok(f), |
| 1294 | None => Err(format!( |
| 1295 | "line {line}: {:?} is not a number", |
| 1296 | String::from_utf8_lossy(field) |
| 1297 | ) |
| 1298 | .into()), |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | fn number(field: &[u8], line: usize) -> crate::Result<u32> { |
| 1303 | match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) { |
| 1304 | Some(n) => Ok(n), |
| 1305 | None => Err(format!( |
| 1306 | "line {line}: {:?} is not a number", |
| 1307 | String::from_utf8_lossy(field) |
| 1308 | ) |
| 1309 | .into()), |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | fn micros(field: &[u8], line: usize) -> crate::Result<i64> { |
| 1318 | match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) { |
| 1319 | Some(n) => Ok(n), |
| 1320 | None => Err(format!( |
| 1321 | "line {line}: {:?} is not a number", |
| 1322 | String::from_utf8_lossy(field) |
| 1323 | ) |
| 1324 | .into()), |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | fn escape(path: &Path, out: &mut Vec<u8>) { |
| 1330 | for &b in path.as_os_str().as_bytes() { |
| 1331 | match b { |
| 1332 | b'%' => out.extend_from_slice(b"%25"), |
| 1333 | b'\n' => out.extend_from_slice(b"%0A"), |
| 1334 | _ => out.push(b), |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | fn unescape(field: &[u8], line: usize) -> crate::Result<PathBuf> { |
| 1342 | let mut out = Vec::with_capacity(field.len()); |
| 1343 | let mut i = 0; |
| 1344 | while i < field.len() { |
| 1345 | if field[i] != b'%' { |
| 1346 | out.push(field[i]); |
| 1347 | i += 1; |
| 1348 | continue; |
| 1349 | } |
| 1350 | let byte = field |
| 1351 | .get(i + 1..i + 3) |
| 1352 | .and_then(|h| std::str::from_utf8(h).ok()) |
| 1353 | .and_then(|h| u8::from_str_radix(h, 16).ok()) |
| 1354 | .ok_or_else(|| -> crate::Error { |
| 1355 | format!("line {line}: truncated % escape in the path").into() |
| 1356 | })?; |
| 1357 | out.push(byte); |
| 1358 | i += 3; |
| 1359 | } |
| 1360 | Ok(PathBuf::from(OsString::from_vec(out))) |
| 1361 | } |
| 1362 | |
| 1363 | #[cfg(test)] |
| 1364 | mod tests { |
| 1365 | use super::*; |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | #[allow(clippy::too_many_arguments)] |
| 1373 | fn emit( |
| 1374 | dir: &Path, |
| 1375 | sources: &[Source], |
| 1376 | lanes: &[(LaneKind, Vec<Clip>)], |
| 1377 | eq: &[EqParams], |
| 1378 | color: &[ColorParams], |
| 1379 | resolution: (u32, u32), |
| 1380 | playhead: u32, |
| 1381 | ) -> Vec<u8> { |
| 1382 | super::emit( |
| 1383 | dir, |
| 1384 | sources, |
| 1385 | lanes, |
| 1386 | &[], |
| 1387 | &[], |
| 1388 | &[], |
| 1389 | eq, |
| 1390 | color, |
| 1391 | resolution, |
| 1392 | None, |
| 1393 | crate::tonemap::Preset::default(), |
| 1394 | false, |
| 1395 | true, |
| 1396 | crate::export::EncoderSeat::default(), |
| 1397 | Limiter::default(), |
| 1398 | playhead, |
| 1399 | ) |
| 1400 | } |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | fn subtitle(path: &str, track: Option<u64>) -> SubtitleTrack { |
| 1405 | SubtitleTrack { |
| 1406 | path: PathBuf::from(path), |
| 1407 | track, |
| 1408 | language: String::new(), |
| 1409 | name: String::new(), |
| 1410 | label: String::new(), |
| 1411 | cues: Vec::new(), |
| 1412 | bitmap: false, |
| 1413 | refused: None, |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | |
| 1419 | |
| 1420 | #[test] |
| 1421 | fn subtitles_round_trip_as_references_and_a_v9_file_has_none() { |
| 1422 | let dir = PathBuf::from("/proj"); |
| 1423 | let (_, sources, lanes) = doc(); |
| 1424 | let tracks = [ |
| 1425 | subtitle("/proj/subs.srt", None), |
| 1426 | subtitle("/proj/a.mp4", Some(3)), |
| 1427 | subtitle("/elsewhere/od d name.ass", None), |
| 1428 | ]; |
| 1429 | let bytes = super::emit( |
| 1430 | &dir, |
| 1431 | &sources, |
| 1432 | &lanes, |
| 1433 | &[], |
| 1434 | &[], |
| 1435 | &tracks, |
| 1436 | &[], |
| 1437 | &[], |
| 1438 | (1280, 720), |
| 1439 | None, |
| 1440 | crate::tonemap::Preset::default(), |
| 1441 | false, |
| 1442 | true, |
| 1443 | crate::export::EncoderSeat::default(), |
| 1444 | Limiter::default(), |
| 1445 | 0, |
| 1446 | ); |
| 1447 | let text = String::from_utf8_lossy(&bytes); |
| 1448 | assert!( |
| 1449 | text.contains( |
| 1450 | "subtitle - subs.srt\n\ |
| 1451 | subtitle 3 a.mp4\n\ |
| 1452 | subtitle - /elsewhere/od d name.ass\n" |
| 1453 | ), |
| 1454 | "the path is relative where it can be, and runs to the end of its \ |
| 1455 | line, spaces and all: {text}" |
| 1456 | ); |
| 1457 | let back = parse(&bytes, &dir).expect("v10 parses"); |
| 1458 | assert_eq!( |
| 1459 | back.subtitles, |
| 1460 | vec![ |
| 1461 | (PathBuf::from("/proj/subs.srt"), None), |
| 1462 | (PathBuf::from("/proj/a.mp4"), Some(3)), |
| 1463 | (PathBuf::from("/elsewhere/od d name.ass"), None), |
| 1464 | ] |
| 1465 | ); |
| 1466 | |
| 1467 | |
| 1468 | let v9 = b"edith 9\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1469 | video 1 0 0 30 0 - - - fit 1000\n"; |
| 1470 | let old = parse(v9, &dir).expect("v9 still loads"); |
| 1471 | assert_eq!(old.subtitles, Vec::new()); |
| 1472 | assert!(flat(&dir, &old.sources, &old.lanes, old.playhead).starts_with(b"edith 16\n")); |
| 1473 | |
| 1474 | let mixed = parse(b"edith 9\nsource 0 a.mp4\nsubtitle - subs.srt\n", &dir) |
| 1475 | .unwrap_err() |
| 1476 | .to_string(); |
| 1477 | assert_eq!(mixed, "line 3: unknown keyword \"subtitle\""); |
| 1478 | |
| 1479 | for line in ["subtitle -\n", "subtitle - \n", "subtitle x subs.srt\n"] { |
| 1480 | let file = format!("edith 10\nsource 0 a.mp4\n{line}"); |
| 1481 | assert!( |
| 1482 | parse(file.as_bytes(), &dir).is_err(), |
| 1483 | "{line:?} is not a subtitle line" |
| 1484 | ); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | #[test] |
| 1493 | fn a_caption_group_round_trips_and_an_old_dialect_refuses_the_field() { |
| 1494 | let dir = PathBuf::from("/proj"); |
| 1495 | let caption = |start: u32, frames: u32, link| SubClip { |
| 1496 | start, |
| 1497 | frames, |
| 1498 | track: 0, |
| 1499 | in_us: 0, |
| 1500 | out_us: i64::from(frames) * 1_000_000, |
| 1501 | link, |
| 1502 | }; |
| 1503 | let lanes = vec![ |
| 1504 | (LaneKind::Video, vec![clip(0, 0, 30, 0, Some(7))]), |
| 1505 | (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(7))]), |
| 1506 | (LaneKind::Subtitle, Vec::new()), |
| 1507 | ]; |
| 1508 | let subs = vec![vec![], vec![], vec![caption(0, 30, Some(7)), caption(40, 10, None)]]; |
| 1509 | let bytes = super::emit( |
| 1510 | &dir, |
| 1511 | &doc().1, |
| 1512 | &lanes, |
| 1513 | &[], |
| 1514 | &subs, |
| 1515 | &[subtitle("/proj/subs.srt", None)], |
| 1516 | &[], |
| 1517 | &[], |
| 1518 | (1280, 720), |
| 1519 | None, |
| 1520 | crate::tonemap::Preset::default(), |
| 1521 | false, |
| 1522 | true, |
| 1523 | crate::export::EncoderSeat::default(), |
| 1524 | Limiter::default(), |
| 1525 | 0, |
| 1526 | ); |
| 1527 | let text = String::from_utf8_lossy(&bytes); |
| 1528 | assert!( |
| 1529 | text.contains("sub 1 0 30 0 0 30000000 7\n"), |
| 1530 | "the grouped caption carries its id: {text}" |
| 1531 | ); |
| 1532 | assert!( |
| 1533 | text.contains("sub 1 40 10 0 0 10000000\n"), |
| 1534 | "a caption in no group writes no field: {text}" |
| 1535 | ); |
| 1536 | let back = parse(&bytes, &dir).expect("v16 parses"); |
| 1537 | assert_eq!(back.subs[2][0].link, Some(7)); |
| 1538 | assert_eq!(back.subs[2][1].link, None); |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | let v15 = parse( |
| 1543 | b"edith 15\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\nsubtitle - subs.srt\n\ |
| 1544 | sub 1 0 30 0 0 30000000 7\n", |
| 1545 | &dir, |
| 1546 | ) |
| 1547 | .unwrap_err() |
| 1548 | .to_string(); |
| 1549 | assert_eq!(v15, "line 6: sub wants 5 fields, found 6"); |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | |
| 1555 | #[test] |
| 1556 | fn the_proxy_switch_round_trips_and_a_v11_file_is_cut_on_its_films() { |
| 1557 | let dir = PathBuf::from("/proj"); |
| 1558 | let (_, sources, lanes) = doc(); |
| 1559 | let bytes = |proxy| { |
| 1560 | super::emit( |
| 1561 | &dir, |
| 1562 | &sources, |
| 1563 | &lanes, |
| 1564 | &[], |
| 1565 | &[], |
| 1566 | &[], |
| 1567 | &[], |
| 1568 | &[], |
| 1569 | (1280, 720), |
| 1570 | None, |
| 1571 | crate::tonemap::Preset::default(), |
| 1572 | proxy, |
| 1573 | true, |
| 1574 | crate::export::EncoderSeat::default(), |
| 1575 | Limiter::default(), |
| 1576 | 0, |
| 1577 | ) |
| 1578 | }; |
| 1579 | let on = bytes(true); |
| 1580 | assert!( |
| 1581 | String::from_utf8_lossy(&on).contains("proxy on\n"), |
| 1582 | "{}", |
| 1583 | String::from_utf8_lossy(&on) |
| 1584 | ); |
| 1585 | assert!(parse(&on, &dir).expect("v12 parses").proxy); |
| 1586 | let off = bytes(false); |
| 1587 | assert!( |
| 1588 | !String::from_utf8_lossy(&off).contains("proxy"), |
| 1589 | "off is the line left out" |
| 1590 | ); |
| 1591 | assert!(!parse(&off, &dir).expect("v12 parses").proxy); |
| 1592 | |
| 1593 | |
| 1594 | let v11 = b"edith 11\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1595 | video 1 0 0 30 0 - - - fit 1000\n"; |
| 1596 | let old = parse(v11, &dir).expect("v11 still loads"); |
| 1597 | assert!(!old.proxy); |
| 1598 | assert_eq!(old.sources.len(), 1, "the rest of it came back too"); |
| 1599 | |
| 1600 | let mixed = parse(b"edith 11\nsource 0 a.mp4\nproxy on\n", &dir) |
| 1601 | .unwrap_err() |
| 1602 | .to_string(); |
| 1603 | assert_eq!(mixed, "line 3: unknown keyword \"proxy\""); |
| 1604 | |
| 1605 | for line in ["proxy\n", "proxy yes\n", "proxy On\n"] { |
| 1606 | let file = format!("edith 13\nsource 0 a.mp4\n{line}"); |
| 1607 | assert!( |
| 1608 | parse(file.as_bytes(), &dir).is_err(), |
| 1609 | "{line:?} is not a proxy line" |
| 1610 | ); |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | #[test] |
| 1619 | fn the_auto_proxy_switch_round_trips_and_a_v12_file_makes_them_by_itself() { |
| 1620 | let dir = PathBuf::from("/proj"); |
| 1621 | let (_, sources, lanes) = doc(); |
| 1622 | let bytes = |proxy, auto| { |
| 1623 | super::emit( |
| 1624 | &dir, |
| 1625 | &sources, |
| 1626 | &lanes, |
| 1627 | &[], |
| 1628 | &[], |
| 1629 | &[], |
| 1630 | &[], |
| 1631 | &[], |
| 1632 | (1280, 720), |
| 1633 | None, |
| 1634 | crate::tonemap::Preset::default(), |
| 1635 | proxy, |
| 1636 | auto, |
| 1637 | crate::export::EncoderSeat::default(), |
| 1638 | Limiter::default(), |
| 1639 | 0, |
| 1640 | ) |
| 1641 | }; |
| 1642 | |
| 1643 | |
| 1644 | let on = bytes(false, true); |
| 1645 | assert!( |
| 1646 | !String::from_utf8_lossy(&on).contains("autoproxy"), |
| 1647 | "on is the line left out: {}", |
| 1648 | String::from_utf8_lossy(&on) |
| 1649 | ); |
| 1650 | assert!(parse(&on, &dir).expect("v13 parses").auto_proxy); |
| 1651 | let off = bytes(true, false); |
| 1652 | let text = String::from_utf8_lossy(&off).to_string(); |
| 1653 | assert!(text.contains("proxy on\nautoproxy off\n"), "{text}"); |
| 1654 | let back = parse(&off, &dir).expect("v13 parses"); |
| 1655 | assert!(back.proxy, "the other switch came back too"); |
| 1656 | assert!(!back.auto_proxy); |
| 1657 | |
| 1658 | |
| 1659 | let v12 = b"edith 12\nplayhead 0\nresolution 1280 720\nproxy on\nsource 0 a.mp4\n\ |
| 1660 | video 1 0 0 30 0 - - - fit 1000\n"; |
| 1661 | let old = parse(v12, &dir).expect("v12 still loads"); |
| 1662 | assert!(old.auto_proxy, "a v12 project made its stand-ins by itself"); |
| 1663 | assert!(old.proxy, "the rest of it came back too"); |
| 1664 | |
| 1665 | let mixed = parse(b"edith 12\nsource 0 a.mp4\nautoproxy off\n", &dir) |
| 1666 | .unwrap_err() |
| 1667 | .to_string(); |
| 1668 | assert_eq!(mixed, "line 3: unknown keyword \"autoproxy\""); |
| 1669 | |
| 1670 | for line in ["autoproxy\n", "autoproxy no\n", "autoproxy Off\n"] { |
| 1671 | let file = format!("edith 13\nsource 0 a.mp4\n{line}"); |
| 1672 | assert!( |
| 1673 | parse(file.as_bytes(), &dir).is_err(), |
| 1674 | "{line:?} is not an autoproxy line" |
| 1675 | ); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | |
| 1682 | #[test] |
| 1683 | fn the_encoder_seat_round_trips_by_name_and_a_v13_file_takes_the_machines() { |
| 1684 | use crate::export::EncoderSeat; |
| 1685 | let dir = PathBuf::from("/proj"); |
| 1686 | let (_, sources, lanes) = doc(); |
| 1687 | let bytes = |seat| { |
| 1688 | super::emit( |
| 1689 | &dir, |
| 1690 | &sources, |
| 1691 | &lanes, |
| 1692 | &[], |
| 1693 | &[], |
| 1694 | &[], |
| 1695 | &[], |
| 1696 | &[], |
| 1697 | (1280, 720), |
| 1698 | None, |
| 1699 | crate::tonemap::Preset::default(), |
| 1700 | false, |
| 1701 | true, |
| 1702 | seat, |
| 1703 | Limiter::default(), |
| 1704 | 0, |
| 1705 | ) |
| 1706 | }; |
| 1707 | |
| 1708 | |
| 1709 | let auto = bytes(EncoderSeat::Auto); |
| 1710 | assert!( |
| 1711 | !String::from_utf8_lossy(&auto).contains("encoder"), |
| 1712 | "auto is the line left out: {}", |
| 1713 | String::from_utf8_lossy(&auto) |
| 1714 | ); |
| 1715 | assert_eq!( |
| 1716 | parse(&auto, &dir).expect("v14 parses").encoder, |
| 1717 | EncoderSeat::Auto |
| 1718 | ); |
| 1719 | for seat in [EncoderSeat::Hardware, EncoderSeat::Software] { |
| 1720 | let written = bytes(seat); |
| 1721 | let text = String::from_utf8_lossy(&written).to_string(); |
| 1722 | assert!(text.contains(&format!("encoder {}\n", seat.name())), "{text}"); |
| 1723 | assert_eq!(parse(&written, &dir).expect("v14 parses").encoder, seat); |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | let v13 = b"edith 13\nplayhead 0\nresolution 1280 720\nautoproxy off\nsource 0 a.mp4\n\ |
| 1729 | video 1 0 0 30 0 - - - fit 1000\n"; |
| 1730 | let old = parse(v13, &dir).expect("v13 still loads"); |
| 1731 | assert_eq!( |
| 1732 | old.encoder, |
| 1733 | EncoderSeat::Auto, |
| 1734 | "a v13 project exported on whatever seat there was" |
| 1735 | ); |
| 1736 | assert!(!old.auto_proxy, "the rest of it came back too"); |
| 1737 | |
| 1738 | let mixed = parse(b"edith 13\nsource 0 a.mp4\nencoder software\n", &dir) |
| 1739 | .unwrap_err() |
| 1740 | .to_string(); |
| 1741 | assert_eq!(mixed, "line 3: unknown keyword \"encoder\""); |
| 1742 | |
| 1743 | for line in ["encoder\n", "encoder gpu\n", "encoder Hardware\n"] { |
| 1744 | let file = format!("edith 15\nsource 0 a.mp4\n{line}"); |
| 1745 | assert!( |
| 1746 | parse(file.as_bytes(), &dir).is_err(), |
| 1747 | "{line:?} is not an encoder line" |
| 1748 | ); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | #[test] |
| 1756 | fn the_tone_map_round_trips_by_name_and_a_v10_file_is_the_reference_one() { |
| 1757 | let dir = PathBuf::from("/proj"); |
| 1758 | let (_, sources, lanes) = doc(); |
| 1759 | for preset in crate::tonemap::Preset::ALL { |
| 1760 | let bytes = super::emit( |
| 1761 | &dir, |
| 1762 | &sources, |
| 1763 | &lanes, |
| 1764 | &[], |
| 1765 | &[], |
| 1766 | &[], |
| 1767 | &[], |
| 1768 | &[], |
| 1769 | (1280, 720), |
| 1770 | None, |
| 1771 | preset, |
| 1772 | false, |
| 1773 | true, |
| 1774 | crate::export::EncoderSeat::default(), |
| 1775 | Limiter::default(), |
| 1776 | 0, |
| 1777 | ); |
| 1778 | let text = String::from_utf8_lossy(&bytes).to_string(); |
| 1779 | assert_eq!( |
| 1780 | text.contains(&format!("tonemap {}\n", preset.name())), |
| 1781 | preset != crate::tonemap::Preset::default(), |
| 1782 | "the default is the line left out: {text}" |
| 1783 | ); |
| 1784 | assert_eq!(parse(&bytes, &dir).expect("v11 parses").tone, preset); |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | |
| 1789 | let v10 = b"edith 10\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1790 | video 1 0 0 30 0 - - - fit 1000\n"; |
| 1791 | let old = parse(v10, &dir).expect("v10 still loads"); |
| 1792 | assert_eq!(old.tone, crate::tonemap::Preset::Reference); |
| 1793 | |
| 1794 | let mixed = parse(b"edith 10\nsource 0 a.mp4\ntonemap vivid\n", &dir) |
| 1795 | .unwrap_err() |
| 1796 | .to_string(); |
| 1797 | assert_eq!(mixed, "line 3: unknown keyword \"tonemap\""); |
| 1798 | |
| 1799 | |
| 1800 | for line in ["tonemap\n", "tonemap bright\n", "tonemap Vivid\n"] { |
| 1801 | let file = format!("edith 11\nsource 0 a.mp4\n{line}"); |
| 1802 | assert!( |
| 1803 | parse(file.as_bytes(), &dir).is_err(), |
| 1804 | "{line:?} is not a tonemap line" |
| 1805 | ); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | fn clip(start: u32, in_frame: u32, out_frame: u32, source: usize, link: Option<u32>) -> Clip { |
| 1810 | Clip { |
| 1811 | start, |
| 1812 | in_frame, |
| 1813 | out_frame, |
| 1814 | source, |
| 1815 | link, |
| 1816 | eq: None, |
| 1817 | color: None, |
| 1818 | fit: FitPolicy::default(), |
| 1819 | speed: Speed::NORMAL, |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | fn flat( |
| 1826 | dir: &Path, |
| 1827 | sources: &[Source], |
| 1828 | lanes: &[(LaneKind, Vec<Clip>)], |
| 1829 | playhead: u32, |
| 1830 | ) -> Vec<u8> { |
| 1831 | emit(dir, sources, lanes, &[], &[], (1280, 720), playhead) |
| 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | fn source(path: &str, audio_stream: usize) -> Source { |
| 1838 | Source { |
| 1839 | path: PathBuf::from(path), |
| 1840 | audio_stream, |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | |
| 1845 | type Lanes = Vec<(LaneKind, Vec<Clip>)>; |
| 1846 | |
| 1847 | |
| 1848 | fn two(video: Vec<Clip>, audio: Vec<Clip>) -> Lanes { |
| 1849 | vec![(LaneKind::Video, video), (LaneKind::Audio, audio)] |
| 1850 | } |
| 1851 | |
| 1852 | fn doc() -> (PathBuf, Vec<Source>, Lanes) { |
| 1853 | ( |
| 1854 | PathBuf::from("/proj"), |
| 1855 | vec![source("/proj/a.mp4", 0), source("/elsewhere/b.mp4", 2)], |
| 1856 | two( |
| 1857 | vec![clip(0, 0, 30, 0, Some(0)), clip(30, 10, 20, 1, Some(1))], |
| 1858 | vec![clip(0, 0, 30, 0, Some(0))], |
| 1859 | ), |
| 1860 | ) |
| 1861 | } |
| 1862 | |
| 1863 | #[test] |
| 1864 | fn relative_and_absolute_paths_round_trip() { |
| 1865 | let (dir, sources, lanes) = doc(); |
| 1866 | let bytes = flat(&dir, &sources, &lanes, 12); |
| 1867 | assert_eq!( |
| 1868 | String::from_utf8_lossy(&bytes), |
| 1869 | "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1870 | source 2 /elsewhere/b.mp4\n\ |
| 1871 | video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ |
| 1872 | audio 1 0 0 30 0 0 - - fit 1000\n", |
| 1873 | "the file under the project directory is written relative to it, \ |
| 1874 | each with the audio stream it plays" |
| 1875 | ); |
| 1876 | let back = parse(&bytes, &dir).expect("parse"); |
| 1877 | assert_eq!(back.sources, sources, "relative entries rejoin the dir"); |
| 1878 | assert_eq!( |
| 1879 | back.lanes, lanes, |
| 1880 | "both lanes, in display order; the trailing gap needs no line" |
| 1881 | ); |
| 1882 | assert_eq!(back.playhead, 12); |
| 1883 | |
| 1884 | assert_eq!(flat(&dir, &back.sources, &back.lanes, back.playhead), bytes); |
| 1885 | } |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | #[test] |
| 1890 | fn any_number_of_lanes_round_trips() { |
| 1891 | let dir = PathBuf::from("/proj"); |
| 1892 | let sources = vec![source("/proj/a.mp4", 0)]; |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | |
| 1897 | let lanes = vec![ |
| 1898 | (LaneKind::Video, vec![clip(0, 0, 30, 0, Some(4))]), |
| 1899 | (LaneKind::Audio, Vec::new()), |
| 1900 | (LaneKind::Video, vec![clip(40, 0, 10, 0, None)]), |
| 1901 | (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(4))]), |
| 1902 | ]; |
| 1903 | let bytes = flat(&dir, &sources, &lanes, 7); |
| 1904 | assert_eq!( |
| 1905 | String::from_utf8_lossy(&bytes), |
| 1906 | "edith 16\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1907 | video 1 0 0 30 0 4 - - fit 1000\naudio 1\n\ |
| 1908 | video 2 40 0 10 0 - - - fit 1000\naudio 2 0 0 30 0 4 - - fit 1000\n", |
| 1909 | "an empty lane is a line of its own; everything else is its clips" |
| 1910 | ); |
| 1911 | let back = parse(&bytes, &dir).expect("parse"); |
| 1912 | assert_eq!(back.lanes, lanes, "lane list, order and links all survive"); |
| 1913 | assert_eq!(back.playhead, 7); |
| 1914 | assert_eq!(flat(&dir, &back.sources, &back.lanes, back.playhead), bytes); |
| 1915 | |
| 1916 | |
| 1917 | |
| 1918 | for (file, want) in [ |
| 1919 | ( |
| 1920 | "edith 4\nsource 0 a.mp4\nvideo 2 0 0 30 0 -\n", |
| 1921 | "line 3: lane V2 comes before V1", |
| 1922 | ), |
| 1923 | ( |
| 1924 | "edith 4\nsource 0 a.mp4\nvideo 1 0 0 30 0 -\naudio 3\n", |
| 1925 | "line 4: lane A3 comes before A1", |
| 1926 | ), |
| 1927 | ( |
| 1928 | "edith 4\nsource 0 a.mp4\nvideo 0 0 0 30 0 -\n", |
| 1929 | "line 3: lane numbers start at 1", |
| 1930 | ), |
| 1931 | |
| 1932 | ( |
| 1933 | "edith 4\nsource 0 a.mp4\nvideo 1 0 0 30 0\n", |
| 1934 | "line 3: clip wants 5 fields, found 4", |
| 1935 | ), |
| 1936 | |
| 1937 | ( |
| 1938 | "edith 3\nsource 0 a.mp4\nvideo 1 0 0 30 0 -\n", |
| 1939 | "line 3: clip wants 5 fields, found 6", |
| 1940 | ), |
| 1941 | ] { |
| 1942 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 1943 | } |
| 1944 | |
| 1945 | let empty = parse(b"edith 4\nsource 0 a.mp4\nvideo 1\naudio 1\n", &dir).expect("parse"); |
| 1946 | assert!(empty.lanes.iter().all(|(_, clips)| clips.is_empty())); |
| 1947 | assert_eq!(empty.lanes.len(), 2, "and its lanes survive the round trip"); |
| 1948 | } |
| 1949 | |
| 1950 | |
| 1951 | fn band_of(freq_hz: f32, gain_db: f32, q: f32, kind: BandKind) -> Band { |
| 1952 | Band { |
| 1953 | freq_hz, |
| 1954 | gain_db, |
| 1955 | q, |
| 1956 | kind, |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | #[test] |
| 1965 | fn equalizers_round_trip_bit_exactly_and_are_shared() { |
| 1966 | let dir = PathBuf::from("/proj"); |
| 1967 | let sources = vec![source("/proj/a.mp4", 0)]; |
| 1968 | let eq = vec![ |
| 1969 | EqParams { |
| 1970 | bands: vec![ |
| 1971 | band_of(80.0, -3.0, 0.707, BandKind::LowShelf), |
| 1972 | band_of(1000.0, 4.5, 1.0, BandKind::Peak), |
| 1973 | ], |
| 1974 | }, |
| 1975 | |
| 1976 | |
| 1977 | |
| 1978 | EqParams { |
| 1979 | bands: vec![band_of( |
| 1980 | 16_777_215.0, |
| 1981 | -0.1, |
| 1982 | f32::MIN_POSITIVE / 3.0, |
| 1983 | BandKind::HighShelf, |
| 1984 | )], |
| 1985 | }, |
| 1986 | EqParams { bands: Vec::new() }, |
| 1987 | ]; |
| 1988 | |
| 1989 | let lanes = two( |
| 1990 | vec![ |
| 1991 | Clip { |
| 1992 | eq: Some(0), |
| 1993 | ..clip(0, 0, 30, 0, Some(0)) |
| 1994 | }, |
| 1995 | Clip { |
| 1996 | eq: Some(1), |
| 1997 | ..clip(30, 10, 20, 0, None) |
| 1998 | }, |
| 1999 | ], |
| 2000 | vec![ |
| 2001 | Clip { |
| 2002 | eq: Some(0), |
| 2003 | ..clip(0, 0, 30, 0, Some(0)) |
| 2004 | }, |
| 2005 | Clip { |
| 2006 | eq: Some(2), |
| 2007 | ..clip(30, 0, 10, 0, None) |
| 2008 | }, |
| 2009 | ], |
| 2010 | ); |
| 2011 | let bytes = emit(&dir, &sources, &lanes, &eq, &[], (1280, 720), 0); |
| 2012 | assert_eq!( |
| 2013 | String::from_utf8_lossy(&bytes), |
| 2014 | "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2015 | eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ |
| 2016 | eq 16777215.0:-0.1:3.918315e-39:hs\n\ |
| 2017 | eq\n\ |
| 2018 | video 1 0 0 30 0 0 0 - fit 1000\nvideo 1 30 10 20 0 - 1 - fit 1000\n\ |
| 2019 | audio 1 0 0 30 0 0 0 - fit 1000\naudio 1 30 0 10 0 - 2 - fit 1000\n", |
| 2020 | "the table comes before the clips, and a clip names a line of it" |
| 2021 | ); |
| 2022 | |
| 2023 | let back = parse(&bytes, &dir).expect("parse"); |
| 2024 | assert_eq!(back.lanes, lanes, "every clip keeps the eq it named"); |
| 2025 | for (i, (got, want)) in back.eq.iter().zip(&eq).enumerate() { |
| 2026 | for (b, (got, want)) in got.bands.iter().zip(&want.bands).enumerate() { |
| 2027 | assert_eq!( |
| 2028 | ( |
| 2029 | got.freq_hz.to_bits(), |
| 2030 | got.gain_db.to_bits(), |
| 2031 | got.q.to_bits(), |
| 2032 | got.kind |
| 2033 | ), |
| 2034 | ( |
| 2035 | want.freq_hz.to_bits(), |
| 2036 | want.gain_db.to_bits(), |
| 2037 | want.q.to_bits(), |
| 2038 | want.kind |
| 2039 | ), |
| 2040 | "eq {i} band {b} came back as other bits" |
| 2041 | ); |
| 2042 | } |
| 2043 | } |
| 2044 | assert_eq!(back.eq.len(), 3, "the empty cascade is an entry of its own"); |
| 2045 | assert_eq!( |
| 2046 | emit( |
| 2047 | &dir, |
| 2048 | &back.sources, |
| 2049 | &back.lanes, |
| 2050 | &back.eq, |
| 2051 | &back.color, |
| 2052 | (1280, 720), |
| 2053 | 0 |
| 2054 | ), |
| 2055 | bytes |
| 2056 | ); |
| 2057 | } |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | #[test] |
| 2063 | fn colours_round_trip_bit_exactly_and_are_shared() { |
| 2064 | let dir = PathBuf::from("/proj"); |
| 2065 | let sources = vec![source("/proj/a.mp4", 0)]; |
| 2066 | let color = vec![ |
| 2067 | ColorParams { |
| 2068 | brightness: 0.1, |
| 2069 | contrast: 1.2, |
| 2070 | saturation: 0.9, |
| 2071 | tint: -0.3, |
| 2072 | }, |
| 2073 | |
| 2074 | |
| 2075 | |
| 2076 | ColorParams { |
| 2077 | brightness: -0.000_000_1, |
| 2078 | contrast: 16_777_215.0, |
| 2079 | saturation: f32::MIN_POSITIVE / 3.0, |
| 2080 | tint: -0.0, |
| 2081 | }, |
| 2082 | ColorParams::default(), |
| 2083 | ]; |
| 2084 | |
| 2085 | let lanes = two( |
| 2086 | vec![ |
| 2087 | Clip { |
| 2088 | color: Some(0), |
| 2089 | fit: FitPolicy::default(), |
| 2090 | speed: Speed::NORMAL, |
| 2091 | ..clip(0, 0, 30, 0, Some(0)) |
| 2092 | }, |
| 2093 | Clip { |
| 2094 | color: Some(1), |
| 2095 | fit: FitPolicy::default(), |
| 2096 | speed: Speed::NORMAL, |
| 2097 | ..clip(30, 10, 20, 0, None) |
| 2098 | }, |
| 2099 | ], |
| 2100 | vec![ |
| 2101 | Clip { |
| 2102 | color: Some(0), |
| 2103 | fit: FitPolicy::default(), |
| 2104 | speed: Speed::NORMAL, |
| 2105 | ..clip(0, 0, 30, 0, Some(0)) |
| 2106 | }, |
| 2107 | Clip { |
| 2108 | color: Some(2), |
| 2109 | fit: FitPolicy::default(), |
| 2110 | speed: Speed::NORMAL, |
| 2111 | ..clip(30, 0, 10, 0, None) |
| 2112 | }, |
| 2113 | ], |
| 2114 | ); |
| 2115 | let bytes = emit(&dir, &sources, &lanes, &[], &color, (1280, 720), 0); |
| 2116 | assert_eq!( |
| 2117 | String::from_utf8_lossy(&bytes), |
| 2118 | "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2119 | color 0.1:1.2:0.9:-0.3\n\ |
| 2120 | color -1e-7:16777215.0:3.918315e-39:-0.0\n\ |
| 2121 | color 0.0:1.0:1.0:0.0\n\ |
| 2122 | video 1 0 0 30 0 0 - 0 fit 1000\nvideo 1 30 10 20 0 - - 1 fit 1000\n\ |
| 2123 | audio 1 0 0 30 0 0 - 0 fit 1000\naudio 1 30 0 10 0 - - 2 fit 1000\n", |
| 2124 | "the table comes before the clips, and a clip names a line of it" |
| 2125 | ); |
| 2126 | |
| 2127 | let back = parse(&bytes, &dir).expect("parse"); |
| 2128 | assert_eq!(back.lanes, lanes, "every clip keeps the colour it named"); |
| 2129 | for (i, (got, want)) in back.color.iter().zip(&color).enumerate() { |
| 2130 | assert_eq!( |
| 2131 | ( |
| 2132 | got.brightness.to_bits(), |
| 2133 | got.contrast.to_bits(), |
| 2134 | got.saturation.to_bits(), |
| 2135 | got.tint.to_bits() |
| 2136 | ), |
| 2137 | ( |
| 2138 | want.brightness.to_bits(), |
| 2139 | want.contrast.to_bits(), |
| 2140 | want.saturation.to_bits(), |
| 2141 | want.tint.to_bits() |
| 2142 | ), |
| 2143 | "color {i} came back as other bits" |
| 2144 | ); |
| 2145 | } |
| 2146 | assert_eq!(back.color.len(), 3); |
| 2147 | assert_eq!( |
| 2148 | emit( |
| 2149 | &dir, |
| 2150 | &back.sources, |
| 2151 | &back.lanes, |
| 2152 | &back.eq, |
| 2153 | &back.color, |
| 2154 | (1280, 720), |
| 2155 | 0 |
| 2156 | ), |
| 2157 | bytes |
| 2158 | ); |
| 2159 | } |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | #[test] |
| 2164 | fn a_v5_file_grades_nothing() { |
| 2165 | let dir = PathBuf::from("/proj"); |
| 2166 | let v5 = b"edith 5\nplayhead 3\nsource 0 a.mp4\neq 80.0:-3.0:0.707:ls\n\ |
| 2167 | video 1 0 0 30 0 0 0\naudio 1 0 0 30 0 0 -\n"; |
| 2168 | let old = parse(v5, &dir).expect("v5 parses"); |
| 2169 | assert!(old.color.is_empty(), "nothing before v6 grades anything"); |
| 2170 | assert_eq!(old.eq.len(), 1, "...and its equalizers are untouched"); |
| 2171 | assert_eq!(old.lanes[0].1[0].eq, Some(0)); |
| 2172 | assert!(old.lanes[0].1[0].color.is_none()); |
| 2173 | assert_eq!( |
| 2174 | String::from_utf8_lossy(&emit( |
| 2175 | &dir, |
| 2176 | &old.sources, |
| 2177 | &old.lanes, |
| 2178 | &old.eq, |
| 2179 | &old.color, |
| 2180 | (1280, 720), |
| 2181 | old.playhead |
| 2182 | )), |
| 2183 | "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2184 | eq 80.0:-3.0:0.707:ls\n\ |
| 2185 | video 1 0 0 30 0 0 0 - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" |
| 2186 | ); |
| 2187 | } |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | |
| 2192 | |
| 2193 | #[test] |
| 2194 | fn speeds_round_trip_and_a_v7_file_plays_at_real_time() { |
| 2195 | let dir = PathBuf::from("/proj"); |
| 2196 | let sources = vec![Source::new("/proj/a.mp4", 0)]; |
| 2197 | let speeded = |start, in_frame, out_frame, permille| Clip { |
| 2198 | start, |
| 2199 | in_frame, |
| 2200 | out_frame, |
| 2201 | source: 0, |
| 2202 | link: None, |
| 2203 | eq: None, |
| 2204 | color: None, |
| 2205 | fit: FitPolicy::default(), |
| 2206 | speed: Speed::from_permille(permille), |
| 2207 | }; |
| 2208 | |
| 2209 | |
| 2210 | |
| 2211 | let lanes = vec![ |
| 2212 | ( |
| 2213 | LaneKind::Video, |
| 2214 | vec![speeded(0, 0, 30, 2000), speeded(15, 30, 40, 250)], |
| 2215 | ), |
| 2216 | (LaneKind::Audio, vec![speeded(0, 0, 30, 2000)]), |
| 2217 | ]; |
| 2218 | let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); |
| 2219 | assert_eq!( |
| 2220 | String::from_utf8_lossy(&bytes), |
| 2221 | "edith 16\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2222 | video 1 0 0 30 0 - - - fit 2000\nvideo 1 15 30 40 0 - - - fit 250\n\ |
| 2223 | audio 1 0 0 30 0 - - - fit 2000\n", |
| 2224 | "the rate is the clip line's last field, in thousandths" |
| 2225 | ); |
| 2226 | let back = parse(&bytes, &dir).expect("parse"); |
| 2227 | assert_eq!(back.lanes, lanes, "every clip keeps the rate it named"); |
| 2228 | |
| 2229 | |
| 2230 | let v7 = b"edith 7\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2231 | video 1 0 0 30 0 0 - - fit\naudio 1 0 0 30 0 0 - - fit\n"; |
| 2232 | let old = parse(v7, &dir).expect("v7 parses"); |
| 2233 | assert!( |
| 2234 | old.lanes |
| 2235 | .iter() |
| 2236 | .flat_map(|(_, clips)| clips) |
| 2237 | .all(|c| c.speed == Speed::NORMAL), |
| 2238 | "nothing before v8 plays at anything but real time" |
| 2239 | ); |
| 2240 | assert_eq!( |
| 2241 | String::from_utf8_lossy(&emit( |
| 2242 | &dir, |
| 2243 | &old.sources, |
| 2244 | &old.lanes, |
| 2245 | &old.eq, |
| 2246 | &old.color, |
| 2247 | (1280, 720), |
| 2248 | old.playhead |
| 2249 | )), |
| 2250 | "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2251 | video 1 0 0 30 0 0 - - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" |
| 2252 | ); |
| 2253 | |
| 2254 | let bad = b"edith 8\nsource 0 a.mp4\nvideo 1 0 0 30 0 - - - fit 9000\n"; |
| 2255 | assert_eq!( |
| 2256 | parse(bad, &dir).unwrap_err().to_string(), |
| 2257 | "line 3: speed 9000 is outside 250-4000 thousandths" |
| 2258 | ); |
| 2259 | } |
| 2260 | |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | #[test] |
| 2266 | fn the_mix_and_the_rate_round_trip_and_a_v8_file_is_flat() { |
| 2267 | let dir = PathBuf::from("/proj"); |
| 2268 | let sources = vec![Source::new("/proj/a.mp4", 0)]; |
| 2269 | let lanes = vec![ |
| 2270 | (LaneKind::Video, vec![clip(0, 0, 30, 0, None)]), |
| 2271 | (LaneKind::Audio, vec![clip(0, 0, 30, 0, None)]), |
| 2272 | |
| 2273 | |
| 2274 | (LaneKind::Audio, vec![clip(0, 0, 30, 0, None)]), |
| 2275 | ]; |
| 2276 | let limiter = Limiter { |
| 2277 | ceiling_db: -1.5, |
| 2278 | on: true, |
| 2279 | }; |
| 2280 | let bytes = super::emit( |
| 2281 | &dir, |
| 2282 | &sources, |
| 2283 | &lanes, |
| 2284 | &[0.0, 3.0, -6.5], |
| 2285 | &[], |
| 2286 | &[], |
| 2287 | &[], |
| 2288 | &[], |
| 2289 | (1280, 720), |
| 2290 | Some(23.976023976023978), |
| 2291 | crate::tonemap::Preset::default(), |
| 2292 | false, |
| 2293 | true, |
| 2294 | crate::export::EncoderSeat::default(), |
| 2295 | limiter, |
| 2296 | 0, |
| 2297 | ); |
| 2298 | assert_eq!( |
| 2299 | String::from_utf8_lossy(&bytes), |
| 2300 | "edith 16\nplayhead 0\nresolution 1280 720\nfps 23.976023976023978\n\ |
| 2301 | limiter -1.5 on\nsource 0 a.mp4\n\ |
| 2302 | video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n\ |
| 2303 | audio 2 0 0 30 0 - - - fit 1000\n\ |
| 2304 | gain audio 1 3.0\ngain audio 2 -6.5\n", |
| 2305 | "the mix is lines of its own, the gains after the lanes they name" |
| 2306 | ); |
| 2307 | let back = parse(&bytes, &dir).expect("parse"); |
| 2308 | assert_eq!(back.lanes, lanes); |
| 2309 | assert_eq!(back.gains, vec![0.0, 3.0, -6.5], "one per lane, in order"); |
| 2310 | assert_eq!(back.limiter, limiter); |
| 2311 | assert_eq!(back.fps, Some(23.976023976023978), "bit-exact, not 23.976"); |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | let v8 = b"edith 8\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2316 | video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n"; |
| 2317 | let old = parse(v8, &dir).expect("v8 parses"); |
| 2318 | assert_eq!(old.gains, vec![0.0, 0.0], "one per lane, all at unity"); |
| 2319 | assert_eq!(old.limiter, Limiter::default()); |
| 2320 | assert_eq!(old.fps, None); |
| 2321 | |
| 2322 | assert_eq!( |
| 2323 | String::from_utf8_lossy(&emit( |
| 2324 | &dir, |
| 2325 | &old.sources, |
| 2326 | &old.lanes, |
| 2327 | &old.eq, |
| 2328 | &old.color, |
| 2329 | (1280, 720), |
| 2330 | old.playhead |
| 2331 | )), |
| 2332 | "edith 16\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2333 | video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" |
| 2334 | ); |
| 2335 | |
| 2336 | |
| 2337 | for (text, want) in [ |
| 2338 | ( |
| 2339 | "edith 9\nfps nope\nsource 0 a.mp4\n", |
| 2340 | "line 2: \"nope\" is not a frame rate", |
| 2341 | ), |
| 2342 | ( |
| 2343 | "edith 9\nlimiter -1.0 maybe\nsource 0 a.mp4\n", |
| 2344 | "line 2: limiter is on or off, not \"maybe\"", |
| 2345 | ), |
| 2346 | ( |
| 2347 | "edith 9\nsource 0 a.mp4\nvideo 1 0 0 30 0 - - - fit 1000\ngain audio 1 -3.0\n", |
| 2348 | "line 4: gain names a lane that is not there", |
| 2349 | ), |
| 2350 | ( |
| 2351 | "edith 9\nsource 0 a.mp4\nvideo 1 0 0 30 0 - - - fit 1000\ngain lane 1 -3.0\n", |
| 2352 | "line 4: gain names \"lane\", not a lane kind", |
| 2353 | ), |
| 2354 | ] { |
| 2355 | assert_eq!( |
| 2356 | parse(text.as_bytes(), &dir).unwrap_err().to_string(), |
| 2357 | want, |
| 2358 | "{text:?}" |
| 2359 | ); |
| 2360 | } |
| 2361 | |
| 2362 | let loud = parse(b"edith 9\nlimiter 12.0 on\nsource 0 a.mp4\n", &dir).expect("parses"); |
| 2363 | assert_eq!(loud.limiter.ceiling_db, Limiter::MAX_DB); |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | #[test] |
| 2368 | fn a_malformed_colour_names_its_line() { |
| 2369 | let dir = PathBuf::from("/proj"); |
| 2370 | let head = "edith 6\nsource 0 a.mp4\n"; |
| 2371 | |
| 2372 | |
| 2373 | for (file, want) in [ |
| 2374 | ( |
| 2375 | format!("{head}color 0.0:1.0:1.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 2376 | "line 3: color wants 4 fields, found 3", |
| 2377 | ), |
| 2378 | ( |
| 2379 | format!("{head}color 0.0:1.0:1.0:0.0 0.0:1.0:1.0:0.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 2380 | "line 3: color wants 1 fields, found 2", |
| 2381 | ), |
| 2382 | ( |
| 2383 | format!("{head}color 0.0:warm:1.0:0.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 2384 | "line 3: \"warm\" is not a number", |
| 2385 | ), |
| 2386 | |
| 2387 | ( |
| 2388 | format!("{head}color inf:1.0:1.0:0.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 2389 | "line 3: \"inf\" is not a number", |
| 2390 | ), |
| 2391 | ( |
| 2392 | format!("{head}color 0.0:1.0:1.0:NaN\nvideo 1 0 0 5 0 - - 0\n"), |
| 2393 | "line 3: \"NaN\" is not a number", |
| 2394 | ), |
| 2395 | ( |
| 2396 | format!("{head}color 0.0:1.0:1.0:0.0\nvideo 1 0 0 5 0 - - 1\n"), |
| 2397 | "line 4: clip names color 1 of 1", |
| 2398 | ), |
| 2399 | ( |
| 2400 | format!("{head}video 1 0 0 5 0 - - 0\n"), |
| 2401 | "line 3: clip names color 0 of 0", |
| 2402 | ), |
| 2403 | |
| 2404 | |
| 2405 | ( |
| 2406 | format!("{head}video 1 0 0 5 0 - -\n"), |
| 2407 | "line 3: clip wants 7 fields, found 6", |
| 2408 | ), |
| 2409 | ( |
| 2410 | format!("{head}video 1 0 0 5 0 - - -\ncolor 0.0:1.0:1.0:0.0\n"), |
| 2411 | "line 4: color after a clip", |
| 2412 | ), |
| 2413 | |
| 2414 | ( |
| 2415 | "edith 5\nsource 0 a.mp4\ncolor 0.0:1.0:1.0:0.0\nvideo 1 0 0 5 0 - -\n".to_string(), |
| 2416 | "line 3: unknown keyword \"color\"", |
| 2417 | ), |
| 2418 | ] { |
| 2419 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | |
| 2424 | #[test] |
| 2425 | fn a_malformed_equalizer_names_its_line() { |
| 2426 | let dir = PathBuf::from("/proj"); |
| 2427 | let head = "edith 5\nsource 0 a.mp4\n"; |
| 2428 | for (file, want) in [ |
| 2429 | ( |
| 2430 | format!("{head}eq 80.0:0.0:0.707\nvideo 1 0 0 5 0 - 0\n"), |
| 2431 | "line 3: band wants 4 fields, found 3", |
| 2432 | ), |
| 2433 | ( |
| 2434 | format!("{head}eq 80.0:0.0:0.707:notch\nvideo 1 0 0 5 0 - 0\n"), |
| 2435 | "line 3: \"notch\" is not a band shape", |
| 2436 | ), |
| 2437 | ( |
| 2438 | format!("{head}eq 80.0:0.0:wide:pk\nvideo 1 0 0 5 0 - 0\n"), |
| 2439 | "line 3: \"wide\" is not a number", |
| 2440 | ), |
| 2441 | |
| 2442 | |
| 2443 | ( |
| 2444 | format!("{head}eq 80.0:inf:0.707:pk\nvideo 1 0 0 5 0 - 0\n"), |
| 2445 | "line 3: \"inf\" is not a number", |
| 2446 | ), |
| 2447 | ( |
| 2448 | format!("{head}eq NaN:0.0:0.707:pk\nvideo 1 0 0 5 0 - 0\n"), |
| 2449 | "line 3: \"NaN\" is not a number", |
| 2450 | ), |
| 2451 | ( |
| 2452 | format!("{head}eq 80.0:0.0:0.707:pk\nvideo 1 0 0 5 0 - 1\n"), |
| 2453 | "line 4: clip names eq 1 of 1", |
| 2454 | ), |
| 2455 | ( |
| 2456 | format!("{head}video 1 0 0 5 0 - 0\n"), |
| 2457 | "line 3: clip names eq 0 of 0", |
| 2458 | ), |
| 2459 | |
| 2460 | |
| 2461 | ( |
| 2462 | format!("{head}video 1 0 0 5 0 -\n"), |
| 2463 | "line 3: clip wants 6 fields, found 5", |
| 2464 | ), |
| 2465 | ( |
| 2466 | format!("{head}video 1 0 0 5 0 - -\neq 80.0:0.0:0.707:pk\n"), |
| 2467 | "line 4: eq after a clip", |
| 2468 | ), |
| 2469 | |
| 2470 | ( |
| 2471 | "edith 4\nsource 0 a.mp4\neq 80.0:0.0:0.707:pk\nvideo 1 0 0 5 0 -\n".to_string(), |
| 2472 | "line 3: unknown keyword \"eq\"", |
| 2473 | ), |
| 2474 | ] { |
| 2475 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | |
| 2482 | #[test] |
| 2483 | fn a_v4_file_equalizes_nothing() { |
| 2484 | let dir = PathBuf::from("/proj"); |
| 2485 | let (_, sources, lanes) = doc(); |
| 2486 | let v4 = b"edith 4\nplayhead 12\nsource 0 a.mp4\nsource 2 /elsewhere/b.mp4\n\ |
| 2487 | video 1 0 0 30 0 0\nvideo 1 30 10 20 1 1\naudio 1 0 0 30 0 0\n"; |
| 2488 | let old = parse(v4, &dir).expect("v4 parses"); |
| 2489 | assert_eq!( |
| 2490 | (&old.sources, &old.lanes, old.playhead), |
| 2491 | (&sources, &lanes, 12) |
| 2492 | ); |
| 2493 | assert!(old.eq.is_empty(), "nothing before v5 equalizes anything"); |
| 2494 | assert_eq!( |
| 2495 | String::from_utf8_lossy(&flat(&dir, &old.sources, &old.lanes, old.playhead)), |
| 2496 | "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2497 | source 2 /elsewhere/b.mp4\n\ |
| 2498 | video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ |
| 2499 | audio 1 0 0 30 0 0 - - fit 1000\n" |
| 2500 | ); |
| 2501 | |
| 2502 | let empty = parse( |
| 2503 | b"edith 5\nsource 0 a.mp4\nvideo 1 0 0 5 0 - -\naudio 1\n", |
| 2504 | &dir, |
| 2505 | ) |
| 2506 | .expect("parse"); |
| 2507 | assert!(empty.lanes[1].1.is_empty()); |
| 2508 | } |
| 2509 | |
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | #[test] |
| 2515 | fn a_v3_file_is_two_lanes_and_a_v2_one_is_stream_0() { |
| 2516 | let dir = PathBuf::from("/proj"); |
| 2517 | let (_, _, lanes) = doc(); |
| 2518 | let v3 = b"edith 3\nplayhead 12\nsource 0 a.mp4\nsource 2 /elsewhere/b.mp4\n\ |
| 2519 | video 0 0 30 0 0\nvideo 30 10 20 1 1\naudio 0 0 30 0 0\n"; |
| 2520 | let old = parse(v3, &dir).expect("v3 parses"); |
| 2521 | assert_eq!( |
| 2522 | (&old.sources, &old.lanes, old.playhead), |
| 2523 | ( |
| 2524 | &vec![source("/proj/a.mp4", 0), source("/elsewhere/b.mp4", 2)], |
| 2525 | &lanes, |
| 2526 | 12 |
| 2527 | ), |
| 2528 | "a v3 file loads as exactly the state its v5 twin does" |
| 2529 | ); |
| 2530 | |
| 2531 | |
| 2532 | assert_eq!( |
| 2533 | flat(&dir, &old.sources, &old.lanes, old.playhead), |
| 2534 | flat(&dir, &old.sources, &lanes, 12), |
| 2535 | ); |
| 2536 | |
| 2537 | let v2 = b"edith 2\nplayhead 12\nsource a.mp4\nsource /elsewhere/b.mp4\n\ |
| 2538 | video 0 0 30 0 0\nvideo 30 10 20 1 1\naudio 0 0 30 0 0\n"; |
| 2539 | let back = parse(v2, &dir).expect("v2 parses"); |
| 2540 | assert_eq!( |
| 2541 | back.sources, |
| 2542 | vec![source("/proj/a.mp4", 0), source("/elsewhere/b.mp4", 0)], |
| 2543 | ); |
| 2544 | assert_eq!(back.lanes, lanes); |
| 2545 | |
| 2546 | let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); |
| 2547 | assert_eq!( |
| 2548 | String::from_utf8_lossy(&v5), |
| 2549 | "edith 16\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 2550 | source 0 /elsewhere/b.mp4\n\ |
| 2551 | video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ |
| 2552 | audio 1 0 0 30 0 0 - - fit 1000\n", |
| 2553 | "a re-saved v2 project differs only by its version, the lane \ |
| 2554 | numbers, the streams it always meant and an equalizer it has none of" |
| 2555 | ); |
| 2556 | let again = parse(&v5, &dir).expect("v5 parses"); |
| 2557 | assert_eq!(again.sources, back.sources); |
| 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | |
| 2562 | #[test] |
| 2563 | fn a_v1_file_loads_as_two_grouped_lanes() { |
| 2564 | let dir = PathBuf::from("/proj"); |
| 2565 | let v1 = b"edith 1\nplayhead 5\nsource a.mp4\nclip 0 30 0\nclip 40 60 0\n"; |
| 2566 | let back = parse(v1, &dir).expect("v1 parses"); |
| 2567 | assert_eq!( |
| 2568 | back.lanes[0].1, |
| 2569 | vec![clip(0, 0, 30, 0, Some(0)), clip(30, 40, 60, 0, Some(1))], |
| 2570 | "v1 clips queue up: the second starts where the first ended" |
| 2571 | ); |
| 2572 | assert_eq!( |
| 2573 | back.lanes, |
| 2574 | two(back.lanes[0].1.clone(), back.lanes[0].1.clone()), |
| 2575 | "one take per clip, on both lanes" |
| 2576 | ); |
| 2577 | assert_eq!(back.playhead, 5); |
| 2578 | |
| 2579 | |
| 2580 | let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); |
| 2581 | assert!(v5.starts_with(b"edith 16\n")); |
| 2582 | let again = parse(&v5, &dir).expect("v5 parses"); |
| 2583 | assert_eq!(again.lanes, back.lanes); |
| 2584 | |
| 2585 | for (bytes, want) in [ |
| 2586 | ( |
| 2587 | &b"edith 1\nsource a.mp4\nvideo 0 0 30 0 -\n"[..], |
| 2588 | "line 3: unknown keyword \"video\"", |
| 2589 | ), |
| 2590 | ( |
| 2591 | b"edith 2\nsource a.mp4\nclip 0 30 0\n", |
| 2592 | "line 3: unknown keyword \"clip\"", |
| 2593 | ), |
| 2594 | ] { |
| 2595 | assert_eq!(parse(bytes, &dir).unwrap_err().to_string(), want); |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | |
| 2600 | |
| 2601 | #[test] |
| 2602 | fn a_lane_out_of_order_or_overlapping_is_refused() { |
| 2603 | let dir = PathBuf::from("/proj"); |
| 2604 | for bytes in [ |
| 2605 | &b"edith 2\nsource a.mp4\nvideo 30 0 30 0 -\nvideo 0 0 30 0 -\n"[..], |
| 2606 | b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n", |
| 2607 | ] { |
| 2608 | let err = parse(bytes, &dir).unwrap_err().to_string(); |
| 2609 | assert!(err.starts_with("line 4: clip at "), "{err}"); |
| 2610 | } |
| 2611 | |
| 2612 | assert!( |
| 2613 | parse( |
| 2614 | b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\naudio 0 0 30 0 -\n", |
| 2615 | &dir |
| 2616 | ) |
| 2617 | .is_ok() |
| 2618 | ); |
| 2619 | |
| 2620 | let only_audio = parse(b"edith 2\nsource a.mp4\naudio 0 0 30 0 -\n", &dir).expect("parse"); |
| 2621 | assert!(only_audio.lanes[0].1.is_empty(), "V1 is there and empty"); |
| 2622 | } |
| 2623 | |
| 2624 | |
| 2625 | |
| 2626 | |
| 2627 | #[test] |
| 2628 | fn a_clip_whose_end_overflows_is_refused() { |
| 2629 | let dir = PathBuf::from("/proj"); |
| 2630 | for bytes in [ |
| 2631 | &b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\naudio 0 0 30 0 -\n"[..], |
| 2632 | |
| 2633 | b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\nvideo 4294967295 0 30 0 -\n", |
| 2634 | |
| 2635 | b"edith 1\nsource a.mp4\nclip 0 4294967295 0\nclip 0 4294967295 0\n", |
| 2636 | ] { |
| 2637 | let err = parse(bytes, &dir).unwrap_err().to_string(); |
| 2638 | assert!( |
| 2639 | err.contains("runs past the last frame there is"), |
| 2640 | "not refused by name: {err}" |
| 2641 | ); |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | |
| 2646 | |
| 2647 | |
| 2648 | |
| 2649 | #[test] |
| 2650 | fn a_non_canonical_project_path_still_writes_relative_sources() { |
| 2651 | assert_eq!( |
| 2652 | project_dir(Path::new("a.edith")), |
| 2653 | std::env::current_dir().expect("cwd"), |
| 2654 | "a bare filename is saved into the working directory" |
| 2655 | ); |
| 2656 | |
| 2657 | let dir = crate::scratch::Scratch::dir("ve_edith_link"); |
| 2658 | std::fs::create_dir_all(dir.join("real")).expect("scratch dir"); |
| 2659 | std::os::unix::fs::symlink(dir.join("real"), dir.join("link")).expect("symlink"); |
| 2660 | let source = dir.join("real/a.mp4"); |
| 2661 | let path = dir.join("link/p.edith"); |
| 2662 | |
| 2663 | let one = [clip(0, 0, 30, 0, None)]; |
| 2664 | let entry = Source { |
| 2665 | path: source, |
| 2666 | audio_stream: 1, |
| 2667 | }; |
| 2668 | save( |
| 2669 | &path, |
| 2670 | &[entry], |
| 2671 | &two(one.to_vec(), one.to_vec()), |
| 2672 | &[], |
| 2673 | &[], |
| 2674 | &[], |
| 2675 | &[], |
| 2676 | &[], |
| 2677 | (1280, 720), |
| 2678 | None, |
| 2679 | crate::tonemap::Preset::default(), |
| 2680 | false, |
| 2681 | true, |
| 2682 | crate::export::EncoderSeat::default(), |
| 2683 | Limiter::default(), |
| 2684 | 0, |
| 2685 | ) |
| 2686 | .expect("save"); |
| 2687 | let bytes = std::fs::read(&path).expect("read back"); |
| 2688 | assert_eq!( |
| 2689 | String::from_utf8_lossy(&bytes), |
| 2690 | "edith 16\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ |
| 2691 | video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" |
| 2692 | ); |
| 2693 | |
| 2694 | |
| 2695 | |
| 2696 | assert_eq!( |
| 2697 | load(&path).expect("load").sources, |
| 2698 | vec![Source { |
| 2699 | path: dir.join("link/a.mp4"), |
| 2700 | audio_stream: 1, |
| 2701 | }] |
| 2702 | ); |
| 2703 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 2704 | } |
| 2705 | |
| 2706 | #[test] |
| 2707 | fn escapes_survive_the_bytes_json_would_not() { |
| 2708 | let dir = PathBuf::from("/proj"); |
| 2709 | |
| 2710 | let nasty = Source { |
| 2711 | path: PathBuf::from(OsString::from_vec(b"/proj/we\nird 100%\xff.mp4".to_vec())), |
| 2712 | audio_stream: 0, |
| 2713 | }; |
| 2714 | let bytes = flat( |
| 2715 | &dir, |
| 2716 | &[nasty.clone()], |
| 2717 | &two(vec![clip(0, 0, 5, 0, None)], Vec::new()), |
| 2718 | 0, |
| 2719 | ); |
| 2720 | assert_eq!( |
| 2721 | bytes.iter().filter(|&&b| b == b'\n').count(), |
| 2722 | 6, |
| 2723 | "the escaped newline must not become a line break: magic, playhead, \ |
| 2724 | resolution, source, the clip, and the empty audio lane's own line" |
| 2725 | ); |
| 2726 | let back = parse(&bytes, &dir).expect("parse"); |
| 2727 | |
| 2728 | |
| 2729 | assert_eq!(back.sources, vec![nasty]); |
| 2730 | } |
| 2731 | |
| 2732 | #[test] |
| 2733 | fn a_wrong_first_line_is_refused_by_name() { |
| 2734 | let dir = PathBuf::from("/proj"); |
| 2735 | let err = parse(b"edith 17\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) |
| 2736 | .unwrap_err() |
| 2737 | .to_string(); |
| 2738 | assert_eq!(err, "line 1: unsupported version 17"); |
| 2739 | for junk in [&b""[..], b"{}\n", b"source a.mp4\n"] { |
| 2740 | assert_eq!( |
| 2741 | parse(junk, &dir).unwrap_err().to_string(), |
| 2742 | "line 1: not a edith file" |
| 2743 | ); |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | #[test] |
| 2748 | fn malformed_lines_name_their_line_number() { |
| 2749 | let dir = PathBuf::from("/proj"); |
| 2750 | let cases: [(&[u8], &str); 10] = [ |
| 2751 | |
| 2752 | ( |
| 2753 | b"edith 3\nsource a.mp4\nvideo 0 0 5 0 -\n", |
| 2754 | "line 2: \"a.mp4\" is not a number", |
| 2755 | ), |
| 2756 | ( |
| 2757 | b"edith 3\nsource 0\nvideo 0 0 5 0 -\n", |
| 2758 | "line 2: source without a path", |
| 2759 | ), |
| 2760 | ( |
| 2761 | b"edith 1\nsource a.mp4\nclip 0 5\n", |
| 2762 | "line 3: clip wants 3 fields, found 2", |
| 2763 | ), |
| 2764 | ( |
| 2765 | b"edith 1\nsource a.mp4\nclip 0 five 0\n", |
| 2766 | "line 3: \"five\" is not a number", |
| 2767 | ), |
| 2768 | ( |
| 2769 | b"edith 1\nsource a.mp4\nclip 5 5 0\n", |
| 2770 | "line 3: clip [5, 5) is empty", |
| 2771 | ), |
| 2772 | ( |
| 2773 | b"edith 1\nsource a.mp4\nclip 7 5 0\n", |
| 2774 | "line 3: clip [7, 5) is empty", |
| 2775 | ), |
| 2776 | ( |
| 2777 | b"edith 1\nsource a.mp4\nclip 0 5 1\n", |
| 2778 | "line 3: clip names source 1 of 1", |
| 2779 | ), |
| 2780 | ( |
| 2781 | b"edith 1\nsource a.mp4\nclip 0 5 0\nsource b.mp4\n", |
| 2782 | "line 4: source after a clip", |
| 2783 | ), |
| 2784 | ( |
| 2785 | b"edith 1\nsource a.mp4\n\nclip 0 5 0\n", |
| 2786 | "line 3: unknown keyword \"\"", |
| 2787 | ), |
| 2788 | ( |
| 2789 | b"edith 1\nsource a.mp4\nplayhead 3\nclip 0 5 0\n", |
| 2790 | "line 3: playhead belongs once, before the sources", |
| 2791 | ), |
| 2792 | ]; |
| 2793 | for (bytes, want) in cases { |
| 2794 | assert_eq!(parse(bytes, &dir).unwrap_err().to_string(), want); |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | |
| 2799 | |
| 2800 | |
| 2801 | |
| 2802 | #[test] |
| 2803 | fn a_project_without_clips_round_trips() { |
| 2804 | let dir = PathBuf::from("/proj"); |
| 2805 | let source = Source { |
| 2806 | path: PathBuf::from("/proj/a.mp4"), |
| 2807 | audio_stream: 0, |
| 2808 | }; |
| 2809 | let bytes = flat(&dir, &[source.clone()], &two(Vec::new(), Vec::new()), 0); |
| 2810 | let back = parse(&bytes, &dir).expect("an emptied timeline is a project"); |
| 2811 | assert_eq!(back.sources, vec![source]); |
| 2812 | assert_eq!(back.lanes, two(Vec::new(), Vec::new())); |
| 2813 | assert_eq!(back.playhead, 0); |
| 2814 | |
| 2815 | assert_eq!( |
| 2816 | parse(b"edith 1\nsource a.mp4\n", &dir).expect("v1").lanes, |
| 2817 | two(Vec::new(), Vec::new()) |
| 2818 | ); |
| 2819 | assert!(parse(b"edith 1\n", &dir).expect("a project of nothing").sources.is_empty()); |
| 2820 | } |
| 2821 | |
| 2822 | |
| 2823 | #[test] |
| 2824 | fn truncations_never_panic() { |
| 2825 | let (dir, sources, lanes) = doc(); |
| 2826 | let bytes = flat(&dir, &sources, &lanes, 12); |
| 2827 | for cut in 0..bytes.len() { |
| 2828 | let _ = parse(&bytes[..cut], &dir); |
| 2829 | |
| 2830 | |
| 2831 | let _ = parse(&bytes[cut..], &dir); |
| 2832 | } |
| 2833 | |
| 2834 | assert_eq!( |
| 2835 | parse(b"edith 1\nsource a%\nclip 0 5 0\n", &dir) |
| 2836 | .unwrap_err() |
| 2837 | .to_string(), |
| 2838 | "line 2: truncated % escape in the path" |
| 2839 | ); |
| 2840 | } |
| 2841 | } |