edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBfef26dfTahinli2 months ago3246 commits
| 1 | //! Inverse transforms (inverse DCT, inverse Walsh-Hadamard) and dequant |
| 2 | //! factor computation for VP8 (RFC 6386 §14). |
| 3 | //! |
| 4 | //! Every function here is a bit-exact transcription of the normative |
| 5 | //! reference implementation in libvpx (`vp8/common/idctllm.c` and |
| 6 | //! `vp8/common/quant_common.c`), which is also what RFC 6386 §§14.3–14.5 |
| 7 | //! print as pseudocode. Bit-level fidelity of the rounding is a hard |
| 8 | //! requirement: arithmetic right shifts (`>>` on `i32` in Rust, as in C) |
| 9 | //! round toward negative infinity, and both implementations rely on that |
| 10 | //! floor behavior for negative operands. |
| 11 | //! |
| 12 | //! Fidelity details reproduced verbatim from the oracle: |
| 13 | //! - The 16-bit fixed-point constants `cospi8sqrt2minus1 = 20091` and |
| 14 | //! `sinpi8sqrt2 = 35468` (§14.4). The `>> 16` multiplies carry **no** |
| 15 | //! rounding constant; the `+4` before the final `>> 3` appears only in |
| 16 | //! the second DCT pass. |
| 17 | //! - `short output[16]` intermediates: libvpx stores the first-pass |
| 18 | //! results into a `short` array, truncating `i32` arithmetic to `i16` |
| 19 | //! between passes. We reproduce those truncation points with `as i16` |
| 20 | //! (wrapping two's-complement truncation, identical to the de facto C |
| 21 | //! behavior) so results stay oracle-exact even for adversarial inputs. |
| 22 | //! - The inverse WHT rounds with `(x + 3) >> 3` (§14.3); the DC-only |
| 23 | //! shortcut uses the same constant on `input[0]` alone. |
| 24 | //! |
| 25 | //! All functions are pure, allocation-free, and free of `unsafe`. |
| 26 | |
| 27 | |
| 28 | |
| 29 | /// 16-bit fixed-point `sqrt(2) * cos(pi/8) - 1` (RFC 6386 §14.4). The |
| 30 | /// product `x * sqrt(2) * cos(pi/8)` is computed as `x + ((x * K) >> 16)` |
| 31 | /// because the constant exceeds 1.0 and would otherwise lose precision. |
| 32 | const COSPI8SQRT2MINUS1: i32 = 20091; |
| 33 | |
| 34 | /// 16-bit fixed-point `sqrt(2) * sin(pi/8)` (RFC 6386 §14.4). |
| 35 | const SINPI8SQRT2: i32 = 35468; |
| 36 | |
| 37 | /// DC quantizer lookup, `dc_qlookup[QINDEX_RANGE]` (RFC 6386 §14.1), |
| 38 | /// transcribed mechanically from libvpx `vp8/common/quant_common.c`. |
| 39 | const DC_QLOOKUP: = |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | ; |
| 47 | |
| 48 | /// AC quantizer lookup, `ac_qlookup[QINDEX_RANGE]` (RFC 6386 §14.1), |
| 49 | /// transcribed mechanically from libvpx `vp8/common/quant_common.c`. |
| 50 | const AC_QLOOKUP: = |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | ; |
| 58 | |
| 59 | /// Clamps a (possibly delta-adjusted) quantizer index to the valid |
| 60 | /// qindex range `[0, 127]` — dixie `clamp_q` / libvpx bounds in |
| 61 | /// `quant_common.c` (RFC 6386 §14.1). |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | /// `dc_qlookup[clamp(q + delta)]` — Y-after-second / per-plane DC factor |
| 68 | /// lookup with delta adjustment (RFC 6386 §14.1 `dc_q`). |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | /// `ac_qlookup[clamp(q + delta)]` — AC factor lookup with delta |
| 75 | /// adjustment (RFC 6386 §14.1 `ac_q`). |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | /// Inverse Walsh-Hadamard transform of the 16 dequantized Y2 DC |
| 82 | /// coefficients into the 16 Y-subblock DC residuals |
| 83 | /// (RFC 6386 §14.3 `vp8_short_inv_walsh4x4_c`, oracle |
| 84 | /// `vp8_short_inv_walsh4x4_c` in `idctllm.c`). |
| 85 | /// |
| 86 | /// `input` is the row-major 4x4 block of dequantized Y2 DCs; the returned |
| 87 | /// array holds the 16 Y-subblock DC values in the same row-major order |
| 88 | /// (the reference scatters `output[i]` to `mb_dqcoeff[i * 16]`, i.e. the |
| 89 | /// DC slot of the i-th Y subblock — the caller places them). Two passes |
| 90 | /// of the exact 4-point Hadamard butterfly, with `short` truncation of |
| 91 | /// the first-pass results and `(x + 3) >> 3` rounding in the second. |
| 92 | |
| 93 | |
| 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 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | /// One-nonzero-DC shortcut of the inverse WHT |
| 136 | /// (RFC 6386 §14.3 `vp8_short_inv_walsh4x4_1_c`, oracle |
| 137 | /// `vp8_short_inv_walsh4x4_1_c` in `idctllm.c`). |
| 138 | /// |
| 139 | /// When only `input[0]` is non-zero, every Y-subblock DC equals |
| 140 | /// `(input[0] + 3) >> 3`. Bit-identical to `iwht4x4` for such inputs. |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// Full inverse DCT of one dequantized 4x4 coefficient block |
| 148 | /// (RFC 6386 §14.4; oracle `short_idct4x4llm_c` in `idctllm.c`). |
| 149 | /// |
| 150 | /// `coeffs` is a row-major 4x4 block of **already dequantized** |
| 151 | /// coefficients (dequantization happens before the transform, RFC 6386 |
| 152 | /// §14.5). Two passes of the 4-point inverse DCT with the §14.4 |
| 153 | /// fixed-point constants; the `>> 16` multiplies carry no rounding |
| 154 | /// constant and the `(x + 4) >> 3` rounding is applied only in the |
| 155 | /// second pass. First-pass results are truncated to `i16` exactly where |
| 156 | /// the oracle stores into its `short output[16]` scratch. |
| 157 | /// |
| 158 | /// Returns the row-major 4x4 residual. Not clamped, not added to the |
| 159 | /// prediction — see [`idct4x4_add`] for the combined form. |
| 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 | /// DC-only inverse DCT shortcut (RFC 6386 §14.4 |
| 216 | /// `vp8_dc_only_idct_add_c` math; oracle `idctllm.c`): when position 0 is |
| 217 | /// the sole non-zero coefficient, every residual pixel is `(dc + 4) >> 3`. |
| 218 | /// |
| 219 | /// Bit-identical to `idct4x4` on such inputs. Not clamped; the caller |
| 220 | /// adds and clamps (or uses [`idct4x4_add`]). |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | /// Combined per-block reconstruction kernel (RFC 6386 §14.4/§14.5; oracle |
| 228 | /// `short_idct4x4llm_c` final loop in `idctllm.c`): inverse-transforms |
| 229 | /// `coeffs` and writes `clamp(predict + residual)` into `recon`. |
| 230 | /// |
| 231 | /// `coeffs` must already be dequantized (dequantization is applied before |
| 232 | /// the transform; for intra Y macroblocks the DC positions already hold |
| 233 | /// the [`iwht4x4`] output). `predict` and `recon` are row-major 4x4 |
| 234 | /// blocks of exactly 16 bytes each. Residuals are fully computed before |
| 235 | /// any output byte is written. |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | /// Dequantization factors for one macroblock, indexed by plane and |
| 246 | /// coefficient type (RFC 6386 §14.1; dixie `struct dequant_factors`). |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | /// Computes the six dequantization factors for quantizer index `q` with |
| 264 | /// the per-frame delta updates (RFC 6386 §9.6 header fields applied per |
| 265 | /// §14.1; dixie `dequant_init`, oracle `vp8cx_init_quantizer` in |
| 266 | /// libvpx `decodframe.c` / `quant_common.c`). |
| 267 | /// |
| 268 | /// `q` is the per-macroblock segment-adjusted base qindex (already |
| 269 | /// absolute or offset by the segment level per the segment header); the |
| 270 | /// delta arguments are the frame-header updates. Each lookup clamps its |
| 271 | /// `q + delta` sum to `[0, 127]`. Semantics transcribed exactly: |
| 272 | /// - `y1_dc = dc_qlookup[clamp(q + y1_dc_delta)]`, |
| 273 | /// `y1_ac = ac_qlookup[clamp(q)]` (no delta exists for Y AC), |
| 274 | /// - `y2_dc = dc_qlookup[clamp(q + y2_dc_delta)] * 2`, |
| 275 | /// - `y2_ac = ac_qlookup[clamp(q + y2_ac_delta)] * 155 / 100` (integer |
| 276 | /// division), floored at 8, |
| 277 | /// - `uv_dc = min(dc_qlookup[clamp(q + uv_dc_delta)], 132)` (cap applied |
| 278 | /// after the lookup), |
| 279 | /// - `uv_ac = ac_qlookup[clamp(q + uv_ac_delta)]`. |
| 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 | |
| 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 | |
| 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 |