| 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 | use std::ffi::OsString; |
| 99 | use std::io::Write; |
| 100 | use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 101 | use std::path::{Path, PathBuf}; |
| 102 | |
| 103 | use crate::color::ColorParams; |
| 104 | use crate::eq::{Band, BandKind, EqParams}; |
| 105 | use crate::project::{Clip, Lane, LaneKind, Source, Speed}; |
| 106 | use crate::scale::FitPolicy; |
| 107 | |
| 108 | |
| 109 | |
| 110 | const MAGIC: &[u8] = b"edith 8"; |
| 111 | const MAGIC_V7: &[u8] = b"edith 7"; |
| 112 | const MAGIC_V6: &[u8] = b"edith 6"; |
| 113 | const MAGIC_V5: &[u8] = b"edith 5"; |
| 114 | const MAGIC_V4: &[u8] = b"edith 4"; |
| 115 | const MAGIC_V3: &[u8] = b"edith 3"; |
| 116 | const MAGIC_V2: &[u8] = b"edith 2"; |
| 117 | const MAGIC_V1: &[u8] = b"edith 1"; |
| 118 | |
| 119 | |
| 120 | |
| 121 | #[derive(Debug)] |
| 122 | pub struct Document { |
| 123 | |
| 124 | |
| 125 | pub sources: Vec<Source>, |
| 126 | |
| 127 | |
| 128 | |
| 129 | pub lanes: Vec<(LaneKind, Vec<Clip>)>, |
| 130 | |
| 131 | |
| 132 | pub eq: Vec<EqParams>, |
| 133 | |
| 134 | |
| 135 | pub color: Vec<ColorParams>, |
| 136 | |
| 137 | |
| 138 | pub resolution: Option<(u32, u32)>, |
| 139 | pub playhead: u32, |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | pub fn save( |
| 145 | path: &Path, |
| 146 | sources: &[Source], |
| 147 | lanes: &[(LaneKind, Vec<Clip>)], |
| 148 | eq: &[EqParams], |
| 149 | color: &[ColorParams], |
| 150 | resolution: (u32, u32), |
| 151 | playhead: u32, |
| 152 | ) -> crate::Result<()> { |
| 153 | let dir = project_dir(path); |
| 154 | let mut part = path.to_path_buf().into_os_string(); |
| 155 | part.push(".part"); |
| 156 | let part = PathBuf::from(part); |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | let result = std::fs::File::create(&part) |
| 162 | .and_then(|mut f| { |
| 163 | f.write_all(&emit(&dir, sources, lanes, eq, color, resolution, playhead))?; |
| 164 | f.sync_all() |
| 165 | }) |
| 166 | .and_then(|()| std::fs::rename(&part, path)) |
| 167 | .and_then(|()| std::fs::File::open(&dir)?.sync_all()); |
| 168 | if result.is_err() { |
| 169 | let _ = std::fs::remove_file(&part); |
| 170 | } |
| 171 | result.map_err(Into::into) |
| 172 | } |
| 173 | |
| 174 | pub fn load(path: &Path) -> crate::Result<Document> { |
| 175 | let data = std::fs::read(path)?; |
| 176 | parse(&data, path.parent().unwrap_or(Path::new(""))) |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | fn project_dir(path: &Path) -> PathBuf { |
| 186 | let dir = path |
| 187 | .parent() |
| 188 | .filter(|p| !p.as_os_str().is_empty()) |
| 189 | .unwrap_or(Path::new(".")); |
| 190 | dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf()) |
| 191 | } |
| 192 | |
| 193 | fn emit( |
| 194 | dir: &Path, |
| 195 | sources: &[Source], |
| 196 | lanes: &[(LaneKind, Vec<Clip>)], |
| 197 | eq: &[EqParams], |
| 198 | color: &[ColorParams], |
| 199 | resolution: (u32, u32), |
| 200 | playhead: u32, |
| 201 | ) -> Vec<u8> { |
| 202 | let mut out = Vec::new(); |
| 203 | out.extend_from_slice(MAGIC); |
| 204 | out.push(b'\n'); |
| 205 | out.extend_from_slice(format!("playhead {playhead}\n").as_bytes()); |
| 206 | out.extend_from_slice(format!("resolution {} {}\n", resolution.0, resolution.1).as_bytes()); |
| 207 | for s in sources { |
| 208 | out.extend_from_slice(format!("source {} ", s.audio_stream).as_bytes()); |
| 209 | escape(s.path.strip_prefix(dir).unwrap_or(&s.path), &mut out); |
| 210 | out.push(b'\n'); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | for params in eq { |
| 215 | out.extend_from_slice(b"eq"); |
| 216 | for b in ¶ms.bands { |
| 217 | |
| 218 | |
| 219 | |
| 220 | let kind = match b.kind { |
| 221 | BandKind::LowShelf => "ls", |
| 222 | BandKind::Peak => "pk", |
| 223 | BandKind::HighShelf => "hs", |
| 224 | }; |
| 225 | out.extend_from_slice( |
| 226 | format!(" {:?}:{:?}:{:?}:{kind}", b.freq_hz, b.gain_db, b.q).as_bytes(), |
| 227 | ); |
| 228 | } |
| 229 | out.push(b'\n'); |
| 230 | } |
| 231 | |
| 232 | for p in color { |
| 233 | out.extend_from_slice( |
| 234 | format!( |
| 235 | "color {:?}:{:?}:{:?}:{:?}\n", |
| 236 | p.brightness, p.contrast, p.saturation, p.tint |
| 237 | ) |
| 238 | .as_bytes(), |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | let (mut video, mut audio) = (0, 0); |
| 245 | for (kind, clips) in lanes { |
| 246 | let (keyword, ord) = match kind { |
| 247 | LaneKind::Video => { |
| 248 | video += 1; |
| 249 | ("video", video) |
| 250 | } |
| 251 | LaneKind::Audio => { |
| 252 | audio += 1; |
| 253 | ("audio", audio) |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | |
| 258 | if clips.is_empty() { |
| 259 | out.extend_from_slice(format!("{keyword} {ord}\n").as_bytes()); |
| 260 | } |
| 261 | for c in clips { |
| 262 | let link = c.link.map_or("-".to_string(), |l| l.to_string()); |
| 263 | let eq = c.eq.map_or("-".to_string(), |e| e.to_string()); |
| 264 | let color = c.color.map_or("-".to_string(), |e| e.to_string()); |
| 265 | out.extend_from_slice( |
| 266 | format!( |
| 267 | "{keyword} {ord} {} {} {} {} {link} {eq} {color} {} {}\n", |
| 268 | c.start, |
| 269 | c.in_frame, |
| 270 | c.out_frame, |
| 271 | c.source, |
| 272 | fit_name(c.fit), |
| 273 | c.speed.permille() |
| 274 | ) |
| 275 | .as_bytes(), |
| 276 | ); |
| 277 | } |
| 278 | } |
| 279 | out |
| 280 | } |
| 281 | |
| 282 | fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { |
| 283 | |
| 284 | |
| 285 | let body = data.strip_suffix(b"\n").unwrap_or(data); |
| 286 | let mut lines = body.split(|&b| b == b'\n').enumerate(); |
| 287 | let (_, first) = lines.next().unwrap_or((0, &[])); |
| 288 | let v1 = first == MAGIC_V1; |
| 289 | |
| 290 | |
| 291 | let streamless = v1 || first == MAGIC_V2; |
| 292 | |
| 293 | let v8 = first == MAGIC; |
| 294 | |
| 295 | let v7 = v8 || first == MAGIC_V7; |
| 296 | |
| 297 | let v6 = v7 || first == MAGIC_V6; |
| 298 | |
| 299 | let v5 = v6 || first == MAGIC_V5; |
| 300 | |
| 301 | let numbered = v5 || first == MAGIC_V4; |
| 302 | if !numbered && !streamless && first != MAGIC_V3 { |
| 303 | return Err(match first.strip_prefix(b"edith ") { |
| 304 | Some(v) => format!("line 1: unsupported version {}", String::from_utf8_lossy(v)), |
| 305 | None => "line 1: not a edith file".to_string(), |
| 306 | } |
| 307 | .into()); |
| 308 | } |
| 309 | |
| 310 | let mut doc = Document { |
| 311 | sources: Vec::new(), |
| 312 | |
| 313 | |
| 314 | lanes: if numbered { |
| 315 | Vec::new() |
| 316 | } else { |
| 317 | vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, Vec::new())] |
| 318 | }, |
| 319 | |
| 320 | eq: Vec::new(), |
| 321 | color: Vec::new(), |
| 322 | |
| 323 | |
| 324 | resolution: None, |
| 325 | playhead: 0, |
| 326 | }; |
| 327 | let mut playhead_seen = false; |
| 328 | |
| 329 | |
| 330 | let mut queued = 0u32; |
| 331 | for (i, line) in lines { |
| 332 | let n = i + 1; |
| 333 | let (keyword, rest) = match line.iter().position(|&b| b == b' ') { |
| 334 | Some(at) => (&line[..at], &line[at + 1..]), |
| 335 | None => (line, &line[line.len()..]), |
| 336 | }; |
| 337 | match keyword { |
| 338 | b"playhead" => { |
| 339 | if playhead_seen || !doc.sources.is_empty() { |
| 340 | return Err( |
| 341 | format!("line {n}: playhead belongs once, before the sources").into(), |
| 342 | ); |
| 343 | } |
| 344 | doc.playhead = number(rest, n)?; |
| 345 | playhead_seen = true; |
| 346 | } |
| 347 | b"resolution" if v7 => { |
| 348 | if doc.resolution.is_some() || !doc.sources.is_empty() { |
| 349 | return Err( |
| 350 | format!("line {n}: resolution belongs once, before the sources").into(), |
| 351 | ); |
| 352 | } |
| 353 | let f = fields(rest, 2, "resolution", n)?; |
| 354 | let (width, height) = (number(f[0], n)?, number(f[1], n)?); |
| 355 | |
| 356 | |
| 357 | |
| 358 | if !crate::is_resolution(width, height) { |
| 359 | return Err(format!("line {n}: {width}x{height} is not a picture").into()); |
| 360 | } |
| 361 | doc.resolution = Some((width, height)); |
| 362 | } |
| 363 | b"source" => { |
| 364 | if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { |
| 365 | return Err(format!("line {n}: source after a clip").into()); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | let (audio_stream, rest) = if streamless { |
| 370 | (0, rest) |
| 371 | } else { |
| 372 | |
| 373 | |
| 374 | |
| 375 | let at = rest.iter().position(|&b| b == b' '); |
| 376 | ( |
| 377 | number(&rest[..at.unwrap_or(rest.len())], n)? as usize, |
| 378 | &rest[at.map_or(rest.len(), |at| at + 1)..], |
| 379 | ) |
| 380 | }; |
| 381 | if rest.is_empty() { |
| 382 | return Err(format!("line {n}: source without a path").into()); |
| 383 | } |
| 384 | let path = unescape(rest, n)?; |
| 385 | |
| 386 | |
| 387 | doc.sources.push(Source { |
| 388 | path: if path.is_absolute() { |
| 389 | path |
| 390 | } else { |
| 391 | dir.join(path) |
| 392 | }, |
| 393 | audio_stream, |
| 394 | }); |
| 395 | } |
| 396 | b"eq" if v5 => { |
| 397 | if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { |
| 398 | return Err(format!("line {n}: eq after a clip").into()); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | let bands = match rest.is_empty() { |
| 403 | true => Vec::new(), |
| 404 | false => rest |
| 405 | .split(|&b| b == b' ') |
| 406 | .map(|field| band(field, n)) |
| 407 | .collect::<crate::Result<Vec<Band>>>()?, |
| 408 | }; |
| 409 | doc.eq.push(EqParams { bands }); |
| 410 | } |
| 411 | b"color" if v6 => { |
| 412 | if doc.lanes.iter().any(|(_, clips)| !clips.is_empty()) { |
| 413 | return Err(format!("line {n}: color after a clip").into()); |
| 414 | } |
| 415 | let f = fields(rest, 1, "color", n)?; |
| 416 | let parts: Vec<&[u8]> = f[0].split(|&b| b == b':').collect(); |
| 417 | if parts.len() != 4 { |
| 418 | return Err( |
| 419 | format!("line {n}: color wants 4 fields, found {}", parts.len()).into(), |
| 420 | ); |
| 421 | } |
| 422 | doc.color.push(ColorParams { |
| 423 | brightness: float(parts[0], n)?, |
| 424 | contrast: float(parts[1], n)?, |
| 425 | saturation: float(parts[2], n)?, |
| 426 | tint: float(parts[3], n)?, |
| 427 | }); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | |
| 432 | b"clip" if v1 => { |
| 433 | let f = fields(rest, 3, "clip", n)?; |
| 434 | let clip = check( |
| 435 | Clip { |
| 436 | start: queued, |
| 437 | in_frame: number(f[0], n)?, |
| 438 | out_frame: number(f[1], n)?, |
| 439 | source: number(f[2], n)? as usize, |
| 440 | link: Some(doc.lanes[0].1.len() as u32), |
| 441 | eq: None, |
| 442 | color: None, |
| 443 | fit: FitPolicy::default(), |
| 444 | speed: Speed::NORMAL, |
| 445 | }, |
| 446 | &doc, |
| 447 | n, |
| 448 | )?; |
| 449 | queued = clip.end(); |
| 450 | doc.lanes[0].1.push(clip); |
| 451 | doc.lanes[1].1.push(clip); |
| 452 | } |
| 453 | b"video" | b"audio" if !v1 => { |
| 454 | let kind = match keyword { |
| 455 | b"video" => LaneKind::Video, |
| 456 | _ => LaneKind::Audio, |
| 457 | }; |
| 458 | |
| 459 | |
| 460 | |
| 461 | let (ord, rest) = if numbered { |
| 462 | let at = rest.iter().position(|&b| b == b' '); |
| 463 | ( |
| 464 | number(&rest[..at.unwrap_or(rest.len())], n)?, |
| 465 | &rest[at.map_or(rest.len(), |at| at + 1)..], |
| 466 | ) |
| 467 | } else { |
| 468 | (1, rest) |
| 469 | }; |
| 470 | let at = lane_of(&mut doc.lanes, kind, ord, n)?; |
| 471 | if numbered && rest.is_empty() { |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | let want = |
| 477 | 5 + usize::from(v5) + usize::from(v6) + usize::from(v7) + usize::from(v8); |
| 478 | let f = fields(rest, want, "clip", n)?; |
| 479 | let clip = check( |
| 480 | Clip { |
| 481 | start: number(f[0], n)?, |
| 482 | in_frame: number(f[1], n)?, |
| 483 | out_frame: number(f[2], n)?, |
| 484 | source: number(f[3], n)? as usize, |
| 485 | link: match f[4] { |
| 486 | b"-" => None, |
| 487 | field => Some(number(field, n)?), |
| 488 | }, |
| 489 | eq: table_index(f.get(5).copied(), doc.eq.len(), "eq", n)?, |
| 490 | color: table_index(f.get(6).copied(), doc.color.len(), "color", n)?, |
| 491 | fit: fit_policy(f.get(7).copied(), n)?, |
| 492 | speed: speed_of(f.get(8).copied(), n)?, |
| 493 | }, |
| 494 | &doc, |
| 495 | n, |
| 496 | )?; |
| 497 | let lane = &mut doc.lanes[at].1; |
| 498 | |
| 499 | |
| 500 | if lane.last().is_some_and(|prev| prev.end() > clip.start) { |
| 501 | return Err(format!( |
| 502 | "line {n}: clip at {} overlaps the one before it, or comes before it", |
| 503 | clip.start |
| 504 | ) |
| 505 | .into()); |
| 506 | } |
| 507 | lane.push(clip); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | |
| 512 | _ => { |
| 513 | return Err(format!( |
| 514 | "line {n}: unknown keyword {:?}", |
| 515 | String::from_utf8_lossy(keyword) |
| 516 | ) |
| 517 | .into()); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | |
| 523 | |
| 524 | Ok(doc) |
| 525 | } |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | fn lane_of( |
| 531 | lanes: &mut Vec<(LaneKind, Vec<Clip>)>, |
| 532 | kind: LaneKind, |
| 533 | ord: u32, |
| 534 | line: usize, |
| 535 | ) -> crate::Result<usize> { |
| 536 | let mut seen = 0; |
| 537 | for (at, (k, _)) in lanes.iter().enumerate() { |
| 538 | if *k == kind { |
| 539 | seen += 1; |
| 540 | if seen == ord { |
| 541 | return Ok(at); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | if ord != seen + 1 { |
| 546 | return Err(match ord.checked_sub(1) { |
| 547 | Some(prev) => format!( |
| 548 | "line {line}: lane {} comes before {}", |
| 549 | Lane::new(kind, prev as usize).label(), |
| 550 | Lane::new(kind, seen as usize).label() |
| 551 | ), |
| 552 | None => format!("line {line}: lane numbers start at 1"), |
| 553 | } |
| 554 | .into()); |
| 555 | } |
| 556 | lanes.push((kind, Vec::new())); |
| 557 | Ok(lanes.len() - 1) |
| 558 | } |
| 559 | |
| 560 | |
| 561 | fn fields<'a>( |
| 562 | rest: &'a [u8], |
| 563 | want: usize, |
| 564 | what: &str, |
| 565 | line: usize, |
| 566 | ) -> crate::Result<Vec<&'a [u8]>> { |
| 567 | let fields: Vec<&[u8]> = rest.split(|&b| b == b' ').collect(); |
| 568 | if fields.len() != want { |
| 569 | return Err(format!( |
| 570 | "line {line}: {what} wants {want} fields, found {}", |
| 571 | fields.len() |
| 572 | ) |
| 573 | .into()); |
| 574 | } |
| 575 | Ok(fields) |
| 576 | } |
| 577 | |
| 578 | |
| 579 | fn check(clip: Clip, doc: &Document, line: usize) -> crate::Result<Clip> { |
| 580 | if clip.out_frame <= clip.in_frame { |
| 581 | return Err(format!( |
| 582 | "line {line}: clip [{}, {}) is empty", |
| 583 | clip.in_frame, clip.out_frame |
| 584 | ) |
| 585 | .into()); |
| 586 | } |
| 587 | if clip.source >= doc.sources.len() { |
| 588 | return Err(format!( |
| 589 | "line {line}: clip names source {} of {}", |
| 590 | clip.source, |
| 591 | doc.sources.len() |
| 592 | ) |
| 593 | .into()); |
| 594 | } |
| 595 | |
| 596 | |
| 597 | |
| 598 | if clip.start.checked_add(clip.frames()).is_none() { |
| 599 | return Err(format!( |
| 600 | "line {line}: clip at {} runs past the last frame there is", |
| 601 | clip.start |
| 602 | ) |
| 603 | .into()); |
| 604 | } |
| 605 | Ok(clip) |
| 606 | } |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | fn speed_of(field: Option<&[u8]>, line: usize) -> crate::Result<Speed> { |
| 615 | let Some(field) = field else { |
| 616 | return Ok(Speed::NORMAL); |
| 617 | }; |
| 618 | let permille = number(field, line)?; |
| 619 | let speed = Speed::from_permille(permille.min(u32::from(u16::MAX)) as u16); |
| 620 | if u32::from(speed.permille()) != permille { |
| 621 | return Err(format!( |
| 622 | "line {line}: speed {permille} is outside {}-{} thousandths", |
| 623 | Speed::MIN.permille(), |
| 624 | Speed::MAX.permille() |
| 625 | ) |
| 626 | .into()); |
| 627 | } |
| 628 | Ok(speed) |
| 629 | } |
| 630 | |
| 631 | |
| 632 | |
| 633 | fn fit_name(fit: FitPolicy) -> &'static str { |
| 634 | match fit { |
| 635 | FitPolicy::Fit => "fit", |
| 636 | FitPolicy::Fill => "fill", |
| 637 | FitPolicy::Stretch => "stretch", |
| 638 | FitPolicy::Center => "center", |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | fn fit_policy(field: Option<&[u8]>, line: usize) -> crate::Result<FitPolicy> { |
| 646 | let Some(field) = field else { |
| 647 | return Ok(FitPolicy::default()); |
| 648 | }; |
| 649 | match field { |
| 650 | b"fit" => Ok(FitPolicy::Fit), |
| 651 | b"fill" => Ok(FitPolicy::Fill), |
| 652 | b"stretch" => Ok(FitPolicy::Stretch), |
| 653 | b"center" => Ok(FitPolicy::Center), |
| 654 | other => Err(format!( |
| 655 | "line {line}: {:?} is not a fit policy", |
| 656 | String::from_utf8_lossy(other) |
| 657 | ) |
| 658 | .into()), |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | fn table_index( |
| 666 | field: Option<&[u8]>, |
| 667 | len: usize, |
| 668 | what: &str, |
| 669 | line: usize, |
| 670 | ) -> crate::Result<Option<u16>> { |
| 671 | let field = match field { |
| 672 | None | Some(b"-") => return Ok(None), |
| 673 | Some(field) => field, |
| 674 | }; |
| 675 | let i = number(field, line)?; |
| 676 | if i as usize >= len || i > u32::from(u16::MAX) { |
| 677 | return Err(format!("line {line}: clip names {what} {i} of {len}").into()); |
| 678 | } |
| 679 | Ok(Some(i as u16)) |
| 680 | } |
| 681 | |
| 682 | |
| 683 | fn band(field: &[u8], line: usize) -> crate::Result<Band> { |
| 684 | let parts: Vec<&[u8]> = field.split(|&b| b == b':').collect(); |
| 685 | if parts.len() != 4 { |
| 686 | return Err(format!("line {line}: band wants 4 fields, found {}", parts.len()).into()); |
| 687 | } |
| 688 | Ok(Band { |
| 689 | freq_hz: float(parts[0], line)?, |
| 690 | gain_db: float(parts[1], line)?, |
| 691 | q: float(parts[2], line)?, |
| 692 | kind: match parts[3] { |
| 693 | b"ls" => BandKind::LowShelf, |
| 694 | b"pk" => BandKind::Peak, |
| 695 | b"hs" => BandKind::HighShelf, |
| 696 | other => { |
| 697 | return Err(format!( |
| 698 | "line {line}: {:?} is not a band shape", |
| 699 | String::from_utf8_lossy(other) |
| 700 | ) |
| 701 | .into()); |
| 702 | } |
| 703 | }, |
| 704 | }) |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | fn float(field: &[u8], line: usize) -> crate::Result<f32> { |
| 712 | match std::str::from_utf8(field) |
| 713 | .ok() |
| 714 | .and_then(|s| s.parse::<f32>().ok()) |
| 715 | .filter(|f| f.is_finite()) |
| 716 | { |
| 717 | Some(f) => Ok(f), |
| 718 | None => Err(format!( |
| 719 | "line {line}: {:?} is not a number", |
| 720 | String::from_utf8_lossy(field) |
| 721 | ) |
| 722 | .into()), |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | fn number(field: &[u8], line: usize) -> crate::Result<u32> { |
| 727 | match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) { |
| 728 | Some(n) => Ok(n), |
| 729 | None => Err(format!( |
| 730 | "line {line}: {:?} is not a number", |
| 731 | String::from_utf8_lossy(field) |
| 732 | ) |
| 733 | .into()), |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | |
| 738 | fn escape(path: &Path, out: &mut Vec<u8>) { |
| 739 | for &b in path.as_os_str().as_bytes() { |
| 740 | match b { |
| 741 | b'%' => out.extend_from_slice(b"%25"), |
| 742 | b'\n' => out.extend_from_slice(b"%0A"), |
| 743 | _ => out.push(b), |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | |
| 750 | fn unescape(field: &[u8], line: usize) -> crate::Result<PathBuf> { |
| 751 | let mut out = Vec::with_capacity(field.len()); |
| 752 | let mut i = 0; |
| 753 | while i < field.len() { |
| 754 | if field[i] != b'%' { |
| 755 | out.push(field[i]); |
| 756 | i += 1; |
| 757 | continue; |
| 758 | } |
| 759 | let byte = field |
| 760 | .get(i + 1..i + 3) |
| 761 | .and_then(|h| std::str::from_utf8(h).ok()) |
| 762 | .and_then(|h| u8::from_str_radix(h, 16).ok()) |
| 763 | .ok_or_else(|| -> crate::Error { |
| 764 | format!("line {line}: truncated % escape in the path").into() |
| 765 | })?; |
| 766 | out.push(byte); |
| 767 | i += 3; |
| 768 | } |
| 769 | Ok(PathBuf::from(OsString::from_vec(out))) |
| 770 | } |
| 771 | |
| 772 | #[cfg(test)] |
| 773 | mod tests { |
| 774 | use super::*; |
| 775 | |
| 776 | fn clip(start: u32, in_frame: u32, out_frame: u32, source: usize, link: Option<u32>) -> Clip { |
| 777 | Clip { |
| 778 | start, |
| 779 | in_frame, |
| 780 | out_frame, |
| 781 | source, |
| 782 | link, |
| 783 | eq: None, |
| 784 | color: None, |
| 785 | fit: FitPolicy::default(), |
| 786 | speed: Speed::NORMAL, |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | |
| 791 | |
| 792 | fn flat( |
| 793 | dir: &Path, |
| 794 | sources: &[Source], |
| 795 | lanes: &[(LaneKind, Vec<Clip>)], |
| 796 | playhead: u32, |
| 797 | ) -> Vec<u8> { |
| 798 | emit(dir, sources, lanes, &[], &[], (1280, 720), playhead) |
| 799 | } |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | fn source(path: &str, audio_stream: usize) -> Source { |
| 805 | Source { |
| 806 | path: PathBuf::from(path), |
| 807 | audio_stream, |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | |
| 812 | type Lanes = Vec<(LaneKind, Vec<Clip>)>; |
| 813 | |
| 814 | |
| 815 | fn two(video: Vec<Clip>, audio: Vec<Clip>) -> Lanes { |
| 816 | vec![(LaneKind::Video, video), (LaneKind::Audio, audio)] |
| 817 | } |
| 818 | |
| 819 | fn doc() -> (PathBuf, Vec<Source>, Lanes) { |
| 820 | ( |
| 821 | PathBuf::from("/proj"), |
| 822 | vec![source("/proj/a.mp4", 0), source("/elsewhere/b.mp4", 2)], |
| 823 | two( |
| 824 | vec![clip(0, 0, 30, 0, Some(0)), clip(30, 10, 20, 1, Some(1))], |
| 825 | vec![clip(0, 0, 30, 0, Some(0))], |
| 826 | ), |
| 827 | ) |
| 828 | } |
| 829 | |
| 830 | #[test] |
| 831 | fn relative_and_absolute_paths_round_trip() { |
| 832 | let (dir, sources, lanes) = doc(); |
| 833 | let bytes = flat(&dir, &sources, &lanes, 12); |
| 834 | assert_eq!( |
| 835 | String::from_utf8_lossy(&bytes), |
| 836 | "edith 8\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 837 | source 2 /elsewhere/b.mp4\n\ |
| 838 | video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ |
| 839 | audio 1 0 0 30 0 0 - - fit 1000\n", |
| 840 | "the file under the project directory is written relative to it, \ |
| 841 | each with the audio stream it plays" |
| 842 | ); |
| 843 | let back = parse(&bytes, &dir).expect("parse"); |
| 844 | assert_eq!(back.sources, sources, "relative entries rejoin the dir"); |
| 845 | assert_eq!( |
| 846 | back.lanes, lanes, |
| 847 | "both lanes, in display order; the trailing gap needs no line" |
| 848 | ); |
| 849 | assert_eq!(back.playhead, 12); |
| 850 | |
| 851 | assert_eq!(flat(&dir, &back.sources, &back.lanes, back.playhead), bytes); |
| 852 | } |
| 853 | |
| 854 | |
| 855 | |
| 856 | #[test] |
| 857 | fn any_number_of_lanes_round_trips() { |
| 858 | let dir = PathBuf::from("/proj"); |
| 859 | let sources = vec![source("/proj/a.mp4", 0)]; |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | let lanes = vec![ |
| 865 | (LaneKind::Video, vec![clip(0, 0, 30, 0, Some(4))]), |
| 866 | (LaneKind::Audio, Vec::new()), |
| 867 | (LaneKind::Video, vec![clip(40, 0, 10, 0, None)]), |
| 868 | (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(4))]), |
| 869 | ]; |
| 870 | let bytes = flat(&dir, &sources, &lanes, 7); |
| 871 | assert_eq!( |
| 872 | String::from_utf8_lossy(&bytes), |
| 873 | "edith 8\nplayhead 7\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 874 | video 1 0 0 30 0 4 - - fit 1000\naudio 1\n\ |
| 875 | video 2 40 0 10 0 - - - fit 1000\naudio 2 0 0 30 0 4 - - fit 1000\n", |
| 876 | "an empty lane is a line of its own; everything else is its clips" |
| 877 | ); |
| 878 | let back = parse(&bytes, &dir).expect("parse"); |
| 879 | assert_eq!(back.lanes, lanes, "lane list, order and links all survive"); |
| 880 | assert_eq!(back.playhead, 7); |
| 881 | assert_eq!(flat(&dir, &back.sources, &back.lanes, back.playhead), bytes); |
| 882 | |
| 883 | |
| 884 | |
| 885 | for (file, want) in [ |
| 886 | ( |
| 887 | "edith 4\nsource 0 a.mp4\nvideo 2 0 0 30 0 -\n", |
| 888 | "line 3: lane V2 comes before V1", |
| 889 | ), |
| 890 | ( |
| 891 | "edith 4\nsource 0 a.mp4\nvideo 1 0 0 30 0 -\naudio 3\n", |
| 892 | "line 4: lane A3 comes before A1", |
| 893 | ), |
| 894 | ( |
| 895 | "edith 4\nsource 0 a.mp4\nvideo 0 0 0 30 0 -\n", |
| 896 | "line 3: lane numbers start at 1", |
| 897 | ), |
| 898 | |
| 899 | ( |
| 900 | "edith 4\nsource 0 a.mp4\nvideo 1 0 0 30 0\n", |
| 901 | "line 3: clip wants 5 fields, found 4", |
| 902 | ), |
| 903 | |
| 904 | ( |
| 905 | "edith 3\nsource 0 a.mp4\nvideo 1 0 0 30 0 -\n", |
| 906 | "line 3: clip wants 5 fields, found 6", |
| 907 | ), |
| 908 | ] { |
| 909 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 910 | } |
| 911 | |
| 912 | let empty = parse(b"edith 4\nsource 0 a.mp4\nvideo 1\naudio 1\n", &dir).expect("parse"); |
| 913 | assert!(empty.lanes.iter().all(|(_, clips)| clips.is_empty())); |
| 914 | assert_eq!(empty.lanes.len(), 2, "and its lanes survive the round trip"); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | fn band_of(freq_hz: f32, gain_db: f32, q: f32, kind: BandKind) -> Band { |
| 919 | Band { |
| 920 | freq_hz, |
| 921 | gain_db, |
| 922 | q, |
| 923 | kind, |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | #[test] |
| 932 | fn equalizers_round_trip_bit_exactly_and_are_shared() { |
| 933 | let dir = PathBuf::from("/proj"); |
| 934 | let sources = vec![source("/proj/a.mp4", 0)]; |
| 935 | let eq = vec![ |
| 936 | EqParams { |
| 937 | bands: vec![ |
| 938 | band_of(80.0, -3.0, 0.707, BandKind::LowShelf), |
| 939 | band_of(1000.0, 4.5, 1.0, BandKind::Peak), |
| 940 | ], |
| 941 | }, |
| 942 | |
| 943 | |
| 944 | |
| 945 | EqParams { |
| 946 | bands: vec![band_of( |
| 947 | 16_777_215.0, |
| 948 | -0.1, |
| 949 | f32::MIN_POSITIVE / 3.0, |
| 950 | BandKind::HighShelf, |
| 951 | )], |
| 952 | }, |
| 953 | EqParams { bands: Vec::new() }, |
| 954 | ]; |
| 955 | |
| 956 | let lanes = two( |
| 957 | vec![ |
| 958 | Clip { |
| 959 | eq: Some(0), |
| 960 | ..clip(0, 0, 30, 0, Some(0)) |
| 961 | }, |
| 962 | Clip { |
| 963 | eq: Some(1), |
| 964 | ..clip(30, 10, 20, 0, None) |
| 965 | }, |
| 966 | ], |
| 967 | vec![ |
| 968 | Clip { |
| 969 | eq: Some(0), |
| 970 | ..clip(0, 0, 30, 0, Some(0)) |
| 971 | }, |
| 972 | Clip { |
| 973 | eq: Some(2), |
| 974 | ..clip(30, 0, 10, 0, None) |
| 975 | }, |
| 976 | ], |
| 977 | ); |
| 978 | let bytes = emit(&dir, &sources, &lanes, &eq, &[], (1280, 720), 0); |
| 979 | assert_eq!( |
| 980 | String::from_utf8_lossy(&bytes), |
| 981 | "edith 8\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 982 | eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ |
| 983 | eq 16777215.0:-0.1:3.918315e-39:hs\n\ |
| 984 | eq\n\ |
| 985 | video 1 0 0 30 0 0 0 - fit 1000\nvideo 1 30 10 20 0 - 1 - fit 1000\n\ |
| 986 | audio 1 0 0 30 0 0 0 - fit 1000\naudio 1 30 0 10 0 - 2 - fit 1000\n", |
| 987 | "the table comes before the clips, and a clip names a line of it" |
| 988 | ); |
| 989 | |
| 990 | let back = parse(&bytes, &dir).expect("parse"); |
| 991 | assert_eq!(back.lanes, lanes, "every clip keeps the eq it named"); |
| 992 | for (i, (got, want)) in back.eq.iter().zip(&eq).enumerate() { |
| 993 | for (b, (got, want)) in got.bands.iter().zip(&want.bands).enumerate() { |
| 994 | assert_eq!( |
| 995 | ( |
| 996 | got.freq_hz.to_bits(), |
| 997 | got.gain_db.to_bits(), |
| 998 | got.q.to_bits(), |
| 999 | got.kind |
| 1000 | ), |
| 1001 | ( |
| 1002 | want.freq_hz.to_bits(), |
| 1003 | want.gain_db.to_bits(), |
| 1004 | want.q.to_bits(), |
| 1005 | want.kind |
| 1006 | ), |
| 1007 | "eq {i} band {b} came back as other bits" |
| 1008 | ); |
| 1009 | } |
| 1010 | } |
| 1011 | assert_eq!(back.eq.len(), 3, "the empty cascade is an entry of its own"); |
| 1012 | assert_eq!( |
| 1013 | emit( |
| 1014 | &dir, |
| 1015 | &back.sources, |
| 1016 | &back.lanes, |
| 1017 | &back.eq, |
| 1018 | &back.color, |
| 1019 | (1280, 720), |
| 1020 | 0 |
| 1021 | ), |
| 1022 | bytes |
| 1023 | ); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | #[test] |
| 1030 | fn colours_round_trip_bit_exactly_and_are_shared() { |
| 1031 | let dir = PathBuf::from("/proj"); |
| 1032 | let sources = vec![source("/proj/a.mp4", 0)]; |
| 1033 | let color = vec![ |
| 1034 | ColorParams { |
| 1035 | brightness: 0.1, |
| 1036 | contrast: 1.2, |
| 1037 | saturation: 0.9, |
| 1038 | tint: -0.3, |
| 1039 | }, |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | ColorParams { |
| 1044 | brightness: -0.000_000_1, |
| 1045 | contrast: 16_777_215.0, |
| 1046 | saturation: f32::MIN_POSITIVE / 3.0, |
| 1047 | tint: -0.0, |
| 1048 | }, |
| 1049 | ColorParams::default(), |
| 1050 | ]; |
| 1051 | |
| 1052 | let lanes = two( |
| 1053 | vec![ |
| 1054 | Clip { |
| 1055 | color: Some(0), |
| 1056 | fit: FitPolicy::default(), |
| 1057 | speed: Speed::NORMAL, |
| 1058 | ..clip(0, 0, 30, 0, Some(0)) |
| 1059 | }, |
| 1060 | Clip { |
| 1061 | color: Some(1), |
| 1062 | fit: FitPolicy::default(), |
| 1063 | speed: Speed::NORMAL, |
| 1064 | ..clip(30, 10, 20, 0, None) |
| 1065 | }, |
| 1066 | ], |
| 1067 | vec![ |
| 1068 | Clip { |
| 1069 | color: Some(0), |
| 1070 | fit: FitPolicy::default(), |
| 1071 | speed: Speed::NORMAL, |
| 1072 | ..clip(0, 0, 30, 0, Some(0)) |
| 1073 | }, |
| 1074 | Clip { |
| 1075 | color: Some(2), |
| 1076 | fit: FitPolicy::default(), |
| 1077 | speed: Speed::NORMAL, |
| 1078 | ..clip(30, 0, 10, 0, None) |
| 1079 | }, |
| 1080 | ], |
| 1081 | ); |
| 1082 | let bytes = emit(&dir, &sources, &lanes, &[], &color, (1280, 720), 0); |
| 1083 | assert_eq!( |
| 1084 | String::from_utf8_lossy(&bytes), |
| 1085 | "edith 8\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1086 | color 0.1:1.2:0.9:-0.3\n\ |
| 1087 | color -1e-7:16777215.0:3.918315e-39:-0.0\n\ |
| 1088 | color 0.0:1.0:1.0:0.0\n\ |
| 1089 | video 1 0 0 30 0 0 - 0 fit 1000\nvideo 1 30 10 20 0 - - 1 fit 1000\n\ |
| 1090 | audio 1 0 0 30 0 0 - 0 fit 1000\naudio 1 30 0 10 0 - - 2 fit 1000\n", |
| 1091 | "the table comes before the clips, and a clip names a line of it" |
| 1092 | ); |
| 1093 | |
| 1094 | let back = parse(&bytes, &dir).expect("parse"); |
| 1095 | assert_eq!(back.lanes, lanes, "every clip keeps the colour it named"); |
| 1096 | for (i, (got, want)) in back.color.iter().zip(&color).enumerate() { |
| 1097 | assert_eq!( |
| 1098 | ( |
| 1099 | got.brightness.to_bits(), |
| 1100 | got.contrast.to_bits(), |
| 1101 | got.saturation.to_bits(), |
| 1102 | got.tint.to_bits() |
| 1103 | ), |
| 1104 | ( |
| 1105 | want.brightness.to_bits(), |
| 1106 | want.contrast.to_bits(), |
| 1107 | want.saturation.to_bits(), |
| 1108 | want.tint.to_bits() |
| 1109 | ), |
| 1110 | "color {i} came back as other bits" |
| 1111 | ); |
| 1112 | } |
| 1113 | assert_eq!(back.color.len(), 3); |
| 1114 | assert_eq!( |
| 1115 | emit( |
| 1116 | &dir, |
| 1117 | &back.sources, |
| 1118 | &back.lanes, |
| 1119 | &back.eq, |
| 1120 | &back.color, |
| 1121 | (1280, 720), |
| 1122 | 0 |
| 1123 | ), |
| 1124 | bytes |
| 1125 | ); |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | #[test] |
| 1131 | fn a_v5_file_grades_nothing() { |
| 1132 | let dir = PathBuf::from("/proj"); |
| 1133 | let v5 = b"edith 5\nplayhead 3\nsource 0 a.mp4\neq 80.0:-3.0:0.707:ls\n\ |
| 1134 | video 1 0 0 30 0 0 0\naudio 1 0 0 30 0 0 -\n"; |
| 1135 | let old = parse(v5, &dir).expect("v5 parses"); |
| 1136 | assert!(old.color.is_empty(), "nothing before v6 grades anything"); |
| 1137 | assert_eq!(old.eq.len(), 1, "...and its equalizers are untouched"); |
| 1138 | assert_eq!(old.lanes[0].1[0].eq, Some(0)); |
| 1139 | assert!(old.lanes[0].1[0].color.is_none()); |
| 1140 | assert_eq!( |
| 1141 | String::from_utf8_lossy(&emit( |
| 1142 | &dir, |
| 1143 | &old.sources, |
| 1144 | &old.lanes, |
| 1145 | &old.eq, |
| 1146 | &old.color, |
| 1147 | (1280, 720), |
| 1148 | old.playhead |
| 1149 | )), |
| 1150 | "edith 8\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1151 | eq 80.0:-3.0:0.707:ls\n\ |
| 1152 | video 1 0 0 30 0 0 0 - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" |
| 1153 | ); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | #[test] |
| 1161 | fn speeds_round_trip_and_a_v7_file_plays_at_real_time() { |
| 1162 | let dir = PathBuf::from("/proj"); |
| 1163 | let sources = vec![Source::new("/proj/a.mp4", 0)]; |
| 1164 | let speeded = |start, in_frame, out_frame, permille| Clip { |
| 1165 | start, |
| 1166 | in_frame, |
| 1167 | out_frame, |
| 1168 | source: 0, |
| 1169 | link: None, |
| 1170 | eq: None, |
| 1171 | color: None, |
| 1172 | fit: FitPolicy::default(), |
| 1173 | speed: Speed::from_permille(permille), |
| 1174 | }; |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | let lanes = vec![ |
| 1179 | ( |
| 1180 | LaneKind::Video, |
| 1181 | vec![speeded(0, 0, 30, 2000), speeded(15, 30, 40, 250)], |
| 1182 | ), |
| 1183 | (LaneKind::Audio, vec![speeded(0, 0, 30, 2000)]), |
| 1184 | ]; |
| 1185 | let bytes = emit(&dir, &sources, &lanes, &[], &[], (1280, 720), 0); |
| 1186 | assert_eq!( |
| 1187 | String::from_utf8_lossy(&bytes), |
| 1188 | "edith 8\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1189 | video 1 0 0 30 0 - - - fit 2000\nvideo 1 15 30 40 0 - - - fit 250\n\ |
| 1190 | audio 1 0 0 30 0 - - - fit 2000\n", |
| 1191 | "the rate is the clip line's last field, in thousandths" |
| 1192 | ); |
| 1193 | let back = parse(&bytes, &dir).expect("parse"); |
| 1194 | assert_eq!(back.lanes, lanes, "every clip keeps the rate it named"); |
| 1195 | |
| 1196 | |
| 1197 | let v7 = b"edith 7\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1198 | video 1 0 0 30 0 0 - - fit\naudio 1 0 0 30 0 0 - - fit\n"; |
| 1199 | let old = parse(v7, &dir).expect("v7 parses"); |
| 1200 | assert!( |
| 1201 | old.lanes |
| 1202 | .iter() |
| 1203 | .flat_map(|(_, clips)| clips) |
| 1204 | .all(|c| c.speed == Speed::NORMAL), |
| 1205 | "nothing before v8 plays at anything but real time" |
| 1206 | ); |
| 1207 | assert_eq!( |
| 1208 | String::from_utf8_lossy(&emit( |
| 1209 | &dir, |
| 1210 | &old.sources, |
| 1211 | &old.lanes, |
| 1212 | &old.eq, |
| 1213 | &old.color, |
| 1214 | (1280, 720), |
| 1215 | old.playhead |
| 1216 | )), |
| 1217 | "edith 8\nplayhead 3\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1218 | video 1 0 0 30 0 0 - - fit 1000\naudio 1 0 0 30 0 0 - - fit 1000\n" |
| 1219 | ); |
| 1220 | |
| 1221 | let bad = b"edith 8\nsource 0 a.mp4\nvideo 1 0 0 30 0 - - - fit 9000\n"; |
| 1222 | assert_eq!( |
| 1223 | parse(bad, &dir).unwrap_err().to_string(), |
| 1224 | "line 3: speed 9000 is outside 250-4000 thousandths" |
| 1225 | ); |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | #[test] |
| 1230 | fn a_malformed_colour_names_its_line() { |
| 1231 | let dir = PathBuf::from("/proj"); |
| 1232 | let head = "edith 6\nsource 0 a.mp4\n"; |
| 1233 | |
| 1234 | |
| 1235 | for (file, want) in [ |
| 1236 | ( |
| 1237 | format!("{head}color 0.0:1.0:1.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 1238 | "line 3: color wants 4 fields, found 3", |
| 1239 | ), |
| 1240 | ( |
| 1241 | 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"), |
| 1242 | "line 3: color wants 1 fields, found 2", |
| 1243 | ), |
| 1244 | ( |
| 1245 | format!("{head}color 0.0:warm:1.0:0.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 1246 | "line 3: \"warm\" is not a number", |
| 1247 | ), |
| 1248 | |
| 1249 | ( |
| 1250 | format!("{head}color inf:1.0:1.0:0.0\nvideo 1 0 0 5 0 - - 0\n"), |
| 1251 | "line 3: \"inf\" is not a number", |
| 1252 | ), |
| 1253 | ( |
| 1254 | format!("{head}color 0.0:1.0:1.0:NaN\nvideo 1 0 0 5 0 - - 0\n"), |
| 1255 | "line 3: \"NaN\" is not a number", |
| 1256 | ), |
| 1257 | ( |
| 1258 | format!("{head}color 0.0:1.0:1.0:0.0\nvideo 1 0 0 5 0 - - 1\n"), |
| 1259 | "line 4: clip names color 1 of 1", |
| 1260 | ), |
| 1261 | ( |
| 1262 | format!("{head}video 1 0 0 5 0 - - 0\n"), |
| 1263 | "line 3: clip names color 0 of 0", |
| 1264 | ), |
| 1265 | |
| 1266 | |
| 1267 | ( |
| 1268 | format!("{head}video 1 0 0 5 0 - -\n"), |
| 1269 | "line 3: clip wants 7 fields, found 6", |
| 1270 | ), |
| 1271 | ( |
| 1272 | format!("{head}video 1 0 0 5 0 - - -\ncolor 0.0:1.0:1.0:0.0\n"), |
| 1273 | "line 4: color after a clip", |
| 1274 | ), |
| 1275 | |
| 1276 | ( |
| 1277 | "edith 5\nsource 0 a.mp4\ncolor 0.0:1.0:1.0:0.0\nvideo 1 0 0 5 0 - -\n".to_string(), |
| 1278 | "line 3: unknown keyword \"color\"", |
| 1279 | ), |
| 1280 | ] { |
| 1281 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | #[test] |
| 1287 | fn a_malformed_equalizer_names_its_line() { |
| 1288 | let dir = PathBuf::from("/proj"); |
| 1289 | let head = "edith 5\nsource 0 a.mp4\n"; |
| 1290 | for (file, want) in [ |
| 1291 | ( |
| 1292 | format!("{head}eq 80.0:0.0:0.707\nvideo 1 0 0 5 0 - 0\n"), |
| 1293 | "line 3: band wants 4 fields, found 3", |
| 1294 | ), |
| 1295 | ( |
| 1296 | format!("{head}eq 80.0:0.0:0.707:notch\nvideo 1 0 0 5 0 - 0\n"), |
| 1297 | "line 3: \"notch\" is not a band shape", |
| 1298 | ), |
| 1299 | ( |
| 1300 | format!("{head}eq 80.0:0.0:wide:pk\nvideo 1 0 0 5 0 - 0\n"), |
| 1301 | "line 3: \"wide\" is not a number", |
| 1302 | ), |
| 1303 | |
| 1304 | |
| 1305 | ( |
| 1306 | format!("{head}eq 80.0:inf:0.707:pk\nvideo 1 0 0 5 0 - 0\n"), |
| 1307 | "line 3: \"inf\" is not a number", |
| 1308 | ), |
| 1309 | ( |
| 1310 | format!("{head}eq NaN:0.0:0.707:pk\nvideo 1 0 0 5 0 - 0\n"), |
| 1311 | "line 3: \"NaN\" is not a number", |
| 1312 | ), |
| 1313 | ( |
| 1314 | format!("{head}eq 80.0:0.0:0.707:pk\nvideo 1 0 0 5 0 - 1\n"), |
| 1315 | "line 4: clip names eq 1 of 1", |
| 1316 | ), |
| 1317 | ( |
| 1318 | format!("{head}video 1 0 0 5 0 - 0\n"), |
| 1319 | "line 3: clip names eq 0 of 0", |
| 1320 | ), |
| 1321 | |
| 1322 | |
| 1323 | ( |
| 1324 | format!("{head}video 1 0 0 5 0 -\n"), |
| 1325 | "line 3: clip wants 6 fields, found 5", |
| 1326 | ), |
| 1327 | ( |
| 1328 | format!("{head}video 1 0 0 5 0 - -\neq 80.0:0.0:0.707:pk\n"), |
| 1329 | "line 4: eq after a clip", |
| 1330 | ), |
| 1331 | |
| 1332 | ( |
| 1333 | "edith 4\nsource 0 a.mp4\neq 80.0:0.0:0.707:pk\nvideo 1 0 0 5 0 -\n".to_string(), |
| 1334 | "line 3: unknown keyword \"eq\"", |
| 1335 | ), |
| 1336 | ] { |
| 1337 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | #[test] |
| 1345 | fn a_v4_file_equalizes_nothing() { |
| 1346 | let dir = PathBuf::from("/proj"); |
| 1347 | let (_, sources, lanes) = doc(); |
| 1348 | let v4 = b"edith 4\nplayhead 12\nsource 0 a.mp4\nsource 2 /elsewhere/b.mp4\n\ |
| 1349 | video 1 0 0 30 0 0\nvideo 1 30 10 20 1 1\naudio 1 0 0 30 0 0\n"; |
| 1350 | let old = parse(v4, &dir).expect("v4 parses"); |
| 1351 | assert_eq!( |
| 1352 | (&old.sources, &old.lanes, old.playhead), |
| 1353 | (&sources, &lanes, 12) |
| 1354 | ); |
| 1355 | assert!(old.eq.is_empty(), "nothing before v5 equalizes anything"); |
| 1356 | assert_eq!( |
| 1357 | String::from_utf8_lossy(&flat(&dir, &old.sources, &old.lanes, old.playhead)), |
| 1358 | "edith 8\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1359 | source 2 /elsewhere/b.mp4\n\ |
| 1360 | video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ |
| 1361 | audio 1 0 0 30 0 0 - - fit 1000\n" |
| 1362 | ); |
| 1363 | |
| 1364 | let empty = parse( |
| 1365 | b"edith 5\nsource 0 a.mp4\nvideo 1 0 0 5 0 - -\naudio 1\n", |
| 1366 | &dir, |
| 1367 | ) |
| 1368 | .expect("parse"); |
| 1369 | assert!(empty.lanes[1].1.is_empty()); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | #[test] |
| 1377 | fn a_v3_file_is_two_lanes_and_a_v2_one_is_stream_0() { |
| 1378 | let dir = PathBuf::from("/proj"); |
| 1379 | let (_, _, lanes) = doc(); |
| 1380 | let v3 = b"edith 3\nplayhead 12\nsource 0 a.mp4\nsource 2 /elsewhere/b.mp4\n\ |
| 1381 | video 0 0 30 0 0\nvideo 30 10 20 1 1\naudio 0 0 30 0 0\n"; |
| 1382 | let old = parse(v3, &dir).expect("v3 parses"); |
| 1383 | assert_eq!( |
| 1384 | (&old.sources, &old.lanes, old.playhead), |
| 1385 | ( |
| 1386 | &vec![source("/proj/a.mp4", 0), source("/elsewhere/b.mp4", 2)], |
| 1387 | &lanes, |
| 1388 | 12 |
| 1389 | ), |
| 1390 | "a v3 file loads as exactly the state its v5 twin does" |
| 1391 | ); |
| 1392 | |
| 1393 | |
| 1394 | assert_eq!( |
| 1395 | flat(&dir, &old.sources, &old.lanes, old.playhead), |
| 1396 | flat(&dir, &old.sources, &lanes, 12), |
| 1397 | ); |
| 1398 | |
| 1399 | let v2 = b"edith 2\nplayhead 12\nsource a.mp4\nsource /elsewhere/b.mp4\n\ |
| 1400 | video 0 0 30 0 0\nvideo 30 10 20 1 1\naudio 0 0 30 0 0\n"; |
| 1401 | let back = parse(v2, &dir).expect("v2 parses"); |
| 1402 | assert_eq!( |
| 1403 | back.sources, |
| 1404 | vec![source("/proj/a.mp4", 0), source("/elsewhere/b.mp4", 0)], |
| 1405 | ); |
| 1406 | assert_eq!(back.lanes, lanes); |
| 1407 | |
| 1408 | let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); |
| 1409 | assert_eq!( |
| 1410 | String::from_utf8_lossy(&v5), |
| 1411 | "edith 8\nplayhead 12\nresolution 1280 720\nsource 0 a.mp4\n\ |
| 1412 | source 0 /elsewhere/b.mp4\n\ |
| 1413 | video 1 0 0 30 0 0 - - fit 1000\nvideo 1 30 10 20 1 1 - - fit 1000\n\ |
| 1414 | audio 1 0 0 30 0 0 - - fit 1000\n", |
| 1415 | "a re-saved v2 project differs only by its version, the lane \ |
| 1416 | numbers, the streams it always meant and an equalizer it has none of" |
| 1417 | ); |
| 1418 | let again = parse(&v5, &dir).expect("v5 parses"); |
| 1419 | assert_eq!(again.sources, back.sources); |
| 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | #[test] |
| 1425 | fn a_v1_file_loads_as_two_grouped_lanes() { |
| 1426 | let dir = PathBuf::from("/proj"); |
| 1427 | let v1 = b"edith 1\nplayhead 5\nsource a.mp4\nclip 0 30 0\nclip 40 60 0\n"; |
| 1428 | let back = parse(v1, &dir).expect("v1 parses"); |
| 1429 | assert_eq!( |
| 1430 | back.lanes[0].1, |
| 1431 | vec![clip(0, 0, 30, 0, Some(0)), clip(30, 40, 60, 0, Some(1))], |
| 1432 | "v1 clips queue up: the second starts where the first ended" |
| 1433 | ); |
| 1434 | assert_eq!( |
| 1435 | back.lanes, |
| 1436 | two(back.lanes[0].1.clone(), back.lanes[0].1.clone()), |
| 1437 | "one take per clip, on both lanes" |
| 1438 | ); |
| 1439 | assert_eq!(back.playhead, 5); |
| 1440 | |
| 1441 | |
| 1442 | let v5 = flat(&dir, &back.sources, &back.lanes, back.playhead); |
| 1443 | assert!(v5.starts_with(b"edith 8\n")); |
| 1444 | let again = parse(&v5, &dir).expect("v5 parses"); |
| 1445 | assert_eq!(again.lanes, back.lanes); |
| 1446 | |
| 1447 | for (bytes, want) in [ |
| 1448 | ( |
| 1449 | &b"edith 1\nsource a.mp4\nvideo 0 0 30 0 -\n"[..], |
| 1450 | "line 3: unknown keyword \"video\"", |
| 1451 | ), |
| 1452 | ( |
| 1453 | b"edith 2\nsource a.mp4\nclip 0 30 0\n", |
| 1454 | "line 3: unknown keyword \"clip\"", |
| 1455 | ), |
| 1456 | ] { |
| 1457 | assert_eq!(parse(bytes, &dir).unwrap_err().to_string(), want); |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | |
| 1463 | #[test] |
| 1464 | fn a_lane_out_of_order_or_overlapping_is_refused() { |
| 1465 | let dir = PathBuf::from("/proj"); |
| 1466 | for bytes in [ |
| 1467 | &b"edith 2\nsource a.mp4\nvideo 30 0 30 0 -\nvideo 0 0 30 0 -\n"[..], |
| 1468 | b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n", |
| 1469 | ] { |
| 1470 | let err = parse(bytes, &dir).unwrap_err().to_string(); |
| 1471 | assert!(err.starts_with("line 4: clip at "), "{err}"); |
| 1472 | } |
| 1473 | |
| 1474 | assert!( |
| 1475 | parse( |
| 1476 | b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\naudio 0 0 30 0 -\n", |
| 1477 | &dir |
| 1478 | ) |
| 1479 | .is_ok() |
| 1480 | ); |
| 1481 | |
| 1482 | let only_audio = parse(b"edith 2\nsource a.mp4\naudio 0 0 30 0 -\n", &dir).expect("parse"); |
| 1483 | assert!(only_audio.lanes[0].1.is_empty(), "V1 is there and empty"); |
| 1484 | } |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | #[test] |
| 1490 | fn a_clip_whose_end_overflows_is_refused() { |
| 1491 | let dir = PathBuf::from("/proj"); |
| 1492 | for bytes in [ |
| 1493 | &b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\naudio 0 0 30 0 -\n"[..], |
| 1494 | |
| 1495 | b"edith 2\nsource a.mp4\nvideo 4294967290 0 30 0 -\nvideo 4294967295 0 30 0 -\n", |
| 1496 | |
| 1497 | b"edith 1\nsource a.mp4\nclip 0 4294967295 0\nclip 0 4294967295 0\n", |
| 1498 | ] { |
| 1499 | let err = parse(bytes, &dir).unwrap_err().to_string(); |
| 1500 | assert!( |
| 1501 | err.contains("runs past the last frame there is"), |
| 1502 | "not refused by name: {err}" |
| 1503 | ); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | #[test] |
| 1512 | fn a_non_canonical_project_path_still_writes_relative_sources() { |
| 1513 | assert_eq!( |
| 1514 | project_dir(Path::new("a.edith")), |
| 1515 | std::env::current_dir().expect("cwd"), |
| 1516 | "a bare filename is saved into the working directory" |
| 1517 | ); |
| 1518 | |
| 1519 | let dir = std::env::temp_dir().join(format!("ve_edith_link_{}", std::process::id())); |
| 1520 | let _ = std::fs::remove_dir_all(&dir); |
| 1521 | std::fs::create_dir_all(dir.join("real")).expect("scratch dir"); |
| 1522 | let dir = std::fs::canonicalize(&dir).expect("canonical scratch dir"); |
| 1523 | std::os::unix::fs::symlink(dir.join("real"), dir.join("link")).expect("symlink"); |
| 1524 | let source = dir.join("real/a.mp4"); |
| 1525 | let path = dir.join("link/p.edith"); |
| 1526 | |
| 1527 | let one = [clip(0, 0, 30, 0, None)]; |
| 1528 | let entry = Source { |
| 1529 | path: source, |
| 1530 | audio_stream: 1, |
| 1531 | }; |
| 1532 | save( |
| 1533 | &path, |
| 1534 | &[entry], |
| 1535 | &two(one.to_vec(), one.to_vec()), |
| 1536 | &[], |
| 1537 | &[], |
| 1538 | (1280, 720), |
| 1539 | 0, |
| 1540 | ) |
| 1541 | .expect("save"); |
| 1542 | let bytes = std::fs::read(&path).expect("read back"); |
| 1543 | assert_eq!( |
| 1544 | String::from_utf8_lossy(&bytes), |
| 1545 | "edith 8\nplayhead 0\nresolution 1280 720\nsource 1 a.mp4\n\ |
| 1546 | video 1 0 0 30 0 - - - fit 1000\naudio 1 0 0 30 0 - - - fit 1000\n" |
| 1547 | ); |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 | assert_eq!( |
| 1552 | load(&path).expect("load").sources, |
| 1553 | vec![Source { |
| 1554 | path: dir.join("link/a.mp4"), |
| 1555 | audio_stream: 1, |
| 1556 | }] |
| 1557 | ); |
| 1558 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 1559 | } |
| 1560 | |
| 1561 | #[test] |
| 1562 | fn escapes_survive_the_bytes_json_would_not() { |
| 1563 | let dir = PathBuf::from("/proj"); |
| 1564 | |
| 1565 | let nasty = Source { |
| 1566 | path: PathBuf::from(OsString::from_vec(b"/proj/we\nird 100%\xff.mp4".to_vec())), |
| 1567 | audio_stream: 0, |
| 1568 | }; |
| 1569 | let bytes = flat( |
| 1570 | &dir, |
| 1571 | &[nasty.clone()], |
| 1572 | &two(vec![clip(0, 0, 5, 0, None)], Vec::new()), |
| 1573 | 0, |
| 1574 | ); |
| 1575 | assert_eq!( |
| 1576 | bytes.iter().filter(|&&b| b == b'\n').count(), |
| 1577 | 6, |
| 1578 | "the escaped newline must not become a line break: magic, playhead, \ |
| 1579 | resolution, source, the clip, and the empty audio lane's own line" |
| 1580 | ); |
| 1581 | let back = parse(&bytes, &dir).expect("parse"); |
| 1582 | |
| 1583 | |
| 1584 | assert_eq!(back.sources, vec![nasty]); |
| 1585 | } |
| 1586 | |
| 1587 | #[test] |
| 1588 | fn a_wrong_first_line_is_refused_by_name() { |
| 1589 | let dir = PathBuf::from("/proj"); |
| 1590 | let err = parse(b"edith 9\nsource 0 a.mp4\nvideo 0 0 5 0 -\n", &dir) |
| 1591 | .unwrap_err() |
| 1592 | .to_string(); |
| 1593 | assert_eq!(err, "line 1: unsupported version 9"); |
| 1594 | for junk in [&b""[..], b"{}\n", b"source a.mp4\n"] { |
| 1595 | assert_eq!( |
| 1596 | parse(junk, &dir).unwrap_err().to_string(), |
| 1597 | "line 1: not a edith file" |
| 1598 | ); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | #[test] |
| 1603 | fn malformed_lines_name_their_line_number() { |
| 1604 | let dir = PathBuf::from("/proj"); |
| 1605 | let cases: [(&[u8], &str); 10] = [ |
| 1606 | |
| 1607 | ( |
| 1608 | b"edith 3\nsource a.mp4\nvideo 0 0 5 0 -\n", |
| 1609 | "line 2: \"a.mp4\" is not a number", |
| 1610 | ), |
| 1611 | ( |
| 1612 | b"edith 3\nsource 0\nvideo 0 0 5 0 -\n", |
| 1613 | "line 2: source without a path", |
| 1614 | ), |
| 1615 | ( |
| 1616 | b"edith 1\nsource a.mp4\nclip 0 5\n", |
| 1617 | "line 3: clip wants 3 fields, found 2", |
| 1618 | ), |
| 1619 | ( |
| 1620 | b"edith 1\nsource a.mp4\nclip 0 five 0\n", |
| 1621 | "line 3: \"five\" is not a number", |
| 1622 | ), |
| 1623 | ( |
| 1624 | b"edith 1\nsource a.mp4\nclip 5 5 0\n", |
| 1625 | "line 3: clip [5, 5) is empty", |
| 1626 | ), |
| 1627 | ( |
| 1628 | b"edith 1\nsource a.mp4\nclip 7 5 0\n", |
| 1629 | "line 3: clip [7, 5) is empty", |
| 1630 | ), |
| 1631 | ( |
| 1632 | b"edith 1\nsource a.mp4\nclip 0 5 1\n", |
| 1633 | "line 3: clip names source 1 of 1", |
| 1634 | ), |
| 1635 | ( |
| 1636 | b"edith 1\nsource a.mp4\nclip 0 5 0\nsource b.mp4\n", |
| 1637 | "line 4: source after a clip", |
| 1638 | ), |
| 1639 | ( |
| 1640 | b"edith 1\nsource a.mp4\n\nclip 0 5 0\n", |
| 1641 | "line 3: unknown keyword \"\"", |
| 1642 | ), |
| 1643 | ( |
| 1644 | b"edith 1\nsource a.mp4\nplayhead 3\nclip 0 5 0\n", |
| 1645 | "line 3: playhead belongs once, before the sources", |
| 1646 | ), |
| 1647 | ]; |
| 1648 | for (bytes, want) in cases { |
| 1649 | assert_eq!(parse(bytes, &dir).unwrap_err().to_string(), want); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | |
| 1657 | #[test] |
| 1658 | fn a_project_without_clips_round_trips() { |
| 1659 | let dir = PathBuf::from("/proj"); |
| 1660 | let source = Source { |
| 1661 | path: PathBuf::from("/proj/a.mp4"), |
| 1662 | audio_stream: 0, |
| 1663 | }; |
| 1664 | let bytes = flat(&dir, &[source.clone()], &two(Vec::new(), Vec::new()), 0); |
| 1665 | let back = parse(&bytes, &dir).expect("an emptied timeline is a project"); |
| 1666 | assert_eq!(back.sources, vec![source]); |
| 1667 | assert_eq!(back.lanes, two(Vec::new(), Vec::new())); |
| 1668 | assert_eq!(back.playhead, 0); |
| 1669 | |
| 1670 | assert_eq!( |
| 1671 | parse(b"edith 1\nsource a.mp4\n", &dir).expect("v1").lanes, |
| 1672 | two(Vec::new(), Vec::new()) |
| 1673 | ); |
| 1674 | assert!(parse(b"edith 1\n", &dir).expect("a project of nothing").sources.is_empty()); |
| 1675 | } |
| 1676 | |
| 1677 | |
| 1678 | #[test] |
| 1679 | fn truncations_never_panic() { |
| 1680 | let (dir, sources, lanes) = doc(); |
| 1681 | let bytes = flat(&dir, &sources, &lanes, 12); |
| 1682 | for cut in 0..bytes.len() { |
| 1683 | let _ = parse(&bytes[..cut], &dir); |
| 1684 | |
| 1685 | |
| 1686 | let _ = parse(&bytes[cut..], &dir); |
| 1687 | } |
| 1688 | |
| 1689 | assert_eq!( |
| 1690 | parse(b"edith 1\nsource a%\nclip 0 5 0\n", &dir) |
| 1691 | .unwrap_err() |
| 1692 | .to_string(), |
| 1693 | "line 2: truncated % escape in the path" |
| 1694 | ); |
| 1695 | } |
| 1696 | } |