edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 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 | /// The eight offsets, at `step` in each axis, a diamond/log search round |
| 59 | /// checks around its current centre (the ninth point, no offset, is the |
| 60 | /// centre itself and is already priced). |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | /// Rounds an MV component to the nearest whole sample (ties away from zero), |
| 75 | /// which is where the integer-pel search stage starts. |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | /// `Av1_get_mv_class`'s class and in-class offset for a coded magnitude `z = |
| 87 | /// |component| - 1` (spec 5.9.15's inverse, the encoder side libaom's |
| 88 | /// `av1_get_mv_class` computes): class 0 covers `z < 16`; each class `c >= 1` |
| 89 | /// covers the doubling range `[8*2^c, 16*2^c)`, so `c = floor(log2(z)) - 3`. |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | /// The bits one non-zero MV component (`diff`, the coded value minus its |
| 102 | /// predictor) spends: sign, class, then the class's own bit/fraction/half-pel |
| 103 | /// symbols, each priced by [`symbol_bits`] against the CDF the tile writer |
| 104 | /// would spend it against (spec 5.9.15's `read_mv_component`, run backwards). |
| 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 | /// The bits an MV difference from its predictor spends: the joint symbol |
| 132 | /// (which axes are non-zero) plus each non-zero axis's own component (spec |
| 133 | /// 5.9.15's `read_mv`). |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | /// One search's outcome: the best MV found and the `SAD + lambda * mv_bits` |
| 152 | /// cost it settled at. |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | /// Searches one `block_w * block_h` block, at `(block_x, block_y)` in the |
| 162 | /// current picture's plane, against `reference` for the motion vector that |
| 163 | /// minimises `SAD + lambda * mv_bits(mv - pred_mv)`. |
| 164 | /// |
| 165 | /// `reference` is `ref_width * ref_height`, row-major, one plane. `source` is |
| 166 | /// the block's own samples, `block_w * block_h`, row-major — what the search |
| 167 | /// compares each candidate's prediction against. `pred_mv` is the MV-stack |
| 168 | /// predictor (e.g. [`crate::mvstack::MvStack::pred_mv`]) the residual, and its |
| 169 | /// cost, are coded against; this function does not build the MV stack |
| 170 | /// itself. |
| 171 | /// |
| 172 | /// SAD, not SSE: both settle on the same minimum on the synthetic translation |
| 173 | /// tests below (a motion search's candidates differ by whole/half/quarter-pel |
| 174 | /// shifts of the same content, so their error surfaces share a minimum either |
| 175 | /// way). A standalone timing probe (2,000,000 SAD/SSE passes over a 64-sample |
| 176 | /// block, `rustc -O`) measured SSE 10% *faster* here (80.6ms vs 89.5ms) — the |
| 177 | /// squaring auto-vectorises as well as the absolute value does, so speed |
| 178 | /// does not decide it. SAD wins on a different ground: it is the metric |
| 179 | /// [`crate::encode::Search`]'s own mode trial (`encode.rs`) already reports |
| 180 | /// error in (`sse: f64` there is a name, not a metric choice — it is fed |
| 181 | /// squared error from the transform's own reconstruction, a different |
| 182 | /// quantity from a raw sample-domain match cost), and SAD is what a motion |
| 183 | /// search conventionally reports for exactly this reason: it is linear in |
| 184 | /// the outlier's size rather than quadratic, so one badly-mismatched sample |
| 185 | /// at a block edge (occlusion, a moving object's boundary) does not swamp |
| 186 | /// the whole candidate's score the way SSE would. |
| 187 | /// |
| 188 | /// # Panics |
| 189 | /// Panics when `source` is not `block_w * block_h` long, or `reference` is |
| 190 | /// empty (the same contracts [`predict`] has). |
| 191 | // one reference plane, one block, one predictor |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | /// [`search`], plus the running best cost after every round any of its three |
| 213 | /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below |
| 214 | /// checks. |
| 215 | // one reference plane, one block, one predictor |
| 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 | /// [`search_traced`] with the integer-pel stage's starting step named |
| 246 | /// explicitly — what `sweep_initial_step` below sweeps to land |
| 247 | /// [`SEARCH_INITIAL_STEP_PEL`]. |
| 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 | |
| 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 |