272 lines
6.5 KiB
Luau
272 lines
6.5 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/video/Legacy/QtRle: QuickTime Animation ('rle ') frames, depths 1, 2, 4, 8 (palettised; 33..40 = grey),
|
|
-- 16 (X-R-G-B 1-5-5-5), 24 (R, G, B), 32 (A, R, G, B). From Apple QuickTime File Format and MultimediaWiki "Apple
|
|
-- QuickTime RLE", bit-exact with ffmpeg on valid streams: chunk size, header (0x0008: start line + line count), per line
|
|
-- a skip byte and codes until -1 (0: another skip; < 0: one group repeated; > 0: literal groups). Groups are 4 pixels at
|
|
-- 8 bits, 8 at 4, 16 at 2, 1 at 16 / 24 / 32. Frames shorter than 8 bytes repeat the picture.
|
|
local Picture = require("@rosetta/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
|
|
local per = 8 // bpp
|
|
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
|
|
|
|
function QtRle.decode(pic: Picture.Picture, d: buffer, depth: number): (boolean, string?)
|
|
local n = buffer.len(d)
|
|
if n < 8 then
|
|
return true, nil
|
|
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
|
|
|
|
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
|