forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBffb600bTahinli2 months ago2850 commits

probe_engine.py54 lines, 1.9 KiBHistoryRawDownload
1"""Batch probe runner: many one-pair probes per ffmpeg invocation."""
2
3import numpy as np
4from mp3probe import Granule, frame_bytes, ffmpeg_decode
5
6GLOBAL_GAIN = 150
7SCALE = 2.0 ** ((GLOBAL_GAIN - 210) / 4.0)
8PART2_3 = 48
9
10
11class Engine:
12 def __init__(self, Minv):
13 self.Minv = Minv
14
15 def run(self, probes):
16 """probes: list of (bits, table_select, big_values, count1table_select).
17
18 Returns one array of 576 requantised coefficients per probe.
19 """
20 stream = b""
21 for i in range(0, len(probes), 2):
22 pair = probes[i:i + 2]
23 grans, mains = [], []
24 for bits, tsel, bigv, c1 in pair:
25 bits = list(bits) + [0] * (PART2_3 - len(bits))
26 grans.append(Granule(part2_3_length=PART2_3, big_values=bigv,
27 global_gain=GLOBAL_GAIN,
28 table_select=(tsel, tsel, tsel),
29 count1table_select=c1))
30 mains.append(bits[:PART2_3])
31 while len(grans) < 2:
32 grans.append(Granule())
33 mains.append([])
34 stream += frame_bytes(grans, mains)
35 stream += frame_bytes([Granule(), Granule()], [[], []])
36 pcm, err = ffmpeg_decode(stream)
37 nframes = (len(probes) + 1) // 2
38 assert len(pcm) >= nframes * 2304, (len(pcm), nframes * 2304, err)
39 windows = np.stack([pcm[i * 2304:(i + 1) * 2304] for i in range(nframes)])
40 coeffs = windows @ self.Minv # (nframes, 1152)
41 out = []
42 for i in range(len(probes)):
43 out.append(coeffs[i // 2][(i % 2) * 576:((i % 2) + 1) * 576])
44 return out
45
46
47def values(xr, n):
48 """First n integer magnitudes of a recovered granule."""
49 mag = np.abs(xr[:n]) / SCALE
50 return np.round(np.power(np.maximum(mag, 0.0), 0.75)).astype(np.int64)
51
52
53def signs(xr, n):
54 return np.sign(xr[:n]).astype(np.int64)