edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB90e4e6eTahinli2 months ago3473 commits
| 1 | #!/usr/bin/env python3 |
| 2 | """Derives the ten SBR Huffman codebooks, black box, from a reference decoder. |
| 3 | |
| 4 | The instrument is the reference decoder's own bit accounting. An SBR payload |
| 5 | rides in a FIL element whose declared byte count `cnt` we choose; the decoder |
| 6 | parses the payload with its own syntax knowledge and complains |
| 7 | |
| 8 | Expected to read <cnt> SBR bytes actually read <n> |
| 9 | |
| 10 | whenever `n > cnt`. Declaring `cnt = 1` therefore turns every probe into a |
| 11 | readout of `n = ceil((4 + bits_the_parser_consumed) / 8)`. |
| 12 | |
| 13 | That is byte-granular, and codeword lengths are bits. Two devices sharpen it: |
| 14 | |
| 15 | * A configuration with exactly one envelope band (`bs_freq_res = 0` and an |
| 16 | `bs_xover_band` chosen so `n_high = 2`) has a payload with *no* Huffman |
| 17 | codewords at all -- every bit is one this script wrote. Sweeping such |
| 18 | configurations validates the frequency-band derivation (`n_master`, `n_high`, |
| 19 | `n_low`, `n_q`) against the decoder without any codebook being known. |
| 20 | * With the band tables trusted, the same pattern repeated in `m` of the `N - 1` |
| 21 | Huffman-coded bands makes the consumed length `K + m * L`; the byte count is a |
| 22 | staircase in `m` whose slope pins `L` exactly. |
| 23 | |
| 24 | Values come from a second observable in the same probe: the decoder range-checks |
| 25 | the accumulated envelope and noise scalefactors and says so on stderr. Bisecting |
| 26 | the raw start value for the point where that complaint appears gives the delta a |
| 27 | codeword carries, with no audio analysis at all. |
| 28 | |
| 29 | Run `--stage sweep` to check the band tables, `--stage lengths` for the prefix |
| 30 | walk, `--stage values` for the assignment, `--stage all` for the lot; the result |
| 31 | is written as Rust to crates/ec-aac/src/sbr_tables.rs. |
| 32 | """ |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | = |
| 44 | = |
| 45 | |
| 46 | |
| 47 | # noqa: E402 |
| 48 | |
| 49 | = |
| 50 | |
| 51 | # ---------------------------------------------------------------- band tables |
| 52 | |
| 53 | |
| 54 | |
| 55 | """`num` band widths geometrically spaced from `start` to `stop`.""" |
| 56 | = ** |
| 57 | = |
| 58 | = |
| 59 | = |
| 60 | |
| 61 | *= |
| 62 | = |
| 63 | |
| 64 | = |
| 65 | |
| 66 | return |
| 67 | |
| 68 | |
| 69 | # `k0` by SBR sample rate and `bs_start_freq`, measured with `--stage k0`: a |
| 70 | # header whose reconstruction span exceeds the tool's limit is refused with the |
| 71 | # span in the message, and `k2` is known from `bs_stop_freq`, so each refusal |
| 72 | # reads `k0` straight out. The tail every row shares (0, 1, 2 ... 7, 9, 11, 13, |
| 73 | # 16, 20, 24) continues past the last cell any refusal could reach; those cells |
| 74 | # are the fitted ones, checked by `--stage fill`. |
| 75 | = |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | """`(start_min, stop_min)`: the QMF band a 3, 4 or 5 kHz border falls in.""" |
| 90 | = 3000 |
| 91 | return // , // |
| 92 | |
| 93 | |
| 94 | |
| 95 | """The widest `k2 - k0` the tool will reconstruct, measured the same way.""" |
| 96 | |
| 97 | return 48 |
| 98 | return 35 |
| 99 | |
| 100 | |
| 101 | |
| 102 | """The master frequency band table (ISO/IEC 14496-3 4.6.18.3.2.1). |
| 103 | |
| 104 | `rate` is the SBR rate: twice the core's. The two band-count rules and the |
| 105 | `k0` table are the reference parser's, read off it by `--stage k0` and |
| 106 | `--stage sweep`; the shapes are the standard's. |
| 107 | """ |
| 108 | = |
| 109 | |
| 110 | return None |
| 111 | , = |
| 112 | = + |
| 113 | |
| 114 | = + |
| 115 | == 14: |
| 116 | = 2 * |
| 117 | |
| 118 | = 3 * |
| 119 | = |
| 120 | |
| 121 | return None |
| 122 | |
| 123 | |
| 124 | # Bands of a constant width, an even number of them: the count is the |
| 125 | # span in whole double-width steps, biased by one step when the width |
| 126 | # is doubled. Both edges of that rule are measured, not assumed. |
| 127 | = 2 |
| 128 | = 2 * |
| 129 | |
| 130 | return None |
| 131 | = * |
| 132 | = - - * |
| 133 | |
| 134 | , = 0, 1 |
| 135 | |
| 136 | , = - 1, -1 |
| 137 | |
| 138 | -= |
| 139 | += |
| 140 | += |
| 141 | = |
| 142 | |
| 143 | |
| 144 | return None |
| 145 | |
| 146 | return |
| 147 | |
| 148 | = 7 - |
| 149 | = 49 * > 110 * |
| 150 | = 2 * |
| 151 | = 2 * |
| 152 | |
| 153 | return None |
| 154 | = |
| 155 | = |
| 156 | = |
| 157 | |
| 158 | |
| 159 | return None |
| 160 | |
| 161 | |
| 162 | return |
| 163 | = 1.0 / 1.3 |
| 164 | = 2 * |
| 165 | |
| 166 | return None |
| 167 | = |
| 168 | |
| 169 | = |
| 170 | += |
| 171 | -= |
| 172 | |
| 173 | |
| 174 | |
| 175 | return None |
| 176 | |
| 177 | return |
| 178 | |
| 179 | |
| 180 | |
| 181 | """`(n_master, n_high, n_low, n_q, f_high, f_low, f_noise)` or None.""" |
| 182 | = |
| 183 | |
| 184 | return None |
| 185 | = - 1 |
| 186 | = |
| 187 | |
| 188 | return None |
| 189 | = - |
| 190 | |
| 191 | return None |
| 192 | = >> 1 |
| 193 | = |
| 194 | = |
| 195 | |
| 196 | += |
| 197 | |
| 198 | += |
| 199 | = |
| 200 | = |
| 201 | = |
| 202 | |
| 203 | return None |
| 204 | = |
| 205 | = 0 |
| 206 | |
| 207 | += // |
| 208 | |
| 209 | return |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | # ------------------------------------------------------------- payload writer |
| 216 | |
| 217 | # Stand-in bits for a Huffman position whose codeword this probe does not care |
| 218 | # about; the parser reads as many as its own syntax says, not as many as these. |
| 219 | = * 24 |
| 220 | # Raw start value widths: `(balance, amp_res)` for an envelope, and the noise |
| 221 | # floor's. Measured by `--stage widths`, which reads them off the byte count. |
| 222 | = |
| 223 | = 5 |
| 224 | |
| 225 | |
| 226 | |
| 227 | """FIXFIX with a single envelope forces the 1.5 dB resolution.""" |
| 228 | return 0 |
| 229 | |
| 230 | |
| 231 | |
| 232 | = |
| 233 | return f |
| 234 | |
| 235 | |
| 236 | |
| 237 | return f |
| 238 | |
| 239 | |
| 240 | |
| 241 | """The Huffman slots one channel spends, as `(kind, raw_bits)` steps.""" |
| 242 | = |
| 243 | = 0 |
| 244 | = |
| 245 | |
| 246 | |
| 247 | += |
| 248 | += * |
| 249 | |
| 250 | += * |
| 251 | = 1 |
| 252 | |
| 253 | |
| 254 | += |
| 255 | += * |
| 256 | |
| 257 | += * |
| 258 | return , |
| 259 | |
| 260 | |
| 261 | |
| 262 | """`(kinds, known_bits)`: the codebook of every Huffman slot, in order, and |
| 263 | the bits the payload spends outside them.""" |
| 264 | = 0 |
| 265 | |
| 266 | += 1 + 1 + 4 + 4 + 3 + 2 + 1 + 1 |
| 267 | += 5 |
| 268 | += 6 |
| 269 | |
| 270 | += 1 |
| 271 | = == |
| 272 | += 1 + |
| 273 | = |
| 274 | |
| 275 | += 1 # bs_coupling |
| 276 | = |
| 277 | += 2 + 2 + 1 # sbr_grid, FIXFIX |
| 278 | = 1 |
| 279 | += * # sbr_dtdf per channel |
| 280 | += 2 * # sbr_invf, sent once under coupling |
| 281 | = |
| 282 | |
| 283 | = 1 |
| 284 | , = |
| 285 | += |
| 286 | += |
| 287 | |
| 288 | += 1 + |
| 289 | += 1 |
| 290 | |
| 291 | += 4 + 8 * |
| 292 | return , |
| 293 | |
| 294 | |
| 295 | |
| 296 | return |
| 297 | |
| 298 | |
| 299 | |
| 300 | """The SBR payload as a bit list; `words` fills the Huffman slots in order.""" |
| 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 | = 1 |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | = |
| 335 | = 1 |
| 336 | = |
| 337 | # bs_frame_class = FIXFIX |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | = 1 |
| 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 | return |
| 380 | |
| 381 | |
| 382 | = |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | """An AAC-LC core element of exactly known length, one or two channels.""" |
| 394 | |
| 395 | |
| 396 | return |
| 397 | = |
| 398 | |
| 399 | = # everything after the element id and instance tag |
| 400 | # CPE, tag, common_window = 0 |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | """One ADTS access unit: an AAC-LC core plus the SBR FIL element.""" |
| 407 | = |
| 408 | |
| 409 | = |
| 410 | # FIL |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | # EXT_SBR_DATA |
| 417 | |
| 418 | # END |
| 419 | # Slack, so a parser that reads a few bits more than this writer laid down |
| 420 | # still has bits to read: what it consumes is the measurement. |
| 421 | |
| 422 | return |
| 423 | |
| 424 | |
| 425 | # -------------------------------------------------------------------- oracle |
| 426 | |
| 427 | = |
| 428 | |
| 429 | |
| 430 | |
| 431 | """Runs the reference decoder on probe frames, with a disk cache.""" |
| 432 | |
| 433 | |
| 434 | = |
| 435 | = 0 |
| 436 | = 0 |
| 437 | |
| 438 | |
| 439 | = |
| 440 | |
| 441 | = |
| 442 | |
| 443 | |
| 444 | = + |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | , , = |
| 451 | = f |
| 452 | |
| 453 | |
| 454 | = |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | return , |
| 460 | |
| 461 | |
| 462 | """`items` is a list of `(key, frame_bytes)`; returns {key: stderr}.""" |
| 463 | = |
| 464 | = |
| 465 | = |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | = |
| 472 | |
| 473 | |
| 474 | = |
| 475 | += 1 |
| 476 | += - |
| 477 | return |
| 478 | |
| 479 | |
| 480 | return |
| 481 | |
| 482 | |
| 483 | |
| 484 | """The byte count the parser consumed, from its last complaint.""" |
| 485 | = |
| 486 | return |
| 487 | |
| 488 | |
| 489 | |
| 490 | """A probe file: silence, a header-bearing frame when the probe carries no |
| 491 | header of its own, then the probe frames.""" |
| 492 | |
| 493 | |
| 494 | = |
| 495 | |
| 496 | = |
| 497 | , = |
| 498 | += |
| 499 | = |
| 500 | return b |
| 501 | |
| 502 | |
| 503 | |
| 504 | return |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | """`cases` is a list of `(cfg, tables, words)`; returns `(bytes_read, err)`.""" |
| 516 | = |
| 517 | = |
| 518 | return |
| 519 | |
| 520 | |
| 521 | |
| 522 | = + |
| 523 | return // 8 |
| 524 | |
| 525 | |
| 526 | # ----------------------------------------------------------------- codebooks |
| 527 | |
| 528 | = |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | # The values the parser assumes when `bs_header_extra_1` is absent. A probe |
| 535 | # that carries exactly these can drop the field without changing a single |
| 536 | # derived table, which is what makes the five-bit shift usable as a knob. |
| 537 | = |
| 538 | |
| 539 | |
| 540 | |
| 541 | """A header whose derived band tables have the shape a probe needs.""" |
| 542 | = |
| 543 | = |
| 544 | = |
| 545 | = |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | = |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | = |
| 558 | |
| 559 | continue |
| 560 | |
| 561 | = |
| 562 | = |
| 563 | |
| 564 | continue |
| 565 | |
| 566 | continue |
| 567 | |
| 568 | continue |
| 569 | |
| 570 | continue |
| 571 | |
| 572 | return , |
| 573 | return None |
| 574 | |
| 575 | |
| 576 | # One probe per codebook. Each is a payload in which the slots of that |
| 577 | # codebook come last and everything before them is either a field this script |
| 578 | # wrote or a codeword already derived: the bits the parser spends past the |
| 579 | # known part are the codeword under test, and nothing else. |
| 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 | return None |
| 622 | , = |
| 623 | , = |
| 624 | |
| 625 | return None |
| 626 | return , |
| 627 | |
| 628 | |
| 629 | |
| 630 | """Payloads that differ only in fields of a size this script knows. |
| 631 | |
| 632 | A byte count pins the bits the parser read to within a byte; shifting the |
| 633 | payload by 4, 5 and 6 bits and reading the count again pins it exactly. |
| 634 | None of these knobs changes the frequency tables or the slots. |
| 635 | """ |
| 636 | = |
| 637 | = |
| 638 | = |
| 639 | # Only fields *before* the slot under test can serve as a knob: the parser |
| 640 | # stops inside the padded slot word, so everything this script writes after |
| 641 | # it sits at an offset the parser never reaches. |
| 642 | |
| 643 | |
| 644 | |
| 645 | = |
| 646 | = - |
| 647 | |
| 648 | = |
| 649 | = |
| 650 | |
| 651 | |
| 652 | continue |
| 653 | |
| 654 | |
| 655 | return |
| 656 | |
| 657 | |
| 658 | # A decoded value the tool refuses is a value it never finished reading, so a |
| 659 | # probe that says this measured nothing. |
| 660 | = |
| 661 | |
| 662 | # Raw start values to try: a noise floor's accumulated value has no centre that |
| 663 | # survives both ends of its table (0 and 30 are the limits the tool enforces), |
| 664 | # so both ends are tried and the probe it does not refuse is the one that |
| 665 | # measured something. |
| 666 | # A balance value is checked doubled (a coupled channel's noise floor is the |
| 667 | # base channel's combined with the ratio the balance value codes), so 16 -- the |
| 668 | # middle of the raw field -- can already read as 32 and refuse on its own, |
| 669 | # before the codeword under test ever contributes: the extra, smaller-magnitude |
| 670 | # pairs are the fallback once that is observed (`exact_bits` requires a sane |
| 671 | # 1..32 answer, not just an unrefused one). |
| 672 | = |
| 673 | |
| 674 | |
| 675 | |
| 676 | return |
| 677 | |
| 678 | |
| 679 | |
| 680 | """A derived codeword of this book, `extra` bits longer than its shortest. |
| 681 | |
| 682 | Swapping one filler for a longer one shifts the payload by a known number of |
| 683 | bits, which is the knob a coupled probe would otherwise lack. |
| 684 | """ |
| 685 | |
| 686 | return None |
| 687 | = |
| 688 | = + |
| 689 | |
| 690 | |
| 691 | return |
| 692 | return None |
| 693 | |
| 694 | |
| 695 | |
| 696 | """Slot words: `word` in the last slot of `kind`, derived codewords before.""" |
| 697 | , = |
| 698 | = |
| 699 | # An envelope value has room for any delta its table holds, so swapping an |
| 700 | # envelope filler is safe; a noise floor's does not, so it is a last resort. |
| 701 | = |
| 702 | += |
| 703 | = None |
| 704 | |
| 705 | |
| 706 | = |
| 707 | break |
| 708 | = |
| 709 | = not |
| 710 | |
| 711 | |
| 712 | |
| 713 | continue |
| 714 | |
| 715 | = |
| 716 | |
| 717 | |
| 718 | = True |
| 719 | continue |
| 720 | = |
| 721 | |
| 722 | return None |
| 723 | |
| 724 | return |
| 725 | |
| 726 | |
| 727 | |
| 728 | """The length of the codeword `word` starts with, exactly. |
| 729 | |
| 730 | A byte count pins the bits the parser read to within a byte. Shifting the |
| 731 | payload ahead of the slot -- by dropping a header group, by adding the |
| 732 | reserved data-extra field, or by swapping one already-derived filler |
| 733 | codeword for a longer one -- and reading the count again pins it exactly. |
| 734 | """ |
| 735 | = |
| 736 | = |
| 737 | = |
| 738 | |
| 739 | = |
| 740 | |
| 741 | continue |
| 742 | |
| 743 | = + |
| 744 | |
| 745 | |
| 746 | |
| 747 | break |
| 748 | |
| 749 | return None |
| 750 | # Refusing a value stops the parse right after the codeword that carried |
| 751 | # it, so the fields the payload spends after the slot go unread: that is a |
| 752 | # measurement too, of the same codeword, with a known amount less around it. |
| 753 | = 1 + |
| 754 | |
| 755 | = |
| 756 | |
| 757 | = |
| 758 | , , = -10 ** 6, 10 ** 6, False |
| 759 | |
| 760 | |
| 761 | = True |
| 762 | break |
| 763 | = 8 * - 4 - + |
| 764 | , = , |
| 765 | |
| 766 | return |
| 767 | return None |
| 768 | |
| 769 | |
| 770 | |
| 771 | """Enumerates a complete prefix code by walking its intervals. |
| 772 | |
| 773 | A complete code tiles the unit interval: the codeword at `cur` is the first |
| 774 | `L` bits of `cur`, and the next one starts at `cur + 2^-L`. One length |
| 775 | measurement per codeword enumerates the whole book, and the walk landing |
| 776 | exactly on 1 is the completeness check. |
| 777 | """ |
| 778 | = |
| 779 | , , = 0, 0, 0.0 |
| 780 | |
| 781 | = |
| 782 | = |
| 783 | |
| 784 | # A complete code ends on a single interval, and its width is the |
| 785 | # last codeword's length: that one needs no probe. |
| 786 | = 1.0 - |
| 787 | = |
| 788 | |
| 789 | = |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | return None |
| 796 | |
| 797 | <<= 1 |
| 798 | += 1 |
| 799 | |
| 800 | += 2.0 ** - |
| 801 | += 1 << |
| 802 | |
| 803 | >>= 1 |
| 804 | -= 1 |
| 805 | |
| 806 | break |
| 807 | |
| 808 | |
| 809 | return None |
| 810 | |
| 811 | |
| 812 | |
| 813 | return |
| 814 | |
| 815 | |
| 816 | |
| 817 | """The first codeword of a book, when there is no filler for it yet. |
| 818 | |
| 819 | Every slot of the kind carries the same candidate word, so the length that |
| 820 | explains the bit count is the one the parser actually read. |
| 821 | """ |
| 822 | , = |
| 823 | |
| 824 | = |
| 825 | return True |
| 826 | |
| 827 | = |
| 828 | |
| 829 | return True |
| 830 | |
| 831 | return False |
| 832 | |
| 833 | |
| 834 | |
| 835 | """Derives every codebook, each with the ones it leans on already in hand.""" |
| 836 | = |
| 837 | = |
| 838 | |
| 839 | |
| 840 | continue |
| 841 | = |
| 842 | |
| 843 | |
| 844 | continue |
| 845 | , = |
| 846 | , = |
| 847 | = |
| 848 | |
| 849 | |
| 850 | continue |
| 851 | |
| 852 | |
| 853 | continue |
| 854 | = |
| 855 | |
| 856 | |
| 857 | continue |
| 858 | = = |
| 859 | return |
| 860 | |
| 861 | |
| 862 | # -------------------------------------------------------------- values stage |
| 863 | |
| 864 | # `env_facs_q`/`noise_facs_q` are the reference decoder's own accumulated |
| 865 | # scalefactor: it prints the value it computed the instant that value leaves |
| 866 | # the valid range, which is `raw + delta` for a plain codebook or, measured |
| 867 | # below, `2 * (raw + delta)` for a balance one (the coupled channel's value is |
| 868 | # reconstructed from double the coded ratio). Placing the codeword under test |
| 869 | # at `raw = 0` and at the field's own maximum brackets every delta the ten |
| 870 | # tables carry: a delta that overflows high is read straight off the |
| 871 | # maximum-raw probe, a delta that overflows low wraps through the decoder's |
| 872 | # own byte-sized accumulator and is read off the zero-raw probe once |
| 873 | # unwrapped -- the write-up above is what pins the wrap width at 256 and the |
| 874 | # scale at 1 or 2, not an assumption. |
| 875 | = |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | = |
| 881 | return |
| 882 | |
| 883 | |
| 884 | |
| 885 | """A config in which `kind` carries exactly one codeword, plus the raw |
| 886 | field/width/scale that codeword's delta is read against.""" |
| 887 | = or |
| 888 | = |
| 889 | = 1 |
| 890 | = 2 |
| 891 | = |
| 892 | |
| 893 | |
| 894 | |
| 895 | # `amp_res_of` forces the 1.5 dB resolution whenever there is |
| 896 | # only one envelope, so a lone envelope can never carry the |
| 897 | # 3 dB book: a second envelope (T-coded, already closed) has |
| 898 | # to ride along to keep the resolution at 3 dB. |
| 899 | = |
| 900 | |
| 901 | |
| 902 | = |
| 903 | |
| 904 | = |
| 905 | , = , |
| 906 | |
| 907 | = |
| 908 | |
| 909 | = |
| 910 | , = , |
| 911 | |
| 912 | return None |
| 913 | , = |
| 914 | |
| 915 | = |
| 916 | |
| 917 | return None |
| 918 | , = |
| 919 | |
| 920 | , = , |
| 921 | |
| 922 | , = , |
| 923 | , = |
| 924 | |
| 925 | return None |
| 926 | return , , , , |
| 927 | |
| 928 | |
| 929 | |
| 930 | """`{(length, code): delta}` for every codeword `stage_books` found.""" |
| 931 | = |
| 932 | = - 1 |
| 933 | = |
| 934 | = |
| 935 | , = , |
| 936 | |
| 937 | # The exact codeword, no trailing pad: `stage_books` already closed the |
| 938 | # length for every codeword here, so (unlike the length-search bisection, |
| 939 | # which pads because the length is what it is looking for) padding would |
| 940 | # only shift every field after this slot and desync the frame. |
| 941 | = |
| 942 | = |
| 943 | |
| 944 | continue |
| 945 | |
| 946 | = |
| 947 | = |
| 948 | = |
| 949 | = |
| 950 | |
| 951 | |
| 952 | = |
| 953 | = |
| 954 | = |
| 955 | = |
| 956 | |
| 957 | = |
| 958 | |
| 959 | # Not a range-check complaint: either a clean decode (only the |
| 960 | # routine byte-count line) or the parser aborting the element for |
| 961 | # an unrelated reason -- only the former licenses the "both ends |
| 962 | # valid" inference below, so the two are told apart here. The |
| 963 | # "channel element 0.1 is not allocated" complaint fires on every |
| 964 | # mono probe regardless of whether the codeword itself was in |
| 965 | # range (the reference decoder treats HE-AAC mono as implicit |
| 966 | # stereo and always misses the second channel element), so it |
| 967 | # carries no information; the byte count the parser actually |
| 968 | # consumed against the full predicted frame length is the real |
| 969 | # discriminator -- an undershoot is a genuine early abort. |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | continue |
| 975 | = |
| 976 | |
| 977 | = % 256 |
| 978 | |
| 979 | -= 256 |
| 980 | |
| 981 | continue |
| 982 | = // |
| 983 | |
| 984 | |
| 985 | continue |
| 986 | = // - |
| 987 | |
| 988 | , = , |
| 989 | |
| 990 | = |
| 991 | |
| 992 | |
| 993 | # Neither end complained and neither crashed either: the |
| 994 | # codeword's delta and the raw value it rides with both |
| 995 | # stayed inside `[0, raw_max]`, which only `delta = 0` can do |
| 996 | # at *both* ends of the field at once. |
| 997 | = 0 |
| 998 | |
| 999 | |
| 1000 | continue |
| 1001 | |
| 1002 | |
| 1003 | continue |
| 1004 | = |
| 1005 | return , |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | """Assigns the delta value every codeword of every closed book carries.""" |
| 1010 | = |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | continue |
| 1015 | = |
| 1016 | |
| 1017 | |
| 1018 | continue |
| 1019 | , , , , = |
| 1020 | , = |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | = |
| 1025 | , = , |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | return |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | """Emits the closed, fully-valued books as `crates/ec-aac/src/sbr_tables.rs`.""" |
| 1034 | = or |
| 1035 | = |
| 1036 | = |
| 1037 | = |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | = |
| 1055 | = |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | return , |
| 1093 | |
| 1094 | |
| 1095 | # ---------------------------------------------------------------- raw widths |
| 1096 | |
| 1097 | = |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | """Configurations with no Huffman slot at all, and what they spend on raw |
| 1102 | start values -- the widths are the only thing in them this script guesses.""" |
| 1103 | = |
| 1104 | = |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | |
| 1120 | = |
| 1121 | |
| 1122 | = |
| 1123 | |
| 1124 | continue |
| 1125 | , = |
| 1126 | assert not , |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | = |
| 1131 | |
| 1132 | return |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | """Solves the raw start value widths against the byte counts.""" |
| 1137 | global RAW_ENV, RAW_NOISE |
| 1138 | = |
| 1139 | # The widths themselves are what the probe is measuring, so they must not |
| 1140 | # enter the prediction: zero them while the equations are built. |
| 1141 | = |
| 1142 | |
| 1143 | = |
| 1144 | = |
| 1145 | |
| 1146 | |
| 1147 | continue |
| 1148 | # The written payload keeps the guessed widths -- what it holds cannot |
| 1149 | # change what the parser reads -- so only the prediction drops them. |
| 1150 | = - |
| 1151 | |
| 1152 | = |
| 1153 | = |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | = |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | = |
| 1167 | = |
| 1168 | = |
| 1169 | |
| 1170 | |
| 1171 | return |
| 1172 | |
| 1173 | |
| 1174 | # ---------------------------------------------------------------- stage sweep |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | """A configuration whose payload holds no Huffman codeword at all. |
| 1179 | |
| 1180 | `bs_xover_band` is chosen so `n_high` is 2, which with `bs_freq_res = 0` |
| 1181 | leaves a single envelope band: the raw start value and nothing else. |
| 1182 | """ |
| 1183 | = |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | = |
| 1189 | = |
| 1190 | |
| 1191 | return None |
| 1192 | = - 2 |
| 1193 | |
| 1194 | return None |
| 1195 | = |
| 1196 | = |
| 1197 | |
| 1198 | return None |
| 1199 | # The core is analysed with 32 QMF bands, so a crossover above band 32 is |
| 1200 | # refused: measured, and the reason several sweep cells used to disagree. |
| 1201 | |
| 1202 | return None |
| 1203 | return , |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | """Checks the band derivation against the decoder, over many headers.""" |
| 1208 | = |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | = |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | = |
| 1224 | = 0 |
| 1225 | |
| 1226 | = |
| 1227 | |
| 1228 | += 1 |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | return == 0 |
| 1238 | |
| 1239 | |
| 1240 | = |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | """`k2` for each `bs_stop_freq` below 14 (4.6.18.3.2.1).""" |
| 1245 | = |
| 1246 | = |
| 1247 | = |
| 1248 | |
| 1249 | |
| 1250 | return |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | """Measures `k0` for every rate and `bs_start_freq`, from the decoder. |
| 1255 | |
| 1256 | A header whose reconstruction span `k2 - k0` exceeds what the tool allows is |
| 1257 | refused with the span itself in the message, and `k2` follows from |
| 1258 | `bs_stop_freq` alone: one refusal per configuration therefore reads `k0` |
| 1259 | straight out, and the several `bs_stop_freq` values that all refuse have to |
| 1260 | agree on it, which is the check that the stop table is right too. |
| 1261 | """ |
| 1262 | = |
| 1263 | = |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | = |
| 1268 | |
| 1269 | |
| 1270 | = |
| 1271 | = |
| 1272 | |
| 1273 | = |
| 1274 | |
| 1275 | continue |
| 1276 | = |
| 1277 | = |
| 1278 | = - |
| 1279 | |
| 1280 | |
| 1281 | = |
| 1282 | |
| 1283 | = |
| 1284 | |
| 1285 | = |
| 1286 | |
| 1287 | = |
| 1288 | = |
| 1289 | = 3000 |
| 1290 | = // |
| 1291 | = |
| 1292 | |
| 1293 | |
| 1294 | return |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | """Measures the decoder's own `n_master` and compares it with ours. |
| 1299 | |
| 1300 | `bs_xover_band > n_master` is refused with a message of its own, so the |
| 1301 | largest accepted crossover *is* `n_master` whenever it fits in the field. |
| 1302 | """ |
| 1303 | = |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | = |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | = |
| 1316 | |
| 1317 | |
| 1318 | = |
| 1319 | = or |
| 1320 | |
| 1321 | = |
| 1322 | = |
| 1323 | |
| 1324 | = |
| 1325 | = not in |
| 1326 | = |
| 1327 | = 0 |
| 1328 | = 0 |
| 1329 | = 0 |
| 1330 | |
| 1331 | = |
| 1332 | = |
| 1333 | |
| 1334 | |
| 1335 | = |
| 1336 | = |
| 1337 | = |
| 1338 | # An out-of-range header is refused for every crossover, ours says None. |
| 1339 | |
| 1340 | = < 0 or |
| 1341 | += 1 |
| 1342 | |
| 1343 | += 1 |
| 1344 | |
| 1345 | += 1 |
| 1346 | |
| 1347 | |
| 1348 | continue |
| 1349 | += 1 |
| 1350 | |
| 1351 | continue # the field caps at 7: no information above it |
| 1352 | |
| 1353 | += 1 |
| 1354 | |
| 1355 | += 1 |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | """Repeats `stage_books` until no further kind closes, honoring the |
| 1363 | dependency order `stage_books` itself discovers via its `missing` check.""" |
| 1364 | = |
| 1365 | = |
| 1366 | |
| 1367 | = |
| 1368 | |
| 1369 | break |
| 1370 | -= |
| 1371 | |
| 1372 | |
| 1373 | return |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | = |
| 1378 | |
| 1379 | |
| 1380 | = |
| 1381 | = |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | = None |
| 1392 | |
| 1393 | = |
| 1394 | |
| 1395 | = |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 |