Files
rosetta/src/Formats/containers/IsoBmff/QtPalette.luau
T
4DBug 8e3873c9fb codecs/video/Legacy: Cinepak, Microsoft Video 1, QuickTime RLE / SMC and FLIC decoders
Registry decoders (codecs/video/Video BUILTIN: cvid, CRAM / MSVC / WHAM, 'rle ', 'smc ', AFLC), rgba8 pictures
painted over the previous picture, palettes from track.palette (or packet.palette). Bit-exact with ffmpeg on
28 AVI / MOV / MKV fixtures (tools/fixtures/legacy.py: ffmpeg's encoders plus encoders in _legacy_mux.py for
QuickTime RLE 1/2/4/8-bit, 8-bit MS Video 1 and Cinepak). Also fixes QtPalette's big-endian reads.
2026-10-31 14:20:17 -05:00

121 lines
4.0 KiB
Luau

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