edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiB64e09f9Tahinli3 months ago453 commits
| 1 | //! Small stand-ins for big files: a half-size, every-frame-a-keyframe H.264 |
| 2 | //! copy of a source, kept in the user's cache and played instead of the |
| 3 | //! original while the cut is being made. |
| 4 | //! |
| 5 | //! Why they exist at all: a 4K HEVC film decodes at a fraction of realtime on |
| 6 | //! this machine, so scrubbing it is a slideshow and every seek is a wait. A |
| 7 | //! proxy is the same film at 1280 on its long edge, coded so that **every frame |
| 8 | //! is a random-access point** -- which is what makes a seek land on the frame |
| 9 | //! that was asked for instead of on the nearest one a decoder can start from, |
| 10 | //! and what takes the whole open-GOP class of seek faults off the table by |
| 11 | //! construction. |
| 12 | //! |
| 13 | //! Three rules, and they are the whole contract: |
| 14 | //! |
| 15 | //! - **Picture only.** No sound is coded into a proxy. The mix always comes off |
| 16 | //! the original file ([`crate::PlaybackSession`] opens audio by its own path, |
| 17 | //! which this module never touches), so nothing a listener hears has been |
| 18 | //! through a second encoder. |
| 19 | //! - **Never exported.** [`crate::export`] reads the project's own sources and |
| 20 | //! knows nothing of this module. Delivery is the original, always. |
| 21 | //! - **Derived, never authoritative.** A proxy is named after the source's |
| 22 | //! path, length and modification time ([`path_for`]), so a re-encoded or |
| 23 | //! re-cut original simply misses the cache and a stale proxy can never be |
| 24 | //! found. Deleting the cache directory costs time and nothing else -- which |
| 25 | //! is what lets this cap itself: over [`CACHE_CAP`] the least recently used |
| 26 | //! stand-ins are deleted to make room ([`sweep`]), because a file written |
| 27 | //! without being asked for must not be able to fill a disk. |
| 28 | //! |
| 29 | //! Generation is [`crate::export`]'s own decode -> scale -> encode walk, given |
| 30 | //! a one-clip project and a smaller canvas: the hardware seat where the GPU has |
| 31 | //! one, the software encoder where it does not, the same progress and the same |
| 32 | //! cancel a file export has ([`Job`]). |
| 33 | |
| 34 | use ; |
| 35 | use UNIX_EPOCH; |
| 36 | |
| 37 | use crate; |
| 38 | use crate; |
| 39 | use crate; |
| 40 | use crateFitPolicy; |
| 41 | |
| 42 | /// The long edge of a proxy picture, in samples: 1280, so a 4K source loses |
| 43 | /// nine tenths of its samples and a 1080p one a little over half. Chosen as |
| 44 | /// what a cut can be judged on at editing size, in the neighbourhood of every |
| 45 | /// shipped proxy preset (Resolve's half-res, kdenlive's 640/1000-wide |
| 46 | /// defaults). |
| 47 | pub const LONG_EDGE: u32 = 1280; |
| 48 | |
| 49 | /// How much disk the stand-ins may hold between them, in bytes: 20 GB, which |
| 50 | /// is one 4K feature film and most of a second (a 2h20 one measures |
| 51 | /// ~11.5 GB at ~1.37 MB per second of film), or a dozen of an ordinary length. |
| 52 | /// |
| 53 | /// A cap is not a nicety here. Proxies are written *without being asked for*, |
| 54 | /// on import, [`AT_ONCE`] at a time -- so without one the answer to "how much |
| 55 | /// of the disk does this take" is "all of it, eventually". Over the cap, the |
| 56 | /// least recently used are deleted until it fits ([`sweep`]). |
| 57 | /// |
| 58 | /// **What this really bounds.** A sweep runs before each new stand-in and |
| 59 | /// leaves room for the ones about to be written ([`reserve`]), so what is |
| 60 | /// *finished* is held to `CACHE_CAP - reserve` and the encodes in flight have |
| 61 | /// the reserve to grow into. The directory therefore sits at about the cap |
| 62 | /// rather than at the cap plus a film per encode, which is what it did when the |
| 63 | /// sweep aimed at the bare number. It can still pass the cap where an encode |
| 64 | /// outruns its own estimate -- a stand-in cannot be deleted while it is being |
| 65 | /// written -- and the next sweep takes that back. |
| 66 | pub const CACHE_CAP: u64 = 20 * 1024 * 1024 * 1024; |
| 67 | |
| 68 | /// How many stand-ins may be made at once. Two, not one per core: each is a |
| 69 | /// whole film through a decoder and an encoder, and the hardware seat they |
| 70 | /// queue on is a single one -- more of them at once is the same work done |
| 71 | /// later, with the editor slower while it happens. |
| 72 | /// |
| 73 | /// It lives here rather than in the front-end that does the fanning out because |
| 74 | /// the cache's own arithmetic needs it ([`reserve`]): the number of encodes in |
| 75 | /// flight is exactly how much room the sweep has to leave, and two places |
| 76 | /// holding that number separately is one place for them to disagree. |
| 77 | pub const AT_ONCE: usize = 2; |
| 78 | |
| 79 | /// What the hardware seat really writes against what it was asked for: 1.25, |
| 80 | /// measured 2026-08-13 (8.3 Mbit/s out of a 6.6 Mbit/s request on radeonsi). |
| 81 | /// Folded into [`reserve`] so the room left for an encode is the room it will |
| 82 | /// actually take, not the room it was told to. |
| 83 | const RATE_OVERSHOOT: f64 = 1.25; |
| 84 | |
| 85 | /// Bits per pixel per second a proxy is coded at, [`crate::export`]'s own rate |
| 86 | /// rule at three times the number: every frame being an IDR means no frame |
| 87 | /// borrows anything from its neighbours, so the same picture at the inter rate |
| 88 | /// would be mush -- and mush is not something a cut can be judged on. |
| 89 | const BITS_PER_PIXEL: f64 = 0.3; |
| 90 | |
| 91 | /// Whether this is a file worth standing in for: bigger than 1080p, or coded in |
| 92 | /// something this machine decodes slowly even at 1080p. H.264 at or under 1080p |
| 93 | /// plays natively and a proxy of it would cost time and buy nothing. |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | /// The picture a proxy of `meta` is coded at: aspect kept, long edge at most |
| 99 | /// [`LONG_EDGE`], both axes even because 4:2:0 has no half chroma samples. A |
| 100 | /// source already that small is coded at its own size (rounded down to even), |
| 101 | /// which is what makes [`generate`] safe to ask for on anything at all. |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | /// Where the proxy of `source` lives, whether or not it has been made yet. |
| 113 | /// |
| 114 | /// The name is one hash of the source's canonical path, its length **and** its |
| 115 | /// modification time, so an original that changed at all is a different name |
| 116 | /// and its old proxy is simply never looked at again -- the staleness check is |
| 117 | /// the lookup. `None` where the file cannot be stat'd or the machine has no |
| 118 | /// cache directory at all, which is a source that gets no proxy rather than one |
| 119 | /// littering the working directory. |
| 120 | /// |
| 121 | /// A re-encoded source therefore leaves its old stand-in behind, and a proxy of |
| 122 | /// a feature film is gigabytes: [`sweep`] is what keeps that from being the |
| 123 | /// whole disk, and it runs before every new one is written. |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | /// How much of [`CACHE_CAP`] the sweep leaves free for the stand-ins that are |
| 144 | /// about to be written into the directory: [`AT_ONCE`] of them, each about the |
| 145 | /// size of the one being started now. |
| 146 | /// |
| 147 | /// The estimate is the file's own arithmetic and not a guess: the picture is |
| 148 | /// coded at [`BITS_PER_PIXEL`] bits per pixel per second and the frame rate |
| 149 | /// cancels out, so it is `width * height * frames * bpp / 8` -- times |
| 150 | /// [`RATE_OVERSHOOT`], because the hardware seat writes over the rate it is |
| 151 | /// asked for. |
| 152 | /// |
| 153 | /// Held to half the cap, whatever that comes to. A single 4K feature estimates |
| 154 | /// at over the whole cap, and reserving that would empty the cache to make room |
| 155 | /// for one film -- deleting stand-ins that are in use, for a bound that cannot |
| 156 | /// be met anyway while two of them are being written. Half is the compromise: |
| 157 | /// what is *finished* stays under half the cap, so the two in flight have the |
| 158 | /// other half to grow into before the disk carries more than [`CACHE_CAP`]. |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | /// Deletes least-recently-used stand-ins until the directory holds at most |
| 167 | /// `cap` bytes, and reports what it holds afterwards. |
| 168 | /// |
| 169 | /// Run before a new one is written, which is the only moment this directory |
| 170 | /// grows. Nothing else evicts, and nothing else can: a proxy is derived, so |
| 171 | /// deleting one costs the time to make it again and no more -- which is exactly |
| 172 | /// what makes a cap safe to apply behind the user's back where an eviction of |
| 173 | /// anything *authored* would not be. |
| 174 | /// |
| 175 | /// Least recently *used* is read off the access time, with the modification |
| 176 | /// time where a filesystem reports none. On a `relatime` mount -- which is the |
| 177 | /// default, and is what this machine's `/home` is -- an access time moves at |
| 178 | /// most once a day, so this is a daily-granularity LRU rather than an exact |
| 179 | /// one. That is the right accuracy for a thing whose eviction costs a |
| 180 | /// re-encode, and it is why the order is by *time* and not by a hit counter |
| 181 | /// nothing would persist. |
| 182 | /// |
| 183 | /// A `.part` is never deleted -- it is being written right now, by this process |
| 184 | /// or another -- but its bytes *are* counted: they are on the disk, and a |
| 185 | /// half-written 11 GB proxy is exactly when the finished ones should make room. |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | /// The proxy of `source` if one has already been made, `None` otherwise. The |
| 221 | /// answer a session asks at open: a hit costs one `stat`. |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | /// A proxy being made -- or one that was already there, which is the same thing |
| 227 | /// to whoever is waiting for a path. |
| 228 | /// |
| 229 | /// Poll [`progress`](Job::progress) while [`is_finished`](Job::is_finished) is |
| 230 | /// false and take the [`outcome`](Job::outcome) when it flips, exactly as an |
| 231 | /// export is polled. Dropping the job does not stop the work; |
| 232 | /// [`cancel`](Job::cancel) does. |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 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 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | /// Starts making the proxy of `source`, or hands back the one already in the |
| 291 | /// cache. Errors are the ones that happen before a worker exists -- a file with |
| 292 | /// no picture in it, nowhere to put a cache -- and everything after them comes |
| 293 | /// back through [`Job::outcome`], so a caller has two places to look and not |
| 294 | /// twenty. |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | /// The same, for a file that is worth standing in for and nobody else |
| 300 | /// ([`wanted`]) -- `Ok(None)` is a film this machine already cuts at speed. |
| 301 | /// |
| 302 | /// The header is read *once* for both questions: on a Matroska that read is the |
| 303 | /// cluster walk, so asking `wanted` and then `generate` separately would pay |
| 304 | /// for it twice. |
| 305 | /// |
| 306 | /// A file with no picture in it is not a film and is answered `Ok(None)`, the |
| 307 | /// same answer a 1080p H.264 film gets -- it is not a failure, and reporting it |
| 308 | /// as one put "NO PROXY for tone.wav" in front of somebody who had imported a |
| 309 | /// song. |
| 310 | /// |
| 311 | /// Twice, because there are two ways to know. A name this engine already reads |
| 312 | /// as sound or as a still is answered before anything is opened, which is the |
| 313 | /// cheap half. The other half is the honest one: **an audio-only `.mp4`, `.mkv` |
| 314 | /// or `.mov` is a video container with no video track in it**, and only its |
| 315 | /// header says so -- so the demuxer's own [`NoVideoTrack`] answer is taken here |
| 316 | /// as the same quiet `None`. A film that *has* a picture and will not open is a |
| 317 | /// different thing and stays loud. |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | /// Both doors: the cache is looked at, the header is read once, and `only_if` |
| 329 | /// decides off that header whether there is anything to do. |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 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 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 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 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 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 |