codecs/video/Legacy: Cinepak, Microsoft Video 1, QuickTime RLE / SMC and FLIC decoders
Registry decoders (codecs/video/Video BUILTIN: cvid, CRAM / MSVC / WHAM, 'rle ', 'smc ', AFLC), rgba8 pictures painted over the previous picture, palettes from track.palette (or packet.palette). Bit-exact with ffmpeg on 28 AVI / MOV / MKV fixtures (tools/fixtures/legacy.py: ffmpeg's encoders plus encoders in _legacy_mux.py for QuickTime RLE 1/2/4/8-bit, 8-bit MS Video 1 and Cinepak). Also fixes QtPalette's big-endian reads.
This commit is contained in:
1 parent
7b4bf1ee43
commit
8e3873c9fb
14 files changed
+2171
-3
No files matched your search
@@ -0,0 +1,187 @@
|
||||
--!strict
|
||||
-- codecs/video/Legacy: FLI / FLC, Cinepak, Microsoft Video 1, QuickTime RLE / SMC and Smacker video decoders.
|
||||
-- The 1990s inter-frame codecs as codecs/video/Video registry decoders (rgba8 pictures, one per packet): Autodesk
|
||||
-- FLI / FLC ("flic", the frames of video/fli), Cinepak ("cinepak",
|
||||
-- 'cvid'), Microsoft Video 1 ("msvideo1": CRAM / MSVC / WHAM, 8-bit palettised and 16-bit), QuickTime Animation
|
||||
-- ("qtrle", 'rle ': depths 1 / 2 / 4 / 8 / 16 / 24 / 32 and grey) and QuickTime Graphics ("smc", 'smc ').
|
||||
-- New in formats-luau, written from the format descriptions (see each submodule); decoding is bit-exact with ffmpeg's
|
||||
-- decoders (tests/codecs/legacy.luau). Every decoder keeps the previous picture (a packet paints over it) and returns
|
||||
-- a fresh copy.
|
||||
--
|
||||
-- Palettes come from the Track: track.palette (256 u32 entries, bytes R, G, B, A: the AVI BITMAPINFO palette, the
|
||||
-- QuickTime sample description's colour table, ...) and a packet's own `palette` (same layout) replaces it from that
|
||||
-- packet on. Depths come from track.bitsPerPixel (AVI biBitCount, the QuickTime depth).
|
||||
--
|
||||
-- local dec, err = Legacy.newDecoder("cinepak", track, { maxPixels = 2^22 }) -- a Types.VideoCodecDecoder
|
||||
-- Legacy.CODECS --> { "flic", "cinepak", "msvideo1", "qtrle", "smc" }
|
||||
--
|
||||
-- Never throws: bad tracks return nil, Err; bad packets return Err from decode().
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
local Flic = require("@formats/codecs/video/Legacy/Flic")
|
||||
local Cinepak = require("@formats/codecs/video/Legacy/Cinepak")
|
||||
local Msvideo1 = require("@formats/codecs/video/Legacy/Msvideo1")
|
||||
local QtRle = require("@formats/codecs/video/Legacy/QtRle")
|
||||
local Smc = require("@formats/codecs/video/Legacy/Smc")
|
||||
|
||||
local Legacy = {}
|
||||
|
||||
Legacy.Picture = Picture
|
||||
Legacy.Flic = Flic
|
||||
Legacy.Cinepak = Cinepak
|
||||
Legacy.Msvideo1 = Msvideo1
|
||||
Legacy.QtRle = QtRle
|
||||
Legacy.Smc = Smc
|
||||
Legacy.CODECS = { "flic", "cinepak", "msvideo1", "qtrle", "smc" }
|
||||
|
||||
export type Options = {
|
||||
maxPixels: number?, -- refuse bigger pictures (LIMIT; default 2^26)
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
-- the per-codec pieces: make(track) -> state, step(state, data, packet) -> image? | nil, Err?
|
||||
type Codec = {
|
||||
make: (track: Types.Track) -> (any, string?),
|
||||
step: (st: any, data: buffer, packet: Types.Packet) -> (Types.Image?, Err.Err?),
|
||||
}
|
||||
|
||||
local function fail(msg: string?): Err.Err
|
||||
return Err.from(msg or "corrupt frame")
|
||||
end
|
||||
|
||||
local function packetPalette(pic: Picture.Picture, packet: Types.Packet): boolean
|
||||
local pal = (packet :: any).palette
|
||||
if type(pal) == "buffer" then
|
||||
Picture.setPalette(pic, pal)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local CODECS: { [string]: Codec } = {
|
||||
qtrle = {
|
||||
make = function(t)
|
||||
local depth = t.bitsPerPixel or 24
|
||||
local bits = if depth > 32 then depth - 32 else depth
|
||||
if not (bits == 1 or bits == 2 or bits == 4 or bits == 8 or bits == 16 or bits == 24 or bits == 32) then
|
||||
return nil, "unsupported: depth " .. depth
|
||||
end
|
||||
return { pic = Picture.new(t.width :: number, t.height :: number, QtRle.palettised(depth), QtRle.pad(depth), 1, t.palette), depth = depth }, nil
|
||||
end,
|
||||
step = function(st, data, packet)
|
||||
packetPalette(st.pic, packet)
|
||||
local ok, msg = QtRle.decode(st.pic, data, st.depth)
|
||||
if not ok then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
return Picture.image(st.pic), nil
|
||||
end,
|
||||
},
|
||||
smc = {
|
||||
make = function(t)
|
||||
return { pic = Picture.new(t.width :: number, t.height :: number, true, 4, 4, t.palette), tables = Smc.tables() }, nil
|
||||
end,
|
||||
step = function(st, data, packet)
|
||||
packetPalette(st.pic, packet)
|
||||
local ok, msg = Smc.decode(st.pic, data, st.tables)
|
||||
if not ok then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
return Picture.image(st.pic), nil
|
||||
end,
|
||||
},
|
||||
msvideo1 = {
|
||||
make = function(t)
|
||||
if (t.width :: number) < 4 or (t.height :: number) < 4 then
|
||||
return nil, "smaller than one block"
|
||||
end
|
||||
local bits = if t.bitsPerPixel == 8 then 8 else 16
|
||||
return { pic = Picture.new(t.width :: number, t.height :: number, bits == 8, 4, 4, t.palette), bits = bits }, nil
|
||||
end,
|
||||
step = function(st, data, packet)
|
||||
if st.bits == 8 then
|
||||
packetPalette(st.pic, packet)
|
||||
end
|
||||
local ok, msg = Msvideo1.decode(st.pic, data, st.bits)
|
||||
if not ok then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
return Picture.image(st.pic), nil
|
||||
end,
|
||||
},
|
||||
cinepak = {
|
||||
make = function(t)
|
||||
local pal = t.bitsPerPixel == 8
|
||||
return { pic = Picture.new(t.width :: number, t.height :: number, pal, 4, 4, t.palette), cp = Cinepak.state() }, nil
|
||||
end,
|
||||
step = function(st, data, packet)
|
||||
local newPal = st.pic.idx ~= nil and packetPalette(st.pic, packet)
|
||||
local ok, msg = Cinepak.decode(st.pic, data, st.cp, newPal)
|
||||
if not ok then
|
||||
return nil, if msg then fail(msg) else nil
|
||||
end
|
||||
return Picture.image(st.pic), nil
|
||||
end,
|
||||
},
|
||||
flic = {
|
||||
make = function(t)
|
||||
return Flic.state(t.width :: number, t.height :: number, t.bitsPerPixel or 8)
|
||||
end,
|
||||
step = function(st, data, _packet)
|
||||
local ok, msg = Flic.decode(st, data)
|
||||
if not ok then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
return Flic.image(st), nil
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
||||
-- newDecoder(id, track, opts?) -> a Types.VideoCodecDecoder | nil, Err
|
||||
function Legacy.newDecoder(id: string, track: Types.Track, opts: Options?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local codec = CODECS[id]
|
||||
if not codec then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "no legacy codec " .. tostring(id))
|
||||
end
|
||||
local w, h = track.width or 0, track.height or 0
|
||||
if type(w) ~= "number" or type(h) ~= "number" or w <= 0 or h <= 0 or w ~= w // 1 or h ~= h // 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "video track without a valid size")
|
||||
end
|
||||
local max = (opts and opts.maxPixels) or 2 ^ 26
|
||||
if w * h > max or w > 32768 or h > 32768 then
|
||||
return nil, Err.new(Err.LIMIT, "picture of " .. w .. " x " .. h .. " over the limit")
|
||||
end
|
||||
local st, msg = codec.make(track)
|
||||
if not st then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
-- Video.simpleDecoder, without requiring the registry (which requires this module)
|
||||
local queue: { any } = {}
|
||||
local self = {}
|
||||
function self.decode(_: any, packet: Types.Packet): Err.Err?
|
||||
local ok, img, err = pcall(codec.step, st, packet.data, packet)
|
||||
if not ok then
|
||||
return Err.new(Err.CORRUPT, "decoder failed: " .. tostring(img))
|
||||
end
|
||||
if img then
|
||||
table.insert(queue, { img, packet.pts, packet.keyframe })
|
||||
end
|
||||
return err
|
||||
end
|
||||
function self.receive(_: any): (Types.Image?, number?, boolean?)
|
||||
local q = table.remove(queue, 1)
|
||||
if q then
|
||||
return q[1], q[2], q[3]
|
||||
end
|
||||
return nil, nil, nil
|
||||
end
|
||||
function self.flush(_: any) end
|
||||
function self.reset(_: any)
|
||||
table.clear(queue)
|
||||
st = codec.make(track) or st -- back to a black picture with the track's palette (decoding restarts at a keyframe)
|
||||
end
|
||||
return self :: any, nil
|
||||
end
|
||||
|
||||
return Legacy
|
||||
Reference in new issue
Block a user