Files
rosetta/src/Formats/containers/IsoBmff/Box.luau
T

194 lines
6.2 KiB
Luau

--!strict
-- containers/IsoBmff/Box: the box tree of an ISO BMFF / QuickTime file (ISO/IEC 14496-12 4.2): 32-bit, 64-bit
-- (size 1) and to-the-end (size 0) sizes, uuid boxes, full-box version / flags, and the recursion into the container
-- boxes (moov / trak / mdia / minf / stbl / udta / meta / ilst items / moof / traf / iprp / ipco / iref / dinf / ...).
-- Sample descriptions (stsd) are left to the demuxer, which knows the sample entry layouts. Never throws.
local Err = require("@formats/core/Err")
local readu8, readu32, readstring = buffer.readu8, buffer.readu32, buffer.readstring
local byteswap = bit32.byteswap
local Box = {}
export type Box = {
type: string, -- four characters ("moov", "\169nam", ...); "uuid" boxes also have `uuid`
start: number, -- absolute offset of the box in the buffer
size: number, -- the whole box, header included
data: number, -- absolute offset of the payload (after size / type / largesize / uuid)
stop: number, -- absolute offset just past the box (start + size, clamped to the parent)
uuid: string?,
children: { Box }?, -- container boxes
truncated: boolean?, -- the box claims more bytes than its parent has (clamped)
}
-- plain container boxes: their payload is a list of boxes
local CONTAINERS = {
moov = true, trak = true, mdia = true, minf = true, stbl = true, dinf = true, edts = true, udta = true,
mvex = true, moof = true, traf = true, mfra = true, tref = true, iprp = true, ipco = true, sinf = true,
schi = true, wave = true, gmhd = true, tapt = true, ilst = true, clip = true, matt = true, rinf = true,
strk = true, strd = true, grpl = true, meco = true, mere = true,
}
-- full boxes (version + flags) whose payload is then a list of boxes, after `skip` more bytes
local FULL_CONTAINERS: { [string]: number } = { meta = 0, iref = 0, dref = 4 }
Box.CONTAINERS = CONTAINERS
local function be32(b: buffer, o: number): number
return byteswap(readu32(b, o))
end
Box.be32 = be32
-- the ilst item boxes ("\169nam", "trkn", "----", ...) contain data / mean / name boxes
local function isIlstItem(parentType: string?): boolean
return parentType == "ilst"
end
-- header(buf, p, limit) -> Box (without children) | nil, Err
function Box.header(b: buffer, p: number, limit: number): (Box?, Err.Err?)
if p + 8 > limit then
return nil, Err.new(Err.TRUNCATED, "box header runs past the end", p)
end
local size = be32(b, p)
local typ = readstring(b, p + 4, 4)
local data = p + 8
if size == 1 then
if p + 16 > limit then
return nil, Err.new(Err.TRUNCATED, "64-bit box size runs past the end", p)
end
size = be32(b, p + 8) * 4294967296 + be32(b, p + 12)
data = p + 16
elseif size == 0 then
size = limit - p
end
local uuid: string? = nil
if typ == "uuid" then
if data + 16 > limit then
return nil, Err.new(Err.TRUNCATED, "uuid runs past the end", p)
end
uuid = readstring(b, data, 16)
data += 16
end
if size < data - p then
return nil, Err.new(Err.CORRUPT, string.format("box '%s' size %d is smaller than its header", typ, size), p)
end
local stop = p + size
local truncated: boolean? = nil
if stop > limit then
stop = limit
truncated = true
end
return { type = typ, start = p, size = size, data = data, stop = stop, uuid = uuid, children = nil, truncated = truncated }, nil
end
-- list(buf, p, limit, parentType?, depth?) -> { Box } (a run of sibling boxes, containers parsed recursively). A
-- broken box ends the list (its siblings so far are kept); the second value is the first error met, if any.
function Box.list(b: buffer, p: number, limit: number, parentType: string?, depth: number?): ({ Box }, Err.Err?)
local d = depth or 0
local out: { Box } = {}
local firstErr: Err.Err? = nil
if d > 32 then
return out, Err.new(Err.CORRUPT, "boxes nested too deep", p)
end
while p + 8 <= limit do
local box, err = Box.header(b, p, limit)
if not box then
firstErr = firstErr or err
break
end
if box.truncated then
firstErr = firstErr or Err.new(Err.TRUNCATED, "box '" .. box.type .. "' runs past the end", p)
end
local t = box.type
local kids: { Box }? = nil
local kerr: Err.Err? = nil
if CONTAINERS[t] or isIlstItem(parentType) then
kids, kerr = Box.list(b, box.data, box.stop, t, d + 1)
elseif FULL_CONTAINERS[t] then
local q = box.data + 4 + FULL_CONTAINERS[t]
-- QuickTime writes udta/meta as a plain container: its first child box follows directly
if t == "meta" and box.data + 8 <= box.stop and readstring(b, box.data + 4, 4) == "hdlr" then
q = box.data
end
if q <= box.stop then
kids, kerr = Box.list(b, q, box.stop, t, d + 1)
end
elseif t == "iinf" then
if box.data + 4 <= box.stop then
local v = readu8(b, box.data)
local q = box.data + 4 + (if v == 0 then 2 else 4)
if q <= box.stop then
kids, kerr = Box.list(b, q, box.stop, t, d + 1)
end
end
end
box.children = kids
if kerr then
firstErr = firstErr or kerr
end
table.insert(out, box)
if box.stop <= p then
break
end
p = box.stop
end
return out, firstErr
end
-- child(box, type) -> the first child of that type
function Box.child(box: Box?, typ: string): Box?
if not box or not box.children then
return nil
end
for _, c in box.children :: { Box } do
if c.type == typ then
return c
end
end
return nil
end
-- children(box, type) -> every child of that type
function Box.childrenOf(box: Box?, typ: string): { Box }
local out = {}
if box and box.children then
for _, c in box.children :: { Box } do
if c.type == typ then
table.insert(out, c)
end
end
end
return out
end
-- path(boxes or box, "moov/trak/mdia") -> the first box along the path
function Box.path(root: any, path: string): Box?
local cur: any = root
for part in string.gmatch(path, "[^/]+") do
if cur == nil then
return nil
end
local list = if cur.children ~= nil or cur.type ~= nil then cur.children else cur
local found = nil
if list then
for _, c in list do
if c.type == part then
found = c
break
end
end
end
cur = found
end
return cur
end
-- version / flags of a full box
function Box.fullHeader(b: buffer, box: Box): (number, number)
if box.data + 4 > box.stop then
return -1, 0
end
local v = be32(b, box.data)
return bit32.rshift(v, 24), bit32.band(v, 0xFFFFFF)
end
return Box