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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 14:02:01 -05:00
1 parent d3dbba500b
commit b3566459a0
8 files changed
+220 -2

No files matched your search

+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