absorb: containers/Ogg (demuxer from ogg-luau be9f604, Err values, Reader input) + a new muxer (page writer); tests vs opusenc / flac / ffmpeg fixtures
This commit is contained in:
1 parent
cdcb5b0b77
commit
c22b50e80b
6 files changed
+1322
No files matched your search
@@ -0,0 +1,209 @@
|
||||
# Ogg fixtures (tools/fixtures.py): containers/Ogg. Ported from ogg-luau tools/gen_fixtures.py.
|
||||
# tests/gen/ogg_cases.luau real encoder files (opusenc, flac --ogg, ffmpeg vorbis) and hand-made multiplexed files
|
||||
# (lacing edge cases, junk, a corrupt page copy, a lost page), each with the packet list
|
||||
# from the small independent parser below: { size, crc32, granule, bos, eos }
|
||||
import math, os, random, struct, tempfile, wave, zlib
|
||||
|
||||
# --- CRC + writer -----------------------------------------------------------------------------------------
|
||||
def _crc_table():
|
||||
t = []
|
||||
for i in range(256):
|
||||
r = i << 24
|
||||
for _ in range(8):
|
||||
r = ((r << 1) ^ 0x04C11DB7) if r & 0x80000000 else (r << 1)
|
||||
r &= 0xFFFFFFFF
|
||||
t.append(r)
|
||||
return t
|
||||
|
||||
|
||||
CRC = _crc_table()
|
||||
|
||||
|
||||
def ogg_crc(data):
|
||||
c = 0
|
||||
for x in data:
|
||||
c = ((c << 8) & 0xFFFFFFFF) ^ CRC[(c >> 24) ^ x]
|
||||
return c
|
||||
|
||||
|
||||
def page(serial, seq, granule, flags, segs, data):
|
||||
h = struct.pack("<4sBBqIIIB", b"OggS", 0, flags, granule, serial, seq, 0, len(segs)) + bytes(segs)
|
||||
p = bytearray(h + data)
|
||||
struct.pack_into("<I", p, 22, ogg_crc(p))
|
||||
return bytes(p)
|
||||
|
||||
|
||||
def mux_stream(serial, packets, max_segs=255, granule_of=None):
|
||||
"""Pages for one stream; packets = list of (bytes, granule). Pages break after max_segs lacing values."""
|
||||
pages = []
|
||||
segs, data, seq, cont, gran = [], b"", 0, False, -1
|
||||
first = True
|
||||
|
||||
def flush(eos=False, next_cont=False):
|
||||
nonlocal segs, data, seq, cont, gran, first
|
||||
flags = (1 if cont else 0) | (2 if first else 0) | (4 if eos else 0)
|
||||
pages.append(page(serial, seq, gran, flags, segs, data))
|
||||
seq += 1
|
||||
first = False
|
||||
segs, data, gran = [], b"", -1
|
||||
cont = next_cont
|
||||
|
||||
for i, (pkt, g) in enumerate(packets):
|
||||
n = len(pkt)
|
||||
lac = [255] * (n // 255) + [n % 255]
|
||||
pos = 0
|
||||
for j, l in enumerate(lac):
|
||||
if len(segs) == max_segs:
|
||||
flush(next_cont=j > 0)
|
||||
segs.append(l)
|
||||
data += pkt[pos:pos + l]
|
||||
pos += l
|
||||
gran = g
|
||||
if i == 0 or (i % 7 == 3): # bos page holds only the first packet; break pages now and then
|
||||
flush()
|
||||
flush(eos=True)
|
||||
return pages
|
||||
|
||||
|
||||
# --- reference parser ---------------------------------------------------------------------------------------
|
||||
def parse(data):
|
||||
"""{serial: [ (bytes, granule, bos, eos) ]} with resync + CRC check, like the library."""
|
||||
out, partial, order, seqs = {}, {}, [], {}
|
||||
pos = 0
|
||||
while True:
|
||||
i = data.find(b"OggS", pos)
|
||||
if i < 0 or i + 27 > len(data):
|
||||
break
|
||||
nseg = data[i + 26]
|
||||
segs = data[i + 27:i + 27 + nseg]
|
||||
total = 27 + nseg + sum(segs)
|
||||
if len(segs) < nseg or i + total > len(data):
|
||||
pos = i + 1
|
||||
continue
|
||||
p = bytearray(data[i:i + total])
|
||||
crc = struct.unpack_from("<I", p, 22)[0]
|
||||
p[22:26] = b"\0\0\0\0"
|
||||
if ogg_crc(p) != crc:
|
||||
pos = i + 1
|
||||
continue
|
||||
flags, gran, serial = p[5], struct.unpack_from("<q", p, 6)[0], struct.unpack_from("<I", p, 14)[0]
|
||||
if serial not in out:
|
||||
out[serial] = []
|
||||
order.append(serial)
|
||||
partial[serial] = None
|
||||
seq = struct.unpack_from("<I", p, 18)[0]
|
||||
if serial in seqs and seq != seqs[serial] + 1:
|
||||
partial[serial] = None
|
||||
seqs[serial] = seq
|
||||
body = 27 + nseg
|
||||
last_complete = max([k for k, l in enumerate(segs) if l < 255], default=-1)
|
||||
cur = partial[serial] if (flags & 1) else None
|
||||
if not (flags & 1):
|
||||
cur = None
|
||||
skip = (flags & 1) and partial[serial] is None
|
||||
first_on_page = True
|
||||
for k, l in enumerate(segs):
|
||||
chunk = bytes(p[body:body + l])
|
||||
body += l
|
||||
if skip:
|
||||
if l < 255:
|
||||
skip = False
|
||||
first_on_page = False
|
||||
continue
|
||||
cur = (cur or b"") + chunk
|
||||
if l < 255:
|
||||
last = k == last_complete
|
||||
out[serial].append((cur, gran if last else -1, bool(flags & 2) and first_on_page,
|
||||
bool(flags & 4) and last))
|
||||
first_on_page = False
|
||||
cur = None
|
||||
partial[serial] = cur
|
||||
pos = i + total
|
||||
return order, out
|
||||
|
||||
|
||||
def codec(pkt):
|
||||
if pkt.startswith(b"OpusHead"):
|
||||
return "opus"
|
||||
if pkt[:5] == b"\x7fFLAC":
|
||||
return "flac"
|
||||
if pkt[:7] == b"\x01vorbis":
|
||||
return "vorbis"
|
||||
return "unknown"
|
||||
|
||||
|
||||
|
||||
def case(name, data, note):
|
||||
order, streams = parse(data)
|
||||
ss = []
|
||||
for s in order:
|
||||
pk = streams[s]
|
||||
gs = [g for _, g, _, _ in pk if g != -1]
|
||||
ss.append({"serial": s, "codec": codec(pk[0][0]), "lastGranule": gs[-1] if gs else -1,
|
||||
"packets": [[len(b), zlib.crc32(b), g, bos, eos] for b, g, bos, eos in pk]})
|
||||
return {"name": name, "note": note, "data": data, "streams": ss}
|
||||
|
||||
|
||||
def tone_wav(path, secs, rate=48000, ch=2):
|
||||
rnd = random.Random(1)
|
||||
with wave.open(path, "wb") as w:
|
||||
w.setnchannels(ch)
|
||||
w.setsampwidth(2)
|
||||
w.setframerate(rate)
|
||||
fr = bytearray()
|
||||
for i in range(int(secs * rate)):
|
||||
t = i / rate
|
||||
for c in range(ch):
|
||||
v = 0.3 * math.sin(2 * math.pi * (220 * (c + 1) + 40 * math.sin(t)) * t) + 0.05 * (rnd.random() - 0.5)
|
||||
fr += struct.pack("<h", int(v * 32767))
|
||||
w.writeframes(bytes(fr))
|
||||
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
tmp = tempfile.mkdtemp()
|
||||
cases = []
|
||||
wav = os.path.join(tmp, "a.wav")
|
||||
tone_wav(wav, 12)
|
||||
opus = os.path.join(tmp, "a.opus")
|
||||
ctx.nix("opus-tools", ["opusenc", "--quiet", "--bitrate", "48", wav, opus])
|
||||
cases.append(case("opus_stereo", open(opus, "rb").read(), "opusenc 48 kb/s, 12 s stereo"))
|
||||
wav2 = os.path.join(tmp, "b.wav")
|
||||
tone_wav(wav2, 1.5, 44100, 1)
|
||||
oflac = os.path.join(tmp, "b.oga")
|
||||
ctx.nix("flac", ["flac", "-s", "--ogg", "-5", "-o", oflac, wav2])
|
||||
cases.append(case("flac_ogg", open(oflac, "rb").read(), "flac --ogg -5, 1.5 s mono"))
|
||||
vorb = os.path.join(tmp, "c.ogg")
|
||||
ctx.nix("ffmpeg", ["ffmpeg", "-v", "error", "-y", "-i", wav2, "-ac", "2", "-c:a", "vorbis", "-strict", "-2", vorb])
|
||||
cases.append(case("vorbis_ffmpeg", open(vorb, "rb").read(), "ffmpeg native vorbis, 1.5 s stereo"))
|
||||
|
||||
# two interleaved streams, lacing edge cases, junk, a corrupt page, a dropped page
|
||||
rnd = random.Random(7)
|
||||
sizes = [0, 1, 254, 255, 256, 509, 510, 511, 765, 4000, 70000, 0, 0, 3, 65025, 255 * 255 + 1]
|
||||
a = [(b"OpusHead" + bytes(11), 0)] + [(bytes(rnd.randrange(256) for _ in range(n)), 100 * (i + 1))
|
||||
for i, n in enumerate(sizes)]
|
||||
b = [(b"\x7fFLAC" + bytes(40), 0)] + [(bytes(rnd.randrange(256) for _ in range(rnd.randrange(1, 900))), 50 * (i + 1))
|
||||
for i in range(60)]
|
||||
pa = mux_stream(0x1234ABCD, a, max_segs=255)
|
||||
pb = mux_stream(7, b, max_segs=17)
|
||||
out = pa[:1] + pb[:1] # bos pages first
|
||||
ra, rb = pa[1:], pb[1:]
|
||||
while ra or rb:
|
||||
if rb and (not ra or rnd.random() < 0.5):
|
||||
out.append(rb.pop(0))
|
||||
else:
|
||||
out.append(ra.pop(0))
|
||||
blob = bytearray()
|
||||
for k, pg in enumerate(out):
|
||||
if k == 5:
|
||||
blob += b"junkOggSjunk" + bytes(40)
|
||||
if k == 9: # a corrupt copy before the real page
|
||||
bad = bytearray(pg)
|
||||
bad[len(bad) // 2] ^= 0xFF
|
||||
blob += bad
|
||||
blob += pg
|
||||
blob += b"OggS\0trailing-truncated"
|
||||
cases.append(case("mux_edge", bytes(blob), "2 streams, lacing edges, junk, a corrupt page copy, truncated tail"))
|
||||
lost = [pg for pg in out if not (struct.unpack_from("<I", pg, 14)[0] == 7 and struct.unpack_from("<I", pg, 18)[0] == 5)]
|
||||
cases.append(case("mux_lost", b"".join(lost), "mux_edge without page 5 of serial 7"))
|
||||
ctx.emit("ogg_cases", cases)
|
||||
Reference in new issue
Block a user