edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB00c1b3fTahinli2 months ago1999 commits
| 1 | //! SILK encoder analysis front end (subtask D1). |
| 2 | //! |
| 3 | //! This module decides *what* a SILK frame should say — resampled input, |
| 4 | //! a voice-activity flag, an LPC filter reduced to NLSF codebook indices, a |
| 5 | //! pitch contour, and per-subframe gain indices — without writing any of it |
| 6 | //! to a bitstream. The NSQ/LTP excitation search and the actual range-coder |
| 7 | //! writes are a separate subtask (D2) that consumes the types here; nothing |
| 8 | //! in this file touches [`crate::range::RangeEncoder`]. |
| 9 | //! |
| 10 | //! Every quantiser below hands its candidate indices to the *decoder's own* |
| 11 | //! functions in [`crate::silk`] (`nlsf_decode`, `nlsf2a`, `gains_dequant`) to |
| 12 | //! measure quantisation error, so the indices this module produces are |
| 13 | //! guaranteed to be indices the decoder in `silk.rs` can read back — there is |
| 14 | //! no separate, possibly-divergent, notion of "encoder-side dequantisation". |
| 15 | //! |
| 16 | //! The VAD is a simplified energy/spectral-flatness gate, *not* RFC 6716 |
| 17 | //! Appendix A's cascade of half-band filters and hangover logic — stated |
| 18 | //! once, here, for the whole module (assumption called out by the D1 |
| 19 | //! charter). |
| 20 | |
| 21 | // D2 (the NSQ/LTP/entropy writer) is the real caller of this API; until |
| 22 | // that charter lands, nothing outside this module's own tests exercises it. |
| 23 | |
| 24 | |
| 25 | use PI; |
| 26 | use PI as PI64; |
| 27 | |
| 28 | use crate |
| 29 | |
| 30 | ; |
| 31 | use crate |
| 32 | |
| 33 | |
| 34 | ; |
| 35 | |
| 36 | |
| 37 | use crate; |
| 38 | |
| 39 | // --------------------------------------------------------------------------- |
| 40 | // 1. Resampler: 48 kHz -> SILK's internal 8/12/16 kHz. |
| 41 | // --------------------------------------------------------------------------- |
| 42 | |
| 43 | /// A linear-phase FIR decimator from 48 kHz to a SILK internal rate. |
| 44 | /// |
| 45 | /// This is a windowed-sinc low-pass (Hamming window, unity DC gain) run at |
| 46 | /// the decimated rate, independent of the decoder's `Resampler` in |
| 47 | /// `silk.rs` — that one's `DELAY_MATRIX_DEC` table only covers *upsampling* |
| 48 | /// paths (internal rate -> API rate) and panics on `rate_id` for a 48 kHz |
| 49 | /// input, so downsampling needs its own path (charter point 1, "add a down |
| 50 | /// path"). |
| 51 | /// |
| 52 | /// Group delay is exactly `(taps - 1) / 2` input (48 kHz) samples, because |
| 53 | /// the kernel is symmetric — see [`Resampler48::delay_samples`]. |
| 54 | /// |
| 55 | /// corner-cut: `process` requires `input.len()` to be a multiple of the |
| 56 | /// decimation factor (2, 3, 4 or 6); 20 ms @ 48 kHz (960 samples) always is. |
| 57 | /// Upgrade path: track a fractional phase across calls if a caller ever |
| 58 | /// needs non-frame-aligned chunks. |
| 59 | |
| 60 | pub |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | // --------------------------------------------------------------------------- |
| 126 | // 2. Voice activity detection. |
| 127 | // --------------------------------------------------------------------------- |
| 128 | |
| 129 | /// Per-20ms-frame VAD result. |
| 130 | |
| 131 | pub |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | /// Simplified energy + spectral-flatness VAD (not RFC 6716 Appendix A). |
| 137 | |
| 138 | pub |
| 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 | // 3. LPC analysis: autocorrelation, Levinson-Durbin, LPC -> NLSF. |
| 199 | // --------------------------------------------------------------------------- |
| 200 | |
| 201 | /// One frame's LPC analysis, ready for NLSF quantisation. |
| 202 | |
| 203 | pub |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | /// Autocorrelation (lags `0..=order`) with a light Gaussian lag window and a |
| 213 | /// white-noise floor added at lag 0, both standard Levinson-Durbin |
| 214 | /// stabilisers. |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | /// Levinson-Durbin recursion. Returns `None` if the signal is exactly |
| 233 | /// silent (autocorrelation at lag 0 is zero). |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | /// Bandwidth-expands (moves poles inward by `gamma` per order) so the LPC |
| 261 | /// filter this module hands to root-finding is comfortably stable. |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | /// LPC -> NLSF: the standard line-spectral-pair construction, independent |
| 271 | /// of `nlsf2a`'s own internal representation (its `find_poly` builds an |
| 272 | /// algebraically different — not root-per-coefficient — polynomial form, |
| 273 | /// so inverting it directly is not the way back; this instead rebuilds the |
| 274 | /// textbook P(z)/Q(z) split and root-finds those on the unit circle, then |
| 275 | /// hands `nlsf2a`/`nlsf_decode` a plain ascending-angle NLSF vector, which |
| 276 | /// is all their documented contract requires). |
| 277 | /// |
| 278 | /// `A(z) = 1 + a[0]z^-1 + .. + a[M-1]z^-M`. Split |
| 279 | /// `P(z) = A(z) + z^-(M+1)A(z^-1)` (root at `z=-1`) and |
| 280 | /// `Q(z) = A(z) - z^-(M+1)A(z^-1)` (root at `z=1`); on the unit circle, |
| 281 | /// after removing the shared linear-phase factor, both reduce to a real |
| 282 | /// function of `w` whose `M/2` zero crossings in `(0, pi)` are exactly the |
| 283 | /// line spectral frequencies. `P`'s and `Q`'s roots interlace by LSP |
| 284 | /// theory, so merging and sorting both root sets gives the ascending NLSF |
| 285 | /// vector directly, without needing to track which physical root came from |
| 286 | /// which half. |
| 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 | /// Runs the full LPC front end on a 20 ms (or 10 ms) analysis frame at the |
| 363 | /// SILK internal rate: autocorrelation, Levinson-Durbin, bandwidth |
| 364 | /// expansion, then LPC -> NLSF. `order` is 10 for NB/MB, 16 for WB. |
| 365 | pub |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | // --------------------------------------------------------------------------- |
| 378 | // 4. NLSF vector quantisation against the decoder's own codebooks. |
| 379 | // --------------------------------------------------------------------------- |
| 380 | |
| 381 | /// Indices `silk::decode_indices` (`silk.rs:615`) reads for one frame's |
| 382 | /// NLSFs, plus the interpolation index (`decode_indices`'s |
| 383 | /// `nlsf_interp_coef_q2`). |
| 384 | |
| 385 | pub |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | /// Quantises `target_q15` (an `order`-length prefix) against the decoder's |
| 397 | /// NB/MB or WB codebook: an 8-best stage-1 survivor search, each survivor |
| 398 | /// completed by a sequential stage-2 residual search that mirrors |
| 399 | /// `nlsf_decode`'s own predictive chain, scored by actually calling |
| 400 | /// `nlsf_decode` — so the winner is whichever candidate the decoder would |
| 401 | /// reconstruct closest to `target_q15`, not an approximation of it. |
| 402 | pub |
| 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 | /// Sequential stage-2 residual search for one stage-1 survivor `cb1_index`, |
| 450 | /// matching `nlsf_decode`'s own back-to-front predictive chain exactly (it |
| 451 | /// calls the same `smulbb`/`smlawb` fixed-point steps) so the indices this |
| 452 | /// returns decode to the value this function itself computed. |
| 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 | // 5. Pitch estimation. |
| 513 | // --------------------------------------------------------------------------- |
| 514 | |
| 515 | /// One frame's pitch decision. |
| 516 | |
| 517 | pub |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | /// Normalised cross-correlation at a single lag over `frame`. |
| 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 | /// Coarse (4 kHz-equivalent decimated) search followed by a fine search at |
| 557 | /// full rate, then a per-subframe refinement and contour-table lookup |
| 558 | /// against the decoder's own `CB_LAGS_*` tables (`silk.rs:157-176`). |
| 559 | /// Voiced decision: libopus silk_find_pitch_lags starts from 0.6 (minus speech-activity, |
| 560 | /// tilt and prev-type terms); value measured on real speech, lane opus-silkq r1. |
| 561 | // Sweep 2026-08-23 (reelm 12k SILK-WB, corr/err_ratio): .35 -> .8081/11.4, |
| 562 | // .45 -> .8127/26.3, .55 -> .8106/44.8, .65 -> .7987/12.3; err non-monotonic, |
| 563 | // threshold is not the 12k mechanism. Kept at .35. |
| 564 | const VOICED_THRESHOLD: f32 = 0.35; |
| 565 | |
| 566 | pub |
| 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 | // 6. Gains. |
| 732 | // --------------------------------------------------------------------------- |
| 733 | |
| 734 | /// `gains_dequant`'s constants, mirrored (they're private to `silk.rs`) |
| 735 | /// only for the inverse estimate below; the actual quantisation decision is |
| 736 | /// always verified against the real `gains_dequant`. |
| 737 | const GAIN_OFFSET: i32 = / 6 + 16 * 128; // MIN_QGAIN_DB = 2 |
| 738 | const GAIN_INV_SCALE_Q16: i32 = / ; // MAX=88, N_LEVELS=64 |
| 739 | |
| 740 | /// Per-subframe gain indices, chosen to minimise error against |
| 741 | /// `target_gains_q16` (the same Q16 linear domain `silk::gains_dequant` |
| 742 | /// returns) as actually measured by calling `gains_dequant`. |
| 743 | pub |
| 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 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 |