From a355bf3ca3ed1bdd8e0278a352ba27b7c8ac4a68 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 01:51:55 -0500 Subject: [PATCH] comments: containers Archive / Demux / Ebml / MpegPs / Riff --- src/Rosetta/containers/Archive.luau | 34 +++---------------------- src/Rosetta/containers/Demux.luau | 25 +----------------- src/Rosetta/containers/Ebml/init.luau | 24 ----------------- src/Rosetta/containers/MpegPs/init.luau | 8 +----- src/Rosetta/containers/Riff/init.luau | 28 -------------------- 5 files changed, 6 insertions(+), 113 deletions(-) diff --git a/src/Rosetta/containers/Archive.luau b/src/Rosetta/containers/Archive.luau index 8c31b67..d544677 100644 --- a/src/Rosetta/containers/Archive.luau +++ b/src/Rosetta/containers/Archive.luau @@ -1,14 +1,6 @@ --!strict --- containers/Archive: the in-memory Archive value (Types.Archive) that archive formats return. Entries hold their data --- or a lazy loader (decompressed on read); add() appends or replaces an entry; write() calls the format's encoder --- (attached by the format), so an archive read from a .tar writes back as a .tar, and any Archive (a zip, a 7z) can be --- encoded by any writable archive format through its list() / read(). --- --- local a = Archive.new("tar", Tar.encode) -- encode(archive, opts) -> buffer | nil, Err --- Archive.put(a, { name = "a.txt", size = 3, data = buf, mtime = 0 }) --- Archive.put(a, { name = "b.bin", size = n, load = function() return inflate(...) end }) --- a:list() a:read("a.txt") a:add("c.txt", "text") a:write() --- +-- containers/Archive: the in-memory Archive value (Types.Archive) archive formats return. Entries hold their data or +-- a lazy loader; write() calls the encoder the format attached, and any Archive can be encoded by any archive writer. -- Single-stream archives (gzip, zst, xz, bz2, br) set meta.stream = true and hold one entry; the api unwraps them -- (a .tar.gz loads as the tar inside). Never throws: read of a missing entry or a failed loader returns nil, Err. local Err = require("@rosetta/core/Err") @@ -34,7 +26,6 @@ export type Encoder = (archive: any, opts: any?) -> (buffer?, Err.Err?) local Methods = {} Methods.__index = Methods --- the public fields of an entry (no data / loader) local function public(e: Entry): Types.ArchiveEntry local out: any = {} for k, v in e :: any do @@ -53,7 +44,6 @@ function Methods.list(self: any): { Types.ArchiveEntry } return out end --- read(name) -> buffer | nil, Err (directories read as an empty buffer) function Methods.read(self: any, name: string): (buffer?, Err.Err?) local e: Entry? = self._byName[name] if not e then @@ -75,7 +65,6 @@ function Methods.read(self: any, name: string): (buffer?, Err.Err?) return buffer.create(0), nil end --- add(name, data, opts?) -> (): opts.mtime (unix seconds), opts.dir, opts.mode; replaces an entry of the same name function Methods.add(self: any, name: string, data: buffer | string, opts: any?) local b: buffer = if type(data) == "string" then buffer.fromstring(data) elseif type(data) == "buffer" then data else buffer.create(0) local o = opts or {} @@ -90,7 +79,6 @@ function Methods.add(self: any, name: string, data: buffer | string, opts: any?) Archive.put(self, e) end --- write(opts?) -> buffer | nil, Err: the archive's own format (opts.format is handled by the api) function Methods.write(self: any, opts: any?): (buffer?, Err.Err?) local enc: Encoder? = self._encode if not enc then @@ -99,7 +87,6 @@ function Methods.write(self: any, opts: any?): (buffer?, Err.Err?) return enc(self, opts) end --- new(format, encode?) -> an empty Archive function Archive.new(format: string, encode: Encoder?): any return setmetatable({ kind = "archive", @@ -111,7 +98,6 @@ function Archive.new(format: string, encode: Encoder?): any }, Methods) end --- put(archive, entry): append (or replace by name) an entry function Archive.put(a: any, e: Entry) local old = a._byName[e.name] if old then @@ -125,8 +111,6 @@ function Archive.put(a: any, e: Entry) a._byName[e.name] = e end --- entries(value) -> { { entry, read: () -> (buffer?, Err?) } } for any Archive-shaped value (ours or another --- format's): what encoders iterate. nil when the value is not an archive. function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () -> (buffer?, Err.Err?) } }? if type(value) ~= "table" or value.kind ~= "archive" or type(value.list) ~= "function" or type(value.read) ~= "function" then return nil @@ -153,7 +137,6 @@ function Archive.entries(value: any): { { entry: Types.ArchiveEntry, read: () -> return out end --- slice(buf, at, n) -> a loader for Entry.load: a copy of buf[at, at + n) (stored entries of uncompressed archives) function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err.Err?) return function(): (buffer?, Err.Err?) local out = buffer.create(n) @@ -162,8 +145,6 @@ function Archive.slice(buf: buffer, at: number, n: number): () -> (buffer?, Err. end end --- collect(value) -> { { entry, data } } | nil, Err: every entry of an Archive-shaped value with its bytes read --- (directories: data = an empty buffer), for writers of simple archives (PAK, WAD, GRP, VPK, ...) function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buffer } }?, Err.Err?) local items = Archive.entries(value) if not items then @@ -184,8 +165,6 @@ function Archive.collect(value: any): ({ { entry: Types.ArchiveEntry, data: buff return out, nil end --- files(value, name?) -> { { name, data, entry } } | nil, Err: the files (directories skipped) of any Archive value, --- read; a buffer or a string is one file named name (default "data"). What single-file and encoding writers take. function Archive.files(value: any, name: string?): ({ { name: string, data: buffer, entry: any } }?, Err.Err?) if type(value) == "buffer" or type(value) == "string" then local b = if type(value) == "string" then buffer.fromstring(value) else value @@ -208,8 +187,6 @@ function Archive.files(value: any, name: string?): ({ { name: string, data: buff return out, nil end --- streamData(value) -> what a single-stream encoder compresses: a buffer / string as it is, an Archive's one file, or --- a tar of the Archive's entries (files, directories, symlinks with their mode / mtime) | nil, Err function Archive.streamData(value: any): (buffer?, Err.Err?) if type(value) == "buffer" then return value, nil @@ -240,8 +217,7 @@ function Archive.streamData(value: any): (buffer?, Err.Err?) return Tar.write(list) end --- fromDos(stamp) -> unix seconds | nil: an MS-DOS date and time (stamp = date * 65536 + time, as LHA / ARJ / ZIP store --- them little endian; local time without a zone, read as UTC); nil for a zero or invalid date +-- MS-DOS date / time: stamp = date * 65536 + time (LHA / ARJ / ZIP); local time without a zone, read as UTC function Archive.fromDos(stamp: number): number? local time, date = stamp % 65536, stamp // 65536 local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31) @@ -252,7 +228,7 @@ function Archive.fromDos(stamp: number): number? return Types.Date.daysFromCivil(y, m, d) * 86400 + secs end --- toDos(unix?) -> date * 65536 + time (clamped to 1980-01-01 .. 2107-12-31) +-- MS-DOS dates cover 1980-01-01 .. 2107-12-31 function Archive.toDos(t: number?): number if type(t) ~= "number" or t ~= t or t < 315532800 then return 0x210000 -- 1980-01-01 00:00 @@ -267,8 +243,6 @@ function Archive.toDos(t: number?): number return ((y - 1980) * 512 + m * 32 + d) * 65536 + (rem // 3600) * 2048 + ((rem // 60) % 60) * 32 + (rem % 60) // 2 end --- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension --- (".tgz" -> ".tar"), else "data" function Archive.streamName(opts: any?, exts: { string }): string local name = if type(opts) == "table" and type(opts.name) == "string" then opts.name else nil if not name then diff --git a/src/Rosetta/containers/Demux.luau b/src/Rosetta/containers/Demux.luau index 5662ec1..76fcb74 100644 --- a/src/Rosetta/containers/Demux.luau +++ b/src/Rosetta/containers/Demux.luau @@ -1,15 +1,6 @@ --!strict -- containers/Demux: the indexed Types.Demuxer every video container builds (Matroska, AVI, MPEG-TS / PS, Y4M, MP4). --- New in rosetta. A container parses its file once into an index of packets (track, pts, dts, duration, --- keyframe, and the bytes as an offset + length into the source buffer or a buffer of their own); this module gives --- that index the Demuxer interface: per-track cursors (a video decoder and an audio decoder read the same file --- independently), a file-order cursor, keyframe seeking, packet copies on demand. --- --- local d = Demux.new("mkv", tracks, src, meta) --- d:add(trackId, pts, dts, duration, keyframe, offset, length) -- in file order --- d:addData(trackId, pts, dts, duration, keyframe, buffer) -- bytes that are not one range of src --- d:finish() -- durations, per-track indexes --- for p in d:packets(1) do ... end; d:read(); d:seek(12.5); d:count(1); d:get(1, i) +-- New in rosetta. Packets are offset + length into the source buffer (or own bytes); per-track cursors are independent. local Types = require("@rosetta/Types") local Demux = {} @@ -42,7 +33,6 @@ export type Demux = typeof(setmetatable( D )) --- new(format, tracks, src, meta?) -> an empty index function Demux.new(format: string, tracks: { Types.Track }, src: buffer, meta: { [string]: any }?): Demux return setmetatable({ kind = "demuxer" :: "demuxer", @@ -68,7 +58,6 @@ function Demux.new(format: string, tracks: { Types.Track }, src: buffer, meta: { }, D) end --- add(track, pts, dts?, duration?, keyframe, offset, length): a packet whose bytes are src[offset, offset + length) function D.add(self: Demux, track: number, pts: number, dts: number?, duration: number?, keyframe: boolean, offset: number, length: number) local i = self.n + 1 self.n = i @@ -81,13 +70,11 @@ function D.add(self: Demux, track: number, pts: number, dts: number?, duration: self.len[i] = length end --- addData(track, pts, dts?, duration?, keyframe, data): a packet with its own bytes function D.addData(self: Demux, track: number, pts: number, dts: number?, duration: number?, keyframe: boolean, data: buffer) self:add(track, pts, dts, duration, keyframe, -1, buffer.len(data)) self.data[self.n] = data end --- setExtra(key, value): a field for the last packet added (Packet[key] = value: discardPadding, ...) function D.setExtra(self: Demux, key: string, value: any) local e = self.extra[self.n] if not e then @@ -97,8 +84,6 @@ function D.setExtra(self: Demux, key: string, value: any) e[key] = value end --- finish(): builds the per-track indexes, fills missing packet durations from the next packet's pts (or the track's --- frame rate), and the track / file durations (seconds) where the container gave none function D.finish(self: Demux) local byTrack: { [number]: { number } } = {} for _, t in self.tracks do @@ -158,7 +143,6 @@ function D.finish(self: Demux) self.startTime = if start ~= math.huge and start > 0 then start else 0 end --- the Packet for index entry i (a copy of its bytes) function D._packet(self: Demux, i: number): Types.Packet local data = self.data[i] if not data then @@ -190,7 +174,6 @@ function D._packet(self: Demux, i: number): Types.Packet return p end --- read(track?) -> the next Packet of the track (or of any track, in file order) | nil at the end function D.read(self: Demux, track: number?): Types.Packet? if track == nil then local i = self.gcur @@ -213,14 +196,12 @@ function D.read(self: Demux, track: number?): Types.Packet? return self:_packet(i) end --- packets(track?) -> an iterator of Packets from the current position (advancing it) function D.packets(self: Demux, track: number?): () -> Types.Packet? return function(): Types.Packet? return self:read(track) end end --- count(track) -> number of packets; get(track, k) -> the k-th packet (1-based) without moving cursors function D.count(self: Demux, track: number): number local list = self.byTrack[track] return if list then #list else 0 @@ -234,7 +215,6 @@ function D.get(self: Demux, track: number, k: number): Types.Packet? return self:_packet(i) end --- info(track, k) -> pts, dts, duration, keyframe, length of the k-th packet (no copy) function D.info(self: Demux, track: number, k: number): (number?, number?, number?, boolean?, number?) local list = self.byTrack[track] local i = list and list[k] @@ -244,7 +224,6 @@ function D.info(self: Demux, track: number, k: number): (number?, number?, numbe return self.pts[i], self.dts[i], self.dur[i], self.key[i], self.len[i] end --- rewind(track?): a track's cursor (or every cursor) back to the start function D.rewind(self: Demux, track: number?) if track then self.cur[track] = 1 @@ -256,7 +235,6 @@ function D.rewind(self: Demux, track: number?) self.gcur = 1 end --- seek(seconds): each track to its last keyframe at or before the time (the file-order cursor to the earliest) function D.seek(self: Demux, seconds: number) local g = self.n + 1 for _, t in self.tracks do @@ -279,7 +257,6 @@ function D.seek(self: Demux, seconds: number) self.gcur = if g > self.n then 1 else g end --- track(kind) -> the first default track of the kind, else the first of the kind function D.track(self: Demux, kind: string): Types.Track? local first = nil for _, t in self.tracks do diff --git a/src/Rosetta/containers/Ebml/init.luau b/src/Rosetta/containers/Ebml/init.luau index 1a3c590..494c66b 100644 --- a/src/Rosetta/containers/Ebml/init.luau +++ b/src/Rosetta/containers/Ebml/init.luau @@ -4,11 +4,6 @@ -- an all-ones size is "unknown"), children iteration, and the value types (unsigned / signed integers, floats, -- strings, binary). Writing: a byte builder with nested master elements whose 8-byte sizes are patched on close, and -- minimal-length value elements. --- --- local id, size, data = Ebml.header(buf, pos, limit) -- size = -1: unknown; nil when malformed / truncated --- for id, off, size in Ebml.children(buf, off, limit) do ... end --- local w = Ebml.writer(); local m = w:open(0x18538067); w:uint(0xD7, 1); w:close(m); local out = w:finish() --- -- Never throws: malformed input gives nil (readers) or stops iteration. local Bytes = require("@rosetta/core/Bytes") @@ -18,7 +13,6 @@ local Ebml = {} Ebml.UNKNOWN = -1 --- vint(buf, pos, limit) -> value (marker removed), length | nil. All-ones values (unknown sizes) return -1. function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?) if pos >= limit then return nil, nil @@ -51,7 +45,6 @@ function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?) return v, len end --- svint(buf, pos, limit) -> a signed vint (EBML lacing deltas) | nil function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?) local v, len = Ebml.vint(buf, pos, limit) if v == nil or len == nil then @@ -63,7 +56,6 @@ function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?) return v - (2 ^ (7 * len - 1) - 1), len end --- id(buf, pos, limit) -> the element ID with its marker bits, length | nil (IDs are 1..4 bytes) function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?) if pos >= limit then return nil, nil @@ -80,7 +72,6 @@ function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?) return v, len end --- header(buf, pos, limit) -> id, size (-1 = unknown), data offset | nil function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number?, number?) local id, il = Ebml.id(buf, pos, limit) if not id or not il then @@ -93,8 +84,6 @@ function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number? return id, size, pos + il + sl end --- children(buf, off, limit) -> iterator of id, data offset, size (an unknown size runs to limit; a size past the --- limit is clipped; a malformed header ends the iteration) function Ebml.children(buf: buffer, off: number, limit: number): () -> (number?, number, number) local pos = off return function(): (number?, number, number) @@ -114,7 +103,6 @@ function Ebml.children(buf: buffer, off: number, limit: number): () -> (number?, end end --- uint(buf, off, len) / int / float / string: element values (0 / "" for empty elements) function Ebml.uint(buf: buffer, off: number, len: number): number local v = 0 for i = 0, math.min(len, 8) - 1 do @@ -156,18 +144,15 @@ function Ebml.binary(buf: buffer, off: number, len: number): buffer return b end --- ------------------------------------------------------------------------------------------------ writing local W = {} W.__index = W export type Writer = typeof(setmetatable({} :: { w: Bytes.Writer }, W)) --- writer(capacity?) -> an EBML byte builder function Ebml.writer(capacity: number?): Writer return setmetatable({ w = Bytes.writer(capacity or 65536) }, W) end --- idBytes(id) -> the ID's bytes as written local function writeId(w: Bytes.Writer, id: number) if id >= 0x1000000 then w:u32be(id) @@ -180,7 +165,6 @@ local function writeId(w: Bytes.Writer, id: number) end end --- size as a minimal vint (n bytes: force a length) local function writeSize(w: Bytes.Writer, size: number, n: number?) local len = n or 1 if not n then @@ -200,7 +184,6 @@ local function writeSize(w: Bytes.Writer, size: number, n: number?) end Ebml.writeSize = writeSize --- vintBytes(v, n?) -> the vint encoding of v as a string (lacing, block track numbers) function Ebml.vintString(v: number, n: number?): string local w = Bytes.writer(8) writeSize(w, v, n) @@ -211,7 +194,6 @@ function W.tell(self: Writer): number return self.w:tell() end --- open(id) -> a handle for close(): writes the ID and an 8-byte size placeholder function W.open(self: Writer, id: number): number writeId(self.w, id) local at = self.w:tell() @@ -221,7 +203,6 @@ function W.open(self: Writer, id: number): number return at end --- close(handle): patches the size of the element opened at handle function W.close(self: Writer, at: number) local w = self.w local v = w:tell() - at - 8 @@ -232,7 +213,6 @@ function W.close(self: Writer, at: number) end end --- unknownSize(id): a master element with an unknown size (live streams) function W.openUnknown(self: Writer, id: number) writeId(self.w, id) self.w:u8(0x01) @@ -289,7 +269,6 @@ function W.string(self: Writer, id: number, s: string) self.w:string(s) end --- binary(id, buf, off?, len?) function W.binary(self: Writer, id: number, b: buffer, off: number?, len: number?) local o = off or 0 local n = len or (buffer.len(b) - o) @@ -298,7 +277,6 @@ function W.binary(self: Writer, id: number, b: buffer, off: number?, len: number self.w:bytes(b, o, n) end --- raw bytes (a pre-built element, block headers) function W.raw(self: Writer, s: string) self.w:string(s) end @@ -306,13 +284,11 @@ function W.rawBuffer(self: Writer, b: buffer, off: number?, len: number?) local o = off or 0 self.w:bytes(b, o, len or (buffer.len(b) - o)) end --- element header for a binary element whose payload follows in pieces: id + size function W.header(self: Writer, id: number, size: number) writeId(self.w, id) writeSize(self.w, size) end --- void(n): an EBML Void element of exactly n bytes (n >= 2), for reserved space function W.void(self: Writer, n: number) self.w:u8(0xEC) if n - 2 < 127 then diff --git a/src/Rosetta/containers/MpegPs/init.luau b/src/Rosetta/containers/MpegPs/init.luau index 85099cb..257ee6a 100644 --- a/src/Rosetta/containers/MpegPs/init.luau +++ b/src/Rosetta/containers/MpegPs/init.luau @@ -4,12 +4,7 @@ -- stream map (stream types), padding, and PES packets of the video (0xE0..0xEF), audio (0xC0..0xDF) and private -- stream 1 (0xBD: AC-3 / DTS / LPCM / subpicture sub-streams) ids with their PTS / DTS (both PES header layouts, -- 33-bit timestamps unwrapped). Data between packs that is not a start code is skipped (resync). --- --- local ps, err = MpegPs.read(buf, base, limit) --- ps.streams[id] = { id, kind, codec, streamType, pes = { { pts?, dts?, offset, size } } } (id: stream_id, or --- 0xBD00 + sub-stream id for private stream 1); ps.order = the ids in order of appearance --- --- Never throws: malformed packets are skipped and counted in ps.errors. +-- Never throws: malformed packets are skipped (ps.errors). Private stream 1 sub-streams are keyed 0xBD00 + their id. local Err = require("@rosetta/core/Err") local MpegEs = require("@rosetta/containers/MpegEs") @@ -44,7 +39,6 @@ local function ts(b: buffer, p: number): number return v end --- read(buf, base, limit) -> File | nil, Err function MpegPs.read(b: buffer, base: number, limit: number): (File?, Err.Err?) local file: File = { streams = {}, order = {}, mpeg2 = false, errors = 0 } local types: { [number]: number } = {} diff --git a/src/Rosetta/containers/Riff/init.luau b/src/Rosetta/containers/Riff/init.luau index 00ee8a7..e8e32d8 100644 --- a/src/Rosetta/containers/Riff/init.luau +++ b/src/Rosetta/containers/Riff/init.luau @@ -2,18 +2,6 @@ -- containers/Riff: the RIFF chunk container (RIFF, RIFX big-endian, RF64 / BW64 with the ds64 chunk), read leniently -- and written. Shared by audio/wav and the other RIFF forms (AVI, WebP, ANI, ...). The chunk walker comes from -- wav-luau fd9a909 (same author, no third-party code), now the RIFF layout of containers/Chunks; the writer is new. --- --- local f, err = Riff.read(r, { openEnded = { data = true } }) -- r: a core/Bytes Reader or a buffer --- f.container, f.form, f.bigEndian -- "RIFF" | "RIFX" | "RF64", "WAVE", false --- for _, c in f.chunks do print(c.id, c.offset, c.size, c.avail) end -- offsets are absolute in f.buf --- local c = Riff.find(f, "fmt ") --- local subs = Riff.list(f, c) -- the sub-chunks of a LIST body (after its type) --- --- local w = Riff.writer("WAVE", { rf64 = "auto" }) -- or bigEndian = true (RIFX) --- w:chunk("fmt ", fmtBody) w:chunk("data", samples) w:list("INFO", { { "INAM", "title" } }) --- local file = w:finish() -- RF64 + ds64 when a size passes 4 GiB --- local parts = w:finish({ parts = true }) -- { buffer }: no copy, for files over 1 GiB --- -- Lenient reading (what real files need): an odd-sized chunk without its pad byte is detected; a chunk declared past -- the end keeps what is there (avail < size, truncated = true) and ends the walk; for the ids in opts.openEnded a -- size of 0 or 0xFFFFFFFF means "up to the end of the file" (streamed writers) and ends the walk; RF64 sizes of @@ -52,13 +40,11 @@ export type ReadOptions = { maxChunks: number?, -- stop after this many chunks (default 100000) } --- ------------------------------------------------------------------------------------------------ read local function u32(b: buffer, o: number, be: boolean): number local v = readu32(b, o) return if be then bit32.byteswap(v) else v end --- sniff(r) -> the form type ("WAVE", ...) when r starts with a RIFF / RIFX / RF64 / BW64 header, else nil function Riff.sniff(r: any): string? local magic = r:peek(4) if magic ~= "RIFF" and magic ~= "RIFX" and magic ~= "RF64" and magic ~= "BW64" then @@ -68,8 +54,6 @@ function Riff.sniff(r: any): string? return if #form == 4 then form else nil end --- walk(b, p, limit, be, opts, ds64) -> the chunks of [p, limit): containers/Chunks with the RIFF layout (padding to 2, --- lenient missing pad bytes, open-ended ids, RF64 sizes from ds64) local function walk(b: buffer, p: number, limit: number, be: boolean, opts: ReadOptions?, ds64: any): { Chunk } local layout: Chunks.Layout = { bigEndian = be, @@ -89,7 +73,6 @@ local function walk(b: buffer, p: number, limit: number, be: boolean, opts: Read return Chunks.walk(b, p, limit, layout) :: any end --- read(src, opts?) -> File | nil, Err. src: a core/Bytes Reader (its range) or a buffer. function Riff.read(src: any, opts: ReadOptions?): (File?, Err.Err?) local r = if type(src) == "buffer" then Bytes.reader(src) else src if type(r) ~= "table" or type(r.buf) ~= "buffer" then @@ -147,7 +130,6 @@ function Riff.read(src: any, opts: ReadOptions?): (File?, Err.Err?) return f, nil end --- find(file, id, from?) -> the first chunk with that id (after index from), and its index function Riff.find(f: File, id: string, from: number?): (Chunk?, number?) for i = (from or 0) + 1, #f.chunks do if f.chunks[i].id == id then @@ -157,7 +139,6 @@ function Riff.find(f: File, id: string, from: number?): (Chunk?, number?) return nil, nil end --- listType(file, chunk) -> the 4-byte type of a LIST / RIFF chunk ("INFO", "movi", ...) or nil function Riff.listType(f: File, c: Chunk): string? if c.avail < 4 then return nil @@ -165,7 +146,6 @@ function Riff.listType(f: File, c: Chunk): string? return readstring(f.buf, c.offset, 4) end --- list(file, chunk, opts?) -> the sub-chunks of a LIST body (after its 4-byte type) function Riff.list(f: File, c: Chunk, opts: ReadOptions?): { Chunk } if c.avail < 4 then return {} @@ -173,12 +153,10 @@ function Riff.list(f: File, c: Chunk, opts: ReadOptions?): { Chunk } return walk(f.buf, c.offset + 4, c.offset + c.avail, f.bigEndian, opts, nil) end --- body(file, chunk) -> a Reader over the chunk's available body function Riff.body(f: File, c: Chunk): Bytes.Reader return Bytes.reader(f.buf, c.offset, c.avail) end --- u16 / u32 in the file's byte order (for chunk bodies) function Riff.u16(f: File, o: number): number if f.bigEndian then return readu8(f.buf, o) * 256 + readu8(f.buf, o + 1) @@ -189,7 +167,6 @@ function Riff.u32(f: File, o: number): number return u32(f.buf, o, f.bigEndian) end --- ------------------------------------------------------------------------------------------------ write local Writer = {} Writer.__index = Writer @@ -200,7 +177,6 @@ export type WriteOptions = { rf64Threshold: number?, -- "auto" switches to RF64 when the RIFF size would pass this (default 0xFFFFFFFF) } --- writer(form, opts?) -> Writer: collects chunks; finish() lays out the file function Riff.writer(form: string, opts: WriteOptions?): any return setmetatable({ form = string.sub(form .. " ", 1, 4), @@ -212,8 +188,6 @@ function Riff.writer(form: string, opts: WriteOptions?): any }, Writer) end --- chunk(id, body, offset?, length?): body is a buffer or a string (a range of a buffer with offset / length), or a --- list of buffers written one after the other (a data chunk bigger than one buffer) function Writer.chunk(self: any, id: string, body: any, offset: number?, length: number?) local parts = {} local n = 0 @@ -231,7 +205,6 @@ function Writer.chunk(self: any, id: string, body: any, offset: number?, length: table.insert(self.chunks, { id = string.sub(id .. " ", 1, 4), parts = parts, length = n }) end --- list(type, items): a LIST chunk of { id, string-or-buffer } sub-chunks (strings get a NUL terminator, INFO style) function Writer.list(self: any, listType: string, items: { { any } }) local w = Bytes.writer() w:string(string.sub(listType .. " ", 1, 4)) @@ -253,7 +226,6 @@ function Writer.list(self: any, listType: string, items: { { any } }) self:chunk("LIST", w:finish()) end --- finish(opts?) -> buffer | nil, Err; with opts.parts: { buffer } (the header pieces and the chunk bodies, not copied) function Writer.finish(self: any, opts: { parts: boolean? }?): (any, Err.Err?) local total = 4 -- the form type local big = false