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