forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBd86dccbTahinli3 months ago783 commits

swb.py119 lines, 3.3 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Derive the scalefactor-band offset tables (ISO 14496-3 tbl 4.140) by probing.
3
4For a frame coded with max_sfb = k, the highest non-zero spectral coefficient is
5swb_offset[k] - 1. Long windows are read back through the exact MDCT inverse;
6short windows through the FFT of the 2048-sample frame, whose bin spacing
7(fs/2048) is eight times finer than one short-block coefficient (fs/256).
8"""
9
10import json
11import os
12import subprocess
13import sys
14
15import numpy as np
16
17import aacprobe as A
18import learn as L
19
20BASIS = np.cos(np.pi / 1024 * np.outer(np.arange(2048) + 512.5, np.arange(1024) + 0.5))
21
22
23def sect_len(w, k, bits):
24 esc = (1 << bits) - 1
25 left = k
26 while left >= esc:
27 w.w(esc, bits)
28 left -= esc
29 w.w(left, bits)
30
31
32def frame_long(k, sf_index):
33 w = A.BitW()
34 w.w(0, 3).w(0, 4).w(200, 8)
35 w.w(0, 1).w(0, 2).w(0, 1).w(k, 6).w(0, 1)
36 w.w(3, 4)
37 sect_len(w, k, 5)
38 w.wbits([0] * k) # the one-bit delta-zero scalefactor codeword per band
39 w.w(0, 3)
40 w.wbits([1] * 6000)
41 return A.adts(w.pack(pad=0), sf_index=sf_index)
42
43
44def frame_short(k, sf_index):
45 w = A.BitW()
46 w.w(0, 3).w(0, 4).w(200, 8)
47 w.w(0, 1).w(2, 2).w(0, 1).w(k, 4).w(0x7F, 7) # EIGHT_SHORT, one group of 8
48 w.w(3, 4)
49 sect_len(w, k, 3)
50 w.wbits([0] * k)
51 w.w(0, 3)
52 w.wbits([1] * 6000)
53 return A.adts(w.pack(pad=0), sf_index=sf_index)
54
55
56def silent(sf_index):
57 w = A.BitW()
58 w.w(0, 3).w(0, 4).w(128, 8)
59 w.w(0, 1).w(0, 2).w(0, 1).w(0, 6).w(0, 1)
60 w.w(0, 3).w(7, 3)
61 return A.adts(w.pack(4), sf_index=sf_index)
62
63
64def blocks(frames, sf_index):
65 src = f"swb-{os.getpid()}.aac"
66 sil = silent(sf_index)
67 with open(src, "wb") as f:
68 f.write(sil + b"".join(fr + sil + sil for fr in frames))
69 raw = subprocess.run(
70 ["ffmpeg", "-v", "quiet", "-i", src, "-f", "f32le", "-ac", "1", "-"],
71 capture_output=True,
72 )
73 pcm = np.frombuffer(raw.stdout, dtype="<f4").astype(np.float64)
74 os.remove(src)
75 nb = len(pcm) // 1024
76 if nb != 3 * len(frames) + 1:
77 if len(frames) == 1:
78 return [None]
79 h = len(frames) // 2
80 return blocks(frames[:h], sf_index) + blocks(frames[h:], sf_index)
81 pcm = pcm[: nb * 1024].reshape(nb, 1024)
82 return [np.concatenate([pcm[3 * i + 1], pcm[3 * i + 2]]) for i in range(len(frames))]
83
84
85def long_offsets(sf_index):
86 out = []
87 got = blocks([frame_long(k, sf_index) for k in range(1, 52)], sf_index)
88 for blk in got:
89 if blk is None:
90 break
91 spec = (BASIS.T @ (blk / L.WIN)) / 1024
92 nz = np.where(np.abs(spec) > 1e-3)[0]
93 if len(nz) == 0:
94 break
95 out.append(int(nz.max()) + 1)
96 return out
97
98
99def short_offsets(sf_index):
100 out = []
101 got = blocks([frame_short(k, sf_index) for k in range(1, 17)], sf_index)
102 for blk in got:
103 if blk is None:
104 break
105 mag = np.abs(np.fft.rfft(blk * np.hanning(2048)))
106 top = np.where(mag > mag.max() * 3e-4)[0].max()
107 out.append(top / 8.0)
108 return out
109
110
111if __name__ == "__main__":
112 out = {}
113 for idx in range(0, 12):
114 lo = long_offsets(idx)
115 sh = short_offsets(idx)
116 out[idx] = {"long": lo, "short": sh}
117 print(idx, "long", len(lo), lo, file=sys.stderr)
118 print(idx, "short", [round(v, 1) for v in sh], file=sys.stderr)
119 json.dump(out, open("swb.json", "w"))