comments: containers Archive / Demux / Ebml / MpegPs / Riff
This commit is contained in:
1 parent
e5a8e62fbc
commit
a355bf3ca3
5 files changed
+6
-113
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user