edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
| 1 | //! Deblocking filters: the edge filters, the deblock plane drivers, the filter-replay capture and the pooled plane scratch. Moved out of `decode/mod.rs` (av1split D12); the items are unchanged apart from visibility. |
| 2 | |
| 3 | use SimdToken; |
| 4 | use *; |
| 5 | |
| 6 | /// `update_sharpness`'s per-level `(blimit, limit, hev_thr)` triple. |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | // read only from the `#[cfg(test)]` gates |
| 19 | pub |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | /// `filter4`: the narrow 4-tap kernel, and every wider kernel's non-flat |
| 31 | /// fallback. |
| 32 | pub |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | /// `filter6`: chroma's flat 5-tap smoothing (spec `[1,2,2,2,1]`) over |
| 65 | /// `filter4`'s narrow taps. |
| 66 | |
| 67 | pub |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | /// `filter8`: luma's flat 7-tap smoothing (spec `[1,1,1,2,1,1,1]`). |
| 92 | |
| 93 | pub |
| 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 | /// `filter14`: the wide 13-tap smoothing used at 16x16-and-larger transform |
| 123 | /// edges; falls back to [`filter8`] (leaving `op6`/`oq6` untouched either |
| 124 | /// way -- they are only ever taps here, never written). |
| 125 | |
| 126 | pub |
| 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 | /// Filters one 4-pixel edge group: `base` is the plane-local byte offset of |
| 200 | /// the first (`q0`) sample past the edge, `outer_stride` advances to the |
| 201 | /// next of the 4 perpendicular samples, `tap_stride` steps from one tap to |
| 202 | /// the next along the edge's own filtering direction. |
| 203 | |
| 204 | pub |
| 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 | |
| 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 | |
| 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 | |
| 317 | |
| 318 | /// lane-perf8: [`filter_edge`]'s AVX2 kernel -- the scalar loop was 8.0% of a |
| 319 | /// 4K decode's self time, spent almost entirely on the per-tap strided |
| 320 | /// addressing (`perf annotate`'s hot lines are `leaq`/`movzwl` pairs, not the |
| 321 | /// arithmetic). Nothing here is spec logic: the same masks, the same |
| 322 | /// `filter4`/`filter6`/`filter8`/`filter14` weighted sums and the same |
| 323 | /// rounding, done for 16 edge lines at once. |
| 324 | /// |
| 325 | /// 16-bit lanes are exact for every value these filters see. Samples are in |
| 326 | /// `[0, 4095]`, so a difference and the `blimit` term (`2 * |p0 - q0| + |
| 327 | /// |p1 - q1| / 2 <= 10237`) fit `i16`; `filter4` works in the centred domain |
| 328 | /// where every intermediate is clamped to `+-128 << (bit_depth - 8)`; and the |
| 329 | /// wide kernels' weights each total 8 or 16, so their sums (at most |
| 330 | /// `16 * 4095 + 8 = 65528`) fit `u16` -- hence `_mm256_srli_epi16` for their |
| 331 | /// rounding shift, which is the unsigned shift the scalar's non-negative sum |
| 332 | /// takes. |
| 333 | /// [`filter_edge`] for 16 adjacent edge lines that share `edge_params`' |
| 334 | /// output. Returns `false` when this build or CPU has no kernel, leaving the |
| 335 | /// caller on the scalar path. |
| 336 | |
| 337 | // Signature mirrors the spec's per-call inputs; a parameter struct would only move them. |
| 338 | |
| 339 | pub |
| 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 | /// One plane's worth of spec 7.14: every vertical edge across the plane, |
| 372 | /// then every horizontal edge (the order the spec and libaom both use). |
| 373 | pub |
| 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 | /// One pass of [`deblock_plane`] -- vertical edges (`horz == false`) or |
| 411 | /// horizontal ones -- restricted to the plane rows `span_lo..span_hi`. |
| 412 | /// |
| 413 | /// A row span is a legal cut of either pass because a band boundary is a |
| 414 | /// multiple of 16 rows and every edge's kernel stays inside its own |
| 415 | /// 4-sample group's reach. |
| 416 | |
| 417 | pub |
| 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 | |
| 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 | /// lane-perf6: reused whole-plane `u16` buffers. Every frame takes a |
| 705 | /// pre-CDEF snapshot of all three planes, a post-deblock snapshot when loop |
| 706 | /// restoration is active, and a fresh LR destination per plane -- freshly |
| 707 | /// allocated that is ~25 MB of `mmap`/`munmap` per frame at 4K, so every |
| 708 | /// sample written also takes a first-touch page fault. The buffers are handed |
| 709 | /// back here instead and refilled in place. |
| 710 | /// |
| 711 | /// lane-memfix: PROCESS-WIDE, not one stash per thread. These buffers do not |
| 712 | /// stay on one thread: the frame thread allocates the snapshots and a filter |
| 713 | /// worker returns them (the picture's pre-LR planes go back into the pool |
| 714 | /// from whichever worker ran the restoration), so a per-thread pool leaked |
| 715 | /// whole-plane buffers into every worker it touched -- 19 MB a set, times |
| 716 | /// every pool thread the decode ever created. One shared pool with a hard |
| 717 | /// entry cap bounds the retention at [`SCRATCH_CAP`] buffers no matter how |
| 718 | /// many threads cycle through it; anything over the cap is freed. |
| 719 | pub static PLANE_SCRATCH: Mutex = new; |
| 720 | |
| 721 | /// Most buffers the shared pool keeps. ~3 whole-plane sets per in-flight 4K |
| 722 | /// frame at the shipped `EC_AV1_THREADS` ceiling; the frames actually in |
| 723 | /// flight cycle through far fewer. |
| 724 | pub const SCRATCH_CAP: usize = 32; |
| 725 | |
| 726 | /// Takes a pooled buffer for a caller that wants `n` samples, best-fit by |
| 727 | /// CAPACITY so a chroma-sized request does not permanently promote a |
| 728 | /// luma-sized allocation (every pooled buffer would drift to the largest |
| 729 | /// size otherwise). `None` when the pool is empty. |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | /// A pooled buffer holding a copy of `src`. |
| 755 | pub |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | /// An empty pooled buffer, for a caller that fills it itself. |
| 763 | pub |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | /// A pooled buffer of exactly `n` samples whose contents are NOT cleared -- |
| 770 | /// for a caller that writes every element it later reads (lane-copy: the |
| 771 | /// post-deblock snapshot writes 4 rows in 64 and reads no other). |
| 772 | pub |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | pub |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | /// The pre-filter reconstruction of one coded frame, plus the per-block state |
| 800 | /// the loop filters read. |
| 801 | /// |
| 802 | /// The encoder's filter search ([`crate::filter_search::pick_filters`]) |
| 803 | /// decodes the same coded tile under ~25 candidate deblock/CDEF parameter |
| 804 | /// sets to score each one, and the entropy decode and reconstruction it |
| 805 | /// repeats are identical every time -- both filters run after it, on the |
| 806 | /// finished planes. That re-parse was 24% of the encoder's profile. A caller |
| 807 | /// arms [`arm_filter_replay`] around ONE decode and then re-runs the filters |
| 808 | /// alone through [`replay_filters`]; the result is the same picture the full |
| 809 | /// decode would have returned, because it is the same two calls on a copy of |
| 810 | /// the same planes. |
| 811 | pub |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | thread_local! |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | /// Capture this thread's next decode's pre-filter frame (see [`FilterReplay`]). |
| 872 | pub |
| 873 | |
| 874 | |
| 875 | |
| 876 | /// Test-only: force every filter candidate through a full decode, so a gate |
| 877 | /// can hold the replay against the decode it stands in for |
| 878 | /// (`encoder::tests::filter_replay_codes_the_same_stream_as_a_full_decode`). |
| 879 | |
| 880 | pub static REPLAY_DISABLED: AtomicBool = new; |
| 881 | |
| 882 | |
| 883 | pub |
| 884 | |
| 885 | |
| 886 | |
| 887 | // Test-only: run the capture decode `replay_final` replaced as well, and |
| 888 | // hold its picture and its `FilterStages` against the replay's |
| 889 | // (`encoder::tests::filter_replay_final_matches_the_capture_decode`). |
| 890 | // Thread-local, not a global: the filter stage runs on the frame thread the |
| 891 | // test itself drives, and a process-wide flag would put every test encoding |
| 892 | // in parallel with this one through the extra decode too. |
| 893 | |
| 894 | thread_local! |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | pub |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | pub |
| 905 | |
| 906 | |
| 907 | |
| 908 | /// Drop the armed flag and anything captured: the tile it belongs to is done. |
| 909 | pub |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | /// The capture itself, called from the decoder's own pre-filter point. Owns |
| 915 | /// copies of the planes, since the caller filters them in place. |
| 916 | // Signature mirrors the spec's per-call inputs; a parameter struct would only move them. |
| 917 | |
| 918 | pub |
| 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 | /// Restores, from the post-deblock cache, every sample the previous CDEF |
| 960 | /// candidate may have written: for each unit row, the column span between |
| 961 | /// its first and last written unit (a superset of the written units, which |
| 962 | /// is what makes the copy contiguous -- and never more than the whole-plane |
| 963 | /// copy it replaces). `unit` is this plane's CDEF unit side, 8 for luma and |
| 964 | /// 4 for 4:2:0 chroma. |
| 965 | pub |
| 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 | /// One candidate's working copy of a captured plane, off [`FilterReplay`]'s |
| 1018 | /// own buffer pool: a cleared `Vec` that already has the capacity keeps the |
| 1019 | /// allocator and the page fault handler out of the filter search. |
| 1020 | pub |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 |