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