150 lines
4.7 KiB
Luau
150 lines
4.7 KiB
Luau
--!strict
|
|
-- video/fli: Autodesk FLI / FLC / FLX animations, from Jim Kent's "FLC - Animator Pro Flic Files" (MultimediaWiki
|
|
-- "FLIC"): magics 0xAF11 (speed in 1/70 s), 0xAF12 / 0xAF44 (ms); frame chunks 0xF1FA / 0xF5FA, rgba8 through
|
|
-- codecs/video/Legacy; the ring frame is not shown. Not supported: writing, 1-bit FLICs, X-COM / Magic Carpet variants.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Demux = require("@rosetta/containers/Demux")
|
|
local Video = require("@rosetta/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?, -- also show the ring frame (as ffmpeg)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 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
|
|
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,
|
|
}
|
|
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
|
|
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
|
|
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
|