merge ws/video2-legacy (FLIC, Cinepak, MSV1, QT RLE / SMC, Smacker; video/fli, video/smk)
This commit is contained in:
commit
9694c652ec
32 files changed
+4786
-5
No files matched your search
@@ -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",
|
||||
@@ -308,8 +320,8 @@ local list: { Entry } = {
|
||||
path = "video/mkv",
|
||||
extensions = { "mkv", "webm", "mka", "mk3d" },
|
||||
mime = { "video/x-matroska", "video/webm", "audio/x-matroska", "audio/webm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" },
|
||||
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, subtitles = true },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "containers/Avi", "containers/IsoBmff/QtPalette", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" },
|
||||
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "theora", "rawvideo" }, audio = true, subtitles = true },
|
||||
load = function()
|
||||
return require("@formats/video/mkv")
|
||||
end,
|
||||
@@ -362,6 +374,18 @@ local list: { Entry } = {
|
||||
return require("@formats/video/ogv")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "smk",
|
||||
category = "video",
|
||||
path = "video/smk",
|
||||
extensions = { "smk" },
|
||||
mime = { "video/vnd.radgamettools.smacker" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/video/Video", "codecs/audio/Stream", "codecs/video/Legacy/Smacker" },
|
||||
caps = { decode = true, encode = false, streaming = true, video = { "smackvid" }, audio = { "smackaudio", "pcm_u8", "pcm_s16le" } },
|
||||
load = function()
|
||||
return require("@formats/video/smk")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "y4m",
|
||||
category = "video",
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Cinepak: Cinepak ('cvid') frames: 24-bit colour (codebook entries of 4 luma + 2 chroma, or 4
|
||||
-- luma for grey), and 8-bit palettised streams (entries are palette indexes). Written from the format description
|
||||
-- (MultimediaWiki "Cinepak"): a frame header (flags, size, width, height, strip count), strips with their own V1 / V4
|
||||
-- codebooks (full or partial updates, kept per strip between frames, inherited from the strip above unless the frame
|
||||
-- flags say otherwise) and the vector chunks (intra, inter with skip flags, V1-only). Colour codebook entries are
|
||||
-- converted once with the Cinepak YUV formula (r = y + 2v, g = y - u/2 - v, b = y + 2u, clamped). Sega FILM's extra
|
||||
-- header bytes are detected on the first frame. Paints over the previous picture. Bit-exact with ffmpeg's cinepak
|
||||
-- decoder. Never throws: every read is range-checked against its chunk.
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu32, buffer.writeu32, buffer.writeu8
|
||||
local band = bit32.band
|
||||
|
||||
local Cinepak = {}
|
||||
|
||||
local MAX_STRIPS = 32
|
||||
-- a codebook: 256 entries of 4 u32 rgba pixels (16 bytes); palettised streams use the low byte (the index)
|
||||
local ENTRY = 16
|
||||
local BOOK = 256 * ENTRY
|
||||
|
||||
export type State = {
|
||||
books: buffer, -- MAX_STRIPS * 2 codebooks (V4 then V1 per strip)
|
||||
filmSkip: number, -- Sega FILM extra header bytes (-1 = not known yet)
|
||||
}
|
||||
|
||||
function Cinepak.state(): State
|
||||
return { books = buffer.create(MAX_STRIPS * 2 * BOOK), filmSkip = -1 }
|
||||
end
|
||||
|
||||
local function be16(d: buffer, o: number): number
|
||||
return readu8(d, o) * 256 + readu8(d, o + 1)
|
||||
end
|
||||
local function be24(d: buffer, o: number): number
|
||||
return readu8(d, o) * 65536 + readu8(d, o + 1) * 256 + readu8(d, o + 2)
|
||||
end
|
||||
local function be32(d: buffer, o: number): number
|
||||
return readu8(d, o) * 16777216 + readu8(d, o + 1) * 65536 + readu8(d, o + 2) * 256 + readu8(d, o + 3)
|
||||
end
|
||||
|
||||
local function clip(v: number): number
|
||||
return if v < 0 then 0 elseif v > 255 then 255 else v
|
||||
end
|
||||
|
||||
-- a codebook chunk (ids 0x20..0x27): entries of 4 or 6 bytes, all of them or those whose flag bit is set
|
||||
local function codebook(books: buffer, book: number, id: number, d: buffer, p: number, stop: number)
|
||||
local six = band(id, 4) == 0
|
||||
local partial = band(id, 1) ~= 0
|
||||
local size = if six then 6 else 4
|
||||
local flag, mask = 0, 0
|
||||
for i = 0, 255 do
|
||||
if partial then
|
||||
mask //= 2
|
||||
if mask == 0 then
|
||||
if p + 4 > stop then
|
||||
return
|
||||
end
|
||||
flag = be32(d, p)
|
||||
p += 4
|
||||
mask = 0x80000000
|
||||
end
|
||||
end
|
||||
if not partial or band(flag, mask) ~= 0 then
|
||||
if p + size > stop then
|
||||
return
|
||||
end
|
||||
local e = book + i * ENTRY
|
||||
local y0, y1, y2, y3 = readu8(d, p), readu8(d, p + 1), readu8(d, p + 2), readu8(d, p + 3)
|
||||
if six then
|
||||
local u, v = readu8(d, p + 4), readu8(d, p + 5)
|
||||
if u >= 128 then
|
||||
u -= 256
|
||||
end
|
||||
if v >= 128 then
|
||||
v -= 256
|
||||
end
|
||||
local uh = if u < 0 then -((-u) // 2) else u // 2 -- C division (towards zero)
|
||||
local dr, dg, db = 2 * v, -uh - v, 2 * u
|
||||
writeu32(books, e, clip(y0 + dr) + clip(y0 + dg) * 256 + clip(y0 + db) * 65536 + 0xFF000000)
|
||||
writeu32(books, e + 4, clip(y1 + dr) + clip(y1 + dg) * 256 + clip(y1 + db) * 65536 + 0xFF000000)
|
||||
writeu32(books, e + 8, clip(y2 + dr) + clip(y2 + dg) * 256 + clip(y2 + db) * 65536 + 0xFF000000)
|
||||
writeu32(books, e + 12, clip(y3 + dr) + clip(y3 + dg) * 256 + clip(y3 + db) * 65536 + 0xFF000000)
|
||||
else
|
||||
writeu32(books, e, y0 * 0x10101 + 0xFF000000)
|
||||
writeu32(books, e + 4, y1 * 0x10101 + 0xFF000000)
|
||||
writeu32(books, e + 8, y2 * 0x10101 + 0xFF000000)
|
||||
writeu32(books, e + 12, y3 * 0x10101 + 0xFF000000)
|
||||
end
|
||||
p += size
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the vectors of a strip (ids 0x30 intra, 0x31 inter with skip flags, 0x32 V1 only); false on truncated data
|
||||
local function vectors(pic: Picture.Picture, books: buffer, v4: number, v1: number, id: number, d: buffer, p: number,
|
||||
stop: number, x1: number, y1: number, x2: number, y2: number): boolean
|
||||
local stride = pic.stride
|
||||
local pal = pic.idx
|
||||
local px = (pal or pic.rgba) :: buffer
|
||||
local flag, mask = 0, 0
|
||||
local inter = band(id, 1) ~= 0
|
||||
local v1only = band(id, 2) ~= 0
|
||||
for y = y1, y2 - 1, 4 do
|
||||
for x = x1, x2 - 1, 4 do
|
||||
local paint = true
|
||||
if inter then
|
||||
mask //= 2
|
||||
if mask == 0 then
|
||||
if p + 4 > stop then
|
||||
return false
|
||||
end
|
||||
flag = be32(d, p)
|
||||
p += 4
|
||||
mask = 0x80000000
|
||||
end
|
||||
paint = band(flag, mask) ~= 0
|
||||
end
|
||||
if paint then
|
||||
local useV1 = v1only
|
||||
if not v1only then
|
||||
mask //= 2
|
||||
if mask == 0 then
|
||||
if p + 4 > stop then
|
||||
return false
|
||||
end
|
||||
flag = be32(d, p)
|
||||
p += 4
|
||||
mask = 0x80000000
|
||||
end
|
||||
useV1 = band(flag, mask) == 0
|
||||
end
|
||||
local o = y * stride + x
|
||||
if useV1 then
|
||||
if p >= stop then
|
||||
return false
|
||||
end
|
||||
local e = v1 + readu8(d, p) * ENTRY
|
||||
p += 1
|
||||
-- each of the 4 entries fills a 2x2 square
|
||||
local a, b, c, e4 = readu32(books, e), readu32(books, e + 4), readu32(books, e + 8), readu32(books, e + 12)
|
||||
if pal then
|
||||
a, b, c, e4 = a % 256, b % 256, c % 256, e4 % 256
|
||||
writeu32(px, o, a * 0x0101 + b * 0x01010000)
|
||||
writeu32(px, o + stride, a * 0x0101 + b * 0x01010000)
|
||||
writeu32(px, o + 2 * stride, c * 0x0101 + e4 * 0x01010000)
|
||||
writeu32(px, o + 3 * stride, c * 0x0101 + e4 * 0x01010000)
|
||||
else
|
||||
for r = 0, 3 do
|
||||
local q = (o + r * stride) * 4
|
||||
local l, rr = if r < 2 then a else c, if r < 2 then b else e4
|
||||
writeu32(px, q, l)
|
||||
writeu32(px, q + 4, l)
|
||||
writeu32(px, q + 8, rr)
|
||||
writeu32(px, q + 12, rr)
|
||||
end
|
||||
end
|
||||
else
|
||||
if p + 4 > stop then
|
||||
return false
|
||||
end
|
||||
local e0 = v4 + readu8(d, p) * ENTRY
|
||||
local e1 = v4 + readu8(d, p + 1) * ENTRY
|
||||
local e2 = v4 + readu8(d, p + 2) * ENTRY
|
||||
local e3 = v4 + readu8(d, p + 3) * ENTRY
|
||||
p += 4
|
||||
-- entry k covers the 2x2 square k (top-left, top-right, bottom-left, bottom-right)
|
||||
if pal then
|
||||
for r = 0, 3 do
|
||||
local l, rr = if r < 2 then e0 else e2, if r < 2 then e1 else e3
|
||||
local off = (r % 2) * 8
|
||||
local q = o + r * stride
|
||||
writeu8(px, q, readu8(books, l + off))
|
||||
writeu8(px, q + 1, readu8(books, l + off + 4))
|
||||
writeu8(px, q + 2, readu8(books, rr + off))
|
||||
writeu8(px, q + 3, readu8(books, rr + off + 4))
|
||||
end
|
||||
else
|
||||
for r = 0, 3 do
|
||||
local l, rr = if r < 2 then e0 else e2, if r < 2 then e1 else e3
|
||||
local off = (r % 2) * 8
|
||||
local q = (o + r * stride) * 4
|
||||
writeu32(px, q, readu32(books, l + off))
|
||||
writeu32(px, q + 4, readu32(books, l + off + 4))
|
||||
writeu32(px, q + 8, readu32(books, rr + off))
|
||||
writeu32(px, q + 12, readu32(books, rr + off + 4))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- decode(pic, data, state, hasPalette) -> picture?, message: true = a picture; false + nil = no picture for this
|
||||
-- packet (an empty frame); false + message = an error
|
||||
function Cinepak.decode(pic: Picture.Picture, d: buffer, st: State, newPalette: boolean): (boolean, string?)
|
||||
local n = buffer.len(d)
|
||||
if n < 10 then
|
||||
return false, "frame too short"
|
||||
end
|
||||
local strips = be16(d, 8)
|
||||
if strips == 0 and not (pic.idx and newPalette) then
|
||||
return false, nil
|
||||
end
|
||||
local size = be24(d, 1)
|
||||
if n < size * 5 // 100 then
|
||||
return false, "frame shorter than its header says"
|
||||
end
|
||||
if st.filmSkip < 0 then
|
||||
if size == 0 then
|
||||
return false, "unsupported: a zero frame size"
|
||||
end
|
||||
if size ~= n and n % size ~= 0 then
|
||||
if n >= 16 and readu8(d, 10) == 0xFE and readu8(d, 11) == 0 and readu8(d, 12) == 0 and readu8(d, 13) == 6
|
||||
and readu8(d, 14) == 0 and readu8(d, 15) == 0 then
|
||||
st.filmSkip = 6
|
||||
else
|
||||
st.filmSkip = 2
|
||||
end
|
||||
else
|
||||
st.filmSkip = 0
|
||||
end
|
||||
end
|
||||
local skip = st.filmSkip
|
||||
if n < 10 + skip + strips * 12 then
|
||||
return false, "truncated strip headers"
|
||||
end
|
||||
if strips > 0 then
|
||||
local ss = be24(d, 10 + skip + 1)
|
||||
if ss < 12 or ss > size then
|
||||
return false, "bad strip size"
|
||||
end
|
||||
end
|
||||
local flags = readu8(d, 0)
|
||||
local p = 10 + skip
|
||||
strips = math.min(strips, MAX_STRIPS)
|
||||
local books = st.books
|
||||
-- the coded area: the size rounded up to whole blocks (the picture's plane)
|
||||
local cw, ch = pic.stride, pic.rows
|
||||
local y0 = 0
|
||||
for i = 0, strips - 1 do
|
||||
if p + 12 > n then
|
||||
return true, nil
|
||||
end
|
||||
local sy1 = be16(d, p + 4)
|
||||
local sx1 = be16(d, p + 6)
|
||||
local sy2, sx2 = be16(d, p + 8), be16(d, p + 10)
|
||||
if sy1 == 0 then
|
||||
sy1 = y0
|
||||
sy2 = y0 + sy2
|
||||
end
|
||||
local ssize = be24(d, p + 1) - 12
|
||||
if ssize < 0 then
|
||||
return true, nil
|
||||
end
|
||||
p += 12
|
||||
local stop = math.min(n, p + ssize)
|
||||
local v4 = i * 2 * BOOK
|
||||
local v1 = v4 + BOOK
|
||||
if i > 0 and band(flags, 1) == 0 then
|
||||
buffer.copy(books, v4, books, v4 - 2 * BOOK, 2 * BOOK)
|
||||
end
|
||||
-- the strip's chunks: codebooks, then one vector chunk
|
||||
-- (strips start on block boundaries in every known stream: the plane holds whole blocks only)
|
||||
if sx2 > cw or sy2 > ch or sx1 >= sx2 or sy1 >= sy2 or band(sx1, 3) ~= 0 or band(sy1, 3) ~= 0 then
|
||||
return true, nil
|
||||
end
|
||||
local q = p
|
||||
local done = false
|
||||
while q + 4 <= stop do
|
||||
local id = readu8(d, q)
|
||||
local csize = be24(d, q + 1) - 4
|
||||
if csize < 0 then
|
||||
return true, nil
|
||||
end
|
||||
q += 4
|
||||
local cstop = math.min(stop, q + csize)
|
||||
if id == 0x20 or id == 0x21 or id == 0x24 or id == 0x25 then
|
||||
codebook(books, v4, id, d, q, cstop)
|
||||
elseif id == 0x22 or id == 0x23 or id == 0x26 or id == 0x27 then
|
||||
codebook(books, v1, id, d, q, cstop)
|
||||
elseif id == 0x30 or id == 0x31 or id == 0x32 then
|
||||
if not vectors(pic, books, v4, v1, id, d, q, cstop, sx1, sy1, sx2, sy2) then
|
||||
return true, nil
|
||||
end
|
||||
done = true
|
||||
break
|
||||
end
|
||||
q = cstop
|
||||
end
|
||||
if not done then
|
||||
return true, nil
|
||||
end
|
||||
p = stop
|
||||
y0 = sy2
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Cinepak
|
||||
@@ -0,0 +1,372 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Flic: Autodesk Animator FLI / FLC frames (the F1FA frame chunks; the file format is
|
||||
-- video/fli). Written from the FLIC file format description (Jim Kent's "FLC - Animator Pro Flic Files", Dr. Dobb's
|
||||
-- 1993; MultimediaWiki "Flic Video"). 8-bit palettised: COLOR_256 (4), COLOR_64 (11, 6-bit entries widened by bit
|
||||
-- replication), DELTA_FLC / SS2 (7: word runs, line skips, last-byte opcode), DELTA_FLI / LC (12: byte runs), BLACK
|
||||
-- (13), BYTE_RUN / BRUN (15), FLI_COPY (16, rows padded to 4), PSTAMP (18, skipped). 15 / 16 / 24-bit FLC and FLX
|
||||
-- (DTA): DELTA / DTA_LC (pixel runs), BLACK, BRUN (byte runs), DTA_BRUN (25, pixel runs), COPY / DTA_COPY (16 / 26);
|
||||
-- pixels are 5-5-5 / 5-6-5 little-endian words or B, G, R bytes. Not supported: 1-bit FLICs, the Magic Carpet
|
||||
-- 6-bit COLOR_256 variant. Paints over the previous picture. Bit-exact with ffmpeg's flic decoder on valid frames.
|
||||
-- Never throws: reads past the frame give zeros, every write is range-checked (a damaged frame gives no picture).
|
||||
local Types = require("@formats/Types")
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, writeu8, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32
|
||||
local bfill = buffer.fill
|
||||
|
||||
local Flic = {}
|
||||
|
||||
-- the decoding state of one stream: a byte plane (stride = width * bytes per pixel) and the palette
|
||||
export type State = {
|
||||
pic: Picture.Picture,
|
||||
depth: number, -- 8, 15, 16, 24
|
||||
bpp: number, -- bytes per pixel
|
||||
}
|
||||
|
||||
-- state(w, h, depth) -> State | nil, message
|
||||
function Flic.state(w: number, h: number, depth: number): (State?, string?)
|
||||
local bpp = if depth == 8 then 1 elseif depth == 15 or depth == 16 then 2 elseif depth == 24 then 3 else 0
|
||||
if bpp == 0 then
|
||||
return nil, "unsupported: " .. depth .. "-bit FLIC"
|
||||
end
|
||||
local pic = Picture.new(w * bpp, h, true)
|
||||
pic.w = w
|
||||
return { pic = pic, depth = depth, bpp = bpp }, nil
|
||||
end
|
||||
|
||||
local function s8(v: number): number
|
||||
return if v >= 128 then v - 256 else v
|
||||
end
|
||||
|
||||
-- decode(st, data) -> true | false, message
|
||||
function Flic.decode(st: State, d: buffer): (boolean, string?)
|
||||
local pic = st.pic
|
||||
local px = pic.idx :: buffer
|
||||
local pal = pic.pal
|
||||
local w, h = pic.w, pic.h
|
||||
local bpp = st.bpp
|
||||
local deep = bpp > 1
|
||||
local stride = pic.stride
|
||||
local limit = stride * h
|
||||
local n = buffer.len(d)
|
||||
if n < 16 then
|
||||
return false, "frame too short"
|
||||
end
|
||||
local p = 0
|
||||
local function u8(): number
|
||||
local v = if p < n then readu8(d, p) else 0
|
||||
p += 1
|
||||
return v
|
||||
end
|
||||
local function u16(): number
|
||||
local a = u8()
|
||||
return a + u8() * 256
|
||||
end
|
||||
local frameSize = math.min(readu32(d, 0), n)
|
||||
local chunks = readu8(d, 6) + readu8(d, 7) * 256
|
||||
if frameSize < 16 then
|
||||
return false, "bad frame size"
|
||||
end
|
||||
frameSize -= 16
|
||||
p = 16
|
||||
while frameSize >= 6 and chunks > 0 and n - p >= 4 do
|
||||
local size = readu32(d, p)
|
||||
if size > frameSize then
|
||||
size = frameSize
|
||||
end
|
||||
local after = p + size
|
||||
p += 4
|
||||
local typ = u16()
|
||||
if typ == 4 or typ == 11 then
|
||||
if not deep then
|
||||
-- palette packets: skip count, change count (0 = 256), r g b
|
||||
local shift = typ == 11
|
||||
local packets = u16()
|
||||
local at = 0
|
||||
for _ = 1, packets do
|
||||
at += u8()
|
||||
local count = u8()
|
||||
if count == 0 then
|
||||
count = 256
|
||||
end
|
||||
if p + count * 3 > after then
|
||||
break
|
||||
end
|
||||
for _ = 1, count do
|
||||
if at >= 256 then
|
||||
at = 0
|
||||
end
|
||||
local r, g, b = u8(), u8(), u8()
|
||||
if shift then
|
||||
-- 6-bit entries: v << 2 (kept to 8 bits), the top two bits repeated below
|
||||
r, g, b = r * 4 % 256, g * 4 % 256, b * 4 % 256
|
||||
r, g, b = r + r // 64, g + g // 64, b + b // 64
|
||||
end
|
||||
writeu32(pal, at * 4, r + g * 256 + b * 65536 + 0xFF000000)
|
||||
at += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif typ == 7 or (deep and typ == 27) then
|
||||
-- word-oriented delta: per line a count of packets (or a line skip / last-byte opcode)
|
||||
local y = 0
|
||||
local lines = u16()
|
||||
while lines > 0 do
|
||||
if p + 2 > after then
|
||||
break
|
||||
end
|
||||
if y > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
local lp = u16()
|
||||
if lp >= 0x8000 then
|
||||
lp -= 0x10000
|
||||
end
|
||||
if deep then
|
||||
if lp < 0 then
|
||||
if -lp > h then
|
||||
return false, "bad line skip"
|
||||
end
|
||||
y += -lp * stride
|
||||
continue
|
||||
end
|
||||
elseif lp < 0 and lp >= -0x4000 then
|
||||
-- 0xC000 .. 0xFFFF: skip lines
|
||||
if -lp > h then
|
||||
return false, "bad line skip"
|
||||
end
|
||||
y += -lp * stride
|
||||
continue
|
||||
elseif lp < 0 then
|
||||
-- 0x8000 .. 0xBFFF: the low byte goes into the line's last pixel (ffmpeg writes the last byte of its
|
||||
-- padded row instead: the same pixel when rows are not padded)
|
||||
local q = y + w - 1
|
||||
if q < 0 or q >= limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
writeu8(px, q, lp % 256)
|
||||
continue
|
||||
elseif lp >= 0x4000 then
|
||||
continue -- undefined opcode
|
||||
end
|
||||
lines -= 1
|
||||
local q = y
|
||||
for _ = 1, lp do
|
||||
if p + 2 > after then
|
||||
break
|
||||
end
|
||||
q += u8() * bpp
|
||||
local run = s8(u8())
|
||||
if deep then
|
||||
local cnt = if run < 0 then -run else run
|
||||
if q + cnt * bpp > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
if run < 0 then
|
||||
local a, b, c = u8(), u8(), if bpp == 3 then u8() else 0
|
||||
for _ = 1, cnt do
|
||||
writeu8(px, q, a)
|
||||
writeu8(px, q + 1, b)
|
||||
if bpp == 3 then
|
||||
writeu8(px, q + 2, c)
|
||||
end
|
||||
q += bpp
|
||||
end
|
||||
else
|
||||
if p + cnt * bpp > after then
|
||||
break
|
||||
end
|
||||
buffer.copy(px, q, d, p, cnt * bpp)
|
||||
p += cnt * bpp
|
||||
q += cnt * bpp
|
||||
end
|
||||
elseif run < 0 then
|
||||
local cnt = -run
|
||||
local a, b = u8(), u8()
|
||||
if q + cnt * 2 > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
for _ = 1, cnt do
|
||||
writeu8(px, q, a)
|
||||
writeu8(px, q + 1, b)
|
||||
q += 2
|
||||
end
|
||||
else
|
||||
if q + run * 2 > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
if p + run * 2 > after then
|
||||
break
|
||||
end
|
||||
buffer.copy(px, q, d, p, run * 2)
|
||||
p += run * 2
|
||||
q += run * 2
|
||||
end
|
||||
end
|
||||
y += stride
|
||||
end
|
||||
elseif typ == 12 and not deep then
|
||||
-- byte-oriented delta (LC): first line, line count, per line packets of (skip, run)
|
||||
local first = u16()
|
||||
if first >= h then
|
||||
return false, "bad first line"
|
||||
end
|
||||
local y = first * stride
|
||||
local lines = u16()
|
||||
while lines > 0 do
|
||||
local q = y
|
||||
if q > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
if p + 1 > after then
|
||||
break
|
||||
end
|
||||
local packets = u8()
|
||||
for _ = 1, packets do
|
||||
if p + 1 > after then
|
||||
break
|
||||
end
|
||||
q += u8()
|
||||
local run = s8(u8())
|
||||
if run > 0 then
|
||||
if q + run > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
if p + run > after then
|
||||
break
|
||||
end
|
||||
buffer.copy(px, q, d, p, run)
|
||||
p += run
|
||||
q += run
|
||||
elseif run < 0 then
|
||||
local v = u8()
|
||||
if q - run > limit then
|
||||
return false, "delta past the picture"
|
||||
end
|
||||
bfill(px, q, v, -run)
|
||||
q -= run
|
||||
end
|
||||
end
|
||||
y += stride
|
||||
lines -= 1
|
||||
end
|
||||
elseif typ == 13 then
|
||||
bfill(px, 0, 0, limit)
|
||||
elseif typ == 15 or (deep and typ == 25) then
|
||||
-- byte run (BRUN: bytes; DTA_BRUN: pixels of a deep FLIC): > 0 a run of one value, < 0 literals
|
||||
local unit = if typ == 25 then bpp else 1
|
||||
local y = 0
|
||||
for _ = 1, h do
|
||||
local q = y
|
||||
p += 1 -- the packet count (unused)
|
||||
local left = if typ == 25 then w else w * bpp
|
||||
while left > 0 do
|
||||
if p + 1 > after then
|
||||
break
|
||||
end
|
||||
local run = s8(u8())
|
||||
if run == 0 and not deep then
|
||||
return false, "zero byte run"
|
||||
end
|
||||
if run > 0 then
|
||||
if q + run * unit > limit then
|
||||
return false, "run past the picture"
|
||||
end
|
||||
if unit == 1 then
|
||||
bfill(px, q, u8(), run)
|
||||
q += run
|
||||
else
|
||||
local a, b, c = u8(), u8(), if bpp == 3 then u8() else 0
|
||||
for _ = 1, run do
|
||||
writeu8(px, q, a)
|
||||
writeu8(px, q + 1, b)
|
||||
if bpp == 3 then
|
||||
writeu8(px, q + 2, c)
|
||||
end
|
||||
q += bpp
|
||||
end
|
||||
end
|
||||
left -= run
|
||||
else
|
||||
local cnt = -run
|
||||
if q + cnt * unit > limit then
|
||||
return false, "run past the picture"
|
||||
end
|
||||
if p + cnt * unit > after then
|
||||
break
|
||||
end
|
||||
buffer.copy(px, q, d, p, cnt * unit)
|
||||
p += cnt * unit
|
||||
q += cnt * unit
|
||||
left -= cnt
|
||||
end
|
||||
end
|
||||
y += stride
|
||||
end
|
||||
elseif typ == 16 or (deep and typ == 26) then
|
||||
-- uncompressed rows (8-bit rows padded to 4 bytes; deep rows of an odd width carry one pixel more)
|
||||
local row = w * bpp
|
||||
local padded = if deep then row + (w % 2) * bpp else ((w + 3) // 4) * 4
|
||||
local ok = if deep then size - 6 <= ((w + 1) // 2) * 2 * h * bpp else size - 6 == padded * h
|
||||
if ok then
|
||||
if deep and n - p < row * h then
|
||||
return false, "truncated copy chunk"
|
||||
end
|
||||
for y = 0, h - 1 do
|
||||
local s = p + y * padded
|
||||
local cnt = math.max(0, math.min(row, n - s))
|
||||
if cnt > 0 then
|
||||
buffer.copy(px, y * stride, d, s, cnt)
|
||||
end
|
||||
end
|
||||
p += padded * h
|
||||
end
|
||||
end
|
||||
-- typ 18 (PSTAMP) and unknown chunks are skipped
|
||||
if after - p >= 0 then
|
||||
p = after
|
||||
else
|
||||
break
|
||||
end
|
||||
frameSize -= size
|
||||
chunks -= 1
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- image(st) -> a fresh rgba8 Image
|
||||
function Flic.image(st: State): Types.Image
|
||||
local pic = st.pic
|
||||
if st.bpp == 1 then
|
||||
return Picture.image(pic)
|
||||
end
|
||||
local w, h = pic.w, pic.h
|
||||
local img = Types.Image.new(w, h, "rgba8")
|
||||
local out = img.pixels
|
||||
local px = pic.idx :: buffer
|
||||
local stride = pic.stride
|
||||
if st.bpp == 3 then
|
||||
for y = 0, h - 1 do
|
||||
local s, o = y * stride, y * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local q = s + x * 3
|
||||
writeu32(out, o + x * 4, readu8(px, q + 2) + readu8(px, q + 1) * 256 + readu8(px, q) * 65536 + 0xFF000000)
|
||||
end
|
||||
end
|
||||
else
|
||||
local lut = if st.depth == 15 then Picture.rgb555() else Picture.rgb565()
|
||||
local mask = if st.depth == 15 then 32768 else 65536
|
||||
for y = 0, h - 1 do
|
||||
local s, o = y * stride, y * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local q = s + x * 2
|
||||
writeu32(out, o + x * 4, readu32(lut, ((readu8(px, q) + readu8(px, q + 1) * 256) % mask) * 4))
|
||||
end
|
||||
end
|
||||
end
|
||||
return img
|
||||
end
|
||||
|
||||
return Flic
|
||||
@@ -0,0 +1,121 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Msvideo1: Microsoft Video 1 (CRAM / MSVC / WHAM) frames, 8-bit palettised and 16-bit
|
||||
-- X-R-G-B 1-5-5-5. Written from the format description (MultimediaWiki "Microsoft Video 1"): 4x4 blocks, block rows
|
||||
-- bottom-up (rows of a block bottom-up too), each block a 16-bit code: a skip count (0x84xx - 0x87xx), a 2-colour
|
||||
-- block (16 flag bits + 2 colours; at 16 bits a first colour with bit 15 set makes it an 8-colour block, one colour
|
||||
-- pair per quadrant; at 8 bits codes >= 0x9000 are 8-colour), or a 1-colour block. Paints over the previous picture.
|
||||
-- Bit-exact with ffmpeg's msvideo1 decoder. Never throws: every read is range-checked (a damaged frame stops there).
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
local band = bit32.band
|
||||
|
||||
local Msvideo1 = {}
|
||||
|
||||
-- the colour of pixel (x, y) of a block (y = 0 is the block's bottom row) for flag bit f: 2-colour c0 / c1
|
||||
-- (bit set = c0), 8-colour: colours[quadrant * 2 + (bit clear)]
|
||||
local function quadrant(x: number, y: number): number
|
||||
return (if y >= 2 then 4 else 0) + (if x >= 2 then 2 else 0)
|
||||
end
|
||||
|
||||
-- decode(pic, data, bits) -> true | false, message (bits = 8: palettised, else 16-bit)
|
||||
function Msvideo1.decode(pic: Picture.Picture, d: buffer, bits: number): (boolean, string?)
|
||||
local n = buffer.len(d)
|
||||
local bw, bh = pic.w // 4, pic.h // 4
|
||||
if n < bw * bh // 512 then
|
||||
return false, "frame too small"
|
||||
end
|
||||
local stride = pic.stride
|
||||
local total = bw * bh
|
||||
local skip = 0
|
||||
local p = 0
|
||||
local wide = bits ~= 8
|
||||
local px = (if wide then pic.rgba else pic.idx) :: buffer
|
||||
local lut = if wide then Picture.rgb555() else nil
|
||||
local c = table.create(8, 0)
|
||||
for by = bh - 1, 0, -1 do
|
||||
for bx = 0, bw - 1 do
|
||||
if skip ~= 0 then
|
||||
skip -= 1
|
||||
total -= 1
|
||||
continue
|
||||
end
|
||||
if p + 2 > n then
|
||||
return true, nil
|
||||
end
|
||||
local a, b = readu8(d, p), readu8(d, p + 1)
|
||||
p += 2
|
||||
if a == 0 and b == 0 and total == 0 then
|
||||
return true, nil
|
||||
end
|
||||
-- the block's bottom-left pixel; its rows go up
|
||||
local o = (by * 4 + 3) * stride + bx * 4
|
||||
if band(b, 0xFC) == 0x84 then
|
||||
skip = (b - 0x84) * 256 + a - 1
|
||||
elseif b < 0x80 or (not wide and b >= 0x90) then
|
||||
local flags = b * 256 + a
|
||||
local eight
|
||||
if wide then
|
||||
if p + 4 > n then
|
||||
return true, nil
|
||||
end
|
||||
c[1], c[2] = readu16(d, p), readu16(d, p + 2)
|
||||
p += 4
|
||||
eight = c[1] >= 0x8000
|
||||
if eight then
|
||||
if p + 12 > n then
|
||||
return true, nil
|
||||
end
|
||||
for k = 3, 8 do
|
||||
c[k] = readu16(d, p)
|
||||
p += 2
|
||||
end
|
||||
end
|
||||
else
|
||||
eight = b >= 0x90
|
||||
local cnt = if eight then 8 else 2
|
||||
if p + cnt > n then
|
||||
return true, nil
|
||||
end
|
||||
for k = 1, cnt do
|
||||
c[k] = readu8(d, p)
|
||||
p += 1
|
||||
end
|
||||
end
|
||||
for y = 0, 3 do
|
||||
local row = o - y * stride
|
||||
for x = 0, 3 do
|
||||
local k = (if band(flags, 1) == 1 then 1 else 2)
|
||||
if eight then
|
||||
k += quadrant(x, y)
|
||||
end
|
||||
flags = flags // 2
|
||||
if lut then
|
||||
writeu32(px, (row + x) * 4, readu32(lut, (c[k] % 32768) * 4))
|
||||
else
|
||||
writeu8(px, row + x, c[k])
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
local v = if wide then readu32(lut :: buffer, ((b * 256 + a) % 32768) * 4) else a
|
||||
for y = 0, 3 do
|
||||
local row = o - y * stride
|
||||
if lut then
|
||||
for x = 0, 3 do
|
||||
writeu32(px, (row + x) * 4, v)
|
||||
end
|
||||
else
|
||||
writeu32(px, row, v * 0x01010101)
|
||||
end
|
||||
end
|
||||
end
|
||||
total -= 1
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Msvideo1
|
||||
@@ -0,0 +1,115 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Picture: the picture every legacy decoder keeps between frames (they are all inter-frame
|
||||
-- codecs: a packet paints over the previous picture). Palettised codecs keep a u8 index plane and a 256-entry palette,
|
||||
-- direct-colour ones an rgba8 plane; both planes are padded (stride / rows) so the codecs' block and run writes past
|
||||
-- the visible edge land in range. Also the 5-5-5 / 5-6-5 to 8-bit tables: channels are widened by bit replication
|
||||
-- (c << 3 | c >> 2), as ffmpeg's swscale does, so the output equals ffmpeg's rgba conversion. New in formats-luau.
|
||||
local Types = require("@formats/Types")
|
||||
local Pixels = require("@formats/codecs/image/Pixels")
|
||||
|
||||
local readu32, writeu32 = buffer.readu32, buffer.writeu32
|
||||
|
||||
local Picture = {}
|
||||
|
||||
export type Picture = {
|
||||
w: number,
|
||||
h: number,
|
||||
stride: number, -- index plane: bytes per row; rgba plane: pixels per row
|
||||
rows: number,
|
||||
idx: buffer?, -- u8 index plane, stride * rows bytes (palettised)
|
||||
rgba: buffer?, -- u32 rgba8 plane, stride * rows * 4 bytes (direct colour)
|
||||
pal: buffer, -- 256 u32 entries, bytes R, G, B, A
|
||||
}
|
||||
|
||||
-- new(w, h, palettised, padW?, padH?, palette?) -> a black picture; padW / padH round the plane up (blocks of 4, ...)
|
||||
function Picture.new(w: number, h: number, palettised: boolean, padW: number?, padH: number?, palette: buffer?): Picture
|
||||
local pw, ph = padW or 1, padH or 1
|
||||
local stride = ((w + pw - 1) // pw) * pw
|
||||
local rows = ((h + ph - 1) // ph) * ph
|
||||
local pal = buffer.create(1024)
|
||||
if palette and buffer.len(palette) >= 1024 then
|
||||
buffer.copy(pal, 0, palette, 0, 1024)
|
||||
end
|
||||
local p: Picture = { w = w, h = h, stride = stride, rows = rows, pal = pal }
|
||||
if palettised then
|
||||
p.idx = buffer.create(stride * rows)
|
||||
else
|
||||
p.rgba = buffer.create(stride * rows * 4)
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
-- image(p) -> a fresh rgba8 Image of the visible w x h
|
||||
function Picture.image(p: Picture): Types.Image
|
||||
local w, h = p.w, p.h
|
||||
local img = Types.Image.new(w, h, "rgba8")
|
||||
local idx, rgba = p.idx, p.rgba
|
||||
if idx then
|
||||
Pixels.indexed(idx, 0, p.stride, 8, img.pixels, w, h, true, p.pal)
|
||||
elseif rgba then
|
||||
if p.stride == w then
|
||||
buffer.copy(img.pixels, 0, rgba, 0, w * h * 4)
|
||||
else
|
||||
for y = 0, h - 1 do
|
||||
buffer.copy(img.pixels, y * w * 4, rgba, y * p.stride * 4, w * 4)
|
||||
end
|
||||
end
|
||||
end
|
||||
return img
|
||||
end
|
||||
|
||||
-- setPalette(p, src): a 1024-byte palette (u32 R, G, B, A entries) from a packet or the track
|
||||
function Picture.setPalette(p: Picture, src: buffer)
|
||||
if buffer.len(src) >= 1024 then
|
||||
buffer.copy(p.pal, 0, src, 0, 1024)
|
||||
end
|
||||
end
|
||||
|
||||
local lut555: buffer? = nil
|
||||
local lut565: buffer? = nil
|
||||
|
||||
-- rgb555() -> u32 table: X-R-G-B 1-5-5-5 value -> rgba8 pixel (opaque); 32768 entries
|
||||
function Picture.rgb555(): buffer
|
||||
local t = lut555
|
||||
if t then
|
||||
return t
|
||||
end
|
||||
local b = buffer.create(32768 * 4)
|
||||
for v = 0, 32767 do
|
||||
local r, g, bl = v // 1024, (v // 32) % 32, v % 32
|
||||
r, g, bl = r * 8 + r // 4, g * 8 + g // 4, bl * 8 + bl // 4
|
||||
writeu32(b, v * 4, r + g * 256 + bl * 65536 + 0xFF000000)
|
||||
end
|
||||
lut555 = b
|
||||
return b
|
||||
end
|
||||
|
||||
-- rgb565() -> u32 table: R-G-B 5-6-5 value -> rgba8 pixel; 65536 entries
|
||||
function Picture.rgb565(): buffer
|
||||
local t = lut565
|
||||
if t then
|
||||
return t
|
||||
end
|
||||
local b = buffer.create(65536 * 4)
|
||||
for v = 0, 65535 do
|
||||
local r, g, bl = v // 2048, (v // 32) % 64, v % 32
|
||||
r, g, bl = r * 8 + r // 4, g * 4 + g // 16, bl * 8 + bl // 4
|
||||
writeu32(b, v * 4, r + g * 256 + bl * 65536 + 0xFF000000)
|
||||
end
|
||||
lut565 = b
|
||||
return b
|
||||
end
|
||||
|
||||
-- copy of a picture's plane (for decoders that read the previous picture while writing the next)
|
||||
function Picture.clonePlane(p: Picture): buffer
|
||||
local src = (p.idx or p.rgba) :: buffer
|
||||
local c = buffer.create(buffer.len(src))
|
||||
buffer.copy(c, 0, src, 0)
|
||||
return c
|
||||
end
|
||||
|
||||
local _ = readu32
|
||||
|
||||
return Picture
|
||||
@@ -0,0 +1,276 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/QtRle: QuickTime Animation ('rle ') frames, depths 1, 2, 4, 8 (palettised; 33..40 = grey) and
|
||||
-- 16 (X-R-G-B 1-5-5-5), 24 (R, G, B), 32 (A, R, G, B). Written from the format description (Apple QuickTime File
|
||||
-- Format; MultimediaWiki "Apple QuickTime RLE"): a chunk size, a header (0x0008: start line + line count), then per
|
||||
-- line a skip byte and run codes until -1 (0: another skip; < 0: one pixel group repeated; > 0: literal groups).
|
||||
-- Groups are 4 pixels at 8 bits, 8 at 4 bits, 16 at 2 bits, 1 pixel at 16 / 24 / 32 bits; the 1-bit layout has its
|
||||
-- own (skip, code) pairs where a skip with bit 7 set moves to the next line. Paints over the previous picture; frames
|
||||
-- shorter than 8 bytes repeat it. Bit-exact with ffmpeg's qtrle decoder on valid streams. Never throws: every read
|
||||
-- and write is range-checked (a damaged frame stops where the damage is).
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local QtRle = {}
|
||||
|
||||
local function s8(v: number): number
|
||||
return if v >= 128 then v - 256 else v
|
||||
end
|
||||
|
||||
-- 1-bit: (skip, code) pairs; skip bit 7 = next line (the first one starts the first line)
|
||||
local function decode1(px: buffer, stride: number, limit: number, d: buffer, p: number, n: number, row: number, lines: number)
|
||||
row -= stride
|
||||
local q = row
|
||||
lines += 1
|
||||
while lines > 0 do
|
||||
if p + 2 > n then
|
||||
return
|
||||
end
|
||||
local skip = readu8(d, p)
|
||||
local code = s8(readu8(d, p + 1))
|
||||
p += 2
|
||||
if code == 0 then
|
||||
return
|
||||
end
|
||||
if skip >= 128 then
|
||||
lines -= 1
|
||||
row += stride
|
||||
q = row + 16 * (skip - 128)
|
||||
else
|
||||
q += 16 * skip
|
||||
end
|
||||
if q < 0 or q > limit then
|
||||
return
|
||||
end
|
||||
if code ~= -1 then
|
||||
if code < 0 then
|
||||
local cnt = -code
|
||||
if p + 2 > n or q + cnt * 16 > limit then
|
||||
return
|
||||
end
|
||||
local a, b = readu8(d, p), readu8(d, p + 1)
|
||||
p += 2
|
||||
for _ = 1, cnt do
|
||||
for k = 7, 0, -1 do
|
||||
writeu8(px, q, band(rshift(a, k), 1))
|
||||
q += 1
|
||||
end
|
||||
for k = 7, 0, -1 do
|
||||
writeu8(px, q, band(rshift(b, k), 1))
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
else
|
||||
local cnt = code * 2
|
||||
if p + cnt > n or q + cnt * 8 > limit then
|
||||
return
|
||||
end
|
||||
for _ = 1, cnt do
|
||||
local a = readu8(d, p)
|
||||
p += 1
|
||||
for k = 7, 0, -1 do
|
||||
writeu8(px, q, band(rshift(a, k), 1))
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 2 / 4 / 8-bit: groups of 16 / 8 / 4 indexes (4 bytes); skips count groups
|
||||
local function decodeIdx(px: buffer, stride: number, limit: number, d: buffer, p: number, n: number, row: number, lines: number, bpp: number)
|
||||
local group = 32 // bpp -- pixels per 4 bytes
|
||||
local per = 8 // bpp -- pixels per byte
|
||||
local mask = 2 ^ bpp - 1
|
||||
for _ = 1, lines do
|
||||
if p >= n then
|
||||
return
|
||||
end
|
||||
local q = row + group * (readu8(d, p) - 1)
|
||||
p += 1
|
||||
if q < 0 or q > limit then
|
||||
return
|
||||
end
|
||||
while true do
|
||||
if p >= n then
|
||||
return
|
||||
end
|
||||
local code = s8(readu8(d, p))
|
||||
p += 1
|
||||
if code == -1 then
|
||||
break
|
||||
end
|
||||
if p >= n then
|
||||
return
|
||||
end
|
||||
if code == 0 then
|
||||
q += group * (readu8(d, p) - 1)
|
||||
p += 1
|
||||
if q < 0 or q > limit then
|
||||
return
|
||||
end
|
||||
elseif code < 0 then
|
||||
local cnt = -code
|
||||
if p + 4 > n or q + cnt * group > limit then
|
||||
return
|
||||
end
|
||||
if bpp == 8 then
|
||||
local v = readu32(d, p)
|
||||
for _ = 1, cnt do
|
||||
writeu32(px, q, v)
|
||||
q += 4
|
||||
end
|
||||
else
|
||||
local v0 = q
|
||||
for b = 0, 3 do
|
||||
local byte = readu8(d, p + b)
|
||||
for k = per - 1, 0, -1 do
|
||||
writeu8(px, q, band(rshift(byte, k * bpp), mask))
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
for _ = 2, cnt do
|
||||
buffer.copy(px, q, px, v0, group)
|
||||
q += group
|
||||
end
|
||||
end
|
||||
p += 4
|
||||
else
|
||||
local bytes = code * 4
|
||||
if p + bytes > n or q + bytes * per > limit then
|
||||
return
|
||||
end
|
||||
if bpp == 8 then
|
||||
buffer.copy(px, q, d, p, bytes)
|
||||
q += bytes
|
||||
else
|
||||
for b = 0, bytes - 1 do
|
||||
local byte = readu8(d, p + b)
|
||||
for k = per - 1, 0, -1 do
|
||||
writeu8(px, q, band(rshift(byte, k * bpp), mask))
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
p += bytes
|
||||
end
|
||||
end
|
||||
row += stride
|
||||
end
|
||||
end
|
||||
|
||||
-- 16 / 24 / 32-bit: one pixel per group, into the rgba8 plane (stride in pixels)
|
||||
local function decodeRgb(px: buffer, stride: number, limit: number, d: buffer, p: number, n: number, row: number, lines: number, depth: number)
|
||||
local bpp = depth // 8
|
||||
local lut = if depth == 16 then Picture.rgb555() else nil
|
||||
for _ = 1, lines do
|
||||
if p >= n then
|
||||
return
|
||||
end
|
||||
local q = row + (readu8(d, p) - 1)
|
||||
p += 1
|
||||
if q < 0 or q > limit then
|
||||
return
|
||||
end
|
||||
while true do
|
||||
if p >= n then
|
||||
return
|
||||
end
|
||||
local code = s8(readu8(d, p))
|
||||
p += 1
|
||||
if code == -1 then
|
||||
break
|
||||
end
|
||||
if p >= n then
|
||||
return
|
||||
end
|
||||
if code == 0 then
|
||||
q += readu8(d, p) - 1
|
||||
p += 1
|
||||
if q < 0 or q > limit then
|
||||
return
|
||||
end
|
||||
else
|
||||
local cnt = if code < 0 then -code else code
|
||||
local need = if code < 0 then bpp else bpp * cnt
|
||||
if p + need > n or q + cnt > limit then
|
||||
return
|
||||
end
|
||||
for _ = 1, cnt do
|
||||
local v
|
||||
if lut then
|
||||
v = readu32(lut, (readu8(d, p) * 256 + readu8(d, p + 1)) % 32768 * 4)
|
||||
elseif bpp == 3 then
|
||||
v = readu8(d, p) + readu8(d, p + 1) * 256 + readu8(d, p + 2) * 65536 + 0xFF000000
|
||||
else
|
||||
v = readu8(d, p + 1) + readu8(d, p + 2) * 256 + readu8(d, p + 3) * 65536 + readu8(d, p) * 16777216
|
||||
end
|
||||
writeu32(px, q * 4, v)
|
||||
q += 1
|
||||
if code > 0 then
|
||||
p += bpp
|
||||
end
|
||||
end
|
||||
if code < 0 then
|
||||
p += bpp
|
||||
end
|
||||
end
|
||||
end
|
||||
row += stride
|
||||
end
|
||||
end
|
||||
|
||||
-- decode(pic, data, depth) -> true (a picture, possibly the previous one again) | false, message
|
||||
function QtRle.decode(pic: Picture.Picture, d: buffer, depth: number): (boolean, string?)
|
||||
local n = buffer.len(d)
|
||||
if n < 8 then
|
||||
return true, nil -- no change
|
||||
end
|
||||
local p = 4
|
||||
local header = readu8(d, p) * 256 + readu8(d, p + 1)
|
||||
p += 2
|
||||
local h = pic.h
|
||||
local start, lines = 0, h
|
||||
if band(header, 8) ~= 0 then
|
||||
if n < 14 then
|
||||
return true, nil
|
||||
end
|
||||
start = readu8(d, p) * 256 + readu8(d, p + 1)
|
||||
lines = readu8(d, p + 4) * 256 + readu8(d, p + 5)
|
||||
p += 8
|
||||
if lines > h - start then
|
||||
return true, nil
|
||||
end
|
||||
end
|
||||
local stride = pic.stride
|
||||
local bits = if depth > 32 then depth - 32 else depth
|
||||
local idx, rgba = pic.idx, pic.rgba
|
||||
if idx and bits <= 8 then
|
||||
local limit = stride * h
|
||||
if bits == 1 then
|
||||
decode1(idx, stride, limit, d, p, n, start * stride, lines)
|
||||
else
|
||||
decodeIdx(idx, stride, limit, d, p, n, start * stride, lines, bits)
|
||||
end
|
||||
elseif rgba and (bits == 16 or bits == 24 or bits == 32) then
|
||||
decodeRgb(rgba, stride, stride * h, d, p, n, start * stride, lines, bits)
|
||||
else
|
||||
return false, "unsupported: depth " .. depth
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- palettised(depth) -> whether the depth uses a palette; pad(depth) -> the row padding the plane needs
|
||||
function QtRle.palettised(depth: number): boolean
|
||||
return depth <= 8 or depth > 32
|
||||
end
|
||||
function QtRle.pad(depth: number): number
|
||||
local bits = if depth > 32 then depth - 32 else depth
|
||||
return if bits <= 8 then 32 // math.max(1, bits) else 1
|
||||
end
|
||||
|
||||
return QtRle
|
||||
@@ -0,0 +1,405 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Smacker: RAD Game Tools' Smacker video (SMK2 / SMK4 frames; the file format is video/smk) and
|
||||
-- the Huffman trees Smacker audio shares. Written from the format description (MultimediaWiki "Smacker"). Bits are
|
||||
-- read LSB first. A tree is a pre-order bit string (1 = node: left subtree then right; 0 = leaf with its value);
|
||||
-- an 8-bit tree's leaves hold bytes, a "big" tree's leaves hold 16-bit values made of a low-byte and a high-byte
|
||||
-- 8-bit tree, three of them being escape leaves that return the three most recently decoded values instead (a
|
||||
-- per-frame cache, reset to 0 at each frame). The four big trees (MMAP, MCLR, FULL, TYPE) come from the file header;
|
||||
-- a frame is 4x4 blocks in raster order, each TYPE code giving a block type and a run length: MONO (two colours and a
|
||||
-- 16-bit mask), FULL (16 pixels as 8 codes; SMK4 adds a 2x2-doubled and a 2x1-doubled mode), SKIP, FILL (one colour).
|
||||
-- Trees are flat arrays (a node stores the size of its left subtree, as a negative number); the frame decoder reads
|
||||
-- 10 bits at a time through a lookup table into them.
|
||||
-- Bit-exact with ffmpeg's smackvid decoder. Never throws: trees are bounded (depth, entries), reads past the data give
|
||||
-- zero bits, a frame whose codes run out of bits gives no picture.
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local Smacker = {}
|
||||
|
||||
local MAX_DEPTH8 = 27 -- 8-bit trees (3 x 9-bit lookups in the reference decoder)
|
||||
local MAX_DEPTH16 = 500
|
||||
|
||||
-- block runs (TYPE code bits 2..7)
|
||||
local RUNS = table.create(64, 0)
|
||||
for i = 1, 59 do
|
||||
RUNS[i] = i
|
||||
end
|
||||
RUNS[60], RUNS[61], RUNS[62], RUNS[63], RUNS[64] = 128, 256, 512, 1024, 2048
|
||||
|
||||
-- a bit position over buf[0, nbits / 8)
|
||||
export type Bits = { buf: buffer, pos: number, nbits: number }
|
||||
|
||||
function Smacker.bits(buf: buffer, offset: number, length: number): Bits
|
||||
return { buf = buf, pos = offset * 8, nbits = (offset + length) * 8 }
|
||||
end
|
||||
|
||||
local function bit(br: Bits): number
|
||||
local p = br.pos
|
||||
br.pos = p + 1
|
||||
if p >= br.nbits then
|
||||
return 0
|
||||
end
|
||||
return band(rshift(readu8(br.buf, rshift(p, 3)), band(p, 7)), 1)
|
||||
end
|
||||
Smacker.bit = bit
|
||||
|
||||
-- read(br, n) -> n bits (LSB first; zeros past the end)
|
||||
function Smacker.read(br: Bits, n: number): number
|
||||
local v, m = 0, 1
|
||||
for _ = 1, n do
|
||||
v += bit(br) * m
|
||||
m *= 2
|
||||
end
|
||||
return v
|
||||
end
|
||||
local readBits = Smacker.read
|
||||
|
||||
-- walk(tree, br) -> the leaf value (no end-of-data check: 8-bit trees, as the reference's table lookups)
|
||||
local function walk(tree: { number }, br: Bits): number
|
||||
local i = 1
|
||||
local e = tree[1]
|
||||
while e < 0 do
|
||||
if bit(br) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
i += 1
|
||||
e = tree[i]
|
||||
end
|
||||
return e
|
||||
end
|
||||
Smacker.walk = walk
|
||||
|
||||
-- the pre-order tree reader: leaf(index) -> value | nil, message; returns the flat tree (1-based)
|
||||
local function build(br: Bits, maxDepth: number, maxEntries: number, leaf: (number) -> (number?, string?), bigChecks: boolean): ({ number }?, string?)
|
||||
local vals: { number } = {}
|
||||
local n = 0
|
||||
local stack: { number } = {}
|
||||
local state: { number } = {}
|
||||
local sp = 0
|
||||
while true do
|
||||
if sp > maxDepth then
|
||||
return nil, "tree too deep"
|
||||
end
|
||||
if n >= maxEntries then
|
||||
return nil, "tree too big"
|
||||
end
|
||||
if bigChecks and br.pos >= br.nbits then
|
||||
return nil, "truncated tree"
|
||||
end
|
||||
if bit(br) == 1 then
|
||||
n += 1
|
||||
vals[n] = -1
|
||||
sp += 1
|
||||
stack[sp], state[sp] = n, 0
|
||||
else
|
||||
local v, msg = leaf(n)
|
||||
if not v then
|
||||
return nil, msg
|
||||
end
|
||||
n += 1
|
||||
vals[n] = v
|
||||
local more = false
|
||||
while sp > 0 do
|
||||
if state[sp] == 0 then
|
||||
local at = stack[sp]
|
||||
vals[at] = -(n - at) - 1 -- the left subtree holds n - at entries
|
||||
state[sp] = 1
|
||||
more = true
|
||||
break
|
||||
end
|
||||
sp -= 1
|
||||
end
|
||||
if not more then
|
||||
return vals, nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- tree8(br) -> an 8-bit tree (leaves are bytes) | nil, message
|
||||
function Smacker.tree8(br: Bits): ({ number }?, string?)
|
||||
local leaves = 0
|
||||
return build(br, MAX_DEPTH8, 511, function(_: number): (number?, string?)
|
||||
if leaves >= 256 then
|
||||
return nil, "tree too big"
|
||||
end
|
||||
leaves += 1
|
||||
if br.nbits - br.pos < 8 then
|
||||
return nil, "truncated tree"
|
||||
end
|
||||
return readBits(br, 8), nil
|
||||
end, false)
|
||||
end
|
||||
local tree8 = Smacker.tree8
|
||||
|
||||
export type BigTree = { vals: { number }, last: { number }, lut: buffer? } -- last: the escape leaves' indexes (1-based)
|
||||
|
||||
-- bigTree(br, size) -> one of the header's trees | nil, message (size = its byte size from the header: bounds entries)
|
||||
local function bigTree(br: Bits, size: number): (BigTree?, string?)
|
||||
if bit(br) == 0 then
|
||||
return { vals = { 0, 0 }, last = { 2, 2, 2 } }, nil -- no tree: every code is 0
|
||||
end
|
||||
local lohi: { { number } } = {}
|
||||
for k = 1, 2 do
|
||||
if bit(br) == 0 then
|
||||
lohi[k] = { 0 }
|
||||
else
|
||||
local t, msg = tree8(br)
|
||||
if not t then
|
||||
return nil, msg
|
||||
end
|
||||
bit(br)
|
||||
lohi[k] = t
|
||||
end
|
||||
end
|
||||
local esc = { readBits(br, 16), readBits(br, 16), readBits(br, 16) }
|
||||
local last = { -1, -1, -1 }
|
||||
local lo, hi = lohi[1], lohi[2]
|
||||
local vals, msg = build(br, MAX_DEPTH16, (size + 3) // 4, function(index: number): (number?, string?)
|
||||
local v = walk(lo, br) + walk(hi, br) * 256
|
||||
for k = 1, 3 do
|
||||
if v == esc[k] then
|
||||
last[k] = index + 1
|
||||
return 0, nil
|
||||
end
|
||||
end
|
||||
return v, nil
|
||||
end, true)
|
||||
if not vals then
|
||||
return nil, msg
|
||||
end
|
||||
bit(br)
|
||||
for k = 1, 3 do
|
||||
if last[k] == -1 then
|
||||
table.insert(vals, 0)
|
||||
last[k] = #vals
|
||||
end
|
||||
end
|
||||
return { vals = vals, last = last }, nil
|
||||
end
|
||||
|
||||
export type Trees = { mmap: BigTree, mclr: BigTree, full: BigTree, type: BigTree }
|
||||
|
||||
-- trees(config) -> the four header trees | nil, message. config = the four u32 tree sizes then the tree bits
|
||||
function Smacker.trees(config: buffer): (Trees?, string?)
|
||||
local n = buffer.len(config)
|
||||
if n <= 16 then
|
||||
return nil, "no trees"
|
||||
end
|
||||
local br = Smacker.bits(config, 16, n - 16)
|
||||
local out: { BigTree } = {}
|
||||
local present = 0
|
||||
for k = 1, 4 do
|
||||
local size = buffer.readu32(config, (k - 1) * 4)
|
||||
if size >= 2 ^ 28 then
|
||||
return nil, "tree size too large"
|
||||
end
|
||||
local here = br.pos < br.nbits and band(rshift(readu8(config, rshift(br.pos, 3)), band(br.pos, 7)), 1) == 1
|
||||
local t, msg = bigTree(br, size)
|
||||
if not t then
|
||||
return nil, msg
|
||||
end
|
||||
if here then
|
||||
present += 1
|
||||
end
|
||||
out[k] = t
|
||||
end
|
||||
if present == 0 or br.pos > br.nbits then
|
||||
return nil, "bad trees"
|
||||
end
|
||||
return { mmap = out[1], mclr = out[2], full = out[3], type = out[4] }, nil
|
||||
end
|
||||
|
||||
local LUT_BITS = 10
|
||||
|
||||
-- the first LUT_BITS bits of a code -> (entry index * 32 + bits used): a leaf, or the node reached after LUT_BITS bits
|
||||
local function lut(vals: { number }): buffer
|
||||
local size = 2 ^ LUT_BITS
|
||||
local t = buffer.create(size * 4)
|
||||
for pat = 0, size - 1 do
|
||||
local i, len = 1, 0
|
||||
local e = vals[1]
|
||||
while e < 0 and len < LUT_BITS do
|
||||
if band(rshift(pat, len), 1) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
i += 1
|
||||
len += 1
|
||||
e = vals[i]
|
||||
end
|
||||
writeu32(t, pat * 4, i * 32 + len)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function reset(t: BigTree)
|
||||
local vals, last = t.vals, t.last
|
||||
vals[last[1]], vals[last[2]], vals[last[3]] = 0, 0, 0
|
||||
end
|
||||
|
||||
-- decode(pic, data, trees, v4) -> true | false, message. pic: palettised, padded to blocks of 4
|
||||
function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boolean): (boolean, string?)
|
||||
local px = pic.idx :: buffer
|
||||
local stride = pic.stride
|
||||
local mmap, mclr, full, typ = trees.mmap, trees.mclr, trees.full, trees.type
|
||||
for _, t in { mmap, mclr, full, typ } do
|
||||
reset(t)
|
||||
if not t.lut then
|
||||
t.lut = lut(t.vals)
|
||||
end
|
||||
end
|
||||
local n = buffer.len(d)
|
||||
local nbits = n * 8
|
||||
local pos = 0
|
||||
-- the next value of a tree (updating its recent-value cache) | -1 when the bits run out inside the tree
|
||||
local function code(t: BigTree): number
|
||||
local vals = t.vals
|
||||
local i, p
|
||||
local byte = rshift(pos, 3)
|
||||
if byte + 3 <= n then
|
||||
local v = band(rshift(readu8(d, byte) + readu8(d, byte + 1) * 256 + readu8(d, byte + 2) * 65536, band(pos, 7)), 1023)
|
||||
local ent = readu32(t.lut :: buffer, v * 4)
|
||||
i = rshift(ent, 5)
|
||||
p = pos + band(ent, 31)
|
||||
else
|
||||
i, p = 1, pos
|
||||
end
|
||||
local e = vals[i]
|
||||
while e < 0 do
|
||||
if p >= nbits then
|
||||
return -1
|
||||
end
|
||||
if band(rshift(readu8(d, rshift(p, 3)), band(p, 7)), 1) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
p += 1
|
||||
i += 1
|
||||
e = vals[i]
|
||||
end
|
||||
pos = p
|
||||
local last = t.last
|
||||
local l0 = last[1]
|
||||
if e ~= vals[l0] then
|
||||
local l1 = last[2]
|
||||
vals[last[3]] = vals[l1]
|
||||
vals[l1] = vals[l0]
|
||||
vals[l0] = e
|
||||
end
|
||||
return e
|
||||
end
|
||||
local function bit(): number
|
||||
local p = pos
|
||||
pos = p + 1
|
||||
if p >= nbits then
|
||||
return 0
|
||||
end
|
||||
return band(rshift(readu8(d, rshift(p, 3)), band(p, 7)), 1)
|
||||
end
|
||||
local bw, bh = pic.w // 4, pic.h // 4
|
||||
local blocks = bw * bh
|
||||
local blk = 0
|
||||
while blk < blocks do
|
||||
local ty = code(typ)
|
||||
if ty < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local run = RUNS[band(rshift(ty, 2), 63) + 1]
|
||||
local kind = band(ty, 3)
|
||||
if kind == 2 then
|
||||
blk = math.min(blocks, blk + run)
|
||||
elseif kind == 3 then
|
||||
local v = rshift(ty, 8) * 0x01010101
|
||||
for _ = 1, run do
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local o = (blk // bw) * stride * 4 + (blk % bw) * 4
|
||||
writeu32(px, o, v)
|
||||
writeu32(px, o + stride, v)
|
||||
writeu32(px, o + 2 * stride, v)
|
||||
writeu32(px, o + 3 * stride, v)
|
||||
blk += 1
|
||||
end
|
||||
elseif kind == 0 then
|
||||
for _ = 1, run do
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local clr = code(mclr)
|
||||
local map = code(mmap)
|
||||
if clr < 0 or map < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local hi, lo = rshift(clr, 8), band(clr, 255)
|
||||
local o = (blk // bw) * stride * 4 + (blk % bw) * 4
|
||||
for r = 0, 3 do
|
||||
local q = o + r * stride
|
||||
writeu8(px, q, if band(map, 1) ~= 0 then hi else lo)
|
||||
writeu8(px, q + 1, if band(map, 2) ~= 0 then hi else lo)
|
||||
writeu8(px, q + 2, if band(map, 4) ~= 0 then hi else lo)
|
||||
writeu8(px, q + 3, if band(map, 8) ~= 0 then hi else lo)
|
||||
map = rshift(map, 4)
|
||||
end
|
||||
blk += 1
|
||||
end
|
||||
else
|
||||
local mode = 0
|
||||
if v4 then
|
||||
if pos >= nbits then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
if bit() == 1 then
|
||||
mode = 1
|
||||
elseif bit() == 1 then
|
||||
mode = 2
|
||||
end
|
||||
end
|
||||
for _ = 1, run do
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local o = (blk // bw) * stride * 4 + (blk % bw) * 4
|
||||
if mode == 0 then
|
||||
for r = 0, 3 do
|
||||
local a = code(full)
|
||||
local b = code(full)
|
||||
if a < 0 or b < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
-- the first code is the right pixel pair, the second the left one
|
||||
writeu32(px, o + r * stride, b + a * 65536)
|
||||
end
|
||||
elseif mode == 1 then
|
||||
for half = 0, 1 do
|
||||
local a = code(full)
|
||||
if a < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local v = band(a, 255) * 0x0101 + rshift(a, 8) * 0x01010000
|
||||
writeu32(px, o + half * 2 * stride, v)
|
||||
writeu32(px, o + (half * 2 + 1) * stride, v)
|
||||
end
|
||||
else
|
||||
for half = 0, 1 do
|
||||
local p2 = code(full)
|
||||
local p1 = code(full)
|
||||
if p1 < 0 or p2 < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local v = p1 + p2 * 65536
|
||||
writeu32(px, o + half * 2 * stride, v)
|
||||
writeu32(px, o + (half * 2 + 1) * stride, v)
|
||||
end
|
||||
end
|
||||
blk += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Smacker
|
||||
@@ -0,0 +1,183 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Smc: QuickTime Graphics ('smc ') frames, 8-bit palettised. Written from the format description
|
||||
-- (MultimediaWiki "Apple SMC"): 4x4 blocks in raster order, opcodes in the high nibble with a block count (low nibble
|
||||
-- + 1, or the next byte + 1 when bit 4 is set): skip, repeat the last block, repeat the last pair of blocks, 1-colour
|
||||
-- fill, 2 / 4 / 8-colour blocks (the colour groups go into 256-entry rotating tables, or come from them by index:
|
||||
-- the tables restart every frame) and 16-colour blocks. Paints over the previous picture. Bit-exact with ffmpeg's smc
|
||||
-- decoder (for widths whose rows are not padded: the previous block of a row start is the last block of the row
|
||||
-- above). Never throws: reads past the data give zeros, the block count bounds every write.
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, writeu8, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local Smc = {}
|
||||
|
||||
-- decode(pic, data, tables) -> true | false, message. tables = Smc.tables() (scratch, reused between frames)
|
||||
function Smc.decode(pic: Picture.Picture, d: buffer, tables: buffer): (boolean, string?)
|
||||
local px = pic.idx :: buffer
|
||||
local n = buffer.len(d)
|
||||
local stride = pic.stride -- a multiple of 4: the plane holds whole blocks
|
||||
local bh = pic.rows // 4
|
||||
local total = ((pic.w + 3) // 4) * ((pic.h + 3) // 4)
|
||||
if total // 1024 > n then
|
||||
return false, "frame too small"
|
||||
end
|
||||
local blocksWide = (pic.w + 3) // 4
|
||||
local p = 4 -- flags byte + 24-bit chunk size
|
||||
-- block position: bx, by (blocks)
|
||||
local bx, by = 0, 0
|
||||
local pairI, quadI, octI = 0, 0, 0
|
||||
-- tables: pairs at 0 (256 * 2), quads at 512 (256 * 4), octets at 1536 (256 * 8)
|
||||
local function u8(): number
|
||||
if p < n then
|
||||
local v = readu8(d, p)
|
||||
p += 1
|
||||
return v
|
||||
end
|
||||
return 0
|
||||
end
|
||||
local function advance()
|
||||
bx += 1
|
||||
if bx >= blocksWide then
|
||||
bx = 0
|
||||
by += 1
|
||||
end
|
||||
total -= 1
|
||||
end
|
||||
while total > 0 do
|
||||
if p >= n then
|
||||
return false, "truncated"
|
||||
end
|
||||
local op = readu8(d, p)
|
||||
p += 1
|
||||
local kind = rshift(op, 4)
|
||||
local count: number
|
||||
if kind < 8 then
|
||||
count = if band(op, 0x10) ~= 0 then 1 + u8() else 1 + band(op, 15)
|
||||
else
|
||||
count = band(op, 15) + 1
|
||||
end
|
||||
if kind <= 1 then
|
||||
for _ = 1, count do
|
||||
if total <= 0 then
|
||||
break
|
||||
end
|
||||
advance()
|
||||
end
|
||||
elseif kind <= 5 then
|
||||
-- repeat the last block (2, 3) or the last pair of blocks (4, 5)
|
||||
local pair = kind >= 4
|
||||
local back = if pair then 2 else 1
|
||||
local pos = by * blocksWide + bx
|
||||
if pos < back then
|
||||
return false, "repeat before any block"
|
||||
end
|
||||
local reps = if pair then count * 2 else count
|
||||
local srcA = pos - back
|
||||
local srcB = pos - 1
|
||||
for k = 0, reps - 1 do
|
||||
if total <= 0 or by >= bh then
|
||||
break
|
||||
end
|
||||
local s = if pair and k % 2 == 1 then srcB else srcA
|
||||
local sx, sy = (s % blocksWide) * 4, (s // blocksWide) * 4
|
||||
local o, so = by * 4 * stride + bx * 4, sy * stride + sx
|
||||
for r = 0, 3 do
|
||||
writeu32(px, o + r * stride, readu32(px, so + r * stride))
|
||||
end
|
||||
advance()
|
||||
end
|
||||
elseif kind <= 7 then
|
||||
local c = u8()
|
||||
local v = c * 0x01010101
|
||||
for _ = 1, count do
|
||||
if total <= 0 or by >= bh then
|
||||
break
|
||||
end
|
||||
local o = by * 4 * stride + bx * 4
|
||||
for r = 0, 3 do
|
||||
writeu32(px, o + r * stride, v)
|
||||
end
|
||||
advance()
|
||||
end
|
||||
elseif kind <= 13 then
|
||||
-- 2 / 4 / 8-colour blocks: a new colour group (even kinds) or one from the table (odd kinds)
|
||||
local size = if kind <= 9 then 2 elseif kind <= 11 then 4 else 8
|
||||
local base = if size == 2 then 0 elseif size == 4 then 512 else 1536
|
||||
local entry: number
|
||||
if kind % 2 == 0 then
|
||||
local i = if size == 2 then pairI elseif size == 4 then quadI else octI
|
||||
for k = 0, size - 1 do
|
||||
writeu8(tables, base + i * size + k, u8())
|
||||
end
|
||||
entry = i
|
||||
i = (i + 1) % 256
|
||||
if size == 2 then
|
||||
pairI = i
|
||||
elseif size == 4 then
|
||||
quadI = i
|
||||
else
|
||||
octI = i
|
||||
end
|
||||
else
|
||||
entry = u8()
|
||||
end
|
||||
local t = base + entry * size
|
||||
for _ = 1, count do
|
||||
if total <= 0 or by >= bh then
|
||||
break
|
||||
end
|
||||
local o = by * 4 * stride + bx * 4
|
||||
if size == 2 then
|
||||
local f = u8() * 256 + u8()
|
||||
for k = 0, 15 do
|
||||
local bit = band(rshift(f, 15 - k), 1)
|
||||
writeu8(px, o + (k // 4) * stride + k % 4, readu8(tables, t + bit))
|
||||
end
|
||||
elseif size == 4 then
|
||||
local f = u8() * 16777216 + u8() * 65536 + u8() * 256 + u8()
|
||||
for k = 0, 15 do
|
||||
local i = band(rshift(f, 30 - 2 * k), 3)
|
||||
writeu8(px, o + (k // 4) * stride + k % 4, readu8(tables, t + i))
|
||||
end
|
||||
else
|
||||
-- 6 bytes 01 23 45 67 89 AB -> rows 0-1 flags 012456, rows 2-3 flags 89A37B (3 bits per pixel)
|
||||
local v1 = u8() * 256 + u8()
|
||||
local v2 = u8() * 256 + u8()
|
||||
local v3 = u8() * 256 + u8()
|
||||
local fa = band(v1, 0xFFF0) * 256 + rshift(v2, 4)
|
||||
local fb = band(v3, 0xFFF0) * 256 + band(v1, 15) * 256 + band(v2, 15) * 16 + band(v3, 15)
|
||||
for k = 0, 15 do
|
||||
local f = if k < 8 then fa else fb
|
||||
local i = band(rshift(f, 21 - 3 * (k % 8)), 7)
|
||||
writeu8(px, o + (k // 4) * stride + k % 4, readu8(tables, t + i))
|
||||
end
|
||||
end
|
||||
advance()
|
||||
end
|
||||
else
|
||||
-- 16-colour blocks
|
||||
for _ = 1, count do
|
||||
if total <= 0 or by >= bh then
|
||||
break
|
||||
end
|
||||
local o = by * 4 * stride + bx * 4
|
||||
for k = 0, 15 do
|
||||
writeu8(px, o + (k // 4) * stride + k % 4, u8())
|
||||
end
|
||||
advance()
|
||||
end
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- tables() -> the colour group scratch (pairs, quads, octets)
|
||||
function Smc.tables(): buffer
|
||||
return buffer.create(512 + 1024 + 2048)
|
||||
end
|
||||
|
||||
return Smc
|
||||
@@ -0,0 +1,214 @@
|
||||
--!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), QuickTime Graphics ("smc", 'smc ') and Smacker
|
||||
-- video ("smackvid", the frames of video/smk: track.config = the header's tree sizes and trees, codecTag SMK2 / SMK4).
|
||||
-- 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", "smackvid" }
|
||||
--
|
||||
-- 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 Smacker = require("@formats/codecs/video/Legacy/Smacker")
|
||||
|
||||
local Legacy = {}
|
||||
|
||||
Legacy.Picture = Picture
|
||||
Legacy.Flic = Flic
|
||||
Legacy.Cinepak = Cinepak
|
||||
Legacy.Msvideo1 = Msvideo1
|
||||
Legacy.QtRle = QtRle
|
||||
Legacy.Smc = Smc
|
||||
Legacy.Smacker = Smacker
|
||||
Legacy.CODECS = { "flic", "cinepak", "msvideo1", "qtrle", "smc", "smackvid" }
|
||||
|
||||
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,
|
||||
},
|
||||
smackvid = {
|
||||
make = function(t)
|
||||
local cfg = t.config
|
||||
if type(cfg) ~= "buffer" then
|
||||
return nil, "no Smacker trees"
|
||||
end
|
||||
local trees, msg = Smacker.trees(cfg)
|
||||
if not trees then
|
||||
return nil, msg
|
||||
end
|
||||
return { pic = Picture.new(t.width :: number, t.height :: number, true, 4, 4, t.palette), trees = trees, v4 = t.codecTag == "SMK4" }, nil
|
||||
end,
|
||||
step = function(st, data, packet)
|
||||
packetPalette(st.pic, packet)
|
||||
if buffer.len(data) == 0 then
|
||||
return nil, fail("empty frame")
|
||||
end
|
||||
local ok, msg = Smacker.decode(st.pic, data, st.trees, st.v4)
|
||||
if not ok then
|
||||
return nil, fail(msg)
|
||||
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
|
||||
@@ -260,6 +260,13 @@ local function theoraDecoder(track: Types.Track, opts: any?): (Types.VideoCodecD
|
||||
return Theora.newVideoDecoder(track, opts)
|
||||
end
|
||||
|
||||
-- FLI / FLC, Cinepak, Microsoft Video 1, QuickTime RLE / SMC, Smacker (codecs/video/Legacy)
|
||||
local function legacyDecoder(id: string): (track: Types.Track, opts: any?) -> (Types.VideoCodecDecoder?, Err.Err?)
|
||||
return function(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
return require("@formats/codecs/video/Legacy").newDecoder(id, track, opts)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ registry
|
||||
-- the codecs known by name (decoders where `new` is set). FourCCs compare case-sensitively first, then upper-cased.
|
||||
local BUILTIN: { Entry } = {
|
||||
@@ -275,6 +282,13 @@ local BUILTIN: { Entry } = {
|
||||
{ id = "av1", name = "AV1", fourcc = { "AV01", "av01" }, matroska = { "V_AV1" } },
|
||||
{ id = "theora", name = "Theora", matroska = { "V_THEORA" }, new = theoraDecoder },
|
||||
{ id = "png", name = "PNG", fourcc = { "png ", "MPNG" }, matroska = { "V_PNG" } },
|
||||
-- legacy inter-frame codecs (codecs/video/Legacy; rgba8 pictures, palettes from track.palette)
|
||||
{ id = "cinepak", name = "Cinepak", fourcc = { "cvid" }, new = legacyDecoder("cinepak") },
|
||||
{ id = "msvideo1", name = "Microsoft Video 1", fourcc = { "CRAM", "MSVC", "WHAM" }, new = legacyDecoder("msvideo1") },
|
||||
{ id = "qtrle", name = "QuickTime Animation (RLE)", fourcc = { "rle " }, new = legacyDecoder("qtrle") },
|
||||
{ id = "smc", name = "QuickTime Graphics (SMC)", fourcc = { "smc " }, new = legacyDecoder("smc") },
|
||||
{ id = "flic", name = "Autodesk Animator FLIC", fourcc = { "AFLC" }, new = legacyDecoder("flic") },
|
||||
{ id = "smackvid", name = "Smacker video", fourcc = { "SMK2", "SMK4" }, new = legacyDecoder("smackvid") },
|
||||
}
|
||||
|
||||
local byId: { [string]: Entry } = {}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
-- avi.streams[i] = { type = "vids" | "auds" | ..., handler, scale, rate, start, length, sampleSize, strf
|
||||
-- (buffer), name, chunks = { { offset, size, key } } } -- offsets of the chunk data, absolute
|
||||
-- local w = Avi.writer({ streams = {...}, avih = {...} }); w:chunk(streamIndex, data, key); local file = w:finish()
|
||||
-- local bi = Avi.bitmapInfo(strf) -- a BITMAPINFOHEADER: size, depth, FourCC, palette (also Matroska VFW)
|
||||
--
|
||||
-- Never throws: malformed data returns nil, Err (a damaged index falls back to scanning).
|
||||
local Err = require("@formats/core/Err")
|
||||
@@ -520,4 +521,50 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer
|
||||
return w:finish()
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ BITMAPINFO
|
||||
export type BitmapInfo = {
|
||||
width: number,
|
||||
height: number, -- signed: > 0 = bottom-up rows
|
||||
bits: number, -- biBitCount
|
||||
compression: string, -- biCompression as a FourCC ("\0\0\0\0" = BI_RGB)
|
||||
palette: buffer?, -- bits <= 8: 256 u32 entries, bytes R, G, B, 0xFF (codecs/image/Pixels layout); unused ones 0
|
||||
extra: buffer?, -- the bytes after the 40-byte header (palette included), nil when none
|
||||
}
|
||||
|
||||
-- bitmapInfo(f) -> the BITMAPINFOHEADER facts of an AVI video strf (or a Matroska V_MS/VFW/FOURCC CodecPrivate) |
|
||||
-- nil when shorter than 40 bytes. The palette is the last (2^bits) * 4 bytes after the header (B, G, R, x entries;
|
||||
-- a trailing "BottomUp" tag is skipped), as the VfW codecs store it.
|
||||
function Avi.bitmapInfo(f: buffer): BitmapInfo?
|
||||
local n = buffer.len(f)
|
||||
if n < 40 then
|
||||
return nil
|
||||
end
|
||||
local bits = readu16(f, 14)
|
||||
local info: BitmapInfo = {
|
||||
width = buffer.readi32(f, 4),
|
||||
height = buffer.readi32(f, 8),
|
||||
bits = bits,
|
||||
compression = readstring(f, 16, 4),
|
||||
}
|
||||
if n > 40 then
|
||||
local e = buffer.create(n - 40)
|
||||
buffer.copy(e, 0, f, 40)
|
||||
info.extra = e
|
||||
if bits >= 1 and bits <= 8 then
|
||||
local count = math.min(2 ^ bits, (n - 40) // 4)
|
||||
local at = n - count * 4
|
||||
if at - 40 >= 9 and readstring(f, n - 9, 9) == "BottomUp\0" then
|
||||
at -= 9
|
||||
end
|
||||
local pal = buffer.create(1024)
|
||||
for i = 0, count - 1 do
|
||||
local o = at + i * 4
|
||||
buffer.writeu32(pal, i * 4, readu8(f, o + 2) + readu8(f, o + 1) * 256 + readu8(f, o) * 65536 + 0xFF000000)
|
||||
end
|
||||
info.palette = pal
|
||||
end
|
||||
end
|
||||
return info
|
||||
end
|
||||
|
||||
return Avi
|
||||
@@ -8,9 +8,14 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Box = require("@formats/containers/IsoBmff/Box")
|
||||
local QtPalette = require("@formats/containers/IsoBmff/QtPalette")
|
||||
|
||||
local readstring = buffer.readstring
|
||||
|
||||
local function readu16be(b: buffer, o: number): number
|
||||
return buffer.readu8(b, o) * 256 + buffer.readu8(b, o + 1)
|
||||
end
|
||||
|
||||
local Demux = {}
|
||||
|
||||
export type Edit = { duration: number, mediaTime: number, rate: number } -- duration in movie ticks, mediaTime in media ticks (-1 = empty)
|
||||
@@ -33,6 +38,8 @@ export type Track = {
|
||||
channels: number?,
|
||||
bitsPerSample: number?,
|
||||
config: buffer?, -- codec configuration (AudioSpecificConfig, ALAC cookie, dOps, avcC, ...)
|
||||
bitsPerPixel: number?, -- video: the sample description's depth (24, 32, 40 = 8-bit grey, ...)
|
||||
palette: buffer?, -- video, 1..8-bit depths: 256 u32 entries (bytes R, G, B, 0xFF)
|
||||
objectType: number?, -- esds objectTypeIndication
|
||||
pcm: { bits: number, float: boolean, little: boolean }?,
|
||||
entryBoxes: { Box.Box }, -- the sample entry's child boxes (esds, btrt, colr, pasp, ...)
|
||||
@@ -194,6 +201,13 @@ local function sampleEntry(b: buffer, e: Box.Box, handler: string, t: Track)
|
||||
t.height = r:u16be()
|
||||
r:skip(4 + 4 + 4 + 2 + 32 + 2 + 2)
|
||||
childStart = r.pos
|
||||
-- QuickTime depth and colour table (palettised codecs: QuickTime RLE / SMC, Cinepak, ...)
|
||||
local depth, pal = QtPalette.read(b, e.start, e.stop, if typ == "cvid" then "cinepak" else typ)
|
||||
t.bitsPerPixel = depth
|
||||
t.palette = pal
|
||||
if pal and not r.err and readu16be(b, childStart - 2) == 0 and childStart + 8 <= e.stop then
|
||||
childStart = math.min(e.stop, childStart + 8 + (readu16be(b, childStart + 6) + 1) * 8)
|
||||
end
|
||||
else
|
||||
childStart = r.pos
|
||||
end
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
--!strict
|
||||
-- containers/IsoBmff/QtPalette: the depth and colour table of a QuickTime video sample description (Apple QuickTime
|
||||
-- File Format, "Video sample description": depth at byte 82, color table ID at 84, then the table when the ID is 0).
|
||||
-- Used by the MOV demuxer and by Matroska's V_QUICKTIME tracks (whose CodecPrivate is the same description). Depths 1,
|
||||
-- 2, 4 and 8 are palettised: the colour table from the description, the Macintosh default table (ID -1), or a grey
|
||||
-- ramp (depths 33..40: the 0x20 bit marks grey). Written from the QuickTime documentation; the default tables are the
|
||||
-- Macintosh system palettes (white first, a 6x6x6 cube, then red / green / blue / grey ramps).
|
||||
--
|
||||
-- local depth, palette = QtPalette.read(buf, entryStart, entryStop, codec)
|
||||
-- -- depth: the stored depth (24, 32, 40, ...), palette: 256 u32 entries (bytes R, G, B, 0xFF) | nil
|
||||
--
|
||||
-- Never throws.
|
||||
local readu8, writeu32 = buffer.readu8, buffer.writeu32
|
||||
|
||||
local function readu16(b: buffer, o: number): number -- big-endian
|
||||
return readu8(b, o) * 256 + readu8(b, o + 1)
|
||||
end
|
||||
local function readu32(b: buffer, o: number): number
|
||||
return readu16(b, o) * 65536 + readu16(b, o + 2)
|
||||
end
|
||||
|
||||
local QtPalette = {}
|
||||
|
||||
local function rgb(r: number, g: number, b: number): number
|
||||
return r + g * 256 + b * 65536 + 0xFF000000
|
||||
end
|
||||
|
||||
-- the Macintosh default colour tables: 2 / 4 / 16 entries (System 7 control panel), 256 (the system palette)
|
||||
local SMALL: { [number]: { number } } = {
|
||||
[1] = { 0xFFFFFF, 0x000000 },
|
||||
[2] = { 0xFFFFFF, 0xACACAC, 0x555555, 0x000000 },
|
||||
[4] = {
|
||||
0xFFFFFF, 0xFCF305, 0xFF6402, 0xDD0806, 0xF20884, 0x4600A5, 0x0000D4, 0x02ABEA,
|
||||
0x1FB714, 0x006411, 0x562C05, 0x90713A, 0xC0C0C0, 0x808080, 0x404040, 0x000000,
|
||||
},
|
||||
}
|
||||
|
||||
local function defaultTable(bits: number): buffer
|
||||
local pal = buffer.create(1024)
|
||||
local small = SMALL[bits]
|
||||
if small then
|
||||
for i, c in small do
|
||||
writeu32(pal, (i - 1) * 4, rgb(c // 65536, (c // 256) % 256, c % 256))
|
||||
end
|
||||
return pal
|
||||
end
|
||||
local i = 0
|
||||
for r = 5, 0, -1 do
|
||||
for g = 5, 0, -1 do
|
||||
for b = 5, 0, -1 do
|
||||
if i < 215 then
|
||||
writeu32(pal, i * 4, rgb(r * 51, g * 51, b * 51))
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
-- ramps without the cube's multiples of 0x33: red, green, blue, grey; then black
|
||||
local RAMP = { 0xEE, 0xDD, 0xBB, 0xAA, 0x88, 0x77, 0x55, 0x44, 0x22, 0x11 }
|
||||
i = 215
|
||||
for ch = 0, 3 do
|
||||
for _, v in RAMP do
|
||||
local r = if ch == 0 or ch == 3 then v else 0
|
||||
local g = if ch == 1 or ch == 3 then v else 0
|
||||
local b = if ch == 2 or ch == 3 then v else 0
|
||||
writeu32(pal, i * 4, rgb(r, g, b))
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
writeu32(pal, 255 * 4, rgb(0, 0, 0))
|
||||
return pal
|
||||
end
|
||||
|
||||
-- read(buf, start, stop, codec?) -> depth, palette | nil: start = the offset of the sample description (its size
|
||||
-- field), stop = its end. Cinepak's grey flag selects its own grey mode, so no grey palette is made for it.
|
||||
function QtPalette.read(b: buffer, start: number, stop: number, codec: string?): (number?, buffer?)
|
||||
if start + 86 > stop or stop > buffer.len(b) then
|
||||
return nil, nil
|
||||
end
|
||||
local raw = readu16(b, start + 82)
|
||||
local id = readu16(b, start + 84)
|
||||
local bits = raw % 32
|
||||
local grey = raw >= 32 and (raw // 32) % 2 == 1
|
||||
if bits ~= 1 and bits ~= 2 and bits ~= 4 and bits ~= 8 then
|
||||
return raw, nil
|
||||
end
|
||||
if grey and codec == "cinepak" then
|
||||
return raw, nil
|
||||
end
|
||||
if grey and bits > 1 and id ~= 0 then
|
||||
local pal = buffer.create(1024)
|
||||
local count = 2 ^ bits
|
||||
local step = 256 // (count - 1)
|
||||
local v = 255
|
||||
for i = 0, count - 1 do
|
||||
writeu32(pal, i * 4, rgb(v, v, v))
|
||||
v = math.max(0, v - step)
|
||||
end
|
||||
return bits, pal
|
||||
elseif id ~= 0 then
|
||||
return bits, defaultTable(bits)
|
||||
end
|
||||
local pal = buffer.create(1024)
|
||||
if start + 94 <= stop then
|
||||
local first = readu32(b, start + 86)
|
||||
local last = readu16(b, start + 92)
|
||||
if first <= 255 and last <= 255 then
|
||||
for i = first, last do
|
||||
local o = start + 94 + (i - first) * 8
|
||||
if o + 8 > stop then
|
||||
break
|
||||
end
|
||||
writeu32(pal, i * 4, rgb(readu8(b, o + 2), readu8(b, o + 4), readu8(b, o + 6)))
|
||||
end
|
||||
end
|
||||
end
|
||||
return bits, pal
|
||||
end
|
||||
|
||||
return QtPalette
|
||||
@@ -110,6 +110,10 @@ local function videoTrack(s: Avi.Stream, id: number): Types.Track
|
||||
t.pixelFormat = "yuv420p"
|
||||
end
|
||||
end
|
||||
local bi = Avi.bitmapInfo(f)
|
||||
if bi and bi.palette then
|
||||
t.palette = bi.palette -- 1..8-bit codecs (Microsoft Video 1, Cinepak, QuickTime RLE / SMC in AVI, ...)
|
||||
end
|
||||
local size = buffer.readu32(f, 0)
|
||||
if size > 40 and buffer.len(f) > 40 then
|
||||
local c = buffer.create(buffer.len(f) - 40)
|
||||
|
||||
@@ -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
|
||||
@@ -9,6 +9,8 @@ local Types = require("@formats/Types")
|
||||
local Ebml = require("@formats/containers/Ebml")
|
||||
local Demux = require("@formats/containers/Demux")
|
||||
local Video = require("@formats/codecs/video/Video")
|
||||
local Avi = require("@formats/containers/Avi")
|
||||
local QtPalette = require("@formats/containers/IsoBmff/QtPalette")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
|
||||
local Parse = {}
|
||||
@@ -152,6 +154,39 @@ local function codecFor(id: string, kind: string, bits: number?): string
|
||||
end
|
||||
Parse.codecFor = codecFor
|
||||
|
||||
-- V_MS/VFW/FOURCC (CodecPrivate = a BITMAPINFOHEADER) and V_QUICKTIME (CodecPrivate = a QuickTime sample
|
||||
-- description, sometimes without its size field): the codec from the FourCC, the depth and palette, the extradata
|
||||
local function vfwOrQuickTime(t: any, codecId: string, priv: buffer)
|
||||
local n = buffer.len(priv)
|
||||
if codecId == "V_MS/VFW/FOURCC" then
|
||||
local bi = Avi.bitmapInfo(priv)
|
||||
if bi then
|
||||
t.codec = Video.fromFourcc(bi.compression) or t.codec
|
||||
t.codecTag = bi.compression
|
||||
t.bitsPerPixel = bi.bits
|
||||
t.palette = bi.palette
|
||||
t.config = bi.extra
|
||||
end
|
||||
elseif codecId == "V_QUICKTIME" and n >= 21 then
|
||||
local desc = priv
|
||||
if Video.fromFourcc(buffer.readstring(priv, 0, 4)) then
|
||||
desc = buffer.create(n + 4)
|
||||
local size = n + 4
|
||||
buffer.writeu8(desc, 0, size // 16777216 % 256)
|
||||
buffer.writeu8(desc, 1, size // 65536 % 256)
|
||||
buffer.writeu8(desc, 2, size // 256 % 256)
|
||||
buffer.writeu8(desc, 3, size % 256)
|
||||
buffer.copy(desc, 4, priv, 0, n)
|
||||
end
|
||||
local fcc = buffer.readstring(desc, 4, 4)
|
||||
t.codec = Video.fromFourcc(fcc) or t.codec
|
||||
t.codecTag = fcc
|
||||
local depth, pal = QtPalette.read(desc, 0, buffer.len(desc), t.codec)
|
||||
t.bitsPerPixel = depth
|
||||
t.palette = pal
|
||||
end
|
||||
end
|
||||
|
||||
type TrackInfo = Types.Track & { strip: buffer?, lacing: boolean?, defaultDurationNs: number? }
|
||||
|
||||
local function parseVideo(buf: buffer, off: number, len: number, t: any)
|
||||
@@ -278,6 +313,10 @@ local function parseTracks(buf: buffer, off: number, len: number, scaleNs: numbe
|
||||
t.kind = if ttype == 1 then "video" elseif ttype == 2 then "audio" elseif ttype == 17 then "subtitle" else "data"
|
||||
t.codecTag = codecId
|
||||
t.codec = codecFor(codecId, t.kind, t.bitsPerSample)
|
||||
local cfg: buffer? = t.config
|
||||
if t.kind == "video" and cfg then
|
||||
vfwOrQuickTime(t, codecId, cfg)
|
||||
end
|
||||
if t.kind == "audio" then
|
||||
t.sampleRate = t.sampleRate or 8000
|
||||
t.channels = t.channels or 1
|
||||
|
||||
@@ -27,8 +27,8 @@ local Mkv = {
|
||||
category = "video",
|
||||
extensions = { "mkv", "webm", "mka", "mk3d" },
|
||||
mime = { "video/x-matroska", "video/webm", "audio/x-matroska", "audio/webm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" },
|
||||
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "rawvideo" }, audio = true, subtitles = true },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ebml", "containers/Demux", "containers/Avi", "containers/IsoBmff/QtPalette", "codecs/compress/Deflate", "codecs/video/Video", "codecs/video/Video/Source" },
|
||||
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "vp8", "mjpeg", "mpeg1", "mpeg2", "theora", "rawvideo" }, audio = true, subtitles = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
|
||||
@@ -133,6 +133,8 @@ function Mp4.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
||||
bitsPerSample = it.bitsPerSample,
|
||||
config = it.config,
|
||||
default = true,
|
||||
bitsPerPixel = it.bitsPerPixel,
|
||||
palette = it.palette,
|
||||
}
|
||||
if it.kind == "video" then
|
||||
if not Video.entry(t.codec) then
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- video/smk/Audio: Smacker's packed audio (SMKA: DPCM with Huffman-coded deltas), the one audio codec only Smacker
|
||||
-- files carry. Written from the format description (MultimediaWiki "Smacker"). A packet: the unpacked size (u32),
|
||||
-- then LSB-first bits: data flag, stereo, 16-bit, one 8-bit Huffman tree per channel and byte (codecs/video/Legacy/
|
||||
-- Smacker trees: 1, 2 or 4), the first sample(s) raw (16-bit ones byte-swapped, the right channel first), then per
|
||||
-- sample a delta added with wraparound (the low byte's tree, and the high byte's for 16 bits). Raw PCM tracks (u8 /
|
||||
-- s16le) are converted here too. Output: interleaved floats (u8: (v - 128) / 128, s16: v / 32768), as ffmpeg gives
|
||||
-- them. Never throws: trees are bounded and every read past the packet gives zero bits.
|
||||
local Smacker = require("@formats/codecs/video/Legacy/Smacker")
|
||||
|
||||
local readu8, readi16 = buffer.readu8, buffer.readi16
|
||||
|
||||
local Audio = {}
|
||||
|
||||
-- decode(data, channels, bits, packed) -> frames, floats (1-based interleaved) | nil, message
|
||||
function Audio.decode(d: buffer, channels: number, bits: number, packed: boolean): (number?, { number } | string)
|
||||
local n = buffer.len(d)
|
||||
if not packed then
|
||||
local bps = bits // 8
|
||||
local frames = n // (bps * channels)
|
||||
local count = frames * channels
|
||||
local out = table.create(count, 0)
|
||||
if bps == 1 then
|
||||
for i = 0, count - 1 do
|
||||
out[i + 1] = (readu8(d, i) - 128) / 128
|
||||
end
|
||||
else
|
||||
for i = 0, count - 1 do
|
||||
out[i + 1] = readi16(d, i * 2) / 32768
|
||||
end
|
||||
end
|
||||
return frames, out
|
||||
end
|
||||
if n <= 4 then
|
||||
return nil, "packet too small"
|
||||
end
|
||||
local size = buffer.readu32(d, 0)
|
||||
if size > 2 ^ 24 then
|
||||
return nil, "packet too big"
|
||||
end
|
||||
local br = Smacker.bits(d, 4, n - 4)
|
||||
local bit, read, walk = Smacker.bit, Smacker.read, Smacker.walk
|
||||
if bit(br) == 0 then
|
||||
return 0, {} -- no data
|
||||
end
|
||||
local stereo = bit(br)
|
||||
local wide = bit(br)
|
||||
if (stereo == 1) ~= (channels == 2) or (wide == 1) ~= (bits == 16) then
|
||||
return nil, "packet does not match the track's format"
|
||||
end
|
||||
local bytes = wide + 1
|
||||
local ch = stereo + 1
|
||||
if size % (ch * bytes) ~= 0 then
|
||||
return nil, "unpacked size is not a whole number of samples"
|
||||
end
|
||||
local trees: { { number } } = {}
|
||||
for k = 1, 2 ^ (wide + stereo) do
|
||||
bit(br)
|
||||
local t, msg = Smacker.tree8(br)
|
||||
if not t then
|
||||
return nil, msg or "bad tree"
|
||||
end
|
||||
bit(br)
|
||||
trees[k] = t
|
||||
end
|
||||
if br.nbits - br.pos < ch * bytes * 8 then
|
||||
return nil, "truncated packet"
|
||||
end
|
||||
-- whether any tree needs bits (else every delta is a constant and no end check applies)
|
||||
local coded = false
|
||||
for _, t in trees do
|
||||
if #t > 1 then
|
||||
coded = true
|
||||
end
|
||||
end
|
||||
local frames = size // (ch * bytes)
|
||||
local total = frames * ch
|
||||
local out = table.create(total, 0)
|
||||
if wide == 1 then
|
||||
local pred = { 0, 0 }
|
||||
for c = ch, 1, -1 do
|
||||
local v = read(br, 16)
|
||||
pred[c] = (v // 256) + (v % 256) * 256
|
||||
end
|
||||
for c = 1, math.min(ch, total) do
|
||||
local v = pred[c]
|
||||
out[c] = (if v >= 32768 then v - 65536 else v) / 32768
|
||||
end
|
||||
for i = ch, total - 1 do
|
||||
local c = if ch == 2 then i % 2 else 0
|
||||
if coded and br.pos > br.nbits then
|
||||
return nil, "truncated packet"
|
||||
end
|
||||
local lo = walk(trees[2 * c + 1], br)
|
||||
local hi = walk(trees[2 * c + 2], br)
|
||||
local v = (pred[c + 1] + lo + hi * 256) % 65536
|
||||
pred[c + 1] = v
|
||||
out[i + 1] = (if v >= 32768 then v - 65536 else v) / 32768
|
||||
end
|
||||
else
|
||||
local pred = { 0, 0 }
|
||||
for c = ch, 1, -1 do
|
||||
pred[c] = read(br, 8)
|
||||
end
|
||||
for c = 1, math.min(ch, total) do
|
||||
out[c] = (pred[c] - 128) / 128
|
||||
end
|
||||
for i = ch, total - 1 do
|
||||
local c = if ch == 2 then i % 2 else 0
|
||||
if br.pos > br.nbits then
|
||||
return nil, "truncated packet"
|
||||
end
|
||||
local v = (pred[c + 1] + walk(trees[c + 1], br)) % 256
|
||||
pred[c + 1] = v
|
||||
out[i + 1] = (v - 128) / 128
|
||||
end
|
||||
end
|
||||
return frames, out
|
||||
end
|
||||
|
||||
return Audio
|
||||
@@ -0,0 +1,320 @@
|
||||
--!strict
|
||||
-- video/smk: RAD Game Tools' Smacker video (SMK2 and SMK4). New in formats-luau, written from the format description
|
||||
-- (MultimediaWiki "Smacker").
|
||||
-- Read: the header (size, frame count, frame time: > 0 ms, < 0 units of 10 us; the ring-frame flag), up to 7 audio
|
||||
-- tracks (rate, packed / 16-bit / stereo flags), the frame sizes and flags, the four Huffman trees; per frame a palette
|
||||
-- update (skip / copy-from-old / new 6-bit entries), the audio chunks and the video data, into a Demuxer. open()
|
||||
-- returns a VideoDecoder: rgba8 frames from the Smacker video codec (codecs/video/Legacy/Smacker, through the
|
||||
-- codecs/video/Video registry: the frame's palette rides on each packet as packet.palette), and the first audio
|
||||
-- track as an audio Decoder built here (Smacker's packed DPCM audio has no audio format, so the format decodes it
|
||||
-- itself: video/smk/Audio over codecs/audio/Stream; raw u8 / s16le tracks too). The ring frame (the extra frame that
|
||||
-- leads back to the first) is not shown unless opts.ringFrame. Smk.demux(r) gives the Demuxer; Smk.openAudio(dmx,
|
||||
-- track) the Decoder of any of its audio tracks.
|
||||
-- Not supported: writing, Bink audio tracks (flags 0x08 / 0x04: reported as "binkaudio", no decoder).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ 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")
|
||||
local Stream = require("@formats/codecs/audio/Stream")
|
||||
local Audio = require("@formats/video/smk/Audio")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Smk = {
|
||||
name = "smk",
|
||||
category = "video",
|
||||
extensions = { "smk" },
|
||||
mime = { "video/vnd.radgamettools.smacker" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/video/Video", "codecs/audio/Stream", "codecs/video/Legacy/Smacker" },
|
||||
caps = { decode = true, encode = false, streaming = true, video = { "smackvid" }, audio = { "smackaudio", "pcm_u8", "pcm_s16le" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type OpenOptions = {
|
||||
rgba: boolean?, -- (frames are rgba8 already)
|
||||
maxPixels: number?, -- default 2^26
|
||||
audio: boolean?, -- false: do not open the audio track
|
||||
ringFrame: boolean?, -- true: also show the ring frame at the end (as ffmpeg does)
|
||||
openAudio: any?, -- the api's hook: used only for tracks this format cannot decode (Bink audio)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local HEADER = 104
|
||||
local RING = 1
|
||||
-- 6-bit palette components -> 8 bits (bit replication: v << 2 | v >> 4)
|
||||
local PAL6 = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
PAL6[i + 1] = i * 4 + i // 16
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Smk.sniff(r: Bytes.Reader): number
|
||||
local s = r:peek(4)
|
||||
if s ~= "SMK2" and s ~= "SMK4" then
|
||||
return 0
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- a frame's palette update over the previous palette (pal: 256 u32 R, G, B, A); false when malformed
|
||||
local function palette(b: buffer, p: number, stop: number, old: buffer): (buffer?)
|
||||
local pal = buffer.create(1024)
|
||||
buffer.copy(pal, 0, old, 0, 1024)
|
||||
local function u8(): number
|
||||
local v = if p < stop then buffer.readu8(b, p) else 0
|
||||
p += 1
|
||||
return v
|
||||
end
|
||||
local sz = 0
|
||||
while sz < 256 do
|
||||
local t = u8()
|
||||
if t >= 0x80 then
|
||||
sz += t - 0x80 + 1
|
||||
elseif t >= 0x40 then
|
||||
local off = u8()
|
||||
local j = t - 0x40 + 1
|
||||
if off + j > 256 then
|
||||
return nil
|
||||
end
|
||||
while j > 0 and sz < 256 do
|
||||
buffer.writeu32(pal, sz * 4, buffer.readu32(old, off * 4))
|
||||
sz += 1
|
||||
off += 1
|
||||
j -= 1
|
||||
end
|
||||
else
|
||||
local r = PAL6[t + 1]
|
||||
local g = PAL6[u8() % 64 + 1]
|
||||
local bl = PAL6[u8() % 64 + 1]
|
||||
buffer.writeu32(pal, sz * 4, r + g * 256 + bl * 65536 + 0xFF000000)
|
||||
sz += 1
|
||||
end
|
||||
end
|
||||
return pal
|
||||
end
|
||||
|
||||
-- demux(r, opts?) -> the Demuxer | nil, Err
|
||||
function Smk.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
||||
local b, base, limit = r.buf, r.base, r.limit
|
||||
if limit - base < HEADER then
|
||||
return nil, Err.new(Err.TRUNCATED, "file shorter than the header")
|
||||
end
|
||||
local hr = Bytes.reader(b, base, HEADER)
|
||||
local magic = hr:string(4)
|
||||
local w, h, frames = hr:u32le(), hr:u32le(), hr:u32le()
|
||||
local ptsInc = hr:i32le()
|
||||
local flags = hr:u32le()
|
||||
hr:skip(28)
|
||||
local treeSize = hr:u32le()
|
||||
local sizes = { hr:u32le(), hr:u32le(), hr:u32le(), hr:u32le() }
|
||||
local rates, aflags = {}, {}
|
||||
for k = 1, 7 do
|
||||
rates[k] = hr:u24le()
|
||||
aflags[k] = hr:u8()
|
||||
end
|
||||
if hr.err then
|
||||
return nil, hr.err
|
||||
end
|
||||
if magic ~= "SMK2" and magic ~= "SMK4" then
|
||||
return nil, Err.new(Err.CORRUPT, "not a Smacker file")
|
||||
end
|
||||
if w == 0 or h == 0 or w > 32768 or h > 32768 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad 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
|
||||
local shown = frames
|
||||
if bit32.band(flags, RING) ~= 0 then
|
||||
frames += 1
|
||||
end
|
||||
local p = base + HEADER
|
||||
if frames > 0xFFFFFF or p + frames * 5 + treeSize > limit then
|
||||
return nil, Err.new(Err.TRUNCATED, "frame tables or trees past the end of the file")
|
||||
end
|
||||
-- the trees (with the four sizes in front, as the video codec takes them)
|
||||
local config = buffer.create(16 + treeSize)
|
||||
for k = 1, 4 do
|
||||
buffer.writeu32(config, (k - 1) * 4, sizes[k])
|
||||
end
|
||||
buffer.copy(config, 16, b, p + frames * 5, treeSize)
|
||||
-- frame time: > 0 ms, < 0 units of 10 us (ticks of 1/100000 s)
|
||||
local tick = if ptsInc < 0 then -ptsInc elseif ptsInc > 0 then ptsInc * 100 else 10000
|
||||
local tracks: { Types.Track } = {
|
||||
{
|
||||
id = 1,
|
||||
kind = "video",
|
||||
codec = "smackvid",
|
||||
codecTag = magic,
|
||||
timescale = 100000,
|
||||
width = w,
|
||||
height = h,
|
||||
frameRate = 100000 / tick,
|
||||
default = true,
|
||||
config = config,
|
||||
flags = flags,
|
||||
} :: Types.Track,
|
||||
}
|
||||
local audio: { [number]: Types.Track } = {}
|
||||
for k = 1, 7 do
|
||||
if rates[k] > 0 then
|
||||
local f = aflags[k]
|
||||
local bits = if bit32.band(f, 0x20) ~= 0 then 16 else 8
|
||||
local codec = if bit32.band(f, 0x0C) ~= 0 then "binkaudio"
|
||||
elseif bit32.band(f, 0x80) ~= 0 then "smackaudio"
|
||||
elseif bits == 16 then "pcm_s16le"
|
||||
else "pcm_u8"
|
||||
local t: Types.Track = {
|
||||
id = k + 1,
|
||||
kind = "audio",
|
||||
codec = codec,
|
||||
codecTag = string.format("0x%02X", f),
|
||||
timescale = rates[k],
|
||||
sampleRate = rates[k],
|
||||
channels = if bit32.band(f, 0x10) ~= 0 then 2 else 1,
|
||||
bitsPerSample = bits,
|
||||
default = #tracks == 1,
|
||||
}
|
||||
audio[k] = t
|
||||
table.insert(tracks, t)
|
||||
end
|
||||
end
|
||||
local dmx = Demux.new("smk", tracks, b, { version = magic, flags = flags, frames = shown, ringFrame = frames > shown })
|
||||
local count = if opts and opts.ringFrame then frames else shown
|
||||
local pal = buffer.create(1024)
|
||||
local apts = { 0, 0, 0, 0, 0, 0, 0 }
|
||||
local at = p + frames * 5 + treeSize
|
||||
for i = 0, frames - 1 do
|
||||
local fsize = buffer.readu32(b, p + i * 4)
|
||||
local fflags = buffer.readu8(b, p + frames * 4 + i)
|
||||
local size = fsize - fsize % 4
|
||||
local q, stop = at, math.min(limit, at + size)
|
||||
at += size
|
||||
local ok = true
|
||||
if fflags % 2 == 1 and q < stop then
|
||||
local psize = buffer.readu8(b, q) * 4
|
||||
if psize > size then
|
||||
ok = false
|
||||
else
|
||||
local np = palette(b, q + 1, math.min(stop, q + psize), pal)
|
||||
if np then
|
||||
pal = np
|
||||
else
|
||||
ok = false
|
||||
end
|
||||
q += psize
|
||||
end
|
||||
end
|
||||
for k = 1, 7 do
|
||||
if not ok then
|
||||
break
|
||||
end
|
||||
if bit32.band(fflags, 2 ^ k) ~= 0 then
|
||||
if q + 4 > stop then
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
local asize = buffer.readu32(b, q)
|
||||
local t = audio[k]
|
||||
local min = if t and t.codec ~= "pcm_s16le" then 8 else 4
|
||||
if asize < min or q + asize > stop then
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
if t then
|
||||
local len = asize - 4
|
||||
local frameBytes = (t.channels :: number) * (t.bitsPerSample :: number) // 8
|
||||
local n = if t.codec == "smackaudio" then buffer.readu32(b, q + 4) // frameBytes
|
||||
elseif t.codec == "binkaudio" then 0
|
||||
else len // frameBytes
|
||||
dmx:add(t.id, apts[k], nil, n, true, q + 4, len)
|
||||
apts[k] += n
|
||||
end
|
||||
q += asize
|
||||
end
|
||||
end
|
||||
if ok and i < count then
|
||||
dmx:add(1, i * tick, nil, tick, i == 0 or fsize % 2 == 1, q, math.max(0, stop - q))
|
||||
dmx:setExtra("palette", pal)
|
||||
end
|
||||
end
|
||||
dmx:finish()
|
||||
return dmx, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- openAudio(dmx, track) -> an audio Decoder for one of the demuxer's audio tracks | nil, Err
|
||||
function Smk.openAudio(dmx: any, track: Types.Track): (Types.Decoder?, Err.Err?)
|
||||
local codec = track.codec
|
||||
if codec ~= "smackaudio" and codec ~= "pcm_u8" and codec ~= "pcm_s16le" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "audio codec " .. codec)
|
||||
end
|
||||
local id = track.id
|
||||
local count = dmx:count(id)
|
||||
local starts: { number } = {}
|
||||
local total = 0
|
||||
for k = 1, count do
|
||||
local pts, _, dur = dmx:info(id, k)
|
||||
starts[k] = pts or total
|
||||
total = (pts or total) + (dur or 0)
|
||||
end
|
||||
starts[count + 1] = total
|
||||
local rate, ch, bits = track.sampleRate :: number, track.channels :: number, track.bitsPerSample :: number
|
||||
local packed = codec == "smackaudio"
|
||||
local floats: { number } = {}
|
||||
local dec: any = {
|
||||
sampleRate = rate,
|
||||
channels = ch,
|
||||
frames = total,
|
||||
duration = total / rate,
|
||||
bitrate = rate * ch * bits,
|
||||
tags = {},
|
||||
}
|
||||
return Stream.packets(dec, {
|
||||
count = count,
|
||||
start = function(i: number): number
|
||||
return starts[math.clamp(i, 1, count + 1)]
|
||||
end,
|
||||
decode = function(i: number): (number?, Err.Err?)
|
||||
local pkt = dmx:get(id, i)
|
||||
if not pkt then
|
||||
return nil, Err.new(Err.TRUNCATED, "no audio packet " .. i)
|
||||
end
|
||||
local n, out = Audio.decode(pkt.data, ch, bits, packed)
|
||||
if not n then
|
||||
return nil, Err.from(out :: string)
|
||||
end
|
||||
floats = out :: { number }
|
||||
return n, nil
|
||||
end,
|
||||
pcm = function(): { number }
|
||||
return floats
|
||||
end,
|
||||
reset = function() end,
|
||||
cumulative = true,
|
||||
})
|
||||
end
|
||||
|
||||
-- open(r, opts?) -> a VideoDecoder | nil, Err
|
||||
function Smk.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
||||
local dmx, err = Smk.demux(r, opts)
|
||||
if not dmx then
|
||||
return nil, err
|
||||
end
|
||||
local o: any = table.clone(opts or {})
|
||||
type Hook = (any, Types.Track) -> (Types.Decoder?, Err.Err?)
|
||||
local hook = o.openAudio :: Hook?
|
||||
o.openAudio = function(d: any, track: Types.Track): (Types.Decoder?, Err.Err?)
|
||||
if track.codec == "binkaudio" and hook then
|
||||
return hook(d, track)
|
||||
end
|
||||
return Smk.openAudio(d, track)
|
||||
end
|
||||
return Video.open(dmx, o)
|
||||
end
|
||||
|
||||
return Smk
|
||||
Reference in new issue
Block a user