edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits
| 1 | //! Hardware encode checks. These need a built `libengine_hw.so` plus a working |
| 2 | //! VA-API driver with an H.264 encode entrypoint, so they are `#[ignore]`d by |
| 3 | //! default. Run them with: |
| 4 | //! |
| 5 | //! ```text |
| 6 | //! cargo build --workspace --release |
| 7 | //! LD_LIBRARY_PATH=target/release \ |
| 8 | //! cargo test -p engine --release --test hw_encode -- --ignored --nocapture --test-threads=1 |
| 9 | //! ``` |
| 10 | //! |
| 11 | //! The round trip goes out through the plugin and back in through the *software* |
| 12 | //! decoder (`ec-h264` driven by hand, not `DecodeSession`), so a bug in the |
| 13 | //! session plumbing cannot cancel out a plugin bug; the independent witness for |
| 14 | //! the stream's well-formedness is ffmpeg, in the export suite. |
| 15 | |
| 16 | use ; |
| 17 | |
| 18 | use ; |
| 19 | use ; |
| 20 | use ; |
| 21 | use H264Decoder; |
| 22 | |
| 23 | const FPS: u32 = 30; |
| 24 | const BITRATE: u64 = 4_000_000; |
| 25 | |
| 26 | /// A moving diagonal gradient: something that actually costs bits, so the |
| 27 | /// encoder cannot make the whole test pass by emitting skip frames. |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | /// Encodes `count` synthetic pictures and returns the whole Annex-B stream plus |
| 42 | /// the number of access units the plugin handed back. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | /// Takes everything the decoder has released so far. |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | /// One decoded picture as plain planes, whatever the decoder calls them. |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | /// Splits an Annex-B stream into access units: a new one at each slice NAL |
| 114 | /// that follows a picture, parameter sets and SEI riding with the slice that |
| 115 | /// follows them. The plugin emits one AU per picture; the split exists so the |
| 116 | /// decoder sees the packet shape a demuxer hands it for a file. |
| 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 | /// 322x242 is even (NV12 needs that) but neither dimension is a multiple of 16, |
| 204 | /// so the surface is padded and the encoded stream has to crop it back. This is |
| 205 | /// the 1080p case in miniature: 1080 pads to 1088. |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | /// The zero-copy path: a picture the hardware decoder wrote goes into the |
| 226 | /// hardware encoder as the buffer it was decoded into, never read back and never |
| 227 | /// written out again -- which is what makes an export of an untouched clip cost |
| 228 | /// no `vaGetImage`, no colour conversion and no upload per frame. |
| 229 | /// |
| 230 | /// Two things are checked and the second is the one that matters. That the |
| 231 | /// buffers are handed over *at all* (a driver whose export the plugin refuses |
| 232 | /// falls back to pixels, silently and correctly, which would make a speed claim |
| 233 | /// a lie). And that what came out is what went in: the encoded stream is decoded |
| 234 | /// in software and compared against the same file decoded through the read-back |
| 235 | /// path, so a wrong plane offset, a swapped object or an unsynchronised surface |
| 236 | /// -- the ways an imported buffer goes wrong -- fails here instead of shipping |
| 237 | /// as a garbled export. |
| 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 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | /// What the process boundary costs, at the size an export is really written at. |
| 302 | /// |
| 303 | /// The encoder lives in a child now ([`engine::hwproc`]) so that a driver's |
| 304 | /// `abort()` cannot take the editor with it, and the price of that has to be |
| 305 | /// small enough that nobody would trade it back: pictures reach the child |
| 306 | /// through a shared mapping rather than down a pipe, so a frame costs one |
| 307 | /// `memcpy` and a ten-byte message. `VE_HW_INPROCESS` pins the seat the way it |
| 308 | /// used to be, which is the only honest baseline there is. |
| 309 | /// |
| 310 | /// This is the *worst* case on purpose: an untouched clip's pictures never leave |
| 311 | /// the GPU at all (`a_decoded_buffer_is_encoded_without_ever_being_read_back`, |
| 312 | /// whose descriptors cross the same socket), so nothing an export really does is |
| 313 | /// slower than this. |
| 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 | /// Decode and encode resolve through two independent symbol tables, so an |
| 352 | /// encoder that cannot load must not cost us the decoder (and vice versa). |
| 353 | /// |
| 354 | /// This is the positive half only: the negative half -- a plugin built without |
| 355 | /// `vh_enc_*` -- needs a second build of the `.so` and belongs to the slice's |
| 356 | /// verifier, not here. |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | /// Walks the Annex B stream and reads, per slice, `(is_idr, frame_num, |
| 373 | /// pic_order_cnt_lsb)`, plus the SPS `level_idc`. Main profile, POC type 0 and |
| 374 | /// frame_mbs_only -- the SPS the vendored encoder writes -- is all this reads; |
| 375 | /// the test fails loudly on anything else rather than guessing. |
| 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 | /// The driver on this box writes `frame_num = 0` and `pic_order_cnt_lsb = 0` |
| 455 | /// into every slice it codes (mesa radeonsi 26.1.7, measured on three real |
| 456 | /// exports); phones' stateful decoders then judder every few seconds. The |
| 457 | /// plugin rewrites both, and this is the stream-level proof: over two GOPs the |
| 458 | /// counters step the way 7.4.3 wants, restart at the second IDR, and the level |
| 459 | /// is the one Table A-1 gives 1080p30 -- not the `L4` that used to be |
| 460 | /// hard-coded for every size. `HW_ENCODE_DUMP=<path>` keeps the stream for |
| 461 | /// `ffmpeg -bsf:v trace_headers`. |
| 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 |