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