edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB8dbd683Tahinli3 months ago856 commits
| 1 | //! The integer transforms and the quantizer (8.6.2 - 8.6.4). |
| 2 | //! |
| 3 | //! The *inverse* path here is normative: a decoder does exactly this, so the |
| 4 | //! encoder's reconstruction runs the same code and is bit-identical to what any |
| 5 | //! conformant decoder produces. The forward path and the quantizer are the |
| 6 | //! encoder's own choice; what makes them correct is that they invert the |
| 7 | //! normative path — which is what [`tests::round_trip_recovers_flat_blocks`] |
| 8 | //! and the the oracle conformance test in `tests/` check. |
| 9 | //! |
| 10 | //! One transcription safeguard is worth naming: only the first 16 columns of the |
| 11 | //! 32x32 matrix are typed in. The rest follow from `M[m][31 - n] = (-1)^m M[m][n]`, |
| 12 | //! which the spec's own second half satisfies, and a test asserts that half back |
| 13 | //! entry by entry. A wrong column is the classic defect in this kind of table |
| 14 | //! (it cost this family a week in H.264), so the table is checked against |
| 15 | //! orthogonality as well as against the printed values. |
| 16 | |
| 17 | /// Columns 0..15 of `transMatrix` (8-319), rows 0..31. |
| 18 | /// |
| 19 | /// The size-N transform uses rows `k * (32 / N)` and columns `0..N`. |
| 20 | |
| 21 | const TRANS_MATRIX_HALF: = |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | ; |
| 55 | |
| 56 | /// `transMatrix` for `trType = 1`: the 4x4 DST used for luma intra 4x4 blocks |
| 57 | /// (8-316). |
| 58 | |
| 59 | const DST_MATRIX: = |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | ; |
| 65 | |
| 66 | /// `M[m][n]` of the 32-point matrix, mirrored out of the stored half. |
| 67 | |
| 68 | const |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | /// The full 32x32 matrix, mirrored out at compile time so the hot butterfly |
| 79 | /// loops below index a flat table instead of taking [`m32`]'s per-element |
| 80 | /// branch: same values (this *is* [`m32`], evaluated ahead of time), fewer |
| 81 | /// instructions per multiply-add. |
| 82 | const |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | const FULL_MATRIX: = build_full_matrix; |
| 96 | |
| 97 | /// [`FULL_MATRIX`] transposed: `FULL_MATRIX_T[col][row] == FULL_MATRIX[row][col]`. |
| 98 | /// [`inverse_1d`]'s odd sum walks a fixed column across varying rows, which |
| 99 | /// this makes a contiguous read. |
| 100 | const |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | const FULL_MATRIX_T: = transpose; |
| 114 | |
| 115 | /// `levelScale[k]`, 8.6.3. |
| 116 | const LEVEL_SCALE: = ; |
| 117 | |
| 118 | /// The quantizer's reciprocal of [`LEVEL_SCALE`], `round(2^20 / levelScale)`. |
| 119 | /// |
| 120 | /// Derived rather than transcribed: the product of the two is 2^20 to within a |
| 121 | /// count, which is the whole design of the HEVC quantizer, and |
| 122 | /// [`tests::quant_scale_is_the_reciprocal`] holds it to that. |
| 123 | const QUANT_SCALE: = ; |
| 124 | |
| 125 | /// Largest and smallest coefficient a Main-profile decoder may hold (`CoeffMin`, |
| 126 | /// `CoeffMax` for `extended_precision_processing_flag = 0`). |
| 127 | const COEFF_MIN: i32 = -32_768; |
| 128 | const COEFF_MAX: i32 = 32_767; |
| 129 | |
| 130 | /// Dot product of two equal-length slices, SIMD in 8-wide chunks with a |
| 131 | /// scalar tail. |
| 132 | /// |
| 133 | /// Both `forward_1d` and `inverse_1d`'s odd sums are `sum_j m[j] * x[j]` over |
| 134 | /// at most 16 terms; matrix entries are `|m| <= 90` and `x` is a coefficient |
| 135 | /// or residual sample clamped to `COEFF_MIN..=COEFF_MAX` (+/-32768), so each |
| 136 | /// product is bounded by ~3.0e6 and the full sum of 16 by ~4.7e7 — nowhere |
| 137 | /// near overflowing i32, whether accumulated in one lane or spread across |
| 138 | /// eight and reduced at the end. |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | /// One-dimensional inverse transform, `y[i] = sum_j M[j][i] * x[j]`. |
| 158 | /// |
| 159 | /// Split even/odd once per level: the even rows of the size-N matrix *are* the |
| 160 | /// size-N/2 matrix, so the recursion costs ~N^2/3 multiplies where the plain |
| 161 | /// product costs N^2. |
| 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 | /// One-dimensional forward transform, `c[k] = sum_x M[k][x] * src[x]`. |
| 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 | /// Forward 4x4 DST (the inverse of the normative `trType = 1` transform). |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | /// Inverse 4x4 DST, `y[i] = sum_j M[j][i] * x[j]` (8-315). |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | /// True when a block takes the DST: luma, intra, 4x4 (8.6.4.1). |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | /// Forward transform of an `n x n` residual block into coefficients. |
| 240 | /// |
| 241 | /// `residual` and `coeffs` are both row-major `n x n`; `coeffs[v * n + k]` is |
| 242 | /// vertical frequency `v`, horizontal frequency `k`, which is the order the |
| 243 | /// scans and the residual coder index. |
| 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 | /// `bdShift` (8-299) at 8-bit: 20 - BitDepth. |
| 290 | const BD_SHIFT: i32 = 12; |
| 291 | |
| 292 | /// Inverse transform of scaled coefficients into residual samples, exactly as |
| 293 | /// clause 8.6.4 specifies including its intermediate clipping. |
| 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 | |
| 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 | /// Forward transform skip (8.6.2): no transform at all, the residual samples |
| 346 | /// standing in for coefficients. |
| 347 | /// |
| 348 | /// The decoder scales what it reads by `tsShift = 5 + Log2(nTbS)` and then |
| 349 | /// takes the bit-depth shift of 20 - 8 off it again, so a coefficient has to |
| 350 | /// carry the residual scaled by the difference for the round trip to return |
| 351 | /// the sample it started from — a factor of 32 at 4x4, the same gain the |
| 352 | /// integer transform leaves behind. |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | /// Inverse transform skip (8.6.2): the scaled coefficient shifted up by |
| 361 | /// `tsShift` and then down by the bit-depth shift, exactly as the decoder |
| 362 | /// does it, so the encoder reconstructs the samples the decoder will. |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | /// Quantise coefficients in place, returning the number of non-zero levels. |
| 372 | /// |
| 373 | /// The rounding offset is a third of a step, which is the intra bias every |
| 374 | /// encoder in this class uses: it costs a little distortion at the rounding |
| 375 | /// boundary and buys back more in rate. |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | /// [`quantize`] with the rounding written out: `dead_zone` is the divisor of |
| 381 | /// the quantiser step the rounding offset is, so 3 is the intra default and 2 |
| 382 | /// is round-to-nearest. |
| 383 | /// |
| 384 | /// A rate-distortion search over the levels wants the wider rounding: a search |
| 385 | /// that only ever takes levels down measures the rounding it started from, not |
| 386 | /// its own decisions. |
| 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 | /// The unrounded level a coefficient would quantise to: the quantiser divides |
| 435 | /// by a step, and this is that division carried out in full precision. Sign |
| 436 | /// data hiding needs it to tell which coefficient is cheapest to nudge. |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | /// Scale levels back into coefficients, exactly as clause 8.6.3 specifies. |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | /// One level scaled back to a coefficient, the per-coefficient half of |
| 452 | /// [`dequantize`]. A rate-distortion search over the levels prices one |
| 453 | /// coefficient at a time and has no use for the whole block. |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | /// What a squared error in the transform domain is worth as a squared error in |
| 465 | /// the sample domain. |
| 466 | /// |
| 467 | /// The forward transform's shifts leave the DC at 128 times the mean residual |
| 468 | /// whatever the block size, where an orthonormal transform would leave it at |
| 469 | /// `n` times; the transform is otherwise orthogonal, so coefficient error |
| 470 | /// energy is `(128 / n)^2` times sample error energy and this undoes it. A |
| 471 | /// rate-distortion search over the levels needs it to weigh transform-domain |
| 472 | /// error against a lambda calibrated on samples. |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | /// The chroma QP a luma QP maps to for 4:2:0 (Table 8-10), before any offset. |
| 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 |