edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
| 1 | //! Motion compensation (8.5.3.3): fractional sample interpolation and the |
| 2 | //! prediction-unit driver that turns a motion vector into final samples. |
| 3 | //! |
| 4 | //! Samples are `u16` planes of 8 to 10 bits, 4:2:0 chroma. The interpolation is |
| 5 | //! separable and keeps the spec's intermediate precision: `predSamplesLX` values |
| 6 | //! live in `i32` buffers. Reference coordinates are clamped to the picture per |
| 7 | //! sample (the spec's `Clip3`); the source window is gathered once per block so |
| 8 | //! the filter loops need no bounds logic. Scratch buffers are reused across calls |
| 9 | //! through [`McScratch`], so the hot path does not allocate after warm-up. |
| 10 | |
| 11 | use Motion; |
| 12 | use ; |
| 13 | use ; |
| 14 | |
| 15 | /// The largest prediction block side (a 64x64 PU). |
| 16 | pub const MAX_PU: usize = 64; |
| 17 | |
| 18 | /// Table 8-11: the luma 8-tap filters, indexed by quarter-sample fraction. Index 0 |
| 19 | /// is the full-sample position, which the interpolation never filters. |
| 20 | pub const LUMA_FILTER: = |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | ; |
| 26 | |
| 27 | /// Table 8-12: the chroma 4-tap filters, indexed by eighth-sample fraction. Index 0 |
| 28 | /// is the full-sample position, which the interpolation never filters. |
| 29 | pub const CHROMA_FILTER: = |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | ; |
| 39 | |
| 40 | /// One sample plane of a reference picture. |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | /// One writable sample plane of the picture being predicted. |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | /// The Y, Cb and Cr planes of one reference picture. |
| 69 | pub type RefPicture<'a> = ; |
| 70 | |
| 71 | /// A rectangle inside one plane, in samples of that plane. |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | /// Reusable scratch buffers for the motion compensation of one decoder. |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | /// Predict one prediction unit into the three output planes. |
| 103 | /// |
| 104 | /// `refs[list][ref_idx]` is the reference picture each list's `refIdxLX` names. |
| 105 | /// `pu` is the luma rectangle; the chroma rectangle is half of it in each |
| 106 | /// direction (4:2:0). `weights` selects explicit weighted prediction; `None` is |
| 107 | /// the default weighted prediction. Each output plane receives the clipped final |
| 108 | /// samples of the rectangle. |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | /// [`predict_pu`] for any chroma subsampling. `sub` is the log2 `(sub_x, sub_y)` of the |
| 121 | /// chroma planes: `(1, 1)` for 4:2:0, `(1, 0)` for 4:2:2, `(0, 0)` for 4:4:4. The chroma |
| 122 | /// vector is then `mvLX * 2 / SubWidthC` and `mvLX * 2 / SubHeightC` in 1/8 chroma samples |
| 123 | /// (8.5.3.2.10), and the chroma rectangle is `pu` shifted by `sub`. |
| 124 | // the PU's refs, motion, weights and output |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 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 | |
| 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 | /// The luma fractional sample interpolation (8.5.3.3.3.1) of one block. |
| 207 | /// |
| 208 | /// `mv` is the motion vector in quarter samples. The `blk.width * blk.height` |
| 209 | /// results of `predSamplesLX` are written to `out` row by row, at 14-bit precision. |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | /// The chroma fractional sample interpolation (8.5.3.3.3.2) of one block, 4:2:0. |
| 224 | /// |
| 225 | /// `mv` is the luma motion vector in quarter samples, which in 4:2:0 is the |
| 226 | /// chroma motion vector in eighth samples. `blk` is in chroma samples. |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | /// Interpolate one block of one component, luma or 4:2:0 chroma. |
| 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 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | /// The shared separable interpolation for a `T`-tap filter (`T` = 8 luma, 4 chroma). |
| 287 | /// |
| 288 | /// `at` is the integer position of the block's top-left sample in the reference |
| 289 | /// plane, `filters` the horizontal and vertical coefficients (`None` when that |
| 290 | /// direction is at a full-sample position). The equations of 8.5.3.3.3 are |
| 291 | /// `shift1 = Min(4, BitDepth - 8)`, `shift2 = 6`, `shift3 = Max(2, 14 - BitDepth)`. |
| 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 | /// One horizontal filter pass: `out[c] = (sum_i coef[i] * row[c + i]) >> shift`. |
| 355 | /// |
| 356 | /// The taps run in the outer loop so each pass is a plain multiply-add over a |
| 357 | /// contiguous run of outputs, which the compiler vectorises. `out.len()` is at most |
| 358 | /// [`MAX_PU`], the accumulator size. |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | /// One vertical filter pass: `out[c] = (sum_i coef[i] * src[i * stride + c]) >> shift`. |
| 364 | /// Same shape as [`filter_row`], with the taps `stride` apart. |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | /// The shared tap loop of [`filter_row`] and [`filter_col`]. The taps run in the |
| 376 | /// outer loop so each pass is a plain multiply-add over a contiguous run of outputs. |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | /// Copy the `bw x bh` reference window whose top-left sample is `(x0, y0)` into |
| 398 | /// `src`, clamping every coordinate into the plane (the spec's `Clip3` on |
| 399 | /// `xInt` and `yInt`, applied per sample). A window inside the plane horizontally is |
| 400 | /// copied a row at a time. |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | /// Reject a plane whose geometry or bit depth the interpolation cannot read. |
| 421 | pub |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | /// Reject a bit depth outside the 8 to 10 bits this module handles. |
| 435 | pub |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | /// Reject a block that is empty, larger than a PU, or too big for its buffer. |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | /// Reject an output rectangle that does not fit inside its plane. |
| 462 | pub |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 |