containers: video track palettes and depths for the legacy codecs

Small backward-compatible additions so palettised codecs find their colour table on the Track:
- containers/Avi: Avi.bitmapInfo(strf) (BITMAPINFOHEADER facts + palette as 256 RGBA u32); video/avi sets
  track.palette for 1..8-bit streams.
- containers/IsoBmff/QtPalette (new): QuickTime sample description depth + colour table (stsd table, Mac default
  tables, grey ramps); IsoBmff Demux sets track.bitsPerPixel / track.palette, video/mp4 copies them.
- video/mkv: V_MS/VFW/FOURCC (BITMAPINFOHEADER) and V_QUICKTIME (sample description) CodecPrivate map to the
  FourCC's codec with depth / palette.
This commit is contained in:
4DBug committed 2026-10-31 14:02:01 -05:00
1 parent de4f14a343
commit 7b4bf1ee43
8 files changed
+220 -2

No files matched your search

+1 -1
View File
@@ -308,7 +308,7 @@ 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" },
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", "rawvideo" }, audio = true, subtitles = true },
load = function()
return require("@formats/video/mkv")
+47
View File
@@ -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
+14
View File
@@ -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,113 @@
--!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, readu16, readu32, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu32
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
+4
View File
@@ -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)
+38
View File
@@ -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,9 @@ 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)
if t.kind == "video" and t.config then
vfwOrQuickTime(t, codecId, t.config)
end
if t.kind == "audio" then
t.sampleRate = t.sampleRate or 8000
t.channels = t.channels or 1
+1 -1
View File
@@ -27,7 +27,7 @@ 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" },
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", "rawvideo" }, audio = true, subtitles = true },
}
+2
View File
@@ -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