edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
| 1 | //! Temporal MV projection (AV1 spec 7.9, `use_ref_frame_mvs`): projects a |
| 2 | //! previously decoded frame's stored motion vectors into the current |
| 3 | //! frame's own 8x8 motion field, ported from libaom's |
| 4 | //! `av1_setup_motion_field`/`motion_field_projection` |
| 5 | //! (`av1/common/mvref_common.c`) and the projection math `add_tpl_ref_mv` |
| 6 | //! (same file) applies a second time, per query block, against the block's |
| 7 | //! own single reference frame. |
| 8 | use cratehit; |
| 9 | |
| 10 | use crate; |
| 11 | |
| 12 | /// One 8x8 cell's saved motion state, as libaom's `av1_copy_frame_mvs` |
| 13 | /// leaves it after decoding the frame that owns this grid: the coded MV and |
| 14 | /// the single reference frame (`LAST_FRAME..=ALTREF_FRAME`, 1..=7) it |
| 15 | /// pointed to. |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | /// A decoded inter frame's whole motion field (spec `MotionFieldMvs`... no |
| 23 | /// — libaom's `cur_frame->mvs`), one [`SavedMv`] per 8x8 unit, plus the |
| 24 | /// order-hint bookkeeping [`setup_motion_field`] needs to project it into a |
| 25 | /// later frame: this frame's own `OrderHint` and the `OrderHint` of each of |
| 26 | /// the 7 references *it* was coded against (`SavedOrderHints`/ |
| 27 | /// `ref_order_hints`). |
| 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 | |
| 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 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | /// lane-mvstack3: one 8x8 temporal-MV cell -- the projected `(mv, |
| 101 | /// ref_frame_offset)` pair. `motion_field_projection` writes a cell only |
| 102 | /// with a STRICTLY POSITIVE `ref_frame_offset` (its own `pos_valid` test), |
| 103 | /// so `0` is exactly libaom's own "this `tpl_mvs` entry was never projected" |
| 104 | /// marker and the `Option` tag this used to carry was redundant: it cost 8 |
| 105 | /// bytes per cell (20 vs 12) over a field that is 2.6 MB at 4K, read up to |
| 106 | /// 19 times per inter block. |
| 107 | pub type TplCell = ; |
| 108 | |
| 109 | /// An unprojected [`TplCell`]. |
| 110 | const TPL_EMPTY: TplCell = ; |
| 111 | |
| 112 | /// The current frame's projected temporal motion field (spec 7.9.2's |
| 113 | /// `MotionFieldMvs`): one projected [`TplCell`] per 8x8 cell, its |
| 114 | /// `ref_frame_offset` left at `0` where no source candidate landed |
| 115 | /// (libaom's `INVALID_MV`). |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | /// spec `get_relative_dist` (5.9.3): `a - b` wrapped into the signed range |
| 134 | /// order hints roll over in at `order_hint_bits` width. |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | /// spec `FRAME_OFFSET_BITS`-derived clamp (libaom `MAX_FRAME_DISTANCE`). |
| 145 | const MAX_FRAME_DISTANCE: i32 = 31; |
| 146 | |
| 147 | /// libaom's `div_mult`: precomputed `16384 / den` (`av1_get_mv_projection`'s |
| 148 | /// own comment: "all the values are strictly under 14 bits"). |
| 149 | const DIV_MULT: = |
| 150 | |
| 151 | |
| 152 | ; |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | /// `av1_get_mv_projection` (spec 7.9.3): scales `mv` by the ratio of two |
| 164 | /// frame-distance offsets. |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | /// `lower_mv_precision` (`mvref_common.h`), both halves. lane-dgolomb: the |
| 178 | /// `is_integer` half used to be dropped as unreachable on the claim that |
| 179 | /// `allow_screen_content_tools` is refused at the stream level -- it is not, |
| 180 | /// and libaom turns screen-content tools on for dark, smooth FILM (its |
| 181 | /// `av1_set_screen_content_options` counts near-flat blocks), so a real |
| 182 | /// libaom stream carries `force_integer_mv == 1` on an inter frame and every |
| 183 | /// temporal MV candidate has to be rounded to full pel. Without it the |
| 184 | /// candidates were 1/8-pel values libaom never produces: a whole-frame |
| 185 | /// prediction mismatch behind a clean parse (class |
| 186 | /// `branch-dropped-as-unreachable`). |
| 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 | /// `get_block_position` (spec 7.9.2): the 8x8 cell a projected `mv` lands on |
| 224 | /// starting from `(blk_row, blk_col)` (already in 8x8 units), or `None` when |
| 225 | /// it falls outside the frame or outside the projection's own bound (a |
| 226 | /// 64x64-superblock-aligned base block, spec's `MAX_OFFSET_WIDTH`/ |
| 227 | /// `MAX_OFFSET_HEIGHT`). |
| 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 | /// `motion_field_projection` (spec 7.9.2): projects `start`'s stored MVs |
| 272 | /// into `tpl`, scaled by the OrderHint distance from `start` to the current |
| 273 | /// frame. `dir` mirrors libaom's own parameter: `2` for a backward-looking |
| 274 | /// source (`LAST_FRAME`/`LAST2_FRAME`, offset negated and `sign_bias` |
| 275 | /// flipped), `0` for a forward one (`BWDREF_FRAME`/`ALTREF2_FRAME`/ |
| 276 | /// `ALTREF_FRAME`). Returns whether the projection ran at all (`start`'s |
| 277 | /// frame size has to match the current frame's — always true for this |
| 278 | /// decoder's fixed-size streams, but ported anyway). |
| 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 | |
| 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 | thread_local! |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | /// Reads the counter above (gate |
| 366 | /// `a_real_aomenc_inter_sequence_with_forward_keyframes_and_temporal_mvs_decodes_pixel_exact`). |
| 367 | // read only from the `#[cfg(test)]` gates |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | /// `av1_setup_motion_field` (spec 7.9.2's own driver): scans up to |
| 376 | /// [`MFMV_STACK_SIZE`]`= 3` reference-frame slots in libaom's fixed order |
| 377 | /// (`LAST_FRAME`, then `BWDREF_FRAME`/`ALTREF2_FRAME`/`ALTREF_FRAME` when |
| 378 | /// each is display-order-forward of the current frame, then `LAST2_FRAME` |
| 379 | /// once the stack still has room) and projects each into the returned |
| 380 | /// [`TplField`]. |
| 381 | /// |
| 382 | /// `slots` is the DPB's 8 reference-picture slots' saved [`MotionField`]s |
| 383 | /// (a `None` slot, or one still holding an intra frame's — never |
| 384 | /// constructed, so also `None` — motion field, contributes nothing); |
| 385 | /// `ref_frame_idx` is the current frame header's own `ref_frame_idx` |
| 386 | /// (`LAST_FRAME..=ALTREF_FRAME`, indexed `[i]` for `LAST_FRAME + i`). |
| 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 | /// The result [`add_tpl_ref_mv`] folds into a query block's MV stack: the |
| 532 | /// projected candidate MV (already scaled to the block's own reference |
| 533 | /// frame and MV-precision-lowered) plus whether the very first (`blk_row == |
| 534 | /// 0 && blk_col == 0`) sample differs enough from the zero vector to raise |
| 535 | /// `GLOBALMV_OFFSET` (spec 7.10.2.8 — this decoder only ever reaches |
| 536 | /// `IDENTITY` global motion, so the spec's `gm_get_motion_vector` compare |
| 537 | /// collapses to "far from `(0, 0)`"). |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | /// `add_tpl_ref_mv`'s single-reference half (spec 7.10.2.8, libaom |
| 543 | /// `mvref_common.c`): probes one 8x8-unit offset `(blk_row, blk_col)` from |
| 544 | /// `(mi_row, mi_col)` in [`TplField`] and, if it landed a candidate, |
| 545 | /// projects it a second time — this time scaled to `cur_offset_0`, the |
| 546 | /// distance from the current frame to the query block's own single |
| 547 | /// reference frame, rather than `start_to_current_frame_offset` from the |
| 548 | /// first (storage-time) projection. |
| 549 | // Signature mirrors the spec's per-call inputs; a parameter struct would only move them. |
| 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 | /// The projection half of [`add_tpl_ref_mv`], split out so a caller holding a |
| 584 | /// [`TplProbe`] cell can project it to more than one reference offset without |
| 585 | /// reading the field twice (lane-tpl: the compound stack does exactly that). |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | /// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s |
| 599 | /// parity derivation and row base are the same for every `(blk_row, |
| 600 | /// blk_col)` the mv stack probes, so they are hoisted here and the per-sample |
| 601 | /// cost drops to an add, a shift and the bounds check: `row8 = mi_row + |
| 602 | /// pos_row` with `pos_row = blk_row + (mi_row even)` means the cell index is |
| 603 | /// `(mi_row + 1 - (mi_row & 1) + blk_row) >> 1`, and an arithmetic shift |
| 604 | /// keeps the spec's `row8 < 0` rejection exact (`row8 < 0` iff the shifted |
| 605 | /// index is negative). |
| 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 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 |