Header (FLI 1/70 s, FLC ms, depths 8 / 15 / 16 / 24), frame chunks into containers/Demux, open() through the registry's flic decoder (codecs/video/Legacy/Flic); the ring frame is dropped unless opts.ringFrame. Fixtures encoded by tools/fixtures/fli.py (every chunk type) with ffmpeg's decode as the reference: bit-exact.
396 lines
14 KiB
Python
396 lines
14 KiB
Python
# FLI / FLC fixtures (tools/fixtures.py): ffmpeg cannot write FLICs, so this encodes them (every chunk type: COLOR_64,
|
|
# COLOR_256, BRUN, LC / DELTA_FLI, SS2 / DELTA_FLC with line skips and the last-byte opcode, BLACK, COPY, PSTAMP; a
|
|
# prefix chunk and a ring frame; 15-bit FLC, 16-bit DTA and 24-bit FLC with BRUN / DTA_BRUN / DELTA / DTA_LC / COPY /
|
|
# DTA_COPY) and uses ffmpeg's decode as the reference (CRC-32 of each frame as rgba).
|
|
# tests/gen/fli_cases.luau { { name, data, width, height, depth, rate, frames = { crc } } }
|
|
import importlib.util, os, random, struct, tempfile, zlib
|
|
|
|
_spec = importlib.util.spec_from_file_location("_video_helpers", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_video.py"))
|
|
V = importlib.util.module_from_spec(_spec)
|
|
_spec.loader.exec_module(V)
|
|
|
|
INPUTS = ["tools/fixtures/_video.py"]
|
|
|
|
|
|
def le16(v):
|
|
return struct.pack("<H", v & 0xFFFF)
|
|
|
|
|
|
def le32(v):
|
|
return struct.pack("<I", v & 0xFFFFFFFF)
|
|
|
|
|
|
def s8(v):
|
|
return v & 0xFF
|
|
|
|
|
|
def chunk(typ, body):
|
|
if len(body) & 1:
|
|
body += b"\0"
|
|
return le32(6 + len(body)) + le16(typ) + body
|
|
|
|
|
|
def frame(chunks, magic=0xF1FA):
|
|
body = b"".join(chunks)
|
|
return le32(16 + len(body)) + le16(magic) + le16(len(chunks)) + bytes(8) + body
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------- chunks
|
|
def color(entries, six):
|
|
"""COLOR_64 / COLOR_256: entries = [(skip, [(r, g, b), ...])]"""
|
|
out = le16(len(entries))
|
|
for skip, cols in entries:
|
|
out += bytes([skip, len(cols) & 0xFF])
|
|
for c in cols:
|
|
out += bytes(c)
|
|
return chunk(11 if six else 4, out)
|
|
|
|
|
|
def runs(seq, maxrun=127):
|
|
"""split a sequence into ('rep', value, n) and ('lit', [values]) pieces"""
|
|
out, i = [], 0
|
|
while i < len(seq):
|
|
j = i
|
|
while j + 1 < len(seq) and seq[j + 1] == seq[i] and j - i + 1 < maxrun:
|
|
j += 1
|
|
if j - i >= 2:
|
|
out.append(("rep", seq[i], j - i + 1))
|
|
i = j + 1
|
|
else:
|
|
k = i
|
|
while k < len(seq) and k - i < maxrun and not (k + 2 < len(seq) and seq[k] == seq[k + 1] == seq[k + 2]):
|
|
k += 1
|
|
k = max(k, i + 1)
|
|
out.append(("lit", seq[i:k]))
|
|
i = k
|
|
return out
|
|
|
|
|
|
def brun_line(units, unit_bytes):
|
|
"""BRUN line body: > 0 repeat, < 0 literal (units are byte strings of unit_bytes each)"""
|
|
out = bytes([0])
|
|
for p in runs(units):
|
|
if p[0] == "rep":
|
|
out += bytes([p[2]]) + p[1]
|
|
else:
|
|
out += bytes([s8(-len(p[1]))]) + b"".join(p[1])
|
|
return out
|
|
|
|
|
|
def brun(img, bpp, pixel_runs, typ=15):
|
|
lines = b""
|
|
for row in img:
|
|
if pixel_runs:
|
|
units = [row[x] for x in range(len(row))]
|
|
else:
|
|
flat = b"".join(row)
|
|
units = [flat[i:i + 1] for i in range(len(flat))]
|
|
lines += brun_line(units, bpp)
|
|
return chunk(typ, lines)
|
|
|
|
|
|
def lc(img, prev):
|
|
"""DELTA_FLI (LC): first changed line, line count, per line (skip, run) packets on bytes"""
|
|
h, w = len(img), len(img[0])
|
|
ch = [y for y in range(h) if img[y] != prev[y]]
|
|
if not ch:
|
|
return chunk(12, le16(0) + le16(0))
|
|
y0, y1 = ch[0], ch[-1] + 1
|
|
out = le16(y0) + le16(y1 - y0)
|
|
for y in range(y0, y1):
|
|
row, prow = img[y], prev[y]
|
|
packets = []
|
|
x = 0
|
|
while x < w:
|
|
skip = 0
|
|
while x < w and row[x] == prow[x] and skip < 255:
|
|
skip += 1
|
|
x += 1
|
|
if x >= w:
|
|
break
|
|
e = x
|
|
while e < w and row[e] != prow[e] and e - x < 127:
|
|
e += 1
|
|
seg = [bytes([v]) for v in row[x:e]]
|
|
pieces = runs(seg)
|
|
first = True
|
|
for p in pieces:
|
|
sk = skip if first else 0
|
|
first = False
|
|
if p[0] == "rep":
|
|
packets.append(bytes([sk, s8(-p[2])]) + p[1])
|
|
else:
|
|
packets.append(bytes([sk, len(p[1])]) + b"".join(p[1]))
|
|
x = e
|
|
out += bytes([len(packets)]) + b"".join(packets)
|
|
return chunk(12, out)
|
|
|
|
|
|
def ss2(img, prev, last_byte=False):
|
|
"""DELTA_FLC (SS2) on 8-bit pixels: word packets, line-skip words, the last-byte opcode on odd lines"""
|
|
h, w = len(img), len(img[0])
|
|
out = b""
|
|
count = 0
|
|
skip_lines = 0
|
|
for y in range(h):
|
|
row, prow = img[y], prev[y]
|
|
if row == prow:
|
|
skip_lines += 1
|
|
continue
|
|
if skip_lines:
|
|
out += le16(-skip_lines)
|
|
skip_lines = 0
|
|
body = b""
|
|
packets = 0
|
|
lastop = last_byte and y % 2 == 1 and row[w - 1] != prow[w - 1]
|
|
ww = w - (1 if lastop else 0)
|
|
words = [bytes(row[x:x + 2]) for x in range(0, ww - ww % 2, 2)]
|
|
pwords = [bytes(prow[x:x + 2]) for x in range(0, ww - ww % 2, 2)]
|
|
i = 0
|
|
while i < len(words):
|
|
skip = 0
|
|
while i < len(words) and words[i] == pwords[i] and skip < 127:
|
|
skip += 1
|
|
i += 1
|
|
if i >= len(words):
|
|
break
|
|
e = i
|
|
while e < len(words) and words[e] != pwords[e] and e - i < 127:
|
|
e += 1
|
|
first = True
|
|
for p in runs(words[i:e]):
|
|
sk = skip * 2 if first else 0
|
|
first = False
|
|
if p[0] == "rep":
|
|
body += bytes([sk, s8(-p[2])]) + p[1]
|
|
else:
|
|
body += bytes([sk, len(p[1])]) + b"".join(p[1])
|
|
packets += 1
|
|
i = e
|
|
if lastop:
|
|
out += le16(0x8000 | row[w - 1])
|
|
out += le16(packets) + body
|
|
count += 1
|
|
return chunk(7, le16(count) + out)
|
|
|
|
|
|
def delta_deep(img, prev, bpp, typ=7):
|
|
"""DELTA / DTA_LC on 15/16/24-bit pixels: negative words skip lines, packets (skip pixels, run)"""
|
|
h, w = len(img), len(img[0])
|
|
out, count, skip_lines = b"", 0, 0
|
|
for y in range(h):
|
|
row, prow = img[y], prev[y]
|
|
if row == prow:
|
|
skip_lines += 1
|
|
continue
|
|
if skip_lines:
|
|
out += le16(-skip_lines)
|
|
skip_lines = 0
|
|
body, packets, x = b"", 0, 0
|
|
while x < w:
|
|
skip = 0
|
|
while x < w and row[x] == prow[x] and skip < 255:
|
|
skip += 1
|
|
x += 1
|
|
if x >= w:
|
|
break
|
|
e = x
|
|
while e < w and row[e] != prow[e] and e - x < 127:
|
|
e += 1
|
|
first = True
|
|
for p in runs(row[x:e]):
|
|
sk = skip if first else 0
|
|
first = False
|
|
if p[0] == "rep":
|
|
body += bytes([sk, s8(-p[2])]) + p[1]
|
|
else:
|
|
body += bytes([sk, len(p[1])]) + b"".join(p[1])
|
|
packets += 1
|
|
x = e
|
|
out += le16(packets) + body
|
|
count += 1
|
|
return chunk(typ, le16(count) + out)
|
|
|
|
|
|
def copy8(img):
|
|
w = len(img[0])
|
|
pad = (w + 3) // 4 * 4 - w
|
|
return le32(6 + len(img) * (w + pad)) + le16(16) + b"".join(bytes(r) + bytes(pad) for r in img)
|
|
|
|
|
|
def copy_deep(img, bpp, typ=16):
|
|
w = len(img[0])
|
|
body = b"".join(b"".join(r) + (bytes(bpp) if w % 2 else b"") for r in img)
|
|
return le32(6 + len(body)) + le16(typ) + body
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------- pictures
|
|
def idx_frames(w, h, n, colors, seed):
|
|
rnd = random.Random(seed)
|
|
out = []
|
|
for t in range(n):
|
|
img = [[(x // 6 + y // 4 + (t // 4)) % colors for x in range(w)] for y in range(h)]
|
|
sx, sy = (t * 7) % (w - 16), (t * 3) % (h - 12)
|
|
for y in range(sy, sy + 12):
|
|
for x in range(sx, sx + 16):
|
|
img[y][x] = (200 + t) % colors
|
|
for y in range(h // 2, h // 2 + 4):
|
|
for x in range(w // 2, w // 2 + 10):
|
|
img[y][x] = rnd.randrange(colors)
|
|
out.append(img)
|
|
return out
|
|
|
|
|
|
def deep_frames(w, h, n, bpp, seed):
|
|
rnd = random.Random(seed)
|
|
out = []
|
|
for t in range(n):
|
|
img = []
|
|
for y in range(h):
|
|
row = []
|
|
for x in range(w):
|
|
v = ((x * 37 + y * 11 + t * 3) // 8) & 0x7FFF if bpp == 2 else ((x * 4) & 255, (y * 5) & 255, (t * 20) & 255)
|
|
if 10 + t <= x < 26 + t and 8 <= y < 20:
|
|
v = 0x7C00 if bpp == 2 else (250, 10, 10)
|
|
if bpp == 2 and h // 2 <= y < h // 2 + 3 and w // 2 <= x < w // 2 + 7:
|
|
v = rnd.randrange(65536)
|
|
row.append(struct.pack("<H", v) if bpp == 2 else bytes([v[2], v[1], v[0]]))
|
|
img.append(row)
|
|
out.append(img)
|
|
return out
|
|
|
|
|
|
def header(typ, frames, w, h, depth, speed, oframes=(0, 0)):
|
|
hd = bytearray(128)
|
|
hd[4:6] = le16(typ)
|
|
hd[6:8] = le16(frames)
|
|
hd[8:10] = le16(w)
|
|
hd[10:12] = le16(h)
|
|
hd[12:14] = le16(depth)
|
|
hd[14:16] = le16(3)
|
|
if typ == 0xAF11:
|
|
hd[16:18] = le16(speed)
|
|
else:
|
|
hd[16:20] = le32(speed)
|
|
hd[80:84] = le32(oframes[0])
|
|
hd[84:88] = le32(oframes[1])
|
|
return hd
|
|
|
|
|
|
def file(typ, w, h, depth, speed, frames, prefix=b"", nframes=None):
|
|
hd = header(typ, nframes if nframes is not None else len(frames), w, h, depth, speed)
|
|
body = prefix + b"".join(frames)
|
|
hd[0:4] = le32(128 + len(body))
|
|
if typ != 0xAF11 and frames:
|
|
hd[80:84] = le32(128 + len(prefix))
|
|
hd[84:88] = le32(128 + len(prefix) + len(frames[0]))
|
|
return bytes(hd) + body
|
|
|
|
|
|
def pal(n, seed, six):
|
|
rnd = random.Random(seed)
|
|
m = 64 if six else 256
|
|
return [(rnd.randrange(m), rnd.randrange(m), rnd.randrange(m)) for _ in range(n)]
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------- files
|
|
def fli_8():
|
|
"""FLI (AF11), 70 Hz jiffies: COLOR_64 + BRUN, LC deltas, a BLACK frame, a COPY frame, a palette change, PSTAMP"""
|
|
w, h, n = 80, 50, 10
|
|
imgs = idx_frames(w, h, n, 256, 1)
|
|
frames = [frame([color([(0, pal(256, 1, True))], True), brun([[bytes([v]) for v in r] for r in imgs[0]], 1, True)])]
|
|
for t in range(1, n):
|
|
if t == 4:
|
|
imgs[t] = [[0] * w for _ in range(h)]
|
|
frames.append(frame([chunk(13, b"")]))
|
|
elif t == 5:
|
|
frames.append(frame([copy8(imgs[t]), chunk(18, le16(10) + le16(6) + bytes(20))]))
|
|
elif t == 7:
|
|
frames.append(frame([color([(10, pal(20, 9, True)), (30, pal(5, 10, True))], True), lc(imgs[t], imgs[t - 1])]))
|
|
else:
|
|
frames.append(frame([lc(imgs[t], imgs[t - 1])]))
|
|
frames.append(frame([lc(imgs[0], imgs[-1])])) # the ring frame
|
|
return file(0xAF11, w, h, 8, 5, frames, nframes=n)
|
|
|
|
|
|
def flc_8():
|
|
"""FLC (AF12), ms: a prefix chunk, COLOR_256 + BRUN, SS2 deltas (line skips, last-byte opcode), COPY, BLACK, an
|
|
empty frame, COLOR_256 updates"""
|
|
w, h, n = 128, 64, 12
|
|
imgs = idx_frames(w, h, n, 256, 2)
|
|
frames = [frame([color([(0, pal(256, 2, False))], False), brun([[bytes([v]) for v in r] for r in imgs[0]], 1, True)])]
|
|
for t in range(1, n):
|
|
if t == 3:
|
|
imgs[t] = imgs[t - 1]
|
|
frames.append(frame([]))
|
|
elif t == 6:
|
|
frames.append(frame([copy8(imgs[t])]))
|
|
elif t == 8:
|
|
imgs[t] = [[0] * w for _ in range(h)]
|
|
frames.append(frame([chunk(13, b""), color([(0, pal(16, 3, False)), (100, pal(50, 4, False))], False)]))
|
|
else:
|
|
frames.append(frame([ss2(imgs[t], imgs[t - 1], last_byte=True)]))
|
|
prefix = le32(16) + le16(0xF100) + le16(0) + bytes(8)
|
|
return file(0xAF12, w, h, 8, 66, frames, prefix=prefix)
|
|
|
|
|
|
def flc_8_odd():
|
|
"""FLC 8-bit at an odd size: BRUN, LC, SS2 without the last-byte opcode, COPY with padded rows"""
|
|
w, h, n = 61, 37, 6
|
|
imgs = idx_frames(w, h, n, 256, 3)
|
|
frames = [frame([color([(0, pal(256, 5, False))], False), brun([[bytes([v]) for v in r] for r in imgs[0]], 1, True)])]
|
|
for t in range(1, n):
|
|
if t == 2:
|
|
frames.append(frame([lc(imgs[t], imgs[t - 1])]))
|
|
elif t == 4:
|
|
frames.append(frame([copy8(imgs[t])]))
|
|
else:
|
|
frames.append(frame([ss2(imgs[t], imgs[t - 1])]))
|
|
return file(0xAF12, w, h, 8, 100, frames)
|
|
|
|
|
|
def flc_deep(typ, depth, bpp, w, h):
|
|
"""15 / 16 / 24-bit: BRUN (bytes) or DTA_BRUN (pixels), DELTA / DTA_LC, BLACK, COPY / DTA_COPY"""
|
|
n = 7
|
|
imgs = deep_frames(w, h, n, bpp, depth)
|
|
frames = [frame([brun(imgs[0], bpp, False)])]
|
|
for t in range(1, n):
|
|
if t == 2:
|
|
frames.append(frame([copy_deep(imgs[t], bpp, 16)]))
|
|
elif t == 3:
|
|
frames.append(frame([brun(imgs[t], bpp, True, 25)]))
|
|
elif t == 4:
|
|
imgs[t] = [[bytes(bpp)] * w for _ in range(h)]
|
|
frames.append(frame([chunk(13, b"")]))
|
|
elif t == 5:
|
|
frames.append(frame([copy_deep(imgs[t], bpp, 26)]))
|
|
else:
|
|
frames.append(frame([delta_deep(imgs[t], imgs[t - 1], bpp, 7 if t % 2 else 27)]))
|
|
return file(typ, w, h, depth, 50, frames)
|
|
|
|
|
|
def rgba_crcs(path, w, h):
|
|
raw = V.ffmpeg(["-i", path, "-map", "0:v:0", "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "rgba", "-"])
|
|
n = w * h * 4
|
|
return [zlib.crc32(raw[i:i + n]) & 0xffffffff for i in range(0, len(raw) - n + 1, n)]
|
|
|
|
|
|
def generate(ctx):
|
|
tmp = tempfile.mkdtemp(prefix="flifx_")
|
|
files = [
|
|
("anim.fli", fli_8(), 80, 50, 8, 70 / 5),
|
|
("anim.flc", flc_8(), 128, 64, 8, 1000 / 66),
|
|
("odd.flc", flc_8_odd(), 61, 37, 8, 10),
|
|
("rgb15.flc", flc_deep(0xAF12, 16, 2, 63, 40), 63, 40, 15, 20),
|
|
("rgb565.flc", flc_deep(0xAF44, 16, 2, 48, 30), 48, 30, 16, 20),
|
|
("bgr24.flc", flc_deep(0xAF12, 24, 3, 41, 30), 41, 30, 24, 20),
|
|
]
|
|
cases = []
|
|
for name, data, w, h, depth, rate in files:
|
|
path = os.path.join(tmp, name)
|
|
open(path, "wb").write(data)
|
|
crcs = rgba_crcs(path, w, h)
|
|
assert crcs, name
|
|
cases.append({"name": name, "data": data, "width": w, "height": h, "depth": depth, "rate": rate, "frames": crcs})
|
|
ctx.emit("fli_cases", cases)
|