comments: containers Chunks / MacFile / MpegEs / Ogg / IsoBmff (part) down to specs

This commit is contained in:
4DBug committed 2026-11-01 01:57:14 -05:00
1 parent 9427e6e5fb
commit e5a8e62fbc
8 files changed
+8 -92

No files matched your search

-7
View File
@@ -42,7 +42,6 @@ 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)
@@ -79,8 +78,6 @@ function Box.header(b: buffer, p: number, limit: number): (Box?, Err.Err?)
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 } = {}
@@ -133,7 +130,6 @@ function Box.list(b: buffer, p: number, limit: number, parentType: string?, dept
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
@@ -146,7 +142,6 @@ function Box.child(box: Box?, typ: string): Box?
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
@@ -159,7 +154,6 @@ function Box.childrenOf(box: Box?, typ: string): { Box }
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
@@ -181,7 +175,6 @@ function Box.path(root: any, path: string): Box?
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
@@ -5,10 +5,6 @@
-- 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
@@ -71,8 +67,7 @@ local function defaultTable(bits: number): buffer
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.
-- 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
-20
View File
@@ -2,15 +2,6 @@
-- containers/IsoBmff: ISO base media file format (ISO/IEC 14496-12) and its relatives: MP4 / M4A / M4V / MOV (QuickTime)
-- / 3GP, fragmented MP4 (moof), HEIF / AVIF items (ISO/IEC 23008-12), iTunes tags. A container (layer 2): m4a, mp4,
-- mov, heif, avif build on it. Written from the specifications; no third-party code.
--
-- local IsoBmff = require("@rosetta/containers/IsoBmff")
-- local movie, err = IsoBmff.parse(r) -- ftyp brand, tracks (Demux.Track: codec, config, sample tables)
-- local dmx = IsoBmff.demux(r) -- Types.Demuxer: tracks, duration, packets(trackId?), seek(s)
-- local tags, pictures = IsoBmff.tags(r, movie) -- moov/udta/meta/ilst (or a top-level meta)
-- local items = IsoBmff.items(r, movie) -- HEIF / AVIF: primary item, items, properties, refs
-- local data = IsoBmff.itemData(r, items, id) -- an item's bytes
-- local file = IsoBmff.mux({ brand = "M4A ", tracks = { { kind = "audio", entry = "alac", ... } }, tags = ... })
--
-- Sample offsets in tracks are absolute offsets into r.buf (r may cover part of a bigger buffer). Supports: 32 / 64-bit
-- boxes, stsz / stz2, stco / co64, ctts v0 / v1, stss, elst v0 / v1, QuickTime sound descriptions v1 / v2 and 'wave',
-- fragments with trex defaults / tfdt / data offsets. Not supported: external data references (dref urls),
@@ -40,8 +31,6 @@ local TOP: { [string]: boolean } = {
styp = true, sidx = true, meta = true, uuid = true, PICT = true,
}
-- sniff(r) -> brand ("M4A ", "isom", "qt ", "avif", "heic", ...) or "" when the file starts with another top-level box,
-- nil when it is not an ISO BMFF file
function IsoBmff.sniff(r: Bytes.Reader): string?
local head = r:peek(12)
if #head < 8 then
@@ -60,18 +49,15 @@ function IsoBmff.sniff(r: Bytes.Reader): string?
return nil
end
-- parse(r, opts?) -> Movie | nil, Err
function IsoBmff.parse(r: Bytes.Reader, opts: Demux.Options?): (Movie?, Err.Err?)
return Demux.parse(r, opts)
end
-- tags(r, movie) -> Tags, { Picture }: iTunes tags from moov/udta/meta/ilst (or moov/meta/ilst, or a top-level meta)
function IsoBmff.tags(r: Bytes.Reader, movie: Movie): ({ [string]: string }, { Picture })
local ilst = Box.path(movie.boxes, "moov/udta/meta/ilst") or Box.path(movie.boxes, "moov/meta/ilst") or Box.path(movie.boxes, "meta/ilst")
return Meta.tags(r.buf, ilst)
end
-- items(r, movie) -> Items | nil, Err (the top-level meta box of a HEIF / AVIF file)
function IsoBmff.items(r: Bytes.Reader, movie: Movie): (Items?, Err.Err?)
local meta = Box.path(movie.boxes, "meta")
if not meta then
@@ -80,12 +66,10 @@ function IsoBmff.items(r: Bytes.Reader, movie: Movie): (Items?, Err.Err?)
return Meta.items(r.buf, meta)
end
-- itemData(r, items, id) -> buffer | nil, Err
function IsoBmff.itemData(r: Bytes.Reader, items: Items, id: number): (buffer?, Err.Err?)
return Meta.itemData(r.buf, r.base, r.limit, items, id)
end
-- sample(r, track, i) -> a copy of sample i (1-based)
function IsoBmff.sample(r: Bytes.Reader, t: Track, i: number): buffer?
if i < 1 or i > t.sampleCount then
return nil
@@ -96,8 +80,6 @@ function IsoBmff.sample(r: Bytes.Reader, t: Track, i: number): buffer?
return out
end
-- demux(r, opts?) -> Types.Demuxer (tracks, duration, packets(trackId?), seek(seconds)) | nil, Err. Packets carry pts /
-- dts / duration in their track's timescale ticks and a copy of the sample bytes.
function IsoBmff.demux(r: Bytes.Reader, opts: Demux.Options?): (any, Err.Err?)
local movie, err = Demux.parse(r, opts)
if not movie then
@@ -137,7 +119,6 @@ function IsoBmff.demux(r: Bytes.Reader, opts: Demux.Options?): (any, Err.Err?)
cursor[trackId] = i + 1
return packet(t, i)
end
-- all tracks in file order
local best: Track? = nil
local bestOff = math.huge
for _, t in movie.tracks do
@@ -180,7 +161,6 @@ function IsoBmff.demux(r: Bytes.Reader, opts: Demux.Options?): (any, Err.Err?)
return dmx, nil
end
-- mux(spec) -> buffer | nil, Err (see containers/IsoBmff/Mux)
function IsoBmff.mux(spec: MuxSpec): (buffer?, Err.Err?)
return Mux.mux(spec)
end