edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBf2bee6bTahinli2 months ago781 commits
| 1 | //! Subtitles: a list of cues with a time on them, out of a file beside the |
| 2 | //! media (`.srt`, `.vtt`, `.ass`) or out of a track inside it. |
| 3 | //! |
| 4 | //! One model for both, because a timeline cannot care which it was: a |
| 5 | //! [`SubtitleTrack`] is where it came from -- a path, and the Matroska track |
| 6 | //! number when it is one of several inside that path -- plus the [`Cue`]s. That |
| 7 | //! pair is also all a `.edith` writes ([`crate::edith`]): the cues are read |
| 8 | //! back out of the file on the way in, exactly as a clip's *pictures* are, so a |
| 9 | //! project file stays an edit list and not a copy of the subtitles. |
| 10 | //! |
| 11 | //! Parsing is `oxideav-subtitle` (SRT, WebVTT) and `oxideav-ass` (ASS/SSA) -- |
| 12 | //! the markup, the override tags and the encoding quirks of a dozen dialects |
| 13 | //! are not this project's to re-derive. What comes back here is plain text: the |
| 14 | //! renderer is a later thing, and a cue nobody can draw yet is still a cue that |
| 15 | //! has to be listed, timed and saved. |
| 16 | //! |
| 17 | //! A cue is not always words. `S_HDMV/PGS` off a BluRay remux is *pictures* -- |
| 18 | //! run-length bitmaps the disc composed against its own frame -- and one of |
| 19 | //! those is a [`Cue`] with a [`CueImage`] on it and no text. The decoding is |
| 20 | //! `oxideav-sub-image`, for the reason above. Everything downstream carries the |
| 21 | //! picture the way it carries the string: [`crate::export::timeline_cues`] maps |
| 22 | //! it onto the timeline, a front-end draws it over the film. |
| 23 | //! |
| 24 | //! What is still *refused* is refused by name rather than by omission |
| 25 | //! ([`SubtitleTrack::refused`]): VobSub off a DVD is a picture format nothing |
| 26 | //! here reads, and a file carrying one opens with a row that says so instead of |
| 27 | //! opening with an empty list. |
| 28 | |
| 29 | use HashMap; |
| 30 | use ; |
| 31 | use Arc; |
| 32 | |
| 33 | use plain_text; |
| 34 | |
| 35 | /// One line of subtitle: on screen from `start_us` to `end_us`, microseconds |
| 36 | /// from the start of the media, which is the unit every parser here speaks. |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | /// A cue that is a picture: one PGS display set, kept the way the Matroska |
| 60 | /// block held it -- run-length and palettised, tens of kilobytes -- and turned |
| 61 | /// into pixels only when something is about to draw it ([`Self::rgba`]). |
| 62 | /// |
| 63 | /// Kept encoded because decoded is enormous: this film's canvas is 8 MB of |
| 64 | /// RGBA and its four PGS tracks carry about eleven thousand display sets |
| 65 | /// between them, so a track that decoded itself at import would cost gigabytes |
| 66 | /// where it now costs the thirty megabytes the file already spends on it. |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | /// The segments of a display set as a Matroska block holds them: a type byte, a |
| 129 | /// big-endian size, that many bytes of body, and again until the block is out. |
| 130 | /// `None` for bytes that do not walk -- a truncated or mislabelled block. |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | /// A subtitle track: where it came from and what it says. |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | /// The one door a saved subtitle line comes back in through: a standalone file |
| 218 | /// (`track` is `None`) or track `n` of a media file, Matroska or mp4 |
| 219 | /// ([`of_media`]). |
| 220 | /// |
| 221 | /// Never an error, whatever is wrong: a deleted `.srt`, a media file that is |
| 222 | /// not there any more, a track number the file no longer has, a codec of |
| 223 | /// pictures. All of them come back [`refused`](SubtitleTrack::refused) by name, |
| 224 | /// because a project that opened yesterday has to open today -- a subtitle is |
| 225 | /// not what a whole timeline should refuse over. |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | /// Every saved subtitle line of a project at once, back in the order they were |
| 232 | /// saved in -- which is the order they are picked by, so it is the row order |
| 233 | /// this hands back whatever it had to read. |
| 234 | /// |
| 235 | /// The reason this exists beside [`open`]: a Matroska file is walked whole to |
| 236 | /// reach *one* of its tracks, and a film whose project names thirty-five of them |
| 237 | /// was thirty-five walks of a 25 GB file -- seven seconds with the window |
| 238 | /// stopped, since a project opens on the UI thread. Each distinct file is walked |
| 239 | /// once here and every row that named it is picked out of that one walk. |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | /// A standalone subtitle file, parsed by its extension. |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | /// Every subtitle track *inside* a media file, whichever container it is: a |
| 304 | /// Matroska's ([`of_matroska`]) or an mp4's timed text ([`of_mp4`]). |
| 305 | /// |
| 306 | /// The one door for it, so a project row, an import and an export's read-back |
| 307 | /// cannot disagree about what a file carries -- and so an mp4 this project |
| 308 | /// wrote comes back into it, which is the whole reason the mp4 reader exists: |
| 309 | /// mp4 is the default export format, and a file edith writes and cannot open |
| 310 | /// is not a round trip. |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | /// Every timed-text (`tx3g`) subtitle track of an mp4, in track order, with its |
| 319 | /// cues -- the mp4 twin of [`of_matroska`]. |
| 320 | /// |
| 321 | /// A `tx3g` track is *continuous*: it states one sample per instant of the film |
| 322 | /// and the stretches with nothing on screen are samples of no text, so those are |
| 323 | /// dropped here rather than kept as cues with an empty line |
| 324 | /// ([`crate::demux::Mp4TextSample::payload`]). What is left is the cues, timed |
| 325 | /// on the file's own clock, and a track whose samples are all empty comes back |
| 326 | /// with none rather than with one per instant. |
| 327 | /// |
| 328 | /// Never a bitmap: an mp4 carries no PGS track this reads, so the refusal |
| 329 | /// [`of_matroska`] has for one has no counterpart here. |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | /// One `tx3g` sample as a cue, or `None` for the sample that is not one: the |
| 347 | /// empty one that says nothing is on screen between here and the next line, and |
| 348 | /// the truncated one whose declared length runs past its own bytes. |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | /// What a list row shows for a track a container declares: the language, the |
| 364 | /// muxer's title, or the pair. |
| 365 | /// |
| 366 | /// A row shows one string, so the two are flattened for it -- and *only* for it. |
| 367 | /// Both are kept on the track beside this |
| 368 | /// ([`SubtitleTrack::language`], [`SubtitleTrack::name`]), so an export writes |
| 369 | /// the container's own two fields rather than guessing them back out of this |
| 370 | /// string, which is what used to leave every French track marked English. |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | /// Every subtitle track of a Matroska file, in file order -- the text ones with |
| 380 | /// their cues, the bitmap ones refused by name (see the module docs). |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | /// The cues of one Matroska subtitle track, or why it has none. |
| 405 | /// |
| 406 | /// A Matroska text track carries the *timing* on the block and the *text* in |
| 407 | /// it, so neither codec is parsed as its own file would be: `S_TEXT/UTF8` is |
| 408 | /// one SRT cue's body per block, `S_TEXT/ASS` is one `Dialogue` line per block |
| 409 | /// with the `Start` and `End` fields cut out and the `[Script Info]` header |
| 410 | /// filed away in `CodecPrivate`. Both are put back into the shape their parser |
| 411 | /// reads -- rather than the markup being re-derived here -- which is also what |
| 412 | /// makes an embedded track and the standalone file it was muxed from come back |
| 413 | /// as the same cues. |
| 414 | /// |
| 415 | /// A PGS track is neither: its blocks are pictures, and they are moved out of |
| 416 | /// `track` rather than copied ([`pgs_cues`]) -- thirty megabytes a track is not |
| 417 | /// a thing to hold twice. |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | /// The cues of an `S_HDMV/PGS` track: one display set per block, and a display |
| 447 | /// set is a picture the disc composes onto its own canvas. |
| 448 | /// |
| 449 | /// Only the blocks that compose *something* are cues. The other half of them |
| 450 | /// are the disc's "take it off again" -- a composition with no object on it -- |
| 451 | /// and they are what says when the picture before them goes away, which is why |
| 452 | /// they are walked and not skipped. A muxer writes `BlockDuration` zero for all |
| 453 | /// of them, so [`end_of`] is what pairs the two. |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | /// The canvas of a display set that paints something -- its `PCS` says how big |
| 478 | /// the disc's picture is, and how many objects are composed onto it. |
| 479 | /// |
| 480 | /// `None` when nothing is composed, which is the disc clearing the screen, and |
| 481 | /// `None` for bytes that are not a display set at all. |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | /// When a cue goes away: its `BlockDuration`, or -- for the muxer that writes |
| 495 | /// none, and for the PGS block that writes a zero, which says the same thing -- |
| 496 | /// when the next one arrives, and [`NO_DURATION_US`] for the last. |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | /// The text of one `S_TEXT/UTF8` block with its markup resolved: through the |
| 513 | /// SRT parser, around a timing line this then throws away, because SRT markup |
| 514 | /// is exactly what such a block holds and a second implementation of it here |
| 515 | /// would be a second set of bugs. |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | /// The cues of an `S_TEXT/ASS` track: the script header out of `CodecPrivate` |
| 531 | /// with every block put back as a `Dialogue` line under it, parsed once as the |
| 532 | /// `.ass` file it came from. |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | /// Microseconds as ASS writes a time: `H:MM:SS.cc`. |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | /// A standalone subtitle file, by extension: the formats this engine reads at |
| 595 | /// all. Anything else is refused by name, as an unknown media file is. |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 |