video/fli: FLI / FLC / FLX animations (read)
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. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
2d014f1a52
commit
50e270086d
5 files changed
+670
No files matched your search
@@ -28,6 +28,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| audio | vorbis | .ogg .oga | yes | yes | streaming: true, seek: true, lossy: true, channels: 255 | `tests/audio/vorbis.luau` |
|
||||
| audio | wav | .wav .wave .rf64 .bw64 | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, rf64: true | `tests/audio/wav.luau` |
|
||||
| video | avi | .avi | yes | yes | streaming: true, video: "mjpeg", "mpeg1", "rawvideo", audio: true, odml: true | `tests/video/avi.luau` |
|
||||
| video | fli | .fli .flc .flx | yes | - | streaming: true, video: "flic", depths: 8, 15, 16, 24 | `tests/video/fli.luau` |
|
||||
| video | h264 | .h264 .264 .avc .jsv .26l .jvt | yes | yes | streaming: true, video: "h264", depths: 8 | `tests/video/h264.luau` |
|
||||
| video | mkv | .mkv .webm .mka .mk3d | yes | yes | streaming: true, remux: true, video: "vp8", "mjpeg", "mpeg1", "rawvideo", audio: true, subtitles: true | `tests/video/mkv.luau` |
|
||||
| video | mp4 | .mp4 .m4v .mov .qt .3gp .3g2 .mj2 | yes | yes | streaming: true, remux: true, video: "mjpeg", "mpeg1", "h264", audio: true, fragments: true | `tests/video/mp4.luau` |
|
||||
|
||||
@@ -290,6 +290,18 @@ local list: { Entry } = {
|
||||
return require("@formats/video/avi")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "fli",
|
||||
category = "video",
|
||||
path = "video/fli",
|
||||
extensions = { "fli", "flc", "flx" },
|
||||
mime = { "video/x-fli", "video/fli", "video/x-flc" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/video/Video" },
|
||||
caps = { decode = true, encode = false, streaming = true, video = { "flic" }, depths = { 8, 15, 16, 24 } },
|
||||
load = function()
|
||||
return require("@formats/video/fli")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "h264",
|
||||
category = "video",
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
--!strict
|
||||
-- video/fli: Autodesk Animator FLI / Animator Pro FLC / FLX animations. New in formats-luau, written from the FLIC
|
||||
-- file format description (Jim Kent, "FLC - Animator Pro Flic Files"; MultimediaWiki "FLIC").
|
||||
-- Read: the 128-byte header (FLI 0xAF11: speed in 1/70 s; FLC 0xAF12 / DTA 0xAF44: speed in ms; depth 8, 15, 16, 24:
|
||||
-- an FLC that says 16 bits holds 5-5-5 pixels, as Animator Pro wrote them), prefix and other non-frame chunks skipped,
|
||||
-- every frame chunk (0xF1FA, and 0xF5FA) a packet of a Demuxer; open() returns a VideoDecoder whose frames are rgba8
|
||||
-- (codecs/video/Legacy: palette chunks, BRUN, LC, SS2, BLACK, COPY, the deep-colour DTA chunks). The ring frame (the
|
||||
-- extra frame after `frames` that leads back to the first) is not shown. Fli.demux(r) gives the Demuxer.
|
||||
-- Not supported: writing, 1-bit FLICs, the X-COM / Magic Carpet game variants (audio chunks, 6-bit COLOR_256).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Demux = require("@formats/containers/Demux")
|
||||
local Video = require("@formats/codecs/video/Video")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Fli = {
|
||||
name = "fli",
|
||||
category = "video",
|
||||
extensions = { "fli", "flc", "flx" },
|
||||
mime = { "video/x-fli", "video/fli", "video/x-flc" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/video/Video" },
|
||||
caps = { decode = true, encode = false, streaming = true, video = { "flic" }, depths = { 8, 15, 16, 24 } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type OpenOptions = {
|
||||
rgba: boolean?, -- (frames are rgba8 already)
|
||||
maxPixels: number?, -- default 2^26
|
||||
ringFrame: boolean?, -- true: also show the ring frame at the end (as ffmpeg does)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local HEADER = 128
|
||||
local FLI, FLC, DTA = 0xAF11, 0xAF12, 0xAF44
|
||||
local FRAME, FRAME2 = 0xF1FA, 0xF5FA
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Fli.sniff(r: Bytes.Reader): number
|
||||
local s = r:peek(18)
|
||||
if #s < 18 then
|
||||
return 0
|
||||
end
|
||||
local magic = string.byte(s, 5) + string.byte(s, 6) * 256
|
||||
if magic ~= FLI and magic ~= FLC and magic ~= DTA then
|
||||
return 0
|
||||
end
|
||||
local w = string.byte(s, 9) + string.byte(s, 10) * 256
|
||||
local h = string.byte(s, 11) + string.byte(s, 12) * 256
|
||||
if w > 4096 or h > 4096 then
|
||||
return 0
|
||||
end
|
||||
return 0.9
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- demux(r, opts?) -> the Demuxer | nil, Err
|
||||
function Fli.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
||||
local b, base, limit = r.buf, r.base, r.limit
|
||||
local n = limit - base
|
||||
if n < HEADER then
|
||||
return nil, Err.new(Err.TRUNCATED, "file shorter than the 128-byte header")
|
||||
end
|
||||
local hr = Bytes.reader(b, base, HEADER)
|
||||
hr:skip(4)
|
||||
local magic = hr:u16le()
|
||||
local frames = hr:u16le()
|
||||
local w, h = hr:u16le(), hr:u16le()
|
||||
local depth = hr:u16le()
|
||||
hr:skip(2)
|
||||
local speed = hr:u32le()
|
||||
if hr.err then
|
||||
return nil, hr.err
|
||||
end
|
||||
if magic ~= FLI and magic ~= FLC and magic ~= DTA then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("bad magic 0x%04x", magic), 4)
|
||||
end
|
||||
if magic == FLI then
|
||||
speed = speed % 65536
|
||||
end
|
||||
if w == 0 or h == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "no picture size")
|
||||
end
|
||||
local max = (opts and opts.maxPixels) or 2 ^ 26
|
||||
if w * h > max then
|
||||
return nil, Err.new(Err.LIMIT, w .. " x " .. h .. " over maxPixels")
|
||||
end
|
||||
if magic == FLI or depth == 0 then
|
||||
depth = 8
|
||||
elseif magic == FLC and depth == 16 then
|
||||
depth = 15 -- Animator Pro's 16-bit FLCs hold 5-5-5 pixels
|
||||
end
|
||||
if speed == 0 then
|
||||
speed = 5
|
||||
end
|
||||
local scale = if magic == FLI then 70 else 1000
|
||||
local track: Types.Track = {
|
||||
id = 1,
|
||||
kind = "video",
|
||||
codec = "flic",
|
||||
codecTag = string.format("0x%04X", magic),
|
||||
timescale = scale,
|
||||
width = w,
|
||||
height = h,
|
||||
frameRate = scale / speed,
|
||||
default = true,
|
||||
bitsPerPixel = depth,
|
||||
}
|
||||
-- the frame chunks
|
||||
local offs, sizes = {}, {}
|
||||
local p = base + HEADER
|
||||
while p + 6 <= limit do
|
||||
local size = buffer.readu32(b, p)
|
||||
local cm = buffer.readu16(b, p + 4)
|
||||
if size < 6 then
|
||||
break
|
||||
end
|
||||
if (cm == FRAME or cm == FRAME2) and size > 6 then
|
||||
if p + size > limit then
|
||||
break -- a truncated last frame
|
||||
end
|
||||
table.insert(offs, p)
|
||||
table.insert(sizes, size)
|
||||
end
|
||||
p += size
|
||||
end
|
||||
local count = #offs
|
||||
if frames > 0 and count > frames and not (opts and opts.ringFrame) then
|
||||
count = frames -- the ring frame is not a frame of the animation
|
||||
end
|
||||
local dmx = Demux.new("fli", { track }, b, {
|
||||
type = magic,
|
||||
frames = frames,
|
||||
depth = depth,
|
||||
speed = speed,
|
||||
ringFrame = #offs > frames and frames > 0,
|
||||
})
|
||||
for i = 1, count do
|
||||
dmx:add(1, (i - 1) * speed, nil, speed, i == 1, offs[i], sizes[i])
|
||||
end
|
||||
dmx:finish()
|
||||
return dmx, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- open(r, opts?) -> a VideoDecoder | nil, Err
|
||||
function Fli.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
||||
local dmx, err = Fli.demux(r, opts)
|
||||
if not dmx then
|
||||
return nil, err
|
||||
end
|
||||
return Video.open(dmx, opts :: Video.OpenOptions?)
|
||||
end
|
||||
|
||||
return Fli
|
||||
@@ -0,0 +1,105 @@
|
||||
-- video/fli tests: FLI (COLOR_64, BRUN, LC, BLACK, COPY, PSTAMP, a ring frame), FLC (prefix chunk, COLOR_256, SS2 with
|
||||
-- line skips and the last-byte opcode, an empty frame), an odd-size FLC, and 15 / 16 / 24-bit FLCs (BRUN, DTA_BRUN,
|
||||
-- DELTA, DTA_LC, COPY, DTA_COPY, BLACK), all encoded by the fixture script: every frame equals ffmpeg's decode
|
||||
-- (CRC-32 of rgba), frame rates, the ring frame option, seeking, errors, the fuzz pass. Fixtures: tools/fixtures/fli.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Fli = require("@formats/video/fli")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/fli_cases")
|
||||
|
||||
local t = T.suite("fli")
|
||||
t:eq(Fli.name, "fli", "name")
|
||||
t:eq(Fli.category, "video", "category")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Fli.sniff(reader(c.data)) > 0, label .. ": sniff")
|
||||
t:eq(F.detect(buffer.fromstring(c.data)), "fli", label .. ": detect")
|
||||
local v, err = F.video.open(buffer.fromstring(c.data))
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, "flic", label .. ": codec")
|
||||
t:check(v.width == c.width and v.height == c.height, label .. ": size")
|
||||
t:check(math.abs(v.frameRate - c.rate) < 1e-9, label .. ": frame rate " .. v.frameRate)
|
||||
t:eq(v.track.bitsPerPixel, c.depth, label .. ": depth")
|
||||
local ring = v.demuxer.meta.ringFrame
|
||||
local want = if ring then #c.frames - 1 else #c.frames
|
||||
local n, bad = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f then
|
||||
t:check(ferr == nil, label .. ": " .. tostring(ferr))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:check(n == want and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ)", label, n - bad, want, bad))
|
||||
v:seek(0.25)
|
||||
local f = v:nextFrame()
|
||||
local k = math.floor(0.25 * c.rate + 1 - 1e-9) + 1
|
||||
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
|
||||
if ring then
|
||||
local v2 = Fli.open(reader(c.data), { ringFrame = true })
|
||||
local m = 0
|
||||
while v2 and v2:nextFrame() do
|
||||
m += 1
|
||||
end
|
||||
t:eq(m, #c.frames, label .. ": with the ring frame")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Fli.open(reader(string.sub(Fx[1].data, 1, 100)))
|
||||
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
|
||||
local bad = Fx[1].data
|
||||
bad = string.sub(bad, 1, 4) .. "\0\0" .. string.sub(bad, 7)
|
||||
t:eq(Fli.sniff(reader(bad)), 0, "bad magic: no sniff")
|
||||
local _, e2 = Fli.open(reader(bad))
|
||||
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
|
||||
local big = string.sub(Fx[1].data, 1, 8) .. "\255\15\255\15" .. string.sub(Fx[1].data, 13)
|
||||
local _, e3 = Fli.open(reader(big), { maxPixels = 2 ^ 20 })
|
||||
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = Fli.open(Bytes.reader(b))
|
||||
if v then
|
||||
while v:nextFrame() do
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, samples, { mutations = 12, garbage = 40, keepHeader = 16 })
|
||||
-- the frame decoder itself (codecs/video/Legacy/Flic, native: no pcall in between)
|
||||
local Flic = require("@formats/codecs/video/Legacy").Flic
|
||||
for _, c in { Fx[1], Fx[2], Fx[4], Fx[6] } do
|
||||
local d = Fli.demux(reader(c.data))
|
||||
local packets = {}
|
||||
for k = 1, math.min(4, d:count(1)) do
|
||||
table.insert(packets, d:get(1, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " frames", function(b: buffer)
|
||||
local st = Flic.state(c.width, c.height, c.depth)
|
||||
if st then
|
||||
Flic.decode(st, b)
|
||||
return Flic.image(st)
|
||||
end
|
||||
return nil
|
||||
end, packets, { mutations = 20, garbage = 30 })
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,395 @@
|
||||
# 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)
|
||||
Reference in new issue
Block a user