edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits
| 1 | //! Forward transforms and quantisation. |
| 2 | //! |
| 3 | //! Nothing here is normative: the standard specifies only the *inverse* |
| 4 | //! transforms, which the decoder side of this crate already implements |
| 5 | //! ([`crate::transform`]). The forward direction is chosen so that the |
| 6 | //! round trip through the normative inverse lands back on the source, and |
| 7 | //! that is what the tests at the bottom check across the whole QP range. |
| 8 | //! |
| 9 | //! The multiplier table pairs with `normAdjust4x4` (Table 8-15 through the |
| 10 | //! `LevelScale` of Equation 8-313): `MF[m][class] * normAdjust4x4[m][class] * |
| 11 | //! 16 ~= 2^21`, which is what makes `level = (|c| * MF + f) >> (15 + qp / 6)` |
| 12 | //! invert the decoder's dequantisation. |
| 13 | |
| 14 | use crate; |
| 15 | use crateLevelScale8x8; |
| 16 | |
| 17 | /// Quantisation multipliers per `qp % 6`, indexed by the same position class |
| 18 | /// as [`NORM_ADJUST_CLASS_4X4`] (0 = even/even, 1 = odd/odd, 2 = mixed). |
| 19 | const MF: = |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | ; |
| 27 | |
| 28 | /// Rounding offset numerator: an intra block rounds at 1/3 of a step, an inter |
| 29 | /// block at 1/6 — the classic asymmetry, which spends bits where prediction is |
| 30 | /// weakest. Measured against 1/3 and 1/2 for the inter case on 1080p camera |
| 31 | /// and screen-capture clips: 1/6 is level or ahead at matched bitrate. |
| 32 | /// |
| 33 | /// Re-measured 2026-08-23 against x264 with `bd_psnr_vs_x264` on a 3840x1608 |
| 34 | /// clip, sweeping both numerators as sixty-fourths the way x264 spells |
| 35 | /// `--deadzone-intra` / `--deadzone-inter`. The intra offset is the one that |
| 36 | /// matters and ours is the better of the two: x264's 11/64 reads -1.667 dB |
| 37 | /// all-intra where ours (21/64, i.e. 1/3) reads -0.745 dB, with the inter |
| 38 | /// value making no difference to that. The inter offset is inert across the |
| 39 | /// whole useful range -- 6/64, 16/64, 21/64 and 32/64 span -1.571 to |
| 40 | /// -1.532 dB, a 0.04 dB spread, while the bit counts move by 0.8%. So the |
| 41 | /// remaining distance to x264 is not in the dead zone. |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | /// Forward 4x4 core transform (the inverse of spec 8.5.12.2), raster in and |
| 48 | /// out, integer and exact. |
| 49 | pub |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | /// Forward 4x4 Hadamard over the sixteen luma DC coefficients of an |
| 70 | /// Intra_16x16 macroblock (the inverse of spec 8.5.10), raster in and out. |
| 71 | pub |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | /// Forward 2x2 Hadamard over the four chroma DC coefficients (the inverse of |
| 90 | /// spec 8.5.11). |
| 91 | pub |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | /// Forward 8x8 core transform (the exact algebraic inverse of `idct8_1d` / |
| 100 | /// [`crate::transform::inverse_transform_8x8`]), raster in, scaled coefficients |
| 101 | /// out. |
| 102 | /// |
| 103 | /// `idct8_1d`'s matrix `M` (one 1D pass) is not its own transpose-inverse pair |
| 104 | /// the way the 4x4 transform's small butterfly nearly is: `M^T * M` is |
| 105 | /// diagonal but `M * M^T` is not, so a hand-picked small-integer transpose |
| 106 | /// leaks between frequencies by ~15%, which is too much for the ±1-per-sample |
| 107 | /// tolerance this encoder's residuals need (up to ±255, far past the small |
| 108 | /// ranges a 4x4 block sees). [`FWD8`] is `11560 * M^-1`, computed once by |
| 109 | /// exact rational matrix inversion (`M^-1 * M = I` exactly), so applying it in |
| 110 | /// the same row-then-column order `idct8_1d` uses, then the real inverse |
| 111 | /// transform, reproduces the input exactly, scaled by a single known constant |
| 112 | /// (`11560^2 / 64 = 2_088_025`, see [`quant_8x8`]) — no leakage at all, only |
| 113 | /// the ordinary quantiser rounding. |
| 114 | const FWD8: = |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | ; |
| 124 | |
| 125 | /// One 1D pass of [`FWD8`] over eight values. |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | /// Forward 8x8 transform (raster in, raster out), unnormalised: the caller |
| 132 | /// (`quant_8x8`) divides out the `2_088_025` gain along with the usual |
| 133 | /// per-position quantiser scale. |
| 134 | pub |
| 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 | /// `raw / denom`, rounded with the same 1/3 (intra) or 1/6 (inter) dead zone |
| 161 | /// as [`round_offset`], expressed as plain division instead of a |
| 162 | /// precomputed shift because [`forward_8x8`]'s coefficients already carry |
| 163 | /// far more headroom than a `<<15`-ish fixed point would give. |
| 164 | |
| 165 | /// Quantise an 8x8 [`forward_8x8`] output into scan (zig-zag) order, undoing |
| 166 | /// both the `2_088_025` transform gain and the per-position [`LevelScale8x8`] |
| 167 | /// / QP scale that [`crate::transform::dequant_8x8`] will re-apply. |
| 168 | pub |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | /// [`quant_8x8`] with the rounding offset named outright: the level is |
| 179 | /// `(raw + denom / divisor) / denom`, so a divisor of 2 rounds to nearest and |
| 180 | /// 6 is the inter dead zone. A search that offers smaller magnitudes wants to |
| 181 | /// pick this itself. |
| 182 | pub |
| 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 | /// Quantise a transformed 4x4 block into scan (zig-zag) order. |
| 216 | /// |
| 217 | /// `skip_dc` leaves scan position 0 out and writes the fifteen AC levels into |
| 218 | /// `out[0..15]`, which is the layout the AC residual blocks of Intra_16x16 and |
| 219 | /// chroma use. Returns the number of non-zero levels. |
| 220 | pub |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | /// [`quant_4x4`] with the dead zone spelled out: the rounding offset is |
| 231 | /// `1 / divisor` of a quantiser step. |
| 232 | /// |
| 233 | /// Rate-distortion quantisation asks for a *smaller* dead zone than the plain |
| 234 | /// path uses, because it walks levels down itself and cannot walk them up: a |
| 235 | /// level the dead zone already dropped is not a candidate any more. |
| 236 | pub |
| 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 | /// Quantise the sixteen Hadamard-transformed luma DC coefficients into scan |
| 263 | /// order. Two extra bits of shift pay for the gain of the forward Hadamard, |
| 264 | /// which the decoder's inverse (8.5.10, a `>> 6` where the AC path shifts by |
| 265 | /// four) takes back out. |
| 266 | pub |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | /// Quantise the four Hadamard-transformed chroma DC coefficients (raster |
| 284 | /// order, which is also their scan order). |
| 285 | pub |
| 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 |