edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
| 1 | //! SBR high-frequency generation (ISO/IEC 14496-3 §4.6.18.6): patch |
| 2 | //! construction that copies low-band QMF content up into the region the |
| 3 | //! core codec never carried, plus per-subband second-order linear |
| 4 | //! prediction (LPC) whose bandwidth-expanded ("chirped") coefficients |
| 5 | //! whiten the copied content so it can be re-tonalised by the envelope |
| 6 | //! adjustment stage rather than sounding like a raw comb of copies. |
| 7 | //! |
| 8 | //! # Patch construction |
| 9 | //! |
| 10 | //! Each patch copies a contiguous run of QMF bands from just below the |
| 11 | //! crossover (`kx`) up into the target HF region `[kx, k2)`. The walk keeps |
| 12 | //! two pointers: `msb`, the source read pointer, starts at `kx` and moves |
| 13 | //! *down* toward band 0 as patches are consumed; `sb`, the target write |
| 14 | //! pointer, starts at `kx` and moves *up* toward `k2`. A patch's width is |
| 15 | //! chosen to land on a boundary of the target-side high-resolution band |
| 16 | //! table (`tables.f_high`) so envelope adjustment never has to split a |
| 17 | //! patch mid-band, and is capped by how many source bands remain below the |
| 18 | //! current `msb`. When the source is exhausted the read pointer wraps back |
| 19 | //! to `kx` (the band closest to the crossover, so the next patch borrows |
| 20 | //! the most plausible content again rather than running off the bottom of |
| 21 | //! the spectrum). |
| 22 | //! |
| 23 | //! # Linear prediction and chirp |
| 24 | //! |
| 25 | //! For each *source* subband, a covariance-method order-2 LPC is fit to |
| 26 | //! that subband's own QMF time series (two slots of carried-over history |
| 27 | //! plus the current frame's slots). The residual against those raw |
| 28 | //! coefficients is then re-synthesised through a *bandwidth-expanded* |
| 29 | //! all-pole filter -- coefficients scaled by `bw` and `bw^2` -- which is |
| 30 | //! the standard SBR "inverse filtering" trick: `bw` near 1 leaves the |
| 31 | //! subband's own resonance intact (a strongly tonal patch), `bw` near 0 |
| 32 | //! flattens it toward its whitened residual (a noise-like patch). `bw` |
| 33 | //! itself comes from `bs_invf_mode` and is smoothed frame to frame with a |
| 34 | //! fast-attack/slow-release curve so inverse-filter mode changes don't pop. |
| 35 | //! |
| 36 | //! Round-48 replaced this all-pole residual resynthesis with the spec's |
| 37 | //! literal reading of §4.6.18.6, a feed-forward 2-tap FIR extension of the |
| 38 | //! raw copy (`X_high[l] = X_src[l] + bw*a1*X_src[l-1] + bw^2*a2*X_src[l-2]`). |
| 39 | //! That version compiled, passed its own unit test, and is *closer* to a |
| 40 | //! literal spec transcription -- but measured reproducibly WORSE on real |
| 41 | //! content, then (round-50) and again re-tried and re-lost (later round) |
| 42 | //! after `sbr_qmf` became the normative ISO/IEC 14496-3 4.6.18.4/4.6.18.8.2 |
| 43 | //! bank rather than the earlier fitted Kaiser one -- so the QMF bank change |
| 44 | //! was not the missing piece either. Full-band correlation against the |
| 45 | //! reference decoder on reeln: 0.986993/0.986238 (all-pole, ch0/ch1) vs |
| 46 | //! 0.965566/0.965680 (feed-forward); FMJ: 0.997880/0.993373 (all-pole) vs |
| 47 | //! 0.994118/0.983006 (feed-forward); synthetic 48k-family above-crossover |
| 48 | //! band also regresses under feed-forward (e.g. 0.9882->0.9646 at 48k/48kbps). |
| 49 | //! This module keeps the all-pole structure documented above on that |
| 50 | //! measurement, not on the spec reading: the bar this project holds itself |
| 51 | //! to is matching the reference decoder's actual output, and a from-memory |
| 52 | //! "more spec-literal" label does not outrank it. The feed-forward |
| 53 | //! alternative and its discriminating unit test remain in history (commit |
| 54 | //! 8bc305b) as the documented alternative, should some other change in this |
| 55 | //! pipeline ever make it the better match. |
| 56 | |
| 57 | use crateBandTables; |
| 58 | use Complex; |
| 59 | |
| 60 | /// Characterizes (but, as of round-37, is deliberately NOT applied to) a QMF |
| 61 | /// bank convention gap `sbr_qmf`'s round-trip tuning never exercised: |
| 62 | /// replaying a source band's raw complex series into a *different* synthesis |
| 63 | /// band (exactly what patch copying does) lands the content at the "wrong" |
| 64 | /// absolute frequency by exactly one synthesis-band width whenever |
| 65 | /// `target_band - source_band` is odd, with the sign flipping every other |
| 66 | /// odd step -- i.e. a period-4 pattern in `target - source` (even |
| 67 | /// difference: no error; `+1 mod 4`: content lands one band flat; `+3 mod |
| 68 | /// 4`: one band sharp). `sbr_qmf::Synthesis`'s modulation kernel centres |
| 69 | /// band `k` at `(k+0.5) * pi/(2*SYNTHESIS_BANDS)` via a phase intercept that |
| 70 | /// is itself `(k+0.5) * pi/4` -- exactly the odd-stacked cosine-modulated- |
| 71 | /// bank convention the round-trip test tuned for same-band replay, where |
| 72 | /// this intercept cancels against the receiving band's own reconstruction. |
| 73 | /// |
| 74 | /// Round-35 had `generate` counter-rotate the patched series by this amount |
| 75 | /// before it reaches `Synthesis`, and proved (in isolation, via a |
| 76 | /// stationary-tone FFT probe through the actual production call site -- |
| 77 | /// `sbr_qmf::tests::patch_replay_through_generate_preserves_absolute_frequency_mapping`) |
| 78 | /// that doing so restores textbook-coherent absolute-frequency placement. |
| 79 | /// Round-36/37's real-file mechanical A/B (`EC_AAC_SBR_PARITY_SPLIT`) |
| 80 | /// showed applying that correction REGRESSES odd-gap coherence against the |
| 81 | /// reference decoder (ch0 0.1907->0.0714, ch1 0.2461->0.0741; a |
| 82 | /// sign-flipped variant and a half-rate variant both regress it too, to |
| 83 | /// 0.0757 and 0.1449 respectively on ch0) -- the reference apparently |
| 84 | /// reproduces this same "wrong" convention itself, so leaving the raw copy |
| 85 | /// unrotated is what actually matches it. This function is kept (and |
| 86 | /// pinned by its own unit tests) purely to document that convention as |
| 87 | /// intentional, not to be called from `generate`'s patch-copy site. |
| 88 | /// |
| 89 | /// Round-46 raised a re-trial candidate: `EC_AAC_SBR_PARITY_SPLIT` (an 8-bin |
| 90 | /// STFT metric) had been suspected untrustworthy at this precision, and a |
| 91 | /// QMF-exact fit of the reference decoder's own HF content on patch |
| 92 | /// `(3,28,11)` (gap 25, `mod 4 == 1`) found a transfer with this function's |
| 93 | /// quarter-turn phase signature. Round-47 re-applied the rotation at |
| 94 | /// `generate`'s patch-copy site (env-gated, both signs) and re-judged with |
| 95 | /// `EC_AAC_SBR_QMF_WITNESS`'s own QMF-domain coherence table instead of the |
| 96 | /// STFT one -- the trustworthy instrument the re-trial was chartered to use. |
| 97 | /// Both signs made sim-vs-ours coherence on patch `(3,28,11)` WORSE, not |
| 98 | /// better (band28 0.2628 unrotated -> 0.0161 rotated -> 0.0282 flipped; |
| 99 | /// band38 0.0669 -> 0.0694 -> 0.1068), moving further from the reference's |
| 100 | /// 0.42-0.79 rather than toward it, refuting round-46's fit-based |
| 101 | /// prediction under the very instrument that was supposed to vindicate it. |
| 102 | /// (One sign did nudge whole-file full-band correlation up rather than |
| 103 | /// round-36's dip, but that's moot once the witness itself disagrees.) The |
| 104 | /// verdict stands as round-37 left it: `generate`'s patch-copy site keeps |
| 105 | /// the plain, unrotated copy. |
| 106 | /// Returns the signed number of `pi/2` quarter-turns per QMF slot that |
| 107 | /// *would* need to be applied to restore frequency-pure placement. |
| 108 | // documentation of the characterized convention; tests call it |
| 109 | pub |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | /// One copy-up patch: `width` consecutive QMF bands starting at |
| 119 | /// `source_start` are written to `target_start..target_start+width`. |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | /// Patch construction, ISO/IEC 14496-3 §4.6.18.6.3 (Figure 4.31): source |
| 128 | /// bands are read downward from `k0 = f_master[0]`, patches end on master |
| 129 | /// borders, `goalSb = round(2048 kHz / fs)` bounds the first patch, and a |
| 130 | /// trailing patch narrower than 3 bands is dropped. |
| 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 | /// Builds the limiter band table (ISO/IEC 14496-3 §4.6.18.7.2, Figure 4.33): |
| 192 | /// the union of `f_low` and the patch borders, sorted, then thinned so two |
| 193 | /// borders closer than `0.49/limBands` octaves collapse (`bs_limiter_bands` |
| 194 | /// 1/2/3 => 1.2/2/3 bands per octave), keeping patch borders over |
| 195 | /// envelope borders when one of the pair must go. `0` => one band. |
| 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 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /// `bs_invf_mode` chirp targets per ISO 14496-3 4.6.18.6.3 (Table 4.163): |
| 248 | /// the target depends on the current AND previous frame's mode -- |
| 249 | /// OFF is 0.0 except 0.6 when falling from LOW; LOW is 0.75 except 0.6 |
| 250 | /// when rising from OFF; MID 0.9; HIGH 0.98. The previous flat table |
| 251 | /// (`[0, 0.6, 0.9, 0.98]`) under-whitened every steady-state LOW band. |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | /// Per-noise-band chirp factor state, smoothed frame to frame with a |
| 264 | /// fast-attack (toward a *lower* bw, i.e. more whitening) / slow-release |
| 265 | /// curve, as the spec's own smoothing does. |
| 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 | /// `EC_AAC_SBR_BW_SCALE` global multiplier on the chirp target, clamped below 1.0 |
| 327 | /// (a chirp target at or past 1.0 is an undamped all-pole resonance, not a |
| 328 | /// meaningful "stronger" whitening). `1.0` (no-op) when unset or unparsable. |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | /// Order-2 linear-prediction coefficients per ISO 14496-3 4.6.18.6.2 |
| 353 | /// (covariance method, `phi(i,j) = sum_n x[n-i]*conj(x[n-j])` over |
| 354 | /// `n = 2..x.len()`, index 0 of `x` is the oldest slot). Returns the spec's |
| 355 | /// `(alpha0, alpha1)` -- the PREDICTION-ERROR filter taps, i.e. the |
| 356 | /// negated predictor coefficients: for `x[n] = a1*x[n-1] + a2*x[n-2] + e` |
| 357 | /// this yields `(-a1, -a2)`, so `x + alpha0*x[n-1] + alpha1*x[n-2]` |
| 358 | /// whitens. `(0, 0)` on degenerate input or when either |alpha| >= 4 |
| 359 | /// (spec guard). Witness: scratch `alpha/witness.py` (numpy) recovers a |
| 360 | /// synthetic complex AR(2) with this sign convention and whitens at bw=1. |
| 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 | /// Per-source-subband history: the last two QMF slots, carried across |
| 394 | /// `generate` calls so the LPC and whitening filter have continuity at |
| 395 | /// frame boundaries instead of restarting cold every frame. |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | const HIST: usize = 6; |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | /// Generates one frame's raw (pre-envelope-adjustment) HF QMF matrix, |
| 415 | /// `[target band index, 0-based from kx][slot]`, `k2 - kx` bands by |
| 416 | /// `low_cur[0].len()` slots (`low_cur` is `[source band][slot]`, bands |
| 417 | /// `0..kx` only need be populated). |
| 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 |