edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBa913c84Tahinli3 months ago146 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-29): the sanity gate still FAILS after two real fixes landed |
| 40 | this round (see the ledger's round-29 facts): (1) `sbr_bits_full`'s `slot()` |
| 41 | default (`T.FILL`, 24 raw zero bits) desyncs the bitstream whenever a |
| 42 | multi-band delta run is used -- ENV30_F/NOISE_F's shortest codeword (the one |
| 43 | "0" bits actually decode as) is only 1 bit, so 23 excess zero bits per slot |
| 44 | leak into every field transmitted after it; `stream_one_band` now supplies |
| 45 | exactly enough real 1-bit zero-delta codewords instead (fixed, confirmed via |
| 46 | `EC_AAC_SBR_DEBUG`: `e_q[1]`/`q_q[1]` now read back the intended raw values, |
| 47 | were silently zeroed before). (2) The tone's fractional-offset placement |
| 48 | (`GROUP_OFFSET`) is unaffected by that fix. What remains OPEN: `k = active_p |
| 49 | * 16` (the MDCT-bin-per-QMF-band assumption every prior round's readout used, |
| 50 | inherited unchanged here) does not track the true source-band placement -- |
| 51 | calibration by sweeping raw bin `k` and reading which target band lights up |
| 52 | shows a clean *32*-bin-per-band slope (not 16) locally, but with an offset |
| 53 | that is NOT a fixed constant across headers (k=64 lands on `target == kx` |
| 54 | for headers A and C, whose `patches[0]` happens to have `source_start == 0`, |
| 55 | but for header B -- `xover_band=1`, whose `patches[0]` has `source_start == |
| 56 | 2` -- k=64 *also* lands on `target == kx`, meaning it tracked `source_start` |
| 57 | itself, not a fixed `p`). The low/source band index space is not a simple |
| 58 | `p*N + offset` function of MDCT bin position; it needs deriving from the |
| 59 | tables' own low-band structure rather than assumed, which this round did not |
| 60 | reach. Sanity gate: DO NOT trust `measured(ref)` output below until this is |
| 61 | resolved -- Task 2/3 (reading and acting on the reference's map) are blocked |
| 62 | on it. |
| 63 | """ |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | = |
| 71 | |
| 72 | |
| 73 | # noqa: E402 |
| 74 | # noqa: E402 |
| 75 | # noqa: E402 |
| 76 | # noqa: E402 |
| 77 | |
| 78 | = 7 |
| 79 | = 44100 |
| 80 | = |
| 81 | = / 128 # QMF band width, both source and target index units |
| 82 | = 0.25 # fractional offset (of one band width) the probe tone is placed at |
| 83 | = 4 # bins: 4/16 == D, the finest offset tone_core's grouping allows |
| 84 | = |
| 85 | |
| 86 | |
| 87 | |
| 88 | """Python port of `build_patches` (crates/ec-aac/src/sbr_hf.rs), for |
| 89 | comparing the reference's measured map against our own prediction.""" |
| 90 | = |
| 91 | |
| 92 | return |
| 93 | , = , |
| 94 | |
| 95 | , = 0, |
| 96 | |
| 97 | = |
| 98 | |
| 99 | break |
| 100 | = - |
| 101 | |
| 102 | break |
| 103 | += |
| 104 | = |
| 105 | |
| 106 | = |
| 107 | = - |
| 108 | |
| 109 | += |
| 110 | = |
| 111 | return |
| 112 | |
| 113 | |
| 114 | |
| 115 | = |
| 116 | |
| 117 | |
| 118 | |
| 119 | return |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | return , // 4 |
| 126 | return None |
| 127 | |
| 128 | |
| 129 | |
| 130 | = |
| 131 | |
| 132 | |
| 133 | = |
| 134 | return , |
| 135 | |
| 136 | |
| 137 | |
| 138 | """`n_frames` FIXFIX/2-env amp_res=1 frames (sbrchirp.py's `stream()` |
| 139 | engaging shape, byte-for-byte -- FIXFIX, num_env=2, amp_res=1, env0 at a |
| 140 | healthy mid value, noise0 at the quiet end so noise floor cannot bury the |
| 141 | line), core = one isolated tone at low QMF band `active_p`'s SECOND 4-bin |
| 142 | group (offset `D` of a band width into the band, not the ambiguous |
| 143 | boundary bin), held fixed; invf NONE, no harmonics.""" |
| 144 | = |
| 145 | = * 16 + |
| 146 | = |
| 147 | assert is not None, f |
| 148 | , = |
| 149 | = |
| 150 | = |
| 151 | # sbrtables.py's `slot()` falls back to `T.FILL` (24 raw zero bits) for |
| 152 | # any delta slot the caller doesn't supply explicit bits for -- fine for |
| 153 | # the bootstrap probes that don't yet know a codebook's length, but for |
| 154 | # amp_res=1/coupling=0/df_env=df_noise=0 (this probe's exact config) the |
| 155 | # book is known: ENV30_F and NOISE_F (crates/ec-aac/src/sbr_tables.rs) |
| 156 | # both give delta=0 its SHORTEST codeword, (len=1, code=0). 24 zero bits |
| 157 | # where only 1 is consumed leaves 23 unconsumed zero bits per slot that |
| 158 | # bleed into every field transmitted after it (env1's raw start value, |
| 159 | # noise0/1, ...), silently corrupting it to whatever a run of stray zero |
| 160 | # bits happens to decode as -- this is round-29's "readout is wrong" |
| 161 | # candidate B made concrete, and the fix. |
| 162 | = |
| 163 | = 2 * + 2 * |
| 164 | |
| 165 | = |
| 166 | |
| 167 | = |
| 168 | |
| 169 | = |
| 170 | |
| 171 | = // 8 |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | = |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | return |
| 186 | |
| 187 | |
| 188 | |
| 189 | = f |
| 190 | |
| 191 | |
| 192 | = |
| 193 | = |
| 194 | |
| 195 | return , |
| 196 | |
| 197 | |
| 198 | |
| 199 | """Power spectrum averaged over several overlapping steady-state windows |
| 200 | (Welch-style) for a cleaner noise floor than a single FFT window.""" |
| 201 | = |
| 202 | = |
| 203 | = |
| 204 | |
| 205 | = * |
| 206 | |
| 207 | += |
| 208 | assert , |
| 209 | = |
| 210 | = |
| 211 | return , |
| 212 | |
| 213 | |
| 214 | |
| 215 | """Local-maximum bins within [lo_hz, hi_hz) whose power clearly exceeds |
| 216 | the local noise floor (median of the in-range spectrum) -- power GATES |
| 217 | detection only, never decides the mapping. Sub-bin frequency via |
| 218 | parabolic interpolation around each peak.""" |
| 219 | = |
| 220 | |
| 221 | return |
| 222 | = |
| 223 | |
| 224 | = . |
| 225 | = - |
| 226 | = |
| 227 | |
| 228 | = |
| 229 | |
| 230 | , , = , , |
| 231 | = - 2 * + |
| 232 | = 0.5 * / |
| 233 | = |
| 234 | |
| 235 | return |
| 236 | |
| 237 | |
| 238 | |
| 239 | """Patch map read directly from fractional frequency offset: a detected |
| 240 | HF line in target band `q` counts as "source -> q" iff its offset within |
| 241 | the band matches `d` (direct) or `1 - d` (mirrored, per the charter's |
| 242 | filterbank-parity note -- logged, not discarded). Returns |
| 243 | (direct_targets, mirrored_targets, all_lines_for_debug).""" |
| 244 | = |
| 245 | , , = , , |
| 246 | |
| 247 | = |
| 248 | = / - |
| 249 | |
| 250 | |
| 251 | |
| 252 | <= : |
| 253 | |
| 254 | return , , |
| 255 | |
| 256 | |
| 257 | |
| 258 | , = |
| 259 | assert is not None, f |
| 260 | , = , |
| 261 | |
| 262 | |
| 263 | = |
| 264 | |
| 265 | |
| 266 | , , = , , |
| 267 | = True |
| 268 | |
| 269 | = |
| 270 | , = |
| 271 | , = |
| 272 | |
| 273 | |
| 274 | , = |
| 275 | , = |
| 276 | , , = |
| 277 | , , = |
| 278 | = |
| 279 | = |
| 280 | = |
| 281 | |
| 282 | = False |
| 283 | = |
| 284 | = |
| 285 | = f |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | return , , , , , , |
| 301 | |
| 302 | |
| 303 | |
| 304 | assert , f |
| 305 | # Header A: the real file's ACTUAL SBR header, read straight off |
| 306 | # reeln's own bitstream via `EC_AAC_SBR_DEBUG=1` on our own decoder |
| 307 | # (start_freq=5 stop_freq=8 xover_band=0 freq_scale=2 alter_scale=1 |
| 308 | # noise_bands=2 -> kx=14 k2=43 n_q=3) -- NOT the `sbr_hf.rs` unit test's |
| 309 | # `real_file_tables()`, whose stop_freq/xover_band guess (3/2) was wrong |
| 310 | # and gives a different, smaller kx=16 k2=29 header. |
| 311 | |
| 312 | |
| 313 | # Headers B and C: different start/stop/xover -> differently-shaped |
| 314 | # kx/k2, to pin the algorithm's shape rather than one lucky header. |
| 315 | |
| 316 | |
| 317 | |
| 318 |