edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiB2e2ed69Tahinli3 months ago69 commits
| 1 | //! Export: the edit list rendered back out as one mp4. |
| 2 | //! |
| 3 | //! Video is fully re-encoded — a cut lands mid-GOP, so stream-copying across it |
| 4 | //! is impossible — while audio is copied packet for packet, because there is no |
| 5 | //! pure-Rust AAC encoder to re-encode with. The worker owns everything: the |
| 6 | //! caller gets an [`ExportHandle`] and polls it from its render loop. |
| 7 | //! |
| 8 | //! Nothing partial survives a failure: the worker writes to `<out>.part` and |
| 9 | //! renames it onto `out` only once the file is closed and complete, so the |
| 10 | //! output either does not exist or is finished — there is no window where a |
| 11 | //! half-written `.export.mp4` is sitting there looking playable. Cancel and |
| 12 | //! every error path delete the `.part`. A killed *process* leaves the `.part` |
| 13 | //! behind (only in-process cleanup is promised), which is an orphan a user can |
| 14 | //! delete rather than a file that plays for two seconds and stops. |
| 15 | |
| 16 | use ; |
| 17 | use ; |
| 18 | use ; |
| 19 | use thread; |
| 20 | |
| 21 | use ; |
| 22 | |
| 23 | use crateAudioSession; |
| 24 | use crate; |
| 25 | use crate; |
| 26 | use crate; |
| 27 | use crate; |
| 28 | |
| 29 | /// Progress is reported in permille: an atomic integer the render loop can read |
| 30 | /// without a lock, fine enough for any progress bar. |
| 31 | const PROGRESS_SCALE: u32 = 1_000; |
| 32 | |
| 33 | /// D2 rate control: bits per pixel per second, then a sane range. 720p30 lands |
| 34 | /// at 2.76 Mbps, which S1 measured the software encoder hitting within 1%. |
| 35 | const BITS_PER_PIXEL: f64 = 0.1; |
| 36 | const MIN_BITRATE: u64 = 1_000_000; |
| 37 | const MAX_BITRATE: u64 = 20_000_000; |
| 38 | |
| 39 | /// What the caller gets to decide about the output. The codec and container are |
| 40 | /// not among it: H.264 in mp4 is the only pair with both an encoder and a |
| 41 | /// decoder under this project's no-install rule, so offering a choice would be |
| 42 | /// offering files we cannot read back. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | /// A running export. Poll [`is_finished`](ExportHandle::is_finished) once per |
| 61 | /// rendered frame and take the [`result`](ExportHandle::result) when it flips; |
| 62 | /// dropping the handle does *not* stop the worker, [`cancel`](ExportHandle::cancel) |
| 63 | /// does. |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | /// Starts the worker. Failures are reported through the handle rather than |
| 94 | /// returned, so a caller has exactly one place to look. The files to read are |
| 95 | /// the project's own [`sources`](Project::sources) -- every clip names one -- |
| 96 | /// so nothing but the edit list decides what is decoded. |
| 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 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 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 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | /// The I420 planes of one black picture, allocated once for a whole export: a |
| 236 | /// gap in the video lane is encoded, not skipped, or every frame after it would |
| 237 | /// arrive early. Limited-range black is `Y=16, U=V=128`, the same convention |
| 238 | /// [`crate::convert`] decodes with. |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | /// `Err` once a cancel has been asked for. Called at every point where the work |
| 263 | /// left is more than an instant, so an `esc` at 99.9% still stops the export |
| 264 | /// instead of quietly completing it. |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | /// Writes one access unit, creating the file on the first one -- the parameter |
| 273 | /// sets for `avcC` only exist once the encoder has coded something. Units with |
| 274 | /// no coded slice are skipped: a software encoder may hand back an empty buffer |
| 275 | /// while it buffers, and the muxer rejects a sample that would carry no picture. |
| 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 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | /// Hardware where it is available, software everywhere else -- chosen once for |
| 317 | /// the whole export, so the stream never changes encoder mid-file. |
| 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 | |
| 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 | /// I420 straight out of the decoder: the export never converts to BGRA and back, |
| 424 | /// which would cost two conversions and a generation of colour precision. |
| 425 | /// |
| 426 | /// This mirrors `decode`'s two worker loops rather than reusing `DecodeSession`, |
| 427 | /// which only speaks BGRA. Unlike playback there is no mid-clip fallback to |
| 428 | /// software: a hardware decode that fails after the first picture fails the |
| 429 | /// export, which then deletes the half-written file. |
| 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 |