edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBcc667e6Tahinli3 months ago285 commits
| 1 | //! Export: the edit list rendered back out as one mp4 (H.264, AV1 or HEVC) or |
| 2 | //! one Matroska file (AV1 or HEVC) — or, picture left behind, as one WAV, FLAC, |
| 3 | //! MP3 or Ogg Vorbis of the timeline's audio alone. |
| 4 | //! |
| 5 | //! The HEVC pair is **intra-only**, and says so wherever it is offered: every |
| 6 | //! frame is a self-contained IDR from the vendored `oxideav-h265`, which is the |
| 7 | //! only shape of a pure-Rust HEVC encode fast enough to wait for (its inter |
| 8 | //! modes code 1080p at 0.81 fps across 12 cores against the intra path's 4.30). |
| 9 | //! That makes it an intraframe master in the family of ProRes and DNxHD — large |
| 10 | //! files, every frame a cut point — and not a delivery codec. |
| 11 | //! |
| 12 | //! Video is fully re-encoded — a cut lands mid-GOP, so stream-copying across it |
| 13 | //! is impossible — while audio is copied packet for packet wherever a copy can |
| 14 | //! say what the timeline says: a copy is exact, free and never a generation of |
| 15 | //! loss. Where it *cannot* — a second audio lane to mix in, a speeded clip, an |
| 16 | //! equalized one, a source that is not AAC inside an mp4's own sample table — |
| 17 | //! the sound is decoded, mixed and encoded again with `rusty_aac` |
| 18 | //! ([`encode_audio`]). The audio-only formats have no such split: `hound` writes |
| 19 | //! PCM, `flacenc` encodes FLAC, `rusty_mp3` encodes MP3 and `rusty_vorbis` |
| 20 | //! encodes Vorbis, so those are always *decoded* out of the timeline. The worker owns everything: the caller gets an |
| 21 | //! [`ExportHandle`] and polls it from its render loop. |
| 22 | //! |
| 23 | //! **Every video format here carries the timeline's sound**, in whichever |
| 24 | //! container: the mp4 muxer writes an AAC track and so does the Matroska one. |
| 25 | //! Nothing is written picture-only and nothing is left silent without saying so |
| 26 | //! — the only sound that does not come out is sound this engine cannot decode at |
| 27 | //! all (Opus and AC-3 inside a Matroska file), and that is an error by name. |
| 28 | //! |
| 29 | //! A **speeded** clip splits along the same line, and for the same reason. The |
| 30 | //! picture is re-encoded, so the frame walk honours a rate: it takes the source |
| 31 | //! frame each timeline frame shows ([`crate::project::Speed::source_at`], the |
| 32 | //! very frame preview holds there) and encodes a held one again rather than |
| 33 | //! decoding it twice. A clip whose *file* was shot at another rate than the |
| 34 | //! timeline goes through the same walk with one more conversion on the end |
| 35 | //! ([`crate::project::Rate`]), so it is encoded at the speed it was shot at. |
| 36 | //! The sound is *copied* unless something forces a decode, and a packet carries |
| 37 | //! no rate — so a speeded lane is one of the things that forces one |
| 38 | //! ([`copy_audio`]) rather than being written out at 1.00x under a re-timed |
| 39 | //! picture. The audio-only formats are decoded and resampled, so they honour a |
| 40 | //! rate like the picture does. |
| 41 | //! |
| 42 | //! Nothing partial survives a failure: the worker writes to `<out>.part` and |
| 43 | //! renames it onto `out` only once the file is closed and complete, so the |
| 44 | //! output either does not exist or is finished — there is no window where a |
| 45 | //! half-written `.export.mp4` is sitting there looking playable. Cancel and |
| 46 | //! every error path delete the `.part`. A killed *process* leaves the `.part` |
| 47 | //! behind (only in-process cleanup is promised), which is an orphan a user can |
| 48 | //! delete rather than a file that plays for two seconds and stops. |
| 49 | |
| 50 | use File; |
| 51 | use ; |
| 52 | use ; |
| 53 | use ; |
| 54 | use ; |
| 55 | use thread; |
| 56 | |
| 57 | use ; |
| 58 | |
| 59 | use BitRepr; |
| 60 | use Verify; |
| 61 | // The trait the Ogg muxer's `write_header` / `write_packet` / `write_trailer` |
| 62 | // live on -- `oxideav_ogg::mux::open_concrete` hands back the concrete muxer |
| 63 | // (the only way to reach `set_page_target_bytes`), so the trait has to be here. |
| 64 | use Muxer as _; |
| 65 | |
| 66 | use crate; |
| 67 | use crateColorDescription; |
| 68 | use crate; |
| 69 | use crate; |
| 70 | use crate; |
| 71 | use crate; |
| 72 | use crateComposer; |
| 73 | use crate; |
| 74 | |
| 75 | /// Progress is reported in permille: an atomic integer the render loop can read |
| 76 | /// without a lock, fine enough for any progress bar. |
| 77 | const PROGRESS_SCALE: u32 = 1_000; |
| 78 | |
| 79 | /// D2 rate control: bits per pixel per second, then a sane range. 720p30 lands |
| 80 | /// at 2.76 Mbps, which S1 measured the software encoder hitting within 1%. |
| 81 | const BITS_PER_PIXEL: f64 = 0.1; |
| 82 | const MIN_BITRATE: u64 = 1_000_000; |
| 83 | const MAX_BITRATE: u64 = 20_000_000; |
| 84 | |
| 85 | /// The rates the sound may be coded at, smallest first -- what a front-end |
| 86 | /// offers and what [`ExportSettings::audio_kbps`] is clamped into. Every one of |
| 87 | /// them is a legal MPEG-1 Layer III value, so the MP3 path writes the figure it |
| 88 | /// was given rather than the nearest one `rusty_mp3` snaps to. |
| 89 | pub const AUDIO_KBPS: = ; |
| 90 | |
| 91 | /// What the sound is coded at when nobody picked: the rate this project wrote |
| 92 | /// before the choice existed. 256 and not the encoders' own 128, because an |
| 93 | /// export is a master a person edits from again and this is a second lossy |
| 94 | /// generation over sources that may already have been one -- measured on the |
| 95 | /// suite's fixtures, an untouched clip re-encoded moves 0.15 dB here against |
| 96 | /// 0.46 dB at 128, and 32 KB/s beside a 2.76 Mbps picture is not a size anyone |
| 97 | /// is counting. |
| 98 | pub const DEFAULT_AUDIO_KBPS: u32 = 256; |
| 99 | |
| 100 | /// `rav1e`'s fastest preset. Anything slower is minutes per second of timeline |
| 101 | /// on a build with no assembly, which is what this one is. |
| 102 | const AV1_SPEED: u8 = 10; |
| 103 | |
| 104 | /// The HEVC coding tree block this encoder is fixed at: the coded picture is a |
| 105 | /// whole number of them, which is what the conformance window crops back. |
| 106 | const CTB: usize = 16; |
| 107 | /// How many frames the intra HEVC encoder codes at once. Twelve is where the |
| 108 | /// 2026-08-11 bench stopped scaling on this box (4.30 fps at 1080p); more lanes |
| 109 | /// only holds more decoded frames in memory. |
| 110 | const HEVC_LANES: usize = 12; |
| 111 | /// Bits per pixel the intra encoder spends at QP 27, measured on the same bench: |
| 112 | /// 0.607 at 720p and 0.586 at 1080p. What [`hevc_qp`] maps a bitrate row through. |
| 113 | const HEVC_BPP_AT_27: f64 = 0.6; |
| 114 | /// The band [`hevc_qp`] is clamped into, for the reason stated there. |
| 115 | const HEVC_QP_MIN: i32 = 22; |
| 116 | const HEVC_QP_MAX: i32 = 40; |
| 117 | |
| 118 | /// What an export writes. H.264-in-mp4, AV1 and HEVC -- each of the last two in |
| 119 | /// Matroska or in mp4, the user's pick of container -- are the *video* pairs |
| 120 | /// with both an encoder and a decoder under this project's no-install rule, and |
| 121 | /// WAV, FLAC, MP3 and Ogg Vorbis are the standalone audio formats with a |
| 122 | /// pure-Rust encoder: `hound`, `flacenc`, `rusty_mp3` and `rusty_vorbis`. Opus |
| 123 | /// still has none -- `oxideav-opus` encodes CELT alone, which is half a codec. |
| 124 | /// (AAC has one too -- `rusty_aac`, which is what a re-encoded video track's |
| 125 | /// sound leaves through -- but AAC is a container's own audio, never a file of |
| 126 | /// its own here.) VP9 is the |
| 127 | /// one codec left that comes in through the plugin and stops there, HEVC having |
| 128 | /// gained an encoder (an intra-only one, which the rows say). A front-end says |
| 129 | /// so rather than hiding the row. |
| 130 | /// |
| 131 | /// **Every video format carries sound.** Which way it carries it is the only |
| 132 | /// difference: copied packet for packet where a copy can say what the timeline |
| 133 | /// says, decoded and encoded again ([`encode_audio`]) where it cannot. |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 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 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | /// What the caller gets to decide about the output. The codec is not among it: |
| 226 | /// [`Format`] names a container *and* what goes in it, because every pair we |
| 227 | /// can write is a pair we can also read back. |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 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 | /// A running export. Poll [`is_finished`](ExportHandle::is_finished) once per |
| 270 | /// rendered frame and take the [`result`](ExportHandle::result) when it flips; |
| 271 | /// dropping the handle does *not* stop the worker, [`cancel`](ExportHandle::cancel) |
| 272 | /// does. |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 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 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | /// Starts the worker. Failures are reported through the handle rather than |
| 310 | /// returned, so a caller has exactly one place to look. The files to read are |
| 311 | /// the project's own [`sources`](Project::sources) -- every clip names one -- |
| 312 | /// so nothing but the edit list decides what is decoded. |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 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 | /// Whether any video lane holds anything at all -- what tells an audio-only |
| 380 | /// timeline from one that merely opens on a gap. Asked of the *lanes* rather |
| 381 | /// than of the sources, because a project may name a video file no clip plays |
| 382 | /// from any more. |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | /// Whether any audio lane holds anything, [`has_picture`]'s pair: what tells a |
| 391 | /// timeline whose export will carry sound from one whose file has none. |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | /// Whether any equalizer on the sound is doing something -- the one edit a |
| 400 | /// packet copy cannot carry, so an mp4 whose lane has one is decoded and |
| 401 | /// re-encoded ([`encode_audio`]) instead of copied. Asked of every audio lane: |
| 402 | /// the copy path has exactly one by the time it asks, and [`planned_audio`] is |
| 403 | /// asked before that is settled. |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | /// Whether the *mix* is doing something -- a lane turned off unity, or a |
| 412 | /// limiter in circuit. [`equalized`]'s pair, and a copy cannot carry either of |
| 413 | /// them: both live where the lanes are summed |
| 414 | /// ([`AudioSession::open_mixed_streams_master`]), and a copied packet was never |
| 415 | /// summed at all. An mp4 whose timeline is mixed is decoded and re-encoded. |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | /// The bitrate an export codes at: the caller's number through the same clamp |
| 421 | /// as the computed one, for the reason [`Enc::open`] states. |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | /// The rate the sound codes at: the caller's pick clamped into the offered |
| 429 | /// range, [`DEFAULT_AUDIO_KBPS`] where there was none. Clamped and not refused, |
| 430 | /// for [`bitrate_of`]'s reason -- a number out of range is a caller's slip and |
| 431 | /// not a file worth failing. |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | /// The hardware seat for these settings, opened exactly as the export opens it |
| 439 | /// -- the same pins, the same AV1 opt-in, the same dimensions a driver may |
| 440 | /// refuse -- or `None` where the software encoder takes the file. The single |
| 441 | /// place that choice is made, which is what lets [`planned_video`] *measure* it |
| 442 | /// instead of promising it. |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | /// How a front-end names a video seat: which of the two encoders has the file, |
| 466 | /// and which library that is. Not the codec -- a caller shows this beside the |
| 467 | /// format it picked, and `rav1e` names AV1 as `rusty_h264` names H.264. One |
| 468 | /// place, so what a card says before an export and what its progress line says |
| 469 | /// during one cannot drift apart. |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | /// The cheap half of "does this timeline's sound have to be re-encoded": what |
| 480 | /// the *edit* says, with no file opened. Two lanes are a mix and a mix is not a |
| 481 | /// copy; a speeded lane is resampled; an equalizer is sample math. The other |
| 482 | /// half -- whether the sources can be copied out of at all -- costs a file open |
| 483 | /// each and belongs to [`copy_audio`], which may re-encode where this says copy. |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | /// What the sound is written by. Every audio encoder here is software -- there |
| 499 | /// is no hardware AAC, PCM, FLAC or MP3 seat -- so this names *which* one, and |
| 500 | /// whether a container escapes encoding altogether by copying the packets it was |
| 501 | /// given. Both AV1 formats are in it like the mp4: an AV1 export carries the |
| 502 | /// timeline's sound too, so leaving it unnamed would be the old lie. |
| 503 | /// |
| 504 | /// A prediction, and it says so: `copied` is what [`copy_audio`] *did*, which a |
| 505 | /// card asking before the export has no way to know (a source that cannot be |
| 506 | /// copied out of is only found by opening it). `None` predicts from the edit |
| 507 | /// alone. |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | /// What [`start`] would write this timeline's sound with, before one is |
| 530 | /// started. Pure: no probe and no file opened, so a card may ask it per repaint. |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | /// Why the sound's rate is nothing this timeline can be asked about in this |
| 536 | /// format, or `None` where it is a real choice: there is no sound to write, the |
| 537 | /// format codes the samples themselves, or the packets are the source's own and |
| 538 | /// carry the rate *it* was coded at. Pure, like [`planned_audio`], and a |
| 539 | /// prediction in the same way -- a copy that turns out impossible (a source no |
| 540 | /// mp4 sample table holds) falls through to [`encode_audio`], which codes at |
| 541 | /// [`ExportSettings::audio_kbps`] like every other encode. So the value travels |
| 542 | /// even where this refuses; what it cannot do is silently write a rate nobody |
| 543 | /// picked. |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | /// The track a Matroska export carries, or `None`: no pick, a pick nothing |
| 562 | /// answers, a track that could not be read, one with no cue left on the |
| 563 | /// exported timeline -- and every mp4, whose refusal [`planned_subtitles`] says |
| 564 | /// out loud before the button is pressed. |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | /// The track's cues where the *exported* timeline puts them, which is the only |
| 588 | /// clock the file has: the export writes timeline frame 0 onwards and nothing |
| 589 | /// else, exactly as the sound is mixed from frame 0 and resized to the |
| 590 | /// timeline's own length ([`encode_audio`]). |
| 591 | /// |
| 592 | /// Which clock the cues arrive in is what the track itself says: |
| 593 | /// |
| 594 | /// * a track out of a *file* (`SubtitleTrack::track`) is timed against that |
| 595 | /// file, so each cue is carried through the spans that play it -- a cue over |
| 596 | /// a stretch that was cut out is gone, one straddling a cut keeps the half |
| 597 | /// that is still there, and one after a rippled delete moves back with the |
| 598 | /// picture it belongs to. The *edges* a cut clips to are frames -- that is |
| 599 | /// what a cut is -- but a cue nothing clipped keeps its own microseconds, so |
| 600 | /// an untouched timeline exports the times the source states. |
| 601 | /// * a *standalone* file (`subs.srt`) is timed against nothing else -- there is |
| 602 | /// no clip to hang it on and no offset in the project to shift it by -- so it |
| 603 | /// is the timeline's own clock, which is where it was drawn while editing |
| 604 | /// (`Player::subtitle_overlay`). Clipped to the exported length and no more. |
| 605 | /// |
| 606 | /// This is also what the *preview* draws: the plate over the picture and the |
| 607 | /// timeline strip both ask it through |
| 608 | /// [`PlaybackSession::timeline_cues`](crate::PlaybackSession::timeline_cues), so |
| 609 | /// what a rippled timeline shows and what its export writes are one answer by |
| 610 | /// construction rather than two maps kept in step by hand. |
| 611 | /// |
| 612 | /// Pure and cheap enough to ask per repaint (a walk of the spans and of the |
| 613 | /// cues, no file opened), which is how a front-end asks it. |
| 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 | /// What [`start`] would do about the subtitles, in the words a card shows. |
| 679 | /// Pure, like [`planned_audio`]: no file is opened, so it may be asked per |
| 680 | /// repaint. |
| 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 | /// What [`start`] would encode the picture with, probed the way the export |
| 706 | /// probes it -- the very encoder is opened and closed again -- so this is a |
| 707 | /// measurement and not a promise. `None` for a format that carries no picture. |
| 708 | /// |
| 709 | /// Costs that open (~100 ms here): ask it off a render thread and keep the |
| 710 | /// answer until the format, the resolution or the bitrate changes. |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | /// The audio track an export writes, and how it was made -- a copy is the |
| 725 | /// source's own packets, bit for bit, and anything else is this project's |
| 726 | /// encoder over the decoded mix. [`run`] publishes which of the two really |
| 727 | /// happened, so the encoder line a user reads is measured and not promised. |
| 728 | pub |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | /// The timeline's audio track for a file that carries picture: copied packet for |
| 736 | /// packet where a copy says exactly what the timeline says, and decoded, mixed |
| 737 | /// and encoded again ([`encode_audio`]) where it cannot -- a second audio lane, |
| 738 | /// a speeded clip, an equalizer, a source that is not AAC inside an mp4 sample |
| 739 | /// table. Nothing is refused for being uncopyable any more; the only refusals |
| 740 | /// left below the copy are about sound this engine cannot *decode* either, and |
| 741 | /// those come out of the decode path by name. |
| 742 | /// |
| 743 | /// The copy is still the default and still bit-exact: a timeline nobody has |
| 744 | /// touched leaves as the very packets its source holds. |
| 745 | /// |
| 746 | /// ponytail: this holds the whole exported AAC track in memory (~3 kB per |
| 747 | /// 23 ms packet, so ~500 MB for an hour). Upgrade path is a streaming |
| 748 | /// `copy_segments` that yields packets instead of collecting them. |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | /// The same lane, *decoded*: what a packet copy cannot carry comes out here as |
| 836 | /// AAC written by this project's own encoder. |
| 837 | /// |
| 838 | /// The samples are the very ones [`run_audio`] would put in a WAV -- the same |
| 839 | /// opener with the same equalizers, the same rates and the same resize onto the |
| 840 | /// timeline's own length -- so an mp4 and a WAV of one timeline are one mix, |
| 841 | /// not two that could disagree. |
| 842 | /// |
| 843 | /// AAC-LC's encoder delay is one 1024-frame block, which is exactly the priming |
| 844 | /// a reader drops when the file carries no edit list (`audio::DEFAULT_PRIMING`, |
| 845 | /// and [`crate::mux::Mp4Muxer`] writes none): the first packet is that delay, so |
| 846 | /// the sound starts at timeline frame 0 as a copied track's does. |
| 847 | /// |
| 848 | /// ponytail: the mix sits in memory as f32 before it is encoded (~46 MB a |
| 849 | /// minute of 48 kHz stereo), for [`run_audio`]'s reason -- `rusty_aac` buffers |
| 850 | /// the whole stream anyway, since it encodes its frames in parallel. Upgrade |
| 851 | /// path is a chunked push once that encoder streams. |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | /// What rate `path` was shot at against a timeline at `timeline_fps` |
| 1178 | /// ([`Rate`]). [`Rate::REAL_TIME`] for the files that have no rate of their own |
| 1179 | /// -- a still, a song -- and for one that will not open here: the decoder is |
| 1180 | /// opened a few lines later and fails the export by name, which is a better |
| 1181 | /// error than this one could raise. |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | /// The audio lanes alone, summed, as a WAV or a FLAC. |
| 1204 | /// |
| 1205 | /// Nothing here re-derives the edit: the play lists are |
| 1206 | /// [`Project::audio_segments_from`] and they are handed to the very |
| 1207 | /// [`AudioSession::open_mixed_streams`] playback feeds from, so what is written |
| 1208 | /// is what was heard -- every lane, mixed the same way, gaps included, which |
| 1209 | /// arrive as real silence from that choke point rather than as a hole this would |
| 1210 | /// have to invent. |
| 1211 | /// |
| 1212 | /// The length is the *timeline's*: each segment resolves its window to whole |
| 1213 | /// samples on its own, so the sum can miss the timeline by a sample or two, and |
| 1214 | /// a source that runs out early would leave the file short. Both are settled by |
| 1215 | /// one `resize` at the end -- padding with silence, trimming what overshot -- |
| 1216 | /// so an exported file is exactly as long as what was edited. |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | /// 16-bit PCM at the timeline's own rate and layout: no resampling anywhere in |
| 1296 | /// this engine, so what the decoder handed over is what the header says. |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | /// The same samples as MPEG-1 Layer III, `rusty_mp3` doing the encoding -- pure |
| 1317 | /// Rust like every other encoder here, and the reason this format is a row at |
| 1318 | /// all: the LGPL `shine-rs` was the only one when it was not. |
| 1319 | /// |
| 1320 | /// CBR at the caller's rate, [`DEFAULT_AUDIO_KBPS`] where there was none. Every |
| 1321 | /// offered rate is already a legal Layer III value, so the encoder's snap is a |
| 1322 | /// backstop and not a silent substitution; a sample rate MPEG has no frame for |
| 1323 | /// (anything but 8-48 kHz) is refused there by name rather than written as |
| 1324 | /// something else. |
| 1325 | /// |
| 1326 | /// ponytail: CBR only -- the card offers rates and not a quality index. Upgrade |
| 1327 | /// path is `Mp3EncoderConfig::vbr_quality` behind a setting of its own. |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | /// The Vorbis quality every `.ogg` this program writes is coded at, on |
| 1352 | /// `rusty_vorbis`'s normalised `[0, 1]` scale -- about `-q 8.35` on the scale |
| 1353 | /// Vorbis itself is spoken in (`quality01_from_vorbis_q`). |
| 1354 | /// |
| 1355 | /// A measured number, not a taste. The 2026-08-11 bench encoded three of this |
| 1356 | /// suite's fixtures (`test_tone_48k.wav`, `test_av.mp4`, `test_tone.mp3`) at |
| 1357 | /// every quality from 0.40 to 0.95 and decoded each back with **symphonia** -- |
| 1358 | /// an independent decoder, and the very one [`crate::audio`] reopens an export |
| 1359 | /// with. Two things came out of it: |
| 1360 | /// |
| 1361 | /// * **Below 0.50 the encoder is quiet, not just coarse.** The decoded RMS |
| 1362 | /// against the source's runs 0.83-0.94 at quality 0.40 (a 1 to 1.6 dB level |
| 1363 | /// error), and at 0.10 and below the file decodes to near-silence -- the same |
| 1364 | /// class of failure the AC-3 mono downmix has in `crate::audio`. From 0.55 up |
| 1365 | /// the level sits at 1.000-1.009, which is what a lossy codec should do. |
| 1366 | /// * **The band tops out well under the rates the Sound row offers.** Quality |
| 1367 | /// 0.85 measured 176, 144 and 148 kbps on the three fixtures; 0.95 only |
| 1368 | /// reaches ~200. That is why [`audio_rate_refusal`] takes the row away rather |
| 1369 | /// than mapping 128/192/256/320 onto quality steps: the top two are numbers |
| 1370 | /// this encoder cannot produce, and a card that printed them would be lying. |
| 1371 | /// |
| 1372 | /// 0.85 is the top of the band where every fixture still decoded to *exactly* |
| 1373 | /// its own length (0.95 overshot one by 820 samples), which is the property |
| 1374 | /// [`write_ogg`] is built around. |
| 1375 | const VORBIS_QUALITY: f32 = 0.85; |
| 1376 | |
| 1377 | /// The Vorbis block hop, and the size of the pre-roll [`write_ogg`] feeds the |
| 1378 | /// encoder. `rusty_vorbis` emits long blocks only (`N = 2048`, hop `N/2`). |
| 1379 | const VORBIS_HOP: usize = 1024; |
| 1380 | |
| 1381 | /// The same samples as Vorbis I in an Ogg container: `rusty_vorbis` encodes, |
| 1382 | /// `oxideav-ogg` pages. Pure Rust on both halves, like every other encoder here. |
| 1383 | /// |
| 1384 | /// Two corrections stand between that library pair and a file this project would |
| 1385 | /// ship, and both are measured rather than guessed (2026-08-11, decoded back |
| 1386 | /// with symphonia every time): |
| 1387 | /// |
| 1388 | /// 1. **One hop of pre-roll.** `rusty_vorbis` advances the granule by a full hop |
| 1389 | /// on its *first* audio packet, which its own doc comment says decodes to |
| 1390 | /// zero samples. Written straight through, every sample lands ~1024 early: |
| 1391 | /// a marker signal whose only energy was its first and last 1024 samples came |
| 1392 | /// back with the head at 36% of its amplitude and a full-scale burst 1500 |
| 1393 | /// samples *before* the real one, in what should have been silence. Feeding |
| 1394 | /// [`VORBIS_HOP`] samples of silence ahead of the mix and subtracting the same |
| 1395 | /// from every granule puts it back: head 0.86 against an input peak of 0.80, |
| 1396 | /// the phantom burst down to 0.04, the real burst at its own place. The |
| 1397 | /// decoder drops the pre-roll itself -- a first page whose granule is under |
| 1398 | /// what its packets decode to is exactly how Ogg says "skip this much". |
| 1399 | /// 2. **The tail is declared, not encoded.** The block grid overshoots the last |
| 1400 | /// sample, so the stream is padded out and the *last* page's granule is set |
| 1401 | /// to the timeline's own frame count. A decoder trims to it, which is what |
| 1402 | /// makes an `.ogg` here exactly as long as the WAV of the same timeline -- |
| 1403 | /// the promise [`run_audio`] makes for every other audio format. |
| 1404 | /// |
| 1405 | /// **Stereo, always.** The embedded setup header `rusty_vorbis` ships is a |
| 1406 | /// stereo profile: a mono push is refused outright ("bad coupling channels"), |
| 1407 | /// and so is anything wider. A mono mix is therefore written as dual mono, both |
| 1408 | /// channels bit-identical (measured: `max|L-R|` is exactly 0, and Vorbis's own |
| 1409 | /// channel coupling codes the empty side channel for almost nothing). Wider than |
| 1410 | /// stereo cannot reach here -- [`crate::audio`] folds 5.1 to stereo when the |
| 1411 | /// source is opened -- but it is refused by name rather than left to the |
| 1412 | /// library's own message. |
| 1413 | /// |
| 1414 | /// ponytail: one quality, [`VORBIS_QUALITY`], with no user control -- the Sound |
| 1415 | /// row is refused for this format because the rates it offers are not rates |
| 1416 | /// Vorbis reaches. Upgrade path is a quality picker of its own, worth building |
| 1417 | /// the day the card has a control that speaks in quality rather than kbps. |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | |
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | |
| 1499 | |
| 1500 | |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | /// The page size [`write_ogg`] asks the Ogg muxer to aim at, in bytes. RFC 3533 |
| 1512 | /// describes pages as "usually 4-8 kB", which is also the muxer's own suggested |
| 1513 | /// general-purpose value. |
| 1514 | const OGG_PAGE_BYTES: usize = 4096; |
| 1515 | |
| 1516 | /// The same samples, losslessly compressed. `flacenc`'s errors are flattened to |
| 1517 | /// text: they are not `Send + Sync`, and every one of them fails the export the |
| 1518 | /// same way. |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 | |
| 1552 | /// The I420 planes of one black picture, allocated once for a whole export: a |
| 1553 | /// gap in the video lane is encoded, not skipped, or every frame after it would |
| 1554 | /// arrive early. Limited-range black is `Y=16, U=V=128`, the same convention |
| 1555 | /// [`crate::convert`] decodes with. |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | |
| 1579 | /// `Err` once a cancel has been asked for. Called at every point where the work |
| 1580 | /// left is more than an instant, so an `esc` at 99.9% still stops the export |
| 1581 | /// instead of quietly completing it. |
| 1582 | |
| 1583 | |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | /// The two containers an export writes, so the picture loop above is one loop: |
| 1590 | /// H.264 goes into mp4, AV1 into Matroska, and nothing else is offered. |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | |
| 1604 | |
| 1605 | /// Writes one access unit, creating the file on the first one -- the parameter |
| 1606 | /// sets for `avcC` (and the sequence header for `CodecPrivate`) only exist once |
| 1607 | /// the encoder has coded something. Units with no coded slice are skipped: a |
| 1608 | /// software encoder may hand back an empty buffer while it buffers, and the |
| 1609 | /// muxer rejects a sample that would carry no picture. |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | |
| 1624 | |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | |
| 1644 | |
| 1645 | |
| 1646 | |
| 1647 | |
| 1648 | |
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |
| 1662 | |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | |
| 1682 | |
| 1683 | |
| 1684 | |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | |
| 1690 | |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | |
| 1697 | |
| 1698 | |
| 1699 | |
| 1700 | |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | |
| 1719 | |
| 1720 | |
| 1721 | |
| 1722 | |
| 1723 | |
| 1724 | |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | |
| 1738 | |
| 1739 | |
| 1740 | |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | |
| 1747 | |
| 1748 | |
| 1749 | |
| 1750 | |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | /// The `hvcC` record of the first coded unit, or the refusal by name. Split out |
| 1756 | /// because both HEVC containers ask it in the same words. |
| 1757 | |
| 1758 | |
| 1759 | |
| 1760 | |
| 1761 | |
| 1762 | |
| 1763 | |
| 1764 | |
| 1765 | |
| 1766 | |
| 1767 | |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | /// Hardware where it is available, software everywhere else -- chosen once for |
| 1772 | /// the whole export, so the stream never changes encoder mid-file. Both codecs |
| 1773 | /// have both seats: an AV1 export runs on the plugin's VA-API encoder where the |
| 1774 | /// GPU has one and on `rav1e` where it has not, which is the same pair H.264 has |
| 1775 | /// had and the same silent fallback. |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | |
| 1787 | |
| 1788 | |
| 1789 | |
| 1790 | |
| 1791 | |
| 1792 | |
| 1793 | |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | |
| 1800 | |
| 1801 | |
| 1802 | |
| 1803 | |
| 1804 | /// The intra HEVC seat. Frames arrive one at a time (the export walk is one |
| 1805 | /// picture per timeline frame) and are held until there are [`lanes`] of them, |
| 1806 | /// then coded in parallel and queued in **display order** -- fanning out is the |
| 1807 | /// only thing that makes a pure-Rust HEVC export bearable (4.30 fps at 1080p on |
| 1808 | /// 12 lanes against 0.55 fps on one, measured 2026-08-11), and an export whose |
| 1809 | /// frames came back shuffled would be no export at all. |
| 1810 | /// |
| 1811 | /// ponytail: a batch of padded planes sits in memory (~3 MB a frame at 1080p, |
| 1812 | /// so ~37 MB at 12 lanes) and the tail of a timeline is coded on fewer lanes |
| 1813 | /// than the middle. Upgrade path is a pipeline that keeps every lane fed from a |
| 1814 | /// decoder running ahead of the encoder rather than a batch barrier. |
| 1815 | |
| 1816 | |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | |
| 1828 | |
| 1829 | |
| 1830 | |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | |
| 1842 | |
| 1843 | |
| 1844 | |
| 1845 | |
| 1846 | |
| 1847 | |
| 1848 | |
| 1849 | |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | |
| 1857 | |
| 1858 | |
| 1859 | |
| 1860 | |
| 1861 | |
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | |
| 1873 | |
| 1874 | |
| 1875 | |
| 1876 | |
| 1877 | |
| 1878 | |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | |
| 1908 | |
| 1909 | |
| 1910 | |
| 1911 | |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | |
| 1916 | |
| 1917 | |
| 1918 | |
| 1919 | |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | |
| 1924 | |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 | |
| 1936 | |
| 1937 | |
| 1938 | |
| 1939 | |
| 1940 | |
| 1941 | |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | |
| 1966 | |
| 1967 | |
| 1968 | |
| 1969 | |
| 1970 | |
| 1971 | |
| 1972 | |
| 1973 | |
| 1974 | |
| 1975 | |
| 1976 | |
| 1977 | |
| 1978 | |
| 1979 | |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | |
| 1984 | |
| 1985 | |
| 1986 | |
| 1987 | |
| 1988 | |
| 1989 | |
| 1990 | |
| 1991 | |
| 1992 | |
| 1993 | |
| 1994 | |
| 1995 | |
| 1996 | |
| 1997 | |
| 1998 | |
| 1999 | |
| 2000 | |
| 2001 | |
| 2002 | |
| 2003 | |
| 2004 | |
| 2005 | |
| 2006 | |
| 2007 | |
| 2008 | |
| 2009 | |
| 2010 | |
| 2011 | |
| 2012 | |
| 2013 | |
| 2014 | |
| 2015 | |
| 2016 | |
| 2017 | |
| 2018 | |
| 2019 | |
| 2020 | |
| 2021 | |
| 2022 | |
| 2023 | |
| 2024 | |
| 2025 | |
| 2026 | |
| 2027 | |
| 2028 | |
| 2029 | |
| 2030 | |
| 2031 | |
| 2032 | |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | |
| 2043 | |
| 2044 | |
| 2045 | |
| 2046 | |
| 2047 | |
| 2048 | |
| 2049 | |
| 2050 | |
| 2051 | |
| 2052 | |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | |
| 2063 | |
| 2064 | |
| 2065 | |
| 2066 | |
| 2067 | |
| 2068 | |
| 2069 | |
| 2070 | |
| 2071 | |
| 2072 | |
| 2073 | |
| 2074 | |
| 2075 | |
| 2076 | |
| 2077 | |
| 2078 | |
| 2079 | |
| 2080 | |
| 2081 | |
| 2082 | |
| 2083 | |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | |
| 2088 | |
| 2089 | |
| 2090 | |
| 2091 | |
| 2092 | |
| 2093 | |
| 2094 | |
| 2095 | |
| 2096 | |
| 2097 | |
| 2098 | |
| 2099 | |
| 2100 | |
| 2101 | |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | |
| 2110 | |
| 2111 | |
| 2112 | |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | |
| 2121 | |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | |
| 2126 | |
| 2127 | |
| 2128 | |
| 2129 | |
| 2130 | |
| 2131 | |
| 2132 | |
| 2133 | |
| 2134 | |
| 2135 | |
| 2136 | |
| 2137 | |
| 2138 | |
| 2139 | |
| 2140 | |
| 2141 | |
| 2142 | |
| 2143 | |
| 2144 | |
| 2145 | |
| 2146 | |
| 2147 | |
| 2148 | |
| 2149 | |
| 2150 | |
| 2151 | |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | /// The oldest coded intra AU, lent out under the same "valid until the next |
| 2156 | /// call" contract [`pop_av1`] has. Every one of them is an IDR, so the key flag |
| 2157 | /// is not a guess. |
| 2158 | |
| 2159 | |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | /// One I420 picture copied onto the padded plane sizes the CTB grid needs, the |
| 2164 | /// added rows and columns filled by **replicating the edge** rather than with |
| 2165 | /// black: the padding is coded and then cropped away, and a black border would |
| 2166 | /// bleed into the last real column through the intra prediction and the |
| 2167 | /// transform that straddles it. |
| 2168 | |
| 2169 | |
| 2170 | |
| 2171 | |
| 2172 | |
| 2173 | |
| 2174 | |
| 2175 | |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | |
| 2185 | |
| 2186 | |
| 2187 | |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | |
| 2192 | |
| 2193 | |
| 2194 | /// The quantiser an intra HEVC export codes at, mapped from the *bitrate* row a |
| 2195 | /// user picked -- the card has no QP control and this codec has no rate control, |
| 2196 | /// so the two are joined by measurement rather than promised to each other. |
| 2197 | /// |
| 2198 | /// The encoder codes ~0.6 bits per pixel at QP 27 ([`HEVC_BPP_AT_27`], measured |
| 2199 | /// on the 720p and 1080p benches to within 4% of each other), and a step of 6 QP |
| 2200 | /// is a factor of two in rate, which is the classic HEVC relation. So the QP |
| 2201 | /// that would land on a target is `27 + 6 * log2(0.6 / target)` -- and it is |
| 2202 | /// clamped, because this is a *quality* dial and not a rate controller: below QP |
| 2203 | /// 22 the file grows without a picture to show for it, and past QP 40 an |
| 2204 | /// intra-only frame goes blocky in a way no bitrate row asked for. A 1080p30 |
| 2205 | /// timeline at the automatic 6.2 Mbps therefore codes at QP 40 and comes out |
| 2206 | /// bigger than that, which is what "intra-only, large files" on the format row |
| 2207 | /// means: the row buys quality, not a size. |
| 2208 | |
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | /// Every temporal unit `rav1e` has finished, moved into the queue in display |
| 2216 | /// order. `NeedMoreData` (send another picture) and `LimitReached` (the flush is |
| 2217 | /// through) are the two ways it says there is nothing more; `Encoded` means a |
| 2218 | /// picture was coded but not yet packetised, which is a *keep asking* -- taking |
| 2219 | /// it for an end leaves the tail of the export inside the encoder. |
| 2220 | |
| 2221 | |
| 2222 | |
| 2223 | |
| 2224 | |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 | |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | /// The oldest finished unit, moved into the buffer that is lent out -- the same |
| 2236 | /// "valid until the next call" contract the plugin's slice has. |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | /// I420 straight out of the decoder: the export never converts to BGRA and back, |
| 2247 | /// which would cost two conversions and a generation of colour precision. |
| 2248 | /// |
| 2249 | /// This mirrors `decode`'s two worker loops rather than reusing `DecodeSession`, |
| 2250 | /// which only speaks BGRA. Unlike playback there is no mid-clip fallback to |
| 2251 | /// software: a hardware decode that fails after the first picture fails the |
| 2252 | /// export, which then deletes the half-written file. |
| 2253 | |
| 2254 | |
| 2255 | |
| 2256 | |
| 2257 | |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | |
| 2268 | |
| 2269 | |
| 2270 | |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | |
| 2277 | |
| 2278 | |
| 2279 | |
| 2280 | |
| 2281 | |
| 2282 | |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | |
| 2290 | |
| 2291 | |
| 2292 | |
| 2293 | |
| 2294 | |
| 2295 | |
| 2296 | |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | |
| 2301 | |
| 2302 | |
| 2303 | |
| 2304 | |
| 2305 | |
| 2306 | |
| 2307 | |
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | |
| 2316 | |
| 2317 | |
| 2318 | |
| 2319 | |
| 2320 | |
| 2321 | |
| 2322 | |
| 2323 | |
| 2324 | |
| 2325 | |
| 2326 | |
| 2327 | |
| 2328 | |
| 2329 | |
| 2330 | |
| 2331 | |
| 2332 | |
| 2333 | |
| 2334 | |
| 2335 | |
| 2336 | |
| 2337 | |
| 2338 | |
| 2339 | |
| 2340 | |
| 2341 | |
| 2342 | |
| 2343 | |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | |
| 2356 | |
| 2357 | |
| 2358 | |
| 2359 | |
| 2360 | |
| 2361 | |
| 2362 | |
| 2363 | |
| 2364 | |
| 2365 | |
| 2366 | |
| 2367 | |
| 2368 | |
| 2369 | |
| 2370 | |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | |
| 2383 | |
| 2384 | |
| 2385 | |
| 2386 | |
| 2387 | |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
| 2395 | |
| 2396 | |
| 2397 | |
| 2398 | |
| 2399 | |
| 2400 | |
| 2401 | |
| 2402 | |
| 2403 | |
| 2404 | |
| 2405 | |
| 2406 | |
| 2407 | |
| 2408 | |
| 2409 | |
| 2410 | |
| 2411 | |
| 2412 | |
| 2413 | |
| 2414 | |
| 2415 | |
| 2416 | |
| 2417 | |
| 2418 | |
| 2419 | |
| 2420 | |
| 2421 | |
| 2422 | |
| 2423 | |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | |
| 2428 | |
| 2429 | |
| 2430 | |
| 2431 | |
| 2432 | |
| 2433 | |
| 2434 | |
| 2435 | |
| 2436 | |
| 2437 | |
| 2438 | |
| 2439 | |
| 2440 | |
| 2441 | |
| 2442 | |
| 2443 | |
| 2444 | |
| 2445 | |
| 2446 | |
| 2447 | |
| 2448 | |
| 2449 | |
| 2450 | |
| 2451 | |
| 2452 | |
| 2453 | |
| 2454 | |
| 2455 | |
| 2456 | |
| 2457 | |
| 2458 | |
| 2459 | |
| 2460 | |
| 2461 | |
| 2462 | |
| 2463 | |
| 2464 | |
| 2465 | |
| 2466 | |
| 2467 | |
| 2468 | |
| 2469 | |
| 2470 | |
| 2471 | |
| 2472 | |
| 2473 | |
| 2474 | |
| 2475 | |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | |
| 2482 | |
| 2483 | |
| 2484 | |
| 2485 | |
| 2486 | |
| 2487 | |
| 2488 | |
| 2489 | |
| 2490 | |
| 2491 | |
| 2492 | |
| 2493 | |
| 2494 | |
| 2495 | |
| 2496 | |
| 2497 | |
| 2498 | |
| 2499 | |
| 2500 | |
| 2501 | |
| 2502 | |
| 2503 | |
| 2504 | |
| 2505 | |
| 2506 | |
| 2507 | |
| 2508 | |
| 2509 | |
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 | |
| 2519 | |
| 2520 | |
| 2521 | |
| 2522 | |
| 2523 | |
| 2524 | |
| 2525 | |
| 2526 | |
| 2527 | |
| 2528 | |
| 2529 | |
| 2530 | |
| 2531 | |
| 2532 | |
| 2533 | |
| 2534 | |
| 2535 | |
| 2536 | |
| 2537 | |
| 2538 | |
| 2539 | |
| 2540 | |
| 2541 | |
| 2542 | |
| 2543 | |
| 2544 | |
| 2545 | |
| 2546 | |
| 2547 | |
| 2548 | |
| 2549 | |
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | |
| 2557 | |
| 2558 | |
| 2559 | |
| 2560 | |
| 2561 | |
| 2562 | |
| 2563 | |
| 2564 | |
| 2565 | |
| 2566 | |
| 2567 | |
| 2568 | |
| 2569 | |
| 2570 | |
| 2571 | |
| 2572 | |
| 2573 |