edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits
| 1 | //! Integer-then-subpel motion search for one inter block over one reference |
| 2 | //! plane (spec 7.10's `NEWMV` case, the search a real encoder needs to *find* |
| 3 | //! that MV; the syntax that codes it lives in [`crate::cdf`] and the tile |
| 4 | //! writer, neither of which this module touches). |
| 5 | //! |
| 6 | //! The search runs in three stages, each over [`crate::mc::predict`]'s own |
| 7 | //! sub-pel filter (so the cost a stage measures is the cost the block will |
| 8 | //! actually be coded at, not an approximation of it): a log/diamond search |
| 9 | //! over whole-pel positions starting at the MV-stack predictor, then one |
| 10 | //! ±1-step refinement at half-pel, then one more at quarter-pel. Every |
| 11 | //! candidate's cost is `SAD + lambda * mv_bits`, where `mv_bits` is priced by |
| 12 | //! walking the same symbol/CDF sequence the tile writer would spend on the MV |
| 13 | //! residual, through [`crate::encode::symbol_bits`]'s interval-narrowing |
| 14 | //! (never a per-entry probability lookup, which the AAC lane found 8% off |
| 15 | //! real bytes for a table shaped just like this one). |
| 16 | //! |
| 17 | //! Motion vectors here are `(row, col)` in the spec's 1/8-pel units, matching |
| 18 | //! [`crate::mvstack::MvStack::pred_mv`]. |
| 19 | |
| 20 | use cratecdf; |
| 21 | use cratesymbol_bits; |
| 22 | use cratepredict; |
| 23 | |
| 24 | /// One 1/8-pel unit's worth of 1/16-pel steps: [`crate::mc::predict`]'s |
| 25 | /// `x_q4`/`y_q4` run twice as fine as the MV unit this module searches in. |
| 26 | const Q4_PER_Q3: i32 = 2; |
| 27 | |
| 28 | /// One whole sample, in 1/8-pel units. |
| 29 | const PEL_Q3: i32 = 8; |
| 30 | |
| 31 | /// One half sample, in 1/8-pel units — the step the search's second stage |
| 32 | /// refines by. Fixed by the unit, not swept. |
| 33 | const HALF_PEL_Q3: i32 = 4; |
| 34 | |
| 35 | /// One quarter sample, in 1/8-pel units — the step the search's third stage |
| 36 | /// refines by. Fixed by the unit, not swept. |
| 37 | const QUARTER_PEL_Q3: i32 = 2; |
| 38 | |
| 39 | /// The widest step the integer-pel search starts its log search at, in whole |
| 40 | /// samples. |
| 41 | /// |
| 42 | /// Swept 4/8/16 against a synthetic clip of four blocks translated by 8 |
| 43 | /// samples on each axis, all four signs (`sweep_initial_step` below): 4 |
| 44 | /// finds only 2/4 (an 8-sample displacement needs two same-step hops at |
| 45 | /// step 4, and the second hop can drift off the true match first -- see the |
| 46 | /// corner-cut note on `finds_exact_integer_translation_all_four_signs`); 8 |
| 47 | /// and 16 both find every translation exactly (256 block evaluations for 8, |
| 48 | /// 320 for 16 -- measured by `sweep_initial_step`, `--nocapture`), so 8 does |
| 49 | /// the same job for 25% less. 8 lands. |
| 50 | const SEARCH_INITIAL_STEP_PEL: i32 = 8; |
| 51 | |
| 52 | /// The narrowest step the integer-pel search's log stage runs at, in whole |
| 53 | /// samples, before subpel refinement takes over. One sample — going finer |
| 54 | /// here would just duplicate the half-pel stage's job at the coarser SAD-only |
| 55 | /// cost function. |
| 56 | const SEARCH_MIN_STEP_PEL: i32 = 1; |
| 57 | |
| 58 | /// [`SEARCH_INITIAL_STEP_PEL`], swept by `EC_AV1_MV_STEP` in a test build. |
| 59 | /// Swept 1/2/4/8 on the BD gate with the stack seeds below in place: the |
| 60 | /// narrow seeded shape a predictor-seeded search is supposed to allow costs |
| 61 | /// far more rate than it saves wall (step 1 +145.2/+219.4/+81.1 vs libaom, |
| 62 | /// step 2 +134.2/+196.1/+81.1, step 4 +126.0/+161.2/+81.1 against 8's |
| 63 | /// +125.6/+152.1/+80.9) for 0.2 s of a 6 s ladder -- the wide log stage is |
| 64 | /// what finds the 2160p clip's motion, and the search is not the wall. |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | /// Whether [`search`] starts from the best of the MV-stack's own candidates |
| 74 | /// (libaom's shape) as well as `pred_mv`. `EC_AV1_MV_SEEDED=0` restores the |
| 75 | /// `pred_mv`-only start in a test build (what the lane measured both arms |
| 76 | /// with). |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | /// How the search spends itself since the last [`take_census`]: 0 calls, |
| 88 | /// 1 total candidate evaluations (one `predict` + SAD each), 2 of which in |
| 89 | /// the two subpel stages, 3 integer-stage rounds, 4 calls whose winner is |
| 90 | /// within one whole sample of `pred_mv`, 5 calls whose winner is the |
| 91 | /// starting centre itself. A gate prints these rather than assume where the |
| 92 | /// wall-owning stage goes (gate-blind-to-feature). |
| 93 | static CENSUS: = |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | ; |
| 101 | |
| 102 | /// Takes (and clears) the census. |
| 103 | |
| 104 | pub |
| 105 | |
| 106 | |
| 107 | |
| 108 | /// The eight offsets, at `step` in each axis, a diamond/log search round |
| 109 | /// checks around its current centre (the ninth point, no offset, is the |
| 110 | /// centre itself and is already priced). |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | /// Rounds an MV component to the nearest whole sample (ties away from zero), |
| 125 | /// which is where the integer-pel search stage starts. |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | /// `Av1_get_mv_class`'s class and in-class offset for a coded magnitude `z = |
| 137 | /// |component| - 1` (spec 5.9.15's inverse, the encoder side libaom's |
| 138 | /// `av1_get_mv_class` computes): class 0 covers `z < 16`; each class `c >= 1` |
| 139 | /// covers the doubling range `[8*2^c, 16*2^c)`, so `c = floor(log2(z)) - 3`. |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | /// The bits one non-zero MV component (`diff`, the coded value minus its |
| 152 | /// predictor) spends: sign, class, then the class's own bit/fraction/half-pel |
| 153 | /// symbols, each priced by [`symbol_bits`] against the CDF the tile writer |
| 154 | /// would spend it against (spec 5.9.15's `read_mv_component`, run backwards). |
| 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 | /// The bits an MV difference from its predictor spends: the joint symbol |
| 182 | /// (which axes are non-zero) plus each non-zero axis's own component (spec |
| 183 | /// 5.9.15's `read_mv`). |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | /// One search's outcome: the best MV found and the `SAD + lambda * mv_bits` |
| 202 | /// cost it settled at. |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | /// One candidate's `SAD + lambda * mv_bits(mv - pred_mv)`, predicted into the |
| 212 | /// caller's `dst16` scratch (`block_w * block_h`) so a whole search allocates |
| 213 | /// once. |
| 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 | /// [`cost_of_mv`] for one vector, on its own scratch -- what a caller uses to |
| 251 | /// price a single candidate (an extra reference's `NEARESTMV`, say) on the |
| 252 | /// same scale [`search`] reports its own cost on, for one evaluation instead |
| 253 | /// of a whole search. |
| 254 | |
| 255 | pub |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | /// Searches one `block_w * block_h` block, at `(block_x, block_y)` in the |
| 278 | /// current picture's plane, against `reference` for the motion vector that |
| 279 | /// minimises `SAD + lambda * mv_bits(mv - pred_mv)`. |
| 280 | /// |
| 281 | /// `reference` is `ref_width * ref_height`, row-major, one plane. `source` is |
| 282 | /// the block's own samples, `block_w * block_h`, row-major — what the search |
| 283 | /// compares each candidate's prediction against. `pred_mv` is the MV-stack |
| 284 | /// predictor (e.g. [`crate::mvstack::MvStack::pred_mv`]) the residual, and its |
| 285 | /// cost, are coded against; this function does not build the MV stack |
| 286 | /// itself. |
| 287 | /// |
| 288 | /// SAD, not SSE: both settle on the same minimum on the synthetic translation |
| 289 | /// tests below (a motion search's candidates differ by whole/half/quarter-pel |
| 290 | /// shifts of the same content, so their error surfaces share a minimum either |
| 291 | /// way). A standalone timing probe (2,000,000 SAD/SSE passes over a 64-sample |
| 292 | /// block, `rustc -O`) measured SSE 10% *faster* here (80.6ms vs 89.5ms) — the |
| 293 | /// squaring auto-vectorises as well as the absolute value does, so speed |
| 294 | /// does not decide it. SAD wins on a different ground: it is the metric |
| 295 | /// [`crate::encode::Search`]'s own mode trial (`encode.rs`) already reports |
| 296 | /// error in (`sse: f64` there is a name, not a metric choice — it is fed |
| 297 | /// squared error from the transform's own reconstruction, a different |
| 298 | /// quantity from a raw sample-domain match cost), and SAD is what a motion |
| 299 | /// search conventionally reports for exactly this reason: it is linear in |
| 300 | /// the outlier's size rather than quadratic, so one badly-mismatched sample |
| 301 | /// at a block edge (occlusion, a moving object's boundary) does not swamp |
| 302 | /// the whole candidate's score the way SSE would. |
| 303 | /// |
| 304 | /// # Panics |
| 305 | /// Panics when `source` is not `block_w * block_h` long, or `reference` is |
| 306 | /// empty (the same contracts [`predict`] has). |
| 307 | // one reference plane, one block, one predictor |
| 308 | pub |
| 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 | /// The integer-pel log stage's starting step for a reference at order-hint |
| 338 | /// distance `dist`: `step * (1 + k * (dist - 1))`, `k` = `EC_AV1_MV_DIST_SCALE` |
| 339 | /// in a test build. `k = 0` is the flat step every reference used before. |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | /// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate |
| 357 | /// (vs libaom, 1080p/2160p/screen): 0 = +100.7/+115.8/+73.3, 0.25 |
| 358 | /// +98.5/+112.2, 0.375 +98.2/+115.6, 0.5 +94.4/+104.3, 0.625 +95.9/+106.5, |
| 359 | /// 0.75 +92.8/+112.7, 1.0 +98.8/+113.0 -- screen is byte-identical at every |
| 360 | /// point (its motion is small enough that a wider start converges to the same |
| 361 | /// vector). 0.5 wins both film clips together; the ranking either side of it |
| 362 | /// is near-tie churn, not a gradient. |
| 363 | const MV_DIST_SCALE: f64 = 0.5; |
| 364 | |
| 365 | /// [`search`], plus the running best cost after every round any of its three |
| 366 | /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below |
| 367 | /// checks. |
| 368 | |
| 369 | // one reference plane, one block, one predictor |
| 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 | /// [`search_traced`] with the integer-pel stage's starting step named |
| 406 | /// explicitly — what `sweep_initial_step` below sweeps to land |
| 407 | /// [`SEARCH_INITIAL_STEP_PEL`]. |
| 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 | |
| 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 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 |