edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB5f6a10aTahinli2 months ago3237 commits
| 1 | #!/usr/bin/env python3 |
| 2 | """Round-29: measures the reference decoder's ACTUAL SBR HF patch map (which |
| 3 | low QMF source band feeds which high target band), by isolating one source |
| 4 | band at a time and reading off the FRACTIONAL FREQUENCY OFFSET of the |
| 5 | resulting HF line(s), not their power. |
| 6 | |
| 7 | Round-28 found that a power-bucket readout cannot work: the envelope adjuster |
| 8 | renormalizes every target band's POWER to the transmitted envelope regardless |
| 9 | of patch content, so every source band lit the SAME target range at similar |
| 10 | power (expected physics, not a bug). Gains rescale amplitude, never a line's |
| 11 | frequency position within its band, so the fix is to read frequency instead. |
| 12 | |
| 13 | Method: the core spectrum for a probe frame is a single isolated MDCT bin -- |
| 14 | `tone_core()` in `sbrchirp.py` -- placed not at low QMF band `p`'s first bin |
| 15 | (a group/band boundary, ambiguous under FFT leakage and coincident with the |
| 16 | target band edge too) but at its SECOND 4-bin group (`k = p*16 + 4`), i.e. a |
| 17 | known, non-zero fractional offset `D = 0.25` of a QMF band's width inside |
| 18 | band `p` (0.25 is the finest offset `tone_core`'s group granularity allows; |
| 19 | any of 0.25/0.5/0.75 is equally "distinctive", 0.25 chosen arbitrarily). |
| 20 | Patching copies that source band's complex QMF series verbatim into every |
| 21 | target band any patch maps it to, translating the tone in frequency by an |
| 22 | integer number of band widths -- so the offset is preserved (or, per the |
| 23 | charter's note on filterbank parity, possibly mirrored to `1 - D`) in |
| 24 | whichever target band(s) the patch actually lit up. An averaged FFT of the |
| 25 | decoded output finds every HF line clearly above the local noise floor |
| 26 | (power only GATES detection); for each line, `target band q = floor(f/bw)` |
| 27 | and `frac = f/bw - q` are computed, and a line is read as "source `p` -> |
| 28 | target `q`" iff `frac` is within a few percent of `D` (direct) or `1 - D` |
| 29 | (mirrored, logged separately as data, not discarded). |
| 30 | |
| 31 | Runs the same probes through our own decoder via the `adts_to_pcm` example |
| 32 | (`cargo build -p ec-aac --example adts_to_pcm`) for the sanity cross-check the |
| 33 | charter requires: our measured map must equal `build_patches`' prediction, or |
| 34 | the fingerprint method itself is the thing that's broken. Also checks the |
| 35 | reference's stderr for "No quantized data read for sbr_dequant." -- if |
| 36 | present the run used a shape that leaves the SBR gain stage unengaged and the |
| 37 | decode is not trustworthy. |
| 38 | |
| 39 | STATUS (round-30): the bin-per-band placement math is now fixed -- a source |
| 40 | QMF band is 1024/32 == 32 core-MDCT bins wide, not 16, so `stream_one_band` |
| 41 | places its probe tone at `k = active_p * 32 + 16` (the band CENTER, which |
| 42 | also lands exactly on a 4-bin group boundary, so no fractional-offset trick |
| 43 | is needed; `D` is 0.5, not 0.25). That fix, however, could NOT be calibrated |
| 44 | against the header-A anchor `(13, 42, 1)`, because a DEEPER, BLOCKING defect |
| 45 | was found first: the reference decoder is not actually engaging its SBR |
| 46 | upsampling path for ANY of these synthetic ADTS probe streams at all, for |
| 47 | either `tone_core` or `broadband_core_bits` content, regardless of header |
| 48 | shape (A's kx=14/k2=43 and the round-19 DEFAULT shape both affected |
| 49 | identically) -- |
| 50 | |
| 51 | - `ffprobe` on a probe stream built by `stream_one_band`/`stream` reports |
| 52 | `codec_name=aac`, NO `profile=HE-AAC`, `sample_rate=22050` (the bare |
| 53 | core/ADTS-declared rate) -- vs. the real `~/Music/reeln.mp4` |
| 54 | fixture (round-19's working baseline), which reports |
| 55 | `profile=HE-AAC sample_rate=44100`. Decoded PCM length for a 26-frame |
| 56 | probe is exactly `26 * 1024` samples, i.e. literally core-rate output, |
| 57 | never doubled -- this is not just a metadata-reporting quirk, the actual |
| 58 | decoded sample count proves SBR upsampling never ran. |
| 59 | - This is NOT visible from the `"No quantized data read for sbr_dequant."` |
| 60 | warning's raw occurrence count alone: it fires only ONCE across a |
| 61 | 26-frame stream (24 of them real SBR frames with `header=1` every |
| 62 | frame) -- by the charter's own "once-or-twice = benign" heuristic this |
| 63 | reads as a clean, engaged run. It is NOT: an envelope-value sweep |
| 64 | (`env0` raw 3..63, `amp_res=1`) shows the power *inside* the |
| 65 | `[kx*BAND_HZ, k2*BAND_HZ)` window our own table math predicts is |
| 66 | completely FLAT (no order-of-magnitude movement -- fails the charter's |
| 67 | own engagement test outright), while a huge, smoothly env0-scaling |
| 68 | power increase (orders of magnitude, from ~18 to ~6e9) shows up OUTSIDE |
| 69 | that window, concentrated roughly 9-15 kHz and tailing out past 20 kHz |
| 70 | -- i.e. real envelope-driven energy exists, but not where our table |
| 71 | math says the SBR target band should be, AND it never causes the |
| 72 | decoder to report itself as HE-AAC/44100 the way the real file does. |
| 73 | The likely explanation: without an out-of-band MPEG-4 `AudioSpecificConfig` |
| 74 | (which the real `.mp4` fixture's container carries and our bare `.aac` |
| 75 | ADTS files do not), the reference decoder's *implicit* SBR detection |
| 76 | from frame 1's fill element is deciding "no SBR" once, on the very |
| 77 | first payload frame, and locks the whole stream's output rate to |
| 78 | core-only from then on even though later frames carry ostensibly valid |
| 79 | bits -- consistent with, and a generalization of, round-27's dead-end |
| 80 | (`tone_core` "does not round-trip" against the real header/grid shape), |
| 81 | now shown to affect `broadband_core_bits` identically, so it is a |
| 82 | property of the shared frame/extension-payload construction in |
| 83 | `stream()`/`stream_one_band()`, not of either core-spectrum writer. |
| 84 | |
| 85 | Sanity gate: DO NOT trust `measured(ref)` output below -- it is not reading |
| 86 | genuine SBR-patched content at all, in either the old or new placement math. |
| 87 | Task 2/3/4 (reading and acting on the reference's map) remain BLOCKED, now |
| 88 | on getting the synthetic ADTS fixture writer to genuinely engage the |
| 89 | reference's implicit SBR/HE-AAC upsampling path (matching the real file's |
| 90 | `profile=HE-AAC sample_rate=44100`), not on the placement-math bug this |
| 91 | round set out to fix. |
| 92 | """ |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | = |
| 100 | |
| 101 | |
| 102 | # noqa: E402 |
| 103 | # noqa: E402 |
| 104 | # noqa: E402 |
| 105 | # noqa: E402 |
| 106 | |
| 107 | = 7 |
| 108 | = 44100 |
| 109 | = |
| 110 | # Round-32: field-by-field bisection (scripts/aac-tables/sbrbisect.py) proved |
| 111 | # stream_one_band's bit construction ALREADY engages the reference's SBR |
| 112 | # dequant once wrapped in an explicit-SBR ASC (env0 sweep moves in-band power |
| 113 | # ~1e12x, warning fires once at stream head only, PCM length doubles) -- |
| 114 | # every flip tried (header, core content, delta words) engaged identically. |
| 115 | # The round-30/31 "flat" reading was this module still calling `P.decode` |
| 116 | # (bare ADTS, implicit per-frame detection) on the reference side while |
| 117 | # `wrap_sbr` (round-31) sat unused; `ref_decode` below is the fix. |
| 118 | = |
| 119 | |
| 120 | |
| 121 | |
| 122 | assert , f |
| 123 | = f |
| 124 | = f |
| 125 | |
| 126 | |
| 127 | = |
| 128 | assert == 0, f |
| 129 | = |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | return , |
| 136 | = / 128 # QMF band width, both source and target index units |
| 137 | # Round-34 fix: round-30's "32 core-MDCT bins per QMF band" was itself wrong |
| 138 | # by 2x. Empirically re-derived by dumping our own decoder's QMF analysis |
| 139 | # stage (EC_AAC_SBR_QMFDUMP=1, crates/ec-aac/src/sbr_chain.rs) for k = |
| 140 | # active_p*32+16 across several p: the dominant analysis band always read |
| 141 | # back as 2p or 2p+1, never p -- a source (low) QMF band is 16 core-MDCT |
| 142 | # bins wide (1024-bin core MDCT / 64, not / 32), confirmed by re-running the |
| 143 | # same dump with k = active_p*16+8 and seeing the dominant band land exactly |
| 144 | # on p for every probed value (0, 5, 13). Placed at the BAND CENTER (bin 8 |
| 145 | # of the 16), which lands on a 4-bin group boundary (8 % 4 == 0) so |
| 146 | # tone_core's grouping needs no fractional-offset trick. A center tone's |
| 147 | # fractional position within its 16-bin source band is 0.5; patches |
| 148 | # translate by whole band widths, so the translated line keeps frac == 0.5 |
| 149 | # in its target band too (or 1-0.5 == 0.5 under mirroring -- indistinguishable |
| 150 | # at the center, which is fine, this probe only needs the TARGET BAND INDEX, |
| 151 | # not direct/mirror discrimination). |
| 152 | = 0.5 # fractional offset (of one band width) the probe tone is placed at |
| 153 | = |
| 154 | |
| 155 | |
| 156 | |
| 157 | """Python port of `build_patches` (crates/ec-aac/src/sbr_hf.rs), for |
| 158 | comparing the reference's measured map against our own prediction.""" |
| 159 | = |
| 160 | |
| 161 | return |
| 162 | , = , |
| 163 | |
| 164 | , = 0, |
| 165 | |
| 166 | = |
| 167 | |
| 168 | break |
| 169 | = - |
| 170 | |
| 171 | break |
| 172 | += |
| 173 | = |
| 174 | |
| 175 | = |
| 176 | = - |
| 177 | |
| 178 | += |
| 179 | = |
| 180 | return |
| 181 | |
| 182 | |
| 183 | |
| 184 | = |
| 185 | |
| 186 | |
| 187 | |
| 188 | return |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | return , // 4 |
| 195 | return None |
| 196 | |
| 197 | |
| 198 | |
| 199 | = |
| 200 | |
| 201 | |
| 202 | = |
| 203 | return , |
| 204 | |
| 205 | |
| 206 | |
| 207 | """`n_frames` FIXFIX/2-env amp_res=1 frames (sbrchirp.py's `stream()` |
| 208 | engaging shape, byte-for-byte -- FIXFIX, num_env=2, amp_res=1, env0 at a |
| 209 | healthy mid value, noise0 at the quiet end so noise floor cannot bury the |
| 210 | line), core = one isolated tone at low QMF band `active_p`'s SECOND 4-bin |
| 211 | group (offset `D` of a band width into the band, not the ambiguous |
| 212 | boundary bin), held fixed; invf NONE, no harmonics. |
| 213 | |
| 214 | Round-34: noise0's raw index runs the OPPOSITE direction from what this |
| 215 | docstring's "quiet end" phrase assumed -- `dequant_noise` |
| 216 | (`crates/ec-aac/src/sbr_env.rs`) is `2^(NOISE_FLOOR_OFFSET - q_q)`, so a |
| 217 | LOW raw index is the LOUD end (matches the spec's own noise-floor |
| 218 | dequantization direction) and `noise0=[2, 2]` was injecting a broadband |
| 219 | noise floor loud enough to swamp the QMF-domain energy of the probe's own |
| 220 | tone (confirmed via EC_AAC_SBR_QMFDUMP=1: post-adjust energy was flat |
| 221 | across every target band, not peaked at the patched ones). `[30, 30]` is |
| 222 | the genuinely quiet end.""" |
| 223 | = |
| 224 | = * 16 + 8 |
| 225 | = |
| 226 | assert is not None, f |
| 227 | , = |
| 228 | = |
| 229 | = |
| 230 | # sbrtables.py's `slot()` falls back to `T.FILL` (24 raw zero bits) for |
| 231 | # any delta slot the caller doesn't supply explicit bits for -- fine for |
| 232 | # the bootstrap probes that don't yet know a codebook's length, but for |
| 233 | # amp_res=1/coupling=0/df_env=df_noise=0 (this probe's exact config) the |
| 234 | # book is known: ENV30_F and NOISE_F (crates/ec-aac/src/sbr_tables.rs) |
| 235 | # both give delta=0 its SHORTEST codeword, (len=1, code=0). 24 zero bits |
| 236 | # where only 1 is consumed leaves 23 unconsumed zero bits per slot that |
| 237 | # bleed into every field transmitted after it (env1's raw start value, |
| 238 | # noise0/1, ...), silently corrupting it to whatever a run of stray zero |
| 239 | # bits happens to decode as -- this is round-29's "readout is wrong" |
| 240 | # candidate B made concrete, and the fix. |
| 241 | = |
| 242 | = 2 * + 2 * |
| 243 | |
| 244 | = |
| 245 | |
| 246 | = |
| 247 | |
| 248 | = |
| 249 | |
| 250 | = // 8 |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | = |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | return |
| 265 | |
| 266 | |
| 267 | |
| 268 | = f |
| 269 | |
| 270 | |
| 271 | = |
| 272 | = |
| 273 | |
| 274 | return , |
| 275 | |
| 276 | |
| 277 | |
| 278 | """Power spectrum averaged over several overlapping steady-state windows |
| 279 | (Welch-style) for a cleaner noise floor than a single FFT window.""" |
| 280 | = |
| 281 | = |
| 282 | = |
| 283 | |
| 284 | = * |
| 285 | |
| 286 | += |
| 287 | assert , |
| 288 | = |
| 289 | = |
| 290 | return , |
| 291 | |
| 292 | |
| 293 | |
| 294 | """Local-maximum bins within [lo_hz, hi_hz) whose power clearly exceeds |
| 295 | the local noise floor (median of the in-range spectrum) -- power GATES |
| 296 | detection only, never decides the mapping. Sub-bin frequency via |
| 297 | parabolic interpolation around each peak.""" |
| 298 | = |
| 299 | |
| 300 | return |
| 301 | = |
| 302 | |
| 303 | = . |
| 304 | = - |
| 305 | = |
| 306 | |
| 307 | = |
| 308 | |
| 309 | , , = , , |
| 310 | = - 2 * + |
| 311 | = 0.5 * / |
| 312 | = |
| 313 | |
| 314 | return |
| 315 | |
| 316 | |
| 317 | |
| 318 | """Patch map read directly from fractional frequency offset: a detected |
| 319 | HF line in target band `q` counts as "source -> q" iff its offset within |
| 320 | the band matches `d` (direct) or `1 - d` (mirrored, per the charter's |
| 321 | filterbank-parity note -- logged, not discarded). Returns |
| 322 | (direct_targets, mirrored_targets, all_lines_for_debug).""" |
| 323 | = |
| 324 | , , = , , |
| 325 | |
| 326 | = |
| 327 | = / - |
| 328 | |
| 329 | |
| 330 | |
| 331 | <= : |
| 332 | |
| 333 | return , , |
| 334 | |
| 335 | |
| 336 | |
| 337 | , = |
| 338 | assert is not None, f |
| 339 | , = , |
| 340 | |
| 341 | |
| 342 | = |
| 343 | |
| 344 | |
| 345 | , , = , , |
| 346 | = True |
| 347 | |
| 348 | = |
| 349 | , = |
| 350 | , = |
| 351 | |
| 352 | |
| 353 | , = |
| 354 | , = |
| 355 | , , = |
| 356 | , , = |
| 357 | = |
| 358 | = |
| 359 | = |
| 360 | |
| 361 | = False |
| 362 | = |
| 363 | = |
| 364 | = f |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | return , , , , , , |
| 380 | |
| 381 | |
| 382 | |
| 383 | assert , f |
| 384 | # Header A: the real file's ACTUAL SBR header, read straight off |
| 385 | # reeln's own bitstream via `EC_AAC_SBR_DEBUG=1` on our own decoder |
| 386 | # (start_freq=5 stop_freq=8 xover_band=0 freq_scale=2 alter_scale=1 |
| 387 | # noise_bands=2 -> kx=14 k2=43 n_q=3) -- NOT the `sbr_hf.rs` unit test's |
| 388 | # `real_file_tables()`, whose stop_freq/xover_band guess (3/2) was wrong |
| 389 | # and gives a different, smaller kx=16 k2=29 header. |
| 390 | |
| 391 | |
| 392 | # Headers B and C: different start/stop/xover -> differently-shaped |
| 393 | # kx/k2, to pin the algorithm's shape rather than one lucky header. |
| 394 | |
| 395 | |
| 396 | |
| 397 |