--!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