edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBf44b458Tahinli3 months ago649 commits
| 1 | //! The inverse transform (spec 7.13), so the encoder can reconstruct exactly |
| 2 | //! what a decoder will show. |
| 3 | //! |
| 4 | //! Only the inverse DCT is here, which is all this encoder's subset codes: a |
| 5 | //! square `DCT_DCT` transform per block. The butterfly network is the spec's |
| 6 | //! own, stage for stage — every stage's rounding and intermediate clamp is |
| 7 | //! part of the bitstream contract, so a mathematically equal but differently |
| 8 | //! rounded DCT would drift away from the decoder by a sample here and there. |
| 9 | |
| 10 | /// `Cos128_Lookup` (spec 7.13.2.1): `4096 * cos(angle * pi / 128)` rounded. |
| 11 | static COS128_LOOKUP: = |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | ; |
| 17 | |
| 18 | /// The fixed-point position of the cosine table. |
| 19 | const COS_BITS: usize = 12; |
| 20 | |
| 21 | /// `cos128(angle)` (spec 7.13.2.1), folded from the quarter table. |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | /// `sin128(angle)` (spec 7.13.2.1). |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | /// `brev(numBits, x)` (spec 7.13.2.1): the bit reversal of the low `num_bits`. |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | /// `Round2(x, n)` (spec 4.7): shift down `n` places, rounding halves up. |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | /// `Clip3` against a signed integer of `r` bits, which is what bounds every |
| 54 | /// intermediate of the transform. |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | /// `B(a, b, angle, flip, r)` (spec 7.13.2.1): a butterfly rotation, and an |
| 62 | /// exchange of the two entries when `flip` is set. |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | /// `H(a, b, flip, r)` (spec 7.13.2.1): a Hadamard rotation, with the indices |
| 76 | /// exchanged when `flip` is set. |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | /// The inverse DCT array permutation (spec 7.13.2.2): an in-place bit-reversal |
| 85 | /// of the first `1 << n` entries. |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | /// The inverse DCT process (spec 7.13.2.3): an in-place inverse DCT of the |
| 95 | /// first `1 << n` entries of `t`, clamping intermediates to `r` bits. |
| 96 | /// |
| 97 | /// The stage list is the spec's, in the spec's order; the guards on `n` are |
| 98 | /// what make one network serve every transform size from 4 to 64. |
| 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 | |
| 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 | /// `Transform_Row_Shift` (spec 7.13.3) for the square transforms. |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | /// The 2D inverse transform (spec 7.13.3) for a square `DCT_DCT` block. |
| 293 | /// |
| 294 | /// `dequant` is the dequantized coefficient grid in raster order; the returned |
| 295 | /// residual is in the same order. A 64-point transform only ever carries |
| 296 | /// coefficients in its first 32 rows and columns, and the spec zeroes the rest |
| 297 | /// before the row transform, which is what the `< 32` guard does. |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | /// Dequantize (spec 7.12.3) and inverse transform one square `DCT_DCT` block, |
| 336 | /// returning its residual in raster order. |
| 337 | /// |
| 338 | /// This is the encoder's model of what the decoder will add to its prediction, |
| 339 | /// so it follows the spec's dequantization exactly, truncation toward zero and |
| 340 | /// all. |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | /// The orthonormal DCT-II basis row for output `u` of an `n`-point transform. |
| 347 | /// |
| 348 | /// The decoder's network is this basis scaled by a constant the encoder has to |
| 349 | /// undo, and nothing else: measuring the network's response to a unit |
| 350 | /// coefficient at every size gives the same `1 / (8 * sqrt(2))` per unit of |
| 351 | /// `dqDenom`, which is what [`forward_transform_2d`] divides out. |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | /// [`build_dct_basis`], computed once per transform size and reused: the |
| 366 | /// search calls [`forward_transform_2d`] for every mode of every block, and |
| 367 | /// the basis does not depend on the residual, only on `n` -- recomputing |
| 368 | /// `n * n` cosines per call was measured as this search's single largest |
| 369 | /// per-trial cost (`stage_timing_breakdown`, ec-av1 perf lane). `n` is always |
| 370 | /// one of the transform sizes this crate codes (4 to 64, a power of two), so |
| 371 | /// a small fixed table indexed by `log2(n)` covers every caller without a |
| 372 | /// hash lookup. |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | /// How much the decoder's inverse transform shrinks an orthonormal DCT. |
| 383 | /// |
| 384 | /// Measured, not asserted: feeding a single dequantized coefficient through |
| 385 | /// `inverse_transform_2d` reproduces the orthonormal basis function scaled by |
| 386 | /// `dq_denom(side) / 8` at every size (see `the_inverse_network_is_an_orthonormal_dct_over_eight`). |
| 387 | /// The dequantizer has already divided by `dq_denom`, so the two cancel and |
| 388 | /// what an encoder owes a level is size-independent: |
| 389 | /// `level = 8 * orthonormal(residual) / q`. |
| 390 | /// |
| 391 | /// The spec fixes the decoder; the encoder is free in how it reaches the |
| 392 | /// coefficients it sends, and this constant is the one thing the two ends have |
| 393 | /// to agree on for a level to mean what the encoder thinks it means. |
| 394 | const INVERSE_GAIN_RECIPROCAL: f64 = 8.0; |
| 395 | |
| 396 | /// The forward transform (encoder side, spec-free): the transpose of what the |
| 397 | /// decoder's inverse does, so that `inverse_transform_2d` of the result |
| 398 | /// reproduces `residual`. |
| 399 | /// |
| 400 | /// AV1 specifies only the inverse transform. The encoder may reach its |
| 401 | /// coefficients however it likes, and this reaches them the direct way — an |
| 402 | /// orthonormal DCT-II in double precision, scaled to the decoder's fixed-point |
| 403 | /// gain. It is deterministic, and its accuracy is measured against the |
| 404 | /// decoder's own inverse rather than asserted. |
| 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 | /// Quantize forward-transform coefficients into the levels the tile syntax |
| 457 | /// carries. |
| 458 | /// |
| 459 | /// `deadzone` is the fraction of a quantizer step a coefficient has to reach |
| 460 | /// before it is coded at all, as a rounding offset: 0.5 rounds to nearest, |
| 461 | /// smaller values pull coefficients toward zero, which is what buys the rate |
| 462 | /// back on noisy content. A 64-point transform only carries its top-left |
| 463 | /// 32x32, so everything outside that is dropped here rather than silently by |
| 464 | /// the writer. |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | /// Transform and quantize one block's residual, the encoder's half of the |
| 489 | /// round trip [`dequant_and_inverse`] completes. |
| 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 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 |